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

415 lines
8.2 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"
#include "Emu/SysCalls/SysCalls.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
#include "Emu/Cell/SPUThread.h"
2014-08-23 02:16:54 +02:00
#include "Emu/Event.h"
#include "sys_sync.h"
2014-08-23 16:51:51 +02:00
#include "sys_process.h"
#include "sys_event.h"
SysCallBase sys_event("sys_event");
extern u64 get_system_time();
2015-07-01 19:09:26 +02:00
lv2_event_queue_t::lv2_event_queue_t(u32 protocol, s32 type, u64 name, u64 key, s32 size)
: id(idm::get_last_id())
2015-07-01 19:09:26 +02:00
, protocol(protocol)
, type(type)
, name(name)
, key(key)
, size(size)
{
}
2015-07-19 14:58:11 +02:00
void lv2_event_queue_t::push(lv2_lock_t& lv2_lock, u64 source, u64 data1, u64 data2, u64 data3)
{
CHECK_LV2_LOCK(lv2_lock);
// save event if no waiters
if (sq.empty())
{
return events.emplace_back(source, data1, data2, data3);
}
if (events.size())
{
throw EXCEPTION("Unexpected");
}
2015-07-19 14:58:11 +02:00
// notify waiter; protocol is ignored in current implementation
auto& thread = sq.front();
if (type == SYS_PPU_QUEUE && thread->get_type() == CPU_THREAD_PPU)
2015-07-19 14:58:11 +02:00
{
// store event data in registers
auto& ppu = static_cast<PPUThread&>(*thread);
ppu.GPR[4] = source;
ppu.GPR[5] = data1;
ppu.GPR[6] = data2;
ppu.GPR[7] = data3;
}
else if (type == SYS_SPU_QUEUE && thread->get_type() == CPU_THREAD_SPU)
{
// 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
{
throw EXCEPTION("Unexpected (queue_type=%d, thread_type=%d)", type, thread->get_type());
}
if (!sq.front()->signal())
{
throw EXCEPTION("Thread already signaled");
2015-07-19 14:58:11 +02:00
}
return sq.pop_front();
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)
{
2015-03-04 05:42:04 +01:00
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)
{
2015-07-19 14:58:11 +02:00
sys_event.Error("sys_event_queue_create(): unknown protocol (0x%x)", protocol);
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)
{
2015-07-19 14:58:11 +02:00
sys_event.Error("sys_event_queue_create(): unknown type (0x%x)", type);
return CELL_EINVAL;
}
const auto queue = Emu.GetEventManager().MakeEventQueue(event_queue_key, 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)
{
2015-04-14 04:00:31 +02:00
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;
}
2015-07-19 14:58:11 +02:00
if (!mode && queue->sq.size())
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
Emu.GetEventManager().UnregisterKey(queue->key);
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
for (auto& thread : queue->sq)
2015-03-04 05:42:04 +01:00
{
if (queue->type == SYS_PPU_QUEUE && thread->get_type() == CPU_THREAD_PPU)
{
static_cast<PPUThread&>(*thread).GPR[3] = 1;
}
else if (queue->type == SYS_SPU_QUEUE && thread->get_type() == CPU_THREAD_SPU)
{
static_cast<SPUThread&>(*thread).ch_in_mbox.set_values(1, CELL_ECANCELED);
}
else
{
throw EXCEPTION("Unexpected (queue_type=%d, thread_type=%d)", queue->type, thread->get_type());
}
2015-07-19 14:58:11 +02:00
thread->signal();
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)
{
2015-04-14 04:00:31 +02:00
sys_event.Log("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)
{
2015-07-01 19:09:26 +02:00
throw EXCEPTION("Negative size");
}
if (queue->type != SYS_PPU_QUEUE)
{
return CELL_EINVAL;
}
2015-03-04 05:42:04 +01:00
s32 count = 0;
2015-07-19 14:58:11 +02:00
while (queue->sq.empty() && count < size && queue->events.size())
{
2015-06-21 02:17:42 +02:00
auto& dest = event_array[count++];
2015-07-19 14:58:11 +02:00
std::tie(dest.source, dest.data1, dest.data2, dest.data3) = queue->events.front();
2015-03-04 05:42:04 +01:00
queue->events.pop_front();
}
*number = count;
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
2015-07-19 14:58:11 +02:00
s32 sys_event_queue_receive(PPUThread& ppu, u32 equeue_id, vm::ptr<sys_event_t> dummy_event, u64 timeout)
{
2015-07-19 14:58:11 +02:00
sys_event.Log("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;
}
if (queue->events.size())
{
// 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->events.front();
2015-07-03 18:07:36 +02:00
queue->events.pop_front();
2015-07-19 14:58:11 +02:00
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
sleep_queue_entry_t waiter(ppu, queue->sq);
while (!ppu.unsignal())
{
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
}
ppu.cv.wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
}
else
{
ppu.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
{
if (idm::check<lv2_event_queue_t>(equeue_id))
{
2015-07-19 14:58:11 +02:00
throw EXCEPTION("Unexpected");
}
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)
{
2015-04-14 04:00:31 +02:00
sys_event.Log("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;
}
2015-04-12 03:36:25 +02:00
queue->events.clear();
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)
{
2015-03-04 05:42:04 +01:00
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)
{
2015-07-19 14:58:11 +02:00
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)
{
2015-04-14 04:00:31 +02:00
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)
{
2015-04-14 04:00:31 +02:00
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)
{
2015-04-14 04:00:31 +02:00
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)
{
2015-04-14 04:00:31 +02:00
sys_event.Log("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;
}
2015-03-04 05:42:04 +01:00
if (queue->events.size() >= 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;
}