Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
syscall.c
Go to the documentation of this file.
1#include "syscall.h"
2#include "hal/cpu/core.h"
3#include "hal/cpu/interrupt.h"
4#include "hal/cpu/msr.h"
5#include "hal/cpu/paging.h"
6#include "init/init.h"
7#include "libk/serial.h"
8#include "sys/err_no.h"
9
10// prototype
12extern void syscall_entry(void);
13
14static const char* get_syscall_name(int rax);
15void syscall_init(void) {
17 vxWRSR(MSR_EFER, efer | 1); // SCE bit
18
20
21 uint64_t star = 0;
22 star |= (uint64_t)0x0028 << 32; // kernel CS (0x28), kernel SS (0x30)
23 star |= (uint64_t)0x003B << 48; // user base for SYSRET (CS32 = 0x38, SS
24 // = +8 = 0x40, CS64 = +16 = 0x48)
25 vxWRSR(MSR_STAR, star);
26
27 vxWRSR(MSR_FMASK, (1 << 9)); // IF bit
28}
29
31 // #DEBUG
32 if (rsp->rax != SYSCALL_EXIT)
33 LOG2_DEBUG("syscall", "called %d (%s) 0x%x 0x%x %d", rsp->rax,
34 get_syscall_name((int)rsp->rax), rsp->rdi, rsp->rsi,
35 rsp->rdx);
36
37 auto int_no = rsp->rax;
38
39
40 // TODO: refactor this using array of linker section
41 switch (int_no) {
42 case SYSCALL_READ:
44 (int)rsp->rdi, (void*)rsp->rsi, (int)rsp->rdx);
45 break;
46 case SYSCALL_WRITE: {
48 (int)rsp->rdi, (void*)rsp->rsi, (int)rsp->rdx);
49 break;
50 }
51 case SYSCALL_OPEN: {
52 rsp->rax = (uint64_t)syscall_open((const char*)rsp->rdi,
53 (int)rsp->rsi, (int)rsp->rdx);
54 break;
55 }
56 case SYSCALL_CLOSE: {
57 rsp->rax = 0;
58 break;
59 }
60 case SYSCALL_ARCH_PRCTL: {
62 (int)rsp->rdi, (unsigned long)rsp->rsi);
63 break;
64 }
65 case SYSCALL_SET_TID: {
67 break;
68 }
69 case SYSCALL_EXIT: {
70 auto thr = get_current_core_data()->active_thread;
71 thr->state = THREAD_STATE_TERMINATED;
72 *thr->clear_child_tid = 0;
73 // TODO: clear allocated memory on heap and mmap
74 break;
75 }
76 case SYSCALL_IOCTL: {
77 rsp->rax = (uint64_t)syscall_ioctl((int)rsp->rdi, (uint32_t)rsp->rsi,
78 (void*)rsp->rdx);
79 break;
80 }
81 case SYSCALL_WRITEV: {
83 (int)rsp->rdi, (const struct iovec*)rsp->rsi,
84 (int)rsp->rdx);
85 break;
86 }
87 case SYSCALL_BRK: {
88 rsp->rax = (uint64_t)syscall_brk((void*)rsp->rdi);
89 break;
90 }
91 case SYSCALL_MMAP: {
93 (void*)rsp->rdi, (size_t)rsp->rsi, (int)rsp->rdx, (int)rsp->r10,
94 (int)rsp->r8, (int)rsp->r9);
95 break;
96 }
97 case SYSCALL_MPORTECT: {
99 (void*)rsp->rdi, (size_t)rsp->rsi, (int)rsp->rdx);
100 break;
101 }
102 case SYSCALL_EXIT_GROUP: {
103 syscall_exit_group((int)rsp->rdi);
104 break;
105 }
106 default:
107 serial2_printf("unknown syscall %d\n", int_no);
108 rsp->rax = (uint64_t)-ENOSYS;
109 break;
110 }
111}
112
113INIT(Syscall) { syscall_init(); }
114
115extern const char* syscall_names[335];
116static const char* get_syscall_name(int rax) {
117 int max_syscalls = sizeof(syscall_names) / sizeof(syscall_names[0]);
118
119 if (rax >= 0 && rax < max_syscalls && syscall_names[rax] != NULL) {
120 return syscall_names[rax];
121 }
122 return "UNKNOWN_SYSCALL / NOT_IMPLEMENTED";
123}
each_core_data * get_current_core_data(void)
Definition core.c:54
#define ENOSYS
Definition err_no.h:34
uint64_t rsp[3]
Definition gdt.h:1
@ THREAD_STATE_TERMINATED
Definition thread.h:26
#define INIT(fn)
Definition init.h:26
void serial2_printf(const char *fmt,...)
void vxWRSR(uint32_t msr, uint64_t value)
Definition msr.c:3
uint64_t vxRDMSR(uint32_t msr)
Definition msr.c:13
#define MSR_FMASK
Definition msr.h:8
#define MSR_EFER
Definition msr.h:9
#define MSR_STAR
Definition msr.h:6
#define MSR_LSTAR
Definition msr.h:7
#define LOG2_DEBUG(mod, fmt,...)
Definition serial.h:35
static const char * get_syscall_name(int rax)
Definition syscall.c:116
const char * syscall_names[335]
Definition syscall_def.c:1
void syscall_init(void)
Definition syscall.c:15
void syscall_entry(void)
void syscall_dispatch(interrupt_stack_frame_t *rsp)
Definition syscall.c:30
void * syscall_mmap(void *addr, size_t len, int prot, int flags, int fd, long off)
Definition mmap.c:100
#define SYSCALL_WRITEV
Definition syscall.h:19
#define SYSCALL_MMAP
Definition syscall.h:22
void syscall_exit_group(int status)
Definition exit_group.c:5
int syscall_mprotect(void *addr, size_t len, int prot)
Definition mprotect.c:9
#define SYSCALL_EXIT_GROUP
Definition syscall.h:20
#define SYSCALL_ARCH_PRCTL
Definition syscall.h:14
#define SYSCALL_SET_TID
Definition syscall.h:16
int syscall_open(const char *path, int flags, int mode)
Definition open.c:6
#define SYSCALL_MPORTECT
Definition syscall.h:23
#define SYSCALL_EXIT
Definition syscall.h:17
#define SYSCALL_CLOSE
Definition syscall.h:11
#define SYSCALL_WRITE
Definition syscall.h:9
#define SYSCALL_BRK
Definition syscall.h:21
pid_t syscall_set_tid(uint32_t tid)
Definition set_tid.c:5
int syscall_write(int fd, void *buf, long count)
Definition write.c:8
int syscall_arch_prctl(int code, unsigned long addr)
Definition arch_prctl.c:14
int syscall_read(int fd, void *buf, long count)
Definition read.c:9
#define SYSCALL_OPEN
Definition syscall.h:10
long syscall_writev(int fd, const struct iovec *iov, int iovcnt)
Definition writev.c:9
#define SYSCALL_READ
Definition syscall.h:8
int syscall_ioctl(int fd, uint32_t req, void *arg)
Definition ioctl.c:8
intptr_t syscall_brk(void *addr)
Definition brk.c:8
#define SYSCALL_IOCTL
Definition syscall.h:18
#define NULL
Definition type.h:76
unsigned int uint32_t
Definition type.h:19
unsigned long uint64_t
Definition type.h:25