process.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 *retp;
  179. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  180. *childregs = *regs;
  181. childregs->d0 = 0;
  182. retp = ((unsigned long *) regs);
  183. stack = ((struct switch_stack *) retp) - 1;
  184. childstack = ((struct switch_stack *) childregs) - 1;
  185. *childstack = *stack;
  186. childstack->retpc = (unsigned long)ret_from_fork;
  187. p->thread.usp = usp;
  188. p->thread.ksp = (unsigned long)childstack;
  189. /*
  190. * Must save the current SFC/DFC value, NOT the value when
  191. * the parent was last descheduled - RGH 10-08-96
  192. */
  193. p->thread.fs = get_fs().seg;
  194. #ifdef CONFIG_FPU
  195. if (!FPU_IS_EMU) {
  196. /* Copy the current fpu state */
  197. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  198. if (p->thread.fpstate[0])
  199. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  200. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  201. : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
  202. : "memory");
  203. /* Restore the state in case the fpu was busy */
  204. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  205. }
  206. #endif
  207. return 0;
  208. }
  209. /* Fill in the fpu structure for a core dump. */
  210. int dump_fpu(struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  211. {
  212. #ifdef CONFIG_FPU
  213. char fpustate[216];
  214. if (FPU_IS_EMU) {
  215. int i;
  216. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  217. memcpy(fpu->fpregs, current->thread.fp, 96);
  218. /* Convert internal fpu reg representation
  219. * into long double format
  220. */
  221. for (i = 0; i < 24; i += 3)
  222. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  223. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  224. return 1;
  225. }
  226. /* First dump the fpu context to avoid protocol violation. */
  227. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  228. if (!fpustate[0])
  229. return 0;
  230. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  231. :: "m" (fpu->fpcntl[0])
  232. : "memory");
  233. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  234. :: "m" (fpu->fpregs[0])
  235. : "memory");
  236. #endif
  237. return 1;
  238. }
  239. /*
  240. * Generic dumping code. Used for panic and debug.
  241. */
  242. void dump(struct pt_regs *fp)
  243. {
  244. unsigned long *sp;
  245. unsigned char *tp;
  246. int i;
  247. printk(KERN_EMERG "\nCURRENT PROCESS:\n\n");
  248. printk(KERN_EMERG "COMM=%s PID=%d\n", current->comm, current->pid);
  249. if (current->mm) {
  250. printk(KERN_EMERG "TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
  251. (int) current->mm->start_code,
  252. (int) current->mm->end_code,
  253. (int) current->mm->start_data,
  254. (int) current->mm->end_data,
  255. (int) current->mm->end_data,
  256. (int) current->mm->brk);
  257. printk(KERN_EMERG "USER-STACK=%08x KERNEL-STACK=%08x\n\n",
  258. (int) current->mm->start_stack,
  259. (int)(((unsigned long) current) + THREAD_SIZE));
  260. }
  261. printk(KERN_EMERG "PC: %08lx\n", fp->pc);
  262. printk(KERN_EMERG "SR: %08lx SP: %08lx\n", (long) fp->sr, (long) fp);
  263. printk(KERN_EMERG "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
  264. fp->d0, fp->d1, fp->d2, fp->d3);
  265. printk(KERN_EMERG "d4: %08lx d5: %08lx a0: %08lx a1: %08lx\n",
  266. fp->d4, fp->d5, fp->a0, fp->a1);
  267. printk(KERN_EMERG "\nUSP: %08x TRAPFRAME: %08x\n", (unsigned int) rdusp(),
  268. (unsigned int) fp);
  269. printk(KERN_EMERG "\nCODE:");
  270. tp = ((unsigned char *) fp->pc) - 0x20;
  271. for (sp = (unsigned long *) tp, i = 0; (i < 0x40); i += 4) {
  272. if ((i % 0x10) == 0)
  273. printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
  274. printk(KERN_EMERG "%08x ", (int) *sp++);
  275. }
  276. printk(KERN_EMERG "\n");
  277. printk(KERN_EMERG "\nKERNEL STACK:");
  278. tp = ((unsigned char *) fp) - 0x40;
  279. for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
  280. if ((i % 0x10) == 0)
  281. printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
  282. printk(KERN_EMERG "%08x ", (int) *sp++);
  283. }
  284. printk(KERN_EMERG "\n");
  285. printk(KERN_EMERG "\n");
  286. printk(KERN_EMERG "\nUSER STACK:");
  287. tp = (unsigned char *) (rdusp() - 0x10);
  288. for (sp = (unsigned long *) tp, i = 0; (i < 0x80); i += 4) {
  289. if ((i % 0x10) == 0)
  290. printk(KERN_EMERG "\n%08x: ", (int) (tp + i));
  291. printk(KERN_EMERG "%08x ", (int) *sp++);
  292. }
  293. printk(KERN_EMERG "\n\n");
  294. }
  295. /*
  296. * sys_execve() executes a new program.
  297. */
  298. asmlinkage int sys_execve(char *name, char **argv, char **envp)
  299. {
  300. int error;
  301. char * filename;
  302. struct pt_regs *regs = (struct pt_regs *) &name;
  303. lock_kernel();
  304. filename = getname(name);
  305. error = PTR_ERR(filename);
  306. if (IS_ERR(filename))
  307. goto out;
  308. error = do_execve(filename, argv, envp, regs);
  309. putname(filename);
  310. out:
  311. unlock_kernel();
  312. return error;
  313. }
  314. unsigned long get_wchan(struct task_struct *p)
  315. {
  316. unsigned long fp, pc;
  317. unsigned long stack_page;
  318. int count = 0;
  319. if (!p || p == current || p->state == TASK_RUNNING)
  320. return 0;
  321. stack_page = (unsigned long)p;
  322. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  323. do {
  324. if (fp < stack_page+sizeof(struct thread_info) ||
  325. fp >= 8184+stack_page)
  326. return 0;
  327. pc = ((unsigned long *)fp)[1];
  328. if (!in_sched_functions(pc))
  329. return pc;
  330. fp = *(unsigned long *) fp;
  331. } while (count++ < 16);
  332. return 0;
  333. }
  334. /*
  335. * Return saved PC of a blocked thread.
  336. */
  337. unsigned long thread_saved_pc(struct task_struct *tsk)
  338. {
  339. struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
  340. /* Check whether the thread is blocked in resume() */
  341. if (in_sched_functions(sw->retpc))
  342. return ((unsigned long *)sw->a6)[1];
  343. else
  344. return sw->retpc;
  345. }