arm/mpu: implement setup_virt_paging for MPU system
PATCH 1/1 arm/mpu: implement setup_virt_paging for MPU system
From: Penny Zheng <[email protected]>
For MMU system, setup_virt_paging is used to configure stage 2 address translation regime, like IPA bits, VMID allocator set up, etc. Some could be inherited in MPU system, like VMID allocator set up, etc.
For MPU system, we could have the following memory translation regime:
- PMSAv8-64 at both EL1/EL0 and EL2
- VMSAv8-64 at EL1/EL0 and PMSAv8-64 at EL2
The default option will be the second, unless the platform could not support, which could be checked against MSA_frac bit in Memory Model Feature Register 0( ID_AA64MMFR0_EL1).
--- xen/arch/arm/arm64/mpu/p2m.c | 90 +++++++++++++++++++++++- xen/arch/arm/include/asm/arm32/mpu.h | 2 + xen/arch/arm/include/asm/arm64/mpu.h | 2 + xen/arch/arm/include/asm/arm64/sysregs.h | 5 ++ xen/arch/arm/include/asm/cpufeature.h | 7 ++ xen/arch/arm/include/asm/mpu.h | 7 +- xen/arch/arm/include/asm/mpu/p2m.h | 12 ++++ xen/arch/arm/include/asm/p2m.h | 5 ++ xen/arch/arm/include/asm/processor.h | 13 ++++ xen/arch/arm/mpu/p2m.c | 71 ++++++++++++++++++- 10 files changed, 209 insertions(+), 5 deletions(-)
xen/arch/arm/arm64/mpu/p2m.c +89 -1
@@ -2,11 +2,99 @@#include <xen/bug.h> #include <xen/init.h> +#include <xen/warning.h> #include <asm/p2m.h> void __init setup_virt_paging (void ) { -BUG_ON ("unimplemented" ); +uint64_t vtcr_el2 =0 , vstcr_el2 =0 ; +bool p2m_vmsa = true; + +/* PA size */ +const unsigned int pa_range_info[] = {32 ,36 ,40 ,42 ,44 ,48 ,52 ,0 ,/* Invalid */ }; + +/* +* Restrict "p2m_ipa_bits" if needed. As P2M table is always configured +* with IPA bits == PA bits, compare against "pabits". +*/ +if ( pa_range_info[system_cpuinfo.mm64.pa_range] < p2m_ipa_bits ) + p2m_ipa_bits = pa_range_info[system_cpuinfo.mm64.pa_range]; + +/* +* Clear VTCR_EL2.NSA bit to configure non-secure stage 2 translation output +* address space to access the Secure PA space. +*/ + vtcr_el2 &=NSA_SEL2 ; + +/* +* The MSA and MSA_frac fields in the ID_AA64MMFR0_EL1 register identify the +* memory system configurations supported at EL1. In Armv8-R AArch64, the +* only permitted value for ID_AA64MMFR0_EL1.MSA is 0b1111. +*/ +if ( system_cpuinfo.mm64.msa !=MM64_MSA_PMSA_SUPPORT ) + goto fault; + +/* Permitted values for ID_AA64MMFR0_EL1.MSA_frac are 0b0001 and 0b0010. */ +if ( system_cpuinfo.mm64.msa_frac ==MM64_MSA_FRAC_NONE_SUPPORT ) + goto fault; + +/* +* When ID_AA64MMFR0_EL1.MSA_frac is 0b0010 the stage 1 EL1&0 translation +* regime supports both PMSAv8-64 and VMSAv8-64. +*/ +if ( system_cpuinfo.mm64.msa_frac !=MM64_MSA_FRAC_VMSA_SUPPORT ) + { + p2m_vmsa = false; +warning_add ("Be aware of that there is no support for VMSAv8-64 at EL1 on this platform\n" ); + } + +/* +* If PE supports both PMSAv8-64 and VMSAv8-64 at EL1, then VTCR_EL2.MSA +* determines the memory system architecture enabled at stage 1 of the +* Secure EL1&0 translation regime. +* +* Normally, we set the initial VTCR_EL2.MSA value VMSAv8-64 support, +* unless this platform only supports PMSAv8-64. +*/ +if ( !p2m_vmsa ) + vtcr_el2 &=VTCR_MSA_PMSA ; +else + vtcr_el2 |=VTCR_MSA_VMSA ; + +/* +* cpuinfo sanitization makes sure we support 16bits VMID only if all cores +* are supporting it. +*/ +if ( system_cpuinfo.mm64.vmid_bits ==MM64_VMID_16_BITS_SUPPORT ) + max_vmid =MAX_VMID_16_BIT ; + +/* Set the VS bit only if 16 bit VMID is supported. */ +if (MAX_VMID ==MAX_VMID_16_BIT ) + vtcr_el2 |=VTCR_VS ; + +p2m_vmid_allocator_init (); + +WRITE_SYSREG (vtcr_el2,VTCR_EL2 ); + +/* +* VSTCR_EL2.SA defines secure stage 2 translation output address space. +* To make sure that all stage 2 translations for the Secure PA space access +* the Secure PA space, we keep SA bit as 0. +* +* VSTCR_EL2.SC is NS check enable bit. To make sure that Stage 2 NS +* configuration is checked against stage 1 NS configuration in EL1&0 +* translation regime for the given address, and generates a fault if they +* are different, we set SC bit 1. +*/ + vstcr_el2 =1 <<VSTCR_EL2_RES1_SHIFT ; + vstcr_el2 &=VSTCR_EL2_SA ; + vstcr_el2 |=VSTCR_EL2_SC ; +WRITE_SYSREG (vstcr_el2,VSTCR_EL2 ); + +return ; + + fault: +panic ("Hardware with no PMSAv8-64 support in any translation regime\n" ); }/*
xen/arch/arm/include/asm/arm32/mpu.h +2 -0
@@ -11,6 +11,8 @@*/ #define MPU_REGION_RES0 0x0 +#define VSCTLR_VMID_SHIFT 16 +/* Hypervisor Protection Region Base Address Register */ typedef union { struct {
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
@@ -7,6 +7,8 @@#define MPU_REGION_RES0 (0xFFFFULL << 48) +#define VSCTLR_VMID_SHIFT 48 +/* Protection Region Base Address Register */ typedef union { struct __packed {
xen/arch/arm/include/asm/arm64/sysregs.h +5 -0
@@ -462,6 +462,11 @@#define ZCR_ELx_LEN_SIZE 9#define ZCR_ELx_LEN_MASK 0x1ff +/* Virtualization Secure Translation Control Register */ +#define VSTCR_EL2_RES1_SHIFT 31 +#define VSTCR_EL2_SA ~(_AC(0x1,UL)<<30) +#define VSTCR_EL2_SC (_AC(0x1,UL)<<20) +#ifdef CONFIG_MPU /* * The Armv8-R AArch64 architecture always executes code in Secure
xen/arch/arm/include/asm/cpufeature.h +7 -0
@@ -248,6 +248,12 @@ struct cpuinfo_arm { unsigned long tgranule_16K:4; unsigned long tgranule_64K:4; unsigned long tgranule_4K:4; +#if !defined(CONFIG_MMU ) + unsigned long __res:16; + unsigned long msa:4; + unsigned long msa_frac:4; + unsigned long __res0:8; +#else unsigned long tgranule_16k_2:4; unsigned long tgranule_64k_2:4; unsigned long tgranule_4k_2:4;
@@ -255,6 +261,7 @@ struct cpuinfo_arm { unsigned long __res0:8; unsigned long fgt:4; unsigned long ecv:4; +#endif /* MMFR1 */ unsigned long hafdbs:4;
xen/arch/arm/include/asm/mpu.h +6 -1
@@ -87,7 +87,12 @@ static inline bool region_is_valid(const pr_t *pr)return pr->prlar.reg.en; } -#endif /* __ASSEMBLER__ */ +static inline register_t generate_vsctlr (uint16_t vmid) +{ +return ((register_t )vmid <<VSCTLR_VMID_SHIFT ); +} + +#endif /* __ASSEMBLY__ */ #endif /* __ARM_MPU_H__ */
xen/arch/arm/include/asm/mpu/p2m.h +12 -0
@@ -5,6 +5,18 @@struct p2m_domain ; +/* +* The architecture allows at most 255 EL2 MPU memory regions. The size of the +* MPU structure entry (pr_t) is 32 Bytes on AArch64 (requiring two 4KB pages) +* and 16 bytes on AArch32 (requiring one 4KB page). +*/ +#ifdef CONFIG_ARM_64 +#define P2M_ROOT_ORDER 1 +#else +#define P2M_ROOT_ORDER 0 +#endif + +/* Not used on MPU system */ static inline void p2m_clear_root_pages (struct p2m_domain *p2m) {}static inline void p2m_tlb_flush_sync (struct p2m_domain *p2m) {}
xen/arch/arm/include/asm/p2m.h +5 -0
@@ -48,8 +48,13 @@ struct p2m_domain {/* Current VMID in use */ uint16_t vmid; +#if defined(CONFIG_MMU )/* Current Translation Table Base Register for the p2m */ uint64_t vttbr; +#else +/* Current Virtualization System Control Register for the p2m */ +register_t vsctlr; +#endif /* Highest guest frame that's ever been mapped in the p2m */ gfn_t max_mapped_gfn;
xen/arch/arm/include/asm/processor.h +13 -0
@@ -403,6 +403,12 @@#define VTCR_RES1 (_AC(1,UL)<<31) +#if !defined(CONFIG_MMU ) && defined(CONFIG_ARM_64 ) +#define VTCR_MSA_VMSA (_AC(0x1,UL)<<31) +#define VTCR_MSA_PMSA ~(_AC(0x1,UL)<<31) +#define NSA_SEL2 ~(_AC(0x1,UL)<<30) +#endif +/* HCPTR Hyp. Coprocessor Trap Register */ #define HCPTR_TAM ((_AC(1,U)<<30))#define HCPTR_TTA ((_AC(1,U)<<20))/* Trap trace registers */
@@ -464,6 +470,13 @@#define MM64_VMID_16_BITS_SUPPORT 0x2#endif +#if !defined(CONFIG_MMU ) && defined(CONFIG_ARM_64 ) +#define MM64_MSA_PMSA_SUPPORT 0xf +#define MM64_MSA_FRAC_NONE_SUPPORT 0x0 +#define MM64_MSA_FRAC_PMSA_SUPPORT 0x1 +#define MM64_MSA_FRAC_VMSA_SUPPORT 0x2 +#endif +#ifndef __ASSEMBLER__extern register_t __cpu_logical_map[];
xen/arch/arm/mpu/p2m.c +68 -3
85 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 9 lines · Harry Ramsey
xen/arch/arm/include/asm/mpu.h +0
-0@@ -87,7 +87,12 @@ static inline bool region_is_valid(const pr_t *pr)return pr->prlar.reg.en; }
xen/arch/arm/include/asm/mpu.h static inline bool region_is_valid(const pr_t *pr) · line 98
Quoted 10 lines
xen/arch/arm/include/asm/mpu.h +6
-1-#endif /* __ASSEMBLER__ */ +static inline register_t generate_vsctlr (uint16_t vmid) +{ +return ((register_t )vmid <<VSCTLR_VMID_SHIFT ); +} + +#endif /* __ASSEMBLY__ */ #endif /* __ARM_MPU_H__ */
You've got a rebasing error here. __ASSEMBLY__ doesn't exist any more.
~Andrew
Quoted 5 lines · Harry Ramsey
From: Penny Zheng <[email protected]>
For MMU system,
setup_virt_pagingis used to configure stage 2 address translation regime, like IPA bits, VMID allocator set up, etc. Some could be inherited in MPU system, like VMID allocator set up, etc.
The commit msg only mentions setup_virt_paging, so why do you implement other p2m functions in the same commit? I would split the patch in two for clarity and ease of review. I will focus on setup_virt_paging.
Quoted 7 lines
For MPU system, we could have the following memory translation regime:
- PMSAv8-64 at both EL1/EL0 and EL2
- VMSAv8-64 at EL1/EL0 and PMSAv8-64 at EL2
The default option will be the second, unless the platform could not support, which could be checked againstMSA_fracbit in Memory Model Feature Register 0(ID_AA64MMFR0_EL1).
What's the reasoning behind it? Why do we prefer VMSA at EL1 rather than PMSA? AFAICT PMSA is a default and VMSA is optional, so logically PMSA should be preffered. We should also make it configurable I think, so that a user has a choice.
Quoted 28 lines
Signed-off-by Penny Zheng <[email protected]>Signed-off-by Wei Chen <[email protected]>Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Hari Limaye <[email protected]>Signed-off-by Harry Ramsey <[email protected]>--- xen/arch/arm/arm64/mpu/p2m.c | 90 +++++++++++++++++++++++- xen/arch/arm/include/asm/arm32/mpu.h | 2 + xen/arch/arm/include/asm/arm64/mpu.h | 2 + xen/arch/arm/include/asm/arm64/sysregs.h | 5 ++ xen/arch/arm/include/asm/cpufeature.h | 7 ++ xen/arch/arm/include/asm/mpu.h | 7 +- xen/arch/arm/include/asm/mpu/p2m.h | 12 ++++ xen/arch/arm/include/asm/p2m.h | 5 ++ xen/arch/arm/include/asm/processor.h | 13 ++++ xen/arch/arm/mpu/p2m.c | 71 ++++++++++++++++++- 10 files changed, 209 insertions(+), 5 deletions(-)xen/arch/arm/arm64/mpu/p2m.c +1
-0@@ -2,11 +2,99 @@#include <xen/bug.h> #include <xen/init.h> +#include <xen/warning.h>
xen/arch/arm/arm64/mpu/p2m.c line 14
Quoted 10 lines
xen/arch/arm/arm64/mpu/p2m.c +5
-1#include <asm/p2m.h> void __init setup_virt_paging (void ) { -BUG_ON ("unimplemented" ); +uint64_t vtcr_el2 =0 , vstcr_el2 =0 ; +bool p2m_vmsa = true; + +/* PA size */ +const unsigned int pa_range_info[] = {32 ,36 ,40 ,42 ,44 ,48 ,52 ,0 ,/* Invalid */ };
80 chars exceeded.
Do we still need 0 here to denote invalid value?
xen/arch/arm/arm64/mpu/p2m.c line 27
Quoted 13 lines
xen/arch/arm/arm64/mpu/p2m.c +13
-0+ +/* +* Restrict "p2m_ipa_bits" if needed. As P2M table is always configured +* with IPA bits == PA bits, compare against "pabits". +*/ +if ( pa_range_info[system_cpuinfo.mm64.pa_range] < p2m_ipa_bits ) + p2m_ipa_bits = pa_range_info[system_cpuinfo.mm64.pa_range]; + +/* +* Clear VTCR_EL2.NSA bit to configure non-secure stage 2 translation output +* address space to access the Secure PA space. +*/ + vtcr_el2 &=NSA_SEL2 ;
This has no effect.
xen/arch/arm/arm64/mpu/p2m.c line 31
Quoted 4 lines
xen/arch/arm/arm64/mpu/p2m.c +4
-0+ +/* +* The MSA and MSA_frac fields in the ID_AA64MMFR0_EL1 register identify the +* memory system configurations supported at EL1. In Armv8-R AArch64, the
The spec mentions all translation regimes + specific configuration for EL1, so it's not EL1 only.
Quoted 7 lines
xen/arch/arm/arm64/mpu/p2m.c +7
-0+* only permitted value for ID_AA64MMFR0_EL1.MSA is 0b1111. +*/ +if ( system_cpuinfo.mm64.msa !=MM64_MSA_PMSA_SUPPORT ) + goto fault; + +/* Permitted values for ID_AA64MMFR0_EL1.MSA_frac are 0b0001 and 0b0010. */ +if ( system_cpuinfo.mm64.msa_frac ==MM64_MSA_FRAC_NONE_SUPPORT )
xen/arch/arm/arm64/mpu/p2m.c line 48
Quoted 10 lines
xen/arch/arm/arm64/mpu/p2m.c +10
-0+ goto fault; + +/* +* When ID_AA64MMFR0_EL1.MSA_frac is 0b0010 the stage 1 EL1&0 translation +* regime supports both PMSAv8-64 and VMSAv8-64. +*/ +if ( system_cpuinfo.mm64.msa_frac !=MM64_MSA_FRAC_VMSA_SUPPORT ) + { + p2m_vmsa = false; +warning_add ("Be aware of that there is no support for VMSAv8-64 at EL1 on this platform\n" );
I think we should make PMSA a default.
If at all, a warning message could be made simpler e.g. "No support for VMSAv8-64 at EL1". Seeing a warning already means that user should be aware of sth and it's clear that the message is about this platform.
Quoted 14 lines
xen/arch/arm/arm64/mpu/p2m.c +14
-0+ } + +/* +* If PE supports both PMSAv8-64 and VMSAv8-64 at EL1, then VTCR_EL2.MSA +* determines the memory system architecture enabled at stage 1 of the +* Secure EL1&0 translation regime. +* +* Normally, we set the initial VTCR_EL2.MSA value VMSAv8-64 support, +* unless this platform only supports PMSAv8-64. +*/ +if ( !p2m_vmsa ) + vtcr_el2 &=VTCR_MSA_PMSA ; +else + vtcr_el2 |=VTCR_MSA_VMSA ;
xen/arch/arm/arm64/mpu/p2m.c line 72
Quoted 10 lines
xen/arch/arm/arm64/mpu/p2m.c +10
-0+ +/* +* cpuinfo sanitization makes sure we support 16bits VMID only if all cores +* are supporting it. +*/ +if ( system_cpuinfo.mm64.vmid_bits ==MM64_VMID_16_BITS_SUPPORT ) + max_vmid =MAX_VMID_16_BIT ; + +/* Set the VS bit only if 16 bit VMID is supported. */ +if (MAX_VMID ==MAX_VMID_16_BIT )
Instead of a macro, use max_vmid variable here
xen/arch/arm/arm64/mpu/p2m.c line 77
Quoted 5 lines
xen/arch/arm/arm64/mpu/p2m.c +5
-0+ vtcr_el2 |=VTCR_VS ; + +p2m_vmid_allocator_init (); + +WRITE_SYSREG (vtcr_el2,VTCR_EL2 );
Where do we set NSA bit?
xen/arch/arm/arm64/mpu/p2m.c line 82
Quoted 5 lines
xen/arch/arm/arm64/mpu/p2m.c +5
-0+ +/* +* VSTCR_EL2.SA defines secure stage 2 translation output address space. +* To make sure that all stage 2 translations for the Secure PA space access +* the Secure PA space, we keep SA bit as 0.
This says that we want to access secure PA space but not why.
xen/arch/arm/arm64/mpu/p2m.c line 89
Quoted 7 lines
xen/arch/arm/arm64/mpu/p2m.c +7
-0+* +* VSTCR_EL2.SC is NS check enable bit. To make sure that Stage 2 NS +* configuration is checked against stage 1 NS configuration in EL1&0 +* translation regime for the given address, and generates a fault if they +* are different, we set SC bit 1. +*/ + vstcr_el2 =1 <<VSTCR_EL2_RES1_SHIFT ;
Why are we setting the RES1 bit?
xen/arch/arm/arm64/mpu/p2m.c line 90
Quoted 1 line
xen/arch/arm/arm64/mpu/p2m.c +1
-0+ vstcr_el2 &=VSTCR_EL2_SA ;
This has no effect because you initialized it to 0.
Quoted 57 lines
xen/arch/arm/arm64/mpu/p2m.c +7
-0+ vstcr_el2 |=VSTCR_EL2_SC ; +WRITE_SYSREG (vstcr_el2,VSTCR_EL2 ); + +return ; + + fault: +panic ("Hardware with no PMSAv8-64 support in any translation regime\n" ); }/* xen/arch/arm/include/asm/arm32/mpu.h +2
-0@@ -11,6 +11,8 @@*/ #define MPU_REGION_RES0 0x0 +#define VSCTLR_VMID_SHIFT 16 +/* Hypervisor Protection Region Base Address Register */ typedef union { struct {xen/arch/arm/include/asm/arm64/mpu.h +2
-0@@ -7,6 +7,8 @@#define MPU_REGION_RES0 (0xFFFFULL << 48) +#define VSCTLR_VMID_SHIFT 48 +/* Protection Region Base Address Register */ typedef union { struct __packed {xen/arch/arm/include/asm/arm64/sysregs.h +5
-0@@ -462,6 +462,11 @@#define ZCR_ELx_LEN_SIZE 9#define ZCR_ELx_LEN_MASK 0x1ff +/* Virtualization Secure Translation Control Register */ +#define VSTCR_EL2_RES1_SHIFT 31 +#define VSTCR_EL2_SA ~(_AC(0x1,UL)<<30) +#define VSTCR_EL2_SC (_AC(0x1,UL)<<20) +#ifdef CONFIG_MPU /* * The Armv8-R AArch64 architecture always executes code in Secure xen/arch/arm/include/asm/cpufeature.h +0
-0
xen/arch/arm/include/asm/cpufeature.h struct cpuinfo_arm { · line 251
Quoted 4 lines
xen/arch/arm/include/asm/cpufeature.h +1
-0@@ -248,6 +248,12 @@ struct cpuinfo_arm { unsigned long tgranule_16K:4; unsigned long tgranule_64K:4; unsigned long tgranule_4K:4; +#if !defined(CONFIG_MMU )
Why not #ifdef CONFIG_MPU here and elsewhere?
xen/arch/arm/include/asm/cpufeature.h struct cpuinfo_arm { · line 252
Quoted 1 line
xen/arch/arm/include/asm/cpufeature.h +1
-0+ unsigned long __res:16;
If there are multiple RES0 fields, we usually start from __res0 and iterate the number with each occurence of RES0.
~Michal
Quoted 9 lines · Michal
Quoted 6 lines
For MPU system, we could have the following memory translation regime:
- PMSAv8-64 at both EL1/EL0 and EL2
- VMSAv8-64 at EL1/EL0 and PMSAv8-64 at EL2
The default option will be the second, unless the platform could not support, which could be checked againstMSA_fracbit in Memory Model Feature Register 0(ID_AA64MMFR0_EL1).What's the reasoning behind it? Why do we prefer VMSA at EL1 rather than PMSA? AFAICT PMSA is a default and VMSA is optional, so logically PMSA should be preffered. We should also make it configurable I think, so that a user has a choice.
Can we introduce this as a device tree parameter since we may want to run multiple guests with one guest to use PMSA such as Zephyr and Linux to use VMSA?
Quoted 8 lines
xen/arch/arm/arm64/mpu/p2m.c line 14
Quoted 6 lines
xen/arch/arm/arm64/mpu/p2m.c +5
-1-BUG_ON ("unimplemented" ); +uint64_t vtcr_el2 =0 , vstcr_el2 =0 ; +bool p2m_vmsa = true; + +/* PA size */ +const unsigned int pa_range_info[] = {32 ,36 ,40 ,42 ,44 ,48 ,52 ,0 ,/* Invalid */ };80 chars exceeded.
Do we still need 0 here to denote invalid value?
It is possible for the architecture to be expanded with new features which increase the potential PA range (requiring additional support). Additionally for consistency this is how it is defined in MMU. Is there another reason why we should remove it?
Quoted 6 lines
xen/arch/arm/arm64/mpu/p2m.c line 77
Quoted 5 lines
xen/arch/arm/arm64/mpu/p2m.c +5
-0+ vtcr_el2 |=VTCR_VS ; + +p2m_vmid_allocator_init (); + +WRITE_SYSREG (vtcr_el2,VTCR_EL2 );Where do we set NSA bit?
On ARMv8-R EL0, EL1 and EL2 all run in the secure state so we do not set that bit in this context.
Regarding the other comments, I plan on reworking/cleaning up the patch to address them and make the code/intents clearer.
Thanks,
Harry Ramsey