process.c 9.5 KB

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