blob: 5393babc05989ebc0cbcbbb21251f2c241e3df04 [file] [log] [blame]
Thomas Gleixner28a27752018-04-29 15:01:37 +02001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_SPECCTRL_H_
3#define _ASM_X86_SPECCTRL_H_
4
Thomas Gleixner885f82b2018-04-29 15:21:42 +02005#include <linux/thread_info.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +02006#include <asm/nospec-branch.h>
7
8/*
9 * On VMENTER we must preserve whatever view of the SPEC_CTRL MSR
10 * the guest has, while on VMEXIT we restore the host view. This
11 * would be easier if SPEC_CTRL were architecturally maskable or
12 * shadowable for guests but this is not (currently) the case.
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020013 * Takes the guest view of SPEC_CTRL MSR as a parameter and also
14 * the guest's version of VIRT_SPEC_CTRL, if emulated.
Thomas Gleixner28a27752018-04-29 15:01:37 +020015 */
Borislav Petkovcc69b342018-05-12 00:14:51 +020016extern void x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool guest);
17
18/**
19 * x86_spec_ctrl_set_guest - Set speculation control registers for the guest
20 * @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
21 * @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
22 * (may get translated to MSR_AMD64_LS_CFG bits)
23 *
24 * Avoids writing to the MSR if the content/bits are the same
25 */
26static inline
27void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
28{
29 x86_virt_spec_ctrl(guest_spec_ctrl, guest_virt_spec_ctrl, true);
30}
31
32/**
33 * x86_spec_ctrl_restore_host - Restore host speculation control registers
34 * @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
35 * @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
36 * (may get translated to MSR_AMD64_LS_CFG bits)
37 *
38 * Avoids writing to the MSR if the content/bits are the same
39 */
40static inline
41void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
42{
43 x86_virt_spec_ctrl(guest_spec_ctrl, guest_virt_spec_ctrl, false);
44}
Thomas Gleixner28a27752018-04-29 15:01:37 +020045
46/* AMD specific Speculative Store Bypass MSR data */
47extern u64 x86_amd_ls_cfg_base;
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +020048extern u64 x86_amd_ls_cfg_ssbd_mask;
Thomas Gleixner28a27752018-04-29 15:01:37 +020049
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +020050static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
Thomas Gleixner885f82b2018-04-29 15:21:42 +020051{
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +020052 BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
53 return (tifn & _TIF_SSBD) >> (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
Thomas Gleixner885f82b2018-04-29 15:21:42 +020054}
55
Tim Chen5bfbe3a2018-11-25 19:33:46 +010056static inline u64 stibp_tif_to_spec_ctrl(u64 tifn)
57{
58 BUILD_BUG_ON(TIF_SPEC_IB < SPEC_CTRL_STIBP_SHIFT);
59 return (tifn & _TIF_SPEC_IB) >> (TIF_SPEC_IB - SPEC_CTRL_STIBP_SHIFT);
60}
61
Thomas Gleixner47c61b32018-05-10 20:42:48 +020062static inline unsigned long ssbd_spec_ctrl_to_tif(u64 spec_ctrl)
63{
64 BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
65 return (spec_ctrl & SPEC_CTRL_SSBD) << (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
66}
67
Tim Chen5bfbe3a2018-11-25 19:33:46 +010068static inline unsigned long stibp_spec_ctrl_to_tif(u64 spec_ctrl)
69{
70 BUILD_BUG_ON(TIF_SPEC_IB < SPEC_CTRL_STIBP_SHIFT);
71 return (spec_ctrl & SPEC_CTRL_STIBP) << (TIF_SPEC_IB - SPEC_CTRL_STIBP_SHIFT);
72}
73
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +020074static inline u64 ssbd_tif_to_amd_ls_cfg(u64 tifn)
Thomas Gleixner885f82b2018-04-29 15:21:42 +020075{
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +020076 return (tifn & _TIF_SSBD) ? x86_amd_ls_cfg_ssbd_mask : 0ULL;
Thomas Gleixner885f82b2018-04-29 15:21:42 +020077}
78
Thomas Gleixner1f50ddb2018-05-09 21:53:09 +020079#ifdef CONFIG_SMP
80extern void speculative_store_bypass_ht_init(void);
81#else
82static inline void speculative_store_bypass_ht_init(void) { }
83#endif
84
Thomas Gleixner26c4d752018-11-25 19:33:34 +010085extern void speculation_ctrl_update(unsigned long tif);
Thomas Gleixner6d991ba2018-11-28 10:56:57 +010086extern void speculation_ctrl_update_current(void);
Thomas Gleixner885f82b2018-04-29 15:21:42 +020087
Thomas Gleixner28a27752018-04-29 15:01:37 +020088#endif