aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/memory/paging.c
diff options
context:
space:
mode:
authorMarvin Borner2020-06-17 18:31:46 +0200
committerMarvin Borner2020-06-17 18:31:46 +0200
commiteed77bd2970a00d1394ed027ceca5b646e4671ce (patch)
treec44643d98aed2b6818f2b33417c0dea9c5853094 /src/kernel/memory/paging.c
parent49dfa1f4021026bf7c4d77817959c8aa24067016 (diff)
Started rewrite
Diffstat (limited to 'src/kernel/memory/paging.c')
-rw-r--r--src/kernel/memory/paging.c117
1 files changed, 0 insertions, 117 deletions
diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c
deleted file mode 100644
index e2c48b8..0000000
--- a/src/kernel/memory/paging.c
+++ /dev/null
@@ -1,117 +0,0 @@
-#include <io/io.h>
-#include <lib/lib.h>
-#include <memory/alloc.h>
-#include <memory/mmap.h>
-#include <memory/paging.h>
-#include <stdint.h>
-#include <system.h>
-
-struct page_dir *kernel_page_directory;
-
-void paging_install()
-{
- memory_init();
-
- struct page_table *page_table;
-
- page_table = kmalloc_frames(1);
- memset(page_table, 0, sizeof(struct page_table));
- for (u32 i = 0; i < PAGE_COUNT; i++) {
- page_table->entries[i].present = 1;
- page_table->entries[i].writable = 1;
- page_table->entries[i].address = SHIFT(i * PAGE_SIZE);
- }
-
- kernel_page_directory = kmalloc_frames(1);
- memset(kernel_page_directory, 0, sizeof(struct page_dir));
- kernel_page_directory->entries[0].present = 1;
- kernel_page_directory->entries[0].writable = 1;
- kernel_page_directory->entries[0].address = SHIFT((u32)page_table);
-
- paging_switch_dir((u32)kernel_page_directory);
- paging_enable();
- info("Installed paging");
-
- // Test mallocing
- u32 *c = malloc(2048);
- c[42] = 0x4242;
- assert(c[42] == 0x4242);
- free(c);
- info("Malloc test succeeded!");
-}
-
-void paging_disable()
-{
- u32 cr0 = cr0_get();
- cr0 &= 0x7fffffff;
- cr0_set(cr0);
- paging_enabled = 0;
-}
-
-void paging_enable()
-{
- u32 cr0 = cr0_get();
- cr0 |= 0x80000000;
- cr0_set(cr0);
- paging_enabled = 1;
-}
-
-void paging_switch_dir(u32 dir)
-{
- cr3_set(dir);
-}
-
-struct page_table_entry *paging_get_page(u32 address, struct page_dir *page_dir)
-{
- struct page_table *page_table;
-
- address /= PAGE_SIZE;
- u32 n = address / PAGE_COUNT;
-
- if (page_dir->entries[n].present == 0) {
- page_table = kmalloc_frames(1);
- memset(page_table, 0, sizeof(struct page_table));
-
- page_dir->entries[n].address = SHIFT((u32)page_table);
- page_dir->entries[n].present = 1;
- page_dir->entries[n].writable = 1;
- page_dir->entries[n].user = 1;
- } else {
- page_table = (void *)UNSHIFT(page_dir->entries[n].address);
- }
-
- return &page_table->entries[address % PAGE_COUNT];
-}
-
-void paging_frame_alloc(struct page_table_entry *page)
-{
- void *ptr = kmalloc_frames(1);
-
- if (page->address != 0) {
- warn("Page is already allocated");
- return;
- }
- page->address = SHIFT((u32)ptr);
- page->present = 1;
- page->user = 1;
-}
-
-void paging_frame_free(struct page_table_entry *page)
-{
- kfree_frames((void *)UNSHIFT(page->address), 1);
- memset((void *)page, 0, sizeof(struct page_table_entry));
-}
-
-struct page_dir *paging_make_dir()
-{
- struct page_dir *ret = kmalloc_frames(1);
-
- memcpy(ret, kernel_page_directory, sizeof(struct page_dir));
-
- return ret;
-}
-
-void paging_free_dir(struct page_dir *page_dir)
-{
- kfree_frames(page_dir, 1);
-} \ No newline at end of file