starting to work on pty drivers, fork(), and scheduler redesign

This commit is contained in:
2026-07-07 00:13:24 -05:00
parent 6d7a23d747
commit 177a0b8fe9
29 changed files with 859 additions and 111 deletions

View File

@@ -1,8 +1,8 @@
#include "memory/mm.h"
#include "lib/print.h"
#include "multiboot.h"
#include <stddef.h>
#include <lib/string.h>
extern uint8_t __kernel_start;
extern uint8_t __kernel_end;
@@ -17,6 +17,8 @@ extern uint32_t page_directory[1024];
#define PAGE_DIRECTORY 0xFFFFF000
#define TEMP_MAPPING_VADDR 0xDFFF0000
#define TEMP_COPY_SRC 0xDFFE0000 // Safely 1 page down
#define TEMP_COPY_DST 0xDFFD0000
#define USR_PAGE 0x07
#define SUP_PAGE 0x03
@@ -190,4 +192,77 @@ void* create_task_pd() {
uint32_t* kernel_pt = (uint32_t*)(PAGE_TABLES + (pde * PAGE_SIZE));
kernel_pt[pte] = 0;
return (void*)pd_address;
}
void* clone_task_pd() {
uint32_t child_pd_phys = (uint32_t)p_alloc_frame();
if (child_pd_phys == 0) return NULL;
map_page(TEMP_MAPPING_VADDR, child_pd_phys, PAGE_PRESENT | PAGE_WRITABLE);
uint32_t* child_pd = (uint32_t*)TEMP_MAPPING_VADDR;
uint32_t* parent_pd = (uint32_t*)PAGE_DIRECTORY;
for (int i = 768; i < 1023; i++) {
child_pd[i] = parent_pd[i];
}
child_pd[1023] = child_pd_phys | PAGE_PRESENT | PAGE_WRITABLE;
for (int i = 0; i < 768; i++) {
child_pd[i] = 0;
}
for (int i = 0; i < 768; i++) {
uint32_t pde = parent_pd[i];
if (!(pde & PAGE_PRESENT)) continue;
uint32_t child_pt_phys = (uint32_t)p_alloc_frame();
child_pd[i] = child_pt_phys | (pde & 0xFFF);
map_page(TEMP_COPY_SRC, pde & ~(PAGE_SIZE - 1), PAGE_PRESENT | PAGE_WRITABLE);
map_page(TEMP_COPY_DST, child_pt_phys, PAGE_PRESENT | PAGE_WRITABLE);
flush_tlb();
uint32_t* src_pt = (uint32_t*)TEMP_COPY_SRC;
uint32_t* dst_pt = (uint32_t*)TEMP_COPY_DST;
for (int j = 0; j < 1024; j++) {
uint32_t pte = src_pt[j];
if (!(pte & PAGE_PRESENT)) {
dst_pt[j] = 0;
continue;
}
uint32_t child_frame_phys = (uint32_t)p_alloc_frame();
dst_pt[j] = child_frame_phys | (pte & 0xFFF);
uint32_t parent_data_phys = pte & ~(PAGE_SIZE - 1);
map_page(TEMP_COPY_SRC, parent_data_phys, PAGE_PRESENT | PAGE_WRITABLE);
map_page(TEMP_COPY_DST, child_frame_phys, PAGE_PRESENT | PAGE_WRITABLE);
flush_tlb();
memcpy((void*)TEMP_COPY_DST, (void*)TEMP_COPY_SRC, PAGE_SIZE);
map_page(TEMP_COPY_SRC, pde & ~(PAGE_SIZE - 1), PAGE_PRESENT | PAGE_WRITABLE);
map_page(TEMP_COPY_DST, child_pt_phys, PAGE_PRESENT | PAGE_WRITABLE);
flush_tlb();
}
}
uint32_t pde_main = TEMP_MAPPING_VADDR >> 22;
uint32_t* pt_main = (uint32_t*)(PAGE_TABLES + (pde_main * PAGE_SIZE));
pt_main[(TEMP_MAPPING_VADDR >> 12) & 0x3FF] = 0;
uint32_t pde_src = TEMP_COPY_SRC >> 22;
uint32_t* pt_src = (uint32_t*)(PAGE_TABLES + (pde_src * PAGE_SIZE));
pt_src[(TEMP_COPY_SRC >> 12) & 0x3FF] = 0;
uint32_t pde_dst = TEMP_COPY_DST >> 22;
uint32_t* pt_dst = (uint32_t*)(PAGE_TABLES + (pde_dst * PAGE_SIZE));
pt_dst[(TEMP_COPY_DST >> 12) & 0x3FF] = 0;
flush_tlb();
return (void*)child_pd_phys;
}

View File

@@ -21,6 +21,7 @@ void* p_alloc_frame();
uint32_t total_free_memory();
void* create_task_pd();
void* clone_task_pd();
void map_page(uint32_t vaddr, uint32_t paddr, uint32_t flags);