Fourth MPU Series
v2 cover Fourth MPU Series
This series aims to further the ongoing work to introduce support for MPU systems in xen.
The patches in this series implement various memory functions and enable the hypervisor timer.
Luca Fancellu (3):
arm/mpu: Implement copy_from_paddr for MPU systems arm/mpu: Implement vmap functions for MPU arm/mpu: Introduce modify_after_init_mappings
Penny Zheng (3):
arm/mpu: Implement free_init_memory for MPU systems
arm: Use secure hypervisor timer in MPU system arm/mpu: Map domain page in AArch64 MPU systems
xen/arch/arm/Kconfig | 1 + xen/arch/arm/include/asm/arm64/sysregs.h | 11 ++ xen/arch/arm/include/asm/mpu/mm.h | 16 +- xen/arch/arm/include/asm/setup.h | 5 + xen/arch/arm/mmu/setup.c | 15 ++ xen/arch/arm/mpu/Makefile | 1 + xen/arch/arm/mpu/domain-page.c | 53 +++++++ xen/arch/arm/mpu/mm.c | 189 ++++++++++++++++++----- xen/arch/arm/mpu/setup.c | 54 ++++++- xen/arch/arm/mpu/vmap.c | 14 +- xen/arch/arm/setup.c | 15 +- 11 files changed, 318 insertions(+), 56 deletions(-) create mode 100644 xen/arch/arm/mpu/domain-page.c
--
2.43.0
PATCH v2 1/6 arm/mpu: Implement copy_from_paddr for MPU systems
From: Luca Fancellu <[email protected]>
Implement the function copy_from_paddr variant for MPU systems, using the map_pages_to_xen/destroy_xen_mappings to temporarily map the memory range to be copied.
---
v2:
- add Michal R-by
--- xen/arch/arm/mpu/setup.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
xen/arch/arm/mpu/setup.c +13 -1
@@ -91,7 +91,19 @@ void * __init early_fdt_map(paddr_t fdt_paddr)*/ void __init copy_from_paddr (void *dst,paddr_t paddr,unsigned long len) { -BUG_ON ("unimplemented" ); +paddr_t start_pg =round_pgdown (paddr); +paddr_t end_pg =round_pgup (paddr + len); +unsigned long nr_mfns = (end_pg - start_pg) >>PAGE_SHIFT ; +mfn_t mfn =maddr_to_mfn (start_pg); + +if (map_pages_to_xen (start_pg, mfn, nr_mfns,PAGE_HYPERVISOR_WC ) ) +panic ("Unable to map range for copy_from_paddr\n" ); + +memcpy (dst,maddr_to_virt (paddr), len); +clean_dcache_va_range (dst, len); + +if (destroy_xen_mappings (start_pg, end_pg) ) +panic ("Unable to unmap range for copy_from_paddr\n" ); }void __init remove_early_mappings (void )
Signature · 1 line
2.43.0
Re: [PATCH v2 1/6] arm/mpu: Implement copy_from_paddr for MPU systems
Quoted 10 lines · Harry Ramsey
From: Luca Fancellu <[email protected]>
Implement the function
copy_from_paddrvariant for MPU systems, using the map_pages_to_xen/destroy_xen_mappings to temporarily map the memory range to be copied.Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Hari Limaye <[email protected]>Signed-off-by Harry Ramsey <[email protected]>Reviewed-by default avatarMichal Orzel <[email protected]>
This tag looks to be broken.
~Michal
PATCH v2 2/6 arm/mpu: Implement vmap functions for MPU
In reply to Harry RamseyFrom: Luca Fancellu <[email protected]>
HAS_VMAP is not enabled on MPU systems, but the vmap functions are used in places across common code. In order to keep the existing code and maintain correct functionality, implement the vmap_contig and vunmap functions for MPU systems.
Introduce a helper function destroy_xen_mapping_containing to aid with unmapping an entire region when only the start address is known.
---
v2:
- Rename destroy_entire_xen_mapping to destroy_xen_mapping_containing
- Improve code documentation.
- Refactor nested code.
- Fix ignored rc error code in destroy_xen_mapping_containing.
--- xen/arch/arm/include/asm/mpu/mm.h | 10 +++++ xen/arch/arm/mpu/mm.c | 74 ++++++++++++++++++++++++++----- xen/arch/arm/mpu/vmap.c | 14 ++++-- 3 files changed, 83 insertions(+), 15 deletions(-)
xen/arch/arm/include/asm/mpu/mm.h +10 -0
@@ -111,6 +111,16 @@ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags);int mpumap_contains_region (pr_t *table,uint8_t nr_regions,paddr_t base,paddr_t limit,uint8_t *index); + +/* +* Destroys and frees (if reference count is 0) an entire xen mapping on MPU +* systems where only the start address is known. +* +* @param s Start address of memory region to be destroyed. +* @return: 0 on success, negative on error. +*/ +int destroy_xen_mapping_containing (paddr_t s); +#endif /* __ARM_MPU_MM_H__ */ /*
xen/arch/arm/mpu/mm.c +62 -12
92 lines not on this page · raw message
xen/arch/arm/mpu/vmap.c +11 -3
@@ -1,19 +1,27 @@/* SPDX-License-Identifier: GPL-2.0-only */ #include <xen/bug.h> +#include <xen/mm.h> #include <xen/mm-frame.h> #include <xen/types.h> #include <xen/vmap.h> void *vmap_contig (mfn_t mfn,unsigned int nr) { -BUG_ON ("unimplemented" ); -return NULL ; +paddr_t base =mfn_to_maddr (mfn); + +if (map_pages_to_xen (base, mfn, nr,PAGE_HYPERVISOR ) ) +return NULL ; + +return maddr_to_virt (base); }void vunmap (const void *va) { -BUG_ON ("unimplemented" ); +paddr_t base =virt_to_maddr (va); + +if (destroy_xen_mapping_containing (base) ) +panic ("Failed to vunmap region\n" ); }/*
Signature · 1 line
2.43.0
Re: [PATCH v2 2/6] arm/mpu: Implement vmap functions for MPU
Quoted 73 lines · Harry Ramsey
From: Luca Fancellu <[email protected]>
HAS_VMAPis not enabled on MPU systems, but the vmap functions are used in places across common code. In order to keep the existing code and maintain correct functionality, implement thevmap_contigandvunmapfunctions for MPU systems.Introduce a helper function
destroy_xen_mapping_containingto aid with unmapping an entire region when only the start address is known.Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Harry Ramsey <[email protected]>---
v2:
- Renamedestroy_entire_xen_mappingtodestroy_xen_mapping_containing
- Improve code documentation.
- Refactor nested code.
- Fix ignored rc error code indestroy_xen_mapping_containing.--- xen/arch/arm/include/asm/mpu/mm.h | 10 +++++ xen/arch/arm/mpu/mm.c | 74 ++++++++++++++++++++++++++----- xen/arch/arm/mpu/vmap.c | 14 ++++-- 3 files changed, 83 insertions(+), 15 deletions(-)xen/arch/arm/include/asm/mpu/mm.h +10
-0@@ -111,6 +111,16 @@ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags);int mpumap_contains_region (pr_t *table,uint8_t nr_regions,paddr_t base,paddr_t limit,uint8_t *index); + +/* +* Destroys and frees (if reference count is 0) an entire xen mapping on MPU +* systems where only the start address is known. +* +* @param s Start address of memory region to be destroyed. +* @return: 0 on success, negative on error. +*/ +int destroy_xen_mapping_containing (paddr_t s); +#endif /* __ARM_MPU_MM_H__ */ /* xen/arch/arm/mpu/mm.c +20
-0@@ -290,6 +290,42 @@ static void disable_mpu_region_from_index(uint8_t index)write_protection_region (&xen_mpumap[index], index); } +/* +* Free a xen_mpumap entry given the index. A mpu region is actually disabled +* when the refcount is 0 and the region type is MPUMAP_REGION_FOUND. +* +* @param idx Index of the mpumap entry. +* @param region_found_type MPUMAP_REGION_* value. +* @return 0 on success, otherwise negative on error. +*/ +static int xen_mpumap_free_entry (uint8_t idx,int region_found_type) +{ +ASSERT (spin_is_locked (&xen_mpumap_lock)); +ASSERT (idx !=INVALID_REGION_IDX ); + +if (MPUMAP_REGION_OVERLAP == region_found_type ) + { +printk (XENLOG_ERR "Cannot remove an overlapping region\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount )
xen/arch/arm/mpu/mm.c static void disable_mpu_region_from_index(uint8_t index) · line 322
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+ { + xen_mpumap[idx].refcount -=1 ; +return 0 ; + } + +if (MPUMAP_REGION_FOUND == region_found_type ) +disable_mpu_region_from_index (idx); +else + { +printk (XENLOG_ERR "Cannot remove a partial region\n" );
Shouldn't this be moved above refcount checking? Do we expect this function to be called with region_found_type being MPUMAP_REGION_INCLUSIVE?
Quoted 37 lines
xen/arch/arm/mpu/mm.c +11
-12+return -EINVAL ; + } + +return 0 ; +} +/* * Update the entry in the MPU memory region mapping table (xen_mpumap) for the * given memory range and flags, creating one if none exists. @@ -357,18 +393,7 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,return -EINVAL ; } -if ( xen_mpumap[idx].refcount ==0 ) - { -if (MPUMAP_REGION_FOUND == rc ) -disable_mpu_region_from_index (idx); -else - { -printk ("Cannot remove a partial region\n" ); -return -EINVAL ; - } - } -else - xen_mpumap[idx].refcount -=1 ; +return xen_mpumap_free_entry (idx, rc); }return 0 ;@@ -418,6 +443,31 @@ int destroy_xen_mappings(unsigned long s, unsigned long e)return xen_mpumap_update (s, e,0 ); } +int destroy_xen_mapping_containing (paddr_t s) +{ +int rc; +uint8_t idx;
xen/arch/arm/mpu/mm.c int destroy_xen_mappings(unsigned long s, unsigned long e) · line 459
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+ +ASSERT (IS_ALIGNED (s,PAGE_SIZE )); + +spin_lock (&xen_mpumap_lock); + + rc =mpumap_contains_region (xen_mpumap, max_mpu_regions, s, s +PAGE_SIZE , + &idx); +if ( rc ==MPUMAP_REGION_NOTFOUND ) + { +printk (XENLOG_ERR "Cannot remove entry that does not exist" );
Why do we split sanity checking between this and xen_mpumap_free_entry? What are the possible region types that xen_mpumap_free_entry is expected to work with? I thought that it should only be MPUMAP_REGION_FOUND.
~Michal
Re: [PATCH v2 2/6] arm/mpu: Implement vmap functions for MPU
Quoted 79 lines · Michal
Quoted 77 lines · Harry Ramsey
From: Luca Fancellu <[email protected]>
HAS_VMAPis not enabled on MPU systems, but the vmap functions are used in places across common code. In order to keep the existing code and maintain correct functionality, implement thevmap_contigandvunmapfunctions for MPU systems.Introduce a helper function
destroy_xen_mapping_containingto aid with unmapping an entire region when only the start address is known.Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Harry Ramsey <[email protected]>---
v2:
- Renamedestroy_entire_xen_mappingtodestroy_xen_mapping_containing
- Improve code documentation.
- Refactor nested code.
- Fix ignored rc error code indestroy_xen_mapping_containing.--- xen/arch/arm/include/asm/mpu/mm.h | 10 +++++ xen/arch/arm/mpu/mm.c | 74 ++++++++++++++++++++++++++----- xen/arch/arm/mpu/vmap.c | 14 ++++-- 3 files changed, 83 insertions(+), 15 deletions(-)xen/arch/arm/include/asm/mpu/mm.h +10
-0@@ -111,6 +111,16 @@ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags);int mpumap_contains_region (pr_t *table,uint8_t nr_regions,paddr_t base,paddr_t limit,uint8_t *index); + +/* +* Destroys and frees (if reference count is 0) an entire xen mapping on MPU +* systems where only the start address is known. +* +* @param s Start address of memory region to be destroyed. +* @return: 0 on success, negative on error. +*/ +int destroy_xen_mapping_containing (paddr_t s); +#endif /* __ARM_MPU_MM_H__ */ /* xen/arch/arm/mpu/mm.c +24
-0@@ -290,6 +290,42 @@ static void disable_mpu_region_from_index(uint8_t index)write_protection_region (&xen_mpumap[index], index); } +/* +* Free a xen_mpumap entry given the index. A mpu region is actually disabled +* when the refcount is 0 and the region type is MPUMAP_REGION_FOUND. +* +* @param idx Index of the mpumap entry. +* @param region_found_type MPUMAP_REGION_* value. +* @return 0 on success, otherwise negative on error. +*/ +static int xen_mpumap_free_entry (uint8_t idx,int region_found_type) +{ +ASSERT (spin_is_locked (&xen_mpumap_lock)); +ASSERT (idx !=INVALID_REGION_IDX ); + +if (MPUMAP_REGION_OVERLAP == region_found_type ) + { +printk (XENLOG_ERR "Cannot remove an overlapping region\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount ) + { + xen_mpumap[idx].refcount -=1 ; +return 0 ; + }
Quoted 8 lines
xen/arch/arm/mpu/mm.c static void disable_mpu_region_from_index(uint8_t index) · line 322
Quoted 6 lines
xen/arch/arm/mpu/mm.c +6
-0+ +if (MPUMAP_REGION_FOUND == region_found_type ) +disable_mpu_region_from_index (idx); +else + { +printk (XENLOG_ERR "Cannot remove a partial region\n" );Shouldn't this be moved above refcount checking? Do we expect this function to be called with
region_found_typebeingMPUMAP_REGION_INCLUSIVE?
This function is expected to be given MPUMAP_REGION_INCLUSIVE and in doing so decrease refcount by 1. If however refcount is already 0, the region can only be freed by calling the entire region bounds. This message is moreso a debug error that our MPU API has been used in an incorrect way, it should not generally occur but I would prefer to keep it for sanity checking.
Quoted 43 lines
Quoted 43 lines
xen/arch/arm/mpu/mm.c +16
-12+return -EINVAL ; + } + +return 0 ; +} +/* * Update the entry in the MPU memory region mapping table (xen_mpumap) for the * given memory range and flags, creating one if none exists. @@ -357,18 +393,7 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,return -EINVAL ; } -if ( xen_mpumap[idx].refcount ==0 ) - { -if (MPUMAP_REGION_FOUND == rc ) -disable_mpu_region_from_index (idx); -else - { -printk ("Cannot remove a partial region\n" ); -return -EINVAL ; - } - } -else - xen_mpumap[idx].refcount -=1 ; +return xen_mpumap_free_entry (idx, rc); }return 0 ;@@ -418,6 +443,31 @@ int destroy_xen_mappings(unsigned long s, unsigned long e)return xen_mpumap_update (s, e,0 ); } +int destroy_xen_mapping_containing (paddr_t s) +{ +int rc; +uint8_t idx; + +ASSERT (IS_ALIGNED (s,PAGE_SIZE )); + +spin_lock (&xen_mpumap_lock); +
Quoted 8 lines
xen/arch/arm/mpu/mm.c int destroy_xen_mappings(unsigned long s, unsigned long e) · line 459
Quoted 5 lines
xen/arch/arm/mpu/mm.c +5
-0+ rc =mpumap_contains_region (xen_mpumap, max_mpu_regions, s, s +PAGE_SIZE , + &idx); +if ( rc ==MPUMAP_REGION_NOTFOUND ) + { +printk (XENLOG_ERR "Cannot remove entry that does not exist" );Why do we split sanity checking between this and
xen_mpumap_free_entry? What are the possible region types thatxen_mpumap_free_entryis expected to work with? I thought that it should only beMPUMAP_REGION_FOUND.
I will move the region checks to xen_mpumap_free_entry since we only want xen_mpumap_update_entry and xen_mpumap_free_entry to modify our refcount properties. These functions should then account for all potential values of MPUMAP_REGION_*.
Quoted 2 lines
~Michal
Thanks,
Harry Ramsey.
Re: [PATCH v2 2/6] arm/mpu: Implement vmap functions for MPU
Quoted 54 lines · Harry Ramsey
Quoted 44 lines · Michal
Quoted 42 lines · Harry Ramsey
+ return
-EINVAL;
+ }xen/arch/arm/mpu/mm.c +4
-0+ + return 0; +} +/*
* Update the entry in the MPU memory region mapping table (xen_mpumap) for the
* given memory range and flags, creating one if none exists.xen/arch/arm/mpu/mm.c +10
-11@@ -357,18 +393,7 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, return -EINVAL; } - if ( xen_mpumap[idx].refcount == 0 ) - { - if ( MPUMAP_REGION_FOUND == rc ) - disable_mpu_region_from_index(idx); - else - { - printk("Cannot remove a partial region\n"); - return -EINVAL; - } - } - else - xen_mpumap[idx].refcount -= 1; + return xen_mpumap_free_entry(idx, rc); } return 0;@@ -418,6 +443,31 @@ int destroy_xen_mappings(unsigned long s, unsigned long e) return xen_mpumap_update(s, e, 0); } +int destroy_xen_mapping_containing(paddr_t s) +{ + int rc; + uint8_t idx; + + ASSERT(IS_ALIGNED(s, PAGE_SIZE)); + + spin_lock(&xen_mpumap_lock); + + rc = mpumap_contains_region(xen_mpumap, max_mpu_regions, s, s +Quoted 8 lines
xen/arch/arm/mpu/mm.c int destroy_xen_mappings(unsigned long s, unsigned long e) · line 459
Quoted 5 lines
xen/arch/arm/mpu/mm.c +4
-0PAGE_SIZE , + &idx); + if ( rc == MPUMAP_REGION_NOTFOUND ) + { + printk(XENLOG_ERR "Cannot remove entry that does not exist");Why do we split sanity checking between this and
xen_mpumap_free_entry? What are the possible region types thatxen_mpumap_free_entryis expected to work with? I thought that it should only beMPUMAP_REGION_FOUND.
Quoted 1 line
I will move the region checks to
xen_mpumap_free_entrysince we only wantxen_mpumap_update_entryandxen_mpumap_free_entryto modify our refcount properties. These functions should then account for all potential values of MPUMAP_REGION_*.
Sorry, after looking back at this code the reason the sanity checking happens here is because we require MPUMAP_REGION_FOUND to be used in destroy_xen_mapping_containing to actually remove the region. In destroy_xen_mapping_containing we are forcibly setting the region type to be MPUMAP_REGION_FOUND to ensure it is removed and thus we require the check here too.
Thanks,
Harry Ramsey.
Re: [PATCH v2 2/6] arm/mpu: Implement vmap functions for MPU
In reply to Harry RamseyAlso, two more things here apart from my other remarks.
Quoted 19 lines · Harry Ramsey
From: Luca Fancellu <[email protected]>
HAS_VMAPis not enabled on MPU systems, but the vmap functions are used in places across common code. In order to keep the existing code and maintain correct functionality, implement thevmap_contigandvunmapfunctions for MPU systems.Introduce a helper function
destroy_xen_mapping_containingto aid with unmapping an entire region when only the start address is known.Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Harry Ramsey <[email protected]>---
v2:
- Renamedestroy_entire_xen_mappingtodestroy_xen_mapping_containing
- Improve code documentation.
- Refactor nested code.
- Fix ignored rc error code indestroy_xen_mapping_containing.
---
...
xen/arch/arm/mpu/mm.c int destroy_xen_mappings(unsigned long s, unsigned long e) · line 453
Quoted 9 lines
xen/arch/arm/mpu/mm.c +8
-0+int destroy_xen_mapping_containing (paddr_t s) +{ +int rc; +uint8_t idx; + +ASSERT (IS_ALIGNED (s,PAGE_SIZE )); + +spin_lock (&xen_mpumap_lock);
Here you take a lock...
xen/arch/arm/mpu/mm.c int destroy_xen_mappings(unsigned long s, unsigned long e) · line 460
Quoted 7 lines
xen/arch/arm/mpu/mm.c +7
-0+ + rc =mpumap_contains_region (xen_mpumap, max_mpu_regions, s, s +PAGE_SIZE , + &idx); +if ( rc ==MPUMAP_REGION_NOTFOUND ) + { +printk (XENLOG_ERR "Cannot remove entry that does not exist" ); +return -EINVAL ;
and here you would return while still holding the lock i.e. deadlock.
xen/arch/arm/mpu/mm.c int destroy_xen_mappings(unsigned long s, unsigned long e) · line 464
Quoted 4 lines
xen/arch/arm/mpu/mm.c +4
-0+ } + +/* As we are unmapping entire region use MPUMAP_REGION_FOUND instead */ + rc =xen_mpumap_free_entry (idx,MPUMAP_REGION_FOUND );
Why don't you perform context_sync_mpu() here?
~Michal
PATCH v2 3/6 arm/mpu: Implement free_init_memory for MPU systems
In reply to Harry RamseyFrom: Penny Zheng <[email protected]>
Implement the function free_init_memory for MPU systems. In order to support this, the function modify_xen_mappings is implemented.
On MPU systems, we map the init text and init data sections using separate MPU memory regions. Therefore these are removed separately in free_init_memory.
Additionally remove warning messages from is_mm_attr_match as some permissions can now be updated by xen_mpumap_update_entry.
---
v2:
- Refactor is_mm_attr_match to return logical values regarding the permission mismatch.
- Improve code documentation.
--- xen/arch/arm/include/asm/mpu/mm.h | 6 +- xen/arch/arm/include/asm/setup.h | 2 + xen/arch/arm/mpu/mm.c | 113 +++++++++++++++++++++++------- 3 files changed, 95 insertions(+), 26 deletions(-)
xen/arch/arm/include/asm/mpu/mm.h +5 -1
@@ -15,7 +15,11 @@#define MPUMAP_REGION_FOUND 1#define MPUMAP_REGION_INCLUSIVE 2 -#define INVALID_REGION_IDX 0xFFU +#define MPU_ATTR_RO_MISMATCH -1 +#define MPU_ATTR_XN_MISMATCH -2 +#define MPU_ATTR_AI_MISMATCH -3 + +#define INVALID_REGION_IDX 0xFFUextern struct page_info *frame_table;
xen/arch/arm/include/asm/setup.h +2 -0
@@ -65,6 +65,8 @@ int map_irq_to_domain(struct domain *d, unsigned int irq,int map_range_to_domain (const struct dt_device_node *dev,uint64_t addr,uint64_t len,void *data); +extern const char __init_data_begin[], __bss_start[], __bss_end[]; +struct init_info {/* Pointer to the stack, used by head.S when entering in C */
xen/arch/arm/mpu/mm.c +88 -25
148 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 3/6] arm/mpu: Implement free_init_memory for MPU systems
Quoted 33 lines · Harry Ramsey
From: Penny Zheng <[email protected]>
Implement the function
free_init_memoryfor MPU systems. In order to support this, the functionmodify_xen_mappingsis implemented.On MPU systems, we map the init text and init data sections using separate MPU memory regions. Therefore these are removed separately in
free_init_memory.Additionally remove warning messages from
is_mm_attr_matchas some permissions can now be updated byxen_mpumap_update_entry.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]>---
v2:
- Refactoris_mm_attr_matchto return logical values regarding the permission mismatch.
- Improve code documentation.--- xen/arch/arm/include/asm/mpu/mm.h | 6 +- xen/arch/arm/include/asm/setup.h | 2 + xen/arch/arm/mpu/mm.c | 113 +++++++++++++++++++++++------- 3 files changed, 95 insertions(+), 26 deletions(-)xen/arch/arm/include/asm/mpu/mm.h +0
-0
xen/arch/arm/include/asm/mpu/mm.h line 20
Quoted 7 lines
xen/arch/arm/include/asm/mpu/mm.h +3
-1@@ -15,7 +15,11 @@#define MPUMAP_REGION_FOUND 1#define MPUMAP_REGION_INCLUSIVE 2 -#define INVALID_REGION_IDX 0xFFU +#define MPU_ATTR_RO_MISMATCH -1 +#define MPU_ATTR_XN_MISMATCH -2 +#define MPU_ATTR_AI_MISMATCH -3
You don't seem to use these outside of mm.c, so I would suggest to move them there.
Quoted 36 lines
xen/arch/arm/include/asm/mpu/mm.h +2
-0+ +#define INVALID_REGION_IDX 0xFFUextern struct page_info *frame_table;xen/arch/arm/include/asm/setup.h +2
-0@@ -65,6 +65,8 @@ int map_irq_to_domain(struct domain *d, unsigned int irq,int map_range_to_domain (const struct dt_device_node *dev,uint64_t addr,uint64_t len,void *data); +extern const char __init_data_begin[], __bss_start[], __bss_end[]; +struct init_info {/* Pointer to the stack, used by head.S when entering in C */ xen/arch/arm/mpu/mm.c +1
-7@@ -171,33 +171,18 @@ int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base,return MPUMAP_REGION_NOTFOUND ; } -static bool is_mm_attr_match (pr_t *region,unsigned int attributes) +static int is_mm_attr_match (pr_t *region,unsigned int attributes) {if ( region->prbar.reg.ro !=PAGE_RO_MASK (attributes) ) - { -printk (XENLOG_WARNING -"Mismatched Access Permission attributes (%#x instead of %#x)\n" , - region->prbar.reg.ro,PAGE_RO_MASK (attributes)); -return false; - }
xen/arch/arm/mpu/mm.c int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, · line 180
Quoted 10 lines
xen/arch/arm/mpu/mm.c +2
-6+return MPU_ATTR_RO_MISMATCH ;if ( region->prbar.reg.xn !=PAGE_XN_MASK (attributes) ) - { -printk (XENLOG_WARNING -"Mismatched Execute Never attributes (%#x instead of %#x)\n" , - region->prbar.reg.xn,PAGE_XN_MASK (attributes)); -return false; - } +return MPU_ATTR_XN_MISMATCH ;
Later below you don't seem to differentiate between MPU_ATTR_RO_MISMATCH and MPU_ATTR_XN_MISMATCH. Therefore I would suggest to keep them as one to simplify the code.
Quoted 26 lines
xen/arch/arm/mpu/mm.c +6
-9if ( region->prlar.reg.ai !=PAGE_AI_MASK (attributes) ) - { -printk (XENLOG_WARNING -"Mismatched Memory Attribute Index (%#x instead of %#x)\n" , - region->prlar.reg.ai,PAGE_AI_MASK (attributes)); -return false; - } +return MPU_ATTR_AI_MISMATCH ; -return true; +return 0 ; }/* Map a frame table to cover physical addresses ps through pe */ @@ -357,12 +342,45 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,*/ if ( flags_has_page_present && (rc >=MPUMAP_REGION_FOUND ) ) { -if ( !is_mm_attr_match (&xen_mpumap[idx], flags) ) +int attr_match =is_mm_attr_match (&xen_mpumap[idx], flags); + +/* We do not support modifying AI attribute. */ +if (MPU_ATTR_AI_MISMATCH == attr_match ) { -printk ("Modifying an existing entry is not supported\n" );
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 359
Quoted 10 lines
xen/arch/arm/mpu/mm.c +7
-0+printk (XENLOG_ERR +"Modifying memory attribute is not supported\n" );return -EINVAL ; } +/* +* Permissions RO and XN can be changed only by the full region. +* Permissions that match can continue and just increment refcount. +*/ + if ( MPU_ATTR_RO_MISMATCH == attr_match ||
Enlcose in brackets () || ()
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 372
Quoted 13 lines
xen/arch/arm/mpu/mm.c +13
-0+ MPU_ATTR_XN_MISMATCH == attr_match ) + { +if ( rc ==MPUMAP_REGION_INCLUSIVE ) + { +printk (XENLOG_ERR +"Cannot modify partial region permissions\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount !=0 ) + { +printk (XENLOG_ERR +"Cannot modify memory permissions for a region mapped multiple times\n" );
Memory permission? Here you are checking for XN/RO.
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 378
Quoted 6 lines
xen/arch/arm/mpu/mm.c +6
-0+return -EINVAL ; + } + +/* Set new permission */ + xen_mpumap[idx].prbar.reg.ro =PAGE_RO_MASK (flags); + xen_mpumap[idx].prbar.reg.xn =PAGE_XN_MASK (flags);
Here you always change both, that's why there is no need to provide two separate macros as I mentioned above.
~Michal
Re: [PATCH v2 3/6] arm/mpu: Implement free_init_memory for MPU systems
Hi Michal,
Quoted 76 lines
Quoted 11 lines
xen/arch/arm/mpu/mm.c +1
-1-static bool is_mm_attr_match (pr_t *region,unsigned int attributes) +static int is_mm_attr_match (pr_t *region,unsigned int attributes){ if ( region->prbar.reg.ro != PAGE_RO_MASK(attributes) )xen/arch/arm/mpu/mm.c +0
-6- { -printk (XENLOG_WARNING -"Mismatched Access Permission attributes (%#x instead of %#x)\n" , - region->prbar.reg.ro,PAGE_RO_MASK (attributes)); -return false; - }xen/arch/arm/mpu/mm.c int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, · line 180
Quoted 10 lines
xen/arch/arm/mpu/mm.c +2
-6+return MPU_ATTR_RO_MISMATCH ;if ( region->prbar.reg.xn !=PAGE_XN_MASK (attributes) ) - { -printk (XENLOG_WARNING -"Mismatched Execute Never attributes (%#x instead of %#x)\n" , - region->prbar.reg.xn,PAGE_XN_MASK (attributes)); -return false; - } +return MPU_ATTR_XN_MISMATCH ;Later below you don't seem to differentiate between
MPU_ATTR_RO_MISMATCHandMPU_ATTR_XN_MISMATCH. Therefore I would suggest to keep them as one to simplify the code.Quoted 26 lines
if ( region->prlar.reg.ai != PAGE_AI_MASK(attributes) )xen/arch/arm/mpu/mm.c +2
-7- { -printk (XENLOG_WARNING -"Mismatched Memory Attribute Index (%#x instead of %#x)\n" , - region->prlar.reg.ai,PAGE_AI_MASK (attributes)); -return false; - } +return MPU_ATTR_AI_MISMATCH ; -return true; +return 0 ;}
/* Map a frame table to cover physical addresses ps through pe */
xen/arch/arm/mpu/mm.c +4
-2@@ -357,12 +342,45 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,*/ if ( flags_has_page_present && (rc >=MPUMAP_REGION_FOUND ) ) { -if ( !is_mm_attr_match (&xen_mpumap[idx], flags) ) +int attr_match =is_mm_attr_match (&xen_mpumap[idx], flags); + +/* We do not support modifying AI attribute. */ +if (MPU_ATTR_AI_MISMATCH == attr_match ) { -printk ("Modifying an existing entry is not supported\n" );xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 359
Quoted 10 lines
xen/arch/arm/mpu/mm.c +7
-0+printk (XENLOG_ERR +"Modifying memory attribute is not supported\n" );return -EINVAL ; } +/* +* Permissions RO and XN can be changed only by the full region. +* Permissions that match can continue and just increment refcount. +*/ + if ( MPU_ATTR_RO_MISMATCH == attr_match ||Enlcose in brackets () || ()
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 372
Quoted 13 lines
xen/arch/arm/mpu/mm.c +13
-0+ MPU_ATTR_XN_MISMATCH == attr_match ) + { +if ( rc ==MPUMAP_REGION_INCLUSIVE ) + { +printk (XENLOG_ERR +"Cannot modify partial region permissions\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount !=0 ) + { +printk (XENLOG_ERR +"Cannot modify memory permissions for a region mapped multiple times\n" );
Quoted 1 line
Memory permission? Here you are checking for XN/RO.
Right, is it better “memory attributes”?
Quoted 9 lines
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 378
Quoted 7 lines
xen/arch/arm/mpu/mm.c +6
-0+return -EINVAL ; + } + +/* Set new permission */ + xen_mpumap[idx].prbar.reg.ro =PAGE_RO_MASK (flags); + xen_mpumap[idx].prbar.reg.xn =PAGE_XN_MASK (flags);Here you always change both, that's why there is no need to provide two separate macros as I mentioned above.
good point.
Cheers,
Luca
Re: [PATCH v2 3/6] arm/mpu: Implement free_init_memory for MPU systems
Quoted 78 lines · Luca Fancellu
Hi Michal,
Quoted 76 lines
Quoted 11 lines
xen/arch/arm/mpu/mm.c +1
-1-static bool is_mm_attr_match (pr_t *region,unsigned int attributes) +static int is_mm_attr_match (pr_t *region,unsigned int attributes){ if ( region->prbar.reg.ro != PAGE_RO_MASK(attributes) )xen/arch/arm/mpu/mm.c +0
-6- { -printk (XENLOG_WARNING -"Mismatched Access Permission attributes (%#x instead of %#x)\n" , - region->prbar.reg.ro,PAGE_RO_MASK (attributes)); -return false; - }xen/arch/arm/mpu/mm.c int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, · line 180
Quoted 10 lines
xen/arch/arm/mpu/mm.c +2
-6+return MPU_ATTR_RO_MISMATCH ;if ( region->prbar.reg.xn !=PAGE_XN_MASK (attributes) ) - { -printk (XENLOG_WARNING -"Mismatched Execute Never attributes (%#x instead of %#x)\n" , - region->prbar.reg.xn,PAGE_XN_MASK (attributes)); -return false; - } +return MPU_ATTR_XN_MISMATCH ;Later below you don't seem to differentiate between
MPU_ATTR_RO_MISMATCHandMPU_ATTR_XN_MISMATCH. Therefore I would suggest to keep them as one to simplify the code.Quoted 26 lines
if ( region->prlar.reg.ai != PAGE_AI_MASK(attributes) )xen/arch/arm/mpu/mm.c +2
-7- { -printk (XENLOG_WARNING -"Mismatched Memory Attribute Index (%#x instead of %#x)\n" , - region->prlar.reg.ai,PAGE_AI_MASK (attributes)); -return false; - } +return MPU_ATTR_AI_MISMATCH ; -return true; +return 0 ;}
/* Map a frame table to cover physical addresses ps through pe */
xen/arch/arm/mpu/mm.c +4
-2@@ -357,12 +342,45 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,*/ if ( flags_has_page_present && (rc >=MPUMAP_REGION_FOUND ) ) { -if ( !is_mm_attr_match (&xen_mpumap[idx], flags) ) +int attr_match =is_mm_attr_match (&xen_mpumap[idx], flags); + +/* We do not support modifying AI attribute. */ +if (MPU_ATTR_AI_MISMATCH == attr_match ) { -printk ("Modifying an existing entry is not supported\n" );xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 359
Quoted 10 lines
xen/arch/arm/mpu/mm.c +7
-0+printk (XENLOG_ERR +"Modifying memory attribute is not supported\n" );return -EINVAL ; } +/* +* Permissions RO and XN can be changed only by the full region. +* Permissions that match can continue and just increment refcount. +*/ + if ( MPU_ATTR_RO_MISMATCH == attr_match ||Enlcose in brackets () || ()
Quoted 13 lines
xen/arch/arm/mpu/mm.c +13
-0+ MPU_ATTR_XN_MISMATCH == attr_match ) + { +if ( rc ==MPUMAP_REGION_INCLUSIVE ) + { +printk (XENLOG_ERR +"Cannot modify partial region permissions\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount !=0 ) + { +printk (XENLOG_ERR +"Cannot modify memory permissions for a region mapped multiple times\n" );
Quoted 3 lines
Quoted 1 line
Memory permission? Here you are checking for XN/RO.
Right, is it better “memory attributes”?
Yet in a if() block for MPU_ATTR_AI_MISMATCH == attr_match you already have the same message.
~Michal
Quoted 16 lines
Quoted 10 lines
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 378
Quoted 8 lines
xen/arch/arm/mpu/mm.c +6
-0+return -EINVAL ; + } + +/* Set new permission */ + xen_mpumap[idx].prbar.reg.ro =PAGE_RO_MASK (flags); + xen_mpumap[idx].prbar.reg.xn =PAGE_XN_MASK (flags);Here you always change both, that's why there is no need to provide two separate macros as I mentioned above.
good point.
Cheers,
Luca
PATCH v2 4/6 arm/mpu: Introduce modify_after_init_mappings
In reply to Harry RamseyFrom: Luca Fancellu <[email protected]>
During init_done, Xen sets the permissions of all symbols marked with __ro_after_init to be read-only. This does not work on MPU systems at present because part-region modification is not supported.
Therefore introduce the function modify_after_init_mappings for MMU and MPU, to handle the divergent approaches to setting permissions of __ro_after_init symbols.
For MPU systems modify_xen_mappings will shrink the RW mapping on one side and extend the RO mapping on the other. This approach prevents wasting an additional region between RW and RO mappings.
As the new function is marked with __init, it needs to be called before free_init_memory.
---
v2:
- No changes
--- xen/arch/arm/include/asm/setup.h | 3 +++ xen/arch/arm/mmu/setup.c | 15 ++++++++++++ xen/arch/arm/mpu/mm.c | 2 +- xen/arch/arm/mpu/setup.c | 40 ++++++++++++++++++++++++++++++++ xen/arch/arm/setup.c | 15 ++---------- 5 files changed, 61 insertions(+), 14 deletions(-)
xen/arch/arm/include/asm/setup.h +3 -0
@@ -78,6 +78,9 @@ struct init_infopaddr_t consider_modules (paddr_t s,paddr_t e,uint32_t size,paddr_t align,int first_mod); +/* Modify some mappings after the init is done */ +void modify_after_init_mappings (void ); +#endif /* * Local variables:
xen/arch/arm/mmu/setup.c +15 -0
@@ -213,6 +213,21 @@ void __init remove_early_mappings(void)BUG_ON (rc); } +void __init modify_after_init_mappings (void ) +{ +/* +* We have finished booting. Mark the section .data.ro_after_init +* read-only. +*/ +int rc =modify_xen_mappings ((unsigned long )&__ro_after_init_start, + (unsigned long )&__ro_after_init_end, +PAGE_HYPERVISOR_RO ); + +if ( rc ) +panic ("Unable to mark the .data.ro_after_init section read-only (rc = %d)\n" , + rc); +} +/* * After boot, Xen page-tables should not contain mapping that are both * Writable and eXecutables.
xen/arch/arm/mpu/mm.c +1 -1
@@ -32,7 +32,7 @@ DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR) \/* EL2 Xen MPU memory region mapping table. */ pr_t __cacheline_aligned __section(".data") xen_mpumap[MAX_MPU_REGION_NR]; -static DEFINE_SPINLOCK (xen_mpumap_lock); +DEFINE_SPINLOCK (xen_mpumap_lock);static void __init __maybe_unused build_assertions (void ) {
xen/arch/arm/mpu/setup.c +40 -0
@@ -8,11 +8,14 @@#include <xen/pfn.h> #include <xen/types.h> #include <xen/sizes.h> +#include <xen/spinlock.h> #include <asm/setup.h> static paddr_t __initdata mapped_fdt_base =INVALID_PADDR ;static paddr_t __initdata mapped_fdt_limit =INVALID_PADDR ; +extern spinlock_t xen_mpumap_lock; +void __init setup_pagetables (void ) {}void *__init early_fdt_map (paddr_t fdt_paddr)
@@ -106,6 +109,43 @@ void __init copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)panic ("Unable to unmap range for copy_from_paddr\n" ); } +void __init modify_after_init_mappings (void ) +{ +int rc; +uint8_t idx_rodata; +uint8_t idx_rwdata; + +spin_lock (&xen_mpumap_lock); + + rc =mpumap_contains_region (xen_mpumap, max_mpu_regions, + (unsigned long )_srodata, + (unsigned long )_erodata, + &idx_rodata); + +if ( rc <MPUMAP_REGION_FOUND ) +panic ("Unable to find rodata section (rc = %d)\n" , rc); + + rc =mpumap_contains_region (xen_mpumap, max_mpu_regions, + (unsigned long )__ro_after_init_start, + (unsigned long )__init_begin, + &idx_rwdata); + +if ( rc <MPUMAP_REGION_FOUND ) +panic ("Unable to find rwdata section (rc = %d)\n" , rc); + +/* Shrink rwdata section to begin at __ro_after_init_end */ +pr_set_base (&xen_mpumap[idx_rwdata], (unsigned long )__ro_after_init_end); + +/* Extend rodata section to end at __ro_after_init_end */ +pr_set_limit (&xen_mpumap[idx_rodata], (unsigned long )__ro_after_init_end); + +write_protection_region (&xen_mpumap[idx_rwdata], idx_rwdata); +write_protection_region (&xen_mpumap[idx_rodata], idx_rodata); +context_sync_mpu (); + +spin_unlock (&xen_mpumap_lock); +} +void __init remove_early_mappings (void ) { int rc = destroy_xen_mappings(round_pgdown(mapped_fdt_base),
xen/arch/arm/setup.c +2 -13
@@ -66,23 +66,12 @@ domid_t __read_mostly max_init_domid;static __used void noreturninit_done (void ) { -int rc; -/* Must be done past setting system_state. */ unregister_init_virtual_region (); -free_init_memory (); +modify_after_init_mappings (); -/* -* We have finished booting. Mark the section .data.ro_after_init -* read-only. -*/ - rc =modify_xen_mappings ((unsigned long )&__ro_after_init_start, - (unsigned long )&__ro_after_init_end, -PAGE_HYPERVISOR_RO ); -if ( rc ) -panic ("Unable to mark the .data.ro_after_init section read-only (rc = %d)\n" , - rc); +free_init_memory ();startup_cpu_idle_loop (); }
Signature · 1 line
2.43.0
Re: [PATCH v2 4/6] arm/mpu: Introduce modify_after_init_mappings
Quoted 14 lines · Harry Ramsey
From: Luca Fancellu <[email protected]>
During
init_done, Xen sets the permissions of all symbols marked with__ro_after_initto be read-only. This does not work on MPU systems at present because part-region modification is not supported.Therefore introduce the function
modify_after_init_mappingsfor MMU and MPU, to handle the divergent approaches to setting permissions of__ro_after_initsymbols.For MPU systems
modify_xen_mappingswill shrink the RW mapping on one side and extend the RO mapping on the other. This approach prevents wasting an additional region between RW and RO mappings.
Quoted 6 lines
As the new function is marked with
__init, it needs to be called beforefree_init_memory.Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Hari Limaye <[email protected]>Signed-off-by Harry Ramsey <[email protected]>
~Michal
PATCH v2 5/6 arm: Use secure hypervisor timer in MPU system
In reply to Harry RamseyFrom: Penny Zheng <[email protected]>
As MPU systems only have one secure state, we have to use secure EL2 hypervisor timer for Xen in secure EL2.
---
v2:
- Remove unncessary kconfig attribute.
- Remove unncessary hypervisor timer macro.
--- xen/arch/arm/include/asm/arm64/sysregs.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
xen/arch/arm/include/asm/arm64/sysregs.h +11 -0
@@ -462,6 +462,17 @@#define ZCR_ELx_LEN_SIZE 9#define ZCR_ELx_LEN_MASK 0x1ff +#ifdef CONFIG_MPU +/* +* The Armv8-R AArch64 architecture always executes code in Secure +* state with EL2 as the highest exception level. +* +* Hypervisor timer registers for Secure EL2. +*/ +#define CNTHP_CTL_EL2 CNTHPS_CTL_EL2 +#define CNTHP_CVAL_EL2 CNTHPS_CVAL_EL2 +#endif +#define REGION_TEXT_PRBAR 0x38/* SH=11 AP=10 XN=00 */ #define REGION_RO_PRBAR 0x3A/* SH=11 AP=10 XN=10 */ #define REGION_DATA_PRBAR 0x32/* SH=11 AP=00 XN=10 */
Signature · 1 line
2.43.0
Re: [PATCH v2 5/6] arm: Use secure hypervisor timer in MPU system
Hi Harry,
Quoted 9 lines · Harry Ramsey
From: Penny Zheng <[email protected]>
As MPU systems only have one secure state, we have to use secure EL2 hypervisor timer for Xen in secure EL2.
Signed-off-by Penny Zheng <[email protected]>Signed-off-by Wei Chen <[email protected]>Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Harry Ramsey <[email protected]>
- Ayan
Re: [PATCH v2 5/6] arm: Use secure hypervisor timer in MPU system
In reply to Harry RamseyQuoted 9 lines · Harry Ramsey
From: Penny Zheng <[email protected]>
As MPU systems only have one secure state, we have to use secure EL2 hypervisor timer for Xen in secure EL2.
Signed-off-by Penny Zheng <[email protected]>Signed-off-by Wei Chen <[email protected]>Signed-off-by Luca Fancellu <[email protected]>Signed-off-by Harry Ramsey <[email protected]>
~Michal
PATCH v2 6/6 arm/mpu: Map domain page in AArch64 MPU systems
In reply to Harry RamseyFrom: Penny Zheng <[email protected]>
In MPU systems, we implement map_domain_page()/unmap_domain_page() through mapping the domain page with a MPU region on demand.
---
v2:
- No changes
--- xen/arch/arm/Kconfig | 1 + xen/arch/arm/mpu/Makefile | 1 + xen/arch/arm/mpu/domain-page.c | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 xen/arch/arm/mpu/domain-page.c
xen/arch/arm/Kconfig +1 -0
@@ -91,6 +91,7 @@ config MMU config MPU bool "MPU" if UNSUPPORTED + select ARCH_MAP_DOMAIN_PAGE if ARM_64 select STATIC_MEMORY help Memory Protection Unit (MPU). Select if you plan to run Xen on ARMv8-R
xen/arch/arm/mpu/Makefile +1 -0
@@ -1,5 +1,6 @@ obj-$(CONFIG_ARM_32) += arm32/ obj-$(CONFIG_ARM_64) += arm64/ +obj-$(CONFIG_ARM_64) += domain-page.o obj-y += mm.o obj-y += p2m.o obj-y += setup.init.o
xen/arch/arm/mpu/domain-page.c new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.43.0