Skip to content

Commit

Permalink
sch: Clean up scheduler funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
marv7000 committed Dec 18, 2024
1 parent da2da9c commit e8ab530
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion include/menix/system/sch/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void sch_init();

// Makes the scheduler act immediately instead of waiting for a timer.
// ? Defined per architecture.
void sch_invoke();
void sch_arch_invoke();

// Saves the architecture dependent data of the `thread`.
// ? Defined per architecture.
Expand Down
3 changes: 2 additions & 1 deletion kernel/arch/x86_64/include/bits/asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
"lretq\n" \
"L_reload_regs%=:\n" \
"mov %1, %%ax\n" \
"mov %%ax, %%ss\n" \
"xor %%ax, %%ax\n" \
"mov %%ax, %%ds\n" \
"mov %%ax, %%es\n" \
"mov %%ax, %%fs\n" \
"mov %%ax, %%gs\n" \
"mov %%ax, %%ss\n" \
: \
: "i"(code_seg), "i"(data_seg) \
: "rax")
Expand Down
11 changes: 7 additions & 4 deletions kernel/arch/x86_64/sch/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

#include <menix/system/arch.h>
#include <menix/system/sch/scheduler.h>
#include <menix/util/log.h>

#include <apic.h>

void sch_invoke()
void sch_arch_invoke()
{
// Make sure interrupts are enabled.
asm_interrupt_enable();
Expand All @@ -17,7 +18,8 @@ void sch_invoke()
void sch_arch_save(Cpu* core, Thread* thread)
{
thread->fs_base = asm_rdmsr(MSR_FS_BASE);
thread->gs_base = asm_rdmsr(MSR_GS_BASE);
// Save the swapped out GSBASE, not our own!
thread->gs_base = asm_rdmsr(MSR_KERNEL_GS_BASE);

core->fpu_save(thread->saved_fpu);
}
Expand All @@ -28,13 +30,14 @@ void sch_arch_update(Cpu* core, Thread* next)
core->fpu_restore(next->saved_fpu);

asm_wrmsr(MSR_FS_BASE, next->fs_base);
asm_wrmsr(MSR_GS_BASE, next->gs_base);
// Restore the swapped out GSBASE, not our own!
asm_wrmsr(MSR_KERNEL_GS_BASE, next->gs_base);
}

ATTR(noreturn) void sch_arch_stop()
{
apic_send_eoi();
asm_interrupt_enable();
apic_send_eoi();
while (true)
asm_halt();
}
2 changes: 1 addition & 1 deletion kernel/arch/x86_64/system/arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void arch_init_cpu(Cpu* cpu, Cpu* boot_cpu)
if (ebx & CPUID_7B_FSGSBASE)
{
cr4 |= CR4_FSGSBASE;
asm_wrmsr(MSR_KERNEL_GS_BASE, (u64)cpu);
asm_wrmsr(MSR_KERNEL_GS_BASE, 0);
asm_wrmsr(MSR_GS_BASE, (u64)cpu);
asm_wrmsr(MSR_FS_BASE, 0);
}
Expand Down
3 changes: 2 additions & 1 deletion kernel/arch/x86_64/system/arch.s
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

/* Enter syscall via AMD64 syscall/sysret instructions. */
.global sc_syscall
.extern syscall_handler
.align 0x10
sc_syscall:
swapgs /* Change GS to kernel mode. */
Expand All @@ -70,7 +69,9 @@ sc_syscall:
push_all_regs /* Push general purpose registers so they can be written to by syscalls */
mov %rsp, %rdi /* Load the Context* as first argument. */
xor %rbp, %rbp /* Zero out the base pointer. */
.extern syscall_handler
call syscall_handler /* Call syscall handler */
mov %rax, %rsp
pop_all_regs /* Pop stack values back to the general purpose registers. */
add $0x10, %rsp /* Skip Context.error and Context.isr fields. */
movq %gs:16, %rsp /* Load user stack from `Cpu.user_stack`. */
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/x86_64/system/gdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ void gdt_load(Gdt* gdt_table)
};

asm_gdt_set(gdtr);
asm_flush_segment_regs(offsetof(Gdt, kernel_code), offsetof(Gdt, kernel_data));
tss_reload();
asm_flush_segment_regs(offsetof(Gdt, kernel_code), offsetof(Gdt, kernel_data));
}
2 changes: 1 addition & 1 deletion kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ATTR(noreturn) void kernel_init(BootInfo* info)
sch_init();

while (true)
sch_invoke();
sch_arch_invoke();
}

ATTR(noreturn) void kernel_main()
Expand Down
8 changes: 4 additions & 4 deletions kernel/system/isr.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ Context* isr_handler(Context* regs)
// If we have a handler for this interrupt, call it.
if (regs->isr < ARRAY_SIZE(current->irq_handlers) && current->irq_handlers[regs->isr])
{
return current->irq_handlers[regs->isr](regs);
Context* result = current->irq_handlers[regs->isr](regs);
return result;
}

// If unhandled and caused by the user, terminate the process with SIGILL.
if (current->thread->is_user)
if (current->thread && current->thread->is_user)
{
Process* proc = current->thread->parent;
print_log("Unhandled interrupt %zu caused by user program! Terminating PID %i!\n", regs->isr, proc->id);
arch_dump_registers(regs);

proc_kill(proc, true);
return regs;
return sch_reschedule(regs);
}

// Disable spinlocks so we have a chance of displaying a message.
Expand Down
2 changes: 1 addition & 1 deletion kernel/system/sch/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void sch_init()
Process* kernel_proc = proc_create("kernel", ProcessState_Ready, false, NULL);
Thread* kernel_thread = thread_create(kernel_proc);
thread_setup(kernel_thread, (VirtAddr)kernel_main, false, 0);
sch_invoke();
sch_arch_invoke();
}

Thread* sch_next(Thread* list)
Expand Down

0 comments on commit e8ab530

Please sign in to comment.