process.c 8.9 KB

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