process.c 8.6 KB

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