Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch-axel-24] init_freemem() with phys addresses #100

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 0 additions & 6 deletions include/arch/arm/arch/64/mode/kernel/vspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ hw_asid_t getHWASID(asid_t asid);

asid_map_t findMapForASID(asid_t asid);

#ifdef __clang__
static const region_t BOOT_RODATA mode_reserved_region[] = {};
#else
static const region_t BOOT_RODATA *mode_reserved_region = NULL;
#endif

#define PAR_EL1_MASK 0x0000fffffffff000ul
#define GET_PAR_ADDR(x) ((x) & PAR_EL1_MASK)

Expand Down
55 changes: 35 additions & 20 deletions include/arch/arm/arch/bootinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,45 @@
#pragma once

#include <config.h>

#if defined(CONFIG_ARCH_AARCH32)

#include <plat/machine/devices_gen.h>
#include <kernel/vspace.h>

/* The max number of free memory regions is:
* +1 for each available physical memory region (elements in avail_p_regs)
* +1 for each MODE_RESERVED region, there might be none
* +1 to allow the kernel to release its own boot data region
* +1 for a possible gap between ELF images and rootserver objects
*/
#define MAX_NUM_FREEMEM_REG (ARRAY_SIZE(avail_p_regs) + MODE_RESERVED + 1 + 1)
#define MAX_NUM_FREEMEM_REG \
( \
ARRAY_SIZE(avail_p_regs) /* from the auto-generated code */ \
+ MODE_RESERVED \
+ 1 /* allow kernel to release its own boot data region */ \
+ 1 /* possible gap between ELF images and rootserver objects, see arm/arch_init_freemem */ \
)

/* The regions reserved by the boot code are:
* +1 for kernel
* +1 for device tree binary
* +1 for user image.
* +1 for each the MODE_RESERVED region, there might be none
*/
#define NUM_RESERVED_REGIONS (3 + MODE_RESERVED)
#define NUM_RESERVED_REGIONS \
( \
NUM_KERNEL_DEVICE_FRAMES \
+ MODE_RESERVED \
+ 1 /* kernel image */ \
+ 1 /* usage image */ \
+ 1 /* DTB from bootloader */ \
)

#elif defined(CONFIG_ARCH_AARCH64)

/* The maximum number of reserved regions is:
* +1 for each free memory region (MAX_NUM_FREEMEM_REG)
* +1 for each kernel frame (NUM_KERNEL_DEVICE_FRAMES, there might be none)
* +1 for each region reserved by the boot code (NUM_RESERVED_REGIONS)
/* The value for the max number of free memory region is basically an arbitrary
* choice. We could make the macro calculate the exact number, but just picking
* a value will also do for now. Increase this value if the boot fails.
*/
#define MAX_NUM_RESV_REG (MAX_NUM_FREEMEM_REG + NUM_KERNEL_DEVICE_FRAMES + \
NUM_RESERVED_REGIONS)
#define MAX_NUM_FREEMEM_REG 16

#define NUM_RESERVED_REGIONS \
( \
NUM_KERNEL_DEVICE_FRAMES \
+ 1 /* kernel image */ \
+ 1 /* usage image */ \
+ 1 /* DTB from bootloader */ \
)

#else
#error "unknown ARM architecture"
#endif
26 changes: 9 additions & 17 deletions include/arch/riscv/arch/bootinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@
#pragma once

#include <config.h>
#include <plat/machine/devices_gen.h>

/* The value for the max number of free memory region is basically an arbitrary
* choice. We could calculate the exact number, but just picking 16 will also
* do for now. Increase this value if the boot fails.
* choice. We could make the macro calculate the exact number, but just picking
* a value will also do for now. Increase this value if the boot fails.
*/
#define MAX_NUM_FREEMEM_REG 16

/* The regions reserved by the boot code are:
* +1 for kernel
* +1 for device tree binary
* +1 for user image.
*/
#define NUM_RESERVED_REGIONS 3

/* The maximum number of reserved regions is:
* +1 for each free memory region (MAX_NUM_FREEMEM_REG)
* +1 for each kernel frame (NUM_KERNEL_DEVICE_FRAMES, there might be none)
* +1 for each region reserved by the boot code (NUM_RESERVED_REGIONS)
*/
#define MAX_NUM_RESV_REG (MAX_NUM_FREEMEM_REG + NUM_KERNEL_DEVICE_FRAMES + \
NUM_RESERVED_REGIONS)
#define NUM_RESERVED_REGIONS \
( \
NUM_KERNEL_DEVICE_FRAMES \
+ 1 /* kernel image */ \
+ 1 /* usage image */ \
+ 1 /* DTB from bootloader */ \
)
22 changes: 12 additions & 10 deletions include/arch/x86/arch/bootinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

