process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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)task_stack_page(p) +
  213. THREAD_SIZE;
  214. ti->addr_limit = KERNEL_DS;
  215. }
  216. if (clone_flags & CLONE_SETTLS)
  217. childregs->gbr = childregs->regs[0];
  218. childregs->regs[0] = 0; /* Set return value for child */
  219. p->thread.sp = (unsigned long) childregs;
  220. p->thread.pc = (unsigned long) ret_from_fork;
  221. p->thread.ubc_pc = 0;
  222. return 0;
  223. }
  224. /* Tracing by user break controller. */
  225. static void ubc_set_tracing(int asid, unsigned long pc)
  226. {
  227. #if defined(CONFIG_CPU_SH4A)
  228. unsigned long val;
  229. val = (UBC_CBR_ID_INST | UBC_CBR_RW_READ | UBC_CBR_CE);
  230. val |= (UBC_CBR_AIE | UBC_CBR_AIV_SET(asid));
  231. ctrl_outl(val, UBC_CBR0);
  232. ctrl_outl(pc, UBC_CAR0);
  233. ctrl_outl(0x0, UBC_CAMR0);
  234. ctrl_outl(0x0, UBC_CBCR);
  235. val = (UBC_CRR_RES | UBC_CRR_PCB | UBC_CRR_BIE);
  236. ctrl_outl(val, UBC_CRR0);
  237. /* Read UBC register that we wrote last, for checking update */
  238. val = ctrl_inl(UBC_CRR0);
  239. #else /* CONFIG_CPU_SH4A */
  240. ctrl_outl(pc, UBC_BARA);
  241. #ifdef CONFIG_MMU
  242. /* We don't have any ASID settings for the SH-2! */
  243. if (current_cpu_data.type != CPU_SH7604)
  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. ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
  250. ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
  251. } else {
  252. ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
  253. ctrl_outw(BRCR_PCBA, UBC_BRCR);
  254. }
  255. #endif /* CONFIG_CPU_SH4A */
  256. }
  257. /*
  258. * switch_to(x,y) should switch tasks from x to y.
  259. *
  260. */
  261. struct task_struct *__switch_to(struct task_struct *prev,
  262. struct task_struct *next)
  263. {
  264. #if defined(CONFIG_SH_FPU)
  265. unlazy_fpu(prev, task_pt_regs(prev));
  266. #endif
  267. #ifdef CONFIG_PREEMPT
  268. {
  269. unsigned long flags;
  270. struct pt_regs *regs;
  271. local_irq_save(flags);
  272. regs = task_pt_regs(prev);
  273. if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
  274. int offset = (int)regs->regs[15];
  275. /* Reset stack pointer: clear critical region mark */
  276. regs->regs[15] = regs->regs[1];
  277. if (regs->pc < regs->regs[0])
  278. /* Go to rewind point */
  279. regs->pc = regs->regs[0] + offset;
  280. }
  281. local_irq_restore(flags);
  282. }
  283. #endif
  284. #ifdef CONFIG_MMU
  285. /*
  286. * Restore the kernel mode register
  287. * k7 (r7_bank1)
  288. */
  289. asm volatile("ldc %0, r7_bank"
  290. : /* no output */
  291. : "r" (task_thread_info(next)));
  292. #endif
  293. /* If no tasks are using the UBC, we're done */
  294. if (ubc_usercnt == 0)
  295. /* If no tasks are using the UBC, we're done */;
  296. else if (next->thread.ubc_pc && next->mm) {
  297. int asid = 0;
  298. #ifdef CONFIG_MMU
  299. asid |= cpu_asid(smp_processor_id(), next->mm);
  300. #endif
  301. ubc_set_tracing(asid, next->thread.ubc_pc);
  302. } else {
  303. #if defined(CONFIG_CPU_SH4A)
  304. ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
  305. ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
  306. #else
  307. ctrl_outw(0, UBC_BBRA);
  308. ctrl_outw(0, UBC_BBRB);
  309. #endif
  310. }
  311. return prev;
  312. }
  313. asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
  314. unsigned long r6, unsigned long r7,
  315. struct pt_regs __regs)
  316. {
  317. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  318. #ifdef CONFIG_MMU
  319. return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
  320. #else
  321. /* fork almost works, enough to trick you into looking elsewhere :-( */
  322. return -EINVAL;
  323. #endif
  324. }
  325. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  326. unsigned long parent_tidptr,
  327. unsigned long child_tidptr,
  328. struct pt_regs __regs)
  329. {
  330. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  331. if (!newsp)
  332. newsp = regs->regs[15];
  333. return do_fork(clone_flags, newsp, regs, 0,
  334. (int __user *)parent_tidptr,
  335. (int __user *)child_tidptr);
  336. }
  337. /*
  338. * This is trivial, and on the face of it looks like it
  339. * could equally well be done in user mode.
  340. *
  341. * Not so, for quite unobvious reasons - register pressure.
  342. * In user mode vfork() cannot have a stack frame, and if
  343. * done by calling the "clone()" system call directly, you
  344. * do not have enough call-clobbered registers to hold all
  345. * the information you need.
  346. */
  347. asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
  348. unsigned long r6, unsigned long r7,
  349. struct pt_regs __regs)
  350. {
  351. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  352. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
  353. 0, NULL, NULL);
  354. }
  355. /*
  356. * sys_execve() executes a new program.
  357. */
  358. asmlinkage int sys_execve(char *ufilename, char **uargv,
  359. char **uenvp, unsigned long r7,
  360. struct pt_regs __regs)
  361. {
  362. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  363. int error;
  364. char *filename;
  365. filename = getname((char __user *)ufilename);
  366. error = PTR_ERR(filename);
  367. if (IS_ERR(filename))
  368. goto out;
  369. error = do_execve(filename,
  370. (char __user * __user *)uargv,
  371. (char __user * __user *)uenvp,
  372. regs);
  373. if (error == 0) {
  374. task_lock(current);
  375. current->ptrace &= ~PT_DTRACE;
  376. task_unlock(current);
  377. }
  378. putname(filename);
  379. out:
  380. return error;
  381. }
  382. unsigned long get_wchan(struct task_struct *p)
  383. {
  384. unsigned long schedule_frame;
  385. unsigned long pc;
  386. if (!p || p == current || p->state == TASK_RUNNING)
  387. return 0;
  388. /*
  389. * The same comment as on the Alpha applies here, too ...
  390. */
  391. pc = thread_saved_pc(p);
  392. if (in_sched_functions(pc)) {
  393. schedule_frame = (unsigned long)p->thread.sp;
  394. return ((unsigned long *)schedule_frame)[21];
  395. }
  396. return pc;
  397. }
  398. asmlinkage void break_point_trap(void)
  399. {
  400. /* Clear tracing. */
  401. #if defined(CONFIG_CPU_SH4A)
  402. ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
  403. ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
  404. #else
  405. ctrl_outw(0, UBC_BBRA);
  406. ctrl_outw(0, UBC_BBRB);
  407. #endif
  408. current->thread.ubc_pc = 0;
  409. ubc_usercnt -= 1;
  410. force_sig(SIGTRAP, current);
  411. }
  412. /*
  413. * Generic trap handler.
  414. */
  415. asmlinkage void debug_trap_handler(unsigned long r4, unsigned long r5,
  416. unsigned long r6, unsigned long r7,
  417. struct pt_regs __regs)
  418. {
  419. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  420. /* Rewind */
  421. regs->pc -= 2;
  422. force_sig(SIGTRAP, current);
  423. }
  424. /*
  425. * Special handler for BUG() traps.
  426. */
  427. asmlinkage void bug_trap_handler(unsigned long r4, unsigned long r5,
  428. unsigned long r6, unsigned long r7,
  429. struct pt_regs __regs)
  430. {
  431. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  432. /* Rewind */
  433. regs->pc -= 2;
  434. #ifdef CONFIG_BUG
  435. if (__kernel_text_address(instruction_pointer(regs))) {
  436. u16 insn = *(u16 *)instruction_pointer(regs);
  437. if (insn == TRAPA_BUG_OPCODE)
  438. handle_BUG(regs);
  439. }
  440. #endif
  441. force_sig(SIGTRAP, current);
  442. }