]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
drivers/virt: pkvm: Add initial support for running as a protected guest
authorWill Deacon <will@kernel.org>
Fri, 30 Aug 2024 13:01:45 +0000 (14:01 +0100)
committerWill Deacon <will@kernel.org>
Fri, 30 Aug 2024 15:30:41 +0000 (16:30 +0100)
Implement a pKVM protected guest driver to probe the presence of pKVM
and determine the memory protection granule using the HYP_MEMINFO
hypercall.

Acked-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20240830130150.8568-3-will@kernel.org
Signed-off-by: Will Deacon <will@kernel.org>
Documentation/virt/kvm/arm/hypercalls.rst
arch/arm64/include/asm/hypervisor.h
drivers/virt/coco/Kconfig
drivers/virt/coco/Makefile
drivers/virt/coco/pkvm-guest/Kconfig [new file with mode: 0644]
drivers/virt/coco/pkvm-guest/Makefile [new file with mode: 0644]
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c [new file with mode: 0644]
include/linux/arm-smccc.h

index 17be111f493f56ca2e0b0d8ee80a269dd863242c..16515eb421493380b633abf37584da8d097564d4 100644 (file)
@@ -44,3 +44,25 @@ Provides a discovery mechanism for other KVM/arm64 hypercalls.
 ----------------------------------------
 
 See ptp_kvm.rst
+
+``ARM_SMCCC_KVM_FUNC_HYP_MEMINFO``
+----------------------------------
+
+Query the memory protection parameters for a pKVM protected virtual machine.
+
++---------------------+-------------------------------------------------------------+
+| Presence:           | Optional; pKVM protected guests only.                       |
++---------------------+-------------------------------------------------------------+
+| Calling convention: | HVC64                                                       |
++---------------------+----------+--------------------------------------------------+
+| Function ID:        | (uint32) | 0xC6000002                                       |
++---------------------+----------+----+---------------------------------------------+
+| Arguments:          | (uint64) | R1 | Reserved / Must be zero                     |
+|                     +----------+----+---------------------------------------------+
+|                     | (uint64) | R2 | Reserved / Must be zero                     |
+|                     +----------+----+---------------------------------------------+
+|                     | (uint64) | R3 | Reserved / Must be zero                     |
++---------------------+----------+----+---------------------------------------------+
+| Return Values:      | (int64)  | R0 | ``INVALID_PARAMETER (-3)`` on error, else   |
+|                     |          |    | memory protection granule in bytes          |
++---------------------+----------+----+---------------------------------------------+
index 8cab2ab535b7e01a36a95ceaf47cbb495f62c154..409e239834d1947596fe0404ca04781b04c01142 100644 (file)
@@ -7,8 +7,15 @@
 void kvm_init_hyp_services(void);
 bool kvm_arm_hyp_service_available(u32 func_id);
 
+#ifdef CONFIG_ARM_PKVM_GUEST
+void pkvm_init_hyp_services(void);
+#else
+static inline void pkvm_init_hyp_services(void) { };
+#endif
+
 static inline void kvm_arch_init_hyp_services(void)
 {
+       pkvm_init_hyp_services();
 };
 
 #endif
index 87d142c1f9321164d6b0cd49317c71e86b1b29d9..d9ff676bf48db7b6e8554d72e4d0e3c7299ff8f8 100644 (file)
@@ -9,6 +9,8 @@ config TSM_REPORTS
 
 source "drivers/virt/coco/efi_secret/Kconfig"
 
+source "drivers/virt/coco/pkvm-guest/Kconfig"
+
 source "drivers/virt/coco/sev-guest/Kconfig"
 
 source "drivers/virt/coco/tdx-guest/Kconfig"
index 18c1aba5edb7911df8620959dd0e34b4e6f35b1f..b69c30c1c7203b7ac6538b6b90772aef3de803e3 100644 (file)
@@ -4,5 +4,6 @@
 #
 obj-$(CONFIG_TSM_REPORTS)      += tsm.o
 obj-$(CONFIG_EFI_SECRET)       += efi_secret/
+obj-$(CONFIG_ARM_PKVM_GUEST)   += pkvm-guest/
 obj-$(CONFIG_SEV_GUEST)                += sev-guest/
 obj-$(CONFIG_INTEL_TDX_GUEST)  += tdx-guest/
diff --git a/drivers/virt/coco/pkvm-guest/Kconfig b/drivers/virt/coco/pkvm-guest/Kconfig
new file mode 100644 (file)
index 0000000..d2f344f
--- /dev/null
@@ -0,0 +1,10 @@
+config ARM_PKVM_GUEST
+       bool "Arm pKVM protected guest driver"
+       depends on ARM64
+       help
+         Protected guests running under the pKVM hypervisor on arm64
+         are isolated from the host and must issue hypercalls to enable
+         interaction with virtual devices. This driver implements
+         support for probing and issuing these hypercalls.
+
+         If unsure, say 'N'.
diff --git a/drivers/virt/coco/pkvm-guest/Makefile b/drivers/virt/coco/pkvm-guest/Makefile
new file mode 100644 (file)
index 0000000..4bee245
--- /dev/null
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_ARM_PKVM_GUEST) += arm-pkvm-guest.o
diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
new file mode 100644 (file)
index 0000000..a514870
--- /dev/null
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Support for the hypercall interface exposed to protected guests by
+ * pKVM.
+ *
+ * Author: Will Deacon <will@kernel.org>
+ * Copyright (C) 2024 Google LLC
+ */
+
+#include <linux/arm-smccc.h>
+#include <linux/array_size.h>
+#include <linux/mm.h>
+
+#include <asm/hypervisor.h>
+
+static size_t pkvm_granule;
+
+void pkvm_init_hyp_services(void)
+{
+       int i;
+       struct arm_smccc_res res;
+       const u32 funcs[] = {
+               ARM_SMCCC_KVM_FUNC_HYP_MEMINFO,
+       };
+
+       for (i = 0; i < ARRAY_SIZE(funcs); ++i) {
+               if (!kvm_arm_hyp_service_available(funcs[i]))
+                       return;
+       }
+
+       arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID,
+                            0, 0, 0, &res);
+       if (res.a0 > PAGE_SIZE) /* Includes error codes */
+               return;
+
+       pkvm_granule = res.a0;
+}
index 083f8565371616269fa0050036ec8a3631c44357..16b6dcc54e028002a084158d43f4019bfc0c71db 100644 (file)
 /* KVM "vendor specific" services */
 #define ARM_SMCCC_KVM_FUNC_FEATURES            0
 #define ARM_SMCCC_KVM_FUNC_PTP                 1
+#define ARM_SMCCC_KVM_FUNC_HYP_MEMINFO         2
 #define ARM_SMCCC_KVM_FUNC_FEATURES_2          127
 #define ARM_SMCCC_KVM_NUM_FUNCS                        128
 
                           ARM_SMCCC_OWNER_VENDOR_HYP,                  \
                           ARM_SMCCC_KVM_FUNC_PTP)
 
+#define ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID                   \
+       ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,                         \
+                          ARM_SMCCC_SMC_64,                            \
+                          ARM_SMCCC_OWNER_VENDOR_HYP,                  \
+                          ARM_SMCCC_KVM_FUNC_HYP_MEMINFO)
+
 /* ptp_kvm counter type ID */
 #define KVM_PTP_VIRT_COUNTER                   0
 #define KVM_PTP_PHYS_COUNTER                   1