Second Extended Filesystem (ext2) and other improvements (#33)
[lunaix-os.git] / lunaix-os / arch / x86 / syscall64.S
1 #define __ASM__
2 #include <lunaix/syscall.h>
3 #include "sys/interrupt64.S.inc"
4
5 .section .data
6     /*
7         注意,这里的顺序非常重要。每个系统调用在这个地址表里的索引等于其调用号。
8     */
9     syscall_table:
10         1:
11         .8byte 0
12         .8byte __lxsys_fork          /* 1 */
13         .8byte __lxsys_yield
14         .8byte __lxsys_sbrk
15         .8byte __lxsys_brk
16         .8byte __lxsys_getpid        /* 5 */
17         .8byte __lxsys_getppid
18         .8byte __lxsys_sleep
19         .8byte __lxsys_exit
20         .8byte __lxsys_wait          
21         .8byte __lxsys_waitpid       /* 10 */
22         .8byte __lxsys_sigreturn
23         .8byte __lxsys_sigprocmask
24         .8byte __lxsys_sys_sigaction
25         .8byte __lxsys_pause
26         .8byte __lxsys_kill          /* 15 */
27         .8byte __lxsys_alarm
28         .8byte __lxsys_sigpending
29         .8byte __lxsys_sigsuspend
30         .8byte __lxsys_open
31         .8byte __lxsys_close         /* 20 */
32         .8byte __lxsys_read
33         .8byte __lxsys_write
34         .8byte __lxsys_sys_readdir
35         .8byte __lxsys_mkdir
36         .8byte __lxsys_lseek         /* 25 */
37         .8byte __lxsys_geterrno
38         .8byte __lxsys_readlink
39         .8byte __lxsys_readlinkat
40         .8byte __lxsys_rmdir
41         .8byte __lxsys_unlink        /* 30 */
42         .8byte __lxsys_unlinkat
43         .8byte __lxsys_link
44         .8byte __lxsys_fsync
45         .8byte __lxsys_dup
46         .8byte __lxsys_dup2          /* 35 */
47         .8byte __lxsys_realpathat
48         .8byte __lxsys_symlink
49         .8byte __lxsys_chdir
50         .8byte __lxsys_fchdir
51         .8byte __lxsys_getcwd        /* 40 */
52         .8byte __lxsys_rename
53         .8byte __lxsys_mount
54         .8byte __lxsys_unmount
55         .8byte __lxsys_getxattr
56         .8byte __lxsys_setxattr      /* 45 */
57         .8byte __lxsys_fgetxattr
58         .8byte __lxsys_fsetxattr
59         .8byte __lxsys_ioctl
60         .8byte __lxsys_getpgid
61         .8byte __lxsys_setpgid       /* 50 */
62         .8byte __lxsys_syslog
63         .8byte __lxsys_sys_mmap
64         .8byte __lxsys_munmap
65         .8byte __lxsys_execve
66         .8byte __lxsys_fstat         /* 55 */
67         .8byte __lxsys_pollctl
68         .8byte __lxsys_th_create
69         .8byte __lxsys_th_self
70         .8byte __lxsys_th_exit
71         .8byte __lxsys_th_join       /* 60 */
72         .8byte __lxsys_th_kill
73         .8byte __lxsys_th_detach
74         .8byte __lxsys_th_sigmask
75         2:
76         .rept __SYSCALL_MAX - (2b - 1b) / 8
77             .8byte 0
78         .endr
79
80
81 .section .text
82     .type syscall_hndlr, @function
83     .global syscall_hndlr
84     syscall_hndlr:
85         pushq   %rbp
86         movq    %rsp, %rbp
87         pushq   %rbx
88         
89         movq    %rdi, %rbx          // struct hart_state*
90
91         movq    irax(%rbx), %rax          /* rax: call code as well as the return value from syscall */
92         cmpq    $__SYSCALL_MAX, %rax
93         jae     2f
94
95         shlq    $3, %rax               // %rax * 8
96         movabsq $syscall_table, %r8
97         addq    %r8, %rax
98         cmpq    $0, (%rax)
99         jne     1f
100     2:    
101         negq    %rax
102         popq    %rbx
103         movq    %rbp, %rsp
104         popq    %rbp
105
106         ret
107
108     1:
109         movq    (%rax),     %rdi
110         movq    irbx(%rbx), %rsi    /* rbx -> rsi #1 arg */
111         movq    ircx(%rbx), %rdx    /* rcx -> rdx #2 arg */
112         movq    irdx(%rbx), %rcx    /* rdx -> rcx #3 arg */
113         movq    irdi(%rbx), %r8     /* rdi -> r8  #4 arg */
114         movq    irsi(%rbx), %r9     /* rsi -> r9  #5 arg */
115         
116         call    dispatch_syscall
117         
118         movq    %rax, irax(%rbx)    /* save the return value */
119         
120         popq    %rbx
121         movq    %rbp,  %rsp
122         popq    %rbp
123         
124         ret