TLS again

This commit is contained in:
Aaron Robinson 2003-04-06 00:25:30 +00:00
parent 66e0d7d6d1
commit a0538fa45e
10 changed files with 98 additions and 23 deletions

View File

@ -254,6 +254,11 @@ class Xbe : public Error
// ****************************************************************** // ******************************************************************
uint08 *GetTLSData() { if(m_TLS == 0) return 0; else return GetAddr(m_TLS->dwDataStartAddr); } uint08 *GetTLSData() { if(m_TLS == 0) return 0; else return GetAddr(m_TLS->dwDataStartAddr); }
// ******************************************************************
// * GetTLSIndex
// ******************************************************************
uint32 *GetTLSIndex() { if(m_TLS == 0) return 0; else return (uint32*)GetAddr(m_TLS->dwTLSIndexAddr); }
private: private:
// ****************************************************************** // ******************************************************************
// * Constructor Initialization // * Constructor Initialization

View File

@ -47,7 +47,7 @@ extern "C" CXBXKRNL_API void NTAPI EmuNoFunc();
// ****************************************************************** // ******************************************************************
// * func: EmuInit // * func: EmuInit
// ****************************************************************** // ******************************************************************
extern "C" CXBXKRNL_API void NTAPI EmuInit(Xbe::LibraryVersion *LibraryVersion, DebugMode DbgMode, char *szDebugFilename, Xbe::Header *XbeHeader, uint32 XbeHeaderSize, void (*Entry)()); extern "C" CXBXKRNL_API void NTAPI EmuInit(uint32 TlsAdjust, Xbe::LibraryVersion *LibraryVersion, DebugMode DbgMode, char *szDebugFilename, Xbe::Header *XbeHeader, uint32 XbeHeaderSize, void (*Entry)());
// ****************************************************************** // ******************************************************************
// * func: EmuPanic // * func: EmuPanic
@ -59,4 +59,9 @@ extern "C" CXBXKRNL_API void NTAPI EmuPanic();
// ****************************************************************** // ******************************************************************
extern "C" CXBXKRNL_API uint32 KernelThunkTable[367]; extern "C" CXBXKRNL_API uint32 KernelThunkTable[367];
// ******************************************************************
// * data: g_TlsAdjust
// ******************************************************************
extern uint32 g_TlsAdjust;
#endif #endif

View File

@ -56,7 +56,7 @@ static inline void EmuSwapFS()
// ****************************************************************** // ******************************************************************
// * func: EmuGenerateFS // * func: EmuGenerateFS
// ****************************************************************** // ******************************************************************
void EmuGenerateFS(); void EmuGenerateFS(int TlsAdjust);
// ****************************************************************** // ******************************************************************
// * func: EmuInitFS // * func: EmuInitFS

View File

