proc: enter into userspace

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-19 19:25:38 -04:00
commit cd916f624b
12 changed files with 138 additions and 29 deletions

View file

@ -51,7 +51,7 @@ void arch_init()
idt_init();
wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
wrmsr(IA32_FMASK, RFLAGS_IF);
wrmsr(IA32_FMASK, 0x3F7FD5);
}
void arch_ap_startup()
@ -60,6 +60,4 @@ void arch_ap_startup()
struct cpu_local *this_local = &cpu_local_arr[id];
wrmsr(IA32_GS_BASE, (u64)this_local);
yield();
}

View file

@ -78,15 +78,14 @@ _syscall_enter:
mov rsp, gs:[8]
/* save registers into struct pt_regs */
push gs:[16] /* save user RSP */
push qword ptr gs:[16] /* save user RSP */
push_all
mov rdi, rsp
call syscall
/* restore registers */
pop_all
add rsp, 8
mov rsp, [rsp] /* load user RSP */
pop rsp
swapgs
sysretq
@ -99,6 +98,30 @@ switch_to:
pop_all
ret
.global _enter_userspace_init
_enter_userspace_init:
mov rcx, rdi
mov r11, rsi
mov rsp, rdx
xor rax, rax
xor rbx, rbx
xor rdx, rdx
xor rdi, rdi
xor rsi, rsi
xor rbp, rbp
xor r8, r8
xor r9, r9
xor r10, r10
xor r12, r12
xor r13, r13
xor r14, r14
xor r15, r15
swapgs
sysretq
/* exceptions */
exception 0x00
exception 0x01

View file

@ -34,5 +34,7 @@ struct pt_regs {
struct cpu_local *tls_get();
struct proc *get_cur_proc();
struct thread *get_cur_thread();
void enter_userspace_init(struct thread *thread, uaddr_t entry);
#endif /* _ARCH_PROC_H_ */

View file

@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += atomic.o
obj-y += spinlock.o
obj-y += proc.o

View file

@ -9,12 +9,37 @@ struct cpu_local *tls_get()
return (void *)rdmsr(IA32_GS_BASE);
}
struct proc *get_cur_proc()
struct thread *get_cur_thread()
{
struct cpu_local *l = tls_get();
if (!l)
return NULL;
if (!l->cur_thread)
return NULL;
return l->cur_thread->proc;
return l->cur_thread;
}
struct proc *get_cur_proc()
{
struct thread *t = get_cur_thread();
if (!t)
return NULL;
return t->proc;
}
/* the thread that calls this will be lost, so it should only be called
* on the initial process, and while doing an exec
*/
void enter_userspace_init(struct thread *thread, uaddr_t entry)
{
struct cpu_local *tls = tls_get();
tls->cur_thread = thread;
pgd_load(thread->proc->pgd);
thread->state = PROC_STATE_RUNNING;
void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top);
_enter_userspace_init(entry, 0x202, thread->stack_addr);
}