make_i386_ms_pe_masm.asm 4.8 KB

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