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   |  hidden |  *
16 *  ----------------------------------------------------------------------------------  *
17 *  ----------------------------------------------------------------------------------  *
18 *  |    8    |    9    |    10   |    11   |    12    |    13   |    14   |    15   |  *
19 *  ----------------------------------------------------------------------------------  *
20 *  |   0x20  |   0x24  |                                                            |  *
21 *  ----------------------------------------------------------------------------------  *
22 *  |    to   |   data  |                                                            |  *
23 *  ----------------------------------------------------------------------------------  *
24 *                                                                                      *
25 ****************************************************************************************/
26
27.file "make_i386_sysv_elf_gas.S"
28.text
29.globl make_fcontext
30.align 2
31.type make_fcontext,@function
32make_fcontext:
33    /* first arg of make_fcontext() == top of context-stack */
34    movl  0x4(%esp), %eax
35
36    /* reserve space for first argument of context-function
37       eax might already point to a 16byte border */
38    leal  -0x8(%eax), %eax
39
40    /* shift address in EAX to lower 16 byte boundary */
41    andl  $-16, %eax
42
43    /* reserve space for context-data on context-stack */
44    leal  -0x28(%eax), %eax
45
46    /* third arg of make_fcontext() == address of context-function */
47    /* stored in EBX */
48    movl  0xc(%esp), %ecx
49    movl  %ecx, 0x10(%eax)
50
51    /* save MMX control- and status-word */
52    stmxcsr (%eax)
53    /* save x87 control-word */
54    fnstcw  0x4(%eax)
55
56    /* return transport_t */
57    /* FCTX == EDI, DATA == ESI */
58    leal  0x8(%eax), %ecx
59    movl  %ecx, 0x1c(%eax)
60
61    /* compute abs address of label trampoline */
62    call  1f
63    /* address of trampoline 1 */
641:  popl  %ecx
65    /* compute abs address of label trampoline */
66    addl  $trampoline-1b, %ecx
67    /* save address of trampoline as return address */
68    /* will be entered after calling jump_fcontext() first time */
69    movl  %ecx, 0x18(%eax)
70
71    /* compute abs address of label finish */
72    call  2f
73    /* address of label 2 */
742:  popl  %ecx
75    /* compute abs address of label finish */
76    addl  $finish-2b, %ecx
77    /* save address of finish as return-address for context-function */
78    /* will be entered after context-function returns */
79    movl  %ecx, 0x14(%eax)
80
81    ret /* return pointer to context-data */
82
83trampoline:
84    /* move transport_t for entering context-function */
85    movl  %edi, (%esp)
86    movl  %esi, 0x4(%esp)
87    pushl %ebp
88    /* jump to context-function */
89    jmp *%ebx
90
91finish:
92    call  3f
93    /* address of label 3 */
943:  popl  %ebx
95    /* compute address of GOT and store it in EBX */
96    addl  $_GLOBAL_OFFSET_TABLE_+[.-3b], %ebx
97
98    /* exit code is zero */
99    xorl  %eax, %eax
100    movl  %eax, (%esp)
101    /* exit application */
102    call  _exit@PLT
103    hlt
104.size make_fcontext,.-make_fcontext
105
106/* Mark that we don't need executable stack.  */
107.section .note.GNU-stack,"",%progbits
108