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