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
1:
push_all /* pushes 15 registers */
mov gs:[28], rsp /* save RSP after push_all for scheduler */
mov rdi, [rsp + 120] /* irq no */
call irq_handler
irq_epilogue:
pop_all
test qword ptr [rsp + 24], 3 /* from userspace? */
jz 2f
@ -99,6 +101,11 @@ irq_common:
add rsp, 8
iretq
.global sched_restore_state
sched_restore_state:
mov rsp, [rdi + 8] /* load from thread->kstack_ptr */
jmp irq_epilogue
.global _syscall_enter
_syscall_enter:
swapgs
@ -121,15 +128,6 @@ _syscall_enter:
swapgs
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 */
.global _enter_userspace_init
_enter_userspace_init:

View file

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

View file

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

View file

@ -35,6 +35,11 @@ u32 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
* on the initial process, and while doing an exec
*/