2019-02-17 16:53:38 +01:00
|
|
|
|
#include "stdafx.h"
|
2018-09-29 00:12:00 +02:00
|
|
|
|
#include "sys_ppu_thread.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"
|
2017-04-13 17:37:24 +02:00
|
|
|
|
#include "sys_event.h"
|
2019-11-01 20:21:15 +01:00
|
|
|
|
#include "sys_process.h"
|
2017-10-08 22:37:54 +02:00
|
|
|
|
#include "sys_mmapper.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
2018-08-25 14:39:00 +02:00
|
|
|
|
LOG_CHANNEL(sys_ppu_thread);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
|
void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode)
|
2012-11-15 00:39:56 +01:00
|
|
|
|
{
|
2017-03-11 00:14:48 +01:00
|
|
|
|
vm::temporary_unlock(ppu);
|
|
|
|
|
|
|
2019-06-11 21:45:11 +02:00
|
|
|
|
// Need to wait until the current writer finish
|
|
|
|
|
|
if (ppu.state & cpu_flag::memory) vm::g_mutex.lock_unlock();
|
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
|
sys_ppu_thread.trace("_sys_ppu_thread_exit(errorcode=0x%llx)", errorcode);
|
2015-02-11 21:11:49 +01:00
|
|
|
|
|
2016-08-09 16:14:41 +02:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2020-02-19 18:03:59 +01:00
|
|
|
|
else if (value != umax)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
// Joinable, joined
|
|
|
|
|
|
value = -2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Detached otherwise
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2020-02-19 18:03:59 +01:00
|
|
|
|
if (jid == umax)
|
2016-07-27 23:43:22 +02:00
|
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
|
// Detach detached thread, id will be removed on cleanup
|
|
|
|
|
|
static_cast<named_thread<ppu_thread>&>(ppu) = thread_state::detached;
|
2015-02-11 21:11:49 +01:00
|
|
|
|
}
|
2017-02-06 19:36:46 +01:00
|
|
|
|
else if (jid != 0)
|
|
|
|
|
|
{
|
2018-09-03 21:28:33 +02:00
|
|
|
|
std::lock_guard lock(id_manager::g_mutex);
|
2017-02-06 19:36:46 +01:00
|
|
|
|
|
|
|
|
|
|
// Schedule joiner and unqueue
|
2020-01-01 18:38:05 +01:00
|
|
|
|
lv2_obj::awake(idm::check_unlocked<named_thread<ppu_thread>>(jid));
|
2017-02-06 19:36:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Unqueue
|
2017-02-22 11:10:55 +01:00
|
|
|
|
lv2_obj::sleep(ppu);
|
2017-02-06 19:36:46 +01:00
|
|
|
|
|
|
|
|
|
|
// Remove suspend state (TODO)
|
|
|
|
|
|
ppu.state -= cpu_flag::suspend;
|
2012-11-15 00:39:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
|
void sys_ppu_thread_yield(ppu_thread& ppu)
|
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
|
|
|
|
|
2019-04-25 16:27:50 +02:00
|
|
|
|
lv2_obj::yield(ppu);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
error_code sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr<u64> vptr)
|
2012-11-15 00:39:56 +01:00
|
|
|
|
{
|
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);
|
2013-07-12 14:42:17 +02:00
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::get<named_thread<ppu_thread>>(thread_id, [&](ppu_thread& thread) -> CellError
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
CellError result = thread.joiner.atomic_op([&](u32& value) -> CellError
|
|
|
|
|
|
{
|
2020-02-19 18:03:59 +01:00
|
|
|
|
if (value == 0u - 3)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
value = -2;
|
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-19 18:03:59 +01:00
|
|
|
|
if (value == 0u - 2)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: check precedence?
|
|
|
|
|
|
if (&ppu == &thread)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_EDEADLK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
value = ppu.id;
|
|
|
|
|
|
return {};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
2017-02-22 11:10:55 +01:00
|
|
|
|
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;
|
|
|
|
|
|
});
|
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;
|
|
|
|
|
|
}
|
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;
|
2014-03-07 13:03:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
|
// Wait for cleanup
|
2018-10-11 00:17:19 +02:00
|
|
|
|
(*thread.ptr)();
|
2017-01-30 14:20:09 +01:00
|
|
|
|
|
2019-02-09 17:58:54 +01:00
|
|
|
|
if (ppu.test_stopped())
|
2017-01-30 14:20:09 +01:00
|
|
|
|
{
|
2019-02-09 17:58:54 +01:00
|
|
|
|
return 0;
|
2017-01-30 14:20:09 +01:00
|
|
|
|
}
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
// Cleanup
|
2018-10-11 00:17:19 +02:00
|
|
|
|
idm::remove<named_thread<ppu_thread>>(thread->id);
|
2019-02-09 17:58:54 +01:00
|
|
|
|
|
|
|
|
|
|
if (!vptr)
|
|
|
|
|
|
{
|
2019-12-20 08:17:22 +01:00
|
|
|
|
return not_an_error(CELL_EFAULT);
|
2019-02-09 17:58:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the exit status from the register
|
|
|
|
|
|
*vptr = thread->gpr[3];
|
2012-11-15 00:39:56 +01:00
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
error_code sys_ppu_thread_detach(u32 thread_id)
|
2012-11-15 00:39:56 +01:00
|
|
|
|
{
|
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
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::check<named_thread<ppu_thread>>(thread_id, [&](ppu_thread& thread) -> CellError
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
return thread.joiner.atomic_op([&](u32& value) -> CellError
|
|
|
|
|
|
{
|
2020-02-19 18:03:59 +01:00
|
|
|
|
if (value == 0u - 3)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
value = -2;
|
|
|
|
|
|
return CELL_EAGAIN;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-19 18:03:59 +01:00
|
|
|
|
if (value == 0u - 2)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-19 18:03:59 +01:00
|
|
|
|
if (value == umax)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
value = -1;
|
|
|
|
|
|
return {};
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
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
|
|
|
|
|
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
|
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
|
idm::remove<named_thread<ppu_thread>>(thread_id);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
}
|
2018-02-09 15:49:37 +01:00
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-25 08:56:17 +01:00
|
|
|
|
error_code sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ptr<s32> isjoinable)
|
2012-11-15 00:39:56 +01:00
|
|
|
|
{
|
2017-01-30 14:20:09 +01:00
|
|
|
|
sys_ppu_thread.trace("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
2019-01-25 08:56:17 +01:00
|
|
|
|
if (!isjoinable)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_EFAULT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-19 18:03:59 +01:00
|
|
|
|
*isjoinable = ppu.joiner != umax;
|
2019-01-25 08:56:17 +01:00
|
|
|
|
return CELL_OK;
|
2012-11-15 00:39:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
|
error_code sys_ppu_thread_set_priority(ppu_thread& ppu, 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
|
|
|
|
|
2019-11-01 20:21:15 +01:00
|
|
|
|
if (prio < (g_ps3_process_info.debug_or_root() ? -512 : 0) || prio > 3071)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::check<named_thread<ppu_thread>>(thread_id, [&](ppu_thread& thread)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
2018-10-18 22:02:03 +02:00
|
|
|
|
if (thread.prio != prio)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
2019-11-01 20:21:15 +01:00
|
|
|
|
lv2_obj::set_priority(thread, prio);
|
2017-02-06 19:36:46 +01: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
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
error_code 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
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::check<named_thread<ppu_thread>>(thread_id, [&](ppu_thread& thread)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
*priop = thread.prio;
|
|
|
|
|
|
});
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
|
|
|
|
|
if (!thread)
|
2015-04-12 03:36:25 +02:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
error_code sys_ppu_thread_get_stack_information(ppu_thread& 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
error_code 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
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::get<named_thread<ppu_thread>>(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
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
error_code 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
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::get<named_thread<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
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
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 _stacksz, u64 flags, vm::cptr<char> threadname)
|
2012-11-15 00:39:56 +01:00
|
|
|
|
{
|
2020-01-23 20:32:06 +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)",
|
2018-10-11 00:17:19 +02:00
|
|
|
|
thread_id, param, arg, unk, prio, _stacksz, flags, threadname);
|
2014-06-22 12:59:28 +02:00
|
|
|
|
|
2018-04-06 22:57:33 +02:00
|
|
|
|
// thread_id is checked for null in stub -> CELL_ENOMEM
|
|
|
|
|
|
// unk is set to 0 in sys_ppu_thread_create stub
|
|
|
|
|
|
|
|
|
|
|
|
if (!param || !param->entry)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_EFAULT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-01 20:21:15 +01:00
|
|
|
|
if (prio < (g_ps3_process_info.debug_or_root() ? -512 : 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
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
|
if ((flags & 3) == 3) // Check two flags: joinable + interrupt not allowed
|
2015-03-04 22:51:14 +01:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_EPERM;
|
2014-06-22 12:59:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
// Compute actual stack size and allocate
|
2019-07-27 06:51:29 +02:00
|
|
|
|
const u32 stack_size = ::align<u32>(std::max<u32>(_stacksz, 4096), 4096);
|
2018-10-11 00:17:19 +02:00
|
|
|
|
|
2018-10-20 20:13:25 +02:00
|
|
|
|
const vm::addr_t stack_base{vm::alloc(stack_size, vm::stack, 4096)};
|
2018-10-11 00:17:19 +02:00
|
|
|
|
|
|
|
|
|
|
if (!stack_base)
|
2016-07-27 23:43:22 +02:00
|
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
|
return CELL_ENOMEM;
|
|
|
|
|
|
}
|
2017-01-30 14:20:09 +01:00
|
|
|
|
|
2020-01-23 20:32:06 +01:00
|
|
|
|
std::string ppu_name;
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const u32 tid = idm::import<named_thread<ppu_thread>>([&]()
|
|
|
|
|
|
{
|
|
|
|
|
|
const u32 tid = idm::last_id();
|
2017-01-30 14:20:09 +01:00
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
std::string full_name = fmt::format("PPU[0x%x] Thread", tid);
|
2017-01-30 14:20:09 +01:00
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
if (threadname)
|
2017-01-30 14:20:09 +01:00
|
|
|
|
{
|
2020-01-17 15:01:55 +01:00
|
|
|
|
constexpr u32 max_size = 27; // max size including null terminator
|
|
|
|
|
|
const auto pname = threadname.get_ptr();
|
|
|
|
|
|
ppu_name.assign(pname, std::find(pname, pname + max_size, '\0'));
|
2020-01-23 20:32:06 +01:00
|
|
|
|
|
|
|
|
|
|
if (!ppu_name.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
fmt::append(full_name, " (%s)", ppu_name);
|
|
|
|
|
|
}
|
2017-01-30 14:20:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
ppu_thread_params p;
|
|
|
|
|
|
p.stack_addr = stack_base;
|
|
|
|
|
|
p.stack_size = stack_size;
|
|
|
|
|
|
p.tls_addr = param->tls;
|
|
|
|
|
|
p.entry = param->entry;
|
|
|
|
|
|
p.arg0 = arg;
|
|
|
|
|
|
p.arg1 = unk;
|
|
|
|
|
|
|
|
|
|
|
|
return std::make_shared<named_thread<ppu_thread>>(full_name, p, ppu_name, prio, 1 - static_cast<int>(flags & 3));
|
2017-01-30 14:20:09 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!tid)
|
2016-07-27 23:43:22 +02:00
|
|
|
|
{
|
2019-04-30 08:53:46 +02:00
|
|
|
|
vm::dealloc(stack_base);
|
2017-01-30 14:20:09 +01:00
|
|
|
|
return CELL_EAGAIN;
|
2016-07-27 23:43:22 +02:00
|
|
|
|
}
|
2015-01-28 13:59:16 +01:00
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
*thread_id = tid;
|
2020-01-23 20:32:06 +01:00
|
|
|
|
sys_ppu_thread.warning(u8"_sys_ppu_thread_create(): Thread “%s” created (id=0x%x)", ppu_name, tid);
|
2015-04-12 03:36:25 +02:00
|
|
|
|
return CELL_OK;
|
2013-07-06 01:49:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
|
error_code sys_ppu_thread_start(ppu_thread& ppu, u32 thread_id)
|
2013-07-06 01:49:38 +02:00
|
|
|
|
{
|
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
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::get<named_thread<ppu_thread>>(thread_id, [&](ppu_thread& thread)
|
2017-02-06 19:36:46 +01:00
|
|
|
|
{
|
2019-11-01 20:21:15 +01:00
|
|
|
|
lv2_obj::awake(&thread);
|
2017-02-06 19:36:46 +01:00
|
|
|
|
});
|
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
|
|
|
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
|
thread_ctrl::notify(*thread);
|
2017-04-13 17:37:24 +02:00
|
|
|
|
|
|
|
|
|
|
// Dirty hack for sound: confirm the creation of _mxr000 event queue
|
2018-10-11 00:17:19 +02:00
|
|
|
|
if (thread->ppu_name.get() == "_cellsurMixerMain"sv)
|
2017-04-13 17:37:24 +02:00
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}))
|
|
|
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
|
if (ppu.is_stopped())
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-13 17:37:24 +02:00
|
|
|
|
thread_ctrl::wait_for(50000);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
if (ppu.test_stopped())
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2017-04-13 17:37:24 +02:00
|
|
|
|
}
|
2017-02-06 19:36:46 +01:00
|
|
|
|
}
|
2013-07-06 01:49:38 +02:00
|
|
|
|
|
2014-08-26 01:45:15 +02:00
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-30 14:20:09 +01:00
|
|
|
|
error_code sys_ppu_thread_rename(u32 thread_id, vm::cptr<char> name)
|
2014-08-26 01:45:15 +02:00
|
|
|
|
{
|
2020-01-23 20:32:06 +01:00
|
|
|
|
sys_ppu_thread.warning("sys_ppu_thread_rename(thread_id=0x%x, name=*0x%x)", thread_id, name);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::get<named_thread<ppu_thread>>(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
|
|
|
|
|
2018-04-06 22:57:33 +02:00
|
|
|
|
if (!name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_EFAULT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-17 15:01:55 +01:00
|
|
|
|
constexpr u32 max_size = 27; // max size including null terminator
|
|
|
|
|
|
const auto pname = name.get_ptr();
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
// thread_ctrl name is not changed (TODO)
|
2020-01-23 20:32:06 +01:00
|
|
|
|
const std::string res = thread->ppu_name.assign(pname, std::find(pname, pname + max_size, '\0'));
|
|
|
|
|
|
sys_ppu_thread.warning(u8"sys_ppu_thread_rename(): Thread renamed to “%s”", res);
|
2013-07-06 01:49:38 +02:00
|
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
}
|
2017-10-08 22:37:54 +02:00
|
|
|
|
|
|
|
|
|
|
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);
|
2018-10-11 00:17:19 +02:00
|
|
|
|
|
|
|
|
|
|
const auto thread = idm::get<named_thread<ppu_thread>>(thread_id);
|
|
|
|
|
|
|
2017-10-08 22:37:54 +02:00
|
|
|
|
if (!thread)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-01 18:27:08 +02:00
|
|
|
|
return mmapper_thread_recover_page_fault(thread_id);
|
2017-10-08 22:37:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
|
const auto thread = idm::get<named_thread<ppu_thread>>(thread_id);
|
|
|
|
|
|
|
2017-10-08 22:37:54 +02:00
|
|
|
|
if (!thread)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We can only get a context if the thread is being suspended for a page fault.
|
2019-08-21 19:30:58 +02:00
|
|
|
|
auto pf_events = g_fxo->get<page_fault_event_entries>();
|
2019-02-17 16:53:38 +01:00
|
|
|
|
std::shared_lock lock(pf_events->pf_mutex);
|
2017-10-08 22:37:54 +02:00
|
|
|
|
|
2019-02-17 16:53:38 +01:00
|
|
|
|
const auto evt = pf_events->events.find(thread_id);
|
|
|
|
|
|
if (evt == pf_events->events.end())
|
2017-10-08 22:37:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Fill ctxt with proper information.
|
|
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|