Skip to content

Commit

Permalink
elfloader,RISC-V: Relax kernel alignment checks
Browse files Browse the repository at this point in the history
It's not necessary to require the kernel and ELFloader images are loaded
at the start of page boundaries.

Signed-off-by: Kent McLeod <[email protected]>
  • Loading branch information
kent-mcleod committed Feb 1, 2022
1 parent f3b5452 commit 3a3c02f
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions elfloader-tool/src/arch-riscv/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
#define PT_LEVEL_2 2

#define PT_LEVEL_1_BITS 30
#if __riscv_xlen == 32
#define PT_LEVEL_2_BITS 22
#else
#define PT_LEVEL_2_BITS 21
#endif

#define PTE_TYPE_TABLE 0x00
#define PTE_TYPE_SRWX 0xCE
Expand All @@ -46,8 +50,6 @@

#define GET_PT_INDEX(addr, n) (((addr) >> (((PT_INDEX_BITS) * ((CONFIG_PT_LEVELS) - (n))) + RISCV_PGSHIFT)) % PTES_PER_PT)

#define VIRT_PHYS_ALIGNED(virt, phys, level_bits) (IS_ALIGNED((virt), (level_bits)) && IS_ALIGNED((phys), (level_bits)))

struct image_info kernel_info;
struct image_info user_info;

Expand Down Expand Up @@ -89,11 +91,6 @@ static int map_kernel_window(struct image_info *kernel_info)

/* Map the elfloader into the new address space */

if (!IS_ALIGNED((uintptr_t)_text, PT_LEVEL_2_BITS)) {
printf("ERROR: ELF Loader not properly aligned\n");
return -1;
}

index = GET_PT_INDEX((uintptr_t)_text, PT_LEVEL_1);

#if __riscv_xlen == 32
Expand All @@ -105,18 +102,12 @@ static int map_kernel_window(struct image_info *kernel_info)
#endif

for (unsigned int page = 0; index < PTES_PER_PT; index++, page++) {
lpt[index] = PTE_CREATE_LEAF((uintptr_t)_text +
lpt[index] = PTE_CREATE_LEAF(ROUND_DOWN((uintptr_t)_text, PT_LEVEL_2_BITS) +
(page << PT_LEVEL_2_BITS));
}

/* Map the kernel into the new address space */

if (!VIRT_PHYS_ALIGNED(kernel_info->virt_region_start,
kernel_info->phys_region_start, PT_LEVEL_2_BITS)) {
printf("ERROR: Kernel not properly aligned\n");
return -1;
}

index = GET_PT_INDEX(kernel_info->virt_region_start, PT_LEVEL_1);

#if __riscv_xlen == 64
Expand All @@ -126,7 +117,7 @@ static int map_kernel_window(struct image_info *kernel_info)
#endif

for (unsigned int page = 0; index < PTES_PER_PT; index++, page++) {
lpt[index] = PTE_CREATE_LEAF(kernel_info->phys_region_start +
lpt[index] = PTE_CREATE_LEAF(ROUND_DOWN(kernel_info->phys_region_start, PT_LEVEL_2_BITS) +
(page << PT_LEVEL_2_BITS));
}

Expand Down

0 comments on commit 3a3c02f

Please sign in to comment.