process_32.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. /*
  90. * This is the mechanism for creating a new kernel thread.
  91. *
  92. */
  93. extern void kernel_thread_helper(void);
  94. __asm__(".align 5\n"
  95. "kernel_thread_helper:\n\t"
  96. "jsr @r5\n\t"
  97. " nop\n\t"
  98. "mov.l 1f, r1\n\t"
  99. "jsr @r1\n\t"
  100. " mov r0, r4\n\t"
  101. ".align 2\n\t"
  102. "1:.long do_exit");
  103. /* Don't use this in BL=1(cli). Or else, CPU resets! */
  104. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  105. {
  106. struct pt_regs regs;
  107. int pid;
  108. memset(&regs, 0, sizeof(regs));
  109. regs.regs[4] = (unsigned long)arg;
  110. regs.regs[5] = (unsigned long)fn;
  111. regs.pc = (unsigned long)kernel_thread_helper;
  112. regs.sr = (1 << 30);
  113. /* Ok, create the new process.. */
  114. pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
  115. &regs, 0, NULL, NULL);
  116. trace_mark(kernel_arch_kthread_create, "pid %d fn %p", pid, fn);
  117. return pid;
  118. }
  119. /*
  120. * Free current thread data structures etc..
  121. */
  122. void exit_thread(void)
  123. {
  124. if (current->thread.ubc_pc) {
  125. current->thread.ubc_pc = 0;
  126. ubc_usercnt -= 1;
  127. }
  128. }
  129. void flush_thread(void)
  130. {
  131. #if defined(CONFIG_SH_FPU)
  132. struct task_struct *tsk = current;
  133. /* Forget lazy FPU state */
  134. clear_fpu(tsk, task_pt_regs(tsk));
  135. clear_used_math();
  136. #endif
  137. }
  138. void release_thread(struct task_struct *dead_task)
  139. {
  140. /* do nothing */
  141. }
  142. /* Fill in the fpu structure for a core dump.. */
  143. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  144. {
  145. int fpvalid = 0;
  146. #if defined(CONFIG_SH_FPU)
  147. struct task_struct *tsk = current;
  148. fpvalid = !!tsk_used_math(tsk);
  149. if (fpvalid)
  150. fpvalid = !fpregs_get(tsk, NULL, 0,
  151. sizeof(struct user_fpu_struct),
  152. fpu, NULL);
  153. #endif
  154. return fpvalid;
  155. }
  156. asmlinkage void ret_from_fork(void);
  157. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  158. unsigned long unused,
  159. struct task_struct *p, struct pt_regs *regs)
  160. {
  161. struct thread_info *ti = task_thread_info(p);
  162. struct pt_regs *childregs;
  163. #if defined(CONFIG_SH_FPU)
  164. struct task_struct *tsk = current;
  165. unlazy_fpu(tsk, regs);
  166. p->thread.fpu = tsk->thread.fpu;
  167. copy_to_stopped_child_used_math(p);
  168. #endif
  169. childregs = task_pt_regs(p);
  170. *childregs = *regs;
  171. if (user_mode(regs)) {
  172. childregs->regs[15] = usp;
  173. ti->addr_limit = USER_DS;
  174. } else {
  175. childregs->regs[15] = (unsigned long)childregs;
  176. ti->addr_limit = KERNEL_DS;
  177. }
  178. if (clone_flags & CLONE_SETTLS)
  179. childregs->gbr = childregs->regs[0];
  180. childregs->regs[0] = 0; /* Set return value for child */
  181. p->thread.sp = (unsigned long) childregs;
  182. p->thread.pc = (unsigned long) ret_from_fork;
  183. p->thread.ubc_pc = 0;
  184. return 0;
  185. }
  186. /* Tracing by user break controller. */
  187. static void ubc_set_tracing(int asid, unsigned long pc)
  188. {
  189. #if defined(CONFIG_CPU_SH4A)
  190. unsigned long val;
  191. val = (UBC_CBR_ID_INST | UBC_CBR_RW_READ | UBC_CBR_CE);
  192. val |= (UBC_CBR_AIE | UBC_CBR_AIV_SET(asid));
  193. ctrl_outl(val, UBC_CBR0);
  194. ctrl_outl(pc, UBC_CAR0);
  195. ctrl_outl(0x0, UBC_CAMR0);
  196. ctrl_outl(0x0, UBC_CBCR);
  197. val = (UBC_CRR_RES | UBC_CRR_PCB | UBC_CRR_BIE);
  198. ctrl_outl(val, UBC_CRR0);
  199. /* Read UBC register that we wrote last, for checking update */
  200. val = ctrl_inl(UBC_CRR0);
  201. #else /* CONFIG_CPU_SH4A */
  202. ctrl_outl(pc, UBC_BARA);
  203. #ifdef CONFIG_MMU
  204. ctrl_outb(asid, UBC_BASRA);
  205. #endif
  206. ctrl_outl(0, UBC_BAMRA);
  207. if (current_cpu_data.type == CPU_SH7729 ||
  208. current_cpu_data.type == CPU_SH7710 ||
  209. current_cpu_data.type == CPU_SH7712) {
  210. ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
  211. ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
  212. } else {
  213. ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
  214. ctrl_outw(BRCR_PCBA, UBC_BRCR);
  215. }
  216. #endif /* CONFIG_CPU_SH4A */
  217. }
  218. /*
  219. * switch_to(x,y) should switch tasks from x to y.
  220. *
  221. */
  222. struct task_struct *__switch_to(struct task_struct *prev,
  223. struct task_struct *next)
  224. {
  225. #if defined(CONFIG_SH_FPU)
  226. unlazy_fpu(prev, task_pt_regs(prev));
  227. #endif
  228. #ifdef CONFIG_MMU
  229. /*
  230. * Restore the kernel mode register
  231. * k7 (r7_bank1)
  232. */
  233. asm volatile("ldc %0, r7_bank"
  234. : /* no output */
  235. : "r" (task_thread_info(next)));
  236. #endif
  237. /* If no tasks are using the UBC, we're done */
  238. if (ubc_usercnt == 0)
  239. /* If no tasks are using the UBC, we're done */;
  240. else if (next->thread.ubc_pc && next->mm) {
  241. int asid = 0;
  242. #ifdef CONFIG_MMU
  243. asid |= cpu_asid(smp_processor_id(), next->mm);
  244. #endif
  245. ubc_set_tracing(asid, next->thread.ubc_pc);
  246. } else {
  247. #if defined(CONFIG_CPU_SH4A)
  248. ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
  249. ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
  250. #else
  251. ctrl_outw(0, UBC_BBRA);
  252. ctrl_outw(0, UBC_BBRB);
  253. #endif
  254. }
  255. return prev;
  256. }
  257. asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
  258. unsigned long r6, unsigned long r7,
  259. struct pt_regs __regs)
  260. {
  261. #ifdef CONFIG_MMU
  262. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  263. return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
  264. #else
  265. /* fork almost works, enough to trick you into looking elsewhere :-( */
  266. return -EINVAL;
  267. #endif
  268. }
  269. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  270. unsigned long parent_tidptr,
  271. unsigned long child_tidptr,
  272. struct pt_regs __regs)
  273. {
  274. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  275. if (!newsp)
  276. newsp = regs->regs[15];
  277. return do_fork(clone_flags, newsp, regs, 0,
  278. (int __user *)parent_tidptr,
  279. (int __user *)child_tidptr);
  280. }
  281. /*
  282. * This is trivial, and on the face of it looks like it
  283. * could equally well be done in user mode.
  284. *
  285. * Not so, for quite unobvious reasons - register pressure.
  286. * In user mode vfork() cannot have a stack frame, and if
  287. * done by calling the "clone()" system call directly, you
  288. * do not have enough call-clobbered registers to hold all
  289. * the information you need.
  290. */
  291. asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
  292. unsigned long r6, unsigned long r7,
  293. struct pt_regs __regs)
  294. {
  295. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  296. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
  297. 0, NULL, NULL);
  298. }
  299. /*
  300. * sys_execve() executes a new program.
  301. */
  302. asmlinkage int sys_execve(char __user *ufilename, char __user * __user *uargv,
  303. char __user * __user *uenvp, unsigned long r7,
  304. struct pt_regs __regs)
  305. {
  306. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  307. int error;
  308. char *filename;
  309. filename = getname(ufilename);
  310. error = PTR_ERR(filename);
  311. if (IS_ERR(filename))
  312. goto out;
  313. error = do_execve(filename, uargv, uenvp, regs);
  314. if (error == 0) {
  315. task_lock(current);
  316. current->ptrace &= ~PT_DTRACE;
  317. task_unlock(current);
  318. }
  319. putname(filename);
  320. out:
  321. return error;
  322. }
  323. unsigned long get_wchan(struct task_struct *p)
  324. {
  325. unsigned long pc;
  326. if (!p || p == current || p->state == TASK_RUNNING)
  327. return 0;
  328. /*
  329. * The same comment as on the Alpha applies here, too ...
  330. */
  331. pc = thread_saved_pc(p);
  332. #ifdef CONFIG_FRAME_POINTER
  333. if (in_sched_functions(pc)) {
  334. unsigned long schedule_frame = (unsigned long)p->thread.sp;
  335. return ((unsigned long *)schedule_frame)[21];
  336. }
  337. #endif
  338. return pc;
  339. }
  340. asmlinkage void break_point_trap(void)
  341. {
  342. /* Clear tracing. */
  343. #if defined(CONFIG_CPU_SH4A)
  344. ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
  345. ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
  346. #else
  347. ctrl_outw(0, UBC_BBRA);
  348. ctrl_outw(0, UBC_BBRB);
  349. #endif
  350. current->thread.ubc_pc = 0;
  351. ubc_usercnt -= 1;
  352. force_sig(SIGTRAP, current);
  353. }