process_32.c 11 KB

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