process.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/sched/debug.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/slab.h>
  20. #include <linux/fs.h>
  21. #include <linux/smp.h>
  22. #include <linux/stddef.h>
  23. #include <linux/unistd.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/user.h>
  26. #include <linux/reboot.h>
  27. #include <linux/init_task.h>
  28. #include <linux/mqueue.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/traps.h>
  32. #include <asm/machdep.h>
  33. #include <asm/setup.h>
  34. #include <asm/pgtable.h>
  35. asmlinkage void ret_from_fork(void);
  36. asmlinkage void ret_from_kernel_thread(void);
  37. /*
  38. * Return saved PC from a blocked thread
  39. */
  40. unsigned long thread_saved_pc(struct task_struct *tsk)
  41. {
  42. struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
  43. /* Check whether the thread is blocked in resume() */
  44. if (in_sched_functions(sw->retpc))
  45. return ((unsigned long *)sw->a6)[1];
  46. else
  47. return sw->retpc;
  48. }
  49. void arch_cpu_idle(void)
  50. {
  51. #if defined(MACH_ATARI_ONLY)
  52. /* block out HSYNC on the atari (falcon) */
  53. __asm__("stop #0x2200" : : : "cc");
  54. #else
  55. __asm__("stop #0x2000" : : : "cc");
  56. #endif
  57. }
  58. void machine_restart(char * __unused)
  59. {
  60. if (mach_reset)
  61. mach_reset();
  62. for (;;);
  63. }
  64. void machine_halt(void)
  65. {
  66. if (mach_halt)
  67. mach_halt();
  68. for (;;);
  69. }
  70. void machine_power_off(void)
  71. {
  72. if (mach_power_off)
  73. mach_power_off();
  74. for (;;);
  75. }
  76. void (*pm_power_off)(void) = machine_power_off;
  77. EXPORT_SYMBOL(pm_power_off);
  78. void show_regs(struct pt_regs * regs)
  79. {
  80. pr_info("Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
  81. regs->format, regs->vector, regs->pc, regs->sr,
  82. print_tainted());
  83. pr_info("ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
  84. regs->orig_d0, regs->d0, regs->a2, regs->a1);
  85. pr_info("A0: %08lx D5: %08lx D4: %08lx\n", regs->a0, regs->d5,
  86. regs->d4);
  87. pr_info("D3: %08lx D2: %08lx D1: %08lx\n", regs->d3, regs->d2,
  88. regs->d1);
  89. if (!(regs->sr & PS_S))
  90. pr_info("USP: %08lx\n", rdusp());
  91. }
  92. void flush_thread(void)
  93. {
  94. current->thread.fs = __USER_DS;
  95. #ifdef CONFIG_FPU
  96. if (!FPU_IS_EMU) {
  97. unsigned long zero = 0;
  98. asm volatile("frestore %0": :"m" (zero));
  99. }
  100. #endif
  101. }
  102. /*
  103. * Why not generic sys_clone, you ask? m68k passes all arguments on stack.
  104. * And we need all registers saved, which means a bunch of stuff pushed
  105. * on top of pt_regs, which means that sys_clone() arguments would be
  106. * buried. We could, of course, copy them, but it's too costly for no
  107. * good reason - generic clone() would have to copy them *again* for
  108. * do_fork() anyway. So in this case it's actually better to pass pt_regs *
  109. * and extract arguments for do_fork() from there. Eventually we might
  110. * go for calling do_fork() directly from the wrapper, but only after we
  111. * are finished with do_fork() prototype conversion.
  112. */
  113. asmlinkage int m68k_clone(struct pt_regs *regs)
  114. {
  115. /* regs will be equal to current_pt_regs() */
  116. return do_fork(regs->d1, regs->d2, 0,
  117. (int __user *)regs->d3, (int __user *)regs->d4);
  118. }
  119. int copy_thread(unsigned long clone_flags, unsigned long usp,
  120. unsigned long arg, struct task_struct *p)
  121. {
  122. struct fork_frame {
  123. struct switch_stack sw;
  124. struct pt_regs regs;
  125. } *frame;
  126. frame = (struct fork_frame *) (task_stack_page(p) + THREAD_SIZE) - 1;
  127. p->thread.ksp = (unsigned long)frame;
  128. p->thread.esp0 = (unsigned long)&frame->regs;
  129. /*
  130. * Must save the current SFC/DFC value, NOT the value when
  131. * the parent was last descheduled - RGH 10-08-96
  132. */
  133. p->thread.fs = get_fs().seg;
  134. if (unlikely(p->flags & PF_KTHREAD)) {
  135. /* kernel thread */
  136. memset(frame, 0, sizeof(struct fork_frame));
  137. frame->regs.sr = PS_S;
  138. frame->sw.a3 = usp; /* function */
  139. frame->sw.d7 = arg;
  140. frame->sw.retpc = (unsigned long)ret_from_kernel_thread;
  141. p->thread.usp = 0;
  142. return 0;
  143. }
  144. memcpy(frame, container_of(current_pt_regs(), struct fork_frame, regs),
  145. sizeof(struct fork_frame));
  146. frame->regs.d0 = 0;
  147. frame->sw.retpc = (unsigned long)ret_from_fork;
  148. p->thread.usp = usp ?: rdusp();
  149. if (clone_flags & CLONE_SETTLS)
  150. task_thread_info(p)->tp_value = frame->regs.d5;
  151. #ifdef CONFIG_FPU
  152. if (!FPU_IS_EMU) {
  153. /* Copy the current fpu state */
  154. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  155. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
  156. if (CPU_IS_COLDFIRE) {
  157. asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
  158. "fmovel %/fpiar,%1\n\t"
  159. "fmovel %/fpcr,%2\n\t"
  160. "fmovel %/fpsr,%3"
  161. :
  162. : "m" (p->thread.fp[0]),
  163. "m" (p->thread.fpcntl[0]),
  164. "m" (p->thread.fpcntl[1]),
  165. "m" (p->thread.fpcntl[2])
  166. : "memory");
  167. } else {
  168. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  169. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  170. :
  171. : "m" (p->thread.fp[0]),
  172. "m" (p->thread.fpcntl[0])
  173. : "memory");
  174. }
  175. }
  176. /* Restore the state in case the fpu was busy */
  177. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  178. }
  179. #endif /* CONFIG_FPU */
  180. return 0;
  181. }
  182. /* Fill in the fpu structure for a core dump. */
  183. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  184. {
  185. if (FPU_IS_EMU) {
  186. int i;
  187. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  188. memcpy(fpu->fpregs, current->thread.fp, 96);
  189. /* Convert internal fpu reg representation
  190. * into long double format
  191. */
  192. for (i = 0; i < 24; i += 3)
  193. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  194. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  195. return 1;
  196. }
  197. if (IS_ENABLED(CONFIG_FPU)) {
  198. char fpustate[216];
  199. /* First dump the fpu context to avoid protocol violation. */
  200. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  201. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  202. return 0;
  203. if (CPU_IS_COLDFIRE) {
  204. asm volatile ("fmovel %/fpiar,%0\n\t"
  205. "fmovel %/fpcr,%1\n\t"
  206. "fmovel %/fpsr,%2\n\t"
  207. "fmovemd %/fp0-%/fp7,%3"
  208. :
  209. : "m" (fpu->fpcntl[0]),
  210. "m" (fpu->fpcntl[1]),
  211. "m" (fpu->fpcntl[2]),
  212. "m" (fpu->fpregs[0])
  213. : "memory");
  214. } else {
  215. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  216. :
  217. : "m" (fpu->fpcntl[0])
  218. : "memory");
  219. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  220. :
  221. : "m" (fpu->fpregs[0])
  222. : "memory");
  223. }
  224. }
  225. return 1;
  226. }
  227. EXPORT_SYMBOL(dump_fpu);
  228. unsigned long get_wchan(struct task_struct *p)
  229. {
  230. unsigned long fp, pc;
  231. unsigned long stack_page;
  232. int count = 0;
  233. if (!p || p == current || p->state == TASK_RUNNING)
  234. return 0;
  235. stack_page = (unsigned long)task_stack_page(p);
  236. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  237. do {
  238. if (fp < stack_page+sizeof(struct thread_info) ||
  239. fp >= 8184+stack_page)
  240. return 0;
  241. pc = ((unsigned long *)fp)[1];
  242. if (!in_sched_functions(pc))
  243. return pc;
  244. fp = *(unsigned long *) fp;
  245. } while (count++ < 16);
  246. return 0;
  247. }