proc: enter into userspace
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
1a8061e3b3
commit
cd916f624b
12 changed files with 138 additions and 29 deletions
|
|
@ -51,7 +51,7 @@ void arch_init()
|
||||||
idt_init();
|
idt_init();
|
||||||
wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
|
wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
|
||||||
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
|
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
|
||||||
wrmsr(IA32_FMASK, RFLAGS_IF);
|
wrmsr(IA32_FMASK, 0x3F7FD5);
|
||||||
}
|
}
|
||||||
|
|
||||||
void arch_ap_startup()
|
void arch_ap_startup()
|
||||||
|
|
@ -60,6 +60,4 @@ void arch_ap_startup()
|
||||||
|
|
||||||
struct cpu_local *this_local = &cpu_local_arr[id];
|
struct cpu_local *this_local = &cpu_local_arr[id];
|
||||||
wrmsr(IA32_GS_BASE, (u64)this_local);
|
wrmsr(IA32_GS_BASE, (u64)this_local);
|
||||||
|
|
||||||
yield();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -78,15 +78,14 @@ _syscall_enter:
|
||||||
mov rsp, gs:[8]
|
mov rsp, gs:[8]
|
||||||
|
|
||||||
/* save registers into struct pt_regs */
|
/* save registers into struct pt_regs */
|
||||||
push gs:[16] /* save user RSP */
|
push qword ptr gs:[16] /* save user RSP */
|
||||||
push_all
|
push_all
|
||||||
|
|
||||||
mov rdi, rsp
|
mov rdi, rsp
|
||||||
call syscall
|
call syscall
|
||||||
/* restore registers */
|
/* restore registers */
|
||||||
pop_all
|
pop_all
|
||||||
add rsp, 8
|
pop rsp
|
||||||
mov rsp, [rsp] /* load user RSP */
|
|
||||||
|
|
||||||
swapgs
|
swapgs
|
||||||
sysretq
|
sysretq
|
||||||
|
|
@ -99,6 +98,30 @@ switch_to:
|
||||||
pop_all
|
pop_all
|
||||||
ret
|
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 */
|
/* exceptions */
|
||||||
exception 0x00
|
exception 0x00
|
||||||
exception 0x01
|
exception 0x01
|
||||||
|
|
|
||||||
|
|
@ -34,5 +34,7 @@ struct pt_regs {
|
||||||
|
|
||||||
struct cpu_local *tls_get();
|
struct cpu_local *tls_get();
|
||||||
struct proc *get_cur_proc();
|
struct proc *get_cur_proc();
|
||||||
|
struct thread *get_cur_thread();
|
||||||
|
void enter_userspace_init(struct thread *thread, uaddr_t entry);
|
||||||
|
|
||||||
#endif /* _ARCH_PROC_H_ */
|
#endif /* _ARCH_PROC_H_ */
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
# SPDX-License-Identifier: GPL-2.0-only
|
# SPDX-License-Identifier: GPL-2.0-only
|
||||||
obj-y += atomic.o
|
obj-y += atomic.o
|
||||||
obj-y += spinlock.o
|
obj-y += spinlock.o
|
||||||
|
obj-y += proc.o
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,37 @@ struct cpu_local *tls_get()
|
||||||
return (void *)rdmsr(IA32_GS_BASE);
|
return (void *)rdmsr(IA32_GS_BASE);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct proc *get_cur_proc()
|
struct thread *get_cur_thread()
|
||||||
{
|
{
|
||||||
struct cpu_local *l = tls_get();
|
struct cpu_local *l = tls_get();
|
||||||
if (!l)
|
if (!l)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!l->cur_thread)
|
if (!l->cur_thread)
|
||||||
return NULL;
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ struct _fs {
|
||||||
u64 flags;
|
u64 flags;
|
||||||
size_t block_size;
|
size_t block_size;
|
||||||
size_t n_blocks;
|
size_t n_blocks;
|
||||||
void *priv;
|
void *priv;
|
||||||
|
|
||||||
dirent_t *root_dirent;
|
dirent_t *root_dirent;
|
||||||
|
|
||||||
|
|
@ -97,6 +97,12 @@ struct mount {
|
||||||
u64 flags;
|
u64 flags;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct fildes {
|
||||||
|
u64 fdno;
|
||||||
|
dirent_t *file;
|
||||||
|
struct proc *proc;
|
||||||
|
};
|
||||||
|
|
||||||
void vfs_init(void);
|
void vfs_init(void);
|
||||||
void vfs_register(struct fs_driver *fs);
|
void vfs_register(struct fs_driver *fs);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,6 @@ struct thread {
|
||||||
size_t stack_size;
|
size_t stack_size;
|
||||||
|
|
||||||
u64 state;
|
u64 state;
|
||||||
bool in_kernel;
|
|
||||||
|
|
||||||
u64 tls_base;
|
u64 tls_base;
|
||||||
|
|
||||||
|
|
@ -61,13 +60,16 @@ struct proc {
|
||||||
spinlock_t alloc_lock;
|
spinlock_t alloc_lock;
|
||||||
rbtree_t alloc_map;
|
rbtree_t alloc_map;
|
||||||
|
|
||||||
|
uaddr_t entry;
|
||||||
|
|
||||||
struct ehdr *ehdr;
|
struct ehdr *ehdr;
|
||||||
struct phdrs *phdrs;
|
struct phdr *phdrs;
|
||||||
size_t n_phdrs;
|
size_t n_phdrs;
|
||||||
};
|
};
|
||||||
|
|
||||||
int proc_kill(struct proc *proc, int signal);
|
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);
|
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);
|
struct proc *proc_create(struct proc *parent);
|
||||||
void proc_free(struct proc *proc);
|
void proc_free(struct proc *proc);
|
||||||
void switch_to(struct thread *from, struct thread *to);
|
void switch_to(struct thread *from, struct thread *to);
|
||||||
|
|
|
||||||
27
init/main.c
27
init/main.c
|
|
@ -4,6 +4,7 @@
|
||||||
*
|
*
|
||||||
* Copyright 2023-2026 Michael Shaw
|
* Copyright 2023-2026 Michael Shaw
|
||||||
*/
|
*/
|
||||||
|
#include <arch/proc.h>
|
||||||
|
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
#include <kernel/buddy.h>
|
#include <kernel/buddy.h>
|
||||||
|
|
@ -90,9 +91,31 @@ static void kmain()
|
||||||
do_initcalls();
|
do_initcalls();
|
||||||
arch_ap_startup();
|
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;
|
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()
|
void _before_stack()
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@
|
||||||
#define PT_DYNAMIC 2 /* Dynamic linking information */
|
#define PT_DYNAMIC 2 /* Dynamic linking information */
|
||||||
#define PT_INTERP 3 /* Program interpreter */
|
#define PT_INTERP 3 /* Program interpreter */
|
||||||
|
|
||||||
#define PF_X (1 << 0) /* Segment is executable */
|
#define PF_X (1 << 0) /* Segment is executable */
|
||||||
#define PF_W (1 << 1) /* Segment is writable */
|
#define PF_W (1 << 1) /* Segment is writable */
|
||||||
#define PF_R (1 << 2) /* Segment is readable */
|
#define PF_R (1 << 2) /* Segment is readable */
|
||||||
|
|
||||||
#define EM_X86_64 62
|
#define EM_X86_64 62
|
||||||
|
|
||||||
|
|
@ -54,6 +54,6 @@ struct phdr {
|
||||||
Elf64_Xword p_align; /* Segment alignment */
|
Elf64_Xword p_align; /* Segment alignment */
|
||||||
} PACKED;
|
} 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_ */
|
#endif /* _ELF_H_ */
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
#include "elf.h"
|
#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);
|
struct ehdr *ehdr = kalloc(sizeof(struct ehdr), ALLOC_KERN);
|
||||||
if (!ehdr)
|
if (!ehdr)
|
||||||
|
|
@ -36,20 +36,27 @@ int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_
|
||||||
|
|
||||||
*out_ehdr = ehdr;
|
*out_ehdr = ehdr;
|
||||||
*out_phdrs = phdrs;
|
*out_phdrs = phdrs;
|
||||||
|
*entry = ehdr->e_entry;
|
||||||
|
|
||||||
return ehdr->e_phnum;
|
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 ehdr *ehdr;
|
||||||
struct phdr *phdrs;
|
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)
|
if (res < 0)
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
|
proc->ehdr = ehdr;
|
||||||
|
proc->phdrs = phdrs;
|
||||||
|
proc->n_phdrs = res;
|
||||||
|
proc->entry = entry;
|
||||||
|
|
||||||
for (int i = 0; i < res; i++) {
|
for (int i = 0; i < res; i++) {
|
||||||
struct phdr *cur = phdrs + i;
|
struct phdr *cur = phdrs + i;
|
||||||
if (cur->p_type != PT_LOAD)
|
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);
|
proc_mmap(proc, TO_PHYS(program), page_vaddr, map_size, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
spinlock_release(&proc->mem_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void enter_userspace(struct thread *thread)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void *proc_alloc(struct proc *proc, size_t size)
|
void *proc_alloc(struct proc *proc, size_t size)
|
||||||
{
|
{
|
||||||
struct proc_alloc *a = kalloc(sizeof(struct proc_alloc), ALLOC_KERN);
|
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_size = USTACK_DEFAULT_SIZE;
|
||||||
r_mt->stack_addr = 0x7FFFFFFFF000;
|
r_mt->stack_addr = 0x7FFFFFFFF000;
|
||||||
|
r_mt->proc = ret;
|
||||||
|
r_mt->state = PROC_STATE_DNS;
|
||||||
|
|
||||||
void *ustack = proc_alloc(ret, r_mt->stack_size);
|
void *ustack = proc_alloc(ret, r_mt->stack_size);
|
||||||
|
|
||||||
|
|
@ -93,10 +91,6 @@ struct proc *proc_create(struct proc *parent)
|
||||||
return NULL;
|
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();
|
ret->pgd = pgd_create_user();
|
||||||
if (!ret->pgd) {
|
if (!ret->pgd) {
|
||||||
slab_free(proc_slab, ret);
|
slab_free(proc_slab, ret);
|
||||||
|
|
@ -105,6 +99,10 @@ struct proc *proc_create(struct proc *parent)
|
||||||
return NULL;
|
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) {
|
if (parent) {
|
||||||
int res = copy_parent(ret, parent);
|
int res = copy_parent(ret, parent);
|
||||||
if (res)
|
if (res)
|
||||||
|
|
|
||||||
|
|
@ -12,3 +12,27 @@ syscall_t get_syscall(u64 no)
|
||||||
|
|
||||||
return syscalls[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;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue