summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/sys/syscall.c
blob: 2a5ce08e1d15f32e80e0b75bc3bc799030a08963 (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
#include "arch/x86_64/hal/obj.h"
#include "arch/x86_64/proc/process.h"
#include "arch/x86_64/hal/smp.h"
#include "lib/printf.h"

#include "syscall.h"

syscall_handle_t s_arch_handles[128] = {
    NULL
};

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_getpid(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 %%rdx; \
                     pushq %%r11; \
                     pushq %%rcx; \
                     pushq %%rbp; \
                     callq syscall_handler; \
                     swapgs; \
                     xorq %%r10, %%r10; \
                     xorq %%r9, %%r9; \
                     xorq %%r8, %%r8; \
                     xorq %%rsi, %%rsi; \
                     xorq %%rdi, %%rdi; \
                     xorq %%rdx, %%rdx; \
                     popq %%rbp; \
                     popq %%rcx; \
                     popq %%r11; \
                     popq %%rsp; \
                     sysretq"
                     ::
                     [pcb] "i"(offsetof(processor_t, pcb)),
                     [ksp] "i"(offsetof(struct pcb_header, ksp))
                     : "memory");
}