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

521 lines
10 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "sys_event.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"
LOG_CHANNEL(sys_event);
template<> DECLARE(ipc_manager<lv2_event_queue, u64>::g_ipc) {};
std::shared_ptr<lv2_event_queue> lv2_event_queue::find(u64 ipc_key)
2016-04-14 00:23:53 +02:00
{
if (ipc_key == SYS_EVENT_QUEUE_LOCAL)
{
2016-04-14 00:23:53 +02:00
// Invalid IPC key
return{};
}
return ipc_manager<lv2_event_queue, u64>::get(ipc_key);
2016-04-14 00:23:53 +02:00
}
2017-02-03 22:36:04 +01:00
bool lv2_event_queue::send(lv2_event event)
2017-01-25 18:50:30 +01:00
{
std::lock_guard lock(mutex);
2016-04-14 00:23:53 +02:00
2017-02-03 22:36:04 +01:00
if (sq.empty())
{
2017-02-03 22:36:04 +01:00
if (events.size() < this->size)
{
// Save event
events.emplace_back(event);
return true;
}
2015-07-19 14:58:11 +02:00
2017-02-03 22:36:04 +01:00
return false;
}
2017-02-03 22:36:04 +01:00
if (type == SYS_PPU_QUEUE)
2015-07-19 14:58:11 +02:00
{
2017-02-03 22:36:04 +01:00
// Store event in registers
auto& ppu = static_cast<ppu_thread&>(*schedule<ppu_thread>(sq, protocol));
2017-02-03 22:36:04 +01:00
std::tie(ppu.gpr[4], ppu.gpr[5], ppu.gpr[6], ppu.gpr[7]) = event;
awake(&ppu);
}
else
{
2017-02-03 22:36:04 +01:00
// Store event in In_MBox
auto& spu = static_cast<spu_thread&>(*sq.front());
2017-02-03 22:36:04 +01:00
// TODO: use protocol?
sq.pop_front();
2016-04-14 00:23:53 +02:00
2017-02-03 22:36:04 +01:00
const u32 data1 = static_cast<u32>(std::get<1>(event));
const u32 data2 = static_cast<u32>(std::get<2>(event));
const u32 data3 = static_cast<u32>(std::get<3>(event));
spu.ch_in_mbox.set_values(4, CELL_OK, data1, data2, data3);
2017-02-06 19:36:46 +01:00
spu.state += cpu_flag::signal;
spu.notify();
2017-02-03 22:36:04 +01:00
}
return true;
2015-07-19 14:58:11 +02:00
}
2017-02-03 22:36:04 +01:00
error_code sys_event_queue_create(vm::ptr<u32> equeue_id, vm::ptr<sys_event_queue_attribute_t> attr, u64 event_queue_key, s32 size)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock();
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;
}
2017-07-24 17:59:48 +02:00
auto queue = std::make_shared<lv2_event_queue>(protocol, type, attr->name_u64, event_queue_key, size);
2017-02-03 22:36:04 +01:00
if (event_queue_key == SYS_EVENT_QUEUE_LOCAL)
{
// Not an IPC queue
2017-07-24 17:59:48 +02:00
if (const u32 _id = idm::import_existing<lv2_obj, lv2_event_queue>(std::move(queue)))
2017-02-03 22:36:04 +01:00
{
*equeue_id = _id;
return CELL_OK;
}
2015-03-04 05:42:04 +01:00
2017-02-03 22:36:04 +01:00
return CELL_EAGAIN;
}
// Create IPC queue
2017-07-24 17:59:48 +02:00
if (!ipc_manager<lv2_event_queue, u64>::add(event_queue_key, [&]() -> std::shared_ptr<lv2_event_queue>
2017-02-03 22:36:04 +01:00
{
2017-07-24 17:59:48 +02:00
if (const u32 _id = idm::import_existing<lv2_obj, lv2_event_queue>(queue))
{
*equeue_id = _id;
return std::move(queue);
}
return nullptr;
2017-02-03 22:36:04 +01:00
}))
{
2015-03-11 16:30:50 +01:00
return CELL_EEXIST;
}
2017-07-24 17:59:48 +02:00
if (queue)
2017-02-03 22:36:04 +01:00
{
2017-07-24 17:59:48 +02:00
return CELL_EAGAIN;
2017-02-03 22:36:04 +01:00
}
2017-07-24 17:59:48 +02:00
return CELL_OK;
}
2017-02-06 19:36:46 +01:00
error_code sys_event_queue_destroy(ppu_thread& ppu, u32 equeue_id, s32 mode)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock(ppu);
sys_event.warning("sys_event_queue_destroy(equeue_id=0x%x, mode=%d)", equeue_id, mode);
2015-03-04 05:42:04 +01:00
2017-02-03 22:36:04 +01:00
if (mode && mode != SYS_EVENT_QUEUE_DESTROY_FORCE)
{
return CELL_EINVAL;
}
const auto queue = idm::withdraw<lv2_obj, lv2_event_queue>(equeue_id, [&](lv2_event_queue& queue) -> CellError
{
std::lock_guard lock(queue.mutex);
2017-02-03 22:36:04 +01:00
if (!mode && !queue.sq.empty())
{
return CELL_EBUSY;
}
return {};
});
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
2017-02-03 22:36:04 +01:00
if (queue.ret)
{
2017-02-03 22:36:04 +01:00
return queue.ret;
}
2017-02-03 22:36:04 +01:00
if (mode == SYS_EVENT_QUEUE_DESTROY_FORCE)
2015-03-04 05:42:04 +01:00
{
std::lock_guard lock(queue->mutex);
2015-03-11 16:30:50 +01:00
if (queue->type == SYS_PPU_QUEUE)
{
for (auto cpu : queue->sq)
2017-02-03 22:36:04 +01:00
{
static_cast<ppu_thread&>(*cpu).gpr[3] = CELL_ECANCELED;
queue->append(cpu);
2017-02-03 22:36:04 +01:00
}
if (!queue->sq.empty())
{
lv2_obj::awake_all();
}
}
else
{
for (auto cpu : queue->sq)
2017-02-03 22:36:04 +01:00
{
static_cast<spu_thread&>(*cpu).ch_in_mbox.set_values(1, CELL_ECANCELED);
2017-02-06 19:36:46 +01:00
cpu->state += cpu_flag::signal;
cpu->notify();
2017-02-03 22:36:04 +01:00
}
}
2015-03-04 05:42:04 +01:00
}
return CELL_OK;
}
2019-07-14 17:06:02 +02:00
error_code sys_event_queue_tryreceive(ppu_thread& ppu, u32 equeue_id, vm::ptr<sys_event_t> event_array, s32 size, vm::ptr<u32> number)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock(ppu);
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
const auto queue = idm::get<lv2_obj, lv2_event_queue>(equeue_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
if (queue->type != SYS_PPU_QUEUE)
{
return CELL_EINVAL;
}
std::lock_guard lock(queue->mutex);
2017-02-03 22:36:04 +01:00
2015-03-04 05:42:04 +01:00
s32 count = 0;
2017-02-03 22:36:04 +01:00
while (queue->sq.empty() && count < size && !queue->events.empty())
{
2015-06-21 02:17:42 +02:00
auto& dest = event_array[count++];
2017-02-03 22:36:04 +01:00
auto event = queue->events.front();
queue->events.pop_front();
2015-06-21 02:17:42 +02:00
2017-02-03 22:36:04 +01:00
std::tie(dest.source, dest.data1, dest.data2, dest.data3) = event;
}
*number = count;
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
2017-02-03 22:36:04 +01:00
error_code sys_event_queue_receive(ppu_thread& ppu, u32 equeue_id, vm::ptr<sys_event_t> dummy_event, u64 timeout)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock(ppu);
sys_event.trace("sys_event_queue_receive(equeue_id=0x%x, *0x%x, timeout=0x%llx)", equeue_id, dummy_event, timeout);
2017-07-29 15:07:51 +02:00
ppu.gpr[3] = CELL_OK;
2017-02-03 22:36:04 +01:00
const auto queue = idm::get<lv2_obj, lv2_event_queue>(equeue_id, [&](lv2_event_queue& queue) -> CellError
{
if (queue.type != SYS_PPU_QUEUE)
{
return CELL_EINVAL;
}
2015-03-04 05:42:04 +01:00
std::lock_guard lock(queue.mutex);
2018-02-09 15:49:37 +01:00
2017-02-03 22:36:04 +01:00
if (queue.events.empty())
{
queue.sq.emplace_back(&ppu);
queue.sleep(ppu, timeout);
2017-02-03 22:36:04 +01:00
return CELL_EBUSY;
}
std::tie(ppu.gpr[4], ppu.gpr[5], ppu.gpr[6], ppu.gpr[7]) = queue.events.front();
queue.events.pop_front();
return {};
});
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
2017-02-03 22:36:04 +01:00
if (queue.ret)
{
2017-02-03 22:36:04 +01:00
if (queue.ret != CELL_EBUSY)
{
return queue.ret;
}
}
2017-02-03 22:36:04 +01:00
else
{
return CELL_OK;
}
2015-07-19 14:58:11 +02:00
2017-02-03 22:36:04 +01:00
// If cancelled, gpr[3] will be non-zero. Other registers must contain event data.
while (!ppu.state.test_and_reset(cpu_flag::signal))
{
if (ppu.is_stopped())
{
return 0;
}
if (timeout)
{
2019-07-14 05:55:11 +02:00
if (lv2_obj::wait_timeout(timeout, &ppu))
2015-07-19 14:58:11 +02:00
{
std::lock_guard lock(queue->mutex);
2017-02-03 22:36:04 +01:00
if (!queue->unqueue(queue->sq, &ppu))
{
timeout = 0;
continue;
}
2017-02-06 19:36:46 +01:00
ppu.gpr[3] = CELL_ETIMEDOUT;
break;
2015-07-19 14:58:11 +02:00
}
}
else
{
2017-02-03 22:36:04 +01:00
thread_ctrl::wait();
2014-12-23 00:31:11 +01:00
}
2015-07-19 14:58:11 +02:00
}
2014-12-23 00:31:11 +01:00
2017-02-06 19:36:46 +01:00
return not_an_error(ppu.gpr[3]);
}
2019-07-14 17:06:02 +02:00
error_code sys_event_queue_drain(ppu_thread& ppu, u32 equeue_id)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock(ppu);
sys_event.trace("sys_event_queue_drain(equeue_id=0x%x)", equeue_id);
2017-02-03 22:36:04 +01:00
const auto queue = idm::check<lv2_obj, lv2_event_queue>(equeue_id, [&](lv2_event_queue& queue)
{
std::lock_guard lock(queue.mutex);
2015-03-04 05:42:04 +01:00
2017-02-03 22:36:04 +01:00
queue.events.clear();
});
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!queue)
{
return CELL_ESRCH;
}
return CELL_OK;
}
2017-02-03 22:36:04 +01:00
error_code sys_event_port_create(vm::ptr<u32> eport_id, s32 port_type, u64 name)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock();
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 && port_type != 3)
{
sys_event.error("sys_event_port_create(): unknown port type (%d)", port_type);
return CELL_EINVAL;
}
2017-02-03 22:36:04 +01:00
if (const u32 id = idm::make<lv2_obj, lv2_event_port>(port_type, name))
{
*eport_id = id;
return CELL_OK;
}
2018-02-09 15:49:37 +01:00
2017-02-03 22:36:04 +01:00
return CELL_EAGAIN;
}
2019-07-14 17:06:02 +02:00
error_code sys_event_port_destroy(ppu_thread& ppu, u32 eport_id)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock(ppu);
sys_event.warning("sys_event_port_destroy(eport_id=0x%x)", eport_id);
2017-02-03 22:36:04 +01:00
const auto port = idm::withdraw<lv2_obj, lv2_event_port>(eport_id, [](lv2_event_port& port) -> CellError
{
if (!port.queue.expired())
{
return CELL_EISCONN;
}
2017-02-03 22:36:04 +01:00
return {};
});
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;
}
2017-02-03 22:36:04 +01:00
if (port.ret)
{
2017-02-03 22:36:04 +01:00
return port.ret;
}
return CELL_OK;
}
2017-02-03 22:36:04 +01:00
error_code sys_event_port_connect_local(u32 eport_id, u32 equeue_id)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock();
sys_event.warning("sys_event_port_connect_local(eport_id=0x%x, equeue_id=0x%x)", eport_id, equeue_id);
std::lock_guard lock(id_manager::g_mutex);
2015-03-05 22:29:05 +01:00
2017-02-03 22:36:04 +01:00
const auto port = idm::check_unlocked<lv2_obj, lv2_event_port>(eport_id);
2018-02-09 15:49:37 +01:00
2017-02-03 22:36:04 +01:00
if (!port || !idm::check_unlocked<lv2_obj, lv2_event_queue>(equeue_id))
{
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;
}
2017-02-03 22:36:04 +01:00
port->queue = idm::get_unlocked<lv2_obj, lv2_event_queue>(equeue_id);
2015-03-05 22:29:05 +01:00
return CELL_OK;
}
2019-07-14 17:06:02 +02:00
error_code sys_event_port_connect_ipc(ppu_thread& ppu, u32 eport_id, u64 ipc_key)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock(ppu);
sys_event.warning("sys_event_port_connect_ipc(eport_id=0x%x, ipc_key=0x%x)", eport_id, ipc_key);
if (ipc_key == 0)
{
return CELL_EINVAL;
}
auto queue = lv2_event_queue::find(ipc_key);
std::lock_guard lock(id_manager::g_mutex);
const auto port = idm::check_unlocked<lv2_obj, lv2_event_port>(eport_id);
if (!port || !queue)
{
return CELL_ESRCH;
}
if (port->type != 3)
{
return CELL_EINVAL;
}
if (!port->queue.expired())
{
return CELL_EISCONN;
}
port->queue = std::move(queue);
2017-09-02 22:51:37 +02:00
return CELL_OK;
}
2019-07-14 17:06:02 +02:00
error_code sys_event_port_disconnect(ppu_thread& ppu, u32 eport_id)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock(ppu);
sys_event.warning("sys_event_port_disconnect(eport_id=0x%x)", eport_id);
std::lock_guard lock(id_manager::g_mutex);
2015-03-04 05:42:04 +01:00
2017-02-03 22:36:04 +01:00
const auto port = idm::check_unlocked<lv2_obj, lv2_event_port>(eport_id);
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!port)
{
return CELL_ESRCH;
}
2017-02-03 22:36:04 +01:00
if (port->queue.expired())
{
return CELL_ENOTCONN;
}
2017-02-03 22:36:04 +01:00
// TODO: return CELL_EBUSY if necessary (can't detect the condition)
2015-03-04 22:51:14 +01:00
2015-03-04 05:42:04 +01:00
port->queue.reset();
return CELL_OK;
}
2017-07-26 04:57:43 +02:00
error_code sys_event_port_send(u32 eport_id, u64 data1, u64 data2, u64 data3)
{
2019-07-14 17:06:02 +02:00
vm::temporary_unlock();
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
2017-02-03 22:36:04 +01:00
const auto port = idm::get<lv2_obj, lv2_event_port>(eport_id, [&](lv2_event_port& port) -> CellError
{
if (const auto queue = port.queue.lock())
{
const u64 source = port.name ? port.name : ((u64)process_getpid() << 32) | (u64)eport_id;
2017-02-03 22:36:04 +01:00
if (queue->send(source, data1, data2, data3))
{
return {};
}
return CELL_EBUSY;
}
return CELL_ENOTCONN;
});
2015-03-05 22:29:05 +01:00
2015-04-12 03:36:25 +02:00
if (!port)
{
return CELL_ESRCH;
}
2017-02-03 22:36:04 +01:00
if (port.ret)
{
2017-02-03 22:36:04 +01:00
if (port.ret == CELL_EBUSY)
{
return not_an_error(CELL_EBUSY);
}
2017-02-03 22:36:04 +01:00
return port.ret;
}
return CELL_OK;
}