From patchwork Fri Mar 11 17:57:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12778491 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 28548C433F5 for ; Fri, 11 Mar 2022 17:58:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Yq3l8I085+lNCphwYgCOJfqqdUzjn67sEW4DGBrGNKI=; b=rTaQkoLsQf8MQiuK8mmy3Yc1X0 W9dR31xhHL/iXP8nvmMwyRWoMub+nfl3r1U5Mz1jN8mFarQYl/5+CDFs2BeEbOMUndmkrhJLWMv0W eVkqzQTdrPZpHouv9388tjfvgjrd9v4oTQjOU8Vsa66SvMvrDS3KevnUB4jbETkIRgCzL8kFjMwp2 jwegGSXFs5QV/3Dh7+Pt1GUvEUgj9f+fYF6Mjx1EIF5s00pT7vgyXLlOdHsRwijyzfDniTK8vf1an fW8+wHuNqkGdNiJizfL2J7rIIKTZmMqByhwWpcGqZnqWwnvCf1QV6s9wIEhqfkoMNdBOkVQeTlZVo oDgRUB7g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWK-00HYHW-5g; Fri, 11 Mar 2022 17:57:44 +0000 Received: from mail-il1-x149.google.com ([2607:f8b0:4864:20::149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWG-00HYG1-7r for linux-arm-kernel@lists.infradead.org; Fri, 11 Mar 2022 17:57:41 +0000 Received: by mail-il1-x149.google.com with SMTP id f18-20020a926a12000000b002be48b02bc6so5988576ilc.17 for ; Fri, 11 Mar 2022 09:57:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=BGsr6pN9enYniNk4zGr+nWX2xORuU1JL7wS+cLweFVM=; b=G8cB3TIlGOvwQlgqnb6Mv3A6ZDlk82Jx93jjWrLxun3vDuSleV+2ZENfuUS1eSZq+K IgQ+GM9Xo82V8Mun7Lw//GhahVM4rZ371b9ilQF12s4J9ZiMeW81c7rONn3ZnADLdkRR u2WDOuwVoayXZQ+3DSiHPrNnfLhLIsa/IdvllbJZZ7pvFOzW7aSZNQ8W7lifUWwLCuf2 DzxMLwqWeIeg6ntp2KnMOFHYpbHF7VkUH7zkgVfTqKkbCvbnG93z8jQ35Nzt65Ow3VLB yQHFcdwj2Grigd5frLaWlCQhcWFuBSzZAr/5LUkcx2FguOxyMPYLNFA5zfXBNAuS2oIg Zn/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=BGsr6pN9enYniNk4zGr+nWX2xORuU1JL7wS+cLweFVM=; b=KlmtJ6MED8R5KW3h/4VK9mTkgUXuZYqqJtLLlHjhYaMRPGQQU5mwavBcl4zuEQ5afi /sBJnR4lcSj1kexerx0HKhNIudlJE0HSKJZv25sMKbjFhpbJOPG7tKmMgyTdp9wRG+lB lvBmh7Yx/k1ykjVkkqHawLCIP8mZ3z+LIwMxRvdjFJ0dAvC0lyxxzg8V/POEmUZFrarl hhkUPjTXzOv71bFrLvEneBK143BB9A2V/Y49lvkPx+dLndCwXwcPtpyqdbeHwgeta3yb amZL+Wz7T7rxHXPRWMsvYJH2+YMcI71pDLa7kdLc9x40znUDKmC0vWfAMZDcF6hrgwx3 5H7A== X-Gm-Message-State: AOAM533UHdLxBbp7tLWrMHMtGs9GTkYJfv/Y2ryfuyecXFx1nxJgy6ps Ct1SXIhPiRIQepJJaOZNpyYHLzdymZA= X-Google-Smtp-Source: ABdhPJzy7TljJ49dSoKSsh7qQwadwo8KUyffYqNTXf0RMjBpnuNAtwt/ATgmZWQS3claaoNqk/pp2mmirz8= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6602:15c5:b0:646:4d0f:7728 with SMTP id f5-20020a05660215c500b006464d0f7728mr8810250iow.63.1647021458831; Fri, 11 Mar 2022 09:57:38 -0800 (PST) Date: Fri, 11 Mar 2022 17:57:13 +0000 In-Reply-To: <20220311175717.616958-1-oupton@google.com> Message-Id: <20220311175717.616958-2-oupton@google.com> Mime-Version: 1.0 References: <20220311174001.605719-1-oupton@google.com> <20220311175717.616958-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.723.g4982287a31-goog Subject: [RFC PATCH kvmtool 1/5] TESTONLY: Sync KVM headers with pending changes From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Will Deacon , Julien Thierry , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220311_095740_315413_7FC36FB7 X-CRM114-Status: GOOD ( 10.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Signed-off-by: Oliver Upton --- include/linux/kvm.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/linux/kvm.h b/include/linux/kvm.h index 1daa452..e5bb5f1 100644 --- a/include/linux/kvm.h +++ b/include/linux/kvm.h @@ -444,6 +444,8 @@ struct kvm_run { #define KVM_SYSTEM_EVENT_SHUTDOWN 1 #define KVM_SYSTEM_EVENT_RESET 2 #define KVM_SYSTEM_EVENT_CRASH 3 +#define KVM_SYSTEM_EVENT_WAKEUP 4 +#define KVM_SYSTEM_EVENT_SUSPEND 5 __u32 type; __u64 flags; } system_event; @@ -634,6 +636,7 @@ struct kvm_vapic_addr { #define KVM_MP_STATE_OPERATING 7 #define KVM_MP_STATE_LOAD 8 #define KVM_MP_STATE_AP_RESET_HOLD 9 +#define KVM_MP_STATE_SUSPENDED 10 struct kvm_mp_state { __u32 mp_state; @@ -1131,6 +1134,10 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204 #define KVM_CAP_ARM_MTE 205 #define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206 +#define KVM_CAP_VM_GPA_BITS 207 +#define KVM_CAP_XSAVE2 208 +#define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_ARM_SYSTEM_SUSPEND 210 #ifdef KVM_CAP_IRQ_ROUTING @@ -1162,11 +1169,20 @@ struct kvm_irq_routing_hv_sint { __u32 sint; }; +struct kvm_irq_routing_xen_evtchn { + __u32 port; + __u32 vcpu; + __u32 priority; +}; + +#define KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL ((__u32)(-1)) + /* gsi routing entry types */ #define KVM_IRQ_ROUTING_IRQCHIP 1 #define KVM_IRQ_ROUTING_MSI 2 #define KVM_IRQ_ROUTING_S390_ADAPTER 3 #define KVM_IRQ_ROUTING_HV_SINT 4 +#define KVM_IRQ_ROUTING_XEN_EVTCHN 5 struct kvm_irq_routing_entry { __u32 gsi; @@ -1178,6 +1194,7 @@ struct kvm_irq_routing_entry { struct kvm_irq_routing_msi msi; struct kvm_irq_routing_s390_adapter adapter; struct kvm_irq_routing_hv_sint hv_sint; + struct kvm_irq_routing_xen_evtchn xen_evtchn; __u32 pad[8]; } u; }; @@ -1208,6 +1225,7 @@ struct kvm_x86_mce { #define KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL (1 << 1) #define KVM_XEN_HVM_CONFIG_SHARED_INFO (1 << 2) #define KVM_XEN_HVM_CONFIG_RUNSTATE (1 << 3) +#define KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL (1 << 4) struct kvm_xen_hvm_config { __u32 flags; @@ -2031,4 +2049,7 @@ struct kvm_stats_desc { #define KVM_GET_STATS_FD _IO(KVMIO, 0xce) +/* Available with KVM_CAP_XSAVE2 */ +#define KVM_GET_XSAVE2 _IOR(KVMIO, 0xcf, struct kvm_xsave) + #endif /* __LINUX_KVM_H */ From patchwork Fri Mar 11 17:57:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12778492 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 78F3DC433EF for ; Fri, 11 Mar 2022 17:59:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Hz2cnKnjC0LJewY41vbHRNVdYkMBVk/5SWbg0rmZvZI=; b=oOs+gv7f1sVckEmqhn9Qea+Cox d260wvscg64/xJ2Dchdse4dPEtuhk/eXcaki4qP6Dxz0hsVP8GA/Jd2rx7JhtDwZSUdX9dUTI9iut HC1EWKHuU2pw6nO6heqneOB0u72KCuC+Q6Qa4Y/zxvz9CrcwLAqfNlF/yAPui/l7ldvZav2Dc7YC3 +ReNLacjwCVSTVSkfe0m7f3PQoyH1zhJ22FlGbZUsSDpF0oziDOMAzdXkCf5LCtns/WgM4b2pjDv+ q/TpB8J0D3MLD5ztR5ove6ot1tePYyEDh8nygYQbQDMUA2RuUsgnQ1by42J3aNtyPVssKVF8FktIn /JIfZXGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWW-00HYOJ-B2; Fri, 11 Mar 2022 17:57:56 +0000 Received: from mail-io1-xd4a.google.com ([2607:f8b0:4864:20::d4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWI-00HYGv-PJ for linux-arm-kernel@lists.infradead.org; Fri, 11 Mar 2022 17:57:44 +0000 Received: by mail-io1-xd4a.google.com with SMTP id d19-20020a0566022bf300b00645eba5c992so6810072ioy.4 for ; Fri, 11 Mar 2022 09:57:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=reY3JxO0Qz3iJXrFE+fLTwQO2MuKL1UPGAjn20Lqoao=; b=n/M+lnBk+N7zApRlOssTP+KALpqfkDUpxmbbZf9OVnsbAGTF24lyWr2yvrKj3eJhHh KlwuG3VKWe3524zQ0pEL4J6TBwFwPOoRAebN3n0vpides8mGXB+EbYFeJmmkZfQTfGzZ XYvyGdDyjkXVzSAzkiq91lVWeQmg14E+ZshflwW/ntyiQxAJxwuPNucacslawQCbPYYm WD3ZFVMIGBG78tv4VKrrv5Q/SbGCWnBDtlQP2/T9o/33WqeTY/4Pf+YD0SNDX81gEwaX G3KYqOFofFz3jHwq3lO7/3gzvWvfYvIGk9aFdVJLuYnlmsuLWzAaO0YJUmB9pJCY0LLG 0zmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=reY3JxO0Qz3iJXrFE+fLTwQO2MuKL1UPGAjn20Lqoao=; b=lmCerL5qdfjl3A4t97vzBRIekY7N9DSwpPY8ucrRwi0e44/aTe7e1Gx3SKL+zOVjAt thxg9YpIFEewmCe53dPxtpzujMons9OIpUffAQpjlf1/tt159gXPA9n3nRGsqOagQeHm upSkKQxp+2CVFMJyeYapWFyuxJ5B6txBj03hGJGlA5N4UTJV5pRPzvppQKXPKFCBHfIN gIhXpgiiyJ+165dPbcji/nn0FWsvaUBuES7RQSnuihvrnZdUk0kYI/Dgrm8fQ+EpXsUt i+U7uBDgjmrUSRmBLH2sBf1EvEkLeUjEoZXoBnOoMnxY9gbKgeAOZks0I9mijmSREllc 6vyA== X-Gm-Message-State: AOAM530Mmt5J+T2yAL6deiqV7XkXh3Y9raFcV0SUlD1j2pJO13mYo7/V 2It5tBRjglo0JgZfYurfk0O1K2NEsLQ= X-Google-Smtp-Source: ABdhPJwIV2dE5aKw/FZ4JxGUOLBoR07htZIM8UZWxJDfUBN8z5M+T3xIKkUCNUzF/rey8m9ZGZWSrRl15SE= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a02:a081:0:b0:317:b141:29ca with SMTP id g1-20020a02a081000000b00317b14129camr9018933jah.275.1647021461237; Fri, 11 Mar 2022 09:57:41 -0800 (PST) Date: Fri, 11 Mar 2022 17:57:14 +0000 In-Reply-To: <20220311175717.616958-1-oupton@google.com> Message-Id: <20220311175717.616958-3-oupton@google.com> Mime-Version: 1.0 References: <20220311174001.605719-1-oupton@google.com> <20220311175717.616958-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.723.g4982287a31-goog Subject: [RFC PATCH kvmtool 2/5] Allow architectures to hook KVM_EXIT_SYSTEM_EVENT From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Will Deacon , Julien Thierry , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220311_095742_868919_C04242B5 X-CRM114-Status: GOOD ( 10.75 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Certain system events require architecture-specific handling. Allow architectures to intervene for exits unhandled by the default exit handler. Signed-off-by: Oliver Upton --- include/kvm/kvm-cpu.h | 1 + kvm-cpu.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/kvm/kvm-cpu.h b/include/kvm/kvm-cpu.h index 0f16f8d..75e42d8 100644 --- a/include/kvm/kvm-cpu.h +++ b/include/kvm/kvm-cpu.h @@ -20,6 +20,7 @@ void kvm_cpu__run(struct kvm_cpu *vcpu); int kvm_cpu__start(struct kvm_cpu *cpu); bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu); int kvm_cpu__get_endianness(struct kvm_cpu *vcpu); +bool kvm_cpu__arch_handle_system_event(struct kvm_cpu *vcpu); int kvm_cpu__get_debug_fd(void); void kvm_cpu__set_debug_fd(int fd); diff --git a/kvm-cpu.c b/kvm-cpu.c index 7dec088..d615c37 100644 --- a/kvm-cpu.c +++ b/kvm-cpu.c @@ -23,6 +23,11 @@ int __attribute__((weak)) kvm_cpu__get_endianness(struct kvm_cpu *vcpu) return VIRTIO_ENDIAN_HOST; } +bool __attribute__((weak)) kvm_cpu__arch_handle_system_event(struct kvm_cpu *vcpu) +{ + return false; +} + void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu) { struct kvm_guest_debug debug = { @@ -224,6 +229,9 @@ int kvm_cpu__start(struct kvm_cpu *cpu) */ switch (cpu->kvm_run->system_event.type) { default: + if (kvm_cpu__arch_handle_system_event(cpu)) + break; + pr_warning("unknown system event type %d", cpu->kvm_run->system_event.type); /* fall through for now */ From patchwork Fri Mar 11 17:57:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12778501 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 54C10C433EF for ; Fri, 11 Mar 2022 17:59:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=omMqdFjSToX7TciELx7JZezFwSVuTS/d5icfrYbjEYA=; b=SSdFCbYdZJzPzP5tlrujl7aUOV E5RoEzh2Fqe3P00hxZupmebAAx2vVtu4W5XZa7BH+nGwO8XE8GaMyoLrWYr6towNhaSTulUfPF0we +MlwTOJFO+7pPUY33sxxctqke2bWOrXmi/2t/rjL2n3P8IqHzFS0HZUYtKlomgVeiJEFbgoejAe8P mZrlgf3fkHa6CmDcx7C17TWCGX7/VAprqfbkGMebmv33pQwFvQawy/G/u2zdLnVYWB3PYlXEBufWi zYiq9f/YkaxWPXRQ49wKm3Y8wg+hiarrolz9pPUOZckME7RdG4HpvW0qEQQE1X8YoTBXwOhWMOMVo 32BQKLQA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWl-00HYUc-9s; Fri, 11 Mar 2022 17:58:11 +0000 Received: from mail-io1-xd4a.google.com ([2607:f8b0:4864:20::d4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWK-00HYHq-7Z for linux-arm-kernel@lists.infradead.org; Fri, 11 Mar 2022 17:57:45 +0000 Received: by mail-io1-xd4a.google.com with SMTP id w28-20020a05660205dc00b00645d3cdb0f7so6762005iox.10 for ; Fri, 11 Mar 2022 09:57:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=BxxrnSwrIA4lSZ5dK5jxyr2r2tLhZUFOrxcbnlilois=; b=fa5WoWeDUYBwu4AqAKpVBlulmdk6ITEQVYZONtJ2u9pcPIxaUyOx/pOBQgOFg52Yd/ wSfGcWikT539Mu8oowRqbz3fzCMwnwM2i5FeKo6/JzeMJsuGnlAwC+RArs0TelINjAXJ YiiylZhsdD+TKZvFRp2bvcQju/VH3KxGC+y7ml99Qp85+xH7mQOVUVbQSpIhRWsaekAN zUx51OPbUCnvBVDxI6VrdMmWwvrjuDqtvEB+rxlLXPR1SULdF0qUS6NAVJnqAB18H1g1 DwvvD2Doyg3jUEWe2BlVwIkjcpUQlP74u15eK66wr6aSPlw3R/xOQ6q7VbMENibsurOl iEYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=BxxrnSwrIA4lSZ5dK5jxyr2r2tLhZUFOrxcbnlilois=; b=c0HEsUtEwZFKsG0VeCVxwu5R3BGxkn9uTk7a9e2onbLOzLlG0ene/6kt0KbZJzCtWN wPaa43y+PtFbYcGriaZbUIgi/mP/XBdvJaRsG26QpQ9bZyZW1SC5hHcTZKD8UbyCC+af DHdKfASCvVnt3aq755qAfKt82Z+4o6aDUZtpk0iETXRVC+baIH6XzhfCn7w1k5tiNXXH 3PQv2raatRZWmtztRCT0rLXNNse4D4f0f0KJLWziveroi5nUa4rF/FjFkFStaYKNQVvo 1DX9SrelU63N6G/eV+im5tcvocCG5k8my5Gihnme02GfweCJXeIPeoiSaXR2rvowv2G/ g75g== X-Gm-Message-State: AOAM530BKtpng959LdJyanfNfaB3EvFUKeJxvEWnA5JBozESNtRrK8x5 /pbaq5n/RRiDkvlIAGgGWjsbHprHSVc= X-Google-Smtp-Source: ABdhPJwX1T89M6pC5yjyrmOZYn4413egwXefNOi34Uw2IPjEjKr6OBMM3S5JvF/Oiv1+jz9CqWb3dK8SDTQ= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6638:2388:b0:314:1ec0:b012 with SMTP id q8-20020a056638238800b003141ec0b012mr9295720jat.224.1647021462801; Fri, 11 Mar 2022 09:57:42 -0800 (PST) Date: Fri, 11 Mar 2022 17:57:15 +0000 In-Reply-To: <20220311175717.616958-1-oupton@google.com> Message-Id: <20220311175717.616958-4-oupton@google.com> Mime-Version: 1.0 References: <20220311174001.605719-1-oupton@google.com> <20220311175717.616958-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.723.g4982287a31-goog Subject: [RFC PATCH kvmtool 3/5] ARM: Stash vcpu_init in the vCPU structure From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Will Deacon , Julien Thierry , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220311_095744_330209_C68390D7 X-CRM114-Status: GOOD ( 11.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org A subsequent change to kvmtool will require that a vCPU be reset more than once. Derive a valid target/feature set exactly once and stash that for later use. Signed-off-by: Oliver Upton --- arm/include/arm-common/kvm-cpu-arch.h | 18 +++++++++--------- arm/kvm-cpu.c | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/arm/include/arm-common/kvm-cpu-arch.h b/arm/include/arm-common/kvm-cpu-arch.h index 923d2c4..4027afe 100644 --- a/arm/include/arm-common/kvm-cpu-arch.h +++ b/arm/include/arm-common/kvm-cpu-arch.h @@ -10,18 +10,18 @@ struct kvm; struct kvm_cpu { pthread_t thread; - unsigned long cpu_id; - unsigned long cpu_type; - const char *cpu_compatible; + unsigned long cpu_id; + struct kvm_vcpu_init init; + const char *cpu_compatible; - struct kvm *kvm; - int vcpu_fd; - struct kvm_run *kvm_run; + struct kvm *kvm; + int vcpu_fd; + struct kvm_run *kvm_run; struct kvm_cpu_task *task; - u8 is_running; - u8 paused; - u8 needs_nmi; + u8 is_running; + u8 paused; + u8 needs_nmi; struct kvm_coalesced_mmio_ring *ring; diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c index 6a2408c..1ea56bb 100644 --- a/arm/kvm-cpu.c +++ b/arm/kvm-cpu.c @@ -128,7 +128,7 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) /* Populate the vcpu structure. */ vcpu->kvm = kvm; vcpu->cpu_id = cpu_id; - vcpu->cpu_type = vcpu_init.target; + vcpu->init = vcpu_init; vcpu->cpu_compatible = target->compatible; vcpu->is_running = true; From patchwork Fri Mar 11 17:57:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12778502 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 18906C433F5 for ; Fri, 11 Mar 2022 17:59:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=pbNUTtV9hKu+6nUxJl2AdkGbEQLOrYc6+B9/1sCPAPk=; b=FBzIK5tNBCFzEDXfCMXFygu7Jw A8QkhOZBCo5bsSxlnWelJymsyQC9zFSwhs2/3FOqmmDBOPLIYUolTozG6fAy0zSuHP0hi/lkOrMiP 3w+Gn0ZGy+h56VyrvcK/brInCblOglKyhwfv28YM0ROpGmnxsUADW3WZN1VrtspYa6WZGzWADUbO8 +XCzysq2Rxp5DqEh9MHcP22hkjeeGQYIIE3YiqLJ6zHp5KxN79Khq74iU5oxx3PCfvYheHUstiMfm xXBNaiw8Sw3RUnJwOs14M25KYN095i/zz9lO5Wu7p3GJ4FCBE1W6MvI9YBxVbbmmhasb65feSaOCu znCiC/NA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWy-00HYat-6S; Fri, 11 Mar 2022 17:58:24 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWM-00HYJ5-8G for linux-arm-kernel@lists.infradead.org; Fri, 11 Mar 2022 17:57:47 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2d726bd83a2so74672277b3.20 for ; Fri, 11 Mar 2022 09:57:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=iMUX4p8oYYdLskY6YlQfUR43FNfUoWfkZJK4wIrDvVI=; b=kbWvR7NSwT//jyD2uA/WkBGtSra3OAWNZLtQDDPfNKfREZh2jH89JBDrkrpTgQ+YNv BUozf2+Uo6ONunKNVgwoR5E9m7HxiNyKMvg/WGnR+sSX0XuH3X6nfPaGNxi3vJ37o/lM d+e2mKjeL9oPXKYywZpE/b7Xl/51yZO7v1R8Wc8yzsnBcH7u1/IxQKScxbUdOYyJ2EmK LOX+JHVm4kFAdEXVqPovN8vrY7fDTjstsHEv/kIjboyaWqWt/BP/PCBTiUiUXfkaA2m/ FTSD1xnG8bA32t2Mx3cnmlZRYON6s8EvQNUCszZAool1frje4oMd6fFK+SEr9nABWojl +9xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=iMUX4p8oYYdLskY6YlQfUR43FNfUoWfkZJK4wIrDvVI=; b=sdMAM98SolcKwxFLCydW/mtgciE+rKyPYOZDSLOgjk2RVCm/AhWRJzFsNVXzE6Nfih tMrwCK7C3DKRxrTKNOIn7/jAUMWd8pw3qZYdrtLnGE0qjSb+DTqMzWB2isBP6nLhKvSq UfUQ3dGRmInZkUnvAzu+OuyLYVfFTgSMD0pHTuvnld3zTIkQaCGf9tNbP9EHs74FAKO5 jAhrLCbKxBae85sF1VXepPMyB1aVPQfanYHwNCoSt+kYsWI0hVSkNRU07prBmxEmF5Ok EvrwxtDKA58qAfMmaQ/zhhrwkHO16eBhnGd3B4DGs/j3vqs/N8Yk91b7f8i+CBTyz93r z0cg== X-Gm-Message-State: AOAM530HwBrmB7JBPlCRIycK57xGrv2BTnA/nuXn6gez+D+1TZ/JZfZl Ct0AHN6nxyau6Ys9vwx9XMMxrNWfFS4= X-Google-Smtp-Source: ABdhPJzPRbWq+blf3W5J139AGKxE7e6TJnEjyAP/74A7vawiP9FcCu7oAu7fi1c5ZnCuQttujGbnOdDK+/Y= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a81:d847:0:b0:2d8:1c55:942a with SMTP id n7-20020a81d847000000b002d81c55942amr9375589ywl.260.1647021464480; Fri, 11 Mar 2022 09:57:44 -0800 (PST) Date: Fri, 11 Mar 2022 17:57:16 +0000 In-Reply-To: <20220311175717.616958-1-oupton@google.com> Message-Id: <20220311175717.616958-5-oupton@google.com> Mime-Version: 1.0 References: <20220311174001.605719-1-oupton@google.com> <20220311175717.616958-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.723.g4982287a31-goog Subject: [RFC PATCH kvmtool 4/5] ARM: Add a helper to re-init a vCPU From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Will Deacon , Julien Thierry , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220311_095746_320704_49389092 X-CRM114-Status: GOOD ( 12.02 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Create a helper that can be called to reinitialize a particular vCPU, or in other words issue the KVM_ARM_VCPU_INIT and KVM_ARM_VCPU_FINALIZE ioctls accordingly. Make use of the helper from kvm_cpu__arch_init() after the correct target/feature set have been identified. Calling KVM_ARM_VCPU_INIT with the same target more than once is benign. Signed-off-by: Oliver Upton --- arm/kvm-cpu.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c index 1ea56bb..164e399 100644 --- a/arm/kvm-cpu.c +++ b/arm/kvm-cpu.c @@ -35,6 +35,15 @@ int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target) return -ENOSPC; } +static void kvm_cpu__arch_reinit(struct kvm_cpu *vcpu) +{ + if (ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu->init) < 0) + die("KVM_ARM_VCPU_INIT failed"); + + if (kvm_cpu__configure_features(vcpu)) + die("Unable to configure requested vcpu features"); +} + struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) { struct kvm_arm_target *target; @@ -132,8 +141,7 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) vcpu->cpu_compatible = target->compatible; vcpu->is_running = true; - if (kvm_cpu__configure_features(vcpu)) - die("Unable to configure requested vcpu features"); + kvm_cpu__arch_reinit(vcpu); return vcpu; } From patchwork Fri Mar 11 17:57:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12778503 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6D343C433EF for ; Fri, 11 Mar 2022 18:00:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=/pqEHmqvvpXDRSzOvtkQIIVIAkGAjgAyBaoaKV7Xt68=; b=Acp00qBx8nE5xIgFe138btquAn pKsJOJaQxdOb3e2Sl3mjPgLBesPQUwWUn0/JU+438rS9Au+NeFADiPYzSZep8/7fLJXHK1nIL7rJm YejW0acB1IFv6ZkyMv/8yXhyYyIrcZ/p/W0kCJMYRZD5nuDS/LnLnEgdDOh67eH9LyOcD14MoZMcU vbZLB0p0izgaqZTQ9e1dCeusrW65Bb5ceSwwi1rk1HPmB7jvRklKV1cF6JAAecwbYBu8VwOWqNEE+ /tLnG2hj4XoEHYiZNyNY/x/8EnpEKkPzSIOol5zeFcYKOuuUGmHeiDnLFEdmaUYBFZSKLJVeFPD/W ZKzid0IA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjX9-00HYgm-TO; Fri, 11 Mar 2022 17:58:36 +0000 Received: from mail-io1-xd4a.google.com ([2607:f8b0:4864:20::d4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSjWN-00HYKG-PK for linux-arm-kernel@lists.infradead.org; Fri, 11 Mar 2022 17:57:52 +0000 Received: by mail-io1-xd4a.google.com with SMTP id w28-20020a05660205dc00b00645d3cdb0f7so6762092iox.10 for ; Fri, 11 Mar 2022 09:57:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=4Pv27JBfLZfH/CmvIWK/wVlW7nXuMPk8mN4kvQnrF8Y=; b=LkJFfaF+QDSe8RcIrq5JqVH3HGsEBs6ubj8L5VyPFuDtXYYvW5BAzKcm+lxvvq4MKL iw6Ci+dRx3gDW6Il7hzdXHUXBSOu2ZR3LpsAvK3ISNBGxKKNzwD7mB11XiEaz+Ef7jIh vTpUPJM1SnQPELYHqYezGl0SjuOO9A1V0ezaQ8f+PgJnQ+WwCGIW4wG5me1oNdrmXwxI DmBiVhMsWpXaKbTiNZMqkwamgxdJR2++mu51KA23u7F7k7p9sdvSrPnB/xpF/QzHDKy/ d8EFpp61p/23yPk7SAe/1JkL3dRSgNU0l0v5RNMsOVmyBmEaHQstYer3GWULVNLnaz72 +kvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=4Pv27JBfLZfH/CmvIWK/wVlW7nXuMPk8mN4kvQnrF8Y=; b=4PSx4Hk0nTNAWiLWErsOBrZAy60XhpzCAczgagvxgCChT6s/rNx8XB552jvm0CtHlw xdXdG1r+HUzL8Mq3uJlSpcLAoVUkOFrUtTSBt6kW9Ea6JiJ1YVyslVPI/PfR+wyUBfth g8p5s2wF/Y5KCXs4BOnDIaXOTvsiej1QddSX57prNC4aOGRDGZkdT875EBOfDbH/b+hq nYc+MXwbuYNIXMB4QqjZRbYSzIDKT74NSDCd+JsPy0QWueopZJdRSjplEz55Ylx38NWH 0dGV+bkhrvMsAUsfVbrtJu7XJT3o66z6MfuKp9eo7wpgMM1JAYElAvZEEuV1y3i4AA6K qkzw== X-Gm-Message-State: AOAM530dN5CGneSOxsxeojauoJgd4RqwTUt+s4pA/mT3NX4KxuD7Eyd2 dZQa6/uihvEITeckIoTej3OyjyfS3aM= X-Google-Smtp-Source: ABdhPJxMl3lNbjrYiw3dPTWd6Qtur4nKZbyrPolqLB3Wtk5CoxQO4h6cUg3DgclyKsOnvihxhaP1UAZjIDM= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6638:d93:b0:317:ca63:2d38 with SMTP id l19-20020a0566380d9300b00317ca632d38mr3186286jaj.171.1647021466505; Fri, 11 Mar 2022 09:57:46 -0800 (PST) Date: Fri, 11 Mar 2022 17:57:17 +0000 In-Reply-To: <20220311175717.616958-1-oupton@google.com> Message-Id: <20220311175717.616958-6-oupton@google.com> Mime-Version: 1.0 References: <20220311174001.605719-1-oupton@google.com> <20220311175717.616958-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.723.g4982287a31-goog Subject: [RFC PATCH kvmtool 5/5] ARM: Implement PSCI SYSTEM_SUSPEND From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Will Deacon , Julien Thierry , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220311_095747_873147_DA6C6A8F X-CRM114-Status: GOOD ( 17.27 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org KVM_CAP_ARM_SYSTEM_SUSPEND allows VMMs to trap guest attempts to use the PSCI SYSTEM_SUSPEND hypercall. Make use of that capability in KVM tool to implement guest suspend support. Add some minimal SMCCC register handling (params, return values) for AArch32 and AArch64. Perform only the required sanity check before suspending the VM by ensuring all other vCPUs besides the caller are powered off. Leverage KVM_MP_STATE_SUSPENDED to emulate the suspend as an architectural WFI. Signed-off-by: Oliver Upton --- arm/aarch32/kvm-cpu.c | 72 ++++++++++++++++++++++++ arm/aarch64/kvm-cpu.c | 66 ++++++++++++++++++++++ arm/include/arm-common/kvm-cpu-arch.h | 5 ++ arm/kvm-cpu.c | 81 +++++++++++++++++++++++++++ arm/kvm.c | 9 +++ 5 files changed, 233 insertions(+) diff --git a/arm/aarch32/kvm-cpu.c b/arm/aarch32/kvm-cpu.c index 95fb1da..dd5f70f 100644 --- a/arm/aarch32/kvm-cpu.c +++ b/arm/aarch32/kvm-cpu.c @@ -130,3 +130,75 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu) die("KVM_GET_ONE_REG failed (LR_svc)"); dprintf(debug_fd, " LR_svc: 0x%x\n", data); } + +u64 kvm_cpu__smccc_get_arg1(struct kvm_vcpu *vcpu) +{ + struct kvm_one_reg reg; + u32 data; + + reg.addr = (u64)&data; + reg.id = ARM_CORE_REG(usr_regs.ARM_r1); + if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) + die("KVM_GET_ONE_REG failed (r1)"); + + return data; +} + +u64 kvm_cpu__smccc_get_arg2(struct kvm_vcpu *vcpu) +{ + struct kvm_one_reg reg; + u32 data; + + reg.addr = (u64)&data; + reg.id = ARM_CORE_REG(usr_regs.ARM_r2); + if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) + die("KVM_GET_ONE_REG failed (r2)"); + + return data; +} + +void kvm_cpu__smccc_return(struct kvm_cpu *vcpu, u64 a0, u64 a1, u64 a2, u64 a3) +{ + struct kvm_one_reg reg; + u32 data; + + reg.addr = (u64)&data; + + data = (u32)a0; + reg.id = ARM_CORE_REG(usr_regs.ARM_r0); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (r0)"); + + data = (u32)a1; + reg.id = ARM_CORE_REG(usr_regs.ARM_r1); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (r1)"); + + data = (u32)a2; + reg.id = ARM_CORE_REG(usr_regs.ARM_r2); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (r2)"); + + data = (u32)a3; + reg.id = ARM_CORE_REG(usr_regs.ARM_r3); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (r3)"); +} + +void kvm_cpu__psci_set_entry(struct kvm_cpu *vcpu, u64 entry_addr, u64 context_id) +{ + struct kvm_one_reg reg; + u32 data; + + reg.addr = (u64)&data; + + data = (u32)entry_addr; + reg.id = ARM_CORE_REG(usr_regs.ARM_pc); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (pc)"); + + data = (u32)context_id; + reg.id = ARM64_CORE_REG(usr_regs.ARM_r0); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (r0)"); +} diff --git a/arm/aarch64/kvm-cpu.c b/arm/aarch64/kvm-cpu.c index 9f3e858..3118c54 100644 --- a/arm/aarch64/kvm-cpu.c +++ b/arm/aarch64/kvm-cpu.c @@ -254,3 +254,69 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu) die("KVM_GET_ONE_REG failed (lr)"); dprintf(debug_fd, " LR: 0x%lx\n", data); } + +u64 kvm_cpu__smccc_get_arg1(struct kvm_cpu *vcpu) +{ + struct kvm_one_reg reg; + u64 data; + + reg.addr = (u64)&data; + reg.id = ARM64_CORE_REG(regs.regs[1]); + if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) + die("KVM_GET_ONE_REG failed (x1)"); + + return data; +} + +u64 kvm_cpu__smccc_get_arg2(struct kvm_cpu *vcpu) +{ + struct kvm_one_reg reg; + u64 data; + + reg.addr = (u64)&data; + reg.id = ARM64_CORE_REG(regs.regs[2]); + if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) + die("KVM_GET_ONE_REG failed (x2)"); + + return data; +} + +void kvm_cpu__smccc_return(struct kvm_cpu *vcpu, u64 a0, u64 a1, u64 a2, u64 a3) +{ + struct kvm_one_reg reg; + + reg.addr = (u64)&a0; + reg.id = ARM64_CORE_REG(regs.regs[0]); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (x0)"); + + reg.addr = (u64)&a1; + reg.id = ARM64_CORE_REG(regs.regs[1]); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (x1)"); + + reg.addr = (u64)&a2; + reg.id = ARM64_CORE_REG(regs.regs[2]); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (x2)"); + + reg.addr = (u64)&a3; + reg.id = ARM64_CORE_REG(regs.regs[3]); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (x3)"); +} + +void kvm_cpu__psci_set_entry(struct kvm_cpu *vcpu, u64 entry_addr, u64 context_id) +{ + struct kvm_one_reg reg; + + reg.addr = (u64)&entry_addr; + reg.id = ARM64_CORE_REG(regs.pc); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (pc)"); + + reg.addr = (u64)&context_id; + reg.id = ARM64_CORE_REG(regs.regs[0]); + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) + die("KVM_SET_ONE_REG failed (x0)"); +} diff --git a/arm/include/arm-common/kvm-cpu-arch.h b/arm/include/arm-common/kvm-cpu-arch.h index 4027afe..17179c2 100644 --- a/arm/include/arm-common/kvm-cpu-arch.h +++ b/arm/include/arm-common/kvm-cpu-arch.h @@ -59,4 +59,9 @@ static inline bool kvm_cpu__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, unsigned long kvm_cpu__get_vcpu_mpidr(struct kvm_cpu *vcpu); +u64 kvm_cpu__smccc_get_arg1(struct kvm_cpu *vcpu); +u64 kvm_cpu__smccc_get_arg2(struct kvm_cpu *vcpu); +void kvm_cpu__smccc_return(struct kvm_cpu *vcpu, u64 a0, u64 a1, u64 a2, u64 a3); +void kvm_cpu__psci_set_entry(struct kvm_cpu *vcpu, u64 entry_addr, u64 context_id); + #endif /* ARM_COMMON__KVM_CPU_ARCH_H */ diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c index 164e399..677f8bb 100644 --- a/arm/kvm-cpu.c +++ b/arm/kvm-cpu.c @@ -163,3 +163,84 @@ bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu) { } + +static void kvm_cpu__arm_suspend(struct kvm_cpu *vcpu) +{ + struct kvm_mp_state mp_state = { + .mp_state = KVM_MP_STATE_SUSPENDED, + }; + + if (ioctl(vcpu->vcpu_fd, KVM_SET_MP_STATE, &mp_state) < 0) + die("KVM_SET_MP_STATE failed"); +} + +static void kvm_cpu__wakeup(struct kvm_cpu *vcpu) +{ + u64 entry_addr, context_id; + + entry_addr = kvm_cpu__smccc_get_arg1(vcpu); + context_id = kvm_cpu__smccc_get_arg2(vcpu); + + /* + * The resuming CPU could have been a secondary CPU at boot. Ensure the + * vCPU is made runnable. + */ + vcpu->init.features[0] &= ~(1ul << KVM_ARM_VCPU_POWER_OFF); + + kvm_cpu__arch_reinit(vcpu); + kvm_cpu__reset_vcpu(vcpu); + kvm_cpu__psci_set_entry(vcpu, entry_addr, context_id); +} + +static void kvm_cpu__psci_system_suspend(struct kvm_cpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + bool denied = false; + int i; + + /* + * Mark the caller as paused before actually pausing the VM. This avoids + * the hazard of attempting to acquire the pause_lock in the SIGKVMPAUSE + * handler from the thread that already holds it. + */ + vcpu->paused = 1; + + kvm__pause(kvm); + for (i = 0; i < kvm->nrcpus; i++) { + struct kvm_cpu *tmp = kvm->cpus[i]; + struct kvm_mp_state mp_state; + + if (vcpu == tmp) + continue; + + if (ioctl(tmp->vcpu_fd, KVM_GET_MP_STATE, &mp_state) < 0) + die("KVM_GET_MP_STATE failed"); + + if (mp_state.mp_state != KVM_MP_STATE_STOPPED) { + denied = true; + break; + } + } + + if (!denied) + kvm_cpu__arm_suspend(vcpu); + else + kvm_cpu__smccc_return(vcpu, PSCI_RET_DENIED, 0, 0, 0); + + vcpu->paused = 0; + kvm__continue(kvm); +} + +bool kvm_cpu__arch_handle_system_event(struct kvm_cpu *vcpu) +{ + switch (vcpu->kvm_run->system_event.type) { + case KVM_SYSTEM_EVENT_SUSPEND: + kvm_cpu__psci_system_suspend(vcpu); + return true; + case KVM_SYSTEM_EVENT_WAKEUP: + kvm_cpu__wakeup(vcpu); + return true; + default: + return false; + } +} diff --git a/arm/kvm.c b/arm/kvm.c index 80d233f..8ae4711 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -86,6 +86,15 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) /* Create the virtual GIC. */ if (gic__create(kvm, kvm->cfg.arch.irqchip)) die("Failed to create virtual GIC"); + + if (kvm__supports_extension(kvm, KVM_CAP_ARM_SYSTEM_SUSPEND)) { + struct kvm_enable_cap cap = { + .cap = KVM_CAP_ARM_SYSTEM_SUSPEND, + }; + + if (ioctl(kvm->vm_fd, KVM_ENABLE_CAP, &cap) < 0) + die("Enabling KVM_CAP_ARM_SYSTEM_SUSPEND failed"); + } } #define FDT_ALIGN SZ_2M