diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..20596b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.elf diff --git a/Makefile b/Makefile index 7b917e5..fc77b16 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,36 @@ +BUILDDIR = build + +all: githooks $(BUILDDIR)/kernel.elf + githooks: | hooks @git config core.hooksPath hooks/ + +$(BUILDDIR): + @mkdir $(BUILDDIR) + +clean: + @$(RM) -rf $(BUILDDIR) + +CC = $(CROSS_PREFIX)gcc + +SRCS = $(wildcard src/*.S src/*.c) +OBJS = $(patsubst src/%.S,$(BUILDDIR)/%.o,$(SRCS)) +OBJS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(OBJS)) + +CFLAGS = -I include/ -mcmodel=medany +LDFLAGS = -ffreestanding -nostdlib + +$(BUILDDIR)/%.o: src/%.S | $(BUILDDIR) + @printf " CCAS\t$(@F)\n" + @$(CC) $< -c -o $@ $(CFLAGS) + +$(BUILDDIR)/%.o: src/%.c | $(BUILDDIR) + @printf " CC\t$(@F)\n" + @$(CC) $< -c -o $@ $(CFLAGS) + +$(BUILDDIR)/kernel.elf: $(OBJS) kernel.ld | $(BUILDDIR) + @printf " CCLD\t$(@F)\n" + @$(CC) $(OBJS) -o $@ -T kernel.ld $(LDFLAGS) + +run: $(BUILDDIR)/kernel.elf + @qemu-system-riscv64 -M virt -nographic -kernel $< diff --git a/include/attribs.h b/include/attribs.h new file mode 100644 index 0000000..c4c0ec7 --- /dev/null +++ b/include/attribs.h @@ -0,0 +1,3 @@ +#pragma once + +#include diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..cb1df71 --- /dev/null +++ b/include/config.h @@ -0,0 +1,3 @@ +#pragma once + +#define KSTACK_BASE 0x88000000 diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..1060720 --- /dev/null +++ b/include/types.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef uintptr_t usize; +typedef intptr_t ssize; diff --git a/kernel.ld b/kernel.ld new file mode 100644 index 0000000..c65a0b9 --- /dev/null +++ b/kernel.ld @@ -0,0 +1,33 @@ +ENTRY(_start) + +SECTIONS { + . = 0x80200000; + + .init : { + *(.init .init.*) + } + + .text : { + *(.text .text.*) + } + + .rodata : { + *(.rodata .rodata.*) + } + + .data : { + *(.data .data.*) + } + + .sdata : { + *(.sdata .sdata.*) + } + + .sbss : { + *(.sbss .sbss.*) + } + + .bss : { + *(.bss .bss.*) + } +} diff --git a/src/entry.S b/src/entry.S new file mode 100644 index 0000000..e7bd246 --- /dev/null +++ b/src/entry.S @@ -0,0 +1,8 @@ +#include + + .section .init + + .global _start +_start: + li sp, KSTACK_BASE + tail kmain diff --git a/src/kmain.c b/src/kmain.c new file mode 100644 index 0000000..d3d4ee8 --- /dev/null +++ b/src/kmain.c @@ -0,0 +1,13 @@ +#include +#include + +noreturn void kmain(void) { + volatile u8 *uart = (volatile u8 *) 0x10000000; + const char *s = "Hello, world!\n"; + + for (int i = 0; s[i] != '\0'; i++) + *uart = s[i]; + + while (true) + ; +}