mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-06-02 15:49:48 -04:00
kern: implement uart init + logging
This commit is contained in:
parent
323858cf96
commit
5961151a92
17 changed files with 828 additions and 14 deletions
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* 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>
|
||||
#include "../../../kern_debug_log_impl.hpp"
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
namespace {
|
||||
|
||||
enum UartRegister {
|
||||
UartRegister_THR = 0,
|
||||
UartRegister_IER = 1,
|
||||
UartRegister_FCR = 2,
|
||||
UartRegister_LCR = 3,
|
||||
|
||||
UartRegister_LSR = 5,
|
||||
|
||||
UartRegister_DLL = 0,
|
||||
UartRegister_DLH = 1,
|
||||
};
|
||||
|
||||
KVirtualAddress g_uart_address = 0;
|
||||
|
||||
NOINLINE u32 ReadUartRegister(UartRegister which) {
|
||||
return GetPointer<volatile u32>(g_uart_address)[which];
|
||||
}
|
||||
|
||||
NOINLINE void WriteUartRegister(UartRegister which, u32 value) {
|
||||
GetPointer<volatile u32>(g_uart_address)[which] = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool KDebugLogImpl::Initialize() {
|
||||
/* Set the uart register base address. */
|
||||
g_uart_address = KMemoryLayout::GetUartAddress();
|
||||
|
||||
/* Parameters for uart. */
|
||||
constexpr u32 BaudRate = 115200;
|
||||
constexpr u32 Pllp = 408000000;
|
||||
constexpr u32 Rate = 16 * BaudRate;
|
||||
constexpr u32 Divisor = (Pllp + Rate / 2) / Rate;
|
||||
|
||||
/* Initialize the UART registers. */
|
||||
/* Set Divisor Latch Access bit, to allow access to DLL/DLH */
|
||||
WriteUartRegister(UartRegister_LCR, 0x80);
|
||||
ReadUartRegister(UartRegister_LCR);
|
||||
|
||||
/* Program the divisor into DLL/DLH. */
|
||||
WriteUartRegister(UartRegister_DLL, Divisor & 0xFF);
|
||||
WriteUartRegister(UartRegister_DLH, (Divisor >> 8) & 0xFF);
|
||||
ReadUartRegister(UartRegister_DLH);
|
||||
|
||||
/* Set word length to 3, clear Divisor Latch Access. */
|
||||
WriteUartRegister(UartRegister_LCR, 0x03);
|
||||
ReadUartRegister(UartRegister_LCR);
|
||||
|
||||
/* Disable UART interrupts. */
|
||||
WriteUartRegister(UartRegister_IER, 0x00);
|
||||
|
||||
/* Configure the FIFOO to be enabled and clear receive. */
|
||||
WriteUartRegister(UartRegister_FCR, 0x03);
|
||||
ReadUartRegister(UartRegister_FCR);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void KDebugLogImpl::PutChar(char c) {
|
||||
while (ReadUartRegister(UartRegister_LSR) & 0x100) {
|
||||
/* While the FIFO is full, yield. */
|
||||
__asm__ __volatile__("yield" ::: "memory");
|
||||
}
|
||||
WriteUartRegister(UartRegister_THR, c);
|
||||
cpu::DataSynchronizationBarrier();
|
||||
}
|
||||
|
||||
void KDebugLogImpl::Flush() {
|
||||
while ((ReadUartRegister(UartRegister_LSR) & 0x40) == 0) {
|
||||
/* Wait for the TMTY bit to be one (transmit empty). */
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -22,13 +22,27 @@ namespace ams::kern {
|
|||
constexpr uintptr_t DramPhysicalAddress = 0x80000000;
|
||||
constexpr size_t ReservedEarlyDramSize = 0x60000;
|
||||
|
||||
ALWAYS_INLINE bool SetupUartPhysicalMemoryRegion() {
|
||||
#if defined(MESOSPHERE_DEBUG_LOG_USE_UART_A)
|
||||
return KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70006000, 0x40, KMemoryRegionType_Uart | KMemoryRegionAttr_ShouldKernelMap);
|
||||
#elif defined(MESOSPHERE_DEBUG_LOG_USE_UART_B)
|
||||
return KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70006040, 0x40, KMemoryRegionType_Uart | KMemoryRegionAttr_ShouldKernelMap);
|
||||
#elif defined(MESOSPHERE_DEBUG_LOG_USE_UART_C)
|
||||
return KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70006200, 0x100, KMemoryRegionType_Uart | KMemoryRegionAttr_ShouldKernelMap);
|
||||
#elif defined(MESOSPHERE_DEBUG_LOG_USE_UART_D)
|
||||
return KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70006300, 0x100, KMemoryRegionType_Uart | KMemoryRegionAttr_ShouldKernelMap);
|
||||
#else
|
||||
#error "Unknown Debug UART device!"
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace init {
|
||||
|
||||
void SetupDevicePhysicalMemoryRegions() {
|
||||
/* TODO: Give these constexpr defines somewhere? */
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70006000, 0x40, KMemoryRegionType_Uart | KMemoryRegionAttr_ShouldKernelMap));
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(SetupUartPhysicalMemoryRegion());
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70019000, 0x1000, KMemoryRegionType_MemoryController | KMemoryRegionAttr_NoUserMap));
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x7001C000, 0x1000, KMemoryRegionType_MemoryController0 | KMemoryRegionAttr_NoUserMap));
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x7001D000, 0x1000, KMemoryRegionType_MemoryController1 | KMemoryRegionAttr_NoUserMap));
|
||||
|
|
|
@ -20,6 +20,9 @@ namespace ams::kern {
|
|||
|
||||
namespace {
|
||||
|
||||
/* Global variables for panic. */
|
||||
bool g_call_smc_on_panic;
|
||||
|
||||
/* Global variables for randomness. */
|
||||
/* Incredibly, N really does use std:: randomness... */
|
||||
bool g_initialized_random_generator;
|
||||
|
@ -70,6 +73,20 @@ namespace ams::kern {
|
|||
return value;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE u64 GetConfigU64(smc::ConfigItem which) {
|
||||
u64 value;
|
||||
smc::GetConfig(&value, 1, which);
|
||||
return value;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE u32 GetConfigU32(smc::ConfigItem which) {
|
||||
return static_cast<u32>(GetConfigU64(which));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE bool GetConfigBool(smc::ConfigItem which) {
|
||||
return GetConfigU64(which) != 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Initialization. */
|
||||
|
@ -160,6 +177,44 @@ namespace ams::kern {
|
|||
}
|
||||
}
|
||||
|
||||
/* System Initialization. */
|
||||
void KSystemControl::Initialize() {
|
||||
/* Set IsDebugMode. */
|
||||
{
|
||||
KTargetSystem::SetIsDebugMode(GetConfigBool(smc::ConfigItem::IsDebugMode));
|
||||
|
||||
/* If debug mode, we want to initialize uart logging. */
|
||||
KTargetSystem::EnableDebugLogging(KTargetSystem::IsDebugMode());
|
||||
KDebugLog::Initialize();
|
||||
}
|
||||
|
||||
/* Set Kernel Configuration. */
|
||||
{
|
||||
const auto kernel_config = util::BitPack32{GetConfigU32(smc::ConfigItem::KernelConfiguration)};
|
||||
|
||||
KTargetSystem::EnableDebugMemoryFill(kernel_config.Get<smc::KernelConfiguration::DebugFillMemory>());
|
||||
KTargetSystem::EnableUserExceptionHandlers(kernel_config.Get<smc::KernelConfiguration::EnableUserExceptionHandlers>());
|
||||
KTargetSystem::EnableUserPmuAccess(kernel_config.Get<smc::KernelConfiguration::EnableUserPmuAccess>());
|
||||
|
||||
g_call_smc_on_panic = kernel_config.Get<smc::KernelConfiguration::UseSecureMonitorPanicCall>();
|
||||
}
|
||||
|
||||
/* Set Program Verification. */
|
||||
{
|
||||
/* NOTE: This is used to restrict access to SvcKernelDebug/SvcChangeKernelTraceState. */
|
||||
/* Mesosphere may wish to not require this, as we'd ideally keep ProgramVerification enabled for userland. */
|
||||
KTargetSystem::EnableKernelDebugging(GetConfigBool(smc::ConfigItem::DisableProgramVerification));
|
||||
}
|
||||
|
||||
/* Configure the Kernel Carveout region. */
|
||||
{
|
||||
const auto carveout = KMemoryLayout::GetCarveoutRegionExtents();
|
||||
smc::ConfigureCarveout(0, carveout.GetAddress(), carveout.GetSize());
|
||||
}
|
||||
|
||||
/* TODO: KResourceLimit initialization. */
|
||||
}
|
||||
|
||||
/* Randomness. */
|
||||
void KSystemControl::GenerateRandomBytes(void *dst, size_t size) {
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(size <= 0x38);
|
||||
|
@ -181,8 +236,10 @@ namespace ams::kern {
|
|||
}
|
||||
|
||||
void KSystemControl::StopSystem() {
|
||||
/* Display a panic screen via exosphere. */
|
||||
smc::Panic(0xF00);
|
||||
if (g_call_smc_on_panic) {
|
||||
/* Display a panic screen via secure monitor. */
|
||||
smc::Panic(0xF00);
|
||||
}
|
||||
while (true) { /* ... */ }
|
||||
}
|
||||
|
||||
|
|
|
@ -143,6 +143,20 @@ namespace ams::kern::smc {
|
|||
|
||||
}
|
||||
|
||||
void GetConfig(u64 *out, size_t num_qwords, ConfigItem config_item) {
|
||||
SecureMonitorArguments args = { FunctionId_GetConfig, static_cast<u32>(config_item) };
|
||||
CallPrivilegedSecureMonitorFunction(args);
|
||||
MESOSPHERE_ABORT_UNLESS((static_cast<SmcResult>(args.x[0]) == SmcResult::Success));
|
||||
for (size_t i = 0; i < num_qwords && i < 7; i++) {
|
||||
out[i] = args.x[1 + i];
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigureCarveout(size_t which, uintptr_t address, size_t size) {
|
||||
SecureMonitorArguments args = { FunctionId_ConfigureCarveout, static_cast<u64>(which), static_cast<u64>(address), static_cast<u64>(size) };
|
||||
CallPrivilegedSecureMonitorFunction(args);
|
||||
MESOSPHERE_ABORT_UNLESS((static_cast<SmcResult>(args.x[0]) == SmcResult::Success));
|
||||
}
|
||||
|
||||
void GenerateRandomBytes(void *dst, size_t size) {
|
||||
/* Setup for call. */
|
||||
|
|
|
@ -85,6 +85,8 @@ namespace ams::kern::smc {
|
|||
|
||||
/* TODO: Rest of Secure Monitor API. */
|
||||
void GenerateRandomBytes(void *dst, size_t size);
|
||||
void GetConfig(u64 *out, size_t num_qwords, ConfigItem config_item);
|
||||
void ConfigureCarveout(size_t which, uintptr_t address, size_t size);
|
||||
void NORETURN Panic(u32 color);
|
||||
|
||||
namespace init {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue