// ****************************************************************** // * // * .,-::::: .,:: .::::::::. .,:: .: // * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;; // * [[[ '[[,,[[' [[[__[[\. '[[,,[[' // * $$$ Y$$$P $$""""Y$$ Y$$$P // * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo, // * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm, // * // * Cxbx->Win32->CxbxKrnl->EmuFS.cpp // * // * This file is part of the Cxbx project. // * // * Cxbx and Cxbe are free software; you can redistribute them // * and/or modify them under the terms of the GNU General Public // * License as published by the Free Software Foundation; either // * version 2 of the license, or (at your option) any later version. // * // * This program is distributed in the hope that it will be useful, // * but WITHOUT ANY WARRANTY; without even the implied warranty of // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // * GNU General Public License for more details. // * // * You should have recieved a copy of the GNU General Public License // * along with this program; see the file COPYING. // * If not, write to the Free Software Foundation, Inc., // * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA. // * // * (c) 2002-2003 Aaron Robinson // * // * All rights reserved // * // ****************************************************************** #define _CXBXKRNL_INTERNAL #define _XBOXKRNL_LOCAL_ // ****************************************************************** // * prevent name collisions // ****************************************************************** namespace xboxkrnl { #include }; #include "EmuFS.h" #include "EmuLDT.h" #undef FIELD_OFFSET // prevent macro redefinition warnings #include #include // ****************************************************************** // * func: EmuInitFS // ****************************************************************** void EmuInitFS() { EmuInitLDT(); } // ****************************************************************** // * func: EmuGenerateFS // ****************************************************************** void EmuGenerateFS(Xbe::TLS *pTLS) { NT_TIB *OrgNtTib; xboxkrnl::KPCR *NewPcr; uint16 NewFS = -1, OrgFS = -1; // ****************************************************************** // * Obtain "OrgFS" // ****************************************************************** __asm { // Obtain "OrgFS" mov ax, fs mov OrgFS, ax // Obtain "OrgNtTib" mov eax, fs:[0x18] mov OrgNtTib, eax } // ****************************************************************** // * Allocate LDT entry // ****************************************************************** { uint32 dwSize = sizeof(xboxkrnl::KPCR); NewPcr = (xboxkrnl::KPCR*)new char[dwSize]; memset(NewPcr, 0, sizeof(*NewPcr)); NewFS = EmuAllocateLDT((uint32)NewPcr, (uint32)NewPcr + dwSize); } // ****************************************************************** // * Update "OrgFS" with NewFS and (bIsBoxFs = false) // ****************************************************************** { __asm { mov ax, NewFS mov bh, 0 mov fs:[0x14], ax mov fs:[0x16], bh } } // ****************************************************************** // * Generate TIB // ****************************************************************** { xboxkrnl::KTHREAD *KThread = new xboxkrnl::KTHREAD(); memcpy(&NewPcr->NtTib, OrgNtTib, sizeof(NT_TIB)); NewPcr->NtTib.Self = &NewPcr->NtTib; NewPcr->PrcbData.CurrentThread = KThread; NewPcr->Prcb = &NewPcr->PrcbData; NewPcr->PrcbData.CurrentThread->TlsData = (void*)pTLS->dwTLSIndexAddr; } // ****************************************************************** // * Swap into the "NewFS" // ****************************************************************** EmuSwapFS(); // ****************************************************************** // * Update "NewFS" with OrgFS and (bIsBoxFs = true) // ****************************************************************** { __asm { mov ax, OrgFS mov bh, 1 mov fs:[0x14], ax mov fs:[0x16], bh } } // ****************************************************************** // * Save "TLSPtr" inside NewFS.StackBase // ****************************************************************** { uint32 dwTLSIndexAddr = pTLS->dwTLSIndexAddr; __asm { mov eax, dwTLSIndexAddr mov fs:[0x04], eax } } // ****************************************************************** // * Swap back into the "OrgFS" // ****************************************************************** EmuSwapFS(); // ****************************************************************** // * Debug output // ****************************************************************** printf("EmuFS (0x%.08X): OrgFS=%d NewFS=%d pTLS=%d\n", GetCurrentThreadId(), OrgFS, NewFS, pTLS); } // ****************************************************************** // * func: EmuCleanupFS // ****************************************************************** void EmuCleanupFS() { uint16 wSwapFS = 0; __asm { mov ax, fs:[0x14] // FS.ArbitraryUserPointer mov wSwapFS, ax } if(wSwapFS == 0) return; // TODO: Cleanup TLS data (after it's implemented) EmuDeallocateLDT(wSwapFS); }