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

262 lines
5.4 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
#include "Emu/SysCalls/SysCalls.h"
#include "Emu/SysCalls/CB_FUNC.h"
2014-08-23 16:51:51 +02:00
2014-08-26 01:55:37 +02:00
#include "Emu/CPU/CPUThreadManager.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
#include "sys_ppu_thread.h"
2015-03-02 22:09:20 +01:00
SysCallBase sys_ppu_thread("sys_ppu_thread");
2015-04-12 03:36:25 +02:00
void _sys_ppu_thread_exit(PPUThread& CPU, u64 errorcode)
{
2015-04-17 21:46:06 +02:00
sys_ppu_thread.Log("_sys_ppu_thread_exit(errorcode=0x%llx)", errorcode);
2015-04-12 03:36:25 +02:00
CPU.SetExitStatus(errorcode);
CPU.Stop();
if (!CPU.IsJoinable())
{
const u32 id = CPU.GetId();
CallAfter([id]()
{
Emu.GetCPU().RemoveThread(id);
});
}
}
2015-04-12 03:36:25 +02:00
void sys_ppu_thread_yield()
{
2014-08-23 16:51:51 +02:00
sys_ppu_thread.Log("sys_ppu_thread_yield()");
2014-07-12 09:02:39 +02:00
// Note: Or do we actually want to yield?
2014-12-23 00:31:11 +01:00
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_join(u32 thread_id, vm::ptr<u64> vptr)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Warning("sys_ppu_thread_join(thread_id=0x%x, vptr=*0x%x)", thread_id, vptr);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id);
2015-04-12 03:36:25 +02:00
if (!t)
{
return CELL_ESRCH;
}
while (t->IsAlive())
{
if (Emu.IsStopped())
{
2014-08-23 16:51:51 +02:00
sys_ppu_thread.Warning("sys_ppu_thread_join(%d) aborted", thread_id);
return CELL_OK;
}
2014-12-23 00:31:11 +01:00
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack
}
2015-04-12 03:36:25 +02:00
*vptr = t->GetExitStatus();
Emu.GetCPU().RemoveThread(thread_id);
return CELL_OK;
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_detach(u32 thread_id)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Warning("sys_ppu_thread_detach(thread_id=0x%x)", thread_id);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id);
2015-04-12 03:36:25 +02:00
if (!t)
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
2015-04-12 03:36:25 +02:00
if (!t->IsJoinable())
{
return CELL_EINVAL;
2015-04-12 03:36:25 +02:00
}
t->SetJoinable(false);
return CELL_OK;
}
void sys_ppu_thread_get_join_state(PPUThread& CPU, vm::ptr<s32> isjoinable)
{
2015-04-12 03:36:25 +02:00
sys_ppu_thread.Warning("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable);
*isjoinable = CPU.IsJoinable();
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Log("sys_ppu_thread_set_priority(thread_id=0x%x, prio=%d)", thread_id, prio);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id);
2015-04-12 03:36:25 +02:00
if (!t)
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
2015-04-12 03:36:25 +02:00
t->SetPrio(prio);
return CELL_OK;
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ptr<s32> priop)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Log("sys_ppu_thread_get_priority(thread_id=0x%x, priop=*0x%x)", thread_id, priop);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id);
2015-04-12 03:36:25 +02:00
if (!t)
{
return CELL_ESRCH;
}
*priop = static_cast<s32>(t->GetPrio());
return CELL_OK;
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_get_stack_information(PPUThread& CPU, vm::ptr<sys_ppu_thread_stack_t> sp)
{
2015-04-12 03:36:25 +02:00
sys_ppu_thread.Log("sys_ppu_thread_get_stack_information(sp=*0x%x)", sp);
2015-04-12 03:36:25 +02:00
sp->pst_addr = CPU.GetStackAddr();
sp->pst_size = CPU.GetStackSize();
return CELL_OK;
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_stop(u32 thread_id)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Error("sys_ppu_thread_stop(thread_id=0x%x)", thread_id);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id);
2015-04-12 03:36:25 +02:00
if (!t)
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
2015-04-12 03:36:25 +02:00
t->Stop();
return CELL_OK;
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_restart(u32 thread_id)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Error("sys_ppu_thread_restart(thread_id=0x%x)", thread_id);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id);
2015-04-12 03:36:25 +02:00
if (!t)
{
2015-02-20 14:55:00 +01:00
return CELL_ESRCH;
2015-04-12 03:36:25 +02:00
}
2015-04-12 03:36:25 +02:00
t->Stop();
t->Run();
return CELL_OK;
}
2015-03-04 22:51:14 +01:00
u32 ppu_thread_create(u32 entry, u64 arg, s32 prio, u32 stacksize, bool is_joinable, bool is_interrupt, std::string name, std::function<void(PPUThread&)> task)
2014-09-19 13:27:51 +02:00
{
2015-04-12 03:36:25 +02:00
const auto new_thread = Emu.GetCPU().AddThread(CPU_THREAD_PPU);
2015-03-04 22:51:14 +01:00
auto& ppu = static_cast<PPUThread&>(*new_thread);
2014-09-19 13:27:51 +02:00
2015-03-04 22:51:14 +01:00
ppu.SetEntry(entry);
ppu.SetPrio(prio);
ppu.SetStackSize(stacksize < 0x4000 ? 0x4000 : stacksize); // (hack) adjust minimal stack size
ppu.SetJoinable(is_joinable);
ppu.SetName(name);
ppu.custom_task = task;
ppu.Run();
2014-09-19 13:27:51 +02:00
if (!is_interrupt)
{
2015-03-04 22:51:14 +01:00
ppu.GPR[3] = arg;
ppu.Exec();
2014-09-19 13:27:51 +02:00
}
2015-03-04 22:51:14 +01:00
return ppu.GetId();
2014-09-19 13:27:51 +02:00
}
2015-04-12 03:36:25 +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::ptr<const char> threadname)
{
2015-04-12 03:36:25 +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=*0x%x)",
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;
}
2015-04-12 03:36:25 +02:00
const bool is_joinable = flags & SYS_PPU_THREAD_CREATE_JOINABLE;
const bool is_interrupt = flags & SYS_PPU_THREAD_CREATE_INTERRUPT;
2015-03-04 22:51:14 +01:00
if (is_joinable && is_interrupt)
{
return CELL_EPERM;
}
2015-04-12 03:36:25 +02:00
const auto new_thread = Emu.GetCPU().AddThread(CPU_THREAD_PPU);
2015-04-12 03:36:25 +02:00
auto& ppu = static_cast<PPUThread&>(*new_thread);
2015-03-09 20:56:55 +01:00
2015-04-12 03:36:25 +02:00
ppu.SetEntry(param->entry);
ppu.SetPrio(prio);
ppu.SetStackSize(stacksize < 0x4000 ? 0x4000 : stacksize); // (hack) adjust minimal stack size
ppu.SetJoinable(is_joinable);
2015-04-17 21:46:06 +02:00
ppu.SetName(threadname ? threadname.get_ptr() : "");
2015-04-12 03:36:25 +02:00
ppu.Run();
2015-04-12 03:36:25 +02:00
ppu.GPR[3] = arg;
ppu.GPR[4] = unk; // actually unknown
2015-01-28 13:59:16 +01:00
2015-04-12 03:36:25 +02:00
if (u32 tls = param->tls) // hack
{
2015-04-12 03:36:25 +02:00
ppu.GPR[13] = tls;
}
2015-04-12 03:36:25 +02:00
*thread_id = ppu.GetId();
return CELL_OK;
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_start(u32 thread_id)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Warning("sys_ppu_thread_start(thread_id=0x%x)", thread_id);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id, CPU_THREAD_PPU);
if (!t)
{
return CELL_ESRCH;
}
t->Exec();
return CELL_OK;
}
2015-04-12 03:36:25 +02:00
s32 sys_ppu_thread_rename(u32 thread_id, vm::ptr<const char> name)
{
2015-04-14 04:00:31 +02:00
sys_ppu_thread.Error("sys_ppu_thread_rename(thread_id=0x%x, name=*0x%x)", thread_id, name);
2015-04-12 03:36:25 +02:00
const auto t = Emu.GetCPU().GetThread(thread_id, CPU_THREAD_PPU);
2015-03-04 22:51:14 +01:00
if (!t)
{
return CELL_ESRCH;
}
t->SetThreadName(name.get_ptr());
2015-04-12 03:36:25 +02:00
return CELL_OK;
}