From nobody Sat Sep 13 09:10:48 2025 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 BA439C64EC4 for ; Sat, 4 Feb 2023 02:42:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232957AbjBDCmF (ORCPT ); Fri, 3 Feb 2023 21:42:05 -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-Google-Smtp-Source: AK7set+GastANbRQWUNsBSM1yNh87W0jU7lCKIwfhNQnZ7ejoI3Z3tHCYK4aK2tViQ5b81qKbARX3HZQtfo= 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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); =20 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); =20 void __vm_xsave_require_permission(int bit, const char *name); =20 diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/tes= ting/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, uin= t64_t a1, uint64_t a2, return X86_HYPERCALL("a"(nr), "b"(a0), "c"(a1), "d"(a2), "S"(a3)); } =20 +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/t= esting/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) =20 /* Our turn. Deliver event channel (to ourselves) with * EVTCHNOP_send hypercall. */ - unsigned long rax; struct evtchn_send s =3D { .port =3D 127 }; - __asm__ __volatile__ ("vmcall" : - "=3Da" (rax) : - "a" (__HYPERVISOR_event_channel_op), - "D" (EVTCHNOP_send), - "S" (&s)); - - GUEST_ASSERT(rax =3D=3D 0); + xen_hypercall(__HYPERVISOR_event_channel_op, EVTCHNOP_send, &s); =20 guest_wait_for_irq(); =20 @@ -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 =3D 197; - __asm__ __volatile__ ("vmcall" : - "=3Da" (rax) : - "a" (__HYPERVISOR_event_channel_op), - "D" (EVTCHNOP_send), - "S" (&s)); - - GUEST_ASSERT(rax =3D=3D 0); + xen_hypercall(__HYPERVISOR_event_channel_op, EVTCHNOP_send, &s); =20 guest_wait_for_irq(); =20 GUEST_SYNC(13); =20 /* Set a timer 100ms in the future. */ - __asm__ __volatile__ ("vmcall" : - "=3Da" (rax) : - "a" (__HYPERVISOR_set_timer_op), - "D" (rs->state_entry_time + 100000000)); - GUEST_ASSERT(rax =3D=3D 0); + xen_hypercall(__HYPERVISOR_set_timer_op, + rs->state_entry_time + 100000000, NULL); =20 GUEST_SYNC(14); =20 @@ -284,37 +268,19 @@ static void guest_code(void) .timeout =3D 0, }; =20 - __asm__ __volatile__ ("vmcall" : - "=3Da" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax =3D=3D 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); =20 GUEST_SYNC(17); =20 /* Poll for an unset port and wait for the timeout. */ p.timeout =3D 100000000; - __asm__ __volatile__ ("vmcall" : - "=3Da" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax =3D=3D 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); =20 GUEST_SYNC(18); =20 /* A timer will wake the masked port we're waiting on, while we poll */ p.timeout =3D 0; - __asm__ __volatile__ ("vmcall" : - "=3Da" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax =3D=3D 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); =20 GUEST_SYNC(19); =20 @@ -322,13 +288,7 @@ static void guest_code(void) * actual interrupt, while we're polling on a different port. */ ports[0]++; p.timeout =3D 0; - __asm__ __volatile__ ("vmcall" : - "=3Da" (rax) : - "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p)); - - GUEST_ASSERT(rax =3D=3D 0); + xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); =20 guest_wait_for_irq(); =20 @@ -363,12 +323,7 @@ static void guest_code(void) * timer IRQ is dropped due to an invalid event channel. */ for (i =3D 0; i < 100 && !guest_saw_irq; i++) - asm volatile("vmcall" - : "=3Da" (rax) - : "a" (__HYPERVISOR_sched_op), - "D" (SCHEDOP_poll), - "S" (&p) - : "memory"); + __xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p); =20 /* * Re-send the timer IRQ if it was (likely) dropped due to the timer --=20 2.39.1.519.gcb327c4b5f-goog