Skip to content

Commit

Permalink
more boot, more better
Browse files Browse the repository at this point in the history
  • Loading branch information
tombl committed Feb 10, 2024
1 parent 41a2025 commit 3436fa5
Show file tree
Hide file tree
Showing 36 changed files with 769 additions and 410 deletions.
30 changes: 7 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@ jobs:
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- run: nix build . --print-build-logs
- uses: actions/upload-artifact@v4
- uses: cloudflare/pages-action@v1
with:
name: result
path: result/

- run: rm -f tools/wasm/vmlinux.wasm && cp result/vmlinux.wasm tools/wasm/
- uses: actions/configure-pages@v4
- uses: actions/upload-pages-artifact@v3
with:
path: tools/wasm/

deploy:
runs-on: ubuntu-22.04
needs: build
if: github.ref == 'refs/heads/wasm'
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v4
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: 7d87194d9d4af2959fb81de343dd63d9
projectName: linux
directory: result/
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
wranglerVersion: "3"
4 changes: 1 addition & 3 deletions arch/wasm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ config WASM
select BUG
select FLATMEM
select GENERIC_ATOMIC64
select GENERIC_BUG
select GENERIC_CSUM
select GENERIC_HWEIGHT
select HAS_CURRENT_STACK_POINTER
Expand Down Expand Up @@ -54,8 +53,7 @@ config PAGE_SIZE_64KB
bool

config GENERIC_BUG
def_bool y
depends on BUG
bool

config GENERIC_HWEIGHT
def_bool y
Expand Down
5 changes: 4 additions & 1 deletion arch/wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ LD = wasm-ld
WASM_FEATURES = -matomics -mbulk-memory -mexception-handling -mextended-const -mmultivalue -mmutable-globals -mnontrapping-fptoint -mreference-types -mrelaxed-simd -msign-ext -msimd128

cflags-y += -Wno-incompatible-library-redeclaration '-D__builtin_return_address=wasm_return_address' $(WASM_FEATURES)
ldflags-y += --import-memory
ldflags-y += --import-memory --shared-memory --max-memory=4294967296 --no-gc-sections

KBUILD_CFLAGS += $(cflags-y)
KBUILD_AFLAGS += $(aflags-y)
Expand All @@ -16,6 +16,9 @@ arch/wasm/vmlinux.wasm: vmlinux
$(Q)$(MAKE) vmlinux
$(Q)cp vmlinux $@

tools/wasm/vmlinux.wasm: vmlinux
$(Q)cp vmlinux $@

define archhelp
echo '* vmlinux.wasm - Kernel binary (arch/$(ARCH)/vmlinux.wasm)'
endef
34 changes: 34 additions & 0 deletions arch/wasm/configs/defconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is not set
CONFIG_CC_OPTIMIZE_FOR_SIZE=y
# CONFIG_KERNEL_GZIP is not set
# CONFIG_KERNEL_BZIP2 is not set
# CONFIG_KERNEL_LZMA is not set
CONFIG_KERNEL_XZ=y
# CONFIG_KERNEL_LZO is not set
# CONFIG_KERNEL_LZ4 is not set
# CONFIG_SLAB is not set
# CONFIG_SLUB is not set
CONFIG_SLOB=y

CONFIG_PRINTK=y
CONFIG_BINFMT_SCRIPT=y
CONFIG_BUG=y
CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_KERNEL=y
CONFIG_DEBUG_MEMORY_INIT=y
CONFIG_DEBUG_MISC=y
CONFIG_DEBUG_NOMMU_REGIONS=y
CONFIG_DEBUG_VM=y
CONFIG_EARLY_PRINTK=y
CONFIG_EMBEDDED=y
CONFIG_EXPERT=y
# CONFIG_KALLSYMS is not set
# CONFIG_MMU is not set
CONFIG_UACCESS_MEMCPY=y
CONFIG_PRINTK_TIME=y
CONFIG_EXT4_FS=y
CONFIG_NET=y


CONFIG_EMBEDDED=y
5 changes: 2 additions & 3 deletions arch/wasm/include/asm/wasm_imports.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#define import(name) __attribute__((import_module("kernel"), import_name(name)))

import("print") void wasm_print(const char *msg, size_t len);
import("boot_console_write") void wasm_boot_console_write(const char *msg, size_t len);
import("boot_console_close") void wasm_boot_console_close(void);

import("set_irq_enabled") void wasm_set_irq_enabled(int enabled);
import("get_irq_enabled") int wasm_get_irq_enabled(void);
Expand All @@ -21,6 +22,4 @@ import("get_now_nsec") unsigned long long wasm_get_now_nsec(void);

#undef import

#define wasm_puts(msg) wasm_print(msg, strlen(msg))

#endif
1 change: 1 addition & 0 deletions arch/wasm/kernel/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extra-y := vmlinux.lds

obj-y += \
cpuinfo.o \
early_printk.o \
irq.o \
process.o \
Expand Down
38 changes: 38 additions & 0 deletions arch/wasm/kernel/cpuinfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <linux/seq_file.h>

static void percpu_print(void *arg)
{
struct seq_file *m = arg;
seq_printf(m, "processor : %d\n", smp_processor_id());
seq_printf(m, "\n");
}

static int c_show(struct seq_file *m, void *v)
{
int cpu;

for_each_online_cpu(cpu)
smp_call_function_single(cpu, percpu_print, m, true);

return 0;
}

static void *c_start(struct seq_file *m, loff_t *pos)
{
return *pos < 1 ? (void *)1 : NULL;
}

static void *c_next(struct seq_file *m, void *v, loff_t *pos)
{
++*pos;
return NULL;
}

