error_code_t free_kheap_pages(void* address, uint32_t num_pages) { if(!((uint32_t)address >= KHEAP_BEGIN_ADDRESS && (uint32_t)address < KHEAP_END_ADDRESS)) { return INVALID_ARGUMENT; } if(PAGE_BOUNDARY(address) != (uint32_t)address) { return INVALID_ARGUMENT; } //Update bitmap uint32_t bit_index = address_to_bit_index((uint32_t)address, KHEAP_BEGIN_ADDRESS); turn_bits_on(kheap_st.bitmap, bit_index, num_pages); //Free the actual page in the page table. physical_page_ptr_t* base_page_table = (physical_page_ptr_t*)KHEAP_RECURSIVE_ADDRESS; for(int i = ADDRESS_TO_PAGE_TABLE_ENTRY(address); i < ADDRESS_TO_PAGE_TABLE_ENTRY(address) + num_pages; i++) { free_physical_page((uint32_t)base_page_table[i]); base_page_table[i] = 0; } tlb_flush(); return SUCCESS; }
void deletePageTable(uint64_t cr3_content){ int l1=0; int pages_deleted =0; uint64_t *pml4e = (uint64_t*)(align_4KB(cr3_content)); for(l1=0;l1<1;l1++){ if(pml4e[l1] == 0) continue; uint64_t *pdtpe = (uint64_t*)get_linear_address(align_4KB(pml4e[l1])); int l2=0; for(l2=0;l2<512;l2++){ if(pdtpe[l2]==0) continue; uint64_t *pde = (uint64_t*)get_linear_address(align_4KB(pdtpe[l2])); int l3=0; for(l3=0;l3<512;l3++){ if(pde[l3]==0) continue; uint64_t *pte = (uint64_t*)get_linear_address(align_4KB(pde[l3])); int l4 =0; for(l4 = 0; l4<512;l4++){ if(pte[l4] == 0) continue; free_physical_page(pte[l4],cr3_content); pages_deleted +=1; pte[l4] = 0; } pages_deleted +=1; free_physical_page(pde[l3],cr3_content); pde[l3]=0; } free_physical_page(pdtpe[l2],cr3_content); pdtpe[l2]=0; pages_deleted +=1; } free_physical_page(pml4e[l1],cr3_content); pml4e[l1]=0; pages_deleted +=1; } //free_page((uint64_t)pml4e); // pages_deleted +=1; //print("~~~~~ Pages deleted In page table= %d~~~~\n",pages_deleted); }