syscall.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. regs->regs[0] = ret;
  45. }
  46. static inline bool has_syscall_work(unsigned long flags)
  47. {
  48. return unlikely(flags & _TIF_SYSCALL_WORK);
  49. }
  50. int syscall_trace_enter(struct pt_regs *regs);
  51. void syscall_trace_exit(struct pt_regs *regs);
  52. #ifdef CONFIG_ARM64_ERRATUM_1463225
  53. DECLARE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
  54. static void cortex_a76_erratum_1463225_svc_handler(void)
  55. {
  56. u32 reg, val;
  57. if (!unlikely(test_thread_flag(TIF_SINGLESTEP)))
  58. return;
  59. if (!unlikely(this_cpu_has_cap(ARM64_WORKAROUND_1463225)))
  60. return;
  61. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 1);
  62. reg = read_sysreg(mdscr_el1);
  63. val = reg | DBG_MDSCR_SS | DBG_MDSCR_KDE;
  64. write_sysreg(val, mdscr_el1);
  65. asm volatile("msr daifclr, #8");
  66. isb();
  67. /* We will have taken a single-step exception by this point */
  68. write_sysreg(reg, mdscr_el1);
  69. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 0);
  70. }
  71. #else
  72. static void cortex_a76_erratum_1463225_svc_handler(void) { }
  73. #endif /* CONFIG_ARM64_ERRATUM_1463225 */
  74. static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
  75. const syscall_fn_t syscall_table[])
  76. {
  77. unsigned long flags = current_thread_info()->flags;
  78. regs->orig_x0 = regs->regs[0];
  79. regs->syscallno = scno;
  80. cortex_a76_erratum_1463225_svc_handler();
  81. local_daif_restore(DAIF_PROCCTX);
  82. user_exit();
  83. if (has_syscall_work(flags)) {
  84. /* set default errno for user-issued syscall(-1) */
  85. if (scno == NO_SYSCALL)
  86. regs->regs[0] = -ENOSYS;
  87. scno = syscall_trace_enter(regs);
  88. if (scno == NO_SYSCALL)
  89. goto trace_exit;
  90. }
  91. invoke_syscall(regs, scno, sc_nr, syscall_table);
  92. /*
  93. * The tracing status may have changed under our feet, so we have to
  94. * check again. However, if we were tracing entry, then we always trace
  95. * exit regardless, as the old entry assembly did.
  96. */
  97. if (!has_syscall_work(flags) && !IS_ENABLED(CONFIG_DEBUG_RSEQ)) {
  98. local_daif_mask();
  99. flags = current_thread_info()->flags;
  100. if (!has_syscall_work(flags)) {
  101. /*
  102. * We're off to userspace, where interrupts are
  103. * always enabled after we restore the flags from
  104. * the SPSR.
  105. */
  106. trace_hardirqs_on();
  107. return;
  108. }
  109. local_daif_restore(DAIF_PROCCTX);
  110. }
  111. trace_exit:
  112. syscall_trace_exit(regs);
  113. }
  114. static inline void sve_user_discard(void)
  115. {
  116. if (!system_supports_sve())
  117. return;
  118. clear_thread_flag(TIF_SVE);
  119. /*
  120. * task_fpsimd_load() won't be called to update CPACR_EL1 in
  121. * ret_to_user unless TIF_FOREIGN_FPSTATE is still set, which only
  122. * happens if a context switch or kernel_neon_begin() or context
  123. * modification (sigreturn, ptrace) intervenes.
  124. * So, ensure that CPACR_EL1 is already correct for the fast-path case.
  125. */
  126. sve_user_disable();
  127. }
  128. asmlinkage void el0_svc_handler(struct pt_regs *regs)
  129. {
  130. sve_user_discard();
  131. el0_svc_common(regs, regs->regs[8], __NR_syscalls, sys_call_table);
  132. }
  133. #ifdef CONFIG_COMPAT
  134. asmlinkage void el0_svc_compat_handler(struct pt_regs *regs)
  135. {
  136. el0_svc_common(regs, regs->regs[7], __NR_compat_syscalls,
  137. compat_sys_call_table);
  138. }
  139. #endif