tasking, mm: remove unused functions and page_directory field from the Task struct

This commit is contained in:
2025-06-01 14:04:49 +03:00
parent 5f87e55067
commit d1ac41deeb
7 changed files with 10 additions and 642 deletions
-91
View File
@@ -17,60 +17,6 @@ uint32_t get_pte(void *virtualaddr) {
return pt[ptindex];
}
static uint32_t next_user_virt = 0x00400000;
void* setup_user_process(uint32_t* pd, void* user_code_phys, uint32_t* user_stack_top) {
if ((uint32_t)user_code_phys & 0xFFF) {
printf("SETUP_USER_PROCESS ERROR: user_code_phys not aligned\n");
return 0;
}
void* stack_phys = alloc_page();
if (!stack_phys) return 0;
uint32_t code_virt = next_user_virt;
uint32_t stack_virt = code_virt + 0x3FF000; // 4 МБ - 4 КБ
if (next_user_virt >= 0xC0000000 - 0x400000) {
printf("SETUP_USER_PROCESS ERROR: out of user address space\n");
return 0;
}
//unmap_page((void*)code_virt);
//unmap_page((void*)stack_virt);
map_page_in_directory(pd, user_code_phys, (void*)code_virt, 0x7);
map_page_in_directory(pd, stack_phys, (void*)stack_virt, 0x7);
// Verify mappings
uint32_t code_pte = get_pte((void*)code_virt);
uint32_t stack_pte = get_pte((void*)stack_virt);
uint32_t check_pte = get_pte((void*)0x00400000);
printf("Code PTE at 0x%x: 0x%x (phys 0x%x, %s, %s)\n",
code_virt, code_pte, code_pte & ~0xFFF,
(code_pte & 0x2) ? "writable" : "read-only",
(code_pte & 0x4) ? "user" : "supervisor");
printf("Stack PTE at 0x%x: 0x%x (phys 0x%x, %s, %s)\n",
stack_virt, stack_pte, stack_pte & ~0xFFF,
(stack_pte & 0x2) ? "writable" : "read-only",
(stack_pte & 0x4) ? "user" : "supervisor");
printf("PTE for 0x00400000: 0x%x (phys 0x%x, %s, %s)\n",
check_pte, check_pte & ~0xFFF,
(check_pte & 0x2) ? "writable" : "read-only",
(check_pte & 0x4) ? "user" : "supervisor");
*user_stack_top = stack_virt + 0x1000 -4;//here we place the stack pointer to its top (because the stack is growing down)
next_user_virt += 0x400000; // Следующий 4 МБ блок
return (void*)code_virt;
}
void free_user_process(void* code_phys, void* stack_phys) {
free_page(code_phys);
free_page(stack_phys);
}
void *get_physaddr(void *virtualaddr) {
unsigned long pdindex = (unsigned long)virtualaddr >> 22;
unsigned long ptindex = (unsigned long)virtualaddr >> 12 & 0x03FF;
@@ -194,40 +140,3 @@ int map_page_in_directory(uint32_t* pd_virt_addr, void* phys_addr, void* virt_ad
return 0; // Success
}
void map_kernel_page(void* virtualaddr, unsigned int flags) {
uint32_t virt = (uint32_t)virtualaddr & ~0xFFF; // Выравниваем по 4 КБ
if (virt < 0xC0000000) while(1); // Ошибка: ядро только выше 3 ГБ
uint32_t pdindex = virt >> 22; // Индекс в каталоге
uint32_t ptindex = (virt >> 12) & 0x3FF; // Индекс в таблице страниц
// Проверяем, существует ли таблица страниц
if (!(kpage_dir[pdindex] & 0x1)) {
void* new_pt = alloc_page();
if (!new_pt) while(1); // Нет памяти
// Убеждаемся, что адрес выровнен
if ((uint32_t)new_pt & 0xFFF) while(1); // Ошибка выравнивания
kpage_dir[pdindex] = ((uint32_t)new_pt) | 0x3; // Временно записываем в каталог
asm volatile("invlpg (%0)" : : "r" ((uint32_t)&kpage_dir[pdindex]) : "memory"); // Инвалидируем TLB
uint32_t* pt = ((uint32_t*)kpage_dir+0x1000) + (0x400 * pdindex);
for (int i = 0; i < 1024; i++) {
pt[i] = 0;
}
}
// Получаем таблицу страниц
uint32_t* pt = ((uint32_t*)kpage_dir+0x1000) + (0x400 * pdindex);
void* phys = alloc_page();
if (!phys) while(1);
// Проверяем, не занята ли страница
if (pt[ptindex] & 0x1) while(1); // Ошибка: страница уже отображена
pt[ptindex] = (uint32_t)phys | (flags & 0xFFF) | 0x1; // supervisor, present
asm volatile("invlpg (%0)" : : "r" (virtualaddr) : "memory");
}