Merge branch 'linus' into core/rcu
[sfrench/cifs-2.6.git] / arch / sh / kernel / process_64.c
1 /*
2  * arch/sh/kernel/process_64.c
3  *
4  * This file handles the architecture-dependent parts of process handling..
5  *
6  * Copyright (C) 2000, 2001  Paolo Alberelli
7  * Copyright (C) 2003 - 2007  Paul Mundt
8  * Copyright (C) 2003, 2004 Richard Curnow
9  *
10  * Started from SH3/4 version:
11  *   Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
12  *
13  *   In turn started from i386 version:
14  *     Copyright (C) 1995  Linus Torvalds
15  *
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
18  * for more details.
19  */
20 #include <linux/mm.h>
21 #include <linux/fs.h>
22 #include <linux/ptrace.h>
23 #include <linux/reboot.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/io.h>
27 #include <asm/syscalls.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/mmu_context.h>
31 #include <asm/fpu.h>
32
33 struct task_struct *last_task_used_math = NULL;
34
35 void show_regs(struct pt_regs *regs)
36 {
37         unsigned long long ah, al, bh, bl, ch, cl;
38
39         printk("\n");
40
41         ah = (regs->pc) >> 32;
42         al = (regs->pc) & 0xffffffff;
43         bh = (regs->regs[18]) >> 32;
44         bl = (regs->regs[18]) & 0xffffffff;
45         ch = (regs->regs[15]) >> 32;
46         cl = (regs->regs[15]) & 0xffffffff;
47         printk("PC  : %08Lx%08Lx LINK: %08Lx%08Lx SP  : %08Lx%08Lx\n",
48                ah, al, bh, bl, ch, cl);
49
50         ah = (regs->sr) >> 32;
51         al = (regs->sr) & 0xffffffff;
52         asm volatile ("getcon   " __TEA ", %0" : "=r" (bh));
53         asm volatile ("getcon   " __TEA ", %0" : "=r" (bl));
54         bh = (bh) >> 32;
55         bl = (bl) & 0xffffffff;
56         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (ch));
57         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (cl));
58         ch = (ch) >> 32;
59         cl = (cl) & 0xffffffff;
60         printk("SR  : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
61                ah, al, bh, bl, ch, cl);
62
63         ah = (regs->regs[0]) >> 32;
64         al = (regs->regs[0]) & 0xffffffff;
65         bh = (regs->regs[1]) >> 32;
66         bl = (regs->regs[1]) & 0xffffffff;
67         ch = (regs->regs[2]) >> 32;
68         cl = (regs->regs[2]) & 0xffffffff;
69         printk("R0  : %08Lx%08Lx R1  : %08Lx%08Lx R2  : %08Lx%08Lx\n",
70                ah, al, bh, bl, ch, cl);
71
72         ah = (regs->regs[3]) >> 32;
73         al = (regs->regs[3]) & 0xffffffff;
74         bh = (regs->regs[4]) >> 32;
75         bl = (regs->regs[4]) & 0xffffffff;
76         ch = (regs->regs[5]) >> 32;
77         cl = (regs->regs[5]) & 0xffffffff;
78         printk("R3  : %08Lx%08Lx R4  : %08Lx%08Lx R5  : %08Lx%08Lx\n",
79                ah, al, bh, bl, ch, cl);
80
81         ah = (regs->regs[6]) >> 32;
82         al = (regs->regs[6]) & 0xffffffff;
83         bh = (regs->regs[7]) >> 32;
84         bl = (regs->regs[7]) & 0xffffffff;
85         ch = (regs->regs[8]) >> 32;
86         cl = (regs->regs[8]) & 0xffffffff;
87         printk("R6  : %08Lx%08Lx R7  : %08Lx%08Lx R8  : %08Lx%08Lx\n",
88                ah, al, bh, bl, ch, cl);
89
90         ah = (regs->regs[9]) >> 32;
91         al = (regs->regs[9]) & 0xffffffff;
92         bh = (regs->regs[10]) >> 32;
93         bl = (regs->regs[10]) & 0xffffffff;
94         ch = (regs->regs[11]) >> 32;
95         cl = (regs->regs[11]) & 0xffffffff;
96         printk("R9  : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
97                ah, al, bh, bl, ch, cl);
98
99         ah = (regs->regs[12]) >> 32;
100         al = (regs->regs[12]) & 0xffffffff;
101         bh = (regs->regs[13]) >> 32;
102         bl = (regs->regs[13]) & 0xffffffff;
103         ch = (regs->regs[14]) >> 32;
104         cl = (regs->regs[14]) & 0xffffffff;
105         printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
106                ah, al, bh, bl, ch, cl);
107
108         ah = (regs->regs[16]) >> 32;
109         al = (regs->regs[16]) & 0xffffffff;
110         bh = (regs->regs[17]) >> 32;
111         bl = (regs->regs[17]) & 0xffffffff;
112         ch = (regs->regs[19]) >> 32;
113         cl = (regs->regs[19]) & 0xffffffff;
114         printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
115                ah, al, bh, bl, ch, cl);
116
117         ah = (regs->regs[20]) >> 32;
118         al = (regs->regs[20]) & 0xffffffff;
119         bh = (regs->regs[21]) >> 32;
120         bl = (regs->regs[21]) & 0xffffffff;
121         ch = (regs->regs[22]) >> 32;
122         cl = (regs->regs[22]) & 0xffffffff;
123         printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
124                ah, al, bh, bl, ch, cl);
125
126         ah = (regs->regs[23]) >> 32;
127         al = (regs->regs[23]) & 0xffffffff;
128         bh = (regs->regs[24]) >> 32;
129         bl = (regs->regs[24]) & 0xffffffff;
130         ch = (regs->regs[25]) >> 32;
131         cl = (regs->regs[25]) & 0xffffffff;
132         printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
133                ah, al, bh, bl, ch, cl);
134
135         ah = (regs->regs[26]) >> 32;
136         al = (regs->regs[26]) & 0xffffffff;
137         bh = (regs->regs[27]) >> 32;
138         bl = (regs->regs[27]) & 0xffffffff;
139         ch = (regs->regs[28]) >> 32;
140         cl = (regs->regs[28]) & 0xffffffff;
141         printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
142                ah, al, bh, bl, ch, cl);
143
144         ah = (regs->regs[29]) >> 32;
145         al = (regs->regs[29]) & 0xffffffff;
146         bh = (regs->regs[30]) >> 32;
147         bl = (regs->regs[30]) & 0xffffffff;
148         ch = (regs->regs[31]) >> 32;
149         cl = (regs->regs[31]) & 0xffffffff;
150         printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
151                ah, al, bh, bl, ch, cl);
152
153         ah = (regs->regs[32]) >> 32;
154         al = (regs->regs[32]) & 0xffffffff;
155         bh = (regs->regs[33]) >> 32;
156         bl = (regs->regs[33]) & 0xffffffff;
157         ch = (regs->regs[34]) >> 32;
158         cl = (regs->regs[34]) & 0xffffffff;
159         printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
160                ah, al, bh, bl, ch, cl);
161
162         ah = (regs->regs[35]) >> 32;
163         al = (regs->regs[35]) & 0xffffffff;
164         bh = (regs->regs[36]) >> 32;
165         bl = (regs->regs[36]) & 0xffffffff;
166         ch = (regs->regs[37]) >> 32;
167         cl = (regs->regs[37]) & 0xffffffff;
168         printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
169                ah, al, bh, bl, ch, cl);
170
171         ah = (regs->regs[38]) >> 32;
172         al = (regs->regs[38]) & 0xffffffff;
173         bh = (regs->regs[39]) >> 32;
174         bl = (regs->regs[39]) & 0xffffffff;
175         ch = (regs->regs[40]) >> 32;
176         cl = (regs->regs[40]) & 0xffffffff;
177         printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
178                ah, al, bh, bl, ch, cl);
179
180         ah = (regs->regs[41]) >> 32;
181         al = (regs->regs[41]) & 0xffffffff;
182         bh = (regs->regs[42]) >> 32;
183         bl = (regs->regs[42]) & 0xffffffff;
184         ch = (regs->regs[43]) >> 32;
185         cl = (regs->regs[43]) & 0xffffffff;
186         printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
187                ah, al, bh, bl, ch, cl);
188
189         ah = (regs->regs[44]) >> 32;
190         al = (regs->regs[44]) & 0xffffffff;
191         bh = (regs->regs[45]) >> 32;
192         bl = (regs->regs[45]) & 0xffffffff;
193         ch = (regs->regs[46]) >> 32;
194         cl = (regs->regs[46]) & 0xffffffff;
195         printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
196                ah, al, bh, bl, ch, cl);
197
198         ah = (regs->regs[47]) >> 32;
199         al = (regs->regs[47]) & 0xffffffff;
200         bh = (regs->regs[48]) >> 32;
201         bl = (regs->regs[48]) & 0xffffffff;
202         ch = (regs->regs[49]) >> 32;
203         cl = (regs->regs[49]) & 0xffffffff;
204         printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
205                ah, al, bh, bl, ch, cl);
206
207         ah = (regs->regs[50]) >> 32;
208         al = (regs->regs[50]) & 0xffffffff;
209         bh = (regs->regs[51]) >> 32;
210         bl = (regs->regs[51]) & 0xffffffff;
211         ch = (regs->regs[52]) >> 32;
212         cl = (regs->regs[52]) & 0xffffffff;
213         printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
214                ah, al, bh, bl, ch, cl);
215
216         ah = (regs->regs[53]) >> 32;
217         al = (regs->regs[53]) & 0xffffffff;
218         bh = (regs->regs[54]) >> 32;
219         bl = (regs->regs[54]) & 0xffffffff;
220         ch = (regs->regs[55]) >> 32;
221         cl = (regs->regs[55]) & 0xffffffff;
222         printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
223                ah, al, bh, bl, ch, cl);
224
225         ah = (regs->regs[56]) >> 32;
226         al = (regs->regs[56]) & 0xffffffff;
227         bh = (regs->regs[57]) >> 32;
228         bl = (regs->regs[57]) & 0xffffffff;
229         ch = (regs->regs[58]) >> 32;
230         cl = (regs->regs[58]) & 0xffffffff;
231         printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
232                ah, al, bh, bl, ch, cl);
233
234         ah = (regs->regs[59]) >> 32;
235         al = (regs->regs[59]) & 0xffffffff;
236         bh = (regs->regs[60]) >> 32;
237         bl = (regs->regs[60]) & 0xffffffff;
238         ch = (regs->regs[61]) >> 32;
239         cl = (regs->regs[61]) & 0xffffffff;
240         printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
241                ah, al, bh, bl, ch, cl);
242
243         ah = (regs->regs[62]) >> 32;
244         al = (regs->regs[62]) & 0xffffffff;
245         bh = (regs->tregs[0]) >> 32;
246         bl = (regs->tregs[0]) & 0xffffffff;
247         ch = (regs->tregs[1]) >> 32;
248         cl = (regs->tregs[1]) & 0xffffffff;
249         printk("R62 : %08Lx%08Lx T0  : %08Lx%08Lx T1  : %08Lx%08Lx\n",
250                ah, al, bh, bl, ch, cl);
251
252         ah = (regs->tregs[2]) >> 32;
253         al = (regs->tregs[2]) & 0xffffffff;
254         bh = (regs->tregs[3]) >> 32;
255         bl = (regs->tregs[3]) & 0xffffffff;
256         ch = (regs->tregs[4]) >> 32;
257         cl = (regs->tregs[4]) & 0xffffffff;
258         printk("T2  : %08Lx%08Lx T3  : %08Lx%08Lx T4  : %08Lx%08Lx\n",
259                ah, al, bh, bl, ch, cl);
260
261         ah = (regs->tregs[5]) >> 32;
262         al = (regs->tregs[5]) & 0xffffffff;
263         bh = (regs->tregs[6]) >> 32;
264         bl = (regs->tregs[6]) & 0xffffffff;
265         ch = (regs->tregs[7]) >> 32;
266         cl = (regs->tregs[7]) & 0xffffffff;
267         printk("T5  : %08Lx%08Lx T6  : %08Lx%08Lx T7  : %08Lx%08Lx\n",
268                ah, al, bh, bl, ch, cl);
269
270         /*
271          * If we're in kernel mode, dump the stack too..
272          */
273         if (!user_mode(regs)) {
274                 void show_stack(struct task_struct *tsk, unsigned long *sp);
275                 unsigned long sp = regs->regs[15] & 0xffffffff;
276                 struct task_struct *tsk = get_current();
277
278                 tsk->thread.kregs = regs;
279
280                 show_stack(tsk, (unsigned long *)sp);
281         }
282 }
283
284 /*
285  * Create a kernel thread
286  */
287 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
288 {
289         do_exit(fn(arg));
290 }
291
292 /*
293  * This is the mechanism for creating a new kernel thread.
294  *
295  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
296  * who haven't done an "execve()") should use this: it will work within
297  * a system call from a "real" process, but the process memory space will
298  * not be freed until both the parent and the child have exited.
299  */
300 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
301 {
302         struct pt_regs regs;
303
304         memset(&regs, 0, sizeof(regs));
305         regs.regs[2] = (unsigned long)arg;
306         regs.regs[3] = (unsigned long)fn;
307
308         regs.pc = (unsigned long)kernel_thread_helper;
309         regs.sr = (1 << 30);
310
311         /* Ok, create the new process.. */
312         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
313                       &regs, 0, NULL, NULL);
314 }
315 EXPORT_SYMBOL(kernel_thread);
316
317 /*
318  * Free current thread data structures etc..
319  */
320 void exit_thread(void)
321 {
322         /*
323          * See arch/sparc/kernel/process.c for the precedent for doing
324          * this -- RPC.
325          *
326          * The SH-5 FPU save/restore approach relies on
327          * last_task_used_math pointing to a live task_struct.  When
328          * another task tries to use the FPU for the 1st time, the FPUDIS
329          * trap handling (see arch/sh/kernel/cpu/sh5/fpu.c) will save the
330          * existing FPU state to the FP regs field within
331          * last_task_used_math before re-loading the new task's FPU state
332          * (or initialising it if the FPU has been used before).  So if
333          * last_task_used_math is stale, and its page has already been
334          * re-allocated for another use, the consequences are rather
335          * grim. Unless we null it here, there is no other path through
336          * which it would get safely nulled.
337          */
338 #ifdef CONFIG_SH_FPU
339         if (last_task_used_math == current) {
340                 last_task_used_math = NULL;
341         }
342 #endif
343 }
344
345 void flush_thread(void)
346 {
347
348         /* Called by fs/exec.c (setup_new_exec) to remove traces of a
349          * previously running executable. */
350 #ifdef CONFIG_SH_FPU
351         if (last_task_used_math == current) {
352                 last_task_used_math = NULL;
353         }
354         /* Force FPU state to be reinitialised after exec */
355         clear_used_math();
356 #endif
357
358         /* if we are a kernel thread, about to change to user thread,
359          * update kreg
360          */
361         if(current->thread.kregs==&fake_swapper_regs) {
362           current->thread.kregs =
363              ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
364           current->thread.uregs = current->thread.kregs;
365         }
366 }
367
368 void release_thread(struct task_struct *dead_task)
369 {
370         /* do nothing */
371 }
372
373 /* Fill in the fpu structure for a core dump.. */
374 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
375 {
376 #ifdef CONFIG_SH_FPU
377         int fpvalid;
378         struct task_struct *tsk = current;
379
380         fpvalid = !!tsk_used_math(tsk);
381         if (fpvalid) {
382                 if (current == last_task_used_math) {
383                         enable_fpu();
384                         save_fpu(tsk);
385                         disable_fpu();
386                         last_task_used_math = 0;
387                         regs->sr |= SR_FD;
388                 }
389
390                 memcpy(fpu, &tsk->thread.xstate->hardfpu, sizeof(*fpu));
391         }
392
393         return fpvalid;
394 #else
395         return 0; /* Task didn't use the fpu at all. */
396 #endif
397 }
398 EXPORT_SYMBOL(dump_fpu);
399
400 asmlinkage void ret_from_fork(void);
401
402 int copy_thread(unsigned long clone_flags, unsigned long usp,
403                 unsigned long unused,
404                 struct task_struct *p, struct pt_regs *regs)
405 {
406         struct pt_regs *childregs;
407
408 #ifdef CONFIG_SH_FPU
409         if(last_task_used_math == current) {
410                 enable_fpu();
411                 save_fpu(current);
412                 disable_fpu();
413                 last_task_used_math = NULL;
414                 regs->sr |= SR_FD;
415         }
416 #endif
417         /* Copy from sh version */
418         childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
419
420         *childregs = *regs;
421
422         /*
423          * Sign extend the edited stack.
424          * Note that thread.pc and thread.pc will stay
425          * 32-bit wide and context switch must take care
426          * of NEFF sign extension.
427          */
428         if (user_mode(regs)) {
429                 childregs->regs[15] = neff_sign_extend(usp);
430                 p->thread.uregs = childregs;
431         } else {
432                 childregs->regs[15] =
433                         neff_sign_extend((unsigned long)task_stack_page(p) +
434                                          THREAD_SIZE);
435         }
436
437         childregs->regs[9] = 0; /* Set return value for child */
438         childregs->sr |= SR_FD; /* Invalidate FPU flag */
439
440         p->thread.sp = (unsigned long) childregs;
441         p->thread.pc = (unsigned long) ret_from_fork;
442
443         return 0;
444 }
445
446 asmlinkage int sys_fork(unsigned long r2, unsigned long r3,
447                         unsigned long r4, unsigned long r5,
448                         unsigned long r6, unsigned long r7,
449                         struct pt_regs *pregs)
450 {
451         return do_fork(SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
452 }
453
454 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
455                          unsigned long r4, unsigned long r5,
456                          unsigned long r6, unsigned long r7,
457                          struct pt_regs *pregs)
458 {
459         if (!newsp)
460                 newsp = pregs->regs[15];
461         return do_fork(clone_flags, newsp, pregs, 0, 0, 0);
462 }
463
464 /*
465  * This is trivial, and on the face of it looks like it
466  * could equally well be done in user mode.
467  *
468  * Not so, for quite unobvious reasons - register pressure.
469  * In user mode vfork() cannot have a stack frame, and if
470  * done by calling the "clone()" system call directly, you
471  * do not have enough call-clobbered registers to hold all
472  * the information you need.
473  */
474 asmlinkage int sys_vfork(unsigned long r2, unsigned long r3,
475                          unsigned long r4, unsigned long r5,
476                          unsigned long r6, unsigned long r7,
477                          struct pt_regs *pregs)
478 {
479         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
480 }
481
482 /*
483  * sys_execve() executes a new program.
484  */
485 asmlinkage int sys_execve(char *ufilename, char **uargv,
486                           char **uenvp, unsigned long r5,
487                           unsigned long r6, unsigned long r7,
488                           struct pt_regs *pregs)
489 {
490         int error;
491         char *filename;
492
493         filename = getname((char __user *)ufilename);
494         error = PTR_ERR(filename);
495         if (IS_ERR(filename))
496                 goto out;
497
498         error = do_execve(filename,
499                           (char __user * __user *)uargv,
500                           (char __user * __user *)uenvp,
501                           pregs);
502         putname(filename);
503 out:
504         return error;
505 }
506
507 /*
508  * These bracket the sleeping functions..
509  */
510 extern void interruptible_sleep_on(wait_queue_head_t *q);
511
512 #define mid_sched       ((unsigned long) interruptible_sleep_on)
513
514 #ifdef CONFIG_FRAME_POINTER
515 static int in_sh64_switch_to(unsigned long pc)
516 {
517         extern char __sh64_switch_to_end;
518         /* For a sleeping task, the PC is somewhere in the middle of the function,
519            so we don't have to worry about masking the LSB off */
520         return (pc >= (unsigned long) sh64_switch_to) &&
521                (pc < (unsigned long) &__sh64_switch_to_end);
522 }
523 #endif
524
525 unsigned long get_wchan(struct task_struct *p)
526 {
527         unsigned long pc;
528
529         if (!p || p == current || p->state == TASK_RUNNING)
530                 return 0;
531
532         /*
533          * The same comment as on the Alpha applies here, too ...
534          */
535         pc = thread_saved_pc(p);
536
537 #ifdef CONFIG_FRAME_POINTER
538         if (in_sh64_switch_to(pc)) {
539                 unsigned long schedule_fp;
540                 unsigned long sh64_switch_to_fp;
541                 unsigned long schedule_caller_pc;
542
543                 sh64_switch_to_fp = (long) p->thread.sp;
544                 /* r14 is saved at offset 4 in the sh64_switch_to frame */
545                 schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
546
547                 /* and the caller of 'schedule' is (currently!) saved at offset 24
548                    in the frame of schedule (from disasm) */
549                 schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
550                 return schedule_caller_pc;
551         }
552 #endif
553         return pc;
554 }