process_32.c 8.9 KB

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