process.c 11 KB

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