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.text
28.globl _make_fcontext
29.align 8
30_make_fcontext:
31    /* first arg of make_fcontext() == top of context-stack */
32    movq  %rdi, %rax
33
34    /* shift address in RAX to lower 16 byte boundary */
35    andq  $-16, %rax
36
37    /* reserve space for context-data on context-stack */
38    /* on context-function entry: (RSP -0x8) % 16 == 0 */
39    leaq  -0x40(%rax), %rax
40
41    /* third arg of make_fcontext() == address of context-function */
42    /* stored in RBX */
43    movq  %rdx, 0x28(%rax)
44
45    /* save MMX control- and status-word */
46    stmxcsr  (%rax)
47    /* save x87 control-word */
48    fnstcw   0x4(%rax)
49
50    /* compute abs address of label trampoline */
51    leaq  trampoline(%rip), %rcx
52    /* save address of trampoline as return-address for context-function */
53    /* will be entered after calling jump_fcontext() first time */
54    movq  %rcx, 0x38(%rax)
55
56    /* compute abs address of label finish */
57    leaq  finish(%rip), %rcx
58    /* save address of finish as return-address for context-function */
59    /* will be entered after context-function returns */
60    movq  %rcx, 0x30(%rax)
61
62    ret /* return pointer to context-data */
63
64trampoline:
65    /* store return address on stack */
66    /* fix stack alignment */
67    push %rbp
68    /* jump to context-function */
69    jmp *%rbx
70
71finish:
72    /* exit code is zero */
73    xorq  %rdi, %rdi
74    /* exit application */
75    call  __exit
76    hlt
77