Skip to content

Commit 5de4a37

Browse files
mzhang3579sean-jc
authored andcommitted
KVM: selftests: Add a fully functional "struct xstate" for x86
Add a working xstate data structure for the usage of AMX and potential future usage on other xstate components. AMX selftest requires checking both the xstate_bv and xcomp_bv. Existing code relies on pointer arithmetics to fetch xstate_bv and does not support xcomp_bv. So, add a working xstate data structure into processor.h for x86. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Mingwei Zhang <mizhang@google.com> Link: https://lore.kernel.org/r/20230221163655.920289-3-mizhang@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 735b0e0 commit 5de4a37

2 files changed

Lines changed: 23 additions & 25 deletions

File tree

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ extern bool host_cpu_is_amd;
4848
#define X86_CR4_SMAP (1ul << 21)
4949
#define X86_CR4_PKE (1ul << 22)
5050

51+
struct xstate_header {
52+
u64 xstate_bv;
53+
u64 xcomp_bv;
54+
u64 reserved[6];
55+
} __attribute__((packed));
56+
57+
struct xstate {
58+
u8 i387[512];
59+
struct xstate_header header;
60+
u8 extended_state_area[0];
61+
} __attribute__ ((packed, aligned (64)));
62+
5163
/* Note, these are ordered alphabetically to match kvm_cpuid_entry2. Eww. */
5264
enum cpuid_output_regs {
5365
KVM_CPUID_EAX,

tools/testing/selftests/kvm/x86_64/amx_test.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141

4242
#define XSAVE_HDR_OFFSET 512
4343

44-
struct xsave_data {
45-
u8 area[XSAVE_SIZE];
46-
} __aligned(64);
47-
4844
struct tile_config {
4945
u8 palette_id;
5046
u8 start_row;
@@ -103,13 +99,13 @@ static inline void __tilerelease(void)
10399
asm volatile(".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0" ::);
104100
}
105101

106-
static inline void __xsavec(struct xsave_data *data, uint64_t rfbm)
102+
static inline void __xsavec(struct xstate *xstate, uint64_t rfbm)
107103
{
108104
uint32_t rfbm_lo = rfbm;
109105
uint32_t rfbm_hi = rfbm >> 32;
110106

111107
asm volatile("xsavec (%%rdi)"
112-
: : "D" (data), "a" (rfbm_lo), "d" (rfbm_hi)
108+
: : "D" (xstate), "a" (rfbm_lo), "d" (rfbm_hi)
113109
: "memory");
114110
}
115111

@@ -158,16 +154,6 @@ static void set_tilecfg(struct tile_config *cfg)
158154
}
159155
}
160156

161-
static void set_xstatebv(void *data, uint64_t bv)
162-
{
163-
*(uint64_t *)(data + XSAVE_HDR_OFFSET) = bv;
164-
}
165-
166-
static u64 get_xstatebv(void *data)
167-
{
168-
return *(u64 *)(data + XSAVE_HDR_OFFSET);
169-
}
170-
171157
static void init_regs(void)
172158
{
173159
uint64_t cr4, xcr0;
@@ -184,7 +170,7 @@ static void init_regs(void)
184170

185171
static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg,
186172
struct tile_data *tiledata,
187-
struct xsave_data *xsave_data)
173+
struct xstate *xstate)
188174
{
189175
init_regs();
190176
check_cpuid_xsave();
@@ -205,9 +191,9 @@ static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg,
205191
__tilerelease();
206192
GUEST_SYNC(5);
207193
/* bit 18 not in the XCOMP_BV after xsavec() */
208-
set_xstatebv(xsave_data, XFEATURE_MASK_XTILEDATA);
209-
__xsavec(xsave_data, XFEATURE_MASK_XTILEDATA);
210-
GUEST_ASSERT((get_xstatebv(xsave_data) & XFEATURE_MASK_XTILEDATA) == 0);
194+
xstate->header.xstate_bv = XFEATURE_MASK_XTILEDATA;
195+
__xsavec(xstate, XFEATURE_MASK_XTILEDATA);
196+
GUEST_ASSERT(!(xstate->header.xstate_bv & XFEATURE_MASK_XTILEDATA));
211197

212198
/* xfd=0x40000, disable amx tiledata */
213199
wrmsr(MSR_IA32_XFD, XFEATURE_MASK_XTILEDATA);
@@ -243,7 +229,7 @@ int main(int argc, char *argv[])
243229
struct kvm_vm *vm;
244230
struct kvm_x86_state *state;
245231
int xsave_restore_size;
246-
vm_vaddr_t amx_cfg, tiledata, xsavedata;
232+
vm_vaddr_t amx_cfg, tiledata, xstate;
247233
struct ucall uc;
248234
u32 amx_offset;
249235
int stage, ret;
@@ -282,10 +268,10 @@ int main(int argc, char *argv[])
282268
tiledata = vm_vaddr_alloc_pages(vm, 2);
283269
memset(addr_gva2hva(vm, tiledata), rand() | 1, 2 * getpagesize());
284270

285-
/* xsave data for guest_code */
286-
xsavedata = vm_vaddr_alloc_pages(vm, 3);
287-
memset(addr_gva2hva(vm, xsavedata), 0, 3 * getpagesize());
288-
vcpu_args_set(vcpu, 3, amx_cfg, tiledata, xsavedata);
271+
/* XSAVE state for guest_code */
272+
xstate = vm_vaddr_alloc_pages(vm, DIV_ROUND_UP(XSAVE_SIZE, PAGE_SIZE));
273+
memset(addr_gva2hva(vm, xstate), 0, PAGE_SIZE * DIV_ROUND_UP(XSAVE_SIZE, PAGE_SIZE));
274+
vcpu_args_set(vcpu, 3, amx_cfg, tiledata, xstate);
289275

290276
for (stage = 1; ; stage++) {
291277
vcpu_run(vcpu);

0 commit comments

Comments
 (0)