process.c 10 KB

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