Merge pull request #1035 from Nekotekina/master

sys_fs review, cleanup
This commit is contained in:
DHrpcs3 2015-03-14 23:07:47 +02:00
commit 1f05275ac4
54 changed files with 2396 additions and 1785 deletions

View file

@ -193,8 +193,8 @@ class squeue_t
public: public:
squeue_t() squeue_t()
: m_sync({})
{ {
m_sync.write_relaxed({});
} }
u32 get_max_size() const u32 get_max_size() const
@ -204,7 +204,7 @@ public:
bool is_full() const volatile 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<bool()>& test_exit) bool push(const T& data, const std::function<bool()>& test_exit)

View file

@ -1,6 +1,14 @@
#pragma once #pragma once
#include "SPUThread.h" #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) __forceinline static u32 GetRawSPURegAddrByNum(int num, int offset)
{ {
return RAW_SPU_OFFSET * num + RAW_SPU_BASE_ADDR + RAW_SPU_PROB_OFFSET + offset; return RAW_SPU_OFFSET * num + RAW_SPU_BASE_ADDR + RAW_SPU_PROB_OFFSET + offset;

View file

@ -79,28 +79,29 @@ void SPUThread::InitRegs()
mfc_queue.clear(); mfc_queue.clear();
ch_tag_mask = 0; ch_tag_mask = 0;
ch_tag_stat.data = {}; ch_tag_stat = {};
ch_stall_stat.data = {}; ch_stall_stat = {};
ch_atomic_stat.data = {}; ch_atomic_stat = {};
ch_in_mbox.clear(); ch_in_mbox.clear();
ch_out_mbox.data = {}; ch_out_mbox = {};
ch_out_intr_mbox.data = {}; ch_out_intr_mbox = {};
snr_config = 0; snr_config = 0;
ch_snr1.data = {}; ch_snr1 = {};
ch_snr2.data = {}; ch_snr2 = {};
ch_event_mask = 0; ch_event_mask = 0;
ch_event_stat.write_relaxed(0); ch_event_stat = {};
ch_dec_start_timestamp = get_time(); // ??? ch_dec_start_timestamp = get_time(); // ???
ch_dec_value = 0; ch_dec_value = 0;
run_ctrl.write_relaxed(0); run_ctrl = {};
status.write_relaxed(0); status = {};
npc = {};
int0.clear(); int0.clear();
int2.clear(); int2.clear();
@ -604,19 +605,17 @@ void SPUThread::set_ch_value(u32 ch, u32 value)
if (!queue) 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); 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 ch_in_mbox.push_uncond(CELL_ENOTCONN); // TODO: check error passing
return;
} }
if (queue->events.size() >= queue->size) if (queue->events.size() >= queue->size)
{ {
ch_in_mbox.push_uncond(CELL_EBUSY); return ch_in_mbox.push_uncond(CELL_EBUSY);
return;
} }
queue->push(SYS_SPU_THREAD_EVENT_USER_KEY, GetId(), ((u64)spup << 32) | (value & 0x00ffffff), data); 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) else if (code < 128)
{ {
@ -685,20 +684,22 @@ void SPUThread::set_ch_value(u32 ch, u32 value)
if (!Emu.GetIdManager().GetIDData(data, ef)) if (!Emu.GetIdManager().GetIDData(data, ef))
{ {
ch_in_mbox.push_uncond(CELL_ESRCH); return ch_in_mbox.push_uncond(CELL_ESRCH);
return;
} }
while (ef->waiters < 0) while (ef->cancelled)
{ {
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
} }
ef->flags |= 1ull << flag; ef->flags |= 1ull << flag;
ef->cv.notify_all();
ch_in_mbox.push_uncond(CELL_OK); if (ef->waiters)
return; {
ef->cv.notify_all();
}
return ch_in_mbox.push_uncond(CELL_OK);
} }
else if (code == 192) else if (code == 192)
{ {
@ -732,13 +733,18 @@ void SPUThread::set_ch_value(u32 ch, u32 value)
return; return;
} }
while (ef->waiters < 0) while (ef->cancelled)
{ {
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
} }
ef->flags |= 1ull << flag; ef->flags |= 1ull << flag;
if (ef->waiters)
{
ef->cv.notify_all(); ef->cv.notify_all();
}
return; return;
} }
else else
@ -958,8 +964,7 @@ void SPUThread::stop_and_signal(u32 code)
if (ch_in_mbox.get_count()) if (ch_in_mbox.get_count())
{ {
LOG_ERROR(SPU, "sys_spu_thread_receive_event(spuq=0x%x): In_MBox is not empty", spuq); 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 ch_in_mbox.push_uncond(CELL_EBUSY);
return;
} }
if (Ini.HLELogging.GetValue()) if (Ini.HLELogging.GetValue())
@ -986,20 +991,17 @@ void SPUThread::stop_and_signal(u32 code)
if (!queue) if (!queue)
{ {
ch_in_mbox.push_uncond(CELL_EINVAL); // TODO: check error value return ch_in_mbox.push_uncond(CELL_EINVAL); // TODO: check error value
return;
} }
// protocol is ignored in current implementation // protocol is ignored in current implementation
queue->waiters++; assert(queue->waiters > 0); queue->waiters++;
while (queue->events.empty()) while (queue->events.empty())
{ {
if (queue->waiters < 0) if (queue->cancelled)
{ {
queue->waiters--; assert(queue->waiters < 0); return ch_in_mbox.push_uncond(CELL_ECANCELED);
ch_in_mbox.push_uncond(CELL_ECANCELED);
return;
} }
if (Emu.IsStopped()) if (Emu.IsStopped())
@ -1018,7 +1020,7 @@ void SPUThread::stop_and_signal(u32 code)
ch_in_mbox.push_uncond((u32)event.data3); ch_in_mbox.push_uncond((u32)event.data3);
queue->events.pop_front(); queue->events.pop_front();
queue->waiters--; assert(queue->waiters >= 0); queue->waiters--;
if (queue->events.size()) if (queue->events.size())
{ {

View file

@ -139,10 +139,7 @@ union spu_channel_t
atomic_t<sync_var_t> sync_var; // atomic variable atomic_t<sync_var_t> sync_var; // atomic variable
sync_var_t data; // unsafe direct access
public: public:
bool push(u32 value) bool push(u32 value)
{ {
bool out_result; bool out_result;
@ -205,14 +202,14 @@ public:
sync_var.write_relaxed({ count, value }); 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: public:
void clear() void clear()
{ {
sync_var.write_relaxed({}); sync_var = {};
value3.write_relaxed({}); value3 = {};
} }
void push_uncond(u32 value) void push_uncond(u32 value)
@ -275,9 +272,9 @@ public:
return out_result; 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() void clear()
{ {
mask.write_relaxed(0); mask = {};
stat.write_relaxed(0); stat = {};
assigned.write_relaxed(-1); assigned = { -1 };
} }
}; };

View file

@ -161,7 +161,7 @@ vfsDirBase* VFS::OpenDir(const std::string& ps3_path) const
return nullptr; 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; std::string path;
if (vfsDevice* dev = GetDevice(ps3_path, path)) if (vfsDevice* dev = GetDevice(ps3_path, path))
@ -170,7 +170,7 @@ bool VFS::CreateFile(const std::string& ps3_path) const
if (res) if (res)
{ {
return res->Create(path); return res->Create(path, overwrite);
} }
} }

View file

@ -78,7 +78,7 @@ struct VFS
vfsFileBase* OpenFile(const std::string& ps3_path, vfsOpenMode mode) const; vfsFileBase* OpenFile(const std::string& ps3_path, vfsOpenMode mode) const;
vfsDirBase* OpenDir(const std::string& ps3_path) 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 CreateDir(const std::string& ps3_path) const;
bool RemoveFile(const std::string& ps3_path) const; bool RemoveFile(const std::string& ps3_path) const;
bool RemoveDir(const std::string& ps3_path) const; bool RemoveDir(const std::string& ps3_path) const;

View file

@ -26,9 +26,9 @@ bool vfsFile::Open(const std::string& path, vfsOpenMode mode)
return m_stream && m_stream->IsOpened(); 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) bool vfsFile::Exists(const std::string& path)

View file

@ -11,7 +11,7 @@ public:
vfsFile(const std::string& path, vfsOpenMode mode = vfsRead); vfsFile(const std::string& path, vfsOpenMode mode = vfsRead);
virtual bool Open(const std::string& path, vfsOpenMode mode = vfsRead) override; 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 Exists(const std::string& path) override;
virtual bool Rename(const std::string& from, const std::string& to) override; virtual bool Rename(const std::string& from, const std::string& to) override;
virtual bool Remove(const std::string& path) override; virtual bool Remove(const std::string& path) override;

View file

@ -27,7 +27,7 @@ public:
virtual bool Open(const std::string& path, vfsOpenMode mode); virtual bool Open(const std::string& path, vfsOpenMode mode);
virtual bool Close() override; 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 Exists(const std::string& path) { return false; }
virtual bool Rename(const std::string& from, const std::string& to) { return false; } virtual bool Rename(const std::string& from, const std::string& to) { return false; }
virtual bool Remove(const std::string& path) { return false; } virtual bool Remove(const std::string& path) { return false; }

View file

@ -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(uint p=1; p < path.length() && path[p] != '\0' ; p++)
{ {
for(; 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)) // ??? if(m != '/' && m != '\\' && !rExists(path)) // ???
{ {
rFile f; rFile f;
if (!f.Create(path)) { if (!f.Create(path, overwrite)) {
LOG_NOTICE(HLE, "vfsLocalFile::Create: couldn't create file"); if (overwrite) LOG_NOTICE(HLE, "vfsLocalFile::Create: couldn't create file");
return false; return false;
} }
else else

View file

@ -11,7 +11,7 @@ public:
vfsLocalFile(vfsDevice* device); vfsLocalFile(vfsDevice* device);
virtual bool Open(const std::string& path, vfsOpenMode mode = vfsRead) override; 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 Close() override;
virtual bool Exists(const std::string& path) override; virtual bool Exists(const std::string& path) override;

View file

@ -153,7 +153,9 @@ public:
std::lock_guard<std::mutex> lock(m_mtx_main); std::lock_guard<std::mutex> lock(m_mtx_main);
m_id_map.emplace(m_cur_id, ID(data, type)); 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); m_types[type].insert(m_cur_id);
} }
@ -173,7 +175,8 @@ public:
auto f = m_id_map.find(id); 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; return false;
} }
@ -182,6 +185,20 @@ public:
return true; return true;
} }
template<typename T> std::shared_ptr<T> GetIDData(const u32 id)
{
std::lock_guard<std::mutex> 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<T>();
}
bool HasID(const u32 id) bool HasID(const u32 id)
{ {
std::lock_guard<std::mutex> lock(m_mtx_main); std::lock_guard<std::mutex> lock(m_mtx_main);
@ -195,10 +212,13 @@ public:
auto item = m_id_map.find(id); 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; return false;
} }
if (item->second.GetType() < TYPE_OTHER) {
if (item->second.GetType() < TYPE_OTHER)
{
m_types[item->second.GetType()].erase(id); m_types[item->second.GetType()].erase(id);
} }

View file

