Skip to content

Commit adac42b

Browse files
committed
KVM: SVM: Save/restore args across SEV-ES VMRUN via host save area
Use the host save area to preserve volatile registers that are used in __svm_sev_es_vcpu_run() to access function parameters after #VMEXIT. Like saving/restoring non-volatile registers, there's no reason not to take advantage of hardware restoring registers on #VMEXIT, as doing so shaves a few instructions and the save area is going to be accessed no matter what. Converting all register save/restore code to use the host save area also make it easier to follow the SEV-ES VMRUN flow in its entirety, as opposed to having a mix of stack-based versus host save area save/restore. Add a parameter to RESTORE_HOST_SPEC_CTRL_BODY so that the SEV-ES path doesn't need to write @spec_ctrl_intercepted to memory just to play nice with the common macro. Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Link: https://lore.kernel.org/r/20240223204233.3337324-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent c92be2f commit adac42b

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

arch/x86/kvm/svm/vmenter.S

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"", X86_FEATURE_V_SPEC_CTRL
6868
901:
6969
.endm
70-
.macro RESTORE_HOST_SPEC_CTRL_BODY
70+
.macro RESTORE_HOST_SPEC_CTRL_BODY spec_ctrl_intercepted:req
7171
900:
7272
/* Same for after vmexit. */
7373
mov $MSR_IA32_SPEC_CTRL, %ecx
@@ -76,7 +76,7 @@
7676
* Load the value that the guest had written into MSR_IA32_SPEC_CTRL,
7777
* if it was not intercepted during guest execution.
7878
*/
79-
cmpb $0, (%_ASM_SP)
79+
cmpb $0, \spec_ctrl_intercepted
8080
jnz 998f
8181
rdmsr
8282
movl %eax, SVM_spec_ctrl(%_ASM_DI)
@@ -269,7 +269,7 @@ SYM_FUNC_START(__svm_vcpu_run)
269269
RET
270270

271271
RESTORE_GUEST_SPEC_CTRL_BODY
272-
RESTORE_HOST_SPEC_CTRL_BODY
272+
RESTORE_HOST_SPEC_CTRL_BODY (%_ASM_SP)
273273

274274
10: cmpb $0, _ASM_RIP(kvm_rebooting)
275275
jne 2b
@@ -298,6 +298,8 @@ SYM_FUNC_END(__svm_vcpu_run)
298298
#define SEV_ES_GPRS_BASE 0x300
299299
#define SEV_ES_RBX (SEV_ES_GPRS_BASE + __VCPU_REGS_RBX * WORD_SIZE)
300300
#define SEV_ES_RBP (SEV_ES_GPRS_BASE + __VCPU_REGS_RBP * WORD_SIZE)
301+
#define SEV_ES_RSI (SEV_ES_GPRS_BASE + __VCPU_REGS_RSI * WORD_SIZE)
302+
#define SEV_ES_RDI (SEV_ES_GPRS_BASE + __VCPU_REGS_RDI * WORD_SIZE)
301303
#define SEV_ES_R12 (SEV_ES_GPRS_BASE + __VCPU_REGS_R12 * WORD_SIZE)
302304
#define SEV_ES_R13 (SEV_ES_GPRS_BASE + __VCPU_REGS_R13 * WORD_SIZE)
303305
#define SEV_ES_R14 (SEV_ES_GPRS_BASE + __VCPU_REGS_R14 * WORD_SIZE)
@@ -322,11 +324,12 @@ SYM_FUNC_START(__svm_sev_es_vcpu_run)
322324
mov %r12, SEV_ES_R12 (%rdx)
323325
mov %rbx, SEV_ES_RBX (%rdx)
324326

325-
/* Accessed directly from the stack in RESTORE_HOST_SPEC_CTRL. */
326-
push %rsi
327-
328-
/* Save @svm. */
329-
push %rdi
327+
/*
328+
* Save volatile registers that hold arguments that are needed after
329+
* #VMEXIT (RDI=@svm and RSI=@spec_ctrl_intercepted).
330+
*/
331+
mov %rdi, SEV_ES_RDI (%rdx)
332+
mov %rsi, SEV_ES_RSI (%rdx)
330333

331334
/* Clobbers RAX, RCX, RDX (@hostsa). */
332335
RESTORE_GUEST_SPEC_CTRL
@@ -342,15 +345,12 @@ SYM_FUNC_START(__svm_sev_es_vcpu_run)
342345

343346
2: cli
344347

345-
/* Pop @svm to RDI, guest registers have been saved already. */
346-
pop %rdi
347-
348348
#ifdef CONFIG_MITIGATION_RETPOLINE
349349
/* IMPORTANT: Stuff the RSB immediately after VM-Exit, before RET! */
350350
FILL_RETURN_BUFFER %rax, RSB_CLEAR_LOOPS, X86_FEATURE_RETPOLINE
351351
#endif
352352

353-
/* Clobbers RAX, RCX, RDX, consumes RDI (@svm). */
353+
/* Clobbers RAX, RCX, RDX, consumes RDI (@svm) and RSI (@spec_ctrl_intercepted). */
354354
RESTORE_HOST_SPEC_CTRL
355355

356356
/*
@@ -362,13 +362,10 @@ SYM_FUNC_START(__svm_sev_es_vcpu_run)
362362
*/
363363
UNTRAIN_RET_VM
364364

365-
/* "Pop" and discard @spec_ctrl_intercepted. */
366-
pop %rax
367-
368365
RET
369366

370367
RESTORE_GUEST_SPEC_CTRL_BODY
371-
RESTORE_HOST_SPEC_CTRL_BODY
368+
RESTORE_HOST_SPEC_CTRL_BODY %sil
372369

373370
3: cmpb $0, kvm_rebooting(%rip)
374371
jne 2b

0 commit comments

Comments
 (0)