From 053781afd934b1d58df62f93109bb513ed0ccd23 Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Sat, 26 Nov 2016 10:04:47 +0000 Subject: [PATCH 1/7] Add newline to return statement logging --- src/Common/Logging.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Logging.h b/src/Common/Logging.h index fea003e26..b15f6cadf 100644 --- a/src/Common/Logging.h +++ b/src/Common/Logging.h @@ -95,7 +95,7 @@ extern thread_local std::string _logPrefix; // LOG_FUNC_RESULT logs the function return result #define LOG_FUNC_RESULT(r) \ - std::cout << _logPrefix << " returns " << r; + std::cout << _logPrefix << " returns " << r << "\n"; #else #define LOG_FUNC_BEGIN #define LOG_FUNC_ARG(arg) From eebf17ac23b41f20f0a35f1c4c5f94c98a8aa977 Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Sat, 26 Nov 2016 21:19:02 +0000 Subject: [PATCH 2/7] Restore XREF only function handler, seems like it was too early to remove this --- src/CxbxKrnl/HLEIntercept.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/CxbxKrnl/HLEIntercept.cpp b/src/CxbxKrnl/HLEIntercept.cpp index 210277096..4e6466295 100644 --- a/src/CxbxKrnl/HLEIntercept.cpp +++ b/src/CxbxKrnl/HLEIntercept.cpp @@ -776,7 +776,11 @@ static void EmuInstallWrappers(OOVPATable *OovpaTable, uint32 OovpaTableSize, Xb DbgPrintf("HLE: 0x%.08X -> %s\n", pFunc, OovpaTable[a].szFuncName); #endif - if(OovpaTable[a].lpRedirect != 0) + if(OovpaTable[a].lpRedirect == 0) + { + EmuInstallWrapper(pFunc, EmuXRefFailure); + } + else { EmuInstallWrapper(pFunc, OovpaTable[a].lpRedirect); funcExclude[fcount++] = (uint32)pFunc; From 661a4c06822ed8790776e405b7a89ea50486ae53 Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Sat, 26 Nov 2016 21:19:30 +0000 Subject: [PATCH 3/7] Add EBP Register to EmuX86_GetRegisterPointer --- src/CxbxKrnl/EmuX86.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CxbxKrnl/EmuX86.cpp b/src/CxbxKrnl/EmuX86.cpp index 99a167ebc..9dd54d59b 100644 --- a/src/CxbxKrnl/EmuX86.cpp +++ b/src/CxbxKrnl/EmuX86.cpp @@ -76,6 +76,8 @@ DWORD* EmuX86_GetRegisterPointer(LPEXCEPTION_POINTERS e, Zydis::Register reg) return &e->ContextRecord->Edi; case Zydis::Register::ESI: return &e->ContextRecord->Esi; + case Zydis::Register::EBP: + return &e->ContextRecord->Ebp; } return nullptr; From 7323a2c711b10ed48fad31867b62ac21bdd30d1c Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Sat, 26 Nov 2016 22:07:11 +0000 Subject: [PATCH 4/7] KfLower/RaiseIrql should be fastcall, not stdcall. Add implementation for KeRaiseIrql from ReactOS as it seems to be identical to the Xbox implementation. We may not be able to do the same for KfLowerIrql though, the implementation looks (slightly) different to me. --- import/OpenXDK/include/xboxkrnl/xbox.h | 4 +-- src/CxbxKrnl/EmuKrnl.cpp | 36 ++++++++++++++++++++------ src/CxbxKrnl/EmuKrnlKe.cpp | 2 +- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/import/OpenXDK/include/xboxkrnl/xbox.h b/import/OpenXDK/include/xboxkrnl/xbox.h index 18c63be99..49c0d7a86 100644 --- a/import/OpenXDK/include/xboxkrnl/xbox.h +++ b/import/OpenXDK/include/xboxkrnl/xbox.h @@ -67,7 +67,7 @@ XBSYSAPI EXPORTNUM(89) BOOLEAN KdDebuggerNotPresent; // ****************************************************************** // * KfRaiseIrql // ****************************************************************** -XBSYSAPI EXPORTNUM(160) UCHAR* NTAPI KfRaiseIrql +XBSYSAPI EXPORTNUM(160) UCHAR _fastcall KfRaiseIrql ( IN UCHAR NewIrql ); @@ -75,7 +75,7 @@ XBSYSAPI EXPORTNUM(160) UCHAR* NTAPI KfRaiseIrql // ****************************************************************** // * KfLowerIrql // ****************************************************************** -XBSYSAPI EXPORTNUM(161) VOID NTAPI KfLowerIrql +XBSYSAPI EXPORTNUM(161) VOID _fastcall KfLowerIrql ( IN UCHAR NewIrql ); diff --git a/src/CxbxKrnl/EmuKrnl.cpp b/src/CxbxKrnl/EmuKrnl.cpp index 405c1dcc0..4f40765ab 100644 --- a/src/CxbxKrnl/EmuKrnl.cpp +++ b/src/CxbxKrnl/EmuKrnl.cpp @@ -47,6 +47,8 @@ namespace xboxkrnl #include "Logging.h" +#include "CxbxKrnl.h" + // prevent name collisions namespace NtDll { @@ -126,24 +128,42 @@ using namespace xboxkrnl; // ****************************************************************** // * 0x00A0 - KfRaiseIrql // ****************************************************************** -XBSYSAPI EXPORTNUM(160) xboxkrnl::UCHAR* NTAPI xboxkrnl::KfRaiseIrql +XBSYSAPI EXPORTNUM(160) xboxkrnl::UCHAR _fastcall xboxkrnl::KfRaiseIrql ( IN UCHAR NewIrql ) { - // HACK: Not thread safe! - static xboxkrnl::UCHAR previousIrqlValue = 0; - LOG_FUNC_ONE_ARG(NewIrql); - - // Return addr where old irq level should be stored - RETURN(&previousIrqlValue); + + UCHAR OldIrql; + KPCR* Pcr = nullptr; + + // Fetch KPCR data structure + __asm { + push eax + mov eax, fs:[0x14] + mov Pcr, eax + pop eax + } + + if (NewIrql < Pcr->Irql) { + // TODO: Enable this after KeBugCheck is implemented + //KeBugCheck(IRQL_NOT_GREATER_OR_EQUAL); + // for (;;); + + CxbxKrnlCleanup("IRQL_NOT_GREATER_OR_EQUAL"); + } + + OldIrql = Pcr->Irql; + Pcr->Irql = NewIrql; + + RETURN(OldIrql); } // ****************************************************************** // * 0x00A1 - KfLowerIrql // ****************************************************************** -XBSYSAPI EXPORTNUM(161) VOID NTAPI xboxkrnl::KfLowerIrql +XBSYSAPI EXPORTNUM(161) VOID _fastcall xboxkrnl::KfLowerIrql ( IN UCHAR NewIrql ) diff --git a/src/CxbxKrnl/EmuKrnlKe.cpp b/src/CxbxKrnl/EmuKrnlKe.cpp index 69bb184d1..a69c20575 100644 --- a/src/CxbxKrnl/EmuKrnlKe.cpp +++ b/src/CxbxKrnl/EmuKrnlKe.cpp @@ -316,7 +316,7 @@ XBSYSAPI EXPORTNUM(129) xboxkrnl::UCHAR NTAPI xboxkrnl::KeRaiseIrqlToDpcLevel() // I really tried to avoid adding this... // __asm int 3; // CxbxKrnlCleanup("KeRaiseIrqlToDpcLevel not implemented! (Tell blueshogun -_-)"); - // LOG_UNIMPLEMENTED(); + LOG_UNIMPLEMENTED(); RETURN(0); } From 86c3bbf947d50815abdffd7578a0a00d06391b28 Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Sat, 26 Nov 2016 22:21:15 +0000 Subject: [PATCH 5/7] This is not really needed --- src/CxbxKrnl/HLEIntercept.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/CxbxKrnl/HLEIntercept.cpp b/src/CxbxKrnl/HLEIntercept.cpp index 4e6466295..7666e4ef1 100644 --- a/src/CxbxKrnl/HLEIntercept.cpp +++ b/src/CxbxKrnl/HLEIntercept.cpp @@ -157,16 +157,7 @@ void EmuHLEIntercept(Xbe::LibraryVersion *pLibraryVersion, Xbe::Header *pXbeHead } } - // - // initialize openxdk emulation (TODO) - // - - if(pLibraryVersion == 0) - { - DbgPrintf("HLE: Detected OpenXDK application...\n"); - } - - // + // // initialize Microsoft XDK emulation // @@ -796,3 +787,4 @@ static void EmuXRefFailure() CxbxKrnlCleanup("XRef-only function body reached. Fatal Error."); } + \ No newline at end of file From 08228c30a194692ba15db688abcebd934b6bfa78 Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Sat, 26 Nov 2016 22:21:44 +0000 Subject: [PATCH 6/7] Fix a crash when opening an XBE with an invalid TLS structure --- src/CxbxKrnl/EmuFS.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/CxbxKrnl/EmuFS.cpp b/src/CxbxKrnl/EmuFS.cpp index d412e37f8..6c0e702db 100644 --- a/src/CxbxKrnl/EmuFS.cpp +++ b/src/CxbxKrnl/EmuFS.cpp @@ -328,6 +328,10 @@ void EmuInitFS() // generate fs segment selector void EmuGenerateFS(Xbe::TLS *pTLS, void *pTLSData) { + // Make sure the TLS Start and End addresses are within Xbox Memory + if (pTLS->dwDataStartAddr > XBOX_MEMORY_SIZE || pTLS->dwDataEndAddr > XBOX_MEMORY_SIZE) { + return; + } NT_TIB *OrgNtTib; xboxkrnl::KPCR *NewPcr; From c7caf082e2ce459acc114a99480bad174706519f Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Sat, 26 Nov 2016 23:01:10 +0000 Subject: [PATCH 7/7] Add D3DFMT_X1R5G5B5 --- src/CxbxKrnl/EmuD3D8/Convert.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/CxbxKrnl/EmuD3D8/Convert.cpp b/src/CxbxKrnl/EmuD3D8/Convert.cpp index 9e0209eec..d79725224 100644 --- a/src/CxbxKrnl/EmuD3D8/Convert.cpp +++ b/src/CxbxKrnl/EmuD3D8/Convert.cpp @@ -147,6 +147,9 @@ XTL::D3DFORMAT XTL::EmuXB2PC_D3DFormat(X_D3DFORMAT Format) case X_D3DFMT_A8: // Swizzled result = D3DFMT_A8; break; + case X_D3DFMT_LIN_X1R5G5B5: // Linear + result = D3DFMT_X1R5G5B5; + break; case X_D3DFMT_LIN_A4R4G4B4: case // Linear X_D3DFMT_A4R4G4B4: // Swizzled result = D3DFMT_A4R4G4B4; @@ -311,7 +314,9 @@ XTL::X_D3DFORMAT XTL::EmuPC2XB_D3DFormat(D3DFORMAT Format) result = X_D3DFMT_LIN_A4R4G4B4; break; // Linear // Result := X_D3DFMT_A4R4G4B4; // Swizzled - + case D3DFMT_X1R5G5B5: // Linear + result = X_D3DFMT_LIN_X1R5G5B5; + break; case D3DFMT_A8: result = X_D3DFMT_A8; break;