process_32.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * arch/sh/kernel/process.c
  3. *
  4. * This file handles the architecture-dependent parts of process handling..
  5. *
  6. * Copyright (C) 1995 Linus Torvalds
  7. *
  8. * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
  9. * Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
  10. * Copyright (C) 2002 - 2008 Paul Mundt
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file "COPYING" in the main directory of this archive
  14. * for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/mm.h>
  18. #include <linux/slab.h>
  19. #include <linux/elfcore.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/fs.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/hw_breakpoint.h>
  24. #include <linux/prefetch.h>
  25. #include <linux/stackprotector.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/fpu.h>
  29. #include <asm/syscalls.h>
  30. #include <asm/switch_to.h>
  31. void show_regs(struct pt_regs * regs)
  32. {
  33. printk("\n");
  34. printk("Pid : %d, Comm: \t\t%s\n", task_pid_nr(current), current->comm);
  35. printk("CPU : %d \t\t%s (%s %.*s)\n\n",
  36. smp_processor_id(), print_tainted(), init_utsname()->release,
  37. (int)strcspn(init_utsname()->version, " "),
  38. init_utsname()->version);
  39. print_symbol("PC is at %s\n", instruction_pointer(regs));
  40. print_symbol("PR is at %s\n", regs->pr);
  41. printk("PC : %08lx SP : %08lx SR : %08lx ",
  42. regs->pc, regs->regs[15], regs->sr);
  43. #ifdef CONFIG_MMU
  44. printk("TEA : %08x\n", __raw_readl(MMU_TEA));
  45. #else
  46. printk("\n");
  47. #endif
  48. printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
  49. regs->regs[0],regs->regs[1],
  50. regs->regs[2],regs->regs[3]);
  51. printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
  52. regs->regs[4],regs->regs[5],
  53. regs->regs[6],regs->regs[7]);
  54. printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
  55. regs->regs[8],regs->regs[9],
  56. regs->regs[10],regs->regs[11]);
  57. printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
  58. regs->regs[12],regs->regs[13],
  59. regs->regs[14]);
  60. printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
  61. regs->mach, regs->macl, regs->gbr, regs->pr);
  62. show_trace(NULL, (unsigned long *)regs->regs[15], regs);
  63. show_code(regs);
  64. }
  65. /*
  66. * Create a kernel thread
  67. */
  68. __noreturn void kernel_thread_helper(void *arg, int (*fn)(void *))
  69. {
  70. do_exit(fn(arg));
  71. }
  72. /* Don't use this in BL=1(cli). Or else, CPU resets! */
  73. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  74. {
  75. struct pt_regs regs;
  76. int pid;
  77. memset(&regs, 0, sizeof(regs));
  78. regs.regs[4] = (unsigned long)arg;
  79. regs.regs[5] = (unsigned long)fn;
  80. regs.pc = (unsigned long)kernel_thread_helper;
  81. regs.sr = SR_MD;
  82. #if defined(CONFIG_SH_FPU)
  83. regs.sr |= SR_FD;
  84. #endif
  85. /* Ok, create the new process.. */
  86. pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
  87. &regs, 0, NULL, NULL);
  88. return pid;
  89. }
  90. EXPORT_SYMBOL(kernel_thread);
  91. void start_thread(struct pt_regs *regs, unsigned long new_pc,
  92. unsigned long new_sp)
  93. {
  94. regs->pr = 0;
  95. regs->sr = SR_FD;
  96. regs->pc = new_pc;
  97. regs->regs[15] = new_sp;
  98. free_thread_xstate(current);
  99. }
  100. EXPORT_SYMBOL(start_thread);
  101. /*
  102. * Free current thread data structures etc..
  103. */
  104. void exit_thread(void)
  105. {
  106. }
  107. void flush_thread(void)
  108. {
  109. struct task_struct *tsk = current;
  110. flush_ptrace_hw_breakpoint(tsk);
  111. #if defined(CONFIG_SH_FPU)
  112. /* Forget lazy FPU state */
  113. clear_fpu(tsk, task_pt_regs(tsk));
  114. clear_used_math();
  115. #endif
  116. }
  117. void release_thread(struct task_struct *dead_task)
  118. {
  119. /* do nothing */
  120. }
  121. /* Fill in the fpu structure for a core dump.. */
  122. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  123. {
  124. int fpvalid = 0;
  125. #if defined(CONFIG_SH_FPU)
  126. struct task_struct *tsk = current;
  127. fpvalid = !!tsk_used_math(tsk);
  128. if (fpvalid)
  129. fpvalid = !fpregs_get(tsk, NULL, 0,
  130. sizeof(struct user_fpu_struct),
  131. fpu, NULL);
  132. #endif
  133. return fpvalid;
  134. }
  135. EXPORT_SYMBOL(dump_fpu);
  136. asmlinkage void ret_from_fork(void);
  137. int copy_thread(unsigned long clone_flags, unsigned long usp,
  138. unsigned long unused,
  139. struct task_struct *p, struct pt_regs *regs)
  140. {
  141. struct thread_info *ti = task_thread_info(p);
  142. struct pt_regs *childregs;
  143. #if defined(CONFIG_SH_DSP)
  144. struct task_struct *tsk = current;
  145. if (is_dsp_enabled(tsk)) {
  146. /* We can use the __save_dsp or just copy the struct:
  147. * __save_dsp(p);
  148. * p->thread.dsp_status.status |= SR_DSP
  149. */
  150. p->thread.dsp_status = tsk->thread.dsp_status;
  151. }
  152. #endif
  153. childregs = task_pt_regs(p);
  154. *childregs = *regs;
  155. if (user_mode(regs)) {
  156. childregs->regs[15] = usp;
  157. ti->addr_limit = USER_DS;
  158. } else {
  159. childregs->regs[15] = (unsigned long)childregs;
  160. ti->addr_limit = KERNEL_DS;
  161. ti->status &= ~TS_USEDFPU;
  162. p->fpu_counter = 0;
  163. }
  164. if (clone_flags & CLONE_SETTLS)
  165. childregs->gbr = childregs->regs[0];
  166. childregs->regs[0] = 0; /* Set return value for child */
  167. p->thread.sp = (unsigned long) childregs;
  168. p->thread.pc = (unsigned long) ret_from_fork;
  169. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  170. return 0;
  171. }
  172. /*
  173. * switch_to(x,y) should switch tasks from x to y.
  174. *
  175. */
  176. __notrace_funcgraph struct task_struct *
  177. __switch_to(struct task_struct *prev, struct task_struct *next)
  178. {
  179. struct thread_struct *next_t = &next->thread;
  180. #if defined(CONFIG_CC_STACKPROTECTOR) && !defined(CONFIG_SMP)
  181. __stack_chk_guard = next->stack_canary;
  182. #endif
  183. unlazy_fpu(prev, task_pt_regs(prev));
  184. /* we're going to use this soon, after a few expensive things */
  185. if (next->fpu_counter > 5)
  186. prefetch(next_t->xstate);
  187. #ifdef CONFIG_MMU
  188. /*
  189. * Restore the kernel mode register
  190. * k7 (r7_bank1)
  191. */
  192. asm volatile("ldc %0, r7_bank"
  193. : /* no output */
  194. : "r" (task_thread_info(next)));
  195. #endif
  196. /*
  197. * If the task has used fpu the last 5 timeslices, just do a full
  198. * restore of the math state immediately to avoid the trap; the
  199. * chances of needing FPU soon are obviously high now
  200. */
  201. if (next->fpu_counter > 5)
  202. __fpu_state_restore();
  203. return prev;
  204. }
  205. asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
  206. unsigned long r6, unsigned long r7,
  207. struct pt_regs __regs)
  208. {
  209. #ifdef CONFIG_MMU
  210. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  211. return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
  212. #else
  213. /* fork almost works, enough to trick you into looking elsewhere :-( */
  214. return -EINVAL;
  215. #endif
  216. }
  217. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  218. unsigned long parent_tidptr,
  219. unsigned long child_tidptr,
  220. struct pt_regs __regs)
  221. {
  222. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  223. if (!newsp)
  224. newsp = regs->regs[15];
  225. return do_fork(clone_flags, newsp, regs, 0,
  226. (int __user *)parent_tidptr,
  227. (int __user *)child_tidptr);
  228. }
  229. /*
  230. * This is trivial, and on the face of it looks like it
  231. * could equally well be done in user mode.
  232. *
  233. * Not so, for quite unobvious reasons - register pressure.
  234. * In user mode vfork() cannot have a stack frame, and if
  235. * done by calling the "clone()" system call directly, you
  236. * do not have enough call-clobbered registers to hold all
  237. * the information you need.
  238. */
  239. asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
  240. unsigned long r6, unsigned long r7,
  241. struct pt_regs __regs)
  242. {
  243. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  244. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
  245. 0, NULL, NULL);
  246. }
  247. /*
  248. * sys_execve() executes a new program.
  249. */
  250. asmlinkage int sys_execve(const char __user *ufilename,
  251. const char __user *const __user *uargv,
  252. const char __user *const __user *uenvp,
  253. unsigned long r7, struct pt_regs __regs)
  254. {
  255. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  256. int error;
  257. char *filename;
  258. filename = getname(ufilename);
  259. error = PTR_ERR(filename);
  260. if (IS_ERR(filename))
  261. goto out;
  262. error = do_execve(filename, uargv, uenvp, regs);
  263. putname(filename);
  264. out:
  265. return error;
  266. }
  267. unsigned long get_wchan(struct task_struct *p)
  268. {
  269. unsigned long pc;
  270. if (!p || p == current || p->state == TASK_RUNNING)
  271. return 0;
  272. /*
  273. * The same comment as on the Alpha applies here, too ...
  274. */
  275. pc = thread_saved_pc(p);
  276. #ifdef CONFIG_FRAME_POINTER
  277. if (in_sched_functions(pc)) {
  278. unsigned long schedule_frame = (unsigned long)p->thread.sp;
  279. return ((unsigned long *)schedule_frame)[21];
  280. }
  281. #endif
  282. return pc;
  283. }