process.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* $Id: process.c,v 1.28 2004/05/05 16:54:23 lethal Exp $
  2. *
  3. * linux/arch/sh/kernel/process.c
  4. *
  5. * Copyright (C) 1995 Linus Torvalds
  6. *
  7. * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
  8. */
  9. /*
  10. * This file handles the architecture-dependent parts of process handling..
  11. */
  12. #include <linux/module.h>
  13. #include <linux/unistd.h>
  14. #include <linux/mm.h>
  15. #include <linux/elfcore.h>
  16. #include <linux/slab.h>
  17. #include <linux/a.out.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/platform.h>
  20. #include <linux/kallsyms.h>
  21. #include <asm/io.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/elf.h>
  25. #if defined(CONFIG_SH_HS7751RVOIP)
  26. #include <asm/hs7751rvoip/hs7751rvoip.h>
  27. #elif defined(CONFIG_SH_RTS7751R2D)
  28. #include <asm/rts7751r2d/rts7751r2d.h>
  29. #endif
  30. static int hlt_counter=0;
  31. int ubc_usercnt = 0;
  32. #define HARD_IDLE_TIMEOUT (HZ / 3)
  33. void disable_hlt(void)
  34. {
  35. hlt_counter++;
  36. }
  37. EXPORT_SYMBOL(disable_hlt);
  38. void enable_hlt(void)
  39. {
  40. hlt_counter--;
  41. }
  42. EXPORT_SYMBOL(enable_hlt);
  43. void cpu_idle(void)
  44. {
  45. /* endless idle loop with no priority at all */
  46. while (1) {
  47. if (hlt_counter) {
  48. while (!need_resched())
  49. cpu_relax();
  50. } else {
  51. while (!need_resched())
  52. cpu_sleep();
  53. }
  54. preempt_enable_no_resched();
  55. schedule();
  56. preempt_disable();
  57. }
  58. }
  59. void machine_restart(char * __unused)
  60. {
  61. #ifdef CONFIG_KEXEC
  62. struct kimage *image;
  63. image = xchg(&kexec_image, 0);
  64. if (image) {
  65. machine_shutdown();
  66. machine_kexec(image);
  67. }
  68. #endif
  69. /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
  70. asm volatile("ldc %0, sr\n\t"
  71. "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
  72. }
  73. void machine_halt(void)
  74. {
  75. #if defined(CONFIG_SH_HS7751RVOIP)
  76. unsigned short value;
  77. value = ctrl_inw(PA_OUTPORTR);
  78. ctrl_outw((value & 0xffdf), PA_OUTPORTR);
  79. #elif defined(CONFIG_SH_RTS7751R2D)
  80. ctrl_outw(0x0001, PA_POWOFF);
  81. #endif
  82. while (1)
  83. cpu_sleep();
  84. }
  85. void machine_power_off(void)
  86. {
  87. #if defined(CONFIG_SH_HS7751RVOIP)
  88. unsigned short value;
  89. value = ctrl_inw(PA_OUTPORTR);
  90. ctrl_outw((value & 0xffdf), PA_OUTPORTR);
  91. #elif defined(CONFIG_SH_RTS7751R2D)
  92. ctrl_outw(0x0001, PA_POWOFF);
  93. #endif
  94. }
  95. void show_regs(struct pt_regs * regs)
  96. {
  97. printk("\n");
  98. printk("Pid : %d, Comm: %20s\n", current->pid, current->comm);
  99. print_symbol("PC is at %s\n", regs->pc);
  100. printk("PC : %08lx SP : %08lx SR : %08lx ",
  101. regs->pc, regs->regs[15], regs->sr);
  102. #ifdef CONFIG_MMU
  103. printk("TEA : %08x ", ctrl_inl(MMU_TEA));
  104. #else
  105. printk(" ");
  106. #endif
  107. printk("%s\n", print_tainted());
  108. printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
  109. regs->regs[0],regs->regs[1],
  110. regs->regs[2],regs->regs[3]);
  111. printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
  112. regs->regs[4],regs->regs[5],
  113. regs->regs[6],regs->regs[7]);
  114. printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
  115. regs->regs[8],regs->regs[9],
  116. regs->regs[10],regs->regs[11]);
  117. printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
  118. regs->regs[12],regs->regs[13],
  119. regs->regs[14]);
  120. printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
  121. regs->mach, regs->macl, regs->gbr, regs->pr);
  122. /*
  123. * If we're in kernel mode, dump the stack too..
  124. */
  125. if (!user_mode(regs)) {
  126. extern void show_task(unsigned long *sp);
  127. unsigned long sp = regs->regs[15];
  128. show_task((unsigned long *)sp);
  129. }
  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. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  149. { /* Don't use this in BL=1(cli). Or else, CPU resets! */
  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, &regs, 0, NULL, NULL);
  158. }
  159. /*
  160. * Free current thread data structures etc..
  161. */
  162. void exit_thread(void)
  163. {
  164. if (current->thread.ubc_pc) {
  165. current->thread.ubc_pc = 0;
  166. ubc_usercnt -= 1;
  167. }
  168. }
  169. void flush_thread(void)
  170. {
  171. #if defined(CONFIG_SH_FPU)
  172. struct task_struct *tsk = current;
  173. /* Forget lazy FPU state */
  174. clear_fpu(tsk, task_pt_regs(tsk));
  175. clear_used_math();
  176. #endif
  177. }
  178. void release_thread(struct task_struct *dead_task)
  179. {
  180. /* do nothing */
  181. }
  182. /* Fill in the fpu structure for a core dump.. */
  183. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  184. {
  185. int fpvalid = 0;
  186. #if defined(CONFIG_SH_FPU)
  187. struct task_struct *tsk = current;
  188. fpvalid = !!tsk_used_math(tsk);
  189. if (fpvalid) {
  190. unlazy_fpu(tsk, regs);
  191. memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
  192. }
  193. #endif
  194. return fpvalid;
  195. }
  196. /*
  197. * Capture the user space registers if the task is not running (in user space)
  198. */
  199. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  200. {
  201. struct pt_regs ptregs;
  202. ptregs = *task_pt_regs(tsk);
  203. elf_core_copy_regs(regs, &ptregs);
  204. return 1;
  205. }
  206. int
  207. 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 pt_regs *childregs;
  225. #if defined(CONFIG_SH_FPU)
  226. struct task_struct *tsk = current;
  227. unlazy_fpu(tsk, regs);
  228. p->thread.fpu = tsk->thread.fpu;
  229. copy_to_stopped_child_used_math(p);
  230. #endif
  231. childregs = task_pt_regs(p);
  232. *childregs = *regs;
  233. if (user_mode(regs)) {
  234. childregs->regs[15] = usp;
  235. } else {
  236. childregs->regs[15] = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  237. }
  238. if (clone_flags & CLONE_SETTLS) {
  239. childregs->gbr = childregs->regs[0];
  240. }
  241. childregs->regs[0] = 0; /* Set return value for child */
  242. p->thread.sp = (unsigned long) childregs;
  243. p->thread.pc = (unsigned long) ret_from_fork;
  244. p->thread.ubc_pc = 0;
  245. return 0;
  246. }
  247. /* Tracing by user break controller. */
  248. static void
  249. ubc_set_tracing(int asid, unsigned long pc)
  250. {
  251. ctrl_outl(pc, UBC_BARA);
  252. /* We don't have any ASID settings for the SH-2! */
  253. if (cpu_data->type != CPU_SH7604)
  254. ctrl_outb(asid, UBC_BASRA);
  255. ctrl_outl(0, UBC_BAMRA);
  256. if (cpu_data->type == CPU_SH7729) {
  257. ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
  258. ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
  259. } else {
  260. ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
  261. ctrl_outw(BRCR_PCBA, UBC_BRCR);
  262. }
  263. }
  264. /*
  265. * switch_to(x,y) should switch tasks from x to y.
  266. *
  267. */
  268. struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
  269. {
  270. #if defined(CONFIG_SH_FPU)
  271. unlazy_fpu(prev, task_pt_regs(prev));
  272. #endif
  273. #ifdef CONFIG_PREEMPT
  274. {
  275. unsigned long flags;
  276. struct pt_regs *regs;
  277. local_irq_save(flags);
  278. regs = task_pt_regs(prev);
  279. if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
  280. int offset = (int)regs->regs[15];
  281. /* Reset stack pointer: clear critical region mark */
  282. regs->regs[15] = regs->regs[1];
  283. if (regs->pc < regs->regs[0])
  284. /* Go to rewind point */
  285. regs->pc = regs->regs[0] + offset;
  286. }
  287. local_irq_restore(flags);
  288. }
  289. #endif
  290. /*
  291. * Restore the kernel mode register
  292. * k7 (r7_bank1)
  293. */
  294. asm volatile("ldc %0, r7_bank"
  295. : /* no output */
  296. : "r" (task_thread_info(next)));
  297. #ifdef CONFIG_MMU
  298. /* If no tasks are using the UBC, we're done */
  299. if (ubc_usercnt == 0)
  300. /* If no tasks are using the UBC, we're done */;
  301. else if (next->thread.ubc_pc && next->mm) {
  302. ubc_set_tracing(next->mm->context & MMU_CONTEXT_ASID_MASK,
  303. next->thread.ubc_pc);
  304. } else {
  305. ctrl_outw(0, UBC_BBRA);
  306. ctrl_outw(0, UBC_BBRB);
  307. }
  308. #endif
  309. return prev;
  310. }
  311. asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
  312. unsigned long r6, unsigned long r7,
  313. struct pt_regs regs)
  314. {
  315. #ifdef CONFIG_MMU
  316. return do_fork(SIGCHLD, regs.regs[15], &regs, 0, NULL, NULL);
  317. #else
  318. /* fork almost works, enough to trick you into looking elsewhere :-( */
  319. return -EINVAL;
  320. #endif
  321. }
  322. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  323. unsigned long parent_tidptr,
  324. unsigned long child_tidptr,
  325. struct pt_regs regs)
  326. {
  327. if (!newsp)
  328. newsp = regs.regs[15];
  329. return do_fork(clone_flags, newsp, &regs, 0,
  330. (int __user *)parent_tidptr, (int __user *)child_tidptr);
  331. }
  332. /*
  333. * This is trivial, and on the face of it looks like it
  334. * could equally well be done in user mode.
  335. *
  336. * Not so, for quite unobvious reasons - register pressure.
  337. * In user mode vfork() cannot have a stack frame, and if
  338. * done by calling the "clone()" system call directly, you
  339. * do not have enough call-clobbered registers to hold all
  340. * the information you need.
  341. */
  342. asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
  343. unsigned long r6, unsigned long r7,
  344. struct pt_regs regs)
  345. {
  346. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.regs[15], &regs,
  347. 0, NULL, NULL);
  348. }
  349. /*
  350. * sys_execve() executes a new program.
  351. */
  352. asmlinkage int sys_execve(char *ufilename, char **uargv,
  353. char **uenvp, unsigned long r7,
  354. struct pt_regs regs)
  355. {
  356. int error;
  357. char *filename;
  358. filename = getname((char __user *)ufilename);
  359. error = PTR_ERR(filename);
  360. if (IS_ERR(filename))
  361. goto out;
  362. error = do_execve(filename,
  363. (char __user * __user *)uargv,
  364. (char __user * __user *)uenvp,
  365. &regs);
  366. if (error == 0) {
  367. task_lock(current);
  368. current->ptrace &= ~PT_DTRACE;
  369. task_unlock(current);
  370. }
  371. putname(filename);
  372. out:
  373. return error;
  374. }
  375. unsigned long get_wchan(struct task_struct *p)
  376. {
  377. unsigned long schedule_frame;
  378. unsigned long pc;
  379. if (!p || p == current || p->state == TASK_RUNNING)
  380. return 0;
  381. /*
  382. * The same comment as on the Alpha applies here, too ...
  383. */
  384. pc = thread_saved_pc(p);
  385. if (in_sched_functions(pc)) {
  386. schedule_frame = ((unsigned long *)(long)p->thread.sp)[1];
  387. return (unsigned long)((unsigned long *)schedule_frame)[1];
  388. }
  389. return pc;
  390. }
  391. asmlinkage void break_point_trap(unsigned long r4, unsigned long r5,
  392. unsigned long r6, unsigned long r7,
  393. struct pt_regs regs)
  394. {
  395. /* Clear tracing. */
  396. ctrl_outw(0, UBC_BBRA);
  397. ctrl_outw(0, UBC_BBRB);
  398. current->thread.ubc_pc = 0;
  399. ubc_usercnt -= 1;
  400. force_sig(SIGTRAP, current);
  401. }
  402. asmlinkage void break_point_trap_software(unsigned long r4, unsigned long r5,
  403. unsigned long r6, unsigned long r7,
  404. struct pt_regs regs)
  405. {
  406. regs.pc -= 2;
  407. force_sig(SIGTRAP, current);
  408. }