sh: generic quicklist support.
[powerpc.git] / arch / sh / kernel / process.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 - 2007  Paul Mundt
11  */
12 #include <linux/module.h>
13 #include <linux/mm.h>
14 #include <linux/elfcore.h>
15 #include <linux/pm.h>
16 #include <linux/kallsyms.h>
17 #include <linux/kexec.h>
18 #include <asm/kdebug.h>
19 #include <asm/uaccess.h>
20 #include <asm/mmu_context.h>
21 #include <asm/pgalloc.h>
22 #include <asm/ubc.h>
23
24 static int hlt_counter;
25 int ubc_usercnt = 0;
26
27 #define HARD_IDLE_TIMEOUT (HZ / 3)
28
29 void (*pm_idle)(void);
30 void (*pm_power_off)(void);
31 EXPORT_SYMBOL(pm_power_off);
32
33 void disable_hlt(void)
34 {
35         hlt_counter++;
36 }
37 EXPORT_SYMBOL(disable_hlt);
38
39 void enable_hlt(void)
40 {
41         hlt_counter--;
42 }
43 EXPORT_SYMBOL(enable_hlt);
44
45 void default_idle(void)
46 {
47         if (!hlt_counter)
48                 cpu_sleep();
49         else
50                 cpu_relax();
51 }
52
53 void cpu_idle(void)
54 {
55         /* endless idle loop with no priority at all */
56         while (1) {
57                 void (*idle)(void) = pm_idle;
58
59                 if (!idle)
60                         idle = default_idle;
61
62                 while (!need_resched())
63                         idle();
64
65                 preempt_enable_no_resched();
66                 schedule();
67                 preempt_disable();
68                 check_pgt_cache();
69         }
70 }
71
72 void machine_restart(char * __unused)
73 {
74         /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
75         asm volatile("ldc %0, sr\n\t"
76                      "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
77 }
78
79 void machine_halt(void)
80 {
81         local_irq_disable();
82
83         while (1)
84                 cpu_sleep();
85 }
86
87 void machine_power_off(void)
88 {
89         if (pm_power_off)
90                 pm_power_off();
91 }
92
93 void show_regs(struct pt_regs * regs)
94 {
95         printk("\n");
96         printk("Pid : %d, Comm: %20s\n", current->pid, current->comm);
97         print_symbol("PC is at %s\n", instruction_pointer(regs));
98         printk("PC  : %08lx SP  : %08lx SR  : %08lx ",
99                regs->pc, regs->regs[15], regs->sr);
100 #ifdef CONFIG_MMU
101         printk("TEA : %08x    ", ctrl_inl(MMU_TEA));
102 #else
103         printk("                  ");
104 #endif
105         printk("%s\n", print_tainted());
106
107         printk("R0  : %08lx R1  : %08lx R2  : %08lx R3  : %08lx\n",
108                regs->regs[0],regs->regs[1],
109                regs->regs[2],regs->regs[3]);
110         printk("R4  : %08lx R5  : %08lx R6  : %08lx R7  : %08lx\n",
111                regs->regs[4],regs->regs[5],
112                regs->regs[6],regs->regs[7]);
113         printk("R8  : %08lx R9  : %08lx R10 : %08lx R11 : %08lx\n",
114                regs->regs[8],regs->regs[9],
115                regs->regs[10],regs->regs[11]);
116         printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
117                regs->regs[12],regs->regs[13],
118                regs->regs[14]);
119         printk("MACH: %08lx MACL: %08lx GBR : %08lx PR  : %08lx\n",
120                regs->mach, regs->macl, regs->gbr, regs->pr);
121
122         show_trace(NULL, (unsigned long *)regs->regs[15], regs);
123 }
124
125 /*
126  * Create a kernel thread
127  */
128
129 /*
130  * This is the mechanism for creating a new kernel thread.
131  *
132  */
133 extern void kernel_thread_helper(void);
134 __asm__(".align 5\n"
135         "kernel_thread_helper:\n\t"
136         "jsr    @r5\n\t"
137         " nop\n\t"
138         "mov.l  1f, r1\n\t"
139         "jsr    @r1\n\t"
140         " mov   r0, r4\n\t"
141         ".align 2\n\t"
142         "1:.long do_exit");
143
144 /* Don't use this in BL=1(cli).  Or else, CPU resets! */
145 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
146 {
147         struct pt_regs regs;
148
149         memset(&regs, 0, sizeof(regs));
150         regs.regs[4] = (unsigned long)arg;
151         regs.regs[5] = (unsigned long)fn;
152
153         regs.pc = (unsigned long)kernel_thread_helper;
154         regs.sr = (1 << 30);
155
156         /* Ok, create the new process.. */
157         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
158                        &regs, 0, NULL, NULL);
159 }
160
161 /*
162  * Free current thread data structures etc..
163  */
164 void exit_thread(void)
165 {
166         if (current->thread.ubc_pc) {
167                 current->thread.ubc_pc = 0;
168                 ubc_usercnt -= 1;
169         }
170 }
171
172 void flush_thread(void)
173 {
174 #if defined(CONFIG_SH_FPU)
175         struct task_struct *tsk = current;
176         /* Forget lazy FPU state */
177         clear_fpu(tsk, task_pt_regs(tsk));
178         clear_used_math();
179 #endif
180 }
181
182 void release_thread(struct task_struct *dead_task)
183 {
184         /* do nothing */
185 }
186
187 /* Fill in the fpu structure for a core dump.. */
188 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
189 {
190         int fpvalid = 0;
191
192 #if defined(CONFIG_SH_FPU)
193         struct task_struct *tsk = current;
194
195         fpvalid = !!tsk_used_math(tsk);
196         if (fpvalid) {
197                 unlazy_fpu(tsk, regs);
198                 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
199         }
200 #endif
201
202         return fpvalid;
203 }
204
205 /*
206  * Capture the user space registers if the task is not running (in user space)
207  */
208 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
209 {
210         struct pt_regs ptregs;
211
212         ptregs = *task_pt_regs(tsk);
213         elf_core_copy_regs(regs, &ptregs);
214
215         return 1;
216 }
217
218 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpu)
219 {
220         int fpvalid = 0;
221
222 #if defined(CONFIG_SH_FPU)
223         fpvalid = !!tsk_used_math(tsk);
224         if (fpvalid) {
225                 unlazy_fpu(tsk, task_pt_regs(tsk));
226                 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
227         }
228 #endif
229
230         return fpvalid;
231 }
232
233 asmlinkage void ret_from_fork(void);
234
235 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
236                 unsigned long unused,
237                 struct task_struct *p, struct pt_regs *regs)
238 {
239         struct thread_info *ti = task_thread_info(p);
240         struct pt_regs *childregs;
241 #if defined(CONFIG_SH_FPU)
242         struct task_struct *tsk = current;
243
244         unlazy_fpu(tsk, regs);
245         p->thread.fpu = tsk->thread.fpu;
246         copy_to_stopped_child_used_math(p);
247 #endif
248
249         childregs = task_pt_regs(p);
250         *childregs = *regs;
251
252         if (user_mode(regs)) {
253                 childregs->regs[15] = usp;
254                 ti->addr_limit = USER_DS;
255         } else {
256                 childregs->regs[15] = (unsigned long)childregs;
257                 ti->addr_limit = KERNEL_DS;
258         }
259
260         if (clone_flags & CLONE_SETTLS)
261                 childregs->gbr = childregs->regs[0];
262
263         childregs->regs[0] = 0; /* Set return value for child */
264
265         p->thread.sp = (unsigned long) childregs;
266         p->thread.pc = (unsigned long) ret_from_fork;
267
268         p->thread.ubc_pc = 0;
269
270         return 0;
271 }
272
273 /* Tracing by user break controller.  */
274 static void ubc_set_tracing(int asid, unsigned long pc)
275 {
276 #if defined(CONFIG_CPU_SH4A)
277         unsigned long val;
278
279         val = (UBC_CBR_ID_INST | UBC_CBR_RW_READ | UBC_CBR_CE);
280         val |= (UBC_CBR_AIE | UBC_CBR_AIV_SET(asid));
281
282         ctrl_outl(val, UBC_CBR0);
283         ctrl_outl(pc,  UBC_CAR0);
284         ctrl_outl(0x0, UBC_CAMR0);
285         ctrl_outl(0x0, UBC_CBCR);
286
287         val = (UBC_CRR_RES | UBC_CRR_PCB | UBC_CRR_BIE);
288         ctrl_outl(val, UBC_CRR0);
289
290         /* Read UBC register that we wrote last, for checking update */
291         val = ctrl_inl(UBC_CRR0);
292
293 #else   /* CONFIG_CPU_SH4A */
294         ctrl_outl(pc, UBC_BARA);
295
296 #ifdef CONFIG_MMU
297         /* We don't have any ASID settings for the SH-2! */
298         if (current_cpu_data.type != CPU_SH7604)
299                 ctrl_outb(asid, UBC_BASRA);
300 #endif
301
302         ctrl_outl(0, UBC_BAMRA);
303
304         if (current_cpu_data.type == CPU_SH7729 ||
305             current_cpu_data.type == CPU_SH7710 ||
306             current_cpu_data.type == CPU_SH7712) {
307                 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
308                 ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
309         } else {
310                 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
311                 ctrl_outw(BRCR_PCBA, UBC_BRCR);
312         }
313 #endif  /* CONFIG_CPU_SH4A */
314 }
315
316 /*
317  *      switch_to(x,y) should switch tasks from x to y.
318  *
319  */
320 struct task_struct *__switch_to(struct task_struct *prev,
321                                 struct task_struct *next)
322 {
323 #if defined(CONFIG_SH_FPU)
324         unlazy_fpu(prev, task_pt_regs(prev));
325 #endif
326
327 #ifdef CONFIG_PREEMPT
328         {
329                 unsigned long flags;
330                 struct pt_regs *regs;
331
332                 local_irq_save(flags);
333                 regs = task_pt_regs(prev);
334                 if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
335                         int offset = (int)regs->regs[15];
336
337                         /* Reset stack pointer: clear critical region mark */
338                         regs->regs[15] = regs->regs[1];
339                         if (regs->pc < regs->regs[0])
340                                 /* Go to rewind point */
341                                 regs->pc = regs->regs[0] + offset;
342                 }
343                 local_irq_restore(flags);
344         }
345 #endif
346
347 #ifdef CONFIG_MMU
348         /*
349          * Restore the kernel mode register
350          *      k7 (r7_bank1)
351          */
352         asm volatile("ldc       %0, r7_bank"
353                      : /* no output */
354                      : "r" (task_thread_info(next)));
355 #endif
356
357         /* If no tasks are using the UBC, we're done */
358         if (ubc_usercnt == 0)
359                 /* If no tasks are using the UBC, we're done */;
360         else if (next->thread.ubc_pc && next->mm) {
361                 int asid = 0;
362 #ifdef CONFIG_MMU
363                 asid |= cpu_asid(smp_processor_id(), next->mm);
364 #endif
365                 ubc_set_tracing(asid, next->thread.ubc_pc);
366         } else {
367 #if defined(CONFIG_CPU_SH4A)
368                 ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
369                 ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
370 #else
371                 ctrl_outw(0, UBC_BBRA);
372                 ctrl_outw(0, UBC_BBRB);
373 #endif
374         }
375
376         return prev;
377 }
378
379 asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
380                         unsigned long r6, unsigned long r7,
381                         struct pt_regs __regs)
382 {
383         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
384 #ifdef CONFIG_MMU
385         return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
386 #else
387         /* fork almost works, enough to trick you into looking elsewhere :-( */
388         return -EINVAL;
389 #endif
390 }
391
392 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
393                          unsigned long parent_tidptr,
394                          unsigned long child_tidptr,
395                          struct pt_regs __regs)
396 {
397         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
398         if (!newsp)
399                 newsp = regs->regs[15];
400         return do_fork(clone_flags, newsp, regs, 0,
401                         (int __user *)parent_tidptr,
402                         (int __user *)child_tidptr);
403 }
404
405 /*
406  * This is trivial, and on the face of it looks like it
407  * could equally well be done in user mode.
408  *
409  * Not so, for quite unobvious reasons - register pressure.
410  * In user mode vfork() cannot have a stack frame, and if
411  * done by calling the "clone()" system call directly, you
412  * do not have enough call-clobbered registers to hold all
413  * the information you need.
414  */
415 asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
416                          unsigned long r6, unsigned long r7,
417                          struct pt_regs __regs)
418 {
419         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
420         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
421                        0, NULL, NULL);
422 }
423
424 /*
425  * sys_execve() executes a new program.
426  */
427 asmlinkage int sys_execve(char *ufilename, char **uargv,
428                           char **uenvp, unsigned long r7,
429                           struct pt_regs __regs)
430 {
431         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
432         int error;
433         char *filename;
434
435         filename = getname((char __user *)ufilename);
436         error = PTR_ERR(filename);
437         if (IS_ERR(filename))
438                 goto out;
439
440         error = do_execve(filename,
441                           (char __user * __user *)uargv,
442                           (char __user * __user *)uenvp,
443                           regs);
444         if (error == 0) {
445                 task_lock(current);
446                 current->ptrace &= ~PT_DTRACE;
447                 task_unlock(current);
448         }
449         putname(filename);
450 out:
451         return error;
452 }
453
454 unsigned long get_wchan(struct task_struct *p)
455 {
456         unsigned long schedule_frame;
457         unsigned long pc;
458
459         if (!p || p == current || p->state == TASK_RUNNING)
460                 return 0;
461
462         /*
463          * The same comment as on the Alpha applies here, too ...
464          */
465         pc = thread_saved_pc(p);
466         if (in_sched_functions(pc)) {
467                 schedule_frame = (unsigned long)p->thread.sp;
468                 return ((unsigned long *)schedule_frame)[21];
469         }
470
471         return pc;
472 }
473
474 asmlinkage void break_point_trap(void)
475 {
476         /* Clear tracing.  */
477 #if defined(CONFIG_CPU_SH4A)
478         ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
479         ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
480 #else
481         ctrl_outw(0, UBC_BBRA);
482         ctrl_outw(0, UBC_BBRB);
483 #endif
484         current->thread.ubc_pc = 0;
485         ubc_usercnt -= 1;
486
487         force_sig(SIGTRAP, current);
488 }
489
490 /*
491  * Generic trap handler.
492  */
493 asmlinkage void debug_trap_handler(unsigned long r4, unsigned long r5,
494                                    unsigned long r6, unsigned long r7,
495                                    struct pt_regs __regs)
496 {
497         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
498
499         /* Rewind */
500         regs->pc -= 2;
501
502         if (notify_die(DIE_TRAP, regs, regs->tra & 0xff,
503                        SIGTRAP) == NOTIFY_STOP)
504                 return;
505
506         force_sig(SIGTRAP, current);
507 }
508
509 /*
510  * Special handler for BUG() traps.
511  */
512 asmlinkage void bug_trap_handler(unsigned long r4, unsigned long r5,
513                                  unsigned long r6, unsigned long r7,
514                                  struct pt_regs __regs)
515 {
516         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
517
518         /* Rewind */
519         regs->pc -= 2;
520
521         if (notify_die(DIE_TRAP, regs, TRAPA_BUG_OPCODE & 0xff,
522                        SIGTRAP) == NOTIFY_STOP)
523                 return;
524
525 #ifdef CONFIG_BUG
526         if (__kernel_text_address(instruction_pointer(regs))) {
527                 u16 insn = *(u16 *)instruction_pointer(regs);
528                 if (insn == TRAPA_BUG_OPCODE)
529                         handle_BUG(regs);
530         }
531 #endif
532
533         force_sig(SIGTRAP, current);
534 }