Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3037965pxb; Sun, 28 Feb 2021 23:07:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJwG6IDUArm5OF03huDRAp1sKybOg2pjyXXUJuR2K/YQxLTzpHmE8qqFSufEpa2XJHTlRHkW X-Received: by 2002:a05:6402:30b9:: with SMTP id df25mr15217451edb.136.1614582479052; Sun, 28 Feb 2021 23:07:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614582479; cv=none; d=google.com; s=arc-20160816; b=ZqSM5rQO78Lw1qumzbN9GStz3C3n6Ci3CJIM6NRN6aXUbDdI/XIvGOL8largPtXPds E4GauLcLR+yTl+4RFyuR/T0F4M50wYVaEaP4TZ/qAj34XZ4gTXpv3XI08zwt4hwJ40P6 6wihmQ4st84nE3Yn2LIG5n4eU+1muvK2WXW3CTpbzG6A1eZZ0GjalP1UrH6/mzDJ9spm qYdd8zvEaHO9zT8WZoIdcatoJv68gAGi4FP5MbaW52bBO/Qv9sT9k4RoAjIZQ9njE+YJ fAmGR+GQwAorGWyLbcs99EmLttng/JB++UW+d3NQV6Z95OqWKvJnuMB7NodqkI6eEk85 Ocrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=rUij1ru3LQDov1hHUa+uigvd/Hwfn6Mjl+M44KzY+c0=; b=I2aVkXmHCuCIRPvmQGbrc3F3EPLfQZ36+cydKJfAMDvBzBUa4SFIT1sy+p8zmE/ePd v6GTTk6s2Kmg96J8Ex+BuCXiejCisSz0IULeVug14vioj2phRU/p0itv5gdBIQpFZKLD rsrFjO/ZPqlIXGcFXJlFI7tRlNhFPK2k6SkgXak2c4itZzzvMad2PIV26yUTmB10TgZO FL0lhiUaIRYJsGFkD9xIAn5b90mzJ6/PPgpYCNpe0dhlBp2eei0L0FZf9vBOENuTaCY3 XbQgtn2jV36rgSwf2UVx4J37p2zG1m9NECdPJ9cH741gMYcezbRhEORUoK4osVvQFqY6 RmBg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id he39si11270744ejc.512.2021.02.28.23.07.36; Sun, 28 Feb 2021 23:07:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232296AbhCAHGJ (ORCPT + 99 others); Mon, 1 Mar 2021 02:06:09 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:12656 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232222AbhCAHBg (ORCPT ); Mon, 1 Mar 2021 02:01:36 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Dprfs0cxlzlR8Z; Mon, 1 Mar 2021 14:57:25 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.187.128) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.498.0; Mon, 1 Mar 2021 14:59:24 +0800 From: Yanan Wang To: , , CC: Paolo Bonzini , Ben Gardon , "Sean Christopherson" , Vitaly Kuznetsov , Andrew Jones , Peter Xu , Marc Zyngier , Ingo Molnar , Adrian Hunter , Jiri Olsa , "Arnaldo Carvalho de Melo" , Arnd Bergmann , Michael Kerrisk , Thomas Gleixner , , , , Yanan Wang Subject: [RFC PATCH v3 4/8] KVM: selftests: Make a generic helper to get vm guest mode strings Date: Mon, 1 Mar 2021 14:59:12 +0800 Message-ID: <20210301065916.11484-5-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210301065916.11484-1-wangyanan55@huawei.com> References: <20210301065916.11484-1-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.174.187.128] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For generality and conciseness, make an API which can be used in all kvm libs and selftests to get vm guest mode strings. And the index i is checked in the API in case of possiable faults. Reviewed-by: Andrew Jones Suggested-by: Sean Christopherson Signed-off-by: Yanan Wang --- .../testing/selftests/kvm/include/kvm_util.h | 4 +-- tools/testing/selftests/kvm/lib/kvm_util.c | 29 ++++++++++++------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 2d7eb6989e83..f52a7492f47f 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -68,9 +68,6 @@ enum vm_guest_mode { #define MIN_PAGE_SIZE (1U << MIN_PAGE_SHIFT) #define PTES_PER_MIN_PAGE ptes_per_page(MIN_PAGE_SIZE) -#define vm_guest_mode_string(m) vm_guest_mode_string[m] -extern const char * const vm_guest_mode_string[]; - struct vm_guest_mode_params { unsigned int pa_bits; unsigned int va_bits; @@ -84,6 +81,7 @@ int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap); int vcpu_enable_cap(struct kvm_vm *vm, uint32_t vcpu_id, struct kvm_enable_cap *cap); void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size); +const char *vm_guest_mode_string(uint32_t i); struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm); void kvm_vm_free(struct kvm_vm *vmp); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index d787cb802b4a..cc22c4ab7d67 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -141,17 +141,24 @@ static void vm_open(struct kvm_vm *vm, int perm) "rc: %i errno: %i", vm->fd, errno); } -const char * const vm_guest_mode_string[] = { - "PA-bits:52, VA-bits:48, 4K pages", - "PA-bits:52, VA-bits:48, 64K pages", - "PA-bits:48, VA-bits:48, 4K pages", - "PA-bits:48, VA-bits:48, 64K pages", - "PA-bits:40, VA-bits:48, 4K pages", - "PA-bits:40, VA-bits:48, 64K pages", - "PA-bits:ANY, VA-bits:48, 4K pages", -}; -_Static_assert(sizeof(vm_guest_mode_string)/sizeof(char *) == NUM_VM_MODES, - "Missing new mode strings?"); +const char *vm_guest_mode_string(uint32_t i) +{ + static const char * const strings[] = { + [VM_MODE_P52V48_4K] = "PA-bits:52, VA-bits:48, 4K pages", + [VM_MODE_P52V48_64K] = "PA-bits:52, VA-bits:48, 64K pages", + [VM_MODE_P48V48_4K] = "PA-bits:48, VA-bits:48, 4K pages", + [VM_MODE_P48V48_64K] = "PA-bits:48, VA-bits:48, 64K pages", + [VM_MODE_P40V48_4K] = "PA-bits:40, VA-bits:48, 4K pages", + [VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages", + [VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages", + }; + _Static_assert(sizeof(strings)/sizeof(char *) == NUM_VM_MODES, + "Missing new mode strings?"); + + TEST_ASSERT(i < NUM_VM_MODES, "Guest mode ID %d too big", i); + + return strings[i]; +} const struct vm_guest_mode_params vm_guest_mode_params[] = { { 52, 48, 0x1000, 12 }, -- 2.23.0