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

@ -281,6 +281,7 @@ int kcmd_exec(int argc, char **argv)
proc->state = PROC_RUNNING;
void _return_to_user(struct procregs *regs, paddr_t cr3);
proc_set_current(pid);
_return_to_user(&proc->regs, proc->cr3);
return 0;

View file

@ -34,13 +34,13 @@ uint16_t gdt_insert_tss(uintptr_t rsp0)
struct gdt_desc_sys *desc = (struct gdt_desc_sys *)((struct gdt_desc *)__gdt + gdt_index);
paddr_t tss_addr = (paddr_t)(tss_table + tss_index) & (~hhdm_start);
paddr_t tss_addr = (paddr_t)(tss_table + tss_index);
desc->limit = sizeof(struct tss) - 1;
desc->base = tss_addr & 0xFFFF;
desc->base = tss_addr & 0xFFFFFF;
desc->base_2 = (tss_addr >> 16) & 0xFF;
desc->base_3 = (tss_addr >> 24) & 0xFF;
desc->base_4 = (tss_addr >> 32) & 0xFFFFFFFF;
desc->access = 0x89;
desc->access = 0xE9;
desc->flags = 0x00;
tss_index += 1;

View file

@ -16,6 +16,8 @@
static bool irq_mapped[16] = { false };
static void *irq_handlers[16] = { NULL };
void gate_syscall();
exception_decl(0x00);
exception_decl(0x01);
exception_decl(0x02);
@ -119,12 +121,6 @@ void irq(int irq)
}
}
void gate_syscall(uint64_t rdi)
{
kprintf("System call: %X\n", rdi);
yield();
}
void idt_init()
{
idt_insert(0x00, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x00));
@ -177,7 +173,7 @@ void idt_init()
idt_insert(0x2E, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0E));
idt_insert(0x2F, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0F));
idt_insert(0x80, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, gate_syscall);
idt_insert(0x80, GATE_INTR_DPL3, 0, GDT_SEGMENT_CODE_RING0, gate_syscall);
irq_map(4, serial_trap);

View file

@ -41,20 +41,17 @@ uintptr_t bss_end;
void gdt_load();
void idt_load();
void yield();
void ap_kmain(struct limine_smp_info *info);
void load_stack_and_park(uintptr_t rsp, uintptr_t rbp);
uintptr_t kstacks[256] = { 0 };
void prompt()
{
kprintf(COL_YELLOW_BLACK "vbos # " COL_RESET);
}
void yield()
{
for (;;) {
__asm__("hlt");
}
}
void panic()
{
@ -159,7 +156,7 @@ void kmain()
char *root_path = kcmdline_get_symbol("root");
if (!root_path) {
kprintf(LOG_ERROR "No root= parameter specified\n");
kerror_print_blkdevs();
panic();
} else {
@ -184,7 +181,6 @@ void kmain()
/* init the application processors */
struct limine_smp_response *smp_resp = smp_req.response;
proc_init(smp_resp->cpu_count);
for (unsigned i = 0; i < smp_resp->cpu_count; i++) {
struct limine_smp_info *info = smp_resp->cpus[i];
@ -193,6 +189,9 @@ void kmain()
info->goto_address = ap_kmain;
}
proc_init(smp_resp->cpu_count);
void syscall_init();
syscall_init();
/* wait for the application processors to finish their initialization */
for (unsigned i = 0; i < 100000; i++)
@ -220,6 +219,8 @@ void ap_kmain(struct limine_smp_info *info)
void *kstack = buddy_alloc(KSTACK_SIZE);
uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8;
kstacks[info->lapic_id] = (uintptr_t)ptr;
/* insert the TSS for each processor into the GDT */
#ifdef KDEBUG
kprintf(LOG_DEBUG "Inserting TSS for AP #%d\n", info->lapic_id);