From patchwork Tue Apr 6 08:26:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 12184505 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8290C43460 for ; Tue, 6 Apr 2021 08:27:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92CF3613C0 for ; Tue, 6 Apr 2021 08:27:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240209AbhDFI1J (ORCPT ); Tue, 6 Apr 2021 04:27:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:57018 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240288AbhDFI1G (ORCPT ); Tue, 6 Apr 2021 04:27:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617697619; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5pS0wh7N3KpBX2QM5qANzzkJZX1lhQrm39b/3Z7eSz4=; b=gavnwrK1Xn5MZoLVxDKa/P/rfjDkioEsJhkHE6tr9IChC5eFq+zkmPYj1rAD5QPzk5eQGk 80M8De8rgVoXvKLz4FA0UMOWnXM+3G9SbhSWEbEq+x+zr6Ga0nXapNXO4Q0u6aYuE/GRwo amdc2WYSJUv/QF9AZW66MXbsFOM0Ufo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-567-5s6b4nFBMl-osbL7yVzetw-1; Tue, 06 Apr 2021 04:26:55 -0400 X-MC-Unique: 5s6b4nFBMl-osbL7yVzetw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BBA0C18C8C05; Tue, 6 Apr 2021 08:26:53 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-114-253.ams2.redhat.com [10.36.114.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA9A25D9D0; Tue, 6 Apr 2021 08:26:49 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Emanuele Giuseppe Esposito , Jim Mattson , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Shuah Khan , Alexander Graf , Andrew Jones , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v3 1/4] KVM: x86: Fix a spurious -E2BIG in KVM_GET_EMULATED_CPUID Date: Tue, 6 Apr 2021 10:26:39 +0200 Message-Id: <20210406082642.20115-2-eesposit@redhat.com> In-Reply-To: <20210406082642.20115-1-eesposit@redhat.com> References: <20210406082642.20115-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When retrieving emulated CPUID entries, check for an insufficient array size if and only if KVM is actually inserting an entry. If userspace has a priori knowledge of the exact array size, KVM_GET_EMULATED_CPUID will incorrectly fail due to effectively requiring an extra, unused entry. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vitaly Kuznetsov --- arch/x86/kvm/cpuid.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 6bd2f8b830e4..27059ddf9f0a 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -567,34 +567,33 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array, static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func) { - struct kvm_cpuid_entry2 *entry; - - if (array->nent >= array->maxnent) - return -E2BIG; + struct kvm_cpuid_entry2 entry; - entry = &array->entries[array->nent]; - entry->function = func; - entry->index = 0; - entry->flags = 0; + memset(&entry, 0, sizeof(entry)); + entry.function = func; switch (func) { case 0: - entry->eax = 7; - ++array->nent; + entry.eax = 7; break; case 1: - entry->ecx = F(MOVBE); - ++array->nent; + entry.ecx = F(MOVBE); break; case 7: - entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; - entry->eax = 0; - entry->ecx = F(RDPID); - ++array->nent; - default: + entry.flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; + entry.eax = 0; + entry.ecx = F(RDPID); break; + default: + goto out; } + if (array->nent >= array->maxnent) + return -E2BIG; + + memcpy(&array->entries[array->nent++], &entry, sizeof(entry)); + +out: return 0; } From patchwork Tue Apr 6 08:26:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 12184507 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 458E4C433B4 for ; Tue, 6 Apr 2021 08:27:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29855613C5 for ; Tue, 6 Apr 2021 08:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240435AbhDFI1Z (ORCPT ); Tue, 6 Apr 2021 04:27:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:46938 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240449AbhDFI1Z (ORCPT ); Tue, 6 Apr 2021 04:27:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617697637; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i9iBaLNrNdHy5Myw5Ru7jrMYIVd0xBvqef8GrfatpKg=; b=XZrHfyNgqWRghH45YislxzDw2oAanFOEzkTeM0Ehz0vtcH2BQQWNsvZ1u9ieg8KbZdGuow IYds0e/WEuIAGVRcB/i7IBl08ay4w2aydPQbZA+evdOhuw2o+bQcXat3MHtKAhyhZmyWqE AEbY2za7TRoHOMYMH4rAm+U9bclr9R4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-284-Rw1bV6WmOlKOJQfKrQ7jkA-1; Tue, 06 Apr 2021 04:27:13 -0400 X-MC-Unique: Rw1bV6WmOlKOJQfKrQ7jkA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 33F10190A7A0; Tue, 6 Apr 2021 08:27:12 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-114-253.ams2.redhat.com [10.36.114.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id 202195D9D0; Tue, 6 Apr 2021 08:26:53 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Emanuele Giuseppe Esposito , Jim Mattson , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Shuah Khan , Alexander Graf , Andrew Jones , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v3 2/4] Documentation: KVM: update KVM_GET_EMULATED_CPUID ioctl description Date: Tue, 6 Apr 2021 10:26:40 +0200 Message-Id: <20210406082642.20115-3-eesposit@redhat.com> In-Reply-To: <20210406082642.20115-1-eesposit@redhat.com> References: <20210406082642.20115-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM_GET_EMULATED_CPUID returns -E2BIG if the nent field of struct kvm_cpuid2 is smaller than the actual entries, while it adjusts nent if the provided amount is bigger than the actual amount. Update documentation accordingly. ENOMEM is just returned if the allocation fails, like all other calls. Signed-off-by: Emanuele Giuseppe Esposito --- Documentation/virt/kvm/api.rst | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 307f2fcf1b02..8ba23bc2a625 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -3404,12 +3404,10 @@ which features are emulated by kvm instead of being present natively. Userspace invokes KVM_GET_EMULATED_CPUID by passing a kvm_cpuid2 structure with the 'nent' field indicating the number of entries in -the variable-size array 'entries'. If the number of entries is too low -to describe the cpu capabilities, an error (E2BIG) is returned. If the -number is too high, the 'nent' field is adjusted and an error (ENOMEM) -is returned. If the number is just right, the 'nent' field is adjusted -to the number of valid entries in the 'entries' array, which is then -filled. +the variable-size array 'entries'. +If the number of entries is too low to describe the cpu +capabilities, an error (E2BIG) is returned. If the number is too high, +the 'nent' field is adjusted and the entries array is filled. The entries returned are the set CPUID bits of the respective features which kvm emulates, as returned by the CPUID instruction, with unknown From patchwork Tue Apr 6 08:26:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 12184509 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27C70C433B4 for ; Tue, 6 Apr 2021 08:27:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 02F20613C2 for ; Tue, 6 Apr 2021 08:27:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240467AbhDFI1a (ORCPT ); Tue, 6 Apr 2021 04:27:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:28249 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240449AbhDFI1a (ORCPT ); Tue, 6 Apr 2021 04:27:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617697642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nYGtSBURRU7WTSXl83fHRTlB7M6uxRtcmancVGQ+Jas=; b=W/HVsTz5PvGPcCAAbiZMGfWga4QjJ+TTuAcDU1ZuoNxzo+W/tE+bO14ud0NtCrdcdbbNCV LUq9b0sAwDWaA2z2D8kltZVzCSWyRj0Le05m8uoi5unOEGUtTbBMjEBAc8KmgtD/XUD/i9 CzLpgq3CH1L3/seZNWyUqB/NjSp+JwY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-500-e1zQkeYqNIexaNoPEd0Afw-1; Tue, 06 Apr 2021 04:27:21 -0400 X-MC-Unique: e1zQkeYqNIexaNoPEd0Afw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7C974107B789; Tue, 6 Apr 2021 08:27:18 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-114-253.ams2.redhat.com [10.36.114.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id BF8745D9DC; Tue, 6 Apr 2021 08:27:12 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Emanuele Giuseppe Esposito , Jim Mattson , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Shuah Khan , Alexander Graf , Andrew Jones , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v3 3/4] selftests: add kvm_get_emulated_cpuid to processor.h Date: Tue, 6 Apr 2021 10:26:41 +0200 Message-Id: <20210406082642.20115-4-eesposit@redhat.com> In-Reply-To: <20210406082642.20115-1-eesposit@redhat.com> References: <20210406082642.20115-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org As the similar kvm_get_supported_cpuid(), kvm_get_emulated_cpuid allocates and gets a struct kvm_cpuid2 filled with emulated features. Signed-off-by: Emanuele Giuseppe Esposito --- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/lib/x86_64/processor.c | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 0b30b4e15c38..ae1b9530e187 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -353,6 +353,7 @@ void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_msr_list *kvm_get_msr_index_list(void); uint64_t kvm_get_feature_msr(uint64_t msr_index); struct kvm_cpuid2 *kvm_get_supported_cpuid(void); +struct kvm_cpuid2 *kvm_get_emulated_cpuid(void); struct kvm_cpuid2 *vcpu_get_cpuid(struct kvm_vm *vm, uint32_t vcpuid); void vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid, diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index e676fe40bfe6..2ea14421bdfe 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -669,6 +669,39 @@ struct kvm_cpuid2 *kvm_get_supported_cpuid(void) return cpuid; } +/* + * KVM Emulated CPUID Get + * + * Input Args: None + * + * Output Args: + * + * Return: The emulated KVM CPUID + * + * Get the guest CPUID emulated by KVM. + */ +struct kvm_cpuid2 *kvm_get_emulated_cpuid(void) +{ + static struct kvm_cpuid2 *cpuid; + int ret; + int kvm_fd; + + if (cpuid) + return cpuid; + + cpuid = allocate_kvm_cpuid2(); + kvm_fd = open(KVM_DEV_PATH, O_RDONLY); + if (kvm_fd < 0) + exit(KSFT_SKIP); + + ret = ioctl(kvm_fd, KVM_GET_EMULATED_CPUID, cpuid); + TEST_ASSERT(ret == 0, "KVM_GET_EMULATED_CPUID failed %d %d\n", + ret, errno); + + close(kvm_fd); + return cpuid; +} + /* * KVM Get MSR * From patchwork Tue Apr 6 08:26:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 12184511 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74C64C43461 for ; Tue, 6 Apr 2021 08:27:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 53F21613C2 for ; Tue, 6 Apr 2021 08:27:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233326AbhDFI1l (ORCPT ); Tue, 6 Apr 2021 04:27:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:49402 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244345AbhDFI1h (ORCPT ); Tue, 6 Apr 2021 04:27:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617697649; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=huJU/7eTGA9L+HVGecfG2Pp+AIuWsVkRoZrlV5LSukk=; b=Z0NrYMK1cFozuFadwgkHxX+C1ZoQwH9W1NchDQmY6kabawRVF6xK3i0rBKY2b3Y+Lg2os1 GAWQs4Ew+dtn/kwCTGw+jcuZ//6nLe+j19I8P+CoyBFHEsRjS9cWOP9dsIUGS+0Ry3+YuC RSQRuCdEMSeNuvMAxiC9ri5ge7n0DuU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-184-mSbVVsygNgS1L7Mhxqab1g-1; Tue, 06 Apr 2021 04:27:26 -0400 X-MC-Unique: mSbVVsygNgS1L7Mhxqab1g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 656401800D50; Tue, 6 Apr 2021 08:27:24 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-114-253.ams2.redhat.com [10.36.114.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id 835D55D9D0; Tue, 6 Apr 2021 08:27:18 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Emanuele Giuseppe Esposito , Jim Mattson , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Shuah Khan , Alexander Graf , Andrew Jones , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v3 4/4] selftests: KVM: extend get_cpuid_test to include KVM_GET_EMULATED_CPUID Date: Tue, 6 Apr 2021 10:26:42 +0200 Message-Id: <20210406082642.20115-5-eesposit@redhat.com> In-Reply-To: <20210406082642.20115-1-eesposit@redhat.com> References: <20210406082642.20115-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Extend the get_cpuid_test.c selftest to include the KVM_GET_EMULATED_CPUID ioctl. Since the behavior and functionality is similar to KVM_GET_SUPPORTED_CPUID, we only check additionally: 1) checks for corner case in the nent field of the struct kvm_cpuid2. 2) sets and gets it as cpuid from the guest VM Signed-off-by: Emanuele Giuseppe Esposito --- .../selftests/kvm/x86_64/get_cpuid_test.c | 90 ++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/get_cpuid_test.c b/tools/testing/selftests/kvm/x86_64/get_cpuid_test.c index 9b78e8889638..b9f0fba1b0ea 100644 --- a/tools/testing/selftests/kvm/x86_64/get_cpuid_test.c +++ b/tools/testing/selftests/kvm/x86_64/get_cpuid_test.c @@ -13,6 +13,7 @@ #include "processor.h" #define VCPU_ID 0 +#define MAX_NENT 1000 /* CPUIDs known to differ */ struct { @@ -137,7 +138,8 @@ static void run_vcpu(struct kvm_vm *vm, uint32_t vcpuid, int stage) } } -struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, struct kvm_cpuid2 *cpuid) +static struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, + struct kvm_cpuid2 *cpuid) { int size = sizeof(*cpuid) + cpuid->nent * sizeof(cpuid->entries[0]); vm_vaddr_t gva = vm_vaddr_alloc(vm, size, @@ -150,9 +152,84 @@ struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, struct return guest_cpuids; } +static struct kvm_cpuid2 *alloc_custom_kvm_cpuid2(int nent) +{ + struct kvm_cpuid2 *cpuid; + size_t size; + + size = sizeof(*cpuid); + size += nent * sizeof(struct kvm_cpuid_entry2); + cpuid = calloc(1, size); + if (!cpuid) { + perror("malloc"); + abort(); + } + + cpuid->nent = nent; + + return cpuid; +} + +static void clean_entries_kvm_cpuid2(struct kvm_cpuid2 *cpuid) +{ + size_t size; + int old_nent = cpuid->nent; + + size = sizeof(*cpuid); + size += MAX_NENT * sizeof(struct kvm_cpuid_entry2); + memset(cpuid, 0, size); + cpuid->nent = old_nent; +} + +static void test_emulated_entries(struct kvm_vm *vm) +{ + int res, right_nent; + struct kvm_cpuid2 *cpuid; + + cpuid = alloc_custom_kvm_cpuid2(MAX_NENT); + + /* 0 nent, return E2BIG */ + cpuid->nent = 0; + res = _kvm_ioctl(vm, KVM_GET_EMULATED_CPUID, cpuid); + TEST_ASSERT(res == -1 && errno == E2BIG, "nent=0 should fail as E2BIG"); + clean_entries_kvm_cpuid2(cpuid); + + /* high nent, set the entries and adjust */ + cpuid->nent = MAX_NENT; + res = _kvm_ioctl(vm, KVM_GET_EMULATED_CPUID, cpuid); + TEST_ASSERT(res == 0, "nent > actual nent should not fail"); + right_nent = cpuid->nent; + clean_entries_kvm_cpuid2(cpuid); + + /* high nent, set the entries and adjust */ + cpuid->nent++; + res = _kvm_ioctl(vm, KVM_GET_EMULATED_CPUID, cpuid); + TEST_ASSERT(res == 0, "nent > actual nent should not fail"); + TEST_ASSERT(right_nent == cpuid->nent, "nent should be always the same"); + clean_entries_kvm_cpuid2(cpuid); + + /* low nent, return E2BIG */ + if (right_nent > 1) { + cpuid->nent = 1; + res = _kvm_ioctl(vm, KVM_GET_EMULATED_CPUID, cpuid); + TEST_ASSERT(res == -1 && errno == E2BIG, "nent=1 should fail"); + clean_entries_kvm_cpuid2(cpuid); + } + + /* exact nent */ + cpuid->nent = right_nent; + res = _kvm_ioctl(vm, KVM_GET_EMULATED_CPUID, cpuid); + TEST_ASSERT(res == 0, "nent == actual nent should not fail"); + TEST_ASSERT(cpuid->nent == right_nent, + "KVM_GET_EMULATED_CPUID should be invaried when nent is exact"); + clean_entries_kvm_cpuid2(cpuid); + + free(cpuid); +} + int main(void) { - struct kvm_cpuid2 *supp_cpuid, *cpuid2; + struct kvm_cpuid2 *supp_cpuid, *emul_cpuid, *cpuid2; vm_vaddr_t cpuid_gva; struct kvm_vm *vm; int stage; @@ -171,5 +248,14 @@ int main(void) for (stage = 0; stage < 3; stage++) run_vcpu(vm, VCPU_ID, stage); + if (kvm_check_cap(KVM_CAP_EXT_EMUL_CPUID)) { + emul_cpuid = kvm_get_emulated_cpuid(); + vcpu_set_cpuid(vm, VCPU_ID, emul_cpuid); + cpuid2 = vcpu_get_cpuid(vm, VCPU_ID); + + test_emulated_entries(vm); + compare_cpuids(emul_cpuid, cpuid2); + } + kvm_vm_free(vm); }