arm64: sanitize copy_thread(), switch to generic fork/vfork/clone
[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 13#include <linux/ptrace.h>
5a0e3ad6 14#include <linux/slab.h>
5f97f7f9 15#include <linux/reboot.h>
d45ad062 16#include <linux/tick.h>
623b0355 17#include <linux/uaccess.h>
5f97f7f9
HS
18#include <linux/unistd.h>
19
20#include <asm/sysreg.h>
21#include <asm/ocd.h>
c80ce2d5 22#include <asm/syscalls.h>
5f97f7f9 23
3663b736 24#include <mach/pm.h>
7e59128f 25
b3bc2c55 26void (*pm_power_off)(void);
5f97f7f9
HS
27EXPORT_SYMBOL(pm_power_off);
28
29/*
30 * This file handles the architecture-dependent parts of process handling..
31 */
32
33void cpu_idle(void)
34{
35 /* endless idle loop with no priority at all */
36 while (1) {
1268fbc7
FW
37 tick_nohz_idle_enter();
38 rcu_idle_enter();
5f97f7f9 39 while (!need_resched())
19b7ce8b 40 cpu_idle_sleep();
1268fbc7
FW
41 rcu_idle_exit();
42 tick_nohz_idle_exit();
bd2f5536 43 schedule_preempt_disabled();
5f97f7f9
HS
44 }
45}
46
47void machine_halt(void)
48{
c2eb5090
HS
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");
5f97f7f9
HS
56}
57
58void machine_power_off(void)
59{
ed3fa7c9
PM
60 if (pm_power_off)
61 pm_power_off();
5f97f7f9
HS
62}
63
64void machine_restart(char *cmd)
65{
8dfe8f29
HS
66 ocd_write(DC, (1 << OCD_DC_DBE_BIT));
67 ocd_write(DC, (1 << OCD_DC_RES_BIT));
5f97f7f9
HS
68 while (1) ;
69}
70
5f97f7f9
HS
71/*
72 * Free current thread data structures etc
73 */
74void exit_thread(void)
75{
13b54a50 76 ocd_disable(current);
5f97f7f9
HS
77}
78
79void flush_thread(void)
80{
81 /* nothing to do */
82}
83
84void release_thread(struct task_struct *dead_task)
85{
86 /* do nothing */
87}
88
623b0355
HS
89static 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
116out:
117 return;
118}
119
120static 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
127static 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
166static 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
188void 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
211void show_stack(struct task_struct *tsk, unsigned long *stack)
212{
213 show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
214}
215
216void dump_stack(void)
217{
218 unsigned long stack;
219
220 show_trace_log_lvl(current, &stack, NULL, "");
221}
222EXPORT_SYMBOL(dump_stack);
223
5f97f7f9
HS
224static const char *cpu_modes[] = {
225 "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
226 "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
227};
228
623b0355 229void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
5f97f7f9
HS
230{
231 unsigned long sp = regs->sp;
232 unsigned long lr = regs->lr;
233 unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
234
623b0355 235 if (!user_mode(regs)) {
5f97f7f9
HS
236 sp = (unsigned long)regs + FRAME_SIZE_FULL;
237
623b0355
HS
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,
5f97f7f9
HS
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');
df679771 260 printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl,
5f97f7f9 261 regs->sr & SR_H ? 'H' : 'h',
5f97f7f9 262 regs->sr & SR_J ? 'J' : 'j',
df679771
HS
263 regs->sr & SR_DM ? 'M' : 'm',
264 regs->sr & SR_D ? 'D' : 'd',
5f97f7f9
HS
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');
623b0355
HS
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
277void 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;
5f97f7f9 283
623b0355
HS
284 show_regs_log_lvl(regs, "");
285 show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
5f97f7f9
HS
286}
287EXPORT_SYMBOL(show_regs);
288
289/* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
290int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
291{
292 /* Not valid */
293 return 0;
294}
295
296asmlinkage void ret_from_fork(void);
5adc807f
AV
297asmlinkage void ret_from_kernel_thread(void);
298asmlinkage void syscall_return(void);
5f97f7f9 299
6f2c55b8 300int copy_thread(unsigned long clone_flags, unsigned long usp,
5adc807f 301 unsigned long arg,
5f97f7f9
HS
302 struct task_struct *p, struct pt_regs *regs)
303{
5adc807f
AV
304 struct pt_regs *childregs = task_pt_regs(p);
305
306 if (unlikely(!regs)) {
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 = *regs;
5f97f7f9 315 childregs->sp = usp;
5adc807f
AV
316 childregs->r12 = 0; /* Set return value for child */
317 p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
318 }
5f97f7f9
HS
319
320 p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
321 p->thread.cpu_context.ksp = (unsigned long)childregs;
5f97f7f9 322
325d6f55 323 clear_tsk_thread_flag(p, TIF_DEBUG);
13b54a50
HS
324 if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG))
325 ocd_enable(p);
326
5f97f7f9
HS
327 return 0;
328}
329
330/* r12-r8 are dummy parameters to force the compiler to use the stack */
331asmlinkage int sys_fork(struct pt_regs *regs)
332{
333 return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
334}
335
336asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
992a88b6
HCE
337 void __user *parent_tidptr, void __user *child_tidptr,
338 struct pt_regs *regs)
5f97f7f9
HS
339{
340 if (!newsp)
341 newsp = regs->sp;
992a88b6
HCE
342 return do_fork(clone_flags, newsp, regs, 0, parent_tidptr,
343 child_tidptr);
5f97f7f9
HS
344}
345
346asmlinkage int sys_vfork(struct pt_regs *regs)
347{
348 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs,
349 0, NULL, NULL);
350}
351
5f97f7f9
HS
352/*
353 * This function is supposed to answer the question "who called
354 * schedule()?"
355 */
356unsigned long get_wchan(struct task_struct *p)
357{
358 unsigned long pc;
359 unsigned long stack_page;
360
361 if (!p || p == current || p->state == TASK_RUNNING)
362 return 0;
363
c9f4f06d 364 stack_page = (unsigned long)task_stack_page(p);
5f97f7f9
HS
365 BUG_ON(!stack_page);
366
367 /*
368 * The stored value of PC is either the address right after
369 * the call to __switch_to() or ret_from_fork.
370 */
371 pc = thread_saved_pc(p);
372 if (in_sched_functions(pc)) {
373#ifdef CONFIG_FRAME_POINTER
374 unsigned long fp = p->thread.cpu_context.r7;
375 BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
376 pc = *(unsigned long *)fp;
377#else
378 /*
379 * We depend on the frame size of schedule here, which
380 * is actually quite ugly. It might be possible to
381 * determine the frame size automatically at build
382 * time by doing this:
383 * - compile sched.c
384 * - disassemble the resulting sched.o
385 * - look for 'sub sp,??' shortly after '<schedule>:'
386 */
387 unsigned long sp = p->thread.cpu_context.ksp + 16;
388 BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
389 pc = *(unsigned long *)sp;
390#endif
391 }
392
393 return pc;
394}
This page took 0.451877 seconds and 5 git commands to generate.