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> | |
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 | 26 | void (*pm_power_off)(void); |
5f97f7f9 HS |
27 | EXPORT_SYMBOL(pm_power_off); |
28 | ||
29 | /* | |
30 | * This file handles the architecture-dependent parts of process handling.. | |
31 | */ | |
32 | ||
33 | void 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 | ||
47 | void 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 | ||
58 | void machine_power_off(void) | |
59 | { | |
ed3fa7c9 PM |
60 | if (pm_power_off) |
61 | pm_power_off(); | |
5f97f7f9 HS |
62 | } |
63 | ||
64 | void 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 | */ | |
74 | void exit_thread(void) | |
75 | { | |
13b54a50 | 76 | ocd_disable(current); |
5f97f7f9 HS |
77 | } |
78 | ||
79 | void flush_thread(void) | |
80 | { | |
81 | /* nothing to do */ | |
82 | } | |
83 | ||
84 | void release_thread(struct task_struct *dead_task) | |
85 | { | |
86 | /* do nothing */ | |
87 | } | |
88 | ||
623b0355 HS |
89 | static 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 | ||
116 | out: | |
117 | return; | |
118 | } | |
119 | ||
120 | static 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 | |
127 | static 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 | |
166 | static 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 | ||
188 | void 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 | ||
211 | void show_stack(struct task_struct *tsk, unsigned long *stack) | |
212 | { | |
213 | show_stack_log_lvl(tsk, (unsigned long)stack, NULL, ""); | |
214 | } | |
215 | ||
216 | void dump_stack(void) | |
217 | { | |
218 | unsigned long stack; | |
219 | ||
220 | show_trace_log_lvl(current, &stack, NULL, ""); | |
221 | } | |
222 | EXPORT_SYMBOL(dump_stack); | |
223 | ||
5f97f7f9 HS |
224 | static 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 | 229 | void 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 | ||
277 | void 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 | } |
287 | EXPORT_SYMBOL(show_regs); | |
288 | ||
289 | /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */ | |
290 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu) | |
291 | { | |
292 | /* Not valid */ | |
293 | return 0; | |
294 | } | |
295 | ||
296 | asmlinkage void ret_from_fork(void); | |
5adc807f AV |
297 | asmlinkage void ret_from_kernel_thread(void); |
298 | asmlinkage void syscall_return(void); | |
5f97f7f9 | 299 | |
6f2c55b8 | 300 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
5adc807f | 301 | unsigned long arg, |
afa86fc4 | 302 | struct task_struct *p) |
5f97f7f9 | 303 | { |
5adc807f AV |
304 | struct pt_regs *childregs = task_pt_regs(p); |
305 | ||
584271bc | 306 | if (unlikely(p->flags & PF_KTHREAD)) { |
5adc807f AV |
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 { | |
584271bc AV |
314 | *childregs = *current_pt_regs(); |
315 | if (usp) | |
316 | childregs->sp = usp; | |
5adc807f AV |
317 | childregs->r12 = 0; /* Set return value for child */ |
318 | p->thread.cpu_context.pc = (unsigned long)ret_from_fork; | |
319 | } | |
5f97f7f9 HS |
320 | |
321 | p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM; | |
322 | p->thread.cpu_context.ksp = (unsigned long)childregs; | |
5f97f7f9 | 323 | |
325d6f55 | 324 | clear_tsk_thread_flag(p, TIF_DEBUG); |
13b54a50 HS |
325 | if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG)) |
326 | ocd_enable(p); | |
327 | ||
5f97f7f9 HS |
328 | return 0; |
329 | } | |
330 | ||
5f97f7f9 HS |
331 | /* |
332 | * This function is supposed to answer the question "who called | |
333 | * schedule()?" | |
334 | */ | |
335 | unsigned long get_wchan(struct task_struct *p) | |
336 | { | |
337 | unsigned long pc; | |
338 | unsigned long stack_page; | |
339 | ||
340 | if (!p || p == current || p->state == TASK_RUNNING) | |
341 | return 0; | |
342 | ||
c9f4f06d | 343 | stack_page = (unsigned long)task_stack_page(p); |
5f97f7f9 HS |
344 | BUG_ON(!stack_page); |
345 | ||
346 | /* | |
347 | * The stored value of PC is either the address right after | |
348 | * the call to __switch_to() or ret_from_fork. | |
349 | */ | |
350 | pc = thread_saved_pc(p); | |
351 | if (in_sched_functions(pc)) { | |
352 | #ifdef CONFIG_FRAME_POINTER | |
353 | unsigned long fp = p->thread.cpu_context.r7; | |
354 | BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page)); | |
355 | pc = *(unsigned long *)fp; | |
356 | #else | |
357 | /* | |
358 | * We depend on the frame size of schedule here, which | |
359 | * is actually quite ugly. It might be possible to | |
360 | * determine the frame size automatically at build | |
361 | * time by doing this: | |
362 | * - compile sched.c | |
363 | * - disassemble the resulting sched.o | |
364 | * - look for 'sub sp,??' shortly after '<schedule>:' | |
365 | */ | |
366 | unsigned long sp = p->thread.cpu_context.ksp + 16; | |
367 | BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page)); | |
368 | pc = *(unsigned long *)sp; | |
369 | #endif | |
370 | } | |
371 | ||
372 | return pc; | |
373 | } |