Skip to content

Commit 32e69f2

Browse files
Maxim Levitskysean-jc
authored andcommitted
KVM: x86: Use emulator callbacks instead of duplicating "host flags"
Instead of re-defining the "host flags" bits, just expose dedicated helpers for each of the two remaining flags that are consumed by the emulator. The emulator never consumes both "is guest" and "is SMM" in close proximity, so there is no motivation to avoid additional indirect branches. Also while at it, garbage collect the recently removed host flags. No functional change is intended. Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Tested-by: Santosh Shukla <Santosh.Shukla@amd.com> Link: https://lore.kernel.org/r/20221129193717.513824-6-mlevitsk@redhat.com [sean: fix CONFIG_KVM_SMM=n builds, tweak names of wrappers] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 916b54a commit 32e69f2

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,11 +2074,11 @@ enum {
20742074
TASK_SWITCH_GATE = 3,
20752075
};
20762076

2077-
#define HF_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
2077+
#define HF_GUEST_MASK (1 << 0) /* VCPU is in guest-mode */
20782078

20792079
#ifdef CONFIG_KVM_SMM
2080-
#define HF_SMM_MASK (1 << 6)
2081-
#define HF_SMM_INSIDE_NMI_MASK (1 << 7)
2080+
#define HF_SMM_MASK (1 << 1)
2081+
#define HF_SMM_INSIDE_NMI_MASK (1 << 2)
20822082

20832083
# define __KVM_VCPU_MULTIPLE_ADDRESS_SPACE
20842084
# define KVM_ADDRESS_SPACE_NUM 2

arch/x86/kvm/emulate.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,7 @@ static int em_lseg(struct x86_emulate_ctxt *ctxt)
23102310

23112311
static int em_rsm(struct x86_emulate_ctxt *ctxt)
23122312
{
2313-
if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
2313+
if (!ctxt->ops->is_smm(ctxt))
23142314
return emulate_ud(ctxt);
23152315

23162316
if (ctxt->ops->leave_smm(ctxt))
@@ -5133,7 +5133,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
51335133
const struct x86_emulate_ops *ops = ctxt->ops;
51345134
int rc = X86EMUL_CONTINUE;
51355135
int saved_dst_type = ctxt->dst.type;
5136-
unsigned emul_flags;
5136+
bool is_guest_mode = ctxt->ops->is_guest_mode(ctxt);
51375137

51385138
ctxt->mem_read.pos = 0;
51395139

@@ -5148,7 +5148,6 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
51485148
goto done;
51495149
}
51505150

5151-
emul_flags = ctxt->ops->get_hflags(ctxt);
51525151
if (unlikely(ctxt->d &
51535152
(No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
51545153
if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
@@ -5182,7 +5181,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
51825181
fetch_possible_mmx_operand(&ctxt->dst);
51835182
}
51845183

5185-
if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
5184+
if (unlikely(is_guest_mode) && ctxt->intercept) {
51865185
rc = emulator_check_intercept(ctxt, ctxt->intercept,
51875186
X86_ICPT_PRE_EXCEPT);
51885187
if (rc != X86EMUL_CONTINUE)
@@ -5211,7 +5210,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
52115210
goto done;
52125211
}
52135212

5214-
if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
5213+
if (unlikely(is_guest_mode) && (ctxt->d & Intercept)) {
52155214
rc = emulator_check_intercept(ctxt, ctxt->intercept,
52165215
X86_ICPT_POST_EXCEPT);
52175216
if (rc != X86EMUL_CONTINUE)
@@ -5265,7 +5264,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
52655264

52665265
special_insn:
52675266

5268-
if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
5267+
if (unlikely(is_guest_mode) && (ctxt->d & Intercept)) {
52695268
rc = emulator_check_intercept(ctxt, ctxt->intercept,
52705269
X86_ICPT_POST_MEMACCESS);
52715270
if (rc != X86EMUL_CONTINUE)

arch/x86/kvm/kvm_emulate.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ struct x86_emulate_ops {
220220

221221
void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked);
222222

223-
unsigned (*get_hflags)(struct x86_emulate_ctxt *ctxt);
223+
bool (*is_smm)(struct x86_emulate_ctxt *ctxt);
224+
bool (*is_guest_mode)(struct x86_emulate_ctxt *ctxt);
224225
int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
225226
void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
226227
int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
@@ -275,10 +276,6 @@ enum x86emul_mode {
275276
X86EMUL_MODE_PROT64, /* 64-bit (long) mode. */
276277
};
277278

278-
/* These match some of the HF_* flags defined in kvm_host.h */
279-
#define X86EMUL_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
280-
#define X86EMUL_SMM_MASK (1 << 6)
281-
282279
/*
283280
* fastop functions are declared as taking a never-defined fastop parameter,
284281
* so they can't be called from C directly.

arch/x86/kvm/smm.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ static void check_smram_offsets(void)
111111

112112
void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm)
113113
{
114-
BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
115-
116114
trace_kvm_smm_transition(vcpu->vcpu_id, vcpu->arch.smbase, entering_smm);
117115

118116
if (entering_smm) {

arch/x86/kvm/x86.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8150,9 +8150,14 @@ static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
81508150
static_call(kvm_x86_set_nmi_mask)(emul_to_vcpu(ctxt), masked);
81518151
}
81528152

8153-
static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
8153+
static bool emulator_is_smm(struct x86_emulate_ctxt *ctxt)
81548154
{
8155-
return emul_to_vcpu(ctxt)->arch.hflags;
8155+
return is_smm(emul_to_vcpu(ctxt));
8156+
}
8157+
8158+
static bool emulator_is_guest_mode(struct x86_emulate_ctxt *ctxt)
8159+
{
8160+
return is_guest_mode(emul_to_vcpu(ctxt));
81568161
}
81578162

81588163
#ifndef CONFIG_KVM_SMM
@@ -8221,7 +8226,8 @@ static const struct x86_emulate_ops emulate_ops = {
82218226
.guest_has_fxsr = emulator_guest_has_fxsr,
82228227
.guest_has_rdpid = emulator_guest_has_rdpid,
82238228
.set_nmi_mask = emulator_set_nmi_mask,
8224-
.get_hflags = emulator_get_hflags,
8229+
.is_smm = emulator_is_smm,
8230+
.is_guest_mode = emulator_is_guest_mode,
82258231
.leave_smm = emulator_leave_smm,
82268232
.triple_fault = emulator_triple_fault,
82278233
.set_xcr = emulator_set_xcr,
@@ -8293,8 +8299,6 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
82938299
(cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
82948300
cs_db ? X86EMUL_MODE_PROT32 :
82958301
X86EMUL_MODE_PROT16;
8296-
BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
8297-
82988302
ctxt->interruptibility = 0;
82998303
ctxt->have_exception = false;
83008304
ctxt->exception.vector = -1;

0 commit comments

Comments
 (0)