process.c 10 KB

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