From patchwork Tue Feb 21 16:36:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 60165 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp106576wrd; Tue, 21 Feb 2023 08:38:30 -0800 (PST) X-Google-Smtp-Source: AK7set9BqYdx+aU9YICWoUYFOe7n5IjGRyCrSYQFHhshXsGSORzkkLbokJlpZwMQIEpWgOWgUbAf X-Received: by 2002:a17:906:55d4:b0:8de:e66a:ee68 with SMTP id z20-20020a17090655d400b008dee66aee68mr3052147ejp.35.1676997510830; Tue, 21 Feb 2023 08:38:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676997510; cv=none; d=google.com; s=arc-20160816; b=T8NEmWQzWvoAfI6ec3hDrhCsVTDzGD62QVJp2dXCFYlxvVWxLQurtTcR1XWqbfEb8m 0ng5Wikd508Z107c3B7INGrcs2bC9EOADiddcciHhr97zeTdGxSWi5xjRawk6TaL5QK8 gE5KfGGhxEYP/eM/e/bo/DRkU3yOqKCu5SyENrCYQZlLsb/0VS+CiR9AfauyfkO4IOr8 rtKL3FXJUzGBl7bE2+DG2MgVaifgKWgNtif/2E6hD3/i6JaIgL1HBmJvEamxDmv5BvWv 7+B6dZNTpPrkluuY6H4h3874zjZnFxHhNdwqmZnPpB1iondlh7tvOoNvccNcIKGzfRg3 ydYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=CDOmnOlE8Y16AZivwv4VrFjTYze8Lvbli++k4cFr8iw=; b=o+ooizgcAl470g6YfBAYv52EBuI2x5gZ3E8aqRgX7WBmxvyI72XGq+csjy1mlN8etG Nh41fskw+yYCv2/OP8wUNGt/Guqu9wmL2ed/fS5NHeTKeNBzJ+KPOK79+DlBecFZwFgh Tk5oK2D4KAVNyQC22fkxE9DmXPtzAGtq/H7Pd9xoMAPEQpx3JIuO1653QIM45+YJ2csi k7qURKqb3MTmGQ87OjWe9wUh6hpQ2PFnyO/F62QnAKVwdcQswGyvVgx2t75DyxKRBwW3 AGRHw/PGf4C4HXquXas1KyJLZ3ZAszGRrxdwzdhmAs43K/K1+cPy5r33VMAeAc21v2yG dBMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=PAhlZ3TH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id wj4-20020a170907050400b008c797d4010esi10662222ejb.1005.2023.02.21.08.38.07; Tue, 21 Feb 2023 08:38:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=PAhlZ3TH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234785AbjBUQhb (ORCPT + 99 others); Tue, 21 Feb 2023 11:37:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234508AbjBUQh1 (ORCPT ); Tue, 21 Feb 2023 11:37:27 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 797432CC67 for ; Tue, 21 Feb 2023 08:37:21 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id cx12-20020a17090afd8c00b002366e47e91bso2384017pjb.7 for ; Tue, 21 Feb 2023 08:37:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=CDOmnOlE8Y16AZivwv4VrFjTYze8Lvbli++k4cFr8iw=; b=PAhlZ3TH/qi87Rr35Iq03hgqMaNyTkBefespOAIKOR/aUKjLdJ5kjRUHll7B1SOPRP caqMqqEaoONZstLTVWne2rgdF7JqpXohyWkg8KiM6ga+4pg31UD/3Cy1IV4e153IF8O4 CTbMT7hYH07pc+exh90qXksXCE7KWlUOajHS6W/SoG+BtS0ENgjnEFBKI8GM2EdZ/uai 2nkqhBr70racrzPMN0lpvzrhC6P7WB50lH5b8H2C53mPkX3xwEBdoCMPyHV1JWUM5FSV sHoE4/Zem+o5Bb4PpV672rHwIQ7Ggdn1D+8Qorn7Dfozq/9s3VP+OVk4vc93j+4aUypw kTXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CDOmnOlE8Y16AZivwv4VrFjTYze8Lvbli++k4cFr8iw=; b=6d8KgGPQFqeMDMKIM+1+hWQGyTnJlJ6HjrkHkypEkpmi1kAsiU3glq6PbDRbiysiPR H4le+wJPh9tJYCPfWuwqGuNFcW/8bhfJRULyTsvRX2NlVMjllNc8vRlH1oeXuQ+TgF0C rNanyx8Zhzi58+nCef/6lLiZDzfQK5+lA8cNz1V1YdgcDkz0Pe0+v+52oUfLbPetJblx SeEf3LCUGOEcytzoT3QWyCWUAgYskq5tYDpQKSe8aVa/3gzoxkCPoHL0L86wG0ai//F7 OJaJKwMtHEvZ5qGlC5t5ybEOWQ0sKX3/GOkGeAS7RjqDcc1mdtNCjMy7nR3/mtepyPrt tDDw== X-Gm-Message-State: AO0yUKUiAHz7v1KnpGA7zM0mB4dqj0+KMvyMpGBV5IOVRZPTtdb5ZEfc QHBAoLJ7h7c3DiwmtoSEgq8r5gGBy+C7 X-Received: from mizhang-super.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1071]) (user=mizhang job=sendgmr) by 2002:a17:903:2651:b0:19b:8cbb:30fe with SMTP id je17-20020a170903265100b0019b8cbb30femr708174plb.13.1676997440971; Tue, 21 Feb 2023 08:37:20 -0800 (PST) Reply-To: Mingwei Zhang Date: Tue, 21 Feb 2023 16:36:44 +0000 In-Reply-To: <20230221163655.920289-1-mizhang@google.com> Mime-Version: 1.0 References: <20230221163655.920289-1-mizhang@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230221163655.920289-3-mizhang@google.com> Subject: [PATCH v3 02/13] KVM: selftests: x86: Add a working xstate data structure From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner Cc: "H. Peter Anvin" , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Mingwei Zhang , Jim Mattson , Venkatesh Srinivas , Aaron Lewis , "Chang S. Bae" , Chao Gao X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758459341875642362?= X-GMAIL-MSGID: =?utf-8?q?1758459341875642362?= 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 Signed-off-by: Mingwei Zhang --- .../selftests/kvm/include/x86_64/processor.h | 12 +++++++ tools/testing/selftests/kvm/x86_64/amx_test.c | 36 ++++++------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index b1a31de7108a..5cfd7ef40d78 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -45,6 +45,18 @@ #define X86_CR4_SMAP (1ul << 21) #define X86_CR4_PKE (1ul << 22) +struct xstate_header { + u64 xstate_bv; + u64 xcomp_bv; + u64 reserved[6]; +} __attribute__((packed)); + +struct xstate { + u8 i387[512]; + struct xstate_header header; + u8 extended_state_area[0]; +} __attribute__ ((packed, aligned (64))); + /* Note, these are ordered alphabetically to match kvm_cpuid_entry2. Eww. */ enum cpuid_output_regs { KVM_CPUID_EAX, diff --git a/tools/testing/selftests/kvm/x86_64/amx_test.c b/tools/testing/selftests/kvm/x86_64/amx_test.c index bd72c6eb3b67..bb9dc0008f43 100644 --- a/tools/testing/selftests/kvm/x86_64/amx_test.c +++ b/tools/testing/selftests/kvm/x86_64/amx_test.c @@ -41,10 +41,6 @@ #define XSAVE_HDR_OFFSET 512 -struct xsave_data { - u8 area[XSAVE_SIZE]; -} __aligned(64); - struct tile_config { u8 palette_id; u8 start_row; @@ -103,13 +99,13 @@ static inline void __tilerelease(void) asm volatile(".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0" ::); } -static inline void __xsavec(struct xsave_data *data, uint64_t rfbm) +static inline void __xsavec(struct xstate *xstate, uint64_t rfbm) { uint32_t rfbm_lo = rfbm; uint32_t rfbm_hi = rfbm >> 32; asm volatile("xsavec (%%rdi)" - : : "D" (data), "a" (rfbm_lo), "d" (rfbm_hi) + : : "D" (xstate), "a" (rfbm_lo), "d" (rfbm_hi) : "memory"); } @@ -158,16 +154,6 @@ static void set_tilecfg(struct tile_config *cfg) } } -static void set_xstatebv(void *data, uint64_t bv) -{ - *(uint64_t *)(data + XSAVE_HDR_OFFSET) = bv; -} - -static u64 get_xstatebv(void *data) -{ - return *(u64 *)(data + XSAVE_HDR_OFFSET); -} - static void init_regs(void) { uint64_t cr4, xcr0; @@ -184,7 +170,7 @@ static void init_regs(void) static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, struct tile_data *tiledata, - struct xsave_data *xsave_data) + struct xstate *xstate) { init_regs(); check_cpuid_xsave(); @@ -205,9 +191,9 @@ static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, __tilerelease(); GUEST_SYNC(5); /* bit 18 not in the XCOMP_BV after xsavec() */ - set_xstatebv(xsave_data, XFEATURE_MASK_XTILEDATA); - __xsavec(xsave_data, XFEATURE_MASK_XTILEDATA); - GUEST_ASSERT((get_xstatebv(xsave_data) & XFEATURE_MASK_XTILEDATA) == 0); + xstate->header.xstate_bv = XFEATURE_MASK_XTILEDATA; + __xsavec(xstate, XFEATURE_MASK_XTILEDATA); + GUEST_ASSERT(!(xstate->header.xstate_bv & XFEATURE_MASK_XTILEDATA)); /* xfd=0x40000, disable amx tiledata */ wrmsr(MSR_IA32_XFD, XFEATURE_MASK_XTILEDATA); @@ -244,7 +230,7 @@ int main(int argc, char *argv[]) struct kvm_run *run; struct kvm_x86_state *state; int xsave_restore_size; - vm_vaddr_t amx_cfg, tiledata, xsavedata; + vm_vaddr_t amx_cfg, tiledata, xstate; struct ucall uc; u32 amx_offset; int stage, ret; @@ -284,10 +270,10 @@ int main(int argc, char *argv[]) tiledata = vm_vaddr_alloc_pages(vm, 2); memset(addr_gva2hva(vm, tiledata), rand() | 1, 2 * getpagesize()); - /* xsave data for guest_code */ - xsavedata = vm_vaddr_alloc_pages(vm, 3); - memset(addr_gva2hva(vm, xsavedata), 0, 3 * getpagesize()); - vcpu_args_set(vcpu, 3, amx_cfg, tiledata, xsavedata); + /* XSAVE state for guest_code */ + xstate = vm_vaddr_alloc_pages(vm, DIV_ROUND_UP(XSAVE_SIZE, PAGE_SIZE)); + memset(addr_gva2hva(vm, xstate), 0, PAGE_SIZE * DIV_ROUND_UP(XSAVE_SIZE, PAGE_SIZE)); + vcpu_args_set(vcpu, 3, amx_cfg, tiledata, xstate); for (stage = 1; ; stage++) { vcpu_run(vcpu);