process.c 12 KB

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