From 1734b485b52e5ee0d0130ee9e35f2a3bdebe713c Mon Sep 17 00:00:00 2001 From: "brice.boisson" Date: Wed, 6 Sep 2023 12:51:34 +0900 Subject: [PATCH] Add: Paging --- src/idt.c | 1 - src/kernel.c | 3 ++ src/paging.c | 95 ++++++++++++++++++++++++++++++++++++++++++---------- src/paging.h | 4 +-- 4 files changed, 82 insertions(+), 21 deletions(-) diff --git a/src/idt.c b/src/idt.c index 34433cc..9bac021 100644 --- a/src/idt.c +++ b/src/idt.c @@ -96,5 +96,4 @@ void init_idt(void) DEBUG_INFO("IDT LOADED"); DEBUG_INFO("ENABLING STI"); - asm volatile ("sti"); } diff --git a/src/kernel.c b/src/kernel.c index 542a26d..9460275 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -23,6 +23,7 @@ void main(void) void kernel_main(void) { + init_serial(); DEBUG_INFO("Starting kernel"); @@ -30,6 +31,8 @@ void kernel_main(void) init_gdt(); init_idt(); pic_init(); + make_page(); + asm volatile ("sti"); main(); } diff --git a/src/paging.c b/src/paging.c index dd728e4..0a4118e 100644 --- a/src/paging.c +++ b/src/paging.c @@ -1,7 +1,10 @@ #include "paging.h" -struct page_directory_entry page_dir[1024]; -struct page_table_entry page_table[1024]; +#include "debug.h" +#include "serial.h" + +struct page_directory_entry *page_dir = 0x2000; //__attribute__((aligned(4096))); +struct page_table_entry *page_table = 0x3000; //__attribute__((aligned(4096))); struct page_directory_param { u8 P; @@ -31,11 +34,18 @@ struct page_directory_entry create_page_directory_entry(struct page_directory_pa { struct page_directory_entry page_dir; - page_dir.flags = 2; + page_dir.flags = 0; + page_dir.flags |= (u8) param.P; + page_dir.flags |= (u8) (param.R_W << 1); + page_dir.flags |= (u8) (param.U << 2); + page_dir.flags |= (u8) (param.PWT << 3); + page_dir.flags |= (u8) (param.PCD << 4); + page_dir.flags |= (u8) (param.A << 5); + page_dir.flags |= (u8) (param.PS << 7); - page_dir.available = (u8) 10; + page_dir.available = (u8) 0; - page_dir.address = (u32) 24; + page_dir.address = (u32) (param.address >> 12); return page_dir; } @@ -44,35 +54,39 @@ struct page_table_entry create_page_table_entry(struct page_table_param param) { struct page_table_entry page_table; - page_table.flags = 2; + page_table.flags = 0; + page_table.flags = (u8) param.P; + page_table.flags |= (u8) (param.R_W << 1); + page_table.flags |= (u8) (param.U << 2); + page_table.flags |= (u8) (param.PWT << 3); + page_table.flags |= (u8) (param.PCD << 4); + page_table.flags |= (u8) (param.A << 5); + page_table.flags |= (u8) (param.D << 6); + page_table.flags |= (u8) (param.PAT << 7); - page_table.g = (u8) 10; + page_table.g = (u8) param.G; page_table.available = (u8) 0; - page_table.address = (u32) 24; + page_table.address = (u32) param.address; return page_table; } int make_page(void) { - page_table[0] = create_page_table_entry((struct page_table_param) { - .P = 1, .R_W = 1, .U = 1, .PWT = 1, .PCD = 1, - .A = 0, .D = 0, .PAT = 0, .G = 0, - .address = 0}); - - for (int i = 1; i < 1024; i++) + for (int i = 0; i < 1024; i++) { page_table[i] = create_page_table_entry((struct page_table_param) { - .P = 0, .R_W = 0, .U = 0, .PWT = 0, .PCD = 0, - .A = 0, .D = 0, .PAT = 0, .G = 0, - .address = 0}); + .P = 1, .R_W = 1, .U = 1, .PWT = 0, .PCD = 0, + .A = 0, .D = 0, .PAT = 0, .G = 0, + .address = 1 * i}); } page_dir[0] = create_page_directory_entry((struct page_directory_param) { - .P = 1, .R_W = 1, .U = 1, .PWT = 1, .PCD = 1, + .P = 1, .R_W = 1, .U = 1, .PWT = 0, .PCD = 0, .A = 0, .PS = 0, .address = page_table}); + for (int i = 1; i < 1024; i++) { page_dir[i] = create_page_directory_entry((struct page_directory_param) { @@ -80,5 +94,50 @@ int make_page(void) .A = 0, .PS = 0, .address = 0}); } + // #define DEBUG_PAGING + #ifdef DEBUG_PAGING + DEBUG_INFO("------ page_dir ------"); + for (int i = 0; i < 1024; i++) + { + int val = ((int *) page_dir)[i]; + if (i % 64 == 63) + { + write_serial_nb(val, false); + write_serial("\n"); + } + else + { + write_serial_nb(val, false); + write_serial(" "); + } + } + + DEBUG_INFO("------ page_table ------"); + for (int i = 0; i < 1024; i++) + { + u32 val = ((u32 *) page_table)[i]; + if (i % 64 == 63) + { + write_serial_nb(val, false); + write_serial("\n"); + } + else + { + write_serial_nb(val, false); + write_serial(" "); + } + } + #endif + + DEBUG_INFO("address of Page Directory Array: %d", page_dir); + DEBUG_INFO("address of the first Page Table array: %d", page_table); + + asm volatile (" \ + mov %0, %%eax \n \ + mov %%eax, %%cr3 \n \ + mov %%cr0, %%eax \n \ + or $0x80000000, %%eax \n \ + mov %%eax, %%cr0" : "=m" (page_dir)); + return 0; } diff --git a/src/paging.h b/src/paging.h index af9c942..f5ca0db 100644 --- a/src/paging.h +++ b/src/paging.h @@ -18,7 +18,7 @@ struct page_table_entry { int make_page(void); -extern struct page_directory_entry page_dir[1024]; -extern struct page_table_entry page_table[1024]; +// extern struct page_directory_entry page_dir[1024]; +// extern struct page_table_entry page_table[1024]; #endif /* !PAGING_H */