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