2005-07-19 21:04:19 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
2015-11-14 14:57:11 +00:00
|
|
|
* FILE: dll/win32/kernel32/client/i386/fiber.S
|
2005-07-19 21:04:19 +00:00
|
|
|
* PURPOSE: Fiber context switch code for the x86 architecture
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
|
|
|
|
* KJK::Hyperion <noog@libero.it>
|
2003-05-29 00:36:41 +00:00
|
|
|
*/
|
|
|
|
|
2010-11-27 22:12:15 +00:00
|
|
|
#include <asm.inc>
|
|
|
|
#include <ks386.inc>
|
2003-05-29 00:36:41 +00:00
|
|
|
|
2011-07-23 17:16:51 +00:00
|
|
|
EXTERN _BaseThreadStartup@8:PROC
|
|
|
|
|
2010-11-27 22:12:15 +00:00
|
|
|
.code
|
2003-05-29 00:36:41 +00:00
|
|
|
|
2011-07-23 17:16:51 +00:00
|
|
|
PUBLIC _BaseFiberStartup@0
|
2015-10-08 16:26:29 +00:00
|
|
|
FUNC _BaseFiberStartup@0
|
2011-07-23 17:16:51 +00:00
|
|
|
FPO 0, 0, 0, 0, 0, FRAME_FPO
|
|
|
|
|
2015-10-08 16:26:29 +00:00
|
|
|
/* Note that EBP is already zeroed for us during fiber creation */
|
|
|
|
|
2011-07-23 17:16:51 +00:00
|
|
|
/* Get the fiber data */
|
|
|
|
mov eax, fs:[TEB_FIBER_DATA]
|
|
|
|
|
2015-10-08 16:26:29 +00:00
|
|
|
/* Start the thread with our parameters */
|
|
|
|
push dword ptr [eax+FIBER_CONTEXT_EBX]
|
|
|
|
push dword ptr [eax+FIBER_CONTEXT_EAX]
|
2015-10-08 17:19:40 +00:00
|
|
|
push 0
|
2015-10-08 16:26:29 +00:00
|
|
|
jmp _BaseThreadStartup@8
|
|
|
|
|
|
|
|
ENDFUNC
|
2011-07-23 17:16:51 +00:00
|
|
|
|
|
|
|
|
2010-11-27 22:12:15 +00:00
|
|
|
PUBLIC _SwitchToFiber@4
|
2015-10-08 16:26:29 +00:00
|
|
|
FUNC _SwitchToFiber@4
|
|
|
|
FPO 0, 0, 0, 0, 0, FRAME_FPO
|
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Get the TEB */
|
2008-10-27 02:48:54 +00:00
|
|
|
mov edx, fs:[TEB_SELF]
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Get the Fiber */
|
|
|
|
mov eax, [edx+TEB_FIBER_DATA]
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Save the non-volatile registers */
|
|
|
|
mov [eax+FIBER_CONTEXT_EBX], ebx
|
|
|
|
mov [eax+FIBER_CONTEXT_ESI], esi
|
|
|
|
mov [eax+FIBER_CONTEXT_EDI], edi
|
|
|
|
mov [eax+FIBER_CONTEXT_EBP], ebp
|
2010-05-16 00:30:11 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Check if we're to save FPU State */
|
2010-11-27 22:12:15 +00:00
|
|
|
cmp dword ptr [eax+FIBER_CONTEXT_FLAGS], CONTEXT_FULL OR CONTEXT_FLOATING_POINT
|
2005-07-19 21:04:19 +00:00
|
|
|
jnz NoFpuStateSave
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Save the FPU State (Status and Control)*/
|
|
|
|
fstsw [eax+FIBER_CONTEXT_FLOAT_SAVE_STATUS_WORD]
|
2011-07-23 12:08:36 +00:00
|
|
|
fnstcw [eax+FIBER_CONTEXT_FLOAT_SAVE_CONTROL_WORD]
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Check if the CPU supports SIMD MXCSR State Save */
|
2015-10-08 16:26:29 +00:00
|
|
|
cmp byte ptr ds:[USER_SHARED_DATA + USER_SHARED_DATA_PROCESSOR_FEATURES + PF_XMMI_INSTRUCTIONS_AVAILABLE], 1
|
2005-07-19 21:04:19 +00:00
|
|
|
jnz NoFpuStateSave
|
|
|
|
stmxcsr [eax+FIBER_CONTEXT_DR6]
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
NoFpuStateSave:
|
2003-06-09 22:39:48 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Save stack since we're not touching it anymore */
|
|
|
|
mov [eax+FIBER_CONTEXT_ESP], esp
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Transfer some data from the TEB */
|
|
|
|
mov ecx, [edx+TEB_FLS_DATA]
|
|
|
|
mov [eax+FIBER_FLS_DATA], ecx
|
|
|
|
mov ecx, [edx+TEB_ACTIVATION_CONTEXT_STACK_POINTER]
|
|
|
|
mov [eax+FIBER_ACTIVATION_CONTEXT_STACK], ecx
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Transfer some data related to the Stack */
|
|
|
|
mov ecx, [edx+TEB_EXCEPTION_LIST]
|
|
|
|
mov [eax+FIBER_EXCEPTION_LIST], ecx
|
|
|
|
mov ecx, [edx+TEB_STACK_LIMIT]
|
|
|
|
mov [eax+FIBER_STACK_LIMIT], ecx
|
|
|
|
mov ecx, [edx+TEB_GUARANTEED_STACK_BYTES]
|
|
|
|
mov [eax+FIBER_GUARANTEED_STACK_BYTES], ecx
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Switch to the new fiber */
|
|
|
|
mov ecx, [esp+4]
|
|
|
|
mov [edx+TEB_FIBER_DATA], ecx
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Switch Fiber Data */
|
|
|
|
mov esi, [ecx+FIBER_EXCEPTION_LIST]
|
|
|
|
mov [edx+TEB_EXCEPTION_LIST], esi
|
|
|
|
mov esi, [ecx+FIBER_STACK_BASE]
|
|
|
|
mov [edx+TEB_STACK_BASE], esi
|
|
|
|
mov esi, [ecx+FIBER_STACK_LIMIT]
|
|
|
|
mov [edx+TEB_STACK_LIMIT], esi
|
|
|
|
mov esi, [ecx+FIBER_DEALLOCATION_STACK]
|
|
|
|
mov [edx+TEB_DEALLOCATION_STACK], esi
|
|
|
|
mov esi, [ecx+FIBER_GUARANTEED_STACK_BYTES]
|
|
|
|
mov [edx+TEB_GUARANTEED_STACK_BYTES], esi
|
|
|
|
mov esi, [ecx+FIBER_ACTIVATION_CONTEXT_STACK]
|
|
|
|
mov [edx+TEB_ACTIVATION_CONTEXT_STACK_POINTER], esi
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Restore FPU State */
|
2010-11-27 22:12:15 +00:00
|
|
|
cmp dword ptr [eax+FIBER_CONTEXT_FLAGS], CONTEXT_FULL OR CONTEXT_FLOATING_POINT
|
2005-07-19 21:04:19 +00:00
|
|
|
jnz NoFpuStateRestore
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Check if the Status Word Changed */
|
|
|
|
mov esi, [eax+FIBER_CONTEXT_FLOAT_SAVE_STATUS_WORD]
|
|
|
|
cmp si, word ptr [ecx+FIBER_CONTEXT_FLOAT_SAVE_STATUS_WORD]
|
|
|
|
jnz StatusWordChanged
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Check if the Control Word Changed */
|
|
|
|
mov esi, [eax+FIBER_CONTEXT_FLOAT_SAVE_CONTROL_WORD]
|
|
|
|
cmp si, word ptr [ecx+FIBER_CONTEXT_FLOAT_SAVE_CONTROL_WORD]
|
|
|
|
jz ControlWordEqual
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
StatusWordChanged:
|
2003-05-29 00:36:41 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Load the new one */
|
2015-10-08 16:26:29 +00:00
|
|
|
mov word ptr [ecx+FIBER_CONTEXT_FLOAT_SAVE_TAG_WORD], HEX(FFFF)
|
2005-07-19 21:04:19 +00:00
|
|
|
fldenv [ecx+FIBER_CONTEXT_FLOAT_SAVE_CONTROL_WORD]
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
ControlWordEqual:
|
2003-05-29 00:36:41 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Load the new one */
|
2015-10-08 16:26:29 +00:00
|
|
|
cmp byte ptr ds:[USER_SHARED_DATA + USER_SHARED_DATA_PROCESSOR_FEATURES + PF_XMMI_INSTRUCTIONS_AVAILABLE], 1
|
2005-07-19 21:04:19 +00:00
|
|
|
jnz NoFpuStateRestore
|
|
|
|
ldmxcsr [ecx+FIBER_CONTEXT_DR6]
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
NoFpuStateRestore:
|
2003-05-29 00:36:41 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Restore non-volatile registers */
|
|
|
|
mov esi, [ecx+FIBER_CONTEXT_ESI]
|
|
|
|
mov edi, [ecx+FIBER_CONTEXT_EDI]
|
|
|
|
mov ebx, [ecx+FIBER_CONTEXT_EBX]
|
|
|
|
mov ebp, [ecx+FIBER_CONTEXT_EBP]
|
|
|
|
mov esp, [ecx+FIBER_CONTEXT_ESP]
|
2011-07-23 17:16:51 +00:00
|
|
|
|
2005-07-19 21:04:19 +00:00
|
|
|
/* Restore FLS Data */
|
|
|
|
mov eax, [ecx+FIBER_FLS_DATA]
|
|
|
|
mov [edx+TEB_FLS_DATA], eax
|
2003-05-29 00:36:41 +00:00
|
|
|
|
2010-05-16 00:30:11 +00:00
|
|
|
/* Jump to new fiber */
|
2011-07-23 12:08:36 +00:00
|
|
|
mov esp, [ecx+FIBER_CONTEXT_ESP]
|
|
|
|
ret 4
|
|
|
|
|
2015-10-08 16:26:29 +00:00
|
|
|
ENDFUNC
|
2011-10-13 15:16:29 +00:00
|
|
|
|
|
|
|
END
|
2003-05-29 00:36:41 +00:00
|
|
|
/* EOF */
|