process.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * linux/arch/m68k/kernel/process.c
  3. *
  4. * Copyright (C) 1995 Hamish Macdonald
  5. *
  6. * 68060 fixes by Jesper Skov
  7. */
  8. /*
  9. * This file handles the architecture-dependent parts of process handling..
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/smp.h>
  19. #include <linux/stddef.h>
  20. #include <linux/unistd.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/user.h>
  23. #include <linux/reboot.h>
  24. #include <linux/init_task.h>
  25. #include <linux/mqueue.h>
  26. #include <linux/rcupdate.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/traps.h>
  29. #include <asm/machdep.h>
  30. #include <asm/setup.h>
  31. #include <asm/pgtable.h>
  32. asmlinkage void ret_from_fork(void);
  33. asmlinkage void ret_from_kernel_thread(void);
  34. /*
  35. * Return saved PC from a blocked thread
  36. */
  37. unsigned long thread_saved_pc(struct task_struct *tsk)
  38. {
  39. struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
  40. /* Check whether the thread is blocked in resume() */
  41. if (in_sched_functions(sw->retpc))
  42. return ((unsigned long *)sw->a6)[1];
  43. else
  44. return sw->retpc;
  45. }
  46. /*
  47. * The idle loop on an m68k..
  48. */
  49. static void default_idle(void)
  50. {
  51. if (!need_resched())
  52. #if defined(MACH_ATARI_ONLY)
  53. /* block out HSYNC on the atari (falcon) */
  54. __asm__("stop #0x2200" : : : "cc");
  55. #else
  56. __asm__("stop #0x2000" : : : "cc");
  57. #endif
  58. }
  59. void (*idle)(void) = default_idle;
  60. /*
  61. * The idle thread. There's no useful work to be
  62. * done, so just try to conserve power and have a
  63. * low exit latency (ie sit in a loop waiting for
  64. * somebody to say that they'd like to reschedule)
  65. */
  66. void cpu_idle(void)
  67. {
  68. /* endless idle loop with no priority at all */
  69. while (1) {
  70. rcu_idle_enter();
  71. while (!need_resched())
  72. idle();
  73. rcu_idle_exit();
  74. schedule_preempt_disabled();
  75. }
  76. }
  77. void machine_restart(char * __unused)
  78. {
  79. if (mach_reset)
  80. mach_reset();
  81. for (;;);
  82. }
  83. void machine_halt(void)
  84. {
  85. if (mach_halt)
  86. mach_halt();
  87. for (;;);
  88. }
  89. void machine_power_off(void)
  90. {
  91. if (mach_power_off)
  92. mach_power_off();
  93. for (;;);
  94. }
  95. void (*pm_power_off)(void) = machine_power_off;
  96. EXPORT_SYMBOL(pm_power_off);
  97. void show_regs(struct pt_regs * regs)
  98. {
  99. printk("\n");
  100. printk("Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
  101. regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
  102. printk("ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
  103. regs->orig_d0, regs->d0, regs->a2, regs->a1);
  104. printk("A0: %08lx D5: %08lx D4: %08lx\n",
  105. regs->a0, regs->d5, regs->d4);
  106. printk("D3: %08lx D2: %08lx D1: %08lx\n",
  107. regs->d3, regs->d2, regs->d1);
  108. if (!(regs->sr & PS_S))
  109. printk("USP: %08lx\n", rdusp());
  110. }
  111. void flush_thread(void)
  112. {
  113. current->thread.fs = __USER_DS;
  114. #ifdef CONFIG_FPU
  115. if (!FPU_IS_EMU) {
  116. unsigned long zero = 0;
  117. asm volatile("frestore %0": :"m" (zero));
  118. }
  119. #endif
  120. }
  121. /*
  122. * "m68k_fork()".. By the time we get here, the
  123. * non-volatile registers have also been saved on the
  124. * stack. We do some ugly pointer stuff here.. (see
  125. * also copy_thread)
  126. */
  127. asmlinkage int m68k_fork(struct pt_regs *regs)
  128. {
  129. #ifdef CONFIG_MMU
  130. return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  131. #else
  132. return -EINVAL;
  133. #endif
  134. }
  135. asmlinkage int m68k_vfork(struct pt_regs *regs)
  136. {
  137. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0,
  138. NULL, NULL);
  139. }
  140. asmlinkage int m68k_clone(struct pt_regs *regs)
  141. {
  142. unsigned long clone_flags;
  143. unsigned long newsp;
  144. int __user *parent_tidptr, *child_tidptr;
  145. /* syscall2 puts clone_flags in d1 and usp in d2 */
  146. clone_flags = regs->d1;
  147. newsp = regs->d2;
  148. parent_tidptr = (int __user *)regs->d3;
  149. child_tidptr = (int __user *)regs->d4;
  150. if (!newsp)
  151. newsp = rdusp();
  152. return do_fork(clone_flags, newsp, regs, 0,
  153. parent_tidptr, child_tidptr);
  154. }
  155. int copy_thread(unsigned long clone_flags, unsigned long usp,
  156. unsigned long arg,
  157. struct task_struct * p, struct pt_regs * regs)
  158. {
  159. struct pt_regs * childregs;
  160. struct switch_stack *childstack;
  161. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  162. childstack = ((struct switch_stack *) childregs) - 1;
  163. p->thread.usp = usp;
  164. p->thread.ksp = (unsigned long)childstack;
  165. p->thread.esp0 = (unsigned long)childregs;
  166. /*
  167. * Must save the current SFC/DFC value, NOT the value when
  168. * the parent was last descheduled - RGH 10-08-96
  169. */
  170. p->thread.fs = get_fs().seg;
  171. if (unlikely(!regs)) {
  172. /* kernel thread */
  173. memset(childstack, 0,
  174. sizeof(struct switch_stack) + sizeof(struct pt_regs));
  175. childregs->sr = PS_S;
  176. childstack->a3 = usp; /* function */
  177. childstack->d7 = arg;
  178. childstack->retpc = (unsigned long)ret_from_kernel_thread;
  179. p->thread.usp = 0;
  180. return 0;
  181. }
  182. *childregs = *regs;
  183. childregs->d0 = 0;
  184. *childstack = ((struct switch_stack *) regs)[-1];
  185. childstack->retpc = (unsigned long)ret_from_fork;
  186. if (clone_flags & CLONE_SETTLS)
  187. task_thread_info(p)->tp_value = regs->d5;
  188. #ifdef CONFIG_FPU
  189. if (!FPU_IS_EMU) {
  190. /* Copy the current fpu state */
  191. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  192. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
  193. if (CPU_IS_COLDFIRE) {
  194. asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
  195. "fmovel %/fpiar,%1\n\t"
  196. "fmovel %/fpcr,%2\n\t"
  197. "fmovel %/fpsr,%3"
  198. :
  199. : "m" (p->thread.fp[0]),
  200. "m" (p->thread.fpcntl[0]),
  201. "m" (p->thread.fpcntl[1]),
  202. "m" (p->thread.fpcntl[2])
  203. : "memory");
  204. } else {
  205. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  206. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  207. :
  208. : "m" (p->thread.fp[0]),
  209. "m" (p->thread.fpcntl[0])
  210. : "memory");
  211. }
  212. }
  213. /* Restore the state in case the fpu was busy */
  214. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  215. }
  216. #endif /* CONFIG_FPU */
  217. return 0;
  218. }
  219. /* Fill in the fpu structure for a core dump. */
  220. #ifdef CONFIG_FPU
  221. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  222. {
  223. char fpustate[216];
  224. if (FPU_IS_EMU) {
  225. int i;
  226. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  227. memcpy(fpu->fpregs, current->thread.fp, 96);
  228. /* Convert internal fpu reg representation
  229. * into long double format
  230. */
  231. for (i = 0; i < 24; i += 3)
  232. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  233. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  234. return 1;
  235. }
  236. /* First dump the fpu context to avoid protocol violation. */
  237. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  238. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  239. return 0;
  240. if (CPU_IS_COLDFIRE) {
  241. asm volatile ("fmovel %/fpiar,%0\n\t"
  242. "fmovel %/fpcr,%1\n\t"
  243. "fmovel %/fpsr,%2\n\t"
  244. "fmovemd %/fp0-%/fp7,%3"
  245. :
  246. : "m" (fpu->fpcntl[0]),
  247. "m" (fpu->fpcntl[1]),
  248. "m" (fpu->fpcntl[2]),
  249. "m" (fpu->fpregs[0])
  250. : "memory");
  251. } else {
  252. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  253. :
  254. : "m" (fpu->fpcntl[0])
  255. : "memory");
  256. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  257. :
  258. : "m" (fpu->fpregs[0])
  259. : "memory");
  260. }
  261. return 1;
  262. }
  263. EXPORT_SYMBOL(dump_fpu);
  264. #endif /* CONFIG_FPU */
  265. unsigned long get_wchan(struct task_struct *p)
  266. {
  267. unsigned long fp, pc;
  268. unsigned long stack_page;
  269. int count = 0;
  270. if (!p || p == current || p->state == TASK_RUNNING)
  271. return 0;
  272. stack_page = (unsigned long)task_stack_page(p);
  273. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  274. do {
  275. if (fp < stack_page+sizeof(struct thread_info) ||
  276. fp >= 8184+stack_page)
  277. return 0;
  278. pc = ((unsigned long *)fp)[1];
  279. if (!in_sched_functions(pc))
  280. return pc;
  281. fp = *(unsigned long *) fp;
  282. } while (count++ < 16);
  283. return 0;
  284. }