process.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/ptrace.h>
  13. #include <linux/reboot.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/unistd.h>
  16. #include <asm/sysreg.h>
  17. #include <asm/ocd.h>
  18. void (*pm_power_off)(void) = NULL;
  19. EXPORT_SYMBOL(pm_power_off);
  20. extern void cpu_idle_sleep(void);
  21. /*
  22. * This file handles the architecture-dependent parts of process handling..
  23. */
  24. void cpu_idle(void)
  25. {
  26. /* endless idle loop with no priority at all */
  27. while (1) {
  28. while (!need_resched())
  29. cpu_idle_sleep();
  30. preempt_enable_no_resched();
  31. schedule();
  32. preempt_disable();
  33. }
  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. }
  48. void machine_restart(char *cmd)
  49. {
  50. __mtdr(DBGREG_DC, DC_DBE);
  51. __mtdr(DBGREG_DC, DC_RES);
  52. while (1) ;
  53. }
  54. /*
  55. * PC is actually discarded when returning from a system call -- the
  56. * return address must be stored in LR. This function will make sure
  57. * LR points to do_exit before starting the thread.
  58. *
  59. * Also, when returning from fork(), r12 is 0, so we must copy the
  60. * argument as well.
  61. *
  62. * r0 : The argument to the main thread function
  63. * r1 : The address of do_exit
  64. * r2 : The address of the main thread function
  65. */
  66. asmlinkage extern void kernel_thread_helper(void);
  67. __asm__(" .type kernel_thread_helper, @function\n"
  68. "kernel_thread_helper:\n"
  69. " mov r12, r0\n"
  70. " mov lr, r2\n"
  71. " mov pc, r1\n"
  72. " .size kernel_thread_helper, . - kernel_thread_helper");
  73. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  74. {
  75. struct pt_regs regs;
  76. memset(&regs, 0, sizeof(regs));
  77. regs.r0 = (unsigned long)arg;
  78. regs.r1 = (unsigned long)fn;
  79. regs.r2 = (unsigned long)do_exit;
  80. regs.lr = (unsigned long)kernel_thread_helper;
  81. regs.pc = (unsigned long)kernel_thread_helper;
  82. regs.sr = MODE_SUPERVISOR;
  83. return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
  84. 0, &regs, 0, NULL, NULL);
  85. }
  86. EXPORT_SYMBOL(kernel_thread);
  87. /*
  88. * Free current thread data structures etc
  89. */
  90. void exit_thread(void)
  91. {
  92. /* nothing to do */
  93. }
  94. void flush_thread(void)
  95. {
  96. /* nothing to do */
  97. }
  98. void release_thread(struct task_struct *dead_task)
  99. {
  100. /* do nothing */
  101. }
  102. static void dump_mem(const char *str, const char *log_lvl,
  103. unsigned long bottom, unsigned long top)
  104. {
  105. unsigned long p;
  106. int i;
  107. printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top);
  108. for (p = bottom & ~31; p < top; ) {
  109. printk("%s%04lx: ", log_lvl, p & 0xffff);
  110. for (i = 0; i < 8; i++, p += 4) {
  111. unsigned int val;
  112. if (p < bottom || p >= top)
  113. printk(" ");
  114. else {
  115. if (__get_user(val, (unsigned int __user *)p)) {
  116. printk("\n");
  117. goto out;
  118. }
  119. printk("%08x ", val);
  120. }
  121. }
  122. printk("\n");
  123. }
  124. out:
  125. return;
  126. }
  127. static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
  128. {
  129. return (p > (unsigned long)tinfo)
  130. && (p < (unsigned long)tinfo + THREAD_SIZE - 3);
  131. }
  132. #ifdef CONFIG_FRAME_POINTER
  133. static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
  134. struct pt_regs *regs, const char *log_lvl)
  135. {
  136. unsigned long lr, fp;
  137. struct thread_info *tinfo;
  138. if (regs)
  139. fp = regs->r7;
  140. else if (tsk == current)
  141. asm("mov %0, r7" : "=r"(fp));
  142. else
  143. fp = tsk->thread.cpu_context.r7;
  144. /*
  145. * Walk the stack as long as the frame pointer (a) is within
  146. * the kernel stack of the task, and (b) it doesn't move
  147. * downwards.
  148. */
  149. tinfo = task_thread_info(tsk);
  150. printk("%sCall trace:\n", log_lvl);
  151. while (valid_stack_ptr(tinfo, fp)) {
  152. unsigned long new_fp;
  153. lr = *(unsigned long *)fp;
  154. #ifdef CONFIG_KALLSYMS
  155. printk("%s [<%08lx>] ", log_lvl, lr);
  156. #else
  157. printk(" [<%08lx>] ", lr);
  158. #endif
  159. print_symbol("%s\n", lr);
  160. new_fp = *(unsigned long *)(fp + 4);
  161. if (new_fp <= fp)
  162. break;
  163. fp = new_fp;
  164. }
  165. printk("\n");
  166. }
  167. #else
  168. static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
  169. struct pt_regs *regs, const char *log_lvl)
  170. {
  171. unsigned long addr;
  172. printk("%sCall trace:\n", log_lvl);
  173. while (!kstack_end(sp)) {
  174. addr = *sp++;
  175. if (kernel_text_address(addr)) {
  176. #ifdef CONFIG_KALLSYMS
  177. printk("%s [<%08lx>] ", log_lvl, addr);
  178. #else
  179. printk(" [<%08lx>] ", addr);
  180. #endif
  181. print_symbol("%s\n", addr);
  182. }
  183. }
  184. printk("\n");
  185. }
  186. #endif
  187. void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp,
  188. struct pt_regs *regs, const char *log_lvl)
  189. {
  190. struct thread_info *tinfo;
  191. if (sp == 0) {
  192. if (tsk)
  193. sp = tsk->thread.cpu_context.ksp;
  194. else
  195. sp = (unsigned long)&tinfo;
  196. }
  197. if (!tsk)
  198. tsk = current;
  199. tinfo = task_thread_info(tsk);
  200. if (valid_stack_ptr(tinfo, sp)) {
  201. dump_mem("Stack: ", log_lvl, sp,
  202. THREAD_SIZE + (unsigned long)tinfo);
  203. show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl);
  204. }
  205. }
  206. void show_stack(struct task_struct *tsk, unsigned long *stack)
  207. {
  208. show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
  209. }
  210. void dump_stack(void)
  211. {
  212. unsigned long stack;
  213. show_trace_log_lvl(current, &stack, NULL, "");
  214. }
  215. EXPORT_SYMBOL(dump_stack);
  216. static const char *cpu_modes[] = {
  217. "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
  218. "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
  219. };
  220. void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
  221. {
  222. unsigned long sp = regs->sp;
  223. unsigned long lr = regs->lr;
  224. unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
  225. if (!user_mode(regs)) {
  226. sp = (unsigned long)regs + FRAME_SIZE_FULL;
  227. printk("%s", log_lvl);
  228. print_symbol("PC is at %s\n", instruction_pointer(regs));
  229. printk("%s", log_lvl);
  230. print_symbol("LR is at %s\n", lr);
  231. }
  232. printk("%spc : [<%08lx>] lr : [<%08lx>] %s\n"
  233. "%ssp : %08lx r12: %08lx r11: %08lx\n",
  234. log_lvl, instruction_pointer(regs), lr, print_tainted(),
  235. log_lvl, sp, regs->r12, regs->r11);
  236. printk("%sr10: %08lx r9 : %08lx r8 : %08lx\n",
  237. log_lvl, regs->r10, regs->r9, regs->r8);
  238. printk("%sr7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
  239. log_lvl, regs->r7, regs->r6, regs->r5, regs->r4);
  240. printk("%sr3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
  241. log_lvl, regs->r3, regs->r2, regs->r1, regs->r0);
  242. printk("%sFlags: %c%c%c%c%c\n", log_lvl,
  243. regs->sr & SR_Q ? 'Q' : 'q',
  244. regs->sr & SR_V ? 'V' : 'v',
  245. regs->sr & SR_N ? 'N' : 'n',
  246. regs->sr & SR_Z ? 'Z' : 'z',
  247. regs->sr & SR_C ? 'C' : 'c');
  248. printk("%sMode bits: %c%c%c%c%c%c%c%c%c\n", log_lvl,
  249. regs->sr & SR_H ? 'H' : 'h',
  250. regs->sr & SR_R ? 'R' : 'r',
  251. regs->sr & SR_J ? 'J' : 'j',
  252. regs->sr & SR_EM ? 'E' : 'e',
  253. regs->sr & SR_I3M ? '3' : '.',
  254. regs->sr & SR_I2M ? '2' : '.',
  255. regs->sr & SR_I1M ? '1' : '.',
  256. regs->sr & SR_I0M ? '0' : '.',
  257. regs->sr & SR_GM ? 'G' : 'g');
  258. printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]);
  259. printk("%sProcess: %s [%d] (task: %p thread: %p)\n",
  260. log_lvl, current->comm, current->pid, current,
  261. task_thread_info(current));
  262. }
  263. void show_regs(struct pt_regs *regs)
  264. {
  265. unsigned long sp = regs->sp;
  266. if (!user_mode(regs))
  267. sp = (unsigned long)regs + FRAME_SIZE_FULL;
  268. show_regs_log_lvl(regs, "");
  269. show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
  270. }
  271. EXPORT_SYMBOL(show_regs);
  272. /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
  273. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  274. {
  275. /* Not valid */
  276. return 0;
  277. }
  278. asmlinkage void ret_from_fork(void);
  279. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  280. unsigned long unused,
  281. struct task_struct *p, struct pt_regs *regs)
  282. {
  283. struct pt_regs *childregs;
  284. childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)task_stack_page(p))) - 1;
  285. *childregs = *regs;
  286. if (user_mode(regs))
  287. childregs->sp = usp;
  288. else
  289. childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  290. childregs->r12 = 0; /* Set return value for child */
  291. p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
  292. p->thread.cpu_context.ksp = (unsigned long)childregs;
  293. p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
  294. return 0;
  295. }
  296. /* r12-r8 are dummy parameters to force the compiler to use the stack */
  297. asmlinkage int sys_fork(struct pt_regs *regs)
  298. {
  299. return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  300. }
  301. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  302. unsigned long parent_tidptr,
  303. unsigned long child_tidptr, struct pt_regs *regs)
  304. {
  305. if (!newsp)
  306. newsp = regs->sp;
  307. return do_fork(clone_flags, newsp, regs, 0,
  308. (int __user *)parent_tidptr,
  309. (int __user *)child_tidptr);
  310. }
  311. asmlinkage int sys_vfork(struct pt_regs *regs)
  312. {
  313. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs,
  314. 0, NULL, NULL);
  315. }
  316. asmlinkage int sys_execve(char __user *ufilename, char __user *__user *uargv,
  317. char __user *__user *uenvp, struct pt_regs *regs)
  318. {
  319. int error;
  320. char *filename;
  321. filename = getname(ufilename);
  322. error = PTR_ERR(filename);
  323. if (IS_ERR(filename))
  324. goto out;
  325. error = do_execve(filename, uargv, uenvp, regs);
  326. if (error == 0)
  327. current->ptrace &= ~PT_DTRACE;
  328. putname(filename);
  329. out:
  330. return error;
  331. }
  332. /*
  333. * This function is supposed to answer the question "who called
  334. * schedule()?"
  335. */
  336. unsigned long get_wchan(struct task_struct *p)
  337. {
  338. unsigned long pc;
  339. unsigned long stack_page;
  340. if (!p || p == current || p->state == TASK_RUNNING)
  341. return 0;
  342. stack_page = (unsigned long)task_stack_page(p);
  343. BUG_ON(!stack_page);
  344. /*
  345. * The stored value of PC is either the address right after
  346. * the call to __switch_to() or ret_from_fork.
  347. */
  348. pc = thread_saved_pc(p);
  349. if (in_sched_functions(pc)) {
  350. #ifdef CONFIG_FRAME_POINTER
  351. unsigned long fp = p->thread.cpu_context.r7;
  352. BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
  353. pc = *(unsigned long *)fp;
  354. #else
  355. /*
  356. * We depend on the frame size of schedule here, which
  357. * is actually quite ugly. It might be possible to
  358. * determine the frame size automatically at build
  359. * time by doing this:
  360. * - compile sched.c
  361. * - disassemble the resulting sched.o
  362. * - look for 'sub sp,??' shortly after '<schedule>:'
  363. */
  364. unsigned long sp = p->thread.cpu_context.ksp + 16;
  365. BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
  366. pc = *(unsigned long *)sp;
  367. #endif
  368. }
  369. return pc;
  370. }