From patchwork Sat Feb 4 02:41:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 52721 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1171567wrn; Fri, 3 Feb 2023 18:58:20 -0800 (PST) X-Google-Smtp-Source: AK7set+WAcr2IJEcjyAxki6VFfHaElTEVpUpndu/zYJRYMpsg8nVnPnyjS7nibzqI1t5suceRMP7 X-Received: by 2002:a17:906:8559:b0:88c:a43d:81ba with SMTP id h25-20020a170906855900b0088ca43d81bamr13537754ejy.11.1675479500446; Fri, 03 Feb 2023 18:58:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675479500; cv=none; d=google.com; s=arc-20160816; b=UtMz79L9Gasvosb3QJ8eZvNHaCuqyQTI6k+92sqiyGMr842VtKuJ6kMxcAJouGqoHH GfitoDV5RT7rz4PM3/x2VLANAuu3thHaYV5axAjaSFqBK0tTPvCrTbP6GKzf8DM17d0O ANdFHK5ZjbjTJUrPuuLVOGuR3w5MItuAlOxI91hh8zJwYmT+6awgmmqA9eWsUzFAUy3m bWPNV0A36g0856XHUkQw9J+J+FHVqB1tiyzgbf1C/Fqaoiss/R5xKle3V8b+Ue4amwjr odBS3pDFWqSGYLJcSzLPvahAZO4A1OxDxYHl0tA+WazIQe2UyxrnsJmyCiq9PTfBHvSj 8DaA== 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=yKig0vjfTYuO47fr0RMiwa4iEAaNVUOtpbtuBUQfHMc=; b=NYH1uw5y5/3QUZfO5BHo/37Ls1VmRyfh1qEgMLRXQtR9ETZq3E+z0alHxQuVxM+sPU ah5x3oJxnRIA0ewMTFpkksDctxhIgTgpzX8j7oisotNd+cECuntZZihkN/zfNlcD0Yfd 1F0i1UWgPmEJ87wKfPgVf1rqL3Okc8ehwQ2O6ifoHjn0rEcWeDVJZC/HKT5EdM4Bg6ml S6BoZ+6r10o0b+Zxu412b01nXhMvM57pZQ6pULl8OxO/PvbMIjVdQ/lKhKC9WDW+5ktH LhK/w739TVP6bGQzFjZq0FHqXw3HLZ9BFNnXZ0SbHuho/1psTuSootIj99Biq3KbAbn9 f7ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=LtupmW8F; 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 fh4-20020a1709073a8400b008787bb7c56asi4068374ejc.408.2023.02.03.18.57.50; Fri, 03 Feb 2023 18:58:20 -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=LtupmW8F; 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 S232893AbjBDCmE (ORCPT + 99 others); Fri, 3 Feb 2023 21:42:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232699AbjBDCmA (ORCPT ); Fri, 3 Feb 2023 21:42:00 -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 86CB586EA0 for ; Fri, 3 Feb 2023 18:41:58 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id n34-20020a056a000d6200b005912ead88aeso3553495pfv.3 for ; Fri, 03 Feb 2023 18:41:58 -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=yKig0vjfTYuO47fr0RMiwa4iEAaNVUOtpbtuBUQfHMc=; b=LtupmW8Ff5jvfqLv1vPvuPNb0joReY2g7hk69icp1jpAX97zUbFPcSB9sGbw4CMgA6 NmTQfFnT637bJY2ZaLJisqM1rIHEt/Od/b8F2woINex4KYZ2D5G/0LUN7yNh+quetIlx UWnvGEA0jhXv5Iu+9TCmgSo0yLnWYhdolQlia2LJNLU5/x/o5jqWwey/Sh7h4vVFMsvG MZrNLehZnk+WncUl+pLiqz1hBBUr/eXC7czqi/aLBNNmfbZjeFE2+MlSffoN/xgLTjbI y2HuNiofopMaGmZ8U/e2PPaRLIJPX3BAb3jgvUsmmVORpzYdzB2LK1jqo+8rXVa86g+O TZmw== 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=yKig0vjfTYuO47fr0RMiwa4iEAaNVUOtpbtuBUQfHMc=; b=ADshQwpDqzI+jJ6dSqxAhNbGhlnkqZBAI4j99Q/FnBbVUtDAtVShWv5QUampIowkPk e2AIQd1d2jyLQacznUMPywPNoSsznxMhEl+MxmCV+p0Xh4JBur0GeSCdStb0WoAkGZb9 TqeqU0ha3TXC8e+rseVKzp5KjPdR1ZL5tirQn1SJefVtblRlkgOaujR8SOvdmHLeBUNY mESVtxA6GChE+KgyyLJC+dkuGDAQ5fTPZaQJMdHp92tqyajsSTdpLwd0/A7IRgqUfNJn R6STlzQ+XoOhCqLU/krO+5tW/w1NULUQkJk7mMDTHA6GhNedRK80dEQtZP+ssQOvC0y1 GfWw== X-Gm-Message-State: AO0yUKUH/XcZUNDafJAsS2Zpjy329JwyzXduXp2RYiRLSsLw9kS35/ww NUPwH3PKoVEBoVsj4bwYYoXEx3/KnDg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ab4d:b0:196:21d8:fb05 with SMTP id ij13-20020a170902ab4d00b0019621d8fb05mr2915228plb.20.1675478517892; Fri, 03 Feb 2023 18:41:57 -0800 (PST) Reply-To: Sean Christopherson Date: Sat, 4 Feb 2023 02:41:49 +0000 In-Reply-To: <20230204024151.1373296-1-seanjc@google.com> Mime-Version: 1.0 References: <20230204024151.1373296-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog Message-ID: <20230204024151.1373296-3-seanjc@google.com> Subject: [PATCH v2 2/4] KVM: selftests: Add helpers to make Xen-style VMCALL/VMMCALL hypercalls From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Christopherson , David Woodhouse 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=ham 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?1756867592609853152?= X-GMAIL-MSGID: =?utf-8?q?1756867592609853152?= Add wrappers to do hypercalls using VMCALL/VMMCALL and Xen's register ABI (as opposed to full Xen-style hypercalls through a hypervisor provided page). Using the common helpers dedups a pile of code, and uses the native hypercall instruction when running on AMD. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 2 + .../selftests/kvm/lib/x86_64/processor.c | 10 +++ .../selftests/kvm/x86_64/xen_shinfo_test.c | 63 +++---------------- 3 files changed, 21 insertions(+), 54 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..90387ddcb2a9 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1063,6 +1063,8 @@ uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr); uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t a3); +uint64_t __xen_hypercall(uint64_t nr, uint64_t a0, void *a1); +void xen_hypercall(uint64_t nr, uint64_t a0, void *a1); void __vm_xsave_require_permission(int bit, const char *name); diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index ff901cb47ffc..c39a4353ba19 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -1161,6 +1161,16 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, return X86_HYPERCALL("a"(nr), "b"(a0), "c"(a1), "d"(a2), "S"(a3)); } +uint64_t __xen_hypercall(uint64_t nr, uint64_t a0, void *a1) +{ + return X86_HYPERCALL("a"(nr), "D"(a0), "S"(a1)); +} + +void xen_hypercall(uint64_t nr, uint64_t a0, void *a1) +{ + GUEST_ASSERT(!__xen_hypercall(nr, a0, a1)); +} + const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void) { static struct kvm_cpuid2 *cpuid; diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c index 6a838df69174..1380947af1fe 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -228,15 +228,8 @@ static void guest_code(void) /* Our turn. Deliver event channel (to ourselves) with * EVTCHNOP_send hypercall. */ - unsigned long rax; struct evtchn_send s = { .port = 127 }; - __asm__ __volatile__ ("vmcall" : - "=a" (rax) : - "a" (__HYPERVISOR_event_channel_op), - "D" (EVTCHNOP_send), - "S" (&s)); - - GUEST_ASSERT(rax == 0); + xen_hypercall(__HYPERVISOR_event_channel_op, EVTCHNOP_send, &s); guest_wait_for_irq(); @@ -245,24 +238,15 @@ static void guest_code(void) /* Deliver "outbound" event channel to an eventfd which * happens to be one of our own irqfds. */ s.port = 197; - __asm__ __volatile__ ("vmcall" : - "=a" (rax) : - "a" (__HYPERVISOR_event_channel_op), - "D" (EVTCHNOP_send), - "S" (&s)); - - GUEST_ASSERT(rax == 0); + xen_hypercall(__HYPERVISOR_event_channel_op, EVTCHNOP_send, &s); guest_wait_for_irq(); GUEST_SYNC(13); /* Set a timer 100ms in the future. */ - __asm__ __volatile__ ("vmcall" : - "=a" (rax) : - "a" (__HYPERVISOR_set_timer_op), - "D" (rs->state_entry_time + 100000000)); - GUEST_ASSERT(rax == 0); + xen_hypercall(__HYPERVISOR_set_timer_op, + rs->state_entry_time + 100000000, NULL); GUEST_SYNC(14); @@ -284,37 +268,19 @@ static void guest_code(void) .timeout = 0, }; - __asm__ __volatile__ ("vmcall" : - "=a" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax == 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); GUEST_SYNC(17); /* Poll for an unset port and wait for the timeout. */ p.timeout = 100000000; - __asm__ __volatile__ ("vmcall" : - "=a" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax == 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); GUEST_SYNC(18); /* A timer will wake the masked port we're waiting on, while we poll */ p.timeout = 0; - __asm__ __volatile__ ("vmcall" : - "=a" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax == 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); GUEST_SYNC(19); @@ -322,13 +288,7 @@ static void guest_code(void) * actual interrupt, while we're polling on a different port. */ ports[0]++; p.timeout = 0; - __asm__ __volatile__ ("vmcall" : - "=a" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax == 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); guest_wait_for_irq(); @@ -363,12 +323,7 @@ static void guest_code(void) * timer IRQ is dropped due to an invalid event channel. */ for (i = 0; i < 100 && !guest_saw_irq; i++) - asm volatile("vmcall" - : "=a" (rax) - : "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p) - : "memory"); + __xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); /* * Re-send the timer IRQ if it was (likely) dropped due to the timer