process_32.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <asm/uaccess.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/system.h>
  28. #include <asm/fpu.h>
  29. #include <asm/syscalls.h>
  30. void show_regs(struct pt_regs * regs)
  31. {
  32. printk("\n");
  33. printk("Pid : %d, Comm: \t\t%s\n", task_pid_nr(current), current->comm);
  34. printk("CPU : %d \t\t%s (%s %.*s)\n\n",
  35. smp_processor_id(), print_tainted(), init_utsname()->release,
  36. (int)strcspn(init_utsname()->version, " "),
  37. init_utsname()->version);
  38. print_symbol("PC is at %s\n", instruction_pointer(regs));
  39. print_symbol("PR is at %s\n", regs->pr);
  40. printk("PC : %08lx SP : %08lx SR : %08lx ",
  41. regs->pc, regs->regs[15], regs->sr);
  42. #ifdef CONFIG_MMU
  43. printk("TEA : %08x\n", __raw_readl(MMU_TEA));
  44. #else
  45. printk("\n");
  46. #endif
  47. printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
  48. regs->regs[0],regs->regs[1],
  49. regs->regs[2],regs->regs[3]);
  50. printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
  51. regs->regs[4],regs->regs[5],
  52. regs->regs[6],regs->regs[7]);
  53. printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
  54. regs->regs[8],regs->regs[9],
  55. regs->regs[10],regs->regs[11]);
  56. printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
  57. regs->regs[12],regs->regs[13],
  58. regs->regs[14]);
  59. printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
  60. regs->mach, regs->macl, regs->gbr, regs->pr);
  61. show_trace(NULL, (unsigned long *)regs->regs[15], regs);
  62. show_code(regs);
  63. }
  64. /*
  65. * Create a kernel thread
  66. */
  67. ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
  68. {
  69. do_exit(fn(arg));
  70. }
  71. /* Don't use this in BL=1(cli). Or else, CPU resets! */
  72. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  73. {
  74. struct pt_regs regs;
  75. int pid;
  76. memset(&regs, 0, sizeof(regs));
  77. regs.regs[4] = (unsigned long)arg;
  78. regs.regs[5] = (unsigned long)fn;
  79. regs.pc = (unsigned long)kernel_thread_helper;
  80. regs.sr = SR_MD;
  81. #if defined(CONFIG_SH_FPU)
  82. regs.sr |= SR_FD;
  83. #endif
  84. /* Ok, create the new process.. */
  85. pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
  86. &regs, 0, NULL, NULL);
  87. return pid;
  88. }
  89. EXPORT_SYMBOL(kernel_thread);
  90. void start_thread(struct pt_regs *regs, unsigned long new_pc,
  91. unsigned long new_sp)
  92. {
  93. set_fs(USER_DS);
  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. /*
  137. * This gets called before we allocate a new thread and copy
  138. * the current task into it.
  139. */
  140. void prepare_to_copy(struct task_struct *tsk)
  141. {
  142. unlazy_fpu(tsk, task_pt_regs(tsk));
  143. }
  144. asmlinkage void ret_from_fork(void);
  145. int copy_thread(unsigned long clone_flags, unsigned long usp,
  146. unsigned long unused,
  147. struct task_struct *p, struct pt_regs *regs)
  148. {
  149. struct thread_info *ti = task_thread_info(p);
  150. struct pt_regs *childregs;
  151. #if defined(CONFIG_SH_DSP)
  152. struct task_struct *tsk = current;
  153. if (is_dsp_enabled(tsk)) {
  154. /* We can use the __save_dsp or just copy the struct:
  155. * __save_dsp(p);
  156. * p->thread.dsp_status.status |= SR_DSP
  157. */
  158. p->thread.dsp_status = tsk->thread.dsp_status;
  159. }
  160. #endif
  161. childregs = task_pt_regs(p);
  162. *childregs = *regs;
  163. if (user_mode(regs)) {
  164. childregs->regs[15] = usp;
  165. ti->addr_limit = USER_DS;
  166. } else {
  167. childregs->regs[15] = (unsigned long)childregs;
  168. ti->addr_limit = KERNEL_DS;
  169. ti->status &= ~TS_USEDFPU;
  170. p->fpu_counter = 0;
  171. }
  172. if (clone_flags & CLONE_SETTLS)
  173. childregs->gbr = childregs->regs[0];
  174. childregs->regs[0] = 0; /* Set return value for child */
  175. p->thread.sp = (unsigned long) childregs;
  176. p->thread.pc = (unsigned long) ret_from_fork;
  177. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  178. return 0;
  179. }
  180. /*
  181. * switch_to(x,y) should switch tasks from x to y.
  182. *
  183. */
  184. __notrace_funcgraph struct task_struct *
  185. __switch_to(struct task_struct *prev, struct task_struct *next)
  186. {
  187. struct thread_struct *next_t = &next->thread;
  188. unlazy_fpu(prev, task_pt_regs(prev));
  189. /* we're going to use this soon, after a few expensive things */
  190. if (next->fpu_counter > 5)
  191. prefetch(next_t->xstate);
  192. #ifdef CONFIG_MMU
  193. /*
  194. * Restore the kernel mode register
  195. * k7 (r7_bank1)
  196. */
  197. asm volatile("ldc %0, r7_bank"
  198. : /* no output */
  199. : "r" (task_thread_info(next)));
  200. #endif
  201. /*
  202. * If the task has used fpu the last 5 timeslices, just do a full
  203. * restore of the math state immediately to avoid the trap; the
  204. * chances of needing FPU soon are obviously high now
  205. */
  206. if (next->fpu_counter > 5)
  207. __fpu_state_restore();
  208. return prev;
  209. }
  210. asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
  211. unsigned long r6, unsigned long r7,
  212. struct pt_regs __regs)
  213. {
  214. #ifdef CONFIG_MMU
  215. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  216. return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
  217. #else
  218. /* fork almost works, enough to trick you into looking elsewhere :-( */
  219. return -EINVAL;
  220. #endif
  221. }
  222. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  223. unsigned long parent_tidptr,
  224. unsigned long child_tidptr,
  225. struct pt_regs __regs)
  226. {
  227. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  228. if (!newsp)
  229. newsp = regs->regs[15];
  230. return do_fork(clone_flags, newsp, regs, 0,
  231. (int __user *)parent_tidptr,
  232. (int __user *)child_tidptr);
  233. }
  234. /*
  235. * This is trivial, and on the face of it looks like it
  236. * could equally well be done in user mode.
  237. *
  238. * Not so, for quite unobvious reasons - register pressure.
  239. * In user mode vfork() cannot have a stack frame, and if
  240. * done by calling the "clone()" system call directly, you
  241. * do not have enough call-clobbered registers to hold all
  242. * the information you need.
  243. */
  244. asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
  245. unsigned long r6, unsigned long r7,
  246. struct pt_regs __regs)
  247. {
  248. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  249. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
  250. 0, NULL, NULL);
  251. }
  252. /*
  253. * sys_execve() executes a new program.
  254. */
  255. asmlinkage int sys_execve(const char __user *ufilename,
  256. const char __user *const __user *uargv,
  257. const char __user *const __user *uenvp,
  258. unsigned long r7, struct pt_regs __regs)
  259. {
  260. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  261. int error;
  262. char *filename;
  263. filename = getname(ufilename);
  264. error = PTR_ERR(filename);
  265. if (IS_ERR(filename))
  266. goto out;
  267. error = do_execve(filename, uargv, uenvp, regs);
  268. putname(filename);
  269. out:
  270. return error;
  271. }
  272. unsigned long get_wchan(struct task_struct *p)
  273. {
  274. unsigned long pc;
  275. if (!p || p == current || p->state == TASK_RUNNING)
  276. return 0;
  277. /*
  278. * The same comment as on the Alpha applies here, too ...
  279. */
  280. pc = thread_saved_pc(p);
  281. #ifdef CONFIG_FRAME_POINTER
  282. if (in_sched_functions(pc)) {
  283. unsigned long schedule_frame = (unsigned long)p->thread.sp;
  284. return ((unsigned long *)schedule_frame)[21];
  285. }
  286. #endif
  287. return pc;
  288. }