1/*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <asm.h>
25
26#define SYSCALL_ENTRY_SIZE    0x08
27
28FUNCTION (x86_syscall)
29    /* switch to kernel gs */
30    swapgs
31
32    sti
33    pushq %rbx /* Ring 3 return address */
34    pushq %rbp /* Ring 3 RSP */
35
36    pushq %r15
37    pushq %r14
38    pushq %r13
39    pushq %r12
40    pushq %r11
41    pushq %r10
42    pushq %r9
43    pushq %r8
44    pushq %rcx
45    pushq %rdx
46    pushq %rbx
47    pushq %rbp
48    pushq %rsi
49    pushq %rdi
50
51    movq %rdx,%r9
52    /*
53     * Arranging params to pass to sysenter handling
54     * syscall table is long. Multiplication to find offset.
55     * Multiplier different for 32 and 64
56     */
57    movq $SYSCALL_ENTRY_SIZE, %r15
58    /* Result is stored in RAX. Before multiplication RAX contains syscall No. */
59
60    mul %r15
61    movq $syscall_table, %r14
62    add %r14, %rax
63    movq %r9,%rdx
64    call *(%rax)
65
66    popq %rdi
67    popq %rsi
68    popq %rbp
69    popq %rbx
70    popq %rdx
71    popq %rcx
72    popq %r8
73    popq %r9
74    popq %r10
75    popq %r11
76    popq %r12
77    popq %r13
78    popq %r14
79    popq %r15
80
81    popq %rcx /* pop RSP */
82    popq %rdx /* pop RIP */
83
84    /*
85     * switch to user gs
86     * Have to make sure there is no interrupt triggered between swapgs and sysexit instructions,
87     * if this happens, GS.base is switch to user level GS.base, but current context is still in
88     * kernel level, it leads to get global states failure on current processor.
89     */
90    cli
91    swapgs
92    sti
93
94    sysexitq
95