interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / arch / exception.c

118 lines · 3934 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* exception.c -- synchronous exceptions, FIQ/SError, and the IRQ dispatcher.
18 * Any exception other than an armed probe, an IRQ or an M3 EL0 task's
19 * syscall/contained fault (core/m3.c) is fatal: it prints
20 * "report_kind: fault" with ESR/FAR/ELR and resets. */
21#include "arch.h"
22#include "ck_internal.h"
23
24struct ck_probe ck_probe_state;
25
26void ck_exception(struct ck_frame *f, uint64_t vector);
27struct ck_frame *ck_irq_dispatch(struct ck_frame *f);
28
29/* M3 hooks (core/m3.c), set only while its checks run. ck_tick_switch may
30 * return another frame to resume (timer preemption); ck_lower_sync handles
31 * a synchronous exception from an M3 EL0 task (0) or declines it (-1). */
32struct ck_frame *(*ck_tick_switch)(struct ck_frame *f);
33int (*ck_lower_sync)(struct ck_frame *f, uint64_t esr);
34void ck_el2_fatal(uint64_t vector);
35
36static const char *const vec_names[16] = {
37 "sync_el_sp0", "irq_el_sp0", "fiq_el_sp0", "serror_el_sp0",
38 "sync_el_spx", "irq_el_spx", "fiq_el_spx", "serror_el_spx",
39 "sync_lower64", "irq_lower64", "fiq_lower64", "serror_lower64",
40 "sync_lower32", "irq_lower32", "fiq_lower32", "serror_lower32",
41};
42
43void ck_exception(struct ck_frame *f, uint64_t vector)
44{
45 /* Only VBAR_EL1 points here (EL2 uses ck_vectors_el2). */
46 unsigned el = ck_current_el();
47 uint64_t esr = ck_rd(esr_el1), far = ck_rd(far_el1), elr = f->elr;
48 uint64_t ec = (esr >> 26) & 0x3f;
49 /* sync_lower64: an EL0 task of the M3 checks (syscall or contained fault). */
50 if (vector == 8 && ck_lower_sync && ck_lower_sync(f, esr) == 0)
51 return;
52 /* Armed probe: a data abort at the probing load on the current EL. */
53 if (el == 1 && vector == 4 && ec == 0x25 && ck_probe_state.fixup &&
54 elr == ck_probe_state.insn) {
55 ck_probe_state.esr = esr;
56 ck_probe_state.far = far;
57 f->elr = ck_probe_state.fixup;
58 ck_probe_state.fixup = 0;
59 return;
60 }
61 ck_fault_report(vector < 16 ? vec_names[vector] : "unknown", esr, far, elr, el);
62}
63
64/* Any exception taken to EL2 before the drop to EL1 (ck_vectors_el2). */
65void ck_el2_fatal(uint64_t vector)
66{
67 ck_fault_report(vector < 16 ? vec_names[vector] : "unknown", ck_rd(esr_el2), ck_rd(far_el2),
68 ck_rd(elr_el2), 2);
69}
70
71/* ---- IRQ registration (ck.h additions) ---- */
72#define CK_IRQ_MAX 1020u
73static struct {
74 void (*fn)(void *);
75 void *arg;
76} irq_table[CK_IRQ_MAX];
77
78int ck_irq_register(uint32_t intid, void (*fn)(void *arg), void *arg)
79{
80 if (intid >= CK_IRQ_MAX || intid == 30 || !fn)
81 return -1;
82 if (irq_table[intid].fn)
83 return -2;
84 irq_table[intid].arg = arg;
85 irq_table[intid].fn = fn;
86 return 0;
87}
88
89static uint64_t spurious, unhandled;
90
91struct ck_frame *ck_irq_dispatch(struct ck_frame *f)
92{
93 uint32_t id = ck_gic_ack();
94 if (id >= 1020) {
95 spurious++;
96 return f;
97 }
98 if (id == 30)
99 ck_timer_tick(); /* re-arms before EOI */
100 else if (irq_table[id].fn)
101 irq_table[id].fn(irq_table[id].arg);
102 else
103 unhandled++;
104 ck_gic_eoi(id);
105 ck_isb();
106 if (id == 30 && ck_tick_switch)
107 return ck_tick_switch(f);
108 return f;
109}
110
111void ck_irq_cpu_enable(int on)
112{
113 if (on)
114 ck_irq_on();
115 else
116 ck_irq_off();
117 ck_isb();
118}