process_32.c 10 KB

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