arm64: Support for Arm CCA in KVM
Subsystems (5)
v10 cover arm64: Support for Arm CCA in KVM
This series adds support for running protected VMs using KVM under the Arm Confidential Compute Architecture (CCA).
The related guest support was merged for v6.14-rc1 so you no longer need that separately.
There are a few changes since v9, many thanks for the review comments. The highlights are below, and individual patches have a changelog.
* Fix a potential issue where the host was walking the stage 2 page tables on realm destruction. If the RMM didn't zero when undelegated (which it isn't required to) then the kernel would attempt to work the junk values and crash.
* Avoid RCU stall warnings by correctly settign may_block in kvm_free_stage2_pgd().
* Rebased onto v6.17-rc1.
Things to note:
* The magic numbers for capabilities and ioctls have been updated. So you'll need to update your VMM. See below for the updated kvmtool branch.
* This series doesn't attempt to integrate with the guest-memfd changes that are being discussed (see below).
* Vishal raised an important question about what to do in the case of undelegate failures (also see below).
guest-memfd
===========
This series still implements the same API as previous versions, which means only the private memory of the guest is backed by guest-memfd, and the shared portion is accessed using VMAs from the VMM. This approach is compatible with the proposed changes to support guest-memfd backing shared memory but would require the VMM to mmap() the shared memory at the appropriate time so that KVM can access via the VMM's VMAs.
Changing to access both shared and private through the guest-memfd API shouldn't be difficult and could be done in a backwards compatible manner (with the VMM choosing which method to use). It's not clear to me whether we want to hold things up waiting for the full guest-memfd (and only support that uAPI) or if it would be fine to just support both approaches and let VMM's switch when they are ready.
There is a slight wrinkle around the 'populate' uAPI (KVM_CAP_ARM_RME_POPULATE). At the moment this expects 'double mapping' - the contents to be populated should be in the shared memory region, but the physical pages for the private region are also allocated from the guest_memfd. Arm CCA doesn't support a true 'in-place' conversion so this is required in some form. However it may make sense to allow the populate call to take a user space pointer for the data to be copied rather than require it to be in the shared memory region. We do already have a "flags" argument so there's also scope for easily supporting both options. The current approach integrates quite nicely in kvmtool with the existing logic for setting up a normal (non-CoCo) guest. But I'm aware kvmtool isn't entirely representative of what VMMs do, so I'd welcome feedback on what a good populate uAPI would look like.
Undelegation failure
====================
When the kernel is tearing down a CCA VM, it will attempt to "undelegate" pages allowing them to be used by the Normal World again. Assuming no bugs then these operations will always succeed. However, the RMM has the ability to return a failure if it considers these pages to still be in use. A failure like this is always a bug in the code, but it would be good to be able to handle these without resorting to an immediate BUG_ON().
The current approach in the code is to simply WARN() and use get_page() to take an extra reference to the page to stop it being reused (as it will immediately cause a GPF if the Normal World attempts to access the page).
However this will cause problems when we start supporting huge pages. It may be possible to use the HWPOISON infrastructure to flag the page as unusable (although note there is no way of 'scrubbing' a page to recover from this situation). The other option is to just accept this this "should never happen" and upgrade the WARN() to a BUG_ON() so we don't have to deal with the situation. A third option is to do nothing (other than WARN) and let the system run until the inevitable GPF which will probably bring it down (and hope there's enough time for the user to save work etc).
I'd welcome thoughts on the best solution.
====================
The ABI to the RMM (the RMI) is based on RMM v1.0-rel0 specification[1].
This series is based on v6.17-rc1. It is also available as a git repository:
https://gitlab.arm.com/linux-arm/linux-cca cca-host/v10
Work in progress changes for kvmtool are available from the git repository below:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/v8
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Jean-Philippe Brucker (7):
arm64: RME: Propagate number of breakpoints and watchpoints to
userspace
arm64: RME: Set breakpoint parameters through SET_ONE_REG
arm64: RME: Initialize PMCR.N with number counter supported by RMM
arm64: RME: Propagate max SVE vector length from RMM
arm64: RME: Configure max SVE vector length for a Realm
arm64: RME: Provide register list for unfinalized RME RECs
arm64: RME: Provide accurate register list
Joey Gouly (2):
arm64: RME: allow userspace to inject aborts
arm64: RME: support RSI_HOST_CALL
Steven Price (31):
arm64: RME: Handle Granule Protection Faults (GPFs)
arm64: RME: Add SMC definitions for calling the RMM
arm64: RME: Add wrappers for RMI calls
arm64: RME: Check for RME support at KVM init
arm64: RME: Define the user ABI
arm64: RME: ioctls to create and configure realms
KVM: arm64: Allow passing machine type in KVM creation
arm64: RME: RTT tear down
arm64: RME: Allocate/free RECs to match vCPUs
KVM: arm64: vgic: Provide helper for number of list registers
arm64: RME: Support for the VGIC in realms
KVM: arm64: Support timers in realm RECs
arm64: RME: Allow VMM to set RIPAS
arm64: RME: Handle realm enter/exit
arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE
KVM: arm64: Handle realm MMIO emulation
arm64: RME: Allow populating initial contents
arm64: RME: Runtime faulting of memory
KVM: arm64: Handle realm VCPU load
KVM: arm64: Validate register access for a Realm VM
KVM: arm64: Handle Realm PSCI requests
KVM: arm64: WARN on injected undef exceptions
arm64: Don't expose stolen time for realm guests
arm64: RME: Always use 4k pages for realms
arm64: RME: Prevent Device mappings for Realms
arm_pmu: Provide a mechanism for disabling the physical IRQ
arm64: RME: Enable PMU support with a realm guest
arm64: RME: Hide KVM_CAP_READONLY_MEM for realm guests
KVM: arm64: Expose support for private memory
KVM: arm64: Expose KVM_ARM_VCPU_REC to user space
KVM: arm64: Allow activating realms
Suzuki K Poulose (3):
kvm: arm64: Include kvm_emulate.h in kvm/arm_psci.h
kvm: arm64: Don't expose debug capabilities for realm guests
arm64: RME: Allow checking SVE on VM instance
Documentation/virt/kvm/api.rst | 92 +- arch/arm64/include/asm/kvm_emulate.h | 38 + arch/arm64/include/asm/kvm_host.h | 13 +- arch/arm64/include/asm/kvm_rme.h | 135 ++ arch/arm64/include/asm/rmi_cmds.h | 508 ++++++++ arch/arm64/include/asm/rmi_smc.h | 269 ++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 49 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arch_timer.c | 44 +- arch/arm64/kvm/arm.c | 169 ++- arch/arm64/kvm/guest.c | 108 +- arch/arm64/kvm/hypercalls.c | 4 +- arch/arm64/kvm/inject_fault.c | 5 +- arch/arm64/kvm/mmio.c | 16 +- arch/arm64/kvm/mmu.c | 209 ++- arch/arm64/kvm/pmu-emul.c | 6 + arch/arm64/kvm/psci.c | 30 + arch/arm64/kvm/reset.c | 23 +- arch/arm64/kvm/rme-exit.c | 207 +++ arch/arm64/kvm/rme.c | 1746 ++++++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 53 +- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/kvm/vgic/vgic.c | 61 +- arch/arm64/mm/fault.c | 31 +- drivers/perf/arm_pmu.c | 15 + include/kvm/arm_arch_timer.h | 2 + include/kvm/arm_pmu.h | 4 + include/kvm/arm_psci.h | 2 + include/linux/perf/arm_pmu.h | 5 + include/uapi/linux/kvm.h | 29 +- 32 files changed, 3778 insertions(+), 101 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_rme.h create mode 100644 arch/arm64/include/asm/rmi_cmds.h create mode 100644 arch/arm64/include/asm/rmi_smc.h create mode 100644 arch/arm64/kvm/rme-exit.c create mode 100644 arch/arm64/kvm/rme.c
Signature · 1 line
2.43.0
PATCH v10 1/43 kvm: arm64: Include kvm_emulate.h in kvm/arm_psci.h
From: Suzuki K Poulose <[email protected]>
Fix a potential build error (like below, when asm/kvm_emulate.h gets included after the kvm/arm_psci.h) by including the missing header file in kvm/arm_psci.h:
./include/kvm/arm_psci.h: In function ‘kvm_psci_version’:./include/kvm/arm_psci.h:29:13: error: implicit declaration of function
‘vcpu_has_feature’; did you mean ‘cpu_have_feature’? [-Werror=implicit-function-declaration]
29 | if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2)) {
| ^~~~~~~~~~~~~~~~
| cpu_have_feature--- include/kvm/arm_psci.h | 2 ++ 1 file changed, 2 insertions(+)
include/kvm/arm_psci.h +2 -0
@@ -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 v10 2/43 arm64: RME: 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 v2:
* Include missing "Granule Protection Fault at level -1"
--- arch/arm64/mm/fault.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-)
arch/arm64/mm/fault.c +25 -6
@@ -854,6 +854,25 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,return 0 ; } +static int do_gpf_ptw (unsigned long far,unsigned long esr,struct pt_regs *regs) +{ +const struct fault_info *inf =esr_to_fault_info (esr); + +die_kernel_fault (inf->name, far, esr, regs); +return 0 ; +} + +static int do_gpf (unsigned long far,unsigned long esr,struct pt_regs *regs) +{ +const struct fault_info *inf =esr_to_fault_info (esr); + +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; + +arm64_notify_die (inf->name, regs, inf->sig, inf->code, far, esr); +return 0 ; +} +static const struct fault_info fault_info[] = { { do_bad,SIGKILL ,SI_KERNEL ,"ttbr address size fault" }, { do_bad,SIGKILL ,SI_KERNEL ,"level 1 address size fault" },
@@ -890,12 +909,12 @@ static const struct fault_info fault_info[] = { { do_bad, SIGKILL, SI_KERNEL, "unknown 32" }, { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" }, { do_bad, SIGKILL, SI_KERNEL, "unknown 34" }, - { do_bad, SIGKILL, SI_KERNEL, "unknown 35" }, - { do_bad, SIGKILL, SI_KERNEL, "unknown 36" }, - { do_bad, SIGKILL, SI_KERNEL, "unknown 37" }, - { do_bad, SIGKILL, SI_KERNEL, "unknown 38" }, - { do_bad, SIGKILL, SI_KERNEL, "unknown 39" }, - { do_bad, SIGKILL, SI_KERNEL, "unknown 40" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "Granule Protection Fault at level -1" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "Granule Protection Fault at level 0" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "Granule Protection Fault at level 1" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "Granule Protection Fault at level 2" }, + { do_gpf_ptw, SIGKILL, SI_KERNEL, "Granule Protection Fault at level 3" }, + { do_gpf, SIGBUS, SI_KERNEL, "Granule Protection Fault not on table walk" }, { do_bad, SIGKILL, SI_KERNEL, "level -1 address size fault" }, { do_bad, SIGKILL, SI_KERNEL, "unknown 42" }, { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level -1 translation fault" },
Signature · 1 line
2.43.0
Catalin Marinas
maintainerRe: [PATCH v10 02/43] arm64: RME: Handle Granule Protection Faults (GPFs)
Quoted 25 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 v2:
* Include missing "Granule Protection Fault at level -1"--- arch/arm64/mm/fault.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-)arch/arm64/mm/fault.c +0
-0
arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 863
Quoted 10 lines
arch/arm64/mm/fault.c +7
-0@@ -854,6 +854,25 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,return 0 ; } +static int do_gpf_ptw (unsigned long far,unsigned long esr,struct pt_regs *regs) +{ +const struct fault_info *inf =esr_to_fault_info (esr); + +die_kernel_fault (inf->name, far, esr, regs); +return 0 ; +}
This is fine, it's irrelevant whether the fault happened at EL0 or EL1.
arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 874
Quoted 10 lines
arch/arm64/mm/fault.c +10
-0+static int do_gpf (unsigned long far,unsigned long esr,struct pt_regs *regs) +{ +const struct fault_info *inf =esr_to_fault_info (esr); + +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; + +arm64_notify_die (inf->name, regs, inf->sig, inf->code, far, esr); +return 0 ; +}
The end result is somewhat similar but why not just return 1 and avoid the arm64_notify_die() call? Let do_mem_abort() handle the oops vs user signal. With die_kernel_fault() we print the "Unable to handle kernel..." message and some more information.
Signature · 1 line
Catalin
Re: [PATCH v10 02/43] arm64: RME: Handle Granule Protection Faults (GPFs)
Quoted 39 lines · Catalin Marinas
Quoted 25 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 v2:
* Include missing "Granule Protection Fault at level -1"--- arch/arm64/mm/fault.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-)arch/arm64/mm/fault.c +0
-0arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 863
Quoted 10 lines
arch/arm64/mm/fault.c +7
-0@@ -854,6 +854,25 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,return 0 ; } +static int do_gpf_ptw (unsigned long far,unsigned long esr,struct pt_regs *regs) +{ +const struct fault_info *inf =esr_to_fault_info (esr); + +die_kernel_fault (inf->name, far, esr, regs); +return 0 ; +}This is fine, it's irrelevant whether the fault happened at EL0 or EL1.
Quoted 16 lines
arch/arm64/mm/fault.c static int do_tag_check_fault(unsigned long far, unsigned long esr, · line 874
Quoted 10 lines
arch/arm64/mm/fault.c +10
-0+static int do_gpf (unsigned long far,unsigned long esr,struct pt_regs *regs) +{ +const struct fault_info *inf =esr_to_fault_info (esr); + +if (!is_el1_instruction_abort (esr) &&fixup_exception (regs, esr)) +return 0 ; + +arm64_notify_die (inf->name, regs, inf->sig, inf->code, far, esr); +return 0 ; +}The end result is somewhat similar but why not just return 1 and avoid the
arm64_notify_die()call? Letdo_mem_abort()handle the oops vs user signal. Withdie_kernel_fault()we print the "Unable to handle kernel..." message and some more information.
Yes, that makes sense - something has gone very wrong if the kernel hits a GPF but that's no reason not to output a (more) useful message.
Thanks,
Steve
PATCH v10 3/43 arm64: RME: Add SMC definitions for calling the RMM
In reply to Steven PriceThe RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.
The SMC definitions are based on DEN0137[1] version 1.0-rel0
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
---
Changes since 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.
--- arch/arm64/include/asm/rmi_smc.h | 269 +++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +269 -0
@@ -0,0 +1,269 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023-2024 ARM Ltd. +* +* The values and structures in this file are from the Realm Management Monitor +* specification (DEN0137) version 1.0-rel0: +* https://developer.arm.com/documentation/den0137/1-0rel0/ +*/ + +#ifndef __ASM_RMI_SMC_H +#define __ASM_RMI_SMC_H + +#include <linux/arm-smccc.h> + +#define SMC_RMI_CALL (func) \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD, \ + (func)) + +#define SMC_RMI_VERSION SMC_RMI_CALL(0x0150) +#define SMC_RMI_GRANULE_DELEGATE SMC_RMI_CALL(0x0151) +#define SMC_RMI_GRANULE_UNDELEGATE SMC_RMI_CALL(0x0152) +#define SMC_RMI_DATA_CREATE SMC_RMI_CALL(0x0153) +#define SMC_RMI_DATA_CREATE_UNKNOWN SMC_RMI_CALL(0x0154) +#define SMC_RMI_DATA_DESTROY SMC_RMI_CALL(0x0155) + +#define SMC_RMI_REALM_ACTIVATE SMC_RMI_CALL(0x0157) +#define SMC_RMI_REALM_CREATE SMC_RMI_CALL(0x0158) +#define SMC_RMI_REALM_DESTROY SMC_RMI_CALL(0x0159) +#define SMC_RMI_REC_CREATE SMC_RMI_CALL(0x015a) +#define SMC_RMI_REC_DESTROY SMC_RMI_CALL(0x015b) +#define SMC_RMI_REC_ENTER SMC_RMI_CALL(0x015c) +#define SMC_RMI_RTT_CREATE SMC_RMI_CALL(0x015d) +#define SMC_RMI_RTT_DESTROY SMC_RMI_CALL(0x015e) +#define SMC_RMI_RTT_MAP_UNPROTECTED SMC_RMI_CALL(0x015f) + +#define SMC_RMI_RTT_READ_ENTRY SMC_RMI_CALL(0x0161) +#define SMC_RMI_RTT_UNMAP_UNPROTECTED SMC_RMI_CALL(0x0162) + +#define SMC_RMI_PSCI_COMPLETE SMC_RMI_CALL(0x0164) +#define SMC_RMI_FEATURES SMC_RMI_CALL(0x0165) +#define SMC_RMI_RTT_FOLD SMC_RMI_CALL(0x0166) +#define SMC_RMI_REC_AUX_COUNT SMC_RMI_CALL(0x0167) +#define SMC_RMI_RTT_INIT_RIPAS SMC_RMI_CALL(0x0168) +#define SMC_RMI_RTT_SET_RIPAS SMC_RMI_CALL(0x0169) + +#define RMI_ABI_MAJOR_VERSION 1 +#define RMI_ABI_MINOR_VERSION 0 + +#define RMI_ABI_VERSION_GET_MAJOR (version) ((version) >> 16) +#define RMI_ABI_VERSION_GET_MINOR (version) ((version) & 0xFFFF) +#define RMI_ABI_VERSION (major, minor) (((major) << 16) | (minor)) + +#define RMI_UNASSIGNED 0 +#define RMI_ASSIGNED 1 +#define RMI_TABLE 2 + +#define RMI_RETURN_STATUS (ret) ((ret) & 0xFF) +#define RMI_RETURN_INDEX (ret) (((ret) >> 8) & 0xFF) + +#define RMI_SUCCESS 0 +#define RMI_ERROR_INPUT 1 +#define RMI_ERROR_REALM 2 +#define RMI_ERROR_REC 3 +#define RMI_ERROR_RTT 4 + +enum rmi_ripas { +RMI_EMPTY =0 , +RMI_RAM =1 , +RMI_DESTROYED =2 , +}; + +#define RMI_NO_MEASURE_CONTENT 0 +#define RMI_MEASURE_CONTENT 1 + +#define RMI_FEATURE_REGISTER_0_S2SZ GENMASK(7, 0) +#define RMI_FEATURE_REGISTER_0_LPA2 BIT(8) +#define RMI_FEATURE_REGISTER_0_SVE_EN BIT(9) +#define RMI_FEATURE_REGISTER_0_SVE_VL GENMASK(13, 10) +#define RMI_FEATURE_REGISTER_0_NUM_BPS GENMASK(19, 14) +#define RMI_FEATURE_REGISTER_0_NUM_WPS GENMASK(25, 20) +#define RMI_FEATURE_REGISTER_0_PMU_EN BIT(26) +#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS GENMASK(31, 27) +#define RMI_FEATURE_REGISTER_0_HASH_SHA_256 BIT(32) +#define RMI_FEATURE_REGISTER_0_HASH_SHA_512 BIT(33) +#define RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS GENMASK(37, 34) +#define RMI_FEATURE_REGISTER_0_MAX_RECS_ORDER GENMASK(41, 38) +#define RMI_FEATURE_REGISTER_0_Reserved GENMASK(63, 42) + +#define RMI_REALM_PARAM_FLAG_LPA2 BIT(0) +#define RMI_REALM_PARAM_FLAG_SVE BIT(1) +#define RMI_REALM_PARAM_FLAG_PMU BIT(2) + +/* +* Note many of these fields are smaller than u64 but all fields have u64 +* alignment, so use u64 to ensure correct alignment. +*/ +struct realm_params { +union {/* 0x0 */ +struct { +u64 flags; +u64 s2sz; +u64 sve_vl; +u64 num_bps; +u64 num_wps; +u64 pmu_num_ctrs; +u64 hash_algo; + }; +u8 padding0[0x400 ]; + }; +union {/* 0x400 */ +u8 rpv[64 ]; +u8 padding1[0x400 ]; + }; +union {/* 0x800 */ +struct { +u64 vmid; +u64 rtt_base; +s64 rtt_level_start; +u64 rtt_num_start; + }; +u8 padding2[0x800 ]; + }; +}; + +/* +* The number of GPRs (starting from X0) that are +* configured by the host when a REC is created. +*/ +#define REC_CREATE_NR_GPRS 8 + +#define REC_PARAMS_FLAG_RUNNABLE BIT_ULL(0) + +#define REC_PARAMS_AUX_GRANULES 16 + +struct rec_params { +union {/* 0x0 */ +u64 flags; +u8 padding0[0x100 ]; + }; +union {/* 0x100 */ +u64 mpidr; +u8 padding1[0x100 ]; + }; +union {/* 0x200 */ +u64 pc; +u8 padding2[0x100 ]; + }; +union {/* 0x300 */ +u64 gprs[REC_CREATE_NR_GPRS ]; +u8 padding3[0x500 ]; + }; +union {/* 0x800 */ +struct { +u64 num_rec_aux; +u64 aux[REC_PARAMS_AUX_GRANULES ]; + }; +u8 padding4[0x800 ]; + }; +}; + +#define REC_ENTER_FLAG_EMULATED_MMIO BIT(0) +#define REC_ENTER_FLAG_INJECT_SEA BIT(1) +#define REC_ENTER_FLAG_TRAP_WFI BIT(2) +#define REC_ENTER_FLAG_TRAP_WFE BIT(3) +#define REC_ENTER_FLAG_RIPAS_RESPONSE BIT(4) + +#define REC_RUN_GPRS 31 +#define REC_MAX_GIC_NUM_LRS 16 + +#define RMI_PERMITTED_GICV3_HCR_BITS (ICH_HCR_EL2_UIE | \ + ICH_HCR_EL2_LRENPIE | \ + ICH_HCR_EL2_NPIE | \ + ICH_HCR_EL2_VGrp0EIE | \ + ICH_HCR_EL2_VGrp0DIE | \ + ICH_HCR_EL2_VGrp1EIE | \ + ICH_HCR_EL2_VGrp1DIE | \ + ICH_HCR_EL2_TDIR) + +struct rec_enter { +union {/* 0x000 */ +u64 flags; +u8 padding0[0x200 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding1[0x100 ]; + }; +union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; + }; +u8 padding2[0x100 ]; + }; +u8 padding3[0x400 ]; +}; + +#define RMI_EXIT_SYNC 0x00 +#define RMI_EXIT_IRQ 0x01 +#define RMI_EXIT_FIQ 0x02 +#define RMI_EXIT_PSCI 0x03 +#define RMI_EXIT_RIPAS_CHANGE 0x04 +#define RMI_EXIT_HOST_CALL 0x05 +#define RMI_EXIT_SERROR 0x06 + +struct rec_exit { +union {/* 0x000 */ +u8 exit_reason; +u8 padding0[0x100 ]; + }; +union {/* 0x100 */ +struct { +u64 esr; +u64 far; +u64 hpfar; + }; +u8 padding1[0x100 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding2[0x100 ]; + }; +union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; +u64 gicv3_misr; +u64 gicv3_vmcr; + }; +u8 padding3[0x100 ]; + }; +union {/* 0x400 */ +struct { +u64 cntp_ctl; +u64 cntp_cval; +u64 cntv_ctl; +u64 cntv_cval; + }; +u8 padding4[0x100 ]; + }; +union {/* 0x500 */ +struct { +u64 ripas_base; +u64 ripas_top; +u8 ripas_value; +u8 padding8[7 ]; + }; +u8 padding5[0x100 ]; + }; +union {/* 0x600 */ +u16 imm; +u8 padding6[0x100 ]; + }; +union {/* 0x700 */ +struct { +u8 pmu_ovf_status; + }; +u8 padding7[0x100 ]; + }; +}; + +struct rec_run { +struct rec_enter enter; +struct rec_exit exit; +}; + +#endif /* __ASM_RMI_SMC_H */
Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v10 03/43] arm64: RME: Add SMC definitions for calling the RMM
Quoted 48 lines · Steven Price
The RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.
The SMC definitions are based on DEN0137[1] version 1.0-rel0
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v9:
* Corrected size of 'ripas_value' in structrec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* AddedRMI_PERMITTED_GICV3_HCR_BITSto define which bits the RMM permits to be modified.
Changes since v6:
* RenamedREC_ENTER_xxxdefines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* RenamedSMI_RxI_CALLtoSMI_RMI_CALLsince the macro is only used for RMI calls.
* RenamedREC_GIC_NUM_LRStoREC_MAX_GIC_NUM_LRSsince 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.
* Renamerec_entry->rec_enterto match spec.
* Fix size ofpmu_ovf_statusto match spec.--- arch/arm64/include/asm/rmi_smc.h | 269 +++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_smc.harch/arm64/include/asm/rmi_smc.h new file +0
-0
[...]
arch/arm64/include/asm/rmi_smc.h line 179
Quoted 8 lines
arch/arm64/include/asm/rmi_smc.h new file +8
-0+#define RMI_PERMITTED_GICV3_HCR_BITS (ICH_HCR_EL2_UIE | \ + ICH_HCR_EL2_LRENPIE | \ + ICH_HCR_EL2_NPIE | \ + ICH_HCR_EL2_VGrp0EIE | \ + ICH_HCR_EL2_VGrp0DIE | \ + ICH_HCR_EL2_VGrp1EIE | \ + ICH_HCR_EL2_VGrp1DIE | \ + ICH_HCR_EL2_TDIR)
Why should KVM care about what bits the RMM wants to use? Also, why should KVM be forbidden to use the TALL0, TALL1 and TC bits? If interrupt delivery is the host's business, then the RMM has no business interfering with the GIC programming.
Quoted 40 lines
arch/arm64/include/asm/rmi_smc.h new file +40
-0+ +struct rec_enter { +union {/* 0x000 */ +u64 flags; +u8 padding0[0x200 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding1[0x100 ]; + }; +union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; + }; +u8 padding2[0x100 ]; + }; +u8 padding3[0x400 ]; +}; + +#define RMI_EXIT_SYNC 0x00 +#define RMI_EXIT_IRQ 0x01 +#define RMI_EXIT_FIQ 0x02 +#define RMI_EXIT_PSCI 0x03 +#define RMI_EXIT_RIPAS_CHANGE 0x04 +#define RMI_EXIT_HOST_CALL 0x05 +#define RMI_EXIT_SERROR 0x06 + +struct rec_exit { +union {/* 0x000 */ +u8 exit_reason; +u8 padding0[0x100 ]; + }; +union {/* 0x100 */ +struct { +u64 esr; +u64 far; +u64 hpfar; + }; +u8 padding1[0x100 ];
arch/arm64/include/asm/rmi_smc.h line 229
Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10
-0+ }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding2[0x100 ]; + }; +union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; +u64 gicv3_misr;
Why do we care about ICH_MISR_EL2? Surely we get everything in the registers themselves, right? I think this goes back to my question
above: why is the RMM getting in the way of ICH_*_EL2 accesses?
arch/arm64/include/asm/rmi_smc.h
Quoted 2 lines
arch/arm64/include/asm/rmi_smc.h new file +2
-0+u64 gicv3_vmcr; + };
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 03/43] arm64: RME: Add SMC definitions for calling the RMM
Hi Marc,
Quoted 53 lines · Marc Zyngier
Quoted 48 lines · Steven Price
The RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.
The SMC definitions are based on DEN0137[1] version 1.0-rel0
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v9:
* Corrected size of 'ripas_value' in structrec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* AddedRMI_PERMITTED_GICV3_HCR_BITSto define which bits the RMM permits to be modified.
Changes since v6:
* RenamedREC_ENTER_xxxdefines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* RenamedSMI_RxI_CALLtoSMI_RMI_CALLsince the macro is only used for RMI calls.
* RenamedREC_GIC_NUM_LRStoREC_MAX_GIC_NUM_LRSsince 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.
* Renamerec_entry->rec_enterto match spec.
* Fix size ofpmu_ovf_statusto match spec.--- arch/arm64/include/asm/rmi_smc.h | 269 +++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_smc.harch/arm64/include/asm/rmi_smc.h new file +0
-0[...]
Quoted 13 lines
arch/arm64/include/asm/rmi_smc.h line 179
Quoted 8 lines
arch/arm64/include/asm/rmi_smc.h new file +8
-0+#define RMI_PERMITTED_GICV3_HCR_BITS (ICH_HCR_EL2_UIE | \ + ICH_HCR_EL2_LRENPIE | \ + ICH_HCR_EL2_NPIE | \ + ICH_HCR_EL2_VGrp0EIE | \ + ICH_HCR_EL2_VGrp0DIE | \ + ICH_HCR_EL2_VGrp1EIE | \ + ICH_HCR_EL2_VGrp1DIE | \ + ICH_HCR_EL2_TDIR)Why should KVM care about what bits the RMM wants to use? Also, why should KVM be forbidden to use the TALL0, TALL1 and TC bits? If interrupt delivery is the host's business, then the RMM has no business interfering with the GIC programming.
The RMM receives the guest's GIC state in a field within the REC entry structure (enter.gicv3_hcr). The RMM spec states that the above is the list of fields that will be considered and that everything else must be 0[1]. So this is used to filter the configuration to make sure it's valid for the RMM.
In terms of TALL0/TALL1/TC bits: these control trapping to EL2, and when in a realm guest the RMM is EL2 - so it's up to the RMM to configure these bits appropriately as it is the RMM which will have to deal with the trap.
[1] RWVGFJ in the 1.0 spec from
https://developer.arm.com/documentation/den0137/latest
Quoted 46 lines
Quoted 46 lines
arch/arm64/include/asm/rmi_smc.h new file +46
-0+ +struct rec_enter { +union {/* 0x000 */ +u64 flags; +u8 padding0[0x200 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding1[0x100 ]; + }; +union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; + }; +u8 padding2[0x100 ]; + }; +u8 padding3[0x400 ]; +}; + +#define RMI_EXIT_SYNC 0x00 +#define RMI_EXIT_IRQ 0x01 +#define RMI_EXIT_FIQ 0x02 +#define RMI_EXIT_PSCI 0x03 +#define RMI_EXIT_RIPAS_CHANGE 0x04 +#define RMI_EXIT_HOST_CALL 0x05 +#define RMI_EXIT_SERROR 0x06 + +struct rec_exit { +union {/* 0x000 */ +u8 exit_reason; +u8 padding0[0x100 ]; + }; +union {/* 0x100 */ +struct { +u64 esr; +u64 far; +u64 hpfar; + }; +u8 padding1[0x100 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding2[0x100 ]; + }; +union {/* 0x300 */
Quoted 8 lines
arch/arm64/include/asm/rmi_smc.h line 229
Quoted 4 lines
arch/arm64/include/asm/rmi_smc.h new file +4
-0+struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; +u64 gicv3_misr;Why do we care about
ICH_MISR_EL2? Surely we get everything in the registers themselves, right? I think this goes back to my question
above: why is the RMM getting in the way of ICH_*_EL2 accesses?
As mentioned above, the state of the guest's GIC isn't passed through the CPU's registers, but instead using the rec_enter/rec_exit structures. So unlike a normal guest entry we don't set all the CPU's register state before entering, but instead hand over a shared data structure and the RMM is responsible for actually programming the registers on the CPU. Since many of the registers are (deliberately) unavailable to the host (e.g. all the GPRs) it makes some sense the RMM also handles the GIC registers save/restore.
Thanks,
Steve
Quoted 7 lines
arch/arm64/include/asm/rmi_smc.h
Quoted 2 lines
arch/arm64/include/asm/rmi_smc.h new file +2
-0+u64 gicv3_vmcr; + };Thanks,
M.
Marc Zyngier
maintainerRe: [PATCH v10 03/43] arm64: RME: Add SMC definitions for calling the RMM
Quoted 77 lines · Steven Price
Hi Marc,
Quoted 53 lines · Marc Zyngier
Quoted 48 lines · Steven Price
The RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.
The SMC definitions are based on DEN0137[1] version 1.0-rel0
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v9:
* Corrected size of 'ripas_value' in structrec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* AddedRMI_PERMITTED_GICV3_HCR_BITSto define which bits the RMM permits to be modified.
Changes since v6:
* RenamedREC_ENTER_xxxdefines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* RenamedSMI_RxI_CALLtoSMI_RMI_CALLsince the macro is only used for RMI calls.
* RenamedREC_GIC_NUM_LRStoREC_MAX_GIC_NUM_LRSsince 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.
* Renamerec_entry->rec_enterto match spec.
* Fix size ofpmu_ovf_statusto match spec.--- arch/arm64/include/asm/rmi_smc.h | 269 +++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_smc.harch/arm64/include/asm/rmi_smc.h new file +0
-0[...]
Quoted 13 lines
arch/arm64/include/asm/rmi_smc.h line 179
Quoted 8 lines
arch/arm64/include/asm/rmi_smc.h new file +8
-0+#define RMI_PERMITTED_GICV3_HCR_BITS (ICH_HCR_EL2_UIE | \ + ICH_HCR_EL2_LRENPIE | \ + ICH_HCR_EL2_NPIE | \ + ICH_HCR_EL2_VGrp0EIE | \ + ICH_HCR_EL2_VGrp0DIE | \ + ICH_HCR_EL2_VGrp1EIE | \ + ICH_HCR_EL2_VGrp1DIE | \ + ICH_HCR_EL2_TDIR)Why should KVM care about what bits the RMM wants to use? Also, why should KVM be forbidden to use the TALL0, TALL1 and TC bits? If interrupt delivery is the host's business, then the RMM has no business interfering with the GIC programming.
The RMM receives the guest's GIC state in a field within the REC entry structure (enter.gicv3_hcr). The RMM spec states that the above is the list of fields that will be considered and that everything else must be 0[1]. So this is used to filter the configuration to make sure it's valid for the RMM.
Quoted 4 lines
In terms of TALL0/TALL1/TC bits: these control trapping to EL2, and when in a realm guest the RMM is EL2 - so it's up to the RMM to configure these bits appropriately as it is the RMM which will have to deal with the trap.
And I claim this is *wrong*. Again, if the host is in charge of interrupt injection, then the RMM has absolutely no business is deciding what can or cannot be trapped. There is zero information exposed by these traps that the host is not already aware of.
Quoted 2 lines
[1] RWVGFJ in the 1.0 spec from
https://developer.arm.com/documentation/den0137/latest
Well, until someone explains what this is protecting against, I consider this as broken.
Quoted 10 lines
Quoted 9 lines
arch/arm64/include/asm/rmi_smc.h line 229
Quoted 5 lines
arch/arm64/include/asm/rmi_smc.h new file +5
-0+union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; +u64 gicv3_misr;Why do we care about
ICH_MISR_EL2? Surely we get everything in the registers themselves, right? I think this goes back to my question
above: why is the RMM getting in the way of ICH_*_EL2 accesses?
Quoted 8 lines
As mentioned above, the state of the guest's GIC isn't passed through the CPU's registers, but instead using the rec_enter/rec_exit structures. So unlike a normal guest entry we don't set all the CPU's register state before entering, but instead hand over a shared data structure and the RMM is responsible for actually programming the registers on the CPU. Since many of the registers are (deliberately) unavailable to the host (e.g. all the GPRs) it makes some sense the RMM also handles the GIC registers save/restore.
And I claim this is nonsense. There is nothing in these registers that the host doesn't need to know about, which is why they are basically copied over.
It all feels just wrong.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 03/43] arm64: RME: Add SMC definitions for calling the RMM
Quoted 89 lines · Marc Zyngier
Quoted 77 lines · Steven Price
Hi Marc,
Quoted 53 lines · Marc Zyngier
Quoted 48 lines · Steven Price
The RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.
The SMC definitions are based on DEN0137[1] version 1.0-rel0
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v9:
* Corrected size of 'ripas_value' in structrec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* AddedRMI_PERMITTED_GICV3_HCR_BITSto define which bits the RMM permits to be modified.
Changes since v6:
* RenamedREC_ENTER_xxxdefines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* RenamedSMI_RxI_CALLtoSMI_RMI_CALLsince the macro is only used for RMI calls.
* RenamedREC_GIC_NUM_LRStoREC_MAX_GIC_NUM_LRSsince 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.
* Renamerec_entry->rec_enterto match spec.
* Fix size ofpmu_ovf_statusto match spec.--- arch/arm64/include/asm/rmi_smc.h | 269 +++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_smc.harch/arm64/include/asm/rmi_smc.h new file +0
-0[...]
Quoted 13 lines
arch/arm64/include/asm/rmi_smc.h line 179
Quoted 8 lines
arch/arm64/include/asm/rmi_smc.h new file +8
-0+#define RMI_PERMITTED_GICV3_HCR_BITS (ICH_HCR_EL2_UIE | \ + ICH_HCR_EL2_LRENPIE | \ + ICH_HCR_EL2_NPIE | \ + ICH_HCR_EL2_VGrp0EIE | \ + ICH_HCR_EL2_VGrp0DIE | \ + ICH_HCR_EL2_VGrp1EIE | \ + ICH_HCR_EL2_VGrp1DIE | \ + ICH_HCR_EL2_TDIR)Why should KVM care about what bits the RMM wants to use? Also, why should KVM be forbidden to use the TALL0, TALL1 and TC bits? If interrupt delivery is the host's business, then the RMM has no business interfering with the GIC programming.
The RMM receives the guest's GIC state in a field within the REC entry structure (enter.gicv3_hcr). The RMM spec states that the above is the list of fields that will be considered and that everything else must be 0[1]. So this is used to filter the configuration to make sure it's valid for the RMM.
Quoted 4 lines
In terms of TALL0/TALL1/TC bits: these control trapping to EL2, and when in a realm guest the RMM is EL2 - so it's up to the RMM to configure these bits appropriately as it is the RMM which will have to deal with the trap.
And I claim this is *wrong*. Again, if the host is in charge of interrupt injection, then the RMM has absolutely no business is deciding what can or cannot be trapped. There is zero information exposed by these traps that the host is not already aware of.
Quoted 5 lines
Quoted 2 lines
[1] RWVGFJ in the 1.0 spec from
https://developer.arm.com/documentation/den0137/latestWell, until someone explains what this is protecting against, I consider this as broken.
I'm not sure I understand how you want this to work. Ultimately the realm guest entry is a bounce from NS-EL2 to EL3 to R-EL2 to R-EL1/0. So the RMM has to have some control over the trapping behaviour for its own protection. The current spec means that the RMM does not have to implement the trap handlers for TALL0/TALL1/TC and can simply force these bits to 0. Allowing the host to enable traps that the RMM isn't expecting will obviously end in problems.
If your argument is that because the NS host is emulating the GIC it needs to be able to do these traps, then that's something that can be fed back to the spec and hopefully improved. In that case the trap information would be provided in the rec_entry structure and on trap the RMM would return prepare information in the rec_exit structure. This could in theory be handled similar to an emulatable data abort with a new exit reason.
The other approach would be to push more GIC handling into the RMM such that these trap bits are not needed (i.e. there's no requirement to exit to the NS host to handle the trap, and the RMM can program them independently). I'm afraid I don't understand the GIC well enough to know how these traps are used and how feasible it is for the RMM to just "do the right thing" here.
Quoted 19 lines
Quoted 10 lines
Quoted 9 lines
arch/arm64/include/asm/rmi_smc.h line 229
Quoted 5 lines
arch/arm64/include/asm/rmi_smc.h new file +5
-0+union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; +u64 gicv3_misr;Why do we care about
ICH_MISR_EL2? Surely we get everything in the registers themselves, right? I think this goes back to my question
above: why is the RMM getting in the way of ICH_*_EL2 accesses?Quoted 8 lines
As mentioned above, the state of the guest's GIC isn't passed through the CPU's registers, but instead using the rec_enter/rec_exit structures. So unlike a normal guest entry we don't set all the CPU's register state before entering, but instead hand over a shared data structure and the RMM is responsible for actually programming the registers on the CPU. Since many of the registers are (deliberately) unavailable to the host (e.g. all the GPRs) it makes some sense the RMM also handles the GIC registers save/restore.
Quoted 3 lines
And I claim this is nonsense. There is nothing in these registers that the host doesn't need to know about, which is why they are basically copied over.
Well it's fairly obvious that the host (generally) doesn't need to know the general purpose registers. And it's fairly clear that confidential compute would be pretty pointless if the hypervisor leaked those registers. So I hope we agree that some architectural registers are going to have to be handled differently from a normal guest.
The GIC is unusual because it's (partly) emulated by the host. The configuration is also complex because during guest entry rather than just dropping down to EL1/0 we're actually performing an SMC to EL3 and world-switching. So I'm not sure to what extent programming the architectural registers in the normal world would work.
Quoted 1 line
It all feels just wrong.
I think fundamentally the confusing thing is there are two hypervisors pretending to be one. Both KVM and the RMM are providing part of the role of the hypervisor. It would "feel" neater for the RMM to take on more responsibility of the hypervisor role but that leads to more complexity in the RMM (whose simplicity is part of the value of CCA) and potentially less flexibility because you haven't got the functionality of KVM.
Thanks,
Steve
Suzuki K Poulose
reviewerRe: [PATCH v10 03/43] arm64: RME: Add SMC definitions for calling the RMM
Hi,
Quoted 96 lines · Steven Price
Quoted 89 lines · Marc Zyngier
Quoted 77 lines · Steven Price
Hi Marc,
Quoted 53 lines · Marc Zyngier
Quoted 48 lines · Steven Price
The RMM (Realm Management Monitor) provides functionality that can be accessed by SMC calls from the host.
The SMC definitions are based on DEN0137[1] version 1.0-rel0
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v9:
* Corrected size of 'ripas_value' in structrec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* AddedRMI_PERMITTED_GICV3_HCR_BITSto define which bits the RMM permits to be modified.
Changes since v6:
* RenamedREC_ENTER_xxxdefines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* RenamedSMI_RxI_CALLtoSMI_RMI_CALLsince the macro is only used for RMI calls.
* RenamedREC_GIC_NUM_LRStoREC_MAX_GIC_NUM_LRSsince 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.
* Renamerec_entry->rec_enterto match spec.
* Fix size ofpmu_ovf_statusto match spec.--- arch/arm64/include/asm/rmi_smc.h | 269 +++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+)create mode 100644
arch/arm64/include/asm/rmi_smc.harch/arm64/include/asm/rmi_smc.h new file +0
-0[...]
Quoted 13 lines
arch/arm64/include/asm/rmi_smc.h line 179
Quoted 8 lines
arch/arm64/include/asm/rmi_smc.h new file +8
-0+#define RMI_PERMITTED_GICV3_HCR_BITS (ICH_HCR_EL2_UIE | \ + ICH_HCR_EL2_LRENPIE | \ + ICH_HCR_EL2_NPIE | \ + ICH_HCR_EL2_VGrp0EIE | \ + ICH_HCR_EL2_VGrp0DIE | \ + ICH_HCR_EL2_VGrp1EIE | \ + ICH_HCR_EL2_VGrp1DIE | \ + ICH_HCR_EL2_TDIR)Why should KVM care about what bits the RMM wants to use? Also, why should KVM be forbidden to use the TALL0, TALL1 and TC bits? If interrupt delivery is the host's business, then the RMM has no business interfering with the GIC programming.
The RMM receives the guest's GIC state in a field within the REC entry structure (enter.gicv3_hcr). The RMM spec states that the above is the list of fields that will be considered and that everything else must be 0[1]. So this is used to filter the configuration to make sure it's valid for the RMM.
Quoted 4 lines
In terms of TALL0/TALL1/TC bits: these control trapping to EL2, and when in a realm guest the RMM is EL2 - so it's up to the RMM to configure these bits appropriately as it is the RMM which will have to deal with the trap.
And I claim this is *wrong*. Again, if the host is in charge of interrupt injection, then the RMM has absolutely no business is deciding what can or cannot be trapped. There is zero information exposed by these traps that the host is not already aware of.
Quoted 5 lines
Quoted 2 lines
[1] RWVGFJ in the 1.0 spec from
https://developer.arm.com/documentation/den0137/latestWell, until someone explains what this is protecting against, I consider this as broken.
Quoted 7 lines
I'm not sure I understand how you want this to work. Ultimately the realm guest entry is a bounce from NS-EL2 to EL3 to R-EL2 to R-EL1/0. So the RMM has to have some control over the trapping behaviour for its own protection. The current spec means that the RMM does not have to implement the trap handlers for TALL0/TALL1/TC and can simply force these bits to 0. Allowing the host to enable traps that the RMM isn't expecting will obviously end in problems.
The RMM design took a conservative approach of exposing bare minimum controls to the host to manage the VGIC, without increasing the complexity in the RMM. But if you think that the current set of controls are not sufficient for the Host to manage the Realm VGIC, like Steven mentions below, we could feed this back to the RMM spec and extend it in the future versions. I expect the new traps would be reported back as "sysreg" accesses (similar to the already exposed ICC_DIR, ICC_SGIxR).
Thanks
Suzuki
Quoted 63 lines
If your argument is that because the NS host is emulating the GIC it needs to be able to do these traps, then that's something that can be fed back to the spec and hopefully improved. In that case the trap information would be provided in the
rec_entrystructure and on trap the RMM would return prepare information in therec_exitstructure. This could in theory be handled similar to an emulatable data abort with a new exit reason.The other approach would be to push more GIC handling into the RMM such that these trap bits are not needed (i.e. there's no requirement to exit to the NS host to handle the trap, and the RMM can program them independently). I'm afraid I don't understand the GIC well enough to know how these traps are used and how feasible it is for the RMM to just "do the right thing" here.
Quoted 19 lines
Quoted 10 lines
Quoted 9 lines
arch/arm64/include/asm/rmi_smc.h line 229
Quoted 5 lines
arch/arm64/include/asm/rmi_smc.h new file +5
-0+union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_MAX_GIC_NUM_LRS ]; +u64 gicv3_misr;Why do we care about
ICH_MISR_EL2? Surely we get everything in the registers themselves, right? I think this goes back to my question
above: why is the RMM getting in the way of ICH_*_EL2 accesses?Quoted 8 lines
As mentioned above, the state of the guest's GIC isn't passed through the CPU's registers, but instead using the rec_enter/rec_exit structures. So unlike a normal guest entry we don't set all the CPU's register state before entering, but instead hand over a shared data structure and the RMM is responsible for actually programming the registers on the CPU. Since many of the registers are (deliberately) unavailable to the host (e.g. all the GPRs) it makes some sense the RMM also handles the GIC registers save/restore.
Quoted 3 lines
And I claim this is nonsense. There is nothing in these registers that the host doesn't need to know about, which is why they are basically copied over.
Well it's fairly obvious that the host (generally) doesn't need to know the general purpose registers. And it's fairly clear that confidential compute would be pretty pointless if the hypervisor leaked those registers. So I hope we agree that some architectural registers are going to have to be handled differently from a normal guest.
The GIC is unusual because it's (partly) emulated by the host. The configuration is also complex because during guest entry rather than just dropping down to EL1/0 we're actually performing an SMC to EL3 and world-switching. So I'm not sure to what extent programming the architectural registers in the normal world would work.
Quoted 1 line
It all feels just wrong.
I think fundamentally the confusing thing is there are two hypervisors pretending to be one. Both KVM and the RMM are providing part of the role of the hypervisor. It would "feel" neater for the RMM to take on more responsibility of the hypervisor role but that leads to more complexity in the RMM (whose simplicity is part of the value of CCA) and potentially less flexibility because you haven't got the functionality of KVM.
Thanks,
Steve
PATCH v10 4/43 arm64: RME: Add wrappers for RMI calls
In reply to Steven PriceThe wrappers make the call sites easier to read and deal with the boiler plate of handling the error codes from the RMM.
---
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()
--- arch/arm64/include/asm/rmi_cmds.h | 508 ++++++++++++++++++++++++++++++ 1 file changed, 508 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_cmds.h
arch/arm64/include/asm/rmi_cmds.h new file +508 -0
@@ -0,0 +1,508 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_RMI_CMDS_H +#define __ASM_RMI_CMDS_H + +#include <linux/arm-smccc.h> + +#include <asm/rmi_smc.h> + +struct rtt_entry { +unsigned long walk_level; +unsigned long desc; +int state; +int ripas; +}; + +/** +* rmi_data_create() - Create a data granule +* @rd: PA of the RD +* @data: PA of the target granule +* @ipa: IPA at which the granule will be mapped in the guest +* @src: PA of the source granule +* @flags: RMI_MEASURE_CONTENT if the contents should be measured +* +* Create a new data granule, copying contents from a non-secure granule. +* +* Return: RMI return code +*/ +static inline int rmi_data_create (unsigned long rd,unsigned long data, +unsigned long ipa,unsigned long src, +unsigned long flags) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE , rd, data, ipa, src, + flags, &res); + +return res.a0; +} + +/** +* rmi_data_create_unknown() - Create a data granule with unknown contents +* @rd: PA of the RD +* @data: PA of the target granule +* @ipa: IPA at which the granule will be mapped in the guest +* +* Return: RMI return code +*/ +static inline int rmi_data_create_unknown (unsigned long rd, +unsigned long data, +unsigned long ipa) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE_UNKNOWN , rd, data, ipa, &res); + +return res.a0; +} + +/** +* rmi_data_destroy() - Destroy a data granule +* @rd: PA of the RD +* @ipa: IPA at which the granule is mapped in the guest +* @data_out: PA of the granule which was destroyed +* @top_out: Top IPA of non-live RTT entries +* +* Unmap a protected IPA from stage 2, transitioning it to DESTROYED. +* The IPA cannot be used by the guest unless it is transitioned to RAM again +* by the realm guest. +* +* Return: RMI return code +*/ +static inline int rmi_data_destroy (unsigned long rd,unsigned long ipa, +unsigned long *data_out, +unsigned long *top_out) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_DESTROY , rd, ipa, &res); + +if (data_out) + *data_out = res.a1; +if (top_out) + *top_out = res.a2; + +return res.a0; +} + +/** +* rmi_features() - Read feature register +* @index: Feature register index +* @out: Feature register value is written to this pointer +* +* Return: RMI return code +*/ +static inline int rmi_features (unsigned long index,unsigned long *out) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_FEATURES , index, &res); + +if (out) + *out = res.a1; +return res.a0; +} + +/** +* rmi_granule_delegate() - Delegate a granule +* @phys: PA of the granule +* +* Delegate a granule for use by the realm world. +* +* Return: RMI return code +*/ +static inline int rmi_granule_delegate (unsigned long phys) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_GRANULE_DELEGATE , phys, &res); + +return res.a0; +} + +/** +* rmi_granule_undelegate() - Undelegate a granule +* @phys: PA of the granule +* +* Undelegate a granule to allow use by the normal world. Will fail if the +* granule is in use. +* +* Return: RMI return code +*/ +static inline int rmi_granule_undelegate (unsigned long phys) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_GRANULE_UNDELEGATE , phys, &res); + +return res.a0; +} + +/** +* rmi_psci_complete() - Complete pending PSCI command +* @calling_rec: PA of the calling REC +* @target_rec: PA of the target REC +* @status: Status of the PSCI request +* +* Completes a pending PSCI command which was called with an MPIDR argument, by +* providing the corresponding REC. +* +* Return: RMI return code +*/ +static inline int rmi_psci_complete (unsigned long calling_rec, +unsigned long target_rec, +unsigned long status) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_PSCI_COMPLETE , calling_rec, target_rec, + status, &res); + +return res.a0; +} + +/** +* rmi_realm_activate() - Active a realm +* @rd: PA of the RD +* +* Mark a realm as Active signalling that creation is complete and allowing +* execution of the realm. +* +* Return: RMI return code +*/ +static inline int rmi_realm_activate (unsigned long rd) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REALM_ACTIVATE , rd, &res); + +return res.a0; +} + +/** +* rmi_realm_create() - Create a realm +* @rd: PA of the RD +* @params: PA of realm parameters +* +* Create a new realm using the given parameters. +* +* Return: RMI return code +*/ +static inline int rmi_realm_create (unsigned long rd,unsigned long params) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REALM_CREATE , rd, params, &res); + +return res.a0; +} + +/** +* rmi_realm_destroy() - Destroy a realm +* @rd: PA of the RD +* +* Destroys a realm, all objects belonging to the realm must be destroyed first. +* +* Return: RMI return code +*/ +static inline int rmi_realm_destroy (unsigned long rd) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REALM_DESTROY , rd, &res); + +return res.a0; +} + +/** +* rmi_rec_aux_count() - Get number of auxiliary granules required +* @rd: PA of the RD +* @aux_count: Number of granules written to this pointer +* +* A REC may require extra auxiliary granules to be delegated for the RMM to +* store metadata (not visible to the normal world) in. This function provides +* the number of granules that are required. +* +* Return: RMI return code +*/ +static inline int rmi_rec_aux_count (unsigned long rd,unsigned long *aux_count) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REC_AUX_COUNT , rd, &res); + +if (aux_count) + *aux_count = res.a1; +return res.a0; +} + +/** +* rmi_rec_create() - Create a REC +* @rd: PA of the RD +* @rec: PA of the target REC +* @params: PA of REC parameters +* +* Create a REC using the parameters specified in the struct rec_params pointed +* to by @params. +* +* Return: RMI return code +*/ +static inline int rmi_rec_create (unsigned long rd,unsigned long rec, +unsigned long params) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REC_CREATE , rd, rec, params, &res); + +return res.a0; +} + +/** +* rmi_rec_destroy() - Destroy a REC +* @rec: PA of the target REC +* +* Destroys a REC. The REC must not be running. +* +* Return: RMI return code +*/ +static inline int rmi_rec_destroy (unsigned long rec) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REC_DESTROY , rec, &res); + +return res.a0; +} + +/** +* rmi_rec_enter() - Enter a REC +* @rec: PA of the target REC +* @run_ptr: PA of RecRun structure +* +* Starts (or continues) execution within a REC. +* +* Return: RMI return code +*/ +static inline int rmi_rec_enter (unsigned long rec,unsigned long run_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REC_ENTER , rec, run_ptr, &res); + +return res.a0; +} + +/** +* rmi_rtt_create() - Creates an RTT +* @rd: PA of the RD +* @rtt: PA of the target RTT +* @ipa: Base of the IPA range described by the RTT +* @level: Depth of the RTT within the tree +* +* Creates an RTT (Realm Translation Table) at the specified level for the +* translation of the specified address within the realm. +* +* Return: RMI return code +*/ +static inline int rmi_rtt_create (unsigned long rd,unsigned long rtt, +unsigned long ipa,long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_CREATE , rd, rtt, ipa, level, &res); + +return res.a0; +} + +/** +* rmi_rtt_destroy() - Destroy an RTT +* @rd: PA of the RD +* @ipa: Base of the IPA range described by the RTT +* @level: Depth of the RTT within the tree +* @out_rtt: Pointer to write the PA of the RTT which was destroyed +* @out_top: Pointer to write the top IPA of non-live RTT entries +* +* Destroys an RTT. The RTT must be non-live, i.e. none of the entries in the +* table are in ASSIGNED or TABLE state. +* +* Return: RMI return code. +*/ +static inline int rmi_rtt_destroy (unsigned long rd, +unsigned long ipa, +long level, +unsigned long *out_rtt, +unsigned long *out_top) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_DESTROY , rd, ipa, level, &res); + +if (out_rtt) + *out_rtt = res.a1; +if (out_top) + *out_top = res.a2; + +return res.a0; +} + +/** +* rmi_rtt_fold() - Fold an RTT +* @rd: PA of the RD +* @ipa: Base of the IPA range described by the RTT +* @level: Depth of the RTT within the tree +* @out_rtt: Pointer to write the PA of the RTT which was destroyed +* +* Folds an RTT. If all entries with the RTT are 'homogeneous' the RTT can be +* folded into the parent and the RTT destroyed. +* +* Return: RMI return code +*/ +static inline int rmi_rtt_fold (unsigned long rd,unsigned long ipa, +long level,unsigned long *out_rtt) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_FOLD , rd, ipa, level, &res); + +if (out_rtt) + *out_rtt = res.a1; + +return res.a0; +} + +/** +* rmi_rtt_init_ripas() - Set RIPAS for new realm +* @rd: PA of the RD +* @base: Base of target IPA region +* @top: Top of target IPA region +* @out_top: Top IPA of range whose RIPAS was modified +* +* Sets the RIPAS of a target IPA range to RAM, for a realm in the NEW state. +* +* Return: RMI return code +*/ +static inline int rmi_rtt_init_ripas (unsigned long rd,unsigned long base, +unsigned long top,unsigned long *out_top) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_INIT_RIPAS , rd, base, top, &res); + +if (out_top) + *out_top = res.a1; + +return res.a0; +} + +/** +* rmi_rtt_map_unprotected() - Map NS granules into a realm +* @rd: PA of the RD +* @ipa: Base IPA of the mapping +* @level: Depth within the RTT tree +* @desc: RTTE descriptor +* +* Create a mapping from an Unprotected IPA to a Non-secure PA. +* +* Return: RMI return code +*/ +static inline int rmi_rtt_map_unprotected (unsigned long rd, +unsigned long ipa, +long level, +unsigned long desc) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_MAP_UNPROTECTED , rd, ipa, level, + desc, &res); + +return res.a0; +} + +/** +* rmi_rtt_read_entry() - Read an RTTE +* @rd: PA of the RD +* @ipa: IPA for which to read the RTTE +* @level: RTT level at which to read the RTTE +* @rtt: Output structure describing the RTTE +* +* Reads a RTTE (Realm Translation Table Entry). +* +* Return: RMI return code +*/ +static inline int rmi_rtt_read_entry (unsigned long rd,unsigned long ipa, +long level,struct rtt_entry *rtt) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_READ_ENTRY , + rd, ipa, level + }; + +arm_smccc_1_2_invoke (®s, ®s); + + rtt->walk_level = regs.a1; + rtt->state = regs.a2 &0xFF ; + rtt->desc = regs.a3; + rtt->ripas = regs.a4 &0xFF ; + +return regs.a0; +} + +/** +* rmi_rtt_set_ripas() - Set RIPAS for an running realm +* @rd: PA of the RD +* @rec: PA of the REC making the request +* @base: Base of target IPA region +* @top: Top of target IPA region +* @out_top: Pointer to write top IPA of range whose RIPAS was modified +* +* Completes a request made by the realm to change the RIPAS of a target IPA +* range. +* +* Return: RMI return code +*/ +static inline int rmi_rtt_set_ripas (unsigned long rd,unsigned long rec, +unsigned long base,unsigned long top, +unsigned long *out_top) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_SET_RIPAS , rd, rec, base, top, &res); + +if (out_top) + *out_top = res.a1; + +return res.a0; +} + +/** +* rmi_rtt_unmap_unprotected() - Remove a NS mapping +* @rd: PA of the RD +* @ipa: Base IPA of the mapping +* @level: Depth within the RTT tree +* @out_top: Pointer to write top IPA of non-live RTT entries +* +* Removes a mapping at an Unprotected IPA. +* +* Return: RMI return code +*/ +static inline int rmi_rtt_unmap_unprotected (unsigned long rd, +unsigned long ipa, +long level, +unsigned long *out_top) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_UNMAP_UNPROTECTED , rd, ipa, + level, &res); + +if (out_top) + *out_top = res.a1; + +return res.a0; +} + +#endif /* __ASM_RMI_CMDS_H */
Signature · 1 line
2.43.0
PATCH v10 5/43 arm64: RME: Check for RME support at KVM init
In reply to Steven PriceQuery the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.
Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.
Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.
---
Changes since 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_emulate.h | 18 +++++++++ arch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/include/asm/kvm_rme.h | 56 ++++++++++++++++++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++ arch/arm64/kvm/rme.c | 56 ++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_rme.h create mode 100644 arch/arm64/kvm/rme.c
arch/arm64/include/asm/kvm_emulate.h +18 -0
@@ -674,4 +674,22 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) vcpu->arch.hcrx_el2 |= HCRX_EL2_SCTLR2En; } } + +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available) && kvm) +return kvm->arch.is_realm; +return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -27,6 +27,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #include <asm/vncr_mapping.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -404,6 +405,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +bool is_realm; +struct realm realm; };struct kvm_vcpu_fault_info {
arch/arm64/include/asm/kvm_rme.h new file +56 -0
56 lines not on this page · raw message
arch/arm64/include/asm/virt.h +1 -0
@@ -82,6 +82,7 @@ void __hyp_reset_vectors(void);bool is_kvm_arm_initialised (void );DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DECLARE_STATIC_KEY_FALSE (kvm_rme_is_available);static inline bool is_pkvm_initialized (void ) {
arch/arm64/kvm/Makefile +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 rme.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
@@ -40,6 +40,7 @@#include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_ptrauth.h> +#include <asm/kvm_rme.h> #include <asm/sections.h> #include <kvm/arm_hypercalls.h>
@@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {static enum kvm_wfx_trap_policy kvm_wfi_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ;static enum kvm_wfx_trap_policy kvm_wfe_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ; +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); + DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
@@ -2836,6 +2839,8 @@ static __init int kvm_arm_init(void) in_hyp_mode =is_kernel_in_hyp_mode (); +kvm_init_rme (); +if (cpus_have_final_cap (ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE ) ||cpus_have_final_cap (ARM64_WORKAROUND_1508412 )) kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
arch/arm64/kvm/rme.c new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v10 05/43] arm64: RME: Check for RME support at KVM init
Quoted 36 lines · Steven Price
Query the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.
Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.
Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v8:
* No need to guardkvm_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 fromkvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.--- arch/arm64/include/asm/kvm_emulate.h | 18 +++++++++ arch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/include/asm/kvm_rme.h | 56 ++++++++++++++++++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++ arch/arm64/kvm/rme.c | 56 ++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 1 deletion(-)create mode 100644
arch/arm64/include/asm/kvm_rme.hcreate mode 100644arch/arm64/kvm/rme.c
[...]
arch/arm64/kvm/arm.c line 45
Quoted 11 lines
arch/arm64/kvm/arm.c +1
-0@@ -40,6 +40,7 @@#include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_ptrauth.h> +#include <asm/kvm_rme.h> #include <asm/sections.h>
Nit: The header file <asm/kvm_rme.h> has been included to <asm/kvm_host.h> and <linux/kvm_host.h>, which has been included to arm.c. So this explicit inclusion can be dropped.
Quoted 64 lines
arch/arm64/kvm/arm.c +4
-0#include <kvm/arm_hypercalls.h> @@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {static enum kvm_wfx_trap_policy kvm_wfi_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ;static enum kvm_wfx_trap_policy kvm_wfe_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ; +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); + DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);@@ -2836,6 +2839,8 @@ static __init int kvm_arm_init(void) in_hyp_mode =is_kernel_in_hyp_mode (); +kvm_init_rme (); +if (cpus_have_final_cap (ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE ) ||cpus_have_final_cap (ARM64_WORKAROUND_1508412 )) kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \arch/arm64/kvm/rme.c new file +39
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#include <linux/kvm_host.h> + +#include <asm/rmi_cmds.h> +#include <asm/virt.h> + +static int rmi_check_version (void ) +{ +struct arm_smccc_res res; +unsigned short version_major, version_minor; +unsigned long host_version =RMI_ABI_VERSION (RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); + +arm_smccc_1_1_invoke (SMC_RMI_VERSION , host_version, &res); + +if (res.a0 ==SMCCC_RET_NOT_SUPPORTED ) +return -ENXIO ; + + version_major =RMI_ABI_VERSION_GET_MAJOR (res.a1); + version_minor =RMI_ABI_VERSION_GET_MINOR (res.a1); + +if (res.a0 !=RMI_SUCCESS ) { +unsigned short high_version_major, high_version_minor; + + high_version_major =RMI_ABI_VERSION_GET_MAJOR (res.a2); + high_version_minor =RMI_ABI_VERSION_GET_MINOR (res.a2); + +kvm_err ("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n" , + version_major, version_minor, + high_version_major, high_version_minor, +RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); +return -ENXIO ; + } +
arch/arm64/kvm/rme.c line 49
Quoted 10 lines
arch/arm64/kvm/rme.c new file +10
-0+kvm_info ("RMI ABI version %d.%d\n" , version_major, version_minor); + +return 0 ; +} + +void kvm_init_rme (void ) +{ +if (PAGE_SIZE !=SZ_4K ) +/* Only 4k page size on the host is supported */ +return ;
Nit: The comment can be moved before the check, something like below. Otherwise, {} is needed here.
/* Only 4kB page size is supported */ if (PAGE_SIZE != SZ_4K) return;
arch/arm64/kvm/rme.c line 53
Quoted 4 lines
arch/arm64/kvm/rme.c new file +4
-0+ +if (rmi_check_version ()) +/* Continue without realm support */ +return ;
Nit: same as above.
arch/arm64/kvm/rme.c line 56
Quoted 3 lines
arch/arm64/kvm/rme.c new file +3
-0+ +/* Future patch will enable static branch kvm_rme_is_available */ +}
Thanks,
Gavin
Re: [PATCH v10 05/43] arm64: RME: Check for RME support at KVM init
Quoted 49 lines · Gavin Shan
Quoted 36 lines · Steven Price
Query the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.
Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.
Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v8:
* No need to guardkvm_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 fromkvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.
---
arch/arm64/include/asm/kvm_emulate.h| 18 +++++++++ arch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/include/asm/kvm_rme.h | 56 ++++++++++++++++++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++ arch/arm64/kvm/rme.c | 56 ++++++++++++++++++++++++++++7 files changed, 141 insertions(+), 1 deletion(-)create mode 100644
arch/arm64/include/asm/kvm_rme.hcreate mode 100644arch/arm64/kvm/rme.c[...]
Quoted 9 lines
arch/arm64/kvm/arm.c +1
-0@@ -40,6 +40,7 @@ #include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_ptrauth.h> +#include <asm/kvm_rme.h>
Quoted 8 lines
Quoted 2 lines
arch/arm64/kvm/arm.c +0
-0#include <asm/sections.h>Nit: The header file <
asm/kvm_rme.h> has been included to <asm/kvm_host.h> and
<linux/kvm_host.h>, which has been included toarm.c. So this explicit inclusion
can be dropped.
While it's true that it could be dropped because of the indirect include, generally it's better to explicitly include a header file when you are using the definitions from it. That way the code can be refactored (e.g. if asm/kvm_host.h is changed to no longer needs the header it can then safely drop the header include).
We have a similar situation with asm/kvm_asm.h being included both here and in asm/kvm_host.h (and probably many others, this was just the first I spotted).
Quoted 83 lines
Quoted 64 lines
#include <
kvm/arm_hypercalls.h>arch/arm64/kvm/arm.c +0
-0@@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {static enum
kvm_wfx_trap_policykvm_wfi_trap_policy__read_mostly=KVM_WFX_NOTRAP_SINGLE_TASK;
static enumkvm_wfx_trap_policykvm_wfe_trap_policy__read_mostly=KVM_WFX_NOTRAP_SINGLE_TASK; +DEFINE_STATIC_KEY_FALSE(kvm_rme_is_available);
+
DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);arch/arm64/kvm/arm.c +1
-0@@ -2836,6 +2839,8 @@ static __init int kvm_arm_init(void) in_hyp_mode = is_kernel_in_hyp_mode(); + kvm_init_rme(); +if (
cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE)||cpus_have_final_cap(ARM64_WORKAROUND_1508412)) kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \arch/arm64/kvm/rme.c new file +32
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#include <linux/kvm_host.h> + +#include <asm/rmi_cmds.h> +#include <asm/virt.h> + +static int rmi_check_version (void ) +{ + struct arm_smccc_res res; + unsigned short version_major, version_minor; + unsigned long host_version = RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION, + RMI_ABI_MINOR_VERSION); + + arm_smccc_1_1_invoke(SMC_RMI_VERSION, host_version, &res); + + if (res.a0 == SMCCC_RET_NOT_SUPPORTED) + return -ENXIO; + + version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1); + version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1); + + if (res.a0 != RMI_SUCCESS) { + unsigned short high_version_major, high_version_minor; + + high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2); + high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2); + + kvm_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n",
+version_major,version_minor,
+high_version_major,high_version_minor, +RMI_ABI_MAJOR_VERSION,
+RMI_ABI_MINOR_VERSION);
+ return-ENXIO;
+ }arch/arm64/kvm/rme.c new file +1
-0+arch/arm64/kvm/rme.c line 49
Quoted 10 lines
arch/arm64/kvm/rme.c new file +10
-0+ kvm_info("RMI ABI version %d.%d\n", version_major, version_minor); + + return 0; +} + +void kvm_init_rme (void ) +{ + if (PAGE_SIZE != SZ_4K) + /* Only 4k page size on the host is supported */ + return;Nit: The comment can be moved before the check, something like below. Otherwise,
{} is needed here./* Only 4kB page size is supported */
if (PAGE_SIZE!=SZ_4K)
return;
Quoted 6 lines
Quoted 4 lines
+
+ if (rmi_check_version())
+ /* Continue without realm support */
+ return;Nit: same as above.
Fair enough, I'm never sure what's best here - the comments are for the error path "Continue without realm support [if we can't agree on a version]", but it's a single statement so braces aren't needed. Still the first instance can easily be hoisted above without losing its meaning, and the second can be extended to clarify.
Thanks,
Steve
Quoted 7 lines
arch/arm64/kvm/rme.c line 56
Quoted 3 lines
arch/arm64/kvm/rme.c new file +3
-0+ + /* Future patch will enable static branch kvm_rme_is_available */ +}Thanks,
Gavin
Marc Zyngier
maintainerRe: [PATCH v10 05/43] arm64: RME: Check for RME support at KVM init
In reply to Steven PriceQuoted 42 lines · Steven Price
Query the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.
Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.
Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v8:
* No need to guardkvm_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 fromkvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.--- arch/arm64/include/asm/kvm_emulate.h | 18 +++++++++ arch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/include/asm/kvm_rme.h | 56 ++++++++++++++++++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++ arch/arm64/kvm/rme.c | 56 ++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_rme.h create mode 100644 arch/arm64/kvm/rme.carch/arm64/include/asm/kvm_emulate.h +0
-0
arch/arm64/include/asm/kvm_emulate.h static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) · line 680
Quoted 7 lines
arch/arm64/include/asm/kvm_emulate.h +4
-0@@ -674,4 +674,22 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) vcpu->arch.hcrx_el2 |= HCRX_EL2_SCTLR2En; } } + +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available) && kvm)
Under what circumstances would you call this with a NULL pointer?
Quoted 28 lines
arch/arm64/include/asm/kvm_emulate.h +14
-0+return kvm->arch.is_realm; + return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_host.h +1
-0@@ -27,6 +27,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #include <asm/vncr_mapping.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 410
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -404,6 +405,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +bool is_realm; +struct realm realm;
Given that pkvm and CCA are pretty much exclusive, I don't think we need to store both states separately. Make those a union.
Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +0
-0};struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_rme.h new file +0
-0
arch/arm64/include/asm/kvm_rme.h line 7
Quoted 7 lines
arch/arm64/include/asm/kvm_rme.h new file +7
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_KVM_RME_H +#define __ASM_KVM_RME_H
None of that is about RME. This is about CCA, which is purely a SW construct, and not a CPU architecture feature.
So 's/rme/cca/' everywhere that describe something that is not a direct effect of FEAT_RME being implemented on the CPU, but instead something that is CCA-specific.
Quoted 54 lines
arch/arm64/include/asm/kvm_rme.h new file +49
-0+ +/** +* enum realm_state - State of a Realm +*/ +enum realm_state { +/** +* @REALM_STATE_NONE: +* Realm has not yet been created. rmi_realm_create() may be +* called to create the realm. +*/ +REALM_STATE_NONE , +/** +* @REALM_STATE_NEW: +* Realm is under construction, not eligible for execution. Pages +* may be populated with rmi_data_create(). +*/ +REALM_STATE_NEW , +/** +* @REALM_STATE_ACTIVE: +* Realm has been created and is eligible for execution with +* rmi_rec_enter(). Pages may no longer be populated with +* rmi_data_create(). +*/ +REALM_STATE_ACTIVE , +/** +* @REALM_STATE_DYING: +* Realm is in the process of being destroyed or has already been +* destroyed. +*/ +REALM_STATE_DYING , +/** +* @REALM_STATE_DEAD: +* Realm has been destroyed. +*/ +REALM_STATE_DEAD +}; + +/** +* struct realm - Additional per VM data for a Realm +* +* @state: The lifetime state machine for the realm +*/ +struct realm { +enum realm_state state; +}; + +void kvm_init_rme (void ); + +#endif /* __ASM_KVM_RME_H */ arch/arm64/include/asm/virt.h +0
-0
arch/arm64/include/asm/virt.h void __hyp_reset_vectors(void); · line 85
Quoted 4 lines
arch/arm64/include/asm/virt.h +1
-0@@ -82,6 +82,7 @@ void __hyp_reset_vectors(void);bool is_kvm_arm_initialised (void );DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DECLARE_STATIC_KEY_FALSE (kvm_rme_is_available);
Same thing about RME.
Quoted 60 lines
arch/arm64/include/asm/virt.h +0
-0static inline bool is_pkvm_initialized (void ) {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 rme.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@@ -40,6 +40,7 @@#include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_ptrauth.h> +#include <asm/kvm_rme.h> #include <asm/sections.h> #include <kvm/arm_hypercalls.h> @@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {static enum kvm_wfx_trap_policy kvm_wfi_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ;static enum kvm_wfx_trap_policy kvm_wfe_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ; +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); + DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);@@ -2836,6 +2839,8 @@ static __init int kvm_arm_init(void) in_hyp_mode =is_kernel_in_hyp_mode (); +kvm_init_rme (); +if (cpus_have_final_cap (ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE ) ||cpus_have_final_cap (ARM64_WORKAROUND_1508412 )) kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \arch/arm64/kvm/rme.c new file +8
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#include <linux/kvm_host.h> + +#include <asm/rmi_cmds.h>
arch/arm64/kvm/rme.c line 18
Quoted 10 lines
arch/arm64/kvm/rme.c new file +10
-0+#include <asm/virt.h> + +static int rmi_check_version (void ) +{ +struct arm_smccc_res res; +unsigned short version_major, version_minor; +unsigned long host_version =RMI_ABI_VERSION (RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); + +arm_smccc_1_1_invoke (SMC_RMI_VERSION , host_version, &res);
Shouldn't you first check that RME is actually available, by looking at ID_AA64PFR0_EL1.RME?
Quoted 21 lines
arch/arm64/kvm/rme.c new file +21
-0+ +if (res.a0 ==SMCCC_RET_NOT_SUPPORTED ) +return -ENXIO ; + + version_major =RMI_ABI_VERSION_GET_MAJOR (res.a1); + version_minor =RMI_ABI_VERSION_GET_MINOR (res.a1); + +if (res.a0 !=RMI_SUCCESS ) { +unsigned short high_version_major, high_version_minor; + + high_version_major =RMI_ABI_VERSION_GET_MAJOR (res.a2); + high_version_minor =RMI_ABI_VERSION_GET_MINOR (res.a2); + +kvm_err ("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n" , + version_major, version_minor, + high_version_major, high_version_minor, +RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); +return -ENXIO ; + } +
arch/arm64/kvm/rme.c line 49
Quoted 10 lines
arch/arm64/kvm/rme.c new file +10
-0+kvm_info ("RMI ABI version %d.%d\n" , version_major, version_minor); + +return 0 ; +} + +void kvm_init_rme (void ) +{ +if (PAGE_SIZE !=SZ_4K ) +/* Only 4k page size on the host is supported */ +return ;
Move the comment above the check (same thing below).
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 05/43] arm64: RME: Check for RME support at KVM init
Quoted 39 lines · Marc Zyngier
Quoted 37 lines · Steven Price
Query the RMI version number and check if it is a compatible version. A static key is also provided to signal that a supported RMM is available.
Functions are provided to query if a VM or VCPU is a realm (or rec) which currently will always return false.
Later patches make use of struct realm and the states as the ioctls interfaces are added to support realm and REC creation and destruction.
Reviewed-by Gavin Shan <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>---
Changes since v8:
* No need to guardkvm_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 fromkvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.--- arch/arm64/include/asm/kvm_emulate.h | 18 +++++++++ arch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/include/asm/kvm_rme.h | 56 ++++++++++++++++++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++ arch/arm64/kvm/rme.c | 56 ++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_rme.h create mode 100644 arch/arm64/kvm/rme.c
Quoted 14 lines
arch/arm64/include/asm/kvm_emulate.h static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) · line 680
Quoted 12 lines
arch/arm64/include/asm/kvm_emulate.h +4
-0@@ -674,4 +674,22 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) vcpu->arch.hcrx_el2 |= HCRX_EL2_SCTLR2En; } } + +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available) && kvm)Under what circumstances would you call this with a NULL pointer?
kvm_vm_ioctl_check_extension() is the culprit. I guess this could be handled with an equivalent to kvm_pvm_ext_allowed().
Quoted 26 lines
Quoted 26 lines
arch/arm64/include/asm/kvm_emulate.h +14
-0+return kvm->arch.is_realm; + return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_host.h +1
-0@@ -27,6 +27,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #include <asm/vncr_mapping.h>
Quoted 11 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 410
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +3
-0#define __KVM_HAVE_ARCH_INTC_INITIALIZED@@ -404,6 +405,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +bool is_realm; +struct realm realm;Given that pkvm and CCA are pretty much exclusive, I don't think we need to store both states separately. Make those a union.
Ack
Quoted 20 lines
Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +0
-0};struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_rme.h new file +0
-0arch/arm64/include/asm/kvm_rme.h line 7
Quoted 7 lines
arch/arm64/include/asm/kvm_rme.h new file +7
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_KVM_RME_H +#define __ASM_KVM_RME_HNone of that is about RME. This is about CCA, which is purely a SW construct, and not a CPU architecture feature.
Quoted 3 lines
So 's/rme/cca/' everywhere that describe something that is not a direct effect of
FEAT_RMEbeing implemented on the CPU, but instead something that is CCA-specific.
Ok, it's a lot of churn but you've got a good point.
Quoted 126 lines
Quoted 54 lines
arch/arm64/include/asm/kvm_rme.h new file +49
-0+ +/** +* enum realm_state - State of a Realm +*/ +enum realm_state { +/** +* @REALM_STATE_NONE: +* Realm has not yet been created. rmi_realm_create() may be +* called to create the realm. +*/ +REALM_STATE_NONE , +/** +* @REALM_STATE_NEW: +* Realm is under construction, not eligible for execution. Pages +* may be populated with rmi_data_create(). +*/ +REALM_STATE_NEW , +/** +* @REALM_STATE_ACTIVE: +* Realm has been created and is eligible for execution with +* rmi_rec_enter(). Pages may no longer be populated with +* rmi_data_create(). +*/ +REALM_STATE_ACTIVE , +/** +* @REALM_STATE_DYING: +* Realm is in the process of being destroyed or has already been +* destroyed. +*/ +REALM_STATE_DYING , +/** +* @REALM_STATE_DEAD: +* Realm has been destroyed. +*/ +REALM_STATE_DEAD +}; + +/** +* struct realm - Additional per VM data for a Realm +* +* @state: The lifetime state machine for the realm +*/ +struct realm { +enum realm_state state; +}; + +void kvm_init_rme (void ); + +#endif /* __ASM_KVM_RME_H */ arch/arm64/include/asm/virt.h +0
-0arch/arm64/include/asm/virt.h void __hyp_reset_vectors(void); · line 85
Quoted 4 lines
arch/arm64/include/asm/virt.h +1
-0@@ -82,6 +82,7 @@ void __hyp_reset_vectors(void);bool is_kvm_arm_initialised (void );DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DECLARE_STATIC_KEY_FALSE (kvm_rme_is_available);Same thing about RME.
Quoted 65 lines
arch/arm64/include/asm/virt.h +0
-0static inline bool is_pkvm_initialized (void ) {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 rme.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@@ -40,6 +40,7 @@#include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_ptrauth.h> +#include <asm/kvm_rme.h> #include <asm/sections.h> #include <kvm/arm_hypercalls.h> @@ -59,6 +60,8 @@ enum kvm_wfx_trap_policy {static enum kvm_wfx_trap_policy kvm_wfi_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ;static enum kvm_wfx_trap_policy kvm_wfe_trap_policy__read_mostly =KVM_WFX_NOTRAP_SINGLE_TASK ; +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); + DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);@@ -2836,6 +2839,8 @@ static __init int kvm_arm_init(void) in_hyp_mode =is_kernel_in_hyp_mode (); +kvm_init_rme (); +if (cpus_have_final_cap (ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE ) ||cpus_have_final_cap (ARM64_WORKAROUND_1508412 )) kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \arch/arm64/kvm/rme.c new file +13
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#include <linux/kvm_host.h> + +#include <asm/rmi_cmds.h> +#include <asm/virt.h> + +static int rmi_check_version (void ) +{ +struct arm_smccc_res res;
Quoted 8 lines
arch/arm64/kvm/rme.c line 18
Quoted 5 lines
arch/arm64/kvm/rme.c new file +5
-0+unsigned short version_major, version_minor; +unsigned long host_version =RMI_ABI_VERSION (RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); + +arm_smccc_1_1_invoke (SMC_RMI_VERSION , host_version, &res);Shouldn't you first check that RME is actually available, by looking at ID_AA64PFR0_EL1.RME?
Well, you made a good point above that this isn't RME, it's CCA. And I guess there's a possible world where the CCA interface could be supported with something other than FEAT_RME (FEAT_RME2 maybe?) so I'm not sure it necessarily a good idea to pin this on a CPU feature bit.
Ultimately what we want to know is whether the firmware thinks it can supply us with the CCA interface and we don't really care how it achieves it.
Quoted 25 lines
Quoted 25 lines
arch/arm64/kvm/rme.c new file +25
-0+ +if (res.a0 ==SMCCC_RET_NOT_SUPPORTED ) +return -ENXIO ; + + version_major =RMI_ABI_VERSION_GET_MAJOR (res.a1); + version_minor =RMI_ABI_VERSION_GET_MINOR (res.a1); + +if (res.a0 !=RMI_SUCCESS ) { +unsigned short high_version_major, high_version_minor; + + high_version_major =RMI_ABI_VERSION_GET_MAJOR (res.a2); + high_version_minor =RMI_ABI_VERSION_GET_MINOR (res.a2); + +kvm_err ("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n" , + version_major, version_minor, + high_version_major, high_version_minor, +RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); +return -ENXIO ; + } + +kvm_info ("RMI ABI version %d.%d\n" , version_major, version_minor); + +return 0 ; +}
Quoted 8 lines
arch/arm64/kvm/rme.c line 49
Quoted 6 lines
arch/arm64/kvm/rme.c new file +6
-0+ +void kvm_init_rme (void ) +{ +if (PAGE_SIZE !=SZ_4K ) +/* Only 4k page size on the host is supported */ +return ;Move the comment above the check (same thing below).
Ack.
Thanks,
Steve
Marc Zyngier
maintainerRe: [PATCH v10 05/43] arm64: RME: Check for RME support at KVM init
Quoted 13 lines · Steven Price
Quoted 12 lines
arch/arm64/kvm/rme.c line 18
Quoted 9 lines
arch/arm64/kvm/rme.c new file +8
-0+static int rmi_check_version (void ) +{ +struct arm_smccc_res res; +unsigned short version_major, version_minor; +unsigned long host_version =RMI_ABI_VERSION (RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); + +arm_smccc_1_1_invoke (SMC_RMI_VERSION , host_version, &res);Shouldn't you first check that RME is actually available, by looking at ID_AA64PFR0_EL1.RME?
Quoted 5 lines
Well, you made a good point above that this isn't RME, it's CCA. And I guess there's a possible world where the CCA interface could be supported with something other than
FEAT_RME(FEAT_RME2maybe?) so I'm not sure it necessarily a good idea to pin this on a CPU feature bit.
But you cannot have CCA without RME. You cannot have CCA with GICv3. And my point was more that RME could be used by something other than CCA - I certainly don't anticipate someone else adopting the CCA interface for anything...
Quoted 3 lines
Ultimately what we want to know is whether the firmware thinks it can supply us with the CCA interface and we don't really care how it achieves it.
I disagree. You rely on specific feature sets to be available (hell, everything is baked around GICv3... GICv5 anyone?).
For this sort of stuff, you absolutely need to know what you are running on, not what some broken firmware tries to pretend it is.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 05/43] arm64: RME: Check for RME support at KVM init
Quoted 30 lines · Marc Zyngier
Quoted 13 lines · Steven Price
Quoted 12 lines
arch/arm64/kvm/rme.c line 18
Quoted 9 lines
arch/arm64/kvm/rme.c new file +8
-0+static int rmi_check_version (void ) +{ +struct arm_smccc_res res; +unsigned short version_major, version_minor; +unsigned long host_version =RMI_ABI_VERSION (RMI_ABI_MAJOR_VERSION , +RMI_ABI_MINOR_VERSION ); + +arm_smccc_1_1_invoke (SMC_RMI_VERSION , host_version, &res);Shouldn't you first check that RME is actually available, by looking at ID_AA64PFR0_EL1.RME?
Quoted 5 lines
Well, you made a good point above that this isn't RME, it's CCA. And I guess there's a possible world where the CCA interface could be supported with something other than
FEAT_RME(FEAT_RME2maybe?) so I'm not sure it necessarily a good idea to pin this on a CPU feature bit.But you cannot have CCA without RME. You cannot have CCA with GICv3. And my point was more that RME could be used by something other than CCA - I certainly don't anticipate someone else adopting the CCA interface for anything...
Quoted 3 lines
Ultimately what we want to know is whether the firmware thinks it can supply us with the CCA interface and we don't really care how it achieves it.
Quoted 5 lines
I disagree. You rely on specific feature sets to be available (hell, everything is baked around GICv3... GICv5 anyone?).
For this sort of stuff, you absolutely need to know what you are running on, not what some broken firmware tries to pretend it is.
Well I don't agree, but equally I think the chances of the RMM interface existing without RME is nil, so I guess the extra check doesn't really matter. So I'll add it. In the (highly) unlikely event that it causes a problem then it would be easy enough to remove.
Thanks,
Steve
PATCH v10 6/43 arm64: RME: Define the user ABI
In reply to Steven PriceThere is one (multiplexed) CAP which can be used to create, populate and then activate the realm.
---
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 | 71 +++++++++++++++++++++++++++++++ arch/arm64/include/uapi/asm/kvm.h | 49 +++++++++++++++++++++ include/uapi/linux/kvm.h | 10 +++++ 3 files changed, 130 insertions(+)
Documentation/virt/kvm/api.rst +71 -0
@@ -3549,6 +3549,11 @@ Possible features: Depends on KVM_CAP_ARM_EL2_E2H0. KVM_ARM_VCPU_HAS_EL2 must also be set. + - KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this + VCPU. This must be specified on all VCPUs created in a Realm VM. + Depends on KVM_CAP_ARM_RME. + Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC). + 4.83 KVM_ARM_PREFERRED_TARGET -----------------------------
@@ -5122,6 +5127,7 @@ Recognised values for feature: ===== =========================================== arm64 KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE) + arm64 KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME) ===== =========================================== Finalizes the configuration of the specified vcpu feature.
@@ -6476,6 +6482,30 @@ the capability to be present. `flags` must currently be zero. +4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE +------------------------------------ + +:Capability: KVM_CAP_ARM_RME +:Architectures: arm64 +:Type: vcpu ioctl +:Parameters: struct kvm_arm_rmm_psci_complete (in) +:Returns: 0 if successful, < 0 on error + +:: + + struct kvm_arm_rmm_psci_complete { + __u64 target_mpidr; + __u32 psci_status; + __u32 padding[3]; + }; + +Where PSCI functions are handled by user space, the RMM needs to be informed of +the target of the operation using `target_mpidr`, along with the status +(`psci_status`). The RMM v1.0 specification defines two functions that require +this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO. + +If the kernel is handling PSCI then this is done automatically and the VMM +doesn't need to call this ioctl. .. _kvm_run:
@@ -8662,6 +8692,47 @@ This capability indicate to the userspace whether a PFNMAP memory region can be safely mapped as cacheable. This relies on the presence of force write back (FWB) feature support on the hardware. +7.44 KVM_CAP_ARM_RME +-------------------- + +:Architectures: arm64 +:Target: VM +:Parameters: args[0] provides an action, args[1] points to a structure in + memory for the action. +:Returns: 0 on success, negative value on error + +Used to configure and set up the memory for a Realm. The available actions are: + +================================= ============================================= + KVM_CAP_ARM_RME_CONFIG_REALM Takes struct arm_rme_config as args[1] and + configures realm parameters prior to it being + created. + + Options are ARM_RME_CONFIG_RPV to set the + "Realm Personalization Value" and + ARM_RME_CONFIG_HASH_ALGO to set the hash + algorithm. + + KVM_CAP_ARM_RME_CREATE_REALM Request the RMM to create the realm. The + realm's configuration parameters must be set + first. + + KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1] + and sets the RIPAS (Realm IPA State) to + RIPAS_RAM of a specified area of the realm's + IPA. + + KVM_CAP_ARM_RME_POPULATE_REALM Takes struct arm_rme_populate_realm as + args[1] and populates a region of protected + address space by copying the data from the + shared alias. + + KVM_CAP_ARM_RME_ACTIVATE_REALM Request the RMM to activate the realm. No + changes can be made to the Realm's populated + memory, IPA state, configuration parameters + or vCPU additions after this step. +================================= ============================================= + 8. Other capabilities. ======================
arch/arm64/include/uapi/asm/kvm.h +49 -0
61 lines not on this page · raw message
include/uapi/linux/kvm.h +10 -0
@@ -962,6 +962,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_EL2_E2H0 241#define KVM_CAP_RISCV_MP_STATE_RESET 242#define KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED 243 +#define KVM_CAP_ARM_RME 244struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1614,4 +1615,13 @@ struct kvm_pre_fault_memory { __u64 padding[5]; }; +/* Available with KVM_CAP_ARM_RME, only for VMs with KVM_VM_TYPE_ARM_REALM */ +struct kvm_arm_rmm_psci_complete { +__u64 target_mpidr; +__u32 psci_status; +__u32 padding[3 ]; +}; + +#define KVM_ARM_VCPU_RMM_PSCI_COMPLETE _IOW(KVMIO, 0xd6, struct kvm_arm_rmm_psci_complete) +#endif /* __LINUX_KVM_H */
Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v10 06/43] arm64: RME: Define the user ABI
Quoted 72 lines · Steven Price
There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.
Co-developed-by Suzuki K Poulose <[email protected]>Signed-off-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>Reviewed-by Gavin Shan <[email protected]>---
Changes since v9:
* Improvements to documentation.
* Bump the magic number forKVM_CAP_ARM_RMEto 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 bumpingKVM_VCPU_MAX_FEATURES- note this also exposesKVM_ARM_VCPU_HAS_EL2!--- Documentation/virt/kvm/api.rst | 71 +++++++++++++++++++++++++++++++ arch/arm64/include/uapi/asm/kvm.h | 49 +++++++++++++++++++++ include/uapi/linux/kvm.h | 10 +++++ 3 files changed, 130 insertions(+)Documentation/virt/kvm/api.rst +20
-0@@ -3549,6 +3549,11 @@ Possible features: Depends on KVM_CAP_ARM_EL2_E2H0. KVM_ARM_VCPU_HAS_EL2 must also be set. + - KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this + VCPU. This must be specified on all VCPUs created in a Realm VM. + Depends on KVM_CAP_ARM_RME. + Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC). + 4.83 KVM_ARM_PREFERRED_TARGET -----------------------------@@ -5122,6 +5127,7 @@ Recognised values for feature: ===== =========================================== arm64 KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE) + arm64 KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME) ===== =========================================== Finalizes the configuration of the specified vcpu feature.@@ -6476,6 +6482,30 @@ the capability to be present. `flags` must currently be zero. +4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE +------------------------------------ + +:Capability: KVM_CAP_ARM_RME +:Architectures: arm64 +:Type: vcpu ioctl +:Parameters: struct kvm_arm_rmm_psci_complete (in) +:Returns: 0 if successful, < 0 on error + +:: + + struct kvm_arm_rmm_psci_complete { + __u64 target_mpidr; + __u32 psci_status;
Documentation/virt/kvm/api.rst the capability to be present. · line 6508
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+ __u32 padding[3]; + }; + +Where PSCI functions are handled by user space, the RMM needs to be informed of +the target of the operation using `target_mpidr`, along with the status +(`psci_status`). The RMM v1.0 specification defines two functions that require +this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO. + +If the kernel is handling PSCI then this is done automatically and the VMM +doesn't need to call this ioctl.
Why should userspace involved in this? Why can't this be a notification that the host delivers to the RMM when the vcpu is about to run?
Quoted 38 lines
Documentation/virt/kvm/api.rst +31
-0.. _kvm_run:@@ -8662,6 +8692,47 @@ This capability indicate to the userspace whether a PFNMAP memory region can be safely mapped as cacheable. This relies on the presence of force write back (FWB) feature support on the hardware. +7.44 KVM_CAP_ARM_RME +-------------------- + +:Architectures: arm64 +:Target: VM +:Parameters: args[0] provides an action, args[1] points to a structure in + memory for the action. +:Returns: 0 on success, negative value on error + +Used to configure and set up the memory for a Realm. The available actions are: + +================================= ============================================= + KVM_CAP_ARM_RME_CONFIG_REALM Takes struct arm_rme_config as args[1] and + configures realm parameters prior to it being + created. + + Options are ARM_RME_CONFIG_RPV to set the + "Realm Personalization Value" and + ARM_RME_CONFIG_HASH_ALGO to set the hash + algorithm. + + KVM_CAP_ARM_RME_CREATE_REALM Request the RMM to create the realm. The + realm's configuration parameters must be set + first. + + KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1] + and sets the RIPAS (Realm IPA State) to + RIPAS_RAM of a specified area of the realm's + IPA. + + KVM_CAP_ARM_RME_POPULATE_REALM Takes struct arm_rme_populate_realm as
Documentation/virt/kvm/api.rst This capability indicate to the userspace whether a PFNMAP memory region · line 8735
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+ args[1] and populates a region of protected + address space by copying the data from the + shared alias. + + KVM_CAP_ARM_RME_ACTIVATE_REALM Request the RMM to activate the realm. No + changes can be made to the Realm's populated + memory, IPA state, configuration parameters + or vCPU additions after this step. +================================= ============================================= +
These are not capabilities, they are actions that the VMM may perform on a VM. You don't configure a VM using capabilities. You use it to buy into some behaviours, but that's all.
And then there is the semantic of this stuff. Why do I need something like KVM_CAP_ARM_RME_CREATE_REALM when I can just pass this as part of the VM type? Why do I need a new way to describe memory region when we already have memslots for that exact purpose?
Overall, you are leaking the RMM interface into userspace, and that's an absolute show-stopper. We have an API, it is not pretty, but it exists. We don't need another one that will be just as broken. If the RMM needs some impedance matching, that's the kernel's job.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 06/43] arm64: RME: Define the user ABI
Quoted 80 lines · Marc Zyngier
Quoted 78 lines · Steven Price
There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.
Co-developed-by Suzuki K Poulose <[email protected]>Signed-off-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>Reviewed-by Gavin Shan <[email protected]>---
Changes since v9:
* Improvements to documentation.
* Bump the magic number forKVM_CAP_ARM_RMEto 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 bumpingKVM_VCPU_MAX_FEATURES- note this also exposesKVM_ARM_VCPU_HAS_EL2!--- Documentation/virt/kvm/api.rst | 71 +++++++++++++++++++++++++++++++ arch/arm64/include/uapi/asm/kvm.h | 49 +++++++++++++++++++++ include/uapi/linux/kvm.h | 10 +++++ 3 files changed, 130 insertions(+)Documentation/virt/kvm/api.rst +26
-0@@ -3549,6 +3549,11 @@ Possible features: Depends on KVM_CAP_ARM_EL2_E2H0. KVM_ARM_VCPU_HAS_EL2 must also be set. + - KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this + VCPU. This must be specified on all VCPUs created in a Realm VM. + Depends on KVM_CAP_ARM_RME. + Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC). + 4.83 KVM_ARM_PREFERRED_TARGET -----------------------------@@ -5122,6 +5127,7 @@ Recognised values for feature: ===== =========================================== arm64 KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE) + arm64 KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME) ===== =========================================== Finalizes the configuration of the specified vcpu feature.@@ -6476,6 +6482,30 @@ the capability to be present. `flags` must currently be zero. +4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE +------------------------------------ + +:Capability: KVM_CAP_ARM_RME +:Architectures: arm64 +:Type: vcpu ioctl +:Parameters: struct kvm_arm_rmm_psci_complete (in) +:Returns: 0 if successful, < 0 on error + +:: + + struct kvm_arm_rmm_psci_complete { + __u64 target_mpidr; + __u32 psci_status; + __u32 padding[3]; + }; + +Where PSCI functions are handled by user space, the RMM needs to be informed of +the target of the operation using `target_mpidr`, along with the status +(`psci_status`). The RMM v1.0 specification defines two functions that require
Quoted 8 lines
Documentation/virt/kvm/api.rst the capability to be present. · line 6508
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO. + +If the kernel is handling PSCI then this is done automatically and the VMM +doesn't need to call this ioctl.Why should userspace involved in this? Why can't this be a notification that the host delivers to the RMM when the vcpu is about to run?
This is only when PSCI is being handled by user space. If the kernel (i.e KVM) is handling PSCI then indeed there's no user space involvement.
I'm not sure how we could avoid this when PSCI is being implemented in user space. Or am I missing something?
Quoted 58 lines
Quoted 38 lines
Documentation/virt/kvm/api.rst +31
-0.. _kvm_run:@@ -8662,6 +8692,47 @@ This capability indicate to the userspace whether a PFNMAP memory region can be safely mapped as cacheable. This relies on the presence of force write back (FWB) feature support on the hardware. +7.44 KVM_CAP_ARM_RME +-------------------- + +:Architectures: arm64 +:Target: VM +:Parameters: args[0] provides an action, args[1] points to a structure in + memory for the action. +:Returns: 0 on success, negative value on error + +Used to configure and set up the memory for a Realm. The available actions are: + +================================= ============================================= + KVM_CAP_ARM_RME_CONFIG_REALM Takes struct arm_rme_config as args[1] and + configures realm parameters prior to it being + created. + + Options are ARM_RME_CONFIG_RPV to set the + "Realm Personalization Value" and + ARM_RME_CONFIG_HASH_ALGO to set the hash + algorithm. + + KVM_CAP_ARM_RME_CREATE_REALM Request the RMM to create the realm. The + realm's configuration parameters must be set + first. + + KVM_CAP_ARM_RME_INIT_RIPAS_REALM Takes struct arm_rme_init_ripas as args[1] + and sets the RIPAS (Realm IPA State) to + RIPAS_RAM of a specified area of the realm's + IPA. + + KVM_CAP_ARM_RME_POPULATE_REALM Takes struct arm_rme_populate_realm asDocumentation/virt/kvm/api.rst This capability indicate to the userspace whether a PFNMAP memory region · line 8735
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+ args[1] and populates a region of protected + address space by copying the data from the + shared alias. + + KVM_CAP_ARM_RME_ACTIVATE_REALM Request the RMM to activate the realm. No + changes can be made to the Realm's populated + memory, IPA state, configuration parameters + or vCPU additions after this step. +================================= ============================================= +These are not capabilities, they are actions that the VMM may perform on a VM. You don't configure a VM using capabilities. You use it to buy into some behaviours, but that's all.
And then there is the semantic of this stuff. Why do I need something like
KVM_CAP_ARM_RME_CREATE_REALMwhen I can just pass this as part of the VM type? Why do I need a new way to describe memory region when we already have memslots for that exact purpose?
Quoted 4 lines
Overall, you are leaking the RMM interface into userspace, and that's an absolute show-stopper. We have an API, it is not pretty, but it exists. We don't need another one that will be just as broken. If the RMM needs some impedance matching, that's the kernel's job.
So I'll admit the (ab)use of capabilities to set up the realm is a bit of a hack. I initially did it this way to maintain some compatibility with a prototype implementation, but it's taken until v10 for anyone to express displeasure with the approach! It would of course be possible to implement these are separate ioctls.
However, it seems we need to pin down the semantics before I do that refactoring.
KVM_CAP_ARM_RME_CREATE_REALM
============================
The desire is that you can configure the aspects of the realm piecemeal and then later trigger the SMC to the RMM. This avoids the need for a big structure (which will grow with new features) containing all the configuration. It also allows KVM_CAP_ARM_RME_CONFIG_REALM to validate each configuration and provide immediate failure when a configuration option is invalid.
Would you prefer ditching KVM_CAP_ARM_RME_CONFIG_REALM and passing a structure of all the configuration options to KVM_CAP_ARM_RME_CREATE_REALM? It would make identifying what part of an invalid configuration is problematic harder. And we'd probably therefore need to add a new discovery mechanism to find e.g. which hash algorithms are supported.
KVM_CAP_ARM_RME_INIT_RIPAS_REALM
================================
This is a property that doesn't exist for a normal guest and as such the VMM is going to have to do something extra to describe which areas of RAM are protected and which are shared. I guess with the recent guest_memfd changes it might be possible to pull that from the guest_memfd instance(s). But note that it's entirely valid (from the CCA perspective) to have both protected and shared regions that are not backed at guest start. So setting the RIPAS to RAM (i.e. making an area of memory protected) is independent of the guest_memfd backing status.
KVM_CAP_ARM_RME_POPULATE_REALM
==============================
This ties in with the above. This is effectively doing a data-preserving conversion of shared to protected. We need some form of this to set the initial data for the guest (boot loader/kernel image etc).
In this patch set this API is a little broken as it requires double backing (both a valid VMA in the VMM and allocation in the guest_memfd). My expectation was to replace this with a version that simply does an in-place data-preserving conversion in guest_memfd. The only drawback is that this requires a double memcpy() - KVM would need to copy the data to a temporary buffer, and then the RMM would copy it back (after dealing with memory encryption setup).
The alternative would be to provide a separate input buffer for POPULATE_REALM. That could e.g. be a mmap()ed file, but that moves CCA further away from a standard interface.
KVM_CAP_ARM_RME_ACTIVATE_REALM
==============================
This is easiest to drop - we could just do this step when the VMM first tries to start a VCPU. The main benefits are:
* It makes error reporting more obvious (you can tell the difference
between the RMM failing the activate and something else preventing a VCPU entry).
* It makes the KVM code slightly clearer because the state transition
is triggered by an obvious action from the VMM, and there's less potential for races.
I agree it's a shame that the VMM can't just use the standard KVM interface for realm guest setup. But there are some fundamental differences between a normal guest and a realm guest. So the VMM will need to be enlightened. Feel free to comment on the above if you have suggestions on how the above API can be improved.
Thanks,
Steve
Suzuki K Poulose
reviewerRe: [PATCH v10 06/43] arm64: RME: Define the user ABI
Quoted 90 lines · Steven Price
Quoted 80 lines · Marc Zyngier
Quoted 78 lines · Steven Price
There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.
Co-developed-by Suzuki K Poulose <[email protected]>Signed-off-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>Reviewed-by Gavin Shan <[email protected]>---
Changes since v9:
* Improvements to documentation.
* Bump the magic number forKVM_CAP_ARM_RMEto 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 bumpingKVM_VCPU_MAX_FEATURES- note this also exposesKVM_ARM_VCPU_HAS_EL2!--- Documentation/virt/kvm/api.rst | 71 +++++++++++++++++++++++++++++++ arch/arm64/include/uapi/asm/kvm.h | 49 +++++++++++++++++++++ include/uapi/linux/kvm.h | 10 +++++ 3 files changed, 130 insertions(+)Documentation/virt/kvm/api.rst +26
-0@@ -3549,6 +3549,11 @@ Possible features: Depends on KVM_CAP_ARM_EL2_E2H0. KVM_ARM_VCPU_HAS_EL2 must also be set. + - KVM_ARM_VCPU_REC: Allocate a REC (Realm Execution Context) for this + VCPU. This must be specified on all VCPUs created in a Realm VM. + Depends on KVM_CAP_ARM_RME. + Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_REC). + 4.83 KVM_ARM_PREFERRED_TARGET -----------------------------@@ -5122,6 +5127,7 @@ Recognised values for feature: ===== =========================================== arm64 KVM_ARM_VCPU_SVE (requires KVM_CAP_ARM_SVE) + arm64 KVM_ARM_VCPU_REC (requires KVM_CAP_ARM_RME) ===== =========================================== Finalizes the configuration of the specified vcpu feature.@@ -6476,6 +6482,30 @@ the capability to be present. `flags` must currently be zero. +4.144 KVM_ARM_VCPU_RMM_PSCI_COMPLETE +------------------------------------ + +:Capability: KVM_CAP_ARM_RME +:Architectures: arm64 +:Type: vcpu ioctl +:Parameters: struct kvm_arm_rmm_psci_complete (in) +:Returns: 0 if successful, < 0 on error + +:: + + struct kvm_arm_rmm_psci_complete { + __u64 target_mpidr; + __u32 psci_status; + __u32 padding[3]; + }; + +Where PSCI functions are handled by user space, the RMM needs to be informed of +the target of the operation using `target_mpidr`, along with the status +(`psci_status`). The RMM v1.0 specification defines two functions that requireQuoted 8 lines
Documentation/virt/kvm/api.rst the capability to be present. · line 6508
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+this call: PSCI_CPU_ON and PSCI_AFFINITY_INFO. + +If the kernel is handling PSCI then this is done automatically and the VMM +doesn't need to call this ioctl.Why should userspace involved in this? Why can't this be a notification that the host delivers to the RMM when the vcpu is about to run?
Quoted 5 lines
This is only when PSCI is being handled by user space. If the kernel (i.e KVM) is handling PSCI then indeed there's no user space involvement.
I'm not sure how we could avoid this when PSCI is being implemented in user space. Or am I missing something?
I think there is a bit of disconnect here.
The RMM doesn't track the RECs for a given vCPU. So, when it requires
the REC object for a given vCPU, the Host provides this via an RMI_PSCI_COMPLETE call. This is used for PSCI_CPU_ON and PSCI_AFFINITY_INFO today, where the RMM can do the book keeping for the REC and emulate the PSCI. Now, the host does have a control
on whether to ACCEPT or REJECT a request (for CPU_ON).
The RMM requires the PSCI_COMPLETE call, before it can return thePSCI_CPU_ON back to the caller and also before the target vCPU can
run. Thus, this cannot be delayed until the "new VCPU" is run.
Like Steven mentioned, this is only useful in the UABI if the VMM is handling the PSCI. And this must be issued, before the target vCPU can be scheduled.
Suzuki
PATCH v10 7/43 arm64: RME: ioctls to create and configure realms
In reply to Steven PriceAdd the KVM_CAP_ARM_RME_CREATE_RD ioctl to create a realm. This involves delegating pages to the RMM to hold the Realm Descriptor (RD) and for the base level of the Realm Translation Tables (RTT). A VMID also need to be picked, since the RMM has a separate VMID address space a dedicated allocator is added for this purpose.
KVM_CAP_ARM_RME_CONFIG_REALM is provided to allow configuring the realm before it is created. Configuration options can be classified as:
1. Parameters specific to the Realm stage2 (e.g. IPA Size, vmid, stage2 entry level, entry level RTTs, number of RTTs in start level, LPA2) Most of these are not measured by RMM and comes from KVM book keeping.
2. Parameters controlling "Arm Architecture features for the VM". (e.g. SVE VL, PMU counters, number of HW BRPs/WPs), configured by the VMM using the "user ID register write" mechanism. These will be supported in the later patches.
3. Parameters are not part of the core Arm architecture but defined by the RMM spec (e.g. Hash algorithm for measurement, Personalisation value). These are programmed via KVM_CAP_ARM_RME_CONFIG_REALM.
For the IPA size there is the possibility that the RMM supports a different size to the IPA size supported by KVM for normal guests. At the moment the 'normal limit' is exposed by KVM_CAP_ARM_VM_IPA_SIZE and the IPA size is configured by the bottom bits of vm_type in KVM_CREATE_VM. This means that it isn't easy for the VMM to discover what IPA sizes are supported for Realm guests. Since the IPA is part of the measurement of the realm guest the current expectation is that the VMM will be required to pick the IPA size demanded by attestation and therefore simply failing if this isn't available is fine. An option would be to expose a new capability ioctl to obtain the RMM's maximum IPA size if this is needed in the future.
---
Changes since 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 | 5 + arch/arm64/include/asm/kvm_rme.h | 19 ++ arch/arm64/kvm/arm.c | 16 ++ arch/arm64/kvm/mmu.c | 24 +- arch/arm64/kvm/rme.c | 322 +++++++++++++++++++++++++++ 5 files changed, 383 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +5 -0
@@ -687,6 +687,11 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)return READ_ONCE (kvm->arch.realm.state); } +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ +return kvm_is_realm (kvm) &&kvm_realm_state (kvm) !=REALM_STATE_NONE ; +} +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) {return false;
arch/arm64/include/asm/kvm_rme.h +19 -0
@@ -6,6 +6,8 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H +#include <uapi/linux/kvm.h> +/** * enum realm_state - State of a Realm */
@@ -46,11 +48,28 @@ enum realm_state {* struct realm - Additional per VM data for a Realm * * @state: The lifetime state machine for the realm +* @rd: Kernel mapping of the Realm Descriptor (RD) +* @params: Parameters for the RMI_REALM_CREATE command +* @num_aux: The number of auxiliary pages required by the RMM +* @vmid: VMID to be used by the RMM for the realm +* @ia_bits: Number of valid Input Address bits in the IPA */ struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };void kvm_init_rme (void ); +u32 kvm_realm_ipa_limit (void ); + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap); +int kvm_init_realm_vm (struct kvm *kvm); +void kvm_destroy_realm (struct kvm *kvm);#endif /* __ASM_KVM_RME_H */
arch/arm64/kvm/arm.c +16 -0
@@ -136,6 +136,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_ARM_RME : +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default :break ; }
@@ -198,6 +203,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)bitmap_zero (kvm->arch.vcpu_features,KVM_VCPU_MAX_FEATURES ); +/* Initialise the realm bits after the generic bits are enabled */ +if (kvm_is_realm (kvm)) { + ret =kvm_init_realm_vm (kvm); +if (ret) + goto err_free_cpumask; + } +return 0 ; err_free_cpumask:
@@ -257,6 +269,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_unshare_hyp (kvm, kvm +1 );kvm_arm_teardown_hypercalls (kvm); +kvm_destroy_realm (kvm); }static bool kvm_has_full_ptr_auth (void )
@@ -417,6 +430,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else r =kvm_supports_cacheable_pfnmap ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ;
arch/arm64/kvm/mmu.c +21 -3
@@ -871,12 +871,16 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { .icache_inval_pou = invalidate_icache_guest_page, }; -static int kvm_init_ipa_range (struct kvm_s2_mmu *mmu,unsigned long type) +static int kvm_init_ipa_range (struct kvm *kvm, +struct kvm_s2_mmu *mmu,unsigned long type) {u32 kvm_ipa_limit =get_kvm_ipa_limit ();u64 mmfr0, mmfr1;u32 phys_shift; +if (kvm_is_realm (kvm)) + kvm_ipa_limit =kvm_realm_ipa_limit (); +if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK )return -EINVAL ;
@@ -941,7 +945,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn -EINVAL ; } - err =kvm_init_ipa_range (mmu, type); + err =kvm_init_ipa_range (kvm, mmu, type);if (err)return err;
@@ -1067,6 +1071,19 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)struct kvm_pgtable *pgt =NULL ;write_lock (&kvm->mmu_lock); +if (kvm_is_realm (kvm) && + (kvm_realm_state (kvm) !=REALM_STATE_DEAD && +kvm_realm_state (kvm) !=REALM_STATE_NONE )) { +/* Tearing down RTTs will be added in a later patch */ +write_unlock (&kvm->mmu_lock); + +/* +* The PGD pages can be reclaimed only after the realm (RD) is +* destroyed. We call this again from kvm_destroy_realm() after +* the RD is destroyed. +*/ +return ; + } pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;
@@ -1076,7 +1093,8 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)write_unlock (&kvm->mmu_lock);if (pgt) { -KVM_PGT_FN (kvm_pgtable_stage2_destroy)(pgt); +if (!kvm_is_realm (kvm)) +KVM_PGT_FN (kvm_pgtable_stage2_destroy)(pgt);kfree (pgt); } }
arch/arm64/kvm/rme.c +322 -0
342 lines not on this page · raw message
Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v10 07/43] arm64: RME: ioctls to create and configure realms
Quoted 25 lines · Steven Price
Add the
KVM_CAP_ARM_RME_CREATE_RDioctl to create a realm. This involves delegating pages to the RMM to hold the Realm Descriptor (RD) and for the base level of the Realm Translation Tables (RTT). A VMID also need to be picked, since the RMM has a separate VMID address space a dedicated allocator is added for this purpose.
KVM_CAP_ARM_RME_CONFIG_REALMis provided to allow configuring the realm before it is created. Configuration options can be classified as:1. Parameters specific to the Realm stage2 (e.g. IPA Size, vmid, stage2 entry level, entry level RTTs, number of RTTs in start level, LPA2) Most of these are not measured by RMM and comes from KVM book keeping.
2. Parameters controlling "Arm Architecture features for the VM". (e.g. SVE VL, PMU counters, number of HW BRPs/WPs), configured by the VMM using the "user ID register write" mechanism. These will be supported in the later patches.
3. Parameters are not part of the core Arm architecture but defined by the RMM spec (e.g. Hash algorithm for measurement, Personalisation value). These are programmed via
KVM_CAP_ARM_RME_CONFIG_REALM.
Quoted 11 lines
For the IPA size there is the possibility that the RMM supports a different size to the IPA size supported by KVM for normal guests. At the moment the 'normal limit' is exposed by
KVM_CAP_ARM_VM_IPA_SIZEand the IPA size is configured by the bottom bits ofvm_typeinKVM_CREATE_VM. This means that it isn't easy for the VMM to discover what IPA sizes are supported for Realm guests. Since the IPA is part of the measurement of the realm guest the current expectation is that the VMM will be required to pick the IPA size demanded by attestation and therefore simply failing if this isn't available is fine. An option would be to expose a new capability ioctl to obtain the RMM's maximum IPA size if this is needed in the future.
I think you must have something of the sort. Configuring the IPA size is one of the basic things a VMM performs, querying the host for that information. Implicit IPA size prevents the basics of what we have userspace for: management of the IPA space. Otherwise, how can the guest know where to place its I/O, for example?
Quoted 63 lines
Co-developed-by Suzuki K Poulose <[email protected]>Signed-off-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>Reviewed-by Gavin Shan <[email protected]>---
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 inrealm_create_rd()to actually return an error value.
Changes since v8:
* Fixfree_delegated_granule()to not callkvm_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 hostPAGE_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:
* Introducefree_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 forrmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.--- arch/arm64/include/asm/kvm_emulate.h | 5 + arch/arm64/include/asm/kvm_rme.h | 19 ++ arch/arm64/kvm/arm.c | 16 ++ arch/arm64/kvm/mmu.c | 24 +- arch/arm64/kvm/rme.c | 322 +++++++++++++++++++++++++++ 5 files changed, 383 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_emulate.h +5
-0@@ -687,6 +687,11 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)return READ_ONCE (kvm->arch.realm.state); } +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ +return kvm_is_realm (kvm) &&kvm_realm_state (kvm) !=REALM_STATE_NONE ; +} +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) {return false;arch/arm64/include/asm/kvm_rme.h +0
-0
arch/arm64/include/asm/kvm_rme.h line 10
Quoted 5 lines
arch/arm64/include/asm/kvm_rme.h +2
-0@@ -6,6 +6,8 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H +#include <uapi/linux/kvm.h> +
Why do you need to include a uapi-specific file for something that is deeply arch-specific? This is a recipe for circular dependencies that are horrible to solve.
Quoted 118 lines
arch/arm64/include/asm/kvm_rme.h +17
-0/** * enum realm_state - State of a Realm */ @@ -46,11 +48,28 @@ enum realm_state {* struct realm - Additional per VM data for a Realm * * @state: The lifetime state machine for the realm +* @rd: Kernel mapping of the Realm Descriptor (RD) +* @params: Parameters for the RMI_REALM_CREATE command +* @num_aux: The number of auxiliary pages required by the RMM +* @vmid: VMID to be used by the RMM for the realm +* @ia_bits: Number of valid Input Address bits in the IPA */ struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };void kvm_init_rme (void ); +u32 kvm_realm_ipa_limit (void ); + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap); +int kvm_init_realm_vm (struct kvm *kvm); +void kvm_destroy_realm (struct kvm *kvm);#endif /* __ASM_KVM_RME_H */ arch/arm64/kvm/arm.c +16
-0@@ -136,6 +136,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_ARM_RME : +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default :break ; }@@ -198,6 +203,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)bitmap_zero (kvm->arch.vcpu_features,KVM_VCPU_MAX_FEATURES ); +/* Initialise the realm bits after the generic bits are enabled */ +if (kvm_is_realm (kvm)) { + ret =kvm_init_realm_vm (kvm); +if (ret) + goto err_free_cpumask; + } +return 0 ; err_free_cpumask:@@ -257,6 +269,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_unshare_hyp (kvm, kvm +1 );kvm_arm_teardown_hypercalls (kvm); +kvm_destroy_realm (kvm); }static bool kvm_has_full_ptr_auth (void )@@ -417,6 +430,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else r =kvm_supports_cacheable_pfnmap ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ;arch/arm64/kvm/mmu.c +9
-2@@ -871,12 +871,16 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { .icache_inval_pou = invalidate_icache_guest_page, }; -static int kvm_init_ipa_range (struct kvm_s2_mmu *mmu,unsigned long type) +static int kvm_init_ipa_range (struct kvm *kvm, +struct kvm_s2_mmu *mmu,unsigned long type) {u32 kvm_ipa_limit =get_kvm_ipa_limit ();u64 mmfr0, mmfr1;u32 phys_shift; +if (kvm_is_realm (kvm)) + kvm_ipa_limit =kvm_realm_ipa_limit (); +if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK )return -EINVAL ;@@ -941,7 +945,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn -EINVAL ; } - err =kvm_init_ipa_range (mmu, type); + err =kvm_init_ipa_range (kvm, mmu, type);if (err)return err;@@ -1067,6 +1071,19 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)struct kvm_pgtable *pgt =NULL ;write_lock (&kvm->mmu_lock); +if (kvm_is_realm (kvm) && + (kvm_realm_state (kvm) !=REALM_STATE_DEAD && +kvm_realm_state (kvm) !=REALM_STATE_NONE )) {
arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1086
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+/* Tearing down RTTs will be added in a later patch */ +write_unlock (&kvm->mmu_lock); + +/* +* The PGD pages can be reclaimed only after the realm (RD) is +* destroyed. We call this again from kvm_destroy_realm() after +* the RD is destroyed. +*/ +return ; + }
Please move S2 management outside of this patch. It's hard to follow what is happening when this sort of stuff is split over multiple patches.
arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1097
Quoted 10 lines
arch/arm64/kvm/mmu.c +2
-1pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;@@ -1076,7 +1093,8 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)write_unlock (&kvm->mmu_lock);if (pgt) { -KVM_PGT_FN (kvm_pgtable_stage2_destroy)(pgt); +if (!kvm_is_realm (kvm)) +KVM_PGT_FN (kvm_pgtable_stage2_destroy)(pgt);
having to do stuff like this is horrid. Please provide a backend to the KVM_PGT_FN() helper instead.
Quoted 37 lines
arch/arm64/kvm/mmu.c +0
-0kfree (pgt); } }arch/arm64/kvm/rme.c +16
-0@@ -5,9 +5,23 @@#include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> +#include <asm/kvm_pgtable.h> + +static unsigned long rmm_feat_reg0; + +#define RMM_PAGE_SHIFT 12 +#define RMM_PAGE_SIZE BIT(RMM_PAGE_SHIFT) + +static bool rme_has_feature (unsigned long feature) +{ +return !!u64_get_bits (rmm_feat_reg0, feature); +} +static int rmi_check_version (void ) {struct arm_smccc_res res;@@ -42,6 +56,308 @@ static int rmi_check_version(void)return 0 ; } +u32 kvm_realm_ipa_limit (void ) +{
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 70
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_S2SZ ); +} + +static int get_start_level (struct realm *realm) +{ +/* +* Open coded version of 4 - stage2_pgtable_levels(ia_bits) but using +* the RMM's page size rather than the host's. +*/ +return 4 - ((realm->ia_bits -8 ) / (RMM_PAGE_SHIFT -3 ));
Why 8?
Quoted 16 lines
arch/arm64/kvm/rme.c +16
-0+} + +static int free_delegated_granule (phys_addr_t phys) +{ +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Undelegate failed: leak the page */ +return -EBUSY ; + } + +free_page ((unsigned long )phys_to_virt (phys)); + +return 0 ; +} + +/* Calculate the number of s2 root rtts needed */ +static int realm_num_root_rtts (struct realm *realm)
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 96
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+{ +unsigned int ipa_bits = realm->ia_bits; +unsigned int levels =4 -get_start_level (realm); +unsigned int sl_ipa_bits = levels * (RMM_PAGE_SHIFT -3 ) + +RMM_PAGE_SHIFT ; + +if (sl_ipa_bits >= ipa_bits) +return 1 ; + +return 1 << (ipa_bits - sl_ipa_bits);
Don't we already have similar things in pgtables.c? It has a strong feel of deja-vu.
Quoted 90 lines
arch/arm64/kvm/rme.c +90
-0+} + +static int realm_create_rd (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct realm_params *params = realm->params; +void *rd =NULL ; +phys_addr_t rd_phys, params_phys; +size_t pgd_size =kvm_pgtable_stage2_pgd_size (kvm->arch.mmu.vtcr); +int i, r; +int rtt_num_start; + + realm->ia_bits =VTCR_EL2_IPA (kvm->arch.mmu.vtcr); + rtt_num_start =realm_num_root_rtts (realm); + +if (WARN_ON (realm->rd || !realm->params)) +return -EEXIST ; + +if (pgd_size /RMM_PAGE_SIZE < rtt_num_start) +return -EINVAL ; + + rd = (void *)__get_free_page (GFP_KERNEL ); +if (!rd) +return -ENOMEM ; + + rd_phys =virt_to_phys (rd); +if (rmi_granule_delegate (rd_phys)) { + r = -ENXIO ; + goto free_rd; + } + +for (i =0 ; i < pgd_size; i +=RMM_PAGE_SIZE ) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i; + +if (rmi_granule_delegate (pgd_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + } + + params->s2sz =VTCR_EL2_IPA (kvm->arch.mmu.vtcr); + params->rtt_level_start =get_start_level (realm); + params->rtt_num_start = rtt_num_start; + params->rtt_base = kvm->arch.mmu.pgd_phys; + params->vmid = realm->vmid; + + params_phys =virt_to_phys (params); + +if (rmi_realm_create (rd_phys, params_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + +if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) { +WARN_ON (rmi_realm_destroy (rd_phys)); + r = -ENXIO ; + goto out_undelegate_tables; + } + + realm->rd = rd; + +return 0 ; + +out_undelegate_tables: +while (i >0 ) { + i -=RMM_PAGE_SIZE ; + +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) { +/* Leak the pages if they cannot be returned */ + kvm->arch.mmu.pgt =NULL ; +break ; + } + } +if (WARN_ON (rmi_granule_undelegate (rd_phys))) { +/* Leak the page if it isn't returned */ +return r; + } +free_rd: +free_page ((unsigned long )rd); +return r; +} + +/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 196
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ; + } + +return 0 ; +}
Why do we need a VMID allocator? This is a different translation regime, which won't intersect with the host's. Surely the RMM can maintain its own?
Quoted 56 lines
arch/arm64/kvm/rme.c +56
-0+ +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (kvm_realm_is_created (kvm)) +return -EEXIST ; + + ret =rme_vmid_reserve (); +if (ret <0 ) +return ret; + realm->vmid = ret; + + ret =realm_create_rd (kvm); +if (ret) { +rme_vmid_release (realm->vmid); +return ret; + } + +WRITE_ONCE (realm->state,REALM_STATE_NEW ); + +/* The realm is up, free the parameters. */ +free_page ((unsigned long )realm->params); + realm->params =NULL ; + +return 0 ; +} + +static int config_realm_hash_algo (struct realm *realm, +struct arm_rme_config *cfg) +{ +switch (cfg->hash_algo) { +case ARM_RME_CONFIG_HASH_ALGO_SHA256 : +if (!rme_has_feature (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ;
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 262
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+case ARM_RME_CONFIG_HASH_ALGO_SHA512 : +if (!rme_has_feature (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + realm->params->hash_algo = cfg->hash_algo; +return 0 ; +}
What is the purpose of this?
Overall, there is a lot here that needs to be explained, because I don't immediately understand what this stuff is all about. This file needs a large comment at the front explaining what is all that for.
Paste the whole spec in if that helps.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 07/43] arm64: RME: ioctls to create and configure realms
Quoted 39 lines · Marc Zyngier
Quoted 25 lines · Steven Price
Add the
KVM_CAP_ARM_RME_CREATE_RDioctl to create a realm. This involves delegating pages to the RMM to hold the Realm Descriptor (RD) and for the base level of the Realm Translation Tables (RTT). A VMID also need to be picked, since the RMM has a separate VMID address space a dedicated allocator is added for this purpose.
KVM_CAP_ARM_RME_CONFIG_REALMis provided to allow configuring the realm before it is created. Configuration options can be classified as:1. Parameters specific to the Realm stage2 (e.g. IPA Size, vmid, stage2 entry level, entry level RTTs, number of RTTs in start level, LPA2) Most of these are not measured by RMM and comes from KVM book keeping.
2. Parameters controlling "Arm Architecture features for the VM". (e.g. SVE VL, PMU counters, number of HW BRPs/WPs), configured by the VMM using the "user ID register write" mechanism. These will be supported in the later patches.
3. Parameters are not part of the core Arm architecture but defined by the RMM spec (e.g. Hash algorithm for measurement, Personalisation value). These are programmed via
KVM_CAP_ARM_RME_CONFIG_REALM.Quoted 11 lines
For the IPA size there is the possibility that the RMM supports a different size to the IPA size supported by KVM for normal guests. At the moment the 'normal limit' is exposed by
KVM_CAP_ARM_VM_IPA_SIZEand the IPA size is configured by the bottom bits ofvm_typeinKVM_CREATE_VM. This means that it isn't easy for the VMM to discover what IPA sizes are supported for Realm guests. Since the IPA is part of the measurement of the realm guest the current expectation is that the VMM will be required to pick the IPA size demanded by attestation and therefore simply failing if this isn't available is fine. An option would be to expose a new capability ioctl to obtain the RMM's maximum IPA size if this is needed in the future.
Quoted 5 lines
I think you must have something of the sort. Configuring the IPA size is one of the basic things a VMM performs, querying the host for that information. Implicit IPA size prevents the basics of what we have userspace for: management of the IPA space. Otherwise, how can the guest know where to place its I/O, for example?
Well the argument is that user space doesn't have much of a choice on IPA size if it wants the attestation to match, and that realms aren't much use without attestation. I'm happy to add a query but I was leaving it until someone actually found it useful.
Quoted 66 lines
Quoted 66 lines
Co-developed-by Suzuki K Poulose <[email protected]>Signed-off-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>Reviewed-by Gavin Shan <[email protected]>---
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 inrealm_create_rd()to actually return an error value.
Changes since v8:
* Fixfree_delegated_granule()to not callkvm_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 hostPAGE_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:
* Introducefree_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 forrmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.--- arch/arm64/include/asm/kvm_emulate.h | 5 + arch/arm64/include/asm/kvm_rme.h | 19 ++ arch/arm64/kvm/arm.c | 16 ++ arch/arm64/kvm/mmu.c | 24 +- arch/arm64/kvm/rme.c | 322 +++++++++++++++++++++++++++ 5 files changed, 383 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_emulate.h +5
-0@@ -687,6 +687,11 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)return READ_ONCE (kvm->arch.realm.state); } +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ +return kvm_is_realm (kvm) &&kvm_realm_state (kvm) !=REALM_STATE_NONE ; +} +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) {return false;arch/arm64/include/asm/kvm_rme.h +0
-0@@ -6,6 +6,8 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H
Quoted 6 lines
arch/arm64/include/asm/kvm_rme.h line 10
Quoted 2 lines
arch/arm64/include/asm/kvm_rme.h +2
-0+#include <uapi/linux/kvm.h> +Why do you need to include a uapi-specific file for something that is deeply arch-specific? This is a recipe for circular dependencies that are horrible to solve.
Good point - that was me just being lazy. We need a forward declaration of struct kvm_enable_cap for kvm_realm_enable_cap(), but there's no reason to bring that header in for just that. Thanks for pointing it out.
Quoted 123 lines
Quoted 123 lines
arch/arm64/include/asm/kvm_rme.h +17
-0/** * enum realm_state - State of a Realm */ @@ -46,11 +48,28 @@ enum realm_state {* struct realm - Additional per VM data for a Realm * * @state: The lifetime state machine for the realm +* @rd: Kernel mapping of the Realm Descriptor (RD) +* @params: Parameters for the RMI_REALM_CREATE command +* @num_aux: The number of auxiliary pages required by the RMM +* @vmid: VMID to be used by the RMM for the realm +* @ia_bits: Number of valid Input Address bits in the IPA */ struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };void kvm_init_rme (void ); +u32 kvm_realm_ipa_limit (void ); + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap); +int kvm_init_realm_vm (struct kvm *kvm); +void kvm_destroy_realm (struct kvm *kvm);#endif /* __ASM_KVM_RME_H */ arch/arm64/kvm/arm.c +16
-0@@ -136,6 +136,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_ARM_RME : +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default :break ; }@@ -198,6 +203,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)bitmap_zero (kvm->arch.vcpu_features,KVM_VCPU_MAX_FEATURES ); +/* Initialise the realm bits after the generic bits are enabled */ +if (kvm_is_realm (kvm)) { + ret =kvm_init_realm_vm (kvm); +if (ret) + goto err_free_cpumask; + } +return 0 ; err_free_cpumask:@@ -257,6 +269,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_unshare_hyp (kvm, kvm +1 );kvm_arm_teardown_hypercalls (kvm); +kvm_destroy_realm (kvm); }static bool kvm_has_full_ptr_auth (void )@@ -417,6 +430,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else r =kvm_supports_cacheable_pfnmap ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ;arch/arm64/kvm/mmu.c +14
-2@@ -871,12 +871,16 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { .icache_inval_pou = invalidate_icache_guest_page, }; -static int kvm_init_ipa_range (struct kvm_s2_mmu *mmu,unsigned long type) +static int kvm_init_ipa_range (struct kvm *kvm, +struct kvm_s2_mmu *mmu,unsigned long type) {u32 kvm_ipa_limit =get_kvm_ipa_limit ();u64 mmfr0, mmfr1;u32 phys_shift; +if (kvm_is_realm (kvm)) + kvm_ipa_limit =kvm_realm_ipa_limit (); +if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK )return -EINVAL ;@@ -941,7 +945,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn -EINVAL ; } - err =kvm_init_ipa_range (mmu, type); + err =kvm_init_ipa_range (kvm, mmu, type);if (err)return err;@@ -1067,6 +1071,19 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)struct kvm_pgtable *pgt =NULL ;write_lock (&kvm->mmu_lock); +if (kvm_is_realm (kvm) && + (kvm_realm_state (kvm) !=REALM_STATE_DEAD && +kvm_realm_state (kvm) !=REALM_STATE_NONE )) { +/* Tearing down RTTs will be added in a later patch */ +write_unlock (&kvm->mmu_lock); + +/* +* The PGD pages can be reclaimed only after the realm (RD) is
Quoted 8 lines
arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1086
Quoted 5 lines
arch/arm64/kvm/mmu.c +5
-0+* destroyed. We call this again from kvm_destroy_realm() after +* the RD is destroyed. +*/ +return ; + }Please move S2 management outside of this patch. It's hard to follow what is happening when this sort of stuff is split over multiple patches.
Fair enough.
Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/mmu.c +0
-0pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;@@ -1076,7 +1093,8 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)write_unlock (&kvm->mmu_lock);
Quoted 7 lines
arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1097
Quoted 4 lines
arch/arm64/kvm/mmu.c +2
-1if (pgt) { -KVM_PGT_FN (kvm_pgtable_stage2_destroy)(pgt); +if (!kvm_is_realm (kvm)) +KVM_PGT_FN (kvm_pgtable_stage2_destroy)(pgt);having to do stuff like this is horrid. Please provide a backend to the
KVM_PGT_FN()helper instead.
I'm not really sure how to do that. pKVM wraps a variety of functions because the behaviour with pKVM varies depending on whether pKVM is in use in the system, but whether the particular VM is protected.
Here we just have a single function and the behaviour difference is whether this particular VM is a realm or not.
Obviously I could modify the function to take a 'kvm' argument, provide CCA wrappers for all these functions and move everything into the wrappers. But that would be even more horrid as we'd have a ton of useless wrappers (just calling the existing functions) just to hide this one if...
But maybe I'm missing something?
Quoted 35 lines
Quoted 35 lines
arch/arm64/kvm/mmu.c +0
-0kfree (pgt); } }arch/arm64/kvm/rme.c +14
-0@@ -5,9 +5,23 @@#include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> +#include <asm/kvm_pgtable.h> + +static unsigned long rmm_feat_reg0; + +#define RMM_PAGE_SHIFT 12 +#define RMM_PAGE_SIZE BIT(RMM_PAGE_SHIFT) + +static bool rme_has_feature (unsigned long feature) +{ +return !!u64_get_bits (rmm_feat_reg0, feature); +} +static int rmi_check_version (void ) {struct arm_smccc_res res;@@ -42,6 +56,308 @@ static int rmi_check_version(void)return 0 ; }
Quoted 14 lines
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 70
Quoted 12 lines
arch/arm64/kvm/rme.c +12
-0+u32 kvm_realm_ipa_limit (void ) +{ +return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_S2SZ ); +} + +static int get_start_level (struct realm *realm) +{ +/* +* Open coded version of 4 - stage2_pgtable_levels(ia_bits) but using +* the RMM's page size rather than the host's. +*/ +return 4 - ((realm->ia_bits -8 ) / (RMM_PAGE_SHIFT -3 ));Why 8?
So I welcome ideas on how to code this better, the expression comes from expanding '4 - stage2_pgtable_levels(ia_bits)' as the comment says:
4 - stage2_pgtable_levels(ia_bits)
4 - ARM64_HW_PGTABLE_LEVELS((ia_bits) - 4)
4 - (((ia_bits - 4) - PTDESC_ORDER - 1) / PTDESC_TABLE_SHIFT) 4 - (((ia_bits - 4) - PTDESC_ORDER - 1) / (PAGE_SHIFT - PTDESC_ORDER))
PTDESC_ORDER is 3, and the reason for this mess is we don't want the host's PAGE_SHIFT and instead want to use RMM_PAGE_SHIFT. So subtituting those in:
4 - (((ia_bits - 4) - 3 - 1) / (RMM_PAGE_SHIFT - 3))
which simplifies to:
4 - ((ia_bits - 8) / (RMM_PAGE_SHIFT - 3))
Any ideas on how to improve the readability appreciated. But this was the best I could come up with.
Quoted 21 lines
Quoted 21 lines
arch/arm64/kvm/rme.c +21
-0+} + +static int free_delegated_granule (phys_addr_t phys) +{ +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Undelegate failed: leak the page */ +return -EBUSY ; + } + +free_page ((unsigned long )phys_to_virt (phys)); + +return 0 ; +} + +/* Calculate the number of s2 root rtts needed */ +static int realm_num_root_rtts (struct realm *realm) +{ +unsigned int ipa_bits = realm->ia_bits; +unsigned int levels =4 -get_start_level (realm); +unsigned int sl_ipa_bits = levels * (RMM_PAGE_SHIFT -3 ) + +RMM_PAGE_SHIFT ;
Quoted 8 lines
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 96
Quoted 5 lines
arch/arm64/kvm/rme.c +5
-0+ +if (sl_ipa_bits >= ipa_bits) +return 1 ; + +return 1 << (ipa_bits - sl_ipa_bits);Don't we already have similar things in
pgtables.c? It has a strong feel of deja-vu.
We do, again the problem is here we want to use RMM_PAGE_SHIFT rather than PAGE_SHIFT, and the existing functions/macros assume the host's page size.
Originally I did just use pgt->pgd_pages here as KVM has already allocated an appropriate number of PGD pages for a normal guest. But I've tried to make this code work with other host PAGE_SIZEs and simply removing the check 'works'.
But for now I've left the check in the init code for PAGE_SIZE=4k because there are some rough edges (the number of PGD pages allocated is wrong - although I believe always on the high side so should always work), and I haven't done much testing. I also didn't want to distract the review of the basics for the more complex mixed page size configurations.
Quoted 96 lines
Quoted 96 lines
arch/arm64/kvm/rme.c +96
-0+} + +static int realm_create_rd (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct realm_params *params = realm->params; +void *rd =NULL ; +phys_addr_t rd_phys, params_phys; +size_t pgd_size =kvm_pgtable_stage2_pgd_size (kvm->arch.mmu.vtcr); +int i, r; +int rtt_num_start; + + realm->ia_bits =VTCR_EL2_IPA (kvm->arch.mmu.vtcr); + rtt_num_start =realm_num_root_rtts (realm); + +if (WARN_ON (realm->rd || !realm->params)) +return -EEXIST ; + +if (pgd_size /RMM_PAGE_SIZE < rtt_num_start) +return -EINVAL ; + + rd = (void *)__get_free_page (GFP_KERNEL ); +if (!rd) +return -ENOMEM ; + + rd_phys =virt_to_phys (rd); +if (rmi_granule_delegate (rd_phys)) { + r = -ENXIO ; + goto free_rd; + } + +for (i =0 ; i < pgd_size; i +=RMM_PAGE_SIZE ) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i; + +if (rmi_granule_delegate (pgd_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + } + + params->s2sz =VTCR_EL2_IPA (kvm->arch.mmu.vtcr); + params->rtt_level_start =get_start_level (realm); + params->rtt_num_start = rtt_num_start; + params->rtt_base = kvm->arch.mmu.pgd_phys; + params->vmid = realm->vmid; + + params_phys =virt_to_phys (params); + +if (rmi_realm_create (rd_phys, params_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + +if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) { +WARN_ON (rmi_realm_destroy (rd_phys)); + r = -ENXIO ; + goto out_undelegate_tables; + } + + realm->rd = rd; + +return 0 ; + +out_undelegate_tables: +while (i >0 ) { + i -=RMM_PAGE_SIZE ; + +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) { +/* Leak the pages if they cannot be returned */ + kvm->arch.mmu.pgt =NULL ; +break ; + } + } +if (WARN_ON (rmi_granule_undelegate (rd_phys))) { +/* Leak the page if it isn't returned */ +return r; + } +free_rd: +free_page ((unsigned long )rd); +return r; +} + +/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{ +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ;
Quoted 8 lines
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 83
Quoted 4 lines
arch/arm64/kvm/rme.c +4
-0+ } + +return 0 ; +}Why do we need a VMID allocator? This is a different translation regime, which won't intersect with the host's. Surely the RMM can maintain its own?
I'm not sure exactly why it's in the current design, but as things stand the RMM specification requires that the host allocates the VMIDs and the RMM just validates that they don't overlap. As you say it's a different translation regime so these have no impact on the host's VMIDs.
Quoted 60 lines
Quoted 60 lines
arch/arm64/kvm/rme.c +60
-0+ +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (kvm_realm_is_created (kvm)) +return -EEXIST ; + + ret =rme_vmid_reserve (); +if (ret <0 ) +return ret; + realm->vmid = ret; + + ret =realm_create_rd (kvm); +if (ret) { +rme_vmid_release (realm->vmid); +return ret; + } + +WRITE_ONCE (realm->state,REALM_STATE_NEW ); + +/* The realm is up, free the parameters. */ +free_page ((unsigned long )realm->params); + realm->params =NULL ; + +return 0 ; +} + +static int config_realm_hash_algo (struct realm *realm, +struct arm_rme_config *cfg) +{ +switch (cfg->hash_algo) { +case ARM_RME_CONFIG_HASH_ALGO_SHA256 : +if (!rme_has_feature (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ; +case ARM_RME_CONFIG_HASH_ALGO_SHA512 : +if (!rme_has_feature (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ;
Quoted 8 lines
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 262
Quoted 6 lines
arch/arm64/kvm/rme.c +6
-0+default : +return -EINVAL ; + } + realm->params->hash_algo = cfg->hash_algo; +return 0 ; +}What is the purpose of this?
Specifically config_realm_hash_algo()? I thought that it was fairly obvious it's to allow the VMM to choose the hash-algorithm that the RMM uses (for the purpose of generating the attestation).
It follows the usual practise of checking that the system supports the requested value and returning -EINVAL if unsupported, otherwise stashing the value away to use at realm creation time. This also provides a mechanism for user space to probe the supported algorithms.
Quoted 3 lines
Overall, there is a lot here that needs to be explained, because I don't immediately understand what this stuff is all about. This file needs a large comment at the front explaining what is all that for.
Can you be more specific about the type of documentation you're interested in? I'll admit that having worked on this for quite some time a lot of it just seems 'obvious' to me now, but probably isn't really.
Quoted 1 line
Paste the whole spec in if that helps.
Tempting, but I'm fairly sure you'd be unhappy if I did! ;) And it could be easily argued that the spec doesn't really help explain what it's all about anyway.
Thanks,
Steve
PATCH v10 8/43 kvm: arm64: Don't expose debug capabilities for realm guests
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
RMM v1.0 provides no mechanism for the host to perform debug operations on the guest. So don't expose KVM_CAP_SET_GUEST_DEBUG and report 0 breakpoints and 0 watch points.
---
Changes since v7:
* Remove the helper functions and inline the kvm_is_realm() check with a ternary operator.
* Rewrite the commit message to explain this patch.
--- arch/arm64/kvm/arm.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm64/kvm/arm.c +5 -3
@@ -331,7 +331,6 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 :case KVM_CAP_ARM_NISV_TO_USER :case KVM_CAP_ARM_INJECT_EXT_DABT : -case KVM_CAP_SET_GUEST_DEBUG :case KVM_CAP_VCPU_ATTRIBUTES :case KVM_CAP_PTP_KVM :case KVM_CAP_ARM_SYSTEM_SUSPEND :
@@ -340,6 +339,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS : r =1 ;break ; +case KVM_CAP_SET_GUEST_DEBUG : + r = !kvm_is_realm (kvm); +break ;case KVM_CAP_SET_GUEST_DEBUG2 :return KVM_GUESTDBG_VALID_MASK ;case KVM_CAP_ARM_SET_DEVICE_ADDR :
@@ -391,10 +393,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =cpus_have_final_cap (ARM64_HAS_HCR_NV1 );break ;case KVM_CAP_GUEST_DEBUG_HW_BPS : - r =get_num_brps (); + r =kvm_is_realm (kvm) ?0 :get_num_brps ();break ;case KVM_CAP_GUEST_DEBUG_HW_WPS : - r =get_num_wrps (); + r =kvm_is_realm (kvm) ?0 :get_num_wrps ();break ;case KVM_CAP_ARM_PMU_V3 : r =kvm_supports_guest_pmuv3 ();
Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v10 08/43] kvm: arm64: Don't expose debug capabilities for realm guests
Quoted 6 lines · Steven Price
From: Suzuki K Poulose <[email protected]>
RMM v1.0 provides no mechanism for the host to perform debug operations on the guest. So don't expose
KVM_CAP_SET_GUEST_DEBUGand report 0 breakpoints and 0 watch points.
What is the guest seeing for the same things?
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 08/43] kvm: arm64: Don't expose debug capabilities for realm guests
Quoted 10 lines · Marc Zyngier
Quoted 6 lines · Steven Price
From: Suzuki K Poulose <[email protected]>
RMM v1.0 provides no mechanism for the host to perform debug operations on the guest. So don't expose
KVM_CAP_SET_GUEST_DEBUGand report 0 breakpoints and 0 watch points.What is the guest seeing for the same things?
The number of breakpoints/watchpoints is configured using the usual architectural register ID_AA64DFR0_EL1. So the VMM can configure the guest as it pleases to be able to debug itself.
Obviously CCA is about the host not seeing into the guest so debugging the guest is generally not permitted.
RMM v1.1 should provide some mechanisms for the host to debug a realm - but this would also change the attestation measurement so needs buy in from the guest's attestation flow. I don't think the RMM API for that is finalised yet, and I certainly don't have any Linux patches.
Thanks,
Steve
PATCH v10 9/43 KVM: arm64: Allow passing 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 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 | 16 ++++++++++++++-- arch/arm64/kvm/arm.c | 16 ++++++++++++++++ arch/arm64/kvm/mmu.c | 3 --- include/uapi/linux/kvm.h | 19 +++++++++++++++---- 4 files changed, 45 insertions(+), 9 deletions(-)
Documentation/virt/kvm/api.rst +14 -2
@@ -181,8 +181,20 @@ flag KVM_VM_MIPS_VZ. ARM64: ^^^^^^ -On arm64, the physical address size for a VM (IPA Size limit) is limited -to 40bits by default. The limit can be configured if the host supports the +On arm64, the machine type identifier is used to encode a type and the +physical address size for the VM. The lower byte (bits[7-0]) encode the +address size and the upper bits[11-8] encode a machine type. The machine +types that might be available are: + + ====================== ============================================ + KVM_VM_TYPE_ARM_NORMAL A standard VM + KVM_VM_TYPE_ARM_REALM A "Realm" VM using the Arm Confidential + Compute extensions, the VM's memory is + protected from the host. + ====================== ============================================ + +The physical address size for a VM (IPA Size limit) is limited to 40bits +by default. The limit can be configured if the host supports the extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use KVM_VM_TYPE_ARM_IPA_SIZE(IPA_Bits) to set the size in the machine type identifier, where IPA_Bits is the maximum width of any physical
arch/arm64/kvm/arm.c +16 -0
@@ -172,6 +172,22 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)mutex_unlock (&kvm->lock);#endif +if (type & ~(KVM_VM_TYPE_ARM_MASK |KVM_VM_TYPE_ARM_IPA_SIZE_MASK )) +return -EINVAL ; + +switch (type &KVM_VM_TYPE_ARM_MASK ) { +case KVM_VM_TYPE_ARM_NORMAL : +break ; +case KVM_VM_TYPE_ARM_REALM : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EPERM ; +WRITE_ONCE (kvm->arch.realm.state,REALM_STATE_NONE ); + kvm->arch.is_realm = true; +break ; +default : +return -EINVAL ; + } +kvm_init_nested (kvm); ret =kvm_share_hyp (kvm, kvm +1 );
arch/arm64/kvm/mmu.c +0 -3
@@ -881,9 +881,6 @@ static int kvm_init_ipa_range(struct kvm *kvm,if (kvm_is_realm (kvm)) kvm_ipa_limit =kvm_realm_ipa_limit (); -if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK ) -return -EINVAL ; - phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;
include/uapi/linux/kvm.h +15 -4
@@ -672,14 +672,25 @@ struct kvm_enable_cap {#define KVM_S390_SIE_PAGE_OFFSET 1/* -* On arm64, machine type can be used to request the physical -* address size for the VM. Bits[7-0] are reserved for the guest -* PA size shift (i.e, log2(PA_Size)). For backward compatibility, -* value 0 implies the default IPA size, 40bits. +* On arm64, machine type can be used to request both the machine type and +* the physical address size for the VM. +* +* Bits[11-8] are reserved for the ARM specific machine type. +* +* Bits[7-0] are reserved for the guest PA size shift (i.e, log2(PA_Size)). +* For backward compatibility, value 0 implies the default IPA size, 40bits. */ +#define KVM_VM_TYPE_ARM_SHIFT 8 +#define KVM_VM_TYPE_ARM_MASK (0xfULL << KVM_VM_TYPE_ARM_SHIFT) +#define KVM_VM_TYPE_ARM (_type) \ + (((_type) << KVM_VM_TYPE_ARM_SHIFT) & KVM_VM_TYPE_ARM_MASK) +#define KVM_VM_TYPE_ARM_NORMAL KVM_VM_TYPE_ARM(0) +#define KVM_VM_TYPE_ARM_REALM KVM_VM_TYPE_ARM(1) +#define KVM_VM_TYPE_ARM_IPA_SIZE_MASK 0xffULL#define KVM_VM_TYPE_ARM_IPA_SIZE (x) \ ((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK) +/* * ioctls for /dev/kvm fds: */
Signature · 1 line
2.43.0
Marc Zyngier
maintainerRe: [PATCH v10 09/43] KVM: arm64: Allow passing machine type in KVM creation
Quoted 60 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.Reviewed-by Suzuki K Poulose <[email protected]>Reviewed-by Gavin Shan <[email protected]>Signed-off-by Steven Price <[email protected]>---
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 | 16 ++++++++++++++-- arch/arm64/kvm/arm.c | 16 ++++++++++++++++ arch/arm64/kvm/mmu.c | 3 --- include/uapi/linux/kvm.h | 19 +++++++++++++++---- 4 files changed, 45 insertions(+), 9 deletions(-)Documentation/virt/kvm/api.rst +14
-2@@ -181,8 +181,20 @@ flag KVM_VM_MIPS_VZ. ARM64: ^^^^^^ -On arm64, the physical address size for a VM (IPA Size limit) is limited -to 40bits by default. The limit can be configured if the host supports the +On arm64, the machine type identifier is used to encode a type and the +physical address size for the VM. The lower byte (bits[7-0]) encode the +address size and the upper bits[11-8] encode a machine type. The machine +types that might be available are: + + ====================== ============================================ + KVM_VM_TYPE_ARM_NORMAL A standard VM + KVM_VM_TYPE_ARM_REALM A "Realm" VM using the Arm Confidential + Compute extensions, the VM's memory is + protected from the host. + ====================== ============================================ + +The physical address size for a VM (IPA Size limit) is limited to 40bits +by default. The limit can be configured if the host supports the extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use KVM_VM_TYPE_ARM_IPA_SIZE(IPA_Bits) to set the size in the machine type identifier, where IPA_Bits is the maximum width of any physicalarch/arm64/kvm/arm.c +0
-0@@ -172,6 +172,22 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)mutex_unlock (&kvm->lock);#endif
arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 183
Quoted 10 lines
arch/arm64/kvm/arm.c +9
-0+if (type & ~(KVM_VM_TYPE_ARM_MASK |KVM_VM_TYPE_ARM_IPA_SIZE_MASK )) +return -EINVAL ; + +switch (type &KVM_VM_TYPE_ARM_MASK ) { +case KVM_VM_TYPE_ARM_NORMAL : +break ; +case KVM_VM_TYPE_ARM_REALM : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EPERM ;
EPERM? That's rather odd. You can only do that if the CCA capability has been advertised. So asking for it when you can find that it is not there deserves more of an EINVAL response.
Quoted 15 lines
arch/arm64/kvm/arm.c +7
-0+WRITE_ONCE (kvm->arch.realm.state,REALM_STATE_NONE ); + kvm->arch.is_realm = true; +break ; +default : +return -EINVAL ; + } +kvm_init_nested (kvm); ret =kvm_share_hyp (kvm, kvm +1 );arch/arm64/kvm/mmu.c +0
-0
arch/arm64/kvm/mmu.c static int kvm_init_ipa_range(struct kvm *kvm, · line 886
Quoted 6 lines
arch/arm64/kvm/mmu.c +0
-3@@ -881,9 +881,6 @@ static int kvm_init_ipa_range(struct kvm *kvm,if (kvm_is_realm (kvm)) kvm_ipa_limit =kvm_realm_ipa_limit (); -if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK ) -return -EINVAL ; -
How about the *other* bits? You need to be able to detect that userspace is using the reserved bits and error out.
Quoted 19 lines
arch/arm64/kvm/mmu.c +0
-0phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;include/uapi/linux/kvm.h +4
-4@@ -672,14 +672,25 @@ struct kvm_enable_cap {#define KVM_S390_SIE_PAGE_OFFSET 1/* -* On arm64, machine type can be used to request the physical -* address size for the VM. Bits[7-0] are reserved for the guest -* PA size shift (i.e, log2(PA_Size)). For backward compatibility, -* value 0 implies the default IPA size, 40bits. +* On arm64, machine type can be used to request both the machine type and +* the physical address size for the VM. +* +* Bits[11-8] are reserved for the ARM specific machine type.
include/uapi/linux/kvm.h struct kvm_enable_cap { · line 688
Quoted 10 lines
include/uapi/linux/kvm.h +9
-0+* +* Bits[7-0] are reserved for the guest PA size shift (i.e, log2(PA_Size)). +* For backward compatibility, value 0 implies the default IPA size, 40bits. */ +#define KVM_VM_TYPE_ARM_SHIFT 8 +#define KVM_VM_TYPE_ARM_MASK (0xfULL << KVM_VM_TYPE_ARM_SHIFT) +#define KVM_VM_TYPE_ARM (_type) \ + (((_type) << KVM_VM_TYPE_ARM_SHIFT) & KVM_VM_TYPE_ARM_MASK) +#define KVM_VM_TYPE_ARM_NORMAL KVM_VM_TYPE_ARM(0) +#define KVM_VM_TYPE_ARM_REALM KVM_VM_TYPE_ARM(1)
Why can't that be just "PROTECTED", using bit 31, just like pKVM? I don't see the point in deviating from an established practice.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 09/43] KVM: arm64: Allow passing machine type in KVM creation
Quoted 63 lines · Marc Zyngier
Quoted 61 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.Reviewed-by Suzuki K Poulose <[email protected]>Reviewed-by Gavin Shan <[email protected]>Signed-off-by Steven Price <[email protected]>---
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 | 16 ++++++++++++++-- arch/arm64/kvm/arm.c | 16 ++++++++++++++++ arch/arm64/kvm/mmu.c | 3 --- include/uapi/linux/kvm.h | 19 +++++++++++++++---- 4 files changed, 45 insertions(+), 9 deletions(-)Documentation/virt/kvm/api.rst +14
-2@@ -181,8 +181,20 @@ flag KVM_VM_MIPS_VZ. ARM64: ^^^^^^ -On arm64, the physical address size for a VM (IPA Size limit) is limited -to 40bits by default. The limit can be configured if the host supports the +On arm64, the machine type identifier is used to encode a type and the +physical address size for the VM. The lower byte (bits[7-0]) encode the +address size and the upper bits[11-8] encode a machine type. The machine +types that might be available are: + + ====================== ============================================ + KVM_VM_TYPE_ARM_NORMAL A standard VM + KVM_VM_TYPE_ARM_REALM A "Realm" VM using the Arm Confidential + Compute extensions, the VM's memory is + protected from the host. + ====================== ============================================ + +The physical address size for a VM (IPA Size limit) is limited to 40bits +by default. The limit can be configured if the host supports the extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use KVM_VM_TYPE_ARM_IPA_SIZE(IPA_Bits) to set the size in the machine type identifier, where IPA_Bits is the maximum width of any physicalarch/arm64/kvm/arm.c +0
-0@@ -172,6 +172,22 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)mutex_unlock (&kvm->lock);#endif
Quoted 13 lines
arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 183
Quoted 9 lines
arch/arm64/kvm/arm.c +9
-0+if (type & ~(KVM_VM_TYPE_ARM_MASK |KVM_VM_TYPE_ARM_IPA_SIZE_MASK )) +return -EINVAL ; + +switch (type &KVM_VM_TYPE_ARM_MASK ) { +case KVM_VM_TYPE_ARM_NORMAL : +break ; +case KVM_VM_TYPE_ARM_REALM : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EPERM ;EPERM? That's rather odd. You can only do that if the CCA capability has been advertised. So asking for it when you can find that it is not there deserves more of an EINVAL response.
Ack
Quoted 18 lines
Quoted 18 lines
arch/arm64/kvm/arm.c +7
-0+WRITE_ONCE (kvm->arch.realm.state,REALM_STATE_NONE ); + kvm->arch.is_realm = true; +break ; +default : +return -EINVAL ; + } +kvm_init_nested (kvm); ret =kvm_share_hyp (kvm, kvm +1 );arch/arm64/kvm/mmu.c +0
-0@@ -881,9 +881,6 @@ static int kvm_init_ipa_range(struct kvm *kvm,if (kvm_is_realm (kvm)) kvm_ipa_limit =kvm_realm_ipa_limit ();
Quoted 6 lines
arch/arm64/kvm/mmu.c static int kvm_init_ipa_range(struct kvm *kvm, · line 886
Quoted 3 lines
arch/arm64/kvm/mmu.c +0
-3-if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK ) -return -EINVAL ; -How about the *other* bits? You need to be able to detect that userspace is using the reserved bits and error out.
That check is now further up in kvm_arch_init_vm():
Quoted 2 lines
arch/arm64/kvm/mmu.c static int kvm_init_ipa_range(struct kvm *kvm, · line 885
Quoted 2 lines
arch/arm64/kvm/mmu.c +2
-0+if (type & ~(KVM_VM_TYPE_ARM_MASK |KVM_VM_TYPE_ARM_IPA_SIZE_MASK )) +return -EINVAL ;
(along with the default branch in the switch handling unknown 'types')
Quoted 24 lines
Quoted 24 lines
arch/arm64/kvm/mmu.c +0
-0phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;include/uapi/linux/kvm.h +8
-4@@ -672,14 +672,25 @@ struct kvm_enable_cap {#define KVM_S390_SIE_PAGE_OFFSET 1/* -* On arm64, machine type can be used to request the physical -* address size for the VM. Bits[7-0] are reserved for the guest -* PA size shift (i.e, log2(PA_Size)). For backward compatibility, -* value 0 implies the default IPA size, 40bits. +* On arm64, machine type can be used to request both the machine type and +* the physical address size for the VM. +* +* Bits[11-8] are reserved for the ARM specific machine type. +* +* Bits[7-0] are reserved for the guest PA size shift (i.e, log2(PA_Size)). +* For backward compatibility, value 0 implies the default IPA size, 40bits. */ +#define KVM_VM_TYPE_ARM_SHIFT 8
Quoted 8 lines
include/uapi/linux/kvm.h struct kvm_enable_cap { · line 688
Quoted 5 lines
include/uapi/linux/kvm.h +5
-0+#define KVM_VM_TYPE_ARM_MASK (0xfULL << KVM_VM_TYPE_ARM_SHIFT) +#define KVM_VM_TYPE_ARM (_type) \ + (((_type) << KVM_VM_TYPE_ARM_SHIFT) & KVM_VM_TYPE_ARM_MASK) +#define KVM_VM_TYPE_ARM_NORMAL KVM_VM_TYPE_ARM(0) +#define KVM_VM_TYPE_ARM_REALM KVM_VM_TYPE_ARM(1)Why can't that be just "PROTECTED", using bit 31, just like pKVM? I don't see the point in deviating from an established practice.
As far as I'm aware this pKVM practise isn't upstream. We also need to distinguish between pKVM and CCA - it's entirely possible that both might coexist on the same platform.
This is just extending a proposal that Will posted years ago[1] for pkvm with room for more types - that used bit 8 for a KVM_VM_TYPE_ARM_PROTECTED flag. I agree there might be an argument for the realm type being 2 if we want to keep 1 for pKVM. But it looks like downstream pKVM has done something different and incompatible anyway.
Thanks,
Steve
[1]
https://lists.infradead.org/pipermail/linux-arm-kernel/2022-May/744865.html
PATCH v10 10/43 arm64: RME: RTT tear down
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 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_rme.h | 7 ++ arch/arm64/kvm/mmu.c | 6 +- arch/arm64/kvm/rme.c | 140 +++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rme.h +7 -0
@@ -71,5 +71,12 @@ u32 kvm_realm_ipa_limit(void);int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int kvm_init_realm_vm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm); +void kvm_realm_destroy_rtts (struct kvm *kvm,u32 ia_bits); + +static inline bool kvm_realm_is_private_address (struct realm *realm, +unsigned long addr) +{ +return !(addr &BIT (realm->ia_bits -1 )); +}#endif /* __ASM_KVM_RME_H */
arch/arm64/kvm/mmu.c +3 -3
@@ -1065,14 +1065,15 @@ void stage2_unmap_vm(struct kvm *kvm)void kvm_free_stage2_pgd (struct kvm_s2_mmu *mmu) {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); -struct kvm_pgtable *pgt =NULL ; +struct kvm_pgtable *pgt;write_lock (&kvm->mmu_lock); + pgt = mmu->pgt;if (kvm_is_realm (kvm) && (kvm_realm_state (kvm) !=REALM_STATE_DEAD &&kvm_realm_state (kvm) !=REALM_STATE_NONE )) { -/* Tearing down RTTs will be added in a later patch */ write_unlock (&kvm->mmu_lock); +kvm_realm_destroy_rtts (kvm, pgt->ia_bits);/* * The PGD pages can be reclaimed only after the realm (RD) is
@@ -1081,7 +1082,6 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)*/ return ; } - pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ; mmu->pgt =NULL ;
arch/arm64/kvm/rme.c +140 -0
152 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v10 11/43 arm64: RME: Allocate/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 must also be supplied with addition pages - auxiliary (or AUX) granules - for storing the larger registers state (e.g. for SVE). The number of AUX granules for a REC depends on the parameters with which the Realm was created - the RMM makes this information available via the RMI_REC_AUX_COUNT call performed after creating the Realm Descriptor (RD).
Note that only some of register state for the REC can be set by KVM, the rest is defined by the RMM (zeroed). The register state then cannot be changed by KVM after the REC is created (except when the guest explicitly requests this e.g. by performing a PSCI call). The RMM also requires that the VMM creates RECs in ascending order of the MPIDR.
See Realm Management Monitor specification (DEN0137) for more information:
https://developer.arm.com/documentation/den0137/
---
Changes since 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 | 7 ++ arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/include/asm/kvm_rme.h | 23 ++++ arch/arm64/kvm/arm.c | 13 +- arch/arm64/kvm/reset.c | 11 ++ arch/arm64/kvm/rme.c | 180 +++++++++++++++++++++++++++ 6 files changed, 235 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +7 -0
@@ -694,7 +694,14 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) { +if (static_branch_unlikely (&kvm_rme_is_available)) +return vcpu_has_feature (vcpu,KVM_ARM_VCPU_REC );return false; } +static inline bool kvm_arm_rec_finalized (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.rec.mpidr !=INVALID_HWID ; +} +#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -891,6 +891,9 @@ struct kvm_vcpu_arch {/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */ struct vncr_tlb *vncr_tlb; + +/* Realm meta data */ +struct realm_rec rec; };/*
arch/arm64/include/asm/kvm_rme.h +23 -0
@@ -6,6 +6,7 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H +#include <asm/rmi_smc.h> #include <uapi/linux/kvm.h> /**
@@ -65,6 +66,26 @@ struct realm {unsigned int ia_bits; }; +/** +* struct realm_rec - Additional per VCPU data for a Realm +* +* @mpidr: MPIDR (Multiprocessor Affinity Register) value to identify this VCPU +* @rec_page: Kernel VA of the RMM's private page for this REC +* @aux_pages: Additional pages private to the RMM for this REC +* @run: Kernel VA of the RmiRecRun structure shared with the RMM +*/ +struct realm_rec { +unsigned long mpidr; +void *rec_page; +/* +* REC_PARAMS_AUX_GRANULES is the maximum number of 4K granules that +* the RMM can require. The array is sized to be large enough for the +* maximum number of host sized pages that could be required. +*/ +struct page *aux_pages[(REC_PARAMS_AUX_GRANULES *SZ_4K ) >>PAGE_SHIFT ]; +struct rec_run *run; +}; +void kvm_init_rme (void );u32 kvm_realm_ipa_limit (void );
@@ -72,6 +93,8 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);int kvm_init_realm_vm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm);void kvm_realm_destroy_rtts (struct kvm *kvm,u32 ia_bits); +int kvm_create_rec (struct kvm_vcpu *vcpu); +void kvm_destroy_rec (struct kvm_vcpu *vcpu);static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr)
arch/arm64/kvm/arm.c +11 -2
@@ -503,6 +503,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 */
@@ -1466,7 +1468,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,return -EINVAL ; } -static unsigned long system_supported_vcpu_features (void ) +static unsigned long system_supported_vcpu_features (struct kvm *kvm) {unsigned long features =KVM_VCPU_VALID_FEATURES ;
@@ -1487,6 +1489,9 @@ static unsigned long system_supported_vcpu_features(void)if (!cpus_have_final_cap (ARM64_HAS_NESTED_VIRT ))clear_bit (KVM_ARM_VCPU_HAS_EL2 , &features); +if (!kvm_is_realm (kvm)) +clear_bit (KVM_ARM_VCPU_REC , &features); +return features; }
@@ -1504,7 +1509,7 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,return -ENOENT ; } -if (features & ~system_supported_vcpu_features ()) +if (features & ~system_supported_vcpu_features (vcpu->kvm))return -EINVAL ;/*
@@ -1526,6 +1531,10 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,if (test_bit (KVM_ARM_VCPU_HAS_EL2 , &features))return -EINVAL ; +/* RME is incompatible with AArch32 */ +if (test_bit (KVM_ARM_VCPU_REC , &features)) +return -EINVAL ; +return 0 ; }
arch/arm64/kvm/reset.c +11 -0
@@ -137,6 +137,11 @@ int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)return -EPERM ;return kvm_vcpu_finalize_sve (vcpu); +case KVM_ARM_VCPU_REC : +if (!kvm_is_realm (vcpu->kvm) || !vcpu_is_rec (vcpu)) +return -EINVAL ; + +return kvm_create_rec (vcpu); }return -EINVAL ;
@@ -147,6 +152,11 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)if (vcpu_has_sve (vcpu) && !kvm_arm_vcpu_sve_finalized (vcpu))return false; +if (kvm_is_realm (vcpu->kvm) && + !(vcpu_is_rec (vcpu) &&kvm_arm_rec_finalized (vcpu) && +READ_ONCE (vcpu->kvm->arch.realm.state) ==REALM_STATE_ACTIVE )) +return false; +return true; }
@@ -161,6 +171,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/rme.c +180 -0
186 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v10 12/43 KVM: arm64: vgic: Provide helper for number of list registers
In reply to Steven PriceCurrently the number of list registers available is stored in a global (kvm_vgic_global_state.nr_lr). With Arm CCA the RMM is permitted to reserve list registers for its own use and so the number of available list registers can be fewer for a realm VM. Provide a wrapper function to fetch the global in preparation for restricting nr_lr when dealing with a realm VM.
---
New patch for v6
--- arch/arm64/kvm/vgic/vgic.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic.c +8 -3
@@ -21,6 +21,11 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = { .gicv3_cpuif = STATIC_KEY_FALSE_INIT, }; +static inline int kvm_vcpu_vgic_nr_lr (struct kvm_vcpu *vcpu) +{ +return kvm_vgic_global_state.nr_lr; +} +/* * Locking order is always: * kvm->lock (mutex)
@@ -802,7 +807,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)lockdep_assert_held (&vgic_cpu->ap_list_lock); count =compute_ap_list_depth (vcpu, &multi_sgi); -if (count > kvm_vgic_global_state.nr_lr || multi_sgi) +if (count >kvm_vcpu_vgic_nr_lr (vcpu) || multi_sgi)vgic_sort_ap_list (vcpu); count =0 ;
@@ -831,7 +836,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)raw_spin_unlock (&irq->irq_lock); -if (count == kvm_vgic_global_state.nr_lr) { +if (count ==kvm_vcpu_vgic_nr_lr (vcpu)) {if (!list_is_last (&irq->ap_list, &vgic_cpu->ap_list_head))vgic_set_underflow (vcpu);
@@ -840,7 +845,7 @@ static void vgic_flush_lr_state(struct kvm_vcpu *vcpu) }/* Nuke remaining LRs */ -for (i = count ; i < kvm_vgic_global_state.nr_lr; i++) +for (i = count; i <kvm_vcpu_vgic_nr_lr (vcpu); i++)vgic_clear_lr (vcpu, i);if (!static_branch_unlikely (&kvm_vgic_global_state.gicv3_cpuif))
Signature · 1 line
2.43.0
PATCH v10 13/43 arm64: RME: Support for the VGIC in realms
In reply to Steven PriceThe RMM provides emulation of a VGIC to the realm guest but delegates much of the handling to the host. Implement support in KVM for saving/restoring state to/from the REC structure.
---
Changes from 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/include/asm/kvm_rme.h | 1 + arch/arm64/kvm/arm.c | 16 ++++++++-- arch/arm64/kvm/rme.c | 5 ++++ arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/kvm/vgic/vgic.c | 50 ++++++++++++++++++++++++++++++-- 5 files changed, 68 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_rme.h +1 -0
@@ -88,6 +88,7 @@ struct realm_rec {void kvm_init_rme (void );u32 kvm_realm_ipa_limit (void ); +u32 kvm_realm_vgic_nr_lr (void );int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int kvm_init_realm_vm (struct kvm *kvm);
arch/arm64/kvm/arm.c +13 -3
@@ -697,19 +697,24 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)kvm_call_hyp_nvhe (__pkvm_vcpu_put); } +kvm_timer_vcpu_put (vcpu); +kvm_vgic_put (vcpu); + + vcpu->cpu =-1 ; + +if (vcpu_is_rec (vcpu)) +return ; +kvm_vcpu_put_debug (vcpu);kvm_arch_vcpu_put_fp (vcpu);if (has_vhe ())kvm_vcpu_put_vhe (vcpu); -kvm_timer_vcpu_put (vcpu); -kvm_vgic_put (vcpu);kvm_vcpu_pmu_restore_host (vcpu);if (vcpu_has_nv (vcpu))kvm_vcpu_put_hw_mmu (vcpu);kvm_arm_vmid_clear_active ();vcpu_clear_on_unsupported_cpu (vcpu); - vcpu->cpu =-1 ; }static void __kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu)
@@ -927,6 +932,11 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)return ret; } +if (!irqchip_in_kernel (kvm) &&kvm_is_realm (vcpu->kvm)) { +/* Userspace irqchip not yet supported with Realms */ +return -EOPNOTSUPP ; + } +mutex_lock (&kvm->arch.config_lock);set_bit (KVM_ARCH_FLAG_HAS_RAN_ONCE , &kvm->arch.flags);mutex_unlock (&kvm->arch.config_lock);
arch/arm64/kvm/rme.c +5 -0
@@ -77,6 +77,11 @@ u32 kvm_realm_ipa_limit(void)return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_S2SZ ); } +u32 kvm_realm_vgic_nr_lr (void ) +{ +return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS ); +} +static int get_start_level (struct realm *realm) {/*
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 ;/*
arch/arm64/kvm/vgic/vgic.c +48 -2
95 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v10 14/43 KVM: arm64: 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.
The RMM doesn't provide a mechanism to set the counter offset, so don't expose KVM_CAP_COUNTER_OFFSET for a realm VM.
A later patch adds the support for propagating the timer values from the exit data structure and calling kvm_realm_timers_update().
---
Changes since 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 | 44 ++++++++++++++++++++++++++++++++++-- arch/arm64/kvm/arm.c | 2 +- include/kvm/arm_arch_timer.h | 2 ++ 3 files changed, 45 insertions(+), 3 deletions(-)
arch/arm64/kvm/arch_timer.c +42 -2
@@ -148,6 +148,13 @@ static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)static void timer_set_offset (struct arch_timer_context *ctxt,u64 offset) { +struct kvm_vcpu *vcpu = ctxt->vcpu; + +if (kvm_is_realm (vcpu->kvm)) { +WARN_ON (offset); +return ; + } +if (!ctxt->offset.vm_offset) {WARN (offset,"timer %ld\n" ,arch_timer_ctx_index (ctxt));return ;
@@ -462,6 +469,21 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, timer_ctx); } +void kvm_realm_timers_update (struct kvm_vcpu *vcpu) +{ +struct arch_timer_cpu *arch_timer = &vcpu->arch.timer_cpu; +int i; + +for (i =0 ; i <NR_KVM_EL0_TIMERS ; i++) { +struct arch_timer_context *timer = &arch_timer->timers[i]; +bool status =timer_get_ctl (timer) &ARCH_TIMER_CTRL_IT_STAT ; +bool level =kvm_timer_irq_can_fire (timer) && status; + +if (level != timer->irq.level) +kvm_timer_update_irq (vcpu, level, timer); + } +} +/* Only called for a fully emulated timer */ static void timer_emulate (struct arch_timer_context *ctx) {
@@ -1065,7 +1087,9 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid) ctxt->vcpu = vcpu; -if (timerid ==TIMER_VTIMER ) +if (kvm_is_realm (vcpu->kvm)) + ctxt->offset.vm_offset =NULL ; +else if (timerid ==TIMER_VTIMER ) ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;else ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
@@ -1087,13 +1111,19 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer =vcpu_timer (vcpu); +u64 cntvoff;for (int i =0 ; i <NR_KVM_TIMERS ; i++)timer_context_init (vcpu, i); +if (kvm_is_realm (vcpu->kvm)) + cntvoff =0 ; +else + cntvoff =kvm_phys_timer_read (); +/* Synchronize offsets across timers of a VM if not already provided */ if (!test_bit (KVM_ARCH_FLAG_VM_COUNTER_OFFSET , &vcpu->kvm->arch.flags)) { -timer_set_offset (vcpu_vtimer (vcpu),kvm_phys_timer_read ()); +timer_set_offset (vcpu_vtimer (vcpu), cntvoff);timer_set_offset (vcpu_ptimer (vcpu),0 ); }
@@ -1633,6 +1663,13 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)return -EINVAL ; } +/* +* We don't use mapped IRQs for Realms because the RMI doesn't allow +* us setting the LR.HW bit in the VGIC. +*/ +if (vcpu_is_rec (vcpu)) +return 0 ; +get_timer_map (vcpu, &map); ret = kvm_vgic_map_phys_irq(vcpu,
@@ -1764,6 +1801,9 @@ int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm,if (offset->reserved)return -EINVAL ; +if (kvm_is_realm (kvm)) +return -EINVAL ; +mutex_lock (&kvm->lock);if (!kvm_trylock_all_vcpus (kvm)) {
arch/arm64/kvm/arm.c +1 -1
@@ -351,10 +351,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_PTP_KVM :case KVM_CAP_ARM_SYSTEM_SUSPEND :case KVM_CAP_IRQFD_RESAMPLE : -case KVM_CAP_COUNTER_OFFSET :case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS : r =1 ;break ; +case KVM_CAP_COUNTER_OFFSET :case KVM_CAP_SET_GUEST_DEBUG : r = !kvm_is_realm (kvm);break ;
include/kvm/arm_arch_timer.h +2 -0
@@ -113,6 +113,8 @@ int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);int kvm_arm_timer_get_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr);int kvm_arm_timer_has_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr); +void kvm_realm_timers_update (struct kvm_vcpu *vcpu); +u64 kvm_phys_timer_read (void );void kvm_timer_vcpu_load (struct kvm_vcpu *vcpu);
Signature · 1 line
2.43.0
PATCH v10 15/43 arm64: RME: Allow VMM to set RIPAS
In reply to Steven PriceEach page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.
When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.
When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.
---
Changes from v9:
* Minor coding style fixes.
Changes from v8:
* Propagate the 'may_block' flag to allow conditional calls to cond_resched_rwlock_write().
* Introduce alloc_rtt() to wrap alloc_delegated_granule() and kvm_account_pgtable_pages() and use when allocating RTTs.
* Code reorganisation to allow init_ipa_state and set_ipa_state to share a common ripas_change() function,
* Other minor changes following review.
Changes from v7:
* Replace use of "only_shared" with the upstream "attr_filter" field of struct kvm_gfn_range.
* Clean up the logic in alloc_delegated_granule() for when to call kvm_account_pgtable_pages().
* Rename realm_destroy_protected_granule() to realm_destroy_private_granule() to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
* Several other minor changes to names/return codes.
Changes from v6:
* Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
* Drop the useless flags argument from alloc_delegated_granule().
* Account RTTs allocated for a guest using kvm_account_pgtable_pages().
* Deal with the RMM granule size potentially being smaller than the host's PAGE_SIZE. Although note alloc_delegated_granule() currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
* Adapt to rebasing.
* Introduce find_map_level()
* Rename some functions to be clearer.
* Drop the "spare page" functionality.
Changes from v2:
* {alloc,free}_delegated_page() moved from previous patch to this one.
* alloc_delegated_page() now takes a gfp_t flags parameter.
* Fix the reference counting of guestmem pages to avoid leaking memory.
* Several misc code improvements and extra comments.
--- arch/arm64/include/asm/kvm_rme.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rme.c | 446 +++++++++++++++++++++++++++++++ 3 files changed, 457 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rme.h +6 -0
@@ -97,6 +97,12 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);int kvm_create_rec (struct kvm_vcpu *vcpu);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +void kvm_realm_unmap_range (struct kvm *kvm, +unsigned long ipa, +unsigned long size, +bool unmap_private, +bool may_block); +static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr) {
arch/arm64/kvm/mmu.c +5 -3
@@ -318,6 +318,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
@@ -325,7 +326,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;
@@ -339,7 +340,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64void kvm_stage2_unmap_range (struct kvm_s2_mmu *mmu,phys_addr_t start,u64 size,bool may_block) { -__unmap_stage2_range (mmu, start, size, may_block); +__unmap_stage2_range (mmu, start, size, may_block, false); }void kvm_stage2_flush_range (struct kvm_s2_mmu *mmu,phys_addr_t addr,phys_addr_t end)
@@ -2015,7 +2016,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)__unmap_stage2_range (&kvm->arch.mmu, range->start <<PAGE_SHIFT , (range->end - range->start) <<PAGE_SHIFT , - range->may_block); + range->may_block, + !(range->attr_filter &KVM_FILTER_PRIVATE ));kvm_nested_s2_unmap (kvm, range->may_block);return false;
arch/arm64/kvm/rme.c +446 -0
488 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v10 15/43] arm64: RME: Allow VMM to set RIPAS
Quoted 58 lines · Steven Price
Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime.
When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range.
When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed.
Signed-off-by Steven Price <[email protected]>---
Changes from v9:
* Minor coding style fixes.
Changes from v8:
* Propagate the 'may_block' flag to allow conditional calls tocond_resched_rwlock_write().
* Introducealloc_rtt()to wrapalloc_delegated_granule()andkvm_account_pgtable_pages()and use when allocating RTTs.
* Code reorganisation to allowinit_ipa_stateandset_ipa_stateto share a commonripas_change()function,
* Other minor changes following review.
Changes from v7:
* Replace use of "only_shared" with the upstream "attr_filter" field of structkvm_gfn_range.
* Clean up the logic inalloc_delegated_granule()for when to callkvm_account_pgtable_pages().
* Renamerealm_destroy_protected_granule()torealm_destroy_private_granule()to match the naming elsewhere. Also fix the return codes in the function to be descriptive.
* Several other minor changes to names/return codes.
Changes from v6:
* Split the code dealing with the guest triggering a RIPAS change into a separate patch, so this patch is purely for the VMM setting up the RIPAS before the guest first runs.
* Drop the useless flags argument fromalloc_delegated_granule().
* Account RTTs allocated for a guest usingkvm_account_pgtable_pages().
* Deal with the RMM granule size potentially being smaller than the host'sPAGE_SIZE. Although notealloc_delegated_granule()currently still allocates an entire host page for every RMM granule (so wasting memory when PAGE_SIZE>4k).
Changes from v5:
* Adapt to rebasing.
* Introducefind_map_level()
* Rename some functions to be clearer.
* Drop the "spare page" functionality.
Changes from v2:
* {alloc,free}_delegated_page() moved from previous patch to this one.
*alloc_delegated_page()now takes agfp_tflags parameter.
* Fix the reference counting of guestmem pages to avoid leaking memory.
* Several misc code improvements and extra comments.
Quoted 6 lines
--- arch/arm64/include/asm/kvm_rme.h | 6 + arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rme.c | 446 +++++++++++++++++++++++++++++++ 3 files changed, 457 insertions(+), 3 deletions(-)
PATCH v10 16/43 arm64: RME: 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 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_rme.h | 4 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 22 +++- arch/arm64/kvm/rme-exit.c | 178 +++++++++++++++++++++++++++++++ arch/arm64/kvm/rme.c | 38 +++++++ 5 files changed, 239 insertions(+), 5 deletions(-) create mode 100644 arch/arm64/kvm/rme-exit.c
arch/arm64/include/asm/kvm_rme.h +4 -0
@@ -97,6 +97,10 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits);int kvm_create_rec (struct kvm_vcpu *vcpu);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +int kvm_rec_enter (struct kvm_vcpu *vcpu); +int kvm_rec_pre_enter (struct kvm_vcpu *vcpu); +int handle_rec_exit (struct kvm_vcpu *vcpu,int rec_run_status); + void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, unsigned long size,
arch/arm64/kvm/Makefile +1 -1
@@ -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 rme.o \ + vgic-sys-reg-v3.o fpsimd.o pkvm.o rme.o rme-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 +18 -4
@@ -1233,6 +1233,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)if (ret >0 ) ret =check_vcpu_requests (vcpu); +if (ret >0 &&vcpu_is_rec (vcpu)) + ret =kvm_rec_pre_enter (vcpu); +/* * Preparing the interrupts to be injected also * involves poking the GIC, which must be done in a
@@ -1280,7 +1283,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)trace_kvm_entry (*vcpu_pc (vcpu));guest_timing_enter_irqoff (); - ret =kvm_arm_vcpu_enter_exit (vcpu); +if (vcpu_is_rec (vcpu)) + ret =kvm_rec_enter (vcpu); +else + ret =kvm_arm_vcpu_enter_exit (vcpu); vcpu->mode =OUTSIDE_GUEST_MODE ; vcpu->stat.exits++;
@@ -1338,8 +1344,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)trace_kvm_exit (ret,kvm_vcpu_trap_get_class (vcpu), *vcpu_pc (vcpu)); -/* Exit types that need handling before we can be preempted */ -handle_exit_early (vcpu, ret); +if (!vcpu_is_rec (vcpu)) { +/* +* Exit types that need handling before we can be +* preempted +*/ +handle_exit_early (vcpu, ret); + }kvm_nested_sync_hwstate (vcpu);
@@ -1364,7 +1375,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) ret =ARM_EXCEPTION_IL ; } - ret =handle_exit (vcpu, ret); +if (vcpu_is_rec (vcpu)) + ret =handle_rec_exit (vcpu, ret); +else + ret =handle_exit (vcpu, ret); }/* Tell userspace about in-kernel device output levels */
arch/arm64/kvm/rme-exit.c new file +178 -0
178 lines not on this page · raw message
arch/arm64/kvm/rme.c +38 -0
@@ -940,6 +940,44 @@ void kvm_destroy_realm(struct kvm *kvm)kvm_free_stage2_pgd (&kvm->arch.mmu); } +/* +* kvm_rec_pre_enter - Complete operations before entering a REC +* +* Some operations require work to be completed before entering a realm. That +* work may require memory allocation so cannot be done in the kvm_rec_enter() +* call. +* +* Return: 1 if we should enter the guest +* 0 if we should exit to userspace +* < 0 if we should exit to userspace, where the return value indicates +* an error +*/ +int kvm_rec_pre_enter (struct kvm_vcpu *vcpu) +{ +struct realm_rec *rec = &vcpu->arch.rec; + +if (kvm_realm_state (vcpu->kvm) !=REALM_STATE_ACTIVE ) +return -EINVAL ; + +switch (rec->run->exit.exit_reason) { +case RMI_EXIT_HOST_CALL : +case RMI_EXIT_PSCI : +for (int i =0 ; i <REC_RUN_GPRS ; i++) + rec->run->enter.gprs[i] =vcpu_get_reg (vcpu, i); +break ; + } + +return 1 ; +} + +int kvm_rec_enter (struct kvm_vcpu *vcpu) +{ +struct realm_rec *rec = &vcpu->arch.rec; + +return rmi_rec_enter (virt_to_phys (rec->rec_page), +virt_to_phys (rec->run)); +} +static void free_rec_aux (struct page **aux_pages,unsigned int num_aux) {
Signature · 1 line
2.43.0
PATCH v10 17/43 arm64: RME: Handle RMI_EXIT_RIPAS_CHANGE
In reply to Steven PriceThe guest can request that a region of it's protected address space is switched between RIPAS_RAM and RIPAS_EMPTY (and back) using RSI_IPA_STATE_SET. This causes a guest exit with the RMI_EXIT_RIPAS_CHANGE code. We treat this as a request to convert a protected region to unprotected (or back), exiting to the VMM to make the necessary changes to the guest_memfd and memslot mappings. On the next entry the RIPAS changes are committed by making RMI_RTT_SET_RIPAS calls.
The VMM may wish to reject the RIPAS change requested by the guest. For now it can only do with by no longer scheduling the VCPU as we don't currently have a usecase for returning that rejection to the guest, but by postponing the RMI_RTT_SET_RIPAS changes to entry we leave the door open for adding a new ioctl in the future for this purpose.
---
Changes since 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/kvm/rme.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
arch/arm64/kvm/rme.c +46 -0
@@ -733,6 +733,21 @@ static int ripas_change(struct kvm *kvm,return 0 ; } +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 (ripas ==RMI_EMPTY && *top_ipa != start) +realm_unmap_private_range (kvm, start, *top_ipa, false); + +return ret; +} +static int realm_init_ipa_state (struct kvm *kvm,unsigned long ipa,unsigned long end)
@@ -940,6 +955,34 @@ void kvm_destroy_realm(struct kvm *kvm)kvm_free_stage2_pgd (&kvm->arch.mmu); } +static void kvm_complete_ripas_change (struct kvm_vcpu *vcpu) +{ +struct kvm *kvm = vcpu->kvm; +struct realm_rec *rec = &vcpu->arch.rec; +unsigned long base = rec->run->exit.ripas_base; +unsigned long top = rec->run->exit.ripas_top; +unsigned long ripas = rec->run->exit.ripas_value; +unsigned long top_ipa; +int ret; + +do { +kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_page_cache, +kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu)); +write_lock (&kvm->mmu_lock); + ret =realm_set_ipa_state (vcpu, base, top, ripas, &top_ipa); +write_unlock (&kvm->mmu_lock); + +if (WARN_RATELIMIT (ret && ret != -ENOMEM , +"Unable to satisfy RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n" , + base, top, ripas)) +break ; + + base = top_ipa; + }while (base < top); + + rec->run->exit.ripas_base = base; +} +/* * kvm_rec_pre_enter - Complete operations before entering a REC *
@@ -965,6 +1008,9 @@ int kvm_rec_pre_enter(struct kvm_vcpu *vcpu)for (int i =0 ; i <REC_RUN_GPRS ; i++) rec->run->enter.gprs[i] =vcpu_get_reg (vcpu, i);break ; +case RMI_EXIT_RIPAS_CHANGE : +kvm_complete_ripas_change (vcpu); +break ; }return 1 ;
Signature · 1 line
2.43.0
PATCH v10 18/43 KVM: arm64: 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 v7:
* New comment for rec_exit_sync_dabt() explaining the call to vcpu_set_reg().
Changes since v5:
* Inject SEA to the guest is an emulatable MMIO access triggers a data abort.
* kvm_handle_mmio_return() - disable kvm_incr_pc() for a REC (as the PC isn't under the host's control) and move the REC_ENTER_EMULATED_MMIO flag setting to this location (as that tells the RMM to skip the instruction).
--- arch/arm64/kvm/inject_fault.c | 4 +++- arch/arm64/kvm/mmio.c | 16 ++++++++++++---- arch/arm64/kvm/rme-exit.c | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-)
arch/arm64/kvm/inject_fault.c +3 -1
@@ -201,7 +201,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 <asm/rmi_smc.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 ; }
@@ -164,14 +172,14 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)* No valid syndrome? Ask userspace for help if it has * volunteered to do so, and bail out otherwise. * -* In the protected VM case, there isn't much userspace can do +* In the protected/realm VM case, there isn't much userspace can do * though, so directly deliver an exception to the guest. */ if (!kvm_vcpu_dabt_isvalid (vcpu)) {trace_kvm_mmio_nisv (*vcpu_pc (vcpu),kvm_vcpu_get_esr (vcpu),kvm_vcpu_get_hfar (vcpu), fault_ipa); -if (vcpu_is_protected (vcpu)) +if (vcpu_is_protected (vcpu) ||vcpu_is_rec (vcpu))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/rme-exit.c +14 -0
@@ -25,6 +25,20 @@ static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu)static int rec_exit_sync_dabt (struct kvm_vcpu *vcpu) { +struct realm_rec *rec = &vcpu->arch.rec; + +/* +* In the case of a write, copy over gprs[0] to the target GPR, +* preparing to handle MMIO write fault. The content to be written has +* been saved to gprs[0] by the RMM (even if another register was used +* by the guest). In the case of normal memory access this is redundant +* (the guest will replay the instruction), but the overhead is +* minimal. +*/ +if (kvm_vcpu_dabt_iswrite (vcpu) &&kvm_vcpu_dabt_isvalid (vcpu)) +vcpu_set_reg (vcpu,kvm_vcpu_dabt_get_rd (vcpu), + rec->run->exit.gprs[0 ]); +return kvm_handle_guest_abort (vcpu); }
Signature · 1 line
2.43.0
PATCH v10 19/43 arm64: RME: 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 v7:
* Improve the error codes.
* Other minor changes from review.
Changes since v6:
* Handle host potentially having a larger page size than the RMM granule.
* Drop historic "par" (protected address range) from populate_par_region() - it doesn't exist within the current architecture.
* Add a cond_resched() call in kvm_populate_realm().
Changes since v5:
* Refactor to use PFNs rather than tracking struct page in realm_create_protected_data_page().
* Pull changes from a later patch (in the v5 series) for accessing pages from a guest memfd.
* Do the populate in chunks to avoid holding locks for too long and triggering RCU stall warnings.
--- arch/arm64/kvm/rme.c | 227 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+)
arch/arm64/kvm/rme.c +227 -0
@@ -663,6 +663,221 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long start,realm_unmap_private_range (kvm, start, end, may_block); } +static int realm_create_protected_data_granule (struct realm *realm, +unsigned long ipa, +phys_addr_t dst_phys, +phys_addr_t src_phys, +unsigned long flags) +{ +phys_addr_t rd =virt_to_phys (realm->rd); +int ret; + +if (rmi_granule_delegate (dst_phys)) +return -ENXIO ; + + ret =rmi_data_create (rd, dst_phys, ipa, src_phys, flags); +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + +WARN_ON (level ==RMM_RTT_MAX_LEVEL ); + + ret =realm_create_rtt_levels (realm, ipa, level, +RMM_RTT_MAX_LEVEL ,NULL ); +if (ret) +return -EIO ; + + ret =rmi_data_create (rd, dst_phys, ipa, src_phys, flags); + } +if (ret) +return -EIO ; + +return 0 ; +} + +static int realm_create_protected_data_page (struct realm *realm, +unsigned long ipa, +kvm_pfn_t dst_pfn, +kvm_pfn_t src_pfn, +unsigned long flags) +{ +unsigned long rd =virt_to_phys (realm->rd); +phys_addr_t dst_phys, src_phys; +bool undelegate_failed = false; +int ret, offset; + + dst_phys =__pfn_to_phys (dst_pfn); + src_phys =__pfn_to_phys (src_pfn); + +for (offset =0 ; offset <PAGE_SIZE ; offset +=RMM_PAGE_SIZE ) { + ret =realm_create_protected_data_granule (realm, + ipa, + dst_phys, + src_phys, + flags); +if (ret) + goto err; + + ipa +=RMM_PAGE_SIZE ; + dst_phys +=RMM_PAGE_SIZE ; + src_phys +=RMM_PAGE_SIZE ; + } + +return 0 ; + +err: +if (ret == -EIO ) { +/* current offset needs undelegating */ +if (WARN_ON (rmi_granule_undelegate (dst_phys))) + undelegate_failed = true; + } +while (offset >0 ) { + ipa -=RMM_PAGE_SIZE ; + offset -=RMM_PAGE_SIZE ; + dst_phys -=RMM_PAGE_SIZE ; + +rmi_data_destroy (rd, ipa,NULL ,NULL ); + +if (WARN_ON (rmi_granule_undelegate (dst_phys))) + undelegate_failed = true; + } + +if (undelegate_failed) { +/* +* A granule could not be undelegated, +* so the page has to be leaked +*/ +get_page (pfn_to_page (dst_pfn)); + } + +return -ENXIO ; +} + +static int populate_region (struct kvm *kvm, +phys_addr_t ipa_base, +phys_addr_t ipa_end, +unsigned long data_flags) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_memory_slot *memslot; +gfn_t base_gfn, end_gfn; +int idx; +phys_addr_t ipa = ipa_base; +int ret =0 ; + + base_gfn =gpa_to_gfn (ipa_base); + end_gfn =gpa_to_gfn (ipa_end); + + idx =srcu_read_lock (&kvm->srcu); + memslot =gfn_to_memslot (kvm, base_gfn); +if (!memslot) { + ret = -EFAULT ; + goto out; + } + +/* We require the region to be contained within a single memslot */ +if (memslot->base_gfn + memslot->npages < end_gfn) { + ret = -EINVAL ; + goto out; + } + +if (!kvm_slot_can_be_private (memslot)) { + ret = -EPERM ; + goto out; + } + +while (ipa < ipa_end) { +struct vm_area_struct *vma; +unsigned long hva; +struct page *page; +bool writeable; +kvm_pfn_t pfn; +kvm_pfn_t priv_pfn; +struct page *gmem_page; + + hva =gfn_to_hva_memslot (memslot,gpa_to_gfn (ipa)); + vma =vma_lookup (current->mm, hva); +if (!vma) { + ret = -EFAULT ; +break ; + } + + pfn =__kvm_faultin_pfn (memslot,gpa_to_gfn (ipa),FOLL_WRITE , + &writeable, &page); + +if (is_error_pfn (pfn)) { + ret = -EFAULT ; +break ; + } + + ret =kvm_gmem_get_pfn (kvm, memslot, + ipa >>PAGE_SHIFT , + &priv_pfn, &gmem_page,NULL ); +if (ret) +break ; + + ret =realm_create_protected_data_page (realm, ipa, + priv_pfn, + pfn, + data_flags); + +kvm_release_page_clean (page); + +if (ret) +break ; + + ipa +=PAGE_SIZE ; + } + +out: +srcu_read_unlock (&kvm->srcu, idx); +return ret; +} + +static int kvm_populate_realm (struct kvm *kvm, +struct arm_rme_populate_realm *args) +{ +phys_addr_t ipa_base, ipa_end; +unsigned long data_flags =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EPERM ; + +if (!IS_ALIGNED (args->base,PAGE_SIZE ) || + !IS_ALIGNED (args->size,PAGE_SIZE ) || + (args->flags & ~RMI_MEASURE_CONTENT )) +return -EINVAL ; + + ipa_base = args->base; + ipa_end = ipa_base + args->size; + +if (ipa_end < ipa_base) +return -EINVAL ; + +if (args->flags &RMI_MEASURE_CONTENT ) + data_flags |=RMI_MEASURE_CONTENT ; + +/* +* Perform the population in parts to ensure locks are not held for too +* long +*/ +while (ipa_base < ipa_end) { +phys_addr_t end =min (ipa_end, ipa_base +SZ_2M ); + +int ret =populate_region (kvm, ipa_base, end, + args->flags); + +if (ret) +return ret; + + ipa_base = end; + +cond_resched (); + } + +return 0 ; +} +enum ripas_action {RIPAS_INIT ,RIPAS_SET ,
@@ -907,6 +1122,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) r =kvm_init_ipa_range_realm (kvm, &args);break ; } +case KVM_CAP_ARM_RME_POPULATE_REALM : { +struct arm_rme_populate_realm args; +void __user *argp =u64_to_user_ptr (cap->args[1 ]); + +if (copy_from_user (&args, argp,sizeof (args))) { + r = -EFAULT ; +break ; + } + + r =kvm_populate_realm (kvm, &args); +break ; + }default : r = -EINVAL ;break ;
Signature · 1 line
2.43.0
PATCH v10 20/43 arm64: RME: 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 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_rme.h | 10 ++ arch/arm64/kvm/mmu.c | 133 +++++++++++++++++++++- arch/arm64/kvm/rme.c | 164 +++++++++++++++++++++++++++ 4 files changed, 309 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +8 -0
@@ -692,6 +692,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 (struct kvm_vcpu *vcpu) {if (static_branch_unlikely (&kvm_rme_is_available))
arch/arm64/include/asm/kvm_rme.h +10 -0
@@ -106,6 +106,16 @@ void kvm_realm_unmap_range(struct kvm *kvm,unsigned long size, bool unmap_private, bool may_block); +int realm_map_protected (struct realm *realm, +unsigned long base_ipa, +kvm_pfn_t pfn, +unsigned long size, +struct kvm_mmu_memory_cache *memcache); +int realm_map_non_secure (struct realm *realm, +unsigned long ipa, +kvm_pfn_t pfn, +unsigned long size, +struct kvm_mmu_memory_cache *memcache);static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr)
arch/arm64/kvm/mmu.c +127 -6
206 lines not on this page · raw message
arch/arm64/kvm/rme.c +164 -0
170 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v10 21/43 KVM: arm64: 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
@@ -615,7 +615,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))
@@ -658,12 +658,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_timer_vcpu_load (vcpu);kvm_vgic_load (vcpu);kvm_vcpu_load_debug (vcpu); -if (has_vhe ()) -kvm_vcpu_load_vhe (vcpu); -kvm_arch_vcpu_load_fp (vcpu); -kvm_vcpu_pmu_restore_guest (vcpu); -if (kvm_arm_is_pvtime_enabled (&vcpu->arch)) -kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (kvm_vcpu_should_clear_twe (vcpu)) vcpu->arch.hcr_el2 &= ~HCR_TWE ;
@@ -685,6 +679,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 v10 22/43 KVM: arm64: Validate register access for a Realm VM
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;
@@ -783,12 +802,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 v10 23/43 KVM: arm64: Handle Realm PSCI requests
In reply to Steven PriceThe RMM needs to be informed of the target REC when a PSCI call is made with an MPIDR argument. Expose an ioctl to the userspace in case the PSCI is handled by it.
---
Changes since v6:
* Use vcpu_is_rec() rather than kvm_is_realm(vcpu->kvm).
* Minor renaming/formatting fixes.
--- arch/arm64/include/asm/kvm_rme.h | 3 +++ arch/arm64/kvm/arm.c | 25 +++++++++++++++++++++++++ arch/arm64/kvm/psci.c | 30 ++++++++++++++++++++++++++++++ arch/arm64/kvm/rme.c | 14 ++++++++++++++ 4 files changed, 72 insertions(+)
arch/arm64/include/asm/kvm_rme.h +3 -0
@@ -116,6 +116,9 @@ int realm_map_non_secure(struct realm *realm,kvm_pfn_t pfn, unsigned long size, struct kvm_mmu_memory_cache *memcache); +int realm_psci_complete (struct kvm_vcpu *source, +struct kvm_vcpu *target, +unsigned long status);static inline bool kvm_realm_is_private_address (struct realm *realm,unsigned long addr)
arch/arm64/kvm/arm.c +25 -0
@@ -1769,6 +1769,22 @@ static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,return __kvm_arm_vcpu_set_events (vcpu, events); } +static int kvm_arm_vcpu_rmm_psci_complete (struct kvm_vcpu *vcpu, +struct kvm_arm_rmm_psci_complete *arg) +{ +struct kvm_vcpu *target =kvm_mpidr_to_vcpu (vcpu->kvm, arg->target_mpidr); + +if (!target) +return -EINVAL ; + +/* +* RMM v1.0 only supports PSCI_RET_SUCCESS or PSCI_RET_DENIED +* for the status. But, let us leave it to the RMM to filter +* for making this future proof. +*/ +return realm_psci_complete (vcpu, target, arg->psci_status); +} +long kvm_arch_vcpu_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {
@@ -1891,6 +1907,15 @@ long kvm_arch_vcpu_ioctl(struct file *filp,return kvm_arm_vcpu_finalize (vcpu, what); } +case KVM_ARM_VCPU_RMM_PSCI_COMPLETE : { +struct kvm_arm_rmm_psci_complete req; + +if (!vcpu_is_rec (vcpu)) +return -EPERM ; +if (copy_from_user (&req, argp,sizeof (req))) +return -EFAULT ; +return kvm_arm_vcpu_rmm_psci_complete (vcpu, &req); + }default : r = -EINVAL ; }
arch/arm64/kvm/psci.c +30 -0
@@ -103,6 +103,12 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) reset_state->reset = true;kvm_make_request (KVM_REQ_VCPU_RESET , vcpu); +/* +* Make sure we issue PSCI_COMPLETE before the VCPU can be +* scheduled. +*/ +if (vcpu_is_rec (vcpu)) +realm_psci_complete (source_vcpu, vcpu,PSCI_RET_SUCCESS );/* * Make sure the reset request is observed if the RUNNABLE mp_state is
@@ -115,6 +121,11 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) out_unlock:spin_unlock (&vcpu->arch.mp_state_lock); +if (vcpu_is_rec (vcpu) && ret !=PSCI_RET_SUCCESS ) { +realm_psci_complete (source_vcpu, vcpu, + ret ==PSCI_RET_ALREADY_ON ? +PSCI_RET_SUCCESS :PSCI_RET_DENIED ); + }return ret; }
@@ -142,6 +153,25 @@ static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)/* Ignore other bits of target affinity */ target_affinity &= target_affinity_mask; +if (vcpu_is_rec (vcpu)) { +struct kvm_vcpu *target_vcpu; + +/* RMM supports only zero affinity level */ +if (lowest_affinity_level !=0 ) +return PSCI_RET_INVALID_PARAMS ; + + target_vcpu =kvm_mpidr_to_vcpu (kvm, target_affinity); +if (!target_vcpu) +return PSCI_RET_INVALID_PARAMS ; + +/* +* Provide the references of the source and target RECs to the +* RMM so that the RMM can complete the PSCI request. +*/ +realm_psci_complete (vcpu, target_vcpu,PSCI_RET_SUCCESS ); +return PSCI_RET_SUCCESS ; + } +/* * If one or more VCPU matching target affinity are running * then ON else OFF
arch/arm64/kvm/rme.c +14 -0
@@ -164,6 +164,20 @@ 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,struct kvm_vcpu *target, +unsigned long status) +{ +int ret; + + ret =rmi_psci_complete (virt_to_phys (source->arch.rec.rec_page), +virt_to_phys (target->arch.rec.rec_page), + status); +if (ret) +return -EINVAL ; + +return 0 ; +} + static int realm_rtt_create(struct realm *realm, unsigned long addr, int level,
Signature · 1 line
2.43.0
PATCH v10 24/43 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 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
@@ -266,6 +266,7 @@ void kvm_inject_size_fault(struct kvm_vcpu *vcpu)*/ void kvm_inject_undefined (struct kvm_vcpu *vcpu) { +WARN (vcpu_is_rec (vcpu),"Unexpected undefined exception injection to REC" );if (vcpu_el1_is_32bit (vcpu))inject_undef32 (vcpu);else
Signature · 1 line
2.43.0
PATCH v10 25/43 arm64: Don't expose stolen time for realm guests
In reply to Steven PriceIt doesn't make much sense as a realm guest wouldn't want to trust the host. It will also need some extra work to ensure that KVM will only attempt to write into a shared memory region. So for now just disable it.
---
Changes since v7:
* Update the documentation to add a note about stolen time being unavailable in a realm.
--- Documentation/virt/kvm/api.rst | 3 +++ arch/arm64/kvm/arm.c | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +3 -0
@@ -9012,6 +9012,9 @@ is supported, than the other should as well and vice versa. For arm64 see Documentation/virt/kvm/devices/vcpu.rst "KVM_ARM_VCPU_PVTIME_CTRL". For x86 see Documentation/virt/kvm/x86/msr.rst "MSR_KVM_STEAL_TIME". +Note that steal time accounting is not available when a guest is running +within a Arm CCA realm (machine type KVM_VM_TYPE_ARM_REALM). + 8.25 KVM_CAP_S390_DIAG318 -------------------------
arch/arm64/kvm/arm.c +4 -1
@@ -397,7 +397,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =system_supports_mte ();break ;case KVM_CAP_STEAL_TIME : - r =kvm_arm_pvtime_supported (); +if (kvm_is_realm (kvm)) + r =0 ; +else + r =kvm_arm_pvtime_supported ();break ;case KVM_CAP_ARM_EL1_32BIT : r =cpus_have_final_cap (ARM64_HAS_32BIT_EL1 );
Signature · 1 line
2.43.0
PATCH v10 26/43 arm64: RME: 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
@@ -1307,6 +1307,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
@@ -897,6 +897,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 v10 27/43 arm64: RME: support RSI_HOST_CALL
In reply to Steven PriceFrom: Joey Gouly <[email protected]>
Forward RSI_HOST_CALLS to KVM's HVC handler.
---
Changes since v7:
* Avoid turning a negative return from kvm_smccc_call_handler() into a error response to the guest. Instead propogate 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/rme-exit.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/arm64/kvm/rme-exit.c +15 -0
@@ -116,6 +116,19 @@ static int rec_exit_ripas_change(struct kvm_vcpu *vcpu)return -EFAULT ; } +static int rec_exit_host_call (struct kvm_vcpu *vcpu) +{ +int i; +struct realm_rec *rec = &vcpu->arch.rec; + + vcpu->stat.hvc_exit_stat++; + +for (i =0 ; i <REC_RUN_GPRS ; i++) +vcpu_set_reg (vcpu, i, rec->run->exit.gprs[i]); + +return kvm_smccc_call_handler (vcpu); +} +static void update_arch_timer_irq_lines (struct kvm_vcpu *vcpu) {struct realm_rec *rec = &vcpu->arch.rec;
@@ -183,6 +196,8 @@ int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret)return rec_exit_psci (vcpu);case RMI_EXIT_RIPAS_CHANGE :return rec_exit_ripas_change (vcpu); +case RMI_EXIT_HOST_CALL : +return rec_exit_host_call (vcpu); } kvm_pr_unimpl("Unsupported exit reason: %u\n",
Signature · 1 line
2.43.0
PATCH v10 28/43 arm64: RME: 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.
--- arch/arm64/include/asm/kvm_rme.h | 2 ++ arch/arm64/kvm/arm.c | 5 ++++- arch/arm64/kvm/rme.c | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rme.h +2 -0
@@ -90,6 +90,8 @@ void kvm_init_rme(void);u32 kvm_realm_ipa_limit (void );u32 kvm_realm_vgic_nr_lr (void ); +bool kvm_rme_supports_sve (void ); +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int kvm_init_realm_vm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm);
arch/arm64/kvm/arm.c +4 -1
@@ -427,7 +427,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =get_kvm_ipa_limit ();break ;case KVM_CAP_ARM_SVE : - r =system_supports_sve (); +if (kvm_is_realm (kvm)) + r =kvm_rme_supports_sve (); +else + r =system_supports_sve ();break ;case KVM_CAP_ARM_PTRAUTH_ADDRESS :case KVM_CAP_ARM_PTRAUTH_GENERIC :
arch/arm64/kvm/rme.c +5 -0
@@ -38,6 +38,11 @@ static bool rme_has_feature(unsigned long feature)return !!u64_get_bits (rmm_feat_reg0, feature); } +bool kvm_rme_supports_sve (void ) +{ +return rme_has_feature (RMI_FEATURE_REGISTER_0_SVE_EN ); +} +static int rmi_check_version (void ) {struct arm_smccc_res res;
Signature · 1 line
2.43.0
PATCH v10 29/43 arm64: RME: Always use 4k pages for realms
In reply to Steven PriceGuest_memfd doesn't yet natively support huge pages, and there are currently difficulties for a VMM to manage huge pages efficiently so for now always split up mappings to PTE (4k).
The two issues that need progressing before supporting huge pages for realms are:
1. guest_memfd needs to be able to allocate from an appropriate allocator which can provide huge pages.
2. The VMM needs to be able to repurpose private memory for a shared mapping when the guest VM requests memory is transitioned. Because this can happen at a 4k granularity it isn't possible to free/reallocate while huge pages are in use. Allowing the VMM to mmap() the shared portion of a huge page would allow the huge page to be recreated when the memory is unshared and made protected again.
These two issues are not specific to realms and don't affect the realm API, so for now just break everything down to 4k pages in the RMM controlled stage 2. Future work can add huge page support without changing the uAPI.
---
Changes since v7:
* Rewritten commit message
--- arch/arm64/kvm/mmu.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kvm/mmu.c +4 -0
@@ -1674,6 +1674,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,if (logging_active) { force_pte = true; vma_shift =PAGE_SHIFT ; + }else if (vcpu_is_rec (vcpu)) { +/* Force PTE level mappings for realms */ + force_pte = true; + vma_shift =PAGE_SHIFT ; }else { vma_shift =get_vma_page_shift (vma, hva); }
Signature · 1 line
2.43.0
PATCH v10 30/43 arm64: RME: Prevent Device mappings for Realms
In reply to Steven PricePhysical device assignment is not yet supported by the RMM, so it doesn't make much sense to allow device mappings within the realm. Prevent them 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 | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/arm64/kvm/mmu.c +13 -0
@@ -1184,6 +1184,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 ;
@@ -1821,6 +1825,15 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,if (exec_fault && s2_force_noncacheable)return -ENOEXEC ; +/* +* For now we shouldn't be hitting protected addresses because they are +* handled in private_memslot_fault(). In the future this check may be +* relaxed to support e.g. protected devices. +*/ +if (vcpu_is_rec (vcpu) && +kvm_gpa_from_fault (kvm, fault_ipa) == fault_ipa) +return -EINVAL ; +/* * Potentially reduce shadow S2 permissions to match the guest's own * S2. For exec faults, we'd only reach this point if the guest
Signature · 1 line
2.43.0
PATCH v10 31/43 arm_pmu: Provide a mechanism for disabling the physical IRQ
In reply to Steven PriceArm CCA assigns the physical PMU device to the guest running in realm world, however the IRQs are routed via the host. To enter a realm guest while a PMU IRQ is pending it is necessary to block the physical IRQ to prevent an immediate exit. Provide a mechanism in the PMU driver for KVM to control the physical IRQ.
---
v3: Add a dummy function for the !CONFIG_ARM_PMU case.
--- drivers/perf/arm_pmu.c | 15 +++++++++++++++ include/linux/perf/arm_pmu.h | 5 +++++ 2 files changed, 20 insertions(+)
drivers/perf/arm_pmu.c +15 -0
@@ -743,6 +743,21 @@ static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)return 0 ; } +void arm_pmu_set_phys_irq (bool enable) +{ +int cpu =get_cpu (); +struct arm_pmu *pmu =per_cpu (cpu_armpmu, cpu); +int irq; + + irq =armpmu_get_cpu_irq (pmu, cpu); +if (irq && !enable) +per_cpu (cpu_irq_ops, cpu)->disable_pmuirq (irq); +else if (irq && enable) +per_cpu (cpu_irq_ops, cpu)->enable_pmuirq (irq); + +put_cpu (); +} +#ifdef CONFIG_CPU_PM static void cpu_pm_pmu_setup (struct arm_pmu *armpmu,unsigned long cmd) {
include/linux/perf/arm_pmu.h +5 -0
@@ -185,6 +185,7 @@ void kvm_host_pmu_init(struct arm_pmu *pmu);#endif bool arm_pmu_irq_is_nmi (void ); +void arm_pmu_set_phys_irq (bool enable);/* Internal functions only for core arm_pmu code */ struct arm_pmu *armpmu_alloc (void );
@@ -195,6 +196,10 @@ void armpmu_free_irq(int irq, int cpu);#define ARMV8_PMU_PDEV_NAME "armv8-pmu" +#else /* CONFIG_ARM_PMU */ + +static inline void arm_pmu_set_phys_irq (bool enable) {} +#endif /* CONFIG_ARM_PMU */ #define ARMV8_SPE_PDEV_NAME "arm,spe-v1"
Signature · 1 line
2.43.0
Re: [PATCH v10 31/43] arm_pmu: Provide a mechanism for disabling the physical IRQ
Quoted 6 lines · Steven Price
Arm CCA assigns the physical PMU device to the guest running in realm world, however the IRQs are routed via the host. To enter a realm guest while a PMU IRQ is pending it is necessary to block the physical IRQ to prevent an immediate exit. Provide a mechanism in the PMU driver for KVM to control the physical IRQ.
Quoted 8 lines
Signed-off-by Steven Price <[email protected]>---
v3: Add a dummy function for the !CONFIG_ARM_PMU case.--- drivers/perf/arm_pmu.c | 15 +++++++++++++++ include/linux/perf/arm_pmu.h | 5 +++++ 2 files changed, 20 insertions(+)
PATCH v10 32/43 arm64: RME: Enable PMU support with a realm guest
In reply to Steven PriceUse the PMU registers from the RmiRecExit structure to identify when an overflow interrupt is due and inject it into the guest. Also hook up the configuration option for enabling the PMU within the guest.
When entering a realm guest with a PMU interrupt pending, it is necessary to disable the physical interrupt. Otherwise when the RMM restores the PMU state the physical interrupt will trigger causing an immediate exit back to the host. The guest is expected to acknowledge the interrupt causing a host exit (to update the GIC state) which gives the opportunity to re-enable the physical interrupt before the next PMU event.
Number of PMU counters is configured by the VMM by writing to PMCR.N.
---
Changes since v2:
* Add a macro kvm_pmu_get_irq_level() to avoid compile issues when PMU support is disabled.
--- arch/arm64/kvm/arm.c | 11 +++++++++++ arch/arm64/kvm/guest.c | 7 +++++++ arch/arm64/kvm/pmu-emul.c | 3 +++ arch/arm64/kvm/rme.c | 8 ++++++++ arch/arm64/kvm/sys_regs.c | 5 +++-- include/kvm/arm_pmu.h | 4 ++++ 6 files changed, 36 insertions(+), 2 deletions(-)
arch/arm64/kvm/arm.c +11 -0
@@ -15,6 +15,7 @@#include <linux/vmalloc.h> #include <linux/fs.h> #include <linux/mman.h> +#include <linux/perf/arm_pmu.h> #include <linux/sched.h> #include <linux/kvm.h> #include <linux/kvm_irqfd.h>
@@ -1234,6 +1235,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) run->exit_reason =KVM_EXIT_UNKNOWN ; run->flags =0 ;while (ret >0 ) { +bool pmu_stopped = false; +/* * Check conditions before entering the guest */
@@ -1259,6 +1262,11 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)if (kvm_vcpu_has_pmu (vcpu))kvm_pmu_flush_hwstate (vcpu); +if (vcpu_is_rec (vcpu) &&kvm_pmu_get_irq_level (vcpu)) { + pmu_stopped = true; +arm_pmu_set_phys_irq (false); + } +local_irq_disable ();kvm_vgic_flush_hwstate (vcpu);
@@ -1367,6 +1375,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)preempt_enable (); +if (pmu_stopped) +arm_pmu_set_phys_irq (true); +/* * The ARMv8 architecture doesn't give the hypervisor * a mechanism to prevent a guest from dropping to AArch32 EL0
arch/arm64/kvm/guest.c +7 -0
@@ -802,6 +802,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_PMCR_EL0 ARM64_SYS_REG(3, 3, 9, 12, 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
@@ -816,6 +818,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_PMCR_EL0 : +return true; + } }return false;
arch/arm64/kvm/pmu-emul.c +3 -0
@@ -374,6 +374,9 @@ static bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu) {u64 reg =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); +if (vcpu_is_rec (vcpu)) +return vcpu->arch.rec.run->exit.pmu_ovf_status; + reg &=__vcpu_sys_reg (vcpu,PMINTENSET_EL1 );/*
arch/arm64/kvm/rme.c +8 -0
@@ -603,6 +603,11 @@ static int realm_create_rd(struct kvm *kvm) params->rtt_base = kvm->arch.mmu.pgd_phys; params->vmid = realm->vmid; +if (kvm->arch.arm_pmu) { + params->pmu_num_ctrs = kvm->arch.nr_pmu_counters; + params->flags |=RMI_REALM_PARAM_FLAG_PMU ; + } + params_phys =virt_to_phys (params);if (rmi_realm_create (rd_phys, params_phys)) {
@@ -1526,6 +1531,9 @@ int kvm_create_rec(struct kvm_vcpu *vcpu)if (!vcpu_has_feature (vcpu,KVM_ARM_VCPU_PSCI_0_2 ))return -EINVAL ; +if (vcpu->kvm->arch.arm_pmu && !kvm_vcpu_has_pmu (vcpu)) +return -EINVAL ; +BUILD_BUG_ON (sizeof (*params) >PAGE_SIZE );BUILD_BUG_ON (sizeof (*rec->run) >PAGE_SIZE );
arch/arm64/kvm/sys_regs.c +3 -2
@@ -1220,8 +1220,9 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,* implements. Ignore this error to maintain compatibility * with the existing KVM behavior. */ -if (!kvm_vm_has_ran_once (kvm) && - !vcpu_has_nv (vcpu) && +if (!kvm_vm_has_ran_once (kvm) && + !kvm_realm_is_created (kvm) && + !vcpu_has_nv (vcpu) && new_n <=kvm_arm_pmu_get_max_counters (kvm)) kvm->arch.nr_pmu_counters = new_n;
include/kvm/arm_pmu.h +4 -0
@@ -70,6 +70,8 @@ void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_resync_el0 (void ); +#define kvm_pmu_get_irq_level (vcpu) ((vcpu)->arch.pmu.irq_level) +#define kvm_vcpu_has_pmu (vcpu) \ (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
@@ -157,6 +159,8 @@ static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)return 0 ; } +#define kvm_pmu_get_irq_level (vcpu) (false) +#define kvm_vcpu_has_pmu (vcpu) ({ false; })static inline void kvm_pmu_update_vcpu_events (struct kvm_vcpu *vcpu) {}static inline void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu) {}
Signature · 1 line
2.43.0
Re: [PATCH v10 32/43] arm64: RME: Enable PMU support with a realm guest
Quoted 19 lines · Steven Price
Use the PMU registers from the RmiRecExit structure to identify when an overflow interrupt is due and inject it into the guest. Also hook up the configuration option for enabling the PMU within the guest.
When entering a realm guest with a PMU interrupt pending, it is necessary to disable the physical interrupt. Otherwise when the RMM restores the PMU state the physical interrupt will trigger causing an immediate exit back to the host. The guest is expected to acknowledge the interrupt causing a host exit (to update the GIC state) which gives the opportunity to re-enable the physical interrupt before the next PMU event.
Number of PMU counters is configured by the VMM by writing to PMCR.N.
Signed-off-by Steven Price <[email protected]>---
Changes since v2:
* Add a macrokvm_pmu_get_irq_level()to avoid compile issues when PMU support is disabled.
Quoted 9 lines
--- arch/arm64/kvm/arm.c | 11 +++++++++++ arch/arm64/kvm/guest.c | 7 +++++++ arch/arm64/kvm/pmu-emul.c | 3 +++ arch/arm64/kvm/rme.c | 8 ++++++++ arch/arm64/kvm/sys_regs.c | 5 +++-- include/kvm/arm_pmu.h | 4 ++++ 6 files changed, 36 insertions(+), 2 deletions(-)
PATCH v10 33/43 arm64: RME: Hide KVM_CAP_READONLY_MEM for realm guests
In reply to Steven PriceFor protected memory read only isn't supported by the RMM. While it may be possible to support read only for unprotected memory, this isn't supported at the present time.
Note that this does mean that ROM (or flash) data cannot be emulated correctly by the VMM as the stage 2 mappings are either always read/write or are trapped as MMIO (so don't support operations where the syndrome information doesn't allow emulation, e.g. load/store pair).
This restriction can be lifted in the future by allowing the unprotected stage 2 mappings to be made read only.
---
Changes since v7:
* Updated commit message to spell out the impact on ROM/flash emulation.
--- arch/arm64/kvm/arm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/arm.c +1 -1
@@ -341,7 +341,6 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ONE_REG :case KVM_CAP_ARM_PSCI :case KVM_CAP_ARM_PSCI_0_2 : -case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT :case KVM_CAP_VCPU_EVENTS :
@@ -356,6 +355,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =1 ;break ;case KVM_CAP_COUNTER_OFFSET : +case KVM_CAP_READONLY_MEM :case KVM_CAP_SET_GUEST_DEBUG : r = !kvm_is_realm (kvm);break ;
Signature · 1 line
2.43.0
PATCH v10 34/43 arm64: RME: Propagate number of breakpoints and watchpoints 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_rme.h | 2 ++ arch/arm64/kvm/rme.c | 22 ++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rme.h +2 -0
@@ -90,6 +90,8 @@ void kvm_init_rme(void);u32 kvm_realm_ipa_limit (void );u32 kvm_realm_vgic_nr_lr (void ); +u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val); +bool kvm_rme_supports_sve (void );int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);
arch/arm64/kvm/rme.c +22 -0
@@ -87,6 +87,28 @@ u32 kvm_realm_vgic_nr_lr(void)return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS ); } +u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val) +{ +u32 bps =u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_NUM_BPS ); +u32 wps =u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_NUM_WPS ); +u32 ctx_cmps; + +if (!kvm_is_realm (vcpu->kvm)) +return val; + +/* 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) {/*
arch/arm64/kvm/sys_regs.c +1 -1
@@ -1855,7 +1855,7 @@ static u64 sanitise_id_aa64dfr0_el1(const struct kvm_vcpu *vcpu, u64 val)/* Hide BRBE from guests */ val &= ~ID_AA64DFR0_EL1_BRBE_MASK ; -return val; +return kvm_realm_reset_id_aa64dfr0_el1 (vcpu, val); } static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
Signature · 1 line
2.43.0
PATCH v10 35/43 arm64: RME: 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 | 2 ++ arch/arm64/kvm/rme.c | 3 +++ arch/arm64/kvm/sys_regs.c | 17 +++++++++++------ 3 files changed, 16 insertions(+), 6 deletions(-)
arch/arm64/kvm/guest.c +2 -0
@@ -803,6 +803,7 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) }#define KVM_REG_ARM_PMCR_EL0 ARM64_SYS_REG(3, 3, 9, 12, 0) +#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
@@ -821,6 +822,7 @@ static bool validate_realm_set_reg(struct kvm_vcpu *vcpu, }else {switch (reg->id) {case KVM_REG_ARM_PMCR_EL0 : +case KVM_REG_ARM_ID_AA64DFR0_EL1 :return true; } }
arch/arm64/kvm/rme.c +3 -0
@@ -588,6 +588,7 @@ static int realm_create_rd(struct kvm *kvm)void *rd =NULL ;phys_addr_t rd_phys, params_phys;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 );int i, r;int rtt_num_start;
@@ -624,6 +625,8 @@ static int realm_create_rd(struct kvm *kvm) params->rtt_num_start = rtt_num_start; params->rtt_base = kvm->arch.mmu.pgd_phys; params->vmid = realm->vmid; + params->num_bps =SYS_FIELD_GET (ID_AA64DFR0_EL1 , BRPs, dfr0); + params->num_wps =SYS_FIELD_GET (ID_AA64DFR0_EL1 , WRPs, dfr0);if (kvm->arch.arm_pmu) { params->pmu_num_ctrs = kvm->arch.nr_pmu_counters;
arch/arm64/kvm/sys_regs.c +11 -6
@@ -1864,6 +1864,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
@@ -1883,10 +1886,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 ;return set_id_reg (vcpu, rd, val);
@@ -2103,10 +2107,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 v10 36/43 arm64: RME: Initialize PMCR.N with number counter supported by RMM
In reply to Steven PriceFrom: Jean-Philippe Brucker <[email protected]>
Provide an accurate number of available PMU counters to userspace when setting up a Realm.
--- arch/arm64/include/asm/kvm_rme.h | 1 + arch/arm64/kvm/pmu-emul.c | 3 +++ arch/arm64/kvm/rme.c | 5 +++++ 3 files changed, 9 insertions(+)
arch/arm64/include/asm/kvm_rme.h +1 -0
@@ -89,6 +89,7 @@ struct realm_rec {void kvm_init_rme (void );u32 kvm_realm_ipa_limit (void );u32 kvm_realm_vgic_nr_lr (void ); +u8 kvm_realm_max_pmu_counters (void );u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val);
arch/arm64/kvm/pmu-emul.c +3 -0
@@ -1014,6 +1014,9 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) {struct arm_pmu *arm_pmu = kvm->arch.arm_pmu; +if (kvm_is_realm (kvm)) +return kvm_realm_max_pmu_counters (); +/* * PMUv3 requires that all event counters are capable of counting any * event, though the same may not be true of non-PMUv3 hardware.
arch/arm64/kvm/rme.c +5 -0
@@ -87,6 +87,11 @@ u32 kvm_realm_vgic_nr_lr(void)return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_GICV3_NUM_LRS ); } +u8 kvm_realm_max_pmu_counters (void ) +{ +return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS ); +} +u64 kvm_realm_reset_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu,u64 val) {u32 bps =u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_NUM_BPS );
Signature · 1 line
2.43.0
PATCH v10 37/43 arm64: RME: Propagate max SVE vector length from 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_rme.h | 1 + arch/arm64/kvm/guest.c | 2 +- arch/arm64/kvm/reset.c | 12 ++++++++++-- arch/arm64/kvm/rme.c | 6 ++++++ 5 files changed, 19 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -77,9 +77,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_rme.h +1 -0
@@ -90,6 +90,7 @@ void kvm_init_rme(void);u32 kvm_realm_ipa_limit (void );u32 kvm_realm_vgic_nr_lr (void );u8 kvm_realm_max_pmu_counters (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/rme.c +6 -0
@@ -92,6 +92,12 @@ u8 kvm_realm_max_pmu_counters(void)return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS ); } +unsigned int kvm_realm_sve_max_vl (void ) +{ +return sve_vl_from_vq (u64_get_bits (rmm_feat_reg0, +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 (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_NUM_BPS );
Signature · 1 line
2.43.0
PATCH v10 38/43 arm64: RME: 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/rme.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 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))
@@ -823,6 +823,7 @@ static bool validate_realm_set_reg(struct kvm_vcpu *vcpu,switch (reg->id) {case KVM_REG_ARM_PMCR_EL0 :case KVM_REG_ARM_ID_AA64DFR0_EL1 : +case KVM_REG_ARM64_SVE_VLS :return true; } }
arch/arm64/kvm/rme.c +42 -0
54 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v10 39/43 arm64: RME: Provide register list for unfinalized RME 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
@@ -1856,10 +1856,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
@@ -675,8 +675,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 */
@@ -693,9 +693,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:
@@ -705,6 +702,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 v10 40/43 arm64: RME: Provide 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 v8:
* Minor type changes following review.
Changes since v7:
* Reworked on upstream changes.
--- arch/arm64/kvm/guest.c | 19 ++++++++++++++----- arch/arm64/kvm/hypercalls.c | 4 ++-- arch/arm64/kvm/sys_regs.c | 29 +++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 13 deletions(-)
arch/arm64/kvm/guest.c +14 -5
@@ -619,8 +619,6 @@ static const u64 timer_reg_list[] = { KVM_REG_ARM_PTIMER_CVAL, }; -#define NUM_TIMER_REGS ARRAY_SIZE(timer_reg_list) -static bool is_timer_reg (u64 index) {switch (index) {
@@ -635,9 +633,14 @@ static bool is_timer_reg(u64 index)return false; } +static inline unsigned long num_timer_regs (struct kvm_vcpu *vcpu) +{ +return kvm_is_realm (vcpu->kvm) ?0 :ARRAY_SIZE (timer_reg_list); +} +static int copy_timer_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) { -for (int i =0 ; i <NUM_TIMER_REGS ; i++) { +for (unsigned long i =0 ; i <num_timer_regs (vcpu); i++) {if (put_user (timer_reg_list[i], uindices))return -EFAULT ; uindices++;
@@ -678,6 +681,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 */ }
@@ -705,6 +711,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);
@@ -743,7 +752,7 @@ unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu) res +=num_sve_regs (vcpu); res +=kvm_arm_num_sys_reg_descs (vcpu); res +=kvm_arm_get_fw_num_regs (vcpu); - res +=NUM_TIMER_REGS ; + res +=num_timer_regs (vcpu);return res; }
@@ -777,7 +786,7 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) ret =copy_timer_indices (vcpu, uindices);if (ret <0 )return ret; - uindices +=NUM_TIMER_REGS ; + uindices +=num_timer_regs (vcpu);return kvm_arm_copy_sys_reg_indices (vcpu, uindices); }
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 +23 -6
@@ -5120,18 +5120,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++;
@@ -5162,11 +5162,28 @@ 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) +{ +if (!kvm_is_realm (vcpu->kvm)) +return false; + +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 (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.
@@ -5204,7 +5221,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 ); }
@@ -5217,7 +5234,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 v10 41/43 KVM: arm64: Expose support for private memory
In reply to Steven PriceSelect KVM_GENERIC_PRIVATE_MEM and provide the necessary support functions.
---
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 ++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -1559,6 +1559,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);
arch/arm64/kvm/Kconfig +1 -0
@@ -37,6 +37,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_GENERIC_PRIVATE_MEM help Support hosting virtualized guest machines.
arch/arm64/kvm/mmu.c +24 -0
@@ -2419,6 +2419,30 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,return ret; } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +bool kvm_arch_pre_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range) +{ +WARN_ON_ONCE (!kvm_arch_has_private_mem (kvm)); +return false; +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range) +{ +if (WARN_ON_ONCE (!kvm_arch_has_private_mem (kvm))) +return false; + +if (range->arg.attributes &KVM_MEMORY_ATTRIBUTE_PRIVATE ) + range->attr_filter =KVM_FILTER_SHARED ; +else + range->attr_filter =KVM_FILTER_PRIVATE ; +kvm_unmap_gfn_range (kvm, range); + +return false; +} +#endif +void kvm_arch_free_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) { }
Signature · 1 line
2.43.0
PATCH v10 42/43 KVM: arm64: Expose KVM_ARM_VCPU_REC to user space
In reply to Steven PriceIncrement KVM_VCPU_MAX_FEATURES to expose the new capability to user space.
---
Changes since v8:
* Since NV is now merged and enabled, this no longer conflicts with it.
--- arch/arm64/include/asm/kvm_host.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -40,7 +40,7 @@#define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS -#define KVM_VCPU_MAX_FEATURES 9 +#define KVM_VCPU_MAX_FEATURES 10#define KVM_VCPU_VALID_FEATURES (BIT(KVM_VCPU_MAX_FEATURES) - 1)#define KVM_REQ_SLEEP \
Signature · 1 line
2.43.0
PATCH v10 43/43 KVM: arm64: Allow activating realms
In reply to Steven PriceAdd the ioctl to activate a realm and set the static branch to enable access to the realm functionality if the RMM is detected.
--- arch/arm64/kvm/rme.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
arch/arm64/kvm/rme.c +18 -1
@@ -1253,6 +1253,20 @@ static int kvm_init_ipa_range_realm(struct kvm *kvm,return realm_init_ipa_state (kvm, addr, end); } +static int kvm_activate_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; + +if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EINVAL ; + +if (rmi_realm_activate (virt_to_phys (realm->rd))) +return -ENXIO ; + +WRITE_ONCE (realm->state,REALM_STATE_ACTIVE ); +return 0 ; +} +/* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK (rme_vmid_lock);static unsigned long *rme_vmid_bitmap;
@@ -1400,6 +1414,9 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) r =kvm_populate_realm (kvm, &args);break ; } +case KVM_CAP_ARM_RME_ACTIVATE_REALM : + r =kvm_activate_realm (kvm); +break ;default : r = -EINVAL ;break ;
@@ -1725,5 +1742,5 @@ void kvm_init_rme(void)if (rme_vmid_init ())return ; -/* Future patch will enable static branch kvm_rme_is_available */ +static_branch_enable (&kvm_rme_is_available); }
Signature · 1 line
2.43.0
Quoted 20 lines · Steven Price
This series adds support for running protected VMs using KVM under the Arm Confidential Compute Architecture (CCA).
The related guest support was merged for v6.14-rc1 so you no longer need that separately.
There are a few changes since v9, many thanks for the review comments. The highlights are below, and individual patches have a changelog.
* Fix a potential issue where the host was walking the stage 2 page tables on realm destruction. If the RMM didn't zero when undelegated (which it isn't required to) then the kernel would attempt to work the junk values and crash.
* Avoid RCU stall warnings by correctly settign
may_blockinkvm_free_stage2_pgd().* Rebased onto v6.17-rc1.
Things to note:
Quoted 9 lines
* The magic numbers for capabilities and ioctls have been updated. So you'll need to update your VMM. See below for the updated kvmtool branch.
* This series doesn't attempt to integrate with the guest-memfd changes that are being discussed (see below).
* Vishal raised an important question about what to do in the case of undelegate failures (also see below).
[...]
I tried to boot a guest using the following combinations, nothing obvious went to wrong except several long existing issues (described below). So feel free to add:
Combination
===========
host.tf-a https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git (v2.13-rc0) host.tf-rmm https://git.codelinaro.org/linaro/dcap/rmm (cca/v8) host.edk2 [email protected]:tianocore/edk2.git (edk2-stable202411) host.kernel [email protected]:gwshan/linux.git (cca/host-v10) (this series) host.qemu https://git.qemu.org/git/qemu.git (stable-9.2) host.buildroot https://github.com/buildroot/buildroot (master) guest.qemu https://git.codelinaro.org/linaro/dcap/qemu.git (cca/latest) (with linux-headers sync'ed) guest.kvmtool https://gitlab.arm.com/linux-arm/kvmtool-cca (cca/latest) guest.edk2 https://git.codelinaro.org/linaro/dcap/edk2 (cca/latest) guest.kernel git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git (v6.17.rc3) guest.buildroot https://github.com/buildroot/buildroot (master)
Script to start the host
========================
gshan@nvidia-grace-hopper-01:~/sandbox/qemu/host$ cat start.sh #!/bin/sh HOST_PATH=/home/gshan/sandbox/qemu/host GUEST_PATH=/home/gshan/sandbox/qemu/guest IF_UP_SCRIPT=/etc/qemu-ifup-gshan IF_DOWN_SCRIPT=/etc/qemu-ifdown-gshan
sudo ${HOST_PATH}/qemu/build/qemu-system-aarch64 \
-M virt,virtualization=on,secure=on,gic-version=3,acpi=off \
-cpu max,x-rme=on -m 3G -smp 8 \
-serial mon:stdio -monitor none -nographic -nodefaults \
-bios ${HOST_PATH}/tf-a/flash.bin \
-kernel ${HOST_PATH}/linux/arch/arm64/boot/Image \
-initrd ${HOST_PATH}/buildroot/output/images/rootfs.cpio.xz \
-device pcie-root-port,bus=pcie.0,chassis=1,id=pcie.1 \
-device pcie-root-port,bus=pcie.0,chassis=2,id=pcie.2 \
-device pcie-root-port,bus=pcie.0,chassis=3,id=pcie.3 \
-device pcie-root-port,bus=pcie.0,chassis=4,id=pcie.4 \
-device virtio-9p-device,fsdev=shr0,mount_tag=shr0 \
-fsdev local,security_model=none,path=${GUEST_PATH},id=shr0 \-netdev tap,id=tap1,script=${IF_UP_SCRIPT},downscript=${IF_DOWN_SCRIPT} \
-device virtio-net-pci,bus=pcie.2,netdev=tap1,mac=b8:3f:d2:1d:3e:f1
Script to start the guest
=========================
gshan@nvidia-grace-hopper-01:~/sandbox/qemu/guest$ cat start_full.sh #!/bin/sh key="VGhlIHJlYWxtIGd1ZXN0IHBlcnNvbmFsaXphdGlvbiBrZXkgaW4gZm9ybWF0IG9mIGJhc2U2NCAgICAgICAgIA==" IF_UP_SCRIPT=/etc/qemu-ifup IF_DOWN_SCRIPT=/etc/qemu-ifdown
qemu-system-aarch64 -enable-kvm \
-object rme-guest,id=rme0,measurement-algorithm=sha512,personalization-value=${key} \
-M virt,gic-version=3,confidential-guest-support=rme0 \
-cpu host -smp 4 -m 2G -boot c \
-serial mon:stdio -monitor none -nographic -nodefaults \
-bios /mnt/edk2/Build/ArmVirtQemu-AARCH64/RELEASE_GCC5/FV/QEMU_EFI.fd \
-device pcie-root-port,bus=pcie.0,chassis=1,id=pcie.1 \
-device pcie-root-port,bus=pcie.0,chassis=2,id=pcie.2 \
-drive file=/mnt/rhel10.qcow2,if=none,id=drive0 \
-device virtio-blk-pci,id=virtblk0,bus=pcie.1,drive=drive0,num-queues=4 \
-netdev tap,id=tap0,script=${IF_UP_SCRIPT},downscript=${IF_DOWN_SCRIPT} \-device virtio-net-pci,bus=pcie.2,netdev=tap0,mac=b8:3f:d2:1d:3e:f9
Issues
======
1. virtio-iommu isn't supported by QEMU. The guest kernel becomes stuck at IOMMU probing time where the endpoint's capabilities is queried by sending request over virtio device's vring and the response is expected to be fed by QEMU. The request can't be seen by QEMU due to the wrong IOMMU address translation used in QEMU as virtio-iommu provides a different IOMMU address translation operations to override the platform one, leading the DMA address (in the shared space) can't be properly recognized. The information has been shared to Jean.
2. 'reboot' command doesn't work in the guest. QEMU complains some registers aren't accessible from QEMU. I didn't sorted out a workaround for this.
3. HMP command 'dump-guest-memory' causes QEMU to exit abnormally. The cause is the realm is reconfigured when the VM is resumed after the guest memory is dumped. The reconfiguration is rejected by the host, leading QEMU's abnormal exit. The fix would be to avoid the reconfiguration on the realm. The issue was originally reported by Fujitsu and all the information has been shared to Fujitsu.
4. In QEMU, the CPU property 'kvm-no-adjvtime' can't be set to off. Otherwise, QEMU tries to access the timer registers, which have been hidden by the host. So we need to take the parameter (for QEMU) to by pass it: "-cpu host,kvm-no-adjvtime=on".
5. I didn't try virtio-mem and memory balloon, which isn't expected to work, especially when the guest memory is hot added or hot removed.
Thanks,
Gavin
Quoted 38 lines · Gavin Shan
Quoted 28 lines · Steven Price
This series adds support for running protected VMs using KVM under the Arm Confidential Compute Architecture (CCA).
The related guest support was merged for v6.14-rc1 so you no longer need that separately.
There are a few changes since v9, many thanks for the review comments. The highlights are below, and individual patches have a changelog.
* Fix a potential issue where the host was walking the stage 2 page tables on
realm destruction. If the RMM didn't zero when undelegated (which it isn't
required to) then the kernel would attempt to work the junk values and crash.* Avoid RCU stall warnings by correctly settign
may_blockinkvm_free_stage2_pgd().* Rebased onto v6.17-rc1.
Things to note:
* The magic numbers for capabilities and ioctls have been updated. So you'll need to update your VMM. See below for the updated kvmtool branch.
Quoted 7 lines
* This series doesn't attempt to integrate with the guest-memfd changes that
are being discussed (see below).* Vishal raised an important question about what to do in the case of undelegate failures (also see below).
Quoted 8 lines
[...]
I tried to boot a guest using the following combinations, nothing obvious went to
wrong except several long existing issues (described below). So feel free to add:Tested-by Gavin Shan <[email protected]>
Thanks for testing!
Regards,
Steve
Quoted 127 lines
Combination
===========
host.tf-a https://git.trustedfirmware.org/TF-A/trusted-firmware-
a.git (v2.13-rc0)
host.tf-rmm https://git.codelinaro.org/linaro/dcap/
rmm (cca/v8)
host.edk2 [email protected]:tianocore/
edk2.git (edk2-stable202411) host.kernel [email protected]:gwshan/
linux.git (cca/host-v10) (this series) host.qemu https://git.qemu.org/git/
qemu.git (stable-9.2) host.buildroot https://github.com/buildroot/
buildroot (master)
guest.qemu https://git.codelinaro.org/linaro/dcap/
qemu.git (cca/latest) (with linux-headers sync'ed) guest.kvmtool https://gitlab.arm.com/linux-arm/kvmtool-
cca (cca/latest)
guest.edk2 https://git.codelinaro.org/linaro/dcap/
edk2 (cca/latest)
guest.kernelgit://git.kernel.org/pub/scm/linux/kernel/git/torvalds/
linux.git (v6.17.rc3)
guest.buildroot https://github.com/buildroot/
buildroot (master)Script to start the host
========================gshan@nvidia-grace-hopper-01:~/sandbox/qemu/host$ cat start.sh #!/bin/sh HOST_PATH=/home/gshan/sandbox/qemu/host GUEST_PATH=/home/gshan/sandbox/qemu/guest IF_UP_SCRIPT=/etc/qemu-ifup-gshan IF_DOWN_SCRIPT=/etc/qemu-ifdown-gshansudo ${HOST_PATH}/qemu/build/qemu-system-aarch64 \
(quoted diff) +0
-14-M virt,virtualization=on,secure=on,gic-version=3,acpi=off \ -cpu max,x-rme=on -m 3G -smp 8 \ -serial mon:stdio -monitor none -nographic -nodefaults \ -bios ${HOST_PATH}/tf-a/flash.bin \ -kernel ${HOST_PATH}/linux/arch/arm64/boot/Image \ -initrd ${HOST_PATH}/buildroot/output/images/rootfs.cpio.xz \ -device pcie-root-port,bus=pcie.0,chassis=1,id=pcie.1 \ -device pcie-root-port,bus=pcie.0,chassis=2,id=pcie.2 \ -device pcie-root-port,bus=pcie.0,chassis=3,id=pcie.3 \ -device pcie-root-port,bus=pcie.0,chassis=4,id=pcie.4 \ -device virtio-9p-device,fsdev=shr0,mount_tag=shr0 \ -fsdev local,security_model=none,path=${GUEST_PATH},id=shr0 \ -netdev tap,id=tap1,script=${IF_UP_SCRIPT},downscript=${IF_DOWN_SCRIPT} \ -device virtio-net-pci,bus=pcie.2,netdev=tap1,mac=b8:3f:d2:1d:3e:f1Script to start the guest
=========================gshan@nvidia-grace-hopper-01:~/sandbox/qemu/guest$ cat start_full.sh #!/bin/sh key="VGhlIHJlYWxtIGd1ZXN0IHBlcnNvbmFsaXphdGlvbiBrZXkgaW4gZm9ybWF0IG9mIGJhc2U2NCAgICAgICAgIA==" IF_UP_SCRIPT=/etc/qemu-ifup IF_DOWN_SCRIPT=/etc/qemu-ifdownqemu-system-aarch64 -enable-kvm \
-object rme-guest,id=rme0,measurement-algorithm=sha512,personalization- value=${key} \
-M virt,gic-version=3,confidential-guest-
support=rme0 \
-cpu host -smp 4 -m 2G -boot
c \
-serial mon:stdio -monitor none -nographic -
nodefaults \
-bios/mnt/edk2/Build/ArmVirtQemu-AARCH64/RELEASE_GCC5/FV/QEMU_EFI.fd \
-device pcie-root-
port,bus=pcie.0,chassis=1,id=pcie.1 \
-device pcie-root-
port,bus=pcie.0,chassis=2,id=pcie.2 \
-drive file=/mnt/
rhel10.qcow2,if=none,id=drive0 \
-device virtio-blk-pci,id=virtblk0,bus=pcie.1,drive=drive0,num- queues=4 \
-netdev tap,id=tap0,script=${IF_UP_SCRIPT},downscript=${IF_DOWN_SCRIPT} \
-device virtio-net-pci,bus=pcie.2,netdev=tap0,mac=b8:3f:d2:1d:3e:f9Issues
======
1. virtio-iommu isn't supported by QEMU. The guest kernel becomes stuck at IOMMU
probing time where the endpoint's capabilities is queried by sending request over
virtio device's vring and the response is expected to be fed by QEMU. The request
can't be seen by QEMU due to the wrong IOMMU address translation used in QEMU as
virtio-iommu provides a different IOMMU address translation operations to override
the platform one, leading the DMA address (in the shared space) can't be properly
recognized. The information has been shared to Jean.2. 'reboot' command doesn't work in the guest. QEMU complains some registers aren't
accessible from QEMU. I didn't sorted out a workaround for this.3. HMP command 'dump-guest-memory' causes QEMU to exit abnormally. The cause is the
realm is reconfigured when the VM is resumed after the guest memory is dumped. The
reconfiguration is rejected by the host, leading QEMU's abnormal exit. The fix would
be to avoid the reconfiguration on the realm. The issue was originally reported by
Fujitsu and all the information has been shared to Fujitsu.4. In QEMU, the CPU property 'kvm-no-adjvtime' can't be set to off. Otherwise, QEMU
tries to access the timer registers, which have been hidden by the host. So we need
to take the parameter (for QEMU) to by pass it: "-cpu host,kvm-no- adjvtime=on".5. I didn't try virtio-mem and memory balloon, which isn't expected to work, especially
when the guest memory is hot added or hot removed.Thanks,
Gavin
We tested this patch in our internal simulator which is a hardware simulator for Fujitsu's next generation CPU known as Monaka, and it produced the expected results.
I have verified the following
1. Launching the realm VM using Internal simulator -> Successfully launched by disabling MPAM support in the ID register.
2. Running kvm-unit-tests (with lkvm) -> All tests passed except for PMU (as expected, since PMU is not supported by the Internal simulator).[1]
Quoted 193 lines
This series adds support for running protected VMs using KVM under the Arm Confidential Compute Architecture (CCA).
The related guest support was merged for v6.14-rc1 so you no longer need that separately.
There are a few changes since v9, many thanks for the review comments. The highlights are below, and individual patches have a changelog.
* Fix a potential issue where the host was walking the stage 2 page tables on realm destruction. If the RMM didn't zero when undelegated (which it isn't required to) then the kernel would attempt to work the junk values and crash.
* Avoid RCU stall warnings by correctly settign
may_blockinkvm_free_stage2_pgd().* Rebased onto v6.17-rc1.
Things to note:
* The magic numbers for capabilities and ioctls have been updated. So you'll need to update your VMM. See below for the updated kvmtool branch.
* This series doesn't attempt to integrate with the guest-memfd changes that are being discussed (see below).
* Vishal raised an important question about what to do in the case of undelegate failures (also see below).
guest-memfd
===========This series still implements the same API as previous versions, which means only the private memory of the guest is backed by guest-memfd, and the shared portion is accessed using VMAs from the VMM. This approach is compatible with the proposed changes to support guest-memfd backing shared memory but would require the VMM to
mmap()the shared memory at the appropriate time so that KVM can access via the VMM's VMAs.Changing to access both shared and private through the guest-memfd API shouldn't be difficult and could be done in a backwards compatible manner (with the VMM choosing which method to use). It's not clear to me whether we want to hold things up waiting for the full guest-memfd (and only support that uAPI) or if it would be fine to just support both approaches and let VMM's switch when they are ready.
There is a slight wrinkle around the 'populate' uAPI (
KVM_CAP_ARM_RME_POPULATE). At the moment this expects 'double mapping' - the contents to be populated should be in the shared memory region, but the physical pages for the private region are also allocated from theguest_memfd.
Arm CCA doesn't support a true 'in-place' conversion so this is required in some form. However it may make sense to allow the populate call to take a user space pointer for the data to be copied rather than require it to be in the shared memory region. We do already have a "flags" argument so there's also scope for easily supporting both options. The current approach integrates quite nicely in kvmtool with the existing logic for setting up a normal
(non-CoCo) guest. But I'm aware kvmtool isn't entirely representative of what VMMs do, so I'd welcome feedback on what a good populate uAPI would look like.Undelegation failure
====================When the kernel is tearing down a CCA VM, it will attempt to "undelegate" pages allowing them to be used by the Normal World again. Assuming no bugs then these operations will always succeed. However, the RMM has the ability to return a failure if it considers these pages to still be in use. A failure like this is always a bug in the code, but it would be good to be able to handle these without resorting to an immediate
BUG_ON().The current approach in the code is to simply
WARN()and useget_page()to take an extra reference to the page to stop it being reused (as it will immediately cause a GPF if the Normal World attempts to access the page).However this will cause problems when we start supporting huge pages. It may be possible to use the HWPOISON infrastructure to flag the page as unusable (although note there is no way of 'scrubbing' a page to recover from this situation). The other option is to just accept this this "should never happen"
and upgrade theWARN()to aBUG_ON()so we don't have to deal with the situation. A third option is to do nothing (other than WARN) and let the system run until the inevitable GPF which will probably bring it down (and hope there's enough time for the user to save work etc).I'd welcome thoughts on the best solution.
====================
The ABI to the RMM (the RMI) is based on RMM v1.0-rel0 specification[1].
This series is based on v6.17-rc1. It is also available as a git repository:
https://gitlab.arm.com/linux-arm/linux-cca cca-host/v10
Work in progress changes for kvmtool are available from the git repository below:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/v8
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Jean-Philippe Brucker (7):
arm64: RME: Propagate number of breakpoints and watchpoints to
userspace
arm64: RME: Set breakpoint parameters throughSET_ONE_REG
arm64: RME: Initialize PMCR.N with number counter supported by RMM
arm64: RME: Propagate max SVE vector length from RMM
arm64: RME: Configure max SVE vector length for a Realm
arm64: RME: Provide register list for unfinalized RME RECs
arm64: RME: Provide accurate register listJoey Gouly (2):
arm64: RME: allow userspace to inject aborts
arm64: RME: supportRSI_HOST_CALLSteven Price (31):
arm64: RME: Handle Granule Protection Faults (GPFs)
arm64: RME: Add SMC definitions for calling the RMM
arm64: RME: Add wrappers for RMI calls
arm64: RME: Check for RME support at KVM init
arm64: RME: Define the user ABI
arm64: RME: ioctls to create and configure realms
KVM: arm64: Allow passing machine type in KVM creation
arm64: RME: RTT tear down
arm64: RME: Allocate/free RECs to match vCPUs
KVM: arm64: vgic: Provide helper for number of list registers
arm64: RME: Support for the VGIC in realms
KVM: arm64: Support timers in realm RECs
arm64: RME: Allow VMM to set RIPAS
arm64: RME: Handle realm enter/exit
arm64: RME: HandleRMI_EXIT_RIPAS_CHANGE
KVM: arm64: Handle realm MMIO emulation
arm64: RME: Allow populating initial contents
arm64: RME: Runtime faulting of memory
KVM: arm64: Handle realm VCPU load
KVM: arm64: Validate register access for a Realm VM
KVM: arm64: Handle Realm PSCI requests
KVM: arm64: WARN on injected undef exceptions
arm64: Don't expose stolen time for realm guests
arm64: RME: Always use 4k pages for realms
arm64: RME: Prevent Device mappings for Realms
arm_pmu: Provide a mechanism for disabling the physical IRQ
arm64: RME: Enable PMU support with a realm guest
arm64: RME: HideKVM_CAP_READONLY_MEMfor realm guests
KVM: arm64: Expose support for private memory
KVM: arm64: ExposeKVM_ARM_VCPU_RECto user space
KVM: arm64: Allow activating realmsSuzuki K Poulose (3):
kvm: arm64: Includekvm_emulate.hinkvm/arm_psci.h
kvm: arm64: Don't expose debug capabilities for realm guests
arm64: RME: Allow checking SVE on VM instanceDocumentation/virt/kvm/api.rst | 92 +- arch/arm64/include/asm/kvm_emulate.h | 38 + arch/arm64/include/asm/kvm_host.h | 13 +- arch/arm64/include/asm/kvm_rme.h | 135 ++ arch/arm64/include/asm/rmi_cmds.h | 508 ++++++++ arch/arm64/include/asm/rmi_smc.h | 269 ++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 49 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arch_timer.c | 44 +- arch/arm64/kvm/arm.c | 169 ++- arch/arm64/kvm/guest.c | 108 +- arch/arm64/kvm/hypercalls.c | 4 +- arch/arm64/kvm/inject_fault.c | 5 +- arch/arm64/kvm/mmio.c | 16 +- arch/arm64/kvm/mmu.c | 209 ++- arch/arm64/kvm/pmu-emul.c | 6 + arch/arm64/kvm/psci.c | 30 + arch/arm64/kvm/reset.c | 23 +- arch/arm64/kvm/rme-exit.c | 207 +++ arch/arm64/kvm/rme.c | 1746 ++++++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 53 +- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/kvm/vgic/vgic.c | 61 +- arch/arm64/mm/fault.c | 31 +- drivers/perf/arm_pmu.c | 15 + include/kvm/arm_arch_timer.h | 2 + include/kvm/arm_pmu.h | 4 + include/kvm/arm_psci.h | 2 + include/linux/perf/arm_pmu.h | 5 + include/uapi/linux/kvm.h | 29 +- 32 files changed, 3778 insertions(+), 101 deletions(-) create mode 100644
arch/arm64/include/asm/kvm_rme.hcreate mode 100644arch/arm64/include/asm/rmi_cmds.hcreate mode 100644arch/arm64/include/asm/rmi_smc.hcreate mode 100644arch/arm64/kvm/rme-exit.ccreate mode 100644arch/arm64/kvm/rme.c--
2.43.0
Hi Emi,
Many thanks for testing this!
Regards,
Steve
Quoted 202 lines · Emi Kisanuki (Fujitsu)
We tested this patch in our internal simulator which is a hardware simulator for Fujitsu's next generation CPU known as Monaka, and it produced the expected results.
I have verified the following
1. Launching the realm VM using Internal simulator -> Successfully launched by disabling MPAM support in the ID register.
2. Running kvm-unit-tests (with lkvm) -> All tests passed except for PMU (as expected, since PMU is not supported by the Internal simulator).[1]Tested-by Emi Kisanuki <[email protected]> [1] https://gitlab.arm.com/linux-arm/kvmEmi -unit-tests-cca cca/latestQuoted 193 lines
This series adds support for running protected VMs using KVM under the Arm Confidential Compute Architecture (CCA).
The related guest support was merged for v6.14-rc1 so you no longer need that separately.
There are a few changes since v9, many thanks for the review comments. The highlights are below, and individual patches have a changelog.
* Fix a potential issue where the host was walking the stage 2 page tables on realm destruction. If the RMM didn't zero when undelegated (which it isn't required to) then the kernel would attempt to work the junk values and crash.
* Avoid RCU stall warnings by correctly settign
may_blockinkvm_free_stage2_pgd().* Rebased onto v6.17-rc1.
Things to note:
* The magic numbers for capabilities and ioctls have been updated. So you'll need to update your VMM. See below for the updated kvmtool branch.
* This series doesn't attempt to integrate with the guest-memfd changes that are being discussed (see below).
* Vishal raised an important question about what to do in the case of undelegate failures (also see below).
guest-memfd
===========This series still implements the same API as previous versions, which means only the private memory of the guest is backed by guest-memfd, and the shared portion is accessed using VMAs from the VMM. This approach is compatible with the proposed changes to support guest-memfd backing shared memory but would require the VMM to
mmap()the shared memory at the appropriate time so that KVM can access via the VMM's VMAs.Changing to access both shared and private through the guest-memfd API shouldn't be difficult and could be done in a backwards compatible manner (with the VMM choosing which method to use). It's not clear to me whether we want to hold things up waiting for the full guest-memfd (and only support that uAPI) or if it would be fine to just support both approaches and let VMM's switch when they are ready.
There is a slight wrinkle around the 'populate' uAPI (
KVM_CAP_ARM_RME_POPULATE). At the moment this expects 'double mapping' - the contents to be populated should be in the shared memory region, but the physical pages for the private region are also allocated from theguest_memfd.
Arm CCA doesn't support a true 'in-place' conversion so this is required in some form. However it may make sense to allow the populate call to take a user space pointer for the data to be copied rather than require it to be in the shared memory region. We do already have a "flags" argument so there's also scope for easily supporting both options. The current approach integrates quite nicely in kvmtool with the existing logic for setting up a normal
(non-CoCo) guest. But I'm aware kvmtool isn't entirely representative of what VMMs do, so I'd welcome feedback on what a good populate uAPI would look like.Undelegation failure
====================When the kernel is tearing down a CCA VM, it will attempt to "undelegate" pages allowing them to be used by the Normal World again. Assuming no bugs then these operations will always succeed. However, the RMM has the ability to return a failure if it considers these pages to still be in use. A failure like this is always a bug in the code, but it would be good to be able to handle these without resorting to an immediate
BUG_ON().The current approach in the code is to simply
WARN()and useget_page()to take an extra reference to the page to stop it being reused (as it will immediately cause a GPF if the Normal World attempts to access the page).However this will cause problems when we start supporting huge pages. It may be possible to use the HWPOISON infrastructure to flag the page as unusable (although note there is no way of 'scrubbing' a page to recover from this situation). The other option is to just accept this this "should never happen"
and upgrade theWARN()to aBUG_ON()so we don't have to deal with the situation. A third option is to do nothing (other than WARN) and let the system run until the inevitable GPF which will probably bring it down (and hope there's enough time for the user to save work etc).I'd welcome thoughts on the best solution.
====================
The ABI to the RMM (the RMI) is based on RMM v1.0-rel0 specification[1].
This series is based on v6.17-rc1. It is also available as a git repository:
https://gitlab.arm.com/linux-arm/linux-cca cca-host/v10
Work in progress changes for kvmtool are available from the git repository below:
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/v8
[1] https://developer.arm.com/documentation/den0137/1-0rel0/
Jean-Philippe Brucker (7):
arm64: RME: Propagate number of breakpoints and watchpoints to
userspace
arm64: RME: Set breakpoint parameters throughSET_ONE_REG
arm64: RME: Initialize PMCR.N with number counter supported by RMM
arm64: RME: Propagate max SVE vector length from RMM
arm64: RME: Configure max SVE vector length for a Realm
arm64: RME: Provide register list for unfinalized RME RECs
arm64: RME: Provide accurate register listJoey Gouly (2):
arm64: RME: allow userspace to inject aborts
arm64: RME: supportRSI_HOST_CALLSteven Price (31):
arm64: RME: Handle Granule Protection Faults (GPFs)
arm64: RME: Add SMC definitions for calling the RMM
arm64: RME: Add wrappers for RMI calls
arm64: RME: Check for RME support at KVM init
arm64: RME: Define the user ABI
arm64: RME: ioctls to create and configure realms
KVM: arm64: Allow passing machine type in KVM creation
arm64: RME: RTT tear down
arm64: RME: Allocate/free RECs to match vCPUs
KVM: arm64: vgic: Provide helper for number of list registers
arm64: RME: Support for the VGIC in realms
KVM: arm64: Support timers in realm RECs
arm64: RME: Allow VMM to set RIPAS
arm64: RME: Handle realm enter/exit
arm64: RME: HandleRMI_EXIT_RIPAS_CHANGE
KVM: arm64: Handle realm MMIO emulation
arm64: RME: Allow populating initial contents
arm64: RME: Runtime faulting of memory
KVM: arm64: Handle realm VCPU load
KVM: arm64: Validate register access for a Realm VM
KVM: arm64: Handle Realm PSCI requests
KVM: arm64: WARN on injected undef exceptions
arm64: Don't expose stolen time for realm guests
arm64: RME: Always use 4k pages for realms
arm64: RME: Prevent Device mappings for Realms
arm_pmu: Provide a mechanism for disabling the physical IRQ
arm64: RME: Enable PMU support with a realm guest
arm64: RME: HideKVM_CAP_READONLY_MEMfor realm guests
KVM: arm64: Expose support for private memory
KVM: arm64: ExposeKVM_ARM_VCPU_RECto user space
KVM: arm64: Allow activating realmsSuzuki K Poulose (3):
kvm: arm64: Includekvm_emulate.hinkvm/arm_psci.h
kvm: arm64: Don't expose debug capabilities for realm guests
arm64: RME: Allow checking SVE on VM instanceDocumentation/virt/kvm/api.rst | 92 +- arch/arm64/include/asm/kvm_emulate.h | 38 + arch/arm64/include/asm/kvm_host.h | 13 +- arch/arm64/include/asm/kvm_rme.h | 135 ++ arch/arm64/include/asm/rmi_cmds.h | 508 ++++++++ arch/arm64/include/asm/rmi_smc.h | 269 ++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 49 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arch_timer.c | 44 +- arch/arm64/kvm/arm.c | 169 ++- arch/arm64/kvm/guest.c | 108 +- arch/arm64/kvm/hypercalls.c | 4 +- arch/arm64/kvm/inject_fault.c | 5 +- arch/arm64/kvm/mmio.c | 16 +- arch/arm64/kvm/mmu.c | 209 ++- arch/arm64/kvm/pmu-emul.c | 6 + arch/arm64/kvm/psci.c | 30 + arch/arm64/kvm/reset.c | 23 +- arch/arm64/kvm/rme-exit.c | 207 +++ arch/arm64/kvm/rme.c | 1746 ++++++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 53 +- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/kvm/vgic/vgic.c | 61 +- arch/arm64/mm/fault.c | 31 +- drivers/perf/arm_pmu.c | 15 + include/kvm/arm_arch_timer.h | 2 + include/kvm/arm_pmu.h | 4 + include/kvm/arm_psci.h | 2 + include/linux/perf/arm_pmu.h | 5 + include/uapi/linux/kvm.h | 29 +- 32 files changed, 3778 insertions(+), 101 deletions(-) create mode 100644
arch/arm64/include/asm/kvm_rme.hcreate mode 100644arch/arm64/include/asm/rmi_cmds.hcreate mode 100644arch/arm64/include/asm/rmi_smc.hcreate mode 100644arch/arm64/kvm/rme-exit.ccreate mode 100644arch/arm64/kvm/rme.c--
2.43.0
v11 cover arm64: Support for Arm CCA in KVM
In reply to Steven PriceHi all,
v6.18-rc1 is out, so I've rebased and refreshed the series. Branches are below:
kernel: https://gitlab.arm.com/linux-arm/linux-cca cca-host/v11
kvmtool: https://gitlab.arm.com/linux-arm/kvmtool-cca cca/v9
However I'm not going to spam you all with the patches because there are some significant changes that are still to be worked out which Marc has pointed out (thanks for the review!). Specifically:
* We've agreed that the uAPI should be more like "normal" KVM. So rather than expose the underlying operations (create RECs, init RIPAS, populate memory, activate), the VMM should set this up 'as normal' (but using the guestmem_fd capabilities to mark the memory private) and then on first vcpu run the realm should be configured, RIPAS set based on the memslots/guestmem_fd and any data written in the guestmem_fd populated into the realm.
The upshot of which is that a VMM will require only minimal changes to support CCA, and the ordering requirements (for attestation of setting up the realm will be handled by KVM).
Since this is a big change in the uAPI it's going to take a while to prototype and figure out all the details, so please bear with me while I work on an updated version.
* There are issues with the PMU handling where the host is not provided with as much flexibility it should. For the PMU it's not possible for the host to maintain a PMU counter while the realm is executing. This means that sensible uses like getting an overflow interrupt on the cycle counter break with the use of a realm. This, however, will require a spec update to fix as the RMM will need to in some cases emulate the PMU registers.
* The GIC handling is also more restrictive then it should be. Although the host is responsible for emulating the GIC, it is unable to set trap bits, restricting the host's ability to do this. There is concern that this could limit the ability to implement hardware workarounds in the future. Again a spec update is needed to fix this.
However, changes that you can see (in the branch) include:
* Fixing the naming of various symbols. There were previously a lot of instances of "rme" in functions which were not directly related to the hardware extension. These are now renamed to "rmi" reflecting that they are part of the interface to the RMM.
(This is the only change affecting kvmtool, although it is also binary compatible)
* The code now checks with the hardware feature (RME) before probing for RMI.
* There is now an allowlist for CAPs rather than individually disabling the ones known to be an issue with realms. (This also simplified the code, dropping one patch).
* Fixes for the vgic handling: make sure the VGIC v4 state is synchronised and only populate valid lrs.
* A few other minor updates from reviews, and some minor changes from rebasing.
Thanks,
Steve