arm64: Support for Arm CCA in KVM
Subsystems (4)
v16 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), including the firmware support needed to communicate with the Realm Management Monitor (RMM).
For v15 the series was split into a 6-patch generic firmware/RMM series and a 37-patch KVM series. The two parts are combined again for v16, as requested to allow the complete stack to be tested by Sashiko as a single series.
The first six patches add the generic firmware layer for talking to the RMM, as specified by version 2.0-bet2 of the RMM specification[0]. They provide the RMI definitions and wrappers, discover and configure the RMM, implement Stateful RMI Operations (SROs), and ensure that the RMM has GPT entries for host memory. The remaining patches add the KVM support needed to create, populate and run Realm VMs.
Note that RMM v2.0 Beta 3 specification should be published soon. The changes introduced in that are minor and I expect this series to be largely compatible with the new spec.
The RMM v2.0 specification introduces SROs, which allow the RMM to complete an operation over several SMC calls while requesting or returning memory to the host. This allows interrupts to be handled in the middle of an operation and lets the RMM dynamically allocate memory for internal tracking purposes. For example, RMI_REC_CREATE no longer needs auxiliary granules to be provided up front, and can instead request memory during the operation.
The main changes since v15 are:
* Recombine the firmware/RMM and KVM portions into one series. The Realm-specific RMI wrappers remain separate from the generic firmware wrappers, in a new KVM patch.
* Rewrite Realm entry and exit handling to fit into the generic KVM run loop. Work which cannot be performed in the entry path is now completed through KVM requests, and MMIO, PSCI, RIPAS changes and host calls are adapted to the new flow.
* Rewrite Realm timer support to use KVM's IRQ-ops infrastructure and software resampling.
* Improve SRO cancellation and error handling. Ensure a cancelled operation isn't treated as successful, wrapper return values can carry negative Linux errors as well as RMI return values, and those errors are propagated through the KVM users.
* Check that the RMM supports the host page size before configuring it, and exclude firmware-reserved NOMAP memory when creating GPT entries.
* Expand the KVM_ARM_RMI_POPULATE documentation to make clear that Arm CCA cannot preserve memory contents during an in-place shared-to-private conversion.
* Allocate Realm parameters only while creating the Realm descriptor.
* Document the encoding of the arm64 VM types and require ICH_HCR_EL2.TDIR before advertising or creating Realm VMs.
This series is based on the guest_memfd in-place conversion v9 tree[1], which is itself based on kvm-x86/next. It is also available as a git repository:
https://gitlab.arm.com/linux-arm/linux-cca cca-host/v16
Work in progress changes for kvmtool are available from the git repository below:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/v13
The TF-RMM branch used for testing this series is available here:
https://git.trustedfirmware.org/TF-RMM/tf-rmm.git topics/rmm-v2.0-poc_3
There is a kvm-unit-test branch updated to support the attestation used in RMM v2.0 available here:
https://gitlab.arm.com/linux-arm/kvm-unit-tests-cca cca/v4
[0] https://developer.arm.com/documentation/den0137/2-0bet2/
One bet2 change, which moves metadata out of the individual address range descriptors, has intentionally not been implemented because that part of the specification is expected to be reverted.
[1] https://github.com/googleprodkernel/linux-cc/commits/guest_memfd-inplace-conversion-v9
Jean-Philippe Brucker (6):
KVM: arm64: CCA: Propagate breakpoint and watchpoint counts to
userspace
KVM: arm64: CCA: Set breakpoint parameters through SET_ONE_REG
KVM: arm64: CCA: Propagate max SVE vector length from the RMM
KVM: arm64: CCA: Configure max SVE vector length for a Realm
KVM: arm64: CCA: Provide register list for unfinalized RECs
KVM: arm64: CCA: Provide an accurate register list
Joey Gouly (2):
KVM: arm64: CCA: Allow userspace to inject aborts
KVM: arm64: CCA: Support RSI_HOST_CALL
Steven Price (34):
firmware: arm_rmm: Add SMC definitions for calling the RMM
firmware: arm_rmm: Add wrappers for direct RMI calls
firmware: arm_rmm: Check for RMI support at init
firmware: arm_rmm: Configure the RMM with the host's page size
firmware: arm_rmm: Add support for SRO
firmware: arm_rmm: Ensure the RMM has GPT entries for memory
arm64: mm: Handle Granule Protection Faults (GPFs)
KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
KVM: arm64: CCA: Add wrappers for realm related RMIs
KVM: arm64: CCA: Check for RMI support at KVM init
KVM: arm64: CCA: Check for LPA2 support
KVM: arm64: CCA: Define the user ABI
KVM: arm64: CCA: Add basic infrastructure for creating a realm
KVM: arm64: CCA: Allow passing the machine type in KVM creation
KVM: arm64: CCA: Tear down RTTs
KVM: arm64: CCA: Allocate and free RECs to match vCPUs
KVM: arm64: CCA: Support the VGIC in realms
KVM: arm64: CCA: Support timers in realm RECs
KVM: arm64: CCA: Handle realm enter/exit
KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
KVM: arm64: CCA: Handle realm MMIO emulation
KVM: arm64: Expose support for private memory
KVM: arm64: CCA: Create the realm descriptor
KVM: arm64: CCA: Activate realms on first vCPU run
KVM: arm64: CCA: Allow populating initial contents
KVM: arm64: CCA: Set RIPAS of initial memslots
KVM: arm64: CCA: Support runtime faulting of memory
KVM: arm64: CCA: Handle realm vCPU load
KVM: arm64: CCA: Validate register access for Realm VMs
KVM: arm64: CCA: Handle Realm PSCI requests
KVM: arm64: WARN on injected undef exceptions
KVM: arm64: CCA: Prevent Device mappings for realms
KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
KVM: arm64: CCA: Enable realms to be created
Suzuki K Poulose (3):
KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h
KVM: arm64: CCA: Don't expose unsupported capabilities for realm
guests
KVM: arm64: CCA: Allow checking SVE on VM instance
Documentation/virt/kvm/api.rst | 70 +- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kvm_asm.h | 2 + arch/arm64/include/asm/kvm_emulate.h | 37 + arch/arm64/include/asm/kvm_host.h | 12 +- arch/arm64/include/asm/kvm_pgtable.h | 6 +- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/include/asm/kvm_rmi.h | 139 +++ arch/arm64/include/asm/rmi_cmds.h | 465 ++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kernel/cpufeature.c | 1 + arch/arm64/kvm/Kconfig | 2 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arch_timer.c | 34 +- arch/arm64/kvm/arm.c | 139 ++- arch/arm64/kvm/guest.c | 93 +- arch/arm64/kvm/handle_exit.c | 14 + arch/arm64/kvm/hyp/pgtable.c | 1 + arch/arm64/kvm/hypercalls.c | 4 +- arch/arm64/kvm/inject_fault.c | 5 +- arch/arm64/kvm/mmio.c | 16 +- arch/arm64/kvm/mmu.c | 144 ++- arch/arm64/kvm/reset.c | 13 +- arch/arm64/kvm/rmi-exit.c | 178 +++ arch/arm64/kvm/rmi.c | 1561 ++++++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 47 +- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/mm/fault.c | 28 +- drivers/firmware/Kconfig | 1 + drivers/firmware/Makefile | 1 + drivers/firmware/arm_rmm/Kconfig | 26 + drivers/firmware/arm_rmm/Makefile | 2 + drivers/firmware/arm_rmm/rmi.c | 776 +++++++++++++ include/kvm/arm_psci.h | 2 + include/linux/arm-rmi-cmds.h | 201 ++++ include/linux/arm-smccc-rmi.h | 493 ++++++++ include/uapi/linux/kvm.h | 20 +- 37 files changed, 4446 insertions(+), 95 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_rmi.h create mode 100644 arch/arm64/include/asm/rmi_cmds.h create mode 100644 arch/arm64/kvm/rmi-exit.c create mode 100644 arch/arm64/kvm/rmi.c create mode 100644 drivers/firmware/arm_rmm/Kconfig create mode 100644 drivers/firmware/arm_rmm/Makefile create mode 100644 drivers/firmware/arm_rmm/rmi.c create mode 100644 include/linux/arm-rmi-cmds.h create mode 100644 include/linux/arm-smccc-rmi.h
Signature · 1 line
2.43.0
PATCH v16 1/45 firmware: arm_rmm: Add SMC definitions for calling the RMM
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 2.0-bet2
[1] https://developer.arm.com/documentation/den0137/2-0bet2/
---
Changes since v15:
* Dropped unused symbols REC_MAX_GIC_NUM_LRS and RMI_PERMITTED_GICV3_HCR_BITS.
* Output is now (partially) generated from the spec source.
Changes since v14:
* Updated to RMM spec v2.0-bet2 but without the changes to move metadata out of individual address range descriptors as this is expected to be reverted in a future spec release.
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
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.
--- include/linux/arm-smccc-rmi.h | 493 ++++++++++++++++++++++++++++++++++ 1 file changed, 493 insertions(+) create mode 100644 include/linux/arm-smccc-rmi.h
include/linux/arm-smccc-rmi.h new file +493 -0
@@ -0,0 +1,493 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023-2026 ARM Ltd. +* +* The values and structures in this file are from the Realm Management Monitor +* specification (DEN0137) version 2.0-bet2: +* https://developer.arm.com/documentation/den0137/2-0bet2/ +*/ + +#ifndef __LINUX_ARM_SMCCC_RMI_H_ +#define __LINUX_ARM_SMCCC_RMI_H_ + +#include <linux/arm-smccc.h> +#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/build_bug.h> +#include <linux/sizes.h> + +#include <asm/page.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_RTT_DATA_MAP_INIT SMC_RMI_CALL(0x0153) + +#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_READ_ENTRY SMC_RMI_CALL(0x0161) + +#define SMC_RMI_RTT_DEV_VALIDATE SMC_RMI_CALL(0x0163) +#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_RTT_INIT_RIPAS SMC_RMI_CALL(0x0168) +#define SMC_RMI_RTT_SET_RIPAS SMC_RMI_CALL(0x0169) +#define SMC_RMI_VSMMU_CREATE SMC_RMI_CALL(0x016a) +#define SMC_RMI_VSMMU_DESTROY SMC_RMI_CALL(0x016b) + +#define SMC_RMI_RMM_CONFIG_SET SMC_RMI_CALL(0x016e) +#define SMC_RMI_PSMMU_IRQ_NOTIFY SMC_RMI_CALL(0x016f) +#define SMC_RMI_ATTEST_PLAT_TOKEN_REFRESH SMC_RMI_CALL(0x0170) + +#define SMC_RMI_PDEV_ABORT SMC_RMI_CALL(0x0174) +#define SMC_RMI_PDEV_COMMUNICATE SMC_RMI_CALL(0x0175) +#define SMC_RMI_PDEV_CREATE SMC_RMI_CALL(0x0176) +#define SMC_RMI_PDEV_DESTROY SMC_RMI_CALL(0x0177) +#define SMC_RMI_PDEV_GET_STATE SMC_RMI_CALL(0x0178) + +#define SMC_RMI_PDEV_STREAM_KEY_REFRESH SMC_RMI_CALL(0x017a) +#define SMC_RMI_PDEV_SET_PUBKEY SMC_RMI_CALL(0x017b) +#define SMC_RMI_PDEV_STOP SMC_RMI_CALL(0x017c) +#define SMC_RMI_RTT_AUX_CREATE SMC_RMI_CALL(0x017d) +#define SMC_RMI_RTT_AUX_DESTROY SMC_RMI_CALL(0x017e) +#define SMC_RMI_RTT_AUX_FOLD SMC_RMI_CALL(0x017f) + +#define SMC_RMI_VDEV_ABORT SMC_RMI_CALL(0x0185) +#define SMC_RMI_VDEV_COMMUNICATE SMC_RMI_CALL(0x0186) +#define SMC_RMI_VDEV_CREATE SMC_RMI_CALL(0x0187) +#define SMC_RMI_VDEV_DESTROY SMC_RMI_CALL(0x0188) +#define SMC_RMI_VDEV_GET_STATE SMC_RMI_CALL(0x0189) +#define SMC_RMI_VDEV_UNLOCK SMC_RMI_CALL(0x018a) +#define SMC_RMI_RTT_SET_S2AP SMC_RMI_CALL(0x018b) + +#define SMC_RMI_VDEV_GET_INTERFACE_REPORT SMC_RMI_CALL(0x01d0) +#define SMC_RMI_VDEV_GET_MEASUREMENTS SMC_RMI_CALL(0x01d1) +#define SMC_RMI_VDEV_LOCK SMC_RMI_CALL(0x01d2) +#define SMC_RMI_VDEV_START SMC_RMI_CALL(0x01d3) +#define SMC_RMI_VDEV_P2P_BIND SMC_RMI_CALL(0x01d4) +#define SMC_RMI_VDEV_P2P_UNBIND SMC_RMI_CALL(0x01d5) +#define SMC_RMI_VSMMU_EVENT_HANDLE SMC_RMI_CALL(0x01d6) +#define SMC_RMI_PSMMU_ACTIVATE SMC_RMI_CALL(0x01d7) +#define SMC_RMI_PSMMU_DEACTIVATE SMC_RMI_CALL(0x01d8) + +#define SMC_RMI_PSMMU_ST_L2_CREATE SMC_RMI_CALL(0x01db) +#define SMC_RMI_PSMMU_ST_L2_DESTROY SMC_RMI_CALL(0x01dc) +#define SMC_RMI_DPT_L0_CREATE SMC_RMI_CALL(0x01dd) +#define SMC_RMI_DPT_L0_DESTROY SMC_RMI_CALL(0x01de) +#define SMC_RMI_DPT_L1_CREATE SMC_RMI_CALL(0x01df) +#define SMC_RMI_DPT_L1_DESTROY SMC_RMI_CALL(0x01e0) +#define SMC_RMI_GRANULE_TRACKING_GET SMC_RMI_CALL(0x01e1) + +#define SMC_RMI_GRANULE_TRACKING_SET SMC_RMI_CALL(0x01e3) +#define SMC_RMI_CMEM_ADD_PDEV SMC_RMI_CALL(0x01e4) +#define SMC_RMI_CMEM_CREATE SMC_RMI_CALL(0x01e5) +#define SMC_RMI_CMEM_DESTROY SMC_RMI_CALL(0x01e6) +#define SMC_RMI_CMEM_POPULATE SMC_RMI_CALL(0x01e7) +#define SMC_RMI_CMEM_REMOVE_PDEV SMC_RMI_CALL(0x01e8) +#define SMC_RMI_CMEM_START SMC_RMI_CALL(0x01e9) +#define SMC_RMI_CMEM_STOP SMC_RMI_CALL(0x01ea) +#define SMC_RMI_CMEM_UNPOPULATE SMC_RMI_CALL(0x01eb) +#define SMC_RMI_RMM_CONFIG_GET SMC_RMI_CALL(0x01ec) +#define SMC_RMI_PDEV_MEC_REFRESH SMC_RMI_CALL(0x01ed) +#define SMC_RMI_RMM_STATE_GET SMC_RMI_CALL(0x01ee) + +#define SMC_RMI_PSMMU_EVENT_CONSUME SMC_RMI_CALL(0x01f0) +#define SMC_RMI_GRANULE_RANGE_DELEGATE SMC_RMI_CALL(0x01f1) +#define SMC_RMI_GRANULE_RANGE_UNDELEGATE SMC_RMI_CALL(0x01f2) +#define SMC_RMI_GPT_L1_CREATE SMC_RMI_CALL(0x01f3) +#define SMC_RMI_GPT_L1_DESTROY SMC_RMI_CALL(0x01f4) +#define SMC_RMI_RTT_DATA_MAP SMC_RMI_CALL(0x01f5) +#define SMC_RMI_RTT_DATA_UNMAP SMC_RMI_CALL(0x01f6) +#define SMC_RMI_RTT_DEV_MAP SMC_RMI_CALL(0x01f7) +#define SMC_RMI_RTT_DEV_UNMAP SMC_RMI_CALL(0x01f8) +#define SMC_RMI_RTT_ARCH_DEV_MAP SMC_RMI_CALL(0x01f9) +#define SMC_RMI_RTT_ARCH_DEV_UNMAP SMC_RMI_CALL(0x01fa) +#define SMC_RMI_RTT_UNPROT_MAP SMC_RMI_CALL(0x01fb) +#define SMC_RMI_RTT_UNPROT_UNMAP SMC_RMI_CALL(0x01fc) +#define SMC_RMI_RTT_AUX_PROT_MAP SMC_RMI_CALL(0x01fd) +#define SMC_RMI_RTT_AUX_PROT_UNMAP SMC_RMI_CALL(0x01fe) +#define SMC_RMI_RTT_AUX_UNPROT_MAP SMC_RMI_CALL(0x01ff) +#define SMC_RMI_RTT_AUX_UNPROT_UNMAP SMC_RMI_CALL(0x0200) +#define SMC_RMI_REALM_TERMINATE SMC_RMI_CALL(0x0201) +#define SMC_RMI_RMM_ACTIVATE SMC_RMI_CALL(0x0202) +#define SMC_RMI_OP_CONTINUE SMC_RMI_CALL(0x0203) +#define SMC_RMI_PDEV_STREAM_CONNECT SMC_RMI_CALL(0x0204) +#define SMC_RMI_PDEV_STREAM_DISCONNECT SMC_RMI_CALL(0x0205) +#define SMC_RMI_PDEV_STREAM_COMPLETE SMC_RMI_CALL(0x0206) +#define SMC_RMI_PDEV_STREAM_KEY_PURGE SMC_RMI_CALL(0x0207) +#define SMC_RMI_OP_MEM_DONATE SMC_RMI_CALL(0x0208) +#define SMC_RMI_OP_MEM_RECLAIM SMC_RMI_CALL(0x0209) +#define SMC_RMI_OP_CANCEL SMC_RMI_CALL(0x020a) +#define SMC_RMI_VSMMU_FEATURES SMC_RMI_CALL(0x020b) +#define SMC_RMI_VSMMU_CMD_GET SMC_RMI_CALL(0x020c) +#define SMC_RMI_VSMMU_CMD_COMPLETE SMC_RMI_CALL(0x020d) +#define SMC_RMI_PSMMU_INFO SMC_RMI_CALL(0x020e) + +#define RMI_ABI_MAJOR_VERSION 2 +#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_RETURN_STATUS_MASK (0xFFUL) +#define RMI_RETURN_INDEX_MASK (0xFFUL << 8) +#define RMI_RETURN_MEMREQ_MASK (0x3UL << 8) +#define RMI_RETURN_CAN_CANCEL_MASK (0x1UL << 10) + +#define RMI_RETURN_STATUS (ret) FIELD_GET(RMI_RETURN_STATUS_MASK, ret) +#define RMI_RETURN_INDEX (ret) FIELD_GET(RMI_RETURN_INDEX_MASK, ret) +#define RMI_RETURN_MEMREQ (ret) FIELD_GET(RMI_RETURN_MEMREQ_MASK, ret) +#define RMI_RETURN_CAN_CANCEL (ret) FIELD_GET(RMI_RETURN_CAN_CANCEL_MASK, ret) + +#define RMI_SUCCESS 0 +#define RMI_ERROR_INPUT 1 +#define RMI_ERROR_REALM 2 +#define RMI_ERROR_REC 3 +#define RMI_ERROR_RTT 4 +#define RMI_ERROR_NOT_SUPPORTED 5 +#define RMI_ERROR_DEVICE 6 +#define RMI_ERROR_RTT_AUX 7 +#define RMI_ERROR_PSMMU_ST 8 +#define RMI_ERROR_DPT 9 +#define RMI_BUSY 10 +#define RMI_ERROR_GLOBAL 11 +#define RMI_ERROR_TRACKING 12 +#define RMI_INCOMPLETE 13 +#define RMI_BLOCKED 14 +#define RMI_ERROR_GPT 15 +#define RMI_ERROR_GRANULE 16 + +#define RMI_CONTINUE_KEEP_GOING 0 +#define RMI_CONTINUE_STOP 1 + +#define RMI_OP_MEM_REQ_NONE 0 +#define RMI_OP_MEM_REQ_DONATE 1 +#define RMI_OP_MEM_REQ_RECLAIM 2 + +#define RMI_DONATE_SIZE_MASK 3UL +#define RMI_DONATE_COUNT_MASK GENMASK(15, 2) +#define RMI_DONATE_CONTIG_MASK BIT(16) +#define RMI_DONATE_STATE_MASK BIT(17) + +#define RMI_DONATE_SIZE (req) FIELD_GET(RMI_DONATE_SIZE_MASK, req) +#define RMI_DONATE_COUNT (req) FIELD_GET(RMI_DONATE_COUNT_MASK, req) +#define RMI_DONATE_CONTIG (req) FIELD_GET(RMI_DONATE_CONTIG_MASK, req) +#define RMI_DONATE_STATE (req) FIELD_GET(RMI_DONATE_STATE_MASK, req) + +#define RMI_OP_MEM_DELEGATED 0 +#define RMI_OP_MEM_UNDELEGATED 1 + +#define RMI_ADDR_TYPE_NONE 0 +#define RMI_ADDR_TYPE_SINGLE 1 +#define RMI_ADDR_TYPE_LIST 2 + +#define RMI_ADDR_RANGE_SIZE_MASK GENMASK(1, 0) +#define RMI_ADDR_RANGE_COUNT_MASK GENMASK(PAGE_SHIFT - 1, 2) +#define RMI_ADDR_RANGE_ADDR_MASK (PAGE_MASK & GENMASK(51, 0)) +#define RMI_ADDR_RANGE_STATE_MASK BIT(63) + +#define RMI_ADDR_RANGE_SIZE (ar) (FIELD_GET(RMI_ADDR_RANGE_SIZE_MASK, \ + (ar))) +#define RMI_ADDR_RANGE_COUNT (ar) (FIELD_GET(RMI_ADDR_RANGE_COUNT_MASK, \ + (ar))) +#define RMI_ADDR_RANGE_ADDR (ar) ((ar) & RMI_ADDR_RANGE_ADDR_MASK) +#define RMI_ADDR_RANGE_STATE (ar) (FIELD_GET(RMI_ADDR_RANGE_STATE_MASK, \ + (ar))) + +enum rmi_ripas { +RMI_EMPTY =0 , +RMI_RAM =1 , +RMI_DESTROYED =2 , +RMI_DEV =3 , +}; + +#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 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 BIT(26) +#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS GENMASK(31, 27) + +#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB BIT(0) +#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB BIT(1) +#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB BIT(2) +#define RMI_FEATURE_REGISTER_1_HASH_SHA_256 BIT(3) +#define RMI_FEATURE_REGISTER_1_HASH_SHA_384 BIT(4) +#define RMI_FEATURE_REGISTER_1_HASH_SHA_512 BIT(5) +#define RMI_FEATURE_REGISTER_1_MAX_RECS_ORDER GENMASK(9, 6) +#define RMI_FEATURE_REGISTER_1_L0GPTSZ GENMASK(13, 10) +#define RMI_FEATURE_REGISTER_1_PPS GENMASK(16, 14) + +#define RMI_FEATURE_REGISTER_2_DA BIT(0) +#define RMI_FEATURE_REGISTER_2_DA_COH BIT(1) +#define RMI_FEATURE_REGISTER_2_VSMMU BIT(2) +#define RMI_FEATURE_REGISTER_2_ATS BIT(3) +#define RMI_FEATURE_REGISTER_2_MAX_VDEVS_ORDER GENMASK(7, 4) +#define RMI_FEATURE_REGISTER_2_VDEV_KROU BIT(8) +#define RMI_FEATURE_REGISTER_2_NON_TEE_STREAM BIT(9) +#define RMI_FEATURE_REGISTER_2_P2P BIT(10) +#define RMI_FEATURE_REGISTER_2_CMEM_CXL BIT(11) +#define RMI_FEATURE_REGISTER_2_MAX_CMEM GENMASK(19, 12) +#define RMI_FEATURE_REGISTER_2_CMEM_TSE_REQ BIT(20) + +#define RMI_FEATURE_REGISTER_3_MAX_NUM_AUX_PLANES GENMASK(3, 0) +#define RMI_FEATURE_REGISTER_3_RTT_PLANE GENMASK(5, 4) +#define RMI_FEATURE_REGISTER_3_RTT_S2AP_INDIRECT BIT(6) + +#define RMI_FEATURE_REGISTER_4_MEC_COUNT GENMASK(63, 0) + +#define RMI_MEM_CATEGORY_CONVENTIONAL 0 +#define RMI_MEM_CATEGORY_DEV_NCOH 1 +#define RMI_MEM_CATEGORY_DEV_COH 2 + +#define RMI_TRACKING_RESERVED 0 +#define RMI_TRACKING_NONE 1 +#define RMI_TRACKING_FINE 2 +#define RMI_TRACKING_COARSE 3 + +#define RMI_GRANULE_SIZE_4KB 0 +#define RMI_GRANULE_SIZE_16KB 1 +#define RMI_GRANULE_SIZE_64KB 2 + +/* +* Note many of these fields are smaller than u64 but all fields have u64 +* alignment, so use u64 to ensure correct alignment. +*/ +struct rmm_config { +union {/* 0x0 */ +struct { +u64 tracking_region_size; +u64 rmi_granule_size; + }; +u8 sizer[SZ_4K ]; + }; +}; + +static_assert (sizeof (struct rmm_config ) ==SZ_4K ); + +#define RMI_REALM_PARAM_FLAG_LPA2 BIT(0) +#define RMI_REALM_PARAM_FLAG_SVE BIT(1) +#define RMI_REALM_PARAM_FLAG_PMU BIT(2) +#define RMI_REALM_PARAM_FLAG_DA BIT(3) +#define RMI_REALM_PARAM_FLAG_LFA_POLICY GENMASK(6, 5) +#define RMI_REALM_PARAM_FLAG_MEC_POLICY GENMASK(8, 7) + +#define RMI_HASH_SHA_256 0 +#define RMI_HASH_SHA_512 1 +#define RMI_HASH_SHA_384 2 + +struct realm_params { +union {/* 0x0 */ +struct { +u64 flags0; +u64 s2sz; +u64 sve_vl; +u64 num_bps; +u64 num_wps; +u64 pmu_num_ctrs; +u64 hash_algo; +u64 num_aux_planes; + }; +u8 padding0[0x400 ]; + }; +union {/* 0x400 */ +struct { +u8 rpv[64 ]; +u64 ats_plane; + }; +u8 padding1[0x400 ]; + }; +union {/* 0x800 */ +struct { +u64 padding2; +u64 rtt_base; +s64 rtt_level_start; +u64 rtt_num_start; +u64 flags1; + }; +u8 padding3[0x700 ]; + }; +union {/* 0xf00 */ +struct { +u8 padding4[0x80 ]; +u64 aux_rtt_base[3 ]; + }; +u8 padding5[0x100 ]; + }; +}; + +static_assert (sizeof (struct realm_params ) ==SZ_4K ); + +/* +* 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(0) + +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[0xd00 ]; + }; +}; + +static_assert (sizeof (struct rec_params ) ==SZ_4K ); + +#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_ENTER_FLAG_S2AP_RESPONSE BIT(5) +#define REC_ENTER_FLAG_DEV_MEM_RESPONSE BIT(6) +#define REC_ENTER_FLAG_FORCE_P0 BIT(7) + +#define REC_RUN_GPRS 31 + +struct rec_enter { +union {/* 0x000 */ +u64 flags; +u8 padding0[0x200 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding1[0x600 ]; + }; +}; + +static_assert (sizeof (struct rec_enter ) ==SZ_2K ); + +#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 +#define RMI_EXIT_S2AP_CHANGE 0x07 +#define RMI_EXIT_VDEV_VALIDATE_MAPPING 0x08 +#define RMI_EXIT_VSMMU_COMMAND 0x0a +#define RMI_EXIT_VDEV_P2P_BINDING 0x0b + +struct rec_exit { +union {/* 0x000 */ +u8 exit_reason; +u8 padding0[0x100 ]; + }; +union {/* 0x100 */ +struct { +u64 esr; +u64 far; +u64 hpfar; +u64 rtt_tree; + }; +u8 padding1[0x100 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding2[0x100 ]; + }; +union {/* 0x300 */ +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; +u8 ripas_value; +u8 padding5[0xf ]; +u64 s2ap_base; +u64 s2ap_top; +u64 vdev_id_1; +u64 vdev_id_2; +u64 dev_mem_base; +u64 dev_mem_top; +u64 dev_mem_pa; + }; +u8 padding6[0x100 ]; + }; +union {/* 0x600 */ +struct { +u16 imm; +u8 padding7[0x6 ]; +u64 plane; + }; +u8 padding8[0x100 ]; + }; +union {/* 0x700 */ +struct { +u8 pmu_ovf_status; +u8 padding9[0xf ]; +u64 vsmmu; + }; +u8 padding10[0x100 ]; + }; +}; + +static_assert (sizeof (struct rec_exit ) ==SZ_2K ); + +struct rec_run { +struct rec_enter enter; +struct rec_exit exit; +}; + +static_assert (sizeof (struct rec_run ) ==SZ_4K ); + +/* RMI_RTT_UNPROT_MAP_FLAGS definitions */ +#define RMI_RTT_UNPROT_MAP_FLAGS_OADDR_TYPE GENMASK(1, 0) +#define RMI_RTT_UNPROT_MAP_FLAGS_LIST_COUNT GENMASK(15, 2) +#define RMI_RTT_UNPROT_MAP_FLAGS_MEMATTR GENMASK(18, 16) +#define RMI_RTT_UNPROT_MAP_FLAGS_S2AP GENMASK(22, 19) + +/* RMI_RTT_PROT_MAP_FLAGS definitions */ +#define RMI_RTT_PROT_MAP_FLAGS_OADDR_TYPE GENMASK(1, 0) +#define RMI_RTT_PROT_MAP_FLAGS_LIST_COUNT GENMASK(15, 2) + +/* S2AP Direct Encodings, used in RMI_RTT_UNPROT_MAP_FLAGS_S2AP */ +#define RMI_S2AP_DIRECT_WRITE BIT(0) +#define RMI_S2AP_DIRECT_READ BIT(1) + +#endif /* __LINUX_ARM_SMCCC_RMI_H_ */
Signature · 1 line
2.43.0
PATCH v16 2/45 firmware: arm_rmm: Add wrappers for direct RMI calls
In reply to Steven PriceThe wrappers make the call sites easier to read and deal with the boilerplate of handling the error codes from the RMM.
---
Changes from v15:
* Split out wrappers for realms to a separate commit.
* Wrappers that have output arguments now only write the output when the command completes successfully.
Changes from v14:
* Update to RMM v2.0-bet2 spec.
* Move SRO related wrappers to a later patch due to dependencies.
* Common wrappers are split into include/linux/arm-rmi-cmds.h.
Changes from v13:
* Update to RMM v2.0-bet1 spec including some SRO support (there still some FIXMEs where SRO support is incomplete).
Changes from v12:
* Update to RMM v2.0 specification
Changes from v8:
* Switch from arm_smccc_1_2_smc() to arm_smccc_1_2_invoke() in rmi_rtt_read_entry() for consistency.
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()
--- include/linux/arm-rmi-cmds.h | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 include/linux/arm-rmi-cmds.h
include/linux/arm-rmi-cmds.h new file +83 -0
@@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2026 ARM Ltd. +*/ + +#ifndef __LINUX_ARM_RMI_CMDS_H_ +#define __LINUX_ARM_RMI_CMDS_H_ + +#include <linux/arm-smccc-rmi.h> +#include <linux/bug.h> +#include <linux/types.h> + +#include <asm/page.h> + +/** +* rmi_rmm_config_set() - Configure the RMM +* @cfg_ptr: PA of a struct rmm_config +* +* Sets configuration options on the RMM. +* +* Return: RMI return code +*/ +static inline int rmi_rmm_config_set (unsigned long cfg_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RMM_CONFIG_SET , cfg_ptr, &res); + +return res.a0; +} + +/** +* rmi_granule_tracking_get() - Get configuration of a Granule tracking region +* @start: Base PA of the tracking region +* @end: End of the PA region +* @out_category: Memory category +* @out_state: Tracking region state +* @out_top: Top of the memory region +* +* Return: RMI return code +*/ +static inline int rmi_granule_tracking_get (unsigned long start, +unsigned long end, +unsigned long *out_category, +unsigned long *out_state, +unsigned long *out_top) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_GRANULE_TRACKING_GET , start, end, &res); + +if (res.a0 ==RMI_SUCCESS ) { +if (out_category) + *out_category = res.a1; +if (out_state) + *out_state = res.a2; +if (out_top) + *out_top = res.a3; + } + +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 (res.a0 ==RMI_SUCCESS && out) + *out = res.a1; + +return res.a0; +} + +#endif
Signature · 1 line
2.43.0
PATCH v16 3/45 firmware: arm_rmm: Check for RMI support at init
In reply to Steven PriceQuery the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.
---
v16:
* Update Kconfig text to include PCIe TDISP.
* Export rmi_feat_reg() here rather than in a later commit.
v15:
* The code is moved again, this time into the 'firmware' directory.
v14:
* This moves the basic RMI setup into the 'kernel' directory. This is because RMI will be used for some features outside of KVM so should be available even if KVM isn't compiled in.
--- arch/arm64/Kconfig | 1 + arch/arm64/kernel/cpufeature.c | 1 + drivers/firmware/Kconfig | 1 + drivers/firmware/Makefile | 1 + drivers/firmware/arm_rmm/Kconfig | 26 ++++++++ drivers/firmware/arm_rmm/Makefile | 2 + drivers/firmware/arm_rmm/rmi.c | 101 ++++++++++++++++++++++++++++++ include/linux/arm-rmi-cmds.h | 2 + 8 files changed, 135 insertions(+) create mode 100644 drivers/firmware/arm_rmm/Kconfig create mode 100644 drivers/firmware/arm_rmm/Makefile create mode 100644 drivers/firmware/arm_rmm/rmi.c
arch/arm64/Kconfig +1 -0
@@ -38,6 +38,7 @@ config ARM64 select ARCH_HAS_MEMBARRIER_SYNC_CORE select ARCH_HAS_MEM_ENCRYPT select ARCH_SUPPORTS_MSEAL_SYSTEM_MAPPINGS + select ARCH_SUPPORTS_RMM select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE select ARCH_HAS_NONLEAF_PMD_YOUNG if ARM64_HAFT
arch/arm64/kernel/cpufeature.c +1 -0
@@ -293,6 +293,7 @@ static const struct arm64_ftr_bits ftr_id_aa64isar3[] = {static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {ARM64_FTR_BITS (FTR_HIDDEN ,FTR_NONSTRICT ,FTR_LOWER_SAFE ,ID_AA64PFR0_EL1_CSV3_SHIFT ,4 ,0 ),ARM64_FTR_BITS (FTR_HIDDEN ,FTR_NONSTRICT ,FTR_LOWER_SAFE ,ID_AA64PFR0_EL1_CSV2_SHIFT ,4 ,0 ), +ARM64_FTR_BITS (FTR_HIDDEN ,FTR_NONSTRICT ,FTR_LOWER_SAFE ,ID_AA64PFR0_EL1_RME_SHIFT ,4 ,0 ),ARM64_FTR_BITS (FTR_VISIBLE ,FTR_STRICT ,FTR_LOWER_SAFE ,ID_AA64PFR0_EL1_DIT_SHIFT ,4 ,0 ),ARM64_FTR_BITS (FTR_HIDDEN ,FTR_NONSTRICT ,FTR_LOWER_SAFE ,ID_AA64PFR0_EL1_AMU_SHIFT ,4 ,0 ),ARM64_FTR_BITS (FTR_HIDDEN ,FTR_STRICT ,FTR_LOWER_SAFE ,ID_AA64PFR0_EL1_MPAM_SHIFT ,4 ,0 ),
drivers/firmware/Kconfig +1 -0
@@ -301,5 +301,6 @@ source "drivers/firmware/samsung/Kconfig" source "drivers/firmware/smccc/Kconfig" source "drivers/firmware/tegra/Kconfig" source "drivers/firmware/xilinx/Kconfig" +source "drivers/firmware/arm_rmm/Kconfig" endmenu
drivers/firmware/Makefile +1 -0
@@ -38,3 +38,4 @@ obj-y += samsung/ obj-y += smccc/ obj-y += tegra/ obj-y += xilinx/ +obj-y += arm_rmm/
drivers/firmware/arm_rmm/Kconfig new file +26 -0
@@ -0,0 +1,26 @@ + +config ARCH_SUPPORTS_RMM + bool + +config ARM_RMM + bool "Realm Management Monitor (RMM) Support" + depends on ARCH_SUPPORTS_RMM + default y + help + Support the Realm Management Monitor (RMM) on Arm systems that + implement the Realm Management Extension (RME), as defined by the + Arm Confidential Compute Architecture. + + The RMM runs at EL2 in the Realm world and provides the Realm + Management Interface (RMI) used by a Normal World host to create, + manage and run protected virtual machines called Realms. The RMM can + also act as a TSM, as defined by the PCIe TDISP and can manage the + PCI IDE setup for securing the PCIe links. + + This option builds the host-side RMI support used by KVM to detect a + compatible RMM, configure it, manage delegated memory and enable + Realm guests. + + Selecting this option does not by itself make Realm guests available: + the system must also provide RME-capable hardware and firmware with a + compatible RMM implementation.
drivers/firmware/arm_rmm/Makefile new file +2 -0
@@ -0,0 +1,2 @@ + +obj-$(CONFIG_ARM_RMM) = rmi.o
drivers/firmware/arm_rmm/rmi.c new file +101 -0
101 lines not on this page · raw message
include/linux/arm-rmi-cmds.h +2 -0
@@ -12,6 +12,8 @@#include <asm/page.h> +unsigned long rmi_feat_reg (unsigned long id); +/** * rmi_rmm_config_set() - Configure the RMM * @cfg_ptr: PA of a struct rmm_config
Signature · 1 line
2.43.0
PATCH v16 4/45 firmware: arm_rmm: Configure the RMM with the host's page size
In reply to Steven PriceRMM v2.0 brings the ability to set the RMM's granule size. Check the feature registers and configure the RMM so that it matches the host's page size. This means that operations can be done with a granularity equal to PAGE_SIZE.
---
Changes since v15:
* Actually check the feature register for the host's page-size support.
Changes since v14:
* Move the implementation into drivers/firmware/arm_rmm.
Changes since v13:
* Moved out of KVM.
--- drivers/firmware/arm_rmm/rmi.c | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
drivers/firmware/arm_rmm/rmi.c +61 -0
@@ -77,6 +77,63 @@ static int rmi_read_features(void)return 0 ; } +static int rmi_configure (void ) +{ +unsigned long granule_feature; +unsigned long granule_size; +long rmi_ret; +int ret =0 ; +struct rmm_config *config; + +switch (PAGE_SIZE ) { +case SZ_4K : + granule_size =RMI_GRANULE_SIZE_4KB ; + granule_feature =RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB ; +break ; +case SZ_16K : + granule_size =RMI_GRANULE_SIZE_16KB ; + granule_feature =RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB ; +break ; +case SZ_64K : + granule_size =RMI_GRANULE_SIZE_64KB ; + granule_feature =RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB ; +break ; +default : +BUILD_BUG (); + } + +if (!(rmi_feat_reg (1 ) & granule_feature)) { +pr_err ("RMM does not support %luKB granules\n" , +PAGE_SIZE >>10 ); +return -ENXIO ; + } + + config = (struct rmm_config *)get_zeroed_page (GFP_KERNEL ); +if (!config) +return -ENOMEM ; + + config->rmi_granule_size = granule_size; + +/* +* For now we set the tracking_region_size to 0 which is the only option +* for 4KB PAGE_SIZE (1GB for 4KB PAGE_SIZE, 32MB/512MB for 16KB/64KB). +* TODO: Support other tracking sizes via Kconfig option for other +* PAGE_SIZES +*/ + config->tracking_region_size =0 ; + + rmi_ret =rmi_rmm_config_set (virt_to_phys (config)); +if (rmi_ret) { +pr_err ("RMM config set failed\n" ); + ret = -EINVAL ; + goto out_free_config; + } + +out_free_config: +free_page ((unsigned long )config); +return ret; +} +static int __init arm64_init_rmi (void ) {int ret;
@@ -90,6 +147,10 @@ static int __init arm64_init_rmi(void)if (ret)return ret; + ret =rmi_configure (); +if (ret) +return ret; +return 0 ; }
Signature · 1 line
2.43.0
PATCH v16 5/45 firmware: arm_rmm: Add support for SRO
In reply to Steven PriceRMM v2.0 introduces the concept of "Stateful RMI Operations" (SRO). This means that an SMC can return with an operation still in progress. The host is expected to continue the operation until it reaches a conclusion (either success or failure). During this process the RMM can request additional memory ('donate') or hand memory back to the host ('reclaim'). The host can request an in progress operation is cancelled, but still continue the operation until it has completed (otherwise the incomplete operation may cause future RMM operations to fail).
The SRO is tracked using a struct rmi_sro_state object which keeps track of any memory which has been allocated but not yet consumed by the RMM or reclaimed from the RMM. This allows the memory to be reused in a future request within the same operation. It will also permit an operation to be done in a context where memory allocation may be difficult (e.g. atomic context) with the option to abort the operation and retry the memory allocation outside of the atomic context. The memory stored in the struct rmi_sro_state object can then be reused on the subsequent attempt.
Wrappers for SRO RMI commands are also provided here because they depend on the rmi_sro_execute() implementation added by this patch. Delegate/undelegate handles are also added here because they now use the SRO/stateful command infrastructure.
---
v16:
* Wrappers for realm guests split into a separate patch.
* Better support for cancellation - previously a cancelled operation could be treated as successful.
* Consistently use a signed type for wrapper return values so that Linux error codes can be returned as well as RMI return values.
v15:
* Wrappers for SRO RMI functions are provided in this patch due to their dependency on the SRO infrastructure.
* Fold the range delegate/undelegate wrappers into this patch because they depend on the stateful command infrastructure.
* Add cpu_relax() calls when RMI_BUSY/RMI_BLOCKED is returned.
* Various fixes.
v14:
* SRO support has improved although is still not fully complete. The infrastructure has been moved out of KVM.
--- drivers/firmware/arm_rmm/rmi.c | 515 ++++++++++++++++++++++++++++++++- include/linux/arm-rmi-cmds.h | 114 ++++++++ 2 files changed, 628 insertions(+), 1 deletion(-)
drivers/firmware/arm_rmm/rmi.c +514 -1
@@ -6,6 +6,7 @@#include <linux/cpufeature.h> #include <linux/memblock.h> #include <linux/arm-rmi-cmds.h> +#include <linux/processor.h> #include <linux/slab.h> #include <asm/memory.h>
@@ -24,6 +25,502 @@ unsigned long rmi_feat_reg(unsigned long id) }EXPORT_SYMBOL_GPL (rmi_feat_reg); +int rmi_delegate_range (phys_addr_t phys, +unsigned long size, +phys_addr_t *out_phys) +{ +long ret =0 ; +unsigned long top = phys + size; +unsigned long out_top; + +while (phys < top) { + ret =rmi_granule_range_delegate (phys, top, &out_top); +if (ret ==RMI_SUCCESS ) + phys = out_top; +else if (ret ==RMI_BUSY || ret ==RMI_BLOCKED ) +cpu_relax (); +else +break ; + } + +if (out_phys) + *out_phys = phys; + +return ret; +} +EXPORT_SYMBOL_GPL (rmi_delegate_range); + +int rmi_undelegate_range (phys_addr_t phys, +unsigned long size) +{ +long ret =0 ; +unsigned long top = phys + size; +unsigned long out_top; + +while (phys < top) { + ret =rmi_granule_range_undelegate (phys, top, &out_top); +if (ret ==RMI_SUCCESS ) + phys = out_top; +else if (ret ==RMI_BUSY || ret ==RMI_BLOCKED ) +cpu_relax (); +else +break ; + } + +return ret; +} +EXPORT_SYMBOL_GPL (rmi_undelegate_range); + +static unsigned long donate_req_to_size (unsigned long donatereq) +{ +unsigned long unit_size =RMI_DONATE_SIZE (donatereq); + +return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (3 - unit_size)); +} + +static void rmi_smccc_invoke (struct arm_smccc_1_2_regs *regs_in, +struct arm_smccc_1_2_regs *regs_out) +{ +struct arm_smccc_1_2_regs regs = *regs_in; +unsigned long status; + +while (1 ) { +arm_smccc_1_2_invoke (®s, regs_out); + status =RMI_RETURN_STATUS (regs_out->a0); +if (status !=RMI_BUSY && status !=RMI_BLOCKED ) +break ; +cpu_relax (); + } +} + +static void rmi_op_continue (unsigned long sro_handle,unsigned long flags, +struct arm_smccc_1_2_regs *out_regs) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_OP_CONTINUE , sro_handle, flags + }; + +rmi_smccc_invoke (®s, out_regs); +} + +static void rmi_op_cancel (unsigned long sro_handle, +struct arm_smccc_1_2_regs *out_regs) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_OP_CANCEL , sro_handle + }; + +rmi_smccc_invoke (®s, out_regs); +} + +static void rmi_op_mem_donate (unsigned long sro_handle,unsigned long list_addr, +unsigned long list_count,unsigned long flags, +struct arm_smccc_1_2_regs *out_regs) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_OP_MEM_DONATE , sro_handle, list_addr, list_count, flags + }; + +rmi_smccc_invoke (®s, out_regs); +} + +static void rmi_op_mem_reclaim (unsigned long sro_handle, +unsigned long list_addr, +unsigned long list_count, +struct arm_smccc_1_2_regs *out_regs) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_OP_MEM_RECLAIM , sro_handle, list_addr, list_count + }; + +rmi_smccc_invoke (®s, out_regs); +} + +int free_delegated_page (phys_addr_t phys) +{ +if (WARN_ON_ONCE (rmi_undelegate_page (phys))) { +/* Undelegate failed: leak the page */ +return -EBUSY ; + } + +free_page ((unsigned long )phys_to_virt (phys)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (free_delegated_page); + +static int rmi_sro_ensure_capacity (struct rmi_sro_state *sro, +unsigned long count) +{ +if (WARN_ON_ONCE (sro->addr_count >RMI_MAX_ADDR_LIST )) +return -EOVERFLOW ; + +if (count >RMI_MAX_ADDR_LIST - sro->addr_count) +return -ENOSPC ; + +return 0 ; +} + +static int rmi_sro_donate_contig (struct rmi_sro_state *sro, +unsigned long sro_handle, +unsigned long donatereq, +struct arm_smccc_1_2_regs *out_regs, +gfp_t gfp) +{ +unsigned long unit_size =RMI_DONATE_SIZE (donatereq); +unsigned long unit_size_bytes =donate_req_to_size (donatereq); +unsigned long count =RMI_DONATE_COUNT (donatereq); +unsigned long state =RMI_DONATE_STATE (donatereq); +unsigned long size = unit_size_bytes * count; +unsigned long addr_range; +int ret; +void *virt; +phys_addr_t phys; + +/* +* The RMM specification requires contiguous allocations are always a +* power of 2 +*/ +if (WARN_ON_ONCE (!is_power_of_2 (size))) +return -EINVAL ; + +for (int i =0 ; i < sro->addr_count; i++) { +unsigned long entry = sro->addr_list[i]; + +if (RMI_ADDR_RANGE_SIZE (entry) == unit_size && +RMI_ADDR_RANGE_COUNT (entry) == count && +RMI_ADDR_RANGE_STATE (entry) == state && +IS_ALIGNED (RMI_ADDR_RANGE_ADDR (entry), size)) { + sro->addr_count--; +swap (sro->addr_list[sro->addr_count], + sro->addr_list[i]); + + goto out; + } + } + + ret =rmi_sro_ensure_capacity (sro,1 ); +if (ret) +return ret; + + virt =alloc_pages_exact (size, gfp); +if (!virt) +return -ENOMEM ; + phys =virt_to_phys (virt); + +if (state ==RMI_OP_MEM_DELEGATED ) { +phys_addr_t delegated_phys; + +if (rmi_delegate_range (phys, size, &delegated_phys)) { +if (!rmi_undelegate_range (phys, delegated_phys - phys)) +free_pages_exact (virt, size); +return -ENXIO ; + } + } + + addr_range = phys &RMI_ADDR_RANGE_ADDR_MASK ; +FIELD_MODIFY (RMI_ADDR_RANGE_SIZE_MASK , &addr_range, unit_size); +FIELD_MODIFY (RMI_ADDR_RANGE_COUNT_MASK , &addr_range, count); +FIELD_MODIFY (RMI_ADDR_RANGE_STATE_MASK , &addr_range, state); + + sro->addr_list[sro->addr_count] = addr_range; + +out: +rmi_op_mem_donate (sro_handle, +virt_to_phys (&sro->addr_list[sro->addr_count]),1 , +0 , out_regs); + +unsigned long donated_granules = out_regs->a1; +unsigned long donated_size = donated_granules <<PAGE_SHIFT ; + +if (donated_granules ==0 ) { +/* No pages used by the RMM */ + sro->addr_count++; + }else if (donated_size < size) { + phys = sro->addr_list[sro->addr_count] &RMI_ADDR_RANGE_ADDR_MASK ; + +/* Not all granules used by the RMM, free the remaining pages */ +for (long i = donated_size; i < size; i +=PAGE_SIZE ) { +if (state ==RMI_OP_MEM_DELEGATED ) +free_delegated_page (phys + i); +else +__free_page (phys_to_page (phys + i)); + } + } + +return 0 ; +} + +static int rmi_sro_donate_noncontig (struct rmi_sro_state *sro, +unsigned long sro_handle, +unsigned long donatereq, +struct arm_smccc_1_2_regs *out_regs, +gfp_t gfp) +{ +unsigned long unit_size =RMI_DONATE_SIZE (donatereq); +unsigned long unit_size_bytes =donate_req_to_size (donatereq); +unsigned long count =RMI_DONATE_COUNT (donatereq); +unsigned long state =RMI_DONATE_STATE (donatereq); +unsigned long found =0 ; +unsigned long addr_list_start = sro->addr_count; +int ret; + +for (int i =0 ; i < addr_list_start && found < count; i++) { +unsigned long entry = sro->addr_list[i]; + +if (RMI_ADDR_RANGE_SIZE (entry) == unit_size && +RMI_ADDR_RANGE_COUNT (entry) ==1 && +RMI_ADDR_RANGE_STATE (entry) == state) { + addr_list_start--; +swap (sro->addr_list[addr_list_start], + sro->addr_list[i]); + found++; + i--; + } + } + + ret =rmi_sro_ensure_capacity (sro, count - found); +if (ret) +return ret; + +while (found < count) { +unsigned long addr_range; +void *virt =alloc_pages_exact (unit_size_bytes, gfp); +phys_addr_t phys; + +if (!virt) +return -ENOMEM ; + + phys =virt_to_phys (virt); + +if (state ==RMI_OP_MEM_DELEGATED ) { +phys_addr_t delegated_phys; + +if (rmi_delegate_range (phys, unit_size_bytes, + &delegated_phys)) { +if (!rmi_undelegate_range (phys, delegated_phys - phys)) +free_pages_exact (virt, unit_size_bytes); +return -ENXIO ; + } + } + + addr_range = phys &RMI_ADDR_RANGE_ADDR_MASK ; +FIELD_MODIFY (RMI_ADDR_RANGE_SIZE_MASK , &addr_range, unit_size); +FIELD_MODIFY (RMI_ADDR_RANGE_COUNT_MASK , &addr_range,1 ); +FIELD_MODIFY (RMI_ADDR_RANGE_STATE_MASK , &addr_range, state); + + sro->addr_list[sro->addr_count++] = addr_range; + found++; + } + +rmi_op_mem_donate (sro_handle, +virt_to_phys (&sro->addr_list[addr_list_start]), + found,0 , out_regs); + +unsigned long donated_granules = out_regs->a1; +unsigned long granules_per_unit = unit_size_bytes >>PAGE_SHIFT ; +unsigned long consumed_units; + +/* +* The RMM shouldn't report more granules than we provided, but clamp +* just in case. +*/ +if (WARN_ON_ONCE (donated_granules > found * granules_per_unit)) + donated_granules = found * granules_per_unit; + +/* +* The RMM reports the consumed memory in terms of granules, but we +* track in the address lists in unit-sized ranges. So divide to get +* the number of (complete) consumed units. +*/ + consumed_units = donated_granules / granules_per_unit; +if (donated_granules % granules_per_unit) { +/* +* A unit has been partially consumed, the start is owned by +* the RMM, the tail is owned by the host +*/ +unsigned long entry = + sro->addr_list[addr_list_start + consumed_units]; +phys_addr_t phys =RMI_ADDR_RANGE_ADDR (entry); +unsigned long donated_size = + (donated_granules % granules_per_unit) <<PAGE_SHIFT ; + +/* Free the tail back */ +for (unsigned long i = donated_size; i < unit_size_bytes; + i +=PAGE_SIZE ) { +if (state ==RMI_OP_MEM_DELEGATED ) +free_delegated_page (phys + i); +else +__free_page (phys_to_page (phys + i)); + } + +/* +* This unit is now fully 'consumed' (either held by the RMM or +* freed) +*/ + consumed_units++; + } + +/* Keep just the units the RMM didn't use in addr_list */ +for (unsigned long i = consumed_units; i < found; i++) + sro->addr_list[addr_list_start + i - consumed_units] = + sro->addr_list[addr_list_start + i]; + + sro->addr_count -= consumed_units; + +return 0 ; +} + +static int rmi_sro_donate (struct rmi_sro_state *sro, +unsigned long sro_handle, +unsigned long donatereq, +struct arm_smccc_1_2_regs *regs, +gfp_t gfp) +{ +if (WARN_ON_ONCE (!RMI_DONATE_COUNT (donatereq))) +return -EINVAL ; + +if (RMI_DONATE_CONTIG (donatereq)) { +return rmi_sro_donate_contig (sro, sro_handle, donatereq, + regs, gfp); + }else { +return rmi_sro_donate_noncontig (sro, sro_handle, donatereq, + regs, gfp); + } +} + +static int rmi_sro_reclaim (struct rmi_sro_state *sro, +unsigned long sro_handle, +struct arm_smccc_1_2_regs *out_regs) +{ +unsigned long capacity; +int ret; + + ret =rmi_sro_ensure_capacity (sro,1 ); +if (ret) +rmi_sro_free (sro); + + capacity =RMI_MAX_ADDR_LIST - sro->addr_count; + +rmi_op_mem_reclaim (sro_handle, +virt_to_phys (&sro->addr_list[sro->addr_count]), + capacity, out_regs); + +if (WARN_ON_ONCE (out_regs->a1 > capacity)) + out_regs->a1 = capacity; + + sro->addr_count += out_regs->a1; + +return 0 ; +} + +void rmi_sro_free (struct rmi_sro_state *sro) +{ +for (int i =0 ; i < sro->addr_count; i++) { +unsigned long entry = sro->addr_list[i]; +unsigned long addr =RMI_ADDR_RANGE_ADDR (entry); +unsigned long unit_size =RMI_ADDR_RANGE_SIZE (entry); +unsigned long count =RMI_ADDR_RANGE_COUNT (entry); +unsigned long state =RMI_ADDR_RANGE_STATE (entry); +unsigned long size =donate_req_to_size (unit_size) * count; + +if (state ==RMI_OP_MEM_DELEGATED ) { +if (WARN_ON_ONCE (rmi_undelegate_range (addr, size))) { +/* Leak the pages */ +continue ; + } + } +free_pages_exact (phys_to_virt (addr), size); + } + + sro->addr_count =0 ; +} +EXPORT_SYMBOL_GPL (rmi_sro_free); + +long rmi_sro_memxfer_execute (struct rmi_sro_state *sro,gfp_t gfp) +{ +unsigned long sro_handle; +struct arm_smccc_1_2_regs *regs = &sro->regs; +bool cancelled = false; + +rmi_smccc_invoke (regs, regs); + + sro_handle = regs->a1; + +while (RMI_RETURN_STATUS (regs->a0) ==RMI_INCOMPLETE ) { +bool can_cancel =RMI_RETURN_CAN_CANCEL (regs->a0); +int ret =0 ; + +switch (RMI_RETURN_MEMREQ (regs->a0)) { +case RMI_OP_MEM_REQ_NONE : +rmi_op_continue (sro_handle,RMI_CONTINUE_KEEP_GOING , + regs); +break ; +case RMI_OP_MEM_REQ_DONATE : + ret =rmi_sro_donate (sro, sro_handle, regs->a2, regs, + gfp); +break ; +case RMI_OP_MEM_REQ_RECLAIM : + ret =rmi_sro_reclaim (sro, sro_handle, regs); +break ; +default : + ret =WARN_ON_ONCE (1 ); +break ; + } + +if (ret) { +if (can_cancel) { +rmi_op_cancel (sro_handle, regs); + cancelled = true; + } + +if (WARN_ON_ONCE (RMI_RETURN_STATUS (regs->a0) !=RMI_INCOMPLETE )) +return ret; + } + } + +if (cancelled) +return -ECANCELED ; + +return regs->a0; +} +EXPORT_SYMBOL_GPL (rmi_sro_memxfer_execute); + +/* For RMI commands that are stateful but not memory-transferring */ +long rmi_sro_execute (struct arm_smccc_1_2_regs *regs) +{ +unsigned long sro_handle; +bool cancelled = false; + +rmi_smccc_invoke (regs, regs); + + sro_handle = regs->a1; + +while (RMI_RETURN_STATUS (regs->a0) ==RMI_INCOMPLETE ) { +bool can_cancel =RMI_RETURN_CAN_CANCEL (regs->a0); + +switch (RMI_RETURN_MEMREQ (regs->a0)) { +case RMI_OP_MEM_REQ_NONE : +rmi_op_continue (sro_handle,RMI_CONTINUE_KEEP_GOING , + regs); +break ; +default : +WARN_ON_ONCE (1 ); +if (!can_cancel) +return regs->a0; + + cancelled = true; +rmi_op_cancel (sro_handle, regs); + } + } + +if (cancelled) +return -ECANCELED ; + +return regs->a0; +} +EXPORT_SYMBOL_GPL (rmi_sro_execute); +static int rmi_check_version (void ) {struct arm_smccc_res res;
@@ -84,6 +581,7 @@ static int rmi_configure(void)long rmi_ret;int ret =0 ;struct rmm_config *config; +struct rmi_sro_state *sro;switch (PAGE_SIZE ) {case SZ_4K :
@@ -112,6 +610,12 @@ static int rmi_configure(void)if (!config)return -ENOMEM ; + sro =kmalloc_obj (*sro); +if (!sro) { + ret = -ENOMEM ; + goto out_free_config; + } + config->rmi_granule_size = granule_size;/*
@@ -126,9 +630,18 @@ static int rmi_configure(void)if (rmi_ret) {pr_err ("RMM config set failed\n" ); ret = -EINVAL ; - goto out_free_config; + goto out_free_sro; + } + + rmi_ret =rmi_rmm_activate (sro); +if (rmi_ret) { +pr_err ("RMM activate failed\n" ); + ret = rmi_ret <0 ? rmi_ret : -ENXIO ; + goto out_free_sro; } +out_free_sro: +kfree (sro); out_free_config:free_page ((unsigned long )config);return ret;
include/linux/arm-rmi-cmds.h +114 -0
138 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v16 6/45 firmware: arm_rmm: Ensure the RMM has GPT entries for memory
In reply to Steven PriceThe RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegate individual pages.
For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.
Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.
---
Changes since v15:
* Skip firmware-reserved NOMAP memory in rmi_init_metadata()
* Handle negative error codes from wrappers.
Changes since v14:
* Move the implementation into drivers/firmware/arm_rmm.
Changes since v13:
* Moved out of KVM
--- drivers/firmware/arm_rmm/rmi.c | 101 +++++++++++++++++++++++++++++++++ include/linux/arm-rmi-cmds.h | 2 + 2 files changed, 103 insertions(+)
drivers/firmware/arm_rmm/rmi.c +101 -0
@@ -12,6 +12,8 @@#include <asm/memory.h> #include <asm/pgtable-hwdef.h> +static bool arm64_rmi_is_available; +/* Currently only the first 2 registers are used by Linux */ #define RMI_FEAT_REG_COUNT 2static __ro_after_init unsigned long rmi_feat_reg_cache[RMI_FEAT_REG_COUNT ];
@@ -647,6 +649,98 @@ static int rmi_configure(void)return ret; } +/* +* Make sure the area is tracked by RMM at FINE granularity. +* We do not support changing the tracking yet. +*/ +static int rmi_verify_memory_tracking (phys_addr_t start,phys_addr_t end) +{ +while (start < end) { +unsigned long ret, category, state, next; + + ret =rmi_granule_tracking_get (start, end, &category, &state, &next); +if (ret !=RMI_SUCCESS || + state !=RMI_TRACKING_FINE || + category !=RMI_MEM_CATEGORY_CONVENTIONAL ) { +/* TODO: Set granule tracking in this case */ +pr_err ("Granule tracking for region isn't fine/conventional: %llx\n" , + start); +return -ENODEV ; + } + start = next; + } + +return 0 ; +} + +static int rmi_create_gpts (phys_addr_t start,phys_addr_t end) +{ +struct rmi_sro_state *sro; +unsigned long l0gpt_sz; + + sro =kmalloc_obj (*sro,GFP_KERNEL ); +if (!sro) +return -ENOMEM ; + + l0gpt_sz =1UL << (30 +FIELD_GET (RMI_FEATURE_REGISTER_1_L0GPTSZ , +rmi_feat_reg (1 ))); + start =ALIGN_DOWN (start, l0gpt_sz); + end =ALIGN (end, l0gpt_sz); + +while (start < end) { +long ret =rmi_gpt_l1_create (start, sro,GFP_KERNEL ); + +/* +* Make sure the L1 GPT tables are created for the region. +* RMI_ERROR_GPT indicates the L1 table already exists. +*/ +if (ret <0 ) { +kfree (sro); +return ret; + } + +if (ret !=RMI_SUCCESS &&RMI_RETURN_STATUS (ret) !=RMI_ERROR_GPT ) { +pr_err ("GPT Level1 table missing for %llx\n" , start); +kfree (sro); +return -ENOMEM ; + } + start += l0gpt_sz; + } + +kfree (sro); +return 0 ; +} + +static int rmi_init_metadata (void ) +{ +phys_addr_t start, end; +struct memblock_region *r; + +for_each_mem_region (r) { +int ret; + +/* Firmware-reserved NOMAP regions are not usable system RAM */ +if (memblock_is_nomap (r)) +continue ; + + start =memblock_region_memory_base_pfn (r) <<PAGE_SHIFT ; + end =memblock_region_memory_end_pfn (r) <<PAGE_SHIFT ; + ret =rmi_verify_memory_tracking (start, end); +if (ret) +return ret; + ret =rmi_create_gpts (start, end); +if (ret) +return ret; + } + +return 0 ; +} + +bool is_rmi_available (void ) +{ +return arm64_rmi_is_available; +} +static int __init arm64_init_rmi (void ) {int ret;
@@ -664,6 +758,13 @@ static int __init arm64_init_rmi(void)if (ret)return ret; + ret =rmi_init_metadata (); +if (ret) +return ret; + + arm64_rmi_is_available = true; +pr_info ("RMI configured\n" ); +return 0 ; }
include/linux/arm-rmi-cmds.h +2 -0
@@ -37,6 +37,8 @@ static inline int rmi_undelegate_page(phys_addr_t phys)return rmi_undelegate_range (phys,PAGE_SIZE ); } +bool is_rmi_available (void ); +long rmi_sro_memxfer_execute (struct rmi_sro_state *sro,gfp_t gfp);void rmi_sro_free (struct rmi_sro_state *sro);long rmi_sro_execute (struct arm_smccc_1_2_regs *regs);
Signature · 1 line
2.43.0
Suzuki K Poulose
reviewerRe: [PATCH v16 06/45] firmware: arm_rmm: Ensure the RMM has GPT entries for memory
Quoted 13 lines · Steven Price
The RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (
TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegate individual pages.For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.
Quoted 4 lines
Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.
This sounds a bit incomplete to me. We could add:
"Again, this series do not support creation of L1 GPT tables yet. Thus make sure that the firmware has L1 GPTs covering the DRAM region."
Quoted 67 lines
Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Skip firmware-reserved NOMAP memory inrmi_init_metadata()
* Handle negative error codes from wrappers.
Changes since v14:
* Move the implementation intodrivers/firmware/arm_rmm.
Changes since v13:
* Moved out of KVM--- drivers/firmware/arm_rmm/rmi.c | 101 +++++++++++++++++++++++++++++++++ include/linux/arm-rmi-cmds.h | 2 + 2 files changed, 103 insertions(+)drivers/firmware/arm_rmm/rmi.c +37
-0@@ -12,6 +12,8 @@#include <asm/memory.h> #include <asm/pgtable-hwdef.h> +static bool arm64_rmi_is_available; +/* Currently only the first 2 registers are used by Linux */ #define RMI_FEAT_REG_COUNT 2static __ro_after_init unsigned long rmi_feat_reg_cache[RMI_FEAT_REG_COUNT ];@@ -647,6 +649,98 @@ static int rmi_configure(void)return ret; } +/* +* Make sure the area is tracked by RMM at FINE granularity. +* We do not support changing the tracking yet. +*/ +static int rmi_verify_memory_tracking (phys_addr_t start,phys_addr_t end) +{ +while (start < end) { +unsigned long ret, category, state, next; + + ret =rmi_granule_tracking_get (start, end, &category, &state, &next); +if (ret !=RMI_SUCCESS || + state !=RMI_TRACKING_FINE || + category !=RMI_MEM_CATEGORY_CONVENTIONAL ) { +/* TODO: Set granule tracking in this case */ +pr_err ("Granule tracking for region isn't fine/conventional: %llx\n" , + start); +return -ENODEV ; + } + start = next; + } + +return 0 ; +} + +static int rmi_create_gpts (phys_addr_t start,phys_addr_t end) +{ +struct rmi_sro_state *sro; +unsigned long l0gpt_sz; + + sro =kmalloc_obj (*sro,GFP_KERNEL ); +if (!sro) +return -ENOMEM ; + + l0gpt_sz =1UL << (30 +FIELD_GET (RMI_FEATURE_REGISTER_1_L0GPTSZ , +rmi_feat_reg (1 )));
drivers/firmware/arm_rmm/rmi.c static int rmi_configure(void) · line 696
Quoted 10 lines
drivers/firmware/arm_rmm/rmi.c +10
-0+ start =ALIGN_DOWN (start, l0gpt_sz); + end =ALIGN (end, l0gpt_sz); + +while (start < end) { +long ret =rmi_gpt_l1_create (start, sro,GFP_KERNEL ); + +/* +* Make sure the L1 GPT tables are created for the region. +* RMI_ERROR_GPT indicates the L1 table already exists. +*/
minor nit: The comment could be moved down closer to the check.
drivers/firmware/arm_rmm/rmi.c static int rmi_configure(void) · line 709
Quoted 13 lines
drivers/firmware/arm_rmm/rmi.c +13
-0+if (ret <0 ) { +kfree (sro); +return ret; + } + +if (ret !=RMI_SUCCESS &&RMI_RETURN_STATUS (ret) !=RMI_ERROR_GPT ) { +pr_err ("GPT Level1 table missing for %llx\n" , start); +kfree (sro); +return -ENOMEM ; + } + start += l0gpt_sz; + } +
Rest looks good to me
Suzuki
PATCH v16 7/45 arm64: mm: Handle Granule Protection Faults (GPFs)
In reply to Steven PriceIf 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 v10:
* Don't call arm64_notify_die() in do_gpf() but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"
--- arch/arm64/mm/fault.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
arch/arm64/mm/fault.c +22 -6
@@ -909,6 +909,22 @@ 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) +{ +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; + +return 1 ; +} +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" },
@@ -945,12 +961,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, "level -1 granule protection fault (translation table walk)" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "level 0 granule protection fault (translation table walk)" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "level 1 granule protection fault (translation table walk)" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "level 2 granule protection fault (translation table walk)" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "level 3 granule protection fault (translation table walk)" }, + { do_gpf, SIGBUS, SI_KERNEL, "granule protection fault" }, { 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
Catalin Marinas
maintainerRe: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 35 lines · 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.
Reviewed-by Suzuki K Poulose <[email protected]>Reviewed-by Gavin Shan <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v10:
* Don't callarm64_notify_die()indo_gpf()but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"--- arch/arm64/mm/fault.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)arch/arm64/mm/fault.c +5
-0@@ -909,6 +909,22 @@ 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);
arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 926
Quoted 10 lines
arch/arm64/mm/fault.c +10
-0+return 0 ; +} + +static int do_gpf (unsigned long far,unsigned long esr,struct pt_regs *regs) +{ +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; + +return 1 ; +}
Is there a valid case for fixup_exception() here? IOW, do we ever have a valid user mapping of the pages delegated to a guest? If the above is considered a kernel bug, I'd not silently ignore this (like return less bytes copied or -EFAULT to user) but rather warn, potentially rate-limited.
If there is a real use-case for this, what's preventing GUP + memcpy() from triggering a similar fault with no fixup available?
Signature · 1 line
Catalin
Suzuki K Poulose
reviewerRe: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 44 lines · Catalin Marinas
Quoted 43 lines · 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.
Reviewed-by Suzuki K Poulose <[email protected]>Reviewed-by Gavin Shan <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v10:
* Don't callarm64_notify_die()indo_gpf()but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"--- arch/arm64/mm/fault.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)arch/arm64/mm/fault.c +13
-0@@ -909,6 +909,22 @@ 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) +{ +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; +
Quoted 8 lines
arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 926
Quoted 2 lines
arch/arm64/mm/fault.c +2
-0+return 1 ; +}Is there a valid case for
fixup_exception()here? IOW, do we ever have a valid user mapping of the pages delegated to a guest? If the above is considered a kernel bug, I'd not silently ignore this (like return less bytes copied or-EFAULTto user) but rather warn, potentially rate-limited.
Good question. This shouldn't be a valid case. We expect the VMM to use guest_memfd and that should prevent any mmaps and thus fixups shouldn't be required. That said, this series doesn't enforce that the VMM uses GMEM backed memslots for Guest RAM. We should probably do that while mapping things in.
With that, we could drop that fixup and scream a bit.
Cheers
Suzuki
Quoted 4 lines
If there is a real use-case for this, what's preventing GUP +
memcpy()from triggering a similar fault with no fixup available?
Re: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 53 lines · Suzuki K Poulose
Quoted 43 lines · Catalin Marinas
Quoted 42 lines · 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.
Reviewed-by Suzuki K Poulose <[email protected]>Reviewed-by Gavin Shan <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v10:
* Don't callarm64_notify_die()indo_gpf()but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"--- arch/arm64/mm/fault.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)arch/arm64/mm/fault.c +13
-0@@ -909,6 +909,22 @@ 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) +{ +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; +Quoted 8 lines
arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 926
Quoted 2 lines
arch/arm64/mm/fault.c +2
-0+return 1 ; +}Is there a valid case for
fixup_exception()here? IOW, do we ever have a valid user mapping of the pages delegated to a guest? If the above is considered a kernel bug, I'd not silently ignore this (like return less bytes copied or-EFAULTto user) but rather warn, potentially rate-limited.
Quoted 8 lines
Good question. This shouldn't be a valid case. We expect the VMM to use
guest_memfdand that should prevent any mmaps and thus fixups shouldn't be required. That said, this series doesn't enforce that the VMM uses GMEM backed memslots for Guest RAM. We should probably do that while mapping things in.With that, we could drop that fixup and scream a bit.
There is a valid case for fixup_exception() to be needed here in GPF handling.
-000 |load_unaligned_zeropad(inline) -000 |hash_name(inline) -000 |link_path_walk() -001 |path_lookupat() -002 |filename_lookup() -003 |vfs_statx() -004 |vfs_fstatat()
We observed this in Android running Gunyah when the page is mapped in EL1 but unmapped at EL2. path_lookupat() can actually handle this via fixup_exeption() when a word load crosses the page boundary. However, Gunyah injects a Synchronous External Abort and we have a downstream patch [1] that adds fixup_exception() in do_sea(). pKVM injects [2] such faults back to EL1 and fixup_exception() is taken care.
This patch allows Hypervisors like Gunyah to simulate GPF instead of Synchronous External Abort for kernel to gracefully handle the load_unaligned_zeropad() access.
Thanks,
Pavan
[1] https://android-review.googlesource.com/c/kernel/common/+/2996781
[2] https://lore.kernel.org/all/[email protected]/
Catalin Marinas
maintainerRe: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 73 lines · Pavan Kondeti
Quoted 53 lines · Suzuki K Poulose
Quoted 43 lines · Catalin Marinas
Quoted 42 lines · 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.
Reviewed-by Suzuki K Poulose <[email protected]>Reviewed-by Gavin Shan <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v10:
* Don't callarm64_notify_die()indo_gpf()but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"--- arch/arm64/mm/fault.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)arch/arm64/mm/fault.c +13
-0@@ -909,6 +909,22 @@ 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) +{ +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; +Quoted 8 lines
arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 926
Quoted 2 lines
arch/arm64/mm/fault.c +2
-0+return 1 ; +}Is there a valid case for
fixup_exception()here? IOW, do we ever have a valid user mapping of the pages delegated to a guest? If the above is considered a kernel bug, I'd not silently ignore this (like return less bytes copied or-EFAULTto user) but rather warn, potentially rate-limited.Quoted 7 lines
Good question. This shouldn't be a valid case. We expect the VMM to use
guest_memfdand that should prevent any mmaps and thus fixups shouldn't be required. That said, this series doesn't enforce that the VMM uses GMEM backed memslots for Guest RAM. We should probably do that while mapping things in.With that, we could drop that fixup and scream a bit.
There is a valid case for
fixup_exception()to be needed here in GPF handling.-000 |load_unaligned_zeropad(inline) -000 |hash_name(inline) -000 |link_path_walk() -001 |path_lookupat() -002 |filename_lookup() -003 |vfs_statx() -004 |vfs_fstatat()
Quoted 6 lines
We observed this in Android running Gunyah when the page is mapped in EL1 but unmapped at EL2.
path_lookupat()can actually handle this viafixup_exeption()when a word load crosses the page boundary. However, Gunyah injects a Synchronous External Abort and we have a downstream patch [1] that addsfixup_exception()indo_sea(). pKVM injects [2] such faults back to EL1 andfixup_exception()is taken care.
Ah, good point, completely forgot about load_unaligned_zeropad(). Since we don't unmap the linear map for delegated pages, we'll need the fixup_exception(). And I guess warning in this case is not desirable either. We could limit it to EX_TYPE_KACCESS_ERR_ZERO and EX_TYPE_LOAD_UNALIGNED_ZEROPAD, though not sure it's worth it.
Signature · 1 line
Catalin
Will Deacon
maintainerRe: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 19 lines · Catalin Marinas
Quoted 11 lines · Pavan Kondeti
There is a valid case for
fixup_exception()to be needed here in GPF handling.-000 |load_unaligned_zeropad(inline) -000 |hash_name(inline) -000 |link_path_walk() -001 |path_lookupat() -002 |filename_lookup() -003 |vfs_statx() -004 |vfs_fstatat()Quoted 6 lines
We observed this in Android running Gunyah when the page is mapped in EL1 but unmapped at EL2.
path_lookupat()can actually handle this viafixup_exeption()when a word load crosses the page boundary. However, Gunyah injects a Synchronous External Abort and we have a downstream patch [1] that addsfixup_exception()indo_sea(). pKVM injects [2] such faults back to EL1 andfixup_exception()is taken care.
Quoted 5 lines
Ah, good point, completely forgot about
load_unaligned_zeropad(). Since we don't unmap the linear map for delegated pages, we'll need thefixup_exception(). And I guess warning in this case is not desirable either. We could limit it toEX_TYPE_KACCESS_ERR_ZEROandEX_TYPE_LOAD_UNALIGNED_ZEROPAD, though not sure it's worth it.
Alternatively, I think the series to unmap guest memory from the linear map would solve that for gmem:
https://lore.kernel.org/all/[email protected]/
but I can't tell whether that's still being actively pursued.
Will
Re: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 26 lines · Will Deacon
Quoted 19 lines · Catalin Marinas
Quoted 11 lines · Pavan Kondeti
There is a valid case for
fixup_exception()to be needed here in GPF handling.-000 |load_unaligned_zeropad(inline) -000 |hash_name(inline) -000 |link_path_walk() -001 |path_lookupat() -002 |filename_lookup() -003 |vfs_statx() -004 |vfs_fstatat()Quoted 6 lines
We observed this in Android running Gunyah when the page is mapped in EL1 but unmapped at EL2.
path_lookupat()can actually handle this viafixup_exeption()when a word load crosses the page boundary. However, Gunyah injects a Synchronous External Abort and we have a downstream patch [1] that addsfixup_exception()indo_sea(). pKVM injects [2] such faults back to EL1 andfixup_exception()is taken care.Quoted 5 lines
Ah, good point, completely forgot about
load_unaligned_zeropad(). Since we don't unmap the linear map for delegated pages, we'll need thefixup_exception(). And I guess warning in this case is not desirable either. We could limit it toEX_TYPE_KACCESS_ERR_ZEROandEX_TYPE_LOAD_UNALIGNED_ZEROPAD, though not sure it's worth it.
Quoted 5 lines
Alternatively, I think the series to unmap guest memory from the linear map would solve that for gmem:
Thanks Will for sharing this information.
There are use cases outside gmem like FF-A lend to Secure Partition. we may not be enforcing all such memory to be unmapped at EL1, correct?
Thanks,
Pavan
Will Deacon
maintainerRe: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 30 lines · Pavan Kondeti
Quoted 29 lines · Will Deacon
Quoted 19 lines · Catalin Marinas
Quoted 11 lines · Pavan Kondeti
There is a valid case for
fixup_exception()to be needed here in GPF handling.-000 |load_unaligned_zeropad(inline) -000 |hash_name(inline) -000 |link_path_walk() -001 |path_lookupat() -002 |filename_lookup() -003 |vfs_statx() -004 |vfs_fstatat()Quoted 6 lines
We observed this in Android running Gunyah when the page is mapped in EL1 but unmapped at EL2.
path_lookupat()can actually handle this viafixup_exeption()when a word load crosses the page boundary. However, Gunyah injects a Synchronous External Abort and we have a downstream patch [1] that addsfixup_exception()indo_sea(). pKVM injects [2] such faults back to EL1 andfixup_exception()is taken care.Quoted 5 lines
Ah, good point, completely forgot about
load_unaligned_zeropad(). Since we don't unmap the linear map for delegated pages, we'll need thefixup_exception(). And I guess warning in this case is not desirable either. We could limit it toEX_TYPE_KACCESS_ERR_ZEROandEX_TYPE_LOAD_UNALIGNED_ZEROPAD, though not sure it's worth it.Alternatively, I think the series to unmap guest memory from the linear map would solve that for gmem:
Quoted 7 lines
Quoted 2 lines
Thanks Will for sharing this information.
There are use cases outside gmem like FF-A lend to Secure Partition. we may not be enforcing all such memory to be unmapped at EL1, correct?
If you leave the cacheable linear alias of memory intact across an NS -> S transition, then you're in for a bad time [1].
Will
Re: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 39 lines · Will Deacon
Quoted 30 lines · Pavan Kondeti
Quoted 29 lines · Will Deacon
Quoted 19 lines · Catalin Marinas
Quoted 11 lines · Pavan Kondeti
There is a valid case for
fixup_exception()to be needed here in GPF handling.-000 |load_unaligned_zeropad(inline) -000 |hash_name(inline) -000 |link_path_walk() -001 |path_lookupat() -002 |filename_lookup() -003 |vfs_statx() -004 |vfs_fstatat()Quoted 6 lines
We observed this in Android running Gunyah when the page is mapped in EL1 but unmapped at EL2.
path_lookupat()can actually handle this viafixup_exeption()when a word load crosses the page boundary. However, Gunyah injects a Synchronous External Abort and we have a downstream patch [1] that addsfixup_exception()indo_sea(). pKVM injects [2] such faults back to EL1 andfixup_exception()is taken care.Quoted 5 lines
Ah, good point, completely forgot about
load_unaligned_zeropad(). Since we don't unmap the linear map for delegated pages, we'll need thefixup_exception(). And I guess warning in this case is not desirable either. We could limit it toEX_TYPE_KACCESS_ERR_ZEROandEX_TYPE_LOAD_UNALIGNED_ZEROPAD, though not sure it's worth it.Alternatively, I think the series to unmap guest memory from the linear map would solve that for gmem:
Quoted 7 lines
Quoted 2 lines
Thanks Will for sharing this information.
There are use cases outside gmem like FF-A lend to Secure Partition. we may not be enforcing all such memory to be unmapped at EL1, correct?
Quoted 6 lines
If you leave the cacheable linear alias of memory intact across an NS -> S transition, then you're in for a bad time [1].
Will
Thanks for the reply. In the above case, XPU does not like the speculative fetches. I am not sure if there is a hard requirement for unmap of FF_A memory lent to secure partition managed under Root with GPT.
Thanks,
Pavan
Will Deacon
maintainerRe: [PATCH v16 07/45] arm64: mm: Handle Granule Protection Faults (GPFs)
Quoted 43 lines · Pavan Kondeti
Quoted 42 lines · Will Deacon
Quoted 30 lines · Pavan Kondeti
Quoted 29 lines · Will Deacon
Quoted 19 lines · Catalin Marinas
Quoted 11 lines · Pavan Kondeti
There is a valid case for
fixup_exception()to be needed here in GPF handling.-000 |load_unaligned_zeropad(inline) -000 |hash_name(inline) -000 |link_path_walk() -001 |path_lookupat() -002 |filename_lookup() -003 |vfs_statx() -004 |vfs_fstatat()Quoted 6 lines
We observed this in Android running Gunyah when the page is mapped in EL1 but unmapped at EL2.
path_lookupat()can actually handle this viafixup_exeption()when a word load crosses the page boundary. However, Gunyah injects a Synchronous External Abort and we have a downstream patch [1] that addsfixup_exception()indo_sea(). pKVM injects [2] such faults back to EL1 andfixup_exception()is taken care.Quoted 5 lines
Ah, good point, completely forgot about
load_unaligned_zeropad(). Since we don't unmap the linear map for delegated pages, we'll need thefixup_exception(). And I guess warning in this case is not desirable either. We could limit it toEX_TYPE_KACCESS_ERR_ZEROandEX_TYPE_LOAD_UNALIGNED_ZEROPAD, though not sure it's worth it.Alternatively, I think the series to unmap guest memory from the linear map would solve that for gmem:
Quoted 7 lines
Quoted 2 lines
Thanks Will for sharing this information.
There are use cases outside gmem like FF-A lend to Secure Partition. we may not be enforcing all such memory to be unmapped at EL1, correct?
If you leave the cacheable linear alias of memory intact across an NS -> S transition, then you're in for a bad time [1].
Quoted 7 lines
Quoted 3 lines
Will
Thanks for the reply. In the above case, XPU does not like the speculative fetches. I am not sure if there is a hard requirement for unmap of
FF_Amemory lent to secure partition managed under Root with GPT.
If it's not unmapped, then you can get speculative reads on the NS side which will probably trigger aborts at the TZ memory firewall logic. I agree that's different for the GPT, but gmem is growing that support for other architectures and it would solve the load_unaligned_zeropad() problem on arm64. Some folks also seem to want it for addressing the possibility of CPU side-channels, but that's a bit more belt and braces imo.
Vincent looped you in on the thread with Thierry where there is an ongoing effort to address this for TZ.
Will
PATCH v16 8/45 KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h
In reply to Steven PriceFrom: 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
@@ -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
PATCH v16 9/45 KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
In reply to Steven PriceTo avoid future include cycles, drop the linux/kvm_host.h include in kvm_pgtable.h and include the lightweight headers required for the types and inline helpers used there. Additionally provide a forward declaration for struct kvm_s2_mmu as it's only used as a pointer in this file.
Both pgtable.c and kvm_pkvm.h relied on the indirect inclusion of kvm_host.h, so make that explicit.
---
New patch in v13
--- arch/arm64/include/asm/kvm_pgtable.h | 6 +++++- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kvm/hyp/pgtable.c | 1 + 3 files changed, 7 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +5 -1
@@ -8,9 +8,13 @@#define __ARM64_KVM_PGTABLE_H__#include <linux/bits.h> -#include <linux/kvm_host.h> +#include <linux/kvm_types.h> +#include <linux/rbtree_types.h> +#include <linux/rcupdate.h> #include <linux/types.h> +struct kvm_s2_mmu ; +#define KVM_PGTABLE_FIRST_LEVEL -1#define KVM_PGTABLE_LAST_LEVEL 3
arch/arm64/include/asm/kvm_pkvm.h +1 -1
@@ -7,9 +7,9 @@#define __ARM64_KVM_PKVM_H__#include <linux/arm_ffa.h> +#include <linux/kvm_host.h> #include <linux/memblock.h> #include <linux/scatterlist.h> -#include <asm/kvm_host.h> #include <asm/kvm_pgtable.h> /* Maximum number of VMs that can co-exist under pKVM. */
arch/arm64/kvm/hyp/pgtable.c +1 -0
@@ -8,6 +8,7 @@*/ #include <linux/bitfield.h> +#include <linux/kvm_host.h> #include <asm/kvm_pgtable.h> #include <asm/stage2_pgtable.h>
Signature · 1 line
2.43.0
PATCH v16 10/45 KVM: arm64: CCA: Add wrappers for realm related RMIs
In reply to Steven PriceIntroduce wrappers for the RMI functions needed for creating and managing realm guests.
---
v16: Split into new patch
--- arch/arm64/include/asm/rmi_cmds.h | 465 ++++++++++++++++++++++++++++++ 1 file changed, 465 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_cmds.h
arch/arm64/include/asm/rmi_cmds.h new file +465 -0
@@ -0,0 +1,465 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023-2026 ARM Ltd. +*/ + +#ifndef __ASM_RMI_CMDS_H +#define __ASM_RMI_CMDS_H + +#include <linux/arm-rmi-cmds.h> +#include <linux/arm-smccc-rmi.h> + +/** +* rmi_rtt_data_map_init() - Create a protected mapping with data contents +* @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 mapping from Protected IPA space to conventional memory, copying +* contents from a Non-secure Granule provided by the caller. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_data_map_init (unsigned long rd,unsigned long data, +unsigned long ipa,unsigned long src, +unsigned long flags) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_DATA_MAP_INIT , rd, data, ipa, src, flags + }; + +return rmi_sro_execute (®s); +} + +/** +* rmi_rtt_data_map() - Create mappings in protected IPA with unknown contents +* @rd: PA of the RD +* @base: Base of the target IPA range +* @top: Top of the target IPA range +* @flags: Flags +* @oaddr: Output address set descriptor +* @out_top: Top address of range which was processed. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_data_map (unsigned long rd, +unsigned long base, +unsigned long top, +unsigned long flags, +unsigned long oaddr, +unsigned long *out_top) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_DATA_MAP , rd, base, top, flags, oaddr + }; +long ret; + + ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS && out_top) + *out_top = regs.a1; + +return ret; +} + +/** +* rmi_rtt_data_unmap() - Remove mappings to conventional memory +* @rd: PA of the RD for the target Realm +* @base: Base of the target IPA range +* @top: Top of the target IPA range +* @flags: Flags +* @oaddr: Output address set descriptor +* @out_top: Returns top IPA of range which has been unmapped +* @out_range: Output address range +* @out_count: Number of entries in output address list +* +* Removes mappings to convention memory with a target Protected IPA range. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_data_unmap (unsigned long rd, +unsigned long base, +unsigned long top, +unsigned long flags, +unsigned long oaddr, +unsigned long *out_top, +unsigned long *out_range, +unsigned long *out_count) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_DATA_UNMAP , rd, base, top, flags, oaddr + }; +long ret; + + ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS ) { +if (out_top) + *out_top = regs.a1; +if (out_range) + *out_range = regs.a2; +if (out_count) + *out_count = regs.a3; + } + +return ret; +} + +/** +* rmi_psci_complete() - Complete pending PSCI command +* @calling_rec: PA of the calling REC +* @status: Status of the PSCI request +* +* Completes a pending PSCI command. +* +* Return: RMI return code +*/ +static inline long rmi_psci_complete (unsigned long calling_rec, +unsigned long status) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_PSCI_COMPLETE , calling_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 long 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 +* @sro: Preallocated SRO context to be used +* +* Create a new realm using the given parameters. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_realm_create (unsigned long rd,unsigned long params, +struct rmi_sro_state *sro) +{ +return rmi_sro_memxfer_cmd (sro,GFP_KERNEL , +SMC_RMI_REALM_CREATE , rd, params); +} + +/** +* rmi_realm_terminate() - Terminate a realm +* @rd: PA of the RD +* @sro: Preallocated SRO context to be used +* +* Terminates a realm, moving it into a ZOMBIE state +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_realm_terminate (unsigned long rd, +struct rmi_sro_state *sro) +{ +return rmi_sro_memxfer_cmd (sro,GFP_KERNEL , +SMC_RMI_REALM_TERMINATE , rd); +} + +/** +* rmi_realm_destroy() - Destroy a realm +* @rd: PA of the RD +* @sro: Preallocated SRO context to be used +* +* Destroys a realm, all objects belonging to the realm must be destroyed first. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_realm_destroy (unsigned long rd, +struct rmi_sro_state *sro) +{ +return rmi_sro_memxfer_cmd (sro,GFP_KERNEL , +SMC_RMI_REALM_DESTROY , rd); +} + +/** +* rmi_rec_create() - Create a REC +* @rd: PA of the RD +* @rec: PA of the target REC +* @params: PA of REC parameters +* @sro: Allocated SRO context to be used +* +* Create a REC using the parameters specified in the struct rec_params pointed +* to by @params. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rec_create (unsigned long rd, +unsigned long rec, +unsigned long params, +struct rmi_sro_state *sro) +{ +long ret; + + *sro = (struct rmi_sro_state ){.regs = { +SMC_RMI_REC_CREATE , rd, rec, params + }}; + ret =rmi_sro_memxfer_execute (sro,GFP_KERNEL ); +rmi_sro_free (sro); + +return ret; +} + +/** +* rmi_rec_destroy() - Destroy a REC +* @rec: PA of the target REC +* @sro: Allocated SRO context to be used +* +* Destroys a REC. The REC must not be running. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rec_destroy (unsigned long rec, +struct rmi_sro_state *sro) +{ +return rmi_sro_memxfer_cmd (sro,GFP_KERNEL ,SMC_RMI_REC_DESTROY , rec); +} + +/** +* 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 long 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: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_create (unsigned long rd,unsigned long rtt, +unsigned long ipa,long level) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_CREATE , rd, rtt, ipa, level + }; + +return rmi_sro_execute (®s); +} + +/** +* rmi_rtt_destroy() - Destroy an RTT +* @rd: PA of the RD for the target realm +* @ipa: Base of the IPA range described by the RTT +* @level: RTT level +* @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, from entry +* at which the RTT walk terminated. +* +* 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: 0 on success, positive RMI result code or negative Linux error code. +*/ +static inline long rmi_rtt_destroy (unsigned long rd, +unsigned long ipa, +long level, +unsigned long *out_rtt, +unsigned long *out_top) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_DESTROY , rd, ipa, level + }; +long ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS ) { +if (out_rtt) + *out_rtt = regs.a1; +if (out_top) + *out_top = regs.a2; + } + +return ret; +} + +/** +* 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: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_fold (unsigned long rd,unsigned long ipa, +long level,unsigned long *out_rtt) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_FOLD , rd, ipa, level + }; +long ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS && out_rtt) + *out_rtt = regs.a1; + +return ret; +} + +/** +* 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: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_init_ripas (unsigned long rd,unsigned long base, +unsigned long top,unsigned long *out_top) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_INIT_RIPAS , rd, base, top + }; +long ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS && out_top) + *out_top = regs.a1; + +return ret; +} + +/** +* rmi_rtt_unprot_map() - Map unprotected granules into a realm +* @rd: PA of the RD +* @base: Base IPA of the mapping +* @top: Top of the target IPA range +* @flags: Flags +* @oaddr: Output address set descriptor +* @out_top: Top IPA of range which has been mapped +* +* Create mappings to memory within a target unprotected IPA range. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_unprot_map (unsigned long rd, +unsigned long base, +unsigned long top, +unsigned long flags, +unsigned long oaddr, +unsigned long *out_top) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_UNPROT_MAP , rd, base, top, flags, oaddr + }; +long ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS && out_top) + *out_top = regs.a1; + +return ret; +} + +/** +* 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: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_set_ripas (unsigned long rd,unsigned long rec, +unsigned long base,unsigned long top, +unsigned long *out_top) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_SET_RIPAS , rd, rec, base, top + }; +long ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS && out_top) + *out_top = regs.a1; + +return ret; +} + +/** +* rmi_rtt_unprot_unmap() - Remove mappings within an unprotected IPA range +* @rd: PA of the RD +* @base: Base IPA of the mapping +* @top: Top of the target IPA range +* @flags: Flags +* @oaddr: Output address set descriptor +* @out_top: Top IPA which has been unmapped +* @out_range: Output address range +* @out_count: Number of entries in output address list +* +* Removes mappings to memory within a target unprotected IPA range. +* +* Return: 0 on success, positive RMI result code or negative Linux error code +*/ +static inline long rmi_rtt_unprot_unmap (unsigned long rd, +unsigned long base, +unsigned long top, +unsigned long flags, +unsigned long oaddr, +unsigned long *out_top, +unsigned long *out_range, +unsigned long *out_count) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_UNPROT_UNMAP , rd, base, top, flags, oaddr + }; +long ret =rmi_sro_execute (®s); + +if (ret ==RMI_SUCCESS ) { +if (out_top) + *out_top = regs.a1; +if (out_range) + *out_range = regs.a2; +if (out_count) + *out_count = regs.a3; + } + +return ret; +} + +#endif /* __ASM_RMI_CMDS_H */
Signature · 1 line
2.43.0
PATCH v16 11/45 KVM: arm64: CCA: Check for RMI support at KVM init
In reply to Steven PriceCheck if the RMI support is sufficient for using in KVM. Specifically, we currently only support creating realm VMs when KVM is running in VHE mode.
---
Changes since v13:
* Most of the init has been moved out of the 'kvm' directory so this is much more basic now.
Changes since v12:
* Drop check for 4k page size.
Changes since v11:
* Reword slightly the comments on the realm states.
Changes since v10:
* kvm_is_realm() no longer has a NULL check.
* Rename from "rme" to "rmi" when referring to the RMM interface.
* Check for RME (hardware) support before probing for RMI support.
Changes since v8:
* No need to guard kvm_init_rme() behind 'in_hyp_mode'.
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_host.h | 4 ++++ arch/arm64/include/asm/kvm_rmi.h | 17 +++++++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++++ arch/arm64/kvm/rmi.c | 24 ++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_rmi.h create mode 100644 arch/arm64/kvm/rmi.c
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_rmi.h> #include <asm/vncr_mapping.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -424,6 +425,9 @@ struct kvm_arch {/* Nested virtualization info */ struct dentry *debugfs_nv_dentry;#endif + +bool is_realm; +struct realm realm; };struct kvm_vcpu_fault_info {
arch/arm64/include/asm/kvm_rmi.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023-2026 ARM Ltd. +*/ + +#ifndef __ASM_KVM_RMI_H +#define __ASM_KVM_RMI_H + +/** +* struct realm - Additional per VM data for a Realm +*/ +struct realm { +}; + +void kvm_init_rmi (void ); + +#endif /* __ASM_KVM_RMI_H */
arch/arm64/include/asm/virt.h +1 -0
@@ -87,6 +87,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_rmi_is_available);static inline bool is_pkvm_initialized (void ) {
arch/arm64/kvm/Kconfig +1 -0
@@ -37,6 +37,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD + select ARM_RMM help Support hosting virtualized guest machines.
arch/arm64/kvm/Makefile +1 -1
@@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ inject_fault.o va_layout.o handle_exit.o config.o \ guest.o debug.o reset.o sys_regs.o stacktrace.o \ - vgic-sys-reg-v3.o fpsimd.o pkvm.o \ + vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \ arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \ vgic/vgic.o vgic/vgic-init.o \ vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/arm.c +5 -0
@@ -42,6 +42,7 @@#include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_ptrauth.h> +#include <asm/kvm_rmi.h> #include <asm/sections.h> #include <asm/stacktrace/nvhe.h>
@@ -111,6 +112,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext)return -EINVAL ; } +DEFINE_STATIC_KEY_FALSE (kvm_rmi_is_available); + DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
@@ -3019,6 +3022,8 @@ static __init int kvm_arm_init(void) in_hyp_mode =is_kernel_in_hyp_mode (); +kvm_init_rmi (); +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/rmi.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2023-2026 ARM Ltd. +*/ + +#include <linux/kvm_host.h> + +#include <asm/rmi_cmds.h> +#include <asm/virt.h> + +void kvm_init_rmi (void ) +{ +/* +* TODO: Support Realm guests in nVHE mode, this will require adding +* EL2 stub(s) for REC entry and possibly other things. +*/ +if (!is_kernel_in_hyp_mode ()) +return ; + +if (!is_rmi_available ()) +return ; + +/* Future patch will enable static branch kvm_rmi_is_available */ +}
Signature · 1 line
2.43.0
Fuad Tabba
reviewerRe: [PATCH v16 11/45] KVM: arm64: CCA: Check for RMI support at KVM init
Hi Steven,
On Mon, 3 Aug 2026 at 14:45, Steven Price <[email protected]> wrote:
...
Quoted 13 lines
arch/arm64/include/asm/kvm_host.h +1
-0@@ -27,6 +27,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rmi.h> #include <asm/vncr_mapping.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 431
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -424,6 +425,9 @@ struct kvm_arch {/* Nested virtualization info */ struct dentry *debugfs_nv_dentry;#endif + +bool is_realm; +struct realm realm; };
Marc suggested at v10 making the pKVM and CCA state here a union, given the two are exclusive, and I believe you acked it [1]. Was there an issue with it, or did it just get lost across the respins?
Cheers,
/fuad
Quoted 125 lines
arch/arm64/include/asm/kvm_host.h +0
-0struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_rmi.h new file +17
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023-2026 ARM Ltd. +*/ + +#ifndef __ASM_KVM_RMI_H +#define __ASM_KVM_RMI_H + +/** +* struct realm - Additional per VM data for a Realm +*/ +struct realm { +}; + +void kvm_init_rmi (void ); + +#endif /* __ASM_KVM_RMI_H */ arch/arm64/include/asm/virt.h +1
-0@@ -87,6 +87,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_rmi_is_available);static inline bool is_pkvm_initialized (void ) {arch/arm64/kvm/Kconfig +1
-0@@ -37,6 +37,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD + select ARM_RMM help Support hosting virtualized guest machines.arch/arm64/kvm/Makefile +1
-1@@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ inject_fault.o va_layout.o handle_exit.o config.o \ guest.o debug.o reset.o sys_regs.o stacktrace.o \ - vgic-sys-reg-v3.o fpsimd.o pkvm.o \ + vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \ arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \ vgic/vgic.o vgic/vgic-init.o \ vgic/vgic-irqfd.o vgic/vgic-v2.o \arch/arm64/kvm/arm.c +5
-0@@ -42,6 +42,7 @@#include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_ptrauth.h> +#include <asm/kvm_rmi.h> #include <asm/sections.h> #include <asm/stacktrace/nvhe.h> @@ -111,6 +112,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext)return -EINVAL ; } +DEFINE_STATIC_KEY_FALSE (kvm_rmi_is_available); + DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);@@ -3019,6 +3022,8 @@ static __init int kvm_arm_init(void) in_hyp_mode =is_kernel_in_hyp_mode (); +kvm_init_rmi (); +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/rmi.c new file +24
-1@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2023-2026 ARM Ltd. +*/ + +#include <linux/kvm_host.h> + +#include <asm/rmi_cmds.h> +#include <asm/virt.h> + +void kvm_init_rmi (void ) +{ +/* +* TODO: Support Realm guests in nVHE mode, this will require adding +* EL2 stub(s) for REC entry and possibly other things. +*/ +if (!is_kernel_in_hyp_mode ()) +return ; + +if (!is_rmi_available ()) +return ; + +/* Future patch will enable static branch kvm_rmi_is_available */ +} --2.43.0
Suzuki K Poulose
reviewerRe: [PATCH v16 11/45] KVM: arm64: CCA: Check for RMI support at KVM init
Quoted 15 lines · Fuad Tabba
Hi Steven,
On Mon, 3 Aug 2026 at 14:45, Steven Price <[email protected]> wrote:
...Quoted 11 lines
arch/arm64/include/asm/kvm_host.h +1
-0@@ -27,6 +27,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rmi.h> #include <asm/vncr_mapping.h>
Quoted 13 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 431
Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +3
-0#define __KVM_HAVE_ARCH_INTC_INITIALIZED@@ -424,6 +425,9 @@ struct kvm_arch {/* Nested virtualization info */ struct dentry *debugfs_nv_dentry;#endif + +bool is_realm; +struct realm realm; };Marc suggested at v10 making the pKVM and CCA state here a union, given the two are exclusive, and I believe you acked it [1]. Was there an issue with it, or did it just get lost across the respins?
Most likely lost in the respins. Will pick this up.
Thanks for spotting.
Cheers
Suzuki
PATCH v16 12/45 KVM: arm64: CCA: Check for LPA2 support
In reply to Steven PriceIf KVM has enabled LPA2 support then check that the RMM also supports it. If there is a mismatch then disable support for realm guests as the VMM may attempt to create a guest which is incompatible with the RMM.
---
v15:
* Extend rmi_has_feature() to take the register number and check the presence of SHA-256 support which is default.
v13:
* New patch
--- arch/arm64/kvm/rmi.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
arch/arm64/kvm/rmi.c +25 -0
@@ -5,9 +5,31 @@#include <linux/kvm_host.h> +#include <asm/kvm_pgtable.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> +static bool rmi_has_feature (int reg,unsigned long feature) +{ +return !!u64_get_bits (rmi_feat_reg (reg), feature); +} + +static int rmm_check_features (void ) +{ +if (kvm_lpa2_is_enabled () && + !rmi_has_feature (0 ,RMI_FEATURE_REGISTER_0_LPA2 )) { +kvm_err ("RMM doesn't support LPA2\n" ); +return -ENXIO ; + } + +if (!rmi_has_feature (1 ,RMI_FEATURE_REGISTER_1_HASH_SHA_256 )) { +kvm_err ("RMM doesn't support SHA-256 measurements\n" ); +return -ENXIO ; + } + +return 0 ; +} +void kvm_init_rmi (void ) {/*
@@ -20,5 +42,8 @@ void kvm_init_rmi(void)if (!is_rmi_available ())return ; +if (rmm_check_features ()) +return ; +/* Future patch will enable static branch kvm_rmi_is_available */ }
Signature · 1 line
2.43.0
PATCH v16 13/45 KVM: arm64: CCA: Define the user ABI
In reply to Steven PriceThere is one CAP (KVM_CAP_ARM_RMI) which identifies the presence of CCA, and one ioctl. The ioctl (KVM_ARM_RMI_POPULATE) is used to populate memory during creation of the realm as this requires the RMM to copy data from an unprotected address to the protected memory - CCA does not support shared <-> private memory conversion where the memory contents is preserved as this is incompatible with memory encryption.
---
Changes since v15:
* Expand KVM_ARM_RMI_POPULATE documentation to be explicit that in-place shared-to-private conversion isn't supported.
Changes since v13:
* KVM_ARM_VCPU_RMI_PSCI_COMPLETE removed.
* KVM_ARM_RMI_POPULATE documentation updated to reflect that the structure is written by the kernel.
* CAP number bumped.
Changes since v12:
* Change KVM_ARM_RMI_POPULATE to update the structure with the amount that has been progressed rather than return the number of bytes populated.
* Describe the flag KVM_ARM_RMI_POPULATE_FLAGS_MEASURE.
* CAP number is bumped.
* NOTE: The PSCI ioctl may be removed in a future spec release.
Changes since v11:
* Completely reworked to be more implicit. Rather than having explicit CAP operations to progress the realm construction these operations are done when needed (on populating and on first vCPU run).
* Populate and PSCI complete are promoted to proper ioctls.
Changes since v10:
* Rename symbols from RME to RMI.
Changes since v9:
* Improvements to documentation.
* Bump the magic number for KVM_CAP_ARM_RME to avoid conflicts.
Changes since v8:
* Minor improvements to documentation following review.
* Bump the magic numbers to avoid conflicts.
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 | 47 ++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 13 ++++++++++ 2 files changed, 60 insertions(+)
Documentation/virt/kvm/api.rst +47 -0
@@ -6642,6 +6642,44 @@ if the guest_memfd memory was pinned in IOMMU page tables. See also: :ref: `KVM_SET_MEMORY_ATTRIBUTES`. +4.146 KVM_ARM_RMI_POPULATE +-------------------------- + +:Capability: KVM_CAP_ARM_RMI +:Architectures: arm64 +:Type: vm ioctl +:Parameters: struct kvm_arm_rmi_populate (in/out) +:Returns: 0 on success, < 0 on error + +:: + + struct kvm_arm_rmi_populate { + __u64 base; + __u64 size; + __u64 source_uaddr; + __u32 flags; + __u32 reserved; + }; + +Populate the guest address range described by `base` and `size` with data from +the userspace buffer at `source_uaddr`. The guest_memfd range backing the +memory must have `KVM_MEMORY_ATTRIBUTE_PRIVATE` set. + +Arm CCA cannot perform an in-place shared-to-private conversion while +preserving memory contents. Therefore, `source_uaddr` must be a valid, +page-aligned userspace pointer. The operation uses `RMI_RTT_DATA_MAP_INIT`, +which also sets the region to `RIPAS_RAM`. + +This is only valid before any VCPUs have been run. The ioctl might not populate +the entire region and in this case the kernel updates the fields `base`, `size` +and `source_uaddr`. User space may have to repeatedly call it until `size` is 0 +to populate the entire region. + +`flags` can be set to `KVM_ARM_RMI_POPULATE_FLAGS_MEASURE` to request that the +populated data is hashed and added to the guest's Realm Initial Measurement +(RIM) stored by the RMM. This can then be retrieved by the guest (using the RSI +interface) to present to an attestation server. + .. _kvm_run: 5. The kvm_run structure
@@ -9025,6 +9063,15 @@ enabled, cmma can't be enabled anymore and pfmfi and the storage key interpretation are disabled. If cmma has already been enabled or the hpage_2g module parameter is not set to 1, -EINVAL is returned. +7.48 KVM_CAP_ARM_RMI +-------------------- + +:Architectures: arm64 +:Target: VM +:Parameters: None + +This capability indicates that support for CCA realms is available. + 8. Other capabilities. ======================
include/uapi/linux/kvm.h +13 -0
@@ -998,6 +998,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_VSIE_ESAMODE 248#define KVM_CAP_S390_HPAGE_2G 249#define KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES 250 +#define KVM_CAP_ARM_RMI 251struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1686,4 +1687,16 @@ struct kvm_pre_fault_memory { __u64 padding[5]; }; +/* Available with KVM_CAP_ARM_RMI, only for VMs with KVM_VM_TYPE_ARM_REALM */ +#define KVM_ARM_RMI_POPULATE _IOWR(KVMIO, 0xd7, struct kvm_arm_rmi_populate) +#define KVM_ARM_RMI_POPULATE_FLAGS_MEASURE (1 << 0) + +struct kvm_arm_rmi_populate { +__u64 base; +__u64 size; +__u64 source_uaddr; +__u32 flags; +__u32 reserved; +}; +#endif /* __LINUX_KVM_H */
Signature · 1 line
2.43.0
PATCH v16 14/45 KVM: arm64: CCA: Add basic infrastructure for creating a realm
In reply to Steven PriceIntroduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.
The actual work of constructing the realm will be added in future patches.
---
Changes since v15:
* Remove realm->params and only temporarily allocate the page when the realm is being created.
Changes since v14:
* Adapt to the RMM v2.0-bet2 realm parameter layout and SRO-based realm creation.
Changes since v13:
* Rebased and updated to RMM-v2.0-bet1.
* Auxiliary granules have been removed in RMM-v2.0-bet1
Changes since v12:
* Drop the RMM_PAGE_{SHIFT,SIZE} defines - the RMM is now configured to be the same as the host's page size.
* Rework delegate/undelegate functions to use the new RMI range based operations.
Changes since v11:
* Major rework to drop the realm configuration and make the construction of realms implicit rather than driven by the VMM directly.
* The code to create RDs, handle VMIDs etc is moved to later patches.
Changes since v10:
* Rename from RME to RMI.
* Move the stage2 cleanup to a later patch.
Changes since v9:
* Avoid walking the stage 2 page tables when destroying the realm - the real ones are not accessible to the non-secure world, and the RMM may leave junk in the physical pages when returning them.
* Fix an error path in realm_create_rd() to actually return an error value.
Changes since v8:
* Fix free_delegated_granule() to not call kvm_account_pgtable_pages(); a separate wrapper will be introduced in a later patch to deal with RTTs.
* Minor code cleanups following review.
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 | 24 +++++++++++ arch/arm64/include/asm/kvm_rmi.h | 63 ++++++++++++++++++++++++++++ arch/arm64/kvm/arm.c | 12 ++++++ arch/arm64/kvm/mmu.c | 18 +++++++- arch/arm64/kvm/rmi.c | 56 +++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +24 -0
@@ -688,4 +688,28 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR; } } + +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rmi_is_available)) +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 void kvm_set_realm_state (struct kvm *kvm, +enum realm_state new_state) +{ +WRITE_ONCE (kvm->arch.realm.state, new_state); +} + +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ +return kvm_is_realm (kvm) &&kvm_realm_state (kvm) !=REALM_STATE_NONE ; +} +#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_rmi.h +63 -0
75 lines not on this page · raw message
arch/arm64/kvm/arm.c +12 -0
@@ -266,6 +266,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 (kvm); +if (ret) + goto err_uninit_mmu; + } +return 0 ; err_uninit_mmu:
@@ -328,6 +335,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_unshare_hyp (kvm, kvm +1 );kvm_arm_teardown_hypercalls (kvm); +if (kvm_is_realm (kvm)) +kvm_destroy_realm (kvm); }static bool kvm_has_full_ptr_auth (void )
@@ -488,6 +497,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else r =kvm_supports_cacheable_pfnmap ();break ; +case KVM_CAP_ARM_RMI : + r =static_key_enabled (&kvm_rmi_is_available); +break ;default : r =0 ;
arch/arm64/kvm/mmu.c +16 -2
@@ -904,10 +904,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {static int kvm_init_ipa_range (struct kvm_s2_mmu *mmu,unsigned long type) { +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);u32 kvm_ipa_limit =get_kvm_ipa_limit ();u64 mmfr0, mmfr1;u32 phys_shift; +if (kvm_is_realm (kvm)) + kvm_ipa_limit =kvm_rmm_ipa_limit (); + phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;
@@ -957,9 +961,18 @@ static void stage2_destroy_range(struct kvm_pgtable *pgt, phys_addr_t addr,static void kvm_stage2_destroy (struct kvm_pgtable *pgt) { +struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu);unsigned int ia_bits =VTCR_EL2_IPA (pgt->mmu->vtcr); -stage2_destroy_range (pgt,0 ,BIT (ia_bits)); +/* +* Realm RTTs are inaccessible to the host and may contain stale data +* after the RMM has released them. The non-root RTTs are explicitly +* destroyed through RMI before the RD is destroyed; only the root PGD +* pages remain to be freed here. +*/ +if (!kvm_is_realm (kvm)) +stage2_destroy_range (pgt,0 ,BIT (ia_bits)); +KVM_PGT_FN (kvm_pgtable_stage2_destroy_pgd)(pgt); }
@@ -1001,6 +1014,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn -EINVAL ; } + mmu->arch = &kvm->arch; + err =kvm_init_ipa_range (mmu, type);if (err)return err;
@@ -1009,7 +1024,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long tif (!pgt)return -ENOMEM ; - mmu->arch = &kvm->arch; err =KVM_PGT_FN (kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops);if (err) goto out_free_pgtable;
arch/arm64/kvm/rmi.c +56 -0
68 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v16 14/45] KVM: arm64: CCA: Add basic infrastructure for creating a realm
Hi Steve,
Quoted 11 lines · Steven Price
Introduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.
The actual work of constructing the realm will be added in future patches.
Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Removerealm->paramsand only temporarily allocate the page when the realm is being created.
[...]
Quoted 167 lines
--- arch/arm64/include/asm/kvm_emulate.h | 24 +++++++++++ arch/arm64/include/asm/kvm_rmi.h | 63 ++++++++++++++++++++++++++++ arch/arm64/kvm/arm.c | 12 ++++++ arch/arm64/kvm/mmu.c | 18 +++++++- arch/arm64/kvm/rmi.c | 56 +++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 2 deletions(-)arch/arm64/include/asm/kvm_emulate.h +24
-0@@ -688,4 +688,28 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR; } } + +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rmi_is_available)) +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 void kvm_set_realm_state (struct kvm *kvm, +enum realm_state new_state) +{ +WRITE_ONCE (kvm->arch.realm.state, new_state); +} + +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ +return kvm_is_realm (kvm) &&kvm_realm_state (kvm) !=REALM_STATE_NONE ; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_rmi.h +63
-0@@ -6,12 +6,75 @@#ifndef __ASM_KVM_RMI_H#define __ASM_KVM_RMI_H +#include <linux/arm-smccc-rmi.h> + +/** +* enum realm_state - State of a Realm +* +* Mirrors the RMM's Realm lifecycle states where they are meaningful to KVM, +* with REALM_STATE_DYING being a KVM-internal state used to prevent further +* requests while teardown is in progress. KVM does not track REALM_SYSTEM_OFF +* or REALM_ZOMBIE separately as they naturally lead to teardown. +*/ +enum realm_state { +/** +* @REALM_STATE_NONE: +* Realm has not yet been created. rmi_realm_create() has not +* yet been called. +*/ +REALM_STATE_NONE , +/** +* @REALM_STATE_NEW: +* Realm is under construction, rmi_realm_create() has been +* called, but it is not yet activated. Pages may be populated. +*/ +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 +* +* @rd: Kernel mapping of the RMM-managed Realm Descriptor (RD) granule +* @sro: Preallocated SRO state context for Realm MMU operations +* @state: The lifetime state machine for the realm +* @ia_bits: Number of valid Input Address bits in the IPA */ struct realm { +void *rd; +/* +* Reused by RTT map/unmap SRO commands. Those commands are only +* issued from Realm stage-2 map/unmap paths while kvm->mmu_lock is +* held for write, including Realm fault handling where +* kvm_fault_lock() takes the write side, so concurrent use is +* serialized. +*/ +struct rmi_sro_state *sro; +enum realm_state state; +unsigned int ia_bits; };void kvm_init_rmi (void ); +u32 kvm_rmm_ipa_limit (void ); + +int kvm_init_realm (struct kvm *kvm); +void kvm_destroy_realm (struct kvm *kvm);#endif /* __ASM_KVM_RMI_H */ arch/arm64/kvm/arm.c +12
-0@@ -266,6 +266,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 (kvm); +if (ret) + goto err_uninit_mmu; + } +return 0 ; err_uninit_mmu:@@ -328,6 +335,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_unshare_hyp (kvm, kvm +1 );kvm_arm_teardown_hypercalls (kvm); +if (kvm_is_realm (kvm)) +kvm_destroy_realm (kvm); }static bool kvm_has_full_ptr_auth (void )@@ -488,6 +497,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else r =kvm_supports_cacheable_pfnmap ();break ; +case KVM_CAP_ARM_RMI : + r =static_key_enabled (&kvm_rmi_is_available); +break ;default : r =0 ;arch/arm64/kvm/mmu.c +1
-0@@ -904,10 +904,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {static int kvm_init_ipa_range (struct kvm_s2_mmu *mmu,unsigned long type) { +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);
arch/arm64/kvm/mmu.c static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { · line 917
Quoted 10 lines
arch/arm64/kvm/mmu.c +3
-0u32 kvm_ipa_limit =get_kvm_ipa_limit ();u64 mmfr0, mmfr1;u32 phys_shift; +if (kvm_is_realm (kvm)) + kvm_ipa_limit =kvm_rmm_ipa_limit (); + phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;
get_kvm_ipa_limit() and variable 'kvm_ipa_limit' have been exposed through KVM_CAP_ARM_VM_IPA_SIZE for both normal and realm VMs in this series. However, the IPA limit determined by the feature-register-0 in TF-RMM, which is returned by kvm_rmm_ipa_limit(), can be different from 'kvm_ipa_limit'.
This brings confusion to VMM like qemu, where the request to create a realm VM is rejected if the requested IPA size exceeds the limit, exposed by the host through KVM_CAP_ARM_VM_IPA_SIZE. So the host needs to return correct IPA limit through KVM_CAP_ARM_VM_IPA_SIZE for realm VM somewhere in this series?
Thanks,
Gavin
Quoted 111 lines
arch/arm64/kvm/mmu.c +12
-2@@ -957,9 +961,18 @@ static void stage2_destroy_range(struct kvm_pgtable *pgt, phys_addr_t addr,static void kvm_stage2_destroy (struct kvm_pgtable *pgt) { +struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu);unsigned int ia_bits =VTCR_EL2_IPA (pgt->mmu->vtcr); -stage2_destroy_range (pgt,0 ,BIT (ia_bits)); +/* +* Realm RTTs are inaccessible to the host and may contain stale data +* after the RMM has released them. The non-root RTTs are explicitly +* destroyed through RMI before the RD is destroyed; only the root PGD +* pages remain to be freed here. +*/ +if (!kvm_is_realm (kvm)) +stage2_destroy_range (pgt,0 ,BIT (ia_bits)); +KVM_PGT_FN (kvm_pgtable_stage2_destroy_pgd)(pgt); }@@ -1001,6 +1014,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn -EINVAL ; } + mmu->arch = &kvm->arch; + err =kvm_init_ipa_range (mmu, type);if (err)return err;@@ -1009,7 +1024,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long tif (!pgt)return -ENOMEM ; - mmu->arch = &kvm->arch; err =KVM_PGT_FN (kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops);if (err) goto out_free_pgtable;arch/arm64/kvm/rmi.c +56
-0@@ -5,6 +5,8 @@#include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> @@ -14,6 +16,60 @@ static bool rmi_has_feature(int reg, unsigned long feature)return !!u64_get_bits (rmi_feat_reg (reg), feature); } +u32 kvm_rmm_ipa_limit (void ) +{ +return u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_S2SZ ); +} + +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); + +if (!kvm_realm_is_created (kvm)) { +kfree (realm->sro); + realm->sro =NULL ; +return ; + } + +kvm_set_realm_state (kvm,REALM_STATE_DYING ); + +if (realm->rd) { +phys_addr_t rd_phys =virt_to_phys (realm->rd); + +if (WARN_ON (rmi_realm_terminate (rd_phys, realm->sro))) +return ; + +if (WARN_ON (rmi_realm_destroy (rd_phys, realm->sro))) +return ; +free_delegated_page (rd_phys); + realm->rd =NULL ; + } + +if (WARN_ON (rmi_undelegate_range (kvm->arch.mmu.pgd_phys, + pgd_size))) +return ; + +kvm_set_realm_state (kvm,REALM_STATE_DEAD ); + +/* Now that the realm is destroyed, free the entry-level RTTs. */ +kvm_free_stage2_pgd (&kvm->arch.mmu); + +kfree (realm->sro); + realm->sro =NULL ; +} + +int kvm_init_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; + + realm->sro =kmalloc_obj (*realm->sro); +if (!realm->sro) +return -ENOMEM ; + +return 0 ; +} +static int rmm_check_features (void ) { if (kvm_lpa2_is_enabled() &&
Suzuki K Poulose
reviewerRe: [PATCH v16 14/45] KVM: arm64: CCA: Add basic infrastructure for creating a realm
Quoted 197 lines · Gavin Shan
Hi Steve,
Quoted 11 lines · Steven Price
Introduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.
The actual work of constructing the realm will be added in future patches.
Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Removerealm->paramsand only temporarily allocate the page when the realm is being created.[...]
Quoted 164 lines
---
arch/arm64/include/asm/kvm_emulate.h| 24 +++++++++++ arch/arm64/include/asm/kvm_rmi.h | 63 ++++++++++++++++++++++++++++arch/arm64/kvm/arm.c | 12 ++++++ arch/arm64/kvm/mmu.c | 18 +++++++- arch/arm64/kvm/rmi.c | 56 +++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 2 deletions(-)arch/arm64/include/asm/kvm_emulate.h +22
-0@@ -688,4 +688,28 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR; } } + +static inline bool kvm_is_realm (struct kvm *kvm) +{ + if (static_branch_unlikely(&kvm_rmi_is_available)) + 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 void kvm_set_realm_state (struct kvm *kvm, + enum realm_state new_state) +{ + WRITE_ONCE(kvm->arch.realm.state, new_state); +} + +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ + return kvm_is_realm(kvm) && kvm_realm_state(kvm) !=
REALM_STATE_NONE;arch/arm64/include/asm/kvm_emulate.h +2
-0+} +#endif /*
__ARM64_KVM_EMULATE_H__*/arch/arm64/include/asm/kvm_rmi.h +7
-0@@ -6,12 +6,75 @@ #ifndef __ASM_KVM_RMI_H #define __ASM_KVM_RMI_H +#include <linux/arm-smccc-rmi.h> + +/** +* enum realm_state - State of a Realm +* +* Mirrors the RMM's Realm lifecycle states where they are meaningful to KVM, +* with REALM_STATE_DYING being a KVM-internal state used to prevent further
+ * requests while teardown is in progress. KVM does not trackREALM_SYSTEM_OFF
+ * orREALM_ZOMBIEseparately as they naturally lead to teardown.
+ */
+enumrealm_state{
+ /**
+ * @REALM_STATE_NONE:
+ * Realm has not yet been created.rmi_realm_create()has not + * yet been called.
+ */
+REALM_STATE_NONE,
+ /**
+ * @REALM_STATE_NEW:
+ * Realm is under construction,rmi_realm_create()has been + * called, but it is not yet activated. Pages may be populated. + */
+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_DEADarch/arm64/include/asm/kvm_rmi.h +2
-0+}; +/**
* struct realm - Additional per VM data for a Realm
+ *
+ * @rd: Kernel mapping of the RMM-managed Realm Descriptor (RD) granule
+ * @sro: Preallocated SRO state context for Realm MMU operations
+ * @state: The lifetime state machine for the realm
+ * @ia_bits: Number of valid Input Address bits in the IPA */
struct realm {
+ void *rd;
+ /*
+ * Reused by RTT map/unmap SRO commands. Those commands are only + * issued from Realm stage-2 map/unmap paths whilekvm->mmu_lockis + * held for write, including Realm fault handling where + *kvm_fault_lock()takes the write side, so concurrent use is + * serialized.
+ */
+ structrmi_sro_state*sro;
+ enumrealm_statestate;
+ unsigned intia_bits;
};
voidkvm_init_rmi(void);arch/arm64/include/asm/kvm_rmi.h +4
-0+u32 kvm_rmm_ipa_limit (void ); + +int kvm_init_realm (struct kvm *kvm); +void kvm_destroy_realm (struct kvm *kvm);#endif /*
__ASM_KVM_RMI_H*/arch/arm64/kvm/arm.c +7
-0@@ -266,6 +266,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(kvm); + if (ret) + goto err_uninit_mmu; + } + return 0;
err_uninit_mmu:arch/arm64/kvm/arm.c +2
-0@@ -328,6 +335,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm) kvm_unshare_hyp(kvm, kvm + 1); kvm_arm_teardown_hypercalls(kvm); + if (kvm_is_realm(kvm)) + kvm_destroy_realm(kvm); } static bool kvm_has_full_ptr_auth(void)@@ -488,6 +497,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else
r =kvm_supports_cacheable_pfnmap(); break;
+ caseKVM_CAP_ARM_RMI:
+ r =static_key_enabled(&kvm_rmi_is_available); + break;
default:
r = 0;arch/arm64/kvm/mmu.c +0
-0@@ -904,10 +904,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type) {arch/arm64/kvm/mmu.c static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { · line 915
Quoted 10 lines
arch/arm64/kvm/mmu.c +4
-0+ struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu); u32 kvm_ipa_limit = get_kvm_ipa_limit(); u64 mmfr0, mmfr1; u32 phys_shift; + if (kvm_is_realm(kvm)) + kvm_ipa_limit = kvm_rmm_ipa_limit(); + phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type); if (is_protected_kvm_enabled()) { phys_shift = kvm_ipa_limit;
get_kvm_ipa_limit()and variable 'kvm_ipa_limit' have been exposed throughKVM_CAP_ARM_VM_IPA_SIZEfor both normal and realm VMs in this series. However, the IPA limit determined by the feature-register-0 in TF-RMM, which is returned bykvm_rmm_ipa_limit(), can be different from 'kvm_ipa_limit'.
Quoted 4 lines
This brings confusion to VMM like qemu, where the request to create a realm VM is rejected if the requested IPA size exceeds the limit, exposed by the host through
KVM_CAP_ARM_VM_IPA_SIZE. So the host needs to return correct IPA limit throughKVM_CAP_ARM_VM_IPA_SIZEfor realm VM somewhere in this series?
You're right that the limit exposed by the KVM_CAP_ARM_VM_IPA_SIZE doesn't tell the VMM about the limit imposed by the RMM. The only way to expose this is via an ioctl() on the KVM VM fd. But the VM fd is created with an argument (type), which on arm64 unfortunately specifies the IPA Size. So, this is kind of a chicken and egg problem. Or the VMM can create a dummy Realm VM (with default IPA size of 0) and then run the cap.
On the other hand, given the Realm creation is measured, if the Realm can't be created with the "IPA Size" requested by the user, then there is no point in creating the Realm VM.
So, we decided that this failure condition was alright.
This was mentioned in one of the versions of the series, but got lost in the revisions.
If there is interest in getting this exposed, we could do something like the above. Or, the other option is limit the IPA size of the VM that can be created to RMM IPA limit, when we explicitly choose the KVM mode to be CCA (this is something that is coming in the next version)
Cheers
Suzuki
Quoted 114 lines
Thanks,
GavinQuoted 109 lines
arch/arm64/kvm/mmu.c +3
-1@@ -957,9 +961,18 @@ static void stage2_destroy_range(struct kvm_pgtable *pgt, phys_addr_t addr, static void kvm_stage2_destroy(struct kvm_pgtable *pgt) { + struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); unsigned int ia_bits = VTCR_EL2_IPA(pgt->mmu->vtcr); - stage2_destroy_range(pgt, 0, BIT(ia_bits)); + /* +* Realm RTTs are inaccessible to the host and may contain stale data
+ * after the RMM has released them. The non-root RTTs are explicitly + * destroyed through RMI before the RD is destroyed; only the root PGD
+ * pages remain to be freed here.
+ */
+ if (!kvm_is_realm(kvm))
+stage2_destroy_range(pgt, 0, BIT(ia_bits));
+
KVM_PGT_FN(kvm_pgtable_stage2_destroy_pgd)(pgt);
}arch/arm64/kvm/mmu.c +0
-0@@ -1001,6 +1014,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct
kvm_s2_mmu*mmu, unsigned long t
return-EINVAL;
}
+mmu->arch=&kvm->arch;
+
err =kvm_init_ipa_range(mmu, type);
if (err)
return err;arch/arm64/kvm/mmu.c +0
-0@@ -1009,7 +1024,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct
kvm_s2_mmu*mmu, unsigned long t
if (!pgt)
return-ENOMEM;
-mmu->arch=&kvm->arch;
err = KVM_PGT_FN(kvm_pgtable_stage2_init)(pgt, mmu,&kvm_s2_mm_ops); if (err)
gotoout_free_pgtable;arch/arm64/kvm/rmi.c +56
-0@@ -5,6 +5,8 @@ #include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/rmi_cmds.h> #include <asm/virt.h>@@ -14,6 +16,60 @@ static bool rmi_has_feature(int reg, unsigned long feature) return !!u64_get_bits(rmi_feat_reg(reg), feature); } +u32 kvm_rmm_ipa_limit (void ) +{ + return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ); +} + +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); + + if (!kvm_realm_is_created(kvm)) { + kfree(realm->sro); + realm->sro = NULL; + return; + } + + kvm_set_realm_state(kvm, REALM_STATE_DYING); + + if (realm->rd) { + phys_addr_t rd_phys = virt_to_phys(realm->rd); + + if (WARN_ON(rmi_realm_terminate(rd_phys, realm->sro))) + return; + + if (WARN_ON(rmi_realm_destroy(rd_phys, realm->sro))) + return; + free_delegated_page(rd_phys); + realm->rd = NULL; + } + + if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys, + pgd_size))) + return; + + kvm_set_realm_state(kvm, REALM_STATE_DEAD); + + /* Now that the realm is destroyed, free the entry-level RTTs. */ + kvm_free_stage2_pgd(&kvm->arch.mmu); + + kfree(realm->sro); + realm->sro = NULL; +} + +int kvm_init_realm (struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + + realm->sro = kmalloc_obj(*realm->sro); + if (!realm->sro) + return -ENOMEM; + + return 0; +} + static int rmm_check_features(void) {if (
kvm_lpa2_is_enabled()&&
Re: [PATCH v16 14/45] KVM: arm64: CCA: Add basic infrastructure for creating a realm
Quoted 209 lines · Suzuki K Poulose
Quoted 189 lines · Gavin Shan
Hi Steve,
Quoted 11 lines · Steven Price
Introduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.
The actual work of constructing the realm will be added in future patches.
Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Removerealm->paramsand only temporarily allocate the page when the realm is being created.[...]
Quoted 156 lines
---
arch/arm64/include/asm/kvm_emulate.h| 24 +++++++++++ arch/arm64/include/asm/kvm_rmi.h | 63 ++++++++++++++++++++++++++++arch/arm64/kvm/arm.c | 12 ++++++ arch/arm64/kvm/mmu.c | 18 +++++++- arch/arm64/kvm/rmi.c | 56 +++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 2 deletions(-)arch/arm64/include/asm/kvm_emulate.h +24
-0@@ -688,4 +688,28 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR; } } + +static inline bool kvm_is_realm (struct kvm *kvm) +{ + if (static_branch_unlikely(&kvm_rmi_is_available)) + 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 void kvm_set_realm_state (struct kvm *kvm, + enum realm_state new_state) +{ + WRITE_ONCE(kvm->arch.realm.state, new_state); +} + +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ + return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE; +} + #endif /* __ARM64_KVM_EMULATE_H__ */arch/arm64/include/asm/kvm_rmi.h +63
-0@@ -6,12 +6,75 @@ #ifndef __ASM_KVM_RMI_H #define __ASM_KVM_RMI_H +#include <linux/arm-smccc-rmi.h> + +/** +* enum realm_state - State of a Realm +* +* Mirrors the RMM's Realm lifecycle states where they are meaningful to KVM, +* with REALM_STATE_DYING being a KVM-internal state used to prevent further +* requests while teardown is in progress. KVM does not track REALM_SYSTEM_OFF +* or REALM_ZOMBIE separately as they naturally lead to teardown. +*/ +enum realm_state { + /** + * @REALM_STATE_NONE: + * Realm has not yet been created. rmi_realm_create() has not + * yet been called. + */ + REALM_STATE_NONE, + /** + * @REALM_STATE_NEW: + * Realm is under construction, rmi_realm_create() has been + * called, but it is not yet activated. Pages may be populated. + */ + 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 + * + * @rd: Kernel mapping of the RMM-managed Realm Descriptor (RD) granule + * @sro: Preallocated SRO state context for Realm MMU operations + * @state: The lifetime state machine for the realm + * @ia_bits: Number of valid Input Address bits in the IPA */ struct realm { + void *rd; + /* + * Reused by RTT map/unmap SRO commands. Those commands are only + * issued from Realm stage-2 map/unmap paths while kvm->mmu_lock is + * held for write, including Realm fault handling where + * kvm_fault_lock() takes the write side, so concurrent use is + * serialized. + */ + struct rmi_sro_state *sro; + enum realm_state state; + unsigned int ia_bits; }; void kvm_init_rmi(void); +u32 kvm_rmm_ipa_limit (void ); + +int kvm_init_realm (struct kvm *kvm); +void kvm_destroy_realm (struct kvm *kvm); #endif /* __ASM_KVM_RMI_H */arch/arm64/kvm/arm.c +7
-0@@ -266,6 +266,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(kvm); + if (ret) + goto err_uninit_mmu; + } + return 0;
err_uninit_mmu:arch/arm64/kvm/arm.c +2
-0@@ -328,6 +335,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm) kvm_unshare_hyp(kvm, kvm + 1); kvm_arm_teardown_hypercalls(kvm); + if (kvm_is_realm(kvm)) + kvm_destroy_realm(kvm); } static bool kvm_has_full_ptr_auth(void)@@ -488,6 +497,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else
r =kvm_supports_cacheable_pfnmap(); break;
+ caseKVM_CAP_ARM_RMI:
+ r =static_key_enabled(&kvm_rmi_is_available); + break;
default:
r = 0;arch/arm64/kvm/mmu.c +0
-0@@ -904,10 +904,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type) {arch/arm64/kvm/mmu.c static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { · line 915
Quoted 10 lines
arch/arm64/kvm/mmu.c +4
-0+ struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu); u32 kvm_ipa_limit = get_kvm_ipa_limit(); u64 mmfr0, mmfr1; u32 phys_shift; + if (kvm_is_realm(kvm)) + kvm_ipa_limit = kvm_rmm_ipa_limit(); + phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type); if (is_protected_kvm_enabled()) { phys_shift = kvm_ipa_limit;
get_kvm_ipa_limit()and variable 'kvm_ipa_limit' have been exposed throughKVM_CAP_ARM_VM_IPA_SIZEfor both normal and realm VMs in this series. However, the IPA limit determined by the feature-register-0 in TF-RMM, which is returned bykvm_rmm_ipa_limit(), can be different from 'kvm_ipa_limit'.Quoted 4 lines
This brings confusion to VMM like qemu, where the request to create a realm VM is rejected if the requested IPA size exceeds the limit, exposed by the host through
KVM_CAP_ARM_VM_IPA_SIZE. So the host needs to return correct IPA limit throughKVM_CAP_ARM_VM_IPA_SIZEfor realm VM somewhere in this series?You're right that the limit exposed by the
KVM_CAP_ARM_VM_IPA_SIZEdoesn't tell the VMM about the limit imposed by the RMM. The only way to expose this is via anioctl()on the KVM VM fd. But the VM fd is created with an argument (type), which on arm64 unfortunately specifies the IPA Size. So, this is kind of a chicken and egg problem. Or the VMM can create a dummy Realm VM (with default IPA size of 0) and then run the cap.On the other hand, given the Realm creation is measured, if the Realm can't be created with the "IPA Size" requested by the user, then there is no point in creating the Realm VM.
So, we decided that this failure condition was alright.
Quoted 9 lines
This was mentioned in one of the versions of the series, but got lost in the revisions.
If there is interest in getting this exposed, we could do something like the above. Or, the other option is limit the IPA size of the VM that can be created to RMM IPA limit, when we explicitly choose the KVM mode to be CCA (this is something that is coming in the next version)
I was even wandering if KVM_CAP_ARM_RMI can be extended to return the limit. For example, KVM_CAP_ARM_RMI is renamed to KVM_CAP_ARM_REALM_IPA_SIZE. 0 is returned if Realm VM isn't supported. Otherwise, the IPA size enforced by TF-RMM through the feature-register-0 is returned.
With above extension, the existing KVM_CAP_ARM_VM_IPA_SIZE is dedicated to query the IPA size for the regular (or normal) VM, while KVM_CAP_ARM_REALM_IPA_SIZE is used to get the IPA size for the Realm VM. VMM needs to decide which one is to be used.
Another option could be splitting @arg in kvm_vm_ioctl_check_extension() into two parts: (a) Indicator of the scope; (b) Specific capability. For (a), it could be categorized into: (1) 0, global and not specific to any types of VMs;
(2) 1, indicating the capability is associated with a regular (normal) VM; (3) 2, indicating the capability is associated with a Realm VM. With this extension, VMM uses ioctl(kvm_fd, KVM_CHECK_EXTENSION, (2UL << 48) | KVM_CAP_ARM_VM_IPA_SIZE) to retrieve the IPA size of Realm VM that is enforced by TF-RMM. Besides, I'm not sure if pKVM can be benefited from this splitting in long term.
Thanks,
Gavin
Quoted 105 lines
Quoted 104 lines
Quoted 103 lines
arch/arm64/kvm/mmu.c +12
-2@@ -957,9 +961,18 @@ static void stage2_destroy_range(struct kvm_pgtable *pgt, phys_addr_t addr, static void kvm_stage2_destroy(struct kvm_pgtable *pgt) { + struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); unsigned int ia_bits = VTCR_EL2_IPA(pgt->mmu->vtcr); - stage2_destroy_range(pgt, 0, BIT(ia_bits)); + /* + * Realm RTTs are inaccessible to the host and may contain stale data + * after the RMM has released them. The non-root RTTs are explicitly + * destroyed through RMI before the RD is destroyed; only the root PGD + * pages remain to be freed here. + */ + if (!kvm_is_realm(kvm)) + stage2_destroy_range(pgt, 0, BIT(ia_bits)); + KVM_PGT_FN(kvm_pgtable_stage2_destroy_pgd)(pgt); }@@ -1001,6 +1014,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t return -EINVAL; } + mmu->arch = &kvm->arch; + err = kvm_init_ipa_range(mmu, type); if (err) return err;@@ -1009,7 +1024,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t if (!pgt) return -ENOMEM; - mmu->arch = &kvm->arch; err = KVM_PGT_FN(kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops); if (err) goto out_free_pgtable;arch/arm64/kvm/rmi.c +56
-0@@ -5,6 +5,8 @@ #include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/rmi_cmds.h> #include <asm/virt.h>@@ -14,6 +16,60 @@ static bool rmi_has_feature(int reg, unsigned long feature) return !!u64_get_bits(rmi_feat_reg(reg), feature); } +u32 kvm_rmm_ipa_limit (void ) +{ + return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ); +} + +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); + + if (!kvm_realm_is_created(kvm)) { + kfree(realm->sro); + realm->sro = NULL; + return; + } + + kvm_set_realm_state(kvm, REALM_STATE_DYING); + + if (realm->rd) { + phys_addr_t rd_phys = virt_to_phys(realm->rd); + + if (WARN_ON(rmi_realm_terminate(rd_phys, realm->sro))) + return; + + if (WARN_ON(rmi_realm_destroy(rd_phys, realm->sro))) + return; + free_delegated_page(rd_phys); + realm->rd = NULL; + } + + if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys, + pgd_size))) + return; + + kvm_set_realm_state(kvm, REALM_STATE_DEAD); + + /* Now that the realm is destroyed, free the entry-level RTTs. */ + kvm_free_stage2_pgd(&kvm->arch.mmu); + + kfree(realm->sro); + realm->sro = NULL; +} + +int kvm_init_realm (struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + + realm->sro = kmalloc_obj(*realm->sro); + if (!realm->sro) + return -ENOMEM; + + return 0; +} + static int rmm_check_features(void) {if (
kvm_lpa2_is_enabled()&&
PATCH v16 15/45 KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
RMM v2.0 provides no mechanism for the host to perform debug operations on the guest. So limit the extensions that are visible to an allowlist so that only those capabilities we can support are advertised.
---
Changes since v13:
* Add missing check in kvm_vm_ioctl_enable_cap().
Changes since v10:
* Add a kvm_realm_ext_allowed() function which limits which extensions are exposed to an allowlist. This removes the need for special casing various extensions.
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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
arch/arm64/kvm/arm.c +26 -0
@@ -135,6 +135,26 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; } +static bool kvm_realm_ext_allowed (long ext) +{ +switch (ext) { +case KVM_CAP_IRQCHIP : +case KVM_CAP_ARM_PSCI : +case KVM_CAP_ARM_PSCI_0_2 : +case KVM_CAP_NR_VCPUS : +case KVM_CAP_MAX_VCPUS : +case KVM_CAP_MAX_VCPU_ID : +case KVM_CAP_MSI_DEVID : +case KVM_CAP_ARM_VM_IPA_SIZE : +case KVM_CAP_ARM_PTRAUTH_ADDRESS : +case KVM_CAP_ARM_PTRAUTH_GENERIC : +case KVM_CAP_ARM_RMI : +case KVM_CAP_SYNC_MMU : +return true; + } +return false; +} +int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {
@@ -146,6 +166,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,if (is_protected_kvm_enabled () && !kvm_pkvm_ext_allowed (kvm, cap->cap))return -EINVAL ; +if (kvm &&kvm_is_realm (kvm) && !kvm_realm_ext_allowed (cap->cap)) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : r =0 ;
@@ -380,6 +403,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)if (is_protected_kvm_enabled () && !kvm_pkvm_ext_allowed (kvm, ext))return 0 ; +if (kvm &&kvm_is_realm (kvm) && !kvm_realm_ext_allowed (ext)) +return 0 ; +switch (ext) {case KVM_CAP_IRQCHIP : r = vgic_present;
Signature · 1 line
2.43.0
Re: [PATCH v16 15/45] KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests
Hi Steve,
Quoted 41 lines · Steven Price
From: Suzuki K Poulose <[email protected]>
RMM v2.0 provides no mechanism for the host to perform debug operations on the guest. So limit the extensions that are visible to an allowlist so that only those capabilities we can support are advertised.
Signed-off-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v13:
* Add missing check inkvm_vm_ioctl_enable_cap().
Changes since v10:
* Add akvm_realm_ext_allowed()function which limits which extensions are exposed to an allowlist. This removes the need for special casing various extensions.
Changes since v7:
* Remove the helper functions and inline thekvm_is_realm()check with a ternary operator.
* Rewrite the commit message to explain this patch.--- arch/arm64/kvm/arm.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)arch/arm64/kvm/arm.c +10
-0@@ -135,6 +135,26 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; } +static bool kvm_realm_ext_allowed (long ext) +{ +switch (ext) { +case KVM_CAP_IRQCHIP : +case KVM_CAP_ARM_PSCI : +case KVM_CAP_ARM_PSCI_0_2 : +case KVM_CAP_NR_VCPUS : +case KVM_CAP_MAX_VCPUS : +case KVM_CAP_MAX_VCPU_ID : +case KVM_CAP_MSI_DEVID :
arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 157
Quoted 10 lines
arch/arm64/kvm/arm.c +10
-0+case KVM_CAP_ARM_VM_IPA_SIZE : +case KVM_CAP_ARM_PTRAUTH_ADDRESS : +case KVM_CAP_ARM_PTRAUTH_GENERIC : +case KVM_CAP_ARM_RMI : +case KVM_CAP_SYNC_MMU : +return true; + } +return false; +} +
Nitpick: The code can be simplified a bit if @kvm is passed to kvm_realm_ext_allowed(). In this way, kvm_realm_ext_allowed() looks similar to kvm_pkvm_ext_allowed().
static bool kvm_realm_ext_allowed(struct kvm *kvm, long ext)
{
if (!kvm || !kvm_is_realm(kvm))
return true;
switch (ext) {
case KVM_CAP_IRQCHIP:
:
case KVM_CAP_SYNC_MMU:
return true;
}
return false;
}arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 171
Quoted 10 lines
arch/arm64/kvm/arm.c +3
-0int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {@@ -146,6 +166,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,if (is_protected_kvm_enabled () && !kvm_pkvm_ext_allowed (kvm, cap->cap))return -EINVAL ; +if (kvm &&kvm_is_realm (kvm) && !kvm_realm_ext_allowed (cap->cap)) +return -EINVAL ; +
if (!kvm_realm_ext_allowed(kvm, cap->cap)) return -EINVAL;
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 408
Quoted 10 lines
arch/arm64/kvm/arm.c +3
-0switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : r =0 ;@@ -380,6 +403,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)if (is_protected_kvm_enabled () && !kvm_pkvm_ext_allowed (kvm, ext))return 0 ; +if (kvm &&kvm_is_realm (kvm) && !kvm_realm_ext_allowed (ext)) +return 0 ; +
if (!kvm_realm_ext_allowed(kvm, ext)) return 0;
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 411
Quoted 3 lines
arch/arm64/kvm/arm.c +0
-0switch (ext) {case KVM_CAP_IRQCHIP : r = vgic_present;
Thanks,
Gavin
PATCH v16 16/45 KVM: arm64: CCA: Allow passing the machine type in KVM creation
In reply to Steven PricePreviously 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 v15:
* Specify the encoding for each VM type in the documentation.
Changes since v13:
* Rework to use the two top bits for the machine type now that pKVM has merged and used the top bit for KVM_VM_TYPE_ARM_PROTECTED.
* Update the documentation to include KVM_VM_TYPE_ARM_PROTECTED as well.
Changes since v9:
* Explictly set realm.state to REALM_STATE_NONE rather than rely on the zeroing of the structure.
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 | 21 +++++++++++++++++++-- arch/arm64/kvm/arm.c | 11 +++++++++++ include/uapi/linux/kvm.h | 7 ++++++- 3 files changed, 36 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +19 -2
@@ -181,8 +181,25 @@ 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]) encodes the +address size and the upper bits[31:30] encode a machine type. The machine +types that might be available are: + + ========================= ======= ======================================== + Type [31:30] Description + ========================= ======= ======================================== + KVM_VM_TYPE_ARM_NORMAL 0b00 A standard VM + KVM_VM_TYPE_ARM_REALM 0b01 A "realm" VM using the Arm Confidential + Compute extensions, the VM's memory is + protected from the host. + KVM_VM_TYPE_ARM_PROTECTED 0b10 A "protected" VM using pKVM to + isolate the VM from the host. + Reserved 0b11 Reserved for future use + ========================= ======= ======================================== + +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 +11 -0
@@ -249,6 +249,17 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)mutex_unlock (&kvm->lock);#endif +if ((type &KVM_VM_TYPE_ARM_PROTECTED ) && + (type &KVM_VM_TYPE_ARM_REALM )) +return -EINVAL ; + +if (type &KVM_VM_TYPE_ARM_REALM ) { +if (!static_branch_unlikely (&kvm_rmi_is_available)) +return -EINVAL ; +kvm_set_realm_state (kvm,REALM_STATE_NONE ); + kvm->arch.is_realm = true; + } +kvm_init_nested (kvm); ret =kvm_share_hyp (kvm, kvm +1 );
include/uapi/linux/kvm.h +6 -1
@@ -700,14 +700,19 @@ struct kvm_enable_cap {* 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. +* +* Bits[30-31] are reserved for the VM type */ #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) +#define KVM_VM_TYPE_ARM_NORMAL 0 +#define KVM_VM_TYPE_ARM_REALM (1UL << 30)#define KVM_VM_TYPE_ARM_PROTECTED (1UL << 31)#define KVM_VM_TYPE_ARM_MASK (KVM_VM_TYPE_ARM_IPA_SIZE_MASK | \ - KVM_VM_TYPE_ARM_PROTECTED) + KVM_VM_TYPE_ARM_PROTECTED | \ + KVM_VM_TYPE_ARM_REALM)/* * ioctls for /dev/kvm fds:
Signature · 1 line
2.43.0
Re: [PATCH v16 16/45] KVM: arm64: CCA: Allow passing the machine type in KVM creation
Hi Steve,
Quoted 70 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.Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Specify the encoding for each VM type in the documentation.
Changes since v13:
* Rework to use the two top bits for the machine type now that pKVM has merged and used the top bit forKVM_VM_TYPE_ARM_PROTECTED.
* Update the documentation to includeKVM_VM_TYPE_ARM_PROTECTEDas well.
Changes since v9:
* Explictly set realm.state toREALM_STATE_NONErather than rely on the zeroing of the structure.
Changes since v7:
* Add some documentation explaining the new machine type.
Changes since v6:
* Make the check forkvm_rme_is_availablemore 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 | 21 +++++++++++++++++++-- arch/arm64/kvm/arm.c | 11 +++++++++++ include/uapi/linux/kvm.h | 7 ++++++- 3 files changed, 36 insertions(+), 3 deletions(-)Documentation/virt/kvm/api.rst +19
-2@@ -181,8 +181,25 @@ 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]) encodes the +address size and the upper bits[31:30] encode a machine type. The machine +types that might be available are: + + ========================= ======= ======================================== + Type [31:30] Description + ========================= ======= ======================================== + KVM_VM_TYPE_ARM_NORMAL 0b00 A standard VM + KVM_VM_TYPE_ARM_REALM 0b01 A "realm" VM using the Arm Confidential + Compute extensions, the VM's memory is + protected from the host. + KVM_VM_TYPE_ARM_PROTECTED 0b10 A "protected" VM using pKVM to + isolate the VM from the host. + Reserved 0b11 Reserved for future use + ========================= ======= ======================================== + +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 physicalarch/arm64/kvm/arm.c +1
-0@@ -249,6 +249,17 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)mutex_unlock (&kvm->lock);#endif + if ((type & KVM_VM_TYPE_ARM_PROTECTED) &&
arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 262
Quoted 10 lines
arch/arm64/kvm/arm.c +10
-0+ (type & KVM_VM_TYPE_ARM_REALM)) +return -EINVAL ; + +if (type &KVM_VM_TYPE_ARM_REALM ) { +if (!static_branch_unlikely (&kvm_rmi_is_available)) +return -EINVAL ; +kvm_set_realm_state (kvm,REALM_STATE_NONE ); + kvm->arch.is_realm = true; + } +
Shall we use kvm_arm_rmi_supported() here? In that way, the check is consistent to what has been done for KVM_CAP_ARM_RMI. You know that a misbehaving VMM can issue KVM_CREATE_VM to create a realm VM without checking if KVM_CAP_ARM_RMI is supported by the host.
if (type & KVM_VM_TYPE_ARM_REALM) {
if (!kvm_arm_rmi_supported())
return -EINVAL;
kvm_set_realm_state(kvm, REALM_STATE_NONE);
WRITE_ONCE(kvm->arch.is_realm, true);
}Quoted 18 lines
arch/arm64/kvm/arm.c +0
-0kvm_init_nested (kvm); ret =kvm_share_hyp (kvm, kvm +1 );include/uapi/linux/kvm.h +2
-0@@ -700,14 +700,19 @@ struct kvm_enable_cap {* 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. +* +* Bits[30-31] are reserved for the VM type */ #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)
include/uapi/linux/kvm.h struct kvm_enable_cap { · line 718
Quoted 10 lines
include/uapi/linux/kvm.h +4
-1+#define KVM_VM_TYPE_ARM_NORMAL 0 +#define KVM_VM_TYPE_ARM_REALM (1UL << 30)#define KVM_VM_TYPE_ARM_PROTECTED (1UL << 31)#define KVM_VM_TYPE_ARM_MASK (KVM_VM_TYPE_ARM_IPA_SIZE_MASK | \ - KVM_VM_TYPE_ARM_PROTECTED) + KVM_VM_TYPE_ARM_PROTECTED | \ + KVM_VM_TYPE_ARM_REALM)/* * ioctls for /dev/kvm fds:
Thanks,
Gavin
PATCH v16 17/45 KVM: arm64: CCA: Tear down RTTs
In reply to Steven PriceThe 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 v15:
* Propagate negative error codes.
Changes since v14:
* Teardown is now serialized under config_lock and idempotent.
Changes since v13:
* Avoid the double call of kvm_free_stage2_pgd() by splitting the work across that and a new function kvm_realm_uninit_stage2() which is only called for realm guests.
Changes since v12:
* Simplify some functions now we know RMM page size is the same as the host's.
Changes since v11:
* Moved some code from earlier in the series to this one so that it's added when it's first used.
Changes since v10:
* RME->RMI rename.
* Some code to handle freeing stage 2 PGD moved into this patch where it belongs.
Changes since v9:
* Add a comment clarifying that root level RTTs are not destroyed until after the RD is destroyed.
Changes since v8:
* Introduce free_rtt() wrapper which calls free_delegated_granule() followed by kvm_account_pgtable_pages(). This makes it clear where an RTT is being freed rather than just a delegated granule.
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_rmi.h | 11 ++ arch/arm64/kvm/mmu.c | 17 ++- arch/arm64/kvm/rmi.c | 198 +++++++++++++++++++++++++++++++ 3 files changed, 225 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rmi.h +11 -0
@@ -56,6 +56,8 @@ enum realm_state {* @sro: Preallocated SRO state context for Realm MMU operations * @state: The lifetime state machine for the realm * @ia_bits: Number of valid Input Address bits in the IPA +* @stage2_unmapped: The Realm stage-2 mappings have been removed +* @rtts_destroyed: The non-root RTTs have been torn down */ struct realm {void *rd;
@@ -69,6 +71,8 @@ struct realm {struct rmi_sro_state *sro;enum realm_state state;unsigned int ia_bits; +bool stage2_unmapped; +bool rtts_destroyed; };void kvm_init_rmi (void );
@@ -76,5 +80,12 @@ u32 kvm_rmm_ipa_limit(void);int kvm_init_realm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm); +int kvm_realm_teardown_stage2 (struct kvm *kvm); + +static inline bool kvm_realm_is_private_address (struct realm *realm, +unsigned long addr) +{ +return !(addr &BIT (realm->ia_bits -1 )); +}#endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +16 -1
@@ -1060,9 +1060,24 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn err; } +static void kvm_realm_uninit_stage2 (struct kvm_s2_mmu *mmu) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); + +mutex_lock (&kvm->arch.config_lock); +if (kvm_realm_state (kvm) ==REALM_STATE_ACTIVE ) { +kvm_set_realm_state (kvm,REALM_STATE_DYING ); +WARN_ON (kvm_realm_teardown_stage2 (kvm)); + } +mutex_unlock (&kvm->arch.config_lock); +} +void kvm_uninit_stage2_mmu (struct kvm *kvm) { -kvm_free_stage2_pgd (&kvm->arch.mmu); +if (kvm_is_realm (kvm)) +kvm_realm_uninit_stage2 (&kvm->arch.mmu); +else +kvm_free_stage2_pgd (&kvm->arch.mmu);kvm_mmu_free_memory_cache (&kvm->arch.mmu.split_page_cache); }
arch/arm64/kvm/rmi.c +198 -0
227 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v16 17/45] KVM: arm64: CCA: Tear down RTTs
Quoted 15 lines · Steven Price
[...]
(quoted diff) +14
-0+static void realm_unmap_stage2(struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + + lockdep_assert_held(&kvm->arch.config_lock); + + if (realm->stage2_unmapped) + return; + + write_lock(&kvm->mmu_lock); + kvm_stage2_unmap_range(&kvm->arch.mmu, 0, + BIT(realm->ia_bits - 1), true); + write_unlock(&kvm->mmu_lock); +
Review
Quoted 10 lines
(quoted diff) +10
-0+ realm->stage2_unmapped = true; +} + +int kvm_realm_teardown_stage2(struct kvm *kvm) +{ + lockdep_assert_held(&kvm->arch.config_lock); + + realm_unmap_stage2(kvm); + return realm_destroy_rtts(kvm); +}
Hi Steven,
I noticed a warning in dmesg when destroying a realm:
[ 158.533495] Call trace: [ 158.535927] kvm_destroy_realm+0x208/0x218 [ 158.540356] kvm_arch_destroy_vm+0xc8/0xd8 [ 158.544439] kvm_put_kvm+0x324/0x4b0 [ 158.548002] kvm_vm_stats_release+0x1c/0x38
I traced this and found the following:
At this point, calling realm_destroy_rtts() may fail destroying the RTTs because RECs are still active and the Realm is not terminated, but the walk finishes and still sets realm->rtts_destroyed = true.
Later, kvm_destroy_realm() calls rmi_realm_terminate() and then calls realm_destroy_rtts(kvm) again. But because realm->rtts_destroyed is already set to true, it exits early without tearing down the RTTs.
Consequently, rmi_realm_destroy() is invoked while the RTT tables still exist in RMM, which fails and triggers WARN_ON(rmi_realm_destroy).
The fix I used was to remove realm_destroy_rtts() from kvm_realm_teardown_stage2():
arch/arm64/kvm/rmi.c +1 -1
@@ -434,7 +434,7 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)lockdep_assert_held (&kvm->arch.config_lock);realm_unmap_stage2 (kvm); -return realm_destroy_rtts (kvm); +return 0 ; }
I think we don't need RTT destruction here as it will be handled cleanly in kvm_destroy_realm() later.
Thanks,
Alper
Suzuki K Poulose
reviewerRe: [PATCH v16 17/45] KVM: arm64: CCA: Tear down RTTs
Hi there
Quoted 29 lines · Alper Gun
Quoted 15 lines · Steven Price
[...]
(quoted diff) +14
-0+static void realm_unmap_stage2(struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + + lockdep_assert_held(&kvm->arch.config_lock); + + if (realm->stage2_unmapped) + return; + + write_lock(&kvm->mmu_lock); + kvm_stage2_unmap_range(&kvm->arch.mmu, 0, + BIT(realm->ia_bits - 1), true); + write_unlock(&kvm->mmu_lock); +Review
Quoted 10 lines
(quoted diff) +10
-0+ realm->stage2_unmapped = true; +} + +int kvm_realm_teardown_stage2(struct kvm *kvm) +{ + lockdep_assert_held(&kvm->arch.config_lock); + + realm_unmap_stage2(kvm); + return realm_destroy_rtts(kvm); +}Hi Steven,
Quoted 7 lines
I noticed a warning in dmesg when destroying a realm:
[ 158.533495] Call trace: [ 158.535927] kvm_destroy_realm+0x208/0x218 [ 158.540356] kvm_arch_destroy_vm+0xc8/0xd8 [ 158.544439] kvm_put_kvm+0x324/0x4b0 [ 158.548002] kvm_vm_stats_release+0x1c/0x38
Thanks for the report and the detailed analysis.
Quoted 26 lines
I traced this and found the following:
At this point, calling
realm_destroy_rtts()may fail destroying the RTTs because RECs are still active and the Realm is not terminated, but the walk finishes and still setsrealm->rtts_destroyed= true.Later,
kvm_destroy_realm()callsrmi_realm_terminate()and then callsrealm_destroy_rtts(kvm)again. But becauserealm->rtts_destroyedis already set to true, it exits early without tearing down the RTTs.Consequently,
rmi_realm_destroy()is invoked while the RTT tables still exist in RMM, which fails and triggersWARN_ON(rmi_realm_destroy).The fix I used was to remove
realm_destroy_rtts()fromkvm_realm_teardown_stage2():arch/arm64/kvm/rmi.c +1
-1@@ -434,7 +434,7 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)lockdep_assert_held (&kvm->arch.config_lock);realm_unmap_stage2 (kvm); -return realm_destroy_rtts (kvm); +return 0 ; }
Quoted 2 lines
I think we don't need RTT destruction here as it will be handled cleanly in
kvm_destroy_realm()later.
That sounds sensible to me, I will take a look and fix it locally for the next version.
Thanks
Suzuki
Quoted 3 lines
Thanks,
Alper
Suzuki K Poulose
reviewerRe: [PATCH v16 17/45] KVM: arm64: CCA: Tear down RTTs
Quoted 34 lines · Suzuki K Poulose
Hi there
Quoted 31 lines · Alper Gun
Quoted 15 lines · Steven Price
[...]
(quoted diff) +14
-0+static void realm_unmap_stage2(struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + + lockdep_assert_held(&kvm->arch.config_lock); + + if (realm->stage2_unmapped) + return; + + write_lock(&kvm->mmu_lock); + kvm_stage2_unmap_range(&kvm->arch.mmu, 0, + BIT(realm->ia_bits - 1), true); + write_unlock(&kvm->mmu_lock); +Review
Quoted 10 lines
(quoted diff) +10
-0+ realm->stage2_unmapped = true; +} + +int kvm_realm_teardown_stage2(struct kvm *kvm) +{ + lockdep_assert_held(&kvm->arch.config_lock); + + realm_unmap_stage2(kvm); + return realm_destroy_rtts(kvm); +}Hi Steven,
I noticed a warning in dmesg when destroying a realm:
Quoted 13 lines
Quoted 5 lines
[ 158.533495] Call trace:
[ 158.535927]kvm_destroy_realm+0x208/0x218
[ 158.540356]kvm_arch_destroy_vm+0xc8/0xd8
[ 158.544439]kvm_put_kvm+0x324/0x4b0
[ 158.548002]kvm_vm_stats_release+0x1c/0x38Thanks for the report and the detailed analysis.
Quoted 5 lines
I traced this and found the following:
At this point, calling
realm_destroy_rtts()may fail destroying the RTTs because RECs are still active and the Realm is not terminated, but the
May be we should bail out early in the realm_map_ipa() based on the state of the Realm (i.e. state != ACTIVE). But looking at this I see that we serialize the state of the realm using the config_lock, which is a bit odd.
Quoted 21 lines
Quoted 21 lines
walk finishes and still sets
realm->rtts_destroyed= true.Later,
kvm_destroy_realm()callsrmi_realm_terminate()and then callsrealm_destroy_rtts(kvm)again. But becauserealm->rtts_destroyedis already set to true, it exits early without tearing down the RTTs.Consequently,
rmi_realm_destroy()is invoked while the RTT tables still exist in RMM, which fails and triggersWARN_ON(rmi_realm_destroy).The fix I used was to remove
realm_destroy_rtts()fromkvm_realm_teardown_stage2():arch/arm64/kvm/rmi.c +1
-1@@ -434,7 +434,7 @@ int kvm_realm_teardown_stage2(struct kvm *kvm) lockdep_assert_held(&kvm->arch.config_lock); realm_unmap_stage2(kvm); -return realm_destroy_rtts (kvm); +return 0 ; }
Quoted 4 lines
Quoted 2 lines
I think we don't need RTT destruction here as it will be handled cleanly in
kvm_destroy_realm()later.That sounds sensible to me, I will take a look and fix it locally for the next version.
On a second look, I think the rtts_destroyed flag may not be saving us much. If the RTTs are destroyed, we should complete the second round fairly easily.
Suzuki
Quoted 9 lines
Thanks
SuzukiQuoted 3 lines
Thanks,
Alper
Re: [PATCH v16 17/45] KVM: arm64: CCA: Tear down RTTs
Quoted 49 lines · Suzuki K Poulose
Quoted 46 lines · Suzuki K Poulose
Hi there
Quoted 30 lines · Alper Gun
Quoted 15 lines · Steven Price
[...]
(quoted diff) +14
-0+static void realm_unmap_stage2(struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + + lockdep_assert_held(&kvm->arch.config_lock); + + if (realm->stage2_unmapped) + return; + + write_lock(&kvm->mmu_lock); + kvm_stage2_unmap_range(&kvm->arch.mmu, 0, + BIT(realm->ia_bits - 1), true); + write_unlock(&kvm->mmu_lock); +Review
Quoted 10 lines
(quoted diff) +10
-0+ realm->stage2_unmapped = true; +} + +int kvm_realm_teardown_stage2(struct kvm *kvm) +{ + lockdep_assert_held(&kvm->arch.config_lock); + + realm_unmap_stage2(kvm); + return realm_destroy_rtts(kvm); +}Hi Steven,
Quoted 7 lines
I noticed a warning in dmesg when destroying a realm:
[ 158.533495] Call trace:
[ 158.535927]kvm_destroy_realm+0x208/0x218
[ 158.540356]kvm_arch_destroy_vm+0xc8/0xd8
[ 158.544439]kvm_put_kvm+0x324/0x4b0
[ 158.548002]kvm_vm_stats_release+0x1c/0x38Thanks for the report and the detailed analysis.
Quoted 3 lines
I traced this and found the following:
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
At this point, calling
realm_destroy_rtts()may fail destroying the RTTs because RECs are still active and the Realm is not terminated, but theMay be we should bail out early in the
realm_map_ipa()based on the state of the Realm (i.e. state != ACTIVE). But looking at this I see that we serialize the state of the realm using theconfig_lock, which is a bit odd.
Bailing out early in realm_map_ipa() if the VM isn't ACTIVE makes sense, I'm not sure if it's truly necessary but it's a good defensive check.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
walk finishes and still sets
realm->rtts_destroyed= true.Later,
kvm_destroy_realm()callsrmi_realm_terminate()and then callsrealm_destroy_rtts(kvm)again. But becauserealm->rtts_destroyedis already
set to true, it exits early without tearing down the RTTs.Consequently,
rmi_realm_destroy()is invoked while the RTT tables still exist in RMM, which fails and triggersWARN_ON(rmi_realm_destroy).The fix I used was to remove
realm_destroy_rtts()fromkvm_realm_teardown_stage2():arch/arm64/kvm/rmi.c +0
-0@@ -434,7 +434,7 @@ int kvm_realm_teardown_stage2(struct kvm *kvm) lockdep_assert_held(&kvm->arch.config_lock);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/rmi.c +1
-1realm_unmap_stage2(kvm); -return realm_destroy_rtts (kvm); +return 0 ; }I think we don't need RTT destruction here as it will be handled cleanly in
kvm_destroy_realm()later.
Ack - yes this is now unnecessary. Thanks for tracking this down.
Quoted 6 lines
Quoted 2 lines
That sounds sensible to me, I will take a look and fix it locally for the next version.
On a second look, I think the
rtts_destroyedflag may not be saving us much. If the RTTs are destroyed, we should complete the second round fairly easily.
Agreed, I think dropping rtts_destroyed makes sense - it's not actually helping and it's making the code confusing.
Thanks,
Steve
Quoted 12 lines
Suzuki
Quoted 9 lines
Thanks
SuzukiQuoted 3 lines
Thanks,
Alper
PATCH v16 18/45 KVM: arm64: CCA: Allocate and free RECs to match vCPUs
In reply to Steven PriceThe 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 may require additional pages (e.g. for storing larger register state for SVE). The RMM can request extra pages for this purpose using the Stateful RMI Operations (SRO) functionality to request pages during REC creation. These pages are then passed back to the host from the RMM ('reclaimed') when the REC is destroyed. The kernel tracking object (struct rmi_sro_state) is stored in the realm_rec structure to avoid memory allocation during the destruction path.
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).
Note that the function kvm_create_rec() is unused at this point, a future patch will add the call and remove the __maybe_unused attribute.
---
Changes since v15:
* Propagate negative error code in kvm_create_rec().
Changes since v14:
* Handle partial REC creation better by NULLing rec->run, rec->rec_page and rec->sro when freeing.
Changes since v13:
* Support SRO for REC creation/destruction instead of auxiliary granules.
Changes since v12:
* Use the new range-based delegation RMI.
Changes since v11:
* Remove the KVM_ARM_VCPU_REC feature. User space no longer needs to configure each VCPU separately, RECs are created on the first VCPU run of the guest.
Changes since v9:
* Size the aux_pages array according to the PAGE_SIZE of the host.
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 | 5 ++ arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/include/asm/kvm_rmi.h | 20 ++++++ arch/arm64/kvm/arm.c | 6 ++ arch/arm64/kvm/reset.c | 1 + arch/arm64/kvm/rmi.c | 103 +++++++++++++++++++++++++++ 6 files changed, 138 insertions(+)
arch/arm64/include/asm/kvm_emulate.h +5 -0
@@ -712,4 +712,9 @@ 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 (const struct kvm_vcpu *vcpu) +{ +return kvm_is_realm (vcpu->kvm); +} +#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -949,6 +949,9 @@ struct kvm_vcpu_arch {/* Hyp-readable copy of kvm_vcpu::pid */ pid_t pid; + +/* Realm meta data */ +struct realm_rec rec; };/*
arch/arm64/include/asm/kvm_rmi.h +20 -0
@@ -75,12 +75,32 @@ struct realm {bool rtts_destroyed; }; +/** +* 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 +* @rec_phys: Physical address of @rec_page +* @run: Kernel VA of the RmiRecRun structure shared with the RMM +* @run_phys: Physical address of @run +* @sro: A preallocated SRO state context +*/ +struct realm_rec { +unsigned long mpidr; +void *rec_page; +phys_addr_t rec_phys; +struct rec_run *run; +phys_addr_t run_phys; +struct rmi_sro_state *sro; +}; +void kvm_init_rmi (void );u32 kvm_rmm_ipa_limit (void );int kvm_init_realm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm);int kvm_realm_teardown_stage2 (struct kvm *kvm); +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 +6 -0
@@ -589,6 +589,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 */
@@ -1668,6 +1670,10 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,if (test_bit (KVM_ARM_VCPU_HAS_EL2 , &features))return -EINVAL ; +/* Realms are incompatible with AArch32 */ +if (vcpu_is_rec (vcpu)) +return -EINVAL ; +return 0 ; }
arch/arm64/kvm/reset.c +1 -0
@@ -161,6 +161,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)free_page ((unsigned long )vcpu->arch.ctxt.vncr_array);kfree (vcpu->arch.vncr_tlb);kfree (vcpu->arch.ccsidr); +kvm_destroy_rec (vcpu); }static void kvm_vcpu_reset_sve (struct kvm_vcpu *vcpu)
arch/arm64/kvm/rmi.c +103 -0
109 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v16 19/45 KVM: arm64: CCA: Support the VGIC in realms
In reply to Steven PriceThe RMM provides emulation of a VGIC to the realm guest. With RMM v2.0 the registers are passed in the system registers so this works similar to a normal guest, but kvm_arch_vcpu_put() need reordering to early out, and realm guests don't support GICv2 even if the host does.
---
Changes from v12:
* GIC registers are now passed in the system registers rather than via rec_entry/rec_exit which removes most of the changes.
Changes from v11:
* Minor changes to align with the previous patches. Note that the VGIC handling will change with RMM v2.0.
Changes from v10:
* Make sure we sync the VGIC v4 state, and only populate valid lrs from the list.
Changes from v9:
* Copy gicv3_vmcr from the RMM at the same time as gicv3_hcr rather than having to handle that as a special case.
Changes from v8:
* Propagate gicv3_hcr to from the RMM.
Changes from v5:
* Handle RMM providing fewer GIC LRs than the hardware supports.
--- arch/arm64/kvm/arm.c | 11 ++++++++--- arch/arm64/kvm/vgic/vgic-init.c | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-)
arch/arm64/kvm/arm.c +8 -3
@@ -791,19 +791,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);kvm_vcpu_put_debug (vcpu); + + vcpu->cpu =-1 ; + +if (vcpu_is_rec (vcpu)) +return ; +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)
arch/arm64/kvm/vgic/vgic-init.c +1 -1
@@ -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 ;/*
Signature · 1 line
2.43.0
PATCH v16 20/45 KVM: arm64: CCA: Support timers in realm RECs
In reply to Steven PriceThe 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.
A later patch adds the support for propagating the timer values from the exit data structure and calling kvm_realm_timers_update().
---
Changes since v15:
* Major rewrite to make better use of the KVM infrastructure.
Changes since v14:
* Special case in kvm_timer_vcpu_load()/kvm_timer_vcpu_put() the timer handling.
Changes since v12:
* Adapt to upstream changes.
Changes since v11:
* Drop the kvm_is_realm() check from timer_set_offset(). We already ensure that the offset is 0 when calling the function.
Changes since v10:
* KVM_CAP_COUNTER_OFFSET is now already hidden by a previous patch.
Changes since v9:
* No need to move the call to kvm_timer_unblocking() in kvm_timer_vcpu_load().
Changes since v7:
* Hide KVM_CAP_COUNTER_OFFSET for realm guests.
--- arch/arm64/kvm/arch_timer.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)
arch/arm64/kvm/arch_timer.c +28 -6
@@ -56,11 +56,22 @@ static unsigned long kvm_arch_timer_get_irq_flags(void)return kvm_vgic_global_state.no_hw_deactivation ?VGIC_IRQ_SW_RESAMPLE :0 ; } +static unsigned long kvm_realm_timer_get_irq_flags (void ) +{ +/* RMI_REC_ENTER rejects LRs which have the HW bit set. */ +return VGIC_IRQ_SW_RESAMPLE ; +} +static const struct irq_ops arch_timer_irq_ops = { .get_flags = kvm_arch_timer_get_irq_flags, .get_input_level = kvm_arch_timer_get_input_level, }; +static const struct irq_ops realm_timer_irq_ops = { + .get_flags = kvm_realm_timer_get_irq_flags, + .get_input_level = kvm_arch_timer_get_input_level, +}; +static const struct irq_ops arch_timer_irq_ops_vgic_v5 = { .get_input_level = kvm_arch_timer_get_input_level, .queue_irq_unlock = vgic_v5_ppi_queue_irq_unlock,
@@ -1073,7 +1084,8 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid) ctxt->timer_id = timerid; -if (!kvm_vm_is_protected (vcpu->kvm)) { +/* The RMM architecture fixes both Realm counter offsets at zero. */ +if (!kvm_vm_is_protected (vcpu->kvm) && !vcpu_is_rec (vcpu)) {if (timerid ==TIMER_VTIMER ) ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;else
@@ -1103,8 +1115,11 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)for (int i =0 ; i <NR_KVM_TIMERS ; i++)timer_context_init (vcpu, i); -/* Synchronize offsets across timers of a VM if not already provided */ -if (!vcpu_is_protected (vcpu) && +/* +* Synchronize offsets across timers of a VM if not already provided. +* Realm counter offsets are fixed at zero by the RMM architecture. +*/ +if (!vcpu_is_protected (vcpu) && !vcpu_is_rec (vcpu) && !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_ptimer (vcpu),0 );
@@ -1602,8 +1617,15 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)get_timer_map (vcpu, &map); - ops =vgic_is_v5 (vcpu->kvm) ? &arch_timer_irq_ops_vgic_v5 : - &arch_timer_irq_ops; +/* +* RMI_REC_ENTER rejects LRs with the HW bit set, so use the existing +* software resampling mechanism for Realm timer interrupts. +*/ +if (vcpu_is_rec (vcpu)) + ops = &realm_timer_irq_ops; +else + ops =vgic_is_v5 (vcpu->kvm) ? &arch_timer_irq_ops_vgic_v5 : + &arch_timer_irq_ops;for (int i =0 ; i <nr_timers (vcpu); i++)kvm_vgic_set_irq_ops (vcpu,timer_irq (vcpu_get_timer (vcpu, i)), ops);
@@ -1729,7 +1751,7 @@ int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm,if (offset->reserved)return -EINVAL ; -if (kvm_vm_is_protected (kvm)) +if (kvm_vm_is_protected (kvm) ||kvm_is_realm (kvm))return -EINVAL ;mutex_lock (&kvm->lock);
Signature · 1 line
2.43.0
PATCH v16 21/45 KVM: arm64: CCA: Handle realm enter/exit
In reply to Steven PriceEntering 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 v15:
* Major rewrite to use KVM requests and fit in better with the existing KVM code.
Changes since v13:
* The RMM is now required to provide an ESR value with the correct information to emulate MMIO, so we no longer need to hardcode 0s in rec_exit_sys_reg().
* The PSCI changes mean that there is a potential race when turning on a VCPU which can cause a RMI_ERROR_REC return. Exit to user space with -EAGAIN in this case.
Changes since v12:
* Call guest_state_{enter,exit}_irqoff() around rmi_rec_enter().
* Add handling of the IRQ exception case where IRQs need to be briefly enabled before exiting guest timing.
Changes since v8:
* Introduce kvm_rec_pre_enter() called before entering an atomic section to handle operations that might require memory allocation (specifically completing a RIPAS change introduced in a later patch).
* Updates to align with upstream changes to hpfar_el2 which now (ab)uses HPFAR_EL2_NS as a valid flag.
* Fix exit reason when racing with PSCI shutdown to return KVM_EXIT_SHUTDOWN rather than KVM_EXIT_UNKNOWN.
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_asm.h | 2 + arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_rmi.h | 5 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 18 +++- arch/arm64/kvm/handle_exit.c | 14 +++ arch/arm64/kvm/rmi-exit.c | 167 ++++++++++++++++++++++++++++++ arch/arm64/kvm/rmi.c | 56 ++++++++++ 8 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 arch/arm64/kvm/rmi-exit.c
arch/arm64/include/asm/kvm_asm.h +2 -0
@@ -21,6 +21,7 @@#define ARM_EXCEPTION_EL1_SERROR 1#define ARM_EXCEPTION_TRAP 2#define ARM_EXCEPTION_IL 3 +#define ARM_EXCEPTION_EXIT 4/* The hyp-stub will return this for any kvm_call_hyp() call */ #define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR
@@ -28,6 +29,7 @@ {ARM_EXCEPTION_IRQ, "IRQ" }, \ {ARM_EXCEPTION_EL1_SERROR, "SERROR" }, \ {ARM_EXCEPTION_TRAP, "TRAP" }, \ + {ARM_EXCEPTION_EXIT, "EXIT" }, \ {ARM_EXCEPTION_HYP_GONE, "HYP_GONE" }/*
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -56,6 +56,7 @@#define KVM_REQ_GUEST_HYP_IRQ_PENDING KVM_ARCH_REQ(9)#define KVM_REQ_MAP_L1_VNCR_EL2 KVM_ARCH_REQ(10)#define KVM_REQ_VGIC_PROCESS_UPDATE KVM_ARCH_REQ(11) +#define KVM_REQ_RMI KVM_ARCH_REQ(12)#define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ KVM_DIRTY_LOG_INITIALLY_SET)
arch/arm64/include/asm/kvm_rmi.h +5 -0
@@ -102,6 +102,11 @@ void kvm_destroy_realm(struct kvm *kvm);int kvm_realm_teardown_stage2 (struct kvm *kvm);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +int kvm_rec_enter (struct kvm_vcpu *vcpu); +int kvm_rec_exit (struct kvm_vcpu *vcpu,int rec_run_status); +int kvm_rec_handle_request (struct kvm_vcpu *vcpu); +bool kvm_rec_handle_hvc (struct kvm_vcpu *vcpu,int *ret); +static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr) {
arch/arm64/kvm/Makefile +1 -1
@@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ inject_fault.o va_layout.o handle_exit.o config.o \ guest.o debug.o reset.o sys_regs.o stacktrace.o \ - vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \ + vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o rmi-exit.o \ arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \ vgic/vgic.o vgic/vgic-init.o \ vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/arm.c +17 -1
@@ -1206,6 +1206,13 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_SUSPEND , vcpu))return kvm_vcpu_suspend (vcpu); +if (kvm_check_request (KVM_REQ_RMI , vcpu)) { +int ret =kvm_rec_handle_request (vcpu); + +if (ret <=0 ) +return ret; + } +if (kvm_dirty_ring_check_request (vcpu))return 0 ;
@@ -1291,9 +1298,18 @@ static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)int ret;guest_state_enter_irqoff (); - ret =kvm_call_hyp_ret (__kvm_vcpu_run, vcpu); +if (vcpu_is_rec (vcpu)) + ret =kvm_rec_enter (vcpu); +else + ret =kvm_call_hyp_ret (__kvm_vcpu_run, vcpu);guest_state_exit_irqoff (); +if (vcpu_is_rec (vcpu)) { +instrumentation_begin (); + ret =kvm_rec_exit (vcpu, ret); +instrumentation_end (); + } +return ret; }
arch/arm64/kvm/handle_exit.c +14 -0
@@ -18,6 +18,7 @@#include <asm/kvm_emulate.h> #include <asm/kvm_mmu.h> #include <asm/kvm_nested.h> +#include <asm/kvm_rmi.h> #include <asm/debug-monitors.h> #include <asm/stacktrace/nvhe.h> #include <asm/traps.h>
@@ -37,10 +38,15 @@ static void kvm_handle_guest_serror(struct kvm_vcpu *vcpu, u64 esr)static int handle_hvc (struct kvm_vcpu *vcpu) { +int ret; +trace_kvm_hvc_arm64 (*vcpu_pc (vcpu),vcpu_get_reg (vcpu,0 ),kvm_vcpu_hvc_get_imm (vcpu)); vcpu->stat.hvc_exit_stat++; +if (kvm_rec_handle_hvc (vcpu, &ret)) +return ret; +/* Forward hvc instructions to the virtual EL2 if the guest has EL2. */ if (vcpu_has_nv (vcpu)) {if (vcpu_read_sys_reg (vcpu,HCR_EL2 ) &HCR_HCD )
@@ -447,6 +453,9 @@ int handle_exit(struct kvm_vcpu *vcpu, int exception_index) {struct kvm_run *run = vcpu->run; +if (exception_index <0 ) +return exception_index; +if (ARM_SERROR_PENDING (exception_index)) {/* * The SError is handled by handle_exit_early(). If the guest
@@ -478,6 +487,8 @@ int handle_exit(struct kvm_vcpu *vcpu, int exception_index)*/ run->exit_reason =KVM_EXIT_FAIL_ENTRY ;return -EINVAL ; +case ARM_EXCEPTION_EXIT : +return 0 ;default :kvm_pr_unimpl ("Unsupported exception type: %d" , exception_index);
@@ -489,6 +500,9 @@ int handle_exit(struct kvm_vcpu *vcpu, int exception_index)/* For exit types that need handling before we can be preempted */ void handle_exit_early (struct kvm_vcpu *vcpu,int exception_index) { +if (exception_index <0 || exception_index ==ARM_EXCEPTION_EXIT ) +return ; +if (ARM_SERROR_PENDING (exception_index)) {if (this_cpu_has_cap (ARM64_HAS_RAS_EXTN )) {u64 disr =kvm_vcpu_get_disr (vcpu);
arch/arm64/kvm/rmi-exit.c new file +167 -0
167 lines not on this page · raw message
arch/arm64/kvm/rmi.c +56 -0
68 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v16 21/45] KVM: arm64: CCA: Handle realm enter/exit
Quoted 12 lines · Steven Price
arch/arm64/kvm/rmi-exit.c new file +12
-0+switch (rec->run->exit.exit_reason) { +case RMI_EXIT_SYNC : +/* +* HPFAR_EL2_NS is hijacked to indicate a valid HPFAR value, +* see __get_fault_info() +*/ + vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar |HPFAR_EL2_NS ; +rec_exit_sync (vcpu); +return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_IRQ : +case RMI_EXIT_FIQ : +return ARM_EXCEPTION_IRQ ;
arch/arm64/kvm/rmi-exit.c line 163
Quoted 10 lines
arch/arm64/kvm/rmi-exit.c new file +10
-0+case RMI_EXIT_SERROR : +return ARM_EXCEPTION_EL1_SERROR ; +case RMI_EXIT_PSCI : +rec_exit_hvc (vcpu); +kvm_make_request (KVM_REQ_RMI , vcpu); +return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_RIPAS_CHANGE : +rec_exit_hvc (vcpu); +return ARM_EXCEPTION_TRAP ; + }
Should we handle RMI_EXIT_RIPAS_CHANGE differently? Currently, the call path is:
kvm_rec_exit()
-> RMI_EXIT_RIPAS_CHANGE
-> handle_hvc()
-> kvm_rec_handle_hvc()
-> kvm_prepare_memory_fault_exit()
Could we instead treat RMI_EXIT_RIPAS_CHANGE as a special case and avoid redirecting it through handle_hvc()? The completion part of the RIPAS change could still be handled by kvm_rec_handle_request().
-bool kvm_rec_handle_hvc(struct kvm_vcpu *vcpu, int *ret) +static int rec_exit_ripas_change(struct kvm_vcpu *vcpu)
{
- struct realm_rec *rec;
+ struct realm_rec *rec = &vcpu->arch.rec;
struct realm *realm;
unsigned long base;
unsigned long ripas;
unsigned long top;
- if (!vcpu_is_rec(vcpu))
- return false;
-
- rec = &vcpu->arch.rec;
- if (rec->run->exit.exit_reason != RMI_EXIT_RIPAS_CHANGE)
- return false;
-
realm = &vcpu->kvm->arch.realm;
base = rec->run->exit.ripas_base;
top = rec->run->exit.ripas_top;
@@ -89,8 +83,7 @@ bool kvm_rec_handle_hvc(struct kvm_vcpu *vcpu, int *ret)
base, top, ripas);
/* Set RMI_REJECT bit */
rec->run->enter.flags = REC_ENTER_FLAG_RIPAS_RESPONSE;
- *ret = -EINVAL;
- return true;
+ return -EINVAL;
}
/* Exit to VMM, the actual RIPAS change is done on next entry */
@@ -102,8 +95,7 @@ bool kvm_rec_handle_hvc(struct kvm_vcpu *vcpu, int *ret)
* KVM_EXIT_MEMORY_FAULT requires a return code of -EFAULT, see the
* API documentation
*/
- *ret = -EFAULT;
- return true;
+ return -EFAULT;
}
int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)
@@ -169,8 +161,7 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)
kvm_make_request(KVM_REQ_RMI, vcpu);
return ARM_EXCEPTION_TRAP;
case RMI_EXIT_RIPAS_CHANGE:
- rec_exit_hvc(vcpu);
- return ARM_EXCEPTION_TRAP;
+ return rec_exit_ripas_change(vcpu);
}
return rec_exit_fatal(vcpu, "Unsupported Realm exit reason",
-aneesh
Re: [PATCH v16 21/45] KVM: arm64: CCA: Handle realm enter/exit
Quoted 33 lines · Aneesh Kumar K.V
Quoted 12 lines · Steven Price
arch/arm64/kvm/rmi-exit.c new file +12
-0+switch (rec->run->exit.exit_reason) { +case RMI_EXIT_SYNC : +/* +* HPFAR_EL2_NS is hijacked to indicate a valid HPFAR value, +* see __get_fault_info() +*/ + vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar |HPFAR_EL2_NS ; +rec_exit_sync (vcpu); +return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_IRQ : +case RMI_EXIT_FIQ : +return ARM_EXCEPTION_IRQ ;arch/arm64/kvm/rmi-exit.c line 163
Quoted 10 lines
arch/arm64/kvm/rmi-exit.c new file +10
-0+case RMI_EXIT_SERROR : +return ARM_EXCEPTION_EL1_SERROR ; +case RMI_EXIT_PSCI : +rec_exit_hvc (vcpu); +kvm_make_request (KVM_REQ_RMI , vcpu); +return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_RIPAS_CHANGE : +rec_exit_hvc (vcpu); +return ARM_EXCEPTION_TRAP ; + }Should we handle
RMI_EXIT_RIPAS_CHANGEdifferently? Currently, the call path is:
kvm_rec_exit()
->RMI_EXIT_RIPAS_CHANGE
->handle_hvc()
->kvm_rec_handle_hvc()
->kvm_prepare_memory_fault_exit()
Quoted 3 lines
Could we instead treat
RMI_EXIT_RIPAS_CHANGEas a special case and avoid redirecting it throughhandle_hvc()? The completion part of the RIPAS change could still be handled bykvm_rec_handle_request().
Yes that seems like it's slightly neater.
Thanks,
Steve
Quoted 53 lines
(quoted diff) +5
-16-bool kvm_rec_handle_hvc(struct kvm_vcpu *vcpu, int *ret) +static int rec_exit_ripas_change(struct kvm_vcpu *vcpu) { - struct realm_rec *rec; + struct realm_rec *rec = &vcpu->arch.rec; struct realm *realm; unsigned long base; unsigned long ripas; unsigned long top; - if (!vcpu_is_rec(vcpu)) - return false; - - rec = &vcpu->arch.rec; - if (rec->run->exit.exit_reason != RMI_EXIT_RIPAS_CHANGE) - return false; - realm = &vcpu->kvm->arch.realm; base = rec->run->exit.ripas_base; top = rec->run->exit.ripas_top;@@ -89,8 +83,7 @@ bool kvm_rec_handle_hvc(struct kvm_vcpu *vcpu, int *ret) base, top, ripas); /* Set RMI_REJECT bit */ rec->run->enter.flags = REC_ENTER_FLAG_RIPAS_RESPONSE; - *ret = -EINVAL; - return true; + return -EINVAL; } /* Exit to VMM, the actual RIPAS change is done on next entry */@@ -102,8 +95,7 @@ bool kvm_rec_handle_hvc(struct kvm_vcpu *vcpu, int *ret) * KVM_EXIT_MEMORY_FAULT requires a return code of -EFAULT, see the * API documentation */ - *ret = -EFAULT; - return true; + return -EFAULT; } int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)@@ -169,8 +161,7 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret) kvm_make_request(KVM_REQ_RMI, vcpu); return ARM_EXCEPTION_TRAP; case RMI_EXIT_RIPAS_CHANGE: - rec_exit_hvc(vcpu); - return ARM_EXCEPTION_TRAP; + return rec_exit_ripas_change(vcpu); } return rec_exit_fatal(vcpu, "Unsupported Realm exit reason", -aneesh
Re: [PATCH v16 21/45] KVM: arm64: CCA: Handle realm enter/exit
In reply to Steven PriceQuoted 9 lines · Steven Price
arch/arm64/kvm/rmi-exit.c new file +8
-0+ +switch (rec->run->exit.exit_reason) { +case RMI_EXIT_SYNC : +/* +* HPFAR_EL2_NS is hijacked to indicate a valid HPFAR value, +* see __get_fault_info() +*/ + vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar |HPFAR_EL2_NS ;
arch/arm64/kvm/rmi-exit.c line 158
Quoted 10 lines
arch/arm64/kvm/rmi-exit.c new file +10
-0+rec_exit_sync (vcpu); +return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_IRQ : +case RMI_EXIT_FIQ : +return ARM_EXCEPTION_IRQ ; +case RMI_EXIT_SERROR : +return ARM_EXCEPTION_EL1_SERROR ; +case RMI_EXIT_PSCI : +rec_exit_hvc (vcpu); +kvm_make_request (KVM_REQ_RMI , vcpu);
I'm also wondering whether we can move the kvm_make_request() call until after the PSCI/HOST_CALL handling is complete. I tried a quick hack, but it did not look particularly clean, since we would end up adding RMM-specific checks in handle_hvc():
ret = kvm_smccc_call_handler(vcpu);
if (vcpu_is_rec(vcpu) &&
(vcpu->arch.rec.run->exit.exit_reason == RMI_EXIT_PSCI ||
vcpu->arch.rec.run->exit.exit_reason == RMI_EXIT_HOST_CALL))
kvm_make_request(KVM_REQ_RMI, vcpu);That said, having rec_exit_hvc(vcpu) and kvm_make_request() back-to-back is also somewhat confusing. Perhaps we could at least add a comment explaining why the request must always be made here.
arch/arm64/kvm/rmi-exit.c line 167
Quoted 9 lines
arch/arm64/kvm/rmi-exit.c new file +9
-0+return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_RIPAS_CHANGE : +rec_exit_hvc (vcpu); +return ARM_EXCEPTION_TRAP ; + } + +return rec_exit_fatal (vcpu,"Unsupported Realm exit reason" , + rec->run->exit.exit_reason); +}
-aneesh
Re: [PATCH v16 21/45] KVM: arm64: CCA: Handle realm enter/exit
Quoted 28 lines · Aneesh Kumar K.V
Quoted 9 lines · Steven Price
arch/arm64/kvm/rmi-exit.c new file +8
-0+ +switch (rec->run->exit.exit_reason) { +case RMI_EXIT_SYNC : +/* +* HPFAR_EL2_NS is hijacked to indicate a valid HPFAR value, +* see __get_fault_info() +*/ + vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar |HPFAR_EL2_NS ;arch/arm64/kvm/rmi-exit.c line 158
Quoted 10 lines
arch/arm64/kvm/rmi-exit.c new file +10
-0+rec_exit_sync (vcpu); +return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_IRQ : +case RMI_EXIT_FIQ : +return ARM_EXCEPTION_IRQ ; +case RMI_EXIT_SERROR : +return ARM_EXCEPTION_EL1_SERROR ; +case RMI_EXIT_PSCI : +rec_exit_hvc (vcpu); +kvm_make_request (KVM_REQ_RMI , vcpu);I'm also wondering whether we can move the
kvm_make_request()call until after the PSCI/HOST_CALL handling is complete. I tried a quick hack, but it did not look particularly clean, since we would end up adding RMM-specific checks inhandle_hvc():ret =
kvm_smccc_call_handler(vcpu);
Quoted 8 lines
if (
vcpu_is_rec(vcpu)&&(vcpu->arch.rec.run->exit.exit_reason == RMI_EXIT_PSCI || vcpu->arch.rec.run->exit.exit_reason == RMI_EXIT_HOST_CALL)) kvm_make_request(KVM_REQ_RMI, vcpu);That said, having
rec_exit_hvc(vcpu)andkvm_make_request()back-to-back is also somewhat confusing. Perhaps we could at least add a comment explaining why the request must always be made here.
Agreed, a comment would make it clearer what's going on. I'll add that. As you say I'm not convinced trying to delay the kvm_make_request() call is the answer because it ends up with an ugly RMM-specific check.
Thanks,
Steve
Quoted 12 lines
arch/arm64/kvm/rmi-exit.c line 167
Quoted 10 lines
arch/arm64/kvm/rmi-exit.c new file +9
-0+return ARM_EXCEPTION_TRAP ; +case RMI_EXIT_RIPAS_CHANGE : +rec_exit_hvc (vcpu); +return ARM_EXCEPTION_TRAP ; + } + +return rec_exit_fatal (vcpu,"Unsupported Realm exit reason" , + rec->run->exit.exit_reason); +}-aneesh
Re: [PATCH v16 21/45] KVM: arm64: CCA: Handle realm enter/exit
In reply to Steven PriceQuoted 4 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.
Hi Steven,
I found that when the host kernel is booting with pseudo-NMI enabled (irqchip.gicv3_pseudo_nmi=1), Realm VMs fail boot successfully and the watchdog reports RCU stalls and soft lockups.
After some investigations, I confirmed that arch_timer interrupts are not being delivered to some CPUs. This appears to be because PMR is 000000c0 (GICV3_PRIO_IRQ), as shown below:
[ 248.769692] pmr: 000000c0
For normal VMs, KVM opens PMR before entering the guest, because having IRQs masked via PMR when entering the guest means the GIC will not signal the CPU of interrupts of lower priority, and in the worst case a guest exit may never occur.
(See __kvm_vcpu_run in arch/arm64/kvm/hyp/vhe/switch.c)
[ 248.756980] rcu: INFO: rcu_preempt detected stalls on CPUs/tasks: [ 248.758649] rcu: 3-...0: (113 ticks this GP) idle=d2ec/1/0x4000000000000000 softirq=1631/1633 fqs=297 [ 248.759413] rcu: 4-...0: (7 ticks this GP) idle=b77c/1/0x4000000000000000 softirq=1589/1589 fqs=297 [ 248.760101] rcu: (detected by 7, t=6003 jiffies, g=4553, q=86 ncpus=8) [ 248.760731] Sending NMI from CPU 7 to CPUs 3: [ 248.766814] NMI backtrace for cpu 3 [ 248.768285] CPU: 3 UID: 0 PID: 387 Comm: kvm-vcpu-0 Not tainted 7.2.0-rc2-00267-g7326f0114689 #238 PREEMPT(lazy) [ 248.768681] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 02/02/2022 [ 248.768936] pstate: 61402009 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 248.769021] pc : kvm_rec_enter+0xa0/0xb8 [ 248.769585] lr : kvm_rec_enter+0x24/0xb8 [ 248.769655] sp : ffff800084943830 [ 248.769692] pmr: 000000c0 [ 248.769757] x29: ffff800084943830 x28: ffff0000079c6c00 x27: 0000000000000000 [ 248.770441] x26: 0000000000000000 x25: 0000000000000000 x24: 0000000000000000 [ 248.770536] x23: 0000000080000000 x22: 0000000000000001 x21: 0000000049d99000 [ 248.770590] x20: 0000000049d9a000 x19: ffff00000e018000 x18: 0000000000000000 [ 248.770647] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000 [ 248.770756] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000 [ 248.770863] x11: 0000000000000000 x10: 0000000000000000 x9 : ffff8000812ab1bc [ 248.771018] x8 : 0000000000000000 x7 : 0000000000000020 x6 : 0000000000000080 [ 248.771123] x5 : 0000000000000004 x4 : 0000000000000040 x3 : 0000000000000000 [ 248.771172] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000000 [ 248.771385] Call trace: [ 248.771612] kvm_rec_enter+0xa0/0xb8 (P) [ 248.771757] kvm_arm_vcpu_enter_exit+0x8c/0x218 [ 248.771796] kvm_arch_vcpu_ioctl_run+0x274/0x890 [ 248.771843] kvm_vcpu_ioctl+0x180/0xb50 [ 248.771883] __arm64_sys_ioctl+0xb4/0x118 [ 248.771920] invoke_syscall.constprop.0+0xb8/0x120 [ 248.771954] do_el0_svc+0x48/0xc8 [ 248.771982] el0_svc+0x48/0x280 [ 248.772012] el0t_64_sync_handler+0xa0/0xe8 [ 248.772041] el0t_64_sync+0x1ac/0x1b0
[...]
Review
Quoted 12 lines
[...]
(quoted diff) +11
-0+int noinstr kvm_rec_enter(struct kvm_vcpu *vcpu) +{ + struct realm_rec *rec = &vcpu->arch.rec; + int ret; + + ret = rmi_rec_enter(rec->rec_phys, rec->run_phys); + if (!ret) + load_realm_timer_state(vcpu); + + return ret; +}
In my testing environment, adding local_daif_mask/restore() in line with __kvm_vcpu_run() fixes the issue, and Realm VMs successfully boot without RCU stalls or soft lockups.
However, I'm not sure if we can safely call local_daif_mask/restore() here, bacause they call trace_hardirqs_off/on(), which presumably cannot be called from noinstr context.
For reference, the VHE hyp path seems to call local_daif_mask/restore() from noinstr context, but I'm not sure why this is considered safe:
noinstr kvm_arm_vcpu_enter_exit()
kvm_call_hyp_ret(__kvm_vcpu_run, vcpu) <- normal function call for VHE
local_daif_mask()
trace_hardirqs_off()
local_daif_restore()
trace_hardirqs_off/on()
The following change works in my test environment. Any thoughts on the issue and the proposed fix?
arch/arm64/kvm/rmi.c +6 -0
@@ -1307,7 +1307,13 @@ int noinstr kvm_rec_enter(struct kvm_vcpu *vcpu)struct realm_rec *rec = &vcpu->arch.rec;int ret; +local_daif_mask (); +pmr_sync (); + ret =rmi_rec_enter (rec->rec_phys, rec->run_phys); + +local_daif_restore (DAIF_PROCCTX_NOIRQ ); +if (!ret)load_realm_timer_state (vcpu);
Thanks,
Kohei
Re: [PATCH v16 21/45] KVM: arm64: CCA: Handle realm enter/exit
Quoted 60 lines · Kohei Enju
Quoted 4 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.
Hi Steven,
I found that when the host kernel is booting with pseudo-NMI enabled (irqchip.gicv3_pseudo_nmi=1), Realm VMs fail boot successfully and the watchdog reports RCU stalls and soft lockups.
After some investigations, I confirmed that
arch_timerinterrupts are not being delivered to some CPUs. This appears to be because PMR is000000c0(GICV3_PRIO_IRQ), as shown below:[ 248.769692] pmr: 000000c0For normal VMs, KVM opens PMR before entering the guest, because having IRQs masked via PMR when entering the guest means the GIC will not signal the CPU of interrupts of lower priority, and in the worst case a guest exit may never occur.
(See__kvm_vcpu_runinarch/arm64/kvm/hyp/vhe/switch.c)[ 248.756980] rcu: INFO: rcu_preempt detected stalls on CPUs/tasks: [ 248.758649] rcu: 3-...0: (113 ticks this GP) idle=d2ec/1/0x4000000000000000 softirq=1631/1633 fqs=297 [ 248.759413] rcu: 4-...0: (7 ticks this GP) idle=b77c/1/0x4000000000000000 softirq=1589/1589 fqs=297 [ 248.760101] rcu: (detected by 7, t=6003 jiffies, g=4553, q=86 ncpus=8) [ 248.760731] Sending NMI from CPU 7 to CPUs 3: [ 248.766814] NMI backtrace for cpu 3 [ 248.768285] CPU: 3 UID: 0 PID: 387 Comm: kvm-vcpu-0 Not tainted 7.2.0-rc2-00267-g7326f0114689 #238 PREEMPT(lazy) [ 248.768681] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 02/02/2022 [ 248.768936] pstate: 61402009 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 248.769021] pc : kvm_rec_enter+0xa0/0xb8 [ 248.769585] lr : kvm_rec_enter+0x24/0xb8 [ 248.769655] sp : ffff800084943830 [ 248.769692] pmr: 000000c0 [ 248.769757] x29: ffff800084943830 x28: ffff0000079c6c00 x27: 0000000000000000 [ 248.770441] x26: 0000000000000000 x25: 0000000000000000 x24: 0000000000000000 [ 248.770536] x23: 0000000080000000 x22: 0000000000000001 x21: 0000000049d99000 [ 248.770590] x20: 0000000049d9a000 x19: ffff00000e018000 x18: 0000000000000000 [ 248.770647] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000 [ 248.770756] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000 [ 248.770863] x11: 0000000000000000 x10: 0000000000000000 x9 : ffff8000812ab1bc [ 248.771018] x8 : 0000000000000000 x7 : 0000000000000020 x6 : 0000000000000080 [ 248.771123] x5 : 0000000000000004 x4 : 0000000000000040 x3 : 0000000000000000 [ 248.771172] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000000 [ 248.771385] Call trace: [ 248.771612] kvm_rec_enter+0xa0/0xb8 (P) [ 248.771757] kvm_arm_vcpu_enter_exit+0x8c/0x218 [ 248.771796] kvm_arch_vcpu_ioctl_run+0x274/0x890 [ 248.771843] kvm_vcpu_ioctl+0x180/0xb50 [ 248.771883] __arm64_sys_ioctl+0xb4/0x118 [ 248.771920] invoke_syscall.constprop.0+0xb8/0x120 [ 248.771954] do_el0_svc+0x48/0xc8 [ 248.771982] el0_svc+0x48/0x280 [ 248.772012] el0t_64_sync_handler+0xa0/0xe8 [ 248.772041] el0t_64_sync+0x1ac/0x1b0[...]
Quoted 16 lines
Review
Quoted 12 lines
[...]
(quoted diff) +11
-0+int noinstr kvm_rec_enter(struct kvm_vcpu *vcpu) +{ + struct realm_rec *rec = &vcpu->arch.rec; + int ret; + + ret = rmi_rec_enter(rec->rec_phys, rec->run_phys); + if (!ret) + load_realm_timer_state(vcpu); + + return ret; +}In my testing environment, adding local_daif_mask/restore() in line with
__kvm_vcpu_run()fixes the issue, and Realm VMs successfully boot without RCU stalls or soft lockups.
Hi Kohei,
Thanks for the detailed investigation.
Quoted 4 lines
However, I'm not sure if we can safely call local_daif_mask/restore() here, bacause they call trace_hardirqs_off/on(), which presumably cannot be called from noinstr context.
Quoted 8 lines
For reference, the VHE hyp path seems to call local_daif_mask/restore() from noinstr context, but I'm not sure why this is considered safe:
noinstrkvm_arm_vcpu_enter_exit()
kvm_call_hyp_ret(__kvm_vcpu_run, vcpu)<- normal function call for VHE
local_daif_mask()
trace_hardirqs_off()
local_daif_restore()
trace_hardirqs_off/on()
Yes I'm not sure if there's an existing VHE bug there. But it's only if CONFIG_TRACE_IRQFLAGS is enabled, so perhaps no-one has noticed?
Quoted 2 lines
The following change works in my test environment. Any thoughts on the issue and the proposed fix?
Your proposed fix looks like it matches the VHE behaviour, so I'll go for that. If there's an existing VHE bug then hopefully the below will work after the fix for that too.
Thanks,
Steve
Quoted 20 lines
arch/arm64/kvm/rmi.c +6
-0@@ -1307,7 +1307,13 @@ int noinstr kvm_rec_enter(struct kvm_vcpu *vcpu)struct realm_rec *rec = &vcpu->arch.rec;int ret; +local_daif_mask (); +pmr_sync (); + ret =rmi_rec_enter (rec->rec_phys, rec->run_phys); + +local_daif_restore (DAIF_PROCCTX_NOIRQ ); +if (!ret)load_realm_timer_state (vcpu);Thanks,
Kohei
PATCH v16 22/45 KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
In reply to Steven PriceThe guest can request that a region of its 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 this 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 v15:
* Propagate negative error returns.
Changes since v14:
* Use addition rather than bitwise OR for adding the shared_bit in realm_unmap_shared_range(), this handles the case where the region includes the last address (which means 'end' already has the bit set).
Changes since v13:
* Switch to the new RMI_RTT_UNPROT_UNMAP range-based API.
* Drop ugly hack for RMM bug which errored when the RIPAS was already set to the desired value.
Changes since v12:
* Switch to the new RMM v2.0 RMI_RTT_DATA_UNMAP which can unmap an address range.
Changes since v11:
* Combine the "Allow VMM to set RIPAS" patch into this one to avoid adding functions before they are used.
* Drop the CAP for setting RIPAS and adapt to changes from previous patches.
Changes since v10:
* Add comment explaining the assignment of rec->run->exit.ripas_base in kvm_complete_ripas_change().
Changes since v8:
* Make use of ripas_change() from a previous patch to implement realm_set_ipa_state().
* Update exit.ripas_base after a RIPAS change so that, if instead of entering the guest we exit to user space, we don't attempt to repeat the RIPAS change (triggering an error from the RMM).
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/include/asm/kvm_rmi.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rmi.c | 473 +++++++++++++++++++++++++++++++ 3 files changed, 484 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +6 -0
@@ -107,6 +107,12 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);int kvm_rec_handle_request (struct kvm_vcpu *vcpu);bool kvm_rec_handle_hvc (struct kvm_vcpu *vcpu,int *ret); +void kvm_realm_unmap_range (struct kvm *kvm, +unsigned long ipa, +unsigned long size, +bool unmap_private, +bool may_block); +static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr) {
arch/arm64/kvm/mmu.c +5 -3
@@ -319,6 +319,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
@@ -326,7 +327,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;
@@ -343,7 +344,7 @@ void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,if (kvm_vm_is_protected (kvm_s2_mmu_to_kvm (mmu)))return ; -__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)
@@ -2467,7 +2468,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/rmi.c +473 -0
509 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
Quoted 63 lines · Steven Price
The guest can request that a region of its protected address space is switched between
RIPAS_RAMandRIPAS_EMPTY(and back) usingRSI_IPA_STATE_SET. This causes a guest exit with theRMI_EXIT_RIPAS_CHANGEcode. 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 theguest_memfdand memslot mappings. On the next entry the RIPAS changes are committed by makingRMI_RTT_SET_RIPAScalls.The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do this 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_RIPASchanges to entry we leave the door open for adding a new ioctl in the future for this purpose.Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Propagate negative error returns.
Changes since v14:
* Use addition rather than bitwise OR for adding theshared_bitinrealm_unmap_shared_range(), this handles the case where the region includes the last address (which means 'end' already has the bit set).
Changes since v13:
* Switch to the newRMI_RTT_UNPROT_UNMAPrange-based API.
* Drop ugly hack for RMM bug which errored when the RIPAS was already set to the desired value.
Changes since v12:
* Switch to the new RMM v2.0RMI_RTT_DATA_UNMAPwhich can unmap an address range.
Changes since v11:
* Combine the "Allow VMM to set RIPAS" patch into this one to avoid adding functions before they are used.
* Drop the CAP for setting RIPAS and adapt to changes from previous patches.
Changes since v10:
* Add comment explaining the assignment ofrec->run->exit.ripas_baseinkvm_complete_ripas_change().
Changes since v8:
* Make use ofripas_change()from a previous patch to implementrealm_set_ipa_state().
* Update exit.ripas_base after a RIPAS change so that, if instead of entering the guest we exit to user space, we don't attempt to repeat the RIPAS change (triggering an error from the RMM).
Changes since v7:
* Rework the loop inrealm_set_ipa_state()to make it clear when the 'next' output value ofrmi_rtt_set_ripas()is used.
New patch for v7: The code was previously split awkwardly between two other patches.--- arch/arm64/include/asm/kvm_rmi.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rmi.c | 473 +++++++++++++++++++++++++++++++ 3 files changed, 484 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_rmi.h +0
-0@@ -107,6 +107,12 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);int kvm_rec_handle_request (struct kvm_vcpu *vcpu);bool kvm_rec_handle_hvc (struct kvm_vcpu *vcpu,int *ret);
arch/arm64/include/asm/kvm_rmi.h int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status); · line 118
Quoted 10 lines
arch/arm64/include/asm/kvm_rmi.h +6
-0+void kvm_realm_unmap_range (struct kvm *kvm, +unsigned long ipa, +unsigned long size, +bool unmap_private, +bool may_block); +static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr) {
Coming here from guest_memfd, would like to look more closely at the conversions-related stuff, and learn more about ARM CCA in the process :)
Quoted 13 lines
arch/arm64/kvm/mmu.c +1
-0@@ -319,6 +319,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 static void invalidate_icache_guest_page(void *va, size_t size) · line 331
Quoted 6 lines
arch/arm64/kvm/mmu.c +1
-1@@ -326,7 +327,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) {
Why convert attr_filter to a boolean, why not pass the filter through? I think that might allow all the 3 states - unmapping private, unmapping shared, and unmapping both. (Assuming unmapping none == not even calling this function).
Also, perhaps it's better to add this parameter to __unmap_stage2_range() together with the patch that uses the only_shared information :)
I see that in [1] not changing __unmap_stage2_range() was going to be explored, did that work out?
Quoted 13 lines
arch/arm64/kvm/mmu.c +1
-1struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);phys_addr_t end = start + size;@@ -343,7 +344,7 @@ void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,if (kvm_vm_is_protected (kvm_s2_mmu_to_kvm (mmu)))return ; -__unmap_stage2_range (mmu, start, size, may_block); +__unmap_stage2_range (mmu, start, size, may_block, false); }
Suzuki K Poulose
reviewerRe: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
Quoted 95 lines · Ackerley Tng
Quoted 63 lines · Steven Price
The guest can request that a region of its protected address space is switched between
RIPAS_RAMandRIPAS_EMPTY(and back) usingRSI_IPA_STATE_SET. This causes a guest exit with theRMI_EXIT_RIPAS_CHANGEcode. 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 theguest_memfdand memslot mappings. On the next entry the RIPAS changes are committed by makingRMI_RTT_SET_RIPAScalls.The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do this 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_RIPASchanges to entry we leave the door open for adding a new ioctl in the future for this purpose.Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Propagate negative error returns.
Changes since v14:
* Use addition rather than bitwise OR for adding theshared_bitinrealm_unmap_shared_range(), this handles the case where the region includes the last address (which means 'end' already has the bit set).
Changes since v13:
* Switch to the newRMI_RTT_UNPROT_UNMAPrange-based API.
* Drop ugly hack for RMM bug which errored when the RIPAS was already set to the desired value.
Changes since v12:
* Switch to the new RMM v2.0RMI_RTT_DATA_UNMAPwhich can unmap an address range.
Changes since v11:
* Combine the "Allow VMM to set RIPAS" patch into this one to avoid adding functions before they are used.
* Drop the CAP for setting RIPAS and adapt to changes from previous patches.
Changes since v10:
* Add comment explaining the assignment ofrec->run->exit.ripas_baseinkvm_complete_ripas_change().
Changes since v8:
* Make use ofripas_change()from a previous patch to implementrealm_set_ipa_state().* Update exit.ripas_base after a RIPAS change so that, if instead of entering the guest we exit to user space, we don't attempt to repeat the RIPAS change (triggering an error from the RMM).
Changes since v7:
* Rework the loop inrealm_set_ipa_state()to make it clear when the 'next' output value ofrmi_rtt_set_ripas()is used.
New patch for v7: The code was previously split awkwardly between two other patches.--- arch/arm64/include/asm/kvm_rmi.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rmi.c | 473 +++++++++++++++++++++++++++++++ 3 files changed, 484 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_rmi.h +0
-0@@ -107,6 +107,12 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);int kvm_rec_handle_request (struct kvm_vcpu *vcpu);bool kvm_rec_handle_hvc (struct kvm_vcpu *vcpu,int *ret);arch/arm64/include/asm/kvm_rmi.h int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status); · line 118
Quoted 10 lines
arch/arm64/include/asm/kvm_rmi.h +6
-0+void kvm_realm_unmap_range (struct kvm *kvm, +unsigned long ipa, +unsigned long size, +bool unmap_private, +bool may_block); +static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr) {Coming here from
guest_memfd, would like to look more closely at the conversions-related stuff, and learn more about ARM CCA in the process :)Quoted 16 lines
arch/arm64/kvm/mmu.c +1
-0@@ -319,6 +319,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 @@ -326,7 +327,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,
Quoted 8 lines
arch/arm64/kvm/mmu.c static void invalidate_icache_guest_page(void *va, size_t size) · line 331
Quoted 3 lines
arch/arm64/kvm/mmu.c +1
-1- bool may_block) + bool may_block, bool only_shared) {Why convert
attr_filterto a boolean, why not pass the filter through? I think that might allow all the 3 states - unmapping private, unmapping shared, and unmapping both. (Assuming unmapping none == not even calling this function).
You're absolutely right, we should use the attr_filter as it is if we go down this route (like I alluded to in [1]) . I think this was really from the original Guest memfd support (without inplace conversion).
Quoted 8 lines
Also, perhaps it's better to add this parameter to
__unmap_stage2_range()together with the patch that uses theonly_sharedinformation :)I see that in [1] not changing
__unmap_stage2_range()was going to be explored, did that work out?
I haven't got to it yet, I will be taking a look at this.
Cheers
Suzuki
Quoted 16 lines
[1] https://lore.kernel.org/all/[email protected]/
Quoted 13 lines
arch/arm64/kvm/mmu.c +1
-1struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);phys_addr_t end = start + size;@@ -343,7 +344,7 @@ void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,if (kvm_vm_is_protected (kvm_s2_mmu_to_kvm (mmu)))return ; -__unmap_stage2_range (mmu, start, size, may_block); +__unmap_stage2_range (mmu, start, size, may_block, false); }
Re: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
In reply to Steven PriceHi Steven,
Quoted 94 lines · Steven Price
The guest can request that a region of its protected address space is switched between
RIPAS_RAMandRIPAS_EMPTY(and back) usingRSI_IPA_STATE_SET. This causes a guest exit with theRMI_EXIT_RIPAS_CHANGEcode. 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 theguest_memfdand memslot mappings. On the next entry the RIPAS changes are committed by makingRMI_RTT_SET_RIPAScalls.The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do this 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_RIPASchanges to entry we leave the door open for adding a new ioctl in the future for this purpose.Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Propagate negative error returns.
Changes since v14:
* Use addition rather than bitwise OR for adding theshared_bitinrealm_unmap_shared_range(), this handles the case where the region includes the last address (which means 'end' already has the bit set).
Changes since v13:
* Switch to the newRMI_RTT_UNPROT_UNMAPrange-based API.
* Drop ugly hack for RMM bug which errored when the RIPAS was already set to the desired value.
Changes since v12:
* Switch to the new RMM v2.0RMI_RTT_DATA_UNMAPwhich can unmap an address range.
Changes since v11:
* Combine the "Allow VMM to set RIPAS" patch into this one to avoid adding functions before they are used.
* Drop the CAP for setting RIPAS and adapt to changes from previous patches.
Changes since v10:
* Add comment explaining the assignment ofrec->run->exit.ripas_baseinkvm_complete_ripas_change().
Changes since v8:
* Make use ofripas_change()from a previous patch to implementrealm_set_ipa_state().
* Update exit.ripas_base after a RIPAS change so that, if instead of entering the guest we exit to user space, we don't attempt to repeat the RIPAS change (triggering an error from the RMM).
Changes since v7:
* Rework the loop inrealm_set_ipa_state()to make it clear when the 'next' output value ofrmi_rtt_set_ripas()is used.
New patch for v7: The code was previously split awkwardly between two other patches.--- arch/arm64/include/asm/kvm_rmi.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rmi.c | 473 +++++++++++++++++++++++++++++++ 3 files changed, 484 insertions(+), 3 deletions(-)[...]
arch/arm64/kvm/rmi.c +37
-0+ +static int ripas_change (struct kvm *kvm, +struct kvm_vcpu *vcpu, +unsigned long ipa, +unsigned long end, +enum ripas_action action, +unsigned long *top_ipa) +{ +struct realm *realm = &kvm->arch.realm; +phys_addr_t rd_phys =virt_to_phys (realm->rd); +phys_addr_t rec_phys; +struct kvm_mmu_memory_cache *memcache =NULL ; +long ret =0 ; + +if (vcpu) { + rec_phys = vcpu->arch.rec.rec_phys; + memcache = &vcpu->arch.mmu_page_cache; + +WARN_ON (action !=RIPAS_SET ); + }else { +WARN_ON (action !=RIPAS_INIT ); + } + +while (ipa < end) { +unsigned long next = ~0 ; + +switch (action) { +case RIPAS_INIT : + ret =rmi_rtt_init_ripas (rd_phys, ipa, end, &next); +break ; +case RIPAS_SET : + ret =rmi_rtt_set_ripas (rd_phys, rec_phys, ipa, end, + &next); +break ; + } + +if (ret <0 )
arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 585
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ goto out; + +switch (RMI_RETURN_STATUS (ret)) { +case RMI_SUCCESS : + ipa = next; +break ; +case RMI_ERROR_RTT : { +int err_level =RMI_RETURN_INDEX (ret); +int level =find_map_level (realm, ipa, end); +
I have been testing this series using TF-RMM from:
https://git.trustedfirmware.org/TF-RMM/tf-rmm.git topics/rmm-v2.0-poc_3
I found that Linux repeatedly issues the same SMC_RMI_RTT_SET_RIPAS call [0], eventually causing a soft lockup [1].
The root cause appears to be an RMM bug fixed by:
https://git.trustedfirmware.org/plugins/gitiles/TF-RMM/tf-rmm.git/+/38c1de0adf66
Without this fix, RMM returns RMI_ERROR_RTT at level 3. In this case, both err_level and level are 3. Since this is RIPAS_SET, the check below is skipped, realm_create_rtt_levels(realm, ipa, 3, 3, ...) is a no-op, and ipa does not advance. The same RMI is therefore retried indefinitely.
arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 597
Quoted 12 lines
arch/arm64/kvm/rmi.c +12
-0+/* +* If the operation failed at deeper level than +* what is required for the address range, this +* implies encountering an unexpected entry, +* (e.g., RIPAS_DESTROYED), which the RMM prevents +* us from modifying. This is only applicable for +* RMI_RTT_INIT_RIPAS. All the other requests +* are generated by the Realm and thus RMM should +* be able to allow the transition. +*/ +if (action ==RIPAS_INIT &&WARN_ON_ONCE (err_level >= level)) +return -ENXIO ;
Although the root cause is an RMM bug, should we also guard RIPAS_SET against this no-progress case?
Thanks,
Kohei
[0]
SMC_RSI_VERSION 10000 > RSI_SUCCESS 10000 10001 SMC_RSI_REALM_CONFIG 830e5000 > RSI_SUCCESS SMC_RSI_IPA_STATE_SET 80000000 c0000000 1 0 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3
... (the same SMC_RMI_RTT_SET_RIPAS call is logged repeatedly)
[1]
[ 201.945663] CPU: 113 UID: 0 PID: 8093 Comm: kvm-vcpu-0 [...] [ 201.946291] pstate: 61402009 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 201.946416] pc : arm_smccc_1_2_smc+0x34/0x70 [ 201.946430] lr : rmi_smccc_invoke+0xc0/0x108 [ 201.946442] sp : ffff8000b59ab5e0 [ 201.946448] x29: ffff8000b59ab690 x28: 000000008313e000 x27: 0000000000000003 [ 201.946510] x26: 000008241cb3c000 x25: ffffffffffffffff x24: ffff8000b59ab830 [ 201.946569] x23: ffff8000b59ab7c0 x22: 0000082419daa000 x21: 000008241cb3c000 [ 201.946701] x20: 00000000000000fb x19: ffff8000b59ab5f8 x18: 0000000000000000 [ 201.946740] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000 [ 201.947097] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000 [ 201.947127] x11: 0000000000000000 x10: 0000000000000000 x9 : 0000000000000000 [ 201.947550] x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000 [ 201.947783] x5 : 0000000000000000 x4 : 00000000c0000000 x3 : 0000000000000000 [ 201.947802] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000304 [ 201.948305] Call trace: [ 201.948438] arm_smccc_1_2_smc+0x34/0x70 (P) [ 201.948484] rmi_sro_execute+0x24/0xd0 [ 201.948571] rmi_rtt_set_ripas.constprop.0+0x6c/0xb0 [ 201.948698] ripas_change+0xc4/0x1e8 [ 201.948889] kvm_rec_handle_request+0x190/0x308 [ 201.948943] check_vcpu_requests+0xcc/0x4f8 [ 201.948960] kvm_arch_vcpu_ioctl_run+0x208/0x7c0 [ 201.948986] kvm_vcpu_ioctl+0x174/0xac8 [ 201.949062] __arm64_sys_ioctl+0xb4/0x118 [ 201.949089] invoke_syscall.constprop.0+0xa8/0x100 [ 201.949147] do_el0_svc+0xb8/0xc8 [ 201.949159] el0_svc+0x48/0x1f8 [ 201.949169] el0t_64_sync_handler+0xa0/0xe8 [ 201.949194] el0t_64_sync+0x1ac/0x1b0
Quoted 97 lines
arch/arm64/kvm/rmi.c +84
-0+ + ret =realm_create_rtt_levels (realm, ipa, err_level, + level, memcache); +if (ret) + goto out; +/* Retry with the RTT levels in place */ +break ; + } +default : +WARN_ON (1 ); + ret = -ENXIO ; + goto out; + } + } + +out: +if (top_ipa) + *top_ipa = ipa; + +return ret; +} + +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; +int ret =ripas_change (kvm, vcpu, start, end,RIPAS_SET , top_ipa); + +if (!ret && ripas ==RMI_EMPTY && *top_ipa != start) +realm_unmap_private_range (kvm, start, *top_ipa, false); + +return ret; +} + +static int 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 = base; +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 (ret == -ENOMEM ) { +/* If no progress, then stop */ +if (top_ipa == base) +break ; + base = top_ipa; +continue ; + } + +if (WARN_RATELIMIT (ret, +"Unable to satisfy RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n" , + base, top, ripas)) +break ; + + base = top_ipa; + }while (base < top); + +/* +* If this function is called again before the REC_ENTER call then +* avoid calling realm_set_ipa_state() again by changing to the value +* of ripas_base for the part that has already been covered. The RMM +* ignores the contains of the rec_exit structure so this doesn't +* affect the RMM. +*/ + rec->run->exit.ripas_base = base; + +return 1 ; +} +int kvm_rec_handle_request (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec;@@ -224,6 +695,8 @@ int kvm_rec_handle_request(struct kvm_vcpu *vcpu)vcpu_get_reg (vcpu, rt); }break ; +case RMI_EXIT_RIPAS_CHANGE : +return kvm_complete_ripas_change (vcpu);default :KVM_BUG (1 , vcpu->kvm,"Unhandled realm exit_reason" );return -ENXIO ;Signature · 1 line
2.43.0
Re: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
Hi Kohei,
Quoted 114 lines · Kohei Enju
Hi Steven,
Quoted 94 lines · Steven Price
The guest can request that a region of its protected address space is switched between
RIPAS_RAMandRIPAS_EMPTY(and back) usingRSI_IPA_STATE_SET. This causes a guest exit with theRMI_EXIT_RIPAS_CHANGEcode. 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 theguest_memfdand memslot mappings. On the next entry the RIPAS changes are committed by makingRMI_RTT_SET_RIPAScalls.The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do this 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_RIPASchanges to entry we leave the door open for adding a new ioctl in the future for this purpose.Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Propagate negative error returns.
Changes since v14:
* Use addition rather than bitwise OR for adding theshared_bitinrealm_unmap_shared_range(), this handles the case where the region includes the last address (which means 'end' already has the bit set).
Changes since v13:
* Switch to the newRMI_RTT_UNPROT_UNMAPrange-based API.
* Drop ugly hack for RMM bug which errored when the RIPAS was already set to the desired value.
Changes since v12:
* Switch to the new RMM v2.0RMI_RTT_DATA_UNMAPwhich can unmap an address range.
Changes since v11:
* Combine the "Allow VMM to set RIPAS" patch into this one to avoid adding functions before they are used.
* Drop the CAP for setting RIPAS and adapt to changes from previous patches.
Changes since v10:
* Add comment explaining the assignment ofrec->run->exit.ripas_baseinkvm_complete_ripas_change().
Changes since v8:
* Make use ofripas_change()from a previous patch to implementrealm_set_ipa_state().* Update exit.ripas_base after a RIPAS change so that, if instead of entering the guest we exit to user space, we don't attempt to repeat the RIPAS change (triggering an error from the RMM).
Changes since v7:
* Rework the loop inrealm_set_ipa_state()to make it clear when the 'next' output value ofrmi_rtt_set_ripas()is used.
New patch for v7: The code was previously split awkwardly between two other patches.--- arch/arm64/include/asm/kvm_rmi.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rmi.c | 473 +++++++++++++++++++++++++++++++ 3 files changed, 484 insertions(+), 3 deletions(-)[...]
arch/arm64/kvm/rmi.c +37
-0+ +static int ripas_change (struct kvm *kvm, +struct kvm_vcpu *vcpu, +unsigned long ipa, +unsigned long end, +enum ripas_action action, +unsigned long *top_ipa) +{ +struct realm *realm = &kvm->arch.realm; +phys_addr_t rd_phys =virt_to_phys (realm->rd); +phys_addr_t rec_phys; +struct kvm_mmu_memory_cache *memcache =NULL ; +long ret =0 ; + +if (vcpu) { + rec_phys = vcpu->arch.rec.rec_phys; + memcache = &vcpu->arch.mmu_page_cache; + +WARN_ON (action !=RIPAS_SET ); + }else { +WARN_ON (action !=RIPAS_INIT ); + } + +while (ipa < end) { +unsigned long next = ~0 ; + +switch (action) { +case RIPAS_INIT : + ret =rmi_rtt_init_ripas (rd_phys, ipa, end, &next); +break ; +case RIPAS_SET : + ret =rmi_rtt_set_ripas (rd_phys, rec_phys, ipa, end, + &next); +break ; + } + +if (ret <0 )arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 585
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ goto out; + +switch (RMI_RETURN_STATUS (ret)) { +case RMI_SUCCESS : + ipa = next; +break ; +case RMI_ERROR_RTT : { +int err_level =RMI_RETURN_INDEX (ret); +int level =find_map_level (realm, ipa, end); +I have been testing this series using TF-RMM from:
https://git.trustedfirmware.org/TF-RMM/tf-rmm.git topics/rmm-v2.0-poc_3I found that Linux repeatedly issues the same
SMC_RMI_RTT_SET_RIPAScall [0], eventually causing a soft lockup [1].
Quoted 9 lines
The root cause appears to be an RMM bug fixed by:
https://git.trustedfirmware.org/plugins/gitiles/TF-RMM/tf-rmm.git/+/38c1de0adf66Without this fix, RMM returns
RMI_ERROR_RTTat level 3. In this case, botherr_leveland level are 3. Since this isRIPAS_SET, the check below is skipped,realm_create_rtt_levels(realm, ipa, 3, 3, ...)is a no-op, and ipa does not advance. The same RMI is therefore retried indefinitely.
FYI. This is a known issue, reported previously [1]. Following the discussions in the thread will lead to the TF-RMM commit you mentioned.
[1] https://lore.kernel.org/linux-coco/[email protected]/
Thanks,
Gavin
Quoted 159 lines
arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 597
Quoted 12 lines
arch/arm64/kvm/rmi.c +12
-0+/* +* If the operation failed at deeper level than +* what is required for the address range, this +* implies encountering an unexpected entry, +* (e.g., RIPAS_DESTROYED), which the RMM prevents +* us from modifying. This is only applicable for +* RMI_RTT_INIT_RIPAS. All the other requests +* are generated by the Realm and thus RMM should +* be able to allow the transition. +*/ +if (action ==RIPAS_INIT &&WARN_ON_ONCE (err_level >= level)) +return -ENXIO ;Although the root cause is an RMM bug, should we also guard
RIPAS_SETagainst this no-progress case?Thanks,
Kohei[0]
SMC_RSI_VERSION 10000 > RSI_SUCCESS 10000 10001 SMC_RSI_REALM_CONFIG 830e5000 > RSI_SUCCESS SMC_RSI_IPA_STATE_SET 80000000 c0000000 1 0 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3... (the same
SMC_RMI_RTT_SET_RIPAScall is logged repeatedly)[1]
[ 201.945663] CPU: 113 UID: 0 PID: 8093 Comm: kvm-vcpu-0 [...] [ 201.946291] pstate: 61402009 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 201.946416] pc : arm_smccc_1_2_smc+0x34/0x70 [ 201.946430] lr : rmi_smccc_invoke+0xc0/0x108 [ 201.946442] sp : ffff8000b59ab5e0 [ 201.946448] x29: ffff8000b59ab690 x28: 000000008313e000 x27: 0000000000000003 [ 201.946510] x26: 000008241cb3c000 x25: ffffffffffffffff x24: ffff8000b59ab830 [ 201.946569] x23: ffff8000b59ab7c0 x22: 0000082419daa000 x21: 000008241cb3c000 [ 201.946701] x20: 00000000000000fb x19: ffff8000b59ab5f8 x18: 0000000000000000 [ 201.946740] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000 [ 201.947097] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000 [ 201.947127] x11: 0000000000000000 x10: 0000000000000000 x9 : 0000000000000000 [ 201.947550] x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000 [ 201.947783] x5 : 0000000000000000 x4 : 00000000c0000000 x3 : 0000000000000000 [ 201.947802] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000304 [ 201.948305] Call trace: [ 201.948438] arm_smccc_1_2_smc+0x34/0x70 (P) [ 201.948484] rmi_sro_execute+0x24/0xd0 [ 201.948571] rmi_rtt_set_ripas.constprop.0+0x6c/0xb0 [ 201.948698] ripas_change+0xc4/0x1e8 [ 201.948889] kvm_rec_handle_request+0x190/0x308 [ 201.948943] check_vcpu_requests+0xcc/0x4f8 [ 201.948960] kvm_arch_vcpu_ioctl_run+0x208/0x7c0 [ 201.948986] kvm_vcpu_ioctl+0x174/0xac8 [ 201.949062] __arm64_sys_ioctl+0xb4/0x118 [ 201.949089] invoke_syscall.constprop.0+0xa8/0x100 [ 201.949147] do_el0_svc+0xb8/0xc8 [ 201.949159] el0_svc+0x48/0x1f8 [ 201.949169] el0t_64_sync_handler+0xa0/0xe8 [ 201.949194] el0t_64_sync+0x1ac/0x1b0Quoted 97 lines
arch/arm64/kvm/rmi.c +84
-0+ + ret =realm_create_rtt_levels (realm, ipa, err_level, + level, memcache); +if (ret) + goto out; +/* Retry with the RTT levels in place */ +break ; + } +default : +WARN_ON (1 ); + ret = -ENXIO ; + goto out; + } + } + +out: +if (top_ipa) + *top_ipa = ipa; + +return ret; +} + +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; +int ret =ripas_change (kvm, vcpu, start, end,RIPAS_SET , top_ipa); + +if (!ret && ripas ==RMI_EMPTY && *top_ipa != start) +realm_unmap_private_range (kvm, start, *top_ipa, false); + +return ret; +} + +static int 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 = base; +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 (ret == -ENOMEM ) { +/* If no progress, then stop */ +if (top_ipa == base) +break ; + base = top_ipa; +continue ; + } + +if (WARN_RATELIMIT (ret, +"Unable to satisfy RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n" , + base, top, ripas)) +break ; + + base = top_ipa; + }while (base < top); + +/* +* If this function is called again before the REC_ENTER call then +* avoid calling realm_set_ipa_state() again by changing to the value +* of ripas_base for the part that has already been covered. The RMM +* ignores the contains of the rec_exit structure so this doesn't +* affect the RMM. +*/ + rec->run->exit.ripas_base = base; + +return 1 ; +} +int kvm_rec_handle_request (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec;@@ -224,6 +695,8 @@ int kvm_rec_handle_request(struct kvm_vcpu *vcpu)vcpu_get_reg (vcpu, rt); }break ; +case RMI_EXIT_RIPAS_CHANGE : +return kvm_complete_ripas_change (vcpu);default :KVM_BUG (1 , vcpu->kvm,"Unhandled realm exit_reason" );return -ENXIO ;Signature · 1 line
2.43.0
Re: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
Quoted 127 lines · Gavin Shan
Hi Kohei,
Quoted 114 lines · Kohei Enju
Hi Steven,
Quoted 94 lines · Steven Price
The guest can request that a region of its protected address space is switched between
RIPAS_RAMandRIPAS_EMPTY(and back) usingRSI_IPA_STATE_SET. This causes a guest exit with theRMI_EXIT_RIPAS_CHANGEcode. 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 theguest_memfdand memslot mappings. On the next entry the RIPAS changes are committed by makingRMI_RTT_SET_RIPAScalls.The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do this 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_RIPASchanges to entry we leave the door open for adding a new ioctl in the future for this purpose.Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Propagate negative error returns.
Changes since v14:
* Use addition rather than bitwise OR for adding theshared_bitinrealm_unmap_shared_range(), this handles the case where the region includes the last address (which means 'end' already has the bit set).
Changes since v13:
* Switch to the newRMI_RTT_UNPROT_UNMAPrange-based API.
* Drop ugly hack for RMM bug which errored when the RIPAS was already set to the desired value.
Changes since v12:
* Switch to the new RMM v2.0RMI_RTT_DATA_UNMAPwhich can unmap an address range.
Changes since v11:
* Combine the "Allow VMM to set RIPAS" patch into this one to avoid adding functions before they are used.
* Drop the CAP for setting RIPAS and adapt to changes from previous patches.
Changes since v10:
* Add comment explaining the assignment ofrec->run->exit.ripas_baseinkvm_complete_ripas_change().
Changes since v8:
* Make use ofripas_change()from a previous patch to implementrealm_set_ipa_state().* Update exit.ripas_base after a RIPAS change so that, if instead of entering the guest we exit to user space, we don't attempt to repeat the RIPAS change (triggering an error from the RMM).
Changes since v7:
* Rework the loop inrealm_set_ipa_state()to make it clear when the 'next' output value ofrmi_rtt_set_ripas()is used.
New patch for v7: The code was previously split awkwardly between two other patches.--- arch/arm64/include/asm/kvm_rmi.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rmi.c | 473 +++++++++++++++++++++++++++++++ 3 files changed, 484 insertions(+), 3 deletions(-)[...]
arch/arm64/kvm/rmi.c +37
-0+ +static int ripas_change (struct kvm *kvm, +struct kvm_vcpu *vcpu, +unsigned long ipa, +unsigned long end, +enum ripas_action action, +unsigned long *top_ipa) +{ +struct realm *realm = &kvm->arch.realm; +phys_addr_t rd_phys =virt_to_phys (realm->rd); +phys_addr_t rec_phys; +struct kvm_mmu_memory_cache *memcache =NULL ; +long ret =0 ; + +if (vcpu) { + rec_phys = vcpu->arch.rec.rec_phys; + memcache = &vcpu->arch.mmu_page_cache; + +WARN_ON (action !=RIPAS_SET ); + }else { +WARN_ON (action !=RIPAS_INIT ); + } + +while (ipa < end) { +unsigned long next = ~0 ; + +switch (action) { +case RIPAS_INIT : + ret =rmi_rtt_init_ripas (rd_phys, ipa, end, &next); +break ; +case RIPAS_SET : + ret =rmi_rtt_set_ripas (rd_phys, rec_phys, ipa, end, + &next); +break ; + } + +if (ret <0 )arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 585
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ goto out; + +switch (RMI_RETURN_STATUS (ret)) { +case RMI_SUCCESS : + ipa = next; +break ; +case RMI_ERROR_RTT : { +int err_level =RMI_RETURN_INDEX (ret); +int level =find_map_level (realm, ipa, end); +I have been testing this series using TF-RMM from:
https://git.trustedfirmware.org/TF-RMM/tf-rmm.git topics/rmm-v2.0-poc_3I found that Linux repeatedly issues the same
SMC_RMI_RTT_SET_RIPAScall [0], eventually causing a soft lockup [1].Quoted 9 lines
The root cause appears to be an RMM bug fixed by:
https://git.trustedfirmware.org/plugins/gitiles/TF-RMM/tf-rmm.git/+/38c1de0adf66Without this fix, RMM returns
RMI_ERROR_RTTat level 3. In this case, botherr_leveland level are 3. Since this isRIPAS_SET, the check below is skipped,realm_create_rtt_levels(realm, ipa, 3, 3, ...)is a no-op, and ipa does not advance. The same RMI is therefore retried indefinitely.
Quoted 4 lines
FYI. This is a known issue, reported previously [1]. Following the discussions in the thread will lead to the TF-RMM commit you mentioned.
Hi Gavin,
Thank you for the FYI.
I wasn't aware of that discussions, so I appreciate you pointing it out.
Thanks,
Kohei
Quoted 164 lines
Thanks,
GavinQuoted 159 lines
arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 597
Quoted 12 lines
arch/arm64/kvm/rmi.c +12
-0+/* +* If the operation failed at deeper level than +* what is required for the address range, this +* implies encountering an unexpected entry, +* (e.g., RIPAS_DESTROYED), which the RMM prevents +* us from modifying. This is only applicable for +* RMI_RTT_INIT_RIPAS. All the other requests +* are generated by the Realm and thus RMM should +* be able to allow the transition. +*/ +if (action ==RIPAS_INIT &&WARN_ON_ONCE (err_level >= level)) +return -ENXIO ;Although the root cause is an RMM bug, should we also guard
RIPAS_SETagainst this no-progress case?Thanks,
Kohei[0]
SMC_RSI_VERSION 10000 > RSI_SUCCESS 10000 10001 SMC_RSI_REALM_CONFIG 830e5000 > RSI_SUCCESS SMC_RSI_IPA_STATE_SET 80000000 c0000000 1 0 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3 SMC_RMI_RTT_SET_RIPAS 10060182000 10056e1c000 8000c000 c0000000 > RMI_ERROR_RTT 3... (the same
SMC_RMI_RTT_SET_RIPAScall is logged repeatedly)[1]
[ 201.945663] CPU: 113 UID: 0 PID: 8093 Comm: kvm-vcpu-0 [...] [ 201.946291] pstate: 61402009 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 201.946416] pc : arm_smccc_1_2_smc+0x34/0x70 [ 201.946430] lr : rmi_smccc_invoke+0xc0/0x108 [ 201.946442] sp : ffff8000b59ab5e0 [ 201.946448] x29: ffff8000b59ab690 x28: 000000008313e000 x27: 0000000000000003 [ 201.946510] x26: 000008241cb3c000 x25: ffffffffffffffff x24: ffff8000b59ab830 [ 201.946569] x23: ffff8000b59ab7c0 x22: 0000082419daa000 x21: 000008241cb3c000 [ 201.946701] x20: 00000000000000fb x19: ffff8000b59ab5f8 x18: 0000000000000000 [ 201.946740] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000 [ 201.947097] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000 [ 201.947127] x11: 0000000000000000 x10: 0000000000000000 x9 : 0000000000000000 [ 201.947550] x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000 [ 201.947783] x5 : 0000000000000000 x4 : 00000000c0000000 x3 : 0000000000000000 [ 201.947802] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000304 [ 201.948305] Call trace: [ 201.948438] arm_smccc_1_2_smc+0x34/0x70 (P) [ 201.948484] rmi_sro_execute+0x24/0xd0 [ 201.948571] rmi_rtt_set_ripas.constprop.0+0x6c/0xb0 [ 201.948698] ripas_change+0xc4/0x1e8 [ 201.948889] kvm_rec_handle_request+0x190/0x308 [ 201.948943] check_vcpu_requests+0xcc/0x4f8 [ 201.948960] kvm_arch_vcpu_ioctl_run+0x208/0x7c0 [ 201.948986] kvm_vcpu_ioctl+0x174/0xac8 [ 201.949062] __arm64_sys_ioctl+0xb4/0x118 [ 201.949089] invoke_syscall.constprop.0+0xa8/0x100 [ 201.949147] do_el0_svc+0xb8/0xc8 [ 201.949159] el0_svc+0x48/0x1f8 [ 201.949169] el0t_64_sync_handler+0xa0/0xe8 [ 201.949194] el0t_64_sync+0x1ac/0x1b0Quoted 97 lines
arch/arm64/kvm/rmi.c +84
-0+ + ret =realm_create_rtt_levels (realm, ipa, err_level, + level, memcache); +if (ret) + goto out; +/* Retry with the RTT levels in place */ +break ; + } +default : +WARN_ON (1 ); + ret = -ENXIO ; + goto out; + } + } + +out: +if (top_ipa) + *top_ipa = ipa; + +return ret; +} + +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; +int ret =ripas_change (kvm, vcpu, start, end,RIPAS_SET , top_ipa); + +if (!ret && ripas ==RMI_EMPTY && *top_ipa != start) +realm_unmap_private_range (kvm, start, *top_ipa, false); + +return ret; +} + +static int 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 = base; +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 (ret == -ENOMEM ) { +/* If no progress, then stop */ +if (top_ipa == base) +break ; + base = top_ipa; +continue ; + } + +if (WARN_RATELIMIT (ret, +"Unable to satisfy RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n" , + base, top, ripas)) +break ; + + base = top_ipa; + }while (base < top); + +/* +* If this function is called again before the REC_ENTER call then +* avoid calling realm_set_ipa_state() again by changing to the value +* of ripas_base for the part that has already been covered. The RMM +* ignores the contains of the rec_exit structure so this doesn't +* affect the RMM. +*/ + rec->run->exit.ripas_base = base; + +return 1 ; +} +int kvm_rec_handle_request (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec;@@ -224,6 +695,8 @@ int kvm_rec_handle_request(struct kvm_vcpu *vcpu)vcpu_get_reg (vcpu, rt); }break ; +case RMI_EXIT_RIPAS_CHANGE : +return kvm_complete_ripas_change (vcpu);default :KVM_BUG (1 , vcpu->kvm,"Unhandled realm exit_reason" );return -ENXIO ;Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
In reply to Kohei EnjuOn Mon, 07 Sep 2026 06:05:29 +0100,
Kohei Enju <[email protected]> wrote:
[...]
Quoted 2 lines
Although the root cause is an RMM bug, should we also guard
RIPAS_SETagainst this no-progress case?
No. We really should *prevent* the kernel from using a known broken RMM. Which brings me to one of my long standing request: how to we enforce the minimum version of RMM that KVM is willing to work with?
We absolutely need to have a hard deprecation mechanism in place before day-1.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
Hi Marc,
Quoted 10 lines · Marc Zyngier
On Mon, 07 Sep 2026 06:05:29 +0100,
Kohei Enju <[email protected]> wrote:[...]
Quoted 2 lines
Although the root cause is an RMM bug, should we also guard
RIPAS_SETagainst this no-progress case?No. We really should *prevent* the kernel from using a known broken RMM.
I understand.
Quoted 5 lines
Which brings me to one of my long standing request: how to we enforce the minimum version of RMM that KVM is willing to work with?
We absolutely need to have a hard deprecation mechanism in place before day-1.
Agreed. Such a mechanism is clearly necessary.
Is there any ongoing work to address this that you are aware of? This seems like a fairly broad topic that may require a separate discussion.
Thanks,
Kohei
Quoted 5 lines
M.Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
Quoted 2 lines · Kohei Enju
Is there any ongoing work to address this that you are aware of? This seems like a fairly broad topic that may require a separate discussion.
I don't think anyone is working on this yet.
But I guess that making it clear that this needs to be sorted *before* we merge the KVM CCA support should get things (and people) in motion.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Suzuki K Poulose
reviewerRe: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
In reply to Marc ZyngierQuoted 11 lines · Marc Zyngier
On Mon, 07 Sep 2026 06:05:29 +0100,
Kohei Enju <[email protected]> wrote:[...]
Quoted 2 lines
Although the root cause is an RMM bug, should we also guard
RIPAS_SETagainst this no-progress case?No. We really should *prevent* the kernel from using a known broken RMM. Which brings me to one of my long standing request: how to we enforce the minimum version of RMM that KVM is willing to work with?
(1) Do you mean the RMM ABI version ?
(2) Or the particular build version of a given RMM firmware implementation ?
As far as (1) is considered, the kernel already sticks to a single version, v2.0 and doesn't support anything else. And we are fine with KVM/Linux supporting a single version of RMM and deprecating the support for older RMM versions as we decide to move on.
As for (2), the RMM ABI doesn't provide any information about the build/fix version for a given implementation. I will raise this within Arm.
Suzuki
Quoted 6 lines
We absolutely need to have a hard deprecation mechanism in place before day-1.
M.
Marc Zyngier
maintainerRe: [PATCH v16 22/45] KVM: arm64: CCA: Handle RMI_EXIT_RIPAS_CHANGE
Quoted 18 lines · Suzuki K Poulose
Quoted 11 lines · Marc Zyngier
On Mon, 07 Sep 2026 06:05:29 +0100,
Kohei Enju <[email protected]> wrote:[...]
Quoted 2 lines
Although the root cause is an RMM bug, should we also guard
RIPAS_SETagainst this no-progress case?No. We really should *prevent* the kernel from using a known broken RMM. Which brings me to one of my long standing request: how to we enforce the minimum version of RMM that KVM is willing to work with?
(1) Do you mean the RMM ABI version ?
(2) Or the particular build version of a given RMM firmware implementation ?
Quoted 8 lines
As far as (1) is considered, the kernel already sticks to a single version, v2.0 and doesn't support anything else. And we are fine with KVM/Linux supporting a single version of RMM and deprecating the support for older RMM versions as we decide to move on.
As for (2), the RMM ABI doesn't provide any information about the build/fix version for a given implementation. I will raise this within Arm.
But it appears that only checking for RMM v2.0 wouldn't be sufficient in the case that Kohei faces. Nothing distinguishes a vanilla RMM from a fixed one, as we can only check for the ABI.
We need a way to identify *implementations* so that we can refuse to use one that is known to be broken. And given that each and every vendor already have their own toy RMM, each with their own "added value", it cannot be a simple "build number" identifier.
This is not the first time I ask for this.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v16 23/45 KVM: arm64: CCA: Handle realm MMIO emulation
In reply to Steven PriceMMIO 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 v15:
* Adapt to the new exit flow.
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/rmi-exit.c | 10 ++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-)
arch/arm64/kvm/inject_fault.c +3 -1
@@ -228,7 +228,9 @@ static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt, u32 addr)static void __kvm_inject_sea (struct kvm_vcpu *vcpu,bool iabt,u64 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, iabt, addr);else inject_abt64 (vcpu, iabt, addr);
arch/arm64/kvm/mmio.c +12 -4
@@ -6,6 +6,7 @@#include <linux/kvm_host.h> #include <asm/kvm_emulate.h> +#include <linux/arm-smccc-rmi.h> #include <trace/events/kvm.h> #include "trace.h"
@@ -138,14 +139,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 ; }
@@ -167,14 +175,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), esr,kvm_vcpu_get_hfar (vcpu), fault_ipa); -if (vcpu_is_protected (vcpu)) +if (vcpu_is_protected (vcpu) ||vcpu_is_rec (vcpu))return kvm_inject_sea_dabt (vcpu,kvm_vcpu_get_hfar (vcpu)); if (test_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
arch/arm64/kvm/rmi-exit.c +10 -0
@@ -37,6 +37,16 @@ static void rec_exit_sync(struct kvm_vcpu *vcpu)kvm_make_request (KVM_REQ_RMI , vcpu);break ; } +case ESR_ELx_EC_DABT_LOW: +/* +* The RMM reports the value of an MMIO write in gprs[0], +* irrespective of the register named by the instruction. +*/ +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 ]); +break ; } }
Signature · 1 line
2.43.0
PATCH v16 24/45 KVM: arm64: Expose support for private memory
In reply to Steven PriceSelect KVM_GENERIC_MEMORY_ATTRIBUTES and provide the necessary support functions.
---
Changes since v14:
* Switch to guest_memfd in-place memory conversion and drop support for KVM_VM_MEMORY_ATTRIBUTES.
Changes since v13:
* Also update documentation to show that KVM_CAP_MEMORY_ATTRIBUTES is used on arm64.
Changes since v12:
* Only define kvm_arch_has_private_mem() when CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES is set to avoid build issues when KVM is disabled.
Changes since v10:
* KVM_GENERIC_PRIVATE_MEM replacd with KVM_GENERIC_MEMORY_ATTRIBUTES.
Changes since v9:
* Drop the #ifdef CONFIG_KVM_PRIVATE_MEM guard from the definition of kvm_arch_has_private_mem()
Changes since v2:
* Switch kvm_arch_has_private_mem() to a macro to avoid overhead of a function call.
* Guard definitions of kvm_arch_{pre,post}_set_memory_attributes() with #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES.
* Early out in kvm_arch_post_set_memory_attributes() if the WARN_ON should trigger.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -1510,6 +1510,8 @@ struct kvm *kvm_arch_alloc_vm(void);#define vcpu_is_protected (vcpu) kvm_vm_is_protected((vcpu)->kvm) +#define kvm_arch_has_private_mem (kvm) ((kvm)->arch.is_realm) +int kvm_arm_vcpu_finalize (struct kvm_vcpu *vcpu,int feature);bool kvm_arm_vcpu_is_finalized (struct kvm_vcpu *vcpu);
Signature · 1 line
2.43.0
Re: [PATCH v16 24/45] KVM: arm64: Expose support for private memory
Quoted 17 lines · Steven Price
Select
KVM_GENERIC_MEMORY_ATTRIBUTESand provide the necessary support functions.Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v14:
* Switch toguest_memfdin-place memory conversion and drop support forKVM_VM_MEMORY_ATTRIBUTES.[...snip...]
arch/arm64/include/asm/kvm_host.h +0
-0
arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1514
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2
-0@@ -1510,6 +1510,8 @@ struct kvm *kvm_arch_alloc_vm(void);#define vcpu_is_protected (vcpu) kvm_vm_is_protected((vcpu)->kvm) +#define kvm_arch_has_private_mem (kvm) ((kvm)->arch.is_realm) +
Perhaps related to Fuad's question about pKVM and CCA state: would conditioning this on is_realm mess up pKVM?
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +0
-1int kvm_arm_vcpu_finalize (struct kvm_vcpu *vcpu,int feature);bool kvm_arm_vcpu_is_finalized (struct kvm_vcpu *vcpu); --2.43.0
Suzuki K Poulose
reviewerRe: [PATCH v16 24/45] KVM: arm64: Expose support for private memory
Quoted 20 lines · Ackerley Tng
Quoted 18 lines · Steven Price
Select
KVM_GENERIC_MEMORY_ATTRIBUTESand provide the necessary support functions.Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v14:
* Switch toguest_memfdin-place memory conversion and drop support forKVM_VM_MEMORY_ATTRIBUTES.[...snip...]
arch/arm64/include/asm/kvm_host.h +0
-0@@ -1510,6 +1510,8 @@ struct kvm *kvm_arch_alloc_vm(void);
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1514
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +2
-0#define vcpu_is_protected (vcpu) kvm_vm_is_protected((vcpu)->kvm) +#define kvm_arch_has_private_mem (kvm) ((kvm)->arch.is_realm) +Perhaps related to Fuad's question about pKVM and CCA state: would conditioning this on
is_realmmess up pKVM?
Not really. The comment is about unifiying the conditions which both pKVM and Realm selects. Right now, only CCA uses guest_memfd. When pkvm comes along to use guest_memfd, this could be something like:
kvm->arch.needs_private_mem (set by both pKVM and Realm) ?
Cheers
Suzuki
Quoted 6 lines
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +0
-1int kvm_arm_vcpu_finalize (struct kvm_vcpu *vcpu,int feature);bool kvm_arm_vcpu_is_finalized (struct kvm_vcpu *vcpu); --2.43.0
PATCH v16 25/45 KVM: arm64: CCA: Create the realm descriptor
In reply to Steven PriceCreating a realm involves first creating a realm descriptor (RD). This involves passing the configuration information to the RMM. Do this as part of realm_ensure_created() so that the realm is created when it is first needed. The call to realm_ensure_created() will be added in a later patch.
---
Changes since v15:
* The page for realm parameters is now only temporarily allocated in realm_create_rd().
* Deal with negative error codes.
Changes since v14:
* Explicitly set hash_algo = RMI_HASH_SHA_256 (which is the default).
* Support SRO flow during realm creation.
Changes since v13:
* The RMM no longer uses AUX granules, so no need to ask it how many it needs.
* Adapted to other changes.
Changes since v12:
* Since RMM page size is now equal to the host's page size various calculations are simplified.
* Switch to using range based APIs to delegate/undelegate.
* VMID handling is now handled entirely by the RMM.
--- arch/arm64/kvm/rmi.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
arch/arm64/kvm/rmi.c +95 -0
@@ -462,6 +462,83 @@ static void realm_unmap_shared_range(struct kvm *kvm, start, end); } +static int realm_create_rd (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; + struct realm_params *params __free(free_page) = NULL; +void *rd =NULL ; +phys_addr_t rd_phys, params_phys, top_delegated; +size_t pgd_size =kvm_pgtable_stage2_pgd_size (kvm->arch.mmu.vtcr); +long rmi_ret; +int r; + + realm->ia_bits =VTCR_EL2_IPA (kvm->arch.mmu.vtcr); + +if (WARN_ON (realm->rd)) +return -EEXIST ; + + params = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); +if (!params) +return -ENOMEM ; + + rd = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); +if (!rd) +return -ENOMEM ; + + rd_phys =virt_to_phys (rd); +if (rmi_delegate_page (rd_phys)) { + r = -ENXIO ; + goto free_rd; + } + +if (rmi_delegate_range (kvm->arch.mmu.pgd_phys, pgd_size, + &top_delegated)) { + 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 = pgd_size /PAGE_SIZE ; + params->rtt_base = kvm->arch.mmu.pgd_phys; + params->hash_algo =RMI_HASH_SHA_256 ; + +if (kvm->arch.arm_pmu) { + params->pmu_num_ctrs = kvm->arch.nr_pmu_counters; + params->flags0 |=RMI_REALM_PARAM_FLAG_PMU ; + } + +if (kvm_lpa2_is_enabled ()) + params->flags0 |=RMI_REALM_PARAM_FLAG_LPA2 ; + + params_phys =virt_to_phys (params); + + rmi_ret =rmi_realm_create (rd_phys, params_phys, realm->sro); +if (rmi_ret) { + r = rmi_ret <0 ? rmi_ret : -ENXIO ; + goto out_undelegate_tables; + } + + realm->rd = rd; +kvm_set_realm_state (kvm,REALM_STATE_NEW ); + +return 0 ; + +out_undelegate_tables: +if (WARN_ON (rmi_undelegate_range (kvm->arch.mmu.pgd_phys, + top_delegated - kvm->arch.mmu.pgd_phys))) { +/* Leak the pages if they cannot be returned */ + kvm->arch.mmu.pgt =NULL ; + } +if (WARN_ON (rmi_undelegate_page (rd_phys))) { +/* Leak the page if it isn't returned */ +return r; + } +free_rd: +free_page ((unsigned long )rd); +return r; +} + static void realm_unmap_private_range(struct kvm *kvm, unsigned long start, unsigned long end,
@@ -632,6 +709,24 @@ static int realm_set_ipa_state(struct kvm_vcpu *vcpu,return ret; } +static int __maybe_unused realm_ensure_created (struct kvm *kvm) +{ +lockdep_assert_held (&kvm->arch.config_lock); + +switch (kvm_realm_state (kvm)) { +case REALM_STATE_NONE : +break ; +case REALM_STATE_NEW : +return 0 ; +case REALM_STATE_DEAD : +return -ENXIO ; +default : +return -EBUSY ; + } + +return realm_create_rd (kvm); +} +static int kvm_complete_ripas_change (struct kvm_vcpu *vcpu) {struct kvm *kvm = vcpu->kvm;
Signature · 1 line
2.43.0
PATCH v16 26/45 KVM: arm64: CCA: Activate realms on first vCPU run
In reply to Steven PriceUse kvm_arch_vcpu_run_pid_change() to check if this is the first time the realm guest has run. If this is the first run then activate the realm.
---
Changes since v12:
* Fix commit message
* Change realm_state checks to be >= REALM_STATE_ACTIVE to avoid a dead guest being revived by kvm_activate_realm().
--- arch/arm64/include/asm/kvm_rmi.h | 1 + arch/arm64/kvm/arm.c | 6 +++++ arch/arm64/kvm/rmi.c | 45 ++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +1 -0
@@ -98,6 +98,7 @@ void kvm_init_rmi(void);u32 kvm_rmm_ipa_limit (void );int kvm_init_realm (struct kvm *kvm); +int kvm_activate_realm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm);int kvm_realm_teardown_stage2 (struct kvm *kvm);void kvm_destroy_rec (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +6 -0
@@ -1034,6 +1034,12 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)return ret; } +if (kvm_is_realm (vcpu->kvm)) { + ret =kvm_activate_realm (kvm); +if (ret) +return ret; + } +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/rmi.c +43 -2
63 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v16 27/45 KVM: arm64: CCA: Allow populating initial contents
In reply to Steven PriceThe 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 v15:
* Handle negative error codes
Changes since v14:
* Holding of locks slots_lock and config_lock have been moved up the callstack with lockdesp assertions placed in the lower functions.
* Add overflow check into kvm_arm_rmi_populate().
Changes since v13:
* Rename realm_create_protected_data_page() to realm_data_map_init().
Changes since v12:
* The ioctl now updates the structure with the amount populated rather than returning this through the ioctl return code.
* Use the new RMM v2.0 range based RMI calls.
* Adapt to upstream changes in kvm_gmem_populate().
Changes since v11:
* The multiplex CAP is gone and there's a new ioctl which makes use of the generic kvm_gmem_populate() functionality.
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/include/asm/kvm_rmi.h | 4 ++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 13 ++++ arch/arm64/kvm/rmi.c | 119 +++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+)
arch/arm64/include/asm/kvm_rmi.h +4 -0
@@ -108,6 +108,10 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);int kvm_rec_handle_request (struct kvm_vcpu *vcpu);bool kvm_rec_handle_hvc (struct kvm_vcpu *vcpu,int *ret); +struct kvm_arm_rmi_populate ; + +int kvm_arm_rmi_populate (struct kvm *kvm, +struct kvm_arm_rmi_populate *arg); void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, unsigned long size,
arch/arm64/kvm/Kconfig +1 -0
@@ -37,6 +37,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD + select HAVE_KVM_ARCH_GMEM_POPULATE select ARM_RMM help Support hosting virtualized guest machines.
arch/arm64/kvm/arm.c +13 -0
@@ -2154,6 +2154,19 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)return -EFAULT ;return kvm_vm_ioctl_get_reg_writable_masks (kvm, &range); } +case KVM_ARM_RMI_POPULATE : { +struct kvm_arm_rmi_populate req; +int ret; + +if (!kvm_is_realm (kvm)) +return -ENXIO ; +if (copy_from_user (&req, argp,sizeof (req))) +return -EFAULT ; + ret =kvm_arm_rmi_populate (kvm, &req); +if (copy_to_user (argp, &req,sizeof (req))) +return -EFAULT ; +return ret; + }default :return -EINVAL ; }
arch/arm64/kvm/rmi.c +119 -0
131 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v16 27/45] KVM: arm64: CCA: Allow populating initial contents
Quoted 130 lines · 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.
Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Handle negative error codes
Changes since v14:
* Holding of locksslots_lockandconfig_lockhave been moved up the callstack with lockdesp assertions placed in the lower functions.
* Add overflow check intokvm_arm_rmi_populate().
Changes since v13:
* Renamerealm_create_protected_data_page()torealm_data_map_init().
Changes since v12:
* The ioctl now updates the structure with the amount populated rather than returning this through the ioctl return code.
* Use the new RMM v2.0 range based RMI calls.
* Adapt to upstream changes inkvm_gmem_populate().
Changes since v11:
* The multiplex CAP is gone and there's a new ioctl which makes use of the generickvm_gmem_populate()functionality.
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) frompopulate_par_region()- it doesn't exist within the current architecture.
* Add acond_resched()call inkvm_populate_realm().
Changes since v5:
* Refactor to use PFNs rather than tracking struct page inrealm_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/include/asm/kvm_rmi.h | 4 ++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 13 ++++ arch/arm64/kvm/rmi.c | 119 +++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+)arch/arm64/include/asm/kvm_rmi.h +4
-0@@ -108,6 +108,10 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status);int kvm_rec_handle_request (struct kvm_vcpu *vcpu);bool kvm_rec_handle_hvc (struct kvm_vcpu *vcpu,int *ret); +struct kvm_arm_rmi_populate ; + +int kvm_arm_rmi_populate (struct kvm *kvm, +struct kvm_arm_rmi_populate *arg); void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, unsigned long size,arch/arm64/kvm/Kconfig +1
-0@@ -37,6 +37,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD + select HAVE_KVM_ARCH_GMEM_POPULATE select ARM_RMM help Support hosting virtualized guest machines.arch/arm64/kvm/arm.c +13
-0@@ -2154,6 +2154,19 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)return -EFAULT ;return kvm_vm_ioctl_get_reg_writable_masks (kvm, &range); } +case KVM_ARM_RMI_POPULATE : { +struct kvm_arm_rmi_populate req; +int ret; + +if (!kvm_is_realm (kvm)) +return -ENXIO ; +if (copy_from_user (&req, argp,sizeof (req))) +return -EFAULT ; + ret =kvm_arm_rmi_populate (kvm, &req); +if (copy_to_user (argp, &req,sizeof (req))) +return -EFAULT ; +return ret; + }default :return -EINVAL ; }arch/arm64/kvm/rmi.c +25
-0@@ -609,6 +609,76 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,realm_unmap_private_range (kvm, start, end, may_block); } +static int realm_data_map_init (struct kvm *kvm,unsigned long ipa, +kvm_pfn_t dst_pfn,kvm_pfn_t src_pfn, +unsigned long flags) +{ +struct realm *realm = &kvm->arch.realm; +phys_addr_t rd =virt_to_phys (realm->rd); +phys_addr_t dst_phys, src_phys; +long ret; + +lockdep_assert_held (&kvm->slots_lock); +lockdep_assert_held (&kvm->arch.config_lock); + + dst_phys =__pfn_to_phys (dst_pfn); + src_phys =__pfn_to_phys (src_pfn); + +if (rmi_delegate_page (dst_phys)) +return -ENXIO ; + +retry: + ret =rmi_rtt_data_map_init (rd, dst_phys, ipa, src_phys, flags); +if (ret >=0 &&RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + +KVM_BUG_ON (level >=KVM_PGTABLE_LAST_LEVEL , kvm);
arch/arm64/kvm/rmi.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 646
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ + ret =realm_create_rtt_levels (realm, ipa, level, + level +1 ,NULL ); +if (!ret) + goto retry; + } + +if (ret &&WARN_ON (rmi_undelegate_page (dst_phys))) { +/* Leak the page if the undelegate fails */ +get_page (pfn_to_page (dst_pfn));
Is there some way to avoid taking a reference on the page? This would interfere with conversions. There was a similar discussion for TDX as well [1].
TDX originally incremented folio refounts for these:
+ when mapping folios into the Secure EPTs. This one was easier to agree to remove, since TDX can trust guest_memfd to keep pages around on behalf of the guest.
+ To indicate unmapping failure (IIUC this is the same situation as above). This interferes with conversions.
+ An alternative discussed was to mark these pages as HWPOISON, but that was eventually rejected as adding unnecessary complexity to make TDX special for code paths that only occur on kernel bugs. (In TDX's case the unmap failures would probably only be for kernel bugs.)
+ I later worked a bit more on memory failure for guest_memfd HugeTLB and found that because we will need to restructure huge pages for conversions, using the HWPOISON flag would be hard to handle. For TDX since the conclusion was not to use a HWPOISON flag to indicate unmap failures anyway, this turned out to be a non-issue. Nobody wanted to use the HWPOISON flag. (I hope you won't need to either)
So for TDX, on an unmap failure we do a KVM_BUG_ON() and mark the VM as dead, and do nothing about the page, it still gets returned to the system as if nothing happened.
Here's my understanding of why this is okay for TDX (Rick and Yan, could you please help me here):
+ For unmap failures, the page remains in TDX's Physical Address Metadata Table (PAMT), and the page is still assigned to some TD.
+ If the page was assigned to some other TD, it would be blocked, since the PAMT shows it as already assigned.
+ If the page was used by something completely unrelated to TDX, then in the TDX model the host is free to write and read pages. Nothing goes bad until the TD tries to use that same page, but that TD would never use the page again, that TD is already dead and the HKID for the TD was leaked.
Quoted 19 lines
arch/arm64/kvm/rmi.c +19
-0+ } + +return ret <=0 ? ret : -ENXIO ; +} + +static int populate_region_cb (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn, +struct page *src_page,void *opaque) +{ +unsigned long data_flags = *(unsigned long *)opaque; +phys_addr_t ipa =gfn_to_gpa (gfn); + +return realm_data_map_init (kvm, ipa, pfn,page_to_pfn (src_page), + data_flags); +} + +static long populate_region(struct kvm *kvm, + gfn_t base_gfn, + unsigned long pages, + u64 uaddr,
arch/arm64/kvm/rmi.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 675
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ unsigned long data_flags) +{ +long ret =0 ; + +lockdep_assert_held (&kvm->slots_lock); +lockdep_assert_held (&kvm->arch.config_lock); + +if (!uaddr) +return -EINVAL ; +
Why not check for !uaddr together with the other checks in kvm_arm_rmi_populate?
Also would it be okay to inline populate_region into kvm_arm_rmi_populate below?
Quoted 67 lines
arch/arm64/kvm/rmi.c +55
-1+ ret =kvm_gmem_populate (kvm, base_gfn,u64_to_user_ptr (uaddr), pages, + false, populate_region_cb, &data_flags); + +return ret; +} +enum ripas_action {RIPAS_INIT ,RIPAS_SET ,@@ -727,6 +797,55 @@ static int realm_ensure_created(struct kvm *kvm)return realm_create_rd (kvm); } +int kvm_arm_rmi_populate (struct kvm *kvm, +struct kvm_arm_rmi_populate *args) +{ +unsigned long data_flags =0 ; +unsigned long ipa_start = args->base; +unsigned long ipa_end = ipa_start + args->size; +long pages_populated; +int ret; + +if (args->reserved || + (args->flags & ~KVM_ARM_RMI_POPULATE_FLAGS_MEASURE ) || + args->base + args->size < args->base || + !IS_ALIGNED (ipa_start,PAGE_SIZE ) || + !IS_ALIGNED (ipa_end,PAGE_SIZE ) || + !IS_ALIGNED (args->source_uaddr,PAGE_SIZE )) +return -EINVAL ; + +if (args->flags &KVM_ARM_RMI_POPULATE_FLAGS_MEASURE ) + data_flags |=RMI_MEASURE_CONTENT ; + +mutex_lock (&kvm->slots_lock); +mutex_lock (&kvm->arch.config_lock); + + ret =realm_ensure_created (kvm); +if (ret) + goto out_unlock; + +if (args->size ==0 ) + goto out_unlock; + + pages_populated =populate_region (kvm,gpa_to_gfn (ipa_start), + args->size >>PAGE_SHIFT , + args->source_uaddr, data_flags); + +if (pages_populated <0 ) { + ret = pages_populated; + goto out_unlock; + } + + args->size -= pages_populated <<PAGE_SHIFT ; + args->source_uaddr += pages_populated <<PAGE_SHIFT ; + args->base += pages_populated <<PAGE_SHIFT ; + +out_unlock: +mutex_unlock (&kvm->arch.config_lock); +mutex_unlock (&kvm->slots_lock); +return ret; +} +static int kvm_complete_ripas_change (struct kvm_vcpu *vcpu) {struct kvm *kvm = vcpu->kvm; --2.43.0
Suzuki K Poulose
reviewerRe: [PATCH v16 27/45] KVM: arm64: CCA: Allow populating initial contents
Quoted 7 lines · Ackerley Tng
Quoted 5 lines · 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.
Signed-off-by Steven Price <[email protected]>
...
Quoted 31 lines
Quoted 31 lines
arch/arm64/kvm/rmi.c +31
-0+static int realm_data_map_init (struct kvm *kvm,unsigned long ipa, +kvm_pfn_t dst_pfn,kvm_pfn_t src_pfn, +unsigned long flags) +{ +struct realm *realm = &kvm->arch.realm; +phys_addr_t rd =virt_to_phys (realm->rd); +phys_addr_t dst_phys, src_phys; +long ret; + +lockdep_assert_held (&kvm->slots_lock); +lockdep_assert_held (&kvm->arch.config_lock); + + dst_phys =__pfn_to_phys (dst_pfn); + src_phys =__pfn_to_phys (src_pfn); + +if (rmi_delegate_page (dst_phys)) +return -ENXIO ; + +retry: + ret =rmi_rtt_data_map_init (rd, dst_phys, ipa, src_phys, flags); +if (ret >=0 &&RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + +KVM_BUG_ON (level >=KVM_PGTABLE_LAST_LEVEL , kvm); + + ret =realm_create_rtt_levels (realm, ipa, level, + level +1 ,NULL ); +if (!ret) + goto retry; + }
Quoted 8 lines
arch/arm64/kvm/rmi.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 646
Quoted 4 lines
arch/arm64/kvm/rmi.c +4
-0+ +if (ret &&WARN_ON (rmi_undelegate_page (dst_phys))) { +/* Leak the page if the undelegate fails */ +get_page (pfn_to_page (dst_pfn));Is there some way to avoid taking a reference on the page? This would interfere with conversions. There was a similar discussion for TDX as well [1].
Unfortunately, no. The page was transferred to the Realm world (with rmi_delegate_page() above the retry: ). If we fail to bring it back, that page is still in the Realm PAS and any access to it by the normal world would result in a GPF and eventually bring down the system if it happens from the kernel.
We don't expect that undelegate to fail. The granule_delegate() should fail if the page was already in use by the RMM for some purpose (e.g., already mapped at the IPA, because VMM issued DATA_MAP_INIT twice. Even with the relaxation coming in the RMM, we will mandate that the "populate" cases will request strict conditions for granule delegate).
Please note that this is NOT the "unmap" failure, but it is "Bring the page back to the NS world" failure that causes the WARN_ON and the leaking.
Quoted 28 lines
TDX originally incremented folio refounts for these:
+ when mapping folios into the Secure EPTs. This one was easier to agree to remove, since TDX can trust
guest_memfdto keep pages around on behalf of the guest.
+ To indicate unmapping failure (IIUC this is the same situation as above). This interferes with conversions.
+ An alternative discussed was to mark these pages as HWPOISON, but that was eventually rejected as adding unnecessary complexity to make TDX special for code paths that only occur on kernel bugs. (In TDX's case the unmap failures would probably only be for kernel bugs.)
+ I later worked a bit more on memory failure forguest_memfdHugeTLB and found that because we will need to restructure huge pages for conversions, using the HWPOISON flag would be hard to handle. For TDX since the conclusion was not to use a HWPOISON flag to indicate unmap failures anyway, this turned out to be a non-issue. Nobody wanted to use the HWPOISON flag. (I hope you won't need to either)So for TDX, on an unmap failure we do a
KVM_BUG_ON()and mark the VM as dead, and do nothing about the page, it still gets returned to the system as if nothing happened.Here's my understanding of why this is okay for TDX (Rick and Yan, could you please help me here):
Quoted 9 lines
+ For unmap failures, the page remains in TDX's Physical Address Metadata Table (PAMT), and the page is still assigned to some TD.
+ If the page was assigned to some other TD, it would be blocked, since the PAMT shows it as already assigned.
+ If the page was used by something completely unrelated to TDX, then in the TDX model the host is free to write and read pages. Nothing goes bad until the TD tries to use that same page, but that TD would never use the page again, that TD is already dead and the HKID for the TD was leaked.
This is not true for CCA. Like I said above, touching the page in Realm PAS is going to be disastrous for the Host.
Quoted 27 lines
[1] https://lore.kernel.org/all/[email protected]/
Quoted 24 lines
arch/arm64/kvm/rmi.c +24
-0+ } + +return ret <=0 ? ret : -ENXIO ; +} + +static int populate_region_cb (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn, +struct page *src_page,void *opaque) +{ +unsigned long data_flags = *(unsigned long *)opaque; +phys_addr_t ipa =gfn_to_gpa (gfn); + +return realm_data_map_init (kvm, ipa, pfn,page_to_pfn (src_page), + data_flags); +} + +static long populate_region (struct kvm *kvm, +gfn_t base_gfn, +unsigned long pages, +u64 uaddr, +unsigned long data_flags) +{ +long ret =0 ; + +lockdep_assert_held (&kvm->slots_lock);
Quoted 8 lines
arch/arm64/kvm/rmi.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start, · line 675
Quoted 5 lines
arch/arm64/kvm/rmi.c +5
-0+lockdep_assert_held (&kvm->arch.config_lock); + +if (!uaddr) +return -EINVAL ; +Why not check for !uaddr together with the other checks in
kvm_arm_rmi_populate?
Yep, we could move it there.
Quoted 3 lines
Also would it be okay to inline
populate_regionintokvm_arm_rmi_populatebelow?
Ack.
Suzuki
PATCH v16 28/45 KVM: arm64: CCA: Set RIPAS of initial memslots
In reply to Steven PriceThe memory which the realm guest accesses must be set to RIPAS_RAM. Iterate over the memslots and set all gmem memslots to RIPAS_RAM.
---
New patch for v12.
--- arch/arm64/kvm/rmi.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
arch/arm64/kvm/rmi.c +38 -0
@@ -779,6 +779,14 @@ static int realm_set_ipa_state(struct kvm_vcpu *vcpu,return ret; } +static int realm_init_ipa_state (struct kvm *kvm, +unsigned long gfn, +unsigned long pages) +{ +return ripas_change (kvm,NULL ,gfn_to_gpa (gfn),gfn_to_gpa (gfn + pages), +RIPAS_INIT ,NULL ); +} +static int realm_ensure_created (struct kvm *kvm) {lockdep_assert_held (&kvm->arch.config_lock);
@@ -797,6 +805,32 @@ static int realm_ensure_created(struct kvm *kvm)return realm_create_rd (kvm); } +static int set_ripas_of_protected_regions (struct kvm *kvm) +{ +struct kvm_memslots *slots; +struct kvm_memory_slot *memslot; +int idx, bkt; +int ret =0 ; + +lockdep_assert_held (&kvm->arch.config_lock); + + idx =srcu_read_lock (&kvm->srcu); + + slots =kvm_memslots (kvm); +kvm_for_each_memslot (memslot, bkt, slots) { +if (!kvm_slot_has_gmem (memslot)) +continue ; + + ret =realm_init_ipa_state (kvm, memslot->base_gfn, + memslot->npages); +if (ret) +break ; + } +srcu_read_unlock (&kvm->srcu, idx); + +return ret; +} +int kvm_arm_rmi_populate (struct kvm *kvm,struct kvm_arm_rmi_populate *args) {
@@ -1084,6 +1118,10 @@ int kvm_activate_realm(struct kvm *kvm)return ret; } + ret =set_ripas_of_protected_regions (kvm); +if (ret) +return ret; + ret =rmi_realm_activate (virt_to_phys (realm->rd));if (ret)return -ENXIO ;
Signature · 1 line
2.43.0
PATCH v16 29/45 KVM: arm64: CCA: Support runtime faulting of memory
In reply to Steven PriceAt 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 v15:
* Handle negative error codes.
Changes since v13:
* Numerous changes due to rebasing.
* Fix addr_range_desc() to encode the correct block size.
Changes since v12:
* Switch to RMM v2.0 range based APIs.
Changes since v11:
* Adapt to upstream changes.
Changes since v10:
* RME->RMI renaming.
* Adapt to upstream gmem changes.
Changes since v9:
* Fix call to kvm_stage2_unmap_range() in kvm_free_stage2_pgd() to set may_block to avoid stall warnings.
* Minor coding style fixes.
Changes since v8:
* Propagate the may_block flag.
* Minor comments and coding style changes.
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 | 8 + arch/arm64/include/asm/kvm_rmi.h | 5 + arch/arm64/kvm/mmu.c | 97 ++++++++---- arch/arm64/kvm/rmi.c | 220 +++++++++++++++++++++++++++ 4 files changed, 304 insertions(+), 26 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +8 -0
@@ -712,6 +712,14 @@ 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)) +return ipa; + +return ipa & ~BIT (kvm->arch.realm.ia_bits -1 ); +} +static inline bool vcpu_is_rec (const struct kvm_vcpu *vcpu) {return kvm_is_realm (vcpu->kvm);
arch/arm64/include/asm/kvm_rmi.h +5 -0
@@ -6,6 +6,7 @@#ifndef __ASM_KVM_RMI_H#define __ASM_KVM_RMI_H +#include <asm/kvm_pgtable.h> #include <linux/arm-smccc-rmi.h> /**
@@ -117,6 +118,10 @@ void kvm_realm_unmap_range(struct kvm *kvm,unsigned long size, bool unmap_private, bool may_block); +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);static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr)
arch/arm64/kvm/mmu.c +71 -26
210 lines not on this page · raw message
arch/arm64/kvm/rmi.c +220 -0
226 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v16 29/45] KVM: arm64: CCA: Support runtime faulting of memory
Quoted 173 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.
Signed-off-by Steven Price <[email protected]>---
Changes since v15:
* Handle negative error codes.
Changes since v13:
* Numerous changes due to rebasing.
* Fixaddr_range_desc()to encode the correct block size.
Changes since v12:
* Switch to RMM v2.0 range based APIs.
Changes since v11:
* Adapt to upstream changes.
Changes since v10:
*RME->RMIrenaming.
* Adapt to upstream gmem changes.
Changes since v9:
* Fix call tokvm_stage2_unmap_range()inkvm_free_stage2_pgd()to setmay_blockto avoid stall warnings.
* Minor coding style fixes.
Changes since v8:
* Propagate themay_blockflag.
* Minor comments and coding style changes.
Changes since v7:
* Remove redundantWARN_ONsforrealm_create_rtt_levels()- it will internally WARN when necessary.
Changes since v6:
* HandlePAGE_SIZEbeing 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 topopulate_par_region()are moved into the previous patch where they belong.
Changes since v4:
* Code cleanup following review feedback.
* Drop thePTE_SHAREDbit 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 (seertt_get_phys()).
* Adapt to changes in previous patches.--- arch/arm64/include/asm/kvm_emulate.h | 8 + arch/arm64/include/asm/kvm_rmi.h | 5 + arch/arm64/kvm/mmu.c | 97 ++++++++---- arch/arm64/kvm/rmi.c | 220 +++++++++++++++++++++++++++ 4 files changed, 304 insertions(+), 26 deletions(-)arch/arm64/include/asm/kvm_emulate.h +8
-0@@ -712,6 +712,14 @@ 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)) +return ipa; + +return ipa & ~BIT (kvm->arch.realm.ia_bits -1 ); +} +static inline bool vcpu_is_rec (const struct kvm_vcpu *vcpu) {return kvm_is_realm (vcpu->kvm);arch/arm64/include/asm/kvm_rmi.h +5
-0@@ -6,6 +6,7 @@#ifndef __ASM_KVM_RMI_H#define __ASM_KVM_RMI_H +#include <asm/kvm_pgtable.h> #include <linux/arm-smccc-rmi.h> /** @@ -117,6 +118,10 @@ void kvm_realm_unmap_range(struct kvm *kvm,unsigned long size, bool unmap_private, bool may_block); +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);static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr)arch/arm64/kvm/mmu.c +23
-7@@ -334,8 +334,15 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64lockdep_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, + may_block); + }else { +WARN_ON (stage2_apply_range (mmu, start, end, +KVM_PGT_FN (kvm_pgtable_stage2_unmap), + may_block)); + } } void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,@@ -358,7 +365,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, true); +else +kvm_stage2_flush_range (&kvm->arch.mmu, addr, end); }/** @@ -1137,6 +1147,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);@@ -1641,18 +1655,20 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)bool perm_fault =kvm_vcpu_trap_is_permission_fault (s2fd->vcpu);enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ;enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ; -struct kvm_pgtable *pgt = s2fd->vcpu->arch.hw_mmu->pgt; +struct kvm_vcpu *vcpu = s2fd->vcpu; +struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt; +gpa_t gpa =kvm_gpa_from_fault (vcpu->kvm, s2fd->fault_ipa);unsigned long mmu_seq;struct page *page; -struct kvm *kvm = s2fd->vcpu->kvm; +struct kvm *kvm = vcpu->kvm;void *memcache =NULL ;kvm_pfn_t pfn;gfn_t gfn;int ret;if (!perm_fault) { - memcache =get_mmu_memcache (s2fd->vcpu); - ret =topup_mmu_memcache (s2fd->vcpu, memcache); + memcache =get_mmu_memcache (vcpu); + ret =topup_mmu_memcache (vcpu, memcache);if (ret)return ret; }
arch/arm64/kvm/mmu.c static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) · line 1679
Quoted 5 lines
arch/arm64/kvm/mmu.c +1
-1@@ -1660,10 +1676,10 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)if (s2fd->nested) gfn =kvm_s2_trans_output (s2fd->nested) >>PAGE_SHIFT ;else - gfn = s2fd->fault_ipa >>PAGE_SHIFT ; + gfn = gpa >>PAGE_SHIFT ;
I've seen a gpa_to_gfn(), not sure if it works for ARM or works here.
Quoted 74 lines
arch/arm64/kvm/mmu.c +22
-12- write_fault =kvm_is_write_fault (s2fd->vcpu); - exec_fault =kvm_vcpu_trap_is_exec_fault (s2fd->vcpu); + write_fault =kvm_is_write_fault (vcpu); + exec_fault =kvm_vcpu_trap_is_exec_fault (vcpu);VM_WARN_ON_ONCE (write_fault && exec_fault);@@ -1673,7 +1689,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) ret =kvm_gmem_get_pfn (kvm, s2fd->memslot, gfn, &pfn, &page,NULL );if (ret) { -kvm_prepare_memory_fault_exit (s2fd->vcpu, s2fd->fault_ipa,PAGE_SIZE , +kvm_prepare_memory_fault_exit (vcpu, gpa,PAGE_SIZE , write_fault, exec_fault, false);return ret; }@@ -1693,7 +1709,14 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)kvm_fault_lock (kvm);if (mmu_invalidate_retry (kvm, mmu_seq)) { ret = -EAGAIN ; - goto out_unlock; + goto out_release_page; + } + +if (kvm_is_realm (kvm)) { + prot &= ~KVM_PGTABLE_PROT_X ; + ret =realm_map_ipa (kvm, s2fd->fault_ipa, pfn, +PAGE_SIZE , prot, memcache); + goto out_release_page; }if (perm_fault) {@@ -1710,7 +1733,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) memcache, flags); } -out_unlock: +out_release_page:kvm_release_faultin_page (kvm, page, !!ret, prot &KVM_PGTABLE_PROT_W );kvm_fault_unlock (kvm);@@ -1896,7 +1919,7 @@ static int kvm_s2_fault_get_vma_info(const struct kvm_s2_fault_desc *s2fd,* mapping size to ensure we find the right PFN and lay down the * mapping in the right place. */ - s2vi->gfn =ALIGN_DOWN (s2fd->fault_ipa, s2vi->vma_pagesize) >>PAGE_SHIFT ; + s2vi->gfn =kvm_gpa_from_fault (kvm,ALIGN_DOWN (s2fd->fault_ipa, s2vi->vma_pagesize)) >>PAGE_SHIFT ; s2vi->mte_allowed =kvm_vma_mte_allowed (vma);@@ -2092,12 +2115,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,if (!perm_fault_granule && !s2vi->map_non_cacheable &&kvm_has_mte (kvm))sanitise_mte_tags (kvm, pfn, mapping_size); -/* -* Under the premise of getting a FSC_PERM fault, we just need to relax -* permissions only if mapping_size equals perm_fault_granule. Otherwise, -* kvm_pgtable_stage2_map() should be called to change block size. -*/ -if (mapping_size == perm_fault_granule) { +if (kvm_is_realm (kvm)) { + ret =realm_map_ipa (kvm, s2fd->fault_ipa, pfn, mapping_size, + prot, memcache); + }else if (mapping_size == perm_fault_granule) { +/* +* Under the premise of getting a FSC_PERM fault, we just need to relax +* permissions only if mapping_size equals perm_fault_granule. Otherwise, +* kvm_pgtable_stage2_map() should be called to change block size. +*/ /* * Drop the SW bits in favour of those stored in the * PTE, which will be preserved.
arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2296
Quoted 8 lines
arch/arm64/kvm/mmu.c +5
-0@@ -2263,6 +2289,13 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)return 0 ; } +static bool shared_ipa_fault (struct kvm *kvm,phys_addr_t fault_ipa) +{ +gpa_t gpa =kvm_gpa_from_fault (kvm, fault_ipa); + +return (gpa != fault_ipa);
If I understood the intent here I think checking kvm->arch.realm.ia_bits is more obvious. It'd be actually checking if the address is private or shared rather than going through kvm_gpa_from_fault() indirectly.
Also perhaps define private_ipa_fault() would be better instead of defining shared and then checking !shared below.
Actually there's already kvm_realm_is_private_address(), perhaps that can be reused.
Quoted 48 lines
arch/arm64/kvm/mmu.c +12
-3+} +/** * kvm_handle_guest_abort - handles all 2nd stage aborts * @vcpu: the VCPU pointer @@ -2373,8 +2406,9 @@ 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); + 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)) {@@ -2417,7 +2451,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)* of the page size. */ ipa |=FAR_TO_FIPA_OFFSET (kvm_vcpu_get_hfar (vcpu)); - ret =io_mem_abort (vcpu, ipa); + ret =io_mem_abort (vcpu,kvm_gpa_from_fault (vcpu->kvm, ipa)); goto out_unlock; }@@ -2445,7 +2479,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) !write_fault && !kvm_vcpu_trap_is_exec_fault(vcpu)); -if (kvm_slot_has_gmem (memslot)) +if (kvm_slot_has_gmem (memslot) && + (kvm_memslot_is_gmem_only (memslot) || + !shared_ipa_fault (vcpu->kvm, fault_ipa))) ret =gmem_abort (&s2fd);else ret =user_mem_abort (&s2fd);@@ -2482,6 +2518,10 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)if (!kvm->arch.mmu.pgt ||kvm_vm_is_protected (kvm))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);
arch/arm64/kvm/mmu.c bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) · line 2547
Quoted 10 lines
arch/arm64/kvm/mmu.c +4
-0@@ -2498,6 +2538,10 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)if (!kvm->arch.mmu.pgt ||kvm_vm_is_protected (kvm))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);
These sound like they should be in a separate patch since it's to do with aging and not faulting.
Quoted 90 lines
arch/arm64/kvm/mmu.c +4
-3@@ -2677,10 +2721,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,return -EFAULT ;/* -* Only support guest_memfd backed memslots with mappable memory, since -* there aren't any CoCo VMs that support only private memory on arm64. +* Only support guest_memfd backed memslots with mappable memory, +* unless the guest is a CCA realm guest. */ -if (kvm_slot_has_gmem (new) && !kvm_memslot_is_gmem_only (new)) +if (kvm_slot_has_gmem (new) && !kvm_memslot_is_gmem_only (new) && + !kvm_is_realm (kvm))return -EINVAL ; hva = new->userspace_addr;arch/arm64/kvm/rmi.c +64
-0@@ -649,6 +649,226 @@ static int realm_data_map_init(struct kvm *kvm, unsigned long ipa,return ret <=0 ? ret : -ENXIO ; } +static int realm_map_protected (struct kvm *kvm, +unsigned long ipa, +kvm_pfn_t pfn, +unsigned long map_size, +struct kvm_mmu_memory_cache *memcache) +{ +struct realm *realm = &kvm->arch.realm; +phys_addr_t phys =__pfn_to_phys (pfn); +phys_addr_t base_phys = phys; +phys_addr_t rd =virt_to_phys (realm->rd); +phys_addr_t delegated_phys; +unsigned long base_ipa = ipa; +unsigned long ipa_top; +long ret =0 ; + +if (WARN_ON (!IS_ALIGNED (map_size,PAGE_SIZE ) || + !IS_ALIGNED (ipa, map_size))) +return -EINVAL ; + +if (rmi_delegate_range (phys, map_size, &delegated_phys)) { +if (delegated_phys == 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 ; + } +/* Partial delegation - map as much as we can */ + map_size = delegated_phys - phys; + } + + ipa_top = ipa + map_size; + +while (ipa < ipa_top) { +unsigned long flags =RMI_ADDR_TYPE_SINGLE ; +unsigned long range_desc =addr_range_desc (phys, ipa_top - ipa, +RMI_OP_MEM_DELEGATED ); +unsigned long out_top; + + ret =rmi_rtt_data_map (rd, ipa, ipa_top, flags, range_desc, + &out_top); +if (ret <0 ) + goto err_undelegate; + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + +if (WARN_ON (level >=KVM_PGTABLE_LAST_LEVEL )) + goto err_undelegate; + ret =realm_create_rtt_levels (realm, ipa, level, + level +1 , + memcache); +if (ret) + goto err_undelegate; + +continue ; + } + +if (WARN_ON (ret)) + goto err_undelegate; + + phys += out_top - ipa;
arch/arm64/kvm/rmi.c static int realm_data_map_init(struct kvm *kvm, unsigned long ipa, · line 763
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ ipa = out_top; + } + +return 0 ; + +err_undelegate: +realm_unmap_private_range (kvm, base_ipa, ipa, true); +if (WARN_ON (rmi_undelegate_range (base_phys, map_size))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (base_phys));
Please see my comment on the other patch about taking a refcount. This will interfere with conversions.
Quoted 7 lines
arch/arm64/kvm/rmi.c +4
-0+ } +return ret <0 ? ret : -ENXIO ; +} +
Catalin Marinas
maintainerRe: [PATCH v16 29/45] KVM: arm64: CCA: Support runtime faulting of memory
In reply to Steven PriceQuoted 7 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.
Is this still true with in-place guestmem conversion?
Quoted 14 lines
arch/arm64/kvm/mmu.c +8
-1@@ -1693,7 +1709,14 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)kvm_fault_lock (kvm);if (mmu_invalidate_retry (kvm, mmu_seq)) { ret = -EAGAIN ; - goto out_unlock; + goto out_release_page; + } + +if (kvm_is_realm (kvm)) { + prot &= ~KVM_PGTABLE_PROT_X ; + ret =realm_map_ipa (kvm, s2fd->fault_ipa, pfn, +PAGE_SIZE , prot, memcache); + goto out_release_page; }
[...]
Quoted 8 lines
arch/arm64/kvm/rmi.c +8
-0+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; + + ipa =ALIGN_DOWN (ipa, map_size);
arch/arm64/kvm/rmi.c static int realm_data_map_init(struct kvm *kvm, unsigned long ipa, · line 870
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+if (!kvm_realm_is_private_address (realm, ipa)) { +return realm_map_non_secure (kvm, ipa, pfn, map_size, prot, + memcache); + } + +/* It's impossible to map protected pages read-only. */ +if (WARN_ON (!(prot &KVM_PGTABLE_PROT_W ))) +return -EFAULT ; +return realm_map_protected (kvm, ipa, pfn, map_size, memcache); +}
I was trying to understand (with the help of some LLMs) to understand whether we can end up on the do_gpf() path as a result of VMM actions. The above kvm_realm_is_private_address() only checks for the IPA but does not check against guestmem if the page is truly private. I probably miss something but the scenario would be something like:
1. VMM creates the gmem region with GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_INIT_SHARED, mmap()able and GUP-pinnable
2. VMM starts an O_DIRECT write() from that mapping; the block layer FOLL_PINs the shared folio
3. VMM runs a vCPU so the realm touches the protected-IPA alias of the same gfn. gmem_abort() delegates the pinned, still-shared page to the RMM
4. The in-flight I/O then reads the now-Realm page from the kernel linear map. That access takes a GPF at EL1, so do_gpf() -> die_kernel_fault()
x86, IIUC, also checks kvm_gmem_is_private(). They also have kvm_arch_gmem_make_private() triggered on the kvm_gmem_get_pfn() path but I got lost in the call sites, not sure whether that's strictly needed if we check both private IPA and kvm_gmem_is_private().
Signature · 1 line
Catalin
Suzuki K Poulose
reviewerRe: [PATCH v16 29/45] KVM: arm64: CCA: Support runtime faulting of memory
Quoted 59 lines · Catalin Marinas
Quoted 7 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.
Is this still true with in-place guestmem conversion?
Quoted 14 lines
arch/arm64/kvm/mmu.c +8
-1@@ -1693,7 +1709,14 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)kvm_fault_lock (kvm);if (mmu_invalidate_retry (kvm, mmu_seq)) { ret = -EAGAIN ; - goto out_unlock; + goto out_release_page; + } + +if (kvm_is_realm (kvm)) { + prot &= ~KVM_PGTABLE_PROT_X ; + ret =realm_map_ipa (kvm, s2fd->fault_ipa, pfn, +PAGE_SIZE , prot, memcache); + goto out_release_page; }[...]
Quoted 8 lines
arch/arm64/kvm/rmi.c +8
-0+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; + + ipa =ALIGN_DOWN (ipa, map_size);arch/arm64/kvm/rmi.c static int realm_data_map_init(struct kvm *kvm, unsigned long ipa, · line 870
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+if (!kvm_realm_is_private_address (realm, ipa)) { +return realm_map_non_secure (kvm, ipa, pfn, map_size, prot, + memcache); + } + +/* It's impossible to map protected pages read-only. */ +if (WARN_ON (!(prot &KVM_PGTABLE_PROT_W ))) +return -EFAULT ; +return realm_map_protected (kvm, ipa, pfn, map_size, memcache); +}I was trying to understand (with the help of some LLMs) to understand whether we can end up on the
do_gpf()path as a result of VMM actions. The abovekvm_realm_is_private_address()only checks for the IPA but does not check against guestmem if the page is truly private. I probably miss something but the scenario would be something like:1. VMM creates the gmem region with
GUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHARED, mmap()able and GUP-pinnable2. VMM starts an
O_DIRECTwrite()from that mapping; the block layerFOLL_PINsthe shared folio
Quoted 7 lines
3. VMM runs a vCPU so the realm touches the protected-IPA alias of the same gfn.
gmem_abort()delegates the pinned, still-shared page to the RMM4. The in-flight I/O then reads the now-Realm page from the kernel
linear map. That access takes a GPF at EL1, so do_gpf() -> die_kernel_fault()
This is correct. The fundamental issue is that we have a disconnect between the "gmem attribute" changes (to private/shared) and the RIPAS and this is something we want to fix.
e.g., the KVM CCA driver sets the entire DRAM to RIPAS_RAM for the Realm before ACTIVATION and we expect that the VMM changes the gmem to PRIVATE. They both are not in sync. his is something we were discussing the other day with Aneesh.
Once they are in sync, we are protected. If the RIPAS=EMPTY (gmem=shared) a stage2 fault doesn't come to the Host.
If the RIPAS=RAM, the gmem is private and there are no usespace mappings.
The reason why it is disconnected at the moment is due to the weird semantics for Guest triggered conversions in CCA i.e., Realm requests via RSI_IPA_STATE_SET, triggering a RIPAS_CHANGE exit to the KVM.
The KVM exits to VMM with MEMORY_FAULT_EXIT and the VMM can service this by invoking GMEM(SET_ATTRIBUTES2). But, the KVM needs to invoke the RMI_RTT_SET_RIPAS in the context of the "vcpu", which we don't have from the "kvm" context. I guess we can fix it by running through the vcpus and find the matching one with the "ipa" range and the "ripas".
And as for the VMM requests before the Realm is activated, we could map that to RMI_RTT_INIT_RIPAS and removing the implicit change from KVM arm64.
Thanks
Suzuki
Quoted 6 lines
x86, IIUC, also checks
kvm_gmem_is_private(). They also havekvm_arch_gmem_make_private()triggered on thekvm_gmem_get_pfn()path but I got lost in the call sites, not sure whether that's strictly needed if we check both private IPA andkvm_gmem_is_private().
Catalin Marinas
maintainerRe: [PATCH v16 29/45] KVM: arm64: CCA: Support runtime faulting of memory
Quoted 83 lines · Suzuki K Poulose
Quoted 59 lines · Catalin Marinas
Quoted 7 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.
Is this still true with in-place guestmem conversion?
Quoted 14 lines
arch/arm64/kvm/mmu.c +8
-1@@ -1693,7 +1709,14 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)kvm_fault_lock (kvm);if (mmu_invalidate_retry (kvm, mmu_seq)) { ret = -EAGAIN ; - goto out_unlock; + goto out_release_page; + } + +if (kvm_is_realm (kvm)) { + prot &= ~KVM_PGTABLE_PROT_X ; + ret =realm_map_ipa (kvm, s2fd->fault_ipa, pfn, +PAGE_SIZE , prot, memcache); + goto out_release_page; }[...]
Quoted 8 lines
arch/arm64/kvm/rmi.c +8
-0+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; + + ipa =ALIGN_DOWN (ipa, map_size);arch/arm64/kvm/rmi.c static int realm_data_map_init(struct kvm *kvm, unsigned long ipa, · line 870
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+if (!kvm_realm_is_private_address (realm, ipa)) { +return realm_map_non_secure (kvm, ipa, pfn, map_size, prot, + memcache); + } + +/* It's impossible to map protected pages read-only. */ +if (WARN_ON (!(prot &KVM_PGTABLE_PROT_W ))) +return -EFAULT ; +return realm_map_protected (kvm, ipa, pfn, map_size, memcache); +}I was trying to understand (with the help of some LLMs) to understand whether we can end up on the
do_gpf()path as a result of VMM actions. The abovekvm_realm_is_private_address()only checks for the IPA but does not check against guestmem if the page is truly private. I probably miss something but the scenario would be something like:1. VMM creates the gmem region with
GUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHARED, mmap()able and GUP-pinnable2. VMM starts an
O_DIRECTwrite()from that mapping; the block layerFOLL_PINsthe shared folioQuoted 7 lines
3. VMM runs a vCPU so the realm touches the protected-IPA alias of the same gfn.
gmem_abort()delegates the pinned, still-shared page to the RMM4. The in-flight I/O then reads the now-Realm page from the kernel
linear map. That access takes a GPF at EL1, so do_gpf() -> die_kernel_fault()This is correct. The fundamental issue is that we have a disconnect between the "gmem attribute" changes (to private/shared) and the RIPAS and this is something we want to fix.
e.g., the KVM CCA driver sets the entire DRAM to
RIPAS_RAMfor the Realm before ACTIVATION and we expect that the VMM changes the gmem to PRIVATE. They both are not in sync. his is something we were discussing the other day with Aneesh.Once they are in sync, we are protected. If the RIPAS=EMPTY (gmem=shared) a stage2 fault doesn't come to the Host.
If the RIPAS=RAM, the gmem is private and there are no usespace mappings.
Quoted 7 lines
The reason why it is disconnected at the moment is due to the weird semantics for Guest triggered conversions in CCA i.e., Realm requests via
RSI_IPA_STATE_SET, triggering aRIPAS_CHANGEexit to the KVM.The KVM exits to VMM with
MEMORY_FAULT_EXITand the VMM can service this by invokingGMEM(SET_ATTRIBUTES2).
I guess a buggy or malicious VMM may skip the gmem attribute setting and simply resume the guest. Currently we can end up with SET_RIPAS irrespective of what the VMM did. So at this point maybe we need to check that the gmem attribute was actually changed before handling the pending RMI requests.
The other place to check the gmem status is when handling the gmem_abort().
I wonder whether we can have any races with either of these if multiple vCPUs toggle the RIPAS state between RAM and EMPTY and we need both places (the RIPAS_CHANGE exit and the actual stage 2 fault).
Quoted 4 lines
But, the KVM needs to invoke the
RMI_RTT_SET_RIPASin the context of the "vcpu", which we don't have from the "kvm" context. I guess we can fix it by running through the vcpus and find the matching one with the "ipa" range and the "ripas".
Or multiple vcpus? Does the spec allow multiple RIPAS_CHANGE requests for the same IPA? If yes, we probably need to clear all.
Signature · 1 line
Catalin
Suzuki K Poulose
reviewerRe: [PATCH v16 29/45] KVM: arm64: CCA: Support runtime faulting of memory
Quoted 92 lines · Catalin Marinas
Quoted 83 lines · Suzuki K Poulose
Quoted 59 lines · Catalin Marinas
Quoted 7 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.
Is this still true with in-place guestmem conversion?
Quoted 14 lines
arch/arm64/kvm/mmu.c +8
-1@@ -1693,7 +1709,14 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)kvm_fault_lock (kvm);if (mmu_invalidate_retry (kvm, mmu_seq)) { ret = -EAGAIN ; - goto out_unlock; + goto out_release_page; + } + +if (kvm_is_realm (kvm)) { + prot &= ~KVM_PGTABLE_PROT_X ; + ret =realm_map_ipa (kvm, s2fd->fault_ipa, pfn, +PAGE_SIZE , prot, memcache); + goto out_release_page; }[...]
Quoted 8 lines
arch/arm64/kvm/rmi.c +8
-0+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; + + ipa =ALIGN_DOWN (ipa, map_size);arch/arm64/kvm/rmi.c static int realm_data_map_init(struct kvm *kvm, unsigned long ipa, · line 870
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+if (!kvm_realm_is_private_address (realm, ipa)) { +return realm_map_non_secure (kvm, ipa, pfn, map_size, prot, + memcache); + } + +/* It's impossible to map protected pages read-only. */ +if (WARN_ON (!(prot &KVM_PGTABLE_PROT_W ))) +return -EFAULT ; +return realm_map_protected (kvm, ipa, pfn, map_size, memcache); +}I was trying to understand (with the help of some LLMs) to understand whether we can end up on the
do_gpf()path as a result of VMM actions. The abovekvm_realm_is_private_address()only checks for the IPA but does not check against guestmem if the page is truly private. I probably miss something but the scenario would be something like:1. VMM creates the gmem region with
GUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHARED, mmap()able and GUP-pinnable2. VMM starts an
O_DIRECTwrite()from that mapping; the block layerFOLL_PINsthe shared folioQuoted 7 lines
3. VMM runs a vCPU so the realm touches the protected-IPA alias of the same gfn.
gmem_abort()delegates the pinned, still-shared page to the RMM4. The in-flight I/O then reads the now-Realm page from the kernel
linear map. That access takes a GPF at EL1, so do_gpf() -> die_kernel_fault()This is correct. The fundamental issue is that we have a disconnect between the "gmem attribute" changes (to private/shared) and the RIPAS and this is something we want to fix.
e.g., the KVM CCA driver sets the entire DRAM to
RIPAS_RAMfor the Realm before ACTIVATION and we expect that the VMM changes the gmem to PRIVATE. They both are not in sync. his is something we were discussing the other day with Aneesh.Once they are in sync, we are protected. If the RIPAS=EMPTY (gmem=shared) a stage2 fault doesn't come to the Host.
If the RIPAS=RAM, the gmem is private and there are no usespace mappings.
Quoted 7 lines
The reason why it is disconnected at the moment is due to the weird semantics for Guest triggered conversions in CCA i.e., Realm requests via
RSI_IPA_STATE_SET, triggering aRIPAS_CHANGEexit to the KVM.The KVM exits to VMM with
MEMORY_FAULT_EXITand the VMM can service this by invokingGMEM(SET_ATTRIBUTES2).
Quoted 8 lines
I guess a buggy or malicious VMM may skip the gmem attribute setting and simply resume the guest. Currently we can end up with
SET_RIPASirrespective of what the VMM did. So at this point maybe we need to check that the gmem attribute was actually changed before handling the pending RMI requests.The other place to check the gmem status is when handling the
gmem_abort().
We did have that check in v14 [0] and forcing a MEMORY_FAULT_EXIT. I thought we didn't need it with in-place conversion, but looks like that was wrong call.
[0] https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
I wonder whether we can have any races with either of these if multiple vCPUs toggle the RIPAS state between RAM and EMPTY and we need both places (the
RIPAS_CHANGEexit and the actual stage 2 fault).
Quoted 7 lines
Quoted 4 lines
But, the KVM needs to invoke the
RMI_RTT_SET_RIPASin the context of the "vcpu", which we don't have from the "kvm" context. I guess we can fix it by running through the vcpus and find the matching one with the "ipa" range and the "ripas".Or multiple vcpus? Does the spec allow multiple
RIPAS_CHANGErequests for the same IPA? If yes, we probably need to clear all.
Ideally, a guest shouldn't pass overlapping requests to the host. Host could reject either of them, irrespective of the order we got it.
I will get this clarified in the spec.
Cheers
Suzuki
PATCH v16 30/45 KVM: arm64: CCA: Handle realm vCPU load
In reply to Steven PriceWhen 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 | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
arch/arm64/kvm/arm.c +12 -7
@@ -707,7 +707,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)struct kvm_s2_mmu *mmu;int *last_ran; -if (is_protected_kvm_enabled ()) +if (is_protected_kvm_enabled () ||kvm_is_realm (vcpu->kvm)) goto nommu;if (vcpu_has_nv (vcpu))
@@ -751,12 +751,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vgic_load (vcpu);kvm_vcpu_load_debug (vcpu);kvm_vcpu_load_fgt (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);if (kvm_vcpu_should_clear_twe (vcpu)) vcpu->arch.hcr_el2 &= ~HCR_TWE ;
@@ -778,6 +772,17 @@ 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 (kvm_arm_is_pvtime_enabled (&vcpu->arch)) +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu); +if (!cpumask_test_cpu (cpu, vcpu->kvm->arch.supported_cpus))vcpu_set_on_unsupported_cpu (vcpu);
Signature · 1 line
2.43.0
Re: [PATCH v16 30/45] KVM: arm64: CCA: Handle realm vCPU load
Quoted 3 lines · 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.
Hi Steven,
I found that Realm guests can execute WFI/WFE instructions without causing a REC exit, ignoring KVM's WFx trap policy.
I'd suggest translating HCR_EL2.TWE/TWI into the corresponding run->enter.flags to honor the policy. Below is the patch I tested.
----
From 8637cd6f818a3c56d8b898a72874e9485eb94e59 Mon Sep 17 00:00:00 2001
From: Kohei Enju <[email protected]>
Date: Mon, 10 Aug 2026 22:54:27 +0900
Subject: [PATCH] KVM: arm64: CCA: Honor WFx trap policy for Realm vCPUs
Propagate KVM's WFx trap policy to the RMM by translating HCR_EL2.TWE/TWI into REC_ENTER TRAP_WFE/WFI flags before entering a REC.
Without this, guests can execute WFE/WFI instructions without causing a REC exit, ignoring KVM's common trap policy. Configure the REC_ENTER flags to honor the policy.
--- arch/arm64/include/asm/kvm_rmi.h | 2 ++ arch/arm64/kvm/arm.c | 3 +++ arch/arm64/kvm/rmi.c | 14 ++++++++++++++ 3 files changed, 19 insertions(+)
arch/arm64/include/asm/kvm_rmi.h +2 -0
@@ -109,6 +109,8 @@ void kvm_destroy_realm(struct kvm *kvm);int kvm_realm_teardown_stage2 (struct kvm *kvm);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +void kvm_rec_set_wfx_traps (struct kvm_vcpu *vcpu); +int kvm_rec_enter (struct kvm_vcpu *vcpu);int kvm_rec_exit (struct kvm_vcpu *vcpu,int rec_run_status);int kvm_rec_handle_request (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +3 -0
@@ -770,6 +770,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)else vcpu->arch.hcr_el2 |=HCR_TWI ; +if (vcpu_is_rec (vcpu)) +kvm_rec_set_wfx_traps (vcpu); +vcpu_set_pauth_traps (vcpu);if (is_protected_kvm_enabled ()) {
arch/arm64/kvm/rmi.c +14 -0
@@ -1302,6 +1302,20 @@ static void noinstr load_realm_timer_state(struct kvm_vcpu *vcpu)write_sysreg_el0 (rec_exit->cntp_ctl,SYS_CNTP_CTL ); } +void kvm_rec_set_wfx_traps (struct kvm_vcpu *vcpu) +{ +struct realm_rec *rec = &vcpu->arch.rec; + + rec->run->enter.flags &= + ~(REC_ENTER_FLAG_TRAP_WFE |REC_ENTER_FLAG_TRAP_WFI ); + +if (vcpu->arch.hcr_el2 &HCR_TWE ) + rec->run->enter.flags |=REC_ENTER_FLAG_TRAP_WFE ; + +if (vcpu->arch.hcr_el2 &HCR_TWI ) + rec->run->enter.flags |=REC_ENTER_FLAG_TRAP_WFI ; +} + int noinstr kvm_rec_enter(struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec;
Signature · 1 line
2.43.0
Quoted 53 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/arm.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)arch/arm64/kvm/arm.c +12
-7@@ -707,7 +707,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)struct kvm_s2_mmu *mmu;int *last_ran; -if (is_protected_kvm_enabled ()) +if (is_protected_kvm_enabled () ||kvm_is_realm (vcpu->kvm)) goto nommu;if (vcpu_has_nv (vcpu))@@ -751,12 +751,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vgic_load (vcpu);kvm_vcpu_load_debug (vcpu);kvm_vcpu_load_fgt (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);if (kvm_vcpu_should_clear_twe (vcpu)) vcpu->arch.hcr_el2 &= ~HCR_TWE ;@@ -778,6 +772,17 @@ 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 (kvm_arm_is_pvtime_enabled (&vcpu->arch)) +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu); +if (!cpumask_test_cpu (cpu, vcpu->kvm->arch.supported_cpus))vcpu_set_on_unsupported_cpu (vcpu);Signature · 1 line
2.43.0
PATCH v16 31/45 KVM: arm64: CCA: Validate register access for Realm VMs
In reply to Steven PriceThe RMM only allows setting the GPRS (x0-x30) and PC for a realm guest. Check this in kvm_arm_set_reg() so that the VMM can receive a suitable error return if other registers are written to.
The RMM makes similar restrictions for reading of the guest's registers (this is *confidential* compute after all), however we don't impose the restriction here. This allows the VMM to read (stale) values from the registers which might be useful to read back the initial values even if the RMM doesn't provide the latest version. For migration of a realm VM, a new interface will be needed so that the VMM can receive an (encrypted) blob of the VM's state.
---
Changes since v5:
* Upper GPRS can be set as part of a HOST_CALL return, so fix up the test to allow them.
--- arch/arm64/kvm/guest.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
arch/arm64/kvm/guest.c +41 -0
@@ -73,6 +73,25 @@ static u64 core_reg_offset_from_id(u64 id)return id & ~(KVM_REG_ARCH_MASK |KVM_REG_SIZE_MASK |KVM_REG_ARM_CORE ); } +static bool kvm_realm_validate_core_reg (u64 off) +{ +/* +* Note that GPRs can only sometimes be controlled by the VMM. +* For PSCI only X0-X6 are used, higher registers are ignored (restored +* from the REC). +* For HOST_CALL all of X0-X30 are copied to the RsiHostCall structure. +* For emulated MMIO X0 is always used. +* PC can only be set before the realm is activated. +*/ +switch (off) { + case KVM_REG_ARM_CORE_REG(regs.regs[0]) ... +KVM_REG_ARM_CORE_REG (regs.regs[30 ]): +case KVM_REG_ARM_CORE_REG (regs.pc): +return true; + } +return false; +} +static int core_reg_size_from_offset (const struct kvm_vcpu *vcpu,u64 off) {int size;
@@ -716,12 +735,34 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return kvm_arm_sys_reg_get_reg (vcpu, reg); } +/* +* The RMI ABI only enables setting some GPRs and PC. The selection of GPRs +* that are available depends on the Realm state and the reason for the last +* exit. All other registers are reset to architectural or otherwise defined +* reset values by the RMM, except for a few configuration fields that +* correspond to Realm parameters. +*/ +static bool validate_realm_set_reg (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg) +{ +if ((reg->id &KVM_REG_ARM_COPROC_MASK ) ==KVM_REG_ARM_CORE ) { +u64 off =core_reg_offset_from_id (reg->id); + +return kvm_realm_validate_core_reg (off); + } + +return false; +} +int kvm_arm_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {/* We currently use nothing arch-specific in upper 32 bits */ if ((reg->id & ~KVM_REG_SIZE_MASK ) >>32 !=KVM_REG_ARM64 >>32 )return -EINVAL ; +if (kvm_is_realm (vcpu->kvm) && !validate_realm_set_reg (vcpu, reg)) +return -EINVAL ; +switch (reg->id &KVM_REG_ARM_COPROC_MASK ) {case KVM_REG_ARM_CORE :return set_core_reg (vcpu, reg);case KVM_REG_ARM_FW :
Signature · 1 line
2.43.0
PATCH v16 32/45 KVM: arm64: CCA: Handle Realm PSCI requests
In reply to Steven PriceSome PSCI commands cause a REC exit and the host has to call RMI_PSCI_COMPLETE to provide the status value to return to the guest.
---
Changes since v15:
* Adapt to new exit handling.
Changes since v14:
* Dropped the support for providing the target REC for a PSCI call (as per v2.0-bet2 spec).
Changes since v13:
* The ioctl KVM_ARM_VCPU_RMI_PSCI_COMPLETE has gone. The RMI call is made automatically just before entering the REC again.
Changes since v12:
* Change return code for non-realms to -ENXIO to better represent that the ioctl is invalid for non-realms (checkpatch is insistent that "ENOSYS means 'invalid syscall nr' and nothing else").
Changes since v11:
* RMM->RMI renaming.
Changes since v6:
* Use vcpu_is_rec() rather than kvm_is_realm(vcpu->kvm).
* Minor renaming/formatting fixes.
--- arch/arm64/include/asm/kvm_rmi.h | 2 ++ arch/arm64/kvm/rmi.c | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+)
arch/arm64/include/asm/kvm_rmi.h +2 -0
@@ -123,6 +123,8 @@ int realm_map_ipa(struct kvm *kvm, phys_addr_t ipa,enum kvm_pgtable_prot prot, struct kvm_mmu_memory_cache *memcache); +int realm_psci_complete (struct kvm_vcpu *source,unsigned long status); +static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr) {
arch/arm64/kvm/rmi.c +38 -0
@@ -3,6 +3,7 @@* Copyright (C) 2023-2026 ARM Ltd. */ +#include <uapi/linux/psci.h> #include <linux/kvm_host.h> #include <asm/kvm_emulate.h>
@@ -120,6 +121,17 @@ static void free_rtt(phys_addr_t phys)kvm_account_pgtable_pages (phys_to_virt (phys),-1 ); } +int realm_psci_complete (struct kvm_vcpu *source,unsigned long status) +{ +long ret; + + ret =rmi_psci_complete (source->arch.rec.rec_phys, status); +if (ret) +return -ENXIO ; + +return 0 ; +} + static long realm_rtt_create(struct realm *realm, unsigned long addr, int level,
@@ -1145,6 +1157,30 @@ static int kvm_complete_ripas_change(struct kvm_vcpu *vcpu)return 1 ; } +static int kvm_rec_complete_psci (struct kvm_vcpu *vcpu) +{ +struct rec_run *run = vcpu->arch.rec.run; +unsigned long status =PSCI_RET_DENIED ; +unsigned long ret =vcpu_get_reg (vcpu,0 ); +int r; + +switch (run->exit.gprs[0 ]) { +case PSCI_0_2_FN64_CPU_ON : { +if (ret !=PSCI_RET_SUCCESS && + ret !=PSCI_RET_ALREADY_ON ) + status =PSCI_RET_DENIED ; +else + status =PSCI_RET_SUCCESS ; +break ; + } +default : +return 1 ; + } + + r =realm_psci_complete (vcpu, status); +return r ?:1 ; +} +int kvm_rec_handle_request (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec;
@@ -1163,6 +1199,8 @@ int kvm_rec_handle_request(struct kvm_vcpu *vcpu)vcpu_get_reg (vcpu, rt); }break ; +case RMI_EXIT_PSCI : +return kvm_rec_complete_psci (vcpu);case RMI_EXIT_RIPAS_CHANGE :return kvm_complete_ripas_change (vcpu);default :
Signature · 1 line
2.43.0
PATCH v16 33/45 KVM: arm64: WARN on injected undef exceptions
In reply to Steven PriceThe RMM doesn't allow injection of a undefined exception into a realm guest. Add a WARN to catch if this ever happens.
---
Changes since v15:
* Switch to KVM_BUG() to mark the VM as bugged as well.
Changes since v6:
* if (x) WARN(1, ...) makes no sense, just WARN(x, ...)!
--- arch/arm64/kvm/inject_fault.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kvm/inject_fault.c +1 -0
@@ -327,6 +327,7 @@ void kvm_inject_size_fault(struct kvm_vcpu *vcpu)*/ void kvm_inject_undefined (struct kvm_vcpu *vcpu) { +KVM_BUG (vcpu_is_rec (vcpu), vcpu->kvm,"Unexpected undefined exception injection to REC" );if (vcpu_el1_is_32bit (vcpu))inject_undef32 (vcpu);else
Signature · 1 line
2.43.0
PATCH v16 34/45 KVM: arm64: CCA: Allow userspace to inject aborts
In reply to Steven PriceFrom: Joey Gouly <[email protected]>
Extend KVM_SET_VCPU_EVENTS to support realms, where KVM cannot set the system registers, and the RMM must perform it on next REC entry.
--- Documentation/virt/kvm/api.rst | 2 ++ arch/arm64/kvm/guest.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+)
Documentation/virt/kvm/api.rst +2 -0
@@ -1317,6 +1317,8 @@ User space may need to inject several types of events to the guest. Set the pending SError exception state for this VCPU. It is not possible to 'cancel' an Serror that has been made pending. +User space cannot inject SErrors into Realms. + If the guest performed an access to I/O memory which could not be handled by userspace, for example because of missing instruction syndrome decode information or because there is no device mapped at the accessed IPA, then
arch/arm64/kvm/guest.c +24 -0
@@ -827,6 +827,30 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,u64 esr = events->exception.serror_esr;int ret =0 ; +if (vcpu_is_rec (vcpu)) { +/* Cannot inject SError into a Realm. */ +if (serror_pending) +return -EINVAL ; + +/* +* If a data abort is pending, set the flag and let the RMM +* inject an SEA when the REC is scheduled to be run. +*/ +if (ext_dabt_pending) { +/* +* Can only inject SEA into a Realm if the previous exit +* was due to a data abort of an Unprotected IPA. +*/ +if (!(vcpu->arch.rec.run->enter.flags &REC_ENTER_FLAG_EMULATED_MMIO )) +return -EINVAL ; + + vcpu->arch.rec.run->enter.flags &= ~REC_ENTER_FLAG_EMULATED_MMIO ; + vcpu->arch.rec.run->enter.flags |=REC_ENTER_FLAG_INJECT_SEA ; + } + +return 0 ; + } +/* * Immediately commit the pending SEA to the vCPU's architectural * state which is necessary since we do not return a pending SEA
Signature · 1 line
2.43.0
PATCH v16 35/45 KVM: arm64: CCA: Support RSI_HOST_CALL
In reply to Steven PriceFrom: Joey Gouly <[email protected]>
Realm VMs can talk to the hypervisor using the RSI_HOST_CALL SMC. The RMM forwards this to the host and KVM handles them as regular hypercalls.
---
Changes since v15:
* Adapt to new exit handling.
Changes since v7:
* Avoid turning a negative return from kvm_smccc_call_handler() into a error response to the guest. Instead propagate the error back to user space.
Changes since v4:
* Setting GPRS is now done by kvm_rec_enter() rather than rec_exit_host_call() (see previous patch - arm64: RME: Handle realm enter/exit). This fixes a bug where the registers set by user space were being ignored.
--- arch/arm64/kvm/rmi-exit.c | 1 + arch/arm64/kvm/rmi.c | 4 ++++ 2 files changed, 5 insertions(+)
arch/arm64/kvm/rmi-exit.c +1 -0
@@ -164,6 +164,7 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)case RMI_EXIT_SERROR :return ARM_EXCEPTION_EL1_SERROR ;case RMI_EXIT_PSCI : +case RMI_EXIT_HOST_CALL :rec_exit_hvc (vcpu);kvm_make_request (KVM_REQ_RMI , vcpu);return ARM_EXCEPTION_TRAP ;
arch/arm64/kvm/rmi.c +4 -0
@@ -1203,6 +1203,10 @@ int kvm_rec_handle_request(struct kvm_vcpu *vcpu)return kvm_rec_complete_psci (vcpu);case RMI_EXIT_RIPAS_CHANGE :return kvm_complete_ripas_change (vcpu); +case RMI_EXIT_HOST_CALL : +for (int i =0 ; i <REC_RUN_GPRS ; i++) + rec->run->enter.gprs[i] =vcpu_get_reg (vcpu, i); +break ;default :KVM_BUG (1 , vcpu->kvm,"Unhandled realm exit_reason" );return -ENXIO ;
Signature · 1 line
2.43.0
PATCH v16 36/45 KVM: arm64: CCA: Allow checking SVE on VM instance
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
Given we have different types of VMs supported, check the support for SVE for the given instance of the VM to accurately report the status.
---
Changes since v10:
* RME->RMI renaming.
* Adapt to move CAP check to kvm_realm_ext_allowed().
--- arch/arm64/include/asm/kvm_rmi.h | 2 ++ arch/arm64/kvm/arm.c | 2 ++ arch/arm64/kvm/rmi.c | 5 +++++ 3 files changed, 9 insertions(+)
arch/arm64/include/asm/kvm_rmi.h +2 -0
@@ -98,6 +98,8 @@ struct realm_rec {void kvm_init_rmi (void );u32 kvm_rmm_ipa_limit (void ); +bool kvm_rmi_supports_sve (void ); +int kvm_init_realm (struct kvm *kvm);int kvm_activate_realm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm);
arch/arm64/kvm/arm.c +2 -0
@@ -151,6 +151,8 @@ static bool kvm_realm_ext_allowed(long ext)case KVM_CAP_ARM_RMI :case KVM_CAP_SYNC_MMU :return true; +case KVM_CAP_ARM_SVE : +return kvm_rmi_supports_sve (); }return false; }
arch/arm64/kvm/rmi.c +5 -0
@@ -26,6 +26,11 @@ static bool rmi_has_feature(int reg, unsigned long feature)return !!u64_get_bits (rmi_feat_reg (reg), feature); } +bool kvm_rmi_supports_sve (void ) +{ +return rmi_has_feature (0 ,RMI_FEATURE_REGISTER_0_SVE ); +} +u32 kvm_rmm_ipa_limit (void ) {return u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_S2SZ );
Signature · 1 line
2.43.0
PATCH v16 37/45 KVM: arm64: CCA: Prevent Device mappings for realms
In reply to Steven PricePhysical device assignment is not yet supported. RMM v2.0 does add the relevant APIs, but device assignment is a big topic so will be handled in a future patch series. For now prevent device mappings when the guest is a realm.
---
Changes from v6:
* Fix the check in user_mem_abort() to prevent all pages that are not guest_memfd() from being mapped into the protected half of the IPA.
Changes from v5:
* Also prevent accesses in user_mem_abort()
--- arch/arm64/kvm/mmu.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kvm/mmu.c +4 -0
@@ -1261,6 +1261,10 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,if (is_protected_kvm_enabled ())return -EPERM ; +/* We don't support mapping special pages into a Realm */ +if (kvm_is_realm (kvm)) +return -EPERM ; + size +=offset_in_page (guest_ipa); guest_ipa &=PAGE_MASK ;
Signature · 1 line
2.43.0
PATCH v16 38/45 KVM: arm64: CCA: Propagate breakpoint and watchpoint counts to userspace
In reply to Steven PriceFrom: Jean-Philippe Brucker <[email protected]>
The RMM describes the maximum number of BPs/WPs available to the guest in the Feature Register 0. Propagate those numbers into ID_AA64DFR0_EL1, which is visible to userspace. A VMM needs this information in order to set up realm parameters.
--- arch/arm64/include/asm/kvm_rmi.h | 2 ++ arch/arm64/kvm/rmi.c | 19 +++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 3 +++ 3 files changed, 24 insertions(+)
arch/arm64/include/asm/kvm_rmi.h +2 -0
@@ -98,6 +98,8 @@ struct realm_rec {void kvm_init_rmi (void );u32 kvm_rmm_ipa_limit (void ); +u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val); +bool kvm_rmi_supports_sve (void );int kvm_init_realm (struct kvm *kvm);
arch/arm64/kvm/rmi.c +19 -0
@@ -36,6 +36,25 @@ u32 kvm_rmm_ipa_limit(void)return u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_S2SZ ); } +u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val) +{ +u32 bps =u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_NUM_BPS ); +u32 wps =u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_NUM_WPS ); +u32 ctx_cmps; + +/* Ensure CTX_CMPs is still valid */ + ctx_cmps =FIELD_GET (ID_AA64DFR0_EL1_CTX_CMPs, val); + ctx_cmps =min (bps, ctx_cmps); + + val &= ~(ID_AA64DFR0_EL1_BRPs_MASK | ID_AA64DFR0_EL1_WRPs_MASK | + ID_AA64DFR0_EL1_CTX_CMPs); + val |=FIELD_PREP (ID_AA64DFR0_EL1_BRPs_MASK, bps) | +FIELD_PREP (ID_AA64DFR0_EL1_WRPs_MASK, wps) | +FIELD_PREP (ID_AA64DFR0_EL1_CTX_CMPs, ctx_cmps); + +return val; +} +static int get_start_level (struct realm *realm) {return 4 -stage2_pgtable_levels (realm->ia_bits);
arch/arm64/kvm/sys_regs.c +3 -0
@@ -2142,6 +2142,9 @@ static u64 sanitise_id_aa64dfr0_el1(const struct kvm_vcpu *vcpu, u64 val)/* Hide BRBE from guests */ val &= ~ID_AA64DFR0_EL1_BRBE_MASK ; +if (vcpu_is_rec (vcpu)) +return kvm_realm_reset_id_aa64dfr0_el1 (vcpu, val); +return val; }
Signature · 1 line
2.43.0
PATCH v16 39/45 KVM: arm64: CCA: Set breakpoint parameters through SET_ONE_REG
In reply to Steven PriceFrom: Jean-Philippe Brucker <[email protected]>
Allow userspace to configure the number of breakpoints and watchpoints of a Realm VM through KVM_SET_ONE_REG ID_AA64DFR0_EL1.
The KVM sys_reg handler checks the user value against the maximum value given by RMM (arm64_check_features() gets it from the read_sanitised_id_aa64dfr0_el1() reset handler).
Userspace discovers that it can write these fields by issuing a KVM_ARM_GET_REG_WRITABLE_MASKS ioctl.
--- arch/arm64/kvm/guest.c | 7 +++++++ arch/arm64/kvm/rmi.c | 3 +++ arch/arm64/kvm/sys_regs.c | 17 +++++++++++------ 3 files changed, 21 insertions(+), 6 deletions(-)
arch/arm64/kvm/guest.c +7 -0
@@ -735,6 +735,8 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return kvm_arm_sys_reg_get_reg (vcpu, reg); } +#define KVM_REG_ARM_ID_AA64DFR0_EL1 ARM64_SYS_REG(3, 0, 0, 5, 0) +/* * The RMI ABI only enables setting some GPRs and PC. The selection of GPRs * that are available depends on the Realm state and the reason for the last
@@ -749,6 +751,11 @@ static bool validate_realm_set_reg(struct kvm_vcpu *vcpu,u64 off =core_reg_offset_from_id (reg->id);return kvm_realm_validate_core_reg (off); + }else { +switch (reg->id) { +case KVM_REG_ARM_ID_AA64DFR0_EL1 : +return true; + } }return false;
arch/arm64/kvm/rmi.c +3 -0
@@ -505,6 +505,7 @@ static int realm_create_rd(struct kvm *kvm)void *rd =NULL ;phys_addr_t rd_phys, params_phys, top_delegated;size_t pgd_size =kvm_pgtable_stage2_pgd_size (kvm->arch.mmu.vtcr); +u64 dfr0 =kvm_read_vm_id_reg (kvm,SYS_ID_AA64DFR0_EL1 );long rmi_ret;int r;
@@ -537,6 +538,8 @@ static int realm_create_rd(struct kvm *kvm) params->rtt_level_start =get_start_level (realm); params->rtt_num_start = pgd_size /PAGE_SIZE ; params->rtt_base = kvm->arch.mmu.pgd_phys; + params->num_bps =SYS_FIELD_GET (ID_AA64DFR0_EL1 , BRPs, dfr0); + params->num_wps =SYS_FIELD_GET (ID_AA64DFR0_EL1 , WRPs, dfr0); params->hash_algo =RMI_HASH_SHA_256 ;if (kvm->arch.arm_pmu) {
arch/arm64/kvm/sys_regs.c +11 -6
@@ -2174,6 +2174,9 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, {u8 debugver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , DebugVer, val);u8 pmuver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , PMUVer, val); +u8 bps =SYS_FIELD_GET (ID_AA64DFR0_EL1 , BRPs, val); +u8 wps =SYS_FIELD_GET (ID_AA64DFR0_EL1 , WRPs, val); +u8 ctx_cmps =SYS_FIELD_GET (ID_AA64DFR0_EL1 , CTX_CMPs, val);/* * Prior to commit 3d0dba5764b9 ("KVM: arm64: PMU: Move the
@@ -2193,10 +2196,11 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;/* -* ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a -* nonzero minimum safe value. +* ID_AA64DFR0_EL1.DebugVer, BRPs and WRPs all have to be greater than +* zero. CTX_CMPs is never greater than BRPs. */ -if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP) +if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP || !bps || !wps || + ctx_cmps > bps)return -EINVAL ;if (ignore_feat_doublelock (vcpu, val)) {
@@ -2429,10 +2433,11 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,mutex_lock (&vcpu->kvm->arch.config_lock);/* -* Once the VM has started the ID registers are immutable. Reject any -* write that does not match the final register value. +* Once the VM has started or the Realm descriptor is created, the ID +* registers are immutable. Reject any write that does not match the +* final register value. */ -if (kvm_vm_has_ran_once (vcpu->kvm)) { +if (kvm_vm_has_ran_once (vcpu->kvm) ||kvm_realm_is_created (vcpu->kvm)) {if (val !=read_id_reg (vcpu, rd)) ret = -EBUSY ;else
Signature · 1 line
2.43.0
PATCH v16 40/45 KVM: arm64: CCA: Propagate max SVE vector length from the RMM
In reply to Steven PriceFrom: Jean-Philippe Brucker <[email protected]>
RMM provides the maximum vector length it supports for a guest in its feature register. Make it visible to the rest of KVM and to userspace via KVM_REG_ARM64_SVE_VLS.
--- arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/include/asm/kvm_rmi.h | 1 + arch/arm64/kvm/guest.c | 2 +- arch/arm64/kvm/reset.c | 12 ++++++++++-- arch/arm64/kvm/rmi.c | 6 ++++++ 5 files changed, 19 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -79,9 +79,9 @@ enum kvm_mode kvm_get_mode(void);static inline enum kvm_mode kvm_get_mode (void ) {return KVM_MODE_NONE ; };#endif -extern unsigned int __ro_after_init kvm_sve_max_vl;extern unsigned int __ro_after_init kvm_host_sve_max_vl;int __init kvm_arm_init_sve (void ); +unsigned int kvm_sve_get_max_vl (struct kvm *kvm); u32 __attribute_const__ kvm_target_cpu(void);void kvm_reset_vcpu (struct kvm_vcpu *vcpu);
arch/arm64/include/asm/kvm_rmi.h +1 -0
@@ -97,6 +97,7 @@ struct realm_rec {void kvm_init_rmi (void );u32 kvm_rmm_ipa_limit (void ); +unsigned int kvm_realm_sve_max_vl (void );u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val);
arch/arm64/kvm/guest.c +1 -1
@@ -375,7 +375,7 @@ static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if (vq_present (vqs, vq)) max_vq = vq; -if (max_vq >sve_vq_from_vl (kvm_sve_max_vl)) +if (max_vq >sve_vq_from_vl (kvm_sve_get_max_vl (vcpu->kvm)))return -EINVAL ;/*
arch/arm64/kvm/reset.c +10 -2
@@ -46,7 +46,7 @@ unsigned int __ro_after_init kvm_host_sve_max_vl;#define VCPU_RESET_PSTATE_SVC (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \ PSR_AA32_I_BIT | PSR_AA32_F_BIT) -unsigned int __ro_after_init kvm_sve_max_vl; +static unsigned int __ro_after_init kvm_sve_max_vl;int __init kvm_arm_init_sve (void ) {
@@ -76,9 +76,17 @@ int __init kvm_arm_init_sve(void)return 0 ; } +unsigned int kvm_sve_get_max_vl (struct kvm *kvm) +{ +if (kvm_is_realm (kvm)) +return kvm_realm_sve_max_vl (); +else +return kvm_sve_max_vl; +} +static void kvm_vcpu_enable_sve (struct kvm_vcpu *vcpu) { - vcpu->arch.sve_max_vl = kvm_sve_max_vl; + vcpu->arch.sve_max_vl =kvm_sve_get_max_vl (vcpu->kvm);/* * Userspace can still customize the vector lengths by writing
arch/arm64/kvm/rmi.c +6 -0
@@ -36,6 +36,12 @@ u32 kvm_rmm_ipa_limit(void)return u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_S2SZ ); } +unsigned int kvm_realm_sve_max_vl (void ) +{ +return sve_vl_from_vq (u64_get_bits (rmi_feat_reg (0 ), +RMI_FEATURE_REGISTER_0_SVE_VL ) +1 ); +} +u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val) {u32 bps =u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_NUM_BPS );
Signature · 1 line
2.43.0
PATCH v16 41/45 KVM: arm64: CCA: Configure max SVE vector length for a Realm
In reply to Steven PriceFrom: Jean-Philippe Brucker <[email protected]>
Obtain the max vector length configured by userspace on the vCPUs, and write it into the Realm parameters. By default the vCPU is configured with the max vector length reported by RMM, and userspace can reduce it with a write to KVM_REG_ARM64_SVE_VLS.
---
Changes since v6:
* Rename max_vl/realm_max_vl to vl/last_vl - there is nothing "maximum" about them, we're just checking that all realms have the same vector length
--- arch/arm64/kvm/guest.c | 3 ++- arch/arm64/kvm/rmi.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-)
arch/arm64/kvm/guest.c +2 -1
@@ -361,7 +361,7 @@ static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if (!vcpu_has_sve (vcpu))return -ENOENT ; -if (kvm_arm_vcpu_sve_finalized (vcpu)) +if (kvm_arm_vcpu_sve_finalized (vcpu) ||kvm_realm_is_created (vcpu->kvm))return -EPERM ;/* too late! */ if (WARN_ON (vcpu->arch.sve_state))
@@ -754,6 +754,7 @@ static bool validate_realm_set_reg(struct kvm_vcpu *vcpu, }else {switch (reg->id) {case KVM_REG_ARM_ID_AA64DFR0_EL1 : +case KVM_REG_ARM64_SVE_VLS :return true; } }
arch/arm64/kvm/rmi.c +37 -0
@@ -504,6 +504,39 @@ static void realm_unmap_shared_range(struct kvm *kvm, start, end); } +static int realm_init_sve_param (struct kvm *kvm,struct realm_params *params) +{ +unsigned long i; +struct kvm_vcpu *vcpu; +int vl, last_vl =-1 ; + +if (!kvm_has_sve (kvm)) +return 0 ; + +/* +* Get the preferred SVE configuration, set by userspace with the +* KVM_ARM_VCPU_SVE feature and KVM_REG_ARM64_SVE_VLS pseudo-register. +*/ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (!kvm_arm_vcpu_sve_finalized (vcpu)) +return -EINVAL ; + + vl = vcpu->arch.sve_max_vl; + +/* We need all vCPUs to have the same SVE config */ +if (last_vl >=0 && last_vl != vl) +return -EINVAL ; + + last_vl = vl; + } + +if (last_vl >0 ) { + params->sve_vl =sve_vq_from_vl (last_vl) -1 ; + params->flags0 |=RMI_REALM_PARAM_FLAG_SVE ; + } +return 0 ; +} +static int realm_create_rd (struct kvm *kvm) {struct realm *realm = &kvm->arch.realm;
@@ -556,6 +589,10 @@ static int realm_create_rd(struct kvm *kvm)if (kvm_lpa2_is_enabled ()) params->flags0 |=RMI_REALM_PARAM_FLAG_LPA2 ; + r =realm_init_sve_param (kvm, params); +if (r) + goto out_undelegate_tables; + params_phys =virt_to_phys (params); rmi_ret =rmi_realm_create (rd_phys, params_phys, realm->sro);
Signature · 1 line
2.43.0
PATCH v16 42/45 KVM: arm64: CCA: Provide register list for unfinalized RECs
In reply to Steven PriceFrom: Jean-Philippe Brucker <[email protected]>
KVM_GET_REG_LIST should not be called before SVE is finalized. The ioctl handler currently returns -EPERM in this case. But because it uses kvm_arm_vcpu_is_finalized(), it now also rejects the call for unfinalized REC even though finalizing the REC can only be done late, after Realm descriptor creation.
Move the check to copy_sve_reg_indices(). One adverse side effect of this change is that a KVM_GET_REG_LIST call that only probes for the array size will now succeed even if SVE is not finalized, but that seems harmless since the following KVM_GET_REG_LIST with the full array will fail.
--- arch/arm64/kvm/arm.c | 4 ---- arch/arm64/kvm/guest.c | 10 +++++----- 2 files changed, 5 insertions(+), 9 deletions(-)
arch/arm64/kvm/arm.c +0 -4
@@ -1967,10 +1967,6 @@ long kvm_arch_vcpu_ioctl(struct file *filp,if (unlikely (!kvm_vcpu_initialized (vcpu)))break ; - r = -EPERM ; -if (!kvm_arm_vcpu_is_finalized (vcpu)) -break ; - r = -EFAULT ;if (copy_from_user (®_list, user_list,sizeof (reg_list)))break ;
arch/arm64/kvm/guest.c +5 -5
@@ -617,8 +617,8 @@ static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)if (!vcpu_has_sve (vcpu))return 0 ; -/* Policed by KVM_GET_REG_LIST: */ -WARN_ON (!kvm_arm_vcpu_sve_finalized (vcpu)); +if (!kvm_arm_vcpu_sve_finalized (vcpu)) +return 1 ;/* KVM_REG_ARM64_SVE_VLS */ return slices * (SVE_NUM_PREGS +SVE_NUM_ZREGS +1 /* FFR */ ) +1 ;/* KVM_REG_ARM64_SVE_VLS */
@@ -635,9 +635,6 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,if (!vcpu_has_sve (vcpu))return 0 ; -/* Policed by KVM_GET_REG_LIST: */ -WARN_ON (!kvm_arm_vcpu_sve_finalized (vcpu)); -/* * Enumerate this first, so that userspace can save/restore in * the order reported by KVM_GET_REG_LIST:
@@ -647,6 +644,9 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,return -EFAULT ; ++num_regs; +if (!kvm_arm_vcpu_sve_finalized (vcpu)) +return num_regs; +for (i =0 ; i < slices; i++) {for (n =0 ; n <SVE_NUM_ZREGS ; n++) { reg =KVM_REG_ARM64_SVE_ZREG (n, i);
Signature · 1 line
2.43.0
PATCH v16 43/45 KVM: arm64: CCA: Provide an accurate register list
In reply to Steven PriceFrom: Jean-Philippe Brucker <[email protected]>
Userspace can set a few registers with KVM_SET_ONE_REG (9 GP registers at runtime, and 3 system registers during initialization). Update the register list returned by KVM_GET_REG_LIST.
---
Changes since v11:
* Reworked due to upstream changes.
Changes since v8:
* Minor type changes following review.
Changes since v7:
* Reworked on upstream changes.
--- arch/arm64/kvm/guest.c | 6 ++++++ arch/arm64/kvm/hypercalls.c | 4 ++-- arch/arm64/kvm/sys_regs.c | 27 +++++++++++++++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-)
arch/arm64/kvm/guest.c +6 -0
@@ -620,6 +620,9 @@ static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)if (!kvm_arm_vcpu_sve_finalized (vcpu))return 1 ;/* KVM_REG_ARM64_SVE_VLS */ +if (kvm_is_realm (vcpu->kvm)) +return 1 ;/* KVM_REG_ARM64_SVE_VLS */ +return slices * (SVE_NUM_PREGS +SVE_NUM_ZREGS +1 /* FFR */ ) +1 ;/* KVM_REG_ARM64_SVE_VLS */ }
@@ -647,6 +650,9 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,if (!kvm_arm_vcpu_sve_finalized (vcpu))return num_regs; +if (kvm_is_realm (vcpu->kvm)) +return num_regs; +for (i =0 ; i < slices; i++) {for (n =0 ; n <SVE_NUM_ZREGS ; n++) { reg =KVM_REG_ARM64_SVE_ZREG (n, i);
arch/arm64/kvm/hypercalls.c +2 -2
@@ -414,14 +414,14 @@ void kvm_arm_teardown_hypercalls(struct kvm *kvm)int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) { -return ARRAY_SIZE (kvm_arm_fw_reg_ids); +return kvm_is_realm (vcpu->kvm) ?0 :ARRAY_SIZE (kvm_arm_fw_reg_ids); }int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) {int i; -for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_ids); i++) { +for (i =0 ; i <kvm_arm_get_fw_num_regs (vcpu); i++) {if (put_user (kvm_arm_fw_reg_ids[i], uindices++))return -EFAULT ; }
arch/arm64/kvm/sys_regs.c +21 -6
@@ -5540,18 +5540,18 @@ int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); } -static unsigned int num_demux_regs (void ) +static inline unsigned int num_demux_regs (struct kvm_vcpu *vcpu) { -return CSSELR_MAX ; +return kvm_is_realm (vcpu->kvm) ?0 :CSSELR_MAX ; } -static int write_demux_regids (u64 __user *uindices) +static int write_demux_regids (struct kvm_vcpu *vcpu,u64 __user *uindices) {u64 val =KVM_REG_ARM64 |KVM_REG_SIZE_U32 |KVM_REG_ARM_DEMUX ;unsigned int i; val |=KVM_REG_ARM_DEMUX_ID_CCSIDR ; -for (i =0 ; i <CSSELR_MAX ; i++) { +for (i =0 ; i <num_demux_regs (vcpu); i++) {if (put_user (val | i, uindices))return -EFAULT ; uindices++;
@@ -5595,11 +5595,26 @@ static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)return true; } +static inline bool kvm_realm_sys_reg_hidden_user (const struct kvm_vcpu *vcpu, +u64 reg) +{ +switch (reg) { +case SYS_ID_AA64DFR0_EL1 : +case SYS_PMCR_EL0 : +return false; + } +return true; +} +static int walk_one_sys_reg (const struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd,u64 __user **uind,unsigned int *total) { +if (vcpu_is_rec (vcpu) && +kvm_realm_sys_reg_hidden_user (vcpu,reg_to_encoding (rd))) +return 0 ; +/* * Ignore registers we trap but don't save, * and for which no custom user accessor is provided.
@@ -5637,7 +5652,7 @@ static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)unsigned long kvm_arm_num_sys_reg_descs (struct kvm_vcpu *vcpu) { -return num_demux_regs () +return num_demux_regs (vcpu) +walk_sys_regs (vcpu, (u64 __user *)NULL ); }
@@ -5650,7 +5665,7 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)return err; uindices += err; -return write_demux_regids (uindices); +return write_demux_regids (vcpu, uindices); }#define KVM_ARM_FEATURE_ID_RANGE_INDEX (r) \
Signature · 1 line
2.43.0
PATCH v16 44/45 KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
In reply to Steven PriceKVM advertises realm support when the RMM is available, and allows userspace to create a VM with KVM_VM_TYPE_ARM_REALM on that basis.
On CPUs that lack ICH_HCR_EL2.TDIR, KVM uses ICH_HCR_EL2.TC for normal guests so that ICC_DIR_EL1 is still trapped via the common GICv3 CPU interface trap. Realms cannot rely on the normal hyp-side trap handling for that fallback, so advertising RMI support on such systems lets userspace create a realm that cannot safely run.
Require the finalized ARM64_HAS_ICH_HCR_EL2_TDIR capability when reporting KVM_CAP_ARM_RMI and when accepting KVM_VM_TYPE_ARM_REALM. This leaves normal VM creation unchanged on systems that need the TC workaround.
---
New patch for v16
--- arch/arm64/kvm/arm.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
arch/arm64/kvm/arm.c +8 -2
@@ -125,6 +125,12 @@ static bool vgic_present, kvm_arm_initialised; static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized); +static bool kvm_arm_rmi_supported (void ) +{ +return static_key_enabled (&kvm_rmi_is_available) && +cpus_have_final_cap (ARM64_HAS_ICH_HCR_EL2_TDIR ); +} +bool is_kvm_arm_initialised (void ) {return kvm_arm_initialised;
@@ -256,7 +262,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)return -EINVAL ;if (type &KVM_VM_TYPE_ARM_REALM ) { -if (!static_branch_unlikely (&kvm_rmi_is_available)) +if (!kvm_arm_rmi_supported ())return -EINVAL ;kvm_set_realm_state (kvm,REALM_STATE_NONE ); kvm->arch.is_realm = true;
@@ -537,7 +543,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =kvm_supports_cacheable_pfnmap ();break ;case KVM_CAP_ARM_RMI : - r =static_key_enabled (&kvm_rmi_is_available); + r =kvm_arm_rmi_supported ();break ;default :
Signature · 1 line
2.43.0
Re: [PATCH v16 44/45] KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
Quoted 9 lines · Steven Price
KVM advertises realm support when the RMM is available, and allows userspace to create a VM with
KVM_VM_TYPE_ARM_REALMon that basis.On CPUs that lack ICH_HCR_EL2.TDIR, KVM uses ICH_HCR_EL2.TC for normal guests so that
ICC_DIR_EL1is still trapped via the common GICv3 CPU interface trap. Realms cannot rely on the normal hyp-side trap handling for that fallback, so advertising RMI support on such systems lets userspace create a realm that cannot safely run.
Quoted 4 lines
Require the finalized
ARM64_HAS_ICH_HCR_EL2_TDIRcapability when reportingKVM_CAP_ARM_RMIand when acceptingKVM_VM_TYPE_ARM_REALM. This leaves normal VM creation unchanged on systems that need the TC workaround.
Hi Steven,
Thanks for your work on upstreaming CCA.
In the v15 discussion [0], you asked whether the system I was testing was a "hacked up test system" or closer to "production hardware", and I said I would share more when the time came. I can now say that this is not a hacked-up test system. At Fujitsu, we have real hardware (FUJITSU-MONAKA) which implements CCA (FEAT_RME) but does not implement FEAT_GICv3_TDIR. The hardware details are as follows:
- GICv4.2 compliant implementation
- Supports FEAT_GICv3, FEAT_GICv3p1, FEAT_GICv4, FEAT_GICv4p1, and FEAT_GICv3_NMI
- Does not support FEAT_GICv3_LEGACY (deprecated)
- Does not support FEAT_GICv3_TDIR (ICH_VTR_EL2.TDS == 0)
For reference, compared with Arm Neoverse V3, the virtual GIC configuration is largely equivalent. The only missing non-deprecated architectural feature is FEAT_GICv3_TDIR.
The issue I see is that the CCA KVM code currently does not support a configuration (non-TDIR/common-trap) that normal KVM already supports. For normal guests, KVM handles systems without TDIR by using ICH_HCR_EL2.TC and the existing GICv3 CPU interface emulation path. However, Realm guests currently fail because the CCA path bypasses that existing emulation path, as Marc also pointed out in [1].
Also, this is not limited to systems that actually lack TDIR. The same failure can be reproduced on a TDIR-capable system by booting with:
kvm-arm.vgic_v3_common_trap=1
So it seems that the current CCA KVM implementation does not yet cover a configuration that normal KVM already supports today, rather than this being a limitation of the RMM specification or the underlying hardware.
I've included a patch below which reuses the existing GICv3 early emulation path for Realm sysreg exits. This patch does not add any new vGIC emulation code, and leaves the existing vGIC emulation code unchanged. So I believe this is in line with Marc's request in [1]. With this patch, Realm guests can run when the common CPU interface trap path is enabled.
I tested the exact patch both on our real silicon and on QEMU, and confirmed that all Realm-related tests in kvm-unit-tests-cca passed.
I'm not attached to this exact implementation, and I'm happy if the solution is reworked to better fit into the next revision.
Given that this configuration can be supported by reusing the existing KVM emulation infrastructure, I think it would be reasonable for CCA to support the non-TDIR/common-trap configuration rather than requiring ICH_HCR_EL2.TDIR unconditionally for Realm support.
Supporting this configuration would also allow us to validate the upstream CCA KVM implementation on real silicon using upstream code paths, and contribute additional real-hardware testing coverage as the implementation evolves.
I'd be very interested in hearing your thoughts.
Thanks,
Kohei
[0] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/
Below is the patch I tested, and it should be applied on top of this series.
----8<----
From 11ce6b7e5ee2f21a71f657e9f45f58871d011286 Mon Sep 17 00:00:00 2001
From: Kohei Enju <[email protected]>
Date: Tue, 4 Aug 2026 11:45:53 +0900
Subject: [PATCH] KVM: arm64: CCA: Reuse early vGIC sysreg emulation for Realm
exits
Realm vGIC CPU interface sysreg exits currently bypass KVM's early vGIC emulation and reach the generic sysreg descriptors. This breaks Realm guests when common-trap trapping is enabled, including on systems without ICH_HCR_EL2.TDIR.
Prepare REC exits in the state expected by the existing vGIC dispatcher and invoke it before KVM synchronizes the vGIC hardware state. Complete handled accesses through the REC run structure and re-enter the REC.
This reuses the existing vGIC emulation unchanged and allows Realm guests to run with the non-TDIR/common-trap configuration.
--- arch/arm64/kvm/rmi-exit.c | 21 +------ arch/arm64/kvm/rmi.c | 122 ++++++++++++++++++++++++++++++++++---- 2 files changed, 112 insertions(+), 31 deletions(-)
arch/arm64/kvm/rmi-exit.c +3 -18
@@ -22,21 +22,14 @@ static int rec_exit_fatal(struct kvm_vcpu *vcpu, const char *reason,static void rec_exit_sync (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec; -u64 esr = rec->run->exit.esr; +u64 esr =kvm_vcpu_get_esr (vcpu);u8 ec =ESR_ELx_EC (esr);switch (ec) { -case ESR_ELx_EC_SYS64: { -int rt =ESR_ELx_SYS64_ISS_RT (esr); -bool is_write = (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == - ESR_ELx_SYS64_ISS_DIR_WRITE; - -if (is_write && rt <REC_RUN_GPRS ) -vcpu_set_reg (vcpu, rt, rec->run->exit.gprs[rt]); -else if (!is_write) +case ESR_ELx_EC_SYS64: +if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ)kvm_make_request (KVM_REQ_RMI , vcpu);break ; - }case ESR_ELx_EC_DABT_LOW:/* * The RMM reports the value of an MMIO write in gprs[0],
@@ -141,14 +134,6 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)return rec_exit_fatal (vcpu,"Unexpected REC_ENTER status" , rec_run_ret); - vcpu->arch.fault.esr_el2 = rec->run->exit.esr; - vcpu->arch.fault.far_el2 = rec->run->exit.far; -/* HPFAR_EL2 is only valid for RMI_EXIT_SYNC */ - vcpu->arch.fault.hpfar_el2 =0 ; - -/* 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 :/*
arch/arm64/kvm/rmi.c +109 -13
146 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 46 lines
Signed-off-by Steven Price <[email protected]>---
New patch for v16--- arch/arm64/kvm/arm.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)arch/arm64/kvm/arm.c +8
-2@@ -125,6 +125,12 @@ static bool vgic_present, kvm_arm_initialised; static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized); +static bool kvm_arm_rmi_supported (void ) +{ +return static_key_enabled (&kvm_rmi_is_available) && +cpus_have_final_cap (ARM64_HAS_ICH_HCR_EL2_TDIR ); +} +bool is_kvm_arm_initialised (void ) {return kvm_arm_initialised;@@ -256,7 +262,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)return -EINVAL ;if (type &KVM_VM_TYPE_ARM_REALM ) { -if (!static_branch_unlikely (&kvm_rmi_is_available)) +if (!kvm_arm_rmi_supported ())return -EINVAL ;kvm_set_realm_state (kvm,REALM_STATE_NONE ); kvm->arch.is_realm = true;@@ -537,7 +543,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =kvm_supports_cacheable_pfnmap ();break ;case KVM_CAP_ARM_RMI : - r =static_key_enabled (&kvm_rmi_is_available); + r =kvm_arm_rmi_supported ();break ;default :Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v16 44/45] KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
Quoted 27 lines · Kohei Enju
Quoted 9 lines · Steven Price
KVM advertises realm support when the RMM is available, and allows userspace to create a VM with
KVM_VM_TYPE_ARM_REALMon that basis.On CPUs that lack ICH_HCR_EL2.TDIR, KVM uses ICH_HCR_EL2.TC for normal guests so that
ICC_DIR_EL1is still trapped via the common GICv3 CPU interface trap. Realms cannot rely on the normal hyp-side trap handling for that fallback, so advertising RMI support on such systems lets userspace create a realm that cannot safely run.Quoted 4 lines
Require the finalized
ARM64_HAS_ICH_HCR_EL2_TDIRcapability when reportingKVM_CAP_ARM_RMIand when acceptingKVM_VM_TYPE_ARM_REALM. This leaves normal VM creation unchanged on systems that need the TC workaround.Hi Steven,
Thanks for your work on upstreaming CCA.
In the v15 discussion [0], you asked whether the system I was testing was a "hacked up test system" or closer to "production hardware", and I said I would share more when the time came. I can now say that this is not a hacked-up test system. At Fujitsu, we have real hardware (FUJITSU-MONAKA) which implements CCA (
FEAT_RME) but does not implementFEAT_GICv3_TDIR. The hardware details are as follows:
Quoted 8 lines
- GICv4.2 compliant implementation
- SupportsFEAT_GICv3,FEAT_GICv3p1,FEAT_GICv4,FEAT_GICv4p1, andFEAT_GICv3_NMI
- Does not supportFEAT_GICv3_LEGACY(deprecated)
- Does not supportFEAT_GICv3_TDIR(ICH_VTR_EL2.TDS == 0)For reference, compared with Arm Neoverse V3, the virtual GIC configuration is largely equivalent. The only missing non-deprecated architectural feature is
FEAT_GICv3_TDIR.
A *very* significant difference. Given the cost of trapping between R-EL1 and NS-EL2, something as simple as accesses to ICV_PMR_EL1 result in an extremely expensive trap.
Quoted 7 lines
The issue I see is that the CCA KVM code currently does not support a configuration (non-TDIR/common-trap) that normal KVM already supports. For normal guests, KVM handles systems without TDIR by using ICH_HCR_EL2.TC and the existing GICv3 CPU interface emulation path. However, Realm guests currently fail because the CCA path bypasses that existing emulation path, as Marc also pointed out in [1].
Plugging CCA in the emulation code will solve the *functional* aspect. The performance aspect is still there, unfortunately, and there isn't much KVM can do about that.
Quoted 4 lines
Also, this is not limited to systems that actually lack TDIR. The same failure can be reproduced on a TDIR-capable system by booting with:
kvm-arm.vgic_v3_common_trap=1
This is a *debug* option for broken hardware. ThunderX, for example. You really are in good company when this bit is set.
Quoted 5 lines
So it seems that the current CCA KVM implementation does not yet cover a configuration that normal KVM already supports today, rather than this being a limitation of the RMM specification or the underlying hardware.
Quoted 8 lines
I've included a patch below which reuses the existing GICv3 early emulation path for Realm sysreg exits. This patch does not add any new vGIC emulation code, and leaves the existing vGIC emulation code unchanged. So I believe this is in line with Marc's request in [1]. With this patch, Realm guests can run when the common CPU interface trap path is enabled.
I tested the exact patch both on our real silicon and on QEMU, and confirmed that all Realm-related tests in kvm-unit-tests-cca passed.
KUTs are unfortunately not something that people run in production. I wonder why...
Please run a Linux guest compiled with CONFIG_ARM64_PSEUDO_NMI=y and irqchip.gicv3_pseudo_nmi=1 on the command line. Run any significant workload (hackbench, for example), and report the overhead. This will give you the expected impact introduced by the lack of TDIR.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v16 44/45] KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
Quoted 76 lines · Marc Zyngier
Quoted 27 lines · Kohei Enju
Quoted 9 lines · Steven Price
KVM advertises realm support when the RMM is available, and allows userspace to create a VM with
KVM_VM_TYPE_ARM_REALMon that basis.On CPUs that lack ICH_HCR_EL2.TDIR, KVM uses ICH_HCR_EL2.TC for normal guests so that
ICC_DIR_EL1is still trapped via the common GICv3 CPU interface trap. Realms cannot rely on the normal hyp-side trap handling for that fallback, so advertising RMI support on such systems lets userspace create a realm that cannot safely run.Quoted 4 lines
Require the finalized
ARM64_HAS_ICH_HCR_EL2_TDIRcapability when reportingKVM_CAP_ARM_RMIand when acceptingKVM_VM_TYPE_ARM_REALM. This leaves normal VM creation unchanged on systems that need the TC workaround.Hi Steven,
Thanks for your work on upstreaming CCA.
In the v15 discussion [0], you asked whether the system I was testing was a "hacked up test system" or closer to "production hardware", and I said I would share more when the time came. I can now say that this is not a hacked-up test system. At Fujitsu, we have real hardware (FUJITSU-MONAKA) which implements CCA (
FEAT_RME) but does not implementFEAT_GICv3_TDIR. The hardware details are as follows:Quoted 8 lines
- GICv4.2 compliant implementation
- SupportsFEAT_GICv3,FEAT_GICv3p1,FEAT_GICv4,FEAT_GICv4p1, andFEAT_GICv3_NMI
- Does not supportFEAT_GICv3_LEGACY(deprecated)
- Does not supportFEAT_GICv3_TDIR(ICH_VTR_EL2.TDS == 0)For reference, compared with Arm Neoverse V3, the virtual GIC configuration is largely equivalent. The only missing non-deprecated architectural feature is
FEAT_GICv3_TDIR.A *very* significant difference. Given the cost of trapping between R-EL1 and NS-EL2, something as simple as accesses to
ICV_PMR_EL1result in an extremely expensive trap.Quoted 7 lines
The issue I see is that the CCA KVM code currently does not support a configuration (non-TDIR/common-trap) that normal KVM already supports. For normal guests, KVM handles systems without TDIR by using ICH_HCR_EL2.TC and the existing GICv3 CPU interface emulation path. However, Realm guests currently fail because the CCA path bypasses that existing emulation path, as Marc also pointed out in [1].
Plugging CCA in the emulation code will solve the *functional* aspect. The performance aspect is still there, unfortunately, and there isn't much KVM can do about that.
Quoted 4 lines
Also, this is not limited to systems that actually lack TDIR. The same failure can be reproduced on a TDIR-capable system by booting with:
kvm-arm.vgic_v3_common_trap=1This is a *debug* option for broken hardware. ThunderX, for example. You really are in good company when this bit is set.
Quoted 5 lines
So it seems that the current CCA KVM implementation does not yet cover a configuration that normal KVM already supports today, rather than this being a limitation of the RMM specification or the underlying hardware.
Quoted 8 lines
I've included a patch below which reuses the existing GICv3 early emulation path for Realm sysreg exits. This patch does not add any new vGIC emulation code, and leaves the existing vGIC emulation code unchanged. So I believe this is in line with Marc's request in [1]. With this patch, Realm guests can run when the common CPU interface trap path is enabled.
I tested the exact patch both on our real silicon and on QEMU, and confirmed that all Realm-related tests in kvm-unit-tests-cca passed.
Quoted 7 lines
KUTs are unfortunately not something that people run in production. I wonder why...
Please run a Linux guest compiled with
CONFIG_ARM64_PSEUDO_NMI=yand irqchip.gicv3_pseudo_nmi=1 on the command line. Run any significant workload (hackbench, for example), and report the overhead. This will give you the expected impact introduced by the lack of TDIR.
Sorry for the delayed response.
Understood. I will run a Linux guest with CONFIG_ARM64_PSEUDO_NMI=y and irqchip.gicv3_pseudo_nmi=1, measure a representative workload such as hackbench, and report the overhead.
Thanks,
Kohei
Quoted 5 lines
M.Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v16 44/45] KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
In reply to Kohei EnjuQuoted 10 lines · Kohei Enju
Quoted 9 lines · Steven Price
KVM advertises realm support when the RMM is available, and allows userspace to create a VM with
KVM_VM_TYPE_ARM_REALMon that basis.On CPUs that lack ICH_HCR_EL2.TDIR, KVM uses ICH_HCR_EL2.TC for normal guests so that
ICC_DIR_EL1is still trapped via the common GICv3 CPU interface trap. Realms cannot rely on the normal hyp-side trap handling for that fallback, so advertising RMI support on such systems lets userspace create a realm that cannot safely run.
Quoted 6 lines
Quoted 4 lines
Require the finalized
ARM64_HAS_ICH_HCR_EL2_TDIRcapability when reportingKVM_CAP_ARM_RMIand when acceptingKVM_VM_TYPE_ARM_REALM. This leaves normal VM creation unchanged on systems that need the TC workaround.Hi Steven,
Hi Kohei,
Sorry for the slow response.
Quoted 8 lines
Thanks for your work on upstreaming CCA.
In the v15 discussion [0], you asked whether the system I was testing was a "hacked up test system" or closer to "production hardware", and I said I would share more when the time came. I can now say that this is not a hacked-up test system. At Fujitsu, we have real hardware (FUJITSU-MONAKA) which implements CCA (
FEAT_RME) but does not implementFEAT_GICv3_TDIR. The hardware details are as follows:
Cool, I suspected that might be the case - it's good to know there's real hardware on it's way.
Quoted 16 lines
- GICv4.2 compliant implementation
- SupportsFEAT_GICv3,FEAT_GICv3p1,FEAT_GICv4,FEAT_GICv4p1, andFEAT_GICv3_NMI
- Does not supportFEAT_GICv3_LEGACY(deprecated)
- Does not supportFEAT_GICv3_TDIR(ICH_VTR_EL2.TDS == 0)For reference, compared with Arm Neoverse V3, the virtual GIC configuration is largely equivalent. The only missing non-deprecated architectural feature is
FEAT_GICv3_TDIR.The issue I see is that the CCA KVM code currently does not support a configuration (non-TDIR/common-trap) that normal KVM already supports. For normal guests, KVM handles systems without TDIR by using ICH_HCR_EL2.TC and the existing GICv3 CPU interface emulation path. However, Realm guests currently fail because the CCA path bypasses that existing emulation path, as Marc also pointed out in [1].
Quoted 3 lines
Also, this is not limited to systems that actually lack TDIR. The same failure can be reproduced on a TDIR-capable system by booting with:
kvm-arm.vgic_v3_common_trap=1
As Marc says that's a debugging option - handy for those of us who don't have a platform without TDIR to test with.
Quoted 21 lines
So it seems that the current CCA KVM implementation does not yet cover a configuration that normal KVM already supports today, rather than this being a limitation of the RMM specification or the underlying hardware.
I've included a patch below which reuses the existing GICv3 early emulation path for Realm sysreg exits. This patch does not add any new vGIC emulation code, and leaves the existing vGIC emulation code unchanged. So I believe this is in line with Marc's request in [1]. With this patch, Realm guests can run when the common CPU interface trap path is enabled.
I tested the exact patch both on our real silicon and on QEMU, and confirmed that all Realm-related tests in kvm-unit-tests-cca passed.
I'm not attached to this exact implementation, and I'm happy if the solution is reworked to better fit into the next revision.
Given that this configuration can be supported by reusing the existing KVM emulation infrastructure, I think it would be reasonable for CCA to support the non-TDIR/common-trap configuration rather than requiring ICH_HCR_EL2.TDIR unconditionally for Realm support.
Quoted 6 lines
Supporting this configuration would also allow us to validate the upstream CCA KVM implementation on real silicon using upstream code paths, and contribute additional real-hardware testing coverage as the implementation evolves.
I'd be very interested in hearing your thoughts.
So personally I think your patch is a good compromise. It gets the hardware working and I'm keen to enable real-hardware testing. Marc has a very valid point that in terms of performance this could be very bad. Pseudo NMI in particular will be terrible because accesses to GIC registers are used to "emulate" the NMI so the number of traps will be large, and the traps are much more expensive with CCA.
So I'll attempt to incorporate the changes in your patch, but obviously you'll have to decide for yourself whether the performance of the product is suitable.
Thanks,
Steve
Quoted 229 lines
Thanks,
Kohei[0] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/Below is the patch I tested, and it should be applied on top of this series.
----8<----
From11ce6b7e5ee2f21a71f657e9f45f58871d011286Mon Sep 17 00:00:00 2001
From: Kohei Enju <[email protected]>
Date: Tue, 4 Aug 2026 11:45:53 +0900
Subject: [PATCH] KVM: arm64: CCA: Reuse early vGIC sysreg emulation for Realm
exitsRealm vGIC CPU interface sysreg exits currently bypass KVM's early vGIC emulation and reach the generic sysreg descriptors. This breaks Realm guests when common-trap trapping is enabled, including on systems without ICH_HCR_EL2.TDIR.
Prepare REC exits in the state expected by the existing vGIC dispatcher and invoke it before KVM synchronizes the vGIC hardware state. Complete handled accesses through the REC run structure and re-enter the REC.
This reuses the existing vGIC emulation unchanged and allows Realm guests to run with the non-TDIR/common-trap configuration.
Signed-off-by Kohei Enju <[email protected]>--- arch/arm64/kvm/rmi-exit.c | 21 +------ arch/arm64/kvm/rmi.c | 122 ++++++++++++++++++++++++++++++++++---- 2 files changed, 112 insertions(+), 31 deletions(-)arch/arm64/kvm/rmi-exit.c +3
-18@@ -22,21 +22,14 @@ static int rec_exit_fatal(struct kvm_vcpu *vcpu, const char *reason,static void rec_exit_sync (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec; -u64 esr = rec->run->exit.esr; +u64 esr =kvm_vcpu_get_esr (vcpu);u8 ec =ESR_ELx_EC (esr);switch (ec) { -case ESR_ELx_EC_SYS64: { -int rt =ESR_ELx_SYS64_ISS_RT (esr); -bool is_write = (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == - ESR_ELx_SYS64_ISS_DIR_WRITE; - -if (is_write && rt <REC_RUN_GPRS ) -vcpu_set_reg (vcpu, rt, rec->run->exit.gprs[rt]); -else if (!is_write) +case ESR_ELx_EC_SYS64: +if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ)kvm_make_request (KVM_REQ_RMI , vcpu);break ; - }case ESR_ELx_EC_DABT_LOW:/* * The RMM reports the value of an MMIO write in gprs[0], @@ -141,14 +134,6 @@ int kvm_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)return rec_exit_fatal (vcpu,"Unexpected REC_ENTER status" , rec_run_ret); - vcpu->arch.fault.esr_el2 = rec->run->exit.esr; - vcpu->arch.fault.far_el2 = rec->run->exit.far; -/* HPFAR_EL2 is only valid for RMI_EXIT_SYNC */ - vcpu->arch.fault.hpfar_el2 =0 ; - -/* 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 :/* arch/arm64/kvm/rmi.c +109
-13@@ -1251,23 +1251,28 @@ static int kvm_rec_complete_psci(struct kvm_vcpu *vcpu)return r ?:1 ; } +static void noinstr kvm_rec_complete_sysreg_access(struct kvm_vcpu *vcpu) +{ +struct realm_rec *rec = &vcpu->arch.rec; +u64 esr =kvm_vcpu_get_esr (vcpu); +int rt; + +if (ESR_ELx_EC (esr) != ESR_ELx_EC_SYS64 || + (esr & ESR_ELx_SYS64_ISS_DIR_MASK) != ESR_ELx_SYS64_ISS_DIR_READ) +return ; + + rt =kvm_vcpu_sys_get_rt (vcpu); +if (rt <REC_RUN_GPRS ) + rec->run->enter.gprs[rt] =vcpu_get_reg (vcpu, rt); +} +int kvm_rec_handle_request (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec; -u64 esr;switch (rec->run->exit.exit_reason) {case RMI_EXIT_SYNC : - esr = rec->run->exit.esr; -if (ESR_ELx_EC (esr) == ESR_ELx_EC_SYS64 && - (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == - ESR_ELx_SYS64_ISS_DIR_READ) { -int rt =ESR_ELx_SYS64_ISS_RT (esr); - -if (rt <REC_RUN_GPRS ) - rec->run->enter.gprs[rt] = -vcpu_get_reg (vcpu, rt); - } +kvm_rec_complete_sysreg_access (vcpu);break ;case RMI_EXIT_PSCI :return kvm_rec_complete_psci (vcpu);@@ -1302,14 +1307,105 @@ static void noinstr load_realm_timer_state(struct kvm_vcpu *vcpu)write_sysreg_el0 (rec_exit->cntp_ctl,SYS_CNTP_CTL ); } +static void noinstr rec_prepare_exit_state(struct kvm_vcpu *vcpu) +{ +struct realm_rec *rec = &vcpu->arch.rec; +u64 esr = rec->run->exit.esr; +bool is_write; +int rt; + + vcpu->arch.fault.esr_el2 = esr; + vcpu->arch.fault.far_el2 = rec->run->exit.far; +/* HPFAR_EL2 is only valid for RMI_EXIT_SYNC */ + vcpu->arch.fault.hpfar_el2 =0 ; + +/* Reset the emulation flags for the next run of the REC */ + rec->run->enter.flags =0 ; + + is_write = (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == + ESR_ELx_SYS64_ISS_DIR_WRITE; +if (rec->run->exit.exit_reason !=RMI_EXIT_SYNC || +ESR_ELx_EC (esr) != ESR_ELx_EC_SYS64 || !is_write) +return ; + + rt =kvm_vcpu_sys_get_rt (vcpu); +if (rt <REC_RUN_GPRS ) +vcpu_set_reg (vcpu, rt, rec->run->exit.gprs[rt]); +} + +static int noinstr rec_perform_vgic_cpuif_access(struct kvm_vcpu *vcpu) +{ +unsigned long elr, spsr, pc, pstate; +int handled; + + elr =read_sysreg_el2 (SYS_ELR ); + spsr =read_sysreg_el2 (SYS_SPSR ); + pc = *vcpu_pc (vcpu); + pstate = *vcpu_cpsr (vcpu); + +/* +* RMM has already completed the trapped Realm instruction. +* Install disposable AArch64 exception-return state for +* the PC adjustment made by the existing early VGIC +* dispatcher, and restore both views before returning to +* the Realm plumbing. +*/ + *vcpu_cpsr (vcpu) = PSR_MODE_EL1h; +write_sysreg_el2 (0 ,SYS_ELR ); +write_sysreg_el2 (PSR_MODE_EL1h,SYS_SPSR ); + + handled =__vgic_v3_perform_cpuif_access (vcpu); + +write_sysreg_el2 (elr,SYS_ELR ); +write_sysreg_el2 (spsr,SYS_SPSR ); + *vcpu_pc (vcpu) = pc; + *vcpu_cpsr (vcpu) = pstate; + +return handled; +} + +static bool noinstr rec_handle_vgic_cpuif_exit(struct kvm_vcpu *vcpu) +{ +struct realm_rec *rec = &vcpu->arch.rec; +u64 esr; + +if (!static_branch_unlikely (&vgic_v3_cpuif_trap)) +return false; + + esr =kvm_vcpu_get_esr (vcpu); +if (rec->run->exit.exit_reason !=RMI_EXIT_SYNC || +ESR_ELx_EC (esr) != ESR_ELx_EC_SYS64) +return false; + +if (rec_perform_vgic_cpuif_access (vcpu) !=1 ) +return false; + +kvm_rec_complete_sysreg_access (vcpu); + +/* +* The emulation may have updated ICH_HCR_EL2.EOIcount. +* Synchronize that update before reading ICH_HCR_EL2 +* again to enable the CPU interface. +*/ +isb (); +sysreg_clear_set_s (SYS_ICH_HCR_EL2 ,0 , ICH_HCR_EL2_En); + +return true; +} + int noinstr kvm_rec_enter(struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec;int ret; - ret =rmi_rec_enter (rec->rec_phys, rec->run_phys); -if (!ret) +do { + ret =rmi_rec_enter (rec->rec_phys, rec->run_phys); +if (ret) +break ; +load_realm_timer_state (vcpu); +rec_prepare_exit_state (vcpu); + }while (rec_handle_vgic_cpuif_exit (vcpu));return ret; }
Re: [PATCH v16 44/45] KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
Quoted 83 lines · Steven Price
Quoted 10 lines · Kohei Enju
Quoted 9 lines · Steven Price
KVM advertises realm support when the RMM is available, and allows userspace to create a VM with
KVM_VM_TYPE_ARM_REALMon that basis.On CPUs that lack ICH_HCR_EL2.TDIR, KVM uses ICH_HCR_EL2.TC for normal guests so that
ICC_DIR_EL1is still trapped via the common GICv3 CPU interface trap. Realms cannot rely on the normal hyp-side trap handling for that fallback, so advertising RMI support on such systems lets userspace create a realm that cannot safely run.Quoted 6 lines
Quoted 4 lines
Require the finalized
ARM64_HAS_ICH_HCR_EL2_TDIRcapability when reportingKVM_CAP_ARM_RMIand when acceptingKVM_VM_TYPE_ARM_REALM. This leaves normal VM creation unchanged on systems that need the TC workaround.Hi Steven,
Hi Kohei,
Sorry for the slow response.
Quoted 8 lines
Thanks for your work on upstreaming CCA.
In the v15 discussion [0], you asked whether the system I was testing was a "hacked up test system" or closer to "production hardware", and I said I would share more when the time came. I can now say that this is not a hacked-up test system. At Fujitsu, we have real hardware (FUJITSU-MONAKA) which implements CCA (
FEAT_RME) but does not implementFEAT_GICv3_TDIR. The hardware details are as follows:Cool, I suspected that might be the case - it's good to know there's real hardware on it's way.
Quoted 16 lines
- GICv4.2 compliant implementation
- SupportsFEAT_GICv3,FEAT_GICv3p1,FEAT_GICv4,FEAT_GICv4p1, andFEAT_GICv3_NMI
- Does not supportFEAT_GICv3_LEGACY(deprecated)
- Does not supportFEAT_GICv3_TDIR(ICH_VTR_EL2.TDS == 0)For reference, compared with Arm Neoverse V3, the virtual GIC configuration is largely equivalent. The only missing non-deprecated architectural feature is
FEAT_GICv3_TDIR.The issue I see is that the CCA KVM code currently does not support a configuration (non-TDIR/common-trap) that normal KVM already supports. For normal guests, KVM handles systems without TDIR by using ICH_HCR_EL2.TC and the existing GICv3 CPU interface emulation path. However, Realm guests currently fail because the CCA path bypasses that existing emulation path, as Marc also pointed out in [1].
Quoted 3 lines
Also, this is not limited to systems that actually lack TDIR. The same failure can be reproduced on a TDIR-capable system by booting with:
kvm-arm.vgic_v3_common_trap=1As Marc says that's a debugging option - handy for those of us who don't have a platform without TDIR to test with.
Quoted 26 lines
So it seems that the current CCA KVM implementation does not yet cover a configuration that normal KVM already supports today, rather than this being a limitation of the RMM specification or the underlying hardware.
I've included a patch below which reuses the existing GICv3 early emulation path for Realm sysreg exits. This patch does not add any new vGIC emulation code, and leaves the existing vGIC emulation code unchanged. So I believe this is in line with Marc's request in [1]. With this patch, Realm guests can run when the common CPU interface trap path is enabled.
I tested the exact patch both on our real silicon and on QEMU, and confirmed that all Realm-related tests in kvm-unit-tests-cca passed.
I'm not attached to this exact implementation, and I'm happy if the solution is reworked to better fit into the next revision.
Given that this configuration can be supported by reusing the existing KVM emulation infrastructure, I think it would be reasonable for CCA to support the non-TDIR/common-trap configuration rather than requiring ICH_HCR_EL2.TDIR unconditionally for Realm support.
Supporting this configuration would also allow us to validate the upstream CCA KVM implementation on real silicon using upstream code paths, and contribute additional real-hardware testing coverage as the implementation evolves.
Quoted 8 lines
Quoted 1 line
I'd be very interested in hearing your thoughts.
So personally I think your patch is a good compromise. It gets the hardware working and I'm keen to enable real-hardware testing. Marc has a very valid point that in terms of performance this could be very bad. Pseudo NMI in particular will be terrible because accesses to GIC registers are used to "emulate" the NMI so the number of traps will be large, and the traps are much more expensive with CCA.
Yes, in that case ICV_PMR_EL1 would be accessed frequently, and the resulting traps would be expensive.
Quoted 4 lines
So I'll attempt to incorporate the changes in your patch, but obviously you'll have to decide for yourself whether the performance of the product is suitable.
Thanks for the clarification.
I agree with the performance concern, and I'll run some benchmarks on our hardware.
Thanks,
Kohei
Quoted 4 lines
Thanks,
Steve
Re: [PATCH v16 44/45] KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
Quoted 97 lines · Kohei Enju
Quoted 83 lines · Steven Price
Quoted 10 lines · Kohei Enju
Quoted 9 lines · Steven Price
KVM advertises realm support when the RMM is available, and allows userspace to create a VM with
KVM_VM_TYPE_ARM_REALMon that basis.On CPUs that lack ICH_HCR_EL2.TDIR, KVM uses ICH_HCR_EL2.TC for normal guests so that
ICC_DIR_EL1is still trapped via the common GICv3 CPU interface trap. Realms cannot rely on the normal hyp-side trap handling for that fallback, so advertising RMI support on such systems lets userspace create a realm that cannot safely run.Quoted 6 lines
Quoted 4 lines
Require the finalized
ARM64_HAS_ICH_HCR_EL2_TDIRcapability when reportingKVM_CAP_ARM_RMIand when acceptingKVM_VM_TYPE_ARM_REALM. This leaves normal VM creation unchanged on systems that need the TC workaround.Hi Steven,
Hi Kohei,
Sorry for the slow response.
Quoted 8 lines
Thanks for your work on upstreaming CCA.
In the v15 discussion [0], you asked whether the system I was testing was a "hacked up test system" or closer to "production hardware", and I said I would share more when the time came. I can now say that this is not a hacked-up test system. At Fujitsu, we have real hardware (FUJITSU-MONAKA) which implements CCA (
FEAT_RME) but does not implementFEAT_GICv3_TDIR. The hardware details are as follows:Cool, I suspected that might be the case - it's good to know there's real hardware on it's way.
Quoted 16 lines
- GICv4.2 compliant implementation
- SupportsFEAT_GICv3,FEAT_GICv3p1,FEAT_GICv4,FEAT_GICv4p1, andFEAT_GICv3_NMI
- Does not supportFEAT_GICv3_LEGACY(deprecated)
- Does not supportFEAT_GICv3_TDIR(ICH_VTR_EL2.TDS == 0)For reference, compared with Arm Neoverse V3, the virtual GIC configuration is largely equivalent. The only missing non-deprecated architectural feature is
FEAT_GICv3_TDIR.The issue I see is that the CCA KVM code currently does not support a configuration (non-TDIR/common-trap) that normal KVM already supports. For normal guests, KVM handles systems without TDIR by using ICH_HCR_EL2.TC and the existing GICv3 CPU interface emulation path. However, Realm guests currently fail because the CCA path bypasses that existing emulation path, as Marc also pointed out in [1].
Quoted 3 lines
Also, this is not limited to systems that actually lack TDIR. The same failure can be reproduced on a TDIR-capable system by booting with:
kvm-arm.vgic_v3_common_trap=1As Marc says that's a debugging option - handy for those of us who don't have a platform without TDIR to test with.
Quoted 26 lines
So it seems that the current CCA KVM implementation does not yet cover a configuration that normal KVM already supports today, rather than this being a limitation of the RMM specification or the underlying hardware.
I've included a patch below which reuses the existing GICv3 early emulation path for Realm sysreg exits. This patch does not add any new vGIC emulation code, and leaves the existing vGIC emulation code unchanged. So I believe this is in line with Marc's request in [1]. With this patch, Realm guests can run when the common CPU interface trap path is enabled.
I tested the exact patch both on our real silicon and on QEMU, and confirmed that all Realm-related tests in kvm-unit-tests-cca passed.
I'm not attached to this exact implementation, and I'm happy if the solution is reworked to better fit into the next revision.
Given that this configuration can be supported by reusing the existing KVM emulation infrastructure, I think it would be reasonable for CCA to support the non-TDIR/common-trap configuration rather than requiring ICH_HCR_EL2.TDIR unconditionally for Realm support.
Supporting this configuration would also allow us to validate the upstream CCA KVM implementation on real silicon using upstream code paths, and contribute additional real-hardware testing coverage as the implementation evolves.
Quoted 8 lines
Quoted 1 line
I'd be very interested in hearing your thoughts.
So personally I think your patch is a good compromise. It gets the hardware working and I'm keen to enable real-hardware testing. Marc has a very valid point that in terms of performance this could be very bad. Pseudo NMI in particular will be terrible because accesses to GIC registers are used to "emulate" the NMI so the number of traps will be large, and the traps are much more expensive with CCA.
Yes, in that case
ICV_PMR_EL1would be accessed frequently, and the resulting traps would be expensive.Quoted 1 line
Quoted 8 lines
Quoted 3 lines
So I'll attempt to incorporate the changes in your patch, but obviously you'll have to decide for yourself whether the performance of the product is suitable.
Thanks for the clarification.
I agree with the performance concern, and I'll run some benchmarks on our hardware.
Hi Steven and Marc,
Sorry for the late response.
The results confirm your concern. Our initial hackbench measurements showed considerable performance overhead for the non-TDIR Realm configuration with pseudo-NMI enabled. Due to restrictions on disclosing performance data for our hardware, I cannot share the absolute results or further platform details.
However, with pseudo-NMI disabled, performance was comparable to the TDIR-capable configuration. The difference was at most a few percent across runs.
There is a real need to run Realm VMs in environments that do not require pseudo-NMI, and non-TDIR hardware can meet that need. Pseudo-NMI is not enabled by default in many commonly used distributions, and native NMI support through FEAT_NMI may also reduce the need for pseudo-NMI in the near future. As a result, the number of use cases in which the observed performance limitation is relevant is likely to decrease. Therefore, the pseudo-NMI result does not necessarily make non-TDIR Realm support impractical for all current or future use cases.
Note that the initial measurements used pre-production versions of TF-RMM and TF-A. During our evaluation, we applied preliminary tuning to these firmware components and have already observed substantial performance improvements.
For the production release of our hardware, we plan to further tune the software stack, including these firmware components, together with the hardware configuration. We therefore believe that there remains meaningful room for further performance improvement, although we cannot yet quantify the final improvement.
I agree that non-TDIR has a performance limitation for trap-intensive configurations. However, given the actual customer demand for running CCA workloads that do not require pseudo-NMI, I believe that supporting Realm VMs on non-TDIR hardware still has clear practical value. It would also allow us to accelerate validation of the upstream CCA KVM implementation on real silicon using upstream code paths and contribute additional real-hardware test coverage as the implementation evolves.
Thanks,
Kohei
Quoted 8 lines
Thanks,
KoheiQuoted 4 lines
Thanks,
Steve
PATCH v16 45/45 KVM: arm64: CCA: Enable realms to be created
In reply to Steven PriceAll the pieces are now in place, so enable kvm_rmi_is_available when the RMM is detected.
--- arch/arm64/kvm/rmi.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/rmi.c +2 -1
@@ -1556,5 +1556,6 @@ void kvm_init_rmi(void)if (rmm_check_features ())return ; -/* Future patch will enable static branch kvm_rmi_is_available */ +kvm_info ("Realm guests supported\n" ); +static_branch_enable (&kvm_rmi_is_available); }
Signature · 1 line
2.43.0
Marc Zyngier
maintainerQuoted 4 lines · Steven Price
This series is based on the
guest_memfdin-place conversion v9 tree[1], which is itself based on kvm-x86/next. It is also available as a git repository:
Well done. Series on top of another random series, based on a random tree. Unusable. Have you realised there are reasons why we (KVM/arm64) ask people to base their series on a *released* tag from Linus' tree? This means that Sashiko cannot, once again, provide any feedback on this series, as it fails to apply.
Just to avoid any ambiguity: Sashiko reviews, as annoying as they are, are not optional. They are very much mandatory, and I wish I could turn back the clocks to run it on my own code...
Running it on your own is not enough, because *I* want to see what the tool finds. Not to mention that non-Gemini runs of sashiko are much less interesting (unsurprisingly).
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 13 lines · Marc Zyngier
Quoted 4 lines · Steven Price
This series is based on the
guest_memfdin-place conversion v9 tree[1], which is itself based on kvm-x86/next. It is also available as a git repository:Well done. Series on top of another random series, based on a random tree. Unusable. Have you realised there are reasons why we (KVM/arm64) ask people to base their series on a *released* tag from Linus' tree? This means that Sashiko cannot, once again, provide any feedback on this series, as it fails to apply.
Quoted 7 lines
Just to avoid any ambiguity: Sashiko reviews, as annoying as they are, are not optional. They are very much mandatory, and I wish I could turn back the clocks to run it on my own code...
Running it on your own is not enough, because *I* want to see what the tool finds. Not to mention that non-Gemini runs of sashiko are much less interesting (unsurprisingly).
Sorry, but I'm not sure what you expect me to do - clearly I want to base my changes on top of the guest_memfd series which is being worked on and not just ignore that work. So I've based my changes on top of the tree that Ackerley published.
I'm certainly not trying to avoid the Sashiko reviews - it's a rather unfortunate limitation of the public tool that it doesn't seem to be able to deal with these dependencies. Or is there some magic that I can do to fix this?
Thanks,
Steve
Marc Zyngier
maintainerQuoted 19 lines · Steven Price
Quoted 17 lines · Marc Zyngier
Quoted 4 lines · Steven Price
This series is based on the
guest_memfdin-place conversion v9 tree[1], which is itself based on kvm-x86/next. It is also available as a git repository:Well done. Series on top of another random series, based on a random tree. Unusable. Have you realised there are reasons why we (KVM/arm64) ask people to base their series on a *released* tag from Linus' tree? This means that Sashiko cannot, once again, provide any feedback on this series, as it fails to apply.
Just to avoid any ambiguity: Sashiko reviews, as annoying as they are, are not optional. They are very much mandatory, and I wish I could turn back the clocks to run it on my own code...
Quoted 8 lines
Quoted 3 lines
Running it on your own is not enough, because *I* want to see what the tool finds. Not to mention that non-Gemini runs of sashiko are much less interesting (unsurprisingly).
Sorry, but I'm not sure what you expect me to do - clearly I want to base my changes on top of the
guest_memfdseries which is being worked on and not just ignore that work. So I've based my changes on top of the tree that Ackerley published.
But that completely ignores the way the KVM/arm64 tree is managed. If you base a series on a bunch of unmerged patches, how do you expect people to do a sensible job at reviewing it? Now I need to ingest another pile of random patches just to review this set?
Not happening, sorry.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 30 lines · Marc Zyngier
Quoted 19 lines · Steven Price
Quoted 17 lines · Marc Zyngier
Quoted 4 lines · Steven Price
This series is based on the
guest_memfdin-place conversion v9 tree[1], which is itself based on kvm-x86/next. It is also available as a git repository:Well done. Series on top of another random series, based on a random tree. Unusable. Have you realised there are reasons why we (KVM/arm64) ask people to base their series on a *released* tag from Linus' tree? This means that Sashiko cannot, once again, provide any feedback on this series, as it fails to apply.
Just to avoid any ambiguity: Sashiko reviews, as annoying as they are, are not optional. They are very much mandatory, and I wish I could turn back the clocks to run it on my own code...
Quoted 8 lines
Quoted 3 lines
Running it on your own is not enough, because *I* want to see what the tool finds. Not to mention that non-Gemini runs of sashiko are much less interesting (unsurprisingly).
Sorry, but I'm not sure what you expect me to do - clearly I want to base my changes on top of the
guest_memfdseries which is being worked on and not just ignore that work. So I've based my changes on top of the tree that Ackerley published.
Quoted 4 lines
But that completely ignores the way the KVM/arm64 tree is managed. If you base a series on a bunch of unmerged patches, how do you expect people to do a sensible job at reviewing it? Now I need to ingest another pile of random patches just to review this set?
It's based on the same 'base' as v15 was - it depends on the unmerged guest_memfd changes. I've just updated to the latest revision from Ackerley. You've been reviewing the changes, and I've attempted to update the code based on that valuable feedback.
Clearly I'm not expecting this series to be merged yet - we're waiting for the guest_memfd changes. If you have a suggestion on how I should tackle this in the future then please let me know. I could bombard you with an even bigger series with Ackerley's series on the front, but I think you'd shout at me even more if I did that.
Quoted 1 line
Not happening, sorry.
Well I'm off on 'holiday' or more accurately to move house. The new house's (brand new) hot water cylinder is bust and the plumber is currently saying we can't even have cold water, never mind hot, so I'm going to spend the next week worrying about things other than this series.
If you provide review comments then I'll look at them when I get back.
Thanks,
Steve
Fuad Tabba
reviewerHi Steven,
On Mon, 3 Aug 2026 at 14:44, Steven Price <[email protected]> wrote:
...
Quoted 4 lines
* Rewrite Realm entry and exit handling to fit into the generic KVM run loop. Work which cannot be performed in the entry path is now completed through KVM requests, and MMIO, PSCI, RIPAS changes and host calls are adapted to the new flow.
Reading v16 with pKVM in mind: this rework helps, but the series still adds a few kvm_is_realm()/vcpu_is_rec() checks to the core arm64 code, and several sit next to, or inside the same condition as, the existing pKVM checks (the nommu path in kvm_arch_vcpu_load(), the timer offsets, the NISV abort injection). Both checks answer the same
question: is the guest's state owned by something other than KVM. It might be worth a common predicate covering both.
Cheers,
/fuad
Quoted 154 lines
* Rewrite Realm timer support to use KVM's IRQ-ops infrastructure and software resampling.
* Improve SRO cancellation and error handling. Ensure a cancelled operation isn't treated as successful, wrapper return values can carry negative Linux errors as well as RMI return values, and those errors are propagated through the KVM users.
* Check that the RMM supports the host page size before configuring it, and exclude firmware-reserved NOMAP memory when creating GPT entries.
* Expand the
KVM_ARM_RMI_POPULATEdocumentation to make clear that Arm CCA cannot preserve memory contents during an in-place shared-to-private conversion.* Allocate Realm parameters only while creating the Realm descriptor.
* Document the encoding of the arm64 VM types and require ICH_HCR_EL2.TDIR before advertising or creating Realm VMs.
This series is based on the
guest_memfdin-place conversion v9 tree[1], which is itself based on kvm-x86/next. It is also available as a git repository:https://gitlab.arm.com/linux-arm/linux-cca cca-host/v16
Work in progress changes for kvmtool are available from the git repository below:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/v13
The TF-RMM branch used for testing this series is available here:
https://git.trustedfirmware.org/TF-RMM/tf-rmm.git topics/rmm-v2.0-poc_3
There is a kvm-unit-test branch updated to support the attestation used in RMM v2.0 available here:
https://gitlab.arm.com/linux-arm/kvm-unit-tests-cca cca/v4
[0] https://developer.arm.com/documentation/den0137/2-0bet2/
One bet2 change, which moves metadata out of the individual address range descriptors, has intentionally not been implemented because that part of the specification is expected to be reverted.[1] https://github.com/googleprodkernel/linux-cc/commits/guest_memfd-inplace-conversion-v9
Jean-Philippe Brucker (6):
KVM: arm64: CCA: Propagate breakpoint and watchpoint counts to
userspace
KVM: arm64: CCA: Set breakpoint parameters throughSET_ONE_REG
KVM: arm64: CCA: Propagate max SVE vector length from the RMM
KVM: arm64: CCA: Configure max SVE vector length for a Realm
KVM: arm64: CCA: Provide register list for unfinalized RECs
KVM: arm64: CCA: Provide an accurate register listJoey Gouly (2):
KVM: arm64: CCA: Allow userspace to inject aborts
KVM: arm64: CCA: SupportRSI_HOST_CALLSteven Price (34):
firmware:arm_rmm: Add SMC definitions for calling the RMM
firmware:arm_rmm: Add wrappers for direct RMI calls
firmware:arm_rmm: Check for RMI support at init
firmware:arm_rmm: Configure the RMM with the host's page size
firmware:arm_rmm: Add support for SRO
firmware:arm_rmm: Ensure the RMM has GPT entries for memory
arm64: mm: Handle Granule Protection Faults (GPFs)
KVM: arm64: Avoid includinglinux/kvm_host.hinkvm_pgtable.h
KVM: arm64: CCA: Add wrappers for realm related RMIs
KVM: arm64: CCA: Check for RMI support at KVM init
KVM: arm64: CCA: Check for LPA2 support
KVM: arm64: CCA: Define the user ABI
KVM: arm64: CCA: Add basic infrastructure for creating a realm
KVM: arm64: CCA: Allow passing the machine type in KVM creation
KVM: arm64: CCA: Tear down RTTs
KVM: arm64: CCA: Allocate and free RECs to match vCPUs
KVM: arm64: CCA: Support the VGIC in realms
KVM: arm64: CCA: Support timers in realm RECs
KVM: arm64: CCA: Handle realm enter/exit
KVM: arm64: CCA: HandleRMI_EXIT_RIPAS_CHANGE
KVM: arm64: CCA: Handle realm MMIO emulation
KVM: arm64: Expose support for private memory
KVM: arm64: CCA: Create the realm descriptor
KVM: arm64: CCA: Activate realms on first vCPU run
KVM: arm64: CCA: Allow populating initial contents
KVM: arm64: CCA: Set RIPAS of initial memslots
KVM: arm64: CCA: Support runtime faulting of memory
KVM: arm64: CCA: Handle realm vCPU load
KVM: arm64: CCA: Validate register access for Realm VMs
KVM: arm64: CCA: Handle Realm PSCI requests
KVM: arm64: WARN on injected undef exceptions
KVM: arm64: CCA: Prevent Device mappings for realms
KVM: arm64: CCA: Require ICH_HCR_EL2.TDIR for realms
KVM: arm64: CCA: Enable realms to be createdSuzuki K Poulose (3):
KVM: arm64: Includekvm_emulate.hinkvm/arm_psci.h
KVM: arm64: CCA: Don't expose unsupported capabilities for realm
guests
KVM: arm64: CCA: Allow checking SVE on VM instanceDocumentation/virt/kvm/api.rst | 70 +- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kvm_asm.h | 2 + arch/arm64/include/asm/kvm_emulate.h | 37 + arch/arm64/include/asm/kvm_host.h | 12 +- arch/arm64/include/asm/kvm_pgtable.h | 6 +- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/include/asm/kvm_rmi.h | 139 +++ arch/arm64/include/asm/rmi_cmds.h | 465 ++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kernel/cpufeature.c | 1 + arch/arm64/kvm/Kconfig | 2 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arch_timer.c | 34 +- arch/arm64/kvm/arm.c | 139 ++- arch/arm64/kvm/guest.c | 93 +- arch/arm64/kvm/handle_exit.c | 14 + arch/arm64/kvm/hyp/pgtable.c | 1 + arch/arm64/kvm/hypercalls.c | 4 +- arch/arm64/kvm/inject_fault.c | 5 +- arch/arm64/kvm/mmio.c | 16 +- arch/arm64/kvm/mmu.c | 144 ++- arch/arm64/kvm/reset.c | 13 +- arch/arm64/kvm/rmi-exit.c | 178 +++ arch/arm64/kvm/rmi.c | 1561 ++++++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 47 +- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/mm/fault.c | 28 +- drivers/firmware/Kconfig | 1 + drivers/firmware/Makefile | 1 + drivers/firmware/arm_rmm/Kconfig | 26 + drivers/firmware/arm_rmm/Makefile | 2 + drivers/firmware/arm_rmm/rmi.c | 776 +++++++++++++ include/kvm/arm_psci.h | 2 + include/linux/arm-rmi-cmds.h | 201 ++++ include/linux/arm-smccc-rmi.h | 493 ++++++++ include/uapi/linux/kvm.h | 20 +- 37 files changed, 4446 insertions(+), 95 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_rmi.h create mode 100644 arch/arm64/include/asm/rmi_cmds.h create mode 100644 arch/arm64/kvm/rmi-exit.c create mode 100644 arch/arm64/kvm/rmi.c create mode 100644 drivers/firmware/arm_rmm/Kconfig create mode 100644 drivers/firmware/arm_rmm/Makefile create mode 100644 drivers/firmware/arm_rmm/rmi.c create mode 100644 include/linux/arm-rmi-cmds.h create mode 100644 include/linux/arm-smccc-rmi.h--
2.43.0
Suzuki K Poulose
reviewerQuoted 4 lines · Steven Price
This series adds support for running protected VMs using KVM under the Arm Confidential Compute Architecture (CCA), including the firmware support needed to communicate with the Realm Management Monitor (RMM).
...
Quoted 10 lines
This series is based on the
guest_memfdin-place conversion v9 tree[1], which is itself based on kvm-x86/next. It is also available as a git repository:https://gitlab.arm.com/linux-arm/linux-cca cca-host/v16
Work in progress changes for kvmtool are available from the git repository below:
Here is a cleaned up version, rebased on to Will's kvmtool master branch:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/kvm-v16
(This works with the v15 of the KVM series too) Build on top of Fuad's Guest memfd support patches.
Cheers
Suzuki
Quoted 2 lines
On 8/7/26 8:05 AM, Suzuki K Poulose wrote:
[...]
Quoted 9 lines
Here is a cleaned up version, rebased on to Will's kvmtool master branch:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/kvm-v16
(This works with the v15 of the KVM series too) Build on top of Fuad's Guest memfd support patches.
With the following combination, I'm able boot up the realm guest.
tf-rmm: https://git.trustedfirmware.org/TF-RMM/tf-rmm.git (branch: topics/rmm-v2.0-poc_2) tf-a: https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git (branch: master) host: https://git.gitlab.arm.com/linux-arm/linux-cca.git (branch: cca-host/v16) kvmtool: https://gitlab.arm.com/linux-arm/kvmtool-cca (branch: cca/kvm-v16)
However, the guest can't boot up and become stuck in SMC_RSI_IPA_STATE_SET request, which can't completed by the host.
tf-rmm: https://git.trustedfirmware.org/TF-RMM/tf-rmm.git (branch: topics/rmm-v2.0-poc_3)
(1) Login the emulated host
machine$ ssh -o StrictHostKeyChecking=no [email protected]
(2) Start realm guest using kvmtool
root@host:~# lkvm run --realm -c 1 -m 256 \
-k /mnt/linux/arch/arm64/boot/Image \
-i /mnt/buildroot/output/images/rootfs.cpio.xz \
-p earlycon=uart,mmio,0x101000000
:
[ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x000f0510]
[ 0.000000] Linux version 7.2.0-rc5-gavin-gf5098b6bae76 ([email protected]) (gcc (GCC) 14.3.1 20251022 (Red Hat 14.3.1-4), GNU ld version 2.41-65.el10) #47 SMP PREEMPT Mon Jul 27 02:29:03 EDT 2026
[ 0.000000] KASLR enabled
[ 0.000000] Machine model: linux,dummy-virt
[ 0.000000] earlycon: uart0 at MMIO 0x0000000101000000 (options '')
[ 0.000000] printk: legacy bootconsole [uart0] enabled
[ 0.000000] efi: UEFI not found.
[ 0.000000] OF: reserved mem: Reserved memory: No reserved-memory node in the DT
[ 0.000000] NUMA: Faking a node at [mem 0x0000000080000000-0x000000008fffffff]
[ 0.000000] NODE_DATA(0) allocated [mem 0x8ff76dc0-0x8ff7ac7f]
[ 0.000000] psci: probing for conduit method from DT.
[ 0.000000] psci: PSCIv1.1 detected in firmware.
[ 0.000000] psci: Using standard PSCI v0.2 function IDs
[ 0.000000] psci: MIGRATE_INFO_TYPE not supported.
[ 0.000000] psci: SMC Calling Convention v1.2
[ 0.000000] RME: Using RSI version 1.0
<... no more output from the guest ...>(3) The output from host's serial console
SMC_RMI_REALM_CREATE 12a4c7000 12a4c6000 > RMI_INCOMPLETE 0 24 0 0
SMC_RMI_OP_MEM_DONATE 0 1002c7098 9 > RMI_INCOMPLETE 9 0 0 0
SMC_RMI_OP_CONTINUE 0 0 > RMI_SUCCESS 0 0
SMC_RMI_RTT_UNPROT_UNMAP 12a4c7000 18f85c000 18fe00000 0 0 > RMI_ERROR_RTT 2 0 0 0 0
SMC_RMI_RTT_UNPROT_UNMAP 12a4c7000 18fe00000 18fe10000 0 0 > RMI_ERROR_RTT 2 0 0 0 0
SMC_RMI_RTT_UNPROT_UNMAP 12a4c7000 18fe00000 18fe10000 0 0 > RMI_ERROR_RTT 2 0 0 0 0
SMC_RMI_REC_CREATE 12a4c7000 12a392000 12a391000 > RMI_INCOMPLETE 0 40 0 0
SMC_RMI_OP_MEM_DONATE 0 106406098 10 > RMI_INCOMPLETE 10 0 0 0
SMC_RMI_OP_CONTINUE 0 0 > RMI_SUCCESS 0 0
SMC_RMI_REALM_ACTIVATE 12a4c7000 > RMI_SUCCESS
Unhandled write S2_0_C0_C2_2
SMC_RMI_RTT_DATA_MAP 12a4c7000 8ffb4000 8ffb5000 1 129f1d004 > RMI_INCOMPLETE 0 0 0 1
SMC_RMI_OP_CONTINUE 0 0 > RMI_SUCCESS 8ffb5000 0
PSCI_84000000 0 0 0 0 0 0 0 > 10001 0 0 0
PSCI_84000006 0 0 0 0 0 0 0 > ffffffffffffffff 0 0 0
PSCI_8400000a 80000000 0 0 0 0 0 0 > 0 0 0 0
SMC_80000000 0 0 0 0 0 0 0 > 10002 0 0 0
SMC_84000050 1 0 0 ffffacd67d1e8000 ffffacd67d124000 0 0 > ffffffffffffffff 0 0 0
SMC_80000001 80000002 ffff 0 ffffacd67d1e8000 ffffacd67d124000 ffffacd67cd5d000 ffffacd67cd5dd38 > ffffffffffffffff 0 0 0
PSCI_8400000a c4000001 0 0 0 0 0 0 > 0 0 0 0
PSCI_8400000a c4000012 0 0 0 0 0 0 > ffffffffffffffff 0 0 0
PSCI_8400000a c4000015 0 0 0 0 0 0 > ffffffffffffffff 0 0 0
SMC_8600ff01 ffffacd67cfc0740 10002 0 0 0 0 ffffacd67cfb3cc8 > ffffffffffffffff 0 0 0
SMC_RSI_VERSION 10000 > RSI_SUCCESS 10000 10001
SMC_RSI_REALM_CONFIG 81385000 > RSI_SUCCESS
SMC_RSI_IPA_STATE_SET 80000000 90000000 1 0
SMC_RMI_RTT_SET_RIPAS 12a4c7000 12a392000 80000000 90000000 > RMI_ERROR_RTT 3
SMC_RMI_RTT_SET_RIPAS 12a4c7000 12a392000 80000000 90000000 > RMI_ERROR_RTT 3
SMC_RMI_RTT_SET_RIPAS 12a4c7000 12a392000 80000000 90000000 > RMI_ERROR_RTT 3
SMC_RMI_RTT_SET_RIPAS 12a4c7000 12a392000 80000000 90000000 > RMI_ERROR_RTT 3
SMC_RMI_RTT_SET_RIPAS 12a4c7000 12a392000 80000000 90000000 > RMI_ERROR_RTT 3
SMC_RMI_RTT_SET_RIPAS 12a4c7000 12a392000 80000000 90000000 > RMI_ERROR_RTT 3<... the last message repeats ...>
Thanks,
Gavin
Suzuki K Poulose
reviewerQuoted 10 lines · Gavin Shan
On 8/7/26 8:05 AM, Suzuki K Poulose wrote:
[...]
Quoted 6 lines
Here is a cleaned up version, rebased on to Will's kvmtool master branch:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/kvm-v16
Quoted 8 lines
Quoted 3 lines
(This works with the v15 of the KVM series too) Build on top of Fuad's Guest memfd support patches.
With the following combination, I'm able boot up the realm guest.
tf-rmm: https://git.trustedfirmware.org/TF-RMM/tf-
rmm.git (branch: topics/rmm-v2.0-poc_2)
Please be aware that CCA KVM v15 onwards, the above branch is not compatible. You should be using rmm-v2.0-poc_3. Please could you confirm if this is still an issue ?
Cheers
Suzuki
Quoted 89 lines
tf-a: https://git.trustedfirmware.org/TF-A/trusted-firmware-
a.git (branch: master)
host: https://git.gitlab.arm.com/linux-arm/linux-
cca.git (branch: cca-host/v16)
kvmtool: https://gitlab.arm.com/linux-arm/kvmtool-
cca (branch: cca/kvm-v16)However, the guest can't boot up and become stuck in
SMC_RSI_IPA_STATE_SETrequest, which can't completed by the host.tf-rmm: https://git.trustedfirmware.org/TF-RMM/tf-
rmm.git (branch: topics/rmm-v2.0-poc_3)(1) Login the emulated host
machine$ ssh -o StrictHostKeyChecking=no [email protected]
(2) Start realm guest using kvmtool
root@host:~# lkvm run --realm -c 1 -m 256 \ -k /mnt/linux/arch/arm64/boot/Image \ -i /mnt/buildroot/output/images/rootfs.cpio.xz \ -p earlycon=uart,mmio,0x101000000 : [ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x000f0510] [ 0.000000] Linux version 7.2.0-rc5-gavin-gf5098b6bae76 ([email protected]) (gcc (GCC) 14.3.1 20251022 (Red Hat 14.3.1-4), GNU ld version 2.41-65.el10) #47 SMP PREEMPT Mon Jul 27 02:29:03 EDT 2026 [ 0.000000] KASLR enabled [ 0.000000] Machine model: linux,dummy-virt [ 0.000000] earlycon: uart0 at MMIO 0x0000000101000000 (options '') [ 0.000000] printk: legacy bootconsole [uart0] enabled [ 0.000000] efi: UEFI not found. [ 0.000000] OF: reserved mem: Reserved memory: No reserved-memory node in the DT [ 0.000000] NUMA: Faking a node at [mem 0x0000000080000000-0x000000008fffffff] [ 0.000000] NODE_DATA(0) allocated [mem 0x8ff76dc0-0x8ff7ac7f] [ 0.000000] psci: probing for conduit method from DT. [ 0.000000] psci: PSCIv1.1 detected in firmware. [ 0.000000] psci: Using standard PSCI v0.2 function IDs [ 0.000000] psci: MIGRATE_INFO_TYPE not supported. [ 0.000000] psci: SMC Calling Convention v1.2 [ 0.000000] RME: Using RSI version 1.0 <... no more output from the guest ...>(3) The output from host's serial console
SMC_RMI_REALM_CREATE
12a4c700012a4c6000>RMI_INCOMPLETE0 24 0 0
SMC_RMI_OP_MEM_DONATE 01002c70989 >RMI_INCOMPLETE9 0 0 0
SMC_RMI_OP_CONTINUE 0 0 >RMI_SUCCESS0 0
SMC_RMI_RTT_UNPROT_UNMAP12a4c700018f85c00018fe000000 0 >RMI_ERROR_RTT2 0 0 0 0
SMC_RMI_RTT_UNPROT_UNMAP12a4c700018fe0000018fe100000 0 >RMI_ERROR_RTT2 0 0 0 0
SMC_RMI_RTT_UNPROT_UNMAP12a4c700018fe0000018fe100000 0 >RMI_ERROR_RTT2 0 0 0 0
SMC_RMI_REC_CREATE12a4c700012a39200012a391000>RMI_INCOMPLETE0 40 0 0
SMC_RMI_OP_MEM_DONATE 0 106406098 10 >RMI_INCOMPLETE10 0 0 0
SMC_RMI_OP_CONTINUE 0 0 >RMI_SUCCESS0 0
SMC_RMI_REALM_ACTIVATE12a4c7000>RMI_SUCCESS
Unhandled writeS2_0_C0_C2_2
SMC_RMI_RTT_DATA_MAP12a4c70008ffb40008ffb50001129f1d004>RMI_INCOMPLETE0 0 0 1
SMC_RMI_OP_CONTINUE 0 0 >RMI_SUCCESS8ffb50000
PSCI_84000000 0 0 0 0 0 0 0 > 10001 0 0 0
PSCI_84000006 0 0 0 0 0 0 0 > ffffffffffffffff 0 0 0
PSCI_8400000a 80000000 0 0 0 0 0 0 > 0 0 0 0
SMC_80000000 0 0 0 0 0 0 0 > 10002 0 0 0
SMC_84000050 1 0 0ffffacd67d1e8000ffffacd67d1240000 0 > ffffffffffffffff 0 0 0
SMC_80000001 80000002 ffff 0ffffacd67d1e8000ffffacd67d124000ffffacd67cd5d000ffffacd67cd5dd38> ffffffffffffffff 0 0 0 PSCI_8400000ac40000010 0 0 0 0 0 > 0 0 0 0
PSCI_8400000ac40000120 0 0 0 0 0 > ffffffffffffffff 0 0 0
PSCI_8400000ac40000150 0 0 0 0 0 > ffffffffffffffff 0 0 0
SMC_8600ff01ffffacd67cfc074010002 0 0 0 0ffffacd67cfb3cc8> ffffffffffffffff 0 0 0 SMC_RSI_VERSION 10000 >RSI_SUCCESS10000 10001
SMC_RSI_REALM_CONFIG 81385000 >RSI_SUCCESS
SMC_RSI_IPA_STATE_SET 80000000 90000000 1 0
SMC_RMI_RTT_SET_RIPAS12a4c700012a39200080000000 90000000Quoted 1 line
RMI_ERROR_RTT3SMC_RMI_RTT_SET_RIPAS
12a4c700012a39200080000000 90000000Quoted 1 line
RMI_ERROR_RTT3SMC_RMI_RTT_SET_RIPAS
12a4c700012a39200080000000 90000000Quoted 1 line
RMI_ERROR_RTT3SMC_RMI_RTT_SET_RIPAS
12a4c700012a39200080000000 90000000Quoted 1 line
RMI_ERROR_RTT3SMC_RMI_RTT_SET_RIPAS
12a4c700012a39200080000000 90000000Quoted 1 line
RMI_ERROR_RTT3SMC_RMI_RTT_SET_RIPAS
12a4c700012a39200080000000 90000000Quoted 1 line
RMI_ERROR_RTT3<... the last message repeats ...>
Thanks,
Gavin
Quoted 15 lines · Suzuki K Poulose
Quoted 14 lines · Gavin Shan
On 8/7/26 8:05 AM, Suzuki K Poulose wrote:
[...]
Quoted 9 lines
Here is a cleaned up version, rebased on to Will's kvmtool master branch:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/kvm-v16
(This works with the v15 of the KVM series too) Build on top of Fuad's Guest memfd support patches.
Quoted 8 lines
Quoted 3 lines
With the following combination, I'm able boot up the realm guest.
tf-rmm: https://git.trustedfirmware.org/TF-RMM/tf- rmm.git (branch: topics/rmm-v2.0-poc_2)
Please be aware that CCA KVM v15 onwards, the above branch is not compatible. You should be using rmm-v2.0-poc_3. Please could you confirm if this is still an issue ?
With tf-rmm/topics/rmm-v2.0-poc_2 + cca/host-v16 + kvmtool/cca/v16, there is no issue and the realm guest can boot up successfully.
When tf-rmm/topics/rmm-v2.0-poc_3 is used, the realm guest boot gets stuck as I reported earlier. Note the host is emulated by QEMU (TCG mode).
As the following calltrace indicates, -EAGAIN is returned from tf-rmm::update_ripas() because true is returned from s2tte_drain_pending() for the S2TTE corresponding to IPA 0x80000000. Linux host received error (RMI_ERROR_RTT, level=3) in ripas_change(). Upon this specific error and the IPA range [0x80000000 0x90000000], find_map_level() returns level of 2, and realm_create_rtt_levels() returns 0 without populating any RTTs. After that, rmi_rtt_set_ripas() is re-executed and the above loop starts over again.
Linux host
==========
kvm_arch_vcpu_ioctl_run // cca/host-v16
check_vcpu_requests
kvm_check_request
kvm_rec_handle_request
kvm_complete_ripas_change
realm_set_ipa_state
ripas_change
rmi_rtt_set_ripas
SMC_RMI_RTT_SET_RIPAS
TF-RMM
======
SMC_RMI_RTT_SET_RIPAS // tf-rmm/topics/rmm-v2.0-poc_3
smc_rtt_set_ripas
s2tt_walk_lock_unlock
rtt_set_ripas_range
update_ripas
s2tte_drain_pending // true, returns -EAGAIN
The problem is the pending-bit for RTE corresponding IPA address 0x80000000 isn't cleared when SMC_RMI_RTT_SET_RIPAS is invoked. I didn't figure out how this bit is set and why it's not cleared in time.
Thanks,
Gavin
Quoted 85 lines
Cheers
SuzukiQuoted 79 lines
tf-a: https://git.trustedfirmware.org/TF-A/trusted-firmware- a.git (branch: master) host: https://git.gitlab.arm.com/linux-arm/linux- cca.git (branch: cca-host/v16) kvmtool: https://gitlab.arm.com/linux-arm/kvmtool- cca (branch: cca/kvm-v16)
However, the guest can't boot up and become stuck in
SMC_RSI_IPA_STATE_SETrequest, which can't completed by the host.tf-rmm: https://git.trustedfirmware.org/TF-RMM/tf- rmm.git (branch: topics/rmm-v2.0-poc_3)
(1) Login the emulated host
machine$ ssh -o StrictHostKeyChecking=no [email protected]
(2) Start realm guest using kvmtool
root@host:~# lkvm run --realm -c 1 -m 256 \ -k /mnt/linux/arch/arm64/boot/Image \ -i /mnt/buildroot/output/images/rootfs.cpio.xz \ -p earlycon=uart,mmio,0x101000000 : [ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x000f0510] [ 0.000000] Linux version 7.2.0-rc5-gavin-gf5098b6bae76 ([email protected]) (gcc (GCC) 14.3.1 20251022 (Red Hat 14.3.1-4), GNU ld version 2.41-65.el10) #47 SMP PREEMPT Mon Jul 27 02:29:03 EDT 2026 [ 0.000000] KASLR enabled [ 0.000000] Machine model: linux,dummy-virt [ 0.000000] earlycon: uart0 at MMIO 0x0000000101000000 (options '') [ 0.000000] printk: legacy bootconsole [uart0] enabled [ 0.000000] efi: UEFI not found. [ 0.000000] OF: reserved mem: Reserved memory: No reserved-memory node in the DT [ 0.000000] NUMA: Faking a node at [mem 0x0000000080000000-0x000000008fffffff] [ 0.000000] NODE_DATA(0) allocated [mem 0x8ff76dc0-0x8ff7ac7f] [ 0.000000] psci: probing for conduit method from DT. [ 0.000000] psci: PSCIv1.1 detected in firmware. [ 0.000000] psci: Using standard PSCI v0.2 function IDs [ 0.000000] psci: MIGRATE_INFO_TYPE not supported. [ 0.000000] psci: SMC Calling Convention v1.2 [ 0.000000] RME: Using RSI version 1.0 <... no more output from the guest ...>(3) The output from host's serial console
SMC_RMI_REALM_CREATE
12a4c700012a4c6000>RMI_INCOMPLETE0 24 0 0
SMC_RMI_OP_MEM_DONATE 01002c70989 >RMI_INCOMPLETE9 0 0 0
SMC_RMI_OP_CONTINUE 0 0 >RMI_SUCCESS0 0
SMC_RMI_RTT_UNPROT_UNMAP12a4c700018f85c00018fe000000 0 >RMI_ERROR_RTT2 0 0 0 0
SMC_RMI_RTT_UNPROT_UNMAP12a4c700018fe0000018fe100000 0 >RMI_ERROR_RTT2 0 0 0 0
SMC_RMI_RTT_UNPROT_UNMAP12a4c700018fe0000018fe100000 0 >RMI_ERROR_RTT2 0 0 0 0
SMC_RMI_REC_CREATE12a4c700012a39200012a391000>RMI_INCOMPLETE0 40 0 0
SMC_RMI_OP_MEM_DONATE 0 106406098 10 >RMI_INCOMPLETE10 0 0 0
SMC_RMI_OP_CONTINUE 0 0 >RMI_SUCCESS0 0
SMC_RMI_REALM_ACTIVATE12a4c7000>RMI_SUCCESS
Unhandled writeS2_0_C0_C2_2
SMC_RMI_RTT_DATA_MAP12a4c70008ffb40008ffb50001129f1d004>RMI_INCOMPLETE0 0 0 1
SMC_RMI_OP_CONTINUE 0 0 >RMI_SUCCESS8ffb50000
PSCI_84000000 0 0 0 0 0 0 0 > 10001 0 0 0
PSCI_84000006 0 0 0 0 0 0 0 > ffffffffffffffff 0 0 0
PSCI_8400000a 80000000 0 0 0 0 0 0 > 0 0 0 0
SMC_80000000 0 0 0 0 0 0 0 > 10002 0 0 0
SMC_84000050 1 0 0ffffacd67d1e8000ffffacd67d1240000 0 > ffffffffffffffff 0 0 0
SMC_80000001 80000002 ffff 0ffffacd67d1e8000ffffacd67d124000ffffacd67cd5d000ffffacd67cd5dd38> ffffffffffffffff 0 0 0 PSCI_8400000ac40000010 0 0 0 0 0 > 0 0 0 0
PSCI_8400000ac40000120 0 0 0 0 0 > ffffffffffffffff 0 0 0
PSCI_8400000ac40000150 0 0 0 0 0 > ffffffffffffffff 0 0 0
SMC_8600ff01ffffacd67cfc074010002 0 0 0 0ffffacd67cfb3cc8> ffffffffffffffff 0 0 0 SMC_RSI_VERSION 10000 >RSI_SUCCESS10000 10001
SMC_RSI_REALM_CONFIG 81385000 >RSI_SUCCESS
SMC_RSI_IPA_STATE_SET 80000000 90000000 1 0
SMC_RMI_RTT_SET_RIPAS12a4c700012a39200080000000 90000000 >RMI_ERROR_RTT3
SMC_RMI_RTT_SET_RIPAS12a4c700012a39200080000000 90000000 >RMI_ERROR_RTT3
SMC_RMI_RTT_SET_RIPAS12a4c700012a39200080000000 90000000 >RMI_ERROR_RTT3
SMC_RMI_RTT_SET_RIPAS12a4c700012a39200080000000 90000000 >RMI_ERROR_RTT3
SMC_RMI_RTT_SET_RIPAS12a4c700012a39200080000000 90000000 >RMI_ERROR_RTT3
SMC_RMI_RTT_SET_RIPAS12a4c700012a39200080000000 90000000 >RMI_ERROR_RTT3
<... the last message repeats ...>Thanks,
Gavin
Quoted 29 lines · Gavin Shan
As the following calltrace indicates,
-EAGAINis returned from tf-rmm::update_ripas() because true is returned froms2tte_drain_pending()for the S2TTE corresponding to IPA0x80000000. Linux host received error (RMI_ERROR_RTT, level=3) inripas_change(). Upon this specific error and the IPA range [0x800000000x90000000],find_map_level()returns level of 2, andrealm_create_rtt_levels()returns 0 without populating any RTTs. After that,rmi_rtt_set_ripas()is re-executed and the above loop starts over again.Linux host
==========
kvm_arch_vcpu_ioctl_run// cca/host-v16
check_vcpu_requests
kvm_check_request
kvm_rec_handle_request
kvm_complete_ripas_change
realm_set_ipa_state
ripas_change
rmi_rtt_set_ripas
SMC_RMI_RTT_SET_RIPASTF-RMM
======
SMC_RMI_RTT_SET_RIPAS// tf-rmm/topics/rmm-v2.0-poc_3
smc_rtt_set_ripas
s2tt_walk_lock_unlock
rtt_set_ripas_range
update_ripas
s2tte_drain_pending// true, returns-EAGAIN
Quoted 4 lines
The problem is the pending-bit for RTE corresponding IPA address
0x80000000isn't cleared whenSMC_RMI_RTT_SET_RIPASis invoked. I didn't figure out how this bit is set and why it's not cleared in time.
Hi Gavin, Suzuki,
I think I ran into a similar issue on rmm-v2.0-poc_3 last week. This looks like a potential RMM bug: could bit 32 be part of the physical Address (if PA >= 4 GiB)?
It seems s2tte_drain_pending() in lib/s2tt/src/s2tt.c checks bit 32 without checking whether the descriptor is valid or invalid.
In my testing, guarding the drain checks with a check for S2TTE_INVALID seemed to resolve the boot hang:
lib/s2tt/src/s2tt.c +0 -0
@@ -1701,6 +1701,10 @@ unsigned long
s2tte_clear_drain_pending(unsigned long s2tte)
bool s2tte_drain_pending(unsigned long s2tte) {
+ if ((s2tte & S2TT_DESC_VALID_MASK) != S2TTE_INVALID) {
+ return false;
+ }
+
return (s2tte & S2TTE_SW_DRAIN_PENDING_BIT) != 0UL;
}
@@ -1730,11 +1734,19 @@ unsigned longs2tte_clear_tlbi_pending(unsigned long s2tte)
bool s2tte_tlbi_pending(unsigned long s2tte)
{
+ if ((s2tte & S2TT_DESC_VALID_MASK) != S2TTE_INVALID) {
+ return false;
+ }
+
return (s2tte & S2TTE_SW_TLBI_PENDING_BIT) != 0UL;
}
unsigned int s2tte_drain_handle(unsigned long s2tte) {
+ if ((s2tte & S2TT_DESC_VALID_MASK) != S2TTE_INVALID) {
+ return 0U;
+ }
+
return (unsigned int)EXTRACT(S2TTE_SW_HANDLE, s2tte);
}
Sharing in case it helps.
Thanks,
Alper
Suzuki K Poulose
reviewerHi Alper, Gavin
Quoted 30 lines · Alper Gun
Quoted 29 lines · Gavin Shan
As the following calltrace indicates,
-EAGAINis returned from tf-rmm::update_ripas() because true is returned froms2tte_drain_pending()for the S2TTE corresponding to IPA0x80000000. Linux host received error (RMI_ERROR_RTT, level=3) inripas_change(). Upon this specific error and the IPA range [0x800000000x90000000],find_map_level()returns level of 2, andrealm_create_rtt_levels()returns 0 without populating any RTTs. After that,rmi_rtt_set_ripas()is re-executed and the above loop starts over again.Linux host
==========
kvm_arch_vcpu_ioctl_run// cca/host-v16
check_vcpu_requests
kvm_check_request
kvm_rec_handle_request
kvm_complete_ripas_change
realm_set_ipa_state
ripas_change
rmi_rtt_set_ripas
SMC_RMI_RTT_SET_RIPASTF-RMM
======
SMC_RMI_RTT_SET_RIPAS// tf-rmm/topics/rmm-v2.0-poc_3
smc_rtt_set_ripas
s2tt_walk_lock_unlock
rtt_set_ripas_range
update_ripas
s2tte_drain_pending// true, returns-EAGAIN
Quoted 3 lines
Quoted 3 lines
The problem is the pending-bit for RTE corresponding IPA address
0x80000000isn't cleared whenSMC_RMI_RTT_SET_RIPASis invoked. I didn't figure out how this bit is set and why it's not cleared in time.
Thanks for the details.
Quoted 16 lines
Quoted 1 line
Hi Gavin, Suzuki,
I think I ran into a similar issue on rmm-v2.0-poc_3 last week. This looks like a potential RMM bug: could bit 32 be part of the physical Address (if PA >= 4 GiB)?
It seems
s2tte_drain_pending()inlib/s2tt/src/s2tt.cchecks bit 32 without checking whether the descriptor is valid or invalid.In my testing, guarding the drain checks with a check for
S2TTE_INVALIDseemed to resolve the boot hang:
lib/s2tt/src/s2tt.c unsigned long · line 1706
Quoted 6 lines
lib/s2tt/src/s2tt.c +2
-0@@ -1701,6 +1701,10 @@ unsigned longs2tte_clear_drain_pending (unsigned long s2tte ) bool s2tte_drain_pending(unsigned long s2tte) { +if ((s2tte &S2TT_DESC_VALID_MASK ) !=S2TTE_INVALID ) { +return false;
We should use also consider cases where the entry is INVALID, but has HIPAS=ASSIGNED/ASSIGNED_DEV to make it tighter. So, I think it is better to use :
s2tte_is_unassigned() or in the library stick to :
if (!s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED))
return false;
May be we should assert this and make the caller responsible for checking the bit. I will leave it to the tf-RMM team to fix.
But for now, please use the above fix.
Cheers
Suzuki
Quoted 33 lines
lib/s2tt/src/s2tt.c +10
-0+ } +return (s2tte &S2TTE_SW_DRAIN_PENDING_BIT ) !=0UL ; }@@ -1730,11 +1734,19 @@ unsigned longs2tte_clear_tlbi_pending (unsigned long s2tte ) bool s2tte_tlbi_pending(unsigned long s2tte) { +if ((s2tte &S2TT_DESC_VALID_MASK ) !=S2TTE_INVALID ) { +return false; + } +return (s2tte &S2TTE_SW_TLBI_PENDING_BIT ) !=0UL ; }unsigned int s2tte_drain_handle (unsigned long s2tte) { +if ((s2tte &S2TT_DESC_VALID_MASK ) !=S2TTE_INVALID ) { +return 0U ; + } +return (unsigned int )EXTRACT (S2TTE_SW_HANDLE , s2tte); }Sharing in case it helps.
Thanks,
Alper
Hi Alper and Suzuki,
Quoted 68 lines · Suzuki K Poulose
Hi Alper, Gavin
Quoted 30 lines · Alper Gun
Quoted 29 lines · Gavin Shan
As the following calltrace indicates,
-EAGAINis returned from tf-rmm::update_ripas() because true is returned froms2tte_drain_pending()for the S2TTE corresponding to IPA0x80000000. Linux host received error (RMI_ERROR_RTT, level=3) inripas_change(). Upon this specific error and the IPA range [0x800000000x90000000],find_map_level()returns level of 2, andrealm_create_rtt_levels()returns 0 without populating any RTTs. After that,rmi_rtt_set_ripas()is re-executed and the above loop starts over again.Linux host
==========
kvm_arch_vcpu_ioctl_run // cca/host-v16check_vcpu_requests
kvm_check_request
kvm_rec_handle_requestkvm_complete_ripas_changerealm_set_ipa_state
ripas_change
rmi_rtt_set_ripasSMC_RMI_RTT_SET_RIPASTF-RMM
======
SMC_RMI_RTT_SET_RIPAS // tf-rmm/topics/rmm-v2.0-poc_3smc_rtt_set_ripas
s2tt_walk_lock_unlock
rtt_set_ripas_range
update_ripas
s2tte_drain_pending // true, returns-EAGAINQuoted 3 lines
Quoted 3 lines
The problem is the pending-bit for RTE corresponding IPA address
0x80000000isn't cleared whenSMC_RMI_RTT_SET_RIPASis invoked. I didn't figure out how this bit is set and why it's not cleared in time.Thanks for the details.
Quoted 16 lines
Quoted 1 line
Hi Gavin, Suzuki,
I think I ran into a similar issue on rmm-v2.0-poc_3 last week. This looks like a potential RMM bug: could bit 32 be part of the physical Address (if PA >= 4 GiB)?
It seems
s2tte_drain_pending()inlib/s2tt/src/s2tt.cchecks bit 32 without checking whether the descriptor is valid or invalid.In my testing, guarding the drain checks with a check for
S2TTE_INVALIDseemed to resolve the boot hang:lib/s2tt/src/s2tt.c unsigned long · line 1706
Quoted 6 lines
lib/s2tt/src/s2tt.c +2
-0@@ -1701,6 +1701,10 @@ unsigned longs2tte_clear_drain_pending (unsigned long s2tte ) bool s2tte_drain_pending(unsigned long s2tte) { +if ((s2tte &S2TT_DESC_VALID_MASK ) !=S2TTE_INVALID ) { +return false;We should use also consider cases where the entry is INVALID, but has HIPAS=ASSIGNED/ASSIGNED_DEV to make it tighter. So, I think it is better to use :
s2tte_is_unassigned()or in the library stick to :
Quoted 8 lines
if (!s2tte_has_hipas(s2tte,
S2TTE_INVALID_HIPAS_UNASSIGNED))
return false;May be we should assert this and make the caller responsible for checking the bit. I will leave it to the tf-RMM team to fix.
But for now, please use the above fix.
Both worked for me. With the extra check in place, the realm guest can boot up successfully.
FYI, The below additional checks in s2tte_tlbi_pending() and s2tte_drain_handle() aren't needed because they're always guarded by s2tte_drain_pending() in all calling sites.
Thanks,
Gavin
Quoted 38 lines
Cheers
SuzukiQuoted 33 lines
lib/s2tt/src/s2tt.c +10
-0+ } + return (s2tte & S2TTE_SW_DRAIN_PENDING_BIT) != 0UL; }@@ -1730,11 +1734,19 @@ unsigned longs2tte_clear_tlbi_pending (unsigned long s2tte ) bool s2tte_tlbi_pending(unsigned long s2tte) { +if ((s2tte &S2TT_DESC_VALID_MASK ) !=S2TTE_INVALID ) { +return false; + } + return (s2tte & S2TTE_SW_TLBI_PENDING_BIT) != 0UL; } unsigned int s2tte_drain_handle(unsigned long s2tte) { +if ((s2tte &S2TT_DESC_VALID_MASK ) !=S2TTE_INVALID ) { +return 0U ; + } + return (unsigned int)EXTRACT(S2TTE_SW_HANDLE, s2tte); }Sharing in case it helps.
Thanks,
Alper
Suzuki K Poulose
reviewerQuoted 80 lines · Gavin Shan
Hi Alper and Suzuki,
Quoted 68 lines · Suzuki K Poulose
Hi Alper, Gavin
Quoted 30 lines · Alper Gun
Quoted 29 lines · Gavin Shan
As the following calltrace indicates,
-EAGAINis returned from tf-rmm::update_ripas()because true is returned froms2tte_drain_pending()for the S2TTE corresponding to IPA0x80000000. Linux host received error (RMI_ERROR_RTT, level=3) inripas_change(). Upon this specific error and the IPA range [0x800000000x90000000],find_map_level()returns level of 2, andrealm_create_rtt_levels()returns 0 without populating any RTTs. After that,rmi_rtt_set_ripas()is re-executed and the above loop starts over again.Linux host
==========
kvm_arch_vcpu_ioctl_run // cca/host-v16check_vcpu_requests
kvm_check_request
kvm_rec_handle_requestkvm_complete_ripas_changerealm_set_ipa_state
ripas_change
rmi_rtt_set_ripasSMC_RMI_RTT_SET_RIPASTF-RMM
======
SMC_RMI_RTT_SET_RIPAS // tf-rmm/topics/rmm- v2.0-poc_3smc_rtt_set_ripas
s2tt_walk_lock_unlock
rtt_set_ripas_range
update_ripas
s2tte_drain_pending // true, returns-EAGAINQuoted 3 lines
Quoted 3 lines
The problem is the pending-bit for RTE corresponding IPA address
0x80000000isn't cleared whenSMC_RMI_RTT_SET_RIPASis invoked. I didn't figure out how this bit is set and why it's not cleared in time.Thanks for the details.
Quoted 16 lines
Quoted 1 line
Hi Gavin, Suzuki,
I think I ran into a similar issue on rmm-v2.0-poc_3 last week. This looks like a potential RMM bug: could bit 32 be part of the physical Address (if PA >= 4 GiB)?
It seems
s2tte_drain_pending()inlib/s2tt/src/s2tt.cchecks bit 32 without checking whether the descriptor is valid or invalid.In my testing, guarding the drain checks with a check for
S2TTE_INVALIDseemed to resolve the boot hang:lib/s2tt/src/s2tt.c unsigned long · line 1706
Quoted 6 lines
lib/s2tt/src/s2tt.c +2
-0@@ -1701,6 +1701,10 @@ unsigned longs2tte_clear_drain_pending (unsigned long s2tte ) bool s2tte_drain_pending(unsigned long s2tte) { +if ((s2tte &S2TT_DESC_VALID_MASK ) !=S2TTE_INVALID ) { +return false;We should use also consider cases where the entry is INVALID, but has HIPAS=ASSIGNED/ASSIGNED_DEV to make it tighter. So, I think it is better to use :
s2tte_is_unassigned()or in the library stick to :Quoted 8 lines
if (!s2tte_has_hipas(s2tte,
S2TTE_INVALID_HIPAS_UNASSIGNED)) return false;May be we should assert this and make the caller responsible for checking the bit. I will leave it to the tf-RMM team to fix.
But for now, please use the above fix.
Quoted 6 lines
Both worked for me. With the extra check in place, the realm guest can boot up successfully.
FYI, The below additional checks in
s2tte_tlbi_pending()ands2tte_drain_handle()aren't needed because they're always guarded bys2tte_drain_pending()in all calling sites.
fyi, the tf-RMM patch is out for review here : (Thanks Javier)
https://review.trustedfirmware.org/c/TF-RMM/tf-rmm/+/53531
Please feel free to cherry-pick that one
Cheers
Suzuki