process_32.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. regs->pr = 0;
  94. regs->sr = SR_FD;
  95. regs->pc = new_pc;
  96. regs->regs[15] = new_sp;
  97. free_thread_xstate(current);
  98. }
  99. EXPORT_SYMBOL(start_thread);
  100. /*
  101. * Free current thread data structures etc..
  102. */
  103. void exit_thread(void)
  104. {
  105. }
  106. void flush_thread(void)
  107. {
  108. struct task_struct *tsk = current;
  109. flush_ptrace_hw_breakpoint(tsk);
  110. #if defined(CONFIG_SH_FPU)
  111. /* Forget lazy FPU state */
  112. clear_fpu(tsk, task_pt_regs(tsk));
  113. clear_used_math();
  114. #endif
  115. }
  116. void release_thread(struct task_struct *dead_task)
  117. {
  118. /* do nothing */
  119. }
  120. /* Fill in the fpu structure for a core dump.. */
  121. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  122. {
  123. int fpvalid = 0;
  124. #if defined(CONFIG_SH_FPU)
  125. struct task_struct *tsk = current;
  126. fpvalid = !!tsk_used_math(tsk);
  127. if (fpvalid)
  128. fpvalid = !fpregs_get(tsk, NULL, 0,
  129. sizeof(struct user_fpu_struct),
  130. fpu, NULL);
  131. #endif
  132. return fpvalid;
  133. }
  134. EXPORT_SYMBOL(dump_fpu);
  135. /*
  136. * This gets called before we allocate a new thread and copy
  137. * the current task into it.
  138. */
  139. void prepare_to_copy(struct task_struct *tsk)
  140. {
  141. unlazy_fpu(tsk, task_pt_regs(tsk));
  142. }
  143. asmlinkage void ret_from_fork(void);
  144. int copy_thread(unsigned long clone_flags, unsigned long usp,
  145. unsigned long unused,
  146. struct task_struct *p, struct pt_regs *regs)
  147. {
  148. struct thread_info *ti = task_thread_info(p);
  149. struct pt_regs *childregs;
  150. #if defined(CONFIG_SH_DSP)
  151. struct task_struct *tsk = current;
  152. if (is_dsp_enabled(tsk)) {
  153. /* We can use the __save_dsp or just copy the struct:
  154. * __save_dsp(p);
  155. * p->thread.dsp_status.status |= SR_DSP
  156. */
  157. p->thread.dsp_status = tsk->thread.dsp_status;
  158. }
  159. #endif
  160. childregs = task_pt_regs(p);
  161. *childregs = *regs;
  162. if (user_mode(regs)) {
  163. childregs->regs[15] = usp;
  164. ti->addr_limit = USER_DS;
  165. } else {
  166. childregs->regs[15] = (unsigned long)childregs;
  167. ti->addr_limit = KERNEL_DS;
  168. ti->status &= ~TS_USEDFPU;
  169. p->fpu_counter = 0;
  170. }
  171. if (clone_flags & CLONE_SETTLS)
  172. childregs->gbr = childregs->regs[0];
  173. childregs->regs[0] = 0; /* Set return value for child */
  174. p->thread.sp = (unsigned long) childregs;
  175. p->thread.pc = (unsigned long) ret_from_fork;
  176. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  177. return 0;
  178. }
  179. /*
  180. * switch_to(x,y) should switch tasks from x to y.
  181. *
  182. */
  183. __notrace_funcgraph struct task_struct *
  184. __switch_to(struct task_struct *prev, struct task_struct *next)
  185. {
  186. struct thread_struct *next_t = &next->thread;
  187. unlazy_fpu(prev, task_pt_regs(prev));
  188. /* we're going to use this soon, after a few expensive things */
  189. if (next->fpu_counter > 5)
  190. prefetch(next_t->xstate);
  191. #ifdef CONFIG_MMU
  192. /*
  193. * Restore the kernel mode register
  194. * k7 (r7_bank1)
  195. */
  196. asm volatile("ldc %0, r7_bank"
  197. : /* no output */
  198. : "r" (task_thread_info(next)));
  199. #endif
  200. /*
  201. * If the task has used fpu the last 5 timeslices, just do a full
  202. * restore of the math state immediately to avoid the trap; the
  203. * chances of needing FPU soon are obviously high now
  204. */
  205. if (next->fpu_counter > 5)
  206. __fpu_state_restore();
  207. return prev;
  208. }
  209. asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
  210. unsigned long r6, unsigned long r7,
  211. struct pt_regs __regs)
  212. {
  213. #ifdef CONFIG_MMU
  214. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  215. return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
  216. #else
  217. /* fork almost works, enough to trick you into looking elsewhere :-( */
  218. return -EINVAL;
  219. #endif
  220. }
  221. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  222. unsigned long parent_tidptr,
  223. unsigned long child_tidptr,
  224. struct pt_regs __regs)
  225. {
  226. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  227. if (!newsp)
  228. newsp = regs->regs[15];
  229. return do_fork(clone_flags, newsp, regs, 0,
  230. (int __user *)parent_tidptr,
  231. (int __user *)child_tidptr);
  232. }
  233. /*
  234. * This is trivial, and on the face of it looks like it
  235. * could equally well be done in user mode.
  236. *
  237. * Not so, for quite unobvious reasons - register pressure.
  238. * In user mode vfork() cannot have a stack frame, and if
  239. * done by calling the "clone()" system call directly, you
  240. * do not have enough call-clobbered registers to hold all
  241. * the information you need.
  242. */
  243. asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
  244. unsigned long r6, unsigned long r7,
  245. struct pt_regs __regs)
  246. {
  247. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  248. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
  249. 0, NULL, NULL);
  250. }
  251. /*
  252. * sys_execve() executes a new program.
  253. */
  254. asmlinkage int sys_execve(const char __user *ufilename,
  255. const char __user *const __user *uargv,
  256. const char __user *const __user *uenvp,
  257. unsigned long r7, struct pt_regs __regs)
  258. {
  259. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  260. int error;
  261. char *filename;
  262. filename = getname(ufilename);
  263. error = PTR_ERR(filename);
  264. if (IS_ERR(filename))
  265. goto out;
  266. error = do_execve(filename, uargv, uenvp, regs);
  267. putname(filename);
  268. out:
  269. return error;
  270. }
  271. unsigned long get_wchan(struct task_struct *p)
  272. {
  273. unsigned long pc;
  274. if (!p || p == current || p->state == TASK_RUNNING)
  275. return 0;
  276. /*
  277. * The same comment as on the Alpha applies here, too ...
  278. */
  279. pc = thread_saved_pc(p);
  280. #ifdef CONFIG_FRAME_POINTER
  281. if (in_sched_functions(pc)) {
  282. unsigned long schedule_frame = (unsigned long)p->thread.sp;
  283. return ((unsigned long *)schedule_frame)[21];
  284. }
  285. #endif
  286. return pc;
  287. }