@ -549,6 +549,33 @@ EmuExe::EmuExe(Xbe *x_Xbe, DebugMode x_debug_mode, char *x_debug_filename) : Exe
// ****************************************************************** // ******************************************************************
memcpy(m_bzSection[i] + 0x100 + x_Xbe->m_Header.dwSizeofHeaders + 260, x_Xbe->m_LibraryVersion, sizeof(Xbe::LibraryVersion) * x_Xbe->m_Header.dwLibraryVersions); memcpy(m_bzSection[i] + 0x100 + x_Xbe->m_Header.dwSizeofHeaders + 260, x_Xbe->m_LibraryVersion, sizeof(Xbe::LibraryVersion) * x_Xbe->m_Header.dwLibraryVersions);
// ******************************************************************
// * calculate TLS adjustment
// ******************************************************************
uint32 TlsAdjust = 0;
if(x_Xbe->m_TLS == 0 || OrigTLSAddr == 0)
TlsAdjust = -1;
else
{
// ******************************************************************
// * necessary tls adjustment values
// ******************************************************************
uint32 TlsZF = OrigTLS.dwSizeofZeroFill;
uint32 TlsEA = OrigTLS.dwDataEndAddr;
uint32 TlsSA = OrigTLS.dwDataStartAddr;
// ******************************************************************
// * calculate tls adjustment
// ******************************************************************
TlsAdjust = TlsZF + (TlsEA - TlsSA) + 0x0F;
TlsAdjust &= 0xFFFFFFF0;
TlsAdjust += 4;
TlsAdjust /= 5;
TlsAdjust += 2;
TlsAdjust *= 4;
}
// ****************************************************************** // ******************************************************************
// * patch prolog function parameters // * patch prolog function parameters
// ****************************************************************** // ******************************************************************
@ -559,6 +586,7 @@ EmuExe::EmuExe(Xbe *x_Xbe, DebugMode x_debug_mode, char *x_debug_filename) : Exe
*(uint32 *)((uint32)m_bzSection[i] + 21) = m_SectionHeader[i].m_virtual_addr + m_OptionalHeader.m_image_base + 0x100 + x_Xbe->m_Header.dwSizeofHeaders; *(uint32 *)((uint32)m_bzSection[i] + 21) = m_SectionHeader[i].m_virtual_addr + m_OptionalHeader.m_image_base + 0x100 + x_Xbe->m_Header.dwSizeofHeaders;
*(uint32 *)((uint32)m_bzSection[i] + 26) = x_debug_mode; *(uint32 *)((uint32)m_bzSection[i] + 26) = x_debug_mode;
*(uint32 *)((uint32)m_bzSection[i] + 31) = m_SectionHeader[i].m_virtual_addr + m_OptionalHeader.m_image_base + 0x100 + x_Xbe->m_Header.dwSizeofHeaders + 260; *(uint32 *)((uint32)m_bzSection[i] + 31) = m_SectionHeader[i].m_virtual_addr + m_OptionalHeader.m_image_base + 0x100 + x_Xbe->m_Header.dwSizeofHeaders + 260;
*(uint32 *)((uint32)m_bzSection[i] + 36) = TlsAdjust;
printf("OK\n"); printf("OK\n");
} }

View File

@ -61,6 +61,7 @@ __declspec(allocate(".cxbxplg")) uint08 Prolog[] =
0x68, 0xC3, 0xC3, 0xC3, 0xC3, // push 0xC3C3C3C3 0x68, 0xC3, 0xC3, 0xC3, 0xC3, // push 0xC3C3C3C3
0x68, 0xC3, 0xC3, 0xC3, 0xC3, // push 0xC3C3C3C3 0x68, 0xC3, 0xC3, 0xC3, 0xC3, // push 0xC3C3C3C3
0x68, 0xC3, 0xC3, 0xC3, 0xC3, // push 0xC3C3C3C3 0x68, 0xC3, 0xC3, 0xC3, 0xC3, // push 0xC3C3C3C3
0x68, 0xC3, 0xC3, 0xC3, 0xC3, // push 0xC3C3C3C3
0xFF, 0xD6, // call esi 0xFF, 0xD6, // call esi
0xC3 // ret 0xC3 // ret
}; };

View File

