Skip to content

Commit c3392d0

Browse files
MelodyHuibosean-jc
authored andcommitted
KVM: SVM: Provide helpers to set the error code
Provide helpers to set the error code when converting VMGEXIT SW_EXITINFO1 and SW_EXITINFO2 codes from plain numbers to proper defines. Add comments for better code readability. No functionality changed. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Melody Wang <huibo.wang@amd.com> Link: https://lore.kernel.org/r/20250225213937.2471419-3-huibo.wang@amd.com [sean: tweak comments, fix formatting goofs] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent ea4c2f2 commit c3392d0

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,8 +3410,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
34103410
dump_ghcb(svm);
34113411
}
34123412

3413-
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, GHCB_HV_RESP_MALFORMED_INPUT);
3414-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, reason);
3413+
svm_vmgexit_bad_input(svm, reason);
34153414

34163415
/* Resume the guest to "return" the error code. */
34173416
return 1;
@@ -3554,8 +3553,7 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
35543553
return 0;
35553554

35563555
e_scratch:
3557-
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, GHCB_HV_RESP_MALFORMED_INPUT);
3558-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_SCRATCH_AREA);
3556+
svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_SCRATCH_AREA);
35593557

35603558
return 1;
35613559
}
@@ -3655,7 +3653,14 @@ static void snp_complete_psc(struct vcpu_svm *svm, u64 psc_ret)
36553653
svm->sev_es.psc_inflight = 0;
36563654
svm->sev_es.psc_idx = 0;
36573655
svm->sev_es.psc_2m = false;
3658-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, psc_ret);
3656+
3657+
/*
3658+
* PSC requests always get a "no action" response in SW_EXITINFO1, with
3659+
* a PSC-specific return code in SW_EXITINFO2 that provides the "real"
3660+
* return code. E.g. if the PSC request was interrupted, the need to
3661+
* retry is communicated via SW_EXITINFO2, not SW_EXITINFO1.
3662+
*/
3663+
svm_vmgexit_no_action(svm, psc_ret);
36593664
}
36603665

36613666
static void __snp_complete_one_psc(struct vcpu_svm *svm)
@@ -4058,7 +4063,8 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
40584063
goto out_unlock;
40594064
}
40604065

4061-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(0, fw_err));
4066+
/* No action is requested *from KVM* if there was a firmware error. */
4067+
svm_vmgexit_no_action(svm, SNP_GUEST_ERR(0, fw_err));
40624068

40634069
ret = 1; /* resume guest */
40644070

@@ -4114,8 +4120,7 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r
41144120
return snp_handle_guest_req(svm, req_gpa, resp_gpa);
41154121

41164122
request_invalid:
4117-
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, GHCB_HV_RESP_MALFORMED_INPUT);
4118-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT);
4123+
svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
41194124
return 1; /* resume guest */
41204125
}
41214126

@@ -4307,8 +4312,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
43074312
if (ret)
43084313
return ret;
43094314

4310-
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, GHCB_HV_RESP_NO_ACTION);
4311-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 0);
4315+
svm_vmgexit_success(svm, 0);
43124316

43134317
exit_code = kvm_ghcb_get_sw_exit_code(control);
43144318
switch (exit_code) {
@@ -4352,21 +4356,19 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
43524356
break;
43534357
case 1:
43544358
/* Get AP jump table address */
4355-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, sev->ap_jump_table);
4359+
svm_vmgexit_success(svm, sev->ap_jump_table);
43564360
break;
43574361
default:
43584362
pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
43594363
control->exit_info_1);
4360-
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, GHCB_HV_RESP_MALFORMED_INPUT);
4361-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT);
4364+
svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
43624365
}
43634366

43644367
ret = 1;
43654368
break;
43664369
}
43674370
case SVM_VMGEXIT_HV_FEATURES:
4368-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_HV_FT_SUPPORTED);
4369-
4371+
svm_vmgexit_success(svm, GHCB_HV_FT_SUPPORTED);
43704372
ret = 1;
43714373
break;
43724374
case SVM_VMGEXIT_TERM_REQUEST:
@@ -4387,8 +4389,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
43874389
case SVM_VMGEXIT_AP_CREATION:
43884390
ret = sev_snp_ap_creation(svm);
43894391
if (ret) {
4390-
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, GHCB_HV_RESP_MALFORMED_INPUT);
4391-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT);
4392+
svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
43924393
}
43934394

43944395
ret = 1;
@@ -4624,7 +4625,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
46244625
* Return from an AP Reset Hold VMGEXIT, where the guest will
46254626
* set the CS and RIP. Set SW_EXIT_INFO_2 to a non-zero value.
46264627
*/
4627-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1);
4628+
svm_vmgexit_success(svm, 1);
46284629
break;
46294630
case AP_RESET_HOLD_MSR_PROTO:
46304631
/*

arch/x86/kvm/svm/svm.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,11 +2973,7 @@ static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
29732973
if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb))
29742974
return kvm_complete_insn_gp(vcpu, err);
29752975

2976-
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, GHCB_HV_RESP_ISSUE_EXCEPTION);
2977-
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb,
2978-
X86_TRAP_GP |
2979-
SVM_EVTINJ_TYPE_EXEPT |
2980-
SVM_EVTINJ_VALID);
2976+
svm_vmgexit_inject_exception(svm, X86_TRAP_GP);
29812977
return 1;
29822978
}
29832979

arch/x86/kvm/svm/svm.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,35 @@ static inline bool is_vnmi_enabled(struct vcpu_svm *svm)
579579
return false;
580580
}
581581

582+
static inline void svm_vmgexit_set_return_code(struct vcpu_svm *svm,
583+
u64 response, u64 data)
584+
{
585+
ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, response);
586+
ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, data);
587+
}
588+
589+
static inline void svm_vmgexit_inject_exception(struct vcpu_svm *svm, u8 vector)
590+
{
591+
u64 data = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT | vector;
592+
593+
svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_ISSUE_EXCEPTION, data);
594+
}
595+
596+
static inline void svm_vmgexit_bad_input(struct vcpu_svm *svm, u64 suberror)
597+
{
598+
svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_MALFORMED_INPUT, suberror);
599+
}
600+
601+
static inline void svm_vmgexit_success(struct vcpu_svm *svm, u64 data)
602+
{
603+
svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data);
604+
}
605+
606+
static inline void svm_vmgexit_no_action(struct vcpu_svm *svm, u64 data)
607+
{
608+
svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data);
609+
}
610+
582611
/* svm.c */
583612
#define MSR_INVALID 0xffffffffU
584613

0 commit comments

Comments
 (0)