process_no.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/stddef.h>
  21. #include <linux/unistd.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/user.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/reboot.h>
  26. #include <linux/fs.h>
  27. #include <linux/slab.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/system.h>
  30. #include <asm/traps.h>
  31. #include <asm/machdep.h>
  32. #include <asm/setup.h>
  33. #include <asm/pgtable.h>
  34. asmlinkage void ret_from_fork(void);
  35. /*
  36. * The following aren't currently used.
  37. */
  38. void (*pm_idle)(void);
  39. EXPORT_SYMBOL(pm_idle);
  40. void (*pm_power_off)(void);
  41. EXPORT_SYMBOL(pm_power_off);
  42. /*
  43. * The idle loop on an m68knommu..
  44. */
  45. static void default_idle(void)
  46. {
  47. local_irq_disable();
  48. while (!need_resched()) {
  49. /* This stop will re-enable interrupts */
  50. __asm__("stop #0x2000" : : : "cc");
  51. local_irq_disable();
  52. }
  53. local_irq_enable();
  54. }
  55. void (*idle)(void) = default_idle;
  56. /*
  57. * The idle thread. There's no useful work to be
  58. * done, so just try to conserve power and have a
  59. * low exit latency (ie sit in a loop waiting for
  60. * somebody to say that they'd like to reschedule)
  61. */
  62. void cpu_idle(void)
  63. {
  64. /* endless idle loop with no priority at all */
  65. while (1) {
  66. idle();
  67. preempt_enable_no_resched();
  68. schedule();
  69. preempt_disable();
  70. }
  71. }
  72. void machine_restart(char * __unused)
  73. {
  74. if (mach_reset)
  75. mach_reset();
  76. for (;;);
  77. }
  78. void machine_halt(void)
  79. {
  80. if (mach_halt)
  81. mach_halt();
  82. for (;;);
  83. }
  84. void machine_power_off(void)
  85. {
  86. if (mach_power_off)
  87. mach_power_off();
  88. for (;;);
  89. }
  90. void show_regs(struct pt_regs * regs)
  91. {
  92. printk(KERN_NOTICE "\n");
  93. printk(KERN_NOTICE "Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
  94. regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
  95. printk(KERN_NOTICE "ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
  96. regs->orig_d0, regs->d0, regs->a2, regs->a1);
  97. printk(KERN_NOTICE "A0: %08lx D5: %08lx D4: %08lx\n",
  98. regs->a0, regs->d5, regs->d4);
  99. printk(KERN_NOTICE "D3: %08lx D2: %08lx D1: %08lx\n",
  100. regs->d3, regs->d2, regs->d1);
  101. if (!(regs->sr & PS_S))
  102. printk(KERN_NOTICE "USP: %08lx\n", rdusp());
  103. }
  104. /*
  105. * Create a kernel thread
  106. */
  107. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  108. {
  109. int retval;
  110. long clone_arg = flags | CLONE_VM;
  111. mm_segment_t fs;
  112. fs = get_fs();
  113. set_fs(KERNEL_DS);
  114. __asm__ __volatile__ (
  115. "movel %%sp, %%d2\n\t"
  116. "movel %5, %%d1\n\t"
  117. "movel %1, %%d0\n\t"
  118. "trap #0\n\t"
  119. "cmpl %%sp, %%d2\n\t"
  120. "jeq 1f\n\t"
  121. "movel %3, %%sp@-\n\t"
  122. "jsr %4@\n\t"
  123. "movel %2, %%d0\n\t"
  124. "trap #0\n"
  125. "1:\n\t"
  126. "movel %%d0, %0\n"
  127. : "=d" (retval)
  128. : "i" (__NR_clone),
  129. "i" (__NR_exit),
  130. "a" (arg),
  131. "a" (fn),
  132. "a" (clone_arg)
  133. : "cc", "%d0", "%d1", "%d2");
  134. set_fs(fs);
  135. return retval;
  136. }
  137. EXPORT_SYMBOL(kernel_thread);
  138. void flush_thread(void)
  139. {
  140. #ifdef CONFIG_FPU
  141. unsigned long zero = 0;
  142. #endif
  143. current->thread.fs = __USER_DS;
  144. #ifdef CONFIG_FPU
  145. if (!FPU_IS_EMU)
  146. asm volatile (".chip 68k/68881\n\t"
  147. "frestore %0@\n\t"
  148. ".chip 68k" : : "a" (&zero));
  149. #endif
  150. }
  151. /*
  152. * "m68k_fork()".. By the time we get here, the
  153. * non-volatile registers have also been saved on the
  154. * stack. We do some ugly pointer stuff here.. (see
  155. * also copy_thread)
  156. */
  157. asmlinkage int m68k_fork(struct pt_regs *regs)
  158. {
  159. /* fork almost works, enough to trick you into looking elsewhere :-( */
  160. return(-EINVAL);
  161. }
  162. asmlinkage int m68k_vfork(struct pt_regs *regs)
  163. {
  164. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  165. }
  166. asmlinkage int m68k_clone(struct pt_regs *regs)
  167. {
  168. unsigned long clone_flags;
  169. unsigned long newsp;
  170. /* syscall2 puts clone_flags in d1 and usp in d2 */
  171. clone_flags = regs->d1;
  172. newsp = regs->d2;
  173. if (!newsp)
  174. newsp = rdusp();
  175. return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
  176. }
  177. int copy_thread(unsigned long clone_flags,
  178. unsigned long usp, unsigned long topstk,
  179. struct task_struct * p, struct pt_regs * regs)
  180. {
  181. struct pt_regs * childregs;
  182. struct switch_stack * childstack, *stack;
  183. unsigned long *retp;
  184. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  185. *childregs = *regs;
  186. childregs->d0 = 0;
  187. retp = ((unsigned long *) regs);
  188. stack = ((struct switch_stack *) retp) - 1;
  189. childstack = ((struct switch_stack *) childregs) - 1;
  190. *childstack = *stack;
  191. childstack->retpc = (unsigned long)ret_from_fork;
  192. p->thread.usp = usp;
  193. p->thread.ksp = (unsigned long)childstack;
  194. if (clone_flags & CLONE_SETTLS)
  195. task_thread_info(p)->tp_value = regs->d5;
  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. EXPORT_SYMBOL(dump_fpu);
  247. /*
  248. * Generic dumping code. Used for panic and debug.
  249. */
  250. void dump(struct pt_regs *fp)
  251. {
  252. unsigned long *sp;
  253. unsigned char *tp;
  254. int i;
  255. printk(KERN_EMERG "\nCURRENT PROCESS:\n\n");
  256. printk(KERN_EMERG "COMM=%s PID=%d\n", current->comm, current->pid);
  257. if (current->mm) {
  258. printk(KERN_EMERG "TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
  259. (int) current->mm->start_code,
  260. (int) current->mm->end_code,
  261. (int) current->mm->start_data,
  262. (int) current->mm->end_data,
  263. (int) current->mm->end_data,
  264. (int) current->mm->brk);
  265. printk(KERN_EMERG "USER-STACK=%08x KERNEL-STACK=%08x\n\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 "\nUSP: %08x TRAPFRAME: %p\n",
  276. (unsigned int) rdusp(), fp);
  277. printk(KERN_EMERG "\nCODE:");
  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(KERN_EMERG "%p: ", tp + i);
  282. printk("%08x ", (int) *sp++);
  283. }
  284. printk(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(KERN_EMERG "%p: ", tp + i);
  290. printk("%08x ", (int) *sp++);
  291. }
  292. printk(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(KERN_EMERG "%p: ", tp + i);
  298. printk("%08x ", (int) *sp++);
  299. }
  300. printk(KERN_EMERG "\n");
  301. }
  302. /*
  303. * sys_execve() executes a new program.
  304. */
  305. asmlinkage int sys_execve(const char *name,
  306. const char *const *argv,
  307. const char *const *envp)
  308. {
  309. int error;
  310. char * filename;
  311. struct pt_regs *regs = (struct pt_regs *) &name;
  312. filename = getname(name);
  313. error = PTR_ERR(filename);
  314. if (IS_ERR(filename))
  315. return error;
  316. error = do_execve(filename, argv, envp, regs);
  317. putname(filename);
  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 >= THREAD_SIZE-8+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. }