flagday: don't pass regs to copy_thread()
[deliverable/linux.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/slab.h>
15 #include <linux/reboot.h>
16 #include <linux/tick.h>
17 #include <linux/uaccess.h>
18 #include <linux/unistd.h>
19
20 #include <asm/sysreg.h>
21 #include <asm/ocd.h>
22 #include <asm/syscalls.h>
23
24 #include <mach/pm.h>
25
26 void (*pm_power_off)(void);
27 EXPORT_SYMBOL(pm_power_off);
28
29 /*
30 * This file handles the architecture-dependent parts of process handling..
31 */
32
33 void cpu_idle(void)
34 {
35 /* endless idle loop with no priority at all */
36 while (1) {
37 tick_nohz_idle_enter();
38 rcu_idle_enter();
39 while (!need_resched())
40 cpu_idle_sleep();
41 rcu_idle_exit();
42 tick_nohz_idle_exit();
43 schedule_preempt_disabled();
44 }
45 }
46
47 void machine_halt(void)
48 {
49 /*
50 * Enter Stop mode. The 32 kHz oscillator will keep running so
51 * the RTC will keep the time properly and the system will
52 * boot quickly.
53 */
54 asm volatile("sleep 3\n\t"
55 "sub pc, -2");
56 }
57
58 void machine_power_off(void)
59 {
60 if (pm_power_off)
61 pm_power_off();
62 }
63
64 void machine_restart(char *cmd)
65 {
66 ocd_write(DC, (1 << OCD_DC_DBE_BIT));
67 ocd_write(DC, (1 << OCD_DC_RES_BIT));
68 while (1) ;
69 }
70
71 /*
72 * Free current thread data structures etc
73 */
74 void exit_thread(void)
75 {
76 ocd_disable(current);
77 }
78
79 void flush_thread(void)
80 {
81 /* nothing to do */
82 }
83
84 void release_thread(struct task_struct *dead_task)
85 {
86 /* do nothing */
87 }
88
89 static void dump_mem(const char *str, const char *log_lvl,
90 unsigned long bottom, unsigned long top)
91 {
92 unsigned long p;
93 int i;
94
95 printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top);
96
97 for (p = bottom & ~31; p < top; ) {
98 printk("%s%04lx: ", log_lvl, p & 0xffff);
99
100 for (i = 0; i < 8; i++, p += 4) {
101 unsigned int val;
102
103 if (p < bottom || p >= top)
104 printk(" ");
105 else {
106 if (__get_user(val, (unsigned int __user *)p)) {
107 printk("\n");
108 goto out;
109 }
110 printk("%08x ", val);
111 }
112 }
113 printk("\n");
114 }
115
116 out:
117 return;
118 }
119
120 static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
121 {
122 return (p > (unsigned long)tinfo)
123 && (p < (unsigned long)tinfo + THREAD_SIZE - 3);
124 }
125
126 #ifdef CONFIG_FRAME_POINTER
127 static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
128 struct pt_regs *regs, const char *log_lvl)
129 {
130 unsigned long lr, fp;
131 struct thread_info *tinfo;
132
133 if (regs)
134 fp = regs->r7;
135 else if (tsk == current)
136 asm("mov %0, r7" : "=r"(fp));
137 else
138 fp = tsk->thread.cpu_context.r7;
139
140 /*
141 * Walk the stack as long as the frame pointer (a) is within
142 * the kernel stack of the task, and (b) it doesn't move
143 * downwards.
144 */
145 tinfo = task_thread_info(tsk);
146 printk("%sCall trace:\n", log_lvl);
147 while (valid_stack_ptr(tinfo, fp)) {
148 unsigned long new_fp;
149
150 lr = *(unsigned long *)fp;
151 #ifdef CONFIG_KALLSYMS
152 printk("%s [<%08lx>] ", log_lvl, lr);
153 #else
154 printk(" [<%08lx>] ", lr);
155 #endif
156 print_symbol("%s\n", lr);
157
158 new_fp = *(unsigned long *)(fp + 4);
159 if (new_fp <= fp)
160 break;
161 fp = new_fp;
162 }
163 printk("\n");
164 }
165 #else
166 static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
167 struct pt_regs *regs, const char *log_lvl)
168 {
169 unsigned long addr;
170
171 printk("%sCall trace:\n", log_lvl);
172
173 while (!kstack_end(sp)) {
174 addr = *sp++;
175 if (kernel_text_address(addr)) {
176 #ifdef CONFIG_KALLSYMS
177 printk("%s [<%08lx>] ", log_lvl, addr);
178 #else
179 printk(" [<%08lx>] ", addr);
180 #endif
181 print_symbol("%s\n", addr);
182 }
183 }
184 printk("\n");
185 }
186 #endif
187
188 void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp,
189 struct pt_regs *regs, const char *log_lvl)
190 {
191 struct thread_info *tinfo;
192
193 if (sp == 0) {
194 if (tsk)
195 sp = tsk->thread.cpu_context.ksp;
196 else
197 sp = (unsigned long)&tinfo;
198 }
199 if (!tsk)
200 tsk = current;
201
202 tinfo = task_thread_info(tsk);
203
204 if (valid_stack_ptr(tinfo, sp)) {
205 dump_mem("Stack: ", log_lvl, sp,
206 THREAD_SIZE + (unsigned long)tinfo);
207 show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl);
208 }
209 }
210
211 void show_stack(struct task_struct *tsk, unsigned long *stack)
212 {
213 show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
214 }
215
216 void dump_stack(void)
217 {
218 unsigned long stack;
219
220 show_trace_log_lvl(current, &stack, NULL, "");
221 }
222 EXPORT_SYMBOL(dump_stack);
223
224 static const char *cpu_modes[] = {
225 "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
226 "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
227 };
228
229 void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
230 {
231 unsigned long sp = regs->sp;
232 unsigned long lr = regs->lr;
233 unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
234
235 if (!user_mode(regs)) {
236 sp = (unsigned long)regs + FRAME_SIZE_FULL;
237
238 printk("%s", log_lvl);
239 print_symbol("PC is at %s\n", instruction_pointer(regs));
240 printk("%s", log_lvl);
241 print_symbol("LR is at %s\n", lr);
242 }
243
244 printk("%spc : [<%08lx>] lr : [<%08lx>] %s\n"
245 "%ssp : %08lx r12: %08lx r11: %08lx\n",
246 log_lvl, instruction_pointer(regs), lr, print_tainted(),
247 log_lvl, sp, regs->r12, regs->r11);
248 printk("%sr10: %08lx r9 : %08lx r8 : %08lx\n",
249 log_lvl, regs->r10, regs->r9, regs->r8);
250 printk("%sr7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
251 log_lvl, regs->r7, regs->r6, regs->r5, regs->r4);
252 printk("%sr3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
253 log_lvl, regs->r3, regs->r2, regs->r1, regs->r0);
254 printk("%sFlags: %c%c%c%c%c\n", log_lvl,
255 regs->sr & SR_Q ? 'Q' : 'q',
256 regs->sr & SR_V ? 'V' : 'v',
257 regs->sr & SR_N ? 'N' : 'n',
258 regs->sr & SR_Z ? 'Z' : 'z',
259 regs->sr & SR_C ? 'C' : 'c');
260 printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl,
261 regs->sr & SR_H ? 'H' : 'h',
262 regs->sr & SR_J ? 'J' : 'j',
263 regs->sr & SR_DM ? 'M' : 'm',
264 regs->sr & SR_D ? 'D' : 'd',
265 regs->sr & SR_EM ? 'E' : 'e',
266 regs->sr & SR_I3M ? '3' : '.',
267 regs->sr & SR_I2M ? '2' : '.',
268 regs->sr & SR_I1M ? '1' : '.',
269 regs->sr & SR_I0M ? '0' : '.',
270 regs->sr & SR_GM ? 'G' : 'g');
271 printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]);
272 printk("%sProcess: %s [%d] (task: %p thread: %p)\n",
273 log_lvl, current->comm, current->pid, current,
274 task_thread_info(current));
275 }
276
277 void show_regs(struct pt_regs *regs)
278 {
279 unsigned long sp = regs->sp;
280
281 if (!user_mode(regs))
282 sp = (unsigned long)regs + FRAME_SIZE_FULL;
283
284 show_regs_log_lvl(regs, "");
285 show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
286 }
287 EXPORT_SYMBOL(show_regs);
288
289 /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
290 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
291 {
292 /* Not valid */
293 return 0;
294 }
295
296 asmlinkage void ret_from_fork(void);
297 asmlinkage void ret_from_kernel_thread(void);
298 asmlinkage void syscall_return(void);
299
300 int copy_thread(unsigned long clone_flags, unsigned long usp,
301 unsigned long arg,
302 struct task_struct *p)
303 {
304 struct pt_regs *childregs = task_pt_regs(p);
305
306 if (unlikely(p->flags & PF_KTHREAD)) {
307 memset(childregs, 0, sizeof(struct pt_regs));
308 p->thread.cpu_context.r0 = arg;
309 p->thread.cpu_context.r1 = usp; /* fn */
310 p->thread.cpu_context.r2 = syscall_return;
311 p->thread.cpu_context.pc = (unsigned long)ret_from_kernel_thread;
312 childregs->sr = MODE_SUPERVISOR;
313 } else {
314 *childregs = *current_pt_regs();
315 if (usp)
316 childregs->sp = usp;
317 childregs->r12 = 0; /* Set return value for child */
318 p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
319 }
320
321 p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
322 p->thread.cpu_context.ksp = (unsigned long)childregs;
323
324 clear_tsk_thread_flag(p, TIF_DEBUG);
325 if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG))
326 ocd_enable(p);
327
328 return 0;
329 }
330
331 /*
332 * This function is supposed to answer the question "who called
333 * schedule()?"
334 */
335 unsigned long get_wchan(struct task_struct *p)
336 {
337 unsigned long pc;
338 unsigned long stack_page;
339
340 if (!p || p == current || p->state == TASK_RUNNING)
341 return 0;
342
343 stack_page = (unsigned long)task_stack_page(p);
344 BUG_ON(!stack_page);
345
346 /*
347 * The stored value of PC is either the address right after
348 * the call to __switch_to() or ret_from_fork.
349 */
350 pc = thread_saved_pc(p);
351 if (in_sched_functions(pc)) {
352 #ifdef CONFIG_FRAME_POINTER
353 unsigned long fp = p->thread.cpu_context.r7;
354 BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
355 pc = *(unsigned long *)fp;
356 #else
357 /*
358 * We depend on the frame size of schedule here, which
359 * is actually quite ugly. It might be possible to
360 * determine the frame size automatically at build
361 * time by doing this:
362 * - compile sched.c
363 * - disassemble the resulting sched.o
364 * - look for 'sub sp,??' shortly after '<schedule>:'
365 */
366 unsigned long sp = p->thread.cpu_context.ksp + 16;
367 BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
368 pc = *(unsigned long *)sp;
369 #endif
370 }
371
372 return pc;
373 }
This page took 0.042465 seconds and 5 git commands to generate.