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|        R12        |         R13        |        R14        |  *
16 *  ----------------------------------------------------------------------------------  *
17 *  ----------------------------------------------------------------------------------  *
18 *  |    8    |    9    |   10    |   11    |    12    |    13   |    14   |    15   |  *
19 *  ----------------------------------------------------------------------------------  *
20 *  |   0x20  |   0x24  |   0x28  |  0x2c   |   0x30   |   0x34  |   0x38  |   0x3c  |  *
21 *  ----------------------------------------------------------------------------------  *
22 *  |        R15        |        RBX        |         RBP        |        RIP        |  *
23 *  ----------------------------------------------------------------------------------  *
24 *                                                                                      *
25 ****************************************************************************************/
26
27.file "make_x86_64_sysv_elf_gas.S"
28.text
29.globl make_fcontext
30.type make_fcontext,@function
31.align 16
32make_fcontext:
33    /* first arg of make_fcontext() == top of context-stack */
34    movq  %rdi, %rax
35
36    /* shift address in RAX to lower 16 byte boundary */
37    andq  $-16, %rax
38
39    /* reserve space for context-data on context-stack */
40    /* on context-function entry: (RSP -0x8) % 16 == 0 */
41    leaq  -0x40(%rax), %rax
42
43    /* third arg of make_fcontext() == address of context-function */
44    /* stored in RBX */
45    movq  %rdx, 0x28(%rax)
46
47    /* save MMX control- and status-word */
48    stmxcsr  (%rax)
49    /* save x87 control-word */
50    fnstcw   0x4(%rax)
51
52    /* compute abs address of label trampoline */
53    leaq  trampoline(%rip), %rcx
54    /* save address of trampoline as return-address for context-function */
55    /* will be entered after calling jump_fcontext() first time */
56    movq  %rcx, 0x38(%rax)
57
58    /* compute abs address of label finish */
59    leaq  finish(%rip), %rcx
60    /* save address of finish as return-address for context-function */
61    /* will be entered after context-function returns */
62    movq  %rcx, 0x30(%rax)
63
64    ret /* return pointer to context-data */
65
66trampoline:
67    /* store return address on stack */
68    /* fix stack alignment */
69    push %rbp
70    /* jump to context-function */
71    jmp *%rbx
72
73finish:
74    /* exit code is zero */
75    xorq  %rdi, %rdi
76    /* exit application */
77    call  _exit@PLT
78    hlt
79.size make_fcontext,.-make_fcontext
80
81/* Mark that we don't need executable stack. */
82.section .note.GNU-stack,"",%progbits
83