#pragma once

/* The value for the max number of free memory region is basically an arbitrary
* choice. We could make the macro calculate the exact number, but just picking
* a value will also do for now. Increase this value if the boot fails.
*/
#define MAX_NUM_FREEMEM_REG 16

/*
* The maximum number of reserved regions we have is:
* - 1 for each physical memory region (MAX_NUM_FREEMEM_REG)
* - 1 for each kernel device:
* - ioapics (CONFIG_MAX_NUM_IOAPIC)
* - iommus (MAX_NUM_DRHU)
* - apic (1)
* - the reserved MSI region (1)
*/
#define MAX_NUM_RESV_REG (MAX_NUM_FREEMEM_REG + CONFIG_MAX_NUM_IOAPIC + MAX_NUM_DRHU + 2)
#define NUM_RESERVED_REGIONS \
( \
CONFIG_MAX_NUM_IOAPIC \
+ MAX_NUM_DRHU \
+ 1 /* APIC */ \
+ 1 /* MSI region */ \
+ 1 /* user image */ \
)
11 changes: 7 additions & 4 deletions include/kernel/boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ typedef cte_t *slot_ptr_t;
#define SLOT_PTR(pptr, pos) (((slot_ptr_t)(pptr)) + (pos))
#define pptr_of_cap(cap) ((pptr_t)cap_get_capPtr(cap))

#define MAX_NUM_RESV_REG \
( \
MAX_NUM_FREEMEM_REG \
+ NUM_KERNEL_DEVICE_FRAMES \
+ NUM_RESERVED_REGIONS \
)

/* (node-local) state accessed only during bootstrapping */

typedef struct ndks_boot {
p_region_t reserved[MAX_NUM_RESV_REG];
word_t resv_count;
region_t freemem[MAX_NUM_FREEMEM_REG];
seL4_BootInfo *bi_frame;
seL4_SlotPos slot_pos_cur;
} ndks_boot_t;
Expand All @@ -42,7 +46,6 @@ static inline bool_t is_reg_empty(region_t reg)
p_region_t get_p_reg_kernel_img_boot(void);
p_region_t get_p_reg_kernel_img(void);
bool_t init_freemem(word_t n_available, const p_region_t *available,
word_t n_reserved, const region_t *reserved,
v_region_t it_v_reg, word_t extra_bi_size_bits);
bool_t reserve_region(p_region_t reg);
void write_slot(slot_ptr_t slot_ptr, cap_t cap);
Expand Down
126 changes: 41 additions & 85 deletions src/arch/arm/kernel/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,84 +37,6 @@
BOOT_BSS static volatile int node_boot_lock;
#endif /* ENABLE_SMP_SUPPORT */

BOOT_BSS static region_t reserved[NUM_RESERVED_REGIONS];

BOOT_CODE static bool_t arch_init_freemem(p_region_t ui_p_reg,
p_region_t dtb_p_reg,
v_region_t it_v_reg,
word_t extra_bi_size_bits)
{
/* reserve the kernel image region */
reserved[0] = paddr_to_pptr_reg(get_p_reg_kernel_img());

int index = 1;

/* add the dtb region, if it is not empty */
if (dtb_p_reg.start) {
if (index >= ARRAY_SIZE(reserved)) {
printf("ERROR: no slot to add DTB to reserved regions\n");
return false;
}
reserved[index].start = (pptr_t) paddr_to_pptr(dtb_p_reg.start);
reserved[index].end = (pptr_t) paddr_to_pptr(dtb_p_reg.end);
index++;
}

/* Reserve the user image region and the mode-reserved regions. For now,
* only one mode-reserved region is supported, because this is all that is
* needed.
*/
if (MODE_RESERVED > 1) {
printf("ERROR: MODE_RESERVED > 1 unsupported!\n");
return false;
}
if (ui_p_reg.start < PADDR_TOP) {
region_t ui_reg = paddr_to_pptr_reg(ui_p_reg);
if (MODE_RESERVED == 1) {
if (index + 1 >= ARRAY_SIZE(reserved)) {
printf("ERROR: no slot to add the user image and the "
"mode-reserved region to the reserved regions\n");
return false;
}
if (ui_reg.end > mode_reserved_region[0].start) {
reserved[index] = mode_reserved_region[0];
index++;
reserved[index] = ui_reg;
} else {
reserved[index] = ui_reg;
index++;
reserved[index] = mode_reserved_region[0];
}
index++;
} else {
if (index >= ARRAY_SIZE(reserved)) {
printf("ERROR: no slot to add the user image to the reserved"
"regions\n");
return false;
}
reserved[index] = ui_reg;
index++;
}
} else {
if (MODE_RESERVED == 1) {
if (index >= ARRAY_SIZE(reserved)) {
printf("ERROR: no slot to add the mode-reserved region\n");
return false;
}
reserved[index] = mode_reserved_region[0];
index++;
}

/* Reserve the ui_p_reg region still so it doesn't get turned into device UT. */
reserve_region(ui_p_reg);
}

/* avail_p_regs comes from the auto-generated code */
return init_freemem(ARRAY_SIZE(avail_p_regs), avail_p_regs,
index, reserved,
it_v_reg, extra_bi_size_bits);
}


