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 / core / report.c

131 lines · 3980 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/* report.c -- report headers, panic, fault report, PSCI reset.
18 * Report keys follow the Rust handoff (report_version, aienos_commit,
19 * report_kind, last_stage) so the same scripts read both kernels. */
20#include "arch.h"
21#include "ck_internal.h"
22
23#ifndef AIENOS_COMMIT
24#define AIENOS_COMMIT "unknown"
25#endif
26
27static const char *stage = "uefi_entry";
28static int psci_hvc;
29static int in_fatal;
30
31const char *ck_commit(void) { return AIENOS_COMMIT; }
32uint32_t ck_boot_count_hint(void) { return 0; }
33void ck_set_stage(const char *name)
34{
35 stage = name;
36 if (ck_pi5_stage)
37 ck_pi5_stage(name);
38}
39const char *ck_stage_name(void) { return stage; }
40
41void ck_report_header(const char *kind)
42{
43 ck_printf("report_version: 1\naienos_commit: %s\nreport_kind: %s\nlast_stage: %s\n",
44 ck_commit(), kind, stage);
45}
46
47void ck_psci_configure(const void *fadt)
48{
49 uint16_t flags = 0;
50 if (fadt && ck_fadt_arm_boot_arch(fadt, &flags) == 0)
51 psci_hvc = (flags & 2) != 0;
52}
53
54/* SMCCC v1.0: x4-x17 may be corrupted by the callee. */
55#define PSCI_CLOBBERS "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", \
56 "x15", "x16", "x17", "memory"
57
58int64_t ck_psci_call(uint64_t fn, uint64_t a1, uint64_t a2, uint64_t a3)
59{
60 register uint64_t x0 __asm__("x0") = fn;
61 register uint64_t x1 __asm__("x1") = a1;
62 register uint64_t x2 __asm__("x2") = a2;
63 register uint64_t x3 __asm__("x3") = a3;
64 /* HVC would land in our own EL2 vectors while still at EL2. */
65 if (psci_hvc && ck_current_el() == 1)
66 __asm__ volatile("hvc #0" : "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3) : : PSCI_CLOBBERS);
67 else
68 __asm__ volatile("smc #0" : "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3) : : PSCI_CLOBBERS);
69 return (int64_t)x0;
70}
71
72const char *ck_psci_conduit(void)
73{
74 return (psci_hvc && ck_current_el() == 1) ? "hvc" : "smc";
75}
76
77static void psci_call(uint64_t fn)
78{
79 (void)ck_psci_call(fn, 0, 0, 0);
80}
81
82void ck_reset(void)
83{
84 static int quiescing;
85 __asm__ volatile("msr daifset, #0xf" ::: "memory");
86 if (ck_stage_quiesce && !quiescing) {
87 quiescing = 1; /* a panic inside the hook resets without it */
88 ck_stage_quiesce();
89 }
90 if (ck_pi5_before_reset)
91 ck_pi5_before_reset();
92 ck_console_fb_hold();
93 ck_mb();
94 psci_call(0x84000009u); /* PSCI SYSTEM_RESET */
95 psci_call(0x84000008u); /* SYSTEM_OFF if reset was refused */
96 for (;;)
97 __asm__ volatile("wfi");
98}
99
100void ck_mb(void)
101{
102 __asm__ volatile("dsb sy" ::: "memory");
103}
104
105void ck_panic(const char *fmt, ...)
106{
107 __asm__ volatile("msr daifset, #0xf" ::: "memory");
108 if (in_fatal++)
109 ck_reset();
110 ck_puts("\n");
111 ck_report_header("panic");
112 ck_puts("panic: ");
113 va_list ap;
114 va_start(ap, fmt);
115 ck_vprintf(fmt, ap);
116 va_end(ap);
117 ck_puts("\n");
118 ck_reset();
119}
120
121void ck_fault_report(const char *what, uint64_t esr, uint64_t far, uint64_t elr, unsigned el)
122{
123 if (in_fatal++)
124 ck_reset();
125 ck_puts("\n");
126 ck_report_header("fault");
127 ck_printf("fault: vector=%s el=%u esr=0x%llx ec=0x%llx far=0x%llx elr=0x%llx\n", what, el,
128 (unsigned long long)esr, (unsigned long long)((esr >> 26) & 0x3f),
129 (unsigned long long)far, (unsigned long long)elr);
130 ck_reset();
131}