@ -14,14 +14,6 @@ enum MemoryType
Memory_PSP, 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 class MemoryBase
{ {
std::vector<MemoryBlock*> MemoryBlocks; std::vector<MemoryBlock*> MemoryBlocks;
@ -98,4 +90,3 @@ public:
extern MemoryBase Memory; extern MemoryBase Memory;
#include "vm.h" #include "vm.h"

View file

@ -7,87 +7,101 @@
#undef InterlockedXor #undef InterlockedXor
template<typename T, size_t size = sizeof(T)> template<typename T, size_t size = sizeof(T)>
struct _to_atomic struct _to_atomic_subtype
{ {
static_assert(size == 1 || size == 2 || size == 4 || size == 8 || size == 16, "Invalid atomic type"); static_assert(size == 1 || size == 2 || size == 4 || size == 8 || size == 16, "Invalid atomic type");
typedef T type;
}; };
template<typename T> template<typename T>
struct _to_atomic<T, 1> struct _to_atomic_subtype<T, 1>
{ {
typedef uint8_t type; using type = uint8_t;
}; };
template<typename T> template<typename T>
struct _to_atomic<T, 2> struct _to_atomic_subtype<T, 2>
{ {
typedef uint16_t type; using type = uint16_t;
}; };
template<typename T> template<typename T>
struct _to_atomic<T, 4> struct _to_atomic_subtype<T, 4>
{ {
typedef uint32_t type; using type = uint32_t;
}; };
template<typename T> template<typename T>
struct _to_atomic<T, 8> struct _to_atomic_subtype<T, 8>
{ {
typedef uint64_t type; using type = uint64_t;
}; };
template<typename T> template<typename T>
struct _to_atomic<T, 16> struct _to_atomic_subtype<T, 16>
{ {
typedef u128 type; using type = u128;
}; };
template<typename T> template<typename T>
class _atomic_base union _atomic_base
{ {
typedef typename _to_atomic<T, sizeof(T)>::type atomic_type; using type = typename std::remove_cv<T>::type;
atomic_type data; using subtype = typename _to_atomic_subtype<type, sizeof(type)>::type;
type data; // unsafe direct access
subtype sub_data; // unsafe direct access to substitute type
__forceinline static const subtype to_subtype(const type& value)
{
return reinterpret_cast<const subtype&>(value);
}
__forceinline static const type from_subtype(const subtype value)
{
return reinterpret_cast<const type&>(value);
}
__forceinline static type& to_type(subtype& value)
{
return reinterpret_cast<type&>(value);
}
public: public:
// atomically compare data with cmp, replace with exch if equal, return previous data value anyway // 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) volatile
{ {
const atomic_type res = InterlockedCompareExchange(&data, (atomic_type&)(exch), (atomic_type&)(cmp)); return from_subtype(InterlockedCompareExchange(&sub_data, to_subtype(exch), to_subtype(cmp)));
return (T&)res;
} }
// atomically compare data with cmp, replace with exch if equal, return true if data was replaced // 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) volatile
{ {
return InterlockedCompareExchangeTest(&data, (atomic_type&)(exch), (atomic_type&)(cmp)); return InterlockedCompareExchangeTest(&sub_data, to_subtype(exch), to_subtype(cmp));
} }
// read data with memory barrier // read data with memory barrier
__forceinline const T read_sync() const volatile __forceinline const type read_sync() const volatile
{ {
const atomic_type res = InterlockedCompareExchange(const_cast<volatile atomic_type*>(&data), 0, 0); return from_subtype(InterlockedCompareExchange(const_cast<subtype*>(&sub_data), 0, 0));
return (T&)res;
} }
// atomically replace data with exch, return previous data value // atomically replace data with exch, return previous data value
__forceinline const T exchange(const T& exch) volatile __forceinline const type exchange(const type& exch) volatile
{ {
const atomic_type res = InterlockedExchange(&data, (atomic_type&)(exch)); return from_subtype(InterlockedExchange(&sub_data, to_subtype(exch)));
return (T&)res;
} }
// read data without memory barrier // read data without memory barrier
__forceinline const T read_relaxed() const volatile __forceinline const type read_relaxed() const volatile
{ {
return (T&)data; const subtype value = const_cast<const subtype&>(sub_data);
return from_subtype(value);
} }
// write data without memory barrier // write data without memory barrier
__forceinline void write_relaxed(const T& value) __forceinline void write_relaxed(const type& value) volatile
{ {
data = (atomic_type&)(value); const_cast<subtype&>(sub_data) = to_subtype(value);
} }
// perform atomic operation on data // perform atomic operation on data
@ -95,10 +109,10 @@ public:
{ {
while (true) while (true)
{ {
const T old = read_relaxed(); const subtype old = const_cast<const subtype&>(sub_data);
T _new = old; subtype _new = old;
atomic_proc(_new); // function should accept reference to T type atomic_proc(to_type(_new)); // function should accept reference to T type
if (compare_and_swap_test(old, _new)) return; if (InterlockedCompareExchangeTest(&sub_data, _new, old)) return;
} }
} }
@ -107,24 +121,24 @@ public:
{ {
while (true) while (true)
{ {
const T old = read_relaxed(); const subtype old = const_cast<const subtype&>(sub_data);
T _new = old; subtype _new = old;
RT res = (RT)atomic_proc(_new); // function should accept reference to T type and return some value auto res = static_cast<RT>(atomic_proc(to_type(_new))); // function should accept reference to T type and return some value
if (res != proceed_value) return res; 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 // perform atomic operation on data with additional memory barrier
template<typename FT> __forceinline void atomic_op_sync(const FT atomic_proc) volatile template<typename FT> __forceinline void atomic_op_sync(const FT atomic_proc) volatile
{ {
T old = read_sync(); subtype old = InterlockedCompareExchange(&sub_data, 0, 0);
while (true) while (true)
{ {
T _new = old; subtype _new = old;
atomic_proc(_new); // function should accept reference to T type atomic_proc(to_type(_new)); // function should accept reference to T type
const T val = compare_and_swap(old, _new); const subtype val = InterlockedCompareExchange(&sub_data, _new, old);
if ((atomic_type&)val == (atomic_type&)old) return; if (val == old) return;
old = val; old = val;
} }
} }
@ -132,75 +146,164 @@ public:
// perform atomic operation on data with additional memory barrier and special exit condition (if intermediate result != proceed_value) // perform atomic operation on data with additional memory barrier and special exit condition (if intermediate result != proceed_value)
template<typename RT, typename FT> __forceinline RT atomic_op_sync(const RT proceed_value, const FT atomic_proc) volatile template<typename RT, typename FT> __forceinline RT atomic_op_sync(const RT proceed_value, const FT atomic_proc) volatile
{ {
T old = read_sync(); subtype old = InterlockedCompareExchange(&sub_data, 0, 0);
while (true) while (true)
{ {
T _new = old; subtype _new = old;
RT res = (RT)atomic_proc(_new); // function should accept reference to T type and return some value auto res = static_cast<RT>(atomic_proc(to_type(_new))); // function should accept reference to T type and return some value
if (res != proceed_value) return res; if (res != proceed_value) return res;
const T val = compare_and_swap(old, _new); const subtype val = InterlockedCompareExchange(&sub_data, _new, old);
if ((atomic_type&)val == (atomic_type&)old) return proceed_value; if (val == old) return proceed_value;
old = val; old = val;
} }
} }
// perform non-atomic operation on data directly without memory barriers
template<typename FT> __forceinline void direct_op(const FT direct_proc) volatile
{
direct_proc((T&)data);
}
// atomic bitwise OR, returns previous data // atomic bitwise OR, returns previous data
__forceinline const T _or(const T& right) volatile __forceinline const type _or(const type& right) volatile
{ {
const atomic_type res = InterlockedOr(&data, (atomic_type&)(right)); return from_subtype(InterlockedOr(&sub_data, to_subtype(right)));
return (T&)res;
} }
// atomic bitwise AND, returns previous data // atomic bitwise AND, returns previous data
__forceinline const T _and(const T& right) volatile __forceinline const type _and(const type& right) volatile
{ {
const atomic_type res = InterlockedAnd(&data, (atomic_type&)(right)); return from_subtype(InterlockedAnd(&sub_data, to_subtype(right)));
return (T&)res;
} }
// atomic bitwise AND NOT (inverts right argument), returns previous data // 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) volatile
{ {
const atomic_type res = InterlockedAnd(&data, ~(atomic_type&)(right)); return from_subtype(InterlockedAnd(&sub_data, ~to_subtype(right)));
return (T&)res;
} }
// atomic bitwise XOR, returns previous data // atomic bitwise XOR, returns previous data
__forceinline const T _xor(const T& right) volatile __forceinline const type _xor(const type& right) volatile
{ {
const atomic_type res = InterlockedXor(&data, (atomic_type&)(right)); return from_subtype(InterlockedXor(&sub_data, to_subtype(right)));
return (T&)res;
} }
__forceinline const T operator |= (const T& right) volatile __forceinline const type operator |= (const type& right) volatile
{ {
const atomic_type res = InterlockedOr(&data, (atomic_type&)(right)) | (atomic_type&)(right); return from_subtype(InterlockedOr(&sub_data, to_subtype(right)) | to_subtype(right));
return (T&)res;
} }
__forceinline const T operator &= (const T& right) volatile __forceinline const type operator &= (const type& right) volatile
{ {
const atomic_type res = InterlockedAnd(&data, (atomic_type&)(right)) & (atomic_type&)(right); return from_subtype(InterlockedAnd(&sub_data, to_subtype(right)) & to_subtype(right));
return (T&)res;
} }
__forceinline const T operator ^= (const T& right) volatile __forceinline const type operator ^= (const type& right) volatile
{ {
const atomic_type res = InterlockedXor(&data, (atomic_type&)(right)) ^ (atomic_type&)(right); return from_subtype(InterlockedXor(&sub_data, to_subtype(right)) ^ to_subtype(right));
return (T&)res;
} }
}; };
template<typename T> inline static typename std::enable_if<std::is_arithmetic<T>::value, T>::type operator ++(_atomic_base<be_t<T>>& left, int) // Helper definitions
template<typename T, typename T2 = T> using if_arithmetic_t = const typename std::enable_if<std::is_arithmetic<T>::value && std::is_arithmetic<T2>::value, T>::type;
template<typename T, typename T2 = T> using if_arithmetic_be_t = const typename std::enable_if<std::is_arithmetic<T>::value && std::is_arithmetic<T2>::value, be_t<T>>::type;
template<typename T, typename T2 = T> using if_arithmetic_atomic_t = typename std::enable_if<std::is_arithmetic<T>::value && std::is_arithmetic<T2>::value, _atomic_base<T>&>::type;
template<typename T, typename T2 = T> using if_arithmetic_atomic_be_t = typename std::enable_if<std::is_arithmetic<T>::value && std::is_arithmetic<T2>::value, _atomic_base<be_t<T>>&>::type;
template<typename T> inline static if_arithmetic_t<T> operator ++(_atomic_base<T>& left)
{ {
T result; T result;
left.atomic_op([&result](T& value)
{
result = ++value;
});
return result;
}
template<typename T> inline static if_arithmetic_t<T> operator --(_atomic_base<T>& left)
{
T result;
left.atomic_op([&result](T& value)
{
result = --value;
});
return result;
}
template<typename T> inline static if_arithmetic_t<T> operator ++(_atomic_base<T>& left, int)
{
T result;
left.atomic_op([&result](T& value)
{
result = value++;
});
return result;
}
template<typename T> inline static if_arithmetic_t<T> operator --(_atomic_base<T>& left, int)
{
T result;
left.atomic_op([&result](T& value)
{
result = value--;
});
return result;
}
template<typename T, typename T2> inline static if_arithmetic_t<T, T2> operator +=(_atomic_base<T>& left, T2 right)
{
T result;
left.atomic_op([&result, right](T& value)
{
result = (value += right);
});
return result;
}
template<typename T, typename T2> inline static if_arithmetic_t<T, T2> operator -=(_atomic_base<T>& left, T2 right)
{
T result;
left.atomic_op([&result, right](T& value)
{
result = (value -= right);
});
return result;
}
template<typename T> inline static if_arithmetic_be_t<T> operator ++(_atomic_base<be_t<T>>& left)
{
be_t<T> result;
left.atomic_op([&result](be_t<T>& value)
{
result = ++value;
});
return result;
}
template<typename T> inline static if_arithmetic_be_t<T> operator --(_atomic_base<be_t<T>>& left)
{
be_t<T> result;
left.atomic_op([&result](be_t<T>& value)
{
result = --value;
});
return result;
}
template<typename T> inline static if_arithmetic_be_t<T> operator ++(_atomic_base<be_t<T>>& left, int)
{
be_t<T> result;
left.atomic_op([&result](be_t<T>& value) left.atomic_op([&result](be_t<T>& value)
{ {
result = value++; result = value++;
@ -209,9 +312,9 @@ template<typename T> inline static typename std::enable_if<std::is_arithmetic<T>
return result; return result;
} }
template<typename T> inline static typename std::enable_if<std::is_arithmetic<T>::value, T>::type operator --(_atomic_base<be_t<T>>& left, int) template<typename T> inline static if_arithmetic_be_t<T> operator --(_atomic_base<be_t<T>>& left, int)
{ {
T result; be_t<T> result;
left.atomic_op([&result](be_t<T>& value) left.atomic_op([&result](be_t<T>& value)
{ {
@ -221,9 +324,9 @@ template<typename T> inline static typename std::enable_if<std::is_arithmetic<T>
return result; return result;
} }
template<typename T, typename T2> inline static typename std::enable_if<std::is_arithmetic<T>::value, T>::type operator +=(_atomic_base<be_t<T>>& left, T2 right) template<typename T, typename T2> inline static if_arithmetic_be_t<T, T2> operator +=(_atomic_base<be_t<T>>& left, T2 right)
{ {
T result; be_t<T> result;
left.atomic_op([&result, right](be_t<T>& value) left.atomic_op([&result, right](be_t<T>& value)
{ {
@ -233,9 +336,9 @@ template<typename T, typename T2> inline static typename std::enable_if<std::is_
return result; return result;
} }
template<typename T, typename T2> inline static typename std::enable_if<std::is_arithmetic<T>::value, T>::type operator -=(_atomic_base<be_t<T>>& left, T2 right) template<typename T, typename T2> inline static if_arithmetic_be_t<T, T2> operator -=(_atomic_base<be_t<T>>& left, T2 right)
{ {
T result; be_t<T> result;
left.atomic_op([&result, right](be_t<T>& value) left.atomic_op([&result, right](be_t<T>& value)
{ {

View file

@ -108,14 +108,24 @@ namespace vm
} }
*/ */
template<typename AT> operator const ps3::ptr<T, 1, AT>() const template<typename AT> operator _ptr_base<T, 1, AT>() const
{ {
return ps3::ptr<T, 1, AT>::make(m_addr); return _ptr_base<T, 1, AT>::make(m_addr);
} }
template<typename AT> operator const ps3::ptr<const T, 1, AT>() const template<typename AT> operator _ptr_base<const T, 1, AT>() const
{ {
return ps3::ptr<const T, 1, AT>::make(m_addr); return _ptr_base<const T, 1, AT>::make(m_addr);
}
template<typename AT> operator _ptr_base<void, 1, AT>() const
{
return _ptr_base<void, 1, AT>::make(m_addr);
}
template<typename AT> operator _ptr_base<const void, 1, AT>() const
{
return _ptr_base<const void, 1, AT>::make(m_addr);
} }
operator T&() operator T&()
@ -604,14 +614,24 @@ namespace vm
} }
*/ */
template<typename AT> operator const ps3::ptr<T, 1, AT>() const template<typename AT> operator _ptr_base<T, 1, AT>() const
{ {
return ps3::ptr<T, 1, AT>::make(m_data.addr); return _ptr_base<T, 1, AT>::make(m_data.addr);
} }
template<typename AT> operator const ps3::ptr<const T, 1, AT>() const template<typename AT> operator _ptr_base<const T, 1, AT>() const
{ {
return ps3::ptr<const T, 1, AT>::make(m_data.addr); return _ptr_base<const T, 1, AT>::make(m_data.addr);
}
template<typename AT> operator _ptr_base<void, 1, AT>() const
{
return _ptr_base<void, 1, AT>::make(m_data.addr);
}
template<typename AT> operator _ptr_base<const void, 1, AT>() const
{
return _ptr_base<const void, 1, AT>::make(m_data.addr);
} }
operator T&() operator T&()

View file

@ -1,66 +1,69 @@
#pragma once #pragma once
enum ErrorCode #define ERROR_CODE(code) static_cast<s32>(code)
{
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_EMLIN = 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,
CELL_UNKNOWN_ERROR = 0xFFFFFFFF, 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,
}; };

View file

@ -10,6 +10,7 @@ extern Module cellDmux;
extern Module cellFiber; extern Module cellFiber;
extern Module cellFont; extern Module cellFont;
extern Module cellFontFT; extern Module cellFontFT;
extern Module cellFs;
extern Module cellGame; extern Module cellGame;
extern Module cellGcmSys; extern Module cellGcmSys;
extern Module cellGem; extern Module cellGem;
@ -49,7 +50,6 @@ extern Module sceNpCommerce2;
extern Module sceNpSns; extern Module sceNpSns;
extern Module sceNpTrophy; extern Module sceNpTrophy;
extern Module sceNpTus; extern Module sceNpTus;
extern Module sys_fs;
extern Module sys_io; extern Module sys_io;
extern Module sys_net; extern Module sys_net;
extern Module sysPrxForUser; extern Module sysPrxForUser;
@ -77,7 +77,7 @@ static const g_module_list[] =
{ 0x000b, "cellOvis", &cellOvis }, { 0x000b, "cellOvis", &cellOvis },
{ 0x000c, "cellSheap", nullptr }, { 0x000c, "cellSheap", nullptr },
{ 0x000d, "sys_sync", &cellSync }, { 0x000d, "sys_sync", &cellSync },
{ 0x000e, "sys_fs", &sys_fs }, { 0x000e, "sys_fs", &cellFs },
{ 0x000f, "cellJpgDec", &cellJpgDec }, { 0x000f, "cellJpgDec", &cellJpgDec },
{ 0x0010, "cellGcmSys", &cellGcmSys }, { 0x0010, "cellGcmSys", &cellGcmSys },
{ 0x0011, "cellAudio", &cellAudio }, { 0x0011, "cellAudio", &cellAudio },

View file

@ -30,7 +30,7 @@ int cellFontInitializeWithRevision(u64 revisionFlags, vm::ptr<CellFontConfig> co
return CELL_FONT_OK; return CELL_FONT_OK;
} }
int cellFontGetRevisionFlags(vm::ptr<be_t<u64>> revisionFlags) int cellFontGetRevisionFlags(vm::ptr<u64> revisionFlags)
{ {
UNIMPLEMENTED_FUNC(cellFont); UNIMPLEMENTED_FUNC(cellFont);
return CELL_FONT_OK; return CELL_FONT_OK;
@ -375,7 +375,7 @@ int cellFontSetEffectSlant(vm::ptr<CellFont> font, float slantParam)
return CELL_FONT_OK; return CELL_FONT_OK;
} }
int cellFontGetEffectSlant(vm::ptr<CellFont> font, vm::ptr<be_t<float>> slantParam) int cellFontGetEffectSlant(vm::ptr<CellFont> font, vm::ptr<float> slantParam)
{ {
cellFont.Warning("cellFontSetEffectSlant(font_addr=0x%x, slantParam_addr=0x%x)", font.addr(), slantParam.addr()); cellFont.Warning("cellFontSetEffectSlant(font_addr=0x%x, slantParam_addr=0x%x)", font.addr(), slantParam.addr());

View file

@ -0,0 +1,802 @@
#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<const char> path, s32 flags, vm::ptr<u32> fd, vm::ptr<const void> arg, u64 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, flags & CELL_FS_O_CREAT ? CELL_FS_S_IRUSR | CELL_FS_S_IWUSR : 0, arg, size);
}
s32 cellFsRead(PPUThread& CPU, u32 fd, vm::ptr<void> buf, u64 nbytes, vm::ptr<u64> 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<be_t<u64>>(CPU));
}
s32 cellFsWrite(PPUThread& CPU, u32 fd, vm::ptr<const void> buf, u64 nbytes, vm::ptr<u64> 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<be_t<u64>>(CPU));
}
s32 cellFsClose(u32 fd)
{
cellFs.Log("cellFsClose(fd=0x%x)", fd);
// call the syscall
return sys_fs_close(fd);
}
s32 cellFsOpendir(vm::ptr<const char> path, vm::ptr<u32> 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<CellFsDirent> dir, vm::ptr<u64> 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<const char> path, vm::ptr<CellFsStat> 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<CellFsStat> 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<const char> path, s32 mode)
{
cellFs.Warning("cellFsMkdir(path=*0x%x, mode=%#o) -> sys_fs_mkdir()", path, mode);
// TODO
// call the syscall
return sys_fs_mkdir(path, mode);
}
s32 cellFsRename(vm::ptr<const char> from, vm::ptr<const char> 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<const char> path)
{
cellFs.Warning("cellFsRmdir(path=*0x%x) -> sys_fs_rmdir()", path);
// TODO
// call the syscall
return sys_fs_rmdir(path);
}
s32 cellFsUnlink(vm::ptr<const char> 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<u64> 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<u64> sector_size, vm::ptr<u64> 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<be_t<u64>>(CPU), vm::stackvar<be_t<u64>>(CPU)) : CELL_EFAULT;
}
s32 cellFsGetBlockSize(PPUThread& CPU, vm::ptr<const char> path, vm::ptr<u64> sector_size, vm::ptr<u64> 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<be_t<u64>>(CPU));
}
s32 cellFsTruncate(vm::ptr<const char> 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<const char> path, s32 mode)
{
cellFs.Warning("cellFsChmod(path=*0x%x, mode=%#o) -> sys_fs_chmod()", path, mode);
// TODO
// call the syscall
return sys_fs_chmod(path, mode);
}
s32 cellFsGetFreeSize(vm::ptr<const char> path, vm::ptr<u32> block_size, vm::ptr<u64> 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<CellFsDirectoryEntry> entries, u32 entries_size, vm::ptr<u32> 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<vfsDirBase> 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<size_t>(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 cellFsReadWithOffset(u32 fd, u64 offset, vm::ptr<void> buf, u64 buffer_size, vm::ptr<u64> 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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file) || file->flags & CELL_FS_O_WRONLY)
{
return CELL_FS_EBADF;
}
const auto old_position = file->file->Tell();
file->file->Seek(offset);
const auto read = file->file->Read(buf.get_ptr(), buffer_size);
file->file->Seek(old_position);
if (nread)
{
*nread = read;
}
return CELL_OK;
}
s32 cellFsWriteWithOffset(u32 fd, u64 offset, vm::ptr<const void> buf, u64 data_size, vm::ptr<u64> 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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file) || !(file->flags & CELL_FS_O_ACCMODE))
{
return CELL_FS_EBADF;
}
const auto old_position = file->file->Tell();
file->file->Seek(offset);
const auto written = file->file->Write(buf.get_ptr(), data_size);
file->file->Seek(old_position);
if (nwrite)
{
*nwrite = written;
}
return CELL_OK;
}
s32 cellFsStReadInit(u32 fd, vm::ptr<CellFsRingBuffer> ringbuf)
{
cellFs.Warning("cellFsStReadInit(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf);
std::shared_ptr<fs_file_t> 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<void>(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<fs_file_t> 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<CellFsRingBuffer> ringbuf)
{
cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, ringbuf=*0x%x)", fd, ringbuf);
std::shared_ptr<fs_file_t> 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<u64> status)
{
cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, status=*0x%x)", fd, status);
std::shared_ptr<fs_file_t> 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<u64> regid)
{
cellFs.Warning("cellFsStReadGetRingBuf(fd=0x%x, regid=*0x%x)", fd, regid);
std::shared_ptr<fs_file_t> 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<fs_file_t> 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<fs_file_t> 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<u8> buf, u64 size, vm::ptr<u64> rsize)
{
cellFs.Warning("cellFsStRead(fd=0x%x, buf=*0x%x, size=0x%llx, rsize=*0x%x)", fd, buf, size, rsize);
std::shared_ptr<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file))
return CELL_ESRCH;
// TODO: use ringbuffer (fs_config)
fs_config.m_regid += size;
if (file->file->Eof())
return CELL_FS_ERANGE;
*rsize = file->file->Read(buf.get_ptr(), size);
return CELL_OK;
}
s32 cellFsStReadGetCurrentAddr(u32 fd, vm::ptr<vm::ptr<u8>> addr, vm::ptr<u64> size)
{
cellFs.Todo("cellFsStReadGetCurrentAddr(fd=0x%x, addr=*0x%x, size=*0x%x)", fd, addr, size);
std::shared_ptr<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file))
return CELL_ESRCH;
return CELL_OK;
}
s32 cellFsStReadPutCurrentAddr(u32 fd, vm::ptr<u8> addr, u64 size)
{
cellFs.Todo("cellFsStReadPutCurrentAddr(fd=0x%x, addr=*0x%x, size=0x%llx)", fd, addr, size);
std::shared_ptr<fs_file_t> 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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file))
return CELL_ESRCH;
return CELL_OK;
}
s32 cellFsStReadWaitCallback(u32 fd, u64 size, vm::ptr<void(int xfd, u64 xsize)> func)
{
cellFs.Todo("cellFsStReadWaitCallback(fd=0x%x, size=0x%llx, func=*0x%x)", fd, size, func);
std::shared_ptr<fs_file_t> 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<vfsFileBase> packed_stream(Emu.GetVFS().OpenFile(packed_file, vfsRead));
std::shared_ptr<vfsFileBase> 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(PPUThread& CPU, vm::ptr<const char> path, s32 flags, vm::ptr<u32> fd, vm::ptr<const void> arg, u64 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)
{
return CELL_EINVAL;
}
return cellFsOpen(path, CELL_FS_O_RDONLY, fd, vm::stackvar<be_t<u64>>(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;
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;
*/
}
s32 cellFsSdataOpenByFd(u32 mself_fd, s32 flags, vm::ptr<u32> sdata_fd, u64 offset, vm::ptr<const void> arg, u64 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:
return CELL_OK;
}
std::mutex g_fs_aio_mutex;
using fs_aio_cb_t = vm::ptr<void(vm::ptr<CellFsAio> xaio, s32 error, s32 xid, u64 size)>;
void fsAio(vm::ptr<CellFsAio> aio, bool write, s32 xid, fs_aio_cb_t func)
{
std::lock_guard<std::mutex> 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 result = 0;
std::shared_ptr<fs_file_t> file;
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;
}
else
{
const auto old_position = file->file->Tell();
file->file->Seek(aio->offset);
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([=](PPUThread& CPU)
{
func(CPU, aio, error, xid, result);
});
}
s32 cellFsAioInit(vm::ptr<const char> mount_point)
{
cellFs.Warning("cellFsAioInit(mount_point=*0x%x)", mount_point);
cellFs.Warning("*** mount_point = '%s'", mount_point.get_ptr());
// TODO: create AIO thread (if not exists) for specified mount point
return CELL_OK;
}
s32 cellFsAioFinish(vm::ptr<const char> mount_point)
{
cellFs.Warning("cellFsAioFinish(mount_point=*0x%x)", mount_point);
cellFs.Warning("*** mount_point = '%s'", mount_point.get_ptr());
// TODO: delete existing AIO thread for specified mount point
return CELL_OK;
}
std::atomic<s32> g_fs_aio_id(0);
s32 cellFsAioRead(vm::ptr<CellFsAio> aio, vm::ptr<s32> 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<fs_file_t>(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(fsAio, aio, false, (*id = ++g_fs_aio_id), func)).detach();
return CELL_OK;
}
s32 cellFsAioWrite(vm::ptr<CellFsAio> aio, vm::ptr<s32> id, fs_aio_cb_t func)
{
cellFs.Warning("cellFsAioWrite(aio=*0x%x, id=*0x%x, func=*0x%x)", aio, id, func);
if (!Emu.GetIdManager().CheckID<fs_file_t>(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(fsAio, aio, true, (*id = ++g_fs_aio_id), func)).detach();
return CELL_OK;
}
s32 cellFsAioCancel(s32 id)
{
cellFs.Warning("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);
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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file))
return CELL_ESRCH;
return CELL_OK;
}
Module cellFs("cellFs", []()
{
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, 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);
REG_FUNC(cellFs, cellFsWriteWithOffset);
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);
});

View file

@ -0,0 +1,40 @@
#pragma once
// CellFsRingBuffer.copy
enum : s32
{
CELL_FS_ST_COPY = 0,
CELL_FS_ST_COPYLESS = 1,
};
struct CellFsRingBuffer
{
be_t<u64> ringbuf_size;
be_t<u64> block_size;
be_t<u64> transfer_rate;
be_t<s32> 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<u32> fd;
be_t<u64> offset;
vm::bptr<void> buf;
be_t<u64> size;
be_t<u64> user_data;
};

View file

@ -6,7 +6,11 @@
#include "stblib/stb_image.h" #include "stblib/stb_image.h"
#include "stblib/stb_image.c" // (TODO: Should we put this elsewhere?) #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 "Emu/SysCalls/lv2/sys_fs.h"
#include "cellGifDec.h" #include "cellGifDec.h"
extern Module cellGifDec; extern Module cellGifDec;
@ -39,19 +43,15 @@ int cellGifDecOpen(u32 mainHandle, vm::ptr<u32> subHandle, vm::ptr<CellGifDecSrc
break; break;
case se32(CELL_GIFDEC_FILE): case se32(CELL_GIFDEC_FILE):
// Get file descriptor {
vm::var<be_t<u32>> fd; // Get file descriptor and size
int ret = cellFsOpen(src->fileName, 0, fd, vm::ptr<const void>::make(0), 0); std::shared_ptr<fs_file_t> file(new fs_file_t(std::shared_ptr<vfsStream>(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)), 0, 0));
current_subHandle->fd = fd.value(); if (!file) return CELL_GIFDEC_ERROR_OPEN_FILE;
if (ret != CELL_OK) return CELL_GIFDEC_ERROR_OPEN_FILE; current_subHandle->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE);
current_subHandle->fileSize = file->file->GetSize();
// Get size of file
vm::var<CellFsStat> 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
break; break;
} }
}
// From now, every u32 subHandle argument is a pointer to a CellGifDecSubHandle struct. // From now, every u32 subHandle argument is a pointer to a CellGifDecSubHandle struct.
*subHandle = Emu.GetIdManager().GetNewID(current_subHandle); *subHandle = Emu.GetIdManager().GetNewID(current_subHandle);
@ -74,7 +74,6 @@ int cellGifDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr<CellGifDecInfo>
//Write the header to buffer //Write the header to buffer
vm::var<u8[13]> buffer; // Alloc buffer for GIF header vm::var<u8[13]> buffer; // Alloc buffer for GIF header
vm::var<be_t<u64>> pos, nread;
switch(subHandle_data->src.srcSelect.data()) switch(subHandle_data->src.srcSelect.data())
{ {
@ -83,10 +82,13 @@ int cellGifDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr<CellGifDecInfo>
break; break;
case se32(CELL_GIFDEC_FILE): case se32(CELL_GIFDEC_FILE):
cellFsLseek(fd, 0, CELL_SEEK_SET, pos); {
cellFsRead(fd, vm::ptr<void>::make(buffer.addr()), buffer.size(), nread); auto file = Emu.GetIdManager().GetIDData<fs_file_t>(fd);
file->file->Seek(0);
file->file->Read(buffer.begin(), buffer.size());
break; break;
} }
}
if (*buffer.To<be_t<u32>>(0) != 0x47494638 || if (*buffer.To<be_t<u32>>(0) != 0x47494638 ||
(*buffer.To<u16>(4) != 0x6139 && *buffer.To<u16>(4) != 0x6137)) // Error: The first 6 bytes are not a valid GIF signature (*buffer.To<u16>(4) != 0x6139 && *buffer.To<u16>(4) != 0x6137)) // Error: The first 6 bytes are not a valid GIF signature
@ -156,7 +158,6 @@ int cellGifDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr<u8> data, vm::pt
//Copy the GIF file to a buffer //Copy the GIF file to a buffer
vm::var<unsigned char[]> gif((u32)fileSize); vm::var<unsigned char[]> gif((u32)fileSize);
vm::var<be_t<u64>> pos, nread;
switch(subHandle_data->src.srcSelect.data()) switch(subHandle_data->src.srcSelect.data())
{ {
@ -165,10 +166,13 @@ int cellGifDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr<u8> data, vm::pt
break; break;
case se32(CELL_GIFDEC_FILE): case se32(CELL_GIFDEC_FILE):
cellFsLseek(fd, 0, CELL_SEEK_SET, pos); {
cellFsRead(fd, vm::ptr<void>::make(gif.addr()), gif.size(), nread); auto file = Emu.GetIdManager().GetIDData<fs_file_t>(fd);
file->file->Seek(0);
file->file->Read(gif.ptr(), gif.size());
break; break;
} }
}
//Decode GIF file. (TODO: Is there any faster alternative? Can we do it without external libraries?) //Decode GIF file. (TODO: Is there any faster alternative? Can we do it without external libraries?)
int width, height, actual_components; int width, height, actual_components;
@ -265,7 +269,7 @@ int cellGifDecClose(u32 mainHandle, u32 subHandle)
if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data)) if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data))
return CELL_GIFDEC_ERROR_FATAL; return CELL_GIFDEC_ERROR_FATAL;
cellFsClose(subHandle_data->fd); Emu.GetIdManager().RemoveID<fs_file_t>(subHandle_data->fd);
Emu.GetIdManager().RemoveID<CellGifDecSubHandle>(subHandle); Emu.GetIdManager().RemoveID<CellGifDecSubHandle>(subHandle);
return CELL_OK; return CELL_OK;

