| 346 | } |
| 347 | |
| 348 | void* KernelAllocate4KPages(uint64_t amount){ |
| 349 | uint64_t offset = 0; |
| 350 | uint64_t pageDirOffset = 0; |
| 351 | uint64_t counter = 0; |
| 352 | uintptr_t address = 0; |
| 353 | |
| 354 | uint64_t pml4Index = KERNEL_HEAP_PML4_INDEX; |
| 355 | uint64_t pdptIndex = KERNEL_HEAP_PDPT_INDEX; |
| 356 | |
| 357 | /* Attempt 1: Already Allocated Page Tables*/ |
| 358 | for(int i = 0; i < TABLES_PER_DIR; i++){ |
| 359 | if(kernelHeapDir[i] & 0x1 && !(kernelHeapDir[i] & 0x80)){ |
| 360 | for(int j = 0; j < TABLES_PER_DIR; j++){ |
| 361 | if(kernelHeapDirTables[i][j] & 0x1){ |
| 362 | pageDirOffset = i; |
| 363 | offset = j+1; |
| 364 | counter = 0; |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | counter++; |
| 369 | |
| 370 | if(counter >= amount){ |
| 371 | address = (PDPT_SIZE * pml4Index) + (pdptIndex * PAGE_SIZE_1G) + (pageDirOffset * PAGE_SIZE_2M) + (offset*PAGE_SIZE_4K); |
| 372 | address |= 0xFFFF000000000000; |
| 373 | while(counter--){ |
| 374 | if(offset >= 512){ |
| 375 | pageDirOffset++; |
| 376 | offset = 0; |
| 377 | } |
| 378 | kernelHeapDirTables[pageDirOffset][offset] = 0x3; |
| 379 | offset++; |
| 380 | } |
| 381 | |
| 382 | return (void*)address; |
| 383 | } |
| 384 | } |
| 385 | } else { |
| 386 | pageDirOffset = i+1; |
| 387 | offset = 0; |
| 388 | counter = 0; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | pageDirOffset = 0; |
| 393 | offset = 0; |
| 394 | counter = 0; |
| 395 | |
| 396 | /* Attempt 2: Allocate Page Tables*/ |
| 397 | for(int i = 0; i < TABLES_PER_DIR; i++){ |
| 398 | if(!(kernelHeapDir[i] & 0x1)){ |
| 399 | counter += 512; |
| 400 | |
| 401 | if(counter >= amount){ |
| 402 | address = (PDPT_SIZE * pml4Index) + (pdptIndex * PAGE_SIZE_1G) + (pageDirOffset * PAGE_SIZE_2M) + (offset*PAGE_SIZE_4K); |
| 403 | address |= 0xFFFF000000000000; |
| 404 | //kernelHeapDir[i] = (PAGE_FRAME & ((uintptr_t)&(kernelHeapDirTables[i]) - KERNEL_VIRTUAL_BASE)) | 0x3; |
| 405 | SetPageFrame(&(kernelHeapDir[pageDirOffset]),((uintptr_t)&(kernelHeapDirTables[pageDirOffset]) - KERNEL_VIRTUAL_BASE)); |
no test coverage detected