diff options
author | Jonas Bonn <jonas@southpole.se> | 2012-10-19 12:07:44 -0400 |
---|---|---|
committer | Jonas Bonn <jonas@southpole.se> | 2012-10-19 12:32:36 -0400 |
commit | cbf23cf1b96819599f6a1b9658d1bf3a97c6ff15 (patch) | |
tree | 4ed2583ded570499617cc6fd9caffb4f17eddbd5 | |
parent | 287ad220cd8b5a9d29f71c78f6e4051093f051fc (diff) |
openrisc: use generic kernel_thread/kernel_execve
Signed-off-by: Jonas Bonn <jonas@southpole.se>
-rw-r--r-- | arch/openrisc/Kconfig | 2 | ||||
-rw-r--r-- | arch/openrisc/kernel/entry.S | 30 | ||||
-rw-r--r-- | arch/openrisc/kernel/process.c | 138 |
3 files changed, 68 insertions, 102 deletions
diff --git a/arch/openrisc/Kconfig b/arch/openrisc/Kconfig index 05f2ba41ff1a..e7f1a2993f78 100644 --- a/arch/openrisc/Kconfig +++ b/arch/openrisc/Kconfig | |||
@@ -22,6 +22,8 @@ config OPENRISC | |||
22 | select GENERIC_STRNCPY_FROM_USER | 22 | select GENERIC_STRNCPY_FROM_USER |
23 | select GENERIC_STRNLEN_USER | 23 | select GENERIC_STRNLEN_USER |
24 | select MODULES_USE_ELF_RELA | 24 | select MODULES_USE_ELF_RELA |
25 | select GENERIC_KERNEL_THREAD | ||
26 | select GENERIC_KERNEL_EXECVE | ||
25 | 27 | ||
26 | config MMU | 28 | config MMU |
27 | def_bool y | 29 | def_bool y |
diff --git a/arch/openrisc/kernel/entry.S b/arch/openrisc/kernel/entry.S index 374e63e7c9f6..dce86aef8aba 100644 --- a/arch/openrisc/kernel/entry.S +++ b/arch/openrisc/kernel/entry.S | |||
@@ -894,6 +894,16 @@ ENTRY(ret_from_fork) | |||
894 | l.jal schedule_tail | 894 | l.jal schedule_tail |
895 | l.nop | 895 | l.nop |
896 | 896 | ||
897 | /* Check if we are a kernel thread */ | ||
898 | l.sfeqi r20,0 | ||
899 | l.bf 1f | ||
900 | l.nop | ||
901 | |||
902 | /* ...we are a kernel thread so invoke the requested callback */ | ||
903 | l.jalr r20 | ||
904 | l.or r3,r22,r0 | ||
905 | |||
906 | 1: | ||
897 | /* _syscall_returns expect r11 to contain return value */ | 907 | /* _syscall_returns expect r11 to contain return value */ |
898 | l.lwz r11,PT_GPR11(r1) | 908 | l.lwz r11,PT_GPR11(r1) |
899 | 909 | ||
@@ -915,26 +925,6 @@ ENTRY(ret_from_fork) | |||
915 | l.j _syscall_return | 925 | l.j _syscall_return |
916 | l.nop | 926 | l.nop |
917 | 927 | ||
918 | /* Since syscalls don't save call-clobbered registers, the args to | ||
919 | * kernel_thread_helper will need to be passed through callee-saved | ||
920 | * registers and copied to the parameter registers when the thread | ||
921 | * begins running. | ||
922 | * | ||
923 | * See arch/openrisc/kernel/process.c: | ||
924 | * The args are passed as follows: | ||
925 | * arg1 (r3) : passed in r20 | ||
926 | * arg2 (r4) : passed in r22 | ||
927 | */ | ||
928 | |||
929 | ENTRY(_kernel_thread_helper) | ||
930 | l.or r3,r20,r0 | ||
931 | l.or r4,r22,r0 | ||
932 | l.movhi r31,hi(kernel_thread_helper) | ||
933 | l.ori r31,r31,lo(kernel_thread_helper) | ||
934 | l.jr r31 | ||
935 | l.nop | ||
936 | |||
937 | |||
938 | /* ========================================================[ switch ] === */ | 928 | /* ========================================================[ switch ] === */ |
939 | 929 | ||
940 | /* | 930 | /* |
diff --git a/arch/openrisc/kernel/process.c b/arch/openrisc/kernel/process.c index ad26d5af2649..a0f467e438fc 100644 --- a/arch/openrisc/kernel/process.c +++ b/arch/openrisc/kernel/process.c | |||
@@ -109,65 +109,82 @@ void release_thread(struct task_struct *dead_task) | |||
109 | */ | 109 | */ |
110 | extern asmlinkage void ret_from_fork(void); | 110 | extern asmlinkage void ret_from_fork(void); |
111 | 111 | ||
112 | /* | ||
113 | * copy_thread | ||
114 | * @clone_flags: flags | ||
115 | * @usp: user stack pointer or fn for kernel thread | ||
116 | * @arg: arg to fn for kernel thread; always NULL for userspace thread | ||
117 | * @p: the newly created task | ||
118 | * @regs: CPU context to copy for userspace thread; always NULL for kthread | ||
119 | * | ||
120 | * At the top of a newly initialized kernel stack are two stacked pt_reg | ||
121 | * structures. The first (topmost) is the userspace context of the thread. | ||
122 | * The second is the kernelspace context of the thread. | ||
123 | * | ||
124 | * A kernel thread will not be returning to userspace, so the topmost pt_regs | ||
125 | * struct can be uninitialized; it _does_ need to exist, though, because | ||
126 | * a kernel thread can become a userspace thread by doing a kernel_execve, in | ||
127 | * which case the topmost context will be initialized and used for 'returning' | ||
128 | * to userspace. | ||
129 | * | ||
130 | * The second pt_reg struct needs to be initialized to 'return' to | ||
131 | * ret_from_fork. A kernel thread will need to set r20 to the address of | ||
132 | * a function to call into (with arg in r22); userspace threads need to set | ||
133 | * r20 to NULL in which case ret_from_fork will just continue a return to | ||
134 | * userspace. | ||
135 | * | ||
136 | * A kernel thread 'fn' may return; this is effectively what happens when | ||
137 | * kernel_execve is called. In that case, the userspace pt_regs must have | ||
138 | * been initialized (which kernel_execve takes care of, see start_thread | ||
139 | * below); ret_from_fork will then continue its execution causing the | ||
140 | * 'kernel thread' to return to userspace as a userspace thread. | ||
141 | */ | ||
142 | |||
112 | int | 143 | int |
113 | copy_thread(unsigned long clone_flags, unsigned long usp, | 144 | copy_thread(unsigned long clone_flags, unsigned long usp, |
114 | unsigned long unused, struct task_struct *p, struct pt_regs *regs) | 145 | unsigned long arg, struct task_struct *p, struct pt_regs *regs) |
115 | { | 146 | { |
116 | struct pt_regs *childregs; | 147 | struct pt_regs *userregs; |
117 | struct pt_regs *kregs; | 148 | struct pt_regs *kregs; |
118 | unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE; | 149 | unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE; |
119 | struct thread_info *ti; | ||
120 | unsigned long top_of_kernel_stack; | 150 | unsigned long top_of_kernel_stack; |
121 | 151 | ||
122 | top_of_kernel_stack = sp; | 152 | top_of_kernel_stack = sp; |
123 | 153 | ||
124 | p->set_child_tid = p->clear_child_tid = NULL; | 154 | p->set_child_tid = p->clear_child_tid = NULL; |
125 | 155 | ||
126 | /* Copy registers */ | 156 | /* Locate userspace context on stack... */ |
127 | /* redzone */ | 157 | sp -= STACK_FRAME_OVERHEAD; /* redzone */ |
128 | sp -= STACK_FRAME_OVERHEAD; | ||
129 | sp -= sizeof(struct pt_regs); | 158 | sp -= sizeof(struct pt_regs); |
130 | childregs = (struct pt_regs *)sp; | 159 | userregs = (struct pt_regs *) sp; |
131 | 160 | ||
132 | /* Copy parent registers */ | 161 | /* ...and kernel context */ |
133 | *childregs = *regs; | 162 | sp -= STACK_FRAME_OVERHEAD; /* redzone */ |
163 | sp -= sizeof(struct pt_regs); | ||
164 | kregs = (struct pt_regs *)sp; | ||
134 | 165 | ||
135 | if ((childregs->sr & SPR_SR_SM) == 1) { | 166 | if (unlikely(p->flags & PF_KTHREAD)) { |
136 | /* for kernel thread, set `current_thread_info' | 167 | memset(kregs, 0, sizeof(struct pt_regs)); |
137 | * and stackptr in new task | 168 | kregs->gpr[20] = usp; /* fn, kernel thread */ |
138 | */ | 169 | kregs->gpr[22] = arg; |
139 | childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE; | ||
140 | childregs->gpr[10] = (unsigned long)task_thread_info(p); | ||
141 | } else { | 170 | } else { |
142 | childregs->sp = usp; | 171 | *userregs = *regs; |
143 | } | ||
144 | |||
145 | childregs->gpr[11] = 0; /* Result from fork() */ | ||
146 | 172 | ||
147 | /* | 173 | userregs->sp = usp; |
148 | * The way this works is that at some point in the future | 174 | userregs->gpr[11] = 0; /* Result from fork() */ |
149 | * some task will call _switch to switch to the new task. | ||
150 | * That will pop off the stack frame created below and start | ||
151 | * the new task running at ret_from_fork. The new task will | ||
152 | * do some house keeping and then return from the fork or clone | ||
153 | * system call, using the stack frame created above. | ||
154 | */ | ||
155 | /* redzone */ | ||
156 | sp -= STACK_FRAME_OVERHEAD; | ||
157 | sp -= sizeof(struct pt_regs); | ||
158 | kregs = (struct pt_regs *)sp; | ||
159 | 175 | ||
160 | ti = task_thread_info(p); | 176 | kregs->gpr[20] = 0; /* Userspace thread */ |
161 | ti->ksp = sp; | 177 | } |
162 | 178 | ||
163 | /* kregs->sp must store the location of the 'pre-switch' kernel stack | 179 | /* |
164 | * pointer... for a newly forked process, this is simply the top of | 180 | * _switch wants the kernel stack page in pt_regs->sp so that it |
165 | * the kernel stack. | 181 | * can restore it to thread_info->ksp... see _switch for details. |
166 | */ | 182 | */ |
167 | kregs->sp = top_of_kernel_stack; | 183 | kregs->sp = top_of_kernel_stack; |
168 | kregs->gpr[10] = (unsigned long)task_thread_info(p); | ||
169 | kregs->gpr[9] = (unsigned long)ret_from_fork; | 184 | kregs->gpr[9] = (unsigned long)ret_from_fork; |
170 | 185 | ||
186 | task_thread_info(p)->ksp = (unsigned long)kregs; | ||
187 | |||
171 | return 0; | 188 | return 0; |
172 | } | 189 | } |
173 | 190 | ||
@@ -176,16 +193,14 @@ copy_thread(unsigned long clone_flags, unsigned long usp, | |||
176 | */ | 193 | */ |
177 | void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) | 194 | void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) |
178 | { | 195 | { |
179 | unsigned long sr = regs->sr & ~SPR_SR_SM; | 196 | unsigned long sr = mfspr(SPR_SR) & ~SPR_SR_SM; |
180 | 197 | ||
181 | set_fs(USER_DS); | 198 | set_fs(USER_DS); |
182 | memset(regs->gpr, 0, sizeof(regs->gpr)); | 199 | memset(regs, 0, sizeof(struct pt_regs)); |
183 | 200 | ||
184 | regs->pc = pc; | 201 | regs->pc = pc; |
185 | regs->sr = sr; | 202 | regs->sr = sr; |
186 | regs->sp = sp; | 203 | regs->sp = sp; |
187 | |||
188 | /* printk("start thread, ksp = %lx\n", current_thread_info()->ksp);*/ | ||
189 | } | 204 | } |
190 | 205 | ||
191 | /* Fill in the fpu structure for a core dump. */ | 206 | /* Fill in the fpu structure for a core dump. */ |
@@ -236,31 +251,6 @@ void dump_elf_thread(elf_greg_t *dest, struct pt_regs* regs) | |||
236 | dest[35] = 0; | 251 | dest[35] = 0; |
237 | } | 252 | } |
238 | 253 | ||
239 | extern void _kernel_thread_helper(void); | ||
240 | |||
241 | void __noreturn kernel_thread_helper(int (*fn) (void *), void *arg) | ||
242 | { | ||
243 | do_exit(fn(arg)); | ||
244 | } | ||
245 | |||
246 | /* | ||
247 | * Create a kernel thread. | ||
248 | */ | ||
249 | int kernel_thread(int (*fn) (void *), void *arg, unsigned long flags) | ||
250 | { | ||
251 | struct pt_regs regs; | ||
252 | |||
253 | memset(®s, 0, sizeof(regs)); | ||
254 | |||
255 | regs.gpr[20] = (unsigned long)fn; | ||
256 | regs.gpr[22] = (unsigned long)arg; | ||
257 | regs.sr = mfspr(SPR_SR); | ||
258 | regs.pc = (unsigned long)_kernel_thread_helper; | ||
259 | |||
260 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, | ||
261 | 0, ®s, 0, NULL, NULL); | ||
262 | } | ||
263 | |||
264 | /* | 254 | /* |
265 | * sys_execve() executes a new program. | 255 | * sys_execve() executes a new program. |
266 | */ | 256 | */ |
@@ -291,19 +281,3 @@ unsigned long get_wchan(struct task_struct *p) | |||
291 | 281 | ||
292 | return 0; | 282 | return 0; |
293 | } | 283 | } |
294 | |||
295 | int kernel_execve(const char *filename, char *const argv[], char *const envp[]) | ||
296 | { | ||
297 | register long __res asm("r11") = __NR_execve; | ||
298 | register long __a asm("r3") = (long)(filename); | ||
299 | register long __b asm("r4") = (long)(argv); | ||
300 | register long __c asm("r5") = (long)(envp); | ||
301 | __asm__ volatile ("l.sys 1" | ||
302 | : "=r" (__res), "=r"(__a), "=r"(__b), "=r"(__c) | ||
303 | : "0"(__res), "1"(__a), "2"(__b), "3"(__c) | ||
304 | : "r6", "r7", "r8", "r12", "r13", "r15", | ||
305 | "r17", "r19", "r21", "r23", "r25", "r27", | ||
306 | "r29", "r31"); | ||
307 | __asm__ volatile ("l.nop"); | ||
308 | return __res; | ||
309 | } | ||