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);
}

View file

@ -72,7 +72,7 @@ struct _fs {
u64 flags;
size_t block_size;
size_t n_blocks;
void *priv;
void *priv;
dirent_t *root_dirent;
@ -97,6 +97,12 @@ struct mount {
u64 flags;
};
struct fildes {
u64 fdno;
dirent_t *file;
struct proc *proc;
};
void vfs_init(void);
void vfs_register(struct fs_driver *fs);

View file

@ -37,7 +37,6 @@ struct thread {
size_t stack_size;
u64 state;
bool in_kernel;
u64 tls_base;
@ -61,13 +60,16 @@ struct proc {
spinlock_t alloc_lock;
rbtree_t alloc_map;
uaddr_t entry;
struct ehdr *ehdr;
struct phdrs *phdrs;
struct phdr *phdrs;
size_t n_phdrs;
};
int proc_kill(struct proc *proc, int signal);
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);
struct proc *proc_create(struct proc *parent);
void proc_free(struct proc *proc);
void switch_to(struct thread *from, struct thread *to);

View file

@ -4,6 +4,7 @@
*
* Copyright 2023-2026 Michael Shaw
*/
#include <arch/proc.h>
#include <kernel/common.h>
#include <kernel/buddy.h>
@ -90,9 +91,31 @@ static void kmain()
do_initcalls();
arch_ap_startup();
/* do nothing, for now :) */
dirent_t *init;
res = vfs_open("/bin/utest", &init);
if (res < 0)
panic("failed to open /bin/utest: %s\n", strerror(-res));
char *init_buf = kalloc(init->inode->file_size, ALLOC_KERN);
if (!init_buf)
panic("failed to allocate buffer for /bin/utest: %s\n", strerror(ENOMEM));
res = vfs_read(init->inode, init_buf, init->inode->file_size, 0);
if (res < 0)
panic("failed to read /bin/utest: %s\n", strerror(-res));
struct proc *init_proc = proc_create(NULL);
uaddr_t entry = proc_load_file(init_buf, init_proc);
if ((ssize_t)entry < 0)
panic("failed to load /bin/utest: %s\n", strerror(-(ssize_t)entry));
kernel_init_ready = true;
yield();
init_proc->main_thread->state = PROC_STATE_RUNNING;
enter_userspace_init(init_proc->main_thread, entry);
__builtin_unreachable();
}
void _before_stack()

View file

@ -12,9 +12,9 @@
#define PT_DYNAMIC 2 /* Dynamic linking information */
#define PT_INTERP 3 /* Program interpreter */
#define PF_X (1 << 0) /* Segment is executable */
#define PF_W (1 << 1) /* Segment is writable */
#define PF_R (1 << 2) /* Segment is readable */
#define PF_X (1 << 0) /* Segment is executable */
#define PF_W (1 << 1) /* Segment is writable */
#define PF_R (1 << 2) /* Segment is readable */
#define EM_X86_64 62
@ -54,6 +54,6 @@ struct phdr {
Elf64_Xword p_align; /* Segment alignment */
} PACKED;
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs);
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry);
#endif /* _ELF_H_ */

View file

@ -11,7 +11,7 @@
#include "elf.h"
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs)
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry)
{
struct ehdr *ehdr = kalloc(sizeof(struct ehdr), ALLOC_KERN);
if (!ehdr)
@ -36,20 +36,27 @@ int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_
*out_ehdr = ehdr;
*out_phdrs = phdrs;
*entry = ehdr->e_entry;
return ehdr->e_phnum;
}
int proc_load_file(const char *file, struct proc *proc)
ssize_t proc_load_file(const char *file, struct proc *proc)
{
struct ehdr *ehdr;
struct phdr *phdrs;
int res = elf_get_headers(file, &ehdr, &phdrs);
uaddr_t entry = 0;
int res = elf_get_headers(file, &ehdr, &phdrs, &entry);
if (res < 0)
return res;
proc->ehdr = ehdr;
proc->phdrs = phdrs;
proc->n_phdrs = res;
proc->entry = entry;
for (int i = 0; i < res; i++) {
struct phdr *cur = phdrs + i;
if (cur->p_type != PT_LOAD)
@ -77,5 +84,5 @@ int proc_load_file(const char *file, struct proc *proc)
proc_mmap(proc, TO_PHYS(program), page_vaddr, map_size, flags);
}
return 0;
return entry;
}

View file

@ -21,10 +21,6 @@ void proc_mmap(struct proc *proc, paddr_t p, vaddr_t v, size_t len, u64 flags)
spinlock_release(&proc->mem_lock);
}
void enter_userspace(struct thread *thread)
{
}
void *proc_alloc(struct proc *proc, size_t size)
{
struct proc_alloc *a = kalloc(sizeof(struct proc_alloc), ALLOC_KERN);
@ -84,6 +80,8 @@ struct proc *proc_create(struct proc *parent)
r_mt->stack_size = USTACK_DEFAULT_SIZE;
r_mt->stack_addr = 0x7FFFFFFFF000;
r_mt->proc = ret;
r_mt->state = PROC_STATE_DNS;
void *ustack = proc_alloc(ret, r_mt->stack_size);
@ -93,10 +91,6 @@ struct proc *proc_create(struct proc *parent)
return NULL;
}
proc_mmap(ret, TO_PHYS(ustack), r_mt->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW);
ret->main_thread = r_mt;
ret->pgd = pgd_create_user();
if (!ret->pgd) {
slab_free(proc_slab, ret);
@ -105,6 +99,10 @@ struct proc *proc_create(struct proc *parent)
return NULL;
}
proc_mmap(ret, TO_PHYS(ustack), r_mt->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW);
ret->main_thread = r_mt;
if (parent) {
int res = copy_parent(ret, parent);
if (res)

View file

@ -12,3 +12,27 @@ syscall_t get_syscall(u64 no)
return syscalls[no];
}
int syscall_insert(syscall_t fun, u64 no)
{
if (no >= ARRAY_SIZE(syscalls))
return -EINVAL;
if (syscalls[no])
return -EEXIST;
syscalls[no] = fun;
return no;
}
int syscall_remove(u64 no)
{
if (no >= ARRAY_SIZE(syscalls))
return -EINVAL;
if (!syscalls[no])
return -ENOENT;
syscalls[no] = NULL;
return 0;
}