f'd myself over of course, scheduler can now run user programs....

This commit is contained in:
2026-07-04 01:59:19 -05:00
parent 0b98ac0273
commit 17ccde3556
11 changed files with 109 additions and 60 deletions

View File

@@ -18,10 +18,22 @@ isr\num:
isr_common_stub:
pushal
xor %eax, %eax
mov %ds, %ax
pushl %eax
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
pushl %esp
call common_interrupt_handler
addl $4, %esp
popl %eax
mov %ax, %ds
mov %ax, %es
popal
addl $8, %esp
iret
@@ -44,17 +56,28 @@ ISR_ERRCODE 14 # Page Fault (Has Error Code!)
.extern send_eoi_master # drivers/pic/pic.S
isr32:
cli
pushal
pushal
call send_eoi_master
xor %eax, %eax
mov %ds, %ax
pushl %eax
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
pushl %esp
call switch_context
movl %eax, %esp
call send_eoi_master
popl %eax
mov %ax, %ds
mov %ax, %es
popal
sti
iret
iret
ISR_NOERRCODE 33 # IRQ1 - Keyboard

View File

@@ -29,15 +29,6 @@ typedef struct __attribute__((packed)) {
uint32_t base;
} idt_ptr;
typedef struct __attribute__((packed)) {
uint32_t edi, esi, ebp, dummy, ebx, edx, ecx, eax;
uint32_t int_no;
uint32_t error_code;
uint32_t eip, cs, eflags, u_esp, u_ss;
} intr_regs_t;
// (interrupts.S)
extern void load_idt(void);
extern void isr0();
@@ -89,7 +80,7 @@ void init_idt() {
if (isr_stub_table[i] != 0) {
set_idtr(i, isr_stub_table[i], SEGMENT_KERN, ATTR_KERN_32);
} else {
set_idtr(i, 0, SEGMENT_KERN, 0);
set_idtr(i, 0, SEGMENT_KERN, ATTR_KERN_32);
}
}
@@ -98,6 +89,15 @@ void init_idt() {
load_idt();
}
typedef struct __attribute__((packed)) {
uint32_t ds;
uint32_t edi, esi, ebp, dummy, ebx, edx, ecx, eax;
uint32_t int_no;
uint32_t error_code;
uint32_t eip, cs, eflags, u_esp, u_ss;
} intr_regs_t;
void common_interrupt_handler(intr_regs_t *args) {
switch (args->int_no) {
case 0:
@@ -116,7 +116,6 @@ void common_interrupt_handler(intr_regs_t *args) {
case 13:
case 15:
case 16:
asm volatile("cli");
kprintf("\n--- CPU Exception ---\n");
kprintf("Error: [%d] %s\n", args->int_no, err_messages[args->int_no]);
kprintf("Error Code: 0x%x\n", args->error_code);
@@ -125,10 +124,10 @@ void common_interrupt_handler(intr_regs_t *args) {
kprintf("EAX: 0x%x\n", args->eax);
kprintf("EFLAGS: 0x%x\n", args->eflags);
kprintf("Code Segment: 0x%x\n", args->cs);
kprintf("Calling Data Segment: 0x%x\n", args->ds);
kprintf("Current cr3: 0x%x\n", fetch_cr3());
kprintf("tss.esp0: 0x%x\n", tss.esp0);
process_t* task = current_task();
kprintf("Task kstack_top: 0x%x\n", task->kstack_top);
kprintf("instruction: 0x%x\n", *(uint32_t*)args->eip);
asm volatile("hlt");
break;
case 14:{
@@ -154,6 +153,10 @@ void common_interrupt_handler(intr_regs_t *args) {
asm volatile("hlt");
break;
}
case 32: {
break;
}
case 33: {
kprintf("ps2 keyboard interrupt\n");
uint8_t data = ps2_read_data();

View File

@@ -69,17 +69,13 @@ uint32_t switch_context(uint32_t current_esp) {
uint32_t current_cr3 = fetch_cr3();
if (current_cr3 != process_table[current_process].cr3) {
kprintf("switching cr3 | old: 0x%x | new: 0x%x\n", current_cr3, process_table[current_process].cr3);
load_pd((void*)process_table[current_process].cr3);
}
if (process_table[current_process].flags & PROCESS_FLAG_USER) {
tss.esp0 = process_table[current_process].kstack_top;
}
if (found_ready) {
kprintf("found ready task %d | cr3: 0x%x\n", current_process, process_table[current_process].cr3);
}
return process_table[current_process].esp;
}
@@ -123,12 +119,12 @@ int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) {
if (user) {
*(--esp) = 0x23; // User Data Segment (SS) with RPL 3 (0x20 | 3)
*(--esp) = u_esp; // User Stack Pointer (ESP) - location mapped in user space
*(--esp) = 0x0202; // EFLAGS (Interrupts enabled)
*(--esp) = 0x202; // EFLAGS (Interrupts enabled)
*(--esp) = 0x1B; // User Code Segment (CS) with RPL 3 (0x18 | 3)
*(--esp) = entry_point; // EIP
} else {
// IRET values for Ring 0
*(--esp) = 0x0202; // EFLAGS
*(--esp) = 0x202; // EFLAGS
*(--esp) = 0x08; // Kernel Code Segment (CS)
*(--esp) = entry_point; // EIP
}
@@ -142,6 +138,12 @@ int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) {
*(--esp) = 0; // ESI
*(--esp) = 0; // EDI
if (user) {
*(--esp) = 0x23; // User Data Segment Selector (RPL 3)
} else {
*(--esp) = 0x10; // Kernel Data Segment Selector (RPL 0)
}
process->esp = (uint32_t)esp;
return 1;
}

View File

@@ -4,33 +4,33 @@
syscall_handler:
cli
pushfl
pushl %cs
push $0
push $0x80
movl 8(%esp), %eax
pushl %eax
movl 4(%esp), %ebx # Get CS
movl %ebx, 8(%esp) # Move CS over Old EIP
movl 0(%esp), %ebx # Get Return EIP
movl %ebx, 4(%esp) # Move Return EIP over CS
addl $4, %esp # Adjust stack pointer
pusha
movw %ds, %ax
pushl %eax
push %ds
push %es
push %fs
push %gs
pushal
pushl %esp
call switch_context
movl %eax, %esp
popal
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
popl %eax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
push %esp
call syscall
add $4, %esp
mov 28(%esp), %eax
sti
pop %gs
pop %fs
pop %es
pop %ds
popa
add $8, %esp
iret

View File

@@ -18,6 +18,7 @@ typedef struct registers {
static int32_t sys_exit(int status) {
asm volatile("cli");
asm volatile("hlt");
return 0;
}