Skip to content

Commit

Permalink
elfloader/risc-v: Improve code readability
Browse files Browse the repository at this point in the history
Reorder code blocks and improve comments.

Signed-off-by: Axel Heider <[email protected]>
  • Loading branch information
axel-h committed Feb 7, 2022
1 parent d89b3a0 commit b253549
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions elfloader-tool/src/arch-riscv/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ char elfloader_stack[CONFIG_MAX_NUM_NODES * BIT(CONFIG_KERNEL_STACK_BITS)] __att
void const *dtb = NULL;
size_t dtb_size = 0;

static inline void sfence_vma(void)
{
asm volatile("sfence.vma" ::: "memory");
}

static inline void ifence(void)
{
asm volatile("fence.i" ::: "memory");
}

#if CONFIG_PT_LEVELS == 2
uint64_t vm_mode = 0x1llu << 31;
#elif CONFIG_PT_LEVELS == 3
uint64_t vm_mode = 0x8llu << 60;
#elif CONFIG_PT_LEVELS == 4
uint64_t vm_mode = 0x9llu << 60;
#else
#error "Wrong PT level"
#endif

static inline void enable_virtual_memory(void)
{
sfence_vma();
asm volatile(
"csrw satp, %0\n"
:
: "r"(vm_mode | (uintptr_t)l1pt >> RISCV_PGSHIFT)
:
);
ifence();
}

/*
* overwrite the default implementation for abort()
*/
Expand Down Expand Up @@ -134,16 +166,6 @@ static int map_kernel_window(struct image_info *kernel_info)
return 0;
}

#if CONFIG_PT_LEVELS == 2
uint64_t vm_mode = 0x1llu << 31;
#elif CONFIG_PT_LEVELS == 3
uint64_t vm_mode = 0x8llu << 60;
#elif CONFIG_PT_LEVELS == 4
uint64_t vm_mode = 0x9llu << 60;
#else
#error "Wrong PT level"
#endif

int hsm_exists = 0; /* assembly startup code will initialise this */

#if CONFIG_MAX_NUM_NODES > 1
Expand Down Expand Up @@ -172,28 +194,6 @@ static void set_and_wait_for_ready(word_t hart_id, word_t core_id)

#endif /* CONFIG_MAX_NUM_NODES > 1 */

static inline void sfence_vma(void)
{
asm volatile("sfence.vma" ::: "memory");
}

static inline void ifence(void)
{
asm volatile("fence.i" ::: "memory");
}

static inline void enable_virtual_memory(void)
{
sfence_vma();
asm volatile(
"csrw satp, %0\n"
:
: "r"(vm_mode | (uintptr_t)l1pt >> RISCV_PGSHIFT)
:
);
ifence();
}

static int run_elfloader(UNUSED word_t hart_id, void *bootloader_dtb)
{
int ret;
Expand All @@ -220,6 +220,7 @@ static int run_elfloader(UNUSED word_t hart_id, void *bootloader_dtb)
}

#if CONFIG_MAX_NUM_NODES > 1

while (__atomic_exchange_n(&mutex, 1, __ATOMIC_ACQUIRE) != 0);
printf("Main entry hart_id:%"PRIu_word"\n", hart_id);
__atomic_store_n(&mutex, 0, __ATOMIC_RELEASE);
Expand All @@ -237,6 +238,7 @@ static int run_elfloader(UNUSED word_t hart_id, void *bootloader_dtb)
}

set_and_wait_for_ready(hart_id, 0);

#endif /* CONFIG_MAX_NUM_NODES > 1 */

printf("Enabling MMU and paging\n");
Expand Down

0 comments on commit b253549

Please sign in to comment.