From patchwork Wed Nov 8 11:17:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162961 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842863vqo; Wed, 8 Nov 2023 03:21:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IF8FIA8orI+9jF6ZPpEm9LeCjJbizow2pVQxeFDA5uguTyi5Xqr8wGM8BGAGvJb8iXXm+QT X-Received: by 2002:a17:903:41cd:b0:1cc:6fa6:ab62 with SMTP id u13-20020a17090341cd00b001cc6fa6ab62mr1726146ple.29.1699442507371; Wed, 08 Nov 2023 03:21:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442507; cv=none; d=google.com; s=arc-20160816; b=JUvtotTXtuJG1k0ZU64w9IQJg5q5LDN/ZB+6A4Ld/1tl5dvR94c9W4AJtxyZ1bOadQ fdoGh217XOchgRky8CISLAmCXLy70iK519XOYgZec/VJVb0Ob+c0MwrTdYNM1jDDDqvj wvyLOkf8OdBAXxSHdpFkhZocvfsJvsrHzXNCgFXqlblA5FIpwIjTzk6A8kPE0KFzWWPY Hwg/AY1g1K4ZZ62WW2RVLw6nGBg5ciT86QFHz6pHfNnei0zPw36QQKoiaI6vb93SxlFo 3yQWNMe/zljdLN+/Frjf+I79DpytnYmUSMbXPNpCiMCAc4SaEzAav0oWLQM5upuSrKiG 0OaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=O25EX7uu/BZNg+CriP/M93KalXQ52Vh7t09zkAv2Ya4=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=k1TrE26HACffCsMQ9k+c7TY+PQ+OA+3Ok4MwIpvgkzc5gWklzZ2jo0UQ5tJjDH6YKV b+UDSQWV54N60mJSoT7Sp69NKw+XL0hDxrM1IzYbY+RHYfEsQvqtp3da1a6oU9yEum95 XH117Y0ExdgE4b8tTfKHCOA3UOkQsTYO1xqHDOCtLrnCPoffrHvogQA/jXx3XAyWWZI5 Rxi14FCcBPeDp9b06QR2Jqis04lF2Go5TqIb9NApPTgQBOSaBdyXpSxqLEM9OB65fZAH x8gG/R82oZiDWV1mIU22gURbyIgBBtb6kWiQxCT+5P7iWesv5mnniHtzhSvFpIglcv7U eH5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=aykVI1Qf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id n18-20020a170902d2d200b001c61817e049si2271000plc.67.2023.11.08.03.21.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=aykVI1Qf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 6CD9580907B7; Wed, 8 Nov 2023 03:21:01 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344600AbjKHLUh (ORCPT + 32 others); Wed, 8 Nov 2023 06:20:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344193AbjKHLUb (ORCPT ); Wed, 8 Nov 2023 06:20:31 -0500 Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CC5F101; Wed, 8 Nov 2023 03:20:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442429; x=1730978429; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O25EX7uu/BZNg+CriP/M93KalXQ52Vh7t09zkAv2Ya4=; b=aykVI1QfFrWWdgvl5K28nJc2jLfTCZz/bsoiUlASsTpFbkDS41nZfZr3 D6azOjVhkVl7UL9tqeYijznMZNP5k8HOiCYJHwm4bfyvTjk6jYm4M++87 qhmwsdlgVGNs+hOQearaS91Oy2ZVQ5ATF/zBhTpyNuvjCuCQE0ToZBBkX I=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="593807472" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:20:27 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com (Postfix) with ESMTPS id 527E148E12; Wed, 8 Nov 2023 11:20:23 +0000 (UTC) Received: from EX19MTAEUB002.ant.amazon.com [10.0.17.79:53919] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.33.209:2525] with esmtp (Farcaster) id 70e20ac2-f2b6-471e-8f0e-3ab2809c6acc; Wed, 8 Nov 2023 11:20:21 +0000 (UTC) X-Farcaster-Flow-ID: 70e20ac2-f2b6-471e-8f0e-3ab2809c6acc Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:21 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:16 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 10/33] KVM: x86: hyper-v: Introduce KVM_HV_GET_VSM_STATE Date: Wed, 8 Nov 2023 11:17:43 +0000 Message-ID: <20231108111806.92604-11-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D044UWB002.ant.amazon.com (10.13.139.188) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:21:01 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994626995090302 X-GMAIL-MSGID: 1781994626995090302 HVCALL_GET_VP_REGISTERS exposes the VTL call hypercall page entry offsets to the guest. This hypercall is implemented in user-space while the hypercall page patching happens in-kernel. So expose it as part of the partition wide VSM state. NOTE: Alternatively there is the option of sharing this information through a VTL KVM device attribute (the device is introduced in subsequent patches). Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/uapi/asm/kvm.h | 5 +++++ arch/x86/kvm/hyperv.c | 8 ++++++++ arch/x86/kvm/hyperv.h | 2 ++ arch/x86/kvm/x86.c | 18 ++++++++++++++++++ include/uapi/linux/kvm.h | 4 ++++ 5 files changed, 37 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index f73d137784d7..370483d5d5fd 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -570,4 +570,9 @@ struct kvm_apic_id_groups { __u8 n_bits; /* nr of bits used to represent group in the APIC ID */ }; +/* for KVM_HV_GET_VSM_STATE */ +struct kvm_hv_vsm_state { + __u64 vsm_code_page_offsets; +}; + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 2cf430f6ddd8..caaa859932c5 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2990,3 +2990,11 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, return 0; } + +int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *state) +{ + struct kvm_hv* hv = &kvm->arch.hyperv; + + state->vsm_code_page_offsets = hv->vsm_code_page_offsets.as_u64; + return 0; +} diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index 5433107e7cc8..b3d1113efe82 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -261,4 +261,6 @@ static inline bool kvm_hv_vsm_enabled(struct kvm *kvm) return kvm->arch.hyperv.hv_enable_vsm; } +int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *state); + #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b0512e433032..57f9c58e1e32 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7132,6 +7132,24 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = kvm_vm_ioctl_set_apic_id_groups(kvm, &groups); break; } + case KVM_HV_GET_VSM_STATE: { + struct kvm_hv_vsm_state vsm_state; + + r = -EINVAL; + if (!kvm_hv_vsm_enabled(kvm)) + goto out; + + r = kvm_vm_ioctl_get_hv_vsm_state(kvm, &vsm_state); + if (r) + goto out; + + r = -EFAULT; + if (copy_to_user(argp, &vsm_state, sizeof(vsm_state))) + goto out; + + r = 0; + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 168b6ac6ebe5..03f5c08fd7aa 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2316,4 +2316,8 @@ struct kvm_create_guest_memfd { #define KVM_GUEST_MEMFD_ALLOW_HUGEPAGE (1ULL << 0) #define KVM_SET_APIC_ID_GROUPS _IOW(KVMIO, 0xd7, struct kvm_apic_id_groups) + +/* Get/Set Hyper-V VSM state. Available with KVM_CAP_HYPERV_VSM */ +#define KVM_HV_GET_VSM_STATE _IOR(KVMIO, 0xd5, struct kvm_hv_vsm_state) + #endif /* __LINUX_KVM_H */