process.c 8.5 KB

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