View file

@ -5,7 +5,11 @@
#include "Emu/SysCalls/Modules.h" #include "Emu/SysCalls/Modules.h"
#include "stblib/stb_image.h" #include "stblib/stb_image.h"
#include "Emu/SysCalls/lv2/cellFs.h"
#include "Emu/FS/VFS.h"
#include "Emu/FS/vfsFileBase.h"
#include "Emu/SysCalls/lv2/sys_fs.h"
#include "cellJpgDec.h" #include "cellJpgDec.h"
extern Module cellJpgDec; extern Module cellJpgDec;
@ -45,19 +49,15 @@ int cellJpgDecOpen(u32 mainHandle, vm::ptr<u32> subHandle, vm::ptr<CellJpgDecSrc
break; break;
case se32(CELL_JPGDEC_FILE): case se32(CELL_JPGDEC_FILE):
// Get file descriptor {
vm::var<be_t<u32>> fd; // Get file descriptor and size
int ret = cellFsOpen(src->fileName, 0, fd, vm::ptr<const void>::make(0), 0); std::shared_ptr<fs_file_t> file(new fs_file_t(std::shared_ptr<vfsStream>(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)), 0, 0));
current_subHandle->fd = fd.value(); if (!file) return CELL_JPGDEC_ERROR_OPEN_FILE;
if (ret != CELL_OK) return CELL_JPGDEC_ERROR_OPEN_FILE; current_subHandle->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE);
current_subHandle->fileSize = file->file->GetSize();
// Get size of file
vm::var<CellFsStat> 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
break; break;
} }
}
// From now, every u32 subHandle argument is a pointer to a CellJpgDecSubHandle struct. // From now, every u32 subHandle argument is a pointer to a CellJpgDecSubHandle struct.
*subHandle = Emu.GetIdManager().GetNewID(current_subHandle); *subHandle = Emu.GetIdManager().GetNewID(current_subHandle);
@ -74,7 +74,7 @@ int cellJpgDecClose(u32 mainHandle, u32 subHandle)
if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data)) if(!Emu.GetIdManager().GetIDData(subHandle, subHandle_data))
return CELL_JPGDEC_ERROR_FATAL; return CELL_JPGDEC_ERROR_FATAL;
cellFsClose(subHandle_data->fd); Emu.GetIdManager().RemoveID<fs_file_t>(subHandle_data->fd);
Emu.GetIdManager().RemoveID<CellJpgDecSubHandle>(subHandle); Emu.GetIdManager().RemoveID<CellJpgDecSubHandle>(subHandle);
return CELL_OK; return CELL_OK;
@ -94,7 +94,6 @@ int cellJpgDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr<CellJpgDecInfo>
//Write the header to buffer //Write the header to buffer
vm::var<u8[]> buffer((u32)fileSize); vm::var<u8[]> buffer((u32)fileSize);
vm::var<be_t<u64>> pos, nread;
switch(subHandle_data->src.srcSelect.data()) switch(subHandle_data->src.srcSelect.data())
{ {
@ -103,10 +102,13 @@ int cellJpgDecReadHeader(u32 mainHandle, u32 subHandle, vm::ptr<CellJpgDecInfo>
break; break;
case se32(CELL_JPGDEC_FILE): case se32(CELL_JPGDEC_FILE):
cellFsLseek(fd, 0, CELL_SEEK_SET, pos); {
cellFsRead(fd, vm::ptr<void>::make(buffer.addr()), buffer.size(), nread); auto file = Emu.GetIdManager().GetIDData<fs_file_t>(fd);
file->file->Seek(0);
file->file->Read(buffer.ptr(), buffer.size());
break; break;
} }
}
if (*buffer.To<u32>(0) != 0xE0FFD8FF || // Error: Not a valid SOI header if (*buffer.To<u32>(0) != 0xE0FFD8FF || // Error: Not a valid SOI header
*buffer.To<u32>(6) != 0x4649464A) // Error: Not a valid JFIF string *buffer.To<u32>(6) != 0x4649464A) // Error: Not a valid JFIF string
@ -163,7 +165,6 @@ int cellJpgDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr<u8> data, vm::pt
//Copy the JPG file to a buffer //Copy the JPG file to a buffer
vm::var<unsigned char[]> jpg((u32)fileSize); vm::var<unsigned char[]> jpg((u32)fileSize);
vm::var<be_t<u64>> pos, nread;
switch(subHandle_data->src.srcSelect.data()) switch(subHandle_data->src.srcSelect.data())
{ {
@ -172,10 +173,13 @@ int cellJpgDecDecodeData(u32 mainHandle, u32 subHandle, vm::ptr<u8> data, vm::pt
break; break;
case se32(CELL_JPGDEC_FILE): case se32(CELL_JPGDEC_FILE):
cellFsLseek(fd, 0, CELL_SEEK_SET, pos); {
cellFsRead(fd, vm::ptr<void>::make(jpg.addr()), jpg.size(), nread); auto file = Emu.GetIdManager().GetIDData<fs_file_t>(fd);
file->file->Seek(0);
file->file->Read(jpg.ptr(), jpg.size());
break; break;
} }
}
//Decode JPG file. (TODO: Is there any faster alternative? Can we do it without external libraries?) //Decode JPG file. (TODO: Is there any faster alternative? Can we do it without external libraries?)
int width, height, actual_components; int width, height, actual_components;

View file

@ -1,12 +1,16 @@
#include "stdafx.h" #include "stdafx.h"
#include "Emu/Memory/Memory.h" #include "Emu/Memory/Memory.h"
#include "Emu/System.h" #include "Emu/System.h"
#include "Emu/IdManager.h"
#include "Emu/SysCalls/Modules.h" #include "Emu/SysCalls/Modules.h"
#include "stblib/stb_image.h" #include "stblib/stb_image.h"
#include "Emu/SysCalls/lv2/cellFs.h"
#include "Emu/FS/VFS.h"
#include "Emu/FS/vfsFileBase.h"
#include "Emu/SysCalls/lv2/sys_fs.h"
#include "cellPngDec.h" #include "cellPngDec.h"
#include <map>
extern Module cellPngDec; extern Module cellPngDec;
@ -76,19 +80,15 @@ s32 pngDecOpen(
break; break;
case se32(CELL_PNGDEC_FILE): case se32(CELL_PNGDEC_FILE):
// Get file descriptor {
vm::var<be_t<u32>> fd; // Get file descriptor and size
int ret = cellFsOpen(src->fileName, 0, fd, vm::ptr<const void>::make(0), 0); std::shared_ptr<fs_file_t> file(new fs_file_t(std::shared_ptr<vfsStream>(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)), 0, 0));
stream->fd = fd.value(); if (!file) return CELL_PNGDEC_ERROR_OPEN_FILE;
if (ret != CELL_OK) return CELL_PNGDEC_ERROR_OPEN_FILE; stream->fd = Emu.GetIdManager().GetNewID(file, TYPE_FS_FILE);
stream->fileSize = file->file->GetSize();
// Get size of file
vm::var<CellFsStat> 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
break; break;
} }
}
if (cb) if (cb)
{ {
@ -111,7 +111,8 @@ s32 pngDecOpen(
s32 pngDecClose(CellPngDecSubHandle stream) s32 pngDecClose(CellPngDecSubHandle stream)
{ {
cellFsClose(stream->fd); Emu.GetIdManager().RemoveID<fs_file_t>(stream->fd);
if (!Memory.Free(stream.addr())) if (!Memory.Free(stream.addr()))
{ {
return CELL_PNGDEC_ERROR_FATAL; return CELL_PNGDEC_ERROR_FATAL;
@ -135,7 +136,6 @@ s32 pngReadHeader(
//Write the header to buffer //Write the header to buffer
vm::var<u8[34]> buffer; // Alloc buffer for PNG header vm::var<u8[34]> buffer; // Alloc buffer for PNG header
auto buffer_32 = buffer.To<be_t<u32>>(); auto buffer_32 = buffer.To<be_t<u32>>();
vm::var<be_t<u64>> pos, nread;
switch (stream->src.srcSelect.data()) switch (stream->src.srcSelect.data())
{ {
@ -143,10 +143,13 @@ s32 pngReadHeader(
memmove(buffer.begin(), stream->src.streamPtr.get_ptr(), buffer.size()); memmove(buffer.begin(), stream->src.streamPtr.get_ptr(), buffer.size());
break; break;
case se32(CELL_PNGDEC_FILE): case se32(CELL_PNGDEC_FILE):
cellFsLseek(stream->fd, 0, CELL_SEEK_SET, pos); {
cellFsRead(stream->fd, vm::ptr<void>::make(buffer.addr()), buffer.size(), nread); auto file = Emu.GetIdManager().GetIDData<fs_file_t>(stream->fd);
file->file->Seek(0);
file->file->Read(buffer.begin(), buffer.size());
break; break;
} }
}
if (buffer_32[0].data() != se32(0x89504E47) || if (buffer_32[0].data() != se32(0x89504E47) ||
buffer_32[1].data() != se32(0x0D0A1A0A) || // Error: The first 8 bytes are not a valid PNG signature buffer_32[1].data() != se32(0x0D0A1A0A) || // Error: The first 8 bytes are not a valid PNG signature
@ -244,7 +247,6 @@ s32 pngDecodeData(
//Copy the PNG file to a buffer //Copy the PNG file to a buffer
vm::var<unsigned char[]> png((u32)fileSize); vm::var<unsigned char[]> png((u32)fileSize);
vm::var<be_t<u64>> pos, nread;
switch (stream->src.srcSelect.data()) switch (stream->src.srcSelect.data())
{ {
@ -253,10 +255,13 @@ s32 pngDecodeData(
break; break;
case se32(CELL_PNGDEC_FILE): case se32(CELL_PNGDEC_FILE):
cellFsLseek(fd, 0, CELL_SEEK_SET, pos); {
cellFsRead(fd, vm::ptr<void>::make(png.addr()), png.size(), nread); auto file = Emu.GetIdManager().GetIDData<fs_file_t>(stream->fd);
file->file->Seek(0);
file->file->Read(png.ptr(), png.size());
break; break;
} }
}
//Decode PNG file. (TODO: Is there any faster alternative? Can we do it without external libraries?) //Decode PNG file. (TODO: Is there any faster alternative? Can we do it without external libraries?)
int width, height, actual_components; int width, height, actual_components;

View file

@ -14,7 +14,7 @@ extern s32 cellVideoOutConfigure(u32 videoOut, vm::ptr<CellVideoOutConfiguration
extern s32 cellGcmSetFlipMode(u32 mode); extern s32 cellGcmSetFlipMode(u32 mode);
extern void cellGcmSetFlipHandler(vm::ptr<void(u32)> handler); extern void cellGcmSetFlipHandler(vm::ptr<void(u32)> handler);
extern void cellGcmSetVBlankHandler(vm::ptr<void(u32)> handler); extern void cellGcmSetVBlankHandler(vm::ptr<void(u32)> handler);
extern s32 cellGcmAddressToOffset(u32 address, vm::ptr<be_t<u32>> offset); extern s32 cellGcmAddressToOffset(u32 address, vm::ptr<u32> offset);
extern s32 cellGcmSetDisplayBuffer(u32 id, u32 offset, u32 pitch, u32 width, u32 height); extern s32 cellGcmSetDisplayBuffer(u32 id, u32 offset, u32 pitch, u32 width, u32 height);
extern s32 cellGcmSetPrepareFlip(vm::ptr<CellGcmContextData> ctx, u32 id); extern s32 cellGcmSetPrepareFlip(vm::ptr<CellGcmContextData> ctx, u32 id);
extern s32 cellGcmSetSecondVFrequency(u32 freq); extern s32 cellGcmSetSecondVFrequency(u32 freq);

View file

@ -2428,14 +2428,14 @@ s32 cellSpursCreateTask(vm::ptr<CellSpursTaskset> taskset, vm::ptr<u32> taskId,
return CELL_SPURS_TASK_ERROR_ALIGN; return CELL_SPURS_TASK_ERROR_ALIGN;
} }
vm::var<u32> tmpTaskId; vm::var<be_t<u32>> tmpTaskId;
auto rc = spursCreateTask(taskset, tmpTaskId, vm::ptr<u32>::make(elf_addr), vm::ptr<u32>::make(context_addr), context_size, lsPattern, argument); auto rc = spursCreateTask(taskset, tmpTaskId, vm::ptr<u32>::make(elf_addr), vm::ptr<u32>::make(context_addr), context_size, lsPattern, argument);
if (rc != CELL_OK) if (rc != CELL_OK)
{ {
return rc; return rc;
} }
rc = spursTaskStart(taskset, tmpTaskId); rc = spursTaskStart(taskset, tmpTaskId->value());
if (rc != CELL_OK) if (rc != CELL_OK)
{ {
return rc; return rc;

View file

@ -58,7 +58,7 @@ s32 cellSyncMutexLock(vm::ptr<CellSyncMutex> mutex)
} }
// prx: increase acquire_count and remember its old value // prx: increase acquire_count and remember its old value
const be_t<u16> order = be_t<u16>::make(mutex->acquire_count++); const auto order = mutex->acquire_count++;
// prx: wait until release_count is equal to old acquire_count // prx: wait until release_count is equal to old acquire_count
g_sync_mutex_wm.wait_op(mutex.addr(), [mutex, order]() g_sync_mutex_wm.wait_op(mutex.addr(), [mutex, order]()
@ -882,28 +882,28 @@ void syncLFQueueInit(vm::ptr<CellSyncLFQueue> queue, vm::ptr<u8> buffer, u32 siz
if (direction == CELL_SYNC_QUEUE_ANY2ANY) if (direction == CELL_SYNC_QUEUE_ANY2ANY)
{ {
queue->pop1.write_relaxed({}); queue->pop1 = {};
queue->push1.write_relaxed({}); queue->push1 = {};
queue->m_buffer.set(queue->m_buffer.addr() | 1); queue->m_buffer.set(queue->m_buffer.addr() | 1);
queue->m_bs[0] = -1; queue->m_bs[0] = -1;
queue->m_bs[1] = -1; queue->m_bs[1] = -1;
//m_bs[2] //m_bs[2]
//m_bs[3] //m_bs[3]
queue->m_v1 = -1; queue->m_v1 = -1;
queue->push2.write_relaxed({ be_t<u16>::make(-1) }); queue->push2 = { { be_t<u16>::make(-1) } };
queue->pop2.write_relaxed({ be_t<u16>::make(-1) }); queue->pop2 = { { be_t<u16>::make(-1) } };
} }
else else
{ {
queue->pop1.write_relaxed({ be_t<u16>::make(0), be_t<u16>::make(0), queue->pop1.read_relaxed().m_h3, be_t<u16>::make(0) }); queue->pop1 = { { be_t<u16>::make(0), be_t<u16>::make(0), queue->pop1.read_relaxed().m_h3, be_t<u16>::make(0) } };
queue->push1.write_relaxed({ be_t<u16>::make(0), be_t<u16>::make(0), queue->push1.read_relaxed().m_h7, be_t<u16>::make(0) }); queue->push1 = { { be_t<u16>::make(0), be_t<u16>::make(0), queue->push1.read_relaxed().m_h7, be_t<u16>::make(0) } };
queue->m_bs[0] = -1; // written as u32 queue->m_bs[0] = -1; // written as u32
queue->m_bs[1] = -1; queue->m_bs[1] = -1;
queue->m_bs[2] = -1; queue->m_bs[2] = -1;
queue->m_bs[3] = -1; queue->m_bs[3] = -1;
queue->m_v1 = 0; queue->m_v1 = 0;
queue->push2.write_relaxed({}); queue->push2 = {};
queue->pop2.write_relaxed({}); queue->pop2 = {};
} }
queue->m_v2 = 0; queue->m_v2 = 0;

View file

@ -32,7 +32,7 @@ u32 g_tls_size;
std::array<std::atomic<u32>, TLS_MAX> g_tls_owners; std::array<std::atomic<u32>, 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() void sys_initialize_tls()
{ {
@ -114,7 +114,7 @@ s32 sys_lwmutex_create(vm::ptr<sys_lwmutex_t> lwmutex, vm::ptr<sys_lwmutex_attri
std::shared_ptr<lwmutex_t> lw(new lwmutex_t(protocol, attr->name_u64)); std::shared_ptr<lwmutex_t> 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->attribute = attr->recursive | attr->protocol;
lwmutex->recursive_count = 0; lwmutex->recursive_count = 0;
lwmutex->sleep_queue = Emu.GetIdManager().GetNewID(lw); lwmutex->sleep_queue = Emu.GetIdManager().GetNewID(lw);
@ -127,7 +127,7 @@ s32 sys_lwmutex_destroy(PPUThread& CPU, vm::ptr<sys_lwmutex_t> lwmutex)
sysPrxForUser.Log("sys_lwmutex_destroy(lwmutex=*0x%x)", lwmutex); sysPrxForUser.Log("sys_lwmutex_destroy(lwmutex=*0x%x)", lwmutex);
// check to prevent recursive locking in the next call // 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; return CELL_EBUSY;
} }
@ -575,7 +575,7 @@ s32 sys_lwcond_wait(PPUThread& CPU, vm::ptr<sys_lwcond_t> lwcond, u64 timeout)
const be_t<u32> recursive_value = lwmutex->recursive_count; const be_t<u32> recursive_value = lwmutex->recursive_count;
// set special value // set special value
lwmutex->owner.write_relaxed(lwmutex::reserved); lwmutex->owner = { lwmutex::reserved };
lwmutex->recursive_count = 0; lwmutex->recursive_count = 0;
// call the syscall // call the syscall
@ -766,34 +766,45 @@ std::string ps3_fmt(PPUThread& context, vm::ptr<const char> fmt, u32 g_count, u3
return result; return result;
} }
int _sys_heap_create_heap(const u32 heap_addr, const u32 align, const u32 size) u32 _sys_heap_create_heap(vm::ptr<const char> 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<HeapInfo> heap(new HeapInfo(heap_addr, align, size)); std::shared_ptr<HeapInfo> heap(new HeapInfo(name.get_ptr()));
u32 heap_id = Emu.GetIdManager().GetNewID(heap);
sysPrxForUser.Warning("*** sys_heap created: id = %d", heap_id); return Emu.GetIdManager().GetNewID(heap);
return heap_id;
} }
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<HeapInfo> heap; Emu.GetIdManager().RemoveID<HeapInfo>(heap);
if(!Emu.GetIdManager().GetIDData(heap_id, heap)) return CELL_ESRCH;
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<HeapInfo> heap; return Memory.MainMem.AllocAlign(size, 1);
if(!Emu.GetIdManager().GetIDData(heap_id, heap)) return CELL_ESRCH; }
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() s64 _sys_process_atexitspawn()
@ -812,10 +823,10 @@ s32 sys_interrupt_thread_disestablish(PPUThread& CPU, u32 ih)
{ {
sysPrxForUser.Todo("sys_interrupt_thread_disestablish(ih=%d)", ih); sysPrxForUser.Todo("sys_interrupt_thread_disestablish(ih=%d)", ih);
return _sys_interrupt_thread_disestablish(ih, vm::stackvar<u64>(CPU)); return _sys_interrupt_thread_disestablish(ih, vm::stackvar<be_t<u64>>(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); sysPrxForUser.Log("sys_process_is_stack(p=0x%x)", p);
@ -829,35 +840,36 @@ s64 sys_prx_exitspawn_with_level()
return CELL_OK; return CELL_OK;
} }
int sys_spu_elf_get_information(u32 elf_img, vm::ptr<u32> entry, vm::ptr<u32> nseg) s32 sys_spu_elf_get_information(u32 elf_img, vm::ptr<u32> entry, vm::ptr<u32> 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()); 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; return CELL_OK;
} }
int sys_spu_elf_get_segments(u32 elf_img, vm::ptr<sys_spu_segment> segments, int nseg) s32 sys_spu_elf_get_segments(u32 elf_img, vm::ptr<sys_spu_segment> 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); 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; return CELL_OK;
} }
int sys_spu_image_import(vm::ptr<sys_spu_image> img, u32 src, u32 type) s32 sys_spu_image_import(vm::ptr<sys_spu_image> 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); return spu_image_import(*img, src, type);
} }
int sys_spu_image_close(vm::ptr<sys_spu_image> img) s32 sys_spu_image_close(vm::ptr<sys_spu_image> 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; return CELL_OK;
} }
int sys_raw_spu_load(s32 id, vm::ptr<const char> path, vm::ptr<u32> entry) s32 sys_raw_spu_load(s32 id, vm::ptr<const char> path, vm::ptr<u32> entry)
{ {
sysPrxForUser.Warning("sys_raw_spu_load(id=0x%x, path_addr=0x%x('%s'), entry_addr=0x%x)", sysPrxForUser.Warning("sys_raw_spu_load(id=%d, path=*0x%x, entry=*0x%x)", id, path, entry);
id, path.addr(), path.get_ptr(), entry.addr()); sysPrxForUser.Warning("*** path = '%s'", path.get_ptr());
vfsFile f(path.get_ptr()); vfsFile f(path.get_ptr());
if(!f.IsOpened()) if(!f.IsOpened())
@ -886,9 +898,9 @@ int sys_raw_spu_load(s32 id, vm::ptr<const char> path, vm::ptr<u32> entry)
return CELL_OK; return CELL_OK;
} }
int sys_raw_spu_image_load(int id, vm::ptr<sys_spu_image> img) s32 sys_raw_spu_image_load(s32 id, vm::ptr<sys_spu_image> 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 // TODO: use segment info
memcpy(vm::get_ptr<void>(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * id), vm::get_ptr<void>(img->addr), 256 * 1024); memcpy(vm::get_ptr<void>(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * id), vm::get_ptr<void>(img->addr), 256 * 1024);
@ -897,9 +909,9 @@ int sys_raw_spu_image_load(int id, vm::ptr<sys_spu_image> img)
return CELL_OK; return CELL_OK;
} }
int sys_get_random_number(vm::ptr<u8> addr, u64 size) s32 sys_get_random_number(vm::ptr<u8> 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) if (size > 4096)
size = 4096; size = 4096;
@ -914,77 +926,89 @@ int sys_get_random_number(vm::ptr<u8> addr, u64 size)
vm::ptr<void> _sys_memset(vm::ptr<void> dst, s32 value, u32 size) vm::ptr<void> _sys_memset(vm::ptr<void> 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); memset(dst.get_ptr(), value, size);
return dst; return dst;
} }
vm::ptr<void> _sys_memcpy(vm::ptr<void> dst, vm::ptr<const void> src, u32 size) vm::ptr<void> _sys_memcpy(vm::ptr<void> dst, vm::ptr<const void> 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); memcpy(dst.get_ptr(), src.get_ptr(), size);
return dst; return dst;
} }
s32 _sys_memcmp(vm::ptr<const void> buf1, vm::ptr<const void> buf2, u32 size) s32 _sys_memcmp(vm::ptr<const void> buf1, vm::ptr<const void> 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); return memcmp(buf1.get_ptr(), buf2.get_ptr(), size);
} }
s64 _sys_strlen(vm::ptr<const char> str) s64 _sys_strlen(vm::ptr<const char> str)
{ {
sysPrxForUser.Log("_sys_strlen(str_addr=0x%x)", str.addr()); sysPrxForUser.Log("_sys_strlen(str=*0x%x)", str);
return strlen(str.get_ptr()); return strlen(str.get_ptr());
} }
s32 _sys_strcmp(vm::ptr<const char> str1, vm::ptr<const char> 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<const char> str1, vm::ptr<const char> str2, s32 max) s32 _sys_strncmp(vm::ptr<const char> str1, vm::ptr<const char> 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); return strncmp(str1.get_ptr(), str2.get_ptr(), max);
} }
vm::ptr<char> _sys_strcat(vm::ptr<char> dest, vm::ptr<const char> source) vm::ptr<char> _sys_strcat(vm::ptr<char> dest, vm::ptr<const char> 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()) if (strcat(dest.get_ptr(), source.get_ptr()) != dest.get_ptr())
{ {
assert(!"strcat(): unexpected result"); throw "_sys_strcat() failed: unexpected strcat() result";
} }
return dest; return dest;
} }
vm::ptr<char> _sys_strncat(vm::ptr<char> dest, vm::ptr<const char> source, u32 len) vm::ptr<char> _sys_strncat(vm::ptr<char> dest, vm::ptr<const char> 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()) 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; return dest;
} }
vm::ptr<char> _sys_strcpy(vm::ptr<char> dest, vm::ptr<const char> source) vm::ptr<char> _sys_strcpy(vm::ptr<char> dest, vm::ptr<const char> 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()) if (strcpy(dest.get_ptr(), source.get_ptr()) != dest.get_ptr())
{ {
assert(!"strcpy(): unexpected result"); throw "_sys_strcpy() failed: unexpected strcpy() result";
} }
return dest; return dest;
} }
vm::ptr<char> _sys_strncpy(vm::ptr<char> dest, vm::ptr<const char> source, u32 len) vm::ptr<char> _sys_strncpy(vm::ptr<char> dest, vm::ptr<const char> 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) if (!dest || !source)
{ {
@ -993,24 +1017,20 @@ vm::ptr<char> _sys_strncpy(vm::ptr<char> dest, vm::ptr<const char> source, u32 l
if (strncpy(dest.get_ptr(), source.get_ptr(), len) != dest.get_ptr()) 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; return dest;
} }
vm::ptr<spu_printf_cb_t> spu_printf_agcb; spu_printf_cb_t spu_printf_agcb;
vm::ptr<spu_printf_cb_t> spu_printf_dgcb; spu_printf_cb_t spu_printf_dgcb;
vm::ptr<spu_printf_cb_t> spu_printf_atcb; spu_printf_cb_t spu_printf_atcb;
vm::ptr<spu_printf_cb_t> spu_printf_dtcb; spu_printf_cb_t spu_printf_dtcb;
s32 _sys_spu_printf_initialize( 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)
vm::ptr<spu_printf_cb_t> agcb,
vm::ptr<spu_printf_cb_t> dgcb,
vm::ptr<spu_printf_cb_t> atcb,
vm::ptr<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)", sysPrxForUser.Warning("_sys_spu_printf_initialize(agcb=*0x%x, dgcb=*0x%x, atcb=*0x%x, dtcb=*0x%x)", agcb, dgcb, atcb, dtcb);
agcb.addr(), dgcb.addr(), atcb.addr(), dtcb.addr());
// prx: register some callbacks // prx: register some callbacks
spu_printf_agcb = agcb; spu_printf_agcb = agcb;
@ -1079,9 +1099,32 @@ s32 _sys_spu_printf_detach_thread(PPUThread& CPU, u32 thread)
return spu_printf_dtcb(CPU, 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<char> dst, u32 count, vm::ptr<const char> fmt) // va_args... s32 _sys_snprintf(PPUThread& CPU, vm::ptr<char> dst, u32 count, vm::ptr<const char> 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); std::string result = ps3_fmt(CPU, fmt, 3, 0, 0);
@ -1103,7 +1146,7 @@ s32 _sys_snprintf(PPUThread& CPU, vm::ptr<char> dst, u32 count, vm::ptr<const ch
s32 _sys_printf(vm::ptr<const char> fmt) // va_args... s32 _sys_printf(vm::ptr<const char> fmt) // va_args...
{ {
sysPrxForUser.Todo("_sys_printf(fmt_addr=0x%x, ...)", fmt.addr()); sysPrxForUser.Todo("_sys_printf(fmt=*0x%x, ...)", fmt);
// probably, assertion failed // probably, assertion failed
sysPrxForUser.Warning("_sys_printf: \n%s", fmt.get_ptr()); sysPrxForUser.Warning("_sys_printf: \n%s", fmt.get_ptr());
@ -1113,7 +1156,7 @@ s32 _sys_printf(vm::ptr<const char> fmt) // va_args...
s32 sys_process_get_paramsfo(vm::ptr<char> buffer) s32 sys_process_get_paramsfo(vm::ptr<char> 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 // prx: load some data (0x40 bytes) previously set by _sys_process_get_paramsfo syscall
return _sys_process_get_paramsfo(buffer); 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_load_module_on_memcontainer);
REG_FUNC(sysPrxForUser, sys_prx_exitspawn_with_level); 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_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_memalign);
REG_FUNC(sysPrxForUser, _sys_heap_free);
REG_FUNC(sysPrxForUser, sys_mmapper_allocate_memory); REG_FUNC(sysPrxForUser, sys_mmapper_allocate_memory);
REG_FUNC(sysPrxForUser, sys_mmapper_allocate_memory_from_container); 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_memcpy);
REG_FUNC(sysPrxForUser, _sys_memcmp); REG_FUNC(sysPrxForUser, _sys_memcmp);
REG_FUNC(sysPrxForUser, _sys_strlen); REG_FUNC(sysPrxForUser, _sys_strlen);
REG_FUNC(sysPrxForUser, _sys_strcmp);
REG_FUNC(sysPrxForUser, _sys_strncmp); REG_FUNC(sysPrxForUser, _sys_strncmp);
REG_FUNC(sysPrxForUser, _sys_strcat); REG_FUNC(sysPrxForUser, _sys_strcat);
REG_FUNC(sysPrxForUser, _sys_strncat); 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_attach_thread);
REG_FUNC(sysPrxForUser, _sys_spu_printf_detach_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_snprintf);
REG_FUNC(sysPrxForUser, _sys_printf); REG_FUNC(sysPrxForUser, _sys_printf);

