diff options
Diffstat (limited to 'arch/openrisc/kernel/process.c')
-rw-r--r-- | arch/openrisc/kernel/process.c | 163 |
1 files changed, 56 insertions, 107 deletions
diff --git a/arch/openrisc/kernel/process.c b/arch/openrisc/kernel/process.c index c35f3ab1a8d3..e0874b8e09e4 100644 --- a/arch/openrisc/kernel/process.c +++ b/arch/openrisc/kernel/process.c | |||
@@ -109,66 +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[3] = (unsigned long)current; /* arg to schedule_tail */ | ||
169 | kregs->gpr[10] = (unsigned long)task_thread_info(p); | ||
170 | kregs->gpr[9] = (unsigned long)ret_from_fork; | 184 | kregs->gpr[9] = (unsigned long)ret_from_fork; |
171 | 185 | ||
186 | task_thread_info(p)->ksp = (unsigned long)kregs; | ||
187 | |||
172 | return 0; | 188 | return 0; |
173 | } | 189 | } |
174 | 190 | ||
@@ -177,16 +193,14 @@ copy_thread(unsigned long clone_flags, unsigned long usp, | |||
177 | */ | 193 | */ |
178 | 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) |
179 | { | 195 | { |
180 | unsigned long sr = regs->sr & ~SPR_SR_SM; | 196 | unsigned long sr = mfspr(SPR_SR) & ~SPR_SR_SM; |
181 | 197 | ||
182 | set_fs(USER_DS); | 198 | set_fs(USER_DS); |
183 | memset(regs->gpr, 0, sizeof(regs->gpr)); | 199 | memset(regs, 0, sizeof(struct pt_regs)); |
184 | 200 | ||
185 | regs->pc = pc; | 201 | regs->pc = pc; |
186 | regs->sr = sr; | 202 | regs->sr = sr; |
187 | regs->sp = sp; | 203 | regs->sp = sp; |
188 | |||
189 | /* printk("start thread, ksp = %lx\n", current_thread_info()->ksp);*/ | ||
190 | } | 204 | } |
191 | 205 | ||
192 | /* Fill in the fpu structure for a core dump. */ | 206 | /* Fill in the fpu structure for a core dump. */ |
@@ -237,74 +251,9 @@ void dump_elf_thread(elf_greg_t *dest, struct pt_regs* regs) | |||
237 | dest[35] = 0; | 251 | dest[35] = 0; |
238 | } | 252 | } |
239 | 253 | ||
240 | extern void _kernel_thread_helper(void); | ||
241 | |||
242 | void __noreturn kernel_thread_helper(int (*fn) (void *), void *arg) | ||
243 | { | ||
244 | do_exit(fn(arg)); | ||
245 | } | ||
246 | |||
247 | /* | ||
248 | * Create a kernel thread. | ||
249 | */ | ||
250 | int kernel_thread(int (*fn) (void *), void *arg, unsigned long flags) | ||
251 | { | ||
252 | struct pt_regs regs; | ||
253 | |||
254 | memset(®s, 0, sizeof(regs)); | ||
255 | |||
256 | regs.gpr[20] = (unsigned long)fn; | ||
257 | regs.gpr[22] = (unsigned long)arg; | ||
258 | regs.sr = mfspr(SPR_SR); | ||
259 | regs.pc = (unsigned long)_kernel_thread_helper; | ||
260 | |||
261 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, | ||
262 | 0, ®s, 0, NULL, NULL); | ||
263 | } | ||
264 | |||
265 | /* | ||
266 | * sys_execve() executes a new program. | ||
267 | */ | ||
268 | asmlinkage long _sys_execve(const char __user *name, | ||
269 | const char __user * const __user *argv, | ||
270 | const char __user * const __user *envp, | ||
271 | struct pt_regs *regs) | ||
272 | { | ||
273 | int error; | ||
274 | struct filename *filename; | ||
275 | |||
276 | filename = getname(name); | ||
277 | error = PTR_ERR(filename); | ||
278 | |||
279 | if (IS_ERR(filename)) | ||
280 | goto out; | ||
281 | |||
282 | error = do_execve(filename->name, argv, envp, regs); | ||
283 | putname(filename); | ||
284 | |||
285 | out: | ||
286 | return error; | ||
287 | } | ||
288 | |||
289 | unsigned long get_wchan(struct task_struct *p) | 254 | unsigned long get_wchan(struct task_struct *p) |
290 | { | 255 | { |
291 | /* TODO */ | 256 | /* TODO */ |
292 | 257 | ||
293 | return 0; | 258 | return 0; |
294 | } | 259 | } |
295 | |||
296 | int kernel_execve(const char *filename, char *const argv[], char *const envp[]) | ||
297 | { | ||
298 | register long __res asm("r11") = __NR_execve; | ||
299 | register long __a asm("r3") = (long)(filename); | ||
300 | register long __b asm("r4") = (long)(argv); | ||
301 | register long __c asm("r5") = (long)(envp); | ||
302 | __asm__ volatile ("l.sys 1" | ||
303 | : "=r" (__res), "=r"(__a), "=r"(__b), "=r"(__c) | ||
304 | : "0"(__res), "1"(__a), "2"(__b), "3"(__c) | ||
305 | : "r6", "r7", "r8", "r12", "r13", "r15", | ||
306 | "r17", "r19", "r21", "r23", "r25", "r27", | ||
307 | "r29", "r31"); | ||
308 | __asm__ volatile ("l.nop"); | ||
309 | return __res; | ||
310 | } | ||