process.c 6.7 KB

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