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