Skip to content

Commit

Permalink
core: mm: allocate temporary memory map array
Browse files Browse the repository at this point in the history
With CFG_BOOT_MEM enabled, allocate a temporary memory map array using
boot_mem_alloc_tmp() instead of using the global static_mmap_regions[].
core_mmu_save_mem_map() is added and called from
boot_init_primary_late() before the temporary memory is reused.

Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
jenswi-linaro committed Sep 13, 2024
1 parent 78051bf commit 3a9b8b9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/arch/arm/kernel/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ static void init_primary(unsigned long pageable_part, unsigned long nsec_entry)
malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
#endif

core_mmu_save_mem_map();
if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
IMSG("Initializing virtualization support");
core_mmu_init_virtualization();
Expand Down
1 change: 1 addition & 0 deletions core/include/mm/core_mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ extern const unsigned long core_mmu_tee_load_pa;

void core_init_mmu_map(unsigned long seed, struct core_mmu_config *cfg);
void core_init_mmu_regs(struct core_mmu_config *cfg);
void core_mmu_save_mem_map(void);

/* Arch specific function to help optimizing 1 MMU xlat table */
bool core_mmu_prefer_tee_ram_at_top(paddr_t paddr);
Expand Down
71 changes: 68 additions & 3 deletions core/mm/core_mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ unsigned long default_nsec_shm_size __nex_bss;
unsigned long default_nsec_shm_paddr __nex_bss;
#endif

#ifdef CFG_BOOT_MEM
static struct memory_map static_memory_map __nex_bss;
#else
static struct tee_mmap_region static_mmap_regions[CFG_MMAP_REGIONS
#if defined(CFG_CORE_ASLR) || defined(CFG_CORE_PHYS_RELOCATABLE)
+ 1
Expand All @@ -71,6 +74,8 @@ static struct memory_map static_memory_map __nex_data = {
.map = static_mmap_regions,
.alloc_count = ARRAY_SIZE(static_mmap_regions),
};
#endif
void (*memory_map_realloc_func)(struct memory_map *mem_map) __nex_bss;

/* Define the platform's memory layout. */
struct memaccess_area {
Expand Down Expand Up @@ -123,11 +128,43 @@ static void mmu_unlock(uint32_t exceptions)
cpu_spin_unlock_xrestore(&mmu_spinlock, exceptions);
}

static void heap_realloc_memory_map(struct memory_map *mem_map)
{
struct tee_mmap_region *m = NULL;
struct tee_mmap_region *old = mem_map->map;
size_t old_sz = sizeof(*old) * mem_map->alloc_count;
size_t sz = old_sz + sizeof(*m);

assert(nex_malloc_buffer_is_within_alloced(old, old_sz));
m = nex_realloc(old, sz);
if (!m)
panic();
mem_map->map = m;
mem_map->alloc_count++;
}

static void boot_mem_realloc_memory_map(struct memory_map *mem_map)
{
struct tee_mmap_region *m = NULL;
struct tee_mmap_region *old = mem_map->map;
size_t old_sz = sizeof(*old) * mem_map->alloc_count;
size_t sz = old_sz * 2;

m = boot_mem_alloc_tmp(sz, alignof(*m));
memcpy(m, old, old_sz);
mem_map->map = m;
mem_map->alloc_count *= 2;
}

static void grow_mem_map(struct memory_map *mem_map)
{
if (mem_map->count == mem_map->alloc_count) {
EMSG("Out of entries (%zu) in mem_map", mem_map->alloc_count);
panic();
if (!memory_map_realloc_func) {
EMSG("Out of entries (%zu) in mem_map",
mem_map->alloc_count);
panic();
}
memory_map_realloc_func(mem_map);
}
mem_map->count++;
}
Expand Down Expand Up @@ -1581,7 +1618,16 @@ void __weak core_init_mmu_map(unsigned long seed, struct core_mmu_config *cfg)

check_sec_nsec_mem_config();

mem_map = static_memory_map;
if (IS_ENABLED(CFG_BOOT_MEM)) {
mem_map.alloc_count = MEM_AREA_MAXTYPE;
mem_map.map = boot_mem_alloc_tmp(mem_map.alloc_count *
sizeof(*mem_map.map),
alignof(*mem_map.map));
memory_map_realloc_func = boot_mem_realloc_memory_map;
} else {
mem_map = static_memory_map;
}

static_memory_map = (struct memory_map){
.map = &tmp_mmap_region,
.alloc_count = 1,
Expand All @@ -1608,6 +1654,25 @@ void __weak core_init_mmu_map(unsigned long seed, struct core_mmu_config *cfg)
core_init_mmu_regs(cfg);
cfg->map_offset = offs;
static_memory_map = mem_map;
boot_mem_add_reloc(&static_memory_map.map);
}

void core_mmu_save_mem_map(void)
{
if (IS_ENABLED(CFG_BOOT_MEM)) {
size_t alloc_count = static_memory_map.count + 5;
size_t elem_sz = sizeof(*static_memory_map.map);
void *p = NULL;

p = nex_calloc(alloc_count, elem_sz);
if (!p)
panic();
memcpy(p, static_memory_map.map,
static_memory_map.count * elem_sz);
static_memory_map.map = p;
static_memory_map.alloc_count = alloc_count;
memory_map_realloc_func = heap_realloc_memory_map;
}
}

bool core_mmu_mattr_is_ok(uint32_t mattr)
Expand Down

0 comments on commit 3a9b8b9

Please sign in to comment.