kern: SvcChangeKernelTraceState

This commit is contained in:
Michael Scire 2020-07-31 17:01:01 -07:00 committed by SciresM
parent 89f1c0ce33
commit f9d68db3f6
10 changed files with 279 additions and 45 deletions

View file

@ -338,39 +338,52 @@ namespace ams::kern::board::nintendo::nx {
}
size_t KSystemControl::Init::GetApplicationPoolSize() {
switch (GetMemoryArrangeForInit()) {
case smc::MemoryArrangement_4GB:
default:
return 3285_MB;
case smc::MemoryArrangement_4GBForAppletDev:
return 2048_MB;
case smc::MemoryArrangement_4GBForSystemDev:
return 3285_MB;
case smc::MemoryArrangement_6GB:
return 4916_MB;
case smc::MemoryArrangement_6GBForAppletDev:
return 3285_MB;
case smc::MemoryArrangement_8GB:
return 4916_MB;
}
/* Get the base pool size. */
const size_t base_pool_size = [] ALWAYS_INLINE_LAMBDA () -> size_t {
switch (GetMemoryArrangeForInit()) {
case smc::MemoryArrangement_4GB:
default:
return 3285_MB;
case smc::MemoryArrangement_4GBForAppletDev:
return 2048_MB;
case smc::MemoryArrangement_4GBForSystemDev:
return 3285_MB;
case smc::MemoryArrangement_6GB:
return 4916_MB;
case smc::MemoryArrangement_6GBForAppletDev:
return 3285_MB;
case smc::MemoryArrangement_8GB:
return 4916_MB;
}
}();
/* Return (possibly) adjusted size. */
return base_pool_size;
}
size_t KSystemControl::Init::GetAppletPoolSize() {
switch (GetMemoryArrangeForInit()) {
case smc::MemoryArrangement_4GB:
default:
return 507_MB;
case smc::MemoryArrangement_4GBForAppletDev:
return 1554_MB;
case smc::MemoryArrangement_4GBForSystemDev:
return 448_MB;
case smc::MemoryArrangement_6GB:
return 562_MB;
case smc::MemoryArrangement_6GBForAppletDev:
return 2193_MB;
case smc::MemoryArrangement_8GB:
return 2193_MB;
}
/* Get the base pool size. */
const size_t base_pool_size = [] ALWAYS_INLINE_LAMBDA () -> size_t {
switch (GetMemoryArrangeForInit()) {
case smc::MemoryArrangement_4GB:
default:
return 507_MB;
case smc::MemoryArrangement_4GBForAppletDev:
return 1554_MB;
case smc::MemoryArrangement_4GBForSystemDev:
return 448_MB;
case smc::MemoryArrangement_6GB:
return 562_MB;
case smc::MemoryArrangement_6GBForAppletDev:
return 2193_MB;
case smc::MemoryArrangement_8GB:
return 2193_MB;
}
}();
/* Return (possibly) adjusted size. */
constexpr size_t ExtraSystemMemoryForAtmosphere = 33_MB;
return base_pool_size - ExtraSystemMemoryForAtmosphere - KTraceBufferSize;
}
size_t KSystemControl::Init::GetMinimumNonSecureSystemPoolSize() {
@ -461,6 +474,12 @@ namespace ams::kern::board::nintendo::nx {
g_secure_applet_memory_address = Kernel::GetMemoryManager().AllocateContinuous(SecureAppletMemorySize / PageSize, 1, SecureAppletAllocateOption);
MESOSPHERE_ABORT_UNLESS(g_secure_applet_memory_address != Null<KVirtualAddress>);
}
/* Initialize KTrace. */
if constexpr (IsKTraceEnabled) {
const auto &ktrace = KMemoryLayout::GetKernelTraceBufferRegion();
KTrace::Initialize(ktrace.GetAddress(), ktrace.GetSize());
}
}
u32 KSystemControl::GetInitialProcessBinaryPool() {

View file

@ -227,7 +227,9 @@ namespace ams::kern {
void SetupPoolPartitionMemoryRegions() {
/* Start by identifying the extents of the DRAM memory region. */
const auto dram_extents = KMemoryLayout::GetPhysicalMemoryRegionTree().GetDerivedRegionExtents(KMemoryRegionType_Dram);
const auto dram_extents = KMemoryLayout::GetMainMemoryPhysicalExtents();
const uintptr_t pool_end = dram_extents.GetEndAddress() - KTraceBufferSize;
/* Get Application and Applet pool sizes. */
const size_t application_pool_size = KSystemControl::Init::GetApplicationPoolSize();
@ -242,7 +244,7 @@ namespace ams::kern {
const uintptr_t pool_partitions_start = KMemoryLayout::GetPhysicalMemoryRegionTree().FindFirstRegionByTypeAttr(KMemoryRegionType_DramPoolPartition)->GetAddress();
/* Decide on starting addresses for our pools. */
const uintptr_t application_pool_start = dram_extents.GetEndAddress() - application_pool_size;
const uintptr_t application_pool_start = pool_end - application_pool_size;
const uintptr_t applet_pool_start = application_pool_start - applet_pool_size;
const uintptr_t unsafe_system_pool_start = std::min(kernel_dram_start + CarveoutSizeMax, util::AlignDown(applet_pool_start - unsafe_system_pool_min_size, CarveoutAlignment));
const size_t unsafe_system_pool_size = applet_pool_start - unsafe_system_pool_start;

View file

@ -0,0 +1,104 @@
/*
* 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 {
/* Static initializations. */
constinit bool KTrace::s_is_active = false;
namespace {
constinit KSpinLock g_ktrace_lock;
constinit KVirtualAddress g_ktrace_buffer_address = Null<KVirtualAddress>;
constinit size_t g_ktrace_buffer_size = 0;
struct KTraceHeader {
u32 magic;
u32 offset;
u32 index;
u32 count;
static constexpr u32 Magic = util::FourCC<'K','T','R','0'>::Code;
};
static_assert(util::is_pod<KTraceHeader>::value);
struct KTraceRecord {
u8 core_id;
u8 type;
u16 process_id;
u32 thread_id;
u64 tick;
u64 data[6];
};
static_assert(util::is_pod<KTraceRecord>::value);
static_assert(sizeof(KTraceRecord) == 0x40);
}
void KTrace::Initialize(KVirtualAddress address, size_t size) {
/* Only perform tracing when on development hardware. */
if (KTargetSystem::IsDebugMode()) {
const size_t offset = util::AlignUp(sizeof(KTraceHeader), sizeof(KTraceRecord));
if (offset < size) {
/* Clear the trace buffer. */
std::memset(GetVoidPointer(address), 0, size);
/* Initialize the KTrace header. */
KTraceHeader *header = GetPointer<KTraceHeader>(address);
header->magic = KTraceHeader::Magic;
header->offset = offset;
header->index = 0;
header->count = (size - offset) / sizeof(KTraceRecord);
/* Set the global data. */
g_ktrace_buffer_address = address;
g_ktrace_buffer_size = size;
}
}
}
void KTrace::Start() {
if (g_ktrace_buffer_address != Null<KVirtualAddress>) {
/* Get exclusive access to the trace buffer. */
KScopedInterruptDisable di;
KScopedSpinLock lk(g_ktrace_lock);
/* Reset the header. */
KTraceHeader *header = GetPointer<KTraceHeader>(g_ktrace_buffer_address);
header->index = 0;
/* Reset the records. */
KTraceRecord *records = GetPointer<KTraceRecord>(g_ktrace_buffer_address + header->offset);
std::memset(records, 0, sizeof(*records) * header->count);
/* Note that we're active. */
s_is_active = true;
}
}
void KTrace::Stop() {
if (g_ktrace_buffer_address != Null<KVirtualAddress>) {
/* Get exclusive access to the trace buffer. */
KScopedInterruptDisable di;
KScopedSpinLock lk(g_ktrace_lock);
/* Note that we're paused. */
s_is_active = false;
}
}
}

View file

@ -157,6 +157,10 @@ namespace ams::kern {
PrintMemoryRegion(" SystemUnsafe", KMemoryLayout::GetKernelSystemNonSecurePoolRegionPhysicalExtents());
PrintMemoryRegion(" Applet", KMemoryLayout::GetKernelAppletPoolRegionPhysicalExtents());
PrintMemoryRegion(" Application", KMemoryLayout::GetKernelApplicationPoolRegionPhysicalExtents());
if constexpr (IsKTraceEnabled) {
MESOSPHERE_LOG(" Debug\n");
PrintMemoryRegion(" Trace Buffer", KMemoryLayout::GetKernelTraceBufferRegionPhysicalExtents());
}
MESOSPHERE_LOG("\n");
}

View file

@ -39,12 +39,12 @@ namespace ams::kern::svc {
switch (kern_trace_state) {
case ams::svc::KernelTraceState_Enabled:
{
/* TODO: MESOSPHERE_KTRACE_RESUME(); */
MESOSPHERE_KTRACE_RESUME();
}
break;
case ams::svc::KernelTraceState_Disabled:
{
/* TODO: MESOSPHERE_KTRACE_PAUSE(); */
MESOSPHERE_KTRACE_PAUSE();
}
break;
default: