Merge branches 'sh/serial-rework' and 'sh/oprofile'
[pandora-kernel.git] / arch / avr32 / kernel / process.c
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #include <linux/sched.h>
9 #include <linux/module.h>
10 #include <linux/kallsyms.h>
11 #include <linux/fs.h>
12 #include <linux/pm.h>
13 #include <linux/ptrace.h>
14 #include <linux/reboot.h>
15 #include <linux/tick.h>
16 #include <linux/uaccess.h>
17 #include <linux/unistd.h>
18
19 #include <asm/sysreg.h>
20 #include <asm/ocd.h>
21
22 #include <mach/pm.h>
23
24 void (*pm_power_off)(void);
25 EXPORT_SYMBOL(pm_power_off);
26
27 /*
28  * This file handles the architecture-dependent parts of process handling..
29  */
30
31 void cpu_idle(void)
32 {
33         /* endless idle loop with no priority at all */
34         while (1) {
35                 tick_nohz_stop_sched_tick(1);
36                 while (!need_resched())
37                         cpu_idle_sleep();
38                 tick_nohz_restart_sched_tick();
39                 preempt_enable_no_resched();
40                 schedule();
41                 preempt_disable();
42         }
43 }
44
45 void machine_halt(void)
46 {
47         /*
48          * Enter Stop mode. The 32 kHz oscillator will keep running so
49          * the RTC will keep the time properly and the system will
50          * boot quickly.
51          */
52         asm volatile("sleep 3\n\t"
53                      "sub pc, -2");
54 }
55
56 void machine_power_off(void)
57 {
58         if (pm_power_off)
59                 pm_power_off();
60 }
61
62 void machine_restart(char *cmd)
63 {
64         ocd_write(DC, (1 << OCD_DC_DBE_BIT));
65         ocd_write(DC, (1 << OCD_DC_RES_BIT));
66         while (1) ;
67 }
68
69 /*
70  * PC is actually discarded when returning from a system call -- the
71  * return address must be stored in LR. This function will make sure
72  * LR points to do_exit before starting the thread.
73  *
74  * Also, when returning from fork(), r12 is 0, so we must copy the
75  * argument as well.
76  *
77  *  r0 : The argument to the main thread function
78  *  r1 : The address of do_exit
79  *  r2 : The address of the main thread function
80  */
81 asmlinkage extern void kernel_thread_helper(void);
82 __asm__("       .type   kernel_thread_helper, @function\n"
83         "kernel_thread_helper:\n"
84         "       mov     r12, r0\n"
85         "       mov     lr, r2\n"
86         "       mov     pc, r1\n"
87         "       .size   kernel_thread_helper, . - kernel_thread_helper");
88
89 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
90 {
91         struct pt_regs regs;
92
93         memset(&regs, 0, sizeof(regs));
94
95         regs.r0 = (unsigned long)arg;
96         regs.r1 = (unsigned long)fn;
97         regs.r2 = (unsigned long)do_exit;
98         regs.lr = (unsigned long)kernel_thread_helper;
99         regs.pc = (unsigned long)kernel_thread_helper;
100         regs.sr = MODE_SUPERVISOR;
101
102         return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
103                        0, &regs, 0, NULL, NULL);
104 }
105 EXPORT_SYMBOL(kernel_thread);
106
107 /*
108  * Free current thread data structures etc
109  */
110 void exit_thread(void)
111 {
112         ocd_disable(current);
113 }
114
115 void flush_thread(void)
116 {
117         /* nothing to do */
118 }
119
120 void release_thread(struct task_struct *dead_task)
121 {
122         /* do nothing */
123 }
124
125 static void dump_mem(const char *str, const char *log_lvl,
126                      unsigned long bottom, unsigned long top)
127 {
128         unsigned long p;
129         int i;
130
131         printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top);
132
133         for (p = bottom & ~31; p < top; ) {
134                 printk("%s%04lx: ", log_lvl, p & 0xffff);
135
136                 for (i = 0; i < 8; i++, p += 4) {
137                         unsigned int val;
138
139                         if (p < bottom || p >= top)
140                                 printk("         ");
141                         else {
142                                 if (__get_user(val, (unsigned int __user *)p)) {
143                                         printk("\n");
144                                         goto out;
145                                 }
146                                 printk("%08x ", val);
147                         }
148                 }
149                 printk("\n");
150         }
151
152 out:
153         return;
154 }
155
156 static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
157 {
158         return (p > (unsigned long)tinfo)
159                 && (p < (unsigned long)tinfo + THREAD_SIZE - 3);
160 }
161
162 #ifdef CONFIG_FRAME_POINTER
163 static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
164                                struct pt_regs *regs, const char *log_lvl)
165 {
166         unsigned long lr, fp;
167         struct thread_info *tinfo;
168
169         if (regs)
170                 fp = regs->r7;
171         else if (tsk == current)
172                 asm("mov %0, r7" : "=r"(fp));
173         else
174                 fp = tsk->thread.cpu_context.r7;
175
176         /*
177          * Walk the stack as long as the frame pointer (a) is within
178          * the kernel stack of the task, and (b) it doesn't move
179          * downwards.
180          */
181         tinfo = task_thread_info(tsk);
182         printk("%sCall trace:\n", log_lvl);
183         while (valid_stack_ptr(tinfo, fp)) {
184                 unsigned long new_fp;
185
186                 lr = *(unsigned long *)fp;
187 #ifdef CONFIG_KALLSYMS
188                 printk("%s [<%08lx>] ", log_lvl, lr);
189 #else
190                 printk(" [<%08lx>] ", lr);
191 #endif
192                 print_symbol("%s\n", lr);
193
194                 new_fp = *(unsigned long *)(fp + 4);
195                 if (new_fp <= fp)
196                         break;
197                 fp = new_fp;
198         }
199         printk("\n");
200 }
201 #else
202 static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
203                                struct pt_regs *regs, const char *log_lvl)
204 {
205         unsigned long addr;
206
207         printk("%sCall trace:\n", log_lvl);
208
209         while (!kstack_end(sp)) {
210                 addr = *sp++;
211                 if (kernel_text_address(addr)) {
212 #ifdef CONFIG_KALLSYMS
213                         printk("%s [<%08lx>] ", log_lvl, addr);
214 #else
215                         printk(" [<%08lx>] ", addr);
216 #endif
217                         print_symbol("%s\n", addr);
218                 }
219         }
220         printk("\n");
221 }
222 #endif
223
224 void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp,
225                         struct pt_regs *regs, const char *log_lvl)
226 {
227         struct thread_info *tinfo;
228
229         if (sp == 0) {
230                 if (tsk)
231                         sp = tsk->thread.cpu_context.ksp;
232                 else
233                         sp = (unsigned long)&tinfo;
234         }
235         if (!tsk)
236                 tsk = current;
237
238         tinfo = task_thread_info(tsk);
239
240         if (valid_stack_ptr(tinfo, sp)) {
241                 dump_mem("Stack: ", log_lvl, sp,
242                          THREAD_SIZE + (unsigned long)tinfo);
243                 show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl);
244         }
245 }
246
247 void show_stack(struct task_struct *tsk, unsigned long *stack)
248 {
249         show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
250 }
251
252 void dump_stack(void)
253 {
254         unsigned long stack;
255
256         show_trace_log_lvl(current, &stack, NULL, "");
257 }
258 EXPORT_SYMBOL(dump_stack);
259
260 static const char *cpu_modes[] = {
261         "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
262         "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
263 };
264
265 void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
266 {
267         unsigned long sp = regs->sp;
268         unsigned long lr = regs->lr;
269         unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
270
271         if (!user_mode(regs)) {
272                 sp = (unsigned long)regs + FRAME_SIZE_FULL;
273
274                 printk("%s", log_lvl);
275                 print_symbol("PC is at %s\n", instruction_pointer(regs));
276                 printk("%s", log_lvl);
277                 print_symbol("LR is at %s\n", lr);
278         }
279
280         printk("%spc : [<%08lx>]    lr : [<%08lx>]    %s\n"
281                "%ssp : %08lx  r12: %08lx  r11: %08lx\n",
282                log_lvl, instruction_pointer(regs), lr, print_tainted(),
283                log_lvl, sp, regs->r12, regs->r11);
284         printk("%sr10: %08lx  r9 : %08lx  r8 : %08lx\n",
285                log_lvl, regs->r10, regs->r9, regs->r8);
286         printk("%sr7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
287                log_lvl, regs->r7, regs->r6, regs->r5, regs->r4);
288         printk("%sr3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
289                log_lvl, regs->r3, regs->r2, regs->r1, regs->r0);
290         printk("%sFlags: %c%c%c%c%c\n", log_lvl,
291                regs->sr & SR_Q ? 'Q' : 'q',
292                regs->sr & SR_V ? 'V' : 'v',
293                regs->sr & SR_N ? 'N' : 'n',
294                regs->sr & SR_Z ? 'Z' : 'z',
295                regs->sr & SR_C ? 'C' : 'c');
296         printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl,
297                regs->sr & SR_H ? 'H' : 'h',
298                regs->sr & SR_J ? 'J' : 'j',
299                regs->sr & SR_DM ? 'M' : 'm',
300                regs->sr & SR_D ? 'D' : 'd',
301                regs->sr & SR_EM ? 'E' : 'e',
302                regs->sr & SR_I3M ? '3' : '.',
303                regs->sr & SR_I2M ? '2' : '.',
304                regs->sr & SR_I1M ? '1' : '.',
305                regs->sr & SR_I0M ? '0' : '.',
306                regs->sr & SR_GM ? 'G' : 'g');
307         printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]);
308         printk("%sProcess: %s [%d] (task: %p thread: %p)\n",
309                log_lvl, current->comm, current->pid, current,
310                task_thread_info(current));
311 }
312
313 void show_regs(struct pt_regs *regs)
314 {
315         unsigned long sp = regs->sp;
316
317         if (!user_mode(regs))
318                 sp = (unsigned long)regs + FRAME_SIZE_FULL;
319
320         show_regs_log_lvl(regs, "");
321         show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
322 }
323 EXPORT_SYMBOL(show_regs);
324
325 /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
326 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
327 {
328         /* Not valid */
329         return 0;
330 }
331
332 asmlinkage void ret_from_fork(void);
333
334 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
335                 unsigned long unused,
336                 struct task_struct *p, struct pt_regs *regs)
337 {
338         struct pt_regs *childregs;
339
340         childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)task_stack_page(p))) - 1;
341         *childregs = *regs;
342
343         if (user_mode(regs))
344                 childregs->sp = usp;
345         else
346                 childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
347
348         childregs->r12 = 0; /* Set return value for child */
349
350         p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
351         p->thread.cpu_context.ksp = (unsigned long)childregs;
352         p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
353
354         clear_tsk_thread_flag(p, TIF_DEBUG);
355         if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG))
356                 ocd_enable(p);
357
358         return 0;
359 }
360
361 /* r12-r8 are dummy parameters to force the compiler to use the stack */
362 asmlinkage int sys_fork(struct pt_regs *regs)
363 {
364         return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
365 }
366
367 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
368                          unsigned long parent_tidptr,
369                          unsigned long child_tidptr, struct pt_regs *regs)
370 {
371         if (!newsp)
372                 newsp = regs->sp;
373         return do_fork(clone_flags, newsp, regs, 0,
374                        (int __user *)parent_tidptr,
375                        (int __user *)child_tidptr);
376 }
377
378 asmlinkage int sys_vfork(struct pt_regs *regs)
379 {
380         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs,
381                        0, NULL, NULL);
382 }
383
384 asmlinkage int sys_execve(char __user *ufilename, char __user *__user *uargv,
385                           char __user *__user *uenvp, struct pt_regs *regs)
386 {
387         int error;
388         char *filename;
389
390         filename = getname(ufilename);
391         error = PTR_ERR(filename);
392         if (IS_ERR(filename))
393                 goto out;
394
395         error = do_execve(filename, uargv, uenvp, regs);
396         if (error == 0)
397                 current->ptrace &= ~PT_DTRACE;
398         putname(filename);
399
400 out:
401         return error;
402 }
403
404
405 /*
406  * This function is supposed to answer the question "who called
407  * schedule()?"
408  */
409 unsigned long get_wchan(struct task_struct *p)
410 {
411         unsigned long pc;
412         unsigned long stack_page;
413
414         if (!p || p == current || p->state == TASK_RUNNING)
415                 return 0;
416
417         stack_page = (unsigned long)task_stack_page(p);
418         BUG_ON(!stack_page);
419
420         /*
421          * The stored value of PC is either the address right after
422          * the call to __switch_to() or ret_from_fork.
423          */
424         pc = thread_saved_pc(p);
425         if (in_sched_functions(pc)) {
426 #ifdef CONFIG_FRAME_POINTER
427                 unsigned long fp = p->thread.cpu_context.r7;
428                 BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
429                 pc = *(unsigned long *)fp;
430 #else
431                 /*
432                  * We depend on the frame size of schedule here, which
433                  * is actually quite ugly. It might be possible to
434                  * determine the frame size automatically at build
435                  * time by doing this:
436                  *   - compile sched.c
437                  *   - disassemble the resulting sched.o
438                  *   - look for 'sub sp,??' shortly after '<schedule>:'
439                  */
440                 unsigned long sp = p->thread.cpu_context.ksp + 16;
441                 BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
442                 pc = *(unsigned long *)sp;
443 #endif
444         }
445
446         return pc;
447 }