-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaging.c
107 lines (86 loc) · 3.16 KB
/
paging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "paging.h"
#include "isr.h"
#include "string.h"
#include "memory.h"
#include "log.h"
extern directory_t boot_page_directory; // pre-allocated by nasm (also page aligned)
uint32_t frame_allocations[32767];
#define PAGE_FRAME_SIZE 4096
// addr must be page aligned
static bool test_frame(uint32_t addr) {
uint32_t frame = addr / PAGE_FRAME_SIZE;
uint32_t frame_word = frame / 32;
uint32_t bit_offset = frame % 32;
return (frame_allocations[frame_word] >> bit_offset) & 1;
}
// addr must be page aligned
static void set_frame(uint32_t addr) {
uint32_t frame = addr / PAGE_FRAME_SIZE;
uint32_t frame_word = frame / 32;
uint32_t bit_offset = frame % 32;
frame_allocations[frame_word] |= (1 << bit_offset);
}
// addr must be page aligned
static void clear_frame(uint32_t addr) {
uint32_t frame = addr / PAGE_FRAME_SIZE;
uint32_t frame_word = frame / 32;
uint32_t bit_offset = frame % 32;
frame_allocations[frame_word] &= ~(1 << bit_offset);
}
void handle_page_fault(registers_t regs) {
debug("Page fault!");
}
#define PAGE_READONLY 0
#define PAGE_READWRITE 1
#define PAGE_USER 1
#define PAGE_KERNEL 0
#define PAGE_SIZE_4KB 0
#define PAGE_SIZE_4MB 1
void map(directory_t *page_directory, uint32_t vaddr, uint32_t paddr) {
// debug("\nidentity mapping %x-%x\n", addr & 0xfffff000, (addr & 0xfffff000) + 0x1000 - 1);
uint32_t directory_offset = vaddr >> 22; // 31:22
uint32_t table_offset = (vaddr >> 12) & 0x3ff; // 21:12
// debug("\tdirectory offset: %x\n", directory_offset);
// debug("\ttable offset: %x\n", table_offset);
directory_t *directory = &page_directory[directory_offset/4];
page_t *table;
// debug("kernel page directory resides at %x\n", page_directory);
if (!directory->present) {
// debug("configuring new page directory entry with index %i at %x\n", directory_offset, directory);
directory->present = 1;
directory->rw = PAGE_READWRITE;
directory->us = PAGE_KERNEL;
directory->ps = PAGE_SIZE_4KB;
table = (page_t*)kmalloc_page(); // sizeof(page_t)*1024 == 0x1000
memset(table, 0, 0x1000);
directory->page_table = (uint32_t)table >> 12; // table base (20 high bits)
// debug("directory->page_table is now %x\n", directory->page_table << 12);
} else {
table = (page_t*)(directory->page_table << 12);
// debug("c directory->page_table already exists at %x\n", table);
}
page_t *page = &table[table_offset];
if (!page->present) {
// debug("configuring new page table entry at %x\n", page);
page->present = 1;
page->rw = PAGE_READWRITE;
page->us = PAGE_KERNEL;
}
page->page_frame = paddr >> 12; // frame base (20 high bits)
// debug("page %x is now mapped to frame %x\n", addr, page->page_frame << 12);
set_frame(paddr);
}
extern void load_page_directory(directory_t *directory);
extern void enable_paging();
void init_paging() {
info("initializing paging...");
// listen for page faults
register_interrupt_handler(14, handle_page_fault);
debug("boot_page_directory=%x", &boot_page_directory);
load_page_directory(&boot_page_directory);
enable_paging();
info("paging enabled, good luck.");
}