2 * arch/sh/kernel/process_64.c
4 * This file handles the architecture-dependent parts of process handling..
6 * Copyright (C) 2000, 2001 Paolo Alberelli
7 * Copyright (C) 2003 - 2007 Paul Mundt
8 * Copyright (C) 2003, 2004 Richard Curnow
10 * Started from SH3/4 version:
11 * Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
13 * In turn started from i386 version:
14 * Copyright (C) 1995 Linus Torvalds
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
22 #include <linux/ptrace.h>
23 #include <linux/reboot.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
28 #include <asm/syscalls.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/mmu_context.h>
33 #include <asm/switch_to.h>
35 struct task_struct *last_task_used_math = NULL;
37 void show_regs(struct pt_regs *regs)
39 unsigned long long ah, al, bh, bl, ch, cl;
43 ah = (regs->pc) >> 32;
44 al = (regs->pc) & 0xffffffff;
45 bh = (regs->regs[18]) >> 32;
46 bl = (regs->regs[18]) & 0xffffffff;
47 ch = (regs->regs[15]) >> 32;
48 cl = (regs->regs[15]) & 0xffffffff;
49 printk("PC : %08Lx%08Lx LINK: %08Lx%08Lx SP : %08Lx%08Lx\n",
50 ah, al, bh, bl, ch, cl);
52 ah = (regs->sr) >> 32;
53 al = (regs->sr) & 0xffffffff;
54 asm volatile ("getcon " __TEA ", %0" : "=r" (bh));
55 asm volatile ("getcon " __TEA ", %0" : "=r" (bl));
57 bl = (bl) & 0xffffffff;
58 asm volatile ("getcon " __KCR0 ", %0" : "=r" (ch));
59 asm volatile ("getcon " __KCR0 ", %0" : "=r" (cl));
61 cl = (cl) & 0xffffffff;
62 printk("SR : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
63 ah, al, bh, bl, ch, cl);
65 ah = (regs->regs[0]) >> 32;
66 al = (regs->regs[0]) & 0xffffffff;
67 bh = (regs->regs[1]) >> 32;
68 bl = (regs->regs[1]) & 0xffffffff;
69 ch = (regs->regs[2]) >> 32;
70 cl = (regs->regs[2]) & 0xffffffff;
71 printk("R0 : %08Lx%08Lx R1 : %08Lx%08Lx R2 : %08Lx%08Lx\n",
72 ah, al, bh, bl, ch, cl);
74 ah = (regs->regs[3]) >> 32;
75 al = (regs->regs[3]) & 0xffffffff;
76 bh = (regs->regs[4]) >> 32;
77 bl = (regs->regs[4]) & 0xffffffff;
78 ch = (regs->regs[5]) >> 32;
79 cl = (regs->regs[5]) & 0xffffffff;
80 printk("R3 : %08Lx%08Lx R4 : %08Lx%08Lx R5 : %08Lx%08Lx\n",
81 ah, al, bh, bl, ch, cl);
83 ah = (regs->regs[6]) >> 32;
84 al = (regs->regs[6]) & 0xffffffff;
85 bh = (regs->regs[7]) >> 32;
86 bl = (regs->regs[7]) & 0xffffffff;
87 ch = (regs->regs[8]) >> 32;
88 cl = (regs->regs[8]) & 0xffffffff;
89 printk("R6 : %08Lx%08Lx R7 : %08Lx%08Lx R8 : %08Lx%08Lx\n",
90 ah, al, bh, bl, ch, cl);
92 ah = (regs->regs[9]) >> 32;
93 al = (regs->regs[9]) & 0xffffffff;
94 bh = (regs->regs[10]) >> 32;
95 bl = (regs->regs[10]) & 0xffffffff;
96 ch = (regs->regs[11]) >> 32;
97 cl = (regs->regs[11]) & 0xffffffff;
98 printk("R9 : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
99 ah, al, bh, bl, ch, cl);
101 ah = (regs->regs[12]) >> 32;
102 al = (regs->regs[12]) & 0xffffffff;
103 bh = (regs->regs[13]) >> 32;
104 bl = (regs->regs[13]) & 0xffffffff;
105 ch = (regs->regs[14]) >> 32;
106 cl = (regs->regs[14]) & 0xffffffff;
107 printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
108 ah, al, bh, bl, ch, cl);
110 ah = (regs->regs[16]) >> 32;
111 al = (regs->regs[16]) & 0xffffffff;
112 bh = (regs->regs[17]) >> 32;
113 bl = (regs->regs[17]) & 0xffffffff;
114 ch = (regs->regs[19]) >> 32;
115 cl = (regs->regs[19]) & 0xffffffff;
116 printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
117 ah, al, bh, bl, ch, cl);
119 ah = (regs->regs[20]) >> 32;
120 al = (regs->regs[20]) & 0xffffffff;
121 bh = (regs->regs[21]) >> 32;
122 bl = (regs->regs[21]) & 0xffffffff;
123 ch = (regs->regs[22]) >> 32;
124 cl = (regs->regs[22]) & 0xffffffff;
125 printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
126 ah, al, bh, bl, ch, cl);
128 ah = (regs->regs[23]) >> 32;
129 al = (regs->regs[23]) & 0xffffffff;
130 bh = (regs->regs[24]) >> 32;
131 bl = (regs->regs[24]) & 0xffffffff;
132 ch = (regs->regs[25]) >> 32;
133 cl = (regs->regs[25]) & 0xffffffff;
134 printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
135 ah, al, bh, bl, ch, cl);
137 ah = (regs->regs[26]) >> 32;
138 al = (regs->regs[26]) & 0xffffffff;
139 bh = (regs->regs[27]) >> 32;
140 bl = (regs->regs[27]) & 0xffffffff;
141 ch = (regs->regs[28]) >> 32;
142 cl = (regs->regs[28]) & 0xffffffff;
143 printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
144 ah, al, bh, bl, ch, cl);
146 ah = (regs->regs[29]) >> 32;
147 al = (regs->regs[29]) & 0xffffffff;
148 bh = (regs->regs[30]) >> 32;
149 bl = (regs->regs[30]) & 0xffffffff;
150 ch = (regs->regs[31]) >> 32;
151 cl = (regs->regs[31]) & 0xffffffff;
152 printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
153 ah, al, bh, bl, ch, cl);
155 ah = (regs->regs[32]) >> 32;
156 al = (regs->regs[32]) & 0xffffffff;
157 bh = (regs->regs[33]) >> 32;
158 bl = (regs->regs[33]) & 0xffffffff;
159 ch = (regs->regs[34]) >> 32;
160 cl = (regs->regs[34]) & 0xffffffff;
161 printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
162 ah, al, bh, bl, ch, cl);
164 ah = (regs->regs[35]) >> 32;
165 al = (regs->regs[35]) & 0xffffffff;
166 bh = (regs->regs[36]) >> 32;
167 bl = (regs->regs[36]) & 0xffffffff;
168 ch = (regs->regs[37]) >> 32;
169 cl = (regs->regs[37]) & 0xffffffff;
170 printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
171 ah, al, bh, bl, ch, cl);
173 ah = (regs->regs[38]) >> 32;
174 al = (regs->regs[38]) & 0xffffffff;
175 bh = (regs->regs[39]) >> 32;
176 bl = (regs->regs[39]) & 0xffffffff;
177 ch = (regs->regs[40]) >> 32;
178 cl = (regs->regs[40]) & 0xffffffff;
179 printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
180 ah, al, bh, bl, ch, cl);
182 ah = (regs->regs[41]) >> 32;
183 al = (regs->regs[41]) & 0xffffffff;
184 bh = (regs->regs[42]) >> 32;
185 bl = (regs->regs[42]) & 0xffffffff;
186 ch = (regs->regs[43]) >> 32;
187 cl = (regs->regs[43]) & 0xffffffff;
188 printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
189 ah, al, bh, bl, ch, cl);
191 ah = (regs->regs[44]) >> 32;
192 al = (regs->regs[44]) & 0xffffffff;
193 bh = (regs->regs[45]) >> 32;
194 bl = (regs->regs[45]) & 0xffffffff;
195 ch = (regs->regs[46]) >> 32;
196 cl = (regs->regs[46]) & 0xffffffff;
197 printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
198 ah, al, bh, bl, ch, cl);
200 ah = (regs->regs[47]) >> 32;
201 al = (regs->regs[47]) & 0xffffffff;
202 bh = (regs->regs[48]) >> 32;
203 bl = (regs->regs[48]) & 0xffffffff;
204 ch = (regs->regs[49]) >> 32;
205 cl = (regs->regs[49]) & 0xffffffff;
206 printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
207 ah, al, bh, bl, ch, cl);
209 ah = (regs->regs[50]) >> 32;
210 al = (regs->regs[50]) & 0xffffffff;
211 bh = (regs->regs[51]) >> 32;
212 bl = (regs->regs[51]) & 0xffffffff;
213 ch = (regs->regs[52]) >> 32;
214 cl = (regs->regs[52]) & 0xffffffff;
215 printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
216 ah, al, bh, bl, ch, cl);
218 ah = (regs->regs[53]) >> 32;
219 al = (regs->regs[53]) & 0xffffffff;
220 bh = (regs->regs[54]) >> 32;
221 bl = (regs->regs[54]) & 0xffffffff;
222 ch = (regs->regs[55]) >> 32;
223 cl = (regs->regs[55]) & 0xffffffff;
224 printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
225 ah, al, bh, bl, ch, cl);
227 ah = (regs->regs[56]) >> 32;
228 al = (regs->regs[56]) & 0xffffffff;
229 bh = (regs->regs[57]) >> 32;
230 bl = (regs->regs[57]) & 0xffffffff;
231 ch = (regs->regs[58]) >> 32;
232 cl = (regs->regs[58]) & 0xffffffff;
233 printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
234 ah, al, bh, bl, ch, cl);
236 ah = (regs->regs[59]) >> 32;
237 al = (regs->regs[59]) & 0xffffffff;
238 bh = (regs->regs[60]) >> 32;
239 bl = (regs->regs[60]) & 0xffffffff;
240 ch = (regs->regs[61]) >> 32;
241 cl = (regs->regs[61]) & 0xffffffff;
242 printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
243 ah, al, bh, bl, ch, cl);
245 ah = (regs->regs[62]) >> 32;
246 al = (regs->regs[62]) & 0xffffffff;
247 bh = (regs->tregs[0]) >> 32;
248 bl = (regs->tregs[0]) & 0xffffffff;
249 ch = (regs->tregs[1]) >> 32;
250 cl = (regs->tregs[1]) & 0xffffffff;
251 printk("R62 : %08Lx%08Lx T0 : %08Lx%08Lx T1 : %08Lx%08Lx\n",
252 ah, al, bh, bl, ch, cl);
254 ah = (regs->tregs[2]) >> 32;
255 al = (regs->tregs[2]) & 0xffffffff;
256 bh = (regs->tregs[3]) >> 32;
257 bl = (regs->tregs[3]) & 0xffffffff;
258 ch = (regs->tregs[4]) >> 32;
259 cl = (regs->tregs[4]) & 0xffffffff;
260 printk("T2 : %08Lx%08Lx T3 : %08Lx%08Lx T4 : %08Lx%08Lx\n",
261 ah, al, bh, bl, ch, cl);
263 ah = (regs->tregs[5]) >> 32;
264 al = (regs->tregs[5]) & 0xffffffff;
265 bh = (regs->tregs[6]) >> 32;
266 bl = (regs->tregs[6]) & 0xffffffff;
267 ch = (regs->tregs[7]) >> 32;
268 cl = (regs->tregs[7]) & 0xffffffff;
269 printk("T5 : %08Lx%08Lx T6 : %08Lx%08Lx T7 : %08Lx%08Lx\n",
270 ah, al, bh, bl, ch, cl);
273 * If we're in kernel mode, dump the stack too..
275 if (!user_mode(regs)) {
276 void show_stack(struct task_struct *tsk, unsigned long *sp);
277 unsigned long sp = regs->regs[15] & 0xffffffff;
278 struct task_struct *tsk = get_current();
280 tsk->thread.kregs = regs;
282 show_stack(tsk, (unsigned long *)sp);
287 * Create a kernel thread
289 __noreturn void kernel_thread_helper(void *arg, int (*fn)(void *))
295 * This is the mechanism for creating a new kernel thread.
297 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
298 * who haven't done an "execve()") should use this: it will work within
299 * a system call from a "real" process, but the process memory space will
300 * not be freed until both the parent and the child have exited.
302 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
306 memset(®s, 0, sizeof(regs));
307 regs.regs[2] = (unsigned long)arg;
308 regs.regs[3] = (unsigned long)fn;
310 regs.pc = (unsigned long)kernel_thread_helper;
313 /* Ok, create the new process.. */
314 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
315 ®s, 0, NULL, NULL);
317 EXPORT_SYMBOL(kernel_thread);
320 * Free current thread data structures etc..
322 void exit_thread(void)
325 * See arch/sparc/kernel/process.c for the precedent for doing
328 * The SH-5 FPU save/restore approach relies on
329 * last_task_used_math pointing to a live task_struct. When
330 * another task tries to use the FPU for the 1st time, the FPUDIS
331 * trap handling (see arch/sh/kernel/cpu/sh5/fpu.c) will save the
332 * existing FPU state to the FP regs field within
333 * last_task_used_math before re-loading the new task's FPU state
334 * (or initialising it if the FPU has been used before). So if
335 * last_task_used_math is stale, and its page has already been
336 * re-allocated for another use, the consequences are rather
337 * grim. Unless we null it here, there is no other path through
338 * which it would get safely nulled.
341 if (last_task_used_math == current) {
342 last_task_used_math = NULL;
347 void flush_thread(void)
350 /* Called by fs/exec.c (setup_new_exec) to remove traces of a
351 * previously running executable. */
353 if (last_task_used_math == current) {
354 last_task_used_math = NULL;
356 /* Force FPU state to be reinitialised after exec */
360 /* if we are a kernel thread, about to change to user thread,
363 if(current->thread.kregs==&fake_swapper_regs) {
364 current->thread.kregs =
365 ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
366 current->thread.uregs = current->thread.kregs;
370 void release_thread(struct task_struct *dead_task)
375 /* Fill in the fpu structure for a core dump.. */
376 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
380 struct task_struct *tsk = current;
382 fpvalid = !!tsk_used_math(tsk);
384 if (current == last_task_used_math) {
388 last_task_used_math = 0;
392 memcpy(fpu, &tsk->thread.xstate->hardfpu, sizeof(*fpu));
397 return 0; /* Task didn't use the fpu at all. */
400 EXPORT_SYMBOL(dump_fpu);
402 asmlinkage void ret_from_fork(void);
404 int copy_thread(unsigned long clone_flags, unsigned long usp,
405 unsigned long unused,
406 struct task_struct *p, struct pt_regs *regs)
408 struct pt_regs *childregs;
411 if(last_task_used_math == current) {
415 last_task_used_math = NULL;
419 /* Copy from sh version */
420 childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
425 * Sign extend the edited stack.
426 * Note that thread.pc and thread.pc will stay
427 * 32-bit wide and context switch must take care
428 * of NEFF sign extension.
430 if (user_mode(regs)) {
431 childregs->regs[15] = neff_sign_extend(usp);
432 p->thread.uregs = childregs;
434 childregs->regs[15] =
435 neff_sign_extend((unsigned long)task_stack_page(p) +
439 childregs->regs[9] = 0; /* Set return value for child */
440 childregs->sr |= SR_FD; /* Invalidate FPU flag */
442 p->thread.sp = (unsigned long) childregs;
443 p->thread.pc = (unsigned long) ret_from_fork;
448 asmlinkage int sys_fork(unsigned long r2, unsigned long r3,
449 unsigned long r4, unsigned long r5,
450 unsigned long r6, unsigned long r7,
451 struct pt_regs *pregs)
453 return do_fork(SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
456 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
457 unsigned long r4, unsigned long r5,
458 unsigned long r6, unsigned long r7,
459 struct pt_regs *pregs)
462 newsp = pregs->regs[15];
463 return do_fork(clone_flags, newsp, pregs, 0, 0, 0);
467 * This is trivial, and on the face of it looks like it
468 * could equally well be done in user mode.
470 * Not so, for quite unobvious reasons - register pressure.
471 * In user mode vfork() cannot have a stack frame, and if
472 * done by calling the "clone()" system call directly, you
473 * do not have enough call-clobbered registers to hold all
474 * the information you need.
476 asmlinkage int sys_vfork(unsigned long r2, unsigned long r3,
477 unsigned long r4, unsigned long r5,
478 unsigned long r6, unsigned long r7,
479 struct pt_regs *pregs)
481 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
485 * sys_execve() executes a new program.
487 asmlinkage int sys_execve(const char *ufilename, char **uargv,
488 char **uenvp, unsigned long r5,
489 unsigned long r6, unsigned long r7,
490 struct pt_regs *pregs)
495 filename = getname((char __user *)ufilename);
496 error = PTR_ERR(filename);
497 if (IS_ERR(filename))
500 error = do_execve(filename,
501 (const char __user *const __user *)uargv,
502 (const char __user *const __user *)uenvp,
509 #ifdef CONFIG_FRAME_POINTER
510 static int in_sh64_switch_to(unsigned long pc)
512 extern char __sh64_switch_to_end;
513 /* For a sleeping task, the PC is somewhere in the middle of the function,
514 so we don't have to worry about masking the LSB off */
515 return (pc >= (unsigned long) sh64_switch_to) &&
516 (pc < (unsigned long) &__sh64_switch_to_end);
520 unsigned long get_wchan(struct task_struct *p)
524 if (!p || p == current || p->state == TASK_RUNNING)
528 * The same comment as on the Alpha applies here, too ...
530 pc = thread_saved_pc(p);
532 #ifdef CONFIG_FRAME_POINTER
533 if (in_sh64_switch_to(pc)) {
534 unsigned long schedule_fp;
535 unsigned long sh64_switch_to_fp;
536 unsigned long schedule_caller_pc;
538 sh64_switch_to_fp = (long) p->thread.sp;
539 /* r14 is saved at offset 4 in the sh64_switch_to frame */
540 schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
542 /* and the caller of 'schedule' is (currently!) saved at offset 24
543 in the frame of schedule (from disasm) */
544 schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
545 return schedule_caller_pc;