process.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/unistd.h>
  15. #include <asm/sysreg.h>
  16. #include <asm/ocd.h>
  17. void (*pm_power_off)(void) = NULL;
  18. EXPORT_SYMBOL(pm_power_off);
  19. /*
  20. * This file handles the architecture-dependent parts of process handling..
  21. */
  22. void cpu_idle(void)
  23. {
  24. /* endless idle loop with no priority at all */
  25. while (1) {
  26. /* TODO: Enter sleep mode */
  27. while (!need_resched())
  28. cpu_relax();
  29. preempt_enable_no_resched();
  30. schedule();
  31. preempt_disable();
  32. }
  33. }
  34. void machine_halt(void)
  35. {
  36. /*
  37. * Enter Stop mode. The 32 kHz oscillator will keep running so
  38. * the RTC will keep the time properly and the system will
  39. * boot quickly.
  40. */
  41. asm volatile("sleep 3\n\t"
  42. "sub pc, -2");
  43. }
  44. void machine_power_off(void)
  45. {
  46. }
  47. void machine_restart(char *cmd)
  48. {
  49. __mtdr(DBGREG_DC, DC_DBE);
  50. __mtdr(DBGREG_DC, DC_RES);
  51. while (1) ;
  52. }
  53. /*
  54. * PC is actually discarded when returning from a system call -- the
  55. * return address must be stored in LR. This function will make sure
  56. * LR points to do_exit before starting the thread.
  57. *
  58. * Also, when returning from fork(), r12 is 0, so we must copy the
  59. * argument as well.
  60. *
  61. * r0 : The argument to the main thread function
  62. * r1 : The address of do_exit
  63. * r2 : The address of the main thread function
  64. */
  65. asmlinkage extern void kernel_thread_helper(void);
  66. __asm__(" .type kernel_thread_helper, @function\n"
  67. "kernel_thread_helper:\n"
  68. " mov r12, r0\n"
  69. " mov lr, r2\n"
  70. " mov pc, r1\n"
  71. " .size kernel_thread_helper, . - kernel_thread_helper");
  72. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  73. {
  74. struct pt_regs regs;
  75. memset(&regs, 0, sizeof(regs));
  76. regs.r0 = (unsigned long)arg;
  77. regs.r1 = (unsigned long)fn;
  78. regs.r2 = (unsigned long)do_exit;
  79. regs.lr = (unsigned long)kernel_thread_helper;
  80. regs.pc = (unsigned long)kernel_thread_helper;
  81. regs.sr = MODE_SUPERVISOR;
  82. return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
  83. 0, &regs, 0, NULL, NULL);
  84. }
  85. EXPORT_SYMBOL(kernel_thread);
  86. /*
  87. * Free current thread data structures etc
  88. */
  89. void exit_thread(void)
  90. {
  91. /* nothing to do */
  92. }
  93. void flush_thread(void)
  94. {
  95. /* nothing to do */
  96. }
  97. void release_thread(struct task_struct *dead_task)
  98. {
  99. /* do nothing */
  100. }
  101. static const char *cpu_modes[] = {
  102. "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
  103. "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
  104. };
  105. void show_regs(struct pt_regs *regs)
  106. {
  107. unsigned long sp = regs->sp;
  108. unsigned long lr = regs->lr;
  109. unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
  110. if (!user_mode(regs))
  111. sp = (unsigned long)regs + FRAME_SIZE_FULL;
  112. print_symbol("PC is at %s\n", instruction_pointer(regs));
  113. print_symbol("LR is at %s\n", lr);
  114. printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
  115. "sp : %08lx r12: %08lx r11: %08lx\n",
  116. instruction_pointer(regs),
  117. lr, print_tainted(), sp, regs->r12, regs->r11);
  118. printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
  119. regs->r10, regs->r9, regs->r8);
  120. printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
  121. regs->r7, regs->r6, regs->r5, regs->r4);
  122. printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
  123. regs->r3, regs->r2, regs->r1, regs->r0);
  124. printk("Flags: %c%c%c%c%c\n",
  125. regs->sr & SR_Q ? 'Q' : 'q',
  126. regs->sr & SR_V ? 'V' : 'v',
  127. regs->sr & SR_N ? 'N' : 'n',
  128. regs->sr & SR_Z ? 'Z' : 'z',
  129. regs->sr & SR_C ? 'C' : 'c');
  130. printk("Mode bits: %c%c%c%c%c%c%c%c%c\n",
  131. regs->sr & SR_H ? 'H' : 'h',
  132. regs->sr & SR_R ? 'R' : 'r',
  133. regs->sr & SR_J ? 'J' : 'j',
  134. regs->sr & SR_EM ? 'E' : 'e',
  135. regs->sr & SR_I3M ? '3' : '.',
  136. regs->sr & SR_I2M ? '2' : '.',
  137. regs->sr & SR_I1M ? '1' : '.',
  138. regs->sr & SR_I0M ? '0' : '.',
  139. regs->sr & SR_GM ? 'G' : 'g');
  140. printk("CPU Mode: %s\n", cpu_modes[mode]);
  141. show_trace(NULL, (unsigned long *)sp, regs);
  142. }
  143. EXPORT_SYMBOL(show_regs);
  144. /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
  145. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  146. {
  147. /* Not valid */
  148. return 0;
  149. }
  150. asmlinkage void ret_from_fork(void);
  151. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  152. unsigned long unused,
  153. struct task_struct *p, struct pt_regs *regs)
  154. {
  155. struct pt_regs *childregs;
  156. childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)p->thread_info)) - 1;
  157. *childregs = *regs;
  158. if (user_mode(regs))
  159. childregs->sp = usp;
  160. else
  161. childregs->sp = (unsigned long)p->thread_info + THREAD_SIZE;
  162. childregs->r12 = 0; /* Set return value for child */
  163. p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
  164. p->thread.cpu_context.ksp = (unsigned long)childregs;
  165. p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
  166. return 0;
  167. }
  168. /* r12-r8 are dummy parameters to force the compiler to use the stack */
  169. asmlinkage int sys_fork(struct pt_regs *regs)
  170. {
  171. return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  172. }
  173. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  174. unsigned long parent_tidptr,
  175. unsigned long child_tidptr, struct pt_regs *regs)
  176. {
  177. if (!newsp)
  178. newsp = regs->sp;
  179. return do_fork(clone_flags, newsp, regs, 0,
  180. (int __user *)parent_tidptr,
  181. (int __user *)child_tidptr);
  182. }
  183. asmlinkage int sys_vfork(struct pt_regs *regs)
  184. {
  185. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs,
  186. 0, NULL, NULL);
  187. }
  188. asmlinkage int sys_execve(char __user *ufilename, char __user *__user *uargv,
  189. char __user *__user *uenvp, struct pt_regs *regs)
  190. {
  191. int error;
  192. char *filename;
  193. filename = getname(ufilename);
  194. error = PTR_ERR(filename);
  195. if (IS_ERR(filename))
  196. goto out;
  197. error = do_execve(filename, uargv, uenvp, regs);
  198. if (error == 0)
  199. current->ptrace &= ~PT_DTRACE;
  200. putname(filename);
  201. out:
  202. return error;
  203. }
  204. /*
  205. * This function is supposed to answer the question "who called
  206. * schedule()?"
  207. */
  208. unsigned long get_wchan(struct task_struct *p)
  209. {
  210. unsigned long pc;
  211. unsigned long stack_page;
  212. if (!p || p == current || p->state == TASK_RUNNING)
  213. return 0;
  214. stack_page = (unsigned long)p->thread_info;
  215. BUG_ON(!stack_page);
  216. /*
  217. * The stored value of PC is either the address right after
  218. * the call to __switch_to() or ret_from_fork.
  219. */
  220. pc = thread_saved_pc(p);
  221. if (in_sched_functions(pc)) {
  222. #ifdef CONFIG_FRAME_POINTER
  223. unsigned long fp = p->thread.cpu_context.r7;
  224. BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
  225. pc = *(unsigned long *)fp;
  226. #else
  227. /*
  228. * We depend on the frame size of schedule here, which
  229. * is actually quite ugly. It might be possible to
  230. * determine the frame size automatically at build
  231. * time by doing this:
  232. * - compile sched.c
  233. * - disassemble the resulting sched.o
  234. * - look for 'sub sp,??' shortly after '<schedule>:'
  235. */
  236. unsigned long sp = p->thread.cpu_context.ksp + 16;
  237. BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
  238. pc = *(unsigned long *)sp;
  239. #endif
  240. }
  241. return pc;
  242. }