View file

@ -2,25 +2,21 @@
struct HeapInfo struct HeapInfo
{ {
u32 heap_addr; const std::string name;
u32 align;
u32 size;
HeapInfo(u32 _heap_addr, u32 _align, u32 _size) HeapInfo(const char* name)
: heap_addr(_heap_addr) : name(name)
, align(_align)
, size(_size)
{ {
} }
}; };
typedef s32(spu_printf_cb_t)(u32 arg); using spu_printf_cb_t = vm::ptr<s32(u32 arg)>;
// Aux // Aux
extern vm::ptr<spu_printf_cb_t> spu_printf_agcb; extern spu_printf_cb_t spu_printf_agcb;
extern vm::ptr<spu_printf_cb_t> spu_printf_dgcb; extern spu_printf_cb_t spu_printf_dgcb;
extern vm::ptr<spu_printf_cb_t> spu_printf_atcb; extern spu_printf_cb_t spu_printf_atcb;
extern vm::ptr<spu_printf_cb_t> spu_printf_dtcb; extern spu_printf_cb_t spu_printf_dtcb;
// Functions // Functions
vm::ptr<void> _sys_memset(vm::ptr<void> dst, s32 value, u32 size); vm::ptr<void> _sys_memset(vm::ptr<void> dst, s32 value, u32 size);

