process.c 12 KB

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