syscall.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <linux/context_tracking.h>
  4. #include <linux/errno.h>
  5. #include <linux/nospec.h>
  6. #include <linux/ptrace.h>
  7. #include <linux/syscalls.h>
  8. #include <asm/daifflags.h>
  9. #include <asm/debug-monitors.h>
  10. #include <asm/fpsimd.h>
  11. #include <asm/syscall.h>
  12. #include <asm/thread_info.h>
  13. #include <asm/unistd.h>
  14. long compat_arm_syscall(struct pt_regs *regs, int scno);
  15. long sys_ni_syscall(void);
  16. static long do_ni_syscall(struct pt_regs *regs, int scno)
  17. {
  18. #ifdef CONFIG_COMPAT
  19. long ret;
  20. if (is_compat_task()) {
  21. ret = compat_arm_syscall(regs, scno);
  22. if (ret != -ENOSYS)
  23. return ret;
  24. }
  25. #endif
  26. return sys_ni_syscall();
  27. }
  28. static long __invoke_syscall(struct pt_regs *regs, syscall_fn_t syscall_fn)
  29. {
  30. return syscall_fn(regs);
  31. }
  32. static void invoke_syscall(struct pt_regs *regs, unsigned int scno,
  33. unsigned int sc_nr,
  34. const syscall_fn_t syscall_table[])
  35. {
  36. long ret;
  37. if (scno < sc_nr) {
  38. syscall_fn_t syscall_fn;
  39. syscall_fn = syscall_table[array_index_nospec(scno, sc_nr)];
  40. ret = __invoke_syscall(regs, syscall_fn);
  41. } else {
  42. ret = do_ni_syscall(regs, scno);
  43. }
  44. if (is_compat_task())
  45. ret = lower_32_bits(ret);
  46. regs->regs[0] = ret;
  47. }
  48. static inline bool has_syscall_work(unsigned long flags)
  49. {
  50. return unlikely(flags & _TIF_SYSCALL_WORK);
  51. }
  52. int syscall_trace_enter(struct pt_regs *regs);
  53. void syscall_trace_exit(struct pt_regs *regs);
  54. #ifdef CONFIG_ARM64_ERRATUM_1463225
  55. DECLARE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
  56. static void cortex_a76_erratum_1463225_svc_handler(void)
  57. {
  58. u32 reg, val;
  59. if (!unlikely(test_thread_flag(TIF_SINGLESTEP)))
  60. return;
  61. if (!unlikely(this_cpu_has_cap(ARM64_WORKAROUND_1463225)))
  62. return;
  63. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 1);
  64. reg = read_sysreg(mdscr_el1);
  65. val = reg | DBG_MDSCR_SS | DBG_MDSCR_KDE;
  66. write_sysreg(val, mdscr_el1);
  67. asm volatile("msr daifclr, #8");
  68. isb();
  69. /* We will have taken a single-step exception by this point */
  70. write_sysreg(reg, mdscr_el1);
  71. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 0);
  72. }
  73. #else
  74. static void cortex_a76_erratum_1463225_svc_handler(void) { }
  75. #endif /* CONFIG_ARM64_ERRATUM_1463225 */
  76. static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
  77. const syscall_fn_t syscall_table[])
  78. {
  79. unsigned long flags = current_thread_info()->flags;
  80. regs->orig_x0 = regs->regs[0];
  81. regs->syscallno = scno;
  82. cortex_a76_erratum_1463225_svc_handler();
  83. user_exit_irqoff();
  84. local_daif_restore(DAIF_PROCCTX);
  85. if (has_syscall_work(flags)) {
  86. /* set default errno for user-issued syscall(-1) */
  87. if (scno == NO_SYSCALL)
  88. regs->regs[0] = -ENOSYS;
  89. scno = syscall_trace_enter(regs);
  90. if (scno == NO_SYSCALL)
  91. goto trace_exit;
  92. }
  93. invoke_syscall(regs, scno, sc_nr, syscall_table);
  94. /*
  95. * The tracing status may have changed under our feet, so we have to
  96. * check again. However, if we were tracing entry, then we always trace
  97. * exit regardless, as the old entry assembly did.
  98. */
  99. if (!has_syscall_work(flags) && !IS_ENABLED(CONFIG_DEBUG_RSEQ)) {
  100. local_daif_mask();
  101. flags = current_thread_info()->flags;
  102. if (!has_syscall_work(flags) && !(flags & _TIF_SINGLESTEP)) {
  103. /*
  104. * We're off to userspace, where interrupts are
  105. * always enabled after we restore the flags from
  106. * the SPSR.
  107. */
  108. trace_hardirqs_on();
  109. return;
  110. }
  111. local_daif_restore(DAIF_PROCCTX);
  112. }
  113. trace_exit:
  114. syscall_trace_exit(regs);
  115. }
  116. static inline void sve_user_discard(void)
  117. {
  118. if (!system_supports_sve())
  119. return;
  120. clear_thread_flag(TIF_SVE);
  121. /*
  122. * task_fpsimd_load() won't be called to update CPACR_EL1 in
  123. * ret_to_user unless TIF_FOREIGN_FPSTATE is still set, which only
  124. * happens if a context switch or kernel_neon_begin() or context
  125. * modification (sigreturn, ptrace) intervenes.
  126. * So, ensure that CPACR_EL1 is already correct for the fast-path case.
  127. */
  128. sve_user_disable();
  129. }
  130. asmlinkage void el0_svc_handler(struct pt_regs *regs)
  131. {
  132. sve_user_discard();
  133. el0_svc_common(regs, regs->regs[8], __NR_syscalls, sys_call_table);
  134. }
  135. #ifdef CONFIG_COMPAT
  136. asmlinkage void el0_svc_compat_handler(struct pt_regs *regs)
  137. {
  138. el0_svc_common(regs, regs->regs[7], __NR_compat_syscalls,
  139. compat_sys_call_table);
  140. }
  141. #endif