View file

@ -6,7 +6,6 @@
#include "Emu/System.h" #include "Emu/System.h"
#include "ModuleManager.h" #include "ModuleManager.h"
#include "lv2/cellFs.h"
#include "lv2/sleep_queue.h" #include "lv2/sleep_queue.h"
#include "lv2/sys_lwmutex.h" #include "lv2/sys_lwmutex.h"
#include "lv2/sys_lwcond.h" #include "lv2/sys_lwcond.h"
@ -29,6 +28,7 @@
#include "lv2/sys_trace.h" #include "lv2/sys_trace.h"
#include "lv2/sys_tty.h" #include "lv2/sys_tty.h"
#include "lv2/sys_vm.h" #include "lv2/sys_vm.h"
#include "lv2/sys_fs.h"
#include "Emu/SysCalls/Modules/cellGcmSys.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, null_func, null_func, null_func, null_func, //799 UNS
null_func,//bind_func(sys_fs_test), //800 (0x320) null_func,//bind_func(sys_fs_test), //800 (0x320)
bind_func(cellFsOpen), //801 (0x321) bind_func(sys_fs_open), //801 (0x321)
bind_func(cellFsRead), //802 (0x322) bind_func(sys_fs_read), //802 (0x322)
bind_func(cellFsWrite), //803 (0x323) bind_func(sys_fs_write), //803 (0x323)
bind_func(cellFsClose), //804 (0x324) bind_func(sys_fs_close), //804 (0x324)
bind_func(cellFsOpendir), //805 (0x325) bind_func(sys_fs_opendir), //805 (0x325)
bind_func(cellFsReaddir), //806 (0x326) bind_func(sys_fs_readdir), //806 (0x326)
bind_func(cellFsClosedir), //807 (0x327) bind_func(sys_fs_closedir), //807 (0x327)
bind_func(cellFsStat), //808 (0x328) bind_func(sys_fs_stat), //808 (0x328)
bind_func(cellFsFstat), //809 (0x329) bind_func(sys_fs_fstat), //809 (0x329)
null_func,//bind_func(sys_fs_link), //810 (0x32A) null_func,//bind_func(sys_fs_link), //810 (0x32A)
bind_func(cellFsMkdir), //811 (0x32B) bind_func(sys_fs_mkdir), //811 (0x32B)
bind_func(cellFsRename), //812 (0x32C) bind_func(sys_fs_rename), //812 (0x32C)
bind_func(cellFsRmdir), //813 (0x32D) bind_func(sys_fs_rmdir), //813 (0x32D)
bind_func(cellFsUnlink), //814 (0x32E) bind_func(sys_fs_unlink), //814 (0x32E)
null_func,//bind_func(cellFsUtime), //815 (0x32F) null_func,//bind_func(sys_fs_utime), //815 (0x32F)
null_func,//bind_func(sys_fs_access), //816 (0x330) null_func,//bind_func(sys_fs_access), //816 (0x330)
null_func,//bind_func(sys_fs_fcntl), //817 (0x331) 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(sys_fs_fdatasync), //819 (0x333)
null_func,//bind_func(cellFsFsync), //820 (0x334) null_func,//bind_func(sys_fs_fsync), //820 (0x334)
bind_func(cellFsFGetBlockSize), //821 (0x335) bind_func(sys_fs_fget_block_size), //821 (0x335)
bind_func(cellFsGetBlockSize), //822 (0x336) 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_read), //823 (0x337)
null_func,//bind_func(sys_fs_acl_write), //824 (0x338) null_func,//bind_func(sys_fs_acl_write), //824 (0x338)
null_func,//bind_func(sys_fs_lsn_get_cda_size), //825 (0x339) 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_unlock), //828 (0x33C)
null_func,//bind_func(sys_fs_lsn_read), //829 (0x33D) null_func,//bind_func(sys_fs_lsn_read), //829 (0x33D)
null_func,//bind_func(sys_fs_lsn_write), //830 (0x33E) null_func,//bind_func(sys_fs_lsn_write), //830 (0x33E)
bind_func(cellFsTruncate), //831 (0x33F) bind_func(sys_fs_truncate), //831 (0x33F)
bind_func(cellFsFtruncate), //832 (0x340) bind_func(sys_fs_ftruncate), //832 (0x340)
null_func,//bind_func(sys_fs_symbolic_link), //833 (0x341) 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_chown), //835 (0x343)
null_func,//bind_func(sys_fs_newfs), //836 (0x344) null_func,//bind_func(sys_fs_newfs), //836 (0x344)
null_func,//bind_func(sys_fs_mount), //837 (0x345) null_func,//bind_func(sys_fs_mount), //837 (0x345)

File diff suppressed because it is too large Load diff

View file

