spec-ctrl.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_SPECCTRL_H_
  3. #define _ASM_X86_SPECCTRL_H_
  4. #include <linux/thread_info.h>
  5. #include <asm/nospec-branch.h>
  6. /*
  7. * On VMENTER we must preserve whatever view of the SPEC_CTRL MSR
  8. * the guest has, while on VMEXIT we restore the host view. This
  9. * would be easier if SPEC_CTRL were architecturally maskable or
  10. * shadowable for guests but this is not (currently) the case.
  11. * Takes the guest view of SPEC_CTRL MSR as a parameter and also
  12. * the guest's version of VIRT_SPEC_CTRL, if emulated.
  13. */
  14. extern void x86_virt_spec_ctrl(u64 guest_virt_spec_ctrl, bool guest);
  15. /**
  16. * x86_spec_ctrl_set_guest - Set speculation control registers for the guest
  17. * @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
  18. * @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
  19. * (may get translated to MSR_AMD64_LS_CFG bits)
  20. *
  21. * Avoids writing to the MSR if the content/bits are the same
  22. */
  23. static inline
  24. void x86_spec_ctrl_set_guest(u64 guest_virt_spec_ctrl)
  25. {
  26. x86_virt_spec_ctrl(guest_virt_spec_ctrl, true);
  27. }
  28. /**
  29. * x86_spec_ctrl_restore_host - Restore host speculation control registers
  30. * @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
  31. * @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
  32. * (may get translated to MSR_AMD64_LS_CFG bits)
  33. *
  34. * Avoids writing to the MSR if the content/bits are the same
  35. */
  36. static inline
  37. void x86_spec_ctrl_restore_host(u64 guest_virt_spec_ctrl)
  38. {
  39. x86_virt_spec_ctrl(guest_virt_spec_ctrl, false);
  40. }
  41. /* AMD specific Speculative Store Bypass MSR data */
  42. extern u64 x86_amd_ls_cfg_base;
  43. extern u64 x86_amd_ls_cfg_ssbd_mask;
  44. static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
  45. {
  46. BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
  47. return (tifn & _TIF_SSBD) >> (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
  48. }
  49. static inline u64 stibp_tif_to_spec_ctrl(u64 tifn)
  50. {
  51. BUILD_BUG_ON(TIF_SPEC_IB < SPEC_CTRL_STIBP_SHIFT);
  52. return (tifn & _TIF_SPEC_IB) >> (TIF_SPEC_IB - SPEC_CTRL_STIBP_SHIFT);
  53. }
  54. static inline unsigned long ssbd_spec_ctrl_to_tif(u64 spec_ctrl)
  55. {
  56. BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
  57. return (spec_ctrl & SPEC_CTRL_SSBD) << (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
  58. }
  59. static inline unsigned long stibp_spec_ctrl_to_tif(u64 spec_ctrl)
  60. {
  61. BUILD_BUG_ON(TIF_SPEC_IB < SPEC_CTRL_STIBP_SHIFT);
  62. return (spec_ctrl & SPEC_CTRL_STIBP) << (TIF_SPEC_IB - SPEC_CTRL_STIBP_SHIFT);
  63. }
  64. static inline u64 ssbd_tif_to_amd_ls_cfg(u64 tifn)
  65. {
  66. return (tifn & _TIF_SSBD) ? x86_amd_ls_cfg_ssbd_mask : 0ULL;
  67. }
  68. #ifdef CONFIG_SMP
  69. extern void speculative_store_bypass_ht_init(void);
  70. #else
  71. static inline void speculative_store_bypass_ht_init(void) { }
  72. #endif
  73. extern void speculation_ctrl_update(unsigned long tif);
  74. extern void speculation_ctrl_update_current(void);
  75. #endif