merge branch memory-shit into higher-half

This commit is contained in:
2025-05-10 15:48:10 +03:00
parent 65f543fa92
commit 767e74976f
14 changed files with 419 additions and 178 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ sys_exit_handler:
global test_user_function
test_user_function:
mov eax, 0xDEADBEEF
mov eax, 0xB00B1E5 ; 0xDEADBEEF
int 0x80
jmp $
+51 -28
View File
@@ -87,7 +87,7 @@ void schedule()
next = queue;
if(next != current)
{
loadPageDirectory(next->page_directory);
//loadPageDirectory(next->page_directory);
switchProcess(next);
}
}
@@ -116,7 +116,7 @@ void jump_usermode2(void);
void scheduler_init()
{
//jump_usermode2();
jump_usermode2();
//we create this task two times because in other case it just won't start
task_create((EntryPoint)&idle, NULL, 0);
task_create((EntryPoint)&idle, NULL, 0);
@@ -127,6 +127,8 @@ void scheduler_init()
task_create((EntryPoint)&task2, args2, 2);
}
#include "../include/string.h"
extern void test_user_function(void);
void jump_usermode2(void) {
void* code_phys = alloc_page();
@@ -135,18 +137,25 @@ void jump_usermode2(void) {
while (1);
}
// Временно отображаем code_phys в ядре
uint32_t kernel_temp_virt = 0xC0100000;
map_page(code_phys, (void*)kernel_temp_virt, 0x3); // Present, R/W, Supervisor
// Temporarily map code_phys into kernel space
uint32_t kernel_temp_virt = 0xC0500000;
map_page(code_phys, (void*)kernel_temp_virt, 0x3); // R/W in kernel
// Копируем код
uint32_t* src = (uint32_t*)test_user_function;
uint32_t* dst = (uint32_t*)kernel_temp_virt;
for (int i = 0; i < 1024; i++) {
dst[i] = src[i];
}
printf("Copied code to 0x%x (virt 0x%x): 0x%x 0x%x 0x%x\n",
(uint32_t)code_phys, kernel_temp_virt, dst[0], dst[1], dst[2]);
// Copy user code
memcpy((void*)kernel_temp_virt, test_user_function, 4096); // Use memcpy
printf("Copied code at 0x%x: 0x%x 0x%x 0x%x\n",
kernel_temp_virt,
*(uint32_t*)kernel_temp_virt,
*(uint32_t*)(kernel_temp_virt + 4),
*(uint32_t*)(kernel_temp_virt + 8));
// Unmap temporary kernel mapping
unmap_page((void*)kernel_temp_virt);
printf("Copied code to 0x%x (virt 0x%x)\n",
(uint32_t)code_phys, kernel_temp_virt);
// Настраиваем Ring 3
uint32_t user_stack_top;
@@ -158,22 +167,36 @@ void jump_usermode2(void) {
printf("user_code_virt: 0x%x, user_stack_top: 0x%x\n",
(uint32_t)user_code_virt, user_stack_top);
//map_page(alloc_page(), (void*)0x400000, 0x7);
map_page(alloc_page(), (void*)0x800000, 0x7);
uint32_t code_pte = get_pte(user_code_virt);
printf("Before iret: Code PTE at 0x%x: 0x%x (phys 0x%x, %s, %s)\n",
(uint32_t)user_code_virt, code_pte, code_pte & ~0xFFF,
(code_pte & 0x2) ? "writable" : "read-only",
(code_pte & 0x4) ? "user" : "supervisor");
// Switch to user mode with interrupts enabled
asm volatile (
"mov $0x23, %%dx\n"
"mov %%dx, %%ds\n"
"mov %%dx, %%es\n"
"mov %%dx, %%fs\n"
"mov %%dx, %%gs\n"
"push $0x23\n"
"push %0\n"
"pushf\n"
"push $0x1B\n"
"push %1\n"
"iret\n"
:
: "r" (user_stack_top), "r" (user_code_virt)
: "dx", "memory"
);
"pushf\n"
"pop %%eax\n"
"or $0x200, %%eax\n" // Enable interrupts (IF)
"push %%eax\n"
"mov $0x23, %%dx\n"
"mov %%dx, %%ds\n"
"mov %%dx, %%es\n"
"mov %%dx, %%fs\n"
"mov %%dx, %%gs\n"
"push $0x23\n" // SS (user data segment)
"push %0\n" // ESP (user_stack_top)
"push %%eax\n" // EFLAGS (with IF)
"push $0x1B\n" // CS (user code segment)
"push %1\n" // EIP (user_code_virt)
"iret\n"
:
: "r" (user_stack_top), "r" (user_code_virt)
: "dx", "eax", "memory", "cc"
);
__builtin_unreachable();
}