@ -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<u32> st_mode;
be_t<s32> st_uid;
be_t<s32> st_gid;
be_t<u64> st_atime_;
be_t<u64> st_mtime_;
be_t<u64> st_ctime_;
be_t<u64> st_size;
be_t<u64> st_blksize;
};
struct CellFsUtimbuf
{
be_t<u64> actime;
be_t<u64> 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<u32> fd;
be_t<u64> offset;
vm::bptr<void> buf;
be_t<u64> size;
be_t<u64> user_data;
};
struct CellFsDirectoryEntry
{
CellFsStat attribute;
CellFsDirent entry_name;
};
struct CellFsRingBuffer
{
be_t<u64> ringbuf_size;
be_t<u64> block_size;
be_t<u64> transfer_rate;
be_t<u32> copy;
};
// SysCalls
s32 cellFsOpen(vm::ptr<const char> path, s32 flags, vm::ptr<be_t<u32>> fd, vm::ptr<const void> arg, u64 size);
s32 cellFsRead(u32 fd, vm::ptr<void> buf, u64 nbytes, vm::ptr<be_t<u64>> nread);
s32 cellFsWrite(u32 fd, vm::ptr<const void> buf, u64 nbytes, vm::ptr<u64> nwrite);
s32 cellFsClose(u32 fd);
s32 cellFsOpendir(vm::ptr<const char> path, vm::ptr<u32> fd);
s32 cellFsReaddir(u32 fd, vm::ptr<CellFsDirent> dir, vm::ptr<u64> nread);
s32 cellFsClosedir(u32 fd);
s32 cellFsStat(vm::ptr<const char> path, vm::ptr<CellFsStat> sb);
s32 cellFsFstat(u32 fd, vm::ptr<CellFsStat> sb);
s32 cellFsMkdir(vm::ptr<const char> path, u32 mode);
s32 cellFsRename(vm::ptr<const char> from, vm::ptr<const char> to);
s32 cellFsChmod(vm::ptr<const char> path, u32 mode);
s32 cellFsFsync(u32 fd);
s32 cellFsRmdir(vm::ptr<const char> path);
s32 cellFsUnlink(vm::ptr<const char> path);
s32 cellFsLseek(u32 fd, s64 offset, u32 whence, vm::ptr<be_t<u64>> pos);
s32 cellFsFtruncate(u32 fd, u64 size);
s32 cellFsTruncate(vm::ptr<const char> path, u64 size);
s32 cellFsFGetBlockSize(u32 fd, vm::ptr<u64> sector_size, vm::ptr<u64> block_size);
s32 cellFsGetBlockSize(vm::ptr<const char> path, vm::ptr<u64> sector_size, vm::ptr<u64> block_size);
s32 cellFsGetFreeSize(vm::ptr<const char> path, vm::ptr<u32> block_size, vm::ptr<u64> block_count);
s32 cellFsGetDirectoryEntries(u32 fd, vm::ptr<CellFsDirectoryEntry> entries, u32 entries_size, vm::ptr<u32> data_count);
// no need to add every function here

View file

@ -170,11 +170,15 @@ s32 sys_cond_wait(PPUThread& CPU, u32 cond_id, u64 timeout)
} }
// protocol is ignored in current implementation // protocol is ignored in current implementation
cond->waiters++; assert(cond->waiters > 0); cond->waiters++;
// unlock mutex // unlock mutex
cond->mutex->owner.reset(); cond->mutex->owner.reset();
if (cond->mutex->waiters)
{
cond->mutex->cv.notify_one(); cond->mutex->cv.notify_one();
}
// save recursive value // save recursive value
const u32 recursive_value = cond->mutex->recursive_count.exchange(0); 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 // cancel waiting if the mutex is free, restore its owner and recursive value
cond->mutex->owner = thread; cond->mutex->owner = thread;
cond->mutex->recursive_count = recursive_value; cond->mutex->recursive_count = recursive_value;
cond->waiters--; assert(cond->waiters >= 0); cond->waiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }

View file

@ -24,7 +24,7 @@ struct cond_t
// TODO: use sleep queue, possibly remove condition variable // TODO: use sleep queue, possibly remove condition variable
std::condition_variable cv; std::condition_variable cv;
std::atomic<s32> waiters; std::atomic<u32> waiters;
cond_t(std::shared_ptr<mutex_t>& mutex, u64 name) cond_t(std::shared_ptr<mutex_t>& mutex, u64 name)
: mutex(mutex) : mutex(mutex)

View file

@ -49,18 +49,11 @@ s32 sys_event_queue_create(vm::ptr<u32> equeue_id, vm::ptr<sys_event_queue_attr>
default: sys_event.Error("sys_event_queue_create(): unknown type (0x%x)", type); return CELL_EINVAL; 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<event_queue_t> queue(new event_queue_t(protocol, type, attr->name_u64, event_queue_key, size)); std::shared_ptr<event_queue_t> queue(new event_queue_t(protocol, type, attr->name_u64, event_queue_key, size));
if (!Emu.GetEventManager().RegisterKey(queue, event_queue_key)) if (!Emu.GetEventManager().RegisterKey(queue, event_queue_key))
{ {
return CELL_EAGAIN; return CELL_EEXIST;
} }
*equeue_id = Emu.GetIdManager().GetNewID(queue, TYPE_EVENT_QUEUE); *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; return CELL_EINVAL;
} }
assert(queue->waiters >= 0);
if (!mode && queue->waiters) if (!mode && queue->waiters)
{ {
return CELL_EBUSY; 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(); 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<sys_event_t> event_array, s32 size, vm::ptr<u32> number) s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr<sys_event_t> event_array, s32 size, vm::ptr<u32> 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; LV2_LOCK;
@ -130,7 +125,7 @@ s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr<sys_event_t> event_array,
s32 count = 0; s32 count = 0;
while (count < size && queue->events.size()) while (!queue->waiters && count < size && queue->events.size())
{ {
auto& event = queue->events.front(); auto& event = queue->events.front();
event_array[count++] = { be_t<u64>::make(event.source), be_t<u64>::make(event.data1), be_t<u64>::make(event.data2), be_t<u64>::make(event.data3) }; event_array[count++] = { be_t<u64>::make(event.source), be_t<u64>::make(event.data1), be_t<u64>::make(event.data2), be_t<u64>::make(event.data3) };
@ -140,11 +135,6 @@ s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr<sys_event_t> event_array,
*number = count; *number = count;
if (queue->events.size())
{
queue->cv.notify_one();
}
return CELL_OK; return CELL_OK;
} }
@ -169,19 +159,19 @@ s32 sys_event_queue_receive(PPUThread& CPU, u32 equeue_id, vm::ptr<sys_event_t>
} }
// protocol is ignored in current implementation // protocol is ignored in current implementation
queue->waiters++; assert(queue->waiters > 0); queue->waiters++;
while (queue->events.empty()) while (queue->events.empty())
{ {
if (queue->waiters < 0) if (queue->cancelled)
{ {
queue->waiters--; assert(queue->waiters < 0); queue->waiters--;
return CELL_ECANCELED; return CELL_ECANCELED;
} }
if (timeout && get_system_time() - start_time > timeout) if (timeout && get_system_time() - start_time > timeout)
{ {
queue->waiters--; assert(queue->waiters >= 0); queue->waiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }
@ -202,12 +192,7 @@ s32 sys_event_queue_receive(PPUThread& CPU, u32 equeue_id, vm::ptr<sys_event_t>
CPU.GPR[7] = event.data3; CPU.GPR[7] = event.data3;
queue->events.pop_front(); queue->events.pop_front();
queue->waiters--; assert(queue->waiters >= 0); queue->waiters--;
if (queue->events.size())
{
queue->cv.notify_one();
}
return CELL_OK; return CELL_OK;
} }
@ -247,8 +232,6 @@ s32 sys_event_port_create(vm::ptr<u32> eport_id, s32 port_type, u64 name)
return CELL_EINVAL; return CELL_EINVAL;
} }
LV2_LOCK;
std::shared_ptr<event_port_t> eport(new event_port_t(port_type, name)); std::shared_ptr<event_port_t> eport(new event_port_t(port_type, name));
*eport_id = Emu.GetIdManager().GetNewID(eport, TYPE_EVENT_PORT); *eport_id = Emu.GetIdManager().GetNewID(eport, TYPE_EVENT_PORT);

View file

@ -78,10 +78,11 @@ struct event_queue_t
const s32 size; const s32 size;
std::deque<event_t> events; std::deque<event_t> events;
std::atomic<bool> cancelled;
// TODO: use sleep queue, possibly remove condition variable // TODO: use sleep queue, possibly remove condition variable
std::condition_variable cv; std::condition_variable cv;
std::atomic<s32> waiters; std::atomic<u32> waiters;
event_queue_t(u32 protocol, s32 type, u64 name, u64 key, s32 size) event_queue_t(u32 protocol, s32 type, u64 name, u64 key, s32 size)
: protocol(protocol) : protocol(protocol)
@ -89,6 +90,7 @@ struct event_queue_t
, name(name) , name(name)
, key(key) , key(key)
, size(size) , size(size)
, cancelled(false)
, waiters(0) , waiters(0)
{ {
} }
@ -96,8 +98,12 @@ struct event_queue_t
void push(u64 source, u64 data1, u64 data2, u64 data3) void push(u64 source, u64 data1, u64 data2, u64 data3)
{ {
events.emplace_back(source, data1, data2, data3); events.emplace_back(source, data1, data2, data3);
if (waiters)
{
cv.notify_one(); cv.notify_one();
} }
}
}; };
struct event_port_t struct event_port_t

View file

@ -16,8 +16,6 @@ s32 sys_event_flag_create(vm::ptr<u32> id, vm::ptr<sys_event_flag_attr> attr, u6
{ {
sys_event_flag.Warning("sys_event_flag_create(id=*0x%x, attr=*0x%x, init=0x%llx)", id, attr, init); 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) if (!id || !attr)
{ {
return CELL_EFAULT; return CELL_EFAULT;
@ -69,11 +67,6 @@ s32 sys_event_flag_destroy(u32 id)
return CELL_ESRCH; return CELL_ESRCH;
} }
while (ef->waiters < 0)
{
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
}
if (ef->waiters) if (ef->waiters)
{ {
return CELL_EBUSY; return CELL_EBUSY;
@ -124,7 +117,7 @@ s32 sys_event_flag_wait(u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result, u64 t
return CELL_EPERM; return CELL_EPERM;
} }
while (ef->waiters < 0) while (ef->cancelled)
{ {
// wait until other threads return CELL_ECANCELED (to prevent modifying bit pattern) // wait until other threads return CELL_ECANCELED (to prevent modifying bit pattern)
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); 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<u64> result, u64 t
break; break;
} }
if (ef->waiters <= 0) if (ef->cancelled)
{ {
ef->waiters++; assert(ef->waiters <= 0); if (!--ef->cancelled)
if (!ef->waiters)
{ {
ef->cv.notify_all(); ef->cv.notify_all();
} }
@ -164,7 +155,7 @@ s32 sys_event_flag_wait(u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result, u64 t
if (timeout && get_system_time() - start_time > timeout) if (timeout && get_system_time() - start_time > timeout)
{ {
ef->waiters--; assert(ef->waiters >= 0); ef->waiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }
@ -187,9 +178,7 @@ s32 sys_event_flag_wait(u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result, u64 t
ef->flags = 0; ef->flags = 0;
} }
ef->waiters--; assert(ef->waiters >= 0); if (--ef->waiters && ef->flags)
if (ef->flags)
{ {
ef->cv.notify_one(); ef->cv.notify_one();
} }
@ -235,7 +224,7 @@ s32 sys_event_flag_trywait(u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result)
return CELL_EBUSY; return CELL_EBUSY;
} }
while (ef->waiters < 0) while (ef->cancelled)
{ {
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); 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; return CELL_ESRCH;
} }
while (ef->waiters < 0) while (ef->cancelled)
{ {
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
} }
ef->flags |= bitptn; ef->flags |= bitptn;
if (ef->waiters)
{
ef->cv.notify_all(); ef->cv.notify_all();
}
return CELL_OK; return CELL_OK;
} }
@ -294,7 +287,7 @@ s32 sys_event_flag_clear(u32 id, u64 bitptn)
return CELL_ESRCH; return CELL_ESRCH;
} }
while (ef->waiters < 0) while (ef->cancelled)
{ {
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
} }
@ -322,7 +315,7 @@ s32 sys_event_flag_cancel(u32 id, vm::ptr<u32> num)
return CELL_ESRCH; return CELL_ESRCH;
} }
while (ef->waiters < 0) while (ef->cancelled)
{ {
ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1)); ef->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
} }
@ -332,9 +325,10 @@ s32 sys_event_flag_cancel(u32 id, vm::ptr<u32> num)
*num = ef->waiters; *num = ef->waiters;
} }
// negate value to signal waiting threads and prevent modifying bit pattern if ((ef->cancelled = ef->waiters.exchange(0)))
ef->waiters = -ef->waiters; {
ef->cv.notify_all(); ef->cv.notify_all();
}
return CELL_OK; return CELL_OK;
} }

View file

@ -34,16 +34,18 @@ struct event_flag_t
const u64 name; const u64 name;
std::atomic<u64> flags; std::atomic<u64> flags;
std::atomic<u32> cancelled;
// TODO: use sleep queue, possibly remove condition variable // TODO: use sleep queue, possibly remove condition variable
std::condition_variable cv; std::condition_variable cv;
std::atomic<s32> waiters; std::atomic<u32> waiters;
event_flag_t(u64 pattern, u32 protocol, s32 type, u64 name) event_flag_t(u64 pattern, u32 protocol, s32 type, u64 name)
: flags(pattern) : flags(pattern)
, protocol(protocol) , protocol(protocol)
, type(type) , type(type)
, name(name) , name(name)
, cancelled(0)
, waiters(0) , waiters(0)
{ {
} }

View file

@ -0,0 +1,554 @@
#include "stdafx.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
#include "Emu/IdManager.h"
#include "Emu/SysCalls/SysCalls.h"
#ifdef _WIN32
#include <windows.h>
#undef CreateFile
#else
#include <sys/types.h>
#include <sys/stat.h>
#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<const char> path, s32 flags, vm::ptr<u32> fd, s32 mode, vm::ptr<const void> arg, u64 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<vfsStream> file;
// TODO: other checks for path
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:
{
file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsRead));
break;
}
case CELL_FS_O_WRONLY:
case CELL_FS_O_RDWR:
{
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(), vfsReadWrite));
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));
if ((!file || !file->IsOpened()) && Emu.GetVFS().ExistsFile(path.get_ptr()))
{
return CELL_FS_EEXIST;
}
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());
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 | 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 (%#o)", flags);
return CELL_FS_EINVAL;
}
}
if (!file || !file->IsOpened())
{
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<fs_file_t> file_handler(new fs_file_t(file, mode, flags));
*fd = Emu.GetIdManager().GetNewID(file_handler, TYPE_FS_FILE);
return CELL_OK;
}
s32 sys_fs_read(u32 fd, vm::ptr<void> buf, u64 nbytes, vm::ptr<u64> 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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file) || file->flags & CELL_FS_O_WRONLY)
{
return CELL_FS_EBADF;
}
*nread = file->file->Read(buf.get_ptr(), nbytes);
return CELL_OK;
}
s32 sys_fs_write(u32 fd, vm::ptr<const void> buf, u64 nbytes, vm::ptr<u64> 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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file) || !(file->flags & CELL_FS_O_ACCMODE))
{
return CELL_FS_EBADF;
}
// TODO: return CELL_FS_EBUSY if locked
*nwrite = file->file->Write(buf.get_ptr(), nbytes);
return CELL_OK;
}
s32 sys_fs_close(u32 fd)
{
sys_fs.Log("sys_fs_close(fd=0x%x)", fd);
std::shared_ptr<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file))
{
return CELL_FS_EBADF;
}
// TODO: return CELL_FS_EBUSY if locked
Emu.GetIdManager().RemoveID<fs_file_t>(fd);
return CELL_OK;
}
s32 sys_fs_opendir(vm::ptr<const char> path, vm::ptr<u32> 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<vfsDirBase> directory(Emu.GetVFS().OpenDir(path.get_ptr()));
if (!directory || !directory->IsOpened())
{
sys_fs.Error("sys_fs_opendir(): failed to open '%s'", path.get_ptr());
return CELL_FS_ENOENT;
}
*fd = Emu.GetIdManager().GetNewID(directory, TYPE_FS_DIR);
return CELL_OK;
}
s32 sys_fs_readdir(u32 fd, vm::ptr<CellFsDirent> dir, vm::ptr<u64> nread)
{
sys_fs.Warning("sys_fs_readdir(fd=0x%x, dir=*0x%x, nread=*0x%x)", fd, dir, nread);
std::shared_ptr<vfsDirBase> 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<size_t>(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.Log("sys_fs_closedir(fd=0x%x)", fd);
std::shared_ptr<vfsDirBase> directory;
if (!Emu.GetIdManager().GetIDData(fd, directory))
{
return CELL_ESRCH;
}
Emu.GetIdManager().RemoveID<vfsDirBase>(fd);
return CELL_OK;
}
s32 sys_fs_stat(vm::ptr<const char> path, vm::ptr<CellFsStat> 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<CellFsStat> sb)
{
sys_fs.Warning("sys_fs_fstat(fd=0x%x, sb=*0x%x)", fd, sb);
std::shared_ptr<fs_file_t> 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->file->GetSize();
sb->blksize = 4096;
return CELL_OK;
}
s32 sys_fs_mkdir(vm::ptr<const char> path, s32 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();
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<const char> from, vm::ptr<const char> 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<const char> 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<const char> 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<u64> 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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file))
return CELL_ESRCH;
*pos = file->file->Seek(offset, seek_mode);
return CELL_OK;
}
s32 sys_fs_fget_block_size(u32 fd, vm::ptr<u64> sector_size, vm::ptr<u64> block_size, vm::ptr<u64> arg4, vm::ptr<u64> 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<fs_file_t> 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<const char> path, vm::ptr<u64> sector_size, vm::ptr<u64> block_size, vm::ptr<u64> 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<const char> 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<fs_file_t> file;
if (!Emu.GetIdManager().GetIDData(fd, file))
return CELL_ESRCH;
u64 initialSize = file->file->GetSize();
if (initialSize < size)
{
u64 last_pos = file->file->Tell();
file->file->Seek(0, vfsSeekEnd);
static const char nullbyte = 0;
file->file->Seek(size - initialSize - 1, vfsSeekCur);
file->file->Write(&nullbyte, sizeof(char));
file->file->Seek(last_pos, vfsSeekSet);
}
if (initialSize > size)
{
// (TODO)
}
return CELL_OK;
}
s32 sys_fs_chmod(vm::ptr<const char> path, s32 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;
}

