Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebased: Wrap some SysMemForKernel's nids, fixing #7960 #12225

Merged
merged 13 commits into from
Apr 26, 2020
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,8 @@ add_library(${CoreLibName} ${CoreLinkType}
Core/HLE/sceKernelAlarm.h
Core/HLE/sceKernelEventFlag.cpp
Core/HLE/sceKernelEventFlag.h
Core/HLE/sceKernelHeap.cpp
Core/HLE/sceKernelHeap.h
Core/HLE/sceKernelInterrupt.cpp
Core/HLE/sceKernelInterrupt.h
Core/HLE/sceKernelMbx.cpp
Expand Down
2 changes: 2 additions & 0 deletions Core/Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@
<ClCompile Include="Debugger\WebSocket\WebSocketUtils.cpp" />
<ClCompile Include="FileSystems\BlobFileSystem.cpp" />
<ClCompile Include="HLE\KUBridge.cpp" />
<ClCompile Include="HLE\sceKernelHeap.cpp" />
<ClCompile Include="HLE\sceUsbAcc.cpp" />
<ClCompile Include="HLE\sceUsbCam.cpp" />
<ClCompile Include="HLE\sceUsbMic.cpp" />
Expand Down Expand Up @@ -899,6 +900,7 @@
<ClInclude Include="FileSystems\BlobFileSystem.h" />
<ClInclude Include="HLE\KernelThreadDebugInterface.h" />
<ClInclude Include="HLE\KUBridge.h" />
<ClInclude Include="HLE\sceKernelHeap.h" />
<ClInclude Include="HLE\sceUsbAcc.h" />
<ClInclude Include="HLE\sceUsbCam.h" />
<ClInclude Include="HLE\sceUsbMic.h" />
Expand Down
8 changes: 7 additions & 1 deletion Core/Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,9 @@
<ClCompile Include="HW\Camera.cpp">
<Filter>HW</Filter>
</ClCompile>
<ClCompile Include="HLE\sceKernelHeap.cpp">
<Filter>HLE\Kernel</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ELF\ElfReader.h">
Expand Down Expand Up @@ -1373,10 +1376,13 @@
<ClInclude Include="HW\Camera.h">
<Filter>HW</Filter>
</ClInclude>
<ClInclude Include="HLE\sceKernelHeap.h">
<Filter>HLE\Kernel</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="CMakeLists.txt" />
<None Include="..\LICENSE.TXT" />
<None Include="..\android\jni\Android.mk" />
</ItemGroup>
</Project>
</Project>
5 changes: 5 additions & 0 deletions Core/HLE/FunctionWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ template<int func(const char *, int, int, int, int, int, int)> void WrapI_CIIIII
RETURN(retval);
}

template<int func(int, int, int, const char *)> void WrapI_IIIC() {
u32 retval = func(PARAM(0), PARAM(1), PARAM(2), Memory::GetCharPointer(PARAM(3)));
RETURN(retval);
}

// Hm, do so many params get passed in registers?
template<int func(int, int, int, int, int, int, u32)> void WrapI_IIIIIIU() {
u32 retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4), PARAM(5), PARAM(6));
Expand Down
1 change: 1 addition & 0 deletions Core/HLE/HLETables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "sceJpeg.h"
#include "sceKernel.h"
#include "sceKernelEventFlag.h"
#include "sceKernelHeap.h"
#include "sceKernelMemory.h"
#include "sceKernelInterrupt.h"
#include "sceKernelModule.h"
Expand Down
1 change: 1 addition & 0 deletions Core/HLE/sceKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "sceJpeg.h"
#include "sceKernel.h"
#include "sceKernelAlarm.h"
#include "sceKernelHeap.h"
#include "sceKernelInterrupt.h"
#include "sceKernelThread.h"
#include "sceKernelMemory.h"
Expand Down
1 change: 1 addition & 0 deletions Core/HLE/sceKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ enum TMIDPurpose
PPSSPP_KERNEL_TMID_PMB = 0x100002,
PPSSPP_KERNEL_TMID_File = 0x100003,
PPSSPP_KERNEL_TMID_DirList = 0x100004,
PPSSPP_KERNEL_TMID_Heap = 0x100005,
};

typedef int SceUID;
Expand Down
94 changes: 94 additions & 0 deletions Core/HLE/sceKernelHeap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <string>

