kern: implement svc trace

This commit is contained in:
Michael Scire
2020-07-31 19:48:22 -07:00
committed by SciresM
parent f9d68db3f6
commit 920b017677
8 changed files with 203 additions and 19 deletions

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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 <mesosphere.hpp>
namespace ams::kern::svc {
void TraceSvcEntry(const u64 *data) {
MESOSPHERE_KTRACE_SVC_ENTRY(GetCurrentThread().GetSvcId(), data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
}
void TraceSvcExit(const u64 *data) {
MESOSPHERE_KTRACE_SVC_EXIT(GetCurrentThread().GetSvcId(), data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
}
}

View File

@@ -13,6 +13,7 @@
* 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 <mesosphere/kern_build_config.hpp>
/* ams::kern::arch::arm64::SvcHandler64() */
.section .text._ZN3ams4kern4arch5arm6412SvcHandler64Ev, "ax", %progbits
@@ -83,6 +84,22 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
strb w10, [sp, #(0x120 + 0x12)]
strb w8, [sp, #(0x120 + 0x11)]
/* If we should, trace the svc entry. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING)
sub sp, sp, #0x40
stp x0, x1, [sp, #(8 * 0)]
stp x2, x3, [sp, #(8 * 2)]
stp x4, x5, [sp, #(8 * 4)]
stp x6, x7, [sp, #(8 * 6)]
mov x0, sp
bl _ZN3ams4kern3svc13TraceSvcEntryEPKm
ldp x0, x1, [sp, #(8 * 0)]
ldp x2, x3, [sp, #(8 * 2)]
ldp x4, x5, [sp, #(8 * 4)]
ldp x6, x7, [sp, #(8 * 6)]
add sp, sp, #0x40
#endif
/* Invoke the SVC handler. */
msr daifclr, #2
blr x11
@@ -163,6 +180,22 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
/* Clear our in-SVC note. */
strb wzr, [sp, #(0x120 + 0x12)]
/* If we should, trace the svc exit. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING)
sub sp, sp, #0x40
stp x0, x1, [sp, #(8 * 0)]
stp x2, x3, [sp, #(8 * 2)]
stp x4, x5, [sp, #(8 * 4)]
stp x6, x7, [sp, #(8 * 6)]
mov x0, sp
bl _ZN3ams4kern3svc12TraceSvcExitEPKm
ldp x0, x1, [sp, #(8 * 0)]
ldp x2, x3, [sp, #(8 * 2)]
ldp x4, x5, [sp, #(8 * 4)]
ldp x6, x7, [sp, #(8 * 6)]
add sp, sp, #0x40
#endif
/* Restore registers. */
ldp x30, x8, [sp, #(8 * 30)]
ldp x9, x10, [sp, #(8 * 32)]
@@ -259,6 +292,22 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
strb w15, [sp, #(0x120 + 0x12)]
strb w16, [sp, #(0x120 + 0x11)]
/* If we should, trace the svc entry. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING)
sub sp, sp, #0x40
stp x0, x1, [sp, #(8 * 0)]
stp x2, x3, [sp, #(8 * 2)]
stp x4, x5, [sp, #(8 * 4)]
stp x6, x7, [sp, #(8 * 6)]
mov x0, sp
bl _ZN3ams4kern3svc13TraceSvcEntryEPKm
ldp x0, x1, [sp, #(8 * 0)]
ldp x2, x3, [sp, #(8 * 2)]
ldp x4, x5, [sp, #(8 * 4)]
ldp x6, x7, [sp, #(8 * 6)]
add sp, sp, #0x40
#endif
/* Invoke the SVC handler. */
msr daifclr, #2
blr x19
@@ -327,6 +376,22 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
/* Clear our in-SVC note. */
strb wzr, [sp, #(0x120 + 0x12)]
/* If we should, trace the svc exit. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING)
sub sp, sp, #0x40
stp x0, x1, [sp, #(8 * 0)]
stp x2, x3, [sp, #(8 * 2)]
stp x4, x5, [sp, #(8 * 4)]
stp x6, x7, [sp, #(8 * 6)]
mov x0, sp
bl _ZN3ams4kern3svc12TraceSvcExitEPKm
ldp x0, x1, [sp, #(8 * 0)]
ldp x2, x3, [sp, #(8 * 2)]
ldp x4, x5, [sp, #(8 * 4)]
ldp x6, x7, [sp, #(8 * 6)]
add sp, sp, #0x40
#endif
/* Restore registers. */
ldp x8, x9, [sp, #(8 * 8)]
ldp x10, x11, [sp, #(8 * 10)]

View File

@@ -25,6 +25,7 @@ namespace ams::kern {
constinit KSpinLock g_ktrace_lock;
constinit KVirtualAddress g_ktrace_buffer_address = Null<KVirtualAddress>;
constinit size_t g_ktrace_buffer_size = 0;
constinit u64 g_type_filter = 0;
struct KTraceHeader {
u32 magic;
@@ -47,6 +48,10 @@ namespace ams::kern {
static_assert(util::is_pod<KTraceRecord>::value);
static_assert(sizeof(KTraceRecord) == 0x40);
ALWAYS_INLINE bool IsTypeFiltered(u8 type) {
return (g_type_filter & (UINT64_C(1) << (type & (BITSIZEOF(u64) - 1)))) != 0;
}
}
void KTrace::Initialize(KVirtualAddress address, size_t size) {
@@ -67,6 +72,9 @@ namespace ams::kern {
/* Set the global data. */
g_ktrace_buffer_address = address;
g_ktrace_buffer_size = size;
/* Set the filters to defaults. */
g_type_filter = ~(UINT64_C(0));
}
}
}
@@ -101,4 +109,42 @@ namespace ams::kern {
}
}
void KTrace::PushRecord(u8 type, u64 param0, u64 param1, u64 param2, u64 param3, u64 param4, u64 param5) {
/* Get exclusive access to the trace buffer. */
KScopedInterruptDisable di;
KScopedSpinLock lk(g_ktrace_lock);
/* Check whether we should push the record to the trace buffer. */
if (s_is_active && IsTypeFiltered(type)) {
/* Get the current thread and process. */
KThread &cur_thread = GetCurrentThread();
KProcess *cur_process = GetCurrentProcessPointer();
/* Get the current record index from the header. */
KTraceHeader *header = GetPointer<KTraceHeader>(g_ktrace_buffer_address);
u32 index = header->index;
/* Get the current record. */
KTraceRecord *record = GetPointer<KTraceRecord>(g_ktrace_buffer_address + header->offset + index * sizeof(KTraceRecord));
/* Set the record's data. */
*record = {
.core_id = static_cast<u8>(GetCurrentCoreId()),
.type = type,
.process_id = static_cast<u16>(cur_process != nullptr ? cur_process->GetId() : ~0),
.thread_id = static_cast<u32>(cur_thread.GetId()),
.tick = static_cast<u64>(KHardwareTimer::GetTick()),
.data = { param0, param1, param2, param3, param4, param5 },
};
/* Advance the current index. */
if ((++index) >= header->count) {
index = 0;
}
/* Set the next index. */
header->index = index;
}
}
}