fatal.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <fmt/chrono.h>
  7. #include "common/logging/log.h"
  8. #include "common/scm_rev.h"
  9. #include "common/swap.h"
  10. #include "core/core.h"
  11. #include "core/hle/ipc_helpers.h"
  12. #include "core/hle/service/fatal/fatal.h"
  13. #include "core/hle/service/fatal/fatal_p.h"
  14. #include "core/hle/service/fatal/fatal_u.h"
  15. #include "core/reporter.h"
  16. namespace Service::Fatal {
  17. Module::Interface::Interface(std::shared_ptr<Module> module_, Core::System& system_,
  18. const char* name)
  19. : ServiceFramework{system_, name}, module{std::move(module_)} {}
  20. Module::Interface::~Interface() = default;
  21. struct FatalInfo {
  22. enum class Architecture : s32 {
  23. AArch64,
  24. AArch32,
  25. };
  26. const char* ArchAsString() const {
  27. return arch == Architecture::AArch64 ? "AArch64" : "AArch32";
  28. }
  29. std::array<u64_le, 31> registers{};
  30. u64_le sp{};
  31. u64_le pc{};
  32. u64_le pstate{};
  33. u64_le afsr0{};
  34. u64_le afsr1{};
  35. u64_le esr{};
  36. u64_le far{};
  37. std::array<u64_le, 32> backtrace{};
  38. u64_le program_entry_point{};
  39. // Bit flags that indicate which registers have been set with values
  40. // for this context. The service itself uses these to determine which
  41. // registers to specifically print out.
  42. u64_le set_flags{};
  43. u32_le backtrace_size{};
  44. Architecture arch{};
  45. u32_le unk10{}; // TODO(ogniK): Is this even used or is it just padding?
  46. };
  47. static_assert(sizeof(FatalInfo) == 0x250, "FatalInfo is an invalid size");
  48. enum class FatalType : u32 {
  49. ErrorReportAndScreen = 0,
  50. ErrorReport = 1,
  51. ErrorScreen = 2,
  52. };
  53. static void GenerateErrorReport(Core::System& system, Result error_code, const FatalInfo& info) {
  54. const auto title_id = system.GetCurrentProcessProgramID();
  55. std::string crash_report = fmt::format(
  56. "Yuzu {}-{} crash report\n"
  57. "Title ID: {:016x}\n"
  58. "Result: 0x{:X} ({:04}-{:04d})\n"
  59. "Set flags: 0x{:16X}\n"
  60. "Program entry point: 0x{:16X}\n"
  61. "\n",
  62. Common::g_scm_branch, Common::g_scm_desc, title_id, error_code.raw,
  63. 2000 + static_cast<u32>(error_code.module.Value()),
  64. static_cast<u32>(error_code.description.Value()), info.set_flags, info.program_entry_point);
  65. if (info.backtrace_size != 0x0) {
  66. crash_report += "Registers:\n";
  67. for (size_t i = 0; i < info.registers.size(); i++) {
  68. crash_report +=
  69. fmt::format(" X[{:02d}]: {:016x}\n", i, info.registers[i]);
  70. }
  71. crash_report += fmt::format(" SP: {:016x}\n", info.sp);
  72. crash_report += fmt::format(" PC: {:016x}\n", info.pc);
  73. crash_report += fmt::format(" PSTATE: {:016x}\n", info.pstate);
  74. crash_report += fmt::format(" AFSR0: {:016x}\n", info.afsr0);
  75. crash_report += fmt::format(" AFSR1: {:016x}\n", info.afsr1);
  76. crash_report += fmt::format(" ESR: {:016x}\n", info.esr);
  77. crash_report += fmt::format(" FAR: {:016x}\n", info.far);
  78. crash_report += "\nBacktrace:\n";
  79. for (size_t i = 0; i < info.backtrace_size; i++) {
  80. crash_report +=
  81. fmt::format(" Backtrace[{:02d}]: {:016x}\n", i, info.backtrace[i]);
  82. }
  83. crash_report += fmt::format("Architecture: {}\n", info.ArchAsString());
  84. crash_report += fmt::format("Unknown 10: 0x{:016x}\n", info.unk10);
  85. }
  86. LOG_ERROR(Service_Fatal, "{}", crash_report);
  87. system.GetReporter().SaveCrashReport(
  88. title_id, error_code, info.set_flags, info.program_entry_point, info.sp, info.pc,
  89. info.pstate, info.afsr0, info.afsr1, info.esr, info.far, info.registers, info.backtrace,
  90. info.backtrace_size, info.ArchAsString(), info.unk10);
  91. }
  92. static void ThrowFatalError(Core::System& system, Result error_code, FatalType fatal_type,
  93. const FatalInfo& info) {
  94. LOG_ERROR(Service_Fatal, "Threw fatal error type {} with error code 0x{:X}", fatal_type,
  95. error_code.raw);
  96. switch (fatal_type) {
  97. case FatalType::ErrorReportAndScreen:
  98. GenerateErrorReport(system, error_code, info);
  99. [[fallthrough]];
  100. case FatalType::ErrorScreen:
  101. // Since we have no fatal:u error screen. We should just kill execution instead
  102. ASSERT(false);
  103. break;
  104. // Should not throw a fatal screen but should generate an error report
  105. case FatalType::ErrorReport:
  106. GenerateErrorReport(system, error_code, info);
  107. break;
  108. }
  109. }
  110. void Module::Interface::ThrowFatal(Kernel::HLERequestContext& ctx) {
  111. LOG_ERROR(Service_Fatal, "called");
  112. IPC::RequestParser rp{ctx};
  113. const auto error_code = rp.Pop<Result>();
  114. ThrowFatalError(system, error_code, FatalType::ErrorScreen, {});
  115. IPC::ResponseBuilder rb{ctx, 2};
  116. rb.Push(ResultSuccess);
  117. }
  118. void Module::Interface::ThrowFatalWithPolicy(Kernel::HLERequestContext& ctx) {
  119. LOG_ERROR(Service_Fatal, "called");
  120. IPC::RequestParser rp(ctx);
  121. const auto error_code = rp.Pop<Result>();
  122. const auto fatal_type = rp.PopEnum<FatalType>();
  123. ThrowFatalError(system, error_code, fatal_type,
  124. {}); // No info is passed with ThrowFatalWithPolicy
  125. IPC::ResponseBuilder rb{ctx, 2};
  126. rb.Push(ResultSuccess);
  127. }
  128. void Module::Interface::ThrowFatalWithCpuContext(Kernel::HLERequestContext& ctx) {
  129. LOG_ERROR(Service_Fatal, "called");
  130. IPC::RequestParser rp(ctx);
  131. const auto error_code = rp.Pop<Result>();
  132. const auto fatal_type = rp.PopEnum<FatalType>();
  133. const auto fatal_info = ctx.ReadBuffer();
  134. FatalInfo info{};
  135. ASSERT_MSG(fatal_info.size() == sizeof(FatalInfo), "Invalid fatal info buffer size!");
  136. std::memcpy(&info, fatal_info.data(), sizeof(FatalInfo));
  137. ThrowFatalError(system, error_code, fatal_type, info);
  138. IPC::ResponseBuilder rb{ctx, 2};
  139. rb.Push(ResultSuccess);
  140. }
  141. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) {
  142. auto module = std::make_shared<Module>();
  143. std::make_shared<Fatal_P>(module, system)->InstallAsService(service_manager);
  144. std::make_shared<Fatal_U>(module, system)->InstallAsService(service_manager);
  145. }
  146. } // namespace Service::Fatal