Skip to content

Commit

Permalink
HLE: Remove misc usage of WriteStruct().
Browse files Browse the repository at this point in the history
Prefer PSPPointer and notifying.
  • Loading branch information
unknownbrackets committed Sep 3, 2022
1 parent 867eb99 commit 3a372aa
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 75 deletions.
82 changes: 32 additions & 50 deletions Core/HLE/sceChnnlsv.cpp
Expand Up @@ -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<pspChnnlsvContext1>::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)
Expand Down Expand Up @@ -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<pspChnnlsvContext1>::Create(addressCtx);
if (!ctx.IsValid())
return hleLogError(SCEMISC, 0, "Invalid pointer");
return hleLogSuccessI(SCEMISC, sceSdSetIndex_(*ctx, value));
}

int sceSdSetIndex_(pspChnnlsvContext1& ctx, int value)
Expand All @@ -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<pspChnnlsvContext1>::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)
Expand Down Expand Up @@ -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<pspChnnlsvContext2>::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)
Expand Down Expand Up @@ -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<pspChnnlsvContext2>::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)
Expand Down Expand Up @@ -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<pspChnnlsvContext2>::Create(ctxAddr);
if (!ctx.IsValid())
return hleLogError(SCEMISC, 0, "Invalid pointer");
return hleLogSuccessI(SCEMISC, sceChnnlsv_21BE78B4_(*ctx));
}

int sceChnnlsv_21BE78B4_(pspChnnlsvContext2& ctx)
Expand Down
14 changes: 8 additions & 6 deletions Core/HLE/sceOpenPSID.cpp
Expand Up @@ -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<SceOpenPSID>::Create(OpenPSIDPtr);
if (ptr.IsValid()) {
*ptr = dummyOpenPSID;
ptr.NotifyWrite("OpenPSIDGetOpenPSID");
}
return 0;
}
Expand All @@ -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<SceOpenPSID>::Create(OpenPSIDPtr);
if (ptr.IsValid()) {
*ptr = dummyOpenPSID;
ptr.NotifyWrite("OpenPSIDGetPSID");
}
return 0;
}
Expand Down
33 changes: 21 additions & 12 deletions Core/HLE/sceUsbCam.cpp
Expand Up @@ -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<PspUsbCamSetupMicParam>::Create(paramAddr);
if (param.IsValid()) {
config->micParam = *param;
param.NotifyRead("UsbCamSetupMic");
}
return 0;
return hleLogSuccessInfoI(SCEMISC, 0);
}

static int sceUsbCamStartMic() {
Expand Down Expand Up @@ -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<PspUsbCamSetupVideoParam>::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<PspUsbCamSetupVideoExParam>::Create(paramAddr);
if (param.IsValid()) {
config->videoExParam = *param;
param.NotifyRead("UsbCamSetupVideoEx");
}
config->type = Camera::ConfigType::CfVideoEx;
return 0;
Expand Down Expand Up @@ -222,17 +227,21 @@ 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<PspUsbCamSetupStillParam>::Create(paramAddr);
if (param.IsValid()) {
config->stillParam = *param;
param.NotifyRead("UsbCamSetupStill");
}
config->type = Camera::ConfigType::CfStill;
return 0;
}

static int sceUsbCamSetupStillEx(u32 paramAddr) {
INFO_LOG(HLE, "UNIMPL sceUsbCamSetupStillEx");
if (Memory::IsValidRange(paramAddr, sizeof(PspUsbCamSetupStillExParam))) {
Memory::ReadStruct(paramAddr, &config->stillExParam);
auto param = PSPPointer<PspUsbCamSetupStillExParam>::Create(paramAddr);
if (param.IsValid()) {
config->stillExParam = *param;
param.NotifyRead("UsbCamSetupStillEx");
}
config->type = Camera::ConfigType::CfStillEx;
return 0;
Expand Down
16 changes: 10 additions & 6 deletions Core/HLE/sceUsbGps.cpp
Expand Up @@ -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;
}
Expand All @@ -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<GpsData>::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<SatData>::Create(satDataAddr);
if (satData.IsValid()) {
*satData = *GPS::getSatData();
gpsData.NotifyWrite("UsbGpsGetData");
}
return 0;
}
Expand Down
9 changes: 9 additions & 0 deletions Core/MemMap.cpp
Expand Up @@ -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);
}
}
15 changes: 14 additions & 1 deletion Core/MemMap.h
Expand Up @@ -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 <typename T>
struct PSPPointer
{
Expand Down Expand Up @@ -440,7 +443,17 @@ struct PSPPointer

bool IsValid() const
{
return Memory::IsValidAddress(ptr);
return Memory::IsValidRange(ptr, (u32)sizeof(T));
}

template <size_t tagLen>
void NotifyWrite(const char(&tag)[tagLen]) const {
PSPPointerNotifyRW(1, (uint32_t)ptr, (uint32_t)sizeof(T), tag, tagLen - 1);
}

template <size_t tagLen>
void NotifyRead(const char(&tag)[tagLen]) const {
PSPPointerNotifyRW(2, (uint32_t)ptr, (uint32_t)sizeof(T), tag, tagLen - 1);
}

static PSPPointer<T> Create(u32 ptr) {
Expand Down

0 comments on commit 3a372aa

Please sign in to comment.