summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/sys/syscall.c
blob: b2d3e7f7676815d262840b63a85300a7edfcba4e (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
62
63
64
65
66
67
68
69
70
71
#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(
        jove_owned_obj_t *pcb_obj,
        jove_owned_obj_t *objdir,
        jove_owned_obj_t *message_obj,
        jove_owned_obj_t *target_obj,
        struct syscall_header *header,
        size_t bytes_left,
        void *payload)
{
    uint8_t target_type = obj_gettype(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(pcb_obj)), header->path, header->pathlen);
        return EBADOBJ;
    }

    return call_handle(pcb_obj, objdir, message_obj, target_obj, header, bytes_left, payload);
}

__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 %%rbx; \
                     pushq %%rcx; \
                     pushq %%rbp; \
                     pushq %%r12; \
                     pushq %%r13; \
                     pushq %%r14; \
                     pushq %%r15; \
                     callq syscall_handler; \
                     swapgs; \
                     popq %%r15; \
                     popq %%r14; \
                     popq %%r13; \
                     popq %%r12; \
                     popq %%rbp; \
                     popq %%rcx; \
                     popq %%rbx; \
                     xorq %%rdx, %%rdx; \
                     xorq %%rsi, %%rsi; \
                     xorq %%rdi, %%rdi; \
                     xorq %%r8, %%r8; \
                     xorq %%r9, %%r9; \
                     xorq %%r10, %%r10; \
                     popq %%r11; \
                     popq %%rsp; \
                     sysretq;"
                     ::
                     [pcb] "i"(offsetof(processor_t, pcb)),
                     [ksp] "i"(offsetof(pcb_t, ksp)));
}