process.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: Huacai Chen <[email protected]>
  4. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  5. *
  6. * Derived from MIPS:
  7. * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others.
  8. * Copyright (C) 2005, 2006 by Ralf Baechle ([email protected])
  9. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  10. * Copyright (C) 2004 Thiemo Seufer
  11. * Copyright (C) 2013 Imagination Technologies Ltd.
  12. */
  13. #include <linux/cpu.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/sched/debug.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/sched/task_stack.h>
  21. #include <linux/mm.h>
  22. #include <linux/stddef.h>
  23. #include <linux/unistd.h>
  24. #include <linux/export.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/mman.h>
  27. #include <linux/personality.h>
  28. #include <linux/sys.h>
  29. #include <linux/completion.h>
  30. #include <linux/kallsyms.h>
  31. #include <linux/random.h>
  32. #include <linux/prctl.h>
  33. #include <linux/nmi.h>
  34. #include <asm/asm.h>
  35. #include <asm/bootinfo.h>
  36. #include <asm/cpu.h>
  37. #include <asm/elf.h>
  38. #include <asm/fpu.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/irq_regs.h>
  42. #include <asm/loongarch.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/processor.h>
  45. #include <asm/reg.h>
  46. #include <asm/unwind.h>
  47. #include <asm/vdso.h>
  48. /*
  49. * Idle related variables and functions
  50. */
  51. unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
  52. EXPORT_SYMBOL(boot_option_idle_override);
  53. #ifdef CONFIG_HOTPLUG_CPU
  54. void arch_cpu_idle_dead(void)
  55. {
  56. play_dead();
  57. }
  58. #endif
  59. asmlinkage void ret_from_fork(void);
  60. asmlinkage void ret_from_kernel_thread(void);
  61. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
  62. {
  63. unsigned long crmd;
  64. unsigned long prmd;
  65. unsigned long euen;
  66. /* New thread loses kernel privileges. */
  67. crmd = regs->csr_crmd & ~(PLV_MASK);
  68. crmd |= PLV_USER;
  69. regs->csr_crmd = crmd;
  70. prmd = regs->csr_prmd & ~(PLV_MASK);
  71. prmd |= PLV_USER;
  72. regs->csr_prmd = prmd;
  73. euen = regs->csr_euen & ~(CSR_EUEN_FPEN);
  74. regs->csr_euen = euen;
  75. lose_fpu(0);
  76. clear_thread_flag(TIF_LSX_CTX_LIVE);
  77. clear_thread_flag(TIF_LASX_CTX_LIVE);
  78. clear_used_math();
  79. regs->csr_era = pc;
  80. regs->regs[3] = sp;
  81. }
  82. void exit_thread(struct task_struct *tsk)
  83. {
  84. }
  85. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  86. {
  87. /*
  88. * Save any process state which is live in hardware registers to the
  89. * parent context prior to duplication. This prevents the new child
  90. * state becoming stale if the parent is preempted before copy_thread()
  91. * gets a chance to save the parent's live hardware registers to the
  92. * child context.
  93. */
  94. preempt_disable();
  95. if (is_fpu_owner())
  96. save_fp(current);
  97. preempt_enable();
  98. if (used_math())
  99. memcpy(dst, src, sizeof(struct task_struct));
  100. else
  101. memcpy(dst, src, offsetof(struct task_struct, thread.fpu.fpr));
  102. return 0;
  103. }
  104. /*
  105. * Copy architecture-specific thread state
  106. */
  107. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  108. {
  109. unsigned long childksp;
  110. unsigned long tls = args->tls;
  111. unsigned long usp = args->stack;
  112. unsigned long clone_flags = args->flags;
  113. struct pt_regs *childregs, *regs = current_pt_regs();
  114. childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  115. /* set up new TSS. */
  116. childregs = (struct pt_regs *) childksp - 1;
  117. /* Put the stack after the struct pt_regs. */
  118. childksp = (unsigned long) childregs;
  119. p->thread.sched_cfa = 0;
  120. p->thread.csr_euen = 0;
  121. p->thread.csr_crmd = csr_read32(LOONGARCH_CSR_CRMD);
  122. p->thread.csr_prmd = csr_read32(LOONGARCH_CSR_PRMD);
  123. p->thread.csr_ecfg = csr_read32(LOONGARCH_CSR_ECFG);
  124. if (unlikely(args->fn)) {
  125. /* kernel thread */
  126. p->thread.reg03 = childksp;
  127. p->thread.reg23 = (unsigned long)args->fn;
  128. p->thread.reg24 = (unsigned long)args->fn_arg;
  129. p->thread.reg01 = (unsigned long)ret_from_kernel_thread;
  130. p->thread.sched_ra = (unsigned long)ret_from_kernel_thread;
  131. memset(childregs, 0, sizeof(struct pt_regs));
  132. childregs->csr_euen = p->thread.csr_euen;
  133. childregs->csr_crmd = p->thread.csr_crmd;
  134. childregs->csr_prmd = p->thread.csr_prmd;
  135. childregs->csr_ecfg = p->thread.csr_ecfg;
  136. goto out;
  137. }
  138. /* user thread */
  139. *childregs = *regs;
  140. childregs->regs[4] = 0; /* Child gets zero as return value */
  141. if (usp)
  142. childregs->regs[3] = usp;
  143. p->thread.reg03 = (unsigned long) childregs;
  144. p->thread.reg01 = (unsigned long) ret_from_fork;
  145. p->thread.sched_ra = (unsigned long) ret_from_fork;
  146. /*
  147. * New tasks lose permission to use the fpu. This accelerates context
  148. * switching for most programs since they don't use the fpu.
  149. */
  150. childregs->csr_euen = 0;
  151. if (clone_flags & CLONE_SETTLS)
  152. childregs->regs[2] = tls;
  153. out:
  154. clear_tsk_thread_flag(p, TIF_USEDFPU);
  155. clear_tsk_thread_flag(p, TIF_USEDSIMD);
  156. clear_tsk_thread_flag(p, TIF_LSX_CTX_LIVE);
  157. clear_tsk_thread_flag(p, TIF_LASX_CTX_LIVE);
  158. return 0;
  159. }
  160. unsigned long __get_wchan(struct task_struct *task)
  161. {
  162. unsigned long pc = 0;
  163. struct unwind_state state;
  164. if (!try_get_task_stack(task))
  165. return 0;
  166. for (unwind_start(&state, task, NULL);
  167. !unwind_done(&state); unwind_next_frame(&state)) {
  168. pc = unwind_get_return_address(&state);
  169. if (!pc)
  170. break;
  171. if (in_sched_functions(pc))
  172. continue;
  173. break;
  174. }
  175. put_task_stack(task);
  176. return pc;
  177. }
  178. bool in_irq_stack(unsigned long stack, struct stack_info *info)
  179. {
  180. unsigned long nextsp;
  181. unsigned long begin = (unsigned long)this_cpu_read(irq_stack);
  182. unsigned long end = begin + IRQ_STACK_START;
  183. if (stack < begin || stack >= end)
  184. return false;
  185. nextsp = *(unsigned long *)end;
  186. if (nextsp & (SZREG - 1))
  187. return false;
  188. info->begin = begin;
  189. info->end = end;
  190. info->next_sp = nextsp;
  191. info->type = STACK_TYPE_IRQ;
  192. return true;
  193. }
  194. bool in_task_stack(unsigned long stack, struct task_struct *task,
  195. struct stack_info *info)
  196. {
  197. unsigned long begin = (unsigned long)task_stack_page(task);
  198. unsigned long end = begin + THREAD_SIZE;
  199. if (stack < begin || stack >= end)
  200. return false;
  201. info->begin = begin;
  202. info->end = end;
  203. info->next_sp = 0;
  204. info->type = STACK_TYPE_TASK;
  205. return true;
  206. }
  207. int get_stack_info(unsigned long stack, struct task_struct *task,
  208. struct stack_info *info)
  209. {
  210. task = task ? : current;
  211. if (!stack || stack & (SZREG - 1))
  212. goto unknown;
  213. if (in_task_stack(stack, task, info))
  214. return 0;
  215. if (task != current)
  216. goto unknown;
  217. if (in_irq_stack(stack, info))
  218. return 0;
  219. unknown:
  220. info->type = STACK_TYPE_UNKNOWN;
  221. return -EINVAL;
  222. }
  223. unsigned long stack_top(void)
  224. {
  225. unsigned long top = TASK_SIZE & PAGE_MASK;
  226. /* Space for the VDSO & data page */
  227. top -= PAGE_ALIGN(current->thread.vdso->size);
  228. top -= PAGE_SIZE;
  229. /* Space to randomize the VDSO base */
  230. if (current->flags & PF_RANDOMIZE)
  231. top -= VDSO_RANDOMIZE_SIZE;
  232. return top;
  233. }
  234. /*
  235. * Don't forget that the stack pointer must be aligned on a 8 bytes
  236. * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
  237. */
  238. unsigned long arch_align_stack(unsigned long sp)
  239. {
  240. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  241. sp -= prandom_u32_max(PAGE_SIZE);
  242. return sp & STACK_ALIGN;
  243. }
  244. static DEFINE_PER_CPU(call_single_data_t, backtrace_csd);
  245. static struct cpumask backtrace_csd_busy;
  246. static void handle_backtrace(void *info)
  247. {
  248. nmi_cpu_backtrace(get_irq_regs());
  249. cpumask_clear_cpu(smp_processor_id(), &backtrace_csd_busy);
  250. }
  251. static void raise_backtrace(cpumask_t *mask)
  252. {
  253. call_single_data_t *csd;
  254. int cpu;
  255. for_each_cpu(cpu, mask) {
  256. /*
  257. * If we previously sent an IPI to the target CPU & it hasn't
  258. * cleared its bit in the busy cpumask then it didn't handle
  259. * our previous IPI & it's not safe for us to reuse the
  260. * call_single_data_t.
  261. */
  262. if (cpumask_test_and_set_cpu(cpu, &backtrace_csd_busy)) {
  263. pr_warn("Unable to send backtrace IPI to CPU%u - perhaps it hung?\n",
  264. cpu);
  265. continue;
  266. }
  267. csd = &per_cpu(backtrace_csd, cpu);
  268. csd->func = handle_backtrace;
  269. smp_call_function_single_async(cpu, csd);
  270. }
  271. }
  272. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
  273. {
  274. nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace);
  275. }
  276. #ifdef CONFIG_64BIT
  277. void loongarch_dump_regs64(u64 *uregs, const struct pt_regs *regs)
  278. {
  279. unsigned int i;
  280. for (i = LOONGARCH_EF_R1; i <= LOONGARCH_EF_R31; i++) {
  281. uregs[i] = regs->regs[i - LOONGARCH_EF_R0];
  282. }
  283. uregs[LOONGARCH_EF_ORIG_A0] = regs->orig_a0;
  284. uregs[LOONGARCH_EF_CSR_ERA] = regs->csr_era;
  285. uregs[LOONGARCH_EF_CSR_BADV] = regs->csr_badvaddr;
  286. uregs[LOONGARCH_EF_CSR_CRMD] = regs->csr_crmd;
  287. uregs[LOONGARCH_EF_CSR_PRMD] = regs->csr_prmd;
  288. uregs[LOONGARCH_EF_CSR_EUEN] = regs->csr_euen;
  289. uregs[LOONGARCH_EF_CSR_ECFG] = regs->csr_ecfg;
  290. uregs[LOONGARCH_EF_CSR_ESTAT] = regs->csr_estat;
  291. }
  292. #endif /* CONFIG_64BIT */