Skip to contentSkip to conversation

arm64: Support for Arm CCA in KVM

PATCH Superseded Steven Price 10 participants 164 messages May 13 – Jul 15 Page 1 of 2

Subsystems (4)

v14 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v14 cover arm64: Support for Arm CCA in KVM

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

This is rebased on v7.1-rc1, but still targets RMM v2.0-bet1[1].

The major updates from v13 remain but have been more fully implemented:
the RMM uses the host's page size, range based RMI APIs mean we don't have to break everything down to base page sizes, the GIC state is passed via system registers, and the uAPI has been simplified.

The main changes since v13 are:

* The RMI definitions and wrappers have been fully updated for RMM v2.0-bet1. In particular the temporary RMM v1.0 SMC compatibility patch has been dropped.

* The PSCI completion ioctl has been removed. RMM v2.0-bet1 still requires the host to provide the target REC for PSCI calls which name another vCPU, but KVM now performs the RMI PSCI completion automatically before entering the REC again. Userspace no longer needs to issue KVM_ARM_VCPU_RMI_PSCI_COMPLETE. A future spec should remove the need for the host to provide the MPIDR mapping.

* The generic RMI init, RMM configuration, GPT setup, delegate/undelegate helpers and SRO infrastructure have moved out of KVM into arch/arm64/kernel/rmi.c. RMI is expected to be used by features outside KVM, so this code should be available even when KVM is not built.

* RMI_GRANULE_TRACKING_GET has been updated to work on a range, this allows it to work when the region is not aligned to the tracking size. Solves the problem reported by Mathieu[2].

* SRO support has been moved earlier in the series and improved. It provides a cleaner way for the host to provide the RMM with the extra memory it requires. However support is still incomplete where the TF-RMM code does not yet implement it. This is noted by FIXMEs in the code.

* The ARM VM type encoding has been reworked to coexist with the upstream pKVM KVM_VM_TYPE_ARM_PROTECTED bit.

* The private-memory documentation now notes that arm64 uses KVM_CAP_MEMORY_ATTRIBUTES.

* PMU support is dropped for now. It will be added later in a separate series. Similarly for selecting the hash algorithm and RPV.

There are also the usual rebase updates and smaller fixes, including changes to the RMM v2.0-bet1 range APIs, removal of REC auxiliary granule handling, fixes to the address range descriptor encoding, and cleanups around realm stage-2 teardown.

Stateful RMI Operations
-----------------------

The RMM v2.0 spec introduces Stateful RMI Operations (SROs), which allow the RMM to complete an operation over several SMC calls while requesting or returning memory to the host. This allows interrupts to be handled in the middle of an operation and lets the RMM dynamically allocate memory for internal tracking purposes. For example, RMI_REC_CREATE no longer needs auxiliary granules to be provided up front, and can instead request memory during the operation.

This series includes the generic SRO infrastructure in arch/arm64/kernel/rmi.c and uses it for REC create/destroy. The other cases are not yet used by TF-RMM and a future revision will be needed to finish those paths in Linux.

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

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

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

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

The TF-RMM has not yet merged the RMM v2.0 support, so you will need to use a branch with RMM v2.0-bet1 support. At the time of writing the following branch is being used:

https://git.trustedfirmware.org/TF-RMM/tf-rmm.git topics/rmm-v2.0-poc_2 (tested on commit 3340667a291a)

There is a kvm-unit-test branch which has been updated to support the attestation used in RMMv2.0 available here:

https://gitlab.arm.com/linux-arm/kvm-unit-tests-cca cca/v4

[1] https://developer.arm.com/documentation/den0137/2-0bet1/
[2] https://lore.kernel.org/all/acrj-cKphy4hJsEG@p14s/

Jean-Philippe Brucker (6):
arm64: RMI: Propagate number of breakpoints and watchpoints to
userspace
arm64: RMI: Set breakpoint parameters through SET_ONE_REG
arm64: RMI: Propagate max SVE vector length from RMM
arm64: RMI: Configure max SVE vector length for a Realm
arm64: RMI: Provide register list for unfinalized RMI RECs
arm64: RMI: Provide accurate register list

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

Steven Price (33):
kvm: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
arm64: RME: Handle Granule Protection Faults (GPFs)
arm64: RMI: Add SMC definitions for calling the RMM
arm64: RMI: Add wrappers for RMI calls
arm64: RMI: Check for RMI support at init
arm64: RMI: Configure the RMM with the host's page size
arm64: RMI: Ensure that the RMM has GPT entries for memory
arm64: RMI: Provide functions to delegate/undelegate ranges of memory
arm64: RMI: Add support for SRO
arm64: RMI: Check for RMI support at KVM init
arm64: RMI: Check for LPA2 support
arm64: RMI: Define the user ABI
arm64: RMI: Basic infrastructure for creating a realm.
KVM: arm64: Allow passing machine type in KVM creation
arm64: RMI: RTT tear down
arm64: RMI: Activate realm on first VCPU run
arm64: RMI: Allocate/free RECs to match vCPUs
arm64: RMI: Support for the VGIC in realms
KVM: arm64: Support timers in realm RECs
arm64: RMI: Handle realm enter/exit
arm64: RMI: Handle RMI_EXIT_RIPAS_CHANGE
KVM: arm64: Handle realm MMIO emulation
KVM: arm64: Expose support for private memory
arm64: RMI: Allow populating initial contents
arm64: RMI: Set RIPAS of initial memslots
arm64: RMI: Create the realm descriptor
arm64: RMI: 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: RMI: Prevent Device mappings for Realms
arm64: RMI: Enable realms to be created

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

 Documentation/virt/kvm/api.rst       |   62 +-
 arch/arm64/include/asm/kvm_emulate.h |   37 +
 arch/arm64/include/asm/kvm_host.h    |   13 +-
 arch/arm64/include/asm/kvm_pgtable.h |    5 +-
 arch/arm64/include/asm/kvm_pkvm.h    |    2 +-
 arch/arm64/include/asm/kvm_rmi.h     |  127 +++
 arch/arm64/include/asm/rmi_cmds.h    |  680 +++++++++++++
 arch/arm64/include/asm/rmi_smc.h     |  448 ++++++++
 arch/arm64/include/asm/virt.h        |    1 +
 arch/arm64/kernel/Makefile           |    2 +-
 arch/arm64/kernel/cpufeature.c       |    1 +
 arch/arm64/kernel/rmi.c              |  605 +++++++++++
 arch/arm64/kvm/Kconfig               |    2 +
 arch/arm64/kvm/Makefile              |    2 +-
 arch/arm64/kvm/arch_timer.c          |   28 +-
 arch/arm64/kvm/arm.c                 |  140 ++-
 arch/arm64/kvm/guest.c               |   93 +-
 arch/arm64/kvm/hyp/pgtable.c         |    1 +
 arch/arm64/kvm/hypercalls.c          |    4 +-
 arch/arm64/kvm/inject_fault.c        |    5 +-
 arch/arm64/kvm/mmio.c                |   16 +-
 arch/arm64/kvm/mmu.c                 |  197 +++-
 arch/arm64/kvm/psci.c                |   15 +-
 arch/arm64/kvm/reset.c               |   13 +-
 arch/arm64/kvm/rmi-exit.c            |  215 ++++
 arch/arm64/kvm/rmi.c                 | 1401 ++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c            |   47 +-
 arch/arm64/kvm/vgic/vgic-init.c      |    2 +-
 arch/arm64/mm/fault.c                |   28 +-
 include/kvm/arm_arch_timer.h         |    2 +
 include/kvm/arm_psci.h               |    2 +
 include/uapi/linux/kvm.h             |   20 +-
 32 files changed, 4122 insertions(+), 94 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_rmi.h
 create mode 100644 arch/arm64/include/asm/rmi_cmds.h
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
 create mode 100644 arch/arm64/kernel/rmi.c
 create mode 100644 arch/arm64/kvm/rmi-exit.c
 create mode 100644 arch/arm64/kvm/rmi.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v14 1/44 kvm: arm64: Include kvm_emulate.h in kvm/arm_psci.h

From: Suzuki K Poulose <[email protected]>

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

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

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

Re: [PATCH v14 01/44] kvm: arm64: Include kvm_emulate.h in kvm/arm_psci.h

Quoted 10 lines · Steven Price

From: Suzuki K Poulose <[email protected]>

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

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

Quoted 7 lines
   29 |         if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2)) {
	         |             ^~~~~~~~~~~~~~~~
			       |             cpu_have_feature

Unrelated to this patch, but really easy to fix: the standard prefix for patches targeting KVM/arm64 is:

"KVM: arm64: [opt subsys:] Something starting with a capital letter"

where "opt subsys" could be "CCA" where applicable.

It'd be good to have some consistency.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#3 lore raw

Re: [PATCH v14 01/44] kvm: arm64: Include kvm_emulate.h in kvm/arm_psci.h

Quoted 20 lines · Marc Zyngier
Quoted 10 lines · Steven Price

From: Suzuki K Poulose <[email protected]>

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

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

Quoted 7 lines
   29 |         if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2)) {
	         |             ^~~~~~~~~~~~~~~~
			       |             cpu_have_feature
Quoted 8 lines

Unrelated to this patch, but really easy to fix: the standard prefix for patches targeting KVM/arm64 is:

"KVM: arm64: [opt subsys:] Something starting with a capital letter"

where "opt subsys" could be "CCA" where applicable.

It'd be good to have some consistency.

Sure, I think back when I started this there wasn't great consistency so I picked up something from git log. I'm happy to change this for the next posting.

Thanks,
Steve

Quoted 5 lines

Thanks,

	M.
#4 lore raw

PATCH v14 2/44 kvm: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

In reply to Steven Price

To avoid future include cycles, drop the linux/kvm_host.h include in kvm_pgtable.h and include two _types.h headers for the types that are actually used. Additionally provide a forward declaration for struct kvm_s2_mmu as it's only used as a pointer in this file.

Both pgtable.c and kvm_pkvm.h relied on the indirect inclusion of kvm_host.h, so make that explicit.

---
New patch in v13

---
 arch/arm64/include/asm/kvm_pgtable.h | 5 ++++-
 arch/arm64/include/asm/kvm_pkvm.h    | 2 +-
 arch/arm64/kvm/hyp/pgtable.c         | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +4 -1
index 41a8687938eb..e4770ce2ccf6 100644
@@ -8,9 +8,12 @@
 #define __ARM64_KVM_PGTABLE_H__
 
 #include <linux/bits.h>
-#include <linux/kvm_host.h>
+#include <linux/kvm_types.h>
+#include <linux/rbtree_types.h>
 #include <linux/types.h>
 
+struct kvm_s2_mmu;
+
 #define KVM_PGTABLE_FIRST_LEVEL		-1
 #define KVM_PGTABLE_LAST_LEVEL		3
 
arch/arm64/include/asm/kvm_pkvm.h +1 -1
index 2954b311128c..1bc6a6a34ec9 100644
@@ -9,7 +9,7 @@
 #include <linux/arm_ffa.h>
 #include <linux/memblock.h>
 #include <linux/scatterlist.h>
-#include <asm/kvm_host.h>
+#include <linux/kvm_host.h>
 #include <asm/kvm_pgtable.h>
 
 /* Maximum number of VMs that can co-exist under pKVM. */
arch/arm64/kvm/hyp/pgtable.c +1 -0
index 0c1defa5fb0f..0bcd6f06aafb 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/kvm_host.h>
 #include <asm/kvm_pgtable.h>
 #include <asm/stage2_pgtable.h>
 
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH v14 02/44] kvm: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

Quoted 23 lines · Steven Price

To avoid future include cycles, drop the linux/kvm_host.h include in kvm_pgtable.h and include two _types.h headers for the types that are actually used. Additionally provide a forward declaration for struct kvm_s2_mmu as it's only used as a pointer in this file.

Both pgtable.c and kvm_pkvm.h relied on the indirect inclusion of kvm_host.h, so make that explicit.

---
New patch in v13

---
 arch/arm64/include/asm/kvm_pgtable.h | 5 ++++-
 arch/arm64/include/asm/kvm_pkvm.h    | 2 +-
 arch/arm64/kvm/hyp/pgtable.c         | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +0 -0
index 41a8687938eb..e4770ce2ccf6 100644

arch/arm64/include/asm/kvm_pgtable.h line 12

Quoted 6 lines
arch/arm64/include/asm/kvm_pgtable.h +2 -1
@@ -8,9 +8,12 @@
 #define __ARM64_KVM_PGTABLE_H__
 
 #include <linux/bits.h>
-#include <linux/kvm_host.h>
+#include <linux/kvm_types.h>
+#include <linux/rbtree_types.h>

I'm surprised by this. Where is the rbtree_type.h requirement coming from?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#6 lore raw

Re: [PATCH v14 02/44] kvm: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

Quoted 27 lines · Marc Zyngier
Quoted 25 lines · Steven Price

To avoid future include cycles, drop the linux/kvm_host.h include in kvm_pgtable.h and include two _types.h headers for the types that are actually used. Additionally provide a forward declaration for struct kvm_s2_mmu as it's only used as a pointer in this file.

Both pgtable.c and kvm_pkvm.h relied on the indirect inclusion of kvm_host.h, so make that explicit.

---
New patch in v13

---
 arch/arm64/include/asm/kvm_pgtable.h | 5 ++++-
 arch/arm64/include/asm/kvm_pkvm.h    | 2 +-
 arch/arm64/kvm/hyp/pgtable.c         | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +0 -0
index 41a8687938eb..e4770ce2ccf6 100644
@@ -8,9 +8,12 @@
 #define __ARM64_KVM_PGTABLE_H__
 
Quoted 7 lines

arch/arm64/include/asm/kvm_pgtable.h line 12

Quoted 4 lines
arch/arm64/include/asm/kvm_pgtable.h +2 -1
 #include <linux/bits.h>
-#include <linux/kvm_host.h>
+#include <linux/kvm_types.h>
+#include <linux/rbtree_types.h>

I'm surprised by this. Where is the rbtree_type.h requirement coming from?

struct kvm_pgtable has a "struct rb_root_cached" for pkvm_mappings. There's definitely an argument that that's a bit ugly - but this seemed the cleanest fix from a include perspective.

Thanks,
Steve

Quoted 5 lines

Thanks,

	M.
#7 lore raw

PATCH v14 3/44 arm64: RME: Handle Granule Protection Faults (GPFs)

In reply to Steven Price

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

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

---
Changes since v10:
* Don't call arm64_notify_die() in do_gpf() but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"

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

Re: [PATCH v14 03/44] arm64: RME: Handle Granule Protection Faults (GPFs)

Quoted 56 lines · Steven Price

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

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

---
Changes since v10:
* Don't call arm64_notify_die() in do_gpf() but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"

---
 arch/arm64/mm/fault.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
arch/arm64/mm/fault.c +16 -2
index 0f3c5c7ca054..6358ea4787ba 100644
@@ -905,6 +905,22 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,
 	return 0;
 }
 
+static int do_gpf_ptw(unsigned long far, unsigned long esr, struct pt_regs *regs)
+{
+	const struct fault_info *inf = esr_to_fault_info(esr);
+
+	die_kernel_fault(inf->name, far, esr, regs);
+	return 0;
+}
+
+static int do_gpf(unsigned long far, unsigned long esr, struct pt_regs *regs)
+{
+	if (!is_el1_instruction_abort(esr) && fixup_exception(regs, esr))
+		return 0;
+
+	return 1;
+}
+
 static const struct fault_info fault_info[] = {
 	{ do_bad,		SIGKILL, SI_KERNEL,	"ttbr address size fault"	},
 	{ do_bad,		SIGKILL, SI_KERNEL,	"level 1 address size fault"	},
@@ -941,12 +957,12 @@ static const struct fault_info fault_info[] = {
 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 32"			},
 	{ do_alignment_fault,	SIGBUS,  BUS_ADRALN,	"alignment fault"		},
 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 34"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 35"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 36"			},

arch/arm64/mm/fault.c static const struct fault_info fault_info[] = { · line 965

Quoted 10 lines
arch/arm64/mm/fault.c +6 -4
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 37"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 38"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 39"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 40"			},
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level -1" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 0" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 1" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 2" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 3" },
+	{ do_gpf,		SIGBUS,  SI_KERNEL,	"Granule Protection Fault not on table walk" },

It wouldn't hurt to align the textual description with what we have for other fault syndromes:

"level X granule protection fault (translation table walk)"

for the PTW-trigger faults, and

"granule protection fault"

for the non PTW case.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#9 lore raw

Re: [PATCH v14 03/44] arm64: RME: Handle Granule Protection Faults (GPFs)

Quoted 72 lines · Marc Zyngier
Quoted 56 lines · Steven Price

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

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

---
Changes since v10:
* Don't call arm64_notify_die() in do_gpf() but simply return 1.
Changes since v2:
* Include missing "Granule Protection Fault at level -1"

---
 arch/arm64/mm/fault.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
arch/arm64/mm/fault.c +16 -2
index 0f3c5c7ca054..6358ea4787ba 100644
@@ -905,6 +905,22 @@ static int do_tag_check_fault(unsigned long far, unsigned long esr,
 	return 0;
 }
 
+static int do_gpf_ptw(unsigned long far, unsigned long esr, struct pt_regs *regs)
+{
+	const struct fault_info *inf = esr_to_fault_info(esr);
+
+	die_kernel_fault(inf->name, far, esr, regs);
+	return 0;
+}
+
+static int do_gpf(unsigned long far, unsigned long esr, struct pt_regs *regs)
+{
+	if (!is_el1_instruction_abort(esr) && fixup_exception(regs, esr))
+		return 0;
+
+	return 1;
+}
+
 static const struct fault_info fault_info[] = {
 	{ do_bad,		SIGKILL, SI_KERNEL,	"ttbr address size fault"	},
 	{ do_bad,		SIGKILL, SI_KERNEL,	"level 1 address size fault"	},
@@ -941,12 +957,12 @@ static const struct fault_info fault_info[] = {
 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 32"			},
 	{ do_alignment_fault,	SIGBUS,  BUS_ADRALN,	"alignment fault"		},
 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 34"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 35"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 36"			},

arch/arm64/mm/fault.c static const struct fault_info fault_info[] = { · line 965

Quoted 10 lines
arch/arm64/mm/fault.c +6 -4
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 37"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 38"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 39"			},
-	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 40"			},
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level -1" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 0" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 1" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 2" },
+	{ do_gpf_ptw,		SIGKILL, SI_KERNEL,	"Granule Protection Fault at level 3" },
+	{ do_gpf,		SIGBUS,  SI_KERNEL,	"Granule Protection Fault not on table walk" },

It wouldn't hurt to align the textual description with what we have for other fault syndromes:

Quoted 7 lines

"level X granule protection fault (translation table walk)"

for the PTW-trigger faults, and

"granule protection fault"

for the non PTW case.

Sure, no problem.

Thanks,
Steve

Quoted 5 lines

Thanks,

	M.
#10 lore raw

PATCH v14 4/44 arm64: RMI: Add SMC definitions for calling the RMM

In reply to Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

---
 arch/arm64/include/asm/rmi_smc.h | 448 +++++++++++++++++++++++++++++++
 1 file changed, 448 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +448 -0
new file mode 100644
index 000000000000..a09b7a631fef
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/
+ */
+
+#ifndef __ASM_RMI_SMC_H
+#define __ASM_RMI_SMC_H
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RMI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD,	\
+			   (func))
+
+#define SMC_RMI_VERSION				SMC_RMI_CALL(0x0150)
+
+#define SMC_RMI_RTT_DATA_MAP_INIT		SMC_RMI_CALL(0x0153)
+
+#define SMC_RMI_REALM_ACTIVATE			SMC_RMI_CALL(0x0157)
+#define SMC_RMI_REALM_CREATE			SMC_RMI_CALL(0x0158)
+#define SMC_RMI_REALM_DESTROY			SMC_RMI_CALL(0x0159)
+#define SMC_RMI_REC_CREATE			SMC_RMI_CALL(0x015a)
+#define SMC_RMI_REC_DESTROY			SMC_RMI_CALL(0x015b)
+#define SMC_RMI_REC_ENTER			SMC_RMI_CALL(0x015c)
+#define SMC_RMI_RTT_CREATE			SMC_RMI_CALL(0x015d)
+#define SMC_RMI_RTT_DESTROY			SMC_RMI_CALL(0x015e)
+
+#define SMC_RMI_RTT_READ_ENTRY			SMC_RMI_CALL(0x0161)
+
+#define SMC_RMI_RTT_DEV_VALIDATE		SMC_RMI_CALL(0x0163)
+#define SMC_RMI_PSCI_COMPLETE			SMC_RMI_CALL(0x0164)
+#define SMC_RMI_FEATURES			SMC_RMI_CALL(0x0165)
+#define SMC_RMI_RTT_FOLD			SMC_RMI_CALL(0x0166)
+
+#define SMC_RMI_RTT_INIT_RIPAS			SMC_RMI_CALL(0x0168)
+#define SMC_RMI_RTT_SET_RIPAS			SMC_RMI_CALL(0x0169)
+#define SMC_RMI_VSMMU_CREATE			SMC_RMI_CALL(0x016a)
+#define SMC_RMI_VSMMU_DESTROY			SMC_RMI_CALL(0x016b)
+#define SMC_RMI_RMM_CONFIG_SET			SMC_RMI_CALL(0x016e)
+#define SMC_RMI_PSMMU_IRQ_NOTIFY		SMC_RMI_CALL(0x016f)
+
+#define SMC_RMI_PDEV_ABORT			SMC_RMI_CALL(0x0174)
+#define SMC_RMI_PDEV_COMMUNICATE		SMC_RMI_CALL(0x0175)
+#define SMC_RMI_PDEV_CREATE			SMC_RMI_CALL(0x0176)
+#define SMC_RMI_PDEV_DESTROY			SMC_RMI_CALL(0x0177)
+#define SMC_RMI_PDEV_GET_STATE			SMC_RMI_CALL(0x0178)
+
+#define SMC_RMI_PDEV_STREAM_KEY_REFRESH		SMC_RMI_CALL(0x017a)
+#define SMC_RMI_PDEV_SET_PUBKEY			SMC_RMI_CALL(0x017b)
+#define SMC_RMI_PDEV_STOP			SMC_RMI_CALL(0x017c)
+#define SMC_RMI_RTT_AUX_CREATE			SMC_RMI_CALL(0x017d)
+#define SMC_RMI_RTT_AUX_DESTROY			SMC_RMI_CALL(0x017e)
+#define SMC_RMI_RTT_AUX_FOLD			SMC_RMI_CALL(0x017f)
+
+#define SMC_RMI_VDEV_ABORT			SMC_RMI_CALL(0x0185)
+#define SMC_RMI_VDEV_COMMUNICATE		SMC_RMI_CALL(0x0186)
+#define SMC_RMI_VDEV_CREATE			SMC_RMI_CALL(0x0187)
+#define SMC_RMI_VDEV_DESTROY			SMC_RMI_CALL(0x0188)
+#define SMC_RMI_VDEV_GET_STATE			SMC_RMI_CALL(0x0189)
+#define SMC_RMI_VDEV_UNLOCK			SMC_RMI_CALL(0x018a)
+#define SMC_RMI_RTT_SET_S2AP			SMC_RMI_CALL(0x018b)
+#define SMC_RMI_VDEV_COMPLETE			SMC_RMI_CALL(0x018e)
+
+#define SMC_RMI_VDEV_GET_INTERFACE_REPORT	SMC_RMI_CALL(0x01d0)
+#define SMC_RMI_VDEV_GET_MEASUREMENTS		SMC_RMI_CALL(0x01d1)
+#define SMC_RMI_VDEV_LOCK			SMC_RMI_CALL(0x01d2)
+#define SMC_RMI_VDEV_START			SMC_RMI_CALL(0x01d3)
+
+#define SMC_RMI_VSMMU_EVENT_NOTIFY		SMC_RMI_CALL(0x01d6)
+#define SMC_RMI_PSMMU_ACTIVATE			SMC_RMI_CALL(0x01d7)
+#define SMC_RMI_PSMMU_DEACTIVATE		SMC_RMI_CALL(0x01d8)
+
+#define SMC_RMI_PSMMU_ST_L2_CREATE		SMC_RMI_CALL(0x01db)
+#define SMC_RMI_PSMMU_ST_L2_DESTROY		SMC_RMI_CALL(0x01dc)
+#define SMC_RMI_DPT_L0_CREATE			SMC_RMI_CALL(0x01dd)
+#define SMC_RMI_DPT_L0_DESTROY			SMC_RMI_CALL(0x01de)
+#define SMC_RMI_DPT_L1_CREATE			SMC_RMI_CALL(0x01df)
+#define SMC_RMI_DPT_L1_DESTROY			SMC_RMI_CALL(0x01e0)
+#define SMC_RMI_GRANULE_TRACKING_GET		SMC_RMI_CALL(0x01e1)
+
+#define SMC_RMI_GRANULE_TRACKING_SET		SMC_RMI_CALL(0x01e3)
+
+#define SMC_RMI_RMM_CONFIG_GET			SMC_RMI_CALL(0x01ec)
+
+#define SMC_RMI_RMM_STATE_GET			SMC_RMI_CALL(0x01ee)
+
+#define SMC_RMI_PSMMU_EVENT_CONSUME		SMC_RMI_CALL(0x01f0)
+#define SMC_RMI_GRANULE_RANGE_DELEGATE		SMC_RMI_CALL(0x01f1)
+#define SMC_RMI_GRANULE_RANGE_UNDELEGATE	SMC_RMI_CALL(0x01f2)
+#define SMC_RMI_GPT_L1_CREATE			SMC_RMI_CALL(0x01f3)
+#define SMC_RMI_GPT_L1_DESTROY			SMC_RMI_CALL(0x01f4)
+#define SMC_RMI_RTT_DATA_MAP			SMC_RMI_CALL(0x01f5)
+#define SMC_RMI_RTT_DATA_UNMAP			SMC_RMI_CALL(0x01f6)
+#define SMC_RMI_RTT_DEV_MAP			SMC_RMI_CALL(0x01f7)
+#define SMC_RMI_RTT_DEV_UNMAP			SMC_RMI_CALL(0x01f8)
+#define SMC_RMI_RTT_ARCH_DEV_MAP		SMC_RMI_CALL(0x01f9)
+#define SMC_RMI_RTT_ARCH_DEV_UNMAP		SMC_RMI_CALL(0x01fa)
+#define SMC_RMI_RTT_UNPROT_MAP			SMC_RMI_CALL(0x01fb)
+#define SMC_RMI_RTT_UNPROT_UNMAP		SMC_RMI_CALL(0x01fc)
+#define SMC_RMI_RTT_AUX_PROT_MAP		SMC_RMI_CALL(0x01fd)
+#define SMC_RMI_RTT_AUX_PROT_UNMAP		SMC_RMI_CALL(0x01fe)
+#define SMC_RMI_RTT_AUX_UNPROT_MAP		SMC_RMI_CALL(0x01ff)
+#define SMC_RMI_RTT_AUX_UNPROT_UNMAP		SMC_RMI_CALL(0x0200)
+#define SMC_RMI_REALM_TERMINATE			SMC_RMI_CALL(0x0201)
+#define SMC_RMI_RMM_ACTIVATE			SMC_RMI_CALL(0x0202)
+#define SMC_RMI_OP_CONTINUE			SMC_RMI_CALL(0x0203)
+#define SMC_RMI_PDEV_STREAM_CONNECT		SMC_RMI_CALL(0x0204)
+#define SMC_RMI_PDEV_STREAM_DISCONNECT		SMC_RMI_CALL(0x0205)
+#define SMC_RMI_PDEV_STREAM_COMPLETE		SMC_RMI_CALL(0x0206)
+#define SMC_RMI_PDEV_STREAM_KEY_PURGE		SMC_RMI_CALL(0x0207)
+#define SMC_RMI_OP_MEM_DONATE			SMC_RMI_CALL(0x0208)
+#define SMC_RMI_OP_MEM_RECLAIM			SMC_RMI_CALL(0x0209)
+#define SMC_RMI_OP_CANCEL			SMC_RMI_CALL(0x020a)
+#define SMC_RMI_VSMMU_FEATURES			SMC_RMI_CALL(0x020b)
+#define SMC_RMI_VSMMU_CMD_GET			SMC_RMI_CALL(0x020c)
+#define SMC_RMI_VSMMU_CMD_COMPLETE		SMC_RMI_CALL(0x020d)
+#define SMC_RMI_PSMMU_INFO			SMC_RMI_CALL(0x020e)
+
+#define RMI_ABI_MAJOR_VERSION	2
+#define RMI_ABI_MINOR_VERSION	0
+
+#define RMI_ABI_VERSION_GET_MAJOR(version) ((version) >> 16)
+#define RMI_ABI_VERSION_GET_MINOR(version) ((version) & 0xFFFF)
+#define RMI_ABI_VERSION(major, minor)      (((major) << 16) | (minor))
+
+#define RMI_UNASSIGNED			0
+#define RMI_ASSIGNED			1
+#define RMI_TABLE			2
+
+#define RMI_RETURN_STATUS(ret)		((ret) & 0xFF)
+#define RMI_RETURN_INDEX(ret)		(((ret) >> 8) & 0xFF)
+#define RMI_RETURN_MEMREQ(ret)		(((ret) >> 8) & 0x3)
+#define RMI_RETURN_CAN_CANCEL(ret)	(((ret) >> 10) & 0x1)
+
+#define RMI_SUCCESS			0
+#define RMI_ERROR_INPUT			1
+#define RMI_ERROR_REALM			2
+#define RMI_ERROR_REC			3
+#define RMI_ERROR_RTT			4
+#define RMI_ERROR_NOT_SUPPORTED		5
+#define RMI_ERROR_DEVICE		6
+#define RMI_ERROR_RTT_AUX		7
+#define RMI_ERROR_PSMMU_ST		8
+#define RMI_ERROR_DPT			9
+#define RMI_BUSY			10
+#define RMI_ERROR_GLOBAL		11
+#define RMI_ERROR_TRACKING		12
+#define RMI_INCOMPLETE			13
+#define RMI_BLOCKED			14
+#define RMI_ERROR_GPT			15
+#define RMI_ERROR_GRANULE		16
+
+#define RMI_OP_MEM_REQ_NONE		0
+#define RMI_OP_MEM_REQ_DONATE		1
+#define RMI_OP_MEM_REQ_RECLAIM		2
+
+#define RMI_DONATE_SIZE(req)		((req) & 0x3)
+#define RMI_DONATE_COUNT_MASK		GENMASK(15, 2)
+#define RMI_DONATE_COUNT(req)		(((req) & RMI_DONATE_COUNT_MASK) >> 2)
+#define RMI_DONATE_CONTIG(req)		(!!((req) & BIT(16)))
+#define RMI_DONATE_STATE(req)		(!!((req) & BIT(17)))
+
+#define RMI_OP_MEM_DELEGATED		0
+#define RMI_OP_MEM_UNDELEGATED		1
+
+#define RMI_ADDR_TYPE_NONE		0
+#define RMI_ADDR_TYPE_SINGLE		1
+#define RMI_ADDR_TYPE_LIST		2
+
+#define RMI_ADDR_RANGE_SIZE_MASK	GENMASK(1, 0)
+#define RMI_ADDR_RANGE_COUNT_MASK	GENMASK(PAGE_SHIFT - 1, 2)
+#define RMI_ADDR_RANGE_ADDR_MASK	(PAGE_MASK & GENMASK(51, 0))
+#define RMI_ADDR_RANGE_STATE_MASK	BIT(63)
+
+#define RMI_ADDR_RANGE_SIZE(ar)		(FIELD_GET(RMI_ADDR_RANGE_SIZE_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_COUNT(ar)	(FIELD_GET(RMI_ADDR_RANGE_COUNT_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_ADDR(ar)		((ar) & RMI_ADDR_RANGE_ADDR_MASK)
+#define RMI_ADDR_RANGE_STATE(ar)	(FIELD_GET(RMI_ADDR_RANGE_STATE_MASK, \
+						   (ar)))
+
+enum rmi_ripas {
+	RMI_EMPTY = 0,
+	RMI_RAM = 1,
+	RMI_DESTROYED = 2,
+	RMI_DEV = 3,
+};
+
+#define RMI_NO_MEASURE_CONTENT	0
+#define RMI_MEASURE_CONTENT	1
+
+#define RMI_FEATURE_REGISTER_0_S2SZ		GENMASK(7, 0)
+#define RMI_FEATURE_REGISTER_0_LPA2		BIT(8)
+#define RMI_FEATURE_REGISTER_0_SVE		BIT(9)
+#define RMI_FEATURE_REGISTER_0_SVE_VL		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_0_NUM_BPS		GENMASK(19, 14)
+#define RMI_FEATURE_REGISTER_0_NUM_WPS		GENMASK(25, 20)
+#define RMI_FEATURE_REGISTER_0_PMU		BIT(26)
+#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS	GENMASK(31, 27)
+
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB	BIT(0)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB	BIT(1)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB	BIT(2)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_256	BIT(3)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_384	BIT(4)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_512	BIT(5)
+#define RMI_FEATURE_REGISTER_1_MAX_RECS_ORDER	GENMASK(9, 6)
+#define RMI_FEATURE_REGISTER_1_L0GPTSZ		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_1_PPS		GENMASK(16, 14)
+
+#define RMI_FEATURE_REGISTER_2_DA		BIT(0)
+#define RMI_FEATURE_REGISTER_2_DA_COH		BIT(1)
+#define RMI_FEATURE_REGISTER_2_VSMMU		BIT(2)
+#define RMI_FEATURE_REGISTER_2_ATS		BIT(3)
+#define RMI_FEATURE_REGISTER_2_MAX_VDEVS_ORDER	GENMASK(7, 4)
+#define RMI_FEATURE_REGISTER_2_VDEV_KROU	BIT(8)
+#define RMI_FEATURE_REGISTER_2_NON_TEE_STREAM	BIT(9)
+
+#define RMI_FEATURE_REGISTER_3_MAX_NUM_AUX_PLANES	GENMASK(3, 0)
+#define RMI_FEATURE_REGISTER_3_RTT_PLAN			GENMASK(5, 4)
+#define RMI_FEATURE_REGISTER_3_RTT_S2AP_INDIRECT	BIT(6)
+
+#define RMI_FEATURE_REGISTER_4_MEC_COUNT		GENMASK(63, 0)
+
+#define RMI_MEM_CATEGORY_CONVENTIONAL		0
+#define RMI_MEM_CATEGORY_DEV_NCOH		1
+#define RMI_MEM_CATEGORY_DEV_COH		2
+
+#define RMI_TRACKING_RESERVED			0
+#define RMI_TRACKING_NONE			1
+#define RMI_TRACKING_FINE			2
+#define RMI_TRACKING_COARSE			3
+
+#define RMI_GRANULE_SIZE_4KB	0
+#define RMI_GRANULE_SIZE_16KB	1
+#define RMI_GRANULE_SIZE_64KB	2
+
+/*
+ * Note many of these fields are smaller than u64 but all fields have u64
+ * alignment, so use u64 to ensure correct alignment.
+ */
+struct rmm_config {
+	union { /* 0x0 */
+		struct {
+			u64 tracking_region_size;
+			u64 rmi_granule_size;
+		};
+		u8 sizer[0x1000];
+	};
+};
+
+#define RMI_REALM_PARAM_FLAG_LPA2		BIT(0)
+#define RMI_REALM_PARAM_FLAG_SVE		BIT(1)
+#define RMI_REALM_PARAM_FLAG_PMU		BIT(2)
+
+struct realm_params {
+	union { /* 0x0 */
+		struct {
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+			u64 num_aux_planes;
+		};
+		u8 padding0[0x400];
+	};
+	union { /* 0x400 */
+		struct {
+			u8 rpv[64];
+			u64 ats_plane;
+		};
+		u8 padding1[0x400];
+	};
+	union { /* 0x800 */
+		struct {
+			u64 padding;
+			u64 rtt_base;
+			s64 rtt_level_start;
+			u64 rtt_num_start;
+			u64 flags1;
+			u64 aux_rtt_base[3];
+		};
+		u8 padding2[0x800];
+	};
+};
+
+/*
+ * The number of GPRs (starting from X0) that are
+ * configured by the host when a REC is created.
+ */
+#define REC_CREATE_NR_GPRS		8
+
+#define REC_PARAMS_FLAG_RUNNABLE	BIT_ULL(0)
+
+struct rec_params {
+	union { /* 0x0 */
+		u64 flags;
+		u8 padding0[0x100];
+	};
+	union { /* 0x100 */
+		u64 mpidr;
+		u8 padding1[0x100];
+	};
+	union { /* 0x200 */
+		u64 pc;
+		u8 padding2[0x100];
+	};
+	union { /* 0x300 */
+		u64 gprs[REC_CREATE_NR_GPRS];
+		u8 padding3[0xd00];
+	};
+};
+
+#define REC_ENTER_FLAG_EMULATED_MMIO	BIT(0)
+#define REC_ENTER_FLAG_INJECT_SEA	BIT(1)
+#define REC_ENTER_FLAG_TRAP_WFI		BIT(2)
+#define REC_ENTER_FLAG_TRAP_WFE		BIT(3)
+#define REC_ENTER_FLAG_RIPAS_RESPONSE	BIT(4)
+#define REC_ENTER_FLAG_S2AP_RESPONSE	BIT(5)
+#define REC_ENTER_FLAG_DEV_MEM_RESPONSE	BIT(6)
+#define REC_ENTER_FLAG_FORCE_P0		BIT(7)
+
+#define REC_RUN_GPRS			31
+#define REC_MAX_GIC_NUM_LRS		16
+
+#define RMI_PERMITTED_GICV3_HCR_BITS	(ICH_HCR_EL2_UIE |		\
+					 ICH_HCR_EL2_LRENPIE |		\
+					 ICH_HCR_EL2_NPIE |		\
+					 ICH_HCR_EL2_VGrp0EIE |		\
+					 ICH_HCR_EL2_VGrp0DIE |		\
+					 ICH_HCR_EL2_VGrp1EIE |		\
+					 ICH_HCR_EL2_VGrp1DIE |		\
+					 ICH_HCR_EL2_TDIR)
+
+struct rec_enter {
+	union { /* 0x000 */
+		u64 flags;
+		u8 padding0[0x200];
+	};
+	union { /* 0x200 */
+		u64 gprs[REC_RUN_GPRS];
+		u8 padding1[0x100];
+	};
+	u8 padding3[0x500];
+};
+
+#define RMI_EXIT_SYNC			0x00
+#define RMI_EXIT_IRQ			0x01
+#define RMI_EXIT_FIQ			0x02
+#define RMI_EXIT_PSCI			0x03
+#define RMI_EXIT_RIPAS_CHANGE		0x04
+#define RMI_EXIT_HOST_CALL		0x05
+#define RMI_EXIT_SERROR			0x06
+#define RMI_EXIT_S2AP_CHANGE		0x07
+#define RMI_EXIT_VDEV_REQUEST		0x08
+#define RMI_EXIT_VDEV_VALIDATE_MAPPING	0x09
+#define RMI_EXIT_VSMMU_COMMAND		0x0a
+
+struct rec_exit {
+	union { /* 0x000 */
+		u8 exit_reason;
+		u8 padding0[0x100];
+	};
+	union { /* 0x100 */
+		struct {
+			u64 esr;
+			u64 far;
+			u64 hpfar;
+			u64 rtt_tree;
+		};
+		u8 padding1[0x100];
+	};
+	union { /* 0x200 */
+		u64 gprs[REC_RUN_GPRS];
+		u8 padding2[0x100];
+	};
+	union { /* 0x300 */
+		u8 padding3[0x100];
+	};
+	union { /* 0x400 */
+		struct {
+			u64 cntp_ctl;
+			u64 cntp_cval;
+			u64 cntv_ctl;
+			u64 cntv_cval;
+		};
+		u8 padding4[0x100];
+	};
+	union { /* 0x500 */
+		struct {
+			u64 ripas_base;
+			u64 ripas_top;
+			u8 ripas_value;
+			u8 padding8[15];
+			u64 s2ap_base;
+			u64 s2ap_top;
+			u64 vdev_id_1;
+			u64 vdev_id_2;
+			u64 dev_mem_base;
+			u64 dev_mem_top;
+			u64 dev_mem_pa;
+		};
+		u8 padding5[0x100];
+	};
+	union { /* 0x600 */
+		struct {
+			u16 imm;
+			u16 padding9;
+			u64 plane;
+		};
+		u8 padding6[0x100];
+	};
+	union { /* 0x700 */
+		struct {
+			u8 pmu_ovf_status;
+			u8 padding10[15];
+			u64 vsmmu;
+		};
+		u8 padding7[0x100];
+	};
+};
+
+struct rec_run {
+	struct rec_enter enter;
+	struct rec_exit exit;
+};
+
+/* RMI_RTT_UNPROT_MAP_FLAGS definitions */
+#define RMI_RTT_UNPROT_MAP_FLAGS_OADDR_TYPE	GENMASK(1, 0)
+#define RMI_RTT_UNPROT_MAP_FLAGS_LIST_COUNT	GENMASK(15, 2)
+#define RMI_RTT_UNPROT_MAP_FLAGS_MEMATTR	GENMASK(18, 16)
+#define RMI_RTT_UNPROT_MAP_FLAGS_S2AP		GENMASK(22, 19)
+
+/* S2AP Direct Encodings, used in RMI_RTT_UNPROT_MAP_FLAGS_S2AP */
+#define RMI_S2AP_DIRECT_WRITE			BIT(0)
+#define RMI_S2AP_DIRECT_READ			BIT(1)
+
+#endif /* __ASM_RMI_SMC_H */
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v14 04/44] arm64: RMI: Add SMC definitions for calling the RMM

Hi Steven,

Quoted 175 lines · Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

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

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

arch/arm64/include/asm/rmi_smc.h new file +125 -0
new file mode 100644
index 000000000000..a09b7a631fef
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/
+ */
+
+#ifndef __ASM_RMI_SMC_H
+#define __ASM_RMI_SMC_H
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RMI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD,	\
+			   (func))
+
+#define SMC_RMI_VERSION				SMC_RMI_CALL(0x0150)
+
+#define SMC_RMI_RTT_DATA_MAP_INIT		SMC_RMI_CALL(0x0153)
+
+#define SMC_RMI_REALM_ACTIVATE			SMC_RMI_CALL(0x0157)
+#define SMC_RMI_REALM_CREATE			SMC_RMI_CALL(0x0158)
+#define SMC_RMI_REALM_DESTROY			SMC_RMI_CALL(0x0159)
+#define SMC_RMI_REC_CREATE			SMC_RMI_CALL(0x015a)
+#define SMC_RMI_REC_DESTROY			SMC_RMI_CALL(0x015b)
+#define SMC_RMI_REC_ENTER			SMC_RMI_CALL(0x015c)
+#define SMC_RMI_RTT_CREATE			SMC_RMI_CALL(0x015d)
+#define SMC_RMI_RTT_DESTROY			SMC_RMI_CALL(0x015e)
+
+#define SMC_RMI_RTT_READ_ENTRY			SMC_RMI_CALL(0x0161)
+
+#define SMC_RMI_RTT_DEV_VALIDATE		SMC_RMI_CALL(0x0163)
+#define SMC_RMI_PSCI_COMPLETE			SMC_RMI_CALL(0x0164)
+#define SMC_RMI_FEATURES			SMC_RMI_CALL(0x0165)
+#define SMC_RMI_RTT_FOLD			SMC_RMI_CALL(0x0166)
+
+#define SMC_RMI_RTT_INIT_RIPAS			SMC_RMI_CALL(0x0168)
+#define SMC_RMI_RTT_SET_RIPAS			SMC_RMI_CALL(0x0169)
+#define SMC_RMI_VSMMU_CREATE			SMC_RMI_CALL(0x016a)
+#define SMC_RMI_VSMMU_DESTROY			SMC_RMI_CALL(0x016b)
+#define SMC_RMI_RMM_CONFIG_SET			SMC_RMI_CALL(0x016e)
+#define SMC_RMI_PSMMU_IRQ_NOTIFY		SMC_RMI_CALL(0x016f)
+
+#define SMC_RMI_PDEV_ABORT			SMC_RMI_CALL(0x0174)
+#define SMC_RMI_PDEV_COMMUNICATE		SMC_RMI_CALL(0x0175)
+#define SMC_RMI_PDEV_CREATE			SMC_RMI_CALL(0x0176)
+#define SMC_RMI_PDEV_DESTROY			SMC_RMI_CALL(0x0177)
+#define SMC_RMI_PDEV_GET_STATE			SMC_RMI_CALL(0x0178)
+
+#define SMC_RMI_PDEV_STREAM_KEY_REFRESH		SMC_RMI_CALL(0x017a)
+#define SMC_RMI_PDEV_SET_PUBKEY			SMC_RMI_CALL(0x017b)
+#define SMC_RMI_PDEV_STOP			SMC_RMI_CALL(0x017c)
+#define SMC_RMI_RTT_AUX_CREATE			SMC_RMI_CALL(0x017d)
+#define SMC_RMI_RTT_AUX_DESTROY			SMC_RMI_CALL(0x017e)
+#define SMC_RMI_RTT_AUX_FOLD			SMC_RMI_CALL(0x017f)
+
+#define SMC_RMI_VDEV_ABORT			SMC_RMI_CALL(0x0185)
+#define SMC_RMI_VDEV_COMMUNICATE		SMC_RMI_CALL(0x0186)
+#define SMC_RMI_VDEV_CREATE			SMC_RMI_CALL(0x0187)
+#define SMC_RMI_VDEV_DESTROY			SMC_RMI_CALL(0x0188)
+#define SMC_RMI_VDEV_GET_STATE			SMC_RMI_CALL(0x0189)
+#define SMC_RMI_VDEV_UNLOCK			SMC_RMI_CALL(0x018a)
+#define SMC_RMI_RTT_SET_S2AP			SMC_RMI_CALL(0x018b)
+#define SMC_RMI_VDEV_COMPLETE			SMC_RMI_CALL(0x018e)
+
+#define SMC_RMI_VDEV_GET_INTERFACE_REPORT	SMC_RMI_CALL(0x01d0)
+#define SMC_RMI_VDEV_GET_MEASUREMENTS		SMC_RMI_CALL(0x01d1)
+#define SMC_RMI_VDEV_LOCK			SMC_RMI_CALL(0x01d2)
+#define SMC_RMI_VDEV_START			SMC_RMI_CALL(0x01d3)
+
+#define SMC_RMI_VSMMU_EVENT_NOTIFY		SMC_RMI_CALL(0x01d6)
+#define SMC_RMI_PSMMU_ACTIVATE			SMC_RMI_CALL(0x01d7)
+#define SMC_RMI_PSMMU_DEACTIVATE		SMC_RMI_CALL(0x01d8)
+
+#define SMC_RMI_PSMMU_ST_L2_CREATE		SMC_RMI_CALL(0x01db)
+#define SMC_RMI_PSMMU_ST_L2_DESTROY		SMC_RMI_CALL(0x01dc)
+#define SMC_RMI_DPT_L0_CREATE			SMC_RMI_CALL(0x01dd)
+#define SMC_RMI_DPT_L0_DESTROY			SMC_RMI_CALL(0x01de)
+#define SMC_RMI_DPT_L1_CREATE			SMC_RMI_CALL(0x01df)
+#define SMC_RMI_DPT_L1_DESTROY			SMC_RMI_CALL(0x01e0)
+#define SMC_RMI_GRANULE_TRACKING_GET		SMC_RMI_CALL(0x01e1)
+
+#define SMC_RMI_GRANULE_TRACKING_SET		SMC_RMI_CALL(0x01e3)
+
+#define SMC_RMI_RMM_CONFIG_GET			SMC_RMI_CALL(0x01ec)
+
+#define SMC_RMI_RMM_STATE_GET			SMC_RMI_CALL(0x01ee)
+
+#define SMC_RMI_PSMMU_EVENT_CONSUME		SMC_RMI_CALL(0x01f0)
+#define SMC_RMI_GRANULE_RANGE_DELEGATE		SMC_RMI_CALL(0x01f1)
+#define SMC_RMI_GRANULE_RANGE_UNDELEGATE	SMC_RMI_CALL(0x01f2)
+#define SMC_RMI_GPT_L1_CREATE			SMC_RMI_CALL(0x01f3)
+#define SMC_RMI_GPT_L1_DESTROY			SMC_RMI_CALL(0x01f4)
+#define SMC_RMI_RTT_DATA_MAP			SMC_RMI_CALL(0x01f5)
+#define SMC_RMI_RTT_DATA_UNMAP			SMC_RMI_CALL(0x01f6)
+#define SMC_RMI_RTT_DEV_MAP			SMC_RMI_CALL(0x01f7)
+#define SMC_RMI_RTT_DEV_UNMAP			SMC_RMI_CALL(0x01f8)
+#define SMC_RMI_RTT_ARCH_DEV_MAP		SMC_RMI_CALL(0x01f9)
+#define SMC_RMI_RTT_ARCH_DEV_UNMAP		SMC_RMI_CALL(0x01fa)
+#define SMC_RMI_RTT_UNPROT_MAP			SMC_RMI_CALL(0x01fb)
+#define SMC_RMI_RTT_UNPROT_UNMAP		SMC_RMI_CALL(0x01fc)
+#define SMC_RMI_RTT_AUX_PROT_MAP		SMC_RMI_CALL(0x01fd)
+#define SMC_RMI_RTT_AUX_PROT_UNMAP		SMC_RMI_CALL(0x01fe)
+#define SMC_RMI_RTT_AUX_UNPROT_MAP		SMC_RMI_CALL(0x01ff)
+#define SMC_RMI_RTT_AUX_UNPROT_UNMAP		SMC_RMI_CALL(0x0200)
+#define SMC_RMI_REALM_TERMINATE			SMC_RMI_CALL(0x0201)
+#define SMC_RMI_RMM_ACTIVATE			SMC_RMI_CALL(0x0202)
+#define SMC_RMI_OP_CONTINUE			SMC_RMI_CALL(0x0203)
+#define SMC_RMI_PDEV_STREAM_CONNECT		SMC_RMI_CALL(0x0204)
+#define SMC_RMI_PDEV_STREAM_DISCONNECT		SMC_RMI_CALL(0x0205)
+#define SMC_RMI_PDEV_STREAM_COMPLETE		SMC_RMI_CALL(0x0206)
+#define SMC_RMI_PDEV_STREAM_KEY_PURGE		SMC_RMI_CALL(0x0207)
+#define SMC_RMI_OP_MEM_DONATE			SMC_RMI_CALL(0x0208)
+#define SMC_RMI_OP_MEM_RECLAIM			SMC_RMI_CALL(0x0209)
+#define SMC_RMI_OP_CANCEL			SMC_RMI_CALL(0x020a)
+#define SMC_RMI_VSMMU_FEATURES			SMC_RMI_CALL(0x020b)
+#define SMC_RMI_VSMMU_CMD_GET			SMC_RMI_CALL(0x020c)
+#define SMC_RMI_VSMMU_CMD_COMPLETE		SMC_RMI_CALL(0x020d)
+#define SMC_RMI_PSMMU_INFO			SMC_RMI_CALL(0x020e)
+
+#define RMI_ABI_MAJOR_VERSION	2

arch/arm64/include/asm/rmi_smc.h line 135

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+#define RMI_ABI_MINOR_VERSION	0
+
+#define RMI_ABI_VERSION_GET_MAJOR(version) ((version) >> 16)
+#define RMI_ABI_VERSION_GET_MINOR(version) ((version) & 0xFFFF)
+#define RMI_ABI_VERSION(major, minor)      (((major) << 16) | (minor))
+
+#define RMI_UNASSIGNED			0
+#define RMI_ASSIGNED			1
+#define RMI_TABLE			2
+

Those definations are inconsistent to those defined in tf-rmm/lib/smc/include/smc-rmi.h where their size are 64-bits. Also, other two definations are missed here and perhaps worthy to be added here.

#define RMI_ASSIGNED_DEV UL(3)
#define RMI_AUX_DESTROYED UL(5)

Quoted 17 lines
arch/arm64/include/asm/rmi_smc.h new file +17 -0
+#define RMI_RETURN_STATUS(ret)		((ret) & 0xFF)
+#define RMI_RETURN_INDEX(ret)		(((ret) >> 8) & 0xFF)
+#define RMI_RETURN_MEMREQ(ret)		(((ret) >> 8) & 0x3)
+#define RMI_RETURN_CAN_CANCEL(ret)	(((ret) >> 10) & 0x1)
+
+#define RMI_SUCCESS			0
+#define RMI_ERROR_INPUT			1
+#define RMI_ERROR_REALM			2
+#define RMI_ERROR_REC			3
+#define RMI_ERROR_RTT			4
+#define RMI_ERROR_NOT_SUPPORTED		5
+#define RMI_ERROR_DEVICE		6
+#define RMI_ERROR_RTT_AUX		7
+#define RMI_ERROR_PSMMU_ST		8
+#define RMI_ERROR_DPT			9
+#define RMI_BUSY			10
+#define RMI_ERROR_GLOBAL		11

arch/arm64/include/asm/rmi_smc.h line 162

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+#define RMI_ERROR_TRACKING		12
+#define RMI_INCOMPLETE			13
+#define RMI_BLOCKED			14
+#define RMI_ERROR_GPT			15
+#define RMI_ERROR_GRANULE		16
+
+#define RMI_OP_MEM_REQ_NONE		0
+#define RMI_OP_MEM_REQ_DONATE		1
+#define RMI_OP_MEM_REQ_RECLAIM		2
+

The size of those definations are 32-bits, different to that of them defined in tf-rmm/lib/smc/include/smc-rmi.h

#define RMI_OP_MEM_REQ_NONE             (0UL)
#define RMI_OP_MEM_REQ_DONATE           (1UL)
#define RMI_OP_MEM_REQ_RECLAIM          (2UL)

arch/arm64/include/asm/rmi_smc.h line 171

Quoted 9 lines
arch/arm64/include/asm/rmi_smc.h new file +9 -0
+#define RMI_DONATE_SIZE(req)		((req) & 0x3)
+#define RMI_DONATE_COUNT_MASK		GENMASK(15, 2)
+#define RMI_DONATE_COUNT(req)		(((req) & RMI_DONATE_COUNT_MASK) >> 2)
+#define RMI_DONATE_CONTIG(req)		(!!((req) & BIT(16)))
+#define RMI_DONATE_STATE(req)		(!!((req) & BIT(17)))
+
+#define RMI_OP_MEM_DELEGATED		0
+#define RMI_OP_MEM_UNDELEGATED		1
+

As above, inconsistent size to those definations in tf-rmm/lib/smc/include/smc-rmi.h

arch/arm64/include/asm/rmi_smc.h line 175

Quoted 4 lines
arch/arm64/include/asm/rmi_smc.h new file +4 -0
+#define RMI_ADDR_TYPE_NONE		0
+#define RMI_ADDR_TYPE_SINGLE		1
+#define RMI_ADDR_TYPE_LIST		2
+

As above, inconsistent size to those definations in tf-rmm/lib/smc/include/smc-rmi.h

Quoted 263 lines
arch/arm64/include/asm/rmi_smc.h new file +263 -0
+#define RMI_ADDR_RANGE_SIZE_MASK	GENMASK(1, 0)
+#define RMI_ADDR_RANGE_COUNT_MASK	GENMASK(PAGE_SHIFT - 1, 2)
+#define RMI_ADDR_RANGE_ADDR_MASK	(PAGE_MASK & GENMASK(51, 0))
+#define RMI_ADDR_RANGE_STATE_MASK	BIT(63)
+
+#define RMI_ADDR_RANGE_SIZE(ar)		(FIELD_GET(RMI_ADDR_RANGE_SIZE_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_COUNT(ar)	(FIELD_GET(RMI_ADDR_RANGE_COUNT_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_ADDR(ar)		((ar) & RMI_ADDR_RANGE_ADDR_MASK)
+#define RMI_ADDR_RANGE_STATE(ar)	(FIELD_GET(RMI_ADDR_RANGE_STATE_MASK, \
+						   (ar)))
+
+enum rmi_ripas {
+	RMI_EMPTY = 0,
+	RMI_RAM = 1,
+	RMI_DESTROYED = 2,
+	RMI_DEV = 3,
+};
+
+#define RMI_NO_MEASURE_CONTENT	0
+#define RMI_MEASURE_CONTENT	1
+
+#define RMI_FEATURE_REGISTER_0_S2SZ		GENMASK(7, 0)
+#define RMI_FEATURE_REGISTER_0_LPA2		BIT(8)
+#define RMI_FEATURE_REGISTER_0_SVE		BIT(9)
+#define RMI_FEATURE_REGISTER_0_SVE_VL		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_0_NUM_BPS		GENMASK(19, 14)
+#define RMI_FEATURE_REGISTER_0_NUM_WPS		GENMASK(25, 20)
+#define RMI_FEATURE_REGISTER_0_PMU		BIT(26)
+#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS	GENMASK(31, 27)
+
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB	BIT(0)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB	BIT(1)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB	BIT(2)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_256	BIT(3)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_384	BIT(4)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_512	BIT(5)
+#define RMI_FEATURE_REGISTER_1_MAX_RECS_ORDER	GENMASK(9, 6)
+#define RMI_FEATURE_REGISTER_1_L0GPTSZ		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_1_PPS		GENMASK(16, 14)
+
+#define RMI_FEATURE_REGISTER_2_DA		BIT(0)
+#define RMI_FEATURE_REGISTER_2_DA_COH		BIT(1)
+#define RMI_FEATURE_REGISTER_2_VSMMU		BIT(2)
+#define RMI_FEATURE_REGISTER_2_ATS		BIT(3)
+#define RMI_FEATURE_REGISTER_2_MAX_VDEVS_ORDER	GENMASK(7, 4)
+#define RMI_FEATURE_REGISTER_2_VDEV_KROU	BIT(8)
+#define RMI_FEATURE_REGISTER_2_NON_TEE_STREAM	BIT(9)
+
+#define RMI_FEATURE_REGISTER_3_MAX_NUM_AUX_PLANES	GENMASK(3, 0)
+#define RMI_FEATURE_REGISTER_3_RTT_PLAN			GENMASK(5, 4)
+#define RMI_FEATURE_REGISTER_3_RTT_S2AP_INDIRECT	BIT(6)
+
+#define RMI_FEATURE_REGISTER_4_MEC_COUNT		GENMASK(63, 0)
+
+#define RMI_MEM_CATEGORY_CONVENTIONAL		0
+#define RMI_MEM_CATEGORY_DEV_NCOH		1
+#define RMI_MEM_CATEGORY_DEV_COH		2
+
+#define RMI_TRACKING_RESERVED			0
+#define RMI_TRACKING_NONE			1
+#define RMI_TRACKING_FINE			2
+#define RMI_TRACKING_COARSE			3
+
+#define RMI_GRANULE_SIZE_4KB	0
+#define RMI_GRANULE_SIZE_16KB	1
+#define RMI_GRANULE_SIZE_64KB	2
+
+/*
+ * Note many of these fields are smaller than u64 but all fields have u64
+ * alignment, so use u64 to ensure correct alignment.
+ */
+struct rmm_config {
+	union { /* 0x0 */
+		struct {
+			u64 tracking_region_size;
+			u64 rmi_granule_size;
+		};
+		u8 sizer[0x1000];
+	};
+};
+
+#define RMI_REALM_PARAM_FLAG_LPA2		BIT(0)
+#define RMI_REALM_PARAM_FLAG_SVE		BIT(1)
+#define RMI_REALM_PARAM_FLAG_PMU		BIT(2)
+
+struct realm_params {
+	union { /* 0x0 */
+		struct {
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+			u64 num_aux_planes;
+		};
+		u8 padding0[0x400];
+	};
+	union { /* 0x400 */
+		struct {
+			u8 rpv[64];
+			u64 ats_plane;
+		};
+		u8 padding1[0x400];
+	};
+	union { /* 0x800 */
+		struct {
+			u64 padding;
+			u64 rtt_base;
+			s64 rtt_level_start;
+			u64 rtt_num_start;
+			u64 flags1;
+			u64 aux_rtt_base[3];
+		};
+		u8 padding2[0x800];
+	};
+};
+
+/*
+ * The number of GPRs (starting from X0) that are
+ * configured by the host when a REC is created.
+ */
+#define REC_CREATE_NR_GPRS		8
+
+#define REC_PARAMS_FLAG_RUNNABLE	BIT_ULL(0)
+
+struct rec_params {
+	union { /* 0x0 */
+		u64 flags;
+		u8 padding0[0x100];
+	};
+	union { /* 0x100 */
+		u64 mpidr;
+		u8 padding1[0x100];
+	};
+	union { /* 0x200 */
+		u64 pc;
+		u8 padding2[0x100];
+	};
+	union { /* 0x300 */
+		u64 gprs[REC_CREATE_NR_GPRS];
+		u8 padding3[0xd00];
+	};
+};
+
+#define REC_ENTER_FLAG_EMULATED_MMIO	BIT(0)
+#define REC_ENTER_FLAG_INJECT_SEA	BIT(1)
+#define REC_ENTER_FLAG_TRAP_WFI		BIT(2)
+#define REC_ENTER_FLAG_TRAP_WFE		BIT(3)
+#define REC_ENTER_FLAG_RIPAS_RESPONSE	BIT(4)
+#define REC_ENTER_FLAG_S2AP_RESPONSE	BIT(5)
+#define REC_ENTER_FLAG_DEV_MEM_RESPONSE	BIT(6)
+#define REC_ENTER_FLAG_FORCE_P0		BIT(7)
+
+#define REC_RUN_GPRS			31
+#define REC_MAX_GIC_NUM_LRS		16
+
+#define RMI_PERMITTED_GICV3_HCR_BITS	(ICH_HCR_EL2_UIE |		\
+					 ICH_HCR_EL2_LRENPIE |		\
+					 ICH_HCR_EL2_NPIE |		\
+					 ICH_HCR_EL2_VGrp0EIE |		\
+					 ICH_HCR_EL2_VGrp0DIE |		\
+					 ICH_HCR_EL2_VGrp1EIE |		\
+					 ICH_HCR_EL2_VGrp1DIE |		\
+					 ICH_HCR_EL2_TDIR)
+
+struct rec_enter {
+	union { /* 0x000 */
+		u64 flags;
+		u8 padding0[0x200];
+	};
+	union { /* 0x200 */
+		u64 gprs[REC_RUN_GPRS];
+		u8 padding1[0x100];
+	};
+	u8 padding3[0x500];
+};
+
+#define RMI_EXIT_SYNC			0x00
+#define RMI_EXIT_IRQ			0x01
+#define RMI_EXIT_FIQ			0x02
+#define RMI_EXIT_PSCI			0x03
+#define RMI_EXIT_RIPAS_CHANGE		0x04
+#define RMI_EXIT_HOST_CALL		0x05
+#define RMI_EXIT_SERROR			0x06
+#define RMI_EXIT_S2AP_CHANGE		0x07
+#define RMI_EXIT_VDEV_REQUEST		0x08
+#define RMI_EXIT_VDEV_VALIDATE_MAPPING	0x09
+#define RMI_EXIT_VSMMU_COMMAND		0x0a
+
+struct rec_exit {
+	union { /* 0x000 */
+		u8 exit_reason;
+		u8 padding0[0x100];
+	};
+	union { /* 0x100 */
+		struct {
+			u64 esr;
+			u64 far;
+			u64 hpfar;
+			u64 rtt_tree;
+		};
+		u8 padding1[0x100];
+	};
+	union { /* 0x200 */
+		u64 gprs[REC_RUN_GPRS];
+		u8 padding2[0x100];
+	};
+	union { /* 0x300 */
+		u8 padding3[0x100];
+	};
+	union { /* 0x400 */
+		struct {
+			u64 cntp_ctl;
+			u64 cntp_cval;
+			u64 cntv_ctl;
+			u64 cntv_cval;
+		};
+		u8 padding4[0x100];
+	};
+	union { /* 0x500 */
+		struct {
+			u64 ripas_base;
+			u64 ripas_top;
+			u8 ripas_value;
+			u8 padding8[15];
+			u64 s2ap_base;
+			u64 s2ap_top;
+			u64 vdev_id_1;
+			u64 vdev_id_2;
+			u64 dev_mem_base;
+			u64 dev_mem_top;
+			u64 dev_mem_pa;
+		};
+		u8 padding5[0x100];
+	};
+	union { /* 0x600 */
+		struct {
+			u16 imm;
+			u16 padding9;
+			u64 plane;
+		};
+		u8 padding6[0x100];
+	};
+	union { /* 0x700 */
+		struct {
+			u8 pmu_ovf_status;
+			u8 padding10[15];
+			u64 vsmmu;
+		};
+		u8 padding7[0x100];
+	};
+};
+
+struct rec_run {
+	struct rec_enter enter;
+	struct rec_exit exit;
+};
+
+/* RMI_RTT_UNPROT_MAP_FLAGS definitions */

arch/arm64/include/asm/rmi_smc.h line 448

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+#define RMI_RTT_UNPROT_MAP_FLAGS_OADDR_TYPE	GENMASK(1, 0)
+#define RMI_RTT_UNPROT_MAP_FLAGS_LIST_COUNT	GENMASK(15, 2)
+#define RMI_RTT_UNPROT_MAP_FLAGS_MEMATTR	GENMASK(18, 16)
+#define RMI_RTT_UNPROT_MAP_FLAGS_S2AP		GENMASK(22, 19)
+
+/* S2AP Direct Encodings, used in RMI_RTT_UNPROT_MAP_FLAGS_S2AP */
+#define RMI_S2AP_DIRECT_WRITE			BIT(0)
+#define RMI_S2AP_DIRECT_READ			BIT(1)
+
+#endif /* __ASM_RMI_SMC_H */

Thanks,
Gavin

#12 lore raw

Re: [PATCH v14 04/44] arm64: RMI: Add SMC definitions for calling the RMM

Quoted 188 lines · Gavin Shan

Hi Steven,

Quoted 185 lines · Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
  * Updated to RMM spec v2.0-bet1
Changes since v12:
  * Updated to RMM spec v2.0-bet0
Changes since v9:
  * Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64). Changes since v8:
  * Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
  * Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
  * Sorted the SMC #defines by value.
  * Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
  * Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
  * Provided a define for the reserved fields of FeatureRegister0.
  * Fix inconsistent names for padding fields.
Changes since v4:
  * Update to point to final released RMM spec.
  * Minor rearrangements.
Changes since v3:
  * Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
  * Fix specification link.
  * Rename rec_entry->rec_enter to match spec.
  * Fix size of pmu_ovf_status to match spec.
---
  arch/arm64/include/asm/rmi_smc.h | 448 +++++++++++++++++++++++++++++++

  1 file changed, 448 insertions(+)

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

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

asm/rmi_smc.h
new file mode 100644
index 000000000000..a09b7a631fef

arch/arm64/include/asm/rmi_smc.h new file +5 -0
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ *
+ * The values and structures in this file are from the Realm

Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/
+ */

arch/arm64/include/asm/rmi_smc.h new file +125 -0
+
+#ifndef __ASM_RMI_SMC_H
+#define __ASM_RMI_SMC_H
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RMI_CALL(func)                \
+    ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,        \
+               ARM_SMCCC_SMC_64,        \
+               ARM_SMCCC_OWNER_STANDARD,    \
+               (func))
+
+#define SMC_RMI_VERSION                SMC_RMI_CALL(0x0150)
+
+#define SMC_RMI_RTT_DATA_MAP_INIT        SMC_RMI_CALL(0x0153)
+
+#define SMC_RMI_REALM_ACTIVATE            SMC_RMI_CALL(0x0157)
+#define SMC_RMI_REALM_CREATE            SMC_RMI_CALL(0x0158)
+#define SMC_RMI_REALM_DESTROY            SMC_RMI_CALL(0x0159)
+#define SMC_RMI_REC_CREATE            SMC_RMI_CALL(0x015a)
+#define SMC_RMI_REC_DESTROY            SMC_RMI_CALL(0x015b)
+#define SMC_RMI_REC_ENTER            SMC_RMI_CALL(0x015c)
+#define SMC_RMI_RTT_CREATE            SMC_RMI_CALL(0x015d)
+#define SMC_RMI_RTT_DESTROY            SMC_RMI_CALL(0x015e)
+
+#define SMC_RMI_RTT_READ_ENTRY            SMC_RMI_CALL(0x0161)
+
+#define SMC_RMI_RTT_DEV_VALIDATE        SMC_RMI_CALL(0x0163)
+#define SMC_RMI_PSCI_COMPLETE            SMC_RMI_CALL(0x0164)
+#define SMC_RMI_FEATURES            SMC_RMI_CALL(0x0165)
+#define SMC_RMI_RTT_FOLD            SMC_RMI_CALL(0x0166)
+
+#define SMC_RMI_RTT_INIT_RIPAS            SMC_RMI_CALL(0x0168)
+#define SMC_RMI_RTT_SET_RIPAS            SMC_RMI_CALL(0x0169)
+#define SMC_RMI_VSMMU_CREATE            SMC_RMI_CALL(0x016a)
+#define SMC_RMI_VSMMU_DESTROY            SMC_RMI_CALL(0x016b)
+#define SMC_RMI_RMM_CONFIG_SET            SMC_RMI_CALL(0x016e)
+#define SMC_RMI_PSMMU_IRQ_NOTIFY        SMC_RMI_CALL(0x016f)
+
+#define SMC_RMI_PDEV_ABORT            SMC_RMI_CALL(0x0174)
+#define SMC_RMI_PDEV_COMMUNICATE        SMC_RMI_CALL(0x0175)
+#define SMC_RMI_PDEV_CREATE            SMC_RMI_CALL(0x0176)
+#define SMC_RMI_PDEV_DESTROY            SMC_RMI_CALL(0x0177)
+#define SMC_RMI_PDEV_GET_STATE            SMC_RMI_CALL(0x0178)
+
+#define SMC_RMI_PDEV_STREAM_KEY_REFRESH        SMC_RMI_CALL(0x017a)
+#define SMC_RMI_PDEV_SET_PUBKEY            SMC_RMI_CALL(0x017b)
+#define SMC_RMI_PDEV_STOP            SMC_RMI_CALL(0x017c)
+#define SMC_RMI_RTT_AUX_CREATE            SMC_RMI_CALL(0x017d)
+#define SMC_RMI_RTT_AUX_DESTROY            SMC_RMI_CALL(0x017e)
+#define SMC_RMI_RTT_AUX_FOLD            SMC_RMI_CALL(0x017f)
+
+#define SMC_RMI_VDEV_ABORT            SMC_RMI_CALL(0x0185)
+#define SMC_RMI_VDEV_COMMUNICATE        SMC_RMI_CALL(0x0186)
+#define SMC_RMI_VDEV_CREATE            SMC_RMI_CALL(0x0187)
+#define SMC_RMI_VDEV_DESTROY            SMC_RMI_CALL(0x0188)
+#define SMC_RMI_VDEV_GET_STATE            SMC_RMI_CALL(0x0189)
+#define SMC_RMI_VDEV_UNLOCK            SMC_RMI_CALL(0x018a)
+#define SMC_RMI_RTT_SET_S2AP            SMC_RMI_CALL(0x018b)
+#define SMC_RMI_VDEV_COMPLETE            SMC_RMI_CALL(0x018e)
+
+#define SMC_RMI_VDEV_GET_INTERFACE_REPORT    SMC_RMI_CALL(0x01d0)
+#define SMC_RMI_VDEV_GET_MEASUREMENTS        SMC_RMI_CALL(0x01d1)
+#define SMC_RMI_VDEV_LOCK            SMC_RMI_CALL(0x01d2)
+#define SMC_RMI_VDEV_START            SMC_RMI_CALL(0x01d3)
+
+#define SMC_RMI_VSMMU_EVENT_NOTIFY        SMC_RMI_CALL(0x01d6)
+#define SMC_RMI_PSMMU_ACTIVATE            SMC_RMI_CALL(0x01d7)
+#define SMC_RMI_PSMMU_DEACTIVATE        SMC_RMI_CALL(0x01d8)
+
+#define SMC_RMI_PSMMU_ST_L2_CREATE        SMC_RMI_CALL(0x01db)
+#define SMC_RMI_PSMMU_ST_L2_DESTROY        SMC_RMI_CALL(0x01dc)
+#define SMC_RMI_DPT_L0_CREATE            SMC_RMI_CALL(0x01dd)
+#define SMC_RMI_DPT_L0_DESTROY            SMC_RMI_CALL(0x01de)
+#define SMC_RMI_DPT_L1_CREATE            SMC_RMI_CALL(0x01df)
+#define SMC_RMI_DPT_L1_DESTROY            SMC_RMI_CALL(0x01e0)
+#define SMC_RMI_GRANULE_TRACKING_GET        SMC_RMI_CALL(0x01e1)
+
+#define SMC_RMI_GRANULE_TRACKING_SET        SMC_RMI_CALL(0x01e3)
+
+#define SMC_RMI_RMM_CONFIG_GET            SMC_RMI_CALL(0x01ec)
+
+#define SMC_RMI_RMM_STATE_GET            SMC_RMI_CALL(0x01ee)
+
+#define SMC_RMI_PSMMU_EVENT_CONSUME        SMC_RMI_CALL(0x01f0)
+#define SMC_RMI_GRANULE_RANGE_DELEGATE        SMC_RMI_CALL(0x01f1)
+#define SMC_RMI_GRANULE_RANGE_UNDELEGATE    SMC_RMI_CALL(0x01f2)
+#define SMC_RMI_GPT_L1_CREATE            SMC_RMI_CALL(0x01f3)
+#define SMC_RMI_GPT_L1_DESTROY            SMC_RMI_CALL(0x01f4)
+#define SMC_RMI_RTT_DATA_MAP            SMC_RMI_CALL(0x01f5)
+#define SMC_RMI_RTT_DATA_UNMAP            SMC_RMI_CALL(0x01f6)
+#define SMC_RMI_RTT_DEV_MAP            SMC_RMI_CALL(0x01f7)
+#define SMC_RMI_RTT_DEV_UNMAP            SMC_RMI_CALL(0x01f8)
+#define SMC_RMI_RTT_ARCH_DEV_MAP        SMC_RMI_CALL(0x01f9)
+#define SMC_RMI_RTT_ARCH_DEV_UNMAP        SMC_RMI_CALL(0x01fa)
+#define SMC_RMI_RTT_UNPROT_MAP            SMC_RMI_CALL(0x01fb)
+#define SMC_RMI_RTT_UNPROT_UNMAP        SMC_RMI_CALL(0x01fc)
+#define SMC_RMI_RTT_AUX_PROT_MAP        SMC_RMI_CALL(0x01fd)
+#define SMC_RMI_RTT_AUX_PROT_UNMAP        SMC_RMI_CALL(0x01fe)
+#define SMC_RMI_RTT_AUX_UNPROT_MAP        SMC_RMI_CALL(0x01ff)
+#define SMC_RMI_RTT_AUX_UNPROT_UNMAP        SMC_RMI_CALL(0x0200)
+#define SMC_RMI_REALM_TERMINATE            SMC_RMI_CALL(0x0201)
+#define SMC_RMI_RMM_ACTIVATE            SMC_RMI_CALL(0x0202)
+#define SMC_RMI_OP_CONTINUE            SMC_RMI_CALL(0x0203)
+#define SMC_RMI_PDEV_STREAM_CONNECT        SMC_RMI_CALL(0x0204)
+#define SMC_RMI_PDEV_STREAM_DISCONNECT        SMC_RMI_CALL(0x0205)
+#define SMC_RMI_PDEV_STREAM_COMPLETE        SMC_RMI_CALL(0x0206)
+#define SMC_RMI_PDEV_STREAM_KEY_PURGE        SMC_RMI_CALL(0x0207)
+#define SMC_RMI_OP_MEM_DONATE            SMC_RMI_CALL(0x0208)
+#define SMC_RMI_OP_MEM_RECLAIM            SMC_RMI_CALL(0x0209)
+#define SMC_RMI_OP_CANCEL            SMC_RMI_CALL(0x020a)
+#define SMC_RMI_VSMMU_FEATURES            SMC_RMI_CALL(0x020b)
+#define SMC_RMI_VSMMU_CMD_GET            SMC_RMI_CALL(0x020c)
+#define SMC_RMI_VSMMU_CMD_COMPLETE        SMC_RMI_CALL(0x020d)
+#define SMC_RMI_PSMMU_INFO            SMC_RMI_CALL(0x020e)
+
+#define RMI_ABI_MAJOR_VERSION    2
+#define RMI_ABI_MINOR_VERSION    0
+
+#define RMI_ABI_VERSION_GET_MAJOR(version) ((version) >> 16)
+#define RMI_ABI_VERSION_GET_MINOR(version) ((version) & 0xFFFF)
+#define RMI_ABI_VERSION(major, minor)      (((major) << 16) | (minor))
+
+#define RMI_UNASSIGNED            0
+#define RMI_ASSIGNED            1
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h

Quoted 2 lines
arch/arm64/include/asm/rmi_smc.h new file +2 -0
+#define RMI_TABLE            2
+

Those definations are inconsistent to those defined in tf-rmm/lib/smc/ include/smc-rmi.h
where their size are 64-bits. Also, other two definations are missed here and perhaps
worthy to be added here.

Actually these should really be removed altogether (they are no longer used in the code). The spec names for these have also changed, the new names are:

0 VOID
1 DATA
2 TABLE
3 NARCH_DEV
4 AUX_DESTROYED
5 ARCH_DEV

Quoted 2 lines

#define RMI_ASSIGNED_DEV        UL(3)
#define RMI_AUX_DESTROYED       UL(5)

So this looks like the RMM versions are also out of date.

Quoted 34 lines
Quoted 19 lines
arch/arm64/include/asm/rmi_smc.h new file +17 -0
+#define RMI_RETURN_STATUS(ret)        ((ret) & 0xFF)
+#define RMI_RETURN_INDEX(ret)        (((ret) >> 8) & 0xFF)
+#define RMI_RETURN_MEMREQ(ret)        (((ret) >> 8) & 0x3)
+#define RMI_RETURN_CAN_CANCEL(ret)    (((ret) >> 10) & 0x1)
+
+#define RMI_SUCCESS            0
+#define RMI_ERROR_INPUT            1
+#define RMI_ERROR_REALM            2
+#define RMI_ERROR_REC            3
+#define RMI_ERROR_RTT            4
+#define RMI_ERROR_NOT_SUPPORTED        5
+#define RMI_ERROR_DEVICE        6
+#define RMI_ERROR_RTT_AUX        7
+#define RMI_ERROR_PSMMU_ST        8
+#define RMI_ERROR_DPT            9
+#define RMI_BUSY            10
+#define RMI_ERROR_GLOBAL        11

arch/arm64/include/asm/rmi_smc.h line 162

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+#define RMI_ERROR_TRACKING        12
+#define RMI_INCOMPLETE            13
+#define RMI_BLOCKED            14
+#define RMI_ERROR_GPT            15
+#define RMI_ERROR_GRANULE        16
+
+#define RMI_OP_MEM_REQ_NONE        0
+#define RMI_OP_MEM_REQ_DONATE        1
+#define RMI_OP_MEM_REQ_RECLAIM        2
+

The size of those definations are 32-bits, different to that of them defined
in tf-rmm/lib/smc/include/smc-rmi.h

Quoted 3 lines

#define RMI_OP_MEM_REQ_NONE             (0UL)
#define RMI_OP_MEM_REQ_DONATE           (1UL)
#define RMI_OP_MEM_REQ_RECLAIM          (2UL)

Well the size according to the spec is a 2 bit enumeration. RMI_RETURN_MEMREQ() is used to extract it from the result. I can update all (or at least most) of the integers in this file to have a UL suffix if there's a good reason. Ultimately the values are passed in the 64 bit registers which Linux uses unsigned long for so it does make some sense
- but it seems a little unneceesary to me when the values are known to fix within the size of an int (32 bits).

Note that the TF-RMM project isn't the "truth" - it is just 'one implementation' - the spec is the real arbiter on these matters.

Quoted 15 lines

arch/arm64/include/asm/rmi_smc.h line 171

Quoted 11 lines
arch/arm64/include/asm/rmi_smc.h new file +3 -0
+#define RMI_DONATE_SIZE(req)        ((req) & 0x3)
+#define RMI_DONATE_COUNT_MASK        GENMASK(15, 2)
+#define RMI_DONATE_COUNT(req)        (((req) & RMI_DONATE_COUNT_MASK)
Quoted 1 line
Quoted 1 line

2)

arch/arm64/include/asm/rmi_smc.h new file +6 -0
+#define RMI_DONATE_CONTIG(req)        (!!((req) & BIT(16)))
+#define RMI_DONATE_STATE(req)        (!!((req) & BIT(17)))
+
+#define RMI_OP_MEM_DELEGATED        0
+#define RMI_OP_MEM_UNDELEGATED        1
+

As above, inconsistent size to those definations in tf-rmm/lib/smc/ include/smc-rmi.h

Quoted 7 lines

arch/arm64/include/asm/rmi_smc.h line 175

Quoted 4 lines
arch/arm64/include/asm/rmi_smc.h new file +4 -0
+#define RMI_ADDR_TYPE_NONE        0
+#define RMI_ADDR_TYPE_SINGLE        1
+#define RMI_ADDR_TYPE_LIST        2
+

As above, inconsistent size to those definations in tf-rmm/lib/smc/ include/smc-rmi.h

As above these are enumerations that are 2 bits (well RMI_OP_MEM_xxx was originally 1 bit and is now 2 bits in the 2.0-bet2 spec - I'll update to include the new value when moving to the new spec).

Thanks,
Steve

Quoted 281 lines
Quoted 267 lines
arch/arm64/include/asm/rmi_smc.h new file +6 -0
+#define RMI_ADDR_RANGE_SIZE_MASK    GENMASK(1, 0)
+#define RMI_ADDR_RANGE_COUNT_MASK    GENMASK(PAGE_SHIFT - 1, 2)
+#define RMI_ADDR_RANGE_ADDR_MASK    (PAGE_MASK & GENMASK(51, 0))
+#define RMI_ADDR_RANGE_STATE_MASK    BIT(63)
+
+#define RMI_ADDR_RANGE_SIZE(ar)       

(FIELD_GET(RMI_ADDR_RANGE_SIZE_MASK, \
+                           (ar)))
+#define RMI_ADDR_RANGE_COUNT(ar)
(FIELD_GET(RMI_ADDR_RANGE_COUNT_MASK, \
+                           (ar)))

arch/arm64/include/asm/rmi_smc.h new file +2 -0
+#define RMI_ADDR_RANGE_ADDR(ar)        ((ar) & RMI_ADDR_RANGE_ADDR_MASK)
+#define RMI_ADDR_RANGE_STATE(ar)   

(FIELD_GET(RMI_ADDR_RANGE_STATE_MASK, \
+                           (ar)))

arch/arm64/include/asm/rmi_smc.h new file +59 -0
+
+enum rmi_ripas {
+    RMI_EMPTY = 0,
+    RMI_RAM = 1,
+    RMI_DESTROYED = 2,
+    RMI_DEV = 3,
+};
+
+#define RMI_NO_MEASURE_CONTENT    0
+#define RMI_MEASURE_CONTENT    1
+
+#define RMI_FEATURE_REGISTER_0_S2SZ        GENMASK(7, 0)
+#define RMI_FEATURE_REGISTER_0_LPA2        BIT(8)
+#define RMI_FEATURE_REGISTER_0_SVE        BIT(9)
+#define RMI_FEATURE_REGISTER_0_SVE_VL        GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_0_NUM_BPS        GENMASK(19, 14)
+#define RMI_FEATURE_REGISTER_0_NUM_WPS        GENMASK(25, 20)
+#define RMI_FEATURE_REGISTER_0_PMU        BIT(26)
+#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS    GENMASK(31, 27)
+
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB    BIT(0)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB    BIT(1)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB    BIT(2)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_256    BIT(3)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_384    BIT(4)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_512    BIT(5)
+#define RMI_FEATURE_REGISTER_1_MAX_RECS_ORDER    GENMASK(9, 6)
+#define RMI_FEATURE_REGISTER_1_L0GPTSZ        GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_1_PPS        GENMASK(16, 14)
+
+#define RMI_FEATURE_REGISTER_2_DA        BIT(0)
+#define RMI_FEATURE_REGISTER_2_DA_COH        BIT(1)
+#define RMI_FEATURE_REGISTER_2_VSMMU        BIT(2)
+#define RMI_FEATURE_REGISTER_2_ATS        BIT(3)
+#define RMI_FEATURE_REGISTER_2_MAX_VDEVS_ORDER    GENMASK(7, 4)
+#define RMI_FEATURE_REGISTER_2_VDEV_KROU    BIT(8)
+#define RMI_FEATURE_REGISTER_2_NON_TEE_STREAM    BIT(9)
+
+#define RMI_FEATURE_REGISTER_3_MAX_NUM_AUX_PLANES    GENMASK(3, 0)
+#define RMI_FEATURE_REGISTER_3_RTT_PLAN            GENMASK(5, 4)
+#define RMI_FEATURE_REGISTER_3_RTT_S2AP_INDIRECT    BIT(6)
+
+#define RMI_FEATURE_REGISTER_4_MEC_COUNT        GENMASK(63, 0)
+
+#define RMI_MEM_CATEGORY_CONVENTIONAL        0
+#define RMI_MEM_CATEGORY_DEV_NCOH        1
+#define RMI_MEM_CATEGORY_DEV_COH        2
+
+#define RMI_TRACKING_RESERVED            0
+#define RMI_TRACKING_NONE            1
+#define RMI_TRACKING_FINE            2
+#define RMI_TRACKING_COARSE            3
+
+#define RMI_GRANULE_SIZE_4KB    0
+#define RMI_GRANULE_SIZE_16KB    1
+#define RMI_GRANULE_SIZE_64KB    2
+
+/*
+ * Note many of these fields are smaller than u64 but all fields have

u64
+ * alignment, so use u64 to ensure correct alignment.
+ */

arch/arm64/include/asm/rmi_smc.h new file +190 -0
+struct rmm_config {
+    union { /* 0x0 */
+        struct {
+            u64 tracking_region_size;
+            u64 rmi_granule_size;
+        };
+        u8 sizer[0x1000];
+    };
+};
+
+#define RMI_REALM_PARAM_FLAG_LPA2        BIT(0)
+#define RMI_REALM_PARAM_FLAG_SVE        BIT(1)
+#define RMI_REALM_PARAM_FLAG_PMU        BIT(2)
+
+struct realm_params {
+    union { /* 0x0 */
+        struct {
+            u64 flags;
+            u64 s2sz;
+            u64 sve_vl;
+            u64 num_bps;
+            u64 num_wps;
+            u64 pmu_num_ctrs;
+            u64 hash_algo;
+            u64 num_aux_planes;
+        };
+        u8 padding0[0x400];
+    };
+    union { /* 0x400 */
+        struct {
+            u8 rpv[64];
+            u64 ats_plane;
+        };
+        u8 padding1[0x400];
+    };
+    union { /* 0x800 */
+        struct {
+            u64 padding;
+            u64 rtt_base;
+            s64 rtt_level_start;
+            u64 rtt_num_start;
+            u64 flags1;
+            u64 aux_rtt_base[3];
+        };
+        u8 padding2[0x800];
+    };
+};
+
+/*
+ * The number of GPRs (starting from X0) that are
+ * configured by the host when a REC is created.
+ */
+#define REC_CREATE_NR_GPRS        8
+
+#define REC_PARAMS_FLAG_RUNNABLE    BIT_ULL(0)
+
+struct rec_params {
+    union { /* 0x0 */
+        u64 flags;
+        u8 padding0[0x100];
+    };
+    union { /* 0x100 */
+        u64 mpidr;
+        u8 padding1[0x100];
+    };
+    union { /* 0x200 */
+        u64 pc;
+        u8 padding2[0x100];
+    };
+    union { /* 0x300 */
+        u64 gprs[REC_CREATE_NR_GPRS];
+        u8 padding3[0xd00];
+    };
+};
+
+#define REC_ENTER_FLAG_EMULATED_MMIO    BIT(0)
+#define REC_ENTER_FLAG_INJECT_SEA    BIT(1)
+#define REC_ENTER_FLAG_TRAP_WFI        BIT(2)
+#define REC_ENTER_FLAG_TRAP_WFE        BIT(3)
+#define REC_ENTER_FLAG_RIPAS_RESPONSE    BIT(4)
+#define REC_ENTER_FLAG_S2AP_RESPONSE    BIT(5)
+#define REC_ENTER_FLAG_DEV_MEM_RESPONSE    BIT(6)
+#define REC_ENTER_FLAG_FORCE_P0        BIT(7)
+
+#define REC_RUN_GPRS            31
+#define REC_MAX_GIC_NUM_LRS        16
+
+#define RMI_PERMITTED_GICV3_HCR_BITS    (ICH_HCR_EL2_UIE |        \
+                     ICH_HCR_EL2_LRENPIE |        \
+                     ICH_HCR_EL2_NPIE |        \
+                     ICH_HCR_EL2_VGrp0EIE |        \
+                     ICH_HCR_EL2_VGrp0DIE |        \
+                     ICH_HCR_EL2_VGrp1EIE |        \
+                     ICH_HCR_EL2_VGrp1DIE |        \
+                     ICH_HCR_EL2_TDIR)
+
+struct rec_enter {
+    union { /* 0x000 */
+        u64 flags;
+        u8 padding0[0x200];
+    };
+    union { /* 0x200 */
+        u64 gprs[REC_RUN_GPRS];
+        u8 padding1[0x100];
+    };
+    u8 padding3[0x500];
+};
+
+#define RMI_EXIT_SYNC            0x00
+#define RMI_EXIT_IRQ            0x01
+#define RMI_EXIT_FIQ            0x02
+#define RMI_EXIT_PSCI            0x03
+#define RMI_EXIT_RIPAS_CHANGE        0x04
+#define RMI_EXIT_HOST_CALL        0x05
+#define RMI_EXIT_SERROR            0x06
+#define RMI_EXIT_S2AP_CHANGE        0x07
+#define RMI_EXIT_VDEV_REQUEST        0x08
+#define RMI_EXIT_VDEV_VALIDATE_MAPPING    0x09
+#define RMI_EXIT_VSMMU_COMMAND        0x0a
+
+struct rec_exit {
+    union { /* 0x000 */
+        u8 exit_reason;
+        u8 padding0[0x100];
+    };
+    union { /* 0x100 */
+        struct {
+            u64 esr;
+            u64 far;
+            u64 hpfar;
+            u64 rtt_tree;
+        };
+        u8 padding1[0x100];
+    };
+    union { /* 0x200 */
+        u64 gprs[REC_RUN_GPRS];
+        u8 padding2[0x100];
+    };
+    union { /* 0x300 */
+        u8 padding3[0x100];
+    };
+    union { /* 0x400 */
+        struct {
+            u64 cntp_ctl;
+            u64 cntp_cval;
+            u64 cntv_ctl;
+            u64 cntv_cval;
+        };
+        u8 padding4[0x100];
+    };
+    union { /* 0x500 */
+        struct {
+            u64 ripas_base;
+            u64 ripas_top;
+            u8 ripas_value;
+            u8 padding8[15];
+            u64 s2ap_base;
+            u64 s2ap_top;
+            u64 vdev_id_1;
+            u64 vdev_id_2;
+            u64 dev_mem_base;
+            u64 dev_mem_top;
+            u64 dev_mem_pa;
+        };
+        u8 padding5[0x100];
+    };
+    union { /* 0x600 */
+        struct {
+            u16 imm;
+            u16 padding9;
+            u64 plane;
+        };
+        u8 padding6[0x100];
+    };
+    union { /* 0x700 */
+        struct {
+            u8 pmu_ovf_status;
+            u8 padding10[15];
+            u64 vsmmu;
+        };
+        u8 padding7[0x100];
+    };
+};
+
+struct rec_run {
+    struct rec_enter enter;
+    struct rec_exit exit;
+};
+
+/* RMI_RTT_UNPROT_MAP_FLAGS definitions */

arch/arm64/include/asm/rmi_smc.h line 448

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+#define RMI_RTT_UNPROT_MAP_FLAGS_OADDR_TYPE    GENMASK(1, 0)
+#define RMI_RTT_UNPROT_MAP_FLAGS_LIST_COUNT    GENMASK(15, 2)
+#define RMI_RTT_UNPROT_MAP_FLAGS_MEMATTR    GENMASK(18, 16)
+#define RMI_RTT_UNPROT_MAP_FLAGS_S2AP        GENMASK(22, 19)
+
+/* S2AP Direct Encodings, used in RMI_RTT_UNPROT_MAP_FLAGS_S2AP */
+#define RMI_S2AP_DIRECT_WRITE            BIT(0)
+#define RMI_S2AP_DIRECT_READ            BIT(1)
+
+#endif /* __ASM_RMI_SMC_H */

Thanks,
Gavin

maintainer
#13 lore raw

Re: [PATCH v14 04/44] arm64: RMI: Add SMC definitions for calling the RMM

In reply to Steven Price
Quoted 51 lines · Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

---
 arch/arm64/include/asm/rmi_smc.h | 448 +++++++++++++++++++++++++++++++
 1 file changed, 448 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +0 -0
new file mode 100644
index 000000000000..a09b7a631fef

arch/arm64/include/asm/rmi_smc.h line 7

Quoted 7 lines
arch/arm64/include/asm/rmi_smc.h new file +7 -0
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/

How long is this spec going to be available on the ARM web site, which has a tendency of being reorganised every other week? And there is already a beta2.

Quoted 122 lines
arch/arm64/include/asm/rmi_smc.h new file +122 -0
+ */
+
+#ifndef __ASM_RMI_SMC_H
+#define __ASM_RMI_SMC_H
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RMI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD,	\
+			   (func))
+
+#define SMC_RMI_VERSION				SMC_RMI_CALL(0x0150)
+
+#define SMC_RMI_RTT_DATA_MAP_INIT		SMC_RMI_CALL(0x0153)
+
+#define SMC_RMI_REALM_ACTIVATE			SMC_RMI_CALL(0x0157)
+#define SMC_RMI_REALM_CREATE			SMC_RMI_CALL(0x0158)
+#define SMC_RMI_REALM_DESTROY			SMC_RMI_CALL(0x0159)
+#define SMC_RMI_REC_CREATE			SMC_RMI_CALL(0x015a)
+#define SMC_RMI_REC_DESTROY			SMC_RMI_CALL(0x015b)
+#define SMC_RMI_REC_ENTER			SMC_RMI_CALL(0x015c)
+#define SMC_RMI_RTT_CREATE			SMC_RMI_CALL(0x015d)
+#define SMC_RMI_RTT_DESTROY			SMC_RMI_CALL(0x015e)
+
+#define SMC_RMI_RTT_READ_ENTRY			SMC_RMI_CALL(0x0161)
+
+#define SMC_RMI_RTT_DEV_VALIDATE		SMC_RMI_CALL(0x0163)
+#define SMC_RMI_PSCI_COMPLETE			SMC_RMI_CALL(0x0164)
+#define SMC_RMI_FEATURES			SMC_RMI_CALL(0x0165)
+#define SMC_RMI_RTT_FOLD			SMC_RMI_CALL(0x0166)
+
+#define SMC_RMI_RTT_INIT_RIPAS			SMC_RMI_CALL(0x0168)
+#define SMC_RMI_RTT_SET_RIPAS			SMC_RMI_CALL(0x0169)
+#define SMC_RMI_VSMMU_CREATE			SMC_RMI_CALL(0x016a)
+#define SMC_RMI_VSMMU_DESTROY			SMC_RMI_CALL(0x016b)
+#define SMC_RMI_RMM_CONFIG_SET			SMC_RMI_CALL(0x016e)
+#define SMC_RMI_PSMMU_IRQ_NOTIFY		SMC_RMI_CALL(0x016f)
+
+#define SMC_RMI_PDEV_ABORT			SMC_RMI_CALL(0x0174)
+#define SMC_RMI_PDEV_COMMUNICATE		SMC_RMI_CALL(0x0175)
+#define SMC_RMI_PDEV_CREATE			SMC_RMI_CALL(0x0176)
+#define SMC_RMI_PDEV_DESTROY			SMC_RMI_CALL(0x0177)
+#define SMC_RMI_PDEV_GET_STATE			SMC_RMI_CALL(0x0178)
+
+#define SMC_RMI_PDEV_STREAM_KEY_REFRESH		SMC_RMI_CALL(0x017a)
+#define SMC_RMI_PDEV_SET_PUBKEY			SMC_RMI_CALL(0x017b)
+#define SMC_RMI_PDEV_STOP			SMC_RMI_CALL(0x017c)
+#define SMC_RMI_RTT_AUX_CREATE			SMC_RMI_CALL(0x017d)
+#define SMC_RMI_RTT_AUX_DESTROY			SMC_RMI_CALL(0x017e)
+#define SMC_RMI_RTT_AUX_FOLD			SMC_RMI_CALL(0x017f)
+
+#define SMC_RMI_VDEV_ABORT			SMC_RMI_CALL(0x0185)
+#define SMC_RMI_VDEV_COMMUNICATE		SMC_RMI_CALL(0x0186)
+#define SMC_RMI_VDEV_CREATE			SMC_RMI_CALL(0x0187)
+#define SMC_RMI_VDEV_DESTROY			SMC_RMI_CALL(0x0188)
+#define SMC_RMI_VDEV_GET_STATE			SMC_RMI_CALL(0x0189)
+#define SMC_RMI_VDEV_UNLOCK			SMC_RMI_CALL(0x018a)
+#define SMC_RMI_RTT_SET_S2AP			SMC_RMI_CALL(0x018b)
+#define SMC_RMI_VDEV_COMPLETE			SMC_RMI_CALL(0x018e)
+
+#define SMC_RMI_VDEV_GET_INTERFACE_REPORT	SMC_RMI_CALL(0x01d0)
+#define SMC_RMI_VDEV_GET_MEASUREMENTS		SMC_RMI_CALL(0x01d1)
+#define SMC_RMI_VDEV_LOCK			SMC_RMI_CALL(0x01d2)
+#define SMC_RMI_VDEV_START			SMC_RMI_CALL(0x01d3)
+
+#define SMC_RMI_VSMMU_EVENT_NOTIFY		SMC_RMI_CALL(0x01d6)
+#define SMC_RMI_PSMMU_ACTIVATE			SMC_RMI_CALL(0x01d7)
+#define SMC_RMI_PSMMU_DEACTIVATE		SMC_RMI_CALL(0x01d8)
+
+#define SMC_RMI_PSMMU_ST_L2_CREATE		SMC_RMI_CALL(0x01db)
+#define SMC_RMI_PSMMU_ST_L2_DESTROY		SMC_RMI_CALL(0x01dc)
+#define SMC_RMI_DPT_L0_CREATE			SMC_RMI_CALL(0x01dd)
+#define SMC_RMI_DPT_L0_DESTROY			SMC_RMI_CALL(0x01de)
+#define SMC_RMI_DPT_L1_CREATE			SMC_RMI_CALL(0x01df)
+#define SMC_RMI_DPT_L1_DESTROY			SMC_RMI_CALL(0x01e0)
+#define SMC_RMI_GRANULE_TRACKING_GET		SMC_RMI_CALL(0x01e1)
+
+#define SMC_RMI_GRANULE_TRACKING_SET		SMC_RMI_CALL(0x01e3)
+
+#define SMC_RMI_RMM_CONFIG_GET			SMC_RMI_CALL(0x01ec)
+
+#define SMC_RMI_RMM_STATE_GET			SMC_RMI_CALL(0x01ee)
+
+#define SMC_RMI_PSMMU_EVENT_CONSUME		SMC_RMI_CALL(0x01f0)
+#define SMC_RMI_GRANULE_RANGE_DELEGATE		SMC_RMI_CALL(0x01f1)
+#define SMC_RMI_GRANULE_RANGE_UNDELEGATE	SMC_RMI_CALL(0x01f2)
+#define SMC_RMI_GPT_L1_CREATE			SMC_RMI_CALL(0x01f3)
+#define SMC_RMI_GPT_L1_DESTROY			SMC_RMI_CALL(0x01f4)
+#define SMC_RMI_RTT_DATA_MAP			SMC_RMI_CALL(0x01f5)
+#define SMC_RMI_RTT_DATA_UNMAP			SMC_RMI_CALL(0x01f6)
+#define SMC_RMI_RTT_DEV_MAP			SMC_RMI_CALL(0x01f7)
+#define SMC_RMI_RTT_DEV_UNMAP			SMC_RMI_CALL(0x01f8)
+#define SMC_RMI_RTT_ARCH_DEV_MAP		SMC_RMI_CALL(0x01f9)
+#define SMC_RMI_RTT_ARCH_DEV_UNMAP		SMC_RMI_CALL(0x01fa)
+#define SMC_RMI_RTT_UNPROT_MAP			SMC_RMI_CALL(0x01fb)
+#define SMC_RMI_RTT_UNPROT_UNMAP		SMC_RMI_CALL(0x01fc)
+#define SMC_RMI_RTT_AUX_PROT_MAP		SMC_RMI_CALL(0x01fd)
+#define SMC_RMI_RTT_AUX_PROT_UNMAP		SMC_RMI_CALL(0x01fe)
+#define SMC_RMI_RTT_AUX_UNPROT_MAP		SMC_RMI_CALL(0x01ff)
+#define SMC_RMI_RTT_AUX_UNPROT_UNMAP		SMC_RMI_CALL(0x0200)
+#define SMC_RMI_REALM_TERMINATE			SMC_RMI_CALL(0x0201)
+#define SMC_RMI_RMM_ACTIVATE			SMC_RMI_CALL(0x0202)
+#define SMC_RMI_OP_CONTINUE			SMC_RMI_CALL(0x0203)
+#define SMC_RMI_PDEV_STREAM_CONNECT		SMC_RMI_CALL(0x0204)
+#define SMC_RMI_PDEV_STREAM_DISCONNECT		SMC_RMI_CALL(0x0205)
+#define SMC_RMI_PDEV_STREAM_COMPLETE		SMC_RMI_CALL(0x0206)
+#define SMC_RMI_PDEV_STREAM_KEY_PURGE		SMC_RMI_CALL(0x0207)
+#define SMC_RMI_OP_MEM_DONATE			SMC_RMI_CALL(0x0208)
+#define SMC_RMI_OP_MEM_RECLAIM			SMC_RMI_CALL(0x0209)
+#define SMC_RMI_OP_CANCEL			SMC_RMI_CALL(0x020a)
+#define SMC_RMI_VSMMU_FEATURES			SMC_RMI_CALL(0x020b)
+#define SMC_RMI_VSMMU_CMD_GET			SMC_RMI_CALL(0x020c)
+#define SMC_RMI_VSMMU_CMD_COMPLETE		SMC_RMI_CALL(0x020d)
+#define SMC_RMI_PSMMU_INFO			SMC_RMI_CALL(0x020e)
+
+#define RMI_ABI_MAJOR_VERSION	2
+#define RMI_ABI_MINOR_VERSION	0
+
+#define RMI_ABI_VERSION_GET_MAJOR(version) ((version) >> 16)
+#define RMI_ABI_VERSION_GET_MINOR(version) ((version) & 0xFFFF)

arch/arm64/include/asm/rmi_smc.h line 139

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+#define RMI_ABI_VERSION(major, minor)      (((major) << 16) | (minor))
+
+#define RMI_UNASSIGNED			0
+#define RMI_ASSIGNED			1
+#define RMI_TABLE			2
+
+#define RMI_RETURN_STATUS(ret)		((ret) & 0xFF)
+#define RMI_RETURN_INDEX(ret)		(((ret) >> 8) & 0xFF)
+#define RMI_RETURN_MEMREQ(ret)		(((ret) >> 8) & 0x3)
+#define RMI_RETURN_CAN_CANCEL(ret)	(((ret) >> 10) & 0x1)

Use FIELD_GET() and specify masks that define the actual fields.

Quoted 18 lines
arch/arm64/include/asm/rmi_smc.h new file +18 -0
+
+#define RMI_SUCCESS			0
+#define RMI_ERROR_INPUT			1
+#define RMI_ERROR_REALM			2
+#define RMI_ERROR_REC			3
+#define RMI_ERROR_RTT			4
+#define RMI_ERROR_NOT_SUPPORTED		5
+#define RMI_ERROR_DEVICE		6
+#define RMI_ERROR_RTT_AUX		7
+#define RMI_ERROR_PSMMU_ST		8
+#define RMI_ERROR_DPT			9
+#define RMI_BUSY			10
+#define RMI_ERROR_GLOBAL		11
+#define RMI_ERROR_TRACKING		12
+#define RMI_INCOMPLETE			13
+#define RMI_BLOCKED			14
+#define RMI_ERROR_GPT			15
+#define RMI_ERROR_GRANULE		16

arch/arm64/include/asm/rmi_smc.h line 167

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+
+#define RMI_OP_MEM_REQ_NONE		0
+#define RMI_OP_MEM_REQ_DONATE		1
+#define RMI_OP_MEM_REQ_RECLAIM		2
+
+#define RMI_DONATE_SIZE(req)		((req) & 0x3)
+#define RMI_DONATE_COUNT_MASK		GENMASK(15, 2)
+#define RMI_DONATE_COUNT(req)		(((req) & RMI_DONATE_COUNT_MASK) >> 2)
+#define RMI_DONATE_CONTIG(req)		(!!((req) & BIT(16)))
+#define RMI_DONATE_STATE(req)		(!!((req) & BIT(17)))

FIELD_GET().

Quoted 78 lines
arch/arm64/include/asm/rmi_smc.h new file +78 -0
+
+#define RMI_OP_MEM_DELEGATED		0
+#define RMI_OP_MEM_UNDELEGATED		1
+
+#define RMI_ADDR_TYPE_NONE		0
+#define RMI_ADDR_TYPE_SINGLE		1
+#define RMI_ADDR_TYPE_LIST		2
+
+#define RMI_ADDR_RANGE_SIZE_MASK	GENMASK(1, 0)
+#define RMI_ADDR_RANGE_COUNT_MASK	GENMASK(PAGE_SHIFT - 1, 2)
+#define RMI_ADDR_RANGE_ADDR_MASK	(PAGE_MASK & GENMASK(51, 0))
+#define RMI_ADDR_RANGE_STATE_MASK	BIT(63)
+
+#define RMI_ADDR_RANGE_SIZE(ar)		(FIELD_GET(RMI_ADDR_RANGE_SIZE_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_COUNT(ar)	(FIELD_GET(RMI_ADDR_RANGE_COUNT_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_ADDR(ar)		((ar) & RMI_ADDR_RANGE_ADDR_MASK)
+#define RMI_ADDR_RANGE_STATE(ar)	(FIELD_GET(RMI_ADDR_RANGE_STATE_MASK, \
+						   (ar)))
+
+enum rmi_ripas {
+	RMI_EMPTY = 0,
+	RMI_RAM = 1,
+	RMI_DESTROYED = 2,
+	RMI_DEV = 3,
+};
+
+#define RMI_NO_MEASURE_CONTENT	0
+#define RMI_MEASURE_CONTENT	1
+
+#define RMI_FEATURE_REGISTER_0_S2SZ		GENMASK(7, 0)
+#define RMI_FEATURE_REGISTER_0_LPA2		BIT(8)
+#define RMI_FEATURE_REGISTER_0_SVE		BIT(9)
+#define RMI_FEATURE_REGISTER_0_SVE_VL		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_0_NUM_BPS		GENMASK(19, 14)
+#define RMI_FEATURE_REGISTER_0_NUM_WPS		GENMASK(25, 20)
+#define RMI_FEATURE_REGISTER_0_PMU		BIT(26)
+#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS	GENMASK(31, 27)
+
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB	BIT(0)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB	BIT(1)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB	BIT(2)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_256	BIT(3)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_384	BIT(4)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_512	BIT(5)
+#define RMI_FEATURE_REGISTER_1_MAX_RECS_ORDER	GENMASK(9, 6)
+#define RMI_FEATURE_REGISTER_1_L0GPTSZ		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_1_PPS		GENMASK(16, 14)
+
+#define RMI_FEATURE_REGISTER_2_DA		BIT(0)
+#define RMI_FEATURE_REGISTER_2_DA_COH		BIT(1)
+#define RMI_FEATURE_REGISTER_2_VSMMU		BIT(2)
+#define RMI_FEATURE_REGISTER_2_ATS		BIT(3)
+#define RMI_FEATURE_REGISTER_2_MAX_VDEVS_ORDER	GENMASK(7, 4)
+#define RMI_FEATURE_REGISTER_2_VDEV_KROU	BIT(8)
+#define RMI_FEATURE_REGISTER_2_NON_TEE_STREAM	BIT(9)
+
+#define RMI_FEATURE_REGISTER_3_MAX_NUM_AUX_PLANES	GENMASK(3, 0)
+#define RMI_FEATURE_REGISTER_3_RTT_PLAN			GENMASK(5, 4)
+#define RMI_FEATURE_REGISTER_3_RTT_S2AP_INDIRECT	BIT(6)
+
+#define RMI_FEATURE_REGISTER_4_MEC_COUNT		GENMASK(63, 0)
+
+#define RMI_MEM_CATEGORY_CONVENTIONAL		0
+#define RMI_MEM_CATEGORY_DEV_NCOH		1
+#define RMI_MEM_CATEGORY_DEV_COH		2
+
+#define RMI_TRACKING_RESERVED			0
+#define RMI_TRACKING_NONE			1
+#define RMI_TRACKING_FINE			2
+#define RMI_TRACKING_COARSE			3
+
+#define RMI_GRANULE_SIZE_4KB	0
+#define RMI_GRANULE_SIZE_16KB	1
+#define RMI_GRANULE_SIZE_64KB	2
+
+/*

arch/arm64/include/asm/rmi_smc.h line 255

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+ * Note many of these fields are smaller than u64 but all fields have u64
+ * alignment, so use u64 to ensure correct alignment.
+ */
+struct rmm_config {
+	union { /* 0x0 */
+		struct {
+			u64 tracking_region_size;
+			u64 rmi_granule_size;
+		};
+		u8 sizer[0x1000];

SZ_4K?

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+	};
+};
+
+#define RMI_REALM_PARAM_FLAG_LPA2		BIT(0)
+#define RMI_REALM_PARAM_FLAG_SVE		BIT(1)
+#define RMI_REALM_PARAM_FLAG_PMU		BIT(2)
+
+struct realm_params {
+	union { /* 0x0 */
+		struct {

arch/arm64/include/asm/rmi_smc.h line 275

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+			u64 num_aux_planes;
+		};
+		u8 padding0[0x400];

SZ_1K? And similarly all over the shop?

I haven't checked the details of the encodings (life is too short), but I wonder how much of this exists as an MRS and could be automatically generated?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#14 lore raw

Re: [PATCH v14 04/44] arm64: RMI: Add SMC definitions for calling the RMM

Quoted 336 lines · Marc Zyngier
Quoted 51 lines · Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

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

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

arch/arm64/include/asm/rmi_smc.h new file +0 -0
new file mode 100644
index 000000000000..a09b7a631fef

arch/arm64/include/asm/rmi_smc.h line 7

Quoted 7 lines
arch/arm64/include/asm/rmi_smc.h new file +7 -0
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/

How long is this spec going to be available on the ARM web site, which has a tendency of being reorganised every other week? And there is already a beta2.

Quoted 122 lines
arch/arm64/include/asm/rmi_smc.h new file +122 -0
+ */
+
+#ifndef __ASM_RMI_SMC_H
+#define __ASM_RMI_SMC_H
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RMI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD,	\
+			   (func))
+
+#define SMC_RMI_VERSION				SMC_RMI_CALL(0x0150)
+
+#define SMC_RMI_RTT_DATA_MAP_INIT		SMC_RMI_CALL(0x0153)
+
+#define SMC_RMI_REALM_ACTIVATE			SMC_RMI_CALL(0x0157)
+#define SMC_RMI_REALM_CREATE			SMC_RMI_CALL(0x0158)
+#define SMC_RMI_REALM_DESTROY			SMC_RMI_CALL(0x0159)
+#define SMC_RMI_REC_CREATE			SMC_RMI_CALL(0x015a)
+#define SMC_RMI_REC_DESTROY			SMC_RMI_CALL(0x015b)
+#define SMC_RMI_REC_ENTER			SMC_RMI_CALL(0x015c)
+#define SMC_RMI_RTT_CREATE			SMC_RMI_CALL(0x015d)
+#define SMC_RMI_RTT_DESTROY			SMC_RMI_CALL(0x015e)
+
+#define SMC_RMI_RTT_READ_ENTRY			SMC_RMI_CALL(0x0161)
+
+#define SMC_RMI_RTT_DEV_VALIDATE		SMC_RMI_CALL(0x0163)
+#define SMC_RMI_PSCI_COMPLETE			SMC_RMI_CALL(0x0164)
+#define SMC_RMI_FEATURES			SMC_RMI_CALL(0x0165)
+#define SMC_RMI_RTT_FOLD			SMC_RMI_CALL(0x0166)
+
+#define SMC_RMI_RTT_INIT_RIPAS			SMC_RMI_CALL(0x0168)
+#define SMC_RMI_RTT_SET_RIPAS			SMC_RMI_CALL(0x0169)
+#define SMC_RMI_VSMMU_CREATE			SMC_RMI_CALL(0x016a)
+#define SMC_RMI_VSMMU_DESTROY			SMC_RMI_CALL(0x016b)
+#define SMC_RMI_RMM_CONFIG_SET			SMC_RMI_CALL(0x016e)
+#define SMC_RMI_PSMMU_IRQ_NOTIFY		SMC_RMI_CALL(0x016f)
+
+#define SMC_RMI_PDEV_ABORT			SMC_RMI_CALL(0x0174)
+#define SMC_RMI_PDEV_COMMUNICATE		SMC_RMI_CALL(0x0175)
+#define SMC_RMI_PDEV_CREATE			SMC_RMI_CALL(0x0176)
+#define SMC_RMI_PDEV_DESTROY			SMC_RMI_CALL(0x0177)
+#define SMC_RMI_PDEV_GET_STATE			SMC_RMI_CALL(0x0178)
+
+#define SMC_RMI_PDEV_STREAM_KEY_REFRESH		SMC_RMI_CALL(0x017a)
+#define SMC_RMI_PDEV_SET_PUBKEY			SMC_RMI_CALL(0x017b)
+#define SMC_RMI_PDEV_STOP			SMC_RMI_CALL(0x017c)
+#define SMC_RMI_RTT_AUX_CREATE			SMC_RMI_CALL(0x017d)
+#define SMC_RMI_RTT_AUX_DESTROY			SMC_RMI_CALL(0x017e)
+#define SMC_RMI_RTT_AUX_FOLD			SMC_RMI_CALL(0x017f)
+
+#define SMC_RMI_VDEV_ABORT			SMC_RMI_CALL(0x0185)
+#define SMC_RMI_VDEV_COMMUNICATE		SMC_RMI_CALL(0x0186)
+#define SMC_RMI_VDEV_CREATE			SMC_RMI_CALL(0x0187)
+#define SMC_RMI_VDEV_DESTROY			SMC_RMI_CALL(0x0188)
+#define SMC_RMI_VDEV_GET_STATE			SMC_RMI_CALL(0x0189)
+#define SMC_RMI_VDEV_UNLOCK			SMC_RMI_CALL(0x018a)
+#define SMC_RMI_RTT_SET_S2AP			SMC_RMI_CALL(0x018b)
+#define SMC_RMI_VDEV_COMPLETE			SMC_RMI_CALL(0x018e)
+
+#define SMC_RMI_VDEV_GET_INTERFACE_REPORT	SMC_RMI_CALL(0x01d0)
+#define SMC_RMI_VDEV_GET_MEASUREMENTS		SMC_RMI_CALL(0x01d1)
+#define SMC_RMI_VDEV_LOCK			SMC_RMI_CALL(0x01d2)
+#define SMC_RMI_VDEV_START			SMC_RMI_CALL(0x01d3)
+
+#define SMC_RMI_VSMMU_EVENT_NOTIFY		SMC_RMI_CALL(0x01d6)
+#define SMC_RMI_PSMMU_ACTIVATE			SMC_RMI_CALL(0x01d7)
+#define SMC_RMI_PSMMU_DEACTIVATE		SMC_RMI_CALL(0x01d8)
+
+#define SMC_RMI_PSMMU_ST_L2_CREATE		SMC_RMI_CALL(0x01db)
+#define SMC_RMI_PSMMU_ST_L2_DESTROY		SMC_RMI_CALL(0x01dc)
+#define SMC_RMI_DPT_L0_CREATE			SMC_RMI_CALL(0x01dd)
+#define SMC_RMI_DPT_L0_DESTROY			SMC_RMI_CALL(0x01de)
+#define SMC_RMI_DPT_L1_CREATE			SMC_RMI_CALL(0x01df)
+#define SMC_RMI_DPT_L1_DESTROY			SMC_RMI_CALL(0x01e0)
+#define SMC_RMI_GRANULE_TRACKING_GET		SMC_RMI_CALL(0x01e1)
+
+#define SMC_RMI_GRANULE_TRACKING_SET		SMC_RMI_CALL(0x01e3)
+
+#define SMC_RMI_RMM_CONFIG_GET			SMC_RMI_CALL(0x01ec)
+
+#define SMC_RMI_RMM_STATE_GET			SMC_RMI_CALL(0x01ee)
+
+#define SMC_RMI_PSMMU_EVENT_CONSUME		SMC_RMI_CALL(0x01f0)
+#define SMC_RMI_GRANULE_RANGE_DELEGATE		SMC_RMI_CALL(0x01f1)
+#define SMC_RMI_GRANULE_RANGE_UNDELEGATE	SMC_RMI_CALL(0x01f2)
+#define SMC_RMI_GPT_L1_CREATE			SMC_RMI_CALL(0x01f3)
+#define SMC_RMI_GPT_L1_DESTROY			SMC_RMI_CALL(0x01f4)
+#define SMC_RMI_RTT_DATA_MAP			SMC_RMI_CALL(0x01f5)
+#define SMC_RMI_RTT_DATA_UNMAP			SMC_RMI_CALL(0x01f6)
+#define SMC_RMI_RTT_DEV_MAP			SMC_RMI_CALL(0x01f7)
+#define SMC_RMI_RTT_DEV_UNMAP			SMC_RMI_CALL(0x01f8)
+#define SMC_RMI_RTT_ARCH_DEV_MAP		SMC_RMI_CALL(0x01f9)
+#define SMC_RMI_RTT_ARCH_DEV_UNMAP		SMC_RMI_CALL(0x01fa)
+#define SMC_RMI_RTT_UNPROT_MAP			SMC_RMI_CALL(0x01fb)
+#define SMC_RMI_RTT_UNPROT_UNMAP		SMC_RMI_CALL(0x01fc)
+#define SMC_RMI_RTT_AUX_PROT_MAP		SMC_RMI_CALL(0x01fd)
+#define SMC_RMI_RTT_AUX_PROT_UNMAP		SMC_RMI_CALL(0x01fe)
+#define SMC_RMI_RTT_AUX_UNPROT_MAP		SMC_RMI_CALL(0x01ff)
+#define SMC_RMI_RTT_AUX_UNPROT_UNMAP		SMC_RMI_CALL(0x0200)
+#define SMC_RMI_REALM_TERMINATE			SMC_RMI_CALL(0x0201)
+#define SMC_RMI_RMM_ACTIVATE			SMC_RMI_CALL(0x0202)
+#define SMC_RMI_OP_CONTINUE			SMC_RMI_CALL(0x0203)
+#define SMC_RMI_PDEV_STREAM_CONNECT		SMC_RMI_CALL(0x0204)
+#define SMC_RMI_PDEV_STREAM_DISCONNECT		SMC_RMI_CALL(0x0205)
+#define SMC_RMI_PDEV_STREAM_COMPLETE		SMC_RMI_CALL(0x0206)
+#define SMC_RMI_PDEV_STREAM_KEY_PURGE		SMC_RMI_CALL(0x0207)
+#define SMC_RMI_OP_MEM_DONATE			SMC_RMI_CALL(0x0208)
+#define SMC_RMI_OP_MEM_RECLAIM			SMC_RMI_CALL(0x0209)
+#define SMC_RMI_OP_CANCEL			SMC_RMI_CALL(0x020a)
+#define SMC_RMI_VSMMU_FEATURES			SMC_RMI_CALL(0x020b)
+#define SMC_RMI_VSMMU_CMD_GET			SMC_RMI_CALL(0x020c)
+#define SMC_RMI_VSMMU_CMD_COMPLETE		SMC_RMI_CALL(0x020d)
+#define SMC_RMI_PSMMU_INFO			SMC_RMI_CALL(0x020e)
+
+#define RMI_ABI_MAJOR_VERSION	2
+#define RMI_ABI_MINOR_VERSION	0
+
+#define RMI_ABI_VERSION_GET_MAJOR(version) ((version) >> 16)
+#define RMI_ABI_VERSION_GET_MINOR(version) ((version) & 0xFFFF)

arch/arm64/include/asm/rmi_smc.h line 139

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+#define RMI_ABI_VERSION(major, minor)      (((major) << 16) | (minor))
+
+#define RMI_UNASSIGNED			0
+#define RMI_ASSIGNED			1
+#define RMI_TABLE			2
+
+#define RMI_RETURN_STATUS(ret)		((ret) & 0xFF)
+#define RMI_RETURN_INDEX(ret)		(((ret) >> 8) & 0xFF)
+#define RMI_RETURN_MEMREQ(ret)		(((ret) >> 8) & 0x3)
+#define RMI_RETURN_CAN_CANCEL(ret)	(((ret) >> 10) & 0x1)

Use FIELD_GET() and specify masks that define the actual fields.

Quoted 18 lines
arch/arm64/include/asm/rmi_smc.h new file +18 -0
+
+#define RMI_SUCCESS			0
+#define RMI_ERROR_INPUT			1
+#define RMI_ERROR_REALM			2
+#define RMI_ERROR_REC			3
+#define RMI_ERROR_RTT			4
+#define RMI_ERROR_NOT_SUPPORTED		5
+#define RMI_ERROR_DEVICE		6
+#define RMI_ERROR_RTT_AUX		7
+#define RMI_ERROR_PSMMU_ST		8
+#define RMI_ERROR_DPT			9
+#define RMI_BUSY			10
+#define RMI_ERROR_GLOBAL		11
+#define RMI_ERROR_TRACKING		12
+#define RMI_INCOMPLETE			13
+#define RMI_BLOCKED			14
+#define RMI_ERROR_GPT			15
+#define RMI_ERROR_GRANULE		16

arch/arm64/include/asm/rmi_smc.h line 167

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+
+#define RMI_OP_MEM_REQ_NONE		0
+#define RMI_OP_MEM_REQ_DONATE		1
+#define RMI_OP_MEM_REQ_RECLAIM		2
+
+#define RMI_DONATE_SIZE(req)		((req) & 0x3)
+#define RMI_DONATE_COUNT_MASK		GENMASK(15, 2)
+#define RMI_DONATE_COUNT(req)		(((req) & RMI_DONATE_COUNT_MASK) >> 2)
+#define RMI_DONATE_CONTIG(req)		(!!((req) & BIT(16)))
+#define RMI_DONATE_STATE(req)		(!!((req) & BIT(17)))

FIELD_GET().

Quoted 78 lines
arch/arm64/include/asm/rmi_smc.h new file +78 -0
+
+#define RMI_OP_MEM_DELEGATED		0
+#define RMI_OP_MEM_UNDELEGATED		1
+
+#define RMI_ADDR_TYPE_NONE		0
+#define RMI_ADDR_TYPE_SINGLE		1
+#define RMI_ADDR_TYPE_LIST		2
+
+#define RMI_ADDR_RANGE_SIZE_MASK	GENMASK(1, 0)
+#define RMI_ADDR_RANGE_COUNT_MASK	GENMASK(PAGE_SHIFT - 1, 2)
+#define RMI_ADDR_RANGE_ADDR_MASK	(PAGE_MASK & GENMASK(51, 0))
+#define RMI_ADDR_RANGE_STATE_MASK	BIT(63)
+
+#define RMI_ADDR_RANGE_SIZE(ar)		(FIELD_GET(RMI_ADDR_RANGE_SIZE_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_COUNT(ar)	(FIELD_GET(RMI_ADDR_RANGE_COUNT_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_ADDR(ar)		((ar) & RMI_ADDR_RANGE_ADDR_MASK)
+#define RMI_ADDR_RANGE_STATE(ar)	(FIELD_GET(RMI_ADDR_RANGE_STATE_MASK, \
+						   (ar)))
+
+enum rmi_ripas {
+	RMI_EMPTY = 0,
+	RMI_RAM = 1,
+	RMI_DESTROYED = 2,
+	RMI_DEV = 3,
+};
+
+#define RMI_NO_MEASURE_CONTENT	0
+#define RMI_MEASURE_CONTENT	1
+
+#define RMI_FEATURE_REGISTER_0_S2SZ		GENMASK(7, 0)
+#define RMI_FEATURE_REGISTER_0_LPA2		BIT(8)
+#define RMI_FEATURE_REGISTER_0_SVE		BIT(9)
+#define RMI_FEATURE_REGISTER_0_SVE_VL		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_0_NUM_BPS		GENMASK(19, 14)
+#define RMI_FEATURE_REGISTER_0_NUM_WPS		GENMASK(25, 20)
+#define RMI_FEATURE_REGISTER_0_PMU		BIT(26)
+#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS	GENMASK(31, 27)
+
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB	BIT(0)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB	BIT(1)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB	BIT(2)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_256	BIT(3)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_384	BIT(4)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_512	BIT(5)
+#define RMI_FEATURE_REGISTER_1_MAX_RECS_ORDER	GENMASK(9, 6)
+#define RMI_FEATURE_REGISTER_1_L0GPTSZ		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_1_PPS		GENMASK(16, 14)
+
+#define RMI_FEATURE_REGISTER_2_DA		BIT(0)
+#define RMI_FEATURE_REGISTER_2_DA_COH		BIT(1)
+#define RMI_FEATURE_REGISTER_2_VSMMU		BIT(2)
+#define RMI_FEATURE_REGISTER_2_ATS		BIT(3)
+#define RMI_FEATURE_REGISTER_2_MAX_VDEVS_ORDER	GENMASK(7, 4)
+#define RMI_FEATURE_REGISTER_2_VDEV_KROU	BIT(8)
+#define RMI_FEATURE_REGISTER_2_NON_TEE_STREAM	BIT(9)
+
+#define RMI_FEATURE_REGISTER_3_MAX_NUM_AUX_PLANES	GENMASK(3, 0)
+#define RMI_FEATURE_REGISTER_3_RTT_PLAN			GENMASK(5, 4)
+#define RMI_FEATURE_REGISTER_3_RTT_S2AP_INDIRECT	BIT(6)
+
+#define RMI_FEATURE_REGISTER_4_MEC_COUNT		GENMASK(63, 0)
+
+#define RMI_MEM_CATEGORY_CONVENTIONAL		0
+#define RMI_MEM_CATEGORY_DEV_NCOH		1
+#define RMI_MEM_CATEGORY_DEV_COH		2
+
+#define RMI_TRACKING_RESERVED			0
+#define RMI_TRACKING_NONE			1
+#define RMI_TRACKING_FINE			2
+#define RMI_TRACKING_COARSE			3
+
+#define RMI_GRANULE_SIZE_4KB	0
+#define RMI_GRANULE_SIZE_16KB	1
+#define RMI_GRANULE_SIZE_64KB	2
+
+/*

arch/arm64/include/asm/rmi_smc.h line 255

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+ * Note many of these fields are smaller than u64 but all fields have u64
+ * alignment, so use u64 to ensure correct alignment.
+ */
+struct rmm_config {
+	union { /* 0x0 */
+		struct {
+			u64 tracking_region_size;
+			u64 rmi_granule_size;
+		};
+		u8 sizer[0x1000];

SZ_4K?

Quoted 14 lines
arch/arm64/include/asm/rmi_smc.h new file +14 -0
+	};
+};
+
+#define RMI_REALM_PARAM_FLAG_LPA2		BIT(0)
+#define RMI_REALM_PARAM_FLAG_SVE		BIT(1)
+#define RMI_REALM_PARAM_FLAG_PMU		BIT(2)
+
+struct realm_params {
+	union { /* 0x0 */
+		struct {
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h line 275

Quoted 6 lines
arch/arm64/include/asm/rmi_smc.h new file +6 -0
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+			u64 num_aux_planes;
+		};
+		u8 padding0[0x400];

SZ_1K? And similarly all over the shop?

Agreed to the comments above.

Quoted 4 lines

I haven't checked the details of the encodings (life is too short), but I wonder how much of this exists as an MRS and could be automatically generated?

Good point. This is something that we can check and get back to you.

Thanks
Suzuki

Quoted 5 lines

Thanks,

	M.
#15 lore raw

Re: [PATCH v14 04/44] arm64: RMI: Add SMC definitions for calling the RMM

In reply to Marc Zyngier
Quoted 56 lines · Marc Zyngier
Quoted 54 lines · Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

---
 arch/arm64/include/asm/rmi_smc.h | 448 +++++++++++++++++++++++++++++++
 1 file changed, 448 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +3 -0
new file mode 100644
index 000000000000..a09b7a631fef
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h line 7

Quoted 4 lines
arch/arm64/include/asm/rmi_smc.h new file +4 -0
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/

How long is this spec going to be available on the ARM web site, which has a tendency of being reorganised every other week? And there is already a beta2.

Obviously I can't predict the next reorganisation - but at least it's a link that could be fed into archive.org or similar.

There is a beta2 - that was released just after this series. I'll obviously be updating to that shortly. Sadly the spec is still a bit of a moving target, but hopefully all the major changes have already happened.

Quoted 127 lines
Quoted 127 lines
arch/arm64/include/asm/rmi_smc.h new file +126 -0
+ */
+
+#ifndef __ASM_RMI_SMC_H
+#define __ASM_RMI_SMC_H
+
+#include <linux/arm-smccc.h>
+
+#define SMC_RMI_CALL(func)				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,		\
+			   ARM_SMCCC_SMC_64,		\
+			   ARM_SMCCC_OWNER_STANDARD,	\
+			   (func))
+
+#define SMC_RMI_VERSION				SMC_RMI_CALL(0x0150)
+
+#define SMC_RMI_RTT_DATA_MAP_INIT		SMC_RMI_CALL(0x0153)
+
+#define SMC_RMI_REALM_ACTIVATE			SMC_RMI_CALL(0x0157)
+#define SMC_RMI_REALM_CREATE			SMC_RMI_CALL(0x0158)
+#define SMC_RMI_REALM_DESTROY			SMC_RMI_CALL(0x0159)
+#define SMC_RMI_REC_CREATE			SMC_RMI_CALL(0x015a)
+#define SMC_RMI_REC_DESTROY			SMC_RMI_CALL(0x015b)
+#define SMC_RMI_REC_ENTER			SMC_RMI_CALL(0x015c)
+#define SMC_RMI_RTT_CREATE			SMC_RMI_CALL(0x015d)
+#define SMC_RMI_RTT_DESTROY			SMC_RMI_CALL(0x015e)
+
+#define SMC_RMI_RTT_READ_ENTRY			SMC_RMI_CALL(0x0161)
+
+#define SMC_RMI_RTT_DEV_VALIDATE		SMC_RMI_CALL(0x0163)
+#define SMC_RMI_PSCI_COMPLETE			SMC_RMI_CALL(0x0164)
+#define SMC_RMI_FEATURES			SMC_RMI_CALL(0x0165)
+#define SMC_RMI_RTT_FOLD			SMC_RMI_CALL(0x0166)
+
+#define SMC_RMI_RTT_INIT_RIPAS			SMC_RMI_CALL(0x0168)
+#define SMC_RMI_RTT_SET_RIPAS			SMC_RMI_CALL(0x0169)
+#define SMC_RMI_VSMMU_CREATE			SMC_RMI_CALL(0x016a)
+#define SMC_RMI_VSMMU_DESTROY			SMC_RMI_CALL(0x016b)
+#define SMC_RMI_RMM_CONFIG_SET			SMC_RMI_CALL(0x016e)
+#define SMC_RMI_PSMMU_IRQ_NOTIFY		SMC_RMI_CALL(0x016f)
+
+#define SMC_RMI_PDEV_ABORT			SMC_RMI_CALL(0x0174)
+#define SMC_RMI_PDEV_COMMUNICATE		SMC_RMI_CALL(0x0175)
+#define SMC_RMI_PDEV_CREATE			SMC_RMI_CALL(0x0176)
+#define SMC_RMI_PDEV_DESTROY			SMC_RMI_CALL(0x0177)
+#define SMC_RMI_PDEV_GET_STATE			SMC_RMI_CALL(0x0178)
+
+#define SMC_RMI_PDEV_STREAM_KEY_REFRESH		SMC_RMI_CALL(0x017a)
+#define SMC_RMI_PDEV_SET_PUBKEY			SMC_RMI_CALL(0x017b)
+#define SMC_RMI_PDEV_STOP			SMC_RMI_CALL(0x017c)
+#define SMC_RMI_RTT_AUX_CREATE			SMC_RMI_CALL(0x017d)
+#define SMC_RMI_RTT_AUX_DESTROY			SMC_RMI_CALL(0x017e)
+#define SMC_RMI_RTT_AUX_FOLD			SMC_RMI_CALL(0x017f)
+
+#define SMC_RMI_VDEV_ABORT			SMC_RMI_CALL(0x0185)
+#define SMC_RMI_VDEV_COMMUNICATE		SMC_RMI_CALL(0x0186)
+#define SMC_RMI_VDEV_CREATE			SMC_RMI_CALL(0x0187)
+#define SMC_RMI_VDEV_DESTROY			SMC_RMI_CALL(0x0188)
+#define SMC_RMI_VDEV_GET_STATE			SMC_RMI_CALL(0x0189)
+#define SMC_RMI_VDEV_UNLOCK			SMC_RMI_CALL(0x018a)
+#define SMC_RMI_RTT_SET_S2AP			SMC_RMI_CALL(0x018b)
+#define SMC_RMI_VDEV_COMPLETE			SMC_RMI_CALL(0x018e)
+
+#define SMC_RMI_VDEV_GET_INTERFACE_REPORT	SMC_RMI_CALL(0x01d0)
+#define SMC_RMI_VDEV_GET_MEASUREMENTS		SMC_RMI_CALL(0x01d1)
+#define SMC_RMI_VDEV_LOCK			SMC_RMI_CALL(0x01d2)
+#define SMC_RMI_VDEV_START			SMC_RMI_CALL(0x01d3)
+
+#define SMC_RMI_VSMMU_EVENT_NOTIFY		SMC_RMI_CALL(0x01d6)
+#define SMC_RMI_PSMMU_ACTIVATE			SMC_RMI_CALL(0x01d7)
+#define SMC_RMI_PSMMU_DEACTIVATE		SMC_RMI_CALL(0x01d8)
+
+#define SMC_RMI_PSMMU_ST_L2_CREATE		SMC_RMI_CALL(0x01db)
+#define SMC_RMI_PSMMU_ST_L2_DESTROY		SMC_RMI_CALL(0x01dc)
+#define SMC_RMI_DPT_L0_CREATE			SMC_RMI_CALL(0x01dd)
+#define SMC_RMI_DPT_L0_DESTROY			SMC_RMI_CALL(0x01de)
+#define SMC_RMI_DPT_L1_CREATE			SMC_RMI_CALL(0x01df)
+#define SMC_RMI_DPT_L1_DESTROY			SMC_RMI_CALL(0x01e0)
+#define SMC_RMI_GRANULE_TRACKING_GET		SMC_RMI_CALL(0x01e1)
+
+#define SMC_RMI_GRANULE_TRACKING_SET		SMC_RMI_CALL(0x01e3)
+
+#define SMC_RMI_RMM_CONFIG_GET			SMC_RMI_CALL(0x01ec)
+
+#define SMC_RMI_RMM_STATE_GET			SMC_RMI_CALL(0x01ee)
+
+#define SMC_RMI_PSMMU_EVENT_CONSUME		SMC_RMI_CALL(0x01f0)
+#define SMC_RMI_GRANULE_RANGE_DELEGATE		SMC_RMI_CALL(0x01f1)
+#define SMC_RMI_GRANULE_RANGE_UNDELEGATE	SMC_RMI_CALL(0x01f2)
+#define SMC_RMI_GPT_L1_CREATE			SMC_RMI_CALL(0x01f3)
+#define SMC_RMI_GPT_L1_DESTROY			SMC_RMI_CALL(0x01f4)
+#define SMC_RMI_RTT_DATA_MAP			SMC_RMI_CALL(0x01f5)
+#define SMC_RMI_RTT_DATA_UNMAP			SMC_RMI_CALL(0x01f6)
+#define SMC_RMI_RTT_DEV_MAP			SMC_RMI_CALL(0x01f7)
+#define SMC_RMI_RTT_DEV_UNMAP			SMC_RMI_CALL(0x01f8)
+#define SMC_RMI_RTT_ARCH_DEV_MAP		SMC_RMI_CALL(0x01f9)
+#define SMC_RMI_RTT_ARCH_DEV_UNMAP		SMC_RMI_CALL(0x01fa)
+#define SMC_RMI_RTT_UNPROT_MAP			SMC_RMI_CALL(0x01fb)
+#define SMC_RMI_RTT_UNPROT_UNMAP		SMC_RMI_CALL(0x01fc)
+#define SMC_RMI_RTT_AUX_PROT_MAP		SMC_RMI_CALL(0x01fd)
+#define SMC_RMI_RTT_AUX_PROT_UNMAP		SMC_RMI_CALL(0x01fe)
+#define SMC_RMI_RTT_AUX_UNPROT_MAP		SMC_RMI_CALL(0x01ff)
+#define SMC_RMI_RTT_AUX_UNPROT_UNMAP		SMC_RMI_CALL(0x0200)
+#define SMC_RMI_REALM_TERMINATE			SMC_RMI_CALL(0x0201)
+#define SMC_RMI_RMM_ACTIVATE			SMC_RMI_CALL(0x0202)
+#define SMC_RMI_OP_CONTINUE			SMC_RMI_CALL(0x0203)
+#define SMC_RMI_PDEV_STREAM_CONNECT		SMC_RMI_CALL(0x0204)
+#define SMC_RMI_PDEV_STREAM_DISCONNECT		SMC_RMI_CALL(0x0205)
+#define SMC_RMI_PDEV_STREAM_COMPLETE		SMC_RMI_CALL(0x0206)
+#define SMC_RMI_PDEV_STREAM_KEY_PURGE		SMC_RMI_CALL(0x0207)
+#define SMC_RMI_OP_MEM_DONATE			SMC_RMI_CALL(0x0208)
+#define SMC_RMI_OP_MEM_RECLAIM			SMC_RMI_CALL(0x0209)
+#define SMC_RMI_OP_CANCEL			SMC_RMI_CALL(0x020a)
+#define SMC_RMI_VSMMU_FEATURES			SMC_RMI_CALL(0x020b)
+#define SMC_RMI_VSMMU_CMD_GET			SMC_RMI_CALL(0x020c)
+#define SMC_RMI_VSMMU_CMD_COMPLETE		SMC_RMI_CALL(0x020d)
+#define SMC_RMI_PSMMU_INFO			SMC_RMI_CALL(0x020e)
+
+#define RMI_ABI_MAJOR_VERSION	2
+#define RMI_ABI_MINOR_VERSION	0
+
+#define RMI_ABI_VERSION_GET_MAJOR(version) ((version) >> 16)
+#define RMI_ABI_VERSION_GET_MINOR(version) ((version) & 0xFFFF)
+#define RMI_ABI_VERSION(major, minor)      (((major) << 16) | (minor))
+
+#define RMI_UNASSIGNED			0
+#define RMI_ASSIGNED			1
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h line 139

Quoted 6 lines
arch/arm64/include/asm/rmi_smc.h new file +6 -0
+#define RMI_TABLE			2
+
+#define RMI_RETURN_STATUS(ret)		((ret) & 0xFF)
+#define RMI_RETURN_INDEX(ret)		(((ret) >> 8) & 0xFF)
+#define RMI_RETURN_MEMREQ(ret)		(((ret) >> 8) & 0x3)
+#define RMI_RETURN_CAN_CANCEL(ret)	(((ret) >> 10) & 0x1)

Use FIELD_GET() and specify masks that define the actual fields.

Sure, that makes sense.

Quoted 136 lines
Quoted 18 lines
arch/arm64/include/asm/rmi_smc.h new file +18 -0
+
+#define RMI_SUCCESS			0
+#define RMI_ERROR_INPUT			1
+#define RMI_ERROR_REALM			2
+#define RMI_ERROR_REC			3
+#define RMI_ERROR_RTT			4
+#define RMI_ERROR_NOT_SUPPORTED		5
+#define RMI_ERROR_DEVICE		6
+#define RMI_ERROR_RTT_AUX		7
+#define RMI_ERROR_PSMMU_ST		8
+#define RMI_ERROR_DPT			9
+#define RMI_BUSY			10
+#define RMI_ERROR_GLOBAL		11
+#define RMI_ERROR_TRACKING		12
+#define RMI_INCOMPLETE			13
+#define RMI_BLOCKED			14
+#define RMI_ERROR_GPT			15
+#define RMI_ERROR_GRANULE		16

arch/arm64/include/asm/rmi_smc.h line 167

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+
+#define RMI_OP_MEM_REQ_NONE		0
+#define RMI_OP_MEM_REQ_DONATE		1
+#define RMI_OP_MEM_REQ_RECLAIM		2
+
+#define RMI_DONATE_SIZE(req)		((req) & 0x3)
+#define RMI_DONATE_COUNT_MASK		GENMASK(15, 2)
+#define RMI_DONATE_COUNT(req)		(((req) & RMI_DONATE_COUNT_MASK) >> 2)
+#define RMI_DONATE_CONTIG(req)		(!!((req) & BIT(16)))
+#define RMI_DONATE_STATE(req)		(!!((req) & BIT(17)))

FIELD_GET().

Quoted 78 lines
arch/arm64/include/asm/rmi_smc.h new file +78 -0
+
+#define RMI_OP_MEM_DELEGATED		0
+#define RMI_OP_MEM_UNDELEGATED		1
+
+#define RMI_ADDR_TYPE_NONE		0
+#define RMI_ADDR_TYPE_SINGLE		1
+#define RMI_ADDR_TYPE_LIST		2
+
+#define RMI_ADDR_RANGE_SIZE_MASK	GENMASK(1, 0)
+#define RMI_ADDR_RANGE_COUNT_MASK	GENMASK(PAGE_SHIFT - 1, 2)
+#define RMI_ADDR_RANGE_ADDR_MASK	(PAGE_MASK & GENMASK(51, 0))
+#define RMI_ADDR_RANGE_STATE_MASK	BIT(63)
+
+#define RMI_ADDR_RANGE_SIZE(ar)		(FIELD_GET(RMI_ADDR_RANGE_SIZE_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_COUNT(ar)	(FIELD_GET(RMI_ADDR_RANGE_COUNT_MASK, \
+						   (ar)))
+#define RMI_ADDR_RANGE_ADDR(ar)		((ar) & RMI_ADDR_RANGE_ADDR_MASK)
+#define RMI_ADDR_RANGE_STATE(ar)	(FIELD_GET(RMI_ADDR_RANGE_STATE_MASK, \
+						   (ar)))
+
+enum rmi_ripas {
+	RMI_EMPTY = 0,
+	RMI_RAM = 1,
+	RMI_DESTROYED = 2,
+	RMI_DEV = 3,
+};
+
+#define RMI_NO_MEASURE_CONTENT	0
+#define RMI_MEASURE_CONTENT	1
+
+#define RMI_FEATURE_REGISTER_0_S2SZ		GENMASK(7, 0)
+#define RMI_FEATURE_REGISTER_0_LPA2		BIT(8)
+#define RMI_FEATURE_REGISTER_0_SVE		BIT(9)
+#define RMI_FEATURE_REGISTER_0_SVE_VL		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_0_NUM_BPS		GENMASK(19, 14)
+#define RMI_FEATURE_REGISTER_0_NUM_WPS		GENMASK(25, 20)
+#define RMI_FEATURE_REGISTER_0_PMU		BIT(26)
+#define RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS	GENMASK(31, 27)
+
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB	BIT(0)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB	BIT(1)
+#define RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB	BIT(2)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_256	BIT(3)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_384	BIT(4)
+#define RMI_FEATURE_REGISTER_1_HASH_SHA_512	BIT(5)
+#define RMI_FEATURE_REGISTER_1_MAX_RECS_ORDER	GENMASK(9, 6)
+#define RMI_FEATURE_REGISTER_1_L0GPTSZ		GENMASK(13, 10)
+#define RMI_FEATURE_REGISTER_1_PPS		GENMASK(16, 14)
+
+#define RMI_FEATURE_REGISTER_2_DA		BIT(0)
+#define RMI_FEATURE_REGISTER_2_DA_COH		BIT(1)
+#define RMI_FEATURE_REGISTER_2_VSMMU		BIT(2)
+#define RMI_FEATURE_REGISTER_2_ATS		BIT(3)
+#define RMI_FEATURE_REGISTER_2_MAX_VDEVS_ORDER	GENMASK(7, 4)
+#define RMI_FEATURE_REGISTER_2_VDEV_KROU	BIT(8)
+#define RMI_FEATURE_REGISTER_2_NON_TEE_STREAM	BIT(9)
+
+#define RMI_FEATURE_REGISTER_3_MAX_NUM_AUX_PLANES	GENMASK(3, 0)
+#define RMI_FEATURE_REGISTER_3_RTT_PLAN			GENMASK(5, 4)
+#define RMI_FEATURE_REGISTER_3_RTT_S2AP_INDIRECT	BIT(6)
+
+#define RMI_FEATURE_REGISTER_4_MEC_COUNT		GENMASK(63, 0)
+
+#define RMI_MEM_CATEGORY_CONVENTIONAL		0
+#define RMI_MEM_CATEGORY_DEV_NCOH		1
+#define RMI_MEM_CATEGORY_DEV_COH		2
+
+#define RMI_TRACKING_RESERVED			0
+#define RMI_TRACKING_NONE			1
+#define RMI_TRACKING_FINE			2
+#define RMI_TRACKING_COARSE			3
+
+#define RMI_GRANULE_SIZE_4KB	0
+#define RMI_GRANULE_SIZE_16KB	1
+#define RMI_GRANULE_SIZE_64KB	2
+
+/*

arch/arm64/include/asm/rmi_smc.h line 255

Quoted 10 lines
arch/arm64/include/asm/rmi_smc.h new file +10 -0
+ * Note many of these fields are smaller than u64 but all fields have u64
+ * alignment, so use u64 to ensure correct alignment.
+ */
+struct rmm_config {
+	union { /* 0x0 */
+		struct {
+			u64 tracking_region_size;
+			u64 rmi_granule_size;
+		};
+		u8 sizer[0x1000];

SZ_4K?

Quoted 14 lines
arch/arm64/include/asm/rmi_smc.h new file +14 -0
+	};
+};
+
+#define RMI_REALM_PARAM_FLAG_LPA2		BIT(0)
+#define RMI_REALM_PARAM_FLAG_SVE		BIT(1)
+#define RMI_REALM_PARAM_FLAG_PMU		BIT(2)
+
+struct realm_params {
+	union { /* 0x0 */
+		struct {
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h line 275

Quoted 6 lines
arch/arm64/include/asm/rmi_smc.h new file +6 -0
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+			u64 num_aux_planes;
+		};
+		u8 padding0[0x400];

SZ_1K? And similarly all over the shop?

I'm a bit less sure that makes the code more readable - these structures are a bit of a pain because they are somewhat sparse. I've left a comment where the beginning of each union is, and personally I find it easier to see 0x0 + 0x400 == 0x400 rather than trying to work out what SZ_1K is in hex. This is particularly the case in terms of:

Quoted 10 lines
struct rec_params {
	union { /* 0x0 */
		u64 flags;
		u8 padding0[0x100];
	};
	union { /* 0x100 */
		u64 mpidr;
		u8 padding1[0x100];
	};
	union { /* 0x200 */
Quoted 8 lines
		u64 pc;
		u8 padding2[0x100];
	};
	union { /* 0x300 */
		u64 gprs[REC_CREATE_NR_GPRS];
		u8 padding3[0xd00];
	};
};

Where 0xd00 doesn't even have a correspoding SZ_ define.

The RMM deals with this with macro magic:

Quoted 10 lines
struct rmi_rec_params {
        /* Flags */
        SET_MEMBER_RMI(unsigned long flags, 0, 0x100);  /* Offset 0 */
        /* MPIDR of the REC */
        SET_MEMBER_RMI(unsigned long mpidr, 0x100, 0x200);      /* 0x100 */
        /* Program counter */
        SET_MEMBER_RMI(unsigned long pc, 0x200, 0x300); /* 0x200 */
        /* General-purpose registers */
        SET_MEMBER_RMI(unsigned long gprs[REC_CREATE_NR_GPRS], 0x300, 0x1000); /* 0x300 */
};

where the offsets are just directly encoded in the macro - but it's not an especially robust macro and I'm not convinced it's more readable.

I'm happy to hear other suggestions on how to encode this neatly.

Quoted 3 lines

I haven't checked the details of the encodings (life is too short), but I wonder how much of this exists as an MRS and could be automatically generated?

Automatically generating this would be good - I'm not sure whether we have a (public) source available to generate from at the moment. I have tried to methodically work through the spec when updating this file, but as Gavin has already pointed out there was at least one mistake (in currently unused definitions) this time.

Thanks,
Steve

maintainer
#16 lore raw

Re: [PATCH v14 04/44] arm64: RMI: Add SMC definitions for calling the RMM

Quoted 67 lines · Steven Price
Quoted 56 lines · Marc Zyngier
Quoted 54 lines · Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

---
 arch/arm64/include/asm/rmi_smc.h | 448 +++++++++++++++++++++++++++++++
 1 file changed, 448 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +3 -0
new file mode 100644
index 000000000000..a09b7a631fef
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h line 7

Quoted 4 lines
arch/arm64/include/asm/rmi_smc.h new file +4 -0
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/

How long is this spec going to be available on the ARM web site, which has a tendency of being reorganised every other week? And there is already a beta2.

Quoted 2 lines

Obviously I can't predict the next reorganisation - but at least it's a link that could be fed into archive.org or similar.

I found that the PDF spec was less susceptible to creative nonsense, and people can download it for future reference, whereas ARM has happily *deleted* specs from the website over time (try to find PSCI 0.1, for example...).

[...]

Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
arch/arm64/include/asm/rmi_smc.h new file +7 -0
+struct realm_params {
+	union { /* 0x0 */
+		struct {
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h line 275

Quoted 6 lines
arch/arm64/include/asm/rmi_smc.h new file +6 -0
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+			u64 num_aux_planes;
+		};
+		u8 padding0[0x400];

SZ_1K? And similarly all over the shop?

I'm a bit less sure that makes the code more readable - these structures are a bit of a pain because they are somewhat sparse. I've left a comment where the beginning of each union is, and personally I find it easier to see 0x0 + 0x400 == 0x400 rather than trying to work out what SZ_1K is in hex. This is particularly the case in terms of:

Quoted 10 lines
struct rec_params {
	union { /* 0x0 */
		u64 flags;
		u8 padding0[0x100];
	};
	union { /* 0x100 */
		u64 mpidr;
		u8 padding1[0x100];
	};
	union { /* 0x200 */
Quoted 8 lines
		u64 pc;
		u8 padding2[0x100];
	};
	union { /* 0x300 */
		u64 gprs[REC_CREATE_NR_GPRS];
		u8 padding3[0xd00];
	};
};
Quoted 1 line

Where 0xd00 doesn't even have a correspoding SZ_ define.

Indeed, but it is (SZ_4K - SZ_256 * 3). And a lot of these structures seem to be designed to form a 4kB blob. I'm sure we can make use of that information (BUILD_BUG_ON?).

Quoted 14 lines

The RMM deals with this with macro magic:

Quoted 10 lines
struct rmi_rec_params {
        /* Flags */
        SET_MEMBER_RMI(unsigned long flags, 0, 0x100);  /* Offset 0 */
        /* MPIDR of the REC */
        SET_MEMBER_RMI(unsigned long mpidr, 0x100, 0x200);      /* 0x100 */
        /* Program counter */
        SET_MEMBER_RMI(unsigned long pc, 0x200, 0x300); /* 0x200 */
        /* General-purpose registers */
        SET_MEMBER_RMI(unsigned long gprs[REC_CREATE_NR_GPRS], 0x300, 0x1000); /* 0x300 */
};
Quoted 2 lines

where the offsets are just directly encoded in the macro - but it's not an especially robust macro and I'm not convinced it's more readable.

I think this is just as horrible, but at least it seems to take the boundaries of the structure into account.

Quoted 2 lines

I'm happy to hear other suggestions on how to encode this neatly.

Honestly, I wouldn't mind having the structures described in a more abstract way and then pre-processed to generate the include files. If the architectural MRS wasn't so huge, I would have added it to the kernel and used that directly for KVM.

Quoted 10 lines
Quoted 4 lines

I haven't checked the details of the encodings (life is too short), but I wonder how much of this exists as an MRS and could be automatically generated?

Automatically generating this would be good - I'm not sure whether we have a (public) source available to generate from at the moment. I have tried to methodically work through the spec when updating this file, but as Gavin has already pointed out there was at least one mistake (in currently unused definitions) this time.

I'm slightly baffled that even the RMM is written this way. Given the formalism used in the RMM spec, I was expecting that you'd have a bunch of JSON at hand and able to generate any output from that. Doing this stuff by hand is both incredibly dull work *and* extremely error prone.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#17 lore raw

Re: [PATCH v14 04/44] arm64: RMI: Add SMC definitions for calling the RMM

Quoted 69 lines · Marc Zyngier
Quoted 67 lines · Steven Price
Quoted 64 lines · Marc Zyngier
Quoted 51 lines · Steven Price

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

The SMC definitions are based on DEN0137[1] version 2.0-bet1

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

---
Changes since v13:
* Updated to RMM spec v2.0-bet1
Changes since v12:
* Updated to RMM spec v2.0-bet0
Changes since v9:
* Corrected size of 'ripas_value' in struct rec_exit. The spec states this is an 8-bit type with padding afterwards (rather than a u64).
Changes since v8:
* Added RMI_PERMITTED_GICV3_HCR_BITS to define which bits the RMM permits to be modified.
Changes since v6:
* Renamed REC_ENTER_xxx defines to include 'FLAG' to make it obvious these are flag values.
Changes since v5:
* Sorted the SMC #defines by value.
* Renamed SMI_RxI_CALL to SMI_RMI_CALL since the macro is only used for RMI calls.
* Renamed REC_GIC_NUM_LRS to REC_MAX_GIC_NUM_LRS since the actual number of available list registers could be lower.
* Provided a define for the reserved fields of FeatureRegister0.
* Fix inconsistent names for padding fields.
Changes since v4:
* Update to point to final released RMM spec.
* Minor rearrangements.
Changes since v3:
* Update to match RMM spec v1.0-rel0-rc1.
Changes since v2:
* Fix specification link.
* Rename rec_entry->rec_enter to match spec.
* Fix size of pmu_ovf_status to match spec.

---
 arch/arm64/include/asm/rmi_smc.h | 448 +++++++++++++++++++++++++++++++
 1 file changed, 448 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_smc.h
arch/arm64/include/asm/rmi_smc.h new file +0 -0
new file mode 100644
index 000000000000..a09b7a631fef

arch/arm64/include/asm/rmi_smc.h line 7

Quoted 7 lines
arch/arm64/include/asm/rmi_smc.h new file +7 -0
@@ -0,0 +1,448 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ *
+ * The values and structures in this file are from the Realm Management Monitor
+ * specification (DEN0137) version 2.0-bet1:
+ * https://developer.arm.com/documentation/den0137/2-0bet1/

How long is this spec going to be available on the ARM web site, which has a tendency of being reorganised every other week? And there is already a beta2.

Quoted 7 lines
Quoted 2 lines

Obviously I can't predict the next reorganisation - but at least it's a link that could be fed into archive.org or similar.

I found that the PDF spec was less susceptible to creative nonsense, and people can download it for future reference, whereas ARM has happily *deleted* specs from the website over time (try to find PSCI 0.1, for example...).

Sadly the nearest I found to a link directly to the PDF is:

https://documentation-service.arm.com/static/69cb945ac1586b7c59b1c00c

But I have 0 confidence that that link will work for long (if indeed it even works for others now!). If you know of any way of getting a better link out of the Arm website that I'm all ears!

Quoted 43 lines

[...]

Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
arch/arm64/include/asm/rmi_smc.h new file +7 -0
+struct realm_params {
+	union { /* 0x0 */
+		struct {
+			u64 flags;
+			u64 s2sz;
+			u64 sve_vl;
+			u64 num_bps;
Quoted 8 lines

arch/arm64/include/asm/rmi_smc.h line 275

Quoted 6 lines
arch/arm64/include/asm/rmi_smc.h new file +6 -0
+			u64 num_wps;
+			u64 pmu_num_ctrs;
+			u64 hash_algo;
+			u64 num_aux_planes;
+		};
+		u8 padding0[0x400];

SZ_1K? And similarly all over the shop?

I'm a bit less sure that makes the code more readable - these structures are a bit of a pain because they are somewhat sparse. I've left a comment where the beginning of each union is, and personally I find it easier to see 0x0 + 0x400 == 0x400 rather than trying to work out what SZ_1K is in hex. This is particularly the case in terms of:

Quoted 18 lines
struct rec_params {
	union { /* 0x0 */
		u64 flags;
		u8 padding0[0x100];
	};
	union { /* 0x100 */
		u64 mpidr;
		u8 padding1[0x100];
	};
	union { /* 0x200 */
		u64 pc;
		u8 padding2[0x100];
	};
	union { /* 0x300 */
		u64 gprs[REC_CREATE_NR_GPRS];
		u8 padding3[0xd00];
	};
};
Quoted 3 lines
Quoted 1 line

Where 0xd00 doesn't even have a correspoding SZ_ define.

Indeed, but it is (SZ_4K - SZ_256 * 3).

Do you really think

u8 padding3[SZ_4K - SZ_256 * 3];

is better? I certainly don't. I'll give you (SZ_4K - 0x300) is tempting. Although it then makes the BUILD_BUG_ON idea below somewhat pointless.

Quoted 1 line

And a lot of these structures> seem to be designed to form a 4kB blob.

I'm sure we can make use of

Quoted 1 line

that information (BUILD_BUG_ON?).

BUILD_BUG_ON requires being in a function. But static_assert() can be used in the header by the struct definitions - I'll add that, thanks for the suggestion.

Quoted 39 lines
Quoted 14 lines

The RMM deals with this with macro magic:

Quoted 10 lines
struct rmi_rec_params {
        /* Flags */
        SET_MEMBER_RMI(unsigned long flags, 0, 0x100);  /* Offset 0 */
        /* MPIDR of the REC */
        SET_MEMBER_RMI(unsigned long mpidr, 0x100, 0x200);      /* 0x100 */
        /* Program counter */
        SET_MEMBER_RMI(unsigned long pc, 0x200, 0x300); /* 0x200 */
        /* General-purpose registers */
        SET_MEMBER_RMI(unsigned long gprs[REC_CREATE_NR_GPRS], 0x300, 0x1000); /* 0x300 */
};
Quoted 2 lines

where the offsets are just directly encoded in the macro - but it's not an especially robust macro and I'm not convinced it's more readable.

I think this is just as horrible, but at least it seems to take the boundaries of the structure into account.

Quoted 2 lines

I'm happy to hear other suggestions on how to encode this neatly.

Honestly, I wouldn't mind having the structures described in a more abstract way and then pre-processed to generate the include files. If the architectural MRS wasn't so huge, I would have added it to the kernel and used that directly for KVM.

Quoted 10 lines
Quoted 4 lines

I haven't checked the details of the encodings (life is too short), but I wonder how much of this exists as an MRS and could be automatically generated?

Automatically generating this would be good - I'm not sure whether we have a (public) source available to generate from at the moment. I have tried to methodically work through the spec when updating this file, but as Gavin has already pointed out there was at least one mistake (in currently unused definitions) this time.

Quoted 5 lines

I'm slightly baffled that even the RMM is written this way. Given the formalism used in the RMM spec, I was expecting that you'd have a bunch of JSON at hand and able to generate any output from that. Doing this stuff by hand is both incredibly dull work *and* extremely error prone.

I'll look into the possibility of generating the headers. While dull and error prone I have found it is sometimes useful for forcing a review of the spec itself. There have been a number of bugs I've found (and have been corrected) in the spec while writing the header files - it's very easy to skim read those parts of the document otherwise.

Writing the structures out in a "more abstract way" might be a good idea, but I'm just a little wary of writing another tool which is only used in this one spot. The RMM structures are somewhat unusual in being so sparse.

Thanks,
Steve

Quoted 4 lines

Thanks,

	M.
#18 lore raw

PATCH v14 5/44 arm64: RMI: Add wrappers for RMI calls

In reply to Steven Price

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

---
Changes from v13:
* Update to RMM v2.0-bet1 spec including some SRO support (there still some FIXMEs where SRO support is incomplete).
Changes from v12:
* Update to RMM v2.0 specification
Changes from v8:
* Switch from arm_smccc_1_2_smc() to arm_smccc_1_2_invoke() in rmi_rtt_read_entry() for consistency.
Changes from v7:
* Minor renaming of parameters and updated comments
Changes from v5:
* Further improve comments
Changes from v4:
* Improve comments
Changes from v2:
* Make output arguments optional.
* Mask RIPAS value rmi_rtt_read_entry()
* Drop unused rmi_rtt_get_phys()

---
 arch/arm64/include/asm/rmi_cmds.h | 661 ++++++++++++++++++++++++++++++
 1 file changed, 661 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_cmds.h
arch/arm64/include/asm/rmi_cmds.h new file +661 -0
new file mode 100644
index 000000000000..04f7066894e9
@@ -0,0 +1,661 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#ifndef __ASM_RMI_CMDS_H
+#define __ASM_RMI_CMDS_H
+
+#include <linux/arm-smccc.h>
+
+#include <asm/rmi_smc.h>
+
+struct rtt_entry {
+	unsigned long walk_level;
+	unsigned long desc;
+	int state;
+	int ripas;
+};
+
+#define RMI_MAX_ADDR_LIST	256
+
+struct rmi_sro_state {
+	struct arm_smccc_1_2_regs regs;
+	unsigned long addr_count;
+	unsigned long addr_list[RMI_MAX_ADDR_LIST];
+};
+
+#define rmi_smccc(...) do {						\
+	arm_smccc_1_1_invoke(__VA_ARGS__);				\
+} while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
+	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
+
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
+void rmi_sro_free(struct rmi_sro_state *sro);
+
+/**
+ * rmi_rmm_config_set() - Configure the RMM
+ * @cfg_ptr: PA of a struct rmm_config
+ *
+ * Sets configuration options on the RMM.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rmm_config_set(unsigned long cfg_ptr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RMM_CONFIG_SET, cfg_ptr, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rmm_activate() - Activate the RMM
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rmm_activate(void)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RMM_ACTIVATE, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_granule_tracking_get() - Get configuration of a Granule tracking region
+ * @start: Base PA of the tracking region
+ * @end: End of the PA region
+ * @out_category: Memory category
+ * @out_state: Tracking region state
+ * @out_top: Top of the memory region
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_granule_tracking_get(unsigned long start,
+					   unsigned long end,
+					   unsigned long *out_category,
+					   unsigned long *out_state,
+					   unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GRANULE_TRACKING_GET, start, end, &res);
+
+	if (out_category)
+		*out_category = res.a1;
+	if (out_state)
+		*out_state = res.a2;
+	if (out_top)
+		*out_top = res.a3;
+
+	return res.a0;
+}
+
+/**
+ * rmi_gpt_l1_create() - Create a Level 1 GPT
+ * @addr: Base of physical address region described by the L1GPT
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_gpt_l1_create(unsigned long addr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GPT_L1_CREATE, addr, &res);
+
+	if (RMI_RETURN_STATUS(res.a0) == RMI_INCOMPLETE) {
+		/* FIXME */
+		return WARN_ON(res.a0);
+	}
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_data_map_init() - Create a protected mapping with data contents
+ * @rd: PA of the RD
+ * @data: PA of the target granule
+ * @ipa: IPA at which the granule will be mapped in the guest
+ * @src: PA of the source granule
+ * @flags: RMI_MEASURE_CONTENT if the contents should be measured
+ *
+ * Create a mapping from Protected IPA space to conventional memory, copying
+ * contents from a Non-secure Granule provided by the caller.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_data_map_init(unsigned long rd, unsigned long data,
+					unsigned long ipa, unsigned long src,
+					unsigned long flags)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_DATA_MAP_INIT, rd, data, ipa, src,
+			     flags, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_data_map() - Create mappings in protected IPA with unknown contents
+ * @rd: PA of the RD
+ * @base: Base of the target IPA range
+ * @top: Top of the target IPA range
+ * @flags: Flags
+ * @oaddr: Output address set descriptor
+ * @out_top: Top address of range which was processed.
+ *
+ * Return RMI return code
+ */
+static inline int rmi_rtt_data_map(unsigned long rd,
+				   unsigned long base,
+				   unsigned long top,
+				   unsigned long flags,
+				   unsigned long oaddr,
+				   unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_DATA_MAP, rd, base, top, flags, oaddr,
+			     &res);
+
+	if (RMI_RETURN_STATUS(res.a0) == RMI_INCOMPLETE) {
+		/* FIXME */
+		return WARN_ON(res.a0);
+	}
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_data_unmap() - Remove mappings to conventional memory
+ * @rd: PA of the RD for the target Realm
+ * @base: Base of the target IPA range
+ * @top: Top of the target IPA range
+ * @flags: Flags
+ * @oaddr: Output address set descriptor
+ * @out_top: Returns top IPA of range which has been unmapped
+ * @out_range: Output address range
+ * @out_count: Number of entries in output address list
+ *
+ * Removes mappings to convention memory with a target Protected IPA range.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_data_unmap(unsigned long rd,
+				     unsigned long base,
+				     unsigned long top,
+				     unsigned long flags,
+				     unsigned long oaddr,
+				     unsigned long *out_top,
+				     unsigned long *out_range,
+				     unsigned long *out_count)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_DATA_UNMAP, rd, base, top, flags,
+			     oaddr, &res);
+
+	/* FIXME: Handle SRO */
+
+	if (out_top)
+		*out_top = res.a1;
+	if (out_range)
+		*out_range = res.a2;
+	if (out_count)
+		*out_count = res.a3;
+
+	return res.a0;
+}
+
+/**
+ * rmi_features() - Read feature register
+ * @index: Feature register index
+ * @out: Feature register value is written to this pointer
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_features(unsigned long index, unsigned long *out)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_FEATURES, index, &res);
+
+	if (out)
+		*out = res.a1;
+	return res.a0;
+}
+
+/**
+ * rmi_granule_range_delegate() - Delegate granules
+ * @base: PA of the first granule of the range
+ * @top: PA of the first granule after the range
+ * @out_top: PA of the first granule not delegated
+ *
+ * Delegate a range of granule for use by the realm world. If the entire range
+ * was delegated then @out_top == @top, otherwise the function should be called
+ * again with @base == @out_top.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_granule_range_delegate(unsigned long base,
+					     unsigned long top,
+					     unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GRANULE_RANGE_DELEGATE, base, top, &res);
+
+	if (RMI_RETURN_STATUS(res.a0) == RMI_INCOMPLETE) {
+		/* FIXME - Handle SRO */
+		return WARN_ON(res.a0);
+	}
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_granule_range_undelegate() - Undelegate a range of granules
+ * @base: Base PA of the target range
+ * @top: Top PA of the target range
+ * @out_top: Returns the top PA of range whose state is undelegated
+ *
+ * Undelegate a range of granules to allow use by the normal world. Will fail if
+ * the granules are in use.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_granule_range_undelegate(unsigned long base,
+					       unsigned long top,
+					       unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GRANULE_RANGE_UNDELEGATE, base, top, &res);
+
+	if (RMI_RETURN_STATUS(res.a0) == RMI_INCOMPLETE) {
+		/* FIXME - Handle SRO */
+		return WARN_ON(res.a0);
+	}
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_psci_complete() - Complete pending PSCI command
+ * @calling_rec: PA of the calling REC
+ * @target_rec: PA of the target REC
+ * @status: Status of the PSCI request
+ *
+ * Completes a pending PSCI command which was called with an MPIDR argument, by
+ * providing the corresponding REC.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_psci_complete(unsigned long calling_rec,
+				    unsigned long target_rec,
+				    unsigned long status)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_PSCI_COMPLETE, calling_rec, target_rec,
+			     status, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_realm_activate() - Active a realm
+ * @rd: PA of the RD
+ *
+ * Mark a realm as Active signalling that creation is complete and allowing
+ * execution of the realm.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_realm_activate(unsigned long rd)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REALM_ACTIVATE, rd, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_realm_create() - Create a realm
+ * @rd: PA of the RD
+ * @params: PA of realm parameters
+ *
+ * Create a new realm using the given parameters.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_realm_create(unsigned long rd, unsigned long params)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REALM_CREATE, rd, params, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_realm_terminate() - Terminate a realm
+ * @rd: PA of the RD
+ *
+ * Terminates a realm, moving it into a ZOMBIE state
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_realm_terminate(unsigned long rd)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REALM_TERMINATE, rd, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_realm_destroy() - Destroy a realm
+ * @rd: PA of the RD
+ *
+ * Destroys a realm, all objects belonging to the realm must be destroyed first.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_realm_destroy(unsigned long rd)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REALM_DESTROY, rd, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rec_create() - Create a REC
+ * @rd: PA of the RD
+ * @rec: PA of the target REC
+ * @params: PA of REC parameters
+ * @sro: Allocated SRO context to be used
+ *
+ * Create a REC using the parameters specified in the struct rec_params pointed
+ * to by @params.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rec_create(unsigned long rd,
+				 unsigned long rec,
+				 unsigned long params,
+				 struct rmi_sro_state *sro)
+{
+	int ret;
+
+	*sro = (struct rmi_sro_state){.regs = {
+		SMC_RMI_REC_CREATE, rd, rec, params
+	}};
+	ret = rmi_sro_execute(sro, GFP_KERNEL);
+	rmi_sro_free(sro);
+
+	return ret;
+}
+
+/**
+ * rmi_rec_destroy() - Destroy a REC
+ * @rec: PA of the target REC
+ * @sro: Allocated SRO context to be used
+ *
+ * Destroys a REC. The REC must not be running.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rec_destroy(unsigned long rec,
+				  struct rmi_sro_state *sro)
+{
+	int ret;
+
+	*sro = (struct rmi_sro_state){.regs = {
+		SMC_RMI_REC_DESTROY, rec
+	}};
+	ret = rmi_sro_execute(sro, GFP_KERNEL);
+	rmi_sro_free(sro);
+
+	return ret;
+}
+
+/**
+ * rmi_rec_enter() - Enter a REC
+ * @rec: PA of the target REC
+ * @run_ptr: PA of RecRun structure
+ *
+ * Starts (or continues) execution within a REC.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rec_enter(unsigned long rec, unsigned long run_ptr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_REC_ENTER, rec, run_ptr, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_create() - Creates an RTT
+ * @rd: PA of the RD
+ * @rtt: PA of the target RTT
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ *
+ * Creates an RTT (Realm Translation Table) at the specified level for the
+ * translation of the specified address within the realm.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_create(unsigned long rd, unsigned long rtt,
+				 unsigned long ipa, long level)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_CREATE, rd, rtt, ipa, level, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_destroy() - Destroy an RTT
+ * @rd: PA of the RD
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ * @out_rtt: Pointer to write the PA of the RTT which was destroyed
+ * @out_top: Pointer to write the top IPA of non-live RTT entries
+ *
+ * Destroys an RTT. The RTT must be non-live, i.e. none of the entries in the
+ * table are in ASSIGNED or TABLE state.
+ *
+ * Return: RMI return code.
+ */
+static inline int rmi_rtt_destroy(unsigned long rd,
+				  unsigned long ipa,
+				  long level,
+				  unsigned long *out_rtt,
+				  unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_DESTROY, rd, ipa, level, &res);
+
+	if (out_rtt)
+		*out_rtt = res.a1;
+	if (out_top)
+		*out_top = res.a2;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_fold() - Fold an RTT
+ * @rd: PA of the RD
+ * @ipa: Base of the IPA range described by the RTT
+ * @level: Depth of the RTT within the tree
+ * @out_rtt: Pointer to write the PA of the RTT which was destroyed
+ *
+ * Folds an RTT. If all entries with the RTT are 'homogeneous' the RTT can be
+ * folded into the parent and the RTT destroyed.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_fold(unsigned long rd, unsigned long ipa,
+			       long level, unsigned long *out_rtt)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_FOLD, rd, ipa, level, &res);
+
+	if (out_rtt)
+		*out_rtt = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_init_ripas() - Set RIPAS for new realm
+ * @rd: PA of the RD
+ * @base: Base of target IPA region
+ * @top: Top of target IPA region
+ * @out_top: Top IPA of range whose RIPAS was modified
+ *
+ * Sets the RIPAS of a target IPA range to RAM, for a realm in the NEW state.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_init_ripas(unsigned long rd, unsigned long base,
+				     unsigned long top, unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_INIT_RIPAS, rd, base, top, &res);
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_unprot_map() - Map unprotected granules into a realm
+ * @rd: PA of the RD
+ * @base: Base IPA of the mapping
+ * @top: Top of the target IPA range
+ * @flags: Flags
+ * @oaddr: Output address set descriptor
+ * @out_top: Top IPA of range which has been mapped
+ *
+ * Create mappings to memory within a target unprotected IPA range.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_unprot_map(unsigned long rd,
+				     unsigned long base,
+				     unsigned long top,
+				     unsigned long flags,
+				     unsigned long oaddr,
+				     unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_UNPROT_MAP, rd, base, top, flags,
+			     oaddr, &res);
+
+	/* FIXME: Handle SRO */
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_set_ripas() - Set RIPAS for an running realm
+ * @rd: PA of the RD
+ * @rec: PA of the REC making the request
+ * @base: Base of target IPA region
+ * @top: Top of target IPA region
+ * @out_top: Pointer to write top IPA of range whose RIPAS was modified
+ *
+ * Completes a request made by the realm to change the RIPAS of a target IPA
+ * range.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_set_ripas(unsigned long rd, unsigned long rec,
+				    unsigned long base, unsigned long top,
+				    unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_SET_RIPAS, rd, rec, base, top, &res);
+
+	if (out_top)
+		*out_top = res.a1;
+
+	return res.a0;
+}
+
+/**
+ * rmi_rtt_unprot_unmap() - Remove mappings within an unprotected IPA range
+ * @rd: PA of the RD
+ * @base: Base IPA of the mapping
+ * @top: Top of the target IPA range
+ * @flags: Flags
+ * @oaddr: Output address set descriptor
+ * @out_top: Top IPA which has been unmapped
+ * @out_range: Output address range
+ * @out_count: Number of entries in output address list
+ *
+ * Removes mappings to memory within a target unprotected IPA range.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rtt_unprot_unmap(unsigned long rd,
+				       unsigned long base,
+				       unsigned long top,
+				       unsigned long flags,
+				       unsigned long oaddr,
+				       unsigned long *out_top,
+				       unsigned long *out_range,
+				       unsigned long *out_count)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_UNPROT_UNMAP, rd, base, top,
+			     flags, oaddr, &res);
+
+	/* FIXME: Handle SRO */
+
+	if (out_top)
+		*out_top = res.a1;
+	if (out_range)
+		*out_range = res.a2;
+	if (out_count)
+		*out_count = res.a3;
+
+	return res.a0;
+}
+
+#endif /* __ASM_RMI_CMDS_H */
Signature · 1 line
2.43.0
#19 lore raw

Re: [PATCH v14 05/44] arm64: RMI: Add wrappers for RMI calls

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

Quoted 9 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.

arch/arm64/include/asm/rmi_cmds.h new file +5 -0
+#define rmi_smccc(...) do {						\
+	arm_smccc_1_1_invoke(__VA_ARGS__);				\
+} while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
+	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
+

I guess this is not used. Also, that would require the call site to have a struct arm_smccc_res res.

-aneesh

#20 lore raw

Re: [PATCH v14 05/44] arm64: RMI: Add wrappers for RMI calls

Quoted 5 lines · Aneesh Kumar K.V
Quoted 3 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.

Quoted 8 lines

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

Quoted 6 lines
arch/arm64/include/asm/rmi_cmds.h new file +5 -0
+#define rmi_smccc(...) do {						\
+	arm_smccc_1_1_invoke(__VA_ARGS__);				\
+} while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
+	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
+

I guess this is not used. Also, that would require the call site to have a struct arm_smccc_res res.

Ah good spot - yes this was replaced with a proper static inline rmi_smccc_invoke() function. I missed removing this macro.

Thanks,
Steve

#21 lore raw

Re: [PATCH v14 05/44] arm64: RMI: Add wrappers for RMI calls

In reply to Steven Price

Hi Steven,

Quoted 44 lines · Steven Price

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

---
Changes from v13:
* Update to RMM v2.0-bet1 spec including some SRO support (there still some FIXMEs where SRO support is incomplete).
Changes from v12:
* Update to RMM v2.0 specification
Changes from v8:
* Switch from arm_smccc_1_2_smc() to arm_smccc_1_2_invoke() in rmi_rtt_read_entry() for consistency.
Changes from v7:
* Minor renaming of parameters and updated comments
Changes from v5:
* Further improve comments
Changes from v4:
* Improve comments
Changes from v2:
* Make output arguments optional.
* Mask RIPAS value rmi_rtt_read_entry()
* Drop unused rmi_rtt_get_phys()

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

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

arch/arm64/include/asm/rmi_cmds.h new file +10 -0
new file mode 100644
index 000000000000..04f7066894e9
@@ -0,0 +1,661 @@
+/* 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 487

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

In most cases, the parameters are well explained in RMM-v2.0-bet1 spec, I think it's nice to keep the code and the spec synchronized. For those specific parameters of this function, they're well explained in RMM-v2.0-bet1 spec as below.

@rd: PA of the RD for the target realm
@ipa: Base of the IPA range described by the RTT
@level: RTT level
@out_rtt: PA of the RTT which was destroyed
@out_top: Top IPA of non-live RTT entries, from entry at which the RTT walk terminated

Quoted 13 lines

+ * Destroys an RTT. The RTT must be non-live, i.e. none of the entries in the
+ * table are in ASSIGNED or TABLE state.
+ *
+ * Return: RMI return code.
+ */

arch/arm64/include/asm/rmi_cmds.h new file +8 -0
+static inline int rmi_rtt_destroy(unsigned long rd,
+				  unsigned long ipa,
+				  long level,
+				  unsigned long *out_rtt,
+				  unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+

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

Quoted 10 lines
arch/arm64/include/asm/rmi_cmds.h new file +10 -0
+	arm_smccc_1_1_invoke(SMC_RMI_RTT_DESTROY, rd, ipa, level, &res);
+
+	if (out_rtt)
+		*out_rtt = res.a1;
+	if (out_top)
+		*out_top = res.a2;
+
+	return res.a0;
+}
+

[...]

Thanks,
Gavin

#22 lore raw

Re: [PATCH v14 05/44] arm64: RMI: Add wrappers for RMI calls

Quoted 67 lines · Gavin Shan

Hi Steven,

Quoted 45 lines · Steven Price

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

---
Changes from v13:
  * Update to RMM v2.0-bet1 spec including some SRO support (there still some FIXMEs where SRO support is incomplete).
Changes from v12:
  * Update to RMM v2.0 specification
Changes from v8:
  * Switch from arm_smccc_1_2_smc() to arm_smccc_1_2_invoke() in rmi_rtt_read_entry() for consistency.
Changes from v7:
  * Minor renaming of parameters and updated comments Changes from v5:
  * Further improve comments
Changes from v4:
  * Improve comments
Changes from v2:
  * Make output arguments optional.
  * Mask RIPAS value rmi_rtt_read_entry()
  * Drop unused rmi_rtt_get_phys()
---
  arch/arm64/include/asm/rmi_cmds.h | 661 ++++++++++++++++++++++++++++++

  1 file changed, 661 insertions(+)

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

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

asm/rmi_cmds.h
new file mode 100644
index 000000000000..04f7066894e9

arch/arm64/include/asm/rmi_cmds.h new file +10 -0
@@ -0,0 +1,661 @@
+/* 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 487

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

In most cases, the parameters are well explained in RMM-v2.0-bet1 spec, I think
it's nice to keep the code and the spec synchronized. For those specific parameters
of this function, they're well explained in RMM-v2.0-bet1 spec as below.

Quoted 6 lines

   @rd: PA of the RD for the target realm
   @ipa: Base of the IPA range described by the RTT @level: RTT level
   @out_rtt: PA of the RTT which was destroyed
   @out_top: Top IPA of non-live RTT entries, from entry at which the RTT walk terminated

I have attempted to keep the descriptions consistent with the spec - I'm not quite sure what you think the issue is here. The @rd parameter gains a "for the target realm" - which isn't really very informative (clearly rmi_rtt_destroy() is targetting the realm which is being passed into the function). @level is less informative. @out_xxx are prefixed with "Pointer to write the" because the C function does indeed take a pointer for the output parameter to be written.

But fair enough I can align them more precisely. In some cases I've written the code before the final spec wording has been available which might explain some differences.

Thanks,
Steve

Quoted 30 lines
Quoted 14 lines

+ * Destroys an RTT. The RTT must be non-live, i.e. none of the entries in the
+ * table are in ASSIGNED or TABLE state.
+ *
+ * Return: RMI return code.
+ */

arch/arm64/include/asm/rmi_cmds.h new file +8 -0
+static inline int rmi_rtt_destroy(unsigned long rd,
+                  unsigned long ipa,
+                  long level,
+                  unsigned long *out_rtt,
+                  unsigned long *out_top)
+{
+    struct arm_smccc_res res;
+

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

Quoted 10 lines
arch/arm64/include/asm/rmi_cmds.h new file +10 -0
+    arm_smccc_1_1_invoke(SMC_RMI_RTT_DESTROY, rd, ipa, level, &res);
+
+    if (out_rtt)
+        *out_rtt = res.a1;
+    if (out_top)
+        *out_top = res.a2;
+
+    return res.a0;
+}
+

[...]

Thanks,
Gavin

maintainer
#23 lore raw

Re: [PATCH v14 05/44] arm64: RMI: Add wrappers for RMI calls

In reply to Steven Price
Quoted 135 lines · Steven Price

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

---
Changes from v13:
* Update to RMM v2.0-bet1 spec including some SRO support (there still some FIXMEs where SRO support is incomplete).
Changes from v12:
* Update to RMM v2.0 specification
Changes from v8:
* Switch from arm_smccc_1_2_smc() to arm_smccc_1_2_invoke() in rmi_rtt_read_entry() for consistency.
Changes from v7:
* Minor renaming of parameters and updated comments
Changes from v5:
* Further improve comments
Changes from v4:
* Improve comments
Changes from v2:
* Make output arguments optional.
* Mask RIPAS value rmi_rtt_read_entry()
* Drop unused rmi_rtt_get_phys()

---
 arch/arm64/include/asm/rmi_cmds.h | 661 ++++++++++++++++++++++++++++++
 1 file changed, 661 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_cmds.h
arch/arm64/include/asm/rmi_cmds.h new file +100 -0
new file mode 100644
index 000000000000..04f7066894e9
@@ -0,0 +1,661 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#ifndef __ASM_RMI_CMDS_H
+#define __ASM_RMI_CMDS_H
+
+#include <linux/arm-smccc.h>
+
+#include <asm/rmi_smc.h>
+
+struct rtt_entry {
+	unsigned long walk_level;
+	unsigned long desc;
+	int state;
+	int ripas;
+};
+
+#define RMI_MAX_ADDR_LIST	256
+
+struct rmi_sro_state {
+	struct arm_smccc_1_2_regs regs;
+	unsigned long addr_count;
+	unsigned long addr_list[RMI_MAX_ADDR_LIST];
+};
+
+#define rmi_smccc(...) do {						\
+	arm_smccc_1_1_invoke(__VA_ARGS__);				\
+} while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
+	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
+
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
+void rmi_sro_free(struct rmi_sro_state *sro);
+
+/**
+ * rmi_rmm_config_set() - Configure the RMM
+ * @cfg_ptr: PA of a struct rmm_config
+ *
+ * Sets configuration options on the RMM.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rmm_config_set(unsigned long cfg_ptr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RMM_CONFIG_SET, cfg_ptr, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rmm_activate() - Activate the RMM
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rmm_activate(void)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RMM_ACTIVATE, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_granule_tracking_get() - Get configuration of a Granule tracking region
+ * @start: Base PA of the tracking region
+ * @end: End of the PA region
+ * @out_category: Memory category
+ * @out_state: Tracking region state
+ * @out_top: Top of the memory region
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_granule_tracking_get(unsigned long start,
+					   unsigned long end,
+					   unsigned long *out_category,
+					   unsigned long *out_state,
+					   unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GRANULE_TRACKING_GET, start, end, &res);
+
+	if (out_category)
+		*out_category = res.a1;
+	if (out_state)
+		*out_state = res.a2;
+	if (out_top)
+		*out_top = res.a3;
+
+	return res.a0;
+}
+
+/**
+ * rmi_gpt_l1_create() - Create a Level 1 GPT
+ * @addr: Base of physical address region described by the L1GPT
+ *

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

Quoted 10 lines
arch/arm64/include/asm/rmi_cmds.h new file +10 -0
+ * Return: RMI return code
+ */
+static inline int rmi_gpt_l1_create(unsigned long addr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GPT_L1_CREATE, addr, &res);
+
+	if (RMI_RETURN_STATUS(res.a0) == RMI_INCOMPLETE) {
+		/* FIXME */

Is that part of the SRO stuff you're talking about in the notes? What is the ETA for fixing all these FIXMEs?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#24 lore raw

Re: [PATCH v14 05/44] arm64: RMI: Add wrappers for RMI calls

Quoted 142 lines · Marc Zyngier
Quoted 140 lines · Steven Price

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

---
Changes from v13:
* Update to RMM v2.0-bet1 spec including some SRO support (there still some FIXMEs where SRO support is incomplete).
Changes from v12:
* Update to RMM v2.0 specification
Changes from v8:
* Switch from arm_smccc_1_2_smc() to arm_smccc_1_2_invoke() in rmi_rtt_read_entry() for consistency.
Changes from v7:
* Minor renaming of parameters and updated comments
Changes from v5:
* Further improve comments
Changes from v4:
* Improve comments
Changes from v2:
* Make output arguments optional.
* Mask RIPAS value rmi_rtt_read_entry()
* Drop unused rmi_rtt_get_phys()

---
 arch/arm64/include/asm/rmi_cmds.h | 661 ++++++++++++++++++++++++++++++
 1 file changed, 661 insertions(+)
 create mode 100644 arch/arm64/include/asm/rmi_cmds.h
arch/arm64/include/asm/rmi_cmds.h new file +105 -0
new file mode 100644
index 000000000000..04f7066894e9
@@ -0,0 +1,661 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 ARM Ltd.
+ */
+
+#ifndef __ASM_RMI_CMDS_H
+#define __ASM_RMI_CMDS_H
+
+#include <linux/arm-smccc.h>
+
+#include <asm/rmi_smc.h>
+
+struct rtt_entry {
+	unsigned long walk_level;
+	unsigned long desc;
+	int state;
+	int ripas;
+};
+
+#define RMI_MAX_ADDR_LIST	256
+
+struct rmi_sro_state {
+	struct arm_smccc_1_2_regs regs;
+	unsigned long addr_count;
+	unsigned long addr_list[RMI_MAX_ADDR_LIST];
+};
+
+#define rmi_smccc(...) do {						\
+	arm_smccc_1_1_invoke(__VA_ARGS__);				\
+} while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
+	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
+
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
+void rmi_sro_free(struct rmi_sro_state *sro);
+
+/**
+ * rmi_rmm_config_set() - Configure the RMM
+ * @cfg_ptr: PA of a struct rmm_config
+ *
+ * Sets configuration options on the RMM.
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rmm_config_set(unsigned long cfg_ptr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RMM_CONFIG_SET, cfg_ptr, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_rmm_activate() - Activate the RMM
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_rmm_activate(void)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_RMM_ACTIVATE, &res);
+
+	return res.a0;
+}
+
+/**
+ * rmi_granule_tracking_get() - Get configuration of a Granule tracking region
+ * @start: Base PA of the tracking region
+ * @end: End of the PA region
+ * @out_category: Memory category
+ * @out_state: Tracking region state
+ * @out_top: Top of the memory region
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_granule_tracking_get(unsigned long start,
+					   unsigned long end,
+					   unsigned long *out_category,
+					   unsigned long *out_state,
+					   unsigned long *out_top)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_invoke(SMC_RMI_GRANULE_TRACKING_GET, start, end, &res);
+
+	if (out_category)
+		*out_category = res.a1;
+	if (out_state)
+		*out_state = res.a2;
+	if (out_top)
+		*out_top = res.a3;
+
+	return res.a0;
+}
+
+/**
+ * rmi_gpt_l1_create() - Create a Level 1 GPT
+ * @addr: Base of physical address region described by the L1GPT
+ *
+ * Return: RMI return code
+ */
+static inline int rmi_gpt_l1_create(unsigned long addr)
+{
+	struct arm_smccc_res res;
Quoted 8 lines

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

Quoted 5 lines
arch/arm64/include/asm/rmi_cmds.h new file +5 -0
+
+	arm_smccc_1_1_invoke(SMC_RMI_GPT_L1_CREATE, addr, &res);
+
+	if (RMI_RETURN_STATUS(res.a0) == RMI_INCOMPLETE) {
+		/* FIXME */

Is that part of the SRO stuff you're talking about in the notes? What is the ETA for fixing all these FIXMEs?

Yes, RMI_INCOMPLETE is the return for SRO. Fixing all this up is on the plan for my next posting which I expect to be after 7.2-rc1 (so July). There were some changes in the beta 2 spec and the RMM doesn't implement most of this yet so I didn't want to rush out completely untested code which might change.

Thanks,
Steve

Quoted 4 lines

Thanks,

	M.
#25 lore raw

PATCH v14 6/44 arm64: RMI: Check for RMI support at init

In reply to Steven Price

Query the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.

---
v14:
* This moves the basic RMI setup into the 'kernel' directory. This is because RMI will be used for some features outside of KVM so should be available even if KVM isn't compiled in.

---
 arch/arm64/include/asm/rmi_cmds.h |  3 ++
 arch/arm64/kernel/Makefile        |  2 +-
 arch/arm64/kernel/cpufeature.c    |  1 +
 arch/arm64/kernel/rmi.c           | 65 +++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/kernel/rmi.c
arch/arm64/include/asm/rmi_cmds.h +3 -0
index 04f7066894e9..9179934925c5 100644
@@ -10,6 +10,9 @@
 
 #include <asm/rmi_smc.h>
 
+extern unsigned long rmm_feat_reg0;
+extern unsigned long rmm_feat_reg1;
+
 struct rtt_entry {
 	unsigned long walk_level;
 	unsigned long desc;
arch/arm64/kernel/Makefile +1 -1
index 74b76bb70452..d68f351aae75 100644
@@ -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 idle.o patching.o pi/	\
-			   rsi.o jump_label.o
+			   rsi.o jump_label.o rmi.o
 
 obj-$(CONFIG_COMPAT)			+= sys32.o signal32.o			\
 					   sys_compat.o
arch/arm64/kernel/cpufeature.c +1 -0
index 6d53bb15cf7b..8bdd95a8c2de 100644
@@ -292,6 +292,7 @@ static const struct arm64_ftr_bits ftr_id_aa64isar3[] = {
 static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV3_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV2_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_RME_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_DIT_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AMU_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_MPAM_SHIFT, 4, 0),
arch/arm64/kernel/rmi.c new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.43.0
#26 lore raw

Re: [PATCH v14 06/44] arm64: RMI: Check for RMI support at init

Hi Steven,

Quoted 18 lines · Steven Price

Query the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.

---
v14:
* This moves the basic RMI setup into the 'kernel' directory. This is because RMI will be used for some features outside of KVM so should be available even if KVM isn't compiled in.

---
  arch/arm64/include/asm/rmi_cmds.h |  3 ++
  arch/arm64/kernel/Makefile        |  2 +-
  arch/arm64/kernel/cpufeature.c    |  1 +
  arch/arm64/kernel/rmi.c           | 65 +++++++++++++++++++++++++++++++
  4 files changed, 70 insertions(+), 1 deletion(-)

create mode 100644 arch/arm64/kernel/rmi.c

[...]

Quoted 52 lines
arch/arm64/kernel/rmi.c new file +46 -0
new file mode 100644
index 000000000000..99c1ccc35c11
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#include <linux/memblock.h>
+
+#include <asm/rmi_cmds.h>
+
+unsigned long rmm_feat_reg0;
+unsigned long rmm_feat_reg1;
+
+static int rmi_check_version(void)
+{
+	struct arm_smccc_res res;
+	unsigned short version_major, version_minor;
+	unsigned long host_version = RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION,
+						     RMI_ABI_MINOR_VERSION);
+	unsigned long aa64pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+
+	/* If RME isn't supported, then RMI can't be */
+	if (cpuid_feature_extract_unsigned_field(aa64pfr0, ID_AA64PFR0_EL1_RME_SHIFT) == 0)
+		return -ENXIO;
+
+	arm_smccc_1_1_invoke(SMC_RMI_VERSION, host_version, &res);
+
+	if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
+		return -ENXIO;
+
+	version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1);
+	version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1);
+
+	if (res.a0 != RMI_SUCCESS) {
+		unsigned short high_version_major, high_version_minor;
+
+		high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2);
+		high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2);
+
+		pr_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n",
+		       version_major, version_minor,
+		       high_version_major, high_version_minor,
+		       RMI_ABI_MAJOR_VERSION,
+		       RMI_ABI_MINOR_VERSION);
+		return -ENXIO;
+	}
+

arch/arm64/kernel/rmi.c line 56

Quoted 10 lines
arch/arm64/kernel/rmi.c new file +10 -0
+	pr_info("RMI ABI version %d.%d\n", version_major, version_minor);
+
+	return 0;
+}
+
+static int __init arm64_init_rmi(void)
+{
+	/* Continue without realm support if we can't agree on a version */
+	if (rmi_check_version())
+		return 0;

Is this still a valid point that we have to return zero on errors returned from rmi_check_version() or other other function calls like rmi_features()? arm64_init_rmi() is triggered by subsys_initcall() where the return value needs to indicate success or failure. It's fine to return error code from arm64_init_rmi() in the path.

arch/arm64/kernel/rmi.c line 65

Quoted 9 lines
arch/arm64/kernel/rmi.c new file +9 -0
+
+	if (WARN_ON(rmi_features(0, &rmm_feat_reg0)))
+		return 0;
+	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
+		return 0;
+
+	return 0;
+}
+subsys_initcall(arm64_init_rmi);

Thanks,
Gavin

#27 lore raw

Re: [PATCH v14 06/44] arm64: RMI: Check for RMI support at init

Quoted 86 lines · Gavin Shan

Hi Steven,

Quoted 18 lines · Steven Price

Query the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.

---
v14:
  * This moves the basic RMI setup into the 'kernel' directory. This is because RMI will be used for some features outside of KVM so should be available even if KVM isn't compiled in.
---
  arch/arm64/include/asm/rmi_cmds.h |  3 ++ arch/arm64/kernel/Makefile        |  2 +- arch/arm64/kernel/cpufeature.c    |  1 + arch/arm64/kernel/rmi.c           | 65 +++++++++++++++++++++++++++++++

  4 files changed, 70 insertions(+), 1 deletion(-)

  create mode 100644 arch/arm64/kernel/rmi.c

[...]

Quoted 62 lines
arch/arm64/kernel/rmi.c new file +19 -0
new file mode 100644
index 000000000000..99c1ccc35c11
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#include <linux/memblock.h>
+
+#include <asm/rmi_cmds.h>
+
+unsigned long rmm_feat_reg0;
+unsigned long rmm_feat_reg1;
+
+static int rmi_check_version(void)
+{
+    struct arm_smccc_res res;
+    unsigned short version_major, version_minor;
+    unsigned long host_version = RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION,
+                             RMI_ABI_MINOR_VERSION);
+    unsigned long aa64pfr0 =

read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+
+    /* If RME isn't supported, then RMI can't be */ +    if (cpuid_feature_extract_unsigned_field(aa64pfr0, ID_AA64PFR0_EL1_RME_SHIFT) == 0)
+        return -ENXIO;

arch/arm64/kernel/rmi.c new file +31 -0
+
+    arm_smccc_1_1_invoke(SMC_RMI_VERSION, host_version, &res);
+
+    if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
+        return -ENXIO;
+
+    version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1);
+    version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1);
+
+    if (res.a0 != RMI_SUCCESS) {
+        unsigned short high_version_major, high_version_minor;
+
+        high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2);
+        high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2);
+
+        pr_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n",
+               version_major, version_minor,
+               high_version_major, high_version_minor,
+               RMI_ABI_MAJOR_VERSION,
+               RMI_ABI_MINOR_VERSION);
+        return -ENXIO;
+    }
+
+    pr_info("RMI ABI version %d.%d\n", version_major, version_minor);
+
+    return 0;
+}
+
+static int __init arm64_init_rmi(void)
+{
+    /* Continue without realm support if we can't agree on a version */
Quoted 8 lines
Quoted 2 lines

+    if (rmi_check_version())
+        return 0;

Is this still a valid point that we have to return zero on errors returned from rmi_check_version() or other other function calls like rmi_features()? arm64_init_rmi() is triggered by subsys_initcall() where the return value needs to indicate success or failure. It's fine to return error code from arm64_init_rmi() in the path.

Hmm, I guess now this is moved to arm64 code this indeed doesn't need to. Within a module I believe an error return can fail the module loading.

I'm not sure it really makes much difference though - if this initialisation fails then it's not really an error - it just means the feature is unavailable.

Thanks,
Steve

Quoted 13 lines

arch/arm64/kernel/rmi.c line 65

Quoted 9 lines
arch/arm64/kernel/rmi.c new file +9 -0
+
+    if (WARN_ON(rmi_features(0, &rmm_feat_reg0)))
+        return 0;
+    if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
+        return 0;
+
+    return 0;
+}
+subsys_initcall(arm64_init_rmi);

Thanks,
Gavin

#28 lore raw

Re: [PATCH v14 06/44] arm64: RMI: Check for RMI support at init

Hi Steve,

Quoted 94 lines · Steven Price
Quoted 84 lines · Gavin Shan
Quoted 18 lines · Steven Price

Query the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.

---
v14:
  * This moves the basic RMI setup into the 'kernel' directory. This is because RMI will be used for some features outside of KVM so should be available even if KVM isn't compiled in.

---
   arch/arm64/include/asm/rmi_cmds.h |  3 ++
   arch/arm64/kernel/Makefile        |  2 +-
   arch/arm64/kernel/cpufeature.c    |  1 +
   arch/arm64/kernel/rmi.c           | 65 +++++++++++++++++++++++++++++++
   4 files changed, 70 insertions(+), 1 deletion(-)

  create mode 100644 arch/arm64/kernel/rmi.c

[...]

Quoted 62 lines
arch/arm64/kernel/rmi.c new file +19 -0
new file mode 100644
index 000000000000..99c1ccc35c11
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#include <linux/memblock.h>
+
+#include <asm/rmi_cmds.h>
+
+unsigned long rmm_feat_reg0;
+unsigned long rmm_feat_reg1;
+
+static int rmi_check_version(void)
+{
+    struct arm_smccc_res res;
+    unsigned short version_major, version_minor;
+    unsigned long host_version = RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION,
+                             RMI_ABI_MINOR_VERSION);
+    unsigned long aa64pfr0 =

read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+
+    /* If RME isn't supported, then RMI can't be */ +    if (cpuid_feature_extract_unsigned_field(aa64pfr0, ID_AA64PFR0_EL1_RME_SHIFT) == 0)
+        return -ENXIO;

arch/arm64/kernel/rmi.c new file +31 -0
+
+    arm_smccc_1_1_invoke(SMC_RMI_VERSION, host_version, &res);
+
+    if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
+        return -ENXIO;
+
+    version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1);
+    version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1);
+
+    if (res.a0 != RMI_SUCCESS) {
+        unsigned short high_version_major, high_version_minor;
+
+        high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2);
+        high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2);
+
+        pr_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n",
+               version_major, version_minor,
+               high_version_major, high_version_minor,
+               RMI_ABI_MAJOR_VERSION,
+               RMI_ABI_MINOR_VERSION);
+        return -ENXIO;
+    }
+
+    pr_info("RMI ABI version %d.%d\n", version_major, version_minor);
+
+    return 0;
+}
+
+static int __init arm64_init_rmi(void)
+{
+    /* Continue without realm support if we can't agree on a version */
Quoted 8 lines
Quoted 2 lines

+    if (rmi_check_version())
+        return 0;

Is this still a valid point that we have to return zero on errors returned from rmi_check_version() or other other function calls like rmi_features()? arm64_init_rmi() is triggered by subsys_initcall() where the return value needs to indicate success or failure. It's fine to return error code from arm64_init_rmi() in the path.

Quoted 7 lines

Hmm, I guess now this is moved to arm64 code this indeed doesn't need to. Within a module I believe an error return can fail the module loading.

I'm not sure it really makes much difference though - if this initialisation fails then it's not really an error - it just means the feature is unavailable.

I think the return value would be consistent to the value of 'arm64_rmi_is_available'. 'arm64_rmi_is_available' is true when zero is returned, otherwise, 'arm64_rmi_is_available' is false.

With the consistency between the return value and 'arm64_rmi_is_available', users are able to know the value of 'arm64_rmi_is_available' through kernel parameter 'initcall_debug'. With the kernel parameter, the initcalls including arm64_init_rmi() are traced and its return value is outputted in the traced messages, seeing do_trace_initcall_start().

Quoted 13 lines

Thanks,
Steve

Quoted 10 lines
Quoted 9 lines
arch/arm64/kernel/rmi.c new file +9 -0
+
+    if (WARN_ON(rmi_features(0, &rmm_feat_reg0)))
+        return 0;
+    if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
+        return 0;
+
+    return 0;
+}
+subsys_initcall(arm64_init_rmi);

Thanks,
Gavin

#29 lore raw

Re: [PATCH v14 06/44] arm64: RMI: Check for RMI support at init

Quoted 122 lines · Gavin Shan

Hi Steve,

Quoted 104 lines · Steven Price
Quoted 94 lines · Gavin Shan
Quoted 21 lines · Steven Price

Query the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.

---
v14:
   * This moves the basic RMI setup into the 'kernel' directory. This is
     because RMI will be used for some features outside of KVM so should
     be available even if KVM isn't compiled in.
---
   arch/arm64/include/asm/rmi_cmds.h |  3 ++ arch/arm64/kernel/Makefile        |  2 +- arch/arm64/kernel/cpufeature.c    |  1 + arch/arm64/kernel/rmi.c           | 65 ++++++++++++++++++++++++++
+++++

   4 files changed, 70 insertions(+), 1 deletion(-)

   create mode 100644 arch/arm64/kernel/rmi.c

[...]

Quoted 64 lines
arch/arm64/kernel/rmi.c new file +17 -0
new file mode 100644
index 000000000000..99c1ccc35c11
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#include <linux/memblock.h>
+
+#include <asm/rmi_cmds.h>
+
+unsigned long rmm_feat_reg0;
+unsigned long rmm_feat_reg1;
+
+static int rmi_check_version(void)
+{
+    struct arm_smccc_res res;
+    unsigned short version_major, version_minor;
+    unsigned long host_version =

RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION,
+                             RMI_ABI_MINOR_VERSION); +    unsigned long aa64pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+
+    /* If RME isn't supported, then RMI can't be */ +    if (cpuid_feature_extract_unsigned_field(aa64pfr0, ID_AA64PFR0_EL1_RME_SHIFT) == 0)
+        return -ENXIO;

arch/arm64/kernel/rmi.c new file +16 -0
+
+    arm_smccc_1_1_invoke(SMC_RMI_VERSION, host_version, &res);
+
+    if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
+        return -ENXIO;
+
+    version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1);
+    version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1);
+
+    if (res.a0 != RMI_SUCCESS) {
+        unsigned short high_version_major, high_version_minor;
+
+        high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2);
+        high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2);
+
+        pr_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.

%d\n",
+               version_major, version_minor, +               high_version_major, high_version_minor, +               RMI_ABI_MAJOR_VERSION,
+               RMI_ABI_MINOR_VERSION);
+        return -ENXIO;
+    }

arch/arm64/kernel/rmi.c new file +9 -0
+
+    pr_info("RMI ABI version %d.%d\n", version_major, version_minor);
+
+    return 0;
+}
+
+static int __init arm64_init_rmi(void)
+{
+    /* Continue without realm support if we can't agree on a
Quoted 3 lines

version */
+    if (rmi_check_version())
+        return 0;

Is this still a valid point that we have to return zero on errors

Quoted 8 lines

returned
from rmi_check_version() or other other function calls like rmi_features()?
arm64_init_rmi() is triggered by subsys_initcall() where the return value
needs to indicate success or failure. It's fine to return error code from
arm64_init_rmi() in the path.

Quoted 8 lines

Hmm, I guess now this is moved to arm64 code this indeed doesn't need to. Within a module I believe an error return can fail the module loading.

I'm not sure it really makes much difference though - if this initialisation fails then it's not really an error - it just means the feature is unavailable.

I think the return value would be consistent to the value of 'arm64_rmi_is_available'.
'arm64_rmi_is_available' is true when zero is returned, otherwise, 'arm64_rmi_is_available'
is false.

Quoted 8 lines

With the consistency between the return value and 'arm64_rmi_is_available', users are
able to know the value of 'arm64_rmi_is_available' through kernel parameter 'initcall_debug'.
With the kernel parameter, the initcalls including arm64_init_rmi() are traced and its
return value is outputted in the traced messages, seeing do_trace_initcall_start().

Fair enough, and actually refactoring this function to pass error codes up the call stack I think does improve the look.

Thanks,
Steve

Quoted 17 lines
Quoted 13 lines

Thanks,
Steve

Quoted 10 lines
Quoted 9 lines
arch/arm64/kernel/rmi.c new file +9 -0
+
+    if (WARN_ON(rmi_features(0, &rmm_feat_reg0)))
+        return 0;
+    if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
+        return 0;
+
+    return 0;
+}
+subsys_initcall(arm64_init_rmi);

Thanks,
Gavin

maintainer
#30 lore raw

Re: [PATCH v14 06/44] arm64: RMI: Check for RMI support at init

In reply to Steven Price
Quoted 65 lines · Steven Price

Query the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.

---
v14:
* This moves the basic RMI setup into the 'kernel' directory. This is because RMI will be used for some features outside of KVM so should be available even if KVM isn't compiled in.

---
 arch/arm64/include/asm/rmi_cmds.h |  3 ++
 arch/arm64/kernel/Makefile        |  2 +-
 arch/arm64/kernel/cpufeature.c    |  1 +
 arch/arm64/kernel/rmi.c           | 65 +++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/kernel/rmi.c
arch/arm64/include/asm/rmi_cmds.h +3 -0
index 04f7066894e9..9179934925c5 100644
@@ -10,6 +10,9 @@
 
 #include <asm/rmi_smc.h>
 
+extern unsigned long rmm_feat_reg0;
+extern unsigned long rmm_feat_reg1;
+
 struct rtt_entry {
 	unsigned long walk_level;
 	unsigned long desc;
arch/arm64/kernel/Makefile +1 -1
index 74b76bb70452..d68f351aae75 100644
@@ -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 idle.o patching.o pi/	\
-			   rsi.o jump_label.o
+			   rsi.o jump_label.o rmi.o
 
 obj-$(CONFIG_COMPAT)			+= sys32.o signal32.o			\
 					   sys_compat.o
arch/arm64/kernel/cpufeature.c +1 -0
index 6d53bb15cf7b..8bdd95a8c2de 100644
@@ -292,6 +292,7 @@ static const struct arm64_ftr_bits ftr_id_aa64isar3[] = {
 static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV3_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV2_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_RME_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_DIT_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AMU_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_MPAM_SHIFT, 4, 0),
arch/arm64/kernel/rmi.c new file +1 -0
new file mode 100644
index 000000000000..99c1ccc35c11
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0

arch/arm64/kernel/rmi.c line 11

Quoted 10 lines
arch/arm64/kernel/rmi.c new file +10 -0
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#include <linux/memblock.h>
+
+#include <asm/rmi_cmds.h>
+
+unsigned long rmm_feat_reg0;
+unsigned long rmm_feat_reg1;

What is the requirement for making those globally accessible? Can't they be made static and use an accessor that returns them? Can the variables be made __ro_after_init?

Quoted 44 lines
arch/arm64/kernel/rmi.c new file +44 -0
+
+static int rmi_check_version(void)
+{
+	struct arm_smccc_res res;
+	unsigned short version_major, version_minor;
+	unsigned long host_version = RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION,
+						     RMI_ABI_MINOR_VERSION);
+	unsigned long aa64pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+
+	/* If RME isn't supported, then RMI can't be */
+	if (cpuid_feature_extract_unsigned_field(aa64pfr0, ID_AA64PFR0_EL1_RME_SHIFT) == 0)
+		return -ENXIO;
+
+	arm_smccc_1_1_invoke(SMC_RMI_VERSION, host_version, &res);
+
+	if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
+		return -ENXIO;
+
+	version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1);
+	version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1);
+
+	if (res.a0 != RMI_SUCCESS) {
+		unsigned short high_version_major, high_version_minor;
+
+		high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2);
+		high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2);
+
+		pr_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n",
+		       version_major, version_minor,
+		       high_version_major, high_version_minor,
+		       RMI_ABI_MAJOR_VERSION,
+		       RMI_ABI_MINOR_VERSION);
+		return -ENXIO;
+	}
+
+	pr_info("RMI ABI version %d.%d\n", version_major, version_minor);
+
+	return 0;
+}
+
+static int __init arm64_init_rmi(void)
+{
+	/* Continue without realm support if we can't agree on a version */
+	if (rmi_check_version())

arch/arm64/kernel/rmi.c line 65

Quoted 10 lines
arch/arm64/kernel/rmi.c new file +10 -0
+		return 0;
+
+	if (WARN_ON(rmi_features(0, &rmm_feat_reg0)))
+		return 0;
+	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
+		return 0;
+
+	return 0;
+}
+subsys_initcall(arm64_init_rmi);

Is there any reliance on this being executed before or after KVM's own initialisation? If so, this should be captured.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#31 lore raw

Re: [PATCH v14 06/44] arm64: RMI: Check for RMI support at init

Quoted 73 lines · Marc Zyngier
Quoted 71 lines · Steven Price

Query the RMI version number and check if it is a compatible version. The first two feature registers are read and exposed for future code to use.

---
v14:
* This moves the basic RMI setup into the 'kernel' directory. This is because RMI will be used for some features outside of KVM so should be available even if KVM isn't compiled in.

---
 arch/arm64/include/asm/rmi_cmds.h |  3 ++
 arch/arm64/kernel/Makefile        |  2 +-
 arch/arm64/kernel/cpufeature.c    |  1 +
 arch/arm64/kernel/rmi.c           | 65 +++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/kernel/rmi.c
arch/arm64/include/asm/rmi_cmds.h +3 -0
index 04f7066894e9..9179934925c5 100644
@@ -10,6 +10,9 @@
 
 #include <asm/rmi_smc.h>
 
+extern unsigned long rmm_feat_reg0;
+extern unsigned long rmm_feat_reg1;
+
 struct rtt_entry {
 	unsigned long walk_level;
 	unsigned long desc;
arch/arm64/kernel/Makefile +1 -1
index 74b76bb70452..d68f351aae75 100644
@@ -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 idle.o patching.o pi/	\
-			   rsi.o jump_label.o
+			   rsi.o jump_label.o rmi.o
 
 obj-$(CONFIG_COMPAT)			+= sys32.o signal32.o			\
 					   sys_compat.o
arch/arm64/kernel/cpufeature.c +1 -0
index 6d53bb15cf7b..8bdd95a8c2de 100644
@@ -292,6 +292,7 @@ static const struct arm64_ftr_bits ftr_id_aa64isar3[] = {
 static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV3_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV2_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_RME_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_DIT_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AMU_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_MPAM_SHIFT, 4, 0),
arch/arm64/kernel/rmi.c new file +7 -0
new file mode 100644
index 000000000000..99c1ccc35c11
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#include <linux/memblock.h>
+
Quoted 8 lines

arch/arm64/kernel/rmi.c line 11

Quoted 4 lines
arch/arm64/kernel/rmi.c new file +4 -0
+#include <asm/rmi_cmds.h>
+
+unsigned long rmm_feat_reg0;
+unsigned long rmm_feat_reg1;

What is the requirement for making those globally accessible? Can't they be made static and use an accessor that returns them? Can the variables be made __ro_after_init?

Good point - there's no requirement. Also the name isn't quite right - these should be named rmi_ as there is a different set for RSI.

Quoted 49 lines
Quoted 49 lines
arch/arm64/kernel/rmi.c new file +49 -0
+
+static int rmi_check_version(void)
+{
+	struct arm_smccc_res res;
+	unsigned short version_major, version_minor;
+	unsigned long host_version = RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION,
+						     RMI_ABI_MINOR_VERSION);
+	unsigned long aa64pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+
+	/* If RME isn't supported, then RMI can't be */
+	if (cpuid_feature_extract_unsigned_field(aa64pfr0, ID_AA64PFR0_EL1_RME_SHIFT) == 0)
+		return -ENXIO;
+
+	arm_smccc_1_1_invoke(SMC_RMI_VERSION, host_version, &res);
+
+	if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
+		return -ENXIO;
+
+	version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1);
+	version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1);
+
+	if (res.a0 != RMI_SUCCESS) {
+		unsigned short high_version_major, high_version_minor;
+
+		high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2);
+		high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2);
+
+		pr_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n",
+		       version_major, version_minor,
+		       high_version_major, high_version_minor,
+		       RMI_ABI_MAJOR_VERSION,
+		       RMI_ABI_MINOR_VERSION);
+		return -ENXIO;
+	}
+
+	pr_info("RMI ABI version %d.%d\n", version_major, version_minor);
+
+	return 0;
+}
+
+static int __init arm64_init_rmi(void)
+{
+	/* Continue without realm support if we can't agree on a version */
+	if (rmi_check_version())
+		return 0;
+
+	if (WARN_ON(rmi_features(0, &rmm_feat_reg0)))
+		return 0;
+	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
Quoted 8 lines

arch/arm64/kernel/rmi.c line 65

Quoted 5 lines
arch/arm64/kernel/rmi.c new file +5 -0
+		return 0;
+
+	return 0;
+}
+subsys_initcall(arm64_init_rmi);

Is there any reliance on this being executed before or after KVM's own initialisation? If so, this should be captured.

Yes I'm expecting this to be called before KVM's initialisation. kvm_init_rmi() alls rmi_is_available() to check if CCA is supported and only enables the KVM side of things if that check passes. So if the initialisation was the other way round then Realm guests would be unsupported. I'll add a comment

/*
* Note arm64_init_rmi() must be called before kvm_init_rmi() otherwise KVM
* will not support realm guests. subsys_initcall() is called before
* module_init() (used for KVM) so this is OK.
*/

Thanks,
Steve

#32 lore raw

PATCH v14 7/44 arm64: RMI: Configure the RMM with the host's page size

In reply to Steven Price

RMM v2.0 brings the ability to set the RMM's granule size. Check the feature registers and configure the RMM so that it matches the host's page size. This means that operations can be done with a granulatity equal to PAGE_SIZE.

---
Changes since v13:
* Moved out of KVM.

---
 arch/arm64/kernel/rmi.c | 42 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
arch/arm64/kernel/rmi.c +42 -0
index 99c1ccc35c11..a14ead5dedda 100644
@@ -49,6 +49,45 @@ static int rmi_check_version(void)
 	return 0;
 }
 
+static int rmi_configure(void)
+{
+	struct rmm_config *config __free(free_page) = NULL;
+	unsigned long ret;
+
+	config = (struct rmm_config *)get_zeroed_page(GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;
+
+	switch (PAGE_SIZE) {
+	case SZ_4K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_4KB;
+		break;
+	case SZ_16K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_16KB;
+		break;
+	case SZ_64K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_64KB;
+		break;
+	default:
+		pr_err("Unsupported PAGE_SIZE for RMM\n");
+		return -EINVAL;
+	}
+
+	ret = rmi_rmm_config_set(virt_to_phys(config));
+	if (ret) {
+		pr_err("RMM config set failed\n");
+		return -EINVAL;
+	}
+
+	ret = rmi_rmm_activate();
+	if (ret) {
+		pr_err("RMM activate failed\n");
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
 static int __init arm64_init_rmi(void)
 {
 	/* Continue without realm support if we can't agree on a version */
@@ -60,6 +99,9 @@ static int __init arm64_init_rmi(void)
 	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
 		return 0;
 
+	if (rmi_configure())
+		return 0;
+
 	return 0;
 }
 subsys_initcall(arm64_init_rmi);
Signature · 1 line
2.43.0
#33 lore raw

Re: [PATCH v14 07/44] arm64: RMI: Configure the RMM with the host's page size

Hi Steven,

Quoted 41 lines · Steven Price

RMM v2.0 brings the ability to set the RMM's granule size. Check the feature registers and configure the RMM so that it matches the host's page size. This means that operations can be done with a granulatity equal to PAGE_SIZE.

---
Changes since v13:
* Moved out of KVM.

---
  arch/arm64/kernel/rmi.c | 42 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 42 insertions(+)
arch/arm64/kernel/rmi.c +20 -0
index 99c1ccc35c11..a14ead5dedda 100644
@@ -49,6 +49,45 @@ static int rmi_check_version(void)
  	return 0;
  }
 
+static int rmi_configure(void)
+{
+	struct rmm_config *config __free(free_page) = NULL;
+	unsigned long ret;
+
+	config = (struct rmm_config *)get_zeroed_page(GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;
+
+	switch (PAGE_SIZE) {
+	case SZ_4K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_4KB;
+		break;
+	case SZ_16K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_16KB;
+		break;
+	case SZ_64K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_64KB;
+		break;
+	default:

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 81

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+		pr_err("Unsupported PAGE_SIZE for RMM\n");
+		return -EINVAL;
+	}
+
+	ret = rmi_rmm_config_set(virt_to_phys(config));
+	if (ret) {
+		pr_err("RMM config set failed\n");
+		return -EINVAL;
+	}
+

Looking at branch 'topics/rmm-v2.0-poc_2' of RMM implementation, the granule size is fixed to be 4KB at present. I'm not sure if I have looked into correct RMM implementation, but 'topics/rmm-v2.0-poc_2' is recommended one in the cover letter.

Besides, there has checks in the handler of the RMI command to make sure that struct rmm_config::tracking_region_size to be 1GB, indicated by zero. It maybe worthy to set it before call to rmi_rmm_config_set().

	config.tracking_region_size = 0; /* 1GB */
	ret = rmi_rmm_config_set(virt_to_phys(config));
Quoted 13 lines
arch/arm64/kernel/rmi.c +9 -0
+	ret = rmi_rmm_activate();
+	if (ret) {
+		pr_err("RMM activate failed\n");
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
  static int __init arm64_init_rmi(void)
  {
  	/* Continue without realm support if we can't agree on a version */

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 107

Quoted 9 lines
arch/arm64/kernel/rmi.c +3 -0
@@ -60,6 +99,9 @@ static int __init arm64_init_rmi(void)
  	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
  		return 0;
 
+	if (rmi_configure())
+		return 0;
+
  	return 0;
  }
  subsys_initcall(arm64_init_rmi);

Thanks,
Gavin

#34 lore raw

Re: [PATCH v14 07/44] arm64: RMI: Configure the RMM with the host's page size

Quoted 50 lines · Gavin Shan

Hi Steven,

Quoted 47 lines · Steven Price

RMM v2.0 brings the ability to set the RMM's granule size. Check the feature registers and configure the RMM so that it matches the host's page size. This means that operations can be done with a granulatity equal to PAGE_SIZE.

---
Changes since v13:
  * Moved out of KVM.
---
  arch/arm64/kernel/rmi.c | 42 +++++++++++++++++++++++++++++++++++++++++

  1 file changed, 42 insertions(+)
arch/arm64/kernel/rmi.c +27 -0
index 99c1ccc35c11..a14ead5dedda 100644
@@ -49,6 +49,45 @@ static int rmi_check_version(void)
       return 0;
   }
+static int rmi_configure(void)
+{
+    struct rmm_config *config __free(free_page) = NULL;
+    unsigned long ret;
+
+    config = (struct rmm_config *)get_zeroed_page(GFP_KERNEL);
+    if (!config)
+        return -ENOMEM;
+
+    switch (PAGE_SIZE) {
+    case SZ_4K:
+        config->rmi_granule_size = RMI_GRANULE_SIZE_4KB;
+        break;
+    case SZ_16K:
+        config->rmi_granule_size = RMI_GRANULE_SIZE_16KB;
+        break;
+    case SZ_64K:
+        config->rmi_granule_size = RMI_GRANULE_SIZE_64KB;
+        break;
+    default:
+        pr_err("Unsupported PAGE_SIZE for RMM\n");
+        return -EINVAL;
+    }
+
+    ret = rmi_rmm_config_set(virt_to_phys(config));
+    if (ret) {
+        pr_err("RMM config set failed\n");
Quoted 9 lines

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 75

Quoted 3 lines
arch/arm64/kernel/rmi.c +3 -0
+        return -EINVAL;
+    }
+

Looking at branch 'topics/rmm-v2.0-poc_2' of RMM implementation, the granule size is fixed to be 4KB at present. I'm not sure if I have looked into correct RMM implementation, but 'topics/rmm-v2.0-poc_2' is recommended one in the cover letter.

You are right. The tf-RMM only supports 4KB. The policy at the KVM host is to set the Linux PAGE_SIZE for the GRANULE_SIZE (at least for now). If the RMM doesn't support the PAGE_SIZE, we don't support the RMM.

Quoted 5 lines

Besides, there has checks in the handler of the RMI command to make sure that struct rmm_config::tracking_region_size to be 1GB, indicated by zero. It maybe worthy to set it before call to rmi_rmm_config_set().

    config.tracking_region_size = 0; /* 1GB */

Thanks, this explicit initialisation is missing, though in effect the
value is 0'd. Also, we can't really say 1GB here, because the driver should work for an RMM capable of 64K. So, instead, may be we could :

	/* See the definition of RMM_GRANULE_TRACKING_SIZE */
	config.tracking_region_size = 0;

Suzuki

Quoted 28 lines

    ret = rmi_rmm_config_set(virt_to_phys(config));

Quoted 13 lines
arch/arm64/kernel/rmi.c +9 -0
+    ret = rmi_rmm_activate();
+    if (ret) {
+        pr_err("RMM activate failed\n");
+        return -ENXIO;
+    }
+
+    return 0;
+}
+
   static int __init arm64_init_rmi(void)
   {
       /* Continue without realm support if we can't agree on a version */

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 106

Quoted 8 lines
arch/arm64/kernel/rmi.c +3 -0
@@ -60,6 +99,9 @@ static int __init arm64_init_rmi(void)
       if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
           return 0;
+    if (rmi_configure())
+        return 0;
+
       return 0;
   }
   subsys_initcall(arm64_init_rmi);

Thanks,
Gavin

maintainer
#35 lore raw

Re: [PATCH v14 07/44] arm64: RMI: Configure the RMM with the host's page size

In reply to Steven Price
Quoted 20 lines · Steven Price

RMM v2.0 brings the ability to set the RMM's granule size. Check the feature registers and configure the RMM so that it matches the host's page size. This means that operations can be done with a granulatity equal to PAGE_SIZE.

---
Changes since v13:
* Moved out of KVM.

---
 arch/arm64/kernel/rmi.c | 42 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
arch/arm64/kernel/rmi.c +0 -0
index 99c1ccc35c11..a14ead5dedda 100644
@@ -49,6 +49,45 @@ static int rmi_check_version(void)
 	return 0;

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 59

Quoted 10 lines
arch/arm64/kernel/rmi.c +8 -0
 }
 
+static int rmi_configure(void)
+{
+	struct rmm_config *config __free(free_page) = NULL;
+	unsigned long ret;
+
+	config = (struct rmm_config *)get_zeroed_page(GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;

This is the sort of buggy construct that is highlighted in include/linux/cleanup.h: initialising the object for cleanup with NULL, and only later assigning the expected value.

It may not matter here, but it will catch you (or more probably me) in the future.

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 72

Quoted 13 lines
arch/arm64/kernel/rmi.c +13 -0
+
+	switch (PAGE_SIZE) {
+	case SZ_4K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_4KB;
+		break;
+	case SZ_16K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_16KB;
+		break;
+	case SZ_64K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_64KB;
+		break;
+	default:
+		pr_err("Unsupported PAGE_SIZE for RMM\n");

Do you really anticipate PAGE_SIZE being any other value? This is 100% dead code. If you want to be extra cautious, have a BUILD_BUg_ON().

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 80

Quoted 8 lines
arch/arm64/kernel/rmi.c +8 -0
+		return -EINVAL;
+	}
+
+	ret = rmi_rmm_config_set(virt_to_phys(config));
+	if (ret) {
+		pr_err("RMM config set failed\n");
+		return -EINVAL;
+	}

What is the live cycle of the page when the call succeeds? Is it switched back to the NS PAS and allowed to be freed?

Quoted 14 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	ret = rmi_rmm_activate();
+	if (ret) {
+		pr_err("RMM activate failed\n");
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
 static int __init arm64_init_rmi(void)
 {
 	/* Continue without realm support if we can't agree on a version */

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 107

Quoted 9 lines
arch/arm64/kernel/rmi.c +3 -0
@@ -60,6 +99,9 @@ static int __init arm64_init_rmi(void)
 	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
 		return 0;
 
+	if (rmi_configure())
+		return 0;
+
 	return 0;
 }
 subsys_initcall(arm64_init_rmi);

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#36 lore raw

Re: [PATCH v14 07/44] arm64: RMI: Configure the RMM with the host's page size

Quoted 57 lines · Marc Zyngier
Quoted 20 lines · Steven Price

RMM v2.0 brings the ability to set the RMM's granule size. Check the feature registers and configure the RMM so that it matches the host's page size. This means that operations can be done with a granulatity equal to PAGE_SIZE.

---
Changes since v13:
* Moved out of KVM.

---
  arch/arm64/kernel/rmi.c | 42 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 42 insertions(+)
arch/arm64/kernel/rmi.c +0 -0
index 99c1ccc35c11..a14ead5dedda 100644
@@ -49,6 +49,45 @@ static int rmi_check_version(void)
  	return 0;

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 59

Quoted 10 lines
arch/arm64/kernel/rmi.c +8 -0
  }
 
+static int rmi_configure(void)
+{
+	struct rmm_config *config __free(free_page) = NULL;
+	unsigned long ret;
+
+	config = (struct rmm_config *)get_zeroed_page(GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;

This is the sort of buggy construct that is highlighted in include/linux/cleanup.h: initialising the object for cleanup with NULL, and only later assigning the expected value.

It may not matter here, but it will catch you (or more probably me) in the future.

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 72

Quoted 13 lines
arch/arm64/kernel/rmi.c +13 -0
+
+	switch (PAGE_SIZE) {
+	case SZ_4K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_4KB;
+		break;
+	case SZ_16K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_16KB;
+		break;
+	case SZ_64K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_64KB;
+		break;
+	default:
+		pr_err("Unsupported PAGE_SIZE for RMM\n");

Do you really anticipate PAGE_SIZE being any other value? This is 100% dead code. If you want to be extra cautious, have a BUILD_BUg_ON().

Quoted 11 lines

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 80

Quoted 8 lines
arch/arm64/kernel/rmi.c +8 -0
+		return -EINVAL;
+	}
+
+	ret = rmi_rmm_config_set(virt_to_phys(config));
+	if (ret) {
+		pr_err("RMM config set failed\n");
+		return -EINVAL;
+	}

What is the live cycle of the page when the call succeeds? Is it switched back to the NS PAS and allowed to be freed?

It always remains in the NS world. We relay some information in the NS PAS page, which the RMM consumes. The checks are performed on the values consumed by the RMM.

Kind regards
Suzuki

Quoted 29 lines
Quoted 15 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	ret = rmi_rmm_activate();
+	if (ret) {
+		pr_err("RMM activate failed\n");
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
  static int __init arm64_init_rmi(void)
  {
  	/* Continue without realm support if we can't agree on a version */

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 107

Quoted 9 lines
arch/arm64/kernel/rmi.c +3 -0
@@ -60,6 +99,9 @@ static int __init arm64_init_rmi(void)
  	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
  		return 0;
 
+	if (rmi_configure())
+		return 0;
+
  	return 0;
  }
  subsys_initcall(arm64_init_rmi);

Thanks,

	M.
#37 lore raw

Re: [PATCH v14 07/44] arm64: RMI: Configure the RMM with the host's page size

In reply to Marc Zyngier
Quoted 37 lines · Marc Zyngier
Quoted 20 lines · Steven Price

RMM v2.0 brings the ability to set the RMM's granule size. Check the feature registers and configure the RMM so that it matches the host's page size. This means that operations can be done with a granulatity equal to PAGE_SIZE.

---
Changes since v13:
* Moved out of KVM.

---
 arch/arm64/kernel/rmi.c | 42 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
arch/arm64/kernel/rmi.c +0 -0
index 99c1ccc35c11..a14ead5dedda 100644
@@ -49,6 +49,45 @@ static int rmi_check_version(void)
 	return 0;

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 59

Quoted 10 lines
arch/arm64/kernel/rmi.c +8 -0
 }
 
+static int rmi_configure(void)
+{
+	struct rmm_config *config __free(free_page) = NULL;
+	unsigned long ret;
+
+	config = (struct rmm_config *)get_zeroed_page(GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;

This is the sort of buggy construct that is highlighted in include/linux/cleanup.h: initialising the object for cleanup with NULL, and only later assigning the expected value.

Quoted 2 lines

It may not matter here, but it will catch you (or more probably me) in the future.

Good spot. I have to admit I'm still getting the hang of these cleanup handlers.

Quoted 8 lines
Quoted 8 lines
arch/arm64/kernel/rmi.c +8 -0
+
+	switch (PAGE_SIZE) {
+	case SZ_4K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_4KB;
+		break;
+	case SZ_16K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_16KB;
+		break;
Quoted 8 lines

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 72

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+	case SZ_64K:
+		config->rmi_granule_size = RMI_GRANULE_SIZE_64KB;
+		break;
+	default:
+		pr_err("Unsupported PAGE_SIZE for RMM\n");

Do you really anticipate PAGE_SIZE being any other value? This is 100% dead code. If you want to be extra cautious, have a BUILD_BUg_ON().

No, but falling through is clearly wrong (and likely to trigger AI review comments if nothing else) - BUILD_BUG() sounds like a good solution.

Quoted 11 lines

arch/arm64/kernel/rmi.c static int rmi_check_version(void) · line 80

Quoted 8 lines
arch/arm64/kernel/rmi.c +8 -0
+		return -EINVAL;
+	}
+
+	ret = rmi_rmm_config_set(virt_to_phys(config));
+	if (ret) {
+		pr_err("RMM config set failed\n");
+		return -EINVAL;
+	}

What is the live cycle of the page when the call succeeds? Is it switched back to the NS PAS and allowed to be freed?

Yes, as Suzuki answered - it never leaves the NS PAS. The RMM just reads it.

Thanks,
Steve

Quoted 28 lines
Quoted 14 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	ret = rmi_rmm_activate();
+	if (ret) {
+		pr_err("RMM activate failed\n");
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
 static int __init arm64_init_rmi(void)
 {
 	/* Continue without realm support if we can't agree on a version */

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 107

Quoted 9 lines
arch/arm64/kernel/rmi.c +3 -0
@@ -60,6 +99,9 @@ static int __init arm64_init_rmi(void)
 	if (WARN_ON(rmi_features(1, &rmm_feat_reg1)))
 		return 0;
 
+	if (rmi_configure())
+		return 0;
+
 	return 0;
 }
 subsys_initcall(arm64_init_rmi);

Thanks,

	M.
#38 lore raw

PATCH v14 8/44 arm64: RMI: Ensure that the RMM has GPT entries for memory

In reply to Steven Price

The RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegated individual pages.

For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.

Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.

Note: support is currently missing for SROs which means that if the RMM needs memory donating this will fail (and render CCA unusable in Linux). This effectively means that the L1 GPT tables must be created before Linux starts.

---
Changes since v13:
* Moved out of KVM

---
 arch/arm64/include/asm/rmi_cmds.h |   2 +
 arch/arm64/kernel/rmi.c           | 103 ++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +2 -0
index 9179934925c5..9078a2920a7c 100644
@@ -33,6 +33,8 @@ struct rmi_sro_state {
 } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
 	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+bool rmi_is_available(void);
+
 unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
 void rmi_sro_free(struct rmi_sro_state *sro);
 
arch/arm64/kernel/rmi.c +103 -0

121 lines not on this page · raw message

Signature · 1 line
2.43.0
#39 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 188

Quoted 6 lines
arch/arm64/kernel/rmi.c +6 -0
+
+bool rmi_is_available(void)
+{
+	return arm64_rmi_is_available;
+}
+

Can we rename to is_rmi_available(void) ?

-aneesh

#40 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

Quoted 8 lines · Aneesh Kumar K.V

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 188

Quoted 6 lines
arch/arm64/kernel/rmi.c +6 -0
+
+bool rmi_is_available(void)
+{
+	return arm64_rmi_is_available;
+}
+

Can we rename to is_rmi_available(void) ?

Sure, will do.

Thanks,
Steve

#41 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

In reply to Steven Price

Hi Steven,

Quoted 64 lines · Steven Price

The RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegated individual pages.

For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.

Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.

Note: support is currently missing for SROs which means that if the RMM needs memory donating this will fail (and render CCA unusable in Linux). This effectively means that the L1 GPT tables must be created before Linux starts.

---
Changes since v13:
* Moved out of KVM

---
  arch/arm64/include/asm/rmi_cmds.h |   2 +
  arch/arm64/kernel/rmi.c           | 103 ++++++++++++++++++++++++++++++
  2 files changed, 105 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +2 -0
index 9179934925c5..9078a2920a7c 100644
@@ -33,6 +33,8 @@ struct rmi_sro_state {
  } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
  	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+bool rmi_is_available(void);
+
  unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
  void rmi_sro_free(struct rmi_sro_state *sro);
 
arch/arm64/kernel/rmi.c +4 -0
index a14ead5dedda..52a415e99500 100644
@@ -7,6 +7,8 @@
 
  #include <asm/rmi_cmds.h>
 
+static bool arm64_rmi_is_available;
+
  unsigned long rmm_feat_reg0;
  unsigned long rmm_feat_reg1;
 
@@ -88,6 +90,102 @@ static int rmi_configure(void)
  	return 0;
  }
 
+/*
+ * For now we set the tracking_region_size to 0 for RMI_RMM_CONFIG_SET().

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 104

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+ * TODO: Support other tracking sizes (via Kconfig option).
+ */
+#ifdef CONFIG_PAGE_SIZE_4KB
+#define RMM_GRANULE_TRACKING_SIZE	SZ_1G
+#elif defined(CONFIG_PAGE_SIZE_16KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_32M
+#elif defined(CONFIG_PAGE_SIZE_64KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_512M
+#endif
+

RMM_GRANULE_TRACKING_SIZE is never used in this series.

Quoted 20 lines
arch/arm64/kernel/rmi.c +20 -0
+/*
+ * Make sure the area is tracked by RMM at FINE granularity.
+ * We do not support changing the tracking yet.
+ */
+static int rmi_verify_memory_tracking(phys_addr_t start, phys_addr_t end)
+{
+	while (start < end) {
+		unsigned long ret, category, state, next;
+
+		ret = rmi_granule_tracking_get(start, end, &category, &state, &next);
+		if (ret != RMI_SUCCESS ||
+		    state != RMI_TRACKING_FINE ||
+		    category != RMI_MEM_CATEGORY_CONVENTIONAL) {
+			/* TODO: Set granule tracking in this case */
+			pr_err("Granule tracking for region isn't fine/conventional: %llx",
+			       start);
+			return -ENODEV;
+		}
+		start = next;
+	}

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 134

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	return 0;
+}
+
+static unsigned long rmi_l0gpt_size(void)
+{
+	return 1UL << (30 + FIELD_GET(RMI_FEATURE_REGISTER_1_L0GPTSZ,
+				      rmm_feat_reg1));
+}
+

rmi_l0gpt_size() is only used by rmi_create_gpts(), its logic can be combined to that function.

Quoted 59 lines
arch/arm64/kernel/rmi.c +54 -0
+static int rmi_create_gpts(phys_addr_t start, phys_addr_t end)
+{
+	unsigned long l0gpt_sz = rmi_l0gpt_size();
+
+	start = ALIGN_DOWN(start, l0gpt_sz);
+	end = ALIGN(end, l0gpt_sz);
+
+	while (start < end) {
+		int ret = rmi_gpt_l1_create(start);
+
+		/*
+		 * Make sure the L1 GPT tables are created for the region.
+		 * RMI_ERROR_GPT indicates the L1 table already exists.
+		 */
+		if (ret && ret != RMI_ERROR_GPT) {
+			/*
+			 * FIXME: Handle SRO so that memory can be donated for
+			 * the tables.
+			 */
+			pr_err("GPT Level1 table missing for %llx\n", start);
+			return -ENOMEM;
+		}
+		start += l0gpt_sz;
+	}
+
+	return 0;
+}
+
+static int rmi_init_metadata(void)
+{
+	phys_addr_t start, end;
+	const struct memblock_region *r;
+
+	for_each_mem_region(r) {
+		int ret;
+
+		start = memblock_region_memory_base_pfn(r) << PAGE_SHIFT;
+		end = memblock_region_memory_end_pfn(r) << PAGE_SHIFT;
+		ret = rmi_verify_memory_tracking(start, end);
+		if (ret)
+			return ret;
+		ret = rmi_create_gpts(start, end);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+bool rmi_is_available(void)
+{
+	return arm64_rmi_is_available;
+}
+
  static int __init arm64_init_rmi(void)
  {
  	/* Continue without realm support if we can't agree on a version */
@@ -101,6 +199,11 @@ static int __init arm64_init_rmi(void)
 

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 209

Quoted 10 lines
arch/arm64/kernel/rmi.c +5 -0
  	if (rmi_configure())
  		return 0;
+	if (rmi_init_metadata())
+		return 0;
+
+	arm64_rmi_is_available = true;
+	pr_info("RMI configured");
 
  	return 0;
  }

Thanks,
Gavin

#42 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

Quoted 67 lines · Gavin Shan

Hi Steven,

Quoted 64 lines · Steven Price

The RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegated individual pages.

For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.

Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.

Note: support is currently missing for SROs which means that if the RMM needs memory donating this will fail (and render CCA unusable in Linux). This effectively means that the L1 GPT tables must be created before Linux starts.

---
Changes since v13:
  * Moved out of KVM
---
  arch/arm64/include/asm/rmi_cmds.h |   2 + arch/arm64/kernel/rmi.c           | 103 ++++++++++++++++++++++++++++++

  2 files changed, 105 insertions(+)
arch/arm64/include/ renamed from arch/arm64/include/asm/rmi_cmds.h +0 -0

asm/rmi_cmds.h
index 9179934925c5..9078a2920a7c 100644

arch/arm64/include/asm/rmi_cmds.h +1 -0
@@ -33,6 +33,8 @@ struct rmi_sro_state {
   } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||            \
        RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
   +bool rmi_is_available(void);
+
   unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
   void rmi_sro_free(struct rmi_sro_state *sro);

  diff --git a/arch/arm64/kernel/rmi.c b/arch/arm64/kernel/rmi.c index a14ead5dedda..52a415e99500 100644

arch/arm64/kernel/rmi.c +1 -0
@@ -7,6 +7,8 @@
     #include <asm/rmi_cmds.h>
   +static bool arm64_rmi_is_available;
+
   unsigned long rmm_feat_reg0;
   unsigned long rmm_feat_reg1;
   @@ -88,6 +90,102 @@ static int rmi_configure(void)
       return 0;

  }
  +/*
+ * For now we set the tracking_region_size to 0 for RMI_RMM_CONFIG_SET().
+ * TODO: Support other tracking sizes (via Kconfig option).
+ */

arch/arm64/kernel/rmi.c +2 -0
+#ifdef CONFIG_PAGE_SIZE_4KB
+#define RMM_GRANULE_TRACKING_SIZE    SZ_1G
Quoted 8 lines

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 104

Quoted 6 lines
arch/arm64/kernel/rmi.c +6 -0
+#elif defined(CONFIG_PAGE_SIZE_16KB)
+#define RMM_GRANULE_TRACKING_SIZE    SZ_32M
+#elif defined(CONFIG_PAGE_SIZE_64KB)
+#define RMM_GRANULE_TRACKING_SIZE    SZ_512M
+#endif
+

RMM_GRANULE_TRACKING_SIZE is never used in this series.

Ah, good spot. In a previous version the tracking size was necessary when walking below. But the spec was updated to a range based API so this is no longer necessary.

Quoted 28 lines
Quoted 28 lines
arch/arm64/kernel/rmi.c +5 -0
+/*
+ * Make sure the area is tracked by RMM at FINE granularity.
+ * We do not support changing the tracking yet.
+ */
+static int rmi_verify_memory_tracking(phys_addr_t start, phys_addr_t

end)

arch/arm64/kernel/rmi.c +5 -0
+{
+    while (start < end) {
+        unsigned long ret, category, state, next;
+
+        ret = rmi_granule_tracking_get(start, end, &category, &state,

&next);
+        if (ret != RMI_SUCCESS ||
+            state != RMI_TRACKING_FINE ||
+            category != RMI_MEM_CATEGORY_CONVENTIONAL) { +            /* TODO: Set granule tracking in this case */ +            pr_err("Granule tracking for region isn't fine/
conventional: %llx",
+                   start);
+            return -ENODEV;
+        }
+        start = next;
+    }

arch/arm64/kernel/rmi.c +5 -0
+
+    return 0;
+}
+
+static unsigned long rmi_l0gpt_size(void)
Quoted 8 lines

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 134

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+{
+    return 1UL << (30 + FIELD_GET(RMI_FEATURE_REGISTER_1_L0GPTSZ,
+                      rmm_feat_reg1));
+}
+

rmi_l0gpt_size() is only used by rmi_create_gpts(), its logic can be combined to that function.

True - I think partly due to the long line I split this into a separate function. But I could do something like:

	unsigned long l0gpt_sz;

	l0gpt_sz = 1UL << (30 + FIELD_GET(RMI_FEATURE_REGISTER_1_L0GPTSZ,
					  rmi_feat_reg(1)));

which isn't too bad.

Thanks,
Steve

Quoted 71 lines
Quoted 58 lines
arch/arm64/kernel/rmi.c +54 -0
+static int rmi_create_gpts(phys_addr_t start, phys_addr_t end)
+{
+    unsigned long l0gpt_sz = rmi_l0gpt_size();
+
+    start = ALIGN_DOWN(start, l0gpt_sz);
+    end = ALIGN(end, l0gpt_sz);
+
+    while (start < end) {
+        int ret = rmi_gpt_l1_create(start);
+
+        /*
+         * Make sure the L1 GPT tables are created for the region.
+         * RMI_ERROR_GPT indicates the L1 table already exists.
+         */
+        if (ret && ret != RMI_ERROR_GPT) {
+            /*
+             * FIXME: Handle SRO so that memory can be donated for
+             * the tables.
+             */
+            pr_err("GPT Level1 table missing for %llx\n", start);
+            return -ENOMEM;
+        }
+        start += l0gpt_sz;
+    }
+
+    return 0;
+}
+
+static int rmi_init_metadata(void)
+{
+    phys_addr_t start, end;
+    const struct memblock_region *r;
+
+    for_each_mem_region(r) {
+        int ret;
+
+        start = memblock_region_memory_base_pfn(r) << PAGE_SHIFT;
+        end = memblock_region_memory_end_pfn(r) << PAGE_SHIFT;
+        ret = rmi_verify_memory_tracking(start, end);
+        if (ret)
+            return ret;
+        ret = rmi_create_gpts(start, end);
+        if (ret)
+            return ret;
+    }
+
+    return 0;
+}
+
+bool rmi_is_available(void)
+{
+    return arm64_rmi_is_available;
+}
+

  static int __init arm64_init_rmi(void)
  {
      /* Continue without realm support if we can't agree on a version */

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 207

Quoted 9 lines
arch/arm64/kernel/rmi.c +5 -0
@@ -101,6 +199,11 @@ static int __init arm64_init_rmi(void)
         if (rmi_configure())
           return 0;
+    if (rmi_init_metadata())
+        return 0;
+
+    arm64_rmi_is_available = true;
+    pr_info("RMI configured");
         return 0;
   }

Thanks,
Gavin

maintainer
#43 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

In reply to Steven Price
Quoted 64 lines · Steven Price

The RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegated individual pages.

For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.

Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.

Note: support is currently missing for SROs which means that if the RMM needs memory donating this will fail (and render CCA unusable in Linux). This effectively means that the L1 GPT tables must be created before Linux starts.

---
Changes since v13:
* Moved out of KVM

---
 arch/arm64/include/asm/rmi_cmds.h |   2 +
 arch/arm64/kernel/rmi.c           | 103 ++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +2 -0
index 9179934925c5..9078a2920a7c 100644
@@ -33,6 +33,8 @@ struct rmi_sro_state {
 } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
 	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+bool rmi_is_available(void);
+
 unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
 void rmi_sro_free(struct rmi_sro_state *sro);
 
arch/arm64/kernel/rmi.c +3 -0
index a14ead5dedda..52a415e99500 100644
@@ -7,6 +7,8 @@
 
 #include <asm/rmi_cmds.h>
 
+static bool arm64_rmi_is_available;
+
 unsigned long rmm_feat_reg0;
 unsigned long rmm_feat_reg1;
 
@@ -88,6 +90,102 @@ static int rmi_configure(void)
 	return 0;
 }
 
+/*

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 103

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+ * For now we set the tracking_region_size to 0 for RMI_RMM_CONFIG_SET().
+ * TODO: Support other tracking sizes (via Kconfig option).
+ */
+#ifdef CONFIG_PAGE_SIZE_4KB
+#define RMM_GRANULE_TRACKING_SIZE	SZ_1G
+#elif defined(CONFIG_PAGE_SIZE_16KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_32M
+#elif defined(CONFIG_PAGE_SIZE_64KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_512M
+#endif

Basically, a level 2 mapping. Which means this whole block really is:

#define RMM_GRANULE_TRAKING_SIZE (2 * PAGE_SHIFT - 3)

(adjust for D128 as needed).

Quoted 8 lines
arch/arm64/kernel/rmi.c +8 -0
+
+/*
+ * Make sure the area is tracked by RMM at FINE granularity.
+ * We do not support changing the tracking yet.
+ */
+static int rmi_verify_memory_tracking(phys_addr_t start, phys_addr_t end)
+{
+	while (start < end) {

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 121

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+		unsigned long ret, category, state, next;
+
+		ret = rmi_granule_tracking_get(start, end, &category, &state, &next);
+		if (ret != RMI_SUCCESS ||
+		    state != RMI_TRACKING_FINE ||
+		    category != RMI_MEM_CATEGORY_CONVENTIONAL) {
+			/* TODO: Set granule tracking in this case */
+			pr_err("Granule tracking for region isn't fine/conventional: %llx",
+			       start);
+			return -ENODEV;

How is this triggered? Do we really need to spam the console with this? A PA doesn't mean much, and there is no context (stack trace).

If that's not expected, turn this into a WARN_ONCE().

Quoted 24 lines
arch/arm64/kernel/rmi.c +24 -0
+		}
+		start = next;
+	}
+
+	return 0;
+}
+
+static unsigned long rmi_l0gpt_size(void)
+{
+	return 1UL << (30 + FIELD_GET(RMI_FEATURE_REGISTER_1_L0GPTSZ,
+				      rmm_feat_reg1));
+}
+
+static int rmi_create_gpts(phys_addr_t start, phys_addr_t end)
+{
+	unsigned long l0gpt_sz = rmi_l0gpt_size();
+
+	start = ALIGN_DOWN(start, l0gpt_sz);
+	end = ALIGN(end, l0gpt_sz);
+
+	while (start < end) {
+		int ret = rmi_gpt_l1_create(start);
+
+		/*

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 155

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+		 * Make sure the L1 GPT tables are created for the region.
+		 * RMI_ERROR_GPT indicates the L1 table already exists.
+		 */
+		if (ret && ret != RMI_ERROR_GPT) {
+			/*
+			 * FIXME: Handle SRO so that memory can be donated for
+			 * the tables.
+			 */
+			pr_err("GPT Level1 table missing for %llx\n", start);
+			return -ENOMEM;

If any of this fails, where is the cleanup done? Is that part of the missing SRO support that's indicated in the commit message?

Quoted 14 lines
arch/arm64/kernel/rmi.c +14 -0
+		}
+		start += l0gpt_sz;
+	}
+
+	return 0;
+}
+
+static int rmi_init_metadata(void)
+{
+	phys_addr_t start, end;
+	const struct memblock_region *r;
+
+	for_each_mem_region(r) {
+		int ret;

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 179

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+
+		start = memblock_region_memory_base_pfn(r) << PAGE_SHIFT;
+		end = memblock_region_memory_end_pfn(r) << PAGE_SHIFT;
+		ret = rmi_verify_memory_tracking(start, end);
+		if (ret)
+			return ret;
+		ret = rmi_create_gpts(start, end);
+		if (ret)
+			return ret;
+	}

How does this work with, say, memory hotplug?

Quoted 14 lines
arch/arm64/kernel/rmi.c +9 -0
+
+	return 0;
+}
+
+bool rmi_is_available(void)
+{
+	return arm64_rmi_is_available;
+}
+
 static int __init arm64_init_rmi(void)
 {
 	/* Continue without realm support if we can't agree on a version */
@@ -101,6 +199,11 @@ static int __init arm64_init_rmi(void)
 

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 209

Quoted 10 lines
arch/arm64/kernel/rmi.c +5 -0
 	if (rmi_configure())
 		return 0;
+	if (rmi_init_metadata())
+		return 0;
+
+	arm64_rmi_is_available = true;
+	pr_info("RMI configured");
 
 	return 0;
 }

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#44 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

Quoted 18 lines · Marc Zyngier

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 103

Quoted 12 lines · Steven Price
arch/arm64/kernel/rmi.c +11 -0
+/*
+ * For now we set the tracking_region_size to 0 for RMI_RMM_CONFIG_SET().
+ * TODO: Support other tracking sizes (via Kconfig option).
+ */
+#ifdef CONFIG_PAGE_SIZE_4KB
+#define RMM_GRANULE_TRACKING_SIZE	SZ_1G
+#elif defined(CONFIG_PAGE_SIZE_16KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_32M
+#elif defined(CONFIG_PAGE_SIZE_64KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_512M
+#endif

Basically, a level 2 mapping. Which means this whole block really is:

Quoted 1 line

#define RMM_GRANULE_TRAKING_SIZE (2 * PAGE_SHIFT - 3)

Obviously wrong:

#define RMM_GRANULE_TRAKING_SIZE BIT(2 * PAGE_SHIFT - 3)

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#45 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

In reply to Marc Zyngier
Quoted 79 lines · Marc Zyngier
Quoted 64 lines · Steven Price

The RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegated individual pages.

For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.

Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.

Note: support is currently missing for SROs which means that if the RMM needs memory donating this will fail (and render CCA unusable in Linux). This effectively means that the L1 GPT tables must be created before Linux starts.

---
Changes since v13:
* Moved out of KVM

---
  arch/arm64/include/asm/rmi_cmds.h |   2 +
  arch/arm64/kernel/rmi.c           | 103 ++++++++++++++++++++++++++++++
  2 files changed, 105 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +2 -0
index 9179934925c5..9078a2920a7c 100644
@@ -33,6 +33,8 @@ struct rmi_sro_state {
  } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
  	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+bool rmi_is_available(void);
+
  unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
  void rmi_sro_free(struct rmi_sro_state *sro);
 
arch/arm64/kernel/rmi.c +3 -0
index a14ead5dedda..52a415e99500 100644
@@ -7,6 +7,8 @@
 
  #include <asm/rmi_cmds.h>
 
+static bool arm64_rmi_is_available;
+
  unsigned long rmm_feat_reg0;
  unsigned long rmm_feat_reg1;
 
@@ -88,6 +90,102 @@ static int rmi_configure(void)
  	return 0;
  }
 
+/*

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 103

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+ * For now we set the tracking_region_size to 0 for RMI_RMM_CONFIG_SET().
+ * TODO: Support other tracking sizes (via Kconfig option).
+ */
+#ifdef CONFIG_PAGE_SIZE_4KB
+#define RMM_GRANULE_TRACKING_SIZE	SZ_1G
+#elif defined(CONFIG_PAGE_SIZE_16KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_32M
+#elif defined(CONFIG_PAGE_SIZE_64KB)
+#define RMM_GRANULE_TRACKING_SIZE	SZ_512M
+#endif

Basically, a level 2 mapping. Which means this whole block really is:

Quoted 3 lines

#define RMM_GRANULE_TRAKING_SIZE (2 * PAGE_SHIFT - 3)

(adjust for D128 as needed).

True,

Quoted 14 lines
Quoted 14 lines
arch/arm64/kernel/rmi.c +13 -0
+
+/*
+ * Make sure the area is tracked by RMM at FINE granularity.
+ * We do not support changing the tracking yet.
+ */
+static int rmi_verify_memory_tracking(phys_addr_t start, phys_addr_t end)
+{
+	while (start < end) {
+		unsigned long ret, category, state, next;
+
+		ret = rmi_granule_tracking_get(start, end, &category, &state, &next);
+		if (ret != RMI_SUCCESS ||
+		    state != RMI_TRACKING_FINE ||
Quoted 8 lines

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 121

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+		    category != RMI_MEM_CATEGORY_CONVENTIONAL) {
+			/* TODO: Set granule tracking in this case */
+			pr_err("Granule tracking for region isn't fine/conventional: %llx",
+			       start);
+			return -ENODEV;

How is this triggered? Do we really need to spam the console with this? A PA doesn't mean much, and there is no context (stack trace).

This could be triggered if the RMM doesn't have static carveout for tracking the DRAM granules. (state != RMI_TRACKING_FINE). This not worth WARN_ONCE(), we could simply not enable KVM. We plan to add support for donating memory to the RMM in the future. (Primarily we don't yet have an RMM implementation that does dynamic management via SRO. This can be added later as a separate series)

Quoted 36 lines

If that's not expected, turn this into a WARN_ONCE().

Quoted 29 lines
arch/arm64/kernel/rmi.c +29 -0
+		}
+		start = next;
+	}
+
+	return 0;
+}
+
+static unsigned long rmi_l0gpt_size(void)
+{
+	return 1UL << (30 + FIELD_GET(RMI_FEATURE_REGISTER_1_L0GPTSZ,
+				      rmm_feat_reg1));
+}
+
+static int rmi_create_gpts(phys_addr_t start, phys_addr_t end)
+{
+	unsigned long l0gpt_sz = rmi_l0gpt_size();
+
+	start = ALIGN_DOWN(start, l0gpt_sz);
+	end = ALIGN(end, l0gpt_sz);
+
+	while (start < end) {
+		int ret = rmi_gpt_l1_create(start);
+
+		/*
+		 * Make sure the L1 GPT tables are created for the region.
+		 * RMI_ERROR_GPT indicates the L1 table already exists.
+		 */
+		if (ret && ret != RMI_ERROR_GPT) {
+			/*
Quoted 9 lines

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 155

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+			 * FIXME: Handle SRO so that memory can be donated for
+			 * the tables.
+			 */
+			pr_err("GPT Level1 table missing for %llx\n", start);
+			return -ENOMEM;

If any of this fails, where is the cleanup done? Is that part of the missing SRO support that's indicated in the commit message?

For now, there is no cleanup required. What we essentially do here is making sure that the GPT tables have been created upto L1 (i.e., by checking ret == RMI_ERROR_GPT).

We do not donate any memory now, but only support RMMs with static memory carved out for L1 GPT. Support for dynamic RMMs could be added as a separate series, at which point, we could defer the table creation to
the actual use case (e.g, RMI_GRANULE_DELEGATE).

Clean up would be required when we donate memory to the RMM.

Quoted 18 lines
Quoted 18 lines
arch/arm64/kernel/rmi.c +18 -0
+		}
+		start += l0gpt_sz;
+	}
+
+	return 0;
+}
+
+static int rmi_init_metadata(void)
+{
+	phys_addr_t start, end;
+	const struct memblock_region *r;
+
+	for_each_mem_region(r) {
+		int ret;
+
+		start = memblock_region_memory_base_pfn(r) << PAGE_SHIFT;
+		end = memblock_region_memory_end_pfn(r) << PAGE_SHIFT;
+		ret = rmi_verify_memory_tracking(start, end);
Quoted 8 lines

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 179

Quoted 6 lines
arch/arm64/kernel/rmi.c +6 -0
+		if (ret)
+			return ret;
+		ret = rmi_create_gpts(start, end);
+		if (ret)
+			return ret;
+	}

How does this work with, say, memory hotplug?

Good point, we need a hook for hotpug to make sure this is taken care of. As mentioned above, when we add support for RMM with support for dynamic Tracking/GPT with SRO, this could be deferred to the actual use (handling RMI return codes, RMI_ERROR_TRACKING/RMI_ERROR_GPT)

Suzuki

Quoted 30 lines
Quoted 15 lines
arch/arm64/kernel/rmi.c +9 -0
+
+	return 0;
+}
+
+bool rmi_is_available(void)
+{
+	return arm64_rmi_is_available;
+}
+
  static int __init arm64_init_rmi(void)
  {
  	/* Continue without realm support if we can't agree on a version */
@@ -101,6 +199,11 @@ static int __init arm64_init_rmi(void)
 

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 209

Quoted 10 lines
arch/arm64/kernel/rmi.c +5 -0
  	if (rmi_configure())
  		return 0;
+	if (rmi_init_metadata())
+		return 0;
+
+	arm64_rmi_is_available = true;
+	pr_info("RMI configured");
 
  	return 0;
  }

Thanks,

	M.
#46 lore raw

Re: [PATCH v14 08/44] arm64: RMI: Ensure that the RMM has GPT entries for memory

Quoted 76 lines · Suzuki K Poulose
Quoted 75 lines · Marc Zyngier
Quoted 63 lines · Steven Price

The RMM maintains the state of all the granules in the system to make sure that the host is abiding by the rules. This state can be maintained at different granularity, per page (TRACKING_FINE) or per region (TRACKING_COARSE). The region size depends on the underlying "RMI_GRANULE_SIZE". For a "coarse" region all pages in the region must be of the same state, this implies we need to have "fine" tracking for DRAM, so that we can delegated individual pages.

For now we only support a statically carved out memory for tracking granules for the "fine" regions. This can be extended in the future to allow modifying the tracking granularity and remove the need for a static allocation.

Similarly, the firmware may create L0 GPT entries describing the total address space. But if we change the "PAS" (Physical Address Space) of a granule then the firmware may need to create L1 tables to track the PAS at a finer granularity.

Note: support is currently missing for SROs which means that if the RMM needs memory donating this will fail (and render CCA unusable in Linux). This effectively means that the L1 GPT tables must be created before Linux starts.

---
Changes since v13:
  * Moved out of KVM
---
  arch/arm64/include/asm/rmi_cmds.h |   2 + arch/arm64/kernel/rmi.c           | 103 ++++++++++++++++++++++++++++++

  2 files changed, 105 insertions(+)
arch/arm64/include/ renamed from arch/arm64/include/asm/rmi_cmds.h +0 -0

asm/rmi_cmds.h
index 9179934925c5..9078a2920a7c 100644

arch/arm64/include/asm/rmi_cmds.h +1 -0
@@ -33,6 +33,8 @@ struct rmi_sro_state {
   } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||            \
        RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
   +bool rmi_is_available(void);
+
   unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
   void rmi_sro_free(struct rmi_sro_state *sro);

  diff --git a/arch/arm64/kernel/rmi.c b/arch/arm64/kernel/rmi.c index a14ead5dedda..52a415e99500 100644

arch/arm64/kernel/rmi.c +1 -0
@@ -7,6 +7,8 @@
     #include <asm/rmi_cmds.h>
   +static bool arm64_rmi_is_available;
+
   unsigned long rmm_feat_reg0;
   unsigned long rmm_feat_reg1;
   @@ -88,6 +90,102 @@ static int rmi_configure(void)
       return 0;

  }
  +/*
+ * For now we set the tracking_region_size to 0 for RMI_RMM_CONFIG_SET().
+ * TODO: Support other tracking sizes (via Kconfig option).
+ */

arch/arm64/kernel/rmi.c static int rmi_configure(void) · line 103

Quoted 7 lines
arch/arm64/kernel/rmi.c +7 -0
+#ifdef CONFIG_PAGE_SIZE_4KB
+#define RMM_GRANULE_TRACKING_SIZE    SZ_1G
+#elif defined(CONFIG_PAGE_SIZE_16KB)
+#define RMM_GRANULE_TRACKING_SIZE    SZ_32M
+#elif defined(CONFIG_PAGE_SIZE_64KB)
+#define RMM_GRANULE_TRACKING_SIZE    SZ_512M
+#endif

Basically, a level 2 mapping. Which means this whole block really is:

Quoted 5 lines
Quoted 3 lines

#define RMM_GRANULE_TRAKING_SIZE    (2 * PAGE_SHIFT - 3)

(adjust for D128 as needed).

True,

As Gavin pointed out we actually don't need this anymore because of the move to a range based API.

It's also not quite that simple because for 4K PAGE_SIZED the RMM doesn't support 2MB (which would be the level 2 size), instead jumping to 1GB. And if we add a Kconfig option in the future then this could change because of that.

For now I'll just delete this block since it's unused.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
arch/arm64/kernel/rmi.c +6 -0
+
+/*
+ * Make sure the area is tracked by RMM at FINE granularity.
+ * We do not support changing the tracking yet.
+ */
+static int rmi_verify_memory_tracking(phys_addr_t start, phys_addr_t

end)

arch/arm64/kernel/rmi.c +5 -0
+{
+    while (start < end) {
+        unsigned long ret, category, state, next;
+
+        ret = rmi_granule_tracking_get(start, end, &category,

&state, &next);
+        if (ret != RMI_SUCCESS ||
+            state != RMI_TRACKING_FINE ||
+            category != RMI_MEM_CATEGORY_CONVENTIONAL) {

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

+            /* TODO: Set granule tracking in this case */ +            pr_err("Granule tracking for region isn't fine/
conventional: %llx",
+                   start);
+            return -ENODEV;

How is this triggered? Do we really need to spam the console with this? A PA doesn't mean much, and there is no context (stack trace).

I'm not sure 1 message really counts as 'spam' - it provides the information on why the RMI interface (and therefore realm guests) is unavailable. The PA might help track down whether this physical region was intended to be given to Linux.

Quoted 7 lines

This could be triggered if the RMM doesn't have static carveout for tracking the DRAM granules. (state != RMI_TRACKING_FINE). This not worth WARN_ONCE(), we could simply not enable KVM. We plan to add support for donating memory to the RMM in the future. (Primarily we don't yet have an RMM implementation that does dynamic management via SRO. This can be added later as a separate series)

As Suzuki says - this case should be handled in the future - so it's a limitation in the current implementation. So a WARN_ONCE is a bit strong
- it's not a "can never happen" situation - it's a "Linux doesn't support this (yet)".

Quoted 50 lines
Quoted 36 lines

If that's not expected, turn this into a WARN_ONCE().

Quoted 29 lines

+        }
+        start = next;
+    }

arch/arm64/kernel/rmi.c +26 -0
+
+    return 0;
+}
+
+static unsigned long rmi_l0gpt_size(void)
+{
+    return 1UL << (30 + FIELD_GET(RMI_FEATURE_REGISTER_1_L0GPTSZ,
+                      rmm_feat_reg1));
+}
+
+static int rmi_create_gpts(phys_addr_t start, phys_addr_t end)
+{
+    unsigned long l0gpt_sz = rmi_l0gpt_size();
+
+    start = ALIGN_DOWN(start, l0gpt_sz);
+    end = ALIGN(end, l0gpt_sz);
+
+    while (start < end) {
+        int ret = rmi_gpt_l1_create(start);
+
+        /*
+         * Make sure the L1 GPT tables are created for the region.
+         * RMI_ERROR_GPT indicates the L1 table already exists.
+         */
+        if (ret && ret != RMI_ERROR_GPT) {
+            /*
Quoted 9 lines
Quoted 5 lines

+             * FIXME: Handle SRO so that memory can be donated for +             * the tables.
+             */
+            pr_err("GPT Level1 table missing for %llx\n", start); +            return -ENOMEM;

If any of this fails, where is the cleanup done? Is that part of the missing SRO support that's indicated in the commit message?

For now, there is no cleanup required. What we essentially do here is making sure that the GPT tables have been created upto L1 (i.e., by checking ret == RMI_ERROR_GPT).

Quoted 6 lines

We do not donate any memory now, but only support RMMs with static memory carved out for L1 GPT. Support for dynamic RMMs could be added as a separate series, at which point, we could defer the table creation to the actual use case (e.g, RMI_GRANULE_DELEGATE).

Clean up would be required when we donate memory to the RMM.

The missing SRO support is why we're not donating memory - with that missing the clean up is unnecessary as Suzuki says.

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines

+        }
+        start += l0gpt_sz;
+    }

arch/arm64/kernel/rmi.c +15 -0
+
+    return 0;
+}
+
+static int rmi_init_metadata(void)
+{
+    phys_addr_t start, end;
+    const struct memblock_region *r;
+
+    for_each_mem_region(r) {
+        int ret;
+
+        start = memblock_region_memory_base_pfn(r) << PAGE_SHIFT;
+        end = memblock_region_memory_end_pfn(r) << PAGE_SHIFT;
+        ret = rmi_verify_memory_tracking(start, end);
Quoted 8 lines
Quoted 6 lines

+        if (ret)
+            return ret;
+        ret = rmi_create_gpts(start, end);
+        if (ret)
+            return ret;
+    }

How does this work with, say, memory hotplug?

Quoted 4 lines

Good point, we need a hook for hotpug to make sure this is taken care of. As mentioned above, when we add support for RMM with support for dynamic Tracking/GPT with SRO, this could be deferred to the actual use (handling RMI return codes, RMI_ERROR_TRACKING/RMI_ERROR_GPT)

Yep, that was an oversight - we definitely will need to handle hotplug.

Thanks,
Steve

Quoted 33 lines

Suzuki

Quoted 29 lines
Quoted 15 lines
arch/arm64/kernel/rmi.c +9 -0
+
+    return 0;
+}
+
+bool rmi_is_available(void)
+{
+    return arm64_rmi_is_available;
+}
+

  static int __init arm64_init_rmi(void)
  {
      /* Continue without realm support if we can't agree on a version */

arch/arm64/kernel/rmi.c static int __init arm64_init_rmi(void) · line 207

Quoted 9 lines
arch/arm64/kernel/rmi.c +5 -0
@@ -101,6 +199,11 @@ static int __init arm64_init_rmi(void)
         if (rmi_configure())
           return 0;
+    if (rmi_init_metadata())
+        return 0;
+
+    arm64_rmi_is_available = true;
+    pr_info("RMI configured");
         return 0;
   }

Thanks,

    M.

#47 lore raw

PATCH v14 9/44 arm64: RMI: Provide functions to delegate/undelegate ranges of memory

In reply to Steven Price

The RMM requires memory is 'delegated' to it so that it can be used either for a realm guest or for various tracking purposes within the RMM (e.g. for metadata or page tables). Memory that has been delegated cannot be accessed by the host (it will result in a Granule Protection Fault).

Undelegation may fail if the memory is still in use by the RMM. This shouldn't happen (Linux should ensure it has destroyed the RMM objects before attempting to undelegate). In the event that it does happen this points to a programming bug and the only reasonable approach is for the physical pages to be leaked - it is up to the caller of rmi_undelegate_range() to handle this.

---
v14:
* Split into separate patch and moved out of KVM

---
 arch/arm64/include/asm/rmi_cmds.h | 13 +++++++++++
 arch/arm64/kernel/rmi.c           | 36 +++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +13 -0
index 9078a2920a7c..eb213c8e6f26 100644
@@ -33,6 +33,19 @@ struct rmi_sro_state {
 } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
 	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size);
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+
+static inline int rmi_delegate_page(phys_addr_t phys)
+{
+	return rmi_delegate_range(phys, PAGE_SIZE);
+}
+
+static inline int rmi_undelegate_page(phys_addr_t phys)
+{
+	return rmi_undelegate_range(phys, PAGE_SIZE);
+}
+
 bool rmi_is_available(void);
 
 unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
arch/arm64/kernel/rmi.c +36 -0
index 52a415e99500..08cef54acadb 100644
@@ -12,6 +12,42 @@ static bool arm64_rmi_is_available;
 unsigned long rmm_feat_reg0;
 unsigned long rmm_feat_reg1;
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	while (phys < top) {
+		ret = rmi_granule_range_delegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;
+		else if (ret != RMI_BUSY && ret != RMI_BLOCKED)
+			return ret;
+	}
+
+	return ret;
+}
+
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	WARN_ON(size == 0);
+
+	while (phys < top) {
+		ret = rmi_granule_range_undelegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;
+		else if (ret != RMI_BUSY && ret != RMI_BLOCKED)
+			return ret;
+	}
+
+	return ret;
+}
+
 static int rmi_check_version(void)
 {
 	struct arm_smccc_res res;
Signature · 1 line
2.43.0
maintainer
#48 lore raw

Re: [PATCH v14 09/44] arm64: RMI: Provide functions to delegate/undelegate ranges of memory

Quoted 69 lines · Steven Price

The RMM requires memory is 'delegated' to it so that it can be used either for a realm guest or for various tracking purposes within the RMM (e.g. for metadata or page tables). Memory that has been delegated cannot be accessed by the host (it will result in a Granule Protection Fault).

Undelegation may fail if the memory is still in use by the RMM. This shouldn't happen (Linux should ensure it has destroyed the RMM objects before attempting to undelegate). In the event that it does happen this points to a programming bug and the only reasonable approach is for the physical pages to be leaked - it is up to the caller of rmi_undelegate_range() to handle this.

---
v14:
* Split into separate patch and moved out of KVM

---
 arch/arm64/include/asm/rmi_cmds.h | 13 +++++++++++
 arch/arm64/kernel/rmi.c           | 36 +++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +13 -0
index 9078a2920a7c..eb213c8e6f26 100644
@@ -33,6 +33,19 @@ struct rmi_sro_state {
 } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
 	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size);
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+
+static inline int rmi_delegate_page(phys_addr_t phys)
+{
+	return rmi_delegate_range(phys, PAGE_SIZE);
+}
+
+static inline int rmi_undelegate_page(phys_addr_t phys)
+{
+	return rmi_undelegate_range(phys, PAGE_SIZE);
+}
+
 bool rmi_is_available(void);
 
 unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
arch/arm64/kernel/rmi.c +14 -0
index 52a415e99500..08cef54acadb 100644
@@ -12,6 +12,42 @@ static bool arm64_rmi_is_available;
 unsigned long rmm_feat_reg0;
 unsigned long rmm_feat_reg1;
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	while (phys < top) {
+		ret = rmi_granule_range_delegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;
+		else if (ret != RMI_BUSY && ret != RMI_BLOCKED)
+			return ret;
+	}
+

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 38

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+	return ret;
+}
+
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	WARN_ON(size == 0);

I find it odd to warn on size = 0. After all, free(NULL) is not an error. But even then, you continue feeding this to the RMM.

You also don't seem to be bothered with that on the delegation side...

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 43

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+
+	while (phys < top) {
+		ret = rmi_granule_range_undelegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;

and size==0 doesn't violate any of the failure conditions listed in B4.5.18.2 (beta2). Will you end-up looping around forever?

Same questions for the delegation, obviously.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#49 lore raw

Re: [PATCH v14 09/44] arm64: RMI: Provide functions to delegate/undelegate ranges of memory

Quoted 87 lines · Marc Zyngier
Quoted 69 lines · Steven Price

The RMM requires memory is 'delegated' to it so that it can be used either for a realm guest or for various tracking purposes within the RMM (e.g. for metadata or page tables). Memory that has been delegated cannot be accessed by the host (it will result in a Granule Protection Fault).

Undelegation may fail if the memory is still in use by the RMM. This shouldn't happen (Linux should ensure it has destroyed the RMM objects before attempting to undelegate). In the event that it does happen this points to a programming bug and the only reasonable approach is for the physical pages to be leaked - it is up to the caller of rmi_undelegate_range() to handle this.

---
v14:
* Split into separate patch and moved out of KVM

---
  arch/arm64/include/asm/rmi_cmds.h | 13 +++++++++++
  arch/arm64/kernel/rmi.c           | 36 +++++++++++++++++++++++++++++++
  2 files changed, 49 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +13 -0
index 9078a2920a7c..eb213c8e6f26 100644
@@ -33,6 +33,19 @@ struct rmi_sro_state {
  } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
  	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size);
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+
+static inline int rmi_delegate_page(phys_addr_t phys)
+{
+	return rmi_delegate_range(phys, PAGE_SIZE);
+}
+
+static inline int rmi_undelegate_page(phys_addr_t phys)
+{
+	return rmi_undelegate_range(phys, PAGE_SIZE);
+}
+
  bool rmi_is_available(void);
 
  unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
arch/arm64/kernel/rmi.c +14 -0
index 52a415e99500..08cef54acadb 100644
@@ -12,6 +12,42 @@ static bool arm64_rmi_is_available;
  unsigned long rmm_feat_reg0;
  unsigned long rmm_feat_reg1;
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	while (phys < top) {
+		ret = rmi_granule_range_delegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;
+		else if (ret != RMI_BUSY && ret != RMI_BLOCKED)
+			return ret;
+	}
+

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 38

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+	return ret;
+}
+
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	WARN_ON(size == 0);

I find it odd to warn on size = 0. After all, free(NULL) is not an error. But even then, you continue feeding this to the RMM.

You also don't seem to be bothered with that on the delegation side...

Quoted 8 lines

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 43

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+
+	while (phys < top) {
+		ret = rmi_granule_range_undelegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;

and size==0 doesn't violate any of the failure conditions listed in B4.5.18.2 (beta2). Will you end-up looping around forever?

That is not true ? It triggers, top_bound error condition, for both.

pre: UInt(top) <= UInt(base)
post: result.status == RMI_ERROR_INPUT

Suzuki

Quoted 5 lines

Same questions for the delegation, obviously.

	M.
maintainer
#50 lore raw

Re: [PATCH v14 09/44] arm64: RMI: Provide functions to delegate/undelegate ranges of memory

Quoted 96 lines · Suzuki K Poulose
Quoted 85 lines · Marc Zyngier
Quoted 67 lines · Steven Price

The RMM requires memory is 'delegated' to it so that it can be used either for a realm guest or for various tracking purposes within the RMM (e.g. for metadata or page tables). Memory that has been delegated cannot be accessed by the host (it will result in a Granule Protection Fault).

Undelegation may fail if the memory is still in use by the RMM. This shouldn't happen (Linux should ensure it has destroyed the RMM objects before attempting to undelegate). In the event that it does happen this points to a programming bug and the only reasonable approach is for the physical pages to be leaked - it is up to the caller of rmi_undelegate_range() to handle this.

---
v14:
* Split into separate patch and moved out of KVM

---
  arch/arm64/include/asm/rmi_cmds.h | 13 +++++++++++
  arch/arm64/kernel/rmi.c           | 36 +++++++++++++++++++++++++++++++
  2 files changed, 49 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +12 -0
index 9078a2920a7c..eb213c8e6f26 100644
@@ -33,6 +33,19 @@ struct rmi_sro_state {
  } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
  	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
  +int rmi_delegate_range(phys_addr_t phys, unsigned long size);
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+
+static inline int rmi_delegate_page(phys_addr_t phys)
+{
+	return rmi_delegate_range(phys, PAGE_SIZE);
+}
+
+static inline int rmi_undelegate_page(phys_addr_t phys)
+{
+	return rmi_undelegate_range(phys, PAGE_SIZE);
+}
+
  bool rmi_is_available(void);
    unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t
 gfp);
arch/arm64/kernel/rmi.c +13 -0
index 52a415e99500..08cef54acadb 100644
@@ -12,6 +12,42 @@ static bool arm64_rmi_is_available;
  unsigned long rmm_feat_reg0;
  unsigned long rmm_feat_reg1;
  +int rmi_delegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	while (phys < top) {
+		ret = rmi_granule_range_delegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;
+		else if (ret != RMI_BUSY && ret != RMI_BLOCKED)
+			return ret;
+	}
+

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 37

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+	return ret;
+}
+
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	WARN_ON(size == 0);

I find it odd to warn on size = 0. After all, free(NULL) is not an error. But even then, you continue feeding this to the RMM.

You also don't seem to be bothered with that on the delegation side...

Quoted 8 lines

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 43

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+
+	while (phys < top) {
+		ret = rmi_granule_range_undelegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;

and size==0 doesn't violate any of the failure conditions listed in B4.5.18.2 (beta2). Will you end-up looping around forever?

Quoted 5 lines

That is not true ? It triggers, top_bound error condition, for both.

pre: UInt(top) <= UInt(base)
post: result.status == RMI_ERROR_INPUT

News flash, I can't read. Ignore me.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#51 lore raw

Re: [PATCH v14 09/44] arm64: RMI: Provide functions to delegate/undelegate ranges of memory

In reply to Marc Zyngier
Quoted 76 lines · Marc Zyngier
Quoted 74 lines · Steven Price

The RMM requires memory is 'delegated' to it so that it can be used either for a realm guest or for various tracking purposes within the RMM (e.g. for metadata or page tables). Memory that has been delegated cannot be accessed by the host (it will result in a Granule Protection Fault).

Undelegation may fail if the memory is still in use by the RMM. This shouldn't happen (Linux should ensure it has destroyed the RMM objects before attempting to undelegate). In the event that it does happen this points to a programming bug and the only reasonable approach is for the physical pages to be leaked - it is up to the caller of rmi_undelegate_range() to handle this.

---
v14:
* Split into separate patch and moved out of KVM

---
 arch/arm64/include/asm/rmi_cmds.h | 13 +++++++++++
 arch/arm64/kernel/rmi.c           | 36 +++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +13 -0
index 9078a2920a7c..eb213c8e6f26 100644
@@ -33,6 +33,19 @@ struct rmi_sro_state {
 } while (RMI_RETURN_STATUS(res.a0) == RMI_BUSY ||			\
 	 RMI_RETURN_STATUS(res.a0) == RMI_BLOCKED)
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size);
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+
+static inline int rmi_delegate_page(phys_addr_t phys)
+{
+	return rmi_delegate_range(phys, PAGE_SIZE);
+}
+
+static inline int rmi_undelegate_page(phys_addr_t phys)
+{
+	return rmi_undelegate_range(phys, PAGE_SIZE);
+}
+
 bool rmi_is_available(void);
 
 unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp);
arch/arm64/kernel/rmi.c +19 -0
index 52a415e99500..08cef54acadb 100644
@@ -12,6 +12,42 @@ static bool arm64_rmi_is_available;
 unsigned long rmm_feat_reg0;
 unsigned long rmm_feat_reg1;
 
+int rmi_delegate_range(phys_addr_t phys, unsigned long size)
+{
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	while (phys < top) {
+		ret = rmi_granule_range_delegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;
+		else if (ret != RMI_BUSY && ret != RMI_BLOCKED)
+			return ret;
+	}
+
+	return ret;
+}
+
+int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
+{
Quoted 8 lines

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 38

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+	unsigned long ret = 0;
+	unsigned long top = phys + size;
+	unsigned long out_top;
+
+	WARN_ON(size == 0);

I find it odd to warn on size = 0. After all, free(NULL) is not an error. But even then, you continue feeding this to the RMM.

Ok, I'll admit that this is left over debugging - although this is a condition that shouldn't happen.

Note that the while() condition prevents this from actually getting to the RMM.

I'll drop the WARN_ON() since it's confusing.

Thanks,
Steve

Quoted 15 lines

You also don't seem to be bothered with that on the delegation side...

arch/arm64/kernel/rmi.c static bool arm64_rmi_is_available; · line 43

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+
+	while (phys < top) {
+		ret = rmi_granule_range_undelegate(phys, top, &out_top);
+		if (ret == RMI_SUCCESS)
+			phys = out_top;

and size==0 doesn't violate any of the failure conditions listed in B4.5.18.2 (beta2). Will you end-up looping around forever?

Same questions for the delegation, obviously.

	M.
#52 lore raw

PATCH v14 10/44 arm64: RMI: Add support for SRO

In reply to Steven Price

RMM v2.0 introduces the concept of "Stateful RMI Operations" (SRO). This means that an SMC can return with an operation still in progress. The host is excepted to continue the operation until is reaches a conclusion (either success or failure). During this process the RMM can request additional memory ('donate') or hand memory back to the host ('reclaim'). The host can request an in progress operation is cancelled, but still continue the operation until it has completed (otherwise the incomplete operation may cause future RMM operations to fail).

The SRO is tracked using a struct rmi_sro_state object which keeps track of any memory which has been allocated but not yet consumed by the RMM or reclaimed from the RMM. This allows the memory to be reused in a future request within the same operation. It will also permit an operation to be done in a context where memory allocation may be difficult (e.g. atomic context) with the option to abort the operation and retry the memory allocation outside of the atomic context. The memory stored in the struct rmi_sro_state object can then be reused on the subsequent attempt.

---
v14:
* SRO support has improved although is still not fully complete. The infrastructure has been moved out of KVM.

---
 arch/arm64/include/asm/rmi_cmds.h |   1 +
 arch/arm64/kernel/rmi.c           | 359 ++++++++++++++++++++++++++++++
 2 files changed, 360 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +1 -0
index eb213c8e6f26..1a7b0c8f1e38 100644
@@ -35,6 +35,7 @@ struct rmi_sro_state {
 
 int rmi_delegate_range(phys_addr_t phys, unsigned long size);
 int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+int free_delegated_page(phys_addr_t phys);
 
 static inline int rmi_delegate_page(phys_addr_t phys)
 {
arch/arm64/kernel/rmi.c +359 -0

365 lines not on this page · raw message

Signature · 1 line
2.43.0
#53 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

Quoted 43 lines · Steven Price
arch/arm64/kernel/rmi.c +43 -0
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
+{
+	unsigned long sro_handle;
+	struct arm_smccc_1_2_regs regs;
+	struct arm_smccc_1_2_regs *regs_in = &sro->regs;
+
+	rmi_smccc_invoke(regs_in, &regs);
+
+	sro_handle = regs.a1;
+
+	while (RMI_RETURN_STATUS(regs.a0) == RMI_INCOMPLETE) {
+		bool can_cancel = RMI_RETURN_CAN_CANCEL(regs.a0);
+		int ret;
+
+		switch (RMI_RETURN_MEMREQ(regs.a0)) {
+		case RMI_OP_MEM_REQ_NONE:
+			regs = (struct arm_smccc_1_2_regs){
+				SMC_RMI_OP_CONTINUE, sro_handle, 0
+			};
+			rmi_smccc_invoke(&regs, &regs);
+			break;
+		case RMI_OP_MEM_REQ_DONATE:
+			ret = rmi_sro_donate(sro, sro_handle, regs.a2, &regs,
+					     gfp);
+			break;
+		case RMI_OP_MEM_REQ_RECLAIM:
+			ret = rmi_sro_reclaim(sro, sro_handle, &regs);
+			break;
+		default:
+			ret = WARN_ON(1);
+			break;
+		}
+
+		if (ret) {
+			if (can_cancel) {
+				/*
+				 * FIXME: Handle cancelling properly!
+				 *
+				 * If the operation has failed due to memory
+				 * allocation failure then the information on
+				 * the memory allocation should be saved, so
+				 * that the allocation can be repeated outside
+				 * of any context which prevented the

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 408

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+				 * allocation.
+				 */
+			}
+			if (WARN_ON(ret))
+				return ret;
+		}
+	}
+
+	return regs.a0;
+}

Can you also add support to return x1,x2 etc

This would help things like

static int rmi_rtt_dev_unmap(unsigned long rd_phys,
unsigned long base, unsigned long top, unsigned long *out_ipa, unsigned long *out_desc, unsigned long *rmi_ret)

{
	unsigned long flags = RMI_ADDR_TYPE_SINGLE;
	struct rmi_sro_state *sro __free(sro) =
		rmi_sro_init(SMC_RMI_RTT_DEV_UNMAP, rd_phys, base, top, flags, NULL);
	if (!sro)
		return -ENOMEM;

	*rmi_ret = rmi_sro_execute(sro);
	if (*rmi_ret)
		return 0;

	*out_ipa = sro->regs.a1;
	*out_desc = sro->regs.a2;

	return 0;
}

-aneesh

#54 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

Quoted 49 lines · Aneesh Kumar K.V
Quoted 47 lines · Steven Price
(quoted diff) +47 -0
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
+{
+	unsigned long sro_handle;
+	struct arm_smccc_1_2_regs regs;
+	struct arm_smccc_1_2_regs *regs_in = &sro->regs;
+
+	rmi_smccc_invoke(regs_in, &regs);
+
+	sro_handle = regs.a1;
+
+	while (RMI_RETURN_STATUS(regs.a0) == RMI_INCOMPLETE) {
+		bool can_cancel = RMI_RETURN_CAN_CANCEL(regs.a0);
+		int ret;
+
+		switch (RMI_RETURN_MEMREQ(regs.a0)) {
+		case RMI_OP_MEM_REQ_NONE:
+			regs = (struct arm_smccc_1_2_regs){
+				SMC_RMI_OP_CONTINUE, sro_handle, 0
+			};
+			rmi_smccc_invoke(&regs, &regs);
+			break;
+		case RMI_OP_MEM_REQ_DONATE:
+			ret = rmi_sro_donate(sro, sro_handle, regs.a2, &regs,
+					     gfp);
+			break;
+		case RMI_OP_MEM_REQ_RECLAIM:
+			ret = rmi_sro_reclaim(sro, sro_handle, &regs);
+			break;
+		default:
+			ret = WARN_ON(1);
+			break;
+		}
+
+		if (ret) {
+			if (can_cancel) {
+				/*
+				 * FIXME: Handle cancelling properly!
+				 *
+				 * If the operation has failed due to memory
+				 * allocation failure then the information on
+				 * the memory allocation should be saved, so
+				 * that the allocation can be repeated outside
+				 * of any context which prevented the
+				 * allocation.
+				 */
+			}
+			if (WARN_ON(ret))
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+				return ret;
+		}
+	}
+
+	return regs.a0;
+}

Can you also add support to return x1,x2 etc

Indeed that's going to be needed. Looking at this function again I don't think we actually need the on-stack 'regs' any more. So the below (very lightly tested) diff would use the regs from sro which also means they will be there for the caller if it needs them.

Thanks,
Steve

---8<---

arch/arm64/kernel/rmi.c +11 -12
index a8107ca9bb6d..58a0216be409 100644
@@ -356,30 +356,29 @@ void rmi_sro_free(struct rmi_sro_state *sro)
 unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
 {
 	unsigned long sro_handle;
-	struct arm_smccc_1_2_regs regs;
-	struct arm_smccc_1_2_regs *regs_in = &sro->regs;
+	struct arm_smccc_1_2_regs *regs = &sro->regs;
 
-	rmi_smccc_invoke(regs_in, &regs);
+	rmi_smccc_invoke(regs, regs);
 
-	sro_handle = regs.a1;
+	sro_handle = regs->a1;
 
-	while (RMI_RETURN_STATUS(regs.a0) == RMI_INCOMPLETE) {
-		bool can_cancel = RMI_RETURN_CAN_CANCEL(regs.a0);
+	while (RMI_RETURN_STATUS(regs->a0) == RMI_INCOMPLETE) {
+		bool can_cancel = RMI_RETURN_CAN_CANCEL(regs->a0);
 		int ret;
 
-		switch (RMI_RETURN_MEMREQ(regs.a0)) {
+		switch (RMI_RETURN_MEMREQ(regs->a0)) {
 		case RMI_OP_MEM_REQ_NONE:
-			regs = (struct arm_smccc_1_2_regs){
+			*regs = (struct arm_smccc_1_2_regs){
 				SMC_RMI_OP_CONTINUE, sro_handle, 0
 			};
-			rmi_smccc_invoke(&regs, &regs);
+			rmi_smccc_invoke(regs, regs);
 			break;
 		case RMI_OP_MEM_REQ_DONATE:
-			ret = rmi_sro_donate(sro, sro_handle, regs.a2, &regs,
+			ret = rmi_sro_donate(sro, sro_handle, regs->a2, regs,
 					     gfp);
 			break;
 		case RMI_OP_MEM_REQ_RECLAIM:
-			ret = rmi_sro_reclaim(sro, sro_handle, &regs);
+			ret = rmi_sro_reclaim(sro, sro_handle, regs);
 			break;
 		default:
 			ret = WARN_ON(1);
@@ -404,7 +403,7 @@ unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
 		}
 	}
 
-	return regs.a0;
+	return regs->a0;
 }
 
 static int rmi_check_version(void)
#55 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

In reply to Steven Price
Quoted 57 lines · Steven Price
arch/arm64/kernel/rmi.c +55 -0
+static unsigned long donate_req_to_size(unsigned long donatereq)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+
+	switch (unit_size) {
+	case 0:
+		return PAGE_SIZE;
+	case 1:
+		return PMD_SIZE;
+	case 2:
+		return PUD_SIZE;
+	case 3:
+		return P4D_SIZE;
+	}
+	unreachable();
+}
 
+
+static void rmi_smccc_invoke(struct arm_smccc_1_2_regs *regs_in,
+			     struct arm_smccc_1_2_regs *regs_out)
+{
+	struct arm_smccc_1_2_regs regs = *regs_in;
+	unsigned long status;
+
+	do {
+		arm_smccc_1_2_invoke(&regs, regs_out);
+		status = RMI_RETURN_STATUS(regs_out->a0);
+	} while (status == RMI_BUSY || status == RMI_BLOCKED);
+}
+
+int free_delegated_page(phys_addr_t phys)
+{
+	if (WARN_ON(rmi_undelegate_page(phys))) {
+		/* Undelegate failed: leak the page */
+		return -EBUSY;
+	}
+
+	free_page((unsigned long)phys_to_virt(phys));
+
+	return 0;
+}
+
+static int rmi_sro_ensure_capacity(struct rmi_sro_state *sro,
+				   unsigned long count)
+{
+	if (WARN_ON_ONCE(sro->addr_count > RMI_MAX_ADDR_LIST))
+		return -EOVERFLOW;
+
+	if (count > RMI_MAX_ADDR_LIST - sro->addr_count)
+		return -ENOSPC;
+
+	return 0;
+}
+
+static int rmi_sro_donate_contig(struct rmi_sro_state *sro,
+				 unsigned long sro_handle,

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 115

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+				 unsigned long donatereq,
+				 struct arm_smccc_1_2_regs *out_regs,
+				 gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long size = unit_size_bytes * count;
+	unsigned long addr_range;

Looking at above and the related code, I am wondering whether we should use u64 instead of unsigned long for everything that the specification defines as 64-bit.

-aneesh

#56 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

Quoted 65 lines · Aneesh Kumar K.V
Quoted 63 lines · Steven Price
arch/arm64/kernel/rmi.c +62 -0
+static unsigned long donate_req_to_size(unsigned long donatereq)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+
+	switch (unit_size) {
+	case 0:
+		return PAGE_SIZE;
+	case 1:
+		return PMD_SIZE;
+	case 2:
+		return PUD_SIZE;
+	case 3:
+		return P4D_SIZE;
+	}
+	unreachable();
+}
 
+
+static void rmi_smccc_invoke(struct arm_smccc_1_2_regs *regs_in,
+			     struct arm_smccc_1_2_regs *regs_out)
+{
+	struct arm_smccc_1_2_regs regs = *regs_in;
+	unsigned long status;
+
+	do {
+		arm_smccc_1_2_invoke(&regs, regs_out);
+		status = RMI_RETURN_STATUS(regs_out->a0);
+	} while (status == RMI_BUSY || status == RMI_BLOCKED);
+}
+
+int free_delegated_page(phys_addr_t phys)
+{
+	if (WARN_ON(rmi_undelegate_page(phys))) {
+		/* Undelegate failed: leak the page */
+		return -EBUSY;
+	}
+
+	free_page((unsigned long)phys_to_virt(phys));
+
+	return 0;
+}
+
+static int rmi_sro_ensure_capacity(struct rmi_sro_state *sro,
+				   unsigned long count)
+{
+	if (WARN_ON_ONCE(sro->addr_count > RMI_MAX_ADDR_LIST))
+		return -EOVERFLOW;
+
+	if (count > RMI_MAX_ADDR_LIST - sro->addr_count)
+		return -ENOSPC;
+
+	return 0;
+}
+
+static int rmi_sro_donate_contig(struct rmi_sro_state *sro,
+				 unsigned long sro_handle,
+				 unsigned long donatereq,
+				 struct arm_smccc_1_2_regs *out_regs,
+				 gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
Quoted 8 lines

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 115

Quoted 4 lines
arch/arm64/kernel/rmi.c +3 -0
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long size = unit_size_bytes * count;
+	unsigned long addr_range;

Looking at above and the related code, I am wondering whether we should use u64 instead of unsigned long for everything that the specification defines as 64-bit.

I'm split on this. The kernel makes use of "(unsigned) long" for a register sized value in quite a few places. Not least in struct arm_smccc_1_2_regs which is ultimately where most of the values are read from or written to.

I'm not a great fan of the kernel's approach to using long like this - there's a good argument that uintptr_t is more correct. Equally when we're in arch code for a 64 bit architecture (i.e. "arm64") then we know the size is u64.

The disadvantage here is that if I use u64 then there's a bunch of implicit conversions going on between unsigned long and u64 - which might come back to bite if anything changes. Hence my current view that "unsigned long" is the best option here in the kernel.

Anyone else have any view on the best type here?

Thanks,
Steve

#57 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

In reply to Steven Price

Hi Steven,

Quoted 56 lines · Steven Price

RMM v2.0 introduces the concept of "Stateful RMI Operations" (SRO). This means that an SMC can return with an operation still in progress. The host is excepted to continue the operation until is reaches a conclusion (either success or failure). During this process the RMM can request additional memory ('donate') or hand memory back to the host ('reclaim'). The host can request an in progress operation is cancelled, but still continue the operation until it has completed (otherwise the incomplete operation may cause future RMM operations to fail).

The SRO is tracked using a struct rmi_sro_state object which keeps track of any memory which has been allocated but not yet consumed by the RMM or reclaimed from the RMM. This allows the memory to be reused in a future request within the same operation. It will also permit an operation to be done in a context where memory allocation may be difficult (e.g. atomic context) with the option to abort the operation and retry the memory allocation outside of the atomic context. The memory stored in the struct rmi_sro_state object can then be reused on the subsequent attempt.

---
v14:
* SRO support has improved although is still not fully complete. The infrastructure has been moved out of KVM.

---
  arch/arm64/include/asm/rmi_cmds.h |   1 +
  arch/arm64/kernel/rmi.c           | 359 ++++++++++++++++++++++++++++++
  2 files changed, 360 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +1 -0
index eb213c8e6f26..1a7b0c8f1e38 100644
@@ -35,6 +35,7 @@ struct rmi_sro_state {
 
  int rmi_delegate_range(phys_addr_t phys, unsigned long size);
  int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+int free_delegated_page(phys_addr_t phys);
 
  static inline int rmi_delegate_page(phys_addr_t phys)
  {
arch/arm64/kernel/rmi.c +7 -0
index 08cef54acadb..a8107ca9bb6d 100644
@@ -48,6 +48,365 @@ int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
  	return ret;
  }
 
+static unsigned long donate_req_to_size(unsigned long donatereq)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+
+	switch (unit_size) {
+	case 0:
+		return PAGE_SIZE;

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 67

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+	case 1:
+		return PMD_SIZE;
+	case 2:
+		return PUD_SIZE;
+	case 3:
+		return P4D_SIZE;
+	}
+	unreachable();
+}
+

It's worthy to have 'inline'. {P4D, PUD, PMD}_SIZE can be equal if there are no P4D and PUD, depending on CONFIG_PGTABLE_LEVELS. In this case, can the 'unit_size' be translated to wrong value?

Quoted 69 lines
arch/arm64/kernel/rmi.c +69 -0
+static void rmi_smccc_invoke(struct arm_smccc_1_2_regs *regs_in,
+			     struct arm_smccc_1_2_regs *regs_out)
+{
+	struct arm_smccc_1_2_regs regs = *regs_in;
+	unsigned long status;
+
+	do {
+		arm_smccc_1_2_invoke(&regs, regs_out);
+		status = RMI_RETURN_STATUS(regs_out->a0);
+	} while (status == RMI_BUSY || status == RMI_BLOCKED);
+}
+
+int free_delegated_page(phys_addr_t phys)
+{
+	if (WARN_ON(rmi_undelegate_page(phys))) {
+		/* Undelegate failed: leak the page */
+		return -EBUSY;
+	}
+
+	free_page((unsigned long)phys_to_virt(phys));
+
+	return 0;
+}
+
+static int rmi_sro_ensure_capacity(struct rmi_sro_state *sro,
+				   unsigned long count)
+{
+	if (WARN_ON_ONCE(sro->addr_count > RMI_MAX_ADDR_LIST))
+		return -EOVERFLOW;
+
+	if (count > RMI_MAX_ADDR_LIST - sro->addr_count)
+		return -ENOSPC;
+
+	return 0;
+}
+
+static int rmi_sro_donate_contig(struct rmi_sro_state *sro,
+				 unsigned long sro_handle,
+				 unsigned long donatereq,
+				 struct arm_smccc_1_2_regs *out_regs,
+				 gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long size = unit_size_bytes * count;
+	unsigned long addr_range;
+	int ret;
+	void *virt;
+	phys_addr_t phys;
+	struct arm_smccc_1_2_regs regs = {
+		SMC_RMI_OP_MEM_DONATE,
+		sro_handle
+	};
+
+	for (int i = 0; i < sro->addr_count; i++) {
+		unsigned long entry = sro->addr_list[i];
+
+		if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+		    RMI_ADDR_RANGE_COUNT(entry) == count &&
+		    RMI_ADDR_RANGE_STATE(entry) == state) {
+			sro->addr_count--;
+			swap(sro->addr_list[sro->addr_count],
+			     sro->addr_list[i]);
+
+			goto out;
+		}
+	}

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 146

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	ret = rmi_sro_ensure_capacity(sro, 1);
+	if (ret)
+		return ret;
+
+	virt = alloc_pages_exact(size, gfp);
+	if (!virt)
+		return -ENOMEM;
+	phys = virt_to_phys(virt);
+

alloc_pages_exact() will fail if the requested size exceeds the maximal allowed size (1 << MAX_PAGE_ORDER). The maximal size is usually smaller than PUD_SIZE but PUD_SIZE is allowed by the RMM.

Quoted 220 lines
arch/arm64/kernel/rmi.c +220 -0
+	if (state == RMI_OP_MEM_DELEGATED) {
+		if (rmi_delegate_range(phys, size)) {
+			free_pages_exact(virt, size);
+			return -ENXIO;
+		}
+	}
+
+	addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+	FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
+	FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, count);
+	FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+	sro->addr_list[sro->addr_count] = addr_range;
+
+out:
+	regs.a2 = virt_to_phys(&sro->addr_list[sro->addr_count]);
+	regs.a3 = 1;
+	rmi_smccc_invoke(&regs, out_regs);
+
+	unsigned long donated_granules = out_regs->a1;
+	unsigned long donated_size = donated_granules << PAGE_SHIFT;
+
+	if (donated_granules == 0) {
+		/* No pages used by the RMM */
+		sro->addr_count++;
+	} else if (donated_size < size) {
+		phys = sro->addr_list[sro->addr_count] & RMI_ADDR_RANGE_ADDR_MASK;
+
+		/* Not all granules used by the RMM, free the remaining pages */
+		for (long i = donated_size; i < size; i += PAGE_SIZE) {
+			if (state == RMI_OP_MEM_DELEGATED)
+				free_delegated_page(phys + i);
+			else
+				__free_page(phys_to_page(phys + i));
+		}
+	}
+
+	return 0;
+}
+
+static int rmi_sro_donate_noncontig(struct rmi_sro_state *sro,
+				    unsigned long sro_handle,
+				    unsigned long donatereq,
+				    struct arm_smccc_1_2_regs *out_regs,
+				    gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long found = 0;
+	unsigned long addr_list_start = sro->addr_count;
+	int ret;
+	struct arm_smccc_1_2_regs regs = {
+		SMC_RMI_OP_MEM_DONATE,
+		sro_handle
+	};
+
+	for (int i = 0; i < addr_list_start && found < count; i++) {
+		unsigned long entry = sro->addr_list[i];
+
+		if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+		    RMI_ADDR_RANGE_COUNT(entry) == 1 &&
+		    RMI_ADDR_RANGE_STATE(entry) == state) {
+			addr_list_start--;
+			swap(sro->addr_list[addr_list_start],
+			     sro->addr_list[i]);
+			found++;
+			i--;
+		}
+	}
+
+	ret = rmi_sro_ensure_capacity(sro, count - found);
+	if (ret)
+		return ret;
+
+	while (found < count) {
+		unsigned long addr_range;
+		void *virt = alloc_pages_exact(unit_size_bytes, gfp);
+		phys_addr_t phys;
+
+		if (!virt)
+			return -ENOMEM;
+
+		phys = virt_to_phys(virt);
+
+		if (state == RMI_OP_MEM_DELEGATED) {
+			if (rmi_delegate_range(phys, unit_size_bytes)) {
+				free_pages_exact(virt, unit_size_bytes);
+				return -ENXIO;
+			}
+		}
+
+		addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+		FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
+		FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, 1);
+		FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+		sro->addr_list[sro->addr_count++] = addr_range;
+		found++;
+	}
+
+	regs.a2 = virt_to_phys(&sro->addr_list[addr_list_start]);
+	regs.a3 = found;
+	rmi_smccc_invoke(&regs, out_regs);
+
+	unsigned long donated_granules = out_regs->a1;
+
+	if (WARN_ON(donated_granules & ((unit_size_bytes >> PAGE_SHIFT) - 1))) {
+		/*
+		 * FIXME: RMM has only consumed part of a huge page, this leaks
+		 * the rest of the huge page
+		 */
+		donated_granules = ALIGN(donated_granules,
+					 (unit_size_bytes >> PAGE_SHIFT));
+	}
+	unsigned long donated_blocks = donated_granules / (unit_size_bytes >> PAGE_SHIFT);
+
+	if (WARN_ON(donated_blocks > found))
+		donated_blocks = found;
+
+	unsigned long undonated_blocks = found - donated_blocks;
+
+	while (donated_blocks && undonated_blocks) {
+		sro->addr_count--;
+		swap(sro->addr_list[addr_list_start],
+		     sro->addr_list[sro->addr_count]);
+		addr_list_start++;
+
+		donated_blocks--;
+		undonated_blocks--;
+	}
+	sro->addr_count -= donated_blocks;
+
+	return 0;
+}
+
+static int rmi_sro_donate(struct rmi_sro_state *sro,
+			  unsigned long sro_handle,
+			  unsigned long donatereq,
+			  struct arm_smccc_1_2_regs *regs,
+			  gfp_t gfp)
+{
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+
+	if (WARN_ON(!count))
+		return 0;
+
+	if (RMI_DONATE_CONTIG(donatereq)) {
+		return rmi_sro_donate_contig(sro, sro_handle, donatereq,
+					     regs, gfp);
+	} else {
+		return rmi_sro_donate_noncontig(sro, sro_handle, donatereq,
+						regs, gfp);
+	}
+}
+
+static int rmi_sro_reclaim(struct rmi_sro_state *sro,
+			   unsigned long sro_handle,
+			   struct arm_smccc_1_2_regs *out_regs)
+{
+	unsigned long capacity;
+	struct arm_smccc_1_2_regs regs;
+	int ret;
+
+	ret = rmi_sro_ensure_capacity(sro, 1);
+	if (ret)
+		rmi_sro_free(sro);
+
+	capacity = RMI_MAX_ADDR_LIST - sro->addr_count;
+
+	regs = (struct arm_smccc_1_2_regs){
+		SMC_RMI_OP_MEM_RECLAIM,
+		sro_handle,
+		virt_to_phys(&sro->addr_list[sro->addr_count]),
+		capacity
+	};
+	rmi_smccc_invoke(&regs, out_regs);
+
+	if (WARN_ON_ONCE(out_regs->a1 > capacity))
+		out_regs->a1 = capacity;
+
+	sro->addr_count += out_regs->a1;
+
+	return 0;
+}
+
+void rmi_sro_free(struct rmi_sro_state *sro)
+{
+	for (int i = 0; i < sro->addr_count; i++) {
+		unsigned long entry = sro->addr_list[i];
+		unsigned long addr = RMI_ADDR_RANGE_ADDR(entry);
+		unsigned long unit_size = RMI_ADDR_RANGE_SIZE(entry);
+		unsigned long count = RMI_ADDR_RANGE_COUNT(entry);
+		unsigned long state = RMI_ADDR_RANGE_STATE(entry);
+		unsigned long size = donate_req_to_size(unit_size) * count;
+
+		if (state == RMI_OP_MEM_DELEGATED) {
+			if (WARN_ON(rmi_undelegate_range(addr, size))) {
+				/* Leak the pages */
+				continue;
+			}
+		}
+		free_pages_exact(phys_to_virt(addr), size);
+	}
+
+	sro->addr_count = 0;
+}
+
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
+{
+	unsigned long sro_handle;
+	struct arm_smccc_1_2_regs regs;
+	struct arm_smccc_1_2_regs *regs_in = &sro->regs;
+
+	rmi_smccc_invoke(regs_in, &regs);
+
+	sro_handle = regs.a1;
+
+	while (RMI_RETURN_STATUS(regs.a0) == RMI_INCOMPLETE) {

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 376

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+		bool can_cancel = RMI_RETURN_CAN_CANCEL(regs.a0);
+		int ret;
+
+		switch (RMI_RETURN_MEMREQ(regs.a0)) {
+		case RMI_OP_MEM_REQ_NONE:
+			regs = (struct arm_smccc_1_2_regs){
+				SMC_RMI_OP_CONTINUE, sro_handle, 0
+			};
+			rmi_smccc_invoke(&regs, &regs);
+			break;

'ret' isn't initialized for case RMI_OP_MEM_REQ_NONE.

Quoted 26 lines
arch/arm64/kernel/rmi.c +26 -0
+		case RMI_OP_MEM_REQ_DONATE:
+			ret = rmi_sro_donate(sro, sro_handle, regs.a2, &regs,
+					     gfp);
+			break;
+		case RMI_OP_MEM_REQ_RECLAIM:
+			ret = rmi_sro_reclaim(sro, sro_handle, &regs);
+			break;
+		default:
+			ret = WARN_ON(1);
+			break;
+		}
+
+		if (ret) {
+			if (can_cancel) {
+				/*
+				 * FIXME: Handle cancelling properly!
+				 *
+				 * If the operation has failed due to memory
+				 * allocation failure then the information on
+				 * the memory allocation should be saved, so
+				 * that the allocation can be repeated outside
+				 * of any context which prevented the
+				 * allocation.
+				 */
+			}
+			if (WARN_ON(ret))

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 412

Quoted 10 lines
arch/arm64/kernel/rmi.c +7 -0
+				return ret;
+		}
+	}
+
+	return regs.a0;
+}
+
  static int rmi_check_version(void)
  {
  	struct arm_smccc_res res;

Thanks,
Gavin

#58 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

Quoted 62 lines · Gavin Shan

Hi Steven,

Quoted 59 lines · Steven Price

RMM v2.0 introduces the concept of "Stateful RMI Operations" (SRO). This means that an SMC can return with an operation still in progress. The host is excepted to continue the operation until is reaches a conclusion (either success or failure). During this process the RMM can request additional memory ('donate') or hand memory back to the host ('reclaim'). The host can request an in progress operation is cancelled, but still continue the operation until it has completed (otherwise the incomplete operation may cause future RMM operations to fail).

The SRO is tracked using a struct rmi_sro_state object which keeps track of any memory which has been allocated but not yet consumed by the RMM or reclaimed from the RMM. This allows the memory to be reused in a future request within the same operation. It will also permit an operation to be done in a context where memory allocation may be difficult (e.g. atomic context) with the option to abort the operation and retry the memory allocation outside of the atomic context. The memory stored in the struct rmi_sro_state object can then be reused on the subsequent attempt.

---
v14:
  * SRO support has improved although is still not fully complete. The infrastructure has been moved out of KVM.
---
  arch/arm64/include/asm/rmi_cmds.h |   1 + arch/arm64/kernel/rmi.c           | 359 ++++++++++++++++++++++++++++++

  2 files changed, 360 insertions(+)
arch/arm64/include/ renamed from arch/arm64/include/asm/rmi_cmds.h +0 -0

asm/rmi_cmds.h
index eb213c8e6f26..1a7b0c8f1e38 100644

arch/arm64/include/asm/rmi_cmds.h +1 -0
@@ -35,6 +35,7 @@ struct rmi_sro_state {
     int rmi_delegate_range(phys_addr_t phys, unsigned long size);
   int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+int free_delegated_page(phys_addr_t phys);
     static inline int rmi_delegate_page(phys_addr_t phys)
   {
arch/arm64/kernel/rmi.c +10 -0
index 08cef54acadb..a8107ca9bb6d 100644
@@ -48,6 +48,365 @@ int rmi_undelegate_range(phys_addr_t phys,
 unsigned long size)
       return ret;
   }
   +static unsigned long donate_req_to_size(unsigned long donatereq)
+{
+    unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+
+    switch (unit_size) {
+    case 0:
+        return PAGE_SIZE;
+    case 1:
+        return PMD_SIZE;
+    case 2:
+        return PUD_SIZE;
Quoted 8 lines

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 67

Quoted 6 lines
arch/arm64/kernel/rmi.c +6 -0
+    case 3:
+        return P4D_SIZE;
+    }
+    unreachable();
+}
+

It's worthy to have 'inline'.

Generally it's best to let the compiler make the decision. A small static function like this is highly likely to be inlined by the compiler already.

The exception of course is when the function is in a header and then it's necessary to use "static inline".

Quoted 3 lines

{P4D, PUD, PMD}_SIZE can be equal if there> are
no P4D and PUD, depending on CONFIG_PGTABLE_LEVELS. In this case, can the 'unit_size' be translated to wrong value?

Technically yes. I think I can actually rewrite this more simply as:

static unsigned long donate_req_to_size(unsigned long donatereq)

{
	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);

	return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(3 - unit_size));
}

which neatly sidesteps the CONFIG_PGTABLE_LEVELS problem too.

Quoted 77 lines
Quoted 77 lines
arch/arm64/kernel/rmi.c +77 -0
+static void rmi_smccc_invoke(struct arm_smccc_1_2_regs *regs_in,
+                 struct arm_smccc_1_2_regs *regs_out)
+{
+    struct arm_smccc_1_2_regs regs = *regs_in;
+    unsigned long status;
+
+    do {
+        arm_smccc_1_2_invoke(&regs, regs_out);
+        status = RMI_RETURN_STATUS(regs_out->a0);
+    } while (status == RMI_BUSY || status == RMI_BLOCKED);
+}
+
+int free_delegated_page(phys_addr_t phys)
+{
+    if (WARN_ON(rmi_undelegate_page(phys))) {
+        /* Undelegate failed: leak the page */
+        return -EBUSY;
+    }
+
+    free_page((unsigned long)phys_to_virt(phys));
+
+    return 0;
+}
+
+static int rmi_sro_ensure_capacity(struct rmi_sro_state *sro,
+                   unsigned long count)
+{
+    if (WARN_ON_ONCE(sro->addr_count > RMI_MAX_ADDR_LIST))
+        return -EOVERFLOW;
+
+    if (count > RMI_MAX_ADDR_LIST - sro->addr_count)
+        return -ENOSPC;
+
+    return 0;
+}
+
+static int rmi_sro_donate_contig(struct rmi_sro_state *sro,
+                 unsigned long sro_handle,
+                 unsigned long donatereq,
+                 struct arm_smccc_1_2_regs *out_regs,
+                 gfp_t gfp)
+{
+    unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+    unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+    unsigned long count = RMI_DONATE_COUNT(donatereq);
+    unsigned long state = RMI_DONATE_STATE(donatereq);
+    unsigned long size = unit_size_bytes * count;
+    unsigned long addr_range;
+    int ret;
+    void *virt;
+    phys_addr_t phys;
+    struct arm_smccc_1_2_regs regs = {
+        SMC_RMI_OP_MEM_DONATE,
+        sro_handle
+    };
+
+    for (int i = 0; i < sro->addr_count; i++) {
+        unsigned long entry = sro->addr_list[i];
+
+        if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+            RMI_ADDR_RANGE_COUNT(entry) == count &&
+            RMI_ADDR_RANGE_STATE(entry) == state) {
+            sro->addr_count--;
+            swap(sro->addr_list[sro->addr_count],
+                 sro->addr_list[i]);
+
+            goto out;
+        }
+    }
+
+    ret = rmi_sro_ensure_capacity(sro, 1);
+    if (ret)
+        return ret;
+
+    virt = alloc_pages_exact(size, gfp);
+    if (!virt)
+        return -ENOMEM;
Quoted 8 lines

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, · line 146

Quoted 2 lines
arch/arm64/kernel/rmi.c +2 -0
+    phys = virt_to_phys(virt);
+

alloc_pages_exact() will fail if the requested size exceeds the maximal allowed
size (1 << MAX_PAGE_ORDER). The maximal size is usually smaller than PUD_SIZE
but PUD_SIZE is allowed by the RMM.

This is an area where to be honest I'm really not sure what to do. Technically the RMM is allowed to ask for a contiguous range of 512GB pages (on a 4K system - larger with larger page sizes) - but clearly no real OS is going to be able to provide anything like that.

In practise we don't expect the RMM to do anything so crazy. It's not really clear to be whether even 2MB (PMD_SIZE) is needed. But the spec is written to be generic.

So my current approach is to calculate the required size and pass it into alloc_pages_exact(). For "stupidly large" values this will fail and Linux just doesn't support an RMM which attempts this. If there is ever a usecase which needs this then we'd need to find a different method of providing the memory (most likely some form of carveout to avoid fragmentation). But my view is we should wait for that usecase to be identified first.

Quoted 227 lines
Quoted 227 lines
arch/arm64/kernel/rmi.c +117 -0
+    if (state == RMI_OP_MEM_DELEGATED) {
+        if (rmi_delegate_range(phys, size)) {
+            free_pages_exact(virt, size);
+            return -ENXIO;
+        }
+    }
+
+    addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+    FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
+    FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, count);
+    FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+    sro->addr_list[sro->addr_count] = addr_range;
+
+out:
+    regs.a2 = virt_to_phys(&sro->addr_list[sro->addr_count]);
+    regs.a3 = 1;
+    rmi_smccc_invoke(&regs, out_regs);
+
+    unsigned long donated_granules = out_regs->a1;
+    unsigned long donated_size = donated_granules << PAGE_SHIFT;
+
+    if (donated_granules == 0) {
+        /* No pages used by the RMM */
+        sro->addr_count++;
+    } else if (donated_size < size) {
+        phys = sro->addr_list[sro->addr_count] &
 RMI_ADDR_RANGE_ADDR_MASK;
+
+        /* Not all granules used by the RMM, free the remaining pages */
+        for (long i = donated_size; i < size; i += PAGE_SIZE) {
+            if (state == RMI_OP_MEM_DELEGATED)
+                free_delegated_page(phys + i);
+            else
+                __free_page(phys_to_page(phys + i));
+        }
+    }
+
+    return 0;
+}
+
+static int rmi_sro_donate_noncontig(struct rmi_sro_state *sro,
+                    unsigned long sro_handle,
+                    unsigned long donatereq,
+                    struct arm_smccc_1_2_regs *out_regs,
+                    gfp_t gfp)
+{
+    unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+    unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+    unsigned long count = RMI_DONATE_COUNT(donatereq);
+    unsigned long state = RMI_DONATE_STATE(donatereq);
+    unsigned long found = 0;
+    unsigned long addr_list_start = sro->addr_count;
+    int ret;
+    struct arm_smccc_1_2_regs regs = {
+        SMC_RMI_OP_MEM_DONATE,
+        sro_handle
+    };
+
+    for (int i = 0; i < addr_list_start && found < count; i++) {
+        unsigned long entry = sro->addr_list[i];
+
+        if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+            RMI_ADDR_RANGE_COUNT(entry) == 1 &&
+            RMI_ADDR_RANGE_STATE(entry) == state) {
+            addr_list_start--;
+            swap(sro->addr_list[addr_list_start],
+                 sro->addr_list[i]);
+            found++;
+            i--;
+        }
+    }
+
+    ret = rmi_sro_ensure_capacity(sro, count - found);
+    if (ret)
+        return ret;
+
+    while (found < count) {
+        unsigned long addr_range;
+        void *virt = alloc_pages_exact(unit_size_bytes, gfp);
+        phys_addr_t phys;
+
+        if (!virt)
+            return -ENOMEM;
+
+        phys = virt_to_phys(virt);
+
+        if (state == RMI_OP_MEM_DELEGATED) {
+            if (rmi_delegate_range(phys, unit_size_bytes)) {
+                free_pages_exact(virt, unit_size_bytes);
+                return -ENXIO;
+            }
+        }
+
+        addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+        FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
+        FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, 1);
+        FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+        sro->addr_list[sro->addr_count++] = addr_range;
+        found++;
+    }
+
+    regs.a2 = virt_to_phys(&sro->addr_list[addr_list_start]);
+    regs.a3 = found;
+    rmi_smccc_invoke(&regs, out_regs);
+
+    unsigned long donated_granules = out_regs->a1;
+
+    if (WARN_ON(donated_granules & ((unit_size_bytes >> PAGE_SHIFT) -
 1))) {
+        /*
+         * FIXME: RMM has only consumed part of a huge page, this leaks
+         * the rest of the huge page
+         */
+        donated_granules = ALIGN(donated_granules,
+                     (unit_size_bytes >> PAGE_SHIFT));
+    }
+    unsigned long donated_blocks = donated_granules /

(unit_size_bytes >> PAGE_SHIFT);

arch/arm64/kernel/rmi.c +107 -0
+
+    if (WARN_ON(donated_blocks > found))
+        donated_blocks = found;
+
+    unsigned long undonated_blocks = found - donated_blocks;
+
+    while (donated_blocks && undonated_blocks) {
+        sro->addr_count--;
+        swap(sro->addr_list[addr_list_start],
+             sro->addr_list[sro->addr_count]);
+        addr_list_start++;
+
+        donated_blocks--;
+        undonated_blocks--;
+    }
+    sro->addr_count -= donated_blocks;
+
+    return 0;
+}
+
+static int rmi_sro_donate(struct rmi_sro_state *sro,
+              unsigned long sro_handle,
+              unsigned long donatereq,
+              struct arm_smccc_1_2_regs *regs,
+              gfp_t gfp)
+{
+    unsigned long count = RMI_DONATE_COUNT(donatereq);
+
+    if (WARN_ON(!count))
+        return 0;
+
+    if (RMI_DONATE_CONTIG(donatereq)) {
+        return rmi_sro_donate_contig(sro, sro_handle, donatereq,
+                         regs, gfp);
+    } else {
+        return rmi_sro_donate_noncontig(sro, sro_handle, donatereq,
+                        regs, gfp);
+    }
+}
+
+static int rmi_sro_reclaim(struct rmi_sro_state *sro,
+               unsigned long sro_handle,
+               struct arm_smccc_1_2_regs *out_regs)
+{
+    unsigned long capacity;
+    struct arm_smccc_1_2_regs regs;
+    int ret;
+
+    ret = rmi_sro_ensure_capacity(sro, 1);
+    if (ret)
+        rmi_sro_free(sro);
+
+    capacity = RMI_MAX_ADDR_LIST - sro->addr_count;
+
+    regs = (struct arm_smccc_1_2_regs){
+        SMC_RMI_OP_MEM_RECLAIM,
+        sro_handle,
+        virt_to_phys(&sro->addr_list[sro->addr_count]),
+        capacity
+    };
+    rmi_smccc_invoke(&regs, out_regs);
+
+    if (WARN_ON_ONCE(out_regs->a1 > capacity))
+        out_regs->a1 = capacity;
+
+    sro->addr_count += out_regs->a1;
+
+    return 0;
+}
+
+void rmi_sro_free(struct rmi_sro_state *sro)
+{
+    for (int i = 0; i < sro->addr_count; i++) {
+        unsigned long entry = sro->addr_list[i];
+        unsigned long addr = RMI_ADDR_RANGE_ADDR(entry);
+        unsigned long unit_size = RMI_ADDR_RANGE_SIZE(entry);
+        unsigned long count = RMI_ADDR_RANGE_COUNT(entry);
+        unsigned long state = RMI_ADDR_RANGE_STATE(entry);
+        unsigned long size = donate_req_to_size(unit_size) * count;
+
+        if (state == RMI_OP_MEM_DELEGATED) {
+            if (WARN_ON(rmi_undelegate_range(addr, size))) {
+                /* Leak the pages */
+                continue;
+            }
+        }
+        free_pages_exact(phys_to_virt(addr), size);
+    }
+
+    sro->addr_count = 0;
+}
+
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
+{
+    unsigned long sro_handle;
+    struct arm_smccc_1_2_regs regs;
+    struct arm_smccc_1_2_regs *regs_in = &sro->regs;
+
+    rmi_smccc_invoke(regs_in, &regs);
+
+    sro_handle = regs.a1;
+
+    while (RMI_RETURN_STATUS(regs.a0) == RMI_INCOMPLETE) {
+        bool can_cancel = RMI_RETURN_CAN_CANCEL(regs.a0);
+        int ret;
+
+        switch (RMI_RETURN_MEMREQ(regs.a0)) {
Quoted 8 lines
Quoted 6 lines

+        case RMI_OP_MEM_REQ_NONE:
+            regs = (struct arm_smccc_1_2_regs){ +                SMC_RMI_OP_CONTINUE, sro_handle, 0 +            };
+            rmi_smccc_invoke(&regs, &regs); +            break;

'ret' isn't initialized for case RMI_OP_MEM_REQ_NONE.

Good spot - ret should be initialised to 0.

Thanks,
Steve

Quoted 40 lines
Quoted 33 lines

+        case RMI_OP_MEM_REQ_DONATE:
+            ret = rmi_sro_donate(sro, sro_handle, regs.a2, &regs, +                         gfp);
+            break;
+        case RMI_OP_MEM_REQ_RECLAIM:
+            ret = rmi_sro_reclaim(sro, sro_handle, &regs); +            break;
+        default:
+            ret = WARN_ON(1);
+            break;
+        }

arch/arm64/kernel/rmi.c +22 -0
+
+        if (ret) {
+            if (can_cancel) {
+                /*
+                 * FIXME: Handle cancelling properly!
+                 *
+                 * If the operation has failed due to memory
+                 * allocation failure then the information on
+                 * the memory allocation should be saved, so
+                 * that the allocation can be repeated outside
+                 * of any context which prevented the
+                 * allocation.
+                 */
+            }
+            if (WARN_ON(ret))
+                return ret;
+        }
+    }
+
+    return regs.a0;
+}
+
Quoted 3 lines

  static int rmi_check_version(void)
  {
      struct arm_smccc_res res;

Thanks,
Gavin

#59 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

Steven Price wrote:
[..]

Quoted 15 lines
Quoted 5 lines

alloc_pages_exact() will fail if the requested size exceeds the maximal allowed
size (1 << MAX_PAGE_ORDER). The maximal size is usually smaller than PUD_SIZE
but PUD_SIZE is allowed by the RMM.

This is an area where to be honest I'm really not sure what to do. Technically the RMM is allowed to ask for a contiguous range of 512GB pages (on a 4K system - larger with larger page sizes) - but clearly no real OS is going to be able to provide anything like that.

In practise we don't expect the RMM to do anything so crazy. It's not really clear to be whether even 2MB (PMD_SIZE) is needed. But the spec is written to be generic.

Quoted 7 lines

So my current approach is to calculate the required size and pass it into alloc_pages_exact(). For "stupidly large" values this will fail and Linux just doesn't support an RMM which attempts this. If there is ever a usecase which needs this then we'd need to find a different method of providing the memory (most likely some form of carveout to avoid fragmentation). But my view is we should wait for that usecase to be identified first.

Just some comparison comments as I am also going through the TDX patches which enable "Extension SEAMCALLs". These new SEAMCALLs are similar to the SRO mechanism [1].

TDX asks for an upfront delegation of memory at init time using alloc_contig_pages() that is never returned until entire module is shutdown. alloc_contig_pages() is not subject to the MAX_ORDER limit, but not sure that alloc_contig_pages() is suitable for small+dynamic runtime memory add / release that SRO potentially wants to do?

Does SRO always balance the size of RMI_OP_MEM_REQ_DONATE with RMI_OP_MEM_REQ_RECLAIM, or might some donate requests be a one way donation like TDX? Just poking to see if there is a path to preallocate a pool vs the fine grained per-operation alloc/free.

[1]: http://lore.kernel.org/[email protected]

#60 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

Hi Dan,

Quoted 25 lines · Dan Williams (nvidia)

Steven Price wrote:
[..]

Quoted 15 lines
Quoted 5 lines

alloc_pages_exact() will fail if the requested size exceeds the maximal allowed
size (1 << MAX_PAGE_ORDER). The maximal size is usually smaller than PUD_SIZE
but PUD_SIZE is allowed by the RMM.

This is an area where to be honest I'm really not sure what to do. Technically the RMM is allowed to ask for a contiguous range of 512GB pages (on a 4K system - larger with larger page sizes) - but clearly no real OS is going to be able to provide anything like that.

In practise we don't expect the RMM to do anything so crazy. It's not really clear to be whether even 2MB (PMD_SIZE) is needed. But the spec is written to be generic.

Quoted 7 lines

So my current approach is to calculate the required size and pass it into alloc_pages_exact(). For "stupidly large" values this will fail and Linux just doesn't support an RMM which attempts this. If there is ever a usecase which needs this then we'd need to find a different method of providing the memory (most likely some form of carveout to avoid fragmentation). But my view is we should wait for that usecase to be identified first.

Quoted 3 lines

Just some comparison comments as I am also going through the TDX patches which enable "Extension SEAMCALLs". These new SEAMCALLs are similar to the SRO mechanism [1].

Looks like at least at the moment it's much more one-way than the SRO mechanism - there's no reclaim mechanism (yet).

Quoted 5 lines

TDX asks for an upfront delegation of memory at init time using alloc_contig_pages() that is never returned until entire module is shutdown. alloc_contig_pages() is not subject to the MAX_ORDER limit, but not sure that alloc_contig_pages() is suitable for small+dynamic runtime memory add / release that SRO potentially wants to do?

Yeah I'm not sure quite what is best. I expect the RMM to only request contiguous memory for very small allocations to use as hardware page tables. It's an issue I'm trying to work through that the specification doesn't provide any guidance for what sort of allocations the host should expect to provide.

Quoted 4 lines

Does SRO always balance the size of RMI_OP_MEM_REQ_DONATE with RMI_OP_MEM_REQ_RECLAIM, or might some donate requests be a one way donation like TDX? Just poking to see if there is a path to preallocate a pool vs the fine grained per-operation alloc/free.

The spec is unfortunately not prescriptive on this point. For an operation which eventually fails, the expectation is that the RMM will return all the memory that was provided (and exactly that memory). But the specification doesn't actually require that.

The problem is that there are situations where a racing operation on another CPU could trigger this to not happen. For example, a new page table needs to be allocated to complete a map operation, but then a racing operation on another CPU makes use of this page table (e.g due to a map at a different address), the memory for the page table cannot be returned even if the operation doesn't complete because it's in use from the racing operation.

I don't believe the current RMM design will actually do this - but it's not something we actually want to prevent in the spec.

Equally the expectation is that all the donated memory for a guest will be returned when the guest is destroyed. But we don't have anything in the spec to enforce this.

I don't particularly expect a pool to be that useful for the expected memory allocation patterns as I expect SRO donations to be long lived. We don't (yet at least) have a concept of donating memory just for "scratch" memory during an operation. Although the SRO mechanism doesn't rule that out.

Thanks,
Steve

maintainer
#61 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

In reply to Steven Price
Quoted 53 lines · Steven Price

RMM v2.0 introduces the concept of "Stateful RMI Operations" (SRO). This means that an SMC can return with an operation still in progress. The host is excepted to continue the operation until is reaches a conclusion (either success or failure). During this process the RMM can request additional memory ('donate') or hand memory back to the host ('reclaim'). The host can request an in progress operation is cancelled, but still continue the operation until it has completed (otherwise the incomplete operation may cause future RMM operations to fail).

The SRO is tracked using a struct rmi_sro_state object which keeps track of any memory which has been allocated but not yet consumed by the RMM or reclaimed from the RMM. This allows the memory to be reused in a future request within the same operation. It will also permit an operation to be done in a context where memory allocation may be difficult (e.g. atomic context) with the option to abort the operation and retry the memory allocation outside of the atomic context. The memory stored in the struct rmi_sro_state object can then be reused on the subsequent attempt.

---
v14:
* SRO support has improved although is still not fully complete. The infrastructure has been moved out of KVM.

---
 arch/arm64/include/asm/rmi_cmds.h |   1 +
 arch/arm64/kernel/rmi.c           | 359 ++++++++++++++++++++++++++++++
 2 files changed, 360 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +1 -0
index eb213c8e6f26..1a7b0c8f1e38 100644
@@ -35,6 +35,7 @@ struct rmi_sro_state {
 
 int rmi_delegate_range(phys_addr_t phys, unsigned long size);
 int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+int free_delegated_page(phys_addr_t phys);
 
 static inline int rmi_delegate_page(phys_addr_t phys)
 {
arch/arm64/kernel/rmi.c +3 -0
index 08cef54acadb..a8107ca9bb6d 100644
@@ -48,6 +48,365 @@ int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
 	return ret;
 }
 
+static unsigned long donate_req_to_size(unsigned long donatereq)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 63

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	switch (unit_size) {
+	case 0:
+		return PAGE_SIZE;
+	case 1:
+		return PMD_SIZE;
+	case 2:
+		return PUD_SIZE;
+	case 3:
+		return P4D_SIZE;

How does this work when we have folded levels? If this is supposed to be the architected size, then it should actively express that:

	return BIT(unit_size * (PAGE_SHIFT - 3) + PAGE_SHIFT);
Quoted 9 lines
arch/arm64/kernel/rmi.c +9 -0
+	}
+	unreachable();
+}
+
+static void rmi_smccc_invoke(struct arm_smccc_1_2_regs *regs_in,
+			     struct arm_smccc_1_2_regs *regs_out)
+{
+	struct arm_smccc_1_2_regs regs = *regs_in;
+	unsigned long status;

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 82

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	do {
+		arm_smccc_1_2_invoke(&regs, regs_out);
+		status = RMI_RETURN_STATUS(regs_out->a0);
+	} while (status == RMI_BUSY || status == RMI_BLOCKED);
+}
+
+int free_delegated_page(phys_addr_t phys)
+{
+	if (WARN_ON(rmi_undelegate_page(phys))) {

Please drop this WARN_ON(). Or at least make it ONCE. Everywhere.

Quoted 68 lines
arch/arm64/kernel/rmi.c +68 -0
+		/* Undelegate failed: leak the page */
+		return -EBUSY;
+	}
+
+	free_page((unsigned long)phys_to_virt(phys));
+
+	return 0;
+}
+
+static int rmi_sro_ensure_capacity(struct rmi_sro_state *sro,
+				   unsigned long count)
+{
+	if (WARN_ON_ONCE(sro->addr_count > RMI_MAX_ADDR_LIST))
+		return -EOVERFLOW;
+
+	if (count > RMI_MAX_ADDR_LIST - sro->addr_count)
+		return -ENOSPC;
+
+	return 0;
+}
+
+static int rmi_sro_donate_contig(struct rmi_sro_state *sro,
+				 unsigned long sro_handle,
+				 unsigned long donatereq,
+				 struct arm_smccc_1_2_regs *out_regs,
+				 gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long size = unit_size_bytes * count;
+	unsigned long addr_range;
+	int ret;
+	void *virt;
+	phys_addr_t phys;
+	struct arm_smccc_1_2_regs regs = {
+		SMC_RMI_OP_MEM_DONATE,
+		sro_handle
+	};
+
+	for (int i = 0; i < sro->addr_count; i++) {
+		unsigned long entry = sro->addr_list[i];
+
+		if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+		    RMI_ADDR_RANGE_COUNT(entry) == count &&
+		    RMI_ADDR_RANGE_STATE(entry) == state) {
+			sro->addr_count--;
+			swap(sro->addr_list[sro->addr_count],
+			     sro->addr_list[i]);
+
+			goto out;
+		}
+	}
+
+	ret = rmi_sro_ensure_capacity(sro, 1);
+	if (ret)
+		return ret;
+
+	virt = alloc_pages_exact(size, gfp);
+	if (!virt)
+		return -ENOMEM;
+	phys = virt_to_phys(virt);
+
+	if (state == RMI_OP_MEM_DELEGATED) {
+		if (rmi_delegate_range(phys, size)) {
+			free_pages_exact(virt, size);
+			return -ENXIO;

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 160

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+		}
+	}
+
+	addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+	FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
+	FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, count);
+	FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+	sro->addr_list[sro->addr_count] = addr_range;
+

Shouldn't this be moved to a helper that ensures capacity, and returns an error otherwise?

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 163

Quoted 3 lines
arch/arm64/kernel/rmi.c +3 -0
+out:
+	regs.a2 = virt_to_phys(&sro->addr_list[sro->addr_count]);
+	regs.a3 = 1;

This could really do with context specific helpers that populate regs based on a set of parameters. I have no idea what this 1 here is, and the init is spread over too much code. Think of the children!

That's valid for the whole patch.

	M.
Quoted 226 lines
arch/arm64/kernel/rmi.c +226 -0
+	rmi_smccc_invoke(&regs, out_regs);
+
+	unsigned long donated_granules = out_regs->a1;
+	unsigned long donated_size = donated_granules << PAGE_SHIFT;
+
+	if (donated_granules == 0) {
+		/* No pages used by the RMM */
+		sro->addr_count++;
+	} else if (donated_size < size) {
+		phys = sro->addr_list[sro->addr_count] & RMI_ADDR_RANGE_ADDR_MASK;
+
+		/* Not all granules used by the RMM, free the remaining pages */
+		for (long i = donated_size; i < size; i += PAGE_SIZE) {
+			if (state == RMI_OP_MEM_DELEGATED)
+				free_delegated_page(phys + i);
+			else
+				__free_page(phys_to_page(phys + i));
+		}
+	}
+
+	return 0;
+}
+
+static int rmi_sro_donate_noncontig(struct rmi_sro_state *sro,
+				    unsigned long sro_handle,
+				    unsigned long donatereq,
+				    struct arm_smccc_1_2_regs *out_regs,
+				    gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long found = 0;
+	unsigned long addr_list_start = sro->addr_count;
+	int ret;
+	struct arm_smccc_1_2_regs regs = {
+		SMC_RMI_OP_MEM_DONATE,
+		sro_handle
+	};
+
+	for (int i = 0; i < addr_list_start && found < count; i++) {
+		unsigned long entry = sro->addr_list[i];
+
+		if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+		    RMI_ADDR_RANGE_COUNT(entry) == 1 &&
+		    RMI_ADDR_RANGE_STATE(entry) == state) {
+			addr_list_start--;
+			swap(sro->addr_list[addr_list_start],
+			     sro->addr_list[i]);
+			found++;
+			i--;
+		}
+	}
+
+	ret = rmi_sro_ensure_capacity(sro, count - found);
+	if (ret)
+		return ret;
+
+	while (found < count) {
+		unsigned long addr_range;
+		void *virt = alloc_pages_exact(unit_size_bytes, gfp);
+		phys_addr_t phys;
+
+		if (!virt)
+			return -ENOMEM;
+
+		phys = virt_to_phys(virt);
+
+		if (state == RMI_OP_MEM_DELEGATED) {
+			if (rmi_delegate_range(phys, unit_size_bytes)) {
+				free_pages_exact(virt, unit_size_bytes);
+				return -ENXIO;
+			}
+		}
+
+		addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+		FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
+		FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, 1);
+		FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+		sro->addr_list[sro->addr_count++] = addr_range;
+		found++;
+	}
+
+	regs.a2 = virt_to_phys(&sro->addr_list[addr_list_start]);
+	regs.a3 = found;
+	rmi_smccc_invoke(&regs, out_regs);
+
+	unsigned long donated_granules = out_regs->a1;
+
+	if (WARN_ON(donated_granules & ((unit_size_bytes >> PAGE_SHIFT) - 1))) {
+		/*
+		 * FIXME: RMM has only consumed part of a huge page, this leaks
+		 * the rest of the huge page
+		 */
+		donated_granules = ALIGN(donated_granules,
+					 (unit_size_bytes >> PAGE_SHIFT));
+	}
+	unsigned long donated_blocks = donated_granules / (unit_size_bytes >> PAGE_SHIFT);
+
+	if (WARN_ON(donated_blocks > found))
+		donated_blocks = found;
+
+	unsigned long undonated_blocks = found - donated_blocks;
+
+	while (donated_blocks && undonated_blocks) {
+		sro->addr_count--;
+		swap(sro->addr_list[addr_list_start],
+		     sro->addr_list[sro->addr_count]);
+		addr_list_start++;
+
+		donated_blocks--;
+		undonated_blocks--;
+	}
+	sro->addr_count -= donated_blocks;
+
+	return 0;
+}
+
+static int rmi_sro_donate(struct rmi_sro_state *sro,
+			  unsigned long sro_handle,
+			  unsigned long donatereq,
+			  struct arm_smccc_1_2_regs *regs,
+			  gfp_t gfp)
+{
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+
+	if (WARN_ON(!count))
+		return 0;
+
+	if (RMI_DONATE_CONTIG(donatereq)) {
+		return rmi_sro_donate_contig(sro, sro_handle, donatereq,
+					     regs, gfp);
+	} else {
+		return rmi_sro_donate_noncontig(sro, sro_handle, donatereq,
+						regs, gfp);
+	}
+}
+
+static int rmi_sro_reclaim(struct rmi_sro_state *sro,
+			   unsigned long sro_handle,
+			   struct arm_smccc_1_2_regs *out_regs)
+{
+	unsigned long capacity;
+	struct arm_smccc_1_2_regs regs;
+	int ret;
+
+	ret = rmi_sro_ensure_capacity(sro, 1);
+	if (ret)
+		rmi_sro_free(sro);
+
+	capacity = RMI_MAX_ADDR_LIST - sro->addr_count;
+
+	regs = (struct arm_smccc_1_2_regs){
+		SMC_RMI_OP_MEM_RECLAIM,
+		sro_handle,
+		virt_to_phys(&sro->addr_list[sro->addr_count]),
+		capacity
+	};
+	rmi_smccc_invoke(&regs, out_regs);
+
+	if (WARN_ON_ONCE(out_regs->a1 > capacity))
+		out_regs->a1 = capacity;
+
+	sro->addr_count += out_regs->a1;
+
+	return 0;
+}
+
+void rmi_sro_free(struct rmi_sro_state *sro)
+{
+	for (int i = 0; i < sro->addr_count; i++) {
+		unsigned long entry = sro->addr_list[i];
+		unsigned long addr = RMI_ADDR_RANGE_ADDR(entry);
+		unsigned long unit_size = RMI_ADDR_RANGE_SIZE(entry);
+		unsigned long count = RMI_ADDR_RANGE_COUNT(entry);
+		unsigned long state = RMI_ADDR_RANGE_STATE(entry);
+		unsigned long size = donate_req_to_size(unit_size) * count;
+
+		if (state == RMI_OP_MEM_DELEGATED) {
+			if (WARN_ON(rmi_undelegate_range(addr, size))) {
+				/* Leak the pages */
+				continue;
+			}
+		}
+		free_pages_exact(phys_to_virt(addr), size);
+	}
+
+	sro->addr_count = 0;
+}
+
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
+{
+	unsigned long sro_handle;
+	struct arm_smccc_1_2_regs regs;
+	struct arm_smccc_1_2_regs *regs_in = &sro->regs;
+
+	rmi_smccc_invoke(regs_in, &regs);
+
+	sro_handle = regs.a1;
+
+	while (RMI_RETURN_STATUS(regs.a0) == RMI_INCOMPLETE) {
+		bool can_cancel = RMI_RETURN_CAN_CANCEL(regs.a0);
+		int ret;
+
+		switch (RMI_RETURN_MEMREQ(regs.a0)) {
+		case RMI_OP_MEM_REQ_NONE:
+			regs = (struct arm_smccc_1_2_regs){
+				SMC_RMI_OP_CONTINUE, sro_handle, 0
+			};
+			rmi_smccc_invoke(&regs, &regs);
+			break;
+		case RMI_OP_MEM_REQ_DONATE:
+			ret = rmi_sro_donate(sro, sro_handle, regs.a2, &regs,
+					     gfp);
+			break;
+		case RMI_OP_MEM_REQ_RECLAIM:
+			ret = rmi_sro_reclaim(sro, sro_handle, &regs);
+			break;
+		default:
+			ret = WARN_ON(1);
+			break;
+		}
+
+		if (ret) {

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 399

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+			if (can_cancel) {
+				/*
+				 * FIXME: Handle cancelling properly!
+				 *
+				 * If the operation has failed due to memory
+				 * allocation failure then the information on
+				 * the memory allocation should be saved, so
+				 * that the allocation can be repeated outside
+				 * of any context which prevented the
+				 * allocation.

Honestly, this is the sort of stuff that I'd expect to be solved *before* posting this code. Since this is so central to the whole memory management, it needs to be correct from day-1.

If you can't make it work in time, then tone the supported features down. But FIXMEs and WARN_ONs are not the way to go.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#62 lore raw

Re: [PATCH v14 10/44] arm64: RMI: Add support for SRO

Quoted 69 lines · Marc Zyngier
Quoted 53 lines · Steven Price

RMM v2.0 introduces the concept of "Stateful RMI Operations" (SRO). This means that an SMC can return with an operation still in progress. The host is excepted to continue the operation until is reaches a conclusion (either success or failure). During this process the RMM can request additional memory ('donate') or hand memory back to the host ('reclaim'). The host can request an in progress operation is cancelled, but still continue the operation until it has completed (otherwise the incomplete operation may cause future RMM operations to fail).

The SRO is tracked using a struct rmi_sro_state object which keeps track of any memory which has been allocated but not yet consumed by the RMM or reclaimed from the RMM. This allows the memory to be reused in a future request within the same operation. It will also permit an operation to be done in a context where memory allocation may be difficult (e.g. atomic context) with the option to abort the operation and retry the memory allocation outside of the atomic context. The memory stored in the struct rmi_sro_state object can then be reused on the subsequent attempt.

---
v14:
* SRO support has improved although is still not fully complete. The infrastructure has been moved out of KVM.

---
 arch/arm64/include/asm/rmi_cmds.h |   1 +
 arch/arm64/kernel/rmi.c           | 359 ++++++++++++++++++++++++++++++
 2 files changed, 360 insertions(+)
arch/arm64/include/asm/rmi_cmds.h +1 -0
index eb213c8e6f26..1a7b0c8f1e38 100644
@@ -35,6 +35,7 @@ struct rmi_sro_state {
 
 int rmi_delegate_range(phys_addr_t phys, unsigned long size);
 int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
+int free_delegated_page(phys_addr_t phys);
 
 static inline int rmi_delegate_page(phys_addr_t phys)
 {
arch/arm64/kernel/rmi.c +3 -0
index 08cef54acadb..a8107ca9bb6d 100644
@@ -48,6 +48,365 @@ int rmi_undelegate_range(phys_addr_t phys, unsigned long size)
 	return ret;
 }
 
+static unsigned long donate_req_to_size(unsigned long donatereq)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 63

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+
+	switch (unit_size) {
+	case 0:
+		return PAGE_SIZE;
+	case 1:
+		return PMD_SIZE;
+	case 2:
+		return PUD_SIZE;
+	case 3:
+		return P4D_SIZE;

How does this work when we have folded levels? If this is supposed to be the architected size, then it should actively express that:

Quoted 1 line
	return BIT(unit_size * (PAGE_SHIFT - 3) + PAGE_SHIFT);

It doesn't work (as Gavin also pointed out). There's an existing macro to make this even cleaner:

return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(3 - unit_size));

Quoted 13 lines
Quoted 13 lines
arch/arm64/kernel/rmi.c +13 -0
+	}
+	unreachable();
+}
+
+static void rmi_smccc_invoke(struct arm_smccc_1_2_regs *regs_in,
+			     struct arm_smccc_1_2_regs *regs_out)
+{
+	struct arm_smccc_1_2_regs regs = *regs_in;
+	unsigned long status;
+
+	do {
+		arm_smccc_1_2_invoke(&regs, regs_out);
+		status = RMI_RETURN_STATUS(regs_out->a0);
Quoted 8 lines

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 82

Quoted 6 lines
arch/arm64/kernel/rmi.c +6 -0
+	} while (status == RMI_BUSY || status == RMI_BLOCKED);
+}
+
+int free_delegated_page(phys_addr_t phys)
+{
+	if (WARN_ON(rmi_undelegate_page(phys))) {

Please drop this WARN_ON(). Or at least make it ONCE. Everywhere.

Happy to change to WARN_ON_ONCE(). I think we should keep a WARN of some sort as this is causing Linux to leak pages - it's definitely something the sysadmin would want to know about.

Quoted 73 lines
Quoted 73 lines
arch/arm64/kernel/rmi.c +73 -0
+		/* Undelegate failed: leak the page */
+		return -EBUSY;
+	}
+
+	free_page((unsigned long)phys_to_virt(phys));
+
+	return 0;
+}
+
+static int rmi_sro_ensure_capacity(struct rmi_sro_state *sro,
+				   unsigned long count)
+{
+	if (WARN_ON_ONCE(sro->addr_count > RMI_MAX_ADDR_LIST))
+		return -EOVERFLOW;
+
+	if (count > RMI_MAX_ADDR_LIST - sro->addr_count)
+		return -ENOSPC;
+
+	return 0;
+}
+
+static int rmi_sro_donate_contig(struct rmi_sro_state *sro,
+				 unsigned long sro_handle,
+				 unsigned long donatereq,
+				 struct arm_smccc_1_2_regs *out_regs,
+				 gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long size = unit_size_bytes * count;
+	unsigned long addr_range;
+	int ret;
+	void *virt;
+	phys_addr_t phys;
+	struct arm_smccc_1_2_regs regs = {
+		SMC_RMI_OP_MEM_DONATE,
+		sro_handle
+	};
+
+	for (int i = 0; i < sro->addr_count; i++) {
+		unsigned long entry = sro->addr_list[i];
+
+		if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+		    RMI_ADDR_RANGE_COUNT(entry) == count &&
+		    RMI_ADDR_RANGE_STATE(entry) == state) {
+			sro->addr_count--;
+			swap(sro->addr_list[sro->addr_count],
+			     sro->addr_list[i]);
+
+			goto out;
+		}
+	}
+
+	ret = rmi_sro_ensure_capacity(sro, 1);
+	if (ret)
+		return ret;
+
+	virt = alloc_pages_exact(size, gfp);
+	if (!virt)
+		return -ENOMEM;
+	phys = virt_to_phys(virt);
+
+	if (state == RMI_OP_MEM_DELEGATED) {
+		if (rmi_delegate_range(phys, size)) {
+			free_pages_exact(virt, size);
+			return -ENXIO;
+		}
+	}
+
+	addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+	FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
Quoted 8 lines

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 160

Quoted 5 lines
arch/arm64/kernel/rmi.c +5 -0
+	FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, count);
+	FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+	sro->addr_list[sro->addr_count] = addr_range;
+

Shouldn't this be moved to a helper that ensures capacity, and returns an error otherwise?

I'm not sure quite what you are suggesting. I already have a rmi_sro_ensure_capacity() helper. By this point we know there's space.

Quoted 9 lines

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 163

Quoted 3 lines
arch/arm64/kernel/rmi.c +3 -0
+out:
+	regs.a2 = virt_to_phys(&sro->addr_list[sro->addr_count]);
+	regs.a3 = 1;

This could really do with context specific helpers that populate regs based on a set of parameters. I have no idea what this 1 here is, and the init is spread over too much code. Think of the children!

That's valid for the whole patch.

That's a good point. SRO is a bit tricky because I wanted the actual SMC call to be done in one place so we can handle all the RMI_INCOMPLETE cases together. But I could certainly add some helpers to setup the registers rather than assigning directly to regs.a<n>.

Thanks,
Steve

Quoted 247 lines
	M.
Quoted 226 lines
arch/arm64/kernel/rmi.c +226 -0
+	rmi_smccc_invoke(&regs, out_regs);
+
+	unsigned long donated_granules = out_regs->a1;
+	unsigned long donated_size = donated_granules << PAGE_SHIFT;
+
+	if (donated_granules == 0) {
+		/* No pages used by the RMM */
+		sro->addr_count++;
+	} else if (donated_size < size) {
+		phys = sro->addr_list[sro->addr_count] & RMI_ADDR_RANGE_ADDR_MASK;
+
+		/* Not all granules used by the RMM, free the remaining pages */
+		for (long i = donated_size; i < size; i += PAGE_SIZE) {
+			if (state == RMI_OP_MEM_DELEGATED)
+				free_delegated_page(phys + i);
+			else
+				__free_page(phys_to_page(phys + i));
+		}
+	}
+
+	return 0;
+}
+
+static int rmi_sro_donate_noncontig(struct rmi_sro_state *sro,
+				    unsigned long sro_handle,
+				    unsigned long donatereq,
+				    struct arm_smccc_1_2_regs *out_regs,
+				    gfp_t gfp)
+{
+	unsigned long unit_size = RMI_DONATE_SIZE(donatereq);
+	unsigned long unit_size_bytes = donate_req_to_size(donatereq);
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+	unsigned long state = RMI_DONATE_STATE(donatereq);
+	unsigned long found = 0;
+	unsigned long addr_list_start = sro->addr_count;
+	int ret;
+	struct arm_smccc_1_2_regs regs = {
+		SMC_RMI_OP_MEM_DONATE,
+		sro_handle
+	};
+
+	for (int i = 0; i < addr_list_start && found < count; i++) {
+		unsigned long entry = sro->addr_list[i];
+
+		if (RMI_ADDR_RANGE_SIZE(entry) == unit_size &&
+		    RMI_ADDR_RANGE_COUNT(entry) == 1 &&
+		    RMI_ADDR_RANGE_STATE(entry) == state) {
+			addr_list_start--;
+			swap(sro->addr_list[addr_list_start],
+			     sro->addr_list[i]);
+			found++;
+			i--;
+		}
+	}
+
+	ret = rmi_sro_ensure_capacity(sro, count - found);
+	if (ret)
+		return ret;
+
+	while (found < count) {
+		unsigned long addr_range;
+		void *virt = alloc_pages_exact(unit_size_bytes, gfp);
+		phys_addr_t phys;
+
+		if (!virt)
+			return -ENOMEM;
+
+		phys = virt_to_phys(virt);
+
+		if (state == RMI_OP_MEM_DELEGATED) {
+			if (rmi_delegate_range(phys, unit_size_bytes)) {
+				free_pages_exact(virt, unit_size_bytes);
+				return -ENXIO;
+			}
+		}
+
+		addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
+		FIELD_MODIFY(RMI_ADDR_RANGE_SIZE_MASK, &addr_range, unit_size);
+		FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, 1);
+		FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
+
+		sro->addr_list[sro->addr_count++] = addr_range;
+		found++;
+	}
+
+	regs.a2 = virt_to_phys(&sro->addr_list[addr_list_start]);
+	regs.a3 = found;
+	rmi_smccc_invoke(&regs, out_regs);
+
+	unsigned long donated_granules = out_regs->a1;
+
+	if (WARN_ON(donated_granules & ((unit_size_bytes >> PAGE_SHIFT) - 1))) {
+		/*
+		 * FIXME: RMM has only consumed part of a huge page, this leaks
+		 * the rest of the huge page
+		 */
+		donated_granules = ALIGN(donated_granules,
+					 (unit_size_bytes >> PAGE_SHIFT));
+	}
+	unsigned long donated_blocks = donated_granules / (unit_size_bytes >> PAGE_SHIFT);
+
+	if (WARN_ON(donated_blocks > found))
+		donated_blocks = found;
+
+	unsigned long undonated_blocks = found - donated_blocks;
+
+	while (donated_blocks && undonated_blocks) {
+		sro->addr_count--;
+		swap(sro->addr_list[addr_list_start],
+		     sro->addr_list[sro->addr_count]);
+		addr_list_start++;
+
+		donated_blocks--;
+		undonated_blocks--;
+	}
+	sro->addr_count -= donated_blocks;
+
+	return 0;
+}
+
+static int rmi_sro_donate(struct rmi_sro_state *sro,
+			  unsigned long sro_handle,
+			  unsigned long donatereq,
+			  struct arm_smccc_1_2_regs *regs,
+			  gfp_t gfp)
+{
+	unsigned long count = RMI_DONATE_COUNT(donatereq);
+
+	if (WARN_ON(!count))
+		return 0;
+
+	if (RMI_DONATE_CONTIG(donatereq)) {
+		return rmi_sro_donate_contig(sro, sro_handle, donatereq,
+					     regs, gfp);
+	} else {
+		return rmi_sro_donate_noncontig(sro, sro_handle, donatereq,
+						regs, gfp);
+	}
+}
+
+static int rmi_sro_reclaim(struct rmi_sro_state *sro,
+			   unsigned long sro_handle,
+			   struct arm_smccc_1_2_regs *out_regs)
+{
+	unsigned long capacity;
+	struct arm_smccc_1_2_regs regs;
+	int ret;
+
+	ret = rmi_sro_ensure_capacity(sro, 1);
+	if (ret)
+		rmi_sro_free(sro);
+
+	capacity = RMI_MAX_ADDR_LIST - sro->addr_count;
+
+	regs = (struct arm_smccc_1_2_regs){
+		SMC_RMI_OP_MEM_RECLAIM,
+		sro_handle,
+		virt_to_phys(&sro->addr_list[sro->addr_count]),
+		capacity
+	};
+	rmi_smccc_invoke(&regs, out_regs);
+
+	if (WARN_ON_ONCE(out_regs->a1 > capacity))
+		out_regs->a1 = capacity;
+
+	sro->addr_count += out_regs->a1;
+
+	return 0;
+}
+
+void rmi_sro_free(struct rmi_sro_state *sro)
+{
+	for (int i = 0; i < sro->addr_count; i++) {
+		unsigned long entry = sro->addr_list[i];
+		unsigned long addr = RMI_ADDR_RANGE_ADDR(entry);
+		unsigned long unit_size = RMI_ADDR_RANGE_SIZE(entry);
+		unsigned long count = RMI_ADDR_RANGE_COUNT(entry);
+		unsigned long state = RMI_ADDR_RANGE_STATE(entry);
+		unsigned long size = donate_req_to_size(unit_size) * count;
+
+		if (state == RMI_OP_MEM_DELEGATED) {
+			if (WARN_ON(rmi_undelegate_range(addr, size))) {
+				/* Leak the pages */
+				continue;
+			}
+		}
+		free_pages_exact(phys_to_virt(addr), size);
+	}
+
+	sro->addr_count = 0;
+}
+
+unsigned long rmi_sro_execute(struct rmi_sro_state *sro, gfp_t gfp)
+{
+	unsigned long sro_handle;
+	struct arm_smccc_1_2_regs regs;
+	struct arm_smccc_1_2_regs *regs_in = &sro->regs;
+
+	rmi_smccc_invoke(regs_in, &regs);
+
+	sro_handle = regs.a1;
+
+	while (RMI_RETURN_STATUS(regs.a0) == RMI_INCOMPLETE) {
+		bool can_cancel = RMI_RETURN_CAN_CANCEL(regs.a0);
+		int ret;
+
+		switch (RMI_RETURN_MEMREQ(regs.a0)) {
+		case RMI_OP_MEM_REQ_NONE:
+			regs = (struct arm_smccc_1_2_regs){
+				SMC_RMI_OP_CONTINUE, sro_handle, 0
+			};
+			rmi_smccc_invoke(&regs, &regs);
+			break;
+		case RMI_OP_MEM_REQ_DONATE:
+			ret = rmi_sro_donate(sro, sro_handle, regs.a2, &regs,
+					     gfp);
+			break;
+		case RMI_OP_MEM_REQ_RECLAIM:
+			ret = rmi_sro_reclaim(sro, sro_handle, &regs);
+			break;
+		default:
+			ret = WARN_ON(1);
+			break;
+		}
+
+		if (ret) {

arch/arm64/kernel/rmi.c int rmi_undelegate_range(phys_addr_t phys, unsigned long size) · line 399

Quoted 10 lines
arch/arm64/kernel/rmi.c +10 -0
+			if (can_cancel) {
+				/*
+				 * FIXME: Handle cancelling properly!
+				 *
+				 * If the operation has failed due to memory
+				 * allocation failure then the information on
+				 * the memory allocation should be saved, so
+				 * that the allocation can be repeated outside
+				 * of any context which prevented the
+				 * allocation.

Honestly, this is the sort of stuff that I'd expect to be solved *before* posting this code. Since this is so central to the whole memory management, it needs to be correct from day-1.

If you can't make it work in time, then tone the supported features down. But FIXMEs and WARN_ONs are not the way to go.

	M.
#63 lore raw

PATCH v14 11/44 arm64: RMI: Check for RMI support at KVM init

In reply to Steven Price

Check if the RMI support is sufficient for using in KVM. Specifically we currently only support KVM in VHE mode when for creating realm VMs.

---
Changes since v13:
* Most of the init has been moved out of the 'kvm' directory so this is much more basic now.
Changes since v12:
* Drop check for 4k page size.
Changes since v11:
* Reword slightly the comments on the realm states.
Changes since v10:
* kvm_is_realm() no longer has a NULL check.
* Rename from "rme" to "rmi" when referring to the RMM interface.
* Check for RME (hardware) support before probing for RMI support.
Changes since v8:
* No need to guard kvm_init_rme() behind 'in_hyp_mode'.
Changes since v6:
* Improved message for an unsupported RMI ABI version.
Changes since v5:
* Reword "unsupported" message from "host supports" to "we want" to clarify that 'we' are the 'host'.
Changes since v2:
* Drop return value from kvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.

---
 arch/arm64/include/asm/kvm_host.h |  4 ++++
 arch/arm64/include/asm/kvm_rmi.h  | 17 +++++++++++++++++
 arch/arm64/include/asm/virt.h     |  1 +
 arch/arm64/kvm/Makefile           |  2 +-
 arch/arm64/kvm/arm.c              |  5 +++++
 arch/arm64/kvm/rmi.c              | 24 ++++++++++++++++++++++++
 6 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/include/asm/kvm_rmi.h
 create mode 100644 arch/arm64/kvm/rmi.c
arch/arm64/include/asm/kvm_host.h +4 -0
index 851f6171751c..3512696ed506 100644
@@ -27,6 +27,7 @@
 #include <asm/fpsimd.h>
 #include <asm/kvm.h>
 #include <asm/kvm_asm.h>
+#include <asm/kvm_rmi.h>
 #include <asm/vncr_mapping.h>
 
 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -424,6 +425,9 @@ struct kvm_arch {
 	/* Nested virtualization info */
 	struct dentry *debugfs_nv_dentry;
 #endif
+
+	bool is_realm;
+	struct realm realm;
 };
 
 struct kvm_vcpu_fault_info {
arch/arm64/include/asm/kvm_rmi.h new file +17 -0
new file mode 100644
index 000000000000..4936007947fd
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#ifndef __ASM_KVM_RMI_H
+#define __ASM_KVM_RMI_H
+
+/**
+ * struct realm - Additional per VM data for a Realm
+ */
+struct realm {
+};
+
+void kvm_init_rmi(void);
+
+#endif /* __ASM_KVM_RMI_H */
arch/arm64/include/asm/virt.h +1 -0
index b546703c3ab9..92cec42952f4 100644
@@ -87,6 +87,7 @@ void __hyp_reset_vectors(void);
 bool is_kvm_arm_initialised(void);
 
 DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
+DECLARE_STATIC_KEY_FALSE(kvm_rmi_is_available);
 
 static inline bool is_pkvm_initialized(void)
 {
arch/arm64/kvm/Makefile +1 -1
index 59612d2f277c..ed3cf30eb06e 100644
@@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o config.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
-	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+	 vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \
 	 arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \
 	 vgic/vgic.o vgic/vgic-init.o \
 	 vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/arm.c +5 -0
index 176cbe8baad3..247e03b33035 100644
@@ -41,6 +41,7 @@
 #include <asm/kvm_nested.h>
 #include <asm/kvm_pkvm.h>
 #include <asm/kvm_ptrauth.h>
+#include <asm/kvm_rmi.h>
 #include <asm/sections.h>
 #include <asm/stacktrace/nvhe.h>
 
@@ -109,6 +110,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext)
 	return -EINVAL;
 }
 
+DEFINE_STATIC_KEY_FALSE(kvm_rmi_is_available);
+
 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
@@ -2975,6 +2978,8 @@ static __init int kvm_arm_init(void)
 
 	in_hyp_mode = is_kernel_in_hyp_mode();
 
+	kvm_init_rmi();
+
 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
arch/arm64/kvm/rmi.c new file +24 -0
new file mode 100644
index 000000000000..1acc972a4b92
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2025 ARM Ltd.
+ */
+
+#include <linux/kvm_host.h>
+
+#include <asm/rmi_cmds.h>
+#include <asm/virt.h>
+
+void kvm_init_rmi(void)
+{
+	/*
+	 * TODO: Support Realm guests in nVHE mode, this will require adding
+	 * EL2 stub(s) for REC entry and possibly other things.
+	 */
+	if (!is_kernel_in_hyp_mode())
+		return;
+
+	if (!rmi_is_available())
+		return;
+
+	/* Future patch will enable static branch kvm_rmi_is_available */
+}
Signature · 1 line
2.43.0
#64 lore raw

PATCH v14 12/44 arm64: RMI: Check for LPA2 support

In reply to Steven Price

If KVM has enabled LPA2 support then check that the RMM also supports it. If there is a mismatch then disable support for realm guests as the VMM may attempt to create a guest which is incompatible with the RMM.

---
New patch for v13

---
 arch/arm64/kvm/rmi.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
arch/arm64/kvm/rmi.c +19 -0
index 1acc972a4b92..6e28b669ded2 100644
@@ -5,9 +5,25 @@
 
 #include <linux/kvm_host.h>
 
+#include <asm/kvm_pgtable.h>
 #include <asm/rmi_cmds.h>
 #include <asm/virt.h>
 
+static bool rmi_has_feature(unsigned long feature)
+{
+	return !!u64_get_bits(rmm_feat_reg0, feature);
+}
+
+static int rmm_check_features(void)
+{
+	if (kvm_lpa2_is_enabled() && !rmi_has_feature(RMI_FEATURE_REGISTER_0_LPA2)) {
+		kvm_err("RMM doesn't support LPA2");
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
 void kvm_init_rmi(void)
 {
 	/*
@@ -20,5 +36,8 @@ void kvm_init_rmi(void)
 	if (!rmi_is_available())
 		return;
 
+	if (rmm_check_features())
+		return;
+
 	/* Future patch will enable static branch kvm_rmi_is_available */
 }
Signature · 1 line
2.43.0
#65 lore raw

PATCH v14 13/44 arm64: RMI: Define the user ABI

In reply to Steven Price

There is one CAP which identified the presence of CCA, and one ioctl. The ioctl is used to populate memory during creation of the realm as this requires the RMM to copy data from an unprotected address to the protected memory - CCA does not support memory conversion where the memory contents is preserved as this is incompatible with memory encryption.

---
Changes since v13:
* KVM_ARM_VCPU_RMI_PSCI_COMPLETE removed.
* KVM_ARM_RMI_POPULATE documentation updated to reflect that the structure is written by the kernel.
* CAP number bumped.
Changes since v12:
* Change KVM_ARM_RMI_POPULATE to update the structure with the amount that has been progressed rather than return the number of bytes populated.
* Describe the flag KVM_ARM_RMI_POPULATE_FLAGS_MEASURE.
* CAP number is bumped.
* NOTE: The PSCI ioctl may be removed in a future spec release.
Changes since v11:
* Completely reworked to be more implicit. Rather than having explicit CAP operations to progress the realm construction these operations are done when needed (on populating and on first vCPU run).
* Populate and PSCI complete are promoted to proper ioctls.
Changes since v10:
* Rename symbols from RME to RMI.
Changes since v9:
* Improvements to documentation.
* Bump the magic number for KVM_CAP_ARM_RME to avoid conflicts.
Changes since v8:
* Minor improvements to documentation following review.
* Bump the magic numbers to avoid conflicts.
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

---
 Documentation/virt/kvm/api.rst | 40 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h       | 13 +++++++++++
 2 files changed, 53 insertions(+)
Documentation/virt/kvm/api.rst +40 -0
index 52bbbb553ce1..ca68aae7faa2 100644
@@ -6553,6 +6553,37 @@ KVM_S390_KEYOP_SSKE
   Sets the storage key for the guest address ``guest_addr`` to the key
   specified in ``key``, returning the previous value in ``key``.
 
+4.145 KVM_ARM_RMI_POPULATE
+--------------------------
+
+:Capability: KVM_CAP_ARM_RMI
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_rmi_populate (in/out)
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+  };
+
+Populate a region of protected address space by copying the data from the
+(non-protected) user space pointer provided into a protected region (backed by
+guestmem_fd). It implicitly sets the destination region to RIPAS RAM. This is
+only valid before any VCPUs have been run. The ioctl might not populate the
+entire region and in this case the kernel updates the fields `base`, `size` and
+`source_uaddr`. User space may have to repeatedly call it until `size` is 0 to
+populate the entire region.
+
+`flags` can be set to `KVM_ARM_RMI_POPULATE_FLAGS_MEASURE` to request that the
+populated data is hashed and added to the guest's Realm Initial Measurement
+(RIM).
+
 .. _kvm_run:
 
 5. The kvm_run structure
@@ -8904,6 +8935,15 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_ARM_RMI
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: None
+
+This capability indicates that support for CCA realms is available.
+
 8. Other capabilities.
 ======================
 
include/uapi/linux/kvm.h +13 -0
index 6c8afa2047bf..b8cff0938041 100644
@@ -996,6 +996,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
 #define KVM_CAP_S390_VSIE_ESAMODE 248
+#define KVM_CAP_ARM_RMI 249
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1669,4 +1670,16 @@ struct kvm_pre_fault_memory {
 	__u64 padding[5];
 };
 
+/* Available with KVM_CAP_ARM_RMI, only for VMs with KVM_VM_TYPE_ARM_REALM */
+#define KVM_ARM_RMI_POPULATE	_IOWR(KVMIO, 0xd7, struct kvm_arm_rmi_populate)
+#define KVM_ARM_RMI_POPULATE_FLAGS_MEASURE	(1 << 0)
+
+struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+};
+
 #endif /* __LINUX_KVM_H */
Signature · 1 line
2.43.0
#66 lore raw

Re: [PATCH v14 13/44] arm64: RMI: Define the user ABI

Quoted 6 lines · Steven Price

There is one CAP which identified the presence of CCA, and one ioctl. The ioctl is used to populate memory during creation of the realm as this requires the RMM to copy data from an unprotected address to the protected memory - CCA does not support memory conversion where the memory contents is preserved as this is incompatible with memory encryption.

Nit:
I believe spelling out the CAP and ioctl names can improve the commit message. Also "memory conversion" is a little vague, maybe

... CCA does not support shared <-> private memory conversion where ...

would make this clearer?

Thanks,
Wei-Lin Chang

Quoted 132 lines

---
Changes since v13:
* KVM_ARM_VCPU_RMI_PSCI_COMPLETE removed.
* KVM_ARM_RMI_POPULATE documentation updated to reflect that the structure is written by the kernel.
* CAP number bumped.
Changes since v12:
* Change KVM_ARM_RMI_POPULATE to update the structure with the amount that has been progressed rather than return the number of bytes populated.
* Describe the flag KVM_ARM_RMI_POPULATE_FLAGS_MEASURE.
* CAP number is bumped.
* NOTE: The PSCI ioctl may be removed in a future spec release.
Changes since v11:
* Completely reworked to be more implicit. Rather than having explicit CAP operations to progress the realm construction these operations are done when needed (on populating and on first vCPU run).
* Populate and PSCI complete are promoted to proper ioctls.
Changes since v10:
* Rename symbols from RME to RMI.
Changes since v9:
* Improvements to documentation.
* Bump the magic number for KVM_CAP_ARM_RME to avoid conflicts.
Changes since v8:
* Minor improvements to documentation following review.
* Bump the magic numbers to avoid conflicts.
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

---
 Documentation/virt/kvm/api.rst | 40 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h       | 13 +++++++++++
 2 files changed, 53 insertions(+)
Documentation/virt/kvm/api.rst +40 -0
index 52bbbb553ce1..ca68aae7faa2 100644
@@ -6553,6 +6553,37 @@ KVM_S390_KEYOP_SSKE
   Sets the storage key for the guest address ``guest_addr`` to the key
   specified in ``key``, returning the previous value in ``key``.
 
+4.145 KVM_ARM_RMI_POPULATE
+--------------------------
+
+:Capability: KVM_CAP_ARM_RMI
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_rmi_populate (in/out)
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+  };
+
+Populate a region of protected address space by copying the data from the
+(non-protected) user space pointer provided into a protected region (backed by
+guestmem_fd). It implicitly sets the destination region to RIPAS RAM. This is
+only valid before any VCPUs have been run. The ioctl might not populate the
+entire region and in this case the kernel updates the fields `base`, `size` and
+`source_uaddr`. User space may have to repeatedly call it until `size` is 0 to
+populate the entire region.
+
+`flags` can be set to `KVM_ARM_RMI_POPULATE_FLAGS_MEASURE` to request that the
+populated data is hashed and added to the guest's Realm Initial Measurement
+(RIM).
+
 .. _kvm_run:
 
 5. The kvm_run structure
@@ -8904,6 +8935,15 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_ARM_RMI
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: None
+
+This capability indicates that support for CCA realms is available.
+
 8. Other capabilities.
 ======================
 
include/uapi/linux/kvm.h +13 -0
index 6c8afa2047bf..b8cff0938041 100644
@@ -996,6 +996,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
 #define KVM_CAP_S390_VSIE_ESAMODE 248
+#define KVM_CAP_ARM_RMI 249
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1669,4 +1670,16 @@ struct kvm_pre_fault_memory {
 	__u64 padding[5];
 };
 
+/* Available with KVM_CAP_ARM_RMI, only for VMs with KVM_VM_TYPE_ARM_REALM */
+#define KVM_ARM_RMI_POPULATE	_IOWR(KVMIO, 0xd7, struct kvm_arm_rmi_populate)
+#define KVM_ARM_RMI_POPULATE_FLAGS_MEASURE	(1 << 0)
+
+struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+};
+
 #endif /* __LINUX_KVM_H */
Signature · 1 line
2.43.0
#67 lore raw

Re: [PATCH v14 13/44] arm64: RMI: Define the user ABI

Quoted 8 lines · Wei-Lin Chang
Quoted 6 lines · Steven Price

There is one CAP which identified the presence of CCA, and one ioctl. The ioctl is used to populate memory during creation of the realm as this requires the RMM to copy data from an unprotected address to the protected memory - CCA does not support memory conversion where the memory contents is preserved as this is incompatible with memory encryption.

Quoted 7 lines

Nit:
I believe spelling out the CAP and ioctl names can improve the commit message. Also "memory conversion" is a little vague, maybe

... CCA does not support shared <-> private memory conversion where ...

would make this clearer?

Thanks for the suggestions - yes I agree that would make it clearer.

Thanks,
Steve

Quoted 135 lines

Thanks,
Wei-Lin Chang

Quoted 132 lines

---
Changes since v13:
* KVM_ARM_VCPU_RMI_PSCI_COMPLETE removed.
* KVM_ARM_RMI_POPULATE documentation updated to reflect that the structure is written by the kernel.
* CAP number bumped.
Changes since v12:
* Change KVM_ARM_RMI_POPULATE to update the structure with the amount that has been progressed rather than return the number of bytes populated.
* Describe the flag KVM_ARM_RMI_POPULATE_FLAGS_MEASURE.
* CAP number is bumped.
* NOTE: The PSCI ioctl may be removed in a future spec release.
Changes since v11:
* Completely reworked to be more implicit. Rather than having explicit CAP operations to progress the realm construction these operations are done when needed (on populating and on first vCPU run).
* Populate and PSCI complete are promoted to proper ioctls.
Changes since v10:
* Rename symbols from RME to RMI.
Changes since v9:
* Improvements to documentation.
* Bump the magic number for KVM_CAP_ARM_RME to avoid conflicts.
Changes since v8:
* Minor improvements to documentation following review.
* Bump the magic numbers to avoid conflicts.
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

---
 Documentation/virt/kvm/api.rst | 40 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h       | 13 +++++++++++
 2 files changed, 53 insertions(+)
Documentation/virt/kvm/api.rst +40 -0
index 52bbbb553ce1..ca68aae7faa2 100644
@@ -6553,6 +6553,37 @@ KVM_S390_KEYOP_SSKE
   Sets the storage key for the guest address ``guest_addr`` to the key
   specified in ``key``, returning the previous value in ``key``.
 
+4.145 KVM_ARM_RMI_POPULATE
+--------------------------
+
+:Capability: KVM_CAP_ARM_RMI
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_rmi_populate (in/out)
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+  };
+
+Populate a region of protected address space by copying the data from the
+(non-protected) user space pointer provided into a protected region (backed by
+guestmem_fd). It implicitly sets the destination region to RIPAS RAM. This is
+only valid before any VCPUs have been run. The ioctl might not populate the
+entire region and in this case the kernel updates the fields `base`, `size` and
+`source_uaddr`. User space may have to repeatedly call it until `size` is 0 to
+populate the entire region.
+
+`flags` can be set to `KVM_ARM_RMI_POPULATE_FLAGS_MEASURE` to request that the
+populated data is hashed and added to the guest's Realm Initial Measurement
+(RIM).
+
 .. _kvm_run:
 
 5. The kvm_run structure
@@ -8904,6 +8935,15 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_ARM_RMI
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: None
+
+This capability indicates that support for CCA realms is available.
+
 8. Other capabilities.
 ======================
 
include/uapi/linux/kvm.h +13 -0
index 6c8afa2047bf..b8cff0938041 100644
@@ -996,6 +996,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
 #define KVM_CAP_S390_VSIE_ESAMODE 248
+#define KVM_CAP_ARM_RMI 249
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1669,4 +1670,16 @@ struct kvm_pre_fault_memory {
 	__u64 padding[5];
 };
 
+/* Available with KVM_CAP_ARM_RMI, only for VMs with KVM_VM_TYPE_ARM_REALM */
+#define KVM_ARM_RMI_POPULATE	_IOWR(KVMIO, 0xd7, struct kvm_arm_rmi_populate)
+#define KVM_ARM_RMI_POPULATE_FLAGS_MEASURE	(1 << 0)
+
+struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+};
+
 #endif /* __LINUX_KVM_H */
Signature · 1 line
2.43.0
maintainer
#68 lore raw

Re: [PATCH v14 13/44] arm64: RMI: Define the user ABI

In reply to Steven Price
Quoted 44 lines · Steven Price

There is one CAP which identified the presence of CCA, and one ioctl. The ioctl is used to populate memory during creation of the realm as this requires the RMM to copy data from an unprotected address to the protected memory - CCA does not support memory conversion where the memory contents is preserved as this is incompatible with memory encryption.

---
Changes since v13:
* KVM_ARM_VCPU_RMI_PSCI_COMPLETE removed.
* KVM_ARM_RMI_POPULATE documentation updated to reflect that the structure is written by the kernel.
* CAP number bumped.
Changes since v12:
* Change KVM_ARM_RMI_POPULATE to update the structure with the amount that has been progressed rather than return the number of bytes populated.
* Describe the flag KVM_ARM_RMI_POPULATE_FLAGS_MEASURE.
* CAP number is bumped.
* NOTE: The PSCI ioctl may be removed in a future spec release.
Changes since v11:
* Completely reworked to be more implicit. Rather than having explicit CAP operations to progress the realm construction these operations are done when needed (on populating and on first vCPU run).
* Populate and PSCI complete are promoted to proper ioctls.
Changes since v10:
* Rename symbols from RME to RMI.
Changes since v9:
* Improvements to documentation.
* Bump the magic number for KVM_CAP_ARM_RME to avoid conflicts.
Changes since v8:
* Minor improvements to documentation following review.
* Bump the magic numbers to avoid conflicts.
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

Quoted 4 lines
---
 Documentation/virt/kvm/api.rst | 40 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h       | 13 +++++++++++
 2 files changed, 53 insertions(+)

$SUBJECT looks wrong. This is a KVM change, not an RMI change.

Quoted 29 lines
Documentation/virt/kvm/api.rst +20 -0
index 52bbbb553ce1..ca68aae7faa2 100644
@@ -6553,6 +6553,37 @@ KVM_S390_KEYOP_SSKE
   Sets the storage key for the guest address ``guest_addr`` to the key
   specified in ``key``, returning the previous value in ``key``.
 
+4.145 KVM_ARM_RMI_POPULATE
+--------------------------
+
+:Capability: KVM_CAP_ARM_RMI
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_rmi_populate (in/out)
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+  };
+
+Populate a region of protected address space by copying the data from the

Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6585

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+(non-protected) user space pointer provided into a protected region (backed by
+guestmem_fd). It implicitly sets the destination region to RIPAS RAM. This is
+only valid before any VCPUs have been run. The ioctl might not populate the
+entire region and in this case the kernel updates the fields `base`, `size` and
+`source_uaddr`. User space may have to repeatedly call it until `size` is 0 to
+populate the entire region.
+
+`flags` can be set to `KVM_ARM_RMI_POPULATE_FLAGS_MEASURE` to request that the
+populated data is hashed and added to the guest's Realm Initial Measurement
+(RIM).

Where is that measurement stored? And retrieved? At least a pointer to that would help.

Quoted 39 lines
Documentation/virt/kvm/api.rst +10 -0
+
 .. _kvm_run:
 
 5. The kvm_run structure
@@ -8904,6 +8935,15 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_ARM_RMI
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: None
+
+This capability indicates that support for CCA realms is available.
+
 8. Other capabilities.
 ======================
 
include/uapi/linux/kvm.h +4 -0
index 6c8afa2047bf..b8cff0938041 100644
@@ -996,6 +996,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
 #define KVM_CAP_S390_VSIE_ESAMODE 248
+#define KVM_CAP_ARM_RMI 249
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1669,4 +1670,16 @@ struct kvm_pre_fault_memory {
 	__u64 padding[5];
 };
 
+/* Available with KVM_CAP_ARM_RMI, only for VMs with KVM_VM_TYPE_ARM_REALM */
+#define KVM_ARM_RMI_POPULATE	_IOWR(KVMIO, 0xd7, struct kvm_arm_rmi_populate)
+#define KVM_ARM_RMI_POPULATE_FLAGS_MEASURE	(1 << 0)

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

Quoted 10 lines
include/uapi/linux/kvm.h +9 -0
+
+struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+};
+
 #endif /* __LINUX_KVM_H */

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#69 lore raw

Re: [PATCH v14 13/44] arm64: RMI: Define the user ABI

Hi Marc

Quoted 87 lines · Marc Zyngier
Quoted 44 lines · Steven Price

There is one CAP which identified the presence of CCA, and one ioctl. The ioctl is used to populate memory during creation of the realm as this requires the RMM to copy data from an unprotected address to the protected memory - CCA does not support memory conversion where the memory contents is preserved as this is incompatible with memory encryption.

---
Changes since v13:
* KVM_ARM_VCPU_RMI_PSCI_COMPLETE removed.
* KVM_ARM_RMI_POPULATE documentation updated to reflect that the structure is written by the kernel.
* CAP number bumped.
Changes since v12:
* Change KVM_ARM_RMI_POPULATE to update the structure with the amount that has been progressed rather than return the number of bytes populated.
* Describe the flag KVM_ARM_RMI_POPULATE_FLAGS_MEASURE.
* CAP number is bumped.
* NOTE: The PSCI ioctl may be removed in a future spec release.
Changes since v11:
* Completely reworked to be more implicit. Rather than having explicit CAP operations to progress the realm construction these operations are done when needed (on populating and on first vCPU run).
* Populate and PSCI complete are promoted to proper ioctls.
Changes since v10:
* Rename symbols from RME to RMI.
Changes since v9:
* Improvements to documentation.
* Bump the magic number for KVM_CAP_ARM_RME to avoid conflicts.
Changes since v8:
* Minor improvements to documentation following review.
* Bump the magic numbers to avoid conflicts.
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

Quoted 4 lines
---
  Documentation/virt/kvm/api.rst | 40 ++++++++++++++++++++++++++++++++++
  include/uapi/linux/kvm.h       | 13 +++++++++++
  2 files changed, 53 insertions(+)

$SUBJECT looks wrong. This is a KVM change, not an RMI change.

Quoted 34 lines
Documentation/virt/kvm/api.rst +25 -0
index 52bbbb553ce1..ca68aae7faa2 100644
@@ -6553,6 +6553,37 @@ KVM_S390_KEYOP_SSKE
    Sets the storage key for the guest address ``guest_addr`` to the key
    specified in ``key``, returning the previous value in ``key``.
 
+4.145 KVM_ARM_RMI_POPULATE
+--------------------------
+
+:Capability: KVM_CAP_ARM_RMI
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_rmi_populate (in/out)
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+  };
+
+Populate a region of protected address space by copying the data from the
+(non-protected) user space pointer provided into a protected region (backed by
+guestmem_fd). It implicitly sets the destination region to RIPAS RAM. This is
+only valid before any VCPUs have been run. The ioctl might not populate the
+entire region and in this case the kernel updates the fields `base`, `size` and
+`source_uaddr`. User space may have to repeatedly call it until `size` is 0 to
Quoted 8 lines

Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6585

Quoted 5 lines
Documentation/virt/kvm/api.rst +5 -0
+populate the entire region.
+
+`flags` can be set to `KVM_ARM_RMI_POPULATE_FLAGS_MEASURE` to request that the
+populated data is hashed and added to the guest's Realm Initial Measurement
+(RIM).

Where is that measurement stored? And retrieved? At least a pointer to that would help.

The measurement is stored by the RMM and is made available to the Guests
via RSI interface (RSI_ATTEST_TOKEN_{INIT,CONTINUE}) as part of the attestation report along with the Platform attestation. On Linux Guest, this could be fetched using TSM report infrastructure. This could be added to the doc.

Suzuki

Quoted 55 lines
Quoted 40 lines
Documentation/virt/kvm/api.rst +10 -0
+
  .. _kvm_run:
 
  5. The kvm_run structure
@@ -8904,6 +8935,15 @@ helpful if user space wants to emulate instructions which are not
  This capability can be enabled dynamically even if VCPUs were already
  created and are running.
 
+7.47 KVM_CAP_ARM_RMI
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: None
+
+This capability indicates that support for CCA realms is available.
+
  8. Other capabilities.
  ======================
 
include/uapi/linux/kvm.h +4 -0
index 6c8afa2047bf..b8cff0938041 100644
@@ -996,6 +996,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_S390_USER_OPEREXEC 246
  #define KVM_CAP_S390_KEYOP 247
  #define KVM_CAP_S390_VSIE_ESAMODE 248
+#define KVM_CAP_ARM_RMI 249
 
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
@@ -1669,4 +1670,16 @@ struct kvm_pre_fault_memory {
  	__u64 padding[5];
  };
 
+/* Available with KVM_CAP_ARM_RMI, only for VMs with KVM_VM_TYPE_ARM_REALM */
+#define KVM_ARM_RMI_POPULATE	_IOWR(KVMIO, 0xd7, struct kvm_arm_rmi_populate)
+#define KVM_ARM_RMI_POPULATE_FLAGS_MEASURE	(1 << 0)

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

Quoted 10 lines
include/uapi/linux/kvm.h +9 -0
+
+struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+};
+
  #endif /* __LINUX_KVM_H */

Thanks,

	M.
#70 lore raw

Re: [PATCH v14 13/44] arm64: RMI: Define the user ABI

In reply to Marc Zyngier
Quoted 51 lines · Marc Zyngier
Quoted 44 lines · Steven Price

There is one CAP which identified the presence of CCA, and one ioctl. The ioctl is used to populate memory during creation of the realm as this requires the RMM to copy data from an unprotected address to the protected memory - CCA does not support memory conversion where the memory contents is preserved as this is incompatible with memory encryption.

---
Changes since v13:
* KVM_ARM_VCPU_RMI_PSCI_COMPLETE removed.
* KVM_ARM_RMI_POPULATE documentation updated to reflect that the structure is written by the kernel.
* CAP number bumped.
Changes since v12:
* Change KVM_ARM_RMI_POPULATE to update the structure with the amount that has been progressed rather than return the number of bytes populated.
* Describe the flag KVM_ARM_RMI_POPULATE_FLAGS_MEASURE.
* CAP number is bumped.
* NOTE: The PSCI ioctl may be removed in a future spec release.
Changes since v11:
* Completely reworked to be more implicit. Rather than having explicit CAP operations to progress the realm construction these operations are done when needed (on populating and on first vCPU run).
* Populate and PSCI complete are promoted to proper ioctls.
Changes since v10:
* Rename symbols from RME to RMI.
Changes since v9:
* Improvements to documentation.
* Bump the magic number for KVM_CAP_ARM_RME to avoid conflicts.
Changes since v8:
* Minor improvements to documentation following review.
* Bump the magic numbers to avoid conflicts.
Changes since v7:
* Add documentation of new ioctls
* Bump the magic numbers to avoid conflicts
Changes since v6:
* Rename some of the symbols to make their usage clearer and avoid repetition.
Changes from v5:
* Actually expose the new VCPU capability (KVM_ARM_VCPU_REC) by bumping KVM_VCPU_MAX_FEATURES - note this also exposes KVM_ARM_VCPU_HAS_EL2!

Quoted 4 lines
---
 Documentation/virt/kvm/api.rst | 40 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h       | 13 +++++++++++
 2 files changed, 53 insertions(+)
Quoted 1 line

$SUBJECT looks wrong. This is a KVM change, not an RMI change.

Ah, true I guess "KVM: arm64: Define the user ABI for CCA" is more accurate.

Quoted 34 lines
Quoted 34 lines
Documentation/virt/kvm/api.rst +25 -0
index 52bbbb553ce1..ca68aae7faa2 100644
@@ -6553,6 +6553,37 @@ KVM_S390_KEYOP_SSKE
   Sets the storage key for the guest address ``guest_addr`` to the key
   specified in ``key``, returning the previous value in ``key``.
 
+4.145 KVM_ARM_RMI_POPULATE
+--------------------------
+
+:Capability: KVM_CAP_ARM_RMI
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_rmi_populate (in/out)
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+  };
+
+Populate a region of protected address space by copying the data from the
+(non-protected) user space pointer provided into a protected region (backed by
+guestmem_fd). It implicitly sets the destination region to RIPAS RAM. This is
+only valid before any VCPUs have been run. The ioctl might not populate the
+entire region and in this case the kernel updates the fields `base`, `size` and
+`source_uaddr`. User space may have to repeatedly call it until `size` is 0 to
Quoted 8 lines

Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6585

Quoted 5 lines
Documentation/virt/kvm/api.rst +5 -0
+populate the entire region.
+
+`flags` can be set to `KVM_ARM_RMI_POPULATE_FLAGS_MEASURE` to request that the
+populated data is hashed and added to the guest's Realm Initial Measurement
+(RIM).

Where is that measurement stored? And retrieved? At least a pointer to that would help.

It's stored within the RMM and retrieved by the guest (using the RSI interface). I'll update to:

flags can be set to KVM_ARM_RMI_POPULATE_FLAGS_MEASURE to request that the populated data is hashed and added to the guest's Realm Initial Measurement (RIM) stored by the RMM. This can then be retrieved by the guest (using the RSI interface) to present to an attestation server.

Thanks,

Steve

Quoted 54 lines
Quoted 39 lines
Documentation/virt/kvm/api.rst +10 -0
+
 .. _kvm_run:
 
 5. The kvm_run structure
@@ -8904,6 +8935,15 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_ARM_RMI
+--------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: None
+
+This capability indicates that support for CCA realms is available.
+
 8. Other capabilities.
 ======================
 
include/uapi/linux/kvm.h +4 -0
index 6c8afa2047bf..b8cff0938041 100644
@@ -996,6 +996,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
 #define KVM_CAP_S390_VSIE_ESAMODE 248
+#define KVM_CAP_ARM_RMI 249
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1669,4 +1670,16 @@ struct kvm_pre_fault_memory {
 	__u64 padding[5];
 };
 
+/* Available with KVM_CAP_ARM_RMI, only for VMs with KVM_VM_TYPE_ARM_REALM */
+#define KVM_ARM_RMI_POPULATE	_IOWR(KVMIO, 0xd7, struct kvm_arm_rmi_populate)
+#define KVM_ARM_RMI_POPULATE_FLAGS_MEASURE	(1 << 0)

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

Quoted 10 lines
include/uapi/linux/kvm.h +9 -0
+
+struct kvm_arm_rmi_populate {
+	__u64 base;
+	__u64 size;
+	__u64 source_uaddr;
+	__u32 flags;
+	__u32 reserved;
+};
+
 #endif /* __LINUX_KVM_H */

Thanks,

	M.
#71 lore raw

PATCH v14 14/44 arm64: RMI: Basic infrastructure for creating a realm.

In reply to Steven Price

Introduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.

The actual work of constructing the realm will be added in future patches.

---
Changes since v13:
* Rebased and updated to RMM-v2.0-bet1.
* Auxiliary granules have been removed in RMM-v2.0-bet1
Changes since v12:
* Drop the RMM_PAGE_{SHIFT,SIZE} defines - the RMM is now configured to be the same as the host's page size.
* Rework delegate/undelegate functions to use the new RMI range based operations.
Changes since v11:
* Major rework to drop the realm configuration and make the construction of realms implicit rather than driven by the VMM directly.
* The code to create RDs, handle VMIDs etc is moved to later patches.
Changes since v10:
* Rename from RME to RMI.
* Move the stage2 cleanup to a later patch.
Changes since v9:
* Avoid walking the stage 2 page tables when destroying the realm - the real ones are not accessible to the non-secure world, and the RMM may leave junk in the physical pages when returning them.
* Fix an error path in realm_create_rd() to actually return an error value.
Changes since v8:
* Fix free_delegated_granule() to not call kvm_account_pgtable_pages(); a separate wrapper will be introduced in a later patch to deal with RTTs.
* Minor code cleanups following review.
Changes since v7:
* Minor code cleanup following Gavin's review.
Changes since v6:
* Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an RMM which uses 4k granules.
Changes since v5:
* Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
* Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
* Improved commit description.
* Improved return failures for rmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.

---
 arch/arm64/include/asm/kvm_emulate.h | 29 ++++++++++++++
 arch/arm64/include/asm/kvm_rmi.h     | 51 +++++++++++++++++++++++++
 arch/arm64/kvm/arm.c                 | 12 ++++++
 arch/arm64/kvm/mmu.c                 | 12 +++++-
 arch/arm64/kvm/rmi.c                 | 57 ++++++++++++++++++++++++++++
 5 files changed, 159 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +29 -0
index 5bf3d7e1d92c..82fd777bd9bb 100644
@@ -688,4 +688,33 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
 			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR;
 	}
 }
+
+static inline bool kvm_is_realm(struct kvm *kvm)
+{
+	if (static_branch_unlikely(&kvm_rmi_is_available))
+		return kvm->arch.is_realm;
+	return false;
+}
+
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+	return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline void kvm_set_realm_state(struct kvm *kvm,
+				       enum realm_state new_state)
+{
+	WRITE_ONCE(kvm->arch.realm.state, new_state);
+}
+
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
+}
+
+static inline bool vcpu_is_rec(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_rmi.h +51 -0

63 lines not on this page · raw message

arch/arm64/kvm/arm.c +12 -0
index 247e03b33035..18251e561524 100644
@@ -264,6 +264,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	/* Initialise the realm bits after the generic bits are enabled */
+	if (kvm_is_realm(kvm)) {
+		ret = kvm_init_realm(kvm);
+		if (ret)
+			goto err_uninit_mmu;
+	}
+
 	return 0;
 
 err_uninit_mmu:
@@ -326,6 +333,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvm_unshare_hyp(kvm, kvm + 1);
 
 	kvm_arm_teardown_hypercalls(kvm);
+	if (kvm_is_realm(kvm))
+		kvm_destroy_realm(kvm);
 }
 
 static bool kvm_has_full_ptr_auth(void)
@@ -486,6 +495,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		else
 			r = kvm_supports_cacheable_pfnmap();
 		break;
+	case KVM_CAP_ARM_RMI:
+		r = static_key_enabled(&kvm_rmi_is_available);
+		break;
 
 	default:
 		r = 0;
arch/arm64/kvm/mmu.c +10 -2
index d089c107d9b7..ba8286472286 100644
@@ -877,10 +877,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
 
 static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
 {
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 	u32 kvm_ipa_limit = get_kvm_ipa_limit();
 	u64 mmfr0, mmfr1;
 	u32 phys_shift;
 
+	if (kvm_is_realm(kvm))
+		kvm_ipa_limit = kvm_realm_ipa_limit();
+
 	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
 	if (is_protected_kvm_enabled()) {
 		phys_shift = kvm_ipa_limit;
@@ -974,6 +978,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 		return -EINVAL;
 	}
 
+	mmu->arch = &kvm->arch;
+
 	err = kvm_init_ipa_range(mmu, type);
 	if (err)
 		return err;
@@ -982,7 +988,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	if (!pgt)
 		return -ENOMEM;
 
-	mmu->arch = &kvm->arch;
 	err = KVM_PGT_FN(kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops);
 	if (err)
 		goto out_free_pgtable;
@@ -1114,7 +1119,10 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 	write_unlock(&kvm->mmu_lock);
 
 	if (pgt) {
-		kvm_stage2_destroy(pgt);
+		if (!kvm_is_realm(kvm))
+			kvm_stage2_destroy(pgt);
+		else
+			kvm_pgtable_stage2_destroy_pgd(pgt);
 		kfree(pgt);
 	}
 }
arch/arm64/kvm/rmi.c +57 -0

69 lines not on this page · raw message

Signature · 1 line
2.43.0
#72 lore raw

Re: [PATCH v14 14/44] arm64: RMI: Basic infrastructure for creating a realm.

arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1128

Quoted 12 lines · Steven Price
arch/arm64/kvm/mmu.c +4 -1
@@ -1114,7 +1119,10 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 	write_unlock(&kvm->mmu_lock);
 
 	if (pgt) {
-		kvm_stage2_destroy(pgt);
+		if (!kvm_is_realm(kvm))
+			kvm_stage2_destroy(pgt);
+		else
+			kvm_pgtable_stage2_destroy_pgd(pgt);
 		kfree(pgt);
 	}
 }

Maybe add a comment here explaining the difference.

We now have:

kvm_arch_destroy_vm()
-> kvm_uninit_stage2_mmu()

       -> kvm_realm_uninit_stage2()
            -> unmap_range(0, max_ipa)        // for Realm VMs
       -> kvm_free_stage2_pgd()
            -> unmap and free PGD             // for non-Realm VMs
  -> kvm_destroy_realm()                      // for Realm VMs
       -> kvm_free_stage2_pgd()
            -> free PGD                       // for Realm VMs

I wonder whether this can be simplified using different functions names? (can we call kvm_pgtable_stage2_destroy_pgd() from kvm_destroy_realm()? )

-aneesh

maintainer
#73 lore raw

Re: [PATCH v14 14/44] arm64: RMI: Basic infrastructure for creating a realm.

In reply to Steven Price
Quoted 6 lines · Steven Price

Introduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.

The actual work of constructing the realm will be added in future patches.

Again, $SUBJECT doesn't reflect that this is purely a KVM patch.

Quoted 128 lines

---
Changes since v13:
* Rebased and updated to RMM-v2.0-bet1.
* Auxiliary granules have been removed in RMM-v2.0-bet1
Changes since v12:
* Drop the RMM_PAGE_{SHIFT,SIZE} defines - the RMM is now configured to be the same as the host's page size.
* Rework delegate/undelegate functions to use the new RMI range based operations.
Changes since v11:
* Major rework to drop the realm configuration and make the construction of realms implicit rather than driven by the VMM directly.
* The code to create RDs, handle VMIDs etc is moved to later patches.
Changes since v10:
* Rename from RME to RMI.
* Move the stage2 cleanup to a later patch.
Changes since v9:
* Avoid walking the stage 2 page tables when destroying the realm - the real ones are not accessible to the non-secure world, and the RMM may leave junk in the physical pages when returning them.
* Fix an error path in realm_create_rd() to actually return an error value.
Changes since v8:
* Fix free_delegated_granule() to not call kvm_account_pgtable_pages(); a separate wrapper will be introduced in a later patch to deal with RTTs.
* Minor code cleanups following review.
Changes since v7:
* Minor code cleanup following Gavin's review.
Changes since v6:
* Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an RMM which uses 4k granules.
Changes since v5:
* Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
* Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
* Improved commit description.
* Improved return failures for rmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.

---
 arch/arm64/include/asm/kvm_emulate.h | 29 ++++++++++++++
 arch/arm64/include/asm/kvm_rmi.h     | 51 +++++++++++++++++++++++++
 arch/arm64/kvm/arm.c                 | 12 ++++++
 arch/arm64/kvm/mmu.c                 | 12 +++++-
 arch/arm64/kvm/rmi.c                 | 57 ++++++++++++++++++++++++++++
 5 files changed, 159 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +29 -0
index 5bf3d7e1d92c..82fd777bd9bb 100644
@@ -688,4 +688,33 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
 			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR;
 	}
 }
+
+static inline bool kvm_is_realm(struct kvm *kvm)
+{
+	if (static_branch_unlikely(&kvm_rmi_is_available))
+		return kvm->arch.is_realm;
+	return false;
+}
+
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+	return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline void kvm_set_realm_state(struct kvm *kvm,
+				       enum realm_state new_state)
+{
+	WRITE_ONCE(kvm->arch.realm.state, new_state);
+}
+
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
+}
+
+static inline bool vcpu_is_rec(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_rmi.h +27 -0
index 4936007947fd..9de34983ee52 100644
@@ -6,12 +6,63 @@
 #ifndef __ASM_KVM_RMI_H
 #define __ASM_KVM_RMI_H
 
+#include <asm/rmi_smc.h>
+
+/**
+ * enum realm_state - State of a Realm
+ */
+enum realm_state {
+	/**
+	 * @REALM_STATE_NONE:
+	 *      Realm has not yet been created. rmi_realm_create() has not
+	 *      yet been called.
+	 */
+	REALM_STATE_NONE,
+	/**
+	 * @REALM_STATE_NEW:
+	 *      Realm is under construction, rmi_realm_create() has been
+	 *      called, but it is not yet activated. Pages may be populated.
+	 */
+	REALM_STATE_NEW,
+	/**
+	 * @REALM_STATE_ACTIVE:
+	 *      Realm has been created and is eligible for execution with
+	 *      rmi_rec_enter(). Pages may no longer be populated with
+	 *      rmi_data_create().
+	 */
+	REALM_STATE_ACTIVE,
+	/**
+	 * @REALM_STATE_DYING:

arch/arm64/include/asm/kvm_rmi.h line 45

Quoted 10 lines
arch/arm64/include/asm/kvm_rmi.h +10 -0
+	 *      Realm is in the process of being destroyed or has already been
+	 *      destroyed.
+	 */
+	REALM_STATE_DYING,
+	/**
+	 * @REALM_STATE_DEAD:
+	 *      Realm has been destroyed.
+	 */
+	REALM_STATE_DEAD
+};

What is the ABI status of this state? Is it purely internal to KVM? Or is it something that the RMM actively tracks?

arch/arm64/include/asm/kvm_rmi.h line 57

Quoted 12 lines
arch/arm64/include/asm/kvm_rmi.h +8 -0
+
 /**
  * struct realm - Additional per VM data for a Realm
+ *
+ * @state: The lifetime state machine for the realm
+ * @rd: Kernel mapping of the Realm Descriptor (RD)
+ * @params: Parameters for the RMI_REALM_CREATE command
+ * @ia_bits: Number of valid Input Address bits in the IPA
  */
 struct realm {
+	enum realm_state state;
+	void *rd;

Why is this void? Doesn't it have a proper type?

arch/arm64/include/asm/kvm_rmi.h line 59

Quoted 2 lines
arch/arm64/include/asm/kvm_rmi.h +2 -0
+	struct realm_params *params;
+	unsigned int ia_bits;

Consider reordering this structure to avoid holes.

arch/arm64/include/asm/kvm_rmi.h line 63

Quoted 4 lines
arch/arm64/include/asm/kvm_rmi.h +1 -0
 };
 
 void kvm_init_rmi(void);
+u32 kvm_realm_ipa_limit(void);

The use of 'realm' is confusing. This is not a per-realm property, but something global. I'd rather reserve the term 'realm' for CCA VMs (cue the two prototypes below).

Quoted 71 lines
arch/arm64/include/asm/kvm_rmi.h +3 -0
+
+int kvm_init_realm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);
 
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/arm.c +12 -0
index 247e03b33035..18251e561524 100644
@@ -264,6 +264,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	/* Initialise the realm bits after the generic bits are enabled */
+	if (kvm_is_realm(kvm)) {
+		ret = kvm_init_realm(kvm);
+		if (ret)
+			goto err_uninit_mmu;
+	}
+
 	return 0;
 
 err_uninit_mmu:
@@ -326,6 +333,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvm_unshare_hyp(kvm, kvm + 1);
 
 	kvm_arm_teardown_hypercalls(kvm);
+	if (kvm_is_realm(kvm))
+		kvm_destroy_realm(kvm);
 }
 
 static bool kvm_has_full_ptr_auth(void)
@@ -486,6 +495,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		else
 			r = kvm_supports_cacheable_pfnmap();
 		break;
+	case KVM_CAP_ARM_RMI:
+		r = static_key_enabled(&kvm_rmi_is_available);
+		break;
 
 	default:
 		r = 0;
arch/arm64/kvm/mmu.c +6 -0
index d089c107d9b7..ba8286472286 100644
@@ -877,10 +877,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
 
 static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
 {
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 	u32 kvm_ipa_limit = get_kvm_ipa_limit();
 	u64 mmfr0, mmfr1;
 	u32 phys_shift;
 
+	if (kvm_is_realm(kvm))
+		kvm_ipa_limit = kvm_realm_ipa_limit();
+
 	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
 	if (is_protected_kvm_enabled()) {
 		phys_shift = kvm_ipa_limit;
@@ -974,6 +978,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 		return -EINVAL;
 	}
 
+	mmu->arch = &kvm->arch;
+
 	err = kvm_init_ipa_range(mmu, type);
 	if (err)
 		return err;

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 985

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -1
@@ -982,7 +988,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	if (!pgt)
 		return -ENOMEM;
 
-	mmu->arch = &kvm->arch;

Why moving this init?

arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1125

Quoted 12 lines
arch/arm64/kvm/mmu.c +4 -1
 	err = KVM_PGT_FN(kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops);
 	if (err)
 		goto out_free_pgtable;
@@ -1114,7 +1119,10 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 	write_unlock(&kvm->mmu_lock);
 
 	if (pgt) {
-		kvm_stage2_destroy(pgt);
+		if (!kvm_is_realm(kvm))
+			kvm_stage2_destroy(pgt);
+		else
+			kvm_pgtable_stage2_destroy_pgd(pgt);

Why can't you make kvm_stage2_destroy() do the right thing? Surely the PTs have to be reclaimed one way or another.

Quoted 55 lines
arch/arm64/kvm/mmu.c +0 -0
 		kfree(pgt);
 	}
 }
arch/arm64/kvm/rmi.c +37 -0
index 6e28b669ded2..f51ec667445e 100644
@@ -5,6 +5,8 @@
 
 #include <linux/kvm_host.h>
 
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
 #include <asm/kvm_pgtable.h>
 #include <asm/rmi_cmds.h>
 #include <asm/virt.h>
@@ -14,6 +16,61 @@ static bool rmi_has_feature(unsigned long feature)
 	return !!u64_get_bits(rmm_feat_reg0, feature);
 }
 
+u32 kvm_realm_ipa_limit(void)
+{
+	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
+}
+
+void kvm_destroy_realm(struct kvm *kvm)
+{
+	struct realm *realm = &kvm->arch.realm;
+	size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
+
+	if (realm->params) {
+		free_page((unsigned long)realm->params);
+		realm->params = NULL;
+	}
+
+	if (!kvm_realm_is_created(kvm))
+		return;
+
+	kvm_set_realm_state(kvm, REALM_STATE_DYING);
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(&kvm->arch.mmu, 0,
+			       BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+
+	if (realm->rd) {
+		phys_addr_t rd_phys = virt_to_phys(realm->rd);
+
+		if (WARN_ON(rmi_realm_terminate(rd_phys)))
+			return;
+
+		if (WARN_ON(rmi_realm_destroy(rd_phys)))
+			return;
+		free_delegated_page(rd_phys);
+		realm->rd = NULL;

arch/arm64/kvm/rmi.c static bool rmi_has_feature(unsigned long feature) · line 63

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+	}
+
+	if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys, pgd_size)))
+		return;
+
+	kvm_set_realm_state(kvm, REALM_STATE_DEAD);
+
+	/* Now that the Realm is destroyed, free the entry level RTTs */
+	kvm_free_stage2_pgd(&kvm->arch.mmu);
+}

This really needs documentation: what happens at each stage? What memory is reclaimed when?

But even more importantly, why is this built in a completely parallel way, potentially deviating from the existing KVM S2 management?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#74 lore raw

Re: [PATCH v14 14/44] arm64: RMI: Basic infrastructure for creating a realm.

Hi Marc

Quoted 144 lines · Marc Zyngier
Quoted 6 lines · Steven Price

Introduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.

The actual work of constructing the realm will be added in future patches.

Again, $SUBJECT doesn't reflect that this is purely a KVM patch.

Quoted 133 lines

---
Changes since v13:
* Rebased and updated to RMM-v2.0-bet1.
* Auxiliary granules have been removed in RMM-v2.0-bet1
Changes since v12:
* Drop the RMM_PAGE_{SHIFT,SIZE} defines - the RMM is now configured to be the same as the host's page size.
* Rework delegate/undelegate functions to use the new RMI range based operations.
Changes since v11:
* Major rework to drop the realm configuration and make the construction of realms implicit rather than driven by the VMM directly.
* The code to create RDs, handle VMIDs etc is moved to later patches.
Changes since v10:
* Rename from RME to RMI.
* Move the stage2 cleanup to a later patch.
Changes since v9:
* Avoid walking the stage 2 page tables when destroying the realm - the real ones are not accessible to the non-secure world, and the RMM may leave junk in the physical pages when returning them.
* Fix an error path in realm_create_rd() to actually return an error value.
Changes since v8:
* Fix free_delegated_granule() to not call kvm_account_pgtable_pages(); a separate wrapper will be introduced in a later patch to deal with RTTs.
* Minor code cleanups following review.
Changes since v7:
* Minor code cleanup following Gavin's review.
Changes since v6:
* Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an RMM which uses 4k granules.
Changes since v5:
* Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
* Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
* Improved commit description.
* Improved return failures for rmi_check_version().
* Clear contents of PGD after it has been undelegated in case the RMM left stale data.
* Minor changes to reflect changes in previous patches.

---
  arch/arm64/include/asm/kvm_emulate.h | 29 ++++++++++++++
  arch/arm64/include/asm/kvm_rmi.h     | 51 +++++++++++++++++++++++++
  arch/arm64/kvm/arm.c                 | 12 ++++++
  arch/arm64/kvm/mmu.c                 | 12 +++++-
  arch/arm64/kvm/rmi.c                 | 57 ++++++++++++++++++++++++++++
  5 files changed, 159 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +29 -0
index 5bf3d7e1d92c..82fd777bd9bb 100644
@@ -688,4 +688,33 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
  			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR;
  	}
  }
+
+static inline bool kvm_is_realm(struct kvm *kvm)
+{
+	if (static_branch_unlikely(&kvm_rmi_is_available))
+		return kvm->arch.is_realm;
+	return false;
+}
+
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+	return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline void kvm_set_realm_state(struct kvm *kvm,
+				       enum realm_state new_state)
+{
+	WRITE_ONCE(kvm->arch.realm.state, new_state);
+}
+
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+	return kvm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
+}
+
+static inline bool vcpu_is_rec(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
  #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_rmi.h +32 -0
index 4936007947fd..9de34983ee52 100644
@@ -6,12 +6,63 @@
  #ifndef __ASM_KVM_RMI_H
  #define __ASM_KVM_RMI_H
 
+#include <asm/rmi_smc.h>
+
+/**
+ * enum realm_state - State of a Realm
+ */
+enum realm_state {
+	/**
+	 * @REALM_STATE_NONE:
+	 *      Realm has not yet been created. rmi_realm_create() has not
+	 *      yet been called.
+	 */
+	REALM_STATE_NONE,
+	/**
+	 * @REALM_STATE_NEW:
+	 *      Realm is under construction, rmi_realm_create() has been
+	 *      called, but it is not yet activated. Pages may be populated.
+	 */
+	REALM_STATE_NEW,
+	/**
+	 * @REALM_STATE_ACTIVE:
+	 *      Realm has been created and is eligible for execution with
+	 *      rmi_rec_enter(). Pages may no longer be populated with
+	 *      rmi_data_create().
+	 */
+	REALM_STATE_ACTIVE,
+	/**
+	 * @REALM_STATE_DYING:
+	 *      Realm is in the process of being destroyed or has already been
+	 *      destroyed.
+	 */
+	REALM_STATE_DYING,
+	/**
Quoted 8 lines

arch/arm64/include/asm/kvm_rmi.h line 45

Quoted 5 lines
arch/arm64/include/asm/kvm_rmi.h +5 -0
+	 * @REALM_STATE_DEAD:
+	 *      Realm has been destroyed.
+	 */
+	REALM_STATE_DEAD
+};

What is the ABI status of this state? Is it purely internal to KVM? Or is it something that the RMM actively tracks?

The states are in line with what the RMM maintains for the Realm state, (Section A2.2.5 Realm Lifecycle)
except for :

1. REALM_STATE_DYING is really a KVM internal state to indicate, we are in the process of destroying the Realm and no further requests needs to be serviced

2. We don't track the REALM_SYSTEM_OFF, REALM_ZOMBIE states separately as we :
a) Always TERMINATE the Realm, just before the DESTROY
b) SYSTEM_OFF is naturally triggering the tear down path, leading to
DYING.

Quoted 15 lines

arch/arm64/include/asm/kvm_rmi.h line 57

Quoted 13 lines
arch/arm64/include/asm/kvm_rmi.h +8 -0
+
  /**
   * struct realm - Additional per VM data for a Realm
+ *
+ * @state: The lifetime state machine for the realm
+ * @rd: Kernel mapping of the Realm Descriptor (RD)
+ * @params: Parameters for the RMI_REALM_CREATE command
+ * @ia_bits: Number of valid Input Address bits in the IPA
   */
  struct realm {
+	enum realm_state state;
+	void *rd;

Why is this void? Doesn't it have a proper type?

Not really. This is an object that RMM manages (Realm Descriptor) in the Realm world. We use it as a parameter to address the Realm.

Quoted 6 lines

arch/arm64/include/asm/kvm_rmi.h line 59

Quoted 3 lines
arch/arm64/include/asm/kvm_rmi.h +2 -0
+	struct realm_params *params;
+	unsigned int ia_bits;

Consider reordering this structure to avoid holes.

Quoted 8 lines

arch/arm64/include/asm/kvm_rmi.h line 63

Quoted 4 lines
arch/arm64/include/asm/kvm_rmi.h +1 -0
  };
 
  void kvm_init_rmi(void);
+u32 kvm_realm_ipa_limit(void);

The use of 'realm' is confusing. This is not a per-realm property, but something global. I'd rather reserve the term 'realm' for CCA VMs (cue the two prototypes below).

Agreed. Perhaps, kvm_rmm_ipa_limit() ?

Quoted 75 lines
Quoted 75 lines
arch/arm64/include/asm/kvm_rmi.h +3 -0
+
+int kvm_init_realm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);
 
  #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/arm.c +12 -0
index 247e03b33035..18251e561524 100644
@@ -264,6 +264,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
  	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	/* Initialise the realm bits after the generic bits are enabled */
+	if (kvm_is_realm(kvm)) {
+		ret = kvm_init_realm(kvm);
+		if (ret)
+			goto err_uninit_mmu;
+	}
+
  	return 0;
 
  err_uninit_mmu:
@@ -326,6 +333,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
  	kvm_unshare_hyp(kvm, kvm + 1);
 
  	kvm_arm_teardown_hypercalls(kvm);
+	if (kvm_is_realm(kvm))
+		kvm_destroy_realm(kvm);
  }
 
  static bool kvm_has_full_ptr_auth(void)
@@ -486,6 +495,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  		else
  			r = kvm_supports_cacheable_pfnmap();
  		break;
+	case KVM_CAP_ARM_RMI:
+		r = static_key_enabled(&kvm_rmi_is_available);
+		break;
 
  	default:
  		r = 0;
arch/arm64/kvm/mmu.c +6 -0
index d089c107d9b7..ba8286472286 100644
@@ -877,10 +877,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
 
  static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
  {
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
  	u32 kvm_ipa_limit = get_kvm_ipa_limit();
  	u64 mmfr0, mmfr1;
  	u32 phys_shift;
 
+	if (kvm_is_realm(kvm))
+		kvm_ipa_limit = kvm_realm_ipa_limit();
+
  	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
  	if (is_protected_kvm_enabled()) {
  		phys_shift = kvm_ipa_limit;
@@ -974,6 +978,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
  		return -EINVAL;
  	}
 
+	mmu->arch = &kvm->arch;
+
  	err = kvm_init_ipa_range(mmu, type);
  	if (err)
  		return err;
@@ -982,7 +988,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
  	if (!pgt)
  		return -ENOMEM;
 
Quoted 3 lines

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 985

Quoted 1 line
arch/arm64/kvm/mmu.c +0 -1
-	mmu->arch = &kvm->arch;

Why moving this init?

Because, we need to know the "kvm" instance for kvm_init_ipa_range to detect the limit that applies to Realms.

Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/mmu.c +0 -0
  	err = KVM_PGT_FN(kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops);
  	if (err)
  		goto out_free_pgtable;
@@ -1114,7 +1119,10 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
  	write_unlock(&kvm->mmu_lock);
 
Quoted 9 lines

arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1125

Quoted 6 lines
arch/arm64/kvm/mmu.c +4 -1
  	if (pgt) {
-		kvm_stage2_destroy(pgt);
+		if (!kvm_is_realm(kvm))
+			kvm_stage2_destroy(pgt);
+		else
+			kvm_pgtable_stage2_destroy_pgd(pgt);

Why can't you make kvm_stage2_destroy() do the right thing? Surely the PTs have to be reclaimed one way or another.

Actually yes, we could make it work. We need to skip walking the page
table for Realms. We may be able to do the checks via pgt->mmu->arch->kvm and skip the walking for Realms. ( The S2 is unmapped and torn down before the RD is destroyed in kvm_destroy_realm(). We can't
rely on the contents of the PGDs to be zero - e.g., with MEC.)

Quoted 61 lines
Quoted 61 lines
arch/arm64/kvm/mmu.c +0 -0
  		kfree(pgt);
  	}
  }
arch/arm64/kvm/rmi.c +42 -0
index 6e28b669ded2..f51ec667445e 100644
@@ -5,6 +5,8 @@
 
  #include <linux/kvm_host.h>
 
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
  #include <asm/kvm_pgtable.h>
  #include <asm/rmi_cmds.h>
  #include <asm/virt.h>
@@ -14,6 +16,61 @@ static bool rmi_has_feature(unsigned long feature)
  	return !!u64_get_bits(rmm_feat_reg0, feature);
  }
 
+u32 kvm_realm_ipa_limit(void)
+{
+	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
+}
+
+void kvm_destroy_realm(struct kvm *kvm)
+{
+	struct realm *realm = &kvm->arch.realm;
+	size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
+
+	if (realm->params) {
+		free_page((unsigned long)realm->params);
+		realm->params = NULL;
+	}
+
+	if (!kvm_realm_is_created(kvm))
+		return;
+
+	kvm_set_realm_state(kvm, REALM_STATE_DYING);
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(&kvm->arch.mmu, 0,
+			       BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+
+	if (realm->rd) {
+		phys_addr_t rd_phys = virt_to_phys(realm->rd);
+
+		if (WARN_ON(rmi_realm_terminate(rd_phys)))
+			return;
+
+		if (WARN_ON(rmi_realm_destroy(rd_phys)))
+			return;
+		free_delegated_page(rd_phys);
+		realm->rd = NULL;
+	}
+
+	if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys, pgd_size)))
+		return;
+
Quoted 8 lines

arch/arm64/kvm/rmi.c static bool rmi_has_feature(unsigned long feature) · line 63

Quoted 5 lines
arch/arm64/kvm/rmi.c +5 -0
+	kvm_set_realm_state(kvm, REALM_STATE_DEAD);
+
+	/* Now that the Realm is destroyed, free the entry level RTTs */
+	kvm_free_stage2_pgd(&kvm->arch.mmu);
+}

This really needs documentation: what happens at each stage? What memory is reclaimed when?

Agreed.

Quoted 3 lines

But even more importantly, why is this built in a completely parallel way, potentially deviating from the existing KVM S2 management?

RMM requires a Realm is not live at the time of REALM_DESTROY. (See section A2.2.4 Realm Liveness).
i.e., All RECs are destroyed, Root RTTs wiped clean (no live mappings) before the RD is destroyed. So, we need to make sure all of this is done at Realm Destroy. Hence we delay the kvm_free_stage2_pgd() until we destroy the RD.

Does that help? May be we could improve the comments around it.

Suzuki

Quoted 3 lines

Thanks,>

	M.
#75 lore raw

Re: [PATCH v14 14/44] arm64: RMI: Basic infrastructure for creating a realm.

Quoted 13 lines · Suzuki K Poulose

Hi Marc

Quoted 10 lines · Marc Zyngier
Quoted 6 lines · Steven Price

Introduce the skeleton functions for creating and destroying a realm. The IPA size requested is checked against what the RMM supports.

The actual work of constructing the realm will be added in future patches.

Again, $SUBJECT doesn't reflect that this is purely a KVM patch.

Indeed - "KVM: arm64: CCA" is a better prefix.

Quoted 160 lines
Quoted 143 lines
Quoted 143 lines

---
Changes since v13:
  * Rebased and updated to RMM-v2.0-bet1.
  * Auxiliary granules have been removed in RMM-v2.0-bet1 Changes since v12:
  * Drop the RMM_PAGE_{SHIFT,SIZE} defines - the RMM is now configured to
    be the same as the host's page size.
  * Rework delegate/undelegate functions to use the new RMI range based operations.
Changes since v11:
  * Major rework to drop the realm configuration and make the construction of realms implicit rather than driven by the VMM directly.
  * The code to create RDs, handle VMIDs etc is moved to later patches. Changes since v10:
  * Rename from RME to RMI.
  * Move the stage2 cleanup to a later patch.
Changes since v9:
  * Avoid walking the stage 2 page tables when destroying the realm - the real ones are not accessible to the non-secure world, and the RMM
    may leave junk in the physical pages when returning them.
  * Fix an error path in realm_create_rd() to actually return an error value.
Changes since v8:
  * Fix free_delegated_granule() to not call kvm_account_pgtable_pages();
    a separate wrapper will be introduced in a later patch to deal with RTTs.
  * Minor code cleanups following review.
Changes since v7:
  * Minor code cleanup following Gavin's review.
Changes since v6:
  * Separate RMM RTT calculations from host PAGE_SIZE. This allows the host page size to be larger than 4k while still communicating with an
    RMM which uses 4k granules.
Changes since v5:
  * Introduce free_delegated_granule() to replace many undelegate/free_page() instances and centralise the comment on leaking when the undelegate fails.
  * Several other minor improvements suggested by reviews - thanks for the feedback!
Changes since v2:
  * Improved commit description.
  * Improved return failures for rmi_check_version().
  * Clear contents of PGD after it has been undelegated in case the RMM left stale data.
  * Minor changes to reflect changes in previous patches.
---
  arch/arm64/include/asm/kvm_emulate.h | 29 ++++++++++++++ arch/arm64/include/asm/kvm_rmi.h     | 51 +++++++++++++++++++++++++

  arch/arm64/kvm/arm.c                 | 12 ++++++
  arch/arm64/kvm/mmu.c                 | 12 +++++-
  arch/arm64/kvm/rmi.c                 | 57 ++++++++++++++++++++++++++++
  5 files changed, 159 insertions(+), 2 deletions(-)
arch/arm64/ renamed from arch/arm64/include/asm/kvm_emulate.h +0 -0

include/asm/kvm_emulate.h
index 5bf3d7e1d92c..82fd777bd9bb 100644

arch/arm64/include/asm/kvm_emulate.h +22 -0
@@ -688,4 +688,33 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu
 *vcpu)
               vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR;
       }
   }
+
+static inline bool kvm_is_realm(struct kvm *kvm)
+{
+    if (static_branch_unlikely(&kvm_rmi_is_available))
+        return kvm->arch.is_realm;
+    return false;
+}
+
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+    return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline void kvm_set_realm_state(struct kvm *kvm,
+                       enum realm_state new_state)
+{
+    WRITE_ONCE(kvm->arch.realm.state, new_state);
+}
+
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+    return kvm_is_realm(kvm) && kvm_realm_state(kvm) !=

REALM_STATE_NONE;

arch/arm64/include/asm/kvm_emulate.h +7 -0
+}
+
+static inline bool vcpu_is_rec(const struct kvm_vcpu *vcpu)
+{
+    return false;
+}
+

  #endif /* __ARM64_KVM_EMULATE_H__ */

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

asm/kvm_rmi.h
index 4936007947fd..9de34983ee52 100644

arch/arm64/include/asm/kvm_rmi.h +0 -0
@@ -6,12 +6,63 @@
   #ifndef __ASM_KVM_RMI_H
   #define __ASM_KVM_RMI_H

  +#include <asm/rmi_smc.h>

arch/arm64/include/asm/kvm_rmi.h +15 -0
+
+/**
+ * enum realm_state - State of a Realm
+ */
+enum realm_state {
+    /**
+     * @REALM_STATE_NONE:
+     *      Realm has not yet been created. rmi_realm_create() has not
+     *      yet been called.
+     */
+    REALM_STATE_NONE,
+    /**
+     * @REALM_STATE_NEW:
+     *      Realm is under construction, rmi_realm_create() has been
+     *      called, but it is not yet activated. Pages may be

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

Quoted 8 lines
Quoted 5 lines

+     * @REALM_STATE_DEAD:
+     *      Realm has been destroyed.
+     */
+    REALM_STATE_DEAD
+};

What is the ABI status of this state? Is it purely internal to KVM? Or is it something that the RMM actively tracks?

The states are in line with what the RMM maintains for the Realm state, (Section A2.2.5 Realm Lifecycle)
except for :

1. REALM_STATE_DYING is really a KVM internal state to indicate, we are in the process of destroying the Realm and no further requests needs to be serviced

Quoted 6 lines

2. We don't track the REALM_SYSTEM_OFF, REALM_ZOMBIE states separately as we :
 a) Always TERMINATE the Realm, just before the DESTROY
 b) SYSTEM_OFF is naturally triggering the tear down path, leading to DYING.

I'll add a comment:

+ * Mirrors the RMM's Realm lifecycle states where they are meaningful to KVM,
+ * with REALM_STATE_DYING being a KVM-internal state used to prevent further
+ * requests while teardown is in progress. KVM does not track REALM_SYSTEM_OFF
+ * or REALM_ZOMBIE separately as they naturally lead to teardown.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

+
  /**
   * struct realm - Additional per VM data for a Realm
+ *
+ * @state: The lifetime state machine for the realm
+ * @rd: Kernel mapping of the Realm Descriptor (RD)
+ * @params: Parameters for the RMI_REALM_CREATE command
+ * @ia_bits: Number of valid Input Address bits in the IPA */
  struct realm {
+    enum realm_state state;
+    void *rd;

Quoted 11 lines
Quoted 1 line

Why is this void? Doesn't it have a proper type?

Not really. This is an object that RMM manages (Realm Descriptor) in the Realm world. We use it as a parameter to address the Realm.

Quoted 5 lines
Quoted 3 lines

+    struct realm_params *params;
+    unsigned int ia_bits;

Consider reordering this structure to avoid holes.

Sure

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

  };
    void kvm_init_rmi(void);
+u32 kvm_realm_ipa_limit(void);

The use of 'realm' is confusing. This is not a per-realm property, but something global. I'd rather reserve the term 'realm' for CCA VMs (cue the two prototypes below).

Agreed. Perhaps, kvm_rmm_ipa_limit() ?

Sounds good to me.

Quoted 95 lines
Quoted 66 lines
Quoted 66 lines
arch/arm64/include/asm/kvm_rmi.h +3 -0
+
+int kvm_init_realm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);

    #endif /* __ASM_KVM_RMI_H */

arch/arm64/kvm/arm.c +0 -0
index 247e03b33035..18251e561524 100644
@@ -264,6 +264,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned
 long type)
         bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);

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

arch/arm64/kvm/arm.c +2 -0
@@ -326,6 +333,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
       kvm_unshare_hyp(kvm, kvm + 1);
         kvm_arm_teardown_hypercalls(kvm);
+    if (kvm_is_realm(kvm))
+        kvm_destroy_realm(kvm);
   }
     static bool kvm_has_full_ptr_auth(void)
@@ -486,6 +495,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm,
 long ext)

          else
              r = kvm_supports_cacheable_pfnmap(); break;
+    case KVM_CAP_ARM_RMI:
+        r = static_key_enabled(&kvm_rmi_is_available); +        break;
        default:
          r = 0;

arch/arm64/kvm/mmu.c +0 -0
index d089c107d9b7..ba8286472286 100644
@@ -877,10 +877,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {

    static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
  {
+    struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
      u32 kvm_ipa_limit = get_kvm_ipa_limit();
      u64 mmfr0, mmfr1;
      u32 phys_shift;
  +    if (kvm_is_realm(kvm))
+        kvm_ipa_limit = kvm_realm_ipa_limit();
+
      phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
      if (is_protected_kvm_enabled()) {
          phys_shift = kvm_ipa_limit;

arch/arm64/kvm/mmu.c +0 -0
@@ -974,6 +978,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct

kvm_s2_mmu *mmu, unsigned long t
          return -EINVAL;
      }
  +    mmu->arch = &kvm->arch;
+
      err = kvm_init_ipa_range(mmu, type);
      if (err)

Quoted 8 lines
Quoted 6 lines

          return err;

arch/arm64/kvm/mmu.c +0 -0
@@ -982,7 +988,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct

kvm_s2_mmu *mmu, unsigned long t
      if (!pgt)
          return -ENOMEM;
  -    mmu->arch = &kvm->arch;

Why moving this init?

Because, we need to know the "kvm" instance for kvm_init_ipa_range to detect the limit that applies to Realms.

Quoted 16 lines

arch/arm64/kvm/mmu.c void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) · line 1124

Quoted 13 lines

      err = KVM_PGT_FN(kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops);
      if (err)
          goto out_free_pgtable;

arch/arm64/kvm/mmu.c +4 -1
@@ -1114,7 +1119,10 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
       write_unlock(&kvm->mmu_lock);
         if (pgt) {
-        kvm_stage2_destroy(pgt);
+        if (!kvm_is_realm(kvm))
+            kvm_stage2_destroy(pgt);
+        else
+            kvm_pgtable_stage2_destroy_pgd(pgt);

Why can't you make kvm_stage2_destroy() do the right thing? Surely the PTs have to be reclaimed one way or another.

Quoted 5 lines

Actually yes, we could make it work. We need to skip walking the page table for Realms. We may be able to do the checks via pgt->mmu->arch-

Quoted 1 line

kvm and skip the walking for Realms. ( The S2 is unmapped and torn

down before the RD is destroyed in kvm_destroy_realm(). We can't rely on the contents of the PGDs to be zero - e.g., with MEC.)

Yes I'll move the check into kvm_stage2_destroy() instead with a comment explaining what's going on.

Quoted 75 lines
Quoted 59 lines
Quoted 59 lines
arch/arm64/kvm/mmu.c +0 -0
           kfree(pgt);
       }
   }
arch/arm64/kvm/rmi.c +0 -0
index 6e28b669ded2..f51ec667445e 100644
@@ -5,6 +5,8 @@
     #include <linux/kvm_host.h>

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

arch/arm64/kvm/rmi.c +39 -0
@@ -14,6 +16,61 @@ static bool rmi_has_feature(unsigned long feature)
       return !!u64_get_bits(rmm_feat_reg0, feature);
   }
   +u32 kvm_realm_ipa_limit(void)
+{
+    return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
+}
+
+void kvm_destroy_realm(struct kvm *kvm)
+{
+    struct realm *realm = &kvm->arch.realm;
+    size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
+
+    if (realm->params) {
+        free_page((unsigned long)realm->params);
+        realm->params = NULL;
+    }
+
+    if (!kvm_realm_is_created(kvm))
+        return;
+
+    kvm_set_realm_state(kvm, REALM_STATE_DYING);
+
+    write_lock(&kvm->mmu_lock);
+    kvm_stage2_unmap_range(&kvm->arch.mmu, 0,
+                   BIT(realm->ia_bits - 1), true);
+    write_unlock(&kvm->mmu_lock);
+
+    if (realm->rd) {
+        phys_addr_t rd_phys = virt_to_phys(realm->rd);
+
+        if (WARN_ON(rmi_realm_terminate(rd_phys)))
+            return;
+
+        if (WARN_ON(rmi_realm_destroy(rd_phys)))
+            return;
+        free_delegated_page(rd_phys);
+        realm->rd = NULL;
+    }
+
+    if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys,
 pgd_size)))
+        return;
+
Quoted 8 lines

arch/arm64/kvm/rmi.c static bool rmi_has_feature(unsigned long feature) · line 63

Quoted 5 lines
arch/arm64/kvm/rmi.c +5 -0
+    kvm_set_realm_state(kvm, REALM_STATE_DEAD);
+
+    /* Now that the Realm is destroyed, free the entry level RTTs */
+    kvm_free_stage2_pgd(&kvm->arch.mmu);
+}

This really needs documentation: what happens at each stage? What memory is reclaimed when?

Agreed.

Quoted 3 lines

But even more importantly, why is this built in a completely parallel way, potentially deviating from the existing KVM S2 management?

Quoted 8 lines

RMM requires a Realm is not live at the time of REALM_DESTROY. (See section A2.2.4 Realm Liveness).
i.e., All RECs are destroyed, Root RTTs wiped clean (no live mappings) before the RD is destroyed. So, we need to make sure all of this is done at Realm Destroy. Hence we delay the kvm_free_stage2_pgd() until we destroy the RD.

Does that help? May be we could improve the comments around it.

I'll add a comment in kvm_destroy_realm().

Thanks,
Steve

Quoted 9 lines

Suzuki

Quoted 3 lines

Thanks,>
    M.

#76 lore raw

PATCH v14 15/44 kvm: arm64: Don't expose unsupported capabilities for realm guests

In reply to Steven Price

From: Suzuki K Poulose <[email protected]>

RMM v2.0 provides no mechanism for the host to perform debug operations on the guest. So limit the extensions that are visible to an allowlist so that only those capabilities we can support are advertised.

---
Changes since v13:
* Add missing check in kvm_vm_ioctl_enable_cap().
Changes since v10:
* Add a kvm_realm_ext_allowed() function which limits which extensions are exposed to an allowlist. This removes the need for special casing various extensions.
Changes since v7:
* Remove the helper functions and inline the kvm_is_realm() check with a ternary operator.
* Rewrite the commit message to explain this patch.

---
 arch/arm64/kvm/arm.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
arch/arm64/kvm/arm.c +25 -0
index 18251e561524..c6ebc5913e40 100644
@@ -133,6 +133,25 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
 }
 
+static bool kvm_realm_ext_allowed(long ext)
+{
+	switch (ext) {
+	case KVM_CAP_IRQCHIP:
+	case KVM_CAP_ARM_PSCI:
+	case KVM_CAP_ARM_PSCI_0_2:
+	case KVM_CAP_NR_VCPUS:
+	case KVM_CAP_MAX_VCPUS:
+	case KVM_CAP_MAX_VCPU_ID:
+	case KVM_CAP_MSI_DEVID:
+	case KVM_CAP_ARM_VM_IPA_SIZE:
+	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
+	case KVM_CAP_ARM_PTRAUTH_GENERIC:
+	case KVM_CAP_ARM_RMI:
+		return true;
+	}
+	return false;
+}
+
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 			    struct kvm_enable_cap *cap)
 {
@@ -144,6 +163,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 	if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, cap->cap))
 		return -EINVAL;
 
+	if (kvm && kvm_is_realm(kvm) && !kvm_realm_ext_allowed(cap->cap))
+		return -EINVAL;
+
 	switch (cap->cap) {
 	case KVM_CAP_ARM_NISV_TO_USER:
 		r = 0;
@@ -378,6 +400,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, ext))
 		return 0;
 
+	if (kvm && kvm_is_realm(kvm) && !kvm_realm_ext_allowed(ext))
+		return 0;
+
 	switch (ext) {
 	case KVM_CAP_IRQCHIP:
 		r = vgic_present;
Signature · 1 line
2.43.0
#77 lore raw

PATCH v14 16/44 KVM: arm64: Allow passing machine type in KVM creation

In reply to Steven Price

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

---
Changes since v13:
* Rework to use the two top bits for the machine type now that pKVM has merged and used the top bit for KVM_VM_TYPE_ARM_PROTECTED.
* Update the documentation to include KVM_VM_TYPE_ARM_PROTECTED as well.
Changes since v9:
* Explictly set realm.state to REALM_STATE_NONE rather than rely on the zeroing of the structure.
Changes since v7:
* Add some documentation explaining the new machine type.
Changes since v6:
* Make the check for kvm_rme_is_available more visible and report an error code of -EPERM (instead of -EINVAL) to make it explicit that the kernel supports RME, but the platform doesn't.

---
 Documentation/virt/kvm/api.rst | 18 ++++++++++++++++--
 arch/arm64/kvm/arm.c           | 11 +++++++++++
 include/uapi/linux/kvm.h       |  7 ++++++-
 3 files changed, 33 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +16 -2
index ca68aae7faa2..31a5919d8d5f 100644
@@ -181,8 +181,22 @@ flag KVM_VM_MIPS_VZ.
 ARM64:
 ^^^^^^
 
-On arm64, the physical address size for a VM (IPA Size limit) is limited
-to 40bits by default. The limit can be configured if the host supports the
+On arm64, the machine type identifier is used to encode a type and the
+physical address size for the VM. The lower byte (bits[7-0]) encode the
+address size and the upper bits[30-31] encode a machine type. The machine
+types that might be available are:
+
+ =========================   ============================================
+ KVM_VM_TYPE_ARM_NORMAL      A standard VM
+ KVM_VM_TYPE_ARM_REALM       A "Realm" VM using the Arm Confidential
+                             Compute extensions, the VM's memory is
+                             protected from the host.
+ KVM_VM_TYPE_ARM_PROTECTED   A "protected" VM using pKVM to isolate the
+                             VM from the host.
+ =========================   ============================================
+
+The physical address size for a VM (IPA Size limit) is limited to 40bits
+by default. The limit can be configured if the host supports the
 extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use
 KVM_VM_TYPE_ARM_IPA_SIZE(IPA_Bits) to set the size in the machine type
 identifier, where IPA_Bits is the maximum width of any physical
arch/arm64/kvm/arm.c +11 -0
index c6ebc5913e40..41d35b2d1dee 100644
@@ -246,6 +246,17 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	mutex_unlock(&kvm->lock);
 #endif
 
+	if ((type & KVM_VM_TYPE_ARM_PROTECTED) &&
+	    (type & KVM_VM_TYPE_ARM_REALM))
+		return -EINVAL;
+
+	if (type & KVM_VM_TYPE_ARM_REALM) {
+		if (!static_branch_unlikely(&kvm_rmi_is_available))
+			return -EINVAL;
+		kvm_set_realm_state(kvm, REALM_STATE_NONE);
+		kvm->arch.is_realm = true;
+	}
+
 	kvm_init_nested(kvm);
 
 	ret = kvm_share_hyp(kvm, kvm + 1);
include/uapi/linux/kvm.h +6 -1
index b8cff0938041..7b2507a3865e 100644
@@ -700,14 +700,19 @@ struct kvm_enable_cap {
  * address size for the VM. Bits[7-0] are reserved for the guest
  * PA size shift (i.e, log2(PA_Size)). For backward compatibility,
  * value 0 implies the default IPA size, 40bits.
+ *
+ * Bits[30-31] are reserved for the VM type
  */
 #define KVM_VM_TYPE_ARM_IPA_SIZE_MASK	0xffULL
 #define KVM_VM_TYPE_ARM_IPA_SIZE(x)		\
 	((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
 
+#define KVM_VM_TYPE_ARM_NORMAL		0
+#define KVM_VM_TYPE_ARM_REALM		(1UL << 30)
 #define KVM_VM_TYPE_ARM_PROTECTED	(1UL << 31)
 #define KVM_VM_TYPE_ARM_MASK		(KVM_VM_TYPE_ARM_IPA_SIZE_MASK | \
-					 KVM_VM_TYPE_ARM_PROTECTED)
+					 KVM_VM_TYPE_ARM_PROTECTED | \
+					 KVM_VM_TYPE_ARM_REALM)
 
 /*
  * ioctls for /dev/kvm fds:
Signature · 1 line
2.43.0
#78 lore raw

PATCH v14 17/44 arm64: RMI: RTT tear down

In reply to Steven Price

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

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

---
Changes since v13:
* Avoid the double call of kvm_free_stage2_pgd() by splitting the work across that and a new function kvm_realm_uninit_stage2() which is only called for realm guests.
Changes since v12:
* Simplify some functions now we know RMM page size is the same as the host's.
Changes since v11:
* Moved some code from earlier in the series to this one so that it's added when it's first used.
Changes since v10:
* RME->RMI rename.
* Some code to handle freeing stage 2 PGD moved into this patch where it belongs.
Changes since v9:
* Add a comment clarifying that root level RTTs are not destroyed until after the RD is destroyed.
Changes since v8:
* Introduce free_rtt() wrapper which calls free_delegated_granule() followed by kvm_account_pgtable_pages(). This makes it clear where an RTT is being freed rather than just a delegated granule.
Changes since v6:
* Move rme_rtt_level_mapsize() and supporting defines from kvm_rme.h into rme.c as they are only used in that file.
Changes since v5:
* Rename some RME_xxx defines to do with page sizes as RMM_xxx - they are a property of the RMM specification not the RME architecture.
Changes since v2:
* Moved {alloc,free}_delegated_page() and ensure_spare_page() to a later patch when they are actually used.
* Some simplifications now rmi_xxx() functions allow NULL as an output parameter.
* Improved comments and code layout.

---
 arch/arm64/include/asm/kvm_rmi.h |   7 ++
 arch/arm64/kvm/mmu.c             |  21 ++++-
 arch/arm64/kvm/rmi.c             | 148 +++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +7 -0
index 9de34983ee52..06ba0d4745c6 100644
@@ -64,5 +64,12 @@ u32 kvm_realm_ipa_limit(void);
 
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+void kvm_realm_destroy_rtts(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}
 
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +19 -2
index ba8286472286..eb56d4e7f21a 100644
@@ -1024,9 +1024,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	return err;
 }
 
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct realm *realm = &kvm->arch.realm;
+
+	if (kvm_realm_state(kvm) != REALM_STATE_ACTIVE)
+		return;
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+	kvm_realm_destroy_rtts(kvm);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_is_realm(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 
@@ -1103,7 +1120,7 @@ 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;
arch/arm64/kvm/rmi.c +148 -0

166 lines not on this page · raw message

Signature · 1 line
2.43.0
#79 lore raw

Re: [PATCH v14 17/44] arm64: RMI: RTT tear down

Quoted 4 lines · Steven Price
arch/arm64/kvm/mmu.c +4 -0
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct realm *realm = &kvm->arch.realm;

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1040

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	if (kvm_realm_state(kvm) != REALM_STATE_ACTIVE)
+		return;
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+	kvm_realm_destroy_rtts(kvm);
+}
+

We also call kvm_stage2_unmap_range in kvm_destroy_realm()

void kvm_destroy_realm(struct kvm *kvm)
{
...

	write_lock(&kvm->mmu_lock);
	kvm_stage2_unmap_range(&kvm->arch.mmu, 0,
			       BIT(realm->ia_bits - 1), true);
	write_unlock(&kvm->mmu_lock);

-aneesh

#80 lore raw

Re: [PATCH v14 17/44] arm64: RMI: RTT tear down

In reply to Steven Price

Hi,

Quoted 101 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.

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

---
Changes since v13:
* Avoid the double call of kvm_free_stage2_pgd() by splitting the work across that and a new function kvm_realm_uninit_stage2() which is only called for realm guests.
Changes since v12:
* Simplify some functions now we know RMM page size is the same as the host's.
Changes since v11:
* Moved some code from earlier in the series to this one so that it's added when it's first used.
Changes since v10:
* RME->RMI rename.
* Some code to handle freeing stage 2 PGD moved into this patch where it belongs.
Changes since v9:
* Add a comment clarifying that root level RTTs are not destroyed until after the RD is destroyed.
Changes since v8:
* Introduce free_rtt() wrapper which calls free_delegated_granule() followed by kvm_account_pgtable_pages(). This makes it clear where an RTT is being freed rather than just a delegated granule.
Changes since v6:
* Move rme_rtt_level_mapsize() and supporting defines from kvm_rme.h into rme.c as they are only used in that file.
Changes since v5:
* Rename some RME_xxx defines to do with page sizes as RMM_xxx - they are a property of the RMM specification not the RME architecture.
Changes since v2:
* Moved {alloc,free}_delegated_page() and ensure_spare_page() to a later patch when they are actually used.
* Some simplifications now rmi_xxx() functions allow NULL as an output parameter.
* Improved comments and code layout.

---
 arch/arm64/include/asm/kvm_rmi.h |   7 ++
 arch/arm64/kvm/mmu.c             |  21 ++++-
 arch/arm64/kvm/rmi.c             | 148 +++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +7 -0
index 9de34983ee52..06ba0d4745c6 100644
@@ -64,5 +64,12 @@ u32 kvm_realm_ipa_limit(void);
 
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+void kvm_realm_destroy_rtts(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}
 
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +18 -1
index ba8286472286..eb56d4e7f21a 100644
@@ -1024,9 +1024,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	return err;
 }
 
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct realm *realm = &kvm->arch.realm;
+
+	if (kvm_realm_state(kvm) != REALM_STATE_ACTIVE)
+		return;
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+	kvm_realm_destroy_rtts(kvm);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_is_realm(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 

arch/arm64/kvm/mmu.c void stage2_unmap_vm(struct kvm *kvm) · line 1123

Quoted 5 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1103,7 +1120,7 @@ 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;

Is this included by accident?

arch/arm64/kvm/mmu.c void stage2_unmap_vm(struct kvm *kvm) · line 1126

Quoted 3 lines
arch/arm64/kvm/mmu.c +0 -0
 	write_lock(&kvm->mmu_lock);
 	pgt = mmu->pgt;

[...]

Thanks,
Wei-Lin Chang

#81 lore raw

Re: [PATCH v14 17/44] arm64: RMI: RTT tear down

Quoted 103 lines · Wei-Lin Chang

Hi,

Quoted 100 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.

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

---
Changes since v13:
* Avoid the double call of kvm_free_stage2_pgd() by splitting the work across that and a new function kvm_realm_uninit_stage2() which is only called for realm guests.
Changes since v12:
* Simplify some functions now we know RMM page size is the same as the host's.
Changes since v11:
* Moved some code from earlier in the series to this one so that it's added when it's first used.
Changes since v10:
* RME->RMI rename.
* Some code to handle freeing stage 2 PGD moved into this patch where it belongs.
Changes since v9:
* Add a comment clarifying that root level RTTs are not destroyed until after the RD is destroyed.
Changes since v8:
* Introduce free_rtt() wrapper which calls free_delegated_granule() followed by kvm_account_pgtable_pages(). This makes it clear where an RTT is being freed rather than just a delegated granule.
Changes since v6:
* Move rme_rtt_level_mapsize() and supporting defines from kvm_rme.h into rme.c as they are only used in that file.
Changes since v5:
* Rename some RME_xxx defines to do with page sizes as RMM_xxx - they are a property of the RMM specification not the RME architecture.
Changes since v2:
* Moved {alloc,free}_delegated_page() and ensure_spare_page() to a later patch when they are actually used.
* Some simplifications now rmi_xxx() functions allow NULL as an output parameter.
* Improved comments and code layout.

---
 arch/arm64/include/asm/kvm_rmi.h |   7 ++
 arch/arm64/kvm/mmu.c             |  21 ++++-
 arch/arm64/kvm/rmi.c             | 148 +++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +7 -0
index 9de34983ee52..06ba0d4745c6 100644
@@ -64,5 +64,12 @@ u32 kvm_realm_ipa_limit(void);
 
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+void kvm_realm_destroy_rtts(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}
 
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +18 -1
index ba8286472286..eb56d4e7f21a 100644
@@ -1024,9 +1024,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	return err;
 }
 
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct realm *realm = &kvm->arch.realm;
+
+	if (kvm_realm_state(kvm) != REALM_STATE_ACTIVE)
+		return;
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+	kvm_realm_destroy_rtts(kvm);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_is_realm(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 
Quoted 8 lines

arch/arm64/kvm/mmu.c void stage2_unmap_vm(struct kvm *kvm) · line 1123

Quoted 6 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1103,7 +1120,7 @@ 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;

Is this included by accident?

Thanks for spotting that. Yes that change shouldn't have sneaked in here. The original code before this series had the redundant assignment to NULL. But it's unrelated to this patch so I'll drop the change.

Thanks,
Steve

Quoted 9 lines

arch/arm64/kvm/mmu.c void stage2_unmap_vm(struct kvm *kvm) · line 1126

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
 	write_lock(&kvm->mmu_lock);
 	pgt = mmu->pgt;

[...]

Thanks,
Wei-Lin Chang

#82 lore raw

Re: [PATCH v14 17/44] arm64: RMI: RTT tear down

In reply to Steven Price

Hi,

Quoted 157 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.

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

---
Changes since v13:
* Avoid the double call of kvm_free_stage2_pgd() by splitting the work across that and a new function kvm_realm_uninit_stage2() which is only called for realm guests.
Changes since v12:
* Simplify some functions now we know RMM page size is the same as the host's.
Changes since v11:
* Moved some code from earlier in the series to this one so that it's added when it's first used.
Changes since v10:
* RME->RMI rename.
* Some code to handle freeing stage 2 PGD moved into this patch where it belongs.
Changes since v9:
* Add a comment clarifying that root level RTTs are not destroyed until after the RD is destroyed.
Changes since v8:
* Introduce free_rtt() wrapper which calls free_delegated_granule() followed by kvm_account_pgtable_pages(). This makes it clear where an RTT is being freed rather than just a delegated granule.
Changes since v6:
* Move rme_rtt_level_mapsize() and supporting defines from kvm_rme.h into rme.c as they are only used in that file.
Changes since v5:
* Rename some RME_xxx defines to do with page sizes as RMM_xxx - they are a property of the RMM specification not the RME architecture.
Changes since v2:
* Moved {alloc,free}_delegated_page() and ensure_spare_page() to a later patch when they are actually used.
* Some simplifications now rmi_xxx() functions allow NULL as an output parameter.
* Improved comments and code layout.

---
 arch/arm64/include/asm/kvm_rmi.h |   7 ++
 arch/arm64/kvm/mmu.c             |  21 ++++-
 arch/arm64/kvm/rmi.c             | 148 +++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +7 -0
index 9de34983ee52..06ba0d4745c6 100644
@@ -64,5 +64,12 @@ u32 kvm_realm_ipa_limit(void);
 
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+void kvm_realm_destroy_rtts(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}
 
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +19 -2
index ba8286472286..eb56d4e7f21a 100644
@@ -1024,9 +1024,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	return err;
 }
 
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct realm *realm = &kvm->arch.realm;
+
+	if (kvm_realm_state(kvm) != REALM_STATE_ACTIVE)
+		return;
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+	kvm_realm_destroy_rtts(kvm);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_is_realm(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 
@@ -1103,7 +1120,7 @@ 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;
arch/arm64/kvm/rmi.c +33 -0
index f51ec667445e..5b00ccca4af3 100644
@@ -11,6 +11,14 @@
 #include <asm/rmi_cmds.h>
 #include <asm/virt.h>
 
+static inline unsigned long rmi_rtt_level_mapsize(int level)
+{
+	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+		return PAGE_SIZE;
+
+	return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
+}
+
 static bool rmi_has_feature(unsigned long feature)
 {
 	return !!u64_get_bits(rmm_feat_reg0, feature);
@@ -21,6 +29,144 @@ u32 kvm_realm_ipa_limit(void)
 	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
 }
 
+static int get_start_level(struct realm *realm)
+{
+	return 4 - stage2_pgtable_levels(realm->ia_bits);
+}
+
+static void free_rtt(phys_addr_t phys)
+{
+	if (free_delegated_page(phys))
+		return;
+
+	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
+}
+
+/*
+ * realm_rtt_destroy - Destroy an RTT at @level for @addr.
+ *
+ * Returns - Result of the RMI_RTT_DESTROY call, and:
+ * @rtt_granule:	RTT granule, if the RTT was destroyed.
+ * @next_addr:		IPA corresponding to the next possible valid entry we
+ *			can target
+ */
+static int realm_rtt_destroy(struct realm *realm, unsigned long addr,
+			     int level, phys_addr_t *rtt_granule,
+			     unsigned long *next_addr)
+{

arch/arm64/kvm/rmi.c u32 kvm_realm_ipa_limit(void) · line 66

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+	unsigned long out_rtt;
+	int ret;
+
+	ret = rmi_rtt_destroy(virt_to_phys(realm->rd), addr, level,
+			      &out_rtt, next_addr);
+
+	*rtt_granule = out_rtt;
+
+	return ret;
+}

Looks like out_rtt can be simplified out.

[...]

Thanks,
Wei-Lin Chang

#83 lore raw

Re: [PATCH v14 17/44] arm64: RMI: RTT tear down

Quoted 164 lines · Wei-Lin Chang

Hi,

Quoted 161 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.

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

---
Changes since v13:
* Avoid the double call of kvm_free_stage2_pgd() by splitting the work across that and a new function kvm_realm_uninit_stage2() which is only called for realm guests.
Changes since v12:
* Simplify some functions now we know RMM page size is the same as the host's.
Changes since v11:
* Moved some code from earlier in the series to this one so that it's added when it's first used.
Changes since v10:
* RME->RMI rename.
* Some code to handle freeing stage 2 PGD moved into this patch where it belongs.
Changes since v9:
* Add a comment clarifying that root level RTTs are not destroyed until after the RD is destroyed.
Changes since v8:
* Introduce free_rtt() wrapper which calls free_delegated_granule() followed by kvm_account_pgtable_pages(). This makes it clear where an RTT is being freed rather than just a delegated granule.
Changes since v6:
* Move rme_rtt_level_mapsize() and supporting defines from kvm_rme.h into rme.c as they are only used in that file.
Changes since v5:
* Rename some RME_xxx defines to do with page sizes as RMM_xxx - they are a property of the RMM specification not the RME architecture.
Changes since v2:
* Moved {alloc,free}_delegated_page() and ensure_spare_page() to a later patch when they are actually used.
* Some simplifications now rmi_xxx() functions allow NULL as an output parameter.
* Improved comments and code layout.

---
 arch/arm64/include/asm/kvm_rmi.h |   7 ++
 arch/arm64/kvm/mmu.c             |  21 ++++-
 arch/arm64/kvm/rmi.c             | 148 +++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +7 -0
index 9de34983ee52..06ba0d4745c6 100644
@@ -64,5 +64,12 @@ u32 kvm_realm_ipa_limit(void);
 
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+void kvm_realm_destroy_rtts(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}
 
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +19 -2
index ba8286472286..eb56d4e7f21a 100644
@@ -1024,9 +1024,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	return err;
 }
 
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct realm *realm = &kvm->arch.realm;
+
+	if (kvm_realm_state(kvm) != REALM_STATE_ACTIVE)
+		return;
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(mmu, 0, BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+	kvm_realm_destroy_rtts(kvm);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_is_realm(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 
@@ -1103,7 +1120,7 @@ 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;
arch/arm64/kvm/rmi.c +37 -0
index f51ec667445e..5b00ccca4af3 100644
@@ -11,6 +11,14 @@
 #include <asm/rmi_cmds.h>
 #include <asm/virt.h>
 
+static inline unsigned long rmi_rtt_level_mapsize(int level)
+{
+	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+		return PAGE_SIZE;
+
+	return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
+}
+
 static bool rmi_has_feature(unsigned long feature)
 {
 	return !!u64_get_bits(rmm_feat_reg0, feature);
@@ -21,6 +29,144 @@ u32 kvm_realm_ipa_limit(void)
 	return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ);
 }
 
+static int get_start_level(struct realm *realm)
+{
+	return 4 - stage2_pgtable_levels(realm->ia_bits);
+}
+
+static void free_rtt(phys_addr_t phys)
+{
+	if (free_delegated_page(phys))
+		return;
+
+	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
+}
+
+/*
+ * realm_rtt_destroy - Destroy an RTT at @level for @addr.
+ *
+ * Returns - Result of the RMI_RTT_DESTROY call, and:
+ * @rtt_granule:	RTT granule, if the RTT was destroyed.
+ * @next_addr:		IPA corresponding to the next possible valid entry we
+ *			can target
+ */
+static int realm_rtt_destroy(struct realm *realm, unsigned long addr,
+			     int level, phys_addr_t *rtt_granule,
+			     unsigned long *next_addr)
+{
+	unsigned long out_rtt;
+	int ret;
+
+	ret = rmi_rtt_destroy(virt_to_phys(realm->rd), addr, level,
Quoted 8 lines

arch/arm64/kvm/rmi.c u32 kvm_realm_ipa_limit(void) · line 66

Quoted 6 lines
arch/arm64/kvm/rmi.c +6 -0
+			      &out_rtt, next_addr);
+
+	*rtt_granule = out_rtt;
+
+	return ret;
+}

Looks like out_rtt can be simplified out.

The issue here is there's a type conversion going on. rmi_rtt_destroy() takes an "unsigned long *" to match the general approach of using "unsigned long" for the inputs/outputs of SMCCC calls. But rtt_granule is a "phys_addr_t". While we know these are (currently) the same size, they are not the same type according to the compiler - phys_addr_t is "long long unsigned int".

Thanks,
Steve

Quoted 4 lines

[...]

Thanks,
Wei-Lin Chang

#84 lore raw

PATCH v14 18/44 arm64: RMI: Activate realm on first VCPU run

In reply to Steven Price

Use kvm_arch_vcpu_run_pid_change() to check if this is the first time the realm guest has run. If this is the first run then activate the realm.

Before the realm can be activated it must first be created, this is a stub in this patch and will be filled in by a later patch.

---
Changes since v12:
* Fix commit message
* Change realm_state checks to be >= REALM_STATE_ACTIVE to avoid a dead guest being revived by kvm_activate_realm().

---
 arch/arm64/include/asm/kvm_rmi.h |  1 +
 arch/arm64/kvm/arm.c             |  6 +++++
 arch/arm64/kvm/rmi.c             | 39 ++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
arch/arm64/include/asm/kvm_rmi.h +1 -0
index 06ba0d4745c6..8bd743093ccf 100644
@@ -63,6 +63,7 @@ void kvm_init_rmi(void);
 u32 kvm_realm_ipa_limit(void);
 
 int kvm_init_realm(struct kvm *kvm);
+int kvm_activate_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
 void kvm_realm_destroy_rtts(struct kvm *kvm);
 
arch/arm64/kvm/arm.c +6 -0
index 41d35b2d1dee..eb2b61fe1f0a 100644
@@ -1018,6 +1018,12 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 			return ret;
 	}
 
+	if (kvm_is_realm(vcpu->kvm)) {
+		ret = kvm_activate_realm(kvm);
+		if (ret)
+			return ret;
+	}
+
 	mutex_lock(&kvm->arch.config_lock);
 	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
 	mutex_unlock(&kvm->arch.config_lock);
arch/arm64/kvm/rmi.c +39 -0
index 5b00ccca4af3..849111817af7 100644
@@ -167,6 +167,45 @@ void kvm_realm_destroy_rtts(struct kvm *kvm)
 	realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits));
 }
 
+static int realm_ensure_created(struct kvm *kvm)
+{
+	/* Provided in later patch */
+	return -ENXIO;
+}
+
+int kvm_activate_realm(struct kvm *kvm)
+{
+	struct realm *realm = &kvm->arch.realm;
+	int ret;
+
+	if (kvm_realm_state(kvm) >= REALM_STATE_ACTIVE)
+		return 0;
+
+	if (!irqchip_in_kernel(kvm)) {
+		/* Userspace irqchip not yet supported with realms */
+		return -EOPNOTSUPP;
+	}
+
+	guard(mutex)(&kvm->arch.config_lock);
+	/* Check again with the lock held */
+	if (kvm_realm_state(kvm) >= REALM_STATE_ACTIVE)
+		return 0;
+
+	ret = realm_ensure_created(kvm);
+	if (ret)
+		return ret;
+
+	/* Mark state as dead in case we fail */
+	kvm_set_realm_state(kvm, REALM_STATE_DEAD);
+
+	ret = rmi_realm_activate(virt_to_phys(realm->rd));
+	if (ret)
+		return -ENXIO;
+
+	kvm_set_realm_state(kvm, REALM_STATE_ACTIVE);
+	return 0;
+}
+
 void kvm_destroy_realm(struct kvm *kvm)
 {
 	struct realm *realm = &kvm->arch.realm;
Signature · 1 line
2.43.0
#85 lore raw

PATCH v14 19/44 arm64: RMI: Allocate/free RECs to match vCPUs

In reply to Steven Price

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

RECs may require additional pages (e.g. for storing larger register state for SVE). The RMM can request extra pages for this purpose using the Stateful RMI Operations (SRO) functionality to request pages during REC creation. These pages are then passed back to the host from the RMM ('reclaimed') when the REC is destroyed. The kernel tracking object (struct rmi_sro_state) is stored in the realm_rec structure to avoid memory allocation during the destruction path.

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

---
Changes since v13:
* Support SRO for REC creation/destruction instead of auxiliary granules.
Changes since v12:
* Use the new range-based delegation RMI.
Changes since v11:
* Remove the KVM_ARM_VCPU_REC feature. User space no longer needs to configure each VCPU separately, RECs are created on the first VCPU run of the guest.
Changes since v9:
* Size the aux_pages array according to the PAGE_SIZE of the host.
Changes since v7:
* Add comment explaining the aux_pages array.
* Rename "undeleted_failed" variable to "should_free" to avoid a confusing double negative.
Changes since v6:
* Avoid reporting the KVM_ARM_VCPU_REC feature if the guest isn't a realm guest.
* Support host page size being larger than RMM's granule size when allocating/freeing aux granules.
Changes since v5:
* Separate the concept of vcpu_is_rec() and kvm_arm_vcpu_rec_finalized() by using the KVM_ARM_VCPU_REC feature as the indication that the VCPU is a REC.
Changes since v2:
* Free rec->run earlier in kvm_destroy_realm() and adapt to previous patches.

---
 arch/arm64/include/asm/kvm_emulate.h |   2 +-
 arch/arm64/include/asm/kvm_host.h    |   3 +
 arch/arm64/include/asm/kvm_rmi.h     |  17 +++++
 arch/arm64/kvm/arm.c                 |   6 ++
 arch/arm64/kvm/reset.c               |   1 +
 arch/arm64/kvm/rmi.c                 | 105 +++++++++++++++++++++++++++
 6 files changed, 133 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_emulate.h +1 -1
index 82fd777bd9bb..2e69fe494716 100644
@@ -714,7 +714,7 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)
 
 static inline bool vcpu_is_rec(const struct kvm_vcpu *vcpu)
 {
-	return false;
+	return kvm_is_realm(vcpu->kvm);
 }
 
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +3 -0
index 3512696ed506..39b5de03d0fe 100644
@@ -969,6 +969,9 @@ struct kvm_vcpu_arch {
 
 	/* Hyp-readable copy of kvm_vcpu::pid */
 	pid_t pid;
+
+	/* Realm meta data */
+	struct realm_rec rec;
 };
 
 /*
arch/arm64/include/asm/kvm_rmi.h +17 -0
index 8bd743093ccf..d99bf4fc3c39 100644
@@ -59,6 +59,22 @@ struct realm {
 	unsigned int ia_bits;
 };
 
+/**
+ * struct realm_rec - Additional per VCPU data for a Realm
+ *
+ * @mpidr: MPIDR (Multiprocessor Affinity Register) value to identify this VCPU
+ * @rec_page: Kernel VA of the RMM's private page for this REC
+ * @aux_pages: Additional pages private to the RMM for this REC
+ * @run: Kernel VA of the RmiRecRun structure shared with the RMM
+ * @sro: A preallocated SRO state context
+ */
+struct realm_rec {
+	unsigned long mpidr;
+	void *rec_page;
+	struct rec_run *run;
+	struct rmi_sro_state *sro;
+};
+
 void kvm_init_rmi(void);
 u32 kvm_realm_ipa_limit(void);
 
@@ -66,6 +82,7 @@ int kvm_init_realm(struct kvm *kvm);
 int kvm_activate_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
 void kvm_realm_destroy_rtts(struct kvm *kvm);
+void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
 static inline bool kvm_realm_is_private_address(struct realm *realm,
 						unsigned long addr)
arch/arm64/kvm/arm.c +6 -0
index eb2b61fe1f0a..93d34762db91 100644
@@ -586,6 +586,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	/* Force users to call KVM_ARM_VCPU_INIT */
 	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
 
+	vcpu->arch.rec.mpidr = INVALID_HWID;
+
 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
 
 	/* Set up the timer */
@@ -1651,6 +1653,10 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 	if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
 		return -EINVAL;
 
+	/* Realms are incompatible with AArch32 */
+	if (vcpu_is_rec(vcpu))
+		return -EINVAL;
+
 	return 0;
 }
 
arch/arm64/kvm/reset.c +1 -0
index b963fd975aac..c18cdca7d125 100644
@@ -161,6 +161,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
 	kfree(vcpu->arch.vncr_tlb);
 	kfree(vcpu->arch.ccsidr);
+	kvm_destroy_rec(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
arch/arm64/kvm/rmi.c +105 -0

120 lines not on this page · raw message

Signature · 1 line
2.43.0
#86 lore raw

Re: [PATCH v14 19/44] arm64: RMI: Allocate/free RECs to match vCPUs

Hi,

Quoted 177 lines · Steven Price

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

RECs may require additional pages (e.g. for storing larger register state for SVE). The RMM can request extra pages for this purpose using the Stateful RMI Operations (SRO) functionality to request pages during REC creation. These pages are then passed back to the host from the RMM ('reclaimed') when the REC is destroyed. The kernel tracking object (struct rmi_sro_state) is stored in the realm_rec structure to avoid memory allocation during the destruction path.

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

---
Changes since v13:
* Support SRO for REC creation/destruction instead of auxiliary granules.
Changes since v12:
* Use the new range-based delegation RMI.
Changes since v11:
* Remove the KVM_ARM_VCPU_REC feature. User space no longer needs to configure each VCPU separately, RECs are created on the first VCPU run of the guest.
Changes since v9:
* Size the aux_pages array according to the PAGE_SIZE of the host.
Changes since v7:
* Add comment explaining the aux_pages array.
* Rename "undeleted_failed" variable to "should_free" to avoid a confusing double negative.
Changes since v6:
* Avoid reporting the KVM_ARM_VCPU_REC feature if the guest isn't a realm guest.
* Support host page size being larger than RMM's granule size when allocating/freeing aux granules.
Changes since v5:
* Separate the concept of vcpu_is_rec() and kvm_arm_vcpu_rec_finalized() by using the KVM_ARM_VCPU_REC feature as the indication that the VCPU is a REC.
Changes since v2:
* Free rec->run earlier in kvm_destroy_realm() and adapt to previous patches.

---
 arch/arm64/include/asm/kvm_emulate.h |   2 +-
 arch/arm64/include/asm/kvm_host.h    |   3 +
 arch/arm64/include/asm/kvm_rmi.h     |  17 +++++
 arch/arm64/kvm/arm.c                 |   6 ++
 arch/arm64/kvm/reset.c               |   1 +
 arch/arm64/kvm/rmi.c                 | 105 +++++++++++++++++++++++++++
 6 files changed, 133 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_emulate.h +1 -1
index 82fd777bd9bb..2e69fe494716 100644
@@ -714,7 +714,7 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)
 
 static inline bool vcpu_is_rec(const struct kvm_vcpu *vcpu)
 {
-	return false;
+	return kvm_is_realm(vcpu->kvm);
 }
 
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +3 -0
index 3512696ed506..39b5de03d0fe 100644
@@ -969,6 +969,9 @@ struct kvm_vcpu_arch {
 
 	/* Hyp-readable copy of kvm_vcpu::pid */
 	pid_t pid;
+
+	/* Realm meta data */
+	struct realm_rec rec;
 };
 
 /*
arch/arm64/include/asm/kvm_rmi.h +17 -0
index 8bd743093ccf..d99bf4fc3c39 100644
@@ -59,6 +59,22 @@ struct realm {
 	unsigned int ia_bits;
 };
 
+/**
+ * struct realm_rec - Additional per VCPU data for a Realm
+ *
+ * @mpidr: MPIDR (Multiprocessor Affinity Register) value to identify this VCPU
+ * @rec_page: Kernel VA of the RMM's private page for this REC
+ * @aux_pages: Additional pages private to the RMM for this REC
+ * @run: Kernel VA of the RmiRecRun structure shared with the RMM
+ * @sro: A preallocated SRO state context
+ */
+struct realm_rec {
+	unsigned long mpidr;
+	void *rec_page;
+	struct rec_run *run;
+	struct rmi_sro_state *sro;
+};
+
 void kvm_init_rmi(void);
 u32 kvm_realm_ipa_limit(void);
 
@@ -66,6 +82,7 @@ int kvm_init_realm(struct kvm *kvm);
 int kvm_activate_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
 void kvm_realm_destroy_rtts(struct kvm *kvm);
+void kvm_destroy_rec(struct kvm_vcpu *vcpu);
 
 static inline bool kvm_realm_is_private_address(struct realm *realm,
 						unsigned long addr)
arch/arm64/kvm/arm.c +6 -0
index eb2b61fe1f0a..93d34762db91 100644
@@ -586,6 +586,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	/* Force users to call KVM_ARM_VCPU_INIT */
 	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
 
+	vcpu->arch.rec.mpidr = INVALID_HWID;
+
 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
 
 	/* Set up the timer */
@@ -1651,6 +1653,10 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 	if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
 		return -EINVAL;
 
+	/* Realms are incompatible with AArch32 */
+	if (vcpu_is_rec(vcpu))
+		return -EINVAL;
+
 	return 0;
 }
 
arch/arm64/kvm/reset.c +1 -0
index b963fd975aac..c18cdca7d125 100644
@@ -161,6 +161,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
 	kfree(vcpu->arch.vncr_tlb);
 	kfree(vcpu->arch.ccsidr);
+	kvm_destroy_rec(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
arch/arm64/kvm/rmi.c +16 -0
index 849111817af7..353a5ca45e78 100644
@@ -173,9 +173,108 @@ static int realm_ensure_created(struct kvm *kvm)
 	return -ENXIO;
 }
 
+static int kvm_create_rec(struct kvm_vcpu *vcpu)
+{
+	struct user_pt_regs *vcpu_regs = vcpu_gp_regs(vcpu);
+	unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
+	struct realm *realm = &vcpu->kvm->arch.realm;
+	struct realm_rec *rec = &vcpu->arch.rec;
+	unsigned long rec_page_phys;
+	struct rec_params *params;
+	int r, i;
+
+	if (rec->run)
+		return -EBUSY;
+
+	/*
+	 * The RMM will report PSCI v1.0 to Realms and the KVM_ARM_VCPU_PSCI_0_2
+	 * flag covers v0.2 and onwards.

arch/arm64/kvm/rmi.c static int realm_ensure_created(struct kvm *kvm) · line 201

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+	 */
+	if (!vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2))
+		return -EINVAL;
+
+	BUILD_BUG_ON(sizeof(*params) > PAGE_SIZE);
+	BUILD_BUG_ON(sizeof(*rec->run) > PAGE_SIZE);
+
+	params = (struct rec_params *)get_zeroed_page(GFP_KERNEL);
+	rec->rec_page = (void *)__get_free_page(GFP_KERNEL);
+	rec->run = (void *)get_zeroed_page(GFP_KERNEL);

Should this be cast to (struct rec_run *) ?

Quoted 36 lines
arch/arm64/kvm/rmi.c +36 -0
+	rec->sro = kmalloc_obj(*rec->sro);
+	if (!params || !rec->rec_page || !rec->run || !rec->sro) {
+		r = -ENOMEM;
+		goto out_free_pages;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(params->gprs); i++)
+		params->gprs[i] = vcpu_regs->regs[i];
+
+	params->pc = vcpu_regs->pc;
+
+	if (vcpu->vcpu_id == 0)
+		params->flags |= REC_PARAMS_FLAG_RUNNABLE;
+
+	rec_page_phys = virt_to_phys(rec->rec_page);
+
+	if (rmi_delegate_page(rec_page_phys)) {
+		r = -ENXIO;
+		goto out_free_pages;
+	}
+
+	params->mpidr = mpidr;
+
+	if (rmi_rec_create(virt_to_phys(realm->rd), rec_page_phys,
+			   virt_to_phys(params), rec->sro)) {
+		r = -ENXIO;
+		goto out_undelegate_rmm_rec;
+	}
+
+	rec->mpidr = mpidr;
+
+	free_page((unsigned long)params);
+	return 0;
+
+out_undelegate_rmm_rec:
+	if (WARN_ON(rmi_undelegate_page(rec_page_phys)))

arch/arm64/kvm/rmi.c static int realm_ensure_created(struct kvm *kvm) · line 247

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+		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);
+	kfree(rec->sro);
+	rec->run = NULL;
+	return r;
+}
+

[...]

Thanks,
Wei-Lin Chang