process_32.c 10 KB

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