x86emul: support LKGS
PATCH 1/1 x86emul: support LKGS
Provide support for this insn, which is a prereq to FRED. CPUID-wise introduce both its and FRED's bit at this occasion, thus allowing to also express the dependency right away.
While adding a testcase, also add a SWAPGS one. In order to not affect the behavior of pre-existing tests, install write_{segment,msr} hooks only transiently.
---
Since I was asked to post this work, I've rebased it ahead of most other emulator work I have pending, _except_ "x86emul: a few small steps towards disintegration".
Instead of ->read_segment() we could of course also use ->read_msr() to fetch the original GS base. I don't think I can see a clear advantage of either approach; the way it's done it matches how we handle SWAPGS.
For PV save_segments() would need adjustment, but the insn being restricted to ring 0 means PV guests can't use it anyway (unless we wanted to emulate it as another privileged insn).
tools/libs/light/libxl_cpuid.c +2 -0
@@ -235,6 +235,8 @@ int libxl_cpuid_parse_config(libxl_cpuid {"fzrm", 0x00000007, 1, CPUID_REG_EAX, 10, 1}, {"fsrs", 0x00000007, 1, CPUID_REG_EAX, 11, 1}, {"fsrcs", 0x00000007, 1, CPUID_REG_EAX, 12, 1}, + {"fred", 0x00000007, 1, CPUID_REG_EAX, 17, 1}, + {"lkgs", 0x00000007, 1, CPUID_REG_EAX, 18, 1}, {"wrmsrns", 0x00000007, 1, CPUID_REG_EAX, 19, 1}, {"cet-sss", 0x00000007, 1, CPUID_REG_EDX, 18, 1},
tools/misc/xen-cpuid.c +2 -1
@@ -190,7 +190,8 @@ static const char *const str_7a1[32] = [10] = "fzrm", [11] = "fsrs", [12 ] ="fsrcs" , - /* 18 */ [19] = "wrmsrns", + /* 16 */ [17] = "fred", + [18] = "lkgs", [19] = "wrmsrns", }; static const char *const str_e21a[32] =
tools/tests/x86_emulator/predicates.c +1 -0
@@ -326,6 +326,7 @@ static const struct { { { 0x00, 0x18 }, { 2, 2 }, T, R }, /* ltr */ { { 0x00, 0x20 }, { 2, 2 }, T, R }, /* verr */ { { 0x00, 0x28 }, { 2, 2 }, T, R }, /* verw */ + { { 0x00, 0x30 }, { 0, 2 }, T, R, pfx_f2 }, /* lkgs */ { { 0x01, 0x00 }, { 2, 2 }, F, W }, /* sgdt */ { { 0x01, 0x08 }, { 2, 2 }, F, W }, /* sidt */ { { 0x01, 0x10 }, { 2, 2 }, F, R }, /* lgdt */
tools/tests/x86_emulator/test_x86_emulator.c +100 -0
132 lines not on this page · raw message
tools/tests/x86_emulator/x86-emulate.c +1 -0
@@ -86,6 +86,7 @@ bool emul_test_init(void) cp.feat.adx = true; cp.feat.avx512pf = cp.feat.avx512f; cp.feat.rdpid = true; + cp.feat.lkgs = true; cp.extd.clzero = true;if ( cpu_has_xsave )
xen/arch/x86/x86_emulate/decode.c +5 -1
@@ -744,8 +744,12 @@ decode_twobyte(struct x86_emulate_statecase 0 : s->desc |= DstMem | SrcImplicit | Mov;break ; +case 6 : +if ( !(s->modrm_reg &1 ) &&mode_64bit () ) + {case 2 :case 4 : - s->desc |= SrcMem16; + s->desc |= SrcMem16; + }break ; }break ;
xen/arch/x86/x86_emulate/private.h +1 -0
@@ -593,6 +593,7 @@ amd_like(const struct x86_emulate_ctxt *#define vcpu_has_tsxldtrk () (ctxt->cpuid->feat.tsxldtrk)#define vcpu_has_avx_vnni () (ctxt->cpuid->feat.avx_vnni)#define vcpu_has_avx512_bf16 () (ctxt->cpuid->feat.avx512_bf16) +#define vcpu_has_lkgs () (ctxt->cpuid->feat.lkgs)#define vcpu_must_have (feat) \ generate_exception_if(!vcpu_has_##feat(), X86_EXC_UD)
xen/arch/x86/x86_emulate/x86_emulate.c +25 -2
@@ -2890,8 +2890,31 @@ x86_emulate(break ; }break ; -default : -generate_exception_if (true,EXC_UD ); +case 6 :/* lkgs */ +generate_exception_if ((modrm_reg &1 ) || vex.pfx != vex_f2,EXC_UD ); +generate_exception_if (!mode_64bit () || !mode_ring0 (),EXC_UD ); +vcpu_must_have (lkgs); +fail_if (!ops->read_segment || !ops->read_msr || + !ops->write_segment || !ops->write_msr); +if ( (rc = ops->read_msr (MSR_SHADOW_GS_BASE , &msr_val, + ctxt)) !=X86EMUL_OKAY || + (rc = ops->read_segment (x86_seg_gs, &sreg, + ctxt)) !=X86EMUL_OKAY ) + goto done; + dst.orig_val = sreg.base; +if ( (rc =protmode_load_seg (x86_seg_gs, src.val, false, &sreg, + ctxt, ops)) !=X86EMUL_OKAY || + (rc = ops->write_msr (MSR_SHADOW_GS_BASE , sreg.base, + ctxt)) !=X86EMUL_OKAY ) + goto done; + sreg.base = dst.orig_val; +if ( (rc = ops->write_segment (x86_seg_gs, &sreg, + ctxt)) !=X86EMUL_OKAY ) + { +/* Best effort unwind (i.e. no error checking). */ + ops->write_msr (MSR_SHADOW_GS_BASE , msr_val, ctxt); + goto done; + }break ; }break ;
xen/include/public/arch-x86/cpufeatureset.h +2 -0
@@ -281,6 +281,8 @@ XEN_CPUFEATURE(AVX512_BF16, 10*32+ 5) / XEN_CPUFEATURE(FZRM, 10*32+10) /*A Fast Zero-length REP MOVSB */ XEN_CPUFEATURE(FSRS, 10*32+11) /*A Fast Short REP STOSB */ XEN_CPUFEATURE(FSRCS, 10*32+12) /*A Fast Short REP CMPSB/SCASB */ +XEN_CPUFEATURE(FRED, 10*32+17) /* Flexible Return and Event Delivery */ +XEN_CPUFEATURE(LKGS, 10*32+18) /*S Load Kernel GS Base */XEN_CPUFEATURE (WRMSRNS ,10 *32 +19 )/* WRMSR Non-Serialising */ /* AMD-defined CPU features, CPUID level 0x80000021.eax, word 11 */
xen/tools/gen-cpuid.py +3 -0
@@ -295,6 +295,9 @@ def crunch_numbers(state): # In principle the TSXLDTRK insns could also be considered independent. RTM: [TSXLDTRK], + + # FRED builds on the LKGS instruction. + LKGS: [FRED], } deep_features = tuple(sorted(deps.keys()))