Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions arch/wasm/configs/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ CONFIG_EXT2_FS=y
CONFIG_EXT4_FS=y
# CONFIG_DNOTIFY is not set
# CONFIG_INOTIFY_USER is not set
CONFIG_OVERLAY_FS=y
CONFIG_SQUASHFS=y
CONFIG_EROFS_FS=y
# CONFIG_EROFS_FS_XATTR is not set
# CONFIG_EROFS_FS_BACKED_BY_FILE is not set
# CONFIG_EROFS_FS_ZIP is not set
# CONFIG_DEBUG_MISC is not set
# CONFIG_RCU_TRACE is not set
# CONFIG_RUNTIME_TESTING_MENU is not set
2 changes: 1 addition & 1 deletion arch/wasm/include/asm/wasm_imports.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__attribute__((import_module(#ns), import_name(#name))) \
wasm_##ns##_##name

void wasm_import(boot, get_devicetree)(char *buf, size_t size);
size_t wasm_import(boot, get_devicetree)(char *buf, size_t size);
int wasm_import(boot, get_initramfs)(char *buf, size_t size);

void wasm_import(kernel, breakpoint)(void);
Expand Down
36 changes: 34 additions & 2 deletions arch/wasm/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,41 @@ static void do_start_kernel(void *unused)
start_kernel();
}

static void *__init load_devicetree(size_t *size)
{
phys_addr_t bootstrap_size;
void *devicetree;
size_t copied;

*size = wasm_boot_get_devicetree(NULL, 0);
BUG_ON(!*size || *size > INT_MAX);

/*
* memblock has no memory ranges until it reads /memory from the FDT.
* Temporarily describe the materialized linear memory plus enough space
* for this exact blob, then remove that description before the FDT scan
* installs the authoritative range.
*/
bootstrap_size = PFN_PHYS(__builtin_wasm_memory_size(0));
BUG_ON(bootstrap_size > PHYS_ADDR_MAX - PAGE_SIZE);
BUG_ON(*size > PHYS_ADDR_MAX - bootstrap_size - PAGE_SIZE);
bootstrap_size = PAGE_ALIGN(bootstrap_size + *size);
BUG_ON(memblock_add(0, bootstrap_size));

devicetree = memblock_alloc_or_panic(
*size, roundup_pow_of_two(FDT_V17_SIZE));
copied = wasm_boot_get_devicetree(devicetree, *size);
BUG_ON(copied != *size || fdt_check_header(devicetree) ||
fdt_totalsize(devicetree) > *size);
BUG_ON(memblock_remove(0, bootstrap_size));
return devicetree;
}

__attribute__((export_name("boot"))) void __init _start(void)
{
static char devicetree[2048];
static char initramfs[512];
void *devicetree;
size_t devicetree_size;
int node;

memblock_set_bottom_up(true);
Expand All @@ -41,8 +72,9 @@ __attribute__((export_name("boot"))) void __init _start(void)
__initramfs_start = initramfs;
__initramfs_size = wasm_boot_get_initramfs(initramfs, ARRAY_SIZE(initramfs));

wasm_boot_get_devicetree(devicetree, ARRAY_SIZE(devicetree));
devicetree = load_devicetree(&devicetree_size);
BUG_ON(!early_init_dt_scan(devicetree, __pa(devicetree)));
BUG_ON(!memblock_is_region_memory(__pa(devicetree), devicetree_size));
early_init_fdt_scan_reserved_mem();

node = fdt_path_offset(devicetree, "/chosen/sections");
Expand Down
3 changes: 3 additions & 0 deletions tools/wasm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ export async function spawnMachine(
get_devicetree: (buf, size) => {
const address = buf >>> 0;
const capacity = size >>> 0;
if (capacity === 0) return generated_devicetree.byteLength;

assert(
capacity >= generated_devicetree.byteLength,
"Device tree truncated",
Expand All @@ -445,6 +447,7 @@ export async function spawnMachine(
generated_devicetree,
address,
);
return generated_devicetree.byteLength;
},
get_initramfs: (buf, size) => {
const address = buf >>> 0;
Expand Down
2 changes: 1 addition & 1 deletion tools/wasm/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export type MachineTerminationReason =
export interface Imports {
env: { memory: WebAssembly.Memory };
boot: {
get_devicetree(buf: number, size: number): void;
get_devicetree(buf: number, size: number): number;
get_initramfs(buf: number, size: number): number;
};
kernel: {
Expand Down
Loading