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    /* store RSP (pointing to context-data) in RAX */
76    movq  %rsp, %rax
77
78    /* restore RSP (pointing to context-data) from RDI */
79    movq  %rdi, %rsp
80
81#if BOOST_CONTEXT_SHADOW_STACK
82    /* first 8 bytes are SSP */
83    movq  (%rsp), %rcx
84    leaq  0x8(%rsp), %rsp
85
86    /* Restore target(new) shadow stack */
87    rstorssp  -8(%rcx)
88    /* restore token for previous shadow stack is pushed */
89    /* on previous shadow stack after saveprevssp */
90    saveprevssp
91
92    /* when return, jump_fcontext jump to restored return address */
93    /* (r8) instead of RET. This miss of RET implies us to unwind */
94    /* shadow stack accordingly. Otherwise mismatch occur */
95    movq  $1, %rcx
96    incsspq  %rcx
97#endif
98
99    movq  0x40(%rsp), %r8  /* restore return-address */
100
101#if !defined(BOOST_USE_TSX)
102    ldmxcsr  (%rsp)     /* restore MMX control- and status-word */
103    fldcw    0x4(%rsp)  /* restore x87 control-word */
104#endif
105
106#if defined(BOOST_CONTEXT_TLS_STACK_PROTECTOR)
107    movq  0x8(%rsp), %rdx  /* load stack guard */
108    movq  %rdx, %fs:0x28   /* restore stack guard to TLS record */
109#endif
110
111    movq  0x10(%rsp), %r12  /* restore R12 */
112    movq  0x18(%rsp), %r13  /* restore R13 */
113    movq  0x20(%rsp), %r14  /* restore R14 */
114    movq  0x28(%rsp), %r15  /* restore R15 */
115    movq  0x30(%rsp), %rbx  /* restore RBX */
116    movq  0x38(%rsp), %rbp  /* restore RBP */
117
118    leaq  0x48(%rsp), %rsp /* prepare stack */
119
120    /* return transfer_t from jump */
121#if !defined(_ILP32)
122    /* RAX == fctx, RDX == data */
123    movq  %rsi, %rdx
124#else
125    /* RAX == data:fctx */
126    salq  $32, %rsi
127    orq   %rsi, %rax
128#endif
129    /* pass transfer_t as first arg in context function */
130#if !defined(_ILP32)
131    /* RDI == fctx, RSI == data */
132#else
133    /* RDI == data:fctx */
134#endif
135    movq  %rax, %rdi
136
137    /* indirect jump to context */
138    jmp  *%r8
139.size jump_fcontext,.-jump_fcontext
140
141/* Mark that we don't need executable stack.  */
142.section .note.GNU-stack,"",%progbits
143