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 --- 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 +- 6 files changed, 260 insertions(+), 208 deletions(-) create mode 100644 src/c/irq.c delete mode 100644 src/c/irq_handler.c create mode 100644 src/c/paging.c (limited to 'src/c') 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