process.c 12 KB

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