fix: free: use the new pt system

This commit is contained in:
2024-10-30 21:26:00 +01:00
parent ae3cfa7647
commit 6533334824
3 changed files with 30 additions and 22 deletions

View File

@ -60,22 +60,32 @@ void *alloc_pages(size_t size)
int free_pages(void *page_ptr, size_t size)
{
const uint32_t page_addr = (uint32_t)page_ptr;
const uint32_t nb_pages = CEIL(size, PAGE_SIZE);
const uint32_t page_index = (uint32_t)page_ptr / PAGE_SIZE;
const uint32_t page_index = page_addr / PAGE_SIZE;
const uint32_t pd_index = page_index / PD_SIZE;
const uint32_t pt_index = page_index % PD_SIZE;
if ((uint32_t)page_ptr > PT_SIZE * PAGE_SIZE) {
if ((uint32_t)pd_index > 0x300) {
kprintf(KERN_WARNING "Address out of range\n");
return -1;
} else if ((uint32_t)page_ptr % PAGE_SIZE) {
} else if (page_addr % PAGE_SIZE) {
kprintf(KERN_WARNING "Invalid address\n");
return -1;
} else if (page_index + nb_pages > PT_SIZE) {
} else if (pt_index + nb_pages > PT_SIZE) {
kprintf(KERN_WARNING "Invalid number of frames\n");
return -1;
}
for (size_t i = page_index; i < page_index + nb_pages; i++) {
free_frames((void *)(i >> 12), PAGE_SIZE);
i = i << 12 | INIT_FLAGS;
uint32_t *page_table =
(uint32_t *)GET_PAGE_ADDR(0, PT_START + pd_index);
for (uint16_t i = pt_index; i < pt_index + nb_pages; i++) {
if (page_table[i] >> 12 == i) {
kprintf(KERN_WARNING "Page already free\n");
return -2;
}
free_frames((void *)(page_table[i] & PAGE_MASK), PAGE_SIZE);
page_table[i] = i << 12;
}
return 0;
}