This repository has been archived on 2024-06-25. You can view files and clone it, but cannot push or open issues or pull requests.
rockOS/kernel/rockos/paging.c

41 lines
1.1 KiB
C
Raw Normal View History

2022-07-20 02:23:23 +03:00
#include <rockos/paging.h>
#include <stdint.h>
2022-07-21 12:20:35 +03:00
void load_page_dir(uint32_t* page_dir) {
2022-07-20 02:23:23 +03:00
asm volatile (
"mov %%eax, %%cr3"
:
: "a"(page_dir)
: "memory"
);
}
2022-07-18 22:37:43 +03:00
2022-07-20 02:23:23 +03:00
void enable_paging() {
asm volatile (
"mov %cr0, %eax\n"
"or $0x80000000, %eax\n"
"mov %eax, %cr0"
);
}
2022-07-18 22:37:43 +03:00
void paging_init() {
uint32_t page_dir[PAGE_ENTRIES] __attribute__((aligned(PAGE_SIZE)));
for(int i = 0; i < PAGE_ENTRIES; i++) {
// Supervisor: only kernel-mode can access
// Write Enabled: Can be both read and written to
// Not Present: Page table not present
page_dir[i] = 0x00000002;
}
uint32_t first_page_table[PAGE_ENTRIES] __attribute__((aligned(PAGE_SIZE)));
for(unsigned int i = 0; i < PAGE_ENTRIES; i++) {
// As the address is page aligned, it will leave 12 bits zeroed,
// which are used by the attributes.
first_page_table[i] = (i * 0x1000) | 3; // attr: supervisor, r/w, present
}
page_dir[0] = ((unsigned int)first_page_table) | 3; //attr: supervisor, r/w, present
load_page_dir(page_dir);
enable_paging();
2022-07-20 02:23:23 +03:00
}