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;  |     0   |     1   |     2    |     3   |     4   |     5   |     6   |     7   |
9;  ----------------------------------------------------------------------------------
10;  |    0x0  |    0x4  |    0x8   |    0xc  |   0x10  |   0x14  |   0x18  |   0x1c  |
11;  ----------------------------------------------------------------------------------
12;  |                          SEE registers (XMM6-XMM15)                            |
13;  ----------------------------------------------------------------------------------
14;  ----------------------------------------------------------------------------------
15;  |     8   |    9    |    10    |    11   |    12   |    13   |    14   |    15   |
16;  ----------------------------------------------------------------------------------
17;  |   0x20  |  0x24   |   0x28   |   0x2c  |   0x30  |   0x34  |   0x38  |   0x3c  |
18;  ----------------------------------------------------------------------------------
19;  |                          SEE registers (XMM6-XMM15)                            |
20;  ----------------------------------------------------------------------------------
21;  ----------------------------------------------------------------------------------
22;  |    16   |    17   |    18   |    19    |    20   |    21   |    22   |    23   |
23;  ----------------------------------------------------------------------------------
24;  |   0xe40  |   0x44 |   0x48  |   0x4c   |   0x50  |   0x54  |   0x58  |   0x5c  |
25;  ----------------------------------------------------------------------------------
26;  |                          SEE registers (XMM6-XMM15)                            |
27;  ----------------------------------------------------------------------------------
28;  ----------------------------------------------------------------------------------
29;  |    24   |   25    |    26    |   27    |    28   |    29   |    30   |    31   |
30;  ----------------------------------------------------------------------------------
31;  |   0x60  |   0x64  |   0x68   |   0x6c  |   0x70  |   0x74  |   0x78  |   0x7c  |
32;  ----------------------------------------------------------------------------------
33;  |                          SEE registers (XMM6-XMM15)                            |
34;  ----------------------------------------------------------------------------------
35;  ----------------------------------------------------------------------------------
36;  |    32   |   32    |    33    |   34    |    35   |    36   |    37   |    38   |
37;  ----------------------------------------------------------------------------------
38;  |   0x80  |   0x84  |   0x88   |   0x8c  |   0x90  |   0x94  |   0x98  |   0x9c  |
39;  ----------------------------------------------------------------------------------
40;  |                          SEE registers (XMM6-XMM15)                            |
41;  ----------------------------------------------------------------------------------
42;  ----------------------------------------------------------------------------------
43;  |    39   |   40    |    41    |   42    |    43   |    44   |    45   |    46   |
44;  ----------------------------------------------------------------------------------
45;  |   0xa0  |   0xa4  |   0xa8   |   0xac  |   0xb0  |   0xb4  |   0xb8  |   0xbc  |
46;  ----------------------------------------------------------------------------------
47;  | fc_mxcsr|fc_x87_cw|     <alignment>    |       fbr_strg    |      fc_dealloc   |
48;  ----------------------------------------------------------------------------------
49;  ----------------------------------------------------------------------------------
50;  |    47   |   48    |    49    |   50    |    51   |    52   |    53   |    54   |
51;  ----------------------------------------------------------------------------------
52;  |   0xc0  |   0xc4  |   0xc8   |   0xcc  |   0xd0  |   0xd4  |   0xd8  |   0xdc  |
53;  ----------------------------------------------------------------------------------
54;  |        limit      |         base       |         R12       |         R13       |
55;  ----------------------------------------------------------------------------------
56;  ----------------------------------------------------------------------------------
57;  |    55   |   56    |    57    |   58    |    59   |    60   |    61   |    62   |
58;  ----------------------------------------------------------------------------------
59;  |   0xe0  |   0xe4  |   0xe8   |   0xec  |   0xf0  |   0xf4  |   0xf8  |   0xfc  |
60;  ----------------------------------------------------------------------------------
61;  |        R14        |         R15        |         RDI       |        RSI        |
62;  ----------------------------------------------------------------------------------
63;  ----------------------------------------------------------------------------------
64;  |    63   |   64    |    65    |   66    |    67   |    68   |    69   |    70   |
65;  ----------------------------------------------------------------------------------
66;  |  0x100  |  0x104  |  0x108   |  0x10c  |  0x110  |  0x114  |  0x118  |  0x11c  |
67;  ----------------------------------------------------------------------------------
68;  |        RBX        |         RBP        |       hidden      |        RIP        |
69;  ----------------------------------------------------------------------------------
70;  ----------------------------------------------------------------------------------
71;  |    71   |   72    |    73    |   74    |    75   |    76   |    77   |    78   |
72;  ----------------------------------------------------------------------------------
73;  |  0x120  |  0x124  |  0x128   |  0x12c  |  0x130  |  0x134  |  0x138  |  0x13c  |
74;  ----------------------------------------------------------------------------------
75;  |                                   parameter area                               |
76;  ----------------------------------------------------------------------------------
77;  ----------------------------------------------------------------------------------
78;  |    79   |   80    |    81    |   82    |    83   |    84   |    85   |    86   |
79;  ----------------------------------------------------------------------------------
80;  |  0x140  |  0x144  |  0x148   |  0x14c  |  0x150  |  0x154  |  0x158  |  0x15c  |
81;  ----------------------------------------------------------------------------------
82;  |       FCTX        |        DATA        |                                       |
83;  ----------------------------------------------------------------------------------
84
85; standard C library function
86EXTERN  _exit:PROC
87.code
88
89; generate function table entry in .pdata and unwind information in
90make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME
91    ; .xdata for a function's structured exception handling unwind behavior
92    .endprolog
93
94    ; first arg of make_fcontext() == top of context-stack
95    mov  rax, rcx
96
97    ; shift address in RAX to lower 16 byte boundary
98    ; == pointer to fcontext_t and address of context stack
99    and  rax, -16
100
101    ; reserve space for context-data on context-stack
102    ; on context-function entry: (RSP -0x8) % 16 == 0
103    sub  rax, 0150h
104
105    ; third arg of make_fcontext() == address of context-function
106    ; stored in RBX
107    mov  [rax+0100h], r8
108
109    ; first arg of make_fcontext() == top of context-stack
110    ; save top address of context stack as 'base'
111    mov  [rax+0c8h], rcx
112    ; second arg of make_fcontext() == size of context-stack
113    ; negate stack size for LEA instruction (== substraction)
114    neg  rdx
115    ; compute bottom address of context stack (limit)
116    lea  rcx, [rcx+rdx]
117    ; save bottom address of context stack as 'limit'
118    mov  [rax+0c0h], rcx
119    ; save address of context stack limit as 'dealloction stack'
120    mov  [rax+0b8h], rcx
121	; set fiber-storage to zero
122	xor  rcx, rcx
123    mov  [rax+0b0h], rcx
124
125	; save MMX control- and status-word
126    stmxcsr  [rax+0a0h]
127    ; save x87 control-word
128    fnstcw  [rax+0a4h]
129
130    ; compute address of transport_t
131    lea rcx, [rax+0140h]
132    ; store address of transport_t in hidden field
133    mov [rax+0110h], rcx
134
135    ; compute abs address of label trampoline
136    lea  rcx, trampoline
137    ; save address of trampoline as return-address for context-function
138    ; will be entered after calling jump_fcontext() first time
139    mov  [rax+0118h], rcx
140
141    ; compute abs address of label finish
142    lea  rcx, finish
143    ; save address of finish as return-address for context-function in RBP
144    ; will be entered after context-function returns
145    mov  [rax+0108h], rcx
146
147    ret ; return pointer to context-data
148
149trampoline:
150    ; store return address on stack
151    ; fix stack alignment
152    push rbp
153    ; jump to context-function
154    jmp rbx
155
156finish:
157    ; exit code is zero
158    xor  rcx, rcx
159    ; exit application
160    call  _exit
161    hlt
162make_fcontext ENDP
163END
164