process.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4. * Chen Liqin <[email protected]>
  5. * Lennox Wu <[email protected]>
  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 <asm/unistd.h>
  18. #include <asm/processor.h>
  19. #include <asm/csr.h>
  20. #include <asm/stacktrace.h>
  21. #include <asm/string.h>
  22. #include <asm/switch_to.h>
  23. #include <asm/thread_info.h>
  24. #include <asm/cpuidle.h>
  25. register unsigned long gp_in_global __asm__("gp");
  26. #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
  27. #include <linux/stackprotector.h>
  28. unsigned long __stack_chk_guard __read_mostly;
  29. EXPORT_SYMBOL(__stack_chk_guard);
  30. #endif
  31. extern asmlinkage void ret_from_fork(void);
  32. extern asmlinkage void ret_from_kernel_thread(void);
  33. void arch_cpu_idle(void)
  34. {
  35. cpu_do_idle();
  36. raw_local_irq_enable();
  37. }
  38. void __show_regs(struct pt_regs *regs)
  39. {
  40. show_regs_print_info(KERN_DEFAULT);
  41. if (!user_mode(regs)) {
  42. pr_cont("epc : %pS\n", (void *)regs->epc);
  43. pr_cont(" ra : %pS\n", (void *)regs->ra);
  44. }
  45. pr_cont("epc : " REG_FMT " ra : " REG_FMT " sp : " REG_FMT "\n",
  46. regs->epc, regs->ra, regs->sp);
  47. pr_cont(" gp : " REG_FMT " tp : " REG_FMT " t0 : " REG_FMT "\n",
  48. regs->gp, regs->tp, regs->t0);
  49. pr_cont(" t1 : " REG_FMT " t2 : " REG_FMT " s0 : " REG_FMT "\n",
  50. regs->t1, regs->t2, regs->s0);
  51. pr_cont(" s1 : " REG_FMT " a0 : " REG_FMT " a1 : " REG_FMT "\n",
  52. regs->s1, regs->a0, regs->a1);
  53. pr_cont(" a2 : " REG_FMT " a3 : " REG_FMT " a4 : " REG_FMT "\n",
  54. regs->a2, regs->a3, regs->a4);
  55. pr_cont(" a5 : " REG_FMT " a6 : " REG_FMT " a7 : " REG_FMT "\n",
  56. regs->a5, regs->a6, regs->a7);
  57. pr_cont(" s2 : " REG_FMT " s3 : " REG_FMT " s4 : " REG_FMT "\n",
  58. regs->s2, regs->s3, regs->s4);
  59. pr_cont(" s5 : " REG_FMT " s6 : " REG_FMT " s7 : " REG_FMT "\n",
  60. regs->s5, regs->s6, regs->s7);
  61. pr_cont(" s8 : " REG_FMT " s9 : " REG_FMT " s10: " REG_FMT "\n",
  62. regs->s8, regs->s9, regs->s10);
  63. pr_cont(" s11: " REG_FMT " t3 : " REG_FMT " t4 : " REG_FMT "\n",
  64. regs->s11, regs->t3, regs->t4);
  65. pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT "\n",
  66. regs->t5, regs->t6);
  67. pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n",
  68. regs->status, regs->badaddr, regs->cause);
  69. }
  70. void show_regs(struct pt_regs *regs)
  71. {
  72. __show_regs(regs);
  73. if (!user_mode(regs))
  74. dump_backtrace(regs, NULL, KERN_DEFAULT);
  75. }
  76. #ifdef CONFIG_COMPAT
  77. static bool compat_mode_supported __read_mostly;
  78. bool compat_elf_check_arch(Elf32_Ehdr *hdr)
  79. {
  80. return compat_mode_supported &&
  81. hdr->e_machine == EM_RISCV &&
  82. hdr->e_ident[EI_CLASS] == ELFCLASS32;
  83. }
  84. static int __init compat_mode_detect(void)
  85. {
  86. unsigned long tmp = csr_read(CSR_STATUS);
  87. csr_write(CSR_STATUS, (tmp & ~SR_UXL) | SR_UXL_32);
  88. compat_mode_supported =
  89. (csr_read(CSR_STATUS) & SR_UXL) == SR_UXL_32;
  90. csr_write(CSR_STATUS, tmp);
  91. pr_info("riscv: ELF compat mode %s",
  92. compat_mode_supported ? "supported" : "unsupported");
  93. return 0;
  94. }
  95. early_initcall(compat_mode_detect);
  96. #endif
  97. void start_thread(struct pt_regs *regs, unsigned long pc,
  98. unsigned long sp)
  99. {
  100. regs->status = SR_PIE;
  101. if (has_fpu()) {
  102. regs->status |= SR_FS_INITIAL;
  103. /*
  104. * Restore the initial value to the FP register
  105. * before starting the user program.
  106. */
  107. fstate_restore(current, regs);
  108. }
  109. regs->epc = pc;
  110. regs->sp = sp;
  111. #ifdef CONFIG_64BIT
  112. regs->status &= ~SR_UXL;
  113. if (is_compat_task())
  114. regs->status |= SR_UXL_32;
  115. else
  116. regs->status |= SR_UXL_64;
  117. #endif
  118. }
  119. void flush_thread(void)
  120. {
  121. #ifdef CONFIG_FPU
  122. /*
  123. * Reset FPU state and context
  124. * frm: round to nearest, ties to even (IEEE default)
  125. * fflags: accrued exceptions cleared
  126. */
  127. fstate_off(current, task_pt_regs(current));
  128. memset(&current->thread.fstate, 0, sizeof(current->thread.fstate));
  129. #endif
  130. }
  131. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  132. {
  133. fstate_save(src, task_pt_regs(src));
  134. *dst = *src;
  135. return 0;
  136. }
  137. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  138. {
  139. unsigned long clone_flags = args->flags;
  140. unsigned long usp = args->stack;
  141. unsigned long tls = args->tls;
  142. struct pt_regs *childregs = task_pt_regs(p);
  143. memset(&p->thread.s, 0, sizeof(p->thread.s));
  144. /* p->thread holds context to be restored by __switch_to() */
  145. if (unlikely(args->fn)) {
  146. /* Kernel thread */
  147. memset(childregs, 0, sizeof(struct pt_regs));
  148. childregs->gp = gp_in_global;
  149. /* Supervisor/Machine, irqs on: */
  150. childregs->status = SR_PP | SR_PIE;
  151. p->thread.ra = (unsigned long)ret_from_kernel_thread;
  152. p->thread.s[0] = (unsigned long)args->fn;
  153. p->thread.s[1] = (unsigned long)args->fn_arg;
  154. } else {
  155. *childregs = *(current_pt_regs());
  156. if (usp) /* User fork */
  157. childregs->sp = usp;
  158. if (clone_flags & CLONE_SETTLS)
  159. childregs->tp = tls;
  160. childregs->a0 = 0; /* Return value of fork() */
  161. p->thread.ra = (unsigned long)ret_from_fork;
  162. }
  163. p->thread.sp = (unsigned long)childregs; /* kernel sp */
  164. return 0;
  165. }