summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/sys/syscall.c
blob: a694674025bd1d6d2e6762709d5a944a92ceae9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "arch/x86_64/hal/obj.h"
#include "arch/x86_64/hal/process.h"
#include "arch/x86_64/hal/smp.h"
#include "lib/printf.h"

#include "syscall.h"

syscall_handle_t s_arch_handles[128] = {
    [JOVE_OBJ_SERIAL_COM-0x80] = _syscall_handle_serial
};

int
_syscall_handle_arch(syscall_context_t *ctx)
{
    uint8_t target_type = obj_gettype(ctx->target.obj) - 0x80;
    syscall_handle_t call_handle = s_arch_handles[target_type];
    if(call_handle == NULL) {
        kprintf("syscall: Process %i tried to call syscall on object %P which doesn't support syscalls!\n",
                    pcb_pid(obj_deref(ctx->pcb)), ctx->header->path, ctx->header->pathlen);
        return -EBADOBJ;
    }

    return call_handle(ctx);
}

__attribute__((naked))
void _syscall_entry(void)
{
    __asm__ volatile(" \
                     swapgs; \
                     movq %%gs:%c[pcb], %%rax; \
                     movq %%rsp, %%rdx; \
                     movq %c[ksp](%%rax), %%rsp; \
                     pushq %%rcx; \
                     pushq %%rdx; \
                     pushq %%r11; \
                     pushq %%rbp; \
                     callq syscall_handler; \
                     swapgs; \
                     xorq %%r15, %%r15; \
                     xorq %%r14, %%r14; \
                     xorq %%r13, %%r13; \
                     xorq %%r12, %%r12; \
                     xorq %%r10, %%r10; \
                     xorq %%r9, %%r9; \
                     xorq %%r8, %%r8; \
                     xorq %%rsi, %%rsi; \
                     xorq %%rdi, %%rdi; \
                     xorq %%rbp, %%rbp; \
                     xorq %%rdx, %%rdx; \
                     xorq %%rcx, %%rcx; \
                     xorq %%rbx, %%rbx; \
                     popq %%rbp; \
                     popq %%r11; \
                     popq %%rsp; \
                     popq %%rcx; \
                     sysretq;"
                     ::
                     [pcb] "i"(offsetof(processor_t, pcb)),
                     [ksp] "i"(offsetof(pcb_t, ksp)));
}