usr: implement user process switching

This is a rather large commit, so the gist of it is this:

User context switching is now functional, after a change to the TSS, and
a few minor details in the scheduler and loader.

ELF parsing is rather primitive, but basically functional, as long as it
only loads code.

The interrupt 0x80 is for syscalls, and will switch from user mode to
kernel mode. It is currently the only interrupt that can be called from
user mode

This adds the syscall functionality, which currently uses a red-black
tree to store the function pointers, although when the table is more
flushed out, it will probably be replaced with an array

The pager now correctly marks the parent nodes with attributes as it
allocates them. Without doing this, the page table entries will never be
reached in lower privilege modes, causing page faults

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2023-11-20 21:59:12 -05:00
commit 37518aea81
19 changed files with 262 additions and 69 deletions

View file

@ -190,6 +190,70 @@ irq 0x0D
irq 0x0E
irq 0x0F
.global gate_syscall
gate_syscall:
cli
/* push scratch registers */
pushq %rax
pushq %rcx
pushq %rdx
pushq %rsi
pushq %rdi
pushq %r8
pushq %r9
pushq %r10
pushq %r11
/* get current task registers */
call proc_current_regs
/* save current task registers */
popq %rdi
movq %rdi, 80(%rax) /* r11 */
popq %rdi
movq %rdi, 72(%rax) /* r10 */
popq %rdi
movq %rdi, 64(%rax) /* r9 */
popq %rdi
movq %rdi, 56(%rax) /* r8 */
popq %rdi
movq %rdi, 40(%rax) /* rdi */
popq %rdi
movq %rdi, 32(%rax) /* rsi */
popq %rdi
movq %rdi, 24(%rax) /* rdx */
popq %rdi
movq %rdi, 16(%rax) /* rcx */
popq %rdi
movq %rdi, 0(%rax) /* rax */
/* save remaining registers */
movq %rbx, 8(%rax)
movq %rbp, 48(%rax)
movq %r12, 88(%rax)
movq %r13, 96(%rax)
movq %r14, 104(%rax)
movq %r15, 112(%rax)
/* save IRETQ frame */
popq %rdi
movq %rdi, 120(%rax)
popq %rdi
movq %rdi, 128(%rax)
popq %rdi
movq %rdi, 136(%rax)
popq %rdi
movq %rdi, 144(%rax)
popq %rdi
movq %rdi, 152(%rax)
movq 0(%rax), %rdi /* syscall number */
movq 40(%rax), %rsi /* arguments */
call syscall
call schedule
/* void return_from_irq(struct procregs *regs, paddr_t cr3); */
.global _return_to_user
_return_to_user:
@ -224,3 +288,7 @@ _return_to_user:
iretq
.global yield
yield:
hlt
jmp yield

View file

