From 1a8a1ae9f1e6b78bd12e4b49dce06b68eeebe940 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Fri, 27 Sep 2019 15:18:59 +0900 Subject: [PATCH] Separate the architecture dependent and independent parts of the kernel more cleanly: - Add pg_dir_get_pdbr() function - Make definitions, types, and functions necessary for other parts of the kernel visible to them by moving them into the kernel/include/arch.h header --- kernel/arch/paging.c | 6 ++++++ kernel/arch/paging.h | 9 --------- kernel/core/process.c | 5 ++--- kernel/include/arch.h | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/kernel/arch/paging.c b/kernel/arch/paging.c index 222df81..bbe2c79 100644 --- a/kernel/arch/paging.c +++ b/kernel/arch/paging.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "cpu.h" #include "paging.h" #include "heap.h" @@ -878,3 +879,8 @@ static int _pg_dir_add_region(pg_dir_t *pd, void *base, u32_t size, return(ret_val); } + +void* pg_dir_get_pdbr(pg_dir_t *pd) +{ + return(pd->pd_base); +} diff --git a/kernel/arch/paging.h b/kernel/arch/paging.h index da4a6e4..248a01e 100644 --- a/kernel/arch/paging.h +++ b/kernel/arch/paging.h @@ -43,8 +43,6 @@ struct region { u32_t reg_flags; }; -typedef struct pagedir pg_dir_t; - struct pagedir { void *pd_base; u32_t pd_flags; @@ -96,11 +94,4 @@ void* pg_frame_alloc_start(void); void* pg_frame_alloc_end(void); void pg_frame_free(void*); -int pg_dir_create(pg_dir_t**); -int pg_dir_map(pg_dir_t*, const void*, const void*, const u32_t, const u32_t); -int pg_dir_unmap(pg_dir_t*, const void*, const u32_t); - -void* pg_dir_get_kstack(pg_dir_t*); -void* pg_dir_get_ustack(pg_dir_t*); - #endif /* __PAGING_H */ diff --git a/kernel/core/process.c b/kernel/core/process.c index 6b53f48..fb0b75c 100644 --- a/kernel/core/process.c +++ b/kernel/core/process.c @@ -1,11 +1,10 @@ +#include #include #include #include #include #include #include -#include "paging.h" -#include "cpu.h" struct process { struct pagedir *p_pgdir; @@ -32,7 +31,7 @@ int process_create(process_t **dst, u32_t ppl, void *entry) goto cleanup; } - pdbr = proc->p_pgdir->pd_base; + pdbr = pg_dir_get_pdbr(proc->p_pgdir); kstack = pg_dir_get_kstack(proc->p_pgdir); ustack = pg_dir_get_ustack(proc->p_pgdir); diff --git a/kernel/include/arch.h b/kernel/include/arch.h index dacad3c..e77593e 100644 --- a/kernel/include/arch.h +++ b/kernel/include/arch.h @@ -37,4 +37,41 @@ u32_t cpu_set_pstate(int pstate); int task_prepare(struct task*, u32_t cr3, u32_t eip, u32_t esp0, u32_t esp, u32_t priv); int task_switch(struct task*); +/* + * Definitions, types, and prototypes related to paging + */ + +#define ALIGNED(_v, _a) (!((_v) & (((_a) - 1)))) +#define ALIGN(_v, _a) (ALIGNED((_v), (_a)) ? (_v) : (((_v) & ~((_a) - 1)) + (_a))) + +#define PDPT_ALIGN 32 +#define PAGE_ALIGN 0x1000 +#define ARCH_ALIGN sizeof(void*) + +#define PAGE_SIZE 0x1000 +#define PAGE_SIZE_BIG 0x200000 +#define PAGE_SIZE_LARGE 0x400000 +#define PAGE_SIZE_HUGE 0x40000000 + +#define PAGE_ATTR_PRESENT (1 << 0) +#define PAGE_ATTR_WRITABLE (1 << 1) +#define PAGE_ATTR_USER (1 << 2) +#define PAGE_ATTR_WRITE_THRU (1 << 3) +#define PAGE_ATTR_CACHE_DISABLE (1 << 4) +#define PAGE_ATTR_ACCESSED (1 << 5) +#define PAGE_ATTR_DIRTY (1 << 6) +#define PAGE_ATTR_SIZE (1 << 7) +#define PAGE_ATTR_GLOBAL (1 << 8) +#define PAGE_ATTR_NO_EXEC 0 /* (1 << 63) */ + +typedef struct pagedir pg_dir_t; + +int pg_dir_create(pg_dir_t**); +int pg_dir_map(pg_dir_t*, const void*, const void*, const u32_t, const u32_t); +int pg_dir_unmap(pg_dir_t*, const void*, const u32_t); +void* pg_dir_get_pdbr(pg_dir_t*); + +void* pg_dir_get_kstack(pg_dir_t*); +void* pg_dir_get_ustack(pg_dir_t*); + #endif /* __ARCH_H */ -- 2.47.3