Skip to content

Commit ea99935

Browse files
suryasaimadhugregkh
authored andcommitted
x86/bugs: Unify x86_spec_ctrl_{set_guest,restore_host}
commit cc69b34 upstream Function bodies are very similar and are going to grow more almost identical code. Add a bool arg to determine whether SPEC_CTRL is being set for the guest or restored to the host. No functional changes. Signed-off-by: Borislav Petkov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Konrad Rzeszutek Wilk <[email protected]> Signed-off-by: David Woodhouse <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b7b8440 commit ea99935

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

arch/x86/include/asm/spec-ctrl.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,35 @@
1313
* Takes the guest view of SPEC_CTRL MSR as a parameter and also
1414
* the guest's version of VIRT_SPEC_CTRL, if emulated.
1515
*/
16-
extern void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl,
17-
u64 guest_virt_spec_ctrl);
18-
extern void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl,
19-
u64 guest_virt_spec_ctrl);
16+
extern 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+
*/
26+
static inline
27+
void 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+
*/
40+
static inline
41+
void 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+
}
2045

2146
/* AMD specific Speculative Store Bypass MSR data */
2247
extern u64 x86_amd_ls_cfg_base;

arch/x86/kernel/cpu/bugs.c

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -150,55 +150,25 @@ u64 x86_spec_ctrl_get_default(void)
150150
}
151151
EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
152152

153-
/**
154-
* x86_spec_ctrl_set_guest - Set speculation control registers for the guest
155-
* @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
156-
* @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
157-
* (may get translated to MSR_AMD64_LS_CFG bits)
158-
*
159-
* Avoids writing to the MSR if the content/bits are the same
160-
*/
161-
void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
153+
void
154+
x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
162155
{
163-
u64 host = x86_spec_ctrl_base;
156+
struct thread_info *ti = current_thread_info();
157+
u64 msr, host = x86_spec_ctrl_base;
164158

165159
/* Is MSR_SPEC_CTRL implemented ? */
166-
if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
167-
return;
168-
169-
/* SSBD controlled in MSR_SPEC_CTRL */
170-
if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
171-
host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
172-
173-
if (host != guest_spec_ctrl)
174-
wrmsrl(MSR_IA32_SPEC_CTRL, guest_spec_ctrl);
175-
}
176-
EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest);
177-
178-
/**
179-
* x86_spec_ctrl_restore_host - Restore host speculation control registers
180-
* @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
181-
* @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
182-
* (may get translated to MSR_AMD64_LS_CFG bits)
183-
*
184-
* Avoids writing to the MSR if the content/bits are the same
185-
*/
186-
void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
187-
{
188-
u64 host = x86_spec_ctrl_base;
189-
190-
/* Is MSR_SPEC_CTRL implemented ? */
191-
if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
192-
return;
193-
194-
/* SSBD controlled in MSR_SPEC_CTRL */
195-
if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
196-
host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
197-
198-
if (host != guest_spec_ctrl)
199-
wrmsrl(MSR_IA32_SPEC_CTRL, host);
160+
if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
161+
/* SSBD controlled in MSR_SPEC_CTRL */
162+
if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
163+
host |= ssbd_tif_to_spec_ctrl(ti->flags);
164+
165+
if (host != guest_spec_ctrl) {
166+
msr = setguest ? guest_spec_ctrl : host;
167+
wrmsrl(MSR_IA32_SPEC_CTRL, msr);
168+
}
169+
}
200170
}
201-
EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host);
171+
EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
202172

203173
static void x86_amd_ssb_disable(void)
204174
{

0 commit comments

Comments
 (0)