View file

@ -0,0 +1,180 @@
#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<s32> mode;
be_t<s32> uid;
be_t<s32> gid;
be_t<s64> atime;
be_t<s64> mtime;
be_t<s64> ctime;
be_t<u64> size;
be_t<u64> blksize;
};
struct CellFsUtimbuf
{
be_t<s64> actime;
be_t<s64> modtime;
};
#pragma pack(pop)
struct fs_file_t
{
const std::shared_ptr<vfsStream> file;
const s32 mode;
const s32 flags;
fs_file_t(std::shared_ptr<vfsStream>& file, s32 mode, s32 flags)
: file(file)
, mode(mode)
, flags(flags)
{
}
};
// SysCalls
s32 sys_fs_open(vm::ptr<const char> path, s32 flags, vm::ptr<u32> fd, s32 mode, vm::ptr<const void> arg, u64 size);
s32 sys_fs_read(u32 fd, vm::ptr<void> buf, u64 nbytes, vm::ptr<u64> nread);
s32 sys_fs_write(u32 fd, vm::ptr<const void> buf, u64 nbytes, vm::ptr<u64> nwrite);
s32 sys_fs_close(u32 fd);
s32 sys_fs_opendir(vm::ptr<const char> path, vm::ptr<u32> fd);
s32 sys_fs_readdir(u32 fd, vm::ptr<CellFsDirent> dir, vm::ptr<u64> nread);
s32 sys_fs_closedir(u32 fd);
s32 sys_fs_stat(vm::ptr<const char> path, vm::ptr<CellFsStat> sb);
s32 sys_fs_fstat(u32 fd, vm::ptr<CellFsStat> sb);
s32 sys_fs_mkdir(vm::ptr<const char> path, s32 mode);
s32 sys_fs_rename(vm::ptr<const char> from, vm::ptr<const char> to);
s32 sys_fs_rmdir(vm::ptr<const char> path);
s32 sys_fs_unlink(vm::ptr<const char> path);
s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr<u64> pos);
s32 sys_fs_fget_block_size(u32 fd, vm::ptr<u64> sector_size, vm::ptr<u64> block_size, vm::ptr<u64> arg4, vm::ptr<u64> arg5);
s32 sys_fs_get_block_size(vm::ptr<const char> path, vm::ptr<u64> sector_size, vm::ptr<u64> block_size, vm::ptr<u64> arg4);
s32 sys_fs_truncate(vm::ptr<const char> path, u64 size);
s32 sys_fs_ftruncate(u32 fd, u64 size);
s32 sys_fs_chmod(vm::ptr<const char> path, s32 mode);

View file

@ -38,6 +38,7 @@ s32 _sys_lwcond_destroy(u32 lwcond_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lwcond_t> cond; std::shared_ptr<lwcond_t> cond;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{ {
return CELL_ESRCH; 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; LV2_LOCK;
std::shared_ptr<lwcond_t> cond; std::shared_ptr<lwcond_t> cond;
std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
std::shared_ptr<lwmutex_t> mutex;
if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{ {
return CELL_ESRCH; 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->signaled1++;
} }
cond->waiters--; if (--cond->waiters)
{
cond->cv.notify_one(); cond->cv.notify_one();
}
return CELL_OK; return CELL_OK;
} }
@ -130,12 +134,13 @@ s32 _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lwcond_t> cond; std::shared_ptr<lwcond_t> cond;
std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
std::shared_ptr<lwmutex_t> mutex;
if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{ {
return CELL_ESRCH; 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); const s32 count = cond->waiters.exchange(0);
if (count)
{
cond->cv.notify_all(); cond->cv.notify_all();
}
if (mode == 1) if (mode == 1)
{ {
@ -176,12 +185,13 @@ s32 _sys_lwcond_queue_wait(u32 lwcond_id, u32 lwmutex_id, u64 timeout)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lwcond_t> cond; std::shared_ptr<lwcond_t> cond;
std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond)) if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -189,10 +199,14 @@ s32 _sys_lwcond_queue_wait(u32 lwcond_id, u32 lwmutex_id, u64 timeout)
// finalize unlocking the mutex // finalize unlocking the mutex
mutex->signaled++; mutex->signaled++;
if (mutex->waiters)
{
mutex->cv.notify_one(); mutex->cv.notify_one();
}
// protocol is ignored in current implementation // protocol is ignored in current implementation
cond->waiters++; assert(cond->waiters > 0); cond->waiters++;
while (!(cond->signaled1 && mutex->signaled) && !cond->signaled2) 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) if (is_timedout && !cond->signaled1)
{ {
// cancel waiting // cancel waiting
cond->waiters--; assert(cond->waiters >= 0); cond->waiters--;
if (mutex->signaled) if (mutex->signaled)
{ {

View file

@ -26,7 +26,7 @@ struct lwcond_t
// TODO: use sleep queue // TODO: use sleep queue
std::condition_variable cv; std::condition_variable cv;
std::atomic<s32> waiters; std::atomic<u32> waiters;
lwcond_t(u64 name) lwcond_t(u64 name)
: name(name) : name(name)

View file

@ -16,7 +16,7 @@ void lwmutex_create(sys_lwmutex_t& lwmutex, bool recursive, u32 protocol, u64 na
{ {
std::shared_ptr<lwmutex_t> mutex(new lwmutex_t(protocol, name)); std::shared_ptr<lwmutex_t> 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.attribute = protocol | (recursive ? SYS_SYNC_RECURSIVE : SYS_SYNC_NOT_RECURSIVE);
lwmutex.recursive_count = 0; lwmutex.recursive_count = 0;
lwmutex.sleep_queue = Emu.GetIdManager().GetNewID(mutex, TYPE_LWMUTEX); lwmutex.sleep_queue = Emu.GetIdManager().GetNewID(mutex, TYPE_LWMUTEX);
@ -53,6 +53,7 @@ s32 _sys_lwmutex_destroy(u32 lwmutex_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lwmutex_t> mutex; std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -77,19 +78,20 @@ s32 _sys_lwmutex_lock(u32 lwmutex_id, u64 timeout)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lwmutex_t> mutex; std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
// protocol is ignored in current implementation // protocol is ignored in current implementation
mutex->waiters++; assert(mutex->waiters > 0); mutex->waiters++;
while (!mutex->signaled) while (!mutex->signaled)
{ {
if (timeout && get_system_time() - start_time > timeout) if (timeout && get_system_time() - start_time > timeout)
{ {
mutex->waiters--; assert(mutex->waiters >= 0); mutex->waiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }
@ -104,7 +106,7 @@ s32 _sys_lwmutex_lock(u32 lwmutex_id, u64 timeout)
mutex->signaled--; mutex->signaled--;
mutex->waiters--; assert(mutex->waiters >= 0); mutex->waiters--;
return CELL_OK; return CELL_OK;
} }
@ -116,6 +118,7 @@ s32 _sys_lwmutex_trylock(u32 lwmutex_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lwmutex_t> mutex; std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -138,6 +141,7 @@ s32 _sys_lwmutex_unlock(u32 lwmutex_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lwmutex_t> mutex; std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex)) if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -149,7 +153,11 @@ s32 _sys_lwmutex_unlock(u32 lwmutex_id)
} }
mutex->signaled++; mutex->signaled++;
if (mutex->waiters)
{
mutex->cv.notify_one(); mutex->cv.notify_one();
}
return CELL_OK; return CELL_OK;
} }

View file

@ -76,7 +76,7 @@ struct lwmutex_t
// TODO: use sleep queue, possibly remove condition variable // TODO: use sleep queue, possibly remove condition variable
std::condition_variable cv; std::condition_variable cv;
std::atomic<s32> waiters; std::atomic<u32> waiters;
lwmutex_t(u32 protocol, u64 name) lwmutex_t(u32 protocol, u64 name)
: protocol(protocol) : protocol(protocol)

View file

@ -16,8 +16,6 @@ s32 sys_mutex_create(vm::ptr<u32> mutex_id, vm::ptr<sys_mutex_attribute_t> attr)
{ {
sys_mutex.Warning("sys_mutex_create(mutex_id=*0x%x, attr=*0x%x)", mutex_id, attr); sys_mutex.Warning("sys_mutex_create(mutex_id=*0x%x, attr=*0x%x)", mutex_id, attr);
LV2_LOCK;
if (!mutex_id || !attr) if (!mutex_id || !attr)
{ {
return CELL_EFAULT; return CELL_EFAULT;
@ -119,13 +117,13 @@ s32 sys_mutex_lock(PPUThread& CPU, u32 mutex_id, u64 timeout)
} }
// protocol is ignored in current implementation // protocol is ignored in current implementation
mutex->waiters++; assert(mutex->waiters > 0); mutex->waiters++;
while (!mutex->owner.expired()) while (!mutex->owner.expired())
{ {
if (timeout && get_system_time() - start_time > timeout) if (timeout && get_system_time() - start_time > timeout)
{ {
mutex->waiters--; assert(mutex->waiters >= 0); mutex->waiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }
@ -139,7 +137,7 @@ s32 sys_mutex_lock(PPUThread& CPU, u32 mutex_id, u64 timeout)
} }
mutex->owner = thread; mutex->owner = thread;
mutex->waiters--; assert(mutex->waiters >= 0); mutex->waiters--;
return CELL_OK; return CELL_OK;
} }
@ -218,8 +216,12 @@ s32 sys_mutex_unlock(PPUThread& CPU, u32 mutex_id)
else else
{ {
mutex->owner.reset(); mutex->owner.reset();
if (mutex->waiters)
{
mutex->cv.notify_one(); mutex->cv.notify_one();
} }
}
return CELL_OK; return CELL_OK;
} }

View file

@ -29,7 +29,7 @@ struct mutex_t
// TODO: use sleep queue, possibly remove condition variable // TODO: use sleep queue, possibly remove condition variable
std::condition_variable cv; std::condition_variable cv;
std::atomic<s32> waiters; std::atomic<u32> waiters;
mutex_t(bool recursive, u32 protocol, u64 name) mutex_t(bool recursive, u32 protocol, u64 name)
: recursive(recursive) : recursive(recursive)

View file

@ -50,12 +50,13 @@ s32 sys_rwlock_destroy(u32 rw_lock_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<rwlock_t> rwlock; std::shared_ptr<rwlock_t> rwlock;
if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock))
{ {
return CELL_ESRCH; 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; return CELL_EBUSY;
} }
@ -74,15 +75,20 @@ s32 sys_rwlock_rlock(u32 rw_lock_id, u64 timeout)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<rwlock_t> rwlock; std::shared_ptr<rwlock_t> rwlock;
if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock))
{ {
return CELL_ESRCH; 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) if (timeout && get_system_time() - start_time > timeout)
{ {
rwlock->rwaiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }
@ -92,10 +98,11 @@ s32 sys_rwlock_rlock(u32 rw_lock_id, u64 timeout)
return CELL_OK; 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->readers++;
rwlock->rwaiters--;
return CELL_OK; return CELL_OK;
} }
@ -107,12 +114,13 @@ s32 sys_rwlock_tryrlock(u32 rw_lock_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<rwlock_t> rwlock; std::shared_ptr<rwlock_t> rwlock;
if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock))
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
if (rwlock->writer || rwlock->waiters) if (rwlock->writer || rwlock->wwaiters)
{ {
return CELL_EBUSY; return CELL_EBUSY;
} }
@ -129,6 +137,7 @@ s32 sys_rwlock_runlock(u32 rw_lock_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<rwlock_t> rwlock; std::shared_ptr<rwlock_t> rwlock;
if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -139,9 +148,9 @@ s32 sys_rwlock_runlock(u32 rw_lock_id)
return CELL_EPERM; return CELL_EPERM;
} }
if (!--rwlock->readers) if (!--rwlock->readers && rwlock->wwaiters)
{ {
rwlock->cv.notify_one(); rwlock->wcv.notify_one();
} }
return CELL_OK; return CELL_OK;
@ -156,6 +165,7 @@ s32 sys_rwlock_wlock(PPUThread& CPU, u32 rw_lock_id, u64 timeout)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<rwlock_t> rwlock; std::shared_ptr<rwlock_t> rwlock;
if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock))
{ {
return CELL_ESRCH; 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 // protocol is ignored in current implementation
rwlock->waiters++; assert(rwlock->waiters > 0); rwlock->wwaiters++;
while (rwlock->readers || rwlock->writer) while (rwlock->readers || rwlock->writer)
{ {
if (timeout && get_system_time() - start_time > timeout) if (timeout && get_system_time() - start_time > timeout)
{ {
rwlock->waiters--; assert(rwlock->waiters >= 0); rwlock->wwaiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }
@ -183,11 +193,11 @@ s32 sys_rwlock_wlock(PPUThread& CPU, u32 rw_lock_id, u64 timeout)
return CELL_OK; 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->writer = CPU.GetId();
rwlock->waiters--; assert(rwlock->waiters >= 0); rwlock->wwaiters--;
return CELL_OK; return CELL_OK;
} }
@ -199,6 +209,7 @@ s32 sys_rwlock_trywlock(PPUThread& CPU, u32 rw_lock_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<rwlock_t> rwlock; std::shared_ptr<rwlock_t> rwlock;
if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -209,7 +220,7 @@ s32 sys_rwlock_trywlock(PPUThread& CPU, u32 rw_lock_id)
return CELL_EDEADLK; return CELL_EDEADLK;
} }
if (rwlock->readers || rwlock->writer || rwlock->waiters) if (rwlock->readers || rwlock->writer || rwlock->wwaiters)
{ {
return CELL_EBUSY; return CELL_EBUSY;
} }
@ -226,6 +237,7 @@ s32 sys_rwlock_wunlock(PPUThread& CPU, u32 rw_lock_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<rwlock_t> rwlock; std::shared_ptr<rwlock_t> rwlock;
if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock)) if (!Emu.GetIdManager().GetIDData(rw_lock_id, rwlock))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -237,7 +249,15 @@ s32 sys_rwlock_wunlock(PPUThread& CPU, u32 rw_lock_id)
} }
rwlock->writer = 0; 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; return CELL_OK;
} }

View file

@ -23,16 +23,19 @@ struct rwlock_t
std::atomic<u32> readers; // reader count std::atomic<u32> readers; // reader count
std::atomic<u32> writer; // writer id std::atomic<u32> writer; // writer id
// TODO: use sleep queue, possibly remove condition variable // TODO: use sleep queue, possibly remove condition variables
std::condition_variable cv; std::condition_variable rcv;
std::atomic<s32> waiters; std::condition_variable wcv;
std::atomic<u32> rwaiters;
std::atomic<u32> wwaiters;
rwlock_t(u32 protocol, u64 name) rwlock_t(u32 protocol, u64 name)
: protocol(protocol) : protocol(protocol)
, name(name) , name(name)
, readers(0) , readers(0)
, writer(0) , writer(0)
, waiters(0) , rwaiters(0)
, wwaiters(0)
{ {
} }
}; };

View file

