process.c 11 KB

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