Skip to content

Commit a06c3fa

Browse files
committed
drivers/virt: pkvm: Add initial support for running as a protected guest
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent 0ba5b4b commit a06c3fa

File tree

8 files changed

+88
-0
lines changed

8 files changed

+88
-0
lines changed

Documentation/virt/kvm/arm/hypercalls.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,25 @@ Provides a discovery mechanism for other KVM/arm64 hypercalls.
4444
----------------------------------------
4545

4646
See ptp_kvm.rst
47+
48+
``ARM_SMCCC_KVM_FUNC_HYP_MEMINFO``
49+
----------------------------------
50+
51+
Query the memory protection parameters for a pKVM protected virtual machine.
52+
53+
+---------------------+-------------------------------------------------------------+
54+
| Presence: | Optional; pKVM protected guests only. |
55+
+---------------------+-------------------------------------------------------------+
56+
| Calling convention: | HVC64 |
57+
+---------------------+----------+--------------------------------------------------+
58+
| Function ID: | (uint32) | 0xC6000002 |
59+
+---------------------+----------+----+---------------------------------------------+
60+
| Arguments: | (uint64) | R1 | Reserved / Must be zero |
61+
| +----------+----+---------------------------------------------+
62+
| | (uint64) | R2 | Reserved / Must be zero |
63+
| +----------+----+---------------------------------------------+
64+
| | (uint64) | R3 | Reserved / Must be zero |
65+
+---------------------+----------+----+---------------------------------------------+
66+
| Return Values: | (int64) | R0 | ``INVALID_PARAMETER (-3)`` on error, else |
67+
| | | | memory protection granule in bytes |
68+
+---------------------+----------+----+---------------------------------------------+

arch/arm64/include/asm/hypervisor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
void kvm_init_hyp_services(void);
88
bool kvm_arm_hyp_service_available(u32 func_id);
99

10+
#ifdef CONFIG_ARM_PKVM_GUEST
11+
void pkvm_init_hyp_services(void);
12+
#else
13+
static inline void pkvm_init_hyp_services(void) { };
14+
#endif
15+
1016
static inline void kvm_arch_init_hyp_services(void)
1117
{
18+
pkvm_init_hyp_services();
1219
};
1320

1421
#endif

drivers/virt/coco/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ config TSM_REPORTS
99

1010
source "drivers/virt/coco/efi_secret/Kconfig"
1111

12+
source "drivers/virt/coco/pkvm-guest/Kconfig"
13+
1214
source "drivers/virt/coco/sev-guest/Kconfig"
1315

1416
source "drivers/virt/coco/tdx-guest/Kconfig"

drivers/virt/coco/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
#
55
obj-$(CONFIG_TSM_REPORTS) += tsm.o
66
obj-$(CONFIG_EFI_SECRET) += efi_secret/
7+
obj-$(CONFIG_ARM_PKVM_GUEST) += pkvm-guest/
78
obj-$(CONFIG_SEV_GUEST) += sev-guest/
89
obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-guest/

drivers/virt/coco/pkvm-guest/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
config ARM_PKVM_GUEST
2+
bool "Arm pKVM protected guest driver"
3+
depends on ARM64
4+
help
5+
Protected guests running under the pKVM hypervisor on arm64
6+
are isolated from the host and must issue hypercalls to enable
7+
interaction with virtual devices. This driver implements
8+
support for probing and issuing these hypercalls.
9+
10+
If unsure, say 'N'.

drivers/virt/coco/pkvm-guest/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
obj-$(CONFIG_ARM_PKVM_GUEST) += arm-pkvm-guest.o
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Support for the hypercall interface exposed to protected guests by
4+
* pKVM.
5+
*
6+
* Author: Will Deacon <[email protected]>
7+
* Copyright (C) 2024 Google LLC
8+
*/
9+
10+
#include <linux/arm-smccc.h>
11+
#include <linux/array_size.h>
12+
#include <linux/mm.h>
13+
14+
#include <asm/hypervisor.h>
15+
16+
static size_t pkvm_granule;
17+
18+
void pkvm_init_hyp_services(void)
19+
{
20+
int i;
21+
struct arm_smccc_res res;
22+
const u32 funcs[] = {
23+
ARM_SMCCC_KVM_FUNC_HYP_MEMINFO,
24+
};
25+
26+
for (i = 0; i < ARRAY_SIZE(funcs); ++i) {
27+
if (!kvm_arm_hyp_service_available(funcs[i]))
28+
return;
29+
}
30+
31+
arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID,
32+
0, 0, 0, &res);
33+
if (res.a0 > PAGE_SIZE) /* Includes error codes */
34+
return;
35+
36+
pkvm_granule = res.a0;
37+
}

include/linux/arm-smccc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
/* KVM "vendor specific" services */
116116
#define ARM_SMCCC_KVM_FUNC_FEATURES 0
117117
#define ARM_SMCCC_KVM_FUNC_PTP 1
118+
#define ARM_SMCCC_KVM_FUNC_HYP_MEMINFO 2
118119
#define ARM_SMCCC_KVM_FUNC_FEATURES_2 127
119120
#define ARM_SMCCC_KVM_NUM_FUNCS 128
120121

@@ -137,6 +138,12 @@
137138
ARM_SMCCC_OWNER_VENDOR_HYP, \
138139
ARM_SMCCC_KVM_FUNC_PTP)
139140

141+
#define ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID \
142+
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
143+
ARM_SMCCC_SMC_64, \
144+
ARM_SMCCC_OWNER_VENDOR_HYP, \
145+
ARM_SMCCC_KVM_FUNC_HYP_MEMINFO)
146+
140147
/* ptp_kvm counter type ID */
141148
#define KVM_PTP_VIRT_COUNTER 0
142149
#define KVM_PTP_PHYS_COUNTER 1

0 commit comments

Comments
 (0)