process_32.c 10 KB

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