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;  |    0h   |   04h   |   08h   |   0ch   |   010h  |   014h  |   018h  |   01ch  |
11;  ---------------------------------------------------------------------------------
12;  | fc_mxcsr|fc_x87_cw| fc_strg |fc_deallo|  limit  |   base  |  fc_seh |   EDI   |
13;  ---------------------------------------------------------------------------------
14;  ---------------------------------------------------------------------------------
15;  |    8    |    9    |   10    |    11   |    12   |    13   |    14   |    15   |
16;  ---------------------------------------------------------------------------------
17;  |   020h  |  024h   |  028h   |   02ch  |   030h  |   034h  |   038h  |   03ch  |
18;  ---------------------------------------------------------------------------------
19;  |   ESI   |   EBX   |   EBP   |   EIP   |   EXIT  |         | SEH NXT |SEH HNDLR|
20;  ---------------------------------------------------------------------------------
21
22.386
23.XMM
24.model flat, c
25; standard C library function
26_exit PROTO, value:SDWORD
27.code
28
29make_fcontext PROC
30    ; first arg of make_fcontext() == top of context-stack
31    mov  eax, [esp+04h]
32
33    ; reserve space for first argument of context-function
34    ; EAX might already point to a 16byte border
35    lea  eax, [eax-08h]
36
37    ; shift address in EAX to lower 16 byte boundary
38    and  eax, -16
39
40    ; reserve space for context-data on context-stack
41    ; size for fc_mxcsr .. EIP + return-address for context-function
42    ; on context-function entry: (ESP -0x4) % 8 == 0
43    ; additional space is required for SEH
44    lea  eax, [eax-03ch]
45
46    ; first arg of make_fcontext() == top of context-stack
47    mov  ecx, [esp+04h]
48    ; save top address of context stack as 'base'
49    mov  [eax+014h], ecx
50    ; second arg of make_fcontext() == size of context-stack
51    mov  edx, [esp+08h]
52    ; negate stack size for LEA instruction (== substraction)
53    neg  edx
54    ; compute bottom address of context stack (limit)
55    lea  ecx, [ecx+edx]
56    ; save bottom address of context-stack as 'limit'
57    mov  [eax+010h], ecx
58    ; save bottom address of context-stack as 'dealloction stack'
59    mov  [eax+0ch], ecx
60
61    ; third arg of make_fcontext() == address of context-function
62    mov  ecx, [esp+0ch]
63    mov  [eax+02ch], ecx
64
65    ; save MMX control- and status-word
66    stmxcsr  [eax]
67    ; save x87 control-word
68    fnstcw  [eax+04h]
69
70    ; compute abs address of label finish
71    mov  ecx, finish
72    ; save address of finish as return-address for context-function
73    ; will be entered after context-function returns
74    mov  [eax+030h], ecx
75
76    ; traverse current seh chain to get the last exception handler installed by Windows
77    ; note that on Windows Server 2008 and 2008 R2, SEHOP is activated by default
78    ; the exception handler chain is tested for the presence of ntdll.dll!FinalExceptionHandler
79    ; at its end by RaiseException all seh-handlers are disregarded if not present and the
80    ; program is aborted
81    assume  fs:nothing
82    ; load NT_TIB into ECX
83    mov  ecx, fs:[0h]
84    assume  fs:error
85
86walk:
87    ; load 'next' member of current SEH into EDX
88    mov  edx, [ecx]
89    ; test if 'next' of current SEH is last (== 0xffffffff)
90    inc  edx
91    jz  found
92    dec  edx
93    ; exchange content; ECX contains address of next SEH
94    xchg edx, ecx
95    ; inspect next SEH
96    jmp  walk
97
98found:
99    ; load 'handler' member of SEH == address of last SEH handler installed by Windows
100    mov  ecx, [ecx+04h]
101    ; save address in ECX as SEH handler for context
102    mov  [eax+03ch], ecx
103    ; set ECX to -1
104    mov  ecx, 0ffffffffh
105    ; save ECX as next SEH item
106    mov  [eax+038h], ecx
107    ; load address of next SEH item
108    lea  ecx, [eax+038h]
109    ; save next SEH
110    mov  [eax+018h], ecx
111
112    ret ; return pointer to context-data
113
114finish:
115    ; exit code is zero
116    xor  eax, eax
117    mov  [esp], eax
118    ; exit application
119    call  _exit
120    hlt
121make_fcontext ENDP
122END
123