From a3d400b5cca5d778e4ec6188f46663e7d621e804 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 11 Mar 2015 18:30:50 +0300 Subject: [PATCH 01/20] Minor cleanup --- rpcs3/Emu/Cell/RawSPUThread.h | 8 ++++ rpcs3/Emu/Cell/SPUThread.cpp | 49 ++++++++++++----------- rpcs3/Emu/Memory/Memory.h | 9 ----- rpcs3/Emu/SysCalls/lv2/sys_cond.cpp | 10 +++-- rpcs3/Emu/SysCalls/lv2/sys_cond.h | 2 +- rpcs3/Emu/SysCalls/lv2/sys_event.cpp | 47 +++++++--------------- rpcs3/Emu/SysCalls/lv2/sys_event.h | 10 ++++- rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp | 44 +++++++++----------- rpcs3/Emu/SysCalls/lv2/sys_event_flag.h | 3 +- rpcs3/Emu/SysCalls/lv2/sys_lwcond.cpp | 32 ++++++++++----- rpcs3/Emu/SysCalls/lv2/sys_lwcond.h | 2 +- rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp | 16 ++++++-- rpcs3/Emu/SysCalls/lv2/sys_lwmutex.h | 2 +- rpcs3/Emu/SysCalls/lv2/sys_mutex.cpp | 14 ++++--- rpcs3/Emu/SysCalls/lv2/sys_mutex.h | 2 +- rpcs3/Emu/SysCalls/lv2/sys_rwlock.cpp | 44 ++++++++++++++------ rpcs3/Emu/SysCalls/lv2/sys_rwlock.h | 11 +++-- rpcs3/Emu/SysCalls/lv2/sys_semaphore.cpp | 24 ++++++++--- rpcs3/Emu/SysCalls/lv2/sys_semaphore.h | 2 +- rpcs3/Emu/SysCalls/lv2/sys_timer.cpp | 9 +++-- 20 files changed, 195 insertions(+), 145 deletions(-) diff --git a/rpcs3/Emu/Cell/RawSPUThread.h b/rpcs3/Emu/Cell/RawSPUThread.h index d401a917c7..51811d4f22 100644 --- a/rpcs3/Emu/Cell/RawSPUThread.h +++ b/rpcs3/Emu/Cell/RawSPUThread.h @@ -1,6 +1,14 @@ #pragma once #include "SPUThread.h" +enum : u32 +{ + RAW_SPU_OFFSET = 0x00100000, + RAW_SPU_BASE_ADDR = 0xE0000000, + RAW_SPU_LS_OFFSET = 0x00000000, + RAW_SPU_PROB_OFFSET = 0x00040000, +}; + __forceinline static u32 GetRawSPURegAddrByNum(int num, int offset) { return RAW_SPU_OFFSET * num + RAW_SPU_BASE_ADDR + RAW_SPU_PROB_OFFSET + offset; diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index 5ccec5fcc5..b529b6276f 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -604,19 +604,17 @@ void SPUThread::set_ch_value(u32 ch, u32 value) if (!queue) { LOG_WARNING(SPU, "sys_spu_thread_send_event(spup=%d, data0=0x%x, data1=0x%x): event queue not connected", spup, (value & 0x00ffffff), data); - ch_in_mbox.push_uncond(CELL_ENOTCONN); // TODO: check error passing - return; + return ch_in_mbox.push_uncond(CELL_ENOTCONN); // TODO: check error passing } if (queue->events.size() >= queue->size) { - ch_in_mbox.push_uncond(CELL_EBUSY); - return; + return ch_in_mbox.push_uncond(CELL_EBUSY); } queue->push(SYS_SPU_THREAD_EVENT_USER_KEY, GetId(), ((u64)spup << 32) | (value & 0x00ffffff), data); - ch_in_mbox.push_uncond(CELL_OK); - return; + + return ch_in_mbox.push_uncond(CELL_OK); } else if (code < 128) { @@ -685,20 +683,22 @@ void SPUThread::set_ch_value(u32 ch, u32 value) if (!Emu.GetIdManager().GetIDData(data, ef)) { - ch_in_mbox.push_uncond(CELL_ESRCH); - return; + return ch_in_mbox.push_uncond(CELL_ESRCH); } - while (ef->waiters < 0) + while (ef->cancelled) { ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } ef->flags |= 1ull << flag; - ef->cv.notify_all(); - ch_in_mbox.push_uncond(CELL_OK); - return; + if (ef->waiters) + { + ef->cv.notify_all(); + } + + return ch_in_mbox.push_uncond(CELL_OK); } else if (code == 192) { @@ -732,13 +732,18 @@ void SPUThread::set_ch_value(u32 ch, u32 value) return; } - while (ef->waiters < 0) + while (ef->cancelled) { ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } ef->flags |= 1ull << flag; - ef->cv.notify_all(); + + if (ef->waiters) + { + ef->cv.notify_all(); + } + return; } else @@ -958,8 +963,7 @@ void SPUThread::stop_and_signal(u32 code) if (ch_in_mbox.get_count()) { LOG_ERROR(SPU, "sys_spu_thread_receive_event(spuq=0x%x): In_MBox is not empty", spuq); - ch_in_mbox.push_uncond(CELL_EBUSY); - return; + return ch_in_mbox.push_uncond(CELL_EBUSY); } if (Ini.HLELogging.GetValue()) @@ -986,20 +990,17 @@ void SPUThread::stop_and_signal(u32 code) if (!queue) { - ch_in_mbox.push_uncond(CELL_EINVAL); // TODO: check error value - return; + return ch_in_mbox.push_uncond(CELL_EINVAL); // TODO: check error value } // protocol is ignored in current implementation - queue->waiters++; assert(queue->waiters > 0); + queue->waiters++; while (queue->events.empty()) { - if (queue->waiters < 0) + if (queue->cancelled) { - queue->waiters--; assert(queue->waiters < 0); - ch_in_mbox.push_uncond(CELL_ECANCELED); - return; + return ch_in_mbox.push_uncond(CELL_ECANCELED); } if (Emu.IsStopped()) @@ -1018,7 +1019,7 @@ void SPUThread::stop_and_signal(u32 code) ch_in_mbox.push_uncond((u32)event.data3); queue->events.pop_front(); - queue->waiters--; assert(queue->waiters >= 0); + queue->waiters--; if (queue->events.size()) { diff --git a/rpcs3/Emu/Memory/Memory.h b/rpcs3/Emu/Memory/Memory.h index ae07d8442f..a7d82989ac 100644 --- a/rpcs3/Emu/Memory/Memory.h +++ b/rpcs3/Emu/Memory/Memory.h @@ -14,14 +14,6 @@ enum MemoryType Memory_PSP, }; -enum : u32 -{ - RAW_SPU_OFFSET = 0x00100000, - RAW_SPU_BASE_ADDR = 0xE0000000, - RAW_SPU_LS_OFFSET = 0x00000000, - RAW_SPU_PROB_OFFSET = 0x00040000, -}; - class MemoryBase { std::vector MemoryBlocks; @@ -98,4 +90,3 @@ public: extern MemoryBase Memory; #include "vm.h" - diff --git a/rpcs3/Emu/SysCalls/lv2/sys_cond.cpp b/rpcs3/Emu/SysCalls/lv2/sys_cond.cpp index 31eb558f9f..844fb58641 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_cond.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_cond.cpp @@ -170,11 +170,15 @@ s32 sys_cond_wait(PPUThread& CPU, u32 cond_id, u64 timeout) } // protocol is ignored in current implementation - cond->waiters++; assert(cond->waiters > 0); + cond->waiters++; // unlock mutex cond->mutex->owner.reset(); - cond->mutex->cv.notify_one(); + + if (cond->mutex->waiters) + { + cond->mutex->cv.notify_one(); + } // save recursive value const u32 recursive_value = cond->mutex->recursive_count.exchange(0); @@ -189,7 +193,7 @@ s32 sys_cond_wait(PPUThread& CPU, u32 cond_id, u64 timeout) // cancel waiting if the mutex is free, restore its owner and recursive value cond->mutex->owner = thread; cond->mutex->recursive_count = recursive_value; - cond->waiters--; assert(cond->waiters >= 0); + cond->waiters--; return CELL_ETIMEDOUT; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_cond.h b/rpcs3/Emu/SysCalls/lv2/sys_cond.h index eb9f3b5d40..6b2bda4720 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_cond.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_cond.h @@ -24,7 +24,7 @@ struct cond_t // TODO: use sleep queue, possibly remove condition variable std::condition_variable cv; - std::atomic waiters; + std::atomic waiters; cond_t(std::shared_ptr& mutex, u64 name) : mutex(mutex) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_event.cpp b/rpcs3/Emu/SysCalls/lv2/sys_event.cpp index e94af91c3f..fbe5ea7b8d 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_event.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_event.cpp @@ -49,18 +49,11 @@ s32 sys_event_queue_create(vm::ptr equeue_id, vm::ptr default: sys_event.Error("sys_event_queue_create(): unknown type (0x%x)", type); return CELL_EINVAL; } - LV2_LOCK; - - if (Emu.GetEventManager().CheckKey(event_queue_key)) - { - return CELL_EEXIST; - } - std::shared_ptr queue(new event_queue_t(protocol, type, attr->name_u64, event_queue_key, size)); if (!Emu.GetEventManager().RegisterKey(queue, event_queue_key)) { - return CELL_EAGAIN; + return CELL_EEXIST; } *equeue_id = Emu.GetIdManager().GetNewID(queue, TYPE_EVENT_QUEUE); @@ -86,16 +79,18 @@ s32 sys_event_queue_destroy(u32 equeue_id, s32 mode) return CELL_EINVAL; } - assert(queue->waiters >= 0); - if (!mode && queue->waiters) { return CELL_EBUSY; } - else + + if (queue->cancelled.exchange(true)) + { + throw __FUNCTION__; + } + + if (queue->waiters) { - // set special value for waiters - queue->waiters.exchange(-1); queue->cv.notify_all(); } @@ -107,7 +102,7 @@ s32 sys_event_queue_destroy(u32 equeue_id, s32 mode) s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr event_array, s32 size, vm::ptr number) { - sys_event.Warning("sys_event_queue_tryreceive(equeue_id=%d, event_array=*0x%x, size=%d, number=*0x%x)", equeue_id, event_array, size, number); + sys_event.Log("sys_event_queue_tryreceive(equeue_id=%d, event_array=*0x%x, size=%d, number=*0x%x)", equeue_id, event_array, size, number); LV2_LOCK; @@ -130,7 +125,7 @@ s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr event_array, s32 count = 0; - while (count < size && queue->events.size()) + while (!queue->waiters && count < size && queue->events.size()) { auto& event = queue->events.front(); event_array[count++] = { be_t::make(event.source), be_t::make(event.data1), be_t::make(event.data2), be_t::make(event.data3) }; @@ -140,11 +135,6 @@ s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr event_array, *number = count; - if (queue->events.size()) - { - queue->cv.notify_one(); - } - return CELL_OK; } @@ -169,19 +159,19 @@ s32 sys_event_queue_receive(PPUThread& CPU, u32 equeue_id, vm::ptr } // protocol is ignored in current implementation - queue->waiters++; assert(queue->waiters > 0); + queue->waiters++; while (queue->events.empty()) { - if (queue->waiters < 0) + if (queue->cancelled) { - queue->waiters--; assert(queue->waiters < 0); + queue->waiters--; return CELL_ECANCELED; } if (timeout && get_system_time() - start_time > timeout) { - queue->waiters--; assert(queue->waiters >= 0); + queue->waiters--; return CELL_ETIMEDOUT; } @@ -202,12 +192,7 @@ s32 sys_event_queue_receive(PPUThread& CPU, u32 equeue_id, vm::ptr CPU.GPR[7] = event.data3; queue->events.pop_front(); - queue->waiters--; assert(queue->waiters >= 0); - - if (queue->events.size()) - { - queue->cv.notify_one(); - } + queue->waiters--; return CELL_OK; } @@ -247,8 +232,6 @@ s32 sys_event_port_create(vm::ptr eport_id, s32 port_type, u64 name) return CELL_EINVAL; } - LV2_LOCK; - std::shared_ptr eport(new event_port_t(port_type, name)); *eport_id = Emu.GetIdManager().GetNewID(eport, TYPE_EVENT_PORT); diff --git a/rpcs3/Emu/SysCalls/lv2/sys_event.h b/rpcs3/Emu/SysCalls/lv2/sys_event.h index 781df1d5a5..842d4b267a 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_event.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_event.h @@ -78,10 +78,11 @@ struct event_queue_t const s32 size; std::deque events; + std::atomic cancelled; // TODO: use sleep queue, possibly remove condition variable std::condition_variable cv; - std::atomic waiters; + std::atomic waiters; event_queue_t(u32 protocol, s32 type, u64 name, u64 key, s32 size) : protocol(protocol) @@ -89,6 +90,7 @@ struct event_queue_t , name(name) , key(key) , size(size) + , cancelled(false) , waiters(0) { } @@ -96,7 +98,11 @@ struct event_queue_t void push(u64 source, u64 data1, u64 data2, u64 data3) { events.emplace_back(source, data1, data2, data3); - cv.notify_one(); + + if (waiters) + { + cv.notify_one(); + } } }; diff --git a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp index 718260f28c..bd1857bdd4 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp @@ -16,8 +16,6 @@ s32 sys_event_flag_create(vm::ptr id, vm::ptr attr, u6 { sys_event_flag.Warning("sys_event_flag_create(id=*0x%x, attr=*0x%x, init=0x%llx)", id, attr, init); - LV2_LOCK; - if (!id || !attr) { return CELL_EFAULT; @@ -69,11 +67,6 @@ s32 sys_event_flag_destroy(u32 id) return CELL_ESRCH; } - while (ef->waiters < 0) - { - ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); - } - if (ef->waiters) { return CELL_EBUSY; @@ -124,7 +117,7 @@ s32 sys_event_flag_wait(u32 id, u64 bitptn, u32 mode, vm::ptr result, u64 t return CELL_EPERM; } - while (ef->waiters < 0) + while (ef->cancelled) { // wait until other threads return CELL_ECANCELED (to prevent modifying bit pattern) ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); @@ -150,11 +143,9 @@ s32 sys_event_flag_wait(u32 id, u64 bitptn, u32 mode, vm::ptr result, u64 t break; } - if (ef->waiters <= 0) + if (ef->cancelled) { - ef->waiters++; assert(ef->waiters <= 0); - - if (!ef->waiters) + if (!--ef->cancelled) { ef->cv.notify_all(); } @@ -164,7 +155,7 @@ s32 sys_event_flag_wait(u32 id, u64 bitptn, u32 mode, vm::ptr result, u64 t if (timeout && get_system_time() - start_time > timeout) { - ef->waiters--; assert(ef->waiters >= 0); + ef->waiters--; return CELL_ETIMEDOUT; } @@ -187,9 +178,7 @@ s32 sys_event_flag_wait(u32 id, u64 bitptn, u32 mode, vm::ptr result, u64 t ef->flags = 0; } - ef->waiters--; assert(ef->waiters >= 0); - - if (ef->flags) + if (--ef->waiters && ef->flags) { ef->cv.notify_one(); } @@ -235,7 +224,7 @@ s32 sys_event_flag_trywait(u32 id, u64 bitptn, u32 mode, vm::ptr result) return CELL_EBUSY; } - while (ef->waiters < 0) + while (ef->cancelled) { ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } @@ -270,13 +259,17 @@ s32 sys_event_flag_set(u32 id, u64 bitptn) return CELL_ESRCH; } - while (ef->waiters < 0) + while (ef->cancelled) { ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } ef->flags |= bitptn; - ef->cv.notify_all(); + + if (ef->waiters) + { + ef->cv.notify_all(); + } return CELL_OK; } @@ -294,7 +287,7 @@ s32 sys_event_flag_clear(u32 id, u64 bitptn) return CELL_ESRCH; } - while (ef->waiters < 0) + while (ef->cancelled) { ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } @@ -322,7 +315,7 @@ s32 sys_event_flag_cancel(u32 id, vm::ptr num) return CELL_ESRCH; } - while (ef->waiters < 0) + while (ef->cancelled) { ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } @@ -332,10 +325,11 @@ s32 sys_event_flag_cancel(u32 id, vm::ptr num) *num = ef->waiters; } - // negate value to signal waiting threads and prevent modifying bit pattern - ef->waiters = -ef->waiters; - ef->cv.notify_all(); - + if ((ef->cancelled = ef->waiters.exchange(0))) + { + ef->cv.notify_all(); + } + return CELL_OK; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h index a470d6e496..7e15f27246 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h @@ -34,10 +34,11 @@ struct event_flag_t const u64 name; std::atomic flags; + std::atomic cancelled; // TODO: use sleep queue, possibly remove condition variable std::condition_variable cv; - std::atomic waiters; + std::atomic waiters; event_flag_t(u64 pattern, u32 protocol, s32 type, u64 name) : flags(pattern) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_lwcond.cpp b/rpcs3/Emu/SysCalls/lv2/sys_lwcond.cpp index 9cb791bfb7..a2765b462a 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_lwcond.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_lwcond.cpp @@ -38,6 +38,7 @@ s32 _sys_lwcond_destroy(u32 lwcond_id) LV2_LOCK; std::shared_ptr cond; + if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) { return CELL_ESRCH; @@ -60,12 +61,13 @@ s32 _sys_lwcond_signal(u32 lwcond_id, u32 lwmutex_id, u32 ppu_thread_id, u32 mod LV2_LOCK; std::shared_ptr cond; + std::shared_ptr mutex; + if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) { return CELL_ESRCH; } - std::shared_ptr mutex; if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) { return CELL_ESRCH; @@ -117,8 +119,10 @@ s32 _sys_lwcond_signal(u32 lwcond_id, u32 lwmutex_id, u32 ppu_thread_id, u32 mod cond->signaled1++; } - cond->waiters--; - cond->cv.notify_one(); + if (--cond->waiters) + { + cond->cv.notify_one(); + } return CELL_OK; } @@ -130,12 +134,13 @@ s32 _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode) LV2_LOCK; std::shared_ptr cond; + std::shared_ptr mutex; + if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) { return CELL_ESRCH; } - std::shared_ptr mutex; if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) { return CELL_ESRCH; @@ -147,7 +152,11 @@ s32 _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode) } const s32 count = cond->waiters.exchange(0); - cond->cv.notify_all(); + + if (count) + { + cond->cv.notify_all(); + } if (mode == 1) { @@ -176,12 +185,13 @@ s32 _sys_lwcond_queue_wait(u32 lwcond_id, u32 lwmutex_id, u64 timeout) LV2_LOCK; std::shared_ptr cond; + std::shared_ptr mutex; + if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) { return CELL_ESRCH; } - std::shared_ptr mutex; if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) { return CELL_ESRCH; @@ -189,10 +199,14 @@ s32 _sys_lwcond_queue_wait(u32 lwcond_id, u32 lwmutex_id, u64 timeout) // finalize unlocking the mutex mutex->signaled++; - mutex->cv.notify_one(); + + if (mutex->waiters) + { + mutex->cv.notify_one(); + } // protocol is ignored in current implementation - cond->waiters++; assert(cond->waiters > 0); + cond->waiters++; while (!(cond->signaled1 && mutex->signaled) && !cond->signaled2) { @@ -202,7 +216,7 @@ s32 _sys_lwcond_queue_wait(u32 lwcond_id, u32 lwmutex_id, u64 timeout) if (is_timedout && !cond->signaled1) { // cancel waiting - cond->waiters--; assert(cond->waiters >= 0); + cond->waiters--; if (mutex->signaled) { diff --git a/rpcs3/Emu/SysCalls/lv2/sys_lwcond.h b/rpcs3/Emu/SysCalls/lv2/sys_lwcond.h index 8236cc2fe6..ef02f85899 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_lwcond.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_lwcond.h @@ -26,7 +26,7 @@ struct lwcond_t // TODO: use sleep queue std::condition_variable cv; - std::atomic waiters; + std::atomic waiters; lwcond_t(u64 name) : name(name) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp b/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp index 96d87ab0b4..5b8f99e1c3 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp @@ -53,6 +53,7 @@ s32 _sys_lwmutex_destroy(u32 lwmutex_id) LV2_LOCK; std::shared_ptr mutex; + if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) { return CELL_ESRCH; @@ -77,19 +78,20 @@ s32 _sys_lwmutex_lock(u32 lwmutex_id, u64 timeout) LV2_LOCK; std::shared_ptr mutex; + if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) { return CELL_ESRCH; } // protocol is ignored in current implementation - mutex->waiters++; assert(mutex->waiters > 0); + mutex->waiters++; while (!mutex->signaled) { if (timeout && get_system_time() - start_time > timeout) { - mutex->waiters--; assert(mutex->waiters >= 0); + mutex->waiters--; return CELL_ETIMEDOUT; } @@ -104,7 +106,7 @@ s32 _sys_lwmutex_lock(u32 lwmutex_id, u64 timeout) mutex->signaled--; - mutex->waiters--; assert(mutex->waiters >= 0); + mutex->waiters--; return CELL_OK; } @@ -116,6 +118,7 @@ s32 _sys_lwmutex_trylock(u32 lwmutex_id) LV2_LOCK; std::shared_ptr mutex; + if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) { return CELL_ESRCH; @@ -138,6 +141,7 @@ s32 _sys_lwmutex_unlock(u32 lwmutex_id) LV2_LOCK; std::shared_ptr mutex; + if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) { return CELL_ESRCH; @@ -149,7 +153,11 @@ s32 _sys_lwmutex_unlock(u32 lwmutex_id) } mutex->signaled++; - mutex->cv.notify_one(); + + if (mutex->waiters) + { + mutex->cv.notify_one(); + } return CELL_OK; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.h b/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.h index f29e2866d6..4a26d65909 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.h @@ -76,7 +76,7 @@ struct lwmutex_t // TODO: use sleep queue, possibly remove condition variable std::condition_variable cv; - std::atomic waiters; + std::atomic waiters; lwmutex_t(u32 protocol, u64 name) : protocol(protocol) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_mutex.cpp b/rpcs3/Emu/SysCalls/lv2/sys_mutex.cpp index 4eefd74ca0..29ee05df8d 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_mutex.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_mutex.cpp @@ -16,8 +16,6 @@ s32 sys_mutex_create(vm::ptr mutex_id, vm::ptr attr) { sys_mutex.Warning("sys_mutex_create(mutex_id=*0x%x, attr=*0x%x)", mutex_id, attr); - LV2_LOCK; - if (!mutex_id || !attr) { return CELL_EFAULT; @@ -119,13 +117,13 @@ s32 sys_mutex_lock(PPUThread& CPU, u32 mutex_id, u64 timeout) } // protocol is ignored in current implementation - mutex->waiters++; assert(mutex->waiters > 0); + mutex->waiters++; while (!mutex->owner.expired()) { if (timeout && get_system_time() - start_time > timeout) { - mutex->waiters--; assert(mutex->waiters >= 0); + mutex->waiters--; return CELL_ETIMEDOUT; } @@ -139,7 +137,7 @@ s32 sys_mutex_lock(PPUThread& CPU, u32 mutex_id, u64 timeout) } mutex->owner = thread; - mutex->waiters--; assert(mutex->waiters >= 0); + mutex->waiters--; return CELL_OK; } @@ -218,7 +216,11 @@ s32 sys_mutex_unlock(PPUThread& CPU, u32 mutex_id) else { mutex->owner.reset(); - mutex->cv.notify_one(); + + if (mutex->waiters) + { + mutex->cv.notify_one(); + } } return CELL_OK; diff --git a/rpcs3/Emu/SysCalls/lv2/sys_mutex.h b/rpcs3/Emu/SysCalls/lv2/sys_mutex.h index 3d5f01e509..31f155b574 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_mutex.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_mutex.h @@ -29,7 +29,7 @@ struct mutex_t // TODO: use sleep queue, possibly remove condition variable std::condition_variable cv; - std::atomic waiters; + std::atomic waiters; mutex_t(bool recursive, u32 protocol, u64 name) : recursive(recursive) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_rwlock.cpp b/rpcs3/Emu/SysCalls/lv2/sys_rwlock.cpp index 45998a738a..81c79aacf4 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_rwlock.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_rwlock.cpp @@ -50,12 +50,13 @@ s32 sys_rwlock_destroy(u32 rw_lock_id) LV2_LOCK; std::shared_ptr rwlock; + if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) { return CELL_ESRCH; } - if (rwlock.use_count() > 2 || rwlock->readers || rwlock->writer || rwlock->waiters) + if (rwlock->readers || rwlock->writer || rwlock->rwaiters || rwlock->wwaiters) { return CELL_EBUSY; } @@ -74,15 +75,20 @@ s32 sys_rwlock_rlock(u32 rw_lock_id, u64 timeout) LV2_LOCK; std::shared_ptr rwlock; + if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) { return CELL_ESRCH; } - while (rwlock->writer || rwlock->waiters) + // waiting threads are not properly registered in current implementation + rwlock->rwaiters++; + + while (rwlock->writer || rwlock->wwaiters) { if (timeout && get_system_time() - start_time > timeout) { + rwlock->rwaiters--; return CELL_ETIMEDOUT; } @@ -92,10 +98,11 @@ s32 sys_rwlock_rlock(u32 rw_lock_id, u64 timeout) return CELL_OK; } - rwlock->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); + rwlock->rcv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } rwlock->readers++; + rwlock->rwaiters--; return CELL_OK; } @@ -107,12 +114,13 @@ s32 sys_rwlock_tryrlock(u32 rw_lock_id) LV2_LOCK; std::shared_ptr rwlock; + if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) { return CELL_ESRCH; } - if (rwlock->writer || rwlock->waiters) + if (rwlock->writer || rwlock->wwaiters) { return CELL_EBUSY; } @@ -129,6 +137,7 @@ s32 sys_rwlock_runlock(u32 rw_lock_id) LV2_LOCK; std::shared_ptr rwlock; + if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) { return CELL_ESRCH; @@ -139,9 +148,9 @@ s32 sys_rwlock_runlock(u32 rw_lock_id) return CELL_EPERM; } - if (!--rwlock->readers) + if (!--rwlock->readers && rwlock->wwaiters) { - rwlock->cv.notify_one(); + rwlock->wcv.notify_one(); } return CELL_OK; @@ -156,6 +165,7 @@ s32 sys_rwlock_wlock(PPUThread& CPU, u32 rw_lock_id, u64 timeout) LV2_LOCK; std::shared_ptr rwlock; + if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) { return CELL_ESRCH; @@ -167,13 +177,13 @@ s32 sys_rwlock_wlock(PPUThread& CPU, u32 rw_lock_id, u64 timeout) } // protocol is ignored in current implementation - rwlock->waiters++; assert(rwlock->waiters > 0); + rwlock->wwaiters++; while (rwlock->readers || rwlock->writer) { if (timeout && get_system_time() - start_time > timeout) { - rwlock->waiters--; assert(rwlock->waiters >= 0); + rwlock->wwaiters--; return CELL_ETIMEDOUT; } @@ -183,11 +193,11 @@ s32 sys_rwlock_wlock(PPUThread& CPU, u32 rw_lock_id, u64 timeout) return CELL_OK; } - rwlock->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); + rwlock->wcv.wait_for(lv2_lock, std::chrono::milliseconds(1)); } rwlock->writer = CPU.GetId(); - rwlock->waiters--; assert(rwlock->waiters >= 0); + rwlock->wwaiters--; return CELL_OK; } @@ -199,6 +209,7 @@ s32 sys_rwlock_trywlock(PPUThread& CPU, u32 rw_lock_id) LV2_LOCK; std::shared_ptr rwlock; + if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) { return CELL_ESRCH; @@ -209,7 +220,7 @@ s32 sys_rwlock_trywlock(PPUThread& CPU, u32 rw_lock_id) return CELL_EDEADLK; } - if (rwlock->readers || rwlock->writer || rwlock->waiters) + if (rwlock->readers || rwlock->writer || rwlock->wwaiters) { return CELL_EBUSY; } @@ -226,6 +237,7 @@ s32 sys_rwlock_wunlock(PPUThread& CPU, u32 rw_lock_id) LV2_LOCK; std::shared_ptr rwlock; + if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) { return CELL_ESRCH; @@ -237,7 +249,15 @@ s32 sys_rwlock_wunlock(PPUThread& CPU, u32 rw_lock_id) } rwlock->writer = 0; - rwlock->cv.notify_all(); + + if (rwlock->wwaiters) + { + rwlock->wcv.notify_one(); + } + else if (rwlock->rwaiters) + { + rwlock->rcv.notify_all(); + } return CELL_OK; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_rwlock.h b/rpcs3/Emu/SysCalls/lv2/sys_rwlock.h index 495b94bac9..5fdfa81e94 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_rwlock.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_rwlock.h @@ -23,16 +23,19 @@ struct rwlock_t std::atomic readers; // reader count std::atomic writer; // writer id - // TODO: use sleep queue, possibly remove condition variable - std::condition_variable cv; - std::atomic waiters; + // TODO: use sleep queue, possibly remove condition variables + std::condition_variable rcv; + std::condition_variable wcv; + std::atomic rwaiters; + std::atomic wwaiters; rwlock_t(u32 protocol, u64 name) : protocol(protocol) , name(name) , readers(0) , writer(0) - , waiters(0) + , rwaiters(0) + , wwaiters(0) { } }; diff --git a/rpcs3/Emu/SysCalls/lv2/sys_semaphore.cpp b/rpcs3/Emu/SysCalls/lv2/sys_semaphore.cpp index a0366516fa..72b7fd7bd0 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_semaphore.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_semaphore.cpp @@ -62,6 +62,7 @@ s32 sys_semaphore_destroy(u32 sem) LV2_LOCK; std::shared_ptr semaphore; + if (!Emu.GetIdManager().GetIDData(sem, semaphore)) { return CELL_ESRCH; @@ -86,19 +87,20 @@ s32 sys_semaphore_wait(u32 sem, u64 timeout) LV2_LOCK; std::shared_ptr semaphore; + if (!Emu.GetIdManager().GetIDData(sem, semaphore)) { return CELL_ESRCH; } // protocol is ignored in current implementation - semaphore->waiters++; assert(semaphore->waiters > 0); + semaphore->waiters++; while (semaphore->value <= 0) { if (timeout && get_system_time() - start_time > timeout) { - semaphore->waiters--; assert(semaphore->waiters >= 0); + semaphore->waiters--; return CELL_ETIMEDOUT; } @@ -112,7 +114,7 @@ s32 sys_semaphore_wait(u32 sem, u64 timeout) } semaphore->value--; - semaphore->waiters--; assert(semaphore->waiters >= 0); + semaphore->waiters--; return CELL_OK; } @@ -124,6 +126,7 @@ s32 sys_semaphore_trywait(u32 sem) LV2_LOCK; std::shared_ptr semaphore; + if (!Emu.GetIdManager().GetIDData(sem, semaphore)) { return CELL_ESRCH; @@ -146,6 +149,7 @@ s32 sys_semaphore_post(u32 sem, s32 count) LV2_LOCK; std::shared_ptr semaphore; + if (!Emu.GetIdManager().GetIDData(sem, semaphore)) { return CELL_ESRCH; @@ -156,13 +160,20 @@ s32 sys_semaphore_post(u32 sem, s32 count) return CELL_EINVAL; } - if (semaphore->value + count > semaphore->max + semaphore->waiters) + const u64 new_value = semaphore->value + count; + const u64 max_value = semaphore->max + semaphore->waiters; + + if (new_value > max_value) { return CELL_EBUSY; } - semaphore->value += count; assert(semaphore->value >= 0); - semaphore->cv.notify_all(); + semaphore->value += count; + + if (semaphore->waiters) + { + semaphore->cv.notify_all(); + } return CELL_OK; } @@ -179,6 +190,7 @@ s32 sys_semaphore_get_value(u32 sem, vm::ptr count) LV2_LOCK; std::shared_ptr semaphore; + if (!Emu.GetIdManager().GetIDData(sem, semaphore)) { return CELL_ESRCH; diff --git a/rpcs3/Emu/SysCalls/lv2/sys_semaphore.h b/rpcs3/Emu/SysCalls/lv2/sys_semaphore.h index 674b2d94a8..102545bc2c 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_semaphore.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_semaphore.h @@ -25,7 +25,7 @@ struct semaphore_t // TODO: use sleep queue, possibly remove condition variable std::condition_variable cv; - std::atomic waiters; + std::atomic waiters; semaphore_t(u32 protocol, s32 max, u64 name, s32 value) : protocol(protocol) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_timer.cpp b/rpcs3/Emu/SysCalls/lv2/sys_timer.cpp index 7f86c570a9..823199a827 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_timer.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_timer.cpp @@ -32,8 +32,7 @@ s32 sys_timer_create(vm::ptr timer_id) if (queue) { - queue->events.emplace_back(timer->source, timer->data1, timer->data2, timer->start); - queue->cv.notify_one(); + queue->push(timer->source, timer->data1, timer->data2, timer->start); } if (timer->period && queue) @@ -64,6 +63,7 @@ s32 sys_timer_destroy(u32 timer_id) LV2_LOCK; std::shared_ptr timer; + if (!Emu.GetIdManager().GetIDData(timer_id, timer)) { return CELL_ESRCH; @@ -86,6 +86,7 @@ s32 sys_timer_get_information(u32 timer_id, vm::ptr inf LV2_LOCK; std::shared_ptr timer; + if (!Emu.GetIdManager().GetIDData(timer_id, timer)) { return CELL_ESRCH; @@ -94,7 +95,6 @@ s32 sys_timer_get_information(u32 timer_id, vm::ptr inf info->next_expiration_time = timer->start; info->period = timer->period; - info->timer_state = timer->state; return CELL_OK; @@ -109,6 +109,7 @@ s32 _sys_timer_start(u32 timer_id, u64 base_time, u64 period) LV2_LOCK; std::shared_ptr timer; + if (!Emu.GetIdManager().GetIDData(timer_id, timer)) { return CELL_ESRCH; @@ -160,6 +161,7 @@ s32 sys_timer_stop(u32 timer_id) LV2_LOCK; std::shared_ptr timer; + if (!Emu.GetIdManager().GetIDData(timer_id, timer)) { return CELL_ESRCH; @@ -204,6 +206,7 @@ s32 sys_timer_disconnect_event_queue(u32 timer_id) LV2_LOCK; std::shared_ptr timer; + if (!Emu.GetIdManager().GetIDData(timer_id, timer)) { return CELL_ESRCH; From bd517cf91596735d7807d43a316fcb90a4d3e711 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 11 Mar 2015 19:58:50 +0300 Subject: [PATCH 02/20] atomic_t operators update --- rpcs3/Emu/Memory/atomic.h | 116 ++++++++++++++++++++++-- rpcs3/Emu/SysCalls/Modules/cellSync.cpp | 2 +- 2 files changed, 110 insertions(+), 8 deletions(-) diff --git a/rpcs3/Emu/Memory/atomic.h b/rpcs3/Emu/Memory/atomic.h index e6f9995ec6..9641aadf95 100644 --- a/rpcs3/Emu/Memory/atomic.h +++ b/rpcs3/Emu/Memory/atomic.h @@ -197,10 +197,112 @@ public: } }; -template inline static typename std::enable_if::value, T>::type operator ++(_atomic_base>& left, int) +// Helper definitions +template using if_arithmetic_t = typename std::enable_if::value && std::is_arithmetic::value, T>::type; +template using if_arithmetic_be_t = typename std::enable_if::value && std::is_arithmetic::value, be_t>::type; +template using if_arithmetic_atomic_t = typename std::enable_if::value && std::is_arithmetic::value, _atomic_base>::type; +template using if_arithmetic_atomic_be_t = typename std::enable_if::value && std::is_arithmetic::value, _atomic_base>>::type; + +template inline static if_arithmetic_t operator ++(_atomic_base& left) { T result; + left.atomic_op([&result](T& value) + { + result = ++value; + }); + + return result; +} + +template inline static if_arithmetic_t operator --(_atomic_base& left) +{ + T result; + + left.atomic_op([&result](T& value) + { + result = --value; + }); + + return result; +} + +template inline static if_arithmetic_t operator ++(_atomic_base& left, int) +{ + T result; + + left.atomic_op([&result](T& value) + { + result = value++; + }); + + return result; +} + +template inline static if_arithmetic_t operator --(_atomic_base& left, int) +{ + T result; + + left.atomic_op([&result](T& value) + { + result = value--; + }); + + return result; +} + +template inline static if_arithmetic_t operator +=(_atomic_base& left, T2 right) +{ + T result; + + left.atomic_op([&result, right](T& value) + { + result = (value += right); + }); + + return result; +} + +template inline static if_arithmetic_t operator -=(_atomic_base& left, T2 right) +{ + T result; + + left.atomic_op([&result, right](T& value) + { + result = (value -= right); + }); + + return result; +} + +template inline static if_arithmetic_be_t operator ++(_atomic_base>& left) +{ + be_t result; + + left.atomic_op([&result](be_t& value) + { + result = ++value; + }); + + return result; +} + +template inline static if_arithmetic_be_t operator --(_atomic_base>& left) +{ + be_t result; + + left.atomic_op([&result](be_t& value) + { + result = --value; + }); + + return result; +} + +template inline static if_arithmetic_be_t operator ++(_atomic_base>& left, int) +{ + be_t result; + left.atomic_op([&result](be_t& value) { result = value++; @@ -209,9 +311,9 @@ template inline static typename std::enable_if return result; } -template inline static typename std::enable_if::value, T>::type operator --(_atomic_base>& left, int) +template inline static if_arithmetic_be_t operator --(_atomic_base>& left, int) { - T result; + be_t result; left.atomic_op([&result](be_t& value) { @@ -221,9 +323,9 @@ template inline static typename std::enable_if return result; } -template inline static typename std::enable_if::value, T>::type operator +=(_atomic_base>& left, T2 right) +template inline static if_arithmetic_be_t operator +=(_atomic_base>& left, T2 right) { - T result; + be_t result; left.atomic_op([&result, right](be_t& value) { @@ -233,9 +335,9 @@ template inline static typename std::enable_if inline static typename std::enable_if::value, T>::type operator -=(_atomic_base>& left, T2 right) +template inline static if_arithmetic_be_t operator -=(_atomic_base>& left, T2 right) { - T result; + be_t result; left.atomic_op([&result, right](be_t& value) { diff --git a/rpcs3/Emu/SysCalls/Modules/cellSync.cpp b/rpcs3/Emu/SysCalls/Modules/cellSync.cpp index 618653b7d9..56f5f43c81 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellSync.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellSync.cpp @@ -58,7 +58,7 @@ s32 cellSyncMutexLock(vm::ptr mutex) } // prx: increase acquire_count and remember its old value - const be_t order = be_t::make(mutex->acquire_count++); + const auto order = mutex->acquire_count++; // prx: wait until release_count is equal to old acquire_count g_sync_mutex_wm.wait_op(mutex.addr(), [mutex, order]() From 0cf7bb056e67a85a59002846be5f24f49b69d695 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 11 Mar 2015 20:50:11 +0300 Subject: [PATCH 03/20] Bugfix --- rpcs3/Emu/SysCalls/lv2/sys_event_flag.h | 1 + 1 file changed, 1 insertion(+) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h index 7e15f27246..c76effbea4 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.h @@ -45,6 +45,7 @@ struct event_flag_t , protocol(protocol) , type(type) , name(name) + , cancelled(0) , waiters(0) { } From 583b6d92ecaf6114dcf8575a0b5ffd41a1879476 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Thu, 12 Mar 2015 22:02:02 +0300 Subject: [PATCH 04/20] sys_fs syscalls detached from cellFs --- rpcs3/Emu/IdManager.h | 28 +- rpcs3/Emu/Memory/vm_var.h | 16 +- rpcs3/Emu/SysCalls/ErrorCodes.h | 2 +- rpcs3/Emu/SysCalls/ModuleManager.cpp | 4 +- rpcs3/Emu/SysCalls/Modules/cellFont.cpp | 4 +- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 769 ++++++++++++ rpcs3/Emu/SysCalls/Modules/cellFs.h | 40 + rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp | 41 +- rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp | 41 +- rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp | 44 +- rpcs3/Emu/SysCalls/Modules/cellResc.cpp | 2 +- rpcs3/Emu/SysCalls/Modules/cellSpurs.cpp | 4 +- rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp | 6 +- rpcs3/Emu/SysCalls/SysCalls.cpp | 44 +- rpcs3/Emu/SysCalls/lv2/cellFs.cpp | 1091 ------------------ rpcs3/Emu/SysCalls/lv2/cellFs.h | 154 --- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 511 ++++++++ rpcs3/Emu/SysCalls/lv2/sys_fs.h | 166 +++ rpcs3/emucore.vcxproj | 6 +- rpcs3/emucore.vcxproj.filters | 18 +- 20 files changed, 1635 insertions(+), 1356 deletions(-) create mode 100644 rpcs3/Emu/SysCalls/Modules/cellFs.cpp create mode 100644 rpcs3/Emu/SysCalls/Modules/cellFs.h delete mode 100644 rpcs3/Emu/SysCalls/lv2/cellFs.cpp delete mode 100644 rpcs3/Emu/SysCalls/lv2/cellFs.h create mode 100644 rpcs3/Emu/SysCalls/lv2/sys_fs.cpp create mode 100644 rpcs3/Emu/SysCalls/lv2/sys_fs.h diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index da2bc62b2c..eddb77e483 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -153,7 +153,9 @@ public: std::lock_guard lock(m_mtx_main); m_id_map.emplace(m_cur_id, ID(data, type)); - if (type < TYPE_OTHER) { + + if (type < TYPE_OTHER) + { m_types[type].insert(m_cur_id); } @@ -173,7 +175,8 @@ public: auto f = m_id_map.find(id); - if (f == m_id_map.end() || f->second.GetInfo() != typeid(T)) { + if (f == m_id_map.end() || f->second.GetInfo() != typeid(T)) + { return false; } @@ -182,6 +185,20 @@ public: return true; } + template std::shared_ptr GetIDData(const u32 id) + { + std::lock_guard lock(m_mtx_main); + + auto f = m_id_map.find(id); + + if (f == m_id_map.end() || f->second.GetInfo() != typeid(T)) + { + return nullptr; + } + + return f->second.GetData()->get(); + } + bool HasID(const u32 id) { std::lock_guard lock(m_mtx_main); @@ -195,10 +212,13 @@ public: auto item = m_id_map.find(id); - if (item == m_id_map.end() || item->second.GetInfo() != typeid(T)) { + if (item == m_id_map.end() || item->second.GetInfo() != typeid(T)) + { return false; } - if (item->second.GetType() < TYPE_OTHER) { + + if (item->second.GetType() < TYPE_OTHER) + { m_types[item->second.GetType()].erase(id); } diff --git a/rpcs3/Emu/Memory/vm_var.h b/rpcs3/Emu/Memory/vm_var.h index a8c380aac1..1a3d104c3d 100644 --- a/rpcs3/Emu/Memory/vm_var.h +++ b/rpcs3/Emu/Memory/vm_var.h @@ -108,14 +108,14 @@ namespace vm } */ - template operator const ps3::ptr() const + template operator _ptr_base() const { - return ps3::ptr::make(m_addr); + return _ptr_base::make(m_addr); } - template operator const ps3::ptr() const + template operator _ptr_base() const { - return ps3::ptr::make(m_addr); + return _ptr_base::make(m_addr); } operator T&() @@ -604,14 +604,14 @@ namespace vm } */ - template operator const ps3::ptr() const + template operator _ptr_base() const { - return ps3::ptr::make(m_data.addr); + return _ptr_base::make(m_data.addr); } - template operator const ps3::ptr() const + template operator _ptr_base() const { - return ps3::ptr::make(m_data.addr); + return _ptr_base::make(m_data.addr); } operator T&() diff --git a/rpcs3/Emu/SysCalls/ErrorCodes.h b/rpcs3/Emu/SysCalls/ErrorCodes.h index 5f5a512685..9cedbc510b 100644 --- a/rpcs3/Emu/SysCalls/ErrorCodes.h +++ b/rpcs3/Emu/SysCalls/ErrorCodes.h @@ -34,7 +34,7 @@ enum ErrorCode CELL_EFPOS = 0x8001001E, CELL_EINTR = 0x8001001F, CELL_EFBIG = 0x80010020, - CELL_EMLIN = 0x80010021, + CELL_EMLINK = 0x80010021, CELL_ENFILE = 0x80010022, CELL_ENOSPC = 0x80010023, CELL_ENOTTY = 0x80010024, diff --git a/rpcs3/Emu/SysCalls/ModuleManager.cpp b/rpcs3/Emu/SysCalls/ModuleManager.cpp index 02183c82e7..e0256fd20e 100644 --- a/rpcs3/Emu/SysCalls/ModuleManager.cpp +++ b/rpcs3/Emu/SysCalls/ModuleManager.cpp @@ -10,6 +10,7 @@ extern Module cellDmux; extern Module cellFiber; extern Module cellFont; extern Module cellFontFT; +extern Module cellFs; extern Module cellGame; extern Module cellGcmSys; extern Module cellGem; @@ -49,7 +50,6 @@ extern Module sceNpCommerce2; extern Module sceNpSns; extern Module sceNpTrophy; extern Module sceNpTus; -extern Module sys_fs; extern Module sys_io; extern Module sys_net; extern Module sysPrxForUser; @@ -77,7 +77,7 @@ static const g_module_list[] = { 0x000b, "cellOvis", &cellOvis }, { 0x000c, "cellSheap", nullptr }, { 0x000d, "sys_sync", &cellSync }, - { 0x000e, "sys_fs", &sys_fs }, + { 0x000e, "sys_fs", &cellFs }, { 0x000f, "cellJpgDec", &cellJpgDec }, { 0x0010, "cellGcmSys", &cellGcmSys }, { 0x0011, "cellAudio", &cellAudio }, diff --git a/rpcs3/Emu/SysCalls/Modules/cellFont.cpp b/rpcs3/Emu/SysCalls/Modules/cellFont.cpp index 6e58293669..3d574a2486 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFont.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFont.cpp @@ -30,7 +30,7 @@ int cellFontInitializeWithRevision(u64 revisionFlags, vm::ptr co return CELL_FONT_OK; } -int cellFontGetRevisionFlags(vm::ptr> revisionFlags) +int cellFontGetRevisionFlags(vm::ptr revisionFlags) { UNIMPLEMENTED_FUNC(cellFont); return CELL_FONT_OK; @@ -375,7 +375,7 @@ int cellFontSetEffectSlant(vm::ptr font, float slantParam) return CELL_FONT_OK; } -int cellFontGetEffectSlant(vm::ptr font, vm::ptr> slantParam) +int cellFontGetEffectSlant(vm::ptr font, vm::ptr slantParam) { cellFont.Warning("cellFontSetEffectSlant(font_addr=0x%x, slantParam_addr=0x%x)", font.addr(), slantParam.addr()); diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp new file mode 100644 index 0000000000..d3ca1644ad --- /dev/null +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -0,0 +1,769 @@ +#include "stdafx.h" +#include "Emu/Memory/Memory.h" +#include "Emu/System.h" +#include "Emu/IdManager.h" +#include "Emu/SysCalls/Modules.h" +#include "Emu/SysCalls/Callback.h" +#include "Emu/SysCalls/CB_FUNC.h" + +#include "Emu/FS/VFS.h" +#include "Emu/FS/vfsFile.h" +#include "Emu/FS/vfsDir.h" + +#include "Emu/SysCalls/lv2/sys_fs.h" +#include "cellFs.h" + +extern Module cellFs; + +struct CellFsDirectoryEntry +{ + CellFsStat attribute; + CellFsDirent entry_name; +}; + +struct FsRingBufferConfig +{ + CellFsRingBuffer m_ring_buffer; // Currently unused after assignment + u32 m_buffer; + u64 m_fs_status; + u64 m_regid; + u32 m_alloc_mem_size; + u32 m_current_addr; + + FsRingBufferConfig() + : m_fs_status(CELL_FS_ST_NOT_INITIALIZED) + , m_regid(0) + , m_alloc_mem_size(0) + , m_current_addr(0) + , m_ring_buffer() { } + +} fs_config; + + +s32 cellFsOpen(vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) +{ + cellFs.Warning("cellFsOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx) -> sys_fs_open()", path, flags, fd, arg, size); + + // TODO + + // call the syscall + return sys_fs_open(path, flags, fd, 0, arg, size); +} + +s32 cellFsRead(PPUThread& CPU, u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread) +{ + cellFs.Log("cellFsRead(fd=0x%x, buf=0x%x, nbytes=0x%llx, nread=0x%x)", fd, buf, nbytes, nread); + + // call the syscall + return sys_fs_read(fd, buf, nbytes, nread ? nread : vm::stackvar>(CPU)); +} + +s32 cellFsWrite(PPUThread& CPU, u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite) +{ + cellFs.Log("cellFsWrite(fd=0x%x, buf=*0x%x, nbytes=0x%llx, nwrite=*0x%x)", fd, buf, nbytes, nwrite); + + // call the syscall + return sys_fs_write(fd, buf, nbytes, nwrite ? nwrite : vm::stackvar>(CPU)); +} + +s32 cellFsClose(u32 fd) +{ + cellFs.Log("cellFsClose(fd=0x%x)", fd); + + // call the syscall + return sys_fs_close(fd); +} + +s32 cellFsOpendir(vm::ptr path, vm::ptr fd) +{ + cellFs.Warning("cellFsOpendir(path=*0x%x, fd=*0x%x) -> sys_fs_opendir()", path, fd); + + // TODO + + // call the syscall + return sys_fs_opendir(path, fd); +} + +s32 cellFsReaddir(u32 fd, vm::ptr dir, vm::ptr nread) +{ + cellFs.Log("cellFsReaddir(fd=0x%x, dir=*0x%x, nread=*0x%x)", fd, dir, nread); + + // call the syscall + return dir && nread ? sys_fs_readdir(fd, dir, nread) : CELL_EFAULT; +} + +s32 cellFsClosedir(u32 fd) +{ + cellFs.Log("cellFsClosedir(fd=0x%x)", fd); + + // call the syscall + return sys_fs_closedir(fd); +} + +s32 cellFsStat(vm::ptr path, vm::ptr sb) +{ + cellFs.Warning("cellFsStat(path=*0x%x, sb=*0x%x) -> sys_fs_stat()", path, sb); + + // TODO + + // call the syscall + return sys_fs_stat(path, sb); +} + +s32 cellFsFstat(u32 fd, vm::ptr sb) +{ + cellFs.Log("cellFsFstat(fd=0x%x, sb=*0x%x)", fd, sb); + + // call the syscall + return sys_fs_fstat(fd, sb); +} + +s32 cellFsMkdir(vm::ptr path, CellFsMode mode) +{ + cellFs.Warning("cellFsMkdir(path=*0x%x, mode=%d) -> sys_fs_mkdir()", path, mode); + + // TODO + + // call the syscall + return sys_fs_mkdir(path, mode); +} + +s32 cellFsRename(vm::ptr from, vm::ptr to) +{ + cellFs.Warning("cellFsRename(from=*0x%x, to=*0x%x) -> sys_fs_rename()", from, to); + + // TODO + + // call the syscall + return sys_fs_rename(from, to); +} + +s32 cellFsRmdir(vm::ptr path) +{ + cellFs.Warning("cellFsRmdir(path=*0x%x) -> sys_fs_rmdir()", path); + + // TODO + + // call the syscall + return sys_fs_rmdir(path); +} + +s32 cellFsUnlink(vm::ptr path) +{ + cellFs.Warning("cellFsUnlink(path=*0x%x) -> sys_fs_unlink()", path); + + // TODO + + // call the syscall + return sys_fs_unlink(path); +} + +s32 cellFsLseek(u32 fd, s64 offset, u32 whence, vm::ptr pos) +{ + cellFs.Log("cellFsLseek(fd=0x%x, offset=0x%llx, whence=0x%x, pos=*0x%x)", fd, offset, whence, pos); + + // call the syscall + return pos ? sys_fs_lseek(fd, offset, whence, pos) : CELL_EFAULT; +} + +s32 cellFsFsync(u32 fd) +{ + cellFs.Todo("cellFsFsync(fd=0x%x)", fd); + + return CELL_OK; +} + +s32 cellFsFGetBlockSize(PPUThread& CPU, u32 fd, vm::ptr sector_size, vm::ptr block_size) +{ + cellFs.Log("cellFsFGetBlockSize(fd=0x%x, sector_size=*0x%x, block_size=*0x%x)", fd, sector_size, block_size); + + // call the syscall + return sector_size && block_size ? sys_fs_fget_block_size(fd, sector_size, block_size, vm::stackvar>(CPU), vm::stackvar>(CPU)) : CELL_EFAULT; +} + +s32 cellFsGetBlockSize(PPUThread& CPU, vm::ptr path, vm::ptr sector_size, vm::ptr block_size) +{ + cellFs.Warning("cellFsGetBlockSize(path=*0x%x, sector_size=*0x%x, block_size=*0x%x) -> sys_fs_get_block_size()", path, sector_size, block_size); + + // TODO + + // call the syscall + return sys_fs_get_block_size(path, sector_size, block_size, vm::stackvar>(CPU)); +} + +s32 cellFsTruncate(vm::ptr path, u64 size) +{ + cellFs.Warning("cellFsTruncate(path=*0x%x, size=0x%llx) -> sys_fs_truncate()", path, size); + + // TODO + + // call the syscall + return sys_fs_truncate(path, size); +} + +s32 cellFsFtruncate(u32 fd, u64 size) +{ + cellFs.Log("cellFsFtruncate(fd=0x%x, size=0x%llx)", fd, size); + + // call the syscall + return sys_fs_ftruncate(fd, size); +} + +s32 cellFsChmod(vm::ptr path, CellFsMode mode) +{ + cellFs.Warning("cellFsChmod(path=*0x%x, mode=%d) -> sys_fs_chmod()", path, mode); + + // TODO + + // call the syscall + return sys_fs_chmod(path, mode); +} + +s32 cellFsGetFreeSize(vm::ptr path, vm::ptr block_size, vm::ptr block_count) +{ + cellFs.Warning("cellFsGetFreeSize(path=*0x%x, block_size=*0x%x, block_count=*0x%x)", path, block_size, block_count); + cellFs.Warning("*** path = '%s'", path.get_ptr()); + + // TODO: Get real values. Currently, it always returns 40 GB of free space divided in 4 KB blocks + *block_size = 4096; // ? + *block_count = 10 * 1024 * 1024; // ? + + return CELL_OK; +} + +s32 cellFsGetDirectoryEntries(u32 fd, vm::ptr entries, u32 entries_size, vm::ptr data_count) +{ + cellFs.Warning("cellFsGetDirectoryEntries(fd=0x%x, entries=*0x%x, entries_size=0x%x, data_count=*0x%x)", fd, entries, entries_size, data_count); + + std::shared_ptr directory; + if (!Emu.GetIdManager().GetIDData(fd, directory)) + return CELL_ESRCH; + + const DirEntryInfo* info = directory->Read(); + if (info) + { + entries->attribute.mode = + CELL_FS_S_IRUSR | CELL_FS_S_IWUSR | CELL_FS_S_IXUSR | + CELL_FS_S_IRGRP | CELL_FS_S_IWGRP | CELL_FS_S_IXGRP | + CELL_FS_S_IROTH | CELL_FS_S_IWOTH | CELL_FS_S_IXOTH; + + entries->attribute.uid = 0; + entries->attribute.gid = 0; + entries->attribute.atime = 0; //TODO + entries->attribute.mtime = 0; //TODO + entries->attribute.ctime = 0; //TODO + entries->attribute.blksize = 4096; + + entries->entry_name.d_type = (info->flags & DirEntry_TypeFile) ? CELL_FS_TYPE_REGULAR : CELL_FS_TYPE_DIRECTORY; + entries->entry_name.d_namlen = u8(std::min(info->name.length(), CELL_FS_MAX_FS_FILE_NAME_LENGTH)); + strcpy_trunc(entries->entry_name.d_name, info->name); + *data_count = 1; + } + else + { + *data_count = 0; + } + + return CELL_OK; +} + +s32 cellFsStReadInit(u32 fd, vm::ptr ringbuf) +{ + cellFs.Warning("cellFsStReadInit(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + fs_config.m_ring_buffer = *ringbuf; + + // If the size is less than 1MB + if (ringbuf->ringbuf_size < 0x40000000) + fs_config.m_alloc_mem_size = (((u32)ringbuf->ringbuf_size + 64 * 1024 - 1) / (64 * 1024)) * (64 * 1024); + else + fs_config.m_alloc_mem_size = (((u32)ringbuf->ringbuf_size + 1024 * 1024 - 1) / (1024 * 1024)) * (1024 * 1024); + + // alloc memory + fs_config.m_buffer = (u32)Memory.Alloc(fs_config.m_alloc_mem_size, 1024); + memset(vm::get_ptr(fs_config.m_buffer), 0, fs_config.m_alloc_mem_size); + + fs_config.m_fs_status = CELL_FS_ST_INITIALIZED; + + return CELL_OK; +} + +s32 cellFsStReadFinish(u32 fd) +{ + cellFs.Warning("cellFsStReadFinish(fd=0x%x)", fd); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + Memory.Free(fs_config.m_buffer); + fs_config.m_fs_status = CELL_FS_ST_NOT_INITIALIZED; + + return CELL_OK; +} + +s32 cellFsStReadGetRingBuf(u32 fd, vm::ptr ringbuf) +{ + cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + *ringbuf = fs_config.m_ring_buffer; + + cellFs.Warning("*** fs stream config: block_size=0x%llx, copy=0x%x, ringbuf_size=0x%llx, transfer_rate=0x%llx", + ringbuf->block_size, ringbuf->copy, ringbuf->ringbuf_size, ringbuf->transfer_rate); + return CELL_OK; +} + +s32 cellFsStReadGetStatus(u32 fd, vm::ptr status) +{ + cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, status=*0x%x)", fd, status); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + *status = fs_config.m_fs_status; + + return CELL_OK; +} + +s32 cellFsStReadGetRegid(u32 fd, vm::ptr regid) +{ + cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, regid=*0x%x)", fd, regid); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + *regid = fs_config.m_regid; + + return CELL_OK; +} + +s32 cellFsStReadStart(u32 fd, u64 offset, u64 size) +{ + cellFs.Todo("cellFsStReadStart(fd=0x%x, offset=0x%llx, size=0x%llx)", fd, offset, size); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + fs_config.m_current_addr = fs_config.m_buffer + (u32)offset; + fs_config.m_fs_status = CELL_FS_ST_PROGRESS; + + return CELL_OK; +} + +s32 cellFsStReadStop(u32 fd) +{ + cellFs.Warning("cellFsStReadStop(fd=0x%x)", fd); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + fs_config.m_fs_status = CELL_FS_ST_STOP; + + return CELL_OK; +} + +s32 cellFsStRead(u32 fd, vm::ptr buf, u64 size, vm::ptr rsize) +{ + cellFs.Warning("cellFsStRead(fd=0x%x, buf=*0x%x, size=0x%llx, rsize=*0x%x)", fd, buf, size, rsize); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + // TODO: use ringbuffer (fs_config) + fs_config.m_regid += size; + + if (file->Eof()) + return CELL_FS_ERANGE; + + *rsize = file->Read(buf.get_ptr(), size); + + return CELL_OK; +} + +s32 cellFsStReadGetCurrentAddr(u32 fd, vm::ptr> addr, vm::ptr size) +{ + cellFs.Todo("cellFsStReadGetCurrentAddr(fd=0x%x, addr=*0x%x, size=*0x%x)", fd, addr, size); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + return CELL_OK; +} + +s32 cellFsStReadPutCurrentAddr(u32 fd, vm::ptr addr, u64 size) +{ + cellFs.Todo("cellFsStReadPutCurrentAddr(fd=0x%x, addr=*0x%x, size=0x%llx)", fd, addr, size); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + return CELL_OK; +} + +s32 cellFsStReadWait(u32 fd, u64 size) +{ + cellFs.Todo("cellFsStReadWait(fd=0x%x, size=0x%llx)", fd, size); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + return CELL_OK; +} + +s32 cellFsStReadWaitCallback(u32 fd, u64 size, vm::ptr func) +{ + cellFs.Todo("cellFsStReadWaitCallback(fd=0x%x, size=0x%llx, func=*0x%x)", fd, size, func); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + return CELL_OK; +} + +bool sdata_check(u32 version, u32 flags, u64 filesizeInput, u64 filesizeTmp) +{ + if (version > 4 || flags & 0x7EFFFFC0){ + printf("ERROR: unknown version"); + return false; + } + + if ((version == 1 && (flags & 0x7FFFFFFE)) || + (version == 2 && (flags & 0x7EFFFFC0))){ + printf("ERROR: unknown or unsupported type"); + return false; + } + + if (filesizeTmp > filesizeInput){ + printf("ERROR: input file size is too short."); + return false; + } + + if (!(flags & 0x80000000)){ + printf("ERROR: cannot extract finalized edata."); + return false; + } + + return true; +} + +int sdata_unpack(const std::string& packed_file, const std::string& unpacked_file) +{ + std::shared_ptr packed_stream(Emu.GetVFS().OpenFile(packed_file, vfsRead)); + std::shared_ptr unpacked_stream(Emu.GetVFS().OpenFile(unpacked_file, vfsWrite)); + + if (!packed_stream || !packed_stream->IsOpened()) + { + cellFs.Error("'%s' not found! flags: 0x%02x", packed_file.c_str(), vfsRead); + return CELL_ENOENT; + } + + if (!unpacked_stream || !unpacked_stream->IsOpened()) + { + cellFs.Error("'%s' couldn't be created! flags: 0x%02x", unpacked_file.c_str(), vfsWrite); + return CELL_ENOENT; + } + + char buffer[10200]; + packed_stream->Read(buffer, 256); + u32 format = re32(*(u32*)&buffer[0]); + if (format != 0x4E504400) // "NPD\x00" + { + cellFs.Error("Illegal format. Expected 0x4E504400, but got 0x%08x", format); + return CELL_EFSSPECIFIC; + } + + u32 version = re32(*(u32*)&buffer[0x04]); + u32 flags = re32(*(u32*)&buffer[0x80]); + u32 blockSize = re32(*(u32*)&buffer[0x84]); + u64 filesizeOutput = re64(*(u64*)&buffer[0x88]); + u64 filesizeInput = packed_stream->GetSize(); + u32 blockCount = (u32)((filesizeOutput + blockSize - 1) / blockSize); + + // SDATA file is compressed + if (flags & 0x1) + { + cellFs.Warning("cellFsSdataOpen: Compressed SDATA files are not supported yet."); + return CELL_EFSSPECIFIC; + } + + // SDATA file is NOT compressed + else + { + u32 t1 = (flags & 0x20) ? 0x20 : 0x10; + u32 startOffset = (blockCount * t1) + 0x100; + u64 filesizeTmp = (filesizeOutput + 0xF) & 0xFFFFFFF0 + startOffset; + + if (!sdata_check(version, flags, filesizeInput, filesizeTmp)) + { + cellFs.Error("cellFsSdataOpen: Wrong header information."); + return CELL_EFSSPECIFIC; + } + + if (flags & 0x20) + packed_stream->Seek(0x100); + else + packed_stream->Seek(startOffset); + + for (u32 i = 0; i < blockCount; i++) + { + if (flags & 0x20) + packed_stream->Seek(packed_stream->Tell() + t1); + + if (!(blockCount - i - 1)) + blockSize = (u32)(filesizeOutput - i * blockSize); + + packed_stream->Read(buffer + 256, blockSize); + unpacked_stream->Write(buffer + 256, blockSize); + } + } + + return CELL_OK; +} + +s32 cellFsSdataOpen(vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) +{ + cellFs.Warning("cellFsSdataOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx) -> sys_fs_open()", path, flags, fd, arg, size); + + /*if (flags != CELL_O_RDONLY) + return CELL_EINVAL; + + std::string suffix = path.substr(path.length() - 5, 5); + if (suffix != ".sdat" && suffix != ".SDAT") + return CELL_ENOTSDATA; + + std::string::size_type last_slash = path.rfind('/'); //TODO: use a filesystem library to solve this more robustly + last_slash = last_slash == std::string::npos ? 0 : last_slash+1; + std::string unpacked_path = "/dev_hdd1/"+path.substr(last_slash,path.length()-last_slash)+".unpacked"; + int ret = sdata_unpack(path, unpacked_path); + if (ret) return ret; + + fd = Emu.GetIdManager().GetNewID(Emu.GetVFS().OpenFile(unpacked_path, vfsRead), TYPE_FS_FILE); + + return CELL_OK;*/ + + return sys_fs_open(path, flags, fd, 0, arg, size); +} + +s32 cellFsSdataOpenByFd(u32 mself_fd, s32 flags, vm::ptr sdata_fd, u64 offset, vm::ptr arg, u64 size) +{ + cellFs.Todo("cellFsSdataOpenByFd(mself_fd=0x%x, flags=0x%x, sdata_fd=*0x%x, offset=0x%llx, arg=*0x%x, size=0x%llx)", mself_fd, flags, sdata_fd, offset, arg, size); + + // TODO: + + return CELL_OK; +} + +std::atomic g_FsAioReadID(0); +std::atomic g_FsAioReadCur(0); +bool aio_init = false; + +void fsAioRead(u32 fd, vm::ptr aio, int xid, vm::ptr xaio, int error, int xid, u64 size)> func) +{ + while (g_FsAioReadCur != xid) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack + if (Emu.IsStopped()) + { + cellFs.Warning("fsAioRead() aborted"); + return; + } + } + + u32 error = CELL_OK; + u64 res = 0; + { + std::shared_ptr orig_file; + if (!Emu.GetIdManager().GetIDData(fd, orig_file)) + { + cellFs.Error("Wrong fd (%s)", fd); + Emu.Pause(); + return; + } + + u64 nbytes = aio->size; + + vfsStream& file = *orig_file; + const u64 old_pos = file.Tell(); + file.Seek((u64)aio->offset); + + if (nbytes != (u32)nbytes) + { + error = CELL_ENOMEM; + } + else + { + res = nbytes ? file.Read(aio->buf.get_ptr(), nbytes) : 0; + } + + file.Seek(old_pos); + + cellFs.Log("*** fsAioRead(fd=%d, offset=0x%llx, buf=0x%x, size=0x%llx, error=0x%x, res=0x%llx, xid=0x%x)", + fd, aio->offset, aio->buf, aio->size, error, res, xid); + } + + if (func) + { + Emu.GetCallbackManager().Async([func, aio, error, xid, res](PPUThread& CPU) + { + func(CPU, aio, error, xid, res); + }); + } + + g_FsAioReadCur++; +} + +s32 cellFsAioRead(vm::ptr aio, vm::ptr id, vm::ptr xaio, s32 error, s32 xid, u64 size)> func) +{ + cellFs.Warning("cellFsAioRead(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); + + if (!aio_init) + { + return CELL_ENXIO; + } + + std::shared_ptr orig_file; + u32 fd = aio->fd; + + if (!Emu.GetIdManager().GetIDData(fd, orig_file)) + { + return CELL_EBADF; + } + + //get a unique id for the callback (may be used by cellFsAioCancel) + const s32 xid = g_FsAioReadID++; + *id = xid; + + thread_t t("CellFsAio Reading Thread", std::bind(fsAioRead, fd, aio, xid, func)); + return CELL_OK; +} + +s32 cellFsAioWrite(vm::ptr aio, vm::ptr id, vm::ptr xaio, s32 error, s32 xid, u64 size)> func) +{ + cellFs.Todo("cellFsAioWrite(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); + + // TODO: + + return CELL_OK; +} + +s32 cellFsAioInit(vm::ptr mount_point) +{ + cellFs.Warning("cellFsAioInit(mount_point=*0x%x)", mount_point); + cellFs.Warning("*** mount_point = '%s'", mount_point.get_ptr()); + + aio_init = true; + return CELL_OK; +} + +s32 cellFsAioFinish(vm::ptr mount_point) +{ + cellFs.Warning("cellFsAioFinish(mount_point=*0x%x)", mount_point); + cellFs.Warning("*** mount_point = '%s'", mount_point.get_ptr()); + + //aio_init = false; + return CELL_OK; +} + +s32 cellFsReadWithOffset(PPUThread& CPU, u32 fd, u64 offset, vm::ptr buf, u64 buffer_size, vm::ptr nread) +{ + cellFs.Warning("cellFsReadWithOffset(fd=%d, offset=0x%llx, buf=*0x%x, buffer_size=%lld, nread=*0x%llx)", fd, offset, buf, buffer_size, nread); + + int ret; + vm::stackvar> oldPos(CPU), newPos(CPU); + ret = cellFsLseek(fd, 0, CELL_FS_SEEK_CUR, oldPos); // Save the current position + if (ret) return ret; + ret = cellFsLseek(fd, offset, CELL_FS_SEEK_SET, newPos); // Move to the specified offset + if (ret) return ret; + ret = cellFsRead(CPU, fd, buf, buffer_size, nread); // Read the file + if (ret) return ret; + ret = cellFsLseek(fd, oldPos.value(), CELL_FS_SEEK_SET, newPos); // Return to the old position + if (ret) return ret; + + return CELL_OK; +} + +s32 cellFsSetDefaultContainer(u32 id, u32 total_limit) +{ + cellFs.Todo("cellFsSetDefaultContainer(id=%d, total_limit=%d)", id, total_limit); + + return CELL_OK; +} + +s32 cellFsSetIoBufferFromDefaultContainer(u32 fd, u32 buffer_size, u32 page_type) +{ + cellFs.Todo("cellFsSetIoBufferFromDefaultContainer(fd=%d, buffer_size=%d, page_type=%d)", fd, buffer_size, page_type); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + return CELL_OK; +} + +Module cellFs("cellFs", []() +{ + g_FsAioReadID = 0; + g_FsAioReadCur = 0; + aio_init = false; + + REG_FUNC(cellFs, cellFsOpen); + REG_FUNC(cellFs, cellFsSdataOpen); + REG_FUNC(cellFs, cellFsSdataOpenByFd); + REG_FUNC(cellFs, cellFsRead); + REG_FUNC(cellFs, cellFsWrite); + REG_FUNC(cellFs, cellFsClose); + REG_FUNC(cellFs, cellFsOpendir); + REG_FUNC(cellFs, cellFsReaddir); + REG_FUNC(cellFs, cellFsClosedir); + REG_FUNC(cellFs, cellFsStat); + REG_FUNC(cellFs, cellFsFstat); + REG_FUNC(cellFs, cellFsMkdir); + REG_FUNC(cellFs, cellFsRename); + REG_FUNC(cellFs, cellFsChmod); + REG_FUNC(cellFs, cellFsFsync); + REG_FUNC(cellFs, cellFsRmdir); + REG_FUNC(cellFs, cellFsUnlink); + REG_FUNC(cellFs, cellFsLseek); + REG_FUNC(cellFs, cellFsFtruncate); + REG_FUNC(cellFs, cellFsTruncate); + REG_FUNC(cellFs, cellFsFGetBlockSize); + REG_FUNC(cellFs, cellFsAioRead); + REG_FUNC(cellFs, cellFsAioWrite); + REG_FUNC(cellFs, cellFsAioInit); + REG_FUNC(cellFs, cellFsAioFinish); + REG_FUNC(cellFs, cellFsGetBlockSize); + REG_FUNC(cellFs, cellFsGetFreeSize); + REG_FUNC(cellFs, cellFsReadWithOffset); + REG_FUNC(cellFs, cellFsGetDirectoryEntries); + REG_FUNC(cellFs, cellFsStReadInit); + REG_FUNC(cellFs, cellFsStReadFinish); + REG_FUNC(cellFs, cellFsStReadGetRingBuf); + REG_FUNC(cellFs, cellFsStReadGetStatus); + REG_FUNC(cellFs, cellFsStReadGetRegid); + REG_FUNC(cellFs, cellFsStReadStart); + REG_FUNC(cellFs, cellFsStReadStop); + REG_FUNC(cellFs, cellFsStRead); + REG_FUNC(cellFs, cellFsStReadGetCurrentAddr); + REG_FUNC(cellFs, cellFsStReadPutCurrentAddr); + REG_FUNC(cellFs, cellFsStReadWait); + REG_FUNC(cellFs, cellFsStReadWaitCallback); + REG_FUNC(cellFs, cellFsSetDefaultContainer); + REG_FUNC(cellFs, cellFsSetIoBufferFromDefaultContainer); +}); diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.h b/rpcs3/Emu/SysCalls/Modules/cellFs.h new file mode 100644 index 0000000000..d45512d5ff --- /dev/null +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.h @@ -0,0 +1,40 @@ +#pragma once + +// CellFsRingBuffer.copy +enum : s32 +{ + CELL_FS_ST_COPY = 0, + CELL_FS_ST_COPYLESS = 1, +}; + +struct CellFsRingBuffer +{ + be_t ringbuf_size; + be_t block_size; + be_t transfer_rate; + be_t copy; +}; + +// cellFsSt(Read|Write)GetStatus status +enum : u64 +{ + CELL_FS_ST_INITIALIZED = 0x0001, + CELL_FS_ST_NOT_INITIALIZED = 0x0002, + CELL_FS_ST_STOP = 0x0100, + CELL_FS_ST_PROGRESS = 0x0200, +}; + +enum : s32 +{ + CELL_FS_AIO_MAX_FS = 10, // cellFsAioInit limit + CELL_FS_AIO_MAX_REQUEST = 32, // cellFsAioRead request limit per mount point +}; + +struct CellFsAio +{ + be_t fd; + be_t offset; + vm::bptr buf; + be_t size; + be_t user_data; +}; diff --git a/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp index cbcb8f397f..28d570fbfe 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp @@ -6,7 +6,10 @@ #include "stblib/stb_image.h" #include "stblib/stb_image.c" // (TODO: Should we put this elsewhere?) -#include "Emu/SysCalls/lv2/cellFs.h" + +#include "Emu/FS/VFS.h" +#include "Emu/FS/vfsFileBase.h" + #include "cellGifDec.h" extern Module cellGifDec; @@ -39,19 +42,15 @@ int cellGifDecOpen(u32 mainHandle, vm::ptr subHandle, vm::ptr> fd; - int ret = cellFsOpen(src->fileName, 0, fd, vm::ptr::make(0), 0); - current_subHandle->fd = fd.value(); - if (ret != CELL_OK) return CELL_GIFDEC_ERROR_OPEN_FILE; - - // Get size of file - vm::var sb; // Alloc a CellFsStat struct - ret = cellFsFstat(current_subHandle->fd, sb); - if (ret != CELL_OK) return ret; - current_subHandle->fileSize = sb->st_size; // Get CellFsStat.st_size + { + // Get file descriptor and size + std::shared_ptr file(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); + if (!file) return CELL_GIFDEC_ERROR_OPEN_FILE; + current_subHandle->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); + current_subHandle->fileSize = file->GetSize(); break; } + } // From now, every u32 subHandle argument is a pointer to a CellGifDecSubHandle struct. *subHandle = Emu.GetIdManager().GetNewID(current_subHandle); @@ -74,7 +73,6 @@ int cellGifDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr //Write the header to buffer vm::var buffer; // Alloc buffer for GIF header - vm::var> pos, nread; switch(subHandle_data->src.srcSelect.data()) { @@ -83,10 +81,13 @@ int cellGifDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr break; case se32(CELL_GIFDEC_FILE): - cellFsLseek(fd, 0, CELL_SEEK_SET, pos); - cellFsRead(fd, vm::ptr::make(buffer.addr()), buffer.size(), nread); + { + auto file = Emu.GetIdManager().GetIDData(fd); + file->Seek(0); + file->Read(buffer.begin(), buffer.size()); break; } + } if (*buffer.To>(0) != 0x47494638 || (*buffer.To(4) != 0x6139 && *buffer.To(4) != 0x6137)) // Error: The first 6 bytes are not a valid GIF signature @@ -156,7 +157,6 @@ int cellGifDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr data, vm::pt //Copy the GIF file to a buffer vm::var gif((u32)fileSize); - vm::var> pos, nread; switch(subHandle_data->src.srcSelect.data()) { @@ -165,10 +165,13 @@ int cellGifDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr data, vm::pt break; case se32(CELL_GIFDEC_FILE): - cellFsLseek(fd, 0, CELL_SEEK_SET, pos); - cellFsRead(fd, vm::ptr::make(gif.addr()), gif.size(), nread); + { + auto file = Emu.GetIdManager().GetIDData(fd); + file->Seek(0); + file->Read(gif.ptr(), gif.size()); break; } + } //Decode GIF file. (TODO: Is there any faster alternative? Can we do it without external libraries?) int width, height, actual_components; @@ -265,7 +268,7 @@ int cellGifDecClose(u32 mainHandle, u32 subHandle) if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data)) return CELL_GIFDEC_ERROR_FATAL; - cellFsClose(subHandle_data->fd); + Emu.GetIdManager().RemoveID(subHandle_data->fd); Emu.GetIdManager().RemoveID(subHandle); return CELL_OK; diff --git a/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp index 322b763d9d..62ac42645f 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp @@ -5,7 +5,10 @@ #include "Emu/SysCalls/Modules.h" #include "stblib/stb_image.h" -#include "Emu/SysCalls/lv2/cellFs.h" + +#include "Emu/FS/VFS.h" +#include "Emu/FS/vfsFileBase.h" + #include "cellJpgDec.h" extern Module cellJpgDec; @@ -45,19 +48,15 @@ int cellJpgDecOpen(u32 mainHandle, vm::ptr subHandle, vm::ptr> fd; - int ret = cellFsOpen(src->fileName, 0, fd, vm::ptr::make(0), 0); - current_subHandle->fd = fd.value(); - if (ret != CELL_OK) return CELL_JPGDEC_ERROR_OPEN_FILE; - - // Get size of file - vm::var sb; // Alloc a CellFsStat struct - ret = cellFsFstat(current_subHandle->fd, sb); - if (ret != CELL_OK) return ret; - current_subHandle->fileSize = sb->st_size; // Get CellFsStat.st_size + { + // Get file descriptor and size + std::shared_ptr file(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); + if (!file) return CELL_JPGDEC_ERROR_OPEN_FILE; + current_subHandle->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); + current_subHandle->fileSize = file->GetSize(); break; } + } // From now, every u32 subHandle argument is a pointer to a CellJpgDecSubHandle struct. *subHandle = Emu.GetIdManager().GetNewID(current_subHandle); @@ -74,7 +73,7 @@ int cellJpgDecClose(u32 mainHandle, u32 subHandle) if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data)) return CELL_JPGDEC_ERROR_FATAL; - cellFsClose(subHandle_data->fd); + Emu.GetIdManager().RemoveID(subHandle_data->fd); Emu.GetIdManager().RemoveID(subHandle); return CELL_OK; @@ -94,7 +93,6 @@ int cellJpgDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr //Write the header to buffer vm::var buffer((u32)fileSize); - vm::var> pos, nread; switch(subHandle_data->src.srcSelect.data()) { @@ -103,10 +101,13 @@ int cellJpgDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr break; case se32(CELL_JPGDEC_FILE): - cellFsLseek(fd, 0, CELL_SEEK_SET, pos); - cellFsRead(fd, vm::ptr::make(buffer.addr()), buffer.size(), nread); + { + auto file = Emu.GetIdManager().GetIDData(fd); + file->Seek(0); + file->Read(buffer.ptr(), buffer.size()); break; } + } if (*buffer.To(0) != 0xE0FFD8FF || // Error: Not a valid SOI header *buffer.To(6) != 0x4649464A) // Error: Not a valid JFIF string @@ -163,7 +164,6 @@ int cellJpgDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr data, vm::pt //Copy the JPG file to a buffer vm::var jpg((u32)fileSize); - vm::var> pos, nread; switch(subHandle_data->src.srcSelect.data()) { @@ -172,10 +172,13 @@ int cellJpgDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr data, vm::pt break; case se32(CELL_JPGDEC_FILE): - cellFsLseek(fd, 0, CELL_SEEK_SET, pos); - cellFsRead(fd, vm::ptr::make(jpg.addr()), jpg.size(), nread); + { + auto file = Emu.GetIdManager().GetIDData(fd); + file->Seek(0); + file->Read(jpg.ptr(), jpg.size()); break; } + } //Decode JPG file. (TODO: Is there any faster alternative? Can we do it without external libraries?) int width, height, actual_components; diff --git a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp index 9e10fdf8dc..6020324200 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp @@ -1,12 +1,15 @@ #include "stdafx.h" #include "Emu/Memory/Memory.h" #include "Emu/System.h" +#include "Emu/IdManager.h" #include "Emu/SysCalls/Modules.h" #include "stblib/stb_image.h" -#include "Emu/SysCalls/lv2/cellFs.h" + +#include "Emu/FS/VFS.h" +#include "Emu/FS/vfsFileBase.h" + #include "cellPngDec.h" -#include extern Module cellPngDec; @@ -76,19 +79,15 @@ s32 pngDecOpen( break; case se32(CELL_PNGDEC_FILE): - // Get file descriptor - vm::var> fd; - int ret = cellFsOpen(src->fileName, 0, fd, vm::ptr::make(0), 0); - stream->fd = fd.value(); - if (ret != CELL_OK) return CELL_PNGDEC_ERROR_OPEN_FILE; - - // Get size of file - vm::var sb; // Alloc a CellFsStat struct - ret = cellFsFstat(stream->fd, sb); - if (ret != CELL_OK) return ret; - stream->fileSize = sb->st_size; // Get CellFsStat.st_size + { + // Get file descriptor and size + std::shared_ptr file(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); + if (!file) return CELL_PNGDEC_ERROR_OPEN_FILE; + stream->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); + stream->fileSize = file->GetSize(); break; } + } if (cb) { @@ -111,7 +110,8 @@ s32 pngDecOpen( s32 pngDecClose(CellPngDecSubHandle stream) { - cellFsClose(stream->fd); + Emu.GetIdManager().RemoveID(stream->fd); + if (!Memory.Free(stream.addr())) { return CELL_PNGDEC_ERROR_FATAL; @@ -135,7 +135,6 @@ s32 pngReadHeader( //Write the header to buffer vm::var buffer; // Alloc buffer for PNG header auto buffer_32 = buffer.To>(); - vm::var> pos, nread; switch (stream->src.srcSelect.data()) { @@ -143,10 +142,13 @@ s32 pngReadHeader( memmove(buffer.begin(), stream->src.streamPtr.get_ptr(), buffer.size()); break; case se32(CELL_PNGDEC_FILE): - cellFsLseek(stream->fd, 0, CELL_SEEK_SET, pos); - cellFsRead(stream->fd, vm::ptr::make(buffer.addr()), buffer.size(), nread); + { + auto file = Emu.GetIdManager().GetIDData(stream->fd); + file->Seek(0); + file->Read(buffer, buffer.size()); break; } + } if (buffer_32[0].data() != se32(0x89504E47) || buffer_32[1].data() != se32(0x0D0A1A0A) || // Error: The first 8 bytes are not a valid PNG signature @@ -244,7 +246,6 @@ s32 pngDecodeData( //Copy the PNG file to a buffer vm::var png((u32)fileSize); - vm::var> pos, nread; switch (stream->src.srcSelect.data()) { @@ -253,10 +254,13 @@ s32 pngDecodeData( break; case se32(CELL_PNGDEC_FILE): - cellFsLseek(fd, 0, CELL_SEEK_SET, pos); - cellFsRead(fd, vm::ptr::make(png.addr()), png.size(), nread); + { + auto file = Emu.GetIdManager().GetIDData(stream->fd); + file->Seek(0); + file->Read(png.ptr(), png.size()); break; } + } //Decode PNG file. (TODO: Is there any faster alternative? Can we do it without external libraries?) int width, height, actual_components; diff --git a/rpcs3/Emu/SysCalls/Modules/cellResc.cpp b/rpcs3/Emu/SysCalls/Modules/cellResc.cpp index 0647516044..c280f9374f 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellResc.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellResc.cpp @@ -14,7 +14,7 @@ extern s32 cellVideoOutConfigure(u32 videoOut, vm::ptr handler); extern void cellGcmSetVBlankHandler(vm::ptr handler); -extern s32 cellGcmAddressToOffset(u32 address, vm::ptr> offset); +extern s32 cellGcmAddressToOffset(u32 address, vm::ptr offset); extern s32 cellGcmSetDisplayBuffer(u32 id, u32 offset, u32 pitch, u32 width, u32 height); extern s32 cellGcmSetPrepareFlip(vm::ptr ctx, u32 id); extern s32 cellGcmSetSecondVFrequency(u32 freq); diff --git a/rpcs3/Emu/SysCalls/Modules/cellSpurs.cpp b/rpcs3/Emu/SysCalls/Modules/cellSpurs.cpp index 7b78219c5d..720a9d1251 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellSpurs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellSpurs.cpp @@ -2428,14 +2428,14 @@ s32 cellSpursCreateTask(vm::ptr taskset, vm::ptr taskId, return CELL_SPURS_TASK_ERROR_ALIGN; } - vm::var tmpTaskId; + vm::var> tmpTaskId; auto rc = spursCreateTask(taskset, tmpTaskId, vm::ptr::make(elf_addr), vm::ptr::make(context_addr), context_size, lsPattern, argument); if (rc != CELL_OK) { return rc; } - rc = spursTaskStart(taskset, tmpTaskId); + rc = spursTaskStart(taskset, tmpTaskId->value()); if (rc != CELL_OK) { return rc; diff --git a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp index 9274ccbdad..54ab1e2bb4 100644 --- a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp +++ b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp @@ -32,7 +32,7 @@ u32 g_tls_size; std::array, TLS_MAX> g_tls_owners; -waiter_map_t g_sys_spinlock_wm("sys_spinlock_wm"); // TODO +waiter_map_t g_sys_spinlock_wm("sys_spinlock_wm"); void sys_initialize_tls() { @@ -127,7 +127,7 @@ s32 sys_lwmutex_destroy(PPUThread& CPU, vm::ptr lwmutex) sysPrxForUser.Log("sys_lwmutex_destroy(lwmutex=*0x%x)", lwmutex); // check to prevent recursive locking in the next call - if (lwmutex->lock_var.read_relaxed().owner == CPU.GetId()) + if (lwmutex->owner.read_relaxed() == CPU.GetId()) { return CELL_EBUSY; } @@ -812,7 +812,7 @@ s32 sys_interrupt_thread_disestablish(PPUThread& CPU, u32 ih) { sysPrxForUser.Todo("sys_interrupt_thread_disestablish(ih=%d)", ih); - return _sys_interrupt_thread_disestablish(ih, vm::stackvar(CPU)); + return _sys_interrupt_thread_disestablish(ih, vm::stackvar>(CPU)); } int sys_process_is_stack(u32 p) diff --git a/rpcs3/Emu/SysCalls/SysCalls.cpp b/rpcs3/Emu/SysCalls/SysCalls.cpp index 1def24c74e..39f94edfd2 100644 --- a/rpcs3/Emu/SysCalls/SysCalls.cpp +++ b/rpcs3/Emu/SysCalls/SysCalls.cpp @@ -6,7 +6,6 @@ #include "Emu/System.h" #include "ModuleManager.h" -#include "lv2/cellFs.h" #include "lv2/sleep_queue.h" #include "lv2/sys_lwmutex.h" #include "lv2/sys_lwcond.h" @@ -29,6 +28,7 @@ #include "lv2/sys_trace.h" #include "lv2/sys_tty.h" #include "lv2/sys_vm.h" +#include "lv2/sys_fs.h" #include "Emu/SysCalls/Modules/cellGcmSys.h" @@ -696,28 +696,28 @@ const ppu_func_caller sc_table[1024] = null_func, null_func, null_func, null_func, null_func, //799 UNS null_func,//bind_func(sys_fs_test), //800 (0x320) - bind_func(cellFsOpen), //801 (0x321) - bind_func(cellFsRead), //802 (0x322) - bind_func(cellFsWrite), //803 (0x323) - bind_func(cellFsClose), //804 (0x324) - bind_func(cellFsOpendir), //805 (0x325) - bind_func(cellFsReaddir), //806 (0x326) - bind_func(cellFsClosedir), //807 (0x327) - bind_func(cellFsStat), //808 (0x328) - bind_func(cellFsFstat), //809 (0x329) + bind_func(sys_fs_open), //801 (0x321) + bind_func(sys_fs_read), //802 (0x322) + bind_func(sys_fs_write), //803 (0x323) + bind_func(sys_fs_close), //804 (0x324) + bind_func(sys_fs_opendir), //805 (0x325) + bind_func(sys_fs_readdir), //806 (0x326) + bind_func(sys_fs_closedir), //807 (0x327) + bind_func(sys_fs_stat), //808 (0x328) + bind_func(sys_fs_fstat), //809 (0x329) null_func,//bind_func(sys_fs_link), //810 (0x32A) - bind_func(cellFsMkdir), //811 (0x32B) - bind_func(cellFsRename), //812 (0x32C) - bind_func(cellFsRmdir), //813 (0x32D) - bind_func(cellFsUnlink), //814 (0x32E) - null_func,//bind_func(cellFsUtime), //815 (0x32F) + bind_func(sys_fs_mkdir), //811 (0x32B) + bind_func(sys_fs_rename), //812 (0x32C) + bind_func(sys_fs_rmdir), //813 (0x32D) + bind_func(sys_fs_unlink), //814 (0x32E) + null_func,//bind_func(sys_fs_utime), //815 (0x32F) null_func,//bind_func(sys_fs_access), //816 (0x330) null_func,//bind_func(sys_fs_fcntl), //817 (0x331) - bind_func(cellFsLseek), //818 (0x332) + bind_func(sys_fs_lseek), //818 (0x332) null_func,//bind_func(sys_fs_fdatasync), //819 (0x333) - null_func,//bind_func(cellFsFsync), //820 (0x334) - bind_func(cellFsFGetBlockSize), //821 (0x335) - bind_func(cellFsGetBlockSize), //822 (0x336) + null_func,//bind_func(sys_fs_fsync), //820 (0x334) + bind_func(sys_fs_fget_block_size), //821 (0x335) + bind_func(sys_fs_get_block_size), //822 (0x336) null_func,//bind_func(sys_fs_acl_read), //823 (0x337) null_func,//bind_func(sys_fs_acl_write), //824 (0x338) null_func,//bind_func(sys_fs_lsn_get_cda_size), //825 (0x339) @@ -726,10 +726,10 @@ const ppu_func_caller sc_table[1024] = null_func,//bind_func(sys_fs_lsn_unlock), //828 (0x33C) null_func,//bind_func(sys_fs_lsn_read), //829 (0x33D) null_func,//bind_func(sys_fs_lsn_write), //830 (0x33E) - bind_func(cellFsTruncate), //831 (0x33F) - bind_func(cellFsFtruncate), //832 (0x340) + bind_func(sys_fs_truncate), //831 (0x33F) + bind_func(sys_fs_ftruncate), //832 (0x340) null_func,//bind_func(sys_fs_symbolic_link), //833 (0x341) - null_func,//bind_func(cellFsChmod), //834 (0x342) + bind_func(sys_fs_chmod), //834 (0x342) null_func,//bind_func(sys_fs_chown), //835 (0x343) null_func,//bind_func(sys_fs_newfs), //836 (0x344) null_func,//bind_func(sys_fs_mount), //837 (0x345) diff --git a/rpcs3/Emu/SysCalls/lv2/cellFs.cpp b/rpcs3/Emu/SysCalls/lv2/cellFs.cpp deleted file mode 100644 index 8a332d3674..0000000000 --- a/rpcs3/Emu/SysCalls/lv2/cellFs.cpp +++ /dev/null @@ -1,1091 +0,0 @@ -#include "stdafx.h" -#include "Emu/Memory/Memory.h" -#include "Emu/System.h" -#include "Emu/IdManager.h" -#include "Emu/SysCalls/Modules.h" -#include "Emu/SysCalls/Callback.h" -#include "Emu/SysCalls/CB_FUNC.h" - -#ifdef _WIN32 - #include - #undef CreateFile -#else - #include - #include -#endif - -#include "Emu/FS/VFS.h" -#include "Emu/FS/vfsFile.h" -#include "Emu/FS/vfsDir.h" -#include "cellFs.h" - -extern Module sys_fs; - -struct FsRingBufferConfig -{ - CellFsRingBuffer m_ring_buffer; // Currently unused after assignment - u32 m_buffer; - u64 m_fs_status; - u64 m_regid; - u32 m_alloc_mem_size; - u32 m_current_addr; - - FsRingBufferConfig() - : m_fs_status(CELL_FS_ST_NOT_INITIALIZED) - , m_regid(0) - , m_alloc_mem_size(0) - , m_current_addr(0) - , m_ring_buffer() { } - -} fs_config; - - -s32 cellFsOpen(vm::ptr path, s32 flags, vm::ptr> fd, vm::ptr arg, u64 size) -{ - sys_fs.Log("cellFsOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); - sys_fs.Log("*** path = '%s'", path.get_ptr()); - - const std::string _path = path.get_ptr(); - - s32 _oflags = flags; - if (flags & CELL_O_CREAT) - { - _oflags &= ~CELL_O_CREAT; - Emu.GetVFS().CreateFile(_path); - } - - vfsOpenMode o_mode; - - switch (flags & CELL_O_ACCMODE) - { - case CELL_O_RDONLY: - _oflags &= ~CELL_O_RDONLY; - o_mode = vfsRead; - break; - - case CELL_O_WRONLY: - _oflags &= ~CELL_O_WRONLY; - - if(flags & CELL_O_APPEND) - { - _oflags &= ~CELL_O_APPEND; - o_mode = vfsWriteAppend; - } - else if(flags & CELL_O_EXCL) - { - _oflags &= ~CELL_O_EXCL; - o_mode = vfsWriteExcl; - } - else //if (flags & CELL_O_TRUNC) - { - _oflags &= ~CELL_O_TRUNC; - o_mode = vfsWrite; - } - break; - - case CELL_O_RDWR: - _oflags &= ~CELL_O_RDWR; - if (flags & CELL_O_TRUNC) - { - _oflags &= ~CELL_O_TRUNC; - //truncate file before opening it as read/write - auto filePtr = Emu.GetVFS().OpenFile(_path, vfsWrite); - delete filePtr; - } - o_mode = vfsReadWrite; - break; - } - - if (_oflags != 0) - { - sys_fs.Error("cellFsOpen(): '%s' has unknown flags! flags: 0x%08x", path.get_ptr(), flags); - return CELL_EINVAL; - } - - if (!Emu.GetVFS().ExistsFile(_path)) - { - sys_fs.Error("cellFsOpen(): '%s' not found! flags: 0x%08x", path.get_ptr(), flags); - return CELL_ENOENT; - } - - std::shared_ptr stream((vfsStream*)Emu.GetVFS().OpenFile(_path, o_mode)); - - if (!stream || !stream->IsOpened()) - { - sys_fs.Error("cellFsOpen(): '%s' not found! flags: 0x%08x", path.get_ptr(), flags); - return CELL_ENOENT; - } - - u32 id = Emu.GetIdManager().GetNewID(stream, TYPE_FS_FILE); - *fd = id; - sys_fs.Notice("cellFsOpen(): '%s' opened, id -> 0x%x", path.get_ptr(), id); - - return CELL_OK; -} - -s32 cellFsRead(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr> nread) -{ - sys_fs.Log("cellFsRead(fd=0x%x, buf=0x%x, nbytes=0x%llx, nread=0x%x)", fd, buf, nbytes, nread); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - // TODO: checks - - const u64 res = nbytes ? file->Read(buf.get_ptr(), nbytes) : 0; - - if (nread) *nread = res; - - return CELL_OK; -} - -s32 cellFsWrite(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite) -{ - sys_fs.Log("cellFsWrite(fd=0x%x, buf=*0x%x, nbytes=0x%llx, nwrite=*0x%x)", fd, buf, nbytes, nwrite); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; - - // TODO: checks - - const u64 res = nbytes ? file->Write(buf.get_ptr(), nbytes) : 0; - - if (nwrite) *nwrite = res; - - return CELL_OK; -} - -s32 cellFsClose(u32 fd) -{ - sys_fs.Warning("cellFsClose(fd=0x%x)", fd); - - if (!Emu.GetIdManager().RemoveID(fd)) - return CELL_ESRCH; - - return CELL_OK; -} - -s32 cellFsOpendir(vm::ptr path, vm::ptr fd) -{ - sys_fs.Warning("cellFsOpendir(path=*0x%x, fd=*0x%x)", path, fd); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - std::shared_ptr dir(Emu.GetVFS().OpenDir(path.get_ptr())); - if (!dir || !dir->IsOpened()) - { - return CELL_ENOENT; - } - - *fd = Emu.GetIdManager().GetNewID(dir, TYPE_FS_DIR); - return CELL_OK; -} - -s32 cellFsReaddir(u32 fd, vm::ptr dir, vm::ptr nread) -{ - sys_fs.Warning("cellFsReaddir(fd=0x%x, dir=*0x%x, nread=*0x%x)", fd, dir, nread); - - std::shared_ptr directory; - if (!Emu.GetIdManager().GetIDData(fd, directory)) - return CELL_ESRCH; - - const DirEntryInfo* info = directory->Read(); - if (info) - { - dir->d_type = (info->flags & DirEntry_TypeFile) ? CELL_FS_TYPE_REGULAR : CELL_FS_TYPE_DIRECTORY; - dir->d_namlen = u8(std::min((u32)info->name.length(), (u32)CELL_MAX_FS_FILE_NAME_LENGTH)); - strcpy_trunc(dir->d_name, info->name); - *nread = sizeof(CellFsDirent); - } - else - { - *nread = 0; - } - - return CELL_OK; -} - -s32 cellFsClosedir(u32 fd) -{ - sys_fs.Warning("cellFsClosedir(fd=0x%x)", fd); - - if (!Emu.GetIdManager().RemoveID(fd)) - return CELL_ESRCH; - - return CELL_OK; -} - -s32 cellFsStat(vm::ptr path, vm::ptr sb) -{ - sys_fs.Warning("cellFsStat(path=*0x%x, sb=*0x%x)", path, sb); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - const std::string _path = path.get_ptr(); - - u32 mode = 0; - s32 uid = 0; - s32 gid = 0; - u64 atime = 0; - u64 mtime = 0; - u64 ctime = 0; - u64 size = 0; - - std::string real_path; - - Emu.GetVFS().GetDevice(_path, real_path); - - int stat_result; -#ifdef _WIN32 - struct _stat64 buf; - stat_result = _stat64(real_path.c_str(), &buf); -#else - struct stat buf; - stat_result = stat(real_path.c_str(), &buf); -#endif - if (stat_result) - { - sys_fs.Error("cellFsStat(): stat('%s') failed -> 0x%x", real_path.c_str(), stat_result); - } - else - { - mode = buf.st_mode; - uid = buf.st_uid; - gid = buf.st_gid; - atime = buf.st_atime; - mtime = buf.st_mtime; - ctime = buf.st_ctime; - size = buf.st_size; - } - - sb->st_mode = - CELL_FS_S_IRUSR | CELL_FS_S_IWUSR | CELL_FS_S_IXUSR | - CELL_FS_S_IRGRP | CELL_FS_S_IWGRP | CELL_FS_S_IXGRP | - CELL_FS_S_IROTH | CELL_FS_S_IWOTH | CELL_FS_S_IXOTH; - - if (sb->st_mode == mode) - sys_fs.Error("cellFsStat(): mode is the same (0x%x)", mode); - - sb->st_uid = uid; - sb->st_gid = gid; - sb->st_atime_ = atime; - sb->st_mtime_ = mtime; - sb->st_ctime_ = ctime; - sb->st_blksize = 4096; - - { - vfsDir dir(_path); - if(dir.IsOpened()) - { - sb->st_mode |= CELL_FS_S_IFDIR; - return CELL_OK; - } - } - - { - vfsFile f(_path); - if(f.IsOpened()) - { - sb->st_mode |= CELL_FS_S_IFREG; - sb->st_size = f.GetSize(); - return CELL_OK; - } - } - - if (sb->st_size == size && size != 0) - sys_fs.Error("cellFsStat(): size is the same (0x%x)", size); - - sys_fs.Warning("cellFsStat(): '%s' not found", path.get_ptr()); - return CELL_ENOENT; -} - -s32 cellFsFstat(u32 fd, vm::ptr sb) -{ - sys_fs.Warning("cellFsFstat(fd=0x%x, sb=*0x%x)", fd, sb); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - sb->st_mode = - CELL_FS_S_IRUSR | CELL_FS_S_IWUSR | CELL_FS_S_IXUSR | - CELL_FS_S_IRGRP | CELL_FS_S_IWGRP | CELL_FS_S_IXGRP | - CELL_FS_S_IROTH | CELL_FS_S_IWOTH | CELL_FS_S_IXOTH; - - sb->st_mode |= CELL_FS_S_IFREG; //TODO: dir CELL_FS_S_IFDIR - sb->st_uid = 0; - sb->st_gid = 0; - sb->st_atime_ = 0; //TODO - sb->st_mtime_ = 0; //TODO - sb->st_ctime_ = 0; //TODO - sb->st_size = file->GetSize(); - sb->st_blksize = 4096; - - return CELL_OK; -} - -s32 cellFsMkdir(vm::ptr path, u32 mode) -{ - sys_fs.Warning("cellFsMkdir(path=*0x%x, mode=0x%x)", path, mode); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - const std::string _path = path.get_ptr(); - - if (vfsDir().IsExists(_path)) - return CELL_EEXIST; - - if (!Emu.GetVFS().CreateDir(_path)) - return CELL_EBUSY; - - sys_fs.Notice("cellFsMkdir(): directory '%s' created", path.get_ptr()); - return CELL_OK; -} - -s32 cellFsRename(vm::ptr from, vm::ptr to) -{ - sys_fs.Warning("cellFsRename(from=*0x%x, to=*0x%x)", from, to); - sys_fs.Warning("*** from = '%s'", from.get_ptr()); - sys_fs.Warning("*** to = '%s'", to.get_ptr()); - - std::string _from = from.get_ptr(); - std::string _to = to.get_ptr(); - - { - vfsDir dir; - if(dir.IsExists(_from)) - { - if(!dir.Rename(_from, _to)) - return CELL_EBUSY; - - sys_fs.Notice("cellFsRename(): directory '%s' renamed to '%s'", from.get_ptr(), to.get_ptr()); - return CELL_OK; - } - } - - { - vfsFile f; - - if(f.Exists(_from)) - { - if(!f.Rename(_from, _to)) - return CELL_EBUSY; - - sys_fs.Notice("cellFsRename(): file '%s' renamed to '%s'", from.get_ptr(), to.get_ptr()); - return CELL_OK; - } - } - - return CELL_ENOENT; -} -s32 cellFsChmod(vm::ptr path, u32 mode) -{ - sys_fs.Todo("cellFsChmod(path=*0x%x, mode=0x%x)", path, mode); - sys_fs.Todo("*** path = '%s'", path.get_ptr()); - - // TODO: - - return CELL_OK; -} - -s32 cellFsFsync(u32 fd) -{ - sys_fs.Todo("cellFsFsync(fd=0x%x)", fd); - - // TODO: - - return CELL_OK; -} - -s32 cellFsRmdir(vm::ptr path) -{ - sys_fs.Warning("cellFsRmdir(path=*0x%x)", path); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - std::string _path = path.get_ptr(); - - vfsDir d; - if (!d.IsExists(_path)) - return CELL_ENOENT; - - if (!d.Remove(_path)) - return CELL_EBUSY; - - sys_fs.Notice("cellFsRmdir(): directory '%s' removed", path.get_ptr()); - return CELL_OK; -} - -s32 cellFsUnlink(vm::ptr path) -{ - sys_fs.Warning("cellFsUnlink(path=*0x%x)", path); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - std::string _path = path.get_ptr(); - - if (vfsDir().IsExists(_path)) - return CELL_EISDIR; - - if (!Emu.GetVFS().ExistsFile(_path)) - return CELL_ENOENT; - - if (!Emu.GetVFS().RemoveFile(_path)) - return CELL_EACCES; - - sys_fs.Notice("cellFsUnlink(): file '%s' removed", path.get_ptr()); - return CELL_OK; -} - -s32 cellFsLseek(u32 fd, s64 offset, u32 whence, vm::ptr> pos) -{ - sys_fs.Log("cellFsLseek(fd=0x%x, offset=0x%llx, whence=0x%x, pos=*0x%x)", fd, offset, whence, pos); - - vfsSeekMode seek_mode; - switch(whence) - { - case CELL_SEEK_SET: seek_mode = vfsSeekSet; break; - case CELL_SEEK_CUR: seek_mode = vfsSeekCur; break; - case CELL_SEEK_END: seek_mode = vfsSeekEnd; break; - default: - sys_fs.Error("cellFsLseek(fd=0x%x): Unknown seek whence! (0x%x)", fd, whence); - return CELL_EINVAL; - } - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - *pos = file->Seek(offset, seek_mode); - return CELL_OK; -} - -s32 cellFsFtruncate(u32 fd, u64 size) -{ - sys_fs.Warning("cellFsFtruncate(fd=0x%x, size=0x%llx)", fd, size); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - u64 initialSize = file->GetSize(); - - if (initialSize < size) - { - u64 last_pos = file->Tell(); - file->Seek(0, vfsSeekEnd); - static const char nullbyte = 0; - file->Seek(size-initialSize-1, vfsSeekCur); - file->Write(&nullbyte, sizeof(char)); - file->Seek(last_pos, vfsSeekSet); - } - - if (initialSize > size) - { - // (TODO) - } - - return CELL_OK; -} - -s32 cellFsTruncate(vm::ptr path, u64 size) -{ - sys_fs.Warning("cellFsTruncate(path=*0x%x, size=0x%llx)", path, size); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - vfsFile f(path.get_ptr(), vfsReadWrite); - if (!f.IsOpened()) - { - sys_fs.Warning("cellFsTruncate(): '%s' not found", path.get_ptr()); - return CELL_ENOENT; - } - u64 initialSize = f.GetSize(); - - if (initialSize < size) - { - u64 last_pos = f.Tell(); - f.Seek(0, vfsSeekEnd); - static const char nullbyte = 0; - f.Seek(size-initialSize-1, vfsSeekCur); - f.Write(&nullbyte, sizeof(char)); - f.Seek(last_pos, vfsSeekSet); - } - - if (initialSize > size) - { - // (TODO) - } - - return CELL_OK; -} - -s32 cellFsFGetBlockSize(u32 fd, vm::ptr sector_size, vm::ptr block_size) -{ - sys_fs.Warning("cellFsFGetBlockSize(fd=0x%x, sector_size=*0x%x, block_size=*0x%x)", fd, sector_size, block_size); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - *sector_size = 4096; // ? - *block_size = 4096; // ? - - return CELL_OK; -} - -s32 cellFsGetBlockSize(vm::ptr path, vm::ptr sector_size, vm::ptr block_size) -{ - sys_fs.Warning("cellFsGetBlockSize(path=*0x%x, sector_size=*0x%x, block_size=*0x%x)", path, sector_size, block_size); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - *sector_size = 4096; // ? - *block_size = 4096; // ? - - return CELL_OK; -} - -s32 cellFsGetFreeSize(vm::ptr path, vm::ptr block_size, vm::ptr block_count) -{ - sys_fs.Warning("cellFsGetFreeSize(path=*0x%x, block_size=*0x%x, block_count=*0x%x)", path, block_size, block_count); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - // TODO: Get real values. Currently, it always returns 40 GB of free space divided in 4 KB blocks - *block_size = 4096; // ? - *block_count = 10 * 1024 * 1024; // ? - - return CELL_OK; -} - -s32 cellFsGetDirectoryEntries(u32 fd, vm::ptr entries, u32 entries_size, vm::ptr data_count) -{ - sys_fs.Warning("cellFsGetDirectoryEntries(fd=0x%x, entries=*0x%x, entries_size=0x%x, data_count=*0x%x)", fd, entries, entries_size, data_count); - - std::shared_ptr directory; - if (!Emu.GetIdManager().GetIDData(fd, directory)) - return CELL_ESRCH; - - const DirEntryInfo* info = directory->Read(); - if (info) - { - entries->attribute.st_mode = - CELL_FS_S_IRUSR | CELL_FS_S_IWUSR | CELL_FS_S_IXUSR | - CELL_FS_S_IRGRP | CELL_FS_S_IWGRP | CELL_FS_S_IXGRP | - CELL_FS_S_IROTH | CELL_FS_S_IWOTH | CELL_FS_S_IXOTH; - - entries->attribute.st_uid = 0; - entries->attribute.st_gid = 0; - entries->attribute.st_atime_ = 0; //TODO - entries->attribute.st_mtime_ = 0; //TODO - entries->attribute.st_ctime_ = 0; //TODO - entries->attribute.st_blksize = 4096; - - entries->entry_name.d_type = (info->flags & DirEntry_TypeFile) ? CELL_FS_TYPE_REGULAR : CELL_FS_TYPE_DIRECTORY; - entries->entry_name.d_namlen = u8(std::min((u32)info->name.length(), (u32)CELL_MAX_FS_FILE_NAME_LENGTH)); - strcpy_trunc(entries->entry_name.d_name, info->name); - *data_count = 1; - } - else - { - *data_count = 0; - } - - return CELL_OK; -} - -s32 cellFsStReadInit(u32 fd, vm::ptr ringbuf) -{ - sys_fs.Warning("cellFsStReadInit(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - fs_config.m_ring_buffer = *ringbuf; - - // If the size is less than 1MB - if (ringbuf->ringbuf_size < 0x40000000) - fs_config.m_alloc_mem_size = (((u32)ringbuf->ringbuf_size + 64 * 1024 - 1) / (64 * 1024)) * (64 * 1024); - else - fs_config.m_alloc_mem_size = (((u32)ringbuf->ringbuf_size + 1024 * 1024 - 1) / (1024 * 1024)) * (1024 * 1024); - - // alloc memory - fs_config.m_buffer = (u32)Memory.Alloc(fs_config.m_alloc_mem_size, 1024); - memset(vm::get_ptr(fs_config.m_buffer), 0, fs_config.m_alloc_mem_size); - - fs_config.m_fs_status = CELL_FS_ST_INITIALIZED; - - return CELL_OK; -} - -s32 cellFsStReadFinish(u32 fd) -{ - sys_fs.Warning("cellFsStReadFinish(fd=0x%x)", fd); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - Memory.Free(fs_config.m_buffer); - fs_config.m_fs_status = CELL_FS_ST_NOT_INITIALIZED; - - return CELL_OK; -} - -s32 cellFsStReadGetRingBuf(u32 fd, vm::ptr ringbuf) -{ - sys_fs.Warning("cellFsStReadGetRingBuf(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - *ringbuf = fs_config.m_ring_buffer; - - sys_fs.Warning("*** fs stream config: block_size=0x%llx, copy=0x%x, ringbuf_size=0x%llx, transfer_rate=0x%llx", - ringbuf->block_size, ringbuf->copy, ringbuf->ringbuf_size, ringbuf->transfer_rate); - return CELL_OK; -} - -s32 cellFsStReadGetStatus(u32 fd, vm::ptr status) -{ - sys_fs.Warning("cellFsStReadGetRingBuf(fd=0x%x, status=*0x%x)", fd, status); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - *status = fs_config.m_fs_status; - - return CELL_OK; -} - -s32 cellFsStReadGetRegid(u32 fd, vm::ptr regid) -{ - sys_fs.Warning("cellFsStReadGetRingBuf(fd=0x%x, regid=*0x%x)", fd, regid); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - *regid = fs_config.m_regid; - - return CELL_OK; -} - -s32 cellFsStReadStart(u32 fd, u64 offset, u64 size) -{ - sys_fs.Todo("cellFsStReadStart(fd=0x%x, offset=0x%llx, size=0x%llx)", fd, offset, size); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - fs_config.m_current_addr = fs_config.m_buffer + (u32)offset; - fs_config.m_fs_status = CELL_FS_ST_PROGRESS; - - return CELL_OK; -} - -s32 cellFsStReadStop(u32 fd) -{ - sys_fs.Warning("cellFsStReadStop(fd=0x%x)", fd); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - fs_config.m_fs_status = CELL_FS_ST_STOP; - - return CELL_OK; -} - -s32 cellFsStRead(u32 fd, vm::ptr buf, u64 size, vm::ptr rsize) -{ - sys_fs.Warning("cellFsStRead(fd=0x%x, buf=*0x%x, size=0x%llx, rsize=*0x%x)", fd, buf, size, rsize); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - // TODO: use ringbuffer (fs_config) - fs_config.m_regid += size; - - if (file->Eof()) - return CELL_FS_ERANGE; - - *rsize = file->Read(buf.get_ptr(), size); - - return CELL_OK; -} - -s32 cellFsStReadGetCurrentAddr(u32 fd, vm::ptr> addr, vm::ptr size) -{ - sys_fs.Todo("cellFsStReadGetCurrentAddr(fd=0x%x, addr=*0x%x, size=*0x%x)", fd, addr, size); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - return CELL_OK; -} - -s32 cellFsStReadPutCurrentAddr(u32 fd, vm::ptr addr, u64 size) -{ - sys_fs.Todo("cellFsStReadPutCurrentAddr(fd=0x%x, addr=*0x%x, size=0x%llx)", fd, addr, size); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - return CELL_OK; -} - -s32 cellFsStReadWait(u32 fd, u64 size) -{ - sys_fs.Todo("cellFsStReadWait(fd=0x%x, size=0x%llx)", fd, size); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - return CELL_OK; -} - -s32 cellFsStReadWaitCallback(u32 fd, u64 size, vm::ptr func) -{ - sys_fs.Todo("cellFsStReadWaitCallback(fd=0x%x, size=0x%llx, func=*0x%x)", fd, size, func); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - return CELL_OK; -} - -bool sdata_check(u32 version, u32 flags, u64 filesizeInput, u64 filesizeTmp) -{ - if (version > 4 || flags & 0x7EFFFFC0){ - printf("ERROR: unknown version"); - return false; - } - - if ((version == 1 && (flags & 0x7FFFFFFE)) || - (version == 2 && (flags & 0x7EFFFFC0))){ - printf("ERROR: unknown or unsupported type"); - return false; - } - - if (filesizeTmp > filesizeInput){ - printf("ERROR: input file size is too short."); - return false; - } - - if (!(flags & 0x80000000)){ - printf("ERROR: cannot extract finalized edata."); - return false; - } - - return true; -} - -int sdata_unpack(const std::string& packed_file, const std::string& unpacked_file) -{ - std::shared_ptr packed_stream(Emu.GetVFS().OpenFile(packed_file, vfsRead)); - std::shared_ptr unpacked_stream(Emu.GetVFS().OpenFile(unpacked_file, vfsWrite)); - - if (!packed_stream || !packed_stream->IsOpened()) - { - sys_fs.Error("'%s' not found! flags: 0x%02x", packed_file.c_str(), vfsRead); - return CELL_ENOENT; - } - - if (!unpacked_stream || !unpacked_stream->IsOpened()) - { - sys_fs.Error("'%s' couldn't be created! flags: 0x%02x", unpacked_file.c_str(), vfsWrite); - return CELL_ENOENT; - } - - char buffer[10200]; - packed_stream->Read(buffer, 256); - u32 format = re32(*(u32*)&buffer[0]); - if (format != 0x4E504400) // "NPD\x00" - { - sys_fs.Error("Illegal format. Expected 0x4E504400, but got 0x%08x", format); - return CELL_EFSSPECIFIC; - } - - u32 version = re32(*(u32*)&buffer[0x04]); - u32 flags = re32(*(u32*)&buffer[0x80]); - u32 blockSize = re32(*(u32*)&buffer[0x84]); - u64 filesizeOutput = re64(*(u64*)&buffer[0x88]); - u64 filesizeInput = packed_stream->GetSize(); - u32 blockCount = (u32)((filesizeOutput + blockSize - 1) / blockSize); - - // SDATA file is compressed - if (flags & 0x1) - { - sys_fs.Warning("cellFsSdataOpen: Compressed SDATA files are not supported yet."); - return CELL_EFSSPECIFIC; - } - - // SDATA file is NOT compressed - else - { - u32 t1 = (flags & 0x20) ? 0x20 : 0x10; - u32 startOffset = (blockCount * t1) + 0x100; - u64 filesizeTmp = (filesizeOutput + 0xF) & 0xFFFFFFF0 + startOffset; - - if (!sdata_check(version, flags, filesizeInput, filesizeTmp)) - { - sys_fs.Error("cellFsSdataOpen: Wrong header information."); - return CELL_EFSSPECIFIC; - } - - if (flags & 0x20) - packed_stream->Seek(0x100); - else - packed_stream->Seek(startOffset); - - for (u32 i = 0; i < blockCount; i++) - { - if (flags & 0x20) - packed_stream->Seek(packed_stream->Tell() + t1); - - if (!(blockCount - i - 1)) - blockSize = (u32)(filesizeOutput - i * blockSize); - - packed_stream->Read(buffer + 256, blockSize); - unpacked_stream->Write(buffer + 256, blockSize); - } - } - - return CELL_OK; -} - -s32 cellFsSdataOpen(vm::ptr path, s32 flags, vm::ptr> fd, vm::ptr arg, u64 size) -{ - sys_fs.Warning("cellFsSdataOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx) -> cellFsOpen()", path, flags, fd, arg, size); - sys_fs.Warning("*** path = '%s'", path.get_ptr()); - - /*if (flags != CELL_O_RDONLY) - return CELL_EINVAL; - - std::string suffix = path.substr(path.length() - 5, 5); - if (suffix != ".sdat" && suffix != ".SDAT") - return CELL_ENOTSDATA; - - std::string::size_type last_slash = path.rfind('/'); //TODO: use a filesystem library to solve this more robustly - last_slash = last_slash == std::string::npos ? 0 : last_slash+1; - std::string unpacked_path = "/dev_hdd1/"+path.substr(last_slash,path.length()-last_slash)+".unpacked"; - int ret = sdata_unpack(path, unpacked_path); - if (ret) return ret; - - fd = Emu.GetIdManager().GetNewID(Emu.GetVFS().OpenFile(unpacked_path, vfsRead), TYPE_FS_FILE); - - return CELL_OK;*/ - - return cellFsOpen(path, flags, fd, arg, size); -} - -s32 cellFsSdataOpenByFd(u32 mself_fd, s32 flags, vm::ptr sdata_fd, u64 offset, vm::ptr arg, u64 size) -{ - sys_fs.Todo("cellFsSdataOpenByFd(mself_fd=0x%x, flags=0x%x, sdata_fd=*0x%x, offset=0x%llx, arg=*0x%x, size=0x%llx)", mself_fd, flags, sdata_fd, offset, arg, size); - - // TODO: - - return CELL_OK; -} - -std::atomic g_FsAioReadID(0); -std::atomic g_FsAioReadCur(0); -bool aio_init = false; - -void fsAioRead(u32 fd, vm::ptr aio, int xid, vm::ptr xaio, int error, int xid, u64 size)> func) -{ - while (g_FsAioReadCur != xid) - { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack - if (Emu.IsStopped()) - { - sys_fs.Warning("fsAioRead() aborted"); - return; - } - } - - u32 error = CELL_OK; - u64 res = 0; - { - std::shared_ptr orig_file; - if (!Emu.GetIdManager().GetIDData(fd, orig_file)) - { - sys_fs.Error("Wrong fd (%s)", fd); - Emu.Pause(); - return; - } - - u64 nbytes = aio->size; - - vfsStream& file = *orig_file; - const u64 old_pos = file.Tell(); - file.Seek((u64)aio->offset); - - if (nbytes != (u32)nbytes) - { - error = CELL_ENOMEM; - } - else - { - res = nbytes ? file.Read(aio->buf.get_ptr(), nbytes) : 0; - } - - file.Seek(old_pos); - - sys_fs.Log("*** fsAioRead(fd=%d, offset=0x%llx, buf=0x%x, size=0x%llx, error=0x%x, res=0x%llx, xid=0x%x)", - fd, aio->offset, aio->buf, aio->size, error, res, xid); - } - - if (func) - { - Emu.GetCallbackManager().Async([func, aio, error, xid, res](PPUThread& CPU) - { - func(CPU, aio, error, xid, res); - }); - } - - g_FsAioReadCur++; -} - -s32 cellFsAioRead(vm::ptr aio, vm::ptr id, vm::ptr xaio, s32 error, s32 xid, u64 size)> func) -{ - sys_fs.Warning("cellFsAioRead(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); - - if (!aio_init) - { - return CELL_ENXIO; - } - - std::shared_ptr orig_file; - u32 fd = aio->fd; - - if (!Emu.GetIdManager().GetIDData(fd, orig_file)) - { - return CELL_EBADF; - } - - //get a unique id for the callback (may be used by cellFsAioCancel) - const s32 xid = g_FsAioReadID++; - *id = xid; - - thread_t t("CellFsAio Reading Thread", std::bind(fsAioRead, fd, aio, xid, func)); - return CELL_OK; -} - -s32 cellFsAioWrite(vm::ptr aio, vm::ptr id, vm::ptr xaio, s32 error, s32 xid, u64 size)> func) -{ - sys_fs.Todo("cellFsAioWrite(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); - - // TODO: - - return CELL_OK; -} - -s32 cellFsAioInit(vm::ptr mount_point) -{ - sys_fs.Warning("cellFsAioInit(mount_point=*0x%x)", mount_point); - sys_fs.Warning("*** mount_point = '%s'", mount_point.get_ptr()); - - aio_init = true; - return CELL_OK; -} - -s32 cellFsAioFinish(vm::ptr mount_point) -{ - sys_fs.Warning("cellFsAioFinish(mount_point=*0x%x)", mount_point); - sys_fs.Warning("*** mount_point = '%s'", mount_point.get_ptr()); - - //aio_init = false; - return CELL_OK; -} - -s32 cellFsReadWithOffset(PPUThread& CPU, u32 fd, u64 offset, vm::ptr buf, u64 buffer_size, vm::ptr> nread) -{ - sys_fs.Warning("cellFsReadWithOffset(fd=%d, offset=0x%llx, buf=*0x%x, buffer_size=%lld, nread=*0x%llx)", fd, offset, buf, buffer_size, nread); - - int ret; - vm::stackvar> oldPos(CPU), newPos(CPU); - ret = cellFsLseek(fd, 0, CELL_SEEK_CUR, oldPos); // Save the current position - if (ret) return ret; - ret = cellFsLseek(fd, offset, CELL_SEEK_SET, newPos); // Move to the specified offset - if (ret) return ret; - ret = cellFsRead(fd, buf, buffer_size, nread); // Read the file - if (ret) return ret; - ret = cellFsLseek(fd, oldPos.value(), CELL_SEEK_SET, newPos); // Return to the old position - if (ret) return ret; - - return CELL_OK; -} - -s32 cellFsSetDefaultContainer(u32 id, u32 total_limit) -{ - sys_fs.Todo("cellFsSetDefaultContainer(id=%d, total_limit=%d)", id, total_limit); - - return CELL_OK; -} - -s32 cellFsSetIoBufferFromDefaultContainer(u32 fd, u32 buffer_size, u32 page_type) -{ - sys_fs.Todo("cellFsSetIoBufferFromDefaultContainer(fd=%d, buffer_size=%d, page_type=%d)", fd, buffer_size, page_type); - - std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; - - return CELL_OK; -} - -Module sys_fs("sys_fs", []() -{ - g_FsAioReadID = 0; - g_FsAioReadCur = 0; - aio_init = false; - - REG_FUNC(sys_fs, cellFsOpen); - REG_FUNC(sys_fs, cellFsSdataOpen); - REG_FUNC(sys_fs, cellFsSdataOpenByFd); - REG_FUNC(sys_fs, cellFsRead); - REG_FUNC(sys_fs, cellFsWrite); - REG_FUNC(sys_fs, cellFsClose); - REG_FUNC(sys_fs, cellFsOpendir); - REG_FUNC(sys_fs, cellFsReaddir); - REG_FUNC(sys_fs, cellFsClosedir); - REG_FUNC(sys_fs, cellFsStat); - REG_FUNC(sys_fs, cellFsFstat); - REG_FUNC(sys_fs, cellFsMkdir); - REG_FUNC(sys_fs, cellFsRename); - REG_FUNC(sys_fs, cellFsChmod); - REG_FUNC(sys_fs, cellFsFsync); - REG_FUNC(sys_fs, cellFsRmdir); - REG_FUNC(sys_fs, cellFsUnlink); - REG_FUNC(sys_fs, cellFsLseek); - REG_FUNC(sys_fs, cellFsFtruncate); - REG_FUNC(sys_fs, cellFsTruncate); - REG_FUNC(sys_fs, cellFsFGetBlockSize); - REG_FUNC(sys_fs, cellFsAioRead); - REG_FUNC(sys_fs, cellFsAioWrite); - REG_FUNC(sys_fs, cellFsAioInit); - REG_FUNC(sys_fs, cellFsAioFinish); - REG_FUNC(sys_fs, cellFsGetBlockSize); - REG_FUNC(sys_fs, cellFsGetFreeSize); - REG_FUNC(sys_fs, cellFsReadWithOffset); - REG_FUNC(sys_fs, cellFsGetDirectoryEntries); - REG_FUNC(sys_fs, cellFsStReadInit); - REG_FUNC(sys_fs, cellFsStReadFinish); - REG_FUNC(sys_fs, cellFsStReadGetRingBuf); - REG_FUNC(sys_fs, cellFsStReadGetStatus); - REG_FUNC(sys_fs, cellFsStReadGetRegid); - REG_FUNC(sys_fs, cellFsStReadStart); - REG_FUNC(sys_fs, cellFsStReadStop); - REG_FUNC(sys_fs, cellFsStRead); - REG_FUNC(sys_fs, cellFsStReadGetCurrentAddr); - REG_FUNC(sys_fs, cellFsStReadPutCurrentAddr); - REG_FUNC(sys_fs, cellFsStReadWait); - REG_FUNC(sys_fs, cellFsStReadWaitCallback); - REG_FUNC(sys_fs, cellFsSetDefaultContainer); - REG_FUNC(sys_fs, cellFsSetIoBufferFromDefaultContainer); -}); diff --git a/rpcs3/Emu/SysCalls/lv2/cellFs.h b/rpcs3/Emu/SysCalls/lv2/cellFs.h deleted file mode 100644 index e114ef3464..0000000000 --- a/rpcs3/Emu/SysCalls/lv2/cellFs.h +++ /dev/null @@ -1,154 +0,0 @@ -#pragma once - -enum CellFsOflag -{ - CELL_O_RDONLY = 000000, - CELL_O_WRONLY = 000001, - CELL_O_RDWR = 000002, - CELL_O_ACCMODE = 000003, - CELL_O_CREAT = 000100, - CELL_O_EXCL = 000200, - CELL_O_TRUNC = 001000, - CELL_O_APPEND = 002000, - CELL_O_MSELF = 010000, -}; - -static const u32 CELL_FS_TYPE_UNKNOWN = 0; - -enum CellFsSeek -{ - CELL_SEEK_SET, - CELL_SEEK_CUR, - CELL_SEEK_END, -}; - -enum CellFsLength -{ - CELL_MAX_FS_PATH_LENGTH = 1024, - CELL_MAX_FS_FILE_NAME_LENGTH = 255, - CELL_MAX_FS_MP_LENGTH = 31, -}; - -enum -{ - CELL_FS_S_IFDIR = 0040000, //directory - CELL_FS_S_IFREG = 0100000, //regular - CELL_FS_S_IFLNK = 0120000, //symbolic link - CELL_FS_S_IFWHT = 0160000, //unknown - - CELL_FS_S_IRUSR = 0000400, //R for owner - CELL_FS_S_IWUSR = 0000200, //W for owner - CELL_FS_S_IXUSR = 0000100, //X for owner - - CELL_FS_S_IRGRP = 0000040, //R for group - CELL_FS_S_IWGRP = 0000020, //W for group - CELL_FS_S_IXGRP = 0000010, //X for group - - CELL_FS_S_IROTH = 0000004, //R for other - CELL_FS_S_IWOTH = 0000002, //W for other - CELL_FS_S_IXOTH = 0000001, //X for other -}; - -enum FsDirentType -{ - CELL_FS_TYPE_DIRECTORY = 1, - CELL_FS_TYPE_REGULAR = 2, - CELL_FS_TYPE_SYMLINK = 3, -}; - -enum CellFsRingBufferCopy -{ - CELL_FS_ST_COPY = 0, - CELL_FS_ST_COPYLESS = 1, -}; - -enum cellFsStStatus -{ - CELL_FS_ST_INITIALIZED = 0x0001, - CELL_FS_ST_NOT_INITIALIZED = 0x0002, - CELL_FS_ST_STOP = 0x0100, - CELL_FS_ST_PROGRESS = 0x0200, -}; - -enum cellFsErrors -{ - CELL_FS_ERANGE = 34 -}; - -#pragma pack(push, 4) - -struct CellFsStat -{ - be_t st_mode; - be_t st_uid; - be_t st_gid; - be_t st_atime_; - be_t st_mtime_; - be_t st_ctime_; - be_t st_size; - be_t st_blksize; -}; - -struct CellFsUtimbuf -{ - be_t actime; - be_t modtime; -}; - -struct CellFsDirent -{ - u8 d_type; - u8 d_namlen; - char d_name[CELL_MAX_FS_FILE_NAME_LENGTH + 1]; -}; - -#pragma pack(pop) - -struct CellFsAio -{ - be_t fd; - be_t offset; - vm::bptr buf; - be_t size; - be_t user_data; -}; - -struct CellFsDirectoryEntry -{ - CellFsStat attribute; - CellFsDirent entry_name; -}; - -struct CellFsRingBuffer -{ - be_t ringbuf_size; - be_t block_size; - be_t transfer_rate; - be_t copy; -}; - -// SysCalls -s32 cellFsOpen(vm::ptr path, s32 flags, vm::ptr> fd, vm::ptr arg, u64 size); -s32 cellFsRead(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr> nread); -s32 cellFsWrite(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite); -s32 cellFsClose(u32 fd); -s32 cellFsOpendir(vm::ptr path, vm::ptr fd); -s32 cellFsReaddir(u32 fd, vm::ptr dir, vm::ptr nread); -s32 cellFsClosedir(u32 fd); -s32 cellFsStat(vm::ptr path, vm::ptr sb); -s32 cellFsFstat(u32 fd, vm::ptr sb); -s32 cellFsMkdir(vm::ptr path, u32 mode); -s32 cellFsRename(vm::ptr from, vm::ptr to); -s32 cellFsChmod(vm::ptr path, u32 mode); -s32 cellFsFsync(u32 fd); -s32 cellFsRmdir(vm::ptr path); -s32 cellFsUnlink(vm::ptr path); -s32 cellFsLseek(u32 fd, s64 offset, u32 whence, vm::ptr> pos); -s32 cellFsFtruncate(u32 fd, u64 size); -s32 cellFsTruncate(vm::ptr path, u64 size); -s32 cellFsFGetBlockSize(u32 fd, vm::ptr sector_size, vm::ptr block_size); -s32 cellFsGetBlockSize(vm::ptr path, vm::ptr sector_size, vm::ptr block_size); -s32 cellFsGetFreeSize(vm::ptr path, vm::ptr block_size, vm::ptr block_count); -s32 cellFsGetDirectoryEntries(u32 fd, vm::ptr entries, u32 entries_size, vm::ptr data_count); - -// no need to add every function here diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp new file mode 100644 index 0000000000..9050aa0f85 --- /dev/null +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -0,0 +1,511 @@ +#include "stdafx.h" +#include "Emu/Memory/Memory.h" +#include "Emu/System.h" +#include "Emu/IdManager.h" +#include "Emu/SysCalls/SysCalls.h" + +#ifdef _WIN32 +#include +#undef CreateFile +#else +#include +#include +#endif + +#include "Emu/FS/VFS.h" +#include "Emu/FS/vfsFile.h" +#include "Emu/FS/vfsDir.h" + +#include "sys_fs.h" + +SysCallBase sys_fs("sys_fs"); + +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u64 mode, vm::ptr arg, u64 size) +{ + sys_fs.Warning("sys_fs_open(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); + sys_fs.Warning("*** path = '%s'", path.get_ptr()); + + const std::string _path = path.get_ptr(); + + s32 _oflags = flags; + if (flags & CELL_FS_O_CREAT) + { + _oflags &= ~CELL_FS_O_CREAT; + Emu.GetVFS().CreateFile(_path); + } + + vfsOpenMode o_mode; + + switch (flags & CELL_FS_O_ACCMODE) + { + case CELL_FS_O_RDONLY: + _oflags &= ~CELL_FS_O_RDONLY; + o_mode = vfsRead; + break; + + case CELL_FS_O_WRONLY: + _oflags &= ~CELL_FS_O_WRONLY; + + if (flags & CELL_FS_O_APPEND) + { + _oflags &= ~CELL_FS_O_APPEND; + o_mode = vfsWriteAppend; + } + else if (flags & CELL_FS_O_EXCL) + { + _oflags &= ~CELL_FS_O_EXCL; + o_mode = vfsWriteExcl; + } + else //if (flags & CELL_FS_O_TRUNC) + { + _oflags &= ~CELL_FS_O_TRUNC; + o_mode = vfsWrite; + } + break; + + case CELL_FS_O_RDWR: + _oflags &= ~CELL_FS_O_RDWR; + if (flags & CELL_FS_O_TRUNC) + { + _oflags &= ~CELL_FS_O_TRUNC; + //truncate file before opening it as read/write + auto filePtr = Emu.GetVFS().OpenFile(_path, vfsWrite); + delete filePtr; + } + o_mode = vfsReadWrite; + break; + } + + if (_oflags != 0) + { + sys_fs.Error("sys_fs_open(): '%s' has unknown flags! flags: 0x%08x", path.get_ptr(), flags); + return CELL_EINVAL; + } + + if (!Emu.GetVFS().ExistsFile(_path)) + { + sys_fs.Error("sys_fs_open(): '%s' not found! flags: 0x%08x", path.get_ptr(), flags); + return CELL_ENOENT; + } + + std::shared_ptr stream(Emu.GetVFS().OpenFile(_path, o_mode)); + + if (!stream || !stream->IsOpened()) + { + sys_fs.Error("sys_fs_open(): '%s' not found! flags: 0x%08x", path.get_ptr(), flags); + return CELL_ENOENT; + } + + u32 id = Emu.GetIdManager().GetNewID(stream, TYPE_FS_FILE); + *fd = id; + sys_fs.Notice("sys_fs_open(): '%s' opened, id -> 0x%x", path.get_ptr(), id); + + return CELL_OK; +} + +s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread) +{ + sys_fs.Log("sys_fs_read(fd=0x%x, buf=0x%x, nbytes=0x%llx, nread=0x%x)", fd, buf, nbytes, nread); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + // TODO: checks + + const u64 res = nbytes ? file->Read(buf.get_ptr(), nbytes) : 0; + + if (nread) *nread = res; + + return CELL_OK; +} + +s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite) +{ + sys_fs.Log("sys_fs_write(fd=0x%x, buf=*0x%x, nbytes=0x%llx, nwrite=*0x%x)", fd, buf, nbytes, nwrite); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; + + // TODO: checks + + const u64 res = nbytes ? file->Write(buf.get_ptr(), nbytes) : 0; + + if (nwrite) *nwrite = res; + + return CELL_OK; +} + +s32 sys_fs_close(u32 fd) +{ + sys_fs.Warning("sys_fs_close(fd=0x%x)", fd); + + if (!Emu.GetIdManager().RemoveID(fd)) + return CELL_ESRCH; + + return CELL_OK; +} + +s32 sys_fs_opendir(vm::ptr path, vm::ptr fd) +{ + sys_fs.Warning("sys_fs_opendir(path=*0x%x, fd=*0x%x)", path, fd); + sys_fs.Warning("*** path = '%s'", path.get_ptr()); + + std::shared_ptr dir(Emu.GetVFS().OpenDir(path.get_ptr())); + if (!dir || !dir->IsOpened()) + { + return CELL_ENOENT; + } + + *fd = Emu.GetIdManager().GetNewID(dir, TYPE_FS_DIR); + return CELL_OK; +} + +s32 sys_fs_readdir(u32 fd, vm::ptr dir, vm::ptr nread) +{ + sys_fs.Warning("sys_fs_readdir(fd=0x%x, dir=*0x%x, nread=*0x%x)", fd, dir, nread); + + std::shared_ptr directory; + if (!Emu.GetIdManager().GetIDData(fd, directory)) + return CELL_ESRCH; + + const DirEntryInfo* info = directory->Read(); + if (info) + { + dir->d_type = (info->flags & DirEntry_TypeFile) ? CELL_FS_TYPE_REGULAR : CELL_FS_TYPE_DIRECTORY; + dir->d_namlen = u8(std::min(info->name.length(), CELL_FS_MAX_FS_FILE_NAME_LENGTH)); + strcpy_trunc(dir->d_name, info->name); + *nread = sizeof(CellFsDirent); + } + else + { + *nread = 0; + } + + return CELL_OK; +} + +s32 sys_fs_closedir(u32 fd) +{ + sys_fs.Warning("sys_fs_closedir(fd=0x%x)", fd); + + if (!Emu.GetIdManager().RemoveID(fd)) + return CELL_ESRCH; + + return CELL_OK; +} + +s32 sys_fs_stat(vm::ptr path, vm::ptr sb) +{ + sys_fs.Warning("sys_fs_stat(path=*0x%x, sb=*0x%x)", path, sb); + sys_fs.Warning("*** path = '%s'", path.get_ptr()); + + const std::string _path = path.get_ptr(); + + u32 mode = 0; + s32 uid = 0; + s32 gid = 0; + u64 atime = 0; + u64 mtime = 0; + u64 ctime = 0; + u64 size = 0; + + std::string real_path; + + Emu.GetVFS().GetDevice(_path, real_path); + + int stat_result; +#ifdef _WIN32 + struct _stat64 buf; + stat_result = _stat64(real_path.c_str(), &buf); +#else + struct stat buf; + stat_result = stat(real_path.c_str(), &buf); +#endif + if (stat_result) + { + sys_fs.Error("sys_fs_stat(): stat('%s') failed -> 0x%x", real_path.c_str(), stat_result); + } + else + { + mode = buf.st_mode; + uid = buf.st_uid; + gid = buf.st_gid; + atime = buf.st_atime; + mtime = buf.st_mtime; + ctime = buf.st_ctime; + size = buf.st_size; + } + + sb->mode = + CELL_FS_S_IRUSR | CELL_FS_S_IWUSR | CELL_FS_S_IXUSR | + CELL_FS_S_IRGRP | CELL_FS_S_IWGRP | CELL_FS_S_IXGRP | + CELL_FS_S_IROTH | CELL_FS_S_IWOTH | CELL_FS_S_IXOTH; + + if (sb->mode == mode) + sys_fs.Error("sys_fs_stat(): mode is the same (0x%x)", mode); + + sb->uid = uid; + sb->gid = gid; + sb->atime = atime; + sb->mtime = mtime; + sb->ctime = ctime; + sb->blksize = 4096; + + { + vfsDir dir(_path); + if (dir.IsOpened()) + { + sb->mode |= CELL_FS_S_IFDIR; + return CELL_OK; + } + } + + { + vfsFile f(_path); + if (f.IsOpened()) + { + sb->mode |= CELL_FS_S_IFREG; + sb->size = f.GetSize(); + return CELL_OK; + } + } + + if (sb->size == size && size != 0) + sys_fs.Error("sys_fs_stat(): size is the same (0x%x)", size); + + sys_fs.Warning("sys_fs_stat(): '%s' not found", path.get_ptr()); + return CELL_ENOENT; +} + +s32 sys_fs_fstat(u32 fd, vm::ptr sb) +{ + sys_fs.Warning("sys_fs_fstat(fd=0x%x, sb=*0x%x)", fd, sb); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + sb->mode = + CELL_FS_S_IRUSR | CELL_FS_S_IWUSR | CELL_FS_S_IXUSR | + CELL_FS_S_IRGRP | CELL_FS_S_IWGRP | CELL_FS_S_IXGRP | + CELL_FS_S_IROTH | CELL_FS_S_IWOTH | CELL_FS_S_IXOTH; + + sb->mode |= CELL_FS_S_IFREG; //TODO: dir CELL_FS_S_IFDIR + sb->uid = 0; + sb->gid = 0; + sb->atime = 0; //TODO + sb->mtime = 0; //TODO + sb->ctime = 0; //TODO + sb->size = file->GetSize(); + sb->blksize = 4096; + + return CELL_OK; +} + +s32 sys_fs_mkdir(vm::ptr path, CellFsMode mode) +{ + sys_fs.Warning("sys_fs_mkdir(path=*0x%x, mode=%d)", path, mode); + sys_fs.Warning("*** path = '%s'", path.get_ptr()); + + const std::string _path = path.get_ptr(); + + if (vfsDir().IsExists(_path)) + return CELL_EEXIST; + + if (!Emu.GetVFS().CreateDir(_path)) + return CELL_EBUSY; + + sys_fs.Notice("sys_fs_mkdir(): directory '%s' created", path.get_ptr()); + return CELL_OK; +} + +s32 sys_fs_rename(vm::ptr from, vm::ptr to) +{ + sys_fs.Warning("sys_fs_rename(from=*0x%x, to=*0x%x)", from, to); + sys_fs.Warning("*** from = '%s'", from.get_ptr()); + sys_fs.Warning("*** to = '%s'", to.get_ptr()); + + std::string _from = from.get_ptr(); + std::string _to = to.get_ptr(); + + { + vfsDir dir; + if (dir.IsExists(_from)) + { + if (!dir.Rename(_from, _to)) + return CELL_EBUSY; + + sys_fs.Notice("sys_fs_rename(): directory '%s' renamed to '%s'", from.get_ptr(), to.get_ptr()); + return CELL_OK; + } + } + + { + vfsFile f; + + if (f.Exists(_from)) + { + if (!f.Rename(_from, _to)) + return CELL_EBUSY; + + sys_fs.Notice("sys_fs_rename(): file '%s' renamed to '%s'", from.get_ptr(), to.get_ptr()); + return CELL_OK; + } + } + + return CELL_ENOENT; +} + +s32 sys_fs_rmdir(vm::ptr path) +{ + sys_fs.Warning("sys_fs_rmdir(path=*0x%x)", path); + sys_fs.Warning("*** path = '%s'", path.get_ptr()); + + std::string _path = path.get_ptr(); + + vfsDir d; + if (!d.IsExists(_path)) + return CELL_ENOENT; + + if (!d.Remove(_path)) + return CELL_EBUSY; + + sys_fs.Notice("sys_fs_rmdir(): directory '%s' removed", path.get_ptr()); + return CELL_OK; +} + +s32 sys_fs_unlink(vm::ptr path) +{ + sys_fs.Warning("sys_fs_unlink(path=*0x%x)", path); + sys_fs.Warning("*** path = '%s'", path.get_ptr()); + + std::string _path = path.get_ptr(); + + if (vfsDir().IsExists(_path)) + return CELL_EISDIR; + + if (!Emu.GetVFS().ExistsFile(_path)) + return CELL_ENOENT; + + if (!Emu.GetVFS().RemoveFile(_path)) + return CELL_EACCES; + + sys_fs.Notice("sys_fs_unlink(): file '%s' deleted", path.get_ptr()); + return CELL_OK; +} + +s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr pos) +{ + sys_fs.Log("sys_fs_lseek(fd=0x%x, offset=0x%llx, whence=0x%x, pos=*0x%x)", fd, offset, whence, pos); + + vfsSeekMode seek_mode; + switch (whence) + { + case CELL_FS_SEEK_SET: seek_mode = vfsSeekSet; break; + case CELL_FS_SEEK_CUR: seek_mode = vfsSeekCur; break; + case CELL_FS_SEEK_END: seek_mode = vfsSeekEnd; break; + default: + sys_fs.Error("sys_fs_lseek(fd=0x%x): unknown seek whence (0x%x)", fd, whence); + return CELL_EINVAL; + } + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + *pos = file->Seek(offset, seek_mode); + return CELL_OK; +} + +s32 sys_fs_fget_block_size(u32 fd, vm::ptr sector_size, vm::ptr block_size, vm::ptr arg4, vm::ptr arg5) +{ + sys_fs.Todo("sys_fs_fget_block_size(fd=%d, sector_size=*0x%x, block_size=*0x%x, arg4=*0x%x, arg5=*0x%x)", fd, sector_size, block_size, arg4, arg5); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + *sector_size = 4096; // ? + *block_size = 4096; // ? + + return CELL_OK; +} + +s32 sys_fs_get_block_size(vm::ptr path, vm::ptr sector_size, vm::ptr block_size, vm::ptr arg4) +{ + sys_fs.Todo("sys_fs_get_block_size(path=*0x%x, sector_size=*0x%x, block_size=*0x%x, arg4=*0x%x, arg5=*0x%x)", path, sector_size, block_size, arg4); + sys_fs.Todo("*** path = '%s'", path.get_ptr()); + + *sector_size = 4096; // ? + *block_size = 4096; // ? + + return CELL_OK; +} + +s32 sys_fs_truncate(vm::ptr path, u64 size) +{ + sys_fs.Warning("sys_fs_truncate(path=*0x%x, size=0x%llx)", path, size); + sys_fs.Warning("*** path = '%s'", path.get_ptr()); + + vfsFile f(path.get_ptr(), vfsReadWrite); + if (!f.IsOpened()) + { + sys_fs.Warning("sys_fs_truncate(): '%s' not found", path.get_ptr()); + return CELL_ENOENT; + } + u64 initialSize = f.GetSize(); + + if (initialSize < size) + { + u64 last_pos = f.Tell(); + f.Seek(0, vfsSeekEnd); + static const char nullbyte = 0; + f.Seek(size - initialSize - 1, vfsSeekCur); + f.Write(&nullbyte, sizeof(char)); + f.Seek(last_pos, vfsSeekSet); + } + + if (initialSize > size) + { + // (TODO) + } + + return CELL_OK; +} + +s32 sys_fs_ftruncate(u32 fd, u64 size) +{ + sys_fs.Warning("sys_fs_ftruncate(fd=0x%x, size=0x%llx)", fd, size); + + std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) + return CELL_ESRCH; + + u64 initialSize = file->GetSize(); + + if (initialSize < size) + { + u64 last_pos = file->Tell(); + file->Seek(0, vfsSeekEnd); + static const char nullbyte = 0; + file->Seek(size - initialSize - 1, vfsSeekCur); + file->Write(&nullbyte, sizeof(char)); + file->Seek(last_pos, vfsSeekSet); + } + + if (initialSize > size) + { + // (TODO) + } + + return CELL_OK; +} + +s32 sys_fs_chmod(vm::ptr path, CellFsMode mode) +{ + sys_fs.Todo("sys_fs_chmod(path=*0x%x, mode=%d) -> CELL_OK", path, mode); + sys_fs.Todo("*** path = '%s'", path.get_ptr()); + + return CELL_OK; +} diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.h b/rpcs3/Emu/SysCalls/lv2/sys_fs.h new file mode 100644 index 0000000000..6ae94e60db --- /dev/null +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.h @@ -0,0 +1,166 @@ +#pragma once + +#pragma pack(push, 4) + +// Error Codes +enum : s32 +{ + CELL_FS_EDOM = CELL_EDOM, + CELL_FS_EFAULT = CELL_EFAULT, + CELL_FS_EFBIG = CELL_EFBIG, + CELL_FS_EFPOS = CELL_EFPOS, + CELL_FS_EMLINK = CELL_EMLINK, + CELL_FS_ENFILE = CELL_ENFILE, + CELL_FS_ENOENT = CELL_ENOENT, + CELL_FS_ENOSPC = CELL_ENOSPC, + CELL_FS_ENOTTY = CELL_ENOTTY, + CELL_FS_EPIPE = CELL_EPIPE, + CELL_FS_ERANGE = CELL_ERANGE, + CELL_FS_EROFS = CELL_EROFS, + CELL_FS_ESPIPE = CELL_ESPIPE, + CELL_FS_E2BIG = CELL_E2BIG, + CELL_FS_EACCES = CELL_EACCES, + CELL_FS_EAGAIN = CELL_EAGAIN, + CELL_FS_EBADF = CELL_EBADF, + CELL_FS_EBUSY = CELL_EBUSY, + //CELL_FS_ECHILD = CELL_ECHILD, + CELL_FS_EEXIST = CELL_EEXIST, + CELL_FS_EINTR = CELL_EINTR, + CELL_FS_EINVAL = CELL_EINVAL, + CELL_FS_EIO = CELL_EIO, + CELL_FS_EISDIR = CELL_EISDIR, + CELL_FS_EMFILE = CELL_EMFILE, + CELL_FS_ENODEV = CELL_ENODEV, + CELL_FS_ENOEXEC = CELL_ENOEXEC, + CELL_FS_ENOMEM = CELL_ENOMEM, + CELL_FS_ENOTDIR = CELL_ENOTDIR, + CELL_FS_ENXIO = CELL_ENXIO, + CELL_FS_EPERM = CELL_EPERM, + CELL_FS_ESRCH = CELL_ESRCH, + CELL_FS_EXDEV = CELL_EXDEV, + CELL_FS_EBADMSG = CELL_EBADMSG, + CELL_FS_ECANCELED = CELL_ECANCELED, + CELL_FS_EDEADLK = CELL_EDEADLK, + CELL_FS_EILSEQ = CELL_EILSEQ, + CELL_FS_EINPROGRESS = CELL_EINPROGRESS, + CELL_FS_EMSGSIZE = CELL_EMSGSIZE, + CELL_FS_ENAMETOOLONG = CELL_ENAMETOOLONG, + CELL_FS_ENOLCK = CELL_ENOLCK, + CELL_FS_ENOSYS = CELL_ENOSYS, + CELL_FS_ENOTEMPTY = CELL_ENOTEMPTY, + CELL_FS_ENOTSUP = CELL_ENOTSUP, + CELL_FS_ETIMEDOUT = CELL_ETIMEDOUT, + CELL_FS_EFSSPECIFIC = CELL_EFSSPECIFIC, + CELL_FS_EOVERFLOW = CELL_EOVERFLOW, + CELL_FS_ENOTMOUNTED = CELL_ENOTMOUNTED, + CELL_FS_ENOTMSELF = CELL_ENOTMSELF, + CELL_FS_ENOTSDATA = CELL_ENOTSDATA, + CELL_FS_EAUTHFATAL = CELL_EAUTHFATAL, +}; + +// Open Flags +enum : s32 +{ + CELL_FS_O_RDONLY = 000000, + CELL_FS_O_WRONLY = 000001, + CELL_FS_O_RDWR = 000002, + CELL_FS_O_ACCMODE = 000003, + CELL_FS_O_CREAT = 000100, + CELL_FS_O_EXCL = 000200, + CELL_FS_O_TRUNC = 001000, + CELL_FS_O_APPEND = 002000, + CELL_FS_O_MSELF = 010000, +}; + +// Seek Mode +enum : s32 +{ + CELL_FS_SEEK_SET, + CELL_FS_SEEK_CUR, + CELL_FS_SEEK_END, +}; + +enum : s32 +{ + CELL_FS_MAX_FS_PATH_LENGTH = 1024, + CELL_FS_MAX_FS_FILE_NAME_LENGTH = 255, + CELL_FS_MAX_MP_LENGTH = 31, +}; + +enum CellFsMode : s32 +{ + CELL_FS_S_IFMT = 0170000, + CELL_FS_S_IFDIR = 0040000, // directory + CELL_FS_S_IFREG = 0100000, // regular + CELL_FS_S_IFLNK = 0120000, // symbolic link + CELL_FS_S_IFWHT = 0160000, // unknown + + CELL_FS_S_IRUSR = 0000400, // R for owner + CELL_FS_S_IWUSR = 0000200, // W for owner + CELL_FS_S_IXUSR = 0000100, // X for owner + + CELL_FS_S_IRGRP = 0000040, // R for group + CELL_FS_S_IWGRP = 0000020, // W for group + CELL_FS_S_IXGRP = 0000010, // X for group + + CELL_FS_S_IROTH = 0000004, // R for other + CELL_FS_S_IWOTH = 0000002, // W for other + CELL_FS_S_IXOTH = 0000001, // X for other +}; + +// CellFsDirent.d_type +enum : u8 +{ + CELL_FS_TYPE_UNKNOWN = 0, + CELL_FS_TYPE_DIRECTORY = 1, + CELL_FS_TYPE_REGULAR = 2, + CELL_FS_TYPE_SYMLINK = 3, +}; + +struct CellFsDirent +{ + u8 d_type; + u8 d_namlen; + char d_name[256]; +}; + +struct CellFsStat +{ + be_t mode; + be_t uid; + be_t gid; + be_t atime; + be_t mtime; + be_t ctime; + be_t size; + be_t blksize; +}; + +struct CellFsUtimbuf +{ + be_t actime; + be_t modtime; +}; + +#pragma pack(pop) + +// SysCalls +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u64 mode, vm::ptr arg, u64 size); +s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread); +s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite); +s32 sys_fs_close(u32 fd); +s32 sys_fs_opendir(vm::ptr path, vm::ptr fd); +s32 sys_fs_readdir(u32 fd, vm::ptr dir, vm::ptr nread); +s32 sys_fs_closedir(u32 fd); +s32 sys_fs_stat(vm::ptr path, vm::ptr sb); +s32 sys_fs_fstat(u32 fd, vm::ptr sb); +s32 sys_fs_mkdir(vm::ptr path, CellFsMode mode); +s32 sys_fs_rename(vm::ptr from, vm::ptr to); +s32 sys_fs_rmdir(vm::ptr path); +s32 sys_fs_unlink(vm::ptr path); +s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr pos); +s32 sys_fs_fget_block_size(u32 fd, vm::ptr sector_size, vm::ptr block_size, vm::ptr arg4, vm::ptr arg5); +s32 sys_fs_get_block_size(vm::ptr path, vm::ptr sector_size, vm::ptr block_size, vm::ptr arg4); +s32 sys_fs_truncate(vm::ptr path, u64 size); +s32 sys_fs_ftruncate(u32 fd, u64 size); +s32 sys_fs_chmod(vm::ptr path, CellFsMode mode); diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index d0f086572f..f7f0b774a9 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -39,6 +39,8 @@ + + @@ -177,7 +179,6 @@ - @@ -439,11 +440,11 @@ - + @@ -472,6 +473,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index c1fa6b8ea9..9d50d29f54 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -650,9 +650,6 @@ Emu\SysCalls - - Emu\SysCalls\lv2 - Utilities @@ -857,6 +854,12 @@ Emu\SysCalls\lv2 + + Emu\SysCalls\lv2 + + + Emu\SysCalls\Modules + @@ -1450,9 +1453,6 @@ Emu\CPU\ARMv7 - - Emu\SysCalls\lv2 - Utilities @@ -1537,5 +1537,11 @@ Emu\Memory + + Emu\SysCalls\lv2 + + + Emu\SysCalls\Modules + \ No newline at end of file From bcad7afcc97956798d8635fb82dec20065a527dd Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Thu, 12 Mar 2015 22:39:41 +0300 Subject: [PATCH 05/20] Bugfix --- rpcs3/Emu/SysCalls/ErrorCodes.h | 129 +++++++++++----------- rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp | 2 +- 2 files changed, 67 insertions(+), 64 deletions(-) diff --git a/rpcs3/Emu/SysCalls/ErrorCodes.h b/rpcs3/Emu/SysCalls/ErrorCodes.h index 9cedbc510b..373c806539 100644 --- a/rpcs3/Emu/SysCalls/ErrorCodes.h +++ b/rpcs3/Emu/SysCalls/ErrorCodes.h @@ -1,66 +1,69 @@ #pragma once -enum ErrorCode -{ - CELL_OK = 0x00000000, - CELL_EAGAIN = 0x80010001, //The resource is temporarily unavailable - CELL_EINVAL = 0x80010002, //An invalid argument value is specified - CELL_ENOSYS = 0x80010003, //The feature is not yet implemented - CELL_ENOMEM = 0x80010004, //Memory allocation failure - CELL_ESRCH = 0x80010005, //The resource with the specified identifier does not exist - CELL_ENOENT = 0x80010006, //The file does not exist - CELL_ENOEXEC = 0x80010007, //The file is in unrecognized format - CELL_EDEADLK = 0x80010008, //Resource deadlock is avoided - CELL_EPERM = 0x80010009, //The operation is not permitted - CELL_EBUSY = 0x8001000A, //The device or resource is busy - CELL_ETIMEDOUT = 0x8001000B, //The operation is timed out - CELL_EABORT = 0x8001000C, //The operation is aborted - CELL_EFAULT = 0x8001000D, //Invalid memory access - CELL_ESTAT = 0x8001000F, //State of the target thread is invalid - CELL_EALIGN = 0x80010010, //Alignment is invalid. - CELL_EKRESOURCE = 0x80010011, //Shortage of the kernel resources - CELL_EISDIR = 0x80010012, //The file is a directory - CELL_ECANCELED = 0x80010013, //Operation canceled - CELL_EEXIST = 0x80010014, //Entry already exists - CELL_EISCONN = 0x80010015, //Port is already connected - CELL_ENOTCONN = 0x80010016, //Port is not connected - CELL_EAUTHFAIL = 0x80010017, //Program authentication fail - CELL_ENOTMSELF = 0x80010018, //The file is not a MSELF - CELL_ESYSVER = 0x80010019, //System version error - CELL_EAUTHFATAL = 0x8001001A, //Fatal system error - CELL_EDOM = 0x8001001B, - CELL_ERANGE = 0x8001001C, - CELL_EILSEQ = 0x8001001D, - CELL_EFPOS = 0x8001001E, - CELL_EINTR = 0x8001001F, - CELL_EFBIG = 0x80010020, - CELL_EMLINK = 0x80010021, - CELL_ENFILE = 0x80010022, - CELL_ENOSPC = 0x80010023, - CELL_ENOTTY = 0x80010024, - CELL_EPIPE = 0x80010025, - CELL_EROFS = 0x80010026, - CELL_ESPIPE = 0x80010027, - CELL_E2BIG = 0x80010028, - CELL_EACCES = 0x80010029, - CELL_EBADF = 0x8001002A, - CELL_EIO = 0x8001002B, - CELL_EMFILE = 0x8001002C, - CELL_ENODEV = 0x8001002D, - CELL_ENOTDIR = 0x8001002E, - CELL_ENXIO = 0x8001002F, - CELL_EXDEV = 0x80010030, - CELL_EBADMSG = 0x80010031, - CELL_EINPROGRESS = 0x80010032, - CELL_EMSGSIZE = 0x80010033, - CELL_ENAMETOOLONG = 0x80010034, - CELL_ENOLCK = 0x80010035, - CELL_ENOTEMPTY = 0x80010036, - CELL_ENOTSUP = 0x80010037, - CELL_EFSSPECIFIC = 0x80010038, - CELL_EOVERFLOW = 0x80010039, - CELL_ENOTMOUNTED = 0x8001003A, - CELL_ENOTSDATA = 0x8001003B, +#define ERROR_CODE(code) static_cast(code) - CELL_UNKNOWN_ERROR = 0xFFFFFFFF, -}; \ No newline at end of file +enum : s32 +{ + CELL_OK = 0, + + CELL_EAGAIN = ERROR_CODE(0x80010001), // The resource is temporarily unavailable + CELL_EINVAL = ERROR_CODE(0x80010002), // An invalid argument value is specified + CELL_ENOSYS = ERROR_CODE(0x80010003), // The feature is not yet implemented + CELL_ENOMEM = ERROR_CODE(0x80010004), // Memory allocation failure + CELL_ESRCH = ERROR_CODE(0x80010005), // The resource with the specified identifier does not exist + CELL_ENOENT = ERROR_CODE(0x80010006), // The file does not exist + CELL_ENOEXEC = ERROR_CODE(0x80010007), // The file is in unrecognized format + CELL_EDEADLK = ERROR_CODE(0x80010008), // Resource deadlock is avoided + CELL_EPERM = ERROR_CODE(0x80010009), // The operation is not permitted + CELL_EBUSY = ERROR_CODE(0x8001000A), // The device or resource is busy + CELL_ETIMEDOUT = ERROR_CODE(0x8001000B), // The operation is timed out + CELL_EABORT = ERROR_CODE(0x8001000C), // The operation is aborted + CELL_EFAULT = ERROR_CODE(0x8001000D), // Invalid memory access + CELL_ESTAT = ERROR_CODE(0x8001000F), // State of the target thread is invalid + CELL_EALIGN = ERROR_CODE(0x80010010), // Alignment is invalid. + CELL_EKRESOURCE = ERROR_CODE(0x80010011), // Shortage of the kernel resources + CELL_EISDIR = ERROR_CODE(0x80010012), // The file is a directory + CELL_ECANCELED = ERROR_CODE(0x80010013), // Operation canceled + CELL_EEXIST = ERROR_CODE(0x80010014), // Entry already exists + CELL_EISCONN = ERROR_CODE(0x80010015), // Port is already connected + CELL_ENOTCONN = ERROR_CODE(0x80010016), // Port is not connected + CELL_EAUTHFAIL = ERROR_CODE(0x80010017), // Program authentication fail + CELL_ENOTMSELF = ERROR_CODE(0x80010018), // The file is not a MSELF + CELL_ESYSVER = ERROR_CODE(0x80010019), // System version error + CELL_EAUTHFATAL = ERROR_CODE(0x8001001A), // Fatal system error + CELL_EDOM = ERROR_CODE(0x8001001B), + CELL_ERANGE = ERROR_CODE(0x8001001C), + CELL_EILSEQ = ERROR_CODE(0x8001001D), + CELL_EFPOS = ERROR_CODE(0x8001001E), + CELL_EINTR = ERROR_CODE(0x8001001F), + CELL_EFBIG = ERROR_CODE(0x80010020), + CELL_EMLINK = ERROR_CODE(0x80010021), + CELL_ENFILE = ERROR_CODE(0x80010022), + CELL_ENOSPC = ERROR_CODE(0x80010023), + CELL_ENOTTY = ERROR_CODE(0x80010024), + CELL_EPIPE = ERROR_CODE(0x80010025), + CELL_EROFS = ERROR_CODE(0x80010026), + CELL_ESPIPE = ERROR_CODE(0x80010027), + CELL_E2BIG = ERROR_CODE(0x80010028), + CELL_EACCES = ERROR_CODE(0x80010029), + CELL_EBADF = ERROR_CODE(0x8001002A), + CELL_EIO = ERROR_CODE(0x8001002B), + CELL_EMFILE = ERROR_CODE(0x8001002C), + CELL_ENODEV = ERROR_CODE(0x8001002D), + CELL_ENOTDIR = ERROR_CODE(0x8001002E), + CELL_ENXIO = ERROR_CODE(0x8001002F), + CELL_EXDEV = ERROR_CODE(0x80010030), + CELL_EBADMSG = ERROR_CODE(0x80010031), + CELL_EINPROGRESS = ERROR_CODE(0x80010032), + CELL_EMSGSIZE = ERROR_CODE(0x80010033), + CELL_ENAMETOOLONG = ERROR_CODE(0x80010034), + CELL_ENOLCK = ERROR_CODE(0x80010035), + CELL_ENOTEMPTY = ERROR_CODE(0x80010036), + CELL_ENOTSUP = ERROR_CODE(0x80010037), + CELL_EFSSPECIFIC = ERROR_CODE(0x80010038), + CELL_EOVERFLOW = ERROR_CODE(0x80010039), + CELL_ENOTMOUNTED = ERROR_CODE(0x8001003A), + CELL_ENOTSDATA = ERROR_CODE(0x8001003B), + + CELL_UNKNOWN_ERROR = -1, +}; diff --git a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp index 6020324200..2b201f20a0 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp @@ -145,7 +145,7 @@ s32 pngReadHeader( { auto file = Emu.GetIdManager().GetIDData(stream->fd); file->Seek(0); - file->Read(buffer, buffer.size()); + file->Read(buffer.begin(), buffer.size()); break; } } From 5764ee7a2ab217912cb5bbb07eb39965238aa0bd Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 02:03:22 +0300 Subject: [PATCH 06/20] Small fix --- Utilities/Thread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index eee78eac10..e1fdec7cb8 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -202,7 +202,7 @@ public: return sq_size; } - bool is_full() const volatile + bool is_full() const { return m_sync.read_relaxed().count == sq_size; } From 5c31037c044c1e969822e518898779f453412ca3 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 02:18:38 +0300 Subject: [PATCH 07/20] atomic_t update --- rpcs3/Emu/Memory/atomic.h | 161 +++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 80 deletions(-) diff --git a/rpcs3/Emu/Memory/atomic.h b/rpcs3/Emu/Memory/atomic.h index 9641aadf95..270f135165 100644 --- a/rpcs3/Emu/Memory/atomic.h +++ b/rpcs3/Emu/Memory/atomic.h @@ -7,193 +7,194 @@ #undef InterlockedXor template -struct _to_atomic +struct _to_atomic_subtype { static_assert(size == 1 || size == 2 || size == 4 || size == 8 || size == 16, "Invalid atomic type"); - - typedef T type; }; template -struct _to_atomic +struct _to_atomic_subtype { - typedef uint8_t type; + using type = uint8_t; }; template -struct _to_atomic +struct _to_atomic_subtype { - typedef uint16_t type; + using type = uint16_t; }; template -struct _to_atomic +struct _to_atomic_subtype { - typedef uint32_t type; + using type = uint32_t; }; template -struct _to_atomic +struct _to_atomic_subtype { - typedef uint64_t type; + using type = uint64_t; }; template -struct _to_atomic +struct _to_atomic_subtype { - typedef u128 type; + using type = u128; }; template -class _atomic_base +union _atomic_base { - typedef typename _to_atomic::type atomic_type; - atomic_type data; + using type = typename std::remove_cv::type; + using subtype = typename _to_atomic_subtype::type; + + type data; // unsafe direct access + subtype volatile sub_data; // unsafe direct access to substitute type + + __forceinline static const subtype to_subtype(const type& value) + { + return reinterpret_cast(value); + } + + __forceinline static const type from_subtype(const subtype value) + { + return reinterpret_cast(value); + } + + __forceinline static type& to_type(subtype& value) + { + return reinterpret_cast(value); + } public: // atomically compare data with cmp, replace with exch if equal, return previous data value anyway - __forceinline const T compare_and_swap(const T& cmp, const T& exch) volatile + __forceinline const type compare_and_swap(const type& cmp, const type& exch) { - const atomic_type res = InterlockedCompareExchange(&data, (atomic_type&)(exch), (atomic_type&)(cmp)); - return (T&)res; + return from_subtype(InterlockedCompareExchange(&sub_data, to_subtype(exch), to_subtype(cmp))); } // atomically compare data with cmp, replace with exch if equal, return true if data was replaced - __forceinline bool compare_and_swap_test(const T& cmp, const T& exch) volatile + __forceinline bool compare_and_swap_test(const type& cmp, const type& exch) { - return InterlockedCompareExchangeTest(&data, (atomic_type&)(exch), (atomic_type&)(cmp)); + return InterlockedCompareExchangeTest(&sub_data, to_subtype(exch), to_subtype(cmp)); } // read data with memory barrier - __forceinline const T read_sync() const volatile + __forceinline const type read_sync() const { - const atomic_type res = InterlockedCompareExchange(const_cast(&data), 0, 0); - return (T&)res; + return from_subtype(InterlockedCompareExchange(const_cast(&sub_data), 0, 0)); } // atomically replace data with exch, return previous data value - __forceinline const T exchange(const T& exch) volatile + __forceinline const type exchange(const type& exch) { - const atomic_type res = InterlockedExchange(&data, (atomic_type&)(exch)); - return (T&)res; + return from_subtype(InterlockedExchange(&sub_data, to_subtype(exch))); } // read data without memory barrier - __forceinline const T read_relaxed() const volatile + __forceinline const type read_relaxed() const { - return (T&)data; + const subtype value = const_cast(sub_data); + return from_subtype(value); } // write data without memory barrier - __forceinline void write_relaxed(const T& value) + __forceinline void write_relaxed(const type& value) { - data = (atomic_type&)(value); + const_cast(sub_data) = to_subtype(value); } // perform atomic operation on data - template __forceinline void atomic_op(const FT atomic_proc) volatile + template __forceinline void atomic_op(const FT atomic_proc) { while (true) { - const T old = read_relaxed(); - T _new = old; - atomic_proc(_new); // function should accept reference to T type - if (compare_and_swap_test(old, _new)) return; + const subtype old = const_cast(sub_data); + subtype _new = old; + atomic_proc(to_type(_new)); // function should accept reference to T type + if (InterlockedCompareExchangeTest(&sub_data, _new, old)) return; } } // perform atomic operation on data with special exit condition (if intermediate result != proceed_value) - template __forceinline RT atomic_op(const RT proceed_value, const FT atomic_proc) volatile + template __forceinline RT atomic_op(const RT proceed_value, const FT atomic_proc) { while (true) { - const T old = read_relaxed(); - T _new = old; - RT res = (RT)atomic_proc(_new); // function should accept reference to T type and return some value + const subtype old = const_cast(sub_data); + subtype _new = old; + auto res = static_cast(atomic_proc(to_type(_new))); // function should accept reference to T type and return some value if (res != proceed_value) return res; - if (compare_and_swap_test(old, _new)) return proceed_value; + if (InterlockedCompareExchangeTest(&sub_data, _new, old)) return proceed_value; } } // perform atomic operation on data with additional memory barrier - template __forceinline void atomic_op_sync(const FT atomic_proc) volatile + template __forceinline void atomic_op_sync(const FT atomic_proc) { - T old = read_sync(); + subtype old = InterlockedCompareExchange(&sub_data, 0, 0); while (true) { - T _new = old; - atomic_proc(_new); // function should accept reference to T type - const T val = compare_and_swap(old, _new); - if ((atomic_type&)val == (atomic_type&)old) return; + subtype _new = old; + atomic_proc(to_type(_new)); // function should accept reference to T type + const subtype val = InterlockedCompareExchange(&sub_data, _new, old); + if (val == old) return; old = val; } } // perform atomic operation on data with additional memory barrier and special exit condition (if intermediate result != proceed_value) - template __forceinline RT atomic_op_sync(const RT proceed_value, const FT atomic_proc) volatile + template __forceinline RT atomic_op_sync(const RT proceed_value, const FT atomic_proc) { - T old = read_sync(); + subtype old = InterlockedCompareExchange(&sub_data, 0, 0); while (true) { - T _new = old; - RT res = (RT)atomic_proc(_new); // function should accept reference to T type and return some value + subtype _new = old; + auto res = static_cast(atomic_proc(to_type(_new))); // function should accept reference to T type and return some value if (res != proceed_value) return res; - const T val = compare_and_swap(old, _new); - if ((atomic_type&)val == (atomic_type&)old) return proceed_value; + const subtype val = InterlockedCompareExchange(&sub_data, _new, old); + if (val == old) return proceed_value; old = val; } } - // perform non-atomic operation on data directly without memory barriers - template __forceinline void direct_op(const FT direct_proc) volatile - { - direct_proc((T&)data); - } - // atomic bitwise OR, returns previous data - __forceinline const T _or(const T& right) volatile + __forceinline const type _or(const type& right) { - const atomic_type res = InterlockedOr(&data, (atomic_type&)(right)); - return (T&)res; + return from_subtype(InterlockedOr(&sub_data, to_subtype(right))); } // atomic bitwise AND, returns previous data - __forceinline const T _and(const T& right) volatile + __forceinline const type _and(const type& right) { - const atomic_type res = InterlockedAnd(&data, (atomic_type&)(right)); - return (T&)res; + return from_subtype(InterlockedAnd(&sub_data, to_subtype(right))); } // atomic bitwise AND NOT (inverts right argument), returns previous data - __forceinline const T _and_not(const T& right) volatile + __forceinline const type _and_not(const type& right) { - const atomic_type res = InterlockedAnd(&data, ~(atomic_type&)(right)); - return (T&)res; + return from_subtype(InterlockedAnd(&sub_data, ~to_subtype(right))); } // atomic bitwise XOR, returns previous data - __forceinline const T _xor(const T& right) volatile + __forceinline const type _xor(const type& right) { - const atomic_type res = InterlockedXor(&data, (atomic_type&)(right)); - return (T&)res; + return from_subtype(InterlockedXor(&sub_data, to_subtype(right))); } - __forceinline const T operator |= (const T& right) volatile + __forceinline const type operator |= (const type& right) { - const atomic_type res = InterlockedOr(&data, (atomic_type&)(right)) | (atomic_type&)(right); - return (T&)res; + return from_subtype(InterlockedOr(&sub_data, to_subtype(right)) | to_subtype(right)); } - __forceinline const T operator &= (const T& right) volatile + __forceinline const type operator &= (const type& right) { - const atomic_type res = InterlockedAnd(&data, (atomic_type&)(right)) & (atomic_type&)(right); - return (T&)res; + return from_subtype(InterlockedAnd(&sub_data, to_subtype(right)) & to_subtype(right)); } - __forceinline const T operator ^= (const T& right) volatile + __forceinline const type operator ^= (const type& right) { - const atomic_type res = InterlockedXor(&data, (atomic_type&)(right)) ^ (atomic_type&)(right); - return (T&)res; + return from_subtype(InterlockedXor(&sub_data, to_subtype(right)) ^ to_subtype(right)); } }; From c88e0a0eb91079c98f24ea622b174485c74daeb7 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 04:09:53 +0300 Subject: [PATCH 08/20] Small cleanup --- Utilities/Thread.h | 6 ++--- rpcs3/Emu/Cell/SPUThread.cpp | 28 ++++++++++---------- rpcs3/Emu/Cell/SPUThread.h | 25 ++++++++--------- rpcs3/Emu/SysCalls/Modules/cellSync.cpp | 16 +++++------ rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp | 4 +-- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 2 +- rpcs3/Emu/SysCalls/lv2/sys_fs.h | 6 ++--- rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp | 2 +- rpcs3/Emu/SysCalls/lv2/sys_spu.cpp | 5 ++-- 9 files changed, 46 insertions(+), 48 deletions(-) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index e1fdec7cb8..d41900a621 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -193,8 +193,8 @@ class squeue_t public: squeue_t() + : m_sync({}) { - m_sync.write_relaxed({}); } u32 get_max_size() const @@ -202,9 +202,9 @@ public: return sq_size; } - bool is_full() const + bool is_full() const volatile { - return m_sync.read_relaxed().count == sq_size; + return m_sync.data.count == sq_size; } bool push(const T& data, const std::function& test_exit) diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index b529b6276f..81c7b6dc8d 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -78,29 +78,29 @@ void SPUThread::InitRegs() ch_mfc_args = {}; mfc_queue.clear(); - ch_tag_mask = 0; - ch_tag_stat.data = {}; - ch_stall_stat.data = {}; - ch_atomic_stat.data = {}; + ch_tag_mask = {}; + ch_tag_stat = {}; + ch_stall_stat = {}; + ch_atomic_stat = {}; ch_in_mbox.clear(); - ch_out_mbox.data = {}; - ch_out_intr_mbox.data = {}; + ch_out_mbox = {}; + ch_out_intr_mbox = {}; - snr_config = 0; + snr_config = {}; - ch_snr1.data = {}; - ch_snr2.data = {}; + ch_snr1 = {}; + ch_snr2 = {}; - ch_event_mask = 0; - ch_event_stat.write_relaxed(0); + ch_event_mask = {}; + ch_event_stat = {}; ch_dec_start_timestamp = get_time(); // ??? - ch_dec_value = 0; + ch_dec_value = {}; - run_ctrl.write_relaxed(0); - status.write_relaxed(0); + run_ctrl = {}; + status = {}; int0.clear(); int2.clear(); diff --git a/rpcs3/Emu/Cell/SPUThread.h b/rpcs3/Emu/Cell/SPUThread.h index db395bb770..bee08966ab 100644 --- a/rpcs3/Emu/Cell/SPUThread.h +++ b/rpcs3/Emu/Cell/SPUThread.h @@ -139,10 +139,7 @@ union spu_channel_t atomic_t sync_var; // atomic variable - sync_var_t data; // unsafe direct access - public: - bool push(u32 value) { bool out_result; @@ -205,14 +202,14 @@ public: sync_var.write_relaxed({ count, value }); } - u32 get_value() + u32 get_value() volatile { - return sync_var.read_relaxed().value; + return sync_var.data.value; } - u32 get_count() + u32 get_count() volatile { - return sync_var.read_relaxed().count; + return sync_var.data.count; } }; @@ -232,8 +229,8 @@ struct spu_channel_4_t public: void clear() { - sync_var.write_relaxed({}); - value3.write_relaxed({}); + sync_var = {}; + value3 = {}; } void push_uncond(u32 value) @@ -275,9 +272,9 @@ public: return out_result; } - u32 get_count() + u32 get_count() volatile { - return sync_var.read_relaxed().count; + return sync_var.data.count; } }; @@ -311,10 +308,10 @@ public: void clear() { - mask.write_relaxed(0); - stat.write_relaxed(0); + mask = {}; + stat = {}; - assigned.write_relaxed(-1); + assigned = { -1 }; } }; diff --git a/rpcs3/Emu/SysCalls/Modules/cellSync.cpp b/rpcs3/Emu/SysCalls/Modules/cellSync.cpp index 56f5f43c81..6bf4e90285 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellSync.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellSync.cpp @@ -882,28 +882,28 @@ void syncLFQueueInit(vm::ptr queue, vm::ptr buffer, u32 siz if (direction == CELL_SYNC_QUEUE_ANY2ANY) { - queue->pop1.write_relaxed({}); - queue->push1.write_relaxed({}); + queue->pop1 = {}; + queue->push1 = {}; queue->m_buffer.set(queue->m_buffer.addr() | 1); queue->m_bs[0] = -1; queue->m_bs[1] = -1; //m_bs[2] //m_bs[3] queue->m_v1 = -1; - queue->push2.write_relaxed({ be_t::make(-1) }); - queue->pop2.write_relaxed({ be_t::make(-1) }); + queue->push2 = { { be_t::make(-1) } }; + queue->pop2 = { { be_t::make(-1) } }; } else { - queue->pop1.write_relaxed({ be_t::make(0), be_t::make(0), queue->pop1.read_relaxed().m_h3, be_t::make(0) }); - queue->push1.write_relaxed({ be_t::make(0), be_t::make(0), queue->push1.read_relaxed().m_h7, be_t::make(0) }); + queue->pop1 = { { be_t::make(0), be_t::make(0), queue->pop1.read_relaxed().m_h3, be_t::make(0) } }; + queue->push1 = { { be_t::make(0), be_t::make(0), queue->push1.read_relaxed().m_h7, be_t::make(0) } }; queue->m_bs[0] = -1; // written as u32 queue->m_bs[1] = -1; queue->m_bs[2] = -1; queue->m_bs[3] = -1; queue->m_v1 = 0; - queue->push2.write_relaxed({}); - queue->pop2.write_relaxed({}); + queue->push2 = {}; + queue->pop2 = {}; } queue->m_v2 = 0; diff --git a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp index 54ab1e2bb4..9f31519d78 100644 --- a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp +++ b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp @@ -114,7 +114,7 @@ s32 sys_lwmutex_create(vm::ptr lwmutex, vm::ptr lw(new lwmutex_t(protocol, attr->name_u64)); - lwmutex->lock_var.write_relaxed({ lwmutex::free, lwmutex::zero }); + lwmutex->lock_var = { { lwmutex::free, lwmutex::zero } }; lwmutex->attribute = attr->recursive | attr->protocol; lwmutex->recursive_count = 0; lwmutex->sleep_queue = Emu.GetIdManager().GetNewID(lw); @@ -575,7 +575,7 @@ s32 sys_lwcond_wait(PPUThread& CPU, vm::ptr lwcond, u64 timeout) const be_t recursive_value = lwmutex->recursive_count; // set special value - lwmutex->owner.write_relaxed(lwmutex::reserved); + lwmutex->owner = { lwmutex::reserved }; lwmutex->recursive_count = 0; // call the syscall diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index 9050aa0f85..99c0c58914 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -20,7 +20,7 @@ SysCallBase sys_fs("sys_fs"); -s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u64 mode, vm::ptr arg, u64 size) +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u32 mode, vm::ptr arg, u64 size) { sys_fs.Warning("sys_fs_open(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.h b/rpcs3/Emu/SysCalls/lv2/sys_fs.h index 6ae94e60db..f54eeeb047 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.h @@ -89,7 +89,7 @@ enum : s32 enum CellFsMode : s32 { - CELL_FS_S_IFMT = 0170000, + CELL_FS_S_IFMT = 0170000, CELL_FS_S_IFDIR = 0040000, // directory CELL_FS_S_IFREG = 0100000, // regular CELL_FS_S_IFLNK = 0120000, // symbolic link @@ -126,7 +126,7 @@ struct CellFsDirent struct CellFsStat { - be_t mode; + be_t mode; be_t uid; be_t gid; be_t atime; @@ -145,7 +145,7 @@ struct CellFsUtimbuf #pragma pack(pop) // SysCalls -s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u64 mode, vm::ptr arg, u64 size); +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u32 mode, vm::ptr arg, u64 size); s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread); s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite); s32 sys_fs_close(u32 fd); diff --git a/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp b/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp index 5b8f99e1c3..4568c84aee 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_lwmutex.cpp @@ -16,7 +16,7 @@ void lwmutex_create(sys_lwmutex_t& lwmutex, bool recursive, u32 protocol, u64 na { std::shared_ptr mutex(new lwmutex_t(protocol, name)); - lwmutex.lock_var.write_relaxed({ lwmutex::free, lwmutex::zero }); + lwmutex.lock_var = { { lwmutex::free, lwmutex::zero } }; lwmutex.attribute = protocol | (recursive ? SYS_SYNC_RECURSIVE : SYS_SYNC_NOT_RECURSIVE); lwmutex.recursive_count = 0; lwmutex.sleep_queue = Emu.GetIdManager().GetNewID(mutex, TYPE_LWMUTEX); diff --git a/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp b/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp index f2f98e2338..f4eb597754 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp @@ -315,11 +315,12 @@ s32 sys_spu_thread_group_start(u32 id) spu.SetEntry(image->entry_point); spu.Run(); - spu.status.write_relaxed(SPU_STATUS_RUNNING); spu.GPR[3] = u128::from64(0, args.arg1); spu.GPR[4] = u128::from64(0, args.arg2); spu.GPR[5] = u128::from64(0, args.arg3); spu.GPR[6] = u128::from64(0, args.arg4); + + spu.status.exchange(SPU_STATUS_RUNNING); } } @@ -518,7 +519,7 @@ s32 sys_spu_thread_group_terminate(u32 id, s32 value) { auto& spu = static_cast(*t); - spu.status.write_relaxed(SPU_STATUS_STOPPED); + spu.status.exchange(SPU_STATUS_STOPPED); spu.FastStop(); } } From 3e34bd64bf3a74a2eb1ce2f1da24ce86ccb15f11 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 04:59:25 +0300 Subject: [PATCH 09/20] Compilation fix --- rpcs3/Emu/Cell/SPUThread.cpp | 9 +++++---- rpcs3/Emu/Memory/atomic.h | 36 ++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index 81c7b6dc8d..854567c362 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -78,7 +78,7 @@ void SPUThread::InitRegs() ch_mfc_args = {}; mfc_queue.clear(); - ch_tag_mask = {}; + ch_tag_mask = 0; ch_tag_stat = {}; ch_stall_stat = {}; ch_atomic_stat = {}; @@ -88,19 +88,20 @@ void SPUThread::InitRegs() ch_out_mbox = {}; ch_out_intr_mbox = {}; - snr_config = {}; + snr_config = 0; ch_snr1 = {}; ch_snr2 = {}; - ch_event_mask = {}; + ch_event_mask = 0; ch_event_stat = {}; ch_dec_start_timestamp = get_time(); // ??? - ch_dec_value = {}; + ch_dec_value = 0; run_ctrl = {}; status = {}; + npc = {}; int0.clear(); int2.clear(); diff --git a/rpcs3/Emu/Memory/atomic.h b/rpcs3/Emu/Memory/atomic.h index 270f135165..54eb1c7427 100644 --- a/rpcs3/Emu/Memory/atomic.h +++ b/rpcs3/Emu/Memory/atomic.h @@ -49,7 +49,7 @@ union _atomic_base using subtype = typename _to_atomic_subtype::type; type data; // unsafe direct access - subtype volatile sub_data; // unsafe direct access to substitute type + subtype sub_data; // unsafe direct access to substitute type __forceinline static const subtype to_subtype(const type& value) { @@ -68,44 +68,44 @@ union _atomic_base public: // atomically compare data with cmp, replace with exch if equal, return previous data value anyway - __forceinline const type compare_and_swap(const type& cmp, const type& exch) + __forceinline const type compare_and_swap(const type& cmp, const type& exch) volatile { return from_subtype(InterlockedCompareExchange(&sub_data, to_subtype(exch), to_subtype(cmp))); } // atomically compare data with cmp, replace with exch if equal, return true if data was replaced - __forceinline bool compare_and_swap_test(const type& cmp, const type& exch) + __forceinline bool compare_and_swap_test(const type& cmp, const type& exch) volatile { return InterlockedCompareExchangeTest(&sub_data, to_subtype(exch), to_subtype(cmp)); } // read data with memory barrier - __forceinline const type read_sync() const + __forceinline const type read_sync() const volatile { return from_subtype(InterlockedCompareExchange(const_cast(&sub_data), 0, 0)); } // atomically replace data with exch, return previous data value - __forceinline const type exchange(const type& exch) + __forceinline const type exchange(const type& exch) volatile { return from_subtype(InterlockedExchange(&sub_data, to_subtype(exch))); } // read data without memory barrier - __forceinline const type read_relaxed() const + __forceinline const type read_relaxed() const volatile { const subtype value = const_cast(sub_data); return from_subtype(value); } // write data without memory barrier - __forceinline void write_relaxed(const type& value) + __forceinline void write_relaxed(const type& value) volatile { const_cast(sub_data) = to_subtype(value); } // perform atomic operation on data - template __forceinline void atomic_op(const FT atomic_proc) + template __forceinline void atomic_op(const FT atomic_proc) volatile { while (true) { @@ -117,7 +117,7 @@ public: } // perform atomic operation on data with special exit condition (if intermediate result != proceed_value) - template __forceinline RT atomic_op(const RT proceed_value, const FT atomic_proc) + template __forceinline RT atomic_op(const RT proceed_value, const FT atomic_proc) volatile { while (true) { @@ -130,7 +130,7 @@ public: } // perform atomic operation on data with additional memory barrier - template __forceinline void atomic_op_sync(const FT atomic_proc) + template __forceinline void atomic_op_sync(const FT atomic_proc) volatile { subtype old = InterlockedCompareExchange(&sub_data, 0, 0); while (true) @@ -144,7 +144,7 @@ public: } // perform atomic operation on data with additional memory barrier and special exit condition (if intermediate result != proceed_value) - template __forceinline RT atomic_op_sync(const RT proceed_value, const FT atomic_proc) + template __forceinline RT atomic_op_sync(const RT proceed_value, const FT atomic_proc) volatile { subtype old = InterlockedCompareExchange(&sub_data, 0, 0); while (true) @@ -159,40 +159,40 @@ public: } // atomic bitwise OR, returns previous data - __forceinline const type _or(const type& right) + __forceinline const type _or(const type& right) volatile { return from_subtype(InterlockedOr(&sub_data, to_subtype(right))); } // atomic bitwise AND, returns previous data - __forceinline const type _and(const type& right) + __forceinline const type _and(const type& right) volatile { return from_subtype(InterlockedAnd(&sub_data, to_subtype(right))); } // atomic bitwise AND NOT (inverts right argument), returns previous data - __forceinline const type _and_not(const type& right) + __forceinline const type _and_not(const type& right) volatile { return from_subtype(InterlockedAnd(&sub_data, ~to_subtype(right))); } // atomic bitwise XOR, returns previous data - __forceinline const type _xor(const type& right) + __forceinline const type _xor(const type& right) volatile { return from_subtype(InterlockedXor(&sub_data, to_subtype(right))); } - __forceinline const type operator |= (const type& right) + __forceinline const type operator |= (const type& right) volatile { return from_subtype(InterlockedOr(&sub_data, to_subtype(right)) | to_subtype(right)); } - __forceinline const type operator &= (const type& right) + __forceinline const type operator &= (const type& right) volatile { return from_subtype(InterlockedAnd(&sub_data, to_subtype(right)) & to_subtype(right)); } - __forceinline const type operator ^= (const type& right) + __forceinline const type operator ^= (const type& right) volatile { return from_subtype(InterlockedXor(&sub_data, to_subtype(right)) ^ to_subtype(right)); } From 6c36013e2a4f87a12ad5b04968925e513a546a48 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 18:06:27 +0300 Subject: [PATCH 10/20] sys_fs_open rewritten --- rpcs3/Emu/FS/VFS.cpp | 4 +- rpcs3/Emu/FS/VFS.h | 2 +- rpcs3/Emu/FS/vfsFile.cpp | 4 +- rpcs3/Emu/FS/vfsFile.h | 2 +- rpcs3/Emu/FS/vfsFileBase.h | 2 +- rpcs3/Emu/FS/vfsLocalFile.cpp | 8 +- rpcs3/Emu/FS/vfsLocalFile.h | 2 +- rpcs3/Emu/Memory/atomic.h | 8 +- rpcs3/Emu/Memory/vm_var.h | 20 +++ rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 20 +-- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 170 +++++++++++++++----------- rpcs3/Emu/SysCalls/lv2/sys_fs.h | 22 ++-- 12 files changed, 158 insertions(+), 106 deletions(-) diff --git a/rpcs3/Emu/FS/VFS.cpp b/rpcs3/Emu/FS/VFS.cpp index 11feecb3ef..6af15b1f47 100644 --- a/rpcs3/Emu/FS/VFS.cpp +++ b/rpcs3/Emu/FS/VFS.cpp @@ -161,7 +161,7 @@ vfsDirBase* VFS::OpenDir(const std::string& ps3_path) const return nullptr; } -bool VFS::CreateFile(const std::string& ps3_path) const +bool VFS::CreateFile(const std::string& ps3_path, bool overwrite) const { std::string path; if (vfsDevice* dev = GetDevice(ps3_path, path)) @@ -170,7 +170,7 @@ bool VFS::CreateFile(const std::string& ps3_path) const if (res) { - return res->Create(path); + return res->Create(path, overwrite); } } diff --git a/rpcs3/Emu/FS/VFS.h b/rpcs3/Emu/FS/VFS.h index 9d202d4cb9..d62ba7c411 100644 --- a/rpcs3/Emu/FS/VFS.h +++ b/rpcs3/Emu/FS/VFS.h @@ -78,7 +78,7 @@ struct VFS vfsFileBase* OpenFile(const std::string& ps3_path, vfsOpenMode mode) const; vfsDirBase* OpenDir(const std::string& ps3_path) const; - bool CreateFile(const std::string& ps3_path) const; + bool CreateFile(const std::string& ps3_path, bool overwrite = false) const; bool CreateDir(const std::string& ps3_path) const; bool RemoveFile(const std::string& ps3_path) const; bool RemoveDir(const std::string& ps3_path) const; diff --git a/rpcs3/Emu/FS/vfsFile.cpp b/rpcs3/Emu/FS/vfsFile.cpp index 8d0bfb1ebb..6f990c31e2 100644 --- a/rpcs3/Emu/FS/vfsFile.cpp +++ b/rpcs3/Emu/FS/vfsFile.cpp @@ -26,9 +26,9 @@ bool vfsFile::Open(const std::string& path, vfsOpenMode mode) return m_stream && m_stream->IsOpened(); } -bool vfsFile::Create(const std::string& path) +bool vfsFile::Create(const std::string& path, bool overwrite) { - return m_stream->Create(path); + return m_stream->Create(path, overwrite); } bool vfsFile::Exists(const std::string& path) diff --git a/rpcs3/Emu/FS/vfsFile.h b/rpcs3/Emu/FS/vfsFile.h index 4ab9ddc87d..a5ed8cf138 100644 --- a/rpcs3/Emu/FS/vfsFile.h +++ b/rpcs3/Emu/FS/vfsFile.h @@ -11,7 +11,7 @@ public: vfsFile(const std::string& path, vfsOpenMode mode = vfsRead); virtual bool Open(const std::string& path, vfsOpenMode mode = vfsRead) override; - virtual bool Create(const std::string& path) override; + virtual bool Create(const std::string& path, bool overwrite = false) override; virtual bool Exists(const std::string& path) override; virtual bool Rename(const std::string& from, const std::string& to) override; virtual bool Remove(const std::string& path) override; diff --git a/rpcs3/Emu/FS/vfsFileBase.h b/rpcs3/Emu/FS/vfsFileBase.h index ff7a56e349..700f397a2a 100644 --- a/rpcs3/Emu/FS/vfsFileBase.h +++ b/rpcs3/Emu/FS/vfsFileBase.h @@ -27,7 +27,7 @@ public: virtual bool Open(const std::string& path, vfsOpenMode mode); virtual bool Close() override; - virtual bool Create(const std::string& path) { return false; } + virtual bool Create(const std::string& path, bool overwrite = false) { return false; } virtual bool Exists(const std::string& path) { return false; } virtual bool Rename(const std::string& from, const std::string& to) { return false; } virtual bool Remove(const std::string& path) { return false; } diff --git a/rpcs3/Emu/FS/vfsLocalFile.cpp b/rpcs3/Emu/FS/vfsLocalFile.cpp index 0e716c40af..f33639bd1d 100644 --- a/rpcs3/Emu/FS/vfsLocalFile.cpp +++ b/rpcs3/Emu/FS/vfsLocalFile.cpp @@ -51,9 +51,9 @@ bool vfsLocalFile::Open(const std::string& path, vfsOpenMode mode) // } } -bool vfsLocalFile::Create(const std::string& path) +bool vfsLocalFile::Create(const std::string& path, bool overwrite) { - LOG_WARNING(HLE, "vfsLocalFile::Create('%s')", path.c_str()); + LOG_WARNING(HLE, "vfsLocalFile::Create('%s', overwrite=%d)", path.c_str(), overwrite); for(uint p=1; p < path.length() && path[p] != '\0' ; p++) { for(; p < path.length() && path[p] != '\0'; p++) @@ -75,8 +75,8 @@ bool vfsLocalFile::Create(const std::string& path) if(m != '/' && m != '\\' && !rExists(path)) // ??? { rFile f; - if (!f.Create(path)) { - LOG_NOTICE(HLE, "vfsLocalFile::Create: couldn't create file"); + if (!f.Create(path, overwrite)) { + if (overwrite) LOG_NOTICE(HLE, "vfsLocalFile::Create: couldn't create file"); return false; } else diff --git a/rpcs3/Emu/FS/vfsLocalFile.h b/rpcs3/Emu/FS/vfsLocalFile.h index 7395eb519a..efef6328d5 100644 --- a/rpcs3/Emu/FS/vfsLocalFile.h +++ b/rpcs3/Emu/FS/vfsLocalFile.h @@ -11,7 +11,7 @@ public: vfsLocalFile(vfsDevice* device); virtual bool Open(const std::string& path, vfsOpenMode mode = vfsRead) override; - virtual bool Create(const std::string& path) override; + virtual bool Create(const std::string& path, bool overwrite = false) override; virtual bool Close() override; virtual bool Exists(const std::string& path) override; diff --git a/rpcs3/Emu/Memory/atomic.h b/rpcs3/Emu/Memory/atomic.h index 54eb1c7427..f53214792e 100644 --- a/rpcs3/Emu/Memory/atomic.h +++ b/rpcs3/Emu/Memory/atomic.h @@ -199,10 +199,10 @@ public: }; // Helper definitions -template using if_arithmetic_t = typename std::enable_if::value && std::is_arithmetic::value, T>::type; -template using if_arithmetic_be_t = typename std::enable_if::value && std::is_arithmetic::value, be_t>::type; -template using if_arithmetic_atomic_t = typename std::enable_if::value && std::is_arithmetic::value, _atomic_base>::type; -template using if_arithmetic_atomic_be_t = typename std::enable_if::value && std::is_arithmetic::value, _atomic_base>>::type; +template using if_arithmetic_t = const typename std::enable_if::value && std::is_arithmetic::value, T>::type; +template using if_arithmetic_be_t = const typename std::enable_if::value && std::is_arithmetic::value, be_t>::type; +template using if_arithmetic_atomic_t = typename std::enable_if::value && std::is_arithmetic::value, _atomic_base&>::type; +template using if_arithmetic_atomic_be_t = typename std::enable_if::value && std::is_arithmetic::value, _atomic_base>&>::type; template inline static if_arithmetic_t operator ++(_atomic_base& left) { diff --git a/rpcs3/Emu/Memory/vm_var.h b/rpcs3/Emu/Memory/vm_var.h index 1a3d104c3d..b42f89096c 100644 --- a/rpcs3/Emu/Memory/vm_var.h +++ b/rpcs3/Emu/Memory/vm_var.h @@ -117,6 +117,16 @@ namespace vm { return _ptr_base::make(m_addr); } + + template operator _ptr_base() const + { + return _ptr_base::make(m_data.addr); + } + + template operator _ptr_base() const + { + return _ptr_base::make(m_data.addr); + } operator T&() { @@ -614,6 +624,16 @@ namespace vm return _ptr_base::make(m_data.addr); } + template operator _ptr_base() const + { + return _ptr_base::make(m_data.addr); + } + + template operator _ptr_base() const + { + return _ptr_base::make(m_data.addr); + } + operator T&() { return *m_data.ptr; diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index d3ca1644ad..3b18e0965a 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -537,13 +537,20 @@ int sdata_unpack(const std::string& packed_file, const std::string& unpacked_fil return CELL_OK; } -s32 cellFsSdataOpen(vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) +s32 cellFsSdataOpen(PPUThread& CPU, vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) { - cellFs.Warning("cellFsSdataOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx) -> sys_fs_open()", path, flags, fd, arg, size); + cellFs.Log("cellFsSdataOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); - /*if (flags != CELL_O_RDONLY) - return CELL_EINVAL; + if (flags != CELL_FS_O_RDONLY) + { + return CELL_EINVAL; + } + return cellFsOpen(path, CELL_FS_O_RDONLY, fd, vm::stackvar>(CPU), 8); + + // Don't implement sdata decryption in this function, it should be done in sys_fs_open() syscall or somewhere else + + /* std::string suffix = path.substr(path.length() - 5, 5); if (suffix != ".sdat" && suffix != ".SDAT") return CELL_ENOTSDATA; @@ -556,9 +563,8 @@ s32 cellFsSdataOpen(vm::ptr path, s32 flags, vm::ptr fd, vm::pt fd = Emu.GetIdManager().GetNewID(Emu.GetVFS().OpenFile(unpacked_path, vfsRead), TYPE_FS_FILE); - return CELL_OK;*/ - - return sys_fs_open(path, flags, fd, 0, arg, size); + return CELL_OK; + */ } s32 cellFsSdataOpenByFd(u32 mself_fd, s32 flags, vm::ptr sdata_fd, u64 offset, vm::ptr arg, u64 size) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index 99c0c58914..7a93f23687 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -22,83 +22,87 @@ SysCallBase sys_fs("sys_fs"); s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u32 mode, vm::ptr arg, u64 size) { - sys_fs.Warning("sys_fs_open(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); + sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%d, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); - const std::string _path = path.get_ptr(); + std::shared_ptr file; - s32 _oflags = flags; - if (flags & CELL_FS_O_CREAT) + if (mode) { - _oflags &= ~CELL_FS_O_CREAT; - Emu.GetVFS().CreateFile(_path); + sys_fs.Error("sys_fs_open(): unknown mode (0x%x)", mode); + return CELL_FS_EINVAL; } - vfsOpenMode o_mode; + // TODO: other checks for path - switch (flags & CELL_FS_O_ACCMODE) + if (Emu.GetVFS().ExistsDir(path.get_ptr())) + { + sys_fs.Error("sys_fs_open(): '%s' is a directory", path.get_ptr()); + return CELL_FS_EISDIR; + } + + switch (flags) { case CELL_FS_O_RDONLY: - _oflags &= ~CELL_FS_O_RDONLY; - o_mode = vfsRead; + { + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsRead)); break; + } + //case CELL_FS_O_WRONLY: + //case CELL_FS_O_WRONLY | CELL_FS_O_CREAT: - case CELL_FS_O_WRONLY: - _oflags &= ~CELL_FS_O_WRONLY; + case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_EXCL: + { + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteExcl)); - if (flags & CELL_FS_O_APPEND) + if ((!file || !file->IsOpened()) && Emu.GetVFS().ExistsFile(path.get_ptr())) { - _oflags &= ~CELL_FS_O_APPEND; - o_mode = vfsWriteAppend; - } - else if (flags & CELL_FS_O_EXCL) - { - _oflags &= ~CELL_FS_O_EXCL; - o_mode = vfsWriteExcl; - } - else //if (flags & CELL_FS_O_TRUNC) - { - _oflags &= ~CELL_FS_O_TRUNC; - o_mode = vfsWrite; + return CELL_FS_EEXIST; } + break; + } + case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: + { + Emu.GetVFS().CreateFile(path.get_ptr()); + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWrite)); + break; + } + case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_APPEND: + { + Emu.GetVFS().CreateFile(path.get_ptr()); + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); + break; + } case CELL_FS_O_RDWR: - _oflags &= ~CELL_FS_O_RDWR; - if (flags & CELL_FS_O_TRUNC) - { - _oflags &= ~CELL_FS_O_TRUNC; - //truncate file before opening it as read/write - auto filePtr = Emu.GetVFS().OpenFile(_path, vfsWrite); - delete filePtr; - } - o_mode = vfsReadWrite; + { + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); break; } - - if (_oflags != 0) + case CELL_FS_O_RDWR | CELL_FS_O_CREAT: { - sys_fs.Error("sys_fs_open(): '%s' has unknown flags! flags: 0x%08x", path.get_ptr(), flags); - return CELL_EINVAL; + Emu.GetVFS().CreateFile(path.get_ptr()); + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); + break; + } + //case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_EXCL: + //case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: + + default: + { + sys_fs.Error("sys_fs_open(): invalid or unimplemented flags (%d)", flags); + return CELL_FS_EINVAL; + } } - if (!Emu.GetVFS().ExistsFile(_path)) + if (!file || !file->IsOpened()) { - sys_fs.Error("sys_fs_open(): '%s' not found! flags: 0x%08x", path.get_ptr(), flags); - return CELL_ENOENT; + sys_fs.Error("sys_fs_open(): failed to open '%s' (flags=%d, mode=0x%x)", path.get_ptr(), flags, mode); + return CELL_FS_ENOENT; } - std::shared_ptr stream(Emu.GetVFS().OpenFile(_path, o_mode)); - - if (!stream || !stream->IsOpened()) - { - sys_fs.Error("sys_fs_open(): '%s' not found! flags: 0x%08x", path.get_ptr(), flags); - return CELL_ENOENT; - } - - u32 id = Emu.GetIdManager().GetNewID(stream, TYPE_FS_FILE); - *fd = id; - sys_fs.Notice("sys_fs_open(): '%s' opened, id -> 0x%x", path.get_ptr(), id); + *fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); return CELL_OK; } @@ -108,14 +112,13 @@ s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread) sys_fs.Log("sys_fs_read(fd=0x%x, buf=0x%x, nbytes=0x%llx, nread=0x%x)", fd, buf, nbytes, nread); std::shared_ptr file; + if (!Emu.GetIdManager().GetIDData(fd, file)) - return CELL_ESRCH; + { + return CELL_FS_EBADF; // TODO: return if not opened for reading + } - // TODO: checks - - const u64 res = nbytes ? file->Read(buf.get_ptr(), nbytes) : 0; - - if (nread) *nread = res; + *nread = file->Read(buf.get_ptr(), nbytes); return CELL_OK; } @@ -125,23 +128,34 @@ s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrit sys_fs.Log("sys_fs_write(fd=0x%x, buf=*0x%x, nbytes=0x%llx, nwrite=*0x%x)", fd, buf, nbytes, nwrite); std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; - // TODO: checks + if (!Emu.GetIdManager().GetIDData(fd, file)) + { + return CELL_FS_EBADF; // TODO: return if not opened for writing + } - const u64 res = nbytes ? file->Write(buf.get_ptr(), nbytes) : 0; - - if (nwrite) *nwrite = res; + *nwrite = file->Write(buf.get_ptr(), nbytes); return CELL_OK; } s32 sys_fs_close(u32 fd) { - sys_fs.Warning("sys_fs_close(fd=0x%x)", fd); + sys_fs.Log("sys_fs_close(fd=0x%x)", fd); - if (!Emu.GetIdManager().RemoveID(fd)) - return CELL_ESRCH; + std::shared_ptr file; + + if (!Emu.GetIdManager().GetIDData(fd, file)) + { + return CELL_FS_EBADF; + } + + if (false) + { + return CELL_FS_EBUSY; // TODO: return if locked + } + + Emu.GetIdManager().RemoveID(fd); return CELL_OK; } @@ -151,13 +165,15 @@ s32 sys_fs_opendir(vm::ptr path, vm::ptr fd) sys_fs.Warning("sys_fs_opendir(path=*0x%x, fd=*0x%x)", path, fd); sys_fs.Warning("*** path = '%s'", path.get_ptr()); - std::shared_ptr dir(Emu.GetVFS().OpenDir(path.get_ptr())); - if (!dir || !dir->IsOpened()) + std::shared_ptr directory(Emu.GetVFS().OpenDir(path.get_ptr())); + + if (!directory || !directory->IsOpened()) { - return CELL_ENOENT; + return CELL_FS_ENOENT; } - *fd = Emu.GetIdManager().GetNewID(dir, TYPE_FS_DIR); + *fd = Emu.GetIdManager().GetNewID(directory, TYPE_FS_DIR); + return CELL_OK; } @@ -166,10 +182,14 @@ s32 sys_fs_readdir(u32 fd, vm::ptr dir, vm::ptr nread) sys_fs.Warning("sys_fs_readdir(fd=0x%x, dir=*0x%x, nread=*0x%x)", fd, dir, nread); std::shared_ptr directory; + if (!Emu.GetIdManager().GetIDData(fd, directory)) + { return CELL_ESRCH; + } const DirEntryInfo* info = directory->Read(); + if (info) { dir->d_type = (info->flags & DirEntry_TypeFile) ? CELL_FS_TYPE_REGULAR : CELL_FS_TYPE_DIRECTORY; @@ -187,10 +207,16 @@ s32 sys_fs_readdir(u32 fd, vm::ptr dir, vm::ptr nread) s32 sys_fs_closedir(u32 fd) { - sys_fs.Warning("sys_fs_closedir(fd=0x%x)", fd); + sys_fs.Log("sys_fs_closedir(fd=0x%x)", fd); - if (!Emu.GetIdManager().RemoveID(fd)) + std::shared_ptr directory; + + if (!Emu.GetIdManager().GetIDData(fd, directory)) + { return CELL_ESRCH; + } + + Emu.GetIdManager().RemoveID(fd); return CELL_OK; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.h b/rpcs3/Emu/SysCalls/lv2/sys_fs.h index f54eeeb047..546544613e 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.h @@ -61,15 +61,15 @@ enum : s32 // Open Flags enum : s32 { - CELL_FS_O_RDONLY = 000000, - CELL_FS_O_WRONLY = 000001, - CELL_FS_O_RDWR = 000002, + CELL_FS_O_RDONLY = 000000, + CELL_FS_O_WRONLY = 000001, + CELL_FS_O_RDWR = 000002, CELL_FS_O_ACCMODE = 000003, - CELL_FS_O_CREAT = 000100, - CELL_FS_O_EXCL = 000200, - CELL_FS_O_TRUNC = 001000, - CELL_FS_O_APPEND = 002000, - CELL_FS_O_MSELF = 010000, + CELL_FS_O_CREAT = 000100, + CELL_FS_O_EXCL = 000200, + CELL_FS_O_TRUNC = 001000, + CELL_FS_O_APPEND = 002000, + CELL_FS_O_MSELF = 010000, }; // Seek Mode @@ -111,10 +111,10 @@ enum CellFsMode : s32 // CellFsDirent.d_type enum : u8 { - CELL_FS_TYPE_UNKNOWN = 0, + CELL_FS_TYPE_UNKNOWN = 0, CELL_FS_TYPE_DIRECTORY = 1, - CELL_FS_TYPE_REGULAR = 2, - CELL_FS_TYPE_SYMLINK = 3, + CELL_FS_TYPE_REGULAR = 2, + CELL_FS_TYPE_SYMLINK = 3, }; struct CellFsDirent From fc81d79807e49bc30bb8cf493be01838c869bb69 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 18:29:10 +0300 Subject: [PATCH 11/20] Fix --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 6 +++--- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 3b18e0965a..a064c34e12 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -42,7 +42,7 @@ struct FsRingBufferConfig s32 cellFsOpen(vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) { - cellFs.Warning("cellFsOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx) -> sys_fs_open()", path, flags, fd, arg, size); + cellFs.Warning("cellFsOpen(path=*0x%x, flags=%d, fd=*0x%x, arg=*0x%x, size=0x%llx) -> sys_fs_open()", path, flags, fd, arg, size); // TODO @@ -539,7 +539,7 @@ int sdata_unpack(const std::string& packed_file, const std::string& unpacked_fil s32 cellFsSdataOpen(PPUThread& CPU, vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) { - cellFs.Log("cellFsSdataOpen(path=*0x%x, flags=0x%x, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); + cellFs.Log("cellFsSdataOpen(path=*0x%x, flags=%d, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); if (flags != CELL_FS_O_RDONLY) { @@ -569,7 +569,7 @@ s32 cellFsSdataOpen(PPUThread& CPU, vm::ptr path, s32 flags, vm::ptr s32 cellFsSdataOpenByFd(u32 mself_fd, s32 flags, vm::ptr sdata_fd, u64 offset, vm::ptr arg, u64 size) { - cellFs.Todo("cellFsSdataOpenByFd(mself_fd=0x%x, flags=0x%x, sdata_fd=*0x%x, offset=0x%llx, arg=*0x%x, size=0x%llx)", mself_fd, flags, sdata_fd, offset, arg, size); + cellFs.Todo("cellFsSdataOpenByFd(mself_fd=0x%x, flags=%d, sdata_fd=*0x%x, offset=0x%llx, arg=*0x%x, size=0x%llx)", mself_fd, flags, sdata_fd, offset, arg, size); // TODO: diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index 7a93f23687..9a06f45f9e 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -22,15 +22,15 @@ SysCallBase sys_fs("sys_fs"); s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u32 mode, vm::ptr arg, u64 size) { - sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%d, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); + sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%d, fd=*0x%x, mode=%d, arg=*0x%x, size=0x%llx)", path, flags, fd, mode, arg, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); std::shared_ptr file; if (mode) { - sys_fs.Error("sys_fs_open(): unknown mode (0x%x)", mode); - return CELL_FS_EINVAL; + sys_fs.Error("sys_fs_open(): unknown mode (%d)", mode); + //return CELL_FS_EINVAL; } // TODO: other checks for path From aa4b74c194b61dda4fc166acc7d3cdb0d1eb77d4 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 18:33:11 +0300 Subject: [PATCH 12/20] Fix --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 2 +- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 4 ++-- rpcs3/Emu/SysCalls/lv2/sys_fs.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index a064c34e12..9e5e363c8a 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -47,7 +47,7 @@ s32 cellFsOpen(vm::ptr path, s32 flags, vm::ptr fd, vm::ptr buf, u64 nbytes, vm::ptr nread) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index 9a06f45f9e..a4bce07b9b 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -20,7 +20,7 @@ SysCallBase sys_fs("sys_fs"); -s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u32 mode, vm::ptr arg, u64 size) +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, CellFsMode mode, vm::ptr arg, u64 size) { sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%d, fd=*0x%x, mode=%d, arg=*0x%x, size=0x%llx)", path, flags, fd, mode, arg, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); @@ -98,7 +98,7 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u32 mode, if (!file || !file->IsOpened()) { - sys_fs.Error("sys_fs_open(): failed to open '%s' (flags=%d, mode=0x%x)", path.get_ptr(), flags, mode); + sys_fs.Error("sys_fs_open(): failed to open '%s' (flags=%d, mode=%d)", path.get_ptr(), flags, mode); return CELL_FS_ENOENT; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.h b/rpcs3/Emu/SysCalls/lv2/sys_fs.h index 546544613e..5e53d46895 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.h @@ -145,7 +145,7 @@ struct CellFsUtimbuf #pragma pack(pop) // SysCalls -s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, u32 mode, vm::ptr arg, u64 size); +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, CellFsMode mode, vm::ptr arg, u64 size); s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread); s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite); s32 sys_fs_close(u32 fd); From d209cc56718221043912737597f766faef44e0e2 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 18:36:03 +0300 Subject: [PATCH 13/20] Fix --- rpcs3/Emu/Memory/vm_var.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpcs3/Emu/Memory/vm_var.h b/rpcs3/Emu/Memory/vm_var.h index b42f89096c..e67c765f71 100644 --- a/rpcs3/Emu/Memory/vm_var.h +++ b/rpcs3/Emu/Memory/vm_var.h @@ -120,12 +120,12 @@ namespace vm template operator _ptr_base() const { - return _ptr_base::make(m_data.addr); + return _ptr_base::make(m_data); } template operator _ptr_base() const { - return _ptr_base::make(m_data.addr); + return _ptr_base::make(m_data); } operator T&() From 0fc6ec2df906bc9fee17d0dc2c04ab1ead1edc9e Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 18:37:00 +0300 Subject: [PATCH 14/20] Fix --- rpcs3/Emu/Memory/vm_var.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpcs3/Emu/Memory/vm_var.h b/rpcs3/Emu/Memory/vm_var.h index e67c765f71..b33eaf69cb 100644 --- a/rpcs3/Emu/Memory/vm_var.h +++ b/rpcs3/Emu/Memory/vm_var.h @@ -120,12 +120,12 @@ namespace vm template operator _ptr_base() const { - return _ptr_base::make(m_data); + return _ptr_base::make(m_addr); } template operator _ptr_base() const { - return _ptr_base::make(m_data); + return _ptr_base::make(m_addr); } operator T&() From 139173caa05bf36dfb342cef286a689904df1ba9 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 13 Mar 2015 23:43:11 +0300 Subject: [PATCH 15/20] sys_fs_open() update, octal formatting --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 16 +++---- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 69 ++++++++++++++++++++------- rpcs3/Emu/SysCalls/lv2/sys_fs.h | 6 +-- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 9e5e363c8a..4c52cb93f0 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -42,12 +42,12 @@ struct FsRingBufferConfig s32 cellFsOpen(vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) { - cellFs.Warning("cellFsOpen(path=*0x%x, flags=%d, fd=*0x%x, arg=*0x%x, size=0x%llx) -> sys_fs_open()", path, flags, fd, arg, size); + cellFs.Warning("cellFsOpen(path=*0x%x, flags=%#o, fd=*0x%x, arg=*0x%x, size=0x%llx) -> sys_fs_open()", path, flags, fd, arg, size); // TODO // call the syscall - return sys_fs_open(path, flags, fd, {}, arg, size); + return sys_fs_open(path, flags, fd, flags & CELL_FS_O_CREAT ? CELL_FS_S_IRUSR | CELL_FS_S_IWUSR : 0, arg, size); } s32 cellFsRead(PPUThread& CPU, u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread) @@ -118,9 +118,9 @@ s32 cellFsFstat(u32 fd, vm::ptr sb) return sys_fs_fstat(fd, sb); } -s32 cellFsMkdir(vm::ptr path, CellFsMode mode) +s32 cellFsMkdir(vm::ptr path, s32 mode) { - cellFs.Warning("cellFsMkdir(path=*0x%x, mode=%d) -> sys_fs_mkdir()", path, mode); + cellFs.Warning("cellFsMkdir(path=*0x%x, mode=%#o) -> sys_fs_mkdir()", path, mode); // TODO @@ -209,9 +209,9 @@ s32 cellFsFtruncate(u32 fd, u64 size) return sys_fs_ftruncate(fd, size); } -s32 cellFsChmod(vm::ptr path, CellFsMode mode) +s32 cellFsChmod(vm::ptr path, s32 mode) { - cellFs.Warning("cellFsChmod(path=*0x%x, mode=%d) -> sys_fs_chmod()", path, mode); + cellFs.Warning("cellFsChmod(path=*0x%x, mode=%#o) -> sys_fs_chmod()", path, mode); // TODO @@ -539,7 +539,7 @@ int sdata_unpack(const std::string& packed_file, const std::string& unpacked_fil s32 cellFsSdataOpen(PPUThread& CPU, vm::ptr path, s32 flags, vm::ptr fd, vm::ptr arg, u64 size) { - cellFs.Log("cellFsSdataOpen(path=*0x%x, flags=%d, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); + cellFs.Log("cellFsSdataOpen(path=*0x%x, flags=%#o, fd=*0x%x, arg=*0x%x, size=0x%llx)", path, flags, fd, arg, size); if (flags != CELL_FS_O_RDONLY) { @@ -569,7 +569,7 @@ s32 cellFsSdataOpen(PPUThread& CPU, vm::ptr path, s32 flags, vm::ptr s32 cellFsSdataOpenByFd(u32 mself_fd, s32 flags, vm::ptr sdata_fd, u64 offset, vm::ptr arg, u64 size) { - cellFs.Todo("cellFsSdataOpenByFd(mself_fd=0x%x, flags=%d, sdata_fd=*0x%x, offset=0x%llx, arg=*0x%x, size=0x%llx)", mself_fd, flags, sdata_fd, offset, arg, size); + cellFs.Todo("cellFsSdataOpenByFd(mself_fd=0x%x, flags=%#o, sdata_fd=*0x%x, offset=0x%llx, arg=*0x%x, size=0x%llx)", mself_fd, flags, sdata_fd, offset, arg, size); // TODO: diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index a4bce07b9b..165940c6d2 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -20,19 +20,13 @@ SysCallBase sys_fs("sys_fs"); -s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, CellFsMode mode, vm::ptr arg, u64 size) +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, vm::ptr arg, u64 size) { - sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%d, fd=*0x%x, mode=%d, arg=*0x%x, size=0x%llx)", path, flags, fd, mode, arg, size); + sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%#o, fd=*0x%x, mode=%#o, arg=*0x%x, size=0x%llx)", path, flags, fd, mode, arg, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); std::shared_ptr file; - if (mode) - { - sys_fs.Error("sys_fs_open(): unknown mode (%d)", mode); - //return CELL_FS_EINVAL; - } - // TODO: other checks for path if (Emu.GetVFS().ExistsDir(path.get_ptr())) @@ -48,10 +42,40 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, CellFsMode file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsRead)); break; } - //case CELL_FS_O_WRONLY: - //case CELL_FS_O_WRONLY | CELL_FS_O_CREAT: + + case CELL_FS_O_WRONLY: + { + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); + + if (file) + { + file->Seek(0); + } + + break; + } + + case CELL_FS_O_WRONLY | CELL_FS_O_CREAT: + { + Emu.GetVFS().CreateFile(path.get_ptr()); + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); + + if (file) + { + file->Seek(0); + } + + break; + } + + case CELL_FS_O_WRONLY | CELL_FS_O_APPEND: + { + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); + break; + } case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_EXCL: + case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_EXCL: // ??? { file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteExcl)); @@ -62,6 +86,7 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, CellFsMode break; } + case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: { Emu.GetVFS().CreateFile(path.get_ptr()); @@ -75,30 +100,37 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, CellFsMode file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); break; } + case CELL_FS_O_RDWR: { file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); break; } + case CELL_FS_O_RDWR | CELL_FS_O_CREAT: { Emu.GetVFS().CreateFile(path.get_ptr()); file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); break; } - //case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_EXCL: - //case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: + + case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: + { + Emu.GetVFS().CreateFile(path.get_ptr(), true); + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); + break; + } default: { - sys_fs.Error("sys_fs_open(): invalid or unimplemented flags (%d)", flags); + sys_fs.Error("sys_fs_open(): invalid or unimplemented flags (%#o)", flags); return CELL_FS_EINVAL; } } if (!file || !file->IsOpened()) { - sys_fs.Error("sys_fs_open(): failed to open '%s' (flags=%d, mode=%d)", path.get_ptr(), flags, mode); + sys_fs.Error("sys_fs_open(): failed to open '%s' (flags=%#o, mode=%#o)", path.get_ptr(), flags, mode); return CELL_FS_ENOENT; } @@ -169,6 +201,7 @@ s32 sys_fs_opendir(vm::ptr path, vm::ptr fd) if (!directory || !directory->IsOpened()) { + sys_fs.Error("sys_fs_opendir(): failed to open '%s'", path.get_ptr()); return CELL_FS_ENOENT; } @@ -329,9 +362,9 @@ s32 sys_fs_fstat(u32 fd, vm::ptr sb) return CELL_OK; } -s32 sys_fs_mkdir(vm::ptr path, CellFsMode mode) +s32 sys_fs_mkdir(vm::ptr path, s32 mode) { - sys_fs.Warning("sys_fs_mkdir(path=*0x%x, mode=%d)", path, mode); + sys_fs.Warning("sys_fs_mkdir(path=*0x%x, mode=%#o)", path, mode); sys_fs.Warning("*** path = '%s'", path.get_ptr()); const std::string _path = path.get_ptr(); @@ -528,9 +561,9 @@ s32 sys_fs_ftruncate(u32 fd, u64 size) return CELL_OK; } -s32 sys_fs_chmod(vm::ptr path, CellFsMode mode) +s32 sys_fs_chmod(vm::ptr path, s32 mode) { - sys_fs.Todo("sys_fs_chmod(path=*0x%x, mode=%d) -> CELL_OK", path, mode); + sys_fs.Todo("sys_fs_chmod(path=*0x%x, mode=%#o) -> CELL_OK", path, mode); sys_fs.Todo("*** path = '%s'", path.get_ptr()); return CELL_OK; diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.h b/rpcs3/Emu/SysCalls/lv2/sys_fs.h index 5e53d46895..7515e7f7b5 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.h @@ -145,7 +145,7 @@ struct CellFsUtimbuf #pragma pack(pop) // SysCalls -s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, CellFsMode mode, vm::ptr arg, u64 size); +s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, vm::ptr arg, u64 size); s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread); s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite); s32 sys_fs_close(u32 fd); @@ -154,7 +154,7 @@ s32 sys_fs_readdir(u32 fd, vm::ptr dir, vm::ptr nread); s32 sys_fs_closedir(u32 fd); s32 sys_fs_stat(vm::ptr path, vm::ptr sb); s32 sys_fs_fstat(u32 fd, vm::ptr sb); -s32 sys_fs_mkdir(vm::ptr path, CellFsMode mode); +s32 sys_fs_mkdir(vm::ptr path, s32 mode); s32 sys_fs_rename(vm::ptr from, vm::ptr to); s32 sys_fs_rmdir(vm::ptr path); s32 sys_fs_unlink(vm::ptr path); @@ -163,4 +163,4 @@ s32 sys_fs_fget_block_size(u32 fd, vm::ptr sector_size, vm::ptr block_ s32 sys_fs_get_block_size(vm::ptr path, vm::ptr sector_size, vm::ptr block_size, vm::ptr arg4); s32 sys_fs_truncate(vm::ptr path, u64 size); s32 sys_fs_ftruncate(u32 fd, u64 size); -s32 sys_fs_chmod(vm::ptr path, CellFsMode mode); +s32 sys_fs_chmod(vm::ptr path, s32 mode); From 9bc5ce300ee0caa63854a791b6e3bca813974530 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 14 Mar 2015 01:05:48 +0300 Subject: [PATCH 16/20] cellFsWriteWithOffset --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 77 ++++++++++++++++++++------- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 44 +++++---------- 2 files changed, 72 insertions(+), 49 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 4c52cb93f0..58eb683b49 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -267,6 +267,64 @@ s32 cellFsGetDirectoryEntries(u32 fd, vm::ptr entries, u32 return CELL_OK; } +s32 cellFsReadWithOffset(u32 fd, u64 offset, vm::ptr buf, u64 buffer_size, vm::ptr nread) +{ + cellFs.Log("cellFsReadWithOffset(fd=0x%x, offset=0x%llx, buf=*0x%x, buffer_size=0x%llx, nread=*0x%x)", fd, offset, buf, buffer_size, nread); + + // TODO: use single sys_fs_fcntl syscall + + std::shared_ptr file; + + if (!Emu.GetIdManager().GetIDData(fd, file)) + { + return CELL_FS_EBADF; + } + + const auto old_position = file->Tell(); + + file->Seek(offset); + + const auto read = file->Read(buf.get_ptr(), buffer_size); + + file->Seek(old_position); + + if (nread) + { + *nread = read; + } + + return CELL_OK; +} + +s32 cellFsWriteWithOffset(u32 fd, u64 offset, vm::ptr buf, u64 data_size, vm::ptr nwrite) +{ + cellFs.Log("cellFsWriteWithOffset(fd=0x%x, offset=0x%llx, buf=*0x%x, data_size=0x%llx, nwrite=*0x%x)", fd, offset, buf, data_size, nwrite); + + // TODO: use single sys_fs_fcntl syscall + + std::shared_ptr file; + + if (!Emu.GetIdManager().GetIDData(fd, file)) + { + return CELL_FS_EBADF; + } + + const auto old_position = file->Tell(); + + file->Seek(offset); + + const auto written = file->Write(buf.get_ptr(), data_size); + + file->Seek(old_position); + + if (nwrite) + { + *nwrite = written; + } + + return CELL_OK; +} + s32 cellFsStReadInit(u32 fd, vm::ptr ringbuf) { cellFs.Warning("cellFsStReadInit(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf); @@ -687,24 +745,6 @@ s32 cellFsAioFinish(vm::ptr mount_point) return CELL_OK; } -s32 cellFsReadWithOffset(PPUThread& CPU, u32 fd, u64 offset, vm::ptr buf, u64 buffer_size, vm::ptr nread) -{ - cellFs.Warning("cellFsReadWithOffset(fd=%d, offset=0x%llx, buf=*0x%x, buffer_size=%lld, nread=*0x%llx)", fd, offset, buf, buffer_size, nread); - - int ret; - vm::stackvar> oldPos(CPU), newPos(CPU); - ret = cellFsLseek(fd, 0, CELL_FS_SEEK_CUR, oldPos); // Save the current position - if (ret) return ret; - ret = cellFsLseek(fd, offset, CELL_FS_SEEK_SET, newPos); // Move to the specified offset - if (ret) return ret; - ret = cellFsRead(CPU, fd, buf, buffer_size, nread); // Read the file - if (ret) return ret; - ret = cellFsLseek(fd, oldPos.value(), CELL_FS_SEEK_SET, newPos); // Return to the old position - if (ret) return ret; - - return CELL_OK; -} - s32 cellFsSetDefaultContainer(u32 id, u32 total_limit) { cellFs.Todo("cellFsSetDefaultContainer(id=%d, total_limit=%d)", id, total_limit); @@ -757,6 +797,7 @@ Module cellFs("cellFs", []() REG_FUNC(cellFs, cellFsGetBlockSize); REG_FUNC(cellFs, cellFsGetFreeSize); REG_FUNC(cellFs, cellFsReadWithOffset); + REG_FUNC(cellFs, cellFsWriteWithOffset); REG_FUNC(cellFs, cellFsGetDirectoryEntries); REG_FUNC(cellFs, cellFsStReadInit); REG_FUNC(cellFs, cellFsStReadFinish); diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index 165940c6d2..e17505e4ce 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -44,27 +44,17 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, } case CELL_FS_O_WRONLY: + case CELL_FS_O_RDWR: { - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); - - if (file) - { - file->Seek(0); - } - + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); break; } case CELL_FS_O_WRONLY | CELL_FS_O_CREAT: + case CELL_FS_O_RDWR | CELL_FS_O_CREAT: { Emu.GetVFS().CreateFile(path.get_ptr()); - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); - - if (file) - { - file->Seek(0); - } - + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); break; } @@ -87,6 +77,12 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, break; } + case CELL_FS_O_WRONLY | CELL_FS_O_TRUNC: + { + file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWrite)); + break; + } + case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: { Emu.GetVFS().CreateFile(path.get_ptr()); @@ -100,19 +96,6 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); break; } - - case CELL_FS_O_RDWR: - { - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); - break; - } - - case CELL_FS_O_RDWR | CELL_FS_O_CREAT: - { - Emu.GetVFS().CreateFile(path.get_ptr()); - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); - break; - } case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: { @@ -166,6 +149,8 @@ s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrit return CELL_FS_EBADF; // TODO: return if not opened for writing } + // TODO: return CELL_FS_EBUSY if locked + *nwrite = file->Write(buf.get_ptr(), nbytes); return CELL_OK; @@ -182,10 +167,7 @@ s32 sys_fs_close(u32 fd) return CELL_FS_EBADF; } - if (false) - { - return CELL_FS_EBUSY; // TODO: return if locked - } + // TODO: return CELL_FS_EBUSY if locked Emu.GetIdManager().RemoveID(fd); From ea8473943b96518c5e64c482be3b3a05faa7e1ce Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 14 Mar 2015 03:38:36 +0300 Subject: [PATCH 17/20] Additional layer for opened file objects --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 60 +++++++++++------------ rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp | 19 +++---- rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp | 19 +++---- rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp | 19 +++---- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 48 +++++++++--------- rpcs3/Emu/SysCalls/lv2/sys_fs.h | 14 ++++++ 6 files changed, 99 insertions(+), 80 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 58eb683b49..42d876804c 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -273,20 +273,20 @@ s32 cellFsReadWithOffset(u32 fd, u64 offset, vm::ptr buf, u64 buffer_size, // TODO: use single sys_fs_fcntl syscall - std::shared_ptr file; + std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) + if (!Emu.GetIdManager().GetIDData(fd, file) || file->flags & CELL_FS_O_WRONLY) { return CELL_FS_EBADF; } - const auto old_position = file->Tell(); + const auto old_position = file->file->Tell(); - file->Seek(offset); + file->file->Seek(offset); - const auto read = file->Read(buf.get_ptr(), buffer_size); + const auto read = file->file->Read(buf.get_ptr(), buffer_size); - file->Seek(old_position); + file->file->Seek(old_position); if (nread) { @@ -302,20 +302,20 @@ s32 cellFsWriteWithOffset(u32 fd, u64 offset, vm::ptr buf, u64 data_ // TODO: use single sys_fs_fcntl syscall - std::shared_ptr file; + std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) + if (!Emu.GetIdManager().GetIDData(fd, file) || !(file->flags & CELL_FS_O_ACCMODE)) { return CELL_FS_EBADF; } - const auto old_position = file->Tell(); + const auto old_position = file->file->Tell(); - file->Seek(offset); + file->file->Seek(offset); - const auto written = file->Write(buf.get_ptr(), data_size); + const auto written = file->file->Write(buf.get_ptr(), data_size); - file->Seek(old_position); + file->file->Seek(old_position); if (nwrite) { @@ -329,7 +329,7 @@ s32 cellFsStReadInit(u32 fd, vm::ptr ringbuf) { cellFs.Warning("cellFsStReadInit(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -354,7 +354,7 @@ s32 cellFsStReadFinish(u32 fd) { cellFs.Warning("cellFsStReadFinish(fd=0x%x)", fd); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -368,7 +368,7 @@ s32 cellFsStReadGetRingBuf(u32 fd, vm::ptr ringbuf) { cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -383,7 +383,7 @@ s32 cellFsStReadGetStatus(u32 fd, vm::ptr status) { cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, status=*0x%x)", fd, status); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -396,7 +396,7 @@ s32 cellFsStReadGetRegid(u32 fd, vm::ptr regid) { cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, regid=*0x%x)", fd, regid); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -409,7 +409,7 @@ s32 cellFsStReadStart(u32 fd, u64 offset, u64 size) { cellFs.Todo("cellFsStReadStart(fd=0x%x, offset=0x%llx, size=0x%llx)", fd, offset, size); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -423,7 +423,7 @@ s32 cellFsStReadStop(u32 fd) { cellFs.Warning("cellFsStReadStop(fd=0x%x)", fd); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -436,17 +436,17 @@ s32 cellFsStRead(u32 fd, vm::ptr buf, u64 size, vm::ptr rsize) { cellFs.Warning("cellFsStRead(fd=0x%x, buf=*0x%x, size=0x%llx, rsize=*0x%x)", fd, buf, size, rsize); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; // TODO: use ringbuffer (fs_config) fs_config.m_regid += size; - if (file->Eof()) + if (file->file->Eof()) return CELL_FS_ERANGE; - *rsize = file->Read(buf.get_ptr(), size); + *rsize = file->file->Read(buf.get_ptr(), size); return CELL_OK; } @@ -455,7 +455,7 @@ s32 cellFsStReadGetCurrentAddr(u32 fd, vm::ptr> addr, vm::ptr s { cellFs.Todo("cellFsStReadGetCurrentAddr(fd=0x%x, addr=*0x%x, size=*0x%x)", fd, addr, size); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -466,7 +466,7 @@ s32 cellFsStReadPutCurrentAddr(u32 fd, vm::ptr addr, u64 size) { cellFs.Todo("cellFsStReadPutCurrentAddr(fd=0x%x, addr=*0x%x, size=0x%llx)", fd, addr, size); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -477,7 +477,7 @@ s32 cellFsStReadWait(u32 fd, u64 size) { cellFs.Todo("cellFsStReadWait(fd=0x%x, size=0x%llx)", fd, size); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -488,7 +488,7 @@ s32 cellFsStReadWaitCallback(u32 fd, u64 size, vm::ptr { cellFs.Todo("cellFsStReadWaitCallback(fd=0x%x, size=0x%llx, func=*0x%x)", fd, size, func); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -653,7 +653,7 @@ void fsAioRead(u32 fd, vm::ptr aio, int xid, vm::ptr orig_file; + std::shared_ptr orig_file; if (!Emu.GetIdManager().GetIDData(fd, orig_file)) { cellFs.Error("Wrong fd (%s)", fd); @@ -663,7 +663,7 @@ void fsAioRead(u32 fd, vm::ptr aio, int xid, vm::ptrsize; - vfsStream& file = *orig_file; + vfsStream& file = *orig_file->file; const u64 old_pos = file.Tell(); file.Seek((u64)aio->offset); @@ -702,7 +702,7 @@ s32 cellFsAioRead(vm::ptr aio, vm::ptr id, vm::ptr orig_file; + std::shared_ptr orig_file; u32 fd = aio->fd; if (!Emu.GetIdManager().GetIDData(fd, orig_file)) @@ -756,7 +756,7 @@ s32 cellFsSetIoBufferFromDefaultContainer(u32 fd, u32 buffer_size, u32 page_type { cellFs.Todo("cellFsSetIoBufferFromDefaultContainer(fd=%d, buffer_size=%d, page_type=%d)", fd, buffer_size, page_type); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; diff --git a/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp index 28d570fbfe..ce29a0f55e 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellGifDec.cpp @@ -9,6 +9,7 @@ #include "Emu/FS/VFS.h" #include "Emu/FS/vfsFileBase.h" +#include "Emu/SysCalls/lv2/sys_fs.h" #include "cellGifDec.h" @@ -44,10 +45,10 @@ int cellGifDecOpen(u32 mainHandle, vm::ptr subHandle, vm::ptr file(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); + std::shared_ptr file(new fs_file_t(std::shared_ptr(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)), 0, 0)); if (!file) return CELL_GIFDEC_ERROR_OPEN_FILE; current_subHandle->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); - current_subHandle->fileSize = file->GetSize(); + current_subHandle->fileSize = file->file->GetSize(); break; } } @@ -82,9 +83,9 @@ int cellGifDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr case se32(CELL_GIFDEC_FILE): { - auto file = Emu.GetIdManager().GetIDData(fd); - file->Seek(0); - file->Read(buffer.begin(), buffer.size()); + auto file = Emu.GetIdManager().GetIDData(fd); + file->file->Seek(0); + file->file->Read(buffer.begin(), buffer.size()); break; } } @@ -166,9 +167,9 @@ int cellGifDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr data, vm::pt case se32(CELL_GIFDEC_FILE): { - auto file = Emu.GetIdManager().GetIDData(fd); - file->Seek(0); - file->Read(gif.ptr(), gif.size()); + auto file = Emu.GetIdManager().GetIDData(fd); + file->file->Seek(0); + file->file->Read(gif.ptr(), gif.size()); break; } } @@ -268,7 +269,7 @@ int cellGifDecClose(u32 mainHandle, u32 subHandle) if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data)) return CELL_GIFDEC_ERROR_FATAL; - Emu.GetIdManager().RemoveID(subHandle_data->fd); + Emu.GetIdManager().RemoveID(subHandle_data->fd); Emu.GetIdManager().RemoveID(subHandle); return CELL_OK; diff --git a/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp index 62ac42645f..c2b58805d5 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellJpgDec.cpp @@ -8,6 +8,7 @@ #include "Emu/FS/VFS.h" #include "Emu/FS/vfsFileBase.h" +#include "Emu/SysCalls/lv2/sys_fs.h" #include "cellJpgDec.h" @@ -50,10 +51,10 @@ int cellJpgDecOpen(u32 mainHandle, vm::ptr subHandle, vm::ptr file(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); + std::shared_ptr file(new fs_file_t(std::shared_ptr(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)), 0, 0)); if (!file) return CELL_JPGDEC_ERROR_OPEN_FILE; current_subHandle->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); - current_subHandle->fileSize = file->GetSize(); + current_subHandle->fileSize = file->file->GetSize(); break; } } @@ -73,7 +74,7 @@ int cellJpgDecClose(u32 mainHandle, u32 subHandle) if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data)) return CELL_JPGDEC_ERROR_FATAL; - Emu.GetIdManager().RemoveID(subHandle_data->fd); + Emu.GetIdManager().RemoveID(subHandle_data->fd); Emu.GetIdManager().RemoveID(subHandle); return CELL_OK; @@ -102,9 +103,9 @@ int cellJpgDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr case se32(CELL_JPGDEC_FILE): { - auto file = Emu.GetIdManager().GetIDData(fd); - file->Seek(0); - file->Read(buffer.ptr(), buffer.size()); + auto file = Emu.GetIdManager().GetIDData(fd); + file->file->Seek(0); + file->file->Read(buffer.ptr(), buffer.size()); break; } } @@ -173,9 +174,9 @@ int cellJpgDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr data, vm::pt case se32(CELL_JPGDEC_FILE): { - auto file = Emu.GetIdManager().GetIDData(fd); - file->Seek(0); - file->Read(jpg.ptr(), jpg.size()); + auto file = Emu.GetIdManager().GetIDData(fd); + file->file->Seek(0); + file->file->Read(jpg.ptr(), jpg.size()); break; } } diff --git a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp index 2b201f20a0..b3de15a608 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp @@ -8,6 +8,7 @@ #include "Emu/FS/VFS.h" #include "Emu/FS/vfsFileBase.h" +#include "Emu/SysCalls/lv2/sys_fs.h" #include "cellPngDec.h" @@ -81,10 +82,10 @@ s32 pngDecOpen( case se32(CELL_PNGDEC_FILE): { // Get file descriptor and size - std::shared_ptr file(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); + std::shared_ptr file(new fs_file_t(std::shared_ptr(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)), 0, 0)); if (!file) return CELL_PNGDEC_ERROR_OPEN_FILE; stream->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); - stream->fileSize = file->GetSize(); + stream->fileSize = file->file->GetSize(); break; } } @@ -110,7 +111,7 @@ s32 pngDecOpen( s32 pngDecClose(CellPngDecSubHandle stream) { - Emu.GetIdManager().RemoveID(stream->fd); + Emu.GetIdManager().RemoveID(stream->fd); if (!Memory.Free(stream.addr())) { @@ -143,9 +144,9 @@ s32 pngReadHeader( break; case se32(CELL_PNGDEC_FILE): { - auto file = Emu.GetIdManager().GetIDData(stream->fd); - file->Seek(0); - file->Read(buffer.begin(), buffer.size()); + auto file = Emu.GetIdManager().GetIDData(stream->fd); + file->file->Seek(0); + file->file->Read(buffer.begin(), buffer.size()); break; } } @@ -255,9 +256,9 @@ s32 pngDecodeData( case se32(CELL_PNGDEC_FILE): { - auto file = Emu.GetIdManager().GetIDData(stream->fd); - file->Seek(0); - file->Read(png.ptr(), png.size()); + auto file = Emu.GetIdManager().GetIDData(stream->fd); + file->file->Seek(0); + file->file->Read(png.ptr(), png.size()); break; } } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index e17505e4ce..4b4a0d5ca6 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -116,8 +116,10 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, sys_fs.Error("sys_fs_open(): failed to open '%s' (flags=%#o, mode=%#o)", path.get_ptr(), flags, mode); return CELL_FS_ENOENT; } + + std::shared_ptr file_handler(new fs_file_t(file, mode, flags)); - *fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE); + *fd = Emu.GetIdManager().GetNewID(file_handler, TYPE_FS_FILE); return CELL_OK; } @@ -126,14 +128,14 @@ s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread) { sys_fs.Log("sys_fs_read(fd=0x%x, buf=0x%x, nbytes=0x%llx, nread=0x%x)", fd, buf, nbytes, nread); - std::shared_ptr file; + std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) + if (!Emu.GetIdManager().GetIDData(fd, file) || file->flags & CELL_FS_O_WRONLY) { - return CELL_FS_EBADF; // TODO: return if not opened for reading + return CELL_FS_EBADF; } - *nread = file->Read(buf.get_ptr(), nbytes); + *nread = file->file->Read(buf.get_ptr(), nbytes); return CELL_OK; } @@ -142,16 +144,16 @@ s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrit { sys_fs.Log("sys_fs_write(fd=0x%x, buf=*0x%x, nbytes=0x%llx, nwrite=*0x%x)", fd, buf, nbytes, nwrite); - std::shared_ptr file; + std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(fd, file)) + if (!Emu.GetIdManager().GetIDData(fd, file) || !(file->flags & CELL_FS_O_ACCMODE)) { - return CELL_FS_EBADF; // TODO: return if not opened for writing + return CELL_FS_EBADF; } // TODO: return CELL_FS_EBUSY if locked - *nwrite = file->Write(buf.get_ptr(), nbytes); + *nwrite = file->file->Write(buf.get_ptr(), nbytes); return CELL_OK; } @@ -160,7 +162,7 @@ s32 sys_fs_close(u32 fd) { sys_fs.Log("sys_fs_close(fd=0x%x)", fd); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) { @@ -169,7 +171,7 @@ s32 sys_fs_close(u32 fd) // TODO: return CELL_FS_EBUSY if locked - Emu.GetIdManager().RemoveID(fd); + Emu.GetIdManager().RemoveID(fd); return CELL_OK; } @@ -323,7 +325,7 @@ s32 sys_fs_fstat(u32 fd, vm::ptr sb) { sys_fs.Warning("sys_fs_fstat(fd=0x%x, sb=*0x%x)", fd, sb); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -338,7 +340,7 @@ s32 sys_fs_fstat(u32 fd, vm::ptr sb) sb->atime = 0; //TODO sb->mtime = 0; //TODO sb->ctime = 0; //TODO - sb->size = file->GetSize(); + sb->size = file->file->GetSize(); sb->blksize = 4096; return CELL_OK; @@ -451,11 +453,11 @@ s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr pos) return CELL_EINVAL; } - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; - *pos = file->Seek(offset, seek_mode); + *pos = file->file->Seek(offset, seek_mode); return CELL_OK; } @@ -463,7 +465,7 @@ s32 sys_fs_fget_block_size(u32 fd, vm::ptr sector_size, vm::ptr block_ { sys_fs.Todo("sys_fs_fget_block_size(fd=%d, sector_size=*0x%x, block_size=*0x%x, arg4=*0x%x, arg5=*0x%x)", fd, sector_size, block_size, arg4, arg5); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; @@ -519,20 +521,20 @@ s32 sys_fs_ftruncate(u32 fd, u64 size) { sys_fs.Warning("sys_fs_ftruncate(fd=0x%x, size=0x%llx)", fd, size); - std::shared_ptr file; + std::shared_ptr file; if (!Emu.GetIdManager().GetIDData(fd, file)) return CELL_ESRCH; - u64 initialSize = file->GetSize(); + u64 initialSize = file->file->GetSize(); if (initialSize < size) { - u64 last_pos = file->Tell(); - file->Seek(0, vfsSeekEnd); + u64 last_pos = file->file->Tell(); + file->file->Seek(0, vfsSeekEnd); static const char nullbyte = 0; - file->Seek(size - initialSize - 1, vfsSeekCur); - file->Write(&nullbyte, sizeof(char)); - file->Seek(last_pos, vfsSeekSet); + file->file->Seek(size - initialSize - 1, vfsSeekCur); + file->file->Write(&nullbyte, sizeof(char)); + file->file->Seek(last_pos, vfsSeekSet); } if (initialSize > size) diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.h b/rpcs3/Emu/SysCalls/lv2/sys_fs.h index 7515e7f7b5..08f5d86773 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.h +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.h @@ -144,6 +144,20 @@ struct CellFsUtimbuf #pragma pack(pop) +struct fs_file_t +{ + const std::shared_ptr file; + const s32 mode; + const s32 flags; + + fs_file_t(std::shared_ptr& file, s32 mode, s32 flags) + : file(file) + , mode(mode) + , flags(flags) + { + } +}; + // SysCalls s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, vm::ptr arg, u64 size); s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread); From 5cb1a2f26d5c24d10629b6615e65c0cd9f6e5c3d Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 14 Mar 2015 14:29:26 +0300 Subject: [PATCH 18/20] cellFsAioWrite implemented --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 185 ++++++++++++++------------ 1 file changed, 100 insertions(+), 85 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 42d876804c..971dca28b9 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -634,97 +634,70 @@ s32 cellFsSdataOpenByFd(u32 mself_fd, s32 flags, vm::ptr sdata_fd, u64 offs return CELL_OK; } -std::atomic g_FsAioReadID(0); -std::atomic g_FsAioReadCur(0); -bool aio_init = false; +std::mutex g_fs_aio_mutex; -void fsAioRead(u32 fd, vm::ptr aio, int xid, vm::ptr xaio, int error, int xid, u64 size)> func) +using fs_aio_cb_t = vm::ptr xaio, s32 error, s32 xid, u64 size)>; + +void fsAioRead(vm::ptr aio, s32 xid, fs_aio_cb_t func) { - while (g_FsAioReadCur != xid) + std::lock_guard lock(g_fs_aio_mutex); + + s32 error = CELL_OK; + u64 nread = 0; + + std::shared_ptr file; + + if (!Emu.GetIdManager().GetIDData(aio->fd, file) || file->flags & CELL_FS_O_WRONLY) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack - if (Emu.IsStopped()) - { - cellFs.Warning("fsAioRead() aborted"); - return; - } + error = CELL_FS_EBADF; + } + else + { + const auto old_position = file->file->Tell(); + + file->file->Seek(aio->offset); + + nread = file->file->Read(aio->buf.get_ptr(), aio->size); + + file->file->Seek(old_position); } - u32 error = CELL_OK; - u64 res = 0; + // should be executed directly by FS AIO thread + Emu.GetCallbackManager().Async([func, aio, error, xid, nread](PPUThread& CPU) { - std::shared_ptr orig_file; - if (!Emu.GetIdManager().GetIDData(fd, orig_file)) - { - cellFs.Error("Wrong fd (%s)", fd); - Emu.Pause(); - return; - } - - u64 nbytes = aio->size; - - vfsStream& file = *orig_file->file; - const u64 old_pos = file.Tell(); - file.Seek((u64)aio->offset); - - if (nbytes != (u32)nbytes) - { - error = CELL_ENOMEM; - } - else - { - res = nbytes ? file.Read(aio->buf.get_ptr(), nbytes) : 0; - } - - file.Seek(old_pos); - - cellFs.Log("*** fsAioRead(fd=%d, offset=0x%llx, buf=0x%x, size=0x%llx, error=0x%x, res=0x%llx, xid=0x%x)", - fd, aio->offset, aio->buf, aio->size, error, res, xid); - } - - if (func) - { - Emu.GetCallbackManager().Async([func, aio, error, xid, res](PPUThread& CPU) - { - func(CPU, aio, error, xid, res); - }); - } - - g_FsAioReadCur++; + func(CPU, aio, error, xid, nread); + }); } -s32 cellFsAioRead(vm::ptr aio, vm::ptr id, vm::ptr xaio, s32 error, s32 xid, u64 size)> func) +void fsAioWrite(vm::ptr aio, s32 xid, fs_aio_cb_t func) { - cellFs.Warning("cellFsAioRead(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); + std::lock_guard lock(g_fs_aio_mutex); - if (!aio_init) + s32 error = CELL_OK; + u64 nwritten = 0; + + std::shared_ptr file; + + if (!Emu.GetIdManager().GetIDData(aio->fd, file) || !(file->flags & CELL_FS_O_ACCMODE)) { - return CELL_ENXIO; + error = CELL_FS_EBADF; + } + else + { + const auto old_position = file->file->Tell(); + + file->file->Seek(aio->offset); + + nwritten = file->file->Write(aio->buf.get_ptr(), aio->size); + + file->file->Seek(old_position); } - std::shared_ptr orig_file; - u32 fd = aio->fd; - - if (!Emu.GetIdManager().GetIDData(fd, orig_file)) + // should be executed directly by FS AIO thread + Emu.GetCallbackManager().Async([func, aio, error, xid, nwritten](PPUThread& CPU) { - return CELL_EBADF; - } - - //get a unique id for the callback (may be used by cellFsAioCancel) - const s32 xid = g_FsAioReadID++; - *id = xid; - - thread_t t("CellFsAio Reading Thread", std::bind(fsAioRead, fd, aio, xid, func)); - return CELL_OK; -} - -s32 cellFsAioWrite(vm::ptr aio, vm::ptr id, vm::ptr xaio, s32 error, s32 xid, u64 size)> func) -{ - cellFs.Todo("cellFsAioWrite(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); - - // TODO: - - return CELL_OK; + func(CPU, aio, error, xid, nwritten); + }); } s32 cellFsAioInit(vm::ptr mount_point) @@ -732,7 +705,8 @@ s32 cellFsAioInit(vm::ptr mount_point) cellFs.Warning("cellFsAioInit(mount_point=*0x%x)", mount_point); cellFs.Warning("*** mount_point = '%s'", mount_point.get_ptr()); - aio_init = true; + // TODO: create AIO thread (if not exists) for specified mount point + return CELL_OK; } @@ -741,10 +715,54 @@ s32 cellFsAioFinish(vm::ptr mount_point) cellFs.Warning("cellFsAioFinish(mount_point=*0x%x)", mount_point); cellFs.Warning("*** mount_point = '%s'", mount_point.get_ptr()); - //aio_init = false; + // TODO: delete existing AIO thread for specified mount point + return CELL_OK; } +std::atomic g_fs_aio_id(0); + +s32 cellFsAioRead(vm::ptr aio, vm::ptr id, fs_aio_cb_t func) +{ + cellFs.Warning("cellFsAioRead(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); + + if (!Emu.GetIdManager().CheckID(aio->fd)) + { + return CELL_FS_EBADF; + } + + // TODO: detect mount point and send AIO request to the AIO thread of this mount point + + thread_t("FS AIO Read Thread", std::bind(fsAioRead, aio, (*id = ++g_fs_aio_id), func)).detach(); + + return CELL_OK; +} + +s32 cellFsAioWrite(vm::ptr aio, vm::ptr id, fs_aio_cb_t func) +{ + cellFs.Todo("cellFsAioWrite(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); + + if (!Emu.GetIdManager().CheckID(aio->fd)) + { + return CELL_FS_EBADF; + } + + // TODO: detect mount point and send AIO request to the AIO thread of this mount point + + thread_t("FS AIO Write Thread", std::bind(fsAioWrite, aio, (*id = ++g_fs_aio_id), func)).detach(); + + return CELL_OK; +} + +s32 cellFsAioCancel(s32 id) +{ + cellFs.Todo("cellFsAioCancel(id=%d) -> CELL_FS_EINVAL", id); + + // TODO: cancelled requests return CELL_FS_ECANCELED through their own callbacks + + return CELL_FS_EINVAL; +} + s32 cellFsSetDefaultContainer(u32 id, u32 total_limit) { cellFs.Todo("cellFsSetDefaultContainer(id=%d, total_limit=%d)", id, total_limit); @@ -765,10 +783,6 @@ s32 cellFsSetIoBufferFromDefaultContainer(u32 fd, u32 buffer_size, u32 page_type Module cellFs("cellFs", []() { - g_FsAioReadID = 0; - g_FsAioReadCur = 0; - aio_init = false; - REG_FUNC(cellFs, cellFsOpen); REG_FUNC(cellFs, cellFsSdataOpen); REG_FUNC(cellFs, cellFsSdataOpenByFd); @@ -790,10 +804,11 @@ Module cellFs("cellFs", []() REG_FUNC(cellFs, cellFsFtruncate); REG_FUNC(cellFs, cellFsTruncate); REG_FUNC(cellFs, cellFsFGetBlockSize); - REG_FUNC(cellFs, cellFsAioRead); - REG_FUNC(cellFs, cellFsAioWrite); REG_FUNC(cellFs, cellFsAioInit); REG_FUNC(cellFs, cellFsAioFinish); + REG_FUNC(cellFs, cellFsAioRead); + REG_FUNC(cellFs, cellFsAioWrite); + REG_FUNC(cellFs, cellFsAioCancel); REG_FUNC(cellFs, cellFsGetBlockSize); REG_FUNC(cellFs, cellFsGetFreeSize); REG_FUNC(cellFs, cellFsReadWithOffset); From 614cb88116ffad78a7dc697a632c010ddc6fdf8e Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 14 Mar 2015 18:46:13 +0300 Subject: [PATCH 19/20] start func running --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 53 ++++++--------------------- rpcs3/Loader/ELF64.cpp | 49 +++++++++++++++++-------- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 971dca28b9..9bbb275ea6 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -638,16 +638,18 @@ std::mutex g_fs_aio_mutex; using fs_aio_cb_t = vm::ptr xaio, s32 error, s32 xid, u64 size)>; -void fsAioRead(vm::ptr aio, s32 xid, fs_aio_cb_t func) +void fsAio(vm::ptr aio, bool write, s32 xid, fs_aio_cb_t func) { std::lock_guard lock(g_fs_aio_mutex); + cellFs.Notice("FS AIO Request(%d): fd=0x%x, offset=0x%llx, buf=*0x%x, size=0x%llx, user_data=0x%llx", xid, aio->fd, aio->offset, aio->buf, aio->size, aio->user_data); + s32 error = CELL_OK; - u64 nread = 0; + u64 result = 0; std::shared_ptr file; - if (!Emu.GetIdManager().GetIDData(aio->fd, file) || file->flags & CELL_FS_O_WRONLY) + if (!Emu.GetIdManager().GetIDData(aio->fd, file) || (!write && file->flags & CELL_FS_O_WRONLY) || (write && !(file->flags & CELL_FS_O_ACCMODE))) { error = CELL_FS_EBADF; } @@ -657,46 +659,15 @@ void fsAioRead(vm::ptr aio, s32 xid, fs_aio_cb_t func) file->file->Seek(aio->offset); - nread = file->file->Read(aio->buf.get_ptr(), aio->size); + result = write ? file->file->Write(aio->buf.get_ptr(), aio->size) : file->file->Read(aio->buf.get_ptr(), aio->size); file->file->Seek(old_position); } // should be executed directly by FS AIO thread - Emu.GetCallbackManager().Async([func, aio, error, xid, nread](PPUThread& CPU) + Emu.GetCallbackManager().Async([func, aio, error, xid, result](PPUThread& CPU) { - func(CPU, aio, error, xid, nread); - }); -} - -void fsAioWrite(vm::ptr aio, s32 xid, fs_aio_cb_t func) -{ - std::lock_guard lock(g_fs_aio_mutex); - - s32 error = CELL_OK; - u64 nwritten = 0; - - std::shared_ptr file; - - if (!Emu.GetIdManager().GetIDData(aio->fd, file) || !(file->flags & CELL_FS_O_ACCMODE)) - { - error = CELL_FS_EBADF; - } - else - { - const auto old_position = file->file->Tell(); - - file->file->Seek(aio->offset); - - nwritten = file->file->Write(aio->buf.get_ptr(), aio->size); - - file->file->Seek(old_position); - } - - // should be executed directly by FS AIO thread - Emu.GetCallbackManager().Async([func, aio, error, xid, nwritten](PPUThread& CPU) - { - func(CPU, aio, error, xid, nwritten); + func(CPU, aio, error, xid, result); }); } @@ -733,14 +704,14 @@ s32 cellFsAioRead(vm::ptr aio, vm::ptr id, fs_aio_cb_t func) // TODO: detect mount point and send AIO request to the AIO thread of this mount point - thread_t("FS AIO Read Thread", std::bind(fsAioRead, aio, (*id = ++g_fs_aio_id), func)).detach(); + thread_t("FS AIO Read Thread", std::bind(fsAio, aio, false, (*id = ++g_fs_aio_id), func)).detach(); return CELL_OK; } s32 cellFsAioWrite(vm::ptr aio, vm::ptr id, fs_aio_cb_t func) { - cellFs.Todo("cellFsAioWrite(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); + cellFs.Warning("cellFsAioWrite(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func); if (!Emu.GetIdManager().CheckID(aio->fd)) { @@ -749,14 +720,14 @@ s32 cellFsAioWrite(vm::ptr aio, vm::ptr id, fs_aio_cb_t func) // TODO: detect mount point and send AIO request to the AIO thread of this mount point - thread_t("FS AIO Write Thread", std::bind(fsAioWrite, aio, (*id = ++g_fs_aio_id), func)).detach(); + thread_t("FS AIO Write Thread", std::bind(fsAio, aio, true, (*id = ++g_fs_aio_id), func)).detach(); return CELL_OK; } s32 cellFsAioCancel(s32 id) { - cellFs.Todo("cellFsAioCancel(id=%d) -> CELL_FS_EINVAL", id); + cellFs.Warning("cellFsAioCancel(id=%d) -> CELL_FS_EINVAL", id); // TODO: cancelled requests return CELL_FS_ECANCELED through their own callbacks diff --git a/rpcs3/Loader/ELF64.cpp b/rpcs3/Loader/ELF64.cpp index d4d6b6b01c..b75ace91b8 100644 --- a/rpcs3/Loader/ELF64.cpp +++ b/rpcs3/Loader/ELF64.cpp @@ -493,40 +493,59 @@ namespace loader ppu_thr_stop_data[1] = BLR(); Emu.SetCPUThreadStop(ppu_thr_stop_data.addr()); - /* - //TODO - static const int branch_size = 6 * 4; + static const int branch_size = 8 * 4; + auto make_branch = [](vm::ptr& ptr, u32 addr) { u32 stub = vm::read32(addr); u32 rtoc = vm::read32(addr + 4); - *ptr++ = implicts::LI(r0, stub >> 16); - *ptr++ = ORIS(r0, r0, stub & 0xffff); - *ptr++ = implicts::LI(r2, rtoc >> 16); - *ptr++ = ORIS(r2, r2, rtoc & 0xffff); + *ptr++ = LI_(r0, 0); + *ptr++ = ORI(r0, r0, stub & 0xffff); + *ptr++ = ORIS(r0, r0, stub >> 16); + *ptr++ = LI_(r2, 0); + *ptr++ = ORI(r2, r2, rtoc & 0xffff); + *ptr++ = ORIS(r2, r2, rtoc >> 16); *ptr++ = MTCTR(r0); *ptr++ = BCTRL(); }; - auto entry = vm::ptr::make(vm::alloc(branch_size * (start_funcs.size() + 1), vm::main)); + auto entry = vm::ptr::make(vm::alloc(56 + branch_size * (start_funcs.size() + 1), vm::main)); - auto OPD = vm::ptr::make(vm::alloc(2 * 4)); - OPD[0] = entry.addr(); - OPD[1] = 0; + const auto OPD = entry; + + // make initial OPD + *entry++ = OPD.addr() + 8; + *entry++ = 0xdeadbeef; + + // save initialization args + *entry++ = MR(r14, r3); + *entry++ = MR(r15, r4); + *entry++ = MR(r16, r5); + *entry++ = MR(r17, r6); + *entry++ = MR(r18, r11); + *entry++ = MR(r19, r12); for (auto &f : start_funcs) { make_branch(entry, f); } - make_branch(entry, m_ehdr.e_entry); - */ + // restore initialization args + *entry++ = MR(r3, r14); + *entry++ = MR(r4, r15); + *entry++ = MR(r5, r16); + *entry++ = MR(r6, r17); + *entry++ = MR(r11, r18); + *entry++ = MR(r12, r19); - ppu_thread main_thread(m_ehdr.e_entry, "main_thread"); + // branch to initialization + make_branch(entry, m_ehdr.e_entry); + + ppu_thread main_thread(OPD.addr(), "main_thread"); main_thread.args({ Emu.GetPath()/*, "-emu"*/ }).run(); - main_thread.gpr(11, m_ehdr.e_entry).gpr(12, Emu.GetMallocPageSize()); + main_thread.gpr(11, OPD.addr()).gpr(12, Emu.GetMallocPageSize()); return ok; } From 469926c5e1c44f8dc40d76f6f58b8c544f976152 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 14 Mar 2015 23:28:59 +0300 Subject: [PATCH 20/20] _sys_heap*, _sys_malloc+, _sys_strcmp implemented --- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 2 +- rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp | 168 ++++++++++++------- rpcs3/Emu/SysCalls/Modules/sysPrxForUser.h | 20 +-- 3 files changed, 117 insertions(+), 73 deletions(-) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 9bbb275ea6..ab5c8ad85c 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -665,7 +665,7 @@ void fsAio(vm::ptr aio, bool write, s32 xid, fs_aio_cb_t func) } // should be executed directly by FS AIO thread - Emu.GetCallbackManager().Async([func, aio, error, xid, result](PPUThread& CPU) + Emu.GetCallbackManager().Async([=](PPUThread& CPU) { func(CPU, aio, error, xid, result); }); diff --git a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp index 9f31519d78..34844ec5e5 100644 --- a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp +++ b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.cpp @@ -766,34 +766,45 @@ std::string ps3_fmt(PPUThread& context, vm::ptr fmt, u32 g_count, u3 return result; } -int _sys_heap_create_heap(const u32 heap_addr, const u32 align, const u32 size) +u32 _sys_heap_create_heap(vm::ptr name, u32 arg2, u32 arg3, u32 arg4) { - sysPrxForUser.Warning("_sys_heap_create_heap(heap_addr=0x%x, align=0x%x, size=0x%x)", heap_addr, align, size); + sysPrxForUser.Warning("_sys_heap_create_heap(name=*0x%x, arg2=0x%x, arg3=0x%x, arg4=0x%x)", name, arg2, arg3, arg4); - std::shared_ptr heap(new HeapInfo(heap_addr, align, size)); - u32 heap_id = Emu.GetIdManager().GetNewID(heap); - sysPrxForUser.Warning("*** sys_heap created: id = %d", heap_id); - return heap_id; + std::shared_ptr heap(new HeapInfo(name.get_ptr())); + + return Emu.GetIdManager().GetNewID(heap); } -u32 _sys_heap_malloc(const u32 heap_id, const u32 size) +s32 _sys_heap_delete_heap(u32 heap) { - sysPrxForUser.Warning("_sys_heap_malloc(heap_id=%d, size=0x%x)", heap_id, size); + sysPrxForUser.Warning("_sys_heap_delete_heap(heap=0x%x)", heap); - std::shared_ptr heap; - if(!Emu.GetIdManager().GetIDData(heap_id, heap)) return CELL_ESRCH; + Emu.GetIdManager().RemoveID(heap); - return (u32)Memory.Alloc(size, 1); + return CELL_OK; } -u32 _sys_heap_memalign(u32 heap_id, u32 align, u32 size) +u32 _sys_heap_malloc(u32 heap, u32 size) { - sysPrxForUser.Warning("_sys_heap_memalign(heap_id=%d, align=0x%x, size=0x%x)", heap_id, align, size); + sysPrxForUser.Warning("_sys_heap_malloc(heap=0x%x, size=0x%x)", heap, size); - std::shared_ptr heap; - if(!Emu.GetIdManager().GetIDData(heap_id, heap)) return CELL_ESRCH; + return Memory.MainMem.AllocAlign(size, 1); +} - return (u32)Memory.Alloc(size, align); +u32 _sys_heap_memalign(u32 heap, u32 align, u32 size) +{ + sysPrxForUser.Warning("_sys_heap_memalign(heap=0x%x, align=0x%x, size=0x%x)", heap, align, size); + + return Memory.MainMem.AllocAlign(size, align); +} + +s32 _sys_heap_free(u32 heap, u32 addr) +{ + sysPrxForUser.Warning("_sys_heap_free(heap=0x%x, addr=0x%x)", heap, addr); + + Memory.MainMem.Free(addr); + + return CELL_OK; } s64 _sys_process_atexitspawn() @@ -815,7 +826,7 @@ s32 sys_interrupt_thread_disestablish(PPUThread& CPU, u32 ih) return _sys_interrupt_thread_disestablish(ih, vm::stackvar>(CPU)); } -int sys_process_is_stack(u32 p) +s32 sys_process_is_stack(u32 p) { sysPrxForUser.Log("sys_process_is_stack(p=0x%x)", p); @@ -829,35 +840,36 @@ s64 sys_prx_exitspawn_with_level() return CELL_OK; } -int sys_spu_elf_get_information(u32 elf_img, vm::ptr entry, vm::ptr nseg) +s32 sys_spu_elf_get_information(u32 elf_img, vm::ptr entry, vm::ptr nseg) { sysPrxForUser.Todo("sys_spu_elf_get_information(elf_img=0x%x, entry_addr=0x%x, nseg_addr=0x%x)", elf_img, entry.addr(), nseg.addr()); return CELL_OK; } -int sys_spu_elf_get_segments(u32 elf_img, vm::ptr segments, int nseg) +s32 sys_spu_elf_get_segments(u32 elf_img, vm::ptr segments, s32 nseg) { sysPrxForUser.Todo("sys_spu_elf_get_segments(elf_img=0x%x, segments_addr=0x%x, nseg=0x%x)", elf_img, segments.addr(), nseg); return CELL_OK; } -int sys_spu_image_import(vm::ptr img, u32 src, u32 type) +s32 sys_spu_image_import(vm::ptr img, u32 src, u32 type) { - sysPrxForUser.Warning("sys_spu_image_import(img=0x%x, src=0x%x, type=%d)", img.addr(), src, type); + sysPrxForUser.Warning("sys_spu_image_import(img=*0x%x, src=0x%x, type=%d)", img, src, type); return spu_image_import(*img, src, type); } -int sys_spu_image_close(vm::ptr img) +s32 sys_spu_image_close(vm::ptr img) { - sysPrxForUser.Warning("sys_spu_image_close(img=0x%x)", img.addr()); + sysPrxForUser.Todo("sys_spu_image_close(img=*0x%x)", img); + return CELL_OK; } -int sys_raw_spu_load(s32 id, vm::ptr path, vm::ptr entry) +s32 sys_raw_spu_load(s32 id, vm::ptr path, vm::ptr entry) { - sysPrxForUser.Warning("sys_raw_spu_load(id=0x%x, path_addr=0x%x('%s'), entry_addr=0x%x)", - id, path.addr(), path.get_ptr(), entry.addr()); + sysPrxForUser.Warning("sys_raw_spu_load(id=%d, path=*0x%x, entry=*0x%x)", id, path, entry); + sysPrxForUser.Warning("*** path = '%s'", path.get_ptr()); vfsFile f(path.get_ptr()); if(!f.IsOpened()) @@ -886,9 +898,9 @@ int sys_raw_spu_load(s32 id, vm::ptr path, vm::ptr entry) return CELL_OK; } -int sys_raw_spu_image_load(int id, vm::ptr img) +s32 sys_raw_spu_image_load(s32 id, vm::ptr img) { - sysPrxForUser.Warning("sys_raw_spu_image_load(id=0x%x, img_addr=0x%x)", id, img.addr()); + sysPrxForUser.Warning("sys_raw_spu_image_load(id=%d, img=*0x%x)", id, img); // TODO: use segment info memcpy(vm::get_ptr(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * id), vm::get_ptr(img->addr), 256 * 1024); @@ -897,9 +909,9 @@ int sys_raw_spu_image_load(int id, vm::ptr img) return CELL_OK; } -int sys_get_random_number(vm::ptr addr, u64 size) +s32 sys_get_random_number(vm::ptr addr, u64 size) { - sysPrxForUser.Warning("sys_get_random_number(addr=0x%x, size=%d)", addr.addr(), size); + sysPrxForUser.Warning("sys_get_random_number(addr=*0x%x, size=%d)", addr, size); if (size > 4096) size = 4096; @@ -914,77 +926,89 @@ int sys_get_random_number(vm::ptr addr, u64 size) vm::ptr _sys_memset(vm::ptr dst, s32 value, u32 size) { - sysPrxForUser.Log("_sys_memset(dst_addr=0x%x, value=%d, size=%d)", dst.addr(), value, size); + sysPrxForUser.Log("_sys_memset(dst=*0x%x, value=%d, size=0x%x)", dst, value, size); memset(dst.get_ptr(), value, size); + return dst; } vm::ptr _sys_memcpy(vm::ptr dst, vm::ptr src, u32 size) { - sysPrxForUser.Log("_sys_memcpy(dst_addr=0x%x, src_addr=0x%x, size=%d)", dst.addr(), src.addr(), size); + sysPrxForUser.Log("_sys_memcpy(dst=*0x%x, src=*0x%x, size=0x%x)", dst, src, size); memcpy(dst.get_ptr(), src.get_ptr(), size); + return dst; } s32 _sys_memcmp(vm::ptr buf1, vm::ptr buf2, u32 size) { - sysPrxForUser.Log("_sys_memcmp(buf1_addr=0x%x, buf2_addr=0x%x, size=%d)", buf1.addr(), buf2.addr(), size); + sysPrxForUser.Log("_sys_memcmp(buf1=*0x%x, buf2=*0x%x, size=%d)", buf1, buf2, size); return memcmp(buf1.get_ptr(), buf2.get_ptr(), size); } s64 _sys_strlen(vm::ptr str) { - sysPrxForUser.Log("_sys_strlen(str_addr=0x%x)", str.addr()); + sysPrxForUser.Log("_sys_strlen(str=*0x%x)", str); return strlen(str.get_ptr()); } +s32 _sys_strcmp(vm::ptr str1, vm::ptr str2) +{ + sysPrxForUser.Log("_sys_strcmp(str1=*0x%x, str2=*0x%x)", str1, str2); + + return strcmp(str1.get_ptr(), str2.get_ptr()); +} + s32 _sys_strncmp(vm::ptr str1, vm::ptr str2, s32 max) { - sysPrxForUser.Log("_sys_strncmp(str1_addr=0x%x, str2_addr=0x%x, max=%d)", str1.addr(), str2.addr(), max); + sysPrxForUser.Log("_sys_strncmp(str1=*0x%x, str2=*0x%x, max=%d)", str1, str2, max); return strncmp(str1.get_ptr(), str2.get_ptr(), max); } vm::ptr _sys_strcat(vm::ptr dest, vm::ptr source) { - sysPrxForUser.Log("_sys_strcat(dest_addr=0x%x, source_addr=0x%x)", dest.addr(), source.addr()); + sysPrxForUser.Log("_sys_strcat(dest=*0x%x, source=*0x%x)", dest, source); if (strcat(dest.get_ptr(), source.get_ptr()) != dest.get_ptr()) { - assert(!"strcat(): unexpected result"); + throw "_sys_strcat() failed: unexpected strcat() result"; } + return dest; } vm::ptr _sys_strncat(vm::ptr dest, vm::ptr source, u32 len) { - sysPrxForUser.Log("_sys_strncat(dest_addr=0x%x, source_addr=0x%x, len=%d)", dest.addr(), source.addr(), len); + sysPrxForUser.Log("_sys_strncat(dest=*0x%x, source=*0x%x, len=%d)", dest, source, len); if (strncat(dest.get_ptr(), source.get_ptr(), len) != dest.get_ptr()) { - assert(!"strncat(): unexpected result"); + throw "_sys_strncat() failed: unexpected strncat() result"; } + return dest; } vm::ptr _sys_strcpy(vm::ptr dest, vm::ptr source) { - sysPrxForUser.Log("_sys_strcpy(dest_addr=0x%x, source_addr=0x%x)", dest.addr(), source.addr()); + sysPrxForUser.Log("_sys_strcpy(dest=*0x%x, source=*0x%x)", dest, source); if (strcpy(dest.get_ptr(), source.get_ptr()) != dest.get_ptr()) { - assert(!"strcpy(): unexpected result"); + throw "_sys_strcpy() failed: unexpected strcpy() result"; } + return dest; } vm::ptr _sys_strncpy(vm::ptr dest, vm::ptr source, u32 len) { - sysPrxForUser.Log("_sys_strncpy(dest_addr=0x%x, source_addr=0x%x, len=%d)", dest.addr(), source.addr(), len); + sysPrxForUser.Log("_sys_strncpy(dest=*0x%x, source=*0x%x, len=%d)", dest, source, len); if (!dest || !source) { @@ -993,24 +1017,20 @@ vm::ptr _sys_strncpy(vm::ptr dest, vm::ptr source, u32 l if (strncpy(dest.get_ptr(), source.get_ptr(), len) != dest.get_ptr()) { - assert(!"strncpy(): unexpected result"); + throw "_sys_strncpy() failed: unexpected strncpy() result"; } + return dest; } -vm::ptr spu_printf_agcb; -vm::ptr spu_printf_dgcb; -vm::ptr spu_printf_atcb; -vm::ptr spu_printf_dtcb; +spu_printf_cb_t spu_printf_agcb; +spu_printf_cb_t spu_printf_dgcb; +spu_printf_cb_t spu_printf_atcb; +spu_printf_cb_t spu_printf_dtcb; -s32 _sys_spu_printf_initialize( - vm::ptr agcb, - vm::ptr dgcb, - vm::ptr atcb, - vm::ptr dtcb) +s32 _sys_spu_printf_initialize(spu_printf_cb_t agcb, spu_printf_cb_t dgcb, spu_printf_cb_t atcb, spu_printf_cb_t dtcb) { - sysPrxForUser.Warning("_sys_spu_printf_initialize(agcb_addr=0x%x, dgcb_addr=0x%x, atcb_addr=0x%x, dtcb_addr=0x%x)", - agcb.addr(), dgcb.addr(), atcb.addr(), dtcb.addr()); + sysPrxForUser.Warning("_sys_spu_printf_initialize(agcb=*0x%x, dgcb=*0x%x, atcb=*0x%x, dtcb=*0x%x)", agcb, dgcb, atcb, dtcb); // prx: register some callbacks spu_printf_agcb = agcb; @@ -1079,9 +1099,32 @@ s32 _sys_spu_printf_detach_thread(PPUThread& CPU, u32 thread) return spu_printf_dtcb(CPU, thread); } +u32 _sys_malloc(u32 size) +{ + sysPrxForUser.Warning("_sys_malloc(size=0x%x)", size); + + return Memory.MainMem.AllocAlign(size, 1); +} + +u32 _sys_memalign(u32 align, u32 size) +{ + sysPrxForUser.Warning("_sys_memalign(align=0x%x, size=0x%x)", align, size); + + return Memory.MainMem.AllocAlign(size, align); +} + +s32 _sys_free(u32 addr) +{ + sysPrxForUser.Warning("_sys_free(addr=0x%x)", addr); + + Memory.MainMem.Free(addr); + + return CELL_OK; +} + s32 _sys_snprintf(PPUThread& CPU, vm::ptr dst, u32 count, vm::ptr fmt) // va_args... { - sysPrxForUser.Warning("_sys_snprintf(dst=0x%x, count=%d, fmt=0x%x, ...)", dst, count, fmt); + sysPrxForUser.Warning("_sys_snprintf(dst=*0x%x, count=%d, fmt=*0x%x, ...)", dst, count, fmt); std::string result = ps3_fmt(CPU, fmt, 3, 0, 0); @@ -1103,7 +1146,7 @@ s32 _sys_snprintf(PPUThread& CPU, vm::ptr dst, u32 count, vm::ptr fmt) // va_args... { - sysPrxForUser.Todo("_sys_printf(fmt_addr=0x%x, ...)", fmt.addr()); + sysPrxForUser.Todo("_sys_printf(fmt=*0x%x, ...)", fmt); // probably, assertion failed sysPrxForUser.Warning("_sys_printf: \n%s", fmt.get_ptr()); @@ -1113,7 +1156,7 @@ s32 _sys_printf(vm::ptr fmt) // va_args... s32 sys_process_get_paramsfo(vm::ptr buffer) { - sysPrxForUser.Warning("sys_process_get_paramsfo(buffer=0x%x)", buffer); + sysPrxForUser.Warning("sys_process_get_paramsfo(buffer=*0x%x)", buffer); // prx: load some data (0x40 bytes) previously set by _sys_process_get_paramsfo syscall return _sys_process_get_paramsfo(buffer); @@ -1224,11 +1267,11 @@ Module sysPrxForUser("sysPrxForUser", []() REG_FUNC(sysPrxForUser, sys_prx_load_module_on_memcontainer); REG_FUNC(sysPrxForUser, sys_prx_exitspawn_with_level); - REG_FUNC(sysPrxForUser, _sys_heap_malloc); - //REG_FUNC(sysPrxForUser, _sys_heap_free); - //REG_FUNC(sysPrxForUser, _sys_heap_delete_heap); REG_FUNC(sysPrxForUser, _sys_heap_create_heap); + REG_FUNC(sysPrxForUser, _sys_heap_delete_heap); + REG_FUNC(sysPrxForUser, _sys_heap_malloc); REG_FUNC(sysPrxForUser, _sys_heap_memalign); + REG_FUNC(sysPrxForUser, _sys_heap_free); REG_FUNC(sysPrxForUser, sys_mmapper_allocate_memory); REG_FUNC(sysPrxForUser, sys_mmapper_allocate_memory_from_container); @@ -1258,6 +1301,7 @@ Module sysPrxForUser("sysPrxForUser", []() REG_FUNC(sysPrxForUser, _sys_memcpy); REG_FUNC(sysPrxForUser, _sys_memcmp); REG_FUNC(sysPrxForUser, _sys_strlen); + REG_FUNC(sysPrxForUser, _sys_strcmp); REG_FUNC(sysPrxForUser, _sys_strncmp); REG_FUNC(sysPrxForUser, _sys_strcat); REG_FUNC(sysPrxForUser, _sys_strncat); @@ -1271,6 +1315,10 @@ Module sysPrxForUser("sysPrxForUser", []() REG_FUNC(sysPrxForUser, _sys_spu_printf_attach_thread); REG_FUNC(sysPrxForUser, _sys_spu_printf_detach_thread); + REG_FUNC(sysPrxForUser, _sys_malloc); + REG_FUNC(sysPrxForUser, _sys_memalign); + REG_FUNC(sysPrxForUser, _sys_free); + REG_FUNC(sysPrxForUser, _sys_snprintf); REG_FUNC(sysPrxForUser, _sys_printf); diff --git a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.h b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.h index 19e166545c..41aa474d9f 100644 --- a/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.h +++ b/rpcs3/Emu/SysCalls/Modules/sysPrxForUser.h @@ -2,25 +2,21 @@ struct HeapInfo { - u32 heap_addr; - u32 align; - u32 size; + const std::string name; - HeapInfo(u32 _heap_addr, u32 _align, u32 _size) - : heap_addr(_heap_addr) - , align(_align) - , size(_size) + HeapInfo(const char* name) + : name(name) { } }; -typedef s32(spu_printf_cb_t)(u32 arg); +using spu_printf_cb_t = vm::ptr; // Aux -extern vm::ptr spu_printf_agcb; -extern vm::ptr spu_printf_dgcb; -extern vm::ptr spu_printf_atcb; -extern vm::ptr spu_printf_dtcb; +extern spu_printf_cb_t spu_printf_agcb; +extern spu_printf_cb_t spu_printf_dgcb; +extern spu_printf_cb_t spu_printf_atcb; +extern spu_printf_cb_t spu_printf_dtcb; // Functions vm::ptr _sys_memset(vm::ptr dst, s32 value, u32 size);