process.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4. * Chen Liqin <liqin.chen@sunplusct.com>
  5. * Lennox Wu <lennox.wu@sunplusct.com>
  6. * Copyright (C) 2012 Regents of the University of California
  7. * Copyright (C) 2017 SiFive
  8. */
  9. #include <linux/cpu.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/debug.h>
  13. #include <linux/sched/task_stack.h>
  14. #include <linux/tick.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/personality.h>
  18. #include <asm/unistd.h>
  19. #include <asm/processor.h>
  20. #include <asm/csr.h>
  21. #include <asm/stacktrace.h>
  22. #include <asm/string.h>
  23. #include <asm/switch_to.h>
  24. #include <asm/thread_info.h>
  25. #include <asm/cpuidle.h>
  26. #include <asm/vector.h>
  27. #include <asm/cpufeature.h>
  28. #include <asm/exec.h>
  29. #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
  30. #include <linux/stackprotector.h>
  31. unsigned long __stack_chk_guard __read_mostly;
  32. EXPORT_SYMBOL(__stack_chk_guard);
  33. #endif
  34. extern asmlinkage void ret_from_fork(void);
  35. void noinstr arch_cpu_idle(void)
  36. {
  37. cpu_do_idle();
  38. }
  39. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  40. {
  41. if (!unaligned_ctl_available())
  42. return -EINVAL;
  43. tsk->thread.align_ctl = val;
  44. return 0;
  45. }
  46. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  47. {
  48. if (!unaligned_ctl_available())
  49. return -EINVAL;
  50. return put_user(tsk->thread.align_ctl, (unsigned long __user *)adr);
  51. }
  52. void __show_regs(struct pt_regs *regs)
  53. {
  54. show_regs_print_info(KERN_DEFAULT);
  55. if (!user_mode(regs)) {
  56. pr_cont("epc : %pS\n", (void *)regs->epc);
  57. pr_cont(" ra : %pS\n", (void *)regs->ra);
  58. }
  59. pr_cont("epc : " REG_FMT " ra : " REG_FMT " sp : " REG_FMT "\n",
  60. regs->epc, regs->ra, regs->sp);
  61. pr_cont(" gp : " REG_FMT " tp : " REG_FMT " t0 : " REG_FMT "\n",
  62. regs->gp, regs->tp, regs->t0);
  63. pr_cont(" t1 : " REG_FMT " t2 : " REG_FMT " s0 : " REG_FMT "\n",
  64. regs->t1, regs->t2, regs->s0);
  65. pr_cont(" s1 : " REG_FMT " a0 : " REG_FMT " a1 : " REG_FMT "\n",
  66. regs->s1, regs->a0, regs->a1);
  67. pr_cont(" a2 : " REG_FMT " a3 : " REG_FMT " a4 : " REG_FMT "\n",
  68. regs->a2, regs->a3, regs->a4);
  69. pr_cont(" a5 : " REG_FMT " a6 : " REG_FMT " a7 : " REG_FMT "\n",
  70. regs->a5, regs->a6, regs->a7);
  71. pr_cont(" s2 : " REG_FMT " s3 : " REG_FMT " s4 : " REG_FMT "\n",
  72. regs->s2, regs->s3, regs->s4);
  73. pr_cont(" s5 : " REG_FMT " s6 : " REG_FMT " s7 : " REG_FMT "\n",
  74. regs->s5, regs->s6, regs->s7);
  75. pr_cont(" s8 : " REG_FMT " s9 : " REG_FMT " s10: " REG_FMT "\n",
  76. regs->s8, regs->s9, regs->s10);
  77. pr_cont(" s11: " REG_FMT " t3 : " REG_FMT " t4 : " REG_FMT "\n",
  78. regs->s11, regs->t3, regs->t4);
  79. pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT "\n",
  80. regs->t5, regs->t6);
  81. pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n",
  82. regs->status, regs->badaddr, regs->cause);
  83. }
  84. void show_regs(struct pt_regs *regs)
  85. {
  86. __show_regs(regs);
  87. if (!user_mode(regs))
  88. dump_backtrace(regs, NULL, KERN_DEFAULT);
  89. }
  90. unsigned long arch_align_stack(unsigned long sp)
  91. {
  92. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  93. sp -= get_random_u32_below(PAGE_SIZE);
  94. return sp & ~0xf;
  95. }
  96. #ifdef CONFIG_COMPAT
  97. static bool compat_mode_supported __read_mostly;
  98. bool compat_elf_check_arch(Elf32_Ehdr *hdr)
  99. {
  100. return compat_mode_supported &&
  101. hdr->e_machine == EM_RISCV &&
  102. hdr->e_ident[EI_CLASS] == ELFCLASS32;
  103. }
  104. static int __init compat_mode_detect(void)
  105. {
  106. unsigned long tmp = csr_read(CSR_STATUS);
  107. csr_write(CSR_STATUS, (tmp & ~SR_UXL) | SR_UXL_32);
  108. compat_mode_supported =
  109. (csr_read(CSR_STATUS) & SR_UXL) == SR_UXL_32;
  110. csr_write(CSR_STATUS, tmp);
  111. pr_info("riscv: ELF compat mode %s",
  112. compat_mode_supported ? "supported" : "unsupported");
  113. return 0;
  114. }
  115. early_initcall(compat_mode_detect);
  116. #endif
  117. void start_thread(struct pt_regs *regs, unsigned long pc,
  118. unsigned long sp)
  119. {
  120. regs->status = SR_PIE;
  121. if (has_fpu()) {
  122. regs->status |= SR_FS_INITIAL;
  123. /*
  124. * Restore the initial value to the FP register
  125. * before starting the user program.
  126. */
  127. fstate_restore(current, regs);
  128. }
  129. regs->epc = pc;
  130. regs->sp = sp;
  131. #ifdef CONFIG_64BIT
  132. regs->status &= ~SR_UXL;
  133. if (is_compat_task())
  134. regs->status |= SR_UXL_32;
  135. else
  136. regs->status |= SR_UXL_64;
  137. #endif
  138. }
  139. void flush_thread(void)
  140. {
  141. #ifdef CONFIG_FPU
  142. /*
  143. * Reset FPU state and context
  144. * frm: round to nearest, ties to even (IEEE default)
  145. * fflags: accrued exceptions cleared
  146. */
  147. fstate_off(current, task_pt_regs(current));
  148. memset(&current->thread.fstate, 0, sizeof(current->thread.fstate));
  149. #endif
  150. #ifdef CONFIG_RISCV_ISA_V
  151. /* Reset vector state */
  152. riscv_v_vstate_ctrl_init(current);
  153. riscv_v_vstate_off(task_pt_regs(current));
  154. kfree(current->thread.vstate.datap);
  155. memset(&current->thread.vstate, 0, sizeof(struct __riscv_v_ext_state));
  156. clear_tsk_thread_flag(current, TIF_RISCV_V_DEFER_RESTORE);
  157. #endif
  158. }
  159. void arch_release_task_struct(struct task_struct *tsk)
  160. {
  161. /* Free the vector context of datap. */
  162. if (has_vector())
  163. riscv_v_thread_free(tsk);
  164. }
  165. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  166. {
  167. fstate_save(src, task_pt_regs(src));
  168. *dst = *src;
  169. /* clear entire V context, including datap for a new task */
  170. memset(&dst->thread.vstate, 0, sizeof(struct __riscv_v_ext_state));
  171. memset(&dst->thread.kernel_vstate, 0, sizeof(struct __riscv_v_ext_state));
  172. clear_tsk_thread_flag(dst, TIF_RISCV_V_DEFER_RESTORE);
  173. return 0;
  174. }
  175. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  176. {
  177. unsigned long clone_flags = args->flags;
  178. unsigned long usp = args->stack;
  179. unsigned long tls = args->tls;
  180. struct pt_regs *childregs = task_pt_regs(p);
  181. memset(&p->thread.s, 0, sizeof(p->thread.s));
  182. /* p->thread holds context to be restored by __switch_to() */
  183. if (unlikely(args->fn)) {
  184. /* Kernel thread */
  185. memset(childregs, 0, sizeof(struct pt_regs));
  186. /* Supervisor/Machine, irqs on: */
  187. childregs->status = SR_PP | SR_PIE;
  188. p->thread.s[0] = (unsigned long)args->fn;
  189. p->thread.s[1] = (unsigned long)args->fn_arg;
  190. } else {
  191. *childregs = *(current_pt_regs());
  192. /* Turn off status.VS */
  193. riscv_v_vstate_off(childregs);
  194. if (usp) /* User fork */
  195. childregs->sp = usp;
  196. if (clone_flags & CLONE_SETTLS)
  197. childregs->tp = tls;
  198. childregs->a0 = 0; /* Return value of fork() */
  199. p->thread.s[0] = 0;
  200. }
  201. p->thread.riscv_v_flags = 0;
  202. if (has_vector())
  203. riscv_v_thread_alloc(p);
  204. p->thread.ra = (unsigned long)ret_from_fork;
  205. p->thread.sp = (unsigned long)childregs; /* kernel sp */
  206. return 0;
  207. }
  208. void __init arch_task_cache_init(void)
  209. {
  210. riscv_v_setup_ctx_cache();
  211. }