static void c_stop(struct seq_file *m, void *v) {}

const struct seq_operations cpuinfo_op = {
.start = c_start,
.next = c_next,
.stop = c_stop,
.show = c_show,
};
10 changes: 8 additions & 2 deletions arch/wasm/kernel/early_printk.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <asm/wasm_imports.h>
#include <linux/compiler_attributes.h>
#include <linux/console.h>
#include <linux/init.h>
#include <linux/kernel.h>
Expand All @@ -8,12 +7,19 @@
static void early_console_write(struct console *con, const char *s,
unsigned int n)
{
wasm_print(s, n);
wasm_boot_console_write(s, n);
}

static int early_console_exit(struct console *con)
{
wasm_boot_console_close();
return 0;
}

static struct console early_console_dev = {
.name = "earlycon",
.write = early_console_write,
.exit = early_console_exit,
.flags = CON_BOOT,
.index = -1,
};
Expand Down
13 changes: 12 additions & 1 deletion arch/wasm/kernel/irq.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#include <linux/printk.h>
#include <linux/irqchip.h>
#include <linux/seq_file.h>

void init_IRQ(void)
{
__builtin_trap();
pr_info("init irq\n");
irqchip_init();
}

int show_interrupts(struct seq_file *p, int prec)
{
seq_printf(p, "nothing to see here\n");
return 0;
}
38 changes: 25 additions & 13 deletions arch/wasm/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <linux/mutex.h>
#include <linux/of_fdt.h>
#include <linux/printk.h>
#include <linux/screen_info.h>
#include <linux/start_kernel.h>
#include <linux/types.h>

Expand All @@ -24,9 +25,19 @@ uintptr_t __stop___ex_table, __start___ex_table, __sched_class_highest,
__per_cpu_load, __per_cpu_end, _stext, _etext, _sinittext, _einittext,
__sched_text_start, __sched_text_end, __cpuidle_text_start,
__cpuidle_text_end, __lock_text_start, __lock_text_end, __bss_start,
__bss_stop, _sdata, _edata, __reservedmem_of_table;
__bss_stop, _sdata, _edata, __reservedmem_of_table, __start_builtin_fw,
__end_builtin_fw;


struct screen_info screen_info = {};

// https://github.com/llvm/llvm-project/blob/d2e4a725da5b4cbef8b5c1446f29fed1487aeab0/lld/wasm/Symbols.h#L515
void __init __wasm_call_ctors(void);
extern void __stack_low;
extern void __stack_high;
extern void __heap_base;
extern void __heap_end;

int __init setup_early_printk(char *buf);

static char wasm_dt[1024];
Expand All @@ -36,10 +47,18 @@ void __init _start(void)
__wasm_call_ctors();

setup_early_printk(NULL);
pr_info("heap: %zu->%zu=%zu\n", (uintptr_t)&__heap_base,
(uintptr_t)&__heap_end,
(uintptr_t)&__heap_end - (uintptr_t)&__heap_base);
pr_info("stack: %zu->%zu=%zu\n", (uintptr_t)&__stack_low,
(uintptr_t)&__stack_high,
(uintptr_t)&__stack_high - (uintptr_t)&__stack_low);

wasm_get_dt(wasm_dt, ARRAY_SIZE(wasm_dt));
early_init_dt_scan(wasm_dt);

memblock_reserve(0, (phys_addr_t)&__heap_base);

start_kernel();
}

Expand All @@ -49,23 +68,16 @@ void __init setup_arch(char **cmdline_p)
strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
*cmdline_p = command_line;

#ifdef CONFIG_BLK_DEV_INITRD
if (initrd_start < initrd_end &&
!mem_reserve(__pa(initrd_start), __pa(initrd_end)))
initrd_below_start_ok = 1;
else
initrd_start = 0;
#endif

parse_early_param();
early_printk("hello!\n");

unflatten_device_tree();

#ifdef CONFIG_SMP
smp_init_cpus();
#endif

memblock_dump_all();

zones_init();
}

Expand All @@ -77,17 +89,17 @@ void __init setup_arch(char **cmdline_p)

void machine_restart(char *cmd)
{
early_printk("restart: %s", cmd);
pr_info("restart: %s", cmd);
__builtin_trap();
}

void machine_halt(void)
{
early_printk("halt");
pr_info("halt");
__builtin_trap();
}
void machine_power_off(void)
{
early_printk("poweroff");
pr_info("poweroff");
__builtin_trap();
}
4 changes: 2 additions & 2 deletions arch/wasm/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ void __init mem_init(void)
{
void *alloc;

early_printk("mem_init\n");
pr_info("mem_init\n");

memblock_free_all();

alloc = kmalloc(64, 0);
early_printk("malloc() = %p\n", alloc);
pr_info("malloc() = %p\n", alloc);
}
10 changes: 8 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@
lld
libllvm
wabt
esbuild
];

enableParallelBuilding = true;
buildPhase = "make -j$NIX_BUILD_CORES tinyconfig arch/wasm/vmlinux.wasm";
installPhase = "mkdir $out && cp arch/wasm/vmlinux.wasm $out/";
configurePhase = "make -j$NIX_BUILD_CORES tinyconfig debug.config";
buildPhase = "make -j$NIX_BUILD_CORES arch/wasm/vmlinux.wasm tools/wasm";
installPhase = ''
mkdir $out
cp -r tools/wasm/* $out/
cp arch/wasm/vmlinux.wasm $out/
'';
};
});
};
Expand Down
Loading

0 comments on commit 3436fa5

Please sign in to comment.