Support for Arm CCA VMs on Linux
Subsystems (13)
Suzuki K Poulose
reviewerWe are happy to announce the early RFC version of the Arm Confidential Compute Architecture (CCA) support for the Linux stack. The intention is to seek early feedback in the following areas:
* KVM integration of the Arm CCA
* KVM UABI for managing the Realms, seeking to generalise the operations wherever possible with other Confidential Compute solutions.
Note: This version doesn't support Guest Private memory, which will be added later (see below).
* Linux Guest support for Realms
Arm CCA Introduction
=====================
The Arm CCA is a reference software architecture and implementation that builds on the Realm Management Extension (RME), enabling the execution of Virtual machines, while preventing access by more privileged software, such as hypervisor. The Arm CCA allows the hypervisor to control the VM, but removes the right for access to the code, register state or data that is used by VM.
More information on the architecture is available here[0].
Arm CCA Reference Software Architecture
Realm World || Normal World || Secure World ||
|| | || ||
EL0 x-------x || x----x | x------x || ||
| Realm | || | | | | | || ||
| | || | VM | | | | || ||
----| VM* |---------||-| |---| |-||----------------||
| | || | | | | H | || ||
EL1 x-------x || x----x | | | || ||
^ || | | o | || ||
| || | | | || ||
------- R*------------------------| s -|---------------------
S || | | || ||
I || | t | || ||
| || | | || ||
v || x------x || ||
EL2 RMM* || ^ || ||
^ || | || ||
========|=============================|========================
| | SMC
x--------- *RMI* -------------x EL3 Root World
EL3 Firmware
===============================================================
Where :
RMM - Realm Management Monitor
RMI - Realm Management Interface
RSI - Realm Service Interface
SMC - Secure Monitor Call
RME introduces a new security state "Realm world", in addition to the traditional Secure and Non-Secure states. The Arm CCA defines a new component, Realm Management Monitor (RMM) that runs at R-EL2. This is a standard piece of firmware, verified, installed and loaded by the EL3 firmware (e.g, TF-A), at system boot.
The RMM provides standard interfaces - Realm Management Interface (RMI) - to the Normal world hypervisor to manage the VMs running in the Realm world (also called Realms in short). These are exposed via SMC and are routed through the EL3 firmwre.
The RMI interface includes:
- Move a physical page from the Normal world to the Realm world
- Creating a Realm with requested parameters, tracked via Realm Descriptor (RD)
- Creating VCPUs aka Realm Execution Context (REC), with initial register state.
- Create stage2 translation table at any level.
- Load initial images into Realm Memory from normal world memory
- Schedule RECs (vCPUs) and handle exits
- Inject virtual interrupts into the Realm
- Service stage2 runtime faults with pages (provided by host, scrubbed by RMM).
- Create "shared" mappings that can be accessed by VMM/Hyp.
- Reclaim the memory allocated for the RAM and RTTs (Realm Translation Tables)
However v1.0 of RMM specifications doesn't support:
- Paging protected memory of a Realm VM. Thus the pages backing the protected memory region must be pinned.
- Live migration of Realms.
- Trusted Device assignment.
- Physical interrupt backed Virtual interrupts for Realms
RMM also provides certain services to the Realms via SMC, called Realm Service Interface (RSI). These include:
- Realm Guest Configuration.
- Attestation & Measurement services
- Managing the state of an Intermediate Physical Address (IPA aka GPA) page.
- Host Call service (Communication with the Normal world Hypervisor)
The specifications for the RMM software is currently at *v1.0-Beta2* and the latest version is available here [1].
The Trusted Firmware foundation has an implementation of the RMM - TF-RMM - available here [3].
Implementation
=================
This version of the stack is based on the RMM specification v1.0-Beta0[2], with following exceptions :
- TF-RMM/KVM currently doesn't support the optional features of PMU, SVE and Self-hosted debug (coming soon).
- The RSI_HOST_CALL structure alignment requirement is reduced to match
RMM v1.0 Beta1
- RMI/RSI version numbers do not match the RMM spec. This will be resolved once the spec/implementation is complete, across TF-RMM+Linux stack.
We plan to update the stack to support the latest version of the RMMv1.0 spec in the coming revisions.
This release includes the following components :
a) Linux Kernel
i) Host / KVM support - Support for driving the Realms via RMI. This is dependent on running in the Kernel at EL2 (aka VHE mode). Also provides UABI for VMMs to manage the Realm VMs. The support is restricted to 4K page size, matching the Stage2 granule supported by RMM. The VMM is responsible for making sure the guest memory is locked.
TODO: Guest Private memory[10] integration - We have been following the series and support will be added once it is merged upstream.
ii) Guest support - Support for a Linux Kernel to run in the Realm VM at Realm-EL1, using RSI services. This includes virtio support (virtio-v1.0 only). All I/O are treated as non-secure/shared.
c) kvmtool - VMM changes required to manage Realm VMs. No guest private memory as mentioned above.
d) kvm-unit-tests - Support for running in Realms along with additional tests for RSI ABI.
Running the stack
====================
To run/test the stack, you would need the following components :
1) FVP Base AEM RevC model with FEAT_RME support [4]
2) TF-A firmware for EL3 [5]
3) TF-A RMM for R-EL2 [3]
4) Linux Kernel [6]
5) kvmtool [7]
6) kvm-unit-tests [8]
Instructions for building the firmware components and running the model are available here [9]. Once, the host kernel is booted, a Realm can be launched by invoking the lkvm commad as follows:
$ lkvm run --realm \ --measurement-algo=["sha256", "sha512"] \ --disable-sve \ <normal-vm-options>
Where:
* --measurement-algo (Optional) specifies the algorithm selected for creating the initial measurements by the RMM for this Realm (defaults to sha256).
* GICv3 is mandatory for the Realms.
* SVE is not yet supported in the TF-RMM, and thus must be disabled using --disable-sve
You may also run the kvm-unit-tests inside the Realm world, using the similar options as above.
Links
============
[0] Arm CCA Landing page (See Key Resources section for various documentations)
https://www.arm.com/architecture/security-features/arm-confidential-compute-architecture
[1] RMM Specification Latest
https://developer.arm.com/documentation/den0137/latest
[2] RMM v1.0-Beta0 specification
https://developer.arm.com/documentation/den0137/1-0bet0/
[3] Trusted Firmware RMM - TF-RMM
https://www.trustedfirmware.org/projects/tf-rmm/
GIT: https://git.trustedfirmware.org/TF-RMM/tf-rmm.git
[4] FVP Base RevC AEM Model (available on x86_64 / Arm64 Linux)
https://developer.arm.com/Tools%20and%20Software/Fixed%20Virtual%20Platforms
[5] Trusted Firmware for A class
https://www.trustedfirmware.org/projects/tf-a/
[6] Linux kernel support for Arm-CCA
https://gitlab.arm.com/linux-arm/linux-cca
Host Support branch: cca-host/rfc-v1
Guest Support branch: cca-guest/rfc-v1
[7] kvmtool support for Arm CCA
https://gitlab.arm.com/linux-arm/kvmtool-cca cca/rfc-v1
[8] kvm-unit-tests support for Arm CCA
https://gitlab.arm.com/linux-arm/kvm-unit-tests-cca cca/rfc-v1
[9] Instructions for Building Firmware components and running the model, see section 4.19.2 "Building and running TF-A with RME"
https://trustedfirmware-a.readthedocs.io/en/latest/components/realm-management-extension.html#building-and-running-tf-a-with-rme
[10] fd based Guest Private memory for KVM
https://lkml.kernel.org/r/[email protected]
Cc: Alexandru Elisei <[email protected]>
Cc: Andrew Jones <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Chao Peng <[email protected]>
Cc: Christoffer Dall <[email protected]>
Cc: Fuad Tabba <[email protected]>
Cc: James Morse <[email protected]>
Cc: Jean-Philippe Brucker <[email protected]>
Cc: Joey Gouly <[email protected]>
Cc: Marc Zyngier <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Oliver Upton <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Quentin Perret <[email protected]>
Cc: Sean Christopherson <[email protected]>
Cc: Steven Price <[email protected]>
Cc: Thomas Huth <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Zenghui Yu <[email protected]>
To: [email protected]
To: [email protected]
Cc: [email protected]
Cc: [email protected]
To: [email protected]
To: [email protected]
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
cover arm64: Support for running as a guest in Arm CCA
This series is an RFC adding support for running Linux in a protected VM under the new Arm Confidential Compute Architecture (CCA). The purpose of this series is to gather feedback on the proposed changes to the architecture code for CCA.
The ABI to the RMM from a realm (the RSI) is based on the Beta 0 specification[2] and will be updated in the future when a final version of the specification is published.
This series is based on v6.2-rc1. It is also available as a git repository:
https://gitlab.arm.com/linux-arm/linux-cca cca-guest/rfc-v1
Introduction
============
A more general introduction to Arm CCA is available on the Arm website[3], and links to the other components involved are available in the overall cover letter[4].
Arm Confidential Compute Architecture adds two new 'worlds' to the
architecture: Root and Realm. A new software component known as the RMM (Realm Management Monitor) runs in Realm EL2 and is trusted by both the Normal World and VMs running within Realms. This enables mutual distrust between the Realm VMs and the Normal World.
Virtual machines running within a Realm can decide on a (4k) page-by-page granularity whether to share a page with the (Normal World) host or to keep it private (protected). This protection is provided by the hardware and attempts to access a page which isn't shared by the Normal World will trigger a Granule Protection Fault.
Realm VMs can communicate with the RMM via another SMC interface known as RSI (Realm Services Interface). This series adds wrappers for the full set of RSI commands and uses them to manage the Realm IPA State (RIPAS) and to discover the configuration of the realm.
The VM running within the Realm needs to ensure that memory that is going to use is marked as 'RIPAS_RAM' (i.e. protected memory accessible only to the guest). This could be provided by the VMM (and subject to measurement to ensure it is setup correctly) or the VM can set it itself. This series includes a patch which will iterate over all described RAM and set the RIPAS. An alternative would be to update booting.rst and state this as a requirement, but this would reduce the flexibility of the VMM to manage the available memory to the guest (as the initial RIPAS state is part of the guest's measurement).
Within the Realm the most-significant active bit of the IPA is used to select whether the access is to protected memory or to memory shared with the host. This series treats this bit as if it is attribute bit in the page tables and will modify it when sharing/unsharing memory with the host.
This top bit usage also necessitates that the IPA width is made more dynamic in the guest. The VMM will choose a width (and therefore which bit controls the shared flag) and the guest must be able to identify this bit to mask it out when necessary. PHYS_MASK_SHIFT/PHYS_MASK are therefore made dynamic.
To allow virtio to communicate with the host the shared buffers must be placed in memory which has this top IPA bit set. This is achieved by implementating the set_memory_{encrypted,decrypted} APIs for arm64 and forcing the use of bounce buffers. For now all device access is considered to required the memory to be shared, at this stage there is no support for real devices to be assigned to a realm guest - obviously if device assignment is added this will have to change.
Finally the GIC is (largely) emulated by the (untrusted) host. The RMM provides some management (including register save/restore) but the ITS buffers must be placed into shared memory for the host to emulate. There is likely to be future work to harden the GIC driver against a malicious host (along with any other drivers used within a Realm guest).
[1] https://lore.kernel.org/r/20221202061347.1070246-1-chao.p.peng%40linux.intel.com
[2] https://developer.arm.com/documentation/den0137/1-0bet0/
[3] https://www.arm.com/architecture/security-features/arm-confidential-compute-architecture
[4] https://lore.kernel.org/r/20230127112248.136810-1-suzuki.poulose%40arm.com
Steven Price (9):
arm64: remove redundant 'extern'
arm64: Detect if in a realm and set RIPAS RAM
arm64: realm: Query IPA size from the RMM
arm64: Mark all I/O as non-secure shared
arm64: Make the PHYS_MASK_SHIFT dynamic
arm64: Enforce bounce buffers for realm DMA
arm64: Enable memory encrypt for Realms
arm64: realm: Support nonsecure ITS emulation shared
HACK: Accept prototype RSI version
Suzuki K Poulose (5):
arm64: rsi: Add RSI definitions
fixmap: Allow architecture overriding set_fixmap_io
arm64: Override set_fixmap_io
arm64: Force device mappings to be non-secure shared
efi: arm64: Map Device with Prot Shared
arch/arm64/Kconfig | 3 + arch/arm64/include/asm/fixmap.h | 4 +- arch/arm64/include/asm/io.h | 6 +- arch/arm64/include/asm/kvm_arm.h | 2 +- arch/arm64/include/asm/mem_encrypt.h | 19 ++++ arch/arm64/include/asm/pgtable-hwdef.h | 4 +- arch/arm64/include/asm/pgtable-prot.h | 2 + arch/arm64/include/asm/pgtable.h | 7 +- arch/arm64/include/asm/rsi.h | 46 +++++++++ arch/arm64/include/asm/rsi_cmds.h | 71 +++++++++++++ arch/arm64/include/asm/rsi_smc.h | 132 +++++++++++++++++++++++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/efi.c | 2 +- arch/arm64/kernel/head.S | 2 +- arch/arm64/kernel/rsi.c | 82 +++++++++++++++ arch/arm64/kernel/setup.c | 3 + arch/arm64/kvm/Kconfig | 8 ++ arch/arm64/mm/init.c | 10 +- arch/arm64/mm/mmu.c | 13 +++ arch/arm64/mm/pageattr.c | 48 ++++++++- drivers/irqchip/irq-gic-v3-its.c | 95 +++++++++++++----- include/asm-generic/fixmap.h | 2 + 22 files changed, 523 insertions(+), 40 deletions(-) create mode 100644 arch/arm64/include/asm/mem_encrypt.h create mode 100644 arch/arm64/include/asm/rsi.h create mode 100644 arch/arm64/include/asm/rsi_cmds.h create mode 100644 arch/arm64/include/asm/rsi_smc.h create mode 100644 arch/arm64/kernel/rsi.c
Signature · 1 line
2.34.1
RFC PATCH 1/14 arm64: remove redundant 'extern'
It isn't necessary to mark function definitions extern and goes against the kernel coding style. Remove the redundant extern keyword.
--- arch/arm64/include/asm/fixmap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/fixmap.h +1 -1
@@ -107,7 +107,7 @@ void __init early_fixmap_init(void);#define __late_set_fixmap __set_fixmap#define __late_clear_fixmap (idx) __set_fixmap((idx), 0, FIXMAP_PAGE_CLEAR) -extern void __set_fixmap (enum fixed_addresses idx,phys_addr_t phys,pgprot_t prot); +void __set_fixmap (enum fixed_addresses idx,phys_addr_t phys,pgprot_t prot);#include <asm-generic/fixmap.h>
Signature · 1 line
2.34.1
RFC PATCH 2/14 arm64: rsi: Add RSI definitions
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
The RMM (Realm Management Monitor) provides functionality that can be accessed by a realm guest through SMC (Realm Services Interface) calls.
The SMC definitions are based on DEN0137[1] version A-bet0.
[1] https://developer.arm.com/documentation/den0137/latest
--- arch/arm64/include/asm/rsi_cmds.h | 57 +++++++++++++ arch/arm64/include/asm/rsi_smc.h | 130 ++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 arch/arm64/include/asm/rsi_cmds.h create mode 100644 arch/arm64/include/asm/rsi_smc.h
arch/arm64/include/asm/rsi_cmds.h new file +57 -0
@@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_RSI_CMDS_H +#define __ASM_RSI_CMDS_H + +#include <linux/arm-smccc.h> + +#include <asm/rsi_smc.h> + +enum ripas { +RSI_RIPAS_EMPTY , +RSI_RIPAS_RAM , +}; + +static inline unsigned long rsi_get_version (void ) +{ +struct arm_smccc_res res; + +arm_smccc_smc (SMC_RSI_ABI_VERSION ,0 ,0 ,0 ,0 ,0 ,0 ,0 , &res); + +return res.a0; +} + +static inline unsigned long invoke_rsi_fn_smc (unsigned long function_id, +unsigned long arg0, +unsigned long arg1, +unsigned long arg2, +unsigned long arg3) +{ +struct arm_smccc_res res; + +arm_smccc_smc (function_id, arg0, arg1, arg2, arg3,0 ,0 ,0 , &res); +return res.a0; +} + +static inline void invoke_rsi_fn_smc_with_res (unsigned long function_id, +unsigned long arg0, +unsigned long arg1, +unsigned long arg2, +unsigned long arg3, +struct arm_smccc_res *res) +{ +arm_smccc_smc (function_id, arg0, arg1, arg2, arg3,0 ,0 ,0 , res); +} + +static inline unsigned long rsi_set_addr_range_state (phys_addr_t start, +phys_addr_t end, +enum ripas state) +{ +return invoke_rsi_fn_smc (SMC_RSI_IPA_STATE_SET , + start, (end - start), state,0 ); +} + +#endif
arch/arm64/include/asm/rsi_smc.h new file +130 -0
130 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 3/14 arm64: Detect if in a realm and set RIPAS RAM
In reply to Steven PriceDetect that the VM is a realm guest by the presence of the RSI interface.
If in a realm then all memory needs to be marked as RIPAS RAM initially, the loader may or may not have done this for us. To be sure iterate over all RAM and mark it as such. Any failure is fatal as that implies the RAM regions passed to Linux are incorrect - which would mean failing later when attempting to access non-existent RAM.
--- arch/arm64/include/asm/rsi.h | 46 ++++++++++++++++++++++++++++ arch/arm64/include/asm/rsi_cmds.h | 12 ++++++-- arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/rsi.c | 50 +++++++++++++++++++++++++++++++ arch/arm64/kernel/setup.c | 3 ++ arch/arm64/mm/init.c | 2 ++ 6 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/include/asm/rsi.h create mode 100644 arch/arm64/kernel/rsi.c
arch/arm64/include/asm/rsi.h new file +46 -0
@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_RSI_H_ +#define __ASM_RSI_H_ + +#include <linux/jump_label.h> +#include <asm/rsi_cmds.h> + +extern struct static_key_false rsi_present; + +void arm64_setup_memory (void ); + +void __init arm64_rsi_init (void ); +static inline bool is_realm_world (void ) +{ +return static_branch_unlikely (&rsi_present); +} + +static inline void set_memory_range (phys_addr_t start,phys_addr_t end, +enum ripas state) +{ +unsigned long ret; +phys_addr_t top; + +while (start != end) { + ret =rsi_set_addr_range_state (start, end, state, &top); +BUG_ON (ret); +BUG_ON (top < start); +BUG_ON (top > end); + start = top; + } +} + +static inline void set_memory_range_protected (phys_addr_t start,phys_addr_t end) +{ +set_memory_range (start, end,RSI_RIPAS_RAM ); +} + +static inline void set_memory_range_shared (phys_addr_t start,phys_addr_t end) +{ +set_memory_range (start, end,RSI_RIPAS_EMPTY ); +} +#endif
arch/arm64/include/asm/rsi_cmds.h +9 -3
@@ -48,10 +48,16 @@ static inline void invoke_rsi_fn_smc_with_res(unsigned long function_id,static inline unsigned long rsi_set_addr_range_state (phys_addr_t start,phys_addr_t end, -enum ripas state) +enum ripas state, +phys_addr_t *top) { -return invoke_rsi_fn_smc (SMC_RSI_IPA_STATE_SET , - start, (end - start), state,0 ); +struct arm_smccc_res res; + +invoke_rsi_fn_smc_with_res (SMC_RSI_IPA_STATE_SET , + start, (end - start), state,0 , &res); + + *top = res.a1; +return res.a0; }#endif
arch/arm64/kernel/Makefile +1 -1
@@ -34,7 +34,7 @@ obj-y := debug-monitors.o entry.o irq.o fpsimd.o \ cpufeature.o alternative.o cacheinfo.o \ smp.o smp_spin_table.o topology.o smccc-call.o \ syscall.o proton-pack.o idreg-override.o idle.o \ - patching.o + patching.o rsi.o obj-$(CONFIG_COMPAT) += sys32.o signal32.o \ sys_compat.o
arch/arm64/kernel/rsi.c new file +50 -0
50 lines not on this page · raw message
arch/arm64/kernel/setup.c +3 -0
@@ -43,6 +43,7 @@#include <asm/cpu_ops.h> #include <asm/kasan.h> #include <asm/numa.h> +#include <asm/rsi.h> #include <asm/scs.h> #include <asm/sections.h> #include <asm/setup.h>
@@ -312,6 +313,8 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)* cpufeature code and early parameters. */ jump_label_init (); +/* Init RSI after jump_labels are active */ +arm64_rsi_init ();parse_early_param ();dynamic_scs_init ();
arch/arm64/mm/init.c +2 -0
@@ -39,6 +39,7 @@#include <asm/kvm_host.h> #include <asm/memory.h> #include <asm/numa.h> +#include <asm/rsi.h> #include <asm/sections.h> #include <asm/setup.h> #include <linux/sizes.h>
@@ -412,6 +413,7 @@ void __init arm64_memblock_init(void)reserve_crashkernel (); high_memory =__va (memblock_end_of_DRAM () -1 ) +1 ; +arm64_setup_memory (); }void __init bootmem_init (void )
Signature · 1 line
2.34.1
RFC PATCH 4/14 arm64: realm: Query IPA size from the RMM
In reply to Steven PriceThe top bit of the configured IPA size is used as an attribute to control whether the address is protected or shared. Query the configuration from the RMM to assertain which bit this is.
--- arch/arm64/include/asm/pgtable-prot.h | 2 ++ arch/arm64/include/asm/rsi_cmds.h | 8 ++++++++ arch/arm64/kernel/rsi.c | 8 ++++++++ 3 files changed, 18 insertions(+)
arch/arm64/include/asm/pgtable-prot.h +2 -0
@@ -33,7 +33,9 @@#include <asm/pgtable-types.h> extern bool arm64_use_ng_mappings; +extern unsigned long prot_ns_shared; +#define PROT_NS_SHARED ((prot_ns_shared))#define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
arch/arm64/include/asm/rsi_cmds.h +8 -0
@@ -46,6 +46,14 @@ static inline void invoke_rsi_fn_smc_with_res(unsigned long function_id,arm_smccc_smc (function_id, arg0, arg1, arg2, arg3,0 ,0 ,0 , res); } +static inline unsigned long rsi_get_realm_config (struct realm_config *cfg) +{ +struct arm_smccc_res res; + +invoke_rsi_fn_smc_with_res (SMC_RSI_REALM_CONFIG ,virt_to_phys (cfg),0 ,0 ,0 , &res); +return res.a0; +} + static inline unsigned long rsi_set_addr_range_state(phys_addr_t start, phys_addr_t end, enum ripas state,
arch/arm64/kernel/rsi.c +8 -0
@@ -7,6 +7,11 @@#include <linux/memblock.h> #include <asm/rsi.h> +struct realm_config __attribute((aligned (PAGE_SIZE ))) config; + +unsigned long prot_ns_shared; +EXPORT_SYMBOL (prot_ns_shared); +DEFINE_STATIC_KEY_FALSE_RO (rsi_present);static bool rsi_version_matches (void )
@@ -45,6 +50,9 @@ void __init arm64_rsi_init(void) {if (!rsi_version_matches ())return ; +if (rsi_get_realm_config (&config)) +return ; + prot_ns_shared =BIT (config.ipa_bits -1 );static_branch_enable (&rsi_present); }
Signature · 1 line
2.34.1
RFC PATCH 5/14 arm64: Mark all I/O as non-secure shared
In reply to Steven PriceAll I/O is by default considered non-secure for realms. As such mark them as shared with the host.
--- arch/arm64/include/asm/io.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/include/asm/io.h +3 -3
@@ -142,12 +142,12 @@ extern void __memset_io(volatile void __iomem *, int, size_t);bool ioremap_allowed (phys_addr_t phys_addr,size_t size,unsigned long prot);#define ioremap_allowed ioremap_allowed -#define _PAGE_IOREMAP PROT_DEVICE_nGnRE +#define _PAGE_IOREMAP (PROT_DEVICE_nGnRE | PROT_NS_SHARED)#define ioremap_wc (addr, size) \ - ioremap_prot((addr), (size), PROT_NORMAL_NC) + ioremap_prot((addr), (size), (PROT_NORMAL_NC | PROT_NS_SHARED))#define ioremap_np (addr, size) \ - ioremap_prot((addr), (size), PROT_DEVICE_nGnRnE) + ioremap_prot((addr), (size), (PROT_DEVICE_nGnRnE | PROT_NS_SHARED))/* * io{read,write}{16,32,64}be() macros
Signature · 1 line
2.34.1
RFC PATCH 6/14 fixmap: Allow architecture overriding set_fixmap_io
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
For a realm guest it will be necessary to ensure IO mappings are shared so that the VMM can emulate the device. The following patch will provide an implementation of set_fixmap_io for arm64 setting the shared bit (if in a realm).
--- include/asm-generic/fixmap.h | 2 ++ 1 file changed, 2 insertions(+)
include/asm-generic/fixmap.h +2 -0
@@ -94,8 +94,10 @@ static inline unsigned long virt_to_fix(const unsigned long vaddr)/* * Some fixmaps are for IO */ +#ifndef set_fixmap_io#define set_fixmap_io (idx, phys) \ __set_fixmap(idx, phys, FIXMAP_PAGE_IO) +#endif #define set_fixmap_offset_io (idx, phys) \ __set_fixmap_offset(idx, phys, FIXMAP_PAGE_IO)
Signature · 1 line
2.34.1
RFC PATCH 7/14 arm64: Override set_fixmap_io
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
Override the set_fixmap_io to set shared permission for the host in case of a CC guest. For now we mark it shared unconditionally. Future changes could filter the physical address and make the decision accordingly.
--- arch/arm64/include/asm/fixmap.h | 2 ++ arch/arm64/mm/mmu.c | 13 +++++++++++++ 2 files changed, 15 insertions(+)
arch/arm64/include/asm/fixmap.h +2 -0
@@ -107,6 +107,8 @@ void __init early_fixmap_init(void);#define __late_set_fixmap __set_fixmap#define __late_clear_fixmap (idx) __set_fixmap((idx), 0, FIXMAP_PAGE_CLEAR) +#define set_fixmap_io set_fixmap_io +void set_fixmap_io (enum fixed_addresses idx,phys_addr_t phys);void __set_fixmap (enum fixed_addresses idx,phys_addr_t phys,pgprot_t prot);#include <asm-generic/fixmap.h>
arch/arm64/mm/mmu.c +13 -0
@@ -1279,6 +1279,19 @@ void __set_fixmap(enum fixed_addresses idx, } } +void set_fixmap_io (enum fixed_addresses idx,phys_addr_t phys) +{ +pgprot_t prot =FIXMAP_PAGE_IO ; + +/* +* For now we consider all I/O as non-secure. For future +* filter the I/O base for setting appropriate permissions. +*/ + prot =__pgprot (pgprot_val (prot) |PROT_NS_SHARED ); + +return __set_fixmap (idx, phys, prot); +} +void *__init fixmap_remap_fdt (phys_addr_t dt_phys,int *size,pgprot_t prot) {const u64 dt_virt_base =__fix_to_virt (FIX_FDT );
Signature · 1 line
2.34.1
RFC PATCH 8/14 arm64: Make the PHYS_MASK_SHIFT dynamic
In reply to Steven PriceMake the PHYS_MASK_SHIFT dynamic for Realms. This is only is required for masking the PFN from a pte entry. Elsewhere, we could still use the PA bits configured by the kernel. So, this patch:
-> renames PHYS_MASK_SHIFT -> MAX_PHYS_SHIFT as supported by the kernel -> Makes PHYS_MASK_SHIFT -> Dynamic value of the (I)PA bit width -> For a realm: reduces phys_mask_shift if the RMM reports a smaller
configured size for the guest.
--- arch/arm64/include/asm/kvm_arm.h | 2 +- arch/arm64/include/asm/pgtable-hwdef.h | 4 ++-- arch/arm64/include/asm/pgtable.h | 5 +++++ arch/arm64/kernel/head.S | 2 +- arch/arm64/kernel/rsi.c | 5 +++++ 5 files changed, 14 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_arm.h +1 -1
@@ -346,7 +346,7 @@* bits in PAR are res0. */ #define PAR_TO_HPFAR (par) \ - (((par) & GENMASK_ULL(52 - 1, 12)) >> 8) + (((par) & GENMASK_ULL(MAX_PHYS_MASK_SHIFT - 1, 12)) >> 8)#define ECN (x) { ESR_ELx_EC_##x, #x }
arch/arm64/include/asm/pgtable-hwdef.h +2 -2
@@ -178,8 +178,8 @@/* * Highest possible physical address supported. */ -#define PHYS_MASK_SHIFT (CONFIG_ARM64_PA_BITS) -#define PHYS_MASK ((UL(1) << PHYS_MASK_SHIFT) - 1) +#define MAX_PHYS_MASK_SHIFT (CONFIG_ARM64_PA_BITS) +#define MAX_PHYS_MASK ((UL(1) << PHYS_MASK_SHIFT) - 1)#define TTBR_CNP_BIT (UL(1) << 0)
arch/arm64/include/asm/pgtable.h +5 -0
@@ -35,6 +35,11 @@#include <linux/sched.h> #include <linux/page_table_check.h> +extern unsigned int phys_mask_shift; + +#define PHYS_MASK_SHIFT (phys_mask_shift) +#define PHYS_MASK ((1UL << PHYS_MASK_SHIFT) - 1) +#ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
arch/arm64/kernel/head.S +1 -1
@@ -321,7 +321,7 @@ SYM_FUNC_START_LOCAL(create_idmap) #error "Mismatch between VA_BITS and page size/number of translation levels" #endif #else -#define IDMAP_PGD_ORDER (PHYS_MASK_SHIFT - PGDIR_SHIFT) +#define IDMAP_PGD_ORDER (MAX_PHYS_MASK_SHIFT - PGDIR_SHIFT) #define EXTRA_SHIFT /* * If VA_BITS == 48, we don't have to configure an additional
arch/arm64/kernel/rsi.c +5 -0
@@ -12,6 +12,8 @@ struct realm_config __attribute((aligned(PAGE_SIZE))) config;unsigned long prot_ns_shared;EXPORT_SYMBOL (prot_ns_shared); +unsigned int phys_mask_shift =CONFIG_ARM64_PA_BITS ; +DEFINE_STATIC_KEY_FALSE_RO (rsi_present);static bool rsi_version_matches (void )
@@ -54,5 +56,8 @@ void __init arm64_rsi_init(void)return ; prot_ns_shared =BIT (config.ipa_bits -1 ); +if (config.ipa_bits -1 < phys_mask_shift) + phys_mask_shift = config.ipa_bits -1 ; +static_branch_enable (&rsi_present); }
Signature · 1 line
2.34.1
kernel test robot
build botRe: [RFC PATCH 08/14] arm64: Make the PHYS_MASK_SHIFT dynamic
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in…
Hi Steven,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on kvmarm/next efi/next tip/irq/core arm/for-next arm/fixes soc/for-next linus/master v6.2-rc5 next-20230127] [cannot apply to arnd-asm-generic/master xilinx-xlnx/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Steven-Price/arm64-rsi-Add-RSI-definitions/20230128-113646 base: https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core patch link: https://lore.kernel.org/r/20230127112758.37891-9-steven.price%40arm.com patch subject: [RFC PATCH 08/14] arm64: Make the PHYS_MASK_SHIFT dynamic config: arm64-randconfig-r011-20230129 (https://download.01.org/0day-ci/archive/20230129/[email protected]/config) compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 4196ca3278f78c6e19246e54ab0ecb364e37d66a)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/31ee93a9816b99af56770eb97a8e1f97808fda01
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Steven-Price/arm64-rsi-Add-RSI-definitions/20230128-113646
git checkout 31ee93a9816b99af56770eb97a8e1f97808fda01
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/mm/
If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <[email protected]>
All errors (new ones prefixed by >>):
>> arch/arm64/mm/init.c:91:52: error: initializer element is not a compile-time constant
phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1;
^~~~~~~~~~~~~
arch/arm64/include/asm/pgtable.h:41:20: note: expanded from macro 'PHYS_MASK'
#define PHYS_MASK ((1UL << PHYS_MASK_SHIFT) - 1)
^1 error generated.
vim +91 arch/arm64/mm/init.c
03ef055fd364e4 Mark Rutland 2018-12-07 58 1a8e1cef7603e2 Nicolas Saenz Julienne 2019-09-11 59 /* d78050ee35440d Catalin Marinas 2021-01-07 60 * If the corresponding config options are enabled, we create both ZONE_DMA d78050ee35440d Catalin Marinas 2021-01-07 61 * and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory d78050ee35440d Catalin Marinas 2021-01-07 62 * unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4). d78050ee35440d Catalin Marinas 2021-01-07 63 * In such case, ZONE_DMA32 covers the rest of the 32-bit addressable memory, d78050ee35440d Catalin Marinas 2021-01-07 64 * otherwise it is empty. 031495635b4668 Vijay Balakrishna 2022-03-02 65 * 031495635b4668 Vijay Balakrishna 2022-03-02 66 * Memory reservation for crash kernel either done early or deferred 031495635b4668 Vijay Balakrishna 2022-03-02 67 * depending on DMA memory zones configs (ZONE_DMA) -- 031495635b4668 Vijay Balakrishna 2022-03-02 68 * 031495635b4668 Vijay Balakrishna 2022-03-02 69 * In absence of ZONE_DMA configs arm64_dma_phys_limit initialized 031495635b4668 Vijay Balakrishna 2022-03-02 70 * here instead of max_zone_phys(). This lets early reservation of 031495635b4668 Vijay Balakrishna 2022-03-02 71 * crash kernel memory which has a dependency on arm64_dma_phys_limit. 031495635b4668 Vijay Balakrishna 2022-03-02 72 * Reserving memory early for crash kernel allows linear creation of block 031495635b4668 Vijay Balakrishna 2022-03-02 73 * mappings (greater than page-granularity) for all the memory bank rangs. 031495635b4668 Vijay Balakrishna 2022-03-02 74 * In this scheme a comparatively quicker boot is observed. 031495635b4668 Vijay Balakrishna 2022-03-02 75 * 031495635b4668 Vijay Balakrishna 2022-03-02 76 * If ZONE_DMA configs are defined, crash kernel memory reservation dd671f16b1cdb1 Julia Lawall 2022-03-18 77 * is delayed until DMA zone memory range size initialization performed in 031495635b4668 Vijay Balakrishna 2022-03-02 78 * zone_sizes_init(). The defer is necessary to steer clear of DMA zone 031495635b4668 Vijay Balakrishna 2022-03-02 79 * memory range to avoid overlap allocation. So crash kernel memory boundaries 031495635b4668 Vijay Balakrishna 2022-03-02 80 * are not known when mapping all bank memory ranges, which otherwise means 031495635b4668 Vijay Balakrishna 2022-03-02 81 * not possible to exclude crash kernel range from creating block mappings 031495635b4668 Vijay Balakrishna 2022-03-02 82 * so page-granularity mappings are created for the entire memory range. 031495635b4668 Vijay Balakrishna 2022-03-02 83 * Hence a slightly slower boot is observed. 031495635b4668 Vijay Balakrishna 2022-03-02 84 * dd671f16b1cdb1 Julia Lawall 2022-03-18 85 * Note: Page-granularity mappings are necessary for crash kernel memory 031495635b4668 Vijay Balakrishna 2022-03-02 86 * range for shrinking its size via /sys/kernel/kexec_crash_size interface. 1a8e1cef7603e2 Nicolas Saenz Julienne 2019-09-11 87 */ 031495635b4668 Vijay Balakrishna 2022-03-02 88 #if IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32) 031495635b4668 Vijay Balakrishna 2022-03-02 89 phys_addr_t __ro_after_init arm64_dma_phys_limit; 031495635b4668 Vijay Balakrishna 2022-03-02 90 #else 770093459b9b33 Will Deacon 2022-03-09 @91 phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1; 031495635b4668 Vijay Balakrishna 2022-03-02 92 #endif c1cc1552616d0f Catalin Marinas 2012-03-05 93
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
kernel test robot
build botRe: [RFC PATCH 08/14] arm64: Make the PHYS_MASK_SHIFT dynamic
In reply to Steven Price[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in…
Hi Steven,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on kvmarm/next efi/next tip/irq/core arm/for-next arm/fixes soc/for-next linus/master v6.2-rc5 next-20230127] [cannot apply to xilinx-xlnx/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Steven-Price/arm64-rsi-Add-RSI-definitions/20230128-113646 base: https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core patch link: https://lore.kernel.org/r/20230127112758.37891-9-steven.price%40arm.com patch subject: [RFC PATCH 08/14] arm64: Make the PHYS_MASK_SHIFT dynamic config: arm64-randconfig-s043-20230129 (https://download.01.org/0day-ci/archive/20230130/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 12.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-39-gce1a6720-dirty
# https://github.com/intel-lab-lkp/linux/commit/31ee93a9816b99af56770eb97a8e1f97808fda01
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Steven-Price/arm64-rsi-Add-RSI-definitions/20230128-113646
git checkout 31ee93a9816b99af56770eb97a8e1f97808fda01
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=arm64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=arm64 SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <[email protected]>
All errors (new ones prefixed by >>):
In file included from include/linux/pgtable.h:6,
from include/linux/kasan.h:33,
from include/linux/slab.h:163,
from include/linux/fs.h:45,
from include/linux/cgroup.h:17,
from include/linux/memcontrol.h:13,
from include/linux/swap.h:9,
from arch/arm64/mm/init.c:12:
>> arch/arm64/include/asm/pgtable.h:41:33: error: initializer element is not constant
41 | #define PHYS_MASK ((1UL << PHYS_MASK_SHIFT) - 1)
| ^
arch/arm64/mm/init.c:91:52: note: in expansion of macro 'PHYS_MASK'
91 | phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1;
| ^~~~~~~~~vim +41 arch/arm64/include/asm/pgtable.h
39
40 #define PHYS_MASK_SHIFT (phys_mask_shift)
> 41 #define PHYS_MASK ((1UL << PHYS_MASK_SHIFT) - 1)
42 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
RFC PATCH 9/14 arm64: Enforce bounce buffers for realm DMA
In reply to Steven PriceWithin a realm guest it's not possible for a device emulated by the VMM to access arbitrary guest memory. So force the use of bounce buffers to ensure that the memory the emulated devices are accessing is in memory which is explicitly shared with the host.
--- arch/arm64/kernel/rsi.c | 2 ++ arch/arm64/mm/init.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
arch/arm64/kernel/rsi.c +2 -0
@@ -5,6 +5,8 @@#include <linux/jump_label.h> #include <linux/memblock.h> +#include <linux/swiotlb.h> +#include <asm/rsi.h> struct realm_config __attribute((aligned (PAGE_SIZE ))) config;
arch/arm64/mm/init.c +7 -1
@@ -472,7 +472,13 @@ void __init bootmem_init(void)*/ void __init mem_init (void ) { -swiotlb_init (max_pfn >PFN_DOWN (arm64_dma_phys_limit),SWIOTLB_VERBOSE ); +if (is_realm_world ()) { +swiotlb_init (true,SWIOTLB_VERBOSE |SWIOTLB_FORCE ); +swiotlb_update_mem_attributes (); + }else { +swiotlb_init (max_pfn >PFN_DOWN (arm64_dma_phys_limit), +SWIOTLB_VERBOSE ); + }/* this will put all unused low memory onto the freelists */ memblock_free_all ();
Signature · 1 line
2.34.1
RFC PATCH 10/14 arm64: Enable memory encrypt for Realms
In reply to Steven PriceUse the memory encryption APIs to trigger a RSI call to request a transition between protected memory and shared memory (or vice versa) and updating the kernel's linear map of modified pages to flip the top bit of the IPA. This requires that block mappings are not used in the direct map for realm guests.
--- arch/arm64/Kconfig | 3 ++ arch/arm64/include/asm/mem_encrypt.h | 19 +++++++++++ arch/arm64/kernel/rsi.c | 12 +++++++ arch/arm64/mm/pageattr.c | 48 +++++++++++++++++++++++++--- 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/include/asm/mem_encrypt.h
arch/arm64/Kconfig +3 -0
@@ -20,6 +20,7 @@ config ARM64 select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2 select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE select ARCH_HAS_CACHE_LINE_SIZE + select ARCH_HAS_CC_PLATFORM select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL select ARCH_HAS_DEBUG_VM_PGTABLE
@@ -39,6 +40,8 @@ config ARM64 select ARCH_HAS_SETUP_DMA_OPS select ARCH_HAS_SET_DIRECT_MAP select ARCH_HAS_SET_MEMORY + select ARCH_HAS_MEM_ENCRYPT + select ARCH_HAS_FORCE_DMA_UNENCRYPTED select ARCH_STACKWALK select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX
arch/arm64/include/asm/mem_encrypt.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_MEM_ENCRYPT_H +#define __ASM_MEM_ENCRYPT_H + +#include <asm/rsi.h> + +/* All DMA must be to non-secure memory for now */ +static inline bool force_dma_unencrypted (struct device *dev) +{ +return is_realm_world (); +} + +int set_memory_encrypted (unsigned long addr,int numpages); +int set_memory_decrypted (unsigned long addr,int numpages); +#endif
arch/arm64/kernel/rsi.c +12 -0
@@ -6,6 +6,7 @@#include <linux/jump_label.h> #include <linux/memblock.h> #include <linux/swiotlb.h> +#include <linux/cc_platform.h> #include <asm/rsi.h>
@@ -18,6 +19,17 @@ unsigned int phys_mask_shift = CONFIG_ARM64_PA_BITS;DEFINE_STATIC_KEY_FALSE_RO (rsi_present); +bool cc_platform_has (enum cc_attr attr) +{ +switch (attr) { +case CC_ATTR_MEM_ENCRYPT : +return is_realm_world (); +default : +return false; + } +} +EXPORT_SYMBOL_GPL (cc_platform_has); +static bool rsi_version_matches (void ) {unsigned long ver =rsi_get_version ();
arch/arm64/mm/pageattr.c +44 -4
78 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 11/14 arm64: Force device mappings to be non-secure shared
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
Device mappings (currently) need to be emulated by the VMM so must be mapped shared with the host.
--- arch/arm64/include/asm/pgtable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +1 -1
@@ -567,7 +567,7 @@ static inline void set_pud_at(struct mm_struct *mm, unsigned long addr,#define pgprot_writecombine (prot) \ __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_NORMAL_NC) | PTE_PXN | PTE_UXN)#define pgprot_device (prot) \ - __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_PXN | PTE_UXN) + __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_PXN | PTE_UXN | PROT_NS_SHARED)#define pgprot_tagged (prot) \ __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_NORMAL_TAGGED))#define pgprot_mhp pgprot_tagged
Signature · 1 line
2.34.1
RFC PATCH 12/14 efi: arm64: Map Device with Prot Shared
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
Device mappings need to be emualted by the VMM so must be mapped shared with the host.
--- arch/arm64/kernel/efi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/efi.c +1 -1
@@ -31,7 +31,7 @@ static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)u32 type = md->type;if (type ==EFI_MEMORY_MAPPED_IO ) -return PROT_DEVICE_nGnRE; +return PROT_NS_SHARED | PROT_DEVICE_nGnRE;if (region_is_misaligned (md)) {static bool __initdata code_is_misaligned;
Signature · 1 line
2.34.1
RFC PATCH 13/14 arm64: realm: Support nonsecure ITS emulation shared
In reply to Steven PriceWithin a realm guest the ITS is emulated by the host. This means the allocations must have been made available to the host by a call to set_memory_decrypted(). Introduce an allocation function which performs this extra call.
--- drivers/irqchip/irq-gic-v3-its.c | 95 ++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 24 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +71 -24
@@ -18,6 +18,7 @@#include <linux/irqdomain.h> #include <linux/list.h> #include <linux/log2.h> +#include <linux/mem_encrypt.h> #include <linux/memblock.h> #include <linux/mm.h> #include <linux/msi.h>
@@ -27,6 +28,7 @@#include <linux/of_pci.h> #include <linux/of_platform.h> #include <linux/percpu.h> +#include <linux/set_memory.h> #include <linux/slab.h> #include <linux/syscore_ops.h>
@@ -165,6 +167,7 @@ struct its_device {struct its_node *its;struct event_lpi_map event_map;void *itt; +u32 itt_order;u32 nr_ites;u32 device_id;bool shared;
@@ -200,6 +203,33 @@ static DEFINE_IDA(its_vpeid_ida);#define gic_data_rdist_rd_base () (gic_data_rdist()->rd_base)#define gic_data_rdist_vlpi_base () (gic_data_rdist_rd_base() + SZ_128K) +static struct page *its_alloc_shared_pages_node (int node,gfp_t gfp, +unsigned int order) +{ +struct page *page; + +if (node ==NUMA_NO_NODE ) + page =alloc_pages (gfp, order); +else + page =alloc_pages_node (node, gfp, order); + +if (page) +set_memory_decrypted ((unsigned long )page_address (page), +1 << order); +return page; +} + +static struct page *its_alloc_shared_pages (gfp_t gfp,unsigned int order) +{ +return its_alloc_shared_pages_node (NUMA_NO_NODE , gfp, order); +} + +static void its_free_shared_pages (void *addr,unsigned int order) +{ +set_memory_encrypted ((unsigned long )addr,1 << order); +free_pages ((unsigned long )addr, order); +} +/* * Skip ITSs that have no vLPIs mapped, unless we're on GICv4.1, as we * always have vSGIs mapped.
@@ -2178,7 +2208,8 @@ static struct page *its_allocate_prop_table(gfp_t gfp_flags) {struct page *prop_page; - prop_page =alloc_pages (gfp_flags,get_order (LPI_PROPBASE_SZ )); + prop_page =its_alloc_shared_pages (gfp_flags, +get_order (LPI_PROPBASE_SZ ));if (!prop_page)return NULL ;
@@ -2189,8 +2220,8 @@ static struct page *its_allocate_prop_table(gfp_t gfp_flags)static void its_free_prop_table (struct page *prop_page) { -free_pages ((unsigned long )page_address (prop_page), -get_order (LPI_PROPBASE_SZ )); +its_free_shared_pages (page_address (prop_page), +get_order (LPI_PROPBASE_SZ )); }static bool gic_check_reserved_range (phys_addr_t addr,unsigned long size)
@@ -2312,10 +2343,10 @@ static int its_setup_baser(struct its_node *its, struct its_baser *baser, order =get_order (GITS_BASER_PAGES_MAX * psz); } - page =alloc_pages_node (its->numa_node,GFP_KERNEL | __GFP_ZERO, order); + page =its_alloc_shared_pages_node (its->numa_node, +GFP_KERNEL | __GFP_ZERO, order);if (!page)return -ENOMEM ; - base = (void *)page_address (page); baser_phys =virt_to_phys (base);
@@ -2325,7 +2356,7 @@ static int its_setup_baser(struct its_node *its, struct its_baser *baser,/* 52bit PA is supported only when PageSize=64K */ if (psz !=SZ_64K ) {pr_err ("ITS: no 52bit PA support when psz=%d\n" , psz); -free_pages ((unsigned long )base, order); +its_free_shared_pages (base, order);return -ENXIO ; }
@@ -2379,7 +2410,7 @@ static int its_setup_baser(struct its_node *its, struct its_baser *baser,pr_err ("ITS@%pa: %s doesn't stick: %llx %llx\n" , &its->phys_base, its_base_type_string[type], val, tmp); -free_pages ((unsigned long )base, order); +its_free_shared_pages (base, order);return -ENXIO ; }
@@ -2518,8 +2549,8 @@ static void its_free_tables(struct its_node *its)for (i =0 ; i <GITS_BASER_NR_REGS ; i++) {if (its->tables[i].base) { -free_pages ((unsigned long )its->tables[i].base, - its->tables[i].order); +its_free_shared_pages (its->tables[i].base, + its->tables[i].order); its->tables[i].base =NULL ; } }
@@ -2778,7 +2809,8 @@ static bool allocate_vpe_l2_table(int cpu, u32 id)/* Allocate memory for 2nd level table */ if (!table[idx]) { - page =alloc_pages (GFP_KERNEL | __GFP_ZERO,get_order (psz)); + page =its_alloc_shared_pages (GFP_KERNEL | __GFP_ZERO, +get_order (psz));if (!page)return false;
@@ -2897,7 +2929,8 @@ static int allocate_vpe_l1_table(void)pr_debug ("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n" , np, npg, psz, epp, esz); - page =alloc_pages (GFP_ATOMIC | __GFP_ZERO,get_order (np *PAGE_SIZE )); + page =its_alloc_shared_pages (GFP_ATOMIC | __GFP_ZERO, +get_order (np *PAGE_SIZE ));if (!page)return -ENOMEM ;
@@ -2941,8 +2974,8 @@ static struct page *its_allocate_pending_table(gfp_t gfp_flags) {struct page *pend_page; - pend_page =alloc_pages (gfp_flags | __GFP_ZERO, -get_order (LPI_PENDBASE_SZ )); + pend_page =its_alloc_shared_pages (gfp_flags | __GFP_ZERO, +get_order (LPI_PENDBASE_SZ ));if (!pend_page)return NULL ;
@@ -2954,7 +2987,8 @@ static struct page *its_allocate_pending_table(gfp_t gfp_flags)static void its_free_pending_table (struct page *pt) { -free_pages ((unsigned long )page_address (pt),get_order (LPI_PENDBASE_SZ )); +its_free_shared_pages (page_address (pt), +get_order (LPI_PENDBASE_SZ )); }/*
@@ -3283,8 +3317,9 @@ static bool its_alloc_table_entry(struct its_node *its,/* Allocate memory for 2nd level table */ if (!table[idx]) { - page =alloc_pages_node (its->numa_node,GFP_KERNEL | __GFP_ZERO, -get_order (baser->psz)); + page =its_alloc_shared_pages_node (its->numa_node, +GFP_KERNEL | __GFP_ZERO, +get_order (baser->psz));if (!page)return false;
@@ -3367,7 +3402,9 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id,unsigned long *lpi_map =NULL ;unsigned long flags;u16 *col_map =NULL ; +struct page *page;void *itt; +int itt_order;int lpi_base;int nr_lpis;int nr_ites;
@@ -3379,7 +3416,6 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id,if (WARN_ON (!is_power_of_2 (nvecs))) nvecs =roundup_pow_of_two (nvecs); - dev =kzalloc (sizeof (*dev),GFP_KERNEL );/* * Even if the device wants a single LPI, the ITT must be * sized as a power of two (and you need at least one bit...).
@@ -3387,7 +3423,16 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id, nr_ites =max (2 , nvecs); sz = nr_ites * (FIELD_GET (GITS_TYPER_ITT_ENTRY_SIZE , its->typer) +1 ); sz =max (sz,ITS_ITT_ALIGN ) +ITS_ITT_ALIGN -1 ; - itt =kzalloc_node (sz,GFP_KERNEL , its->numa_node); + itt_order =get_order (sz); + page =its_alloc_shared_pages_node (its->numa_node, +GFP_KERNEL | __GFP_ZERO, + itt_order); +if (!page) +return NULL ; + itt = (void *)page_address (page); + + dev =kzalloc (sizeof (*dev),GFP_KERNEL ); +if (alloc_lpis) { lpi_map =its_lpi_alloc (nvecs, &lpi_base, &nr_lpis);if (lpi_map)
@@ -3399,9 +3444,9 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id, lpi_base =0 ; } -if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) { +if (!dev || !col_map || (!lpi_map && alloc_lpis)) {kfree (dev); -kfree (itt); +its_free_shared_pages (itt, itt_order);bitmap_free (lpi_map);kfree (col_map);return NULL ;
@@ -3411,6 +3456,7 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id, dev->its = its; dev->itt = itt; + dev->itt_order = itt_order; dev->nr_ites = nr_ites; dev->event_map.lpi_map = lpi_map; dev->event_map.col_map = col_map;
@@ -3438,7 +3484,7 @@ static void its_free_device(struct its_device *its_dev)list_del (&its_dev->entry);raw_spin_unlock_irqrestore (&its_dev->its->lock, flags);kfree (its_dev->event_map.col_map); -kfree (its_dev->itt); +its_free_shared_pages (its_dev->itt, its_dev->itt_order);kfree (its_dev); }
@@ -5064,8 +5110,9 @@ static int __init its_probe_one(struct resource *res, its->numa_node = numa_node; - page =alloc_pages_node (its->numa_node,GFP_KERNEL | __GFP_ZERO, -get_order (ITS_CMD_QUEUE_SZ )); + page =its_alloc_shared_pages_node (its->numa_node, +GFP_KERNEL | __GFP_ZERO, +get_order (ITS_CMD_QUEUE_SZ ));if (!page) { err = -ENOMEM ; goto out_unmap_sgir;
@@ -5131,7 +5178,7 @@ static int __init its_probe_one(struct resource *res, out_free_tables:its_free_tables (its); out_free_cmd: -free_pages ((unsigned long )its->cmd_base,get_order (ITS_CMD_QUEUE_SZ )); +its_free_shared_pages (its->cmd_base,get_order (ITS_CMD_QUEUE_SZ )); out_unmap_sgir:if (its->sgir_base)iounmap (its->sgir_base);
Signature · 1 line
2.34.1
RFC PATCH 14/14 HACK: Accept prototype RSI version
In reply to Steven PriceThe upstream RMM currently advertises the major version of an internal prototype (v12.0) rather than the expected version from the RMM architecture specification (v1.0).
Add a config option to enable support for the prototype RSI v12.0.
--- arch/arm64/include/asm/rsi_smc.h | 2 ++ arch/arm64/kernel/rsi.c | 5 +++++ arch/arm64/kvm/Kconfig | 8 ++++++++ 3 files changed, 15 insertions(+)
arch/arm64/include/asm/rsi_smc.h +2 -0
@@ -29,6 +29,8 @@#define RSI_ABI_VERSION ((RSI_ABI_VERSION_MAJOR << 16) | \ RSI_ABI_VERSION_MINOR) +#define RSI_LEGACY_ABI_VERSION 0xc0000 +#define RSI_ABI_VERSION_GET_MAJOR (_version) ((_version) >> 16)#define RSI_ABI_VERSION_GET_MINOR (_version) ((_version) & 0xFFFF)
arch/arm64/kernel/rsi.c +5 -0
@@ -41,6 +41,11 @@ static bool rsi_version_matches(void)RSI_ABI_VERSION_GET_MAJOR (ver), RSI_ABI_VERSION_GET_MINOR(ver)); +#ifdef CONFIG_RME_USE_PROTOTYPE_HACKS +if (ver ==RSI_LEGACY_ABI_VERSION ) +return true; +#endif +return (ver >=RSI_ABI_VERSION &&RSI_ABI_VERSION_GET_MAJOR (ver) ==RSI_ABI_VERSION_MAJOR ); }
arch/arm64/kvm/Kconfig +8 -0
@@ -58,6 +58,14 @@ config NVHE_EL2_DEBUG If unsure, say N. +config RME_USE_PROTOTYPE_HACKS + bool "Allow RMM prototype version numbers" + default y + help + For compatibility with the the current RMM code allow versions + numbers from a prototype implementation as well as the expected + version number from the RMM specification. + config PROTECTED_NVHE_STACKTRACE bool "Protected KVM hypervisor stacktraces" depends on NVHE_EL2_DEBUG
Signature · 1 line
2.34.1
cover arm64: Support for Arm CCA in KVM
In reply to Suzuki K PouloseThis series is an RFC adding support for running protected VMs using KVM under the new Arm Confidential Compute Architecture (CCA). The purpose of this series is to gather feedback on the proposed changes to the architecture code for CCA.
The user ABI is not in it's final form, we plan to make use of the memfd_restricted() allocator[1] and associated infrastructure which will avoid problems in the current user ABI where a malicious VMM may be able to cause a Granule Protection Fault in the kernel (which is fatal).
The ABI to the RMM (the RMI) is based on the Beta 0 specification[2] and will be updated in the future when a final version of the specification is published.
This series is based on v6.2-rc1. It is also available as a git repository:
https://gitlab.arm.com/linux-arm/linux-cca cca-host/rfc-v1
Introduction
============
A more general introduction to Arm CCA is available on the Arm website[3], and links to the other components involved are available in the overall cover letter[4].
Arm Confidential Compute Architecture adds two new 'worlds' to the
architecture: Root and Realm. A new software component known as the RMM (Realm Management Monitor) runs in Realm EL2 and is trusted by both the Normal World and VMs running within Realms. This enables mutual distrust between the Realm VMs and the Normal World.
Virtual machines running within a Realm can decide on a (4k) page-by-page granularity whether to share a page with the (Normal World) host or to keep it private (protected). This protection is provided by the hardware and attempts to access a page which isn't shared by the Normal World will trigger a Granule Protection Fault. The series starts by adding handling for these; faults within user space can be handled by killing the process, faults within kernel space are considered fatal.
The Normal World host can communicate with the RMM via an SMC interface known as RMI (Realm Management Interface), and Realm VMs can communicate with the RMM via another SMC interface known as RSI (Realm Services Interface). This series adds wrappers for the full set of RMI commands and uses them to manage the realm guests.
The Normal World can use RMI commands to delegate pages to the Realm world and to create, manage and run Realm VMs. Once delegated the pages are inaccessible to the Normal World (unless explicitly shared by the guest). However the Normal World may destroy the Realm VM at any time to be able to reclaim (undelegate) the pages.
Entry/exit of a Realm VM attempts to reuse the KVM infrastructure, but ultimately the final mechanism is different. So this series has a bunch of commits handling the differences. As much as possible is placed in a two new files: rme.c and rme-exit.c.
The RMM specification provides a new mechanism for a guest to communicate with host which goes by the name "Host Call". For now this is simply hooked up to the existing support for HVC calls from a normal guest.
[1] https://lore.kernel.org/r/20221202061347.1070246-1-chao.p.peng%40linux.intel.com
[2] https://developer.arm.com/documentation/den0137/1-0bet0/
[3] https://www.arm.com/architecture/security-features/arm-confidential-compute-architecture
[4] .. cover letter ..
Joey Gouly (2):
arm64: rme: allow userspace to inject aborts
arm64: rme: support RSI_HOST_CALL
Steven Price (25):
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
arm64: kvm: Allow passing machine type in KVM creation
arm64: RME: Keep a spare page delegated to the RMM
arm64: RME: RTT handling
arm64: RME: Allocate/free RECs to match vCPUs
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
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
KVM: arm64: Allow activating realms
arm64: RME: Always use 4k pages for realms
HACK: Accept prototype RMI versions
Suzuki K Poulose (1):
arm64: rme: Allow checking SVE on VM instance
Documentation/virt/kvm/api.rst | 3 + arch/arm64/include/asm/kvm_emulate.h | 29 + arch/arm64/include/asm/kvm_host.h | 7 + arch/arm64/include/asm/kvm_rme.h | 98 ++ arch/arm64/include/asm/rmi_cmds.h | 259 +++++ arch/arm64/include/asm/rmi_smc.h | 242 +++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 63 ++ arch/arm64/kvm/Kconfig | 8 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arch_timer.c | 53 +- arch/arm64/kvm/arm.c | 105 +- arch/arm64/kvm/guest.c | 50 + arch/arm64/kvm/inject_fault.c | 2 + arch/arm64/kvm/mmio.c | 7 + arch/arm64/kvm/mmu.c | 80 +- arch/arm64/kvm/psci.c | 23 + arch/arm64/kvm/reset.c | 41 + arch/arm64/kvm/rme-exit.c | 194 ++++ arch/arm64/kvm/rme.c | 1453 ++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic-v3.c | 9 +- arch/arm64/kvm/vgic/vgic.c | 37 +- arch/arm64/mm/fault.c | 29 +- include/kvm/arm_arch_timer.h | 2 + include/uapi/linux/kvm.h | 21 +- 25 files changed, 2772 insertions(+), 47 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.34.1
RFC PATCH 1/28 arm64: RME: Handle Granule Protection Faults (GPFs)
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.
--- arch/arm64/mm/fault.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
arch/arm64/mm/fault.c +24 -5
@@ -756,6 +756,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)) +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" },
@@ -793,11 +812,11 @@ static const struct fault_info fault_info[] = { { 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 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, "unknown 41" }, { do_bad, SIGKILL, SI_KERNEL, "unknown 42" }, { do_bad, SIGKILL, SI_KERNEL, "unknown 43" },
Signature · 1 line
2.34.1
RFC PATCH 2/28 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 A-bet0.
[1] https://developer.arm.com/documentation/den0137/latest
--- arch/arm64/include/asm/rmi_smc.h | 235 +++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +235 -0
@@ -0,0 +1,235 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_RME_SMC_H +#define __ASM_RME_SMC_H + +#include <linux/arm-smccc.h> + +#define SMC_RxI_CALL (func) \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD, \ + (func)) + +/* FID numbers from alp10 specification */ + +#define SMC_RMI_DATA_CREATE SMC_RxI_CALL(0x0153) +#define SMC_RMI_DATA_CREATE_UNKNOWN SMC_RxI_CALL(0x0154) +#define SMC_RMI_DATA_DESTROY SMC_RxI_CALL(0x0155) +#define SMC_RMI_FEATURES SMC_RxI_CALL(0x0165) +#define SMC_RMI_GRANULE_DELEGATE SMC_RxI_CALL(0x0151) +#define SMC_RMI_GRANULE_UNDELEGATE SMC_RxI_CALL(0x0152) +#define SMC_RMI_PSCI_COMPLETE SMC_RxI_CALL(0x0164) +#define SMC_RMI_REALM_ACTIVATE SMC_RxI_CALL(0x0157) +#define SMC_RMI_REALM_CREATE SMC_RxI_CALL(0x0158) +#define SMC_RMI_REALM_DESTROY SMC_RxI_CALL(0x0159) +#define SMC_RMI_REC_AUX_COUNT SMC_RxI_CALL(0x0167) +#define SMC_RMI_REC_CREATE SMC_RxI_CALL(0x015a) +#define SMC_RMI_REC_DESTROY SMC_RxI_CALL(0x015b) +#define SMC_RMI_REC_ENTER SMC_RxI_CALL(0x015c) +#define SMC_RMI_RTT_CREATE SMC_RxI_CALL(0x015d) +#define SMC_RMI_RTT_DESTROY SMC_RxI_CALL(0x015e) +#define SMC_RMI_RTT_FOLD SMC_RxI_CALL(0x0166) +#define SMC_RMI_RTT_INIT_RIPAS SMC_RxI_CALL(0x0168) +#define SMC_RMI_RTT_MAP_UNPROTECTED SMC_RxI_CALL(0x015f) +#define SMC_RMI_RTT_READ_ENTRY SMC_RxI_CALL(0x0161) +#define SMC_RMI_RTT_SET_RIPAS SMC_RxI_CALL(0x0169) +#define SMC_RMI_RTT_UNMAP_UNPROTECTED SMC_RxI_CALL(0x0162) +#define SMC_RMI_VERSION SMC_RxI_CALL(0x0150) + +#define RMI_ABI_MAJOR_VERSION 1 +#define RMI_ABI_MINOR_VERSION 0 + +#define RMI_UNASSIGNED 0 +#define RMI_DESTROYED 1 +#define RMI_ASSIGNED 2 +#define RMI_TABLE 3 +#define RMI_VALID_NS 4 + +#define RMI_ABI_VERSION_GET_MAJOR (version) ((version) >> 16) +#define RMI_ABI_VERSION_GET_MINOR (version) ((version) & 0xFFFF) + +#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 +#define RMI_ERROR_IN_USE 5 + +#define RMI_EMPTY 0 +#define RMI_RAM 1 + +#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(17, 14) +#define RMI_FEATURE_REGISTER_0_NUM_WPS GENMASK(21, 18) +#define RMI_FEATURE_REGISTER_0_PMU_EN BIT(22) +#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS GENMASK(27, 23) +#define RMI_FEATURE_REGISTER_0_HASH_SHA_256 BIT(28) +#define RMI_FEATURE_REGISTER_0_HASH_SHA_512 BIT(29) + +struct realm_params { +union { +u64 features_0; +u8 padding_1[0x100 ]; + }; +union { +u8 measurement_algo; +u8 padding_2[0x300 ]; + }; +union { +u8 rpv[64 ]; +u8 padding_3[0x400 ]; + }; +union { +struct { +u16 vmid; +u8 padding_4[6 ]; +u64 rtt_base; +u64 rtt_level_start; +u32 rtt_num_start; + }; +u8 padding_5[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 { +u64 flags; +u8 padding1[0x100 ]; + }; +union { +u64 mpidr; +u8 padding2[0x100 ]; + }; +union { +u64 pc; +u8 padding3[0x100 ]; + }; +union { +u64 gprs[REC_CREATE_NR_GPRS ]; +u8 padding4[0x500 ]; + }; +u64 num_rec_aux; +u64 aux[REC_PARAMS_AUX_GRANULES ]; +}; + +#define RMI_EMULATED_MMIO BIT(0) +#define RMI_INJECT_SEA BIT(1) +#define RMI_TRAP_WFI BIT(2) +#define RMI_TRAP_WFE BIT(3) + +#define REC_RUN_GPRS 31 +#define REC_GIC_NUM_LRS 16 + +struct rec_entry { +union {/* 0x000 */ +u64 flags; +u8 padding0[0x200 ]; + }; +union {/* 0x200 */ +u64 gprs[REC_RUN_GPRS ]; +u8 padding2[0x100 ]; + }; +union {/* 0x300 */ +struct { +u64 gicv3_hcr; +u64 gicv3_lrs[REC_GIC_NUM_LRS ]; + }; +u8 padding3[0x100 ]; + }; +u8 padding4[0x400 ]; +}; + +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_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_size; +u64 ripas_value;/* Only lowest bit */ + }; +u8 padding5[0x100 ]; + }; +union {/* 0x600 */ +u16 imm; +u8 padding6[0x100 ]; + }; +union {/* 0x700 */ +struct { +u64 pmu_ovf; +u64 pmu_intr_en; +u64 pmu_cntr_en; + }; +u8 padding7[0x100 ]; + }; +}; + +struct rec_run { +struct rec_entry entry; +struct rec_exit exit; +}; + +#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 + +#endif
Signature · 1 line
2.34.1
RFC PATCH 3/28 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.
--- arch/arm64/include/asm/rmi_cmds.h | 259 ++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_cmds.h
arch/arm64/include/asm/rmi_cmds.h new file +259 -0
@@ -0,0 +1,259 @@ +/* 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; +bool ripas; +}; + +static inline int rmi_data_create (unsigned long data,unsigned long rd, +unsigned long map_addr,unsigned long src, +unsigned long flags) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE , data, rd, map_addr, src, + flags, &res); + +return res.a0; +} + +static inline int rmi_data_create_unknown (unsigned long data, +unsigned long rd, +unsigned long map_addr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE_UNKNOWN , data, rd, map_addr, + &res); + +return res.a0; +} + +static inline int rmi_data_destroy (unsigned long rd,unsigned long map_addr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_DESTROY , rd, map_addr, &res); + +return res.a0; +} + +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); + + *out = res.a1; +return res.a0; +} + +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; +} + +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; +} + +static inline int rmi_psci_complete (unsigned long calling_rec, +unsigned long target_rec) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_PSCI_COMPLETE , calling_rec, target_rec, + &res); + +return res.a0; +} + +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; +} + +static inline int rmi_realm_create (unsigned long rd,unsigned long params_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REALM_CREATE , rd, params_ptr, &res); + +return res.a0; +} + +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; +} + +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); + + *aux_count = res.a1; +return res.a0; +} + +static inline int rmi_rec_create (unsigned long rec,unsigned long rd, +unsigned long params_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REC_CREATE , rec, rd, params_ptr, &res); + +return res.a0; +} + +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; +} + +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; +} + +static inline int rmi_rtt_create (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_CREATE , rtt, rd, map_addr, level, + &res); + +return res.a0; +} + +static inline int rmi_rtt_destroy (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_DESTROY , rtt, rd, map_addr, level, + &res); + +return res.a0; +} + +static inline int rmi_rtt_fold (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_FOLD , rtt, rd, map_addr, level, &res); + +return res.a0; +} + +static inline int rmi_rtt_init_ripas (unsigned long rd,unsigned long map_addr, +unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_INIT_RIPAS , rd, map_addr, level, &res); + +return res.a0; +} + +static inline int rmi_rtt_map_unprotected (unsigned long rd, +unsigned long map_addr, +unsigned long level, +unsigned long desc) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_MAP_UNPROTECTED , rd, map_addr, level, + desc, &res); + +return res.a0; +} + +static inline int rmi_rtt_read_entry (unsigned long rd,unsigned long map_addr, +unsigned long level,struct rtt_entry *rtt) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_READ_ENTRY , + rd, map_addr, level + }; + +arm_smccc_1_2_smc (®s, ®s); + + rtt->walk_level = regs.a1; + rtt->state = regs.a2 &0xFF ; + rtt->desc = regs.a3; + rtt->ripas = regs.a4 &1 ; + +return regs.a0; +} + +static inline int rmi_rtt_set_ripas (unsigned long rd,unsigned long rec, +unsigned long map_addr,unsigned long level, +unsigned long ripas) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_SET_RIPAS , rd, rec, map_addr, level, + ripas, &res); + +return res.a0; +} + +static inline int rmi_rtt_unmap_unprotected (unsigned long rd, +unsigned long map_addr, +unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_UNMAP_UNPROTECTED , rd, map_addr, + level, &res); + +return res.a0; +} + +static inline phys_addr_t rmi_rtt_get_phys (struct rtt_entry *rtt) +{ +return rtt->desc &GENMASK (47 ,12 ); +} + +#endif
Signature · 1 line
2.34.1
Re: [RFC PATCH 03/28] arm64: RME: Add wrappers for RMI calls
Quoted 24 lines · On Fri, 27 Jan 2023 11:29:07 +0000 Steven Price <[email protected]> wrote:
The wrappers make the call sites easier to read and deal with the boiler plate of handling the error codes from the RMM.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/rmi_cmds.h | 259 ++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 arch/arm64/include/asm/rmi_cmds.harch/arm64/include/asm/rmi_cmds.h new file +9
-0@@ -0,0 +1,259 @@ +/* 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>
arch/arm64/include/asm/rmi_cmds.h line 19
Quoted 10 lines
arch/arm64/include/asm/rmi_cmds.h new file +10
-0+ +#include <asm/rmi_smc.h> + +struct rtt_entry { +unsigned long walk_level; +unsigned long desc; +int state; +bool ripas; +}; +
It would be nice to have some information of the follwoing wrappers. E.g. meaning of the return value. They will be quite helpful in the later patch review.
Quoted 240 lines
arch/arm64/include/asm/rmi_cmds.h new file +240
-0+static inline int rmi_data_create (unsigned long data,unsigned long rd, +unsigned long map_addr,unsigned long src, +unsigned long flags) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE , data, rd, map_addr, src, + flags, &res); + +return res.a0; +} + +static inline int rmi_data_create_unknown (unsigned long data, +unsigned long rd, +unsigned long map_addr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE_UNKNOWN , data, rd, map_addr, + &res); + +return res.a0; +} + +static inline int rmi_data_destroy (unsigned long rd,unsigned long map_addr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_DESTROY , rd, map_addr, &res); + +return res.a0; +} + +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); + + *out = res.a1; +return res.a0; +} + +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; +} + +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; +} + +static inline int rmi_psci_complete (unsigned long calling_rec, +unsigned long target_rec) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_PSCI_COMPLETE , calling_rec, target_rec, + &res); + +return res.a0; +} + +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; +} + +static inline int rmi_realm_create (unsigned long rd,unsigned long params_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REALM_CREATE , rd, params_ptr, &res); + +return res.a0; +} + +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; +} + +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); + + *aux_count = res.a1; +return res.a0; +} + +static inline int rmi_rec_create (unsigned long rec,unsigned long rd, +unsigned long params_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REC_CREATE , rec, rd, params_ptr, &res); + +return res.a0; +} + +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; +} + +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; +} + +static inline int rmi_rtt_create (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_CREATE , rtt, rd, map_addr, level, + &res); + +return res.a0; +} + +static inline int rmi_rtt_destroy (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_DESTROY , rtt, rd, map_addr, level, + &res); + +return res.a0; +} + +static inline int rmi_rtt_fold (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_FOLD , rtt, rd, map_addr, level, &res); + +return res.a0; +} + +static inline int rmi_rtt_init_ripas (unsigned long rd,unsigned long map_addr, +unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_INIT_RIPAS , rd, map_addr, level, &res); + +return res.a0; +} + +static inline int rmi_rtt_map_unprotected (unsigned long rd, +unsigned long map_addr, +unsigned long level, +unsigned long desc) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_MAP_UNPROTECTED , rd, map_addr, level, + desc, &res); + +return res.a0; +} + +static inline int rmi_rtt_read_entry (unsigned long rd,unsigned long map_addr, +unsigned long level,struct rtt_entry *rtt) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_READ_ENTRY , + rd, map_addr, level + }; + +arm_smccc_1_2_smc (®s, ®s); + + rtt->walk_level = regs.a1; + rtt->state = regs.a2 &0xFF ; + rtt->desc = regs.a3; + rtt->ripas = regs.a4 &1 ; + +return regs.a0; +} + +static inline int rmi_rtt_set_ripas (unsigned long rd,unsigned long rec, +unsigned long map_addr,unsigned long level, +unsigned long ripas) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_SET_RIPAS , rd, rec, map_addr, level, + ripas, &res); + +return res.a0; +} + +static inline int rmi_rtt_unmap_unprotected (unsigned long rd, +unsigned long map_addr, +unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_UNMAP_UNPROTECTED , rd, map_addr, + level, &res); + +return res.a0; +} + +static inline phys_addr_t rmi_rtt_get_phys (struct rtt_entry *rtt) +{ +return rtt->desc &GENMASK (47 ,12 ); +} + +#endif
Re: [RFC PATCH 03/28] arm64: RME: Add wrappers for RMI calls
In reply to Steven PriceHi Steven,
Quoted 264 lines · Steven Price
The wrappers make the call sites easier to read and deal with the boiler plate of handling the error codes from the RMM.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/rmi_cmds.h | 259 ++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+)create mode 100644
arch/arm64/include/asm/rmi_cmds.harch/arm64/include/asm/rmi_cmds.h new file +249
-0@@ -0,0 +1,259 @@ +/* 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; +bool ripas; +}; + +static inline int rmi_data_create (unsigned long data,unsigned long rd, +unsigned long map_addr,unsigned long src, +unsigned long flags) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE , data, rd, map_addr, src, + flags, &res); + +return res.a0; +} + +static inline int rmi_data_create_unknown (unsigned long data, +unsigned long rd, +unsigned long map_addr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_CREATE_UNKNOWN , data, rd, map_addr, + &res); + +return res.a0; +} + +static inline int rmi_data_destroy (unsigned long rd,unsigned long map_addr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_DATA_DESTROY , rd, map_addr, &res); + +return res.a0; +} + +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); + + *out = res.a1; +return res.a0; +} + +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; +} + +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; +} + +static inline int rmi_psci_complete (unsigned long calling_rec, +unsigned long target_rec) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_PSCI_COMPLETE , calling_rec, target_rec, + &res); + +return res.a0; +} + +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; +} + +static inline int rmi_realm_create (unsigned long rd,unsigned long params_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REALM_CREATE , rd, params_ptr, &res); + +return res.a0; +} + +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; +} + +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); + + *aux_count = res.a1; +return res.a0; +} + +static inline int rmi_rec_create (unsigned long rec,unsigned long rd, +unsigned long params_ptr) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_REC_CREATE , rec, rd, params_ptr, &res); + +return res.a0; +} + +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; +} + +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; +} + +static inline int rmi_rtt_create (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_CREATE , rtt, rd, map_addr, level, + &res); + +return res.a0; +} + +static inline int rmi_rtt_destroy (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_DESTROY , rtt, rd, map_addr, level, + &res); + +return res.a0; +} + +static inline int rmi_rtt_fold (unsigned long rtt,unsigned long rd, +unsigned long map_addr,unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_FOLD , rtt, rd, map_addr, level, &res); + +return res.a0; +} + +static inline int rmi_rtt_init_ripas (unsigned long rd,unsigned long map_addr, +unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_INIT_RIPAS , rd, map_addr, level, &res); + +return res.a0; +} + +static inline int rmi_rtt_map_unprotected (unsigned long rd, +unsigned long map_addr, +unsigned long level, +unsigned long desc) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_MAP_UNPROTECTED , rd, map_addr, level, + desc, &res); + +return res.a0; +} + +static inline int rmi_rtt_read_entry (unsigned long rd,unsigned long map_addr, +unsigned long level,struct rtt_entry *rtt) +{ +struct arm_smccc_1_2_regs regs = { +SMC_RMI_RTT_READ_ENTRY , + rd, map_addr, level + }; + +arm_smccc_1_2_smc (®s, ®s); + + rtt->walk_level = regs.a1; + rtt->state = regs.a2 &0xFF ; + rtt->desc = regs.a3; + rtt->ripas = regs.a4 &1 ; + +return regs.a0; +} + +static inline int rmi_rtt_set_ripas (unsigned long rd,unsigned long rec, +unsigned long map_addr,unsigned long level, +unsigned long ripas) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_SET_RIPAS , rd, rec, map_addr, level, + ripas, &res); + +return res.a0; +} + +static inline int rmi_rtt_unmap_unprotected (unsigned long rd, +unsigned long map_addr, +unsigned long level) +{ +struct arm_smccc_res res; + +arm_smccc_1_1_invoke (SMC_RMI_RTT_UNMAP_UNPROTECTED , rd, map_addr, + level, &res);
arch/arm64/include/asm/rmi_cmds.h line 259
Quoted 10 lines
arch/arm64/include/asm/rmi_cmds.h new file +10
-0+ + return res.a0; +} + +static inline phys_addr_t rmi_rtt_get_phys (struct rtt_entry *rtt) +{ +return rtt->desc &GENMASK (47 ,12 ); +} + +#endif
Can we please replace all occurrence of "unsigned long" with u64? Also as per spec, RTT level is Int64, can we change accordingly?
Please CC me in future cca patch-sets.
[email protected]
Thanks,
Ganapat
Re: [RFC PATCH 03/28] arm64: RME: Add wrappers for RMI calls
Quoted 19 lines · Ganapatrao Kulkarni
Hi Steven,
Quoted 15 lines · Steven Price
The wrappers make the call sites easier to read and deal with the boiler plate of handling the error codes from the RMM.
Signed-off-by Steven Price <[email protected]>---
arch/arm64/include/asm/rmi_cmds.h| 259 ++++++++++++++++++++++++++++++1 file changed, 259 insertions(+)create mode 100644
arch/arm64/include/asm/rmi_cmds.harch/arm64/include/asm/rmi_cmds.h new file +0
-0
[...]
Quoted 44 lines
Quoted 44 lines
+static inline int rmi_rtt_read_entry(unsigned long rd, unsigned long
map_addr,
+ unsigned long level, structrtt_entry*rtt)arch/arm64/include/asm/rmi_cmds.h new file +23
-0+{ + struct arm_smccc_1_2_regs regs = { + SMC_RMI_RTT_READ_ENTRY, + rd, map_addr, level + }; + + arm_smccc_1_2_smc(®s, ®s); + + rtt->walk_level = regs.a1; + rtt->state = regs.a2 & 0xFF; + rtt->desc = regs.a3; + rtt->ripas = regs.a4 & 1; + + return regs.a0; +} + +static inline int rmi_rtt_set_ripas (unsigned long rd,unsigned long rec, + unsigned long map_addr, unsigned long level, + unsigned long ripas) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_invoke(SMC_RMI_RTT_SET_RIPAS, rd, rec, map_addr,level,
+ ripas, &res);arch/arm64/include/asm/rmi_cmds.h new file +16
-0+ + return res.a0; +} + +static inline int rmi_rtt_unmap_unprotected (unsigned long rd, + unsigned long map_addr, + unsigned long level) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_invoke(SMC_RMI_RTT_UNMAP_UNPROTECTED, rd, map_addr, + level, &res); + + return res.a0; +} +
Quoted 8 lines
arch/arm64/include/asm/rmi_cmds.h line 259
Quoted 6 lines
arch/arm64/include/asm/rmi_cmds.h new file +6
-0+static inline phys_addr_t rmi_rtt_get_phys (struct rtt_entry *rtt) +{ + return rtt->desc & GENMASK(47, 12); +} + +#endif Can we please replace all occurrence of "unsigned long" with u64?
I'm conflicted here. On the one hand I agree with you - it would be better to use types that are sized according to the RMM spec. However, this file is a thin wrapper around the low-level SMC calls, and the SMCCC interface is a bunch of "unsigned longs" (e.g. look at struct arm_smccc_1_2_regs).
In particular it could be broken to use smaller types (e.g. char/u8) as it would potentially permit the compiler to leave 'junk' in the top part of the register.
So the question becomes whether to stick with the SMCCC interface sizes (unsigned long) or use our knowledge that it must be a 64 bit platform (RMM isn't support for 32 bit) and therefore use u64. My (mild) preference is for unsigned long because it makes it obvious how this relates to the SMCCC interface it's using. It also seems like it would ease compatibility if (/when?) 128 bit registers become a thing.
Quoted 1 line
Also as per spec, RTT level is Int64, can we change accordingly?
Here, however, I agree you've definitely got a point. level should be signed as (at least in theory) it could be negative.
Quoted 2 lines
Please CC me in future cca patch-sets.
[email protected]
I will do, thanks for the review.
Steve
RFC PATCH 4/28 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.
--- arch/arm64/include/asm/kvm_emulate.h | 17 ++++++++++ arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/include/asm/kvm_rme.h | 22 +++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 8 +++++ arch/arm64/kvm/rme.c | 49 ++++++++++++++++++++++++++++ 7 files changed, 103 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 +17 -0
@@ -490,4 +490,21 @@ static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature) return test_bit(feature, vcpu->arch.features); } +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available)) +return kvm->arch.is_realm; +return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -26,6 +26,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -240,6 +241,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 +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_KVM_RME_H +#define __ASM_KVM_RME_H + +enum realm_state { +REALM_STATE_NONE , +REALM_STATE_NEW , +REALM_STATE_ACTIVE , +REALM_STATE_DYING +}; + +struct realm { +enum realm_state state; +}; + +int kvm_init_rme (void ); + +#endif
arch/arm64/include/asm/virt.h +1 -0
@@ -80,6 +80,7 @@ void __hyp_set_vectors(phys_addr_t phys_vector_base);void __hyp_reset_vectors (void );DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DECLARE_STATIC_KEY_FALSE (kvm_rme_is_available);/* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available (void )
arch/arm64/kvm/Makefile +2 -1
@@ -20,7 +20,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-v3.o vgic/vgic-v4.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ - vgic/vgic-its.o vgic/vgic-debug.o + vgic/vgic-its.o vgic/vgic-debug.o \ + rme.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o
arch/arm64/kvm/arm.c +8 -0
@@ -38,6 +38,7 @@#include <asm/kvm_asm.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pkvm.h> +#include <asm/kvm_rme.h> #include <asm/kvm_emulate.h> #include <asm/sections.h>
@@ -47,6 +48,7 @@static enum kvm_mode kvm_mode =KVM_MODE_DEFAULT ;DEFINE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
@@ -2213,6 +2215,12 @@ int kvm_arch_init(void *opaque) in_hyp_mode =is_kernel_in_hyp_mode (); +if (in_hyp_mode) { + err =kvm_init_rme (); +if (err) +return err; + } +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 +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH 04/28] arm64: RME: Check for RME support at KVM init
Quoted 58 lines · On Fri, 27 Jan 2023 11:29:08 +0000 Steven Price <[email protected]> wrote:
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.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 17 ++++++++++ arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/include/asm/kvm_rme.h | 22 +++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 8 +++++ arch/arm64/kvm/rme.c | 49 ++++++++++++++++++++++++++++ 7 files changed, 103 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 +17
-0@@ -490,4 +490,21 @@ static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature) return test_bit(feature, vcpu->arch.features); } +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available)) +return kvm->arch.is_realm; +return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_host.h +1
-0@@ -26,6 +26,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 245
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2
-0@@ -240,6 +241,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +bool is_realm;
^
It would be better to put more comments which really helps on the review.
I was looking for the user of this memeber to see when it is set. It seems it is not in this patch. It would have been nice to have a quick answer from the comments.
Quoted 146 lines
arch/arm64/include/asm/kvm_host.h +1
-0+struct realm realm; };struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_rme.h new file +22
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_KVM_RME_H +#define __ASM_KVM_RME_H + +enum realm_state { +REALM_STATE_NONE , +REALM_STATE_NEW , +REALM_STATE_ACTIVE , +REALM_STATE_DYING +}; + +struct realm { +enum realm_state state; +}; + +int kvm_init_rme (void ); + +#endif arch/arm64/include/asm/virt.h +1
-0@@ -80,6 +80,7 @@ void __hyp_set_vectors(phys_addr_t phys_vector_base);void __hyp_reset_vectors (void );DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DECLARE_STATIC_KEY_FALSE (kvm_rme_is_available);/* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available (void )arch/arm64/kvm/Makefile +2
-1@@ -20,7 +20,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-v3.o vgic/vgic-v4.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ - vgic/vgic-its.o vgic/vgic-debug.o + vgic/vgic-its.o vgic/vgic-debug.o \ + rme.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.oarch/arm64/kvm/arm.c +8
-0@@ -38,6 +38,7 @@#include <asm/kvm_asm.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pkvm.h> +#include <asm/kvm_rme.h> #include <asm/kvm_emulate.h> #include <asm/sections.h> @@ -47,6 +48,7 @@static enum kvm_mode kvm_mode =KVM_MODE_DEFAULT ;DEFINE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);@@ -2213,6 +2215,12 @@ int kvm_arch_init(void *opaque) in_hyp_mode =is_kernel_in_hyp_mode (); +if (in_hyp_mode) { + err =kvm_init_rme (); +if (err) +return err; + } +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 +49
-0@@ -0,0 +1,49 @@ +// 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; +int version_major, version_minor; + +arm_smccc_1_1_invoke (SMC_RMI_VERSION , &res); + +if (res.a0 ==SMCCC_RET_NOT_SUPPORTED ) +return -ENXIO ; + + version_major =RMI_ABI_VERSION_GET_MAJOR (res.a0); + version_minor =RMI_ABI_VERSION_GET_MINOR (res.a0); + +if (version_major !=RMI_ABI_MAJOR_VERSION ) { +kvm_err ("Unsupported RMI ABI (version %d.%d) we support %d\n" , + version_major, version_minor, +RMI_ABI_MAJOR_VERSION ); +return -ENXIO ; + } + +kvm_info ("RMI ABI version %d.%d\n" , version_major, version_minor); + +return 0 ; +} + +int kvm_init_rme (void ) +{ +if (PAGE_SIZE !=SZ_4K ) +/* Only 4k page size on the host is supported */ +return 0 ; + +if (rmi_check_version ()) +/* Continue without realm support */ +return 0 ; + +/* Future patch will enable static branch kvm_rme_is_available */ + +return 0 ; +}
Re: [RFC PATCH 04/28] arm64: RME: Check for RME support at KVM init
Quoted 60 lines · Zhi Wang
Quoted 57 lines · On Fri, 27 Jan 2023 11:29:08 +0000 Steven Price <[email protected]> wrote:
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.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 17 ++++++++++ arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/include/asm/kvm_rme.h | 22 +++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 8 +++++ arch/arm64/kvm/rme.c | 49 ++++++++++++++++++++++++++++ 7 files changed, 103 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 +17
-0@@ -490,4 +490,21 @@ static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature) return test_bit(feature, vcpu->arch.features); } +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available)) +return kvm->arch.is_realm; +return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_host.h +1
-0@@ -26,6 +26,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 245
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +2
-0@@ -240,6 +241,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +bool is_realm;^It would be better to put more comments which really helps on the review.
Thanks for the feedback - I had thought "is realm" was fairly self-documenting, but perhaps I've just spent too much time with this code.
Quoted 3 lines
I was looking for the user of this memeber to see when it is set. It seems it is not in this patch. It would have been nice to have a quick answer from the comments.
The usage is in the kvm_is_realm() function which is used in several of the later patches as a way to detect this kvm guest is a realm guest.
I think the main issue is that I've got the patches in the wrong other. Patch 7 "arm64: kvm: Allow passing machine type in KVM creation" should probably be before this one, then I could add the assignment of is_realm into this patch (potentially splitting out the is_realm parts into another patch).
Thanks,
Steve
Re: [RFC PATCH 04/28] arm64: RME: Check for RME support at KVM init
Quoted 77 lines · On Mon, 13 Feb 2023 15:59:05 +0000 Steven Price <[email protected]> wrote:
Quoted 60 lines · Zhi Wang
Quoted 57 lines · On Fri, 27 Jan 2023 11:29:08 +0000 Steven Price <[email protected]> wrote:
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.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 17 ++++++++++ arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/include/asm/kvm_rme.h | 22 +++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 8 +++++ arch/arm64/kvm/rme.c | 49 ++++++++++++++++++++++++++++ 7 files changed, 103 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 +17
-0@@ -490,4 +490,21 @@ static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature) return test_bit(feature, vcpu->arch.features); } +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available)) +return kvm->arch.is_realm; +return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_host.h +1
-0@@ -26,6 +26,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZEDQuoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 245
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +2
-0@@ -240,6 +241,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +bool is_realm;^It would be better to put more comments which really helps on the review.
Thanks for the feedback - I had thought "is realm" was fairly self-documenting, but perhaps I've just spent too much time with this code.
Quoted 3 lines
I was looking for the user of this memeber to see when it is set. It seems it is not in this patch. It would have been nice to have a quick answer from the comments.
Quoted 9 lines
The usage is in the
kvm_is_realm()function which is used in several of the later patches as a way to detect this kvm guest is a realm guest.I think the main issue is that I've got the patches in the wrong other. Patch 7 "arm64: kvm: Allow passing machine type in KVM creation" should probably be before this one, then I could add the assignment of
is_realminto this patch (potentially splitting out theis_realmparts into another patch).
I agree the patch order seems a problem here. The name is self-documenting but if the user of the variable is not in this patch, still needs to jump to the related patch to confirm if the variable is used as expected. In that situation, a comment would help to avoid jumping between patches (sometimes finding the the user of a variable from a patch bundle really slows down the review progress and eventually you have to open a terminal and check it in the git tree).
Quoted 4 lines
Thanks,
Steve
Re: [RFC PATCH 04/28] arm64: RME: Check for RME support at KVM init
In reply to Steven PriceQuoted 78 lines · On Fri, 27 Jan 2023 11:29:08 +0000 Steven Price <[email protected]> wrote:
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.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 17 ++++++++++ arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/include/asm/kvm_rme.h | 22 +++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 8 +++++ arch/arm64/kvm/rme.c | 49 ++++++++++++++++++++++++++++ 7 files changed, 103 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 +17
-0@@ -490,4 +490,21 @@ static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature) return test_bit(feature, vcpu->arch.features); } +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available)) +return kvm->arch.is_realm; +return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_host.h +4
-0@@ -26,6 +26,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED@@ -240,6 +241,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 +5
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ +
arch/arm64/include/asm/kvm_rme.h line 15
Quoted 10 lines
arch/arm64/include/asm/kvm_rme.h new file +10
-0+#ifndef __ASM_KVM_RME_H +#define __ASM_KVM_RME_H + +enum realm_state { +REALM_STATE_NONE , +REALM_STATE_NEW , +REALM_STATE_ACTIVE , +REALM_STATE_DYING +}; +
By the way, it is better to add more comments to introduce about the states here.
Quoted 121 lines
arch/arm64/include/asm/kvm_rme.h new file +7
-0+struct realm { +enum realm_state state; +}; + +int kvm_init_rme (void ); + +#endif arch/arm64/include/asm/virt.h +1
-0@@ -80,6 +80,7 @@ void __hyp_set_vectors(phys_addr_t phys_vector_base);void __hyp_reset_vectors (void );DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DECLARE_STATIC_KEY_FALSE (kvm_rme_is_available);/* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available (void )arch/arm64/kvm/Makefile +2
-1@@ -20,7 +20,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-v3.o vgic/vgic-v4.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ - vgic/vgic-its.o vgic/vgic-debug.o + vgic/vgic-its.o vgic/vgic-debug.o \ + rme.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.oarch/arm64/kvm/arm.c +8
-0@@ -38,6 +38,7 @@#include <asm/kvm_asm.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pkvm.h> +#include <asm/kvm_rme.h> #include <asm/kvm_emulate.h> #include <asm/sections.h> @@ -47,6 +48,7 @@static enum kvm_mode kvm_mode =KVM_MODE_DEFAULT ;DEFINE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);@@ -2213,6 +2215,12 @@ int kvm_arch_init(void *opaque) in_hyp_mode =is_kernel_in_hyp_mode (); +if (in_hyp_mode) { + err =kvm_init_rme (); +if (err) +return err; + } +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 +49
-0@@ -0,0 +1,49 @@ +// 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; +int version_major, version_minor; + +arm_smccc_1_1_invoke (SMC_RMI_VERSION , &res); + +if (res.a0 ==SMCCC_RET_NOT_SUPPORTED ) +return -ENXIO ; + + version_major =RMI_ABI_VERSION_GET_MAJOR (res.a0); + version_minor =RMI_ABI_VERSION_GET_MINOR (res.a0); + +if (version_major !=RMI_ABI_MAJOR_VERSION ) { +kvm_err ("Unsupported RMI ABI (version %d.%d) we support %d\n" , + version_major, version_minor, +RMI_ABI_MAJOR_VERSION ); +return -ENXIO ; + } + +kvm_info ("RMI ABI version %d.%d\n" , version_major, version_minor); + +return 0 ; +} + +int kvm_init_rme (void ) +{ +if (PAGE_SIZE !=SZ_4K ) +/* Only 4k page size on the host is supported */ +return 0 ; + +if (rmi_check_version ()) +/* Continue without realm support */ +return 0 ; + +/* Future patch will enable static branch kvm_rme_is_available */ + +return 0 ; +}
Re: [RFC PATCH 04/28] arm64: RME: Check for RME support at KVM init
In reply to Steven PriceQuoted 175 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.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 17 ++++++++++ arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/include/asm/kvm_rme.h | 22 +++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 8 +++++ arch/arm64/kvm/rme.c | 49 ++++++++++++++++++++++++++++ 7 files changed, 103 insertions(+), 1 deletion(-)create mode 100644
arch/arm64/include/asm/kvm_rme.hcreate mode 100644arch/arm64/kvm/rme.carch/arm64/include/asm/kvm_emulate.h +17
-0@@ -490,4 +490,21 @@ static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature) return test_bit(feature, vcpu->arch.features); } +static inline bool kvm_is_realm (struct kvm *kvm) +{ +if (static_branch_unlikely (&kvm_rme_is_available)) +return kvm->arch.is_realm; +return false; +} + +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* __ARM64_KVM_EMULATE_H__ */ arch/arm64/include/asm/kvm_host.h +4
-0@@ -26,6 +26,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_rme.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED@@ -240,6 +241,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 +22
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2023 ARM Ltd. +*/ + +#ifndef __ASM_KVM_RME_H +#define __ASM_KVM_RME_H + +enum realm_state { +REALM_STATE_NONE , +REALM_STATE_NEW , +REALM_STATE_ACTIVE , +REALM_STATE_DYING +}; + +struct realm { +enum realm_state state; +}; + +int kvm_init_rme (void ); + +#endif arch/arm64/include/asm/virt.h +1
-0@@ -80,6 +80,7 @@ void __hyp_set_vectors(phys_addr_t phys_vector_base);void __hyp_reset_vectors (void );DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DECLARE_STATIC_KEY_FALSE (kvm_rme_is_available);/* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available (void )arch/arm64/kvm/Makefile +2
-1@@ -20,7 +20,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-v3.o vgic/vgic-v4.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ - vgic/vgic-its.o vgic/vgic-debug.o + vgic/vgic-its.o vgic/vgic-debug.o \ + rme.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.oarch/arm64/kvm/arm.c +8
-0@@ -38,6 +38,7 @@#include <asm/kvm_asm.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pkvm.h> +#include <asm/kvm_rme.h> #include <asm/kvm_emulate.h> #include <asm/sections.h> @@ -47,6 +48,7 @@static enum kvm_mode kvm_mode =KVM_MODE_DEFAULT ;DEFINE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +DEFINE_STATIC_KEY_FALSE (kvm_rme_is_available); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);@@ -2213,6 +2215,12 @@ int kvm_arch_init(void *opaque) in_hyp_mode =is_kernel_in_hyp_mode (); +if (in_hyp_mode) { + err =kvm_init_rme (); +if (err) +return err; + } +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 +15
-0@@ -0,0 +1,49 @@ +// 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; +int version_major, version_minor; +
arch/arm64/kvm/rme.c line 25
Quoted 10 lines
arch/arm64/kvm/rme.c new file +10
-0+arm_smccc_1_1_invoke (SMC_RMI_VERSION , &res); + +if (res.a0 ==SMCCC_RET_NOT_SUPPORTED ) +return -ENXIO ; + + version_major =RMI_ABI_VERSION_GET_MAJOR (res.a0); + version_minor =RMI_ABI_VERSION_GET_MINOR (res.a0); + +if (version_major !=RMI_ABI_MAJOR_VERSION ) { + kvm_err("Unsupported RMI ABI (version %d.%d) we support %d\n",
Can we please replace "we support" to host supports.
Also in the patch present in the repo, you are using variable our_version, can this be changed to host_version?
Quoted 14 lines
arch/arm64/kvm/rme.c new file +14
-0+ version_major, version_minor, + RMI_ABI_MAJOR_VERSION); +return -ENXIO ; + } + +kvm_info ("RMI ABI version %d.%d\n" , version_major, version_minor); + +return 0 ; +} + +int kvm_init_rme (void ) +{ +if (PAGE_SIZE !=SZ_4K ) +/* Only 4k page size on the host is supported */
arch/arm64/kvm/rme.c line 49
Quoted 10 lines
arch/arm64/kvm/rme.c new file +10
-0+return 0 ; + +if (rmi_check_version ()) +/* Continue without realm support */ +return 0 ; + +/* Future patch will enable static branch kvm_rme_is_available */ + +return 0 ; +}
Thanks,
Ganapat
Re: [RFC PATCH 04/28] arm64: RME: Check for RME support at KVM init
Quoted 22 lines · Ganapatrao Kulkarni
Quoted 19 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.
Signed-off-by Steven Price <[email protected]>---
arch/arm64/include/asm/kvm_emulate.h| 17 ++++++++++ arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/include/asm/kvm_rme.h | 22 +++++++++++++ arch/arm64/include/asm/virt.h | 1 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 8 +++++ arch/arm64/kvm/rme.c | 49 ++++++++++++++++++++++++++++7 files changed, 103 insertions(+), 1 deletion(-)create mode 100644
arch/arm64/include/asm/kvm_rme.hcreate mode 100644arch/arm64/kvm/rme.c
[...]
Quoted 27 lines
Quoted 27 lines
arch/arm64/kvm/rme.c new file +21
-0@@ -0,0 +1,49 @@ +// 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; + int version_major, version_minor; + + arm_smccc_1_1_invoke(SMC_RMI_VERSION, &res); + + if (res.a0 == SMCCC_RET_NOT_SUPPORTED) + return -ENXIO; + + version_major = RMI_ABI_VERSION_GET_MAJOR(res.a0);
Quoted 8 lines
arch/arm64/kvm/rme.c line 25
Quoted 4 lines
arch/arm64/kvm/rme.c new file +4
-0+ version_minor = RMI_ABI_VERSION_GET_MINOR(res.a0); + + if (version_major != RMI_ABI_MAJOR_VERSION) { + kvm_err("Unsupported RMI ABI (version %d.%d) we support %d\n",Can we please replace "we support" to host supports. Also in the patch present in the repo, you are using variable
our_version, can this be changed tohost_version?
Sure, I do have a bad habit using "we" - thanks for point it out.
Steve
Quoted 27 lines
Quoted 14 lines
arch/arm64/kvm/rme.c new file +14
-0+ version_major, version_minor, + RMI_ABI_MAJOR_VERSION); + return -ENXIO; + } + + kvm_info("RMI ABI version %d.%d\n", version_major, version_minor); + + return 0; +} + +int kvm_init_rme (void ) +{ + if (PAGE_SIZE != SZ_4K) + /* Only 4k page size on the host is supported */arch/arm64/kvm/rme.c line 49
Quoted 10 lines
arch/arm64/kvm/rme.c new file +10
-0+ return 0; + + if (rmi_check_version()) + /* Continue without realm support */ + return 0; + + /* Future patch will enable static branch kvm_rme_is_available */ + + return 0; +}Thanks,
Ganapat
RFC PATCH 5/28 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.
--- Documentation/virt/kvm/api.rst | 1 + arch/arm64/include/uapi/asm/kvm.h | 63 +++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 2 + 3 files changed, 66 insertions(+)
Documentation/virt/kvm/api.rst +1 -0
@@ -4965,6 +4965,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.
arch/arm64/include/uapi/asm/kvm.h +63 -0
75 lines not on this page · raw message
include/uapi/linux/kvm.h +2 -0
@@ -1176,6 +1176,8 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224#define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225 +#define KVM_CAP_ARM_RME 300 // FIXME: Large number to prevent conflicts +#ifdef KVM_CAP_IRQ_ROUTING struct kvm_irq_routing_irqchip {
Signature · 1 line
2.34.1
kernel test robot
build botRe: [RFC PATCH 05/28] arm64: RME: Define the user ABI
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in…
Hi Steven,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on arm64/for-next/core]
[also build test WARNING on linus/master v6.2-rc5 next-20230127] [cannot apply to kvmarm/next kvm/queue kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Steven-Price/arm64-RME-Add-SMC-definitions-for-calling-the-RMM/20230128-180035 base: https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core patch link: https://lore.kernel.org/r/20230127112932.38045-6-steven.price%40arm.com patch subject: [RFC PATCH 05/28] arm64: RME: Define the user ABI config: i386-randconfig-a013 (https://download.01.org/0day-ci/archive/20230129/[email protected]/config) compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/8ea2cfe90a1d6ee4dff4fdedef77ac12daf037d1
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Steven-Price/arm64-RME-Add-SMC-definitions-for-calling-the-RMM/20230128-180035 git checkout 8ea2cfe90a1d6ee4dff4fdedef77ac12daf037d1
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <[email protected]>
All warnings (new ones prefixed by >>):
In file included from <built-in>:1:
>> ./usr/include/linux/kvm.h:1177:29: warning: // comments are not allowed in this language [-Wcomment]
#define KVM_CAP_ARM_RME 300 // FIXME: Large number to prevent conflicts
^1 warning generated.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Re: [RFC PATCH 05/28] arm64: RME: Define the user ABI
In reply to Steven PriceQuoted 35 lines · On Fri, 27 Jan 2023 11:29:09 +0000 Steven Price <[email protected]> wrote:
There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/api.rst | 1 + arch/arm64/include/uapi/asm/kvm.h | 63 +++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 2 + 3 files changed, 66 insertions(+)Documentation/virt/kvm/api.rst +1
-0@@ -4965,6 +4965,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.arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -109,6 +109,7 @@ struct kvm_regs {#define KVM_ARM_VCPU_SVE 4/* enable SVE for this CPU */ #define KVM_ARM_VCPU_PTRAUTH_ADDRESS 5/* VCPU uses address authentication */ #define KVM_ARM_VCPU_PTRAUTH_GENERIC 6/* VCPU uses generic authentication */ +#define KVM_ARM_VCPU_REC 7/* VCPU REC state as part of Realm */ struct kvm_vcpu_init {__u32 target;
arch/arm64/include/uapi/asm/kvm.h enum { · line 411
Quoted 10 lines
arch/arm64/include/uapi/asm/kvm.h +7
-0@@ -401,6 +402,68 @@ enum {#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3#define KVM_DEV_ARM_ITS_CTRL_RESET 4 +/* KVM_CAP_ARM_RME on VM fd */ +#define KVM_CAP_ARM_RME_CONFIG_REALM 0 +#define KVM_CAP_ARM_RME_CREATE_RD 1 +#define KVM_CAP_ARM_RME_INIT_IPA_REALM 2 +#define KVM_CAP_ARM_RME_POPULATE_REALM 3 +#define KVM_CAP_ARM_RME_ACTIVATE_REALM 4 +
It is a little bit confusing here. These seems more like 'commands' not caps. Will leave more comments after reviewing the later patches.
Quoted 71 lines
arch/arm64/include/uapi/asm/kvm.h +55
-0+#define KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 0 +#define KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 1 + +#define KVM_CAP_ARM_RME_RPV_SIZE 64 + +/* List of configuration items accepted for KVM_CAP_ARM_RME_CONFIG_REALM */ +#define KVM_CAP_ARM_RME_CFG_RPV 0 +#define KVM_CAP_ARM_RME_CFG_HASH_ALGO 1 +#define KVM_CAP_ARM_RME_CFG_SVE 2 +#define KVM_CAP_ARM_RME_CFG_DBG 3 +#define KVM_CAP_ARM_RME_CFG_PMU 4 + +struct kvm_cap_arm_rme_config_item { +__u32 cfg; +union { +/* cfg == KVM_CAP_ARM_RME_CFG_RPV */ +struct { +__u8 rpv[KVM_CAP_ARM_RME_RPV_SIZE ]; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_HASH_ALGO */ +struct { +__u32 hash_algo; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_SVE */ +struct { +__u32 sve_vq; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_DBG */ +struct { +__u32 num_brps; +__u32 num_wrps; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_PMU */ +struct { +__u32 num_pmu_cntrs; + }; +/* Fix the size of the union */ +__u8 reserved[256 ]; + }; +}; + +struct kvm_cap_arm_rme_populate_realm_args { +__u64 populate_ipa_base; +__u64 populate_ipa_size; +}; + +struct kvm_cap_arm_rme_init_ipa_args { +__u64 init_ipa_base; +__u64 init_ipa_size; +}; +/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0#define KVM_ARM_VCPU_PMU_V3_IRQ 0include/uapi/linux/kvm.h +2
-0@@ -1176,6 +1176,8 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224#define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225 +#define KVM_CAP_ARM_RME 300 // FIXME: Large number to prevent conflicts +#ifdef KVM_CAP_IRQ_ROUTING struct kvm_irq_routing_irqchip {
Re: [RFC PATCH 05/28] arm64: RME: Define the user ABI
Quoted 41 lines · Zhi Wang
Quoted 38 lines · On Fri, 27 Jan 2023 11:29:09 +0000 Steven Price <[email protected]> wrote:
There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/api.rst | 1 + arch/arm64/include/uapi/asm/kvm.h | 63 +++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 2 + 3 files changed, 66 insertions(+)Documentation/virt/kvm/api.rst +1
-0@@ -4965,6 +4965,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.arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -109,6 +109,7 @@ struct kvm_regs {#define KVM_ARM_VCPU_SVE 4/* enable SVE for this CPU */ #define KVM_ARM_VCPU_PTRAUTH_ADDRESS 5/* VCPU uses address authentication */ #define KVM_ARM_VCPU_PTRAUTH_GENERIC 6/* VCPU uses generic authentication */ +#define KVM_ARM_VCPU_REC 7/* VCPU REC state as part of Realm */ struct kvm_vcpu_init {__u32 target;@@ -401,6 +402,68 @@ enum {#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3#define KVM_DEV_ARM_ITS_CTRL_RESET 4
Quoted 10 lines
arch/arm64/include/uapi/asm/kvm.h enum { · line 411
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h +7
-0+/* KVM_CAP_ARM_RME on VM fd */ +#define KVM_CAP_ARM_RME_CONFIG_REALM 0 +#define KVM_CAP_ARM_RME_CREATE_RD 1 +#define KVM_CAP_ARM_RME_INIT_IPA_REALM 2 +#define KVM_CAP_ARM_RME_POPULATE_REALM 3 +#define KVM_CAP_ARM_RME_ACTIVATE_REALM 4 +It is a little bit confusing here. These seems more like 'commands' not caps. Will leave more comments after reviewing the later patches.
Sorry for the slow response. Thank you for your review - I hope to post a new version of this series (rebased on 6.3-rc1) in the coming weeks with your comments addressed.
They are indeed commands - and using caps is a bit of a hack. The benefit here is that all the Realm commands are behind the one KVM_CAP_ARM_RME.
The options I can see are:
a) What I've got here - (ab)using KVM_ENABLE_CAP to perform commands.
b) Add new ioctls for each of the above stages (so 5 new ioctls on top of the CAP for discovery). With any future extensions requiring new ioctls.
c) Add a single new multiplexing ioctl (along with the CAP for discovery).
I'm not massively keen on defining a new multiplexing scheme (c), but equally (b) seems like it's burning through ioctl numbers. Which led me to stick with (a) which at least keeps the rebasing simple (there's only the one CAP number which could conflict) and there's already a multiplexing scheme.
But I'm happy to change if there's consensus a different approach would be preferable.
Thanks,
Steve
Quoted 72 lines
Quoted 71 lines
arch/arm64/include/uapi/asm/kvm.h +55
-0+#define KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 0 +#define KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 1 + +#define KVM_CAP_ARM_RME_RPV_SIZE 64 + +/* List of configuration items accepted for KVM_CAP_ARM_RME_CONFIG_REALM */ +#define KVM_CAP_ARM_RME_CFG_RPV 0 +#define KVM_CAP_ARM_RME_CFG_HASH_ALGO 1 +#define KVM_CAP_ARM_RME_CFG_SVE 2 +#define KVM_CAP_ARM_RME_CFG_DBG 3 +#define KVM_CAP_ARM_RME_CFG_PMU 4 + +struct kvm_cap_arm_rme_config_item { +__u32 cfg; +union { +/* cfg == KVM_CAP_ARM_RME_CFG_RPV */ +struct { +__u8 rpv[KVM_CAP_ARM_RME_RPV_SIZE ]; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_HASH_ALGO */ +struct { +__u32 hash_algo; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_SVE */ +struct { +__u32 sve_vq; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_DBG */ +struct { +__u32 num_brps; +__u32 num_wrps; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_PMU */ +struct { +__u32 num_pmu_cntrs; + }; +/* Fix the size of the union */ +__u8 reserved[256 ]; + }; +}; + +struct kvm_cap_arm_rme_populate_realm_args { +__u64 populate_ipa_base; +__u64 populate_ipa_size; +}; + +struct kvm_cap_arm_rme_init_ipa_args { +__u64 init_ipa_base; +__u64 init_ipa_size; +}; +/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0#define KVM_ARM_VCPU_PMU_V3_IRQ 0include/uapi/linux/kvm.h +2
-0@@ -1176,6 +1176,8 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224#define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225 +#define KVM_CAP_ARM_RME 300 // FIXME: Large number to prevent conflicts +#ifdef KVM_CAP_IRQ_ROUTING struct kvm_irq_routing_irqchip {
Re: [RFC PATCH 05/28] arm64: RME: Define the user ABI
Quoted 53 lines · On Wed, 1 Mar 2023 11:54:34 +0000 Steven Price <[email protected]> wrote:
Quoted 41 lines · Zhi Wang
Quoted 38 lines · On Fri, 27 Jan 2023 11:29:09 +0000 Steven Price <[email protected]> wrote:
There is one (multiplexed) CAP which can be used to create, populate and then activate the realm.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/api.rst | 1 + arch/arm64/include/uapi/asm/kvm.h | 63 +++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 2 + 3 files changed, 66 insertions(+)Documentation/virt/kvm/api.rst +1
-0@@ -4965,6 +4965,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.arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -109,6 +109,7 @@ struct kvm_regs {#define KVM_ARM_VCPU_SVE 4/* enable SVE for this CPU */ #define KVM_ARM_VCPU_PTRAUTH_ADDRESS 5/* VCPU uses address authentication */ #define KVM_ARM_VCPU_PTRAUTH_GENERIC 6/* VCPU uses generic authentication */ +#define KVM_ARM_VCPU_REC 7/* VCPU REC state as part of Realm */ struct kvm_vcpu_init {__u32 target;@@ -401,6 +402,68 @@ enum {#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3#define KVM_DEV_ARM_ITS_CTRL_RESET 4Quoted 10 lines
arch/arm64/include/uapi/asm/kvm.h enum { · line 411
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h +7
-0+/* KVM_CAP_ARM_RME on VM fd */ +#define KVM_CAP_ARM_RME_CONFIG_REALM 0 +#define KVM_CAP_ARM_RME_CREATE_RD 1 +#define KVM_CAP_ARM_RME_INIT_IPA_REALM 2 +#define KVM_CAP_ARM_RME_POPULATE_REALM 3 +#define KVM_CAP_ARM_RME_ACTIVATE_REALM 4 +It is a little bit confusing here. These seems more like 'commands' not caps. Will leave more comments after reviewing the later patches.
Quoted 4 lines
Sorry for the slow response. Thank you for your review - I hope to post a new version of this series (rebased on 6.3-rc1) in the coming weeks with your comments addressed.
Hi:
No worries. I spent most of my time on closing the review of TDX/SNP series recently while stopped at patch 16 of this series. I will try to finish the rest of this series this week.
I am glad if my efforts help and more reviewers can smoothly jump in later.
Quoted 13 lines
They are indeed commands - and using caps is a bit of a hack. The benefit here is that all the Realm commands are behind the one
KVM_CAP_ARM_RME.The options I can see are:
a) What I've got here - (ab)using
KVM_ENABLE_CAPto perform commands.b) Add new ioctls for each of the above stages (so 5 new ioctls on top of the CAP for discovery). With any future extensions requiring new ioctls.
c) Add a single new multiplexing ioctl (along with the CAP for discovery).
Quoted 9 lines
I'm not massively keen on defining a new multiplexing scheme (c), but equally (b) seems like it's burning through ioctl numbers. Which led me to stick with (a) which at least keeps the rebasing simple (there's only the one CAP number which could conflict) and there's already a multiplexing scheme.
But I'm happy to change if there's consensus a different approach would be preferable.
Let's see if others have different opinions.
My coin goes to b as it is better to respect "what it is, make it explicit and clear" when coming to define the UABI. Ioctl number is for UABI. If it is going to burn out, IMHO, we need to find another way, perhaps another fd to group those ioctls, like KVM.
1. a) seems abusing the usage of the cap. for sure, the benefit is obvious.
2. c) seems hiding the details, which saves the ioctl numbers, but it didn't actually help a lot on the complexity and might end up with another bunch of "command code".
Quoted 77 lines
Thanks,
Steve
Quoted 72 lines
Quoted 71 lines
arch/arm64/include/uapi/asm/kvm.h +55
-0+#define KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 0 +#define KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 1 + +#define KVM_CAP_ARM_RME_RPV_SIZE 64 + +/* List of configuration items accepted for KVM_CAP_ARM_RME_CONFIG_REALM */ +#define KVM_CAP_ARM_RME_CFG_RPV 0 +#define KVM_CAP_ARM_RME_CFG_HASH_ALGO 1 +#define KVM_CAP_ARM_RME_CFG_SVE 2 +#define KVM_CAP_ARM_RME_CFG_DBG 3 +#define KVM_CAP_ARM_RME_CFG_PMU 4 + +struct kvm_cap_arm_rme_config_item { +__u32 cfg; +union { +/* cfg == KVM_CAP_ARM_RME_CFG_RPV */ +struct { +__u8 rpv[KVM_CAP_ARM_RME_RPV_SIZE ]; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_HASH_ALGO */ +struct { +__u32 hash_algo; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_SVE */ +struct { +__u32 sve_vq; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_DBG */ +struct { +__u32 num_brps; +__u32 num_wrps; + }; + +/* cfg == KVM_CAP_ARM_RME_CFG_PMU */ +struct { +__u32 num_pmu_cntrs; + }; +/* Fix the size of the union */ +__u8 reserved[256 ]; + }; +}; + +struct kvm_cap_arm_rme_populate_realm_args { +__u64 populate_ipa_base; +__u64 populate_ipa_size; +}; + +struct kvm_cap_arm_rme_init_ipa_args { +__u64 init_ipa_base; +__u64 init_ipa_size; +}; +/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0#define KVM_ARM_VCPU_PMU_V3_IRQ 0include/uapi/linux/kvm.h +2
-0@@ -1176,6 +1176,8 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224#define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225 +#define KVM_CAP_ARM_RME 300 // FIXME: Large number to prevent conflicts +#ifdef KVM_CAP_IRQ_ROUTING struct kvm_irq_routing_irqchip {
RFC PATCH 6/28 arm64: RME: ioctls to create and configure realms
In reply to Steven PriceAdd the KVM_CAP_ARM_RME_CREATE_FD 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.
--- arch/arm64/include/asm/kvm_rme.h | 14 ++ arch/arm64/kvm/arm.c | 19 ++ arch/arm64/kvm/mmu.c | 6 + arch/arm64/kvm/reset.c | 33 +++ arch/arm64/kvm/rme.c | 357 +++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+)
arch/arm64/include/asm/kvm_rme.h +14 -0
@@ -6,6 +6,8 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H +#include <uapi/linux/kvm.h> +enum realm_state {REALM_STATE_NONE , REALM_STATE_NEW,
@@ -15,8 +17,20 @@ enum realm_state {struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };int 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
arch/arm64/kvm/arm.c +19 -0
@@ -103,6 +103,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r =0 ;set_bit (KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED , &kvm->arch.flags);break ; +case KVM_CAP_ARM_RME : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EINVAL ; +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default : r = -EINVAL ;break ;
@@ -172,6 +179,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)*/ kvm->arch.dfr0_pmuver.imp =kvm_arm_pmu_get_pmuver_limit (); +/* 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:
@@ -204,6 +218,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_destroy_vcpus (kvm);kvm_unshare_hyp (kvm, kvm +1 ); + +kvm_destroy_realm (kvm); }int kvm_vm_ioctl_check_extension (struct kvm *kvm,long ext)
@@ -300,6 +316,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ; }
arch/arm64/kvm/mmu.c +6 -0
@@ -840,6 +840,12 @@ 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_DYING ) { +/* TODO: teardown rtts */ +write_unlock (&kvm->mmu_lock); +return ; + } pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;
arch/arm64/kvm/reset.c +33 -0
@@ -395,3 +395,36 @@ int kvm_set_ipa_limit(void)return 0 ; } + +int kvm_arm_setup_stage2 (struct kvm *kvm,unsigned long type) +{ +u64 mmfr0, mmfr1; +u32 phys_shift; +u32 ipa_limit = kvm_ipa_limit; + +if (kvm_is_realm (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 (phys_shift) { +if (phys_shift > ipa_limit || + phys_shift <ARM64_MIN_PARANGE_BITS ) +return -EINVAL ; + }else { + phys_shift =KVM_PHYS_SHIFT ; +if (phys_shift > ipa_limit) { +pr_warn_once ("%s using unsupported default IPA limit, upgrade your VMM\n" , + current->comm); +return -EINVAL ; + } + } + + mmfr0 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR0_EL1 ); + mmfr1 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR1_EL1 ); + kvm->arch.vtcr =kvm_get_vtcr (mmfr0, mmfr1, phys_shift); + +return 0 ; +}
arch/arm64/kvm/rme.c +357 -0
380 lines not on this page · raw message
Signature · 1 line
2.34.1
kernel test robot
build botRe: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in…
Hi Steven,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on arm64/for-next/core]
[cannot apply to kvmarm/next kvm/queue kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Steven-Price/arm64-RME-Add-SMC-definitions-for-calling-the-RMM/20230128-180035 base: https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core patch link: https://lore.kernel.org/r/20230127112932.38045-7-steven.price%40arm.com patch subject: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20230129/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/e7bd258153be4522bd16992b408adc812a1d587b
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Steven-Price/arm64-RME-Add-SMC-definitions-for-calling-the-RMM/20230128-180035 git checkout e7bd258153be4522bd16992b408adc812a1d587b
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/kvm/
If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <[email protected]>
All warnings (new ones prefixed by >>):
>> arch/arm64/kvm/reset.c:399:5: warning: no previous prototype for 'kvm_arm_setup_stage2' [-Wmissing-prototypes]
399 | int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)
| ^~~~~~~~~~~~~~~~~~~~
vim +/kvm_arm_setup_stage2 +399 arch/arm64/kvm/reset.c
398
> 399 int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
In reply to Steven Pricearch/arm64/kvm/rme.c static int rmi_check_version(void) · line 285
Quoted 8 lines · Steven Price
arch/arm64/kvm/rme.c +8
-0+static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ;
This should also check kvm_is_realm() (otherwise we dereference a NULL realm).
I was wondering about fuzzing the API to find more of this kind of issue, but don't know anything about it. Is there a recommended way to fuzz KVM?
Thanks,
Jean
Suzuki K Poulose
reviewerRe: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
Quoted 12 lines · Jean-Philippe Brucker
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 285
Quoted 8 lines · Steven Price
arch/arm64/kvm/rme.c +8
-0+static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ;This should also check
kvm_is_realm()(otherwise we dereference a NULL realm).
Correct, I think this should be done way up in the stack at :
kvm_vm_ioctl_enable_cap() for KVM_CAP_ARM_RME.
Quoted 3 lines
I was wondering about fuzzing the API to find more of this kind of issue, but don't know anything about it. Is there a recommended way to fuzz KVM?
Not sure either. kselftests is one possible way to drive these test at least for unit-testing the new ABIs. This is something we plan to add.
Thanks for catching this.
Suzuki
Quoted 3 lines
Thanks,
Jean
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
In reply to Steven PriceQuoted 34 lines · On Fri, 27 Jan 2023 11:29:10 +0000 Steven Price <[email protected]> wrote:
Add the
KVM_CAP_ARM_RME_CREATE_FDioctl 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.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 14 ++ arch/arm64/kvm/arm.c | 19 ++ arch/arm64/kvm/mmu.c | 6 + arch/arm64/kvm/reset.c | 33 +++ arch/arm64/kvm/rme.c | 357 +++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+)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> +enum realm_state {REALM_STATE_NONE , REALM_STATE_NEW,@@ -15,8 +17,20 @@ enum realm_state {struct realm {
arch/arm64/include/asm/kvm_rme.h enum realm_state { · line 28
Quoted 10 lines
arch/arm64/include/asm/kvm_rme.h +7
-0enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };
Maybe more comments for this structure?
Quoted 261 lines
arch/arm64/include/asm/kvm_rme.h +5
-0int 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 arch/arm64/kvm/arm.c +19
-0@@ -103,6 +103,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r =0 ;set_bit (KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED , &kvm->arch.flags);break ; +case KVM_CAP_ARM_RME : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EINVAL ; +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default : r = -EINVAL ;break ;@@ -172,6 +179,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)*/ kvm->arch.dfr0_pmuver.imp =kvm_arm_pmu_get_pmuver_limit (); +/* 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:@@ -204,6 +218,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_destroy_vcpus (kvm);kvm_unshare_hyp (kvm, kvm +1 ); + +kvm_destroy_realm (kvm); }int kvm_vm_ioctl_check_extension (struct kvm *kvm,long ext)@@ -300,6 +316,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ; }arch/arm64/kvm/mmu.c +6
-0@@ -840,6 +840,12 @@ 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_DYING ) { +/* TODO: teardown rtts */ +write_unlock (&kvm->mmu_lock); +return ; + } pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;arch/arm64/kvm/reset.c +33
-0@@ -395,3 +395,36 @@ int kvm_set_ipa_limit(void)return 0 ; } + +int kvm_arm_setup_stage2 (struct kvm *kvm,unsigned long type) +{ +u64 mmfr0, mmfr1; +u32 phys_shift; +u32 ipa_limit = kvm_ipa_limit; + +if (kvm_is_realm (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 (phys_shift) { +if (phys_shift > ipa_limit || + phys_shift <ARM64_MIN_PARANGE_BITS ) +return -EINVAL ; + }else { + phys_shift =KVM_PHYS_SHIFT ; +if (phys_shift > ipa_limit) { +pr_warn_once ("%s using unsupported default IPA limit, upgrade your VMM\n" , + current->comm); +return -EINVAL ; + } + } + + mmfr0 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR0_EL1 ); + mmfr1 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR1_EL1 ); + kvm->arch.vtcr =kvm_get_vtcr (mmfr0, mmfr1, phys_shift); + +return 0 ; +}arch/arm64/kvm/rme.c +126
-0@@ -5,9 +5,49 @@#include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> +/************ FIXME: Copied from kvm/hyp/pgtable.c **********/ +#include <asm/kvm_pgtable.h> + +struct kvm_pgtable_walk_data { +struct kvm_pgtable *pgt; +struct kvm_pgtable_walker *walker; + +u64 addr; +u64 end; +}; + +static u32 __kvm_pgd_page_idx (struct kvm_pgtable *pgt,u64 addr) +{ +u64 shift =kvm_granule_shift (pgt->start_level -1 );/* May underflow */ +u64 mask =BIT (pgt->ia_bits) -1 ; + +return (addr & mask) >> shift; +} + +static u32 kvm_pgd_pages (u32 ia_bits,u32 start_level) +{ +struct kvm_pgtable pgt = { + .ia_bits = ia_bits, + .start_level = start_level, + }; + +return __kvm_pgd_page_idx (&pgt,-1ULL ) +1 ; +} + +/******************/ + +static unsigned long rmm_feat_reg0; + +static bool rme_supports (unsigned long feature) +{ +return !!u64_get_bits (rmm_feat_reg0, feature); +} +static int rmi_check_version (void ) {struct arm_smccc_res res;@@ -33,8 +73,319 @@ static int rmi_check_version(void)return 0 ; } +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) +{ +unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); +u64 feat_reg0 =0 ; + +int num_bps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_BPS ); +int num_wps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_WPS ); + + feat_reg0 |=u64_encode_bits (ia_bits,RMI_FEATURE_REGISTER_0_S2SZ ); + feat_reg0 |=u64_encode_bits (num_bps,RMI_FEATURE_REGISTER_0_NUM_BPS ); + feat_reg0 |=u64_encode_bits (num_wps,RMI_FEATURE_REGISTER_0_NUM_WPS ); + +return feat_reg0; +} + +u32 kvm_realm_ipa_limit (void ) +{ +return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_S2SZ ); +} + +static u32 get_start_level (struct kvm *kvm) +{ +long sl0 =FIELD_GET (VTCR_EL2_SL0_MASK , kvm->arch.vtcr); + +return VTCR_EL2_TGRAN_SL0_BASE - sl0; +} + +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; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i, r; + +if (WARN_ON (realm->rd) ||WARN_ON (!realm->params)) +return -EEXIST ; + + 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 out; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (rmi_granule_delegate (pgd_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + } + + params->rtt_level_start =get_start_level (kvm); + params->rtt_num_start = pgd_sz; + 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; + } + + realm->rd = rd; + realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); + +if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) { +WARN_ON (rmi_realm_destroy (rd_phys)); + goto out_undelegate_tables; + } + +return 0 ; + +out_undelegate_tables: +while (--i >=0 ) {
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 171
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +WARN_ON (rmi_granule_undelegate (pgd_phys)); + } +WARN_ON (rmi_granule_undelegate (rd_phys)); +out: +free_page ((unsigned long )rd); +return r; +} +
Just curious. Wouldn't it be better to use IDR as this is ID allocation? There were some efforts to change the use of bitmap allocation to IDR before.
Quoted 233 lines
arch/arm64/kvm/rme.c +221
-0+/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{ +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ; + } + +return 0 ; +} + +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (!kvm_is_realm (kvm) ||kvm_realm_state (kvm) !=REALM_STATE_NONE ) +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 kvm_cap_arm_rme_config_item *cfg) +{ +switch (cfg->hash_algo) { +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ; +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + realm->params->measurement_algo = cfg->hash_algo; +return 0 ; +} + +static int config_realm_sve (struct realm *realm, +struct kvm_cap_arm_rme_config_item *cfg) +{ +u64 features_0 = realm->params->features_0; +int max_sve_vq =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_SVE_VL ); + +if (!rme_supports (RMI_FEATURE_REGISTER_0_SVE_EN )) +return -EINVAL ; + +if (cfg->sve_vq > max_sve_vq) +return -EINVAL ; + + features_0 &= ~(RMI_FEATURE_REGISTER_0_SVE_EN | +RMI_FEATURE_REGISTER_0_SVE_VL ); + features_0 |=u64_encode_bits (1 ,RMI_FEATURE_REGISTER_0_SVE_EN ); + features_0 |=u64_encode_bits (cfg->sve_vq, +RMI_FEATURE_REGISTER_0_SVE_VL ); + + realm->params->features_0 = features_0; +return 0 ; +} + +static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ; + +if (copy_from_user (&cfg, (void __user *)cap->args[1 ],sizeof (cfg))) +return -EFAULT ; + +switch (cfg.cfg) { +case KVM_CAP_ARM_RME_CFG_RPV : +memcpy (&realm->params->rpv, &cfg.rpv,sizeof (cfg.rpv)); +break ; +case KVM_CAP_ARM_RME_CFG_HASH_ALGO : + r =config_realm_hash_algo (realm, &cfg); +break ; +case KVM_CAP_ARM_RME_CFG_SVE : + r =config_realm_sve (realm, &cfg); +break ; +default : + r = -EINVAL ; + } + +return r; +} + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +int r =0 ; + +switch (cap->args[0 ]) { +case KVM_CAP_ARM_RME_CONFIG_REALM : + r =kvm_rme_config_realm (kvm, cap); +break ; +case KVM_CAP_ARM_RME_CREATE_RD : +if (kvm->created_vcpus) { + r = -EBUSY ; +break ; + } + + r =kvm_create_realm (kvm); +break ; +default : + r = -EINVAL ; +break ; + } + +return r; +} + +void kvm_destroy_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i; + +if (realm->params) { +free_page ((unsigned long )realm->params); + realm->params =NULL ; + } + +if (kvm_realm_state (kvm) ==REALM_STATE_NONE ) +return ; + +WRITE_ONCE (realm->state,REALM_STATE_DYING ); + +rme_vmid_release (realm->vmid); + +if (realm->rd) { +phys_addr_t rd_phys =virt_to_phys (realm->rd); + +if (WARN_ON (rmi_realm_destroy (rd_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rd_phys))) +return ; +free_page ((unsigned long )realm->rd); + realm->rd =NULL ; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) +return ; + } + +kvm_free_stage2_pgd (&kvm->arch.mmu); +} + +int kvm_init_realm_vm (struct kvm *kvm) +{ +struct realm_params *params; + + params = (struct realm_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return -ENOMEM ; + + params->features_0 =create_realm_feat_reg0 (kvm); + kvm->arch.realm.params = params; +return 0 ; +} +int kvm_init_rme (void ) { +int ret; +if (PAGE_SIZE !=SZ_4K )/* Only 4k page size on the host is supported */ return 0 ;@@ -43,6 +394,12 @@ int kvm_init_rme(void)/* Continue without realm support */ return 0 ; + ret =rme_vmid_init (); +if (ret) +return ret; + +WARN_ON (rmi_features (0 , &rmm_feat_reg0)); +/* Future patch will enable static branch kvm_rme_is_available */ return 0 ;
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
Quoted 36 lines · Zhi Wang
Quoted 33 lines · On Fri, 27 Jan 2023 11:29:10 +0000 Steven Price <[email protected]> wrote:
Add the
KVM_CAP_ARM_RME_CREATE_FDioctl 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.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 14 ++ arch/arm64/kvm/arm.c | 19 ++ arch/arm64/kvm/mmu.c | 6 + arch/arm64/kvm/reset.c | 33 +++ arch/arm64/kvm/rme.c | 357 +++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+)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> +enum realm_state {REALM_STATE_NONE , REALM_STATE_NEW,@@ -15,8 +17,20 @@ enum realm_state {
Quoted 13 lines
arch/arm64/include/asm/kvm_rme.h enum realm_state { · line 28
Quoted 11 lines
arch/arm64/include/asm/kvm_rme.h +7
-0struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };Maybe more comments for this structure?
Agreed, this series is a bit light on comments. I'll try to improve for v2.
<snip>
Quoted 3 lines
Just curious. Wouldn't it be better to use IDR as this is ID allocation? There were some efforts to change the use of bitmap allocation to IDR before.
I'm not sure it makes much difference really. This matches KVM's vmid_map, but here things are much more simple as there's no support for the likes of VMID rollover (the number of Realm VMs is just capped at the number of VMIDs).
IDR provides a lot of functionality we don't need, but equally I don't think performance or memory usage are really a concern here.
Steve
Quoted 234 lines
Quoted 233 lines
arch/arm64/include/asm/kvm_rme.h +221
-0+/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{ +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ; + } + +return 0 ; +} + +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (!kvm_is_realm (kvm) ||kvm_realm_state (kvm) !=REALM_STATE_NONE ) +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 kvm_cap_arm_rme_config_item *cfg) +{ +switch (cfg->hash_algo) { +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ; +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + realm->params->measurement_algo = cfg->hash_algo; +return 0 ; +} + +static int config_realm_sve (struct realm *realm, +struct kvm_cap_arm_rme_config_item *cfg) +{ +u64 features_0 = realm->params->features_0; +int max_sve_vq =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_SVE_VL ); + +if (!rme_supports (RMI_FEATURE_REGISTER_0_SVE_EN )) +return -EINVAL ; + +if (cfg->sve_vq > max_sve_vq) +return -EINVAL ; + + features_0 &= ~(RMI_FEATURE_REGISTER_0_SVE_EN | +RMI_FEATURE_REGISTER_0_SVE_VL ); + features_0 |=u64_encode_bits (1 ,RMI_FEATURE_REGISTER_0_SVE_EN ); + features_0 |=u64_encode_bits (cfg->sve_vq, +RMI_FEATURE_REGISTER_0_SVE_VL ); + + realm->params->features_0 = features_0; +return 0 ; +} + +static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ; + +if (copy_from_user (&cfg, (void __user *)cap->args[1 ],sizeof (cfg))) +return -EFAULT ; + +switch (cfg.cfg) { +case KVM_CAP_ARM_RME_CFG_RPV : +memcpy (&realm->params->rpv, &cfg.rpv,sizeof (cfg.rpv)); +break ; +case KVM_CAP_ARM_RME_CFG_HASH_ALGO : + r =config_realm_hash_algo (realm, &cfg); +break ; +case KVM_CAP_ARM_RME_CFG_SVE : + r =config_realm_sve (realm, &cfg); +break ; +default : + r = -EINVAL ; + } + +return r; +} + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +int r =0 ; + +switch (cap->args[0 ]) { +case KVM_CAP_ARM_RME_CONFIG_REALM : + r =kvm_rme_config_realm (kvm, cap); +break ; +case KVM_CAP_ARM_RME_CREATE_RD : +if (kvm->created_vcpus) { + r = -EBUSY ; +break ; + } + + r =kvm_create_realm (kvm); +break ; +default : + r = -EINVAL ; +break ; + } + +return r; +} + +void kvm_destroy_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i; + +if (realm->params) { +free_page ((unsigned long )realm->params); + realm->params =NULL ; + } + +if (kvm_realm_state (kvm) ==REALM_STATE_NONE ) +return ; + +WRITE_ONCE (realm->state,REALM_STATE_DYING ); + +rme_vmid_release (realm->vmid); + +if (realm->rd) { +phys_addr_t rd_phys =virt_to_phys (realm->rd); + +if (WARN_ON (rmi_realm_destroy (rd_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rd_phys))) +return ; +free_page ((unsigned long )realm->rd); + realm->rd =NULL ; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) +return ; + } + +kvm_free_stage2_pgd (&kvm->arch.mmu); +} + +int kvm_init_realm_vm (struct kvm *kvm) +{ +struct realm_params *params; + + params = (struct realm_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return -ENOMEM ; + + params->features_0 =create_realm_feat_reg0 (kvm); + kvm->arch.realm.params = params; +return 0 ; +} +int kvm_init_rme (void ) { +int ret; +if (PAGE_SIZE !=SZ_4K )/* Only 4k page size on the host is supported */ return 0 ;@@ -43,6 +394,12 @@ int kvm_init_rme(void)/* Continue without realm support */ return 0 ; + ret =rme_vmid_init (); +if (ret) +return ret; + +WARN_ON (rmi_features (0 , &rmm_feat_reg0)); +/* Future patch will enable static branch kvm_rme_is_available */ return 0 ;
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
Quoted 59 lines · On Wed, 1 Mar 2023 11:55:17 +0000 Steven Price <[email protected]> wrote:
Quoted 36 lines · Zhi Wang
Quoted 33 lines · On Fri, 27 Jan 2023 11:29:10 +0000 Steven Price <[email protected]> wrote:
Add the
KVM_CAP_ARM_RME_CREATE_FDioctl 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.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 14 ++ arch/arm64/kvm/arm.c | 19 ++ arch/arm64/kvm/mmu.c | 6 + arch/arm64/kvm/reset.c | 33 +++ arch/arm64/kvm/rme.c | 357 +++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+)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> +enum realm_state {REALM_STATE_NONE , REALM_STATE_NEW,@@ -15,8 +17,20 @@ enum realm_state {Quoted 13 lines
arch/arm64/include/asm/kvm_rme.h enum realm_state { · line 28
Quoted 11 lines
arch/arm64/include/asm/kvm_rme.h +7
-0struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };Maybe more comments for this structure?
Agreed, this series is a bit light on comments. I'll try to improve for v2.
<snip>
Quoted 3 lines
Just curious. Wouldn't it be better to use IDR as this is ID allocation? There were some efforts to change the use of bitmap allocation to IDR before.
Quoted 7 lines
I'm not sure it makes much difference really. This matches KVM's
vmid_map, but here things are much more simple as there's no support for the likes of VMID rollover (the number of Realm VMs is just capped at the number of VMIDs).IDR provides a lot of functionality we don't need, but equally I don't think performance or memory usage are really a concern here.
Agree. I am not opposed to the current approach. I gave this comment because I vaguely remember there were some patch bundles to covert bitmap to IDR in the kernel before. So I think it would be better to raise it up and get a conclusion. It would save some efforts for the people who might jump in the review later.
Quoted 238 lines
Steve
Quoted 234 lines
Quoted 233 lines
arch/arm64/include/asm/kvm_rme.h +221
-0+/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{ +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ; + } + +return 0 ; +} + +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (!kvm_is_realm (kvm) ||kvm_realm_state (kvm) !=REALM_STATE_NONE ) +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 kvm_cap_arm_rme_config_item *cfg) +{ +switch (cfg->hash_algo) { +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ; +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + realm->params->measurement_algo = cfg->hash_algo; +return 0 ; +} + +static int config_realm_sve (struct realm *realm, +struct kvm_cap_arm_rme_config_item *cfg) +{ +u64 features_0 = realm->params->features_0; +int max_sve_vq =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_SVE_VL ); + +if (!rme_supports (RMI_FEATURE_REGISTER_0_SVE_EN )) +return -EINVAL ; + +if (cfg->sve_vq > max_sve_vq) +return -EINVAL ; + + features_0 &= ~(RMI_FEATURE_REGISTER_0_SVE_EN | +RMI_FEATURE_REGISTER_0_SVE_VL ); + features_0 |=u64_encode_bits (1 ,RMI_FEATURE_REGISTER_0_SVE_EN ); + features_0 |=u64_encode_bits (cfg->sve_vq, +RMI_FEATURE_REGISTER_0_SVE_VL ); + + realm->params->features_0 = features_0; +return 0 ; +} + +static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ; + +if (copy_from_user (&cfg, (void __user *)cap->args[1 ],sizeof (cfg))) +return -EFAULT ; + +switch (cfg.cfg) { +case KVM_CAP_ARM_RME_CFG_RPV : +memcpy (&realm->params->rpv, &cfg.rpv,sizeof (cfg.rpv)); +break ; +case KVM_CAP_ARM_RME_CFG_HASH_ALGO : + r =config_realm_hash_algo (realm, &cfg); +break ; +case KVM_CAP_ARM_RME_CFG_SVE : + r =config_realm_sve (realm, &cfg); +break ; +default : + r = -EINVAL ; + } + +return r; +} + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +int r =0 ; + +switch (cap->args[0 ]) { +case KVM_CAP_ARM_RME_CONFIG_REALM : + r =kvm_rme_config_realm (kvm, cap); +break ; +case KVM_CAP_ARM_RME_CREATE_RD : +if (kvm->created_vcpus) { + r = -EBUSY ; +break ; + } + + r =kvm_create_realm (kvm); +break ; +default : + r = -EINVAL ; +break ; + } + +return r; +} + +void kvm_destroy_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i; + +if (realm->params) { +free_page ((unsigned long )realm->params); + realm->params =NULL ; + } + +if (kvm_realm_state (kvm) ==REALM_STATE_NONE ) +return ; + +WRITE_ONCE (realm->state,REALM_STATE_DYING ); + +rme_vmid_release (realm->vmid); + +if (realm->rd) { +phys_addr_t rd_phys =virt_to_phys (realm->rd); + +if (WARN_ON (rmi_realm_destroy (rd_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rd_phys))) +return ; +free_page ((unsigned long )realm->rd); + realm->rd =NULL ; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) +return ; + } + +kvm_free_stage2_pgd (&kvm->arch.mmu); +} + +int kvm_init_realm_vm (struct kvm *kvm) +{ +struct realm_params *params; + + params = (struct realm_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return -ENOMEM ; + + params->features_0 =create_realm_feat_reg0 (kvm); + kvm->arch.realm.params = params; +return 0 ; +} +int kvm_init_rme (void ) { +int ret; +if (PAGE_SIZE !=SZ_4K )/* Only 4k page size on the host is supported */ return 0 ;@@ -43,6 +394,12 @@ int kvm_init_rme(void)/* Continue without realm support */ return 0 ; + ret =rme_vmid_init (); +if (ret) +return ret; + +WARN_ON (rmi_features (0 , &rmm_feat_reg0)); +/* Future patch will enable static branch kvm_rme_is_available */ return 0 ;
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
In reply to Steven PriceQuoted 125 lines · On Fri, 27 Jan 2023 11:29:10 +0000 Steven Price <[email protected]> wrote:
Add the
KVM_CAP_ARM_RME_CREATE_FDioctl 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.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 14 ++ arch/arm64/kvm/arm.c | 19 ++ arch/arm64/kvm/mmu.c | 6 + arch/arm64/kvm/reset.c | 33 +++ arch/arm64/kvm/rme.c | 357 +++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+)arch/arm64/include/asm/kvm_rme.h +14
-0@@ -6,6 +6,8 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H +#include <uapi/linux/kvm.h> +enum realm_state {REALM_STATE_NONE , REALM_STATE_NEW,@@ -15,8 +17,20 @@ enum realm_state {struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };int 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 arch/arm64/kvm/arm.c +19
-0@@ -103,6 +103,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r =0 ;set_bit (KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED , &kvm->arch.flags);break ; +case KVM_CAP_ARM_RME : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EINVAL ; +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default : r = -EINVAL ;break ;@@ -172,6 +179,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)*/ kvm->arch.dfr0_pmuver.imp =kvm_arm_pmu_get_pmuver_limit (); +/* 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:@@ -204,6 +218,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_destroy_vcpus (kvm);kvm_unshare_hyp (kvm, kvm +1 ); + +kvm_destroy_realm (kvm); }int kvm_vm_ioctl_check_extension (struct kvm *kvm,long ext)@@ -300,6 +316,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ; }arch/arm64/kvm/mmu.c +6
-0@@ -840,6 +840,12 @@ 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_DYING ) { +/* TODO: teardown rtts */ +write_unlock (&kvm->mmu_lock); +return ; + } pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;arch/arm64/kvm/reset.c +0
-0
arch/arm64/kvm/reset.c int kvm_set_ipa_limit(void) · line 398
Quoted 4 lines
arch/arm64/kvm/reset.c +1
-0@@ -395,3 +395,36 @@ int kvm_set_ipa_limit(void)return 0 ; } +
The below function doesn't have an user in this patch. Also, it looks like a part of copy from kvm_init_stage2_mmu() in arch/arm64/kvm/mmu.c.
Quoted 419 lines
arch/arm64/kvm/reset.c +32
-0+int kvm_arm_setup_stage2 (struct kvm *kvm,unsigned long type) +{ +u64 mmfr0, mmfr1; +u32 phys_shift; +u32 ipa_limit = kvm_ipa_limit; + +if (kvm_is_realm (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 (phys_shift) { +if (phys_shift > ipa_limit || + phys_shift <ARM64_MIN_PARANGE_BITS ) +return -EINVAL ; + }else { + phys_shift =KVM_PHYS_SHIFT ; +if (phys_shift > ipa_limit) { +pr_warn_once ("%s using unsupported default IPA limit, upgrade your VMM\n" , + current->comm); +return -EINVAL ; + } + } + + mmfr0 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR0_EL1 ); + mmfr1 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR1_EL1 ); + kvm->arch.vtcr =kvm_get_vtcr (mmfr0, mmfr1, phys_shift); + +return 0 ; +}arch/arm64/kvm/rme.c +357
-0@@ -5,9 +5,49 @@#include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> +/************ FIXME: Copied from kvm/hyp/pgtable.c **********/ +#include <asm/kvm_pgtable.h> + +struct kvm_pgtable_walk_data { +struct kvm_pgtable *pgt; +struct kvm_pgtable_walker *walker; + +u64 addr; +u64 end; +}; + +static u32 __kvm_pgd_page_idx (struct kvm_pgtable *pgt,u64 addr) +{ +u64 shift =kvm_granule_shift (pgt->start_level -1 );/* May underflow */ +u64 mask =BIT (pgt->ia_bits) -1 ; + +return (addr & mask) >> shift; +} + +static u32 kvm_pgd_pages (u32 ia_bits,u32 start_level) +{ +struct kvm_pgtable pgt = { + .ia_bits = ia_bits, + .start_level = start_level, + }; + +return __kvm_pgd_page_idx (&pgt,-1ULL ) +1 ; +} + +/******************/ + +static unsigned long rmm_feat_reg0; + +static bool rme_supports (unsigned long feature) +{ +return !!u64_get_bits (rmm_feat_reg0, feature); +} +static int rmi_check_version (void ) {struct arm_smccc_res res;@@ -33,8 +73,319 @@ static int rmi_check_version(void)return 0 ; } +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) +{ +unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); +u64 feat_reg0 =0 ; + +int num_bps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_BPS ); +int num_wps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_WPS ); + + feat_reg0 |=u64_encode_bits (ia_bits,RMI_FEATURE_REGISTER_0_S2SZ ); + feat_reg0 |=u64_encode_bits (num_bps,RMI_FEATURE_REGISTER_0_NUM_BPS ); + feat_reg0 |=u64_encode_bits (num_wps,RMI_FEATURE_REGISTER_0_NUM_WPS ); + +return feat_reg0; +} + +u32 kvm_realm_ipa_limit (void ) +{ +return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_S2SZ ); +} + +static u32 get_start_level (struct kvm *kvm) +{ +long sl0 =FIELD_GET (VTCR_EL2_SL0_MASK , kvm->arch.vtcr); + +return VTCR_EL2_TGRAN_SL0_BASE - sl0; +} + +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; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i, r; + +if (WARN_ON (realm->rd) ||WARN_ON (!realm->params)) +return -EEXIST ; + + 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 out; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (rmi_granule_delegate (pgd_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + } + + params->rtt_level_start =get_start_level (kvm); + params->rtt_num_start = pgd_sz; + 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; + } + + realm->rd = rd; + realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); + +if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) { +WARN_ON (rmi_realm_destroy (rd_phys)); + goto out_undelegate_tables; + } + +return 0 ; + +out_undelegate_tables: +while (--i >=0 ) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +WARN_ON (rmi_granule_undelegate (pgd_phys)); + } +WARN_ON (rmi_granule_undelegate (rd_phys)); +out: +free_page ((unsigned long )rd); +return r; +} + +/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{ +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ; + } + +return 0 ; +} + +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (!kvm_is_realm (kvm) ||kvm_realm_state (kvm) !=REALM_STATE_NONE ) +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 kvm_cap_arm_rme_config_item *cfg) +{ +switch (cfg->hash_algo) { +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ; +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + realm->params->measurement_algo = cfg->hash_algo; +return 0 ; +} + +static int config_realm_sve (struct realm *realm, +struct kvm_cap_arm_rme_config_item *cfg) +{ +u64 features_0 = realm->params->features_0; +int max_sve_vq =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_SVE_VL ); + +if (!rme_supports (RMI_FEATURE_REGISTER_0_SVE_EN )) +return -EINVAL ; + +if (cfg->sve_vq > max_sve_vq) +return -EINVAL ; + + features_0 &= ~(RMI_FEATURE_REGISTER_0_SVE_EN | +RMI_FEATURE_REGISTER_0_SVE_VL ); + features_0 |=u64_encode_bits (1 ,RMI_FEATURE_REGISTER_0_SVE_EN ); + features_0 |=u64_encode_bits (cfg->sve_vq, +RMI_FEATURE_REGISTER_0_SVE_VL ); + + realm->params->features_0 = features_0; +return 0 ; +} + +static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ; + +if (copy_from_user (&cfg, (void __user *)cap->args[1 ],sizeof (cfg))) +return -EFAULT ; + +switch (cfg.cfg) { +case KVM_CAP_ARM_RME_CFG_RPV : +memcpy (&realm->params->rpv, &cfg.rpv,sizeof (cfg.rpv)); +break ; +case KVM_CAP_ARM_RME_CFG_HASH_ALGO : + r =config_realm_hash_algo (realm, &cfg); +break ; +case KVM_CAP_ARM_RME_CFG_SVE : + r =config_realm_sve (realm, &cfg); +break ; +default : + r = -EINVAL ; + } + +return r; +} + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +int r =0 ; + +switch (cap->args[0 ]) { +case KVM_CAP_ARM_RME_CONFIG_REALM : + r =kvm_rme_config_realm (kvm, cap); +break ; +case KVM_CAP_ARM_RME_CREATE_RD : +if (kvm->created_vcpus) { + r = -EBUSY ; +break ; + } + + r =kvm_create_realm (kvm); +break ; +default : + r = -EINVAL ; +break ; + } + +return r; +} + +void kvm_destroy_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i; + +if (realm->params) { +free_page ((unsigned long )realm->params); + realm->params =NULL ; + } + +if (kvm_realm_state (kvm) ==REALM_STATE_NONE ) +return ; + +WRITE_ONCE (realm->state,REALM_STATE_DYING ); + +rme_vmid_release (realm->vmid); + +if (realm->rd) { +phys_addr_t rd_phys =virt_to_phys (realm->rd); + +if (WARN_ON (rmi_realm_destroy (rd_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rd_phys))) +return ; +free_page ((unsigned long )realm->rd); + realm->rd =NULL ; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) +return ; + } + +kvm_free_stage2_pgd (&kvm->arch.mmu); +} + +int kvm_init_realm_vm (struct kvm *kvm) +{ +struct realm_params *params; + + params = (struct realm_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return -ENOMEM ; + + params->features_0 =create_realm_feat_reg0 (kvm); + kvm->arch.realm.params = params; +return 0 ; +} +int kvm_init_rme (void ) { +int ret; +if (PAGE_SIZE !=SZ_4K )/* Only 4k page size on the host is supported */ return 0 ;@@ -43,6 +394,12 @@ int kvm_init_rme(void)/* Continue without realm support */ return 0 ; + ret =rme_vmid_init (); +if (ret) +return ret; + +WARN_ON (rmi_features (0 , &rmm_feat_reg0)); +/* Future patch will enable static branch kvm_rme_is_available */ return 0 ;
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
Quoted 129 lines · Zhi Wang
Quoted 126 lines · On Fri, 27 Jan 2023 11:29:10 +0000 Steven Price <[email protected]> wrote:
Add the
KVM_CAP_ARM_RME_CREATE_FDioctl 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.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 14 ++ arch/arm64/kvm/arm.c | 19 ++ arch/arm64/kvm/mmu.c | 6 + arch/arm64/kvm/reset.c | 33 +++ arch/arm64/kvm/rme.c | 357 +++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+)arch/arm64/include/asm/kvm_rme.h +14
-0@@ -6,6 +6,8 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H +#include <uapi/linux/kvm.h> +enum realm_state {REALM_STATE_NONE , REALM_STATE_NEW,@@ -15,8 +17,20 @@ enum realm_state {struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };int 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 arch/arm64/kvm/arm.c +19
-0@@ -103,6 +103,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r =0 ;set_bit (KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED , &kvm->arch.flags);break ; +case KVM_CAP_ARM_RME : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EINVAL ; +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default : r = -EINVAL ;break ;@@ -172,6 +179,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)*/ kvm->arch.dfr0_pmuver.imp =kvm_arm_pmu_get_pmuver_limit (); +/* 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:@@ -204,6 +218,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_destroy_vcpus (kvm);kvm_unshare_hyp (kvm, kvm +1 ); + +kvm_destroy_realm (kvm); }int kvm_vm_ioctl_check_extension (struct kvm *kvm,long ext)@@ -300,6 +316,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ; }arch/arm64/kvm/mmu.c +6
-0@@ -840,6 +840,12 @@ 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_DYING ) { +/* TODO: teardown rtts */ +write_unlock (&kvm->mmu_lock); +return ; + } pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;arch/arm64/kvm/reset.c +0
-0@@ -395,3 +395,36 @@ int kvm_set_ipa_limit(void)
Quoted 7 lines
arch/arm64/kvm/reset.c int kvm_set_ipa_limit(void) · line 398
Quoted 3 lines
arch/arm64/kvm/reset.c +1
-0return 0 ; } +The below function doesn't have an user in this patch. Also, it looks like a part of copy from
kvm_init_stage2_mmu()inarch/arm64/kvm/mmu.c.
Good spot ;) Yes I discovered this, it should have been removed - it's no longer used. I think this was an error when I was rebasing:
kvm_arm_setup-stage2() was removed in 315775ff7c6d ("KVM: arm64:
Consolidate stage-2 initialisation into a single function").
Steve
Quoted 420 lines
Quoted 419 lines
arch/arm64/kvm/reset.c +32
-0+int kvm_arm_setup_stage2 (struct kvm *kvm,unsigned long type) +{ +u64 mmfr0, mmfr1; +u32 phys_shift; +u32 ipa_limit = kvm_ipa_limit; + +if (kvm_is_realm (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 (phys_shift) { +if (phys_shift > ipa_limit || + phys_shift <ARM64_MIN_PARANGE_BITS ) +return -EINVAL ; + }else { + phys_shift =KVM_PHYS_SHIFT ; +if (phys_shift > ipa_limit) { +pr_warn_once ("%s using unsupported default IPA limit, upgrade your VMM\n" , + current->comm); +return -EINVAL ; + } + } + + mmfr0 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR0_EL1 ); + mmfr1 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR1_EL1 ); + kvm->arch.vtcr =kvm_get_vtcr (mmfr0, mmfr1, phys_shift); + +return 0 ; +}arch/arm64/kvm/rme.c +357
-0@@ -5,9 +5,49 @@#include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> +/************ FIXME: Copied from kvm/hyp/pgtable.c **********/ +#include <asm/kvm_pgtable.h> + +struct kvm_pgtable_walk_data { +struct kvm_pgtable *pgt; +struct kvm_pgtable_walker *walker; + +u64 addr; +u64 end; +}; + +static u32 __kvm_pgd_page_idx (struct kvm_pgtable *pgt,u64 addr) +{ +u64 shift =kvm_granule_shift (pgt->start_level -1 );/* May underflow */ +u64 mask =BIT (pgt->ia_bits) -1 ; + +return (addr & mask) >> shift; +} + +static u32 kvm_pgd_pages (u32 ia_bits,u32 start_level) +{ +struct kvm_pgtable pgt = { + .ia_bits = ia_bits, + .start_level = start_level, + }; + +return __kvm_pgd_page_idx (&pgt,-1ULL ) +1 ; +} + +/******************/ + +static unsigned long rmm_feat_reg0; + +static bool rme_supports (unsigned long feature) +{ +return !!u64_get_bits (rmm_feat_reg0, feature); +} +static int rmi_check_version (void ) {struct arm_smccc_res res;@@ -33,8 +73,319 @@ static int rmi_check_version(void)return 0 ; } +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) +{ +unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); +u64 feat_reg0 =0 ; + +int num_bps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_BPS ); +int num_wps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_WPS ); + + feat_reg0 |=u64_encode_bits (ia_bits,RMI_FEATURE_REGISTER_0_S2SZ ); + feat_reg0 |=u64_encode_bits (num_bps,RMI_FEATURE_REGISTER_0_NUM_BPS ); + feat_reg0 |=u64_encode_bits (num_wps,RMI_FEATURE_REGISTER_0_NUM_WPS ); + +return feat_reg0; +} + +u32 kvm_realm_ipa_limit (void ) +{ +return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_S2SZ ); +} + +static u32 get_start_level (struct kvm *kvm) +{ +long sl0 =FIELD_GET (VTCR_EL2_SL0_MASK , kvm->arch.vtcr); + +return VTCR_EL2_TGRAN_SL0_BASE - sl0; +} + +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; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i, r; + +if (WARN_ON (realm->rd) ||WARN_ON (!realm->params)) +return -EEXIST ; + + 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 out; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (rmi_granule_delegate (pgd_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + } + + params->rtt_level_start =get_start_level (kvm); + params->rtt_num_start = pgd_sz; + 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; + } + + realm->rd = rd; + realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); + +if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) { +WARN_ON (rmi_realm_destroy (rd_phys)); + goto out_undelegate_tables; + } + +return 0 ; + +out_undelegate_tables: +while (--i >=0 ) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +WARN_ON (rmi_granule_undelegate (pgd_phys)); + } +WARN_ON (rmi_granule_undelegate (rd_phys)); +out: +free_page ((unsigned long )rd); +return r; +} + +/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{ +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ; + } + +return 0 ; +} + +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (!kvm_is_realm (kvm) ||kvm_realm_state (kvm) !=REALM_STATE_NONE ) +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 kvm_cap_arm_rme_config_item *cfg) +{ +switch (cfg->hash_algo) { +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ; +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + realm->params->measurement_algo = cfg->hash_algo; +return 0 ; +} + +static int config_realm_sve (struct realm *realm, +struct kvm_cap_arm_rme_config_item *cfg) +{ +u64 features_0 = realm->params->features_0; +int max_sve_vq =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_SVE_VL ); + +if (!rme_supports (RMI_FEATURE_REGISTER_0_SVE_EN )) +return -EINVAL ; + +if (cfg->sve_vq > max_sve_vq) +return -EINVAL ; + + features_0 &= ~(RMI_FEATURE_REGISTER_0_SVE_EN | +RMI_FEATURE_REGISTER_0_SVE_VL ); + features_0 |=u64_encode_bits (1 ,RMI_FEATURE_REGISTER_0_SVE_EN ); + features_0 |=u64_encode_bits (cfg->sve_vq, +RMI_FEATURE_REGISTER_0_SVE_VL ); + + realm->params->features_0 = features_0; +return 0 ; +} + +static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ; + +if (copy_from_user (&cfg, (void __user *)cap->args[1 ],sizeof (cfg))) +return -EFAULT ; + +switch (cfg.cfg) { +case KVM_CAP_ARM_RME_CFG_RPV : +memcpy (&realm->params->rpv, &cfg.rpv,sizeof (cfg.rpv)); +break ; +case KVM_CAP_ARM_RME_CFG_HASH_ALGO : + r =config_realm_hash_algo (realm, &cfg); +break ; +case KVM_CAP_ARM_RME_CFG_SVE : + r =config_realm_sve (realm, &cfg); +break ; +default : + r = -EINVAL ; + } + +return r; +} + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +int r =0 ; + +switch (cap->args[0 ]) { +case KVM_CAP_ARM_RME_CONFIG_REALM : + r =kvm_rme_config_realm (kvm, cap); +break ; +case KVM_CAP_ARM_RME_CREATE_RD : +if (kvm->created_vcpus) { + r = -EBUSY ; +break ; + } + + r =kvm_create_realm (kvm); +break ; +default : + r = -EINVAL ; +break ; + } + +return r; +} + +void kvm_destroy_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i; + +if (realm->params) { +free_page ((unsigned long )realm->params); + realm->params =NULL ; + } + +if (kvm_realm_state (kvm) ==REALM_STATE_NONE ) +return ; + +WRITE_ONCE (realm->state,REALM_STATE_DYING ); + +rme_vmid_release (realm->vmid); + +if (realm->rd) { +phys_addr_t rd_phys =virt_to_phys (realm->rd); + +if (WARN_ON (rmi_realm_destroy (rd_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rd_phys))) +return ; +free_page ((unsigned long )realm->rd); + realm->rd =NULL ; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) +return ; + } + +kvm_free_stage2_pgd (&kvm->arch.mmu); +} + +int kvm_init_realm_vm (struct kvm *kvm) +{ +struct realm_params *params; + + params = (struct realm_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return -ENOMEM ; + + params->features_0 =create_realm_feat_reg0 (kvm); + kvm->arch.realm.params = params; +return 0 ; +} +int kvm_init_rme (void ) { +int ret; +if (PAGE_SIZE !=SZ_4K )/* Only 4k page size on the host is supported */ return 0 ;@@ -43,6 +394,12 @@ int kvm_init_rme(void)/* Continue without realm support */ return 0 ; + ret =rme_vmid_init (); +if (ret) +return ret; + +WARN_ON (rmi_features (0 , &rmm_feat_reg0)); +/* Future patch will enable static branch kvm_rme_is_available */ return 0 ;
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
In reply to Steven PriceQuoted 536 lines · Steven Price
Add the
KVM_CAP_ARM_RME_CREATE_FDioctl 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.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 14 ++ arch/arm64/kvm/arm.c | 19 ++ arch/arm64/kvm/mmu.c | 6 + arch/arm64/kvm/reset.c | 33 +++ arch/arm64/kvm/rme.c | 357 +++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+)arch/arm64/include/asm/kvm_rme.h +14
-0@@ -6,6 +6,8 @@#ifndef __ASM_KVM_RME_H#define __ASM_KVM_RME_H +#include <uapi/linux/kvm.h> +enum realm_state {REALM_STATE_NONE , REALM_STATE_NEW,@@ -15,8 +17,20 @@ enum realm_state {struct realm {enum realm_state state; + +void *rd; +struct realm_params *params; + +unsigned long num_aux; +unsigned int vmid; +unsigned int ia_bits; };int 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 arch/arm64/kvm/arm.c +19
-0@@ -103,6 +103,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r =0 ;set_bit (KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED , &kvm->arch.flags);break ; +case KVM_CAP_ARM_RME : +if (!static_branch_unlikely (&kvm_rme_is_available)) +return -EINVAL ; +mutex_lock (&kvm->lock); + r =kvm_realm_enable_cap (kvm, cap); +mutex_unlock (&kvm->lock); +break ;default : r = -EINVAL ;break ;@@ -172,6 +179,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)*/ kvm->arch.dfr0_pmuver.imp =kvm_arm_pmu_get_pmuver_limit (); +/* 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:@@ -204,6 +218,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_destroy_vcpus (kvm);kvm_unshare_hyp (kvm, kvm +1 ); + +kvm_destroy_realm (kvm); }int kvm_vm_ioctl_check_extension (struct kvm *kvm,long ext)@@ -300,6 +316,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_RME : + r =static_key_enabled (&kvm_rme_is_available); +break ;default : r =0 ; }arch/arm64/kvm/mmu.c +6
-0@@ -840,6 +840,12 @@ 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_DYING ) { +/* TODO: teardown rtts */ +write_unlock (&kvm->mmu_lock); +return ; + } pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ;arch/arm64/kvm/reset.c +33
-0@@ -395,3 +395,36 @@ int kvm_set_ipa_limit(void)return 0 ; } + +int kvm_arm_setup_stage2 (struct kvm *kvm,unsigned long type) +{ +u64 mmfr0, mmfr1; +u32 phys_shift; +u32 ipa_limit = kvm_ipa_limit; + +if (kvm_is_realm (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 (phys_shift) { +if (phys_shift > ipa_limit || + phys_shift <ARM64_MIN_PARANGE_BITS ) +return -EINVAL ; + }else { + phys_shift =KVM_PHYS_SHIFT ; +if (phys_shift > ipa_limit) { +pr_warn_once ("%s using unsupported default IPA limit, upgrade your VMM\n" , + current->comm); +return -EINVAL ; + } + } + + mmfr0 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR0_EL1 ); + mmfr1 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR1_EL1 ); + kvm->arch.vtcr =kvm_get_vtcr (mmfr0, mmfr1, phys_shift); + +return 0 ; +}arch/arm64/kvm/rme.c +351
-0@@ -5,9 +5,49 @@#include <linux/kvm_host.h> +#include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/rmi_cmds.h> #include <asm/virt.h> +/************ FIXME: Copied from kvm/hyp/pgtable.c **********/ +#include <asm/kvm_pgtable.h> + +struct kvm_pgtable_walk_data { +struct kvm_pgtable *pgt; +struct kvm_pgtable_walker *walker; + +u64 addr; +u64 end; +}; + +static u32 __kvm_pgd_page_idx (struct kvm_pgtable *pgt,u64 addr) +{ +u64 shift =kvm_granule_shift (pgt->start_level -1 );/* May underflow */ +u64 mask =BIT (pgt->ia_bits) -1 ; + +return (addr & mask) >> shift; +} + +static u32 kvm_pgd_pages (u32 ia_bits,u32 start_level) +{ +struct kvm_pgtable pgt = { + .ia_bits = ia_bits, + .start_level = start_level, + }; + +return __kvm_pgd_page_idx (&pgt,-1ULL ) +1 ; +} + +/******************/ + +static unsigned long rmm_feat_reg0; + +static bool rme_supports (unsigned long feature) +{ +return !!u64_get_bits (rmm_feat_reg0, feature); +} +static int rmi_check_version (void ) {struct arm_smccc_res res;@@ -33,8 +73,319 @@ static int rmi_check_version(void)return 0 ; } +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) +{ +unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); +u64 feat_reg0 =0 ; + +int num_bps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_BPS ); +int num_wps =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_NUM_WPS ); + + feat_reg0 |=u64_encode_bits (ia_bits,RMI_FEATURE_REGISTER_0_S2SZ ); + feat_reg0 |=u64_encode_bits (num_bps,RMI_FEATURE_REGISTER_0_NUM_BPS ); + feat_reg0 |=u64_encode_bits (num_wps,RMI_FEATURE_REGISTER_0_NUM_WPS ); + +return feat_reg0; +} + +u32 kvm_realm_ipa_limit (void ) +{ +return u64_get_bits (rmm_feat_reg0,RMI_FEATURE_REGISTER_0_S2SZ ); +} + +static u32 get_start_level (struct kvm *kvm) +{ +long sl0 =FIELD_GET (VTCR_EL2_SL0_MASK , kvm->arch.vtcr); + +return VTCR_EL2_TGRAN_SL0_BASE - sl0; +} + +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; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i, r; + +if (WARN_ON (realm->rd) ||WARN_ON (!realm->params)) +return -EEXIST ; + + 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 out; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (rmi_granule_delegate (pgd_phys)) { + r = -ENXIO ; + goto out_undelegate_tables; + } + } + + params->rtt_level_start =get_start_level (kvm); + params->rtt_num_start = pgd_sz; + 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; + } + + realm->rd = rd; + realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr); + +if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) { +WARN_ON (rmi_realm_destroy (rd_phys)); + goto out_undelegate_tables; + } + +return 0 ; + +out_undelegate_tables: +while (--i >=0 ) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +WARN_ON (rmi_granule_undelegate (pgd_phys)); + } +WARN_ON (rmi_granule_undelegate (rd_phys)); +out: +free_page ((unsigned long )rd); +return r; +} + +/* Protects access to rme_vmid_bitmap */ +static DEFINE_SPINLOCK (rme_vmid_lock); +static unsigned long *rme_vmid_bitmap; + +static int rme_vmid_init (void ) +{ +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + + rme_vmid_bitmap =bitmap_zalloc (vmid_count,GFP_KERNEL ); +if (!rme_vmid_bitmap) { +kvm_err ("%s: Couldn't allocate rme vmid bitmap\n" , __func__); +return -ENOMEM ; + } + +return 0 ; +} + +static int rme_vmid_reserve (void ) +{ +int ret; +unsigned int vmid_count =1 <<kvm_get_vmid_bits (); + +spin_lock (&rme_vmid_lock); + ret =bitmap_find_free_region (rme_vmid_bitmap, vmid_count,0 ); +spin_unlock (&rme_vmid_lock); + +return ret; +} + +static void rme_vmid_release (unsigned int vmid) +{ +spin_lock (&rme_vmid_lock); +bitmap_release_region (rme_vmid_bitmap, vmid,0 ); +spin_unlock (&rme_vmid_lock); +} + +static int kvm_create_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +int ret; + +if (!kvm_is_realm (kvm) ||kvm_realm_state (kvm) !=REALM_STATE_NONE ) +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 kvm_cap_arm_rme_config_item *cfg) +{ +switch (cfg->hash_algo) { +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA256 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_256 )) +return -EINVAL ; +break ; +case KVM_CAP_ARM_RME_MEASUREMENT_ALGO_SHA512 : +if (!rme_supports (RMI_FEATURE_REGISTER_0_HASH_SHA_512 )) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + realm->params->measurement_algo = cfg->hash_algo; +return 0 ; +} + +static int config_realm_sve (struct realm *realm, +struct kvm_cap_arm_rme_config_item *cfg) +{ +u64 features_0 = realm->params->features_0; +int max_sve_vq =u64_get_bits (rmm_feat_reg0, +RMI_FEATURE_REGISTER_0_SVE_VL ); + +if (!rme_supports (RMI_FEATURE_REGISTER_0_SVE_EN )) +return -EINVAL ; + +if (cfg->sve_vq > max_sve_vq) +return -EINVAL ; + + features_0 &= ~(RMI_FEATURE_REGISTER_0_SVE_EN | +RMI_FEATURE_REGISTER_0_SVE_VL ); + features_0 |=u64_encode_bits (1 ,RMI_FEATURE_REGISTER_0_SVE_EN ); + features_0 |=u64_encode_bits (cfg->sve_vq, +RMI_FEATURE_REGISTER_0_SVE_VL ); + + realm->params->features_0 = features_0; +return 0 ; +} + +static int kvm_rme_config_realm (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +struct kvm_cap_arm_rme_config_item cfg; +struct realm *realm = &kvm->arch.realm; +int r =0 ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NONE ) +return -EBUSY ; + +if (copy_from_user (&cfg, (void __user *)cap->args[1 ],sizeof (cfg))) +return -EFAULT ; + +switch (cfg.cfg) { +case KVM_CAP_ARM_RME_CFG_RPV : +memcpy (&realm->params->rpv, &cfg.rpv,sizeof (cfg.rpv)); +break ; +case KVM_CAP_ARM_RME_CFG_HASH_ALGO : + r =config_realm_hash_algo (realm, &cfg); +break ; +case KVM_CAP_ARM_RME_CFG_SVE : + r =config_realm_sve (realm, &cfg); +break ; +default : + r = -EINVAL ; + } + +return r; +} + +int kvm_realm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +int r =0 ; + +switch (cap->args[0 ]) { +case KVM_CAP_ARM_RME_CONFIG_REALM : + r =kvm_rme_config_realm (kvm, cap); +break ; +case KVM_CAP_ARM_RME_CREATE_RD : +if (kvm->created_vcpus) { + r = -EBUSY ; +break ; + } + + r =kvm_create_realm (kvm); +break ; +default : + r = -EINVAL ; +break ; + } + +return r; +} + +void kvm_destroy_realm (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; +unsigned int pgd_sz; +int i; + +if (realm->params) { +free_page ((unsigned long )realm->params); + realm->params =NULL ; + } + +if (kvm_realm_state (kvm) ==REALM_STATE_NONE ) +return ; + +WRITE_ONCE (realm->state,REALM_STATE_DYING ); + +rme_vmid_release (realm->vmid); + +if (realm->rd) { +phys_addr_t rd_phys =virt_to_phys (realm->rd); + +if (WARN_ON (rmi_realm_destroy (rd_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rd_phys))) +return ; +free_page ((unsigned long )realm->rd); + realm->rd =NULL ; + } + + pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level); +for (i =0 ; i < pgd_sz; i++) { +phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i *PAGE_SIZE ; + +if (WARN_ON (rmi_granule_undelegate (pgd_phys))) +return ; + } + +kvm_free_stage2_pgd (&kvm->arch.mmu); +} + +int kvm_init_realm_vm (struct kvm *kvm) +{ +struct realm_params *params; + + params = (struct realm_params *)get_zeroed_page (GFP_KERNEL ); +if (!params) +return -ENOMEM ; + + params->features_0 =create_realm_feat_reg0 (kvm); + kvm->arch.realm.params = params; +return 0 ; +} +int kvm_init_rme (void ) { +int ret; +if (PAGE_SIZE !=SZ_4K )/* Only 4k page size on the host is supported */ return 0 ;
arch/arm64/kvm/rme.c int kvm_init_rme(void) · line 401
Quoted 8 lines
arch/arm64/kvm/rme.c +5
-0@@ -43,6 +394,12 @@ int kvm_init_rme(void)/* Continue without realm support */ return 0 ; + ret =rme_vmid_init (); +if (ret) +return ret; + +WARN_ON (rmi_features (0 , &rmm_feat_reg0));
Why WARN_ON, Is that good enough to print err/info message and keep "kvm_rme_is_available" disabled?
IMO, we should print message when rme is enabled, otherwise it should be silent return.
arch/arm64/kvm/rme.c int kvm_init_rme(void) · line 405
Quoted 4 lines
arch/arm64/kvm/rme.c +1
-0+/* Future patch will enable static branch kvm_rme_is_available */ return 0 ;
Thanks,
Ganapat
Re: [RFC PATCH 06/28] arm64: RME: ioctls to create and configure realms
Thanks for taking a look at this.
Quoted 1 line · Ganapatrao Kulkarni
On 27-01-2023 04:59 pm, Steven Price wrote:
[...]
Quoted 10 lines
Quoted 10 lines
int
kvm_init_rme(void)
{
+ int ret;
+
if (PAGE_SIZE!=SZ_4K)
/* Only 4k page size on the host is supported */ return 0;(quoted diff) +0
-0@@ -43,6 +394,12 @@ int kvm_init_rme(void) /* Continue without realm support */ return 0;
Quoted 8 lines
arch/arm64/kvm/rme.c int kvm_init_rme(void) · line 401
Quoted 5 lines
arch/arm64/kvm/rme.c +4
-0+ ret = rme_vmid_init(); + if (ret) + return ret; + + WARN_ON(rmi_features(0, &rmm_feat_reg0));Why
WARN_ON, Is that good enough to print err/info message and keep "kvm_rme_is_available" disabled?
Good point. RMI_FEATURES "does not have any failure conditions" so this is very much a "should never happen" situation. Assuming the call gracefully fails then rmm_feat_reg0 would remain 0 which would in practise stop realms being created, but this is clearly non-ideal.
I'll fix this up in the next version to do the rmi_features() call before rme_vmid_init(), that way we can just return early without setting kvm_rme_is_available in this situation. I'll keep the WARN_ON because something has gone very wrong if this call fails.
Quoted 2 lines
IMO, we should print message when rme is enabled, otherwise it should be silent return.
The rmi_check_version() call already outputs a "RMI ABI version %d.%d" message - I don't want to be too noisy here. Other than the 'cannot happen' situations if you see the "RMI ABI" message then kvm_rme_is_available will be set. And those 'cannot happen' routes will print their own error message (and point to a seriously broken system).
And obviously in the case of SMC_RMI_VERSION not being supported then we silently return as this is taken to mean there isn't an RMM.
Thanks,
Steve
RFC PATCH 7/28 arm64: kvm: 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.
--- arch/arm64/kvm/arm.c | 13 +++++++++++++ arch/arm64/kvm/mmu.c | 3 --- arch/arm64/kvm/reset.c | 3 --- include/uapi/linux/kvm.h | 19 +++++++++++++++---- 4 files changed, 28 insertions(+), 10 deletions(-)
arch/arm64/kvm/arm.c +13 -0
@@ -147,6 +147,19 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {int ret; +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 : + kvm->arch.is_realm = true; +break ; +default : +return -EINVAL ; + } + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;
arch/arm64/kvm/mmu.c +0 -3
@@ -709,9 +709,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long tu64 mmfr0, mmfr1;u32 phys_shift; -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;
arch/arm64/kvm/reset.c +0 -3
@@ -405,9 +405,6 @@ int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)if (kvm_is_realm (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 (phys_shift) { if (phys_shift > ipa_limit ||
include/uapi/linux/kvm.h +15 -4
@@ -898,14 +898,25 @@ struct kvm_ppc_resize_hpt {#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.34.1
Re: [RFC PATCH 07/28] arm64: kvm: Allow passing machine type in KVM creation
Quoted 19 lines · On Fri, 27 Jan 2023 11:29:11 +0000 Steven Price <[email protected]> wrote:
Previously machine type was used purely for specifying the physical address size of the guest. Reserve the higher bits to specify an ARM specific machine type and declare a new type '
KVM_VM_TYPE_ARM_REALM' used to create a realm guest.Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/arm.c | 13 +++++++++++++ arch/arm64/kvm/mmu.c | 3 --- arch/arm64/kvm/reset.c | 3 --- include/uapi/linux/kvm.h | 19 +++++++++++++++---- 4 files changed, 28 insertions(+), 10 deletions(-)arch/arm64/kvm/arm.c +0
-0@@ -147,6 +147,19 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {
arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 157
Quoted 10 lines
arch/arm64/kvm/arm.c +8
-0int ret; +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 : + kvm->arch.is_realm = true;
It is better to let this call fail when !kvm_rme_is_available? It is strange to be able to create a VM with REALM type in a system doesn't support RME.
Quoted 70 lines
arch/arm64/kvm/arm.c +5
-0+break ; +default : +return -EINVAL ; + } + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;arch/arm64/kvm/mmu.c +0
-3@@ -709,9 +709,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long tu64 mmfr0, mmfr1;u32 phys_shift; -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;arch/arm64/kvm/reset.c +0
-3@@ -405,9 +405,6 @@ int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)if (kvm_is_realm (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 (phys_shift) { if (phys_shift > ipa_limit ||include/uapi/linux/kvm.h +15
-4@@ -898,14 +898,25 @@ struct kvm_ppc_resize_hpt {#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: */
Re: [RFC PATCH 07/28] arm64: kvm: Allow passing machine type in KVM creation
Quoted 24 lines · Zhi Wang
Quoted 21 lines · On Fri, 27 Jan 2023 11:29:11 +0000 Steven Price <[email protected]> wrote:
Previously machine type was used purely for specifying the physical address size of the guest. Reserve the higher bits to specify an ARM specific machine type and declare a new type '
KVM_VM_TYPE_ARM_REALM' used to create a realm guest.Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/arm.c | 13 +++++++++++++ arch/arm64/kvm/mmu.c | 3 --- arch/arm64/kvm/reset.c | 3 --- include/uapi/linux/kvm.h | 19 +++++++++++++++---- 4 files changed, 28 insertions(+), 10 deletions(-)arch/arm64/kvm/arm.c +0
-0@@ -147,6 +147,19 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {int ret;
Quoted 12 lines
arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 157
Quoted 8 lines
arch/arm64/kvm/arm.c +8
-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 : + kvm->arch.is_realm = true;It is better to let this call fail when !kvm_rme_is_available? It is strange to be able to create a VM with REALM type in a system doesn't support RME.
Good point - I'll add a check here.
Thanks,
Steve
Quoted 71 lines
Quoted 70 lines
arch/arm64/kvm/arm.c +5
-0+break ; +default : +return -EINVAL ; + } + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;arch/arm64/kvm/mmu.c +0
-3@@ -709,9 +709,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long tu64 mmfr0, mmfr1;u32 phys_shift; -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;arch/arm64/kvm/reset.c +0
-3@@ -405,9 +405,6 @@ int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)if (kvm_is_realm (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 (phys_shift) { if (phys_shift > ipa_limit ||include/uapi/linux/kvm.h +15
-4@@ -898,14 +898,25 @@ struct kvm_ppc_resize_hpt {#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: */
RFC PATCH 8/28 arm64: RME: Keep a spare page delegated to the RMM
In reply to Steven PricePages can only be populated/destroyed on the RMM at the 4KB granule, this requires creating the full depth of RTTs. However if the pages are going to be combined into a 4MB huge page the last RTT is only temporarily needed. Similarly when freeing memory the huge page must be temporarily split requiring temporary usage of the full depth oF RTTs.
To avoid needing to perform a temporary allocation and delegation of a page for this purpose we keep a spare delegated page around. In particular this avoids the need for memory allocation while destroying the realm guest.
--- arch/arm64/include/asm/kvm_rme.h | 3 +++ arch/arm64/kvm/rme.c | 6 ++++++ 2 files changed, 9 insertions(+)
arch/arm64/include/asm/kvm_rme.h +3 -0
@@ -21,6 +21,9 @@ struct realm {void *rd;struct realm_params *params; +/* A spare already delegated page */ +phys_addr_t spare_page; +unsigned long num_aux;unsigned int vmid;unsigned int ia_bits;
arch/arm64/kvm/rme.c +6 -0
@@ -148,6 +148,7 @@ static int realm_create_rd(struct kvm *kvm) } realm->rd = rd; + realm->spare_page =PHYS_ADDR_MAX ; realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) {
@@ -357,6 +358,11 @@ void kvm_destroy_realm(struct kvm *kvm)free_page ((unsigned long )realm->rd); realm->rd =NULL ; } +if (realm->spare_page !=PHYS_ADDR_MAX ) { +if (!WARN_ON (rmi_granule_undelegate (realm->spare_page))) +free_page ((unsigned long )phys_to_virt (realm->spare_page)); + realm->spare_page =PHYS_ADDR_MAX ; + } pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level);for (i =0 ; i < pgd_sz; i++) {
Signature · 1 line
2.34.1
Re: [RFC PATCH 08/28] arm64: RME: Keep a spare page delegated to the RMM
Quoted 44 lines · On Fri, 27 Jan 2023 11:29:12 +0000 Steven Price <[email protected]> wrote:
Pages can only be populated/destroyed on the RMM at the 4KB granule, this requires creating the full depth of RTTs. However if the pages are going to be combined into a 4MB huge page the last RTT is only temporarily needed. Similarly when freeing memory the huge page must be temporarily split requiring temporary usage of the full depth oF RTTs.
To avoid needing to perform a temporary allocation and delegation of a page for this purpose we keep a spare delegated page around. In particular this avoids the need for memory allocation while destroying the realm guest.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 3 +++ arch/arm64/kvm/rme.c | 6 ++++++ 2 files changed, 9 insertions(+)arch/arm64/include/asm/kvm_rme.h +3
-0@@ -21,6 +21,9 @@ struct realm {void *rd;struct realm_params *params; +/* A spare already delegated page */ +phys_addr_t spare_page; +unsigned long num_aux;unsigned int vmid;unsigned int ia_bits;arch/arm64/kvm/rme.c +1
-0@@ -148,6 +148,7 @@ static int realm_create_rd(struct kvm *kvm) } realm->rd = rd; + realm->spare_page =PHYS_ADDR_MAX ; realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) {
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 363
Quoted 6 lines
arch/arm64/kvm/rme.c +3
-0@@ -357,6 +358,11 @@ void kvm_destroy_realm(struct kvm *kvm)free_page ((unsigned long )realm->rd); realm->rd =NULL ; } +if (realm->spare_page !=PHYS_ADDR_MAX ) { +if (!WARN_ON (rmi_granule_undelegate (realm->spare_page))) +free_page ((unsigned long )phys_to_virt (realm->spare_page));
Will the page be leaked (not usable for host and realms) if the undelegate failed? If yes, better at least put a comment.
Quoted 5 lines
arch/arm64/kvm/rme.c +2
-0+ realm->spare_page =PHYS_ADDR_MAX ; + } pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level);for (i =0 ; i < pgd_sz; i++) {
Re: [RFC PATCH 08/28] arm64: RME: Keep a spare page delegated to the RMM
Quoted 45 lines · Zhi Wang
Quoted 42 lines · On Fri, 27 Jan 2023 11:29:12 +0000 Steven Price <[email protected]> wrote:
Pages can only be populated/destroyed on the RMM at the 4KB granule, this requires creating the full depth of RTTs. However if the pages are going to be combined into a 4MB huge page the last RTT is only temporarily needed. Similarly when freeing memory the huge page must be temporarily split requiring temporary usage of the full depth oF RTTs.
To avoid needing to perform a temporary allocation and delegation of a page for this purpose we keep a spare delegated page around. In particular this avoids the need for memory allocation while destroying the realm guest.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 3 +++ arch/arm64/kvm/rme.c | 6 ++++++ 2 files changed, 9 insertions(+)arch/arm64/include/asm/kvm_rme.h +3
-0@@ -21,6 +21,9 @@ struct realm {void *rd;struct realm_params *params; +/* A spare already delegated page */ +phys_addr_t spare_page; +unsigned long num_aux;unsigned int vmid;unsigned int ia_bits;arch/arm64/kvm/rme.c +1
-0@@ -148,6 +148,7 @@ static int realm_create_rd(struct kvm *kvm) } realm->rd = rd; + realm->spare_page =PHYS_ADDR_MAX ; realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);
Quoted 11 lines
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 363
Quoted 8 lines
arch/arm64/kvm/rme.c +3
-0if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) {@@ -357,6 +358,11 @@ void kvm_destroy_realm(struct kvm *kvm)free_page ((unsigned long )realm->rd); realm->rd =NULL ; } +if (realm->spare_page !=PHYS_ADDR_MAX ) { +if (!WARN_ON (rmi_granule_undelegate (realm->spare_page))) +free_page ((unsigned long )phys_to_virt (realm->spare_page));Will the page be leaked (not usable for host and realms) if the undelegate failed? If yes, better at least put a comment.
Yes - I'll add a comment.
In general being unable to undelegate a page points to a programming error in the host. The only reason the RMM should refuse the request is it the page is in use by a Realm which the host has configured. So the WARN() is correct (there's a kernel bug) and the only sensible course of action is to leak the page and limp on.
Thanks,
Steve
Quoted 6 lines
Quoted 5 lines
arch/arm64/kvm/rme.c +2
-0+ realm->spare_page =PHYS_ADDR_MAX ; + } pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level);for (i =0 ; i < pgd_sz; i++) {
Re: [RFC PATCH 08/28] arm64: RME: Keep a spare page delegated to the RMM
Quoted 58 lines · On Wed, 1 Mar 2023 11:55:37 +0000 Steven Price <[email protected]> wrote:
Quoted 45 lines · Zhi Wang
Quoted 42 lines · On Fri, 27 Jan 2023 11:29:12 +0000 Steven Price <[email protected]> wrote:
Pages can only be populated/destroyed on the RMM at the 4KB granule, this requires creating the full depth of RTTs. However if the pages are going to be combined into a 4MB huge page the last RTT is only temporarily needed. Similarly when freeing memory the huge page must be temporarily split requiring temporary usage of the full depth oF RTTs.
To avoid needing to perform a temporary allocation and delegation of a page for this purpose we keep a spare delegated page around. In particular this avoids the need for memory allocation while destroying the realm guest.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 3 +++ arch/arm64/kvm/rme.c | 6 ++++++ 2 files changed, 9 insertions(+)arch/arm64/include/asm/kvm_rme.h +3
-0@@ -21,6 +21,9 @@ struct realm {void *rd;struct realm_params *params; +/* A spare already delegated page */ +phys_addr_t spare_page; +unsigned long num_aux;unsigned int vmid;unsigned int ia_bits;arch/arm64/kvm/rme.c +1
-0@@ -148,6 +148,7 @@ static int realm_create_rd(struct kvm *kvm) } realm->rd = rd; + realm->spare_page =PHYS_ADDR_MAX ; realm->ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);Quoted 11 lines
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 363
Quoted 8 lines
arch/arm64/kvm/rme.c +3
-0if (WARN_ON (rmi_rec_aux_count (rd_phys, &realm->num_aux))) {@@ -357,6 +358,11 @@ void kvm_destroy_realm(struct kvm *kvm)free_page ((unsigned long )realm->rd); realm->rd =NULL ; } +if (realm->spare_page !=PHYS_ADDR_MAX ) { +if (!WARN_ON (rmi_granule_undelegate (realm->spare_page))) +free_page ((unsigned long )phys_to_virt (realm->spare_page));Will the page be leaked (not usable for host and realms) if the undelegate failed? If yes, better at least put a comment.
Quoted 8 lines
Yes - I'll add a comment.
In general being unable to undelegate a page points to a programming error in the host. The only reason the RMM should refuse the request is it the page is in use by a Realm which the host has configured. So the
WARN()is correct (there's a kernel bug) and the only sensible course of action is to leak the page and limp on.
It would be nice to add a summary of above into the patch comments.
Having a comment when leaking a page (which mostly means the page cannot be reclaimed by VMM and used on a REALM any more) is nice. TDX/SNP also have the problem of leaking pages due to mystic reasons.
Imagine the leaking can turn worse bit by bit in a long running server and KVM will definitely have a generic accounting interface for reporting the numbers to the userspace later. Having a explicit comment at this time really makes it easier later.
Quoted 11 lines
Thanks,
Steve
Quoted 6 lines
Quoted 5 lines
arch/arm64/kvm/rme.c +2
-0+ realm->spare_page =PHYS_ADDR_MAX ; + } pgd_sz =kvm_pgd_pages (pgt->ia_bits, pgt->start_level);for (i =0 ; i < pgd_sz; i++) {
RFC PATCH 9/28 arm64: RME: RTT handling
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.
--- arch/arm64/include/asm/kvm_rme.h | 19 +++++ arch/arm64/kvm/mmu.c | 7 +- arch/arm64/kvm/rme.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_rme.h +19 -0
@@ -35,5 +35,24 @@ 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 realm *realm,u32 ia_bits,u32 start_level); + +#define RME_RTT_BLOCK_LEVEL 2 +#define RME_RTT_MAX_LEVEL 3 + +#define RME_PAGE_SHIFT 12 +#define RME_PAGE_SIZE BIT(RME_PAGE_SHIFT) +/* See ARM64_HW_PGTABLE_LEVEL_SHIFT() */ +#define RME_RTT_LEVEL_SHIFT (l) \ + ((RME_PAGE_SHIFT - 3) * (4 - (l)) + 3) +#define RME_L2_BLOCK_SIZE BIT(RME_RTT_LEVEL_SHIFT(2)) + +static inline unsigned long rme_rtt_level_mapsize (int level) +{ +if (WARN_ON (level >RME_RTT_MAX_LEVEL )) +return RME_PAGE_SIZE ; + +return (1UL <<RME_RTT_LEVEL_SHIFT (level)); +}#endif
arch/arm64/kvm/mmu.c +4 -3
@@ -834,16 +834,17 @@ 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_DYING ) { -/* TODO: teardown rtts */ write_unlock (&kvm->mmu_lock); +kvm_realm_destroy_rtts (&kvm->arch.realm, pgt->ia_bits, + pgt->start_level);return ; } - pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ; mmu->pgt =NULL ;
arch/arm64/kvm/rme.c +139 -0
151 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH 09/28] arm64: RME: RTT handling
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:13 +0000 Steven Price <[email protected]> wrote:
The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
This is only an introduction to RTT handling. While this patch is mostly like RTT teardown, better add more introduction to this patch. Also maybe refine the tittle to reflect what this patch is actually doing.
Quoted 72 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 19 +++++ arch/arm64/kvm/mmu.c | 7 +- arch/arm64/kvm/rme.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_rme.h +19
-0@@ -35,5 +35,24 @@ 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 realm *realm,u32 ia_bits,u32 start_level); + +#define RME_RTT_BLOCK_LEVEL 2 +#define RME_RTT_MAX_LEVEL 3 + +#define RME_PAGE_SHIFT 12 +#define RME_PAGE_SIZE BIT(RME_PAGE_SHIFT) +/* See ARM64_HW_PGTABLE_LEVEL_SHIFT() */ +#define RME_RTT_LEVEL_SHIFT (l) \ + ((RME_PAGE_SHIFT - 3) * (4 - (l)) + 3) +#define RME_L2_BLOCK_SIZE BIT(RME_RTT_LEVEL_SHIFT(2)) + +static inline unsigned long rme_rtt_level_mapsize (int level) +{ +if (WARN_ON (level >RME_RTT_MAX_LEVEL )) +return RME_PAGE_SIZE ; + +return (1UL <<RME_RTT_LEVEL_SHIFT (level)); +}#endif arch/arm64/kvm/mmu.c +4
-3@@ -834,16 +834,17 @@ 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_DYING ) { -/* TODO: teardown rtts */ write_unlock (&kvm->mmu_lock); +kvm_realm_destroy_rtts (&kvm->arch.realm, pgt->ia_bits, + pgt->start_level);return ; } - pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ; mmu->pgt =NULL ;arch/arm64/kvm/rme.c +3
-0@@ -73,6 +73,28 @@ static int rmi_check_version(void)return 0 ; } +static void realm_destroy_undelegate_range(struct realm *realm, + unsigned long ipa, + unsigned long addr,
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 88
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+ ssize_t size) +{ +unsigned long rd =virt_to_phys (realm->rd); +int ret; + +while (size >0 ) { + ret =rmi_data_destroy (rd, ipa); +WARN_ON (ret); + ret =rmi_granule_undelegate (addr); +
As the return value is not documented, what will happen if a page undelegate failed? Leaked? Some explanation is required here.
Quoted 136 lines
arch/arm64/kvm/rme.c +126
-0+if (ret) +get_page (phys_to_page (addr)); + + addr +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + size -=PAGE_SIZE ; + } +} +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) {unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);@@ -170,6 +192,123 @@ static int realm_create_rd(struct kvm *kvm)return r; } +static int realm_rtt_destroy (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_destroy (rtt_granule,virt_to_phys (realm->rd), addr, + level); +} + +static int realm_destroy_free_rtt (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ +if (realm_rtt_destroy (realm, addr, level, rtt_granule)) +return -ENXIO ; +if (!WARN_ON (rmi_granule_undelegate (rtt_granule))) +put_page (phys_to_page (rtt_granule)); + +return 0 ; +} + +static int realm_rtt_create (struct realm *realm, +unsigned long addr, +int level, +phys_addr_t phys) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_create (phys,virt_to_phys (realm->rd), addr, level); +} + +static int realm_tear_down_rtt_range (struct realm *realm,int level, +unsigned long start,unsigned long end) +{ +phys_addr_t rd =virt_to_phys (realm->rd); +ssize_t map_size =rme_rtt_level_mapsize (level); +unsigned long addr, next_addr; +bool failed = false; + +for (addr = start; addr < end; addr = next_addr) { +phys_addr_t rtt_addr, tmp_rtt; +struct rtt_entry rtt; +unsigned long end_addr; + + next_addr =ALIGN (addr +1 , map_size); + + end_addr =min (next_addr, end); + +if (rmi_rtt_read_entry (rd,ALIGN_DOWN (addr, map_size), + level, &rtt)) { + failed = true; +continue ; + } + + rtt_addr =rmi_rtt_get_phys (&rtt); +WARN_ON (level != rtt.walk_level); + +switch (rtt.state) { +case RMI_UNASSIGNED : +case RMI_DESTROYED : +break ; +case RMI_TABLE : +if (realm_tear_down_rtt_range (realm, level +1 , + addr, end_addr)) { + failed = true; +break ; + } +if (IS_ALIGNED (addr, map_size) && + next_addr <= end && +realm_destroy_free_rtt (realm, addr, level +1 , + rtt_addr)) + failed = true; +break ; +case RMI_ASSIGNED : +WARN_ON (!rtt_addr); +/* +* If there is a block mapping, break it now, using the +* spare_page. We are sure to have a valid delegated +* page at spare_page before we enter here, otherwise +* WARN once, which will be followed by further +* warnings. +*/ + tmp_rtt = realm->spare_page; +if (level ==2 && + !WARN_ON_ONCE (tmp_rtt ==PHYS_ADDR_MAX ) && +realm_rtt_create (realm, addr, +RME_RTT_MAX_LEVEL , tmp_rtt)) { +WARN_ON (1 ); + failed = true; +break ; + } +realm_destroy_undelegate_range (realm, addr, + rtt_addr, map_size); +/* +* Collapse the last level table and make the spare page +* reusable again. +*/ +if (level ==2 && +realm_rtt_destroy (realm, addr,RME_RTT_MAX_LEVEL , + tmp_rtt)) + failed = true; +break ; +case RMI_VALID_NS : +WARN_ON (rmi_rtt_unmap_unprotected (rd, addr, level)); +break ; +default : +WARN_ON (1 ); + failed = true; +break ; + } + } + +return failed ? -EINVAL :0 ; +} + +void kvm_realm_destroy_rtts (struct realm *realm,u32 ia_bits,u32 start_level) +{ +realm_tear_down_rtt_range (realm, start_level,0 , (1UL << ia_bits)); +} +/* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK (rme_vmid_lock);static unsigned long *rme_vmid_bitmap;
Re: [RFC PATCH 09/28] arm64: RME: RTT handling
Quoted 9 lines · Zhi Wang
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:13 +0000 Steven Price <[email protected]> wrote:
The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
Quoted 3 lines
This is only an introduction to RTT handling. While this patch is mostly like RTT teardown, better add more introduction to this patch. Also maybe refine the tittle to reflect what this patch is actually doing.
You've a definite point that this patch is mostly about RTT teardown. Technically it also adds the RTT creation path (realm_rtt_create) - hence the generic patch title.
But I'll definitely expand the commit message to mention the complexity of tear down which is the bulk of the patch.
Quoted 69 lines
Quoted 69 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 19 +++++ arch/arm64/kvm/mmu.c | 7 +- arch/arm64/kvm/rme.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_rme.h +19
-0@@ -35,5 +35,24 @@ 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 realm *realm,u32 ia_bits,u32 start_level); + +#define RME_RTT_BLOCK_LEVEL 2 +#define RME_RTT_MAX_LEVEL 3 + +#define RME_PAGE_SHIFT 12 +#define RME_PAGE_SIZE BIT(RME_PAGE_SHIFT) +/* See ARM64_HW_PGTABLE_LEVEL_SHIFT() */ +#define RME_RTT_LEVEL_SHIFT (l) \ + ((RME_PAGE_SHIFT - 3) * (4 - (l)) + 3) +#define RME_L2_BLOCK_SIZE BIT(RME_RTT_LEVEL_SHIFT(2)) + +static inline unsigned long rme_rtt_level_mapsize (int level) +{ +if (WARN_ON (level >RME_RTT_MAX_LEVEL )) +return RME_PAGE_SIZE ; + +return (1UL <<RME_RTT_LEVEL_SHIFT (level)); +}#endif arch/arm64/kvm/mmu.c +4
-3@@ -834,16 +834,17 @@ 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_DYING ) { -/* TODO: teardown rtts */ write_unlock (&kvm->mmu_lock); +kvm_realm_destroy_rtts (&kvm->arch.realm, pgt->ia_bits, + pgt->start_level);return ; } - pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ; mmu->pgt =NULL ;arch/arm64/kvm/rme.c +0
-0@@ -73,6 +73,28 @@ static int rmi_check_version(void)return 0 ; }
Quoted 15 lines
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 88
Quoted 13 lines
arch/arm64/kvm/rme.c +13
-0+static void realm_destroy_undelegate_range (struct realm *realm, +unsigned long ipa, +unsigned long addr, +ssize_t size) +{ +unsigned long rd =virt_to_phys (realm->rd); +int ret; + +while (size >0 ) { + ret =rmi_data_destroy (rd, ipa); +WARN_ON (ret); + ret =rmi_granule_undelegate (addr); +As the return value is not documented, what will happen if a page undelegate failed? Leaked? Some explanation is required here.
Yes - it's leaked. I'll add a comment to explain the get_page() call.
Thanks,
Steve
Quoted 137 lines
Quoted 136 lines
arch/arm64/kvm/rme.c +126
-0+if (ret) +get_page (phys_to_page (addr)); + + addr +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + size -=PAGE_SIZE ; + } +} +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) {unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);@@ -170,6 +192,123 @@ static int realm_create_rd(struct kvm *kvm)return r; } +static int realm_rtt_destroy (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_destroy (rtt_granule,virt_to_phys (realm->rd), addr, + level); +} + +static int realm_destroy_free_rtt (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ +if (realm_rtt_destroy (realm, addr, level, rtt_granule)) +return -ENXIO ; +if (!WARN_ON (rmi_granule_undelegate (rtt_granule))) +put_page (phys_to_page (rtt_granule)); + +return 0 ; +} + +static int realm_rtt_create (struct realm *realm, +unsigned long addr, +int level, +phys_addr_t phys) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_create (phys,virt_to_phys (realm->rd), addr, level); +} + +static int realm_tear_down_rtt_range (struct realm *realm,int level, +unsigned long start,unsigned long end) +{ +phys_addr_t rd =virt_to_phys (realm->rd); +ssize_t map_size =rme_rtt_level_mapsize (level); +unsigned long addr, next_addr; +bool failed = false; + +for (addr = start; addr < end; addr = next_addr) { +phys_addr_t rtt_addr, tmp_rtt; +struct rtt_entry rtt; +unsigned long end_addr; + + next_addr =ALIGN (addr +1 , map_size); + + end_addr =min (next_addr, end); + +if (rmi_rtt_read_entry (rd,ALIGN_DOWN (addr, map_size), + level, &rtt)) { + failed = true; +continue ; + } + + rtt_addr =rmi_rtt_get_phys (&rtt); +WARN_ON (level != rtt.walk_level); + +switch (rtt.state) { +case RMI_UNASSIGNED : +case RMI_DESTROYED : +break ; +case RMI_TABLE : +if (realm_tear_down_rtt_range (realm, level +1 , + addr, end_addr)) { + failed = true; +break ; + } +if (IS_ALIGNED (addr, map_size) && + next_addr <= end && +realm_destroy_free_rtt (realm, addr, level +1 , + rtt_addr)) + failed = true; +break ; +case RMI_ASSIGNED : +WARN_ON (!rtt_addr); +/* +* If there is a block mapping, break it now, using the +* spare_page. We are sure to have a valid delegated +* page at spare_page before we enter here, otherwise +* WARN once, which will be followed by further +* warnings. +*/ + tmp_rtt = realm->spare_page; +if (level ==2 && + !WARN_ON_ONCE (tmp_rtt ==PHYS_ADDR_MAX ) && +realm_rtt_create (realm, addr, +RME_RTT_MAX_LEVEL , tmp_rtt)) { +WARN_ON (1 ); + failed = true; +break ; + } +realm_destroy_undelegate_range (realm, addr, + rtt_addr, map_size); +/* +* Collapse the last level table and make the spare page +* reusable again. +*/ +if (level ==2 && +realm_rtt_destroy (realm, addr,RME_RTT_MAX_LEVEL , + tmp_rtt)) + failed = true; +break ; +case RMI_VALID_NS : +WARN_ON (rmi_rtt_unmap_unprotected (rd, addr, level)); +break ; +default : +WARN_ON (1 ); + failed = true; +break ; + } + } + +return failed ? -EINVAL :0 ; +} + +void kvm_realm_destroy_rtts (struct realm *realm,u32 ia_bits,u32 start_level) +{ +realm_tear_down_rtt_range (realm, start_level,0 , (1UL << ia_bits)); +} +/* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK (rme_vmid_lock);static unsigned long *rme_vmid_bitmap;
Re: [RFC PATCH 09/28] arm64: RME: RTT handling
Quoted 10 lines · On Fri, 3 Mar 2023 14:04:56 +0000 Steven Price <[email protected]> wrote:
Quoted 9 lines · Zhi Wang
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:13 +0000 Steven Price <[email protected]> wrote:
The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
Quoted 8 lines
Quoted 3 lines
This is only an introduction to RTT handling. While this patch is mostly like RTT teardown, better add more introduction to this patch. Also maybe refine the tittle to reflect what this patch is actually doing.
You've a definite point that this patch is mostly about RTT teardown. Technically it also adds the RTT creation path (
realm_rtt_create) - hence the generic patch title.
But realm_rtt_create() seem only used in realm_tear_down_rtt_range(). That makes me wonder where is the real RTT creation path.
Quoted 2 lines
But I'll definitely expand the commit message to mention the complexity of tear down which is the bulk of the patch.
It is also a good place to explain more about the RTT.
Quoted 230 lines
Quoted 70 lines
Quoted 70 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 19 +++++ arch/arm64/kvm/mmu.c | 7 +- arch/arm64/kvm/rme.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_rme.h +19
-0@@ -35,5 +35,24 @@ 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 realm *realm,u32 ia_bits,u32 start_level); + +#define RME_RTT_BLOCK_LEVEL 2 +#define RME_RTT_MAX_LEVEL 3 + +#define RME_PAGE_SHIFT 12 +#define RME_PAGE_SIZE BIT(RME_PAGE_SHIFT) +/* See ARM64_HW_PGTABLE_LEVEL_SHIFT() */ +#define RME_RTT_LEVEL_SHIFT (l) \ + ((RME_PAGE_SHIFT - 3) * (4 - (l)) + 3) +#define RME_L2_BLOCK_SIZE BIT(RME_RTT_LEVEL_SHIFT(2)) + +static inline unsigned long rme_rtt_level_mapsize (int level) +{ +if (WARN_ON (level >RME_RTT_MAX_LEVEL )) +return RME_PAGE_SIZE ; + +return (1UL <<RME_RTT_LEVEL_SHIFT (level)); +}#endif arch/arm64/kvm/mmu.c +4
-3@@ -834,16 +834,17 @@ 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_DYING ) { -/* TODO: teardown rtts */ write_unlock (&kvm->mmu_lock); +kvm_realm_destroy_rtts (&kvm->arch.realm, pgt->ia_bits, + pgt->start_level);return ; } - pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ; mmu->pgt =NULL ;arch/arm64/kvm/rme.c +0
-0@@ -73,6 +73,28 @@ static int rmi_check_version(void)return 0 ; }Quoted 15 lines
arch/arm64/kvm/rme.c static int rmi_check_version(void) · line 88
Quoted 13 lines
arch/arm64/kvm/rme.c +13
-0+static void realm_destroy_undelegate_range (struct realm *realm, +unsigned long ipa, +unsigned long addr, +ssize_t size) +{ +unsigned long rd =virt_to_phys (realm->rd); +int ret; + +while (size >0 ) { + ret =rmi_data_destroy (rd, ipa); +WARN_ON (ret); + ret =rmi_granule_undelegate (addr); +As the return value is not documented, what will happen if a page undelegate failed? Leaked? Some explanation is required here.
Yes - it's leaked. I'll add a comment to explain the
get_page()call.Thanks,
Steve
Quoted 137 lines
Quoted 136 lines
arch/arm64/kvm/rme.c +126
-0+if (ret) +get_page (phys_to_page (addr)); + + addr +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + size -=PAGE_SIZE ; + } +} +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) {unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);@@ -170,6 +192,123 @@ static int realm_create_rd(struct kvm *kvm)return r; } +static int realm_rtt_destroy (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_destroy (rtt_granule,virt_to_phys (realm->rd), addr, + level); +} + +static int realm_destroy_free_rtt (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ +if (realm_rtt_destroy (realm, addr, level, rtt_granule)) +return -ENXIO ; +if (!WARN_ON (rmi_granule_undelegate (rtt_granule))) +put_page (phys_to_page (rtt_granule)); + +return 0 ; +} + +static int realm_rtt_create (struct realm *realm, +unsigned long addr, +int level, +phys_addr_t phys) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_create (phys,virt_to_phys (realm->rd), addr, level); +} + +static int realm_tear_down_rtt_range (struct realm *realm,int level, +unsigned long start,unsigned long end) +{ +phys_addr_t rd =virt_to_phys (realm->rd); +ssize_t map_size =rme_rtt_level_mapsize (level); +unsigned long addr, next_addr; +bool failed = false; + +for (addr = start; addr < end; addr = next_addr) { +phys_addr_t rtt_addr, tmp_rtt; +struct rtt_entry rtt; +unsigned long end_addr; + + next_addr =ALIGN (addr +1 , map_size); + + end_addr =min (next_addr, end); + +if (rmi_rtt_read_entry (rd,ALIGN_DOWN (addr, map_size), + level, &rtt)) { + failed = true; +continue ; + } + + rtt_addr =rmi_rtt_get_phys (&rtt); +WARN_ON (level != rtt.walk_level); + +switch (rtt.state) { +case RMI_UNASSIGNED : +case RMI_DESTROYED : +break ; +case RMI_TABLE : +if (realm_tear_down_rtt_range (realm, level +1 , + addr, end_addr)) { + failed = true; +break ; + } +if (IS_ALIGNED (addr, map_size) && + next_addr <= end && +realm_destroy_free_rtt (realm, addr, level +1 , + rtt_addr)) + failed = true; +break ; +case RMI_ASSIGNED : +WARN_ON (!rtt_addr); +/* +* If there is a block mapping, break it now, using the +* spare_page. We are sure to have a valid delegated +* page at spare_page before we enter here, otherwise +* WARN once, which will be followed by further +* warnings. +*/ + tmp_rtt = realm->spare_page; +if (level ==2 && + !WARN_ON_ONCE (tmp_rtt ==PHYS_ADDR_MAX ) && +realm_rtt_create (realm, addr, +RME_RTT_MAX_LEVEL , tmp_rtt)) { +WARN_ON (1 ); + failed = true; +break ; + } +realm_destroy_undelegate_range (realm, addr, + rtt_addr, map_size); +/* +* Collapse the last level table and make the spare page +* reusable again. +*/ +if (level ==2 && +realm_rtt_destroy (realm, addr,RME_RTT_MAX_LEVEL , + tmp_rtt)) + failed = true; +break ; +case RMI_VALID_NS : +WARN_ON (rmi_rtt_unmap_unprotected (rd, addr, level)); +break ; +default : +WARN_ON (1 ); + failed = true; +break ; + } + } + +return failed ? -EINVAL :0 ; +} + +void kvm_realm_destroy_rtts (struct realm *realm,u32 ia_bits,u32 start_level) +{ +realm_tear_down_rtt_range (realm, start_level,0 , (1UL << ia_bits)); +} +/* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK (rme_vmid_lock);static unsigned long *rme_vmid_bitmap;
Re: [RFC PATCH 09/28] arm64: RME: RTT handling
In reply to Steven PriceQuoted 17 lines · Steven Price
The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 19 +++++ arch/arm64/kvm/mmu.c | 7 +- arch/arm64/kvm/rme.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_rme.h +0
-0
arch/arm64/include/asm/kvm_rme.h u32 kvm_realm_ipa_limit(void); · line 44
Quoted 10 lines
arch/arm64/include/asm/kvm_rme.h +7
-0@@ -35,5 +35,24 @@ 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 realm *realm,u32 ia_bits,u32 start_level); + +#define RME_RTT_BLOCK_LEVEL 2 +#define RME_RTT_MAX_LEVEL 3 + +#define RME_PAGE_SHIFT 12 +#define RME_PAGE_SIZE BIT(RME_PAGE_SHIFT)
Can we use PAGE_SIZE and PAGE_SHIFT instead of redefining? May be we can use them to define RME_PAGE_SIZE and RME_PAGE_SHIFT.
arch/arm64/include/asm/kvm_rme.h u32 kvm_realm_ipa_limit(void); · line 47
Quoted 3 lines
arch/arm64/include/asm/kvm_rme.h +3
-0+/* See ARM64_HW_PGTABLE_LEVEL_SHIFT() */ +#define RME_RTT_LEVEL_SHIFT (l) \ + ((RME_PAGE_SHIFT - 3) * (4 - (l)) + 3)
Instead of defining again, can we define toARM64_HW_PGTABLE_LEVEL_SHIFT?
Quoted 183 lines
arch/arm64/include/asm/kvm_rme.h +9
-0+#define RME_L2_BLOCK_SIZE BIT(RME_RTT_LEVEL_SHIFT(2)) + +static inline unsigned long rme_rtt_level_mapsize (int level) +{ +if (WARN_ON (level >RME_RTT_MAX_LEVEL )) +return RME_PAGE_SIZE ; + +return (1UL <<RME_RTT_LEVEL_SHIFT (level)); +}#endif arch/arm64/kvm/mmu.c +4
-3@@ -834,16 +834,17 @@ 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_DYING ) { -/* TODO: teardown rtts */ write_unlock (&kvm->mmu_lock); +kvm_realm_destroy_rtts (&kvm->arch.realm, pgt->ia_bits, + pgt->start_level);return ; } - pgt = mmu->pgt;if (pgt) { mmu->pgd_phys =0 ; mmu->pgt =NULL ;arch/arm64/kvm/rme.c +132
-0@@ -73,6 +73,28 @@ static int rmi_check_version(void)return 0 ; } +static void realm_destroy_undelegate_range (struct realm *realm, +unsigned long ipa, +unsigned long addr, +ssize_t size) +{ +unsigned long rd =virt_to_phys (realm->rd); +int ret; + +while (size >0 ) { + ret =rmi_data_destroy (rd, ipa); +WARN_ON (ret); + ret =rmi_granule_undelegate (addr); + +if (ret) +get_page (phys_to_page (addr)); + + addr +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + size -=PAGE_SIZE ; + } +} +static unsigned long create_realm_feat_reg0 (struct kvm *kvm) {unsigned long ia_bits =VTCR_EL2_IPA (kvm->arch.vtcr);@@ -170,6 +192,123 @@ static int realm_create_rd(struct kvm *kvm)return r; } +static int realm_rtt_destroy (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_destroy (rtt_granule,virt_to_phys (realm->rd), addr, + level); +} + +static int realm_destroy_free_rtt (struct realm *realm,unsigned long addr, +int level,phys_addr_t rtt_granule) +{ +if (realm_rtt_destroy (realm, addr, level, rtt_granule)) +return -ENXIO ; +if (!WARN_ON (rmi_granule_undelegate (rtt_granule))) +put_page (phys_to_page (rtt_granule)); + +return 0 ; +} + +static int realm_rtt_create (struct realm *realm, +unsigned long addr, +int level, +phys_addr_t phys) +{ + addr =ALIGN_DOWN (addr,rme_rtt_level_mapsize (level -1 )); +return rmi_rtt_create (phys,virt_to_phys (realm->rd), addr, level); +} + +static int realm_tear_down_rtt_range (struct realm *realm,int level, +unsigned long start,unsigned long end) +{ +phys_addr_t rd =virt_to_phys (realm->rd); +ssize_t map_size =rme_rtt_level_mapsize (level); +unsigned long addr, next_addr; +bool failed = false; + +for (addr = start; addr < end; addr = next_addr) { +phys_addr_t rtt_addr, tmp_rtt; +struct rtt_entry rtt; +unsigned long end_addr; + + next_addr =ALIGN (addr +1 , map_size); + + end_addr =min (next_addr, end); + +if (rmi_rtt_read_entry (rd,ALIGN_DOWN (addr, map_size), + level, &rtt)) { + failed = true; +continue ; + } + + rtt_addr =rmi_rtt_get_phys (&rtt); +WARN_ON (level != rtt.walk_level); + +switch (rtt.state) { +case RMI_UNASSIGNED : +case RMI_DESTROYED : +break ; +case RMI_TABLE : +if (realm_tear_down_rtt_range (realm, level +1 , + addr, end_addr)) { + failed = true; +break ; + } +if (IS_ALIGNED (addr, map_size) && + next_addr <= end && +realm_destroy_free_rtt (realm, addr, level +1 , + rtt_addr)) + failed = true; +break ; +case RMI_ASSIGNED : +WARN_ON (!rtt_addr); +/* +* If there is a block mapping, break it now, using the +* spare_page. We are sure to have a valid delegated +* page at spare_page before we enter here, otherwise +* WARN once, which will be followed by further +* warnings. +*/ + tmp_rtt = realm->spare_page; +if (level ==2 && + !WARN_ON_ONCE (tmp_rtt ==PHYS_ADDR_MAX ) && +realm_rtt_create (realm, addr, +RME_RTT_MAX_LEVEL , tmp_rtt)) { +WARN_ON (1 ); + failed = true; +break ; + } +realm_destroy_undelegate_range (realm, addr, + rtt_addr, map_size); +/* +* Collapse the last level table and make the spare page +* reusable again. +*/ +if (level ==2 && +realm_rtt_destroy (realm, addr,RME_RTT_MAX_LEVEL , + tmp_rtt)) + failed = true; +break ; +case RMI_VALID_NS : +WARN_ON (rmi_rtt_unmap_unprotected (rd, addr, level)); +break ; +default : +WARN_ON (1 ); + failed = true; +break ; + } + } + +return failed ? -EINVAL :0 ;
arch/arm64/kvm/rme.c static int realm_create_rd(struct kvm *kvm) · line 314
Quoted 10 lines
arch/arm64/kvm/rme.c +7
-0+} + +void kvm_realm_destroy_rtts (struct realm *realm,u32 ia_bits,u32 start_level) +{ +realm_tear_down_rtt_range (realm, start_level,0 , (1UL << ia_bits)); +} +/* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK (rme_vmid_lock);static unsigned long *rme_vmid_bitmap;
Thanks,
Ganapat
Re: [RFC PATCH 09/28] arm64: RME: RTT handling
Quoted 26 lines · Ganapatrao Kulkarni
Quoted 24 lines · Steven Price
The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
Signed-off-by Steven Price <[email protected]>---
arch/arm64/include/asm/kvm_rme.h| 19 +++++ arch/arm64/kvm/mmu.c | 7 +- arch/arm64/kvm/rme.c | 139 +++++++++++++++++++++++++++++++3 files changed, 162 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_rme.h +2
-0@@ -35,5 +35,24 @@ 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 realm *realm,u32 ia_bits,u32 start_level); +
Quoted 8 lines
arch/arm64/include/asm/kvm_rme.h u32 kvm_realm_ipa_limit(void); · line 44
Quoted 5 lines
arch/arm64/include/asm/kvm_rme.h +5
-0+#define RME_RTT_BLOCK_LEVEL 2 +#define RME_RTT_MAX_LEVEL 3 + +#define RME_PAGE_SHIFT 12 +#define RME_PAGE_SIZE BIT(RME_PAGE_SHIFT)Can we use
PAGE_SIZEandPAGE_SHIFTinstead of redefining? May be we can use them to defineRME_PAGE_SIZEandRME_PAGE_SHIFT.
At the moment the code only supports the host page size matching the RMM's. But I want to leave open the possibility for the host size being larger than the RMM's. In this case PAGE_SHIFT/PAGE_SIZE will not equal RME_PAGE_SIZE and RME_PAGE_SHIFT. The host will have to create multiple RMM RTTs for each host page.
Quoted 6 lines
arch/arm64/include/asm/kvm_rme.h u32 kvm_realm_ipa_limit(void); · line 47
Quoted 3 lines
arch/arm64/include/asm/kvm_rme.h +3
-0+/* See ARM64_HW_PGTABLE_LEVEL_SHIFT() */ +#define RME_RTT_LEVEL_SHIFT (l) \ + ((RME_PAGE_SHIFT - 3) * (4 - (l)) + 3)Instead of defining again, can we define to
ARM64_HW_PGTABLE_LEVEL_SHIFT?
Same as above - ARM64_HW_PGTABLE_LEVEL_SHIFT uses PAGE_SHIFT, but we want the same calculation using RME_PAGE_SHIFT which might be different.
Thanks,
Steve
RFC PATCH 10/28 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.
--- arch/arm64/include/asm/kvm_emulate.h | 2 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/include/asm/kvm_rme.h | 10 ++ arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/reset.c | 11 ++ arch/arm64/kvm/rme.c | 144 +++++++++++++++++++++++++++ 6 files changed, 171 insertions(+)
arch/arm64/include/asm/kvm_emulate.h +2 -0
@@ -504,6 +504,8 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) { +if (static_branch_unlikely (&kvm_rme_is_available)) +return vcpu->arch.rec.mpidr !=INVALID_HWID ;return false; }
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -505,6 +505,9 @@ struct kvm_vcpu_arch {u64 last_steal;gpa_t base; } steal; + +/* Realm meta data */ +struct rec rec; };/*
arch/arm64/include/asm/kvm_rme.h +10 -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> enum realm_state {
@@ -29,6 +30,13 @@ struct realm {unsigned int ia_bits; }; +struct rec { +unsigned long mpidr; +void *rec_page; +struct page *aux_pages[REC_PARAMS_AUX_GRANULES ]; +struct rec_run *run; +}; +int kvm_init_rme (void );u32 kvm_realm_ipa_limit (void );
@@ -36,6 +44,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 realm *realm,u32 ia_bits,u32 start_level); +int kvm_create_rec (struct kvm_vcpu *vcpu); +void kvm_destroy_rec (struct kvm_vcpu *vcpu);#define RME_RTT_BLOCK_LEVEL 2#define RME_RTT_MAX_LEVEL 3
arch/arm64/kvm/arm.c +1 -0
@@ -373,6 +373,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES ); + vcpu->arch.rec.mpidr =INVALID_HWID ; vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
arch/arm64/kvm/reset.c +11 -0
@@ -135,6 +135,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)) +return -EINVAL ; + +return kvm_create_rec (vcpu); }return -EINVAL ;
@@ -145,6 +150,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) && +READ_ONCE (vcpu->kvm->arch.realm.state) ==REALM_STATE_ACTIVE )) +return false; +return true; }
@@ -157,6 +167,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)if (sve_state)kvm_unshare_hyp (sve_state, sve_state +vcpu_sve_state_size (vcpu));kfree (sve_state); +kvm_destroy_rec (vcpu); }static void kvm_vcpu_reset_sve (struct kvm_vcpu *vcpu)
arch/arm64/kvm/rme.c +144 -0
150 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH 10/28] arm64: RME: Allocate/free RECs to match vCPUs
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:14 +0000 Steven Price <[email protected]> wrote:
The RMM maintains a data structure known as the Realm Execution Context (or REC). It is similar to struct
kvm_vcpuand 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.
It would be better to leave some pointers to the spec here. It really saves time for reviewers.
Quoted 28 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 2 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/include/asm/kvm_rme.h | 10 ++ arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/reset.c | 11 ++ arch/arm64/kvm/rme.c | 144 +++++++++++++++++++++++++++ 6 files changed, 171 insertions(+)arch/arm64/include/asm/kvm_emulate.h +2
-0@@ -504,6 +504,8 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) { +if (static_branch_unlikely (&kvm_rme_is_available)) +return vcpu->arch.rec.mpidr !=INVALID_HWID ;return false; }arch/arm64/include/asm/kvm_host.h +0
-0
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 510
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -505,6 +505,9 @@ struct kvm_vcpu_arch {u64 last_steal;gpa_t base; } steal; + +/* Realm meta data */ +struct rec rec;
I think the name of the data structure "rec" needs a prefix, it is too common and might conflict with the private data structures in the other modules. Maybe rme_rec or realm_rec?
Quoted 16 lines
arch/arm64/include/asm/kvm_host.h +0
-0};/* arch/arm64/include/asm/kvm_rme.h +1
-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> enum realm_state {
arch/arm64/include/asm/kvm_rme.h struct realm { · line 39
Quoted 10 lines
arch/arm64/include/asm/kvm_rme.h +7
-0@@ -29,6 +30,13 @@ struct realm {unsigned int ia_bits; }; +struct rec { +unsigned long mpidr; +void *rec_page; +struct page *aux_pages[REC_PARAMS_AUX_GRANULES ]; +struct rec_run *run; +}; +
It is better to leave some comments for above members or pointers to the spec, that saves a lot of time for review.
Quoted 41 lines
arch/arm64/include/asm/kvm_rme.h +2
-0int kvm_init_rme (void );u32 kvm_realm_ipa_limit (void );@@ -36,6 +44,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 realm *realm,u32 ia_bits,u32 start_level); +int kvm_create_rec (struct kvm_vcpu *vcpu); +void kvm_destroy_rec (struct kvm_vcpu *vcpu);#define RME_RTT_BLOCK_LEVEL 2#define RME_RTT_MAX_LEVEL 3arch/arm64/kvm/arm.c +1
-0@@ -373,6 +373,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES ); + vcpu->arch.rec.mpidr =INVALID_HWID ; vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;arch/arm64/kvm/reset.c +5
-0@@ -135,6 +135,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)) +return -EINVAL ; + +return kvm_create_rec (vcpu); }return -EINVAL ;
arch/arm64/kvm/reset.c bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu) · line 156
Quoted 7 lines
arch/arm64/kvm/reset.c +4
-0@@ -145,6 +150,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) && +READ_ONCE (vcpu->kvm->arch.realm.state) ==REALM_STATE_ACTIVE )) +return false;
That's why it is better to introduce the realm state in the previous patches so that people can really get the idea of the states at this stage.
Quoted 103 lines
arch/arm64/kvm/reset.c +2
-0+return true; }@@ -157,6 +167,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)if (sve_state)kvm_unshare_hyp (sve_state, sve_state +vcpu_sve_state_size (vcpu));kfree (sve_state); +kvm_destroy_rec (vcpu); }static void kvm_vcpu_reset_sve (struct kvm_vcpu *vcpu)arch/arm64/kvm/rme.c +83
-0@@ -514,6 +514,150 @@ void kvm_destroy_realm(struct kvm *kvm)kvm_free_stage2_pgd (&kvm->arch.mmu); } +static void free_rec_aux (struct page **aux_pages, +unsigned int num_aux) +{ +unsigned int i; + +for (i =0 ; i < num_aux; i++) { +phys_addr_t aux_page_phys =page_to_phys (aux_pages[i]); + +if (WARN_ON (rmi_granule_undelegate (aux_page_phys))) +continue ; + +__free_page (aux_pages[i]); + } +} + +static int alloc_rec_aux (struct page **aux_pages, +u64 *aux_phys_pages, +unsigned int num_aux) +{ +int ret; +unsigned int i; + +for (i =0 ; i < num_aux; i++) { +struct page *aux_page; +phys_addr_t aux_page_phys; + + aux_page =alloc_page (GFP_KERNEL ); +if (!aux_page) { + ret = -ENOMEM ; + goto out_err; + } + aux_page_phys =page_to_phys (aux_page); +if (rmi_granule_delegate (aux_page_phys)) { +__free_page (aux_page); + ret = -ENXIO ; + goto out_err; + } + aux_pages[i] = aux_page; + aux_phys_pages[i] = aux_page_phys; + } + +return 0 ; +out_err: +free_rec_aux (aux_pages, i); +return ret; +} + +int kvm_create_rec (struct kvm_vcpu *vcpu) +{ +struct user_pt_regs *vcpu_regs =vcpu_gp_regs (vcpu); +unsigned long mpidr =kvm_vcpu_get_mpidr_aff (vcpu); +struct realm *realm = &vcpu->kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +unsigned long rec_page_phys; +struct rec_params *params; +int r, i; + +if (kvm_realm_state (vcpu->kvm) !=REALM_STATE_NEW ) +return -ENOENT ; + +/* +* The RMM will report PSCI v1.0 to Realms and the KVM_ARM_VCPU_PSCI_0_2 +* flag covers v0.2 and onwards. +*/ +if (!test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features)) +return -EINVAL ; + +BUILD_BUG_ON (sizeof (*params) >PAGE_SIZE ); +BUILD_BUG_ON (sizeof (*rec->run) >PAGE_SIZE ); + + params = (struct rec_params *)get_zeroed_page (GFP_KERNEL ); + rec->rec_page = (void *)__get_free_page (GFP_KERNEL ); + rec->run = (void *)get_zeroed_page (GFP_KERNEL ); +if (!params || !rec->rec_page || !rec->run) { + r = -ENOMEM ; + goto out_free_pages; + } + +for (i =0 ; i <ARRAY_SIZE (params->gprs); i++) + params->gprs[i] = vcpu_regs->regs[i]; + + params->pc = vcpu_regs->pc; +
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 609
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+if (vcpu->vcpu_id ==0 ) + params->flags |=REC_PARAMS_FLAG_RUNNABLE ; + + rec_page_phys =virt_to_phys (rec->rec_page); + +if (rmi_granule_delegate (rec_page_phys)) { + r = -ENXIO ; + goto out_free_pages; + } +
Wouldn't it be better to extend the alloc_rec_aux() to allocate and delegate pages above? so that you can same some gfps and rmi_granuale_delegates().
Quoted 37 lines
arch/arm64/kvm/rme.c +37
-0+ r =alloc_rec_aux (rec->aux_pages, params->aux, realm->num_aux); +if (r) + goto out_undelegate_rmm_rec; + + params->num_rec_aux = realm->num_aux; + params->mpidr = mpidr; + +if (rmi_rec_create (rec_page_phys, +virt_to_phys (realm->rd), +virt_to_phys (params))) { + r = -ENXIO ; + goto out_free_rec_aux; + } + + rec->mpidr = mpidr; + +free_page ((unsigned long )params); +return 0 ; + +out_free_rec_aux: +free_rec_aux (rec->aux_pages, realm->num_aux); +out_undelegate_rmm_rec: +if (WARN_ON (rmi_granule_undelegate (rec_page_phys))) + rec->rec_page =NULL ; +out_free_pages: +free_page ((unsigned long )rec->run); +free_page ((unsigned long )rec->rec_page); +free_page ((unsigned long )params); +return r; +} + +void kvm_destroy_rec (struct kvm_vcpu *vcpu) +{ +struct realm *realm = &vcpu->kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +unsigned long rec_page_phys; +
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 656
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+if (!vcpu_is_rec (vcpu)) +return ; + + rec_page_phys =virt_to_phys (rec->rec_page); + +if (WARN_ON (rmi_rec_destroy (rec_page_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rec_page_phys))) +return ; +
The two returns above feels off. What is the reason to skip the below page undelegates?
Quoted 7 lines
arch/arm64/kvm/rme.c +4
-0+free_rec_aux (rec->aux_pages, realm->num_aux); +free_page ((unsigned long )rec->rec_page); +} +int kvm_init_realm_vm (struct kvm *kvm) {struct realm_params *params;
Re: [RFC PATCH 10/28] arm64: RME: Allocate/free RECs to match vCPUs
Quoted 11 lines · Zhi Wang
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:14 +0000 Steven Price <[email protected]> wrote:
The RMM maintains a data structure known as the Realm Execution Context (or REC). It is similar to struct
kvm_vcpuand 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.It would be better to leave some pointers to the spec here. It really saves time for reviewers.
Fair enough. I wasn't sure how often to repeat the link to the spec, but a few more times wouldn't hurt ;)
Quoted 23 lines
Quoted 23 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 2 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/include/asm/kvm_rme.h | 10 ++ arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/reset.c | 11 ++ arch/arm64/kvm/rme.c | 144 +++++++++++++++++++++++++++ 6 files changed, 171 insertions(+)arch/arm64/include/asm/kvm_emulate.h +2
-0@@ -504,6 +504,8 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) { +if (static_branch_unlikely (&kvm_rme_is_available)) +return vcpu->arch.rec.mpidr !=INVALID_HWID ;return false; }
Quoted 15 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 510
Quoted 11 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -505,6 +505,9 @@ struct kvm_vcpu_arch {u64 last_steal;gpa_t base; } steal; + +/* Realm meta data */ +struct rec rec;I think the name of the data structure "rec" needs a prefix, it is too common and might conflict with the private data structures in the other modules. Maybe
rme_recorrealm_rec?
struct realm_rec seems like a good choice. I agree 'rec' without context is somewhat ambiguous.
Quoted 19 lines
Quoted 19 lines
arch/arm64/include/asm/kvm_host.h +0
-0};/* arch/arm64/include/asm/kvm_rme.h +1
-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> enum realm_state {@@ -29,6 +30,13 @@ struct realm {unsigned int ia_bits; };
Quoted 10 lines
arch/arm64/include/asm/kvm_rme.h struct realm { · line 39
Quoted 7 lines
arch/arm64/include/asm/kvm_rme.h +7
-0+struct rec { +unsigned long mpidr; +void *rec_page; +struct page *aux_pages[REC_PARAMS_AUX_GRANULES ]; +struct rec_run *run; +}; +It is better to leave some comments for above members or pointers to the spec, that saves a lot of time for review.
Will add comments.
Quoted 160 lines
Quoted 41 lines
arch/arm64/include/asm/kvm_rme.h +2
-0int kvm_init_rme (void );u32 kvm_realm_ipa_limit (void );@@ -36,6 +44,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 realm *realm,u32 ia_bits,u32 start_level); +int kvm_create_rec (struct kvm_vcpu *vcpu); +void kvm_destroy_rec (struct kvm_vcpu *vcpu);#define RME_RTT_BLOCK_LEVEL 2#define RME_RTT_MAX_LEVEL 3arch/arm64/kvm/arm.c +1
-0@@ -373,6 +373,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES ); + vcpu->arch.rec.mpidr =INVALID_HWID ; vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;arch/arm64/kvm/reset.c +5
-0@@ -135,6 +135,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)) +return -EINVAL ; + +return kvm_create_rec (vcpu); }return -EINVAL ;arch/arm64/kvm/reset.c bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu) · line 156
Quoted 7 lines
arch/arm64/kvm/reset.c +4
-0@@ -145,6 +150,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) && +READ_ONCE (vcpu->kvm->arch.realm.state) ==REALM_STATE_ACTIVE )) +return false;That's why it is better to introduce the realm state in the previous patches so that people can really get the idea of the states at this stage.
Quoted 108 lines
arch/arm64/kvm/reset.c +2
-0+return true; }@@ -157,6 +167,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)if (sve_state)kvm_unshare_hyp (sve_state, sve_state +vcpu_sve_state_size (vcpu));kfree (sve_state); +kvm_destroy_rec (vcpu); }static void kvm_vcpu_reset_sve (struct kvm_vcpu *vcpu)arch/arm64/kvm/rme.c +88
-0@@ -514,6 +514,150 @@ void kvm_destroy_realm(struct kvm *kvm)kvm_free_stage2_pgd (&kvm->arch.mmu); } +static void free_rec_aux (struct page **aux_pages, +unsigned int num_aux) +{ +unsigned int i; + +for (i =0 ; i < num_aux; i++) { +phys_addr_t aux_page_phys =page_to_phys (aux_pages[i]); + +if (WARN_ON (rmi_granule_undelegate (aux_page_phys))) +continue ; + +__free_page (aux_pages[i]); + } +} + +static int alloc_rec_aux (struct page **aux_pages, +u64 *aux_phys_pages, +unsigned int num_aux) +{ +int ret; +unsigned int i; + +for (i =0 ; i < num_aux; i++) { +struct page *aux_page; +phys_addr_t aux_page_phys; + + aux_page =alloc_page (GFP_KERNEL ); +if (!aux_page) { + ret = -ENOMEM ; + goto out_err; + } + aux_page_phys =page_to_phys (aux_page); +if (rmi_granule_delegate (aux_page_phys)) { +__free_page (aux_page); + ret = -ENXIO ; + goto out_err; + } + aux_pages[i] = aux_page; + aux_phys_pages[i] = aux_page_phys; + } + +return 0 ; +out_err: +free_rec_aux (aux_pages, i); +return ret; +} + +int kvm_create_rec (struct kvm_vcpu *vcpu) +{ +struct user_pt_regs *vcpu_regs =vcpu_gp_regs (vcpu); +unsigned long mpidr =kvm_vcpu_get_mpidr_aff (vcpu); +struct realm *realm = &vcpu->kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +unsigned long rec_page_phys; +struct rec_params *params; +int r, i; + +if (kvm_realm_state (vcpu->kvm) !=REALM_STATE_NEW ) +return -ENOENT ; + +/* +* The RMM will report PSCI v1.0 to Realms and the KVM_ARM_VCPU_PSCI_0_2 +* flag covers v0.2 and onwards. +*/ +if (!test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features)) +return -EINVAL ; + +BUILD_BUG_ON (sizeof (*params) >PAGE_SIZE ); +BUILD_BUG_ON (sizeof (*rec->run) >PAGE_SIZE ); + + params = (struct rec_params *)get_zeroed_page (GFP_KERNEL ); + rec->rec_page = (void *)__get_free_page (GFP_KERNEL ); + rec->run = (void *)get_zeroed_page (GFP_KERNEL ); +if (!params || !rec->rec_page || !rec->run) { + r = -ENOMEM ; + goto out_free_pages; + } + +for (i =0 ; i <ARRAY_SIZE (params->gprs); i++) + params->gprs[i] = vcpu_regs->regs[i]; + + params->pc = vcpu_regs->pc; + +if (vcpu->vcpu_id ==0 ) + params->flags |=REC_PARAMS_FLAG_RUNNABLE ; + + rec_page_phys =virt_to_phys (rec->rec_page); +
Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 609
Quoted 5 lines
arch/arm64/kvm/rme.c +5
-0+if (rmi_granule_delegate (rec_page_phys)) { + r = -ENXIO ; + goto out_free_pages; + } +Wouldn't it be better to extend the
alloc_rec_aux()to allocate and delegate pages above? so that you can same some gfps andrmi_granuale_delegates().
I don't think it's really an improvement. There's only the one rmi_granule_delegate() call (for the REC page itself). The RecParams and RecRun pages are not delegated because they are shared with the host. It would also hide the structure setup within the new alloc_rec_aux_and_rec() function.
Quoted 42 lines
Quoted 42 lines
arch/arm64/kvm/rme.c +42
-0+ r =alloc_rec_aux (rec->aux_pages, params->aux, realm->num_aux); +if (r) + goto out_undelegate_rmm_rec; + + params->num_rec_aux = realm->num_aux; + params->mpidr = mpidr; + +if (rmi_rec_create (rec_page_phys, +virt_to_phys (realm->rd), +virt_to_phys (params))) { + r = -ENXIO ; + goto out_free_rec_aux; + } + + rec->mpidr = mpidr; + +free_page ((unsigned long )params); +return 0 ; + +out_free_rec_aux: +free_rec_aux (rec->aux_pages, realm->num_aux); +out_undelegate_rmm_rec: +if (WARN_ON (rmi_granule_undelegate (rec_page_phys))) + rec->rec_page =NULL ; +out_free_pages: +free_page ((unsigned long )rec->run); +free_page ((unsigned long )rec->rec_page); +free_page ((unsigned long )params); +return r; +} + +void kvm_destroy_rec (struct kvm_vcpu *vcpu) +{ +struct realm *realm = &vcpu->kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +unsigned long rec_page_phys; + +if (!vcpu_is_rec (vcpu)) +return ; + + rec_page_phys =virt_to_phys (rec->rec_page); +
Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 656
Quoted 5 lines
arch/arm64/kvm/rme.c +5
-0+if (WARN_ON (rmi_rec_destroy (rec_page_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rec_page_phys))) +return ; +The two returns above feels off. What is the reason to skip the below page undelegates?
The reason is the usual: if we fail to undelegate then the pages have to be leaked. I'll add some comments. However it does look like I've got the order wrong here, if the undelegate fails for rec_page_phys it's possible that we might still be able to free the rec_aux pages (although something has gone terribly wrong for that to be the case).
I'll change the order to:
/* If the REC destroy fails, leak all pages relating to the REC */ if (WARN_ON(rmi_rec_destroy(rec_page_phys)))
return;
free_rec_aux(rec->aux_pages, realm->num_aux);
/* If the undelegate fails then leak the REC page */ if (WARN_ON(rmi_granule_undelegate(rec_page_phys)))
return;
free_page((unsigned long)rec->rec_page);
If the rmi_rec_destroy() call has failed then the RMM should prevent the undelegate so there's little point trying.
Steve
Quoted 8 lines
Quoted 7 lines
arch/arm64/kvm/rme.c +4
-0+free_rec_aux (rec->aux_pages, realm->num_aux); +free_page ((unsigned long )rec->rec_page); +} +int kvm_init_realm_vm (struct kvm *kvm) {struct realm_params *params;
Re: [RFC PATCH 10/28] arm64: RME: Allocate/free RECs to match vCPUs
Quoted 10 lines · On Fri, 3 Mar 2023 14:05:02 +0000 Steven Price <[email protected]> wrote:
Quoted 9 lines · Zhi Wang
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:14 +0000 Steven Price <[email protected]> wrote:
The RMM maintains a data structure known as the Realm Execution Context (or REC). It is similar to struct
kvm_vcpuand 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.
Quoted 5 lines
Quoted 2 lines
It would be better to leave some pointers to the spec here. It really saves time for reviewers. /
Fair enough. I wasn't sure how often to repeat the link to the spec, but a few more times wouldn't hurt ;)
Based on my review experience, the right time would be when a new concept is first on the table.
For example, the concept REC appears in this patch series for the first time, it would be nice to have following things in the comments:
1) Basic summary of the concept. Several sentences help people to understand what it is, what it is used for, what/when it is required (mostly this would be helpful on denoting the interaction with existing flows), and then eventually how it will interact with the existing flows. It would be good enough for people who don't have time to dig the concept itself but want to review the interaction flow with the component they are familiar with or the area they are working on.
2) A simple sentence to show the spec name and chapter name would be good enough for people who would like to dig it. It is also a nice timing to educate people who are interested in the detail of the tech concept.
Quoted 244 lines
Quoted 24 lines
Quoted 24 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_emulate.h | 2 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/include/asm/kvm_rme.h | 10 ++ arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/reset.c | 11 ++ arch/arm64/kvm/rme.c | 144 +++++++++++++++++++++++++++ 6 files changed, 171 insertions(+)arch/arm64/include/asm/kvm_emulate.h +2
-0@@ -504,6 +504,8 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)static inline bool vcpu_is_rec (struct kvm_vcpu *vcpu) { +if (static_branch_unlikely (&kvm_rme_is_available)) +return vcpu->arch.rec.mpidr !=INVALID_HWID ;return false; }Quoted 15 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 510
Quoted 11 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -505,6 +505,9 @@ struct kvm_vcpu_arch {u64 last_steal;gpa_t base; } steal; + +/* Realm meta data */ +struct rec rec;I think the name of the data structure "rec" needs a prefix, it is too common and might conflict with the private data structures in the other modules. Maybe
rme_recorrealm_rec?struct
realm_recseems like a good choice. I agree 'rec' without context is somewhat ambiguous.Quoted 19 lines
Quoted 19 lines
arch/arm64/include/asm/kvm_host.h +0
-0};/* arch/arm64/include/asm/kvm_rme.h +1
-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> enum realm_state {@@ -29,6 +30,13 @@ struct realm {unsigned int ia_bits; };Quoted 10 lines
arch/arm64/include/asm/kvm_rme.h struct realm { · line 39
Quoted 7 lines
arch/arm64/include/asm/kvm_rme.h +7
-0+struct rec { +unsigned long mpidr; +void *rec_page; +struct page *aux_pages[REC_PARAMS_AUX_GRANULES ]; +struct rec_run *run; +}; +It is better to leave some comments for above members or pointers to the spec, that saves a lot of time for review.
Will add comments.
Quoted 160 lines
Quoted 41 lines
arch/arm64/include/asm/kvm_rme.h +2
-0int kvm_init_rme (void );u32 kvm_realm_ipa_limit (void );@@ -36,6 +44,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 realm *realm,u32 ia_bits,u32 start_level); +int kvm_create_rec (struct kvm_vcpu *vcpu); +void kvm_destroy_rec (struct kvm_vcpu *vcpu);#define RME_RTT_BLOCK_LEVEL 2#define RME_RTT_MAX_LEVEL 3arch/arm64/kvm/arm.c +1
-0@@ -373,6 +373,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES ); + vcpu->arch.rec.mpidr =INVALID_HWID ; vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;arch/arm64/kvm/reset.c +5
-0@@ -135,6 +135,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)) +return -EINVAL ; + +return kvm_create_rec (vcpu); }return -EINVAL ;arch/arm64/kvm/reset.c bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu) · line 156
Quoted 7 lines
arch/arm64/kvm/reset.c +4
-0@@ -145,6 +150,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) && +READ_ONCE (vcpu->kvm->arch.realm.state) ==REALM_STATE_ACTIVE )) +return false;That's why it is better to introduce the realm state in the previous patches so that people can really get the idea of the states at this stage.
Quoted 108 lines
arch/arm64/kvm/reset.c +2
-0+return true; }@@ -157,6 +167,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)if (sve_state)kvm_unshare_hyp (sve_state, sve_state +vcpu_sve_state_size (vcpu));kfree (sve_state); +kvm_destroy_rec (vcpu); }static void kvm_vcpu_reset_sve (struct kvm_vcpu *vcpu)arch/arm64/kvm/rme.c +88
-0@@ -514,6 +514,150 @@ void kvm_destroy_realm(struct kvm *kvm)kvm_free_stage2_pgd (&kvm->arch.mmu); } +static void free_rec_aux (struct page **aux_pages, +unsigned int num_aux) +{ +unsigned int i; + +for (i =0 ; i < num_aux; i++) { +phys_addr_t aux_page_phys =page_to_phys (aux_pages[i]); + +if (WARN_ON (rmi_granule_undelegate (aux_page_phys))) +continue ; + +__free_page (aux_pages[i]); + } +} + +static int alloc_rec_aux (struct page **aux_pages, +u64 *aux_phys_pages, +unsigned int num_aux) +{ +int ret; +unsigned int i; + +for (i =0 ; i < num_aux; i++) { +struct page *aux_page; +phys_addr_t aux_page_phys; + + aux_page =alloc_page (GFP_KERNEL ); +if (!aux_page) { + ret = -ENOMEM ; + goto out_err; + } + aux_page_phys =page_to_phys (aux_page); +if (rmi_granule_delegate (aux_page_phys)) { +__free_page (aux_page); + ret = -ENXIO ; + goto out_err; + } + aux_pages[i] = aux_page; + aux_phys_pages[i] = aux_page_phys; + } + +return 0 ; +out_err: +free_rec_aux (aux_pages, i); +return ret; +} + +int kvm_create_rec (struct kvm_vcpu *vcpu) +{ +struct user_pt_regs *vcpu_regs =vcpu_gp_regs (vcpu); +unsigned long mpidr =kvm_vcpu_get_mpidr_aff (vcpu); +struct realm *realm = &vcpu->kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +unsigned long rec_page_phys; +struct rec_params *params; +int r, i; + +if (kvm_realm_state (vcpu->kvm) !=REALM_STATE_NEW ) +return -ENOENT ; + +/* +* The RMM will report PSCI v1.0 to Realms and the KVM_ARM_VCPU_PSCI_0_2 +* flag covers v0.2 and onwards. +*/ +if (!test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features)) +return -EINVAL ; + +BUILD_BUG_ON (sizeof (*params) >PAGE_SIZE ); +BUILD_BUG_ON (sizeof (*rec->run) >PAGE_SIZE ); + + params = (struct rec_params *)get_zeroed_page (GFP_KERNEL ); + rec->rec_page = (void *)__get_free_page (GFP_KERNEL ); + rec->run = (void *)get_zeroed_page (GFP_KERNEL ); +if (!params || !rec->rec_page || !rec->run) { + r = -ENOMEM ; + goto out_free_pages; + } + +for (i =0 ; i <ARRAY_SIZE (params->gprs); i++) + params->gprs[i] = vcpu_regs->regs[i]; + + params->pc = vcpu_regs->pc; + +if (vcpu->vcpu_id ==0 ) + params->flags |=REC_PARAMS_FLAG_RUNNABLE ; + + rec_page_phys =virt_to_phys (rec->rec_page); +Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 609
Quoted 5 lines
arch/arm64/kvm/rme.c +5
-0+if (rmi_granule_delegate (rec_page_phys)) { + r = -ENXIO ; + goto out_free_pages; + } +Wouldn't it be better to extend the
alloc_rec_aux()to allocate and delegate pages above? so that you can same some gfps andrmi_granuale_delegates().
Quoted 6 lines
I don't think it's really an improvement. There's only the one
rmi_granule_delegate()call (for the REC page itself). The RecParams and RecRun pages are not delegated because they are shared with the host. It would also hide the structure setup within the newalloc_rec_aux_and_rec()function.
It should make it clearer.
I was thinking if it would be nice to abstract alloc + delegated as a common function, then alloc_rec_aux() and kvm_realm_rec() can be its common user.
Quoted 85 lines
Quoted 42 lines
Quoted 42 lines
arch/arm64/kvm/rme.c +42
-0+ r =alloc_rec_aux (rec->aux_pages, params->aux, realm->num_aux); +if (r) + goto out_undelegate_rmm_rec; + + params->num_rec_aux = realm->num_aux; + params->mpidr = mpidr; + +if (rmi_rec_create (rec_page_phys, +virt_to_phys (realm->rd), +virt_to_phys (params))) { + r = -ENXIO ; + goto out_free_rec_aux; + } + + rec->mpidr = mpidr; + +free_page ((unsigned long )params); +return 0 ; + +out_free_rec_aux: +free_rec_aux (rec->aux_pages, realm->num_aux); +out_undelegate_rmm_rec: +if (WARN_ON (rmi_granule_undelegate (rec_page_phys))) + rec->rec_page =NULL ; +out_free_pages: +free_page ((unsigned long )rec->run); +free_page ((unsigned long )rec->rec_page); +free_page ((unsigned long )params); +return r; +} + +void kvm_destroy_rec (struct kvm_vcpu *vcpu) +{ +struct realm *realm = &vcpu->kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +unsigned long rec_page_phys; + +if (!vcpu_is_rec (vcpu)) +return ; + + rec_page_phys =virt_to_phys (rec->rec_page); +Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_destroy_realm(struct kvm *kvm) · line 656
Quoted 5 lines
arch/arm64/kvm/rme.c +5
-0+if (WARN_ON (rmi_rec_destroy (rec_page_phys))) +return ; +if (WARN_ON (rmi_granule_undelegate (rec_page_phys))) +return ; +The two returns above feels off. What is the reason to skip the below page undelegates?
The reason is the usual: if we fail to undelegate then the pages have to be leaked. I'll add some comments. However it does look like I've got the order wrong here, if the undelegate fails for
rec_page_physit's possible that we might still be able to free therec_auxpages (although something has gone terribly wrong for that to be the case).I'll change the order to:
/* If the REC destroy fails, leak all pages relating to the REC */ if (WARN_ON(rmi_rec_destroy(rec_page_phys)))
return;free_rec_aux(rec->aux_pages, realm->num_aux);/* If the undelegate fails then leak the REC page */ if (WARN_ON(rmi_granule_undelegate(rec_page_phys)))
return;free_page((unsigned long)rec->rec_page);If the
rmi_rec_destroy()call has failed then the RMM should prevent the undelegate so there's little point trying.Steve
Quoted 8 lines
Quoted 7 lines
arch/arm64/kvm/rme.c +4
-0+free_rec_aux (rec->aux_pages, realm->num_aux); +free_page ((unsigned long )rec->rec_page); +} +int kvm_init_realm_vm (struct kvm *kvm) {struct realm_params *params;
RFC PATCH 11/28 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.
--- arch/arm64/kvm/arm.c | 15 +++++++++++--- arch/arm64/kvm/vgic/vgic-v3.c | 9 +++++++-- arch/arm64/kvm/vgic/vgic.c | 37 +++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 7 deletions(-)
arch/arm64/kvm/arm.c +12 -3
@@ -475,17 +475,22 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)void kvm_arch_vcpu_put (struct kvm_vcpu *vcpu) { +kvm_timer_vcpu_put (vcpu); +kvm_vgic_put (vcpu); + + vcpu->cpu =-1 ; + +if (vcpu_is_rec (vcpu)) +return ; +kvm_arch_vcpu_put_debug_state_flags (vcpu);kvm_arch_vcpu_put_fp (vcpu);if (has_vhe ())kvm_vcpu_put_sysregs_vhe (vcpu); -kvm_timer_vcpu_put (vcpu); -kvm_vgic_put (vcpu);kvm_vcpu_pmu_restore_host (vcpu);kvm_arm_vmid_clear_active ();vcpu_clear_on_unsupported_cpu (vcpu); - vcpu->cpu =-1 ; }void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu)
@@ -623,6 +628,10 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) }if (!irqchip_in_kernel (kvm)) { +/* Userspace irqchip not yet supported with Realms */ +if (kvm_is_realm (vcpu->kvm)) +return -EOPNOTSUPP ; +/* * Tell the rest of the code that there are userspace irqchip * VMs in the wild.
arch/arm64/kvm/vgic/vgic-v3.c +7 -2
@@ -6,9 +6,11 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <kvm/arm_vgic.h> +#include <asm/kvm_emulate.h> #include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> #include <asm/kvm_asm.h> +#include <asm/rmi_smc.h> #include "vgic.h"
@@ -669,7 +671,8 @@ int vgic_v3_probe(const struct gic_kvm_info *info) (unsigned long long)info->vcpu.start); }else if (kvm_get_mode () !=KVM_MODE_PROTECTED ) { kvm_vgic_global_state.vcpu_base = info->vcpu.start; - kvm_vgic_global_state.can_emulate_gicv2 = true; +if (!static_branch_unlikely (&kvm_rme_is_available)) + kvm_vgic_global_state.can_emulate_gicv2 = true; ret =kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V2 );if (ret) {kvm_err ("Cannot register GICv2 KVM device.\n" );
@@ -744,7 +747,9 @@ void vgic_v3_vmcr_sync(struct kvm_vcpu *vcpu) {struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; -if (likely (cpu_if->vgic_sre)) +if (vcpu_is_rec (vcpu)) + cpu_if->vgic_vmcr = vcpu->arch.rec.run->exit.gicv3_vmcr; +else if (likely (cpu_if->vgic_sre)) cpu_if->vgic_vmcr =kvm_call_hyp_ret (__vgic_v3_read_vmcr); }
arch/arm64/kvm/vgic/vgic.c +35 -2
@@ -10,7 +10,9 @@#include <linux/list_sort.h> #include <linux/nospec.h> +#include <asm/kvm_emulate.h> #include <asm/kvm_hyp.h> +#include <asm/rmi_smc.h> #include "vgic.h"
@@ -848,10 +850,23 @@ static inline bool can_access_vgic_from_kernel(void)return !static_branch_unlikely (&kvm_vgic_global_state.gicv3_cpuif) ||has_vhe (); } +static inline void vgic_rmm_save_state (struct kvm_vcpu *vcpu) +{ +struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; +int i; + +for (i =0 ; i < kvm_vgic_global_state.nr_lr; i++) { + cpu_if->vgic_lr[i] = vcpu->arch.rec.run->exit.gicv3_lrs[i]; + vcpu->arch.rec.run->entry.gicv3_lrs[i] =0 ; + } +} +static inline void vgic_save_state (struct kvm_vcpu *vcpu) {if (!static_branch_unlikely (&kvm_vgic_global_state.gicv3_cpuif))vgic_v2_save_state (vcpu); +else if (vcpu_is_rec (vcpu)) +vgic_rmm_save_state (vcpu);else __vgic_v3_save_state (&vcpu->arch.vgic_cpu.vgic_v3); }
@@ -878,10 +893,28 @@ void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)vgic_prune_ap_list (vcpu); } +static inline void vgic_rmm_restore_state (struct kvm_vcpu *vcpu) +{ +struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; +int i; + +for (i =0 ; i < kvm_vgic_global_state.nr_lr; i++) { + vcpu->arch.rec.run->entry.gicv3_lrs[i] = cpu_if->vgic_lr[i]; +/* +* Also populate the rec.run->exit copies so that a late +* decision to back out from entering the realm doesn't cause +* the state to be lost +*/ + vcpu->arch.rec.run->exit.gicv3_lrs[i] = cpu_if->vgic_lr[i]; + } +} +static inline void vgic_restore_state (struct kvm_vcpu *vcpu) {if (!static_branch_unlikely (&kvm_vgic_global_state.gicv3_cpuif))vgic_v2_restore_state (vcpu); +else if (vcpu_is_rec (vcpu)) +vgic_rmm_restore_state (vcpu);else __vgic_v3_restore_state (&vcpu->arch.vgic_cpu.vgic_v3); }
@@ -922,7 +955,7 @@ void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)void kvm_vgic_load (struct kvm_vcpu *vcpu) { -if (unlikely (!vgic_initialized (vcpu->kvm))) +if (unlikely (!vgic_initialized (vcpu->kvm)) ||vcpu_is_rec (vcpu))return ;if (kvm_vgic_global_state.type ==VGIC_V2 )
@@ -933,7 +966,7 @@ void kvm_vgic_load(struct kvm_vcpu *vcpu)void kvm_vgic_put (struct kvm_vcpu *vcpu) { -if (unlikely (!vgic_initialized (vcpu->kvm))) +if (unlikely (!vgic_initialized (vcpu->kvm)) ||vcpu_is_rec (vcpu))return ;if (kvm_vgic_global_state.type ==VGIC_V2 )
Signature · 1 line
2.34.1
RFC PATCH 12/28 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.
--- arch/arm64/kvm/arch_timer.c | 53 ++++++++++++++++++++++++++++++++---- include/kvm/arm_arch_timer.h | 2 ++ 2 files changed, 49 insertions(+), 6 deletions(-)
arch/arm64/kvm/arch_timer.c +47 -6
@@ -130,6 +130,11 @@ 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 ; + } +switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER :__vcpu_sys_reg (vcpu,CNTVOFF_EL2 ) = offset;
@@ -411,6 +416,21 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, } } +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_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) {
@@ -621,6 +641,11 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)if (unlikely (!timer->enabled))return ; +kvm_timer_unblocking (vcpu); + +if (vcpu_is_rec (vcpu)) +return ; +get_timer_map (vcpu, &map);if (static_branch_likely (&has_gic_active_state)) {
@@ -633,8 +658,6 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)set_cntvoff (timer_get_offset (map.direct_vtimer)); -kvm_timer_unblocking (vcpu); -timer_restore_state (map.direct_vtimer);if (map.direct_ptimer)timer_restore_state (map.direct_ptimer);
@@ -668,6 +691,9 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)if (unlikely (!timer->enabled))return ; +if (vcpu_is_rec (vcpu)) + goto out; +get_timer_map (vcpu, &map);timer_save_state (map.direct_vtimer);
@@ -686,9 +712,6 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)if (map.emul_ptimer)soft_timer_cancel (&map.emul_ptimer->hrtimer); -if (kvm_vcpu_is_blocking (vcpu)) -kvm_timer_blocking (vcpu); -/* * The kernel may decide to run userspace after calling vcpu_put, so * we reset cntvoff to 0 to ensure a consistent read between user
@@ -697,6 +720,11 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)* virtual offset of zero, so no need to zero CNTVOFF_EL2 register. */ set_cntvoff (0 ); + +out: +if (kvm_vcpu_is_blocking (vcpu)) +kvm_timer_blocking (vcpu); + }/*
@@ -785,12 +813,18 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer =vcpu_timer (vcpu);struct arch_timer_context *vtimer =vcpu_vtimer (vcpu);struct arch_timer_context *ptimer =vcpu_ptimer (vcpu); +u64 cntvoff; vtimer->vcpu = vcpu; ptimer->vcpu = vcpu; +if (kvm_is_realm (vcpu->kvm)) + cntvoff =0 ; +else + cntvoff =kvm_phys_timer_read (); +/* Synchronize cntvoff across all vtimers of a VM. */ -update_vtimer_cntvoff (vcpu,kvm_phys_timer_read ()); +update_vtimer_cntvoff (vcpu, cntvoff);timer_set_offset (ptimer,0 );hrtimer_init (&timer->bg_timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS_HARD );
@@ -1265,6 +1299,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,
include/kvm/arm_arch_timer.h +2 -0
@@ -76,6 +76,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.34.1
Re: [RFC PATCH 12/28] KVM: arm64: Support timers in realm RECs
Quoted 26 lines · Steven Price
The RMM keeps track of the timer while the realm REC is running, but on exit to the normal world KVM is responsible for handling the timers.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/arch_timer.c | 53 ++++++++++++++++++++++++++++++++---- include/kvm/arm_arch_timer.h | 2 ++ 2 files changed, 49 insertions(+), 6 deletions(-)arch/arm64/kvm/arch_timer.c +5
-0@@ -130,6 +130,11 @@ 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 ; + } +switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER :__vcpu_sys_reg (vcpu,CNTVOFF_EL2 ) = offset;
arch/arm64/kvm/arch_timer.c static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, · line 424
Quoted 9 lines
arch/arm64/kvm/arch_timer.c +6
-0@@ -411,6 +416,21 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, } } +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_TIMERS ; i++) {
Do we required to check for all timers, is realm/rmm uses hyp timers?
Quoted 13 lines
arch/arm64/kvm/arch_timer.c +9
-0+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) {
arch/arm64/kvm/arch_timer.c void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) · line 648
Quoted 8 lines
arch/arm64/kvm/arch_timer.c +5
-0@@ -621,6 +641,11 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)if (unlikely (!timer->enabled))return ; +kvm_timer_unblocking (vcpu); + +if (vcpu_is_rec (vcpu)) +return ; +
For realm, timer->enabled is not set, load returns before this check.
Quoted 83 lines
arch/arm64/kvm/arch_timer.c +22
-6get_timer_map (vcpu, &map);if (static_branch_likely (&has_gic_active_state)) {@@ -633,8 +658,6 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)set_cntvoff (timer_get_offset (map.direct_vtimer)); -kvm_timer_unblocking (vcpu); -timer_restore_state (map.direct_vtimer);if (map.direct_ptimer)timer_restore_state (map.direct_ptimer);@@ -668,6 +691,9 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)if (unlikely (!timer->enabled))return ; +if (vcpu_is_rec (vcpu)) + goto out; +get_timer_map (vcpu, &map);timer_save_state (map.direct_vtimer);@@ -686,9 +712,6 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)if (map.emul_ptimer)soft_timer_cancel (&map.emul_ptimer->hrtimer); -if (kvm_vcpu_is_blocking (vcpu)) -kvm_timer_blocking (vcpu); -/* * The kernel may decide to run userspace after calling vcpu_put, so * we reset cntvoff to 0 to ensure a consistent read between user @@ -697,6 +720,11 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)* virtual offset of zero, so no need to zero CNTVOFF_EL2 register. */ set_cntvoff (0 ); + +out: +if (kvm_vcpu_is_blocking (vcpu)) +kvm_timer_blocking (vcpu); + }/* @@ -785,12 +813,18 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer =vcpu_timer (vcpu);struct arch_timer_context *vtimer =vcpu_vtimer (vcpu);struct arch_timer_context *ptimer =vcpu_ptimer (vcpu); +u64 cntvoff; vtimer->vcpu = vcpu; ptimer->vcpu = vcpu; +if (kvm_is_realm (vcpu->kvm)) + cntvoff =0 ; +else + cntvoff =kvm_phys_timer_read (); +/* Synchronize cntvoff across all vtimers of a VM. */ -update_vtimer_cntvoff (vcpu,kvm_phys_timer_read ()); +update_vtimer_cntvoff (vcpu, cntvoff);timer_set_offset (ptimer,0 );hrtimer_init (&timer->bg_timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS_HARD );@@ -1265,6 +1299,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,include/kvm/arm_arch_timer.h +0
-0
include/kvm/arm_arch_timer.h int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); · line 83
Quoted 8 lines
include/kvm/arm_arch_timer.h +2
-0@@ -76,6 +76,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);
Thanks,
Ganapat
Re: [RFC PATCH 12/28] KVM: arm64: Support timers in realm RECs
Quoted 32 lines · Ganapatrao Kulkarni
Quoted 29 lines · Steven Price
The RMM keeps track of the timer while the realm REC is running, but on exit to the normal world KVM is responsible for handling the timers.
Signed-off-by Steven Price <[email protected]>---
arch/arm64/kvm/arch_timer.c | 53 ++++++++++++++++++++++++++++++++----include/kvm/arm_arch_timer.h| 2 ++2 files changed, 49 insertions(+), 6 deletions(-)arch/arm64/kvm/arch_timer.c +4
-0@@ -130,6 +130,11 @@ 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; + } + switch(arch_timer_ctx_index(ctxt)) {case
TIMER_VTIMER:
__vcpu_sys_reg(vcpu, CNTVOFF_EL2)= offset;arch/arm64/kvm/arch_timer.c +0
-0@@ -411,6 +416,21 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, } }
Quoted 8 lines
arch/arm64/kvm/arch_timer.c static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, · line 424
Quoted 6 lines
arch/arm64/kvm/arch_timer.c +5
-0+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_TIMERS; i++) {Do we required to check for all timers, is realm/rmm uses hyp timers?
Good point, the realm guest can't use the hyp timers, so this should be NR_KVM_EL0_TIMERS. The hyp timers are used by the host to interrupt the guest execution. I think this code was written before NV support added the extra timers.
Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/arch_timer.c +9
-0+ 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){
arch/arm64/kvm/arch_timer.c +0
-0@@ -621,6 +641,11 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) if (unlikely(!timer->enabled))
Quoted 8 lines
arch/arm64/kvm/arch_timer.c void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) · line 648
Quoted 6 lines
arch/arm64/kvm/arch_timer.c +4
-0return; + kvm_timer_unblocking(vcpu); + + if (vcpu_is_rec(vcpu)) + return; +For realm,
timer->enabledis not set, load returns before this check.
True, this can be simplified. Thanks.
Steve
Quoted 85 lines
Quoted 74 lines
arch/arm64/kvm/arch_timer.c +2
-3get_timer_map(vcpu, &map); if (static_branch_likely(&has_gic_active_state)) {@@ -633,8 +658,6 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) set_cntvoff(timer_get_offset(map.direct_vtimer)); - kvm_timer_unblocking(vcpu); - timer_restore_state(map.direct_vtimer); if (map.direct_ptimer) timer_restore_state(map.direct_ptimer);@@ -668,6 +691,9 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) if (unlikely(!timer->enabled)) return; + if (vcpu_is_rec(vcpu)) + goto out; + get_timer_map(vcpu, &map); timer_save_state(map.direct_vtimer);@@ -686,9 +712,6 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) if (map.emul_ptimer) soft_timer_cancel(&map.emul_ptimer->hrtimer); - if (kvm_vcpu_is_blocking(vcpu)) - kvm_timer_blocking(vcpu); - /** The kernel may decide to run userspace after calling
vcpu_put, so
* we reset cntvoff to 0 to ensure a consistent read between userarch/arm64/kvm/arch_timer.c +11
-1@@ -697,6 +720,11 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)* virtual offset of zero, so no need to zero CNTVOFF_EL2 register. */ set_cntvoff(0); + +out: + if (kvm_vcpu_is_blocking(vcpu)) + kvm_timer_blocking(vcpu); + } /*@@ -785,12 +813,18 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) struct arch_timer_cpu *timer = vcpu_timer(vcpu); struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); + u64 cntvoff; vtimer->vcpu = vcpu; ptimer->vcpu = vcpu; + if (kvm_is_realm(vcpu->kvm)) + cntvoff = 0; + else + cntvoff = kvm_phys_timer_read(); + /* Synchronize cntvoff across all vtimers of a VM. */ - update_vtimer_cntvoff(vcpu, kvm_phys_timer_read()); + update_vtimer_cntvoff(vcpu, cntvoff); timer_set_offset(ptimer, 0); hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC,HRTIMER_MODE_ABS_HARD );@@ -1265,6 +1299,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,include/kvm/arm_arch_timer.h +0
-0@@ -76,6 +76,8 @@ int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);Quoted 8 lines
int
kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
intkvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
+voidkvm_realm_timers_update(struct kvm_vcpu *vcpu); +
u64kvm_phys_timer_read(void);
voidkvm_timer_vcpu_load(struct kvm_vcpu *vcpu);Thanks,
Ganapat
RFC PATCH 13/28 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.
--- arch/arm64/include/asm/kvm_rme.h | 4 + arch/arm64/kvm/rme.c | 288 +++++++++++++++++++++++++++++++ 2 files changed, 292 insertions(+)
arch/arm64/include/asm/kvm_rme.h +4 -0
@@ -47,6 +47,10 @@ void kvm_realm_destroy_rtts(struct realm *realm, u32 ia_bits, u32 start_level);int kvm_create_rec (struct kvm_vcpu *vcpu);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +int realm_set_ipa_state (struct kvm_vcpu *vcpu, +unsigned long addr,unsigned long end, +unsigned long ripas); +#define RME_RTT_BLOCK_LEVEL 2#define RME_RTT_MAX_LEVEL 3
arch/arm64/kvm/rme.c +288 -0
312 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH 13/28] arm64: RME: Allow VMM to set RIPAS
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:17 +0000 Steven Price <[email protected]> wrote:
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.
The above is just the purpose of this patch. It would be better to have one more paragraph to describe what this patch does (building RTT and set IPA state in the RTT) and explain something might confuse people, for example the spare page.
The spare page is really confusing. When reading __alloc_delegated_page() , it looks like a mechanism to cache a delegated page for the realm. But later in the teardown path, it looks like a workaround. What if the allocation of the spare page failed in the RTT tear down path?
I understand this must be a temporary solution. It would be really nice to have a big picture or some basic introduction to the future plan.
Quoted 341 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 4 + arch/arm64/kvm/rme.c | 288 +++++++++++++++++++++++++++++++ 2 files changed, 292 insertions(+)arch/arm64/include/asm/kvm_rme.h +4
-0@@ -47,6 +47,10 @@ void kvm_realm_destroy_rtts(struct realm *realm, u32 ia_bits, u32 start_level);int kvm_create_rec (struct kvm_vcpu *vcpu);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +int realm_set_ipa_state (struct kvm_vcpu *vcpu, +unsigned long addr,unsigned long end, +unsigned long ripas); +#define RME_RTT_BLOCK_LEVEL 2#define RME_RTT_MAX_LEVEL 3arch/arm64/kvm/rme.c +288
-0@@ -73,6 +73,58 @@ static int rmi_check_version(void)return 0 ; } +static phys_addr_t __alloc_delegated_page (struct realm *realm, +struct kvm_mmu_memory_cache *mc,gfp_t flags) +{ +phys_addr_t phys =PHYS_ADDR_MAX ; +void *virt; + +if (realm->spare_page !=PHYS_ADDR_MAX ) { +swap (realm->spare_page, phys); + goto out; + } + +if (mc) + virt =kvm_mmu_memory_cache_alloc (mc); +else + virt = (void *)__get_free_page (flags); + +if (!virt) + goto out; + + phys =virt_to_phys (virt); + +if (rmi_granule_delegate (phys)) { +free_page ((unsigned long )virt); + + phys =PHYS_ADDR_MAX ; + } + +out: +return phys; +} + +static phys_addr_t alloc_delegated_page (struct realm *realm, +struct kvm_mmu_memory_cache *mc) +{ +return __alloc_delegated_page (realm, mc,GFP_KERNEL ); +} + +static void free_delegated_page (struct realm *realm,phys_addr_t phys) +{ +if (realm->spare_page ==PHYS_ADDR_MAX ) { + realm->spare_page = phys; +return ; + } + +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Undelegate failed: leak the page */ +return ; + } + +free_page ((unsigned long )phys_to_virt (phys)); +} + static void realm_destroy_undelegate_range(struct realm *realm, unsigned long ipa, unsigned long addr,@@ -220,6 +272,30 @@ static int realm_rtt_create(struct realm *realm,return rmi_rtt_create (phys,virt_to_phys (realm->rd), addr, level); } +static int realm_create_rtt_levels (struct realm *realm, +unsigned long ipa, +int level, +int max_level, +struct kvm_mmu_memory_cache *mc) +{ +if (WARN_ON (level == max_level)) +return 0 ; + +while (level++ < max_level) { +phys_addr_t rtt =alloc_delegated_page (realm, mc); + +if (rtt ==PHYS_ADDR_MAX ) +return -ENOMEM ; + +if (realm_rtt_create (realm, ipa, level, rtt)) { +free_delegated_page (realm, rtt); +return -ENXIO ; + } + } + +return 0 ; +} +static int realm_tear_down_rtt_range (struct realm *realm,int level,unsigned long start,unsigned long end) {@@ -309,6 +385,206 @@ void kvm_realm_destroy_rtts(struct realm *realm, u32 ia_bits, u32 start_level)realm_tear_down_rtt_range (realm, start_level,0 , (1UL << ia_bits)); } +void kvm_realm_unmap_range (struct kvm *kvm,unsigned long ipa,u64 size) +{ +u32 ia_bits = kvm->arch.mmu.pgt->ia_bits; +u32 start_level = kvm->arch.mmu.pgt->start_level; +unsigned long end = ipa + size; +struct realm *realm = &kvm->arch.realm; +phys_addr_t tmp_rtt =PHYS_ADDR_MAX ; + +if (end > (1UL << ia_bits)) + end =1UL << ia_bits; +/* +* Make sure we have a spare delegated page for tearing down the +* block mappings. We must use Atomic allocations as we are called +* with kvm->mmu_lock held. +*/ +if (realm->spare_page ==PHYS_ADDR_MAX ) { + tmp_rtt =__alloc_delegated_page (realm,NULL ,GFP_ATOMIC ); +/* +* We don't have to check the status here, as we may not +* have a block level mapping. Delay any error to the point +* where we need it. +*/ + realm->spare_page = tmp_rtt; + } + +realm_tear_down_rtt_range (&kvm->arch.realm, start_level, ipa, end); + +/* Free up the atomic page, if there were any */ +if (tmp_rtt !=PHYS_ADDR_MAX ) { +free_delegated_page (realm, tmp_rtt); +/* +* Update the spare_page after we have freed the +* above page to make sure it doesn't get cached +* in spare_page. +* We should re-write this part and always have +* a dedicated page for handling block mappings. +*/ + realm->spare_page =PHYS_ADDR_MAX ; + } +} + +static int set_ipa_state (struct kvm_vcpu *vcpu, +unsigned long ipa, +unsigned long end, +int level, +unsigned long ripas) +{ +struct kvm *kvm = vcpu->kvm; +struct realm *realm = &kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +phys_addr_t rd_phys =virt_to_phys (realm->rd); +phys_addr_t rec_phys =virt_to_phys (rec->rec_page); +unsigned long map_size =rme_rtt_level_mapsize (level); +int ret; + +while (ipa < end) { + ret =rmi_rtt_set_ripas (rd_phys, rec_phys, ipa, level, ripas); + +if (!ret) { +if (!ripas) +kvm_realm_unmap_range (kvm, ipa, map_size); + }else if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +int walk_level =RMI_RETURN_INDEX (ret); + +if (walk_level < level) { + ret =realm_create_rtt_levels (realm, ipa, + walk_level, + level,NULL ); +if (ret) +return ret; +continue ; + } + +if (WARN_ON (level >=RME_RTT_MAX_LEVEL )) +return -EINVAL ; + +/* Recurse one level lower */ + ret =set_ipa_state (vcpu, ipa, ipa + map_size, + level +1 , ripas); +if (ret) +return ret; + }else { +WARN (1 ,"Unexpected error in %s: %#x\n" , __func__, + ret); +return -EINVAL ; + } + ipa += map_size; + } + +return 0 ; +} + +static int realm_init_ipa_state (struct realm *realm, +unsigned long ipa, +unsigned long end, +int level) +{ +unsigned long map_size =rme_rtt_level_mapsize (level); +phys_addr_t rd_phys =virt_to_phys (realm->rd); +int ret; + +while (ipa < end) { + ret =rmi_rtt_init_ripas (rd_phys, ipa, level); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +int cur_level =RMI_RETURN_INDEX (ret); + +if (cur_level < level) { + ret =realm_create_rtt_levels (realm, ipa, + cur_level, + level,NULL ); +if (ret) +return ret; +/* Retry with the RTT levels in place */ +continue ; + } + +/* There's an entry at a lower level, recurse */ +if (WARN_ON (level >=RME_RTT_MAX_LEVEL )) +return -EINVAL ; + +realm_init_ipa_state (realm, ipa, ipa + map_size, + level +1 ); + }else if (WARN_ON (ret)) { +return -ENXIO ; + } + + ipa += map_size; + } + +return 0 ; +} + +static int find_map_level (struct kvm *kvm,unsigned long start,unsigned long end) +{ +int level =RME_RTT_MAX_LEVEL ; + +while (level >get_start_level (kvm) +1 ) { +unsigned long map_size =rme_rtt_level_mapsize (level -1 ); + +if (!IS_ALIGNED (start, map_size) || + (start + map_size) > end) +break ; + + level--; + } + +return level; +} + +int realm_set_ipa_state (struct kvm_vcpu *vcpu, +unsigned long addr,unsigned long end, +unsigned long ripas) +{ +int ret =0 ; + +while (addr < end) { +int level =find_map_level (vcpu->kvm, addr, end); +unsigned long map_size =rme_rtt_level_mapsize (level); + + ret =set_ipa_state (vcpu, addr, addr + map_size, level, ripas); +if (ret) +break ; + + addr += map_size; + } + +return ret; +} + +static int kvm_init_ipa_range_realm (struct kvm *kvm, +struct kvm_cap_arm_rme_init_ipa_args *args) +{ +int ret =0 ; +gpa_t addr, end; +struct realm *realm = &kvm->arch.realm; + + addr = args->init_ipa_base; + end = addr + args->init_ipa_size; + +if (end < addr) +return -EINVAL ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EBUSY ; + +while (addr < end) { +int level =find_map_level (kvm, addr, end); +unsigned long map_size =rme_rtt_level_mapsize (level); + + ret =realm_init_ipa_state (realm, addr, addr + map_size, level); +if (ret) +break ; + + addr += map_size; + } + +return ret; +} +/* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK (rme_vmid_lock);static unsigned long *rme_vmid_bitmap;@@ -460,6 +736,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) r =kvm_create_realm (kvm);break ; +case KVM_CAP_ARM_RME_INIT_IPA_REALM : { +struct kvm_cap_arm_rme_init_ipa_args args; +void __user *argp =u64_to_user_ptr (cap->args[1 ]); + +if (copy_from_user (&args, argp,sizeof (args))) { + r = -EFAULT ; +break ; + } + + r =kvm_init_ipa_range_realm (kvm, &args); +break ; + }default : r = -EINVAL ;break ;
Re: [RFC PATCH 13/28] arm64: RME: Allow VMM to set RIPAS
Quoted 9 lines · Zhi Wang
Quoted 5 lines · On Fri, 27 Jan 2023 11:29:17 +0000 Steven Price <[email protected]> wrote:
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.
Quoted 4 lines
The above is just the purpose of this patch. It would be better to have one more paragraph to describe what this patch does (building RTT and set IPA state in the RTT) and explain something might confuse people, for example the spare page.
I'll improve the commit message.
Quoted 4 lines
The spare page is really confusing. When reading
__alloc_delegated_page(), it looks like a mechanism to cache a delegated page for the realm. But later in the teardown path, it looks like a workaround. What if the allocation of the spare page failed in the RTT tear down path?
Yeah the spare_page is a bit messy. Ultimately the idea is that rather than having to delegate a page to the RMM temporarily just for breaking up a block mapping which is going to be freed, we keep one spare for the purpose. This also reduces the chance of an allocation failure while trying to free memory.
One area of confusion, and something that might be worth revisiting, is that the spare_page is also used opportunistically in realm_create_rtt_levels(). Again this makes sense in the case where a temporary page is needed when creating a block mapping, but the code doesn't distinguish between this and just creating RTTs for normal mappings.
This leads to the rather unfortunate situation that it's not actually possible to rely on there being a spare_page and therefore this is pre-populated in kvm_realm_unmap_range(), but with a possibility that allocation failure could occur resulting in the function failing (which is 'handled' by a WARN_ON).
Quoted 2 lines
I understand this must be a temporary solution. It would be really nice to have a big picture or some basic introduction to the future plan.
Sadly I don't currently have a "big picture" plan at the moment. I am quite tempted to split spare_page into two:
* A 'guaranteed' spare page purely for destroying block mappings. This
would be allocated when the realm is created and only used for the purpose of tearing down mappings.
* A temporary spare page used as a minor optimisation during block
mapping creation - rather than immediately freeing the page back when folding we can hold on to it with the assumption that it's likely to be useful for creating further mappings in the same call.
However, to be honest, this is all a bit academic because as it stands block mappings can't really be used. But when we switch over to using the memfd approach hopefully huge pages can be translated to block mappings.
Steve
Quoted 342 lines
Quoted 341 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_rme.h | 4 + arch/arm64/kvm/rme.c | 288 +++++++++++++++++++++++++++++++ 2 files changed, 292 insertions(+)arch/arm64/include/asm/kvm_rme.h +4
-0@@ -47,6 +47,10 @@ void kvm_realm_destroy_rtts(struct realm *realm, u32 ia_bits, u32 start_level);int kvm_create_rec (struct kvm_vcpu *vcpu);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +int realm_set_ipa_state (struct kvm_vcpu *vcpu, +unsigned long addr,unsigned long end, +unsigned long ripas); +#define RME_RTT_BLOCK_LEVEL 2#define RME_RTT_MAX_LEVEL 3arch/arm64/kvm/rme.c +288
-0@@ -73,6 +73,58 @@ static int rmi_check_version(void)return 0 ; } +static phys_addr_t __alloc_delegated_page (struct realm *realm, +struct kvm_mmu_memory_cache *mc,gfp_t flags) +{ +phys_addr_t phys =PHYS_ADDR_MAX ; +void *virt; + +if (realm->spare_page !=PHYS_ADDR_MAX ) { +swap (realm->spare_page, phys); + goto out; + } + +if (mc) + virt =kvm_mmu_memory_cache_alloc (mc); +else + virt = (void *)__get_free_page (flags); + +if (!virt) + goto out; + + phys =virt_to_phys (virt); + +if (rmi_granule_delegate (phys)) { +free_page ((unsigned long )virt); + + phys =PHYS_ADDR_MAX ; + } + +out: +return phys; +} + +static phys_addr_t alloc_delegated_page (struct realm *realm, +struct kvm_mmu_memory_cache *mc) +{ +return __alloc_delegated_page (realm, mc,GFP_KERNEL ); +} + +static void free_delegated_page (struct realm *realm,phys_addr_t phys) +{ +if (realm->spare_page ==PHYS_ADDR_MAX ) { + realm->spare_page = phys; +return ; + } + +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Undelegate failed: leak the page */ +return ; + } + +free_page ((unsigned long )phys_to_virt (phys)); +} + static void realm_destroy_undelegate_range(struct realm *realm, unsigned long ipa, unsigned long addr,@@ -220,6 +272,30 @@ static int realm_rtt_create(struct realm *realm,return rmi_rtt_create (phys,virt_to_phys (realm->rd), addr, level); } +static int realm_create_rtt_levels (struct realm *realm, +unsigned long ipa, +int level, +int max_level, +struct kvm_mmu_memory_cache *mc) +{ +if (WARN_ON (level == max_level)) +return 0 ; + +while (level++ < max_level) { +phys_addr_t rtt =alloc_delegated_page (realm, mc); + +if (rtt ==PHYS_ADDR_MAX ) +return -ENOMEM ; + +if (realm_rtt_create (realm, ipa, level, rtt)) { +free_delegated_page (realm, rtt); +return -ENXIO ; + } + } + +return 0 ; +} +static int realm_tear_down_rtt_range (struct realm *realm,int level,unsigned long start,unsigned long end) {@@ -309,6 +385,206 @@ void kvm_realm_destroy_rtts(struct realm *realm, u32 ia_bits, u32 start_level)realm_tear_down_rtt_range (realm, start_level,0 , (1UL << ia_bits)); } +void kvm_realm_unmap_range (struct kvm *kvm,unsigned long ipa,u64 size) +{ +u32 ia_bits = kvm->arch.mmu.pgt->ia_bits; +u32 start_level = kvm->arch.mmu.pgt->start_level; +unsigned long end = ipa + size; +struct realm *realm = &kvm->arch.realm; +phys_addr_t tmp_rtt =PHYS_ADDR_MAX ; + +if (end > (1UL << ia_bits)) + end =1UL << ia_bits; +/* +* Make sure we have a spare delegated page for tearing down the +* block mappings. We must use Atomic allocations as we are called +* with kvm->mmu_lock held. +*/ +if (realm->spare_page ==PHYS_ADDR_MAX ) { + tmp_rtt =__alloc_delegated_page (realm,NULL ,GFP_ATOMIC ); +/* +* We don't have to check the status here, as we may not +* have a block level mapping. Delay any error to the point +* where we need it. +*/ + realm->spare_page = tmp_rtt; + } + +realm_tear_down_rtt_range (&kvm->arch.realm, start_level, ipa, end); + +/* Free up the atomic page, if there were any */ +if (tmp_rtt !=PHYS_ADDR_MAX ) { +free_delegated_page (realm, tmp_rtt); +/* +* Update the spare_page after we have freed the +* above page to make sure it doesn't get cached +* in spare_page. +* We should re-write this part and always have +* a dedicated page for handling block mappings. +*/ + realm->spare_page =PHYS_ADDR_MAX ; + } +} + +static int set_ipa_state (struct kvm_vcpu *vcpu, +unsigned long ipa, +unsigned long end, +int level, +unsigned long ripas) +{ +struct kvm *kvm = vcpu->kvm; +struct realm *realm = &kvm->arch.realm; +struct rec *rec = &vcpu->arch.rec; +phys_addr_t rd_phys =virt_to_phys (realm->rd); +phys_addr_t rec_phys =virt_to_phys (rec->rec_page); +unsigned long map_size =rme_rtt_level_mapsize (level); +int ret; + +while (ipa < end) { + ret =rmi_rtt_set_ripas (rd_phys, rec_phys, ipa, level, ripas); + +if (!ret) { +if (!ripas) +kvm_realm_unmap_range (kvm, ipa, map_size); + }else if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +int walk_level =RMI_RETURN_INDEX (ret); + +if (walk_level < level) { + ret =realm_create_rtt_levels (realm, ipa, + walk_level, + level,NULL ); +if (ret) +return ret; +continue ; + } + +if (WARN_ON (level >=RME_RTT_MAX_LEVEL )) +return -EINVAL ; + +/* Recurse one level lower */ + ret =set_ipa_state (vcpu, ipa, ipa + map_size, + level +1 , ripas); +if (ret) +return ret; + }else { +WARN (1 ,"Unexpected error in %s: %#x\n" , __func__, + ret); +return -EINVAL ; + } + ipa += map_size; + } + +return 0 ; +} + +static int realm_init_ipa_state (struct realm *realm, +unsigned long ipa, +unsigned long end, +int level) +{ +unsigned long map_size =rme_rtt_level_mapsize (level); +phys_addr_t rd_phys =virt_to_phys (realm->rd); +int ret; + +while (ipa < end) { + ret =rmi_rtt_init_ripas (rd_phys, ipa, level); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +int cur_level =RMI_RETURN_INDEX (ret); + +if (cur_level < level) { + ret =realm_create_rtt_levels (realm, ipa, + cur_level, + level,NULL ); +if (ret) +return ret; +/* Retry with the RTT levels in place */ +continue ; + } + +/* There's an entry at a lower level, recurse */ +if (WARN_ON (level >=RME_RTT_MAX_LEVEL )) +return -EINVAL ; + +realm_init_ipa_state (realm, ipa, ipa + map_size, + level +1 ); + }else if (WARN_ON (ret)) { +return -ENXIO ; + } + + ipa += map_size; + } + +return 0 ; +} + +static int find_map_level (struct kvm *kvm,unsigned long start,unsigned long end) +{ +int level =RME_RTT_MAX_LEVEL ; + +while (level >get_start_level (kvm) +1 ) { +unsigned long map_size =rme_rtt_level_mapsize (level -1 ); + +if (!IS_ALIGNED (start, map_size) || + (start + map_size) > end) +break ; + + level--; + } + +return level; +} + +int realm_set_ipa_state (struct kvm_vcpu *vcpu, +unsigned long addr,unsigned long end, +unsigned long ripas) +{ +int ret =0 ; + +while (addr < end) { +int level =find_map_level (vcpu->kvm, addr, end); +unsigned long map_size =rme_rtt_level_mapsize (level); + + ret =set_ipa_state (vcpu, addr, addr + map_size, level, ripas); +if (ret) +break ; + + addr += map_size; + } + +return ret; +} + +static int kvm_init_ipa_range_realm (struct kvm *kvm, +struct kvm_cap_arm_rme_init_ipa_args *args) +{ +int ret =0 ; +gpa_t addr, end; +struct realm *realm = &kvm->arch.realm; + + addr = args->init_ipa_base; + end = addr + args->init_ipa_size; + +if (end < addr) +return -EINVAL ; + +if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EBUSY ; + +while (addr < end) { +int level =find_map_level (kvm, addr, end); +unsigned long map_size =rme_rtt_level_mapsize (level); + + ret =realm_init_ipa_state (realm, addr, addr + map_size, level); +if (ret) +break ; + + addr += map_size; + } + +return ret; +} +/* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK (rme_vmid_lock);static unsigned long *rme_vmid_bitmap;@@ -460,6 +736,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) r =kvm_create_realm (kvm);break ; +case KVM_CAP_ARM_RME_INIT_IPA_REALM : { +struct kvm_cap_arm_rme_init_ipa_args args; +void __user *argp =u64_to_user_ptr (cap->args[1 ]); + +if (copy_from_user (&args, argp,sizeof (args))) { + r = -EFAULT ; +break ; + } + + r =kvm_init_ipa_range_realm (kvm, &args); +break ; + }default : r = -EINVAL ;break ;
RFC PATCH 14/28 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.
--- arch/arm64/include/asm/kvm_rme.h | 11 ++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 19 +++- arch/arm64/kvm/rme-exit.c | 168 +++++++++++++++++++++++++++++++ arch/arm64/kvm/rme.c | 11 ++ 5 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 arch/arm64/kvm/rme-exit.c
arch/arm64/include/asm/kvm_rme.h +11 -0
@@ -47,6 +47,9 @@ void kvm_realm_destroy_rtts(struct realm *realm, u32 ia_bits, u32 start_level);int kvm_create_rec (struct kvm_vcpu *vcpu);void kvm_destroy_rec (struct kvm_vcpu *vcpu); +int kvm_rec_enter (struct kvm_vcpu *vcpu); +int handle_rme_exit (struct kvm_vcpu *vcpu,int rec_run_status); +int realm_set_ipa_state (struct kvm_vcpu *vcpu,unsigned long addr,unsigned long end,unsigned long ripas);
@@ -69,4 +72,12 @@ static inline unsigned long rme_rtt_level_mapsize(int level) return (1UL << RME_RTT_LEVEL_SHIFT(level)); } +static inline bool realm_is_addr_protected (struct realm *realm, +unsigned long addr) +{ +unsigned int ia_bits = realm->ia_bits; + +return !(addr & ~(BIT (ia_bits -1 ) -1 )); +} +#endif
arch/arm64/kvm/Makefile +1 -1
@@ -21,7 +21,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ vgic/vgic-its.o vgic/vgic-debug.o \ - rme.o + rme.o rme-exit.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o
arch/arm64/kvm/arm.c +14 -5
@@ -985,7 +985,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++;
@@ -1039,10 +1042,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)local_irq_enable (); -trace_kvm_exit (ret,kvm_vcpu_trap_get_class (vcpu), *vcpu_pc (vcpu)); -/* Exit types that need handling before we can be preempted */ -handle_exit_early (vcpu, ret); +if (!vcpu_is_rec (vcpu)) { +trace_kvm_exit (ret,kvm_vcpu_trap_get_class (vcpu), + *vcpu_pc (vcpu)); + +handle_exit_early (vcpu, ret); + }preempt_enable ();
@@ -1065,7 +1071,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_rme_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 +168 -0
168 lines not on this page · raw message
arch/arm64/kvm/rme.c +11 -0
@@ -802,6 +802,17 @@ void kvm_destroy_realm(struct kvm *kvm)kvm_free_stage2_pgd (&kvm->arch.mmu); } +int kvm_rec_enter (struct kvm_vcpu *vcpu) +{ +struct rec *rec = &vcpu->arch.rec; + +if (kvm_realm_state (vcpu->kvm) !=REALM_STATE_ACTIVE ) +return -EINVAL ; + +return rmi_rec_enter (virt_to_phys (rec->rec_page), +virt_to_phys (rec->run)); +} +static void free_rec_aux (struct page **aux_pages,unsigned int num_aux) {
Signature · 1 line
2.34.1
RFC PATCH 15/28 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 the RMM interface provides a structure member for providing the read/written value and we can transfer this to the appropriate VCPU's register entry and then depend on the generic MMIO handling code in KVM.
--- arch/arm64/kvm/mmio.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm64/kvm/mmio.c +7 -0
@@ -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"
@@ -109,6 +110,9 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) &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->entry.gprs[0 ] = data; }/*
@@ -179,6 +183,9 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) run->mmio.len = len; vcpu->mmio_needed =1 ; +if (vcpu_is_rec (vcpu)) + vcpu->arch.rec.run->entry.flags |=RMI_EMULATED_MMIO ; +if (!ret) {/* We handled the access successfully in the kernel. */ if (!is_write)
Signature · 1 line
2.34.1
Re: [RFC PATCH 15/28] KVM: arm64: Handle realm MMIO emulation
Quoted 3 lines · On Fri, 27 Jan 2023 11:29:19 +0000 Steven Price <[email protected]> wrote:
MMIO emulation for a realm cannot be done directly with the VM's registers as they are protected from the host. However the RMM interface provides a structure member for providing the read/written value and
More details would be better for helping the review. I can only see the emulated mmio value from the device model (kvmtool or kvm_io_bus) is put into the GPRS[0] of the RecEntry object. But the rest of the flow is missing.
I guess RMM copies the value in the RecEntry.GPRS[0] to the target GPR in the guest context in RMI_REC_ENTER when seeing RMI_EMULATED_MMIO. This is for the guest MMIO read path.
How about the MMIO write path? I don't see where the RecExit.GPRS[0] is loaded to a varible and returned to the userspace.
Quoted 21 lines
we can transfer this to the appropriate VCPU's register entry and then depend on the generic MMIO handling code in KVM.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/mmio.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/arm64/kvm/mmio.c +1
-0@@ -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"
arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 115
Quoted 6 lines
arch/arm64/kvm/mmio.c +3
-0@@ -109,6 +110,9 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) &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->entry.gprs[0 ] = data;
I think the guest context is maintained by RMM (while KVM can only touch Rec{Entry, Exit} object) so that guest context in the legacy VHE mode is unused.
If yes, I guess here is should be:
if (unlikely(vcpu_is_rec(vcpu)))
vcpu->arch.rec.run->entry.gprs[0] = data; else vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
arch/arm64/kvm/mmio.c int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) · line 188
Quoted 10 lines
arch/arm64/kvm/mmio.c +3
-0}/* @@ -179,6 +183,9 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) run->mmio.len = len; vcpu->mmio_needed =1 ; +if (vcpu_is_rec (vcpu)) + vcpu->arch.rec.run->entry.flags |=RMI_EMULATED_MMIO ; +
Wouldn't it be better to set this in the kvm_handle_mmio_return where the MMIO read emulation has been surely successful?
Quoted 3 lines
arch/arm64/kvm/mmio.c +0
-0if (!ret) {/* We handled the access successfully in the kernel. */ if (!is_write)
Re: [RFC PATCH 15/28] KVM: arm64: Handle realm MMIO emulation
Quoted 10 lines · Zhi Wang
Quoted 3 lines · On Fri, 27 Jan 2023 11:29:19 +0000 Steven Price <[email protected]> wrote:
MMIO emulation for a realm cannot be done directly with the VM's registers as they are protected from the host. However the RMM interface provides a structure member for providing the read/written value and
More details would be better for helping the review. I can only see the emulated mmio value from the device model (kvmtool or
kvm_io_bus) is put into the GPRS[0] of the RecEntry object. But the rest of the flow is missing.
The commit message is out of date (sorry about that). A previous version of the spec had a dedicated member for the read/write value, but this was changed to just use GPRS[0] as you've spotted. I'll update the text.
Quoted 3 lines
I guess RMM copies the value in the RecEntry.GPRS[0] to the target GPR in the guest context in
RMI_REC_ENTERwhen seeingRMI_EMULATED_MMIO. This is for the guest MMIO read path.
Yes, when entering the guest after an (emulatable) read data abort the value in GPRS[0] is loaded from the RecEntry structure into the appropriate register for the guest.
Quoted 2 lines
How about the MMIO write path? I don't see where the RecExit.GPRS[0] is loaded to a varible and returned to the userspace.
The RMM will populate GPRS[0] with the written value in this case (even if another register was actually used in the instruction). We then transfer that to the usual VCPU structure so that the normal fault handling logic works.
Quoted 32 lines
Quoted 21 lines
we can transfer this to the appropriate VCPU's register entry and then depend on the generic MMIO handling code in KVM.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/mmio.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/arm64/kvm/mmio.c +1
-0@@ -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" arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 115
Quoted 6 lines
arch/arm64/kvm/mmio.c +3
-0@@ -109,6 +110,9 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) &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->entry.gprs[0 ] = data;I think the guest context is maintained by RMM (while KVM can only touch Rec{Entry, Exit} object) so that guest context in the legacy VHE mode is unused.
Quoted 6 lines
If yes, I guess here is should be:
if (unlikely(vcpu_is_rec(vcpu)))
vcpu->arch.rec.run->entry.gprs[0] = data; else vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
Correct. Although there's no harm in updating with vcpu_set_reg(). But I'll make the change because it's clearer.
Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/mmio.c +0
-0}/* @@ -179,6 +183,9 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) run->mmio.len = len; vcpu->mmio_needed =1 ;
Quoted 6 lines
arch/arm64/kvm/mmio.c int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) · line 188
Quoted 3 lines
arch/arm64/kvm/mmio.c +3
-0+if (vcpu_is_rec (vcpu)) + vcpu->arch.rec.run->entry.flags |=RMI_EMULATED_MMIO ; +Wouldn't it be better to set this in the
kvm_handle_mmio_returnwhere the MMIO read emulation has been surely successful?
Yes, that makes sense - I'll move this.
Thanks,
Steve
Quoted 4 lines
Quoted 3 lines
arch/arm64/kvm/mmio.c +0
-0if (!ret) {/* We handled the access successfully in the kernel. */ if (!is_write)
Re: [RFC PATCH 15/28] KVM: arm64: Handle realm MMIO emulation
Quoted 16 lines · On Fri, 10 Mar 2023 15:47:14 +0000 Steven Price <[email protected]> wrote:
Quoted 10 lines · Zhi Wang
Quoted 3 lines · On Fri, 27 Jan 2023 11:29:19 +0000 Steven Price <[email protected]> wrote:
MMIO emulation for a realm cannot be done directly with the VM's registers as they are protected from the host. However the RMM interface provides a structure member for providing the read/written value and
More details would be better for helping the review. I can only see the emulated mmio value from the device model (kvmtool or
kvm_io_bus) is put into the GPRS[0] of the RecEntry object. But the rest of the flow is missing.The commit message is out of date (sorry about that). A previous version of the spec had a dedicated member for the read/write value, but this was changed to just use GPRS[0] as you've spotted. I'll update the text.
Quoted 11 lines
Quoted 3 lines
I guess RMM copies the value in the RecEntry.GPRS[0] to the target GPR in the guest context in
RMI_REC_ENTERwhen seeingRMI_EMULATED_MMIO. This is for the guest MMIO read path.Yes, when entering the guest after an (emulatable) read data abort the value in GPRS[0] is loaded from the RecEntry structure into the appropriate register for the guest.
Quoted 2 lines
How about the MMIO write path? I don't see where the RecExit.GPRS[0] is loaded to a varible and returned to the userspace.
-----
Quoted 5 lines
The RMM will populate GPRS[0] with the written value in this case (even if another register was actually used in the instruction). We then transfer that to the usual VCPU structure so that the normal fault handling logic works.
-----
Are these in this patch or another patch?
Quoted 67 lines
Quoted 32 lines
Quoted 21 lines
we can transfer this to the appropriate VCPU's register entry and then depend on the generic MMIO handling code in KVM.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/mmio.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/arm64/kvm/mmio.c +1
-0@@ -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" arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 115
Quoted 6 lines
arch/arm64/kvm/mmio.c +3
-0@@ -109,6 +110,9 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) &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->entry.gprs[0 ] = data;I think the guest context is maintained by RMM (while KVM can only touch Rec{Entry, Exit} object) so that guest context in the legacy VHE mode is unused.
Quoted 6 lines
If yes, I guess here is should be:
if (unlikely(vcpu_is_rec(vcpu)))
vcpu->arch.rec.run->entry.gprs[0] = data; else vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);Correct. Although there's no harm in updating with
vcpu_set_reg(). But I'll make the change because it's clearer.Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/mmio.c +0
-0}/* @@ -179,6 +183,9 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) run->mmio.len = len; vcpu->mmio_needed =1 ;Quoted 6 lines
arch/arm64/kvm/mmio.c int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) · line 188
Quoted 3 lines
arch/arm64/kvm/mmio.c +3
-0+if (vcpu_is_rec (vcpu)) + vcpu->arch.rec.run->entry.flags |=RMI_EMULATED_MMIO ; +Wouldn't it be better to set this in the
kvm_handle_mmio_returnwhere the MMIO read emulation has been surely successful?Yes, that makes sense - I'll move this.
Thanks,
Steve
Quoted 4 lines
Quoted 3 lines
arch/arm64/kvm/mmio.c +0
-0if (!ret) {/* We handled the access successfully in the kernel. */ if (!is_write)
Re: [RFC PATCH 15/28] KVM: arm64: Handle realm MMIO emulation
Quoted 32 lines · Zhi Wang
Quoted 16 lines · On Fri, 10 Mar 2023 15:47:14 +0000 Steven Price <[email protected]> wrote:
Quoted 10 lines · Zhi Wang
Quoted 3 lines · On Fri, 27 Jan 2023 11:29:19 +0000 Steven Price <[email protected]> wrote:
MMIO emulation for a realm cannot be done directly with the VM's registers as they are protected from the host. However the RMM interface provides a structure member for providing the read/written value and
More details would be better for helping the review. I can only see the emulated mmio value from the device model (kvmtool or
kvm_io_bus) is put into the GPRS[0] of the RecEntry object. But the rest of the flow is missing.The commit message is out of date (sorry about that). A previous version of the spec had a dedicated member for the read/write value, but this was changed to just use GPRS[0] as you've spotted. I'll update the text.
Quoted 11 lines
Quoted 3 lines
I guess RMM copies the value in the RecEntry.GPRS[0] to the target GPR in the guest context in
RMI_REC_ENTERwhen seeingRMI_EMULATED_MMIO. This is for the guest MMIO read path.Yes, when entering the guest after an (emulatable) read data abort the value in GPRS[0] is loaded from the RecEntry structure into the appropriate register for the guest.
Quoted 2 lines
How about the MMIO write path? I don't see where the RecExit.GPRS[0] is loaded to a varible and returned to the userspace.
-----
Quoted 8 lines
Quoted 5 lines
The RMM will populate GPRS[0] with the written value in this case (even if another register was actually used in the instruction). We then transfer that to the usual VCPU structure so that the normal fault handling logic works.
-----
Are these in this patch or another patch?
The RMM (not included in this particular series[1]) sets the first element of the 'GPRS' array which is in memory shared with the host.
The Linux half to populate the vcpu structure is in the previous patch:
+static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu) +{
+ struct rec *rec = &vcpu->arch.rec;
+
+ 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);
+}
I guess it might make sense to pull the 'if' statement out of the previous patch and into this one to keep all the MMIO code together.
Steve
[1] This Linux code is written against the RMM specification and in theory could work with any RMM implementation. But the TF RMM is open source, so I can point you at the assignment in the latest commit here:
https://git.trustedfirmware.org/TF-RMM/tf-rmm.git/tree/runtime/core/exit.c?id=d294b1b05e8d234d32684a982552aa2a17fb9157#n264
Quoted 68 lines
Quoted 67 lines
Quoted 32 lines
Quoted 21 lines
we can transfer this to the appropriate VCPU's register entry and then depend on the generic MMIO handling code in KVM.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/mmio.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/arm64/kvm/mmio.c +1
-0@@ -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" arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 115
Quoted 6 lines
arch/arm64/kvm/mmio.c +3
-0@@ -109,6 +110,9 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) &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->entry.gprs[0 ] = data;I think the guest context is maintained by RMM (while KVM can only touch Rec{Entry, Exit} object) so that guest context in the legacy VHE mode is unused.
Quoted 6 lines
If yes, I guess here is should be:
if (unlikely(vcpu_is_rec(vcpu)))
vcpu->arch.rec.run->entry.gprs[0] = data; else vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);Correct. Although there's no harm in updating with
vcpu_set_reg(). But I'll make the change because it's clearer.Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/mmio.c +0
-0}/* @@ -179,6 +183,9 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) run->mmio.len = len; vcpu->mmio_needed =1 ;Quoted 6 lines
arch/arm64/kvm/mmio.c int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) · line 188
Quoted 3 lines
arch/arm64/kvm/mmio.c +3
-0+if (vcpu_is_rec (vcpu)) + vcpu->arch.rec.run->entry.flags |=RMI_EMULATED_MMIO ; +Wouldn't it be better to set this in the
kvm_handle_mmio_returnwhere the MMIO read emulation has been surely successful?Yes, that makes sense - I'll move this.
Thanks,
Steve
Quoted 4 lines
Quoted 3 lines
arch/arm64/kvm/mmio.c +0
-0if (!ret) {/* We handled the access successfully in the kernel. */ if (!is_write)
RFC PATCH 16/28 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.
--- arch/arm64/kvm/rme.c | 366 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 366 insertions(+)
arch/arm64/kvm/rme.c +366 -0
@@ -4,6 +4,7 @@*/ #include <linux/kvm_host.h> +#include <linux/hugetlb.h> #include <asm/kvm_emulate.h> #include <asm/kvm_mmu.h>
@@ -426,6 +427,359 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) } } +static int realm_create_protected_data_page (struct realm *realm, +unsigned long ipa, +struct page *dst_page, +struct page *tmp_page) +{ +phys_addr_t dst_phys, tmp_phys; +int ret; + +copy_page (page_address (tmp_page),page_address (dst_page)); + + dst_phys =page_to_phys (dst_page); + tmp_phys =page_to_phys (tmp_page); + +if (rmi_granule_delegate (dst_phys)) +return -ENXIO ; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, tmp_phys, +RMI_MEASURE_CONTENT ); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); +if (ret) + goto err; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, + tmp_phys,RMI_MEASURE_CONTENT ); + } + +if (ret) + goto err; + +return 0 ; + +err: +if (WARN_ON (rmi_granule_undelegate (dst_phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (dst_page); + } +return -ENXIO ; +} + +static int fold_rtt (phys_addr_t rd,unsigned long addr,int level, +struct realm *realm) +{ +struct rtt_entry rtt; +phys_addr_t rtt_addr; + +if (rmi_rtt_read_entry (rd, addr, level, &rtt)) +return -ENXIO ; + +if (rtt.state !=RMI_TABLE ) +return -EINVAL ; + + rtt_addr =rmi_rtt_get_phys (&rtt); +if (rmi_rtt_fold (rtt_addr, rd, addr, level +1 )) +return -ENXIO ; + +free_delegated_page (realm, rtt_addr); + +return 0 ; +} + +int realm_map_protected (struct realm *realm, +unsigned long hva, +unsigned long base_ipa, +struct page *dst_page, +unsigned long map_size, +struct kvm_mmu_memory_cache *memcache) +{ +phys_addr_t dst_phys =page_to_phys (dst_page); +phys_addr_t rd =virt_to_phys (realm->rd); +unsigned long phys = dst_phys; +unsigned long ipa = base_ipa; +unsigned long size; +int map_level; +int ret =0 ; + +if (WARN_ON (!IS_ALIGNED (ipa, map_size))) +return -EINVAL ; + +switch (map_size) { +case PAGE_SIZE : + map_level =3 ; +break ; +case RME_L2_BLOCK_SIZE : + map_level =2 ; +break ; +default : +return -EINVAL ; + } + +if (map_level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate it, +* however if there is an error (e.g. missing parent tables) +* this will be handled below. +*/ +realm_create_rtt_levels (realm, ipa, map_level, +RME_RTT_MAX_LEVEL , memcache); + } + +for (size =0 ; size < map_size; size +=PAGE_SIZE ) { +if (rmi_granule_delegate (phys)) { +struct rtt_entry rtt; + +/* +* It's possible we raced with another VCPU on the same +* fault. If the entry exists and matches then exit +* early and assume the other VCPU will handle the +* mapping. +*/ +if (rmi_rtt_read_entry (rd, ipa,RME_RTT_MAX_LEVEL , &rtt)) + goto err; + +// FIXME: For a block mapping this could race at level +// 2 or 3... +if (WARN_ON ((rtt.walk_level !=RME_RTT_MAX_LEVEL || + rtt.state !=RMI_ASSIGNED || + rtt.desc != phys))) { + goto err; + } + +return 0 ; + } + + ret =rmi_data_create_unknown (phys, rd, ipa); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL , + memcache); +WARN_ON (ret); +if (ret) + goto err_undelegate; + + ret =rmi_data_create_unknown (phys, rd, ipa); + } +WARN_ON (ret); + +if (ret) + goto err_undelegate; + + phys +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + } + +if (map_size ==RME_L2_BLOCK_SIZE ) + ret =fold_rtt (rd, base_ipa, map_level, realm); +if (WARN_ON (ret)) + goto err; + +return 0 ; + +err_undelegate: +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys)); + } +err: +while (size >0 ) { + phys -=PAGE_SIZE ; + size -=PAGE_SIZE ; + ipa -=PAGE_SIZE ; + +rmi_data_destroy (rd, ipa); + +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys)); + } + } +return -ENXIO ; +} + +static int populate_par_region (struct kvm *kvm, +phys_addr_t ipa_base, +phys_addr_t ipa_end) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_memory_slot *memslot; +gfn_t base_gfn, end_gfn; +int idx; +phys_addr_t ipa; +int ret =0 ; +struct page *tmp_page; +phys_addr_t rd =virt_to_phys (realm->rd); + + 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; + } + + tmp_page =alloc_page (GFP_KERNEL ); +if (!tmp_page) { + ret = -ENOMEM ; + goto out; + } + +mmap_read_lock (current->mm); + + ipa = ipa_base; + +while (ipa < ipa_end) { +struct vm_area_struct *vma; +unsigned long map_size; +unsigned int vma_shift; +unsigned long offset; +unsigned long hva; +struct page *page; +kvm_pfn_t pfn; +int level; + + hva =gfn_to_hva_memslot (memslot,gpa_to_gfn (ipa)); + vma =vma_lookup (current->mm, hva); +if (!vma) { + ret = -EFAULT ; +break ; + } + +if (is_vm_hugetlb_page (vma)) + vma_shift =huge_page_shift (hstate_vma (vma)); +else + vma_shift =PAGE_SHIFT ; + + map_size =1 << vma_shift; + +/* +* FIXME: This causes over mapping, but there's no good +* solution here with the ABI as it stands +*/ + ipa =ALIGN_DOWN (ipa, map_size); + +switch (map_size) { +case RME_L2_BLOCK_SIZE : + level =2 ; +break ; +case PAGE_SIZE : + level =3 ; +break ; +default : +WARN_ONCE (1 ,"Unsupport vma_shift %d" , vma_shift); + ret = -EFAULT ; +break ; + } + + pfn =gfn_to_pfn_memslot (memslot,gpa_to_gfn (ipa)); + +if (is_error_pfn (pfn)) { + ret = -EFAULT ; +break ; + } + + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { + ret =realm_create_rtt_levels (realm, ipa, +RMI_RETURN_INDEX (ret), + level,NULL ); +if (ret) +break ; + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (ret) { + ret = -ENXIO ; +break ; + } + } + +if (level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate +* it, however if there is an error (e.g. missing +* parent tables) this will be handled in the +* realm_create_protected_data_page() call. +*/ +realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); + } + + page =pfn_to_page (pfn); + +for (offset =0 ; offset < map_size && !ret; + offset +=PAGE_SIZE , page++) { +phys_addr_t page_ipa = ipa + offset; + + ret =realm_create_protected_data_page (realm, page_ipa, + page, tmp_page); + } +if (ret) + goto err_release_pfn; + +if (level ==2 ) { + ret =fold_rtt (rd, ipa, level, realm); +if (ret) + goto err_release_pfn; + } + + ipa += map_size; +kvm_set_pfn_accessed (pfn); +kvm_set_pfn_dirty (pfn); +kvm_release_pfn_dirty (pfn); +err_release_pfn: +if (ret) { +kvm_release_pfn_clean (pfn); +break ; + } + } + +mmap_read_unlock (current->mm); +__free_page (tmp_page); + +out: +srcu_read_unlock (&kvm->srcu, idx); +return ret; +} + +static int kvm_populate_realm (struct kvm *kvm, +struct kvm_cap_arm_rme_populate_realm_args *args) +{ +phys_addr_t ipa_base, ipa_end; + +if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EBUSY ; + +if (!IS_ALIGNED (args->populate_ipa_base,PAGE_SIZE ) || + !IS_ALIGNED (args->populate_ipa_size,PAGE_SIZE )) +return -EINVAL ; + + ipa_base = args->populate_ipa_base; + ipa_end = ipa_base + args->populate_ipa_size; + +if (ipa_end < ipa_base) +return -EINVAL ; + +return populate_par_region (kvm, ipa_base, ipa_end); +} + static int set_ipa_state(struct kvm_vcpu *vcpu, unsigned long ipa, unsigned long end,
@@ -748,6 +1102,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 kvm_cap_arm_rme_populate_realm_args 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.34.1
Re: [RFC PATCH 16/28] arm64: RME: Allow populating initial contents
Quoted 196 lines · On Fri, 27 Jan 2023 11:29:20 +0000 Steven Price <[email protected]> wrote:
The VMM needs to populate the realm with some data before starting (e.g. a kernel and initrd). This is measured by the RMM and used as part of the attestation later on.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/rme.c | 366 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 366 insertions(+)arch/arm64/kvm/rme.c +172
-0@@ -4,6 +4,7 @@*/ #include <linux/kvm_host.h> +#include <linux/hugetlb.h> #include <asm/kvm_emulate.h> #include <asm/kvm_mmu.h> @@ -426,6 +427,359 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) } } +static int realm_create_protected_data_page (struct realm *realm, +unsigned long ipa, +struct page *dst_page, +struct page *tmp_page) +{ +phys_addr_t dst_phys, tmp_phys; +int ret; + +copy_page (page_address (tmp_page),page_address (dst_page)); + + dst_phys =page_to_phys (dst_page); + tmp_phys =page_to_phys (tmp_page); + +if (rmi_granule_delegate (dst_phys)) +return -ENXIO ; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, tmp_phys, +RMI_MEASURE_CONTENT ); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); +if (ret) + goto err; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, + tmp_phys,RMI_MEASURE_CONTENT ); + } + +if (ret) + goto err; + +return 0 ; + +err: +if (WARN_ON (rmi_granule_undelegate (dst_phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (dst_page); + } +return -ENXIO ; +} + +static int fold_rtt (phys_addr_t rd,unsigned long addr,int level, +struct realm *realm) +{ +struct rtt_entry rtt; +phys_addr_t rtt_addr; + +if (rmi_rtt_read_entry (rd, addr, level, &rtt)) +return -ENXIO ; + +if (rtt.state !=RMI_TABLE ) +return -EINVAL ; + + rtt_addr =rmi_rtt_get_phys (&rtt); +if (rmi_rtt_fold (rtt_addr, rd, addr, level +1 )) +return -ENXIO ; + +free_delegated_page (realm, rtt_addr); + +return 0 ; +} + +int realm_map_protected (struct realm *realm, +unsigned long hva, +unsigned long base_ipa, +struct page *dst_page, +unsigned long map_size, +struct kvm_mmu_memory_cache *memcache) +{ +phys_addr_t dst_phys =page_to_phys (dst_page); +phys_addr_t rd =virt_to_phys (realm->rd); +unsigned long phys = dst_phys; +unsigned long ipa = base_ipa; +unsigned long size; +int map_level; +int ret =0 ; + +if (WARN_ON (!IS_ALIGNED (ipa, map_size))) +return -EINVAL ; + +switch (map_size) { +case PAGE_SIZE : + map_level =3 ; +break ; +case RME_L2_BLOCK_SIZE : + map_level =2 ; +break ; +default : +return -EINVAL ; + } + +if (map_level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate it, +* however if there is an error (e.g. missing parent tables) +* this will be handled below. +*/ +realm_create_rtt_levels (realm, ipa, map_level, +RME_RTT_MAX_LEVEL , memcache); + } + +for (size =0 ; size < map_size; size +=PAGE_SIZE ) { +if (rmi_granule_delegate (phys)) { +struct rtt_entry rtt; + +/* +* It's possible we raced with another VCPU on the same +* fault. If the entry exists and matches then exit +* early and assume the other VCPU will handle the +* mapping. +*/ +if (rmi_rtt_read_entry (rd, ipa,RME_RTT_MAX_LEVEL , &rtt)) + goto err; + +// FIXME: For a block mapping this could race at level +// 2 or 3... +if (WARN_ON ((rtt.walk_level !=RME_RTT_MAX_LEVEL || + rtt.state !=RMI_ASSIGNED || + rtt.desc != phys))) { + goto err; + } + +return 0 ; + } + + ret =rmi_data_create_unknown (phys, rd, ipa); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL , + memcache); +WARN_ON (ret); +if (ret) + goto err_undelegate; + + ret =rmi_data_create_unknown (phys, rd, ipa); + } +WARN_ON (ret); + +if (ret) + goto err_undelegate; + + phys +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + } + +if (map_size ==RME_L2_BLOCK_SIZE ) + ret =fold_rtt (rd, base_ipa, map_level, realm); +if (WARN_ON (ret)) + goto err; + +return 0 ; + +err_undelegate: +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys)); + } +err: +while (size >0 ) { + phys -=PAGE_SIZE ; + size -=PAGE_SIZE ; + ipa -=PAGE_SIZE ; +
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 610
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+rmi_data_destroy (rd, ipa); + +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys)); + } + } +return -ENXIO ; +} +
There seems no caller to the function above. Better move it to the related patch.
Quoted 126 lines
arch/arm64/kvm/rme.c +126
-0+static int populate_par_region (struct kvm *kvm, +phys_addr_t ipa_base, +phys_addr_t ipa_end) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_memory_slot *memslot; +gfn_t base_gfn, end_gfn; +int idx; +phys_addr_t ipa; +int ret =0 ; +struct page *tmp_page; +phys_addr_t rd =virt_to_phys (realm->rd); + + 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; + } + + tmp_page =alloc_page (GFP_KERNEL ); +if (!tmp_page) { + ret = -ENOMEM ; + goto out; + } + +mmap_read_lock (current->mm); + + ipa = ipa_base; + +while (ipa < ipa_end) { +struct vm_area_struct *vma; +unsigned long map_size; +unsigned int vma_shift; +unsigned long offset; +unsigned long hva; +struct page *page; +kvm_pfn_t pfn; +int level; + + hva =gfn_to_hva_memslot (memslot,gpa_to_gfn (ipa)); + vma =vma_lookup (current->mm, hva); +if (!vma) { + ret = -EFAULT ; +break ; + } + +if (is_vm_hugetlb_page (vma)) + vma_shift =huge_page_shift (hstate_vma (vma)); +else + vma_shift =PAGE_SHIFT ; + + map_size =1 << vma_shift; + +/* +* FIXME: This causes over mapping, but there's no good +* solution here with the ABI as it stands +*/ + ipa =ALIGN_DOWN (ipa, map_size); + +switch (map_size) { +case RME_L2_BLOCK_SIZE : + level =2 ; +break ; +case PAGE_SIZE : + level =3 ; +break ; +default : +WARN_ONCE (1 ,"Unsupport vma_shift %d" , vma_shift); + ret = -EFAULT ; +break ; + } + + pfn =gfn_to_pfn_memslot (memslot,gpa_to_gfn (ipa)); + +if (is_error_pfn (pfn)) { + ret = -EFAULT ; +break ; + } + + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { + ret =realm_create_rtt_levels (realm, ipa, +RMI_RETURN_INDEX (ret), + level,NULL ); +if (ret) +break ; + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (ret) { + ret = -ENXIO ; +break ; + } + } + +if (level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate +* it, however if there is an error (e.g. missing +* parent tables) this will be handled in the +* realm_create_protected_data_page() call. +*/ +realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); + } + + page =pfn_to_page (pfn); + +for (offset =0 ; offset < map_size && !ret; + offset +=PAGE_SIZE , page++) { +phys_addr_t page_ipa = ipa + offset; + + ret =realm_create_protected_data_page (realm, page_ipa, + page, tmp_page); + } +if (ret) + goto err_release_pfn; +
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 745
Quoted 10 lines
arch/arm64/kvm/rme.c +9
-0+if (level ==2 ) { + ret =fold_rtt (rd, ipa, level, realm); +if (ret) + goto err_release_pfn; + } + + ipa += map_size; +kvm_set_pfn_accessed (pfn); +kvm_set_pfn_dirty (pfn);
kvm_release_pfn_dirty() has already called kvm_set_pfn_{accessed, dirty}().
Quoted 11 lines
arch/arm64/kvm/rme.c +11
-0+kvm_release_pfn_dirty (pfn); +err_release_pfn: +if (ret) { +kvm_release_pfn_clean (pfn); +break ; + } + } + +mmap_read_unlock (current->mm); +__free_page (tmp_page); +
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 766
Quoted 10 lines
arch/arm64/kvm/rme.c +10
-0+out: +srcu_read_unlock (&kvm->srcu, idx); +return ret; +} + +static int kvm_populate_realm (struct kvm *kvm, +struct kvm_cap_arm_rme_populate_realm_args *args) +{ +phys_addr_t ipa_base, ipa_end; +
Check kvm_is_realm(kvm) here or in the kvm_realm_enable_cap().
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 768
Quoted 2 lines
arch/arm64/kvm/rme.c +2
-0+if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EBUSY ;
Maybe -EINVAL? The realm hasn't been created (RMI_REALM_CREATE is not called yet). The userspace shouldn't reach this path.
Quoted 36 lines
arch/arm64/kvm/rme.c +14
-0+ +if (!IS_ALIGNED (args->populate_ipa_base,PAGE_SIZE ) || + !IS_ALIGNED (args->populate_ipa_size,PAGE_SIZE )) +return -EINVAL ; + + ipa_base = args->populate_ipa_base; + ipa_end = ipa_base + args->populate_ipa_size; + +if (ipa_end < ipa_base) +return -EINVAL ; + +return populate_par_region (kvm, ipa_base, ipa_end); +} + static int set_ipa_state(struct kvm_vcpu *vcpu, unsigned long ipa,arch/arm64/kvm/rme.c +12
-0unsigned long end,@@ -748,6 +1102,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 kvm_cap_arm_rme_populate_realm_args 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 ;
Re: [RFC PATCH 16/28] arm64: RME: Allow populating initial contents
Quoted 204 lines · Zhi Wang
Quoted 201 lines · On Fri, 27 Jan 2023 11:29:20 +0000 Steven Price <[email protected]> wrote:
The VMM needs to populate the realm with some data before starting (e.g. a kernel and initrd). This is measured by the RMM and used as part of the attestation later on.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/rme.c | 366 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 366 insertions(+)arch/arm64/kvm/rme.c +177
-0@@ -4,6 +4,7 @@*/ #include <linux/kvm_host.h> +#include <linux/hugetlb.h> #include <asm/kvm_emulate.h> #include <asm/kvm_mmu.h> @@ -426,6 +427,359 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) } } +static int realm_create_protected_data_page (struct realm *realm, +unsigned long ipa, +struct page *dst_page, +struct page *tmp_page) +{ +phys_addr_t dst_phys, tmp_phys; +int ret; + +copy_page (page_address (tmp_page),page_address (dst_page)); + + dst_phys =page_to_phys (dst_page); + tmp_phys =page_to_phys (tmp_page); + +if (rmi_granule_delegate (dst_phys)) +return -ENXIO ; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, tmp_phys, +RMI_MEASURE_CONTENT ); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); +if (ret) + goto err; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, + tmp_phys,RMI_MEASURE_CONTENT ); + } + +if (ret) + goto err; + +return 0 ; + +err: +if (WARN_ON (rmi_granule_undelegate (dst_phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (dst_page); + } +return -ENXIO ; +} + +static int fold_rtt (phys_addr_t rd,unsigned long addr,int level, +struct realm *realm) +{ +struct rtt_entry rtt; +phys_addr_t rtt_addr; + +if (rmi_rtt_read_entry (rd, addr, level, &rtt)) +return -ENXIO ; + +if (rtt.state !=RMI_TABLE ) +return -EINVAL ; + + rtt_addr =rmi_rtt_get_phys (&rtt); +if (rmi_rtt_fold (rtt_addr, rd, addr, level +1 )) +return -ENXIO ; + +free_delegated_page (realm, rtt_addr); + +return 0 ; +} + +int realm_map_protected (struct realm *realm, +unsigned long hva, +unsigned long base_ipa, +struct page *dst_page, +unsigned long map_size, +struct kvm_mmu_memory_cache *memcache) +{ +phys_addr_t dst_phys =page_to_phys (dst_page); +phys_addr_t rd =virt_to_phys (realm->rd); +unsigned long phys = dst_phys; +unsigned long ipa = base_ipa; +unsigned long size; +int map_level; +int ret =0 ; + +if (WARN_ON (!IS_ALIGNED (ipa, map_size))) +return -EINVAL ; + +switch (map_size) { +case PAGE_SIZE : + map_level =3 ; +break ; +case RME_L2_BLOCK_SIZE : + map_level =2 ; +break ; +default : +return -EINVAL ; + } + +if (map_level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate it, +* however if there is an error (e.g. missing parent tables) +* this will be handled below. +*/ +realm_create_rtt_levels (realm, ipa, map_level, +RME_RTT_MAX_LEVEL , memcache); + } + +for (size =0 ; size < map_size; size +=PAGE_SIZE ) { +if (rmi_granule_delegate (phys)) { +struct rtt_entry rtt; + +/* +* It's possible we raced with another VCPU on the same +* fault. If the entry exists and matches then exit +* early and assume the other VCPU will handle the +* mapping. +*/ +if (rmi_rtt_read_entry (rd, ipa,RME_RTT_MAX_LEVEL , &rtt)) + goto err; + +// FIXME: For a block mapping this could race at level +// 2 or 3... +if (WARN_ON ((rtt.walk_level !=RME_RTT_MAX_LEVEL || + rtt.state !=RMI_ASSIGNED || + rtt.desc != phys))) { + goto err; + } + +return 0 ; + } + + ret =rmi_data_create_unknown (phys, rd, ipa); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL , + memcache); +WARN_ON (ret); +if (ret) + goto err_undelegate; + + ret =rmi_data_create_unknown (phys, rd, ipa); + } +WARN_ON (ret); + +if (ret) + goto err_undelegate; + + phys +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + } + +if (map_size ==RME_L2_BLOCK_SIZE ) + ret =fold_rtt (rd, base_ipa, map_level, realm); +if (WARN_ON (ret)) + goto err; + +return 0 ; + +err_undelegate: +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys)); + } +err: +while (size >0 ) { + phys -=PAGE_SIZE ; + size -=PAGE_SIZE ; + ipa -=PAGE_SIZE ; + +rmi_data_destroy (rd, ipa); + +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys));
Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 474
Quoted 5 lines
arch/arm64/kvm/rme.c +5
-0+ } + } +return -ENXIO ; +} +There seems no caller to the function above. Better move it to the related patch.
Indeed this should really be in the next patch - will move as it's very confusing having it in this patch (sorry about that).
Quoted 134 lines
Quoted 133 lines
arch/arm64/kvm/rme.c +133
-0+static int populate_par_region (struct kvm *kvm, +phys_addr_t ipa_base, +phys_addr_t ipa_end) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_memory_slot *memslot; +gfn_t base_gfn, end_gfn; +int idx; +phys_addr_t ipa; +int ret =0 ; +struct page *tmp_page; +phys_addr_t rd =virt_to_phys (realm->rd); + + 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; + } + + tmp_page =alloc_page (GFP_KERNEL ); +if (!tmp_page) { + ret = -ENOMEM ; + goto out; + } + +mmap_read_lock (current->mm); + + ipa = ipa_base; + +while (ipa < ipa_end) { +struct vm_area_struct *vma; +unsigned long map_size; +unsigned int vma_shift; +unsigned long offset; +unsigned long hva; +struct page *page; +kvm_pfn_t pfn; +int level; + + hva =gfn_to_hva_memslot (memslot,gpa_to_gfn (ipa)); + vma =vma_lookup (current->mm, hva); +if (!vma) { + ret = -EFAULT ; +break ; + } + +if (is_vm_hugetlb_page (vma)) + vma_shift =huge_page_shift (hstate_vma (vma)); +else + vma_shift =PAGE_SHIFT ; + + map_size =1 << vma_shift; + +/* +* FIXME: This causes over mapping, but there's no good +* solution here with the ABI as it stands +*/ + ipa =ALIGN_DOWN (ipa, map_size); + +switch (map_size) { +case RME_L2_BLOCK_SIZE : + level =2 ; +break ; +case PAGE_SIZE : + level =3 ; +break ; +default : +WARN_ONCE (1 ,"Unsupport vma_shift %d" , vma_shift); + ret = -EFAULT ; +break ; + } + + pfn =gfn_to_pfn_memslot (memslot,gpa_to_gfn (ipa)); + +if (is_error_pfn (pfn)) { + ret = -EFAULT ; +break ; + } + + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { + ret =realm_create_rtt_levels (realm, ipa, +RMI_RETURN_INDEX (ret), + level,NULL ); +if (ret) +break ; + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (ret) { + ret = -ENXIO ; +break ; + } + } + +if (level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate +* it, however if there is an error (e.g. missing +* parent tables) this will be handled in the +* realm_create_protected_data_page() call. +*/ +realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); + } + + page =pfn_to_page (pfn); + +for (offset =0 ; offset < map_size && !ret; + offset +=PAGE_SIZE , page++) { +phys_addr_t page_ipa = ipa + offset; + + ret =realm_create_protected_data_page (realm, page_ipa, + page, tmp_page); + } +if (ret) + goto err_release_pfn; + +if (level ==2 ) { + ret =fold_rtt (rd, ipa, level, realm); +if (ret) + goto err_release_pfn; + } + + ipa += map_size;
Quoted 4 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 745
Quoted 2 lines
arch/arm64/kvm/rme.c +2
-0+kvm_set_pfn_accessed (pfn); +kvm_set_pfn_dirty (pfn);
kvm_release_pfn_dirty()has already called kvm_set_pfn_{accessed, dirty}().
Will remove those calls.
Quoted 15 lines
Quoted 15 lines
arch/arm64/kvm/rme.c +15
-0+kvm_release_pfn_dirty (pfn); +err_release_pfn: +if (ret) { +kvm_release_pfn_clean (pfn); +break ; + } + } + +mmap_read_unlock (current->mm); +__free_page (tmp_page); + +out: +srcu_read_unlock (&kvm->srcu, idx); +return ret; +}
Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 766
Quoted 6 lines
arch/arm64/kvm/rme.c +6
-0+ +static int kvm_populate_realm (struct kvm *kvm, +struct kvm_cap_arm_rme_populate_realm_args *args) +{ +phys_addr_t ipa_base, ipa_end; +Check
kvm_is_realm(kvm)here or in thekvm_realm_enable_cap().
I'm going to update kvm_vm_ioctl_enable_cap() to check kvm_is_realm() so we won't get here.
Quoted 5 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 768
Quoted 2 lines
arch/arm64/kvm/rme.c +2
-0+if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EBUSY ;Maybe
-EINVAL? The realm hasn't been created (RMI_REALM_CREATEis not called yet). The userspace shouldn't reach this path.
Well user space can attempt to populate in the ACTIVE state - which is where the idea of 'busy' comes from. Admittedly it's a little confusing when RMI_REALM_CREATE hasn't been called.
I'm not particularly bothered about the return code, but it's useful to have a different code to -EINVAL as it's not an invalid argument, but calling at the wrong time. I can't immediately see a better error code though.
Steve
Quoted 37 lines
Quoted 36 lines
arch/arm64/kvm/rme.c +26
-0+ +if (!IS_ALIGNED (args->populate_ipa_base,PAGE_SIZE ) || + !IS_ALIGNED (args->populate_ipa_size,PAGE_SIZE )) +return -EINVAL ; + + ipa_base = args->populate_ipa_base; + ipa_end = ipa_base + args->populate_ipa_size; + +if (ipa_end < ipa_base) +return -EINVAL ; + +return populate_par_region (kvm, ipa_base, ipa_end); +} + static int set_ipa_state(struct kvm_vcpu *vcpu, unsigned long ipa, unsigned long end,@@ -748,6 +1102,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 kvm_cap_arm_rme_populate_realm_args 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 ;
Re: [RFC PATCH 16/28] arm64: RME: Allow populating initial contents
Quoted 391 lines · On Fri, 10 Mar 2023 15:47:16 +0000 Steven Price <[email protected]> wrote:
Quoted 204 lines · Zhi Wang
Quoted 201 lines · On Fri, 27 Jan 2023 11:29:20 +0000 Steven Price <[email protected]> wrote:
The VMM needs to populate the realm with some data before starting (e.g. a kernel and initrd). This is measured by the RMM and used as part of the attestation later on.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kvm/rme.c | 366 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 366 insertions(+)arch/arm64/kvm/rme.c +177
-0@@ -4,6 +4,7 @@*/ #include <linux/kvm_host.h> +#include <linux/hugetlb.h> #include <asm/kvm_emulate.h> #include <asm/kvm_mmu.h> @@ -426,6 +427,359 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) } } +static int realm_create_protected_data_page (struct realm *realm, +unsigned long ipa, +struct page *dst_page, +struct page *tmp_page) +{ +phys_addr_t dst_phys, tmp_phys; +int ret; + +copy_page (page_address (tmp_page),page_address (dst_page)); + + dst_phys =page_to_phys (dst_page); + tmp_phys =page_to_phys (tmp_page); + +if (rmi_granule_delegate (dst_phys)) +return -ENXIO ; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, tmp_phys, +RMI_MEASURE_CONTENT ); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); +if (ret) + goto err; + + ret =rmi_data_create (dst_phys,virt_to_phys (realm->rd), ipa, + tmp_phys,RMI_MEASURE_CONTENT ); + } + +if (ret) + goto err; + +return 0 ; + +err: +if (WARN_ON (rmi_granule_undelegate (dst_phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (dst_page); + } +return -ENXIO ; +} + +static int fold_rtt (phys_addr_t rd,unsigned long addr,int level, +struct realm *realm) +{ +struct rtt_entry rtt; +phys_addr_t rtt_addr; + +if (rmi_rtt_read_entry (rd, addr, level, &rtt)) +return -ENXIO ; + +if (rtt.state !=RMI_TABLE ) +return -EINVAL ; + + rtt_addr =rmi_rtt_get_phys (&rtt); +if (rmi_rtt_fold (rtt_addr, rd, addr, level +1 )) +return -ENXIO ; + +free_delegated_page (realm, rtt_addr); + +return 0 ; +} + +int realm_map_protected (struct realm *realm, +unsigned long hva, +unsigned long base_ipa, +struct page *dst_page, +unsigned long map_size, +struct kvm_mmu_memory_cache *memcache) +{ +phys_addr_t dst_phys =page_to_phys (dst_page); +phys_addr_t rd =virt_to_phys (realm->rd); +unsigned long phys = dst_phys; +unsigned long ipa = base_ipa; +unsigned long size; +int map_level; +int ret =0 ; + +if (WARN_ON (!IS_ALIGNED (ipa, map_size))) +return -EINVAL ; + +switch (map_size) { +case PAGE_SIZE : + map_level =3 ; +break ; +case RME_L2_BLOCK_SIZE : + map_level =2 ; +break ; +default : +return -EINVAL ; + } + +if (map_level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate it, +* however if there is an error (e.g. missing parent tables) +* this will be handled below. +*/ +realm_create_rtt_levels (realm, ipa, map_level, +RME_RTT_MAX_LEVEL , memcache); + } + +for (size =0 ; size < map_size; size +=PAGE_SIZE ) { +if (rmi_granule_delegate (phys)) { +struct rtt_entry rtt; + +/* +* It's possible we raced with another VCPU on the same +* fault. If the entry exists and matches then exit +* early and assume the other VCPU will handle the +* mapping. +*/ +if (rmi_rtt_read_entry (rd, ipa,RME_RTT_MAX_LEVEL , &rtt)) + goto err; + +// FIXME: For a block mapping this could race at level +// 2 or 3... +if (WARN_ON ((rtt.walk_level !=RME_RTT_MAX_LEVEL || + rtt.state !=RMI_ASSIGNED || + rtt.desc != phys))) { + goto err; + } + +return 0 ; + } + + ret =rmi_data_create_unknown (phys, rd, ipa); + +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { +/* Create missing RTTs and retry */ +int level =RMI_RETURN_INDEX (ret); + + ret =realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL , + memcache); +WARN_ON (ret); +if (ret) + goto err_undelegate; + + ret =rmi_data_create_unknown (phys, rd, ipa); + } +WARN_ON (ret); + +if (ret) + goto err_undelegate; + + phys +=PAGE_SIZE ; + ipa +=PAGE_SIZE ; + } + +if (map_size ==RME_L2_BLOCK_SIZE ) + ret =fold_rtt (rd, base_ipa, map_level, realm); +if (WARN_ON (ret)) + goto err; + +return 0 ; + +err_undelegate: +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys)); + } +err: +while (size >0 ) { + phys -=PAGE_SIZE ; + size -=PAGE_SIZE ; + ipa -=PAGE_SIZE ; + +rmi_data_destroy (rd, ipa); + +if (WARN_ON (rmi_granule_undelegate (phys))) { +/* Page can't be returned to NS world so is lost */ +get_page (phys_to_page (phys));Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 474
Quoted 5 lines
arch/arm64/kvm/rme.c +5
-0+ } + } +return -ENXIO ; +} +There seems no caller to the function above. Better move it to the related patch.
Indeed this should really be in the next patch - will move as it's very confusing having it in this patch (sorry about that).
Quoted 134 lines
Quoted 133 lines
arch/arm64/kvm/rme.c +133
-0+static int populate_par_region (struct kvm *kvm, +phys_addr_t ipa_base, +phys_addr_t ipa_end) +{ +struct realm *realm = &kvm->arch.realm; +struct kvm_memory_slot *memslot; +gfn_t base_gfn, end_gfn; +int idx; +phys_addr_t ipa; +int ret =0 ; +struct page *tmp_page; +phys_addr_t rd =virt_to_phys (realm->rd); + + 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; + } + + tmp_page =alloc_page (GFP_KERNEL ); +if (!tmp_page) { + ret = -ENOMEM ; + goto out; + } + +mmap_read_lock (current->mm); + + ipa = ipa_base; + +while (ipa < ipa_end) { +struct vm_area_struct *vma; +unsigned long map_size; +unsigned int vma_shift; +unsigned long offset; +unsigned long hva; +struct page *page; +kvm_pfn_t pfn; +int level; + + hva =gfn_to_hva_memslot (memslot,gpa_to_gfn (ipa)); + vma =vma_lookup (current->mm, hva); +if (!vma) { + ret = -EFAULT ; +break ; + } + +if (is_vm_hugetlb_page (vma)) + vma_shift =huge_page_shift (hstate_vma (vma)); +else + vma_shift =PAGE_SHIFT ; + + map_size =1 << vma_shift; + +/* +* FIXME: This causes over mapping, but there's no good +* solution here with the ABI as it stands +*/ + ipa =ALIGN_DOWN (ipa, map_size); + +switch (map_size) { +case RME_L2_BLOCK_SIZE : + level =2 ; +break ; +case PAGE_SIZE : + level =3 ; +break ; +default : +WARN_ONCE (1 ,"Unsupport vma_shift %d" , vma_shift); + ret = -EFAULT ; +break ; + } + + pfn =gfn_to_pfn_memslot (memslot,gpa_to_gfn (ipa)); + +if (is_error_pfn (pfn)) { + ret = -EFAULT ; +break ; + } + + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (RMI_RETURN_STATUS (ret) ==RMI_ERROR_RTT ) { + ret =realm_create_rtt_levels (realm, ipa, +RMI_RETURN_INDEX (ret), + level,NULL ); +if (ret) +break ; + ret =rmi_rtt_init_ripas (rd, ipa, level); +if (ret) { + ret = -ENXIO ; +break ; + } + } + +if (level <RME_RTT_MAX_LEVEL ) { +/* +* A temporary RTT is needed during the map, precreate +* it, however if there is an error (e.g. missing +* parent tables) this will be handled in the +* realm_create_protected_data_page() call. +*/ +realm_create_rtt_levels (realm, ipa, level, +RME_RTT_MAX_LEVEL ,NULL ); + } + + page =pfn_to_page (pfn); + +for (offset =0 ; offset < map_size && !ret; + offset +=PAGE_SIZE , page++) { +phys_addr_t page_ipa = ipa + offset; + + ret =realm_create_protected_data_page (realm, page_ipa, + page, tmp_page); + } +if (ret) + goto err_release_pfn; + +if (level ==2 ) { + ret =fold_rtt (rd, ipa, level, realm); +if (ret) + goto err_release_pfn; + } + + ipa += map_size;Quoted 4 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 745
Quoted 2 lines
arch/arm64/kvm/rme.c +2
-0+kvm_set_pfn_accessed (pfn); +kvm_set_pfn_dirty (pfn);
kvm_release_pfn_dirty()has already called kvm_set_pfn_{accessed, dirty}().Will remove those calls.
Quoted 15 lines
Quoted 15 lines
arch/arm64/kvm/rme.c +15
-0+kvm_release_pfn_dirty (pfn); +err_release_pfn: +if (ret) { +kvm_release_pfn_clean (pfn); +break ; + } + } + +mmap_read_unlock (current->mm); +__free_page (tmp_page); + +out: +srcu_read_unlock (&kvm->srcu, idx); +return ret; +}Quoted 8 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 766
Quoted 6 lines
arch/arm64/kvm/rme.c +6
-0+ +static int kvm_populate_realm (struct kvm *kvm, +struct kvm_cap_arm_rme_populate_realm_args *args) +{ +phys_addr_t ipa_base, ipa_end; +Check
kvm_is_realm(kvm)here or in thekvm_realm_enable_cap().I'm going to update
kvm_vm_ioctl_enable_cap()to checkkvm_is_realm()so we won't get here.Quoted 5 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 768
Quoted 2 lines
arch/arm64/kvm/rme.c +2
-0+if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EBUSY ;Maybe
-EINVAL? The realm hasn't been created (RMI_REALM_CREATEis not called yet). The userspace shouldn't reach this path.
Quoted 9 lines
Well user space can attempt to populate in the ACTIVE state - which is where the idea of 'busy' comes from. Admittedly it's a little confusing when
RMI_REALM_CREATEhasn't been called.I'm not particularly bothered about the return code, but it's useful to have a different code to
-EINVALas it's not an invalid argument, but calling at the wrong time. I can't immediately see a better error code though.
The reason why I feel -EBUSY is little bit off is EBUSY usually indicates something is already initialized and currently running, then another calling path wanna to operate it.
I took a look on the ioctls in arch/arm64/kvm/arm.c. It seems people have different opinions for calling execution path at a wrong time:
For example:
long kvm_arch_vcpu_ioctl()
...
case KVM_GET_REG_LIST: {
struct kvm_reg_list __user *user_list = argp;
struct kvm_reg_list reg_list;
unsigned n;
r = -ENOEXEC;
if (unlikely(!kvm_vcpu_initialized(vcpu)))
break;
r = -EPERM;
if (!kvm_arm_vcpu_is_finalized(vcpu))
break;If we have to choose one, I prefer -ENOEXEC as -EPERM is stranger. But personally my vote goes to -EINVAL.
Quoted 40 lines
Steve
Quoted 37 lines
Quoted 36 lines
arch/arm64/kvm/rme.c +26
-0+ +if (!IS_ALIGNED (args->populate_ipa_base,PAGE_SIZE ) || + !IS_ALIGNED (args->populate_ipa_size,PAGE_SIZE )) +return -EINVAL ; + + ipa_base = args->populate_ipa_base; + ipa_end = ipa_base + args->populate_ipa_size; + +if (ipa_end < ipa_base) +return -EINVAL ; + +return populate_par_region (kvm, ipa_base, ipa_end); +} + static int set_ipa_state(struct kvm_vcpu *vcpu, unsigned long ipa, unsigned long end,@@ -748,6 +1102,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 kvm_cap_arm_rme_populate_realm_args 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 ;
Re: [RFC PATCH 16/28] arm64: RME: Allow populating initial contents
Quoted 6 lines · Zhi Wang
Quoted 3 lines · On Fri, 10 Mar 2023 15:47:16 +0000 Steven Price <[email protected]> wrote:
Quoted 2 lines · Zhi Wang
On Fri, 27 Jan 2023 11:29:20 +0000
Steven Price <[email protected]> wrote:
<snip>
Quoted 39 lines
Quoted 6 lines
Quoted 5 lines
arch/arm64/kvm/rme.c void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size) · line 768
Quoted 2 lines
arch/arm64/kvm/rme.c +2
-0+if (kvm_realm_state (kvm) !=REALM_STATE_NEW ) +return -EBUSY ;Maybe
-EINVAL? The realm hasn't been created (RMI_REALM_CREATEis not called yet). The userspace shouldn't reach this path.Quoted 9 lines
Well user space can attempt to populate in the ACTIVE state - which is where the idea of 'busy' comes from. Admittedly it's a little confusing when
RMI_REALM_CREATEhasn't been called.I'm not particularly bothered about the return code, but it's useful to have a different code to
-EINVALas it's not an invalid argument, but calling at the wrong time. I can't immediately see a better error code though.The reason why I feel
-EBUSYis little bit off is EBUSY usually indicates something is already initialized and currently running, then another calling path wanna to operate it.I took a look on the ioctls in
arch/arm64/kvm/arm.c. It seems people have different opinions for calling execution path at a wrong time:For example:
long
kvm_arch_vcpu_ioctl()
...case KVM_GET_REG_LIST: { struct kvm_reg_list __user *user_list = argp; struct kvm_reg_list reg_list; unsigned n; r = -ENOEXEC; if (unlikely(!kvm_vcpu_initialized(vcpu))) break; r = -EPERM; if (!kvm_arm_vcpu_is_finalized(vcpu)) break;
Quoted 2 lines
If we have to choose one, I prefer
-ENOEXECas-EPERMis stranger. But personally my vote goes to-EINVAL.
Ok, I think you've convinced me - I'll change to -EINVAL. It is invalid use of the API and none of the other error codes seem a great fit.
Although I do wish Linux had more descriptive error codes - I often end up peppering the kernel with a few printks when using a new API to find out what I'm doing wrong.
Steve
Quoted 41 lines
Quoted 40 lines
Steve
Quoted 37 lines
Quoted 36 lines
arch/arm64/kvm/rme.c +26
-0+ +if (!IS_ALIGNED (args->populate_ipa_base,PAGE_SIZE ) || + !IS_ALIGNED (args->populate_ipa_size,PAGE_SIZE )) +return -EINVAL ; + + ipa_base = args->populate_ipa_base; + ipa_end = ipa_base + args->populate_ipa_size; + +if (ipa_end < ipa_base) +return -EINVAL ; + +return populate_par_region (kvm, ipa_base, ipa_end); +} + static int set_ipa_state(struct kvm_vcpu *vcpu, unsigned long ipa, unsigned long end,@@ -748,6 +1102,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 kvm_cap_arm_rme_populate_realm_args 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 ;
RFC PATCH 17/28 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.
--- arch/arm64/include/asm/kvm_emulate.h | 10 +++++ arch/arm64/include/asm/kvm_rme.h | 12 ++++++ arch/arm64/kvm/mmu.c | 64 +++++++++++++++++++++++++--- arch/arm64/kvm/rme.c | 48 +++++++++++++++++++++ 4 files changed, 128 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +10 -0
@@ -502,6 +502,16 @@ static inline enum realm_state kvm_realm_state(struct kvm *kvm)return READ_ONCE (kvm->arch.realm.state); } +static inline gpa_t kvm_gpa_stolen_bits (struct kvm *kvm) +{ +if (kvm_is_realm (kvm)) { +struct realm *realm = &kvm->arch.realm; + +return BIT (realm->ia_bits -1 ); + } +return 0 ; +} +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 +12 -0
@@ -50,6 +50,18 @@ void kvm_destroy_rec(struct kvm_vcpu *vcpu);int kvm_rec_enter (struct kvm_vcpu *vcpu);int handle_rme_exit (struct kvm_vcpu *vcpu,int rec_run_status); +void kvm_realm_unmap_range (struct kvm *kvm,unsigned long ipa,u64 size); +int realm_map_protected (struct realm *realm, +unsigned long hva, +unsigned long base_ipa, +struct page *dst_page, +unsigned long map_size, +struct kvm_mmu_memory_cache *memcache); +int realm_map_non_secure (struct realm *realm, +unsigned long ipa, +struct page *page, +unsigned long map_size, +struct kvm_mmu_memory_cache *memcache);int realm_set_ipa_state (struct kvm_vcpu *vcpu,unsigned long addr,unsigned long end,unsigned long ripas);
arch/arm64/kvm/mmu.c +58 -6
151 lines not on this page · raw message
arch/arm64/kvm/rme.c +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.34.1