2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-12-29 02:55:19 +01:00
|
|
|
|
2019-05-18 03:43:26 +02:00
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
2023-01-13 22:06:13 +01:00
|
|
|
|
2019-05-18 03:43:26 +02:00
|
|
|
#include "common/bit_field.h"
|
2018-12-29 02:55:19 +01:00
|
|
|
#include "common/common_types.h"
|
2023-01-13 22:06:13 +01:00
|
|
|
#include "common/demangle.h"
|
2018-12-29 02:55:19 +01:00
|
|
|
#include "common/logging/log.h"
|
2018-12-31 02:46:27 +01:00
|
|
|
#include "core/arm/arm_interface.h"
|
2022-04-09 05:53:42 +02:00
|
|
|
#include "core/arm/symbols.h"
|
2019-05-18 03:43:26 +02:00
|
|
|
#include "core/core.h"
|
2022-05-31 01:35:01 +02:00
|
|
|
#include "core/debugger/debugger.h"
|
2022-04-09 05:53:42 +02:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2023-06-13 03:34:25 +02:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2022-05-31 20:37:37 +02:00
|
|
|
#include "core/hle/kernel/svc.h"
|
2019-05-26 17:40:41 +02:00
|
|
|
#include "core/loader/loader.h"
|
2018-12-29 02:55:19 +01:00
|
|
|
#include "core/memory.h"
|
|
|
|
|
|
|
|
namespace Core {
|
2019-05-18 03:43:26 +02:00
|
|
|
|
|
|
|
constexpr u64 SEGMENT_BASE = 0x7100000000ull;
|
|
|
|
|
2020-03-20 19:05:47 +01:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
|
2022-04-21 02:17:48 +02:00
|
|
|
Core::System& system, const ARM_Interface::ThreadContext32& ctx) {
|
2023-06-13 03:34:25 +02:00
|
|
|
std::vector<BacktraceEntry> out;
|
|
|
|
auto& memory = system.ApplicationMemory();
|
|
|
|
|
|
|
|
const auto& reg = ctx.cpu_registers;
|
|
|
|
u32 pc = reg[15], lr = reg[14], fp = reg[11];
|
|
|
|
out.push_back({"", 0, pc, 0, ""});
|
|
|
|
|
|
|
|
// fp (= r11) points to the last frame record.
|
|
|
|
// Frame records are two words long:
|
|
|
|
// fp+0 : pointer to previous frame record
|
|
|
|
// fp+4 : value of lr for frame
|
|
|
|
for (size_t i = 0; i < 256; i++) {
|
|
|
|
out.push_back({"", 0, lr, 0, ""});
|
|
|
|
if (!fp || (fp % 4 != 0) || !memory.IsValidVirtualAddressRange(fp, 8)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lr = memory.Read32(fp + 4);
|
|
|
|
fp = memory.Read32(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolicateBacktrace(system, out);
|
|
|
|
|
|
|
|
return out;
|
2020-03-20 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 02:17:48 +02:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
|
|
|
|
Core::System& system, const ARM_Interface::ThreadContext64& ctx) {
|
2023-06-13 03:34:25 +02:00
|
|
|
std::vector<BacktraceEntry> out;
|
|
|
|
auto& memory = system.ApplicationMemory();
|
|
|
|
|
|
|
|
const auto& reg = ctx.cpu_registers;
|
|
|
|
u64 pc = ctx.pc, lr = reg[30], fp = reg[29];
|
|
|
|
|
|
|
|
out.push_back({"", 0, pc, 0, ""});
|
|
|
|
|
|
|
|
// fp (= x29) points to the previous frame record.
|
|
|
|
// Frame records are two words long:
|
|
|
|
// fp+0 : pointer to previous frame record
|
|
|
|
// fp+8 : value of lr for frame
|
|
|
|
for (size_t i = 0; i < 256; i++) {
|
|
|
|
out.push_back({"", 0, lr, 0, ""});
|
|
|
|
if (!fp || (fp % 4 != 0) || !memory.IsValidVirtualAddressRange(fp, 16)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lr = memory.Read64(fp + 8);
|
|
|
|
fp = memory.Read64(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolicateBacktrace(system, out);
|
|
|
|
|
|
|
|
return out;
|
2022-04-21 02:17:48 +02:00
|
|
|
}
|
2019-05-18 03:43:26 +02:00
|
|
|
|
2022-04-21 02:17:48 +02:00
|
|
|
void ARM_Interface::SymbolicateBacktrace(Core::System& system, std::vector<BacktraceEntry>& out) {
|
2019-05-26 17:40:41 +02:00
|
|
|
std::map<VAddr, std::string> modules;
|
2019-11-26 20:10:49 +01:00
|
|
|
auto& loader{system.GetAppLoader()};
|
2019-05-26 17:40:41 +02:00
|
|
|
if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
|
2022-04-21 02:17:48 +02:00
|
|
|
return;
|
2019-05-26 17:40:41 +02:00
|
|
|
}
|
|
|
|
|
2022-04-09 05:53:42 +02:00
|
|
|
std::map<std::string, Symbols::Symbols> symbols;
|
2019-05-18 03:43:26 +02:00
|
|
|
for (const auto& module : modules) {
|
2023-10-21 22:47:43 +02:00
|
|
|
symbols.insert_or_assign(module.second,
|
|
|
|
Symbols::GetSymbols(module.first, system.ApplicationMemory(),
|
|
|
|
system.ApplicationProcess()->Is64Bit()));
|
2019-05-18 03:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entry : out) {
|
|
|
|
VAddr base = 0;
|
2019-05-26 17:40:41 +02:00
|
|
|
for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
|
|
|
|
const auto& module{*iter};
|
2019-05-18 03:43:26 +02:00
|
|
|
if (entry.original_address >= module.first) {
|
|
|
|
entry.module = module.second;
|
|
|
|
base = module.first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.offset = entry.original_address - base;
|
|
|
|
entry.address = SEGMENT_BASE + entry.offset;
|
|
|
|
|
2022-04-21 02:17:48 +02:00
|
|
|
if (entry.module.empty()) {
|
2019-05-18 03:43:26 +02:00
|
|
|
entry.module = "unknown";
|
2022-04-21 02:17:48 +02:00
|
|
|
}
|
2019-05-18 03:43:26 +02:00
|
|
|
|
|
|
|
const auto symbol_set = symbols.find(entry.module);
|
|
|
|
if (symbol_set != symbols.end()) {
|
2022-04-09 05:53:42 +02:00
|
|
|
const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
|
2023-01-13 22:06:13 +01:00
|
|
|
if (symbol) {
|
|
|
|
entry.name = Common::DemangleSymbol(*symbol);
|
2019-05-18 03:43:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 02:55:19 +01:00
|
|
|
}
|
2019-05-18 03:43:26 +02:00
|
|
|
|
2023-06-13 03:34:25 +02:00
|
|
|
std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const {
|
|
|
|
if (GetArchitecture() == Architecture::Aarch64) {
|
|
|
|
ThreadContext64 ctx;
|
|
|
|
SaveContext(ctx);
|
|
|
|
return GetBacktraceFromContext(system, ctx);
|
|
|
|
} else {
|
|
|
|
ThreadContext32 ctx;
|
|
|
|
SaveContext(ctx);
|
|
|
|
return GetBacktraceFromContext(system, ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 03:43:26 +02:00
|
|
|
void ARM_Interface::LogBacktrace() const {
|
2022-04-21 02:17:48 +02:00
|
|
|
const VAddr sp = GetSP();
|
2019-05-18 03:43:26 +02:00
|
|
|
const VAddr pc = GetPC();
|
|
|
|
LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", sp, pc);
|
|
|
|
LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address",
|
|
|
|
"Offset", "Symbol");
|
2019-05-26 17:40:41 +02:00
|
|
|
LOG_ERROR(Core_ARM, "");
|
2019-05-18 03:43:26 +02:00
|
|
|
const auto backtrace = GetBacktrace();
|
|
|
|
for (const auto& entry : backtrace) {
|
|
|
|
LOG_ERROR(Core_ARM, "{:20}{:016X} {:016X} {:016X} {}", entry.module, entry.address,
|
|
|
|
entry.original_address, entry.offset, entry.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 20:37:37 +02:00
|
|
|
void ARM_Interface::Run() {
|
|
|
|
using Kernel::StepState;
|
|
|
|
using Kernel::SuspendType;
|
|
|
|
|
|
|
|
while (true) {
|
2022-06-16 16:35:52 +02:00
|
|
|
Kernel::KThread* current_thread{Kernel::GetCurrentThreadPointer(system.Kernel())};
|
2023-06-13 03:34:25 +02:00
|
|
|
HaltReason hr{};
|
2022-05-31 20:37:37 +02:00
|
|
|
|
|
|
|
// Notify the debugger and go to sleep if a step was performed
|
|
|
|
// and this thread has been scheduled again.
|
|
|
|
if (current_thread->GetStepState() == StepState::StepPerformed) {
|
|
|
|
system.GetDebugger().NotifyThreadStopped(current_thread);
|
|
|
|
current_thread->RequestSuspend(SuspendType::Debug);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, run the thread.
|
2023-06-13 03:34:25 +02:00
|
|
|
system.EnterCPUProfile();
|
2022-05-31 20:37:37 +02:00
|
|
|
if (current_thread->GetStepState() == StepState::StepPending) {
|
|
|
|
hr = StepJit();
|
|
|
|
|
2023-06-13 03:34:25 +02:00
|
|
|
if (True(hr & HaltReason::StepThread)) {
|
2022-05-31 20:37:37 +02:00
|
|
|
current_thread->SetStepState(StepState::StepPerformed);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hr = RunJit();
|
|
|
|
}
|
2023-06-13 03:34:25 +02:00
|
|
|
system.ExitCPUProfile();
|
2022-05-31 20:37:37 +02:00
|
|
|
|
2022-09-06 03:19:30 +02:00
|
|
|
// If the thread is scheduled for termination, exit the thread.
|
|
|
|
if (current_thread->HasDpc()) {
|
|
|
|
if (current_thread->IsTerminationRequested()) {
|
|
|
|
current_thread->Exit();
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 02:39:16 +02:00
|
|
|
// Notify the debugger and go to sleep if a breakpoint was hit,
|
|
|
|
// or if the thread is unable to continue for any reason.
|
2023-06-13 03:34:25 +02:00
|
|
|
if (True(hr & HaltReason::InstructionBreakpoint) || True(hr & HaltReason::PrefetchAbort)) {
|
2023-07-09 18:03:25 +02:00
|
|
|
if (!True(hr & HaltReason::PrefetchAbort)) {
|
2022-12-02 04:48:43 +01:00
|
|
|
RewindBreakpointInstruction();
|
|
|
|
}
|
2022-06-21 02:39:16 +02:00
|
|
|
if (system.DebuggerEnabled()) {
|
|
|
|
system.GetDebugger().NotifyThreadStopped(current_thread);
|
2022-12-02 04:48:43 +01:00
|
|
|
} else {
|
|
|
|
LogBacktrace();
|
2022-06-21 02:39:16 +02:00
|
|
|
}
|
2022-12-02 04:48:43 +01:00
|
|
|
current_thread->RequestSuspend(SuspendType::Debug);
|
2022-06-06 18:56:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-06-21 02:39:16 +02:00
|
|
|
|
|
|
|
// Notify the debugger and go to sleep if a watchpoint was hit.
|
2023-06-13 03:34:25 +02:00
|
|
|
if (True(hr & HaltReason::DataAbort)) {
|
2022-06-21 02:39:16 +02:00
|
|
|
if (system.DebuggerEnabled()) {
|
|
|
|
system.GetDebugger().NotifyThreadWatchpoint(current_thread, *HaltedWatchpoint());
|
|
|
|
}
|
2022-06-06 18:56:01 +02:00
|
|
|
current_thread->RequestSuspend(SuspendType::Debug);
|
2022-05-31 20:37:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-27 00:52:16 +02:00
|
|
|
// Handle syscalls and scheduling (this may change the current thread/core)
|
2023-06-13 03:34:25 +02:00
|
|
|
if (True(hr & HaltReason::SupervisorCall)) {
|
2022-05-31 20:37:37 +02:00
|
|
|
Kernel::Svc::Call(system, GetSvcNumber());
|
2022-06-27 00:52:16 +02:00
|
|
|
break;
|
2022-05-31 20:37:37 +02:00
|
|
|
}
|
2023-06-13 03:34:25 +02:00
|
|
|
if (True(hr & HaltReason::BreakLoop) || !uses_wall_clock) {
|
2022-05-31 20:37:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
Fixes and workarounds to make UBSan happier on macOS
There are still some other issues not addressed here, but it's a start.
Workarounds for false-positive reports:
- `RasterizerAccelerated`: Put a gigantic array behind a `unique_ptr`,
because UBSan has a [hardcoded limit](https://stackoverflow.com/questions/64531383/c-runtime-error-using-fsanitize-undefined-object-has-a-possibly-invalid-vp)
of how big it thinks objects can be, specifically when dealing with
offset-to-top values used with multiple inheritance. Hopefully this
doesn't have a performance impact.
- `QueryCacheBase::QueryCacheBase`: Avoid an operation that UBSan thinks
is UB even though it at least arguably isn't. See the link in the
comment for more information.
Fixes for correct reports:
- `PageTable`, `Memory`: Use `uintptr_t` values instead of pointers to
avoid UB from pointer overflow (when pointer arithmetic wraps around
the address space).
- `KScheduler::Reload`: `thread->GetOwnerProcess()` can be `nullptr`;
avoid calling methods on it in this case. (The existing code returns
a garbage reference to a field, which is then passed into
`LoadWatchpointArray`, and apparently it's never used, so it's
harmless in practice but still triggers UBSan.)
- `KAutoObject::Close`: This function calls `this->Destroy()`, which
overwrites the beginning of the object with junk (specifically a free
list pointer). Then it calls `this->UnregisterWithKernel()`. UBSan
complains about a type mismatch because the vtable has been
overwritten, and I believe this is indeed UB. `UnregisterWithKernel`
also loads `m_kernel` from the 'freed' object, which seems to be
technically safe (the overwriting doesn't extend as far as that
field), but seems dubious. Switch to a `static` method and load
`m_kernel` in advance.
2023-07-02 00:00:39 +02:00
|
|
|
void ARM_Interface::LoadWatchpointArray(const WatchpointArray* wp) {
|
|
|
|
watchpoints = wp;
|
2022-06-06 18:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Kernel::DebugWatchpoint* ARM_Interface::MatchingWatchpoint(
|
2023-03-18 02:26:04 +01:00
|
|
|
u64 addr, u64 size, Kernel::DebugWatchpointType access_type) const {
|
2022-06-06 18:56:01 +02:00
|
|
|
if (!watchpoints) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-03-18 02:26:04 +01:00
|
|
|
const u64 start_address{addr};
|
|
|
|
const u64 end_address{addr + size};
|
2022-06-06 18:56:01 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < Core::Hardware::NUM_WATCHPOINTS; i++) {
|
|
|
|
const auto& watch{(*watchpoints)[i]};
|
|
|
|
|
2023-03-18 02:26:04 +01:00
|
|
|
if (end_address <= GetInteger(watch.start_address)) {
|
2022-06-06 18:56:01 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-18 02:26:04 +01:00
|
|
|
if (start_address >= GetInteger(watch.end_address)) {
|
2022-06-06 18:56:01 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((access_type & watch.type) == Kernel::DebugWatchpointType::None) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-31 02:41:30 +01:00
|
|
|
} // namespace Core
|