2013-11-03 20:23:16 +01:00
|
|
|
#include "stdafx.h"
|
2014-08-23 02:16:54 +02:00
|
|
|
#include "rpcs3/Ini.h"
|
2014-06-17 17:44:03 +02:00
|
|
|
#include "Utilities/Log.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-07-12 09:02:39 +02:00
|
|
|
#include "Emu/DbgCommand.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
|
2014-08-23 22:40:04 +02:00
|
|
|
#include "CPUDecoder.h"
|
2013-11-03 20:23:16 +01:00
|
|
|
#include "CPUThread.h"
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
CPUThread::CPUThread(CPUThreadType type, const std::string& name, std::function<std::string()> thread_name)
|
2015-07-01 19:09:26 +02:00
|
|
|
: m_state({ CPU_STATE_STOPPED })
|
2015-08-05 17:30:32 +02:00
|
|
|
, m_id(idm::get_current_id())
|
2013-11-03 20:23:16 +01:00
|
|
|
, m_type(type)
|
2015-07-01 00:25:52 +02:00
|
|
|
, m_name(name)
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-04 21:23:10 +02:00
|
|
|
start(std::move(thread_name), [this]
|
2015-02-18 17:22:06 +01:00
|
|
|
{
|
2015-07-01 19:09:26 +02:00
|
|
|
SendDbgCommand(DID_CREATE_THREAD, this);
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
|
|
// check thread status
|
2015-07-19 13:36:32 +02:00
|
|
|
while (joinable() && is_alive())
|
2015-02-18 17:22:06 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
CHECK_EMU_STATUS;
|
|
|
|
|
|
|
|
|
|
// check stop status
|
2015-07-19 13:36:32 +02:00
|
|
|
if (!is_stopped())
|
2015-02-18 17:22:06 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
if (lock) lock.unlock();
|
|
|
|
|
|
|
|
|
|
try
|
2015-02-18 17:22:06 +01:00
|
|
|
{
|
2015-07-19 13:36:32 +02:00
|
|
|
task();
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
2015-07-01 00:25:52 +02:00
|
|
|
catch (CPUThreadReturn)
|
2015-02-18 17:22:06 +01:00
|
|
|
{
|
2015-07-08 17:01:59 +02:00
|
|
|
;
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
2015-07-01 00:25:52 +02:00
|
|
|
catch (CPUThreadStop)
|
|
|
|
|
{
|
2015-07-01 19:09:26 +02:00
|
|
|
m_state |= CPU_STATE_STOPPED;
|
2015-07-01 00:25:52 +02:00
|
|
|
}
|
|
|
|
|
catch (CPUThreadExit)
|
|
|
|
|
{
|
|
|
|
|
m_state |= CPU_STATE_DEAD;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-07-08 17:01:59 +02:00
|
|
|
catch (const fmt::exception&)
|
|
|
|
|
{
|
2015-07-19 13:36:32 +02:00
|
|
|
dump_info();
|
2015-07-08 17:01:59 +02:00
|
|
|
throw;
|
|
|
|
|
}
|
2015-02-18 17:22:06 +01:00
|
|
|
|
2015-07-01 19:09:26 +02:00
|
|
|
m_state &= ~CPU_STATE_RETURN;
|
2015-07-01 00:25:52 +02:00
|
|
|
continue;
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-08 17:01:59 +02:00
|
|
|
if (!lock)
|
|
|
|
|
{
|
|
|
|
|
lock.lock();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-02-18 17:22:06 +01:00
|
|
|
|
2015-07-06 01:21:15 +02:00
|
|
|
cv.wait(lock);
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
2015-07-01 00:25:52 +02:00
|
|
|
});
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
CPUThread::~CPUThread()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
if (joinable())
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
throw EXCEPTION("Thread not joined");
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
SendDbgCommand(DID_REMOVE_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
bool CPUThread::is_paused() const
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-01 19:09:26 +02:00
|
|
|
return (m_state.load() & CPU_STATE_PAUSED) != 0 || Emu.IsPaused();
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::dump_info() const
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-16 18:31:58 +02:00
|
|
|
if (!Emu.IsStopped())
|
|
|
|
|
{
|
|
|
|
|
LOG_NOTICE(GENERAL, RegsToString());
|
|
|
|
|
}
|
2013-11-05 20:22:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::run()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2014-05-02 08:30:32 +02:00
|
|
|
SendDbgCommand(DID_START_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
init_stack();
|
|
|
|
|
init_regs();
|
|
|
|
|
do_run();
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2014-05-02 08:30:32 +02:00
|
|
|
SendDbgCommand(DID_STARTED_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::pause()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-06 21:35:34 +02:00
|
|
|
SendDbgCommand(DID_PAUSE_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
m_state |= CPU_STATE_PAUSED;
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
SendDbgCommand(DID_PAUSED_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::resume()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-06 21:35:34 +02:00
|
|
|
SendDbgCommand(DID_RESUME_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
{
|
|
|
|
|
// lock for reliable notification
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
m_state &= ~CPU_STATE_PAUSED;
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
cv.notify_one();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SendDbgCommand(DID_RESUMED_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::stop()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2014-05-02 08:30:32 +02:00
|
|
|
SendDbgCommand(DID_STOP_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
if (is_current())
|
2014-01-31 19:40:18 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
throw CPUThreadStop{};
|
2014-09-16 19:46:22 +02:00
|
|
|
}
|
2015-07-01 00:25:52 +02:00
|
|
|
else
|
|
|
|
|
{
|
2015-07-06 21:35:34 +02:00
|
|
|
// lock for reliable notification
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
2015-07-01 19:09:26 +02:00
|
|
|
m_state |= CPU_STATE_STOPPED;
|
2014-01-31 19:40:18 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
cv.notify_one();
|
|
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2014-05-02 08:30:32 +02:00
|
|
|
SendDbgCommand(DID_STOPED_THREAD, this);
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::exec()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2014-05-02 08:30:32 +02:00
|
|
|
SendDbgCommand(DID_EXEC_THREAD, this);
|
2014-01-31 19:40:18 +01:00
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
{
|
|
|
|
|
// lock for reliable notification
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2015-07-01 00:25:52 +02:00
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
m_state &= ~CPU_STATE_STOPPED;
|
|
|
|
|
|
|
|
|
|
cv.notify_one();
|
|
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::exit()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
if (is_current())
|
|
|
|
|
{
|
|
|
|
|
throw CPUThreadExit{};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw EXCEPTION("Unable to exit another thread");
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-02 08:30:32 +02:00
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::step()
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2015-07-08 00:33:24 +02:00
|
|
|
if (m_state.atomic_op([](u64& state) -> bool
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2015-07-08 00:33:24 +02:00
|
|
|
const bool was_paused = (state & CPU_STATE_PAUSED) != 0;
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
state |= CPU_STATE_STEP;
|
2015-07-01 19:09:26 +02:00
|
|
|
state &= ~CPU_STATE_PAUSED;
|
2015-07-08 00:33:24 +02:00
|
|
|
|
|
|
|
|
return was_paused;
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
if (is_current()) return;
|
|
|
|
|
|
|
|
|
|
// lock for reliable notification (only if PAUSE was removed)
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
|
|
cv.notify_one();
|
|
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::sleep()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2015-07-03 18:07:36 +02:00
|
|
|
m_state += CPU_STATE_MAX;
|
|
|
|
|
m_state |= CPU_STATE_SLEEP;
|
2015-07-01 00:25:52 +02:00
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
void CPUThread::awake()
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2015-07-06 21:35:34 +02:00
|
|
|
// must be called after the balanced Sleep() call
|
2015-07-03 18:07:36 +02:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
if (m_state.atomic_op([](u64& state) -> bool
|
2015-07-03 18:07:36 +02:00
|
|
|
{
|
|
|
|
|
if (state < CPU_STATE_MAX)
|
|
|
|
|
{
|
|
|
|
|
throw EXCEPTION("Sleep()/Awake() inconsistency");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((state -= CPU_STATE_MAX) < CPU_STATE_MAX)
|
|
|
|
|
{
|
|
|
|
|
state &= ~CPU_STATE_SLEEP;
|
2015-07-08 00:33:24 +02:00
|
|
|
|
|
|
|
|
// notify the condition variable as well
|
|
|
|
|
return true;
|
2015-07-03 18:07:36 +02:00
|
|
|
}
|
2014-07-07 19:22:36 +02:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
return false;
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
if (is_current()) return;
|
|
|
|
|
|
|
|
|
|
// lock for reliable notification; the condition being checked is probably externally set
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2015-07-06 21:35:34 +02:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
cv.notify_one();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
bool CPUThread::signal()
|
2015-07-08 00:33:24 +02:00
|
|
|
{
|
|
|
|
|
// try to set SIGNAL
|
|
|
|
|
if (m_state._or(CPU_STATE_SIGNAL) & CPU_STATE_SIGNAL)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// not truly responsible for signal delivery, requires additional measures like LV2_LOCK
|
|
|
|
|
cv.notify_one();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
bool CPUThread::unsignal()
|
2015-07-08 00:33:24 +02:00
|
|
|
{
|
|
|
|
|
// remove SIGNAL and return its old value
|
|
|
|
|
return (m_state._and_not(CPU_STATE_SIGNAL) & CPU_STATE_SIGNAL) != 0;
|
2015-07-01 00:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
bool CPUThread::check_status()
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> lock(mutex, std::defer_lock);
|
2014-07-13 20:55:14 +02:00
|
|
|
|
2015-02-18 17:22:06 +01:00
|
|
|
while (true)
|
2014-07-07 19:22:36 +02:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
CHECK_EMU_STATUS; // check at least once
|
2015-02-18 17:22:06 +01:00
|
|
|
|
2015-08-10 21:39:52 +02:00
|
|
|
if (!is_paused() && (m_state.load() & CPU_STATE_INTR) == 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
if (!lock)
|
|
|
|
|
{
|
|
|
|
|
lock.lock();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-08-10 21:39:52 +02:00
|
|
|
if (!is_paused() && (m_state.load() & CPU_STATE_INTR) != 0 && handle_interrupt())
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 21:35:34 +02:00
|
|
|
cv.wait(lock);
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 13:36:32 +02:00
|
|
|
if (m_state.load() & CPU_STATE_RETURN || is_stopped())
|
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
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
if (m_state.load() & CPU_STATE_STEP)
|
|
|
|
|
{
|
|
|
|
|
// set PAUSE, but allow to execute once
|
2015-07-01 19:09:26 +02:00
|
|
|
m_state |= CPU_STATE_PAUSED;
|
2015-07-01 00:25:52 +02:00
|
|
|
m_state &= ~CPU_STATE_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
|
|
|
}
|