A toy operating system written in C.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
freywaros/src/kernel/syscall.c

61 lines
1.5 KiB

#include "src/kernel/syscall.h"
#include "src/kernel/gdt.h"
#include "src/kernel/process.h"
#include "src/lib/layout.h"
#include "src/lib/util.h"
#define MSR_EFER 0xC0000080
#define MSR_STAR 0xC0000081
#define MSR_LSTAR 0xC0000082
#define MSR_FMASK 0xC0000084
static inline uint64_t rdmsr(uint32_t msr) {
uint32_t lo, hi;
__asm__ volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr));
return ((uint64_t)hi << 32) | lo;
}
static inline void wrmsr(uint32_t msr, uint64_t value) {
__asm__ volatile("wrmsr" : : "c"(msr), "a"((uint32_t)value), "d"((uint32_t)(value >> 32)));
}
extern void syscall_entry();
void syscall_init() {
wrmsr(MSR_EFER, rdmsr(MSR_EFER) | 0x01);
wrmsr(MSR_STAR, ((uint64_t)KERNEL_DS << 48) | ((uint64_t)KERNEL_CS << 32));
wrmsr(MSR_LSTAR, (uint64_t)syscall_entry);
wrmsr(MSR_FMASK, 0x200);
}
static void write(uint64_t fd, uint64_t data, uint64_t bytes) {
(void)fd;
current_process->stdout->write(current_process->stdout, (const char *)data, bytes);
}
static void exit() {
__asm__ volatile("mov %0, %%cr3" : : "r"(KERNEL_VIRTUAL_PML4) : "memory");
process_destroy(current_process);
current_process = NUL;
while (1)
;
// shutdown for now
// outw(0x604, 0x2000);
// __asm__ volatile("cli; hlt");
}
void syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3) {
switch (func) {
case SYSCALL_WRITE:
write(arg1, arg2, arg3);
break;
case SYSCALL_EXIT:
exit();
break;
default:
break;
}
}