diff options
| author | Tianhao Wang <shrik3@mailbox.org> | 2024-06-06 23:08:02 +0200 |
|---|---|---|
| committer | Tianhao Wang <shrik3@mailbox.org> | 2024-06-11 15:17:14 +0200 |
| commit | 8aaad696463004a9e51d35e4c466c131b3402822 (patch) | |
| tree | 6d1e84f83e308eca4907d7986b32121b3c92d284 | |
| parent | 1e890f4e5693d141d62660de5e9481fdf16483ad (diff) | |
chore: cleanups
Signed-off-by: Tianhao Wang <shrik3@mailbox.org>
| -rw-r--r-- | Makefile | 42 | ||||
| -rw-r--r-- | defs/x86_64-hm-linker.ld | 14 | ||||
| -rw-r--r-- | defs/x86_64-linker.ld | 99 | ||||
| -rw-r--r-- | src/lib.rs | 1 |
4 files changed, 23 insertions, 133 deletions
@@ -3,22 +3,12 @@ # for those (me included) who are not sure about # the building process. # TODO reorganize... -# TODO add dependencies (.d) if necessary but I don't think so... -# the librustubs is cargo self-contained), others are asm code, -# for which dep files are not needed -# And .. I don't think I'll add c/c++ files to this project.. # TODO replace hardcoded values with variables -# TODO there can be more options of grub-mkrescue -# TODO put the startup.s elsewhere (I don't like it in the root dir) -# TODO maybe put the bootdisk.iso in the build dir too .. - # verbose for testing; VERBOSE=@ to turn off.. VERBOSE=@ BUILD = build ARCH = x86_64 -ASM = nasm -ASMOBJFORMAT = elf64 -ASMFLAGS = -w-zeroing +NASMFLAGS = -w-zeroing -f elf64 LINKER_SCRIPT = ./defs/$(ARCH)-hm-linker.ld CARGO_XBUILD_TARGET = ./defs/$(ARCH)-rustubs.json CARGO_XBUILD_FLAGS = @@ -30,26 +20,26 @@ ASM_SOURCES = $(shell find ./src -name "*.s") ASM_OBJECTS = $(patsubst %.s,_%.o, $(notdir $(ASM_SOURCES))) # I don't like this style... but what can I do? ASMOBJ_PREFIXED = $(addprefix $(BUILD)/,$(ASM_OBJECTS)) +RUST_OBJECT = target/$(ARCH)-rustubs/$(RUST_BUILD)/librustubs.a # Setting directories to look for missing source files VPATH = $(sort $(dir $(ASM_SOURCES))) - - ifneq ($(filter --release,$(CARGO_XBUILD_FLAGS)),) RUST_BUILD = release else RUST_BUILD = debug endif -RUST_OBJECT = target/$(ARCH)-rustubs/$(RUST_BUILD)/librustubs.a - all: bootdisk.iso bootdisk.iso : $(BUILD)/kernel @echo "---BUILDING BOOTDISK IMAGE---" $(VERBOSE) cp $< isofiles/boot/ - $(VERBOSE) grub-mkrescue -d /usr/lib/grub/i386-pc --locales=en@piglatin --themes=none -o bootdisk.iso isofiles > /dev/null 2>&1 + $(VERBOSE) grub-mkrescue -d /usr/lib/grub/i386-pc \ + --locales=en@piglatin --themes=none \ + -o bootdisk.iso isofiles > /dev/null 2>&1 -# Note: explicitly tell the linker to use startup: as the entry point (we have no main here) +# Note: explicitly tell the linker to use startup: as the entry point (we have +# no main here) $(BUILD)/kernel : rust_kernel startup.o $(ASMOBJ_PREFIXED) @echo "---LINKING ... ---" $(VERBOSE) ld $(LDFLAGS) -T $(LINKER_SCRIPT) -o $@ $(BUILD)/startup.o $(ASMOBJ_PREFIXED) $(RUST_OBJECT) @@ -58,22 +48,21 @@ $(BUILD)/kernel : rust_kernel startup.o $(ASMOBJ_PREFIXED) $(BUILD)/_%.o : %.s | $(BUILD) @echo "---ASM $@" @if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi - $(VERBOSE) $(ASM) -f $(ASMOBJFORMAT) $(ASMFLAGS) -o $@ $< + $(VERBOSE) $(ASM) $(ASMFLAGS) -o $@ $< - -# install xbuild first. (cargo install xbuild) -# Compile the rust part: note that the the cargo crate is of type [staticlib], if you don't -# define this, the linker will have troubles, especially when we use a "no_std" build +# Compile the rust part: note that the the cargo crate is of type [staticlib], +# if you don't define this, the linker will have troubles, especially when we +# use a "no_std" build rust_kernel: check @echo "---BUILDING RUST KERNEL---" RUSTFLAGS="$(RUSTC_FLAGS)" cargo build --target $(CARGO_XBUILD_TARGET) $(CARGO_XBUILD_FLAGS) -# need nasm +# compile the assembly source # TODO make this arch dependent startup.o: boot/startup-$(ARCH).s | $(BUILD) @echo "---ASM $@" @if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi - $(VERBOSE) $(ASM) -f $(ASMOBJFORMAT) $(ASMFLAGS) -o $(BUILD)/startup.o boot/startup-$(ARCH).s + $(VERBOSE) nasm $(NASMFLAGS) -o $(BUILD)/startup.o boot/startup-$(ARCH).s .PHONY: $(BUILD) $(BUILD): @@ -99,8 +88,8 @@ gdb: gdb -x /tmp/gdbcommands.$(shell id -u) build/kernel qemu-gdb: bootdisk.iso - @echo "target remote localhost:$(shell echo $$(( $$(id -u) % (65536 - 1024) + 1024 )))" > /tmp/gdbcommands.$(shell id -u) - @qemu-system-x86_64 -drive file=bootdisk.iso,format=raw -k en-us -S -gdb tcp::$(shell echo $$(( $$(id -u) % (65536 - 1024) + 1024 ))) -serial mon:stdio + @echo "target remote localhost:9876" > /tmp/gdbcommands.$(shell id -u) + @qemu-system-x86_64 -drive file=bootdisk.iso,format=raw -k en-us -S -gdb tcp::9876 -serial mon:stdio test: @echo "---BUILD DIR---" @@ -112,5 +101,4 @@ test: @echo "---ASM OBJ PREFIXED" @echo $(ASMOBJ_PREFIXED) - .PHONY: clean qemu test diff --git a/defs/x86_64-hm-linker.ld b/defs/x86_64-hm-linker.ld index fab0699..2055ce4 100644 --- a/defs/x86_64-hm-linker.ld +++ b/defs/x86_64-hm-linker.ld @@ -1,11 +1,11 @@ /* defs for Multiboot headers */ /* https://www.gnu.org/software/grub/manual/multiboot/multiboot.txt */ -MB_MAGIC = 0x1badb002; -/* bit 0 - * all boot modules loaded along with the operating system must be - * aligned on page (4KB) bit 1 must include mem_* structures +/* bit 0: all boot modules loaded along with the operating system must + * be aligned on page (4KB) + * bit 1: must include mem_* structures */ +MB_MAGIC = 0x1badb002; MB_FLAGS = 0x3; MB_CHKSUM = 0x100000000 - (MB_MAGIC + MB_FLAGS); @@ -50,7 +50,6 @@ SECTIONS *(".reserved_0.*") } - /* global page table for 64-bit long mode */ .global_pagetable ALIGN(4096) (NOLOAD) : { @@ -76,6 +75,9 @@ SECTIONS } . = . + KERNEL_OFFSET; + + /* .ltext, .ldata, .lbss, .rodata etc are generated by rust compiler */ + /* because of "code-model=large setting" */ .text : AT(ADDR(.text) - KERNEL_OFFSET) { *(".text") @@ -100,8 +102,8 @@ SECTIONS { PROVIDE (___BSS_START__ = .); *(".bss") - *(".lbss") *(".bss.*") + *(".lbss") *(".lbss.*") *(".lbss$") PROVIDE (___BSS_END__ = .); diff --git a/defs/x86_64-linker.ld b/defs/x86_64-linker.ld deleted file mode 100644 index f22f5ad..0000000 --- a/defs/x86_64-linker.ld +++ /dev/null @@ -1,99 +0,0 @@ -/* defs for Multiboot headers */ -/* https://www.gnu.org/software/grub/manual/multiboot/multiboot.txt */ -MB_MAGIC = 0x1badb002; -/* bit 0 - * all boot modules loaded along with the operating system must be - * aligned on page (4KB) bit 1 must include mem_* structures - */ -MB_FLAGS = 0x3; -MB_CHKSUM = 0x100000000 - (MB_MAGIC + MB_FLAGS); - -SECTIONS -{ - . = 0x100000; /* system's start address */ - - .boot : - { - header_start = .; - LONG(MB_MAGIC) - LONG(MB_FLAGS) - LONG(MB_CHKSUM) - LONG(0) - LONG(0) - LONG(0) - LONG(0) - LONG(0) - LONG(0) - LONG(0) - LONG(0) - LONG(0) - header_end = .; - } - - PROVIDE (___KERNEL_PM_START__ = .); - .text : - { - *(".text") - *(".text.*") - *(".text$") - } - - .data : - { - *(".data") - *(".data.*") - *(".data$") - } - - .reserved : - { - *(".reserved") - *(".reserved.*") - } - - /* - * basically the same as BSS, but I want some flexibility and I don't care - * for zeroing because it's explicitly overwritten anyways. I KNOW WHAT I'M - * DOING! An example is the idt. - */ - .reserved_0 (NOLOAD) : - { - *(".reserved_0") - *(".reserved_0.*") - } - - .rodata : - { - *(".rodata") - *(".rodata$") - *(".rodata.*") - } - - .bss : - { - PROVIDE (___BSS_START__ = .); - *(".bss") - *(".bss.*") - PROVIDE (___BSS_END__ = .); - } - - /* global page table for 64-bit long mode */ - .global_pagetable ALIGN(4096) (NOLOAD) : - { - *(".global_pagetable") - } - - . = ALIGN(4096); - /* reserve space for a premitive stack based physical frame allocator */ - /* each frame is 4KiB in size and has a 64bit (physical) address. e.g. */ - /* for every 1 GiB physical memory we need 2 MiB space reserved for the */ - /* free stack. For a easier bootstraping we are using a fix-sized stack */ - /* array. Currently using 4GiB, therefore reserve 8MiB. */ - PROVIDE (___FREE_PAGE_STACK__ = .); - .global_free_page_stack ALIGN(4096) (NOLOAD) : - { - *("..global_free_page_stack") - } - . = ALIGN(4096); - PROVIDE (___KERNEL_PM_END__ = .); -} @@ -22,7 +22,6 @@ use machine::key::Modifiers; use machine::multiboot; use machine::serial::Serial; -#[cfg(not(test))] #[panic_handler] fn panic(info: &PanicInfo) -> ! { println!("{}", info); |
