Fourth MPU series
v3 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 | 10 ++ 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 | 203 ++++++++++++++++++----- xen/arch/arm/mpu/setup.c | 54 +++++- xen/arch/arm/mpu/vmap.c | 14 +- xen/arch/arm/setup.c | 15 +- 11 files changed, 324 insertions(+), 58 deletions(-) create mode 100644 xen/arch/arm/mpu/domain-page.c
--
2.43.0
PATCH v3 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.
---
v3:
- No changes
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
PATCH v3 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.
---
v3:
- Add additional comments for clarity regarding MPUMAP_REGION checks
- Ensure context_sync_mpu occurs after destroy_entire_xen_mapping
- Fix deadlock if destroy_entire_xen_mapping is called with an address that does not belong to a region
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 | 82 ++++++++++++++++++++++++++----- xen/arch/arm/mpu/vmap.c | 14 ++++-- 3 files changed, 91 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 +70 -12
100 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 v3 2/6] arm/mpu: Implement vmap functions for MPU
Quoted 81 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]>---
v3:
- Add additional comments for clarity regardingMPUMAP_REGIONchecks
- Ensurecontext_sync_mpuoccurs afterdestroy_entire_xen_mapping
- Fix deadlock ifdestroy_entire_xen_mappingis called with an address that does not belong to a region
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 | 82 ++++++++++++++++++++++++++----- xen/arch/arm/mpu/vmap.c | 14 ++++-- 3 files changed, 91 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 +23
-0@@ -290,6 +290,43 @@ 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 ); +ASSERT (MPUMAP_REGION_OVERLAP != region_found_type); + +if (MPUMAP_REGION_NOTFOUND == region_found_type ) + { +printk (XENLOG_ERR "Cannot remove entry that does not exist\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount ) + { + xen_mpumap[idx].refcount -=1 ;
xen/arch/arm/mpu/mm.c static void disable_mpu_region_from_index(uint8_t index) · line 325
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+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" ); +return -EINVAL ; + }
NIT: Try to limit the number of if/else blocks to make the code read better. Here you could do the following to remove one else:
if ( MPUMAP_REGION_FOUND != region_found_type )
{
printk(XENLOG_ERR "Cannot remove a partial region\n");
return -EINVAL;
}
disable_mpu_region_from_index(idx);
return 0;Quoted 38 lines
xen/arch/arm/mpu/mm.c +12
-12+ +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 +394,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 +444,38 @@ 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 )); +
xen/arch/arm/mpu/mm.c int destroy_xen_mappings(unsigned long s, unsigned long e) · line 463
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+spin_lock (&xen_mpumap_lock); + + rc =mpumap_contains_region (xen_mpumap, max_mpu_regions, s, s +PAGE_SIZE , + &idx); + +/* +* Since only entire regions can be freed using `xen_mpumap_free_entry` we +* must first check the region exists. +*/ +if (MPUMAP_REGION_NOTFOUND == rc ) {
Bracket should be placed on its own line.
Quoted 41 lines
xen/arch/arm/mpu/mm.c +15
-0+printk (XENLOG_ERR "Cannot remove entry that does not exist" ); + rc = -EINVAL ; + goto out; + } + +/* As we are unmapping entire region use MPUMAP_REGION_FOUND instead */ + rc =xen_mpumap_free_entry (idx,MPUMAP_REGION_FOUND ); +if ( !rc ) +context_sync_mpu (); + out: +spin_unlock (&xen_mpumap_lock); + +return rc; +} +int map_pages_to_xen (unsigned long virt,mfn_t mfn,unsigned long nr_mfns,unsigned int flags) {xen/arch/arm/mpu/vmap.c +7
-2@@ -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);
xen/arch/arm/mpu/vmap.c line 25
Quoted 10 lines
xen/arch/arm/mpu/vmap.c +4
-1}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" ); }
Looking at this series as a whole, at the end we will end up with vmap_contig/vunmap to contain the same implementation as map_domain_page and alike from the last patch. Shouldn't we define ones using the others?
~Michal
Re: [PATCH v3 2/6] arm/mpu: Implement vmap functions for MPU
Hi Michal,
Quoted 36 lines
Quoted 24 lines
xen/arch/arm/mpu/mm.c +23
-0+/* +* 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 ); +ASSERT (MPUMAP_REGION_OVERLAP != region_found_type); + +if (MPUMAP_REGION_NOTFOUND == region_found_type ) + { +printk (XENLOG_ERR "Cannot remove entry that does not exist\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount ) + { + xen_mpumap[idx].refcount -=1 ;xen/arch/arm/mpu/mm.c static void disable_mpu_region_from_index(uint8_t index) · line 325
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+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" ); +return -EINVAL ; + }NIT: Try to limit the number of if/else blocks to make the code read better. Here you could do the following to remove one else:
Quoted 9 lines
if ( MPUMAP_REGION_FOUND != region_found_type )
{
printk(XENLOG_ERR "Cannot remove a partial region\n");
return -EINVAL;
}
disable_mpu_region_from_index(idx);
return 0;Makes sense, while we are there, shall we have also a comment above that check, something like this:
/*
* If we reached this point, the region is due to be destroyed, as a safety
* check we need to ensure the API is called with the exact region, to prevent
* destroying a region using a partial memory range.
*/
What do you think? Otherwise someone in the future might think it’s ok to move this check together with the other on top.
Quoted 10 lines
Quoted 10 lines
void
vunmap(const void *va)
{xen/arch/arm/mpu/vmap.c +4
-1-BUG_ON ("unimplemented" ); +paddr_t base =virt_to_maddr (va); + +if (destroy_xen_mapping_containing (base) ) +panic ("Failed to vunmap region\n" );}
Quoted 3 lines
Looking at this series as a whole, at the end we will end up with vmap_contig/vunmap to contain the same implementation as
map_domain_pageand alike from the last patch. Shouldn't we define ones using the others?
We can do it, the only thing is that if vunmap fails, we will get a less specific error message, if you are ok with that we will do the change.
Cheers,
Luca
Re: [PATCH v3 2/6] arm/mpu: Implement vmap functions for MPU
Quoted 50 lines · Luca Fancellu
Hi Michal,
Quoted 36 lines
Quoted 24 lines
xen/arch/arm/mpu/mm.c +23
-0+/* +* 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 ); +ASSERT (MPUMAP_REGION_OVERLAP != region_found_type); + +if (MPUMAP_REGION_NOTFOUND == region_found_type ) + { +printk (XENLOG_ERR "Cannot remove entry that does not exist\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount ) + { + xen_mpumap[idx].refcount -=1 ;xen/arch/arm/mpu/mm.c static void disable_mpu_region_from_index(uint8_t index) · line 325
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+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" ); +return -EINVAL ; + }NIT: Try to limit the number of if/else blocks to make the code read better. Here you could do the following to remove one else:
Quoted 9 lines
if ( MPUMAP_REGION_FOUND != region_found_type ) { printk(XENLOG_ERR "Cannot remove a partial region\n"); return -EINVAL; } disable_mpu_region_from_index(idx); return 0;Makes sense, while we are there, shall we have also a comment above that check, something like this:
Quoted 8 lines
/*
* If we reached this point, the region is due to be destroyed, as a safety
* check we need to ensure the API is called with the exact region, to prevent
* destroying a region using a partial memory range.
*/What do you think? Otherwise someone in the future might think it’s ok to move this check together with the other on top.
I'm not sure. I can read this from code but if you think it will be beneficial, I have no objections.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
void
vunmap(const void *va)
{xen/arch/arm/mpu/vmap.c +4
-1-BUG_ON ("unimplemented" ); +paddr_t base =virt_to_maddr (va); + +if (destroy_xen_mapping_containing (base) ) +panic ("Failed to vunmap region\n" );}
Quoted 6 lines
Quoted 3 lines
Looking at this series as a whole, at the end we will end up with vmap_contig/vunmap to contain the same implementation as
map_domain_pageand alike from the last patch. Shouldn't we define ones using the others?We can do it, the only thing is that if vunmap fails, we will get a less specific error message, if you are ok with that we will do the change.
In the MMU case you won't get any info, so I think it's ok.
~Michal
PATCH v3 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 attributes can now be updated by xen_mpumap_update_entry.
---
v3:
- Refactor MPU_ATTR_* defines
v2:
- Refactor is_mm_attr_match to return logical values regarding the attribute mismatch.
- Improve code documentation.
--- xen/arch/arm/include/asm/setup.h | 2 + xen/arch/arm/mpu/mm.c | 119 +++++++++++++++++++++++-------- 2 files changed, 93 insertions(+), 28 deletions(-)
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 +91 -28
157 lines not on this page · raw message
--
2.43.0
Re: [PATCH v3 3/6] arm/mpu: Implement free_init_memory for MPU systems
Quoted 99 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 attributes 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]>---
v3:
- Refactor MPU_ATTR_* defines
v2:
- Refactoris_mm_attr_matchto return logical values regarding the attribute mismatch.
- Improve code documentation.--- xen/arch/arm/include/asm/setup.h | 2 + xen/arch/arm/mpu/mm.c | 119 +++++++++++++++++++++++-------- 2 files changed, 93 insertions(+), 28 deletions(-)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 +9
-23@@ -15,6 +15,9 @@#include <asm/setup.h> #include <asm/sysregs.h> +#define MPU_ATTR_XN_RO_MISMATCH -1 +#define MPU_ATTR_AI_MISMATCH -2 +struct page_info *frame_table;/* Maximum number of supported MPU memory regions by the EL2 MPU. */ @@ -171,33 +174,16 @@ 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; - } - -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; - } +if ( (region->prbar.reg.xn !=PAGE_XN_MASK (attributes)) || + (region->prbar.reg.ro !=PAGE_RO_MASK (attributes)) ) +return MPU_ATTR_XN_RO_MISMATCH ;if ( 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 */ @@ -358,12 +344,44 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,*/ if ( flags_has_page_present && (rc >=MPUMAP_REGION_FOUND ) )
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 353
Quoted 10 lines
xen/arch/arm/mpu/mm.c +6
-2{ -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" ); +printk (XENLOG_ERR +"Modifying memory attribute is not supported\n" );
Because this message is the same as the one a bit lower I would suggest to do s/memory/AI/ here...
Quoted 10 lines
xen/arch/arm/mpu/mm.c +7
-0return -EINVAL ; } +/* +* Attributes RO and XN can be changed only by the full region. +* Attributes that match can continue and just increment refcount. +*/ +if (MPU_ATTR_XN_RO_MISMATCH == attr_match ) + { +if ( rc ==MPUMAP_REGION_INCLUSIVE )
xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 373
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+ { +printk (XENLOG_ERR +"Cannot modify partial region attributes\n" ); +return -EINVAL ; + } + +if ( xen_mpumap[idx].refcount !=0 ) + { +printk (XENLOG_ERR +"Cannot modify memory attributes for a region mapped multiple times\n" );
and s/memory/RO,XN/ here.
Quoted 55 lines
xen/arch/arm/mpu/mm.c +38
-3+return -EINVAL ; + } + +/* Set new attributes */ + xen_mpumap[idx].prbar.reg.ro =PAGE_RO_MASK (flags); + xen_mpumap[idx].prbar.reg.xn =PAGE_XN_MASK (flags); + +write_protection_region (&xen_mpumap[idx], idx); +return 0 ; + } +/* Check for overflow of refcount before incrementing. */ if ( xen_mpumap[idx].refcount ==0xFF ) {@@ -514,8 +532,7 @@ void __init setup_mm_helper(void)int modify_xen_mappings (unsigned long s,unsigned long e,unsigned int nf) { -BUG_ON ("unimplemented" ); -return -EINVAL ; +return xen_mpumap_update (s, e, nf); }void dump_hyp_walk (vaddr_t addr)@@ -526,7 +543,53 @@ void dump_hyp_walk(vaddr_t addr)/* Release all __init and __initdata ranges to be reused */ void free_init_memory (void ) { -BUG_ON ("unimplemented" ); +unsigned long inittext_end = (unsigned long )__init_data_begin; +unsigned long len = __init_end - __init_begin; +uint8_t idx; +int rc; + +/* Modify inittext region to be read/write instead of read/execute. */ + rc =modify_xen_mappings ((unsigned long )__init_begin, inittext_end, +PAGE_HYPERVISOR_RW ); +if ( rc ) +panic ("Unable to map RW the init text section (rc = %d)\n" , rc); + +/* +* From now on, init will not be used for execution anymore, +* so nuke the instruction cache to remove entries related to init. +*/ +invalidate_icache_local (); + +/* +* The initdata region already has read/write attributes so it can just be +* zeroed out. +*/ +memset (__init_begin,0 , len); + + rc =destroy_xen_mappings ((unsigned long )__init_begin, inittext_end); +if ( rc ) +panic ("Unable to remove init text section (rc = %d)\n" , rc);
xen/arch/arm/mpu/mm.c void dump_hyp_walk(vaddr_t addr) · line 581
Quoted 10 lines
xen/arch/arm/mpu/mm.c +10
-0+ +/* +* The initdata and bss sections are mapped using a single MPU region, so +* modify the start of this region to remove the initdata section. +*/ +spin_lock (&xen_mpumap_lock); + + rc = mpumap_contains_region(xen_mpumap, max_mpu_regions, + (unsigned long)__init_data_begin, + (unsigned long)__bss_end,
NIT: I'm thinking if it would not make sense to add some sanity checks in the future to make sure the layout is as expected. For example, what if a new section appeared between __init_end and __bss_start in the future?
For this patch with the printk adjustments:
~Michal
PATCH v3 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.
---
v3:
- Add Michal R-by
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
@@ -35,7 +35,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
PATCH v3 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.
---
v3:
- Add Ayan R-by
- Add Michal A-by
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
PATCH v3 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.
---
v3:
- No changes
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