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|   EDI   |   ESI   |   EBX    |   EBP   |   EIP   |   EXIT  |  *
16 *  ----------------------------------------------------------------------------------  *
17 *                                                                                      *
18 ****************************************************************************************/
19
20.text
21.globl _make_fcontext
22.align 2
23_make_fcontext:
24    /* first arg of make_fcontext() == top of context-stack */
25    movl  0x4(%esp), %eax
26
27    /* reserve space for first argument of context-function
28       rax might already point to a 16byte border */
29    leal  -0x8(%eax), %eax
30
31    /* shift address in EAX to lower 16 byte boundary */
32    andl  $-16, %eax
33
34    /* reserve space for context-data on context-stack */
35    /* size for fc_mxcsr .. EIP + return-address for context-function */
36    /* on context-function entry: (ESP -0x4) % 8 == 0 */
37    leal  -0x20(%eax), %eax
38
39    /* thrid arg of make_fcontext() == address of context-function */
40    movl  0xc(%esp), %edx
41    movl  %edx, 0x18(%eax)
42
43    /* save MMX control- and status-word */
44    stmxcsr  (%eax)
45    /* save x87 control-word */
46    fnstcw  0x4(%eax)
47
48    /* compute abs address of label finish */
49    call  1f
50    /* address of label 1 */
511:  popl  %ecx
52    /* compute abs address of label finish */
53    addl  $finish-1b, %ecx
54    /* save address of finish as return-address for context-function */
55    /* will be entered after context-function returns */
56    movl  %ecx, 0x1c(%eax)
57
58    ret /* return pointer to context-data */
59
60finish:
61    /* exit code is zero */
62    xorl  %eax, %eax
63    movl  %eax, (%esp)
64    /* exit application */
65    call  __exit
66    hlt
67