Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 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> |
| 14 | #include <linux/unistd.h> |
| 15 | |
| 16 | #include <asm/sysreg.h> |
| 17 | #include <asm/ocd.h> |
| 18 | |
| 19 | void (*pm_power_off)(void) = NULL; |
| 20 | EXPORT_SYMBOL(pm_power_off); |
| 21 | |
Hans-Christian Egtvedt | 19b7ce8 | 2007-02-26 13:50:43 +0100 | [diff] [blame^] | 22 | extern void cpu_idle_sleep(void); |
| 23 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 24 | /* |
| 25 | * This file handles the architecture-dependent parts of process handling.. |
| 26 | */ |
| 27 | |
| 28 | void cpu_idle(void) |
| 29 | { |
| 30 | /* endless idle loop with no priority at all */ |
| 31 | while (1) { |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 32 | while (!need_resched()) |
Hans-Christian Egtvedt | 19b7ce8 | 2007-02-26 13:50:43 +0100 | [diff] [blame^] | 33 | cpu_idle_sleep(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 34 | preempt_enable_no_resched(); |
| 35 | schedule(); |
| 36 | preempt_disable(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void machine_halt(void) |
| 41 | { |
Haavard Skinnemoen | c2eb509 | 2006-12-04 12:01:36 +0100 | [diff] [blame] | 42 | /* |
| 43 | * Enter Stop mode. The 32 kHz oscillator will keep running so |
| 44 | * the RTC will keep the time properly and the system will |
| 45 | * boot quickly. |
| 46 | */ |
| 47 | asm volatile("sleep 3\n\t" |
| 48 | "sub pc, -2"); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void machine_power_off(void) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void machine_restart(char *cmd) |
| 56 | { |
| 57 | __mtdr(DBGREG_DC, DC_DBE); |
| 58 | __mtdr(DBGREG_DC, DC_RES); |
| 59 | while (1) ; |
| 60 | } |
| 61 | |
| 62 | /* |
| 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. |
| 66 | * |
| 67 | * Also, when returning from fork(), r12 is 0, so we must copy the |
| 68 | * argument as well. |
| 69 | * |
| 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 |
| 73 | */ |
| 74 | asmlinkage extern void kernel_thread_helper(void); |
| 75 | __asm__(" .type kernel_thread_helper, @function\n" |
| 76 | "kernel_thread_helper:\n" |
| 77 | " mov r12, r0\n" |
| 78 | " mov lr, r2\n" |
| 79 | " mov pc, r1\n" |
| 80 | " .size kernel_thread_helper, . - kernel_thread_helper"); |
| 81 | |
| 82 | int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
| 83 | { |
| 84 | struct pt_regs regs; |
| 85 | |
| 86 | memset(®s, 0, sizeof(regs)); |
| 87 | |
| 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; |
| 94 | |
| 95 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, |
| 96 | 0, ®s, 0, NULL, NULL); |
| 97 | } |
| 98 | EXPORT_SYMBOL(kernel_thread); |
| 99 | |
| 100 | /* |
| 101 | * Free current thread data structures etc |
| 102 | */ |
| 103 | void exit_thread(void) |
| 104 | { |
| 105 | /* nothing to do */ |
| 106 | } |
| 107 | |
| 108 | void flush_thread(void) |
| 109 | { |
| 110 | /* nothing to do */ |
| 111 | } |
| 112 | |
| 113 | void release_thread(struct task_struct *dead_task) |
| 114 | { |
| 115 | /* do nothing */ |
| 116 | } |
| 117 | |
| 118 | static const char *cpu_modes[] = { |
| 119 | "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1", |
| 120 | "Interrupt level 2", "Interrupt level 3", "Exception", "NMI" |
| 121 | }; |
| 122 | |
| 123 | void show_regs(struct pt_regs *regs) |
| 124 | { |
| 125 | unsigned long sp = regs->sp; |
| 126 | unsigned long lr = regs->lr; |
| 127 | unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT; |
| 128 | |
| 129 | if (!user_mode(regs)) |
| 130 | sp = (unsigned long)regs + FRAME_SIZE_FULL; |
| 131 | |
| 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]); |
| 161 | |
| 162 | show_trace(NULL, (unsigned long *)sp, regs); |
| 163 | } |
| 164 | EXPORT_SYMBOL(show_regs); |
| 165 | |
| 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) |
| 168 | { |
| 169 | /* Not valid */ |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | asmlinkage void ret_from_fork(void); |
| 174 | |
| 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) |
| 178 | { |
| 179 | struct pt_regs *childregs; |
| 180 | |
| 181 | childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)p->thread_info)) - 1; |
| 182 | *childregs = *regs; |
| 183 | |
| 184 | if (user_mode(regs)) |
| 185 | childregs->sp = usp; |
| 186 | else |
| 187 | childregs->sp = (unsigned long)p->thread_info + THREAD_SIZE; |
| 188 | |
| 189 | childregs->r12 = 0; /* Set return value for child */ |
| 190 | |
| 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; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* r12-r8 are dummy parameters to force the compiler to use the stack */ |
| 199 | asmlinkage int sys_fork(struct pt_regs *regs) |
| 200 | { |
| 201 | return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL); |
| 202 | } |
| 203 | |
| 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) |
| 207 | { |
| 208 | if (!newsp) |
| 209 | newsp = regs->sp; |
| 210 | return do_fork(clone_flags, newsp, regs, 0, |
| 211 | (int __user *)parent_tidptr, |
| 212 | (int __user *)child_tidptr); |
| 213 | } |
| 214 | |
| 215 | asmlinkage int sys_vfork(struct pt_regs *regs) |
| 216 | { |
| 217 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, |
| 218 | 0, NULL, NULL); |
| 219 | } |
| 220 | |
| 221 | asmlinkage int sys_execve(char __user *ufilename, char __user *__user *uargv, |
| 222 | char __user *__user *uenvp, struct pt_regs *regs) |
| 223 | { |
| 224 | int error; |
| 225 | char *filename; |
| 226 | |
| 227 | filename = getname(ufilename); |
| 228 | error = PTR_ERR(filename); |
| 229 | if (IS_ERR(filename)) |
| 230 | goto out; |
| 231 | |
| 232 | error = do_execve(filename, uargv, uenvp, regs); |
| 233 | if (error == 0) |
| 234 | current->ptrace &= ~PT_DTRACE; |
| 235 | putname(filename); |
| 236 | |
| 237 | out: |
| 238 | return error; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* |
| 243 | * This function is supposed to answer the question "who called |
| 244 | * schedule()?" |
| 245 | */ |
| 246 | unsigned long get_wchan(struct task_struct *p) |
| 247 | { |
| 248 | unsigned long pc; |
| 249 | unsigned long stack_page; |
| 250 | |
| 251 | if (!p || p == current || p->state == TASK_RUNNING) |
| 252 | return 0; |
| 253 | |
| 254 | stack_page = (unsigned long)p->thread_info; |
| 255 | BUG_ON(!stack_page); |
| 256 | |
| 257 | /* |
| 258 | * The stored value of PC is either the address right after |
| 259 | * the call to __switch_to() or ret_from_fork. |
| 260 | */ |
| 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; |
| 267 | #else |
| 268 | /* |
| 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: |
| 273 | * - compile sched.c |
| 274 | * - disassemble the resulting sched.o |
| 275 | * - look for 'sub sp,??' shortly after '<schedule>:' |
| 276 | */ |
| 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; |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | return pc; |
| 284 | } |