@ -62,6 +62,7 @@ s32 sys_semaphore_destroy(u32 sem)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<semaphore_t> semaphore; std::shared_ptr<semaphore_t> semaphore;
if (!Emu.GetIdManager().GetIDData(sem, semaphore)) if (!Emu.GetIdManager().GetIDData(sem, semaphore))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -86,19 +87,20 @@ s32 sys_semaphore_wait(u32 sem, u64 timeout)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<semaphore_t> semaphore; std::shared_ptr<semaphore_t> semaphore;
if (!Emu.GetIdManager().GetIDData(sem, semaphore)) if (!Emu.GetIdManager().GetIDData(sem, semaphore))
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
// protocol is ignored in current implementation // protocol is ignored in current implementation
semaphore->waiters++; assert(semaphore->waiters > 0); semaphore->waiters++;
while (semaphore->value <= 0) while (semaphore->value <= 0)
{ {
if (timeout && get_system_time() - start_time > timeout) if (timeout && get_system_time() - start_time > timeout)
{ {
semaphore->waiters--; assert(semaphore->waiters >= 0); semaphore->waiters--;
return CELL_ETIMEDOUT; return CELL_ETIMEDOUT;
} }
@ -112,7 +114,7 @@ s32 sys_semaphore_wait(u32 sem, u64 timeout)
} }
semaphore->value--; semaphore->value--;
semaphore->waiters--; assert(semaphore->waiters >= 0); semaphore->waiters--;
return CELL_OK; return CELL_OK;
} }
@ -124,6 +126,7 @@ s32 sys_semaphore_trywait(u32 sem)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<semaphore_t> semaphore; std::shared_ptr<semaphore_t> semaphore;
if (!Emu.GetIdManager().GetIDData(sem, semaphore)) if (!Emu.GetIdManager().GetIDData(sem, semaphore))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -146,6 +149,7 @@ s32 sys_semaphore_post(u32 sem, s32 count)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<semaphore_t> semaphore; std::shared_ptr<semaphore_t> semaphore;
if (!Emu.GetIdManager().GetIDData(sem, semaphore)) if (!Emu.GetIdManager().GetIDData(sem, semaphore))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -156,13 +160,20 @@ s32 sys_semaphore_post(u32 sem, s32 count)
return CELL_EINVAL; 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; return CELL_EBUSY;
} }
semaphore->value += count; assert(semaphore->value >= 0); semaphore->value += count;
if (semaphore->waiters)
{
semaphore->cv.notify_all(); semaphore->cv.notify_all();
}
return CELL_OK; return CELL_OK;
} }
@ -179,6 +190,7 @@ s32 sys_semaphore_get_value(u32 sem, vm::ptr<s32> count)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<semaphore_t> semaphore; std::shared_ptr<semaphore_t> semaphore;
if (!Emu.GetIdManager().GetIDData(sem, semaphore)) if (!Emu.GetIdManager().GetIDData(sem, semaphore))
{ {
return CELL_ESRCH; return CELL_ESRCH;

View file

@ -25,7 +25,7 @@ struct semaphore_t
// TODO: use sleep queue, possibly remove condition variable // TODO: use sleep queue, possibly remove condition variable
std::condition_variable cv; std::condition_variable cv;
std::atomic<s32> waiters; std::atomic<u32> waiters;
semaphore_t(u32 protocol, s32 max, u64 name, s32 value) semaphore_t(u32 protocol, s32 max, u64 name, s32 value)
: protocol(protocol) : protocol(protocol)

View file

@ -315,11 +315,12 @@ s32 sys_spu_thread_group_start(u32 id)
spu.SetEntry(image->entry_point); spu.SetEntry(image->entry_point);
spu.Run(); spu.Run();
spu.status.write_relaxed(SPU_STATUS_RUNNING);
spu.GPR[3] = u128::from64(0, args.arg1); spu.GPR[3] = u128::from64(0, args.arg1);
spu.GPR[4] = u128::from64(0, args.arg2); spu.GPR[4] = u128::from64(0, args.arg2);
spu.GPR[5] = u128::from64(0, args.arg3); spu.GPR[5] = u128::from64(0, args.arg3);
spu.GPR[6] = u128::from64(0, args.arg4); 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<SPUThread&>(*t); auto& spu = static_cast<SPUThread&>(*t);
spu.status.write_relaxed(SPU_STATUS_STOPPED); spu.status.exchange(SPU_STATUS_STOPPED);
spu.FastStop(); spu.FastStop();
} }
} }

View file

@ -32,8 +32,7 @@ s32 sys_timer_create(vm::ptr<u32> timer_id)
if (queue) if (queue)
{ {
queue->events.emplace_back(timer->source, timer->data1, timer->data2, timer->start); queue->push(timer->source, timer->data1, timer->data2, timer->start);
queue->cv.notify_one();
} }
if (timer->period && queue) if (timer->period && queue)
@ -64,6 +63,7 @@ s32 sys_timer_destroy(u32 timer_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lv2_timer_t> timer; std::shared_ptr<lv2_timer_t> timer;
if (!Emu.GetIdManager().GetIDData(timer_id, timer)) if (!Emu.GetIdManager().GetIDData(timer_id, timer))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -86,6 +86,7 @@ s32 sys_timer_get_information(u32 timer_id, vm::ptr<sys_timer_information_t> inf
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lv2_timer_t> timer; std::shared_ptr<lv2_timer_t> timer;
if (!Emu.GetIdManager().GetIDData(timer_id, timer)) if (!Emu.GetIdManager().GetIDData(timer_id, timer))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -94,7 +95,6 @@ s32 sys_timer_get_information(u32 timer_id, vm::ptr<sys_timer_information_t> inf
info->next_expiration_time = timer->start; info->next_expiration_time = timer->start;
info->period = timer->period; info->period = timer->period;
info->timer_state = timer->state; info->timer_state = timer->state;
return CELL_OK; return CELL_OK;
@ -109,6 +109,7 @@ s32 _sys_timer_start(u32 timer_id, u64 base_time, u64 period)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lv2_timer_t> timer; std::shared_ptr<lv2_timer_t> timer;
if (!Emu.GetIdManager().GetIDData(timer_id, timer)) if (!Emu.GetIdManager().GetIDData(timer_id, timer))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -160,6 +161,7 @@ s32 sys_timer_stop(u32 timer_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lv2_timer_t> timer; std::shared_ptr<lv2_timer_t> timer;
if (!Emu.GetIdManager().GetIDData(timer_id, timer)) if (!Emu.GetIdManager().GetIDData(timer_id, timer))
{ {
return CELL_ESRCH; return CELL_ESRCH;
@ -204,6 +206,7 @@ s32 sys_timer_disconnect_event_queue(u32 timer_id)
LV2_LOCK; LV2_LOCK;
std::shared_ptr<lv2_timer_t> timer; std::shared_ptr<lv2_timer_t> timer;
if (!Emu.GetIdManager().GetIDData(timer_id, timer)) if (!Emu.GetIdManager().GetIDData(timer_id, timer))
{ {
return CELL_ESRCH; return CELL_ESRCH;

View file

@ -493,40 +493,59 @@ namespace loader
ppu_thr_stop_data[1] = BLR(); ppu_thr_stop_data[1] = BLR();
Emu.SetCPUThreadStop(ppu_thr_stop_data.addr()); Emu.SetCPUThreadStop(ppu_thr_stop_data.addr());
/* static const int branch_size = 8 * 4;
//TODO
static const int branch_size = 6 * 4;
auto make_branch = [](vm::ptr<u32>& ptr, u32 addr) auto make_branch = [](vm::ptr<u32>& ptr, u32 addr)
{ {
u32 stub = vm::read32(addr); u32 stub = vm::read32(addr);
u32 rtoc = vm::read32(addr + 4); u32 rtoc = vm::read32(addr + 4);
*ptr++ = implicts::LI(r0, stub >> 16); *ptr++ = LI_(r0, 0);
*ptr++ = ORIS(r0, r0, stub & 0xffff); *ptr++ = ORI(r0, r0, stub & 0xffff);
*ptr++ = implicts::LI(r2, rtoc >> 16); *ptr++ = ORIS(r0, r0, stub >> 16);
*ptr++ = ORIS(r2, r2, rtoc & 0xffff); *ptr++ = LI_(r2, 0);
*ptr++ = ORI(r2, r2, rtoc & 0xffff);
*ptr++ = ORIS(r2, r2, rtoc >> 16);
*ptr++ = MTCTR(r0); *ptr++ = MTCTR(r0);
*ptr++ = BCTRL(); *ptr++ = BCTRL();
}; };
auto entry = vm::ptr<u32>::make(vm::alloc(branch_size * (start_funcs.size() + 1), vm::main)); auto entry = vm::ptr<u32>::make(vm::alloc(56 + branch_size * (start_funcs.size() + 1), vm::main));
auto OPD = vm::ptr<u32>::make(vm::alloc(2 * 4)); const auto OPD = entry;
OPD[0] = entry.addr();
OPD[1] = 0; // 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) for (auto &f : start_funcs)
{ {
make_branch(entry, f); 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.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; return ok;
} }

View file

@ -39,6 +39,8 @@
<ClCompile Include="..\Utilities\Thread.cpp" /> <ClCompile Include="..\Utilities\Thread.cpp" />
<ClCompile Include="Emu\RSX\CgBinaryFragmentProgram.cpp" /> <ClCompile Include="Emu\RSX\CgBinaryFragmentProgram.cpp" />
<ClCompile Include="Emu\RSX\CgBinaryVertexProgram.cpp" /> <ClCompile Include="Emu\RSX\CgBinaryVertexProgram.cpp" />
<ClCompile Include="Emu\SysCalls\lv2\sys_fs.cpp" />
<ClCompile Include="Emu\SysCalls\Modules\cellFs.cpp" />
<ClCompile Include="Emu\SysCalls\Modules\cellSpursSpu.cpp" /> <ClCompile Include="Emu\SysCalls\Modules\cellSpursSpu.cpp" />
<ClCompile Include="Crypto\aes.cpp" /> <ClCompile Include="Crypto\aes.cpp" />
<ClCompile Include="Crypto\ec.cpp" /> <ClCompile Include="Crypto\ec.cpp" />
@ -177,7 +179,6 @@
<ClCompile Include="Emu\SysCalls\Callback.cpp" /> <ClCompile Include="Emu\SysCalls\Callback.cpp" />
<ClCompile Include="Emu\SysCalls\FuncList.cpp" /> <ClCompile Include="Emu\SysCalls\FuncList.cpp" />
<ClCompile Include="Emu\SysCalls\LogBase.cpp" /> <ClCompile Include="Emu\SysCalls\LogBase.cpp" />
<ClCompile Include="Emu\SysCalls\lv2\cellFs.cpp" />
<ClCompile Include="Emu\SysCalls\lv2\sleep_queue.cpp" /> <ClCompile Include="Emu\SysCalls\lv2\sleep_queue.cpp" />
<ClCompile Include="Emu\SysCalls\lv2\sys_cond.cpp" /> <ClCompile Include="Emu\SysCalls\lv2\sys_cond.cpp" />
<ClCompile Include="Emu\SysCalls\lv2\sys_event.cpp" /> <ClCompile Include="Emu\SysCalls\lv2\sys_event.cpp" />
@ -439,11 +440,11 @@
<ClInclude Include="Emu\SysCalls\CB_FUNC.h" /> <ClInclude Include="Emu\SysCalls\CB_FUNC.h" />
<ClInclude Include="Emu\SysCalls\ErrorCodes.h" /> <ClInclude Include="Emu\SysCalls\ErrorCodes.h" />
<ClInclude Include="Emu\SysCalls\LogBase.h" /> <ClInclude Include="Emu\SysCalls\LogBase.h" />
<ClInclude Include="Emu\SysCalls\lv2\cellFs.h" />
<ClInclude Include="Emu\SysCalls\lv2\sleep_queue.h" /> <ClInclude Include="Emu\SysCalls\lv2\sleep_queue.h" />
<ClInclude Include="Emu\SysCalls\lv2\sys_cond.h" /> <ClInclude Include="Emu\SysCalls\lv2\sys_cond.h" />
<ClInclude Include="Emu\SysCalls\lv2\sys_event.h" /> <ClInclude Include="Emu\SysCalls\lv2\sys_event.h" />
<ClInclude Include="Emu\SysCalls\lv2\sys_event_flag.h" /> <ClInclude Include="Emu\SysCalls\lv2\sys_event_flag.h" />
<ClInclude Include="Emu\SysCalls\lv2\sys_fs.h" />
<ClInclude Include="Emu\SysCalls\lv2\sys_interrupt.h" /> <ClInclude Include="Emu\SysCalls\lv2\sys_interrupt.h" />
<ClInclude Include="Emu\SysCalls\lv2\sys_lwcond.h" /> <ClInclude Include="Emu\SysCalls\lv2\sys_lwcond.h" />
<ClInclude Include="Emu\SysCalls\lv2\sys_lwmutex.h" /> <ClInclude Include="Emu\SysCalls\lv2\sys_lwmutex.h" />
@ -472,6 +473,7 @@
<ClInclude Include="Emu\SysCalls\Modules\cellFiber.h" /> <ClInclude Include="Emu\SysCalls\Modules\cellFiber.h" />
<ClInclude Include="Emu\SysCalls\Modules\cellFont.h" /> <ClInclude Include="Emu\SysCalls\Modules\cellFont.h" />
<ClInclude Include="Emu\SysCalls\Modules\cellFontFT.h" /> <ClInclude Include="Emu\SysCalls\Modules\cellFontFT.h" />
<ClInclude Include="Emu\SysCalls\Modules\cellFs.h" />
<ClInclude Include="Emu\SysCalls\Modules\cellGame.h" /> <ClInclude Include="Emu\SysCalls\Modules\cellGame.h" />
<ClInclude Include="Emu\SysCalls\Modules\cellGcmSys.h" /> <ClInclude Include="Emu\SysCalls\Modules\cellGcmSys.h" />
<ClInclude Include="Emu\SysCalls\Modules\cellGem.h" /> <ClInclude Include="Emu\SysCalls\Modules\cellGem.h" />

View file

@ -650,9 +650,6 @@
<ClCompile Include="Emu\SysCalls\SyncPrimitivesManager.cpp"> <ClCompile Include="Emu\SysCalls\SyncPrimitivesManager.cpp">
<Filter>Emu\SysCalls</Filter> <Filter>Emu\SysCalls</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Emu\SysCalls\lv2\cellFs.cpp">
<Filter>Emu\SysCalls\lv2</Filter>
</ClCompile>
<ClCompile Include="..\Utilities\Thread.cpp"> <ClCompile Include="..\Utilities\Thread.cpp">
<Filter>Utilities</Filter> <Filter>Utilities</Filter>
</ClCompile> </ClCompile>
@ -857,6 +854,12 @@
<ClCompile Include="Emu\SysCalls\lv2\sleep_queue.cpp"> <ClCompile Include="Emu\SysCalls\lv2\sleep_queue.cpp">
<Filter>Emu\SysCalls\lv2</Filter> <Filter>Emu\SysCalls\lv2</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Emu\SysCalls\lv2\sys_fs.cpp">
<Filter>Emu\SysCalls\lv2</Filter>
</ClCompile>
<ClCompile Include="Emu\SysCalls\Modules\cellFs.cpp">
<Filter>Emu\SysCalls\Modules</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Crypto\aes.h"> <ClInclude Include="Crypto\aes.h">
@ -1450,9 +1453,6 @@
<ClInclude Include="Emu\ARMv7\PSVFuncList.h"> <ClInclude Include="Emu\ARMv7\PSVFuncList.h">
<Filter>Emu\CPU\ARMv7</Filter> <Filter>Emu\CPU\ARMv7</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Emu\SysCalls\lv2\cellFs.h">
<Filter>Emu\SysCalls\lv2</Filter>
</ClInclude>
<ClInclude Include="..\Utilities\Thread.h"> <ClInclude Include="..\Utilities\Thread.h">
<Filter>Utilities</Filter> <Filter>Utilities</Filter>
</ClInclude> </ClInclude>
@ -1537,5 +1537,11 @@
<ClInclude Include="Emu\Memory\refcnt.h"> <ClInclude Include="Emu\Memory\refcnt.h">
<Filter>Emu\Memory</Filter> <Filter>Emu\Memory</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Emu\SysCalls\lv2\sys_fs.h">
<Filter>Emu\SysCalls\lv2</Filter>
</ClInclude>
<ClInclude Include="Emu\SysCalls\Modules\cellFs.h">
<Filter>Emu\SysCalls\Modules</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>