2019-07-19 02:09:35 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-07-19 02:09:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "fatal_config.hpp"
|
|
|
|
#include "fatal_debug.hpp"
|
|
|
|
#include "fatal_service.hpp"
|
|
|
|
#include "fatal_service_for_self.hpp"
|
|
|
|
#include "fatal_event_manager.hpp"
|
|
|
|
#include "fatal_task.hpp"
|
|
|
|
|
2019-10-24 09:30:10 +00:00
|
|
|
namespace ams::fatal::srv {
|
2019-07-19 02:09:35 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Service Context. */
|
|
|
|
class ServiceContext {
|
|
|
|
private:
|
|
|
|
ThrowContext context;
|
|
|
|
FatalEventManager event_manager;
|
|
|
|
bool has_thrown;
|
|
|
|
private:
|
|
|
|
Result TrySetHasThrown() {
|
2019-10-24 08:40:44 +00:00
|
|
|
R_UNLESS(!this->has_thrown, ResultAlreadyThrown());
|
2019-07-19 02:09:35 +00:00
|
|
|
this->has_thrown = true;
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
ServiceContext() {
|
|
|
|
this->context.ClearState();
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(eventCreate(&this->context.erpt_event, false));
|
|
|
|
R_ABORT_UNLESS(eventCreate(&this->context.battery_event, false));
|
2019-07-19 02:09:35 +00:00
|
|
|
this->has_thrown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result GetEvent(Handle *out) {
|
|
|
|
return this->event_manager.GetEvent(out);
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
Result ThrowFatal(Result result, os::ProcessId process_id) {
|
2019-10-27 22:57:30 +00:00
|
|
|
return this->ThrowFatalWithCpuContext(result, process_id, FatalPolicy_ErrorReportAndErrorScreen, {});
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 22:57:30 +00:00
|
|
|
Result ThrowFatalWithPolicy(Result result, os::ProcessId process_id, FatalPolicy policy) {
|
2019-10-24 08:40:44 +00:00
|
|
|
return this->ThrowFatalWithCpuContext(result, process_id, policy, {});
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 22:57:30 +00:00
|
|
|
Result ThrowFatalWithCpuContext(Result result, os::ProcessId process_id, FatalPolicy policy, const CpuContext &cpu_ctx);
|
2019-07-19 02:09:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Context global. */
|
|
|
|
ServiceContext g_context;
|
|
|
|
|
|
|
|
/* Throw implementation. */
|
2019-10-27 22:57:30 +00:00
|
|
|
Result ServiceContext::ThrowFatalWithCpuContext(Result result, os::ProcessId process_id, FatalPolicy policy, const CpuContext &cpu_ctx) {
|
2019-07-19 02:09:35 +00:00
|
|
|
/* We don't support Error Report only fatals. */
|
2019-10-27 22:57:30 +00:00
|
|
|
R_UNLESS(policy != FatalPolicy_ErrorReport, ResultSuccess());
|
2019-07-19 02:09:35 +00:00
|
|
|
|
|
|
|
/* Note that we've thrown fatal. */
|
|
|
|
R_TRY(this->TrySetHasThrown());
|
|
|
|
|
|
|
|
/* At this point we have exclusive access to this->context. */
|
2019-10-24 08:40:44 +00:00
|
|
|
this->context.result = result;
|
2019-07-19 02:09:35 +00:00
|
|
|
this->context.cpu_ctx = cpu_ctx;
|
|
|
|
|
|
|
|
/* Cap the stack trace to a sane limit. */
|
|
|
|
if (cpu_ctx.architecture == CpuContext::Architecture_Aarch64) {
|
|
|
|
this->context.cpu_ctx.aarch64_ctx.stack_trace_size = std::max(size_t(this->context.cpu_ctx.aarch64_ctx.stack_trace_size), aarch64::CpuContext::MaxStackTraceDepth);
|
|
|
|
} else {
|
|
|
|
this->context.cpu_ctx.aarch32_ctx.stack_trace_size = std::max(size_t(this->context.cpu_ctx.aarch32_ctx.stack_trace_size), aarch32::CpuContext::MaxStackTraceDepth);
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
/* Get program id. */
|
|
|
|
pm::info::GetProgramId(&this->context.program_id, process_id);
|
|
|
|
this->context.is_creport = (this->context.program_id == ncm::ProgramId::Creport);
|
2019-07-19 02:09:35 +00:00
|
|
|
|
|
|
|
if (!this->context.is_creport) {
|
|
|
|
/* On firmware version 2.0.0, use debugging SVCs to collect information. */
|
2019-10-18 02:39:22 +00:00
|
|
|
if (hos::GetVersion() >= hos::Version_200) {
|
2019-07-19 02:09:35 +00:00
|
|
|
fatal::srv::TryCollectDebugInformation(&this->context, process_id);
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-28 04:43:01 +00:00
|
|
|
/* We received info from creport. Parse program id from afsr0. */
|
2019-07-19 02:09:35 +00:00
|
|
|
if (cpu_ctx.architecture == CpuContext::Architecture_Aarch64) {
|
2019-10-28 04:43:01 +00:00
|
|
|
this->context.program_id = cpu_ctx.aarch64_ctx.GetProgramIdForAtmosphere();
|
2019-07-19 02:09:35 +00:00
|
|
|
} else {
|
2019-10-28 04:43:01 +00:00
|
|
|
this->context.program_id = cpu_ctx.aarch32_ctx.GetProgramIdForAtmosphere();
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decide whether to generate a report. */
|
2019-10-27 22:57:30 +00:00
|
|
|
this->context.generate_error_report = (policy == FatalPolicy_ErrorReportAndErrorScreen);
|
2019-07-19 02:09:35 +00:00
|
|
|
|
|
|
|
/* Adjust error code (2000-0000 -> 2162-0002). */
|
2019-10-24 08:40:44 +00:00
|
|
|
if (R_SUCCEEDED(this->context.result)) {
|
|
|
|
this->context.result = err::ResultSystemModuleAborted();
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (policy) {
|
2019-10-27 22:57:30 +00:00
|
|
|
case FatalPolicy_ErrorReportAndErrorScreen:
|
|
|
|
case FatalPolicy_ErrorScreen:
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Signal that we're throwing. */
|
|
|
|
this->event_manager.SignalEvents();
|
|
|
|
|
|
|
|
if (GetFatalConfig().ShouldTransitionToFatal()) {
|
|
|
|
RunTasks(&this->context);
|
|
|
|
}
|
|
|
|
break;
|
2019-09-28 22:13:20 +00:00
|
|
|
/* N aborts here. Should we just return an error code? */
|
2019-10-24 09:30:10 +00:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
Result ThrowFatalForSelf(Result result) {
|
2019-10-27 22:57:30 +00:00
|
|
|
return g_context.ThrowFatalWithPolicy(result, os::GetCurrentProcessId(), FatalPolicy_ErrorScreen);
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
Result UserService::ThrowFatal(Result result, const sf::ClientProcessId &client_pid) {
|
|
|
|
return g_context.ThrowFatal(result, client_pid.GetValue());
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 22:57:30 +00:00
|
|
|
Result UserService::ThrowFatalWithPolicy(Result result, const sf::ClientProcessId &client_pid, FatalPolicy policy) {
|
2019-10-24 08:40:44 +00:00
|
|
|
return g_context.ThrowFatalWithPolicy(result, client_pid.GetValue(), policy);
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 22:57:30 +00:00
|
|
|
Result UserService::ThrowFatalWithCpuContext(Result result, const sf::ClientProcessId &client_pid, FatalPolicy policy, const CpuContext &cpu_ctx) {
|
2019-10-24 08:40:44 +00:00
|
|
|
return g_context.ThrowFatalWithCpuContext(result, client_pid.GetValue(), policy, cpu_ctx);
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 02:39:22 +00:00
|
|
|
Result PrivateService::GetFatalEvent(sf::OutCopyHandle out_h) {
|
2019-07-19 02:09:35 +00:00
|
|
|
return g_context.GetEvent(out_h.GetHandlePointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|