mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-30 14:35:17 -04:00
kern: implement debug register/vectors init
This commit is contained in:
parent
7c703903ea
commit
7820e5b759
3 changed files with 297 additions and 2 deletions
141
mesosphere/kernel/source/arch/arm64/exception_vectors.s
Normal file
141
mesosphere/kernel/source/arch/arm64/exception_vectors.s
Normal file
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/* Some macros taken from https://github.com/ARM-software/arm-trusted-firmware/blob/master/include/common/aarch64/asm_macros.S */
|
||||
/*
|
||||
* Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
/*
|
||||
* Declare the exception vector table, enforcing it is aligned on a
|
||||
* 2KB boundary, as required by the ARMv8 architecture.
|
||||
* Use zero bytes as the fill value to be stored in the padding bytes
|
||||
* so that it inserts illegal AArch64 instructions. This increases
|
||||
* security, robustness and potentially facilitates debugging.
|
||||
*/
|
||||
.macro vector_base label, section_name=.vectors
|
||||
.section \section_name, "ax"
|
||||
.align 11, 0
|
||||
\label:
|
||||
.endm
|
||||
|
||||
/*
|
||||
* Create an entry in the exception vector table, enforcing it is
|
||||
* aligned on a 128-byte boundary, as required by the ARMv8 architecture.
|
||||
* Use zero bytes as the fill value to be stored in the padding bytes
|
||||
* so that it inserts illegal AArch64 instructions. This increases
|
||||
* security, robustness and potentially facilitates debugging.
|
||||
*/
|
||||
.macro vector_entry label, section_name=.vectors
|
||||
.cfi_sections .debug_frame
|
||||
.section \section_name, "ax"
|
||||
.align 7, 0
|
||||
.type \label, %function
|
||||
.func \label
|
||||
.cfi_startproc
|
||||
\label:
|
||||
.endm
|
||||
|
||||
/*
|
||||
* This macro verifies that the given vector doesnt exceed the
|
||||
* architectural limit of 32 instructions. This is meant to be placed
|
||||
* immediately after the last instruction in the vector. It takes the
|
||||
* vector entry as the parameter
|
||||
*/
|
||||
.macro check_vector_size since
|
||||
.endfunc
|
||||
.cfi_endproc
|
||||
.if (. - \since) > (32 * 4)
|
||||
.error "Vector exceeds 32 instructions"
|
||||
.endif
|
||||
.endm
|
||||
|
||||
/* Actual Vectors for Kernel. */
|
||||
.global _ZN3ams4kern16ExceptionVectorsEv
|
||||
vector_base _ZN3ams4kern16ExceptionVectorsEv
|
||||
|
||||
/* Current EL, SP0 */
|
||||
.global unknown_exception
|
||||
unknown_exception:
|
||||
vector_entry synch_sp0
|
||||
/* Just infinite loop. */
|
||||
b unknown_exception
|
||||
check_vector_size synch_sp0
|
||||
|
||||
vector_entry irq_sp0
|
||||
b unknown_exception
|
||||
check_vector_size irq_sp0
|
||||
|
||||
vector_entry fiq_sp0
|
||||
b unknown_exception
|
||||
check_vector_size fiq_sp0
|
||||
|
||||
vector_entry serror_sp0
|
||||
b unknown_exception
|
||||
check_vector_size serror_sp0
|
||||
|
||||
/* Current EL, SPx */
|
||||
vector_entry synch_spx
|
||||
b unknown_exception
|
||||
check_vector_size synch_spx
|
||||
|
||||
vector_entry irq_spx
|
||||
b unknown_exception
|
||||
check_vector_size irq_spx
|
||||
|
||||
vector_entry fiq_spx
|
||||
b unknown_exception
|
||||
check_vector_size fiq_spx
|
||||
|
||||
vector_entry serror_spx
|
||||
b unknown_exception
|
||||
check_vector_size serror_spx
|
||||
|
||||
/* Lower EL, A64 */
|
||||
vector_entry synch_a64
|
||||
b unknown_exception
|
||||
check_vector_size synch_a64
|
||||
|
||||
vector_entry irq_a64
|
||||
b unknown_exception
|
||||
check_vector_size irq_a64
|
||||
|
||||
vector_entry fiq_a64
|
||||
b unknown_exception
|
||||
check_vector_size fiq_a64
|
||||
|
||||
vector_entry serror_a64
|
||||
b unknown_exception
|
||||
check_vector_size serror_a64
|
||||
|
||||
/* Lower EL, A32 */
|
||||
vector_entry synch_a32
|
||||
b unknown_exception
|
||||
check_vector_size synch_a32
|
||||
|
||||
vector_entry irq_a32
|
||||
b unknown_exception
|
||||
check_vector_size irq_a32
|
||||
|
||||
vector_entry fiq_a32
|
||||
b unknown_exception
|
||||
check_vector_size fiq_a32
|
||||
|
||||
vector_entry serror_a32
|
||||
b unknown_exception
|
||||
check_vector_size serror_a32
|
|
@ -18,6 +18,12 @@
|
|||
extern "C" void _start();
|
||||
extern "C" void __end__();
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
void ExceptionVectors();
|
||||
|
||||
}
|
||||
|
||||
namespace ams::kern::init {
|
||||
|
||||
/* Prototypes for functions declared in ASM that we need to reference. */
|
||||
|
@ -310,11 +316,71 @@ namespace ams::kern::init {
|
|||
}
|
||||
|
||||
void InitializeDebugRegisters() {
|
||||
/* TODO */
|
||||
/* Determine how many watchpoints and breakpoints we have */
|
||||
cpu::DebugFeatureRegisterAccessor aa64dfr0;
|
||||
const auto num_watchpoints = aa64dfr0.GetNumWatchpoints();
|
||||
const auto num_breakpoints = aa64dfr0.GetNumBreakpoints();
|
||||
cpu::EnsureInstructionConsistency();
|
||||
|
||||
/* Clear the debug monitor register and the os lock access register. */
|
||||
cpu::MonitorDebugSystemControlRegisterAccessor(0).Store();
|
||||
cpu::EnsureInstructionConsistency();
|
||||
cpu::OsLockAccessRegisterAccessor(0).Store();
|
||||
cpu::EnsureInstructionConsistency();
|
||||
|
||||
/* Clear all debug watchpoints/breakpoints. */
|
||||
#define FOR_I_IN_15_TO_1(HANDLER, ...) \
|
||||
HANDLER(15, ## __VA_ARGS__) HANDLER(14, ## __VA_ARGS__) HANDLER(13, ## __VA_ARGS__) HANDLER(12, ## __VA_ARGS__) \
|
||||
HANDLER(11, ## __VA_ARGS__) HANDLER(10, ## __VA_ARGS__) HANDLER(9, ## __VA_ARGS__) HANDLER(8, ## __VA_ARGS__) \
|
||||
HANDLER(7, ## __VA_ARGS__) HANDLER(6, ## __VA_ARGS__) HANDLER(5, ## __VA_ARGS__) HANDLER(4, ## __VA_ARGS__) \
|
||||
HANDLER(3, ## __VA_ARGS__) HANDLER(2, ## __VA_ARGS__) HANDLER(1, ## __VA_ARGS__)
|
||||
|
||||
#define MESOSPHERE_INITIALIZE_WATCHPOINT_CASE(ID, ...) \
|
||||
case ID: \
|
||||
cpu::SetDbgWcr##ID##El1(__VA_ARGS__); \
|
||||
cpu::SetDbgWvr##ID##El1(__VA_ARGS__); \
|
||||
|
||||
#define MESOSPHERE_INITIALIZE_BREAKPOINT_CASE(ID, ...) \
|
||||
case ID: \
|
||||
cpu::SetDbgBcr##ID##El1(__VA_ARGS__); \
|
||||
cpu::SetDbgBvr##ID##El1(__VA_ARGS__); \
|
||||
[[fallthrough]];
|
||||
|
||||
|
||||
switch (num_watchpoints) {
|
||||
FOR_I_IN_15_TO_1(MESOSPHERE_INITIALIZE_WATCHPOINT_CASE, 0)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
cpu::SetDbgWcr0El1(0);
|
||||
cpu::SetDbgWvr0El1(0);
|
||||
|
||||
switch (num_breakpoints) {
|
||||
FOR_I_IN_15_TO_1(MESOSPHERE_INITIALIZE_BREAKPOINT_CASE, 0)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
cpu::SetDbgBcr0El1(0);
|
||||
cpu::SetDbgBvr0El1(0);
|
||||
|
||||
#undef MESOSPHERE_INITIALIZE_WATCHPOINT_CASE
|
||||
#undef MESOSPHERE_INITIALIZE_BREAKPOINT_CASE
|
||||
#undef FOR_I_IN_15_TO_1
|
||||
|
||||
cpu::EnsureInstructionConsistency();
|
||||
|
||||
/* Initialize the context id register to all 1s. */
|
||||
cpu::ContextIdRegisterAccessor(0).SetProcId(std::numeric_limits<u32>::max()).Store();
|
||||
cpu::EnsureInstructionConsistency();
|
||||
|
||||
/* Configure the debug monitor register. */
|
||||
cpu::MonitorDebugSystemControlRegisterAccessor(0).SetMde(true).SetTdcc(true).Store();
|
||||
cpu::EnsureInstructionConsistency();
|
||||
}
|
||||
|
||||
void InitializeExceptionVectors() {
|
||||
/* TODO */
|
||||
cpu::SetVbarEl1(reinterpret_cast<uintptr_t>(::ams::kern::ExceptionVectors));
|
||||
cpu::EnsureInstructionConsistency();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue