Savestates Support For PS3 Emulation (#10478)

This commit is contained in:
Elad Ashkenazi 2022-07-04 16:02:17 +03:00 committed by GitHub
parent 969b9eb89d
commit fcd297ffb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
154 changed files with 4948 additions and 635 deletions

View file

@ -1,14 +1,72 @@
#include "stdafx.h"
#include "sys_cond.h"
#include "util/serialization.hpp"
#include "Emu/IdManager.h"
#include "Emu/IPC.h"
#include "Emu/System.h"
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
LOG_CHANNEL(sys_cond);
lv2_cond::lv2_cond(utils::serial& ar)
: key(ar)
, name(ar)
, mtx_id(ar)
, mutex(idm::get_unlocked<lv2_obj, lv2_mutex>(mtx_id)) // May be nullptr
{
}
CellError lv2_cond::on_id_create()
{
exists++;
static auto do_it = [](lv2_cond* _this) -> CellError
{
if (lv2_obj::check(_this->mutex))
{
_this->mutex->cond_count++;
return {};
}
// Mutex has been destroyed, cannot create conditional variable
return CELL_ESRCH;
};
if (mutex)
{
return do_it(this);
}
ensure(!!Emu.DeserialManager());
Emu.DeferDeserialization([this]()
{
if (!mutex)
{
mutex = ensure(idm::get_unlocked<lv2_obj, lv2_mutex>(mtx_id));
}
// Defer function
ensure(CellError{} == do_it(this));
});
return {};
}
std::shared_ptr<void> lv2_cond::load(utils::serial& ar)
{
auto cond = std::make_shared<lv2_cond>(ar);
return lv2_obj::load(cond->key, cond);
}
void lv2_cond::save(utils::serial& ar)
{
ar(key, name, mtx_id);
}
error_code sys_cond_create(ppu_thread& ppu, vm::ptr<u32> cond_id, u32 mutex_id, vm::ptr<sys_cond_attribute_t> attr)
{
ppu.state += cpu_flag::wait;
@ -81,7 +139,7 @@ error_code sys_cond_signal(ppu_thread& ppu, u32 cond_id)
sys_cond.trace("sys_cond_signal(cond_id=0x%x)", cond_id);
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [](lv2_cond& cond)
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond)
{
if (cond.waiters)
{
@ -89,6 +147,12 @@ error_code sys_cond_signal(ppu_thread& ppu, u32 cond_id)
if (const auto cpu = cond.schedule<ppu_thread>(cond.sq, cond.mutex->protocol))
{
if (static_cast<ppu_thread*>(cpu)->state & cpu_flag::again)
{
ppu.state += cpu_flag::again;
return;
}
// TODO: Is EBUSY returned after reqeueing, on sys_cond_destroy?
cond.waiters--;
@ -114,12 +178,21 @@ error_code sys_cond_signal_all(ppu_thread& ppu, u32 cond_id)
sys_cond.trace("sys_cond_signal_all(cond_id=0x%x)", cond_id);
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [](lv2_cond& cond)
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond)
{
if (cond.waiters)
{
std::lock_guard lock(cond.mutex->mutex);
for (auto cpu : cond.sq)
{
if (static_cast<ppu_thread*>(cpu)->state & cpu_flag::again)
{
ppu.state += cpu_flag::again;
return;
}
}
cpu_thread* result = nullptr;
cond.waiters -= ::size32(cond.sq);
@ -167,6 +240,12 @@ error_code sys_cond_signal_to(ppu_thread& ppu, u32 cond_id, u32 thread_id)
{
if (cpu->id == thread_id)
{
if (static_cast<ppu_thread*>(cpu)->state & cpu_flag::again)
{
ppu.state += cpu_flag::again;
return 0;
}
ensure(cond.unqueue(cond.sq, cpu));
cond.waiters--;
@ -208,19 +287,33 @@ error_code sys_cond_wait(ppu_thread& ppu, u32 cond_id, u64 timeout)
const auto cond = idm::get<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond) -> s64
{
if (cond.mutex->owner >> 1 != ppu.id)
if (!ppu.loaded_from_savestate && cond.mutex->owner >> 1 != ppu.id)
{
return -1;
}
std::lock_guard lock(cond.mutex->mutex);
// Register waiter
cond.sq.emplace_back(&ppu);
cond.waiters++;
if (ppu.loaded_from_savestate && ppu.optional_syscall_state & 1)
{
// Mutex sleep
ensure(!cond.mutex->try_own(ppu, ppu.id));
}
else
{
// Register waiter
cond.sq.emplace_back(&ppu);
cond.waiters++;
}
if (ppu.loaded_from_savestate)
{
cond.sleep(ppu, timeout);
return static_cast<u32>(ppu.optional_syscall_state >> 32);
}
// Unlock the mutex
const auto count = cond.mutex->lock_count.exchange(0);
const u32 count = cond.mutex->lock_count.exchange(0);
if (const auto cpu = cond.mutex->reown<ppu_thread>())
{
@ -246,16 +339,28 @@ error_code sys_cond_wait(ppu_thread& ppu, u32 cond_id, u64 timeout)
while (auto state = ppu.state.fetch_sub(cpu_flag::signal))
{
if (is_stopped(state))
{
return {};
}
if (state & cpu_flag::signal)
{
break;
}
if (is_stopped(state))
{
std::lock_guard lock(cond->mutex->mutex);
const bool cond_sleep = std::find(cond->sq.begin(), cond->sq.end(), &ppu) != cond->sq.end();
const bool mutex_sleep = std::find(cond->mutex->sq.begin(), cond->mutex->sq.end(), &ppu) != cond->mutex->sq.end();
if (!cond_sleep && !mutex_sleep)
{
break;
}
ppu.optional_syscall_state = u32{mutex_sleep} | (u64{static_cast<u32>(cond.ret)} << 32);
ppu.state += cpu_flag::again;
return {};
}
if (timeout)
{
if (lv2_obj::wait_timeout(timeout, &ppu))
@ -263,7 +368,7 @@ error_code sys_cond_wait(ppu_thread& ppu, u32 cond_id, u64 timeout)
// Wait for rescheduling
if (ppu.check_state())
{
return {};
continue;
}
std::lock_guard lock(cond->mutex->mutex);