Remove dead #if 0'd code in math.h On amd64, page_size == 4096 constant, on amd64 w/ win32, allocation_granularity == 65536. These values for x86 windows havent changed over the last 20 years so this is probably safe and gives a modest code size reduction Enable XE_USE_KUSER_SHARED. This sources host time from KUSER_SHARED instead of from QueryPerformanceCounter, which is far faster, but only has a granularity of 100 nanoseconds. In some games seemingly random crashes were happening that were hard to trace because the faulting thread was actually not the one that was misbehaving, another threads stack was underflowing into the faulting thread. Added a bunch of code to synchronize the guest stack and host stack so that if a guest longjmps the host's stack will be adjusted. Changes were also made to allow the guest to call into a piece of an existing x64 function. This synchronization might have a slight performance impact on lower end cpus, to disable it set enable_host_guest_stack_synchronization to false. It is possible it may have introduced regressions, but i dont know of any yet So far, i know the synchronization change fixes the "hub crash" in super sonic and allows the game "london 2012" to go ingame. Removed emit_useless_fpscr_updates, not emitting these updates breaks the raiden game MapGuestAddressToMachineCode now returns nullptr if no address was found, instead of the start of the function add Processor::LookupModule Add Backend::DeinitializeBackendContext Use WriteRegisterRangeFromRing_WithKnownBound<0, 0xFFFF> in WriteRegisterRangeFromRing for inlining (previously regressed on performance of ExecutePacketType0) add notes about flags that trap in XamInputGetCapabilities 0 == 3 in XamInputGetCapabilities Name arg 2 of XamInputSetState PrefetchW in critical section kernel funcs if available & doing cmpxchg Add terminated field to X_KTHREAD, set it on termination Expanded the logic of NtResumeThread/NtSuspendThread to include checking the type of the handle (in release, LookupObject doesnt seem to do anything with the type) and returning X_STATUS_OBJECT_TYPE_MISMATCH if invalid. Do termination check in NtSuspendThread. Add basic host exception messagebox, need to flesh it out more (maybe use the new stack tracking stuff if on guest thrd?) Add rdrand patching hack, mostly affects users with nvidia cards who have many threads on zen Use page_size_shift in more places Once again disable precompilation! Raiden is mostly weird ppc asm which probably breaks the precompilation. The code is still useful for running the compiler over the whole of an xex in debug to test for issues "Fix" debug console, we were checking the cvar before any cvars were loaded, and the condition it checks in AttachConsole is somehow always false Remove dead #if 0'd code in math.h On amd64, page_size == 4096 constant, on amd64 w/ win32, allocation_granularity == 65536. These values for x86 windows havent changed over the last 20 years so this is probably safe and gives a modest code size reduction Enable XE_USE_KUSER_SHARED. This sources host time from KUSER_SHARED instead of from QueryPerformanceCounter, which is far faster, but only has a granularity of 100 nanoseconds. In some games seemingly random crashes were happening that were hard to trace because the faulting thread was actually not the one that was misbehaving, another threads stack was underflowing into the faulting thread. Added a bunch of code to synchronize the guest stack and host stack so that if a guest longjmps the host's stack will be adjusted. Changes were also made to allow the guest to call into a piece of an existing x64 function. This synchronization might have a slight performance impact on lower end cpus, to disable it set enable_host_guest_stack_synchronization to false. It is possible it may have introduced regressions, but i dont know of any yet So far, i know the synchronization change fixes the "hub crash" in super sonic and allows the game "london 2012" to go ingame. Removed emit_useless_fpscr_updates, not emitting these updates breaks the raiden game MapGuestAddressToMachineCode now returns nullptr if no address was found, instead of the start of the function add Processor::LookupModule Add Backend::DeinitializeBackendContext Use WriteRegisterRangeFromRing_WithKnownBound<0, 0xFFFF> in WriteRegisterRangeFromRing for inlining (previously regressed on performance of ExecutePacketType0) add notes about flags that trap in XamInputGetCapabilities 0 == 3 in XamInputGetCapabilities Name arg 2 of XamInputSetState PrefetchW in critical section kernel funcs if available & doing cmpxchg Add terminated field to X_KTHREAD, set it on termination Expanded the logic of NtResumeThread/NtSuspendThread to include checking the type of the handle (in release, LookupObject doesnt seem to do anything with the type) and returning X_STATUS_OBJECT_TYPE_MISMATCH if invalid. Do termination check in NtSuspendThread. Add basic host exception messagebox, need to flesh it out more (maybe use the new stack tracking stuff if on guest thrd?) Add rdrand patching hack, mostly affects users with nvidia cards who have many threads on zen Use page_size_shift in more places Once again disable precompilation! Raiden is mostly weird ppc asm which probably breaks the precompilation. The code is still useful for running the compiler over the whole of an xex in debug to test for issues
125 lines
4.2 KiB
C++
125 lines
4.2 KiB
C++
/**
|
|
******************************************************************************
|
|
* Xenia : Xbox 360 Emulator Research Project *
|
|
******************************************************************************
|
|
* Copyright 2013 Ben Vanik. All rights reserved. *
|
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
|
******************************************************************************
|
|
*/
|
|
|
|
#include "xenia/cpu/thread_state.h"
|
|
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
#include "xenia/base/assert.h"
|
|
#include "xenia/base/logging.h"
|
|
#include "xenia/base/threading.h"
|
|
#include "xenia/cpu/processor.h"
|
|
|
|
#include "xenia/xbox.h"
|
|
namespace xe {
|
|
namespace cpu {
|
|
|
|
thread_local ThreadState* thread_state_ = nullptr;
|
|
|
|
static void* AllocateContext() {
|
|
size_t granularity = xe::memory::allocation_granularity();
|
|
for (unsigned pos32 = 0x40; pos32 < 8192; ++pos32) {
|
|
/*
|
|
we want our register which points to the context to have 0xE0000000 in
|
|
the low 32 bits, for checking for whether we need the 4k offset, but also
|
|
if we allocate starting from the page before we allow backends to index
|
|
negatively to get to their own backend specific data, which makes full
|
|
use of int8 displacement
|
|
|
|
|
|
the downside is we waste most of one granula and probably a fair bit of
|
|
the one starting at 0xE0 by using a direct virtual memory allocation
|
|
instead of malloc
|
|
*/
|
|
uintptr_t context_pre =
|
|
((static_cast<uint64_t>(pos32) << 32) | 0xE0000000) - granularity;
|
|
|
|
void* p = memory::AllocFixed(
|
|
(void*)context_pre, granularity + sizeof(ppc::PPCContext),
|
|
memory::AllocationType::kReserveCommit, memory::PageAccess::kReadWrite);
|
|
if (p) {
|
|
return reinterpret_cast<char*>(p) +
|
|
granularity; // now we have a ctx ptr with the e0 constant in low,
|
|
// and one page allocated before it
|
|
}
|
|
}
|
|
|
|
assert_always("giving up on allocating context, likely leaking contexts");
|
|
return nullptr;
|
|
}
|
|
|
|
static void FreeContext(void* ctx) {
|
|
char* true_start_of_ctx = &reinterpret_cast<char*>(
|
|
ctx)[-static_cast<ptrdiff_t>(xe::memory::allocation_granularity())];
|
|
memory::DeallocFixed(true_start_of_ctx, 0,
|
|
memory::DeallocationType::kRelease);
|
|
}
|
|
|
|
ThreadState::ThreadState(Processor* processor, uint32_t thread_id,
|
|
uint32_t stack_base, uint32_t pcr_address)
|
|
: processor_(processor),
|
|
memory_(processor->memory()),
|
|
thread_id_(thread_id) {
|
|
if (thread_id_ == UINT_MAX) {
|
|
// System thread. Assign the system thread ID with a high bit
|
|
// set so people know what's up.
|
|
uint32_t system_thread_handle = xe::threading::current_thread_system_id();
|
|
thread_id_ = 0x80000000 | system_thread_handle;
|
|
}
|
|
backend_data_ = processor->backend()->AllocThreadData();
|
|
|
|
// Allocate with 64b alignment.
|
|
|
|
context_ = reinterpret_cast<ppc::PPCContext*>(
|
|
AllocateContext());
|
|
processor->backend()->InitializeBackendContext(context_);
|
|
assert_true(((uint64_t)context_ & 0x3F) == 0);
|
|
std::memset(context_, 0, sizeof(ppc::PPCContext));
|
|
|
|
// Stash pointers to common structures that callbacks may need.
|
|
context_->global_mutex = &xe::global_critical_region::mutex();
|
|
context_->virtual_membase = memory_->virtual_membase();
|
|
context_->physical_membase = memory_->physical_membase();
|
|
context_->processor = processor_;
|
|
context_->thread_state = this;
|
|
context_->thread_id = thread_id_;
|
|
|
|
// Set initial registers.
|
|
context_->r[1] = stack_base;
|
|
context_->r[13] = pcr_address;
|
|
// fixme: VSCR must be set here!
|
|
}
|
|
|
|
ThreadState::~ThreadState() {
|
|
if (backend_data_) {
|
|
processor_->backend()->FreeThreadData(backend_data_);
|
|
}
|
|
if (thread_state_ == this) {
|
|
thread_state_ = nullptr;
|
|
}
|
|
if (context_) {
|
|
processor_->backend()->DeinitializeBackendContext(context_);
|
|
FreeContext(reinterpret_cast<void*>(context_));
|
|
}
|
|
}
|
|
|
|
void ThreadState::Bind(ThreadState* thread_state) {
|
|
thread_state_ = thread_state;
|
|
}
|
|
|
|
ThreadState* ThreadState::Get() { return thread_state_; }
|
|
|
|
uint32_t ThreadState::GetThreadID() {
|
|
return thread_state_ ? thread_state_->thread_id_ : 0xFFFFFFFF;
|
|
}
|
|
|
|
} // namespace cpu
|
|
} // namespace xe
|