process.c 11 KB

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