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

451 lines
8.6 KiB
C++
Raw Normal View History

#include "stdafx.h"
2018-07-27 21:07:34 +02:00
#include "Emu/Memory/vm.h"
#include "Emu/System.h"
2015-07-01 00:25:52 +02:00
#include "Emu/IdManager.h"
2014-08-23 16:51:51 +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"
#include "sys_ppu_thread.h"
2017-04-13 17:37:24 +02:00
#include "sys_event.h"
#include "sys_mmapper.h"
2018-02-09 15:49:37 +01:00
2016-08-19 23:14:10 +02:00
2017-05-13 20:30:37 +02:00
logs::channel sys_ppu_thread("sys_ppu_thread");
void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode)
{
2017-03-11 00:14:48 +01:00
vm::temporary_unlock(ppu);
2017-02-06 19:36:46 +01:00
sys_ppu_thread.trace("_sys_ppu_thread_exit(errorcode=0x%llx)", errorcode);
ppu.state += cpu_flag::exit;
2016-06-07 22:24:20 +02:00
2017-02-06 19:36:46 +01:00
// Get joiner ID
const u32 jid = ppu.joiner.fetch_op([](u32& value)
{
if (value == 0)
{
// Joinable, not joined
value = -3;
}
else if (value != -1)
{
// Joinable, joined
value = -2;
}
// Detached otherwise
});
if (jid == -1)
{
2017-02-06 19:36:46 +01:00
// Delete detached thread and unqueue
idm::remove<ppu_thread>(ppu.id);
}
2017-02-06 19:36:46 +01:00
else if (jid != 0)
{
writer_lock lock(id_manager::g_mutex);
// Schedule joiner and unqueue
lv2_obj::awake(*idm::check_unlocked<ppu_thread>(jid), -2);
}
// Unqueue
lv2_obj::sleep(ppu);
2017-02-06 19:36:46 +01:00
// Remove suspend state (TODO)
ppu.state -= cpu_flag::suspend;
}
2017-02-06 19:36:46 +01:00
void sys_ppu_thread_yield(ppu_thread& ppu)
{
sys_ppu_thread.trace("sys_ppu_thread_yield()");
2015-07-03 18:07:36 +02:00
2017-02-06 19:36:46 +01:00
lv2_obj::awake(ppu, -4);
}
error_code sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr<u64> vptr)
{
2017-03-11 00:14:48 +01:00
vm::temporary_unlock(ppu);
2017-02-06 19:36:46 +01:00
sys_ppu_thread.trace("sys_ppu_thread_join(thread_id=0x%x, vptr=*0x%x)", thread_id, vptr);
2017-02-06 19:36:46 +01:00
const auto thread = idm::get<ppu_thread>(thread_id, [&](ppu_thread& thread) -> CellError
{
CellError result = thread.joiner.atomic_op([&](u32& value) -> CellError
{
if (value == -3)
{
value = -2;
return CELL_EBUSY;
}
if (value == -2)
{
return CELL_ESRCH;
}
if (value)
{
return CELL_EINVAL;
}
// TODO: check precedence?
if (&ppu == &thread)
{
return CELL_EDEADLK;
}
value = ppu.id;
return {};
});
if (!result)
{
lv2_obj::sleep(ppu);
2017-02-06 19:36:46 +01:00
}
2018-02-09 15:49:37 +01:00
2017-02-06 19:36:46 +01:00
return result;
});
2015-07-01 00:25:52 +02:00
if (!thread)
2015-04-12 03:36:25 +02:00
{
return CELL_ESRCH;
}
2018-02-09 15:49:37 +01:00
2017-02-06 19:36:46 +01:00
if (thread.ret && thread.ret != CELL_EBUSY)
2015-07-01 00:25:52 +02:00
{
2017-02-06 19:36:46 +01:00
return thread.ret;
}
2017-02-06 19:36:46 +01:00
// Wait for cleanup
thread->join();
// Get the exit status from the register
if (vptr)
{
ppu.test_state();
*vptr = thread->gpr[3];
}
2015-07-01 00:25:52 +02:00
// Cleanup
idm::remove<ppu_thread>(thread->id);
return CELL_OK;
}
error_code sys_ppu_thread_detach(u32 thread_id)
{
2017-02-06 19:36:46 +01:00
sys_ppu_thread.trace("sys_ppu_thread_detach(thread_id=0x%x)", thread_id);
2015-04-12 03:36:25 +02:00
2017-02-06 19:36:46 +01:00
const auto thread = idm::check<ppu_thread>(thread_id, [&](ppu_thread& thread) -> CellError
{
return thread.joiner.atomic_op([&](u32& value) -> CellError
{
if (value == -3)
{
value = -2;
return CELL_EAGAIN;
}
if (value == -2)
{
return CELL_ESRCH;
}
if (value == -1)
{
return CELL_EINVAL;
}
if (value)
{
return CELL_EBUSY;
}
value = -1;
return {};
});
});
2015-07-01 00:25:52 +02:00
if (!thread)
2015-04-12 03:36:25 +02:00
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
2017-02-06 19:36:46 +01:00
if (thread.ret && thread.ret != CELL_EAGAIN)
2015-04-12 03:36:25 +02:00
{
2017-02-06 19:36:46 +01:00
return thread.ret;
2015-04-12 03:36:25 +02:00
}
2015-07-01 00:25:52 +02:00
2017-02-06 19:36:46 +01:00
if (thread.ret == CELL_EAGAIN)
2015-07-01 00:25:52 +02:00
{
2017-02-06 19:36:46 +01:00
idm::remove<ppu_thread>(thread_id);
2015-07-01 00:25:52 +02:00
}
2018-02-09 15:49:37 +01:00
return CELL_OK;
}
void sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ptr<s32> isjoinable)
{
sys_ppu_thread.trace("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable);
2015-07-01 00:25:52 +02:00
*isjoinable = ppu.joiner != -1;
}
2017-02-06 19:36:46 +01:00
error_code sys_ppu_thread_set_priority(ppu_thread& ppu, u32 thread_id, s32 prio)
{
sys_ppu_thread.trace("sys_ppu_thread_set_priority(thread_id=0x%x, prio=%d)", thread_id, prio);
2015-04-12 03:36:25 +02:00
2017-02-06 19:36:46 +01:00
if (prio < 0 || prio > 3071)
{
return CELL_EINVAL;
}
const auto thread = idm::check<ppu_thread>(thread_id, [&](ppu_thread& thread)
{
if (thread.prio != prio && thread.prio.exchange(prio) != prio)
{
lv2_obj::awake(thread, prio);
}
});
2015-07-01 00:25:52 +02:00
if (!thread)
2015-04-12 03:36:25 +02:00
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
return CELL_OK;
}
error_code sys_ppu_thread_get_priority(u32 thread_id, vm::ptr<s32> priop)
{
sys_ppu_thread.trace("sys_ppu_thread_get_priority(thread_id=0x%x, priop=*0x%x)", thread_id, priop);
2017-02-06 19:36:46 +01:00
const auto thread = idm::check<ppu_thread>(thread_id, [&](ppu_thread& thread)
{
*priop = thread.prio;
});
2015-07-01 00:25:52 +02:00
if (!thread)
2015-04-12 03:36:25 +02:00
{
return CELL_ESRCH;
}
return CELL_OK;
}
error_code sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr<sys_ppu_thread_stack_t> sp)
{
sys_ppu_thread.trace("sys_ppu_thread_get_stack_information(sp=*0x%x)", sp);
sp->pst_addr = ppu.stack_addr;
sp->pst_size = ppu.stack_size;
return CELL_OK;
}
error_code sys_ppu_thread_stop(u32 thread_id)
{
sys_ppu_thread.todo("sys_ppu_thread_stop(thread_id=0x%x)", thread_id);
2015-07-01 00:25:52 +02:00
const auto thread = idm::get<ppu_thread>(thread_id);
2015-07-01 00:25:52 +02:00
if (!thread)
2015-04-12 03:36:25 +02:00
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
return CELL_OK;
}
error_code sys_ppu_thread_restart(u32 thread_id)
{
sys_ppu_thread.todo("sys_ppu_thread_restart(thread_id=0x%x)", thread_id);
2015-04-12 03:36:25 +02:00
const auto thread = idm::get<ppu_thread>(thread_id);
2015-07-01 00:25:52 +02:00
if (!thread)
2015-04-12 03:36:25 +02:00
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
return CELL_OK;
}
error_code _sys_ppu_thread_create(vm::ptr<u64> thread_id, vm::ptr<ppu_thread_param_t> param, u64 arg, u64 unk, s32 prio, u32 stacksize, u64 flags, vm::cptr<char> threadname)
{
2016-08-11 01:29:59 +02:00
sys_ppu_thread.warning("_sys_ppu_thread_create(thread_id=*0x%x, param=*0x%x, arg=0x%llx, unk=0x%llx, prio=%d, stacksize=0x%x, flags=0x%llx, threadname=%s)",
2015-04-12 03:36:25 +02:00
thread_id, param, arg, unk, prio, stacksize, flags, threadname);
2015-03-04 22:51:14 +01:00
if (prio < 0 || prio > 3071)
{
2015-03-04 22:51:14 +01:00
return CELL_EINVAL;
}
if ((flags & 3) == 3) // Check two flags: joinable + interrupt not allowed
2015-03-04 22:51:14 +01:00
{
return CELL_EPERM;
}
const u32 tid = idm::import<ppu_thread>([&]()
{
auto ppu = std::make_shared<ppu_thread>(threadname ? threadname.get_ptr() : "", prio, stacksize);
if ((flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0)
{
ppu->joiner = 0;
}
ppu->gpr[13] = param->tls.value();
if ((flags & SYS_PPU_THREAD_CREATE_INTERRUPT) == 0)
{
// Initialize thread entry point
ppu->cmd_list
({
{ ppu_cmd::set_args, 2 }, arg, unk, // Actually unknown
{ ppu_cmd::lle_call, param->entry.value() },
});
}
else
{
// Save entry for further use (workaround)
ppu->gpr[2] = param->entry.value();
}
return ppu;
});
if (!tid)
{
return CELL_EAGAIN;
}
2015-01-28 13:59:16 +01:00
*thread_id = tid;
2015-04-12 03:36:25 +02:00
return CELL_OK;
}
2017-02-06 19:36:46 +01:00
error_code sys_ppu_thread_start(ppu_thread& ppu, u32 thread_id)
{
2017-02-06 19:36:46 +01:00
sys_ppu_thread.trace("sys_ppu_thread_start(thread_id=0x%x)", thread_id);
2015-04-12 03:36:25 +02:00
2017-02-06 19:36:46 +01:00
const auto thread = idm::get<ppu_thread>(thread_id, [&](ppu_thread& thread)
{
lv2_obj::awake(thread, -2);
});
2015-07-01 00:25:52 +02:00
if (!thread)
2015-04-12 03:36:25 +02:00
{
return CELL_ESRCH;
}
2017-02-06 19:36:46 +01:00
if (!thread->state.test_and_reset(cpu_flag::stop))
{
// TODO: what happens there?
return CELL_EPERM;
}
else
{
thread->notify();
2017-04-13 17:37:24 +02:00
// Dirty hack for sound: confirm the creation of _mxr000 event queue
if (thread->m_name == "_cellsurMixerMain")
{
lv2_obj::sleep(ppu);
while (!idm::select<lv2_obj, lv2_event_queue>([](u32, lv2_event_queue& eq)
{
2018-01-03 12:48:50 +01:00
//some games do not set event queue name, though key seems constant for them
return (eq.name == "_mxr000\0"_u64) || (eq.key == 0x8000cafe02460300);
2017-04-13 17:37:24 +02:00
}))
{
thread_ctrl::wait_for(50000);
}
ppu.test_state();
}
2017-02-06 19:36:46 +01:00
}
return CELL_OK;
}
error_code sys_ppu_thread_rename(u32 thread_id, vm::cptr<char> name)
{
2016-08-11 01:29:59 +02:00
sys_ppu_thread.todo("sys_ppu_thread_rename(thread_id=0x%x, name=%s)", thread_id, name);
2015-07-01 00:25:52 +02:00
const auto thread = idm::get<ppu_thread>(thread_id);
2015-07-01 00:25:52 +02:00
if (!thread)
2015-03-04 22:51:14 +01:00
{
return CELL_ESRCH;
}
2015-04-12 03:36:25 +02:00
return CELL_OK;
}
error_code sys_ppu_thread_recover_page_fault(u32 thread_id)
{
sys_ppu_thread.warning("sys_ppu_thread_recover_page_fault(thread_id=0x%x)", thread_id);
const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread)
{
return CELL_ESRCH;
}
// We can only wake a thread if it is being suspended for a page fault.
auto pf_events = fxm::get_always<page_fault_event_entries>();
auto pf_event_ind = pf_events->events.begin();
for (auto event_ind = pf_events->events.begin(); event_ind != pf_events->events.end(); ++event_ind)
{
if (event_ind->thread_id == thread_id)
{
pf_event_ind = event_ind;
break;
}
}
if (pf_event_ind == pf_events->events.end())
{ // if not found...
return CELL_EINVAL;
}
pf_events->events.erase(pf_event_ind);
lv2_obj::awake(*thread);
return CELL_OK;
}
error_code sys_ppu_thread_get_page_fault_context(u32 thread_id, vm::ptr<sys_ppu_thread_icontext_t> ctxt)
{
sys_ppu_thread.todo("sys_ppu_thread_get_page_fault_context(thread_id=0x%x, ctxt=*0x%x)", thread_id, ctxt);
const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread)
{
return CELL_ESRCH;
}
// We can only get a context if the thread is being suspended for a page fault.
auto pf_events = fxm::get_always<page_fault_event_entries>();
bool found = false;
for (const auto& ev : pf_events->events)
{
if (ev.thread_id == thread_id)
{
found = true;
break;
}
}
if (!found)
{
return CELL_EINVAL;
}
// TODO: Fill ctxt with proper information.
return CELL_OK;
}