Massively Improved Multitasking

This commit is contained in:
vanten-s 2025-06-27 23:55:47 +02:00
parent 72497974ca
commit b0d1c8ac26
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
6 changed files with 75 additions and 40 deletions

View file

@ -158,7 +158,7 @@ static void add_irqs()
add_entry(entry, i);
}
entry = generate_entry(true, irq0, 0xE, 0, 0x8);
entry = generate_entry(true, irq0, 0xE, 0x3, 0x8);
add_entry(entry, IRQ0);
entry = generate_entry(true, keyboard_interrupt, 0xE, 0, 0x8);

View file

@ -10,8 +10,29 @@
.extern cs
.extern eip
.section .bss
.align 16
task_stack_bottom:
.skip 4096
task_stack_top:
.skip 4
temp_esp:
.skip 4
.section .text
irq0:
mov %esp, (temp_esp)
mov $task_stack_top, %esp
pusha
mov temp_esp, %eax
push %eax
push 8(%eax)
push 4(%eax)
push (%eax)
mov $0x10, %eax
mov %ax, %ds
@ -24,16 +45,18 @@ call _switch_task
iret
jump_kernel:
mov $0x20, %eax
outb %al, $0x20
mov (ss), %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
pop %eax
mov $0x20, %eax
outb %al, $0x20
popa
mov (esp), %esp
@ -45,9 +68,6 @@ push (eip)
iret
jump_user:
mov $0x20, %eax
outb %al, $0x20
mov (ss), %eax
mov %ax, %ds
mov %ax, %es
@ -55,6 +75,10 @@ mov %ax, %fs
mov %ax, %gs
pop %eax
mov $0x20, %eax
outb %al, $0x20
popa
push (ss)

View file

@ -10,10 +10,10 @@ syscall_stack_top:
.global syscall
syscall:
mov $0x10, %edx
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %dx, %ds
mov %dx, %es
mov %dx, %fs
mov %dx, %gs
mov %esp, %edx

View file

@ -26,13 +26,15 @@ void run_program(uint8_t* code, size_t code_length, uint8_t* data, size_t data_l
struct CPUState target = {
.eip = (size_t) userland_code,
.esp = 0x00401000,
.eax = 0,
.esp = 0x00400800,
.eax = 0x00400800,
.ebx = 0,
.ecx = 0,
.edx = 0,
.cs = 0x18 | 0x3,
.ds = 0x20 | 0x3,
/// .cs = 0x08,
// .ds = 0x10,
.eflags = 0x0200,
.ebp = 0,
};

View file

@ -14,7 +14,7 @@
#endif
uint8_t program[] = {
// 0xcd, 0x80, // int $0x80
0xcd, 0x80, // int $0x80
0xb9, 0x02, 0x00, 0x00, 0x00, // mov $0, %ecx
0xff, 0xe1, // jmp *%ecx
};
@ -37,6 +37,8 @@ void kernel_main(void)
setup_tasks();
setup_paging();
terminal_writestring("Woaw\n");
asm("cli");
run_program(program, sizeof(program), data, sizeof(data));
@ -44,8 +46,12 @@ void kernel_main(void)
asm("sti");
asm("int $0x20");
int i = 0;
while (true) {
terminal_writestring("Hello from Kernelspace 1\n");
// terminal_writestring("Hello from Kernelspace 2\n");
i += 1;
if (i == 1 << 16) {
terminal_writestring("Hello\n");
i = 0;
}
}
}

View file

@ -1,6 +1,7 @@
#include <kernel/task.h>
#include <kernel/heap.h>
#include <kernel/tty.h>
#include <debugging.h>
struct Task* tasks;
size_t tasks_capacity;
@ -11,6 +12,8 @@ size_t current_task = 0;
void setup_tasks()
{
tasks = malloc(sizeof(*tasks));
tasks[0].state.cs = 8;
tasks[0].state.ds = 16;
tasks_capacity = 1;
num_tasks = 1;
}
@ -82,27 +85,20 @@ static void save_state(
size_t eflags
) {
struct CPUState state = {
.eax = eax,
.ebx = ebx,
.ecx = ecx,
.edx = edx,
tasks[current_task].state.eax = eax,
tasks[current_task].state.ebx = ebx,
tasks[current_task].state.ecx = ecx,
tasks[current_task].state.edx = edx,
.esp = esp,
.ebp = ebp,
tasks[current_task].state.esp = esp,
tasks[current_task].state.ebp = ebp,
.eip = eip,
tasks[current_task].state.eip = eip,
.esi = esi,
.edi = edi,
tasks[current_task].state.esi = esi,
tasks[current_task].state.edi = edi,
.cs = cs,
.ds = ds,
.eflags = eflags,
};
tasks[current_task].state = state;
tasks[current_task].state.eflags = eflags,
current_task++;
current_task %= num_tasks;
@ -117,8 +113,10 @@ void next_task() {
cs = s.cs;
eip = s.eip;
print_hex_bytes(&tasks[current_task].state.eip, 4);
terminal_writestring("\n");
if ((cs & 3) == 0) {
// esp -= 4;
jump_kernel(
s.edi,
s.esi,
@ -144,6 +142,11 @@ void next_task() {
}
void _switch_task(
size_t eip_passed,
size_t cs_passed,
size_t eflags_passed,
size_t esp_passed,
size_t edi,
size_t esi,
size_t ebp,
@ -153,14 +156,11 @@ void _switch_task(
size_t ecx,
size_t eax,
size_t eip_passed,
size_t cs_passed,
size_t eflags_passed,
size_t esp_passed,
size_t ds_passed
)
{
// terminal_writestring("Switching task\n");
terminal_writestring("Switching task\n");
if (num_tasks == 0) {
return;
};
@ -168,12 +168,15 @@ void _switch_task(
size_t ds;
if ((cs & 3) == 0) {
ds = 0x10;
esp_current = esp_current + sizeof(size_t) * 3;
esp_current = esp_passed + sizeof(size_t) * 3;
} else {
ds = ds_passed;
esp_current = esp_passed;
}
print_hex_bytes(&eip_passed, 4);
terminal_writestring("\n");
save_state(eax, ebx, ecx, edx, esp_current, ebp, esi, edi, eip_passed, cs_passed, ds, eflags_passed);
next_task();