From a12cb77c820dfed9b05aff6245acb4295cb02b87 Mon Sep 17 00:00:00 2001 From: Chen Baozi Date: Mon, 20 Feb 2023 10:50:15 +0000 Subject: [PATCH] refactor(qemu): make use of setup_page_tables() Use the setup_page_tables() helper function to setup page tables. Signed-off-by: Chen Baozi Change-Id: I0bca4e463ed68abf2ef1c79fc8e5cb2b635fcd1c --- plat/qemu/common/qemu_bl1_setup.c | 39 +++++++++++++++++++++++---- plat/qemu/common/qemu_bl2_setup.c | 43 +++++++++++++++++++++++------- plat/qemu/common/qemu_bl31_setup.c | 34 ++++++++++++++++++++--- plat/qemu/common/qemu_common.c | 43 ++++-------------------------- plat/qemu/common/qemu_private.h | 17 ++---------- 5 files changed, 104 insertions(+), 72 deletions(-) diff --git a/plat/qemu/common/qemu_bl1_setup.c b/plat/qemu/common/qemu_bl1_setup.c index 67f33273f..a3f61732d 100644 --- a/plat/qemu/common/qemu_bl1_setup.c +++ b/plat/qemu/common/qemu_bl1_setup.c @@ -14,6 +14,27 @@ #include "qemu_private.h" +#define MAP_BL1_TOTAL MAP_REGION_FLAT( \ + bl1_tzram_layout.total_base, \ + bl1_tzram_layout.total_size, \ + MT_MEMORY | MT_RW | EL3_PAS) + +#define MAP_BL1_RO MAP_REGION_FLAT( \ + BL_CODE_BASE, \ + BL1_CODE_END - BL_CODE_BASE, \ + MT_CODE | EL3_PAS), \ + MAP_REGION_FLAT( \ + BL1_RO_DATA_BASE, \ + BL1_RO_DATA_END \ + - BL_RO_DATA_BASE, \ + MT_RO_DATA | EL3_PAS) + +#define MAP_BL_COHERENT_RAM MAP_REGION_FLAT( \ + BL_COHERENT_RAM_BASE, \ + BL_COHERENT_RAM_END \ + - BL_COHERENT_RAM_BASE, \ + MT_DEVICE | MT_RW | EL3_PAS) + /* Data structure which holds the extents of the trusted SRAM for BL1*/ static meminfo_t bl1_tzram_layout; @@ -49,11 +70,19 @@ void bl1_early_platform_setup(void) void bl1_plat_arch_setup(void) { - QEMU_CONFIGURE_BL1_MMU(bl1_tzram_layout.total_base, - bl1_tzram_layout.total_size, - BL_CODE_BASE, BL1_CODE_END, - BL1_RO_DATA_BASE, BL1_RO_DATA_END, - BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END); + const mmap_region_t bl_regions[] = { + MAP_BL1_TOTAL, + MAP_BL1_RO, + MAP_BL_COHERENT_RAM, + {0} + }; + + setup_page_tables(bl_regions, plat_qemu_get_mmap()); +#ifdef __aarch64__ + enable_mmu_el3(0); +#else + enable_mmu_svc_mon(0); +#endif } void bl1_platform_setup(void) diff --git a/plat/qemu/common/qemu_bl2_setup.c b/plat/qemu/common/qemu_bl2_setup.c index be5587714..8f6066bb5 100644 --- a/plat/qemu/common/qemu_bl2_setup.c +++ b/plat/qemu/common/qemu_bl2_setup.c @@ -23,6 +23,26 @@ #include "qemu_private.h" +#define MAP_BL2_TOTAL MAP_REGION_FLAT( \ + bl2_tzram_layout.total_base, \ + bl2_tzram_layout.total_size, \ + MT_MEMORY | MT_RW | MT_SECURE) + +#define MAP_BL2_RO MAP_REGION_FLAT( \ + BL_CODE_BASE, \ + BL_CODE_END - BL_CODE_BASE, \ + MT_CODE | MT_SECURE), \ + MAP_REGION_FLAT( \ + BL_RO_DATA_BASE, \ + BL_RO_DATA_END \ + - BL_RO_DATA_BASE, \ + MT_RO_DATA | MT_SECURE) + +#define MAP_BL_COHERENT_RAM MAP_REGION_FLAT( \ + BL_COHERENT_RAM_BASE, \ + BL_COHERENT_RAM_END \ + - BL_COHERENT_RAM_BASE, \ + MT_DEVICE | MT_RW | MT_SECURE) /* Data structure which holds the extents of the trusted SRAM for BL2 */ static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE); @@ -83,19 +103,22 @@ void bl2_platform_setup(void) /* TODO Initialize timer */ } +void bl2_plat_arch_setup(void) +{ + const mmap_region_t bl_regions[] = { + MAP_BL2_TOTAL, + MAP_BL2_RO, + MAP_BL_COHERENT_RAM, + {0} + }; + + setup_page_tables(bl_regions, plat_qemu_get_mmap()); + #ifdef __aarch64__ -#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_el1(__VA_ARGS__) + enable_mmu_el1(0); #else -#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_svc_mon(__VA_ARGS__) + enable_mmu_svc_mon(0); #endif - -void bl2_plat_arch_setup(void) -{ - QEMU_CONFIGURE_BL2_MMU(bl2_tzram_layout.total_base, - bl2_tzram_layout.total_size, - BL_CODE_BASE, BL_CODE_END, - BL_RO_DATA_BASE, BL_RO_DATA_END, - BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END); } /******************************************************************************* diff --git a/plat/qemu/common/qemu_bl31_setup.c b/plat/qemu/common/qemu_bl31_setup.c index 4f60eb163..3195d9158 100644 --- a/plat/qemu/common/qemu_bl31_setup.c +++ b/plat/qemu/common/qemu_bl31_setup.c @@ -12,6 +12,26 @@ #include "qemu_private.h" +#define MAP_BL31_TOTAL MAP_REGION_FLAT( \ + BL31_BASE, \ + BL31_END - BL31_BASE, \ + MT_MEMORY | MT_RW | EL3_PAS) +#define MAP_BL31_RO MAP_REGION_FLAT( \ + BL_CODE_BASE, \ + BL_CODE_END - BL_CODE_BASE, \ + MT_CODE | EL3_PAS), \ + MAP_REGION_FLAT( \ + BL_RO_DATA_BASE, \ + BL_RO_DATA_END \ + - BL_RO_DATA_BASE, \ + MT_RO_DATA | EL3_PAS) + +#define MAP_BL_COHERENT_RAM MAP_REGION_FLAT( \ + BL_COHERENT_RAM_BASE, \ + BL_COHERENT_RAM_END \ + - BL_COHERENT_RAM_BASE, \ + MT_DEVICE | MT_RW | EL3_PAS) + /* * Placeholder variables for copying the arguments that have been passed to * BL3-1 from BL2. @@ -64,10 +84,16 @@ void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, void bl31_plat_arch_setup(void) { - qemu_configure_mmu_el3(BL31_BASE, (BL31_END - BL31_BASE), - BL_CODE_BASE, BL_CODE_END, - BL_RO_DATA_BASE, BL_RO_DATA_END, - BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END); + const mmap_region_t bl_regions[] = { + MAP_BL31_TOTAL, + MAP_BL31_RO, + MAP_BL_COHERENT_RAM, + {0} + }; + + setup_page_tables(bl_regions, plat_qemu_get_mmap()); + + enable_mmu_el3(0); } static void qemu_gpio_init(void) diff --git a/plat/qemu/common/qemu_common.c b/plat/qemu/common/qemu_common.c index 23ac5813e..935ba7a77 100644 --- a/plat/qemu/common/qemu_common.c +++ b/plat/qemu/common/qemu_common.c @@ -122,45 +122,12 @@ static const mmap_region_t plat_qemu_mmap[] = { #endif /******************************************************************************* - * Macro generating the code for the function setting up the pagetables as per - * the platform memory map & initialize the mmu, for the given exception level + * Returns QEMU platform specific memory map regions. ******************************************************************************/ - -#define DEFINE_CONFIGURE_MMU_EL(_el) \ - void qemu_configure_mmu_##_el(unsigned long total_base, \ - unsigned long total_size, \ - unsigned long code_start, \ - unsigned long code_limit, \ - unsigned long ro_start, \ - unsigned long ro_limit, \ - unsigned long coh_start, \ - unsigned long coh_limit) \ - { \ - mmap_add_region(total_base, total_base, \ - total_size, \ - MT_MEMORY | MT_RW | MT_SECURE); \ - mmap_add_region(code_start, code_start, \ - code_limit - code_start, \ - MT_CODE | MT_SECURE); \ - mmap_add_region(ro_start, ro_start, \ - ro_limit - ro_start, \ - MT_RO_DATA | MT_SECURE); \ - mmap_add_region(coh_start, coh_start, \ - coh_limit - coh_start, \ - MT_DEVICE | MT_RW | MT_SECURE); \ - mmap_add(plat_qemu_mmap); \ - init_xlat_tables(); \ - \ - enable_mmu_##_el(0); \ - } - -/* Define EL1 and EL3 variants of the function initialising the MMU */ -#ifdef __aarch64__ -DEFINE_CONFIGURE_MMU_EL(el1) -DEFINE_CONFIGURE_MMU_EL(el3) -#else -DEFINE_CONFIGURE_MMU_EL(svc_mon) -#endif +const mmap_region_t *plat_qemu_get_mmap(void) +{ + return plat_qemu_mmap; +} #if MEASURED_BOOT || TRUSTED_BOARD_BOOT int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size) diff --git a/plat/qemu/common/qemu_private.h b/plat/qemu/common/qemu_private.h index 159c44f7a..199ca018e 100644 --- a/plat/qemu/common/qemu_private.h +++ b/plat/qemu/common/qemu_private.h @@ -9,26 +9,13 @@ #include -void qemu_configure_mmu_svc_mon(unsigned long total_base, - unsigned long total_size, - unsigned long code_start, unsigned long code_limit, - unsigned long ro_start, unsigned long ro_limit, - unsigned long coh_start, unsigned long coh_limit); - -void qemu_configure_mmu_el1(unsigned long total_base, unsigned long total_size, - unsigned long code_start, unsigned long code_limit, - unsigned long ro_start, unsigned long ro_limit, - unsigned long coh_start, unsigned long coh_limit); - -void qemu_configure_mmu_el3(unsigned long total_base, unsigned long total_size, - unsigned long code_start, unsigned long code_limit, - unsigned long ro_start, unsigned long ro_limit, - unsigned long coh_start, unsigned long coh_limit); +#include void plat_qemu_io_setup(void); int qemu_io_register_sp_pkg(const char *name, const char *uuid, uintptr_t load_addr); unsigned int plat_qemu_calc_core_pos(u_register_t mpidr); +const mmap_region_t *plat_qemu_get_mmap(void); void qemu_console_init(void); -- 2.39.5