process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * linux/arch/m68knommu/kernel/process.c
  3. *
  4. * Copyright (C) 1995 Hamish Macdonald
  5. *
  6. * 68060 fixes by Jesper Skov
  7. *
  8. * uClinux changes
  9. * Copyright (C) 2000-2002, David McCullough <davidm@snapgear.com>
  10. */
  11. /*
  12. * This file handles the architecture-dependent parts of process handling..
  13. */
  14. #include <linux/config.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/stddef.h>
  23. #include <linux/unistd.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/slab.h>
  26. #include <linux/user.h>
  27. #include <linux/a.out.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/reboot.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/system.h>
  32. #include <asm/traps.h>
  33. #include <asm/machdep.h>
  34. #include <asm/setup.h>
  35. #include <asm/pgtable.h>
  36. asmlinkage void ret_from_fork(void);
  37. /*
  38. * The idle loop on an m68knommu..
  39. */
  40. void default_idle(void)
  41. {
  42. local_irq_disable();
  43. while (!need_resched()) {
  44. /* This stop will re-enable interrupts */
  45. __asm__("stop #0x2000" : : : "cc");
  46. local_irq_disable();
  47. }
  48. local_irq_enable();
  49. }
  50. void (*idle)(void) = default_idle;
  51. /*
  52. * The idle thread. There's no useful work to be
  53. * done, so just try to conserve power and have a
  54. * low exit latency (ie sit in a loop waiting for
  55. * somebody to say that they'd like to reschedule)
  56. */
  57. void cpu_idle(void)
  58. {
  59. /* endless idle loop with no priority at all */
  60. while (1) {
  61. idle();
  62. preempt_enable_no_resched();
  63. schedule();
  64. preempt_disable();
  65. }
  66. }
  67. void machine_restart(char * __unused)
  68. {
  69. if (mach_reset)
  70. mach_reset();
  71. for (;;);
  72. }
  73. void machine_halt(void)
  74. {
  75. if (mach_halt)
  76. mach_halt();
  77. for (;;);
  78. }
  79. void machine_power_off(void)
  80. {
  81. if (mach_power_off)
  82. mach_power_off();
  83. for (;;);
  84. }
  85. void show_regs(struct pt_regs * regs)
  86. {
  87. printk(KERN_NOTICE "\n");
  88. printk(KERN_NOTICE "Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
  89. regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
  90. printk(KERN_NOTICE "ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
  91. regs->orig_d0, regs->d0, regs->a2, regs->a1);
  92. printk(KERN_NOTICE "A0: %08lx D5: %08lx D4: %08lx\n",
  93. regs->a0, regs->d5, regs->d4);
  94. printk(KERN_NOTICE "D3: %08lx D2: %08lx D1: %08lx\n",
  95. regs->d3, regs->d2, regs->d1);
  96. if (!(regs->sr & PS_S))
  97. printk(KERN_NOTICE "USP: %08lx\n", rdusp());
  98. }
  99. /*
  100. * Create a kernel thread
  101. */
  102. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  103. {
  104. int retval;
  105. long clone_arg = flags | CLONE_VM;
  106. mm_segment_t fs;
  107. fs = get_fs();
  108. set_fs(KERNEL_DS);
  109. __asm__ __volatile__ (
  110. "movel %%sp, %%d2\n\t"
  111. "movel %5, %%d1\n\t"
  112. "movel %1, %%d0\n\t"
  113. "trap #0\n\t"
  114. "cmpl %%sp, %%d2\n\t"
  115. "jeq 1f\n\t"
  116. "movel %3, %%sp@-\n\t"
  117. "jsr %4@\n\t"
  118. "movel %2, %%d0\n\t"
  119. "trap #0\n"
  120. "1:\n\t"
  121. "movel %%d0, %0\n"
  122. : "=d" (retval)
  123. : "i" (__NR_clone),
  124. "i" (__NR_exit),
  125. "a" (arg),
  126. "a" (fn),
  127. "a" (clone_arg)
  128. : "cc", "%d0", "%d1", "%d2");
  129. set_fs(fs);
  130. return retval;
  131. }
  132. void flush_thread(void)
  133. {
  134. #ifdef CONFIG_FPU
  135. unsigned long zero = 0;
  136. #endif
  137. set_fs(USER_DS);
  138. current->thread.fs = __USER_DS;
  139. #ifdef CONFIG_FPU
  140. if (!FPU_IS_EMU)
  141. asm volatile (".chip 68k/68881\n\t"
  142. "frestore %0@\n\t"
  143. ".chip 68k" : : "a" (&zero));
  144. #endif
  145. }
  146. /*
  147. * "m68k_fork()".. By the time we get here, the
  148. * non-volatile registers have also been saved on the
  149. * stack. We do some ugly pointer stuff here.. (see
  150. * also copy_thread)
  151. */
  152. asmlinkage int m68k_fork(struct pt_regs *regs)
  153. {
  154. /* fork almost works, enough to trick you into looking elsewhere :-( */
  155. return(-EINVAL);
  156. }
  157. asmlinkage int m68k_vfork(struct pt_regs *regs)
  158. {
  159. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  160. }
  161. asmlinkage int m68k_clone(struct pt_regs *regs)
  162. {
  163. unsigned long clone_flags;
  164. unsigned long newsp;
  165. /* syscall2 puts clone_flags in d1 and usp in d2 */
  166. clone_flags = regs->d1;
  167. newsp = regs->d2;
  168. if (!newsp)
  169. newsp = rdusp();
  170. return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
  171. }
  172. int copy_thread(int nr, unsigned long clone_flags,
  173. unsigned long usp, unsigned long topstk,
  174. struct task_struct * p, struct pt_regs * regs)
  175. {
  176. struct pt_regs * childregs;
  177. struct switch_stack * childstack, *stack;
  178. unsigned long stack_offset, *retp;
  179. stack_offset = THREAD_SIZE - sizeof(struct pt_regs);
  180. childregs = (struct pt_regs *) ((unsigned long) p->thread_info + stack_offset);
  181. *childregs = *regs;
  182. childregs->d0 = 0;
  183. retp = ((unsigned long *) regs);
  184. stack = ((struct switch_stack *) retp) - 1;
  185. childstack = ((struct switch_stack *) childregs) - 1;
  186. *childstack = *stack;
  187. childstack->retpc = (unsigned long)ret_from_fork;
  188. p->thread.usp = usp;
  189. p->thread.ksp = (unsigned long)childstack;
  190. /*
  191. * Must save the current SFC/DFC value, NOT the value when
  192. * the parent was last descheduled - RGH 10-08-96
  193. */
  194. p->thread.fs = get_fs().seg;
  195. #ifdef CONFIG_FPU
  196. if (!FPU_IS_EMU) {
  197. /* Copy the current fpu state */
  198. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  199. if (p->thread.fpstate[0])
  200. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  201. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  202. : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
  203. : "memory");
  204. /* Restore the state in case the fpu was busy */
  205. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  206. }
  207. #endif
  208. return 0;
  209. }
  210. /* Fill in the fpu structure for a core dump. */
  211. int dump_fpu(struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  212. {
  213. #ifdef CONFIG_FPU
  214. char fpustate[216];
  215. if (FPU_IS_EMU) {
  216. int i;
  217. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  218. memcpy(fpu->fpregs, current->thread.fp, 96);
  219. /* Convert internal fpu reg representation
  220. * into long double format
  221. */
  222. for (i = 0; i < 24; i += 3)
  223. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  224. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  225. return 1;
  226. }
  227. /* First dump the fpu context to avoid protocol violation. */
  228. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  229. if (!fpustate[0])
  230. return 0;
  231. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  232. :: "m" (fpu->fpcntl[0])
  233. : "memory");
  234. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  235. :: "m" (fpu->fpregs[0])
  236. : "memory");
  237. #endif
  238. return 1;
  239. }
  240. /*
  241. * fill in the user structure for a core dump..
  242. */
  243. void dump_thread(struct pt_regs * regs, struct user * dump)
  244. {
  245. struct switch_stack *sw;
  246. /* changed the size calculations - should hopefully work better. lbt */
  247. dump->magic = CMAGIC;
  248. dump->start_code = 0;
  249. dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
  250. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  251. dump->u_dsize = ((unsigned long) (current->mm->brk +
  252. (PAGE_SIZE-1))) >> PAGE_SHIFT;
  253. dump->u_dsize -= dump->u_tsize;
  254. dump->u_ssize = 0;
  255. if (dump->start_stack < TASK_SIZE)
  256. dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
  257. dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
  258. sw = ((struct switch_stack *)regs) - 1;
  259. dump->regs.d1 = regs->d1;
  260. dump->regs.d2 = regs->d2;
  261. dump->regs.d3 = regs->d3;
  262. dump->regs.d4 = regs->d4;
  263. dump->regs.d5 = regs->d5;
  264. dump->regs.d6 = sw->d6;
  265. dump->regs.d7 = sw->d7;
  266. dump->regs.a0 = regs->a0;
  267. dump->regs.a1 = regs->a1;
  268. dump->regs.a2 = regs->a2;
  269. dump->regs.a3 = sw->a3;
  270. dump->regs.a4 = sw->a4;
  271. dump->regs.a5 = sw->a5;
  272. dump->regs.a6 = sw->a6;
  273. dump->regs.d0 = regs->d0;
  274. dump->regs.orig_d0 = regs->orig_d0;
  275. dump->regs.stkadj = regs->stkadj;
  276. dump->regs.sr = regs->sr;
  277. dump->regs.pc = regs->pc;
  278. dump->regs.fmtvec = (regs->format << 12) | regs->vector;
  279. /* dump floating point stuff */
  280. dump->u_fpvalid = dump_fpu (regs, &dump->m68kfp);
  281. }
  282. /*
  283. * Generic dumping code. Used for panic and debug.
  284. */
  285. void dump(struct pt_regs *fp)
  286. {
  287. unsigned long *sp;
  288. unsigned char *tp;
  289. int i;
  290. printk(KERN_EMERG "\nCURRENT PROCESS:\n\n");
  291. printk(KERN_EMERG "COMM=%s PID=%d\n", current->comm, current->pid);
  292. if (current->mm) {
  293. printk(KERN_EMERG "TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
  294. (int) current->mm->start_code,
  295. (int) current->mm->end_code,
  296. (int) current->mm->start_data,
  297. (int) current->mm->end_data,
  298. (int) current->mm->end_data,
  299. (int) current->mm->brk);
  300. printk(KERN_EMERG "USER-STACK=%08x KERNEL-STACK=%08x\n\n",
  301. (int) current->mm->start_stack,
  302. (int)(((unsigned long) current) + THREAD_SIZE));
  303. }
  304. printk(KERN_EMERG "PC: %08lx\n", fp->pc);
  305. printk(KERN_EMERG "SR: %08lx SP: %08lx\n", (long) fp->sr, (long) fp);
  306. printk(KERN_EMERG "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
  307. fp->d0, fp->d1, fp->d2, fp->d3);
  308. printk(KERN_EMERG "d4: %08lx d5: %08lx a0: %08lx a1: %08lx\n",
  309. fp->d4, fp->d5, fp->a0, fp->a1);
  310. printk(KERN_EMERG "\nUSP: %08x TRAPFRAME: %08x\n", (unsigned int) rdusp(),
  311. (unsigned int) fp);
  312. printk(KERN_EMERG "\nCODE:");
  313. tp = ((unsigned char *) fp->pc) - 0x20;
  314. for (sp = (unsigned long *) tp, i = 0; (i < 0x40); i += 4) {
  315. if ((i % 0x10) == 0)
  316. printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
  317. printk(KERN_EMERG "%08x ", (int) *sp++);
  318. }
  319. printk(KERN_EMERG "\n");
  320. printk(KERN_EMERG "\nKERNEL STACK:");
  321. tp = ((unsigned char *) fp) - 0x40;
  322. for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
  323. if ((i % 0x10) == 0)
  324. printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
  325. printk(KERN_EMERG "%08x ", (int) *sp++);
  326. }
  327. printk(KERN_EMERG "\n");
  328. printk(KERN_EMERG "\n");
  329. printk(KERN_EMERG "\nUSER STACK:");
  330. tp = (unsigned char *) (rdusp() - 0x10);
  331. for (sp = (unsigned long *) tp, i = 0; (i < 0x80); i += 4) {
  332. if ((i % 0x10) == 0)
  333. printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
  334. printk(KERN_EMERG "%08x ", (int) *sp++);
  335. }
  336. printk(KERN_EMERG "\n\n");
  337. }
  338. /*
  339. * sys_execve() executes a new program.
  340. */
  341. asmlinkage int sys_execve(char *name, char **argv, char **envp)
  342. {
  343. int error;
  344. char * filename;
  345. struct pt_regs *regs = (struct pt_regs *) &name;
  346. lock_kernel();
  347. filename = getname(name);
  348. error = PTR_ERR(filename);
  349. if (IS_ERR(filename))
  350. goto out;
  351. error = do_execve(filename, argv, envp, regs);
  352. putname(filename);
  353. out:
  354. unlock_kernel();
  355. return error;
  356. }
  357. unsigned long get_wchan(struct task_struct *p)
  358. {
  359. unsigned long fp, pc;
  360. unsigned long stack_page;
  361. int count = 0;
  362. if (!p || p == current || p->state == TASK_RUNNING)
  363. return 0;
  364. stack_page = (unsigned long)p;
  365. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  366. do {
  367. if (fp < stack_page+sizeof(struct thread_info) ||
  368. fp >= 8184+stack_page)
  369. return 0;
  370. pc = ((unsigned long *)fp)[1];
  371. if (!in_sched_functions(pc))
  372. return pc;
  373. fp = *(unsigned long *) fp;
  374. } while (count++ < 16);
  375. return 0;
  376. }
  377. /*
  378. * Return saved PC of a blocked thread.
  379. */
  380. unsigned long thread_saved_pc(struct task_struct *tsk)
  381. {
  382. struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
  383. /* Check whether the thread is blocked in resume() */
  384. if (in_sched_functions(sw->retpc))
  385. return ((unsigned long *)sw->a6)[1];
  386. else
  387. return sw->retpc;
  388. }