2020-01-18 06:02:45 +00:00
|
|
|
/*
|
2020-01-24 10:12:37 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2020-01-18 06:02:45 +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/>.
|
|
|
|
*/
|
2020-02-08 19:53:27 +00:00
|
|
|
#ifdef MESOSPHERE_USE_STUBBED_SVC_TABLES
|
2020-02-08 10:49:32 +00:00
|
|
|
#include <mesosphere/kern_debug_log.hpp>
|
2020-02-08 19:53:27 +00:00
|
|
|
#endif
|
|
|
|
|
2020-02-08 10:49:32 +00:00
|
|
|
#include <mesosphere/svc/kern_svc_tables.hpp>
|
2020-01-18 06:02:45 +00:00
|
|
|
#include <vapours/svc/svc_codegen.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern::svc {
|
|
|
|
|
2020-07-29 10:57:40 +00:00
|
|
|
/* Declare special prototypes for the light ipc handlers. */
|
|
|
|
void CallSendSyncRequestLight64();
|
|
|
|
void CallSendSyncRequestLight64From32();
|
|
|
|
|
|
|
|
void CallReplyAndReceiveLight64();
|
|
|
|
void CallReplyAndReceiveLight64From32();
|
|
|
|
|
2020-07-31 12:52:59 +00:00
|
|
|
/* Declare special prototypes for ReturnFromException. */
|
|
|
|
void CallReturnFromException64();
|
|
|
|
void CallReturnFromException64From32();
|
|
|
|
|
2020-07-31 20:33:33 +00:00
|
|
|
/* Declare special prototype for (unsupported) CallCallSecureMonitor64From32. */
|
|
|
|
void CallCallSecureMonitor64From32();
|
|
|
|
|
2020-01-18 06:02:45 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-02-08 19:53:27 +00:00
|
|
|
#ifndef MESOSPHERE_USE_STUBBED_SVC_TABLES
|
2020-02-08 10:49:32 +00:00
|
|
|
#define DECLARE_SVC_STRUCT(ID, RETURN_TYPE, NAME, ...) \
|
|
|
|
class NAME { \
|
|
|
|
private: \
|
|
|
|
using Impl = ::ams::svc::codegen::KernelSvcWrapper<::ams::kern::svc::NAME##64, ::ams::kern::svc::NAME##64From32>; \
|
|
|
|
public: \
|
2020-05-29 07:57:25 +00:00
|
|
|
static NOINLINE void Call64() { return Impl::Call64(); } \
|
2020-02-08 10:49:32 +00:00
|
|
|
static NOINLINE void Call64From32() { return Impl::Call64From32(); } \
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
#define DECLARE_SVC_STRUCT(ID, RETURN_TYPE, NAME, ...) \
|
|
|
|
class NAME { \
|
|
|
|
public: \
|
2020-02-08 19:53:27 +00:00
|
|
|
static NOINLINE void Call64() { MESOSPHERE_PANIC("Stubbed Svc"#NAME"64 was called"); } \
|
|
|
|
static NOINLINE void Call64From32() { MESOSPHERE_PANIC("Stubbed Svc"#NAME"64From32 was called"); } \
|
2020-02-08 10:49:32 +00:00
|
|
|
};
|
|
|
|
#endif
|
2020-01-18 06:02:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Set omit-frame-pointer to prevent GCC from emitting MOV X29, SP instructions. */
|
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC optimize ("omit-frame-pointer")
|
|
|
|
|
|
|
|
AMS_SVC_FOREACH_KERN_DEFINITION(DECLARE_SVC_STRUCT, _)
|
|
|
|
|
|
|
|
#pragma GCC pop_options
|
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
constexpr const std::array<SvcTableEntry, NumSupervisorCalls> SvcTable64From32Impl = [] {
|
|
|
|
std::array<SvcTableEntry, NumSupervisorCalls> table = {};
|
|
|
|
|
|
|
|
#define AMS_KERN_SVC_SET_TABLE_ENTRY(ID, RETURN_TYPE, NAME, ...) \
|
2020-07-14 01:50:37 +00:00
|
|
|
if (table[ID] == nullptr) { table[ID] = NAME::Call64From32; }
|
2020-05-29 07:57:25 +00:00
|
|
|
AMS_SVC_FOREACH_KERN_DEFINITION(AMS_KERN_SVC_SET_TABLE_ENTRY, _)
|
|
|
|
#undef AMS_KERN_SVC_SET_TABLE_ENTRY
|
|
|
|
|
2020-07-29 10:57:40 +00:00
|
|
|
table[svc::SvcId_SendSyncRequestLight] = CallSendSyncRequestLight64From32;
|
|
|
|
table[svc::SvcId_ReplyAndReceiveLight] = CallReplyAndReceiveLight64From32;
|
|
|
|
|
2020-07-31 12:52:59 +00:00
|
|
|
table[svc::SvcId_ReturnFromException] = CallReturnFromException64From32;
|
|
|
|
|
2020-07-31 20:33:33 +00:00
|
|
|
table[svc::SvcId_CallSecureMonitor] = CallCallSecureMonitor64From32;
|
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
return table;
|
|
|
|
}();
|
|
|
|
|
|
|
|
constexpr const std::array<SvcTableEntry, NumSupervisorCalls> SvcTable64Impl = [] {
|
|
|
|
std::array<SvcTableEntry, NumSupervisorCalls> table = {};
|
2020-01-18 06:02:45 +00:00
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
#define AMS_KERN_SVC_SET_TABLE_ENTRY(ID, RETURN_TYPE, NAME, ...) \
|
2020-07-14 01:50:37 +00:00
|
|
|
if (table[ID] == nullptr) { table[ID] = NAME::Call64; }
|
2020-05-29 07:57:25 +00:00
|
|
|
AMS_SVC_FOREACH_KERN_DEFINITION(AMS_KERN_SVC_SET_TABLE_ENTRY, _)
|
|
|
|
#undef AMS_KERN_SVC_SET_TABLE_ENTRY
|
2020-01-18 06:02:45 +00:00
|
|
|
|
2020-07-29 10:57:40 +00:00
|
|
|
table[svc::SvcId_SendSyncRequestLight] = CallSendSyncRequestLight64;
|
|
|
|
table[svc::SvcId_ReplyAndReceiveLight] = CallReplyAndReceiveLight64;
|
|
|
|
|
2020-07-31 12:52:59 +00:00
|
|
|
table[svc::SvcId_ReturnFromException] = CallReturnFromException64;
|
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
return table;
|
|
|
|
}();
|
2020-01-18 06:02:45 +00:00
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
constexpr bool IsValidSvcTable(const std::array<SvcTableEntry, NumSupervisorCalls> &table) {
|
|
|
|
for (size_t i = 0; i < NumSupervisorCalls; i++) {
|
|
|
|
if (table[i] != nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 06:02:45 +00:00
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static_assert(IsValidSvcTable(SvcTable64Impl));
|
|
|
|
static_assert(IsValidSvcTable(SvcTable64From32Impl));
|
|
|
|
|
|
|
|
}
|
2020-01-18 06:02:45 +00:00
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
constinit const std::array<SvcTableEntry, NumSupervisorCalls> SvcTable64 = SvcTable64Impl;
|
2020-01-18 06:02:45 +00:00
|
|
|
|
2020-05-29 07:57:25 +00:00
|
|
|
constinit const std::array<SvcTableEntry, NumSupervisorCalls> SvcTable64From32 = SvcTable64From32Impl;
|
2020-01-18 06:02:45 +00:00
|
|
|
|
2020-07-14 02:16:05 +00:00
|
|
|
void PatchSvcTableEntry(const SvcTableEntry *table, u32 id, SvcTableEntry entry);
|
|
|
|
|
2020-07-14 01:50:37 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* NOTE: Although the SVC tables are constants, our global constructor will run before .rodata is protected R--. */
|
|
|
|
class SvcTablePatcher {
|
|
|
|
private:
|
|
|
|
using SvcTable = std::array<SvcTableEntry, NumSupervisorCalls>;
|
|
|
|
private:
|
|
|
|
static SvcTablePatcher s_instance;
|
|
|
|
private:
|
2020-07-14 02:16:05 +00:00
|
|
|
ALWAYS_INLINE const SvcTableEntry *GetTableData(const SvcTable *table) {
|
2020-07-14 01:50:37 +00:00
|
|
|
if (table != nullptr) {
|
2020-07-14 02:16:05 +00:00
|
|
|
return table->data();
|
2020-07-14 01:50:37 +00:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 02:16:05 +00:00
|
|
|
NOINLINE void PatchTables(const SvcTableEntry *table_64, const SvcTableEntry *table_64_from_32) {
|
2020-07-14 01:50:37 +00:00
|
|
|
/* Get the target firmware. */
|
|
|
|
const auto target_fw = kern::GetTargetFirmware();
|
|
|
|
|
|
|
|
/* 10.0.0 broke the ABI for QueryIoMapping. */
|
|
|
|
if (target_fw < TargetFirmware_10_0_0) {
|
2020-07-14 02:16:05 +00:00
|
|
|
if (table_64) { ::ams::kern::svc::PatchSvcTableEntry(table_64, svc::SvcId_QueryIoMapping, LegacyQueryIoMapping::Call64); }
|
|
|
|
if (table_64_from_32) { ::ams::kern::svc::PatchSvcTableEntry(table_64_from_32, svc::SvcId_QueryIoMapping, LegacyQueryIoMapping::Call64From32); }
|
2020-07-14 01:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 3.0.0 broke the ABI for ContinueDebugEvent. */
|
|
|
|
if (target_fw < TargetFirmware_3_0_0) {
|
2020-07-14 02:16:05 +00:00
|
|
|
if (table_64) { ::ams::kern::svc::PatchSvcTableEntry(table_64, svc::SvcId_ContinueDebugEvent, LegacyContinueDebugEvent::Call64); }
|
|
|
|
if (table_64_from_32) { ::ams::kern::svc::PatchSvcTableEntry(table_64_from_32, svc::SvcId_ContinueDebugEvent, LegacyContinueDebugEvent::Call64From32); }
|
2020-07-14 01:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
SvcTablePatcher(const SvcTable *table_64, const SvcTable *table_64_from_32) {
|
2020-07-14 02:16:05 +00:00
|
|
|
PatchTables(GetTableData(table_64), GetTableData(table_64_from_32));
|
2020-07-14 01:50:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SvcTablePatcher SvcTablePatcher::s_instance(std::addressof(SvcTable64), std::addressof(SvcTable64From32));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-18 06:02:45 +00:00
|
|
|
}
|