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>
12 #include <linux/ptrace.h>
13 #include <linux/reboot.h>
14 <<<<<<< HEAD
:arch
/avr32
/kernel
/process
.c
16 #include <linux/tick.h>
17 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/avr32
/kernel
/process
.c
18 #include <linux/uaccess.h>
19 #include <linux/unistd.h>
21 #include <asm/sysreg.h>
24 void (*pm_power_off
)(void) = NULL
;
25 EXPORT_SYMBOL(pm_power_off
);
27 extern void cpu_idle_sleep(void);
30 * This file handles the architecture-dependent parts of process handling..
35 /* endless idle loop with no priority at all */
37 <<<<<<< HEAD
:arch
/avr32
/kernel
/process
.c
39 tick_nohz_stop_sched_tick();
40 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/avr32
/kernel
/process
.c
41 while (!need_resched())
43 <<<<<<< HEAD
:arch
/avr32
/kernel
/process
.c
45 tick_nohz_restart_sched_tick();
46 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/avr32
/kernel
/process
.c
47 preempt_enable_no_resched();
53 void machine_halt(void)
56 * Enter Stop mode. The 32 kHz oscillator will keep running so
57 * the RTC will keep the time properly and the system will
60 asm volatile("sleep 3\n\t"
64 void machine_power_off(void)
68 void machine_restart(char *cmd
)
70 ocd_write(DC
, (1 << OCD_DC_DBE_BIT
));
71 ocd_write(DC
, (1 << OCD_DC_RES_BIT
));
76 * PC is actually discarded when returning from a system call -- the
77 * return address must be stored in LR. This function will make sure
78 * LR points to do_exit before starting the thread.
80 * Also, when returning from fork(), r12 is 0, so we must copy the
83 * r0 : The argument to the main thread function
84 * r1 : The address of do_exit
85 * r2 : The address of the main thread function
87 asmlinkage
extern void kernel_thread_helper(void);
88 __asm__(" .type kernel_thread_helper, @function\n"
89 "kernel_thread_helper:\n"
93 " .size kernel_thread_helper, . - kernel_thread_helper");
95 int kernel_thread(int (*fn
)(void *), void *arg
, unsigned long flags
)
99 memset(®s
, 0, sizeof(regs
));
101 regs
.r0
= (unsigned long)arg
;
102 regs
.r1
= (unsigned long)fn
;
103 regs
.r2
= (unsigned long)do_exit
;
104 regs
.lr
= (unsigned long)kernel_thread_helper
;
105 regs
.pc
= (unsigned long)kernel_thread_helper
;
106 regs
.sr
= MODE_SUPERVISOR
;
108 return do_fork(flags
| CLONE_VM
| CLONE_UNTRACED
,
109 0, ®s
, 0, NULL
, NULL
);
111 EXPORT_SYMBOL(kernel_thread
);
114 * Free current thread data structures etc
116 void exit_thread(void)
118 ocd_disable(current
);
121 void flush_thread(void)
126 void release_thread(struct task_struct
*dead_task
)
131 static void dump_mem(const char *str
, const char *log_lvl
,
132 unsigned long bottom
, unsigned long top
)
137 printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl
, str
, bottom
, top
);
139 for (p
= bottom
& ~31; p
< top
; ) {
140 printk("%s%04lx: ", log_lvl
, p
& 0xffff);
142 for (i
= 0; i
< 8; i
++, p
+= 4) {
145 if (p
< bottom
|| p
>= top
)
148 if (__get_user(val
, (unsigned int __user
*)p
)) {
152 printk("%08x ", val
);
162 static inline int valid_stack_ptr(struct thread_info
*tinfo
, unsigned long p
)
164 return (p
> (unsigned long)tinfo
)
165 && (p
< (unsigned long)tinfo
+ THREAD_SIZE
- 3);
168 #ifdef CONFIG_FRAME_POINTER
169 static void show_trace_log_lvl(struct task_struct
*tsk
, unsigned long *sp
,
170 struct pt_regs
*regs
, const char *log_lvl
)
172 unsigned long lr
, fp
;
173 struct thread_info
*tinfo
;
177 else if (tsk
== current
)
178 asm("mov %0, r7" : "=r"(fp
));
180 fp
= tsk
->thread
.cpu_context
.r7
;
183 * Walk the stack as long as the frame pointer (a) is within
184 * the kernel stack of the task, and (b) it doesn't move
187 tinfo
= task_thread_info(tsk
);
188 printk("%sCall trace:\n", log_lvl
);
189 while (valid_stack_ptr(tinfo
, fp
)) {
190 unsigned long new_fp
;
192 lr
= *(unsigned long *)fp
;
193 #ifdef CONFIG_KALLSYMS
194 printk("%s [<%08lx>] ", log_lvl
, lr
);
196 printk(" [<%08lx>] ", lr
);
198 print_symbol("%s\n", lr
);
200 new_fp
= *(unsigned long *)(fp
+ 4);
208 static void show_trace_log_lvl(struct task_struct
*tsk
, unsigned long *sp
,
209 struct pt_regs
*regs
, const char *log_lvl
)
213 printk("%sCall trace:\n", log_lvl
);
215 while (!kstack_end(sp
)) {
217 if (kernel_text_address(addr
)) {
218 #ifdef CONFIG_KALLSYMS
219 printk("%s [<%08lx>] ", log_lvl
, addr
);
221 printk(" [<%08lx>] ", addr
);
223 print_symbol("%s\n", addr
);
230 void show_stack_log_lvl(struct task_struct
*tsk
, unsigned long sp
,
231 struct pt_regs
*regs
, const char *log_lvl
)
233 struct thread_info
*tinfo
;
237 sp
= tsk
->thread
.cpu_context
.ksp
;
239 sp
= (unsigned long)&tinfo
;
244 tinfo
= task_thread_info(tsk
);
246 if (valid_stack_ptr(tinfo
, sp
)) {
247 dump_mem("Stack: ", log_lvl
, sp
,
248 THREAD_SIZE
+ (unsigned long)tinfo
);
249 show_trace_log_lvl(tsk
, (unsigned long *)sp
, regs
, log_lvl
);
253 void show_stack(struct task_struct
*tsk
, unsigned long *stack
)
255 show_stack_log_lvl(tsk
, (unsigned long)stack
, NULL
, "");
258 void dump_stack(void)
262 show_trace_log_lvl(current
, &stack
, NULL
, "");
264 EXPORT_SYMBOL(dump_stack
);
266 static const char *cpu_modes
[] = {
267 "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
268 "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
271 void show_regs_log_lvl(struct pt_regs
*regs
, const char *log_lvl
)
273 unsigned long sp
= regs
->sp
;
274 unsigned long lr
= regs
->lr
;
275 unsigned long mode
= (regs
->sr
& MODE_MASK
) >> MODE_SHIFT
;
277 if (!user_mode(regs
)) {
278 sp
= (unsigned long)regs
+ FRAME_SIZE_FULL
;
280 printk("%s", log_lvl
);
281 print_symbol("PC is at %s\n", instruction_pointer(regs
));
282 printk("%s", log_lvl
);
283 print_symbol("LR is at %s\n", lr
);
286 printk("%spc : [<%08lx>] lr : [<%08lx>] %s\n"
287 "%ssp : %08lx r12: %08lx r11: %08lx\n",
288 log_lvl
, instruction_pointer(regs
), lr
, print_tainted(),
289 log_lvl
, sp
, regs
->r12
, regs
->r11
);
290 printk("%sr10: %08lx r9 : %08lx r8 : %08lx\n",
291 log_lvl
, regs
->r10
, regs
->r9
, regs
->r8
);
292 printk("%sr7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
293 log_lvl
, regs
->r7
, regs
->r6
, regs
->r5
, regs
->r4
);
294 printk("%sr3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
295 log_lvl
, regs
->r3
, regs
->r2
, regs
->r1
, regs
->r0
);
296 printk("%sFlags: %c%c%c%c%c\n", log_lvl
,
297 regs
->sr
& SR_Q
? 'Q' : 'q',
298 regs
->sr
& SR_V
? 'V' : 'v',
299 regs
->sr
& SR_N
? 'N' : 'n',
300 regs
->sr
& SR_Z
? 'Z' : 'z',
301 regs
->sr
& SR_C
? 'C' : 'c');
302 printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl
,
303 regs
->sr
& SR_H
? 'H' : 'h',
304 regs
->sr
& SR_J
? 'J' : 'j',
305 regs
->sr
& SR_DM
? 'M' : 'm',
306 regs
->sr
& SR_D
? 'D' : 'd',
307 regs
->sr
& SR_EM
? 'E' : 'e',
308 regs
->sr
& SR_I3M
? '3' : '.',
309 regs
->sr
& SR_I2M
? '2' : '.',
310 regs
->sr
& SR_I1M
? '1' : '.',
311 regs
->sr
& SR_I0M
? '0' : '.',
312 regs
->sr
& SR_GM
? 'G' : 'g');
313 printk("%sCPU Mode: %s\n", log_lvl
, cpu_modes
[mode
]);
314 printk("%sProcess: %s [%d] (task: %p thread: %p)\n",
315 log_lvl
, current
->comm
, current
->pid
, current
,
316 task_thread_info(current
));
319 void show_regs(struct pt_regs
*regs
)
321 unsigned long sp
= regs
->sp
;
323 if (!user_mode(regs
))
324 sp
= (unsigned long)regs
+ FRAME_SIZE_FULL
;
326 show_regs_log_lvl(regs
, "");
327 show_trace_log_lvl(current
, (unsigned long *)sp
, regs
, "");
329 EXPORT_SYMBOL(show_regs
);
331 /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
332 int dump_fpu(struct pt_regs
*regs
, elf_fpregset_t
*fpu
)
338 asmlinkage
void ret_from_fork(void);
340 int copy_thread(int nr
, unsigned long clone_flags
, unsigned long usp
,
341 unsigned long unused
,
342 struct task_struct
*p
, struct pt_regs
*regs
)
344 struct pt_regs
*childregs
;
346 childregs
= ((struct pt_regs
*)(THREAD_SIZE
+ (unsigned long)task_stack_page(p
))) - 1;
352 childregs
->sp
= (unsigned long)task_stack_page(p
) + THREAD_SIZE
;
354 childregs
->r12
= 0; /* Set return value for child */
356 p
->thread
.cpu_context
.sr
= MODE_SUPERVISOR
| SR_GM
;
357 p
->thread
.cpu_context
.ksp
= (unsigned long)childregs
;
358 p
->thread
.cpu_context
.pc
= (unsigned long)ret_from_fork
;
360 <<<<<<< HEAD
:arch
/avr32
/kernel
/process
.c
362 clear_tsk_thread_flag(p
, TIF_DEBUG
);
363 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/avr32
/kernel
/process
.c
364 if ((clone_flags
& CLONE_PTRACE
) && test_thread_flag(TIF_DEBUG
))
370 /* r12-r8 are dummy parameters to force the compiler to use the stack */
371 asmlinkage
int sys_fork(struct pt_regs
*regs
)
373 return do_fork(SIGCHLD
, regs
->sp
, regs
, 0, NULL
, NULL
);
376 asmlinkage
int sys_clone(unsigned long clone_flags
, unsigned long newsp
,
377 unsigned long parent_tidptr
,
378 unsigned long child_tidptr
, struct pt_regs
*regs
)
382 return do_fork(clone_flags
, newsp
, regs
, 0,
383 (int __user
*)parent_tidptr
,
384 (int __user
*)child_tidptr
);
387 asmlinkage
int sys_vfork(struct pt_regs
*regs
)
389 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
->sp
, regs
,
393 asmlinkage
int sys_execve(char __user
*ufilename
, char __user
*__user
*uargv
,
394 char __user
*__user
*uenvp
, struct pt_regs
*regs
)
399 filename
= getname(ufilename
);
400 error
= PTR_ERR(filename
);
401 if (IS_ERR(filename
))
404 error
= do_execve(filename
, uargv
, uenvp
, regs
);
406 current
->ptrace
&= ~PT_DTRACE
;
415 * This function is supposed to answer the question "who called
418 unsigned long get_wchan(struct task_struct
*p
)
421 unsigned long stack_page
;
423 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
426 stack_page
= (unsigned long)task_stack_page(p
);
430 * The stored value of PC is either the address right after
431 * the call to __switch_to() or ret_from_fork.
433 pc
= thread_saved_pc(p
);
434 if (in_sched_functions(pc
)) {
435 #ifdef CONFIG_FRAME_POINTER
436 unsigned long fp
= p
->thread
.cpu_context
.r7
;
437 BUG_ON(fp
< stack_page
|| fp
> (THREAD_SIZE
+ stack_page
));
438 pc
= *(unsigned long *)fp
;
441 * We depend on the frame size of schedule here, which
442 * is actually quite ugly. It might be possible to
443 * determine the frame size automatically at build
444 * time by doing this:
446 * - disassemble the resulting sched.o
447 * - look for 'sub sp,??' shortly after '<schedule>:'
449 unsigned long sp
= p
->thread
.cpu_context
.ksp
+ 16;
450 BUG_ON(sp
< stack_page
|| sp
> (THREAD_SIZE
+ stack_page
));
451 pc
= *(unsigned long *)sp
;