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 #include <linux/unistd.h>
16 #include <asm/sysreg.h>
19 void (*pm_power_off)(void) = NULL;
20 EXPORT_SYMBOL(pm_power_off);
22 extern void cpu_idle_sleep(void);
25 * This file handles the architecture-dependent parts of process handling..
30 /* endless idle loop with no priority at all */
32 while (!need_resched())
34 preempt_enable_no_resched();
40 void machine_halt(void)
43 * Enter Stop mode. The 32 kHz oscillator will keep running so
44 * the RTC will keep the time properly and the system will
47 asm volatile("sleep 3\n\t"
51 void machine_power_off(void)
55 void machine_restart(char *cmd)
57 __mtdr(DBGREG_DC, DC_DBE);
58 __mtdr(DBGREG_DC, DC_RES);
63 * PC is actually discarded when returning from a system call -- the
64 * return address must be stored in LR. This function will make sure
65 * LR points to do_exit before starting the thread.
67 * Also, when returning from fork(), r12 is 0, so we must copy the
70 * r0 : The argument to the main thread function
71 * r1 : The address of do_exit
72 * r2 : The address of the main thread function
74 asmlinkage extern void kernel_thread_helper(void);
75 __asm__(" .type kernel_thread_helper, @function\n"
76 "kernel_thread_helper:\n"
80 " .size kernel_thread_helper, . - kernel_thread_helper");
82 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
86 memset(®s, 0, sizeof(regs));
88 regs.r0 = (unsigned long)arg;
89 regs.r1 = (unsigned long)fn;
90 regs.r2 = (unsigned long)do_exit;
91 regs.lr = (unsigned long)kernel_thread_helper;
92 regs.pc = (unsigned long)kernel_thread_helper;
93 regs.sr = MODE_SUPERVISOR;
95 return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
96 0, ®s, 0, NULL, NULL);
98 EXPORT_SYMBOL(kernel_thread);
101 * Free current thread data structures etc
103 void exit_thread(void)
108 void flush_thread(void)
113 void release_thread(struct task_struct *dead_task)
118 static const char *cpu_modes[] = {
119 "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
120 "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
123 void show_regs(struct pt_regs *regs)
125 unsigned long sp = regs->sp;
126 unsigned long lr = regs->lr;
127 unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
129 if (!user_mode(regs))
130 sp = (unsigned long)regs + FRAME_SIZE_FULL;
132 print_symbol("PC is at %s\n", instruction_pointer(regs));
133 print_symbol("LR is at %s\n", lr);
134 printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
135 "sp : %08lx r12: %08lx r11: %08lx\n",
136 instruction_pointer(regs),
137 lr, print_tainted(), sp, regs->r12, regs->r11);
138 printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
139 regs->r10, regs->r9, regs->r8);
140 printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
141 regs->r7, regs->r6, regs->r5, regs->r4);
142 printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
143 regs->r3, regs->r2, regs->r1, regs->r0);
144 printk("Flags: %c%c%c%c%c\n",
145 regs->sr & SR_Q ? 'Q' : 'q',
146 regs->sr & SR_V ? 'V' : 'v',
147 regs->sr & SR_N ? 'N' : 'n',
148 regs->sr & SR_Z ? 'Z' : 'z',
149 regs->sr & SR_C ? 'C' : 'c');
150 printk("Mode bits: %c%c%c%c%c%c%c%c%c\n",
151 regs->sr & SR_H ? 'H' : 'h',
152 regs->sr & SR_R ? 'R' : 'r',
153 regs->sr & SR_J ? 'J' : 'j',
154 regs->sr & SR_EM ? 'E' : 'e',
155 regs->sr & SR_I3M ? '3' : '.',
156 regs->sr & SR_I2M ? '2' : '.',
157 regs->sr & SR_I1M ? '1' : '.',
158 regs->sr & SR_I0M ? '0' : '.',
159 regs->sr & SR_GM ? 'G' : 'g');
160 printk("CPU Mode: %s\n", cpu_modes[mode]);
162 show_trace(NULL, (unsigned long *)sp, regs);
164 EXPORT_SYMBOL(show_regs);
166 /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
167 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
173 asmlinkage void ret_from_fork(void);
175 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
176 unsigned long unused,
177 struct task_struct *p, struct pt_regs *regs)
179 struct pt_regs *childregs;
181 childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)p->thread_info)) - 1;
187 childregs->sp = (unsigned long)p->thread_info + THREAD_SIZE;
189 childregs->r12 = 0; /* Set return value for child */
191 p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
192 p->thread.cpu_context.ksp = (unsigned long)childregs;
193 p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
198 /* r12-r8 are dummy parameters to force the compiler to use the stack */
199 asmlinkage int sys_fork(struct pt_regs *regs)
201 return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
204 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
205 unsigned long parent_tidptr,
206 unsigned long child_tidptr, struct pt_regs *regs)
210 return do_fork(clone_flags, newsp, regs, 0,
211 (int __user *)parent_tidptr,
212 (int __user *)child_tidptr);
215 asmlinkage int sys_vfork(struct pt_regs *regs)
217 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs,
221 asmlinkage int sys_execve(char __user *ufilename, char __user *__user *uargv,
222 char __user *__user *uenvp, struct pt_regs *regs)
227 filename = getname(ufilename);
228 error = PTR_ERR(filename);
229 if (IS_ERR(filename))
232 error = do_execve(filename, uargv, uenvp, regs);
234 current->ptrace &= ~PT_DTRACE;
243 * This function is supposed to answer the question "who called
246 unsigned long get_wchan(struct task_struct *p)
249 unsigned long stack_page;
251 if (!p || p == current || p->state == TASK_RUNNING)
254 stack_page = (unsigned long)p->thread_info;
258 * The stored value of PC is either the address right after
259 * the call to __switch_to() or ret_from_fork.
261 pc = thread_saved_pc(p);
262 if (in_sched_functions(pc)) {
263 #ifdef CONFIG_FRAME_POINTER
264 unsigned long fp = p->thread.cpu_context.r7;
265 BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
266 pc = *(unsigned long *)fp;
269 * We depend on the frame size of schedule here, which
270 * is actually quite ugly. It might be possible to
271 * determine the frame size automatically at build
272 * time by doing this:
274 * - disassemble the resulting sched.o
275 * - look for 'sub sp,??' shortly after '<schedule>:'
277 unsigned long sp = p->thread.cpu_context.ksp + 16;
278 BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
279 pc = *(unsigned long *)sp;