syscall.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/fpsimd.h>
  10. #include <asm/syscall.h>
  11. #include <asm/thread_info.h>
  12. #include <asm/unistd.h>
  13. long compat_arm_syscall(struct pt_regs *regs);
  14. long sys_ni_syscall(void);
  15. asmlinkage long do_ni_syscall(struct pt_regs *regs)
  16. {
  17. #ifdef CONFIG_COMPAT
  18. long ret;
  19. if (is_compat_task()) {
  20. ret = compat_arm_syscall(regs);
  21. if (ret != -ENOSYS)
  22. return ret;
  23. }
  24. #endif
  25. return sys_ni_syscall();
  26. }
  27. static long __invoke_syscall(struct pt_regs *regs, syscall_fn_t syscall_fn)
  28. {
  29. return syscall_fn(regs);
  30. }
  31. static void invoke_syscall(struct pt_regs *regs, unsigned int scno,
  32. unsigned int sc_nr,
  33. const syscall_fn_t syscall_table[])
  34. {
  35. long ret;
  36. if (scno < sc_nr) {
  37. syscall_fn_t syscall_fn;
  38. syscall_fn = syscall_table[array_index_nospec(scno, sc_nr)];
  39. ret = __invoke_syscall(regs, syscall_fn);
  40. } else {
  41. ret = do_ni_syscall(regs);
  42. }
  43. regs->regs[0] = ret;
  44. }
  45. static inline bool has_syscall_work(unsigned long flags)
  46. {
  47. return unlikely(flags & _TIF_SYSCALL_WORK);
  48. }
  49. int syscall_trace_enter(struct pt_regs *regs);
  50. void syscall_trace_exit(struct pt_regs *regs);
  51. static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
  52. const syscall_fn_t syscall_table[])
  53. {
  54. unsigned long flags = current_thread_info()->flags;
  55. regs->orig_x0 = regs->regs[0];
  56. regs->syscallno = scno;
  57. local_daif_restore(DAIF_PROCCTX);
  58. user_exit();
  59. if (has_syscall_work(flags)) {
  60. /* set default errno for user-issued syscall(-1) */
  61. if (scno == NO_SYSCALL)
  62. regs->regs[0] = -ENOSYS;
  63. scno = syscall_trace_enter(regs);
  64. if (scno == NO_SYSCALL)
  65. goto trace_exit;
  66. }
  67. invoke_syscall(regs, scno, sc_nr, syscall_table);
  68. /*
  69. * The tracing status may have changed under our feet, so we have to
  70. * check again. However, if we were tracing entry, then we always trace
  71. * exit regardless, as the old entry assembly did.
  72. */
  73. if (!has_syscall_work(flags) && !IS_ENABLED(CONFIG_DEBUG_RSEQ)) {
  74. local_daif_mask();
  75. flags = current_thread_info()->flags;
  76. if (!has_syscall_work(flags)) {
  77. /*
  78. * We're off to userspace, where interrupts are
  79. * always enabled after we restore the flags from
  80. * the SPSR.
  81. */
  82. trace_hardirqs_on();
  83. return;
  84. }
  85. local_daif_restore(DAIF_PROCCTX);
  86. }
  87. trace_exit:
  88. syscall_trace_exit(regs);
  89. }
  90. static inline void sve_user_discard(void)
  91. {
  92. if (!system_supports_sve())
  93. return;
  94. clear_thread_flag(TIF_SVE);
  95. /*
  96. * task_fpsimd_load() won't be called to update CPACR_EL1 in
  97. * ret_to_user unless TIF_FOREIGN_FPSTATE is still set, which only
  98. * happens if a context switch or kernel_neon_begin() or context
  99. * modification (sigreturn, ptrace) intervenes.
  100. * So, ensure that CPACR_EL1 is already correct for the fast-path case.
  101. */
  102. sve_user_disable();
  103. }
  104. asmlinkage void el0_svc_handler(struct pt_regs *regs)
  105. {
  106. sve_user_discard();
  107. el0_svc_common(regs, regs->regs[8], __NR_syscalls, sys_call_table);
  108. }
  109. #ifdef CONFIG_COMPAT
  110. asmlinkage void el0_svc_compat_handler(struct pt_regs *regs)
  111. {
  112. el0_svc_common(regs, regs->regs[7], __NR_compat_syscalls,
  113. compat_sys_call_table);
  114. }
  115. #endif