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> |
Haavard Skinnemoen | b3bc2c5 | 2008-07-28 13:05:19 +0200 | [diff] [blame] | 12 | #include <linux/pm.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 13 | #include <linux/ptrace.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 15 | #include <linux/reboot.h> |
Haavard Skinnemoen | d45ad06 | 2008-02-15 14:38:40 +0100 | [diff] [blame] | 16 | #include <linux/tick.h> |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 18 | #include <linux/unistd.h> |
| 19 | |
| 20 | #include <asm/sysreg.h> |
| 21 | #include <asm/ocd.h> |
Jaswinder Singh | c80ce2d | 2008-07-25 13:48:38 +0530 | [diff] [blame] | 22 | #include <asm/syscalls.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 23 | |
Haavard Skinnemoen | 3663b73 | 2008-08-05 13:57:38 +0200 | [diff] [blame] | 24 | #include <mach/pm.h> |
Haavard Skinnemoen | 7e59128 | 2008-02-24 23:24:26 +0100 | [diff] [blame] | 25 | |
Haavard Skinnemoen | b3bc2c5 | 2008-07-28 13:05:19 +0200 | [diff] [blame] | 26 | void (*pm_power_off)(void); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 27 | EXPORT_SYMBOL(pm_power_off); |
| 28 | |
| 29 | /* |
| 30 | * This file handles the architecture-dependent parts of process handling.. |
| 31 | */ |
| 32 | |
Thomas Gleixner | 0142647 | 2013-03-21 22:49:40 +0100 | [diff] [blame] | 33 | void arch_cpu_idle(void) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 34 | { |
Thomas Gleixner | 0142647 | 2013-03-21 22:49:40 +0100 | [diff] [blame] | 35 | cpu_enter_idle(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void machine_halt(void) |
| 39 | { |
Haavard Skinnemoen | c2eb509 | 2006-12-04 12:01:36 +0100 | [diff] [blame] | 40 | /* |
| 41 | * Enter Stop mode. The 32 kHz oscillator will keep running so |
| 42 | * the RTC will keep the time properly and the system will |
| 43 | * boot quickly. |
| 44 | */ |
| 45 | asm volatile("sleep 3\n\t" |
| 46 | "sub pc, -2"); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void machine_power_off(void) |
| 50 | { |
Peter Ma | ed3fa7c | 2008-04-19 00:24:25 -0700 | [diff] [blame] | 51 | if (pm_power_off) |
| 52 | pm_power_off(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void machine_restart(char *cmd) |
| 56 | { |
Haavard Skinnemoen | 8dfe8f2 | 2007-11-27 13:31:20 +0100 | [diff] [blame] | 57 | ocd_write(DC, (1 << OCD_DC_DBE_BIT)); |
| 58 | ocd_write(DC, (1 << OCD_DC_RES_BIT)); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 59 | while (1) ; |
| 60 | } |
| 61 | |
| 62 | /* |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 63 | * Free current thread data structures etc |
| 64 | */ |
| 65 | void exit_thread(void) |
| 66 | { |
Haavard Skinnemoen | 13b54a5 | 2007-11-27 13:50:45 +0100 | [diff] [blame] | 67 | ocd_disable(current); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void flush_thread(void) |
| 71 | { |
| 72 | /* nothing to do */ |
| 73 | } |
| 74 | |
| 75 | void release_thread(struct task_struct *dead_task) |
| 76 | { |
| 77 | /* do nothing */ |
| 78 | } |
| 79 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 80 | static void dump_mem(const char *str, const char *log_lvl, |
| 81 | unsigned long bottom, unsigned long top) |
| 82 | { |
| 83 | unsigned long p; |
| 84 | int i; |
| 85 | |
| 86 | printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top); |
| 87 | |
| 88 | for (p = bottom & ~31; p < top; ) { |
| 89 | printk("%s%04lx: ", log_lvl, p & 0xffff); |
| 90 | |
| 91 | for (i = 0; i < 8; i++, p += 4) { |
| 92 | unsigned int val; |
| 93 | |
| 94 | if (p < bottom || p >= top) |
| 95 | printk(" "); |
| 96 | else { |
| 97 | if (__get_user(val, (unsigned int __user *)p)) { |
| 98 | printk("\n"); |
| 99 | goto out; |
| 100 | } |
| 101 | printk("%08x ", val); |
| 102 | } |
| 103 | } |
| 104 | printk("\n"); |
| 105 | } |
| 106 | |
| 107 | out: |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p) |
| 112 | { |
| 113 | return (p > (unsigned long)tinfo) |
| 114 | && (p < (unsigned long)tinfo + THREAD_SIZE - 3); |
| 115 | } |
| 116 | |
| 117 | #ifdef CONFIG_FRAME_POINTER |
| 118 | static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp, |
| 119 | struct pt_regs *regs, const char *log_lvl) |
| 120 | { |
| 121 | unsigned long lr, fp; |
| 122 | struct thread_info *tinfo; |
| 123 | |
| 124 | if (regs) |
| 125 | fp = regs->r7; |
| 126 | else if (tsk == current) |
| 127 | asm("mov %0, r7" : "=r"(fp)); |
| 128 | else |
| 129 | fp = tsk->thread.cpu_context.r7; |
| 130 | |
| 131 | /* |
| 132 | * Walk the stack as long as the frame pointer (a) is within |
| 133 | * the kernel stack of the task, and (b) it doesn't move |
| 134 | * downwards. |
| 135 | */ |
| 136 | tinfo = task_thread_info(tsk); |
| 137 | printk("%sCall trace:\n", log_lvl); |
| 138 | while (valid_stack_ptr(tinfo, fp)) { |
| 139 | unsigned long new_fp; |
| 140 | |
| 141 | lr = *(unsigned long *)fp; |
| 142 | #ifdef CONFIG_KALLSYMS |
| 143 | printk("%s [<%08lx>] ", log_lvl, lr); |
| 144 | #else |
| 145 | printk(" [<%08lx>] ", lr); |
| 146 | #endif |
| 147 | print_symbol("%s\n", lr); |
| 148 | |
| 149 | new_fp = *(unsigned long *)(fp + 4); |
| 150 | if (new_fp <= fp) |
| 151 | break; |
| 152 | fp = new_fp; |
| 153 | } |
| 154 | printk("\n"); |
| 155 | } |
| 156 | #else |
| 157 | static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp, |
| 158 | struct pt_regs *regs, const char *log_lvl) |
| 159 | { |
| 160 | unsigned long addr; |
| 161 | |
| 162 | printk("%sCall trace:\n", log_lvl); |
| 163 | |
| 164 | while (!kstack_end(sp)) { |
| 165 | addr = *sp++; |
| 166 | if (kernel_text_address(addr)) { |
| 167 | #ifdef CONFIG_KALLSYMS |
| 168 | printk("%s [<%08lx>] ", log_lvl, addr); |
| 169 | #else |
| 170 | printk(" [<%08lx>] ", addr); |
| 171 | #endif |
| 172 | print_symbol("%s\n", addr); |
| 173 | } |
| 174 | } |
| 175 | printk("\n"); |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp, |
| 180 | struct pt_regs *regs, const char *log_lvl) |
| 181 | { |
| 182 | struct thread_info *tinfo; |
| 183 | |
| 184 | if (sp == 0) { |
| 185 | if (tsk) |
| 186 | sp = tsk->thread.cpu_context.ksp; |
| 187 | else |
| 188 | sp = (unsigned long)&tinfo; |
| 189 | } |
| 190 | if (!tsk) |
| 191 | tsk = current; |
| 192 | |
| 193 | tinfo = task_thread_info(tsk); |
| 194 | |
| 195 | if (valid_stack_ptr(tinfo, sp)) { |
| 196 | dump_mem("Stack: ", log_lvl, sp, |
| 197 | THREAD_SIZE + (unsigned long)tinfo); |
| 198 | show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void show_stack(struct task_struct *tsk, unsigned long *stack) |
| 203 | { |
| 204 | show_stack_log_lvl(tsk, (unsigned long)stack, NULL, ""); |
| 205 | } |
| 206 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 207 | static const char *cpu_modes[] = { |
| 208 | "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1", |
| 209 | "Interrupt level 2", "Interrupt level 3", "Exception", "NMI" |
| 210 | }; |
| 211 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 212 | void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 213 | { |
| 214 | unsigned long sp = regs->sp; |
| 215 | unsigned long lr = regs->lr; |
| 216 | unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT; |
| 217 | |
Tejun Heo | a43cb95 | 2013-04-30 15:27:17 -0700 | [diff] [blame] | 218 | show_regs_print_info(log_lvl); |
| 219 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 220 | if (!user_mode(regs)) { |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 221 | sp = (unsigned long)regs + FRAME_SIZE_FULL; |
| 222 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 223 | printk("%s", log_lvl); |
| 224 | print_symbol("PC is at %s\n", instruction_pointer(regs)); |
| 225 | printk("%s", log_lvl); |
| 226 | print_symbol("LR is at %s\n", lr); |
| 227 | } |
| 228 | |
| 229 | printk("%spc : [<%08lx>] lr : [<%08lx>] %s\n" |
| 230 | "%ssp : %08lx r12: %08lx r11: %08lx\n", |
| 231 | log_lvl, instruction_pointer(regs), lr, print_tainted(), |
| 232 | log_lvl, sp, regs->r12, regs->r11); |
| 233 | printk("%sr10: %08lx r9 : %08lx r8 : %08lx\n", |
| 234 | log_lvl, regs->r10, regs->r9, regs->r8); |
| 235 | printk("%sr7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n", |
| 236 | log_lvl, regs->r7, regs->r6, regs->r5, regs->r4); |
| 237 | printk("%sr3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n", |
| 238 | log_lvl, regs->r3, regs->r2, regs->r1, regs->r0); |
| 239 | printk("%sFlags: %c%c%c%c%c\n", log_lvl, |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 240 | regs->sr & SR_Q ? 'Q' : 'q', |
| 241 | regs->sr & SR_V ? 'V' : 'v', |
| 242 | regs->sr & SR_N ? 'N' : 'n', |
| 243 | regs->sr & SR_Z ? 'Z' : 'z', |
| 244 | regs->sr & SR_C ? 'C' : 'c'); |
Haavard Skinnemoen | df67977 | 2007-11-27 18:59:32 +0100 | [diff] [blame] | 245 | printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl, |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 246 | regs->sr & SR_H ? 'H' : 'h', |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 247 | regs->sr & SR_J ? 'J' : 'j', |
Haavard Skinnemoen | df67977 | 2007-11-27 18:59:32 +0100 | [diff] [blame] | 248 | regs->sr & SR_DM ? 'M' : 'm', |
| 249 | regs->sr & SR_D ? 'D' : 'd', |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 250 | regs->sr & SR_EM ? 'E' : 'e', |
| 251 | regs->sr & SR_I3M ? '3' : '.', |
| 252 | regs->sr & SR_I2M ? '2' : '.', |
| 253 | regs->sr & SR_I1M ? '1' : '.', |
| 254 | regs->sr & SR_I0M ? '0' : '.', |
| 255 | regs->sr & SR_GM ? 'G' : 'g'); |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 256 | printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]); |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 257 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 258 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 259 | void show_regs(struct pt_regs *regs) |
| 260 | { |
| 261 | unsigned long sp = regs->sp; |
| 262 | |
| 263 | if (!user_mode(regs)) |
| 264 | sp = (unsigned long)regs + FRAME_SIZE_FULL; |
| 265 | |
| 266 | show_regs_log_lvl(regs, ""); |
| 267 | show_trace_log_lvl(current, (unsigned long *)sp, regs, ""); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 268 | } |
| 269 | EXPORT_SYMBOL(show_regs); |
| 270 | |
| 271 | /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */ |
| 272 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu) |
| 273 | { |
| 274 | /* Not valid */ |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | asmlinkage void ret_from_fork(void); |
Al Viro | 5adc807 | 2012-10-15 02:23:22 -0400 | [diff] [blame] | 279 | asmlinkage void ret_from_kernel_thread(void); |
| 280 | asmlinkage void syscall_return(void); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 281 | |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 282 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
Al Viro | 5adc807 | 2012-10-15 02:23:22 -0400 | [diff] [blame] | 283 | unsigned long arg, |
Al Viro | afa86fc | 2012-10-22 22:51:14 -0400 | [diff] [blame] | 284 | struct task_struct *p) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 285 | { |
Al Viro | 5adc807 | 2012-10-15 02:23:22 -0400 | [diff] [blame] | 286 | struct pt_regs *childregs = task_pt_regs(p); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 287 | |
Al Viro | 584271b | 2012-10-21 15:57:32 -0400 | [diff] [blame] | 288 | if (unlikely(p->flags & PF_KTHREAD)) { |
Al Viro | 5adc807 | 2012-10-15 02:23:22 -0400 | [diff] [blame] | 289 | memset(childregs, 0, sizeof(struct pt_regs)); |
| 290 | p->thread.cpu_context.r0 = arg; |
| 291 | p->thread.cpu_context.r1 = usp; /* fn */ |
| 292 | p->thread.cpu_context.r2 = syscall_return; |
| 293 | p->thread.cpu_context.pc = (unsigned long)ret_from_kernel_thread; |
| 294 | childregs->sr = MODE_SUPERVISOR; |
| 295 | } else { |
Al Viro | 584271b | 2012-10-21 15:57:32 -0400 | [diff] [blame] | 296 | *childregs = *current_pt_regs(); |
| 297 | if (usp) |
| 298 | childregs->sp = usp; |
Al Viro | 5adc807 | 2012-10-15 02:23:22 -0400 | [diff] [blame] | 299 | childregs->r12 = 0; /* Set return value for child */ |
| 300 | p->thread.cpu_context.pc = (unsigned long)ret_from_fork; |
| 301 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 302 | |
| 303 | p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM; |
| 304 | p->thread.cpu_context.ksp = (unsigned long)childregs; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 305 | |
Haavard Skinnemoen | 325d6f5 | 2008-02-27 14:04:29 +0100 | [diff] [blame] | 306 | clear_tsk_thread_flag(p, TIF_DEBUG); |
Haavard Skinnemoen | 13b54a5 | 2007-11-27 13:50:45 +0100 | [diff] [blame] | 307 | if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG)) |
| 308 | ocd_enable(p); |
| 309 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 313 | /* |
| 314 | * This function is supposed to answer the question "who called |
| 315 | * schedule()?" |
| 316 | */ |
| 317 | unsigned long get_wchan(struct task_struct *p) |
| 318 | { |
| 319 | unsigned long pc; |
| 320 | unsigned long stack_page; |
| 321 | |
| 322 | if (!p || p == current || p->state == TASK_RUNNING) |
| 323 | return 0; |
| 324 | |
Roman Zippel | c9f4f06 | 2007-05-09 02:35:16 -0700 | [diff] [blame] | 325 | stack_page = (unsigned long)task_stack_page(p); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 326 | BUG_ON(!stack_page); |
| 327 | |
| 328 | /* |
| 329 | * The stored value of PC is either the address right after |
| 330 | * the call to __switch_to() or ret_from_fork. |
| 331 | */ |
| 332 | pc = thread_saved_pc(p); |
| 333 | if (in_sched_functions(pc)) { |
| 334 | #ifdef CONFIG_FRAME_POINTER |
| 335 | unsigned long fp = p->thread.cpu_context.r7; |
| 336 | BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page)); |
| 337 | pc = *(unsigned long *)fp; |
| 338 | #else |
| 339 | /* |
| 340 | * We depend on the frame size of schedule here, which |
| 341 | * is actually quite ugly. It might be possible to |
| 342 | * determine the frame size automatically at build |
| 343 | * time by doing this: |
Viresh Kumar | 0a0fca9 | 2013-06-04 13:10:24 +0530 | [diff] [blame^] | 344 | * - compile sched/core.c |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 345 | * - disassemble the resulting sched.o |
| 346 | * - look for 'sub sp,??' shortly after '<schedule>:' |
| 347 | */ |
| 348 | unsigned long sp = p->thread.cpu_context.ksp + 16; |
| 349 | BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page)); |
| 350 | pc = *(unsigned long *)sp; |
| 351 | #endif |
| 352 | } |
| 353 | |
| 354 | return pc; |
| 355 | } |