process.c 11 KB

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