kernel threads

kernel threads and preemptive multitasking
This commit is contained in:
Matheus
2022-08-13 14:00:26 -03:00
parent ac5339453b
commit e7f4245c6d
14 changed files with 274 additions and 44 deletions
+5 -1
View File
@@ -1,4 +1,4 @@
objects := $(patsubst %.c,%.o,$(wildcard *.c)) israsm.o
objects := $(patsubst %.c,%.o,$(wildcard *.c)) israsm.o context.o
subdirs=boot
OBJPATH=$(OBJDIR)/arch/${TARGET}/
export
@@ -15,3 +15,7 @@ israsm.o: isr.asm
nasm -f elf64 -o $@ $<
mkdir -p $(OBJPATH)
cp $@ $(OBJPATH)/$@
context.o: context.asm
nasm -f elf64 -o $@ $<
mkdir -p $(OBJPATH)
cp $@ $(OBJPATH)/$@
+32
View File
@@ -0,0 +1,32 @@
global arch_switchcontext
arch_switchcontext:
mov rsp,rdi
add rsp, 8 ; cr2
pop rax
mov gs,rax
pop rax
mov fs,rax
pop rax
mov es,rax
pop rax
mov ds,rax
pop rax
pop rbx
pop rcx
pop rdx
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
pop rdi
pop rsi
pop rbp
add rsp,8 ; remove error code
swapgs
iretq
+1
View File
@@ -22,6 +22,7 @@ typedef struct{
thread_t* thread;
size_t timerticksperus;
timer_req* timerfirstreq;
timer_req schedreq;
} cls_t;
void bsp_setcls();
+6
View File
@@ -0,0 +1,6 @@
#ifndef _KSTACK_H_INCLUDE
#define _KSTACK_H_INCLUDE
void arch_setkernelstack(void*);
#endif
+2
View File
@@ -2,10 +2,12 @@
#define _REGS_H_INCLUDE
#include <stdint.h>
#include <stdbool.h>
typedef struct{
uint64_t cr2,gs,fs,es,ds,rax,rbx,rcx,rdx,r8,r9,r10,r11,r12,r13,r14,r15,rdi,rsi,rbp,error,rip,cs,rflags,rsp,ss;
} arch_regs;
void arch_regs_setupkernel(arch_regs* regs, void* ip, void* stack, bool interrupts);
#endif
+2 -6
View File
@@ -75,7 +75,7 @@ section .text
pop rdi
pop rsi
pop rbp
add rsp,8 ; remove error code
%endmacro
@@ -95,8 +95,6 @@ section .text
call %2
popregs
add rsp,8 ; remove error code
iretq
@@ -120,9 +118,7 @@ section .text
call %2
popregs
add rsp,8 ; remove error code
iretq
%endmacro
+8
View File
@@ -0,0 +1,8 @@
#include <arch/kstack.h>
#include <arch/ist.h>
#include <arch/cls.h>
void arch_setkernelstack(void* stack){
arch_getcls()->ist.rsp0 = (uint64_t)stack;
}
+2 -2
View File
@@ -28,9 +28,7 @@ struct limine_terminal_response* liminetty;
int ttylock = 0;
void liminewrite(char* str, size_t c){
liminetty->write(liminetty->terminals[0], str, c);
}
void kmain(){
@@ -80,6 +78,8 @@ void kmain(){
pci_enumerate();
sched_init();
smp_init();
_panic("End of kmain()", 0);
+13
View File
@@ -0,0 +1,13 @@
#include <arch/regs.h>
void arch_regs_setupkernel(arch_regs* regs, void* ip, void* stack, bool interrupts){
regs->rip = ip;
regs->rsp = stack;
regs->cs = 0x28;
regs->ds = 0x30;
regs->es = 0x30;
regs->fs = 0x30;
regs->gs = 0x30;
regs->ss = 0x30;
regs->rflags = interrupts ? 0x200 : 0;
}