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
|
#include <cstdint>
#include <generic/pmm.hpp>
#include <generic/bootloader/bootloader.hpp>
#include <generic/lock/spinlock.hpp>
#include <generic/hhdm.hpp>
#include <klibc/string.hpp>
#include <klibc/stdio.hpp>
#include <utils/align.hpp>
#include <atomic>
locks::spinlock pmm_lock;
std::uint64_t* freelist_hhdm = 0;
std::size_t memory_size = 0;
buddy_t mem;
buddy_info_t* buddy_find_by_parent(buddy_info_t* blud,char split_x) {
if(split_x) {
for(std::uint64_t i = 0;i < mem.buddy_queue;i++) {
if(mem.mem[i].parent == blud && mem.mem[i].split_x)
return &mem.mem[i];
}
} else {
for(std::uint64_t i = 0;i < mem.buddy_queue;i++) {
if(mem.mem[i].parent == blud && !mem.mem[i].split_x)
return &mem.mem[i];
}
}
return nullptr;
}
buddy_info_t* buddy_find_by_phys(std::uint64_t phys) {
for(std::uint64_t i = 0;i < mem.buddy_queue;i++) {
if(mem.mem[i].phys == phys)
return &mem.mem[i];
}
return 0;
}
buddy_info_t* buddy_find_by_phys_without_split(std::uint64_t phys) {
for(std::uint64_t i = 0;i < mem.buddy_queue;i++) {
if(mem.mem[i].phys == phys && !mem.mem[i].is_splitted)
return &mem.mem[i];
}
return 0;
}
buddy_info_t* pmm::buddy::put(std::uint64_t phys, std::uint8_t level) {
buddy_info_t* blud = &mem.mem[mem.buddy_queue++];
blud->level = level;
blud->phys = phys;
blud->is_free = 1;
blud->is_splitted = 0;
blud->is_was_splitted = 0;
blud->parent = 0;
blud->split_x = 0;
return blud;
}
buddy_info_t* pmm::buddy::split_maximum(buddy_info_t* blud, std::uint64_t size) {
if((size <= LEVEL_TO_SIZE(blud->level) && LEVEL_TO_SIZE(blud->level - 1) < size) || LEVEL_TO_SIZE(blud->level) == 4096)
return blud;
return split_maximum(split(blud).first,size);
}
void pmm::buddy::merge(buddy_info_t* budy) {
buddy_info_t* bl = budy;
buddy_info_t* ud = budy->twin;
if(!bl || !ud || !bl->is_free || !ud->is_free)
return;
buddy_info_t* blud = budy->parent;
bl->is_free = 0;
ud->is_free = 0;
blud->is_splitted = 0;
blud->is_was_splitted = 1;
blud->is_free = 1;
blud->id = 0;
bl->id = 0;
ud->id = 0;
if(blud->parent)
merge(blud);
return;
}
buddy_split_t pmm::buddy::split(buddy_info_t* info) {
if(!info || LEVEL_TO_SIZE(info->level) == 4096 || !info->is_free)
return {info,info};
buddy_info_t* bl = 0;
buddy_info_t* ud = 0;
if(!info->is_was_splitted) {
bl = put(info->phys,info->level - 1);
ud = put(info->phys + LEVEL_TO_SIZE(info->level - 1), info->level - 1);
bl->split_x = 0;
bl->is_free = 1;
ud->split_x = 1;
ud->is_free = 1;
bl->parent = info;
ud->parent = info;
bl->twin = ud;
ud->twin = bl;
} else {
bl = buddy_find_by_parent(info,0);
ud = bl->twin;
bl->is_free = 1;
ud->is_free = 1;
}
info->is_splitted = 1;
info->is_free = 0;
info->is_was_splitted = 1;
return {bl,ud};
}
int __buddy_align_power(uint64_t number) {
if (number == 0) return 0;
uint64_t power = 12;
while (LEVEL_TO_SIZE(power) <= number && power < MAX_LEVEL) {
power++;
}
return power - 1;
}
void pmm::buddy::init() {
limine_memmap_response* mmap = bootloader::bootloader->get_memory_map();
limine_memmap_entry* current = mmap->entries[0];
std::uint64_t top,top_size,total_pages;
top = 0;
top_size = 0;
total_pages = 0;
total_pages = 0;
for(std::size_t i = 0;i < mmap->entry_count; i++) {
current = mmap->entries[i];
if(current->type == LIMINE_MEMMAP_USABLE) {
total_pages += current->length / 4096;
memory_size += current->length;
if(current->length > top_size) {
top = current->base;
top_size = current->length;
}
}
}
std::uint64_t buddy_size = (total_pages * sizeof(buddy_info_t));
klibc::memset(&mem,0,sizeof(buddy_t));
mem.mem = (buddy_info_t*)(top + etc::hhdm());
klibc::memset(mem.mem,0,buddy_size);
mem.buddy_queue = 0;
for(std::size_t i = 0;i < mmap->entry_count; i++) {
current = mmap->entries[i];
if(current->type == LIMINE_MEMMAP_USABLE) {
std::int64_t new_len = 0;
std::uint64_t new_base = 0;
new_len = current->length;
new_base = current->base;
if(new_base == top) {
new_len = top_size - buddy_size;
new_base = ALIGNUP(top + buddy_size,4096);
}
while(new_len > 4096) {
put(new_base,__buddy_align_power(new_len));
new_base += LEVEL_TO_SIZE(__buddy_align_power(new_len));
new_len -= LEVEL_TO_SIZE(__buddy_align_power(new_len));
}
}
}
}
int pmm::buddy::nlfree(std::uint64_t phys) {
auto blud = buddy_find_by_phys_without_split(phys);
if(!blud || blud->is_splitted)
return -1;
blud->is_free = 1;
blud->id = 0;
if(blud->parent)
merge(blud);
return 0;
}
alloc_t pmm::buddy::nlalloc_ext(std::size_t size) {
std::uint64_t top_size = UINT64_MAX;
buddy_info_t* nearest_buddy = 0;
if(size < 4096)
size = 4096;
for(std::uint64_t i = 0;i < mem.buddy_queue; i++) {
if(LEVEL_TO_SIZE(mem.mem[i].level) >= size && LEVEL_TO_SIZE(mem.mem[i].level) < top_size && mem.mem[i].is_free) {
top_size = LEVEL_TO_SIZE(mem.mem[i].level);
nearest_buddy = &mem.mem[i];
if(top_size == size)
break;
}
}
if(nearest_buddy) {
auto blud = split_maximum(nearest_buddy,size);
blud->is_free = 0;
klibc::memset((void*)(blud->phys + etc::hhdm()),0,LEVEL_TO_SIZE(blud->level));
alloc_t result;
result.real_size = LEVEL_TO_SIZE(blud->level);
result.phys = blud->phys;
return result;
}
klibc::printf("There's no memory !\n\r");
return {0,0};
}
alloc_t pmm::buddy::alloc(std::size_t size) {
pmm_lock.lock();
alloc_t res = pmm::buddy::nlalloc_ext(size);
pmm_lock.unlock();
return res;
}
int pmm::buddy::free(std::uint64_t phys) {
pmm_lock.lock();
int res = pmm::buddy::nlfree(phys);
pmm_lock.unlock();
return res;
}
void pmm::init() {
buddy::init();
klibc::printf("PMM: Total usable memory: %lli bytes\r\n",memory_size);
}
std::uint64_t pmm::freelist::alloc_4k() {
pmm_lock.lock();
if(!freelist_hhdm) { // request memory from buddy, 4 mb will be enough
alloc_t res = buddy::nlalloc_ext(1024 * 1024 * 4);
std::uint64_t phys = res.phys;
std::uint64_t ptr = phys;
while(1) {
if(ptr >= phys + (1024 * 1024 * 4))
break;
freelist::nlfree(ptr);
ptr += PAGE_SIZE;
}
}
std::uint64_t mem = (std::uint64_t)freelist_hhdm - etc::hhdm();
freelist_hhdm = (std::uint64_t*)(*freelist_hhdm);
klibc::memset((void*)(mem + etc::hhdm()),0,PAGE_SIZE);
pmm_lock.unlock();
return mem;
}
void pmm::freelist::nlfree(std::uint64_t phys) {
std::uint64_t virt = phys + etc::hhdm();
*(std::uint64_t**)virt = freelist_hhdm;
freelist_hhdm = (std::uint64_t*)virt;
}
void pmm::freelist::free(std::uint64_t phys) {
pmm_lock.lock();
freelist::nlfree(phys);
pmm_lock.unlock();
}
|