process.c 12 KB

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