@ -53,6 +53,8 @@ namespace xboxkrnl
// ****************************************************************** // ******************************************************************
static void EmuInstallWrappers(OOVPATable *OovpaTable, uint32 OovpaTableSize, void (*Entry)(), Xbe::Header *XbeHeader); static void EmuInstallWrappers(OOVPATable *OovpaTable, uint32 OovpaTableSize, void (*Entry)(), Xbe::Header *XbeHeader);
uint32 g_TlsAdjust = 0;
// ****************************************************************** // ******************************************************************
// * func: DllMain // * func: DllMain
// ****************************************************************** // ******************************************************************
@ -82,8 +84,10 @@ extern "C" CXBXKRNL_API void NTAPI EmuNoFunc()
// ****************************************************************** // ******************************************************************
// * func: EmuInit // * func: EmuInit
// ****************************************************************** // ******************************************************************
extern "C" CXBXKRNL_API void NTAPI EmuInit(Xbe::LibraryVersion *LibraryVersion, DebugMode DbgMode, char *szDebugFilename, Xbe::Header *XbeHeader, uint32 XbeHeaderSize, void (*Entry)()) extern "C" CXBXKRNL_API void NTAPI EmuInit(uint32 TlsAdjust, Xbe::LibraryVersion *LibraryVersion, DebugMode DbgMode, char *szDebugFilename, Xbe::Header *XbeHeader, uint32 XbeHeaderSize, void (*Entry)())
{ {
g_TlsAdjust = TlsAdjust;
// ****************************************************************** // ******************************************************************
// * debug console allocation (if configured) // * debug console allocation (if configured)
// ****************************************************************** // ******************************************************************
@ -118,6 +122,7 @@ extern "C" CXBXKRNL_API void NTAPI EmuInit(Xbe::LibraryVersion *LibraryVersion,
printf("Emu: EmuInit\n" printf("Emu: EmuInit\n"
"(\n" "(\n"
" TlsAdjust : 0x%.08X\n"
" LibraryVersion : 0x%.08X\n" " LibraryVersion : 0x%.08X\n"
" DebugConsole : 0x%.08X\n" " DebugConsole : 0x%.08X\n"
" DebugFilename : \"%s\"\n" " DebugFilename : \"%s\"\n"
@ -125,7 +130,7 @@ extern "C" CXBXKRNL_API void NTAPI EmuInit(Xbe::LibraryVersion *LibraryVersion,
" XBEHeaderSize : 0x%.08X\n" " XBEHeaderSize : 0x%.08X\n"
" Entry : 0x%.08X\n" " Entry : 0x%.08X\n"
");\n", ");\n",
LibraryVersion, DbgMode, szDebugFilename, XbeHeader, XbeHeaderSize, Entry); TlsAdjust, LibraryVersion, DbgMode, szDebugFilename, XbeHeader, XbeHeaderSize, Entry);
#else #else
printf("CxbxKrnl (0x%.08X): _DEBUG_TRACE disabled.\n", GetCurrentThreadId()); printf("CxbxKrnl (0x%.08X): _DEBUG_TRACE disabled.\n", GetCurrentThreadId());
@ -204,15 +209,13 @@ extern "C" CXBXKRNL_API void NTAPI EmuInit(Xbe::LibraryVersion *LibraryVersion,
{ {
EmuInitFS(); EmuInitFS();
EmuGenerateFS(); EmuGenerateFS(TlsAdjust);
EmuInitD3D(XbeHeader, XbeHeaderSize); EmuInitD3D(XbeHeader, XbeHeaderSize);
} }
printf("Emu (0x%.08X): Initial thread starting.\n", GetCurrentThreadId()); printf("Emu (0x%.08X): Initial thread starting.\n", GetCurrentThreadId());
// This must be enabled or the debugger may crash (sigh)
// __asm _emit 0xF1
EmuSwapFS(); // XBox FS EmuSwapFS(); // XBox FS
Entry(); Entry();

View File

@ -59,7 +59,7 @@ void EmuInitFS()
// ****************************************************************** // ******************************************************************
// * func: EmuGenerateFS // * func: EmuGenerateFS
// ****************************************************************** // ******************************************************************
void EmuGenerateFS() void EmuGenerateFS(int TlsAdjust)
{ {
NT_TIB *OrgNtTib; NT_TIB *OrgNtTib;
xboxkrnl::KPCR *NewPcr; xboxkrnl::KPCR *NewPcr;
@ -122,11 +122,46 @@ void EmuGenerateFS()
} }
// HACK: This converts from XBE stack form to Windows form (I guess?!) // HACK: This converts from XBE stack form to Windows form (I guess?!)
TLSPtr = (void*)((uint32)TLSPtr+20 + (2*8)); TLSPtr = (void*)((uint32)TLSPtr + TlsAdjust);
// TlsAdjust == -1 implies that there is no TLS
if(TlsAdjust == -1)
{
TLSPtr = new uint32;
*(uint32*)TLSPtr = 0;
}
NewPcr->PrcbData.CurrentThread->TlsData = TLSPtr; NewPcr->PrcbData.CurrentThread->TlsData = TLSPtr;
} }
/*
Data Start Address : 0x00000000
Data End Address : 0x00000000
TLS Index Address : 0x00030DC4
TLS Callback Address : 0x00000000
Size of Zero Fill : 0x0000000C -> 12
Characteristics : 0x00000000 -> 16
Data Start Address : 0x00010494 |
Data End Address : 0x000104A0 |-> 12
TLS Index Address : 0x00030D5C
TLS Callback Address : 0x00000000
Size of Zero Fill : 0x00000008 -> 8
Characteristics : 0x00000000 -> 26
Data Start Address : 0x00010494 |
Data End Address : 0x000104A4 |-> 16
TLS Index Address : 0x00030D5C
TLS Callback Address : 0x00000000
Size of Zero Fill : 0x00000008 -> 8
Characteristics : 0x00000000 -> 36
Data Start Address : 0x00010494 |
Data End Address : 0x000104A8 |-> 20
TLS Index Address : 0x00030D5C
TLS Callback Address : 0x00000000
Size of Zero Fill : 0x00000008 -> 8
Characteristics : 0x00000000 -> 52
*/
// ****************************************************************** // ******************************************************************
// * Swap into the "NewFS" // * Swap into the "NewFS"
// ****************************************************************** // ******************************************************************
@ -144,12 +179,10 @@ void EmuGenerateFS()
// ****************************************************************** // ******************************************************************
// * Save "TLSPtr" inside NewFS.StackBase // * Save "TLSPtr" inside NewFS.StackBase
// ****************************************************************** // ******************************************************************
__asm
{ {
__asm mov eax, TLSPtr
{ mov fs:[0x04], eax
mov eax, TLSPtr
mov fs:[0x04], eax
}
} }
// ****************************************************************** // ******************************************************************

View File

@ -96,7 +96,7 @@ DWORD WINAPI PCSTProxy
delete iPCSTProxyParam; delete iPCSTProxyParam;
EmuGenerateFS(); EmuGenerateFS(g_TlsAdjust);
// ****************************************************************** // ******************************************************************
// * debug trace // * debug trace

View File

@ -45,6 +45,7 @@ namespace xapi
#include "EmuXapi.h" #include "EmuXapi.h"
}; };
#include "Emu.h"
#include "EmuFS.h" #include "EmuFS.h"
#include "EmuD3D8.h" #include "EmuD3D8.h"
#include "EmuDInput.h" #include "EmuDInput.h"
@ -74,7 +75,7 @@ static DWORD WINAPI EmuCreateThreadProxy
delete iEmuCreateThreadProxyParam; delete iEmuCreateThreadProxyParam;
EmuGenerateFS(); EmuGenerateFS(g_TlsAdjust);
// ****************************************************************** // ******************************************************************
// * debug trace // * debug trace
@ -337,8 +338,6 @@ HANDLE WINAPI xapi::EmuCreateThread
lpThreadId lpThreadId
); );
printf("Returned : %d\n", RetHandle);
EmuSwapFS(); // XBox FS EmuSwapFS(); // XBox FS
return RetHandle; return RetHandle;

