process.c 11 KB

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