process.c 9.2 KB

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