From 8c0eb4f6f2da5ce928a3a126c78c161e04958031 Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Tue, 27 Jun 2023 10:02:23 -0700 Subject: [PATCH] add files from learning repo --- .gitignore | 35 +++++++++++++++++++++++++++++++++++ Makefile | 33 +++++++++++++++++++++++++++++++++ boot.s | 8 ++++++++ kernel.nim | 4 ++++ linker.ld | 13 +++++++++++++ nim.cfg | 18 ++++++++++++++++++ panicoverride.nim | 10 ++++++++++ strutil.nim | 5 +++++ 8 files changed, 126 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 boot.s create mode 100644 kernel.nim create mode 100644 linker.ld create mode 100644 nim.cfg create mode 100644 panicoverride.nim create mode 100644 strutil.nim diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5c50867 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +out/* +nimcache/ +nimblecache/ +htmldocs/ + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# out +*.o +*.elf diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..acc2d68 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +SHELL = /bin/bash +.DEFAULT_GOAL := build +BUILDDIR = build +BINDIR = bin +OBJECT_FILES = boot.o + +build: clean-objects assemble-stub compile copy-o link + +assemble-stub: + mkdir -p $(BUILDDIR)/ + aarch64-elf-as boot.s -o $(BUILDDIR)/boot.o + +compile: + mkdir -p $(BUILDDIR)/ + nim c -d:release kernel.nim + +copy-o: + cp nimcache/*.o build/ + +clear, clean: + rm -rf $(BUILDDIR) nimcache/ bin/ + +clean-objects: + rm -rf $(BUILDDIR)/*.o nimcache/*.o + +link: + mkdir -p $(BINDIR) && \ + aarch64-elf-ld -nostdlib -Tlinker.ld -o bin/kernel.elf -O2 $(wildcard build/*.o) + +boot: + +setup-macos: + brew install aarch64-elf-binutils aarch64-elf-gcc nim diff --git a/boot.s b/boot.s new file mode 100644 index 0000000..26ba90c --- /dev/null +++ b/boot.s @@ -0,0 +1,8 @@ +// QEMU Aarch64 Boot Stub + +.global _start +_start: + ldr x30, =stack_top + mov sp, x30 + bl kmain + b . diff --git a/kernel.nim b/kernel.nim new file mode 100644 index 0000000..312f899 --- /dev/null +++ b/kernel.nim @@ -0,0 +1,4 @@ +import strutil + +proc kmain {.exportc.} = + printTxt(cast[seq[char]]("hello world\n")) diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..0286576 --- /dev/null +++ b/linker.ld @@ -0,0 +1,13 @@ +/* All this linker script does is allocate memory and */ +/* prepend the QEMU boot stub to our program */ +ENTRY(_start) +SECTIONS { + . = 0x40000000; + .startup . : { build/boot.o(.text) } + .text : { *(.text) } + .data : { *(.data) } + .bss : { *(.bss COMMON) } + . = ALIGN(8); + . += 0x1000; /* 4kB of stack memory */ + stack_top = .; +} diff --git a/nim.cfg b/nim.cfg new file mode 100644 index 0000000..f0ad17d --- /dev/null +++ b/nim.cfg @@ -0,0 +1,18 @@ +gcc.exe = "aarch64-elf-gcc" +gcc.linkerexe = "aarch64-elf-ld" + +--os:standalone +--cpu:arm64 +-d:useMalloc +--mm:orc +--cc:gcc +--index:off +--passC:"-w -I$lib -I$LIBRARY_PATH -ffreestanding -O2 -Wall -Wextra" +# --passL:"-nostdlib -Tlinker.ld -o kernel.elf -O2" +--boundChecks:on +--noMain +--noLinking +--deadCodeElim:on +--hints:on +--listFullPaths:off +--nimcache:"nimcache" diff --git a/panicoverride.nim b/panicoverride.nim new file mode 100644 index 0000000..0e1498d --- /dev/null +++ b/panicoverride.nim @@ -0,0 +1,10 @@ +import strutil + +func halt = + asm "wfi" + +proc panic*(str: string) = + halt() + +proc rawoutput*(str: string) = + printTxt(cast[seq[char]](str)) diff --git a/strutil.nim b/strutil.nim new file mode 100644 index 0000000..06c7ab0 --- /dev/null +++ b/strutil.nim @@ -0,0 +1,5 @@ +var uart*: ptr uint8 = cast[ptr uint8](0x09000000) + +proc printTxt*(str: seq[char]) = + for i in 0..