C userspace programs
This commit is contained in:
@ -6,8 +6,6 @@
|
||||
#include "hal/hal.h"
|
||||
#include "bootinfo/bootinfo.h"
|
||||
|
||||
LocalCpuData HAL_CPUS[HAL_CPUS_MAX];
|
||||
|
||||
uint64_t hal_cpu_rdmsr(uint32_t id) {
|
||||
uint32_t lo, hi;
|
||||
asm volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(id));
|
||||
@ -21,8 +19,3 @@ uint64_t hal_cpu_wrmsr(uint32_t id, uint64_t val) {
|
||||
return val;
|
||||
}
|
||||
|
||||
void hal_cpu_init(uint32_t cpu) {
|
||||
void *addr = pmm_alloc(16) + (16 * HAL_PAGE_SIZE);
|
||||
HAL_CPUS[cpu].syscall_kstack = (uint64_t *)VIRT(addr);
|
||||
HAL_CPUS[cpu].kcr3 = hal_vmm_current_cr3();
|
||||
}
|
||||
|
@ -13,24 +13,13 @@
|
||||
#define HAL_CPU_STAR 0xC0000081
|
||||
#define HAL_CPU_LSTAR 0xC0000082
|
||||
#define HAL_CPU_CSTAR 0xC0000083
|
||||
#define HAL_CPU_SFMASK 0xC0000084
|
||||
#define HAL_CPU_FMASK 0xC0000084
|
||||
|
||||
#define HAL_CPU_FSBASE 0xC0000100
|
||||
#define HAL_CPU_GSBASE 0xC0000101
|
||||
#define HAL_CPU_KGSBASE 0xC0000102
|
||||
|
||||
typedef struct {
|
||||
uint64_t *syscall_kstack;
|
||||
uint64_t *syscall_ustack;
|
||||
PgTable *kcr3;
|
||||
PgTable *pcr3;
|
||||
IntrStackFrame *frame;
|
||||
} PACKED LocalCpuData;
|
||||
|
||||
uint64_t hal_cpu_rdmsr(uint32_t id);
|
||||
uint64_t hal_cpu_wrmsr(uint32_t id, uint64_t val);
|
||||
void hal_cpu_init(uint32_t cpu);
|
||||
|
||||
extern LocalCpuData HAL_CPUS[HAL_CPUS_MAX];
|
||||
|
||||
#endif // HAL_CPU_H_
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include "pic.h"
|
||||
#include "apic.h"
|
||||
#include "pit.h"
|
||||
#include "syscall.h"
|
||||
|
||||
void hal_init(void) {
|
||||
if (!serial_init()) {
|
||||
@ -33,7 +32,6 @@ void hal_init_withmalloc(void) {
|
||||
pit_init();
|
||||
ioapic_setentry(IOAPIC, acpi_remapirq(0x00), INTR_TIMER);
|
||||
hal_intr_disable();
|
||||
hal_syscall_init();
|
||||
}
|
||||
|
||||
void hal_wait(uint32_t ms) {
|
||||
|
@ -11,6 +11,8 @@
|
||||
#include "apic.h"
|
||||
#include "pit.h"
|
||||
#include "proc/proc.h"
|
||||
#include "syscall/syscall.h"
|
||||
#include "errors.h"
|
||||
|
||||
void hal_intr_disable(void) {
|
||||
asm volatile("cli");
|
||||
@ -122,6 +124,9 @@ void intr_init(void) {
|
||||
MKINTR(45, 0);
|
||||
MKINTR(46, 0);
|
||||
MKINTR(47, 0);
|
||||
|
||||
extern void intr_vec128(void);
|
||||
idt_setentry(0x80, (uint64_t)&intr_vec128, 0, 0xEE);
|
||||
|
||||
idt_init();
|
||||
}
|
||||
@ -149,14 +154,33 @@ void intr_dumpframe(IntrStackFrame *frame) {
|
||||
);
|
||||
}
|
||||
|
||||
void hal_syscalldispatch(IntrStackFrame *frame) {
|
||||
uint64_t sysnum = frame->regs.rax;
|
||||
if (sysnum < SYSCALLS_MAX) {
|
||||
SyscallFn fn = SYSCALL_TABLE[sysnum];
|
||||
if (fn == NULL) {
|
||||
frame->regs.rax = E_BADSYSCALL;
|
||||
return;
|
||||
}
|
||||
int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx,
|
||||
frame->regs.r10, frame->regs.r8, frame->regs.r9);
|
||||
|
||||
if (sysnum == SYS_QUITPROC) {
|
||||
proc_sched((void *)frame);
|
||||
}
|
||||
frame->regs.rax = *(uint64_t *)&ret;
|
||||
}
|
||||
}
|
||||
|
||||
void intr_handleintr(IntrStackFrame *frame) {
|
||||
if (frame->trapnum <= 31) {
|
||||
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
|
||||
intr_dumpframe(frame);
|
||||
if (frame->trapnum == 14 && frame->errnum & 0x4) {
|
||||
kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name);
|
||||
proc_killself();
|
||||
proc_sched((void *)frame);
|
||||
}
|
||||
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
|
||||
intr_dumpframe(frame);
|
||||
hal_hang();
|
||||
} else if (frame->trapnum >= 32 && frame->trapnum <= 47) {
|
||||
if (frame->trapnum == INTR_TIMER) {
|
||||
@ -167,6 +191,8 @@ void intr_handleintr(IntrStackFrame *frame) {
|
||||
lapic_write(LAPIC_EOI, 0x00);
|
||||
|
||||
proc_sched((void *)frame);
|
||||
} else if (frame->trapnum == 0x80) {
|
||||
hal_syscalldispatch(frame);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,6 +50,7 @@
|
||||
.global intr_vec45
|
||||
.global intr_vec46
|
||||
.global intr_vec47
|
||||
.global intr_vec128
|
||||
|
||||
.macro _vecintr_errorcode_present_save num
|
||||
pushq $\num
|
||||
@ -215,3 +216,6 @@ intr_vec46:
|
||||
intr_vec47:
|
||||
_vecintr_plain_save 47
|
||||
_vecintr_bodygen
|
||||
intr_vec128:
|
||||
_vecintr_plain_save 128
|
||||
_vecintr_bodygen
|
||||
|
@ -1,43 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include "syscall.h"
|
||||
#include "cpu.h"
|
||||
#include "intr.h"
|
||||
#include "syscall/syscall.h"
|
||||
#include "proc/proc.h"
|
||||
#include "kprintf.h"
|
||||
#include "errors.h"
|
||||
|
||||
extern void hal_enable_syscallentry(void);
|
||||
extern void hal_syscallentry(void);
|
||||
|
||||
void hal_syscalldispatch(IntrStackFrame *frame) {
|
||||
uint64_t sysnum = frame->regs.rax;
|
||||
if (sysnum < SYSCALLS_MAX) {
|
||||
SyscallFn fn = SYSCALL_TABLE[sysnum];
|
||||
if (fn == NULL) {
|
||||
frame->regs.rax = E_BADSYSCALL;
|
||||
return;
|
||||
}
|
||||
int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx,
|
||||
frame->regs.r10, frame->regs.r8, frame->regs.r9);
|
||||
|
||||
if (sysnum == SYS_QUITPROC) {
|
||||
proc_sched((void *)frame);
|
||||
}
|
||||
frame->regs.rax = *(uint64_t *)&ret;
|
||||
}
|
||||
}
|
||||
|
||||
void hal_syscall_init(void) {
|
||||
hal_cpu_init(0);
|
||||
LocalCpuData *lcd = &HAL_CPUS[0];
|
||||
|
||||
hal_cpu_wrmsr(HAL_CPU_EFER, hal_cpu_rdmsr(HAL_CPU_EFER) | 0x1);
|
||||
hal_enable_syscallentry();
|
||||
|
||||
hal_cpu_wrmsr(HAL_CPU_GSBASE, (uint64_t)lcd);
|
||||
hal_cpu_wrmsr(HAL_CPU_KGSBASE, (uint64_t)lcd);
|
||||
hal_cpu_wrmsr(HAL_CPU_SFMASK, (uint64_t)0);
|
||||
|
||||
hal_cpu_wrmsr(HAL_CPU_LSTAR, (uint64_t)&hal_syscallentry);
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
#ifndef HAL_SYSCALL_H_
|
||||
#define HAL_SYSCALL_H_
|
||||
|
||||
void hal_syscall_init(void);
|
||||
|
||||
#endif // HAL_SYSCALL_H_
|
@ -1,48 +0,0 @@
|
||||
#include "regs.S"
|
||||
|
||||
.extern hal_syscalldispatch
|
||||
|
||||
.global hal_enable_syscallentry
|
||||
hal_enable_syscallentry:
|
||||
movq $0xC0000080, %rcx
|
||||
rdmsr
|
||||
or $0x1, %eax
|
||||
wrmsr
|
||||
movq $0xC0000081, %rcx
|
||||
rdmsr
|
||||
movl $0x180008, %edx
|
||||
wrmsr
|
||||
ret
|
||||
|
||||
.global hal_syscallentry
|
||||
hal_syscallentry:
|
||||
cli
|
||||
swapgs
|
||||
|
||||
movq %rsp, %gs:0x8
|
||||
movq %gs:0x0, %rsp
|
||||
|
||||
pushq $0x23
|
||||
pushq %gs:0x8
|
||||
|
||||
swapgs
|
||||
|
||||
pushq %r11
|
||||
pushq $0x1b
|
||||
pushq %rcx
|
||||
|
||||
_push_regs
|
||||
|
||||
mov %rsp, %rdi
|
||||
mov $0x0, %rbp
|
||||
|
||||
call hal_syscalldispatch
|
||||
cli
|
||||
|
||||
_pop_regs
|
||||
|
||||
popq %rcx
|
||||
add $0x8, %rsp
|
||||
popq %r11
|
||||
popq %rsp
|
||||
sysret
|
Reference in New Issue
Block a user