2012-11-15 00:39:56 +01:00
|
|
|
#include "stdafx.h"
|
2016-04-14 00:23:53 +02:00
|
|
|
#include "Utilities/Config.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
#include "Emu/Memory/Memory.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"
|
2014-06-25 00:38:34 +02:00
|
|
|
#include "sys_ppu_thread.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
LOG_CHANNEL(sys_ppu_thread);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
void _sys_ppu_thread_exit(PPUThread& ppu, u64 errorcode)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.trace("_sys_ppu_thread_exit(errorcode=0x%llx)", errorcode);
|
2015-04-12 03:36:25 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
2015-02-11 21:11:49 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// TODO: Should we really unlock mutexes?
|
|
|
|
|
|
|
|
|
|
//// get all sys_mutex objects
|
|
|
|
|
//for (auto& mutex : idm::get_all<lv2_mutex_t>())
|
|
|
|
|
//{
|
|
|
|
|
// // unlock mutex if locked by this thread
|
|
|
|
|
// if (mutex->owner.get() == &ppu)
|
|
|
|
|
// {
|
|
|
|
|
// mutex->unlock(lv2_lock);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2015-07-08 00:33:24 +02:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
ppu.state += cpu_state::exit;
|
|
|
|
|
|
|
|
|
|
// Delete detached thread
|
2015-07-08 00:33:24 +02:00
|
|
|
if (!ppu.is_joinable)
|
|
|
|
|
{
|
2016-04-14 00:23:53 +02:00
|
|
|
idm::remove<PPUThread>(ppu.id);
|
2015-02-11 21:11:49 +01:00
|
|
|
}
|
2015-07-01 00:25:52 +02:00
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
// Throw if this syscall was not called directly by the SC instruction (hack)
|
|
|
|
|
if (ppu.GPR[11] != 41 || ppu.custom_task)
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
2016-04-14 00:23:53 +02:00
|
|
|
throw cpu_state::exit;
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
void sys_ppu_thread_yield()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.trace("sys_ppu_thread_yield()");
|
2015-07-03 18:07:36 +02:00
|
|
|
|
|
|
|
|
std::this_thread::yield();
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
s32 sys_ppu_thread_join(PPUThread& ppu, u32 thread_id, vm::ptr<u64> vptr)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.warning("sys_ppu_thread_join(thread_id=0x%x, vptr=*0x%x)", thread_id, vptr);
|
2013-07-12 14:42:17 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(thread_id);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
if (!thread)
|
2015-04-12 03:36:25 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
if (!thread->is_joinable || thread->is_joining)
|
2014-03-07 13:03:42 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
if (&ppu == thread.get())
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
|
|
|
|
return CELL_EDEADLK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mark joining
|
|
|
|
|
thread->is_joining = true;
|
|
|
|
|
|
|
|
|
|
// join thread
|
2016-04-14 00:23:53 +02:00
|
|
|
while (!(thread->state & cpu_state::exit))
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
|
|
|
|
CHECK_EMU_STATUS;
|
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
get_current_thread_cv().wait_for(lv2_lock, std::chrono::milliseconds(1));
|
2014-03-07 13:03:42 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
// get exit status from the register
|
2016-04-14 00:23:53 +02:00
|
|
|
if (vptr) *vptr = thread->GPR[3];
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
|
|
|
// cleanup
|
2016-04-14 00:23:53 +02:00
|
|
|
idm::remove<PPUThread>(thread->id);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
s32 sys_ppu_thread_detach(u32 thread_id)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.warning("sys_ppu_thread_detach(thread_id=0x%x)", thread_id);
|
2015-04-12 03:36:25 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(thread_id);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
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
|
|
|
}
|
2013-11-09 02:05:58 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
if (!thread->is_joinable)
|
2015-04-12 03:36:25 +02:00
|
|
|
{
|
2013-11-09 02:05:58 +01:00
|
|
|
return CELL_EINVAL;
|
2015-04-12 03:36:25 +02:00
|
|
|
}
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
|
|
|
if (thread->is_joining)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// "detach"
|
|
|
|
|
thread->is_joinable = false;
|
2013-11-09 02:05:58 +01:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
void sys_ppu_thread_get_join_state(PPUThread& ppu, vm::ptr<s32> isjoinable)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.warning("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable);
|
2014-09-16 19:46:22 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
*isjoinable = ppu.is_joinable;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
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
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(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
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
if (prio < 0 || prio > 3071)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thread->prio = prio;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ptr<s32> priop)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.trace("sys_ppu_thread_get_priority(thread_id=0x%x, priop=*0x%x)", thread_id, priop);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(thread_id);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
|
|
|
if (!thread)
|
2015-04-12 03:36:25 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
*priop = thread->prio;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
s32 sys_ppu_thread_get_stack_information(PPUThread& ppu, vm::ptr<sys_ppu_thread_stack_t> sp)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.trace("sys_ppu_thread_get_stack_information(sp=*0x%x)", sp);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
sp->pst_addr = ppu.stack_addr;
|
|
|
|
|
sp->pst_size = ppu.stack_size;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
s32 sys_ppu_thread_stop(u32 thread_id)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.todo("sys_ppu_thread_stop(thread_id=0x%x)", thread_id);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
|
|
|
LV2_LOCK;
|
2015-04-12 03:36:25 +02:00
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(thread_id);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
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
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
//t->Stop();
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
s32 sys_ppu_thread_restart(u32 thread_id)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.todo("sys_ppu_thread_restart(thread_id=0x%x)", thread_id);
|
2015-04-12 03:36:25 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(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
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
//t->Stop();
|
|
|
|
|
//t->Run();
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-26 22:46:04 +02:00
|
|
|
u32 ppu_thread_create(u32 entry, u64 arg, s32 prio, u32 stacksize, const std::string& name, std::function<void(PPUThread&)> task)
|
2014-09-19 13:27:51 +02:00
|
|
|
{
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto ppu = idm::make_ptr<PPUThread>(name);
|
2015-02-18 16:56:46 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
ppu->prio = prio;
|
2015-09-26 22:46:04 +02:00
|
|
|
ppu->stack_size = stacksize;
|
|
|
|
|
ppu->custom_task = std::move(task);
|
2016-04-14 00:23:53 +02:00
|
|
|
ppu->cpu_init();
|
2014-09-19 13:27:51 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
if (entry)
|
|
|
|
|
{
|
|
|
|
|
ppu->PC = vm::read32(entry);
|
|
|
|
|
ppu->GPR[2] = vm::read32(entry + 4); // rtoc
|
|
|
|
|
}
|
2014-09-19 13:27:51 +02:00
|
|
|
|
2015-09-26 22:46:04 +02:00
|
|
|
ppu->GPR[3] = arg;
|
2016-04-14 00:23:53 +02:00
|
|
|
ppu->state -= cpu_state::stop;
|
2016-04-25 12:49:12 +02:00
|
|
|
ppu->lock_notify();
|
2014-09-19 13:27:51 +02:00
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
return ppu->id;
|
2014-09-19 13:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-22 00:27:58 +02:00
|
|
|
s32 _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)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01: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=*0x%x)",
|
2015-04-12 03:36:25 +02:00
|
|
|
thread_id, param, arg, unk, prio, stacksize, flags, threadname);
|
2014-06-22 12:59:28 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-03-04 22:51:14 +01:00
|
|
|
if (prio < 0 || prio > 3071)
|
2014-06-22 12:59:28 +02:00
|
|
|
{
|
2015-03-04 22:51:14 +01:00
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
2014-11-19 15:16:30 +01:00
|
|
|
|
2015-07-08 17:01:59 +02:00
|
|
|
const bool is_joinable = (flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0;
|
|
|
|
|
const bool is_interrupt = (flags & SYS_PPU_THREAD_CREATE_INTERRUPT) != 0;
|
2014-11-19 15:16:30 +01:00
|
|
|
|
2015-03-04 22:51:14 +01:00
|
|
|
if (is_joinable && is_interrupt)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EPERM;
|
2014-06-22 12:59:28 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto ppu = idm::make_ptr<PPUThread>(threadname ? threadname.get_ptr() : "");
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
ppu->prio = prio;
|
2015-09-26 22:46:04 +02:00
|
|
|
ppu->stack_size = std::max<u32>(stacksize, 0x4000);
|
2016-04-14 00:23:53 +02:00
|
|
|
ppu->cpu_init();
|
2015-03-09 20:56:55 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
ppu->PC = vm::read32(param->entry);
|
|
|
|
|
ppu->GPR[2] = vm::read32(param->entry + 4); // rtoc
|
|
|
|
|
ppu->GPR[3] = arg;
|
|
|
|
|
ppu->GPR[4] = unk; // actually unknown
|
2016-04-14 00:23:53 +02:00
|
|
|
ppu->GPR[13] = param->tls;
|
2013-07-06 01:49:38 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
ppu->is_joinable = is_joinable;
|
2015-01-28 13:59:16 +01:00
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
*thread_id = ppu->id;
|
2015-04-12 03:36:25 +02:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2013-07-06 01:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
s32 sys_ppu_thread_start(u32 thread_id)
|
2013-07-06 01:49:38 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.warning("sys_ppu_thread_start(thread_id=0x%x)", thread_id);
|
2015-04-12 03:36:25 +02:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
LV2_LOCK;
|
2015-04-12 03:36:25 +02:00
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(thread_id);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
|
|
|
if (!thread)
|
2015-04-12 03:36:25 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
thread->state -= cpu_state::stop;
|
2016-04-25 12:49:12 +02:00
|
|
|
thread->lock_notify();
|
2013-07-06 01:49:38 +02:00
|
|
|
|
2014-08-26 01:45:15 +02:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-22 00:27:58 +02:00
|
|
|
s32 sys_ppu_thread_rename(u32 thread_id, vm::cptr<char> name)
|
2014-08-26 01:45:15 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_ppu_thread.todo("sys_ppu_thread_rename(thread_id=0x%x, name=*0x%x)", thread_id, name);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
|
|
|
LV2_LOCK;
|
2014-08-26 01:45:15 +02:00
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto thread = idm::get<PPUThread>(thread_id);
|
2014-08-26 01:45:15 +02:00
|
|
|
|
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
|
|
|
|
2013-07-06 01:49:38 +02:00
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|