2020-12-05 13:08:24 +01:00
|
|
|
#include "stdafx.h"
|
2018-09-29 00:12:00 +02:00
|
|
|
#include "sys_event.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"
|
2022-07-04 15:02:17 +02:00
|
|
|
#include "Emu/System.h"
|
2014-06-25 00:38:34 +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"
|
2015-07-20 21:41:19 +02:00
|
|
|
#include "Emu/Cell/SPUThread.h"
|
2014-08-23 16:51:51 +02:00
|
|
|
#include "sys_process.h"
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2018-08-25 14:39:00 +02:00
|
|
|
LOG_CHANNEL(sys_event);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2021-05-08 19:08:25 +02:00
|
|
|
lv2_event_queue::lv2_event_queue(u32 protocol, s32 type, s32 size, u64 name, u64 ipc_key) noexcept
|
|
|
|
|
: id(idm::last_id())
|
|
|
|
|
, protocol{static_cast<u8>(protocol)}
|
|
|
|
|
, type(static_cast<u8>(type))
|
|
|
|
|
, size(static_cast<u8>(size))
|
2021-05-07 19:48:26 +02:00
|
|
|
, name(name)
|
|
|
|
|
, key(ipc_key)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:02:17 +02:00
|
|
|
lv2_event_queue::lv2_event_queue(utils::serial& ar) noexcept
|
|
|
|
|
: id(idm::last_id())
|
|
|
|
|
, protocol(ar)
|
|
|
|
|
, type(ar)
|
|
|
|
|
, size(ar)
|
|
|
|
|
, name(ar)
|
|
|
|
|
, key(ar)
|
|
|
|
|
{
|
|
|
|
|
ar(events);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<void> lv2_event_queue::load(utils::serial& ar)
|
|
|
|
|
{
|
|
|
|
|
auto queue = std::make_shared<lv2_event_queue>(ar);
|
|
|
|
|
return lv2_obj::load(queue->key, queue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void lv2_event_queue::save(utils::serial& ar)
|
|
|
|
|
{
|
|
|
|
|
ar(protocol, type, size, name, key, events);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void lv2_event_queue::save_ptr(utils::serial& ar, lv2_event_queue* q)
|
|
|
|
|
{
|
|
|
|
|
if (!lv2_obj::check(q))
|
|
|
|
|
{
|
|
|
|
|
ar(u32{0});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ar(q->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<lv2_event_queue> lv2_event_queue::load_ptr(utils::serial& ar, std::shared_ptr<lv2_event_queue>& queue)
|
|
|
|
|
{
|
|
|
|
|
const u32 id = ar.operator u32();
|
|
|
|
|
|
|
|
|
|
if (!id)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto q = idm::get_unlocked<lv2_obj, lv2_event_queue>(id))
|
|
|
|
|
{
|
|
|
|
|
// Already initialized
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Emu.DeferDeserialization([id, &queue]()
|
|
|
|
|
{
|
|
|
|
|
// Defer resolving
|
|
|
|
|
queue = ensure(idm::get_unlocked<lv2_obj, lv2_event_queue>(id));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Null until resolved
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lv2_event_port::lv2_event_port(utils::serial& ar)
|
|
|
|
|
: type(ar)
|
|
|
|
|
, name(ar)
|
|
|
|
|
, queue(lv2_event_queue::load_ptr(ar, queue))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void lv2_event_port::save(utils::serial& ar)
|
|
|
|
|
{
|
|
|
|
|
ar(type, name);
|
|
|
|
|
|
|
|
|
|
lv2_event_queue::save_ptr(ar, queue.get());
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 17:50:18 +01:00
|
|
|
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)
|
2015-07-20 21:41:19 +02:00
|
|
|
{
|
2016-04-14 00:23:53 +02:00
|
|
|
// Invalid IPC key
|
2020-04-09 18:05:43 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 08:58:30 +02:00
|
|
|
return g_fxo->get<ipc_manager<lv2_event_queue, u64>>().get(ipc_key);
|
2020-04-09 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-28 06:36:46 +02:00
|
|
|
extern void resume_spu_thread_group_from_waiting(spu_thread& spu);
|
|
|
|
|
|
2022-07-21 10:08:57 +02:00
|
|
|
CellError lv2_event_queue::send(lv2_event event, bool notify_later)
|
2017-01-25 18:50:30 +01:00
|
|
|
{
|
2022-07-21 10:08:57 +02:00
|
|
|
lv2_obj::notify_all_t notify;
|
|
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(mutex);
|
2016-04-14 00:23:53 +02:00
|
|
|
|
2020-05-02 10:22:28 +02:00
|
|
|
if (!exists)
|
|
|
|
|
{
|
|
|
|
|
return CELL_ENOTCONN;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (sq.empty())
|
2015-07-20 21:41:19 +02:00
|
|
|
{
|
2020-02-19 18:03:59 +01:00
|
|
|
if (events.size() < this->size + 0u)
|
2017-02-03 22:36:04 +01:00
|
|
|
{
|
|
|
|
|
// Save event
|
|
|
|
|
events.emplace_back(event);
|
2020-05-02 10:22:28 +02:00
|
|
|
return {};
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
2015-07-19 14:58:11 +02:00
|
|
|
|
2020-05-02 10:22:28 +02:00
|
|
|
return CELL_EBUSY;
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
2015-07-20 21:41:19 +02:00
|
|
|
|
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));
|
2015-07-20 21:41:19 +02:00
|
|
|
|
2022-07-04 15:02:17 +02:00
|
|
|
if (ppu.state & cpu_flag::again)
|
|
|
|
|
{
|
|
|
|
|
if (auto cpu = get_current_cpu_thread())
|
|
|
|
|
{
|
|
|
|
|
cpu->state += cpu_flag::again;
|
|
|
|
|
cpu->state += cpu_flag::exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sys_event.warning("Ignored event!");
|
|
|
|
|
|
|
|
|
|
// Fake error for abort
|
|
|
|
|
return CELL_EAGAIN;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
std::tie(ppu.gpr[4], ppu.gpr[5], ppu.gpr[6], ppu.gpr[7]) = event;
|
2015-07-20 21:41:19 +02:00
|
|
|
|
2022-07-21 10:08:57 +02:00
|
|
|
awake(&ppu, notify_later);
|
2015-07-20 21:41:19 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-03 22:36:04 +01:00
|
|
|
// Store event in In_MBox
|
2021-08-13 05:38:53 +02:00
|
|
|
auto& spu = static_cast<spu_thread&>(*schedule<spu_thread>(sq, protocol));
|
2022-07-04 15:02:17 +02:00
|
|
|
|
|
|
|
|
if (spu.state & cpu_flag::again)
|
|
|
|
|
{
|
|
|
|
|
if (auto cpu = get_current_cpu_thread())
|
|
|
|
|
{
|
|
|
|
|
cpu->state += cpu_flag::exit + cpu_flag::again;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sys_event.warning("Ignored event!");
|
|
|
|
|
|
|
|
|
|
// Fake error for abort
|
|
|
|
|
return CELL_EAGAIN;
|
|
|
|
|
}
|
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);
|
2021-07-28 06:36:46 +02:00
|
|
|
resume_spu_thread_group_from_waiting(spu);
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-02 10:22:28 +02:00
|
|
|
return {};
|
2015-07-19 14:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-07 08:58:30 +02:00
|
|
|
error_code sys_event_queue_create(cpu_thread& cpu, vm::ptr<u32> equeue_id, vm::ptr<sys_event_queue_attribute_t> attr, u64 ipc_key, s32 size)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-10-30 15:43:09 +01:00
|
|
|
cpu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2021-05-07 08:58:30 +02:00
|
|
|
sys_event.warning("sys_event_queue_create(equeue_id=*0x%x, attr=*0x%x, ipc_key=0x%llx, size=%d)", equeue_id, attr, ipc_key, size);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2015-03-04 05:42:04 +01:00
|
|
|
if (size <= 0 || size > 127)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.error("sys_event_queue_create(): unknown protocol (0x%x)", protocol);
|
2015-07-19 14:58:11 +02:00
|
|
|
return CELL_EINVAL;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.error("sys_event_queue_create(): unknown type (0x%x)", type);
|
2015-07-19 14:58:11 +02:00
|
|
|
return CELL_EINVAL;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-07 10:34:25 +02:00
|
|
|
const u32 pshared = ipc_key == SYS_EVENT_QUEUE_LOCAL ? SYS_SYNC_NOT_PROCESS_SHARED : SYS_SYNC_PROCESS_SHARED;
|
2021-05-07 19:48:26 +02:00
|
|
|
constexpr u32 flags = SYS_SYNC_NEWLY_CREATED;
|
2021-05-07 08:58:30 +02:00
|
|
|
const u64 name = attr->name_u64;
|
2017-02-03 22:36:04 +01:00
|
|
|
|
2021-05-07 08:58:30 +02:00
|
|
|
if (const auto error = lv2_obj::create<lv2_event_queue>(pshared, ipc_key, flags, [&]()
|
2017-02-03 22:36:04 +01:00
|
|
|
{
|
2021-05-08 19:08:25 +02:00
|
|
|
return std::make_shared<lv2_event_queue>(protocol, type, size, name, ipc_key);
|
2017-02-03 22:36:04 +01:00
|
|
|
}))
|
|
|
|
|
{
|
2020-06-04 08:42:04 +02:00
|
|
|
return error;
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-07 08:58:30 +02:00
|
|
|
*equeue_id = idm::last_id();
|
2017-07-24 17:59:48 +02:00
|
|
|
return CELL_OK;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
error_code sys_event_queue_destroy(ppu_thread& ppu, u32 equeue_id, s32 mode)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01: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
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (mode && mode != SYS_EVENT_QUEUE_DESTROY_FORCE)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-13 18:22:54 +01:00
|
|
|
std::vector<lv2_event> events;
|
|
|
|
|
|
2022-07-07 04:03:16 +02:00
|
|
|
std::unique_lock<shared_mutex> qlock;
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
const auto queue = idm::withdraw<lv2_obj, lv2_event_queue>(equeue_id, [&](lv2_event_queue& queue) -> CellError
|
|
|
|
|
{
|
2022-07-07 09:35:24 +02:00
|
|
|
qlock = std::unique_lock{queue.mutex};
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (!mode && !queue.sq.empty())
|
|
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-13 18:22:54 +01:00
|
|
|
if (!queue.events.empty())
|
|
|
|
|
{
|
|
|
|
|
// Copy events for logging, does not empty
|
|
|
|
|
events.insert(events.begin(), queue.events.begin(), queue.events.end());
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 12:08:16 +02:00
|
|
|
lv2_obj::on_id_destroy(queue, queue.key);
|
2022-07-07 04:03:16 +02:00
|
|
|
|
|
|
|
|
if (queue.sq.empty())
|
|
|
|
|
{
|
|
|
|
|
qlock.unlock();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (auto cpu : queue.sq)
|
|
|
|
|
{
|
|
|
|
|
if (cpu->state & cpu_flag::again)
|
|
|
|
|
{
|
|
|
|
|
ppu.state += cpu_flag::again;
|
|
|
|
|
return CELL_EAGAIN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 (!queue)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 04:03:16 +02:00
|
|
|
if (ppu.state & cpu_flag::again)
|
|
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (queue.ret)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2017-02-03 22:36:04 +01:00
|
|
|
return queue.ret;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-13 18:22:54 +01:00
|
|
|
std::string lost_data;
|
|
|
|
|
|
2022-07-07 04:03:16 +02:00
|
|
|
if (qlock.owns_lock())
|
2015-03-04 05:42:04 +01:00
|
|
|
{
|
2021-07-28 06:36:46 +02:00
|
|
|
std::deque<cpu_thread*> sq;
|
|
|
|
|
|
|
|
|
|
sq = std::move(queue->sq);
|
2021-11-13 18:22:54 +01:00
|
|
|
|
|
|
|
|
if (sys_event.warning)
|
|
|
|
|
{
|
|
|
|
|
fmt::append(lost_data, "Forcefully awaken waiters (%u):\n", sq.size());
|
|
|
|
|
|
|
|
|
|
for (auto cpu : sq)
|
|
|
|
|
{
|
|
|
|
|
lost_data += cpu->get_name();
|
|
|
|
|
lost_data += '\n';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 16:27:50 +02:00
|
|
|
if (queue->type == SYS_PPU_QUEUE)
|
2015-07-20 21:41:19 +02:00
|
|
|
{
|
2021-07-28 06:36:46 +02:00
|
|
|
for (auto cpu : sq)
|
2017-02-03 22:36:04 +01:00
|
|
|
{
|
|
|
|
|
static_cast<ppu_thread&>(*cpu).gpr[3] = CELL_ECANCELED;
|
2019-04-25 16:27:50 +02:00
|
|
|
queue->append(cpu);
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
2019-04-25 16:27:50 +02:00
|
|
|
|
2021-11-13 18:22:54 +01:00
|
|
|
lv2_obj::awake_all();
|
2019-04-25 16:27:50 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-07-28 06:36:46 +02:00
|
|
|
for (auto cpu : sq)
|
2017-02-03 22:36:04 +01:00
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
static_cast<spu_thread&>(*cpu).ch_in_mbox.set_values(1, CELL_ECANCELED);
|
2021-07-28 06:36:46 +02:00
|
|
|
resume_spu_thread_group_from_waiting(static_cast<spu_thread&>(*cpu));
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-07 04:03:16 +02:00
|
|
|
|
|
|
|
|
qlock.unlock();
|
2015-03-04 05:42:04 +01:00
|
|
|
}
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2021-11-13 18:22:54 +01:00
|
|
|
if (sys_event.warning)
|
|
|
|
|
{
|
|
|
|
|
if (!events.empty())
|
|
|
|
|
{
|
|
|
|
|
fmt::append(lost_data, "Unread queue events (%u):\n", events.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const lv2_event& evt : events)
|
|
|
|
|
{
|
|
|
|
|
fmt::append(lost_data, "data0=0x%x, data1=0x%x, data2=0x%x, data3=0x%x\n"
|
|
|
|
|
, std::get<0>(evt), std::get<1>(evt), std::get<2>(evt), std::get<3>(evt));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lost_data.empty())
|
|
|
|
|
{
|
|
|
|
|
sys_event.warning("sys_event_queue_destroy(): %s", lost_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02: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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
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
|
|
|
|
2017-01-29 17:50:18 +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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 00:34:20 +01:00
|
|
|
if (queue->type != SYS_PPU_QUEUE)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(queue->mutex);
|
2017-02-03 22:36:04 +01:00
|
|
|
|
2021-11-13 18:22:54 +01:00
|
|
|
if (!queue->exists)
|
|
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
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())
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2015-06-21 02:17:42 +02:00
|
|
|
auto& dest = event_array[count++];
|
2021-04-09 21:12:47 +02:00
|
|
|
const auto event = queue->events.front();
|
2017-02-03 22:36:04 +01:00
|
|
|
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;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-22 00:27:58 +02:00
|
|
|
*number = count;
|
2015-03-05 22:29:05 +01:00
|
|
|
|
2014-06-25 00:38:34 +02: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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.trace("sys_event_queue_receive(equeue_id=0x%x, *0x%x, timeout=0x%llx)", equeue_id, dummy_event, timeout);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
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
|
|
|
|
2022-07-21 10:08:57 +02:00
|
|
|
lv2_obj::notify_all_t notify;
|
|
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(queue.mutex);
|
2018-02-09 15:49:37 +01:00
|
|
|
|
2021-04-25 16:23:37 +02:00
|
|
|
// "/dev_flash/vsh/module/msmw2.sprx" seems to rely on some cryptic shared memory behaviour that we don't emulate correctly
|
|
|
|
|
// This is a hack to avoid waiting for 1m40s every time we boot vsh
|
|
|
|
|
if (queue.key == 0x8005911000000012 && g_ps3_process_info.get_cellos_appname() == "vsh.self"sv)
|
|
|
|
|
{
|
|
|
|
|
sys_event.todo("sys_event_queue_receive(equeue_id=0x%x, *0x%x, timeout=0x%llx) Bypassing timeout for msmw2.sprx", equeue_id, dummy_event, timeout);
|
|
|
|
|
timeout = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (queue.events.empty())
|
|
|
|
|
{
|
|
|
|
|
queue.sq.emplace_back(&ppu);
|
2022-07-21 10:08:57 +02:00
|
|
|
queue.sleep(ppu, timeout, true);
|
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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (queue.ret)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2017-02-03 22:36:04 +01:00
|
|
|
if (queue.ret != CELL_EBUSY)
|
|
|
|
|
{
|
|
|
|
|
return queue.ret;
|
|
|
|
|
}
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
2017-02-03 22:36:04 +01:00
|
|
|
else
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2015-07-20 21:41:19 +02:00
|
|
|
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.
|
2021-02-13 15:50:07 +01:00
|
|
|
while (auto state = ppu.state.fetch_sub(cpu_flag::signal))
|
2015-07-20 21:41:19 +02:00
|
|
|
{
|
2022-07-04 15:02:17 +02:00
|
|
|
if (state & cpu_flag::signal)
|
2018-10-11 00:17:19 +02:00
|
|
|
{
|
2021-02-13 15:50:07 +01:00
|
|
|
break;
|
2018-10-11 00:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:02:17 +02:00
|
|
|
if (is_stopped(state))
|
|
|
|
|
{
|
2022-07-07 09:35:24 +02:00
|
|
|
std::lock_guard lock_rsx(queue->mutex);
|
|
|
|
|
|
|
|
|
|
if (std::find(queue->sq.begin(), queue->sq.end(), &ppu) == queue->sq.end())
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ppu.state += cpu_flag::again;
|
2022-07-04 15:02:17 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 21:41:19 +02:00
|
|
|
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
|
|
|
{
|
2020-03-28 04:16:59 +01:00
|
|
|
// Wait for rescheduling
|
|
|
|
|
if (ppu.check_state())
|
|
|
|
|
{
|
2022-07-04 15:02:17 +02:00
|
|
|
continue;
|
2020-03-28 04:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(queue->mutex);
|
2017-02-03 22:36:04 +01:00
|
|
|
|
|
|
|
|
if (!queue->unqueue(queue->sq, &ppu))
|
|
|
|
|
{
|
2020-03-28 04:16:59 +01:00
|
|
|
break;
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
ppu.gpr[3] = CELL_ETIMEDOUT;
|
|
|
|
|
break;
|
2015-07-19 14:58:11 +02:00
|
|
|
}
|
2015-07-20 21:41:19 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-13 15:50:07 +01:00
|
|
|
thread_ctrl::wait_on(ppu.state, state);
|
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]);
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 17:06:02 +02:00
|
|
|
error_code sys_event_queue_drain(ppu_thread& ppu, u32 equeue_id)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.trace("sys_event_queue_drain(equeue_id=0x%x)", equeue_id);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
const auto queue = idm::check<lv2_obj, lv2_event_queue>(equeue_id, [&](lv2_event_queue& queue)
|
|
|
|
|
{
|
2018-09-03 21:28:33 +02:00
|
|
|
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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 15:43:09 +01:00
|
|
|
error_code sys_event_port_create(cpu_thread& cpu, vm::ptr<u32> eport_id, s32 port_type, u64 name)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-10-30 15:43:09 +01:00
|
|
|
cpu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.warning("sys_event_port_create(eport_id=*0x%x, port_type=%d, name=0x%llx)", eport_id, port_type, name);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2017-08-31 21:10:22 +02:00
|
|
|
if (port_type != SYS_EVENT_PORT_LOCAL && port_type != 3)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.error("sys_event_port_create(): unknown port type (%d)", port_type);
|
2014-06-25 00:38:34 +02:00
|
|
|
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;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 17:06:02 +02:00
|
|
|
error_code sys_event_port_destroy(ppu_thread& ppu, u32 eport_id)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.warning("sys_event_port_destroy(eport_id=0x%x)", eport_id);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
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
|
|
|
|
|
{
|
2021-05-14 19:10:47 +02:00
|
|
|
if (lv2_obj::check(port.queue))
|
2017-02-03 22:36:04 +01:00
|
|
|
{
|
|
|
|
|
return CELL_EISCONN;
|
|
|
|
|
}
|
2014-06-25 00:38:34 +02:00
|
|
|
|
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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2015-03-04 05:42:04 +01:00
|
|
|
return CELL_ESRCH;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (port.ret)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2017-02-03 22:36:04 +01:00
|
|
|
return port.ret;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 15:43:09 +01:00
|
|
|
error_code sys_event_port_connect_local(cpu_thread& cpu, u32 eport_id, u32 equeue_id)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-10-30 15:43:09 +01:00
|
|
|
cpu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.warning("sys_event_port_connect_local(eport_id=0x%x, equeue_id=0x%x)", eport_id, equeue_id);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
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))
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-04 22:51:14 +01:00
|
|
|
if (port->type != SYS_EVENT_PORT_LOCAL)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2021-05-14 19:10:47 +02:00
|
|
|
if (lv2_obj::check(port->queue))
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
2014-06-25 00:38:34 +02: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)
|
2017-08-31 21:10:22 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2017-08-31 21:10:22 +02:00
|
|
|
sys_event.warning("sys_event_port_connect_ipc(eport_id=0x%x, ipc_key=0x%x)", eport_id, ipc_key);
|
|
|
|
|
|
2019-11-01 20:22:43 +01:00
|
|
|
if (ipc_key == 0)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 21:10:22 +02:00
|
|
|
auto queue = lv2_event_queue::find(ipc_key);
|
|
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(id_manager::g_mutex);
|
2017-08-31 21:10:22 +02:00
|
|
|
|
|
|
|
|
const auto port = idm::check_unlocked<lv2_obj, lv2_event_port>(eport_id);
|
|
|
|
|
|
|
|
|
|
if (!port || !queue)
|
|
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 19:21:45 +02:00
|
|
|
if (port->type != SYS_EVENT_PORT_IPC)
|
2017-08-31 21:10:22 +02:00
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 19:10:47 +02:00
|
|
|
if (lv2_obj::check(port->queue))
|
2017-08-31 21:10:22 +02:00
|
|
|
{
|
|
|
|
|
return CELL_EISCONN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->queue = std::move(queue);
|
|
|
|
|
|
2017-09-02 22:51:37 +02:00
|
|
|
return CELL_OK;
|
2017-08-31 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 17:06:02 +02:00
|
|
|
error_code sys_event_port_disconnect(ppu_thread& ppu, u32 eport_id)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_event.warning("sys_event_port_disconnect(eport_id=0x%x)", eport_id);
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 19:10:47 +02:00
|
|
|
if (!lv2_obj::check(port->queue))
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
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();
|
2015-03-05 14:18:06 +01:00
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
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)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2020-10-30 15:43:09 +01:00
|
|
|
if (auto cpu = get_current_cpu_thread())
|
|
|
|
|
{
|
|
|
|
|
cpu->state += cpu_flag::wait;
|
|
|
|
|
}
|
2019-07-14 17:06:02 +02:00
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
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
|
|
|
|
2022-07-21 10:08:57 +02:00
|
|
|
const auto port = idm::check<lv2_obj, lv2_event_port>(eport_id, [&](lv2_event_port& port) -> CellError
|
2017-02-03 22:36:04 +01:00
|
|
|
{
|
2021-05-14 19:10:47 +02:00
|
|
|
if (lv2_obj::check(port.queue))
|
2017-02-03 22:36:04 +01:00
|
|
|
{
|
2019-11-29 23:28:06 +01:00
|
|
|
const u64 source = port.name ? port.name : (s64{process_getpid()} << 32) | u64{eport_id};
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2022-07-21 10:08:57 +02:00
|
|
|
return port.queue->send(source, data1, data2, data3, true);
|
2017-02-03 22:36:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CELL_ENOTCONN;
|
|
|
|
|
});
|
2015-03-05 22:29:05 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!port)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (port.ret)
|
2014-06-25 00:38:34 +02:00
|
|
|
{
|
2022-07-04 15:02:17 +02:00
|
|
|
if (port.ret == CELL_EAGAIN)
|
|
|
|
|
{
|
2022-07-07 09:35:24 +02:00
|
|
|
// Not really an error code exposed to games (thread has raised cpu_flag::again)
|
|
|
|
|
return not_an_error(CELL_EAGAIN);
|
2022-07-04 15:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
if (port.ret == CELL_EBUSY)
|
|
|
|
|
{
|
|
|
|
|
return not_an_error(CELL_EBUSY);
|
|
|
|
|
}
|
2014-06-25 00:38:34 +02:00
|
|
|
|
2017-02-03 22:36:04 +01:00
|
|
|
return port.ret;
|
2014-06-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-07-26 05:31:46 +02:00
|
|
|
}
|