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