2019-07-19 04:09:35 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-07-19 04:09:35 +02: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/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-07-19 04:09:35 +02:00
|
|
|
#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 11:30:10 +02:00
|
|
|
namespace ams::fatal::srv {
|
2019-07-19 04:09:35 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Service Context. */
|
|
|
|
class ServiceContext {
|
|
|
|
private:
|
2021-10-10 09:14:06 +02:00
|
|
|
os::Event m_erpt_event;
|
|
|
|
os::Event m_battery_event;
|
|
|
|
ThrowContext m_context;
|
|
|
|
FatalEventManager m_event_manager;
|
|
|
|
bool m_has_thrown;
|
2019-07-19 04:09:35 +02:00
|
|
|
private:
|
|
|
|
Result TrySetHasThrown() {
|
2021-10-10 09:14:06 +02:00
|
|
|
R_UNLESS(!m_has_thrown, fatal::ResultAlreadyThrown());
|
|
|
|
m_has_thrown = true;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
public:
|
2020-04-08 11:21:35 +02:00
|
|
|
ServiceContext()
|
2021-10-10 09:14:06 +02:00
|
|
|
: m_erpt_event(os::EventClearMode_ManualClear), m_battery_event(os::EventClearMode_ManualClear),
|
|
|
|
m_context(std::addressof(m_erpt_event), std::addressof(m_battery_event)), m_has_thrown(false)
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* ... */
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
Result GetEvent(const os::SystemEventType **out) {
|
2021-10-10 09:14:06 +02:00
|
|
|
return m_event_manager.GetEvent(out);
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
Result ThrowFatal(Result result, os::ProcessId process_id) {
|
2019-10-27 23:57:30 +01:00
|
|
|
return this->ThrowFatalWithCpuContext(result, process_id, FatalPolicy_ErrorReportAndErrorScreen, {});
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 23:57:30 +01:00
|
|
|
Result ThrowFatalWithPolicy(Result result, os::ProcessId process_id, FatalPolicy policy) {
|
2019-10-24 10:40:44 +02:00
|
|
|
return this->ThrowFatalWithCpuContext(result, process_id, policy, {});
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 23:57:30 +01:00
|
|
|
Result ThrowFatalWithCpuContext(Result result, os::ProcessId process_id, FatalPolicy policy, const CpuContext &cpu_ctx);
|
2019-07-19 04:09:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Context global. */
|
|
|
|
ServiceContext g_context;
|
|
|
|
|
|
|
|
/* Throw implementation. */
|
2019-10-27 23:57:30 +01:00
|
|
|
Result ServiceContext::ThrowFatalWithCpuContext(Result result, os::ProcessId process_id, FatalPolicy policy, const CpuContext &cpu_ctx) {
|
2020-03-08 09:06:23 +01:00
|
|
|
/* We don't support Error-Report-only fatals. */
|
|
|
|
R_SUCCEED_IF(policy == FatalPolicy_ErrorReport);
|
2019-07-19 04:09:35 +02:00
|
|
|
|
|
|
|
/* Note that we've thrown fatal. */
|
|
|
|
R_TRY(this->TrySetHasThrown());
|
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
/* At this point we have exclusive access to m_context. */
|
|
|
|
m_context.result = result;
|
|
|
|
m_context.cpu_ctx = cpu_ctx;
|
2019-07-19 04:09:35 +02:00
|
|
|
|
|
|
|
/* Cap the stack trace to a sane limit. */
|
|
|
|
if (cpu_ctx.architecture == CpuContext::Architecture_Aarch64) {
|
2021-10-10 09:14:06 +02:00
|
|
|
m_context.cpu_ctx.aarch64_ctx.stack_trace_size = std::max(size_t(m_context.cpu_ctx.aarch64_ctx.stack_trace_size), aarch64::CpuContext::MaxStackTraceDepth);
|
2019-07-19 04:09:35 +02:00
|
|
|
} else {
|
2021-10-10 09:14:06 +02:00
|
|
|
m_context.cpu_ctx.aarch32_ctx.stack_trace_size = std::max(size_t(m_context.cpu_ctx.aarch32_ctx.stack_trace_size), aarch32::CpuContext::MaxStackTraceDepth);
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
/* Get program id. */
|
2021-10-10 09:14:06 +02:00
|
|
|
pm::info::GetProgramId(std::addressof(m_context.program_id), process_id);
|
|
|
|
m_context.is_creport = (m_context.program_id == ncm::SystemProgramId::Creport);
|
2019-07-19 04:09:35 +02:00
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
if (!m_context.is_creport) {
|
2019-07-19 04:09:35 +02:00
|
|
|
/* On firmware version 2.0.0, use debugging SVCs to collect information. */
|
2020-04-14 07:19:44 +02:00
|
|
|
if (hos::GetVersion() >= hos::Version_2_0_0) {
|
2021-10-10 09:14:06 +02:00
|
|
|
fatal::srv::TryCollectDebugInformation(std::addressof(m_context), process_id);
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-28 05:43:01 +01:00
|
|
|
/* We received info from creport. Parse program id from afsr0. */
|
2019-07-19 04:09:35 +02:00
|
|
|
if (cpu_ctx.architecture == CpuContext::Architecture_Aarch64) {
|
2021-10-10 09:14:06 +02:00
|
|
|
m_context.program_id = cpu_ctx.aarch64_ctx.GetProgramIdForAtmosphere();
|
2019-07-19 04:09:35 +02:00
|
|
|
} else {
|
2021-10-10 09:14:06 +02:00
|
|
|
m_context.program_id = cpu_ctx.aarch32_ctx.GetProgramIdForAtmosphere();
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decide whether to generate a report. */
|
2021-10-10 09:14:06 +02:00
|
|
|
m_context.generate_error_report = (policy == FatalPolicy_ErrorReportAndErrorScreen);
|
2019-07-19 04:09:35 +02:00
|
|
|
|
2021-04-30 13:21:03 +02:00
|
|
|
/* Adjust error code (ResultSuccess()/2000-0000 -> err::ResultSystemProgramAbort()/2162-0002). */
|
2021-10-10 09:14:06 +02:00
|
|
|
if (R_SUCCEEDED(m_context.result)) {
|
|
|
|
m_context.result = err::ResultSystemProgramAbort();
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (policy) {
|
2019-10-27 23:57:30 +01:00
|
|
|
case FatalPolicy_ErrorReportAndErrorScreen:
|
|
|
|
case FatalPolicy_ErrorScreen:
|
2019-07-19 04:09:35 +02:00
|
|
|
/* Signal that we're throwing. */
|
2021-10-10 09:14:06 +02:00
|
|
|
m_event_manager.SignalEvents();
|
2019-07-19 04:09:35 +02:00
|
|
|
|
|
|
|
if (GetFatalConfig().ShouldTransitionToFatal()) {
|
2021-10-10 09:14:06 +02:00
|
|
|
RunTasks(std::addressof(m_context));
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-09-29 00:13:20 +02:00
|
|
|
/* N aborts here. Should we just return an error code? */
|
2019-10-24 11:30:10 +02:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
Result ThrowFatalForSelf(Result result) {
|
2019-10-27 23:57:30 +01:00
|
|
|
return g_context.ThrowFatalWithPolicy(result, os::GetCurrentProcessId(), FatalPolicy_ErrorScreen);
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:07:23 +02:00
|
|
|
Result Service::ThrowFatal(Result result, const sf::ClientProcessId &client_pid) {
|
2019-10-24 10:40:44 +02:00
|
|
|
return g_context.ThrowFatal(result, client_pid.GetValue());
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:07:23 +02:00
|
|
|
Result Service::ThrowFatalWithPolicy(Result result, const sf::ClientProcessId &client_pid, FatalPolicy policy) {
|
2019-10-24 10:40:44 +02:00
|
|
|
return g_context.ThrowFatalWithPolicy(result, client_pid.GetValue(), policy);
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:07:23 +02:00
|
|
|
Result Service::ThrowFatalWithCpuContext(Result result, const sf::ClientProcessId &client_pid, FatalPolicy policy, const CpuContext &cpu_ctx) {
|
2019-10-24 10:40:44 +02:00
|
|
|
return g_context.ThrowFatalWithCpuContext(result, client_pid.GetValue(), policy, cpu_ctx);
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:48:47 +01:00
|
|
|
Result Service::GetFatalEvent(sf::OutCopyHandle out_h) {
|
2020-04-08 11:21:35 +02:00
|
|
|
const os::SystemEventType *event;
|
|
|
|
R_TRY(g_context.GetEvent(std::addressof(event)));
|
2021-10-05 09:11:36 +02:00
|
|
|
out_h.SetValue(os::GetReadableHandleOfSystemEvent(event), false);
|
2020-04-08 11:21:35 +02:00
|
|
|
return ResultSuccess();
|
2019-07-19 04:09:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|