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