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