process.c 12 KB

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