Skip to contentSkip to conversation

arm64: Support for Arm CCA in KVM

PATCH Superseded Steven Price 4 participants 124 messages Apr 16, 2025 – Jun 2, 2025 Page 1 of 2

Subsystems (5)
Documentation KVM arm64 KVM arm64 +1 more subsystems

v8 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v8 cover arm64: Support for Arm CCA in KVM

This series adds support for running protected VMs using KVM under the Arm Confidential Compute Architecture (CCA).

The related guest support was merged for v6.14-rc1 so you no longer need that separately.

There are a few changes since v7, many thanks for the review comments. The highlights are below, and individual patches have a changelog.

* More documentation - the new ioctls and capabilties are now all documented.

* Initial patch adding "only_private"/"only_shared" to struct kvm_gfn_range replaced with already upstream "attr_filter".

* Improvement in variable naming and error codes, and some improved/new comments. All following valuable review feedback (thanks!).

* Drop the final WIP patch for enabling large PAGE_SIZE support. It's not ready for merging and I want to focus on landing the 4k support.

* Rebased onto v6.15-rc1.

Things to note:

* The magic numbers for capabilities and ioctls have been updated. So you'll need to update your VMM. See below for update kvmtool branch.

* Patch 42 increases KVM_VCPU_MAX_FEATURES to expose the new feature. This also exposes the NV features (as they are currently numbered lower). This will resolve when Marc's NV series has landed, see [2].

* There are some conflicts with v6.15-rc2, mostly documentation, but also commit 26fbdf369227 ("KVM: arm64: Don't translate FAR if invalid/unsafe") 'hijacks' HPFAR_EL2_NS as a valid bit. This will require corresponding changes to the CCA code.

The ABI to the RMM (the RMI) is based on RMM v1.0-rel0 specification[1].

This series is based on v6.15-rc1. It is also available as a git repository:

https://gitlab.arm.com/linux-arm/linux-cca cca-host/v8

Work in progress changes for kvmtool are available from the git repository below:

https://gitlab.arm.com/linux-arm/kvmtool-cca cca/v6

[1] https://developer.arm.com/documentation/den0137/1-0rel0/
[2] https://lore.kernel.org/r/20250408105225.4002637-17-maz%40kernel.org

Jean-Philippe Brucker (7):
arm64: RME: Propagate number of breakpoints and watchpoints to
userspace
arm64: RME: Set breakpoint parameters through SET_ONE_REG
arm64: RME: Initialize PMCR.N with number counter supported by RMM
arm64: RME: Propagate max SVE vector length from RMM
arm64: RME: Configure max SVE vector length for a Realm
arm64: RME: Provide register list for unfinalized RME RECs
arm64: RME: Provide accurate register list

Joey Gouly (2):
arm64: RME: allow userspace to inject aborts
arm64: RME: support RSI_HOST_CALL

Steven Price (31):
arm64: RME: Handle Granule Protection Faults (GPFs)
arm64: RME: Add SMC definitions for calling the RMM
arm64: RME: Add wrappers for RMI calls
arm64: RME: Check for RME support at KVM init
arm64: RME: Define the user ABI
arm64: RME: ioctls to create and configure realms
KVM: arm64: Allow passing machine type in KVM creation
arm64: RME: RTT tear down
arm64: RME: Allocate/free RECs to match vCPUs
KVM: arm64: vgic: Provide helper for number of list registers
arm64: RME: Support for the VGIC in realms
KVM: arm64: Support timers in realm RECs
arm64: RME: Allow VMM to set RIPAS
arm64: RME: Handle realm enter/exit
arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE
KVM: arm64: Handle realm MMIO emulation
arm64: RME: Allow populating initial contents
arm64: RME: Runtime faulting of memory
KVM: arm64: Handle realm VCPU load
KVM: arm64: Validate register access for a Realm VM
KVM: arm64: Handle Realm PSCI requests
KVM: arm64: WARN on injected undef exceptions
arm64: Don't expose stolen time for realm guests
arm64: RME: Always use 4k pages for realms
arm64: RME: Prevent Device mappings for Realms
arm_pmu: Provide a mechanism for disabling the physical IRQ
arm64: RME: Enable PMU support with a realm guest
arm64: RME: Hide KVM_CAP_READONLY_MEM for realm guests
KVM: arm64: Expose support for private memory
KVM: arm64: Expose KVM_ARM_VCPU_REC to user space
KVM: arm64: Allow activating realms

Suzuki K Poulose (3):
kvm: arm64: Include kvm_emulate.h in kvm/arm_psci.h
kvm: arm64: Don't expose debug capabilities for realm guests
arm64: RME: Allow checking SVE on VM instance

 Documentation/virt/kvm/api.rst       |   91 +-
 arch/arm64/include/asm/kvm_emulate.h |   40 +
 arch/arm64/include/asm/kvm_host.h    |   17 +-
 arch/arm64/include/asm/kvm_rme.h     |  137 +++
 arch/arm64/include/asm/rmi_cmds.h    |  508 ++++++++
 arch/arm64/include/asm/rmi_smc.h     |  259 ++++
 arch/arm64/include/asm/virt.h        |    1 +
 arch/arm64/include/uapi/asm/kvm.h    |   49 +
 arch/arm64/kvm/Kconfig               |    1 +
 arch/arm64/kvm/Makefile              |    3 +-
 arch/arm64/kvm/arch_timer.c          |   48 +-
 arch/arm64/kvm/arm.c                 |  160 ++-
 arch/arm64/kvm/guest.c               |  104 +-
 arch/arm64/kvm/hypercalls.c          |    4 +-
 arch/arm64/kvm/inject_fault.c        |    5 +-
 arch/arm64/kvm/mmio.c                |   16 +-
 arch/arm64/kvm/mmu.c                 |  201 ++-
 arch/arm64/kvm/pmu-emul.c            |    6 +
 arch/arm64/kvm/psci.c                |   30 +
 arch/arm64/kvm/reset.c               |   23 +-
 arch/arm64/kvm/rme-exit.c            |  199 +++
 arch/arm64/kvm/rme.c                 | 1708 ++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c            |   49 +-
 arch/arm64/kvm/vgic/vgic-init.c      |    2 +-
 arch/arm64/kvm/vgic/vgic-v3.c        |    6 +-
 arch/arm64/kvm/vgic/vgic.c           |   54 +-
 arch/arm64/mm/fault.c                |   31 +-
 drivers/perf/arm_pmu.c               |   15 +
 include/kvm/arm_arch_timer.h         |    2 +
 include/kvm/arm_pmu.h                |    4 +
 include/kvm/arm_psci.h               |    2 +
 include/linux/perf/arm_pmu.h         |    5 +
 include/uapi/linux/kvm.h             |   29 +-
 33 files changed, 3709 insertions(+), 100 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_rme.h
 create mode 100644 arch/arm64/include/asm/rmi_cmds.h
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
 create mode 100644 arch/arm64/kvm/rme-exit.c
 create mode 100644 arch/arm64/kvm/rme.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v8 1/43 kvm: arm64: Include kvm_emulate.h in kvm/arm_psci.h

From: Suzuki K Poulose <[email protected]>

Fix a potential build error (like below, when asm/kvm_emulate.h gets included after the kvm/arm_psci.h) by including the missing header file in kvm/arm_psci.h:

./include/kvm/arm_psci.h: In function ‘kvm_psci_version’:
./include/kvm/arm_psci.h:29:13: error: implicit declaration of function
‘vcpu_has_feature’; did you mean ‘cpu_have_feature’? [-Werror=implicit-function-declaration]

   29 |         if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2)) {
	         |             ^~~~~~~~~~~~~~~~
			       |             cpu_have_feature
---
 include/kvm/arm_psci.h | 2 ++
 1 file changed, 2 insertions(+)
include/kvm/arm_psci.h +2 -0
index cbaec804eb83..38dab7add79b 100644
@@ -10,6 +10,8 @@
 #include <linux/kvm_host.h>
 #include <uapi/linux/psci.h>
 
+#include <asm/kvm_emulate.h>
+
 #define KVM_ARM_PSCI_0_1	PSCI_VERSION(0, 1)
 #define KVM_ARM_PSCI_0_2	PSCI_VERSION(0, 2)
 #define KVM_ARM_PSCI_1_0	PSCI_VERSION(1, 0)
Signature · 1 line
2.43.0
#2 lore raw

PATCH v8 2/43 arm64: RME: Handle Granule Protection Faults (GPFs)

In reply to Steven Price

If the host attempts to access granules that have been delegated for use in a realm these accesses will be caught and will trigger a Granule Protection Fault (GPF).

A fault during a page walk signals a bug in the kernel and is handled by oopsing the kernel. A non-page walk fault could be caused by user space having access to a page which has been delegated to the kernel and will trigger a SIGBUS to allow debugging why user space is trying to access a delegated page.

---
Changes since v2:
* Include missing "Granule Protection Fault at level -1"

---
 arch/arm64/mm/fault.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
arch/arm64/mm/fault.c +25 -6
index ec0a337891dd..59a89a8fb226 100644
@@ -844,6 +844,25 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,
 	return 0;
 }
 
+static int do_gpf_ptw(unsigned long far, unsigned long esr, struct pt_regs *regs)
+{
+	const struct fault_info *inf = esr_to_fault_info(esr);
+
+	die_kernel_fault(inf->name, far, esr, regs);
+	return 0;
+}
+
+static int do_gpf(unsigned long far, unsigned long esr, struct pt_regs *regs)
+{
+	const struct fault_info *inf = esr_to_fault_info(esr);
+
+	if (!is_el1_instruction_abort(esr) && fixup_exception(regs, esr))
+		return 0;
+
+	arm64_notify_die(inf->name, regs, inf->sig, inf->code, far, esr);
+	return 0;
+}
+
 static const struct fault_info fault_info[] = {
 	{ do_bad,		SIGKILL, SI_KERNEL,	"ttbr address size fault"	},
 	{ do_bad,		SIGKILL, SI_KERNEL,	"level 1 address size fault"	},
@@ -880,12 +899,12 @@ static const struct fault_info fault_info[] = {
 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 32"			},
 	{ do_alignment_fault,	SIGBUS,  BUS_ADRALN,	"alignment fault"		},
 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 34"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 35"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 36"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 37"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 38"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 39"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 40"			},
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level -1" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 0" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 1" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 2" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 3" },
+	{ do_gpf,		SIGBUS,  SI_KERNEL,	"Granule Protection Fault not on table walk" },
 	{ do_bad,		SIGKILL, SI_KERNEL,	"level -1 address size fault"	},
 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 42"			},
 	{ do_translation_fault,	SIGSEGV, SEGV_MAPERR,	"level -1 translation fault"	},
Signature · 1 line
2.43.0
#3 lore raw

PATCH v8 3/43 arm64: RME: Add SMC definitions for calling the RMM

In reply to Steven Price

The RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.

The SMC definitions are based on DEN0137[1] version 1.0-rel0

[1] https://developer.arm.com/documentation/den0137/1-0rel0/

---
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

---
 arch/arm64/include/asm/rmi_smc.h | 259 +++++++++++++++++++++++++++++++
 1 file changed, 259 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +259 -0
new file mode 100644
index 000000000000..7a93a3e0ac6e
@@ -0,0 +1,259 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2024 ARM Ltd.
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 1.0-rel0:
+ * https://developer.arm.com/documentation/den0137/1-0rel0/
+ */
+
+#ifndef __ASM_RMI_SMC_H
+#define __ASM_RMI_SMC_H
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RMI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD,	\
+			   (func))
+
+#define SMC_RMI_VERSION			SMC_RMI_CALL(0x0150)
+#define SMC_RMI_GRANULE_DELEGATE	SMC_RMI_CALL(0x0151)
+#define SMC_RMI_GRANULE_UNDELEGATE	SMC_RMI_CALL(0x0152)
+#define SMC_RMI_DATA_CREATE		SMC_RMI_CALL(0x0153)
+#define SMC_RMI_DATA_CREATE_UNKNOWN	SMC_RMI_CALL(0x0154)
+#define SMC_RMI_DATA_DESTROY		SMC_RMI_CALL(0x0155)
+
+#define SMC_RMI_REALM_ACTIVATE		SMC_RMI_CALL(0x0157)
+#define SMC_RMI_REALM_CREATE		SMC_RMI_CALL(0x0158)
+#define SMC_RMI_REALM_DESTROY		SMC_RMI_CALL(0x0159)
+#define SMC_RMI_REC_CREATE		SMC_RMI_CALL(0x015a)
+#define SMC_RMI_REC_DESTROY		SMC_RMI_CALL(0x015b)
+#define SMC_RMI_REC_ENTER		SMC_RMI_CALL(0x015c)
+#define SMC_RMI_RTT_CREATE		SMC_RMI_CALL(0x015d)
+#define SMC_RMI_RTT_DESTROY		SMC_RMI_CALL(0x015e)
+#define SMC_RMI_RTT_MAP_UNPROTECTED	SMC_RMI_CALL(0x015f)
+
+#define SMC_RMI_RTT_READ_ENTRY		SMC_RMI_CALL(0x0161)
+#define SMC_RMI_RTT_UNMAP_UNPROTECTED	SMC_RMI_CALL(0x0162)
+
+#define SMC_RMI_PSCI_COMPLETE		SMC_RMI_CALL(0x0164)
+#define SMC_RMI_FEATURES		SMC_RMI_CALL(0x0165)
+#define SMC_RMI_RTT_FOLD		SMC_RMI_CALL(0x0166)
+#define SMC_RMI_REC_AUX_COUNT		SMC_RMI_CALL(0x0167)
+#define SMC_RMI_RTT_INIT_RIPAS		SMC_RMI_CALL(0x0168)
+#define SMC_RMI_RTT_SET_RIPAS		SMC_RMI_CALL(0x0169)
+
+#define RMI_ABI_MAJOR_VERSION	1
+#define RMI_ABI_MINOR_VERSION	0
+
+#define RMI_ABI_VERSION_GET_MAJOR(version) ((version) >> 16)
+#define RMI_ABI_VERSION_GET_MINOR(version) ((version) & 0xFFFF)
+#define RMI_ABI_VERSION(major, minor)      (((major) << 16) | (minor))
+
+#define RMI_UNASSIGNED			0
+#define RMI_ASSIGNED			1
+#define RMI_TABLE			2
+
+#define RMI_RETURN_STATUS(ret)		((ret) & 0xFF)
+#define RMI_RETURN_INDEX(ret)		(((ret) >> 8) & 0xFF)
+
+#define RMI_SUCCESS		0
+#define RMI_ERROR_INPUT		1
+#define RMI_ERROR_REALM		2
+#define RMI_ERROR_REC		3
+#define RMI_ERROR_RTT		4
+
+enum rmi_ripas {
+	RMI_EMPTY = 0,
+	RMI_RAM = 1,
+	RMI_DESTROYED = 2,
+};
+
+#define RMI_NO_MEASURE_CONTENT	0
+#define RMI_MEASURE_CONTENT	1
+
+#define RMI_FEATURE_REGISTER_0_S2SZ		GENMASK(7, 0)
+#define RMI_FEATURE_REGISTER_0_LPA2		BIT(8)
+#define RMI_FEATURE_REGISTER_0_SVE_EN		BIT(9)
+#define RMI_FEATURE_REGISTER_0_SVE_VL		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_0_NUM_BPS		GENMASK(19, 14)
+#define RMI_FEATURE_REGISTER_0_NUM_WPS		GENMASK(25, 20)
+#define RMI_FEATURE_REGISTER_0_PMU_EN		BIT(26)
+#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS	GENMASK(31, 27)
+#define RMI_FEATURE_REGISTER_0_HASH_SHA_256	BIT(32)
+#define RMI_FEATURE_REGISTER_0_HASH_SHA_512	BIT(33)
+#define RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS	GENMASK(37, 34)
+#define RMI_FEATURE_REGISTER_0_MAX_RECS_ORDER	GENMASK(41, 38)
+#define RMI_FEATURE_REGISTER_0_Reserved		GENMASK(63, 42)
+
+#define RMI_REALM_PARAM_FLAG_LPA2		BIT(0)
+#define RMI_REALM_PARAM_FLAG_SVE		BIT(1)
+#define RMI_REALM_PARAM_FLAG_PMU		BIT(2)
+
+/*
+ * Note many of these fields are smaller than u64 but all fields have u64
+ * alignment, so use u64 to ensure correct alignment.
+ */
+struct realm_params {
+	union { /* 0x0 */
+		struct {
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+		};
+		u8 padding0[0x400];
+	};
+	union { /* 0x400 */
+		u8 rpv[64];
+		u8 padding1[0x400];
+	};
+	union { /* 0x800 */
+		struct {
+			u64 vmid;
+			u64 rtt_base;
+			s64 rtt_level_start;
+			u64 rtt_num_start;
+		};
+		u8 padding2[0x800];
+	};
+};
+
+/*
+ * The number of GPRs (starting from X0) that are
+ * configured by the host when a REC is created.
+ */
+#define REC_CREATE_NR_GPRS		8
+
+#define REC_PARAMS_FLAG_RUNNABLE	BIT_ULL(0)
+
+#define REC_PARAMS_AUX_GRANULES		16
+
+struct rec_params {
+	union { /* 0x0 */
+		u64 flags;
+		u8 padding0[0x100];
+	};
+	union { /* 0x100 */
+		u64 mpidr;
+		u8 padding1[0x100];
+	};
+	union { /* 0x200 */
+		u64 pc;
+		u8 padding2[0x100];
+	};
+	union { /* 0x300 */
+		u64 gprs[REC_CREATE_NR_GPRS];
+		u8 padding3[0x500];
+	};
+	union { /* 0x800 */
+		struct {
+			u64 num_rec_aux;
+			u64 aux[REC_PARAMS_AUX_GRANULES];
+		};
+		u8 padding4[0x800];
+	};
+};
+
+#define REC_ENTER_FLAG_EMULATED_MMIO	BIT(0)
+#define REC_ENTER_FLAG_INJECT_SEA	BIT(1)
+#define REC_ENTER_FLAG_TRAP_WFI		BIT(2)
+#define REC_ENTER_FLAG_TRAP_WFE		BIT(3)
+#define REC_ENTER_FLAG_RIPAS_RESPONSE	BIT(4)
+
+#define REC_RUN_GPRS			31
+#define REC_MAX_GIC_NUM_LRS		16
+
+struct rec_enter {
+	union { /* 0x000 */
+		u64 flags;
+		u8 padding0[0x200];
+	};
+	union { /* 0x200 */
+		u64 gprs[REC_RUN_GPRS];
+		u8 padding1[0x100];
+	};
+	union { /* 0x300 */
+		struct {
+			u64 gicv3_hcr;
+			u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS];
+		};
+		u8 padding2[0x100];
+	};
+	u8 padding3[0x400];
+};
+
+#define RMI_EXIT_SYNC			0x00
+#define RMI_EXIT_IRQ			0x01
+#define RMI_EXIT_FIQ			0x02
+#define RMI_EXIT_PSCI			0x03
+#define RMI_EXIT_RIPAS_CHANGE		0x04
+#define RMI_EXIT_HOST_CALL		0x05
+#define RMI_EXIT_SERROR			0x06
+
+struct rec_exit {
+	union { /* 0x000 */
+		u8 exit_reason;
+		u8 padding0[0x100];
+	};
+	union { /* 0x100 */
+		struct {
+			u64 esr;
+			u64 far;
+			u64 hpfar;
+		};
+		u8 padding1[0x100];
+	};
+	union { /* 0x200 */
+		u64 gprs[REC_RUN_GPRS];
+		u8 padding2[0x100];
+	};
+	union { /* 0x300 */
+		struct {
+			u64 gicv3_hcr;
+			u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS];
+			u64 gicv3_misr;
+			u64 gicv3_vmcr;
+		};
+		u8 padding3[0x100];
+	};
+	union { /* 0x400 */
+		struct {
+			u64 cntp_ctl;
+			u64 cntp_cval;
+			u64 cntv_ctl;
+			u64 cntv_cval;
+		};
+		u8 padding4[0x100];
+	};
+	union { /* 0x500 */
+		struct {
+			u64 ripas_base;
+			u64 ripas_top;
+			u64 ripas_value;
+		};
+		u8 padding5[0x100];
+	};
+	union { /* 0x600 */
+		u16 imm;
+		u8 padding6[0x100];
+	};
+	union { /* 0x700 */
+		struct {
+			u8 pmu_ovf_status;
+		};
+		u8 padding7[0x100];
+	};
+};
+
+struct rec_run {
+	struct rec_enter enter;
+	struct rec_exit exit;
+};
+
+#endif /* __ASM_RMI_SMC_H */
Signature · 1 line
2.43.0
reviewer Reviewed-by on v8 3/43
#4 lore raw

Re: [PATCH v8 03/43] arm64: RME: Add SMC definitions for calling the RMM

Quoted 9 lines · Steven Price

The RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.

The SMC definitions are based on DEN0137[1] version 1.0-rel0

[1] https://developer.arm.com/documentation/den0137/1-0rel0/

#5 lore raw

PATCH v8 4/43 arm64: RME: Add wrappers for RMI calls

In reply to Steven Price

The wrappers make the call sites easier to read and deal with the boiler plate of handling the error codes from the RMM.

---
Changes from v7:
* Minor renaming of parameters and updated comments
Changes from v5:
* Further improve comments
Changes from v4:
* Improve comments
Changes from v2:
* Make output arguments optional.
* Mask RIPAS value rmi_rtt_read_entry()
* Drop unused rmi_rtt_get_phys()

---
 arch/arm64/include/asm/rmi_cmds.h | 508 ++++++++++++++++++++++++++++++
 1 file changed, 508 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_cmds.h
arch/arm64/include/asm/rmi_cmds.h new file +508 -0
new file mode 100644
index 000000000000..27cd2751f3bf
@@ -0,0 +1,508 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#ifndef __ASM_RMI_CMDS_H
+#define __ASM_RMI_CMDS_H
+
+#include <linux/arm-smccc.h>
+
+#include <asm/rmi_smc.h>
+
+struct rtt_entry {
+	unsigned long walk_level;
+	unsigned long desc;
+	int state;
+	int ripas;
+};
+
+/**
+ * rmi_data_create() - Create a data granule
+ * @rd: PA of the RD
+ * @data: PA of the target granule
+ * @ipa: IPA at which the granule will be mapped in the guest
+ * @src: PA of the source granule
+ * @flags: RMI_MEASURE_CONTENT if the contents should be measured
+ *
+ * Create a new data granule, copying contents from a non-secure granule.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_data_create(unsigned long rd, unsigned long data,
+				  unsigned long ipa, unsigned long src,
+				  unsigned long flags)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_DATA_CREATE, rd, data, ipa, src,
+			     flags, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_data_create_unknown() - Create a data granule with unknown contents
+ * @rd: PA of the RD
+ * @data: PA of the target granule
+ * @ipa: IPA at which the granule will be mapped in the guest
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_data_create_unknown(unsigned long rd,
+					  unsigned long data,
+					  unsigned long ipa)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_DATA_CREATE_UNKNOWN, rd, data, ipa, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_data_destroy() - Destroy a data granule
+ * @rd: PA of the RD
+ * @ipa: IPA at which the granule is mapped in the guest
+ * @data_out: PA of the granule which was destroyed
+ * @top_out: Top IPA of non-live RTT entries
+ *
+ * Unmap a protected IPA from stage 2, transitioning it to DESTROYED.
+ * The IPA cannot be used by the guest unless it is transitioned to RAM again
+ * by the realm guest.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_data_destroy(unsigned long rd, unsigned long ipa,
+				   unsigned long *data_out,
+				   unsigned long *top_out)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_DATA_DESTROY, rd, ipa, &res);
+
+	if (data_out)
+		*data_out = res.a1;
+	if (top_out)
+		*top_out = res.a2;
+
+	return res.a0;
+}
+
+/**
+ * rmi_features() - Read feature register
+ * @index: Feature register index
+ * @out: Feature register value is written to this pointer
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_features(unsigned long index, unsigned long *out)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_FEATURES, index, &res);
+
+	if (out)
+		*out = res.a1;
+	return res.a0;
+}
+
+/**
+ * rmi_granule_delegate() - Delegate a granule
+ * @phys: PA of the granule
+ *
+ * Delegate a granule for use by the realm world.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_granule_delegate(unsigned long phys)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GRANULE_DELEGATE, phys, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_granule_undelegate() - Undelegate a granule
+ * @phys: PA of the granule
+ *
+ * Undelegate a granule to allow use by the normal world. Will fail if the
+ * granule is in use.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_granule_undelegate(unsigned long phys)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GRANULE_UNDELEGATE, phys, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_psci_complete() - Complete pending PSCI command
+ * @calling_rec: PA of the calling REC
+ * @target_rec: PA of the target REC
+ * @status: Status of the PSCI request
+ *
+ * Completes a pending PSCI command which was called with an MPIDR argument, by
+ * providing the corresponding REC.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_psci_complete(unsigned long calling_rec,
+				    unsigned long target_rec,
+				    unsigned long status)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_PSCI_COMPLETE, calling_rec, target_rec,
+			     status, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_realm_activate() - Active a realm
+ * @rd: PA of the RD
+ *
+ * Mark a realm as Active signalling that creation is complete and allowing
+ * execution of the realm.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_realm_activate(unsigned long rd)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REALM_ACTIVATE, rd, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_realm_create() - Create a realm
+ * @rd: PA of the RD
+ * @params: PA of realm parameters
+ *
+ * Create a new realm using the given parameters.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_realm_create(unsigned long rd, unsigned long params)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REALM_CREATE, rd, params, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_realm_destroy() - Destroy a realm
+ * @rd: PA of the RD
+ *
+ * Destroys a realm, all objects belonging to the realm must be destroyed first.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_realm_destroy(unsigned long rd)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REALM_DESTROY, rd, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rec_aux_count() - Get number of auxiliary granules required
+ * @rd: PA of the RD
+ * @aux_count: Number of granules written to this pointer
+ *
+ * A REC may require extra auxiliary granules to be delegated for the RMM to
+ * store metadata (not visible to the normal world) in. This function provides
+ * the number of granules that are required.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rec_aux_count(unsigned long rd, unsigned long *aux_count)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REC_AUX_COUNT, rd, &res);
+
+	if (aux_count)
+		*aux_count = res.a1;
+	return res.a0;
+}
+
+/**
+ * rmi_rec_create() - Create a REC
+ * @rd: PA of the RD
+ * @rec: PA of the target REC
+ * @params: PA of REC parameters
+ *
+ * Create a REC using the parameters specified in the struct rec_params pointed
+ * to by @params.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rec_create(unsigned long rd, unsigned long rec,
+				 unsigned long params)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REC_CREATE, rd, rec, params, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rec_destroy() - Destroy a REC
+ * @rec: PA of the target REC
+ *
+ * Destroys a REC. The REC must not be running.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rec_destroy(unsigned long rec)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REC_DESTROY, rec, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rec_enter() - Enter a REC
+ * @rec: PA of the target REC
+ * @run_ptr: PA of RecRun structure
+ *
+ * Starts (or continues) execution within a REC.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rec_enter(unsigned long rec, unsigned long run_ptr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REC_ENTER, rec, run_ptr, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_create() - Creates an RTT
+ * @rd: PA of the RD
+ * @rtt: PA of the target RTT
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ *
+ * Creates an RTT (Realm Translation Table) at the specified level for the
+ * translation of the specified address within the realm.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_create(unsigned long rd, unsigned long rtt,
+				 unsigned long ipa, long level)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_CREATE, rd, rtt, ipa, level, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_destroy() - Destroy an RTT
+ * @rd: PA of the RD
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ * @out_rtt: Pointer to write the PA of the RTT which was destroyed
+ * @out_top: Pointer to write the top IPA of non-live RTT entries
+ *
+ * Destroys an RTT. The RTT must be non-live, i.e. none of the entries in the
+ * table are in ASSIGNED or TABLE state.
+ *
+ * Return: RMI return code.
+ */
+static inline int rmi_rtt_destroy(unsigned long rd,
+				  unsigned long ipa,
+				  long level,
+				  unsigned long *out_rtt,
+				  unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_DESTROY, rd, ipa, level, &res);
+
+	if (out_rtt)
+		*out_rtt = res.a1;
+	if (out_top)
+		*out_top = res.a2;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_fold() - Fold an RTT
+ * @rd: PA of the RD
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ * @out_rtt: Pointer to write the PA of the RTT which was destroyed
+ *
+ * Folds an RTT. If all entries with the RTT are 'homogeneous' the RTT can be
+ * folded into the parent and the RTT destroyed.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_fold(unsigned long rd, unsigned long ipa,
+			       long level, unsigned long *out_rtt)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_FOLD, rd, ipa, level, &res);
+
+	if (out_rtt)
+		*out_rtt = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_init_ripas() - Set RIPAS for new realm
+ * @rd: PA of the RD
+ * @base: Base of target IPA region
+ * @top: Top of target IPA region
+ * @out_top: Top IPA of range whose RIPAS was modified
+ *
+ * Sets the RIPAS of a target IPA range to RAM, for a realm in the NEW state.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_init_ripas(unsigned long rd, unsigned long base,
+				     unsigned long top, unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_INIT_RIPAS, rd, base, top, &res);
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_map_unprotected() - Map NS granules into a realm
+ * @rd: PA of the RD
+ * @ipa: Base IPA of the mapping
+ * @level: Depth within the RTT tree
+ * @desc: RTTE descriptor
+ *
+ * Create a mapping from an Unprotected IPA to a Non-secure PA.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_map_unprotected(unsigned long rd,
+					  unsigned long ipa,
+					  long level,
+					  unsigned long desc)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_MAP_UNPROTECTED, rd, ipa, level,
+			     desc, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_read_entry() - Read an RTTE
+ * @rd: PA of the RD
+ * @ipa: IPA for which to read the RTTE
+ * @level: RTT level at which to read the RTTE
+ * @rtt: Output structure describing the RTTE
+ *
+ * Reads a RTTE (Realm Translation Table Entry).
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_read_entry(unsigned long rd, unsigned long ipa,
+				     long level, struct rtt_entry *rtt)
+{
+	struct arm_smccc_1_2_regs regs = {
+		SMC_RMI_RTT_READ_ENTRY,
+		rd, ipa, level
+	};
+
+	arm_smccc_1_2_smc(&regs, &regs);
+
+	rtt->walk_level = regs.a1;
+	rtt->state = regs.a2 & 0xFF;
+	rtt->desc = regs.a3;
+	rtt->ripas = regs.a4 & 0xFF;
+
+	return regs.a0;
+}
+
+/**
+ * rmi_rtt_set_ripas() - Set RIPAS for an running realm
+ * @rd: PA of the RD
+ * @rec: PA of the REC making the request
+ * @base: Base of target IPA region
+ * @top: Top of target IPA region
+ * @out_top: Pointer to write top IPA of range whose RIPAS was modified
+ *
+ * Completes a request made by the realm to change the RIPAS of a target IPA
+ * range.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_set_ripas(unsigned long rd, unsigned long rec,
+				    unsigned long base, unsigned long top,
+				    unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_SET_RIPAS, rd, rec, base, top, &res);
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_unmap_unprotected() - Remove a NS mapping
+ * @rd: PA of the RD
+ * @ipa: Base IPA of the mapping
+ * @level: Depth within the RTT tree
+ * @out_top: Pointer to write top IPA of non-live RTT entries
+ *
+ * Removes a mapping at an Unprotected IPA.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_unmap_unprotected(unsigned long rd,
+					    unsigned long ipa,
+					    long level,
+					    unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_UNMAP_UNPROTECTED, rd, ipa,
+			     level, &res);
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+#endif /* __ASM_RMI_CMDS_H */
Signature · 1 line
2.43.0
reviewer Reviewed-by on v8 4/43
#6 lore raw

Re: [PATCH v8 04/43] arm64: RME: Add wrappers for RMI calls

Hi Steven

Quoted 27 lines · Steven Price

The wrappers make the call sites easier to read and deal with the boiler plate of handling the error codes from the RMM.

---
Changes from v7:
* Minor renaming of parameters and updated comments
Changes from v5:
* Further improve comments
Changes from v4:
* Improve comments
Changes from v2:
* Make output arguments optional.
* Mask RIPAS value rmi_rtt_read_entry()
* Drop unused rmi_rtt_get_phys()

---
  arch/arm64/include/asm/rmi_cmds.h | 508 ++++++++++++++++++++++++++++++
  1 file changed, 508 insertions(+)

create mode 100644 arch/arm64/include/asm/rmi_cmds.h

arch/arm64/include/asm/rmi_cmds.h new file +0 -0
new file mode 100644
index 000000000000..27cd2751f3bf
@@ -0,0 +1,508 @@

...

arch/arm64/include/asm/rmi_cmds.h line 330

Quoted 11 lines
arch/arm64/include/asm/rmi_cmds.h new file +11 -0
+
+/**
+ * rmi_rtt_destroy() - Destroy an RTT
+ * @rd: PA of the RD
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ * @out_rtt: Pointer to write the PA of the RTT which was destroyed
+ * @out_top: Pointer to write the top IPA of non-live RTT entries
+ *
+ * Destroys an RTT. The RTT must be non-live, i.e. none of the entries in the
+ * table are in ASSIGNED or TABLE state.

minor nit: It may be worth emphasising that you mean HIPAS here and not RMI_ASSIGNED (as returned by RTT_READ_ENTRY). The key is, an unprotected live leaf mapping (i.e., HIPAS=ASSIGNED_NS) is considered non-live.

Either ways:

#7 lore raw

Re: [PATCH v8 04/43] arm64: RME: Add wrappers for RMI calls

Quoted 33 lines · Suzuki K Poulose

Hi Steven

Quoted 27 lines · Steven Price

The wrappers make the call sites easier to read and deal with the boiler plate of handling the error codes from the RMM.

---
Changes from v7:
  * Minor renaming of parameters and updated comments Changes from v5:
  * Further improve comments
Changes from v4:
  * Improve comments
Changes from v2:
  * Make output arguments optional.
  * Mask RIPAS value rmi_rtt_read_entry()
  * Drop unused rmi_rtt_get_phys()
---
  arch/arm64/include/asm/rmi_cmds.h | 508 ++++++++++++++++++++++++++++++

  1 file changed, 508 insertions(+)

  create mode 100644 arch/arm64/include/asm/rmi_cmds.h

arch/arm64/include/asm/rmi_cmds.h new file +0 -0
new file mode 100644
index 000000000000..27cd2751f3bf
@@ -0,0 +1,508 @@

...

Quoted 16 lines
Quoted 10 lines
arch/arm64/include/asm/rmi_cmds.h new file +10 -0
+
+/**
+ * rmi_rtt_destroy() - Destroy an RTT
+ * @rd: PA of the RD
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ * @out_rtt: Pointer to write the PA of the RTT which was destroyed
+ * @out_top: Pointer to write the top IPA of non-live RTT entries
+ *
+ * Destroys an RTT. The RTT must be non-live, i.e. none of the 
Quoted 2 lines

entries in the
+ * table are in ASSIGNED or TABLE state.

minor nit: It may be worth emphasising that you mean HIPAS here and not RMI_ASSIGNED (as returned by RTT_READ_ENTRY). The key is, an unprotected live leaf mapping  (i.e., HIPAS=ASSIGNED_NS) is considered non-live.

Ah, I went back to the spec and TABLE is not a HIPAS ( :facepalm ). They are indeed RmmRttEntryState type. So, the comment is fine as it is. Please ignore ^

Quoted 6 lines

Either ways:

Suzuki

#8 lore raw

PATCH v8 5/43 arm64: RME: Check for RME support at KVM init

In reply to Steven Price

Query the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.

Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.

Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.

---
Changes since v6:
* Improved message for an unsupported RMI ABI version.
Changes since v5:
* Reword "unsupported" message from "host supports" to "we want" to clarify that 'we' are the 'host'.
Changes since v2:
* Drop return value from kvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.

---
 arch/arm64/include/asm/kvm_emulate.h | 18 +++++++++
 arch/arm64/include/asm/kvm_host.h    |  4 ++
 arch/arm64/include/asm/kvm_rme.h     | 56 ++++++++++++++++++++++++++++
 arch/arm64/include/asm/virt.h        |  1 +
 arch/arm64/kvm/Makefile              |  3 +-
 arch/arm64/kvm/arm.c                 |  6 +++
 arch/arm64/kvm/rme.c                 | 56 ++++++++++++++++++++++++++++
 7 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/include/asm/kvm_rme.h
 create mode 100644 arch/arm64/kvm/rme.c
arch/arm64/include/asm/kvm_emulate.h +18 -0
index d7cf66573aca..1c43a4fc25dd 100644
@@ -686,4 +686,22 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
 			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnFPM;
 	}
 }
+
+static inline bool kvm_is_realm(struct kvm *kvm)
+{
+	if (static_branch_unlikely(&kvm_rme_is_available) && kvm)
+		return kvm->arch.is_realm;
+	return false;
+}
+
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+	return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +4 -0
index e98cfe7855a6..7bd81b86eab0 100644
@@ -27,6 +27,7 @@
 #include <asm/fpsimd.h>
 #include <asm/kvm.h>
 #include <asm/kvm_asm.h>
+#include <asm/kvm_rme.h>
 #include <asm/vncr_mapping.h>
 
 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -394,6 +395,9 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	bool is_realm;
+	struct realm realm;
 };
 
 struct kvm_vcpu_fault_info {
arch/arm64/include/asm/kvm_rme.h new file +56 -0

56 lines not on this page · raw message

arch/arm64/include/asm/virt.h +1 -0
index ebf4a9f943ed..e45d47156dcf 100644
@@ -81,6 +81,7 @@ void __hyp_reset_vectors(void);
 bool is_kvm_arm_initialised(void);
 
 DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
+DECLARE_STATIC_KEY_FALSE(kvm_rme_is_available);
 
 static inline bool is_pkvm_initialized(void)
 {
arch/arm64/kvm/Makefile +2 -1
index 209bc76263f1..2ebc66812d49 100644
@@ -23,7 +23,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 vgic/vgic-v3.o vgic/vgic-v4.o \
 	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
 	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
-	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o
+	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
+	 rme.o
 
 kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
 kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/arm.c +6 -0
index 68fec8c95fee..856a721d41ac 100644
@@ -40,6 +40,7 @@
 #include <asm/kvm_nested.h>
 #include <asm/kvm_pkvm.h>
 #include <asm/kvm_ptrauth.h>
+#include <asm/kvm_rme.h>
 #include <asm/sections.h>
 
 #include <kvm/arm_hypercalls.h>
@@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {
 static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
 static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
 
+DEFINE_STATIC_KEY_FALSE(kvm_rme_is_available);
+
 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
@@ -2819,6 +2822,9 @@ static __init int kvm_arm_init(void)
 
 	in_hyp_mode = is_kernel_in_hyp_mode();
 
+	if (in_hyp_mode)
+		kvm_init_rme();
+
 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
arch/arm64/kvm/rme.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer Reviewed-by on v8 5/43
#9 lore raw

Re: [PATCH v8 05/43] arm64: RME: Check for RME support at KVM init

Quoted 192 lines · Steven Price

Query the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.

Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.

Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.

---
Changes since v6:
* Improved message for an unsupported RMI ABI version.
Changes since v5:
* Reword "unsupported" message from "host supports" to "we want" to clarify that 'we' are the 'host'.
Changes since v2:
* Drop return value from kvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.

---
  arch/arm64/include/asm/kvm_emulate.h | 18 +++++++++
  arch/arm64/include/asm/kvm_host.h    |  4 ++
  arch/arm64/include/asm/kvm_rme.h     | 56 ++++++++++++++++++++++++++++
  arch/arm64/include/asm/virt.h        |  1 +
  arch/arm64/kvm/Makefile              |  3 +-
  arch/arm64/kvm/arm.c                 |  6 +++
  arch/arm64/kvm/rme.c                 | 56 ++++++++++++++++++++++++++++
  7 files changed, 143 insertions(+), 1 deletion(-)

create mode 100644 arch/arm64/include/asm/kvm_rme.h create mode 100644 arch/arm64/kvm/rme.c

arch/arm64/include/asm/kvm_emulate.h +18 -0
index d7cf66573aca..1c43a4fc25dd 100644
@@ -686,4 +686,22 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
  			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnFPM;
  	}
  }
+
+static inline bool kvm_is_realm(struct kvm *kvm)
+{
+	if (static_branch_unlikely(&kvm_rme_is_available) && kvm)
+		return kvm->arch.is_realm;
+	return false;
+}
+
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+	return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
  #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +4 -0
index e98cfe7855a6..7bd81b86eab0 100644
@@ -27,6 +27,7 @@
  #include <asm/fpsimd.h>
  #include <asm/kvm.h>
  #include <asm/kvm_asm.h>
+#include <asm/kvm_rme.h>
  #include <asm/vncr_mapping.h>
 
  #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -394,6 +395,9 @@ struct kvm_arch {
  	 * the associated pKVM instance in the hypervisor.
  	 */
  	struct kvm_protected_vm pkvm;
+
+	bool is_realm;
+	struct realm realm;
  };
 
  struct kvm_vcpu_fault_info {
arch/arm64/include/asm/kvm_rme.h new file +56 -0
new file mode 100644
index 000000000000..9c8a0b23e0e4
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#ifndef __ASM_KVM_RME_H
+#define __ASM_KVM_RME_H
+
+/**
+ * enum realm_state - State of a Realm
+ */
+enum realm_state {
+	/**
+	 * @REALM_STATE_NONE:
+	 *      Realm has not yet been created. rmi_realm_create() may be
+	 *      called to create the realm.
+	 */
+	REALM_STATE_NONE,
+	/**
+	 * @REALM_STATE_NEW:
+	 *      Realm is under construction, not eligible for execution. Pages
+	 *      may be populated with rmi_data_create().
+	 */
+	REALM_STATE_NEW,
+	/**
+	 * @REALM_STATE_ACTIVE:
+	 *      Realm has been created and is eligible for execution with
+	 *      rmi_rec_enter(). Pages may no longer be populated with
+	 *      rmi_data_create().
+	 */
+	REALM_STATE_ACTIVE,
+	/**
+	 * @REALM_STATE_DYING:
+	 *      Realm is in the process of being destroyed or has already been
+	 *      destroyed.
+	 */
+	REALM_STATE_DYING,
+	/**
+	 * @REALM_STATE_DEAD:
+	 *      Realm has been destroyed.
+	 */
+	REALM_STATE_DEAD
+};
+
+/**
+ * struct realm - Additional per VM data for a Realm
+ *
+ * @state: The lifetime state machine for the realm
+ */
+struct realm {
+	enum realm_state state;
+};
+
+void kvm_init_rme(void);
+
+#endif /* __ASM_KVM_RME_H */
arch/arm64/include/asm/virt.h +1 -0
index ebf4a9f943ed..e45d47156dcf 100644
@@ -81,6 +81,7 @@ void __hyp_reset_vectors(void);
  bool is_kvm_arm_initialised(void);
 
  DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
+DECLARE_STATIC_KEY_FALSE(kvm_rme_is_available);
 
  static inline bool is_pkvm_initialized(void)
  {
arch/arm64/kvm/Makefile +2 -1
index 209bc76263f1..2ebc66812d49 100644
@@ -23,7 +23,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
  	 vgic/vgic-v3.o vgic/vgic-v4.o \
  	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
  	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
-	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o
+	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
+	 rme.o
 
  kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
  kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/arm.c +3 -0
index 68fec8c95fee..856a721d41ac 100644
@@ -40,6 +40,7 @@
  #include <asm/kvm_nested.h>
  #include <asm/kvm_pkvm.h>
  #include <asm/kvm_ptrauth.h>
+#include <asm/kvm_rme.h>
  #include <asm/sections.h>
 
  #include <kvm/arm_hypercalls.h>
@@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {
  static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
  static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
 
+DEFINE_STATIC_KEY_FALSE(kvm_rme_is_available);
+
  DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
  DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);

arch/arm64/kvm/arm.c static __init int kvm_arm_init(void) · line 2827

Quoted 6 lines
arch/arm64/kvm/arm.c +3 -0
@@ -2819,6 +2822,9 @@ static __init int kvm_arm_init(void)
 
  	in_hyp_mode = is_kernel_in_hyp_mode();
 
+	if (in_hyp_mode)
+		kvm_init_rme();
+

minor nit:

I wondering if this check is necessary. If the host is running under a a hypervisor, it could relay the calls to the RMM. Nothing urgent, but it is a possibility. It doesn't matter to the host as such. The Realm Guest will do its own verification and the host can ignore what lies beneath ?

Either ways,

#10 lore raw

Re: [PATCH v8 05/43] arm64: RME: Check for RME support at KVM init

Quoted 202 lines · Suzuki K Poulose
Quoted 194 lines · Steven Price

Query the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.

Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.

Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.

---
Changes since v6:
  * Improved message for an unsupported RMI ABI version. Changes since v5:
  * Reword "unsupported" message from "host supports" to "we want" to clarify that 'we' are the 'host'.
Changes since v2:
  * Drop return value from kvm_init_rme(), it was always 0.
  * Rely on the RMM return value to identify whether the RSI ABI is compatible.
---
  arch/arm64/include/asm/kvm_emulate.h | 18 +++++++++ arch/arm64/include/asm/kvm_host.h    |  4 ++ arch/arm64/include/asm/kvm_rme.h     | 56 ++++++++++++++++++++++++++++ arch/arm64/include/asm/virt.h        |  1 + arch/arm64/kvm/Makefile              |  3 +- arch/arm64/kvm/arm.c                 |  6 +++ arch/arm64/kvm/rme.c                 | 56 ++++++++++++++++++++++++++++

  7 files changed, 143 insertions(+), 1 deletion(-)

  create mode 100644 arch/arm64/include/asm/kvm_rme.h create mode 100644 arch/arm64/kvm/rme.c

arch/arm64/ renamed from arch/arm64/include/asm/kvm_emulate.h +0 -0

include/asm/kvm_emulate.h
index d7cf66573aca..1c43a4fc25dd 100644

arch/arm64/include/asm/kvm_emulate.h +18 -0
@@ -686,4 +686,22 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu
 *vcpu)
               vcpu->arch.hcrx_el2 |= HCRX_EL2_EnFPM;
       }
   }
+
+static inline bool kvm_is_realm(struct kvm *kvm)
+{
+    if (static_branch_unlikely(&kvm_rme_is_available) && kvm)
+        return kvm->arch.is_realm;
+    return false;
+}
+
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+    return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
+{
+    return false;
+}
+

  #endif /* __ARM64_KVM_EMULATE_H__ */

arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_host.h +0 -0

asm/kvm_host.h
index e98cfe7855a6..7bd81b86eab0 100644

arch/arm64/include/asm/kvm_host.h +4 -0
@@ -27,6 +27,7 @@
   #include <asm/fpsimd.h>
   #include <asm/kvm.h>
   #include <asm/kvm_asm.h>
+#include <asm/kvm_rme.h>
   #include <asm/vncr_mapping.h>
     #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -394,6 +395,9 @@ struct kvm_arch {
        * the associated pKVM instance in the hypervisor.
        */
       struct kvm_protected_vm pkvm;
+
+    bool is_realm;
+    struct realm realm;
   };
     struct kvm_vcpu_fault_info {
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
new file mode 100644
index 000000000000..9c8a0b23e0e4

arch/arm64/include/asm/kvm_rme.h new file +21 -0
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#ifndef __ASM_KVM_RME_H
+#define __ASM_KVM_RME_H
+
+/**
+ * enum realm_state - State of a Realm
+ */
+enum realm_state {
+    /**
+     * @REALM_STATE_NONE:
+     *      Realm has not yet been created. rmi_realm_create() may be
+     *      called to create the realm.
+     */
+    REALM_STATE_NONE,
+    /**
+     * @REALM_STATE_NEW:
+     *      Realm is under construction, not eligible for execution.

Pages
+     *      may be populated with rmi_data_create().
+     */
+    REALM_STATE_NEW,
+    /**
+     * @REALM_STATE_ACTIVE:
+     *      Realm has been created and is eligible for execution with +     *      rmi_rec_enter(). Pages may no longer be populated with +     *      rmi_data_create().
+     */
+    REALM_STATE_ACTIVE,
+    /**
+     * @REALM_STATE_DYING:
+     *      Realm is in the process of being destroyed or has already been
+     *      destroyed.
+     */
+    REALM_STATE_DYING,
+    /**
+     * @REALM_STATE_DEAD:
+     *      Realm has been destroyed.
+     */
+    REALM_STATE_DEAD

arch/arm64/include/asm/kvm_rme.h new file +14 -0
+};
+
+/**
+ * struct realm - Additional per VM data for a Realm
+ *
+ * @state: The lifetime state machine for the realm
+ */
+struct realm {
+    enum realm_state state;
+};
+
+void kvm_init_rme(void);
+
+#endif /* __ASM_KVM_RME_H */
arch/arm64/include/asm/ renamed from arch/arm64/include/asm/virt.h +0 -0

virt.h
index ebf4a9f943ed..e45d47156dcf 100644

arch/arm64/include/asm/virt.h +1 -0
@@ -81,6 +81,7 @@ void __hyp_reset_vectors(void);
   bool is_kvm_arm_initialised(void);
     DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
+DECLARE_STATIC_KEY_FALSE(kvm_rme_is_available);
     static inline bool is_pkvm_initialized(void)
   {
arch/arm64/kvm/Makefile +2 -1
index 209bc76263f1..2ebc66812d49 100644
@@ -23,7 +23,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o
 pvtime.o \
        vgic/vgic-v3.o vgic/vgic-v4.o \
        vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
        vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
-     vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o
+     vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
+     rme.o

    kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o

arch/arm64/kvm/arm.c +1 -0
index 68fec8c95fee..856a721d41ac 100644
@@ -40,6 +40,7 @@
   #include <asm/kvm_nested.h>
   #include <asm/kvm_pkvm.h>
   #include <asm/kvm_ptrauth.h>
+#include <asm/kvm_rme.h>
   #include <asm/sections.h>
     #include <kvm/arm_hypercalls.h>
@@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {

  static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
  static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; +DEFINE_STATIC_KEY_FALSE(kvm_rme_is_available);
+
  DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);

arch/arm64/kvm/arm.c static __init int kvm_arm_init(void) · line 2825

Quoted 4 lines
arch/arm64/kvm/arm.c +2 -0
@@ -2819,6 +2822,9 @@ static __init int kvm_arm_init(void)
         in_hyp_mode = is_kernel_in_hyp_mode();
   +    if (in_hyp_mode)
+        kvm_init_rme();
+

minor nit:

Quoted 5 lines

I wondering if this check is necessary. If the host is running under a a hypervisor, it could relay the calls to the RMM. Nothing urgent, but it is a possibility. It doesn't matter to the host as such. The Realm Guest will do its own verification and the host can ignore what lies beneath ?

Reasonable point - I don't think we have anything yet that can do that sort of relaying. But I guess anything which handles the RMI API we should be compatible with, so there's no need specifically to require in_hyp_mode.

Thanks,
Steve

Quoted 6 lines

Either ways,

#11 lore raw

PATCH v8 6/43 arm64: RME: Define the user ABI

In reply to Steven Price

There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.

---
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

---
 Documentation/virt/kvm/api.rst    | 70 +++++++++++++++++++++++++++++++
 arch/arm64/include/uapi/asm/kvm.h | 49 ++++++++++++++++++++++
 include/uapi/linux/kvm.h          | 10 +++++
 3 files changed, 129 insertions(+)
Documentation/virt/kvm/api.rst +70 -0
index 1f8625b7646a..99ba6c82cf37 100644
@@ -3527,6 +3527,11 @@ Possible features:
 	      - the KVM_REG_ARM64_SVE_VLS pseudo-register is immutable, and can
 	        no longer be written using KVM_SET_ONE_REG.
 
+	- KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this
+	  VCPU. This must be specified on all VCPUs created in a Realm VM.
+	  Depends on KVM_CAP_ARM_RME.
+	  Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC).
+
 4.83 KVM_ARM_PREFERRED_TARGET
 -----------------------------
 
@@ -5098,6 +5103,7 @@ Recognised values for feature:
 
   =====      ===========================================
   arm64      KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE)
+  arm64      KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME)
   =====      ===========================================
 
 Finalizes the configuration of the specified vcpu feature.
@@ -6452,6 +6458,30 @@ the capability to be present.
 
 `flags` must currently be zero.
 
+4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE
+------------------------------------
+
+:Capability: KVM_CAP_ARM_RME
+:Architectures: arm64
+:Type: vcpu ioctl
+:Parameters: struct kvm_arm_rmm_psci_complete (in)
+:Returns: 0 if successful, < 0 on error
+
+::
+
+  struct kvm_arm_rmm_psci_complete {
+	__u64 target_mpidr;
+	__u32 psci_status;
+	__u32 padding[3];
+  };
+
+Where PSCI functions are handled by user space, the RMM needs to be informed of
+the target of the operation using `target_mpidr`, along with the status
+(`psci_status`). The RMM v1.0 specification defines two functions that require
+this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO.
+
+If the kernel is handling PSCI then this is done automatically and the VMM
+doesn't need to call this ioctl.
 
 .. _kvm_run:
 
@@ -8280,6 +8310,46 @@ aforementioned registers before the first KVM_RUN. These registers are VM
 scoped, meaning that the same set of values are presented on all vCPUs in a
 given VM.
 
+7.38 KVM_CAP_ARM_RME
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: args[0] provides an action, args[1] points to a structure in
+	     memory for some actions.
+:Returns: 0 on success, negative value on error
+
+Used to configure and set up the memory for a Realm. The available actions are:
+
+================================= =============================================
+ KVM_CAP_ARM_RME_CONFIG_REALM     Takes struct arm_rme_config as args[1] and
+                                  configures realm parameters prior to it being
+                                  created.
+
+                                  Options are ARM_RME_CONFIG_RPV to set the
+                                  "Realm Personalization Value" and
+                                  ARM_RME_CONFIG_HASH_ALGO to set the hash
+                                  algorithm.
+
+ KVM_CAP_ARM_RME_CREATE_REALM     Request the RMM create the realm. The realm's
+                                  configuration parameters must be set first.
+
+ KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1]
+                                  and sets the RIPAS (Realm IPA State) to
+                                  RIPAS_RAM of a specified area of the realm's
+                                  IPA.
+
+ KVM_CAP_ARM_RME_POPULATE_REALM   Takes struct arm_rme_init_ripas as args[1]
+                                  and populates a region of protected address
+                                  space by copying the data from the shared
+                                  alias.
+
+ KVM_CAP_ARM_RME_ACTIVATE_REALM   Request the RMM activate the realm. No
+                                  further changes can be made to the realm's
+                                  configuration, and VCPUs are not permitted to
+                                  enter the realm until it has been activated.
+================================= =============================================
+
 8. Other capabilities.
 ======================
 
arch/arm64/include/uapi/asm/kvm.h +49 -0

61 lines not on this page · raw message

include/uapi/linux/kvm.h +10 -0
index b6ae8ad8934b..0b8479985581 100644
@@ -930,6 +930,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
 #define KVM_CAP_X86_GUEST_MODE 238
 #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239
+#define KVM_CAP_ARM_RME 240
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1582,4 +1583,13 @@ struct kvm_pre_fault_memory {
 	__u64 padding[5];
 };
 
+/* Available with KVM_CAP_ARM_RME, only for VMs with KVM_VM_TYPE_ARM_REALM  */
+struct kvm_arm_rmm_psci_complete {
+	__u64 target_mpidr;
+	__u32 psci_status;
+	__u32 padding[3];
+};
+
+#define KVM_ARM_VCPU_RMM_PSCI_COMPLETE	_IOW(KVMIO, 0xd6, struct kvm_arm_rmm_psci_complete)
+
 #endif /* __LINUX_KVM_H */
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH v8 06/43] arm64: RME: Define the user ABI

Hi Steven

Quoted 93 lines · Steven Price

There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.

---
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

---
  Documentation/virt/kvm/api.rst    | 70 +++++++++++++++++++++++++++++++
  arch/arm64/include/uapi/asm/kvm.h | 49 ++++++++++++++++++++++
  include/uapi/linux/kvm.h          | 10 +++++
  3 files changed, 129 insertions(+)
Documentation/virt/kvm/api.rst +42 -0
index 1f8625b7646a..99ba6c82cf37 100644
@@ -3527,6 +3527,11 @@ Possible features:
  	      - the KVM_REG_ARM64_SVE_VLS pseudo-register is immutable, and can
  	        no longer be written using KVM_SET_ONE_REG.
 
+	- KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this
+	  VCPU. This must be specified on all VCPUs created in a Realm VM.
+	  Depends on KVM_CAP_ARM_RME.
+	  Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC).
+
  4.83 KVM_ARM_PREFERRED_TARGET
  -----------------------------
 
@@ -5098,6 +5103,7 @@ Recognised values for feature:
 
    =====      ===========================================
    arm64      KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE)
+  arm64      KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME)
    =====      ===========================================
 
  Finalizes the configuration of the specified vcpu feature.
@@ -6452,6 +6458,30 @@ the capability to be present.
 
  `flags` must currently be zero.
 
+4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE
+------------------------------------
+
+:Capability: KVM_CAP_ARM_RME
+:Architectures: arm64
+:Type: vcpu ioctl
+:Parameters: struct kvm_arm_rmm_psci_complete (in)
+:Returns: 0 if successful, < 0 on error
+
+::
+
+  struct kvm_arm_rmm_psci_complete {
+	__u64 target_mpidr;
+	__u32 psci_status;
+	__u32 padding[3];
+  };
+
+Where PSCI functions are handled by user space, the RMM needs to be informed of
+the target of the operation using `target_mpidr`, along with the status
+(`psci_status`). The RMM v1.0 specification defines two functions that require
+this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO.
+
+If the kernel is handling PSCI then this is done automatically and the VMM
+doesn't need to call this ioctl.
 
  .. _kvm_run:
 
@@ -8280,6 +8310,46 @@ aforementioned registers before the first KVM_RUN. These registers are VM
  scoped, meaning that the same set of values are presented on all vCPUs in a
  given VM.
 
+7.38 KVM_CAP_ARM_RME
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: args[0] provides an action, args[1] points to a structure in
+	     memory for some actions.
+:Returns: 0 on success, negative value on error
+
+Used to configure and set up the memory for a Realm. The available actions are:
+
+================================= =============================================

Documentation/virt/kvm/api.rst aforementioned registers before the first KVM_RUN. These registers are VM · line 8334

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+ KVM_CAP_ARM_RME_CONFIG_REALM     Takes struct arm_rme_config as args[1] and
+                                  configures realm parameters prior to it being
+                                  created.
+
+                                  Options are ARM_RME_CONFIG_RPV to set the
+                                  "Realm Personalization Value" and
+                                  ARM_RME_CONFIG_HASH_ALGO to set the hash
+                                  algorithm.
+
+ KVM_CAP_ARM_RME_CREATE_REALM     Request the RMM create the realm. The realm's

minor nit: s/RMM create/RMM to create/

Or may be rephrase it:

Request the RMM to create the Realm with the configured parameters.

Documentation/virt/kvm/api.rst aforementioned registers before the first KVM_RUN. These registers are VM · line 8342

Quoted 8 lines
Documentation/virt/kvm/api.rst +8 -0
+                                  configuration parameters must be set first.
+
+ KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1]
+                                  and sets the RIPAS (Realm IPA State) to
+                                  RIPAS_RAM of a specified area of the realm's
+                                  IPA.
+
+ KVM_CAP_ARM_RME_POPULATE_REALM   Takes struct arm_rme_init_ripas as args[1]

nit: struct arm_rme_populate_realm

Documentation/virt/kvm/api.rst aforementioned registers before the first KVM_RUN. These registers are VM · line 8347

Quoted 5 lines
Documentation/virt/kvm/api.rst +5 -0
+                                  and populates a region of protected address
+                                  space by copying the data from the shared
+                                  alias.
+
+ KVM_CAP_ARM_RME_ACTIVATE_REALM   Request the RMM activate the realm. No

s/the RMM/the RMM to/

Documentation/virt/kvm/api.rst aforementioned registers before the first KVM_RUN. These registers are VM · line 8350

Quoted 3 lines
Documentation/virt/kvm/api.rst +3 -0
+                                  further changes can be made to the realm's
+                                  configuration, and VCPUs are not permitted to
+                                  enter the realm until it has been activated.

minor nit: this sounds as if "configurations" (of parameters) are possible after the CREATE_REALM and before ACTIVATE_REALM ? Could we also make it clear that the VCPUs must have been created before ACTIVATE ?

May be rephrase it to:

No changes can be made to Realm's memory (including the IPA state). No new VCPUs can be created after this step.

Quoted 24 lines
Documentation/virt/kvm/api.rst +2 -0
+================================= =============================================
+
  8. Other capabilities.
  ======================
 
arch/arm64/include/uapi/asm/kvm.h +4 -0
index af9d9acaf997..b57712880605 100644
@@ -106,6 +106,7 @@ struct kvm_regs {
  #define KVM_ARM_VCPU_PTRAUTH_GENERIC	6 /* VCPU uses generic authentication */
  #define KVM_ARM_VCPU_HAS_EL2		7 /* Support nested virtualization */
  #define KVM_ARM_VCPU_HAS_EL2_E2H0	8 /* Limit NV support to E2H RES0 */
+#define KVM_ARM_VCPU_REC		9 /* VCPU REC state as part of Realm */
 
  struct kvm_vcpu_init {
  	__u32 target;
@@ -429,6 +430,54 @@ enum {
  #define   KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES	3
  #define   KVM_DEV_ARM_ITS_CTRL_RESET		4
 
+/* KVM_CAP_ARM_RME on VM fd */
+#define KVM_CAP_ARM_RME_CONFIG_REALM		0
+#define KVM_CAP_ARM_RME_CREATE_REALM		1

arch/arm64/include/uapi/asm/kvm.h enum { · line 445

Quoted 10 lines
arch/arm64/include/uapi/asm/kvm.h +10 -0
+#define KVM_CAP_ARM_RME_INIT_RIPAS_REALM	2
+#define KVM_CAP_ARM_RME_POPULATE_REALM		3
+#define KVM_CAP_ARM_RME_ACTIVATE_REALM		4
+
+/* List of configuration items accepted for KVM_CAP_ARM_RME_CONFIG_REALM */
+#define ARM_RME_CONFIG_RPV			0
+#define ARM_RME_CONFIG_HASH_ALGO		1
+
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA256		0
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA512		1

minor nit:
May be we could rename this to CONFIG_HASH_ALGO_* to align with the RMM spec (btw which also moved from measurement to hash) and the parameter.

Suzuki

Quoted 64 lines
arch/arm64/include/uapi/asm/kvm.h +35 -0
+
+#define ARM_RME_CONFIG_RPV_SIZE 64
+
+struct arm_rme_config {
+	__u32 cfg;
+	union {
+		/* cfg == ARM_RME_CONFIG_RPV */
+		struct {
+			__u8	rpv[ARM_RME_CONFIG_RPV_SIZE];
+		};
+
+		/* cfg == ARM_RME_CONFIG_HASH_ALGO */
+		struct {
+			__u32	hash_algo;
+		};
+
+		/* Fix the size of the union */
+		__u8	reserved[256];
+	};
+};
+
+#define KVM_ARM_RME_POPULATE_FLAGS_MEASURE	(1 << 0)
+struct arm_rme_populate_realm {
+	__u64 base;
+	__u64 size;
+	__u32 flags;
+	__u32 reserved[3];
+};
+
+struct arm_rme_init_ripas {
+	__u64 base;
+	__u64 size;
+	__u64 reserved[2];
+};
+
  /* Device Control API on vcpu fd */
  #define KVM_ARM_VCPU_PMU_V3_CTRL	0
  #define   KVM_ARM_VCPU_PMU_V3_IRQ	0
include/uapi/linux/kvm.h +10 -0
index b6ae8ad8934b..0b8479985581 100644
@@ -930,6 +930,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
  #define KVM_CAP_X86_GUEST_MODE 238
  #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239
+#define KVM_CAP_ARM_RME 240
 
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
@@ -1582,4 +1583,13 @@ struct kvm_pre_fault_memory {
  	__u64 padding[5];
  };
 
+/* Available with KVM_CAP_ARM_RME, only for VMs with KVM_VM_TYPE_ARM_REALM  */
+struct kvm_arm_rmm_psci_complete {
+	__u64 target_mpidr;
+	__u32 psci_status;
+	__u32 padding[3];
+};
+
+#define KVM_ARM_VCPU_RMM_PSCI_COMPLETE	_IOW(KVMIO, 0xd6, struct kvm_arm_rmm_psci_complete)
+
  #endif /* __LINUX_KVM_H */
#13 lore raw

Re: [PATCH v8 06/43] arm64: RME: Define the user ABI

Quoted 107 lines · Suzuki K Poulose

Hi Steven

Quoted 104 lines · Steven Price

There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.

---
Changes since v7:
  * Add documentation of new ioctls
  * Bump the magic numbers to avoid conflicts
Changes since v6:
  * Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
  * Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!
---
  Documentation/virt/kvm/api.rst    | 70 +++++++++++++++++++++++++++++++ arch/arm64/include/uapi/asm/kvm.h | 49 ++++++++++++++++++++++ include/uapi/linux/kvm.h          | 10 +++++

  3 files changed, 129 insertions(+)
Documentation/virt/kvm/ renamed from Documentation/virt/kvm/api.rst +0 -0

api.rst
index 1f8625b7646a..99ba6c82cf37 100644

Documentation/virt/kvm/api.rst +0 -0
@@ -3527,6 +3527,11 @@ Possible features:
             - the KVM_REG_ARM64_SVE_VLS pseudo-register is immutable,

and can
              no longer be written using KVM_SET_ONE_REG.
  +    - KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this
+      VCPU. This must be specified on all VCPUs created in a Realm VM. +      Depends on KVM_CAP_ARM_RME.
+      Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC).
+
  4.83 KVM_ARM_PREFERRED_TARGET
  -----------------------------
  @@ -5098,6 +5103,7 @@ Recognised values for feature:
      =====      =========================================== arm64      KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE)
+  arm64      KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME) =====      =========================================== Finalizes the configuration of the specified vcpu feature.

Documentation/virt/kvm/api.rst +0 -0
@@ -6452,6 +6458,30 @@ the capability to be present.

    flags must currently be zero.
  +4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE
+------------------------------------

Documentation/virt/kvm/api.rst +16 -0
+
+:Capability: KVM_CAP_ARM_RME
+:Architectures: arm64
+:Type: vcpu ioctl
+:Parameters: struct kvm_arm_rmm_psci_complete (in)
+:Returns: 0 if successful, < 0 on error
+
+::
+
+  struct kvm_arm_rmm_psci_complete {
+    __u64 target_mpidr;
+    __u32 psci_status;
+    __u32 padding[3];
+  };
+
+Where PSCI functions are handled by user space, the RMM needs to be

informed of

Documentation/virt/kvm/api.rst +2 -0
+the target of the operation using `target_mpidr`, along with the status
+(`psci_status`). The RMM v1.0 specification defines two functions

that require

Documentation/virt/kvm/api.rst +3 -0
+this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO.
+
+If the kernel is handling PSCI then this is done automatically and

the VMM
+doesn't need to call this ioctl.
    .. _kvm_run:
  @@ -8280,6 +8310,46 @@ aforementioned registers before the first KVM_RUN. These registers are VM
  scoped, meaning that the same set of values are presented on all vCPUs in a
  given VM.
  +7.38 KVM_CAP_ARM_RME
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: args[0] provides an action, args[1] points to a structure in
+         memory for some actions.
+:Returns: 0 on success, negative value on error

Documentation/virt/kvm/api.rst +2 -0
+
+Used to configure and set up the memory for a Realm. The available

actions are:
+
+=================================
=============================================
+ KVM_CAP_ARM_RME_CONFIG_REALM     Takes struct arm_rme_config as args[1] and
+                                  configures realm parameters prior to it being
+                                  created.
+
+                                  Options are ARM_RME_CONFIG_RPV to set the
+                                  "Realm Personalization Value" and

Quoted 8 lines
Quoted 6 lines

+                                  ARM_RME_CONFIG_HASH_ALGO to set the hash
+                                  algorithm.
+
+ KVM_CAP_ARM_RME_CREATE_REALM     Request the RMM create the realm. The realm's

minor nit: s/RMM create/RMM to create/

Ack

Quoted 5 lines

Or may be rephrase it:

Request the RMM to create the Realm with the configured parameters.

Quoted 15 lines
Quoted 5 lines

+                                  configuration parameters must be set first.
+
+ KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1]

Quoted 8 lines

+                                  and sets the RIPAS (Realm IPA State) to
+                                  RIPAS_RAM of a specified area of the realm's
+                                  IPA.
+
+ KVM_CAP_ARM_RME_POPULATE_REALM   Takes struct arm_rme_init_ripas as args[1]

nit: struct arm_rme_populate_realm

Copy/paste error - thanks for spotting.

Quoted 9 lines
Quoted 7 lines

+                                  and populates a region of protected address
+                                  space by copying the data from the shared
+                                  alias.
+
+ KVM_CAP_ARM_RME_ACTIVATE_REALM   Request the RMM activate the realm. No

s/the RMM/the RMM to/

Ack

Quoted 12 lines
Quoted 6 lines

+                                  further changes can be made to the realm's
+                                  configuration, and VCPUs are not permitted to
+                                  enter the realm until it has been activated.

minor nit: this sounds as if "configurations" (of parameters)  are possible after the CREATE_REALM and before ACTIVATE_REALM ? Could we also make it clear that the VCPUs must have been created before ACTIVATE ?

Quoted 4 lines

May be rephrase it to:

No changes can be made to Realm's memory (including the IPA state). No new VCPUs can be created after this step.

I was attmepting to include that CONFIG_REALM cannot be called. How about:

Request the RMM to activate the realm. No changes can be made to the Realm's memory, IPA state or configuration parameters. No new VCPUs can be created after this step.

Quoted 34 lines
Quoted 34 lines

+=================================
=============================================
+
  8. Other capabilities.
  ======================
  diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/ uapi/asm/kvm.h
index af9d9acaf997..b57712880605 100644

arch/arm64/include/uapi/asm/kvm.h +0 -0
@@ -106,6 +106,7 @@ struct kvm_regs {
   #define KVM_ARM_VCPU_PTRAUTH_GENERIC    6 /* VCPU uses generic

authentication */
  #define KVM_ARM_VCPU_HAS_EL2        7 /* Support nested virtualization */
  #define KVM_ARM_VCPU_HAS_EL2_E2H0    8 /* Limit NV support to E2H RES0 */
+#define KVM_ARM_VCPU_REC        9 /* VCPU REC state as part of Realm */ struct kvm_vcpu_init {
      __u32 target;

arch/arm64/include/uapi/asm/kvm.h +0 -0
@@ -429,6 +430,54 @@ enum {
   #define   KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES    3
   #define   KVM_DEV_ARM_ITS_CTRL_RESET        4

  +/* KVM_CAP_ARM_RME on VM fd */

arch/arm64/include/uapi/asm/kvm.h +7 -0
+#define KVM_CAP_ARM_RME_CONFIG_REALM        0
+#define KVM_CAP_ARM_RME_CREATE_REALM        1
+#define KVM_CAP_ARM_RME_INIT_RIPAS_REALM    2
+#define KVM_CAP_ARM_RME_POPULATE_REALM        3
+#define KVM_CAP_ARM_RME_ACTIVATE_REALM        4
+
+/* List of configuration items accepted for

KVM_CAP_ARM_RME_CONFIG_REALM */

arch/arm64/include/uapi/asm/kvm.h +2 -0
+#define ARM_RME_CONFIG_RPV            0
+#define ARM_RME_CONFIG_HASH_ALGO        1
Quoted 8 lines

arch/arm64/include/uapi/asm/kvm.h enum { · line 445

Quoted 3 lines
arch/arm64/include/uapi/asm/kvm.h +3 -0
+
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA256        0
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA512        1

minor nit:
May be we could rename this to CONFIG_HASH_ALGO_* to align with the RMM spec (btw which also moved from measurement to hash) and the parameter.

Ah, I must have missed that rename in the spec - yes that makes sense, I've generally tried to stick closely to the naming in the spec.

Thanks,
Steve

Quoted 68 lines

Suzuki

Quoted 64 lines
arch/arm64/include/uapi/asm/kvm.h +35 -0
+
+#define ARM_RME_CONFIG_RPV_SIZE 64
+
+struct arm_rme_config {
+    __u32 cfg;
+    union {
+        /* cfg == ARM_RME_CONFIG_RPV */
+        struct {
+            __u8    rpv[ARM_RME_CONFIG_RPV_SIZE];
+        };
+
+        /* cfg == ARM_RME_CONFIG_HASH_ALGO */
+        struct {
+            __u32    hash_algo;
+        };
+
+        /* Fix the size of the union */
+        __u8    reserved[256];
+    };
+};
+
+#define KVM_ARM_RME_POPULATE_FLAGS_MEASURE    (1 << 0)
+struct arm_rme_populate_realm {
+    __u64 base;
+    __u64 size;
+    __u32 flags;
+    __u32 reserved[3];
+};
+
+struct arm_rme_init_ripas {
+    __u64 base;
+    __u64 size;
+    __u64 reserved[2];
+};
+

  /* Device Control API on vcpu fd */
  #define KVM_ARM_VCPU_PMU_V3_CTRL    0
  #define   KVM_ARM_VCPU_PMU_V3_IRQ    0

include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..0b8479985581 100644
@@ -930,6 +930,7 @@ struct kvm_enable_cap {
   #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
   #define KVM_CAP_X86_GUEST_MODE 238
   #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239
+#define KVM_CAP_ARM_RME 240
     struct kvm_irq_routing_irqchip {
       __u32 irqchip;
@@ -1582,4 +1583,13 @@ struct kvm_pre_fault_memory {
       __u64 padding[5];
   };

  +/* Available with KVM_CAP_ARM_RME, only for VMs with KVM_VM_TYPE_ARM_REALM  */

include/uapi/linux/kvm.h +7 -0
+struct kvm_arm_rmm_psci_complete {
+    __u64 target_mpidr;
+    __u32 psci_status;
+    __u32 padding[3];
+};
+
+#define KVM_ARM_VCPU_RMM_PSCI_COMPLETE    _IOW(KVMIO, 0xd6, struct

kvm_arm_rmm_psci_complete)
+
  #endif /* __LINUX_KVM_H */

#14 lore raw

Re: [PATCH v8 06/43] arm64: RME: Define the user ABI

Quoted 29 lines · Steven Price
Quoted 23 lines · Suzuki K Poulose

Hi Steven

Quoted 16 lines · Steven Price

There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.

---
Changes since v7:
  * Add documentation of new ioctls
  * Bump the magic numbers to avoid conflicts
Changes since v6:
  * Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
  * Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

Quoted 2 lines
---
   Documentation/virt/kvm/api.rst    | 70 +++++++++++++++++++++++++++++++
Quoted 4 lines

May be rephrase it to:

No changes can be made to Realm's memory (including the IPA state). No new VCPUs can be created after this step.

Quoted 6 lines

I was attmepting to include that CONFIG_REALM cannot be called. How about:

Request the RMM to activate the realm. No changes can be made to the Realm's memory, IPA state or configuration parameters. No new VCPUs can be created after this step.

Looks good to me, cheers

Suzuki

Reviewed-by on v8 6/43
#15 lore raw

Re: [PATCH v8 06/43] arm64: RME: Define the user ABI

In reply to Steven Price
Quoted 16 lines · Steven Price

There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.

---
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

Quoted 6 lines
---
  Documentation/virt/kvm/api.rst    | 70 +++++++++++++++++++++++++++++++
  arch/arm64/include/uapi/asm/kvm.h | 49 ++++++++++++++++++++++
  include/uapi/linux/kvm.h          | 10 +++++
  3 files changed, 129 insertions(+)

With below comment addressed:

Quoted 87 lines
Documentation/virt/kvm/api.rst +58 -0
index 1f8625b7646a..99ba6c82cf37 100644
@@ -3527,6 +3527,11 @@ Possible features:
  	      - the KVM_REG_ARM64_SVE_VLS pseudo-register is immutable, and can
  	        no longer be written using KVM_SET_ONE_REG.
 
+	- KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this
+	  VCPU. This must be specified on all VCPUs created in a Realm VM.
+	  Depends on KVM_CAP_ARM_RME.
+	  Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC).
+
  4.83 KVM_ARM_PREFERRED_TARGET
  -----------------------------
 
@@ -5098,6 +5103,7 @@ Recognised values for feature:
 
    =====      ===========================================
    arm64      KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE)
+  arm64      KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME)
    =====      ===========================================
 
  Finalizes the configuration of the specified vcpu feature.
@@ -6452,6 +6458,30 @@ the capability to be present.
 
  `flags` must currently be zero.
 
+4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE
+------------------------------------
+
+:Capability: KVM_CAP_ARM_RME
+:Architectures: arm64
+:Type: vcpu ioctl
+:Parameters: struct kvm_arm_rmm_psci_complete (in)
+:Returns: 0 if successful, < 0 on error
+
+::
+
+  struct kvm_arm_rmm_psci_complete {
+	__u64 target_mpidr;
+	__u32 psci_status;
+	__u32 padding[3];
+  };
+
+Where PSCI functions are handled by user space, the RMM needs to be informed of
+the target of the operation using `target_mpidr`, along with the status
+(`psci_status`). The RMM v1.0 specification defines two functions that require
+this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO.
+
+If the kernel is handling PSCI then this is done automatically and the VMM
+doesn't need to call this ioctl.
 
  .. _kvm_run:
 
@@ -8280,6 +8310,46 @@ aforementioned registers before the first KVM_RUN. These registers are VM
  scoped, meaning that the same set of values are presented on all vCPUs in a
  given VM.
 
+7.38 KVM_CAP_ARM_RME
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: args[0] provides an action, args[1] points to a structure in
+	     memory for some actions.
+:Returns: 0 on success, negative value on error
+
+Used to configure and set up the memory for a Realm. The available actions are:
+
+================================= =============================================
+ KVM_CAP_ARM_RME_CONFIG_REALM     Takes struct arm_rme_config as args[1] and
+                                  configures realm parameters prior to it being
+                                  created.
+
+                                  Options are ARM_RME_CONFIG_RPV to set the
+                                  "Realm Personalization Value" and
+                                  ARM_RME_CONFIG_HASH_ALGO to set the hash
+                                  algorithm.
+
+ KVM_CAP_ARM_RME_CREATE_REALM     Request the RMM create the realm. The realm's
+                                  configuration parameters must be set first.
+
+ KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1]
+                                  and sets the RIPAS (Realm IPA State) to
+                                  RIPAS_RAM of a specified area of the realm's
+                                  IPA.

Documentation/virt/kvm/api.rst aforementioned registers before the first KVM_RUN. These registers are VM · line 8350

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+ KVM_CAP_ARM_RME_POPULATE_REALM   Takes struct arm_rme_init_ripas as args[1]
+                                  and populates a region of protected address
+                                  space by copying the data from the shared
+                                  alias.
+
+ KVM_CAP_ARM_RME_ACTIVATE_REALM   Request the RMM activate the realm. No
+                                  further changes can be made to the realm's
+                                  configuration, and VCPUs are not permitted to
+                                  enter the realm until it has been activated.
                                                              ^^^^^^^^^^^^^^^^^^

s/has been activated/is deactivated

I don't see where the guard is applied to prevent vCPU is added after realm has been activated. I may missed that. Otherwise, it's something to be improved n the subsequent patch where a vCPU is created and added to the realm.

Quoted 88 lines
Documentation/virt/kvm/api.rst +2 -0
+================================= =============================================
+
  8. Other capabilities.
  ======================
 
arch/arm64/include/uapi/asm/kvm.h +49 -0
index af9d9acaf997..b57712880605 100644
@@ -106,6 +106,7 @@ struct kvm_regs {
  #define KVM_ARM_VCPU_PTRAUTH_GENERIC	6 /* VCPU uses generic authentication */
  #define KVM_ARM_VCPU_HAS_EL2		7 /* Support nested virtualization */
  #define KVM_ARM_VCPU_HAS_EL2_E2H0	8 /* Limit NV support to E2H RES0 */
+#define KVM_ARM_VCPU_REC		9 /* VCPU REC state as part of Realm */
 
  struct kvm_vcpu_init {
  	__u32 target;
@@ -429,6 +430,54 @@ enum {
  #define   KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES	3
  #define   KVM_DEV_ARM_ITS_CTRL_RESET		4
 
+/* KVM_CAP_ARM_RME on VM fd */
+#define KVM_CAP_ARM_RME_CONFIG_REALM		0
+#define KVM_CAP_ARM_RME_CREATE_REALM		1
+#define KVM_CAP_ARM_RME_INIT_RIPAS_REALM	2
+#define KVM_CAP_ARM_RME_POPULATE_REALM		3
+#define KVM_CAP_ARM_RME_ACTIVATE_REALM		4
+
+/* List of configuration items accepted for KVM_CAP_ARM_RME_CONFIG_REALM */
+#define ARM_RME_CONFIG_RPV			0
+#define ARM_RME_CONFIG_HASH_ALGO		1
+
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA256		0
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA512		1
+
+#define ARM_RME_CONFIG_RPV_SIZE 64
+
+struct arm_rme_config {
+	__u32 cfg;
+	union {
+		/* cfg == ARM_RME_CONFIG_RPV */
+		struct {
+			__u8	rpv[ARM_RME_CONFIG_RPV_SIZE];
+		};
+
+		/* cfg == ARM_RME_CONFIG_HASH_ALGO */
+		struct {
+			__u32	hash_algo;
+		};
+
+		/* Fix the size of the union */
+		__u8	reserved[256];
+	};
+};
+
+#define KVM_ARM_RME_POPULATE_FLAGS_MEASURE	(1 << 0)
+struct arm_rme_populate_realm {
+	__u64 base;
+	__u64 size;
+	__u32 flags;
+	__u32 reserved[3];
+};
+
+struct arm_rme_init_ripas {
+	__u64 base;
+	__u64 size;
+	__u64 reserved[2];
+};
+
  /* Device Control API on vcpu fd */
  #define KVM_ARM_VCPU_PMU_V3_CTRL	0
  #define   KVM_ARM_VCPU_PMU_V3_IRQ	0
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..0b8479985581 100644
@@ -930,6 +930,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
  #define KVM_CAP_X86_GUEST_MODE 238
  #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239
+#define KVM_CAP_ARM_RME 240
 
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
@@ -1582,4 +1583,13 @@ struct kvm_pre_fault_memory {
  	__u64 padding[5];
  };
 

include/uapi/linux/kvm.h struct kvm_pre_fault_memory { · line 1595

Quoted 10 lines
include/uapi/linux/kvm.h +9 -0
+/* Available with KVM_CAP_ARM_RME, only for VMs with KVM_VM_TYPE_ARM_REALM  */
+struct kvm_arm_rmm_psci_complete {
+	__u64 target_mpidr;
+	__u32 psci_status;
+	__u32 padding[3];
+};
+
+#define KVM_ARM_VCPU_RMM_PSCI_COMPLETE	_IOW(KVMIO, 0xd6, struct kvm_arm_rmm_psci_complete)
+
  #endif /* __LINUX_KVM_H */

Thanks,
Gavin

#16 lore raw

Re: [PATCH v8 06/43] arm64: RME: Define the user ABI

Hi Gavin,

Quoted 137 lines · Gavin Shan
Quoted 20 lines · Steven Price

There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.

---
Changes since v7:
  * Add documentation of new ioctls
  * Bump the magic numbers to avoid conflicts
Changes since v6:
  * Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
  * Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!
---
  Documentation/virt/kvm/api.rst    | 70 +++++++++++++++++++++++++++++++ arch/arm64/include/uapi/asm/kvm.h | 49 ++++++++++++++++++++++ include/uapi/linux/kvm.h          | 10 +++++

Quoted 2 lines
  3 files changed, 129 insertions(+)

With below comment addressed:

Quoted 109 lines
Documentation/virt/kvm/ renamed from Documentation/virt/kvm/api.rst +0 -0

api.rst
index 1f8625b7646a..99ba6c82cf37 100644

Documentation/virt/kvm/api.rst +0 -0
@@ -3527,6 +3527,11 @@ Possible features:
             - the KVM_REG_ARM64_SVE_VLS pseudo-register is immutable,

and can
              no longer be written using KVM_SET_ONE_REG.
  +    - KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this
+      VCPU. This must be specified on all VCPUs created in a Realm VM. +      Depends on KVM_CAP_ARM_RME.
+      Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC).
+
  4.83 KVM_ARM_PREFERRED_TARGET
  -----------------------------
  @@ -5098,6 +5103,7 @@ Recognised values for feature:
      =====      =========================================== arm64      KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE)
+  arm64      KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME) =====      =========================================== Finalizes the configuration of the specified vcpu feature.

Documentation/virt/kvm/api.rst +0 -0
@@ -6452,6 +6458,30 @@ the capability to be present.

    flags must currently be zero.
  +4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE
+------------------------------------

Documentation/virt/kvm/api.rst +16 -0
+
+:Capability: KVM_CAP_ARM_RME
+:Architectures: arm64
+:Type: vcpu ioctl
+:Parameters: struct kvm_arm_rmm_psci_complete (in)
+:Returns: 0 if successful, < 0 on error
+
+::
+
+  struct kvm_arm_rmm_psci_complete {
+    __u64 target_mpidr;
+    __u32 psci_status;
+    __u32 padding[3];
+  };
+
+Where PSCI functions are handled by user space, the RMM needs to be

informed of

Documentation/virt/kvm/api.rst +2 -0
+the target of the operation using `target_mpidr`, along with the status
+(`psci_status`). The RMM v1.0 specification defines two functions

that require

Documentation/virt/kvm/api.rst +3 -0
+this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO.
+
+If the kernel is handling PSCI then this is done automatically and

the VMM
+doesn't need to call this ioctl.
    .. _kvm_run:
  @@ -8280,6 +8310,46 @@ aforementioned registers before the first KVM_RUN. These registers are VM
  scoped, meaning that the same set of values are presented on all vCPUs in a
  given VM.
  +7.38 KVM_CAP_ARM_RME
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: args[0] provides an action, args[1] points to a structure in
+         memory for some actions.
+:Returns: 0 on success, negative value on error

Documentation/virt/kvm/api.rst +2 -0
+
+Used to configure and set up the memory for a Realm. The available

actions are:
+
+=================================
=============================================
+ KVM_CAP_ARM_RME_CONFIG_REALM     Takes struct arm_rme_config as args[1] and
+                                  configures realm parameters prior to it being
+                                  created.
+
+                                  Options are ARM_RME_CONFIG_RPV to set the
+                                  "Realm Personalization Value" and +                                  ARM_RME_CONFIG_HASH_ALGO to set the hash
+                                  algorithm.
+
+ KVM_CAP_ARM_RME_CREATE_REALM     Request the RMM create the realm. The realm's
+                                  configuration parameters must be set first.
+
+ KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1]
+                                  and sets the RIPAS (Realm IPA State) to
+                                  RIPAS_RAM of a specified area of the realm's
+                                  IPA.
+
+ KVM_CAP_ARM_RME_POPULATE_REALM   Takes struct arm_rme_init_ripas as args[1]
+                                  and populates a region of protected address
+                                  space by copying the data from the shared
+                                  alias.
+
+ KVM_CAP_ARM_RME_ACTIVATE_REALM   Request the RMM activate the realm. No +                                  further changes can be made to the

Quoted 8 lines
Quoted 5 lines

realm's
+                                  configuration, and VCPUs are not permitted to
+                                  enter the realm until it has been activated.


^^^^^^^^^^^^^^^^^^
s/has been activated/is deactivated

I think I worded it badly before (as Susuki has already pointed out) and you haven't understood what I intended.

VCPUs can be added to a Realm guest up until the ACTIVATE_REALM call. There is no "deactivate" - you would need to destroy the guest and start again.

What I intended was to say that a VCPU must not be executed (KVM_RUN) until the ACTIVE_REALM call.

Quoted 4 lines

I don't see where the guard is applied to prevent vCPU is added after realm has been activated. I may missed that. Otherwise, it's something to be improved
n the subsequent patch where a vCPU is created and added to the realm.

This is handled in kvm_create_rec() where there's a check that kvm_realm_state() != REALM_STATE_NEW. This prevents finalization of a VCPU unless the realm is still in "new" state.

Admittedly this doesn't technically stop the user creating a VCPU. I could add a check in kvm_arch_vcpu_create(), however this wouldn't stop a VMM creating a VCPU and not finalising it before activating the realm. So we'd end up with the same situation. I'm not sure whether it's worth trying to stop the VMM or if I should just reword to say that the VMM shouldn't do this.

I don't believe anything bad will happen to the host in these situations
- you will just end up with a useless VCPU which cannot be run.

Thanks,
Steve

Quoted 105 lines
Quoted 98 lines

+=================================
=============================================
+
  8. Other capabilities.
  ======================
  diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/ uapi/asm/kvm.h
index af9d9acaf997..b57712880605 100644

arch/arm64/include/uapi/asm/kvm.h +0 -0
@@ -106,6 +106,7 @@ struct kvm_regs {
   #define KVM_ARM_VCPU_PTRAUTH_GENERIC    6 /* VCPU uses generic

authentication */
  #define KVM_ARM_VCPU_HAS_EL2        7 /* Support nested virtualization */
  #define KVM_ARM_VCPU_HAS_EL2_E2H0    8 /* Limit NV support to E2H RES0 */
+#define KVM_ARM_VCPU_REC        9 /* VCPU REC state as part of Realm */ struct kvm_vcpu_init {
      __u32 target;

arch/arm64/include/uapi/asm/kvm.h +0 -0
@@ -429,6 +430,54 @@ enum {
   #define   KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES    3
   #define   KVM_DEV_ARM_ITS_CTRL_RESET        4

  +/* KVM_CAP_ARM_RME on VM fd */

arch/arm64/include/uapi/asm/kvm.h +7 -0
+#define KVM_CAP_ARM_RME_CONFIG_REALM        0
+#define KVM_CAP_ARM_RME_CREATE_REALM        1
+#define KVM_CAP_ARM_RME_INIT_RIPAS_REALM    2
+#define KVM_CAP_ARM_RME_POPULATE_REALM        3
+#define KVM_CAP_ARM_RME_ACTIVATE_REALM        4
+
+/* List of configuration items accepted for

KVM_CAP_ARM_RME_CONFIG_REALM */

arch/arm64/include/uapi/asm/kvm.h +40 -0
+#define ARM_RME_CONFIG_RPV            0
+#define ARM_RME_CONFIG_HASH_ALGO        1
+
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA256        0
+#define ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA512        1
+
+#define ARM_RME_CONFIG_RPV_SIZE 64
+
+struct arm_rme_config {
+    __u32 cfg;
+    union {
+        /* cfg == ARM_RME_CONFIG_RPV */
+        struct {
+            __u8    rpv[ARM_RME_CONFIG_RPV_SIZE];
+        };
+
+        /* cfg == ARM_RME_CONFIG_HASH_ALGO */
+        struct {
+            __u32    hash_algo;
+        };
+
+        /* Fix the size of the union */
+        __u8    reserved[256];
+    };
+};
+
+#define KVM_ARM_RME_POPULATE_FLAGS_MEASURE    (1 << 0)
+struct arm_rme_populate_realm {
+    __u64 base;
+    __u64 size;
+    __u32 flags;
+    __u32 reserved[3];
+};
+
+struct arm_rme_init_ripas {
+    __u64 base;
+    __u64 size;
+    __u64 reserved[2];
+};
+

  /* Device Control API on vcpu fd */
  #define KVM_ARM_VCPU_PMU_V3_CTRL    0
  #define   KVM_ARM_VCPU_PMU_V3_IRQ    0

include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..0b8479985581 100644
@@ -930,6 +930,7 @@ struct kvm_enable_cap {
   #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
   #define KVM_CAP_X86_GUEST_MODE 238
   #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239
+#define KVM_CAP_ARM_RME 240
     struct kvm_irq_routing_irqchip {
       __u32 irqchip;
@@ -1582,4 +1583,13 @@ struct kvm_pre_fault_memory {
       __u64 padding[5];
   };

  +/* Available with KVM_CAP_ARM_RME, only for VMs with KVM_VM_TYPE_ARM_REALM  */

include/uapi/linux/kvm.h +7 -0
+struct kvm_arm_rmm_psci_complete {
+    __u64 target_mpidr;
+    __u32 psci_status;
+    __u32 padding[3];
+};
+
+#define KVM_ARM_VCPU_RMM_PSCI_COMPLETE    _IOW(KVMIO, 0xd6, struct
Quoted 3 lines

kvm_arm_rmm_psci_complete)
+
  #endif /* __LINUX_KVM_H */

Thanks,
Gavin

#17 lore raw

PATCH v8 7/43 arm64: RME: ioctls to create and configure realms

In reply to Steven Price

Add the KVM_CAP_ARM_RME_CREATE_RD ioctl to create a realm. This involves delegating pages to the RMM to hold the Realm Descriptor (RD) and for the base level of the Realm Translation Tables (RTT). A VMID also need to be picked, since the RMM has a separate VMID address space a dedicated allocator is added for this purpose.

KVM_CAP_ARM_RME_CONFIG_REALM is provided to allow configuring the realm before it is created. Configuration options can be classified as:

1. Parameters specific to the Realm stage2 (e.g. IPA Size, vmid, stage2 entry level, entry level RTTs, number of RTTs in start level, LPA2) Most of these are not measured by RMM and comes from KVM book keeping.

2. Parameters controlling "Arm Architecture features for the VM". (e.g. SVE VL, PMU counters, number of HW BRPs/WPs), configured by the VMM using the "user ID register write" mechanism. These will be supported in the later patches.

3. Parameters are not part of the core Arm architecture but defined by the RMM spec (e.g. Hash algorithm for measurement, Personalisation value). These are programmed via KVM_CAP_ARM_RME_CONFIG_REALM.

For the IPA size there is the possibility that the RMM supports a different size to the IPA size supported by KVM for normal guests. At the moment the 'normal limit' is exposed by KVM_CAP_ARM_VM_IPA_SIZE and the IPA size is configured by the bottom bits of vm_type in KVM_CREATE_VM. This means that it isn't easy for the VMM to discover what IPA sizes are supported for Realm guests. Since the IPA is part of the measurement of the realm guest the current expectation is that the VMM will be required to pick the IPA size demanded by attestation and therefore simply failing if this isn't available is fine. An option would be to expose a new capability ioctl to obtain the RMM's maximum IPA size if this is needed in the future.

---
Changes since v7:
* Minor code cleanup following Gavin's review.
Changes since v6:
* Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an RMM which uses 4k granules.
Changes since v5:
* Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
* Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
* Improved commit description.
* Improved return failures for rmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.

---
 arch/arm64/include/asm/kvm_emulate.h |   5 +
 arch/arm64/include/asm/kvm_rme.h     |  19 ++
 arch/arm64/kvm/arm.c                 |  16 ++
 arch/arm64/kvm/mmu.c                 |  22 +-
 arch/arm64/kvm/rme.c                 | 319 +++++++++++++++++++++++++++
 5 files changed, 379 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +5 -0
index 1c43a4fc25dd..4ee6c215da82 100644
@@ -699,6 +699,11 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)
 	return READ_ONCE(kvm->arch.realm.state);
 }
 
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
+}
+
 static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
 {
 	return false;
arch/arm64/include/asm/kvm_rme.h +19 -0
index 9c8a0b23e0e4..5dc1915de891 100644
@@ -6,6 +6,8 @@
 #ifndef __ASM_KVM_RME_H
 #define __ASM_KVM_RME_H
 
+#include <uapi/linux/kvm.h>
+
 /**
  * enum realm_state - State of a Realm
  */
@@ -46,11 +48,28 @@ enum realm_state {
  * struct realm - Additional per VM data for a Realm
  *
  * @state: The lifetime state machine for the realm
+ * @rd: Kernel mapping of the Realm Descriptor (RD)
+ * @params: Parameters for the RMI_REALM_CREATE command
+ * @num_aux: The number of auxiliary pages required by the RMM
+ * @vmid: VMID to be used by the RMM for the realm
+ * @ia_bits: Number of valid Input Address bits in the IPA
  */
 struct realm {
 	enum realm_state state;
+
+	void *rd;
+	struct realm_params *params;
+
+	unsigned long num_aux;
+	unsigned int vmid;
+	unsigned int ia_bits;
 };
 
 void kvm_init_rme(void);
+u32 kvm_realm_ipa_limit(void);
+
+int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
+int kvm_init_realm_vm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);
 
 #endif /* __ASM_KVM_RME_H */
arch/arm64/kvm/arm.c +16 -0
index 856a721d41ac..0e8482fdc4d3 100644
@@ -136,6 +136,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		}
 		mutex_unlock(&kvm->lock);
 		break;
+	case KVM_CAP_ARM_RME:
+		mutex_lock(&kvm->lock);
+		r = kvm_realm_enable_cap(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
 	default:
 		break;
 	}
@@ -198,6 +203,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	/* Initialise the realm bits after the generic bits are enabled */
+	if (kvm_is_realm(kvm)) {
+		ret = kvm_init_realm_vm(kvm);
+		if (ret)
+			goto err_free_cpumask;
+	}
+
 	return 0;
 
 err_free_cpumask:
@@ -257,6 +269,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvm_unshare_hyp(kvm, kvm + 1);
 
 	kvm_arm_teardown_hypercalls(kvm);
+	kvm_destroy_realm(kvm);
 }
 
 static bool kvm_has_full_ptr_auth(void)
@@ -405,6 +418,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
 		r = BIT(0);
 		break;
+	case KVM_CAP_ARM_RME:
+		r = static_key_enabled(&kvm_rme_is_available);
+		break;
 	default:
 		r = 0;
 	}
arch/arm64/kvm/mmu.c +20 -2
index 2feb6c6b63af..5957a07de86d 100644
@@ -876,12 +876,16 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
 	.icache_inval_pou	= invalidate_icache_guest_page,
 };
 
-static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
+static int kvm_init_ipa_range(struct kvm *kvm,
+			      struct kvm_s2_mmu *mmu, unsigned long type)
 {
 	u32 kvm_ipa_limit = get_kvm_ipa_limit();
 	u64 mmfr0, mmfr1;
 	u32 phys_shift;
 
+	if (kvm_is_realm(kvm))
+		kvm_ipa_limit = kvm_realm_ipa_limit();
+
 	if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
 		return -EINVAL;
 
@@ -946,7 +950,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 		return -EINVAL;
 	}
 
-	err = kvm_init_ipa_range(mmu, type);
+	err = kvm_init_ipa_range(kvm, mmu, type);
 	if (err)
 		return err;
 
@@ -1072,6 +1076,20 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 	struct kvm_pgtable *pgt = NULL;
 
 	write_lock(&kvm->mmu_lock);
+	if (kvm_is_realm(kvm) &&
+	    (kvm_realm_state(kvm) != REALM_STATE_DEAD &&
+	     kvm_realm_state(kvm) != REALM_STATE_NONE)) {
+		/* Tearing down RTTs will be added in a later patch */
+		write_unlock(&kvm->mmu_lock);
+
+		/*
+		 * The physical PGD pages are delegated to the RMM, so cannot
+		 * be freed at this point. This function will be called again
+		 * from kvm_destroy_realm() after the physical pages have been
+		 * returned at which point the memory can be freed.
+		 */
+		return;
+	}
 	pgt = mmu->pgt;
 	if (pgt) {
 		mmu->pgd_phys = 0;
arch/arm64/kvm/rme.c +319 -0

339 lines not on this page · raw message

Signature · 1 line
2.43.0
#18 lore raw

Re: [PATCH v8 07/43] arm64: RME: ioctls to create and configure realms

Quoted 255 lines · Steven Price

Add the KVM_CAP_ARM_RME_CREATE_RD ioctl to create a realm. This involves delegating pages to the RMM to hold the Realm Descriptor (RD) and for the base level of the Realm Translation Tables (RTT). A VMID also need to be picked, since the RMM has a separate VMID address space a dedicated allocator is added for this purpose.

KVM_CAP_ARM_RME_CONFIG_REALM is provided to allow configuring the realm before it is created. Configuration options can be classified as:

1. Parameters specific to the Realm stage2 (e.g. IPA Size, vmid, stage2 entry level, entry level RTTs, number of RTTs in start level, LPA2) Most of these are not measured by RMM and comes from KVM book keeping.

2. Parameters controlling "Arm Architecture features for the VM". (e.g. SVE VL, PMU counters, number of HW BRPs/WPs), configured by the VMM using the "user ID register write" mechanism. These will be supported in the later patches.

3. Parameters are not part of the core Arm architecture but defined by the RMM spec (e.g. Hash algorithm for measurement, Personalisation value). These are programmed via KVM_CAP_ARM_RME_CONFIG_REALM.

For the IPA size there is the possibility that the RMM supports a different size to the IPA size supported by KVM for normal guests. At the moment the 'normal limit' is exposed by KVM_CAP_ARM_VM_IPA_SIZE and the IPA size is configured by the bottom bits of vm_type in KVM_CREATE_VM. This means that it isn't easy for the VMM to discover what IPA sizes are supported for Realm guests. Since the IPA is part of the measurement of the realm guest the current expectation is that the VMM will be required to pick the IPA size demanded by attestation and therefore simply failing if this isn't available is fine. An option would be to expose a new capability ioctl to obtain the RMM's maximum IPA size if this is needed in the future.

---
Changes since v7:
* Minor code cleanup following Gavin's review.
Changes since v6:
* Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an RMM which uses 4k granules.
Changes since v5:
* Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
* Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
* Improved commit description.
* Improved return failures for rmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.

---
  arch/arm64/include/asm/kvm_emulate.h |   5 +
  arch/arm64/include/asm/kvm_rme.h     |  19 ++
  arch/arm64/kvm/arm.c                 |  16 ++
  arch/arm64/kvm/mmu.c                 |  22 +-
  arch/arm64/kvm/rme.c                 | 319 +++++++++++++++++++++++++++
  5 files changed, 379 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +5 -0
index 1c43a4fc25dd..4ee6c215da82 100644
@@ -699,6 +699,11 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)
  	return READ_ONCE(kvm->arch.realm.state);
  }
 
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
+}
+
  static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
  {
  	return false;
arch/arm64/include/asm/kvm_rme.h +19 -0
index 9c8a0b23e0e4..5dc1915de891 100644
@@ -6,6 +6,8 @@
  #ifndef __ASM_KVM_RME_H
  #define __ASM_KVM_RME_H
 
+#include <uapi/linux/kvm.h>
+
  /**
   * enum realm_state - State of a Realm
   */
@@ -46,11 +48,28 @@ enum realm_state {
   * struct realm - Additional per VM data for a Realm
   *
   * @state: The lifetime state machine for the realm
+ * @rd: Kernel mapping of the Realm Descriptor (RD)
+ * @params: Parameters for the RMI_REALM_CREATE command
+ * @num_aux: The number of auxiliary pages required by the RMM
+ * @vmid: VMID to be used by the RMM for the realm
+ * @ia_bits: Number of valid Input Address bits in the IPA
   */
  struct realm {
  	enum realm_state state;
+
+	void *rd;
+	struct realm_params *params;
+
+	unsigned long num_aux;
+	unsigned int vmid;
+	unsigned int ia_bits;
  };
 
  void kvm_init_rme(void);
+u32 kvm_realm_ipa_limit(void);
+
+int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
+int kvm_init_realm_vm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);
 
  #endif /* __ASM_KVM_RME_H */
arch/arm64/kvm/arm.c +16 -0
index 856a721d41ac..0e8482fdc4d3 100644
@@ -136,6 +136,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		}
  		mutex_unlock(&kvm->lock);
  		break;
+	case KVM_CAP_ARM_RME:
+		mutex_lock(&kvm->lock);
+		r = kvm_realm_enable_cap(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
  	default:
  		break;
  	}
@@ -198,6 +203,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
  	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	/* Initialise the realm bits after the generic bits are enabled */
+	if (kvm_is_realm(kvm)) {
+		ret = kvm_init_realm_vm(kvm);
+		if (ret)
+			goto err_free_cpumask;
+	}
+
  	return 0;
 
  err_free_cpumask:
@@ -257,6 +269,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
  	kvm_unshare_hyp(kvm, kvm + 1);
 
  	kvm_arm_teardown_hypercalls(kvm);
+	kvm_destroy_realm(kvm);
  }
 
  static bool kvm_has_full_ptr_auth(void)
@@ -405,6 +418,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  	case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
  		r = BIT(0);
  		break;
+	case KVM_CAP_ARM_RME:
+		r = static_key_enabled(&kvm_rme_is_available);
+		break;
  	default:
  		r = 0;
  	}
arch/arm64/kvm/mmu.c +20 -2
index 2feb6c6b63af..5957a07de86d 100644
@@ -876,12 +876,16 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
  	.icache_inval_pou	= invalidate_icache_guest_page,
  };
 
-static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
+static int kvm_init_ipa_range(struct kvm *kvm,
+			      struct kvm_s2_mmu *mmu, unsigned long type)
  {
  	u32 kvm_ipa_limit = get_kvm_ipa_limit();
  	u64 mmfr0, mmfr1;
  	u32 phys_shift;
 
+	if (kvm_is_realm(kvm))
+		kvm_ipa_limit = kvm_realm_ipa_limit();
+
  	if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
  		return -EINVAL;
 
@@ -946,7 +950,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
  		return -EINVAL;
  	}
 
-	err = kvm_init_ipa_range(mmu, type);
+	err = kvm_init_ipa_range(kvm, mmu, type);
  	if (err)
  		return err;
 
@@ -1072,6 +1076,20 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
  	struct kvm_pgtable *pgt = NULL;
 
  	write_lock(&kvm->mmu_lock);
+	if (kvm_is_realm(kvm) &&
+	    (kvm_realm_state(kvm) != REALM_STATE_DEAD &&
+	     kvm_realm_state(kvm) != REALM_STATE_NONE)) {
+		/* Tearing down RTTs will be added in a later patch */
+		write_unlock(&kvm->mmu_lock);
+
+		/*
+		 * The physical PGD pages are delegated to the RMM, so cannot
+		 * be freed at this point. This function will be called again
+		 * from kvm_destroy_realm() after the physical pages have been
+		 * returned at which point the memory can be freed.
+		 */
+		return;
+	}
  	pgt = mmu->pgt;
  	if (pgt) {
  		mmu->pgd_phys = 0;
arch/arm64/kvm/rme.c +14 -0
index 67cf2d94cb2d..dbb6521fe380 100644
@@ -5,9 +5,23 @@
 
  #include <linux/kvm_host.h>
 
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
  #include <asm/rmi_cmds.h>
  #include <asm/virt.h>
 
+#include <asm/kvm_pgtable.h>
+
+static unsigned long rmm_feat_reg0;
+
+#define RMM_PAGE_SHIFT		12
+#define RMM_PAGE_SIZE		BIT(RMM_PAGE_SHIFT)
+
+static bool rme_has_feature(unsigned long feature)
+{
+	return !!u64_get_bits(rmm_feat_reg0, feature);
+}
+
  static int rmi_check_version(void)
  {
  	struct arm_smccc_res res;
@@ -42,6 +56,305 @@ static int rmi_check_version(void)
  	return 0;

arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 66

Quoted 10 lines
arch/arm64/kvm/rme.c +8 -0
  }
 
+u32 kvm_realm_ipa_limit(void)
+{
+	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
+}
+
+static int get_start_level(struct realm *realm)
+{
+	return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));

minor nit: It may be worth adding a comment here, how we got this magic number 8. We could say:

Open coded version of ARM64_HW_PGTABLE_LEVELS(ia_bits - 4) (accounting for the concatenation of upto 16 tables in entry level) for RMM Stage2.

Quoted 9 lines
arch/arm64/kvm/rme.c +9 -0
+}
+
+static void free_delegated_granule(phys_addr_t phys)
+{
+	if (WARN_ON(rmi_granule_undelegate(phys))) {
+		/* Undelegate failed: leak the page */
+		return;
+	}
+

arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 85

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
+
+	free_page((unsigned long)phys_to_virt(phys));
+}
+
+/* Calculate the number of s2 root rtts needed */
+static int realm_num_root_rtts(struct realm *realm)
+{
+	unsigned int ipa_bits = realm->ia_bits;
+	unsigned int levels = 3 - get_start_level(realm);

nit: Why is this 3 - start_level and not 4 ? Though that is compensated by the "levels + 1" below, hence the calculation is correct.

Quoted 70 lines
arch/arm64/kvm/rme.c +70 -0
+	unsigned int sl_ipa_bits = (levels + 1) * (RMM_PAGE_SHIFT - 3) +
+				   RMM_PAGE_SHIFT;
+
+	if (sl_ipa_bits >= ipa_bits)
+		return 1;
+
+	return 1 << (ipa_bits - sl_ipa_bits);
+}
+
+static int realm_create_rd(struct kvm *kvm)
+{
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_params *params = realm->params;
+	void *rd = NULL;
+	phys_addr_t rd_phys, params_phys;
+	size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
+	int i, r;
+	int rtt_num_start;
+
+	realm->ia_bits = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
+	rtt_num_start = realm_num_root_rtts(realm);
+
+	if (WARN_ON(realm->rd || !realm->params))
+		return -EEXIST;
+
+	if (pgd_size / RMM_PAGE_SIZE < rtt_num_start)
+		return -EINVAL;
+
+	rd = (void *)__get_free_page(GFP_KERNEL);
+	if (!rd)
+		return -ENOMEM;
+
+	rd_phys = virt_to_phys(rd);
+	if (rmi_granule_delegate(rd_phys)) {
+		r = -ENXIO;
+		goto free_rd;
+	}
+
+	for (i = 0; i < pgd_size; i += RMM_PAGE_SIZE) {
+		phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i;
+
+		if (rmi_granule_delegate(pgd_phys)) {
+			r = -ENXIO;
+			goto out_undelegate_tables;
+		}
+	}
+
+	params->s2sz = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
+	params->rtt_level_start = get_start_level(realm);
+	params->rtt_num_start = rtt_num_start;
+	params->rtt_base = kvm->arch.mmu.pgd_phys;
+	params->vmid = realm->vmid;
+
+	params_phys = virt_to_phys(params);
+
+	if (rmi_realm_create(rd_phys, params_phys)) {
+		r = -ENXIO;
+		goto out_undelegate_tables;
+	}
+
+	if (WARN_ON(rmi_rec_aux_count(rd_phys, &realm->num_aux))) {
+		WARN_ON(rmi_realm_destroy(rd_phys));
+		goto out_undelegate_tables;
+	}
+
+	realm->rd = rd;
+
+	return 0;
+
+out_undelegate_tables:

arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 165

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+	while (i > 0) {
+		i -= RMM_PAGE_SIZE;
+
+		phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i;
+
+		if (WARN_ON(rmi_granule_undelegate(pgd_phys))) {
+			/* Leak the pages if they cannot be returned */
+			kvm->arch.mmu.pgt = NULL;
+			break;
+		}

minor nit: Do we need to try undelegating the other pages ? We could make that WARN_ON_ONCE() too.

Suzuki

#19 lore raw

Re: [PATCH v8 07/43] arm64: RME: ioctls to create and configure realms

Quoted 259 lines · Suzuki K Poulose
Quoted 244 lines · Steven Price

Add the KVM_CAP_ARM_RME_CREATE_RD ioctl to create a realm. This involves delegating pages to the RMM to hold the Realm Descriptor (RD) and for the base level of the Realm Translation Tables (RTT). A VMID also need to be picked, since the RMM has a separate VMID address space a dedicated allocator is added for this purpose.

KVM_CAP_ARM_RME_CONFIG_REALM is provided to allow configuring the realm before it is created. Configuration options can be classified as:

  1. Parameters specific to the Realm stage2 (e.g. IPA Size, vmid, stage2 entry level, entry level RTTs, number of RTTs in start level, LPA2) Most of these are not measured by RMM and comes from KVM book keeping.

  2. Parameters controlling "Arm Architecture features for the VM". (e.g. SVE VL, PMU counters, number of HW BRPs/WPs), configured by the VMM using the "user ID register write" mechanism. These will be supported in the later patches.

  3. Parameters are not part of the core Arm architecture but defined by the RMM spec (e.g. Hash algorithm for measurement, Personalisation value). These are programmed via KVM_CAP_ARM_RME_CONFIG_REALM.

For the IPA size there is the possibility that the RMM supports a different size to the IPA size supported by KVM for normal guests. At the moment the 'normal limit' is exposed by KVM_CAP_ARM_VM_IPA_SIZE and the IPA size is configured by the bottom bits of vm_type in KVM_CREATE_VM. This means that it isn't easy for the VMM to discover what IPA sizes are supported for Realm guests. Since the IPA is part of the measurement of the realm guest the current expectation is that the VMM will be required to pick the IPA size demanded by attestation and therefore simply failing if this isn't available is fine. An option would be to expose a new capability ioctl to obtain the RMM's maximum IPA size if this is needed in the future.

---
Changes since v7:
  * Minor code cleanup following Gavin's review.
Changes since v6:
  * Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an RMM which uses 4k granules.
Changes since v5:
  * Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
  * Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
  * Improved commit description.
  * Improved return failures for rmi_check_version().
  * Clear contents of PGD after it has been undelegated in case the RMM left stale data.
  * Minor changes to reflect changes in previous patches.
---
  arch/arm64/include/asm/kvm_emulate.h |   5 + arch/arm64/include/asm/kvm_rme.h     |  19 ++

  arch/arm64/kvm/arm.c                 |  16 ++
  arch/arm64/kvm/mmu.c                 |  22 +-
  arch/arm64/kvm/rme.c                 | 319 +++++++++++++++++++++++++++
  5 files changed, 379 insertions(+), 2 deletions(-)
arch/arm64/ renamed from arch/arm64/include/asm/kvm_emulate.h +0 -0

include/asm/kvm_emulate.h
index 1c43a4fc25dd..4ee6c215da82 100644

arch/arm64/include/asm/kvm_emulate.h +4 -0
@@ -699,6 +699,11 @@ static inline enum realm_state
 kvm_realm_state(struct kvm *kvm)
       return READ_ONCE(kvm->arch.realm.state);
   }
   +static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+    return kvm_is_realm(kvm) && kvm_realm_state(kvm) !=
 REALM_STATE_NONE;
+}
+
   static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)

  {
      return false;

arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index 9c8a0b23e0e4..5dc1915de891 100644

arch/arm64/include/asm/kvm_rme.h +0 -0
@@ -6,6 +6,8 @@
   #ifndef __ASM_KVM_RME_H
   #define __ASM_KVM_RME_H

  +#include <uapi/linux/kvm.h>
+
  /**
   * enum realm_state - State of a Realm
   */

arch/arm64/include/asm/kvm_rme.h +17 -0
@@ -46,11 +48,28 @@ enum realm_state {
    * struct realm - Additional per VM data for a Realm
    *
    * @state: The lifetime state machine for the realm
+ * @rd: Kernel mapping of the Realm Descriptor (RD)
+ * @params: Parameters for the RMI_REALM_CREATE command
+ * @num_aux: The number of auxiliary pages required by the RMM
+ * @vmid: VMID to be used by the RMM for the realm
+ * @ia_bits: Number of valid Input Address bits in the IPA
    */
   struct realm {
       enum realm_state state;
+
+    void *rd;
+    struct realm_params *params;
+
+    unsigned long num_aux;
+    unsigned int vmid;
+    unsigned int ia_bits;
   };
     void kvm_init_rme(void);
+u32 kvm_realm_ipa_limit(void);
+
+int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
+int kvm_init_realm_vm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);
     #endif /* __ASM_KVM_RME_H */
arch/arm64/kvm/arm.c +5 -0
index 856a721d41ac..0e8482fdc4d3 100644
@@ -136,6 +136,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
           }
           mutex_unlock(&kvm->lock);
           break;
+    case KVM_CAP_ARM_RME:
+        mutex_lock(&kvm->lock);
+        r = kvm_realm_enable_cap(kvm, cap);
+        mutex_unlock(&kvm->lock);
+        break;

      default:
          break;
      }

arch/arm64/kvm/arm.c +0 -0
@@ -198,6 +203,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned
 long type)
         bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);

  +    /* Initialise the realm bits after the generic bits are enabled */ +    if (kvm_is_realm(kvm)) {
+        ret = kvm_init_realm_vm(kvm);
+        if (ret)
+            goto err_free_cpumask;
+    }
+
      return 0;
    err_free_cpumask:

arch/arm64/kvm/arm.c +1 -0
@@ -257,6 +269,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
       kvm_unshare_hyp(kvm, kvm + 1);
         kvm_arm_teardown_hypercalls(kvm);
+    kvm_destroy_realm(kvm);
   }
     static bool kvm_has_full_ptr_auth(void)
@@ -405,6 +418,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm,
 long ext)

      case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
          r = BIT(0);
          break;
+    case KVM_CAP_ARM_RME:
+        r = static_key_enabled(&kvm_rme_is_available); +        break;
      default:
          r = 0;
      }

arch/arm64/kvm/mmu.c +0 -0
index 2feb6c6b63af..5957a07de86d 100644
@@ -876,12 +876,16 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
       .icache_inval_pou    = invalidate_icache_guest_page,
   };

  -static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
+static int kvm_init_ipa_range(struct kvm *kvm,
+                  struct kvm_s2_mmu *mmu, unsigned long type) {
      u32 kvm_ipa_limit = get_kvm_ipa_limit();
      u64 mmfr0, mmfr1;
      u32 phys_shift;
  +    if (kvm_is_realm(kvm))
+        kvm_ipa_limit = kvm_realm_ipa_limit();
+
      if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
          return -EINVAL;
  @@ -946,7 +950,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
          return -EINVAL;
      }
  -    err = kvm_init_ipa_range(mmu, type);
+    err = kvm_init_ipa_range(kvm, mmu, type);
      if (err)
          return err;
  @@ -1072,6 +1076,20 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) struct kvm_pgtable *pgt = NULL; write_lock(&kvm->mmu_lock);
+    if (kvm_is_realm(kvm) &&
+        (kvm_realm_state(kvm) != REALM_STATE_DEAD &&
+         kvm_realm_state(kvm) != REALM_STATE_NONE)) {
+        /* Tearing down RTTs will be added in a later patch */ +        write_unlock(&kvm->mmu_lock);
+
+        /*
+         * The physical PGD pages are delegated to the RMM, so cannot +         * be freed at this point. This function will be called again +         * from kvm_destroy_realm() after the physical pages have been +         * returned at which point the memory can be freed. +         */
+        return;
+    }
      pgt = mmu->pgt;
      if (pgt) {
          mmu->pgd_phys = 0;

arch/arm64/kvm/rme.c +0 -0
index 67cf2d94cb2d..dbb6521fe380 100644
@@ -5,9 +5,23 @@
     #include <linux/kvm_host.h>

  +#include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
  #include <asm/rmi_cmds.h>
  #include <asm/virt.h>
  +#include <asm/kvm_pgtable.h>

arch/arm64/kvm/rme.c +11 -0
+
+static unsigned long rmm_feat_reg0;
+
+#define RMM_PAGE_SHIFT        12
+#define RMM_PAGE_SIZE        BIT(RMM_PAGE_SHIFT)
+
+static bool rme_has_feature(unsigned long feature)
+{
+    return !!u64_get_bits(rmm_feat_reg0, feature);
+}
+

  static int rmi_check_version(void)
  {
      struct arm_smccc_res res;

arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 65

Quoted 10 lines
arch/arm64/kvm/rme.c +7 -0
@@ -42,6 +56,305 @@ static int rmi_check_version(void)
       return 0;
   }
   +u32 kvm_realm_ipa_limit(void)
+{
+    return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
+}
+
+static int get_start_level(struct realm *realm)
+{
+    return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));

minor nit: It may be worth adding a comment here, how we got this magic number 8. We could say:

Quoted 3 lines

Open coded version of ARM64_HW_PGTABLE_LEVELS(ia_bits - 4) (accounting for the concatenation of upto 16 tables in entry level) for RMM Stage2.

Agreed a comment makes sense. This is actually an open coded version of "4 - stage2_pgtable_levels()" but using the RMM's page size (indeed previous versions of the series did exactly that). There's already a comment for stage2_pgtable_levels() explaining the concatenation.

Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/rme.c +14 -0
+}
+
+static void free_delegated_granule(phys_addr_t phys)
+{
+    if (WARN_ON(rmi_granule_undelegate(phys))) {
+        /* Undelegate failed: leak the page */
+        return;
+    }
+
+    kvm_account_pgtable_pages(phys_to_virt(phys), -1);
+
+    free_page((unsigned long)phys_to_virt(phys));
+}
+
Quoted 8 lines

arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 85

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+/* Calculate the number of s2 root rtts needed */
+static int realm_num_root_rtts(struct realm *realm)
+{
+    unsigned int ipa_bits = realm->ia_bits;
+    unsigned int levels = 3 - get_start_level(realm);

nit: Why is this 3 - start_level and not 4 ? Though that is compensated by the "levels + 1" below, hence the calculation is correct.

I honestly can't remember - will change ;)

Quoted 75 lines
Quoted 75 lines
arch/arm64/kvm/rme.c +75 -0
+    unsigned int sl_ipa_bits = (levels + 1) * (RMM_PAGE_SHIFT - 3) +
+                   RMM_PAGE_SHIFT;
+
+    if (sl_ipa_bits >= ipa_bits)
+        return 1;
+
+    return 1 << (ipa_bits - sl_ipa_bits);
+}
+
+static int realm_create_rd(struct kvm *kvm)
+{
+    struct realm *realm = &kvm->arch.realm;
+    struct realm_params *params = realm->params;
+    void *rd = NULL;
+    phys_addr_t rd_phys, params_phys;
+    size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
+    int i, r;
+    int rtt_num_start;
+
+    realm->ia_bits = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
+    rtt_num_start = realm_num_root_rtts(realm);
+
+    if (WARN_ON(realm->rd || !realm->params))
+        return -EEXIST;
+
+    if (pgd_size / RMM_PAGE_SIZE < rtt_num_start)
+        return -EINVAL;
+
+    rd = (void *)__get_free_page(GFP_KERNEL);
+    if (!rd)
+        return -ENOMEM;
+
+    rd_phys = virt_to_phys(rd);
+    if (rmi_granule_delegate(rd_phys)) {
+        r = -ENXIO;
+        goto free_rd;
+    }
+
+    for (i = 0; i < pgd_size; i += RMM_PAGE_SIZE) {
+        phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i;
+
+        if (rmi_granule_delegate(pgd_phys)) {
+            r = -ENXIO;
+            goto out_undelegate_tables;
+        }
+    }
+
+    params->s2sz = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
+    params->rtt_level_start = get_start_level(realm);
+    params->rtt_num_start = rtt_num_start;
+    params->rtt_base = kvm->arch.mmu.pgd_phys;
+    params->vmid = realm->vmid;
+
+    params_phys = virt_to_phys(params);
+
+    if (rmi_realm_create(rd_phys, params_phys)) {
+        r = -ENXIO;
+        goto out_undelegate_tables;
+    }
+
+    if (WARN_ON(rmi_rec_aux_count(rd_phys, &realm->num_aux))) {
+        WARN_ON(rmi_realm_destroy(rd_phys));
+        goto out_undelegate_tables;
+    }
+
+    realm->rd = rd;
+
+    return 0;
+
+out_undelegate_tables:
+    while (i > 0) {
+        i -= RMM_PAGE_SIZE;
+
+        phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i;
+
Quoted 8 lines

arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 165

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+        if (WARN_ON(rmi_granule_undelegate(pgd_phys))) {
+            /* Leak the pages if they cannot be returned */
+            kvm->arch.mmu.pgt = NULL;
+            break;
+        }

minor nit: Do we need to try undelegating the other pages ? We could make that WARN_ON_ONCE() too.

We could do, but there's no real point unless we also deal with leaking only the pages which cannot be undelegated. Considering this is a "should never happen" case I felt it was better to keep the code simple and leak more than necessary. To me this feels like a "save everything and reboot as soon as possible" sort of error condition - the kernel (or RMM) has lost track of which pages are delegated and so a GPT fault could be coming at any point. So it's better to play safe and be pessimistic.

Thanks,
Steve

#20 lore raw

Re: [PATCH v8 07/43] arm64: RME: ioctls to create and configure realms

In reply to Steven Price
Quoted 407 lines · Steven Price

Add the KVM_CAP_ARM_RME_CREATE_RD ioctl to create a realm. This involves delegating pages to the RMM to hold the Realm Descriptor (RD) and for the base level of the Realm Translation Tables (RTT). A VMID also need to be picked, since the RMM has a separate VMID address space a dedicated allocator is added for this purpose.

KVM_CAP_ARM_RME_CONFIG_REALM is provided to allow configuring the realm before it is created. Configuration options can be classified as:

1. Parameters specific to the Realm stage2 (e.g. IPA Size, vmid, stage2 entry level, entry level RTTs, number of RTTs in start level, LPA2) Most of these are not measured by RMM and comes from KVM book keeping.

2. Parameters controlling "Arm Architecture features for the VM". (e.g. SVE VL, PMU counters, number of HW BRPs/WPs), configured by the VMM using the "user ID register write" mechanism. These will be supported in the later patches.

3. Parameters are not part of the core Arm architecture but defined by the RMM spec (e.g. Hash algorithm for measurement, Personalisation value). These are programmed via KVM_CAP_ARM_RME_CONFIG_REALM.

For the IPA size there is the possibility that the RMM supports a different size to the IPA size supported by KVM for normal guests. At the moment the 'normal limit' is exposed by KVM_CAP_ARM_VM_IPA_SIZE and the IPA size is configured by the bottom bits of vm_type in KVM_CREATE_VM. This means that it isn't easy for the VMM to discover what IPA sizes are supported for Realm guests. Since the IPA is part of the measurement of the realm guest the current expectation is that the VMM will be required to pick the IPA size demanded by attestation and therefore simply failing if this isn't available is fine. An option would be to expose a new capability ioctl to obtain the RMM's maximum IPA size if this is needed in the future.

---
Changes since v7:
* Minor code cleanup following Gavin's review.
Changes since v6:
* Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an RMM which uses 4k granules.
Changes since v5:
* Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
* Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
* Improved commit description.
* Improved return failures for rmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.

---
  arch/arm64/include/asm/kvm_emulate.h |   5 +
  arch/arm64/include/asm/kvm_rme.h     |  19 ++
  arch/arm64/kvm/arm.c                 |  16 ++
  arch/arm64/kvm/mmu.c                 |  22 +-
  arch/arm64/kvm/rme.c                 | 319 +++++++++++++++++++++++++++
  5 files changed, 379 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +5 -0
index 1c43a4fc25dd..4ee6c215da82 100644
@@ -699,6 +699,11 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)
  	return READ_ONCE(kvm->arch.realm.state);
  }
 
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
+}
+
  static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
  {
  	return false;
arch/arm64/include/asm/kvm_rme.h +19 -0
index 9c8a0b23e0e4..5dc1915de891 100644
@@ -6,6 +6,8 @@
  #ifndef __ASM_KVM_RME_H
  #define __ASM_KVM_RME_H
 
+#include <uapi/linux/kvm.h>
+
  /**
   * enum realm_state - State of a Realm
   */
@@ -46,11 +48,28 @@ enum realm_state {
   * struct realm - Additional per VM data for a Realm
   *
   * @state: The lifetime state machine for the realm
+ * @rd: Kernel mapping of the Realm Descriptor (RD)
+ * @params: Parameters for the RMI_REALM_CREATE command
+ * @num_aux: The number of auxiliary pages required by the RMM
+ * @vmid: VMID to be used by the RMM for the realm
+ * @ia_bits: Number of valid Input Address bits in the IPA
   */
  struct realm {
  	enum realm_state state;
+
+	void *rd;
+	struct realm_params *params;
+
+	unsigned long num_aux;
+	unsigned int vmid;
+	unsigned int ia_bits;
  };
 
  void kvm_init_rme(void);
+u32 kvm_realm_ipa_limit(void);
+
+int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
+int kvm_init_realm_vm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);
 
  #endif /* __ASM_KVM_RME_H */
arch/arm64/kvm/arm.c +16 -0
index 856a721d41ac..0e8482fdc4d3 100644
@@ -136,6 +136,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		}
  		mutex_unlock(&kvm->lock);
  		break;
+	case KVM_CAP_ARM_RME:
+		mutex_lock(&kvm->lock);
+		r = kvm_realm_enable_cap(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
  	default:
  		break;
  	}
@@ -198,6 +203,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
  	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	/* Initialise the realm bits after the generic bits are enabled */
+	if (kvm_is_realm(kvm)) {
+		ret = kvm_init_realm_vm(kvm);
+		if (ret)
+			goto err_free_cpumask;
+	}
+
  	return 0;
 
  err_free_cpumask:
@@ -257,6 +269,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
  	kvm_unshare_hyp(kvm, kvm + 1);
 
  	kvm_arm_teardown_hypercalls(kvm);
+	kvm_destroy_realm(kvm);
  }
 
  static bool kvm_has_full_ptr_auth(void)
@@ -405,6 +418,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  	case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
  		r = BIT(0);
  		break;
+	case KVM_CAP_ARM_RME:
+		r = static_key_enabled(&kvm_rme_is_available);
+		break;
  	default:
  		r = 0;
  	}
arch/arm64/kvm/mmu.c +20 -2
index 2feb6c6b63af..5957a07de86d 100644
@@ -876,12 +876,16 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
  	.icache_inval_pou	= invalidate_icache_guest_page,
  };
 
-static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
+static int kvm_init_ipa_range(struct kvm *kvm,
+			      struct kvm_s2_mmu *mmu, unsigned long type)
  {
  	u32 kvm_ipa_limit = get_kvm_ipa_limit();
  	u64 mmfr0, mmfr1;
  	u32 phys_shift;
 
+	if (kvm_is_realm(kvm))
+		kvm_ipa_limit = kvm_realm_ipa_limit();
+
  	if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
  		return -EINVAL;
 
@@ -946,7 +950,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
  		return -EINVAL;
  	}
 
-	err = kvm_init_ipa_range(mmu, type);
+	err = kvm_init_ipa_range(kvm, mmu, type);
  	if (err)
  		return err;
 
@@ -1072,6 +1076,20 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
  	struct kvm_pgtable *pgt = NULL;
 
  	write_lock(&kvm->mmu_lock);
+	if (kvm_is_realm(kvm) &&
+	    (kvm_realm_state(kvm) != REALM_STATE_DEAD &&
+	     kvm_realm_state(kvm) != REALM_STATE_NONE)) {
+		/* Tearing down RTTs will be added in a later patch */
+		write_unlock(&kvm->mmu_lock);
+
+		/*
+		 * The physical PGD pages are delegated to the RMM, so cannot
+		 * be freed at this point. This function will be called again
+		 * from kvm_destroy_realm() after the physical pages have been
+		 * returned at which point the memory can be freed.
+		 */
+		return;
+	}
  	pgt = mmu->pgt;
  	if (pgt) {
  		mmu->pgd_phys = 0;
arch/arm64/kvm/rme.c +164 -0
index 67cf2d94cb2d..dbb6521fe380 100644
@@ -5,9 +5,23 @@
 
  #include <linux/kvm_host.h>
 
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
  #include <asm/rmi_cmds.h>
  #include <asm/virt.h>
 
+#include <asm/kvm_pgtable.h>
+
+static unsigned long rmm_feat_reg0;
+
+#define RMM_PAGE_SHIFT		12
+#define RMM_PAGE_SIZE		BIT(RMM_PAGE_SHIFT)
+
+static bool rme_has_feature(unsigned long feature)
+{
+	return !!u64_get_bits(rmm_feat_reg0, feature);
+}
+
  static int rmi_check_version(void)
  {
  	struct arm_smccc_res res;
@@ -42,6 +56,305 @@ static int rmi_check_version(void)
  	return 0;
  }
 
+u32 kvm_realm_ipa_limit(void)
+{
+	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
+}
+
+static int get_start_level(struct realm *realm)
+{
+	return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
+}
+
+static void free_delegated_granule(phys_addr_t phys)
+{
+	if (WARN_ON(rmi_granule_undelegate(phys))) {
+		/* Undelegate failed: leak the page */
+		return;
+	}
+
+	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
+
+	free_page((unsigned long)phys_to_virt(phys));
+}
+
+/* Calculate the number of s2 root rtts needed */
+static int realm_num_root_rtts(struct realm *realm)
+{
+	unsigned int ipa_bits = realm->ia_bits;
+	unsigned int levels = 3 - get_start_level(realm);
+	unsigned int sl_ipa_bits = (levels + 1) * (RMM_PAGE_SHIFT - 3) +
+				   RMM_PAGE_SHIFT;
+
+	if (sl_ipa_bits >= ipa_bits)
+		return 1;
+
+	return 1 << (ipa_bits - sl_ipa_bits);
+}
+
+static int realm_create_rd(struct kvm *kvm)
+{
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_params *params = realm->params;
+	void *rd = NULL;
+	phys_addr_t rd_phys, params_phys;
+	size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
+	int i, r;
+	int rtt_num_start;
+
+	realm->ia_bits = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
+	rtt_num_start = realm_num_root_rtts(realm);
+
+	if (WARN_ON(realm->rd || !realm->params))
+		return -EEXIST;
+
+	if (pgd_size / RMM_PAGE_SIZE < rtt_num_start)
+		return -EINVAL;
+
+	rd = (void *)__get_free_page(GFP_KERNEL);
+	if (!rd)
+		return -ENOMEM;
+
+	rd_phys = virt_to_phys(rd);
+	if (rmi_granule_delegate(rd_phys)) {
+		r = -ENXIO;
+		goto free_rd;
+	}
+
+	for (i = 0; i < pgd_size; i += RMM_PAGE_SIZE) {
+		phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i;
+
+		if (rmi_granule_delegate(pgd_phys)) {
+			r = -ENXIO;
+			goto out_undelegate_tables;
+		}
+	}
+
+	params->s2sz = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
+	params->rtt_level_start = get_start_level(realm);
+	params->rtt_num_start = rtt_num_start;
+	params->rtt_base = kvm->arch.mmu.pgd_phys;
+	params->vmid = realm->vmid;
+
+	params_phys = virt_to_phys(params);
+
+	if (rmi_realm_create(rd_phys, params_phys)) {
+		r = -ENXIO;
+		goto out_undelegate_tables;
+	}
+
+	if (WARN_ON(rmi_rec_aux_count(rd_phys, &realm->num_aux))) {
+		WARN_ON(rmi_realm_destroy(rd_phys));
+		goto out_undelegate_tables;
+	}
+
+	realm->rd = rd;
+
+	return 0;
+
+out_undelegate_tables:
+	while (i > 0) {
+		i -= RMM_PAGE_SIZE;
+
+		phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i;
+
+		if (WARN_ON(rmi_granule_undelegate(pgd_phys))) {
+			/* Leak the pages if they cannot be returned */
+			kvm->arch.mmu.pgt = NULL;
+			break;
+		}
+	}
+	if (WARN_ON(rmi_granule_undelegate(rd_phys))) {
+		/* Leak the page if it isn't returned */
+		return r;
+	}
+free_rd:
+	free_page((unsigned long)rd);
+	return r;
+}
+
+/* Protects access to rme_vmid_bitmap */
+static DEFINE_SPINLOCK(rme_vmid_lock);
+static unsigned long *rme_vmid_bitmap;
+
+static int rme_vmid_init(void)
+{
+	unsigned int vmid_count = 1 << kvm_get_vmid_bits();
+
+	rme_vmid_bitmap = bitmap_zalloc(vmid_count, GFP_KERNEL);
+	if (!rme_vmid_bitmap) {
+		kvm_err("%s: Couldn't allocate rme vmid bitmap\n", __func__);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static int rme_vmid_reserve(void)
+{
+	int ret;
+	unsigned int vmid_count = 1 << kvm_get_vmid_bits();
+
+	spin_lock(&rme_vmid_lock);
+	ret = bitmap_find_free_region(rme_vmid_bitmap, vmid_count, 0);
+	spin_unlock(&rme_vmid_lock);
+
+	return ret;
+}
+
+static void rme_vmid_release(unsigned int vmid)
+{
+	spin_lock(&rme_vmid_lock);
+	bitmap_release_region(rme_vmid_bitmap, vmid, 0);

arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 218

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+	spin_unlock(&rme_vmid_lock);
+}
+
+static int kvm_create_realm(struct kvm *kvm)
+{
+	struct realm *realm = &kvm->arch.realm;
+	int ret;
+
+	if (!kvm_is_realm(kvm))
+		return -EINVAL;

This check is duplicate because the only caller kvm_realm_enable_cap() already has the check. So it can be dropped.

Quoted 144 lines
arch/arm64/kvm/rme.c +139 -0
+	if (kvm_realm_is_created(kvm))
+		return -EEXIST;
+
+	ret = rme_vmid_reserve();
+	if (ret < 0)
+		return ret;
+	realm->vmid = ret;
+
+	ret = realm_create_rd(kvm);
+	if (ret) {
+		rme_vmid_release(realm->vmid);
+		return ret;
+	}
+
+	WRITE_ONCE(realm->state, REALM_STATE_NEW);
+
+	/* The realm is up, free the parameters.  */
+	free_page((unsigned long)realm->params);
+	realm->params = NULL;
+
+	return 0;
+}
+
+static int config_realm_hash_algo(struct realm *realm,
+				  struct arm_rme_config *cfg)
+{
+	switch (cfg->hash_algo) {
+	case ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA256:
+		if (!rme_has_feature(RMI_FEATURE_REGISTER_0_HASH_SHA_256))
+			return -EINVAL;
+		break;
+	case ARM_RME_CONFIG_MEASUREMENT_ALGO_SHA512:
+		if (!rme_has_feature(RMI_FEATURE_REGISTER_0_HASH_SHA_512))
+			return -EINVAL;
+		break;
+	default:
+		return -EINVAL;
+	}
+	realm->params->hash_algo = cfg->hash_algo;
+	return 0;
+}
+
+static int kvm_rme_config_realm(struct kvm *kvm, struct kvm_enable_cap *cap)
+{
+	struct arm_rme_config cfg;
+	struct realm *realm = &kvm->arch.realm;
+	int r = 0;
+
+	if (kvm_realm_is_created(kvm))
+		return -EBUSY;
+
+	if (copy_from_user(&cfg, (void __user *)cap->args[1], sizeof(cfg)))
+		return -EFAULT;
+
+	switch (cfg.cfg) {
+	case ARM_RME_CONFIG_RPV:
+		memcpy(&realm->params->rpv, &cfg.rpv, sizeof(cfg.rpv));
+		break;
+	case ARM_RME_CONFIG_HASH_ALGO:
+		r = config_realm_hash_algo(realm, &cfg);
+		break;
+	default:
+		r = -EINVAL;
+	}
+
+	return r;
+}
+
+int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
+{
+	int r = 0;
+
+	if (!kvm_is_realm(kvm))
+		return -EINVAL;
+
+	switch (cap->args[0]) {
+	case KVM_CAP_ARM_RME_CONFIG_REALM:
+		r = kvm_rme_config_realm(kvm, cap);
+		break;
+	case KVM_CAP_ARM_RME_CREATE_REALM:
+		r = kvm_create_realm(kvm);
+		break;
+	default:
+		r = -EINVAL;
+		break;
+	}
+
+	return r;
+}
+
+void kvm_destroy_realm(struct kvm *kvm)
+{
+	struct realm *realm = &kvm->arch.realm;
+	size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
+	int i;
+
+	if (realm->params) {
+		free_page((unsigned long)realm->params);
+		realm->params = NULL;
+	}
+
+	if (!kvm_realm_is_created(kvm))
+		return;
+
+	WRITE_ONCE(realm->state, REALM_STATE_DYING);
+
+	if (realm->rd) {
+		phys_addr_t rd_phys = virt_to_phys(realm->rd);
+
+		if (WARN_ON(rmi_realm_destroy(rd_phys)))
+			return;
+		free_delegated_granule(rd_phys);
+		realm->rd = NULL;
+	}
+
+	rme_vmid_release(realm->vmid);
+
+	for (i = 0; i < pgd_size; i += RMM_PAGE_SIZE) {
+		phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i;
+
+		if (WARN_ON(rmi_granule_undelegate(pgd_phys)))
+			return;
+	}
+
+	WRITE_ONCE(realm->state, REALM_STATE_DEAD);
+
+	/* Now that the Realm is destroyed, free the entry level RTTs */
+	kvm_free_stage2_pgd(&kvm->arch.mmu);
+}
+
+int kvm_init_realm_vm(struct kvm *kvm)
+{
+	kvm->arch.realm.params = (void *)get_zeroed_page(GFP_KERNEL);
+
+	if (!kvm->arch.realm.params)
+		return -ENOMEM;
+	return 0;
+}
+
  void kvm_init_rme(void)
  {
  	if (PAGE_SIZE != SZ_4K)
@@ -52,5 +365,11 @@ void kvm_init_rme(void)
  		/* Continue without realm support */

arch/arm64/kvm/rme.c void kvm_init_rme(void) · line 375

Quoted 10 lines
arch/arm64/kvm/rme.c +6 -0
  		return;
 
+	if (WARN_ON(rmi_features(0, &rmm_feat_reg0)))
+		return;
+
+	if (rme_vmid_init())
+		return;
+
  	/* Future patch will enable static branch kvm_rme_is_available */
  }

Thanks,
Gavin

#21 lore raw

Re: [PATCH v8 07/43] arm64: RME: ioctls to create and configure realms

Quoted 1 line · Gavin Shan

On 4/16/25 11:41 PM, Steven Price wrote:

[...]

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/rme.c +0 -0
index 67cf2d94cb2d..dbb6521fe380 100644

[...]

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/rme.c +4 -0
+
+static int kvm_create_realm(struct kvm *kvm)
+{
+    struct realm *realm = &kvm->arch.realm;
Quoted 8 lines
Quoted 4 lines

+    int ret;
+
+    if (!kvm_is_realm(kvm))
+        return -EINVAL;

This check is duplicate because the only caller kvm_realm_enable_cap() already
has the check. So it can be dropped.

Ah, good spot - thanks!

Steve

#22 lore raw

PATCH v8 8/43 kvm: arm64: Don't expose debug capabilities for realm guests

In reply to Steven Price

From: Suzuki K Poulose <[email protected]>

RMM v1.0 provides no mechanism for the host to perform debug operations on the guest. So don't expose KVM_CAP_SET_GUEST_DEBUG and report 0 breakpoints and 0 watch points.

---
Changes since v7:
* Remove the helper functions and inline the kvm_is_realm() check with a ternary operator.
* Rewrite the commit message to explain this patch.

---
 arch/arm64/kvm/arm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm64/kvm/arm.c +5 -3
index 0e8482fdc4d3..c24448966529 100644
@@ -331,7 +331,6 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
 	case KVM_CAP_ARM_NISV_TO_USER:
 	case KVM_CAP_ARM_INJECT_EXT_DABT:
-	case KVM_CAP_SET_GUEST_DEBUG:
 	case KVM_CAP_VCPU_ATTRIBUTES:
 	case KVM_CAP_PTP_KVM:
 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
@@ -340,6 +339,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
 		r = 1;
 		break;
+	case KVM_CAP_SET_GUEST_DEBUG:
+		r = !kvm_is_realm(kvm);
+		break;
 	case KVM_CAP_SET_GUEST_DEBUG2:
 		return KVM_GUESTDBG_VALID_MASK;
 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
@@ -385,10 +387,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1);
 		break;
 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
-		r = get_num_brps();
+		r = kvm_is_realm(kvm) ? 0 : get_num_brps();
 		break;
 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
-		r = get_num_wrps();
+		r = kvm_is_realm(kvm) ? 0 : get_num_wrps();
 		break;
 	case KVM_CAP_ARM_PMU_V3:
 		r = kvm_supports_guest_pmuv3();
Signature · 1 line
2.43.0
Reviewed-by on v8 8/43
#23 lore raw

Re: [PATCH v8 08/43] kvm: arm64: Don't expose debug capabilities for realm guests

Quoted 8 lines · Steven Price

From: Suzuki K Poulose <[email protected]>

RMM v1.0 provides no mechanism for the host to perform debug operations on the guest. So don't expose KVM_CAP_SET_GUEST_DEBUG and report 0 breakpoints and 0 watch points.

Quoted 9 lines

---
Changes since v7:
* Remove the helper functions and inline the kvm_is_realm() check with a ternary operator.
* Rewrite the commit message to explain this patch.

---
  arch/arm64/kvm/arm.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
#24 lore raw

PATCH v8 9/43 KVM: arm64: Allow passing machine type in KVM creation

In reply to Steven Price

Previously machine type was used purely for specifying the physical address size of the guest. Reserve the higher bits to specify an ARM specific machine type and declare a new type 'KVM_VM_TYPE_ARM_REALM' used to create a realm guest.

---
Changes since v7:
* Add some documentation explaining the new machine type.
Changes since v6:
* Make the check for kvm_rme_is_available more visible and report an error code of -EPERM (instead of -EINVAL) to make it explicit that the kernel supports RME, but the platform doesn't.

---
 Documentation/virt/kvm/api.rst | 16 ++++++++++++++--
 arch/arm64/kvm/arm.c           | 15 +++++++++++++++
 arch/arm64/kvm/mmu.c           |  3 ---
 include/uapi/linux/kvm.h       | 19 +++++++++++++++----
 4 files changed, 44 insertions(+), 9 deletions(-)
Documentation/virt/kvm/api.rst +14 -2
index 99ba6c82cf37..54ae0b98030e 100644
@@ -181,8 +181,20 @@ flag KVM_VM_MIPS_VZ.
 ARM64:
 ^^^^^^
 
-On arm64, the physical address size for a VM (IPA Size limit) is limited
-to 40bits by default. The limit can be configured if the host supports the
+On arm64, the machine type identifier is used to encode a type and the
+physical address size for the VM. The lower byte (bits[7-0]) encode the
+address size and the upper bits[11-8] encode a machine type. The machine
+types that might be available are:
+
+ ======================   ============================================
+ KVM_VM_TYPE_ARM_NORMAL   A standard VM
+ KVM_VM_TYPE_ARM_REALM    A "Realm" VM using the Arm Confidential
+                          Compute extensions, the VM's memory is
+                          protected from the host.
+ ======================   ============================================
+
+The physical address size for a VM (IPA Size limit) is limited to 40bits
+by default. The limit can be configured if the host supports the
 extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use
 KVM_VM_TYPE_ARM_IPA_SIZE(IPA_Bits) to set the size in the machine type
 identifier, where IPA_Bits is the maximum width of any physical
arch/arm64/kvm/arm.c +15 -0
index c24448966529..ba55950e6a6e 100644
@@ -172,6 +172,21 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	mutex_unlock(&kvm->lock);
 #endif
 
+	if (type & ~(KVM_VM_TYPE_ARM_MASK | KVM_VM_TYPE_ARM_IPA_SIZE_MASK))
+		return -EINVAL;
+
+	switch (type & KVM_VM_TYPE_ARM_MASK) {
+	case KVM_VM_TYPE_ARM_NORMAL:
+		break;
+	case KVM_VM_TYPE_ARM_REALM:
+		if (!static_branch_unlikely(&kvm_rme_is_available))
+			return -EPERM;
+		kvm->arch.is_realm = true;
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	kvm_init_nested(kvm);
 
 	ret = kvm_share_hyp(kvm, kvm + 1);
arch/arm64/kvm/mmu.c +0 -3
index 5957a07de86d..7531211f7e25 100644
@@ -886,9 +886,6 @@ static int kvm_init_ipa_range(struct kvm *kvm,
 	if (kvm_is_realm(kvm))
 		kvm_ipa_limit = kvm_realm_ipa_limit();
 
-	if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
-		return -EINVAL;
-
 	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
 	if (is_protected_kvm_enabled()) {
 		phys_shift = kvm_ipa_limit;
include/uapi/linux/kvm.h +15 -4
index 0b8479985581..957e4646ec99 100644
@@ -644,14 +644,25 @@ struct kvm_enable_cap {
 #define KVM_S390_SIE_PAGE_OFFSET 1
 
 /*
- * On arm64, machine type can be used to request the physical
- * address size for the VM. Bits[7-0] are reserved for the guest
- * PA size shift (i.e, log2(PA_Size)). For backward compatibility,
- * value 0 implies the default IPA size, 40bits.
+ * On arm64, machine type can be used to request both the machine type and
+ * the physical address size for the VM.
+ *
+ * Bits[11-8] are reserved for the ARM specific machine type.
+ *
+ * Bits[7-0] are reserved for the guest PA size shift (i.e, log2(PA_Size)).
+ * For backward compatibility, value 0 implies the default IPA size, 40bits.
  */
+#define KVM_VM_TYPE_ARM_SHIFT		8
+#define KVM_VM_TYPE_ARM_MASK		(0xfULL << KVM_VM_TYPE_ARM_SHIFT)
+#define KVM_VM_TYPE_ARM(_type)		\
+	(((_type) << KVM_VM_TYPE_ARM_SHIFT) & KVM_VM_TYPE_ARM_MASK)
+#define KVM_VM_TYPE_ARM_NORMAL		KVM_VM_TYPE_ARM(0)
+#define KVM_VM_TYPE_ARM_REALM		KVM_VM_TYPE_ARM(1)
+
 #define KVM_VM_TYPE_ARM_IPA_SIZE_MASK	0xffULL
 #define KVM_VM_TYPE_ARM_IPA_SIZE(x)		\
 	((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
+
 /*
  * ioctls for /dev/kvm fds:
  */
Signature · 1 line
2.43.0
Reviewed-by on v8 9/43
#25 lore raw

Re: [PATCH v8 09/43] KVM: arm64: Allow passing machine type in KVM creation

Quoted 14 lines · Steven Price

Previously machine type was used purely for specifying the physical address size of the guest. Reserve the higher bits to specify an ARM specific machine type and declare a new type 'KVM_VM_TYPE_ARM_REALM' used to create a realm guest.

---
Changes since v7:
* Add some documentation explaining the new machine type.
Changes since v6:
* Make the check for kvm_rme_is_available more visible and report an error code of -EPERM (instead of -EINVAL) to make it explicit that the kernel supports RME, but the platform doesn't.

Quoted 7 lines
---
  Documentation/virt/kvm/api.rst | 16 ++++++++++++++--
  arch/arm64/kvm/arm.c           | 15 +++++++++++++++
  arch/arm64/kvm/mmu.c           |  3 ---
  include/uapi/linux/kvm.h       | 19 +++++++++++++++----
  4 files changed, 44 insertions(+), 9 deletions(-)
#26 lore raw

PATCH v8 10/43 arm64: RME: RTT tear down

In reply to Steven Price

The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.

Creating new RTTs is the easy part, tearing down is a little more tricky. The result of realm_rtt_destroy() can be used to effectively walk the tree and destroy the entries (undelegating pages that were given to the realm).

---
Changes since v6:
* Move rme_rtt_level_mapsize() and supporting defines from kvm_rme.h into rme.c as they are only used in that file.
Changes since v5:
* Rename some RME_xxx defines to do with page sizes as RMM_xxx - they are a property of the RMM specification not the RME architecture.
Changes since v2:
* Moved {alloc,free}_delegated_page() and ensure_spare_page() to a later patch when they are actually used.
* Some simplifications now rmi_xxx() functions allow NULL as an output parameter.
* Improved comments and code layout.

---
 arch/arm64/include/asm/kvm_rme.h |   7 ++
 arch/arm64/kvm/mmu.c             |   6 +-
 arch/arm64/kvm/rme.c             | 128 +++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rme.h +7 -0
index 5dc1915de891..5f0de9a6d339 100644
@@ -71,5 +71,12 @@ u32 kvm_realm_ipa_limit(void);
 int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
 int kvm_init_realm_vm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}
 
 #endif /* __ASM_KVM_RME_H */
arch/arm64/kvm/mmu.c +3 -3
index 7531211f7e25..d80a9d408f71 100644
@@ -1070,14 +1070,15 @@ void stage2_unmap_vm(struct kvm *kvm)
 void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
-	struct kvm_pgtable *pgt = NULL;
+	struct kvm_pgtable *pgt;
 
 	write_lock(&kvm->mmu_lock);
+	pgt = mmu->pgt;
 	if (kvm_is_realm(kvm) &&
 	    (kvm_realm_state(kvm) != REALM_STATE_DEAD &&
 	     kvm_realm_state(kvm) != REALM_STATE_NONE)) {
-		/* Tearing down RTTs will be added in a later patch */
 		write_unlock(&kvm->mmu_lock);
+		kvm_realm_destroy_rtts(kvm, pgt->ia_bits);
 
 		/*
 		 * The physical PGD pages are delegated to the RMM, so cannot
@@ -1087,7 +1088,6 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 		 */
 		return;
 	}
-	pgt = mmu->pgt;
 	if (pgt) {
 		mmu->pgd_phys = 0;
 		mmu->pgt = NULL;
arch/arm64/kvm/rme.c +128 -0

140 lines not on this page · raw message

Signature · 1 line
2.43.0
#27 lore raw

PATCH v8 11/43 arm64: RME: Allocate/free RECs to match vCPUs

In reply to Steven Price

The RMM maintains a data structure known as the Realm Execution Context (or REC). It is similar to struct kvm_vcpu and tracks the state of the virtual CPUs. KVM must delegate memory and request the structures are created when vCPUs are created, and suitably tear down on destruction.

RECs must also be supplied with addition pages - auxiliary (or AUX) granules - for storing the larger registers state (e.g. for SVE). The number of AUX granules for a REC depends on the parameters with which the Realm was created - the RMM makes this information available via the RMI_REC_AUX_COUNT call performed after creating the Realm Descriptor (RD).

Note that only some of register state for the REC can be set by KVM, the rest is defined by the RMM (zeroed). The register state then cannot be changed by KVM after the REC is created (except when the guest explicitly requests this e.g. by performing a PSCI call). The RMM also requires that the VMM creates RECs in ascending order of the MPIDR.

See Realm Management Monitor specification (DEN0137) for more information:
https://developer.arm.com/documentation/den0137/

---
Changes since v7:
* Add comment explaining the aux_pages array.
* Rename "undeleted_failed" variable to "should_free" to avoid a confusing double negative.
Changes since v6:
* Avoid reporting the KVM_ARM_VCPU_REC feature if the guest isn't a realm guest.
* Support host page size being larger than RMM's granule size when allocating/freeing aux granules.
Changes since v5:
* Separate the concept of vcpu_is_rec() and kvm_arm_vcpu_rec_finalized() by using the KVM_ARM_VCPU_REC feature as the indication that the VCPU is a REC.
Changes since v2:
* Free rec->run earlier in kvm_destroy_realm() and adapt to previous patches.

---
 arch/arm64/include/asm/kvm_emulate.h |   7 ++
 arch/arm64/include/asm/kvm_host.h    |   3 +
 arch/arm64/include/asm/kvm_rme.h     |  27 ++++
 arch/arm64/kvm/arm.c                 |  13 +-
 arch/arm64/kvm/reset.c               |  11 ++
 arch/arm64/kvm/rme.c                 | 180 +++++++++++++++++++++++++++
 6 files changed, 239 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +7 -0
index 4ee6c215da82..c803c8188d9c 100644
@@ -706,7 +706,14 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)
 
 static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
 {
+	if (static_branch_unlikely(&kvm_rme_is_available))
+		return vcpu_has_feature(vcpu, KVM_ARM_VCPU_REC);
 	return false;
 }
 
+static inline bool kvm_arm_rec_finalized(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.rec.mpidr != INVALID_HWID;
+}
+
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +3 -0
index 7bd81b86eab0..ceb5e60ae5ae 100644
@@ -828,6 +828,9 @@ struct kvm_vcpu_arch {
 
 	/* Per-vcpu CCSIDR override or NULL */
 	u32 *ccsidr;
+
+	/* Realm meta data */
+	struct realm_rec rec;
 };
 
 /*
arch/arm64/include/asm/kvm_rme.h +27 -0
index 5f0de9a6d339..f716b890e484 100644
@@ -6,6 +6,7 @@
 #ifndef __ASM_KVM_RME_H
 #define __ASM_KVM_RME_H
 
+#include <asm/rmi_smc.h>
 #include <uapi/linux/kvm.h>
 
 /**
@@ -65,6 +66,30 @@ struct realm {
 	unsigned int ia_bits;
 };
 
+/**
+ * struct realm_rec - Additional per VCPU data for a Realm
+ *
+ * @mpidr: MPIDR (Multiprocessor Affinity Register) value to identify this VCPU
+ * @rec_page: Kernel VA of the RMM's private page for this REC
+ * @aux_pages: Additional pages private to the RMM for this REC
+ * @run: Kernel VA of the RmiRecRun structure shared with the RMM
+ */
+struct realm_rec {
+	unsigned long mpidr;
+	void *rec_page;
+	/*
+	 * REC_PARAMS_AUX_GRANULES is the maximum number of granules that the
+	 * RMM can require. By using that to size the array we know that it
+	 * will be big enough as the page size is always at least as large as
+	 * the granule size. In the case of a larger page size than 4k (or an
+	 * RMM which requires fewer auxiliary granules), the array will be
+	 * bigger than needed however the extra memory required is small and
+	 * this keeps the code cleaner.
+	 */
+	struct page *aux_pages[REC_PARAMS_AUX_GRANULES];
+	struct rec_run *run;
+};
+
 void kvm_init_rme(void);
 u32 kvm_realm_ipa_limit(void);
 
@@ -72,6 +97,8 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
 int kvm_init_realm_vm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
 void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
+int kvm_create_rec(struct kvm_vcpu *vcpu);
+void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
 static inline bool kvm_realm_is_private_address(struct realm *realm,
 						unsigned long addr)
arch/arm64/kvm/arm.c +11 -2
index ba55950e6a6e..fd83efb667cc 100644
@@ -489,6 +489,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	/* Force users to call KVM_ARM_VCPU_INIT */
 	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
 
+	vcpu->arch.rec.mpidr = INVALID_HWID;
+
 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
 
 	/* Set up the timer */
@@ -1447,7 +1449,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return -EINVAL;
 }
 
-static unsigned long system_supported_vcpu_features(void)
+static unsigned long system_supported_vcpu_features(struct kvm *kvm)
 {
 	unsigned long features = KVM_VCPU_VALID_FEATURES;
 
@@ -1468,6 +1470,9 @@ static unsigned long system_supported_vcpu_features(void)
 	if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
 		clear_bit(KVM_ARM_VCPU_HAS_EL2, &features);
 
+	if (!kvm_is_realm(kvm))
+		clear_bit(KVM_ARM_VCPU_REC, &features);
+
 	return features;
 }
 
@@ -1485,7 +1490,7 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 			return -ENOENT;
 	}
 
-	if (features & ~system_supported_vcpu_features())
+	if (features & ~system_supported_vcpu_features(vcpu->kvm))
 		return -EINVAL;
 
 	/*
@@ -1507,6 +1512,10 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 	if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
 		return -EINVAL;
 
+	/* RME is incompatible with AArch32 */
+	if (test_bit(KVM_ARM_VCPU_REC, &features))
+		return -EINVAL;
+
 	return 0;
 }
 
arch/arm64/kvm/reset.c +11 -0
index f82fcc614e13..7ba3c7918a0b 100644
@@ -137,6 +137,11 @@ int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
 			return -EPERM;
 
 		return kvm_vcpu_finalize_sve(vcpu);
+	case KVM_ARM_VCPU_REC:
+		if (!kvm_is_realm(vcpu->kvm) || !vcpu_is_rec(vcpu))
+			return -EINVAL;
+
+		return kvm_create_rec(vcpu);
 	}
 
 	return -EINVAL;
@@ -147,6 +152,11 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
 	if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
 		return false;
 
+	if (kvm_is_realm(vcpu->kvm) &&
+	    !(vcpu_is_rec(vcpu) && kvm_arm_rec_finalized(vcpu) &&
+	      READ_ONCE(vcpu->kvm->arch.realm.state) == REALM_STATE_ACTIVE))
+		return false;
+
 	return true;
 }
 
@@ -159,6 +169,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 		kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
 	kfree(sve_state);
 	kfree(vcpu->arch.ccsidr);
+	kvm_destroy_rec(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
arch/arm64/kvm/rme.c +180 -0

186 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer Reviewed-by on v8 11/43
#28 lore raw

Re: [PATCH v8 11/43] arm64: RME: Allocate/free RECs to match vCPUs

Quoted 17 lines · Steven Price

The RMM maintains a data structure known as the Realm Execution Context (or REC). It is similar to struct kvm_vcpu and tracks the state of the virtual CPUs. KVM must delegate memory and request the structures are created when vCPUs are created, and suitably tear down on destruction.

RECs must also be supplied with addition pages - auxiliary (or AUX) granules - for storing the larger registers state (e.g. for SVE). The number of AUX granules for a REC depends on the parameters with which the Realm was created - the RMM makes this information available via the RMI_REC_AUX_COUNT call performed after creating the Realm Descriptor (RD).

Note that only some of register state for the REC can be set by KVM, the rest is defined by the RMM (zeroed). The register state then cannot be changed by KVM after the REC is created (except when the guest explicitly requests this e.g. by performing a PSCI call). The RMM also requires that the VMM creates RECs in ascending order of the MPIDR.

Quoted 5 lines

See Realm Management Monitor specification (DEN0137) for more information:
https://developer.arm.com/documentation/den0137/

Quoted 205 lines

---
Changes since v7:
* Add comment explaining the aux_pages array.
* Rename "undeleted_failed" variable to "should_free" to avoid a confusing double negative.
Changes since v6:
* Avoid reporting the KVM_ARM_VCPU_REC feature if the guest isn't a realm guest.
* Support host page size being larger than RMM's granule size when allocating/freeing aux granules.
Changes since v5:
* Separate the concept of vcpu_is_rec() and kvm_arm_vcpu_rec_finalized() by using the KVM_ARM_VCPU_REC feature as the indication that the VCPU is a REC.
Changes since v2:
* Free rec->run earlier in kvm_destroy_realm() and adapt to previous patches.

---
  arch/arm64/include/asm/kvm_emulate.h |   7 ++
  arch/arm64/include/asm/kvm_host.h    |   3 +
  arch/arm64/include/asm/kvm_rme.h     |  27 ++++
  arch/arm64/kvm/arm.c                 |  13 +-
  arch/arm64/kvm/reset.c               |  11 ++
  arch/arm64/kvm/rme.c                 | 180 +++++++++++++++++++++++++++
  6 files changed, 239 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +7 -0
index 4ee6c215da82..c803c8188d9c 100644
@@ -706,7 +706,14 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)
 
  static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
  {
+	if (static_branch_unlikely(&kvm_rme_is_available))
+		return vcpu_has_feature(vcpu, KVM_ARM_VCPU_REC);
  	return false;
  }
 
+static inline bool kvm_arm_rec_finalized(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.rec.mpidr != INVALID_HWID;
+}
+
  #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +3 -0
index 7bd81b86eab0..ceb5e60ae5ae 100644
@@ -828,6 +828,9 @@ struct kvm_vcpu_arch {
 
  	/* Per-vcpu CCSIDR override or NULL */
  	u32 *ccsidr;
+
+	/* Realm meta data */
+	struct realm_rec rec;
  };
 
  /*
arch/arm64/include/asm/kvm_rme.h +27 -0
index 5f0de9a6d339..f716b890e484 100644
@@ -6,6 +6,7 @@
  #ifndef __ASM_KVM_RME_H
  #define __ASM_KVM_RME_H
 
+#include <asm/rmi_smc.h>
  #include <uapi/linux/kvm.h>
 
  /**
@@ -65,6 +66,30 @@ struct realm {
  	unsigned int ia_bits;
  };
 
+/**
+ * struct realm_rec - Additional per VCPU data for a Realm
+ *
+ * @mpidr: MPIDR (Multiprocessor Affinity Register) value to identify this VCPU
+ * @rec_page: Kernel VA of the RMM's private page for this REC
+ * @aux_pages: Additional pages private to the RMM for this REC
+ * @run: Kernel VA of the RmiRecRun structure shared with the RMM
+ */
+struct realm_rec {
+	unsigned long mpidr;
+	void *rec_page;
+	/*
+	 * REC_PARAMS_AUX_GRANULES is the maximum number of granules that the
+	 * RMM can require. By using that to size the array we know that it
+	 * will be big enough as the page size is always at least as large as
+	 * the granule size. In the case of a larger page size than 4k (or an
+	 * RMM which requires fewer auxiliary granules), the array will be
+	 * bigger than needed however the extra memory required is small and
+	 * this keeps the code cleaner.
+	 */
+	struct page *aux_pages[REC_PARAMS_AUX_GRANULES];
+	struct rec_run *run;
+};
+
  void kvm_init_rme(void);
  u32 kvm_realm_ipa_limit(void);
 
@@ -72,6 +97,8 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
  int kvm_init_realm_vm(struct kvm *kvm);
  void kvm_destroy_realm(struct kvm *kvm);
  void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
+int kvm_create_rec(struct kvm_vcpu *vcpu);
+void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
  static inline bool kvm_realm_is_private_address(struct realm *realm,
  						unsigned long addr)
arch/arm64/kvm/arm.c +11 -2
index ba55950e6a6e..fd83efb667cc 100644
@@ -489,6 +489,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
  	/* Force users to call KVM_ARM_VCPU_INIT */
  	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
 
+	vcpu->arch.rec.mpidr = INVALID_HWID;
+
  	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
 
  	/* Set up the timer */
@@ -1447,7 +1449,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  	return -EINVAL;
  }
 
-static unsigned long system_supported_vcpu_features(void)
+static unsigned long system_supported_vcpu_features(struct kvm *kvm)
  {
  	unsigned long features = KVM_VCPU_VALID_FEATURES;
 
@@ -1468,6 +1470,9 @@ static unsigned long system_supported_vcpu_features(void)
  	if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
  		clear_bit(KVM_ARM_VCPU_HAS_EL2, &features);
 
+	if (!kvm_is_realm(kvm))
+		clear_bit(KVM_ARM_VCPU_REC, &features);
+
  	return features;
  }
 
@@ -1485,7 +1490,7 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
  			return -ENOENT;
  	}
 
-	if (features & ~system_supported_vcpu_features())
+	if (features & ~system_supported_vcpu_features(vcpu->kvm))
  		return -EINVAL;
 
  	/*
@@ -1507,6 +1512,10 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
  	if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
  		return -EINVAL;
 
+	/* RME is incompatible with AArch32 */
+	if (test_bit(KVM_ARM_VCPU_REC, &features))
+		return -EINVAL;
+
  	return 0;
  }
 
arch/arm64/kvm/reset.c +11 -0
index f82fcc614e13..7ba3c7918a0b 100644
@@ -137,6 +137,11 @@ int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
  			return -EPERM;
 
  		return kvm_vcpu_finalize_sve(vcpu);
+	case KVM_ARM_VCPU_REC:
+		if (!kvm_is_realm(vcpu->kvm) || !vcpu_is_rec(vcpu))
+			return -EINVAL;
+
+		return kvm_create_rec(vcpu);
  	}
 
  	return -EINVAL;
@@ -147,6 +152,11 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
  	if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
  		return false;
 
+	if (kvm_is_realm(vcpu->kvm) &&
+	    !(vcpu_is_rec(vcpu) && kvm_arm_rec_finalized(vcpu) &&
+	      READ_ONCE(vcpu->kvm->arch.realm.state) == REALM_STATE_ACTIVE))
+		return false;
+
  	return true;
  }
 
@@ -159,6 +169,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  		kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
  	kfree(sve_state);
  	kfree(vcpu->arch.ccsidr);
+	kvm_destroy_rec(vcpu);
  }
 
  static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
arch/arm64/kvm/rme.c +0 -0
index 3053261c3b52..f4923fc3b34e 100644
@@ -474,6 +474,186 @@ void kvm_destroy_realm(struct kvm *kvm)
  	kvm_free_stage2_pgd(&kvm->arch.mmu);
  }

arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 485

Quoted 10 lines
arch/arm64/kvm/rme.c +9 -0
 
+static void free_rec_aux(struct page **aux_pages,
+			 unsigned int num_aux)
+{
+	unsigned int i, j;
+	unsigned int page_count = 0;
+
+	for (i = 0; i < num_aux;) {
+		struct page *aux_page = aux_pages[page_count++];
+		phys_addr_t aux_page_phys = page_to_phys(aux_page);

[ Coming back to the top after a full review, feel free to ignore the comments on the naming with non-4K support code. ]

minor nit: aux_phys_granule ? or even aux_granule ? "page" is confusing.

Quoted 6 lines
arch/arm64/kvm/rme.c +6 -0
+		bool should_free = true;
+
+		for (j = 0; j < PAGE_SIZE && i < num_aux; j += RMM_PAGE_SIZE) {
+			if (WARN_ON(rmi_granule_undelegate(aux_page_phys)))
+				should_free = false;
+			aux_page_phys += RMM_PAGE_SIZE;

arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 501

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+			i++;
+		}
+		/* Only free if all the undelegate calls were successful */
+		if (should_free)
+			__free_page(aux_page);
+	}
+}
+
+static int alloc_rec_aux(struct page **aux_pages,
+			 u64 *aux_phys_pages,

minor nit: aux_[phys_]?granules ? as we are storing the granules here.

arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 510

Quoted 9 lines
arch/arm64/kvm/rme.c +9 -0
+			 unsigned int num_aux)
+{
+	struct page *aux_page;
+	int page_count = 0;
+	unsigned int i, j;
+	int ret;
+
+	for (i = 0; i < num_aux;) {
+		phys_addr_t aux_page_phys;

same here, aux_granule

Quoted 95 lines
arch/arm64/kvm/rme.c +95 -0
+
+		aux_page = alloc_page(GFP_KERNEL);
+		if (!aux_page) {
+			ret = -ENOMEM;
+			goto out_err;
+		}
+
+		aux_page_phys = page_to_phys(aux_page);
+		for (j = 0; j < PAGE_SIZE && i < num_aux; j += RMM_PAGE_SIZE) {
+			if (rmi_granule_delegate(aux_page_phys)) {
+				ret = -ENXIO;
+				goto err_undelegate;
+			}
+			aux_phys_pages[i++] = aux_page_phys;
+			aux_page_phys += RMM_PAGE_SIZE;
+		}
+		aux_pages[page_count++] = aux_page;
+	}
+
+	return 0;
+err_undelegate:
+	while (j > 0) {
+		j -= RMM_PAGE_SIZE;
+		i--;
+		if (WARN_ON(rmi_granule_undelegate(aux_phys_pages[i]))) {
+			/* Leak the page if the undelegate fails */
+			goto out_err;
+		}
+	}
+	__free_page(aux_page);
+out_err:
+	free_rec_aux(aux_pages, i);
+	return ret;
+}
+
+int kvm_create_rec(struct kvm_vcpu *vcpu)
+{
+	struct user_pt_regs *vcpu_regs = vcpu_gp_regs(vcpu);
+	unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
+	struct realm *realm = &vcpu->kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long rec_page_phys;
+	struct rec_params *params;
+	int r, i;
+
+	if (kvm_realm_state(vcpu->kvm) != REALM_STATE_NEW)
+		return -ENOENT;
+
+	if (rec->run)
+		return -EBUSY;
+
+	/*
+	 * The RMM will report PSCI v1.0 to Realms and the KVM_ARM_VCPU_PSCI_0_2
+	 * flag covers v0.2 and onwards.
+	 */
+	if (!vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2))
+		return -EINVAL;
+
+	BUILD_BUG_ON(sizeof(*params) > PAGE_SIZE);
+	BUILD_BUG_ON(sizeof(*rec->run) > PAGE_SIZE);
+
+	params = (struct rec_params *)get_zeroed_page(GFP_KERNEL);
+	rec->rec_page = (void *)__get_free_page(GFP_KERNEL);
+	rec->run = (void *)get_zeroed_page(GFP_KERNEL);
+	if (!params || !rec->rec_page || !rec->run) {
+		r = -ENOMEM;
+		goto out_free_pages;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(params->gprs); i++)
+		params->gprs[i] = vcpu_regs->regs[i];
+
+	params->pc = vcpu_regs->pc;
+
+	if (vcpu->vcpu_id == 0)
+		params->flags |= REC_PARAMS_FLAG_RUNNABLE;
+
+	rec_page_phys = virt_to_phys(rec->rec_page);
+
+	if (rmi_granule_delegate(rec_page_phys)) {
+		r = -ENXIO;
+		goto out_free_pages;
+	}
+
+	r = alloc_rec_aux(rec->aux_pages, params->aux, realm->num_aux);
+	if (r)
+		goto out_undelegate_rmm_rec;
+
+	params->num_rec_aux = realm->num_aux;
+	params->mpidr = mpidr;
+
+	if (rmi_rec_create(virt_to_phys(realm->rd),
+			   rec_page_phys,
+			   virt_to_phys(params))) {
+		r = -ENXIO;

arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 615

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+		goto out_free_rec_aux;
+	}
+
+	rec->mpidr = mpidr;
+
+	free_page((unsigned long)params);
+	return 0;
+
+out_free_rec_aux:
+	free_rec_aux(rec->aux_pages, realm->num_aux);

This is problematic. Realm->num_aux is number of AUX granules, not
num_aux_pages and can easily trigger overflow with non-4K. The alloc_rec_aux() is only allocating as many Pages required to cover the num_aux granules.

I would say, simply drop the non-4K page support change, we can come back to fixing the non-4K properly later, when we enable it.

Suzuki

Quoted 44 lines
arch/arm64/kvm/rme.c +41 -0
+out_undelegate_rmm_rec:
+	if (WARN_ON(rmi_granule_undelegate(rec_page_phys)))
+		rec->rec_page = NULL;
+out_free_pages:
+	free_page((unsigned long)rec->run);
+	free_page((unsigned long)rec->rec_page);
+	free_page((unsigned long)params);
+	return r;
+}
+
+void kvm_destroy_rec(struct kvm_vcpu *vcpu)
+{
+	struct realm *realm = &vcpu->kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long rec_page_phys;
+
+	if (!vcpu_is_rec(vcpu))
+		return;
+
+	if (!rec->run) {
+		/* Nothing to do if the VCPU hasn't been finalized */
+		return;
+	}
+
+	free_page((unsigned long)rec->run);
+
+	rec_page_phys = virt_to_phys(rec->rec_page);
+
+	/*
+	 * The REC and any AUX pages cannot be reclaimed until the REC is
+	 * destroyed. So if the REC destroy fails then the REC page and any AUX
+	 * pages will be leaked.
+	 */
+	if (WARN_ON(rmi_rec_destroy(rec_page_phys)))
+		return;
+
+	free_rec_aux(rec->aux_pages, realm->num_aux);
+
+	free_delegated_granule(rec_page_phys);
+}
+
  int kvm_init_realm_vm(struct kvm *kvm)
  {
  	kvm->arch.realm.params = (void *)get_zeroed_page(GFP_KERNEL);
#29 lore raw

PATCH v8 12/43 KVM: arm64: vgic: Provide helper for number of list registers

In reply to Steven Price

Currently the number of list registers available is stored in a global (kvm_vgic_global_state.nr_lr). With Arm CCA the RMM is permitted to reserve list registers for its own use and so the number of available list registers can be fewer for a realm VM. Provide a wrapper function to fetch the global in preparation for restricting nr_lr when dealing with a realm VM.

---
New patch for v6

---
 arch/arm64/kvm/vgic/vgic.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic.c +8 -3
index 8f8096d48925..8d189ce18ea0 100644
@@ -21,6 +21,11 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {
 	.gicv3_cpuif = STATIC_KEY_FALSE_INIT,
 };
 
+static inline int kvm_vcpu_vgic_nr_lr(struct kvm_vcpu *vcpu)
+{
+	return kvm_vgic_global_state.nr_lr;
+}
+
 /*
  * Locking order is always:
  * kvm->lock (mutex)
@@ -802,7 +807,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
 	lockdep_assert_held(&vgic_cpu->ap_list_lock);
 
 	count = compute_ap_list_depth(vcpu, &multi_sgi);
-	if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
+	if (count > kvm_vcpu_vgic_nr_lr(vcpu) || multi_sgi)
 		vgic_sort_ap_list(vcpu);
 
 	count = 0;
@@ -831,7 +836,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
 
 		raw_spin_unlock(&irq->irq_lock);
 
-		if (count == kvm_vgic_global_state.nr_lr) {
+		if (count == kvm_vcpu_vgic_nr_lr(vcpu)) {
 			if (!list_is_last(&irq->ap_list,
 					  &vgic_cpu->ap_list_head))
 				vgic_set_underflow(vcpu);
@@ -840,7 +845,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
 	}
 
 	/* Nuke remaining LRs */
-	for (i = count ; i < kvm_vgic_global_state.nr_lr; i++)
+	for (i = count ; i < kvm_vcpu_vgic_nr_lr(vcpu); i++)
 		vgic_clear_lr(vcpu, i);
 
 	if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
Signature · 1 line
2.43.0
Reviewed-by on v8 12/43
#30 lore raw

Re: [PATCH v8 12/43] KVM: arm64: vgic: Provide helper for number of list registers

Quoted 7 lines · Steven Price

Currently the number of list registers available is stored in a global (kvm_vgic_global_state.nr_lr). With Arm CCA the RMM is permitted to reserve list registers for its own use and so the number of available list registers can be fewer for a realm VM. Provide a wrapper function to fetch the global in preparation for restricting nr_lr when dealing with a realm VM.

Quoted 7 lines

---
New patch for v6

---
  arch/arm64/kvm/vgic/vgic.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)

With below nitpick addressed:

Quoted 35 lines
arch/arm64/kvm/vgic/vgic.c +7 -2
index 8f8096d48925..8d189ce18ea0 100644
@@ -21,6 +21,11 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {
  	.gicv3_cpuif = STATIC_KEY_FALSE_INIT,
  };
 
+static inline int kvm_vcpu_vgic_nr_lr(struct kvm_vcpu *vcpu)
+{
+	return kvm_vgic_global_state.nr_lr;
+}
+
  /*
   * Locking order is always:
   * kvm->lock (mutex)
@@ -802,7 +807,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
  	lockdep_assert_held(&vgic_cpu->ap_list_lock);
 
  	count = compute_ap_list_depth(vcpu, &multi_sgi);
-	if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
+	if (count > kvm_vcpu_vgic_nr_lr(vcpu) || multi_sgi)
  		vgic_sort_ap_list(vcpu);
 
  	count = 0;
@@ -831,7 +836,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
 
  		raw_spin_unlock(&irq->irq_lock);
 
-		if (count == kvm_vgic_global_state.nr_lr) {
+		if (count == kvm_vcpu_vgic_nr_lr(vcpu)) {
  			if (!list_is_last(&irq->ap_list,
  					  &vgic_cpu->ap_list_head))
  				vgic_set_underflow(vcpu);

arch/arm64/kvm/vgic/vgic.c static void vgic_flush_lr_state(struct kvm_vcpu *vcpu) · line 850

Quoted 7 lines
arch/arm64/kvm/vgic/vgic.c +1 -1
@@ -840,7 +845,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
  	}
 
  	/* Nuke remaining LRs */
-	for (i = count ; i < kvm_vgic_global_state.nr_lr; i++)
+	for (i = count ; i < kvm_vcpu_vgic_nr_lr(vcpu); i++)
  		vgic_clear_lr(vcpu, i);
 

The unnecessary space before the semicolon can be dropped.

	for (i = count; i < kvm_vcpu_vgic_nr_lr(vcpu); i++)

arch/arm64/kvm/vgic/vgic.c static void vgic_flush_lr_state(struct kvm_vcpu *vcpu) · line 851

Quoted 1 line
arch/arm64/kvm/vgic/vgic.c +0 -0
  	if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))

Thanks,
Gavin

reviewer Reviewed-by on v8 12/43
#31 lore raw

Re: [PATCH v8 12/43] KVM: arm64: vgic: Provide helper for number of list registers

In reply to Steven Price
Quoted 8 lines · Steven Price

Currently the number of list registers available is stored in a global (kvm_vgic_global_state.nr_lr). With Arm CCA the RMM is permitted to reserve list registers for its own use and so the number of available list registers can be fewer for a realm VM. Provide a wrapper function to fetch the global in preparation for restricting nr_lr when dealing with a realm VM.

#32 lore raw

PATCH v8 13/43 arm64: RME: Support for the VGIC in realms

In reply to Steven Price

The RMM provides emulation of a VGIC to the realm guest but delegates much of the handling to the host. Implement support in KVM for saving/restoring state to/from the REC structure.

---
Changes from v5:
* Handle RMM providing fewer GIC LRs than the hardware supports.

---
 arch/arm64/include/asm/kvm_rme.h |  1 +
 arch/arm64/kvm/arm.c             | 16 +++++++++---
 arch/arm64/kvm/rme.c             |  5 ++++
 arch/arm64/kvm/vgic/vgic-init.c  |  2 +-
 arch/arm64/kvm/vgic/vgic-v3.c    |  6 ++++-
 arch/arm64/kvm/vgic/vgic.c       | 43 ++++++++++++++++++++++++++++++--
 6 files changed, 66 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_rme.h +1 -0
index f716b890e484..9bcad6ec5dbb 100644
@@ -92,6 +92,7 @@ struct realm_rec {
 
 void kvm_init_rme(void);
 u32 kvm_realm_ipa_limit(void);
+u32 kvm_realm_vgic_nr_lr(void);
 
 int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
 int kvm_init_realm_vm(struct kvm *kvm);
arch/arm64/kvm/arm.c +13 -3
index fd83efb667cc..808d7e479571 100644
@@ -683,19 +683,24 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 		kvm_call_hyp_nvhe(__pkvm_vcpu_put);
 	}
 
+	kvm_timer_vcpu_put(vcpu);
+	kvm_vgic_put(vcpu);
+
+	vcpu->cpu = -1;
+
+	if (vcpu_is_rec(vcpu))
+		return;
+
 	kvm_vcpu_put_debug(vcpu);
 	kvm_arch_vcpu_put_fp(vcpu);
 	if (has_vhe())
 		kvm_vcpu_put_vhe(vcpu);
-	kvm_timer_vcpu_put(vcpu);
-	kvm_vgic_put(vcpu);
 	kvm_vcpu_pmu_restore_host(vcpu);
 	if (vcpu_has_nv(vcpu))
 		kvm_vcpu_put_hw_mmu(vcpu);
 	kvm_arm_vmid_clear_active();
 
 	vcpu_clear_on_unsupported_cpu(vcpu);
-	vcpu->cpu = -1;
 }
 
 static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
@@ -912,6 +917,11 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 			return ret;
 	}
 
+	if (!irqchip_in_kernel(kvm) && kvm_is_realm(vcpu->kvm)) {
+		/* Userspace irqchip not yet supported with Realms */
+		return -EOPNOTSUPP;
+	}
+
 	mutex_lock(&kvm->arch.config_lock);
 	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
 	mutex_unlock(&kvm->arch.config_lock);
arch/arm64/kvm/rme.c +5 -0
index f4923fc3b34e..1239eb07aca6 100644
@@ -77,6 +77,11 @@ u32 kvm_realm_ipa_limit(void)
 	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
 }
 
+u32 kvm_realm_vgic_nr_lr(void)
+{
+	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS);
+}
+
 static int get_start_level(struct realm *realm)
 {
 	return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
arch/arm64/kvm/vgic/vgic-init.c +1 -1
index 1f33e71c2a73..a81c7f3d1d42 100644
@@ -81,7 +81,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 	 * the proper checks already.
 	 */
 	if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
-		!kvm_vgic_global_state.can_emulate_gicv2)
+	    (!kvm_vgic_global_state.can_emulate_gicv2 || kvm_is_realm(kvm)))
 		return -ENODEV;
 
 	/* Must be held to avoid race with vCPU creation */
arch/arm64/kvm/vgic/vgic-v3.c +5 -1
index b9ad7c42c5b0..c10ad817030d 100644
@@ -8,9 +8,11 @@
 #include <linux/kvm_host.h>
 #include <linux/string_choices.h>
 #include <kvm/arm_vgic.h>
+#include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
 #include <asm/kvm_mmu.h>
 #include <asm/kvm_asm.h>
+#include <asm/rmi_smc.h>
 
 #include "vgic.h"
 
@@ -758,7 +760,9 @@ void vgic_v3_put(struct kvm_vcpu *vcpu)
 		return;
 	}
 
-	if (likely(!is_protected_kvm_enabled()))
+	if (vcpu_is_rec(vcpu))
+		cpu_if->vgic_vmcr = vcpu->arch.rec.run->exit.gicv3_vmcr;
+	else if (likely(!is_protected_kvm_enabled()))
 		kvm_call_hyp(__vgic_v3_save_vmcr_aprs, cpu_if);
 	WARN_ON(vgic_v4_put(vcpu));
 
arch/arm64/kvm/vgic/vgic.c +41 -2

88 lines not on this page · raw message

Signature · 1 line
2.43.0
#33 lore raw

Re: [PATCH v8 13/43] arm64: RME: Support for the VGIC in realms

Quoted 156 lines · Steven Price

The RMM provides emulation of a VGIC to the realm guest but delegates much of the handling to the host. Implement support in KVM for saving/restoring state to/from the REC structure.

---
Changes from v5:
* Handle RMM providing fewer GIC LRs than the hardware supports.

---
  arch/arm64/include/asm/kvm_rme.h |  1 +
  arch/arm64/kvm/arm.c             | 16 +++++++++---
  arch/arm64/kvm/rme.c             |  5 ++++
  arch/arm64/kvm/vgic/vgic-init.c  |  2 +-
  arch/arm64/kvm/vgic/vgic-v3.c    |  6 ++++-
  arch/arm64/kvm/vgic/vgic.c       | 43 ++++++++++++++++++++++++++++++--
  6 files changed, 66 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_rme.h +1 -0
index f716b890e484..9bcad6ec5dbb 100644
@@ -92,6 +92,7 @@ struct realm_rec {
 
  void kvm_init_rme(void);
  u32 kvm_realm_ipa_limit(void);
+u32 kvm_realm_vgic_nr_lr(void);
 
  int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
  int kvm_init_realm_vm(struct kvm *kvm);
arch/arm64/kvm/arm.c +13 -3
index fd83efb667cc..808d7e479571 100644
@@ -683,19 +683,24 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  		kvm_call_hyp_nvhe(__pkvm_vcpu_put);
  	}
 
+	kvm_timer_vcpu_put(vcpu);
+	kvm_vgic_put(vcpu);
+
+	vcpu->cpu = -1;
+
+	if (vcpu_is_rec(vcpu))
+		return;
+
  	kvm_vcpu_put_debug(vcpu);
  	kvm_arch_vcpu_put_fp(vcpu);
  	if (has_vhe())
  		kvm_vcpu_put_vhe(vcpu);
-	kvm_timer_vcpu_put(vcpu);
-	kvm_vgic_put(vcpu);
  	kvm_vcpu_pmu_restore_host(vcpu);
  	if (vcpu_has_nv(vcpu))
  		kvm_vcpu_put_hw_mmu(vcpu);
  	kvm_arm_vmid_clear_active();
 
  	vcpu_clear_on_unsupported_cpu(vcpu);
-	vcpu->cpu = -1;
  }
 
  static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
@@ -912,6 +917,11 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
  			return ret;
  	}
 
+	if (!irqchip_in_kernel(kvm) && kvm_is_realm(vcpu->kvm)) {
+		/* Userspace irqchip not yet supported with Realms */
+		return -EOPNOTSUPP;
+	}
+
  	mutex_lock(&kvm->arch.config_lock);
  	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
  	mutex_unlock(&kvm->arch.config_lock);
arch/arm64/kvm/rme.c +5 -0
index f4923fc3b34e..1239eb07aca6 100644
@@ -77,6 +77,11 @@ u32 kvm_realm_ipa_limit(void)
  	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
  }
 
+u32 kvm_realm_vgic_nr_lr(void)
+{
+	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS);
+}
+
  static int get_start_level(struct realm *realm)
  {
  	return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
arch/arm64/kvm/vgic/vgic-init.c +1 -1
index 1f33e71c2a73..a81c7f3d1d42 100644
@@ -81,7 +81,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
  	 * the proper checks already.
  	 */
  	if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
-		!kvm_vgic_global_state.can_emulate_gicv2)
+	    (!kvm_vgic_global_state.can_emulate_gicv2 || kvm_is_realm(kvm)))
  		return -ENODEV;
 
  	/* Must be held to avoid race with vCPU creation */
arch/arm64/kvm/vgic/vgic-v3.c +5 -1
index b9ad7c42c5b0..c10ad817030d 100644
@@ -8,9 +8,11 @@
  #include <linux/kvm_host.h>
  #include <linux/string_choices.h>
  #include <kvm/arm_vgic.h>
+#include <asm/kvm_emulate.h>
  #include <asm/kvm_hyp.h>
  #include <asm/kvm_mmu.h>
  #include <asm/kvm_asm.h>
+#include <asm/rmi_smc.h>
 
  #include "vgic.h"
 
@@ -758,7 +760,9 @@ void vgic_v3_put(struct kvm_vcpu *vcpu)
  		return;
  	}
 
-	if (likely(!is_protected_kvm_enabled()))
+	if (vcpu_is_rec(vcpu))
+		cpu_if->vgic_vmcr = vcpu->arch.rec.run->exit.gicv3_vmcr;
+	else if (likely(!is_protected_kvm_enabled()))
  		kvm_call_hyp(__vgic_v3_save_vmcr_aprs, cpu_if);
  	WARN_ON(vgic_v4_put(vcpu));
 
arch/arm64/kvm/vgic/vgic.c +4 -0
index 8d189ce18ea0..c68a41a29917 100644
@@ -10,7 +10,9 @@
  #include <linux/list_sort.h>
  #include <linux/nospec.h>
 
+#include <asm/kvm_emulate.h>
  #include <asm/kvm_hyp.h>
+#include <asm/kvm_rme.h>
 
  #include "vgic.h"
 
@@ -23,6 +25,8 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {
 
  static inline int kvm_vcpu_vgic_nr_lr(struct kvm_vcpu *vcpu)
  {
+	if (unlikely(vcpu_is_rec(vcpu)))
+		return kvm_realm_vgic_nr_lr();
  	return kvm_vgic_global_state.nr_lr;
  }
 
@@ -864,10 +868,23 @@ static inline bool can_access_vgic_from_kernel(void)
  	return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
  }
 

arch/arm64/kvm/vgic/vgic.c static inline bool can_access_vgic_from_kernel(void) · line 880

Quoted 10 lines
arch/arm64/kvm/vgic/vgic.c +10 -0
+static inline void vgic_rmm_save_state(struct kvm_vcpu *vcpu)
+{
+	struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
+	int i;
+
+	for (i = 0; i < kvm_vcpu_vgic_nr_lr(vcpu); i++) {
+		cpu_if->vgic_lr[i] = vcpu->arch.rec.run->exit.gicv3_lrs[i];
+		vcpu->arch.rec.run->enter.gicv3_lrs[i] = 0;
+	}
+}

We also need to save/restore gicv3_hcr/cpuif->vgic_hcr.

Quoted 53 lines
arch/arm64/kvm/vgic/vgic.c +24 -1
+
  static inline void vgic_save_state(struct kvm_vcpu *vcpu)
  {
  	if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  		vgic_v2_save_state(vcpu);
+	else if (vcpu_is_rec(vcpu))
+		vgic_rmm_save_state(vcpu);
  	else
  		__vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
  }
@@ -903,10 +920,28 @@ void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  	vgic_prune_ap_list(vcpu);
  }
 
+static inline void vgic_rmm_restore_state(struct kvm_vcpu *vcpu)
+{
+	struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
+	int i;
+
+	for (i = 0; i < kvm_vcpu_vgic_nr_lr(vcpu); i++) {
+		vcpu->arch.rec.run->enter.gicv3_lrs[i] = cpu_if->vgic_lr[i];
+		/*
+		 * Also populate the rec.run->exit copies so that a late
+		 * decision to back out from entering the realm doesn't cause
+		 * the state to be lost
+		 */
+		vcpu->arch.rec.run->exit.gicv3_lrs[i] = cpu_if->vgic_lr[i];
+	}
+}
+
  static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
  {
  	if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  		vgic_v2_restore_state(vcpu);
+	else if (vcpu_is_rec(vcpu))
+		vgic_rmm_restore_state(vcpu);
  	else
  		__vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
  }
@@ -976,7 +1011,9 @@ void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
 
  void kvm_vgic_load(struct kvm_vcpu *vcpu)
  {
-	if (unlikely(!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm))) {
+	if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
+		     !vgic_initialized(vcpu->kvm) ||
+		     vcpu_is_rec(vcpu))) {
 
 
  		if (has_vhe() && static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  			__vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
  		return;

arch/arm64/kvm/vgic/vgic.c void kvm_vgic_load(struct kvm_vcpu *vcpu) · line 1034

Quoted 9 lines
arch/arm64/kvm/vgic/vgic.c +3 -1
@@ -990,7 +1027,9 @@ void kvm_vgic_load(struct kvm_vcpu *vcpu)
 
  void kvm_vgic_put(struct kvm_vcpu *vcpu)
  {
-	if (unlikely(!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm))) {
+	if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
+		     !vgic_initialized(vcpu->kvm) ||
+		     vcpu_is_rec(vcpu))) {
  		if (has_vhe() && static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  			__vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);

We could return early here for rec, and skip the unnecessary trap steps. Similar for the vgic_load case.

Rest looks good to me.

Suzuki

Quoted 1 line
arch/arm64/kvm/vgic/vgic.c +0 -0
  		return;
#34 lore raw

Re: [PATCH v8 13/43] arm64: RME: Support for the VGIC in realms

Quoted 152 lines · Suzuki K Poulose
Quoted 151 lines · Steven Price

The RMM provides emulation of a VGIC to the realm guest but delegates much of the handling to the host. Implement support in KVM for saving/restoring state to/from the REC structure.

---
Changes from v5:
  * Handle RMM providing fewer GIC LRs than the hardware supports.
---
  arch/arm64/include/asm/kvm_rme.h |  1 + arch/arm64/kvm/arm.c             | 16 +++++++++--- arch/arm64/kvm/rme.c             |  5 ++++ arch/arm64/kvm/vgic/vgic-init.c  |  2 +- arch/arm64/kvm/vgic/vgic-v3.c    |  6 ++++- arch/arm64/kvm/vgic/vgic.c       | 43 ++++++++++++++++++++++++++++++--

  6 files changed, 66 insertions(+), 7 deletions(-)
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index f716b890e484..9bcad6ec5dbb 100644

arch/arm64/include/asm/kvm_rme.h +1 -0
@@ -92,6 +92,7 @@ struct realm_rec {
     void kvm_init_rme(void);
   u32 kvm_realm_ipa_limit(void);
+u32 kvm_realm_vgic_nr_lr(void);

    int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);
  int kvm_init_realm_vm(struct kvm *kvm);

arch/arm64/kvm/arm.c +11 -3
index fd83efb667cc..808d7e479571 100644
@@ -683,19 +683,24 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
           kvm_call_hyp_nvhe(__pkvm_vcpu_put);
       }
   +    kvm_timer_vcpu_put(vcpu);
+    kvm_vgic_put(vcpu);
+
+    vcpu->cpu = -1;
+
+    if (vcpu_is_rec(vcpu))
+        return;
+
       kvm_vcpu_put_debug(vcpu);
       kvm_arch_vcpu_put_fp(vcpu);
       if (has_vhe())
           kvm_vcpu_put_vhe(vcpu);
-    kvm_timer_vcpu_put(vcpu);
-    kvm_vgic_put(vcpu);
       kvm_vcpu_pmu_restore_host(vcpu);
       if (vcpu_has_nv(vcpu))
           kvm_vcpu_put_hw_mmu(vcpu);
       kvm_arm_vmid_clear_active();
         vcpu_clear_on_unsupported_cpu(vcpu);
-    vcpu->cpu = -1;
   }
     static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
@@ -912,6 +917,11 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu
 *vcpu)
               return ret;
       }
   +    if (!irqchip_in_kernel(kvm) && kvm_is_realm(vcpu->kvm)) {
+        /* Userspace irqchip not yet supported with Realms */
+        return -EOPNOTSUPP;
+    }
+
       mutex_lock(&kvm->arch.config_lock);
       set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);

      mutex_unlock(&kvm->arch.config_lock);

arch/arm64/kvm/rme.c +4 -0
index f4923fc3b34e..1239eb07aca6 100644
@@ -77,6 +77,11 @@ u32 kvm_realm_ipa_limit(void)
       return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
   }
   +u32 kvm_realm_vgic_nr_lr(void)
+{
+    return u64_get_bits(rmm_feat_reg0,
 RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS);
+}
+
   static int get_start_level(struct realm *realm)
   {

      return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));

arch/arm64/kvm/vgic/ renamed from arch/arm64/kvm/vgic/vgic-init.c +0 -0

vgic-init.c
index 1f33e71c2a73..a81c7f3d1d42 100644

arch/arm64/kvm/vgic/vgic-init.c +0 -0
@@ -81,7 +81,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
        * the proper checks already.
        */

      if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
-        !kvm_vgic_global_state.can_emulate_gicv2)
+        (!kvm_vgic_global_state.can_emulate_gicv2 || kvm_is_realm(kvm))) return -ENODEV;
        /* Must be held to avoid race with vCPU creation */

arch/arm64/kvm/vgic/vgic- renamed from arch/arm64/kvm/vgic/vgic-v3.c +0 -0

v3.c
index b9ad7c42c5b0..c10ad817030d 100644

arch/arm64/kvm/vgic/vgic-v3.c +2 -0
@@ -8,9 +8,11 @@
   #include <linux/kvm_host.h>
   #include <linux/string_choices.h>
   #include <kvm/arm_vgic.h>
+#include <asm/kvm_emulate.h>
   #include <asm/kvm_hyp.h>
   #include <asm/kvm_mmu.h>
   #include <asm/kvm_asm.h>
+#include <asm/rmi_smc.h>
     #include "vgic.h"
   @@ -758,7 +760,9 @@ void vgic_v3_put(struct kvm_vcpu *vcpu)
           return;

      }
  -    if (likely(!is_protected_kvm_enabled()))
+    if (vcpu_is_rec(vcpu))
+        cpu_if->vgic_vmcr = vcpu->arch.rec.run->exit.gicv3_vmcr; +    else if (likely(!is_protected_kvm_enabled())) kvm_call_hyp(__vgic_v3_save_vmcr_aprs, cpu_if); WARN_ON(vgic_v4_put(vcpu));
  diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c index 8d189ce18ea0..c68a41a29917 100644

arch/arm64/kvm/vgic/vgic.c +0 -0
@@ -10,7 +10,9 @@
   #include <linux/list_sort.h>
   #include <linux/nospec.h>

  +#include <asm/kvm_emulate.h>
  #include <asm/kvm_hyp.h>
+#include <asm/kvm_rme.h>
    #include "vgic.h"
  @@ -23,6 +25,8 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {
    static inline int kvm_vcpu_vgic_nr_lr(struct kvm_vcpu *vcpu) {
+    if (unlikely(vcpu_is_rec(vcpu)))
+        return kvm_realm_vgic_nr_lr();
      return kvm_vgic_global_state.nr_lr;
  }
  @@ -864,10 +868,23 @@ static inline bool can_access_vgic_from_kernel(void)
      return ! static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe(); }
  +static inline void vgic_rmm_save_state(struct kvm_vcpu *vcpu) +{
+    struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; +    int i;

Quoted 8 lines

arch/arm64/kvm/vgic/vgic.c static inline bool can_access_vgic_from_kernel(void) · line 880

Quoted 6 lines
arch/arm64/kvm/vgic/vgic.c +6 -0
+
+    for (i = 0; i < kvm_vcpu_vgic_nr_lr(vcpu); i++) {
+        cpu_if->vgic_lr[i] = vcpu->arch.rec.run->exit.gicv3_lrs[i];
+        vcpu->arch.rec.run->enter.gicv3_lrs[i] = 0;
+    }
+}

We also need to save/restore gicv3_hcr/cpuif->vgic_hcr.

I'm not sure how I missed that. Note that the RMM spec restricts the fields that the normal world is allowed to set. So I'll also add a new define to rmi_smc.h to provide the bitmask:

#define RMI_PERMITTED_GICV3_HCR_BITS   (ICH_HCR_EL2_UIE |              \
                                        ICH_HCR_EL2_LRENPIE |          \
                                        ICH_HCR_EL2_NPIE |             \
                                        ICH_HCR_EL2_VGrp0EIE |         \
                                        ICH_HCR_EL2_VGrp0DIE |         \
                                        ICH_HCR_EL2_VGrp1EIE |         \
                                        ICH_HCR_EL2_VGrp1DIE |         \
                                        ICH_HCR_EL2_TDIR)
Quoted 58 lines
Quoted 58 lines

+
  static inline void vgic_save_state(struct kvm_vcpu *vcpu) {
      if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) vgic_v2_save_state(vcpu);
+    else if (vcpu_is_rec(vcpu))
+        vgic_rmm_save_state(vcpu);
      else __vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3); }

arch/arm64/kvm/vgic/vgic.c +17 -0
@@ -903,10 +920,28 @@ void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
       vgic_prune_ap_list(vcpu);
   }
   +static inline void vgic_rmm_restore_state(struct kvm_vcpu *vcpu)
+{
+    struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
+    int i;
+
+    for (i = 0; i < kvm_vcpu_vgic_nr_lr(vcpu); i++) {
+        vcpu->arch.rec.run->enter.gicv3_lrs[i] = cpu_if->vgic_lr[i];
+        /*
+         * Also populate the rec.run->exit copies so that a late
+         * decision to back out from entering the realm doesn't cause
+         * the state to be lost
+         */
+        vcpu->arch.rec.run->exit.gicv3_lrs[i] = cpu_if->vgic_lr[i];
+    }
+}
+
   static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
   {
       if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
           vgic_v2_restore_state(vcpu);
+    else if (vcpu_is_rec(vcpu))
+        vgic_rmm_restore_state(vcpu);

      else __vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3); }

arch/arm64/kvm/vgic/vgic.c +3 -1
@@ -976,7 +1011,9 @@ void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
     void kvm_vgic_load(struct kvm_vcpu *vcpu)
   {
-    if (unlikely(!irqchip_in_kernel(vcpu->kvm) || !
 vgic_initialized(vcpu->kvm))) {
+    if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
+             !vgic_initialized(vcpu->kvm) ||
+             vcpu_is_rec(vcpu))) {
 
 

          if (has_vhe() && static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) __vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3); return;

arch/arm64/kvm/vgic/vgic.c +1 -1
@@ -990,7 +1027,9 @@ void kvm_vgic_load(struct kvm_vcpu *vcpu)
     void kvm_vgic_put(struct kvm_vcpu *vcpu)
   {
-    if (unlikely(!irqchip_in_kernel(vcpu->kvm) || !
 vgic_initialized(vcpu->kvm))) {
+    if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
Quoted 8 lines
Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c +2 -0
+             !vgic_initialized(vcpu->kvm) ||
+             vcpu_is_rec(vcpu))) {

          if (has_vhe() && static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) __vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);

We could return early here for rec, and skip the unnecessary trap steps. Similar for the vgic_load case.

Ack. I think this was me getting confused during a rebase ;) I'll pull out the "if (unlikely(vcpu_is_rec())" and return early.

Steve

Quoted 7 lines

Rest looks good to me.

Suzuki

Quoted 1 line

          return;

#35 lore raw

PATCH v8 14/43 KVM: arm64: Support timers in realm RECs

In reply to Steven Price

The RMM keeps track of the timer while the realm REC is running, but on exit to the normal world KVM is responsible for handling the timers.

The RMM doesn't provide a mechanism to set the counter offset, so don't expose KVM_CAP_COUNTER_OFFSET for a realm VM.

A later patch adds the support for propagating the timer values from the exit data structure and calling kvm_realm_timers_update().

---
Changes since v7:
* Hide KVM_CAP_COUNTER_OFFSET for realm guests.

---
 arch/arm64/kvm/arch_timer.c  | 48 +++++++++++++++++++++++++++++++++---
 arch/arm64/kvm/arm.c         |  2 +-
 include/kvm/arm_arch_timer.h |  2 ++
 3 files changed, 47 insertions(+), 5 deletions(-)
arch/arm64/kvm/arch_timer.c +44 -4
index 5133dcbfe9f7..3dfc25873be2 100644
@@ -148,6 +148,13 @@ static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)
 
 static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
 {
+	struct kvm_vcpu *vcpu = ctxt->vcpu;
+
+	if (kvm_is_realm(vcpu->kvm)) {
+		WARN_ON(offset);
+		return;
+	}
+
 	if (!ctxt->offset.vm_offset) {
 		WARN(offset, "timer %ld\n", arch_timer_ctx_index(ctxt));
 		return;
@@ -462,6 +469,21 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
 			    timer_ctx);
 }
 
+void kvm_realm_timers_update(struct kvm_vcpu *vcpu)
+{
+	struct arch_timer_cpu *arch_timer = &vcpu->arch.timer_cpu;
+	int i;
+
+	for (i = 0; i < NR_KVM_EL0_TIMERS; i++) {
+		struct arch_timer_context *timer = &arch_timer->timers[i];
+		bool status = timer_get_ctl(timer) & ARCH_TIMER_CTRL_IT_STAT;
+		bool level = kvm_timer_irq_can_fire(timer) && status;
+
+		if (level != timer->irq.level)
+			kvm_timer_update_irq(vcpu, level, timer);
+	}
+}
+
 /* Only called for a fully emulated timer */
 static void timer_emulate(struct arch_timer_context *ctx)
 {
@@ -870,6 +892,8 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
 	if (unlikely(!timer->enabled))
 		return;
 
+	kvm_timer_unblocking(vcpu);
+
 	get_timer_map(vcpu, &map);
 
 	if (static_branch_likely(&has_gic_active_state)) {
@@ -883,8 +907,6 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
 		kvm_timer_vcpu_load_nogic(vcpu);
 	}
 
-	kvm_timer_unblocking(vcpu);
-
 	timer_restore_state(map.direct_vtimer);
 	if (map.direct_ptimer)
 		timer_restore_state(map.direct_ptimer);
@@ -1065,7 +1087,9 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
 
 	ctxt->vcpu = vcpu;
 
-	if (timerid == TIMER_VTIMER)
+	if (kvm_is_realm(vcpu->kvm))
+		ctxt->offset.vm_offset = NULL;
+	else if (timerid == TIMER_VTIMER)
 		ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
 	else
 		ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
@@ -1087,13 +1111,19 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
 {
 	struct arch_timer_cpu *timer = vcpu_timer(vcpu);
+	u64 cntvoff;
 
 	for (int i = 0; i < NR_KVM_TIMERS; i++)
 		timer_context_init(vcpu, i);
 
+	if (kvm_is_realm(vcpu->kvm))
+		cntvoff = 0;
+	else
+		cntvoff = kvm_phys_timer_read();
+
 	/* Synchronize offsets across timers of a VM if not already provided */
 	if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
-		timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
+		timer_set_offset(vcpu_vtimer(vcpu), cntvoff);
 		timer_set_offset(vcpu_ptimer(vcpu), 0);
 	}
 
@@ -1633,6 +1663,13 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)
 		return -EINVAL;
 	}
 
+	/*
+	 * We don't use mapped IRQs for Realms because the RMI doesn't allow
+	 * us setting the LR.HW bit in the VGIC.
+	 */
+	if (vcpu_is_rec(vcpu))
+		return 0;
+
 	get_timer_map(vcpu, &map);
 
 	ret = kvm_vgic_map_phys_irq(vcpu,
@@ -1764,6 +1801,9 @@ int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm,
 	if (offset->reserved)
 		return -EINVAL;
 
+	if (kvm_is_realm(kvm))
+		return -EINVAL;
+
 	mutex_lock(&kvm->lock);
 
 	if (lock_all_vcpus(kvm)) {
arch/arm64/kvm/arm.c +1 -1
index 808d7e479571..7c0bb1b05f4c 100644
@@ -350,10 +350,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_PTP_KVM:
 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
 	case KVM_CAP_IRQFD_RESAMPLE:
-	case KVM_CAP_COUNTER_OFFSET:
 	case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
 		r = 1;
 		break;
+	case KVM_CAP_COUNTER_OFFSET:
 	case KVM_CAP_SET_GUEST_DEBUG:
 		r = !kvm_is_realm(kvm);
 		break;
include/kvm/arm_arch_timer.h +2 -0
index 681cf0c8b9df..f64e317c091b 100644
@@ -113,6 +113,8 @@ int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
 int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
 int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
 
+void kvm_realm_timers_update(struct kvm_vcpu *vcpu);
+
 u64 kvm_phys_timer_read(void);
 
 void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu);
Signature · 1 line
2.43.0
reviewer Reviewed-by on v8 14/43
#36 lore raw

Re: [PATCH v8 14/43] KVM: arm64: Support timers in realm RECs

Quoted 10 lines · Steven Price

The RMM keeps track of the timer while the realm REC is running, but on exit to the normal world KVM is responsible for handling the timers.

The RMM doesn't provide a mechanism to set the counter offset, so don't expose KVM_CAP_COUNTER_OFFSET for a realm VM.

A later patch adds the support for propagating the timer values from the exit data structure and calling kvm_realm_timers_update().

#37 lore raw

PATCH v8 15/43 arm64: RME: Allow VMM to set RIPAS

In reply to Steven Price

Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.

When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.

When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.

---
Changes from v7:
* Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
* Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
* Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
* Several other minor changes to names/return codes.
Changes from v6:
* Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
* Drop the useless flags argument from alloc_delegated_granule().
* Account RTTs allocated for a guest using kvm_account_pgtable_pages().
* Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
* Adapt to rebasing.
* Introduce find_map_level()
* Rename some functions to be clearer.
* Drop the "spare page" functionality.
Changes from v2:
* {alloc,free}_delegated_page() moved from previous patch to this one.
* alloc_delegated_page() now takes a gfp_t flags parameter.
* Fix the reference counting of guestmem pages to avoid leaking memory.
* Several misc code improvements and extra comments.

---
 arch/arm64/include/asm/kvm_rme.h |   5 +
 arch/arm64/kvm/mmu.c             |   8 +-
 arch/arm64/kvm/rme.c             | 384 +++++++++++++++++++++++++++++++
 3 files changed, 394 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rme.h +5 -0
index 9bcad6ec5dbb..b916db8565a2 100644
@@ -101,6 +101,11 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
 int kvm_create_rec(struct kvm_vcpu *vcpu);
 void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
+void kvm_realm_unmap_range(struct kvm *kvm,
+			   unsigned long ipa,
+			   unsigned long size,
+			   bool unmap_private);
+
 static inline bool kvm_realm_is_private_address(struct realm *realm,
 						unsigned long addr)
 {
arch/arm64/kvm/mmu.c +5 -3
index d80a9d408f71..71c04259e39f 100644
@@ -323,6 +323,7 @@ static void invalidate_icache_guest_page(void *va, size_t size)
  * @start: The intermediate physical base address of the range to unmap
  * @size:  The size of the area to unmap
  * @may_block: Whether or not we are permitted to block
+ * @only_shared: If true then protected mappings should not be unmapped
  *
  * Clear a range of stage-2 mappings, lowering the various ref-counts.  Must
  * be called while holding mmu_lock (unless for freeing the stage2 pgd before
@@ -330,7 +331,7 @@ static void invalidate_icache_guest_page(void *va, size_t size)
  * with things behind our backs.
  */
 static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
-				 bool may_block)
+				 bool may_block, bool only_shared)
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 	phys_addr_t end = start + size;
@@ -344,7 +345,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64
 void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
 			    u64 size, bool may_block)
 {
-	__unmap_stage2_range(mmu, start, size, may_block);
+	__unmap_stage2_range(mmu, start, size, may_block, false);
 }
 
 void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
@@ -1975,7 +1976,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 
 	__unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
 			     (range->end - range->start) << PAGE_SHIFT,
-			     range->may_block);
+			     range->may_block,
+			     !(range->attr_filter & KVM_FILTER_PRIVATE));
 
 	kvm_nested_s2_unmap(kvm, range->may_block);
 	return false;
arch/arm64/kvm/rme.c +384 -0

420 lines not on this page · raw message

Signature · 1 line
2.43.0
#38 lore raw

Re: [PATCH v8 15/43] arm64: RME: Allow VMM to set RIPAS

Quoted 136 lines · Steven Price

Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.

When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.

When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.

---
Changes from v7:
* Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
* Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
* Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
* Several other minor changes to names/return codes.
Changes from v6:
* Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
* Drop the useless flags argument from alloc_delegated_granule().
* Account RTTs allocated for a guest using kvm_account_pgtable_pages().
* Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
* Adapt to rebasing.
* Introduce find_map_level()
* Rename some functions to be clearer.
* Drop the "spare page" functionality.
Changes from v2:
* {alloc,free}_delegated_page() moved from previous patch to this one.
* alloc_delegated_page() now takes a gfp_t flags parameter.
* Fix the reference counting of guestmem pages to avoid leaking memory.
* Several misc code improvements and extra comments.

---
  arch/arm64/include/asm/kvm_rme.h |   5 +
  arch/arm64/kvm/mmu.c             |   8 +-
  arch/arm64/kvm/rme.c             | 384 +++++++++++++++++++++++++++++++
  3 files changed, 394 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rme.h +5 -0
index 9bcad6ec5dbb..b916db8565a2 100644
@@ -101,6 +101,11 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
  int kvm_create_rec(struct kvm_vcpu *vcpu);
  void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
+void kvm_realm_unmap_range(struct kvm *kvm,
+			   unsigned long ipa,
+			   unsigned long size,
+			   bool unmap_private);
+
  static inline bool kvm_realm_is_private_address(struct realm *realm,
  						unsigned long addr)
  {
arch/arm64/kvm/mmu.c +5 -3
index d80a9d408f71..71c04259e39f 100644
@@ -323,6 +323,7 @@ static void invalidate_icache_guest_page(void *va, size_t size)
   * @start: The intermediate physical base address of the range to unmap
   * @size:  The size of the area to unmap
   * @may_block: Whether or not we are permitted to block
+ * @only_shared: If true then protected mappings should not be unmapped
   *
   * Clear a range of stage-2 mappings, lowering the various ref-counts.  Must
   * be called while holding mmu_lock (unless for freeing the stage2 pgd before
@@ -330,7 +331,7 @@ static void invalidate_icache_guest_page(void *va, size_t size)
   * with things behind our backs.
   */
  static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
-				 bool may_block)
+				 bool may_block, bool only_shared)
  {
  	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
  	phys_addr_t end = start + size;
@@ -344,7 +345,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64
  void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
  			    u64 size, bool may_block)
  {
-	__unmap_stage2_range(mmu, start, size, may_block);
+	__unmap_stage2_range(mmu, start, size, may_block, false);
  }
 
  void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
@@ -1975,7 +1976,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 
  	__unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
  			     (range->end - range->start) << PAGE_SHIFT,
-			     range->may_block);
+			     range->may_block,
+			     !(range->attr_filter & KVM_FILTER_PRIVATE));
 
  	kvm_nested_s2_unmap(kvm, range->may_block);
  	return false;
arch/arm64/kvm/rme.c +18 -0
index 1239eb07aca6..33eb793d8bdb 100644
@@ -87,6 +87,51 @@ static int get_start_level(struct realm *realm)
  	return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
  }
 
+static int find_map_level(struct realm *realm,
+			  unsigned long start,
+			  unsigned long end)
+{
+	int level = RMM_RTT_MAX_LEVEL;
+
+	while (level > get_start_level(realm)) {
+		unsigned long map_size = rme_rtt_level_mapsize(level - 1);
+
+		if (!IS_ALIGNED(start, map_size) ||
+		    (start + map_size) > end)
+			break;
+
+		level--;
+	}
+
+	return level;
+}

arch/arm64/kvm/rme.c static int get_start_level(struct realm *realm) · line 117

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+
+static phys_addr_t alloc_delegated_granule(struct kvm_mmu_memory_cache *mc)
+{
+	phys_addr_t phys;
+	void *virt;
+
+	if (mc)
+		virt = kvm_mmu_memory_cache_alloc(mc);
+	else
+		virt = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);

It would be more safe to use (GFP_ATOMIC | __GFP_ZERO | __GFP_ACCOUNT), consistent to the flags used by kvm_mmu_memory_cache_alloc().

Quoted 294 lines
arch/arm64/kvm/rme.c +266 -0
+
+	if (!virt)
+		return PHYS_ADDR_MAX;
+
+	phys = virt_to_phys(virt);
+
+	if (rmi_granule_delegate(phys)) {
+		free_page((unsigned long)virt);
+
+		return PHYS_ADDR_MAX;
+	}
+
+	kvm_account_pgtable_pages(virt, 1);
+
+	return phys;
+}
+
  static void free_delegated_granule(phys_addr_t phys)
  {
  	if (WARN_ON(rmi_granule_undelegate(phys))) {
@@ -99,6 +144,154 @@ static void free_delegated_granule(phys_addr_t phys)
  	free_page((unsigned long)phys_to_virt(phys));
  }
 
+static int realm_rtt_create(struct realm *realm,
+			    unsigned long addr,
+			    int level,
+			    phys_addr_t phys)
+{
+	addr = ALIGN_DOWN(addr, rme_rtt_level_mapsize(level - 1));
+	return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level);
+}
+
+static int realm_rtt_fold(struct realm *realm,
+			  unsigned long addr,
+			  int level,
+			  phys_addr_t *rtt_granule)
+{
+	unsigned long out_rtt;
+	int ret;
+
+	ret = rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt);
+
+	if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS && rtt_granule)
+		*rtt_granule = out_rtt;
+
+	return ret;
+}
+
+static int realm_destroy_private_granule(struct realm *realm,
+					 unsigned long ipa,
+					 unsigned long *next_addr,
+					 phys_addr_t *out_rtt)
+{
+	unsigned long rd = virt_to_phys(realm->rd);
+	unsigned long rtt_addr;
+	phys_addr_t rtt;
+	int ret;
+
+retry:
+	ret = rmi_data_destroy(rd, ipa, &rtt_addr, next_addr);
+	if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+		if (*next_addr > ipa)
+			return 0; /* UNASSIGNED */
+		rtt = alloc_delegated_granule(NULL);
+		if (WARN_ON(rtt == PHYS_ADDR_MAX))
+			return -ENOMEM;
+		/*
+		 * ASSIGNED - ipa is mapped as a block, so split. The index
+		 * from the return code should be 2 otherwise it appears
+		 * there's a huge page bigger than KVM currently supports
+		 */
+		WARN_ON(RMI_RETURN_INDEX(ret) != 2);
+		ret = realm_rtt_create(realm, ipa, 3, rtt);
+		if (WARN_ON(ret)) {
+			free_delegated_granule(rtt);
+			return -ENXIO;
+		}
+		goto retry;
+	} else if (WARN_ON(ret)) {
+		return -ENXIO;
+	}
+
+	ret = rmi_granule_undelegate(rtt_addr);
+	if (WARN_ON(ret))
+		return -ENXIO;
+
+	*out_rtt = rtt_addr;
+
+	return 0;
+}
+
+static int realm_unmap_private_page(struct realm *realm,
+				    unsigned long ipa,
+				    unsigned long *next_addr)
+{
+	unsigned long end = ALIGN(ipa + 1, PAGE_SIZE);
+	unsigned long addr;
+	phys_addr_t out_rtt = PHYS_ADDR_MAX;
+	int ret;
+
+	for (addr = ipa; addr < end; addr = *next_addr) {
+		ret = realm_destroy_private_granule(realm, addr, next_addr,
+						    &out_rtt);
+		if (ret)
+			return ret;
+	}
+
+	if (out_rtt != PHYS_ADDR_MAX)
+		free_page((unsigned long)phys_to_virt(out_rtt));
+
+	return 0;
+}
+
+static void realm_unmap_shared_range(struct kvm *kvm,
+				     int level,
+				     unsigned long start,
+				     unsigned long end)
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned long rd = virt_to_phys(realm->rd);
+	ssize_t map_size = rme_rtt_level_mapsize(level);
+	unsigned long next_addr, addr;
+	unsigned long shared_bit = BIT(realm->ia_bits - 1);
+
+	if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+		return;
+
+	start |= shared_bit;
+	end |= shared_bit;
+
+	for (addr = start; addr < end; addr = next_addr) {
+		unsigned long align_addr = ALIGN(addr, map_size);
+		int ret;
+
+		next_addr = ALIGN(addr + 1, map_size);
+
+		if (align_addr != addr || next_addr > end) {
+			/* Need to recurse deeper */
+			if (addr < align_addr)
+				next_addr = align_addr;
+			realm_unmap_shared_range(kvm, level + 1, addr,
+						 min(next_addr, end));
+			continue;
+		}
+
+		ret = rmi_rtt_unmap_unprotected(rd, addr, level, &next_addr);
+		switch (RMI_RETURN_STATUS(ret)) {
+		case RMI_SUCCESS:
+			break;
+		case RMI_ERROR_RTT:
+			if (next_addr == addr) {
+				/*
+				 * There's a mapping here, but it's not a block
+				 * mapping, so reset next_addr to the next block
+				 * boundary and recurse to clear out the pages
+				 * one level deeper.
+				 */
+				next_addr = ALIGN(addr + 1, map_size);
+				realm_unmap_shared_range(kvm, level + 1, addr,
+							 next_addr);
+			}
+			break;
+		default:
+			WARN_ON(1);
+			return;
+		}
+
+		cond_resched_rwlock_write(&kvm->mmu_lock);
+	}
+}
+
  /* Calculate the number of s2 root rtts needed */
  static int realm_num_root_rtts(struct realm *realm)
  {
@@ -209,6 +402,40 @@ static int realm_rtt_destroy(struct realm *realm, unsigned long addr,
  	return ret;
  }
 
+static int realm_create_rtt_levels(struct realm *realm,
+				   unsigned long ipa,
+				   int level,
+				   int max_level,
+				   struct kvm_mmu_memory_cache *mc)
+{
+	if (level == max_level)
+		return 0;
+
+	while (level++ < max_level) {
+		phys_addr_t rtt = alloc_delegated_granule(mc);
+		int ret;
+
+		if (rtt == PHYS_ADDR_MAX)
+			return -ENOMEM;
+
+		ret = realm_rtt_create(realm, ipa, level, rtt);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT &&
+		    RMI_RETURN_INDEX(ret) == level) {
+			/* The RTT already exists, continue */
+			continue;
+		}
+		if (ret) {
+			WARN(1, "Failed to create RTT at level %d: %d\n",
+			     level, ret);
+			free_delegated_granule(rtt);
+			return -ENXIO;
+		}
+	}
+
+	return 0;
+}
+
  static int realm_tear_down_rtt_level(struct realm *realm, int level,
  				     unsigned long start, unsigned long end)
  {
@@ -299,6 +526,61 @@ static int realm_tear_down_rtt_range(struct realm *realm,
  					 start, end);
  }
 
+/*
+ * Returns 0 on successful fold, a negative value on error, a positive value if
+ * we were not able to fold all tables at this level.
+ */
+static int realm_fold_rtt_level(struct realm *realm, int level,
+				unsigned long start, unsigned long end)
+{
+	int not_folded = 0;
+	ssize_t map_size;
+	unsigned long addr, next_addr;
+
+	if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+		return -EINVAL;
+
+	map_size = rme_rtt_level_mapsize(level - 1);
+
+	for (addr = start; addr < end; addr = next_addr) {
+		phys_addr_t rtt_granule;
+		int ret;
+		unsigned long align_addr = ALIGN(addr, map_size);
+
+		next_addr = ALIGN(addr + 1, map_size);
+
+		ret = realm_rtt_fold(realm, align_addr, level, &rtt_granule);
+
+		switch (RMI_RETURN_STATUS(ret)) {
+		case RMI_SUCCESS:
+			free_delegated_granule(rtt_granule);
+			break;
+		case RMI_ERROR_RTT:
+			if (level == RMM_RTT_MAX_LEVEL ||
+			    RMI_RETURN_INDEX(ret) < level) {
+				not_folded++;
+				break;
+			}
+			/* Recurse a level deeper */
+			ret = realm_fold_rtt_level(realm,
+						   level + 1,
+						   addr,
+						   next_addr);
+			if (ret < 0)
+				return ret;
+			else if (ret == 0)
+				/* Try again at this level */
+				next_addr = addr;
+			break;
+		default:
+			WARN_ON(1);
+			return -ENXIO;
+		}
+	}
+
+	return not_folded;
+}
+
  void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits)
  {
  	struct realm *realm = &kvm->arch.realm;
@@ -306,6 +588,96 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits)
  	WARN_ON(realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits)));
  }
 
+static void realm_unmap_private_range(struct kvm *kvm,
+				      unsigned long start,
+				      unsigned long end)
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned long next_addr, addr;
+	int ret;
+
+	for (addr = start; addr < end; addr = next_addr) {
+		ret = realm_unmap_private_page(realm, addr, &next_addr);
+
+		if (ret)

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 612

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+			break;
+	}
+
+	realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+			     start, end);
+}
+
+void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
+			   unsigned long size, bool unmap_private)
+{

This function can be called by the MMU notifier to invalidate the mapping for the specified IPA range. However, we're missing one parameter to indicate if the request from the MMU notifier is blockable or not. We're having the imprecise assumption that all requests are blockable, which isn't always true. With this assumption, cond_resched_rwlock_write(&kvm->mmu_lock) is always called in realm_unmap_shared_range().

Another issue is that cond_resched_rwlock_write(&kvm->mmu_lock) is missed if the request is blockable in realm_unmap_private_page().

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 619

Quoted 7 lines
arch/arm64/kvm/rme.c +7 -0
+	unsigned long end = start + size;
+	struct realm *realm = &kvm->arch.realm;
+
+	end = min(BIT(realm->ia_bits - 1), end);
+
+	if (realm->state == REALM_STATE_NONE)
+		return;
	if (!kvm_realm_is_created(kvm)))
		return;
Quoted 33 lines
arch/arm64/kvm/rme.c +33 -0
+
+	realm_unmap_shared_range(kvm, find_map_level(realm, start, end),
+				 start, end);
+	if (unmap_private)
+		realm_unmap_private_range(kvm, start, end);
+}
+
+static int realm_init_ipa_state(struct realm *realm,
+				unsigned long ipa,
+				unsigned long end)
+{
+	phys_addr_t rd_phys = virt_to_phys(realm->rd);
+	int ret;
+
+	while (ipa < end) {
+		unsigned long next;
+
+		ret = rmi_rtt_init_ripas(rd_phys, ipa, end, &next);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+			int err_level = RMI_RETURN_INDEX(ret);
+			int level = find_map_level(realm, ipa, end);
+
+			if (WARN_ON(err_level >= level))
+				return -ENXIO;
+
+			ret = realm_create_rtt_levels(realm, ipa,
+						      err_level,
+						      level, NULL);
+			if (ret)
+				return ret;
+			/* Retry with the RTT levels in place */
+			continue;

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 662

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+		} else if (WARN_ON(ret)) {
+			return -ENXIO;
+		}
+
+		ipa = next;
+	}
+
+	return 0;
+}
+

The readability would be better if the explicit 'continue' can be avoid, something like below:

	switch (RMI_RETURN_STATUS(ret)) {
	case RMI_SUCCESS:
		ipa = next;
		break;
	case RMI_ERROR_RTT:
		:
		ret = realm_create_rtt_levels(realm, ipa, error_level, level, NULL);
		if (ret)
			return ret;
		break;
	default:
		WARN_ON(1);
		return -ENXIO;
	}

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 673

Quoted 11 lines
arch/arm64/kvm/rme.c +11 -0
+static int kvm_init_ipa_range_realm(struct kvm *kvm,
+				    struct arm_rme_init_ripas *args)
+{
+	gpa_t addr, end;
+	struct realm *realm = &kvm->arch.realm;
+
+	addr = args->base;
+	end = addr + args->size;
+
+	if (end < addr)
+		return -EINVAL;

The check needs to cover 'end <= addr'. RMI_ERROR_INPUT is returned from RMM::smc_rtt_init_ripas() if 'end' is equal to 'addr', but we're returning 0, inconsistent to that.

	if (end <= addr)
		return -EINVAL;

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 676

Quoted 3 lines
arch/arm64/kvm/rme.c +3 -0
+
+	if (kvm_realm_state(kvm) != REALM_STATE_NEW)
+		return -EPERM;

To keep the consistency, kvm_realm_is_created() can be used here.

	if (kvm_realm_is_created(kvm))
		return -EPERM;
Quoted 16 lines
arch/arm64/kvm/rme.c +9 -0
+
+	return realm_init_ipa_state(realm, addr, end);
+}
+
  /* Protects access to rme_vmid_bitmap */
  static DEFINE_SPINLOCK(rme_vmid_lock);
  static unsigned long *rme_vmid_bitmap;
@@ -431,6 +803,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
  	case KVM_CAP_ARM_RME_CREATE_REALM:
  		r = kvm_create_realm(kvm);
  		break;
+	case KVM_CAP_ARM_RME_INIT_RIPAS_REALM: {
+		struct arm_rme_init_ripas args;
+		void __user *argp = u64_to_user_ptr(cap->args[1]);
+
+		if (copy_from_user(&args, argp, sizeof(args))) {

arch/arm64/kvm/rme.c int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 820

Quoted 10 lines
arch/arm64/kvm/rme.c +7 -0
+			r = -EFAULT;
+			break;
+		}
+
+		r = kvm_init_ipa_range_realm(kvm, &args);
+		break;
+	}
  	default:
  		r = -EINVAL;
  		break;

Thanks,
Gavin

#39 lore raw

Re: [PATCH v8 15/43] arm64: RME: Allow VMM to set RIPAS

Quoted 150 lines · Gavin Shan
Quoted 149 lines · Steven Price

Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.

When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.

When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.

---
Changes from v7:
  * Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
  * Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
  * Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
  * Several other minor changes to names/return codes. Changes from v6:
  * Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
  * Drop the useless flags argument from alloc_delegated_granule().
  * Account RTTs allocated for a guest using kvm_account_pgtable_pages().
  * Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
  * Adapt to rebasing.
  * Introduce find_map_level()
  * Rename some functions to be clearer.
  * Drop the "spare page" functionality.
Changes from v2:
  * {alloc,free}_delegated_page() moved from previous patch to this one.
  * alloc_delegated_page() now takes a gfp_t flags parameter.
  * Fix the reference counting of guestmem pages to avoid leaking memory.
  * Several misc code improvements and extra comments.
---
  arch/arm64/include/asm/kvm_rme.h |   5 + arch/arm64/kvm/mmu.c             |   8 +- arch/arm64/kvm/rme.c             | 384 +++++++++++++++++++++++++++++++

  3 files changed, 394 insertions(+), 3 deletions(-)
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index 9bcad6ec5dbb..b916db8565a2 100644

arch/arm64/include/asm/kvm_rme.h +4 -0
@@ -101,6 +101,11 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32
 ia_bits);
   int kvm_create_rec(struct kvm_vcpu *vcpu);
   void kvm_destroy_rec(struct kvm_vcpu *vcpu);
   +void kvm_realm_unmap_range(struct kvm *kvm,
+               unsigned long ipa,
+               unsigned long size,
+               bool unmap_private);
+
   static inline bool kvm_realm_is_private_address(struct realm *realm,
                           unsigned long addr)

  {

arch/arm64/kvm/mmu.c +1 -0
index d80a9d408f71..71c04259e39f 100644
@@ -323,6 +323,7 @@ static void invalidate_icache_guest_page(void *va,
 size_t size)
    * @start: The intermediate physical base address of the range to unmap
    * @size:  The size of the area to unmap
    * @may_block: Whether or not we are permitted to block
+ * @only_shared: If true then protected mappings should not be unmapped
    *
    * Clear a range of stage-2 mappings, lowering the various ref-

counts.  Must
   * be called while holding mmu_lock (unless for freeing the stage2 pgd before

arch/arm64/kvm/mmu.c +0 -0
@@ -330,7 +331,7 @@ static void invalidate_icache_guest_page(void *va,
 size_t size)
    * with things behind our backs.
    */

  static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
-                 bool may_block)
+                 bool may_block, bool only_shared)
  {
      struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
      phys_addr_t end = start + size;

arch/arm64/kvm/mmu.c +1 -1
@@ -344,7 +345,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu
 *mmu, phys_addr_t start, u64
   void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
                   u64 size, bool may_block)
   {
-    __unmap_stage2_range(mmu, start, size, may_block);
+    __unmap_stage2_range(mmu, start, size, may_block, false);
   }

    void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)

arch/arm64/kvm/mmu.c +2 -1
@@ -1975,7 +1976,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct
 kvm_gfn_range *range)
         __unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
                    (range->end - range->start) << PAGE_SHIFT,
-                 range->may_block);
+                 range->may_block,
+                 !(range->attr_filter & KVM_FILTER_PRIVATE));
         kvm_nested_s2_unmap(kvm, range->may_block);
       return false;
arch/arm64/kvm/rme.c +24 -0
index 1239eb07aca6..33eb793d8bdb 100644
@@ -87,6 +87,51 @@ static int get_start_level(struct realm *realm)
       return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
   }
   +static int find_map_level(struct realm *realm,
+              unsigned long start,
+              unsigned long end)
+{
+    int level = RMM_RTT_MAX_LEVEL;
+
+    while (level > get_start_level(realm)) {
+        unsigned long map_size = rme_rtt_level_mapsize(level - 1);
+
+        if (!IS_ALIGNED(start, map_size) ||
+            (start + map_size) > end)
+            break;
+
+        level--;
+    }
+
+    return level;
+}
+
+static phys_addr_t alloc_delegated_granule(struct
 kvm_mmu_memory_cache *mc)
+{
+    phys_addr_t phys;
+    void *virt;
+
+    if (mc)
Quoted 8 lines

arch/arm64/kvm/rme.c static int get_start_level(struct realm *realm) · line 117

Quoted 3 lines
arch/arm64/kvm/rme.c +3 -0
+        virt = kvm_mmu_memory_cache_alloc(mc);
+    else
+        virt = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);

It would be more safe to use (GFP_ATOMIC | __GFP_ZERO | __GFP_ACCOUNT), consistent
to the flags used by kvm_mmu_memory_cache_alloc().

Ack

Quoted 316 lines
Quoted 294 lines
arch/arm64/kvm/rme.c +17 -0
+
+    if (!virt)
+        return PHYS_ADDR_MAX;
+
+    phys = virt_to_phys(virt);
+
+    if (rmi_granule_delegate(phys)) {
+        free_page((unsigned long)virt);
+
+        return PHYS_ADDR_MAX;
+    }
+
+    kvm_account_pgtable_pages(virt, 1);
+
+    return phys;
+}
+
   static void free_delegated_granule(phys_addr_t phys)
   {

      if (WARN_ON(rmi_granule_undelegate(phys))) {

arch/arm64/kvm/rme.c +180 -0
@@ -99,6 +144,154 @@ static void free_delegated_granule(phys_addr_t phys)
       free_page((unsigned long)phys_to_virt(phys));
   }
   +static int realm_rtt_create(struct realm *realm,
+                unsigned long addr,
+                int level,
+                phys_addr_t phys)
+{
+    addr = ALIGN_DOWN(addr, rme_rtt_level_mapsize(level - 1));
+    return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level);
+}
+
+static int realm_rtt_fold(struct realm *realm,
+              unsigned long addr,
+              int level,
+              phys_addr_t *rtt_granule)
+{
+    unsigned long out_rtt;
+    int ret;
+
+    ret = rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt);
+
+    if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS && rtt_granule)
+        *rtt_granule = out_rtt;
+
+    return ret;
+}
+
+static int realm_destroy_private_granule(struct realm *realm,
+                     unsigned long ipa,
+                     unsigned long *next_addr,
+                     phys_addr_t *out_rtt)
+{
+    unsigned long rd = virt_to_phys(realm->rd);
+    unsigned long rtt_addr;
+    phys_addr_t rtt;
+    int ret;
+
+retry:
+    ret = rmi_data_destroy(rd, ipa, &rtt_addr, next_addr);
+    if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+        if (*next_addr > ipa)
+            return 0; /* UNASSIGNED */
+        rtt = alloc_delegated_granule(NULL);
+        if (WARN_ON(rtt == PHYS_ADDR_MAX))
+            return -ENOMEM;
+        /*
+         * ASSIGNED - ipa is mapped as a block, so split. The index
+         * from the return code should be 2 otherwise it appears
+         * there's a huge page bigger than KVM currently supports
+         */
+        WARN_ON(RMI_RETURN_INDEX(ret) != 2);
+        ret = realm_rtt_create(realm, ipa, 3, rtt);
+        if (WARN_ON(ret)) {
+            free_delegated_granule(rtt);
+            return -ENXIO;
+        }
+        goto retry;
+    } else if (WARN_ON(ret)) {
+        return -ENXIO;
+    }
+
+    ret = rmi_granule_undelegate(rtt_addr);
+    if (WARN_ON(ret))
+        return -ENXIO;
+
+    *out_rtt = rtt_addr;
+
+    return 0;
+}
+
+static int realm_unmap_private_page(struct realm *realm,
+                    unsigned long ipa,
+                    unsigned long *next_addr)
+{
+    unsigned long end = ALIGN(ipa + 1, PAGE_SIZE);
+    unsigned long addr;
+    phys_addr_t out_rtt = PHYS_ADDR_MAX;
+    int ret;
+
+    for (addr = ipa; addr < end; addr = *next_addr) {
+        ret = realm_destroy_private_granule(realm, addr, next_addr,
+                            &out_rtt);
+        if (ret)
+            return ret;
+    }
+
+    if (out_rtt != PHYS_ADDR_MAX)
+        free_page((unsigned long)phys_to_virt(out_rtt));
+
+    return 0;
+}
+
+static void realm_unmap_shared_range(struct kvm *kvm,
+                     int level,
+                     unsigned long start,
+                     unsigned long end)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned long rd = virt_to_phys(realm->rd);
+    ssize_t map_size = rme_rtt_level_mapsize(level);
+    unsigned long next_addr, addr;
+    unsigned long shared_bit = BIT(realm->ia_bits - 1);
+
+    if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+        return;
+
+    start |= shared_bit;
+    end |= shared_bit;
+
+    for (addr = start; addr < end; addr = next_addr) {
+        unsigned long align_addr = ALIGN(addr, map_size);
+        int ret;
+
+        next_addr = ALIGN(addr + 1, map_size);
+
+        if (align_addr != addr || next_addr > end) {
+            /* Need to recurse deeper */
+            if (addr < align_addr)
+                next_addr = align_addr;
+            realm_unmap_shared_range(kvm, level + 1, addr,
+                         min(next_addr, end));
+            continue;
+        }
+
+        ret = rmi_rtt_unmap_unprotected(rd, addr, level, &next_addr);
+        switch (RMI_RETURN_STATUS(ret)) {
+        case RMI_SUCCESS:
+            break;
+        case RMI_ERROR_RTT:
+            if (next_addr == addr) {
+                /*
+                 * There's a mapping here, but it's not a block
+                 * mapping, so reset next_addr to the next block
+                 * boundary and recurse to clear out the pages
+                 * one level deeper.
+                 */
+                next_addr = ALIGN(addr + 1, map_size);
+                realm_unmap_shared_range(kvm, level + 1, addr,
+                             next_addr);
+            }
+            break;
+        default:
+            WARN_ON(1);
+            return;
+        }
+
+        cond_resched_rwlock_write(&kvm->mmu_lock);
+    }
+}
+
   /* Calculate the number of s2 root rtts needed */
   static int realm_num_root_rtts(struct realm *realm)
   {
@@ -209,6 +402,40 @@ static int realm_rtt_destroy(struct realm *realm,
 unsigned long addr,
       return ret;
   }
   +static int realm_create_rtt_levels(struct realm *realm,
+                   unsigned long ipa,
+                   int level,
+                   int max_level,
+                   struct kvm_mmu_memory_cache *mc)
+{
+    if (level == max_level)
+        return 0;
+
+    while (level++ < max_level) {
+        phys_addr_t rtt = alloc_delegated_granule(mc);
+        int ret;
+
+        if (rtt == PHYS_ADDR_MAX)
+            return -ENOMEM;
+
+        ret = realm_rtt_create(realm, ipa, level, rtt);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT &&
+            RMI_RETURN_INDEX(ret) == level) {
+            /* The RTT already exists, continue */
+            continue;
+        }
+        if (ret) {
+            WARN(1, "Failed to create RTT at level %d: %d\n",
+                 level, ret);
+            free_delegated_granule(rtt);
+            return -ENXIO;
+        }
+    }
+
+    return 0;
+}
+
   static int realm_tear_down_rtt_level(struct realm *realm, int level,
                        unsigned long start, unsigned long end)

  {

arch/arm64/kvm/rme.c +0 -0
@@ -299,6 +526,61 @@ static int realm_tear_down_rtt_range(struct realm
 *realm,
                        start, end);
   }

  +/*
+ * Returns 0 on successful fold, a negative value on error, a positive value if
+ * we were not able to fold all tables at this level.
+ */

arch/arm64/kvm/rme.c +51 -0
+static int realm_fold_rtt_level(struct realm *realm, int level,
+                unsigned long start, unsigned long end)
+{
+    int not_folded = 0;
+    ssize_t map_size;
+    unsigned long addr, next_addr;
+
+    if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+        return -EINVAL;
+
+    map_size = rme_rtt_level_mapsize(level - 1);
+
+    for (addr = start; addr < end; addr = next_addr) {
+        phys_addr_t rtt_granule;
+        int ret;
+        unsigned long align_addr = ALIGN(addr, map_size);
+
+        next_addr = ALIGN(addr + 1, map_size);
+
+        ret = realm_rtt_fold(realm, align_addr, level, &rtt_granule);
+
+        switch (RMI_RETURN_STATUS(ret)) {
+        case RMI_SUCCESS:
+            free_delegated_granule(rtt_granule);
+            break;
+        case RMI_ERROR_RTT:
+            if (level == RMM_RTT_MAX_LEVEL ||
+                RMI_RETURN_INDEX(ret) < level) {
+                not_folded++;
+                break;
+            }
+            /* Recurse a level deeper */
+            ret = realm_fold_rtt_level(realm,
+                           level + 1,
+                           addr,
+                           next_addr);
+            if (ret < 0)
+                return ret;
+            else if (ret == 0)
+                /* Try again at this level */
+                next_addr = addr;
+            break;
+        default:
+            WARN_ON(1);
+            return -ENXIO;
+        }
+    }
+
+    return not_folded;
+}
+

  void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) {
      struct realm *realm = &kvm->arch.realm;

arch/arm64/kvm/rme.c +11 -0
@@ -306,6 +588,96 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32
 ia_bits)
       WARN_ON(realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits)));
   }
   +static void realm_unmap_private_range(struct kvm *kvm,
+                      unsigned long start,
+                      unsigned long end)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned long next_addr, addr;
+    int ret;
+
+    for (addr = start; addr < end; addr = next_addr) {
+        ret = realm_unmap_private_page(realm, addr, &next_addr);
+
+        if (ret)

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 · line 612

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+            break;
+    }
+
+    realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+                 start, end);
+}
+
+void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
+               unsigned long size, bool unmap_private)
+{

This function can be called by the MMU notifier to invalidate the mapping for
the specified IPA range. However, we're missing one parameter to indicate if
the request from the MMU notifier is blockable or not. We're having the imprecise
assumption that all requests are blockable, which isn't always true. With this
assumption, cond_resched_rwlock_write(&kvm->mmu_lock) is always called in realm_unmap_shared_range().

Quoted 3 lines

Another issue is that cond_resched_rwlock_write(&kvm->mmu_lock) is missed if
the request is blockable in realm_unmap_private_page().

Thanks, that makes a lot of sense. I'd got myself rather confused about whether this function could block or not. Somehow I'd completely missed the may_block flag in __unmap_stage2_range().

Quoted 10 lines

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 619

Quoted 7 lines
arch/arm64/kvm/rme.c +7 -0
+    unsigned long end = start + size;
+    struct realm *realm = &kvm->arch.realm;
+
+    end = min(BIT(realm->ia_bits - 1), end);
+
+    if (realm->state == REALM_STATE_NONE)
+        return;

    if (!kvm_realm_is_created(kvm)))
        return;

Ack

Quoted 48 lines
Quoted 33 lines
arch/arm64/kvm/rme.c +33 -0
+
+    realm_unmap_shared_range(kvm, find_map_level(realm, start, end),
+                 start, end);
+    if (unmap_private)
+        realm_unmap_private_range(kvm, start, end);
+}
+
+static int realm_init_ipa_state(struct realm *realm,
+                unsigned long ipa,
+                unsigned long end)
+{
+    phys_addr_t rd_phys = virt_to_phys(realm->rd);
+    int ret;
+
+    while (ipa < end) {
+        unsigned long next;
+
+        ret = rmi_rtt_init_ripas(rd_phys, ipa, end, &next);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            int err_level = RMI_RETURN_INDEX(ret);
+            int level = find_map_level(realm, ipa, end);
+
+            if (WARN_ON(err_level >= level))
+                return -ENXIO;
+
+            ret = realm_create_rtt_levels(realm, ipa,
+                              err_level,
+                              level, NULL);
+            if (ret)
+                return ret;
+            /* Retry with the RTT levels in place */
+            continue;

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 662

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+        } else if (WARN_ON(ret)) {
+            return -ENXIO;
+        }
+
+        ipa = next;
+    }
+
+    return 0;
+}
+

The readability would be better if the explicit 'continue' can be avoid, something
like below:

Quoted 15 lines

    switch (RMI_RETURN_STATUS(ret)) {
    case RMI_SUCCESS:
        ipa = next;
        break;
    case RMI_ERROR_RTT:
        :
        ret = realm_create_rtt_levels(realm, ipa, error_level, level, NULL);
        if (ret)
            return ret;
        break;
    default:
        WARN_ON(1);
        return -ENXIO;
    }

I have to admit to quite liking 'continue' for retry loops, but in this case your switch statement seems just as readable so I'll switch this.

Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/rme.c +7 -0
+static int kvm_init_ipa_range_realm(struct kvm *kvm,
+                    struct arm_rme_init_ripas *args)
+{
+    gpa_t addr, end;
+    struct realm *realm = &kvm->arch.realm;
+
+    addr = args->base;
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 673

Quoted 4 lines
arch/arm64/kvm/rme.c +4 -0
+    end = addr + args->size;
+
+    if (end < addr)
+        return -EINVAL;

The check needs to cover 'end <= addr'. RMI_ERROR_INPUT is returned from RMM::smc_rtt_init_ripas()
if 'end' is equal to 'addr', but we're returning 0, inconsistent to that.

I agree we're different to smc_rtt_init_ripas(), but I don't really see why we should prevent args->size==0. Calling the low level SMC in that case would clearly be wrong (the kernel should be validating and that would show a lack of validation), but we handle that with the while loop in realm_init_ipa_state().

Do you think it's important to define this uAPI to disallow size==0?

Quoted 11 lines

    if (end <= addr)
        return -EINVAL;

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 676

Quoted 3 lines
arch/arm64/kvm/rme.c +3 -0
+
+    if (kvm_realm_state(kvm) != REALM_STATE_NEW)
+        return -EPERM;

To keep the consistency, kvm_realm_is_created() can be used here.

    if (kvm_realm_is_created(kvm))
        return -EPERM;

This isn't the same - kvm_realm_is_create() is checking for REALM_STATE_NONE, but we want to check for REALM_STATE_NEW.

Thanks,
Steve

Quoted 31 lines
Quoted 24 lines
arch/arm64/kvm/rme.c +4 -0
+
+    return realm_init_ipa_state(realm, addr, end);
+}
+
   /* Protects access to rme_vmid_bitmap */
   static DEFINE_SPINLOCK(rme_vmid_lock);

  static unsigned long *rme_vmid_bitmap;

arch/arm64/kvm/rme.c +0 -0
@@ -431,6 +803,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct
 kvm_enable_cap *cap)

      case KVM_CAP_ARM_RME_CREATE_REALM:
          r = kvm_create_realm(kvm);
          break;
+    case KVM_CAP_ARM_RME_INIT_RIPAS_REALM: {
+        struct arm_rme_init_ripas args;
+        void __user *argp = u64_to_user_ptr(cap->args[1]);

arch/arm64/kvm/rme.c +9 -0
+
+        if (copy_from_user(&args, argp, sizeof(args))) {
+            r = -EFAULT;
+            break;
+        }
+
+        r = kvm_init_ipa_range_realm(kvm, &args);
+        break;
+    }
Quoted 3 lines

      default:
          r = -EINVAL;
          break;

Thanks,
Gavin

#40 lore raw

Re: [PATCH v8 15/43] arm64: RME: Allow VMM to set RIPAS

Quoted 48 lines · Steven Price
Quoted 47 lines · Gavin Shan
Quoted 46 lines · Steven Price

Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.

When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.

When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.

---
Changes from v7:
  * Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
  * Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
  * Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
  * Several other minor changes to names/return codes.
Changes from v6:
  * Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
  * Drop the useless flags argument from alloc_delegated_granule().
  * Account RTTs allocated for a guest using kvm_account_pgtable_pages().
  * Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
  * Adapt to rebasing.
  * Introduce find_map_level()
  * Rename some functions to be clearer.
  * Drop the "spare page" functionality.
Changes from v2:
  * {alloc,free}_delegated_page() moved from previous patch to this one.
  * alloc_delegated_page() now takes a gfp_t flags parameter.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

  * Fix the reference counting of guestmem pages to avoid leaking memory.
  * Several misc code improvements and extra comments.

---
   arch/arm64/include/asm/kvm_rme.h |   5 +
   arch/arm64/kvm/mmu.c             |   8 +-
   arch/arm64/kvm/rme.c             | 384 +++++++++++++++++++++++++++++++
   3 files changed, 394 insertions(+), 3 deletions(-)

.../...

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/rme.c +7 -0
+static int kvm_init_ipa_range_realm(struct kvm *kvm,
+                    struct arm_rme_init_ripas *args)
+{
+    gpa_t addr, end;
+    struct realm *realm = &kvm->arch.realm;
+
+    addr = args->base;
Quoted 8 lines
Quoted 4 lines

+    end = addr + args->size;
+
+    if (end < addr)
+        return -EINVAL;

The check needs to cover 'end <= addr'. RMI_ERROR_INPUT is returned from RMM::smc_rtt_init_ripas()
if 'end' is equal to 'addr', but we're returning 0, inconsistent to that.

Quoted 8 lines

I agree we're different to smc_rtt_init_ripas(), but I don't really see why we should prevent args->size==0. Calling the low level SMC in that case would clearly be wrong (the kernel should be validating and that would show a lack of validation), but we handle that with the while loop in realm_init_ipa_state().

Do you think it's important to define this uAPI to disallow size==0?

No, it's not a big deal since it's just a nitpick. The current implementation isn't wrong because 0 will be returned when size is 0, meaning it succeeds but no work to do there. Please leave the code as of being :)

Quoted 7 lines
Quoted 7 lines

    if (end <= addr)
        return -EINVAL;

Quoted 3 lines

+
+    if (kvm_realm_state(kvm) != REALM_STATE_NEW)
+        return -EPERM;

Quoted 8 lines
Quoted 4 lines

To keep the consistency, kvm_realm_is_created() can be used here.

    if (kvm_realm_is_created(kvm))
        return -EPERM;

This isn't the same - kvm_realm_is_create() is checking for REALM_STATE_NONE, but we want to check for REALM_STATE_NEW.

Hmm, sorry for the noise. The current code is good enough then :)

Thanks,
Gavin

#41 lore raw

Re: [PATCH v8 15/43] arm64: RME: Allow VMM to set RIPAS

In reply to Steven Price

Hi Steven

Quoted 134 lines · Steven Price

Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.

When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.

When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.

---
Changes from v7:
* Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
* Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
* Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
* Several other minor changes to names/return codes.
Changes from v6:
* Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
* Drop the useless flags argument from alloc_delegated_granule().
* Account RTTs allocated for a guest using kvm_account_pgtable_pages().
* Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
* Adapt to rebasing.
* Introduce find_map_level()
* Rename some functions to be clearer.
* Drop the "spare page" functionality.
Changes from v2:
* {alloc,free}_delegated_page() moved from previous patch to this one.
* alloc_delegated_page() now takes a gfp_t flags parameter.
* Fix the reference counting of guestmem pages to avoid leaking memory.
* Several misc code improvements and extra comments.

---
  arch/arm64/include/asm/kvm_rme.h |   5 +
  arch/arm64/kvm/mmu.c             |   8 +-
  arch/arm64/kvm/rme.c             | 384 +++++++++++++++++++++++++++++++
  3 files changed, 394 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rme.h +5 -0
index 9bcad6ec5dbb..b916db8565a2 100644
@@ -101,6 +101,11 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
  int kvm_create_rec(struct kvm_vcpu *vcpu);
  void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
+void kvm_realm_unmap_range(struct kvm *kvm,
+			   unsigned long ipa,
+			   unsigned long size,
+			   bool unmap_private);
+
  static inline bool kvm_realm_is_private_address(struct realm *realm,
  						unsigned long addr)
  {
arch/arm64/kvm/mmu.c +5 -3
index d80a9d408f71..71c04259e39f 100644
@@ -323,6 +323,7 @@ static void invalidate_icache_guest_page(void *va, size_t size)
   * @start: The intermediate physical base address of the range to unmap
   * @size:  The size of the area to unmap
   * @may_block: Whether or not we are permitted to block
+ * @only_shared: If true then protected mappings should not be unmapped
   *
   * Clear a range of stage-2 mappings, lowering the various ref-counts.  Must
   * be called while holding mmu_lock (unless for freeing the stage2 pgd before
@@ -330,7 +331,7 @@ static void invalidate_icache_guest_page(void *va, size_t size)
   * with things behind our backs.
   */
  static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
-				 bool may_block)
+				 bool may_block, bool only_shared)
  {
  	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
  	phys_addr_t end = start + size;
@@ -344,7 +345,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64
  void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
  			    u64 size, bool may_block)
  {
-	__unmap_stage2_range(mmu, start, size, may_block);
+	__unmap_stage2_range(mmu, start, size, may_block, false);
  }
 
  void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
@@ -1975,7 +1976,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 
  	__unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
  			     (range->end - range->start) << PAGE_SHIFT,
-			     range->may_block);
+			     range->may_block,
+			     !(range->attr_filter & KVM_FILTER_PRIVATE));
 
  	kvm_nested_s2_unmap(kvm, range->may_block);
  	return false;
arch/arm64/kvm/rme.c +15 -0
index 1239eb07aca6..33eb793d8bdb 100644
@@ -87,6 +87,51 @@ static int get_start_level(struct realm *realm)
  	return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
  }
 
 
+
+static phys_addr_t alloc_delegated_granule(struct kvm_mmu_memory_cache *mc)
+{
+	phys_addr_t phys;
+	void *virt;
+
+	if (mc)
+		virt = kvm_mmu_memory_cache_alloc(mc);
+	else
+		virt = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+
+	if (!virt)
+		return PHYS_ADDR_MAX;
+
+	phys = virt_to_phys(virt);

arch/arm64/kvm/rme.c static int get_start_level(struct realm *realm) · line 115

Quoted 10 lines
arch/arm64/kvm/rme.c +8 -0
+
+	if (rmi_granule_delegate(phys)) {
+		free_page((unsigned long)virt);
+
+		return PHYS_ADDR_MAX;
+	}
+
 
 
+	kvm_account_pgtable_pages(virt, 1);

Could we delay this to the place where we actually use it ? Otherwise we may get the accounting wrong, if e.g, free it using free_delegated_granule() ? Also, the name doesn't suggest that we are allocating an rtt (though it accepts a memory_cache for rtts).

Quoted 19 lines
arch/arm64/kvm/rme.c +12 -0
+
+	return phys;
+}
+
  static void free_delegated_granule(phys_addr_t phys)
  {
  	if (WARN_ON(rmi_granule_undelegate(phys))) {
@@ -99,6 +144,154 @@ static void free_delegated_granule(phys_addr_t phys)
  	free_page((unsigned long)phys_to_virt(phys));
  }
 
+static int realm_rtt_create(struct realm *realm,
+			    unsigned long addr,
+			    int level,
+			    phys_addr_t phys)
+{
+	addr = ALIGN_DOWN(addr, rme_rtt_level_mapsize(level - 1));
+	return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level);
+}

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 164

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+
+static int realm_rtt_fold(struct realm *realm,
+			  unsigned long addr,
+			  int level,
+			  phys_addr_t *rtt_granule)
+{
+	unsigned long out_rtt;
+	int ret;
+
+	ret = rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt);

minor nit: Should we align "addr" to level - 1, similar to what we do for rtt_create, just to be consistent and safer ?

Quoted 23 lines
arch/arm64/kvm/rme.c +23 -0
+
+	if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS && rtt_granule)
+		*rtt_granule = out_rtt;
+
+	return ret;
+}
+
+static int realm_destroy_private_granule(struct realm *realm,
+					 unsigned long ipa,
+					 unsigned long *next_addr,
+					 phys_addr_t *out_rtt)
+{
+	unsigned long rd = virt_to_phys(realm->rd);
+	unsigned long rtt_addr;
+	phys_addr_t rtt;
+	int ret;
+
+retry:
+	ret = rmi_data_destroy(rd, ipa, &rtt_addr, next_addr);
+	if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+		if (*next_addr > ipa)
+			return 0; /* UNASSIGNED */
+		rtt = alloc_delegated_granule(NULL);

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 197

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+		if (WARN_ON(rtt == PHYS_ADDR_MAX))
+			return -ENOMEM;
+		/*
+		 * ASSIGNED - ipa is mapped as a block, so split. The index
+		 * from the return code should be 2 otherwise it appears
+		 * there's a huge page bigger than KVM currently supports
+		 */
+		WARN_ON(RMI_RETURN_INDEX(ret) != 2);
+		ret = realm_rtt_create(realm, ipa, 3, rtt);
+		if (WARN_ON(ret)) {

Can we race with another thread ? If so, we may need to do an rtt read to confirm if we are safe to retry and ignore the failure.

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 202

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+			free_delegated_granule(rtt);
+			return -ENXIO;
+		}
+		goto retry;
+	} else if (WARN_ON(ret)) {

minor nit: I am wondering, if we should add a new wrapper for warn_on, which also dumps the RTT entry and the operation that caused the WARNING ? Might be useful to debug and pin point issues. We have several cases where this might be used.

Quoted 20 lines
arch/arm64/kvm/rme.c +20 -0
+		return -ENXIO;
+	}
+
+	ret = rmi_granule_undelegate(rtt_addr);
+	if (WARN_ON(ret))
+		return -ENXIO;
+
+	*out_rtt = rtt_addr;
+
+	return 0;
+}
+
+static int realm_unmap_private_page(struct realm *realm,
+				    unsigned long ipa,
+				    unsigned long *next_addr)
+{
+	unsigned long end = ALIGN(ipa + 1, PAGE_SIZE);
+	unsigned long addr;
+	phys_addr_t out_rtt = PHYS_ADDR_MAX;
+	int ret;

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 232

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+
+	for (addr = ipa; addr < end; addr = *next_addr) {
+		ret = realm_destroy_private_granule(realm, addr, next_addr,
+						    &out_rtt);
+		if (ret)
+			return ret;
+	}
+
+	if (out_rtt != PHYS_ADDR_MAX)
+		free_page((unsigned long)phys_to_virt(out_rtt));

nit: If this non-4K host, then, we may want to ALIGN_DOWN(out_rtt, PAGE_SIZE) ?

Quoted 79 lines
arch/arm64/kvm/rme.c +72 -0
+
+	return 0;
+}
+
+static void realm_unmap_shared_range(struct kvm *kvm,
+				     int level,
+				     unsigned long start,
+				     unsigned long end)
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned long rd = virt_to_phys(realm->rd);
+	ssize_t map_size = rme_rtt_level_mapsize(level);
+	unsigned long next_addr, addr;
+	unsigned long shared_bit = BIT(realm->ia_bits - 1);
+
+	if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+		return;
+
+	start |= shared_bit;
+	end |= shared_bit;
+
+	for (addr = start; addr < end; addr = next_addr) {
+		unsigned long align_addr = ALIGN(addr, map_size);
+		int ret;
+
+		next_addr = ALIGN(addr + 1, map_size);
+
+		if (align_addr != addr || next_addr > end) {
+			/* Need to recurse deeper */
+			if (addr < align_addr)
+				next_addr = align_addr;
+			realm_unmap_shared_range(kvm, level + 1, addr,
+						 min(next_addr, end));
+			continue;
+		}
+
+		ret = rmi_rtt_unmap_unprotected(rd, addr, level, &next_addr);
+		switch (RMI_RETURN_STATUS(ret)) {
+		case RMI_SUCCESS:
+			break;
+		case RMI_ERROR_RTT:
+			if (next_addr == addr) {
+				/*
+				 * There's a mapping here, but it's not a block
+				 * mapping, so reset next_addr to the next block
+				 * boundary and recurse to clear out the pages
+				 * one level deeper.
+				 */
+				next_addr = ALIGN(addr + 1, map_size);
+				realm_unmap_shared_range(kvm, level + 1, addr,
+							 next_addr);
+			}
+			break;
+		default:
+			WARN_ON(1);
+			return;
+		}
+
+		cond_resched_rwlock_write(&kvm->mmu_lock);
+	}
+}
+
  /* Calculate the number of s2 root rtts needed */
  static int realm_num_root_rtts(struct realm *realm)
  {
@@ -209,6 +402,40 @@ static int realm_rtt_destroy(struct realm *realm, unsigned long addr,
  	return ret;
  }
 
+static int realm_create_rtt_levels(struct realm *realm,
+				   unsigned long ipa,
+				   int level,
+				   int max_level,
+				   struct kvm_mmu_memory_cache *mc)
+{
+	if (level == max_level)
+		return 0;
+
+	while (level++ < max_level) {

arch/arm64/kvm/rme.c static int realm_rtt_destroy(struct realm *realm, unsigned long addr, · line 424

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+		phys_addr_t rtt = alloc_delegated_granule(mc);
+		int ret;
+
+		if (rtt == PHYS_ADDR_MAX)
+			return -ENOMEM;
+
+		ret = realm_rtt_create(realm, ipa, level, rtt);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT &&
+		    RMI_RETURN_INDEX(ret) == level) {

For the record, as Aneesh pointed out offline, this must be level - 1, as rtt_create only walks to level - 1.

Quoted 86 lines
arch/arm64/kvm/rme.c +72 -0
+			/* The RTT already exists, continue */
+			continue;
+		}
+		if (ret) {
+			WARN(1, "Failed to create RTT at level %d: %d\n",
+			     level, ret);
+			free_delegated_granule(rtt);
+			return -ENXIO;
+		}
+	}
+
+	return 0;
+}
+
  static int realm_tear_down_rtt_level(struct realm *realm, int level,
  				     unsigned long start, unsigned long end)
  {
@@ -299,6 +526,61 @@ static int realm_tear_down_rtt_range(struct realm *realm,
  					 start, end);
  }
 
+/*
+ * Returns 0 on successful fold, a negative value on error, a positive value if
+ * we were not able to fold all tables at this level.
+ */
+static int realm_fold_rtt_level(struct realm *realm, int level,
+				unsigned long start, unsigned long end)
+{
+	int not_folded = 0;
+	ssize_t map_size;
+	unsigned long addr, next_addr;
+
+	if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+		return -EINVAL;
+
+	map_size = rme_rtt_level_mapsize(level - 1);
+
+	for (addr = start; addr < end; addr = next_addr) {
+		phys_addr_t rtt_granule;
+		int ret;
+		unsigned long align_addr = ALIGN(addr, map_size);
+
+		next_addr = ALIGN(addr + 1, map_size);
+
+		ret = realm_rtt_fold(realm, align_addr, level, &rtt_granule);
+
+		switch (RMI_RETURN_STATUS(ret)) {
+		case RMI_SUCCESS:
+			free_delegated_granule(rtt_granule);
+			break;
+		case RMI_ERROR_RTT:
+			if (level == RMM_RTT_MAX_LEVEL ||
+			    RMI_RETURN_INDEX(ret) < level) {
+				not_folded++;
+				break;
+			}
+			/* Recurse a level deeper */
+			ret = realm_fold_rtt_level(realm,
+						   level + 1,
+						   addr,
+						   next_addr);
+			if (ret < 0)
+				return ret;
+			else if (ret == 0)
+				/* Try again at this level */
+				next_addr = addr;
+			break;
+		default:
+			WARN_ON(1);
+			return -ENXIO;
+		}
+	}
+
+	return not_folded;
+}
+
  void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits)
  {
  	struct realm *realm = &kvm->arch.realm;
@@ -306,6 +588,96 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits)
  	WARN_ON(realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits)));
  }
 
+static void realm_unmap_private_range(struct kvm *kvm,
+				      unsigned long start,
+				      unsigned long end)

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 603

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned long next_addr, addr;
+	int ret;
+
+	for (addr = start; addr < end; addr = next_addr) {
+		ret = realm_unmap_private_page(realm, addr, &next_addr);
+
+		if (ret)
+			break;

Do we need yielding CPU here, similar to the shared case ? It may be difficult (not impossible) to define a check point to do that.

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 607

Quoted 4 lines
arch/arm64/kvm/rme.c +4 -0
+	}
+
+	realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+			     start, end);

We don't seem to be reclaiing the RTTs from shared mapping case ?

Quoted 26 lines
arch/arm64/kvm/rme.c +26 -0
+}
+
+void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
+			   unsigned long size, bool unmap_private)
+{
+	unsigned long end = start + size;
+	struct realm *realm = &kvm->arch.realm;
+
+	end = min(BIT(realm->ia_bits - 1), end);
+
+	if (realm->state == REALM_STATE_NONE)
+		return;
+
+	realm_unmap_shared_range(kvm, find_map_level(realm, start, end),
+				 start, end);
+	if (unmap_private)
+		realm_unmap_private_range(kvm, start, end);
+}
+
+static int realm_init_ipa_state(struct realm *realm,
+				unsigned long ipa,
+				unsigned long end)
+{
+	phys_addr_t rd_phys = virt_to_phys(realm->rd);
+	int ret;
+

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 643

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+	while (ipa < end) {
+		unsigned long next;
+
+		ret = rmi_rtt_init_ripas(rd_phys, ipa, end, &next);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+			int err_level = RMI_RETURN_INDEX(ret);
+			int level = find_map_level(realm, ipa, end);
+
+			if (WARN_ON(err_level >= level))

I am wondering if WARN_ON() is required here. A buggy VMM could trigger the WARN_ON(). (e.g, INIT_IPA after POPULATE, where L3 table is created.). The only case where it may be worth WARNING is if the level == 3.

Suzuki

Quoted 59 lines
arch/arm64/kvm/rme.c +49 -0
+				return -ENXIO;
+
+			ret = realm_create_rtt_levels(realm, ipa,
+						      err_level,
+						      level, NULL);
+			if (ret)
+				return ret;
+			/* Retry with the RTT levels in place */
+			continue;
+		} else if (WARN_ON(ret)) {
+			return -ENXIO;
+		}
+
+		ipa = next;
+	}
+
+	return 0;
+}
+
+static int kvm_init_ipa_range_realm(struct kvm *kvm,
+				    struct arm_rme_init_ripas *args)
+{
+	gpa_t addr, end;
+	struct realm *realm = &kvm->arch.realm;
+
+	addr = args->base;
+	end = addr + args->size;
+
+	if (end < addr)
+		return -EINVAL;
+
+	if (kvm_realm_state(kvm) != REALM_STATE_NEW)
+		return -EPERM;
+
+	return realm_init_ipa_state(realm, addr, end);
+}
+
  /* Protects access to rme_vmid_bitmap */
  static DEFINE_SPINLOCK(rme_vmid_lock);
  static unsigned long *rme_vmid_bitmap;
@@ -431,6 +803,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
  	case KVM_CAP_ARM_RME_CREATE_REALM:
  		r = kvm_create_realm(kvm);
  		break;
+	case KVM_CAP_ARM_RME_INIT_RIPAS_REALM: {
+		struct arm_rme_init_ripas args;
+		void __user *argp = u64_to_user_ptr(cap->args[1]);
+
+		if (copy_from_user(&args, argp, sizeof(args))) {
+			r = -EFAULT;
+			break;
+		}
+
+		r = kvm_init_ipa_range_realm(kvm, &args);
+		break;
+	}
  	default:
  		r = -EINVAL;
  		break;
#42 lore raw

Re: [PATCH v8 15/43] arm64: RME: Allow VMM to set RIPAS

Quoted 153 lines · Suzuki K Poulose

Hi Steven

Quoted 148 lines · Steven Price

Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.

When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.

When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.

---
Changes from v7:
  * Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
  * Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
  * Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
  * Several other minor changes to names/return codes. Changes from v6:
  * Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
  * Drop the useless flags argument from alloc_delegated_granule().
  * Account RTTs allocated for a guest using kvm_account_pgtable_pages().
  * Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
  * Adapt to rebasing.
  * Introduce find_map_level()
  * Rename some functions to be clearer.
  * Drop the "spare page" functionality.
Changes from v2:
  * {alloc,free}_delegated_page() moved from previous patch to this one.
  * alloc_delegated_page() now takes a gfp_t flags parameter.
  * Fix the reference counting of guestmem pages to avoid leaking memory.
  * Several misc code improvements and extra comments.
---
  arch/arm64/include/asm/kvm_rme.h |   5 + arch/arm64/kvm/mmu.c             |   8 +- arch/arm64/kvm/rme.c             | 384 +++++++++++++++++++++++++++++++

  3 files changed, 394 insertions(+), 3 deletions(-)
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index 9bcad6ec5dbb..b916db8565a2 100644

arch/arm64/include/asm/kvm_rme.h +4 -0
@@ -101,6 +101,11 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32
 ia_bits);
   int kvm_create_rec(struct kvm_vcpu *vcpu);
   void kvm_destroy_rec(struct kvm_vcpu *vcpu);
   +void kvm_realm_unmap_range(struct kvm *kvm,
+               unsigned long ipa,
+               unsigned long size,
+               bool unmap_private);
+
   static inline bool kvm_realm_is_private_address(struct realm *realm,
                           unsigned long addr)

  {

arch/arm64/kvm/mmu.c +1 -0
index d80a9d408f71..71c04259e39f 100644
@@ -323,6 +323,7 @@ static void invalidate_icache_guest_page(void *va,
 size_t size)
    * @start: The intermediate physical base address of the range to unmap
    * @size:  The size of the area to unmap
    * @may_block: Whether or not we are permitted to block
+ * @only_shared: If true then protected mappings should not be unmapped
    *
    * Clear a range of stage-2 mappings, lowering the various ref-

counts.  Must
   * be called while holding mmu_lock (unless for freeing the stage2 pgd before

arch/arm64/kvm/mmu.c +0 -0
@@ -330,7 +331,7 @@ static void invalidate_icache_guest_page(void *va,
 size_t size)
    * with things behind our backs.
    */

  static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
-                 bool may_block)
+                 bool may_block, bool only_shared)
  {
      struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
      phys_addr_t end = start + size;

arch/arm64/kvm/mmu.c +1 -1
@@ -344,7 +345,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu
 *mmu, phys_addr_t start, u64
   void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
                   u64 size, bool may_block)
   {
-    __unmap_stage2_range(mmu, start, size, may_block);
+    __unmap_stage2_range(mmu, start, size, may_block, false);
   }

    void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)

arch/arm64/kvm/mmu.c +2 -1
@@ -1975,7 +1976,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct
 kvm_gfn_range *range)
         __unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
                    (range->end - range->start) << PAGE_SHIFT,
-                 range->may_block);
+                 range->may_block,
+                 !(range->attr_filter & KVM_FILTER_PRIVATE));
         kvm_nested_s2_unmap(kvm, range->may_block);
       return false;
arch/arm64/kvm/rme.c +0 -0
index 1239eb07aca6..33eb793d8bdb 100644
@@ -87,6 +87,51 @@ static int get_start_level(struct realm *realm)
       return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
   }

arch/arm64/kvm/rme.c +2 -0
+
+static phys_addr_t alloc_delegated_granule(struct

kvm_mmu_memory_cache *mc)

arch/arm64/kvm/rme.c +20 -0
+{
+    phys_addr_t phys;
+    void *virt;
+
+    if (mc)
+        virt = kvm_mmu_memory_cache_alloc(mc);
+    else
+        virt = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+
+    if (!virt)
+        return PHYS_ADDR_MAX;
+
+    phys = virt_to_phys(virt);
+
+    if (rmi_granule_delegate(phys)) {
+        free_page((unsigned long)virt);
+
+        return PHYS_ADDR_MAX;
+    }
+
Quoted 6 lines
Quoted 1 line

+    kvm_account_pgtable_pages(virt, 1);

Could we delay this to the place where we actually use it ? Otherwise we may get the accounting wrong, if e.g, free it using free_delegated_granule() ? Also, the name doesn't suggest that we are allocating an rtt (though it accepts a memory_cache for rtts).

I think what I should do is provide wrappers for the RTT functions and move the kvm_account_pgtables_pages() to the wrappers. That would make it more obvious which allocations are for RTTs and hopefully avoid accounting errors.

Quoted 23 lines
Quoted 23 lines
arch/arm64/kvm/rme.c +4 -0
+
+    return phys;
+}
+

  static void free_delegated_granule(phys_addr_t phys) {
      if (WARN_ON(rmi_granule_undelegate(phys))) {

arch/arm64/kvm/rme.c +12 -0
@@ -99,6 +144,154 @@ static void free_delegated_granule(phys_addr_t phys)
       free_page((unsigned long)phys_to_virt(phys));
   }
   +static int realm_rtt_create(struct realm *realm,
+                unsigned long addr,
+                int level,
+                phys_addr_t phys)
+{
+    addr = ALIGN_DOWN(addr, rme_rtt_level_mapsize(level - 1));
+    return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level);
+}
+
+static int realm_rtt_fold(struct realm *realm,
+              unsigned long addr,
+              int level,
+              phys_addr_t *rtt_granule)
Quoted 8 lines

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 164

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+{
+    unsigned long out_rtt;
+    int ret;
+
+    ret = rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt);

minor nit: Should we align "addr" to level - 1, similar to what we do for rtt_create, just to be consistent and safer ?

Ack

Quoted 28 lines
Quoted 28 lines
arch/arm64/kvm/rme.c +28 -0
+
+    if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS && rtt_granule)
+        *rtt_granule = out_rtt;
+
+    return ret;
+}
+
+static int realm_destroy_private_granule(struct realm *realm,
+                     unsigned long ipa,
+                     unsigned long *next_addr,
+                     phys_addr_t *out_rtt)
+{
+    unsigned long rd = virt_to_phys(realm->rd);
+    unsigned long rtt_addr;
+    phys_addr_t rtt;
+    int ret;
+
+retry:
+    ret = rmi_data_destroy(rd, ipa, &rtt_addr, next_addr);
+    if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+        if (*next_addr > ipa)
+            return 0; /* UNASSIGNED */
+        rtt = alloc_delegated_granule(NULL);
+        if (WARN_ON(rtt == PHYS_ADDR_MAX))
+            return -ENOMEM;
+        /*
+         * ASSIGNED - ipa is mapped as a block, so split. The index
+         * from the return code should be 2 otherwise it appears
Quoted 8 lines

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 197

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+         * there's a huge page bigger than KVM currently supports
+         */
+        WARN_ON(RMI_RETURN_INDEX(ret) != 2);
+        ret = realm_rtt_create(realm, ipa, 3, rtt);
+        if (WARN_ON(ret)) {

Can we race with another thread ? If so, we may need to do an rtt read to confirm if we are safe to retry and ignore the failure.

We're holding the KVM mmu_lock (the only call path is realm_unmap_private_range() > realm_unmap_private_page() > realm_destroy_private_granule(). So we can't race with another thread doing the same thing.

This situation happens when there's a block mapping, so we know there can't be a racing thread faulting in other pages in the same RTT (as all the base pages are already there). So AFAICT this should never happen due to a race...

I may have missed something though ;)

I'm also a little wary of using rtt read because that in itself can be racy (unless we're holding a suitable lock the situation can change before/after the read).

Quoted 11 lines

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 202

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+            free_delegated_granule(rtt);
+            return -ENXIO;
+        }
+        goto retry;
+    } else if (WARN_ON(ret)) {

minor nit: I am wondering, if we should add a new wrapper for warn_on, which also dumps the RTT entry and the operation that caused the WARNING ? Might be useful to debug and pin point issues. We have several cases
where this might be used.

In this particular case the error is not RMI_ERROR_RTT, so the RTT probably isn't particularly enlightening. But I agree the default WARN_ON splat tends not to be informative, I'll take a look at if a wrapper could help.

Quoted 25 lines
Quoted 25 lines
arch/arm64/kvm/rme.c +25 -0
+        return -ENXIO;
+    }
+
+    ret = rmi_granule_undelegate(rtt_addr);
+    if (WARN_ON(ret))
+        return -ENXIO;
+
+    *out_rtt = rtt_addr;
+
+    return 0;
+}
+
+static int realm_unmap_private_page(struct realm *realm,
+                    unsigned long ipa,
+                    unsigned long *next_addr)
+{
+    unsigned long end = ALIGN(ipa + 1, PAGE_SIZE);
+    unsigned long addr;
+    phys_addr_t out_rtt = PHYS_ADDR_MAX;
+    int ret;
+
+    for (addr = ipa; addr < end; addr = *next_addr) {
+        ret = realm_destroy_private_granule(realm, addr, next_addr,
+                            &out_rtt);
+        if (ret)
Quoted 8 lines

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 232

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+            return ret;
+    }
+
+    if (out_rtt != PHYS_ADDR_MAX)
+        free_page((unsigned long)phys_to_virt(out_rtt));

nit: If this non-4K host, then, we may want to ALIGN_DOWN(out_rtt, PAGE_SIZE) ?

Ack

Quoted 84 lines
Quoted 84 lines
arch/arm64/kvm/rme.c +76 -0
+
+    return 0;
+}
+
+static void realm_unmap_shared_range(struct kvm *kvm,
+                     int level,
+                     unsigned long start,
+                     unsigned long end)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned long rd = virt_to_phys(realm->rd);
+    ssize_t map_size = rme_rtt_level_mapsize(level);
+    unsigned long next_addr, addr;
+    unsigned long shared_bit = BIT(realm->ia_bits - 1);
+
+    if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+        return;
+
+    start |= shared_bit;
+    end |= shared_bit;
+
+    for (addr = start; addr < end; addr = next_addr) {
+        unsigned long align_addr = ALIGN(addr, map_size);
+        int ret;
+
+        next_addr = ALIGN(addr + 1, map_size);
+
+        if (align_addr != addr || next_addr > end) {
+            /* Need to recurse deeper */
+            if (addr < align_addr)
+                next_addr = align_addr;
+            realm_unmap_shared_range(kvm, level + 1, addr,
+                         min(next_addr, end));
+            continue;
+        }
+
+        ret = rmi_rtt_unmap_unprotected(rd, addr, level, &next_addr);
+        switch (RMI_RETURN_STATUS(ret)) {
+        case RMI_SUCCESS:
+            break;
+        case RMI_ERROR_RTT:
+            if (next_addr == addr) {
+                /*
+                 * There's a mapping here, but it's not a block
+                 * mapping, so reset next_addr to the next block
+                 * boundary and recurse to clear out the pages
+                 * one level deeper.
+                 */
+                next_addr = ALIGN(addr + 1, map_size);
+                realm_unmap_shared_range(kvm, level + 1, addr,
+                             next_addr);
+            }
+            break;
+        default:
+            WARN_ON(1);
+            return;
+        }
+
+        cond_resched_rwlock_write(&kvm->mmu_lock);
+    }
+}
+
   /* Calculate the number of s2 root rtts needed */
   static int realm_num_root_rtts(struct realm *realm)
   {
@@ -209,6 +402,40 @@ static int realm_rtt_destroy(struct realm *realm,
 unsigned long addr,
       return ret;
   }
   +static int realm_create_rtt_levels(struct realm *realm,
+                   unsigned long ipa,
+                   int level,
+                   int max_level,
+                   struct kvm_mmu_memory_cache *mc)
+{
+    if (level == max_level)
+        return 0;
+
+    while (level++ < max_level) {
+        phys_addr_t rtt = alloc_delegated_granule(mc);
+        int ret;
+
+        if (rtt == PHYS_ADDR_MAX)
+            return -ENOMEM;
Quoted 8 lines

arch/arm64/kvm/rme.c static int realm_rtt_destroy(struct realm *realm, unsigned long addr, · line 424

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+
+        ret = realm_rtt_create(realm, ipa, level, rtt);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT &&
+            RMI_RETURN_INDEX(ret) == level) {

For the record, as Aneesh pointed out offline, this must be level - 1, as rtt_create only walks to level - 1.

Ack

Quoted 92 lines
Quoted 92 lines
arch/arm64/kvm/rme.c +14 -0
+            /* The RTT already exists, continue */
+            continue;
+        }
+        if (ret) {
+            WARN(1, "Failed to create RTT at level %d: %d\n",
+                 level, ret);
+            free_delegated_granule(rtt);
+            return -ENXIO;
+        }
+    }
+
+    return 0;
+}
+
   static int realm_tear_down_rtt_level(struct realm *realm, int level,
                        unsigned long start, unsigned long end)

  {

arch/arm64/kvm/rme.c +0 -0
@@ -299,6 +526,61 @@ static int realm_tear_down_rtt_range(struct realm
 *realm,
                        start, end);
   }

  +/*
+ * Returns 0 on successful fold, a negative value on error, a positive value if
+ * we were not able to fold all tables at this level.
+ */

arch/arm64/kvm/rme.c +51 -0
+static int realm_fold_rtt_level(struct realm *realm, int level,
+                unsigned long start, unsigned long end)
+{
+    int not_folded = 0;
+    ssize_t map_size;
+    unsigned long addr, next_addr;
+
+    if (WARN_ON(level > RMM_RTT_MAX_LEVEL))
+        return -EINVAL;
+
+    map_size = rme_rtt_level_mapsize(level - 1);
+
+    for (addr = start; addr < end; addr = next_addr) {
+        phys_addr_t rtt_granule;
+        int ret;
+        unsigned long align_addr = ALIGN(addr, map_size);
+
+        next_addr = ALIGN(addr + 1, map_size);
+
+        ret = realm_rtt_fold(realm, align_addr, level, &rtt_granule);
+
+        switch (RMI_RETURN_STATUS(ret)) {
+        case RMI_SUCCESS:
+            free_delegated_granule(rtt_granule);
+            break;
+        case RMI_ERROR_RTT:
+            if (level == RMM_RTT_MAX_LEVEL ||
+                RMI_RETURN_INDEX(ret) < level) {
+                not_folded++;
+                break;
+            }
+            /* Recurse a level deeper */
+            ret = realm_fold_rtt_level(realm,
+                           level + 1,
+                           addr,
+                           next_addr);
+            if (ret < 0)
+                return ret;
+            else if (ret == 0)
+                /* Try again at this level */
+                next_addr = addr;
+            break;
+        default:
+            WARN_ON(1);
+            return -ENXIO;
+        }
+    }
+
+    return not_folded;
+}
+

  void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) {
      struct realm *realm = &kvm->arch.realm;

arch/arm64/kvm/rme.c +7 -0
@@ -306,6 +588,96 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32
 ia_bits)
       WARN_ON(realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits)));
   }
   +static void realm_unmap_private_range(struct kvm *kvm,
+                      unsigned long start,
+                      unsigned long end)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned long next_addr, addr;
+    int ret;
+
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 603

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+    for (addr = start; addr < end; addr = next_addr) {
+        ret = realm_unmap_private_page(realm, addr, &next_addr);
+
+        if (ret)
+            break;

Do we need yielding CPU here, similar to the shared case ? It may be difficult (not impossible) to define a check point to do that.

Yes, I've already got a local change adding a "may_block" flag to the function so we can do a cond_resched_rwlock_write() call here when possible.

Quoted 6 lines

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 607

Quoted 4 lines
arch/arm64/kvm/rme.c +4 -0
+    }
+
+    realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+                 start, end);

We don't seem to be reclaiing the RTTs from shared mapping case ?

I'm not sure I follow: realm_fold_rtt_level() will free any RTTs that are released.

Or are you referring to the fact that we don't (yet) fold the shared range? I have been purposefully leaving that for now as normally we'd follow the page size in the VMM to choose the page size on the guest, but that doesn't work when the RMM might have a different page size to the host. So my reasons for leaving it for later are:

* First huge pages is very much a TODO in general.

* When we support >4K host pages then a huge page on the host may not be available in the RMM, so we can't just follow the VMM.

* We don't have support in realm_unmap_shared_range() to split a block mapping up - it could be added, but it's not clear to me if it's best to split a block mapping, or remove the whole and refault as required.

* guest_memfd might well be able to provide some good hints here, but we'll have to wait for that series to settle.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/rme.c +33 -0
+}
+
+void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
+               unsigned long size, bool unmap_private)
+{
+    unsigned long end = start + size;
+    struct realm *realm = &kvm->arch.realm;
+
+    end = min(BIT(realm->ia_bits - 1), end);
+
+    if (realm->state == REALM_STATE_NONE)
+        return;
+
+    realm_unmap_shared_range(kvm, find_map_level(realm, start, end),
+                 start, end);
+    if (unmap_private)
+        realm_unmap_private_range(kvm, start, end);
+}
+
+static int realm_init_ipa_state(struct realm *realm,
+                unsigned long ipa,
+                unsigned long end)
+{
+    phys_addr_t rd_phys = virt_to_phys(realm->rd);
+    int ret;
+
+    while (ipa < end) {
+        unsigned long next;
+
+        ret = rmi_rtt_init_ripas(rd_phys, ipa, end, &next);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            int err_level = RMI_RETURN_INDEX(ret);
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 643

Quoted 3 lines
arch/arm64/kvm/rme.c +3 -0
+            int level = find_map_level(realm, ipa, end);
+
+            if (WARN_ON(err_level >= level))

I am wondering if WARN_ON() is required here. A buggy VMM could trigger the WARN_ON(). (e.g, INIT_IPA after POPULATE, where L3 table is created.). The only case where it may be worth WARNING is if the level == 3.

I have to admit I've struggled to get my head round this ;)

init_ripas will fail with ERROR_RTT in three cases:

1. (base_align) The base address isn't aligned for the level reached.

2. (rtt_state) The rtte state is !UNASSIGNED.

3. (no_progress) base==walk_top - the while condition should prevent this.

So I think case 1 is the case we're expecting, and creating RTTs should resolve it.

Case 2 is presumably the case you are concerned about, although it's not because tables have been created, but because INIT_RIPAS is invalid on areas that have been populated already.

If my reading of the spec is correct, then level == 3 isn't a possible result, so beyond potentially finding RMM bugs I don't think a WARN for that is very interesting. So for now I'll just drop the WARN_ON here altogether.

Thanks,
Steve

Quoted 65 lines

Suzuki

Quoted 60 lines
arch/arm64/kvm/rme.c +37 -0
+                return -ENXIO;
+
+            ret = realm_create_rtt_levels(realm, ipa,
+                              err_level,
+                              level, NULL);
+            if (ret)
+                return ret;
+            /* Retry with the RTT levels in place */
+            continue;
+        } else if (WARN_ON(ret)) {
+            return -ENXIO;
+        }
+
+        ipa = next;
+    }
+
+    return 0;
+}
+
+static int kvm_init_ipa_range_realm(struct kvm *kvm,
+                    struct arm_rme_init_ripas *args)
+{
+    gpa_t addr, end;
+    struct realm *realm = &kvm->arch.realm;
+
+    addr = args->base;
+    end = addr + args->size;
+
+    if (end < addr)
+        return -EINVAL;
+
+    if (kvm_realm_state(kvm) != REALM_STATE_NEW)
+        return -EPERM;
+
+    return realm_init_ipa_state(realm, addr, end);
+}
+
   /* Protects access to rme_vmid_bitmap */
   static DEFINE_SPINLOCK(rme_vmid_lock);

  static unsigned long *rme_vmid_bitmap;

arch/arm64/kvm/rme.c +0 -0
@@ -431,6 +803,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct
 kvm_enable_cap *cap)

      case KVM_CAP_ARM_RME_CREATE_REALM:
          r = kvm_create_realm(kvm);
          break;
+    case KVM_CAP_ARM_RME_INIT_RIPAS_REALM: {
+        struct arm_rme_init_ripas args;
+        void __user *argp = u64_to_user_ptr(cap->args[1]);

arch/arm64/kvm/rme.c +9 -0
+
+        if (copy_from_user(&args, argp, sizeof(args))) {
+            r = -EFAULT;
+            break;
+        }
+
+        r = kvm_init_ipa_range_realm(kvm, &args);
+        break;
+    }

      default:
          r = -EINVAL;
          break;

#43 lore raw

Re: [PATCH v8 15/43] arm64: RME: Allow VMM to set RIPAS

Quoted 242 lines · Steven Price
Quoted 153 lines · Suzuki K Poulose

Hi Steven

Quoted 148 lines · Steven Price

Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.

When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.

When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.

---
Changes from v7:
  * Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
  * Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
  * Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
  * Several other minor changes to names/return codes.
Changes from v6:
  * Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
  * Drop the useless flags argument from alloc_delegated_granule().
  * Account RTTs allocated for a guest using kvm_account_pgtable_pages().
  * Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
  * Adapt to rebasing.
  * Introduce find_map_level()
  * Rename some functions to be clearer.
  * Drop the "spare page" functionality.
Changes from v2:
  * {alloc,free}_delegated_page() moved from previous patch to this one.
  * alloc_delegated_page() now takes a gfp_t flags parameter.
  * Fix the reference counting of guestmem pages to avoid leaking memory.
  * Several misc code improvements and extra comments.

---
   arch/arm64/include/asm/kvm_rme.h |   5 +
   arch/arm64/kvm/mmu.c             |   8 +-
   arch/arm64/kvm/rme.c             | 384 +++++++++++++++++++++++++++++++
   3 files changed, 394 insertions(+), 3 deletions(-)
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index 9bcad6ec5dbb..b916db8565a2 100644

arch/arm64/include/asm/kvm_rme.h +4 -0
@@ -101,6 +101,11 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32
 ia_bits);
   int kvm_create_rec(struct kvm_vcpu *vcpu);
   void kvm_destroy_rec(struct kvm_vcpu *vcpu);
   +void kvm_realm_unmap_range(struct kvm *kvm,
+               unsigned long ipa,
+               unsigned long size,
+               bool unmap_private);
+
   static inline bool kvm_realm_is_private_address(struct realm *realm,
                           unsigned long addr)
   {
arch/arm64/kvm/mmu.c +1 -0
index d80a9d408f71..71c04259e39f 100644
@@ -323,6 +323,7 @@ static void invalidate_icache_guest_page(void *va,
 size_t size)
    * @start: The intermediate physical base address of the range to unmap
    * @size:  The size of the area to unmap
    * @may_block: Whether or not we are permitted to block
+ * @only_shared: If true then protected mappings should not be unmapped
    *
    * Clear a range of stage-2 mappings, lowering the various ref-

counts.  Must
   * be called while holding mmu_lock (unless for freeing the stage2
pgd before

arch/arm64/kvm/mmu.c +1 -1
@@ -330,7 +331,7 @@ static void invalidate_icache_guest_page(void *va,
 size_t size)
    * with things behind our backs.
    */
   static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t
 start, u64 size,
-                 bool may_block)
+                 bool may_block, bool only_shared)
   {

      struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
      phys_addr_t end = start + size;

arch/arm64/kvm/mmu.c +1 -1
@@ -344,7 +345,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu
 *mmu, phys_addr_t start, u64
   void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
                   u64 size, bool may_block)
   {
-    __unmap_stage2_range(mmu, start, size, may_block);
+    __unmap_stage2_range(mmu, start, size, may_block, false);
   }
     void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t

addr, phys_addr_t end)

arch/arm64/kvm/mmu.c +2 -1
@@ -1975,7 +1976,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct
 kvm_gfn_range *range)
         __unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
                    (range->end - range->start) << PAGE_SHIFT,
-                 range->may_block);
+                 range->may_block,
+                 !(range->attr_filter & KVM_FILTER_PRIVATE));
         kvm_nested_s2_unmap(kvm, range->may_block);
       return false;
arch/arm64/kvm/rme.c +22 -0
index 1239eb07aca6..33eb793d8bdb 100644
@@ -87,6 +87,51 @@ static int get_start_level(struct realm *realm)
       return 4 - ((realm->ia_bits - 8) / (RMM_PAGE_SHIFT - 3));
   }
 
 
+
+static phys_addr_t alloc_delegated_granule(struct
 kvm_mmu_memory_cache *mc)
+{
+    phys_addr_t phys;
+    void *virt;
+
+    if (mc)
+        virt = kvm_mmu_memory_cache_alloc(mc);
+    else
+        virt = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+
+    if (!virt)
+        return PHYS_ADDR_MAX;
+
+    phys = virt_to_phys(virt);
+
+    if (rmi_granule_delegate(phys)) {
+        free_page((unsigned long)virt);
+
+        return PHYS_ADDR_MAX;
+    }
+
Quoted 6 lines

arch/arm64/kvm/rme.c static int get_start_level(struct realm *realm) · line 130

Quoted 1 line
arch/arm64/kvm/rme.c +1 -0
+    kvm_account_pgtable_pages(virt, 1);

Could we delay this to the place where we actually use it ? Otherwise we may get the accounting wrong, if e.g, free it using free_delegated_granule() ? Also, the name doesn't suggest that we are allocating an rtt (though it accepts a memory_cache for rtts).

I think what I should do is provide wrappers for the RTT functions and move the kvm_account_pgtables_pages() to the wrappers. That would make it more obvious which allocations are for RTTs and hopefully avoid accounting errors.

Quoted 23 lines
Quoted 23 lines
arch/arm64/kvm/rme.c +16 -0
+
+    return phys;
+}
+
   static void free_delegated_granule(phys_addr_t phys)
   {
       if (WARN_ON(rmi_granule_undelegate(phys))) {
@@ -99,6 +144,154 @@ static void free_delegated_granule(phys_addr_t phys)
       free_page((unsigned long)phys_to_virt(phys));
   }
   +static int realm_rtt_create(struct realm *realm,
+                unsigned long addr,
+                int level,
+                phys_addr_t phys)
+{
+    addr = ALIGN_DOWN(addr, rme_rtt_level_mapsize(level - 1));
+    return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level);
+}
+
+static int realm_rtt_fold(struct realm *realm,
+              unsigned long addr,
+              int level,
+              phys_addr_t *rtt_granule)
Quoted 8 lines

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 164

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+{
+    unsigned long out_rtt;
+    int ret;
+
+    ret = rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt);

minor nit: Should we align "addr" to level - 1, similar to what we do for rtt_create, just to be consistent and safer ?

Ack

Quoted 28 lines
Quoted 28 lines
arch/arm64/kvm/rme.c +28 -0
+
+    if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS && rtt_granule)
+        *rtt_granule = out_rtt;
+
+    return ret;
+}
+
+static int realm_destroy_private_granule(struct realm *realm,
+                     unsigned long ipa,
+                     unsigned long *next_addr,
+                     phys_addr_t *out_rtt)
+{
+    unsigned long rd = virt_to_phys(realm->rd);
+    unsigned long rtt_addr;
+    phys_addr_t rtt;
+    int ret;
+
+retry:
+    ret = rmi_data_destroy(rd, ipa, &rtt_addr, next_addr);
+    if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+        if (*next_addr > ipa)
+            return 0; /* UNASSIGNED */
+        rtt = alloc_delegated_granule(NULL);
+        if (WARN_ON(rtt == PHYS_ADDR_MAX))
+            return -ENOMEM;
+        /*
+         * ASSIGNED - ipa is mapped as a block, so split. The index
+         * from the return code should be 2 otherwise it appears
Quoted 8 lines

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 197

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+         * there's a huge page bigger than KVM currently supports
+         */
+        WARN_ON(RMI_RETURN_INDEX(ret) != 2);
+        ret = realm_rtt_create(realm, ipa, 3, rtt);
+        if (WARN_ON(ret)) {

Can we race with another thread ? If so, we may need to do an rtt read to confirm if we are safe to retry and ignore the failure.

We're holding the KVM mmu_lock (the only call path is realm_unmap_private_range() > realm_unmap_private_page() > realm_destroy_private_granule(). So we can't race with another thread doing the same thing.

Quoted 4 lines

This situation happens when there's a block mapping, so we know there can't be a racing thread faulting in other pages in the same RTT (as all the base pages are already there). So AFAICT this should never happen due to a race...

I see, we shouldn't see a fault as the mappings are valid. Thanks for the clarification.

Quoted 6 lines

I may have missed something though ;)

I'm also a little wary of using rtt read because that in itself can be racy (unless we're holding a suitable lock the situation can change before/after the read).

Very much true. But it at least gives you some chance of a debug.

Quoted 17 lines
Quoted 7 lines

arch/arm64/kvm/rme.c static void free_delegated_granule(phys_addr_t phys) · line 202

Quoted 6 lines
arch/arm64/kvm/rme.c +5 -0
+            free_delegated_granule(rtt);
+            return -ENXIO;
+        }
+        goto retry;
+    } else if (WARN_ON(ret)) {
Quoted 5 lines

minor nit: I am wondering, if we should add a new wrapper for warn_on, which also dumps the RTT entry and the operation that caused the WARNING ? Might be useful to debug and pin point issues. We have several cases
where this might be used.

In this particular case the error is not RMI_ERROR_RTT, so the RTT probably isn't particularly enlightening. But I agree the default WARN_ON splat tends not to be informative, I'll take a look at if a wrapper could help.

...

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines

  void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) {
      struct realm *realm = &kvm->arch.realm;

arch/arm64/kvm/rme.c +7 -0
@@ -306,6 +588,96 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32
 ia_bits)
       WARN_ON(realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits)));
   }
   +static void realm_unmap_private_range(struct kvm *kvm,
+                      unsigned long start,
+                      unsigned long end)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned long next_addr, addr;
+    int ret;
+
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 603

Quoted 5 lines
arch/arm64/kvm/rme.c +5 -0
+    for (addr = start; addr < end; addr = next_addr) {
+        ret = realm_unmap_private_page(realm, addr, &next_addr);
+
+        if (ret)
+            break;

Do we need yielding CPU here, similar to the shared case ? It may be difficult (not impossible) to define a check point to do that.

Quoted 2 lines

Yes, I've already got a local change adding a "may_block" flag to the function so we can do a cond_resched_rwlock_write() call here when possible.

Cool, thanks.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
arch/arm64/kvm/rme.c +4 -0
+    }
+
+    realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+                 start, end);
Quoted 7 lines
Quoted 1 line

We don't seem to be reclaiing the RTTs from shared mapping case ?

I'm not sure I follow: realm_fold_rtt_level() will free any RTTs that are released.

Or are you referring to the fact that we don't (yet) fold the shared range? I have been purposefully leaving that for now as normally we'd

sorry it was a bit vague. We don't seem to be reclaiming the RTTs in realm_unmap_shared_range(), like we do for the private range.

Quoted 9 lines

follow the page size in the VMM to choose the page size on the guest, but that doesn't work when the RMM might have a different page size to the host. So my reasons for leaving it for later are:

* First huge pages is very much a TODO in general.

* When we support >4K host pages then a huge page on the host may not be available in the RMM, so we can't just follow the VMM.

Quoted 7 lines

* We don't have support in realm_unmap_shared_range() to split a block mapping up - it could be added, but it's not clear to me if it's best to split a block mapping, or remove the whole and refault as required.

* guest_memfd might well be able to provide some good hints here, but we'll have to wait for that series to settle.

I am not sure I follow. None of this affects folding, once we have "unmapped". For that matter, we could easily DESTROY the RTTs in shared side without unmapping, but we can do that later as an
optimisation.

Quoted 54 lines
Quoted 34 lines
Quoted 34 lines
arch/arm64/kvm/rme.c +33 -0
+}
+
+void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
+               unsigned long size, bool unmap_private)
+{
+    unsigned long end = start + size;
+    struct realm *realm = &kvm->arch.realm;
+
+    end = min(BIT(realm->ia_bits - 1), end);
+
+    if (realm->state == REALM_STATE_NONE)
+        return;
+
+    realm_unmap_shared_range(kvm, find_map_level(realm, start, end),
+                 start, end);
+    if (unmap_private)
+        realm_unmap_private_range(kvm, start, end);
+}
+
+static int realm_init_ipa_state(struct realm *realm,
+                unsigned long ipa,
+                unsigned long end)
+{
+    phys_addr_t rd_phys = virt_to_phys(realm->rd);
+    int ret;
+
+    while (ipa < end) {
+        unsigned long next;
+
+        ret = rmi_rtt_init_ripas(rd_phys, ipa, end, &next);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            int err_level = RMI_RETURN_INDEX(ret);
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) · line 643

Quoted 3 lines
arch/arm64/kvm/rme.c +3 -0
+            int level = find_map_level(realm, ipa, end);
+
+            if (WARN_ON(err_level >= level))

I am wondering if WARN_ON() is required here. A buggy VMM could trigger the WARN_ON(). (e.g, INIT_IPA after POPULATE, where L3 table is created.). The only case where it may be worth WARNING is if the level == 3.

I have to admit I've struggled to get my head round this ;)

init_ripas will fail with ERROR_RTT in three cases:

1. (base_align) The base address isn't aligned for the level reached.

2. (rtt_state) The rtte state is !UNASSIGNED.

3. (no_progress) base==walk_top - the while condition should prevent this.

Quoted 6 lines

So I think case 1 is the case we're expecting, and creating RTTs should resolve it.

Case 2 is presumably the case you are concerned about, although it's not because tables have been created, but because INIT_RIPAS is invalid on areas that have been populated already.

Correct, this is the case I was referring to.

Quoted 3 lines

If my reading of the spec is correct, then level == 3 isn't a possible result, so beyond potentially finding RMM bugs I don't think a WARN for

It is almost certainly possible, with L3 page mapping created for POPULATE and a follow up INIT_RIPAS with 2M or even 1G alignment, could lead us to expect that only L1 or L2 is required (find_map_level) but the RTT walk reached L3 and failed. This is not a case of RMM bug, but a VMM not following the rules.

Quoted 2 lines

that is very interesting. So for now I'll just drop the WARN_ON here altogether.

Thanks, that is much safer

Suzuki

#44 lore raw

Re: [PATCH v8 15/43] arm64: RME: Allow VMM to set RIPAS

Quoted 5 lines · Suzuki K Poulose
Quoted 4 lines · Steven Price
Quoted 3 lines · Suzuki K Poulose

Hi Steven

On 16/04/2025 14:41, Steven Price wrote:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines

+    }
+
+    realm_fold_rtt_level(realm, get_start_level(realm) + 1, +                 start, end);

We don't seem to be reclaiing the RTTs from shared mapping case ?

Quoted 8 lines
Quoted 5 lines

I'm not sure I follow: realm_fold_rtt_level() will free any RTTs that are released.

Or are you referring to the fact that we don't (yet) fold the shared range? I have been purposefully leaving that for now as normally we'd

sorry it was a bit vague. We don't seem to be reclaiming the RTTs in realm_unmap_shared_range(), like we do for the private range.

Ah, you mean we potentially leave empty RTTs which are only reclaimed when destroying the realm. Whereas in the private range we opportunistically fold which will (usually) cause these to be freed.

Quoted 14 lines
Quoted 14 lines

follow the page size in the VMM to choose the page size on the guest, but that doesn't work when the RMM might have a different page size to the host. So my reasons for leaving it for later are:

  * First huge pages is very much a TODO in general.

  * When we support >4K host pages then a huge page on the host may not be available in the RMM, so we can't just follow the VMM.

  * We don't have support in realm_unmap_shared_range() to split a block mapping up - it could be added, but it's not clear to me if it's best to split a block mapping, or remove the whole and refault as required.

Quoted 7 lines
Quoted 2 lines

  * guest_memfd might well be able to provide some good hints here, but we'll have to wait for that series to settle.

I am not sure I follow. None of this affects folding, once we have "unmapped". For that matter, we could easily DESTROY the RTTs in shared side without unmapping, but we can do that later as an optimisation.

Yeah, ignore the above - like you say it's not relevant to unmapping, I hadn't understood your point ;)

I'll stick a call to realm_fold_rtt_level() at the end of realm_unmap_shared_range() which should opportunistically free unused RTTs. Like you say there's a optimisation to just straight to destroying the RTTs in the shared region - but I think that's best left until later.

Quoted 64 lines
Quoted 54 lines
Quoted 34 lines
Quoted 34 lines
arch/arm64/kvm/rme.c +33 -0
+}
+
+void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
+               unsigned long size, bool unmap_private)
+{
+    unsigned long end = start + size;
+    struct realm *realm = &kvm->arch.realm;
+
+    end = min(BIT(realm->ia_bits - 1), end);
+
+    if (realm->state == REALM_STATE_NONE)
+        return;
+
+    realm_unmap_shared_range(kvm, find_map_level(realm, start, end),
+                 start, end);
+    if (unmap_private)
+        realm_unmap_private_range(kvm, start, end);
+}
+
+static int realm_init_ipa_state(struct realm *realm,
+                unsigned long ipa,
+                unsigned long end)
+{
+    phys_addr_t rd_phys = virt_to_phys(realm->rd);
+    int ret;
+
+    while (ipa < end) {
+        unsigned long next;
+
+        ret = rmi_rtt_init_ripas(rd_phys, ipa, end, &next);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            int err_level = RMI_RETURN_INDEX(ret);
Quoted 8 lines
Quoted 3 lines

+            int level = find_map_level(realm, ipa, end); +
+            if (WARN_ON(err_level >= level))

I am wondering if WARN_ON() is required here. A buggy VMM could trigger the WARN_ON(). (e.g, INIT_IPA after POPULATE, where L3 table is created.). The only case where it may be worth WARNING is if the level == 3.

I have to admit I've struggled to get my head round this ;)

init_ripas will fail with ERROR_RTT in three cases:

  1. (base_align) The base address isn't aligned for the level reached.

  2. (rtt_state) The rtte state is !UNASSIGNED.

  3. (no_progress) base==walk_top - the while condition should prevent this.

Quoted 6 lines

So I think case 1 is the case we're expecting, and creating RTTs should resolve it.

Case 2 is presumably the case you are concerned about, although it's not because tables have been created, but because INIT_RIPAS is invalid on areas that have been populated already.

Correct, this is the case I was referring to.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

If my reading of the spec is correct, then level == 3 isn't a possible result, so beyond potentially finding RMM bugs I don't think a WARN for

It is almost certainly possible, with L3 page mapping created for POPULATE and a follow up INIT_RIPAS with 2M or even 1G alignment, could lead us to expect that only L1 or L2 is required (find_map_level) but the RTT walk reached L3 and failed. This is not a case of RMM bug, but a VMM not following the rules.

Sorry, I wasn't clear. I mean "level == 3" isn't something that we should be WARNing on.

Quoted 4 lines
Quoted 2 lines

that is very interesting. So for now I'll just drop the WARN_ON here altogether.

Thanks, that is much safer

Ack.

Thanks,
Steve

Quoted 2 lines

Suzuki

#45 lore raw

PATCH v8 16/43 arm64: RME: Handle realm enter/exit

In reply to Steven Price

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---
Changes since v7:
* A return of 0 from kvm_handle_sys_reg() doesn't mean the register has been read (although that can never happen in the current code). Tidy up the condition to handle any future refactoring.
Changes since v6:
* Use vcpu_err() rather than pr_err/kvm_err when there is an associated vcpu to the error.
* Return -EFAULT for KVM_EXIT_MEMORY_FAULT as per the documentation for this exit type.
* Split code handling a RIPAS change triggered by the guest to the following patch.
Changes since v5:
* For a RIPAS_CHANGE request from the guest perform the actual RIPAS change on next entry rather than immediately on the exit. This allows the VMM to 'reject' a RIPAS change by refusing to continue scheduling.
Changes since v4:
* Rename handle_rme_exit() to handle_rec_exit()
* Move the loop to copy registers into the REC enter structure from the to rec_exit_handlers callbacks to kvm_rec_enter(). This fixes a bug where the handler exits to user space and user space wants to modify the GPRS.
* Some code rearrangement in rec_exit_ripas_change().
Changes since v2:
* realm_set_ipa_state() now provides an output parameter for the top_iap that was changed. Use this to signal the VMM with the correct range that has been transitioned.
* Adapt to previous patch changes.

---
 arch/arm64/include/asm/kvm_rme.h |   3 +
 arch/arm64/kvm/Makefile          |   2 +-
 arch/arm64/kvm/arm.c             |  19 +++-
 arch/arm64/kvm/rme-exit.c        | 170 +++++++++++++++++++++++++++++++
 arch/arm64/kvm/rme.c             |  19 ++++
 5 files changed, 207 insertions(+), 6 deletions(-)
 create mode 100644 arch/arm64/kvm/rme-exit.c
arch/arm64/include/asm/kvm_rme.h +3 -0
index b916db8565a2..d86051ef0c5c 100644
@@ -101,6 +101,9 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
 int kvm_create_rec(struct kvm_vcpu *vcpu);
 void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
+int kvm_rec_enter(struct kvm_vcpu *vcpu);
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);
+
 void kvm_realm_unmap_range(struct kvm *kvm,
 			   unsigned long ipa,
 			   unsigned long size,
arch/arm64/kvm/Makefile +1 -1
index 2ebc66812d49..c4b10012faa3 100644
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
 	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
 	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-	 rme.o
+	 rme.o rme-exit.o
 
 kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
 kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/arm.c +14 -5
index 7c0bb1b05f4c..cf707130ef66 100644
@@ -1263,7 +1263,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		trace_kvm_entry(*vcpu_pc(vcpu));
 		guest_timing_enter_irqoff();
 
-		ret = kvm_arm_vcpu_enter_exit(vcpu);
+		if (vcpu_is_rec(vcpu))
+			ret = kvm_rec_enter(vcpu);
+		else
+			ret = kvm_arm_vcpu_enter_exit(vcpu);
 
 		vcpu->mode = OUTSIDE_GUEST_MODE;
 		vcpu->stat.exits++;
@@ -1319,10 +1322,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 		local_irq_enable();
 
-		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
-
 		/* Exit types that need handling before we can be preempted */
-		handle_exit_early(vcpu, ret);
+		if (!vcpu_is_rec(vcpu)) {
+			trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu),
+				       *vcpu_pc(vcpu));
+
+			handle_exit_early(vcpu, ret);
+		}
 
 		preempt_enable();
 
@@ -1345,7 +1351,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 			ret = ARM_EXCEPTION_IL;
 		}
 
-		ret = handle_exit(vcpu, ret);
+		if (vcpu_is_rec(vcpu))
+			ret = handle_rec_exit(vcpu, ret);
+		else
+			ret = handle_exit(vcpu, ret);
 	}
 
 	/* Tell userspace about in-kernel device output levels */
arch/arm64/kvm/rme-exit.c new file +170 -0

170 lines not on this page · raw message

arch/arm64/kvm/rme.c +19 -0
index 33eb793d8bdb..bee9dfe12e03 100644
@@ -863,6 +863,25 @@ void kvm_destroy_realm(struct kvm *kvm)
 	kvm_free_stage2_pgd(&kvm->arch.mmu);
 }
 
+int kvm_rec_enter(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	switch (rec->run->exit.exit_reason) {
+	case RMI_EXIT_HOST_CALL:
+	case RMI_EXIT_PSCI:
+		for (int i = 0; i < REC_RUN_GPRS; i++)
+			rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i);
+		break;
+	}
+
+	if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE)
+		return -EINVAL;
+
+	return rmi_rec_enter(virt_to_phys(rec->rec_page),
+			     virt_to_phys(rec->run));
+}
+
 static void free_rec_aux(struct page **aux_pages,
 			 unsigned int num_aux)
 {
Signature · 1 line
2.43.0
Reviewed-by on v8 16/43
#46 lore raw

Re: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Quoted 35 lines · Steven Price

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---
Changes since v7:
* A return of 0 from kvm_handle_sys_reg() doesn't mean the register has been read (although that can never happen in the current code). Tidy up the condition to handle any future refactoring.
Changes since v6:
* Use vcpu_err() rather than pr_err/kvm_err when there is an associated vcpu to the error.
* Return -EFAULT for KVM_EXIT_MEMORY_FAULT as per the documentation for this exit type.
* Split code handling a RIPAS change triggered by the guest to the following patch.
Changes since v5:
* For a RIPAS_CHANGE request from the guest perform the actual RIPAS change on next entry rather than immediately on the exit. This allows the VMM to 'reject' a RIPAS change by refusing to continue scheduling.
Changes since v4:
* Rename handle_rme_exit() to handle_rec_exit()
* Move the loop to copy registers into the REC enter structure from the to rec_exit_handlers callbacks to kvm_rec_enter(). This fixes a bug where the handler exits to user space and user space wants to modify the GPRS.
* Some code rearrangement in rec_exit_ripas_change().
Changes since v2:
* realm_set_ipa_state() now provides an output parameter for the top_iap that was changed. Use this to signal the VMM with the correct range that has been transitioned.
* Adapt to previous patch changes.

Quoted 9 lines
---
  arch/arm64/include/asm/kvm_rme.h |   3 +
  arch/arm64/kvm/Makefile          |   2 +-
  arch/arm64/kvm/arm.c             |  19 +++-
  arch/arm64/kvm/rme-exit.c        | 170 +++++++++++++++++++++++++++++++
  arch/arm64/kvm/rme.c             |  19 ++++
  5 files changed, 207 insertions(+), 6 deletions(-)

create mode 100644 arch/arm64/kvm/rme-exit.c

One nitpick below.

Quoted 121 lines
arch/arm64/include/asm/kvm_rme.h +3 -0
index b916db8565a2..d86051ef0c5c 100644
@@ -101,6 +101,9 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
  int kvm_create_rec(struct kvm_vcpu *vcpu);
  void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
+int kvm_rec_enter(struct kvm_vcpu *vcpu);
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);
+
  void kvm_realm_unmap_range(struct kvm *kvm,
  			   unsigned long ipa,
  			   unsigned long size,
arch/arm64/kvm/Makefile +1 -1
index 2ebc66812d49..c4b10012faa3 100644
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
  	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
  	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
  	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-	 rme.o
+	 rme.o rme-exit.o
 
  kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
  kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/arm.c +14 -5
index 7c0bb1b05f4c..cf707130ef66 100644
@@ -1263,7 +1263,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  		trace_kvm_entry(*vcpu_pc(vcpu));
  		guest_timing_enter_irqoff();
 
-		ret = kvm_arm_vcpu_enter_exit(vcpu);
+		if (vcpu_is_rec(vcpu))
+			ret = kvm_rec_enter(vcpu);
+		else
+			ret = kvm_arm_vcpu_enter_exit(vcpu);
 
  		vcpu->mode = OUTSIDE_GUEST_MODE;
  		vcpu->stat.exits++;
@@ -1319,10 +1322,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
  		local_irq_enable();
 
-		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
-
  		/* Exit types that need handling before we can be preempted */
-		handle_exit_early(vcpu, ret);
+		if (!vcpu_is_rec(vcpu)) {
+			trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu),
+				       *vcpu_pc(vcpu));
+
+			handle_exit_early(vcpu, ret);
+		}
 
  		preempt_enable();
 
@@ -1345,7 +1351,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  			ret = ARM_EXCEPTION_IL;
  		}
 
-		ret = handle_exit(vcpu, ret);
+		if (vcpu_is_rec(vcpu))
+			ret = handle_rec_exit(vcpu, ret);
+		else
+			ret = handle_exit(vcpu, ret);
  	}
 
  	/* Tell userspace about in-kernel device output levels */
arch/arm64/kvm/rme-exit.c new file +43 -0
new file mode 100644
index 000000000000..a1adf5610455
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#include <linux/kvm_host.h>
+#include <kvm/arm_hypercalls.h>
+#include <kvm/arm_psci.h>
+
+#include <asm/rmi_smc.h>
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_rme.h>
+#include <asm/kvm_mmu.h>
+
+typedef int (*exit_handler_fn)(struct kvm_vcpu *vcpu);
+
+static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	vcpu_err(vcpu, "Unhandled exit reason from realm (ESR: %#llx)\n",
+		 rec->run->exit.esr);
+	return -ENXIO;
+}
+
+static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu)
+{
+	return kvm_handle_guest_abort(vcpu);
+}
+
+static int rec_exit_sync_iabt(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	vcpu_err(vcpu, "Unhandled instruction abort (ESR: %#llx).\n",
+		 rec->run->exit.esr);
+	return -ENXIO;
+}
+
+static int rec_exit_sys_reg(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long esr = kvm_vcpu_get_esr(vcpu);

arch/arm64/kvm/rme-exit.c line 53

Quoted 10 lines
arch/arm64/kvm/rme-exit.c new file +10 -0
+	int rt = kvm_vcpu_sys_get_rt(vcpu);
+	bool is_write = !(esr & 1);
+	int ret;
+
+	if (is_write)
+		vcpu_set_reg(vcpu, rt, rec->run->exit.gprs[0]);
+
+	ret = kvm_handle_sys_reg(vcpu);
+	if (ret > 0 && !is_write)
+		rec->run->enter.gprs[0] = vcpu_get_reg(vcpu, rt);

kvm_handle_sys_reg() always returns positive value, so it can be:

	if (!is_write)
		rc->run->enter.gprs[0] = vcpu_get_reg(vcpu, rt);
Quoted 137 lines
arch/arm64/kvm/rme-exit.c new file +117 -0
+
+	return ret;
+}
+
+static exit_handler_fn rec_exit_handlers[] = {
+	[0 ... ESR_ELx_EC_MAX]	= rec_exit_reason_notimpl,
+	[ESR_ELx_EC_SYS64]	= rec_exit_sys_reg,
+	[ESR_ELx_EC_DABT_LOW]	= rec_exit_sync_dabt,
+	[ESR_ELx_EC_IABT_LOW]	= rec_exit_sync_iabt
+};
+
+static int rec_exit_psci(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+	int i;
+
+	for (i = 0; i < REC_RUN_GPRS; i++)
+		vcpu_set_reg(vcpu, i, rec->run->exit.gprs[i]);
+
+	return kvm_smccc_call_handler(vcpu);
+}
+
+static int rec_exit_ripas_change(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long base = rec->run->exit.ripas_base;
+	unsigned long top = rec->run->exit.ripas_top;
+	unsigned long ripas = rec->run->exit.ripas_value;
+
+	if (!kvm_realm_is_private_address(realm, base) ||
+	    !kvm_realm_is_private_address(realm, top - 1)) {
+		vcpu_err(vcpu, "Invalid RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n",
+			 base, top, ripas);
+		return -EINVAL;
+	}
+
+	/* Exit to VMM, the actual RIPAS change is done on next entry */
+	kvm_prepare_memory_fault_exit(vcpu, base, top - base, false, false,
+				      ripas == RMI_RAM);
+
+	/*
+	 * KVM_EXIT_MEMORY_FAULT requires an return code of -EFAULT, see the
+	 * API documentation
+	 */
+	return -EFAULT;
+}
+
+static void update_arch_timer_irq_lines(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	__vcpu_sys_reg(vcpu, CNTV_CTL_EL0) = rec->run->exit.cntv_ctl;
+	__vcpu_sys_reg(vcpu, CNTV_CVAL_EL0) = rec->run->exit.cntv_cval;
+	__vcpu_sys_reg(vcpu, CNTP_CTL_EL0) = rec->run->exit.cntp_ctl;
+	__vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = rec->run->exit.cntp_cval;
+
+	kvm_realm_timers_update(vcpu);
+}
+
+/*
+ * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
+ * proper exit to userspace.
+ */
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+	u8 esr_ec = ESR_ELx_EC(rec->run->exit.esr);
+	unsigned long status, index;
+
+	status = RMI_RETURN_STATUS(rec_run_ret);
+	index = RMI_RETURN_INDEX(rec_run_ret);
+
+	/*
+	 * If a PSCI_SYSTEM_OFF request raced with a vcpu executing, we might
+	 * see the following status code and index indicating an attempt to run
+	 * a REC when the RD state is SYSTEM_OFF.  In this case, we just need to
+	 * return to user space which can deal with the system event or will try
+	 * to run the KVM VCPU again, at which point we will no longer attempt
+	 * to enter the Realm because we will have a sleep request pending on
+	 * the VCPU as a result of KVM's PSCI handling.
+	 */
+	if (status == RMI_ERROR_REALM && index == 1) {
+		vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
+		return 0;
+	}
+
+	if (rec_run_ret)
+		return -ENXIO;
+
+	vcpu->arch.fault.esr_el2 = rec->run->exit.esr;
+	vcpu->arch.fault.far_el2 = rec->run->exit.far;
+	vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar;
+
+	update_arch_timer_irq_lines(vcpu);
+
+	/* Reset the emulation flags for the next run of the REC */
+	rec->run->enter.flags = 0;
+
+	switch (rec->run->exit.exit_reason) {
+	case RMI_EXIT_SYNC:
+		return rec_exit_handlers[esr_ec](vcpu);
+	case RMI_EXIT_IRQ:
+	case RMI_EXIT_FIQ:
+		return 1;
+	case RMI_EXIT_PSCI:
+		return rec_exit_psci(vcpu);
+	case RMI_EXIT_RIPAS_CHANGE:
+		return rec_exit_ripas_change(vcpu);
+	}
+
+	kvm_pr_unimpl("Unsupported exit reason: %u\n",
+		      rec->run->exit.exit_reason);
+	vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
+	return 0;
+}
arch/arm64/kvm/rme.c +12 -0
index 33eb793d8bdb..bee9dfe12e03 100644
@@ -863,6 +863,25 @@ void kvm_destroy_realm(struct kvm *kvm)
  	kvm_free_stage2_pgd(&kvm->arch.mmu);
  }
 
+int kvm_rec_enter(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	switch (rec->run->exit.exit_reason) {
+	case RMI_EXIT_HOST_CALL:
+	case RMI_EXIT_PSCI:
+		for (int i = 0; i < REC_RUN_GPRS; i++)
+			rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i);
+		break;
+	}
+

arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 887

Quoted 10 lines
arch/arm64/kvm/rme.c +7 -0
+	if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE)
+		return -EINVAL;
+
+	return rmi_rec_enter(virt_to_phys(rec->rec_page),
+			     virt_to_phys(rec->run));
+}
+
  static void free_rec_aux(struct page **aux_pages,
  			 unsigned int num_aux)
  {

Thanks,
Gavin

#47 lore raw

Re: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Quoted 46 lines · Gavin Shan
Quoted 41 lines · Steven Price

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---
Changes since v7:
  * A return of 0 from kvm_handle_sys_reg() doesn't mean the register has been read (although that can never happen in the current code). Tidy up the condition to handle any future refactoring. Changes since v6:
  * Use vcpu_err() rather than pr_err/kvm_err when there is an associated vcpu to the error.
  * Return -EFAULT for KVM_EXIT_MEMORY_FAULT as per the documentation for this exit type.
  * Split code handling a RIPAS change triggered by the guest to the following patch.
Changes since v5:
  * For a RIPAS_CHANGE request from the guest perform the actual RIPAS change on next entry rather than immediately on the exit. This allows the VMM to 'reject' a RIPAS change by refusing to continue scheduling.
Changes since v4:
  * Rename handle_rme_exit() to handle_rec_exit()
  * Move the loop to copy registers into the REC enter structure from the to rec_exit_handlers callbacks to kvm_rec_enter(). This fixes a bug where the handler exits to user space and user space wants to modify the GPRS.
  * Some code rearrangement in rec_exit_ripas_change(). Changes since v2:
  * realm_set_ipa_state() now provides an output parameter for the top_iap that was changed. Use this to signal the VMM with the correct range that has been transitioned.
  * Adapt to previous patch changes.
---
  arch/arm64/include/asm/kvm_rme.h |   3 + arch/arm64/kvm/Makefile          |   2 +- arch/arm64/kvm/arm.c             |  19 +++- arch/arm64/kvm/rme-exit.c        | 170 +++++++++++++++++++++++++++++++ arch/arm64/kvm/rme.c             |  19 ++++

Quoted 3 lines
  5 files changed, 207 insertions(+), 6 deletions(-)

  create mode 100644 arch/arm64/kvm/rme-exit.c

Quoted 3 lines

One nitpick below.

Thanks

[...]

Quoted 62 lines
Quoted 49 lines
arch/arm64/kvm/rme-exit.c new file +43 -0
new file mode 100644
index 000000000000..a1adf5610455
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#include <linux/kvm_host.h>
+#include <kvm/arm_hypercalls.h>
+#include <kvm/arm_psci.h>
+
+#include <asm/rmi_smc.h>
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_rme.h>
+#include <asm/kvm_mmu.h>
+
+typedef int (*exit_handler_fn)(struct kvm_vcpu *vcpu);
+
+static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu)
+{
+    struct realm_rec *rec = &vcpu->arch.rec;
+
+    vcpu_err(vcpu, "Unhandled exit reason from realm (ESR: %#llx)\n",
+         rec->run->exit.esr);
+    return -ENXIO;
+}
+
+static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu)
+{
+    return kvm_handle_guest_abort(vcpu);
+}
+
+static int rec_exit_sync_iabt(struct kvm_vcpu *vcpu)
+{
+    struct realm_rec *rec = &vcpu->arch.rec;
+
+    vcpu_err(vcpu, "Unhandled instruction abort (ESR: %#llx).\n",
+         rec->run->exit.esr);
+    return -ENXIO;
+}
+
+static int rec_exit_sys_reg(struct kvm_vcpu *vcpu)
+{
+    struct realm_rec *rec = &vcpu->arch.rec;
+    unsigned long esr = kvm_vcpu_get_esr(vcpu);

arch/arm64/kvm/rme-exit.c line 53

Quoted 10 lines
arch/arm64/kvm/rme-exit.c new file +10 -0
+    int rt = kvm_vcpu_sys_get_rt(vcpu);
+    bool is_write = !(esr & 1);
+    int ret;
+
+    if (is_write)
+        vcpu_set_reg(vcpu, rt, rec->run->exit.gprs[0]);
+
+    ret = kvm_handle_sys_reg(vcpu);
+    if (ret > 0 && !is_write)
+        rec->run->enter.gprs[0] = vcpu_get_reg(vcpu, rt);

kvm_handle_sys_reg() always returns positive value, so it can be:

Quoted 2 lines

    if (!is_write)
        rc->run->enter.gprs[0] = vcpu_get_reg(vcpu, rt);

I'm not sure the "always returns positive values" is a good justification - that might change in the future (although I guess the only reason it returns '1' is just to have the correct function signature - so it's unlike to change).

However there isn't really any harm here in setting the 'enter' value even if it fails. So I'll drop the "ret > 0" check.

Thanks,
Steve

#48 lore raw

Re: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

In reply to Steven Price
Quoted 91 lines · Steven Price

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---
Changes since v7:
* A return of 0 from kvm_handle_sys_reg() doesn't mean the register has been read (although that can never happen in the current code). Tidy up the condition to handle any future refactoring.
Changes since v6:
* Use vcpu_err() rather than pr_err/kvm_err when there is an associated vcpu to the error.
* Return -EFAULT for KVM_EXIT_MEMORY_FAULT as per the documentation for this exit type.
* Split code handling a RIPAS change triggered by the guest to the following patch.
Changes since v5:
* For a RIPAS_CHANGE request from the guest perform the actual RIPAS change on next entry rather than immediately on the exit. This allows the VMM to 'reject' a RIPAS change by refusing to continue scheduling.
Changes since v4:
* Rename handle_rme_exit() to handle_rec_exit()
* Move the loop to copy registers into the REC enter structure from the to rec_exit_handlers callbacks to kvm_rec_enter(). This fixes a bug where the handler exits to user space and user space wants to modify the GPRS.
* Some code rearrangement in rec_exit_ripas_change().
Changes since v2:
* realm_set_ipa_state() now provides an output parameter for the top_iap that was changed. Use this to signal the VMM with the correct range that has been transitioned.
* Adapt to previous patch changes.

---
  arch/arm64/include/asm/kvm_rme.h |   3 +
  arch/arm64/kvm/Makefile          |   2 +-
  arch/arm64/kvm/arm.c             |  19 +++-
  arch/arm64/kvm/rme-exit.c        | 170 +++++++++++++++++++++++++++++++
  arch/arm64/kvm/rme.c             |  19 ++++
  5 files changed, 207 insertions(+), 6 deletions(-)

create mode 100644 arch/arm64/kvm/rme-exit.c

arch/arm64/include/asm/kvm_rme.h +3 -0
index b916db8565a2..d86051ef0c5c 100644
@@ -101,6 +101,9 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);
  int kvm_create_rec(struct kvm_vcpu *vcpu);
  void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
+int kvm_rec_enter(struct kvm_vcpu *vcpu);
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);
+
  void kvm_realm_unmap_range(struct kvm *kvm,
  			   unsigned long ipa,
  			   unsigned long size,
arch/arm64/kvm/Makefile +1 -1
index 2ebc66812d49..c4b10012faa3 100644
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
  	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
  	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
  	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-	 rme.o
+	 rme.o rme-exit.o
 
  kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
  kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/arm.c +4 -1
index 7c0bb1b05f4c..cf707130ef66 100644
@@ -1263,7 +1263,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  		trace_kvm_entry(*vcpu_pc(vcpu));
  		guest_timing_enter_irqoff();
 
-		ret = kvm_arm_vcpu_enter_exit(vcpu);
+		if (vcpu_is_rec(vcpu))
+			ret = kvm_rec_enter(vcpu);
+		else
+			ret = kvm_arm_vcpu_enter_exit(vcpu);
 
  		vcpu->mode = OUTSIDE_GUEST_MODE;
  		vcpu->stat.exits++;
@@ -1319,10 +1322,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
  		local_irq_enable();
 

arch/arm64/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 1331

Quoted 10 lines
arch/arm64/kvm/arm.c +6 -3
-		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
-
  		/* Exit types that need handling before we can be preempted */
-		handle_exit_early(vcpu, ret);
+		if (!vcpu_is_rec(vcpu)) {
+			trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu),
+				       *vcpu_pc(vcpu));
+
+			handle_exit_early(vcpu, ret);
+		}

minor nit: Looks like we loose the kvm_exit trace. Could we add something in handle_rec_exit ?

Quoted 99 lines
arch/arm64/kvm/arm.c +4 -1
  		preempt_enable();
 
@@ -1345,7 +1351,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  			ret = ARM_EXCEPTION_IL;
  		}
 
-		ret = handle_exit(vcpu, ret);
+		if (vcpu_is_rec(vcpu))
+			ret = handle_rec_exit(vcpu, ret);
+		else
+			ret = handle_exit(vcpu, ret);
  	}
 
  	/* Tell userspace about in-kernel device output levels */
arch/arm64/kvm/rme-exit.c new file +78 -0
new file mode 100644
index 000000000000..a1adf5610455
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#include <linux/kvm_host.h>
+#include <kvm/arm_hypercalls.h>
+#include <kvm/arm_psci.h>
+
+#include <asm/rmi_smc.h>
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_rme.h>
+#include <asm/kvm_mmu.h>
+
+typedef int (*exit_handler_fn)(struct kvm_vcpu *vcpu);
+
+static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	vcpu_err(vcpu, "Unhandled exit reason from realm (ESR: %#llx)\n",
+		 rec->run->exit.esr);
+	return -ENXIO;
+}
+
+static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu)
+{
+	return kvm_handle_guest_abort(vcpu);
+}
+
+static int rec_exit_sync_iabt(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	vcpu_err(vcpu, "Unhandled instruction abort (ESR: %#llx).\n",
+		 rec->run->exit.esr);
+	return -ENXIO;
+}
+
+static int rec_exit_sys_reg(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long esr = kvm_vcpu_get_esr(vcpu);
+	int rt = kvm_vcpu_sys_get_rt(vcpu);
+	bool is_write = !(esr & 1);
+	int ret;
+
+	if (is_write)
+		vcpu_set_reg(vcpu, rt, rec->run->exit.gprs[0]);
+
+	ret = kvm_handle_sys_reg(vcpu);
+	if (ret > 0 && !is_write)
+		rec->run->enter.gprs[0] = vcpu_get_reg(vcpu, rt);
+
+	return ret;
+}
+
+static exit_handler_fn rec_exit_handlers[] = {
+	[0 ... ESR_ELx_EC_MAX]	= rec_exit_reason_notimpl,
+	[ESR_ELx_EC_SYS64]	= rec_exit_sys_reg,
+	[ESR_ELx_EC_DABT_LOW]	= rec_exit_sync_dabt,
+	[ESR_ELx_EC_IABT_LOW]	= rec_exit_sync_iabt
+};
+
+static int rec_exit_psci(struct kvm_vcpu *vcpu)
+{
+	struct realm_rec *rec = &vcpu->arch.rec;
+	int i;
+
+	for (i = 0; i < REC_RUN_GPRS; i++)
+		vcpu_set_reg(vcpu, i, rec->run->exit.gprs[i]);
+
+	return kvm_smccc_call_handler(vcpu);
+}
+
+static int rec_exit_ripas_change(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;

arch/arm64/kvm/rme-exit.c line 88

Quoted 10 lines
arch/arm64/kvm/rme-exit.c new file +10 -0
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long base = rec->run->exit.ripas_base;
+	unsigned long top = rec->run->exit.ripas_top;
+	unsigned long ripas = rec->run->exit.ripas_value;
+
+	if (!kvm_realm_is_private_address(realm, base) ||
+	    !kvm_realm_is_private_address(realm, top - 1)) {
+		vcpu_err(vcpu, "Invalid RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n",
+			 base, top, ripas);

Do we need to set the RMI_REJECT for run->enter.flags REC_ENTER_FLAG_RIPAS_RESPONSE ?

Rest looks fine to me.

Suzuki

#49 lore raw

Re: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Quoted 96 lines · Suzuki K Poulose
Quoted 95 lines · Steven Price

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---
Changes since v7:
  * A return of 0 from kvm_handle_sys_reg() doesn't mean the register has been read (although that can never happen in the current code). Tidy up the condition to handle any future refactoring. Changes since v6:
  * Use vcpu_err() rather than pr_err/kvm_err when there is an associated vcpu to the error.
  * Return -EFAULT for KVM_EXIT_MEMORY_FAULT as per the documentation for this exit type.
  * Split code handling a RIPAS change triggered by the guest to the following patch.
Changes since v5:
  * For a RIPAS_CHANGE request from the guest perform the actual RIPAS change on next entry rather than immediately on the exit. This allows the VMM to 'reject' a RIPAS change by refusing to continue scheduling.
Changes since v4:
  * Rename handle_rme_exit() to handle_rec_exit()
  * Move the loop to copy registers into the REC enter structure from the to rec_exit_handlers callbacks to kvm_rec_enter(). This fixes a bug where the handler exits to user space and user space wants to modify the GPRS.
  * Some code rearrangement in rec_exit_ripas_change(). Changes since v2:
  * realm_set_ipa_state() now provides an output parameter for the top_iap that was changed. Use this to signal the VMM with the correct range that has been transitioned.
  * Adapt to previous patch changes.
---
  arch/arm64/include/asm/kvm_rme.h |   3 + arch/arm64/kvm/Makefile          |   2 +- arch/arm64/kvm/arm.c             |  19 +++- arch/arm64/kvm/rme-exit.c        | 170 +++++++++++++++++++++++++++++++ arch/arm64/kvm/rme.c             |  19 ++++

  5 files changed, 207 insertions(+), 6 deletions(-)

  create mode 100644 arch/arm64/kvm/rme-exit.c

arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index b916db8565a2..d86051ef0c5c 100644

arch/arm64/include/asm/kvm_rme.h +2 -0
@@ -101,6 +101,9 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32
 ia_bits);
   int kvm_create_rec(struct kvm_vcpu *vcpu);
   void kvm_destroy_rec(struct kvm_vcpu *vcpu);
   +int kvm_rec_enter(struct kvm_vcpu *vcpu);
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);
+
   void kvm_realm_unmap_range(struct kvm *kvm,
                  unsigned long ipa,

                 unsigned long size,

arch/arm64/kvm/Makefile +1 -1
index 2ebc66812d49..c4b10012faa3 100644
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o
 pvtime.o \
        vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
        vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
        vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-     rme.o
+     rme.o rme-exit.o

    kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o

arch/arm64/kvm/arm.c +5 -2
index 7c0bb1b05f4c..cf707130ef66 100644
@@ -1263,7 +1263,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
           trace_kvm_entry(*vcpu_pc(vcpu));
           guest_timing_enter_irqoff();
   -        ret = kvm_arm_vcpu_enter_exit(vcpu);
+        if (vcpu_is_rec(vcpu))
+            ret = kvm_rec_enter(vcpu);
+        else
+            ret = kvm_arm_vcpu_enter_exit(vcpu);
             vcpu->mode = OUTSIDE_GUEST_MODE;
           vcpu->stat.exits++;
@@ -1319,10 +1322,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
 *vcpu)
             local_irq_enable();
   -        trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu),
 *vcpu_pc(vcpu));
-
           /* Exit types that need handling before we can be preempted */
-        handle_exit_early(vcpu, ret);
+        if (!vcpu_is_rec(vcpu)) {
Quoted 8 lines

arch/arm64/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 1331

Quoted 5 lines
arch/arm64/kvm/arm.c +5 -0
+            trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu),
+                       *vcpu_pc(vcpu));
+
+            handle_exit_early(vcpu, ret);
+        }

minor nit: Looks like we loose the kvm_exit trace. Could we add something in handle_rec_exit ?

I dropped the trace because we can't provide the PC value. I'm not sure what's best here:

(1). Provide kvm_exit trace but with a bogus PC.
(2). Introduce a new 'kvm_rec_exit'.
(3). Wait until someone comes along with a use-case and hope they
implement what they need.

Obviously my preference is 3 ;)

In practice I haven't actually dropped kvm_entry (so that has a bogus PC), so for now I guess I'll go with (1).

Quoted 102 lines
Quoted 102 lines
arch/arm64/kvm/arm.c +0 -0
             preempt_enable();

  @@ -1345,7 +1351,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
              ret = ARM_EXCEPTION_IL;
          }
  -        ret = handle_exit(vcpu, ret);
+        if (vcpu_is_rec(vcpu))
+            ret = handle_rec_exit(vcpu, ret);
+        else
+            ret = handle_exit(vcpu, ret);
      }
        /* Tell userspace about in-kernel device output levels */

arch/arm64/kvm/rme-exit.c new file +84 -0
new file mode 100644
index 000000000000..a1adf5610455
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#include <linux/kvm_host.h>
+#include <kvm/arm_hypercalls.h>
+#include <kvm/arm_psci.h>
+
+#include <asm/rmi_smc.h>
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_rme.h>
+#include <asm/kvm_mmu.h>
+
+typedef int (*exit_handler_fn)(struct kvm_vcpu *vcpu);
+
+static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu)
+{
+    struct realm_rec *rec = &vcpu->arch.rec;
+
+    vcpu_err(vcpu, "Unhandled exit reason from realm (ESR: %#llx)\n",
+         rec->run->exit.esr);
+    return -ENXIO;
+}
+
+static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu)
+{
+    return kvm_handle_guest_abort(vcpu);
+}
+
+static int rec_exit_sync_iabt(struct kvm_vcpu *vcpu)
+{
+    struct realm_rec *rec = &vcpu->arch.rec;
+
+    vcpu_err(vcpu, "Unhandled instruction abort (ESR: %#llx).\n",
+         rec->run->exit.esr);
+    return -ENXIO;
+}
+
+static int rec_exit_sys_reg(struct kvm_vcpu *vcpu)
+{
+    struct realm_rec *rec = &vcpu->arch.rec;
+    unsigned long esr = kvm_vcpu_get_esr(vcpu);
+    int rt = kvm_vcpu_sys_get_rt(vcpu);
+    bool is_write = !(esr & 1);
+    int ret;
+
+    if (is_write)
+        vcpu_set_reg(vcpu, rt, rec->run->exit.gprs[0]);
+
+    ret = kvm_handle_sys_reg(vcpu);
+    if (ret > 0 && !is_write)
+        rec->run->enter.gprs[0] = vcpu_get_reg(vcpu, rt);
+
+    return ret;
+}
+
+static exit_handler_fn rec_exit_handlers[] = {
+    [0 ... ESR_ELx_EC_MAX]    = rec_exit_reason_notimpl,
+    [ESR_ELx_EC_SYS64]    = rec_exit_sys_reg,
+    [ESR_ELx_EC_DABT_LOW]    = rec_exit_sync_dabt,
+    [ESR_ELx_EC_IABT_LOW]    = rec_exit_sync_iabt
+};
+
+static int rec_exit_psci(struct kvm_vcpu *vcpu)
+{
+    struct realm_rec *rec = &vcpu->arch.rec;
+    int i;
+
+    for (i = 0; i < REC_RUN_GPRS; i++)
+        vcpu_set_reg(vcpu, i, rec->run->exit.gprs[i]);
+
+    return kvm_smccc_call_handler(vcpu);
+}
+
+static int rec_exit_ripas_change(struct kvm_vcpu *vcpu)
+{
+    struct kvm *kvm = vcpu->kvm;
+    struct realm *realm = &kvm->arch.realm;
+    struct realm_rec *rec = &vcpu->arch.rec;
+    unsigned long base = rec->run->exit.ripas_base;
+    unsigned long top = rec->run->exit.ripas_top;
+    unsigned long ripas = rec->run->exit.ripas_value;
+
Quoted 8 lines
Quoted 5 lines
arch/arm64/kvm/rme-exit.c new file +3 -0
+    if (!kvm_realm_is_private_address(realm, base) ||
+        !kvm_realm_is_private_address(realm, top - 1)) {
+        vcpu_err(vcpu, "Invalid RIPAS_CHANGE for %#lx - %#lx, ripas:

%#lx\n",
+             base, top, ripas);

Do we need to set the RMI_REJECT for run->enter.flags REC_ENTER_FLAG_RIPAS_RESPONSE ?

Makes sense, will add.

Thanks,
Steve

Quoted 3 lines

Rest looks fine to me.

Suzuki

#50 lore raw

RE: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

In reply to Steven Price

Hello, I comment below.

Quoted 255 lines

Subject: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---
Changes since v7:
* A return of 0 from kvm_handle_sys_reg() doesn't mean the register has been read (although that can never happen in the current code). Tidy up the condition to handle any future refactoring.
Changes since v6:
* Use vcpu_err() rather than pr_err/kvm_err when there is an associated vcpu to the error.
* Return -EFAULT for KVM_EXIT_MEMORY_FAULT as per the documentation
for
this exit type.
* Split code handling a RIPAS change triggered by the guest to the following patch.
Changes since v5:
* For a RIPAS_CHANGE request from the guest perform the actual RIPAS change on next entry rather than immediately on the exit. This allows the VMM to 'reject' a RIPAS change by refusing to continue scheduling.
Changes since v4:
* Rename handle_rme_exit() to handle_rec_exit()
* Move the loop to copy registers into the REC enter structure from the to rec_exit_handlers callbacks to kvm_rec_enter(). This fixes a bug where the handler exits to user space and user space wants to modify the GPRS.
* Some code rearrangement in rec_exit_ripas_change().
Changes since v2:
* realm_set_ipa_state() now provides an output parameter for the top_iap that was changed. Use this to signal the VMM with the correct range that has been transitioned.
* Adapt to previous patch changes.

---
 arch/arm64/include/asm/kvm_rme.h |   3 +
 arch/arm64/kvm/Makefile          |   2 +-
 arch/arm64/kvm/arm.c             |  19 +++-
 arch/arm64/kvm/rme-exit.c        | 170
+++++++++++++++++++++++++++++++
 arch/arm64/kvm/rme.c             |  19 ++++
 5 files changed, 207 insertions(+), 6 deletions(-)  create mode 100644

arch/arm64/kvm/rme-exit.c

arch/arm64/include/asm/kvm_rme.h +0 -0
index b916db8565a2..d86051ef0c5c 100644
@@ -101,6 +101,9 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32

ia_bits); int kvm_create_rec(struct kvm_vcpu *vcpu); void kvm_destroy_rec(struct kvm_vcpu *vcpu);

arch/arm64/include/asm/kvm_rme.h +3 -0
+int kvm_rec_enter(struct kvm_vcpu *vcpu); int handle_rec_exit(struct
+kvm_vcpu *vcpu, int rec_run_status);
+
 void kvm_realm_unmap_range(struct kvm *kvm,
 			   unsigned long ipa,
 			   unsigned long size,
arch/arm64/kvm/Makefile index renamed from arch/arm64/kvm/Makefile +0 -0

2ebc66812d49..c4b10012faa3 100644

arch/arm64/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o
 \
 	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
 	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
 	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-	 rme.o
+	 rme.o rme-exit.o
 
 kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o

kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o diff --git
a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 7c0bb1b05f4c..cf707130ef66 100644

arch/arm64/kvm/arm.c +14 -5
@@ -1263,7 +1263,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		trace_kvm_entry(*vcpu_pc(vcpu));
 		guest_timing_enter_irqoff();
 
-		ret = kvm_arm_vcpu_enter_exit(vcpu);
+		if (vcpu_is_rec(vcpu))
+			ret = kvm_rec_enter(vcpu);
+		else
+			ret = kvm_arm_vcpu_enter_exit(vcpu);
 
 		vcpu->mode = OUTSIDE_GUEST_MODE;
 		vcpu->stat.exits++;
@@ -1319,10 +1322,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 		local_irq_enable();
 
-		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu),
 *vcpu_pc(vcpu));
-
 		/* Exit types that need handling before we can be preempted */
-		handle_exit_early(vcpu, ret);
+		if (!vcpu_is_rec(vcpu)) {
+			trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu),
+				       *vcpu_pc(vcpu));
+
+			handle_exit_early(vcpu, ret);
+		}
 
 		preempt_enable();
@@ -1345,7 +1351,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 			ret = ARM_EXCEPTION_IL;
 		}
 
-		ret = handle_exit(vcpu, ret);
+		if (vcpu_is_rec(vcpu))
+			ret = handle_rec_exit(vcpu, ret);
+		else
+			ret = handle_exit(vcpu, ret);
 	}
 
 	/* Tell userspace about in-kernel device output levels */ diff --git

a/arch/arm64/kvm/rme-exit.c b/arch/arm64/kvm/rme-exit.c new file mode 100644 index 000000000000..a1adf5610455

arch/arm64/kvm/rme-exit.c new file +81 -0
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#include <linux/kvm_host.h>
+#include <kvm/arm_hypercalls.h>
+#include <kvm/arm_psci.h>
+
+#include <asm/rmi_smc.h>
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_rme.h>
+#include <asm/kvm_mmu.h>
+
+typedef int (*exit_handler_fn)(struct kvm_vcpu *vcpu);
+
+static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	vcpu_err(vcpu, "Unhandled exit reason from realm (ESR: %#llx)\n",
+		 rec->run->exit.esr);
+	return -ENXIO;
+}
+
+static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu) {
+	return kvm_handle_guest_abort(vcpu);
+}
+
+static int rec_exit_sync_iabt(struct kvm_vcpu *vcpu) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	vcpu_err(vcpu, "Unhandled instruction abort (ESR: %#llx).\n",
+		 rec->run->exit.esr);
+	return -ENXIO;
+}
+
+static int rec_exit_sys_reg(struct kvm_vcpu *vcpu) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long esr = kvm_vcpu_get_esr(vcpu);
+	int rt = kvm_vcpu_sys_get_rt(vcpu);
+	bool is_write = !(esr & 1);
+	int ret;
+
+	if (is_write)
+		vcpu_set_reg(vcpu, rt, rec->run->exit.gprs[0]);
+
+	ret = kvm_handle_sys_reg(vcpu);
+	if (ret > 0 && !is_write)
+		rec->run->enter.gprs[0] = vcpu_get_reg(vcpu, rt);
+
+	return ret;
+}
+
+static exit_handler_fn rec_exit_handlers[] = {
+	[0 ... ESR_ELx_EC_MAX]	= rec_exit_reason_notimpl,
+	[ESR_ELx_EC_SYS64]	= rec_exit_sys_reg,
+	[ESR_ELx_EC_DABT_LOW]	= rec_exit_sync_dabt,
+	[ESR_ELx_EC_IABT_LOW]	= rec_exit_sync_iabt
+};
+
+static int rec_exit_psci(struct kvm_vcpu *vcpu) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+	int i;
+
+	for (i = 0; i < REC_RUN_GPRS; i++)
+		vcpu_set_reg(vcpu, i, rec->run->exit.gprs[i]);
+
+	return kvm_smccc_call_handler(vcpu);
+}
+
+static int rec_exit_ripas_change(struct kvm_vcpu *vcpu) {
+	struct kvm *kvm = vcpu->kvm;
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long base = rec->run->exit.ripas_base;
+	unsigned long top = rec->run->exit.ripas_top;
+	unsigned long ripas = rec->run->exit.ripas_value;
+
+	if (!kvm_realm_is_private_address(realm, base) ||
+	    !kvm_realm_is_private_address(realm, top - 1)) {
+		vcpu_err(vcpu, "Invalid RIPAS_CHANGE for %#lx - %#lx,

ripas: %#lx\n",

arch/arm64/kvm/rme-exit.c new file +10 -0
+			 base, top, ripas);
+		return -EINVAL;
+	}
+
+	/* Exit to VMM, the actual RIPAS change is done on next entry */
+	kvm_prepare_memory_fault_exit(vcpu, base, top - base, false, false,
+				      ripas == RMI_RAM);
+
+	/*
+	 * KVM_EXIT_MEMORY_FAULT requires an return code of -EFAULT, see

the

arch/arm64/kvm/rme-exit.c new file +31 -0
+	 * API documentation
+	 */
+	return -EFAULT;
+}
+
+static void update_arch_timer_irq_lines(struct kvm_vcpu *vcpu) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	__vcpu_sys_reg(vcpu, CNTV_CTL_EL0) = rec->run->exit.cntv_ctl;
+	__vcpu_sys_reg(vcpu, CNTV_CVAL_EL0) = rec->run->exit.cntv_cval;
+	__vcpu_sys_reg(vcpu, CNTP_CTL_EL0) = rec->run->exit.cntp_ctl;
+	__vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = rec->run->exit.cntp_cval;
+
+	kvm_realm_timers_update(vcpu);
+}
+
+/*
+ * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason)
+on
+ * proper exit to userspace.
+ */
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+	u8 esr_ec = ESR_ELx_EC(rec->run->exit.esr);
+	unsigned long status, index;
+
+	status = RMI_RETURN_STATUS(rec_run_ret);
+	index = RMI_RETURN_INDEX(rec_run_ret);
+
+	/*
+	 * If a PSCI_SYSTEM_OFF request raced with a vcpu executing, we

might

arch/arm64/kvm/rme-exit.c new file +2 -0
+	 * see the following status code and index indicating an attempt to run
+	 * a REC when the RD state is SYSTEM_OFF.  In this case, we just need

to

arch/arm64/kvm/rme-exit.c line 140

Quoted 9 lines
arch/arm64/kvm/rme-exit.c new file +9 -0
+	 * return to user space which can deal with the system event or will try
+	 * to run the KVM VCPU again, at which point we will no longer attempt
+	 * to enter the Realm because we will have a sleep request pending on
+	 * the VCPU as a result of KVM's PSCI handling.
+	 */
+	if (status == RMI_ERROR_REALM && index == 1) {
+		vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
+		return 0;
+	}

Running kvm-unit-tests-cca selftest(smp) test in quick succession may trigger these conditions, resulting in the following error logs.
Error: KVM exit reason: 0 ("KVM_EXIT_UNKNOWN")

Since KVM_EXIT_UNKNOWN is used when no specific exit reason applies, I think it would be better to make it identifiable as an error. How about adding and setting a new ARM64 exit_reason value to indicate that the PSCI_SYSTEM_OFF request is conflicting with a running vcpu?

Best Regards,
Emi Kisanuki

Quoted 62 lines
arch/arm64/kvm/rme-exit.c new file +30 -0
+
+	if (rec_run_ret)
+		return -ENXIO;
+
+	vcpu->arch.fault.esr_el2 = rec->run->exit.esr;
+	vcpu->arch.fault.far_el2 = rec->run->exit.far;
+	vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar;
+
+	update_arch_timer_irq_lines(vcpu);
+
+	/* Reset the emulation flags for the next run of the REC */
+	rec->run->enter.flags = 0;
+
+	switch (rec->run->exit.exit_reason) {
+	case RMI_EXIT_SYNC:
+		return rec_exit_handlers[esr_ec](vcpu);
+	case RMI_EXIT_IRQ:
+	case RMI_EXIT_FIQ:
+		return 1;
+	case RMI_EXIT_PSCI:
+		return rec_exit_psci(vcpu);
+	case RMI_EXIT_RIPAS_CHANGE:
+		return rec_exit_ripas_change(vcpu);
+	}
+
+	kvm_pr_unimpl("Unsupported exit reason: %u\n",
+		      rec->run->exit.exit_reason);
+	vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
+	return 0;
+}
arch/arm64/kvm/rme.c index renamed from arch/arm64/kvm/rme.c +0 -0

33eb793d8bdb..bee9dfe12e03 100644

arch/arm64/kvm/rme.c +18 -1
@@ -863,6 +863,25 @@ void kvm_destroy_realm(struct kvm *kvm)
 	kvm_free_stage2_pgd(&kvm->arch.mmu);
 }
 
+int kvm_rec_enter(struct kvm_vcpu *vcpu) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	switch (rec->run->exit.exit_reason) {
+	case RMI_EXIT_HOST_CALL:
+	case RMI_EXIT_PSCI:
+		for (int i = 0; i < REC_RUN_GPRS; i++)
+			rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i);
+		break;
+	}
+
+	if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE)
+		return -EINVAL;
+
+	return rmi_rec_enter(virt_to_phys(rec->rec_page),
+			     virt_to_phys(rec->run));
+}
+
 static void free_rec_aux(struct page **aux_pages,
 			 unsigned int num_aux)
 {
--

2.43.0

#51 lore raw

Re: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Quoted 1 line · Emi Kisanuki (Fujitsu)

Hello, I comment below.

Hi Emi,

Quoted 8 lines
Quoted 8 lines

Subject: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---

[..]

Quoted 31 lines
Quoted 19 lines
arch/arm64/kvm/rme-exit.c new file +15 -0
+/*
+ * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason)
+on
+ * proper exit to userspace.
+ */
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+	u8 esr_ec = ESR_ELx_EC(rec->run->exit.esr);
+	unsigned long status, index;
+
+	status = RMI_RETURN_STATUS(rec_run_ret);
+	index = RMI_RETURN_INDEX(rec_run_ret);
+
+	/*
+	 * If a PSCI_SYSTEM_OFF request raced with a vcpu executing, we

might

(quoted diff) +2 -0
+	 * see the following status code and index indicating an attempt to run
+	 * a REC when the RD state is SYSTEM_OFF.  In this case, we just need

to

arch/arm64/kvm/rme-exit.c line 140

Quoted 9 lines
arch/arm64/kvm/rme-exit.c new file +9 -0
+	 * return to user space which can deal with the system event or will try
+	 * to run the KVM VCPU again, at which point we will no longer attempt
+	 * to enter the Realm because we will have a sleep request pending on
+	 * the VCPU as a result of KVM's PSCI handling.
+	 */
+	if (status == RMI_ERROR_REALM && index == 1) {
+		vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
+		return 0;
+	}

Running kvm-unit-tests-cca selftest(smp) test in quick succession may trigger these conditions, resulting in the following error logs.
Error: KVM exit reason: 0 ("KVM_EXIT_UNKNOWN")

Quoted 2 lines

Since KVM_EXIT_UNKNOWN is used when no specific exit reason applies, I think it would be better to make it identifiable as an error. How about adding and setting a new ARM64 exit_reason value to indicate that the PSCI_SYSTEM_OFF request is conflicting with a running vcpu?

Aneesh pointed this out to me off-list. We agreed that KVM_EXIT_SHUTDOWN was more appropriate here. I'll make the change for v9.

Thanks,
Steve

Quoted 65 lines

Best Regards,
Emi Kisanuki

Quoted 62 lines
arch/arm64/kvm/rme-exit.c new file +30 -0
+
+	if (rec_run_ret)
+		return -ENXIO;
+
+	vcpu->arch.fault.esr_el2 = rec->run->exit.esr;
+	vcpu->arch.fault.far_el2 = rec->run->exit.far;
+	vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar;
+
+	update_arch_timer_irq_lines(vcpu);
+
+	/* Reset the emulation flags for the next run of the REC */
+	rec->run->enter.flags = 0;
+
+	switch (rec->run->exit.exit_reason) {
+	case RMI_EXIT_SYNC:
+		return rec_exit_handlers[esr_ec](vcpu);
+	case RMI_EXIT_IRQ:
+	case RMI_EXIT_FIQ:
+		return 1;
+	case RMI_EXIT_PSCI:
+		return rec_exit_psci(vcpu);
+	case RMI_EXIT_RIPAS_CHANGE:
+		return rec_exit_ripas_change(vcpu);
+	}
+
+	kvm_pr_unimpl("Unsupported exit reason: %u\n",
+		      rec->run->exit.exit_reason);
+	vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
+	return 0;
+}
arch/arm64/kvm/rme.c index renamed from arch/arm64/kvm/rme.c +0 -0

33eb793d8bdb..bee9dfe12e03 100644

arch/arm64/kvm/rme.c +18 -1
@@ -863,6 +863,25 @@ void kvm_destroy_realm(struct kvm *kvm)
 	kvm_free_stage2_pgd(&kvm->arch.mmu);
 }
 
+int kvm_rec_enter(struct kvm_vcpu *vcpu) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	switch (rec->run->exit.exit_reason) {
+	case RMI_EXIT_HOST_CALL:
+	case RMI_EXIT_PSCI:
+		for (int i = 0; i < REC_RUN_GPRS; i++)
+			rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i);
+		break;
+	}
+
+	if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE)
+		return -EINVAL;
+
+	return rmi_rec_enter(virt_to_phys(rec->rec_page),
+			     virt_to_phys(rec->run));
+}
+
 static void free_rec_aux(struct page **aux_pages,
 			 unsigned int num_aux)
 {
--

2.43.0

#52 lore raw

Re: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Quoted 47 lines · Steven Price
Quoted 1 line · Emi Kisanuki (Fujitsu)

Hello, I comment below.

Hi Emi,

Quoted 8 lines
Quoted 8 lines

Subject: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes need to be handled slightly differently to the normal KVM path so define our own functions for realm enter/exit and hook them in if the guest is a realm guest.

---

[..]

Quoted 31 lines
Quoted 19 lines
arch/arm64/kvm/rme-exit.c new file +15 -0
+/*
+ * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason)
+on
+ * proper exit to userspace.
+ */
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret) {
+	struct realm_rec *rec = &vcpu->arch.rec;
+	u8 esr_ec = ESR_ELx_EC(rec->run->exit.esr);
+	unsigned long status, index;
+
+	status = RMI_RETURN_STATUS(rec_run_ret);
+	index = RMI_RETURN_INDEX(rec_run_ret);
+
+	/*
+	 * If a PSCI_SYSTEM_OFF request raced with a vcpu executing, we

might

(quoted diff) +2 -0
+	 * see the following status code and index indicating an attempt to run
+	 * a REC when the RD state is SYSTEM_OFF.  In this case, we just need

to

arch/arm64/kvm/rme-exit.c line 140

Quoted 9 lines
arch/arm64/kvm/rme-exit.c new file +9 -0
+	 * return to user space which can deal with the system event or will try
+	 * to run the KVM VCPU again, at which point we will no longer attempt
+	 * to enter the Realm because we will have a sleep request pending on
+	 * the VCPU as a result of KVM's PSCI handling.
+	 */
+	if (status == RMI_ERROR_REALM && index == 1) {
+		vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
+		return 0;
+	}

Running kvm-unit-tests-cca selftest(smp) test in quick succession may trigger these conditions, resulting in the following error logs.
Error: KVM exit reason: 0 ("KVM_EXIT_UNKNOWN")

Quoted 5 lines
Quoted 2 lines

Since KVM_EXIT_UNKNOWN is used when no specific exit reason applies, I think it would be better to make it identifiable as an error. How about adding and setting a new ARM64 exit_reason value to indicate that the PSCI_SYSTEM_OFF request is conflicting with a running vcpu?

Aneesh pointed this out to me off-list. We agreed that KVM_EXIT_SHUTDOWN was more appropriate here. I'll make the change for v9.

Sounds good to me

Cheers
Suzuki

#53 lore raw

PATCH v8 17/43 arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE

In reply to Steven Price

The guest can request that a region of it's protected address space is switched between RIPAS_RAM and RIPAS_EMPTY (and back) using RSI_IPA_STATE_SET. This causes a guest exit with the RMI_EXIT_RIPAS_CHANGE code. We treat this as a request to convert a protected region to unprotected (or back), exiting to the VMM to make the necessary changes to the guest_memfd and memslot mappings. On the next entry the RIPAS changes are committed by making RMI_RTT_SET_RIPAS calls.

The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do with by no longer scheduling the VCPU as we don't currently have a usecase for returning that rejection to the guest, but by postponing the RMI_RTT_SET_RIPAS changes to entry we leave the door open for adding a new ioctl in the future for this purpose.

---
Changes since v7:
* Rework the loop in realm_set_ipa_state() to make it clear when the 'next' output value of rmi_rtt_set_ripas() is used.
New patch for v7: The code was previously split awkwardly between two other patches.

---
 arch/arm64/kvm/rme.c | 88 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
arch/arm64/kvm/rme.c +88 -0
index bee9dfe12e03..fe0d5b8703d2 100644
@@ -624,6 +624,65 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
 		realm_unmap_private_range(kvm, start, end);
 }
 
+static int realm_set_ipa_state(struct kvm_vcpu *vcpu,
+			       unsigned long start,
+			       unsigned long end,
+			       unsigned long ripas,
+			       unsigned long *top_ipa)
+{
+	struct kvm *kvm = vcpu->kvm;
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	phys_addr_t rd_phys = virt_to_phys(realm->rd);
+	phys_addr_t rec_phys = virt_to_phys(rec->rec_page);
+	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+	unsigned long ipa = start;
+	int ret = 0;
+
+	while (ipa < end) {
+		unsigned long next;
+
+		ret = rmi_rtt_set_ripas(rd_phys, rec_phys, ipa, end, &next);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS) {
+			ipa = next;
+		} else if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+			int walk_level = RMI_RETURN_INDEX(ret);
+			int level = find_map_level(realm, ipa, end);
+
+			/*
+			 * If the RMM walk ended early then more tables are
+			 * needed to reach the required depth to set the RIPAS.
+			 */
+			if (walk_level < level) {
+				ret = realm_create_rtt_levels(realm, ipa,
+							      walk_level,
+							      level,
+							      memcache);
+				/* Retry with RTTs created */
+				if (!ret)
+					continue;
+			} else {
+				ret = -EINVAL;
+			}
+
+			break;
+		} else {
+			WARN(1, "Unexpected error in %s: %#x\n", __func__,
+			     ret);
+			ret = -ENXIO;
+			break;
+		}
+	}
+
+	*top_ipa = ipa;
+
+	if (ripas == RMI_EMPTY && ipa != start)
+		realm_unmap_private_range(kvm, start, ipa);
+
+	return ret;
+}
+
 static int realm_init_ipa_state(struct realm *realm,
 				unsigned long ipa,
 				unsigned long end)
@@ -863,6 +922,32 @@ void kvm_destroy_realm(struct kvm *kvm)
 	kvm_free_stage2_pgd(&kvm->arch.mmu);
 }
 
+static void kvm_complete_ripas_change(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long base = rec->run->exit.ripas_base;
+	unsigned long top = rec->run->exit.ripas_top;
+	unsigned long ripas = rec->run->exit.ripas_value;
+	unsigned long top_ipa;
+	int ret;
+
+	do {
+		kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_cache,
+					   kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu));
+		write_lock(&kvm->mmu_lock);
+		ret = realm_set_ipa_state(vcpu, base, top, ripas, &top_ipa);
+		write_unlock(&kvm->mmu_lock);
+
+		if (WARN_RATELIMIT(ret && ret != -ENOMEM,
+				   "Unable to satisfy RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n",
+				   base, top, ripas))
+			break;
+
+		base = top_ipa;
+	} while (top_ipa < top);
+}
+
 int kvm_rec_enter(struct kvm_vcpu *vcpu)
 {
 	struct realm_rec *rec = &vcpu->arch.rec;
@@ -873,6 +958,9 @@ int kvm_rec_enter(struct kvm_vcpu *vcpu)
 		for (int i = 0; i < REC_RUN_GPRS; i++)
 			rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i);
 		break;
+	case RMI_EXIT_RIPAS_CHANGE:
+		kvm_complete_ripas_change(vcpu);
+		break;
 	}
 
 	if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE)
Signature · 1 line
2.43.0
Reviewed-by on v8 17/43
#54 lore raw

Re: [PATCH v8 17/43] arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE

Quoted 22 lines · Steven Price

The guest can request that a region of it's protected address space is switched between RIPAS_RAM and RIPAS_EMPTY (and back) using RSI_IPA_STATE_SET. This causes a guest exit with the RMI_EXIT_RIPAS_CHANGE code. We treat this as a request to convert a protected region to unprotected (or back), exiting to the VMM to make the necessary changes to the guest_memfd and memslot mappings. On the next entry the RIPAS changes are committed by making RMI_RTT_SET_RIPAS calls.

The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do with by no longer scheduling the VCPU as we don't currently have a usecase for returning that rejection to the guest, but by postponing the RMI_RTT_SET_RIPAS changes to entry we leave the door open for adding a new ioctl in the future for this purpose.

---
Changes since v7:
* Rework the loop in realm_set_ipa_state() to make it clear when the 'next' output value of rmi_rtt_set_ripas() is used.
New patch for v7: The code was previously split awkwardly between two other patches.

Quoted 4 lines
---
  arch/arm64/kvm/rme.c | 88 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 88 insertions(+)

One nitpick below, either way:

Quoted 21 lines
arch/arm64/kvm/rme.c +13 -0
index bee9dfe12e03..fe0d5b8703d2 100644
@@ -624,6 +624,65 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
  		realm_unmap_private_range(kvm, start, end);
  }
 
+static int realm_set_ipa_state(struct kvm_vcpu *vcpu,
+			       unsigned long start,
+			       unsigned long end,
+			       unsigned long ripas,
+			       unsigned long *top_ipa)
+{
+	struct kvm *kvm = vcpu->kvm;
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	phys_addr_t rd_phys = virt_to_phys(realm->rd);
+	phys_addr_t rec_phys = virt_to_phys(rec->rec_page);
+	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+	unsigned long ipa = start;

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 649

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+	int ret = 0;
+
+	while (ipa < end) {
+		unsigned long next;
+
+		ret = rmi_rtt_set_ripas(rd_phys, rec_phys, ipa, end, &next);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS) {
+			ipa = next;
+		} else if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {

--->

Quoted 9 lines
arch/arm64/kvm/rme.c +9 -0
+			int walk_level = RMI_RETURN_INDEX(ret);
+			int level = find_map_level(realm, ipa, end);
+
+			/*
+			 * If the RMM walk ended early then more tables are
+			 * needed to reach the required depth to set the RIPAS.
+			 */
+			if (walk_level < level) {
+				ret = realm_create_rtt_levels(realm, ipa,

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 668

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+							      walk_level,
+							      level,
+							      memcache);
+				/* Retry with RTTs created */
+				if (!ret)
+					continue;
+			} else {
+				ret = -EINVAL;
+			}
+

<--- This block of code have been existing in multiple functions. I guess it would be worthy to introduce a helper for it if you agree. Alternatively, it's definitely something to do in the future, after this series is merged :)

Quoted 54 lines
arch/arm64/kvm/rme.c +43 -0
+			break;
+		} else {
+			WARN(1, "Unexpected error in %s: %#x\n", __func__,
+			     ret);
+			ret = -ENXIO;
+			break;
+		}
+	}
+
+	*top_ipa = ipa;
+
+	if (ripas == RMI_EMPTY && ipa != start)
+		realm_unmap_private_range(kvm, start, ipa);
+
+	return ret;
+}
+
  static int realm_init_ipa_state(struct realm *realm,
  				unsigned long ipa,
  				unsigned long end)
@@ -863,6 +922,32 @@ void kvm_destroy_realm(struct kvm *kvm)
  	kvm_free_stage2_pgd(&kvm->arch.mmu);
  }
 
+static void kvm_complete_ripas_change(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long base = rec->run->exit.ripas_base;
+	unsigned long top = rec->run->exit.ripas_top;
+	unsigned long ripas = rec->run->exit.ripas_value;
+	unsigned long top_ipa;
+	int ret;
+
+	do {
+		kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_cache,
+					   kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu));
+		write_lock(&kvm->mmu_lock);
+		ret = realm_set_ipa_state(vcpu, base, top, ripas, &top_ipa);
+		write_unlock(&kvm->mmu_lock);
+
+		if (WARN_RATELIMIT(ret && ret != -ENOMEM,
+				   "Unable to satisfy RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n",
+				   base, top, ripas))
+			break;
+
+		base = top_ipa;
+	} while (top_ipa < top);
+}
+
  int kvm_rec_enter(struct kvm_vcpu *vcpu)
  {
  	struct realm_rec *rec = &vcpu->arch.rec;

arch/arm64/kvm/rme.c int kvm_rec_enter(struct kvm_vcpu *vcpu) · line 966

Quoted 9 lines
arch/arm64/kvm/rme.c +3 -0
@@ -873,6 +958,9 @@ int kvm_rec_enter(struct kvm_vcpu *vcpu)
  		for (int i = 0; i < REC_RUN_GPRS; i++)
  			rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i);
  		break;
+	case RMI_EXIT_RIPAS_CHANGE:
+		kvm_complete_ripas_change(vcpu);
+		break;
  	}
 
  	if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE)

Thanks,
Gavin

#55 lore raw

Re: [PATCH v8 17/43] arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE

Quoted 83 lines · Gavin Shan
Quoted 24 lines · Steven Price

The guest can request that a region of it's protected address space is switched between RIPAS_RAM and RIPAS_EMPTY (and back) using RSI_IPA_STATE_SET. This causes a guest exit with the RMI_EXIT_RIPAS_CHANGE code. We treat this as a request to convert a protected region to unprotected (or back), exiting to the VMM to make the necessary changes to the guest_memfd and memslot mappings. On the next entry the RIPAS changes are committed by making RMI_RTT_SET_RIPAS calls.

The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do with by no longer scheduling the VCPU as we don't currently have a usecase for returning that rejection to the guest, but by postponing the RMI_RTT_SET_RIPAS changes to entry we leave the door open for adding a new ioctl in the future for this purpose.

---
Changes since v7:
  * Rework the loop in realm_set_ipa_state() to make it clear when the 'next' output value of rmi_rtt_set_ripas() is used.
New patch for v7: The code was previously split awkwardly between two other patches.
---
  arch/arm64/kvm/rme.c | 88 ++++++++++++++++++++++++++++++++++++++++++++

Quoted 2 lines
  1 file changed, 88 insertions(+)

One nitpick below, either way:

Quoted 21 lines
arch/arm64/kvm/rme.c +12 -0
index bee9dfe12e03..fe0d5b8703d2 100644
@@ -624,6 +624,65 @@ void kvm_realm_unmap_range(struct kvm *kvm,
 unsigned long start,
           realm_unmap_private_range(kvm, start, end);
   }
   +static int realm_set_ipa_state(struct kvm_vcpu *vcpu,
+                   unsigned long start,
+                   unsigned long end,
+                   unsigned long ripas,
+                   unsigned long *top_ipa)
+{
+    struct kvm *kvm = vcpu->kvm;
+    struct realm *realm = &kvm->arch.realm;
+    struct realm_rec *rec = &vcpu->arch.rec;
+    phys_addr_t rd_phys = virt_to_phys(realm->rd);
+    phys_addr_t rec_phys = virt_to_phys(rec->rec_page);
+    struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+    unsigned long ipa = start;

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, · line 649

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+    int ret = 0;
+
+    while (ipa < end) {
+        unsigned long next;
+
+        ret = rmi_rtt_set_ripas(rd_phys, rec_phys, ipa, end, &next);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS) {
+            ipa = next;
+        } else if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {

--->

Quoted 17 lines
arch/arm64/kvm/rme.c +17 -0
+            int walk_level = RMI_RETURN_INDEX(ret);
+            int level = find_map_level(realm, ipa, end);
+
+            /*
+             * If the RMM walk ended early then more tables are
+             * needed to reach the required depth to set the RIPAS.
+             */
+            if (walk_level < level) {
+                ret = realm_create_rtt_levels(realm, ipa,
+                                  walk_level,
+                                  level,
+                                  memcache);
+                /* Retry with RTTs created */
+                if (!ret)
+                    continue;
+            } else {
+                ret = -EINVAL;
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, · line 668

Quoted 2 lines
arch/arm64/kvm/rme.c +2 -0
+            }
+

<--- This block of code have been existing in multiple functions. I guess it would be worthy to introduce a helper for it if you agree. Alternatively,
it's definitely something to do in the future, after this series is merged :)

I believe it's just two functions: realm_set_ipa_state() and realm_init_ipa_state(). Those two functions are going basically the same thing just at different stages (realm_init before the guest has started, and realm_set when it's running).

I've had a go and combing the two functions, it's a little clunky because of the differences, but I think it's an improvement over the repeated code.

Thanks,
Steve

Quoted 66 lines
Quoted 54 lines
arch/arm64/kvm/rme.c +17 -0
+            break;
+        } else {
+            WARN(1, "Unexpected error in %s: %#x\n", __func__,
+                 ret);
+            ret = -ENXIO;
+            break;
+        }
+    }
+
+    *top_ipa = ipa;
+
+    if (ripas == RMI_EMPTY && ipa != start)
+        realm_unmap_private_range(kvm, start, ipa);
+
+    return ret;
+}
+
   static int realm_init_ipa_state(struct realm *realm,
                   unsigned long ipa,

                  unsigned long end)

arch/arm64/kvm/rme.c +25 -0
@@ -863,6 +922,32 @@ void kvm_destroy_realm(struct kvm *kvm)
       kvm_free_stage2_pgd(&kvm->arch.mmu);
   }
   +static void kvm_complete_ripas_change(struct kvm_vcpu *vcpu)
+{
+    struct kvm *kvm = vcpu->kvm;
+    struct realm_rec *rec = &vcpu->arch.rec;
+    unsigned long base = rec->run->exit.ripas_base;
+    unsigned long top = rec->run->exit.ripas_top;
+    unsigned long ripas = rec->run->exit.ripas_value;
+    unsigned long top_ipa;
+    int ret;
+
+    do {
+        kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_cache,
+                       kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu));
+        write_lock(&kvm->mmu_lock);
+        ret = realm_set_ipa_state(vcpu, base, top, ripas, &top_ipa);
+        write_unlock(&kvm->mmu_lock);
+
+        if (WARN_RATELIMIT(ret && ret != -ENOMEM,
+                   "Unable to satisfy RIPAS_CHANGE for %#lx - %#lx,
 ripas: %#lx\n",
+                   base, top, ripas))
+            break;
+
+        base = top_ipa;
+    } while (top_ipa < top);
+}
+
   int kvm_rec_enter(struct kvm_vcpu *vcpu)
   {

      struct realm_rec *rec = &vcpu->arch.rec;

arch/arm64/kvm/rme.c int kvm_rec_enter(struct kvm_vcpu *vcpu) · line 965

Quoted 8 lines
arch/arm64/kvm/rme.c +3 -0
@@ -873,6 +958,9 @@ int kvm_rec_enter(struct kvm_vcpu *vcpu)
           for (int i = 0; i < REC_RUN_GPRS; i++)
               rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i);
           break;
+    case RMI_EXIT_RIPAS_CHANGE:
+        kvm_complete_ripas_change(vcpu);
+        break;
       }
         if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE)

Thanks,
Gavin

#56 lore raw

Re: [PATCH v8 17/43] arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE

In reply to Steven Price
Quoted 60 lines · Steven Price

The guest can request that a region of it's protected address space is switched between RIPAS_RAM and RIPAS_EMPTY (and back) using RSI_IPA_STATE_SET. This causes a guest exit with the RMI_EXIT_RIPAS_CHANGE code. We treat this as a request to convert a protected region to unprotected (or back), exiting to the VMM to make the necessary changes to the guest_memfd and memslot mappings. On the next entry the RIPAS changes are committed by making RMI_RTT_SET_RIPAS calls.

The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do with by no longer scheduling the VCPU as we don't currently have a usecase for returning that rejection to the guest, but by postponing the RMI_RTT_SET_RIPAS changes to entry we leave the door open for adding a new ioctl in the future for this purpose.

---
Changes since v7:
* Rework the loop in realm_set_ipa_state() to make it clear when the 'next' output value of rmi_rtt_set_ripas() is used.
New patch for v7: The code was previously split awkwardly between two other patches.

---
  arch/arm64/kvm/rme.c | 88 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 88 insertions(+)
arch/arm64/kvm/rme.c +26 -0
index bee9dfe12e03..fe0d5b8703d2 100644
@@ -624,6 +624,65 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
  		realm_unmap_private_range(kvm, start, end);
  }
 
+static int realm_set_ipa_state(struct kvm_vcpu *vcpu,
+			       unsigned long start,
+			       unsigned long end,
+			       unsigned long ripas,
+			       unsigned long *top_ipa)
+{
+	struct kvm *kvm = vcpu->kvm;
+	struct realm *realm = &kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	phys_addr_t rd_phys = virt_to_phys(realm->rd);
+	phys_addr_t rec_phys = virt_to_phys(rec->rec_page);
+	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+	unsigned long ipa = start;
+	int ret = 0;
+
+	while (ipa < end) {
+		unsigned long next;
+
+		ret = rmi_rtt_set_ripas(rd_phys, rec_phys, ipa, end, &next);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS) {
+			ipa = next;
+		} else if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+			int walk_level = RMI_RETURN_INDEX(ret);
+			int level = find_map_level(realm, ipa, end);
+

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 662

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+			/*
+			 * If the RMM walk ended early then more tables are
+			 * needed to reach the required depth to set the RIPAS.
+			 */
+			if (walk_level < level) {
+				ret = realm_create_rtt_levels(realm, ipa,
+							      walk_level,
+							      level,
+							      memcache);
+				/* Retry with RTTs created */

minor nit: Do we need to add a comment here, saying, we stop processing the request if we run out of RTT pages in this go and Realm could retry it.

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 675

Quoted 13 lines
arch/arm64/kvm/rme.c +13 -0
+				if (!ret)
+					continue;
+			} else {
+				ret = -EINVAL;
+			}
+
+			break;
+		} else {
+			WARN(1, "Unexpected error in %s: %#x\n", __func__,
+			     ret);
+			ret = -ENXIO;
+			break;
+		}

minor nit: Following from Gavin's comment on another patch, could switch() make the above code more readable and remove the continue; ?

		switch (RMI_RETURN_STATUS(ret)) {
		case RMI_SUCCESS:
			ipa = next;
			break;
		case RMI_ERROR_RTT: {

		
		}
			break;
		default:
			WARN(..);
			ret = -ENXIO;
			goto out;
		}

I am fine either way.

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 677

Quoted 2 lines
arch/arm64/kvm/rme.c +2 -0
+	}
+

out:

Quoted 31 lines
arch/arm64/kvm/rme.c +24 -0
+	*top_ipa = ipa;
+
+	if (ripas == RMI_EMPTY && ipa != start)
+		realm_unmap_private_range(kvm, start, ipa);
+
+	return ret;
+}
+
  static int realm_init_ipa_state(struct realm *realm,
  				unsigned long ipa,
  				unsigned long end)
@@ -863,6 +922,32 @@ void kvm_destroy_realm(struct kvm *kvm)
  	kvm_free_stage2_pgd(&kvm->arch.mmu);
  }
 
+static void kvm_complete_ripas_change(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long base = rec->run->exit.ripas_base;
+	unsigned long top = rec->run->exit.ripas_top;
+	unsigned long ripas = rec->run->exit.ripas_value;
+	unsigned long top_ipa;
+	int ret;
+
+	do {
+		kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_cache,
+					   kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu));
+		write_lock(&kvm->mmu_lock);
+		ret = realm_set_ipa_state(vcpu, base, top, ripas, &top_ipa);
+		write_unlock(&kvm->mmu_lock);

arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 950

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+
+		if (WARN_RATELIMIT(ret && ret != -ENOMEM,
+				   "Unable to satisfy RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n",
+				   base, top, ripas))
+			break;
+
+		base = top_ipa;
+	} while (top_ipa < top);
+}
+

Rest looks good to me.

Suzuki

#57 lore raw

Re: [PATCH v8 17/43] arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE

Quoted 67 lines · Suzuki K Poulose
Quoted 66 lines · Steven Price

The guest can request that a region of it's protected address space is switched between RIPAS_RAM and RIPAS_EMPTY (and back) using RSI_IPA_STATE_SET. This causes a guest exit with the RMI_EXIT_RIPAS_CHANGE code. We treat this as a request to convert a protected region to unprotected (or back), exiting to the VMM to make the necessary changes to the guest_memfd and memslot mappings. On the next entry the RIPAS changes are committed by making RMI_RTT_SET_RIPAS calls.

The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do with by no longer scheduling the VCPU as we don't currently have a usecase for returning that rejection to the guest, but by postponing the RMI_RTT_SET_RIPAS changes to entry we leave the door open for adding a new ioctl in the future for this purpose.

---
Changes since v7:
  * Rework the loop in realm_set_ipa_state() to make it clear when the 'next' output value of rmi_rtt_set_ripas() is used.
New patch for v7: The code was previously split awkwardly between two other patches.
---
  arch/arm64/kvm/rme.c | 88 ++++++++++++++++++++++++++++++++++++++++++++

  1 file changed, 88 insertions(+)
arch/arm64/kvm/rme.c +31 -0
index bee9dfe12e03..fe0d5b8703d2 100644
@@ -624,6 +624,65 @@ void kvm_realm_unmap_range(struct kvm *kvm,
 unsigned long start,
           realm_unmap_private_range(kvm, start, end);
   }
   +static int realm_set_ipa_state(struct kvm_vcpu *vcpu,
+                   unsigned long start,
+                   unsigned long end,
+                   unsigned long ripas,
+                   unsigned long *top_ipa)
+{
+    struct kvm *kvm = vcpu->kvm;
+    struct realm *realm = &kvm->arch.realm;
+    struct realm_rec *rec = &vcpu->arch.rec;
+    phys_addr_t rd_phys = virt_to_phys(realm->rd);
+    phys_addr_t rec_phys = virt_to_phys(rec->rec_page);
+    struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+    unsigned long ipa = start;
+    int ret = 0;
+
+    while (ipa < end) {
+        unsigned long next;
+
+        ret = rmi_rtt_set_ripas(rd_phys, rec_phys, ipa, end, &next);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_SUCCESS) {
+            ipa = next;
+        } else if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            int walk_level = RMI_RETURN_INDEX(ret);
+            int level = find_map_level(realm, ipa, end);
+
+            /*
+             * If the RMM walk ended early then more tables are
+             * needed to reach the required depth to set the RIPAS.
+             */
+            if (walk_level < level) {
+                ret = realm_create_rtt_levels(realm, ipa,
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 662

Quoted 4 lines
arch/arm64/kvm/rme.c +4 -0
+                                  walk_level,
+                                  level,
+                                  memcache);
+                /* Retry with RTTs created */

minor nit: Do we need to add a comment here, saying, we stop processing the request if we run out of RTT pages in this go and Realm could retry it.

I'm not really sure this is the right place, and following Gavin's comment I've combined this with the INIT_RIPAS code.

Also the situation at the moment isn't that we return to the guest - kvm_complete_ripas_change() will topup the memory cache and retry until the whole region is covered. There is an argument that perhaps it shouldn't, but I'm not sure what a guest can usefully do beyond retry in the case of a partial RIPAS change. In which case why have the overhead of returning to the guest?

Quoted 32 lines

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 675

Quoted 13 lines
arch/arm64/kvm/rme.c +13 -0
+                if (!ret)
+                    continue;
+            } else {
+                ret = -EINVAL;
+            }
+
+            break;
+        } else {
+            WARN(1, "Unexpected error in %s: %#x\n", __func__,
+                 ret);
+            ret = -ENXIO;
+            break;
+        }

minor nit: Following from Gavin's comment on another patch, could switch() make the above code more readable and remove the continue; ?

        switch (RMI_RETURN_STATUS(ret)) {
        case RMI_SUCCESS:
            ipa = next;
            break;
        case RMI_ERROR_RTT: {


        }
            break;
        default:
            WARN(..);
            ret = -ENXIO;
            goto out;
        }

Quoted 1 line

I am fine either way.

Since I'm combining the two functions, I've kept just the switch() version of the code.

Quoted 40 lines

arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, · line 677

Quoted 2 lines
arch/arm64/kvm/rme.c +2 -0
+    }
+

out:

Quoted 35 lines
arch/arm64/kvm/rme.c +8 -0
+    *top_ipa = ipa;
+
+    if (ripas == RMI_EMPTY && ipa != start)
+        realm_unmap_private_range(kvm, start, ipa);
+
+    return ret;
+}
+
   static int realm_init_ipa_state(struct realm *realm,
                   unsigned long ipa,

                  unsigned long end)

arch/arm64/kvm/rme.c +19 -0
@@ -863,6 +922,32 @@ void kvm_destroy_realm(struct kvm *kvm)
       kvm_free_stage2_pgd(&kvm->arch.mmu);
   }
   +static void kvm_complete_ripas_change(struct kvm_vcpu *vcpu)
+{
+    struct kvm *kvm = vcpu->kvm;
+    struct realm_rec *rec = &vcpu->arch.rec;
+    unsigned long base = rec->run->exit.ripas_base;
+    unsigned long top = rec->run->exit.ripas_top;
+    unsigned long ripas = rec->run->exit.ripas_value;
+    unsigned long top_ipa;
+    int ret;
+
+    do {
+        kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_cache,
+                       kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu));
+        write_lock(&kvm->mmu_lock);
+        ret = realm_set_ipa_state(vcpu, base, top, ripas, &top_ipa);
+        write_unlock(&kvm->mmu_lock);
+
+        if (WARN_RATELIMIT(ret && ret != -ENOMEM,
+                   "Unable to satisfy RIPAS_CHANGE for %#lx - %#lx,
 ripas: %#lx\n",
+                   base, top, ripas))
Quoted 8 lines

arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 950

Quoted 6 lines
arch/arm64/kvm/rme.c +6 -0
+            break;
+
+        base = top_ipa;
+    } while (top_ipa < top);
+}
+

Rest looks good to me.

Thanks,
Steve

#58 lore raw

PATCH v8 18/43 KVM: arm64: Handle realm MMIO emulation

In reply to Steven Price

MMIO emulation for a realm cannot be done directly with the VM's registers as they are protected from the host. However, for emulatable data aborts, the RMM uses GPRS[0] to provide the read/written value. We can transfer this from/to the equivalent VCPU's register entry and then depend on the generic MMIO handling code in KVM.

For a MMIO read, the value is placed in the shared RecExit structure during kvm_handle_mmio_return() rather than in the VCPU's register entry.

---
Changes since v7:
* New comment for rec_exit_sync_dabt() explaining the call to vcpu_set_reg().
Changes since v5:
* Inject SEA to the guest is an emulatable MMIO access triggers a data abort.
* kvm_handle_mmio_return() - disable kvm_incr_pc() for a REC (as the PC isn't under the host's control) and move the REC_ENTER_EMULATED_MMIO flag setting to this location (as that tells the RMM to skip the instruction).

---
 arch/arm64/kvm/inject_fault.c |  4 +++-
 arch/arm64/kvm/mmio.c         | 16 ++++++++++++----
 arch/arm64/kvm/rme-exit.c     | 14 ++++++++++++++
 3 files changed, 29 insertions(+), 5 deletions(-)
arch/arm64/kvm/inject_fault.c +3 -1
index a640e839848e..2a9682b9834f 100644
@@ -165,7 +165,9 @@ static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt, u32 addr)
  */
 void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
 {
-	if (vcpu_el1_is_32bit(vcpu))
+	if (unlikely(vcpu_is_rec(vcpu)))
+		vcpu->arch.rec.run->enter.flags |= REC_ENTER_FLAG_INJECT_SEA;
+	else if (vcpu_el1_is_32bit(vcpu))
 		inject_abt32(vcpu, false, addr);
 	else
 		inject_abt64(vcpu, false, addr);
arch/arm64/kvm/mmio.c +12 -4
index ab365e839874..bff89d47a4d5 100644
@@ -6,6 +6,7 @@
 
 #include <linux/kvm_host.h>
 #include <asm/kvm_emulate.h>
+#include <asm/rmi_smc.h>
 #include <trace/events/kvm.h>
 
 #include "trace.h"
@@ -136,14 +137,21 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
 			       &data);
 		data = vcpu_data_host_to_guest(vcpu, data, len);
-		vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
+
+		if (vcpu_is_rec(vcpu))
+			vcpu->arch.rec.run->enter.gprs[0] = data;
+		else
+			vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
 	}
 
 	/*
 	 * The MMIO instruction is emulated and should not be re-executed
 	 * in the guest.
 	 */
-	kvm_incr_pc(vcpu);
+	if (vcpu_is_rec(vcpu))
+		vcpu->arch.rec.run->enter.flags |= REC_ENTER_FLAG_EMULATED_MMIO;
+	else
+		kvm_incr_pc(vcpu);
 
 	return 1;
 }
@@ -162,14 +170,14 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
 	 * No valid syndrome? Ask userspace for help if it has
 	 * volunteered to do so, and bail out otherwise.
 	 *
-	 * In the protected VM case, there isn't much userspace can do
+	 * In the protected/realm VM case, there isn't much userspace can do
 	 * though, so directly deliver an exception to the guest.
 	 */
 	if (!kvm_vcpu_dabt_isvalid(vcpu)) {
 		trace_kvm_mmio_nisv(*vcpu_pc(vcpu), kvm_vcpu_get_esr(vcpu),
 				    kvm_vcpu_get_hfar(vcpu), fault_ipa);
 
-		if (vcpu_is_protected(vcpu)) {
+		if (vcpu_is_protected(vcpu) || vcpu_is_rec(vcpu)) {
 			kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
 			return 1;
 		}
arch/arm64/kvm/rme-exit.c +14 -0
index a1adf5610455..6830dc080610 100644
@@ -25,6 +25,20 @@ static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu)
 
 static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu)
 {
+	struct realm_rec *rec = &vcpu->arch.rec;
+
+	/*
+	 * In the case of a write, copy over gprs[0] to the target GPR,
+	 * preparing to handle MMIO write fault. The content to be written has
+	 * been saved to gprs[0] by the RMM (even if another register was used
+	 * by the guest). In the case of normal memory access this is redundant
+	 * (the guest will replay the instruction), but the overhead is
+	 * minimal.
+	 */
+	if (kvm_vcpu_dabt_iswrite(vcpu) && kvm_vcpu_dabt_isvalid(vcpu))
+		vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu),
+			     rec->run->exit.gprs[0]);
+
 	return kvm_handle_guest_abort(vcpu);
 }
 
Signature · 1 line
2.43.0
reviewer Reviewed-by on v8 18/43
#59 lore raw

Re: [PATCH v8 18/43] KVM: arm64: Handle realm MMIO emulation

Quoted 6 lines · Steven Price

MMIO emulation for a realm cannot be done directly with the VM's registers as they are protected from the host. However, for emulatable data aborts, the RMM uses GPRS[0] to provide the read/written value. We can transfer this from/to the equivalent VCPU's register entry and then depend on the generic MMIO handling code in KVM.

Quoted 6 lines

For a MMIO read, the value is placed in the shared RecExit structure during kvm_handle_mmio_return() rather than in the VCPU's register entry.

#60 lore raw

PATCH v8 19/43 arm64: RME: Allow populating initial contents

In reply to Steven Price

The VMM needs to populate the realm with some data before starting (e.g. a kernel and initrd). This is measured by the RMM and used as part of the attestation later on.

---
Changes since v7:
* Improve the error codes.
* Other minor changes from review.
Changes since v6:
* Handle host potentially having a larger page size than the RMM granule.
* Drop historic "par" (protected address range) from populate_par_region() - it doesn't exist within the current architecture.
* Add a cond_resched() call in kvm_populate_realm().
Changes since v5:
* Refactor to use PFNs rather than tracking struct page in realm_create_protected_data_page().
* Pull changes from a later patch (in the v5 series) for accessing pages from a guest memfd.
* Do the populate in chunks to avoid holding locks for too long and triggering RCU stall warnings.

---
 arch/arm64/kvm/rme.c | 227 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 227 insertions(+)
arch/arm64/kvm/rme.c +227 -0
index fe0d5b8703d2..f6af3ea6ea8a 100644
@@ -624,6 +624,221 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,
 		realm_unmap_private_range(kvm, start, end);
 }
 
+static int realm_create_protected_data_granule(struct realm *realm,
+					       unsigned long ipa,
+					       phys_addr_t dst_phys,
+					       phys_addr_t src_phys,
+					       unsigned long flags)
+{
+	phys_addr_t rd = virt_to_phys(realm->rd);
+	int ret;
+
+	if (rmi_granule_delegate(dst_phys))
+		return -ENXIO;
+
+	ret = rmi_data_create(rd, dst_phys, ipa, src_phys, flags);
+	if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+		/* Create missing RTTs and retry */
+		int level = RMI_RETURN_INDEX(ret);
+
+		WARN_ON(level == RMM_RTT_MAX_LEVEL);
+
+		ret = realm_create_rtt_levels(realm, ipa, level,
+					      RMM_RTT_MAX_LEVEL, NULL);
+		if (ret)
+			return -EIO;
+
+		ret = rmi_data_create(rd, dst_phys, ipa, src_phys, flags);
+	}
+	if (ret)
+		return -EIO;
+
+	return 0;
+}
+
+static int realm_create_protected_data_page(struct realm *realm,
+					    unsigned long ipa,
+					    kvm_pfn_t dst_pfn,
+					    kvm_pfn_t src_pfn,
+					    unsigned long flags)
+{
+	unsigned long rd = virt_to_phys(realm->rd);
+	phys_addr_t dst_phys, src_phys;
+	bool undelegate_failed = false;
+	int ret, offset;
+
+	dst_phys = __pfn_to_phys(dst_pfn);
+	src_phys = __pfn_to_phys(src_pfn);
+
+	for (offset = 0; offset < PAGE_SIZE; offset += RMM_PAGE_SIZE) {
+		ret = realm_create_protected_data_granule(realm,
+							  ipa,
+							  dst_phys,
+							  src_phys,
+							  flags);
+		if (ret)
+			goto err;
+
+		ipa += RMM_PAGE_SIZE;
+		dst_phys += RMM_PAGE_SIZE;
+		src_phys += RMM_PAGE_SIZE;
+	}
+
+	return 0;
+
+err:
+	if (ret == -EIO) {
+		/* current offset needs undelegating */
+		if (WARN_ON(rmi_granule_undelegate(dst_phys)))
+			undelegate_failed = true;
+	}
+	while (offset > 0) {
+		ipa -= RMM_PAGE_SIZE;
+		offset -= RMM_PAGE_SIZE;
+		dst_phys -= RMM_PAGE_SIZE;
+
+		rmi_data_destroy(rd, ipa, NULL, NULL);
+
+		if (WARN_ON(rmi_granule_undelegate(dst_phys)))
+			undelegate_failed = true;
+	}
+
+	if (undelegate_failed) {
+		/*
+		 * A granule could not be undelegated,
+		 * so the page has to be leaked
+		 */
+		get_page(pfn_to_page(dst_pfn));
+	}
+
+	return -ENXIO;
+}
+
+static int populate_region(struct kvm *kvm,
+			   phys_addr_t ipa_base,
+			   phys_addr_t ipa_end,
+			   unsigned long data_flags)
+{
+	struct realm *realm = &kvm->arch.realm;
+	struct kvm_memory_slot *memslot;
+	gfn_t base_gfn, end_gfn;
+	int idx;
+	phys_addr_t ipa = ipa_base;
+	int ret = 0;
+
+	base_gfn = gpa_to_gfn(ipa_base);
+	end_gfn = gpa_to_gfn(ipa_end);
+
+	idx = srcu_read_lock(&kvm->srcu);
+	memslot = gfn_to_memslot(kvm, base_gfn);
+	if (!memslot) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	/* We require the region to be contained within a single memslot */
+	if (memslot->base_gfn + memslot->npages < end_gfn) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	if (!kvm_slot_can_be_private(memslot)) {
+		ret = -EPERM;
+		goto out;
+	}
+
+	while (ipa < ipa_end) {
+		struct vm_area_struct *vma;
+		unsigned long hva;
+		struct page *page;
+		bool writeable;
+		kvm_pfn_t pfn;
+		kvm_pfn_t priv_pfn;
+		struct page *gmem_page;
+
+		hva = gfn_to_hva_memslot(memslot, gpa_to_gfn(ipa));
+		vma = vma_lookup(current->mm, hva);
+		if (!vma) {
+			ret = -EFAULT;
+			break;
+		}
+
+		pfn = __kvm_faultin_pfn(memslot, gpa_to_gfn(ipa), FOLL_WRITE,
+					&writeable, &page);
+
+		if (is_error_pfn(pfn)) {
+			ret = -EFAULT;
+			break;
+		}
+
+		ret = kvm_gmem_get_pfn(kvm, memslot,
+				       ipa >> PAGE_SHIFT,
+				       &priv_pfn, &gmem_page, NULL);
+		if (ret)
+			break;
+
+		ret = realm_create_protected_data_page(realm, ipa,
+						       priv_pfn,
+						       pfn,
+						       data_flags);
+
+		kvm_release_page_clean(page);
+
+		if (ret)
+			break;
+
+		ipa += PAGE_SIZE;
+	}
+
+out:
+	srcu_read_unlock(&kvm->srcu, idx);
+	return ret;
+}
+
+static int kvm_populate_realm(struct kvm *kvm,
+			      struct arm_rme_populate_realm *args)
+{
+	phys_addr_t ipa_base, ipa_end;
+	unsigned long data_flags = 0;
+
+	if (kvm_realm_state(kvm) != REALM_STATE_NEW)
+		return -EPERM;
+
+	if (!IS_ALIGNED(args->base, PAGE_SIZE) ||
+	    !IS_ALIGNED(args->size, PAGE_SIZE) ||
+	    (args->flags & ~RMI_MEASURE_CONTENT))
+		return -EINVAL;
+
+	ipa_base = args->base;
+	ipa_end = ipa_base + args->size;
+
+	if (ipa_end < ipa_base)
+		return -EINVAL;
+
+	if (args->flags & RMI_MEASURE_CONTENT)
+		data_flags |= RMI_MEASURE_CONTENT;
+
+	/*
+	 * Perform the population in parts to ensure locks are not held for too
+	 * long
+	 */
+	while (ipa_base < ipa_end) {
+		phys_addr_t end = min(ipa_end, ipa_base + SZ_2M);
+
+		int ret = populate_region(kvm, ipa_base, end,
+					  args->flags);
+
+		if (ret)
+			return ret;
+
+		ipa_base = end;
+
+		cond_resched();
+	}
+
+	return 0;
+}
+
 static int realm_set_ipa_state(struct kvm_vcpu *vcpu,
 			       unsigned long start,
 			       unsigned long end,
@@ -874,6 +1089,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 		r = kvm_init_ipa_range_realm(kvm, &args);
 		break;
 	}
+	case KVM_CAP_ARM_RME_POPULATE_REALM: {
+		struct arm_rme_populate_realm args;
+		void __user *argp = u64_to_user_ptr(cap->args[1]);
+
+		if (copy_from_user(&args, argp, sizeof(args))) {
+			r = -EFAULT;
+			break;
+		}
+
+		r = kvm_populate_realm(kvm, &args);
+		break;
+	}
 	default:
 		r = -EINVAL;
 		break;
Signature · 1 line
2.43.0
#61 lore raw

PATCH v8 20/43 arm64: RME: Runtime faulting of memory

In reply to Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

---
Changes since v7:
* Remove redundant WARN_ONs for realm_create_rtt_levels() - it will internally WARN when necessary.
Changes since v6:
* Handle PAGE_SIZE being larger than RMM granule size.
* Some minor renaming following review comments.
Changes since v5:
* Reduce use of struct page in preparation for supporting the RMM having a different page size to the host.
* Handle a race when delegating a page where another CPU has faulted on a the same page (and already delegated the physical page) but not yet mapped it. In this case simply return to the guest to either use the mapping from the other CPU (or refault if the race is lost).
* The changes to populate_par_region() are moved into the previous patch where they belong.
Changes since v4:
* Code cleanup following review feedback.
* Drop the PTE_SHARED bit when creating unprotected page table entries. This is now set by the RMM and the host has no control of it and the spec requires the bit to be set to zero.
Changes since v2:
* Avoid leaking memory if failing to map it in the realm.
* Correctly mask RTT based on LPA2 flag (see rtt_get_phys()).
* Adapt to changes in previous patches.

---
 arch/arm64/include/asm/kvm_emulate.h |  10 ++
 arch/arm64/include/asm/kvm_rme.h     |  10 ++
 arch/arm64/kvm/mmu.c                 | 127 ++++++++++++++++++-
 arch/arm64/kvm/rme.c                 | 180 +++++++++++++++++++++++++++
 4 files changed, 321 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +10 -0
index c803c8188d9c..def439d6d732 100644
@@ -704,6 +704,16 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)
 	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
 }
 
+static inline gpa_t kvm_gpa_from_fault(struct kvm *kvm, phys_addr_t ipa)
+{
+	if (kvm_is_realm(kvm)) {
+		struct realm *realm = &kvm->arch.realm;
+
+		return ipa & ~BIT(realm->ia_bits - 1);
+	}
+	return ipa;
+}
+
 static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
 {
 	if (static_branch_unlikely(&kvm_rme_is_available))
arch/arm64/include/asm/kvm_rme.h +10 -0
index d86051ef0c5c..47aa6362c6c9 100644
@@ -108,6 +108,16 @@ void kvm_realm_unmap_range(struct kvm *kvm,
 			   unsigned long ipa,
 			   unsigned long size,
 			   bool unmap_private);
+int realm_map_protected(struct realm *realm,
+			unsigned long base_ipa,
+			kvm_pfn_t pfn,
+			unsigned long size,
+			struct kvm_mmu_memory_cache *memcache);
+int realm_map_non_secure(struct realm *realm,
+			 unsigned long ipa,
+			 kvm_pfn_t pfn,
+			 unsigned long size,
+			 struct kvm_mmu_memory_cache *memcache);
 
 static inline bool kvm_realm_is_private_address(struct realm *realm,
 						unsigned long addr)
arch/arm64/kvm/mmu.c +121 -6

200 lines not on this page · raw message

arch/arm64/kvm/rme.c +180 -0

186 lines not on this page · raw message

Signature · 1 line
2.43.0
#62 lore raw

Re: [PATCH v8 20/43] arm64: RME: Runtime faulting of memory

Quoted 92 lines · Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

---
Changes since v7:
* Remove redundant WARN_ONs for realm_create_rtt_levels() - it will internally WARN when necessary.
Changes since v6:
* Handle PAGE_SIZE being larger than RMM granule size.
* Some minor renaming following review comments.
Changes since v5:
* Reduce use of struct page in preparation for supporting the RMM having a different page size to the host.
* Handle a race when delegating a page where another CPU has faulted on a the same page (and already delegated the physical page) but not yet mapped it. In this case simply return to the guest to either use the mapping from the other CPU (or refault if the race is lost).
* The changes to populate_par_region() are moved into the previous patch where they belong.
Changes since v4:
* Code cleanup following review feedback.
* Drop the PTE_SHARED bit when creating unprotected page table entries. This is now set by the RMM and the host has no control of it and the spec requires the bit to be set to zero.
Changes since v2:
* Avoid leaking memory if failing to map it in the realm.
* Correctly mask RTT based on LPA2 flag (see rtt_get_phys()).
* Adapt to changes in previous patches.

---
  arch/arm64/include/asm/kvm_emulate.h |  10 ++
  arch/arm64/include/asm/kvm_rme.h     |  10 ++
  arch/arm64/kvm/mmu.c                 | 127 ++++++++++++++++++-
  arch/arm64/kvm/rme.c                 | 180 +++++++++++++++++++++++++++
  4 files changed, 321 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +10 -0
index c803c8188d9c..def439d6d732 100644
@@ -704,6 +704,16 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)
  	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
  }
 
+static inline gpa_t kvm_gpa_from_fault(struct kvm *kvm, phys_addr_t ipa)
+{
+	if (kvm_is_realm(kvm)) {
+		struct realm *realm = &kvm->arch.realm;
+
+		return ipa & ~BIT(realm->ia_bits - 1);
+	}
+	return ipa;
+}
+
  static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
  {
  	if (static_branch_unlikely(&kvm_rme_is_available))
arch/arm64/include/asm/kvm_rme.h +10 -0
index d86051ef0c5c..47aa6362c6c9 100644
@@ -108,6 +108,16 @@ void kvm_realm_unmap_range(struct kvm *kvm,
  			   unsigned long ipa,
  			   unsigned long size,
  			   bool unmap_private);
+int realm_map_protected(struct realm *realm,
+			unsigned long base_ipa,
+			kvm_pfn_t pfn,
+			unsigned long size,
+			struct kvm_mmu_memory_cache *memcache);
+int realm_map_non_secure(struct realm *realm,
+			 unsigned long ipa,
+			 kvm_pfn_t pfn,
+			 unsigned long size,
+			 struct kvm_mmu_memory_cache *memcache);
 
  static inline bool kvm_realm_is_private_address(struct realm *realm,
  						unsigned long addr)
arch/arm64/kvm/mmu.c +0 -1
index 71c04259e39f..02b66ee35426 100644
@@ -338,8 +338,13 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64
 
  	lockdep_assert_held_write(&kvm->mmu_lock);
  	WARN_ON(size & ~PAGE_MASK);
-	WARN_ON(stage2_apply_range(mmu, start, end, KVM_PGT_FN(kvm_pgtable_stage2_unmap),

arch/arm64/kvm/mmu.c static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 · line 349

Quoted 10 lines
arch/arm64/kvm/mmu.c +7 -1
-				   may_block));
+
+	if (kvm_is_realm(kvm))
+		kvm_realm_unmap_range(kvm, start, size, !only_shared);
+	else
+		WARN_ON(stage2_apply_range(mmu, start, end,
+					   KVM_PGT_FN(kvm_pgtable_stage2_unmap),
+					   may_block));
  }
 

As spotted previsouly, the parameter @may_block isn't handled by kvm_realm_unmap_range().

Quoted 25 lines
arch/arm64/kvm/mmu.c +8 -1
  void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
@@ -359,7 +364,10 @@ static void stage2_flush_memslot(struct kvm *kvm,
  	phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  	phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
 
-	kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
+	if (kvm_is_realm(kvm))
+		kvm_realm_unmap_range(kvm, addr, end - addr, false);
+	else
+		kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
  }
 
  /**
@@ -1053,6 +1061,10 @@ void stage2_unmap_vm(struct kvm *kvm)
  	struct kvm_memory_slot *memslot;
  	int idx, bkt;
 
+	/* For realms this is handled by the RMM so nothing to do here */
+	if (kvm_is_realm(kvm))
+		return;
+
  	idx = srcu_read_lock(&kvm->srcu);
  	mmap_read_lock(current->mm);
  	write_lock(&kvm->mmu_lock);

arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1095

Quoted 6 lines
arch/arm64/kvm/mmu.c +1 -0
@@ -1078,6 +1090,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
  	if (kvm_is_realm(kvm) &&
  	    (kvm_realm_state(kvm) != REALM_STATE_DEAD &&
  	     kvm_realm_state(kvm) != REALM_STATE_NONE)) {
+		kvm_stage2_unmap_range(mmu, 0, (~0ULL) & PAGE_MASK, false);
  		write_unlock(&kvm->mmu_lock);
  		kvm_realm_destroy_rtts(kvm, pgt->ia_bits);

(~0ULL & PAGE_MASK) wouldn't be a problem since the range will be limited to [0, BIT(realm->ia_bits) - 1] in kvm_realm_unmap_range(). I think it's reasonable to pass the maximal size here, something like:

		kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), false);
Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
@@ -1482,6 +1495,82 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)
  	return vma->vm_flags & VM_MTE_ALLOWED;
  }

arch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) · line 1506

Quoted 10 lines
arch/arm64/kvm/mmu.c +9 -0
 
+static int realm_map_ipa(struct kvm *kvm, phys_addr_t ipa,
+			 kvm_pfn_t pfn, unsigned long map_size,
+			 enum kvm_pgtable_prot prot,
+			 struct kvm_mmu_memory_cache *memcache)
+{
+	struct realm *realm = &kvm->arch.realm;
+
+	if (WARN_ON(!(prot & KVM_PGTABLE_PROT_W)))
+		return -EFAULT;

A comment to explain why KVM_PGTABLE_PROT_W is required would be nice, something like:

/*
* Write permission is required for now even though it's possible to
* map unprotected pages (granules) as read-only. It's impossible to
* map protected pages (granules) as read-only.
*/

Quoted 25 lines
arch/arm64/kvm/mmu.c +25 -0
+
+	ipa = ALIGN_DOWN(ipa, PAGE_SIZE);
+
+	if (!kvm_realm_is_private_address(realm, ipa))
+		return realm_map_non_secure(realm, ipa, pfn, map_size,
+					    memcache);
+
+	return realm_map_protected(realm, ipa, pfn, map_size, memcache);
+}
+
+static int private_memslot_fault(struct kvm_vcpu *vcpu,
+				 phys_addr_t fault_ipa,
+				 struct kvm_memory_slot *memslot)
+{
+	struct kvm *kvm = vcpu->kvm;
+	gpa_t gpa = kvm_gpa_from_fault(kvm, fault_ipa);
+	gfn_t gfn = gpa >> PAGE_SHIFT;
+	bool is_priv_gfn = kvm_mem_is_private(kvm, gfn);
+	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+	struct page *page;
+	kvm_pfn_t pfn;
+	int ret;
+	/*
+	 * For Realms, the shared address is an alias of the private GPA with
+	 * the top bit set. Thus is the fault address matches the GPA then it

arch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) · line 1541

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	 * is the private alias.
+	 */
+	bool is_priv_fault = (gpa == fault_ipa);
+
+	if (is_priv_gfn != is_priv_fault) {
+		kvm_prepare_memory_fault_exit(vcpu,
+					      gpa,
+					      PAGE_SIZE,
+					      kvm_is_write_fault(vcpu),
+					      false, is_priv_fault);

nit:

		kvm_prepare_memory_fault_exit(vcpu, gpa, PAGE_SIZE,
				kvm_is_write_fault(vcpu), false, is_priv_fault);
Quoted 153 lines
arch/arm64/kvm/mmu.c +61 -3
+
+		/*
+		 * KVM_EXIT_MEMORY_FAULT requires an return code of -EFAULT,
+		 * see the API documentation
+		 */
+		return -EFAULT;
+	}
+
+	if (!is_priv_fault) {
+		/* Not a private mapping, handling normally */
+		return -EINVAL;
+	}
+
+	ret = kvm_mmu_topup_memory_cache(memcache,
+					 kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu));
+	if (ret)
+		return ret;
+
+	ret = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfn, &page, NULL);
+	if (ret)
+		return ret;
+
+	/* FIXME: Should be able to use bigger than PAGE_SIZE mappings */
+	ret = realm_map_ipa(kvm, fault_ipa, pfn, PAGE_SIZE, KVM_PGTABLE_PROT_W,
+			    memcache);
+	if (!ret)
+		return 1; /* Handled */
+
+	put_page(page);
+	return ret;
+}
+
  static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  			  struct kvm_s2_trans *nested,
  			  struct kvm_memory_slot *memslot, unsigned long hva,
@@ -1509,6 +1598,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (fault_is_perm)
  		fault_granule = kvm_vcpu_trap_get_perm_fault_granule(vcpu);
  	write_fault = kvm_is_write_fault(vcpu);
+
+	/*
+	 * Realms cannot map protected pages read-only
+	 * FIXME: It should be possible to map unprotected pages read-only
+	 */
+	if (vcpu_is_rec(vcpu))
+		write_fault = true;
+
  	exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu);
  	VM_BUG_ON(write_fault && exec_fault);
 
@@ -1623,7 +1720,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  		ipa &= ~(vma_pagesize - 1);
  	}
 
-	gfn = ipa >> PAGE_SHIFT;
+	gfn = kvm_gpa_from_fault(kvm, ipa) >> PAGE_SHIFT;
  	mte_allowed = kvm_vma_mte_allowed(vma);
 
  	vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
@@ -1756,6 +1853,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  		 */
  		prot &= ~KVM_NV_GUEST_MAP_SZ;
  		ret = KVM_PGT_FN(kvm_pgtable_stage2_relax_perms)(pgt, fault_ipa, prot, flags);
+	} else if (kvm_is_realm(kvm)) {
+		ret = realm_map_ipa(kvm, fault_ipa, pfn, vma_pagesize,
+				    prot, memcache);
  	} else {
  		ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, fault_ipa, vma_pagesize,
  					     __pfn_to_phys(pfn), prot,
@@ -1897,8 +1997,15 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
  		nested = &nested_trans;
  	}
 
-	gfn = ipa >> PAGE_SHIFT;
+	gfn = kvm_gpa_from_fault(vcpu->kvm, ipa) >> PAGE_SHIFT;
  	memslot = gfn_to_memslot(vcpu->kvm, gfn);
+
+	if (kvm_slot_can_be_private(memslot)) {
+		ret = private_memslot_fault(vcpu, ipa, memslot);
+		if (ret != -EINVAL)
+			goto out;
+	}
+
  	hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
  	write_fault = kvm_is_write_fault(vcpu);
  	if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
@@ -1942,7 +2049,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
  		 * of the page size.
  		 */
  		ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0);
-		ret = io_mem_abort(vcpu, ipa);
+		ret = io_mem_abort(vcpu, kvm_gpa_from_fault(vcpu->kvm, ipa));
  		goto out_unlock;
  	}
 
@@ -1990,6 +2097,10 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  	if (!kvm->arch.mmu.pgt)
  		return false;
 
+	/* We don't support aging for Realms */
+	if (kvm_is_realm(kvm))
+		return true;
+
  	return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm->arch.mmu.pgt,
  						   range->start << PAGE_SHIFT,
  						   size, true);
@@ -2006,6 +2117,10 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  	if (!kvm->arch.mmu.pgt)
  		return false;
 
+	/* We don't support aging for Realms */
+	if (kvm_is_realm(kvm))
+		return true;
+
  	return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm->arch.mmu.pgt,
  						   range->start << PAGE_SHIFT,
  						   size, false);
arch/arm64/kvm/rme.c +28 -0
index f6af3ea6ea8a..b6959cd17a6c 100644
@@ -714,6 +714,186 @@ static int realm_create_protected_data_page(struct realm *realm,
  	return -ENXIO;
  }
 
+static int fold_rtt(struct realm *realm, unsigned long addr, int level)
+{
+	phys_addr_t rtt_addr;
+	int ret;
+
+	ret = realm_rtt_fold(realm, addr, level, &rtt_addr);
+	if (ret)
+		return ret;
+
+	free_delegated_granule(rtt_addr);
+
+	return 0;
+}
+
+int realm_map_protected(struct realm *realm,
+			unsigned long ipa,
+			kvm_pfn_t pfn,
+			unsigned long map_size,
+			struct kvm_mmu_memory_cache *memcache)
+{
+	phys_addr_t phys = __pfn_to_phys(pfn);
+	phys_addr_t rd = virt_to_phys(realm->rd);
+	unsigned long base_ipa = ipa;
+	unsigned long size;
+	int map_level;
+	int ret = 0;
+
+	if (WARN_ON(!IS_ALIGNED(map_size, RMM_PAGE_SIZE)))

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 754

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(ipa, map_size)))
+		return -EINVAL;
+
+	if (IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE))
+		map_level = 2;
+	else
+		map_level = 3;
+

This block of code can be compacted a bit:

	int map_level = IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE) ? 2 : 3;

	if (WARN_ON(!IS_ALIGNED(map_size, RMM_PAGE_SIZE) || !IS_ALIGNED(ipa, map_size))
		return -EINVAL;
Quoted 91 lines
arch/arm64/kvm/rme.c +91 -0
+	if (map_level < RMM_RTT_MAX_LEVEL) {
+		/*
+		 * A temporary RTT is needed during the map, precreate it,
+		 * however if there is an error (e.g. missing parent tables)
+		 * this will be handled below.
+		 */
+		realm_create_rtt_levels(realm, ipa, map_level,
+					RMM_RTT_MAX_LEVEL, memcache);
+	}
+
+	for (size = 0; size < map_size; size += RMM_PAGE_SIZE) {
+		if (rmi_granule_delegate(phys)) {
+			/*
+			 * It's likely we raced with another VCPU on the same
+			 * fault. Assume the other VCPU has handled the fault
+			 * and return to the guest.
+			 */
+			return 0;
+		}
+
+		ret = rmi_data_create_unknown(rd, phys, ipa);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RETURN_INDEX(ret);
+
+			WARN_ON(level == RMM_RTT_MAX_LEVEL);
+
+			ret = realm_create_rtt_levels(realm, ipa, level,
+						      RMM_RTT_MAX_LEVEL,
+						      memcache);
+			if (ret)
+				goto err_undelegate;
+
+			ret = rmi_data_create_unknown(rd, phys, ipa);
+		}
+
+		if (WARN_ON(ret))
+			goto err_undelegate;
+
+		phys += RMM_PAGE_SIZE;
+		ipa += RMM_PAGE_SIZE;
+	}
+
+	if (map_size == RMM_L2_BLOCK_SIZE) {
+		ret = fold_rtt(realm, base_ipa, map_level + 1);
+		if (WARN_ON(ret))
+			goto err;
+	}
+
+	return 0;
+
+err_undelegate:
+	if (WARN_ON(rmi_granule_undelegate(phys))) {
+		/* Page can't be returned to NS world so is lost */
+		get_page(phys_to_page(phys));
+	}
+err:
+	while (size > 0) {
+		unsigned long data, top;
+
+		phys -= RMM_PAGE_SIZE;
+		size -= RMM_PAGE_SIZE;
+		ipa -= RMM_PAGE_SIZE;
+
+		WARN_ON(rmi_data_destroy(rd, ipa, &data, &top));
+
+		if (WARN_ON(rmi_granule_undelegate(phys))) {
+			/* Page can't be returned to NS world so is lost */
+			get_page(phys_to_page(phys));
+		}
+	}
+	return -ENXIO;
+}
+
+int realm_map_non_secure(struct realm *realm,
+			 unsigned long ipa,
+			 kvm_pfn_t pfn,
+			 unsigned long size,
+			 struct kvm_mmu_memory_cache *memcache)
+{
+	phys_addr_t rd = virt_to_phys(realm->rd);
+	phys_addr_t phys = __pfn_to_phys(pfn);
+	unsigned long offset;
+	int map_size, map_level;
+	int ret = 0;
+
+	if (WARN_ON(!IS_ALIGNED(size, RMM_PAGE_SIZE)))
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(ipa, size)))

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 855

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+		return -EINVAL;
+
+	if (IS_ALIGNED(size, RMM_L2_BLOCK_SIZE)) {
+		map_level = 2;
+		map_size = RMM_L2_BLOCK_SIZE;
+	} else {
+		map_level = 3;
+		map_size = RMM_PAGE_SIZE;
+	}
+

Similiarly, it can be compacted a bit:

	int map_size = IS_ALIGNED(size, RMM_L2_BLOCK_SIZE) ? RMM_L2_BLOCK_SIZE : RMM_PAGE_SIZE;
	int map_level = IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE) ? 2 : 3;

	if (WARN_ON(!IS_ALIGNED(size, RMM_PAGE_SIZE) || !IS_ALIGNED(ipa, size))
		return -EINVAL;
Quoted 23 lines
arch/arm64/kvm/rme.c +23 -0
+	for (offset = 0; offset < size; offset += map_size) {
+		/*
+		 * realm_map_ipa() enforces that the memory is writable,
+		 * so for now we permit both read and write.
+		 */
+		unsigned long desc = phys |
+				     PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) |
+				     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R |
+				     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
+		ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RETURN_INDEX(ret);
+
+			ret = realm_create_rtt_levels(realm, ipa, level,
+						      map_level, memcache);
+			if (ret)
+				return -ENXIO;
+
+			ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc);
+		}
+		/*

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 888

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+		 * RMI_ERROR_RTT can be reported for two reasons: either the
+		 * RTT tables are not there, or there is an RTTE already
+		 * present for the address.  The call to
+		 * realm_create_rtt_levels() above handles the first case, and
+		 * in the second case this indicates that another thread has
+		 * already populated the RTTE for us, so we can ignore the
+		 * error and continue.
+		 */
+		if (ret && RMI_RETURN_STATUS(ret) != RMI_ERROR_RTT)
+			return -ENXIO;

The comments needs to be aligned in format :)

If RMM returns RMI_ERROR_RTT for third case in the future, the assumption here is broken. I think it's worthy to double confirm by checking the RTT entry through RTT_READ_ENTRY interface. If the mapping doesn't exist, we probably still need to retry.

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 899

Quoted 11 lines
arch/arm64/kvm/rme.c +8 -0
+
+		ipa += map_size;
+		phys += map_size;
+	}
+
+	return 0;
+}
+
  static int populate_region(struct kvm *kvm,
  			   phys_addr_t ipa_base,
  			   phys_addr_t ipa_end,

Thanks,
Gavin

#63 lore raw

Re: [PATCH v8 20/43] arm64: RME: Runtime faulting of memory

Quoted 102 lines · Gavin Shan
Quoted 101 lines · Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

---
Changes since v7:
  * Remove redundant WARN_ONs for realm_create_rtt_levels() - it will internally WARN when necessary.
Changes since v6:
  * Handle PAGE_SIZE being larger than RMM granule size.
  * Some minor renaming following review comments.
Changes since v5:
  * Reduce use of struct page in preparation for supporting the RMM having a different page size to the host.
  * Handle a race when delegating a page where another CPU has faulted on a the same page (and already delegated the physical page) but not yet mapped it. In this case simply return to the guest to either use the mapping from the other CPU (or refault if the race is lost).
  * The changes to populate_par_region() are moved into the previous patch where they belong.
Changes since v4:
  * Code cleanup following review feedback.
  * Drop the PTE_SHARED bit when creating unprotected page table entries. This is now set by the RMM and the host has no control of it and the spec requires the bit to be set to zero.
Changes since v2:
  * Avoid leaking memory if failing to map it in the realm.
  * Correctly mask RTT based on LPA2 flag (see rtt_get_phys()).
  * Adapt to changes in previous patches.
---
  arch/arm64/include/asm/kvm_emulate.h |  10 ++ arch/arm64/include/asm/kvm_rme.h     |  10 ++ arch/arm64/kvm/mmu.c                 | 127 ++++++++++++++++++- arch/arm64/kvm/rme.c                 | 180 +++++++++++++++++++++++++++

  4 files changed, 321 insertions(+), 6 deletions(-)
arch/arm64/ renamed from arch/arm64/include/asm/kvm_emulate.h +0 -0

include/asm/kvm_emulate.h
index c803c8188d9c..def439d6d732 100644

arch/arm64/include/asm/kvm_emulate.h +0 -0
@@ -704,6 +704,16 @@ static inline bool kvm_realm_is_created(struct
 kvm *kvm)

      return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
  }
  +static inline gpa_t kvm_gpa_from_fault(struct kvm *kvm, phys_addr_t ipa)

arch/arm64/include/asm/kvm_emulate.h +9 -0
+{
+    if (kvm_is_realm(kvm)) {
+        struct realm *realm = &kvm->arch.realm;
+
+        return ipa & ~BIT(realm->ia_bits - 1);
+    }
+    return ipa;
+}
+

  static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu) {
      if (static_branch_unlikely(&kvm_rme_is_available))

arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index d86051ef0c5c..47aa6362c6c9 100644

arch/arm64/include/asm/kvm_rme.h +10 -0
@@ -108,6 +108,16 @@ void kvm_realm_unmap_range(struct kvm *kvm,
                  unsigned long ipa,
                  unsigned long size,
                  bool unmap_private);
+int realm_map_protected(struct realm *realm,
+            unsigned long base_ipa,
+            kvm_pfn_t pfn,
+            unsigned long size,
+            struct kvm_mmu_memory_cache *memcache);
+int realm_map_non_secure(struct realm *realm,
+             unsigned long ipa,
+             kvm_pfn_t pfn,
+             unsigned long size,
+             struct kvm_mmu_memory_cache *memcache);
     static inline bool kvm_realm_is_private_address(struct realm *realm,
                           unsigned long addr)
arch/arm64/kvm/mmu.c +0 -0
index 71c04259e39f..02b66ee35426 100644
@@ -338,8 +338,13 @@ static void __unmap_stage2_range(struct

kvm_s2_mmu *mmu, phys_addr_t start, u64 lockdep_assert_held_write(&kvm->mmu_lock); WARN_ON(size & ~PAGE_MASK);
-    WARN_ON(stage2_apply_range(mmu, start, end, KVM_PGT_FN(kvm_pgtable_stage2_unmap),
-                   may_block));
+
+    if (kvm_is_realm(kvm))
+        kvm_realm_unmap_range(kvm, start, size, !only_shared); +    else

Quoted 8 lines
Quoted 5 lines

+        WARN_ON(stage2_apply_range(mmu, start, end, +                       KVM_PGT_FN(kvm_pgtable_stage2_unmap), +                       may_block));
  }

As spotted previsouly, the parameter @may_block isn't handled by kvm_realm_unmap_range().

Ack.

Quoted 35 lines
Quoted 22 lines

  void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,

arch/arm64/kvm/mmu.c +4 -0
@@ -359,7 +364,10 @@ static void stage2_flush_memslot(struct kvm *kvm,
       phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
       phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
   -    kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
+    if (kvm_is_realm(kvm))
+        kvm_realm_unmap_range(kvm, addr, end - addr, false);
+    else
+        kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
   }
     /**
@@ -1053,6 +1061,10 @@ void stage2_unmap_vm(struct kvm *kvm)
       struct kvm_memory_slot *memslot;
       int idx, bkt;

  +    /* For realms this is handled by the RMM so nothing to do here */ +    if (kvm_is_realm(kvm))
+        return;
+
      idx = srcu_read_lock(&kvm->srcu); mmap_read_lock(current->mm); write_lock(&kvm->mmu_lock);

arch/arm64/kvm/mmu.c +0 -0
@@ -1078,6 +1090,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
Quoted 6 lines

      if (kvm_is_realm(kvm) &&
          (kvm_realm_state(kvm) != REALM_STATE_DEAD && kvm_realm_state(kvm) != REALM_STATE_NONE)) {
+        kvm_stage2_unmap_range(mmu, 0, (~0ULL) & PAGE_MASK, false); write_unlock(&kvm->mmu_lock); kvm_realm_destroy_rtts(kvm, pgt->ia_bits);

(~0ULL & PAGE_MASK) wouldn't be a problem since the range will be limited to
[0, BIT(realm->ia_bits) - 1] in kvm_realm_unmap_range(). I think it's reasonable
to pass the maximal size here, something like:

Quoted 1 line

        kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), false);

Yes, that's probably neater. There's a WARN_ON() in __unmap_stage2_range() otherwise I'd have just used a simple ~0ULL.

Quoted 17 lines

arch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) · line 1506

Quoted 13 lines

  @@ -1482,6 +1495,82 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)
      return vma->vm_flags & VM_MTE_ALLOWED;
  }
  +static int realm_map_ipa(struct kvm *kvm, phys_addr_t ipa, +             kvm_pfn_t pfn, unsigned long map_size, +             enum kvm_pgtable_prot prot,
+             struct kvm_mmu_memory_cache *memcache)

arch/arm64/kvm/mmu.c +5 -0
+{
+    struct realm *realm = &kvm->arch.realm;
+
+    if (WARN_ON(!(prot & KVM_PGTABLE_PROT_W)))
+        return -EFAULT;

A comment to explain why KVM_PGTABLE_PROT_W is required would be nice, something like:

Quoted 5 lines

    /*
     * Write permission is required for now even though it's possible to
     * map unprotected pages (granules) as read-only. It's impossible to
     * map protected pages (granules) as read-only.
     */

Ack.

Quoted 40 lines
Quoted 29 lines
arch/arm64/kvm/mmu.c +24 -0
+
+    ipa = ALIGN_DOWN(ipa, PAGE_SIZE);
+
+    if (!kvm_realm_is_private_address(realm, ipa))
+        return realm_map_non_secure(realm, ipa, pfn, map_size,
+                        memcache);
+
+    return realm_map_protected(realm, ipa, pfn, map_size, memcache);
+}
+
+static int private_memslot_fault(struct kvm_vcpu *vcpu,
+                 phys_addr_t fault_ipa,
+                 struct kvm_memory_slot *memslot)
+{
+    struct kvm *kvm = vcpu->kvm;
+    gpa_t gpa = kvm_gpa_from_fault(kvm, fault_ipa);
+    gfn_t gfn = gpa >> PAGE_SHIFT;
+    bool is_priv_gfn = kvm_mem_is_private(kvm, gfn);
+    struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+    struct page *page;
+    kvm_pfn_t pfn;
+    int ret;
+    /*
+     * For Realms, the shared address is an alias of the private GPA

with
+     * the top bit set. Thus is the fault address matches the GPA then it
+     * is the private alias.
+     */

Quoted 8 lines

+    bool is_priv_fault = (gpa == fault_ipa); +
+    if (is_priv_gfn != is_priv_fault) {
+        kvm_prepare_memory_fault_exit(vcpu, +                          gpa,
+                          PAGE_SIZE,
+                          kvm_is_write_fault(vcpu), +                          false, is_priv_fault);

nit:

Quoted 2 lines

        kvm_prepare_memory_fault_exit(vcpu, gpa, PAGE_SIZE, kvm_is_write_fault(vcpu), false, is_priv_fault);

Fair enough.

Quoted 170 lines
Quoted 157 lines
arch/arm64/kvm/mmu.c +24 -0
+
+        /*
+         * KVM_EXIT_MEMORY_FAULT requires an return code of -EFAULT,
+         * see the API documentation
+         */
+        return -EFAULT;
+    }
+
+    if (!is_priv_fault) {
+        /* Not a private mapping, handling normally */
+        return -EINVAL;
+    }
+
+    ret = kvm_mmu_topup_memory_cache(memcache,
+                     kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu));
+    if (ret)
+        return ret;
+
+    ret = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfn, &page, NULL);
+    if (ret)
+        return ret;
+
+    /* FIXME: Should be able to use bigger than PAGE_SIZE mappings */
+    ret = realm_map_ipa(kvm, fault_ipa, pfn, PAGE_SIZE,

KVM_PGTABLE_PROT_W,
+                memcache);
+    if (!ret)
+        return 1; /* Handled */

arch/arm64/kvm/mmu.c +5 -0
+
+    put_page(page);
+    return ret;
+}
+

  static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested,
                struct kvm_memory_slot *memslot, unsigned long hva,

arch/arm64/kvm/mmu.c +8 -0
@@ -1509,6 +1598,14 @@ static int user_mem_abort(struct kvm_vcpu
 *vcpu, phys_addr_t fault_ipa,
       if (fault_is_perm)
           fault_granule = kvm_vcpu_trap_get_perm_fault_granule(vcpu);
       write_fault = kvm_is_write_fault(vcpu);
+
+    /*
+     * Realms cannot map protected pages read-only
+     * FIXME: It should be possible to map unprotected pages read-only
+     */
+    if (vcpu_is_rec(vcpu))
+        write_fault = true;
+
       exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu);
       VM_BUG_ON(write_fault && exec_fault);

  @@ -1623,7 +1720,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
          ipa &= ~(vma_pagesize - 1);
      }
  -    gfn = ipa >> PAGE_SHIFT;
+    gfn = kvm_gpa_from_fault(kvm, ipa) >> PAGE_SHIFT; mte_allowed = kvm_vma_mte_allowed(vma); vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;

arch/arm64/kvm/mmu.c +3 -0
@@ -1756,6 +1853,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu,
 phys_addr_t fault_ipa,
            */
           prot &= ~KVM_NV_GUEST_MAP_SZ;
           ret = KVM_PGT_FN(kvm_pgtable_stage2_relax_perms)(pgt,
 fault_ipa, prot, flags);
+    } else if (kvm_is_realm(kvm)) {
+        ret = realm_map_ipa(kvm, fault_ipa, pfn, vma_pagesize,
+                    prot, memcache);
       } else {

          ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, fault_ipa, vma_pagesize,
                           __pfn_to_phys(pfn), prot,

arch/arm64/kvm/mmu.c +9 -1
@@ -1897,8 +1997,15 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
           nested = &nested_trans;
       }
   -    gfn = ipa >> PAGE_SHIFT;
+    gfn = kvm_gpa_from_fault(vcpu->kvm, ipa) >> PAGE_SHIFT;
       memslot = gfn_to_memslot(vcpu->kvm, gfn);
+
+    if (kvm_slot_can_be_private(memslot)) {
+        ret = private_memslot_fault(vcpu, ipa, memslot);
+        if (ret != -EINVAL)
+            goto out;
+    }
+
       hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
       write_fault = kvm_is_write_fault(vcpu);
       if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
@@ -1942,7 +2049,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
            * of the page size.
            */
           ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0);
-        ret = io_mem_abort(vcpu, ipa);
+        ret = io_mem_abort(vcpu, kvm_gpa_from_fault(vcpu->kvm, ipa));
           goto out_unlock;
       }

  @@ -1990,6 +2097,10 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
      if (!kvm->arch.mmu.pgt)
          return false;
  +    /* We don't support aging for Realms */
+    if (kvm_is_realm(kvm))
+        return true;
+
      return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm-

Quoted 1 line

arch.mmu.pgt,

                             range->start << PAGE_SHIFT, size, true);

arch/arm64/kvm/mmu.c +0 -0
@@ -2006,6 +2117,10 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct
 kvm_gfn_range *range)
       if (!kvm->arch.mmu.pgt)
           return false;

  +    /* We don't support aging for Realms */
+    if (kvm_is_realm(kvm))
+        return true;
+
      return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm-

Quoted 1 line

arch.mmu.pgt,

                             range->start << PAGE_SHIFT, size, false);

arch/arm64/kvm/rme.c +0 -0
index f6af3ea6ea8a..b6959cd17a6c 100644
@@ -714,6 +714,186 @@ static int
 realm_create_protected_data_page(struct realm *realm,
       return -ENXIO;
   }

  +static int fold_rtt(struct realm *realm, unsigned long addr, int level)

arch/arm64/kvm/rme.c +27 -0
+{
+    phys_addr_t rtt_addr;
+    int ret;
+
+    ret = realm_rtt_fold(realm, addr, level, &rtt_addr);
+    if (ret)
+        return ret;
+
+    free_delegated_granule(rtt_addr);
+
+    return 0;
+}
+
+int realm_map_protected(struct realm *realm,
+            unsigned long ipa,
+            kvm_pfn_t pfn,
+            unsigned long map_size,
+            struct kvm_mmu_memory_cache *memcache)
+{
+    phys_addr_t phys = __pfn_to_phys(pfn);
+    phys_addr_t rd = virt_to_phys(realm->rd);
+    unsigned long base_ipa = ipa;
+    unsigned long size;
+    int map_level;
+    int ret = 0;
+
+    if (WARN_ON(!IS_ALIGNED(map_size, RMM_PAGE_SIZE)))

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 754

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+        return -EINVAL;
+
+    if (WARN_ON(!IS_ALIGNED(ipa, map_size)))
+        return -EINVAL;
+
+    if (IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE))
+        map_level = 2;
+    else
+        map_level = 3;
+

This block of code can be compacted a bit:

Quoted 5 lines

    int map_level = IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE) ? 2 : 3;

    if (WARN_ON(!IS_ALIGNED(map_size, RMM_PAGE_SIZE) || !IS_ALIGNED(ipa, map_size))
        return -EINVAL;

Ack

Quoted 104 lines
Quoted 91 lines

+    if (map_level < RMM_RTT_MAX_LEVEL) {
+        /*
+         * A temporary RTT is needed during the map, precreate it, +         * however if there is an error (e.g. missing parent tables) +         * this will be handled below.
+         */
+        realm_create_rtt_levels(realm, ipa, map_level,
+                    RMM_RTT_MAX_LEVEL, memcache);
+    }

arch/arm64/kvm/rme.c +82 -0
+
+    for (size = 0; size < map_size; size += RMM_PAGE_SIZE) {
+        if (rmi_granule_delegate(phys)) {
+            /*
+             * It's likely we raced with another VCPU on the same
+             * fault. Assume the other VCPU has handled the fault
+             * and return to the guest.
+             */
+            return 0;
+        }
+
+        ret = rmi_data_create_unknown(rd, phys, ipa);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            /* Create missing RTTs and retry */
+            int level = RMI_RETURN_INDEX(ret);
+
+            WARN_ON(level == RMM_RTT_MAX_LEVEL);
+
+            ret = realm_create_rtt_levels(realm, ipa, level,
+                              RMM_RTT_MAX_LEVEL,
+                              memcache);
+            if (ret)
+                goto err_undelegate;
+
+            ret = rmi_data_create_unknown(rd, phys, ipa);
+        }
+
+        if (WARN_ON(ret))
+            goto err_undelegate;
+
+        phys += RMM_PAGE_SIZE;
+        ipa += RMM_PAGE_SIZE;
+    }
+
+    if (map_size == RMM_L2_BLOCK_SIZE) {
+        ret = fold_rtt(realm, base_ipa, map_level + 1);
+        if (WARN_ON(ret))
+            goto err;
+    }
+
+    return 0;
+
+err_undelegate:
+    if (WARN_ON(rmi_granule_undelegate(phys))) {
+        /* Page can't be returned to NS world so is lost */
+        get_page(phys_to_page(phys));
+    }
+err:
+    while (size > 0) {
+        unsigned long data, top;
+
+        phys -= RMM_PAGE_SIZE;
+        size -= RMM_PAGE_SIZE;
+        ipa -= RMM_PAGE_SIZE;
+
+        WARN_ON(rmi_data_destroy(rd, ipa, &data, &top));
+
+        if (WARN_ON(rmi_granule_undelegate(phys))) {
+            /* Page can't be returned to NS world so is lost */
+            get_page(phys_to_page(phys));
+        }
+    }
+    return -ENXIO;
+}
+
+int realm_map_non_secure(struct realm *realm,
+             unsigned long ipa,
+             kvm_pfn_t pfn,
+             unsigned long size,
+             struct kvm_mmu_memory_cache *memcache)
+{
+    phys_addr_t rd = virt_to_phys(realm->rd);
+    phys_addr_t phys = __pfn_to_phys(pfn);
+    unsigned long offset;
+    int map_size, map_level;
+    int ret = 0;
+
+    if (WARN_ON(!IS_ALIGNED(size, RMM_PAGE_SIZE)))
+        return -EINVAL;
+
+    if (WARN_ON(!IS_ALIGNED(ipa, size)))

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 855

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+        return -EINVAL;
+
+    if (IS_ALIGNED(size, RMM_L2_BLOCK_SIZE)) {
+        map_level = 2;
+        map_size = RMM_L2_BLOCK_SIZE;
+    } else {
+        map_level = 3;
+        map_size = RMM_PAGE_SIZE;
+    }
+

Similiarly, it can be compacted a bit:

Quoted 6 lines

    int map_size = IS_ALIGNED(size, RMM_L2_BLOCK_SIZE) ? RMM_L2_BLOCK_SIZE : RMM_PAGE_SIZE;
    int map_level = IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE) ? 2 : 3;

    if (WARN_ON(!IS_ALIGNED(size, RMM_PAGE_SIZE) || !IS_ALIGNED(ipa, size)) return -EINVAL;

I agree combining the WARN_ON()s makes sense. But the repeated ternary operator just hides the map_level/map_size connection (and I'm not a big fan of the ternary operator).

However, we do have rme_rtt_level_mapsize() which I can use to convert the level to the size and avoid the second ternary.

Quoted 36 lines
Quoted 23 lines

+    for (offset = 0; offset < size; offset += map_size) { +        /*
+         * realm_map_ipa() enforces that the memory is writable, +         * so for now we permit both read and write.
+         */
+        unsigned long desc = phys |
+                     PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) | +                     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | +                     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
+        ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc);

arch/arm64/kvm/rme.c +13 -0
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            /* Create missing RTTs and retry */
+            int level = RMI_RETURN_INDEX(ret);
+
+            ret = realm_create_rtt_levels(realm, ipa, level,
+                              map_level, memcache);
+            if (ret)
+                return -ENXIO;
+
+            ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc);
+        }
+        /*

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 888

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+         * RMI_ERROR_RTT can be reported for two reasons: either the
+         * RTT tables are not there, or there is an RTTE already
+         * present for the address.  The call to
+         * realm_create_rtt_levels() above handles the first case, and
+         * in the second case this indicates that another thread has
+         * already populated the RTTE for us, so we can ignore the
+         * error and continue.
+         */
+        if (ret && RMI_RETURN_STATUS(ret) != RMI_ERROR_RTT)
+            return -ENXIO;

The comments needs to be aligned in format :)

Quoted 5 lines

If RMM returns RMI_ERROR_RTT for third case in the future, the assumption here
is broken. I think it's worthy to double confirm by checking the RTT entry through RTT_READ_ENTRY interface. If the mapping doesn't exist, we probably still need to retry.

The spec is clear on what the RMM is permitted to return error codes, so we're safe for any conforming implementation. And (hopefully) if new error conditions are added we can ensure that they can be distinguished from the existing conditions. Clearly we'd have to update the code to support new spec features.

There's a cost with doing these hypercalls. I expect there will be a strong desire in the near future for optimisations to reduce the number of calls and we'll see spec changes to enable it. So I've tried to avoid "doubling checking".

And if the mapping really doesn't exist then we'd simply expect another fault from the guest when it retries the operation that caused the fault in the first place. So the retry logic is there (albeit in a very expensive form).

Of course if you've spotted a condition here that I haven't thought of then please do let me know - these paths are quite tricky to reason about and very difficult to get meaningful testing on.

Thanks,
Steve

Quoted 15 lines
Quoted 11 lines
arch/arm64/kvm/rme.c +8 -0
+
+        ipa += map_size;
+        phys += map_size;
+    }
+
+    return 0;
+}
+

  static int populate_region(struct kvm *kvm,
                 phys_addr_t ipa_base,
                 phys_addr_t ipa_end,

Thanks,
Gavin

#64 lore raw

Re: [PATCH v8 20/43] arm64: RME: Runtime faulting of memory

Quoted 112 lines · Steven Price
Quoted 102 lines · Gavin Shan
Quoted 101 lines · Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

---
Changes since v7:
  * Remove redundant WARN_ONs for realm_create_rtt_levels() - it will internally WARN when necessary.
Changes since v6:
  * Handle PAGE_SIZE being larger than RMM granule size.
  * Some minor renaming following review comments.
Changes since v5:
  * Reduce use of struct page in preparation for supporting the RMM having a different page size to the host.
  * Handle a race when delegating a page where another CPU has faulted on a the same page (and already delegated the physical page) but not yet mapped it. In this case simply return to the guest to either use the mapping from the other CPU (or refault if the race is lost).
  * The changes to populate_par_region() are moved into the previous patch where they belong.
Changes since v4:
  * Code cleanup following review feedback.
  * Drop the PTE_SHARED bit when creating unprotected page table entries. This is now set by the RMM and the host has no control of it and the spec requires the bit to be set to zero.
Changes since v2:
  * Avoid leaking memory if failing to map it in the realm.
  * Correctly mask RTT based on LPA2 flag (see rtt_get_phys()).
  * Adapt to changes in previous patches.

---
   arch/arm64/include/asm/kvm_emulate.h |  10 ++
   arch/arm64/include/asm/kvm_rme.h     |  10 ++
   arch/arm64/kvm/mmu.c                 | 127 ++++++++++++++++++-
   arch/arm64/kvm/rme.c                 | 180 +++++++++++++++++++++++++++
   4 files changed, 321 insertions(+), 6 deletions(-)
arch/arm64/ renamed from arch/arm64/include/asm/kvm_emulate.h +0 -0

include/asm/kvm_emulate.h
index c803c8188d9c..def439d6d732 100644

arch/arm64/include/asm/kvm_emulate.h +9 -0
@@ -704,6 +704,16 @@ static inline bool kvm_realm_is_created(struct
 kvm *kvm)
       return kvm_is_realm(kvm) && kvm_realm_state(kvm) !=
 REALM_STATE_NONE;
   }
   +static inline gpa_t kvm_gpa_from_fault(struct kvm *kvm, phys_addr_t
 ipa)
+{
+    if (kvm_is_realm(kvm)) {
+        struct realm *realm = &kvm->arch.realm;
+
+        return ipa & ~BIT(realm->ia_bits - 1);
+    }
+    return ipa;
+}
+
   static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu)
   {
       if (static_branch_unlikely(&kvm_rme_is_available))
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index d86051ef0c5c..47aa6362c6c9 100644

arch/arm64/include/asm/kvm_rme.h +10 -0
@@ -108,6 +108,16 @@ void kvm_realm_unmap_range(struct kvm *kvm,
                  unsigned long ipa,
                  unsigned long size,
                  bool unmap_private);
+int realm_map_protected(struct realm *realm,
+            unsigned long base_ipa,
+            kvm_pfn_t pfn,
+            unsigned long size,
+            struct kvm_mmu_memory_cache *memcache);
+int realm_map_non_secure(struct realm *realm,
+             unsigned long ipa,
+             kvm_pfn_t pfn,
+             unsigned long size,
+             struct kvm_mmu_memory_cache *memcache);
     static inline bool kvm_realm_is_private_address(struct realm *realm,
                           unsigned long addr)
arch/arm64/kvm/mmu.c +0 -0
index 71c04259e39f..02b66ee35426 100644
@@ -338,8 +338,13 @@ static void __unmap_stage2_range(struct

kvm_s2_mmu *mmu, phys_addr_t start, u64
        lockdep_assert_held_write(&kvm->mmu_lock); WARN_ON(size & ~PAGE_MASK);
-    WARN_ON(stage2_apply_range(mmu, start, end, KVM_PGT_FN(kvm_pgtable_stage2_unmap),
-                   may_block));
+
+    if (kvm_is_realm(kvm))
+        kvm_realm_unmap_range(kvm, start, size, !only_shared); +    else

Quoted 8 lines
Quoted 5 lines

+        WARN_ON(stage2_apply_range(mmu, start, end, +                       KVM_PGT_FN(kvm_pgtable_stage2_unmap), +                       may_block));
  }

As spotted previsouly, the parameter @may_block isn't handled by kvm_realm_unmap_range().

Quoted 38 lines

Ack.

Quoted 36 lines
Quoted 22 lines

  void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,

arch/arm64/kvm/mmu.c +7 -0
@@ -359,7 +364,10 @@ static void stage2_flush_memslot(struct kvm *kvm,
       phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
       phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
   -    kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
+    if (kvm_is_realm(kvm))
+        kvm_realm_unmap_range(kvm, addr, end - addr, false);
+    else
+        kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
   }
     /**
@@ -1053,6 +1061,10 @@ void stage2_unmap_vm(struct kvm *kvm)
       struct kvm_memory_slot *memslot;
       int idx, bkt;
   +    /* For realms this is handled by the RMM so nothing to do here */
+    if (kvm_is_realm(kvm))
+        return;
+
       idx = srcu_read_lock(&kvm->srcu);
       mmap_read_lock(current->mm);
       write_lock(&kvm->mmu_lock);

arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1095

Quoted 6 lines
arch/arm64/kvm/mmu.c +1 -0
@@ -1078,6 +1090,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
       if (kvm_is_realm(kvm) &&
           (kvm_realm_state(kvm) != REALM_STATE_DEAD &&
            kvm_realm_state(kvm) != REALM_STATE_NONE)) {
+        kvm_stage2_unmap_range(mmu, 0, (~0ULL) & PAGE_MASK, false);
           write_unlock(&kvm->mmu_lock);
           kvm_realm_destroy_rtts(kvm, pgt->ia_bits);

(~0ULL & PAGE_MASK) wouldn't be a problem since the range will be limited to
[0, BIT(realm->ia_bits) - 1] in kvm_realm_unmap_range(). I think it's reasonable
to pass the maximal size here, something like:

        kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), false);

I think this must be, given the end is excluding:

	   kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits), false);

BIT(realm->ia_bits - 1) only covers the protected half. The unprotected half spans [ BIT(realm->ia_bits - 1), BIT(realm->ia_bits))

Suzuki

#65 lore raw

Re: [PATCH v8 20/43] arm64: RME: Runtime faulting of memory

Quoted 159 lines · Suzuki K Poulose
Quoted 158 lines · Steven Price
Quoted 109 lines · Gavin Shan
Quoted 108 lines · Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

---
Changes since v7:
   * Remove redundant WARN_ONs for realm_create_rtt_levels() - it will internally WARN when necessary.
Changes since v6:
   * Handle PAGE_SIZE being larger than RMM granule size.
   * Some minor renaming following review comments.
Changes since v5:
   * Reduce use of struct page in preparation for supporting the RMM having a different page size to the host.
   * Handle a race when delegating a page where another CPU has faulted on
     a the same page (and already delegated the physical page) but not yet
     mapped it. In this case simply return to the guest to either use the
     mapping from the other CPU (or refault if the race is lost).
   * The changes to populate_par_region() are moved into the previous patch where they belong.
Changes since v4:
   * Code cleanup following review feedback.
   * Drop the PTE_SHARED bit when creating unprotected page table entries.
     This is now set by the RMM and the host has no control of it and the
     spec requires the bit to be set to zero.
Changes since v2:
   * Avoid leaking memory if failing to map it in the realm.
   * Correctly mask RTT based on LPA2 flag (see rtt_get_phys()).
   * Adapt to changes in previous patches.
---
   arch/arm64/include/asm/kvm_emulate.h |  10 ++ arch/arm64/include/asm/kvm_rme.h     |  10 ++ arch/arm64/kvm/mmu.c                 | 127 ++++++++++++++++++- arch/arm64/kvm/rme.c                 | 180 ++++++++++++++++++++++
+++++

   4 files changed, 321 insertions(+), 6 deletions(-)
arch/arm64/ renamed from arch/arm64/include/asm/kvm_emulate.h +0 -0

include/asm/kvm_emulate.h
index c803c8188d9c..def439d6d732 100644

arch/arm64/include/asm/kvm_emulate.h +0 -0
@@ -704,6 +704,16 @@ static inline bool kvm_realm_is_created(struct
 kvm *kvm)

       return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
   }
   +static inline gpa_t kvm_gpa_from_fault(struct kvm *kvm, phys_addr_t ipa)

arch/arm64/include/asm/kvm_emulate.h +9 -0
+{
+    if (kvm_is_realm(kvm)) {
+        struct realm *realm = &kvm->arch.realm;
+
+        return ipa & ~BIT(realm->ia_bits - 1);
+    }
+    return ipa;
+}
+

   static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu) {
       if (static_branch_unlikely(&kvm_rme_is_available))

arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rme.h +0 -0

asm/kvm_rme.h
index d86051ef0c5c..47aa6362c6c9 100644

arch/arm64/include/asm/kvm_rme.h +10 -0
@@ -108,6 +108,16 @@ void kvm_realm_unmap_range(struct kvm *kvm,
                   unsigned long ipa,
                   unsigned long size,
                   bool unmap_private);
+int realm_map_protected(struct realm *realm,
+            unsigned long base_ipa,
+            kvm_pfn_t pfn,
+            unsigned long size,
+            struct kvm_mmu_memory_cache *memcache);
+int realm_map_non_secure(struct realm *realm,
+             unsigned long ipa,
+             kvm_pfn_t pfn,
+             unsigned long size,
+             struct kvm_mmu_memory_cache *memcache);

     static inline bool kvm_realm_is_private_address(struct realm *realm,
                           unsigned long addr)

arch/arm64/kvm/mmu.c +0 -0
index 71c04259e39f..02b66ee35426 100644
@@ -338,8 +338,13 @@ static void __unmap_stage2_range(struct

kvm_s2_mmu *mmu, phys_addr_t start, u64 lockdep_assert_held_write(&kvm->mmu_lock); WARN_ON(size & ~PAGE_MASK);
-    WARN_ON(stage2_apply_range(mmu, start, end, KVM_PGT_FN(kvm_pgtable_stage2_unmap),
-                   may_block));
+
+    if (kvm_is_realm(kvm))
+        kvm_realm_unmap_range(kvm, start, size, !only_shared); +    else

Quoted 8 lines
Quoted 5 lines

+        WARN_ON(stage2_apply_range(mmu, start, end, +                       KVM_PGT_FN(kvm_pgtable_stage2_unmap), +                       may_block));
   }

As spotted previsouly, the parameter @may_block isn't handled by kvm_realm_unmap_range().

Ack.

Quoted 37 lines
Quoted 24 lines

   void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,

arch/arm64/kvm/mmu.c +4 -0
@@ -359,7 +364,10 @@ static void stage2_flush_memslot(struct kvm *kvm,
        phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
        phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
    -    kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
+    if (kvm_is_realm(kvm))
+        kvm_realm_unmap_range(kvm, addr, end - addr, false);
+    else
+        kvm_stage2_flush_range(&kvm->arch.mmu, addr, end);
    }
      /**
@@ -1053,6 +1061,10 @@ void stage2_unmap_vm(struct kvm *kvm)
        struct kvm_memory_slot *memslot;
        int idx, bkt;

   +    /* For realms this is handled by the RMM so nothing to do here */
+    if (kvm_is_realm(kvm))
+        return;
+
       idx = srcu_read_lock(&kvm->srcu); mmap_read_lock(current->mm); write_lock(&kvm->mmu_lock);

arch/arm64/kvm/mmu.c +0 -0
@@ -1078,6 +1090,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
Quoted 6 lines

       if (kvm_is_realm(kvm) &&
           (kvm_realm_state(kvm) != REALM_STATE_DEAD && kvm_realm_state(kvm) != REALM_STATE_NONE)) { +        kvm_stage2_unmap_range(mmu, 0, (~0ULL) & PAGE_MASK, false); write_unlock(&kvm->mmu_lock); kvm_realm_destroy_rtts(kvm, pgt->ia_bits);

(~0ULL & PAGE_MASK) wouldn't be a problem since the range will be limited to
[0, BIT(realm->ia_bits) - 1] in kvm_realm_unmap_range(). I think it's reasonable
to pass the maximal size here, something like:

Quoted 7 lines
Quoted 1 line
Quoted 1 line

         kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), false);

I think this must be, given the end is excluding:
       kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits), false);

BIT(realm->ia_bits - 1) only covers the protected half. The unprotected half spans  [ BIT(realm->ia_bits - 1), BIT(realm->ia_bits))

The kernel treats the two halves as aliasing. kvm_realm_unmap_range() caps the end to min(BIT(realm->ia_bits - 1), end). So this wouldn't make any difference.

This an unfortunate outcome of the memory slots describing both the protected region (via guestmem_fd) and the shared region (via VMM maps). So a single memslot describes two regions which leads to the kernel treating those regions as aliasing for some purposes.

Thanks,
Steve

#66 lore raw

Re: [PATCH v8 20/43] arm64: RME: Runtime faulting of memory

In reply to Steven Price

Hi Steven

Quoted 9 lines · Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

Please find some comments below.

...

Quoted 33 lines
arch/arm64/kvm/rme.c +25 -0
index f6af3ea6ea8a..b6959cd17a6c 100644
@@ -714,6 +714,186 @@ static int realm_create_protected_data_page(struct realm *realm,
  	return -ENXIO;
  }
 
+static int fold_rtt(struct realm *realm, unsigned long addr, int level)
+{
+	phys_addr_t rtt_addr;
+	int ret;
+
+	ret = realm_rtt_fold(realm, addr, level, &rtt_addr);
+	if (ret)
+		return ret;
+
+	free_delegated_granule(rtt_addr);
+
+	return 0;
+}
+
+int realm_map_protected(struct realm *realm,
+			unsigned long ipa,
+			kvm_pfn_t pfn,
+			unsigned long map_size,
+			struct kvm_mmu_memory_cache *memcache)
+{
+	phys_addr_t phys = __pfn_to_phys(pfn);
+	phys_addr_t rd = virt_to_phys(realm->rd);
+	unsigned long base_ipa = ipa;
+	unsigned long size;
+	int map_level;

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 751

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+	int ret = 0;
+
+	if (WARN_ON(!IS_ALIGNED(map_size, RMM_PAGE_SIZE)))
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(ipa, map_size)))
+		return -EINVAL;
+
+	if (IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE))
+		map_level = 2;

minor nit : RMM_RTT_BLOCK_LEVEL

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 753

Quoted 2 lines
arch/arm64/kvm/rme.c +2 -0
+	else
+		map_level = 3;

minor nit: RMM_RTT_MAX_LEVEL ?

Quoted 87 lines
arch/arm64/kvm/rme.c +87 -0
+
+	if (map_level < RMM_RTT_MAX_LEVEL) {
+		/*
+		 * A temporary RTT is needed during the map, precreate it,
+		 * however if there is an error (e.g. missing parent tables)
+		 * this will be handled below.
+		 */
+		realm_create_rtt_levels(realm, ipa, map_level,
+					RMM_RTT_MAX_LEVEL, memcache);
+	}
+
+	for (size = 0; size < map_size; size += RMM_PAGE_SIZE) {
+		if (rmi_granule_delegate(phys)) {
+			/*
+			 * It's likely we raced with another VCPU on the same
+			 * fault. Assume the other VCPU has handled the fault
+			 * and return to the guest.
+			 */
+			return 0;
+		}
+
+		ret = rmi_data_create_unknown(rd, phys, ipa);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RETURN_INDEX(ret);
+
+			WARN_ON(level == RMM_RTT_MAX_LEVEL);
+
+			ret = realm_create_rtt_levels(realm, ipa, level,
+						      RMM_RTT_MAX_LEVEL,
+						      memcache);
+			if (ret)
+				goto err_undelegate;
+
+			ret = rmi_data_create_unknown(rd, phys, ipa);
+		}
+
+		if (WARN_ON(ret))
+			goto err_undelegate;
+
+		phys += RMM_PAGE_SIZE;
+		ipa += RMM_PAGE_SIZE;
+	}
+
+	if (map_size == RMM_L2_BLOCK_SIZE) {
+		ret = fold_rtt(realm, base_ipa, map_level + 1);
+		if (WARN_ON(ret))
+			goto err;
+	}
+
+	return 0;
+
+err_undelegate:
+	if (WARN_ON(rmi_granule_undelegate(phys))) {
+		/* Page can't be returned to NS world so is lost */
+		get_page(phys_to_page(phys));
+	}
+err:
+	while (size > 0) {
+		unsigned long data, top;
+
+		phys -= RMM_PAGE_SIZE;
+		size -= RMM_PAGE_SIZE;
+		ipa -= RMM_PAGE_SIZE;
+
+		WARN_ON(rmi_data_destroy(rd, ipa, &data, &top));
+
+		if (WARN_ON(rmi_granule_undelegate(phys))) {
+			/* Page can't be returned to NS world so is lost */
+			get_page(phys_to_page(phys));
+		}
+	}
+	return -ENXIO;
+}
+
+int realm_map_non_secure(struct realm *realm,
+			 unsigned long ipa,
+			 kvm_pfn_t pfn,
+			 unsigned long size,
+			 struct kvm_mmu_memory_cache *memcache)
+{
+	phys_addr_t rd = virt_to_phys(realm->rd);
+	phys_addr_t phys = __pfn_to_phys(pfn);
+	unsigned long offset;
+	int map_size, map_level;
+	int ret = 0;

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 850

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+
+	if (WARN_ON(!IS_ALIGNED(size, RMM_PAGE_SIZE)))
+		return -EINVAL;
+
+	if (WARN_ON(!IS_ALIGNED(ipa, size)))
+		return -EINVAL;
+
+	if (IS_ALIGNED(size, RMM_L2_BLOCK_SIZE)) {
+		map_level = 2;
+		map_size = RMM_L2_BLOCK_SIZE;

Same here, stick to the symbols than digits.

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 858

Quoted 8 lines
arch/arm64/kvm/rme.c +8 -0
+	} else {
+		map_level = 3;
+		map_size = RMM_PAGE_SIZE;
+	}
+
+	for (offset = 0; offset < size; offset += map_size) {
+		/*
+		 * realm_map_ipa() enforces that the memory is writable,

The function names seems to be obsolete, please fix.

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 867

Quoted 9 lines
arch/arm64/kvm/rme.c +9 -0
+		 * so for now we permit both read and write.
+		 */
+		unsigned long desc = phys |
+				     PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) |
+				     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R |
+				     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
+		ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc);
+
+		if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {

Could we hit the following case and end up in failure ?

* Initially a single page is shared and the S2 is mapped
* Later the Realm shares the entire L2 block and encounters a fault at a new IPA within the L2 block.

In this case, we may try to L2 mapping when there is a L3 mapping and we could encounter (RMI_ERROR_RTT, 2).

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 869

Quoted 2 lines
arch/arm64/kvm/rme.c +2 -0
+			/* Create missing RTTs and retry */
+			int level = RMI_RETURN_INDEX(ret);

So we should probably go down the rtt create step, with the following check.

			if (level < map_level) {

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 876

Quoted 7 lines
arch/arm64/kvm/rme.c +7 -0
+
+			ret = realm_create_rtt_levels(realm, ipa, level,
+						      map_level, memcache);
+			if (ret)
+				return -ENXIO;
+
+			ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc);
		} else {

Otherwise, may be we need to do some more hard work to fix it up.

1. If map_level == 3, something is terribly wrong or we raced with another thread ?

2. If map_level < 3 and we didn't race :

a. Going one level down and creating the mappings there and then
folding. But we could endup dealing with ERROR_RTT,3 as in (1).

b. Easiest is to destroy the table at "map_level + 1" and retry the map.

Suzuki

Quoted 23 lines
arch/arm64/kvm/rme.c +20 -0
+		}
+		/*
+		 * RMI_ERROR_RTT can be reported for two reasons: either the
+		 * RTT tables are not there, or there is an RTTE already
+		 * present for the address.  The call to
+		 * realm_create_rtt_levels() above handles the first case, and
+		 * in the second case this indicates that another thread has
+		 * already populated the RTTE for us, so we can ignore the
+		 * error and continue.
+		 */
+		if (ret && RMI_RETURN_STATUS(ret) != RMI_ERROR_RTT)
+			return -ENXIO;
+
+		ipa += map_size;
+		phys += map_size;
+	}
+
+	return 0;
+}
+
  static int populate_region(struct kvm *kvm,
  			   phys_addr_t ipa_base,
  			   phys_addr_t ipa_end,
#67 lore raw

Re: [PATCH v8 20/43] arm64: RME: Runtime faulting of memory

Quoted 22 lines · Suzuki K Poulose

Hi Steven

Quoted 10 lines · Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

Quoted 8 lines

+    } else {
+        map_level = 3;
+        map_size = RMM_PAGE_SIZE;
+    }
+
+    for (offset = 0; offset < size; offset += map_size) { +        /*
+         * realm_map_ipa() enforces that the memory is writable,

Quoted 1 line

The function names seems to be obsolete, please fix.

Doh, it is in arch/arm64/kvm/mmu.c, please ignore this comment.

Suzuki

#68 lore raw

Re: [PATCH v8 20/43] arm64: RME: Runtime faulting of memory

In reply to Suzuki K Poulose
Quoted 64 lines · Suzuki K Poulose

Hi Steven

Quoted 9 lines · Steven Price

At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest.

For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure.

Please find some comments below.

...

Quoted 34 lines
arch/arm64/kvm/rme.c +0 -0
index f6af3ea6ea8a..b6959cd17a6c 100644
@@ -714,6 +714,186 @@ static int
 realm_create_protected_data_page(struct realm *realm,
       return -ENXIO;
   }

  +static int fold_rtt(struct realm *realm, unsigned long addr, int level)

arch/arm64/kvm/rme.c +24 -0
+{
+    phys_addr_t rtt_addr;
+    int ret;
+
+    ret = realm_rtt_fold(realm, addr, level, &rtt_addr);
+    if (ret)
+        return ret;
+
+    free_delegated_granule(rtt_addr);
+
+    return 0;
+}
+
+int realm_map_protected(struct realm *realm,
+            unsigned long ipa,
+            kvm_pfn_t pfn,
+            unsigned long map_size,
+            struct kvm_mmu_memory_cache *memcache)
+{
+    phys_addr_t phys = __pfn_to_phys(pfn);
+    phys_addr_t rd = virt_to_phys(realm->rd);
+    unsigned long base_ipa = ipa;
+    unsigned long size;
+    int map_level;

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 751

Quoted 10 lines
arch/arm64/kvm/rme.c +10 -0
+    int ret = 0;
+
+    if (WARN_ON(!IS_ALIGNED(map_size, RMM_PAGE_SIZE)))
+        return -EINVAL;
+
+    if (WARN_ON(!IS_ALIGNED(ipa, map_size)))
+        return -EINVAL;
+
+    if (IS_ALIGNED(map_size, RMM_L2_BLOCK_SIZE))
+        map_level = 2;

minor nit : RMM_RTT_BLOCK_LEVEL

Quoted 4 lines
Quoted 2 lines

+    else
+        map_level = 3;

minor nit:  RMM_RTT_MAX_LEVEL ?

Ack

Quoted 91 lines
Quoted 91 lines
arch/arm64/kvm/rme.c +91 -0
+
+    if (map_level < RMM_RTT_MAX_LEVEL) {
+        /*
+         * A temporary RTT is needed during the map, precreate it,
+         * however if there is an error (e.g. missing parent tables)
+         * this will be handled below.
+         */
+        realm_create_rtt_levels(realm, ipa, map_level,
+                    RMM_RTT_MAX_LEVEL, memcache);
+    }
+
+    for (size = 0; size < map_size; size += RMM_PAGE_SIZE) {
+        if (rmi_granule_delegate(phys)) {
+            /*
+             * It's likely we raced with another VCPU on the same
+             * fault. Assume the other VCPU has handled the fault
+             * and return to the guest.
+             */
+            return 0;
+        }
+
+        ret = rmi_data_create_unknown(rd, phys, ipa);
+
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {
+            /* Create missing RTTs and retry */
+            int level = RMI_RETURN_INDEX(ret);
+
+            WARN_ON(level == RMM_RTT_MAX_LEVEL);
+
+            ret = realm_create_rtt_levels(realm, ipa, level,
+                              RMM_RTT_MAX_LEVEL,
+                              memcache);
+            if (ret)
+                goto err_undelegate;
+
+            ret = rmi_data_create_unknown(rd, phys, ipa);
+        }
+
+        if (WARN_ON(ret))
+            goto err_undelegate;
+
+        phys += RMM_PAGE_SIZE;
+        ipa += RMM_PAGE_SIZE;
+    }
+
+    if (map_size == RMM_L2_BLOCK_SIZE) {
+        ret = fold_rtt(realm, base_ipa, map_level + 1);
+        if (WARN_ON(ret))
+            goto err;
+    }
+
+    return 0;
+
+err_undelegate:
+    if (WARN_ON(rmi_granule_undelegate(phys))) {
+        /* Page can't be returned to NS world so is lost */
+        get_page(phys_to_page(phys));
+    }
+err:
+    while (size > 0) {
+        unsigned long data, top;
+
+        phys -= RMM_PAGE_SIZE;
+        size -= RMM_PAGE_SIZE;
+        ipa -= RMM_PAGE_SIZE;
+
+        WARN_ON(rmi_data_destroy(rd, ipa, &data, &top));
+
+        if (WARN_ON(rmi_granule_undelegate(phys))) {
+            /* Page can't be returned to NS world so is lost */
+            get_page(phys_to_page(phys));
+        }
+    }
+    return -ENXIO;
+}
+
+int realm_map_non_secure(struct realm *realm,
+             unsigned long ipa,
+             kvm_pfn_t pfn,
+             unsigned long size,
+             struct kvm_mmu_memory_cache *memcache)
+{
+    phys_addr_t rd = virt_to_phys(realm->rd);
+    phys_addr_t phys = __pfn_to_phys(pfn);
+    unsigned long offset;
+    int map_size, map_level;
+    int ret = 0;
+
+    if (WARN_ON(!IS_ALIGNED(size, RMM_PAGE_SIZE)))
+        return -EINVAL;
+
Quoted 8 lines
Quoted 6 lines

+    if (WARN_ON(!IS_ALIGNED(ipa, size)))
+        return -EINVAL;
+
+    if (IS_ALIGNED(size, RMM_L2_BLOCK_SIZE)) {
+        map_level = 2;
+        map_size = RMM_L2_BLOCK_SIZE;

Same here, stick to the symbols than digits.

Ack

Quoted 10 lines
Quoted 8 lines

+    } else {
+        map_level = 3;
+        map_size = RMM_PAGE_SIZE;
+    }
+
+    for (offset = 0; offset < size; offset += map_size) { +        /*
+         * realm_map_ipa() enforces that the memory is writable,

The function names seems to be obsolete, please fix.

realm_map_ipa() is in arch/arm64/kvm/mmu.c and contains...

	/*
	 * Write permission is required for now even though it's possible to
	 * map unprotected pages (granules) as read-only. It's impossible to
	 * map protected pages (granules) as read-only.
	 */
	if (WARN_ON(!(prot & KVM_PGTABLE_PROT_W)))
		return -EFAULT;

...which is what this is referring to.
[And now I've read your follow up email ;) ]

Quoted 10 lines
Quoted 9 lines

+         * so for now we permit both read and write.
+         */
+        unsigned long desc = phys |
+                     PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) | +                     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | +                     KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
+        ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc); +
+        if (RMI_RETURN_STATUS(ret) == RMI_ERROR_RTT) {

Quoted 8 lines

Could we hit the following case and end up in failure ?

* Initially a single page is shared and the S2 is mapped
* Later the Realm shares the entire L2 block and encounters a fault at a new IPA within the L2 block.

In this case, we may try to L2 mapping when there is a L3 mapping and we could encounter (RMI_ERROR_RTT, 2).

Ugh! You are of course correct, this is broken. It turns out this is all irrelevant as things stand because we don't currently support anything bigger than (host) PAGE_SIZE mappings (forced in user_mem_abort()).

Given this I'm very much tempted to just delete the code supporting block mappings for now (just set map_level to RMM_RTT_MAX_LEVEL).

Longer term, I'm not sure what is the best approach, the options are:

1. Drop down and map the region at L3 (ignoring conflicting entries), followed by attempting to fold.

2. Destroy the table and directly map at L2.

Option 2 is clearly the most performant, but is potentially racy. In particular I'm not 100% sure whether you could end up with another thread attempting to map at L3 between the destroy and map, and therefore recreating the table.

Thanks,
Steve

Quoted 61 lines
Quoted 3 lines

+            /* Create missing RTTs and retry */
+            int level = RMI_RETURN_INDEX(ret);

So we should probably go down the rtt create step, with the following check.

            if (level < map_level) {

arch/arm64/kvm/rme.c static int realm_create_protected_data_page(struct realm *realm, · line 876

Quoted 7 lines
arch/arm64/kvm/rme.c +7 -0
+
+            ret = realm_create_rtt_levels(realm, ipa, level,
+                              map_level, memcache);
+            if (ret)
+                return -ENXIO;
+
+            ret = rmi_rtt_map_unprotected(rd, ipa, map_level, desc);

        } else {

Otherwise, may be we need to do some more hard work to fix it up.

1. If map_level == 3, something is terribly wrong or we raced with another thread ?

2. If map_level < 3 and we didn't race :

  a. Going one level down and creating the mappings there and then folding. But we could endup dealing with ERROR_RTT,3 as in (1).

  b. Easiest is to destroy the table at "map_level + 1" and retry the map.

Suzuki

Quoted 23 lines

+        }
+        /*
+         * RMI_ERROR_RTT can be reported for two reasons: either the +         * RTT tables are not there, or there is an RTTE already +         * present for the address.  The call to
+         * realm_create_rtt_levels() above handles the first case, and +         * in the second case this indicates that another thread has +         * already populated the RTTE for us, so we can ignore the +         * error and continue.
+         */
+        if (ret && RMI_RETURN_STATUS(ret) != RMI_ERROR_RTT) +            return -ENXIO;

arch/arm64/kvm/rme.c +8 -0
+
+        ipa += map_size;
+        phys += map_size;
+    }
+
+    return 0;
+}
+

  static int populate_region(struct kvm *kvm,
                 phys_addr_t ipa_base,
                 phys_addr_t ipa_end,

#69 lore raw

PATCH v8 21/43 KVM: arm64: Handle realm VCPU load

In reply to Steven Price

When loading a realm VCPU much of the work is handled by the RMM so only some of the actions are required. Rearrange kvm_arch_vcpu_load() slightly so we can bail out early for a realm guest.

---
 arch/arm64/kvm/arm.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/arm64/kvm/arm.c +9 -4
index cf707130ef66..08d5e0d76749 100644
@@ -644,10 +644,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	kvm_timer_vcpu_load(vcpu);
 	kvm_vgic_load(vcpu);
 	kvm_vcpu_load_debug(vcpu);
-	if (has_vhe())
-		kvm_vcpu_load_vhe(vcpu);
-	kvm_arch_vcpu_load_fp(vcpu);
-	kvm_vcpu_pmu_restore_guest(vcpu);
 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
 
@@ -671,6 +667,15 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 			     &vcpu->arch.vgic_cpu.vgic_v3);
 	}
 
+	/* No additional state needs to be loaded on Realmed VMs */
+	if (vcpu_is_rec(vcpu))
+		return;
+
+	if (has_vhe())
+		kvm_vcpu_load_vhe(vcpu);
+	kvm_arch_vcpu_load_fp(vcpu);
+	kvm_vcpu_pmu_restore_guest(vcpu);
+
 	if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
 		vcpu_set_on_unsupported_cpu(vcpu);
 }
Signature · 1 line
2.43.0