process.c 7.0 KB

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