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