LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
pmm.c
Go to the documentation of this file.
1
9
10#include <stddef.h>
11#include <mm/pmm.h>
12#include <limine/limine.h>
13#include <kernel/panic.h>
14
15#include "kernel/log.h"
16
17struct pmm_state state;
18
20 return state;
21}
22
23void pmm_init(const struct limine_memmap_response* memmap_response)
24{
25 // Get the highest usable address
26 uint64_t highest_address = 0;
27
28 for (int i = 0; i < memmap_response->entry_count; i++)
29 {
30 if (memmap_response->entries[i]->type != LIMINE_MEMMAP_USABLE) continue;
31
32 uint64_t end = memmap_response->entries[i]->base +
33 memmap_response->entries[i]->length;
34
35 if (end > highest_address)
36 highest_address = end;
37 }
38
39 if (highest_address == 0)
40 PANIC("No usable memory found");
41
42 state.frame_count = highest_address / FRAME_SIZE;
43 uint64_t bitmap_bytes = (state.frame_count + 7) / 8;
44 uint64_t bitmap_frames = (bitmap_bytes + (FRAME_SIZE - 1)) / FRAME_SIZE;
45
46 struct limine_memmap_entry *first_usable_entry = NULL;
47
48 // Find the first usable entry starting at or above 1 MiB and is at least bitmap_bytes long
49 for (int i = 0; i < memmap_response->entry_count; i++)
50 {
51 struct limine_memmap_entry *entry = memmap_response->entries[i];
52
53 if (entry->type != LIMINE_MEMMAP_USABLE || // Is not usable
54 entry->base < 0x100000 || // Is not above 1 MiB
55 entry->length < bitmap_bytes) // Is not at least bitmap_bytes long
56 continue; // Not suitable, continue searching
57
58 first_usable_entry = entry;
59 break;
60 }
61
62 if (first_usable_entry == NULL)
63 PANIC("No usable memory found");
64
65 uint64_t bitmap_phys_addr = first_usable_entry->base;
66 state.bitmap = (uint64_t *)(bitmap_phys_addr + get_hddm_offset());
67
68 for (uint64_t i = 0; i < bitmap_frames * (FRAME_SIZE / 8); i++)
69 {
70 state.bitmap[i] = 0xFFFFFFFFFFFFFFFF; // mark all frames as used (all bits = 1)
71 }
72
73 state.free_frames = 0;
74
75 for (uint64_t i = 0; i < memmap_response->entry_count; i++)
76 {
77 struct limine_memmap_entry *entry = memmap_response->entries[i];
78
79 if (entry->type != LIMINE_MEMMAP_USABLE)
80 continue; // Not suitable, continue
81
82 for (uint64_t j = 0; j < entry->length / FRAME_SIZE; j++)
83 {
84 if (entry->base + (j * FRAME_SIZE) < 0x100000) continue;
85
86 uint64_t frame = (entry->base + (j * FRAME_SIZE)) / FRAME_SIZE; // Get the frame number
87 state.bitmap[frame / 64] &= ~(1ULL << (frame % 64)); // clear the bit for the frame
88
89 state.free_frames++;
90 }
91 }
92
93
94 for (uint64_t i = 0; i < bitmap_frames; i++)
95 {
96 // bitmap_phys_addr + (i * FRAME_SIZE) = frame
97 // set its bit
98
99 uint64_t frame = (bitmap_phys_addr + (i * FRAME_SIZE)) / FRAME_SIZE; // Get the frame number
100 state.bitmap[frame / 64] |= 1ULL << (frame % 64); // set the bit for the frame
101
102 state.free_frames--;
103 }
104
105 state.next_hint = 0;
106
107
108 LOG_DEBUG("Total Memory: %lluMiB\n", state.frame_count * FRAME_SIZE / 1024 / 1024);
109
110}
111
112uint64_t pmm_alloc_frame(void)
113{
114 for (uint64_t i = 0; i < (state.frame_count + 63) / 64; i++)
115 {
116 if (state.next_hint == (state.frame_count + 63) / 64) state.next_hint = 0;
117
118 if (state.bitmap[state.next_hint] != 0xFFFFFFFFFFFFFFFF)
119 {
120 uint64_t bit = __builtin_ffsll(~state.bitmap[state.next_hint]);
121 state.bitmap[state.next_hint] |= 1ULL << (bit - 1);
122 state.free_frames--;
123 return (state.next_hint * 64 + (bit - 1)) * FRAME_SIZE;
124 }
125
126 state.next_hint++;
127
128 if (state.next_hint == (state.frame_count + 63) / 64) state.next_hint = 0;
129 }
130
131 return 0;
132}
133
134uint64_t pmm_alloc_contiguous(uint64_t count)
135{
136 if (count == 0) return 0;
137
138 uint64_t frames = 0;
139
140 uint64_t contiguous_hint = 0; // The hint that the contiguous frame starts at
141 uint64_t contiguous_bit = 0; // The bit that holds the first frame of the contiguous frames
142
143 uint64_t t_next_hint = state.next_hint;
144
145 for (uint64_t i = 0; i < (state.frame_count + 63) / 64; i++)
146 {
147 if (state.bitmap[t_next_hint] != 0xFFFFFFFFFFFFFFFF)
148 {
149 // Starting from the left, increase frames for every 0 bit, and reset it to 0 on every 1 bit
150 for (long bit = 0; bit < 64; bit++)
151 {
152 if (frames == 0) contiguous_hint = t_next_hint;
153
154 uint8_t bit_value = (state.bitmap[t_next_hint] >> bit) & 1;
155
156 if (bit_value == 1)
157 {
158 frames = 0;
159 } else
160 {
161 if (frames == 0) contiguous_bit = bit;
162 frames++;
163 if (frames >= count) break;
164 }
165 }
166
167 if (frames >= count)
168 {
169 long current_bit = contiguous_bit;
170 uint64_t current_hint = contiguous_hint;
171 for (uint64_t j = 0; j < count; j++)
172 {
173 if (current_bit >= 64)
174 {
175 current_bit = 0;
176 current_hint++;
177 }
178
179 state.bitmap[current_hint] |= 1ULL << (current_bit);
180
181 current_bit++;
182 }
183 state.free_frames -= count;
184 state.next_hint = t_next_hint;
185 return (contiguous_hint * 64 + (contiguous_bit)) * FRAME_SIZE;
186 }
187 } else
188 {
189 frames = 0;
190 }
191
192 t_next_hint++;
193
194 if (t_next_hint == (state.frame_count + 63) / 64)
195 {
196 t_next_hint = 0;
197 frames = 0;
198 }
199 }
200
201 return 0;
202}
203
204uint64_t pmm_alloc_zeroed(void)
205{
206 uint64_t phys_addr = pmm_alloc_frame();
207
208 if (phys_addr == 0)
209 return 0;
210
211 // Zero the frame
212 return (uint64_t) memset((void*)(phys_addr + get_hddm_offset()), 0, FRAME_SIZE) - get_hddm_offset();
213}
214
215uint64_t pmm_alloc_zeroed_contiguous(uint64_t count)
216{
217 uint64_t phys_addr = pmm_alloc_contiguous(count);
218
219 if (phys_addr == 0)
220 return 0;
221
222 // Zero the frame
223 return (uint64_t) memset((void*)(phys_addr + get_hddm_offset()), 0, FRAME_SIZE * count) - get_hddm_offset(); // Zero `count` frames
224}
225
226
227void pmm_free_frame(uint64_t physical_addr)
228{
229 if (physical_addr % FRAME_SIZE != 0)
230 PANIC("Invalid physical address: %lx", physical_addr);
231
232 uint64_t frame = physical_addr / FRAME_SIZE;
233
234 if (frame >= state.frame_count)
235 PANIC("Invalid frame number %lu. Address: %lx", frame, physical_addr);
236
237 uint64_t word_index = frame / 64;
238
239 if (((state.bitmap[word_index] >> (frame % 64)) & 1) == 1)
240 {
241 // Clear bit
242 state.bitmap[word_index] = ~(~state.bitmap[word_index] | (1ULL << (frame % 64)));
243 } else
244 {
245 PANIC("Double free: %lx", physical_addr);
246 }
247
248 state.free_frames++;
249
250 if (word_index < state.next_hint)
251 state.next_hint = word_index;
252}
void * memset(void *dest, int c, size_t n)
Fills n bytes of the destination with c.
Definition kstring.c:44
Logging utilities for the kernel.
Handler for kernel panics.
#define PANIC(...)
Calls panic and automatically fills in the parameters.
Definition panic.h:17
uint64_t pmm_alloc_frame(void)
Allocate a frame from the physical memory manager.
Definition pmm.c:112
uint64_t pmm_alloc_contiguous(uint64_t count)
Allocates count contiguous frames from the physical memory manager.
Definition pmm.c:134
void pmm_free_frame(uint64_t physical_addr)
Frees a frame from the physical memory manager.
Definition pmm.c:227
void pmm_init(const struct limine_memmap_response *memmap_response)
Initialize the physical memory manager.
Definition pmm.c:23
uint64_t pmm_alloc_zeroed_contiguous(uint64_t count)
Allocate and zero count contiguous frames from the physical memory manager.
Definition pmm.c:215
uint64_t pmm_alloc_zeroed(void)
Allocate and zero a frame from the physical memory manager.
Definition pmm.c:204
Physical memory manager.
struct pmm_state get_pmm_state(void)
Get the current pmm state.
Definition pmm.c:19
Definition limine.h:397
Holds the state of the physical memory manager and information about its memory.
Definition pmm.h:19
uint64_t get_hddm_offset(void)
Gets the offset for the HDDM.
Definition vars.c:18