Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
sorting out fibers
Browse files Browse the repository at this point in the history
  • Loading branch information
complexmath committed Sep 16, 2011
1 parent 7847f2b commit 7f36a14
Showing 1 changed file with 112 additions and 43 deletions.
155 changes: 112 additions & 43 deletions src/core/thread.d
Original file line number Diff line number Diff line change
Expand Up @@ -2794,7 +2794,7 @@ private
version( D_InlineAsm_X86 )
{
version( Windows )
version = AsmX86_Win32;
version = AsmX86_Windows;
else version( Posix )
version = AsmX86_Posix;

Expand All @@ -2803,7 +2803,12 @@ private
}
else version( D_InlineAsm_X86_64 )
{
version( Posix )
version( Windows )
{
version = AsmX86_64_Windows;
version = AlignFiberStackTo16Byte;
}
else version( Posix )
{
version = AsmX86_64_Posix;
version = AlignFiberStackTo16Byte;
Expand All @@ -2821,10 +2826,11 @@ private
import core.sys.posix.unistd; // for sysconf
import core.sys.posix.sys.mman; // for mmap

version( AsmX86_Win32 ) {} else
version( AsmX86_Posix ) {} else
version( AsmPPC_Posix ) {} else
version( AsmX86_64_Posix ) {} else
version( AsmX86_Windows ) {} else
version( AsmX86_Posix ) {} else
version( AsmX86_64_Windows ) {} else
version( AsmX86_64_Posix ) {} else
version( AsmPPC_Posix ) {} else
{
// NOTE: The ucontext implementation requires architecture specific
// data definitions to operate so testing for it must be done
Expand Down Expand Up @@ -2911,22 +2917,22 @@ private
// default stack created by Fiber.initStack or the initial
// switch into a new context will fail.

version( AsmX86_Win32 )
{
version( AsmX86_Windows )
{
asm
{
naked;

// save current stack state
push EBP;
mov EBP, ESP;
push EAX;
push EDI;
push ESI;
push EBX;
push dword ptr FS:[0];
push dword ptr FS:[4];
push dword ptr FS:[8];
push EBX;
push ESI;
push EDI;
push EAX;

// store oldp again with more accurate address
mov EAX, dword ptr 8[EBP];
Expand All @@ -2935,19 +2941,58 @@ private
mov ESP, dword ptr 12[EBP];

// load saved state from new stack
pop EDI;
pop ESI;
pop EBX;
pop EAX;
pop dword ptr FS:[8];
pop dword ptr FS:[4];
pop dword ptr FS:[0];
pop EAX;
pop EBX;
pop ESI;
pop EDI;
pop EBP;

// 'return' to complete switch
ret;
}
}
else version( AsmX86_64_Windows )
{
asm
{
naked;

// save current stack state
push RBP;
mov RBP, RSP;
push RBX;
push R12;
push R13;
push R14;
push R15;
push qword ptr GS:[0];
push qword ptr GS:[8];
push qword ptr GS:[16];

// store oldp
mov [RDI], RSP;
// load newp to begin context switch
mov RSP, RSI;

// load saved state from new stack
pop qword ptr GS:[16];
pop qword ptr GS:[8];
pop qword ptr GS:[0];
pop R15;
pop R14;
pop R13;
pop R12;
pop RBX;
pop RBP;

// 'return' to complete switch
pop RCX;
jmp RCX;
}
}
else version( AsmX86_Posix )
{
asm
Expand All @@ -2957,10 +3002,10 @@ private
// save current stack state
push EBP;
mov EBP, ESP;
push EAX;
push EBX;
push ESI;
push EDI;
push ESI;
push EBX;
push EAX;

// store oldp again with more accurate address
mov EAX, dword ptr 8[EBP];
Expand All @@ -2969,10 +3014,10 @@ private
mov ESP, dword ptr 12[EBP];

// load saved state from new stack
pop EDI;
pop ESI;
pop EBX;
pop EAX;
pop EBX;
pop ESI;
pop EDI;
pop EBP;

// 'return' to complete switch
Expand All @@ -2985,9 +3030,11 @@ private
asm
{
naked;

// save current stack state
push RBX;
push RBP;
mov RBP, RSP;
push RBX;
push R12;
push R13;
push R14;
Expand All @@ -3003,8 +3050,8 @@ private
pop R14;
pop R13;
pop R12;
pop RBP;
pop RBX;
pop RBP;

// 'return' to complete switch
pop RCX;
Expand Down Expand Up @@ -3635,11 +3682,13 @@ private:
}
}

version( AsmX86_Win32 )
{
version( AsmX86_Windows )
{
push( cast(size_t) &fiber_entryPoint ); // EIP
push( 0xFFFFFFFF ); // EBP
push( 0x00000000 ); // EAX
push( cast(size_t) m_ctxt.bstack ); // EBP
push( 0x00000000 ); // EDI
push( 0x00000000 ); // ESI
push( 0x00000000 ); // EBX
push( 0xFFFFFFFF ); // FS:[0]
version( StackGrowsDown )
{
Expand All @@ -3651,30 +3700,50 @@ private:
push( cast(size_t) m_ctxt.bstack ); // FS:[4]
push( cast(size_t) m_ctxt.bstack + m_size ); // FS:[8]
}
push( 0x00000000 ); // EBX
push( 0x00000000 ); // ESI
push( 0x00000000 ); // EDI
push( 0x00000000 ); // EAX
}
else version( AsmX86_Posix )
else version( AsmX86_64_Windows )
{
push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call
push( cast(size_t) &fiber_entryPoint ); // RIP
push( 0x00000000_00000000 ); // RBP
push( 0x00000000_00000000 ); // RBX
push( 0x00000000_00000000 ); // R12
push( 0x00000000_00000000 ); // R13
push( 0x00000000_00000000 ); // R14
push( 0x00000000_00000000 ); // R15
push( 0xFFFFFFFF_FFFFFFFF ); // GS:[0]
version( StackGrowsDown )
{
push( cast(size_t) m_ctxt.bstack ); // GS:[8]
push( cast(size_t) m_ctxt.bstack - m_size ); // GS:[16]
}
else
{
push( cast(size_t) m_ctxt.bstack ); // GS:[8]
push( cast(size_t) m_ctxt.bstack + m_size ); // GS:[16]
}
}
else version( AsmX86_Posix )
{
push( 0x00000000 ); // Return address of fiber_entryPoint call
push( cast(size_t) &fiber_entryPoint ); // EIP
push( 0x00000000 ); // EBP
push( 0x00000000 ); // EAX
push( 0x00000000 ); // EBX
push( 0x00000000 ); // ESI
push( cast(size_t) m_ctxt.bstack ); // EBP
push( 0x00000000 ); // EDI
push( 0x00000000 ); // ESI
push( 0x00000000 ); // EBX
push( 0x00000000 ); // EAX
}
else version( AsmX86_64_Posix )
{
push(0); // Return address of fiber_entryPoint call
push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call
push( cast(size_t) &fiber_entryPoint ); // RIP
push(0); // RBX
push(0); // RBP
push(0); // R12
push(0); // R13
push(0); // R14
push(0); // R15
push( cast(size_t) m_ctxt.bstack ); // RBP
push( 0x00000000_00000000 ); // RBX
push( 0x00000000_00000000 ); // R12
push( 0x00000000_00000000 ); // R13
push( 0x00000000_00000000 ); // R14
push( 0x00000000_00000000 ); // R15
}
else version( AsmPPC_Posix )
{
Expand Down

0 comments on commit 7f36a14

Please sign in to comment.