From 89f7f1b114c1bbea3ad62808bf3653e1d2337d1f Mon Sep 17 00:00:00 2001 From: Aleksa Vučković Date: Wed, 27 Oct 2021 11:44:12 +0200 Subject: Added paging --- Makefile | 23 +++--- src/as/Makefile | 2 +- src/as/paging.s | 21 ++++++ src/c/Makefile | 2 +- src/c/irq.c | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/c/irq_handler.c | 204 ---------------------------------------------------- src/c/kernel.c | 2 + src/c/paging.c | 51 +++++++++++++ src/c/tty.c | 5 +- 9 files changed, 295 insertions(+), 219 deletions(-) create mode 100644 src/as/paging.s create mode 100644 src/c/irq.c delete mode 100644 src/c/irq_handler.c create mode 100644 src/c/paging.c diff --git a/Makefile b/Makefile index dd02b46..e99a6a6 100644 --- a/Makefile +++ b/Makefile @@ -10,10 +10,12 @@ CP=cp QEMU=qemu-system-x86_64 SOURCE_DIR=src +BUILD_DIR=${CURDIR}/build +ISO_DIR=isodir AS_SOURCE_DIR=$(SOURCE_DIR)/as C_SOURCE_DIR=$(SOURCE_DIR)/c -export BUILD_DIR=${CURDIR}/build -ISO_DIR=isodir +export AS_OBJECT_DIR=$(BUILD_DIR)/as +export C_OBJECT_DIR=$(BUILD_DIR)/c TARGET=myos BINARY=$(BUILD_DIR)/$(TARGET).bin @@ -21,19 +23,19 @@ ISO=$(TARGET).iso CRTI_SOURCE=crti.s CRTN_SOURCE=crtn.s -AS_SOURCE=boot.s irq.s -C_SOURCES=gdt.c heap.c idt.c kernel.c keyboard.c keymap.c stdio.c string.c tty.c vga.c irq_handler.c stack_protector.c timer.c +AS_SOURCE=boot.s irq.s paging.s +C_SOURCES=gdt.c heap.c idt.c kernel.c keyboard.c keymap.c stdio.c string.c tty.c vga.c irq.c stack_protector.c timer.c paging.c C_SOURCE_FILES=$(patsubst %,$(C_SOURCE_DIR)/%,$(C_SOURCES)) -export C_OBJECTS=$(patsubst %,$(BUILD_DIR)/%,$(C_SOURCES:c=o)) +export C_OBJECTS=$(patsubst %,$(C_OBJECT_DIR)/%,$(C_SOURCES:c=o)) -AS_OBJECT=$(patsubst %,$(BUILD_DIR)/%,$(AS_SOURCE:s=o)) -CRTI_OBJECT=$(patsubst %,$(BUILD_DIR)/%,$(CRTI_SOURCE:s=o)) -CRTN_OBJECT=$(patsubst %,$(BUILD_DIR)/%,$(CRTN_SOURCE:s=o)) +AS_OBJECT=$(patsubst %,$(AS_OBJECT_DIR)/%,$(AS_SOURCE:s=o)) +CRTI_OBJECT=$(patsubst %,$(AS_OBJECT_DIR)/%,$(CRTI_SOURCE:s=o)) +CRTN_OBJECT=$(patsubst %,$(AS_OBJECT_DIR)/%,$(CRTN_SOURCE:s=o)) AS_SOURCES=$(AS_SOURCE) $(CRTI_SOURCE) $(CRTN_SOURCE) AS_SOURCE_FILES=$(patsubst %,$(AS_SOURCE_DIR)/%,$(AS_SOURCES)) -export AS_OBJECTS=$(patsubst %,$(BUILD_DIR)/%,$(AS_SOURCES:s=o)) +export AS_OBJECTS=$(patsubst %,$(AS_OBJECT_DIR)/%,$(AS_SOURCES:s=o)) CRTBEGIN_OBJ=$(shell $(CC) -print-file-name=crtbegin.o) CRTEND_OBJ=$(shell $(CC) -print-file-name=crtend.o) @@ -48,7 +50,8 @@ $(BINARY): $(OBJ) $(CC) -T $(SOURCE_DIR)/linker.ld -o $(BINARY) $(CFLAGS) -nostdlib -lgcc $(OBJ) compile: $(AS_SOURCE_FILES) $(C_SOURCE_FILES) - $(MKDIR) $(BUILD_DIR) + $(MKDIR) $(AS_OBJECT_DIR) + $(MKDIR) $(C_OBJECT_DIR) $(MAKE) --directory $(AS_SOURCE_DIR) $(MAKE) --directory $(C_SOURCE_DIR) $(MAKE) $(BINARY) diff --git a/src/as/Makefile b/src/as/Makefile index e2c476d..bd5664b 100644 --- a/src/as/Makefile +++ b/src/as/Makefile @@ -1,5 +1,5 @@ .PHONY: all all: $(AS_OBJECTS) -$(BUILD_DIR)/%.o: %.s +$(AS_OBJECT_DIR)/%.o: %.s $(AS) $< -o $@ diff --git a/src/as/paging.s b/src/as/paging.s new file mode 100644 index 0000000..2dd52d3 --- /dev/null +++ b/src/as/paging.s @@ -0,0 +1,21 @@ +.globl loadPageDirectory +loadPageDirectory: + push %ebp + mov %esp, %ebp + mov 8(%esp), %eax + mov %eax, %cr3 + mov %ebp, %esp + pop %ebp + ret + +.text +.globl enablePaging +enablePaging: + push %ebp + mov %esp, %ebp + mov %cr0, %eax + or $0x80000000, %eax + mov %eax, %cr0 + mov %ebp, %esp + pop %ebp + ret diff --git a/src/c/Makefile b/src/c/Makefile index 7c5d1af..9625009 100644 --- a/src/c/Makefile +++ b/src/c/Makefile @@ -1,5 +1,5 @@ .PHONY: all all: $(C_OBJECTS) -$(BUILD_DIR)/%.o: %.c +$(C_OBJECT_DIR)/%.o: %.c $(CC) -c $< -o $@ -std=gnu99 $(CFLAGS) diff --git a/src/c/irq.c b/src/c/irq.c new file mode 100644 index 0000000..6b80a52 --- /dev/null +++ b/src/c/irq.c @@ -0,0 +1,204 @@ +#include"../include/stdio.h" +#include"../include/asm.h" + +#define INTERRUPT_GATE_32 0x8e + +#define KERNEL_CODE 0x08 +#define KERNEL_DATA 0x10 + +#define PIC1_COMMAND_PORT 0x20 +#define PIC1_DATA_PORT 0x21 +#define PIC2_COMMAND_PORT 0xA0 +#define PIC2_DATA_PORT 0xA1 + +void irq0_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 0.\n"); +} + +void irq1_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 1.\n"); +} + +void irq2_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 2.\n"); +} + +void irq3_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 3.\n"); +} + +void irq4_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 4.\n"); +} + +void irq5_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 5.\n"); +} + +void irq6_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 6.\n"); +} + +void irq7_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 7.\n"); +} + +void irq8_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 8.\n"); +} + +void irq9_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 9.\n"); +} + +void irq10_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 10.\n"); +} + +void irq11_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 11.\n"); +} + +void irq12_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 12.\n"); +} + +void irq13_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 13.\n"); +} + +void irq14_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 14.\n"); +} + +void irq15_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 15.\n"); +} + +void irq16_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 16.\n"); +} + +void irq17_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 17.\n"); +} + +void irq18_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 18.\n"); +} + +void irq19_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 19.\n"); +} + +void irq20_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 20.\n"); +} + +void irq21_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 21.\n"); +} + +void irq22_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 22.\n"); +} + +void irq23_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 23.\n"); +} + +void irq24_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 24.\n"); +} + +void irq25_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 25.\n"); +} + +void irq26_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 26.\n"); +} + +void irq27_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 27.\n"); +} + +void irq28_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 28.\n"); +} + +void irq29_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 29.\n"); +} + +void irq30_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 30.\n"); +} + +void irq31_handler() +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 31.\n"); +} diff --git a/src/c/irq_handler.c b/src/c/irq_handler.c deleted file mode 100644 index 6b80a52..0000000 --- a/src/c/irq_handler.c +++ /dev/null @@ -1,204 +0,0 @@ -#include"../include/stdio.h" -#include"../include/asm.h" - -#define INTERRUPT_GATE_32 0x8e - -#define KERNEL_CODE 0x08 -#define KERNEL_DATA 0x10 - -#define PIC1_COMMAND_PORT 0x20 -#define PIC1_DATA_PORT 0x21 -#define PIC2_COMMAND_PORT 0xA0 -#define PIC2_DATA_PORT 0xA1 - -void irq0_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 0.\n"); -} - -void irq1_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 1.\n"); -} - -void irq2_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 2.\n"); -} - -void irq3_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 3.\n"); -} - -void irq4_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 4.\n"); -} - -void irq5_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 5.\n"); -} - -void irq6_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 6.\n"); -} - -void irq7_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 7.\n"); -} - -void irq8_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 8.\n"); -} - -void irq9_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 9.\n"); -} - -void irq10_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 10.\n"); -} - -void irq11_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 11.\n"); -} - -void irq12_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 12.\n"); -} - -void irq13_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 13.\n"); -} - -void irq14_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 14.\n"); -} - -void irq15_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 15.\n"); -} - -void irq16_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 16.\n"); -} - -void irq17_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 17.\n"); -} - -void irq18_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 18.\n"); -} - -void irq19_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 19.\n"); -} - -void irq20_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 20.\n"); -} - -void irq21_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 21.\n"); -} - -void irq22_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 22.\n"); -} - -void irq23_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 23.\n"); -} - -void irq24_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 24.\n"); -} - -void irq25_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 25.\n"); -} - -void irq26_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 26.\n"); -} - -void irq27_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 27.\n"); -} - -void irq28_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 28.\n"); -} - -void irq29_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 29.\n"); -} - -void irq30_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 30.\n"); -} - -void irq31_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 31.\n"); -} diff --git a/src/c/kernel.c b/src/c/kernel.c index 62f444c..48dbccb 100644 --- a/src/c/kernel.c +++ b/src/c/kernel.c @@ -5,10 +5,12 @@ void init_idt_table(void); void init_keyboard(void); void init_timer(uint32_t frequency); void prompt(void); +void set_paging(); void kernel_main(void) { terminal_initialize(); + set_paging(); init_idt_table(); init_keyboard(); init_timer(50); diff --git a/src/c/paging.c b/src/c/paging.c new file mode 100644 index 0000000..b256b74 --- /dev/null +++ b/src/c/paging.c @@ -0,0 +1,51 @@ +#include"../include/types.h" + +extern void loadPageDirectory(uint32_t*); +extern void enablePaging(); + +uint32_t page_directory[1024] __attribute__((aligned(4096))); + +void set_pd() +{ + //set each entry to not present + for(size_t i=0;i<1024;i++) + { + // This sets the following flags to the pages: + // Supervisor: Only kernel-mode can access them + // Write Enabled: It can be both read from and written to + // Not Present: The page table is not present + page_directory[i] = 0x00000002; + } +} + +uint32_t first_page_table[1024] __attribute__((aligned(4096))); + +void set_pt() +{ + // holds the physical address where we want to start mapping these pages to. + // in this case, we want to map these pages to the very beginning of memory. + unsigned int i; + + //we will fill all 1024 entries in the table, mapping 4 megabytes + for(i = 0; i < 1024; i++) + { + // As the address is page aligned, it will always leave 12 bits zeroed. + // Those bits are used by the attributes ;) + first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present. + } +} + +void put_pt() +{ + // attributes: supervisor level, read/write, present + page_directory[0] = ((uint32_t)first_page_table) | 3; +} + +void set_paging() +{ + set_pd(); + set_pt(); + put_pt(); + loadPageDirectory(page_directory); + enablePaging(); +} diff --git a/src/c/tty.c b/src/c/tty.c index 116a15e..c473fa1 100644 --- a/src/c/tty.c +++ b/src/c/tty.c @@ -110,9 +110,8 @@ void neofetch() for(size_t i=0;i<16;i++) { - if(i==0) set_color(i,VGA_COLOR_WHITE); - else set_color(i,VGA_COLOR_BLACK); - printf("@%d ",i); + set_color(0,i); + printf(" ",i); } printf("\n"); -- cgit v1.2.3