-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphys_mm.c
450 lines (370 loc) · 12.9 KB
/
phys_mm.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "phys_mm.h"
memory_info mem_info;
//! total number of blocks
static uint32_t total_blocks;
//! total number of used blocks
static uint32_t used_blocks;
//! bitmap pointer
static uint32_t* memory_map;
//! set block in bitmap
void phys_mm_set_block_bit(uint32_t bit)
{
memory_map[bit / 32] |= (1 << (bit % 32));
}
//! unset block in bitmap
void phys_mm_unset_block_bit(uint32_t bit)
{
memory_map[bit / 32] &= ~(1 << (bit % 32));
}
//! test block in bitmap
bool phys_mm_test_block_bit(uint32_t bit)
{
return memory_map[bit / 32] & (1 << (bit % 32));
}
//! find first free block in bitmap
uint32_t phys_mm_find_first_free()
{
// each block is 32 bits, so divide by 32 and iterate through 32 later
for (uint32_t i = 0; i < phys_mm_get_total_blocks() / 32; i++)
{
while (memory_map[i] != 0xFFFFFFFF)
{
// iterate through 32 bits (32 blocks)
for (uint32_t j = 0; j < 32; j++)
{
uint32_t bit = 1 << j;
//! test if bit is cleared
if (!(memory_map[i] & bit))
{
// return bit index
return i * 32 + j;
}
}
}
}
return 0;
}
//! find first free blocks of particular size in bitmap
uint32_t phys_mm_find_first_free_blocks(uint32_t size)
{
if (size == 0)
{
return 0;
}
if (size == 1)
{
return phys_mm_find_first_free();
}
// each block is 32 bits, so divide by 32 and iterate through 32 later
for (uint32_t i = 0; i < phys_mm_get_total_blocks() / 32; i++)
{
while (memory_map[i] != 0xFFFFFFFF)
{
// iterate through 32 bits (32 blocks)
for (uint32_t j = 0; j < 32; j++)
{
uint32_t bit = 1 << j;
//! test if bit is cleared
if (!(memory_map[i] & bit))
{
// starting bit index
uint32_t starting_bit_index = i * 32 + j;
uint32_t num_free_indices = 0;
for (uint32_t count = 0; count < size; count++)
{
if (!phys_mm_test_block_bit(starting_bit_index + count))
{
num_free_indices++;
}
if (num_free_indices == size)
{
return i * 32 + j;
}
}
}
}
}
}
return 0;
}
//find where the kernel ends and starts
void read_elf_sections()
{
elf_section_header_t* elf_header = (elf_section_header_t*)mem_info.elf_section_table->addr;
//go to next header
elf_header++;
//start of kernel
mem_info.kernel_start = elf_header->sh_addr;
mem_info.kernel_end = elf_header->sh_addr;
//update the end of kernel
for (uint32_t i = 0; i < mem_info.elf_section_table->num - 1; i++)
{
if (elf_header->sh_addr > mem_info.kernel_end)
{
mem_info.kernel_end = elf_header->sh_addr;
}
elf_header++;
}
//round to nearest page
mem_info.kernel_end = ROUND_PAGE_UP(mem_info.kernel_end);
}
//check if memory address is in multiboot memory map
MEMORY_STATE check_multiboot_info_memory_state(uint32_t address)
{
//get the memory maps
uintptr_t current_multiboot_memory_address = (uintptr_t)mem_info.multiboot_memory_map;
uintptr_t end_multiboot_memory_address = current_multiboot_memory_address + mem_info.memory_map_size;
while (current_multiboot_memory_address < end_multiboot_memory_address)
{
multiboot_memory_map_t* current_multiboot_memory_map = (multiboot_memory_map_t*)current_multiboot_memory_address;
//memory address region's start and end (where the memory the entry is defined)
uint32_t start_address = current_multiboot_memory_map->addr;
uint32_t end_address = start_address + current_multiboot_memory_map->len;
//check if memory is in range
if (address >= start_address && address <= end_address)
{
//check if is reserved memory
if (current_multiboot_memory_map->type != MULTIBOOT_MEMORY_AVAILABLE)
{
return MEMORY_RESERVED;
}
//check if address is within memory bounds
if (address + PAGE_SIZE > end_address)
{
return MEMORY_RESERVED;
}
return MEMORY_FREE;
}
current_multiboot_memory_address += current_multiboot_memory_map->size + sizeof(uintptr_t);
}
//memory not found in multiboot memory map
return MEMORY_INVALID;
}
//check if memory address is valid
MEMORY_STATE check_memory_state(uint32_t address)
{
//get to next page (current address + page size)
uint32_t address_end = address + PAGE_SIZE;
if (address <= 0x1000)
{
return MEMORY_RESERVED;
}
//check if address touches multiboot
if ((address >= mem_info.multiboot_info_start && address <= mem_info.multiboot_info_end) || (address_end >= mem_info.multiboot_info_start && address_end <= mem_info.multiboot_info_end))
{
return MEMORY_RESERVED;
}
//check if address touches kernel memory
if ((address >= mem_info.kernel_start && address <= mem_info.kernel_end) || (address_end >= mem_info.kernel_start && address_end <= mem_info.kernel_end))
{
return MEMORY_RESERVED;
}
//check if address touches multiboot info memory map
MEMORY_STATE multiboot_reserved = check_multiboot_info_memory_state(address);
if (multiboot_reserved != MEMORY_FREE)
{
return multiboot_reserved;
}
return MEMORY_FREE;
}
void mark_reserved_sections()
{
for (uint32_t address = 0; address < MAX_MEMORY_ADDRESS; address += PAGE_SIZE)
{
MEMORY_STATE state = check_memory_state(address);
if (state == MEMORY_RESERVED)
{
phys_mm_set_blocks(address, PAGE_SIZE);
}
}
}
//! setup the entire physical memory manager
void phys_mm_init(multiboot_info_t* boot_info)
{
//verify if multiboo has correct info
if (!(boot_info->flags & (1 << 6))) //presence of memory map (bit 6)
{
ABORT_MSG("Multiboot memory map could not be found");
}
if (!(boot_info->flags & (1 << 5))) //presence of elf (bit 5)
{
ABORT_MSG("ELF info could not be found");
}
mem_info.boot_info = boot_info;
mem_info.multiboot_memory_map = (multiboot_memory_map_t*)boot_info->mmap_addr;
mem_info.elf_section_table = &(boot_info->u.elf_sec);
mem_info.memory_map_size = boot_info->mmap_length;
mem_info.multiboot_info_start = (uint32_t)boot_info;
mem_info.multiboot_info_end = (uint32_t)((uintptr_t)boot_info + sizeof(multiboot_info_t));
//if bit 0 in the ‘flags’ word is set, then the ‘mem_*’ fields are valid. ‘mem_lower’ and ‘mem_upper’ indicate the amount of lower and upper memory, respectively, in kilobytes. Lower memory starts at address 0, and upper memory starts at address 1 megabyte. The maximum possible value for lower memory is 640 kilobytes. The value returned for upper memory is maximally the address of the first upper memory hole minus 1 megabyte. It is not guaranteed to be this value.
mem_info.memory_lower = boot_info->mem_lower;
mem_info.memory_upper = boot_info->mem_upper;
// set highest address
mem_info.highest_free_address = mem_info.memory_upper * 1024;
// read elf and update kernel start / end
read_elf_sections();
// start the heap after the kernel end
mem_info.heap_start = ROUND_PAGE_UP(mem_info.kernel_end);
// start at heap start
total_blocks = MAX_MEMORY_ADDRESS / PAGE_SIZE;
used_blocks = 0;
memory_map = (uint32_t*)mem_info.heap_start;
//free everything first
uint32_t bitset_size = (total_blocks + sizeof(uint32_t)) / sizeof(uint32_t);
memset(memory_map, 0x0, bitset_size);
// mark the memory map as used (waste, but covers entire memory range
phys_mm_set_blocks(memory_map, bitset_size);
mem_info.heap_start += bitset_size;
//mark reserved sections into the bitmap
mark_reserved_sections();
// init kernel region
phys_mm_set_blocks(mem_info.kernel_start, mem_info.kernel_end - mem_info.kernel_start);
}
//! marks a region as used
void phys_mm_set_blocks(uint32_t base, uint32_t size)
{
// transform base/size into bitmap's equalivant values
uint32_t base_bit = base / PAGE_SIZE;
uint32_t size_bit = size / PAGE_SIZE;
// mark blocks as used
for (uint32_t i = 0; i < size_bit; i++)
{
phys_mm_set_block_bit(base_bit++);
used_blocks++;
}
}
//! marks a region as free
void phys_mm_unset_blocks(uint32_t base, uint32_t size)
{
// transform base/size into bitmap's equalivant values
uint32_t base_bit = base / PAGE_SIZE;
uint32_t size_bit = size / PAGE_SIZE;
// mark blocks as free
for (uint32_t i = 0; i < size_bit; i++)
{
phys_mm_unset_block_bit(base_bit++);
used_blocks--;
}
}
//! returns an allocated region to be used
void* phys_mm_alloc()
{
// check if there any free blocks
if (phys_mm_get_free_blocks() <= 0)
{
return 0;
}
// find first free block
uint32_t block = phys_mm_find_first_free();
// check if we don't have any more free blocks
if (block == 0)
{
return 0;
}
// set the block to be "used" and update appropriate variables
phys_mm_set_block_bit(block);
used_blocks++;
// convert to actual address
uint32_t address = block * PAGE_SIZE;
return (void*)address;
}
//! free an allocated region
void phys_mm_free(void* region)
{
// convert actual address to bitmap equalivant
uint32_t block = ((uint32_t)region) / PAGE_SIZE;
// mark the region as free
phys_mm_unset_block_bit(block);
used_blocks--;
}
//! returns allocated regions to be used
void* phys_mm_allocs(uint32_t size)
{
// check if there any free blocks
if (phys_mm_get_free_blocks() <= 0)
{
return 0;
}
// find first free block(s)
uint32_t block = phys_mm_find_first_free_blocks(size);
// check if we don't have any more free blocks
if (block == 0)
{
return 0;
}
// set the block to be "used" and update appropriate variables
for (uint32_t i = 0; i < size; i++)
{
phys_mm_set_block_bit(block + i);
used_blocks++;
}
// convert to actual address
uint32_t address = block * PAGE_SIZE;
return (void*)address;
}
//! free allocated regions
void phys_mm_frees(void* region, uint32_t size)
{
// convert actual address to bitmap equalivant
uint32_t block = ((uint32_t)region) / PAGE_SIZE;
// mark the region as free
for (uint32_t i = 0; i < size; i++)
{
phys_mm_unset_block_bit(block + i);
used_blocks--;
}
}
//! get total number of blocks
uint32_t phys_mm_get_total_blocks()
{
return total_blocks;
}
//! get total number of used blocks
uint32_t phys_mm_get_used_blocks()
{
return used_blocks;
}
//! get total number of free blocks
uint32_t phys_mm_get_free_blocks()
{
return total_blocks - used_blocks;
}
//! disable/enable paging
void phys_mm_enable_paging(bool b);
//! test if paging is enabled
bool phys_mm_test_paging();
//! disable/enable pdbr
void phys_mm_enable_pdbr(bool b);
//! test if pdbr is enabled
bool phys_mm_test_pdbr();
static const char* MEMORY_STATE_STRING[] = {
"",
"MEMORY_AVALIABLE",
"MEMORY_RESERVED",
"MEMORY_ACPI_RECLAIMABLE",
"MEMORY_NVS",
"MEMORY_BADRAM"
};
//! debug physical memory
void phys_mm_debug()
{
vga_printf(
"pmm initialized with memLo: %x memHi: %x\n",
mem_info.memory_lower * KILOBYTE_SIZE, mem_info.memory_upper * KILOBYTE_SIZE);
//get the memory maps
uintptr_t current_multiboot_memory_address = (uintptr_t)mem_info.multiboot_memory_map;
uintptr_t end_multiboot_memory_address = current_multiboot_memory_address + mem_info.memory_map_size;
while (current_multiboot_memory_address < end_multiboot_memory_address)
{
multiboot_memory_map_t* current_multiboot_memory_map = (multiboot_memory_map_t*)current_multiboot_memory_address;
//memory address region's start and end (where the memory the entry is defined)
uint32_t start_address = current_multiboot_memory_map->addr;
uint32_t end_address = start_address + current_multiboot_memory_map->len;
vga_printf("Start: %x End: %x Type: %s\n", start_address, end_address, MEMORY_STATE_STRING[current_multiboot_memory_map->type]);
current_multiboot_memory_address += current_multiboot_memory_map->size + sizeof(uintptr_t);
}
vga_printf("\npmm regions initialized: %i allocation blocks; used or "
"reserved blocks: %i\nfree blocks: %i\n",
phys_mm_get_total_blocks(), phys_mm_get_used_blocks(),
phys_mm_get_free_blocks());
}