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