a little cleanup in task.c and paging.h

This commit is contained in:
2025-05-29 23:13:03 +03:00
parent c808a5d295
commit 7c870636c5
2 changed files with 6 additions and 43 deletions
-2
View File
@@ -22,8 +22,6 @@
#define GET_PGDIR(address) ((uint32_t)((address) >> 22) & 0x3FF) #define GET_PGDIR(address) ((uint32_t)((address) >> 22) & 0x3FF)
#define GET_PGTBL(address) ((uint32_t)((address) >> 12) & 0x3FF) #define GET_PGTBL(address) ((uint32_t)((address) >> 12) & 0x3FF)
#define PAGE_SIZE 0x1000 // 4 КБ
// Общий размер памяти // Общий размер памяти
#define MEMORY_SIZE (0xFFFFFFFF-0xC0000000) #define MEMORY_SIZE (0xFFFFFFFF-0xC0000000)
+6 -41
View File
@@ -22,25 +22,17 @@ extern Process* current;
extern uint32_t pid_counter; extern uint32_t pid_counter;
extern uint32_t task_count; extern uint32_t task_count;
// Функция запуска процесса в ядре, после которой произойдёт переход в ring 3
static void processStartup()
{
printf("startup\n");
scheduler_unlock();
// Возврат из этой функции приведёт к вызову trapret
}
Process* task_create(uint32_t func, void** args, uint32_t arg_count, uint32_t ring) Process* task_create(uint32_t func, void** args, uint32_t arg_count, uint32_t ring)
{ {
Process* p = (Process*)alloc_page();//heap_alloc(sizeof(Process)); Process* p = (Process*)alloc_page();
memset(p, 0, sizeof(Process)); memset(p, 0, sizeof(Process));
p->pid = ++pid_counter; p->pid = ++pid_counter;
p->state = Ready; p->state = Ready;
p->ring = ring; p->ring = ring;
// Выделяем стек ядра // Выделяем стек ядра
p->kstack = (char*)alloc_page();//heap_alloc(KSTACKSIZE); p->kstack = (char*)alloc_page();
map_page(p->kstack, p->kstack, 0x3); map_page(p->kstack, p->kstack, PAGE_PRESENT | PAGE_RW);
printf("kstack: %X!!!!!!!!!!!!\n", p->kstack); printf("kstack: %X!!!!!!!!!!!!\n", p->kstack);
uint8_t* sp = (uint8_t*)(p->kstack + KSTACKSIZE); uint8_t* sp = (uint8_t*)(p->kstack + KSTACKSIZE);
@@ -68,7 +60,7 @@ Process* task_create(uint32_t func, void** args, uint32_t arg_count, uint32_t ri
uint32_t user_stack_virtual_top = 0xBFFFF000 - ((p->pid -1) * (USTACKSIZE + 4096)); // Уникальный верх стека для каждого процесса uint32_t user_stack_virtual_top = 0xBFFFF000 - ((p->pid -1) * (USTACKSIZE + 4096)); // Уникальный верх стека для каждого процесса
uint32_t user_stack_virtual_base = user_stack_virtual_top - USTACKSIZE; uint32_t user_stack_virtual_base = user_stack_virtual_top - USTACKSIZE;
map_page(user_stack_physical, (void*)user_stack_virtual_base, 0x7/*PTE_PRESENT | PTE_READ_WRITE | PTE_USER*/); // Флаги 0x7 map_page(user_stack_physical, (void*)user_stack_virtual_base, PAGE_PRESENT | PAGE_RW | PAGE_USER); // Флаги 0x7
} }
@@ -120,8 +112,6 @@ void schedule()
next = queue; next = queue;
if (next != current) if (next != current)
{ {
//loadPageDirectory(next->page_directory);
//printf("switching to %X\n", next->pid);
switchProcess(next); switchProcess(next);
} }
} }
@@ -149,12 +139,11 @@ void jump_usermode2(void) ;
void scheduler_init() void scheduler_init()
{ {
task_create((uint32_t)idle, NULL, 0, 0); task_create((uint32_t)idle, NULL, 0, 0);
task_create((uint32_t)idle, NULL, 0, 0);
void* args1[] = {(void*)42, (void*)"ebalo"}; void* args1[] = {(void*)42, (void*)"ebalo"};
task_create((uint32_t)task1, args1, 2, 0); task_create((uint32_t)task1, args1, 2, 0);
void* args2[] = {(void*)69, (void*)420}; void* args2[] = {(void*)69, (void*)420};
//task_create((EntryPoint)&task2, args2, 2, 0); task_create((uint32_t)task2, args2, 2, 0);
jump_usermode2(); jump_usermode2();
} }
@@ -221,29 +210,5 @@ void jump_usermode2(void) {
(code_pte & 0x4) ? "user" : "supervisor"); (code_pte & 0x4) ? "user" : "supervisor");
task_create((uint32_t)user_code_virt, NULL, 0, 3); task_create((uint32_t)user_code_virt, NULL, 0, 3);
task_create((uint32_t)second_user_code_virt, NULL, 0, 3); //task_create((uint32_t)second_user_code_virt, NULL, 0, 3);
return;
// Switch to user mode with interrupts enabled
asm volatile (
"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();
} }