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 | |
| 33 | void cpu_idle(void) |
| 34 | { |
| 35 | /* endless idle loop with no priority at all */ |
| 36 | while (1) { |
Frederic Weisbecker | 1268fbc | 2011-11-17 18:48:14 +0100 | [diff] [blame] | 37 | tick_nohz_idle_enter(); |
| 38 | rcu_idle_enter(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 39 | while (!need_resched()) |
Hans-Christian Egtvedt | 19b7ce8 | 2007-02-26 13:50:43 +0100 | [diff] [blame] | 40 | cpu_idle_sleep(); |
Frederic Weisbecker | 1268fbc | 2011-11-17 18:48:14 +0100 | [diff] [blame] | 41 | rcu_idle_exit(); |
| 42 | tick_nohz_idle_exit(); |
Thomas Gleixner | bd2f553 | 2011-03-21 12:33:18 +0100 | [diff] [blame^] | 43 | schedule_preempt_disabled(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | void machine_halt(void) |
| 48 | { |
Haavard Skinnemoen | c2eb509 | 2006-12-04 12:01:36 +0100 | [diff] [blame] | 49 | /* |
| 50 | * Enter Stop mode. The 32 kHz oscillator will keep running so |
| 51 | * the RTC will keep the time properly and the system will |
| 52 | * boot quickly. |
| 53 | */ |
| 54 | asm volatile("sleep 3\n\t" |
| 55 | "sub pc, -2"); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void machine_power_off(void) |
| 59 | { |
Peter Ma | ed3fa7c | 2008-04-19 00:24:25 -0700 | [diff] [blame] | 60 | if (pm_power_off) |
| 61 | pm_power_off(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void machine_restart(char *cmd) |
| 65 | { |
Haavard Skinnemoen | 8dfe8f2 | 2007-11-27 13:31:20 +0100 | [diff] [blame] | 66 | ocd_write(DC, (1 << OCD_DC_DBE_BIT)); |
| 67 | ocd_write(DC, (1 << OCD_DC_RES_BIT)); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 68 | while (1) ; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * PC is actually discarded when returning from a system call -- the |
| 73 | * return address must be stored in LR. This function will make sure |
| 74 | * LR points to do_exit before starting the thread. |
| 75 | * |
| 76 | * Also, when returning from fork(), r12 is 0, so we must copy the |
| 77 | * argument as well. |
| 78 | * |
| 79 | * r0 : The argument to the main thread function |
| 80 | * r1 : The address of do_exit |
| 81 | * r2 : The address of the main thread function |
| 82 | */ |
| 83 | asmlinkage extern void kernel_thread_helper(void); |
| 84 | __asm__(" .type kernel_thread_helper, @function\n" |
| 85 | "kernel_thread_helper:\n" |
| 86 | " mov r12, r0\n" |
| 87 | " mov lr, r2\n" |
| 88 | " mov pc, r1\n" |
| 89 | " .size kernel_thread_helper, . - kernel_thread_helper"); |
| 90 | |
| 91 | int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
| 92 | { |
| 93 | struct pt_regs regs; |
| 94 | |
| 95 | memset(®s, 0, sizeof(regs)); |
| 96 | |
| 97 | regs.r0 = (unsigned long)arg; |
| 98 | regs.r1 = (unsigned long)fn; |
| 99 | regs.r2 = (unsigned long)do_exit; |
| 100 | regs.lr = (unsigned long)kernel_thread_helper; |
| 101 | regs.pc = (unsigned long)kernel_thread_helper; |
| 102 | regs.sr = MODE_SUPERVISOR; |
| 103 | |
| 104 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, |
| 105 | 0, ®s, 0, NULL, NULL); |
| 106 | } |
| 107 | EXPORT_SYMBOL(kernel_thread); |
| 108 | |
| 109 | /* |
| 110 | * Free current thread data structures etc |
| 111 | */ |
| 112 | void exit_thread(void) |
| 113 | { |
Haavard Skinnemoen | 13b54a5 | 2007-11-27 13:50:45 +0100 | [diff] [blame] | 114 | ocd_disable(current); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void flush_thread(void) |
| 118 | { |
| 119 | /* nothing to do */ |
| 120 | } |
| 121 | |
| 122 | void release_thread(struct task_struct *dead_task) |
| 123 | { |
| 124 | /* do nothing */ |
| 125 | } |
| 126 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 127 | static void dump_mem(const char *str, const char *log_lvl, |
| 128 | unsigned long bottom, unsigned long top) |
| 129 | { |
| 130 | unsigned long p; |
| 131 | int i; |
| 132 | |
| 133 | printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top); |
| 134 | |
| 135 | for (p = bottom & ~31; p < top; ) { |
| 136 | printk("%s%04lx: ", log_lvl, p & 0xffff); |
| 137 | |
| 138 | for (i = 0; i < 8; i++, p += 4) { |
| 139 | unsigned int val; |
| 140 | |
| 141 | if (p < bottom || p >= top) |
| 142 | printk(" "); |
| 143 | else { |
| 144 | if (__get_user(val, (unsigned int __user *)p)) { |
| 145 | printk("\n"); |
| 146 | goto out; |
| 147 | } |
| 148 | printk("%08x ", val); |
| 149 | } |
| 150 | } |
| 151 | printk("\n"); |
| 152 | } |
| 153 | |
| 154 | out: |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p) |
| 159 | { |
| 160 | return (p > (unsigned long)tinfo) |
| 161 | && (p < (unsigned long)tinfo + THREAD_SIZE - 3); |
| 162 | } |
| 163 | |
| 164 | #ifdef CONFIG_FRAME_POINTER |
| 165 | static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp, |
| 166 | struct pt_regs *regs, const char *log_lvl) |
| 167 | { |
| 168 | unsigned long lr, fp; |
| 169 | struct thread_info *tinfo; |
| 170 | |
| 171 | if (regs) |
| 172 | fp = regs->r7; |
| 173 | else if (tsk == current) |
| 174 | asm("mov %0, r7" : "=r"(fp)); |
| 175 | else |
| 176 | fp = tsk->thread.cpu_context.r7; |
| 177 | |
| 178 | /* |
| 179 | * Walk the stack as long as the frame pointer (a) is within |
| 180 | * the kernel stack of the task, and (b) it doesn't move |
| 181 | * downwards. |
| 182 | */ |
| 183 | tinfo = task_thread_info(tsk); |
| 184 | printk("%sCall trace:\n", log_lvl); |
| 185 | while (valid_stack_ptr(tinfo, fp)) { |
| 186 | unsigned long new_fp; |
| 187 | |
| 188 | lr = *(unsigned long *)fp; |
| 189 | #ifdef CONFIG_KALLSYMS |
| 190 | printk("%s [<%08lx>] ", log_lvl, lr); |
| 191 | #else |
| 192 | printk(" [<%08lx>] ", lr); |
| 193 | #endif |
| 194 | print_symbol("%s\n", lr); |
| 195 | |
| 196 | new_fp = *(unsigned long *)(fp + 4); |
| 197 | if (new_fp <= fp) |
| 198 | break; |
| 199 | fp = new_fp; |
| 200 | } |
| 201 | printk("\n"); |
| 202 | } |
| 203 | #else |
| 204 | static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp, |
| 205 | struct pt_regs *regs, const char *log_lvl) |
| 206 | { |
| 207 | unsigned long addr; |
| 208 | |
| 209 | printk("%sCall trace:\n", log_lvl); |
| 210 | |
| 211 | while (!kstack_end(sp)) { |
| 212 | addr = *sp++; |
| 213 | if (kernel_text_address(addr)) { |
| 214 | #ifdef CONFIG_KALLSYMS |
| 215 | printk("%s [<%08lx>] ", log_lvl, addr); |
| 216 | #else |
| 217 | printk(" [<%08lx>] ", addr); |
| 218 | #endif |
| 219 | print_symbol("%s\n", addr); |
| 220 | } |
| 221 | } |
| 222 | printk("\n"); |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp, |
| 227 | struct pt_regs *regs, const char *log_lvl) |
| 228 | { |
| 229 | struct thread_info *tinfo; |
| 230 | |
| 231 | if (sp == 0) { |
| 232 | if (tsk) |
| 233 | sp = tsk->thread.cpu_context.ksp; |
| 234 | else |
| 235 | sp = (unsigned long)&tinfo; |
| 236 | } |
| 237 | if (!tsk) |
| 238 | tsk = current; |
| 239 | |
| 240 | tinfo = task_thread_info(tsk); |
| 241 | |
| 242 | if (valid_stack_ptr(tinfo, sp)) { |
| 243 | dump_mem("Stack: ", log_lvl, sp, |
| 244 | THREAD_SIZE + (unsigned long)tinfo); |
| 245 | show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void show_stack(struct task_struct *tsk, unsigned long *stack) |
| 250 | { |
| 251 | show_stack_log_lvl(tsk, (unsigned long)stack, NULL, ""); |
| 252 | } |
| 253 | |
| 254 | void dump_stack(void) |
| 255 | { |
| 256 | unsigned long stack; |
| 257 | |
| 258 | show_trace_log_lvl(current, &stack, NULL, ""); |
| 259 | } |
| 260 | EXPORT_SYMBOL(dump_stack); |
| 261 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 262 | static const char *cpu_modes[] = { |
| 263 | "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1", |
| 264 | "Interrupt level 2", "Interrupt level 3", "Exception", "NMI" |
| 265 | }; |
| 266 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 267 | 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] | 268 | { |
| 269 | unsigned long sp = regs->sp; |
| 270 | unsigned long lr = regs->lr; |
| 271 | unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT; |
| 272 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 273 | if (!user_mode(regs)) { |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 274 | sp = (unsigned long)regs + FRAME_SIZE_FULL; |
| 275 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 276 | printk("%s", log_lvl); |
| 277 | print_symbol("PC is at %s\n", instruction_pointer(regs)); |
| 278 | printk("%s", log_lvl); |
| 279 | print_symbol("LR is at %s\n", lr); |
| 280 | } |
| 281 | |
| 282 | printk("%spc : [<%08lx>] lr : [<%08lx>] %s\n" |
| 283 | "%ssp : %08lx r12: %08lx r11: %08lx\n", |
| 284 | log_lvl, instruction_pointer(regs), lr, print_tainted(), |
| 285 | log_lvl, sp, regs->r12, regs->r11); |
| 286 | printk("%sr10: %08lx r9 : %08lx r8 : %08lx\n", |
| 287 | log_lvl, regs->r10, regs->r9, regs->r8); |
| 288 | printk("%sr7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n", |
| 289 | log_lvl, regs->r7, regs->r6, regs->r5, regs->r4); |
| 290 | printk("%sr3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n", |
| 291 | log_lvl, regs->r3, regs->r2, regs->r1, regs->r0); |
| 292 | printk("%sFlags: %c%c%c%c%c\n", log_lvl, |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 293 | regs->sr & SR_Q ? 'Q' : 'q', |
| 294 | regs->sr & SR_V ? 'V' : 'v', |
| 295 | regs->sr & SR_N ? 'N' : 'n', |
| 296 | regs->sr & SR_Z ? 'Z' : 'z', |
| 297 | regs->sr & SR_C ? 'C' : 'c'); |
Haavard Skinnemoen | df67977 | 2007-11-27 18:59:32 +0100 | [diff] [blame] | 298 | 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] | 299 | regs->sr & SR_H ? 'H' : 'h', |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 300 | regs->sr & SR_J ? 'J' : 'j', |
Haavard Skinnemoen | df67977 | 2007-11-27 18:59:32 +0100 | [diff] [blame] | 301 | regs->sr & SR_DM ? 'M' : 'm', |
| 302 | regs->sr & SR_D ? 'D' : 'd', |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 303 | regs->sr & SR_EM ? 'E' : 'e', |
| 304 | regs->sr & SR_I3M ? '3' : '.', |
| 305 | regs->sr & SR_I2M ? '2' : '.', |
| 306 | regs->sr & SR_I1M ? '1' : '.', |
| 307 | regs->sr & SR_I0M ? '0' : '.', |
| 308 | regs->sr & SR_GM ? 'G' : 'g'); |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 309 | printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]); |
| 310 | printk("%sProcess: %s [%d] (task: %p thread: %p)\n", |
| 311 | log_lvl, current->comm, current->pid, current, |
| 312 | task_thread_info(current)); |
| 313 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 314 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 315 | void show_regs(struct pt_regs *regs) |
| 316 | { |
| 317 | unsigned long sp = regs->sp; |
| 318 | |
| 319 | if (!user_mode(regs)) |
| 320 | sp = (unsigned long)regs + FRAME_SIZE_FULL; |
| 321 | |
| 322 | show_regs_log_lvl(regs, ""); |
| 323 | show_trace_log_lvl(current, (unsigned long *)sp, regs, ""); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 324 | } |
| 325 | EXPORT_SYMBOL(show_regs); |
| 326 | |
| 327 | /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */ |
| 328 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu) |
| 329 | { |
| 330 | /* Not valid */ |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | asmlinkage void ret_from_fork(void); |
| 335 | |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 336 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 337 | unsigned long unused, |
| 338 | struct task_struct *p, struct pt_regs *regs) |
| 339 | { |
| 340 | struct pt_regs *childregs; |
| 341 | |
Roman Zippel | c9f4f06 | 2007-05-09 02:35:16 -0700 | [diff] [blame] | 342 | childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)task_stack_page(p))) - 1; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 343 | *childregs = *regs; |
| 344 | |
| 345 | if (user_mode(regs)) |
| 346 | childregs->sp = usp; |
| 347 | else |
Roman Zippel | c9f4f06 | 2007-05-09 02:35:16 -0700 | [diff] [blame] | 348 | childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 349 | |
| 350 | childregs->r12 = 0; /* Set return value for child */ |
| 351 | |
| 352 | p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM; |
| 353 | p->thread.cpu_context.ksp = (unsigned long)childregs; |
| 354 | p->thread.cpu_context.pc = (unsigned long)ret_from_fork; |
| 355 | |
Haavard Skinnemoen | 325d6f5 | 2008-02-27 14:04:29 +0100 | [diff] [blame] | 356 | clear_tsk_thread_flag(p, TIF_DEBUG); |
Haavard Skinnemoen | 13b54a5 | 2007-11-27 13:50:45 +0100 | [diff] [blame] | 357 | if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG)) |
| 358 | ocd_enable(p); |
| 359 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* r12-r8 are dummy parameters to force the compiler to use the stack */ |
| 364 | asmlinkage int sys_fork(struct pt_regs *regs) |
| 365 | { |
| 366 | return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL); |
| 367 | } |
| 368 | |
| 369 | asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp, |
Hans-Christian Egtvedt | 992a88b | 2010-12-09 09:02:06 +0100 | [diff] [blame] | 370 | void __user *parent_tidptr, void __user *child_tidptr, |
| 371 | struct pt_regs *regs) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 372 | { |
| 373 | if (!newsp) |
| 374 | newsp = regs->sp; |
Hans-Christian Egtvedt | 992a88b | 2010-12-09 09:02:06 +0100 | [diff] [blame] | 375 | return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, |
| 376 | child_tidptr); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | asmlinkage int sys_vfork(struct pt_regs *regs) |
| 380 | { |
| 381 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, |
| 382 | 0, NULL, NULL); |
| 383 | } |
| 384 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 385 | asmlinkage int sys_execve(const char __user *ufilename, |
David Howells | d762746 | 2010-08-17 23:52:56 +0100 | [diff] [blame] | 386 | const char __user *const __user *uargv, |
| 387 | const char __user *const __user *uenvp, |
| 388 | struct pt_regs *regs) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 389 | { |
| 390 | int error; |
| 391 | char *filename; |
| 392 | |
| 393 | filename = getname(ufilename); |
| 394 | error = PTR_ERR(filename); |
| 395 | if (IS_ERR(filename)) |
| 396 | goto out; |
| 397 | |
| 398 | error = do_execve(filename, uargv, uenvp, regs); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 399 | putname(filename); |
| 400 | |
| 401 | out: |
| 402 | return error; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /* |
| 407 | * This function is supposed to answer the question "who called |
| 408 | * schedule()?" |
| 409 | */ |
| 410 | unsigned long get_wchan(struct task_struct *p) |
| 411 | { |
| 412 | unsigned long pc; |
| 413 | unsigned long stack_page; |
| 414 | |
| 415 | if (!p || p == current || p->state == TASK_RUNNING) |
| 416 | return 0; |
| 417 | |
Roman Zippel | c9f4f06 | 2007-05-09 02:35:16 -0700 | [diff] [blame] | 418 | stack_page = (unsigned long)task_stack_page(p); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 419 | BUG_ON(!stack_page); |
| 420 | |
| 421 | /* |
| 422 | * The stored value of PC is either the address right after |
| 423 | * the call to __switch_to() or ret_from_fork. |
| 424 | */ |
| 425 | pc = thread_saved_pc(p); |
| 426 | if (in_sched_functions(pc)) { |
| 427 | #ifdef CONFIG_FRAME_POINTER |
| 428 | unsigned long fp = p->thread.cpu_context.r7; |
| 429 | BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page)); |
| 430 | pc = *(unsigned long *)fp; |
| 431 | #else |
| 432 | /* |
| 433 | * We depend on the frame size of schedule here, which |
| 434 | * is actually quite ugly. It might be possible to |
| 435 | * determine the frame size automatically at build |
| 436 | * time by doing this: |
| 437 | * - compile sched.c |
| 438 | * - disassemble the resulting sched.o |
| 439 | * - look for 'sub sp,??' shortly after '<schedule>:' |
| 440 | */ |
| 441 | unsigned long sp = p->thread.cpu_context.ksp + 16; |
| 442 | BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page)); |
| 443 | pc = *(unsigned long *)sp; |
| 444 | #endif |
| 445 | } |
| 446 | |
| 447 | return pc; |
| 448 | } |