process.c 11 KB

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