syscalls: continue working on fork syscall

This commit is contained in:
2025-07-07 00:19:35 +03:00
parent 2a7baf1184
commit e793e45854
3 changed files with 37 additions and 9 deletions
+31 -7
View File
@@ -1,6 +1,8 @@
#include "../include/paging.h"
#include <stdint.h>
#include "../include/string.h"
#include "../include/kheap.h"
#include "../include/liballoc.h"
uint32_t *kpage_dir;
@@ -123,6 +125,11 @@ uint32_t phys_to_virt(uint32_t phys)
return phys+0xC0000000;
}
void* phys_to_virt2(void* phys)
{
return phys+0xC0000000;
}
uint32_t* create_page_dir()
{
void* ppp = alloc_page();
@@ -190,14 +197,19 @@ void destroy_page_dir(uint32_t* page_dir_virt) {
free_page((void*)virt_to_phys(page_dir_virt));
}
static void* scratch_src = (void*)0xE1000000;
static void* scratch_dst = (void*)0xE1001000;
uint32_t* copy_page_dir(uint32_t* page_dir_virt)
{
if(page_dir_virt == NULL)
return 0;
debug_log("====%X %X\n", scratch_dst, scratch_src);
uint32_t* new_pagedir = create_page_dir();
for(int i = 0; i < 768; i++)
for(int i = 1; i < 768; i++)
{
uint32_t pde = page_dir_virt[i];
@@ -208,6 +220,7 @@ uint32_t* copy_page_dir(uint32_t* page_dir_virt)
if(new_page_table == NULL) {
return NULL;
}
uint32_t* new_page_table_virt = (uint32_t*)phys_to_virt((uint32_t)new_page_table);
// Get original page table (physical address from PDE)
uint32_t* orig_page_table = (uint32_t*)phys_to_virt(pde & ~0xFFF);
@@ -225,14 +238,24 @@ uint32_t* copy_page_dir(uint32_t* page_dir_virt)
void* orig_phys_page = (void*)(pte & ~0xFFF);
debug_log("copying %X to %X...", orig_phys_page, new_phys_page);
memcpy(new_phys_page, orig_phys_page, PAGE_SIZE);
debug_log("done\n");
new_page_table[j] = (uint32_t)new_phys_page | (pte & 0xFFF);
map_page(orig_phys_page, scratch_src, PAGE_PRESENT | PAGE_RW);
map_page(new_phys_page, scratch_dst, PAGE_PRESENT | PAGE_RW);
//debug_log("copying %X to %X...", orig_phys_page, new_phys_page);
//memcpy(new_phys_page, orig_phys_page, PAGE_SIZE);
//memcpy(new_virt_page, orig_virt_page, PAGE_SIZE);
memcpy(scratch_dst, scratch_src, PAGE_SIZE);
unmap_page(scratch_dst);
unmap_page(scratch_src);
//debug_log("done\n");
new_page_table_virt[j] = (uint32_t)new_phys_page | (pte & 0xFFF);
}
else
{
new_page_table[j] = pte;
new_page_table_virt[j] = pte;
}
}
@@ -246,5 +269,6 @@ uint32_t* copy_page_dir(uint32_t* page_dir_virt)
}
}
debug_log("ENDENDENDEND\n");
return new_pagedir;
return new_pagedir - 0xC0000000;
}