add simple sum example

This commit is contained in:
2026-01-29 00:19:14 +01:00
parent 8f09e49c8a
commit ce0c7f4f7f
26 changed files with 3333 additions and 115 deletions

154
examples/arch/bochs.mk Normal file
View File

@ -0,0 +1,154 @@
# C -> WASM
WASI_ROOT := /opt/wasi-sdk
WASI_CC := ${WASI_ROOT}/bin/clang
WASI_CFLAGS := --target=wasm32 \
--sysroot=${WASI_ROOT}/share/wasi-sysroot \
-z stack-size=4096 \
-nostdlib \
-O0 -g -Wall \
-Wl,--no-entry \
-Wl,--initial-memory=65536 \
-Wl,--export-all \
-Wl,--export=__heap_base \
-Wl,--export=__data_end
# WASM -> Baremetal
WAMR := /opt/wamr
IWASM_LIB := /opt/wamr-libiwasm
CC := gcc
CFLAGS := -I. -include arch/${ARCH}/lib.c -O0 -g -m32 -fomit-frame-pointer
LDFLAGS := -Wl,-T linker.ld $^ -Wl,--build-id=none -static -nostdlib -m32 \
-Wl,-rpath,${IWASM_LIB} -L${IWASM_LIB} -liwasm
INCL := -I${WAMR}/core/iwasm/include \
-I${WAMR}/core/iwasm/common \
-I${WAMR}/core/shared/platform/include \
-I${WAMR}/core/shared/platform/linux \
-I${WAMR}/core/shared/mem-alloc \
-I${WAMR}/core/shared/utils \
-I${WAMR}/core/shared/utils/uncommon
WAMRC := /opt/wamr-wamrc/wamrc
WAMRCFLAGS := --target=i386 --format=object
XXD := busybox xxd
WASM2C := wasm2c
################################################################
# C -> WASM
${BUILD_DIR}/%/module.wasm: %.c
mkdir -p $(shell dirname $@)
${WASI_CC} ${WASI_CFLAGS} $< -o $@
################################################################
# WASM -> Native Object File
# ${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module.wasm
# ${WAMRC} ${WAMRCFLAGS} -o ${BUILD_DIR}/$*/system.o ${BUILD_DIR}/$*/module.wasm
#
# ${BUILD_DIR}/startup.o: arch/bochs/startup.s
# ${CC} $< ${CFLAGS} -c -ffunction-sections -o $@
#
# ${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
# ${CC} ${LDFLAGS} -o $@
################################################################
# WASM -> AOT -> Loaded by Runtime
${BUILD_DIR}/%/module.aot: ${BUILD_DIR}/%/module.wasm
${WAMRC} -o $@ $<
${BUILD_DIR}/%/module_wasm.c: ${BUILD_DIR}/%/module.aot
${XXD} -i $< > $@
${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module_wasm.c
cp embed/host.c ${BUILD_DIR}/$*/module_host.c
sed -i \
-e "s/__WASM_ARRAY_FILE__/module_wasm.c/g" \
-e "s/__WASM_ARRAY__/build_bochs_$*_module_aot/g" \
-e "s/__WASM_ARRAY_LEN__/build_bochs_$*_module_aot_len/g" \
${BUILD_DIR}/$*/module_host.c
${CC} ${CFLAGS} ${INCL} -c -ffunction-sections ${BUILD_DIR}/$*/module_host.c -o $@
${BUILD_DIR}/startup.o: arch/bochs/startup.s
${CC} $< ${CFLAGS} -c -ffunction-sections -o $@
${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
${CC} ${LDFLAGS} -o $@
################################################################
# Bochs
${BUILD_DIR}/%/system.iso: ${BUILD_DIR}/%/system.elf
rm -rf $(shell dirname $<)/grub
mkdir -p $(shell dirname $<)/grub/boot/grub
cp arch/bochs/grub.cfg $(shell dirname $<)/grub/boot/grub
cp $< $(shell dirname $<)/grub/boot/system.elf
grub-mkrescue -o $@ $(shell dirname $<)/grub
BOCHS_RUNNER_ARGS = \
-V arch/bochs/vgabios.bin \
-b arch/bochs/BIOS-bochs-latest \
${BUILD_DIR}/%/trace.pb: ${BUILD_DIR}/%/system.iso
${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} -1 \
-f ${FAIL_TRACE} \
-e $(shell dirname $<)/system.elf \
-i $(shell dirname $<)/system.iso \
-- \
-Wf,--state-file=$(shell dirname $<)/state \
-Wf,--trace-file=$(shell dirname $<)/trace.pb \
-Wf,--start-symbol=start_trace \
-Wf,--end-symbol=stop_trace \
-Wf,--check-bounds
client-%:
${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} \
-f ${FAIL_INJECT} \
-e ${BUILD_DIR}/$(subst client-,,$@)/system.elf \
-i ${BUILD_DIR}/$(subst client-,,$@)/system.iso \
-j $(shell getconf _NPROCESSORS_ONLN) \
-- \
-Wf,--state-dir=${BUILD_DIR}/$(subst client-,,$@)/state \
-Wf,--trap \
-Wf,--timeout=10 \
-Wf,--ok-marker=ok_marker \
-Wf,--fail-marker=fail_marker \
-Wf,--catch-write-textsegment \
-Wf,--catch-outerspace \
2>/dev/null | grep -B 2 -A 8 'INJECT'
inject-%:
${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} -1 \
-f ${FAIL_INJECT} \
-e ${BUILD_DIR}/$(subst inject-,,$@)/system.elf \
-i ${BUILD_DIR}/$(subst inject-,,$@)/system.iso \
-j 1 -- \
-Wf,--state-dir=${BUILD_DIR}/$(subst inject-,,$@)/state \
-Wf,--trap \
-Wf,--timeout=10 \
-Wf,--ok-marker=ok_marker \
-Wf,--fail-marker=fail_marker \
-Wf,--catch-write-textsegment \
-Wf,--catch-outerspace
# TODO: Command line interface changed
import-arch-%: ${BUILD_DIR}/%/trace.pb ${HOME}/.my.cnf
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b mem -t $< -e $(shell dirname $<)/system.elf -i mem --memory-type ram
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs-trace -t $< -e $(shell dirname $<)/system.elf -i mem --memory-type register
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs -t $< -e $(shell dirname $<)/system.elf -i regs
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b ip -t $< -e $(shell dirname $<)/system.elf -i regs --no-gp --ip
# ${FAIL_PRUNE} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b %% --overwrite
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b mem -t $< -e $(shell dirname $<)/system.elf -i MemoryImporter
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs-trace -t $< -e $(shell dirname $<)/system.elf -i MemoryImporter
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs -t $< -e $(shell dirname $<)/system.elf -i RegisterImporter
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b ip -t $< -e $(shell dirname $<)/system.elf -i RegisterImporter --no-gp --ip
${FAIL_PRUNE} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b %% --overwrite
define arch-make-targets
build-$1: ${BUILD_DIR}/$1/system.iso
trace-$1: ${BUILD_DIR}/$1/trace.pb
endef

Binary file not shown.

View File

@ -0,0 +1,7 @@
set timeout=0
set default=0
menuentry "CoRedOS" {
multiboot /boot/system.elf
boot
}

View File

@ -0,0 +1,4 @@
#pragma once
#define ARCH_ASM_CLOBBER_ALL "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp"

View File

@ -0,0 +1,59 @@
## Bare bone boot.s from wiki.osdev.org
# multiboot header
.section .rodata.multiboot
.align 4
# magic number
.long 0x1BADB002
# flags: align, meminfo
.long 0x3
# checksum: -(magic+flags)
.long -(0x1BADB002 + 0x3)
# the initial kernel stack
.section .kernel_stack
.global os_stack
.size os_stack, 4096
#.Lstack_bottom:
os_stack:
.byte 0
#.skip 16384 # 16 KiB
.skip 4094 # 4 KiB
.byte 0
.Lstack_top:
# The linker script specifies _start as the entry point to the kernel and the
# bootloader will jump to this position once the kernel has been loaded. It
# doesn't make sense to return from this function as the bootloader is gone.
.section .text.startup
.global _start
.type _start, @function
_start:
# Welcome to kernel mode!
# To set up a stack, we simply set the esp register to point to the top of
# our stack (as it grows downwards).
movl $.Lstack_top, %esp
# We are now ready to actually execute C code. (see ./startup.cc)
call os_main
# In case the function returns, we'll want to put the computer into an
# infinite loop. To do that, we use the clear interrupt ('cli') instruction
# to disable interrupts, the halt instruction ('hlt') to stop the CPU until
# the next interrupt arrives, and jumping to the halt instruction if it ever
# continues execution, just to be safe. We will create a local label rather
# than real symbol and jump to there endlessly.
cli
hlt
.Lhang:
jmp .Lhang
# Set the size of the _start symbol to the current location '.' minus its start.
# This is useful when debugging or when you implement call tracing.
.size _start, . - _start

Binary file not shown.