kernel multitasking

This commit is contained in:
rami 2024-07-07 23:42:44 -04:00
parent 5f025d18bf
commit ff347e52f7
9 changed files with 320 additions and 55 deletions

256
.gdb_history Normal file
View File

@ -0,0 +1,256 @@
ni
ni
c
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
c
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
target remote localhost:1234
target remote localhost:1234
ni
ni
c
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
c
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
c
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
c
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni
b pit_isr
c
target remote localhost:1234
c
ni
ni
ni
ni
ni
c
target remote localhost:1234
target remote localhost:1234
c
ni
ni
ni
ni
ni
ni
ni
ni
ni
ni

View File

@ -20,7 +20,7 @@ ISO := Hazel.iso
CFLAGS := -ffreestanding -Wall -Wextra -Werror -I $(INCLUDEDIR) -I lib
LDFLAGS := -ffreestanding -nostdlib -lgcc -T kernel/kernel.ld
QEMUFLAGS := -cdrom $(BUILDDIR)/$(ISO) \
-s -d int \
-s \
-m 512M \
-serial stdio

View File

@ -17,6 +17,6 @@
#define DIVISOR (PIT_FREQUENCY/DESIRED_FREQUENCY)
void pit_init(void);
void pit_handler(void);
int pit_handler(void);
#endif

View File

@ -11,6 +11,7 @@ typedef struct {
} task_t;
int task_init(task_t *task, elf_t *elf);
void task_sort();
void schedule(void);
#endif

View File

@ -86,10 +86,28 @@ halt:
hlt
jmp halt
extern ctx
extern task_sort
global pit_isr
pit_isr:
pushad
extern pit_handler
call pit_handler
cmp eax, 1
jne .ret
mov edi, [ctx+28] ; EDI = current task
; mov esi, [edi+8] ; ESI = next task
; Save current ESP
mov dword [edi], esp
call task_sort
mov edi, [ctx+28] ; EDI = NEW current task
; Set current task to next task
; mov dword [ctx+28], esi
; Change to new ESP
mov esp, [edi]
.ret:
popad
iret
global jmp_user_mode
@ -115,31 +133,6 @@ flush_tss:
ltr [esp+4]
ret
extern ctx
global task_switch
task_switch:
; Save current task's registers
push ebx
push esi
push edi
push ebp
mov edi, [ctx+28] ; EDI = current task
mov esi, [esp+20] ; ESI = next task
; Save current ESP
mov dword [edi], esp
; Set current task to next task
mov dword [ctx+28], esi
; Change to new ESP
mov esp, [esi]
mov ecx, [esi+4]
pop ebp
pop edi
pop esi
pop ebx
ret
extern exception_handler
%macro isr_err_stub 1
isr_stub_%+%1:

View File

@ -28,6 +28,7 @@ void exception_handler(int_stack_frame_t r) {
r.int_no, r.err_code, r.eax, r.ecx, r.edx, r.ebx, r.esi, r.edi, r.ebp, r.esp+24);
LOG("\nEIP: 0x%08X\nCS: 0x%08X\nEFLAGS: 0x%08X\n",
r.eip, r.cs, r.eflags);
for (;;) {}
}
void sleep(int delay) {
@ -49,10 +50,9 @@ void idt_init(void) {
}
void test(void) {
for (int i = 0; i < 1000; i++) {
LOG("O");
for (;;) {
LOG("B");
}
task_switch((task_t *)ctx.current_task->next);
}
void kernel(multiboot_info_t *info) {
@ -69,12 +69,12 @@ void kernel(multiboot_info_t *info) {
LOG("%d bytes of RAM detected\nCreated a %d byte large physical memory map at 0x%08X\n", ctx.mmap_size*BLOCK_SIZE*8, ctx.mmap_size, (uint32_t)ctx.mmap);
idt_init();
/*
pic_remap(PIC_1_START, PIC_2_START);
asm volatile ("sti" ::);
ctx.ticks = 0;
pit_init();
*/
// Setup TSS
uint32_t base = (uint32_t)&tss;
uint32_t limit = base + sizeof(tss_t);
@ -101,20 +101,24 @@ void kernel(multiboot_info_t *info) {
uint32_t *stack = mmap_find_first_free_block();
asm volatile ("mov %%esp, %0" : "=r" (task1.esp));
stack[1023] = (uint32_t)test;
stack[1022] = 0;
stack[1021] = 0;
stack[1020] = 0;
stack[1019] = 0;
task2.esp = (uint32_t)stack + (0x1000 - 20);
stack[1023] = 0x207;
stack[1022] = 0x8;
stack[1021] = (uint32_t)test;
stack[1020] = 0xDEADBEEF;
stack[1019] = 0xDEADBEEF;
stack[1018] = 0xDEADBEEF;
stack[1017] = 0xDEADBEEF;
stack[1016] = 0xDEADBEEF;
stack[1015] = 0xDEADBEEF;
stack[1014] = 0xDEADBEEF;
stack[1013] = 0xDEADBEEF;
task2.esp = (uint32_t)stack + (0x1000 - 44);
task2.cr3 = (uint32_t)boot_page_dir - KERNEL_VMA;
task2.next = (uint32_t *)&task1;
//task_switch(&task2);
for (int i = 0; i < 1000; i++) {
LOG("_");
task2.next = 0;
for (;;) {
LOG("A");
}
task_switch((task_t *)ctx.current_task->next);
task_switch((task_t *)ctx.current_task->next);
/*
multi_mod_t *init = (multi_mod_t *)(info->moduleaddress + KERNEL_VMA);

View File

@ -14,19 +14,13 @@ void pit_init(void) {
extern void task_switch(task_t *task);
void pit_handler(void) {
int pit_handler(void) {
ctx.ticks++;
if (ctx.current_task->next) {
LOG("TASK SWITCH\n");
//task_t *old = ctx.current_task;
//task_t *new = (task_t *)ctx.current_task->next;
//new->next = (uint32_t *)old;
//old->next = (uint32_t *)new;
task_switch((task_t *)ctx.current_task->next);
}
outb(PIT_CHAN0_DATA, (uint8_t)(DIVISOR & 0xff));
outb(PIT_CHAN0_DATA, (uint8_t)((DIVISOR & 0xff00) >> 8));
pic_send_eoi(0);
if ((ctx.ticks % 3) == 0) return 1;
else return 0;
}

View File

@ -7,7 +7,7 @@ extern kernel_ctx_t ctx;
int task_init(task_t *task, elf_t *elf) {
section_t *text = elf_find_section(elf, ".text");
if (!text) return 0;
task->eip = elf->e_entry + text->sh_offset;
//task->eip = elf->e_entry + text->sh_offset;
// Allocate a page for a stack
task->esp = (uint32_t)mmap_find_first_free_block() + 0x1000;
@ -33,6 +33,21 @@ int task_init(task_t *task, elf_t *elf) {
return 1;
}
void task_sort() {
if (!ctx.current_task || !ctx.current_task->next) return; // If the list is empty or has only one node
task_t* new_head = (task_t *)ctx.current_task->next;
task_t* temp = new_head;
while (temp->next) {
temp = (task_t *)temp->next;
}
temp->next = (uint32_t *)ctx.current_task;
ctx.current_task->next = 0;
ctx.current_task = new_head;
}
void schedule(void) {
// If there's only one task running
if (!ctx.current_task->next) return;

View File

@ -0,0 +1,2 @@
break pit_isr