vbos/sched/load.c
Michael Shaw 37518aea81 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>
2023-11-20 22:07:35 -05:00

109 lines
2.4 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
/* ELF loader */
#include <kernel/elf.h>
#include <kernel/slab.h>
#include <kernel/proc.h>
#include <kernel/gdt.h>
#include <fs/vfs.h>
#include <string.h>
static const char elf_magic[] = { 0x7F, 'E', 'L', 'F' };
int elf_check_compat(char *buf)
{
if (strncmp(buf, elf_magic, 4))
return -ENOEXEC;
struct elf64_header *hdr = (struct elf64_header *)buf;
/* must be 64-bit */
if (hdr->e_ident[EI_CLASS] != ELFCLASS64)
return -ENOEXEC;
/* must be little endian */
if (hdr->e_ident[EI_DATA] != ELFDATA2LSB)
return -ENOEXEC;
/* must be executable */
if (hdr->e_type != ET_EXEC)
return -ENOEXEC;
/* must be x86_64 */
if (hdr->e_machine != EM_X86_64)
return -ENOEXEC;
return 0;
}
pid_t elf_load_proc(char *fname)
{
int fd = open(fname, O_RDONLY);
if (fd < 0)
return fd;
seek(fd, 0, SEEK_END);
size_t size = tell(fd);
seek(fd, 0, SEEK_SET);
char *buf = kmalloc(size, ALLOC_KERN);
if (!buf)
return -ENOMEM;
read(fd, buf, size);
close(fd);
int ret;
if ((ret = elf_check_compat(buf))) {
kfree(buf);
return ret;
}
struct elf64_header *hdr = (struct elf64_header *)buf;
/* read data and text segments */
struct proc *proc = proc_create();
/* allocate page tables */
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 (unsigned int i = 0; i < hdr->e_phnum; i++) {
if (phdr[i].p_type != PT_LOAD)
continue;
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 = 0x242;
/* descriptor #7 is the user code segment */
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 = 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 + 0xFF0;
kfree(buf);
return proc->pid;
}