diff --git a/Makefile b/Makefile index 2d37d4b..33d07ef 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ override MAKEFLAGS += -rR override KERNEL := kernel.elf override CC := x86_64-elf-gcc +override LD := x86_64-elf-ld # Convenience macro to reliably declare overridable command variables. define DEFAULT_VAR = @@ -14,14 +15,6 @@ define DEFAULT_VAR = endif endef -# It is highly recommended to use a custom built cross toolchain to build a kernel. -# We are only using "cc" as a placeholder here. It may work by using -# the host system's toolchain, but this is not guaranteed. -$(eval $(call DEFAULT_VAR,CC,cc)) - -# Same thing for "ld" (the linker). -$(eval $(call DEFAULT_VAR,LD,ld)) - # User controllable CFLAGS. CFLAGS ?= -g -O2 -pipe -Wall -Wextra @@ -74,18 +67,21 @@ all: $(KERNEL) # Link rules for the final kernel executable. $(KERNEL): $(OBJ) - $(LD) $(OBJ) $(LDFLAGS) -o $@ + @$(LD) $(OBJ) $(LDFLAGS) -o $@ + @echo " LD $@" # Include header dependencies. -include $(HEADER_DEPS) # Compilation rules for *.c files. %.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ + @$(CC) $(CFLAGS) -c $< -o $@ + @echo " CC $@" # Compilation rules for *.S files. %.o: %.S - $(CC) $(CFLAGS) -c $< -o $@ + @$(CC) $(CFLAGS) -c $< -o $@ + @echo " CC $@" # Remove object files and the final executable. .PHONY: clean @@ -99,7 +95,3 @@ distclean: clean .PHONY += run run: make -C .. run-hdd - -.PHONY += whichcc -whichcc: - echo "$(CC)" diff --git a/dev/serial.c b/dev/serial.c index 7542df9..460244d 100644 --- a/dev/serial.c +++ b/dev/serial.c @@ -1,5 +1,8 @@ +#include #include +#include + #define COM1 0x3F8 int serial_init() @@ -15,10 +18,12 @@ int serial_init() outb(COM1 | 0, 0xAE); /* Enable loopback mode */ if (inb(COM1 | 0) != 0xAE) { /* Loopback test */ + printf("Serial COM1 loopback test failed\n"); return 1; } outb(COM1 | 4, 0x0F); /* Enter normal operation */ + printf("Serial COM1 initialized\n"); return 0; } diff --git a/include/kernel/common.h b/include/kernel/common.h new file mode 100644 index 0000000..0d7379c --- /dev/null +++ b/include/kernel/common.h @@ -0,0 +1,10 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#include + +#include +#include +#include + +#endif /* _COMMON_H_ */ diff --git a/include/kernel/gdt.h b/include/kernel/gdt.h new file mode 100644 index 0000000..89434d0 --- /dev/null +++ b/include/kernel/gdt.h @@ -0,0 +1,37 @@ +#ifndef _GDT_H_ +#define _GDT_H_ + +#define GDT_NUMDESC 64 +#define GDT_SIZE (64 * GDT_NUMDESC) + +#define GDT_ACCESS_DATA_RING0 0b10010010 +#define GDT_ACCESS_CODE_RING0 0b10011010 + +#define GDT_ACCESS_DATA_RING3 0b11110010 +#define GDT_ACCESS_CODE_RING3 0b11111010 + +#ifndef __ASM__ + +#include + +struct gdt_desc { + uint16_t limit; + uint16_t base; + uint8_t base_2; + uint8_t access; + uint8_t flags; + uint8_t base_3; +}; + +struct gdtr { + uint16_t size; + uint64_t offset; +}; + +extern struct gdtr __gdtr; +extern struct gdt_desc __gdt[GDT_NUMDESC]; + +void gdt_init(); + +#endif /* __ASM__ */ +#endif /* _GDT_H_ */ diff --git a/include/kernel/idt.h b/include/kernel/idt.h new file mode 100644 index 0000000..82d80eb --- /dev/null +++ b/include/kernel/idt.h @@ -0,0 +1,34 @@ +#ifndef _IDT_H_ +#define _IDT_H_ + +#define GATE_INTR 0x8E +#define GATE_TRAP 0x8F + +#define IDT_GATE_SIZE 16 +#define IDT_NUM_VECTORS 256 +#define IDT_SIZE 32768 + +#ifndef __ASM__ + +#include + +struct irqdesc { + uint16_t offset_low; + uint16_t segment; + uint8_t ist; + uint8_t attrs; + uint16_t offset_mid; + uint32_t offset_high; + uint32_t reserved; +}; + +struct idtr { + uint16_t size; + uint64_t offset; +}; + +extern struct idtr __idtr; +extern struct irqdesc __idt[IDT_NUM_VECTORS]; + +#endif /* __ASM__ */ +#endif /* _IDT_H_ */ diff --git a/init/descriptor.S b/init/descriptor.S new file mode 100644 index 0000000..902457c --- /dev/null +++ b/init/descriptor.S @@ -0,0 +1,39 @@ +#define __ASM__ +#include +#include + +/* + * Data section + */ +.data + +.global __gdtr +__gdtr: + .word 0 + .quad 0 +.global __gdt +__gdt: + .skip GDT_SIZE + +.global __idtr +__idtr: + .word 0 + .quad 0 +.global __idt +__idt: + .skip IDT_SIZE + +/* + * Code Section + */ +.text + +.global gdt_load +gdt_load: + lgdt (__gdtr) + ret + +.global idt_load +idt_load: + lidt (__idtr) + ret diff --git a/init/gdt.c b/init/gdt.c new file mode 100644 index 0000000..3d927bd --- /dev/null +++ b/init/gdt.c @@ -0,0 +1,32 @@ +#include +#include + +#include + +void gdt_load(); + +void gdt_insert(uint16_t offset, uint8_t access, uint8_t flags) +{ + struct gdt_desc *desc = (__gdt + offset); + desc->limit = 0; + desc->base = 0; + desc->base_2 = 0; + desc->base_3 = 0; + + desc->access = access; + desc->flags = flags; +} + +void gdt_init() +{ + gdt_insert(0, 0, 0); + gdt_insert(1, GDT_ACCESS_CODE_RING0, 0xA0); + gdt_insert(2, GDT_ACCESS_DATA_RING0, 0xC0); + gdt_insert(3, GDT_ACCESS_CODE_RING3, 0xA0); + gdt_insert(4, GDT_ACCESS_DATA_RING3, 0xC0); + __gdtr.size = (sizeof(__gdt) - 1); + __gdtr.offset = (uint64_t)__gdt; + gdt_load(); + + printf("GDT initialized\n"); +} diff --git a/init/idt.c b/init/idt.c new file mode 100644 index 0000000..63a9538 --- /dev/null +++ b/init/idt.c @@ -0,0 +1,19 @@ +#include + +void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, + uint16_t segment, void *func) +{ + uintptr_t f = (uintptr_t)(func); + struct irqdesc *irqdesc = (__idt + gate); + irqdesc->offset_low = f & 0xFFFF; + irqdesc->offset_mid = ((f >> 0x10) & 0xFFFF); + irqdesc->offset_high = ((f >> 0x20) & 0xFFFFFFFF); + + irqdesc->segment = segment; + irqdesc->ist = ist; + irqdesc->attrs = type; +} + +void idt_init() +{ +} diff --git a/init/main.c b/init/main.c index 9abd1b8..0e0bfa7 100644 --- a/init/main.c +++ b/init/main.c @@ -1,9 +1,8 @@ -#include -#include -#include - +#include +#include #include -#include + +#include static void done(void) { @@ -14,10 +13,8 @@ static void done(void) void _start(void) { + gdt_init(); serial_init(); - - int test = printf("test %d", -304); - printf("\nExpect: 9, actual: %d\n", test); done(); } diff --git a/init/printf.c b/init/printf.c index b63f9a4..bf12627 100644 --- a/init/printf.c +++ b/init/printf.c @@ -23,7 +23,6 @@ int printf(const char *restrict fmt, ...) { int ret = 0; char c; - char vac; char *s; int i; @@ -69,9 +68,9 @@ int printf(const char *restrict fmt, ...) ret += printf_write(s); break; case 'c': - vac = va_arg(args, char); + i = va_arg(args, int); ret++; - printf_out(vac); + printf_out(i); } } else { printf_out(c); diff --git a/linker.ld b/linker.ld index 529a4dd..e7b4f86 100644 --- a/linker.ld +++ b/linker.ld @@ -9,49 +9,49 @@ ENTRY(_start) /* MMU permissions */ PHDRS { - text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ - rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ - data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ + text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ + rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ + data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ } SECTIONS { - /* We wanna be placed in the topmost 2GiB of the address space, for optimisations */ - /* and because that is what the Limine spec mandates. */ - /* Any address in this region will do, but often 0xffffffff80000000 is chosen as */ - /* that is the beginning of the region. */ - . = 0xffffffff80000000; + /* We wanna be placed in the topmost 2GiB of the address space, for optimisations */ + /* and because that is what the Limine spec mandates. */ + /* Any address in this region will do, but often 0xffffffff80000000 is chosen as */ + /* that is the beginning of the region. */ + . = 0xffffffff80000000; - .text : { - *(.text .text.*) - } :text + .text : { + *(.text .text.*) + } :text - /* Move to the next memory page for .rodata */ - . += CONSTANT(MAXPAGESIZE); + /* Move to the next memory page for .rodata */ + . += CONSTANT(MAXPAGESIZE); - .rodata : { - *(.rodata .rodata.*) - } :rodata + .rodata : { + *(.rodata .rodata.*) + } :rodata - /* Move to the next memory page for .data */ - . += CONSTANT(MAXPAGESIZE); + /* Move to the next memory page for .data */ + . += CONSTANT(MAXPAGESIZE); - .data : { - *(.data .data.*) - } :data + .data : { + *(.data .data.*) + } :data - /* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */ - /* unnecessary zeros will be written to the binary. */ - /* If you need, for example, .init_array and .fini_array, those should be placed */ - /* above this. */ - .bss : { - *(COMMON) - *(.bss .bss.*) - } :data + /* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */ + /* unnecessary zeros will be written to the binary. */ + /* If you need, for example, .init_array and .fini_array, those should be placed */ + /* above this. */ + .bss : { + *(COMMON) + *(.bss .bss.*) + } :data - /* Discard .note.* and .eh_frame since they may cause issues on some hosts. */ - /DISCARD/ : { - *(.eh_frame) - *(.note .note.*) - } + /* Discard .note.* and .eh_frame since they may cause issues on some hosts. */ + /DISCARD/ : { + *(.eh_frame) + *(.note .note.*) + } }