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