SimpleKernel 1.17.0
Loading...
Searching...
No Matches
interrupt_main.cpp
Go to the documentation of this file.
1
5#include <cpu_io.h>
6
7#include "arch.h"
8#include "basic_info.hpp"
9#include "interrupt.h"
10#include "kernel.h"
11#include "kernel_fdt.hpp"
12#include "kernel_log.hpp"
13#include "kstd_cstdio"
15#include "pl011_singleton.h"
16
18namespace {
25auto HandleException(const char* exception_msg, cpu_io::TrapContext* context,
26 int print_regs = 0) -> void {
27 klog::Err("{}", exception_msg);
29 " ESR_EL1: {:#X}, ELR_EL1: {:#X}, SP_EL0: {:#X}, SP_EL1: {:#X}, "
30 "SPSR_EL1: {:#X}",
31 context->esr_el1, context->elr_el1, context->sp_el0, context->sp_el1,
32 context->spsr_el1);
33
34 if (print_regs == 4) {
35 klog::Err(" x0-x3: {:#X} {:#X} {:#X} {:#X}", context->x0, context->x1,
36 context->x2, context->x3);
37 } else if (print_regs == 8) {
38 klog::Err(" x0-x7: {:#X} {:#X} {:#X} {:#X} {:#X} {:#X} {:#X} {:#X}",
39 context->x0, context->x1, context->x2, context->x3, context->x4,
40 context->x5, context->x6, context->x7);
41 }
42
43 while (true) {
45 }
46}
47} // namespace
48
50extern "C" auto vector_table() -> void;
51
52// 同步异常处理程序
55 -> void {
56 HandleException("Sync Exception at Current EL with SP0", context, 4);
57}
58
61 [[maybe_unused]] cpu_io::TrapContext* context) -> void {
62 klog::Err("IRQ Exception at Current EL with SP0");
63 // 处理 IRQ 中断
64 // ...
65}
66
69 [[maybe_unused]] cpu_io::TrapContext* context) -> void {
70 klog::Err("FIQ Exception at Current EL with SP0");
71 // 处理 FIQ 中断
72 // ...
73}
74
77 -> void {
78 HandleException("Error Exception at Current EL with SP0", context);
79}
80
83 -> void {
84 HandleException("Sync Exception at Current EL with SPx", context, 4);
85}
86
89 -> void {
90 auto cause = cpu_io::ICC_IAR1_EL1::INTID::Get();
91 InterruptSingleton::instance().Do(cause, context);
92}
93
96 [[maybe_unused]] cpu_io::TrapContext* context) -> void {
97 klog::Err("FIQ Exception at Current EL with SPx");
98 // 处理 FIQ 中断
99 // ...
100}
101
104 -> void {
105 HandleException("Error Exception at Current EL with SPx", context);
106}
107
110 -> void {
111 HandleException("Sync Exception at Lower EL using AArch64", context, 8);
112}
113
116 [[maybe_unused]] cpu_io::TrapContext* context) -> void {
117 klog::Err("IRQ Exception at Lower EL using AArch64");
118 // 处理 IRQ 中断
119 // ...
120}
121
124 [[maybe_unused]] cpu_io::TrapContext* context) -> void {
125 klog::Err("FIQ Exception at Lower EL using AArch64");
126 // 处理 FIQ 中断
127 // ...
128}
129
132 -> void {
133 HandleException("Error Exception at Lower EL using AArch64", context);
134}
135
138 -> void {
139 HandleException("Sync Exception at Lower EL using AArch32", context);
140}
141
144 [[maybe_unused]] cpu_io::TrapContext* context) -> void {
145 klog::Err("IRQ Exception at Lower EL using AArch32");
146 // 处理 IRQ 中断
147 // ...
148}
149
152 [[maybe_unused]] cpu_io::TrapContext* context) -> void {
153 klog::Err("FIQ Exception at Lower EL using AArch32");
154 // 处理 FIQ 中断
155 // ...
156}
157
160 -> void {
161 HandleException("Error Exception at Lower EL using AArch32", context);
162}
163
169auto uart_handler(uint64_t cause, cpu_io::TrapContext*) -> uint64_t {
170 Pl011Singleton::instance().HandleInterrupt(
171 [](uint8_t ch) { etl_putchar(ch); });
172 return cause;
173}
174
175auto InterruptInit(int, const char**) -> void {
176 InterruptSingleton::create();
177
178 cpu_io::VBAR_EL1::Write(reinterpret_cast<uint64_t>(vector_table));
179
180 auto uart_intid =
181 KernelFdtSingleton::instance().GetAarch64Intid("arm,pl011").value() +
183
184 klog::Info("uart_intid: {}", uart_intid);
185
186 // 通过统一接口注册 UART 外部中断(先注册 handler,再启用 GIC SPI)
187 InterruptSingleton::instance()
188 .RegisterExternalInterrupt(uart_intid, cpu_io::GetCurrentCoreId(), 0,
189 InterruptDelegate::create<uart_handler>())
190 .or_else([](Error err) -> Expected<void> {
191 klog::Err("Failed to register UART IRQ: {}", err.message());
192 return std::unexpected(err);
193 });
194
196
197 klog::Info("Hello InterruptInit");
198}
199
200auto InterruptInitSMP(int, const char**) -> void {
201 cpu_io::VBAR_EL1::Write(reinterpret_cast<uint64_t>(vector_table));
202
203 InterruptSingleton::instance().SetUp();
204
206
207 klog::Info("Hello InterruptInitSMP");
208}
auto etl_putchar(int c) -> void
早期控制台字符输出
auto irq_lower_el_aarch64_handler(cpu_io::TrapContext *context) -> void
IRQ 异常处理 - Lower EL using AArch64.
auto fiq_current_el_sp0_handler(cpu_io::TrapContext *context) -> void
FIQ 异常处理 - Current EL with SP0.
auto fiq_lower_el_aarch32_handler(cpu_io::TrapContext *context) -> void
FIQ 异常处理 - Lower EL using AArch32.
auto vector_table() -> void
异常向量表入口
auto fiq_lower_el_aarch64_handler(cpu_io::TrapContext *context) -> void
FIQ 异常处理 - Lower EL using AArch64.
auto sync_lower_el_aarch64_handler(cpu_io::TrapContext *context) -> void
同步异常处理 - Lower EL using AArch64
auto fiq_current_el_spx_handler(cpu_io::TrapContext *context) -> void
FIQ 异常处理 - Current EL with SPx.
auto irq_current_el_sp0_handler(cpu_io::TrapContext *context) -> void
IRQ 异常处理 - Current EL with SP0.
auto InterruptInit(int, const char **) -> void
体系结构相关中断初始化
auto error_current_el_spx_handler(cpu_io::TrapContext *context) -> void
错误异常处理 - Current EL with SPx
auto sync_lower_el_aarch32_handler(cpu_io::TrapContext *context) -> void
同步异常处理 - Lower EL using AArch32
auto sync_current_el_spx_handler(cpu_io::TrapContext *context) -> void
同步异常处理 - Current EL with SPx
auto irq_current_el_spx_handler(cpu_io::TrapContext *context) -> void
IRQ 异常处理 - Current EL with SPx.
auto sync_current_el_sp0_handler(cpu_io::TrapContext *context) -> void
同步异常处理 - Current EL with SP0
auto irq_lower_el_aarch32_handler(cpu_io::TrapContext *context) -> void
IRQ 异常处理 - Lower EL using AArch32.
auto error_lower_el_aarch32_handler(cpu_io::TrapContext *context) -> void
错误异常处理 - Lower EL using AArch32
InterruptBase::InterruptDelegate InterruptDelegate
auto error_current_el_sp0_handler(cpu_io::TrapContext *context) -> void
错误异常处理 - Current EL with SP0
auto error_lower_el_aarch64_handler(cpu_io::TrapContext *context) -> void
错误异常处理 - Lower EL using AArch64
auto InterruptInitSMP(int, const char **) -> void
从核的体系结构相关中断初始化
auto uart_handler(uint64_t cause, cpu_io::TrapContext *) -> uint64_t
UART 中断处理函数
static constexpr size_t kSpiBase
Definition gic.h:25
etl::delegate< uint64_t(uint64_t, cpu_io::TrapContext *)> InterruptDelegate
类型安全的中断处理委托
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto GetCurrentCoreId() -> size_t
Definition cpu_io.h:26
void Pause()
Definition cpu_io.h:20
void EnableInterrupt()
Definition cpu_io.h:34
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
错误类型,用于 std::expected
Definition expected.hpp:343
constexpr auto message() const -> const char *
Definition expected.hpp:358