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 / crates / aienos-kernel / src / console.rs

136 lines · 4285 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//! Early serial console chosen from the firmware's ACPI SPCR table.
17//!
18//! The console never depends on accelerator discovery: the firmware says where
19//! the serial port is, and AIENOS drives it if it knows the register model.
20//! Every transmit is bounded, so an absent or unclocked UART cannot hang boot.
21
22use crate::acpi::UartKind;
23use crate::arch::aarch64::{EarlyUart, MmioReg, UART_TX_SPIN_LIMIT};
24use core::cell::Cell;
25
26/// ARM PL011 / SBSA generic UART register offsets.
27pub mod pl011_regs {
28 pub const DATA: usize = 0x00;
29 pub const FLAGS: usize = 0x18;
30 pub const TX_FULL: u32 = 1 << 5;
31}
32
33/// PL011 transmitter with the same bounded behaviour as `EarlyUart`.
34pub struct Pl011Uart {
35 base: usize,
36 dead: Cell<bool>,
37}
38
39impl Pl011Uart {
40 pub const fn new(base: usize) -> Self {
41 Self {
42 base,
43 dead: Cell::new(false),
44 }
45 }
46
47 pub fn is_dead(&self) -> bool {
48 self.dead.get()
49 }
50
51 pub fn write_byte(&self, b: u8) {
52 if self.dead.get() {
53 return;
54 }
55 // SAFETY: `base` is the PL011 register frame this UART was created for;
56 // FLAGS and DATA are register offsets inside it.
57 let flags = unsafe { MmioReg::<u32>::new(self.base + pl011_regs::FLAGS) };
58 let mut polls = 0u32;
59 while flags.read() & pl011_regs::TX_FULL != 0 {
60 polls += 1;
61 if polls >= UART_TX_SPIN_LIMIT {
62 self.dead.set(true);
63 return;
64 }
65 core::hint::spin_loop();
66 }
67 // SAFETY: as above, DATA is inside the PL011 register frame at `base`.
68 unsafe { MmioReg::<u32>::new(self.base + pl011_regs::DATA) }.write(u32::from(b));
69 }
70}
71
72/// The serial console found through SPCR.
73pub enum EarlyConsole {
74 Ns16550(EarlyUart),
75 Pl011(Pl011Uart),
76}
77
78impl EarlyConsole {
79 /// `None` when SPCR described no console this driver set can use.
80 pub fn from_kind(kind: UartKind) -> Option<Self> {
81 match kind {
82 UartKind::Ns16550Mmio32(base) => Some(Self::Ns16550(EarlyUart::new(base as usize))),
83 UartKind::Pl011(base) => Some(Self::Pl011(Pl011Uart::new(base as usize))),
84 UartKind::Unsupported => None,
85 }
86 }
87
88 pub fn write_str(&self, s: &str) {
89 for b in s.bytes() {
90 if b == b'\n' {
91 self.write_byte(b'\r');
92 }
93 self.write_byte(b);
94 }
95 }
96
97 fn write_byte(&self, b: u8) {
98 match self {
99 Self::Ns16550(u) => u.write_byte(b),
100 Self::Pl011(u) => u.write_byte(b),
101 }
102 }
103
104 pub fn is_dead(&self) -> bool {
105 match self {
106 Self::Ns16550(u) => u.is_dead(),
107 Self::Pl011(u) => u.is_dead(),
108 }
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn pl011_writes_when_ready_and_gives_up_when_always_full() {
118 let mut regs = [0u32; 8]; // FLAGS (index 6) clear: ready
119 let console = EarlyConsole::from_kind(UartKind::Pl011(regs.as_mut_ptr() as u64)).unwrap();
120 console.write_str("A");
121 assert_eq!(regs[0], u32::from(b'A'));
122 assert!(!console.is_dead());
123
124 let mut full = [0u32; 8];
125 full[pl011_regs::FLAGS / 4] = pl011_regs::TX_FULL;
126 let stuck = EarlyConsole::from_kind(UartKind::Pl011(full.as_mut_ptr() as u64)).unwrap();
127 stuck.write_str("AIENOS\n");
128 assert!(stuck.is_dead());
129 assert_eq!(full[0], 0);
130 }
131
132 #[test]
133 fn unsupported_spcr_gives_no_console() {
134 assert!(EarlyConsole::from_kind(UartKind::Unsupported).is_none());
135 }
136}