2 * Copyright (C) 2004-2006 Atmel Corporation
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.
8 #include <linux/sched.h>
9 #include <linux/module.h>
10 #include <linux/kallsyms.h>
13 #include <linux/ptrace.h>
14 #include <linux/slab.h>
15 #include <linux/reboot.h>
16 #include <linux/tick.h>
17 #include <linux/uaccess.h>
18 #include <linux/unistd.h>
20 #include <asm/sysreg.h>
22 #include <asm/syscalls.h>
26 void (*pm_power_off
)(void);
27 EXPORT_SYMBOL(pm_power_off
);
30 * This file handles the architecture-dependent parts of process handling..
35 /* endless idle loop with no priority at all */
37 tick_nohz_idle_enter();
39 while (!need_resched())
42 tick_nohz_idle_exit();
43 schedule_preempt_disabled();
47 void machine_halt(void)
50 * Enter Stop mode. The 32 kHz oscillator will keep running so
51 * the RTC will keep the time properly and the system will
54 asm volatile("sleep 3\n\t"
58 void machine_power_off(void)
64 void machine_restart(char *cmd
)
66 ocd_write(DC
, (1 << OCD_DC_DBE_BIT
));
67 ocd_write(DC
, (1 << OCD_DC_RES_BIT
));
72 * Free current thread data structures etc
74 void exit_thread(void)
79 void flush_thread(void)
84 void release_thread(struct task_struct
*dead_task
)
89 static void dump_mem(const char *str
, const char *log_lvl
,
90 unsigned long bottom
, unsigned long top
)
95 printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl
, str
, bottom
, top
);
97 for (p
= bottom
& ~31; p
< top
; ) {
98 printk("%s%04lx: ", log_lvl
, p
& 0xffff);
100 for (i
= 0; i
< 8; i
++, p
+= 4) {
103 if (p
< bottom
|| p
>= top
)
106 if (__get_user(val
, (unsigned int __user
*)p
)) {
110 printk("%08x ", val
);
120 static inline int valid_stack_ptr(struct thread_info
*tinfo
, unsigned long p
)
122 return (p
> (unsigned long)tinfo
)
123 && (p
< (unsigned long)tinfo
+ THREAD_SIZE
- 3);
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
)
130 unsigned long lr
, fp
;
131 struct thread_info
*tinfo
;
135 else if (tsk
== current
)
136 asm("mov %0, r7" : "=r"(fp
));
138 fp
= tsk
->thread
.cpu_context
.r7
;
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
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
;
150 lr
= *(unsigned long *)fp
;
151 #ifdef CONFIG_KALLSYMS
152 printk("%s [<%08lx>] ", log_lvl
, lr
);
154 printk(" [<%08lx>] ", lr
);
156 print_symbol("%s\n", lr
);
158 new_fp
= *(unsigned long *)(fp
+ 4);
166 static void show_trace_log_lvl(struct task_struct
*tsk
, unsigned long *sp
,
167 struct pt_regs
*regs
, const char *log_lvl
)
171 printk("%sCall trace:\n", log_lvl
);
173 while (!kstack_end(sp
)) {
175 if (kernel_text_address(addr
)) {
176 #ifdef CONFIG_KALLSYMS
177 printk("%s [<%08lx>] ", log_lvl
, addr
);
179 printk(" [<%08lx>] ", addr
);
181 print_symbol("%s\n", addr
);
188 void show_stack_log_lvl(struct task_struct
*tsk
, unsigned long sp
,
189 struct pt_regs
*regs
, const char *log_lvl
)
191 struct thread_info
*tinfo
;
195 sp
= tsk
->thread
.cpu_context
.ksp
;
197 sp
= (unsigned long)&tinfo
;
202 tinfo
= task_thread_info(tsk
);
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
);
211 void show_stack(struct task_struct
*tsk
, unsigned long *stack
)
213 show_stack_log_lvl(tsk
, (unsigned long)stack
, NULL
, "");
216 void dump_stack(void)
220 show_trace_log_lvl(current
, &stack
, NULL
, "");
222 EXPORT_SYMBOL(dump_stack
);
224 static const char *cpu_modes
[] = {
225 "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
226 "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
229 void show_regs_log_lvl(struct pt_regs
*regs
, const char *log_lvl
)
231 unsigned long sp
= regs
->sp
;
232 unsigned long lr
= regs
->lr
;
233 unsigned long mode
= (regs
->sr
& MODE_MASK
) >> MODE_SHIFT
;
235 if (!user_mode(regs
)) {
236 sp
= (unsigned long)regs
+ FRAME_SIZE_FULL
;
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
);
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
,
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');
260 printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl
,
261 regs
->sr
& SR_H
? 'H' : 'h',
262 regs
->sr
& SR_J
? 'J' : 'j',
263 regs
->sr
& SR_DM
? 'M' : 'm',
264 regs
->sr
& SR_D
? 'D' : 'd',
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');
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
));
277 void show_regs(struct pt_regs
*regs
)
279 unsigned long sp
= regs
->sp
;
281 if (!user_mode(regs
))
282 sp
= (unsigned long)regs
+ FRAME_SIZE_FULL
;
284 show_regs_log_lvl(regs
, "");
285 show_trace_log_lvl(current
, (unsigned long *)sp
, regs
, "");
287 EXPORT_SYMBOL(show_regs
);
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
)
296 asmlinkage
void ret_from_fork(void);
297 asmlinkage
void ret_from_kernel_thread(void);
298 asmlinkage
void syscall_return(void);
300 int copy_thread(unsigned long clone_flags
, unsigned long usp
,
302 struct task_struct
*p
)
304 struct pt_regs
*childregs
= task_pt_regs(p
);
306 if (unlikely(p
->flags
& PF_KTHREAD
)) {
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
;
314 *childregs
= *current_pt_regs();
317 childregs
->r12
= 0; /* Set return value for child */
318 p
->thread
.cpu_context
.pc
= (unsigned long)ret_from_fork
;
321 p
->thread
.cpu_context
.sr
= MODE_SUPERVISOR
| SR_GM
;
322 p
->thread
.cpu_context
.ksp
= (unsigned long)childregs
;
324 clear_tsk_thread_flag(p
, TIF_DEBUG
);
325 if ((clone_flags
& CLONE_PTRACE
) && test_thread_flag(TIF_DEBUG
))
332 * This function is supposed to answer the question "who called
335 unsigned long get_wchan(struct task_struct
*p
)
338 unsigned long stack_page
;
340 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
343 stack_page
= (unsigned long)task_stack_page(p
);
347 * The stored value of PC is either the address right after
348 * the call to __switch_to() or ret_from_fork.
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
;
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:
363 * - disassemble the resulting sched.o
364 * - look for 'sub sp,??' shortly after '<schedule>:'
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
;