sched: simplify entry-exit path between processes

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-22 01:09:27 -04:00
commit 11bedfe3a8
7 changed files with 24 additions and 20 deletions

View file

@ -88,9 +88,11 @@ irq_common:
swapgs swapgs
1: 1:
push_all /* pushes 15 registers */ push_all /* pushes 15 registers */
mov gs:[28], rsp /* save RSP after push_all for scheduler */
mov rdi, [rsp + 120] /* irq no */ mov rdi, [rsp + 120] /* irq no */
call irq_handler call irq_handler
irq_epilogue:
pop_all pop_all
test qword ptr [rsp + 24], 3 /* from userspace? */ test qword ptr [rsp + 24], 3 /* from userspace? */
jz 2f jz 2f
@ -99,6 +101,11 @@ irq_common:
add rsp, 8 add rsp, 8
iretq iretq
.global sched_restore_state
sched_restore_state:
mov rsp, [rdi + 8] /* load from thread->kstack_ptr */
jmp irq_epilogue
.global _syscall_enter .global _syscall_enter
_syscall_enter: _syscall_enter:
swapgs swapgs
@ -121,15 +128,6 @@ _syscall_enter:
swapgs swapgs
sysretq sysretq
/* called from the scheduler to switch processes */
.global switch_to
switch_to:
push_all
mov [rdi + 8], rsp /* 8: struct thread stack_addr offset */
mov rsp, [rsi + 8]
pop_all
ret
/* return path after a process was execed */ /* return path after a process was execed */
.global _enter_userspace_init .global _enter_userspace_init
_enter_userspace_init: _enter_userspace_init:

View file

@ -5,6 +5,7 @@
#include <kernel/sched.h> #include <kernel/sched.h>
#include <drivers/apic.h> #include <drivers/apic.h>
#include <arch/msr.h> #include <arch/msr.h>
#include <arch/proc.h>
#include <stdint.h> #include <stdint.h>
@ -73,7 +74,9 @@ uint8_t lapic_idno()
static void timer_handler() static void timer_handler()
{ {
extern void schedule(void); struct thread *cur = get_cur_thread();
sched_save_state(cur);
sched_add_thread(cur);
lapic_eoi(); lapic_eoi();
schedule(); schedule();
} }

View file

@ -12,6 +12,7 @@ struct cpu_local {
vaddr_t kernel_rsp; vaddr_t kernel_rsp;
vaddr_t user_rsp; vaddr_t user_rsp;
u32 cpu_id; u32 cpu_id;
vaddr_t irq_rsp;
struct tss tss; struct tss tss;
} PACKED; } PACKED;

View file

@ -35,6 +35,11 @@ u32 cpu_id()
return tls_get()->cpu_id; return tls_get()->cpu_id;
} }
void sched_save_state(struct thread *t)
{
t->kstack_ptr = tls_get()->irq_rsp;
}
/* the thread that calls this will be lost, so it should only be called /* the thread that calls this will be lost, so it should only be called
* on the initial process, and while doing an exec * on the initial process, and while doing an exec
*/ */

View file

@ -40,8 +40,8 @@ struct proc;
struct thread { struct thread {
tid_t tid; tid_t tid;
uaddr_t kstack_ptr; /* kernel RSP save slot for switch_to (offset 8) */ uaddr_t kstack_ptr; /* kernel RSP save slot for sched_restore_state (offset 8) */
uaddr_t ustack_top; /* user stack top */ uaddr_t ustack_top; /* user stack top */
size_t stack_size; size_t stack_size;
u64 state; u64 state;
@ -50,7 +50,7 @@ struct thread {
struct proc *proc; struct proc *proc;
struct thread *next; struct thread *next;
uaddr_t kstack_top; /* top of kernel stack allocation */ uaddr_t kstack_top; /* top of kernel stack allocation */
}; };
struct proc { struct proc {
@ -85,7 +85,6 @@ void proc_mmap(struct proc *proc, paddr_t p, vaddr_t v, size_t len, u64 flags);
ssize_t proc_load_file(const char *file, struct proc *proc); ssize_t proc_load_file(const char *file, struct proc *proc);
struct proc *proc_create(struct thread *parent); struct proc *proc_create(struct thread *parent);
void proc_free(struct proc *proc); void proc_free(struct proc *proc);
void switch_to(struct thread *from, struct thread *to);
void proc_init(); void proc_init();
pid_t sys_fork(); pid_t sys_fork();
void schedule(); void schedule();

View file

@ -5,5 +5,8 @@
#include <kernel/proc.h> #include <kernel/proc.h>
void sched_add_thread(struct thread *t); void sched_add_thread(struct thread *t);
void schedule();
void sched_save_state(struct thread *t);
void sched_restore_state(struct thread *t);
#endif /* _SCHED_H_ */ #endif /* _SCHED_H_ */

View file

@ -64,20 +64,15 @@ static struct thread *dequeue_thread()
void schedule() void schedule()
{ {
struct thread *cur = get_cur_thread();
struct thread *next = dequeue_thread(); struct thread *next = dequeue_thread();
if (!next) if (!next)
return; return;
struct thread *prev = cur;
if (cur)
sched_add_thread(cur);
struct cpu_local *tls = tls_get(); struct cpu_local *tls = tls_get();
tls->cur_thread = next; tls->cur_thread = next;
tls->kernel_rsp = next->kstack_top; tls->kernel_rsp = next->kstack_top;
tls->tss.rsp0 = next->kstack_top; tls->tss.rsp0 = next->kstack_top;
pgd_load(next->proc->pgd); pgd_load(next->proc->pgd);
switch_to(prev, next); sched_restore_state(next);
} }