#include "Common/ChunkFile.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/FunctionWrappers.h"
#include "Core/HLE/sceKernel.h"
#include "Core/HLE/sceKernelHeap.h"
#include "Core/HLE/sceKernelMemory.h"
#include "Core/Util/BlockAllocator.h"

static const u32 HEAP_BLOCK_HEADER_SIZE = 8;
static const bool g_fromBottom = false;

struct Heap : public KernelObject {
int uid = 0;
int partitionId = 0;
u32 size = 0;
int flags = 0;
u32 address = 0;
std::string name;
BlockAllocator alloc;

static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_UID; }
static int GetStaticIDType() { return PPSSPP_KERNEL_TMID_Heap; }
int GetIDType() const override { return PPSSPP_KERNEL_TMID_Heap; }

void DoState(PointerWrap &p) override {
p.Do(uid);
p.Do(partitionId);
p.Do(size);
p.Do(flags);
p.Do(address);
p.Do(name);
p.Do(alloc);
}
};

static int sceKernelCreateHeap(int partitionId, int size, int flags, const char *Name) {
u32 allocSize = (size + 3) & ~3;

// TODO: partitionId should probably decide if we allocate from userMemory or kernel or whatever...
u32 addr = userMemory.Alloc(allocSize, g_fromBottom, "SysMemForKernel-Heap");
if (addr == (u32)-1) {
ERROR_LOG(HLE, "sceKernelCreateHeap(partitionId=%d): Failed to allocate %d bytes memory", partitionId, size);
hrydgard marked this conversation as resolved.
Show resolved Hide resolved
return SCE_KERNEL_ERROR_NO_MEMORY; // Blind guess
}

Heap *heap = new Heap();
SceUID uid = kernelObjects.Create(heap);

heap->partitionId = partitionId;
heap->flags = flags;
heap->name = Name ? Name : ""; // Not sure if this needs validation.
heap->size = allocSize;
heap->address = addr;
heap->alloc.Init(heap->address + 128, heap->size - 128);
heap->uid = uid;
return hleLogSuccessInfoX(SCEKERNEL, uid);
}

static int sceKernelAllocHeapMemory(int heapId, int size) {
u32 error;
Heap *heap = kernelObjects.Get<Heap>(heapId, error);
if (heap) {
// There's 8 bytes at the end of every block, reserved.
u32 memSize = HEAP_BLOCK_HEADER_SIZE + size;
u32 addr = heap->alloc.Alloc(memSize, true);
return hleLogSuccessInfoX(SCEKERNEL, addr);
} else {
return hleLogError(SCEKERNEL, error, "sceKernelAllocHeapMemory(%d): invalid heapId", heapId);
}
}

static int sceKernelDeleteHeap(int heapId) {
u32 error;
Heap *heap = kernelObjects.Get<Heap>(heapId, error);
if (heap) {
userMemory.Free(heap->address);
kernelObjects.Destroy<Heap>(heap->uid);
return hleLogSuccessInfoX(SCEKERNEL, 0);
} else {
return hleLogError(SCEKERNEL, error, "sceKernelDeleteHeap(%d): invalid heapId", heapId);
}
}

const HLEFunction SysMemForKernel[] = {
{ 0X636C953B, &WrapI_II<sceKernelAllocHeapMemory>, "sceKernelAllocHeapMemory", 'x', "ii" },
{ 0XC9805775, &WrapI_I<sceKernelDeleteHeap>, "sceKernelDeleteHeap", 'i', "i" },
{ 0X1C1FBFE7, &WrapI_IIIC<sceKernelCreateHeap>, "sceKernelCreateHeap", 'i', "iixs" },
};

void Register_SysMemForKernel() {
RegisterModule("SysMemForKernel", ARRAY_SIZE(SysMemForKernel), SysMemForKernel);
}
3 changes: 3 additions & 0 deletions Core/HLE/sceKernelHeap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void Register_SysMemForKernel();
10 changes: 0 additions & 10 deletions Core/HLE/sceKernelMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2287,16 +2287,6 @@ const HLEFunction SysMemUserForUser[] = {
{0XD8DE5C1E, &WrapU_V<SysMemUserForUser_D8DE5C1E>, "SysMemUserForUser_D8DE5C1E", 'x', "" },
};

const HLEFunction SysMemForKernel[] = {
{0x636C953B, nullptr, "SysMemForKernel_636c953b", '?', "" },
{0xC9805775, nullptr, "SysMemForKernel_c9805775", '?', "" },
{0x1C1FBFE7, nullptr, "SysMemForKernel_1c1fbfe7", '?', "" },
};

