tasking: continue fixing task_kill

This commit is contained in:
2025-06-23 23:41:01 +03:00
parent 29de6e6f43
commit b769ac0a58
6 changed files with 125 additions and 73 deletions
+40 -21
View File
@@ -41,8 +41,8 @@ uint32_t setup_tmp_pgdir(uint32_t magic, uint32_t info)
if(!(n % 1024)) {
pd = n / 1024;
uint32_t pt_phys = (uint32_t)page_table - PAGE_OFFSET + (PAGE_SIZE * pd);
kpage_dir[pd] = pt_phys | PAGE_PRESENT | PAGE_RW | PAGE_USER;//TODO: fix, I don't think pd should have DPL3
kpage_dir[GET_PGDIR(PAGE_OFFSET) + pd] = pt_phys | PAGE_PRESENT | PAGE_RW | PAGE_USER;//TODO: fix, I don't think pd should have DPL3
kpage_dir[pd] = pt_phys | PAGE_PRESENT | PAGE_RW;//TODO: fix, I don't think pd should have DPL3
kpage_dir[GET_PGDIR(PAGE_OFFSET) + pd] = pt_phys | PAGE_PRESENT | PAGE_RW;//TODO: fix, I don't think pd should have DPL3
}
}
@@ -134,7 +134,9 @@ uint32_t* create_page_dir()
for(int i = 768; i < 1023; i++)
{
new_pd[i] = current_pd[i];
if(current_pd[i] & PAGE_PRESENT) {
new_pd[i] = current_pd[i];
}
}
int vga_pde_index = 0; // Virtual address 0x000B8000 is covered by PDE[0]
@@ -147,27 +149,44 @@ uint32_t* create_page_dir()
return new_pd;
}
//TODO: this function is somehow messes up addresses so free_page passes page_nums like 0xF000F into clear_bit
void destroy_page_dir(uint32_t* page_dir) {
// Temporarily switch to target PD for freeing
uint32_t orig_cr3;
asm volatile("mov %%cr3, %0" : "=r"(orig_cr3));
asm volatile("mov %0, %%cr3" : : "r"(virt_to_phys(page_dir)));
// Free only user-space pages (entries 0-767)
void destroy_page_dir(uint32_t* page_dir_virt) {
if (page_dir_virt == NULL) {
return;
}
// Iterate through user-space page directory entries (PDEs 0-767).
// Kernel space (768+) is shared and should not be freed.
for (int i = 0; i < 768; i++) {
if (page_dir[i] & PAGE_PRESENT) {
uint32_t* pt = (uint32_t*)phys_to_virt(page_dir[i] & ~0xFFF);
for (int j = 0; j < 1024; j++) {
if (pt[j] & PAGE_PRESENT) {
free_page((void*)(pt[j] & ~0xFFF));
uint32_t pde = page_dir_virt[i];
// Check if the page directory entry is present
if (pde & PAGE_PRESENT) {
// Get the physical address of the page table
uint32_t pt_phys = pde & ~0xFFF;
if(is_page_in_use(pt_phys / PAGE_SIZE)){
// Convert it to a virtual address the kernel can access
uint32_t* page_table_virt = (uint32_t*)phys_to_virt(pt_phys);
// Iterate through all 1024 entries in this page table
for (int j = 0; j < 1024; j++) {
uint32_t pte = page_table_virt[j];
// If the page table entry is present, free the physical page (frame) it points to
if (pte & PAGE_PRESENT) {
printf("freeing page %X\n", pte);
free_page((void*)(pte & ~0xFFF));
}
}
// After freeing all pages within the table, free the page table itself
free_page((void*)pt_phys);
}
free_page((void*)(page_dir[i] & ~0xFFF));
}
}
// Restore original page directory
asm volatile("mov %0, %%cr3" : : "r"(orig_cr3));
free_page((void*)virt_to_phys(page_dir));
// Finally, free the page directory itself.
// We need its physical address to pass to the physical memory manager.
printf("freeing PD page %X\n", virt_to_phys(page_dir_virt));
free_page((void*)virt_to_phys(page_dir_virt));
}