process.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * Why not generic sys_clone, you ask? m68k passes all arguments on stack.
  123. * And we need all registers saved, which means a bunch of stuff pushed
  124. * on top of pt_regs, which means that sys_clone() arguments would be
  125. * buried. We could, of course, copy them, but it's too costly for no
  126. * good reason - generic clone() would have to copy them *again* for
  127. * do_fork() anyway. So in this case it's actually better to pass pt_regs *
  128. * and extract arguments for do_fork() from there. Eventually we might
  129. * go for calling do_fork() directly from the wrapper, but only after we
  130. * are finished with do_fork() prototype conversion.
  131. */
  132. asmlinkage int m68k_clone(struct pt_regs *regs)
  133. {
  134. /* regs will be equal to current_pt_regs() */
  135. return do_fork(regs->d1, regs->d2, 0,
  136. (int __user *)regs->d3, (int __user *)regs->d4);
  137. }
  138. int copy_thread(unsigned long clone_flags, unsigned long usp,
  139. unsigned long arg, struct task_struct *p)
  140. {
  141. struct fork_frame {
  142. struct switch_stack sw;
  143. struct pt_regs regs;
  144. } *frame;
  145. frame = (struct fork_frame *) (task_stack_page(p) + THREAD_SIZE) - 1;
  146. p->thread.ksp = (unsigned long)frame;
  147. p->thread.esp0 = (unsigned long)&frame->regs;
  148. /*
  149. * Must save the current SFC/DFC value, NOT the value when
  150. * the parent was last descheduled - RGH 10-08-96
  151. */
  152. p->thread.fs = get_fs().seg;
  153. if (unlikely(p->flags & PF_KTHREAD)) {
  154. /* kernel thread */
  155. memset(frame, 0, sizeof(struct fork_frame));
  156. frame->regs.sr = PS_S;
  157. frame->sw.a3 = usp; /* function */
  158. frame->sw.d7 = arg;
  159. frame->sw.retpc = (unsigned long)ret_from_kernel_thread;
  160. p->thread.usp = 0;
  161. return 0;
  162. }
  163. memcpy(frame, container_of(current_pt_regs(), struct fork_frame, regs),
  164. sizeof(struct fork_frame));
  165. frame->regs.d0 = 0;
  166. frame->sw.retpc = (unsigned long)ret_from_fork;
  167. p->thread.usp = usp ?: rdusp();
  168. if (clone_flags & CLONE_SETTLS)
  169. task_thread_info(p)->tp_value = frame->regs.d5;
  170. #ifdef CONFIG_FPU
  171. if (!FPU_IS_EMU) {
  172. /* Copy the current fpu state */
  173. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  174. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
  175. if (CPU_IS_COLDFIRE) {
  176. asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
  177. "fmovel %/fpiar,%1\n\t"
  178. "fmovel %/fpcr,%2\n\t"
  179. "fmovel %/fpsr,%3"
  180. :
  181. : "m" (p->thread.fp[0]),
  182. "m" (p->thread.fpcntl[0]),
  183. "m" (p->thread.fpcntl[1]),
  184. "m" (p->thread.fpcntl[2])
  185. : "memory");
  186. } else {
  187. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  188. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  189. :
  190. : "m" (p->thread.fp[0]),
  191. "m" (p->thread.fpcntl[0])
  192. : "memory");
  193. }
  194. }
  195. /* Restore the state in case the fpu was busy */
  196. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  197. }
  198. #endif /* CONFIG_FPU */
  199. return 0;
  200. }
  201. /* Fill in the fpu structure for a core dump. */
  202. #ifdef CONFIG_FPU
  203. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  204. {
  205. char fpustate[216];
  206. if (FPU_IS_EMU) {
  207. int i;
  208. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  209. memcpy(fpu->fpregs, current->thread.fp, 96);
  210. /* Convert internal fpu reg representation
  211. * into long double format
  212. */
  213. for (i = 0; i < 24; i += 3)
  214. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  215. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  216. return 1;
  217. }
  218. /* First dump the fpu context to avoid protocol violation. */
  219. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  220. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  221. return 0;
  222. if (CPU_IS_COLDFIRE) {
  223. asm volatile ("fmovel %/fpiar,%0\n\t"
  224. "fmovel %/fpcr,%1\n\t"
  225. "fmovel %/fpsr,%2\n\t"
  226. "fmovemd %/fp0-%/fp7,%3"
  227. :
  228. : "m" (fpu->fpcntl[0]),
  229. "m" (fpu->fpcntl[1]),
  230. "m" (fpu->fpcntl[2]),
  231. "m" (fpu->fpregs[0])
  232. : "memory");
  233. } else {
  234. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  235. :
  236. : "m" (fpu->fpcntl[0])
  237. : "memory");
  238. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  239. :
  240. : "m" (fpu->fpregs[0])
  241. : "memory");
  242. }
  243. return 1;
  244. }
  245. EXPORT_SYMBOL(dump_fpu);
  246. #endif /* CONFIG_FPU */
  247. unsigned long get_wchan(struct task_struct *p)
  248. {
  249. unsigned long fp, pc;
  250. unsigned long stack_page;
  251. int count = 0;
  252. if (!p || p == current || p->state == TASK_RUNNING)
  253. return 0;
  254. stack_page = (unsigned long)task_stack_page(p);
  255. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  256. do {
  257. if (fp < stack_page+sizeof(struct thread_info) ||
  258. fp >= 8184+stack_page)
  259. return 0;
  260. pc = ((unsigned long *)fp)[1];
  261. if (!in_sched_functions(pc))
  262. return pc;
  263. fp = *(unsigned long *) fp;
  264. } while (count++ < 16);
  265. return 0;
  266. }