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