From be3fb904d8d34c41353310191f76c9d151d72e53 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 19 Dec 2021 06:25:11 +0500 Subject: [PATCH] Add mapping func --- kernel/paging.c | 63 ++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/kernel/paging.c b/kernel/paging.c index 180573f..8dc80de 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -1,10 +1,11 @@ #include "paging.h" - #include "panic.h" #include #include +static void mapping(struct Paging *paging, uint32_t virt, uint32_t phys); + void paging_load(struct Paging *const paging) { uint32_t page_dir_phys = (uint32_t)&paging->page_dir; @@ -49,17 +50,8 @@ void paging_clear(struct Paging *const paging) void paging_identity(struct Paging *const paging) { for (size_t i = 0; i < PAGE_DIR_LENGTH; ++i) { - paging->page_dir[i].addr = PAGE_DIR_ADDR(i * PAGE_BIG_SIZE); - - paging->page_dir[i].available1 = 0; - paging->page_dir[i].page_size = 1; - paging->page_dir[i].available0 = 0; - paging->page_dir[i].accessed = 0; - paging->page_dir[i].cache_disabled = 1; - paging->page_dir[i].write_through = 1; - paging->page_dir[i].user = 1; - paging->page_dir[i].writable = 1; - paging->page_dir[i].present = 1; + const size_t addr = i * PAGE_BIG_SIZE; + mapping(paging, addr, addr); } } @@ -70,27 +62,40 @@ void paging_mapkernel( assert(!(kinfo->kernel_phys_base % PAGE_BIG_SIZE), "Kernel physical address is not aligned."); assert(!(kinfo->kernel_virt_base % PAGE_BIG_SIZE), "Kernel virtual address is not aligned."); - size_t pde = kinfo->kernel_virt_base / PAGE_BIG_SIZE; - + size_t phys = kinfo->kernel_phys_base; + size_t virt = kinfo->kernel_virt_base; size_t mapped = 0; - size_t kern_phys = kinfo->kernel_phys_base; while (mapped < kinfo->kernel_size) { - paging->page_dir[pde].addr = PAGE_DIR_ADDR(kern_phys); - - paging->page_dir[pde].available1 = 0; - paging->page_dir[pde].page_size = 1; - paging->page_dir[pde].available0 = 0; - paging->page_dir[pde].accessed = 0; - paging->page_dir[pde].cache_disabled = 0; - paging->page_dir[pde].write_through = 0; - paging->page_dir[pde].user = 0; - paging->page_dir[pde].writable = 1; - paging->page_dir[pde].present = 1; + mapping(paging, virt, phys); + phys += PAGE_BIG_SIZE; + virt += PAGE_BIG_SIZE; mapped += PAGE_BIG_SIZE; - kern_phys += PAGE_BIG_SIZE; - - ++pde; + } +} + +void mapping( + struct Paging *const paging, + const uint32_t virt, + const uint32_t phys +) { + KERNAUX_NOTNULL_RETURN(paging); + + const size_t pde_index = virt / PAGE_BIG_SIZE; + struct KernAux_Arch_I386_PDE *const pde = &paging->page_dir[pde_index]; + + if (!pde->present) { + pde->addr = PAGE_DIR_ADDR(phys); + + pde->available1 = 0; + pde->page_size = 1; + pde->available0 = 0; + pde->accessed = 0; + pde->cache_disabled = 0; + pde->write_through = 0; + pde->user = 0; + pde->writable = 1; + pde->present = 1; } }