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   |    to   |  *
16 *  ----------------------------------------------------------------------------------  *
17 *  ----------------------------------------------------------------------------------  *
18 *  |    8    |    9    |    10   |    11   |    12    |    13   |    14   |    15   |  *
19 *  ----------------------------------------------------------------------------------  *
20 *  |   0x20  |                                                                      |  *
21 *  ----------------------------------------------------------------------------------  *
22 *  |   data  |                                                                      |  *
23 *  ----------------------------------------------------------------------------------  *
24 *                                                                                      *
25 ****************************************************************************************/
26
27.text
28.globl _make_fcontext
29.align 2
30_make_fcontext:
31    /* first arg of make_fcontext() == top of context-stack */
32    movl  0x4(%esp), %eax
33
34    /* reserve space for first argument of context-function
35       eax might already point to a 16byte border */
36    leal  -0x8(%eax), %eax
37
38    /* shift address in EAX to lower 16 byte boundary */
39    andl  $-16, %eax
40
41    /* reserve space for context-data on context-stack, and align the stack */
42    leal  -0x34(%eax), %eax
43
44    /* third arg of make_fcontext() == address of context-function */
45    /* stored in EBX */
46    movl  0xc(%esp), %ecx
47    movl  %ecx, 0x10(%eax)
48
49    /* save MMX control- and status-word */
50    stmxcsr (%eax)
51    /* save x87 control-word */
52    fnstcw  0x4(%eax)
53
54    /* compute abs address of label trampoline */
55    call  1f
56    /* address of trampoline 1 */
571:  popl  %ecx
58    /* compute abs address of label trampoline */
59    addl  $trampoline-1b, %ecx
60    /* save address of trampoline as return address */
61    /* will be entered after calling jump_fcontext() first time */
62    movl  %ecx, 0x18(%eax)
63
64    /* compute abs address of label finish */
65    call  2f
66    /* address of label 2 */
672:  popl  %ecx
68    /* compute abs address of label finish */
69    addl  $finish-2b, %ecx
70    /* save address of finish as return-address for context-function */
71    /* will be entered after context-function returns */
72    movl  %ecx, 0x14(%eax)
73
74    ret /* return pointer to context-data */
75
76trampoline:
77    /* move transport_t for entering context-function */
78    movl  %eax, (%esp)
79    movl  %edx, 0x4(%esp)
80    pushl %ebp
81    /* jump to context-function */
82    jmp *%ebx
83
84finish:
85    /* exit code is zero */
86    xorl  %eax, %eax
87    movl  %eax, (%esp)
88    /* exit application */
89    call  __exit
90    hlt
91