blob: fb92767c14ec1ded95a7a491da3d00b992d878df (
plain) (
blame)
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
|
#include <mm/pmm.h>
#include <kernel.h>
#include <stdio.h>
#define BIT_WIDTH 64
Bitmap* head;
void pmm_init(limine_memmap_response* memmap) {
Bitmap* current = nullptr;
Bitmap* tail = nullptr;
bool first = true;
for (size_t i = 0; i < memmap->entry_count; i++) {
if (memmap->entries[i]->type == LIMINE_MEMMAP_USABLE) {
// sets bitmap struct pointer address
current = (Bitmap*)(memmap->entries[i]->base + _hhdm_offset);
// sets up bitmap
current->address = (uint64_t*)(memmap->entries[i]->base + sizeof(Bitmap) + _hhdm_offset);
current->limit = memmap->entries[i]->length;
// almost always will be less pages than the full space
// since it doesn't account for remainders (yet)
// length / page size / bit width
current->length = memmap->entries[i]->length / 0x1000uL / BIT_WIDTH;
if (current->length < 1) {
continue;
}
// printf("len %lx\n", current->length);
// allocate memory for the bitmaps themselves
// printf("test %lx\n", current->limit / 0x40000uL / BIT_WIDTH);
for (size_t i = 0; i < (current->limit / 0x1000uL / BIT_WIDTH / BIT_WIDTH) + 1; i++) {
for (size_t j = 0; j < BIT_WIDTH; j++) {
// shifts bits off the right and masks the rest out to check for a 0
if (!((current->address[i] >> j) & 0x1uL)) {
current->address[i] |= 0x1uL << j;
// printf("init alloc: %lx\n", ((i * BIT_WIDTH) + j) * 0x1000uL + (uint64_t)current - _hhdm_offset);
break;
}
}
}
if (first) {
current->next = nullptr;
tail = current;
head = current;
first = false;
}
else {
tail->next = current;
tail = current;
}
}
}
tail->next = nullptr;
printf("Initialized PMM\n");
}
uint64_t palloc() {
Bitmap* current = head;
while (true) {
for (size_t i = 0; i < current->length; i++) {
//if ((current->address[i] & UINT64_MAX) != UINT64_MAX) {
for (size_t j = 0; j < BIT_WIDTH; j++) {
// shifts bits off the right and masks the rest out to check for a 0
if (!((current->address[i] >> j) & 0x1uL)) {
current->address[i] |= 0x1uL << j;
// printf("addr: %lx\n", current + j);
return ((i * BIT_WIDTH) + j) * 0x1000uL + (uint64_t)current - _hhdm_offset;
}
}
//}
}
if (current->next == nullptr) {
return NULL;
}
current = current->next;
}
}
void pfree(uint64_t page) {
Bitmap* current = head;
int offset;
while (true) {
if (page + _hhdm_offset >= (uint64_t)current) {
if (page + _hhdm_offset <= current->limit + (uint64_t)current) {
break;
}
}
current = current->next;
}
// Gets the starting number of the mem region page to get the correct bit number
// start 4D000, freeing 4E000, offset D - E = 1 = bit in bitmap
offset = ((uint64_t)current / 0x1000uL) & 0xFuL;
// divided by the amount of address space each index can hold
// should probably put that in its own variable/define later
uint64_t index = (page - ((uint64_t)current - _hhdm_offset)) / 0x40000uL;
int bit = page / 0x1000uL % BIT_WIDTH - offset;
// printf("before: %lx, ", current->address[index]);
current->address[index] &= ~(0x1uL << bit);
// printf("after: %lx\n", current->address[index]);
}
|