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 "make_x86_64_sysv_elf_gas.S"
42.text
43.globl make_fcontext
44.type make_fcontext,@function
45.align 16
46make_fcontext:
47    _CET_ENDBR
48#if BOOST_CONTEXT_SHADOW_STACK
49    /* the new shadow stack pointer (SSP) */
50    movq  -0x8(%rdi), %r9
51#endif
52
53    /* first arg of make_fcontext() == top of context-stack */
54    movq  %rdi, %rax
55
56    /* shift address in RAX to lower 16 byte boundary */
57    andq  $-16, %rax
58
59    /* reserve space for context-data on context-stack */
60    /* on context-function entry: (RSP -0x8) % 16 == 0 */
61    leaq  -0x48(%rax), %rax
62
63    /* third arg of make_fcontext() == address of context-function */
64    /* stored in RBX */
65    movq  %rdx, 0x30(%rax)
66
67    /* save MMX control- and status-word */
68    stmxcsr  (%rax)
69    /* save x87 control-word */
70    fnstcw   0x4(%rax)
71
72#if defined(BOOST_CONTEXT_TLS_STACK_PROTECTOR)
73    /* save stack guard */
74    movq  %fs:0x28, %rcx    /* read stack guard from TLS record */
75    movq  %rcx, 0x8(%rsp)   /* save stack guard */
76#endif
77
78    /* compute abs address of label trampoline */
79    leaq  trampoline(%rip), %rcx
80    /* save address of trampoline as return-address for context-function */
81    /* will be entered after calling jump_fcontext() first time */
82    movq  %rcx, 0x40(%rax)
83
84    /* compute abs address of label finish */
85    leaq  finish(%rip), %rcx
86    /* save address of finish as return-address for context-function */
87    /* will be entered after context-function returns */
88    movq  %rcx, 0x38(%rax)
89
90#if BOOST_CONTEXT_SHADOW_STACK
91    /* Populate the shadow stack and normal stack */
92    /* get original SSP */
93    rdsspq  %r8
94    /* restore new shadow stack */
95    rstorssp  -0x8(%r9)
96    /* save the restore token on the original shadow stack */
97    saveprevssp
98    /* push the address of "jmp trampoline" to the new shadow stack */
99    /* as well as the stack */
100    call  1f
101    jmp  trampoline
1021:
103    /* save address of "jmp trampoline" as return-address */
104    /* for context-function */
105    pop 0x38(%rax)
106    /* Get the new SSP.  */
107    rdsspq  %r9
108    /* restore original shadow stack */
109    rstorssp  -0x8(%r8)
110    /* save the restore token on the new shadow stack.  */
111    saveprevssp
112
113    /* reserve space for the new SSP */
114    leaq  -0x8(%rax), %rax
115    /* save the new SSP to this fcontext */
116    movq  %r9, (%rax)
117#endif
118
119    ret /* return pointer to context-data */
120
121trampoline:
122    _CET_ENDBR
123    /* store return address on stack */
124    /* fix stack alignment */
125#if BOOST_CONTEXT_SHADOW_STACK
126    /* save address of "jmp *%rbp" as return-address */
127    /* on stack and shadow stack */
128    call  2f
129    jmp  *%rbp
1302:
131#else
132    push %rbp
133#endif
134    /* jump to context-function */
135    jmp *%rbx
136
137finish:
138    _CET_ENDBR
139    /* exit code is zero */
140    xorq  %rdi, %rdi
141    /* exit application */
142    call  _exit@PLT
143    hlt
144.size make_fcontext,.-make_fcontext
145
146/* Mark that we don't need executable stack. */
147.section .note.GNU-stack,"",%progbits
148