Skip to content

Instantly share code, notes, and snippets.

@lf-

lf-/Makefile Secret

Created April 27, 2021 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lf-/e2efac2f780ed820277dbaf608805f4e to your computer and use it in GitHub Desktop.
Save lf-/e2efac2f780ed820277dbaf608805f4e to your computer and use it in GitHub Desktop.
// 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
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
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.*)
}
}
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