diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2015-11-03 00:15:58 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2015-11-03 00:15:58 +0100 |
commit | a71fe5ed5d709ef0a3caefb744a99c575fb9dd29 (patch) | |
tree | 9a33e76614e7f51a4c255d8b59068e1dd289b35c | |
download | jeyzuos-a71fe5ed5d709ef0a3caefb744a99c575fb9dd29.zip jeyzuos-a71fe5ed5d709ef0a3caefb744a99c575fb9dd29.tar.gz |
initial commit
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | Makefile | 27 | ||||
-rw-r--r-- | grub/grub.cfg | 5 | ||||
-rw-r--r-- | kernel/Makefile | 16 | ||||
-rw-r--r-- | kernel/entry.S | 42 | ||||
-rw-r--r-- | kernel/entry.asm | 43 | ||||
-rw-r--r-- | kernel/kernel.c | 114 | ||||
-rw-r--r-- | kernel/linker.ld | 46 |
8 files changed, 298 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e80844c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*~ +*.o +*.elf +*.iso +iso/* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0f7ed29 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +GRUB_DIR=grub +KERNEL_DIR=kernel +DST=iso +ISO=kernel.iso + +all: run + +run: kernel.elf + qemu-system-i386 -kernel $(KERNEL_DIR)/kernel.elf + +run-iso: create_iso + qemu-system-i386 -hda $(ISO) + +create_iso: cpy + grub-mkrescue -o $(ISO) $(DST) + +cpy: kernel.elf + mkdir -p $(DST)/boot/grub + cp $(KERNEL_DIR)/kernel.elf $(DST)/boot/ + cp $(GRUB_DIR)/grub.cfg $(DST)/boot/grub/ + +kernel.elf: + cd $(KERNEL_DIR); $(MAKE) + +clean: + cd $(KERNEL_DIR); $(MAKE) clean + -rm -fr $(ISO) $(DST) 2>/dev/null diff --git a/grub/grub.cfg b/grub/grub.cfg new file mode 100644 index 0000000..2cfb7b1 --- /dev/null +++ b/grub/grub.cfg @@ -0,0 +1,5 @@ +set timeout=0 + +menuentry "Jeyzu_OS" { + multiboot /boot/kernel.elf +} diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 0000000..cc03386 --- /dev/null +++ b/kernel/Makefile @@ -0,0 +1,16 @@ +all: kernel.elf + +kernel.elf: kernel.o entry.o + ld -Tlinker.ld -melf_i386 entry.o kernel.o -o kernel.elf + +kernel.o: kernel.c + gcc -std=gnu99 -m32 -fno-builtin -ffreestanding -nostdlib -Wall -Wextra -c kernel.c + +# entry.o: entry.S +# as -32 $< -o entry.o + +entry.o: entry.asm + nasm -f elf32 $< + +clean: + rm -f *.o kernel.elf diff --git a/kernel/entry.S b/kernel/entry.S new file mode 100644 index 0000000..5b16b30 --- /dev/null +++ b/kernel/entry.S @@ -0,0 +1,42 @@ +# declare some constants +.set ALIGN, 1<<0 # align loaded modules on page boundaries +.set MEMINFO, 1<<1 # provide memory map +.set FLAGS, ALIGN | MEMINFO # this is the Multiboot 'flag' field +.set MAGIC, 0x1BADB002 # 'magic number' lets bootloader find the header +.set CHECKSUM, -(MAGIC + FLAGS) # checksum of above, to prove we are multiboot + +# the bootloader will search for this magic sequence +# and recognize us as a multiboot kernel +.section .multiboot +.align 4 +.long MAGIC +.long FLAGS +.long CHECKSUM + +# allocatable, writable, contains no data +.section .bootstrap_stack, "aw", @nobits +stack_bottom: +.skip 16384 # 16 KiB +stack_top: + +# the linker script specifies _start as the entry point to the kernel and the +# bootloader will jump to this position once the kernel has been loaded +.section .text +.global _start +.type _start, @function +_start: + movl $stack_top, %esp + push %eax + push %ebx + + call kernel_main + + cli + +hang: + hlt + jmp hang + +# Set the size of the _start symbol to the current location '.' minus its start. +# This is useful when debugging or when you implement call tracing. +.size _start, . - _start diff --git a/kernel/entry.asm b/kernel/entry.asm new file mode 100644 index 0000000..28e491a --- /dev/null +++ b/kernel/entry.asm @@ -0,0 +1,43 @@ +global _start +global stack_ptr + +extern kernel_main + +; declare some constants +MODULEALIGN equ 1<<0 ; align loaded modules on page boundaries +MEMINFO equ 1<<1 ; provide memory map +FLAGS equ MODULEALIGN | MEMINFO ; this is the Multiboot 'flag' field +MAGIC equ 0x1BADB002 ; 'magic number' lets bootloader find the header +CHECKSUM equ -(MAGIC + FLAGS) ; checksum of above, to prove we are multiboot + +; the bootloader will search for this magic sequence +; and recognize us as a multiboot kernel +section .multiboot +align 4 +MultiBootHeader: + dd MAGIC + dd FLAGS + dd CHECKSUM + +section .code +; setup the stack +STACKSIZE equ 0x16384 + +_start: + mov esp, stack+STACKSIZE + push eax + push ebx + + call kernel_main + + cli + +hang: + hlt + jmp hang + +section .bss +align 4 +stack: + resb STACKSIZE +stack_ptr: diff --git a/kernel/kernel.c b/kernel/kernel.c new file mode 100644 index 0000000..5054231 --- /dev/null +++ b/kernel/kernel.c @@ -0,0 +1,114 @@ +#if !defined(__cplusplus) +#include <stdbool.h> /* C doesn't have booleans by default. */ +#endif +#include <stddef.h> +#include <stdint.h> + +/* Check if the compiler thinks we are targeting the wrong operating system. */ +#if defined(__linux__) +/* TODO how does this check for a cross compiler? It was failing. */ +/*#error "You are not using a cross-compiler, you will most certainly run into trouble"*/ +#endif + +/* This tutorial will only work for the 32-bit ix86 targets. */ +#if !defined(__i386__) +#error "This tutorial needs to be compiled with a ix86-elf compiler" +#endif + +/* Hardware text mode color constants. */ +enum vga_color { + COLOR_BLACK = 0, + COLOR_BLUE = 1, + COLOR_GREEN = 2, + COLOR_CYAN = 3, + COLOR_RED = 4, + COLOR_MAGENTA = 5, + COLOR_BROWN = 6, + COLOR_LIGHT_GREY = 7, + COLOR_DARK_GREY = 8, + COLOR_LIGHT_BLUE = 9, + COLOR_LIGHT_GREEN = 10, + COLOR_LIGHT_CYAN = 11, + COLOR_LIGHT_RED = 12, + COLOR_LIGHT_MAGENTA = 13, + COLOR_LIGHT_BROWN = 14, + COLOR_WHITE = 15, +}; + +static uint8_t make_color(enum vga_color fg, enum vga_color bg) +{ + return fg | bg << 4; +} + +static uint16_t make_vgaentry(char c, uint8_t color) +{ + uint16_t c16 = c; + uint16_t color16 = color; + return c16 | color16 << 8; +} + +/* static size_t strlen(const char* str) */ +/* { */ +/* size_t ret = 0; */ +/* for (const char* p = str; *p != 0; p++, ret++); */ +/* return ret; */ +/* } */ + +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 25; + +size_t terminal_row; +size_t terminal_column; +uint8_t terminal_color; +uint16_t* terminal_buffer; + +static void terminal_initialize() +{ + terminal_row = 0; + terminal_column = 0; + terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK); + terminal_buffer = (uint16_t*) 0xB8000; + for (size_t y = 0; y < VGA_HEIGHT; y++) { + for (size_t x = 0; x < VGA_WIDTH; x++) { + const size_t index = y * VGA_WIDTH + x; + terminal_buffer[index] = make_vgaentry(' ', terminal_color); + } + } +} + +/* static void terminal_setcolor(uint8_t color) */ +/* { */ +/* terminal_color = color; */ +/* } */ + +static 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] = make_vgaentry(c, color); +} + +static 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; + } + } +} + +static void terminal_writestring(const char* data) +{ + for (const char* p = data; *p != 0; p++) + terminal_putchar(*p); +} + +#if defined(__cplusplus) +extern "C" /* Use C linkage for kernel_main. */ +#endif +void kernel_main() +{ + terminal_initialize(); + terminal_writestring("Hello, kernel World!\n"); +} diff --git a/kernel/linker.ld b/kernel/linker.ld new file mode 100644 index 0000000..3f1fe9d --- /dev/null +++ b/kernel/linker.ld @@ -0,0 +1,46 @@ +/* the entry point */ +ENTRY(_start) + +SECTIONS +{ + /* all sections located above 1MB */ + . = 0x100000; + + /* multiboot header first (needs to reside within the first 8KB), + * then the code itself + */ + .text BLOCK(4K) : ALIGN(4K) + { + *(.multiboot) + *(.code) + } + + /* to be sure that this kernel is identified as a multiboot one, + * the GNU notes information should be placed inside the first 8K. + * another option is to exclude this unqiue header with -Wl,--build-id=none + */ + .note.gnu.build-id BLOCK(4K) : ALIGN(4K) + { + *(.note.gnu.build-id) + } + + /* read-only data */ + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata*) + } + + /* read-write data (initialized) */ + .data BLOCK(4K) : ALIGN(4K) + { + *(.data*) + } + + /* read-write data (uninitialized) and stack */ + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + *(.bootstrap_stack) + } +} |