sh: Minor optimisations to FPU handling
[safe/jmp/linux-2.6] / arch / sh / kernel / process_32.c
1 /*
2  * arch/sh/kernel/process.c
3  *
4  * This file handles the architecture-dependent parts of process handling..
5  *
6  *  Copyright (C) 1995  Linus Torvalds
7  *
8  *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
9  *                   Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
10  *                   Copyright (C) 2002 - 2008  Paul Mundt
11  *
12  * This file is subject to the terms and conditions of the GNU General Public
13  * License.  See the file "COPYING" in the main directory of this archive
14  * for more details.
15  */
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/elfcore.h>
19 #include <linux/pm.h>
20 #include <linux/kallsyms.h>
21 #include <linux/kexec.h>
22 #include <linux/kdebug.h>
23 #include <linux/tick.h>
24 #include <linux/reboot.h>
25 #include <linux/fs.h>
26 #include <linux/ftrace.h>
27 #include <linux/preempt.h>
28 #include <asm/uaccess.h>
29 #include <asm/mmu_context.h>
30 #include <asm/pgalloc.h>
31 #include <asm/system.h>
32 #include <asm/ubc.h>
33 #include <asm/fpu.h>
34 #include <asm/syscalls.h>
35 #include <asm/watchdog.h>
36
37 int ubc_usercnt = 0;
38
39 #ifdef CONFIG_32BIT
40 static void watchdog_trigger_immediate(void)
41 {
42         sh_wdt_write_cnt(0xFF);
43         sh_wdt_write_csr(0xC2);
44 }
45
46 void machine_restart(char * __unused)
47 {
48         local_irq_disable();
49
50         /* Use watchdog timer to trigger reset */
51         watchdog_trigger_immediate();
52
53         while (1)
54                 cpu_sleep();
55 }
56 #else
57 void machine_restart(char * __unused)
58 {
59         /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
60         asm volatile("ldc %0, sr\n\t"
61                      "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
62 }
63 #endif
64
65 void machine_halt(void)
66 {
67         local_irq_disable();
68
69         while (1)
70                 cpu_sleep();
71 }
72
73 void machine_power_off(void)
74 {
75         if (pm_power_off)
76                 pm_power_off();
77 }
78
79 void show_regs(struct pt_regs * regs)
80 {
81         printk("\n");
82         printk("Pid : %d, Comm: \t\t%s\n", task_pid_nr(current), current->comm);
83         printk("CPU : %d        \t\t%s  (%s %.*s)\n\n",
84                smp_processor_id(), print_tainted(), init_utsname()->release,
85                (int)strcspn(init_utsname()->version, " "),
86                init_utsname()->version);
87
88         print_symbol("PC is at %s\n", instruction_pointer(regs));
89         print_symbol("PR is at %s\n", regs->pr);
90
91         printk("PC  : %08lx SP  : %08lx SR  : %08lx ",
92                regs->pc, regs->regs[15], regs->sr);
93 #ifdef CONFIG_MMU
94         printk("TEA : %08x\n", ctrl_inl(MMU_TEA));
95 #else
96         printk("\n");
97 #endif
98
99         printk("R0  : %08lx R1  : %08lx R2  : %08lx R3  : %08lx\n",
100                regs->regs[0],regs->regs[1],
101                regs->regs[2],regs->regs[3]);
102         printk("R4  : %08lx R5  : %08lx R6  : %08lx R7  : %08lx\n",
103                regs->regs[4],regs->regs[5],
104                regs->regs[6],regs->regs[7]);
105         printk("R8  : %08lx R9  : %08lx R10 : %08lx R11 : %08lx\n",
106                regs->regs[8],regs->regs[9],
107                regs->regs[10],regs->regs[11]);
108         printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
109                regs->regs[12],regs->regs[13],
110                regs->regs[14]);
111         printk("MACH: %08lx MACL: %08lx GBR : %08lx PR  : %08lx\n",
112                regs->mach, regs->macl, regs->gbr, regs->pr);
113
114         show_trace(NULL, (unsigned long *)regs->regs[15], regs);
115         show_code(regs);
116 }
117
118 /*
119  * Create a kernel thread
120  */
121 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
122 {
123         do_exit(fn(arg));
124 }
125
126 /* Don't use this in BL=1(cli).  Or else, CPU resets! */
127 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
128 {
129         struct pt_regs regs;
130         int pid;
131
132         memset(&regs, 0, sizeof(regs));
133         regs.regs[4] = (unsigned long)arg;
134         regs.regs[5] = (unsigned long)fn;
135
136         regs.pc = (unsigned long)kernel_thread_helper;
137         regs.sr = SR_MD;
138 #if defined(CONFIG_SH_FPU)
139         regs.sr |= SR_FD;
140 #endif
141
142         /* Ok, create the new process.. */
143         pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
144                       &regs, 0, NULL, NULL);
145
146         return pid;
147 }
148 EXPORT_SYMBOL(kernel_thread);
149
150 /*
151  * Free current thread data structures etc..
152  */
153 void exit_thread(void)
154 {
155         if (current->thread.ubc_pc) {
156                 current->thread.ubc_pc = 0;
157                 ubc_usercnt -= 1;
158         }
159 }
160
161 void flush_thread(void)
162 {
163 #if defined(CONFIG_SH_FPU)
164         struct task_struct *tsk = current;
165         /* Forget lazy FPU state */
166         clear_fpu(tsk, task_pt_regs(tsk));
167         clear_used_math();
168 #endif
169 }
170
171 void release_thread(struct task_struct *dead_task)
172 {
173         /* do nothing */
174 }
175
176 /* Fill in the fpu structure for a core dump.. */
177 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
178 {
179         int fpvalid = 0;
180
181 #if defined(CONFIG_SH_FPU)
182         struct task_struct *tsk = current;
183
184         fpvalid = !!tsk_used_math(tsk);
185         if (fpvalid)
186                 fpvalid = !fpregs_get(tsk, NULL, 0,
187                                       sizeof(struct user_fpu_struct),
188                                       fpu, NULL);
189 #endif
190
191         return fpvalid;
192 }
193 EXPORT_SYMBOL(dump_fpu);
194
195 /*
196  * This gets called before we allocate a new thread and copy
197  * the current task into it.
198  */
199 void prepare_to_copy(struct task_struct *tsk)
200 {
201         unlazy_fpu(tsk, task_pt_regs(tsk));
202 }
203
204 asmlinkage void ret_from_fork(void);
205
206 int copy_thread(unsigned long clone_flags, unsigned long usp,
207                 unsigned long unused,
208                 struct task_struct *p, struct pt_regs *regs)
209 {
210         struct thread_info *ti = task_thread_info(p);
211         struct pt_regs *childregs;
212 #if defined(CONFIG_SH_DSP)
213         struct task_struct *tsk = current;
214 #endif
215
216 #if defined(CONFIG_SH_DSP)
217         if (is_dsp_enabled(tsk)) {
218                 /* We can use the __save_dsp or just copy the struct:
219                  * __save_dsp(p);
220                  * p->thread.dsp_status.status |= SR_DSP
221                  */
222                 p->thread.dsp_status = tsk->thread.dsp_status;
223         }
224 #endif
225
226         childregs = task_pt_regs(p);
227         *childregs = *regs;
228
229         if (user_mode(regs)) {
230                 childregs->regs[15] = usp;
231                 ti->addr_limit = USER_DS;
232         } else {
233                 childregs->regs[15] = (unsigned long)childregs;
234                 ti->addr_limit = KERNEL_DS;
235                 ti->status &= ~TS_USEDFPU;
236                 p->fpu_counter = 0;
237         }
238
239         if (clone_flags & CLONE_SETTLS)
240                 childregs->gbr = childregs->regs[0];
241
242         childregs->regs[0] = 0; /* Set return value for child */
243
244         p->thread.sp = (unsigned long) childregs;
245         p->thread.pc = (unsigned long) ret_from_fork;
246
247         p->thread.ubc_pc = 0;
248
249         return 0;
250 }
251
252 /* Tracing by user break controller.  */
253 static void ubc_set_tracing(int asid, unsigned long pc)
254 {
255 #if defined(CONFIG_CPU_SH4A)
256         unsigned long val;
257
258         val = (UBC_CBR_ID_INST | UBC_CBR_RW_READ | UBC_CBR_CE);
259         val |= (UBC_CBR_AIE | UBC_CBR_AIV_SET(asid));
260
261         ctrl_outl(val, UBC_CBR0);
262         ctrl_outl(pc,  UBC_CAR0);
263         ctrl_outl(0x0, UBC_CAMR0);
264         ctrl_outl(0x0, UBC_CBCR);
265
266         val = (UBC_CRR_RES | UBC_CRR_PCB | UBC_CRR_BIE);
267         ctrl_outl(val, UBC_CRR0);
268
269         /* Read UBC register that we wrote last, for checking update */
270         val = ctrl_inl(UBC_CRR0);
271
272 #else   /* CONFIG_CPU_SH4A */
273         ctrl_outl(pc, UBC_BARA);
274
275 #ifdef CONFIG_MMU
276         ctrl_outb(asid, UBC_BASRA);
277 #endif
278
279         ctrl_outl(0, UBC_BAMRA);
280
281         if (current_cpu_data.type == CPU_SH7729 ||
282             current_cpu_data.type == CPU_SH7710 ||
283             current_cpu_data.type == CPU_SH7712 ||
284             current_cpu_data.type == CPU_SH7203){
285                 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
286                 ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
287         } else {
288                 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
289                 ctrl_outw(BRCR_PCBA, UBC_BRCR);
290         }
291 #endif  /* CONFIG_CPU_SH4A */
292 }
293
294 /*
295  *      switch_to(x,y) should switch tasks from x to y.
296  *
297  */
298 __notrace_funcgraph struct task_struct *
299 __switch_to(struct task_struct *prev, struct task_struct *next)
300 {
301         struct thread_struct *next_t = &next->thread;
302
303 #if defined(CONFIG_SH_FPU)
304         unlazy_fpu(prev, task_pt_regs(prev));
305
306         /* we're going to use this soon, after a few expensive things */
307         if (next->fpu_counter > 5)
308                 prefetch(&next_t->fpu.hard);
309 #endif
310
311 #ifdef CONFIG_MMU
312         /*
313          * Restore the kernel mode register
314          *      k7 (r7_bank1)
315          */
316         asm volatile("ldc       %0, r7_bank"
317                      : /* no output */
318                      : "r" (task_thread_info(next)));
319 #endif
320
321         /* If no tasks are using the UBC, we're done */
322         if (ubc_usercnt == 0)
323                 /* If no tasks are using the UBC, we're done */;
324         else if (next->thread.ubc_pc && next->mm) {
325                 int asid = 0;
326 #ifdef CONFIG_MMU
327                 asid |= cpu_asid(smp_processor_id(), next->mm);
328 #endif
329                 ubc_set_tracing(asid, next->thread.ubc_pc);
330         } else {
331 #if defined(CONFIG_CPU_SH4A)
332                 ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
333                 ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
334 #else
335                 ctrl_outw(0, UBC_BBRA);
336                 ctrl_outw(0, UBC_BBRB);
337 #endif
338         }
339
340 #if defined(CONFIG_SH_FPU)
341         /* If the task has used fpu the last 5 timeslices, just do a full
342          * restore of the math state immediately to avoid the trap; the
343          * chances of needing FPU soon are obviously high now
344          */
345         if (next->fpu_counter > 5) {
346                 fpu_state_restore(task_pt_regs(next));
347         }
348 #endif
349
350         return prev;
351 }
352
353 asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
354                         unsigned long r6, unsigned long r7,
355                         struct pt_regs __regs)
356 {
357 #ifdef CONFIG_MMU
358         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
359         return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
360 #else
361         /* fork almost works, enough to trick you into looking elsewhere :-( */
362         return -EINVAL;
363 #endif
364 }
365
366 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
367                          unsigned long parent_tidptr,
368                          unsigned long child_tidptr,
369                          struct pt_regs __regs)
370 {
371         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
372         if (!newsp)
373                 newsp = regs->regs[15];
374         return do_fork(clone_flags, newsp, regs, 0,
375                         (int __user *)parent_tidptr,
376                         (int __user *)child_tidptr);
377 }
378
379 /*
380  * This is trivial, and on the face of it looks like it
381  * could equally well be done in user mode.
382  *
383  * Not so, for quite unobvious reasons - register pressure.
384  * In user mode vfork() cannot have a stack frame, and if
385  * done by calling the "clone()" system call directly, you
386  * do not have enough call-clobbered registers to hold all
387  * the information you need.
388  */
389 asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
390                          unsigned long r6, unsigned long r7,
391                          struct pt_regs __regs)
392 {
393         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
394         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
395                        0, NULL, NULL);
396 }
397
398 /*
399  * sys_execve() executes a new program.
400  */
401 asmlinkage int sys_execve(char __user *ufilename, char __user * __user *uargv,
402                           char __user * __user *uenvp, unsigned long r7,
403                           struct pt_regs __regs)
404 {
405         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
406         int error;
407         char *filename;
408
409         filename = getname(ufilename);
410         error = PTR_ERR(filename);
411         if (IS_ERR(filename))
412                 goto out;
413
414         error = do_execve(filename, uargv, uenvp, regs);
415         putname(filename);
416 out:
417         return error;
418 }
419
420 unsigned long get_wchan(struct task_struct *p)
421 {
422         unsigned long pc;
423
424         if (!p || p == current || p->state == TASK_RUNNING)
425                 return 0;
426
427         /*
428          * The same comment as on the Alpha applies here, too ...
429          */
430         pc = thread_saved_pc(p);
431
432 #ifdef CONFIG_FRAME_POINTER
433         if (in_sched_functions(pc)) {
434                 unsigned long schedule_frame = (unsigned long)p->thread.sp;
435                 return ((unsigned long *)schedule_frame)[21];
436         }
437 #endif
438
439         return pc;
440 }
441
442 asmlinkage void break_point_trap(void)
443 {
444         /* Clear tracing.  */
445 #if defined(CONFIG_CPU_SH4A)
446         ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
447         ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
448 #else
449         ctrl_outw(0, UBC_BBRA);
450         ctrl_outw(0, UBC_BBRB);
451         ctrl_outl(0, UBC_BRCR);
452 #endif
453         current->thread.ubc_pc = 0;
454         ubc_usercnt -= 1;
455
456         force_sig(SIGTRAP, current);
457 }