aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / arch / aarch64.rs
| 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 | //! AArch64 bare-metal architecture primitives, MMIO, and UART for AIENOS. |
| 17 | |
| 18 | use core::marker::PhantomData; |
| 19 | use core::ptr::{read_volatile, write_volatile}; |
| 20 | |
| 21 | /// Memory-Mapped I/O Register abstraction for safe volatile access. |
| 22 | #[repr(transparent)] |
| 23 | pub struct MmioReg<T> { |
| 24 | addr: usize, |
| 25 | _marker: PhantomData<T>, |
| 26 | } |
| 27 | |
| 28 | impl<T: Copy> MmioReg<T> { |
| 29 | /// Create a new MMIO register accessor at the given physical/virtual address. |
| 30 | /// |
| 31 | /// # Safety |
| 32 | /// `addr` must address a mapped device register (or other memory valid for |
| 33 | /// volatile reads and writes of `T`), aligned for `T`, for as long as the |
| 34 | /// returned accessor is used. `read` and `write` access it without further |
| 35 | /// checks. |
| 36 | /// |
| 37 | /// ``` |
| 38 | /// use aienos_kernel::arch::aarch64::MmioReg; |
| 39 | /// let mut word = 0u32; |
| 40 | /// // SAFETY: `word` is a live, aligned u32 for as long as `reg` is used. |
| 41 | /// let reg = unsafe { MmioReg::<u32>::new(core::ptr::addr_of_mut!(word) as usize) }; |
| 42 | /// reg.write(7); |
| 43 | /// assert_eq!(reg.read(), 7); |
| 44 | /// ``` |
| 45 | /// |
| 46 | /// Safe code cannot construct one: |
| 47 | /// |
| 48 | /// ```compile_fail,E0133 |
| 49 | /// use aienos_kernel::arch::aarch64::MmioReg; |
| 50 | /// let _ = MmioReg::<u32>::new(0x0900_0000); |
| 51 | /// ``` |
| 52 | pub const unsafe fn new(addr: usize) -> Self { |
| 53 | Self { |
| 54 | addr, |
| 55 | _marker: PhantomData, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// Read volatile from MMIO register. |
| 60 | pub fn read(&self) -> T { |
| 61 | // SAFETY: `new`'s contract makes `addr` valid for a volatile read of `T`. |
| 62 | unsafe { read_volatile(self.addr as *const T) } |
| 63 | } |
| 64 | |
| 65 | /// Write volatile to MMIO register. |
| 66 | pub fn write(&self, value: T) { |
| 67 | // SAFETY: `new`'s contract makes `addr` valid for a volatile write of `T`. |
| 68 | unsafe { write_volatile(self.addr as *mut T, value) } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// AArch64 Exception Level query. |
| 73 | /// Returns 0 for EL0, 1 for EL1, 2 for EL2, 3 for EL3. |
| 74 | #[inline(always)] |
| 75 | pub fn current_el() -> u8 { |
| 76 | #[cfg(target_arch = "aarch64")] |
| 77 | { |
| 78 | let el: u64; |
| 79 | unsafe { |
| 80 | core::arch::asm!( |
| 81 | "mrs {0}, CurrentEL", |
| 82 | out(reg) el, |
| 83 | options(nomem, nostack) |
| 84 | ); |
| 85 | } |
| 86 | ((el >> 2) & 0x03) as u8 |
| 87 | } |
| 88 | #[cfg(not(target_arch = "aarch64"))] |
| 89 | { |
| 90 | 1 // Default mock EL1 for non-aarch64 hosts |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /// True when running at EL1 with stage-1 translation on (SCTLR_EL1.M set). |
| 95 | pub fn el1_mmu_enabled() -> bool { |
| 96 | #[cfg(target_arch = "aarch64")] |
| 97 | { |
| 98 | if current_el() != 1 { |
| 99 | return false; |
| 100 | } |
| 101 | let sctlr: u64; |
| 102 | unsafe { |
| 103 | core::arch::asm!("mrs {0}, sctlr_el1", out(reg) sctlr, options(nomem, nostack)); |
| 104 | } |
| 105 | sctlr & 1 != 0 |
| 106 | } |
| 107 | #[cfg(not(target_arch = "aarch64"))] |
| 108 | { |
| 109 | false |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Configure stage-1 translation and enter EL1h using an identity-mapped root. |
| 114 | /// The caller must ensure the current PC, stack, code, and data are mapped. |
| 115 | /// |
| 116 | /// # Safety |
| 117 | /// `root` must reference a valid 4 KiB-granule EL1 table tree whose identity |
| 118 | /// mappings cover this function, its caller, the active stack, and all live data. |
| 119 | pub unsafe fn enter_el1h_mmu(root: usize, mair: u64, tcr: u64, sctlr: u64) -> bool { |
| 120 | #[cfg(target_arch = "aarch64")] |
| 121 | { |
| 122 | if current_el() != 2 { |
| 123 | return false; |
| 124 | } |
| 125 | core::arch::asm!( |
| 126 | "msr daifset, #0xf", |
| 127 | "mov x9, sp", |
| 128 | "msr sp_el1, x9", |
| 129 | "mov x9, #0x80000000", |
| 130 | "msr hcr_el2, x9", |
| 131 | "mov x9, #0x33ff", |
| 132 | "msr cptr_el2, x9", |
| 133 | "mov x9, #3", |
| 134 | "msr cnthctl_el2, x9", |
| 135 | "msr cntvoff_el2, xzr", |
| 136 | "mov x9, #0x300000", |
| 137 | "msr cpacr_el1, x9", |
| 138 | "msr mair_el1, x1", |
| 139 | "msr tcr_el1, x2", |
| 140 | "msr ttbr0_el1, x0", |
| 141 | "tlbi vmalle1", |
| 142 | "dsb ish", |
| 143 | "isb", |
| 144 | "msr sctlr_el1, x3", |
| 145 | "isb", |
| 146 | "mov x9, #0x3c5", |
| 147 | "msr spsr_el2, x9", |
| 148 | "adr x9, 2f", |
| 149 | "msr elr_el2, x9", |
| 150 | "isb", |
| 151 | "eret", |
| 152 | "2:", |
| 153 | in("x0") root as u64, |
| 154 | in("x1") mair, |
| 155 | in("x2") tcr, |
| 156 | in("x3") sctlr, |
| 157 | lateout("x9") _, |
| 158 | options(nostack) |
| 159 | ); |
| 160 | true |
| 161 | } |
| 162 | #[cfg(not(target_arch = "aarch64"))] |
| 163 | { |
| 164 | let _ = (root, mair, tcr, sctlr); |
| 165 | false |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /// Read the architectural virtual counter. Its frequency is reported by `counter_frequency_hz`. |
| 170 | #[inline(always)] |
| 171 | pub fn counter_ticks() -> u64 { |
| 172 | #[cfg(target_arch = "aarch64")] |
| 173 | { |
| 174 | let ticks: u64; |
| 175 | unsafe { |
| 176 | core::arch::asm!( |
| 177 | "isb", |
| 178 | "mrs {ticks}, cntvct_el0", |
| 179 | ticks = out(reg) ticks, |
| 180 | options(nomem, nostack, preserves_flags) |
| 181 | ); |
| 182 | } |
| 183 | ticks |
| 184 | } |
| 185 | #[cfg(not(target_arch = "aarch64"))] |
| 186 | { |
| 187 | 0 |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /// Main ID register of the executing core. Bits 15:4 are the part number |
| 192 | /// (0xd87 Cortex-A725, 0xd85 Cortex-X925 on the GB10). |
| 193 | #[inline(always)] |
| 194 | pub fn midr_el1() -> u64 { |
| 195 | #[cfg(target_arch = "aarch64")] |
| 196 | { |
| 197 | let midr: u64; |
| 198 | unsafe { |
| 199 | core::arch::asm!("mrs {0}, midr_el1", out(reg) midr, options(nomem, nostack, preserves_flags)); |
| 200 | } |
| 201 | midr |
| 202 | } |
| 203 | #[cfg(not(target_arch = "aarch64"))] |
| 204 | { |
| 205 | 0 |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /// One 64-bit value from the architectural random number generator (FEAT_RNG |
| 210 | /// `RNDR`), or `None` if the core does not implement it or it keeps failing. |
| 211 | /// Callers that need entropy must fail closed on `None`; there is no fallback. |
| 212 | pub fn rndr() -> Option<u64> { |
| 213 | #[cfg(target_arch = "aarch64")] |
| 214 | { |
| 215 | let isar0: u64; |
| 216 | unsafe { |
| 217 | core::arch::asm!("mrs {0}, id_aa64isar0_el1", out(reg) isar0, options(nomem, nostack, preserves_flags)); |
| 218 | } |
| 219 | if (isar0 >> 60) & 0xf == 0 { |
| 220 | return None; |
| 221 | } |
| 222 | for _ in 0..16 { |
| 223 | let (value, nzcv): (u64, u64); |
| 224 | // RNDR is s3_3_c2_c4_0; a failed read returns 0 with PSTATE.Z = 1. |
| 225 | unsafe { |
| 226 | core::arch::asm!( |
| 227 | "mrs {0}, s3_3_c2_c4_0", |
| 228 | "mrs {1}, nzcv", |
| 229 | out(reg) value, |
| 230 | out(reg) nzcv, |
| 231 | options(nomem, nostack), |
| 232 | ); |
| 233 | } |
| 234 | if nzcv & (1 << 30) == 0 { |
| 235 | return Some(value); |
| 236 | } |
| 237 | } |
| 238 | None |
| 239 | } |
| 240 | #[cfg(not(target_arch = "aarch64"))] |
| 241 | { |
| 242 | None |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// Multiprocessor affinity of the executing core (matches MADT GICC MPIDR). |
| 247 | #[inline(always)] |
| 248 | pub fn mpidr_el1() -> u64 { |
| 249 | #[cfg(target_arch = "aarch64")] |
| 250 | { |
| 251 | let mpidr: u64; |
| 252 | unsafe { |
| 253 | core::arch::asm!("mrs {0}, mpidr_el1", out(reg) mpidr, options(nomem, nostack, preserves_flags)); |
| 254 | } |
| 255 | mpidr |
| 256 | } |
| 257 | #[cfg(not(target_arch = "aarch64"))] |
| 258 | { |
| 259 | 0 |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /// PSCI SYSTEM_RESET (function 0x8400_0009) through the secure monitor: the |
| 264 | /// architected Arm reset path, not a UEFI runtime service. Does not return if |
| 265 | /// the platform implements it; returns if the monitor refused the call. |
| 266 | pub fn psci_system_reset() { |
| 267 | #[cfg(target_arch = "aarch64")] |
| 268 | unsafe { |
| 269 | core::arch::asm!("smc #0", inout("x0") 0x8400_0009u64 => _, options(nostack)); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /// Part number field of a MIDR value. |
| 274 | pub const fn midr_part(midr: u64) -> u16 { |
| 275 | ((midr >> 4) & 0xfff) as u16 |
| 276 | } |
| 277 | |
| 278 | /// Read the frequency of the architectural virtual counter. |
| 279 | #[inline(always)] |
| 280 | pub fn counter_frequency_hz() -> u64 { |
| 281 | #[cfg(target_arch = "aarch64")] |
| 282 | { |
| 283 | let frequency: u64; |
| 284 | unsafe { |
| 285 | core::arch::asm!( |
| 286 | "mrs {frequency}, cntfrq_el0", |
| 287 | frequency = out(reg) frequency, |
| 288 | options(nomem, nostack, preserves_flags) |
| 289 | ); |
| 290 | } |
| 291 | frequency |
| 292 | } |
| 293 | #[cfg(not(target_arch = "aarch64"))] |
| 294 | { |
| 295 | 0 |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /// Data Memory Barrier (DMB). |
| 300 | #[inline(always)] |
| 301 | pub fn dmb() { |
| 302 | #[cfg(target_arch = "aarch64")] |
| 303 | unsafe { |
| 304 | core::arch::asm!("dmb sy", options(nostack)); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /// Data Synchronization Barrier (DSB). |
| 309 | #[inline(always)] |
| 310 | pub fn dsb() { |
| 311 | #[cfg(target_arch = "aarch64")] |
| 312 | unsafe { |
| 313 | core::arch::asm!("dsb sy", options(nostack)); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /// Instruction Synchronization Barrier (ISB). |
| 318 | #[inline(always)] |
| 319 | pub fn isb() { |
| 320 | #[cfg(target_arch = "aarch64")] |
| 321 | unsafe { |
| 322 | core::arch::asm!("isb", options(nostack)); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /// Smallest data cache line in bytes, from CTR_EL0.DminLine (log2 of the |
| 327 | /// line size in 4-byte words). Maintenance loops must step by this, never by |
| 328 | /// a guess: `dc` acts on the one line holding the address it is given. |
| 329 | pub fn dcache_line_bytes() -> usize { |
| 330 | #[cfg(target_arch = "aarch64")] |
| 331 | { |
| 332 | let ctr: u64; |
| 333 | unsafe { |
| 334 | core::arch::asm!("mrs {0}, ctr_el0", out(reg) ctr, options(nomem, nostack, preserves_flags)); |
| 335 | } |
| 336 | 4 << ((ctr >> 16) & 0xf) |
| 337 | } |
| 338 | #[cfg(not(target_arch = "aarch64"))] |
| 339 | { |
| 340 | 64 |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /// Clean `[addr, addr + size)` to the point of coherency so a DMA master |
| 345 | /// sees what the CPU wrote. No-op on hosts. |
| 346 | pub fn clean_dcache_range(addr: usize, size: usize) { |
| 347 | #[cfg(target_arch = "aarch64")] |
| 348 | dcache_range(addr, size, |line| unsafe { |
| 349 | core::arch::asm!("dc cvac, {0}", in(reg) line, options(nostack, preserves_flags)); |
| 350 | }); |
| 351 | #[cfg(not(target_arch = "aarch64"))] |
| 352 | let _ = (addr, size); |
| 353 | } |
| 354 | |
| 355 | /// Clean and invalidate `[addr, addr + size)` so the next CPU read sees what |
| 356 | /// a DMA master wrote. Cleaning first means no CPU write in a shared line is |
| 357 | /// lost. No-op on hosts. |
| 358 | pub fn clean_invalidate_dcache_range(addr: usize, size: usize) { |
| 359 | #[cfg(target_arch = "aarch64")] |
| 360 | dcache_range(addr, size, |line| unsafe { |
| 361 | core::arch::asm!("dc civac, {0}", in(reg) line, options(nostack, preserves_flags)); |
| 362 | }); |
| 363 | #[cfg(not(target_arch = "aarch64"))] |
| 364 | let _ = (addr, size); |
| 365 | } |
| 366 | |
| 367 | #[cfg(target_arch = "aarch64")] |
| 368 | fn dcache_range(addr: usize, size: usize, op: impl Fn(usize)) { |
| 369 | let line = dcache_line_bytes(); |
| 370 | let end = addr.saturating_add(size); |
| 371 | let mut cursor = addr & !(line - 1); |
| 372 | while cursor < end { |
| 373 | op(cursor); |
| 374 | cursor += line; |
| 375 | } |
| 376 | dsb(); |
| 377 | } |
| 378 | |
| 379 | /// Disable interrupts (set DAIF flags). |
| 380 | #[inline(always)] |
| 381 | pub fn disable_interrupts() { |
| 382 | #[cfg(target_arch = "aarch64")] |
| 383 | unsafe { |
| 384 | core::arch::asm!("msr daifset, #2", options(nomem, nostack)); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /// Enable interrupts (clear DAIF flags). |
| 389 | #[inline(always)] |
| 390 | pub fn enable_interrupts() { |
| 391 | #[cfg(target_arch = "aarch64")] |
| 392 | unsafe { |
| 393 | core::arch::asm!("msr daifclr, #2", options(nomem, nostack)); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /// Check if interrupts are enabled (I bit in DAIF). |
| 398 | #[inline(always)] |
| 399 | pub fn are_interrupts_enabled() -> bool { |
| 400 | #[cfg(target_arch = "aarch64")] |
| 401 | { |
| 402 | let daif: u64; |
| 403 | unsafe { |
| 404 | core::arch::asm!("mrs {0}, daif", out(reg) daif, options(nomem, nostack)); |
| 405 | } |
| 406 | (daif & (1 << 7)) == 0 |
| 407 | } |
| 408 | #[cfg(not(target_arch = "aarch64"))] |
| 409 | { |
| 410 | true |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /// Wait For Interrupt (WFI) instruction - places core into low-power sleep until interrupt. |
| 415 | #[inline(always)] |
| 416 | pub fn wfi() { |
| 417 | #[cfg(target_arch = "aarch64")] |
| 418 | unsafe { |
| 419 | core::arch::asm!("wfi", options(nomem, nostack)); |
| 420 | } |
| 421 | #[cfg(not(target_arch = "aarch64"))] |
| 422 | core::hint::spin_loop(); |
| 423 | } |
| 424 | |
| 425 | /// Wait For Event (WFE) instruction. |
| 426 | #[inline(always)] |
| 427 | pub fn wfe() { |
| 428 | #[cfg(target_arch = "aarch64")] |
| 429 | unsafe { |
| 430 | core::arch::asm!("wfe", options(nomem, nostack)); |
| 431 | } |
| 432 | #[cfg(not(target_arch = "aarch64"))] |
| 433 | core::hint::spin_loop(); |
| 434 | } |
| 435 | |
| 436 | /// Send Event (SEV) instruction. |
| 437 | #[inline(always)] |
| 438 | pub fn sev() { |
| 439 | #[cfg(target_arch = "aarch64")] |
| 440 | unsafe { |
| 441 | core::arch::asm!("sev", options(nomem, nostack)); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /// Deterministic bare-metal halt loop: disables interrupts and sleeps in WFI. |
| 446 | pub fn halt() -> ! { |
| 447 | disable_interrupts(); |
| 448 | loop { |
| 449 | wfi(); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /// Serial MMIO base observed in the DGX Spark host kernel command line. |
| 454 | /// The host uses earlycon=uart,mmio32,0x16A00000, the 8250 register layout. |
| 455 | pub const SPARK_16550_UART_BASE: usize = 0x16A0_0000; |
| 456 | |
| 457 | /// 8250 register indices with the observed 32-bit register stride. |
| 458 | pub mod uart16550_regs { |
| 459 | pub const DATA: usize = 0; |
| 460 | pub const LINE_STATUS: usize = 5 * 4; |
| 461 | pub const DATA_READY: u32 = 1; |
| 462 | pub const TX_HOLDING_EMPTY: u32 = 1 << 5; |
| 463 | } |
| 464 | |
| 465 | /// Status polls allowed per byte before the UART is treated as absent. At |
| 466 | /// 921600 baud one byte takes about 11 us, far below this many MMIO reads. |
| 467 | pub const UART_TX_SPIN_LIMIT: u32 = 1_000_000; |
| 468 | |
| 469 | /// Early 8250-compatible UART with 32-bit MMIO registers. |
| 470 | /// |
| 471 | /// Transmission is bounded: if the transmitter never reports ready (no cable, |
| 472 | /// unclocked or absent device), the UART marks itself dead and drops further |
| 473 | /// output instead of hanging the boot before other outputs have run. |
| 474 | pub struct EarlyUart { |
| 475 | base_addr: usize, |
| 476 | dead: core::cell::Cell<bool>, |
| 477 | } |
| 478 | |
| 479 | impl EarlyUart { |
| 480 | /// Create a new EarlyUart driver instance at the specified MMIO base address. |
| 481 | pub const fn new(base_addr: usize) -> Self { |
| 482 | Self { |
| 483 | base_addr, |
| 484 | dead: core::cell::Cell::new(false), |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// Read raw register. |
| 489 | fn reg(&self, offset: usize) -> MmioReg<u32> { |
| 490 | // SAFETY: `base_addr` is the UART register block this driver was created |
| 491 | // for, and every caller passes a `uart16550_regs` offset inside it. |
| 492 | unsafe { MmioReg::new(self.base_addr + offset) } |
| 493 | } |
| 494 | |
| 495 | /// True once a transmit timed out; later writes are skipped. |
| 496 | pub fn is_dead(&self) -> bool { |
| 497 | self.dead.get() |
| 498 | } |
| 499 | |
| 500 | /// Send a single byte through UART transmit FIFO, giving up after |
| 501 | /// `UART_TX_SPIN_LIMIT` status polls. |
| 502 | pub fn write_byte(&self, b: u8) { |
| 503 | if self.dead.get() { |
| 504 | return; |
| 505 | } |
| 506 | let status = self.reg(uart16550_regs::LINE_STATUS); |
| 507 | let data = self.reg(uart16550_regs::DATA); |
| 508 | |
| 509 | let mut polls = 0u32; |
| 510 | while (status.read() & uart16550_regs::TX_HOLDING_EMPTY) == 0 { |
| 511 | polls += 1; |
| 512 | if polls >= UART_TX_SPIN_LIMIT { |
| 513 | self.dead.set(true); |
| 514 | return; |
| 515 | } |
| 516 | core::hint::spin_loop(); |
| 517 | } |
| 518 | data.write(b as u32); |
| 519 | } |
| 520 | |
| 521 | /// Transmit a UTF-8 string through UART. |
| 522 | pub fn write_str(&self, s: &str) { |
| 523 | for b in s.bytes() { |
| 524 | if b == b'\n' { |
| 525 | self.write_byte(b'\r'); |
| 526 | } |
| 527 | self.write_byte(b); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /// Write an unsigned decimal number without heap allocation. |
| 532 | pub fn write_u64(&self, mut value: u64) { |
| 533 | let mut digits = [0u8; 20]; |
| 534 | let mut start = digits.len(); |
| 535 | loop { |
| 536 | start -= 1; |
| 537 | digits[start] = b'0' + (value % 10) as u8; |
| 538 | value /= 10; |
| 539 | if value == 0 { |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | for digit in &digits[start..] { |
| 544 | self.write_byte(*digit); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /// Attempt to read a single byte from UART receive FIFO without blocking. |
| 549 | /// Returns None if receive FIFO is empty (RXFE == 1). |
| 550 | pub fn try_read_byte(&self) -> Option<u8> { |
| 551 | let status = self.reg(uart16550_regs::LINE_STATUS); |
| 552 | if (status.read() & uart16550_regs::DATA_READY) == 0 { |
| 553 | return None; |
| 554 | } |
| 555 | Some((self.reg(uart16550_regs::DATA).read() & 0xFF) as u8) |
| 556 | } |
| 557 | |
| 558 | /// Read a single byte from UART, blocking until data arrives. |
| 559 | pub fn read_byte(&self) -> u8 { |
| 560 | loop { |
| 561 | if let Some(b) = self.try_read_byte() { |
| 562 | return b; |
| 563 | } |
| 564 | core::hint::spin_loop(); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | impl core::fmt::Write for EarlyUart { |
| 570 | fn write_str(&mut self, s: &str) -> core::fmt::Result { |
| 571 | EarlyUart::write_str(self, s); |
| 572 | Ok(()) |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | #[cfg(test)] |
| 577 | mod tests { |
| 578 | use super::*; |
| 579 | |
| 580 | #[test] |
| 581 | fn spark_uart_uses_32_bit_16550_register_stride() { |
| 582 | let mut registers = [0u32; 8]; |
| 583 | registers[5] = uart16550_regs::TX_HOLDING_EMPTY; |
| 584 | let uart = EarlyUart::new(registers.as_mut_ptr() as usize); |
| 585 | uart.write_byte(b'A'); |
| 586 | assert_eq!(registers[0], b'A' as u32); |
| 587 | assert_eq!(uart.try_read_byte(), None); |
| 588 | // SAFETY: LINE_STATUS is inside the local `registers` array, which |
| 589 | // outlives this write. |
| 590 | unsafe { MmioReg::new(registers.as_mut_ptr() as usize + uart16550_regs::LINE_STATUS) } |
| 591 | .write(uart16550_regs::TX_HOLDING_EMPTY | uart16550_regs::DATA_READY); |
| 592 | assert_eq!(uart.try_read_byte(), Some(b'A')); |
| 593 | } |
| 594 | |
| 595 | #[test] |
| 596 | fn a_uart_that_never_becomes_ready_is_abandoned_instead_of_hanging() { |
| 597 | let mut registers = [0u32; 8]; // TX_HOLDING_EMPTY never set |
| 598 | let uart = EarlyUart::new(registers.as_mut_ptr() as usize); |
| 599 | uart.write_str("AIENOS\n"); |
| 600 | assert!(uart.is_dead()); |
| 601 | assert_eq!(registers[0], 0); |
| 602 | } |
| 603 | } |