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

443 lines
9 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2015-03-06 23:58:42 +01:00
#include "Emu/IdManager.h"
2016-05-13 15:55:34 +02:00
#include "Emu/IPC.h"
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"
#include "Emu/Cell/SPUThread.h"
2014-08-23 16:51:51 +02:00
#include "sys_process.h"
#include "sys_event.h"
2016-05-13 15:55:34 +02:00
logs::channel sys_event("sys_event", logs::level::notice);
2016-05-13 15:55:34 +02:00
template<> DECLARE(ipc_manager<lv2_event_queue_t, u64>::g_ipc) {};
extern u64 get_system_time();
2015-07-01 19:09:26 +02:00
2016-04-14 00:23:53 +02:00
std::shared_ptr<lv2_event_queue_t> lv2_event_queue_t::make(u32 protocol, s32 type, u64 name, u64 ipc_key, s32 size)
2015-07-19 14:58:11 +02:00
{
2017-01-25 18:50:30 +01:00
std::shared_ptr<lv2_event_queue_t> result;
2016-05-13 15:55:34 +02:00
2017-01-25 18:50:30 +01:00
auto make_expr = [&]() -> const std::shared_ptr<lv2_event_queue_t>&
{
result = idm::make_ptr<lv2_event_queue_t>(protocol, type, name, ipc_key, size);
return result;
};
2015-07-19 14:58:11 +02:00
2016-04-14 00:23:53 +02:00
if (ipc_key == SYS_EVENT_QUEUE_LOCAL)
{
// Not an IPC queue
2017-01-25 18:50:30 +01:00
make_expr();
return result;
2016-04-14 00:23:53 +02:00
}
// IPC queue
2016-05-13 15:55:34 +02:00
if (ipc_manager<lv2_event_queue_t, u64>::add(ipc_key, make_expr))
{
2017-01-25 18:50:30 +01:00
return result;
2016-05-13 15:55:34 +02:00
}
return nullptr;
2016-04-14 00:23:53 +02:00
}
std::shared_ptr<lv2_event_queue_t> lv2_event_queue_t::find(u64 ipc_key)
{
if (ipc_key == SYS_EVENT_QUEUE_LOCAL)
{
2016-04-14 00:23:53 +02:00
// Invalid IPC key
return{};
}
2016-05-13 15:55:34 +02:00
return ipc_manager<lv2_event_queue_t, u64>::get(ipc_key);
2016-04-14 00:23:53 +02:00
}
2017-01-25 18:50:30 +01:00
lv2_event_queue_t::lv2_event_queue_t(u32 protocol, s32 type, u64 name, u64 ipc_key, s32 size)
: protocol(protocol)
, type(type)
, name(name)
, ipc_key(ipc_key)
, size(size)
, id(idm::last_id())
{
}
2016-04-14 00:23:53 +02:00
void lv2_event_queue_t::push(lv2_lock_t, u64 source, u64 data1, u64 data2, u64 data3)
{
2016-08-15 15:29:38 +02:00
verify(HERE), m_sq.empty() || m_events.empty();
2016-04-14 00:23:53 +02:00
// save event if no waiters
if (m_sq.empty())
{
2016-04-14 00:23:53 +02:00
return m_events.emplace_back(source, data1, data2, data3);
}
2015-07-19 14:58:11 +02:00
// notify waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
auto& thread = m_sq.front();
2017-01-25 18:50:30 +01:00
if (type == SYS_PPU_QUEUE && thread->id_type() == 1)
2015-07-19 14:58:11 +02:00
{
// store event data in registers
auto& ppu = static_cast<ppu_thread&>(*thread);
ppu.gpr[4] = source;
ppu.gpr[5] = data1;
ppu.gpr[6] = data2;
ppu.gpr[7] = data3;
}
2017-01-25 18:50:30 +01:00
else if (type == SYS_SPU_QUEUE && thread->id_type() != 1)
{
// store event data in In_MBox
auto& spu = static_cast<SPUThread&>(*thread);
spu.ch_in_mbox.set_values(4, CELL_OK, static_cast<u32>(data1), static_cast<u32>(data2), static_cast<u32>(data3));
}
else
{
fmt::throw_exception("Unexpected (queue type=%d, tid=%s)" HERE, type, thread->id);
}
2016-08-15 02:11:49 +02:00
thread->set_signal();
2016-04-14 00:23:53 +02:00
return m_sq.pop_front();
}
2016-04-14 00:23:53 +02:00
lv2_event_queue_t::event_type lv2_event_queue_t::pop(lv2_lock_t)
{
2016-08-15 15:29:38 +02:00
verify(HERE), m_events.size();
2016-04-14 00:23:53 +02:00
auto result = m_events.front();
m_events.pop_front();
return result;
2015-07-19 14:58:11 +02:00
}
2015-07-09 17:30:37 +02:00
s32 sys_event_queue_create(vm::ptr<u32> equeue_id, vm::ptr<sys_event_queue_attribute_t> attr, u64 event_queue_key, s32 size)
{
sys_event.warning("sys_event_queue_create(equeue_id=*0x%x, attr=*0x%x, event_queue_key=0x%llx, size=%d)", equeue_id, attr, event_queue_key, size);
2015-03-04 05:42:04 +01:00
if (size <= 0 || size > 127)
{
return CELL_EINVAL;
}
2015-03-04 05:42:04 +01:00
const u32 protocol = attr->protocol;
2015-07-19 14:58:11 +02:00
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_PRIORITY)
{
sys_event.error("sys_event_queue_create(): unknown protocol (0x%x)", protocol);
2015-07-19 14:58:11 +02:00
return CELL_EINVAL;
}
2015-03-04 05:42:04 +01:00
const u32 type = attr->type;
2015-07-19 14:58:11 +02:00
if (type != SYS_PPU_QUEUE && type != SYS_SPU_QUEUE)
{
sys_event.error("sys_event_queue_create(): unknown type (0x%x)", type);
2015-07-19 14:58:11 +02:00
return CELL_EINVAL;
}
2016-04-14 00:23:53 +02:00
const auto queue = lv2_event_queue_t::make(protocol, type, reinterpret_cast<u64&>(attr->name), event_queue_key, size);
2015-03-04 05:42:04 +01:00
if (!queue)
{
2015-03-11 16:30:50 +01:00
return CELL_EEXIST;
}
2015-07-01 19:09:26 +02:00
*equeue_id = queue->id;
2015-03-05 22:29:05 +01:00
2015-03-04 05:42:04 +01:00
return CELL_OK;
}
2015-03-04 05:42:04 +01:00
s32 sys_event_queue_destroy(u32 equeue_id, s32 mode)
{
sys_event.warning("sys_event_queue_destroy(equeue_id=0x%x, mode=%d)", equeue_id, mode);
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto queue = idm::get<lv2_event_queue_t>(equeue_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
if (mode && mode != SYS_EVENT_QUEUE_DESTROY_FORCE)
{
return CELL_EINVAL;
}
2016-04-14 00:23:53 +02:00
if (!mode && queue->waiters())
2015-03-04 05:42:04 +01:00
{
return CELL_EBUSY;
}
2015-03-11 16:30:50 +01:00
2015-07-19 14:58:11 +02:00
// cleanup
idm::remove<lv2_event_queue_t>(equeue_id);
2015-03-11 16:30:50 +01:00
2015-07-19 14:58:11 +02:00
// signal all threads to return CELL_ECANCELED
2016-04-14 00:23:53 +02:00
for (auto& thread : queue->thread_queue(lv2_lock))
2015-03-04 05:42:04 +01:00
{
2017-01-25 18:50:30 +01:00
if (queue->type == SYS_PPU_QUEUE && thread->id_type() == 1)
{
static_cast<ppu_thread&>(*thread).gpr[3] = 1;
}
2017-01-25 18:50:30 +01:00
else if (queue->type == SYS_SPU_QUEUE && thread->id_type() != 1)
{
static_cast<SPUThread&>(*thread).ch_in_mbox.set_values(1, CELL_ECANCELED);
}
else
{
fmt::throw_exception("Unexpected (queue type=%d, tid=%s)" HERE, queue->type, thread->id);
}
thread->state += cpu_flag::signal;
thread->notify();
2015-03-04 05:42:04 +01:00
}
return CELL_OK;
}
s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr<sys_event_t> event_array, s32 size, vm::ptr<u32> number)
{
sys_event.trace("sys_event_queue_tryreceive(equeue_id=0x%x, event_array=*0x%x, size=%d, number=*0x%x)", equeue_id, event_array, size, number);
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto queue = idm::get<lv2_event_queue_t>(equeue_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
if (size < 0)
{
fmt::throw_exception("Negative size (%d)" HERE, size);
}
if (queue->type != SYS_PPU_QUEUE)
{
return CELL_EINVAL;
}
2015-03-04 05:42:04 +01:00
s32 count = 0;
2016-04-14 00:23:53 +02:00
while (queue->waiters() == 0 && count < size && queue->events())
{
2015-06-21 02:17:42 +02:00
auto& dest = event_array[count++];
2016-04-14 00:23:53 +02:00
std::tie(dest.source, dest.data1, dest.data2, dest.data3) = queue->pop(lv2_lock);
}
*number = count;
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
s32 sys_event_queue_receive(ppu_thread& ppu, u32 equeue_id, vm::ptr<sys_event_t> dummy_event, u64 timeout)
{
sys_event.trace("sys_event_queue_receive(equeue_id=0x%x, *0x%x, timeout=0x%llx)", equeue_id, dummy_event, timeout);
const u64 start_time = get_system_time();
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto queue = idm::get<lv2_event_queue_t>(equeue_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
2015-03-04 05:42:04 +01:00
if (queue->type != SYS_PPU_QUEUE)
{
return CELL_EINVAL;
}
2016-04-14 00:23:53 +02:00
if (queue->events())
{
// event data is returned in registers (dummy_event is not used)
std::tie(ppu.gpr[4], ppu.gpr[5], ppu.gpr[6], ppu.gpr[7]) = queue->pop(lv2_lock);
return CELL_OK;
}
2015-07-19 14:58:11 +02:00
// cause (if cancelled) will be returned in r3
ppu.gpr[3] = 0;
2015-07-19 14:58:11 +02:00
// add waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> waiter(queue->thread_queue(lv2_lock), ppu);
while (!ppu.state.test_and_reset(cpu_flag::signal))
{
CHECK_EMU_STATUS;
if (timeout)
{
const u64 passed = get_system_time() - start_time;
if (passed >= timeout)
2015-07-19 14:58:11 +02:00
{
return CELL_ETIMEDOUT;
2015-07-19 14:58:11 +02:00
}
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
}
else
{
get_current_thread_cv().wait(lv2_lock);
2014-12-23 00:31:11 +01:00
}
2015-07-19 14:58:11 +02:00
}
2014-12-23 00:31:11 +01:00
if (ppu.gpr[3])
2015-07-19 14:58:11 +02:00
{
return CELL_ECANCELED;
}
2015-03-04 05:42:04 +01:00
// r4-r7 registers must be set by push()
2015-03-04 05:42:04 +01:00
return CELL_OK;
}
s32 sys_event_queue_drain(u32 equeue_id)
{
sys_event.trace("sys_event_queue_drain(equeue_id=0x%x)", equeue_id);
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto queue = idm::get<lv2_event_queue_t>(equeue_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
2016-04-14 00:23:53 +02:00
queue->clear(lv2_lock);
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
s32 sys_event_port_create(vm::ptr<u32> eport_id, s32 port_type, u64 name)
{
sys_event.warning("sys_event_port_create(eport_id=*0x%x, port_type=%d, name=0x%llx)", eport_id, port_type, name);
if (port_type != SYS_EVENT_PORT_LOCAL)
{
sys_event.error("sys_event_port_create(): unknown port type (%d)", port_type);
return CELL_EINVAL;
}
*eport_id = idm::make<lv2_event_port_t>(port_type, name);
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
s32 sys_event_port_destroy(u32 eport_id)
{
sys_event.warning("sys_event_port_destroy(eport_id=0x%x)", eport_id);
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto port = idm::get<lv2_event_port_t>(eport_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!port)
{
2015-03-04 05:42:04 +01:00
return CELL_ESRCH;
}
2015-03-04 05:42:04 +01:00
if (!port->queue.expired())
{
return CELL_EISCONN;
}
idm::remove<lv2_event_port_t>(eport_id);
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
s32 sys_event_port_connect_local(u32 eport_id, u32 equeue_id)
{
sys_event.warning("sys_event_port_connect_local(eport_id=0x%x, equeue_id=0x%x)", eport_id, equeue_id);
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto port = idm::get<lv2_event_port_t>(eport_id);
const auto queue = idm::get<lv2_event_queue_t>(equeue_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!port || !queue)
{
return CELL_ESRCH;
}
2015-03-04 22:51:14 +01:00
if (port->type != SYS_EVENT_PORT_LOCAL)
{
return CELL_EINVAL;
}
2015-03-04 05:42:04 +01:00
if (!port->queue.expired())
{
return CELL_EISCONN;
}
2015-03-04 05:42:04 +01:00
port->queue = queue;
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
s32 sys_event_port_disconnect(u32 eport_id)
{
sys_event.warning("sys_event_port_disconnect(eport_id=0x%x)", eport_id);
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto port = idm::get<lv2_event_port_t>(eport_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!port)
{
return CELL_ESRCH;
}
2015-04-12 03:36:25 +02:00
const auto queue = port->queue.lock();
2015-03-04 05:42:04 +01:00
if (!queue)
{
return CELL_ENOTCONN;
}
2015-03-04 22:51:14 +01:00
// CELL_EBUSY is not returned
2015-03-04 05:42:04 +01:00
port->queue.reset();
return CELL_OK;
}
s32 sys_event_port_send(u32 eport_id, u64 data1, u64 data2, u64 data3)
{
sys_event.trace("sys_event_port_send(eport_id=0x%x, data1=0x%llx, data2=0x%llx, data3=0x%llx)", eport_id, data1, data2, data3);
2015-03-04 05:42:04 +01:00
LV2_LOCK;
const auto port = idm::get<lv2_event_port_t>(eport_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!port)
{
return CELL_ESRCH;
}
2015-04-12 03:36:25 +02:00
const auto queue = port->queue.lock();
2015-03-04 05:42:04 +01:00
if (!queue)
{
return CELL_ENOTCONN;
}
2016-04-14 00:23:53 +02:00
if (queue->events() >= queue->size)
{
return CELL_EBUSY;
}
2015-03-04 22:51:14 +01:00
const u64 source = port->name ? port->name : ((u64)process_getpid() << 32) | (u64)eport_id;
2015-04-13 15:32:09 +02:00
queue->push(lv2_lock, source, data1, data2, data3);
return CELL_OK;
}