process.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. do_kernel_power_off();
  62. for (;;);
  63. }
  64. void (*pm_power_off)(void);
  65. EXPORT_SYMBOL(pm_power_off);
  66. void show_regs(struct pt_regs * regs)
  67. {
  68. pr_info("Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
  69. regs->format, regs->vector, regs->pc, regs->sr,
  70. print_tainted());
  71. pr_info("ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
  72. regs->orig_d0, regs->d0, regs->a2, regs->a1);
  73. pr_info("A0: %08lx D5: %08lx D4: %08lx\n", regs->a0, regs->d5,
  74. regs->d4);
  75. pr_info("D3: %08lx D2: %08lx D1: %08lx\n", regs->d3, regs->d2,
  76. regs->d1);
  77. if (!(regs->sr & PS_S))
  78. pr_info("USP: %08lx\n", rdusp());
  79. }
  80. void flush_thread(void)
  81. {
  82. current->thread.fc = USER_DATA;
  83. #ifdef CONFIG_FPU
  84. if (!FPU_IS_EMU) {
  85. unsigned long zero = 0;
  86. asm volatile("frestore %0": :"m" (zero));
  87. }
  88. #endif
  89. }
  90. /*
  91. * Why not generic sys_clone, you ask? m68k passes all arguments on stack.
  92. * And we need all registers saved, which means a bunch of stuff pushed
  93. * on top of pt_regs, which means that sys_clone() arguments would be
  94. * buried. We could, of course, copy them, but it's too costly for no
  95. * good reason - generic clone() would have to copy them *again* for
  96. * kernel_clone() anyway. So in this case it's actually better to pass pt_regs *
  97. * and extract arguments for kernel_clone() from there. Eventually we might
  98. * go for calling kernel_clone() directly from the wrapper, but only after we
  99. * are finished with kernel_clone() prototype conversion.
  100. */
  101. asmlinkage int m68k_clone(struct pt_regs *regs)
  102. {
  103. /* regs will be equal to current_pt_regs() */
  104. struct kernel_clone_args args = {
  105. .flags = regs->d1 & ~CSIGNAL,
  106. .pidfd = (int __user *)regs->d3,
  107. .child_tid = (int __user *)regs->d4,
  108. .parent_tid = (int __user *)regs->d3,
  109. .exit_signal = regs->d1 & CSIGNAL,
  110. .stack = regs->d2,
  111. .tls = regs->d5,
  112. };
  113. return kernel_clone(&args);
  114. }
  115. /*
  116. * Because extra registers are saved on the stack after the sys_clone3()
  117. * arguments, this C wrapper extracts them from pt_regs * and then calls the
  118. * generic sys_clone3() implementation.
  119. */
  120. asmlinkage int m68k_clone3(struct pt_regs *regs)
  121. {
  122. return sys_clone3((struct clone_args __user *)regs->d1, regs->d2);
  123. }
  124. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  125. {
  126. unsigned long clone_flags = args->flags;
  127. unsigned long usp = args->stack;
  128. unsigned long tls = args->tls;
  129. struct fork_frame {
  130. struct switch_stack sw;
  131. struct pt_regs regs;
  132. } *frame;
  133. frame = (struct fork_frame *) (task_stack_page(p) + THREAD_SIZE) - 1;
  134. p->thread.ksp = (unsigned long)frame;
  135. p->thread.esp0 = (unsigned long)&frame->regs;
  136. /*
  137. * Must save the current SFC/DFC value, NOT the value when
  138. * the parent was last descheduled - RGH 10-08-96
  139. */
  140. p->thread.fc = USER_DATA;
  141. if (unlikely(args->fn)) {
  142. /* kernel thread */
  143. memset(frame, 0, sizeof(struct fork_frame));
  144. frame->regs.sr = PS_S;
  145. frame->sw.a3 = (unsigned long)args->fn;
  146. frame->sw.d7 = (unsigned long)args->fn_arg;
  147. frame->sw.retpc = (unsigned long)ret_from_kernel_thread;
  148. p->thread.usp = 0;
  149. return 0;
  150. }
  151. memcpy(frame, container_of(current_pt_regs(), struct fork_frame, regs),
  152. sizeof(struct fork_frame));
  153. frame->regs.d0 = 0;
  154. frame->sw.retpc = (unsigned long)ret_from_fork;
  155. p->thread.usp = usp ?: rdusp();
  156. if (clone_flags & CLONE_SETTLS)
  157. task_thread_info(p)->tp_value = tls;
  158. #ifdef CONFIG_FPU
  159. if (!FPU_IS_EMU) {
  160. /* Copy the current fpu state */
  161. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  162. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
  163. if (CPU_IS_COLDFIRE) {
  164. asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
  165. "fmovel %/fpiar,%1\n\t"
  166. "fmovel %/fpcr,%2\n\t"
  167. "fmovel %/fpsr,%3"
  168. :
  169. : "m" (p->thread.fp[0]),
  170. "m" (p->thread.fpcntl[0]),
  171. "m" (p->thread.fpcntl[1]),
  172. "m" (p->thread.fpcntl[2])
  173. : "memory");
  174. } else {
  175. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  176. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  177. :
  178. : "m" (p->thread.fp[0]),
  179. "m" (p->thread.fpcntl[0])
  180. : "memory");
  181. }
  182. }
  183. /* Restore the state in case the fpu was busy */
  184. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  185. }
  186. #endif /* CONFIG_FPU */
  187. return 0;
  188. }
  189. /* Fill in the fpu structure for a core dump. */
  190. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  191. {
  192. if (FPU_IS_EMU) {
  193. int i;
  194. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  195. memcpy(fpu->fpregs, current->thread.fp, 96);
  196. /* Convert internal fpu reg representation
  197. * into long double format
  198. */
  199. for (i = 0; i < 24; i += 3)
  200. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  201. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  202. return 1;
  203. }
  204. if (IS_ENABLED(CONFIG_FPU)) {
  205. char fpustate[216];
  206. /* First dump the fpu context to avoid protocol violation. */
  207. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  208. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  209. return 0;
  210. if (CPU_IS_COLDFIRE) {
  211. asm volatile ("fmovel %/fpiar,%0\n\t"
  212. "fmovel %/fpcr,%1\n\t"
  213. "fmovel %/fpsr,%2\n\t"
  214. "fmovemd %/fp0-%/fp7,%3"
  215. :
  216. : "m" (fpu->fpcntl[0]),
  217. "m" (fpu->fpcntl[1]),
  218. "m" (fpu->fpcntl[2]),
  219. "m" (fpu->fpregs[0])
  220. : "memory");
  221. } else {
  222. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  223. :
  224. : "m" (fpu->fpcntl[0])
  225. : "memory");
  226. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  227. :
  228. : "m" (fpu->fpregs[0])
  229. : "memory");
  230. }
  231. }
  232. return 1;
  233. }
  234. EXPORT_SYMBOL(dump_fpu);
  235. unsigned long __get_wchan(struct task_struct *p)
  236. {
  237. unsigned long fp, pc;
  238. unsigned long stack_page;
  239. int count = 0;
  240. stack_page = (unsigned long)task_stack_page(p);
  241. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  242. do {
  243. if (fp < stack_page+sizeof(struct thread_info) ||
  244. fp >= 8184+stack_page)
  245. return 0;
  246. pc = ((unsigned long *)fp)[1];
  247. if (!in_sched_functions(pc))
  248. return pc;
  249. fp = *(unsigned long *) fp;
  250. } while (count++ < 16);
  251. return 0;
  252. }