rpcsx/rpcs3/Emu/Cell/PPUThread.cpp

393 lines
7.2 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-08-23 02:16:54 +02:00
#include "rpcs3/Ini.h"
#include "Utilities/Log.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2015-07-01 00:25:52 +02:00
#include "Emu/IdManager.h"
#include "Emu/Cell/PPUThread.h"
#include "Emu/Cell/PPUDecoder.h"
#include "Emu/Cell/PPUInterpreter.h"
2015-03-16 22:38:21 +01:00
#include "Emu/Cell/PPUInterpreter2.h"
#include "Emu/Cell/PPULLVMRecompiler.h"
//#include "Emu/Cell/PPURecompiler.h"
#include "Utilities/VirtualMemory.h"
2015-03-16 22:38:21 +01:00
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/mman.h>
#include <sys/stat.h>
#endif
2015-01-08 23:17:26 +01:00
u64 rotate_mask[64][64];
2015-01-30 21:01:13 +01:00
extern u32 ppu_get_tls(u32 thread);
extern void ppu_free_tls(u32 thread);
2015-08-21 13:07:31 +02:00
//thread_local const std::weak_ptr<ppu_decoder_cache_t> g_tls_ppu_decoder_cache = fxm::get<ppu_decoder_cache_t>();
thread_local const ppu_decoder_cache_t* g_tls_ppu_decoder_cache = nullptr; // temporarily, because thread_local is not fully available
2015-03-16 22:38:21 +01:00
2015-08-10 21:39:52 +02:00
ppu_decoder_cache_t::ppu_decoder_cache_t()
: pointer(static_cast<decltype(pointer)>(memory_helper::reserve_memory(0x200000000)))
2015-08-10 21:39:52 +02:00
{
2015-03-16 22:38:21 +01:00
}
2015-08-10 21:39:52 +02:00
ppu_decoder_cache_t::~ppu_decoder_cache_t()
2015-03-16 22:38:21 +01:00
{
memory_helper::free_reserved_memory(pointer, 0x200000000);
2015-03-16 22:38:21 +01:00
}
2015-08-10 21:39:52 +02:00
void ppu_decoder_cache_t::initialize(u32 addr, u32 size)
2015-03-16 22:38:21 +01:00
{
memory_helper::commit_page_memory(pointer + addr / 4, size * 2);
2015-03-16 22:38:21 +01:00
PPUInterpreter2* inter;
PPUDecoder dec(inter = new PPUInterpreter2);
for (u32 pos = addr; pos < addr + size; pos += 4)
{
2015-03-17 21:03:24 +01:00
inter->func = ppu_interpreter::NULL_OP;
2015-03-16 22:38:21 +01:00
// decode PPU opcode
dec.Decode(vm::ps3::read32(pos));
2015-03-16 22:38:21 +01:00
2015-08-10 21:39:52 +02:00
// store function address
pointer[pos / 4] = inter->func;
2015-03-16 22:38:21 +01:00
}
}
2015-07-01 00:25:52 +02:00
PPUThread::PPUThread(const std::string& name)
2015-07-19 13:36:32 +02:00
: CPUThread(CPU_THREAD_PPU, name, WRAP_EXPR(fmt::format("PPU[0x%x] Thread (%s)[0x%08x]", m_id, m_name.c_str(), PC)))
{
2015-03-16 22:38:21 +01:00
InitRotateMask();
}
PPUThread::~PPUThread()
{
2015-07-09 17:30:37 +02:00
if (is_current())
{
detach();
}
else
{
join();
}
2015-07-01 00:25:52 +02:00
2015-07-19 13:36:32 +02:00
close_stack();
2015-07-01 00:25:52 +02:00
ppu_free_tls(m_id);
}
2015-07-19 13:36:32 +02:00
void PPUThread::dump_info() const
{
extern std::string get_ps3_function_name(u64 fid);
2015-07-03 18:07:36 +02:00
if (~hle_code < 1024)
2015-07-01 00:25:52 +02:00
{
LOG_SUCCESS(HLE, "Last syscall: %lld (%s)", ~hle_code, get_ps3_function_name(hle_code));
2015-07-01 00:25:52 +02:00
}
2015-07-08 17:01:59 +02:00
else if (hle_code)
2015-07-01 00:25:52 +02:00
{
LOG_SUCCESS(HLE, "Last function: %s (0x%llx)", get_ps3_function_name(hle_code), hle_code);
2015-07-01 00:25:52 +02:00
}
2015-07-19 13:36:32 +02:00
CPUThread::dump_info();
}
2015-07-19 13:36:32 +02:00
void PPUThread::init_regs()
{
2015-07-01 00:25:52 +02:00
GPR[1] = align(stack_addr + stack_size, 0x200) - 0x200;
GPR[13] = ppu_get_tls(m_id) + 0x7000; // 0x7000 is subtracted from r13 to access first TLS element
2015-02-17 16:27:15 +01:00
LR = 0;
CTR = PC;
CR.CR = 0x22000082;
VSCR.NJ = 1;
TB = 0;
2015-08-10 21:39:52 +02:00
//m_state |= CPU_STATE_INTR;
}
2015-07-19 13:36:32 +02:00
void PPUThread::init_stack()
{
2015-07-01 00:25:52 +02:00
if (!stack_addr)
{
2015-07-01 00:25:52 +02:00
if (!stack_size)
{
throw EXCEPTION("Invalid stack size");
}
2015-07-11 22:44:53 +02:00
stack_addr = vm::alloc(stack_size, vm::stack);
2015-07-01 00:25:52 +02:00
if (!stack_addr)
{
throw EXCEPTION("Out of stack memory");
}
}
}
2015-07-19 13:36:32 +02:00
void PPUThread::close_stack()
{
2015-07-01 00:25:52 +02:00
if (stack_addr)
{
vm::dealloc_verbose_nothrow(stack_addr, vm::stack);
2015-07-01 00:25:52 +02:00
stack_addr = 0;
}
}
2015-08-10 21:39:52 +02:00
bool PPUThread::handle_interrupt()
{
return false;
}
2015-07-19 13:36:32 +02:00
void PPUThread::do_run()
{
2015-07-01 00:25:52 +02:00
m_dec.reset();
2015-03-16 19:44:49 +01:00
2015-03-20 17:53:54 +01:00
switch (auto mode = Ini.CPUDecoderMode.GetValue())
{
2015-03-16 19:44:49 +01:00
case 0: // original interpreter
{
2015-07-01 00:25:52 +02:00
m_dec.reset(new PPUDecoder(new PPUInterpreter(*this)));
2015-03-16 19:44:49 +01:00
break;
}
case 1: // alternative interpreter
{
break;
}
2014-10-19 21:46:35 +02:00
case 2:
2015-07-01 00:25:52 +02:00
{
#ifdef PPU_LLVM_RECOMPILER
m_dec.reset(new ppu_recompiler_llvm::CPUHybridDecoderRecompiler(*this));
#else
LOG_ERROR(PPU, "This image does not include PPU JIT (LLVM)");
Emu.Pause();
#endif
2015-07-01 00:25:52 +02:00
break;
}
2015-07-01 00:25:52 +02:00
//case 3: m_dec.reset(new PPURecompiler(*this)); break;
default:
2015-03-20 17:53:54 +01:00
{
LOG_ERROR(PPU, "Invalid CPU decoder mode: %d", mode);
Emu.Pause();
}
2015-03-20 17:53:54 +01:00
}
}
bool FPRdouble::IsINF(PPCdouble d)
{
return ((u64&)d & 0x7FFFFFFFFFFFFFFFULL) == 0x7FF0000000000000ULL;
}
bool FPRdouble::IsNaN(PPCdouble d)
{
return std::isnan((double)d) ? 1 : 0;
}
bool FPRdouble::IsQNaN(PPCdouble d)
{
return
((u64&)d & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL &&
((u64&)d & 0x0007FFFFFFFFFFFULL) == 0ULL &&
((u64&)d & 0x000800000000000ULL) != 0ULL;
}
bool FPRdouble::IsSNaN(PPCdouble d)
{
return
((u64&)d & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL &&
((u64&)d & 0x000FFFFFFFFFFFFFULL) != 0ULL &&
((u64&)d & 0x0008000000000000ULL) == 0ULL;
}
int FPRdouble::Cmp(PPCdouble a, PPCdouble b)
{
if(a < b) return CR_LT;
if(a > b) return CR_GT;
if(a == b) return CR_EQ;
return CR_SO;
}
2014-08-15 14:50:59 +02:00
2015-07-19 13:36:32 +02:00
u64 PPUThread::get_stack_arg(s32 i)
2014-08-23 16:51:51 +02:00
{
return vm::ps3::read64(VM_CAST(GPR[1] + 0x70 + 0x8 * (i - 9)));
2014-08-23 16:51:51 +02:00
}
2015-07-19 13:36:32 +02:00
void PPUThread::fast_call(u32 addr, u32 rtoc)
{
2015-07-01 00:25:52 +02:00
if (!is_current())
{
throw EXCEPTION("Called from the wrong thread");
}
auto old_PC = PC;
2015-04-18 02:25:26 +02:00
auto old_stack = GPR[1];
auto old_rtoc = GPR[2];
2014-08-20 16:23:48 +02:00
auto old_LR = LR;
2015-07-19 13:36:32 +02:00
auto old_task = std::move(custom_task);
assert(!old_task || !custom_task);
PC = addr;
GPR[2] = rtoc;
LR = Emu.GetCPUThreadStop();
2015-07-19 13:36:32 +02:00
custom_task = nullptr;
2015-07-01 00:25:52 +02:00
try
{
2015-07-19 13:36:32 +02:00
task();
2015-07-01 00:25:52 +02:00
}
catch (CPUThreadReturn)
{
}
2015-07-01 19:09:26 +02:00
m_state &= ~CPU_STATE_RETURN;
2014-08-20 16:23:48 +02:00
PC = old_PC;
2015-04-18 03:35:58 +02:00
2015-07-01 00:25:52 +02:00
if (GPR[1] != old_stack) // GPR[1] shouldn't change
2015-04-18 03:35:58 +02:00
{
2015-07-01 00:25:52 +02:00
throw EXCEPTION("Stack inconsistency (addr=0x%x, rtoc=0x%x, SP=0x%llx, old=0x%llx)", addr, rtoc, GPR[1], old_stack);
2015-04-18 03:35:58 +02:00
}
GPR[2] = old_rtoc;
LR = old_LR;
2015-07-19 13:36:32 +02:00
custom_task = std::move(old_task);
}
2015-07-19 13:36:32 +02:00
void PPUThread::fast_stop()
2014-08-15 14:50:59 +02:00
{
2015-07-01 19:09:26 +02:00
m_state |= CPU_STATE_RETURN;
2014-09-24 20:44:26 +02:00
}
2015-07-19 13:36:32 +02:00
void PPUThread::task()
2014-09-24 20:44:26 +02:00
{
2015-03-17 21:03:24 +01:00
SetHostRoundingMode(FPSCR_RN_NEAR);
if (custom_task)
2014-09-24 20:44:26 +02:00
{
2015-07-19 13:36:32 +02:00
if (check_status()) return;
2015-07-01 00:25:52 +02:00
2015-03-16 22:38:21 +01:00
return custom_task(*this);
2014-09-24 20:44:26 +02:00
}
2015-03-16 22:38:21 +01:00
2015-08-21 13:07:31 +02:00
if (!g_tls_ppu_decoder_cache)
2015-08-10 21:39:52 +02:00
{
2015-08-21 13:07:31 +02:00
const auto decoder_cache = fxm::get<ppu_decoder_cache_t>();
if (!decoder_cache)
{
throw EXCEPTION("PPU Decoder Cache not initialized");
}
2015-08-10 21:39:52 +02:00
2015-08-21 13:07:31 +02:00
g_tls_ppu_decoder_cache = decoder_cache.get(); // unsafe (TODO)
}
const auto exec_map = g_tls_ppu_decoder_cache->pointer;
2015-08-10 21:39:52 +02:00
2015-03-16 22:38:21 +01:00
if (m_dec)
2014-09-24 20:44:26 +02:00
{
2015-07-01 00:25:52 +02:00
while (true)
{
if (m_state && check_status()) break;
2015-03-16 22:38:21 +01:00
2015-07-01 00:25:52 +02:00
// decode instruction using specified decoder
m_dec->DecodeMemory(PC);
2015-03-16 22:38:21 +01:00
2015-07-01 00:25:52 +02:00
// next instruction
PC += 4;
}
}
else
{
while (true)
2015-03-16 22:38:21 +01:00
{
2015-08-10 21:39:52 +02:00
// get cached interpreter function address
const auto func = exec_map[PC / 4];
2015-03-21 00:36:05 +01:00
2015-08-10 21:39:52 +02:00
// check status
if (!m_state)
2015-08-10 21:39:52 +02:00
{
// call interpreter function
func(*this, { vm::ps3::read32(PC) });
2015-03-16 22:38:21 +01:00
2015-08-10 21:39:52 +02:00
// next instruction
PC += 4;
2015-03-16 19:44:49 +01:00
2015-08-10 21:39:52 +02:00
continue;
}
if (check_status())
{
break;
}
2015-07-01 00:25:52 +02:00
}
2015-03-16 19:44:49 +01:00
}
2014-09-24 20:44:26 +02:00
}
2015-07-01 00:25:52 +02:00
ppu_thread::ppu_thread(u32 entry, const std::string& name, u32 stack_size, s32 prio)
{
auto ppu = idm::make_ptr<PPUThread>(name);
2015-07-01 00:25:52 +02:00
if (entry)
{
ppu->PC = vm::ps3::read32(entry);
ppu->GPR[2] = vm::ps3::read32(entry + 4); // rtoc
2015-07-01 00:25:52 +02:00
}
ppu->stack_size = stack_size ? stack_size : Emu.GetPrimaryStackSize();
ppu->prio = prio ? prio : Emu.GetPrimaryPrio();
2015-07-01 00:25:52 +02:00
thread = std::move(ppu);
argc = 0;
}
cpu_thread& ppu_thread::args(std::initializer_list<std::string> values)
{
if (!values.size())
return *this;
assert(argc == 0);
2015-06-21 02:17:42 +02:00
envp.set(vm::alloc(align(sizeof32(*envp), stack_align), vm::main));
*envp = 0;
2015-06-21 02:17:42 +02:00
argv.set(vm::alloc(sizeof32(*argv) * (u32)values.size(), vm::main));
for (auto &arg : values)
{
const u32 arg_size = align(u32(arg.size() + 1), stack_align);
const u32 arg_addr = vm::alloc(arg_size, vm::main);
std::memcpy(vm::get_ptr(arg_addr), arg.c_str(), arg.size() + 1);
argv[argc++] = arg_addr;
}
return *this;
}
cpu_thread& ppu_thread::run()
{
2015-07-19 13:36:32 +02:00
thread->run();
gpr(3, argc);
gpr(4, argv.addr());
gpr(5, envp.addr());
return *this;
}
ppu_thread& ppu_thread::gpr(uint index, u64 value)
{
assert(index < 32);
2015-03-04 22:51:14 +01:00
static_cast<PPUThread&>(*thread).GPR[index] = value;
return *this;
}