From ee2e19ce38b583417ed642ddea1f2f89717b614a Mon Sep 17 00:00:00 2001 From: fincs Date: Sat, 16 Feb 2019 16:42:08 +0100 Subject: [PATCH] Improve names/documentation for jit kernel patch functions in detect.h/c + codestylefixes --- nx/include/switch/kernel/detect.h | 6 +-- nx/source/kernel/detect.c | 64 +++++++++++++++---------------- nx/source/kernel/jit.c | 2 +- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/nx/include/switch/kernel/detect.h b/nx/include/switch/kernel/detect.h index edf4efc8..1871b872 100644 --- a/nx/include/switch/kernel/detect.h +++ b/nx/include/switch/kernel/detect.h @@ -21,6 +21,6 @@ bool kernelAbove600(void); bool detectDebugger(void); /// Returns true if the kernel is patched to allow self-process-jit. -bool detectCfwJitPatch(void); -/// After this has been called, libnx will pretend that CFW is not present. For testing purposes only. -void detectPretendNotCfwForTesting(void); +bool detectJitKernelPatch(void); +/// After this has been called, libnx will ignore the self-process-jit kernel patch. For testing purposes only. +void detectIgnoreJitKernelPatch(void); diff --git a/nx/source/kernel/detect.c b/nx/source/kernel/detect.c index 403d1177..a5d44dbf 100644 --- a/nx/source/kernel/detect.c +++ b/nx/source/kernel/detect.c @@ -14,9 +14,9 @@ static bool g_IsAbove400; static bool g_IsAbove500; static bool g_IsAbove600; -static bool g_CfwJitCached = 0; -static Mutex g_CfwJitMutex; -static bool g_CfwJitPatchDetected; +static bool g_JitKernelPatchCached = 0; +static Mutex g_JitKernelPatchMutex; +static bool g_JitKernelPatchDetected; static void _CacheVersion(void) { @@ -47,43 +47,41 @@ static void _CacheVersion(void) mutexUnlock(&g_VersionMutex); } -static void _CacheCfwJit(void) +static void _CacheJitKernelPatch(void) { - if (__atomic_load_n(&g_CfwJitCached, __ATOMIC_SEQ_CST)) + if (__atomic_load_n(&g_JitKernelPatchCached, __ATOMIC_SEQ_CST)) return; - mutexLock(&g_CfwJitMutex); + mutexLock(&g_JitKernelPatchMutex); - if (g_CfwJitCached) { - mutexUnlock(&g_CfwJitMutex); + if (g_JitKernelPatchCached) { + mutexUnlock(&g_JitKernelPatchMutex); return; } void* heap = memalign(0x1000, 0x1000); - if (heap != NULL) - { - Handle code; - Result rc; - rc = svcCreateCodeMemory(&code, heap, 0x1000); + if (heap != NULL) { + Handle code; + Result rc; + rc = svcCreateCodeMemory(&code, heap, 0x1000); - if (R_SUCCEEDED(rc)) - { - // On an unpatched kernel on 5.0.0 and above, this would return 0xD401. - // It is not allowed for the creator-process of a CodeMemory object to use svcControlCodeMemory on it. - // If the patch is present, the function should return 0xF001, because -1 is not a valid enum CodeOperation. - rc = svcControlCodeMemory(code, -1, 0, 0x1000, 0); + if (R_SUCCEEDED(rc)) { + // On an unpatched kernel on 5.0.0 and above, this would return InvalidMemoryState (0xD401). + // It is not allowed for the creator-process of a CodeMemory object to use svcControlCodeMemory on it. + // If the patch is present, the function should return InvalidEnumValue (0xF001), because -1 is not a valid enum CodeOperation. + rc = svcControlCodeMemory(code, -1, 0, 0x1000, 0); - g_CfwJitPatchDetected = (rc == 0xF001); - __atomic_store_n(&g_CfwJitCached, true, __ATOMIC_SEQ_CST); + g_JitKernelPatchDetected = R_VALUE(rc) == KERNELRESULT(InvalidEnumValue); + __atomic_store_n(&g_JitKernelPatchCached, true, __ATOMIC_SEQ_CST); - svcCloseHandle(code); - } + svcCloseHandle(code); + } - free(heap); + free(heap); } - mutexUnlock(&g_CfwJitMutex); + mutexUnlock(&g_JitKernelPatchMutex); } bool kernelAbove200(void) { @@ -117,14 +115,14 @@ bool detectDebugger(void) { return !!tmp; } -bool detectCfwJitPatch(void) { - _CacheCfwJit(); - return g_CfwJitPatchDetected; +bool detectJitKernelPatch(void) { + _CacheJitKernelPatch(); + return g_JitKernelPatchDetected; } -void detectPretendNotCfwForTesting(void) { - mutexLock(&g_CfwJitMutex); - g_CfwJitPatchDetected = false; - __atomic_store_n(&g_CfwJitCached, true, __ATOMIC_SEQ_CST); - mutexUnlock(&g_CfwJitMutex); +void detectIgnoreJitKernelPatch(void) { + mutexLock(&g_JitKernelPatchMutex); + g_JitKernelPatchDetected = false; + __atomic_store_n(&g_JitKernelPatchCached, true, __ATOMIC_SEQ_CST); + mutexUnlock(&g_JitKernelPatchMutex); } diff --git a/nx/source/kernel/jit.c b/nx/source/kernel/jit.c index 018dfe25..1cbb06b4 100644 --- a/nx/source/kernel/jit.c +++ b/nx/source/kernel/jit.c @@ -16,7 +16,7 @@ Result jitCreate(Jit* j, size_t size) // Use new jit primitive introduced in 4.0.0, if available. // Not usable with 5.0.0+ since svcMapJitMemory doesn't allow using that SVC under the same process which owns that object. if (kernelAbove400() && envIsSyscallHinted(0x4B) && envIsSyscallHinted(0x4C) - && (!kernelAbove500() || detectCfwJitPatch())) { + && (!kernelAbove500() || detectJitKernelPatch())) { type = JitType_JitMemory; } // Fall back to MapProcessCodeMemory if available.