From patchwork Wed Mar 1 05:34:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13155606 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C30EEC7EE23 for ; Wed, 1 Mar 2023 05:34:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229756AbjCAFeo (ORCPT ); Wed, 1 Mar 2023 00:34:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbjCAFen (ORCPT ); Wed, 1 Mar 2023 00:34:43 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEF3C1555B for ; Tue, 28 Feb 2023 21:34:41 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id b8-20020aa78708000000b005eaa50faa35so6133768pfo.20 for ; Tue, 28 Feb 2023 21:34:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648881; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bB74MZqXSTBaNxtCuP+Ee510AKnzU0mpDJN+BqnT3DM=; b=QMDyIWUGPsPvo3xdSYngMeWNs6Cg8PNr5kGQWXYHYWBVu0QtJg+ey0G9kCZUJFYhhB AMHNyhGYuG39hrnuDQDp/yNBTZPefaKN0sm591iif1IeWjTNOHBRkJP/B0Mzp7HuulGw juPeEantOSCkO0v6uAz98zELYqKMDmf0GGRSp/ID2DyC7mkN+N2deJlONhpXaqR9om2Q HzT/JDbRAEoxbqOOdFwlf2QS+hV0X2YVFwTmVuYh3hreY5nZDB+Wf4VVzIB76/JWVg54 BKVhbpqcr8vitgyfO0lPnr5RTinONGra2fekqT+tphk76czWCTXAkEh0xVQyYP6l2FAO jcpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648881; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bB74MZqXSTBaNxtCuP+Ee510AKnzU0mpDJN+BqnT3DM=; b=ObM2FHNpyKlGEdIGymYp+HHCFW31eXXtGay/fkkIQWRCP9GVovapwCgZruWkIlpw9i Ecy90eXHHCv7nNW98RDi1fsW9v94vRLaIZN7JGQKdeIkVgBai88VnzDt18vnmKMVnQcg WcRH6CEvrfJxRpWekrzE+5Jo7aRLD5bhQOCFv1YzXzNYkKxFCmjQOPOPQyi9jWuBPe7k 70sFHt5qt+u+jgCQnJzCzT2+GX+ptQx5JsiCoJztNbWNbp39HaU+zR1qnPNorGBMRXWM tVhAmeZt6ViImvSw0sscUvKVAs8aNfsM6ukPOK3vjEA7ixw7ZFGccabVUYeciSl8La5y WGlg== X-Gm-Message-State: AO0yUKVTlg2RbVz863oRLNPT7+KpilooUo78kWXqrL6MeEf0Vg4H+O1a SgN0vP+M/89Sw5wnp1WeE9pgMvjKAZHBw54Uh2GLVdudZeECArIRi7lvhZvNViheSZJRGikg5ur CXJXgM69OpJdHVUGadYZwcNXGgdOEo+6E1WuOg6OQ5Y2Rk1c0jT6xVl1jZ41GEwdPLvxH X-Google-Smtp-Source: AK7set8I+7ELSK+q2cxylbja8RJZjGuyJnJzoYABWDX3NaaL1pTaQvJ8g6qYbjcHljZzk8vsoWKQfeaA1iOqgRj8 X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a05:6a02:288:b0:501:f894:ae62 with SMTP id bk8-20020a056a02028800b00501f894ae62mr5157268pgb.4.1677648881093; Tue, 28 Feb 2023 21:34:41 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:18 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-2-aaronlewis@google.com> Subject: [PATCH 1/8] KVM: selftests: Hoist XGETBV and XSETBV to make them more accessible From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The instructions XGETBV and XSETBV are useful to other tests. Move them to processor.h to make them available to be used more broadly. No functional change intended. Reviewed-by: Jim Mattson Signed-off-by: Aaron Lewis --- .../selftests/kvm/include/x86_64/processor.h | 19 +++++++++++++++ tools/testing/selftests/kvm/x86_64/amx_test.c | 24 +++---------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 53ffa43c90db..62a5c3953deb 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -496,6 +496,25 @@ static inline void set_cr4(uint64_t val) __asm__ __volatile__("mov %0, %%cr4" : : "r" (val) : "memory"); } +static inline u64 xgetbv(u32 index) +{ + u32 eax, edx; + + __asm__ __volatile__("xgetbv;" + : "=a" (eax), "=d" (edx) + : "c" (index)); + return eax | ((u64)edx << 32); +} + +static inline void xsetbv(u32 index, u64 value) +{ + u32 eax = value; + u32 edx = value >> 32; + + __asm__ __volatile__("xsetbv" :: "a" (eax), "d" (edx), "c" (index)); +} + + static inline struct desc_ptr get_gdt(void) { struct desc_ptr gdt; diff --git a/tools/testing/selftests/kvm/x86_64/amx_test.c b/tools/testing/selftests/kvm/x86_64/amx_test.c index bd72c6eb3b67..4b733ad21831 100644 --- a/tools/testing/selftests/kvm/x86_64/amx_test.c +++ b/tools/testing/selftests/kvm/x86_64/amx_test.c @@ -68,24 +68,6 @@ struct xtile_info { static struct xtile_info xtile; -static inline u64 __xgetbv(u32 index) -{ - u32 eax, edx; - - asm volatile("xgetbv;" - : "=a" (eax), "=d" (edx) - : "c" (index)); - return eax + ((u64)edx << 32); -} - -static inline void __xsetbv(u32 index, u64 value) -{ - u32 eax = value; - u32 edx = value >> 32; - - asm volatile("xsetbv" :: "a" (eax), "d" (edx), "c" (index)); -} - static inline void __ldtilecfg(void *cfg) { asm volatile(".byte 0xc4,0xe2,0x78,0x49,0x00" @@ -121,7 +103,7 @@ static inline void check_cpuid_xsave(void) static bool check_xsave_supports_xtile(void) { - return __xgetbv(0) & XFEATURE_MASK_XTILE; + return xgetbv(0) & XFEATURE_MASK_XTILE; } static void check_xtile_info(void) @@ -177,9 +159,9 @@ static void init_regs(void) cr4 |= X86_CR4_OSXSAVE; set_cr4(cr4); - xcr0 = __xgetbv(0); + xcr0 = xgetbv(0); xcr0 |= XFEATURE_MASK_XTILE; - __xsetbv(0x0, xcr0); + xsetbv(0x0, xcr0); } static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, From patchwork Wed Mar 1 05:34:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13155607 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFE22C64ED6 for ; Wed, 1 Mar 2023 05:34:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229762AbjCAFeq (ORCPT ); Wed, 1 Mar 2023 00:34:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229753AbjCAFeo (ORCPT ); Wed, 1 Mar 2023 00:34:44 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0CE418169 for ; Tue, 28 Feb 2023 21:34:43 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-536e8d6d9ceso260781647b3.12 for ; Tue, 28 Feb 2023 21:34:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648883; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cm42io0xC+Ut0GU3E5WLWJMzeEm/fUz5RzaC8FAGr+s=; b=BDIcDGL0w/tILTFhzbpRcsq+EcguU0SculKGfTPdUSUMOJ9fwhLerg9GazlWH35VYH k18amV/15o5QJuWDI9rcmRIKxiJ0/gNl9Q/+EtgQmtp+YzA4L2ETHcz2NQbvMqcSsVx9 VFGtvWxyb8w2GHnFk06lZjwUxsn0MOzTFuRk34+ivJeWfTeRcUzwhdbCsDACI9cqznOJ HaYXDkCTJQW0qurVP7JgxugLe/eJ20dqB1VSluVIWDVrLUfLsGJNI5vDcxiz1mW+3gTs VYXZPDw+FsX/T/GJMybyONfSCozVSF+Sa6wkIV6OKN2Z/5ktJzvvwtWaaU7xONYOEF0W xWjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648883; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cm42io0xC+Ut0GU3E5WLWJMzeEm/fUz5RzaC8FAGr+s=; b=XBTOkG1lSpqqciBvbIaY0tAUuZNaSsrnSoarvCmQHRSuw+u0QL4UK8cHZ3hHnxxAdP PRFw6L810mhr11I/XYtHbtm4xuqHVqWV7wGjXby6mSlMZDR2kvus7NSQjPAHNvnENK2n 8I/5xIjr3VSHD19NQyHIxYdnRN+udLC1x42FbvKBX329rVlVbzuqCUDucIJa5x9a2Zlz 9QiTozQY34XBwZFDYCwkp4jwTHicWxYZL6ZGq+ShbvpqqYj6D7yhcnEBbJhS3vX6u8jh SQKxGSN2/kmO2MobhfuixHE2Z3wQXsLPfiU5vKJAHJmHbKi5EjaCdEbcHhFNcSMJrsPw Gbgg== X-Gm-Message-State: AO0yUKWR0DRtVI8ABEo1Z2t0JTs5VHVNxUCSiAV8fpf1/vAWKoSS6SoS Wl8K+/jqOEUsvPmaLs4LcEMzh65R2mEZK6mAXUgZ/ZsANsTZ5wHj+kxpJShvtumD9B8h4Vxeyh3 bcp6E+X94avnT2NdV5P5EnDX0dVgRbCGjj/8mjiUrwp3TCDihEMFl4WoNfr1LPvlA064O X-Google-Smtp-Source: AK7set9tsImpPjvy05AEY+GiJz7S120Mj8lWkyCUl+BsGHzMK3snRDEeUblYmA6nhroXrPFrGeB9DrGL5Dr8R03q X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a81:af08:0:b0:536:5557:33a8 with SMTP id n8-20020a81af08000000b00536555733a8mr3220700ywh.9.1677648882819; Tue, 28 Feb 2023 21:34:42 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:19 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-3-aaronlewis@google.com> Subject: [PATCH 2/8] KVM: selftests: Add XFEATURE masks to common code From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add XFEATURE masks to processor.h to make them more broadly available in KVM selftests. They were taken from fpu/types.h, which included a difference in spacing between the ones in amx_test from XTILECFG and XTILEDATA, to XTILE_CFG and XTILE_DATA. This has been reflected in amx_test. Signed-off-by: Aaron Lewis --- .../selftests/kvm/include/x86_64/processor.h | 17 ++++++++++++++ tools/testing/selftests/kvm/x86_64/amx_test.c | 22 +++++++------------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 62a5c3953deb..8e201575ef73 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -48,6 +48,23 @@ extern bool host_cpu_is_amd; #define X86_CR4_SMAP (1ul << 21) #define X86_CR4_PKE (1ul << 22) +#define XFEATURE_MASK_FP BIT_ULL(0) +#define XFEATURE_MASK_SSE BIT_ULL(1) +#define XFEATURE_MASK_YMM BIT_ULL(2) +#define XFEATURE_MASK_BNDREGS BIT_ULL(3) +#define XFEATURE_MASK_BNDCSR BIT_ULL(4) +#define XFEATURE_MASK_OPMASK BIT_ULL(5) +#define XFEATURE_MASK_ZMM_Hi256 BIT_ULL(6) +#define XFEATURE_MASK_Hi16_ZMM BIT_ULL(7) +#define XFEATURE_MASK_XTILE_CFG BIT_ULL(17) +#define XFEATURE_MASK_XTILE_DATA BIT_ULL(18) + +#define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK \ + | XFEATURE_MASK_ZMM_Hi256 \ + | XFEATURE_MASK_Hi16_ZMM) +#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILE_DATA \ + | XFEATURE_MASK_XTILE_CFG) + /* 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 4b733ad21831..14a7656620d5 100644 --- a/tools/testing/selftests/kvm/x86_64/amx_test.c +++ b/tools/testing/selftests/kvm/x86_64/amx_test.c @@ -33,12 +33,6 @@ #define MAX_TILES 16 #define RESERVED_BYTES 14 -#define XFEATURE_XTILECFG 17 -#define XFEATURE_XTILEDATA 18 -#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG) -#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA) -#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) - #define XSAVE_HDR_OFFSET 512 struct xsave_data { @@ -187,14 +181,14 @@ 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); + set_xstatebv(xsave_data, XFEATURE_MASK_XTILE_DATA); + __xsavec(xsave_data, XFEATURE_MASK_XTILE_DATA); + GUEST_ASSERT((get_xstatebv(xsave_data) & XFEATURE_MASK_XTILE_DATA) == 0); /* xfd=0x40000, disable amx tiledata */ - wrmsr(MSR_IA32_XFD, XFEATURE_MASK_XTILEDATA); + wrmsr(MSR_IA32_XFD, XFEATURE_MASK_XTILE_DATA); GUEST_SYNC(6); - GUEST_ASSERT(rdmsr(MSR_IA32_XFD) == XFEATURE_MASK_XTILEDATA); + GUEST_ASSERT(rdmsr(MSR_IA32_XFD) == XFEATURE_MASK_XTILE_DATA); set_tilecfg(amx_cfg); __ldtilecfg(amx_cfg); /* Trigger #NM exception */ @@ -206,11 +200,11 @@ static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, void guest_nm_handler(struct ex_regs *regs) { - /* Check if #NM is triggered by XFEATURE_MASK_XTILEDATA */ + /* Check if #NM is triggered by XFEATURE_MASK_XTILE_DATA */ GUEST_SYNC(7); - GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILEDATA); + GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILE_DATA); GUEST_SYNC(8); - GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILEDATA); + GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILE_DATA); /* Clear xfd_err */ wrmsr(MSR_IA32_XFD_ERR, 0); /* xfd=0, enable amx */ From patchwork Wed Mar 1 05:34:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13155608 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BA3FC64ED6 for ; Wed, 1 Mar 2023 05:34:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbjCAFet (ORCPT ); Wed, 1 Mar 2023 00:34:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229759AbjCAFeq (ORCPT ); Wed, 1 Mar 2023 00:34:46 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2298618169 for ; Tue, 28 Feb 2023 21:34:45 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id t11-20020a170902e84b00b0019e399b2efaso1708828plg.11 for ; Tue, 28 Feb 2023 21:34:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648884; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=nV6aVRBny4wt8k71zP/EmiTrdxTWxBk1zUWSbiL8uGU=; b=S/7svbGRA1qwhgbCnVr9N63YJy3/PMXRGq60L3N0+atayGoX8bw2dxurKMc1zKoCe+ oOyMAz8shNr01LgUvNL9tst7cC/n8Z1E9gMc3lhz7xjmU1prpi0i+i2W8Y66BDPbAE7d C+VVUaQnZ32QzNz7bog3KeZxB/33QfVyxj4Nj1P5vgaDBc+fqtOLNDcfzML9FEy3AYS8 uUl+q5trkJVJ+87e1Cy6O9x0xAVf1qZNvZETWXDiqA8IPYxy/mvausQ3qrKLG31Z+VxU GZz1IZIZMb4K2bAd8IZNmvgTfSCgFRgwhXX5vjfEEbEbnJ05BSi8uOta380VUogD3wpT X12A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648884; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nV6aVRBny4wt8k71zP/EmiTrdxTWxBk1zUWSbiL8uGU=; b=634NZv/qAiK4faor/obFKqePYmk0bjqhFGeTsjHZ5qZYhE6Ng95xDAapSJ9bxDC45G fOtSyrT1BhRK1vPsOe4k702+PH38aWMwGXN7KeYpWcbeNQHWuuBMn5vfSayJIEXf0sLB UURcilocSXilUJ4HpLKqxflap/xBiQ6I7nDKIk8JMjJbrofl62+YSyFFtuIHmnD4hwlh RjWKx4zx4ZsZXxREu0IT2ZNVPDjbOGX3wEeb8S9xVWkeQoWhb+NB/5xZ/BmQkphvmFi8 7lsJfaN7J6lDueP+l3kohmc9JVdMDxOPZ1udO29HI1e5nqLV2rBSwAN5W3aAwBBTuUQb 37RQ== X-Gm-Message-State: AO0yUKU2ILSF8oCbYu2I/LV7L17lGpYT6pOCtOPOqyVg8IHql5+JjUwD ODEWUt8YykHJjDQgoR+1qU2RuTgA2Ik8a8/VEp8ERjCsxop3rCseM3KY0e98MZI2uOfH05gpFH2 gg1/3M0vfHi5CcW4qwWVlhG7FG+B/s9MWBkmmKyFElfGr472GDCvcF0C2t1LlspGe98W5 X-Google-Smtp-Source: AK7set8gyYkISxg+mXiqmbBtNoRYr4O/nynetQbynRrHquVtIrUMnIOLwbmXWU47BOCx6wnAGpTKBW+yYoP1zCA4 X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:902:ef8a:b0:19b:456:6400 with SMTP id iz10-20020a170902ef8a00b0019b04566400mr1867315plb.7.1677648884455; Tue, 28 Feb 2023 21:34:44 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:20 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-4-aaronlewis@google.com> Subject: [PATCH 3/8] KVM: selftests: Add strnlen() to the string overrides From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add strnlen() to the string overrides to allow it to be called in the guest. The implementation for strnlen() was taken from the kernel's generic version, lib/string.c. This will be needed when printf is introduced. Signed-off-by: Aaron Lewis --- tools/testing/selftests/kvm/Makefile | 1 + tools/testing/selftests/kvm/lib/string_override.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 84a627c43795..cda631f10526 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -197,6 +197,7 @@ endif CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ -Wno-gnu-variable-sized-type-not-at-end \ -fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \ + -fno-builtin-strnlen \ -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \ -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \ -I$( X-Patchwork-Id: 13155609 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5B65C64ED6 for ; Wed, 1 Mar 2023 05:34:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229775AbjCAFew (ORCPT ); Wed, 1 Mar 2023 00:34:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229763AbjCAFer (ORCPT ); Wed, 1 Mar 2023 00:34:47 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B02BA28865 for ; Tue, 28 Feb 2023 21:34:46 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id k3-20020a170902ce0300b0019ca6e66303so6400294plg.18 for ; Tue, 28 Feb 2023 21:34:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648886; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=doJHREwFqM2JMRmYYm/Qs7NX61miXXm1Yp1snN9XpN4=; b=Lq/2e/jazDMl9/fQ68ofxu+DdZlhRYZaTqKaqr3FjnwgolpOt6EO0XfCvTYIsu8UZK d1LHDkqJqKoyt6tNK2tH2e7boPdvkX4GBaRGgR9+zsrr4WeI0iNEf0dyBOqwOLkLe5+O vgkNNRzV3GtXyJGQSO40M18SLJodHpgp9fn2uomyFCBzZJ2cyXUGfXqvDZkZqbCxyjds SOD1d90VrW9j2w0/8jOvawb3lrfZyymLriFo+kfQglohrk7G+nx7ennN1diapOwef5WG UCyEH4Aaft0M77jlYknFLLHcKj2TfiYAkBIL77XxqZj9Iqqr5AvLbZFnWtazyMLeynQI jkMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648886; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=doJHREwFqM2JMRmYYm/Qs7NX61miXXm1Yp1snN9XpN4=; b=irmp/Waprp0MPqj/vQCq0p9S973GcDPf78fThBioeb99gJ72MoHNJ569wzbVqzmZV2 siuIMqkN+GRCeugXVEagB04S7xvBH6Ch91/NyPt8x6d0V6V63DR0X4RQ88bmBZOjd6hS 5WHQrzrEl6+8rnOYguLhxsgOHl9ZzOex/9olr5kcNaloInJsEWWFhP3rkQXpegMdbves kOjKBtSml/HyBNRE2oSXycYYmZFdLaVH/gdq5QOd89qJpSSizEJRuiENPhpJQXQZJ3by llRGHLO55FcBz0hx93K9TDFTpSib77/FvBOyUYbfRdfLHYjB39hjS7zquai4mTPDFKBi pLlA== X-Gm-Message-State: AO0yUKWJBYaAhmTRTAgZr8/n4fARDGoSbbDjzqf+Q9Ezr/ItoXbN4DmN R6rt2XduF0TZfXLl6veSWQ0at5Z2axWC5U+6tQLmIh00BSh60/DvroVPJhDJZnm7ctHW3tNiHVq ozdEJixqGUsAw+D6xusbNiVG81Jd+JFH+pzZ06RJrLZWdY/aHbhgvr5AwEsyXin5KDdG3 X-Google-Smtp-Source: AK7set/MygwT3iCb4gKlnzK42RMvwufLoNGgQy/h/Fw27wHDfGzghfamjlEsiKLtjw+pO1PD8O2NWVvfT9nlBbpe X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:90a:fae:b0:237:64dc:5acd with SMTP id 43-20020a17090a0fae00b0023764dc5acdmr2164900pjz.7.1677648886099; Tue, 28 Feb 2023 21:34:46 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:21 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-5-aaronlewis@google.com> Subject: [PATCH 4/8] KVM: selftests: Copy printf.c to KVM selftests From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a local version of vsprintf() for the guest to use. The file printf.c was lifted from arch/x86/boot/printf.c. Signed-off-by: Aaron Lewis --- tools/testing/selftests/kvm/lib/printf.c | 307 +++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 tools/testing/selftests/kvm/lib/printf.c diff --git a/tools/testing/selftests/kvm/lib/printf.c b/tools/testing/selftests/kvm/lib/printf.c new file mode 100644 index 000000000000..1237beeb9540 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/printf.c @@ -0,0 +1,307 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* -*- linux-c -*- ------------------------------------------------------- * + * + * Copyright (C) 1991, 1992 Linus Torvalds + * Copyright 2007 rPath, Inc. - All Rights Reserved + * + * ----------------------------------------------------------------------- */ + +/* + * Oh, it's a waste of space, but oh-so-yummy for debugging. This + * version of printf() does not include 64-bit support. "Live with + * it." + * + */ + +#include "boot.h" + +static int skip_atoi(const char **s) +{ + int i = 0; + + while (isdigit(**s)) + i = i * 10 + *((*s)++) - '0'; + return i; +} + +#define ZEROPAD 1 /* pad with zero */ +#define SIGN 2 /* unsigned/signed long */ +#define PLUS 4 /* show plus */ +#define SPACE 8 /* space if plus */ +#define LEFT 16 /* left justified */ +#define SMALL 32 /* Must be 32 == 0x20 */ +#define SPECIAL 64 /* 0x */ + +#define __do_div(n, base) ({ \ +int __res; \ +__res = ((unsigned long) n) % (unsigned) base; \ +n = ((unsigned long) n) / (unsigned) base; \ +__res; }) + +static char *number(char *str, long num, int base, int size, int precision, + int type) +{ + /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ + static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ + + char tmp[66]; + char c, sign, locase; + int i; + + /* locase = 0 or 0x20. ORing digits or letters with 'locase' + * produces same digits or (maybe lowercased) letters */ + locase = (type & SMALL); + if (type & LEFT) + type &= ~ZEROPAD; + if (base < 2 || base > 16) + return NULL; + c = (type & ZEROPAD) ? '0' : ' '; + sign = 0; + if (type & SIGN) { + if (num < 0) { + sign = '-'; + num = -num; + size--; + } else if (type & PLUS) { + sign = '+'; + size--; + } else if (type & SPACE) { + sign = ' '; + size--; + } + } + if (type & SPECIAL) { + if (base == 16) + size -= 2; + else if (base == 8) + size--; + } + i = 0; + if (num == 0) + tmp[i++] = '0'; + else + while (num != 0) + tmp[i++] = (digits[__do_div(num, base)] | locase); + if (i > precision) + precision = i; + size -= precision; + if (!(type & (ZEROPAD + LEFT))) + while (size-- > 0) + *str++ = ' '; + if (sign) + *str++ = sign; + if (type & SPECIAL) { + if (base == 8) + *str++ = '0'; + else if (base == 16) { + *str++ = '0'; + *str++ = ('X' | locase); + } + } + if (!(type & LEFT)) + while (size-- > 0) + *str++ = c; + while (i < precision--) + *str++ = '0'; + while (i-- > 0) + *str++ = tmp[i]; + while (size-- > 0) + *str++ = ' '; + return str; +} + +int vsprintf(char *buf, const char *fmt, va_list args) +{ + int len; + unsigned long num; + int i, base; + char *str; + const char *s; + + int flags; /* flags to number() */ + + int field_width; /* width of output field */ + int precision; /* min. # of digits for integers; max + number of chars for from string */ + int qualifier; /* 'h', 'l', or 'L' for integer fields */ + + for (str = buf; *fmt; ++fmt) { + if (*fmt != '%') { + *str++ = *fmt; + continue; + } + + /* process flags */ + flags = 0; + repeat: + ++fmt; /* this also skips first '%' */ + switch (*fmt) { + case '-': + flags |= LEFT; + goto repeat; + case '+': + flags |= PLUS; + goto repeat; + case ' ': + flags |= SPACE; + goto repeat; + case '#': + flags |= SPECIAL; + goto repeat; + case '0': + flags |= ZEROPAD; + goto repeat; + } + + /* get field width */ + field_width = -1; + if (isdigit(*fmt)) + field_width = skip_atoi(&fmt); + else if (*fmt == '*') { + ++fmt; + /* it's the next argument */ + field_width = va_arg(args, int); + if (field_width < 0) { + field_width = -field_width; + flags |= LEFT; + } + } + + /* get the precision */ + precision = -1; + if (*fmt == '.') { + ++fmt; + if (isdigit(*fmt)) + precision = skip_atoi(&fmt); + else if (*fmt == '*') { + ++fmt; + /* it's the next argument */ + precision = va_arg(args, int); + } + if (precision < 0) + precision = 0; + } + + /* get the conversion qualifier */ + qualifier = -1; + if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') { + qualifier = *fmt; + ++fmt; + } + + /* default base */ + base = 10; + + switch (*fmt) { + case 'c': + if (!(flags & LEFT)) + while (--field_width > 0) + *str++ = ' '; + *str++ = (unsigned char)va_arg(args, int); + while (--field_width > 0) + *str++ = ' '; + continue; + + case 's': + s = va_arg(args, char *); + len = strnlen(s, precision); + + if (!(flags & LEFT)) + while (len < field_width--) + *str++ = ' '; + for (i = 0; i < len; ++i) + *str++ = *s++; + while (len < field_width--) + *str++ = ' '; + continue; + + case 'p': + if (field_width == -1) { + field_width = 2 * sizeof(void *); + flags |= ZEROPAD; + } + str = number(str, + (unsigned long)va_arg(args, void *), 16, + field_width, precision, flags); + continue; + + case 'n': + if (qualifier == 'l') { + long *ip = va_arg(args, long *); + *ip = (str - buf); + } else { + int *ip = va_arg(args, int *); + *ip = (str - buf); + } + continue; + + case '%': + *str++ = '%'; + continue; + + /* integer number formats - set up the flags and "break" */ + case 'o': + base = 8; + break; + + case 'x': + flags |= SMALL; + case 'X': + base = 16; + break; + + case 'd': + case 'i': + flags |= SIGN; + case 'u': + break; + + default: + *str++ = '%'; + if (*fmt) + *str++ = *fmt; + else + --fmt; + continue; + } + if (qualifier == 'l') + num = va_arg(args, unsigned long); + else if (qualifier == 'h') { + num = (unsigned short)va_arg(args, int); + if (flags & SIGN) + num = (short)num; + } else if (flags & SIGN) + num = va_arg(args, int); + else + num = va_arg(args, unsigned int); + str = number(str, num, base, field_width, precision, flags); + } + *str = '\0'; + return str - buf; +} + +int sprintf(char *buf, const char *fmt, ...) +{ + va_list args; + int i; + + va_start(args, fmt); + i = vsprintf(buf, fmt, args); + va_end(args); + return i; +} + +int printf(const char *fmt, ...) +{ + char printf_buf[1024]; + va_list args; + int printed; + + va_start(args, fmt); + printed = vsprintf(printf_buf, fmt, args); + va_end(args); + + puts(printf_buf); + + return printed; +} From patchwork Wed Mar 1 05:34:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13155610 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFAEDC64EC7 for ; Wed, 1 Mar 2023 05:34:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229790AbjCAFey (ORCPT ); Wed, 1 Mar 2023 00:34:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229778AbjCAFeu (ORCPT ); Wed, 1 Mar 2023 00:34:50 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A57C832CDF for ; Tue, 28 Feb 2023 21:34:48 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-536c039f859so262085417b3.21 for ; Tue, 28 Feb 2023 21:34:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648888; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vuFdvY4jDZFH/7IewN0M02e7qpIZ5TbUelNV+nNNUX8=; b=Op3SxeqvDXwRWHPc9iCbYi7VSiR0ECKgf8hfGcboTmuZ7R3yoH3PULdCyoTRHkYfc6 +SEZnXu86rmrL8WYLohTj5+7ks81G1iDP222ve+lZj4++aXxeHOtRjYGC9aY1e6RTAN0 Vr6eLwVfn+Hoj7FKUaGi0ok5e5ymAsriWP36elWvNDqra40G5Y3vCzQ6Amdpk38eNrZq Qdc8IvXrKJMBg08jkcSflpI7jFbaK1sR8P7AVQqQWYVwGsegHJrs3XBtu7Oi/3I9GJlE NXjfWuRuKVYlweK/uyEd+De0BNfoz50o2yXZ5zRqtkeIDR5+BHx1m6fRLnw7S9rjzOfk +Lag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648888; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vuFdvY4jDZFH/7IewN0M02e7qpIZ5TbUelNV+nNNUX8=; b=utR6yej5izqYeVTGawI05YNpXruIYMaRghqlFqN8eMaCp6ghETMBfQhix3ZGg7LjZZ z0EV3jc8H0sLrwWvvFA3NZS4KMCjbTmfhiMfv5f2dqpFFXSOMrATo1IRF5uzG10cx0Sr ClmpY8hEzhxP0OApjQ7Yx8Fy2lXNI26jiIrCB10Hx/xnsSfI+aLKwCY4l3cQRqRYk+oU eJIGhCkKT0pVlRFjmbkkO6IUqf3sSwtI0YI2dvE0fpZd2LDZfYWWYDs45cVYCfKCRABl VKLldLTD3BQl8qAnqRxTx/g+fSD5eMOG79v2InVIvw6KlwZCOHEZUSTnqq4AH5+dLCFm 9FLA== X-Gm-Message-State: AO0yUKVCOJovjaYiw1h8owEZnfojgvFaY7Kb2pIZIUYNEmG2eih8YvX5 uY3a7Goevks99YpnPdUnFwDYb3D6mPgzn4/JIAFUH1v3iRCtoMFnE+4ky2zVclLKV7UY1uz2Fmq o8gD0Irx0AqAtTa3Cg2W0aALVKRinIYiLuWH8Laux9aWXnvxDxxEmmw6Q83jO5m6x+YYi X-Google-Smtp-Source: AK7set+TBpJWgoq5oaWUnsK9cKAX2XQF2nlG/hEYwWQYJjguhkxPB2nO2w2bMDuQDn5R28HADgFqzYZk6zb0Zrn/ X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a05:6902:4c2:b0:8ea:3d09:b125 with SMTP id v2-20020a05690204c200b008ea3d09b125mr2370302ybs.0.1677648887733; Tue, 28 Feb 2023 21:34:47 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:22 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-6-aaronlewis@google.com> Subject: [PATCH 5/8] KVM: selftests: Add vsprintf() to KVM selftests From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add string formatting support to the guest by adding a local version of vsprintf with no dependencies on LIBC. There were some minor fix-ups needed to get it compiling in selftests: - isdigit() was added as a local helper. - boot.h was switch for test_util.h. - printf and sprintf were removed. Support for printing will go through the ucall framework. Signed-off-by: Aaron Lewis --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/include/test_util.h | 2 ++ tools/testing/selftests/kvm/lib/printf.c | 33 ++++--------------- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index cda631f10526..ce577b564616 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -23,6 +23,7 @@ LIBKVM += lib/guest_modes.c LIBKVM += lib/io.c LIBKVM += lib/kvm_util.c LIBKVM += lib/memstress.c +LIBKVM += lib/printf.c LIBKVM += lib/rbtree.c LIBKVM += lib/sparsebit.c LIBKVM += lib/test_util.c diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index 80d6416f3012..261852598a4a 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -177,4 +177,6 @@ static inline uint32_t atoi_non_negative(const char *name, const char *num_str) return num; } +int vsprintf(char *buf, const char *fmt, va_list args); + #endif /* SELFTEST_KVM_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/lib/printf.c b/tools/testing/selftests/kvm/lib/printf.c index 1237beeb9540..d356e55cbc28 100644 --- a/tools/testing/selftests/kvm/lib/printf.c +++ b/tools/testing/selftests/kvm/lib/printf.c @@ -13,7 +13,12 @@ * */ -#include "boot.h" +#include "test_util.h" + +int isdigit(int ch) +{ + return (ch >= '0') && (ch <= '9'); +} static int skip_atoi(const char **s) { @@ -279,29 +284,3 @@ int vsprintf(char *buf, const char *fmt, va_list args) *str = '\0'; return str - buf; } - -int sprintf(char *buf, const char *fmt, ...) -{ - va_list args; - int i; - - va_start(args, fmt); - i = vsprintf(buf, fmt, args); - va_end(args); - return i; -} - -int printf(const char *fmt, ...) -{ - char printf_buf[1024]; - va_list args; - int printed; - - va_start(args, fmt); - printed = vsprintf(printf_buf, fmt, args); - va_end(args); - - puts(printf_buf); - - return printed; -} From patchwork Wed Mar 1 05:34:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13155611 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 191E2C64EC7 for ; Wed, 1 Mar 2023 05:34:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbjCAFe4 (ORCPT ); Wed, 1 Mar 2023 00:34:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229764AbjCAFev (ORCPT ); Wed, 1 Mar 2023 00:34:51 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4852C1ADC1 for ; Tue, 28 Feb 2023 21:34:50 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id q68-20020a632a47000000b004f74bc0c71fso4174839pgq.18 for ; Tue, 28 Feb 2023 21:34:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648889; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=eAAYyK8vwtil/XqRLlZoyfP+rbFvoTDOm4hFzsm92oA=; b=hqHGSORoVWqLmkQqW8pJAlq8KwtVrOcdIFvTvy3jGyyCttBrU7Np0jInFNxTVIpyRq tPyi+p8ck0xPYDBMS5Vq1TCrZZdkaeMEqSQRYW8Q+lH8oi7LyPAQJ3LDzx3i+RMx7Y/j BA6FbQptH4E0TiHpKjH/uCz+tID6ZGN5+iS2KLltHUonKon2cvso/TMJe/v3ZBmb+x8H aJBz0OQd2kCyHQLcbnlkDZwiM0jdcIM84so50pVl4smBi3AAKa8WlynL0bKPbWzxj79u 81Hp9L3X0cw6zx0iWMhVhnX9BkfnNSr0maHYnwLpmg8OPJ2ejCM7kC+WYg2hmtE5tOgB N/Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648889; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eAAYyK8vwtil/XqRLlZoyfP+rbFvoTDOm4hFzsm92oA=; b=2HrW8JIEpt3lBtRRigl17jBKhA3qMBr3EHDXVv5N/MQTjvrb4oUGoahcCzKvIyXlFK gxyxEv0TzJeff0MJAPuLCENjsdO2CdTEfjfilXxO0mh1u/buwaHKMEUpnt8B8FYH32CC OactS6uyXJssOcbPtdTxKK6RigdGQJCyQCIWsX+cu0Qy/sE/RWWSETkUPrqtsIGv/iRr g5WX3NNCbRlNdGv9asRgQc5tpb4+A3FWI9MSRRcLfGCPR3rZT98Km8xLQX6J6A4x0Jcc sEql0gfDd+n4kTd0ssMOkAOdA/NZE4yLH1FlrHLCHdyqHDoaPekZtA9wNIUZtfqyfWty Oxdg== X-Gm-Message-State: AO0yUKVtCWI5TTDDufgpC/BzSS55nBoxKmH6CiYGWTbwygNpu7djnXu6 w0VAUCZxfSt7gbTXe+lVhAP7XgqNZW/yIIfBAx2Ekc8VLShq9WaezJFs03LtLTwRyYwip5/h78V 1J3vYr/YY0Q45IVfCsqY5jZ3H6X15mAI3E6N8qrXSEmlREG4iwaFNeqmvfVP2o9bFJGQs X-Google-Smtp-Source: AK7set+NZYFgsUFEPSpp/NjURXEN8Mvt2W8zy9D0r8fOH4WYBAsbH/euV3o7uk9k6dHmL0J4k/9fv4wktcFAaA8v X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:902:f7d1:b0:19c:140d:aad7 with SMTP id h17-20020a170902f7d100b0019c140daad7mr1861398plw.4.1677648889508; Tue, 28 Feb 2023 21:34:49 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:23 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-7-aaronlewis@google.com> Subject: [PATCH 6/8] KVM: selftests: Add additional pages to the guest to accommodate ucall From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add additional pages to the guest to account for the number of pages the ucall framework uses. This is done in preparation for adding string formatting options to the guest through ucall helpers. Signed-off-by: Aaron Lewis --- tools/testing/selftests/kvm/include/ucall_common.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 4 ++++ tools/testing/selftests/kvm/lib/ucall_common.c | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h index 1a6aaef5ccae..0b1fde23729b 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -34,6 +34,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu); void ucall(uint64_t cmd, int nargs, ...); uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc); void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa); +int ucall_header_size(void); /* * Perform userspace call without any associated data. This bare call avoids diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 3ea24a5f4c43..e1d6a2f40d2d 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -307,6 +307,7 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus, uint64_t extra_mem_pages) { + uint64_t page_size = vm_guest_mode_params[mode].page_size; uint64_t nr_pages; TEST_ASSERT(nr_runnable_vcpus, @@ -335,6 +336,9 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode, */ nr_pages += (nr_pages + extra_mem_pages) / PTES_PER_MIN_PAGE * 2; + /* Account for the number of pages needed by ucall. */ + nr_pages += align_up(ucall_header_size(), page_size) / page_size; + return vm_adjust_num_guest_pages(mode, nr_pages); } diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index 2f0e2ea941cc..b6a75858fe0d 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -11,6 +11,11 @@ struct ucall_header { struct ucall ucalls[KVM_MAX_VCPUS]; }; +int ucall_header_size(void) +{ + return sizeof(struct ucall_header); +} + /* * ucall_pool holds per-VM values (global data is duplicated by each VM), it * must not be accessed from host code. From patchwork Wed Mar 1 05:34:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13155612 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC6CFC7EE23 for ; Wed, 1 Mar 2023 05:34:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229759AbjCAFe6 (ORCPT ); Wed, 1 Mar 2023 00:34:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229763AbjCAFey (ORCPT ); Wed, 1 Mar 2023 00:34:54 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64FB12F798 for ; Tue, 28 Feb 2023 21:34:51 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-536cb268ab8so262252477b3.17 for ; Tue, 28 Feb 2023 21:34:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648891; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G6nYnlLcyTdm/uK9hS3nWn446jGSSKDzz3oHNJyIs+4=; b=GuaHvVT7hudoJW9T/k6YDhaYWiJvN8iYDPbn+qtjn1BoL4DCIumoJ3fTwgkQ41hX3E pr9Fzd3oiQwNYrxYXEzvZzVkp6v1uyrLHsb9cGf+jFq2ffytcftqCyjT1VtaFczkd0Vs hyWxPzAVYQZDgqgUK4vqQGFqKmbblQjL8h+TEBbPK8ME21kH8RU3vQQhkYCUVW+LI+gj R6RFNODF3tCm4D3FBM3V9Bs+HfiBE5xS9xYNSevatpwz3YHbWEaCN4MuWYbRRUDq9ifT 5E9jUi9LMBKgMXa4wqWibEZA1dMJh2vvIiIZgwCpry36hcR/wyrnqFYkkt+Lax4FRbBb W9+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648891; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G6nYnlLcyTdm/uK9hS3nWn446jGSSKDzz3oHNJyIs+4=; b=qg5fUsNGZ7D78Wz60evlMJBGBHcSWD6UF3NsJ2TFvy0pDSOFp1hgOko8Dq/88YH5wy uGIS1QULiyspEku9sLKIc7ih7ssxhqCRvTOTCYnBmeLDFznExTGPIGecQoBOnqKWiFwo Hjz1WZ0FPuR1IIQjc2qPmHhGiywAvIBaCiAUDzqltAL18rqqgYAGmOnPKxg4PBpXEhd0 JqRWhp24hoabg0uhse1dL0qPtTf2kxkq77YR7ZLEIoKu11puoY9UPoGtR6Q03wVQQ9UQ aUdY2izGooDX+nROV1tBxUfmzrr2eIBkYMCDB0+AH+5Rimef3G6Pkbm0msyrArgn5Z79 ZDKQ== X-Gm-Message-State: AO0yUKX9b5LxrrmKDH0YjLCd3iVverpRyF+JjNqLSqYxLNjyFVNBDgk3 XPVEHP9+OOVyOM7cbAKaYhcVWS3rcZtzVEY8ibtroX/Tz0ljBiKcRQTtQE1LMm8ZdfnO/9I5ixR CKDEZnzS4vmDbuZX6/pZa8xgz+9s5716Q6bWef0xhhj/6e63KpL4ZEFkuUT+ZafbbAQOl X-Google-Smtp-Source: AK7set+h6Q/ib4KSfkPzjNAv8OUxL38UbyTZL3GyNaWAFpoGDr4HR+66N/VrLfsBsGUzYVjMl21dPVGdY4gVG7Dq X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a05:6902:118c:b0:a06:538f:265f with SMTP id m12-20020a056902118c00b00a06538f265fmr12652719ybu.4.1677648891058; Tue, 28 Feb 2023 21:34:51 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:24 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-8-aaronlewis@google.com> Subject: [PATCH 7/8] KVM: selftests: Add string formatting options to ucall From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add more flexibility to guest debugging and testing by adding GUEST_PRINTF() and GUEST_ASSERT_FMT() to the ucall framework. A buffer to hold the formatted string was added to the ucall struct. That allows the guest/host to avoid the problem of passing an arbitrary number of parameters between themselves when resolving the string. Instead, the string is resolved in the guest then passed back to the host to be logged. The formatted buffer is set to 1024 bytes which increases the size of the ucall struct. As a result, this will increase the number of pages requested for the guest. Signed-off-by: Aaron Lewis --- .../selftests/kvm/include/ucall_common.h | 19 +++++++++++++++++++ .../testing/selftests/kvm/lib/ucall_common.c | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h index 0b1fde23729b..2a4400b6761a 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -13,15 +13,18 @@ enum { UCALL_NONE, UCALL_SYNC, UCALL_ABORT, + UCALL_PRINTF, UCALL_DONE, UCALL_UNHANDLED, }; #define UCALL_MAX_ARGS 7 +#define UCALL_BUFFER_LEN 1024 struct ucall { uint64_t cmd; uint64_t args[UCALL_MAX_ARGS]; + char buffer[UCALL_BUFFER_LEN]; /* Host virtual address of this struct. */ struct ucall *hva; @@ -32,6 +35,7 @@ void ucall_arch_do_ucall(vm_vaddr_t uc); void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu); void ucall(uint64_t cmd, int nargs, ...); +void ucall_fmt(uint64_t cmd, const char *fmt, ...); uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc); void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa); int ucall_header_size(void); @@ -47,6 +51,7 @@ int ucall_header_size(void); #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) +#define GUEST_PRINTF(fmt, _args...) ucall_fmt(UCALL_PRINTF, fmt, ##_args) #define GUEST_DONE() ucall(UCALL_DONE, 0) enum guest_assert_builtin_args { @@ -56,6 +61,18 @@ enum guest_assert_builtin_args { GUEST_ASSERT_BUILTIN_NARGS }; +#define __GUEST_ASSERT_FMT(_condition, _condstr, format, _args...) \ +do { \ + if (!(_condition)) \ + ucall_fmt(UCALL_ABORT, \ + "Failed guest assert: " _condstr \ + " at %s:%ld\n " format, \ + __FILE__, __LINE__, ##_args); \ +} while (0) + +#define GUEST_ASSERT_FMT(_condition, format, _args...) \ + __GUEST_ASSERT_FMT(_condition, #_condition, format, ##_args) + #define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...) \ do { \ if (!(_condition)) \ @@ -81,6 +98,8 @@ do { \ #define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b) +#define REPORT_GUEST_ASSERT_FMT(_ucall) TEST_FAIL("%s", _ucall.buffer) + #define __REPORT_GUEST_ASSERT(_ucall, fmt, _args...) \ TEST_FAIL("%s at %s:%ld\n" fmt, \ (const char *)(_ucall).args[GUEST_ERROR_STRING], \ diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index b6a75858fe0d..92ebc5db1c41 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -54,7 +54,9 @@ static struct ucall *ucall_alloc(void) for (i = 0; i < KVM_MAX_VCPUS; ++i) { if (!test_and_set_bit(i, ucall_pool->in_use)) { uc = &ucall_pool->ucalls[i]; + uc->cmd = UCALL_NONE; memset(uc->args, 0, sizeof(uc->args)); + memset(uc->buffer, 0, sizeof(uc->buffer)); return uc; } } @@ -75,6 +77,23 @@ static void ucall_free(struct ucall *uc) clear_bit(uc - ucall_pool->ucalls, ucall_pool->in_use); } +void ucall_fmt(uint64_t cmd, const char *fmt, ...) +{ + struct ucall *uc; + va_list va; + + uc = ucall_alloc(); + uc->cmd = cmd; + + va_start(va, fmt); + vsprintf(uc->buffer, fmt, va); + va_end(va); + + ucall_arch_do_ucall((vm_vaddr_t)uc->hva); + + ucall_free(uc); +} + void ucall(uint64_t cmd, int nargs, ...) { struct ucall *uc; From patchwork Wed Mar 1 05:34:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13155613 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10FDBC64ED6 for ; Wed, 1 Mar 2023 05:35:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229763AbjCAFfA (ORCPT ); Wed, 1 Mar 2023 00:35:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229799AbjCAFey (ORCPT ); Wed, 1 Mar 2023 00:34:54 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78FA12E822 for ; Tue, 28 Feb 2023 21:34:53 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id z1-20020a170902d54100b00198bc9ba4edso6511164plf.21 for ; Tue, 28 Feb 2023 21:34:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677648893; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2BA7krnb0GctuZbm12Ga4THqgKqyBD2b5EGP8+Q0Tr4=; b=taih21hgGZ/uf1ZudAl5STMwJpEMdxlNpHTeGjWwTK8/lGLdIAKlNDwu3byFxiz9+w NBC2Br9ouCkbBsNJ3jT6H0A9ejeE0lgC+NGPEkolW+4IC7TPVhDHETELELSUk79tfq+3 Yha5BHQzp/DTrVn6xL89Lld0BkfFNOegn/myY+xA2FLVk6uN8iajN/PQOBQwbXQJeWwv F+XpU7zia7xrcOGCmzELpKp0IFdLlNXhGcWWTLW5Hg9CXDPQdzrLtANR3JhewuXftxiA ASuuF/E2BAAehcC/nyp5Hv8PU+C4kX9d/9P5FieXOKl+HvdsdLLzRxHo/mSIaiVjXYRj HcHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677648893; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2BA7krnb0GctuZbm12Ga4THqgKqyBD2b5EGP8+Q0Tr4=; b=TK6f6cjfehwm3iFMJozLMAalgFMYcVIc6PGqWWExijyHhJ7ZsOS5S2uaZlHIP/ULbx x6vHp8QwWTaFZ9aPQqOuyXC/Evl2n3lkfEuCVXUjJ4wW38Lxxzmj57mga+nbbr4lLzGy piZRJd/HlSdk94gV+s83gd9n9FBfg9+ytKQFEpRpa0WuPRF22tRL23YQnwkt/zpL9kdL gsiVeMrntigdFLtIUsRwJTilo6T7UXGJmSHh1f6/QoRSeJjKpP34IzEJslLqXBsJXVqH U9rqKamPUr94ohmeqhSfnpj+qSRMegGxsSdYrp5jhKD5GbukjedckRFjdCkmI8oIcmN5 08FQ== X-Gm-Message-State: AO0yUKUiCixpugZba0vagrsYIULVMizI4nJoYWwn5tyjoDYxCOZ33Sf0 LwskuGsEVvJjPcM/haPcNeEFHb2BR9M9hkzii7yyILoRi3kgowWCVlpmTKTDJE7UpRGxjwItWmt vAzsNzoQEYhTaejzQ7YTduD2ODqaWQLp0eYbRkA7TCNKIWpl/jtacFfep0eLgULaC0YTx X-Google-Smtp-Source: AK7set95KB7b7QbhaG6kLjJ3yhw2tNPowlMql/nr3FlWWnRpPUk+mpul36XQEWkyrmQVlacsrPm6TVBHcr2xl2hV X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:90b:374c:b0:230:b842:143e with SMTP id ne12-20020a17090b374c00b00230b842143emr2165241pjb.6.1677648892665; Tue, 28 Feb 2023 21:34:52 -0800 (PST) Date: Wed, 1 Mar 2023 05:34:25 +0000 In-Reply-To: <20230301053425.3880773-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230301053425.3880773-1-aaronlewis@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230301053425.3880773-9-aaronlewis@google.com> Subject: [PATCH 8/8] KVM: selftests: Add a selftest for guest prints and formatted asserts From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a selftest to demonstrate the use of prints and formatted asserts in the guest through the ucall framework. This test isn't intended to be accepted upstream and intentionally asserts at the end to demonstrate GUEST_ASSERT_FMT(). Signed-off-by: Aaron Lewis --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/guest_print_test.c | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/guest_print_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index ce577b564616..8f7238da6b84 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -63,6 +63,7 @@ TEST_PROGS_x86_64 += x86_64/nx_huge_pages_test.sh TEST_GEN_PROGS_x86_64 = x86_64/cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features +TEST_GEN_PROGS_x86_64 += x86_64/guest_print_test TEST_GEN_PROGS_x86_64 += x86_64/exit_on_emulation_failure_test TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock diff --git a/tools/testing/selftests/kvm/x86_64/guest_print_test.c b/tools/testing/selftests/kvm/x86_64/guest_print_test.c new file mode 100644 index 000000000000..2ee16a9b3ebf --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/guest_print_test.c @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A test for GUEST_PRINTF + * + * Copyright 2022, Google, Inc. and/or its affiliates. + */ + +#include +#include +#include +#include +#include + +#include "test_util.h" + +#include "kvm_util.h" +#include "processor.h" + +static void guest_code(void) +{ + const char *s = "from the guest!"; + uint64_t xcr0; + + set_cr4(get_cr4() | X86_CR4_OSXSAVE); + xcr0 = xgetbv(0); + + GUEST_PRINTF("XCR0 = 0x%lx\n", xcr0); + + /* + * Assert that XCR0 is at RESET, and that AVX-512 is not enabled. + * LIBC loves to use AVX-512 instructions when doing string + * formatting, which would be a pain to require of the guest as + * a prerequisite of using print formatting functions. + */ + GUEST_ASSERT_FMT(xcr0 == XFEATURE_MASK_FP, + "Expected an XCR0 value of 0x%lx, got 0x%lx instead.", + XFEATURE_MASK_FP, xcr0); + + /* + * When %s is used in the string format the guest's version of printf + * uses strnlen(), which will use AVX-512 instructions if routed + * through the LIBC version. To prevent that from happening strnlen() + * has been added to the string_override functions. + */ + GUEST_PRINTF("Hello %s\n", s); + + GUEST_SYNC(0); + + /* Demonstrate GUEST_ASSERT_FMT by invoking it. */ + xsetbv(0, xcr0 | XFEATURE_MASK_SSE); + xcr0 = xgetbv(0); + GUEST_ASSERT_FMT(xcr0 == XFEATURE_MASK_FP, + "Expected an XCR0 value of 0x%lx, got 0x%lx instead.", + XFEATURE_MASK_FP, xcr0); + + GUEST_DONE(); +} + +int main(int argc, char *argv[]) +{ + struct kvm_vcpu *vcpu; + struct kvm_run *run; + struct kvm_vm *vm; + struct ucall uc; + + /* Tell stdout not to buffer its content */ + setbuf(stdout, NULL); + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + run = vcpu->run; + + while (1) { + vcpu_run(vcpu); + + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "Unexpected exit reason: %u (%s),\n", + run->exit_reason, + exit_reason_str(run->exit_reason)); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_SYNC: + printf("Hello from the host!\n"); + break; + case UCALL_PRINTF: + printf("%s", uc.buffer); + break; + case UCALL_ABORT: + REPORT_GUEST_ASSERT_FMT(uc); + break; + case UCALL_DONE: + goto done; + default: + TEST_FAIL("Unknown ucall %lu", uc.cmd); + } + } + +done: + kvm_vm_free(vm); + return 0; +}