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