MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / Allocate4KPages

Function Allocate4KPages

Kernel/src/arch/x86_64/paging.cpp:264–346  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

262 }
263
264 void* Allocate4KPages(uint64_t amount, address_space_t* addressSpace){
265 uint64_t offset = 0;
266 uint64_t pageDirOffset = 0;
267 uint64_t counter = 0;
268 uintptr_t address = 0;
269
270 uint64_t pml4Index = 0;
271 for(int d = 0; d < 512; d++){
272 uint64_t pdptIndex = d;
273 if(!(addressSpace->pdpt[d] & 0x1)) break;
274 /* Attempt 1: Already Allocated Page Tables*/
275 for(int i = 0; i < TABLES_PER_DIR; i++){
276 if(addressSpace->pageDirs[d][i] & 0x1 && !(addressSpace->pageDirs[d][i] & 0x80)){
277 for(int j = 0; j < PAGES_PER_TABLE; j++){
278 if(addressSpace->pageTables[d][i][j] & 0x1){
279 pageDirOffset = i;
280 offset = j+1;
281 counter = 0;
282 continue;
283 }
284
285 counter++;
286
287 if(counter >= amount){
288 address = (PDPT_SIZE * pml4Index) + (pdptIndex * PAGE_SIZE_1G) + (pageDirOffset * PAGE_SIZE_2M) + (offset*PAGE_SIZE_4K);
289 while(counter--){
290 if(offset >= 512){
291 pageDirOffset++;
292 offset = 0;
293 }
294 addressSpace->pageTables[d][pageDirOffset][offset] = 0x3;
295 offset++;
296 }
297
298 return (void*)address;
299 }
300 }
301 } else {
302 pageDirOffset = i+1;
303 offset = 0;
304 counter = 0;
305 }
306 }
307
308 pageDirOffset = 0;
309 offset = 0;
310 counter = 0;
311
312 /* Attempt 2: Allocate Page Tables*/
313 for(int i = 0; i < TABLES_PER_DIR; i++){
314 if(!(addressSpace->pageDirs[d][i] & 0x1)){
315
316 CreatePageTable(d,i,addressSpace);
317 for(int j = 0; j < PAGES_PER_TABLE; j++){
318
319 address = (PDPT_SIZE * pml4Index) + (pdptIndex * PAGE_SIZE_1G) + (pageDirOffset * PAGE_SIZE_2M) + (offset*PAGE_SIZE_4K);
320 counter++;
321

Callers 5

MapSharedMemoryFunction · 0.85
CreateELFProcessFunction · 0.85
SysMapFBFunction · 0.85
SysAllocFunction · 0.85
SysMmapFunction · 0.85

Calls 3

CreatePageTableFunction · 0.85
InfoFunction · 0.85
KernelPanicFunction · 0.85

Tested by

no test coverage detected