From b44f6761f15a8b6c3a12c67324cc8d8054b2ed26 Mon Sep 17 00:00:00 2001 From: nigel Date: Sun, 2 May 2021 15:23:50 -0400 Subject: [PATCH] New directory structure --- Makefile | 52 +++++++++++++++++++ src/{ => kernel/arch/i386}/boot.s | 2 + src/kernel/arch/i386/crti.s | 14 +++++ src/kernel/arch/i386/crtn.s | 7 +++ src/{ => kernel/arch/i386}/linker.ld | 0 .../arch/i386/tty/kterm.c} | 42 +++------------ .../arch/i386/tty/kterm.h} | 18 +++---- .../arch/i386/vga/colors.h} | 1 + src/kernel/kernel.c | 27 ++++++++++ src/kernel/kernel.h | 3 ++ src/kterm.h | 11 ---- src/{ => libc/include}/string.h | 0 12 files changed, 122 insertions(+), 55 deletions(-) create mode 100644 Makefile rename src/{ => kernel/arch/i386}/boot.s (94%) create mode 100644 src/kernel/arch/i386/crti.s create mode 100644 src/kernel/arch/i386/crtn.s rename src/{ => kernel/arch/i386}/linker.ld (100%) rename src/{kernel.c => kernel/arch/i386/tty/kterm.c} (74%) rename src/{kernel.h => kernel/arch/i386/tty/kterm.h} (71%) rename src/{vga_colors.h => kernel/arch/i386/vga/colors.h} (96%) create mode 100644 src/kernel/kernel.c create mode 100644 src/kernel/kernel.h delete mode 100644 src/kterm.h rename src/{ => libc/include}/string.h (100%) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..655ab81 --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ + +EMULATOR = qemu-system-i386 +AS = ${HOME}/opt/cross/bin/i686-elf-as +CC = ${HOME}/opt/cross/bin/i686-elf-gcc +CFLAGS = -std=gnu99 -ffreestanding -O2 -Wall -Wextra + +OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o + +SRC_DIR = src +BUILD_DIR = build + +CRTBEGIN_OBJ = $(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o) +CRTEND_OBJ = $(shell $(CC) $(CFLAGS) -print-file-name=crtend.o) + +CRTI_OBJ = $(BUILD_DIR)/crti.o +CRTN_OBJ = $(BUILD_DIR)/crtn.o +OBJ_LINK_LIST = $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(OFILES) $(CRTEND_OBJ) $(CRTN_OBJ) +INTERNAL_OBJS = $(CRTI_OBJ) $(OFILES) $(CRTN_OBJ) + + +all: clean build + +build: build_kernel run + +run: + $(EMULATOR) -kernel $(BUILD_DIR)/myos.bin +build_kernel: $(OBJ_LINK_LIST) + + $(CC) -T $(SRC_DIR)/kernel/arch/i386/linker.ld -o $(BUILD_DIR)/myos.bin \ + -ffreestanding -O2 -nostdlib $(OBJ_LINK_LIST) -lgcc + +build_x86_64: + $(AS) $(SRC_DIR)/cgc/x86_64/crti.s -o $(BUILD_DIR)/crti_64.o + $(AS) $(SRC_DIR)/cgc/x86_64/crtn.s -o $(BUILD_DIR)/crtn.o + +clean: + rm -f $(BUILD_DIR)/myos.bin $(INTERNAL_OBJS) + +$(BUILD_DIR)/kernel.o: + $(CC) -c $(SRC_DIR)/kernel/kernel.c -o $(BUILD_DIR)/kernel.o $(CFLAGS) + +$(BUILD_DIR)/kterm.o: + $(CC) -c $(SRC_DIR)/kernel/arch/i386/tty/kterm.c -o $(BUILD_DIR)/kterm.o $(CFLAGS) + +$(BUILD_DIR)/boot.o: + $(AS) $(SRC_DIR)/kernel/arch/i386/boot.s -o $(BUILD_DIR)/boot.o + +$(BUILD_DIR)/crti.o: + $(AS) $(SRC_DIR)/kernel/arch/i386/crti.s -o $(BUILD_DIR)/crti.o + +$(BUILD_DIR)/crtn.o: + $(AS) $(SRC_DIR)/kernel/arch/i386/crtn.s -o $(BUILD_DIR)/crtn.o diff --git a/src/boot.s b/src/kernel/arch/i386/boot.s similarity index 94% rename from src/boot.s rename to src/kernel/arch/i386/boot.s index b72102a..fa5fce2 100644 --- a/src/boot.s +++ b/src/kernel/arch/i386/boot.s @@ -32,7 +32,9 @@ _start: + call _init call kernel_main + cli 1: hlt diff --git a/src/kernel/arch/i386/crti.s b/src/kernel/arch/i386/crti.s new file mode 100644 index 0000000..af9463d --- /dev/null +++ b/src/kernel/arch/i386/crti.s @@ -0,0 +1,14 @@ +.section .init +.global _init +.type _init, @function +_init: + push %ebp + movl %esp, %ebp + /* gcc will nicely put the contents of crtbegin.o's .init section here. */ + +.section .fini +.global _fini +.type _fini, @function +_fini: + push %ebp + movl %esp, %ebp \ No newline at end of file diff --git a/src/kernel/arch/i386/crtn.s b/src/kernel/arch/i386/crtn.s new file mode 100644 index 0000000..7717f30 --- /dev/null +++ b/src/kernel/arch/i386/crtn.s @@ -0,0 +1,7 @@ +.section .init + popl %ebp + ret + +.section .fini + popl %ebp + ret \ No newline at end of file diff --git a/src/linker.ld b/src/kernel/arch/i386/linker.ld similarity index 100% rename from src/linker.ld rename to src/kernel/arch/i386/linker.ld diff --git a/src/kernel.c b/src/kernel/arch/i386/tty/kterm.c similarity index 74% rename from src/kernel.c rename to src/kernel/arch/i386/tty/kterm.c index dbb9d26..8b3ec6b 100644 --- a/src/kernel.c +++ b/src/kernel/arch/i386/tty/kterm.c @@ -1,39 +1,14 @@ -#include "kernel.h" #include "kterm.h" +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 25; + +size_t kterm_row; +size_t kterm_column; +uint8_t kterm_color; +uint16_t* kterm_buffer; -/** - * simple delay function - **/ -void delay(int t){ - volatile int i,j; - for(i=0;i +#pragma once +#include #include #include - -#include "string.h" - -#define KernelTag "[Kernel]: " -#define AS_KERNEL() ( kterm_setcolor(VGA_COLOR_LIGHT_BLUE),\ - kterm_write(KernelTag, strlen(KernelTag)), \ - kterm_resetcolor()) +#include +#include "../vga/colors.h" void kterm_init(); @@ -20,5 +16,7 @@ void kterm_write(const char*, size_t); void kterm_writestring(const char*); void kterm_scrollup(); - - +#define KernelTag "[Kernel]: " +#define AS_KERNEL() ( kterm_setcolor(VGA_COLOR_LIGHT_BLUE),\ + kterm_write(KernelTag, 10 ), \ + kterm_resetcolor()) \ No newline at end of file diff --git a/src/vga_colors.h b/src/kernel/arch/i386/vga/colors.h similarity index 96% rename from src/vga_colors.h rename to src/kernel/arch/i386/vga/colors.h index 9c610a3..61e4d54 100644 --- a/src/vga_colors.h +++ b/src/kernel/arch/i386/vga/colors.h @@ -1,3 +1,4 @@ +#pragma once enum vga_color { VGA_COLOR_BLACK = 0, VGA_COLOR_BLUE = 1, diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c new file mode 100644 index 0000000..75efa04 --- /dev/null +++ b/src/kernel/kernel.c @@ -0,0 +1,27 @@ +#include "kernel.h" +/** + * simple delay function + **/ +void delay(int t){ + volatile int i,j; + for(i=0;i -#include -#include "vga_colors.h" - -static const size_t VGA_WIDTH = 80; -static const size_t VGA_HEIGHT = 25; - -size_t kterm_row; -size_t kterm_column; -uint8_t kterm_color; -uint16_t* kterm_buffer; diff --git a/src/string.h b/src/libc/include/string.h similarity index 100% rename from src/string.h rename to src/libc/include/string.h