@ -66,42 +66,42 @@ pid_t elf_load_proc(char *fname)
struct proc *proc = proc_create();
/* allocate page tables */
proc->cr3 = (uintptr_t)buddy_alloc(0x1000) & (~hhdm_start);
proc->cr3 = (uintptr_t)buddy_alloc(0x1000);
/* map kernel pages */
memcpy((void *)proc->cr3, (void *)(kcr3 | hhdm_start), 0x1000);
proc->cr3 &= (~hhdm_start);
/* map text segment */
struct elf64_program_header *phdr = (struct elf64_program_header *)(buf + hdr->e_phoff);
for (int i = 0; i < hdr->e_phnum; i++) {
if (phdr[i].p_type == PT_LOAD) {
/* allocate memory for segment */
size_t psize = MAX(0x1000, npow2(phdr[i].p_memsz));
uintptr_t prog_buf = (uintptr_t)buddy_alloc(psize);
void *addr = proc_mmap(proc, prog_buf & (~hhdm_start), phdr[i].p_vaddr, psize, PAGE_RW | PAGE_PRESENT);
if (!addr) {
kfree(buf);
return -ENOMEM;
}
for (unsigned int i = 0; i < hdr->e_phnum; i++) {
if (phdr[i].p_type != PT_LOAD)
continue;
/* copy data from file to memory */
memcpy((void *)prog_buf, buf + phdr[i].p_offset, phdr[i].p_filesz);
/* zero out remaining memory */
memset((void *)prog_buf + phdr[i].p_filesz, 0, phdr[i].p_memsz - phdr[i].p_filesz);
}
uintptr_t vaddr = phdr[i].p_vaddr;
size_t len = MAX(0x1000, npow2(phdr[i].p_filesz));
uintptr_t paddr = (uintptr_t)buddy_alloc(len);
(void)proc_mmap(proc, paddr & (~hhdm_start), vaddr, len, PAGE_RW | PAGE_PRESENT | PAGE_USER);
memcpy((void *)paddr, buf + phdr[i].p_offset, len);
}
/* set entry point */
proc->regs.rip = hdr->e_entry;
proc->regs.rflags = 0x202; /* interrupts enabled, IOPL = 0 */
proc->regs.rflags = 0x242;
/* descriptor #7 is the user code segment */
proc->regs.cs = 7 * sizeof(struct gdt_desc) | 0x3;
proc->regs.ss = 8 * sizeof(struct gdt_desc) | 0x3;
proc->regs.cs = 0x38 | 3;
proc->regs.ss = 0x40 | 3;
ds_write(0x40 | 3);
/* allocate user stack */
uintptr_t stack_buf = (uintptr_t)buddy_alloc(0x1000);
uintptr_t u_stack_addr = 0x202020202000;
(void)proc_mmap(proc, stack_buf & (~hhdm_start), u_stack_addr, 0x1000, PAGE_RW | PAGE_PRESENT);
uintptr_t u_stack_addr = 0x20002000;
(void)proc_mmap(proc, stack_buf & (~hhdm_start), u_stack_addr, 0x1000, PAGE_RW | PAGE_PRESENT | PAGE_USER);
/* set stack pointer */
proc->regs.rsp = u_stack_addr + 0x1000;
proc->regs.rsp = u_stack_addr + 0xFF0;
kfree(buf);

View file

@ -8,10 +8,16 @@
static struct rbtree *proc_tree;
static pid_t *proc_current;
static struct proc kernel_procs[256] = { 0 };
void _return_to_user(struct procregs *regs, paddr_t cr3);
extern uintptr_t kstacks[256];
struct procregs *proc_current_regs()
{
if (proc_current[lapic_idno()] == 0)
return &kernel_procs[lapic_idno()].regs;
struct rbnode *proc_node = rbt_search(proc_tree, proc_current[lapic_idno()]);
if (proc_node) {
@ -23,8 +29,16 @@ struct procregs *proc_current_regs()
}
}
pid_t getpid()
{
return proc_current[lapic_idno()];
}
struct proc *proc_find(pid_t pid)
{
if (pid == 0)
return &kernel_procs[lapic_idno()];
struct rbnode *proc_node = rbt_search(proc_tree, pid);
if (proc_node)
@ -33,6 +47,26 @@ struct proc *proc_find(pid_t pid)
return NULL;
}
void proc_set_current(pid_t pid)
{
proc_current[lapic_idno()] = pid;
}
void proc_term(pid_t pid)
{
struct rbnode *proc_node = rbt_search(proc_tree, pid);
if (proc_node) {
struct proc *proc = (void *)proc_node->value;
rbt_delete(proc_tree, proc_node);
kfree(proc);
} else {
kprintf(LOG_ERROR "proc: proc_term: proc %u not found\n", pid);
panic();
}
}
void schedule()
{
uint8_t id = lapic_idno();
@ -79,15 +113,23 @@ void proc_init(unsigned num_cpus)
for (unsigned i = 0; i < num_cpus; i++) {
proc_current[i] = -1;
struct proc *proc = kzalloc(sizeof(struct proc), ALLOC_KERN);
struct proc *proc = &kernel_procs[i];
proc->pid = rbt_next_key(proc_tree);
proc->pid = 0;
proc->is_kernel = true;
proc->cr3 = kcr3;
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
proc_node->value = (uintptr_t)proc;
proc->regs.rip = (uint64_t)yield;
proc->regs.cs = 0x28;
proc->regs.rflags = 0x246;
proc->regs.rsp = kstacks[i] + 0xFF0;
proc_current[i] = proc->pid;
proc_current[i] = 0;
}
/* ensure that the first process has PID 0
* this value is never actually used, it prevents other processes from
* having PID 0
*/
(void)rbt_insert(proc_tree, 0);
}

50
sched/syscall.c Normal file
View file

@ -0,0 +1,50 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/proc.h>
#include <kernel/slab.h>
#include <kernel/rbtree.h>
#define SYS_EXIT 60
static struct rbtree *syscall_tree;
typedef void (*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
{
struct rbnode *node = rbt_search(syscall_tree, syscall_no);
if (node == NULL) {
return;
}
syscall_t syscall = (void *)node->value;
syscall(arg1, arg2, arg3, arg4, arg5);
}
void sys_exit(int status)
{
struct proc *proc = proc_find(getpid());
if (proc == NULL)
return;
proc->state = PROC_STOPPED;
proc_term(getpid());
kprintf("(syscall)Process %d exited with status 0x%x\n", getpid(), status);
proc_set_current(0);
}
static void syscall_insert(uint64_t syscall_no, syscall_t syscall)
{
struct rbnode *node = rbt_insert(syscall_tree, syscall_no);
node->value = (uintptr_t)syscall;
}
void syscall_init()
{
syscall_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN);
syscall_insert(SYS_EXIT, (syscall_t)sys_exit);
}