process_32.c 11 KB

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