1/*
2            Copyright Oliver Kowalke 2009.
3            Copyright Thomas Sailer 2013.
4   Distributed under the Boost Software License, Version 1.0.
5      (See accompanying file LICENSE_1_0.txt or copy at
6            http://www.boost.org/LICENSE_1_0.txt)
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* |                          SEE registers (XMM6-XMM15)                            | *
16* ---------------------------------------------------------------------------------- *
17* ---------------------------------------------------------------------------------- *
18* |     8   |    9    |    10    |    11   |    12   |    13   |    14   |    15   | *
19* ---------------------------------------------------------------------------------- *
20* |   0x20  |  0x24   |   0x28   |   0x2c  |   0x30  |   0x34  |   0x38  |   0x3c  | *
21* ---------------------------------------------------------------------------------- *
22* |                          SEE registers (XMM6-XMM15)                            | *
23* ---------------------------------------------------------------------------------- *
24* ---------------------------------------------------------------------------------- *
25* |    16   |    17   |    18   |    19    |    20   |    21   |    22   |    23   | *
26* ---------------------------------------------------------------------------------- *
27* |   0xe40  |   0x44 |   0x48  |   0x4c   |   0x50  |   0x54  |   0x58  |   0x5c  | *
28* ---------------------------------------------------------------------------------- *
29* |                          SEE registers (XMM6-XMM15)                            | *
30* ---------------------------------------------------------------------------------- *
31* ---------------------------------------------------------------------------------- *
32* |    24   |   25    |    26    |   27    |    28   |    29   |    30   |    31   | *
33* ---------------------------------------------------------------------------------- *
34* |   0x60  |   0x64  |   0x68   |   0x6c  |   0x70  |   0x74  |   0x78  |   0x7c  | *
35* ---------------------------------------------------------------------------------- *
36* |                          SEE registers (XMM6-XMM15)                            | *
37* ---------------------------------------------------------------------------------- *
38* ---------------------------------------------------------------------------------- *
39* |    32   |   32    |    33    |   34    |    35   |    36   |    37   |    38   | *
40* ---------------------------------------------------------------------------------- *
41* |   0x80  |   0x84  |   0x88   |   0x8c  |   0x90  |   0x94  |   0x98  |   0x9c  | *
42* ---------------------------------------------------------------------------------- *
43* |                          SEE registers (XMM6-XMM15)                            | *
44* ---------------------------------------------------------------------------------- *
45* ---------------------------------------------------------------------------------- *
46* |    39   |   40    |    41    |   42    |    43   |    44   |    45   |    46   | *
47* ---------------------------------------------------------------------------------- *
48* |   0xa0  |   0xa4  |   0xa8   |   0xac  |   0xb0  |   0xb4  |   0xb8  |   0xbc  | *
49* ---------------------------------------------------------------------------------- *
50* | fc_mxcsr|fc_x87_cw|     <alignment>    |       fbr_strg    |      fc_dealloc   | *
51* ---------------------------------------------------------------------------------- *
52* ---------------------------------------------------------------------------------- *
53* |    47   |   48    |    49    |   50    |    51   |    52   |    53   |    54   | *
54* ---------------------------------------------------------------------------------- *
55* |   0xc0  |   0xc4  |   0xc8   |   0xcc  |   0xd0  |   0xd4  |   0xd8  |   0xdc  | *
56* ---------------------------------------------------------------------------------- *
57* |        limit      |         base       |         R12       |         R13       | *
58* ---------------------------------------------------------------------------------- *
59* ---------------------------------------------------------------------------------- *
60* |    55   |   56    |    57    |   58    |    59   |    60   |    61   |    62   | *
61* ---------------------------------------------------------------------------------- *
62* |   0xe0  |   0xe4  |   0xe8   |   0xec  |   0xf0  |   0xf4  |   0xf8  |   0xfc  | *
63* ---------------------------------------------------------------------------------- *
64* |        R14        |         R15        |         RDI       |        RSI        | *
65* ---------------------------------------------------------------------------------- *
66* ---------------------------------------------------------------------------------- *
67* |    63   |   64    |    65    |   66    |    67   |    68   |    69   |    70   | *
68* ---------------------------------------------------------------------------------- *
69* |  0x100  |  0x104  |  0x108   |  0x10c  |  0x110  |  0x114  |  0x118  |  0x11c  | *
70* ---------------------------------------------------------------------------------- *
71* |        RBX        |         RBP        |       hidden      |        RIP        | *
72* ---------------------------------------------------------------------------------- *
73* ---------------------------------------------------------------------------------- *
74* |    71   |   72    |    73    |   74    |    75   |    76   |    77   |    78   | *
75* ---------------------------------------------------------------------------------- *
76* |  0x120  |  0x124  |  0x128   |  0x12c  |  0x130  |  0x134  |  0x138  |  0x13c  | *
77* ---------------------------------------------------------------------------------- *
78* |                                   parameter area                               | *
79* ---------------------------------------------------------------------------------- *
80* ---------------------------------------------------------------------------------- *
81* |    79   |   80    |    81    |   82    |    83   |    84   |    85   |    86   | *
82* ---------------------------------------------------------------------------------- *
83* |  0x140  |  0x144  |  0x148   |  0x14c  |  0x150  |  0x154  |  0x158  |  0x15c  | *
84* ---------------------------------------------------------------------------------- *
85* |       FCTX        |        DATA        |                                       | *
86* ---------------------------------------------------------------------------------- *
87**************************************************************************************/
88
89.file	"make_x86_64_ms_pe_clang_gas.S"
90.text
91.p2align 4,,15
92.globl	make_fcontext
93.def	make_fcontext;	.scl	2;	.type	32;	.endef
94.seh_proc	make_fcontext
95make_fcontext:
96.seh_endprologue
97
98    /* first arg of make_fcontext() == top of context-stack */
99    movq  %rcx, %rax
100
101    /* shift address in RAX to lower 16 byte boundary */
102    /* == pointer to fcontext_t and address of context stack */
103    andq  $-16, %rax
104
105    /* reserve space for context-data on context-stack */
106    /* on context-function entry: (RSP -0x8) % 16 == 0 */
107    leaq  -0x150(%rax), %rax
108
109    /* third arg of make_fcontext() == address of context-function */
110    movq  %r8, 0x100(%rax)
111
112    /* first arg of make_fcontext() == top of context-stack */
113    /* save top address of context stack as 'base' */
114    movq  %rcx, 0xc8(%rax)
115    /* second arg of make_fcontext() == size of context-stack */
116    /* negate stack size for LEA instruction (== substraction) */
117    negq  %rdx
118    /* compute bottom address of context stack (limit) */
119    leaq  (%rcx,%rdx), %rcx
120    /* save bottom address of context stack as 'limit' */
121    movq  %rcx, 0xc0(%rax)
122    /* save address of context stack limit as 'dealloction stack' */
123    movq  %rcx, 0xb8(%rax)
124	/* set fiber-storage to zero */
125    xorq  %rcx, %rcx
126    movq  %rcx, 0xb0(%rax)
127
128	/* save MMX control- and status-word */
129    stmxcsr  0xa0(%rax)
130    /* save x87 control-word */
131    fnstcw   0xa4(%rax)
132
133    /* compute address of transport_t */
134    leaq  0x140(%rax), %rcx
135    /* store address of transport_t in hidden field */
136    movq %rcx, 0x110(%rax)
137
138    /* compute abs address of label trampoline */
139    leaq  trampoline(%rip), %rcx
140    /* save address of finish as return-address for context-function */
141    /* will be entered after jump_fcontext() first time */
142    movq  %rcx, 0x118(%rax)
143
144    /* compute abs address of label finish */
145    leaq  finish(%rip), %rcx
146    /* save address of finish as return-address for context-function */
147    /* will be entered after context-function returns */
148    movq  %rcx, 0x108(%rax)
149
150    ret /* return pointer to context-data */
151
152trampoline:
153    /* store return address on stack */
154    /* fix stack alignment */
155    pushq %rbp
156    /* jump to context-function */
157    jmp *%rbx
158
159finish:
160    /* 32byte shadow-space for _exit() */
161    andq  $-32, %rsp
162    /* 32byte shadow-space for _exit() are */
163    /* already reserved by make_fcontext() */
164    /* exit code is zero */
165    xorq  %rcx, %rcx
166    /* exit application */
167    call  _exit
168    hlt
169.seh_endproc
170
171.def	_exit;	.scl	2;	.type	32;	.endef  /* standard C library function */
172
173.section .drectve
174.ascii " -export:\"make_fcontext\""
175