process_32.c 9.8 KB

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