Skip to content

Commit b299c80

Browse files
authored
Clean up kernel functions (#64)
1 parent b1770a0 commit b299c80

5 files changed

Lines changed: 65 additions & 63 deletions

File tree

MarathonRecomp/kernel/heap.cpp

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ size_t Heap::Size(void* ptr)
6060
uint32_t RtlAllocateHeap(uint32_t heapHandle, uint32_t flags, uint32_t size)
6161
{
6262
void* ptr = g_userHeap.Alloc(size);
63-
// printf("RtlAllocateHeap %x, heapHandle\n", ptr);
6463
if ((flags & 0x8) != 0)
6564
memset(ptr, 0, size);
6665

@@ -71,7 +70,6 @@ uint32_t RtlAllocateHeap(uint32_t heapHandle, uint32_t flags, uint32_t size)
7170
uint32_t RtlReAllocateHeap(uint32_t heapHandle, uint32_t flags, uint32_t memoryPointer, uint32_t size)
7271
{
7372
void* ptr = g_userHeap.Alloc(size);
74-
// printf("RtlReAllocateHeap %x\n", ptr);
7573
if ((flags & 0x8) != 0)
7674
memset(ptr, 0, size);
7775

@@ -88,7 +86,6 @@ uint32_t RtlReAllocateHeap(uint32_t heapHandle, uint32_t flags, uint32_t memoryP
8886

8987
uint32_t RtlFreeHeap(uint32_t heapHandle, uint32_t flags, uint32_t memoryPointer)
9088
{
91-
// printf("RtlFreeHeap\n");
9289
if (memoryPointer != NULL)
9390
g_userHeap.Free(g_memory.Translate(memoryPointer));
9491

@@ -97,7 +94,6 @@ uint32_t RtlFreeHeap(uint32_t heapHandle, uint32_t flags, uint32_t memoryPointer
9794

9895
uint32_t RtlSizeHeap(uint32_t heapHandle, uint32_t flags, uint32_t memoryPointer)
9996
{
100-
// printf("RtlSizeHeap\n");
10197
if (memoryPointer != NULL)
10298
return (uint32_t)g_userHeap.Size(g_memory.Translate(memoryPointer));
10399

@@ -110,7 +106,6 @@ uint32_t XAllocMem(uint32_t size, uint32_t flags)
110106
g_userHeap.AllocPhysical(size, (1ull << ((flags >> 24) & 0xF))) :
111107
g_userHeap.Alloc(size);
112108

113-
// printf("XAllocMem %x (size: %x)\n", ptr, size);
114109
if ((flags & 0x40000000) != 0)
115110
memset(ptr, 0, size);
116111

@@ -120,40 +115,25 @@ uint32_t XAllocMem(uint32_t size, uint32_t flags)
120115

121116
void XFreeMem(uint32_t baseAddress, uint32_t flags)
122117
{
123-
// printf("XFreeMem %x\n", baseAddress);
124118
if (baseAddress != NULL)
125-
{
126119
g_userHeap.Free(g_memory.Translate(baseAddress));
127-
}
128-
else
129-
{
130-
printf("XFreeMem baseAddress is null %x\n", baseAddress);
131-
// __builtin_trap();
132-
}
133120
}
134121

135122
uint32_t XVirtualAlloc(void *lpAddress, unsigned int dwSize, unsigned int flAllocationType, unsigned int flProtect)
136123
{
137-
if (lpAddress != nullptr)
138-
{
139-
printf("XVirtualAlloc lpAddress is not null %p\n", lpAddress);
140-
return 0;
141-
}
142-
uint32_t addr = g_memory.MapVirtual(g_userHeap.Alloc(dwSize));
143-
printf("XVirtualAlloc %x\n", addr);
144-
return addr;
124+
assert(!lpAddress);
125+
return g_memory.MapVirtual(g_userHeap.Alloc(dwSize));
145126
}
146127

147128
uint32_t XVirtualFree(uint32_t lpAddress, unsigned int dwSize, unsigned int dwFreeType)
148129
{
149-
printf("XVirtualFree %x\n", lpAddress);
150130
if ((dwFreeType & 0x8000) != 0 && dwSize)
151-
{
152-
return 0;
153-
}
131+
return FALSE;
132+
154133
if (lpAddress)
155134
g_userHeap.Free(g_memory.Translate(lpAddress));
156-
return 1;
135+
136+
return TRUE;
157137
}
158138

159139
GUEST_FUNCTION_HOOK(sub_82915668, XVirtualAlloc);

MarathonRecomp/kernel/imports.cpp

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <ntstatus.h>
2020
#endif
2121

22-
std::vector<uint32_t> g_doNotDestroyHandles{};
22+
std::unordered_map<uint32_t, uint32_t> g_handleDuplicates{};
2323

2424
struct Event final : KernelObject, HostObject<XKEVENT>
2525
{
@@ -336,7 +336,6 @@ uint32_t XGetAVPack()
336336

337337
void XamLoaderTerminateTitle()
338338
{
339-
printf("XamLoaderTerminateTitle\n");
340339
LOG_UTILITY("!!! STUB !!!");
341340
}
342341

@@ -376,7 +375,7 @@ uint32_t NtCreateFile
376375
uint32_t CreateOptions
377376
)
378377
{
379-
printf("NtCreateFile\n");
378+
LOG_UTILITY("!!! STUB !!!");
380379
return 0;
381380
}
382381

@@ -387,10 +386,13 @@ uint32_t NtClose(uint32_t handle)
387386

388387
if (IsKernelObject(handle))
389388
{
390-
auto it = std::find(g_doNotDestroyHandles.begin(), g_doNotDestroyHandles.end(), handle);
391-
if (it == g_doNotDestroyHandles.end()) {
389+
// If the handle was duplicated, just decrement the duplication count. Otherwise, delete the object.
390+
const auto& it = g_handleDuplicates.find(handle);
391+
if (it == g_handleDuplicates.end() || it->second == 0)
392392
DestroyKernelObject(handle);
393-
}
393+
else if (--it->second == 0)
394+
g_handleDuplicates.erase(it);
395+
394396
return 0;
395397
}
396398
else
@@ -432,6 +434,9 @@ uint32_t XamLoaderSetLaunchData()
432434

433435
uint32_t NtWaitForSingleObjectEx(uint32_t Handle, uint32_t WaitMode, uint32_t Alertable, be<int64_t>* Timeout)
434436
{
437+
if (Handle == GUEST_INVALID_HANDLE_VALUE)
438+
return 0xFFFFFFFF;
439+
435440
uint32_t timeout = GuestTimeoutToMilliseconds(Timeout);
436441
// assert(timeout == 0 || timeout == INFINITE);
437442

@@ -555,11 +560,29 @@ void __C_specific_handler_x()
555560
LOG_UTILITY("!!! STUB !!!");
556561
}
557562

558-
void RtlNtStatusToDosError(int Status)
563+
uint32_t RtlNtStatusToDosError(uint32_t Status)
559564
{
560-
LOG_UTILITY("!!! STUB !!!");
561-
printf("RtlNtStatusToDosError: %x\n", Status);
562-
// __builtin_trap();
565+
// See https://github.com/wine-mirror/wine/blob/master/dlls/ntdll/error.c#L47-L64
566+
if (Status == 0 || (Status & 0x20000000) != 0)
567+
return Status;
568+
569+
if ((Status & 0xF0000000) == 0xD0000000)
570+
Status &= ~0x10000000;
571+
572+
const uint32_t hi = (Status >> 16) & 0xFFFF;
573+
if (hi == 0x8007 || hi == 0xC001 || hi == 0xC007)
574+
return Status & 0xFFFF;
575+
576+
switch (Status)
577+
{
578+
case STATUS_NOT_IMPLEMENTED:
579+
return ERROR_CALL_NOT_IMPLEMENTED;
580+
case STATUS_SEMAPHORE_LIMIT_EXCEEDED:
581+
return ERROR_TOO_MANY_POSTS;
582+
default:
583+
LOGF_WARNING("Unimplemented NtStatus translation: {:#08x}", Status);
584+
return Status;
585+
}
563586
}
564587

565588
void XexGetProcedureAddress()
@@ -642,20 +665,28 @@ void NtReadFile()
642665
LOG_UTILITY("!!! STUB !!!");
643666
}
644667

645-
int NtDuplicateObject(uint32_t sourceHandle, be<uint32_t>* targetHandle, uint32_t options)
668+
uint32_t NtDuplicateObject(uint32_t SourceHandle, be<uint32_t>* TargetHandle, uint32_t Options)
646669
{
647-
auto obj = GetKernelObject(sourceHandle);
648-
// HACK: Duplicating handle doesn't supported now, so we just these handles to array to ignore of closing these handles
649-
printf("NtDuplicateObject: %x %x %d, obj: %x\n", sourceHandle, targetHandle, options, obj);
650-
auto it = std::find(g_doNotDestroyHandles.begin(), g_doNotDestroyHandles.end(), sourceHandle);
651-
if (it == g_doNotDestroyHandles.end()) {
652-
g_doNotDestroyHandles.push_back(sourceHandle);
670+
if (SourceHandle == GUEST_INVALID_HANDLE_VALUE)
671+
return 0xFFFFFFFF;
672+
673+
if (IsKernelObject(SourceHandle))
674+
{
675+
// Increment handle duplicate count.
676+
const auto& it = g_handleDuplicates.find(SourceHandle);
677+
if (it != g_handleDuplicates.end())
678+
++it->second;
679+
else
680+
g_handleDuplicates[SourceHandle] = 1;
681+
682+
*TargetHandle = SourceHandle;
683+
return 0;
684+
}
685+
else
686+
{
687+
assert(false && "Unrecognized kernel object.");
688+
return 0xFFFFFFFF;
653689
}
654-
if (!obj) return -1;
655-
uint32_t newHandle = 0;
656-
newHandle = sourceHandle;
657-
*targetHandle = newHandle;
658-
return 0;
659690
}
660691

661692
void NtAllocateVirtualMemory()
@@ -758,13 +789,11 @@ void RtlEnterCriticalSection(XRTL_CRITICAL_SECTION* cs)
758789

759790
void RtlImageXexHeaderField()
760791
{
761-
printf("RtlImageXexHeaderField\n");
762792
LOG_UTILITY("!!! STUB !!!");
763793
}
764794

765795
void HalReturnToFirmware()
766796
{
767-
printf("HalReturnToFirmware\n");
768797
LOG_UTILITY("!!! STUB !!!");
769798
}
770799

@@ -909,7 +938,7 @@ void sprintf_x()
909938

910939
int32_t ExRegisterTitleTerminateNotification(uint32_t* reg, uint32_t create)
911940
{
912-
printf("ExRegisterTitleTerminateNotification: %x %d\n", reg, create);
941+
LOG_UTILITY("!!! STUB !!!");
913942
return 0;
914943
}
915944

@@ -1111,7 +1140,6 @@ uint32_t KeTlsGetValue(uint32_t dwTlsIndex)
11111140

11121141
uint32_t KeTlsSetValue(uint32_t dwTlsIndex, uint32_t lpTlsValue)
11131142
{
1114-
printf("KeTlsSetValue\n");
11151143
KeTlsGetValueRef(dwTlsIndex) = lpTlsValue;
11161144
return TRUE;
11171145
}

MarathonRecomp/kernel/io/file_system.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ FileHandle* XCreateFileA
9191
uint32_t dwFlagsAndAttributes
9292
)
9393
{
94-
printf("XCreateFileA %s\n", lpFileName);
9594
assert(((dwDesiredAccess & ~(GENERIC_READ | GENERIC_WRITE | FILE_READ_DATA)) == 0) && "Unknown desired access bits.");
9695
assert(((dwShareMode & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) == 0) && "Unknown share mode bits.");
9796
assert(((dwCreationDisposition & ~(CREATE_NEW | CREATE_ALWAYS)) == 0) && "Unknown creation disposition bits.");
@@ -148,7 +147,6 @@ FileHandle* XCreateFileA
148147

149148
static uint32_t XGetFileSizeA(FileHandle* hFile, be<uint32_t>* lpFileSizeHigh)
150149
{
151-
printf("XGetFileSizeA %x\n", hFile);
152150
std::error_code ec;
153151
auto fileSize = std::filesystem::file_size(hFile->path, ec);
154152
if (!ec)
@@ -190,7 +188,6 @@ uint32_t XReadFile
190188
XOVERLAPPED* lpOverlapped
191189
)
192190
{
193-
// printf("XReadFile %x %d\n", hFile, nNumberOfBytesToRead);
194191
uint32_t result = FALSE;
195192
if (lpOverlapped != nullptr)
196193
{
@@ -248,11 +245,6 @@ uint32_t XSetFilePointer(FileHandle* hFile, int32_t lDistanceToMove, be<int32_t>
248245
break;
249246
}
250247

251-
// if (!hFile) {
252-
printf("XSetFilePointer %x %d %d %d %d\n", hFile, lDistanceToMove, distanceToMoveHigh, dwMoveMethod, streamSeekDir);
253-
// return INVALID_SET_FILE_POINTER;
254-
// }
255-
256248
hFile->stream.clear();
257249
hFile->stream.seekg(streamOffset, streamSeekDir);
258250
if (hFile->stream.bad())
@@ -263,7 +255,7 @@ uint32_t XSetFilePointer(FileHandle* hFile, int32_t lDistanceToMove, be<int32_t>
263255
std::streampos streamPos = hFile->stream.tellg();
264256
if (lpDistanceToMoveHigh != nullptr)
265257
*lpDistanceToMoveHigh = int32_t(streamPos >> 32U);
266-
printf("XSetFilePointer streamPos: %d\n", streamPos);
258+
267259
return uint32_t(streamPos);
268260
}
269261

MarathonRecomp/kernel/xam.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ uint32_t XamContentCreateEx(uint32_t dwUserIndex, const char* szRootName, const
314314
uint32_t dwContentFlags, be<uint32_t>* pdwDisposition, be<uint32_t>* pdwLicenseMask,
315315
uint32_t dwFileCacheSize, uint64_t uliContentSize, PXXOVERLAPPED pOverlapped)
316316
{
317-
printf("XamContentCreateEx: %d, %s %d\n", dwUserIndex, szRootName, dwContentFlags);
318317
const auto& registry = gContentRegistry[pContentData->dwContentType - 1];
319318
const auto exists = registry.contains(StringHash(pContentData->szFileName));
320319
const auto mode = dwContentFlags & 0xF;

MarathonRecomp/kernel/xdm.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
#define STATUS_SUCCESS 0x00000000
1515
#define STATUS_WAIT_0 0x00000000
1616
#define STATUS_USER_APC 0x000000C0
17-
#define STATUS_SEMAPHORE_LIMIT_EXCEEDED 0xC0000047
1817
#define STATUS_TIMEOUT 0x00000102
18+
#define STATUS_NOT_IMPLEMENTED 0xC0000002
19+
#define STATUS_SEMAPHORE_LIMIT_EXCEEDED 0xC0000047
1920
#define STATUS_FAIL_CHECK 0xC0000229
2021
#define INFINITE 0xFFFFFFFF
2122
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
@@ -40,7 +41,9 @@
4041
#define ERROR_PATH_NOT_FOUND 0x3
4142
#define ERROR_ACCESS_DENIED 0x5
4243
#define ERROR_FILE_EXISTS 0x50
44+
#define ERROR_CALL_NOT_IMPLEMENTED 0x78
4345
#define ERROR_BAD_ARGUMENTS 0xA0
46+
#define ERROR_TOO_MANY_POSTS 0x12A
4447
#define ERROR_DEVICE_NOT_CONNECTED 0x48F
4548
#define PAGE_READWRITE 0x04
4649

0 commit comments

Comments
 (0)