View File

@ -98,7 +98,7 @@ OOVPATable XAPI_1_0_4627[] =
"EmuCreateThread" "EmuCreateThread"
#endif #endif
}, },
*/ //*/
/* Too High Level /* Too High Level
// CloseHandle (* unchanged since 1.0.4361 *) // CloseHandle (* unchanged since 1.0.4361 *)
{ {
@ -110,7 +110,7 @@ OOVPATable XAPI_1_0_4627[] =
"EmuCloseHandle" "EmuCloseHandle"
#endif #endif
}, },
*/ //*/
/* Too High Level /* Too High Level
// XapiInitProcess (* unchanged since 1.0.4361 *) // XapiInitProcess (* unchanged since 1.0.4361 *)
{ {
@ -122,7 +122,7 @@ OOVPATable XAPI_1_0_4627[] =
"EmuXapiInitProcess" "EmuXapiInitProcess"
#endif #endif
}, },
*/ //*/
// XapiBootToDash (* unchanged since 1.0.3911 *) // XapiBootToDash (* unchanged since 1.0.3911 *)
{ {
(OOVPA*)&XapiBootDash_1_0_3911, (OOVPA*)&XapiBootDash_1_0_3911,
@ -143,7 +143,8 @@ OOVPATable XAPI_1_0_4627[] =
#ifdef _DEBUG_TRACE #ifdef _DEBUG_TRACE
"Emu__rtinit", "Emu__rtinit",
#endif #endif
},*/ },
//*/
/* Too High Level /* Too High Level
// __cinit (* unchanged since 1.0.3911 *) // __cinit (* unchanged since 1.0.3911 *)
{ {
@ -155,7 +156,7 @@ OOVPATable XAPI_1_0_4627[] =
"Emu__cinit", "Emu__cinit",
#endif #endif
}, },
*/ //*/
}; };
// ****************************************************************** // ******************************************************************