process.c 9.4 KB

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