BOOT_CODE static void init_irqs(cap_t root_cnode_cap)
{
Expand Down Expand Up @@ -339,10 +261,6 @@ static BOOT_CODE bool_t try_init_kernel(
cap_t it_ap_cap;
cap_t it_pd_cap;
cap_t ipcbuf_cap;
p_region_t ui_p_reg = (p_region_t) {
ui_p_reg_start, ui_p_reg_end
};
region_t ui_reg = paddr_to_pptr_reg(ui_p_reg);
word_t extra_bi_size = 0;
pptr_t extra_bi_offset = 0;
vptr_t extra_bi_frame_vptr;
Expand Down Expand Up @@ -376,6 +294,34 @@ static BOOT_CODE bool_t try_init_kernel(
/* initialise the platform */
init_plat();

/* Reserve the kernel image region. */
if (!reserve_region(get_p_reg_kernel_img())) {
printf("ERROR: can't add reserved region for kernel image\n");
return false;
}

/* Reserve the user image region. */
p_region_t ui_p_reg = {
.start = ui_p_reg_start,
.end = ui_p_reg_end
};
if (!reserve_region(ui_p_reg)) {
printf("ERROR: can't add reserved region for user image\n");
return false;
}

#ifdef CONFIG_ARCH_AARCH32

/* Reserve the HW ASID region*/
assert(1 == MODE_RESERVED);
p_region_t hw_asid_p_reg = pptr_to_paddr_reg(mode_reserved_region[0]);
if (!reserve_region(hw_asid_p_reg)) {
printf("ERROR: can't add reserved region for HW ASIDs\n");
return false;
}

#endif

/* If a DTB was provided, pass the data on as extra bootinfo */
p_region_t dtb_p_reg = P_REG_EMPTY;
if (dtb_size > 0) {
Expand Down Expand Up @@ -405,6 +351,12 @@ static BOOT_CODE bool_t try_init_kernel(
.start = dtb_phys_addr,
.end = dtb_phys_end
};

/* Reserve the DTB region */
if (!reserve_region(dtb_p_reg)) {
printf("ERROR: can't add reserved region for DTB\n");
return false;
}
}

/* The region of the initial thread is the user image + ipcbuf and boot info */
Expand All @@ -424,8 +376,12 @@ static BOOT_CODE bool_t try_init_kernel(
return false;
}

if (!arch_init_freemem(ui_p_reg, dtb_p_reg, it_v_reg, extra_bi_size_bits)) {
printf("ERROR: free memory management initialization failed\n");
/* make the free memory available to alloc_region(), avail_p_regs comes from
* the auto-generated code.
*/
if (!init_freemem(ARRAY_SIZE(avail_p_regs), avail_p_regs, it_v_reg,
extra_bi_size_bits)) {
printf("ERROR: free memory initn faiuled\n");
return false;
}

Expand Down Expand Up @@ -535,7 +491,7 @@ static BOOT_CODE bool_t try_init_kernel(
create_frames_of_region(
root_cnode_cap,
it_pd_cap,
ui_reg,
paddr_to_pptr_reg(ui_p_reg),
true,
pv_offset
);
Expand Down
Loading
Loading