Skip to content

Commit e8f85d7

Browse files
committed
KVM: x86: Don't treat ENTER and LEAVE as branches, because they aren't
Remove the IsBranch flag from ENTER and LEAVE in KVM's emulator, as ENTER and LEAVE are stack operations, not branches. Add forced emulation of said instructions to the PMU counters test to prove that KVM diverges from hardware, and to guard against regressions. Opportunistically add a missing "1 MOV" to the selftest comment regarding the number of instructions per loop, which commit 7803339 ("KVM: selftests: Use data load to trigger LLC references/misses in Intel PMU") forgot to add. Fixes: 018d70f ("KVM: x86: Update vPMCs when retiring branch instructions") Cc: Jim Mattson <[email protected]> Reviewed-by: Jim Mattson <[email protected]> Reviewed-by: Chao Gao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent c49aa98 commit e8f85d7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

arch/x86/kvm/emulate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4330,8 +4330,8 @@ static const struct opcode opcode_table[256] = {
43304330
I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
43314331
G(ByteOp, group11), G(0, group11),
43324332
/* 0xC8 - 0xCF */
4333-
I(Stack | SrcImmU16 | Src2ImmByte | IsBranch, em_enter),
4334-
I(Stack | IsBranch, em_leave),
4333+
I(Stack | SrcImmU16 | Src2ImmByte, em_enter),
4334+
I(Stack, em_leave),
43354335
I(ImplicitOps | SrcImmU16 | IsBranch, em_ret_far_imm),
43364336
I(ImplicitOps | IsBranch, em_ret_far),
43374337
D(ImplicitOps | IsBranch), DI(SrcImmByte | IsBranch, intn),

tools/testing/selftests/kvm/x86/pmu_counters_test.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#define NUM_BRANCH_INSNS_RETIRED (NUM_LOOPS)
1515

1616
/*
17-
* Number of instructions in each loop. 1 CLFLUSH/CLFLUSHOPT/NOP, 1 MFENCE,
18-
* 1 LOOP.
17+
* Number of instructions in each loop. 1 ENTER, 1 CLFLUSH/CLFLUSHOPT/NOP,
18+
* 1 MFENCE, 1 MOV, 1 LEAVE, 1 LOOP.
1919
*/
20-
#define NUM_INSNS_PER_LOOP 4
20+
#define NUM_INSNS_PER_LOOP 6
2121

2222
/*
2323
* Number of "extra" instructions that will be counted, i.e. the number of
@@ -210,9 +210,11 @@ do { \
210210
__asm__ __volatile__("wrmsr\n\t" \
211211
" mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \
212212
"1:\n\t" \
213+
FEP "enter $0, $0\n\t" \
213214
clflush "\n\t" \
214215
"mfence\n\t" \
215216
"mov %[m], %%eax\n\t" \
217+
FEP "leave\n\t" \
216218
FEP "loop 1b\n\t" \
217219
FEP "mov %%edi, %%ecx\n\t" \
218220
FEP "xor %%eax, %%eax\n\t" \

0 commit comments

Comments
 (0)