2013-11-03 20:23:16 +01:00
|
|
|
#include "stdafx.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
#include "Emu/System.h"
|
2017-03-11 00:14:48 +01:00
|
|
|
#include "Emu/Memory/vm.h"
|
2013-11-03 20:23:16 +01:00
|
|
|
#include "CPUThread.h"
|
2017-04-02 20:10:06 +02:00
|
|
|
#include "Emu/IdManager.h"
|
|
|
|
|
#include "Utilities/GDBDebugServer.h"
|
2018-10-11 00:17:19 +02:00
|
|
|
#include "Emu/Cell/PPUThread.h"
|
|
|
|
|
#include "Emu/Cell/SPUThread.h"
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2017-02-09 23:51:29 +01:00
|
|
|
DECLARE(cpu_thread::g_threads_created){0};
|
|
|
|
|
DECLARE(cpu_thread::g_threads_deleted){0};
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2017-03-11 00:14:48 +01:00
|
|
|
template <>
|
2016-08-09 16:14:41 +02:00
|
|
|
void fmt_class_string<cpu_flag>::format(std::string& out, u64 arg)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-09 16:14:41 +02:00
|
|
|
format_enum(out, arg, [](cpu_flag f)
|
2016-08-07 21:01:27 +02:00
|
|
|
{
|
|
|
|
|
switch (f)
|
|
|
|
|
{
|
2017-02-09 23:51:29 +01:00
|
|
|
case cpu_flag::stop: return "STOP";
|
|
|
|
|
case cpu_flag::exit: return "EXIT";
|
|
|
|
|
case cpu_flag::suspend: return "s";
|
|
|
|
|
case cpu_flag::ret: return "ret";
|
|
|
|
|
case cpu_flag::signal: return "sig";
|
2017-03-11 00:14:48 +01:00
|
|
|
case cpu_flag::memory: return "mem";
|
2017-02-22 11:10:55 +01:00
|
|
|
case cpu_flag::dbg_global_pause: return "G-PAUSE";
|
|
|
|
|
case cpu_flag::dbg_global_stop: return "G-EXIT";
|
2017-02-09 23:51:29 +01:00
|
|
|
case cpu_flag::dbg_pause: return "PAUSE";
|
|
|
|
|
case cpu_flag::dbg_step: return "STEP";
|
2016-08-09 16:14:41 +02:00
|
|
|
case cpu_flag::__bitset_enum_max: break;
|
2016-08-07 21:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unknown;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
2016-08-09 16:14:41 +02:00
|
|
|
void fmt_class_string<bs_t<cpu_flag>>::format(std::string& out, u64 arg)
|
2016-08-07 21:01:27 +02:00
|
|
|
{
|
2016-08-09 16:14:41 +02:00
|
|
|
format_bitset(out, arg, "[", "|", "]", &fmt_class_string<cpu_flag>::format);
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
thread_local cpu_thread* g_tls_current_cpu_thread = nullptr;
|
2015-09-26 22:46:04 +02:00
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
void cpu_thread::operator()()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2016-08-09 16:14:41 +02:00
|
|
|
state -= cpu_flag::exit;
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
g_tls_current_cpu_thread = this;
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
if (g_cfg.core.thread_scheduler_enabled)
|
|
|
|
|
{
|
|
|
|
|
thread_ctrl::set_thread_affinity_mask(thread_ctrl::get_affinity_mask(id_type() == 1 ? thread_class::ppu : thread_class::spu));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (g_cfg.core.lower_spu_priority && id_type() == 2)
|
|
|
|
|
{
|
|
|
|
|
thread_ctrl::set_native_priority(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
// Check thread status
|
2018-09-02 19:22:35 +02:00
|
|
|
while (!(state & (cpu_flag::exit + cpu_flag::dbg_global_stop)))
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
2017-02-05 15:06:03 +01:00
|
|
|
// Check stop status
|
2018-09-02 19:22:35 +02:00
|
|
|
if (!(state & cpu_flag::stop))
|
2015-02-18 17:22:06 +01:00
|
|
|
{
|
2015-11-26 09:06:29 +01:00
|
|
|
try
|
2015-02-18 17:22:06 +01:00
|
|
|
{
|
2015-11-26 09:06:29 +01:00
|
|
|
cpu_task();
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
2016-08-09 16:14:41 +02:00
|
|
|
catch (cpu_flag _s)
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
2016-04-14 00:59:00 +02:00
|
|
|
state += _s;
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
2018-10-11 00:17:19 +02:00
|
|
|
catch (const std::exception& e)
|
2015-07-08 17:01:59 +02:00
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
LOG_FATAL(GENERAL, "%s thrown: %s", typeid(e).name(), e.what());
|
2016-04-14 00:59:00 +02:00
|
|
|
LOG_NOTICE(GENERAL, "\n%s", dump());
|
2018-10-11 00:17:19 +02:00
|
|
|
Emu.Pause();
|
|
|
|
|
break;
|
2015-07-08 17:01:59 +02:00
|
|
|
}
|
2015-02-18 17:22:06 +01:00
|
|
|
|
2016-08-09 16:14:41 +02:00
|
|
|
state -= cpu_flag::ret;
|
2015-11-26 09:06:29 +01:00
|
|
|
continue;
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
thread_ctrl::wait();
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
void cpu_thread::on_abort()
|
2016-04-25 12:49:12 +02:00
|
|
|
{
|
2016-08-09 16:14:41 +02:00
|
|
|
state += cpu_flag::exit;
|
2016-04-25 12:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cpu_thread::~cpu_thread()
|
|
|
|
|
{
|
2017-03-11 00:14:48 +01:00
|
|
|
vm::cleanup_unlock(*this);
|
2017-02-09 23:51:29 +01:00
|
|
|
g_threads_deleted++;
|
2016-04-25 12:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-25 18:50:30 +01:00
|
|
|
cpu_thread::cpu_thread(u32 id)
|
|
|
|
|
: id(id)
|
2016-04-25 12:49:12 +02:00
|
|
|
{
|
2017-02-09 23:51:29 +01:00
|
|
|
g_threads_created++;
|
2016-04-25 12:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
bool cpu_thread::check_state()
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2017-04-02 20:10:06 +02:00
|
|
|
#ifdef WITH_GDB_DEBUGGER
|
2018-09-02 19:22:35 +02:00
|
|
|
if (state & cpu_flag::dbg_pause)
|
|
|
|
|
{
|
2018-02-28 16:31:39 +01:00
|
|
|
fxm::get<GDBDebugServer>()->pause_from(this);
|
2017-04-02 20:10:06 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
bool cpu_sleep_called = false;
|
2017-03-11 00:14:48 +01:00
|
|
|
bool cpu_flag_memory = false;
|
2017-02-06 19:36:46 +01:00
|
|
|
|
2015-02-18 17:22:06 +01:00
|
|
|
while (true)
|
2014-07-07 19:22:36 +02:00
|
|
|
{
|
2018-09-02 19:22:35 +02:00
|
|
|
if (state & cpu_flag::memory && state.test_and_reset(cpu_flag::memory))
|
2017-03-11 00:14:48 +01:00
|
|
|
{
|
|
|
|
|
cpu_flag_memory = true;
|
|
|
|
|
|
|
|
|
|
if (auto& ptr = vm::g_tls_locked)
|
|
|
|
|
{
|
|
|
|
|
ptr->compare_and_swap(this, nullptr);
|
|
|
|
|
ptr = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-02 19:22:35 +02:00
|
|
|
if (state & cpu_flag::exit + cpu_flag::dbg_global_stop)
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-02 19:22:35 +02:00
|
|
|
if (state & cpu_flag::signal && state.test_and_reset(cpu_flag::signal))
|
2017-02-06 19:36:46 +01:00
|
|
|
{
|
|
|
|
|
cpu_sleep_called = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
if (!is_paused())
|
2015-08-10 21:39:52 +02:00
|
|
|
{
|
2018-04-03 16:19:07 +02:00
|
|
|
if (cpu_flag_memory)
|
|
|
|
|
{
|
|
|
|
|
cpu_mem();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 21:39:52 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2018-09-02 19:22:35 +02:00
|
|
|
else if (!cpu_sleep_called && state & cpu_flag::suspend)
|
2017-02-06 19:36:46 +01:00
|
|
|
{
|
|
|
|
|
cpu_sleep();
|
|
|
|
|
cpu_sleep_called = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
thread_ctrl::wait();
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
const auto state_ = state.load();
|
|
|
|
|
|
2018-09-02 19:22:35 +02:00
|
|
|
if (state_ & (cpu_flag::ret + cpu_flag::stop))
|
2014-10-07 23:37:04 +02:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-10-07 23:37:04 +02:00
|
|
|
|
2018-09-02 19:22:35 +02:00
|
|
|
if (state_ & cpu_flag::dbg_step)
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2016-08-09 16:14:41 +02:00
|
|
|
state += cpu_flag::dbg_pause;
|
|
|
|
|
state -= cpu_flag::dbg_step;
|
2014-10-07 23:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
return false;
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
2016-07-27 23:43:22 +02:00
|
|
|
|
2018-10-11 00:17:19 +02:00
|
|
|
void cpu_thread::notify()
|
2017-02-22 11:10:55 +01:00
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
if (id_type() == 1)
|
2017-02-22 11:10:55 +01:00
|
|
|
{
|
2018-10-11 00:17:19 +02:00
|
|
|
thread_ctrl::notify(*static_cast<named_thread<ppu_thread>*>(this));
|
|
|
|
|
}
|
|
|
|
|
else if (id_type() == 2)
|
|
|
|
|
{
|
|
|
|
|
thread_ctrl::notify(*static_cast<named_thread<spu_thread>*>(this));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("Invalid cpu_thread type");
|
2017-02-22 11:10:55 +01:00
|
|
|
}
|
2016-07-27 23:43:22 +02:00
|
|
|
}
|
2016-08-13 16:58:19 +02:00
|
|
|
|
|
|
|
|
std::string cpu_thread::dump() const
|
|
|
|
|
{
|
|
|
|
|
return fmt::format("Type: %s\n" "State: %s\n", typeid(*this).name(), state.load());
|
|
|
|
|
}
|