Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee13...
[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/ptrace.h>
13 #include <linux/reboot.h>
14 #include <linux/unistd.h>
15
16 #include <asm/sysreg.h>
17 #include <asm/ocd.h>
18
19 void (*pm_power_off)(void) = NULL;
20 EXPORT_SYMBOL(pm_power_off);
21
22 /*
23  * This file handles the architecture-dependent parts of process handling..
24  */
25
26 void cpu_idle(void)
27 {
28         /* endless idle loop with no priority at all */
29         while (1) {
30                 /* TODO: Enter sleep mode */
31                 while (!need_resched())
32                         cpu_relax();
33                 preempt_enable_no_resched();
34                 schedule();
35                 preempt_disable();
36         }
37 }
38
39 void machine_halt(void)
40 {
41 }
42
43 void machine_power_off(void)
44 {
45 }
46
47 void machine_restart(char *cmd)
48 {
49         __mtdr(DBGREG_DC, DC_DBE);
50         __mtdr(DBGREG_DC, DC_RES);
51         while (1) ;
52 }
53
54 /*
55  * PC is actually discarded when returning from a system call -- the
56  * return address must be stored in LR. This function will make sure
57  * LR points to do_exit before starting the thread.
58  *
59  * Also, when returning from fork(), r12 is 0, so we must copy the
60  * argument as well.
61  *
62  *  r0 : The argument to the main thread function
63  *  r1 : The address of do_exit
64  *  r2 : The address of the main thread function
65  */
66 asmlinkage extern void kernel_thread_helper(void);
67 __asm__("       .type   kernel_thread_helper, @function\n"
68         "kernel_thread_helper:\n"
69         "       mov     r12, r0\n"
70         "       mov     lr, r2\n"
71         "       mov     pc, r1\n"
72         "       .size   kernel_thread_helper, . - kernel_thread_helper");
73
74 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
75 {
76         struct pt_regs regs;
77
78         memset(&regs, 0, sizeof(regs));
79
80         regs.r0 = (unsigned long)arg;
81         regs.r1 = (unsigned long)fn;
82         regs.r2 = (unsigned long)do_exit;
83         regs.lr = (unsigned long)kernel_thread_helper;
84         regs.pc = (unsigned long)kernel_thread_helper;
85         regs.sr = MODE_SUPERVISOR;
86
87         return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
88                        0, &regs, 0, NULL, NULL);
89 }
90 EXPORT_SYMBOL(kernel_thread);
91
92 /*
93  * Free current thread data structures etc
94  */
95 void exit_thread(void)
96 {
97         /* nothing to do */
98 }
99
100 void flush_thread(void)
101 {
102         /* nothing to do */
103 }
104
105 void release_thread(struct task_struct *dead_task)
106 {
107         /* do nothing */
108 }
109
110 static const char *cpu_modes[] = {
111         "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
112         "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
113 };
114
115 void show_regs(struct pt_regs *regs)
116 {
117         unsigned long sp = regs->sp;
118         unsigned long lr = regs->lr;
119         unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
120
121         if (!user_mode(regs))
122                 sp = (unsigned long)regs + FRAME_SIZE_FULL;
123
124         print_symbol("PC is at %s\n", instruction_pointer(regs));
125         print_symbol("LR is at %s\n", lr);
126         printk("pc : [<%08lx>]    lr : [<%08lx>]    %s\n"
127                "sp : %08lx  r12: %08lx  r11: %08lx\n",
128                instruction_pointer(regs),
129                lr, print_tainted(), sp, regs->r12, regs->r11);
130         printk("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
131                regs->r10, regs->r9, regs->r8);
132         printk("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
133                regs->r7, regs->r6, regs->r5, regs->r4);
134         printk("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
135                regs->r3, regs->r2, regs->r1, regs->r0);
136         printk("Flags: %c%c%c%c%c\n",
137                regs->sr & SR_Q ? 'Q' : 'q',
138                regs->sr & SR_V ? 'V' : 'v',
139                regs->sr & SR_N ? 'N' : 'n',
140                regs->sr & SR_Z ? 'Z' : 'z',
141                regs->sr & SR_C ? 'C' : 'c');
142         printk("Mode bits: %c%c%c%c%c%c%c%c%c\n",
143                regs->sr & SR_H ? 'H' : 'h',
144                regs->sr & SR_R ? 'R' : 'r',
145                regs->sr & SR_J ? 'J' : 'j',
146                regs->sr & SR_EM ? 'E' : 'e',
147                regs->sr & SR_I3M ? '3' : '.',
148                regs->sr & SR_I2M ? '2' : '.',
149                regs->sr & SR_I1M ? '1' : '.',
150                regs->sr & SR_I0M ? '0' : '.',
151                regs->sr & SR_GM ? 'G' : 'g');
152         printk("CPU Mode: %s\n", cpu_modes[mode]);
153
154         show_trace(NULL, (unsigned long *)sp, regs);
155 }
156 EXPORT_SYMBOL(show_regs);
157
158 /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
159 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
160 {
161         /* Not valid */
162         return 0;
163 }
164
165 asmlinkage void ret_from_fork(void);
166
167 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
168                 unsigned long unused,
169                 struct task_struct *p, struct pt_regs *regs)
170 {
171         struct pt_regs *childregs;
172
173         childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)p->thread_info)) - 1;
174         *childregs = *regs;
175
176         if (user_mode(regs))
177                 childregs->sp = usp;
178         else
179                 childregs->sp = (unsigned long)p->thread_info + THREAD_SIZE;
180
181         childregs->r12 = 0; /* Set return value for child */
182
183         p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
184         p->thread.cpu_context.ksp = (unsigned long)childregs;
185         p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
186
187         return 0;
188 }
189
190 /* r12-r8 are dummy parameters to force the compiler to use the stack */
191 asmlinkage int sys_fork(struct pt_regs *regs)
192 {
193         return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
194 }
195
196 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
197                          unsigned long parent_tidptr,
198                          unsigned long child_tidptr, struct pt_regs *regs)
199 {
200         if (!newsp)
201                 newsp = regs->sp;
202         return do_fork(clone_flags, newsp, regs, 0,
203                        (int __user *)parent_tidptr,
204                        (int __user *)child_tidptr);
205 }
206
207 asmlinkage int sys_vfork(struct pt_regs *regs)
208 {
209         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs,
210                        0, NULL, NULL);
211 }
212
213 asmlinkage int sys_execve(char __user *ufilename, char __user *__user *uargv,
214                           char __user *__user *uenvp, struct pt_regs *regs)
215 {
216         int error;
217         char *filename;
218
219         filename = getname(ufilename);
220         error = PTR_ERR(filename);
221         if (IS_ERR(filename))
222                 goto out;
223
224         error = do_execve(filename, uargv, uenvp, regs);
225         if (error == 0)
226                 current->ptrace &= ~PT_DTRACE;
227         putname(filename);
228
229 out:
230         return error;
231 }
232
233
234 /*
235  * This function is supposed to answer the question "who called
236  * schedule()?"
237  */
238 unsigned long get_wchan(struct task_struct *p)
239 {
240         unsigned long pc;
241         unsigned long stack_page;
242
243         if (!p || p == current || p->state == TASK_RUNNING)
244                 return 0;
245
246         stack_page = (unsigned long)p->thread_info;
247         BUG_ON(!stack_page);
248
249         /*
250          * The stored value of PC is either the address right after
251          * the call to __switch_to() or ret_from_fork.
252          */
253         pc = thread_saved_pc(p);
254         if (in_sched_functions(pc)) {
255 #ifdef CONFIG_FRAME_POINTER
256                 unsigned long fp = p->thread.cpu_context.r7;
257                 BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
258                 pc = *(unsigned long *)fp;
259 #else
260                 /*
261                  * We depend on the frame size of schedule here, which
262                  * is actually quite ugly. It might be possible to
263                  * determine the frame size automatically at build
264                  * time by doing this:
265                  *   - compile sched.c
266                  *   - disassemble the resulting sched.o
267                  *   - look for 'sub sp,??' shortly after '<schedule>:'
268                  */
269                 unsigned long sp = p->thread.cpu_context.ksp + 16;
270                 BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
271                 pc = *(unsigned long *)sp;
272 #endif
273         }
274
275         return pc;
276 }