From 4b9b23dfb4001ad8aea495971cbf1bf863a18685 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 25 Dec 2012 22:55:50 -0800 Subject: [PATCH 1/8] Correct arguments in sceIoGetAsyncStat(). --- Core/HLE/sceIo.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 9f03b26d17..74bc480c8c 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -746,7 +746,7 @@ u32 sceIoReadAsync(int id, u32 data_addr, int size) return 0; } -u32 sceIoGetAsyncStat(int id, u32 address, u32 uknwn) +u32 sceIoGetAsyncStat(int id, u32 poll, u32 address) { u32 error; FileNode *f = kernelObjects.Get < FileNode > (id, error); @@ -754,7 +754,7 @@ u32 sceIoGetAsyncStat(int id, u32 address, u32 uknwn) { Memory::Write_U64(f->asyncResult, address); DEBUG_LOG(HLE, "%i = sceIoGetAsyncStat(%i, %i, %08x) (HACK)", - (u32) f->asyncResult, id, address, uknwn); + (u32) f->asyncResult, id, poll, address); return 0; //completed } else From b2ef54e1941a1e00b35f60181354b1216fd25325 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 25 Dec 2012 23:02:53 -0800 Subject: [PATCH 2/8] Make waiting on async IO reschedule. --- Core/HLE/sceIo.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 74bc480c8c..fb635bc0e0 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -755,6 +755,8 @@ u32 sceIoGetAsyncStat(int id, u32 poll, u32 address) Memory::Write_U64(f->asyncResult, address); DEBUG_LOG(HLE, "%i = sceIoGetAsyncStat(%i, %i, %08x) (HACK)", (u32) f->asyncResult, id, poll, address); + if (!poll) + hleReSchedule("io waited"); return 0; //completed } else @@ -764,7 +766,7 @@ u32 sceIoGetAsyncStat(int id, u32 poll, u32 address) } } -void sceIoWaitAsync(int id, u32 address, u32 uknwn) { +int sceIoWaitAsync(int id, u32 address) { u32 error; FileNode *f = kernelObjects.Get < FileNode > (id, error); if (f) { @@ -775,15 +777,16 @@ void sceIoWaitAsync(int id, u32 address, u32 uknwn) { } Memory::Write_U64(res, address); DEBUG_LOG(HLE, "%i = sceIoWaitAsync(%i, %08x) (HACK)", (u32) res, id, - uknwn); - RETURN(0); //completed + address); + hleReSchedule("io waited"); + return 0; //completed } else { ERROR_LOG(HLE, "ERROR - sceIoWaitAsync waiting for invalid id %i", id); - RETURN(-1); + return -1; } } -void sceIoWaitAsyncCB(int id, u32 address) { +int sceIoWaitAsyncCB(int id, u32 address) { // Should process callbacks here u32 error; FileNode *f = kernelObjects.Get < FileNode > (id, error); @@ -796,10 +799,13 @@ void sceIoWaitAsyncCB(int id, u32 address) { Memory::Write_U64(res, address); DEBUG_LOG(HLE, "%i = sceIoWaitAsyncCB(%i, %08x) (HACK)", (u32) res, id, address); - RETURN(0); //completed + hleCheckCurrentCallbacks(); + hleReSchedule(true, "io waited"); + return 0; //completed } else { ERROR_LOG(HLE, "ERROR - sceIoWaitAsyncCB waiting for invalid id %i", id); + return -1; } } @@ -946,8 +952,8 @@ const HLEFunction IoFileMgrForUser[] = { { 0x6d08a871, 0, "sceIoUnassign" }, { 0x42EC03AC, &WrapU_IVI, "sceIoWrite" }, //(int fd, void *data, int size); { 0x0facab19, 0, "sceIoWriteAsync" }, - { 0x35dbd746, &WrapV_IU, "sceIoWaitAsyncCB" }, - { 0xe23eec33, &WrapV_IUU, "sceIoWaitAsync" }, + { 0x35dbd746, &WrapI_IU, "sceIoWaitAsyncCB" }, + { 0xe23eec33, &WrapI_IU, "sceIoWaitAsync" }, }; void Register_IoFileMgrForUser() { From f75f2748b1d9c346c05ff40de4c270ac35fcd731 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 25 Dec 2012 23:04:06 -0800 Subject: [PATCH 3/8] Tests show io open also rescheds, async or not. --- Core/HLE/sceIo.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index fb635bc0e0..62b64e79e3 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -382,6 +382,8 @@ u32 sceIoOpen(const char* filename, int mode) { if (mode & O_CREAT) access |= FILEACCESS_CREATE; + hleReSchedule("file opened"); + u32 h = pspFileSystem.OpenFile(filename, (FileAccess) access); if (h == 0) { From a4154dbc171a161a281dba407b7721587791bf36 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 25 Dec 2012 23:25:17 -0800 Subject: [PATCH 4/8] Clean up a few IO wrappers with deferred HLE. --- Core/HLE/FunctionWrappers.h | 5 ++++ Core/HLE/sceIo.cpp | 47 ++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Core/HLE/FunctionWrappers.h b/Core/HLE/FunctionWrappers.h index 4185bdcdae..2795e24c4f 100644 --- a/Core/HLE/FunctionWrappers.h +++ b/Core/HLE/FunctionWrappers.h @@ -444,6 +444,11 @@ template void WrapU_CI() { RETURN(retval); } +template void WrapU_CII() { + int retval = func(Memory::GetCharPointer(PARAM(0)), PARAM(1), PARAM(2)); + RETURN(retval); +} + template void WrapU_CIUIU() { int retval = func(Memory::GetCharPointer(PARAM(0)), PARAM(1), PARAM(2), PARAM(3), PARAM(4)); diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 62b64e79e3..3b7c7612a4 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -370,16 +370,16 @@ u32 sceIoLseek32(int id, int offset, int whence) { } } -u32 sceIoOpen(const char* filename, int mode) { +u32 sceIoOpen(const char* filename, int flags, int mode) { //memory stick filename int access = FILEACCESS_NONE; - if (mode & O_RDONLY) + if (flags & O_RDONLY) access |= FILEACCESS_READ; - if (mode & O_WRONLY) + if (flags & O_WRONLY) access |= FILEACCESS_WRITE; - if (mode & O_APPEND) + if (flags & O_APPEND) access |= FILEACCESS_APPEND; - if (mode & O_CREAT) + if (flags & O_CREAT) access |= FILEACCESS_CREATE; hleReSchedule("file opened"); @@ -388,8 +388,8 @@ u32 sceIoOpen(const char* filename, int mode) { if (h == 0) { ERROR_LOG(HLE, - "ERROR_ERRNO_FILE_NOT_FOUND=sceIoOpen(%s, %08x) - file not found", - filename, mode); + "ERROR_ERRNO_FILE_NOT_FOUND=sceIoOpen(%s, %08x, %08x) - file not found", + filename, flags, mode); return ERROR_ERRNO_FILE_NOT_FOUND; } @@ -398,7 +398,7 @@ u32 sceIoOpen(const char* filename, int mode) { f->handle = h; f->fullpath = filename; f->asyncResult = id; - DEBUG_LOG(HLE, "%i=sceIoOpen(%s, %08x)", id, filename, mode); + DEBUG_LOG(HLE, "%i=sceIoOpen(%s, %08x, %08x)", id, filename, flags, mode); return id; } @@ -675,10 +675,10 @@ u32 sceIoChdir(const char *dirname) { return 1; } -void sceIoChangeAsyncPriority() +int sceIoChangeAsyncPriority(int id, int priority) { - ERROR_LOG(HLE, "UNIMPL sceIoChangeAsyncPriority(%d)", PARAM(0)); - RETURN(0); + ERROR_LOG(HLE, "UNIMPL sceIoChangeAsyncPriority(%d, %d)", id, priority); + return 0; } u32 __IoClose(SceUID id, int param) @@ -688,13 +688,12 @@ u32 __IoClose(SceUID id, int param) return kernelObjects.Destroy < FileNode > (id); } -//TODO Not really sure if this should be wrapped nor how -void sceIoCloseAsync() +int sceIoCloseAsync(SceUID id) { - DEBUG_LOG(HLE, "sceIoCloseAsync(%d)", PARAM(0)); + DEBUG_LOG(HLE, "sceIoCloseAsync(%d)", id); //sceIoClose(); defAction = &__IoClose; - RETURN(0); + return 0; } u32 sceIoLseekAsync(int id, s64 offset, int whence) @@ -731,13 +730,13 @@ u32 sceIoLseek32Async(int id, int offset, int whence) return 0; } -void sceIoOpenAsync(const char *filename, int mode) +u32 sceIoOpenAsync(const char *filename, int flags, int mode) { DEBUG_LOG(HLE, "sceIoOpenAsync() sorta implemented"); - RETURN(sceIoOpen(filename, mode)); -// __IoCompleteAsyncIO(currentMIPS->r[2]); // The return value - // We have to return a UID here, which may have been destroyed when we reach Wait if it failed. - // Now that we're just faking it, we just don't RETURN(0) here. + u32 fd = sceIoOpen(filename, flags, mode); + //__IoCompleteAsyncIO(fd); // The return value + // We have to return an fd here, which may have been destroyed when we reach Wait if it failed. + return fd; } u32 sceIoReadAsync(int id, u32 data_addr, int size) @@ -931,17 +930,17 @@ const HLEFunction IoFileMgrForUser[] = { { 0x08bd7374, 0, "sceIoGetDevType" }, { 0xB2A628C1, &WrapU_CCCU, "sceIoAssign" }, { 0xe8bc6571, 0, "sceIoCancel" }, - { 0xb293727f, sceIoChangeAsyncPriority, "sceIoChangeAsyncPriority" }, + { 0xb293727f, &WrapI_II, "sceIoChangeAsyncPriority" }, { 0x810C4BC3, &WrapU_I, "sceIoClose" }, //(int fd); - { 0xff5940b6, sceIoCloseAsync, "sceIoCloseAsync" }, + { 0xff5940b6, &WrapI_I, "sceIoCloseAsync" }, { 0x54F5FB11, &WrapU_CIUIUI, "sceIoDevctl" }, //(const char *name int cmd, void *arg, size_t arglen, void *buf, size_t *buflen); { 0xcb05f8d6, &WrapU_IUU, "sceIoGetAsyncStat" }, { 0x27EB27B8, &WrapI64_II64I, "sceIoLseek" }, //(int fd, int offset, int whence); { 0x68963324, &WrapU_III, "sceIoLseek32" }, { 0x1b385d8f, &WrapU_III, "sceIoLseek32Async" }, { 0x71b19e77, &WrapU_II64I, "sceIoLseekAsync" }, - { 0x109F50BC, &WrapU_CI, "sceIoOpen" }, //(const char* file, int mode); - { 0x89AA9906, &WrapV_CI, "sceIoOpenAsync" }, + { 0x109F50BC, &WrapU_CII, "sceIoOpen" }, //(const char* file, int mode); + { 0x89AA9906, &WrapU_CII, "sceIoOpenAsync" }, { 0x06A70004, &WrapU_CI, "sceIoMkdir" }, //(const char *dir, int mode); { 0x3251ea56, &WrapU_IU, "sceIoPollAsync" }, { 0x6A638D83, &WrapU_IUI, "sceIoRead" }, //(int fd, void *data, int size); From 8d946a7a3a478ba0827f11f6c57b6a93e6f73eea Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 25 Dec 2012 23:39:34 -0800 Subject: [PATCH 5/8] Ensure we don't close the wrong fd in async io. --- Core/HLE/sceIo.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 3b7c7612a4..25f7869e72 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -681,18 +681,20 @@ int sceIoChangeAsyncPriority(int id, int priority) return 0; } -u32 __IoClose(SceUID id, int param) +u32 __IoClose(SceUID actedFd, int closedFd) { - DEBUG_LOG(HLE, "Deferred IoClose(%d)", id); - __IoCompleteAsyncIO(id); - return kernelObjects.Destroy < FileNode > (id); + DEBUG_LOG(HLE, "Deferred IoClose(%d, %d)", actedFd, closedFd); + __IoCompleteAsyncIO(closedFd); + return kernelObjects.Destroy < FileNode > (closedFd); } int sceIoCloseAsync(SceUID id) { DEBUG_LOG(HLE, "sceIoCloseAsync(%d)", id); //sceIoClose(); + // TODO: Not sure this is a good solution. Seems like you can defer one per fd. defAction = &__IoClose; + defParam = id; return 0; } From be779d7f77ed1be9f2934c9c54d6a04d8c82236c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 26 Dec 2012 00:12:35 -0800 Subject: [PATCH 6/8] Actually run callbacks for async IO. Probably needs to actually be delayed though. --- Core/HLE/sceIo.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 25f7869e72..dfea927df2 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -224,7 +224,7 @@ void __IoCompleteAsyncIO(SceUID id) { FileNode *f = kernelObjects.Get < FileNode > (id, error); if (f) { if (f->callbackID) { - // __KernelNotifyCallbackType(THREAD_CALLBACK_IO, __KernelGetCurThread(), f->callbackID, f->callbackArg); + __KernelNotifyCallback(THREAD_CALLBACK_IO, f->callbackID, f->callbackArg); } } } @@ -714,9 +714,12 @@ u32 sceIoSetAsyncCallback(int id, u32 clbckId, u32 clbckArg) FileNode *f = kernelObjects.Get < FileNode > (id, error); if (f) { + // TODO: Check replacing / updating? f->callbackID = clbckId; f->callbackArg = clbckArg; - return 0; + // TODO: Not sure when this is unregistered? + error = __KernelRegisterCallback(THREAD_CALLBACK_IO, clbckId); + return error; } else { @@ -736,7 +739,8 @@ u32 sceIoOpenAsync(const char *filename, int flags, int mode) { DEBUG_LOG(HLE, "sceIoOpenAsync() sorta implemented"); u32 fd = sceIoOpen(filename, flags, mode); - //__IoCompleteAsyncIO(fd); // The return value + // TODO: This can't actually have a callback yet, but if it's set before waiting, it should be called. + __IoCompleteAsyncIO(fd); // We have to return an fd here, which may have been destroyed when we reach Wait if it failed. return fd; } From 0587104c16024b9c8e6bca3b109a9930caa791ad Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 26 Dec 2012 02:17:47 -0800 Subject: [PATCH 7/8] No need to register now, actually. Was using NotifyCallbackType before but that wasn't necessary anyway, so this is cleaner/better. --- Core/HLE/sceIo.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index dfea927df2..179a9fe445 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -717,9 +717,7 @@ u32 sceIoSetAsyncCallback(int id, u32 clbckId, u32 clbckArg) // TODO: Check replacing / updating? f->callbackID = clbckId; f->callbackArg = clbckArg; - // TODO: Not sure when this is unregistered? - error = __KernelRegisterCallback(THREAD_CALLBACK_IO, clbckId); - return error; + return 0; } else { From 724640d3080a98ea2c3e74fcdbbc157e629a7a1d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 26 Dec 2012 02:23:04 -0800 Subject: [PATCH 8/8] Revert rescheduling in sceIoOpen(). Broke Orbit and Breakquest, maybe my tests are wrong. Scheduling is hard. This reverts commit f75f2748b1d9c346c05ff40de4c270ac35fcd731. --- Core/HLE/sceIo.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 179a9fe445..2523539a0a 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -382,8 +382,6 @@ u32 sceIoOpen(const char* filename, int flags, int mode) { if (flags & O_CREAT) access |= FILEACCESS_CREATE; - hleReSchedule("file opened"); - u32 h = pspFileSystem.OpenFile(filename, (FileAccess) access); if (h == 0) {