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