1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-28 05:06:08 +00:00

Merge branch 'master' into emunand_dev

This commit is contained in:
hexkyz 2019-06-03 20:14:22 +01:00
commit 4c328b6c50
10 changed files with 407 additions and 169 deletions

View file

@ -19,6 +19,16 @@ Atmosphère consists of multiple components, each of which replaces/modifies a d
* Stratosphère: Custom Sysmodule(s), both Rosalina style to extend the kernel/provide new features, and of the loader reimplementation style to hook important system actions
* Troposphère: Application-level Horizon OS patches, used to implement desirable CFW features
Licensing
=====
This software is licensed under the terms of the GPLv2, with exemptions for specific projects noted below.
You can find a copy of the license in the [LICENSE file](LICENSE).
Exemptions:
* The [yuzu emulator project](https://github.com/yuzu-emu/yuzu) is exempt from GPLv2 licensing and may (at its option) instead license any source code authored for the Atmosphère project as GPLv2 or later.
Credits
=====

View file

@ -17,9 +17,15 @@
#pragma once
#include <switch.h>
struct StackFrame {
u64 fp;
u64 lr;
union StackFrame {
struct {
u64 fp;
u64 lr;
} frame_64;
struct {
u32 fp;
u32 lr;
} frame_32;
};
struct AttachProcessInfo {

View file

@ -75,9 +75,11 @@ bool ThreadInfo::ReadFromProcess(std::map<u64, u64> &tls_map, Handle debug_handl
return false;
}
/* Don't try to parse stack frames if 32-bit. */
/* In AArch32 mode the LR, FP, and SP registers aren't set correctly in the ThreadContext by svcGetDebugThreadParam... */
if (!is_64_bit) {
return true;
this->context.fp = this->context.cpu_gprs[11].x;
this->context.sp = this->context.cpu_gprs[13].x;
this->context.lr = this->context.cpu_gprs[14].x;
}
/* Parse information from TLS if present. */
@ -104,21 +106,41 @@ bool ThreadInfo::ReadFromProcess(std::map<u64, u64> &tls_map, Handle debug_handl
TryGetStackInfo(debug_handle);
u64 cur_fp = this->context.fp;
for (unsigned int i = 0; i < sizeof(this->stack_trace)/sizeof(u64); i++) {
/* Validate the current frame. */
if (cur_fp == 0 || (cur_fp & 0xF)) {
break;
}
/* Read a new frame. */
StackFrame cur_frame;
if (R_FAILED(svcReadDebugProcessMemory(&cur_frame, debug_handle, cur_fp, sizeof(StackFrame)))) {
break;
}
if (is_64_bit) {
for (unsigned int i = 0; i < sizeof(this->stack_trace)/sizeof(u64); i++) {
/* Validate the current frame. */
if (cur_fp == 0 || (cur_fp & 0xF)) {
break;
}
/* Advance to the next frame. */
this->stack_trace[this->stack_trace_size++] = cur_frame.lr;
cur_fp = cur_frame.fp;
/* Read a new frame. */
StackFrame cur_frame;
if (R_FAILED(svcReadDebugProcessMemory(&cur_frame, debug_handle, cur_fp, sizeof(cur_frame.frame_64)))) {
break;
}
/* Advance to the next frame. */
this->stack_trace[this->stack_trace_size++] = cur_frame.frame_64.lr;
cur_fp = cur_frame.frame_64.fp;
}
} else {
for (unsigned int i = 0; i < sizeof(this->stack_trace)/sizeof(u64); i++) {
/* Validate the current frame. */
if (cur_fp == 0 || (cur_fp & 0x7)) {
break;
}
/* Read a new frame. */
StackFrame cur_frame;
if (R_FAILED(svcReadDebugProcessMemory(&cur_frame, debug_handle, cur_fp, sizeof(cur_frame.frame_32)))) {
break;
}
/* Advance to the next frame. */
this->stack_trace[this->stack_trace_size++] = cur_frame.frame_32.lr;
cur_fp = cur_frame.frame_32.fp;
}
}
return true;

View file

@ -14,12 +14,27 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/stat.h>
#include <switch.h>
#include "dmnt_cheat_types.hpp"
#include "dmnt_cheat_vm.hpp"
#include "dmnt_cheat_manager.hpp"
#include "dmnt_hid.hpp"
void DmntCheatVm::DebugLog(u32 log_id, u64 value) {
/* Just unconditionally try to create the log folder. */
mkdir("/atmosphere/cheat_vm_logs", 0777);
FILE *f_log = NULL;
{
char log_path[FS_MAX_PATH];
snprintf(log_path, sizeof(log_path), "/atmosphere/cheat_vm_logs/%x.log", log_id);
f_log = fopen(log_path, "ab");
}
if (f_log != NULL) {
ON_SCOPE_EXIT { fclose(f_log); };
fprintf(f_log, "%016lx\n", value);
}
}
void DmntCheatVm::OpenDebugLogFile() {
#ifdef DMNT_CHEAT_VM_DEBUG_LOG
@ -157,36 +172,36 @@ void DmntCheatVm::LogOpcode(const CheatVmOpcode *opcode) {
this->LogToDebugFile("Bit Width: %x\n", opcode->begin_reg_cond.bit_width);
this->LogToDebugFile("Cond Type: %x\n", opcode->begin_reg_cond.cond_type);
this->LogToDebugFile("V Reg Idx: %x\n", opcode->begin_reg_cond.val_reg_index);
switch (opcode->begin_reg_cond.comp_type) {
case CompareRegisterValueType_StaticValue:
this->LogToDebugFile("Comp Type: Static Value\n");
this->LogToDebugFile("Value: %lx\n", opcode->begin_reg_cond.value.bit64);
break;
case CompareRegisterValueType_OtherRegister:
this->LogToDebugFile("Comp Type: Other Register\n");
this->LogToDebugFile("X Reg Idx: %x\n", opcode->begin_reg_cond.other_reg_index);
break;
case CompareRegisterValueType_MemoryRelAddr:
this->LogToDebugFile("Comp Type: Memory Relative Address\n");
this->LogToDebugFile("Mem Type: %x\n", opcode->begin_reg_cond.mem_type);
this->LogToDebugFile("Rel Addr: %lx\n", opcode->begin_reg_cond.rel_address);
break;
case CompareRegisterValueType_MemoryOfsReg:
this->LogToDebugFile("Comp Type: Memory Offset Register\n");
this->LogToDebugFile("Mem Type: %x\n", opcode->begin_reg_cond.mem_type);
this->LogToDebugFile("O Reg Idx: %x\n", opcode->begin_reg_cond.ofs_reg_index);
break;
case CompareRegisterValueType_RegisterRelAddr:
this->LogToDebugFile("Comp Type: Register Relative Address\n");
this->LogToDebugFile("A Reg Idx: %x\n", opcode->begin_reg_cond.addr_reg_index);
this->LogToDebugFile("Rel Addr: %lx\n", opcode->begin_reg_cond.rel_address);
break;
case CompareRegisterValueType_RegisterOfsReg:
this->LogToDebugFile("Comp Type: Register Offset Register\n");
this->LogToDebugFile("A Reg Idx: %x\n", opcode->begin_reg_cond.addr_reg_index);
this->LogToDebugFile("O Reg Idx: %x\n", opcode->begin_reg_cond.ofs_reg_index);
break;
}
switch (opcode->begin_reg_cond.comp_type) {
case CompareRegisterValueType_StaticValue:
this->LogToDebugFile("Comp Type: Static Value\n");
this->LogToDebugFile("Value: %lx\n", opcode->begin_reg_cond.value.bit64);
break;
case CompareRegisterValueType_OtherRegister:
this->LogToDebugFile("Comp Type: Other Register\n");
this->LogToDebugFile("X Reg Idx: %x\n", opcode->begin_reg_cond.other_reg_index);
break;
case CompareRegisterValueType_MemoryRelAddr:
this->LogToDebugFile("Comp Type: Memory Relative Address\n");
this->LogToDebugFile("Mem Type: %x\n", opcode->begin_reg_cond.mem_type);
this->LogToDebugFile("Rel Addr: %lx\n", opcode->begin_reg_cond.rel_address);
break;
case CompareRegisterValueType_MemoryOfsReg:
this->LogToDebugFile("Comp Type: Memory Offset Register\n");
this->LogToDebugFile("Mem Type: %x\n", opcode->begin_reg_cond.mem_type);
this->LogToDebugFile("O Reg Idx: %x\n", opcode->begin_reg_cond.ofs_reg_index);
break;
case CompareRegisterValueType_RegisterRelAddr:
this->LogToDebugFile("Comp Type: Register Relative Address\n");
this->LogToDebugFile("A Reg Idx: %x\n", opcode->begin_reg_cond.addr_reg_index);
this->LogToDebugFile("Rel Addr: %lx\n", opcode->begin_reg_cond.rel_address);
break;
case CompareRegisterValueType_RegisterOfsReg:
this->LogToDebugFile("Comp Type: Register Offset Register\n");
this->LogToDebugFile("A Reg Idx: %x\n", opcode->begin_reg_cond.addr_reg_index);
this->LogToDebugFile("O Reg Idx: %x\n", opcode->begin_reg_cond.ofs_reg_index);
break;
}
break;
case CheatVmOpcodeType_SaveRestoreRegister:
this->LogToDebugFile("Opcode: Save or Restore Register\n");
@ -201,6 +216,37 @@ void DmntCheatVm::LogOpcode(const CheatVmOpcode *opcode) {
this->LogToDebugFile("Act[%02x]: %d\n", i, opcode->save_restore_regmask.should_operate[i]);
}
break;
case CheatVmOpcodeType_DebugLog:
this->LogToDebugFile("Opcode: Debug Log\n");
this->LogToDebugFile("Bit Width: %x\n", opcode->debug_log.bit_width);
this->LogToDebugFile("Log ID: %x\n", opcode->debug_log.log_id);
this->LogToDebugFile("Val Type: %x\n", opcode->debug_log.val_type);
switch (opcode->debug_log.val_type) {
case DebugLogValueType_RegisterValue:
this->LogToDebugFile("Val Type: Register Value\n");
this->LogToDebugFile("X Reg Idx: %x\n", opcode->debug_log.val_reg_index);
break;
case DebugLogValueType_MemoryRelAddr:
this->LogToDebugFile("Val Type: Memory Relative Address\n");
this->LogToDebugFile("Mem Type: %x\n", opcode->debug_log.mem_type);
this->LogToDebugFile("Rel Addr: %lx\n", opcode->debug_log.rel_address);
break;
case DebugLogValueType_MemoryOfsReg:
this->LogToDebugFile("Val Type: Memory Offset Register\n");
this->LogToDebugFile("Mem Type: %x\n", opcode->debug_log.mem_type);
this->LogToDebugFile("O Reg Idx: %x\n", opcode->debug_log.ofs_reg_index);
break;
case DebugLogValueType_RegisterRelAddr:
this->LogToDebugFile("Val Type: Register Relative Address\n");
this->LogToDebugFile("A Reg Idx: %x\n", opcode->debug_log.addr_reg_index);
this->LogToDebugFile("Rel Addr: %lx\n", opcode->debug_log.rel_address);
break;
case DebugLogValueType_RegisterOfsReg:
this->LogToDebugFile("Val Type: Register Offset Register\n");
this->LogToDebugFile("A Reg Idx: %x\n", opcode->debug_log.addr_reg_index);
this->LogToDebugFile("O Reg Idx: %x\n", opcode->debug_log.ofs_reg_index);
break;
}
default:
this->LogToDebugFile("Unknown opcode: %x\n", opcode->opcode);
break;
@ -260,6 +306,9 @@ bool DmntCheatVm::DecodeNextOpcode(CheatVmOpcode *out) {
if (opcode.opcode >= CheatVmOpcodeType_ExtendedWidth) {
opcode.opcode = (CheatVmOpcodeType)((((u32)opcode.opcode) << 4) | ((first_dword >> 24) & 0xF));
}
if (opcode.opcode >= CheatVmOpcodeType_DoubleExtendedWidth) {
opcode.opcode = (CheatVmOpcodeType)((((u32)opcode.opcode) << 4) | ((first_dword >> 20) & 0xF));
}
/* detect condition start. */
switch (opcode.opcode) {
@ -500,7 +549,53 @@ bool DmntCheatVm::DecodeNextOpcode(CheatVmOpcode *out) {
}
}
break;
case CheatVmOpcodeType_DebugLog:
{
/* FFFTIX## */
/* FFFTI0Ma aaaaaaaa */
/* FFFTI1Mr */
/* FFFTI2Ra aaaaaaaa */
/* FFFTI3Rr */
/* FFFTI4X0 */
/* FFF = opcode 0xFFF */
/* T = bit width. */
/* I = log id. */
/* X = value operand type, 0 = main/heap with relative offset, 1 = main/heap with offset register, */
/* 2 = register with relative offset, 3 = register with offset register, 4 = register value. */
/* M = memory type. */
/* R = address register. */
/* a = relative address. */
/* r = offset register. */
/* X = value register. */
opcode.debug_log.bit_width = (first_dword >> 16) & 0xF;
opcode.debug_log.log_id = ((first_dword >> 12) & 0xF);
opcode.debug_log.val_type = (DebugLogValueType)((first_dword >> 8) & 0xF);
switch (opcode.debug_log.val_type) {
case DebugLogValueType_RegisterValue:
opcode.debug_log.val_reg_index = ((first_dword >> 4) & 0xF);
break;
case DebugLogValueType_MemoryRelAddr:
opcode.debug_log.mem_type = (MemoryAccessType)((first_dword >> 4) & 0xF);
opcode.debug_log.rel_address = (((u64)(first_dword & 0xF) << 32ul) | ((u64)GetNextDword()));
break;
case DebugLogValueType_MemoryOfsReg:
opcode.debug_log.mem_type = (MemoryAccessType)((first_dword >> 4) & 0xF);
opcode.debug_log.ofs_reg_index = (first_dword & 0xF);
break;
case DebugLogValueType_RegisterRelAddr:
opcode.debug_log.addr_reg_index = ((first_dword >> 4) & 0xF);
opcode.debug_log.rel_address = (((u64)(first_dword & 0xF) << 32ul) | ((u64)GetNextDword()));
break;
case DebugLogValueType_RegisterOfsReg:
opcode.debug_log.addr_reg_index = ((first_dword >> 4) & 0xF);
opcode.debug_log.ofs_reg_index = (first_dword & 0xF);
break;
}
}
break;
case CheatVmOpcodeType_ExtendedWidth:
case CheatVmOpcodeType_DoubleExtendedWidth:
default:
/* Unrecognized instruction cannot be decoded. */
valid = false;
@ -1058,6 +1153,57 @@ void DmntCheatVm::Execute(const CheatProcessMetadata *metadata) {
}
}
break;
case CheatVmOpcodeType_DebugLog:
{
/* Read value from memory. */
u64 log_value = 0;
if (cur_opcode.debug_log.val_type == DebugLogValueType_RegisterValue) {
switch (cur_opcode.debug_log.bit_width) {
case 1:
log_value = static_cast<u8>(this->registers[cur_opcode.debug_log.val_reg_index] & 0xFFul);
break;
case 2:
log_value = static_cast<u16>(this->registers[cur_opcode.debug_log.val_reg_index] & 0xFFFFul);
break;
case 4:
log_value = static_cast<u32>(this->registers[cur_opcode.debug_log.val_reg_index] & 0xFFFFFFFFul);
break;
case 8:
log_value = static_cast<u64>(this->registers[cur_opcode.debug_log.val_reg_index] & 0xFFFFFFFFFFFFFFFFul);
break;
}
} else {
u64 val_address = 0;
switch (cur_opcode.debug_log.val_type) {
case DebugLogValueType_MemoryRelAddr:
val_address = GetCheatProcessAddress(metadata, cur_opcode.debug_log.mem_type, cur_opcode.debug_log.rel_address);
break;
case DebugLogValueType_MemoryOfsReg:
val_address = GetCheatProcessAddress(metadata, cur_opcode.debug_log.mem_type, this->registers[cur_opcode.debug_log.ofs_reg_index]);
break;
case DebugLogValueType_RegisterRelAddr:
val_address = this->registers[cur_opcode.debug_log.addr_reg_index] + cur_opcode.debug_log.rel_address;
break;
case DebugLogValueType_RegisterOfsReg:
val_address = this->registers[cur_opcode.debug_log.addr_reg_index] + this->registers[cur_opcode.debug_log.ofs_reg_index];
break;
default:
break;
}
switch (cur_opcode.debug_log.bit_width) {
case 1:
case 2:
case 4:
case 8:
DmntCheatManager::ReadCheatProcessMemoryForVm(val_address, &log_value, cur_opcode.debug_log.bit_width);
break;
}
}
/* Log value. */
this->DebugLog(cur_opcode.debug_log.log_id, log_value);
}
break;
default:
/* By default, we do a no-op. */
break;

View file

@ -45,6 +45,13 @@ enum CheatVmOpcodeType : u32 {
CheatVmOpcodeType_BeginRegisterConditionalBlock = 0xC0,
CheatVmOpcodeType_SaveRestoreRegister = 0xC1,
CheatVmOpcodeType_SaveRestoreRegisterMask = 0xC2,
/* This is a meta entry, and not a real opcode. */
/* This is to facilitate multi-nybble instruction decoding. */
CheatVmOpcodeType_DoubleExtendedWidth = 0xF0,
/* Double-extended width opcodes. */
CheatVmOpcodeType_DebugLog = 0xFFF,
};
enum MemoryAccessType : u32 {
@ -102,6 +109,14 @@ enum SaveRestoreRegisterOpType : u32 {
SaveRestoreRegisterOpType_ClearRegs = 3,
};
enum DebugLogValueType : u32 {
DebugLogValueType_MemoryRelAddr = 0,
DebugLogValueType_MemoryOfsReg = 1,
DebugLogValueType_RegisterRelAddr = 2,
DebugLogValueType_RegisterOfsReg = 3,
DebugLogValueType_RegisterValue = 4,
};
union VmInt {
u8 bit8;
u16 bit16;
@ -211,6 +226,17 @@ struct SaveRestoreRegisterMaskOpcode {
bool should_operate[0x10];
};
struct DebugLogOpcode {
u32 bit_width;
u32 log_id;
DebugLogValueType val_type;
MemoryAccessType mem_type;
u32 addr_reg_index;
u32 val_reg_index;
u32 ofs_reg_index;
u64 rel_address;
};
struct CheatVmOpcode {
CheatVmOpcodeType opcode;
bool begin_conditional_block;
@ -229,6 +255,7 @@ struct CheatVmOpcode {
BeginRegisterConditionalOpcode begin_reg_cond;
SaveRestoreRegisterOpcode save_restore_reg;
SaveRestoreRegisterMaskOpcode save_restore_regmask;
DebugLogOpcode debug_log;
};
};
@ -250,6 +277,9 @@ class DmntCheatVm {
void SkipConditionalBlock();
void ResetState();
/* For implementing the DebugLog opcode. */
void DebugLog(u32 log_id, u64 value);
/* For debugging. These will be IFDEF'd out normally. */
void OpenDebugLogFile();
void CloseDebugLogFile();

View file

@ -35,6 +35,7 @@ static std::atomic<u64> g_debug_on_launch_tid(0);
static IEvent *g_process_event = nullptr;
static IEvent *g_debug_title_event = nullptr;
static IEvent *g_debug_application_event = nullptr;
static IEvent *g_boot_finished_event = nullptr;
static u8 g_ac_buf[4 * sizeof(LoaderProgramInfo)];
@ -46,6 +47,7 @@ void Registration::InitializeSystemResources() {
g_process_event = CreateWriteOnlySystemEvent();
g_debug_title_event = CreateWriteOnlySystemEvent();
g_debug_application_event = CreateWriteOnlySystemEvent();
g_boot_finished_event = CreateWriteOnlySystemEvent();
/* Auto-clear non-system event. */
g_process_launch_start_event = CreateHosEvent(&Registration::ProcessLaunchStartCallback);
@ -501,3 +503,19 @@ Result Registration::DisableDebug(u32 which) {
}
return ResultSuccess;
}
Handle Registration::GetDebugTitleEventHandle() {
return g_debug_title_event->GetHandle();
}
Handle Registration::GetDebugApplicationEventHandle() {
return g_debug_application_event->GetHandle();
}
Handle Registration::GetBootFinishedEventHandle() {
return g_boot_finished_event->GetHandle();
}
void Registration::SignalBootFinished() {
g_boot_finished_event->Signal();
}

View file

@ -193,6 +193,7 @@ class Registration {
static Result DisableDebug(u32 which);
static Handle GetDebugTitleEventHandle();
static Handle GetDebugApplicationEventHandle();
static Handle GetBootFinishedEventHandle();
static void HandleProcessLaunch();
static Result LaunchDebugProcess(u64 pid);
@ -200,6 +201,8 @@ class Registration {
static Result LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid);
static Result LaunchProcessByTidSid(TidSid tid_sid, u64 launch_flags, u64 *out_pid);
static void SignalBootFinished();
static bool HasApplicationProcess(std::shared_ptr<Process> *out);
};

View file

@ -86,6 +86,7 @@ Result ShellService::ClearProcessNotificationFlag(u64 pid) {
void ShellService::NotifyBootFinished() {
if (!g_has_boot_finished) {
g_has_boot_finished = true;
Registration::SignalBootFinished();
EmbeddedBoot2::Main();
}
}
@ -113,13 +114,15 @@ Result ShellService::BoostSystemThreadsResourceLimit() {
}
Result ShellService::GetUnimplementedEventHandle(Out<CopiedHandle> event) {
/* In 8.0.0, Nintendo added this command which should return an event handle. */
/* In addition, they also added code to create a new event in the global PM constructor. */
/* However, nothing signals this event, and this command currently does std::abort();. */
/* We will oblige. */
std::abort();
/* TODO: Return an event handle, once N makes this command a real thing in the future. */
/* TODO: return ResultSuccess; */
void ShellService::GetBootFinishedEvent(Out<CopiedHandle> event) {
/* In 8.0.0, Nintendo added this command, which signals that the boot sysmodule has finished. */
/* Nintendo only signals it in safe mode FIRM, and this function aborts on normal FIRM. */
/* We will signal it always, but only allow this function to succeed on safe mode. */
{
u64 is_recovery_boot = 0;
if (R_FAILED(SmcGetConfig(SplConfigItem_IsRecoveryBoot, &is_recovery_boot)) || !is_recovery_boot) {
std::abort();
}
}
event.SetValue(Registration::GetBootFinishedEventHandle());
}

View file

@ -44,7 +44,7 @@ enum ShellCmd_5X {
Shell_Cmd_5X_BoostSystemMemoryResourceLimit = 7,
Shell_Cmd_BoostSystemThreadsResourceLimit = 8,
Shell_Cmd_GetUnimplementedEventHandle = 9 /* TODO: Rename when Nintendo implements this. */
Shell_Cmd_GetBootFinishedEvent = 9,
};
class ShellService final : public IServiceObject {
@ -61,7 +61,7 @@ class ShellService final : public IServiceObject {
Result GetApplicationProcessId(Out<u64> pid);
Result BoostSystemMemoryResourceLimit(u64 sysmem_size);
Result BoostSystemThreadsResourceLimit();
Result GetUnimplementedEventHandle(Out<CopiedHandle> event);
void GetBootFinishedEvent(Out<CopiedHandle> event);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
/* 1.0.0-4.0.0 */
@ -92,6 +92,6 @@ class ShellService final : public IServiceObject {
MakeServiceCommandMeta<Shell_Cmd_BoostSystemThreadsResourceLimit, &ShellService::BoostSystemThreadsResourceLimit, FirmwareVersion_700>(),
/* 8.0.0-* */
MakeServiceCommandMeta<Shell_Cmd_GetUnimplementedEventHandle, &ShellService::GetUnimplementedEventHandle, FirmwareVersion_800>(),
MakeServiceCommandMeta<Shell_Cmd_GetBootFinishedEvent, &ShellService::GetBootFinishedEvent, FirmwareVersion_800>(),
};
};