From 3a372aa615b7dc27ae2a52396af6bfa875f39348 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 3 Sep 2022 07:48:52 -0700 Subject: [PATCH] HLE: Remove misc usage of WriteStruct(). Prefer PSPPointer and notifying. --- Core/HLE/sceChnnlsv.cpp | 82 ++++++++++++++++------------------------ Core/HLE/sceOpenPSID.cpp | 14 ++++--- Core/HLE/sceUsbCam.cpp | 33 ++++++++++------ Core/HLE/sceUsbGps.cpp | 16 +++++--- Core/MemMap.cpp | 9 +++++ Core/MemMap.h | 15 +++++++- 6 files changed, 94 insertions(+), 75 deletions(-) diff --git a/Core/HLE/sceChnnlsv.cpp b/Core/HLE/sceChnnlsv.cpp index 82e063b34470..fc73837687ba 100644 --- a/Core/HLE/sceChnnlsv.cpp +++ b/Core/HLE/sceChnnlsv.cpp @@ -214,13 +214,12 @@ static int sub_17A8(u8* data) return -261; } -static int sceSdGetLastIndex(u32 addressCtx, u32 addressHash, u32 addressKey) -{ - pspChnnlsvContext1 ctx; - Memory::ReadStruct(addressCtx, &ctx); - int res = sceSdGetLastIndex_(ctx, Memory::GetPointerWrite(addressHash), Memory::GetPointerWrite(addressKey)); - Memory::WriteStruct(addressCtx, &ctx); - return res; +static int sceSdGetLastIndex(u32 addressCtx, u32 addressHash, u32 addressKey) { + auto ctx = PSPPointer::Create(addressCtx); + u8 *hash = Memory::GetPointerWrite(addressHash); + if (!ctx.IsValid() || !hash) + return hleLogError(SCEMISC, 0, "Invalid pointer"); + return hleLogSuccessI(SCEMISC, sceSdGetLastIndex_(*ctx, hash, Memory::GetPointerWrite(addressKey))); } int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, u8* in_key) @@ -325,13 +324,11 @@ int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, u8* in_key) return 0; } -static int sceSdSetIndex(u32 addressCtx, int value) -{ - pspChnnlsvContext1 ctx; - Memory::ReadStruct(addressCtx,&ctx); - int res = sceSdSetIndex_(ctx, value); - Memory::WriteStruct(addressCtx,&ctx); - return res; +static int sceSdSetIndex(u32 addressCtx, int value) { + auto ctx = PSPPointer::Create(addressCtx); + if (!ctx.IsValid()) + return hleLogError(SCEMISC, 0, "Invalid pointer"); + return hleLogSuccessI(SCEMISC, sceSdSetIndex_(*ctx, value)); } int sceSdSetIndex_(pspChnnlsvContext1& ctx, int value) @@ -344,14 +341,11 @@ int sceSdSetIndex_(pspChnnlsvContext1& ctx, int value) } -static int sceSdRemoveValue(u32 addressCtx, u32 addressData, int length) -{ - pspChnnlsvContext1 ctx; - Memory::ReadStruct(addressCtx, &ctx); - int res = sceSdRemoveValue_(ctx, Memory::GetPointerWrite(addressData), length); - Memory::WriteStruct(addressCtx, &ctx); - - return res; +static int sceSdRemoveValue(u32 addressCtx, u32 addressData, int length) { + auto ctx = PSPPointer::Create(addressCtx); + if (!ctx.IsValid() || !Memory::IsValidAddress(addressData)) + return hleLogError(SCEMISC, 0, "Invalid pointer"); + return hleLogSuccessI(SCEMISC, sceSdRemoveValue_(*ctx, Memory::GetPointerWrite(addressData), length)); } int sceSdRemoveValue_(pspChnnlsvContext1& ctx, u8* data, int length) @@ -396,18 +390,14 @@ int sceSdRemoveValue_(pspChnnlsvContext1& ctx, u8* data, int length) return 0; } -static int sceSdCreateList(u32 ctx2Addr, int mode, int unkwn, u32 dataAddr, u32 cryptkeyAddr) -{ - pspChnnlsvContext2 ctx2; - Memory::ReadStruct(ctx2Addr, &ctx2); +static int sceSdCreateList(u32 ctx2Addr, int mode, int unkwn, u32 dataAddr, u32 cryptkeyAddr) { + auto ctx2 = PSPPointer::Create(ctx2Addr); u8* data = Memory::GetPointerWrite(dataAddr); u8* cryptkey = Memory::GetPointerWrite(cryptkeyAddr); + if (!ctx2.IsValid() || !data) + return hleLogError(SCEMISC, 0, "Invalid pointer"); - int res = sceSdCreateList_(ctx2, mode, unkwn, data, cryptkey); - - Memory::WriteStruct(ctx2Addr, &ctx2); - - return res; + return hleLogSuccessI(SCEMISC, sceSdCreateList_(*ctx2, mode, unkwn, data, cryptkey)); } int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, u8* cryptkey) @@ -464,17 +454,13 @@ int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, u8* return 0; } -static int sceSdSetMember(u32 ctxAddr, u32 dataAddr, int alignedLen) -{ - pspChnnlsvContext2 ctx; - Memory::ReadStruct(ctxAddr, &ctx); - u8* data = Memory::GetPointerWrite(dataAddr); - - int res = sceSdSetMember_(ctx, data, alignedLen); - - Memory::WriteStruct(ctxAddr, &ctx); +static int sceSdSetMember(u32 ctxAddr, u32 dataAddr, int alignedLen) { + auto ctx = PSPPointer::Create(ctxAddr); + u8 *data = Memory::GetPointerWrite(dataAddr); + if (!ctx.IsValid() || !data) + return hleLogError(SCEMISC, 0, "Invalid pointer"); - return res; + return hleLogSuccessI(SCEMISC, sceSdSetMember_(*ctx, data, alignedLen)); } int sceSdSetMember_(pspChnnlsvContext2& ctx, u8* data, int alignedLen) @@ -511,15 +497,11 @@ int sceSdSetMember_(pspChnnlsvContext2& ctx, u8* data, int alignedLen) return res; } -static int sceChnnlsv_21BE78B4(u32 ctxAddr) -{ - pspChnnlsvContext2 ctx; - Memory::ReadStruct(ctxAddr, &ctx); - - int res = sceChnnlsv_21BE78B4_(ctx); - - Memory::WriteStruct(ctxAddr, &ctx); - return res; +static int sceChnnlsv_21BE78B4(u32 ctxAddr) { + auto ctx = PSPPointer::Create(ctxAddr); + if (!ctx.IsValid()) + return hleLogError(SCEMISC, 0, "Invalid pointer"); + return hleLogSuccessI(SCEMISC, sceChnnlsv_21BE78B4_(*ctx)); } int sceChnnlsv_21BE78B4_(pspChnnlsvContext2& ctx) diff --git a/Core/HLE/sceOpenPSID.cpp b/Core/HLE/sceOpenPSID.cpp index 43665f530fde..9d47b48374cc 100644 --- a/Core/HLE/sceOpenPSID.cpp +++ b/Core/HLE/sceOpenPSID.cpp @@ -38,9 +38,10 @@ static int sceOpenPSIDGetOpenPSID(u32 OpenPSIDPtr) { WARN_LOG(HLE, "UNTESTED %s(%08x)", __FUNCTION__, OpenPSIDPtr); - if (Memory::IsValidAddress(OpenPSIDPtr)) - { - Memory::WriteStruct(OpenPSIDPtr, &dummyOpenPSID); + auto ptr = PSPPointer::Create(OpenPSIDPtr); + if (ptr.IsValid()) { + *ptr = dummyOpenPSID; + ptr.NotifyWrite("OpenPSIDGetOpenPSID"); } return 0; } @@ -49,9 +50,10 @@ static int sceOpenPSIDGetPSID(u32 OpenPSIDPtr,u32 unknown) { WARN_LOG(HLE, "UNTESTED %s(%08x, %08x)", __FUNCTION__, OpenPSIDPtr, unknown); - if (Memory::IsValidAddress(OpenPSIDPtr)) - { - Memory::WriteStruct(OpenPSIDPtr, &dummyOpenPSID); + auto ptr = PSPPointer::Create(OpenPSIDPtr); + if (ptr.IsValid()) { + *ptr = dummyOpenPSID; + ptr.NotifyWrite("OpenPSIDGetPSID"); } return 0; } diff --git a/Core/HLE/sceUsbCam.cpp b/Core/HLE/sceUsbCam.cpp index 18417bb9c2a2..c8d43a50270d 100644 --- a/Core/HLE/sceUsbCam.cpp +++ b/Core/HLE/sceUsbCam.cpp @@ -113,11 +113,12 @@ static int getCameraResolution(Camera::ConfigType type, int *width, int *height) static int sceUsbCamSetupMic(u32 paramAddr, u32 workareaAddr, int wasize) { - INFO_LOG(HLE, "sceUsbCamSetupMic"); - if (Memory::IsValidRange(paramAddr, sizeof(PspUsbCamSetupMicParam))) { - Memory::ReadStruct(paramAddr, &config->micParam); + auto param = PSPPointer::Create(paramAddr); + if (param.IsValid()) { + config->micParam = *param; + param.NotifyRead("UsbCamSetupMic"); } - return 0; + return hleLogSuccessInfoI(SCEMISC, 0); } static int sceUsbCamStartMic() { @@ -155,16 +156,20 @@ static int sceUsbCamGetMicDataLength() { } static int sceUsbCamSetupVideo(u32 paramAddr, u32 workareaAddr, int wasize) { - if (Memory::IsValidRange(paramAddr, sizeof(PspUsbCamSetupVideoParam))) { - Memory::ReadStruct(paramAddr, &config->videoParam); + auto param = PSPPointer::Create(paramAddr); + if (param.IsValid()) { + config->videoParam = *param; + param.NotifyRead("UsbCamSetupVideo"); } config->type = Camera::ConfigType::CfVideo; return 0; } static int sceUsbCamSetupVideoEx(u32 paramAddr, u32 workareaAddr, int wasize) { - if (Memory::IsValidRange(paramAddr, sizeof(PspUsbCamSetupVideoExParam))) { - Memory::ReadStruct(paramAddr, &config->videoExParam); + auto param = PSPPointer::Create(paramAddr); + if (param.IsValid()) { + config->videoExParam = *param; + param.NotifyRead("UsbCamSetupVideoEx"); } config->type = Camera::ConfigType::CfVideoEx; return 0; @@ -222,8 +227,10 @@ static int sceUsbCamPollReadVideoFrameEnd() { static int sceUsbCamSetupStill(u32 paramAddr) { INFO_LOG(HLE, "UNIMPL sceUsbCamSetupStill"); - if (Memory::IsValidRange(paramAddr, sizeof(PspUsbCamSetupStillParam))) { - Memory::ReadStruct(paramAddr, &config->stillParam); + auto param = PSPPointer::Create(paramAddr); + if (param.IsValid()) { + config->stillParam = *param; + param.NotifyRead("UsbCamSetupStill"); } config->type = Camera::ConfigType::CfStill; return 0; @@ -231,8 +238,10 @@ static int sceUsbCamSetupStill(u32 paramAddr) { static int sceUsbCamSetupStillEx(u32 paramAddr) { INFO_LOG(HLE, "UNIMPL sceUsbCamSetupStillEx"); - if (Memory::IsValidRange(paramAddr, sizeof(PspUsbCamSetupStillExParam))) { - Memory::ReadStruct(paramAddr, &config->stillExParam); + auto param = PSPPointer::Create(paramAddr); + if (param.IsValid()) { + config->stillExParam = *param; + param.NotifyRead("UsbCamSetupStillEx"); } config->type = Camera::ConfigType::CfStillEx; return 0; diff --git a/Core/HLE/sceUsbGps.cpp b/Core/HLE/sceUsbGps.cpp index 2c9f3200440b..020339fe1081 100644 --- a/Core/HLE/sceUsbGps.cpp +++ b/Core/HLE/sceUsbGps.cpp @@ -59,8 +59,8 @@ static int sceUsbGpsGetInitDataLocation(u32 addr) { } static int sceUsbGpsGetState(u32 stateAddr) { - if (Memory::IsValidAddress(stateAddr)) { - Memory::Write_U32(gpsStatus, stateAddr); + if (Memory::IsValidRange(stateAddr, 4)) { + Memory::WriteUnchecked_U32(gpsStatus, stateAddr); } return 0; } @@ -81,11 +81,15 @@ static int sceUsbGpsClose() { } static int sceUsbGpsGetData(u32 gpsDataAddr, u32 satDataAddr) { - if (Memory::IsValidRange(gpsDataAddr, sizeof(GpsData))) { - Memory::WriteStruct(gpsDataAddr, GPS::getGpsData()); + auto gpsData = PSPPointer::Create(gpsDataAddr); + if (gpsData.IsValid()) { + *gpsData = *GPS::getGpsData(); + gpsData.NotifyWrite("UsbGpsGetData"); } - if (Memory::IsValidRange(satDataAddr, sizeof(SatData))) { - Memory::WriteStruct(satDataAddr, GPS::getSatData()); + auto satData = PSPPointer::Create(satDataAddr); + if (satData.IsValid()) { + *satData = *GPS::getSatData(); + gpsData.NotifyWrite("UsbGpsGetData"); } return 0; } diff --git a/Core/MemMap.cpp b/Core/MemMap.cpp index 1a0fd654f67a..52ef9ff5f939 100644 --- a/Core/MemMap.cpp +++ b/Core/MemMap.cpp @@ -490,3 +490,12 @@ void Memset(const u32 _Address, const u8 _iValue, const u32 _iLength, const char } } // namespace + +void PSPPointerNotifyRW(int rw, uint32_t ptr, uint32_t bytes, const char * tag, size_t tagLen) { + if (MemBlockInfoDetailed(bytes)) { + if (rw & 1) + NotifyMemInfo(MemBlockFlags::WRITE, ptr, bytes, tag, tagLen); + if (rw & 2) + NotifyMemInfo(MemBlockFlags::READ, ptr, bytes, tag, tagLen); + } +} diff --git a/Core/MemMap.h b/Core/MemMap.h index 97b7fd76e7f8..306190f24992 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -328,6 +328,9 @@ inline bool IsValidRange(const u32 address, const u32 size) { } // namespace Memory +// Avoiding a global include for NotifyMemInfo. +void PSPPointerNotifyRW(int rw, uint32_t ptr, uint32_t bytes, const char *tag, size_t tagLen); + template struct PSPPointer { @@ -440,7 +443,17 @@ struct PSPPointer bool IsValid() const { - return Memory::IsValidAddress(ptr); + return Memory::IsValidRange(ptr, (u32)sizeof(T)); + } + + template + void NotifyWrite(const char(&tag)[tagLen]) const { + PSPPointerNotifyRW(1, (uint32_t)ptr, (uint32_t)sizeof(T), tag, tagLen - 1); + } + + template + void NotifyRead(const char(&tag)[tagLen]) const { + PSPPointerNotifyRW(2, (uint32_t)ptr, (uint32_t)sizeof(T), tag, tagLen - 1); } static PSPPointer Create(u32 ptr) {