Skip to content

Commit

Permalink
sch: Programs launch again
Browse files Browse the repository at this point in the history
  • Loading branch information
marv7000 committed Nov 21, 2024
1 parent 770cc6f commit 06c399d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
4 changes: 4 additions & 0 deletions include/menix/system/sch/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void sch_arch_save(Cpu* core, Thread* thread);
// ? Defined per architecture.
void sch_arch_update(Cpu* core, Thread* next);

// Stops execution on the current core and waits for another interrupt.
// ? Defined per architecture.
ATTR(noreturn) void sch_arch_stop();

// Implementation of the scheduler. Not meant to be called directly, use `sch_invoke` instead.
// Returns a pointer to the new context.
// ? Defined per architecture.
Expand Down
1 change: 0 additions & 1 deletion include/menix/system/sch/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ typedef struct Thread
{
usize id; // Thread ID.
SpinLock lock; // Access lock.
bool can_exec; // If the thread is ready for execution.
ThreadState state; // Current state of the thread.
Context registers; // The register state at the time of context switch.
VirtAddr stack; // The stack pointer.
Expand Down
8 changes: 8 additions & 0 deletions kernel/arch/x86_64/sch/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ void sch_arch_update(Cpu* core, Thread* next)
core->tss.rsp0 = next->kernel_stack;
core->fpu_restore(next->saved_fpu);
}

ATTR(noreturn) void sch_arch_stop()
{
asm_interrupt_enable();
apic_send_eoi();
while (true)
asm_halt();
}
51 changes: 17 additions & 34 deletions kernel/system/sch/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ Context* sch_reschedule(Context* context)

Cpu* cur = arch_current_cpu();
cur->ticks_active++;
// timer_stop_sched();

if (cur->ticks_active % 10000 == 0)
kmesg("Alive for 0x%p ticks\n", cur->ticks_active);

if (spin_try_lock(&rope_lock))
{
Expand Down Expand Up @@ -219,43 +215,30 @@ Context* sch_reschedule(Context* context)
// Update the state of the currently running thread.
if (running != NULL)
{
if (running->can_exec == true)
{
Process* parent = running->parent;
usize idx;
if (list_find(&parent->threads, idx, running))
list_pop(&parent->threads, idx);

spin_lock(&thread_lock);
}
else
{
// Save the current context.
running->registers = *context;
running->stack = cur->user_stack;
running->kernel_stack = cur->kernel_stack;

sch_arch_save(cur, running);

if (running->state == ThreadState_Running)
running->state = ThreadState_Ready;

spin_unlock(&running->lock);
}
// Save the current context.
running->registers = *context;
running->stack = cur->user_stack;
running->kernel_stack = cur->kernel_stack;

sch_arch_save(cur, running);
if (running->state == ThreadState_Running)
running->state = ThreadState_Ready;
}

// Grab the next thread.
running = sch_next(thread_list);
running = sch_next(running);

// If there are no more threads to run, something went wrong.
if (running == NULL)
if (cur->thread != NULL)
{
kmesg("[Scheduler]\tNo more threads to run, halting!\n");
// The old thread is now free.
spin_unlock(&cur->thread->lock);
}

// If there are no more threads to run, wait until an interrupt happens.
if (running == NULL)
{
cur->thread = NULL;
asm_interrupt_enable();
while (true)
asm_halt();
sch_arch_stop();
}

// Update CPU information.
Expand Down
4 changes: 4 additions & 0 deletions kernel/system/sch/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ void thread_execve(Thread* target, VirtAddr start, char** argv, char** envp, boo
*(--sized_stack) = num_argv;

// Update stack start.
#ifdef CONFIG_arch_x86_64
target->registers.rsp = (void*)sized_stack - foreign + target->stack;
#elif defined(CONFIG_arch_riscv64)
target->registers.x2 = (void*)sized_stack - foreign + target->stack;
#endif

vm_unmap_foreign(foreign, foreign_pages);

Expand Down

0 comments on commit 06c399d

Please sign in to comment.