diff --git a/exosphere/coldboot_main.c b/exosphere/coldboot_main.c index f9f0acb8e..bb76ce8dc 100644 --- a/exosphere/coldboot_main.c +++ b/exosphere/coldboot_main.c @@ -1,3 +1,4 @@ +#include #include "utils.h" #include "mmu.h" #include "memory_map.h" @@ -6,6 +7,8 @@ extern void (*__fini_array_start[])(void); extern void (*__fini_array_end[])(void); extern void _fini(void); +extern uint8_t __pk2ldr_start__[], __pk2ldr_end__[]; + extern void __jump_to_lower_el(uint64_t arg, uintptr_t ep, unsigned int el); void coldboot_main(void); @@ -18,5 +21,13 @@ static void __libc_fini_array(void) __attribute__((used)) { } void coldboot_main(void) { - /* TODO */ + uintptr_t *mmu_l3_table = (uintptr_t *)tzram_get_segment_address(TZRAM_SEGMENT_ID_L3_TRANSLATION_TABLE); + void *pk2ldr = (void *)tzram_get_segment_address(TZRAM_SEGMENT_ID_PK2LDR); + + /* Clear and unmap pk2ldr (which is reused as exception entry stacks) */ + memset(pk2ldr, 0, __pk2ldr_end__ - __pk2ldr_start__); + mmu_unmap_range(mmu_l3_table, pk2ldr, __pk2ldr_end__ - __pk2ldr_start__); + tlb_invalidate_all_inner_shareable(); + + /* TODO: stuff & jump to lower EL */ } diff --git a/exosphere/mmu.h b/exosphere/mmu.h index babcdfe91..f41da88f3 100644 --- a/exosphere/mmu.h +++ b/exosphere/mmu.h @@ -150,21 +150,21 @@ static inline void mmu_unmap(unsigned int level, uintptr_t *tbl, uintptr_t base_ } static inline void mmu_map_block_range(unsigned int level, uintptr_t *tbl, uintptr_t base_addr, uintptr_t phys_addr, size_t size, uint64_t attrs) { - size = (size >> MMU_Lx_SHIFT(level)) << MMU_Lx_SHIFT(level); + size = ((size + (BITL(MMU_Lx_SHIFT(level)) - 1)) >> MMU_Lx_SHIFT(level)) << MMU_Lx_SHIFT(level); for(size_t offset = 0; offset < size; offset += MMU_Lx_SHIFT(level)) { mmu_map_block(level, tbl, base_addr + offset, phys_addr + offset, attrs); } } static inline void mmu_map_page_range(uintptr_t *tbl, uintptr_t base_addr, uintptr_t phys_addr, size_t size, uint64_t attrs) { - size = (size >> MMU_Lx_SHIFT(3)) << MMU_Lx_SHIFT(3); + size = ((size + (BITL(MMU_Lx_SHIFT(3)) - 1)) >> MMU_Lx_SHIFT(3)) << MMU_Lx_SHIFT(3); for(size_t offset = 0; offset < size; offset += MMU_Lx_SHIFT(3)) { mmu_map_page(tbl, base_addr + offset, phys_addr + offset, attrs); } } static inline void mmu_unmap_range(unsigned int level, uintptr_t *tbl, uintptr_t base_addr, size_t size) { - size = (size >> MMU_Lx_SHIFT(level)) << MMU_Lx_SHIFT(level); + size = ((size + (BITL(MMU_Lx_SHIFT(level)) - 1)) >> MMU_Lx_SHIFT(level)) << MMU_Lx_SHIFT(level); for(size_t offset = 0; offset < size; offset += MMU_Lx_SHIFT(level)) { mmu_unmap(level, tbl, base_addr + offset); }