-
-
Save lf-/e2efac2f780ed820277dbaf608805f4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// kernel entry point | |
// largely nicked from https://github.com/mit-pdos/xv6-riscv/blob/riscv/kernel/entry.S | |
.data | |
.globl STACKS | |
.text | |
.section .text.first | |
.globl start | |
.globl _entry | |
// _entry(mhartid: usize, dtb: *const u8) | |
_entry: | |
// we arrive here, in machine mode, once qemu jumps to the start of memory | |
// set up a stack | |
la sp, STACKS | |
li t0, 16384 // use 16k stacks | |
// we want to get the pointer to the top of the (descending) stack | |
// thus we want 16k * (hartid + 1) | |
addi t1, a0, 1 | |
mul t0, t0, t1 | |
add sp, sp, t0 | |
call startup | |
spin: | |
j spin // startup() will not return | |
.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LLVM = /usr | |
QEMU = /opt/qemu/bin/qemu-system-riscv64 | |
TARGETFLAGS = --target=riscv64-none-unknown-elf -march=rv64imac | |
CFLAGS = $(TARGETFLAGS) -mno-relax -fPIC -g | |
QEMUOPTS = -machine virt -bios none -kernel kern -m 128M \ | |
-nographic -s -S | |
LINKFLAGS = $(TARGETFLAGS) -fuse-ld=lld -nostdlib -nodefaultlibs \ | |
-Wl,-Tshoo.ld | |
CC = $(LLVM)/bin/clang | |
kern: init.o start.o | |
$(CC) $(LINKFLAGS) $^ -o $@ | |
qemu: kern | |
$(QEMU) $(QEMUOPTS) | |
%.o: %.s | |
$(CC) $(CFLAGS) -c $^ | |
.PHONY: clean qemu | |
clean: | |
rm *.o kern |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OUTPUT_ARCH("riscv64") | |
ENTRY(_entry) | |
SECTIONS { | |
/* VIRT_DRAM */ | |
. = 0x80000000; | |
.text : { | |
*(.text.first) | |
*(.text .text.*) | |
. = ALIGN(0x1000); | |
} | |
.rodata ALIGN(0x1000) : { | |
. = ALIGN(16); | |
PROVIDE(srodata = .); | |
*(.srodata .srodata.*) | |
. = ALIGN(16); | |
*(.rodata .rodata.*) | |
. = ALIGN(0x1000); | |
PROVIDE(erodata = .); | |
} | |
.data ALIGN(0x1000) : { | |
PROVIDE(srwdata = .); | |
*(.sdata .sdata.*) | |
. = ALIGN(16); | |
*(.data .data.*) | |
} | |
.bss ALIGN(0x1000) : { | |
. = ALIGN(16); | |
*(.sbss .sbss.*) | |
. = ALIGN(16); | |
*(.bss .bss.*) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char STACKS[4*4096]; | |
volatile int i1 = 0; | |
volatile int i2 = 0; | |
volatile int i3 = 0; | |
void fn3(void) { | |
i3 = 10; | |
} | |
void fn2(void) { | |
i2 = 3; | |
fn3(); | |
} | |
void fn1(void) { | |
fn2(); | |
fn2(); | |
i1 = 18; | |
} | |
void startup(void) { | |
fn1(); | |
while (1) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment