entry-common.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef _ASM_X86_ENTRY_COMMON_H
  3. #define _ASM_X86_ENTRY_COMMON_H
  4. #include <linux/randomize_kstack.h>
  5. #include <linux/user-return-notifier.h>
  6. #include <asm/nospec-branch.h>
  7. #include <asm/io_bitmap.h>
  8. #include <asm/fpu/api.h>
  9. /* Check that the stack and regs on entry from user mode are sane. */
  10. static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs)
  11. {
  12. if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
  13. /*
  14. * Make sure that the entry code gave us a sensible EFLAGS
  15. * register. Native because we want to check the actual CPU
  16. * state, not the interrupt state as imagined by Xen.
  17. */
  18. unsigned long flags = native_save_fl();
  19. unsigned long mask = X86_EFLAGS_DF | X86_EFLAGS_NT;
  20. /*
  21. * For !SMAP hardware we patch out CLAC on entry.
  22. */
  23. if (boot_cpu_has(X86_FEATURE_SMAP) ||
  24. (IS_ENABLED(CONFIG_64BIT) && boot_cpu_has(X86_FEATURE_XENPV)))
  25. mask |= X86_EFLAGS_AC;
  26. WARN_ON_ONCE(flags & mask);
  27. /* We think we came from user mode. Make sure pt_regs agrees. */
  28. WARN_ON_ONCE(!user_mode(regs));
  29. /*
  30. * All entries from user mode (except #DF) should be on the
  31. * normal thread stack and should have user pt_regs in the
  32. * correct location.
  33. */
  34. WARN_ON_ONCE(!on_thread_stack());
  35. WARN_ON_ONCE(regs != task_pt_regs(current));
  36. }
  37. }
  38. #define arch_enter_from_user_mode arch_enter_from_user_mode
  39. static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
  40. unsigned long ti_work)
  41. {
  42. if (ti_work & _TIF_USER_RETURN_NOTIFY)
  43. fire_user_return_notifiers();
  44. if (unlikely(ti_work & _TIF_IO_BITMAP))
  45. tss_update_io_bitmap();
  46. fpregs_assert_state_consistent();
  47. if (unlikely(ti_work & _TIF_NEED_FPU_LOAD))
  48. switch_fpu_return();
  49. #ifdef CONFIG_COMPAT
  50. /*
  51. * Compat syscalls set TS_COMPAT. Make sure we clear it before
  52. * returning to user mode. We need to clear it *after* signal
  53. * handling, because syscall restart has a fixup for compat
  54. * syscalls. The fixup is exercised by the ptrace_syscall_32
  55. * selftest.
  56. *
  57. * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
  58. * special case only applies after poking regs and before the
  59. * very next return to user mode.
  60. */
  61. current_thread_info()->status &= ~(TS_COMPAT | TS_I386_REGS_POKED);
  62. #endif
  63. /*
  64. * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
  65. * but not enough for x86 stack utilization comfort. To keep
  66. * reasonable stack head room, reduce the maximum offset to 8 bits.
  67. *
  68. * The actual entropy will be further reduced by the compiler when
  69. * applying stack alignment constraints (see cc_stack_align4/8 in
  70. * arch/x86/Makefile), which will remove the 3 (x86_64) or 2 (ia32)
  71. * low bits from any entropy chosen here.
  72. *
  73. * Therefore, final stack offset entropy will be 5 (x86_64) or
  74. * 6 (ia32) bits.
  75. */
  76. choose_random_kstack_offset(rdtsc() & 0xFF);
  77. }
  78. #define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare
  79. static __always_inline void arch_exit_to_user_mode(void)
  80. {
  81. mds_user_clear_cpu_buffers();
  82. amd_clear_divider();
  83. }
  84. #define arch_exit_to_user_mode arch_exit_to_user_mode
  85. #endif