6125a84f5252d08d05306756febaa4cae0fe6710
[lunaix-os.git] / lunaix-os / arch / x86 / syscall64.S
1 #define __ASM__
2 #include <lunaix/syscall.h>
3 #include "syscall_nr.inc"
4 #include "asm/variants/interrupt64.S.inc"
5
6 .section .text
7     .type syscall_hndlr, @function
8     .global syscall_hndlr
9     syscall_hndlr:
10         pushq   %rbp
11         movq    %rsp, %rbp
12         pushq   %rbx
13         
14         movq    %rdi, %rbx          // struct hart_state*
15
16         movq    irax(%rbx), %rax          /* rax: call code as well as the return value from syscall */
17         cmpq    $__SYSCALL_MAX, %rax
18         jae     2f
19
20         shlq    $3, %rax               // %rax * 8
21         movabsq $syscall_table, %r8
22         addq    %r8, %rax
23         cmpq    $0, (%rax)
24         jne     1f
25     2:    
26         negq    %rax
27         popq    %rbx
28         movq    %rbp, %rsp
29         popq    %rbp
30
31         ret
32
33     1:
34         movq    (%rax),     %rdi
35         movq    irbx(%rbx), %rsi    /* rbx -> rsi #1 arg */
36         movq    ircx(%rbx), %rdx    /* rcx -> rdx #2 arg */
37         movq    irdx(%rbx), %rcx    /* rdx -> rcx #3 arg */
38         movq    irdi(%rbx), %r8     /* rdi -> r8  #4 arg */
39         movq    irsi(%rbx), %r9     /* rsi -> r9  #5 arg */
40         
41         call    dispatch_syscall
42         
43         movq    %rax, irax(%rbx)    /* save the return value */
44         
45         popq    %rbx
46         movq    %rbp,  %rsp
47         popq    %rbp
48         
49         ret