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