swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 1 | #include "paging.h" |
| 2 | #include "io.h" |
| 3 | #include "kint.h" |
| 4 | #include "log.h" |
| 5 | #include "pic.h" |
| 6 | |
| 7 | extern uint end; |
| 8 | static size_t alloc_base = (size_t)&end; |
| 9 | |
| 10 | /* frames bitset, 0 = free, 1 = used */ |
| 11 | static uint *frames; |
| 12 | static ulong num_frames; |
| 13 | |
| 14 | static uint first_page_table[1024] __attribute__((aligned(4096))); |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame^] | 15 | static uint page_directory[1024] __attribute__((aligned(4096))); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 16 | |
| 17 | void *_kmalloc(size_t size, bool align, void **phys) |
| 18 | { |
| 19 | if (align && (alloc_base & 0xfff)) // if not yet aligned |
| 20 | { |
| 21 | alloc_base &= ~0xfff; |
| 22 | alloc_base += 0x1000; |
| 23 | } |
| 24 | |
| 25 | if (phys) |
| 26 | { |
| 27 | *phys = (void *)alloc_base; |
| 28 | } |
| 29 | |
| 30 | size_t addr = alloc_base; |
| 31 | alloc_base += size; |
| 32 | return (void *)addr; |
| 33 | } |
| 34 | |
| 35 | void *kmalloc(size_t size) |
| 36 | { |
| 37 | return _kmalloc(size, false, NULL); |
| 38 | } |
| 39 | |
| 40 | void *kmalloc_a(size_t size) |
| 41 | { |
| 42 | return _kmalloc(size, true, NULL); |
| 43 | } |
| 44 | |
| 45 | void *kmalloc_ap(size_t size, void **p) |
| 46 | { |
| 47 | return _kmalloc(size, true, p); |
| 48 | } |
| 49 | |
| 50 | /* frame utils */ |
| 51 | |
| 52 | #define BITS 32 |
| 53 | |
| 54 | static void set_frame(size_t frame_addr) |
| 55 | { |
| 56 | uint frame = frame_addr / 0x1000; // page aligned |
| 57 | frames[frame / BITS] |= 1 << (frame % BITS); |
| 58 | } |
| 59 | |
| 60 | static bool test_frame(size_t frame_addr) |
| 61 | { |
| 62 | uint frame = frame_addr / 0x1000; // page aligned |
| 63 | return frames[frame / BITS] & 1 << (frame % BITS); |
| 64 | } |
| 65 | |
| 66 | static void clear_frame(size_t frame_addr) |
| 67 | { |
| 68 | uint frame = frame_addr / 0x1000; // page aligned |
| 69 | frames[frame / BITS] &= ~(1 << (frame % BITS)); |
| 70 | } |
| 71 | |
| 72 | static uint first_free_frame() |
| 73 | { |
| 74 | for (int i = 0; i < num_frames / BITS; i++) |
| 75 | { |
| 76 | /* |
| 77 | * If there are any zeroes, ~ will yield a non-zero result, |
| 78 | * meaning that there are pages free. Otherwise, check next set |
| 79 | */ |
| 80 | if (!~frames[i]) |
| 81 | continue; |
| 82 | |
| 83 | for (int j = 0; j < BITS; j++) |
| 84 | { |
| 85 | if ((frames[i] & 1 << j) == 0) |
| 86 | { |
| 87 | /* found unused frame */ |
| 88 | return i * BITS + j; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* did not find a free frame, panic */ |
| 94 | kpanic("first_free_frame failed! no free frames"); |
| 95 | } |
| 96 | |
| 97 | void alloc_frame(uint *page, bool for_kernel, bool writable) |
| 98 | { |
| 99 | if (*page >> 12) |
| 100 | return; /* frame already allocated */ |
| 101 | |
| 102 | uint frame = first_free_frame(); |
| 103 | // kprintf("first_free_frame found %d\n", frame); |
| 104 | set_frame(frame * 0x1000); /* mark as mapped */ |
| 105 | } |
| 106 | |
| 107 | void free_frame(uint page) |
| 108 | { |
| 109 | clear_frame(page / 0x1000); |
| 110 | } |
| 111 | |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame^] | 112 | void map_4mb(size_t virt_start, size_t phys_start, bool user, bool rw) |
| 113 | { |
| 114 | uint page = virt_start / 0x1000; |
| 115 | uint table = virt_start >> 22; |
| 116 | |
| 117 | for (uint i = 0; i < 1024 * 0x1000; i += 0x1000) |
| 118 | { |
| 119 | set_frame(page + i); |
| 120 | } |
| 121 | |
| 122 | page_directory[table] = 0b10000011; |
| 123 | } |
| 124 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 125 | /* paging stuff */ |
| 126 | |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame^] | 127 | void init_paging() |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 128 | { |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame^] | 129 | memset(page_directory, 0, 1024 * 4); |
| 130 | page_directory[KERNEL_PAGE_NUMBER] = 0b10000011; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 131 | |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame^] | 132 | load_page_directory((uint)page_directory - 0xC0000000); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 133 | add_interrupt_handler(14, page_fault); |
| 134 | } |
| 135 | |
| 136 | void page_fault(struct registers *regs) |
| 137 | { |
| 138 | kprintf("Page fault! eip = %d\n", regs->eip); |
| 139 | kpanic("Page fault"); |
| 140 | } |