From e19c7cee50a5120f5093087c349d785f2fac4525 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sat, 24 Mar 2018 18:34:44 -0700 Subject: [PATCH] Give kernel image a header. Kernel image now has a header with version, magic number, and a pointer to its actual entrypoint. Entry point is now _start in boot.s, and we now generate versions.s in the build tree for the version macros. --- Makefile | 26 +++++++++++-------- modules.mk | 7 +++-- parse_version.py | 36 ++++++++++++++++++++++++++ src/arch/x86_64/kernel.ld | 10 +++++--- src/boot/console.c | 10 ++++---- src/boot/main.c | 43 +++++++++++++++++++++++++++---- src/modules/main/boot.s | 26 +++++++++++++++++++ src/modules/main/debug.s | 13 ++++++++++ src/modules/main/main.c | 11 +++++--- src/modules/main/vga.c | 54 +++++++++++++++++++++++++++++++++++++++ src/modules/main/vga.h | 38 +++++++++++++++++++++++++++ 11 files changed, 246 insertions(+), 28 deletions(-) create mode 100755 parse_version.py create mode 100644 src/modules/main/boot.s create mode 100644 src/modules/main/debug.s create mode 100644 src/modules/main/vga.c create mode 100644 src/modules/main/vga.h diff --git a/Makefile b/Makefile index f46572a..e4ee7ef 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ include src/arch/$(ARCH)/config.mk BUILD_D := build ARCH_D := src/arch/$(ARCH) VERSION ?= $(shell git describe --dirty --always) +GITSHA ?= $(shell git rev-parse --short HEAD) KERNEL_FILENAME:= popcorn.bin @@ -48,7 +49,8 @@ WARNFLAGS += -Wno-format-extra-args -Wno-unused-result WARNFLAGS += -Wno-deprecated-declarations -Wno-unused-function WARNFLAGS += -Wno-unused-but-set-parameter -ASFLAGS ?= +ASFLAGS ?= +ASFLAGS += -p $(BUILD_D)/versions.s CFLAGS := $(INCLUDES) $(DEPENDFLAGS) $(BASEFLAGS) $(WARNFLAGS) CFLAGS += -std=c11 -fshort-wchar @@ -64,14 +66,13 @@ ifdef MAX_HRES BOOT_CFLAGS += -DMAX_HRES=$(MAX_HRES) endif -LDFLAGS := -L $(BUILD_D) -ggdb -shared +LDFLAGS := -L $(BUILD_D) -ggdb LDFLAGS += -nostdlib -znocombreloc -Bsymbolic -nostartfiles -BOOT_LDFLAGS := $(LDFLAGS) -BOOT_LDFLAGS += -fPIC +BOOT_LDFLAGS := $(LDFLAGS) -shared BOOT_LDFLAGS += -L $(EFI_ARCH_DIR)/lib -L $(EFI_ARCH_DIR)/gnuefi -AS ?= $(CROSS)as +AS ?= $(CROSS)nasm AR ?= $(CROSS)ar CC ?= $(CROSS)gcc CXX ?= $(CROSS)g++ @@ -120,11 +121,17 @@ clean: dist-clean: clean make -C external/gnu-efi clean -.PHONY: all clean dist-clean init +dump: $(BUILD_D)/kernel.dump + vim $< + +.PHONY: all clean dist-clean init dump $(BUILD_D)/.version: echo '$(VERSION)' | cmp -s - $@ || echo '$(VERSION)' > $@ +$(BUILD_D)/versions.s: + ./parse_version.py "$(VERSION)" "$(GITSHA)" > $@ + -include x $(patsubst %,src/modules/%/module.mk,$(MODULES)) -include x $(DEPS) @@ -153,21 +160,20 @@ $(BUILD_D)/%.bin: $(BUILD_D)/%.elf $(BUILD_D)/boot.dump: $(BUILD_D)/boot.efi $(OBJD) -D -S $< > $@ -$(BUILD_D)/boot/%.s.o: src/boot/%.s $(INIT_DEP) +$(BUILD_D)/boot/%.s.o: src/boot/%.s $(BUILD_D)/versions.s $(INIT_DEP) $(AS) $(ASFLAGS) -o $@ $< $(BUILD_D)/boot/%.c.o: src/boot/%.c $(INIT_DEP) $(CC) $(BOOT_CFLAGS) -c -o $@ $< $(BUILD_D)/kernel.elf: $(KOBJS) $(MOD_TARGETS) $(ARCH_D)/kernel.ld - $(LD) $(LDFLAGS) -u kernel_main -T $(ARCH_D)/kernel.ld -o $@ $(KOBJS) $(patsubst %,-l%,$(MODULES)) + $(LD) $(LDFLAGS) -u _header -T $(ARCH_D)/kernel.ld -o $@ $(patsubst %,-l%,$(MODULES)) $(KOBJS) $(OBJC) --only-keep-debug $@ $@.sym - $(OBJC) --strip-debug $@ $(BUILD_D)/kernel.dump: $(BUILD_D)/kernel.elf $(OBJD) -D -S $< > $@ -$(BUILD_D)/arch/%.s.o: $(ARCH_D)/%.s $(INIT_DEP) +$(BUILD_D)/arch/%.s.o: $(ARCH_D)/%.s $(BUILD_D)/versions.s $(INIT_DEP) $(AS) $(ASFLAGS) -o $@ $< $(BUILD_D)/arch/%.c.o: $(ARCH_D)/%.c $(INIT_DEP) diff --git a/modules.mk b/modules.mk index df0601a..99e93fa 100644 --- a/modules.mk +++ b/modules.mk @@ -16,13 +16,16 @@ MOD_BUILD_D := $(BUILD_D)/d.$(MOD_NAME) MOD_LIBNAME := $(BUILD_D)/lib$(MOD_NAME).a MOD_TARGETS += $(MOD_LIBNAME) -OBJS_$(MOD_NAME) := $(patsubst %.c,build/d.%.o,$(patsubst src/modules/%,%,$(SOURCES))) +OBJS_$(MOD_NAME) := $(patsubst %,build/d.%.o,$(patsubst src/modules/%,%,$(SOURCES))) $(MOD_LIBNAME): $(OBJS_$(MOD_NAME)) $(AR) cr $@ $(OBJS_$(MOD_NAME)) -$(MOD_BUILD_D)/%.o: $(MOD_SRC_D)/%.c $(INIT_DEP) +$(MOD_BUILD_D)/%.c.o: $(MOD_SRC_D)/%.c $(INIT_DEP) $(CC) $(CFLAGS) -c -o $@ $< +$(MOD_BUILD_D)/%.s.o: $(MOD_SRC_D)/%.s $(BUILD_D)/versions.s $(INIT_DEP) + $(AS) $(ASFLAGS) -o $@ $< + DEPS += $(patsubst %.o,%.d,$(OBJS_$(MOD_NAME))) diff --git a/parse_version.py b/parse_version.py new file mode 100755 index 0000000..20ac6ab --- /dev/null +++ b/parse_version.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# +# parse_version.py - Create a NASM version definition file given +# version inputs. Usage: +# +# parse_version.py + + +def split_version(version_string): + major, minor, patch_dirty = version_string.split(".") + patch_dirty = patch_dirty.split("-") + + return major, minor, patch_dirty[0], len(patch_dirty) > 1 + + +def make_nasm(major, minor, patch, dirty, sha): + if dirty: + dirty = "1" + else: + dirty = "0" + + lines = [ + "%define VERSION_MAJOR {}".format(major), + "%define VERSION_MINOR {}".format(minor), + "%define VERSION_PATCH {}".format(patch), + "%define VERSION_GITSHA 0x{}{}".format(dirty, sha), + ] + return "\n".join(lines) + + +if __name__ == "__main__": + import sys + if len(sys.argv) != 3: + print("Usage: {} ".format(sys.argv[0]), file=sys.stderr) + sys.exit(1) + print(make_nasm(*split_version(sys.argv[1]), sys.argv[2])) diff --git a/src/arch/x86_64/kernel.ld b/src/arch/x86_64/kernel.ld index f26d1b3..12c22c1 100755 --- a/src/arch/x86_64/kernel.ld +++ b/src/arch/x86_64/kernel.ld @@ -1,11 +1,15 @@ -ENTRY(kernel_main) +ENTRY(_start) SECTIONS { . = 0x100000; + .header : { + header = .; + KEEP(*(.header)) + } + .text : { code = .; - *(.text.entry) *(.text) } @@ -24,5 +28,5 @@ SECTIONS *(.note.*) } - end = ALIGN(4096); + kernel_end = ALIGN(4096); } diff --git a/src/boot/console.c b/src/boot/console.c index eb5fe6a..31e3165 100644 --- a/src/boot/console.c +++ b/src/boot/console.c @@ -62,16 +62,16 @@ con_initialize (const CHAR16 *version) CHECK_EFI_STATUS_OR_RETURN(status, "ClearScreen"); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTCYAN); - ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"Popcorn OS "); + ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"Popcorn loader "); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTMAGENTA); ST->ConOut->OutputString(ST->ConOut, (CHAR16*)version); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); - ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L" booting...\r\n"); + ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L" booting...\r\n\n"); con_status_begin(L"Setting console display mode: "); - Print(L"%ux%u (%ux%u chars)", + Print(L"\n %ux%u (%ux%u chars)", gfx_out_proto->Mode->Info->HorizontalResolution, gfx_out_proto->Mode->Info->VerticalResolution, ROWS, COLS); @@ -91,14 +91,14 @@ void con_status_ok () { UINTN row = ST->ConOut->Mode->CursorRow; - ST->ConOut->SetCursorPosition(ST->ConOut, COLS-8, row); + ST->ConOut->SetCursorPosition(ST->ConOut, 4, ++row); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"["); ST->ConOut->SetAttribute(ST->ConOut, EFI_GREEN); ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L" ok "); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"]\r"); - ST->ConOut->SetCursorPosition(ST->ConOut, 0, row+1); + ST->ConOut->SetCursorPosition(ST->ConOut, 0, ++row+1); } void diff --git a/src/boot/main.c b/src/boot/main.c index dfce4f3..0a6bf7f 100644 --- a/src/boot/main.c +++ b/src/boot/main.c @@ -10,6 +10,19 @@ #define GIT_VERSION L"no version" #endif +#define KERNEL_MAGIC 0x600db007 + +#pragma pack(push, 1) +struct kernel_version { + uint32_t magic; + uint8_t major; + uint8_t minor; + uint16_t patch; + uint32_t gitsha; + void *entrypoint; +}; +#pragma pack(pop) + EFI_STATUS efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { @@ -26,19 +39,39 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) // because the console is now set up // Get info about the image + /* con_status_begin(L"Gathering image information..."); EFI_LOADED_IMAGE *info = 0; EFI_GUID image_proto = EFI_LOADED_IMAGE_PROTOCOL_GUID; status = ST->BootServices->HandleProtocol(ImageHandle, &image_proto, (void **)&info); CHECK_EFI_STATUS_OR_FAIL(status); con_status_ok(); + */ con_status_begin(L"Loading kernel into memory..."); void *kernel_image = NULL; uint64_t kernel_length = 0; status = loader_load_kernel(&kernel_image, &kernel_length); CHECK_EFI_STATUS_OR_FAIL(status); - Print(L" %u bytes at 0x%x", kernel_length, kernel_image); + Print(L"\n %u bytes at 0x%x", kernel_length, kernel_image); + + struct kernel_version *version = (struct kernel_version*)kernel_image; + if (version->magic != KERNEL_MAGIC) { + Print(L"\n bad magic %x", version->magic); + CHECK_EFI_STATUS_OR_FAIL(EFI_CRC_ERROR); + } + + Print(L"\n Kernel version %d.%d.%d %x%s", + version->major, + version->minor, + version->patch, + version->gitsha & 0x0fffffff, + version->gitsha & 0xf0000000 ? "*" : "" + ); + + Print(L"\n Entrypoint %d", version->entrypoint); + + void (*kernel_main)() = version->entrypoint; con_status_ok(); //memory_dump_map(); @@ -55,6 +88,9 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) status = memory_mark_address_for_update((void**)&kernel_image); CHECK_EFI_STATUS_OR_FAIL(status); + status = memory_mark_address_for_update((void**)&kernel_main); + CHECK_EFI_STATUS_OR_FAIL(status); + status = memory_get_map(&memory_map, &memmap_size, &memmap_key, &desc_size, &desc_version); @@ -70,10 +106,7 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) CHECK_EFI_STATUS_OR_FAIL(status); - void (*kernel_main)() = kernel_image; kernel_main(); - - while (1) __asm__("hlt"); - return status; + return EFI_LOAD_ERROR; } diff --git a/src/modules/main/boot.s b/src/modules/main/boot.s new file mode 100644 index 0000000..22529c3 --- /dev/null +++ b/src/modules/main/boot.s @@ -0,0 +1,26 @@ +MAGIC equ 0x600db007 ; Popcorn OS header magic number + +section .header +align 4 +global _header +_header: + dd MAGIC + db VERSION_MAJOR + db VERSION_MINOR + dw VERSION_PATCH + dd VERSION_GITSHA + dq _start + +section .text +align 16 +global _start:function (_start.end - _start) +_start: + extern kernel_main + call kernel_main + + cli + +.hang: + hlt + jmp .hang +.end: diff --git a/src/modules/main/debug.s b/src/modules/main/debug.s new file mode 100644 index 0000000..14c43d4 --- /dev/null +++ b/src/modules/main/debug.s @@ -0,0 +1,13 @@ + +section .text +global do_the_set_registers +do_the_set_registers: + mov rax, 0xdeadbeef0badc0de + mov rbx, 0xdeadbeef0badc0de + mov rcx, 0xdeadbeef0badc0de + mov rdx, 0xdeadbeef0badc0de + +global _halt +_halt: + hlt + jmp _halt diff --git a/src/modules/main/main.c b/src/modules/main/main.c index 58d0c1f..115e723 100644 --- a/src/modules/main/main.c +++ b/src/modules/main/main.c @@ -1,8 +1,13 @@ -__attribute__((section(".text.entry"))) +#include "vga.h" + +void do_the_set_registers(); + void kernel_main() { volatile register int foo = 0x1a1b1c10; volatile register int bar = 0; - while(1) - foo = foo | 0xfffffff0 + bar++ | 0xf; + terminal_initialize(5); + terminal_writestring("YES HELLO THIS IS KERNEL"); + + do_the_set_registers(); } diff --git a/src/modules/main/vga.c b/src/modules/main/vga.c new file mode 100644 index 0000000..2f058a3 --- /dev/null +++ b/src/modules/main/vga.c @@ -0,0 +1,54 @@ +#include "vga.h" + +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 25; + +static size_t terminal_row; +static size_t terminal_column; +static uint8_t terminal_color; + +/* Note the use of the volatile keyword to prevent the compiler from eliminating dead stores. */ +static volatile uint16_t* terminal_buffer; + +uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg); +uint16_t vga_entry(unsigned char uc, uint8_t color); + +static size_t strlen(const char* str) { + size_t len = 0; + while (str[len++]); + return len; +} + +void terminal_initialize(size_t startrow) { + terminal_row = startrow; + terminal_column = 0; + terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); + terminal_buffer = (uint16_t*) 0xB8000; +} + +void terminal_setcolor(uint8_t color) { + terminal_color = color; +} + +void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { + const size_t index = y * VGA_WIDTH + x; + terminal_buffer[index] = vga_entry(c, color); +} + +void terminal_putchar(char c) { + terminal_putentryat(c, terminal_color, terminal_column, terminal_row); + if (++terminal_column == VGA_WIDTH) { + terminal_column = 0; + if (++terminal_row == VGA_HEIGHT) + terminal_row = 0; + } +} + +void terminal_write(const char* data, size_t size) { + for (size_t i = 0; i < size; i++) + terminal_putchar(data[i]); +} + +void terminal_writestring(const char* data) { + terminal_write(data, strlen(data)); +} diff --git a/src/modules/main/vga.h b/src/modules/main/vga.h new file mode 100644 index 0000000..4be8b71 --- /dev/null +++ b/src/modules/main/vga.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include + +/* Hardware text mode color constants. */ +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_WHITE = 15, +}; + +inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { + return fg | bg << 4; +} + +inline uint16_t vga_entry(unsigned char uc, uint8_t color) { + return (uint16_t) uc | (uint16_t) color << 8; +} + +void terminal_initialize(size_t rowstart); +void terminal_setcolor(uint8_t color); +void terminal_putentryat(char c, uint8_t color, size_t x, size_t y); +void terminal_putchar(char c); +void terminal_write(const char* data, size_t size); +void terminal_writestring(const char* data);