rpcsx/rpcs3/Emu/Cell/lv2/sys_event_flag.cpp

326 lines
6.4 KiB
C++
Raw Normal View History

2014-08-08 17:55:12 +02:00
#include "stdafx.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2015-03-06 23:58:42 +01:00
#include "Emu/IdManager.h"
2014-08-08 17:55:12 +02:00
2016-04-14 00:23:53 +02:00
#include "Emu/Cell/ErrorCodes.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
2014-08-08 17:55:12 +02:00
#include "sys_event_flag.h"
2016-05-13 15:55:34 +02:00
#include <algorithm>
logs::channel sys_event_flag("sys_event_flag", logs::level::notice);
2014-08-08 17:55:12 +02:00
extern u64 get_system_time();
2016-04-14 00:23:53 +02:00
void lv2_event_flag_t::notify_all(lv2_lock_t)
2015-07-19 23:29:40 +02:00
{
2016-04-14 00:23:53 +02:00
auto pred = [this](cpu_thread* thread) -> bool
2015-07-19 23:29:40 +02:00
{
auto& ppu = static_cast<ppu_thread&>(*thread);
2015-07-19 23:29:40 +02:00
// load pattern and mode from registers
const u64 bitptn = ppu.gpr[4];
const u32 mode = static_cast<u32>(ppu.gpr[5]);
2015-07-19 23:29:40 +02:00
// check specific pattern
if (check_pattern(bitptn, mode))
{
// save pattern
ppu.gpr[4] = clear_pattern(bitptn, mode);
2015-07-19 23:29:40 +02:00
2016-08-15 02:11:49 +02:00
thread->set_signal();
2015-07-19 23:29:40 +02:00
return true;
}
return false;
};
2015-07-20 18:46:24 +02:00
// check all waiters; protocol is ignored in current implementation
2015-07-19 23:29:40 +02:00
sq.erase(std::remove_if(sq.begin(), sq.end(), pred), sq.end());
}
2015-07-09 17:30:37 +02:00
s32 sys_event_flag_create(vm::ptr<u32> id, vm::ptr<sys_event_flag_attribute_t> attr, u64 init)
2014-08-23 02:16:54 +02:00
{
sys_event_flag.warning("sys_event_flag_create(id=*0x%x, attr=*0x%x, init=0x%llx)", id, attr, init);
2014-08-23 02:16:54 +02:00
2015-03-05 22:29:05 +01:00
if (!id || !attr)
{
return CELL_EFAULT;
}
2015-03-05 22:29:05 +01:00
const u32 protocol = attr->protocol;
2015-07-19 23:29:40 +02:00
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_RETRY && protocol != SYS_SYNC_PRIORITY && protocol != SYS_SYNC_PRIORITY_INHERIT)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.error("sys_event_flag_create(): unknown protocol (0x%x)", protocol);
2015-07-19 23:29:40 +02:00
return CELL_EINVAL;
2014-08-08 17:55:12 +02:00
}
2015-09-15 18:23:17 +02:00
if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key || attr->flags)
{
sys_event_flag.error("sys_event_flag_create(): unknown attributes (pshared=0x%x, ipc_key=0x%llx, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags);
2014-08-08 17:55:12 +02:00
return CELL_EINVAL;
}
2014-08-08 17:55:12 +02:00
2015-03-05 22:29:05 +01:00
const u32 type = attr->type;
2015-07-19 23:29:40 +02:00
if (type != SYS_SYNC_WAITER_SINGLE && type != SYS_SYNC_WAITER_MULTIPLE)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.error("sys_event_flag_create(): unknown type (0x%x)", type);
2015-07-19 23:29:40 +02:00
return CELL_EINVAL;
2014-08-08 17:55:12 +02:00
}
*id = idm::make<lv2_event_flag_t>(init, protocol, type, attr->name_u64);
2014-08-08 17:55:12 +02:00
return CELL_OK;
}
2015-03-05 22:29:05 +01:00
s32 sys_event_flag_destroy(u32 id)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.warning("sys_event_flag_destroy(id=0x%x)", id);
2015-03-05 22:29:05 +01:00
LV2_LOCK;
2014-08-08 17:55:12 +02:00
const auto eflag = idm::get<lv2_event_flag_t>(id);
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (!eflag)
2015-03-05 22:29:05 +01:00
{
return CELL_ESRCH;
}
2015-07-19 23:29:40 +02:00
if (!eflag->sq.empty())
2014-08-08 17:55:12 +02:00
{
return CELL_EBUSY;
}
idm::remove<lv2_event_flag_t>(id);
2014-08-08 17:55:12 +02:00
return CELL_OK;
}
s32 sys_event_flag_wait(ppu_thread& ppu, u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result, u64 timeout)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.trace("sys_event_flag_wait(id=0x%x, bitptn=0x%llx, mode=0x%x, result=*0x%x, timeout=0x%llx)", id, bitptn, mode, result, timeout);
2015-03-05 22:29:05 +01:00
const u64 start_time = get_system_time();
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
// If this syscall is called through the SC instruction, these registers must already contain corresponding values.
// But let's fixup them (in the case of explicit function call or something) because these values are used externally.
ppu.gpr[4] = bitptn;
ppu.gpr[5] = mode;
2015-03-05 22:29:05 +01:00
2015-07-19 23:29:40 +02:00
LV2_LOCK;
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (result) *result = 0; // This is very annoying.
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (!lv2_event_flag_t::check_mode(mode))
2014-08-08 17:55:12 +02:00
{
sys_event_flag.error("sys_event_flag_wait(): unknown mode (0x%x)", mode);
2015-07-19 23:29:40 +02:00
return CELL_EINVAL;
2014-08-08 17:55:12 +02:00
}
const auto eflag = idm::get<lv2_event_flag_t>(id);
2015-03-05 22:29:05 +01:00
2015-07-19 23:29:40 +02:00
if (!eflag)
2015-03-05 22:29:05 +01:00
{
return CELL_ESRCH;
}
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (eflag->type == SYS_SYNC_WAITER_SINGLE && eflag->sq.size() > 0)
2015-03-05 22:29:05 +01:00
{
return CELL_EPERM;
}
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (eflag->check_pattern(bitptn, mode))
2014-08-08 17:55:12 +02:00
{
2015-07-19 23:29:40 +02:00
const u64 pattern = eflag->clear_pattern(bitptn, mode);
2014-12-23 00:31:11 +01:00
2015-07-19 23:29:40 +02:00
if (result) *result = pattern;
2014-12-23 00:31:11 +01:00
2015-07-19 23:29:40 +02:00
return CELL_OK;
}
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
// add waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> waiter(eflag->sq, ppu);
2014-08-08 17:55:12 +02:00
while (!ppu.state.test_and_reset(cpu_flag::signal))
2015-07-19 23:29:40 +02:00
{
2015-07-04 01:22:24 +02:00
CHECK_EMU_STATUS;
2015-07-19 23:29:40 +02:00
if (timeout)
2015-03-05 22:29:05 +01:00
{
2015-07-19 23:29:40 +02:00
const u64 passed = get_system_time() - start_time;
if (passed >= timeout)
2015-03-06 23:10:04 +01:00
{
2015-07-19 23:29:40 +02:00
if (result) *result = eflag->pattern;
return CELL_ETIMEDOUT;
2015-03-06 23:10:04 +01:00
}
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
2014-08-08 17:55:12 +02:00
}
2015-07-19 23:29:40 +02:00
else
2014-08-08 17:55:12 +02:00
{
get_current_thread_cv().wait(lv2_lock);
2014-08-08 17:55:12 +02:00
}
2015-03-05 22:29:05 +01:00
}
2015-07-19 23:29:40 +02:00
// load pattern saved upon signaling
if (result)
2015-03-05 22:29:05 +01:00
{
*result = ppu.gpr[4];
2015-03-05 22:29:05 +01:00
}
2015-07-19 23:29:40 +02:00
// check cause
if (ppu.gpr[5] == 0)
2015-03-05 22:29:05 +01:00
{
2015-07-19 23:29:40 +02:00
return CELL_ECANCELED;
2014-08-08 17:55:12 +02:00
}
2015-03-05 22:29:05 +01:00
return CELL_OK;
2014-08-08 17:55:12 +02:00
}
2015-03-05 22:29:05 +01:00
s32 sys_event_flag_trywait(u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.trace("sys_event_flag_trywait(id=0x%x, bitptn=0x%llx, mode=0x%x, result=*0x%x)", id, bitptn, mode, result);
2014-08-08 17:55:12 +02:00
2015-03-05 22:29:05 +01:00
LV2_LOCK;
2015-07-19 23:29:40 +02:00
if (result) *result = 0; // This is very annoying.
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (!lv2_event_flag_t::check_mode(mode))
2014-08-08 17:55:12 +02:00
{
sys_event_flag.error("sys_event_flag_trywait(): unknown mode (0x%x)", mode);
2015-07-19 23:29:40 +02:00
return CELL_EINVAL;
2014-08-08 17:55:12 +02:00
}
const auto eflag = idm::get<lv2_event_flag_t>(id);
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (!eflag)
2015-03-05 22:29:05 +01:00
{
return CELL_ESRCH;
}
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (eflag->check_pattern(bitptn, mode))
2015-03-05 22:29:05 +01:00
{
2015-07-19 23:29:40 +02:00
const u64 pattern = eflag->clear_pattern(bitptn, mode);
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (result) *result = pattern;
2015-03-05 22:29:05 +01:00
2015-07-19 23:29:40 +02:00
return CELL_OK;
2014-08-08 17:55:12 +02:00
}
2015-07-19 23:29:40 +02:00
return CELL_EBUSY;
2014-08-08 17:55:12 +02:00
}
2015-03-05 22:29:05 +01:00
s32 sys_event_flag_set(u32 id, u64 bitptn)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.trace("sys_event_flag_set(id=0x%x, bitptn=0x%llx)", id, bitptn);
2014-08-08 17:55:12 +02:00
2015-03-05 22:29:05 +01:00
LV2_LOCK;
2014-08-08 17:55:12 +02:00
const auto eflag = idm::get<lv2_event_flag_t>(id);
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
if (!eflag)
2014-08-08 17:55:12 +02:00
{
2015-03-05 22:29:05 +01:00
return CELL_ESRCH;
2014-08-08 17:55:12 +02:00
}
2015-03-05 22:29:05 +01:00
2015-07-19 23:29:40 +02:00
if (bitptn && ~eflag->pattern.fetch_or(bitptn) & bitptn)
2015-03-05 22:29:05 +01:00
{
2015-07-19 23:29:40 +02:00
eflag->notify_all(lv2_lock);
2015-03-11 16:30:50 +01:00
}
2015-03-05 22:29:05 +01:00
2014-08-08 17:55:12 +02:00
return CELL_OK;
}
2015-03-05 22:29:05 +01:00
s32 sys_event_flag_clear(u32 id, u64 bitptn)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.trace("sys_event_flag_clear(id=0x%x, bitptn=0x%llx)", id, bitptn);
2015-03-05 22:29:05 +01:00
LV2_LOCK;
2014-08-08 17:55:12 +02:00
const auto eflag = idm::get<lv2_event_flag_t>(id);
2015-03-05 22:29:05 +01:00
2015-07-19 23:29:40 +02:00
if (!eflag)
2015-03-05 22:29:05 +01:00
{
return CELL_ESRCH;
}
2015-07-19 23:29:40 +02:00
eflag->pattern &= bitptn;
2015-03-05 22:29:05 +01:00
2014-08-08 17:55:12 +02:00
return CELL_OK;
}
2015-03-05 22:29:05 +01:00
s32 sys_event_flag_cancel(u32 id, vm::ptr<u32> num)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.trace("sys_event_flag_cancel(id=0x%x, num=*0x%x)", id, num);
2014-08-08 17:55:12 +02:00
2015-03-05 22:29:05 +01:00
LV2_LOCK;
2014-08-08 17:55:12 +02:00
2015-03-05 22:29:05 +01:00
if (num)
2014-08-08 17:55:12 +02:00
{
2015-03-05 22:29:05 +01:00
*num = 0;
2014-08-08 17:55:12 +02:00
}
const auto eflag = idm::get<lv2_event_flag_t>(id);
2015-03-05 22:29:05 +01:00
2015-07-19 23:29:40 +02:00
if (!eflag)
2014-08-08 17:55:12 +02:00
{
2015-03-05 22:29:05 +01:00
return CELL_ESRCH;
2014-08-08 17:55:12 +02:00
}
2014-12-23 00:31:11 +01:00
if (num)
{
2015-07-19 23:29:40 +02:00
*num = static_cast<u32>(eflag->sq.size());
2014-12-23 00:31:11 +01:00
}
2014-08-08 17:55:12 +02:00
2015-07-19 23:29:40 +02:00
const u64 pattern = eflag->pattern;
// signal all threads to return CELL_ECANCELED
for (auto& thread : eflag->sq)
2015-03-11 16:30:50 +01:00
{
auto& ppu = static_cast<ppu_thread&>(*thread);
2015-07-19 23:29:40 +02:00
// save existing pattern
ppu.gpr[4] = pattern;
2015-07-19 23:29:40 +02:00
// clear "mode" as a sign of cancellation
ppu.gpr[5] = 0;
2015-07-19 23:29:40 +02:00
2016-08-15 02:11:49 +02:00
thread->set_signal();
2015-03-11 16:30:50 +01:00
}
2015-07-19 23:29:40 +02:00
eflag->sq.clear();
2015-03-11 16:30:50 +01:00
2014-08-08 17:55:12 +02:00
return CELL_OK;
}
2015-03-05 22:29:05 +01:00
s32 sys_event_flag_get(u32 id, vm::ptr<u64> flags)
2014-08-08 17:55:12 +02:00
{
sys_event_flag.trace("sys_event_flag_get(id=0x%x, flags=*0x%x)", id, flags);
2015-03-05 22:29:05 +01:00
LV2_LOCK;
2014-08-08 17:55:12 +02:00
if (!flags)
{
return CELL_EFAULT;
}
const auto eflag = idm::get<lv2_event_flag_t>(id);
2015-03-05 22:29:05 +01:00
2015-07-19 23:29:40 +02:00
if (!eflag)
2015-03-05 22:29:05 +01:00
{
2015-07-19 23:29:40 +02:00
*flags = 0; // This is very annoying.
2015-03-05 22:29:05 +01:00
return CELL_ESRCH;
}
2015-07-19 23:29:40 +02:00
*flags = eflag->pattern;
2014-08-08 17:55:12 +02:00
return CELL_OK;
2015-03-05 22:29:05 +01:00
}