void Register_SysMemForKernel() {
RegisterModule("SysMemForKernel", ARRAY_SIZE(SysMemForKernel), SysMemForKernel);
}

void Register_SysMemUserForUser() {
RegisterModule("SysMemUserForUser", ARRAY_SIZE(SysMemUserForUser), SysMemUserForUser);
}
1 change: 0 additions & 1 deletion Core/HLE/sceKernelMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ int sceKernelGetTlsAddr(SceUID uid);
int sceKernelFreeTlspl(SceUID uid);
int sceKernelReferTlsplStatus(SceUID uid, u32 infoPtr);

void Register_SysMemForKernel();
void Register_SysMemUserForUser();
2 changes: 2 additions & 0 deletions UWP/CoreUWP/CoreUWP.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@
<ClInclude Include="..\..\Core\HLE\sceKernelAlarm.h" />
<ClInclude Include="..\..\Core\HLE\sceKernelEventFlag.h" />
<ClInclude Include="..\..\Core\HLE\sceKernelInterrupt.h" />
<ClInclude Include="..\..\Core\HLE\sceKernelHeap.h" />
<ClInclude Include="..\..\Core\HLE\sceKernelMbx.h" />
<ClInclude Include="..\..\Core\HLE\sceKernelMemory.h" />
<ClInclude Include="..\..\Core\HLE\sceKernelModule.h" />
Expand Down Expand Up @@ -678,6 +679,7 @@
<ClCompile Include="..\..\Core\HLE\sceKernelAlarm.cpp" />
<ClCompile Include="..\..\Core\HLE\sceKernelEventFlag.cpp" />
<ClCompile Include="..\..\Core\HLE\sceKernelInterrupt.cpp" />
<ClCompile Include="..\..\Core\HLE\sceKernelHeap.cpp" />
<ClCompile Include="..\..\Core\HLE\sceKernelMbx.cpp" />
<ClCompile Include="..\..\Core\HLE\sceKernelMemory.cpp" />
<ClCompile Include="..\..\Core\HLE\sceKernelModule.cpp" />
Expand Down
8 changes: 7 additions & 1 deletion UWP/CoreUWP/CoreUWP.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@
<ClCompile Include="..\..\Core\HLE\sceKernelInterrupt.cpp">
<Filter>HLE</Filter>
</ClCompile>
<ClCompile Include="..\..\Core\HLE\sceKernelHeap.cpp">
<Filter>HLE</Filter>
</ClCompile>
<ClCompile Include="..\..\Core\HLE\sceKernelMbx.cpp">
<Filter>HLE</Filter>
</ClCompile>
Expand Down Expand Up @@ -968,6 +971,9 @@
<ClInclude Include="..\..\Core\HLE\sceKernelInterrupt.h">
<Filter>HLE</Filter>
</ClInclude>
<ClInclude Include="..\..\Core\HLE\sceKernelHeap.h">
<Filter>HLE</Filter>
</ClInclude>
<ClInclude Include="..\..\Core\HLE\sceKernelMbx.h">
<Filter>HLE</Filter>
</ClInclude>
Expand Down Expand Up @@ -1298,4 +1304,4 @@
<Filter>HW</Filter>
</ClInclude>
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ EXEC_AND_LIB_FILES := \
$(SRC)/Core/HLE/sceKernel.cpp \
$(SRC)/Core/HLE/sceKernelAlarm.cpp \
$(SRC)/Core/HLE/sceKernelEventFlag.cpp \
$(SRC)/Core/HLE/sceKernelHeap.cpp \
$(SRC)/Core/HLE/sceKernelInterrupt.cpp \
$(SRC)/Core/HLE/sceKernelMemory.cpp \
$(SRC)/Core/HLE/sceKernelModule.cpp \
Expand Down
1 change: 1 addition & 0 deletions libretro/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ SOURCES_CXX += $(NATIVEDIR)/math/dataconv.cpp \
$(COREDIR)/HLE/sceKernel.cpp \
$(COREDIR)/HLE/sceKernelAlarm.cpp \
$(COREDIR)/HLE/sceKernelEventFlag.cpp \
$(COREDIR)/HLE/sceKernelHeap.cpp \
$(COREDIR)/HLE/sceKernelInterrupt.cpp \
$(COREDIR)/HLE/sceKernelMbx.cpp \
$(COREDIR)/HLE/sceKernelMemory.cpp \
Expand Down