1/*
2            Copyright Oliver Kowalke 2009.
3   Distributed under the Boost Software License, Version 1.0.
4      (See accompanying file LICENSE_1_0.txt or copy at
5            http://www.boost.org/LICENSE_1_0.txt)
6*/
7
8/****************************************************************************************
9 *                                                                                      *
10 *  ----------------------------------------------------------------------------------  *
11 *  |    0    |    1    |    2    |    3    |    4     |    5    |    6    |    7    |  *
12 *  ----------------------------------------------------------------------------------  *
13 *  |   0x0   |   0x4   |   0x8   |   0xc   |   0x10   |   0x14  |   0x18  |   0x1c  |  *
14 *  ----------------------------------------------------------------------------------  *
15 *  | fc_mxcsr|fc_x87_cw|       guard       |         R12        |        R13        |  *
16 *  ----------------------------------------------------------------------------------  *
17 *  ----------------------------------------------------------------------------------  *
18 *  |    8    |    9    |   10    |   11    |    12    |    13   |    14   |    15   |  *
19 *  ----------------------------------------------------------------------------------  *
20 *  |   0x20  |   0x24  |   0x28  |  0x2c   |   0x30   |   0x34  |   0x38  |   0x3c  |  *
21 *  ----------------------------------------------------------------------------------  *
22 *  |        R14        |        R15        |         RBX        |        RBP        |  *
23 *  ----------------------------------------------------------------------------------  *
24 *  ----------------------------------------------------------------------------------  *
25 *  |   16    |   17    |   18    |   19    |    20    |    21   |    22   |    23   |  *
26 *  ----------------------------------------------------------------------------------  *
27 *  |   0x40  |   0x44  |                                                            |  *
28 *  ----------------------------------------------------------------------------------  *
29 *  |        RIP        |                                                            |  *
30 *  ----------------------------------------------------------------------------------  *
31 *                                                                                      *
32 ****************************************************************************************/
33
34# if defined __CET__
35#  include <cet.h>
36#  define SHSTK_ENABLED (__CET__ & 0x2)
37#  define BOOST_CONTEXT_SHADOW_STACK (SHSTK_ENABLED && SHADOW_STACK_SYSCALL)
38# else
39#  define _CET_ENDBR
40# endif
41.file "jump_x86_64_sysv_elf_gas.S"
42.text
43.globl jump_fcontext
44.type jump_fcontext,@function
45.align 16
46jump_fcontext:
47    _CET_ENDBR
48    leaq  -0x40(%rsp), %rsp /* prepare stack */
49
50#if !defined(BOOST_USE_TSX)
51    stmxcsr  (%rsp)     /* save MMX control- and status-word */
52    fnstcw   0x4(%rsp)  /* save x87 control-word */
53#endif
54
55#if defined(BOOST_CONTEXT_TLS_STACK_PROTECTOR)
56    movq  %fs:0x28, %rcx    /* read stack guard from TLS record */
57    movq  %rcx, 0x8(%rsp)   /* save stack guard */
58#endif
59
60    movq  %r12, 0x10(%rsp)  /* save R12 */
61    movq  %r13, 0x18(%rsp)  /* save R13 */
62    movq  %r14, 0x20(%rsp)  /* save R14 */
63    movq  %r15, 0x28(%rsp)  /* save R15 */
64    movq  %rbx, 0x30(%rsp)  /* save RBX */
65    movq  %rbp, 0x38(%rsp)  /* save RBP */
66
67#if BOOST_CONTEXT_SHADOW_STACK
68    /* grow the stack to reserve space for shadow stack pointer(SSP) */
69    leaq  -0x8(%rsp), %rsp
70    /* read the current SSP and store it */
71    rdsspq  %rcx
72    movq  %rcx, (%rsp)
73#endif
74
75#if BOOST_CONTEXT_SHADOW_STACK
76    /* grow the stack to reserve space for shadow stack pointer(SSP) */
77    leaq  -0x8(%rsp), %rsp
78    /* read the current SSP and store it */
79    rdsspq  %rcx
80    movq  %rcx, (%rsp)
81# endif
82
83    /* store RSP (pointing to context-data) in RAX */
84    movq  %rsp, %rax
85
86    /* restore RSP (pointing to context-data) from RDI */
87    movq  %rdi, %rsp
88
89#if BOOST_CONTEXT_SHADOW_STACK
90    /* first 8 bytes are SSP */
91    movq  (%rsp), %rcx
92    leaq  0x8(%rsp), %rsp
93
94    /* Restore target(new) shadow stack */
95    rstorssp  -8(%rcx)
96    /* restore token for previous shadow stack is pushed */
97    /* on previous shadow stack after saveprevssp */
98    saveprevssp
99
100    /* when return, jump_fcontext jump to restored return address */
101    /* (r8) instead of RET. This miss of RET implies us to unwind */
102    /* shadow stack accordingly. Otherwise mismatch occur */
103    movq  $1, %rcx
104    incsspq  %rcx
105#endif
106
107    movq  0x40(%rsp), %r8  /* restore return-address */
108
109#if !defined(BOOST_USE_TSX)
110    ldmxcsr  (%rsp)     /* restore MMX control- and status-word */
111    fldcw    0x4(%rsp)  /* restore x87 control-word */
112#endif
113
114#if defined(BOOST_CONTEXT_TLS_STACK_PROTECTOR)
115    movq  0x8(%rsp), %rdx  /* load stack guard */
116    movq  %rdx, %fs:0x28   /* restore stack guard to TLS record */
117#endif
118
119    movq  0x10(%rsp), %r12  /* restore R12 */
120    movq  0x18(%rsp), %r13  /* restore R13 */
121    movq  0x20(%rsp), %r14  /* restore R14 */
122    movq  0x28(%rsp), %r15  /* restore R15 */
123    movq  0x30(%rsp), %rbx  /* restore RBX */
124    movq  0x38(%rsp), %rbp  /* restore RBP */
125
126    leaq  0x48(%rsp), %rsp /* prepare stack */
127
128    /* return transfer_t from jump */
129#if !defined(_ILP32)
130    /* RAX == fctx, RDX == data */
131    movq  %rsi, %rdx
132#else
133    /* RAX == data:fctx */
134    salq  $32, %rsi
135    orq   %rsi, %rax
136#endif
137    /* pass transfer_t as first arg in context function */
138#if !defined(_ILP32)
139    /* RDI == fctx, RSI == data */
140#else
141    /* RDI == data:fctx */
142#endif
143    movq  %rax, %rdi
144
145    /* indirect jump to context */
146    jmp  *%r8
147.size jump_fcontext,.-jump_fcontext
148
149/* Mark that we don't need executable stack.  */
150.section .note.GNU-stack,"",%progbits
151