process.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/syscalls.h>
  32. #include <linux/uaccess.h>
  33. #include <asm/traps.h>
  34. #include <asm/machdep.h>
  35. #include <asm/setup.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. * kernel_clone() anyway. So in this case it's actually better to pass pt_regs *
  98. * and extract arguments for kernel_clone() from there. Eventually we might
  99. * go for calling kernel_clone() directly from the wrapper, but only after we
  100. * are finished with kernel_clone() prototype conversion.
  101. */
  102. asmlinkage int m68k_clone(struct pt_regs *regs)
  103. {
  104. /* regs will be equal to current_pt_regs() */
  105. struct kernel_clone_args args = {
  106. .flags = regs->d1 & ~CSIGNAL,
  107. .pidfd = (int __user *)regs->d3,
  108. .child_tid = (int __user *)regs->d4,
  109. .parent_tid = (int __user *)regs->d3,
  110. .exit_signal = regs->d1 & CSIGNAL,
  111. .stack = regs->d2,
  112. .tls = regs->d5,
  113. };
  114. return kernel_clone(&args);
  115. }
  116. /*
  117. * Because extra registers are saved on the stack after the sys_clone3()
  118. * arguments, this C wrapper extracts them from pt_regs * and then calls the
  119. * generic sys_clone3() implementation.
  120. */
  121. asmlinkage int m68k_clone3(struct pt_regs *regs)
  122. {
  123. return sys_clone3((struct clone_args __user *)regs->d1, regs->d2);
  124. }
  125. int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg,
  126. struct task_struct *p, unsigned long tls)
  127. {
  128. struct fork_frame {
  129. struct switch_stack sw;
  130. struct pt_regs regs;
  131. } *frame;
  132. frame = (struct fork_frame *) (task_stack_page(p) + THREAD_SIZE) - 1;
  133. p->thread.ksp = (unsigned long)frame;
  134. p->thread.esp0 = (unsigned long)&frame->regs;
  135. /*
  136. * Must save the current SFC/DFC value, NOT the value when
  137. * the parent was last descheduled - RGH 10-08-96
  138. */
  139. p->thread.fs = get_fs().seg;
  140. if (unlikely(p->flags & PF_KTHREAD)) {
  141. /* kernel thread */
  142. memset(frame, 0, sizeof(struct fork_frame));
  143. frame->regs.sr = PS_S;
  144. frame->sw.a3 = usp; /* function */
  145. frame->sw.d7 = arg;
  146. frame->sw.retpc = (unsigned long)ret_from_kernel_thread;
  147. p->thread.usp = 0;
  148. return 0;
  149. }
  150. memcpy(frame, container_of(current_pt_regs(), struct fork_frame, regs),
  151. sizeof(struct fork_frame));
  152. frame->regs.d0 = 0;
  153. frame->sw.retpc = (unsigned long)ret_from_fork;
  154. p->thread.usp = usp ?: rdusp();
  155. if (clone_flags & CLONE_SETTLS)
  156. task_thread_info(p)->tp_value = tls;
  157. #ifdef CONFIG_FPU
  158. if (!FPU_IS_EMU) {
  159. /* Copy the current fpu state */
  160. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  161. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
  162. if (CPU_IS_COLDFIRE) {
  163. asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
  164. "fmovel %/fpiar,%1\n\t"
  165. "fmovel %/fpcr,%2\n\t"
  166. "fmovel %/fpsr,%3"
  167. :
  168. : "m" (p->thread.fp[0]),
  169. "m" (p->thread.fpcntl[0]),
  170. "m" (p->thread.fpcntl[1]),
  171. "m" (p->thread.fpcntl[2])
  172. : "memory");
  173. } else {
  174. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  175. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  176. :
  177. : "m" (p->thread.fp[0]),
  178. "m" (p->thread.fpcntl[0])
  179. : "memory");
  180. }
  181. }
  182. /* Restore the state in case the fpu was busy */
  183. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  184. }
  185. #endif /* CONFIG_FPU */
  186. return 0;
  187. }
  188. /* Fill in the fpu structure for a core dump. */
  189. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  190. {
  191. if (FPU_IS_EMU) {
  192. int i;
  193. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  194. memcpy(fpu->fpregs, current->thread.fp, 96);
  195. /* Convert internal fpu reg representation
  196. * into long double format
  197. */
  198. for (i = 0; i < 24; i += 3)
  199. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  200. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  201. return 1;
  202. }
  203. if (IS_ENABLED(CONFIG_FPU)) {
  204. char fpustate[216];
  205. /* First dump the fpu context to avoid protocol violation. */
  206. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  207. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  208. return 0;
  209. if (CPU_IS_COLDFIRE) {
  210. asm volatile ("fmovel %/fpiar,%0\n\t"
  211. "fmovel %/fpcr,%1\n\t"
  212. "fmovel %/fpsr,%2\n\t"
  213. "fmovemd %/fp0-%/fp7,%3"
  214. :
  215. : "m" (fpu->fpcntl[0]),
  216. "m" (fpu->fpcntl[1]),
  217. "m" (fpu->fpcntl[2]),
  218. "m" (fpu->fpregs[0])
  219. : "memory");
  220. } else {
  221. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  222. :
  223. : "m" (fpu->fpcntl[0])
  224. : "memory");
  225. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  226. :
  227. : "m" (fpu->fpregs[0])
  228. : "memory");
  229. }
  230. }
  231. return 1;
  232. }
  233. EXPORT_SYMBOL(dump_fpu);
  234. unsigned long get_wchan(struct task_struct *p)
  235. {
  236. unsigned long fp, pc;
  237. unsigned long stack_page;
  238. int count = 0;
  239. if (!p || p == current || p->state == TASK_RUNNING)
  240. return 0;
  241. stack_page = (unsigned long)task_stack_page(p);
  242. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  243. do {
  244. if (fp < stack_page+sizeof(struct thread_info) ||
  245. fp >= 8184+stack_page)
  246. return 0;
  247. pc = ((unsigned long *)fp)[1];
  248. if (!in_sched_functions(pc))
  249. return pc;
  250. fp = *(unsigned long *) fp;
  251. } while (count++ < 16);
  252. return 0;
  253. }