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