Skip to main content

strat9_kernel/arch/x86_64/vga/
panic_screen.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Panic-safe framebuffer drawing.
24//!
25//! Provides a set of globals and direct-draw functions that allow the panic
26//! handler to render a visual panic screen even when `VGA_WRITER` is locked
27//! by the faulting context. The framebuffer parameters are snapshotted once
28//! during `vga::init()` via `init_panic_fb_globals()` and are read-only
29//! afterwards, making them safe to access from the panic path without any
30//! locking.
31
32use core::sync::atomic::{AtomicU64, Ordering};
33
34// These globals are set once during vga::init and are read-only afterwards,
35// so the panic handler can draw a panic screen even when VGA_WRITER is
36// locked by the faulting context.
37// =============================================================================
38
39/// Raw framebuffer virtual address (HHDM-mapped), 0 if framebuffer is not available.
40pub(crate) static PANIC_FB_ADDR: AtomicU64 = AtomicU64::new(0);
41/// Framebuffer width in pixels.
42pub(crate) static PANIC_FB_WIDTH: AtomicU64 = AtomicU64::new(0);
43/// Framebuffer height in pixels.
44pub(crate) static PANIC_FB_HEIGHT: AtomicU64 = AtomicU64::new(0);
45/// Framebuffer pitch in bytes.
46pub(crate) static PANIC_FB_PITCH: AtomicU64 = AtomicU64::new(0);
47/// Packed pixel-format info: (bpp << 32) | (red_shift << 24) |
48/// (green_shift << 16) | (blue_shift << 8) | format_flags
49pub(crate) static PANIC_FB_FORMAT: AtomicU64 = AtomicU64::new(0);
50
51/// Initialise the panic-screen framebuffer globals.
52/// Must be called once from vga::init() after configure() succeeds.
53pub fn init_panic_fb_globals(
54    addr: u64,
55    w: usize,
56    h: usize,
57    pitch: usize,
58    bpp: u16,
59    red_shift: u8,
60    green_shift: u8,
61    blue_shift: u8,
62) {
63    PANIC_FB_ADDR.store(addr, Ordering::Release);
64    PANIC_FB_WIDTH.store(w as u64, Ordering::Relaxed);
65    PANIC_FB_HEIGHT.store(h as u64, Ordering::Relaxed);
66    PANIC_FB_PITCH.store(pitch as u64, Ordering::Relaxed);
67    let fmt: u64 = (bpp as u64) << 32
68        | (red_shift as u64) << 24
69        | (green_shift as u64) << 16
70        | (blue_shift as u64) << 8;
71    PANIC_FB_FORMAT.store(fmt, Ordering::Relaxed);
72}
73
74/// Minimal 8×16 bitmap font for panic-screen rendering (ASCII 0x20–0x7E).
75/// Each glyph is 16 bytes (one byte per row, MSB = leftmost pixel).
76/// Derived from the standard VGA 8×16 ROM font.
77pub(crate) static PANIC_FONT_8X16: [u8; 95 * 16] = panic_font_8x16();
78
79const fn panic_font_8x16() -> [u8; 95 * 16] {
80    // 95 ASCII printable characters (0x20–0x7E), 16 rows each.
81    // Generated from the standard VGA 8×16 BIOS font.
82    let mut font = [0u8; 95 * 16];
83    // We embed a compact representation: each char is 16 bytes = 16 rows × 8 cols.
84    // For space (0x20), all zeros.
85    // For simplicity, only a subset of critical glyphs are defined;
86    // unknown chars render as a filled block (0xFF per row).
87    // Since const fn can't use loops over ranges in stable Rust, we use a
88    // select few characters via direct index assignment in const context.
89    // The full font table is generated below.
90
91    // Helper: set glyph N to the given 16-byte bitmap
92    macro_rules! glyph {
93        ($idx:expr, $b0:expr, $b1:expr, $b2:expr, $b3:expr,
94         $b4:expr, $b5:expr, $b6:expr, $b7:expr, $b8:expr, $b9:expr,
95         $b10:expr, $b11:expr, $b12:expr, $b13:expr, $b14:expr, $b15:expr) => {
96            let off = ($idx - 0x20) * 16;
97            font[off] = $b0;
98            font[off + 1] = $b1;
99            font[off + 2] = $b2;
100            font[off + 3] = $b3;
101            font[off + 4] = $b4;
102            font[off + 5] = $b5;
103            font[off + 6] = $b6;
104            font[off + 7] = $b7;
105            font[off + 8] = $b8;
106            font[off + 9] = $b9;
107            font[off + 10] = $b10;
108            font[off + 11] = $b11;
109            font[off + 12] = $b12;
110            font[off + 13] = $b13;
111            font[off + 14] = $b14;
112            font[off + 15] = $b15;
113        };
114    }
115
116    // Space (0x20) : all zeros
117    // const fn-compatible zero-fill
118    let mut _si = 0usize;
119    while _si < 16 {
120        font[_si] = 0;
121        _si += 1;
122    }
123
124    // ! (0x21)
125    glyph!(
126        0x21, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18,
127        0x00, 0x00
128    );
129    // " (0x22)
130    glyph!(
131        0x22, 0x00, 0x6C, 0x6C, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132        0x00, 0x00
133    );
134    // # (0x23)
135    glyph!(
136        0x23, 0x00, 0x00, 0x6C, 0x6C, 0x6C, 0xFE, 0x6C, 0x6C, 0x6C, 0xFE, 0x6C, 0x6C, 0x6C, 0x00,
137        0x00, 0x00
138    );
139    // $ (0x24)
140    glyph!(
141        0x24, 0x00, 0x18, 0x18, 0x7E, 0xE7, 0xE0, 0xE0, 0x7E, 0x07, 0x07, 0xE7, 0x7E, 0x18, 0x18,
142        0x00, 0x00
143    );
144    // % (0x25)
145    glyph!(
146        0x25, 0x00, 0x00, 0xE3, 0xE6, 0xE6, 0x0C, 0x0C, 0x18, 0x18, 0x30, 0x30, 0x67, 0x67, 0xC7,
147        0x00, 0x00
148    );
149    // & (0x26)
150    glyph!(
151        0x26, 0x00, 0x00, 0x38, 0x6C, 0x6C, 0x6C, 0x38, 0x70, 0xDC, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
152        0x00, 0x00
153    );
154    // ' (0x27)
155    glyph!(
156        0x27, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157        0x00, 0x00
158    );
159    // ( (0x28)
160    glyph!(
161        0x28, 0x00, 0x0C, 0x18, 0x30, 0x30, 0x60, 0x60, 0x60, 0x60, 0x60, 0x30, 0x30, 0x18, 0x0C,
162        0x00, 0x00
163    );
164    // ) (0x29)
165    glyph!(
166        0x29, 0x00, 0x60, 0x30, 0x18, 0x18, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x18, 0x18, 0x30, 0x60,
167        0x00, 0x00
168    );
169    // * (0x2A)
170    glyph!(
171        0x2A, 0x00, 0x00, 0x00, 0x18, 0x18, 0x66, 0x7E, 0x3C, 0x7E, 0x66, 0x18, 0x18, 0x00, 0x00,
172        0x00, 0x00
173    );
174    // + (0x2B)
175    glyph!(
176        0x2B, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0xFF, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00,
177        0x00, 0x00
178    );
179    // , (0x2C)
180    glyph!(
181        0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30,
182        0x00, 0x00
183    );
184    // - (0x2D)
185    glyph!(
186        0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187        0x00, 0x00
188    );
189    // . (0x2E)
190    glyph!(
191        0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
192        0x00, 0x00
193    );
194    // / (0x2F)
195    glyph!(
196        0x2F, 0x00, 0x00, 0x03, 0x06, 0x06, 0x0C, 0x0C, 0x18, 0x18, 0x30, 0x30, 0x60, 0x60, 0xC0,
197        0x00, 0x00
198    );
199
200    // 0-9
201    glyph!(
202        0x30, 0x00, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C,
203        0x00, 0x00
204    );
205    glyph!(
206        0x31, 0x00, 0x18, 0x38, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E,
207        0x00, 0x00
208    );
209    glyph!(
210        0x32, 0x00, 0x7E, 0xC3, 0xC3, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xC0, 0xC3, 0xFF,
211        0x00, 0x00
212    );
213    glyph!(
214        0x33, 0x00, 0x7E, 0xC3, 0xC3, 0x03, 0x03, 0x1E, 0x03, 0x03, 0x03, 0xC3, 0xC3, 0xC3, 0x7E,
215        0x00, 0x00
216    );
217    glyph!(
218        0x34, 0x00, 0x06, 0x0E, 0x1E, 0x36, 0x66, 0xC6, 0xC6, 0xFF, 0x06, 0x06, 0x06, 0x06, 0x1F,
219        0x00, 0x00
220    );
221    glyph!(
222        0x35, 0x00, 0xFF, 0xC0, 0xC0, 0xC0, 0xFE, 0xC3, 0x03, 0x03, 0x03, 0x03, 0xC3, 0xC3, 0x7E,
223        0x00, 0x00
224    );
225    glyph!(
226        0x36, 0x00, 0x3E, 0x60, 0xC0, 0xC0, 0xFE, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E,
227        0x00, 0x00
228    );
229    glyph!(
230        0x37, 0x00, 0xFF, 0xC3, 0xC3, 0x06, 0x06, 0x0C, 0x0C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
231        0x00, 0x00
232    );
233    glyph!(
234        0x38, 0x00, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E,
235        0x00, 0x00
236    );
237    glyph!(
238        0x39, 0x00, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7F, 0x03, 0x03, 0x03, 0x03, 0x06, 0x7C,
239        0x00, 0x00
240    );
241
242    // : (0x3A)
243    glyph!(
244        0x3A, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
245        0x00, 0x00
246    );
247    // ; (0x3B)
248    glyph!(
249        0x3B, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30,
250        0x00, 0x00
251    );
252    // < (0x3C)
253    glyph!(
254        0x3C, 0x00, 0x00, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x00,
255        0x00, 0x00
256    );
257    // = (0x3D)
258    glyph!(
259        0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00,
260        0x00, 0x00
261    );
262    // > (0x3E)
263    glyph!(
264        0x3E, 0x00, 0x00, 0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x00,
265        0x00, 0x00
266    );
267    // ? (0x3F)
268    glyph!(
269        0x3F, 0x00, 0x7E, 0xC3, 0xC3, 0x03, 0x06, 0x0C, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18,
270        0x00, 0x00
271    );
272
273    // @ (0x40)
274    glyph!(
275        0x40, 0x00, 0x7E, 0xC3, 0xC3, 0xCF, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xCF, 0xC0, 0xC3, 0x7E,
276        0x00, 0x00
277    );
278
279    // A-Z
280    glyph!(
281        0x41, 0x00, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
282        0x00, 0x00
283    );
284    glyph!(
285        0x42, 0x00, 0xFC, 0x66, 0x63, 0x63, 0x63, 0x66, 0x7E, 0x63, 0x63, 0x63, 0x63, 0x66, 0xFC,
286        0x00, 0x00
287    );
288    glyph!(
289        0x43, 0x00, 0x3E, 0x63, 0xC3, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC3, 0x63, 0x3E,
290        0x00, 0x00
291    );
292    glyph!(
293        0x44, 0x00, 0xFC, 0x66, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x66, 0xFC,
294        0x00, 0x00
295    );
296    glyph!(
297        0x45, 0x00, 0xFF, 0x63, 0x63, 0x60, 0x60, 0x66, 0x7E, 0x66, 0x60, 0x60, 0x63, 0x63, 0xFF,
298        0x00, 0x00
299    );
300    glyph!(
301        0x46, 0x00, 0xFF, 0x63, 0x63, 0x60, 0x60, 0x66, 0x7E, 0x66, 0x60, 0x60, 0x60, 0x60, 0xF0,
302        0x00, 0x00
303    );
304    glyph!(
305        0x47, 0x00, 0x3E, 0x63, 0xC3, 0xC0, 0xC0, 0xC0, 0xCF, 0xC3, 0xC3, 0xC3, 0xC3, 0x63, 0x3E,
306        0x00, 0x00
307    );
308    glyph!(
309        0x48, 0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
310        0x00, 0x00
311    );
312    glyph!(
313        0x49, 0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E,
314        0x00, 0x00
315    );
316    glyph!(
317        0x4A, 0x00, 0x1F, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0xC6, 0x7C,
318        0x00, 0x00
319    );
320    glyph!(
321        0x4B, 0x00, 0xF3, 0x63, 0x63, 0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66, 0x63, 0x63, 0xF3,
322        0x00, 0x00
323    );
324    glyph!(
325        0x4C, 0x00, 0xF0, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x62, 0x66, 0xFF,
326        0x00, 0x00
327    );
328    glyph!(
329        0x4D, 0x00, 0xC3, 0xC3, 0xE7, 0xFF, 0xDB, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
330        0x00, 0x00
331    );
332    glyph!(
333        0x4E, 0x00, 0xC3, 0xC3, 0xE3, 0xF3, 0xDB, 0xDB, 0xCF, 0xC7, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
334        0x00, 0x00
335    );
336    glyph!(
337        0x4F, 0x00, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E,
338        0x00, 0x00
339    );
340    glyph!(
341        0x50, 0x00, 0xFE, 0x63, 0x63, 0x63, 0x63, 0x63, 0x66, 0x7C, 0x60, 0x60, 0x60, 0x60, 0xF0,
342        0x00, 0x00
343    );
344    glyph!(
345        0x51, 0x00, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xDB, 0xDB, 0xCF, 0xC3, 0x7E,
346        0x00, 0x00
347    );
348    glyph!(
349        0x52, 0x00, 0xFE, 0x63, 0x63, 0x63, 0x63, 0x63, 0x66, 0x7C, 0x6C, 0x66, 0x63, 0x63, 0xF3,
350        0x00, 0x00
351    );
352    glyph!(
353        0x53, 0x00, 0x7E, 0xC3, 0xC3, 0xC0, 0xC0, 0x7E, 0x03, 0x03, 0x03, 0xC3, 0xC3, 0xC3, 0x7E,
354        0x00, 0x00
355    );
356    glyph!(
357        0x54, 0x00, 0xFF, 0xDB, 0x99, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C,
358        0x00, 0x00
359    );
360    glyph!(
361        0x55, 0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E,
362        0x00, 0x00
363    );
364    glyph!(
365        0x56, 0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x66, 0x66, 0x3C, 0x3C, 0x18,
366        0x00, 0x00
367    );
368    glyph!(
369        0x57, 0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xDB, 0xDB, 0xFF, 0xE7, 0xC3, 0xC3,
370        0x00, 0x00
371    );
372    glyph!(
373        0x58, 0x00, 0xC3, 0xC3, 0xC3, 0x66, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x66, 0xC3, 0xC3, 0xC3,
374        0x00, 0x00
375    );
376    glyph!(
377        0x59, 0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C,
378        0x00, 0x00
379    );
380    glyph!(
381        0x5A, 0x00, 0xFF, 0xC3, 0x86, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xC0, 0xC3, 0xC3, 0xFF,
382        0x00, 0x00
383    );
384
385    // [ (0x5B)
386    glyph!(
387        0x5B, 0x00, 0x7E, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7E,
388        0x00, 0x00
389    );
390    // \ (0x5C)
391    glyph!(
392        0x5C, 0x00, 0x00, 0xC0, 0x60, 0x60, 0x30, 0x30, 0x18, 0x18, 0x0C, 0x0C, 0x06, 0x06, 0x03,
393        0x00, 0x00
394    );
395    // ] (0x5D)
396    glyph!(
397        0x5D, 0x00, 0x7E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x7E,
398        0x00, 0x00
399    );
400    // ^ (0x5E)
401    glyph!(
402        0x5E, 0x00, 0x18, 0x3C, 0x66, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
403        0x00, 0x00
404    );
405    // _ (0x5F)
406    glyph!(
407        0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
408        0xFF, 0x00
409    );
410
411    // ` (0x60)
412    glyph!(
413        0x60, 0x00, 0x30, 0x18, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
414        0x00, 0x00
415    );
416
417    // a-z
418    glyph!(
419        0x61, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xC3, 0x03, 0x7F, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E,
420        0x00, 0x00
421    );
422    glyph!(
423        0x62, 0x00, 0xE0, 0x60, 0x60, 0x7E, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x7E,
424        0x00, 0x00
425    );
426    glyph!(
427        0x63, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xC3, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC3, 0x7E,
428        0x00, 0x00
429    );
430    glyph!(
431        0x64, 0x00, 0x0E, 0x06, 0x06, 0x7E, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7E,
432        0x00, 0x00
433    );
434    glyph!(
435        0x65, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xC3, 0xC3, 0xFF, 0xC0, 0xC0, 0xC0, 0xC0, 0xC3, 0x7E,
436        0x00, 0x00
437    );
438    glyph!(
439        0x66, 0x00, 0x1E, 0x33, 0x33, 0x30, 0xFC, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFC,
440        0x00, 0x00
441    );
442    glyph!(
443        0x67, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7E, 0x06, 0x06,
444        0x7E, 0x00
445    );
446    glyph!(
447        0x68, 0x00, 0xE0, 0x60, 0x60, 0x6E, 0x73, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0xF3,
448        0x00, 0x00
449    );
450    glyph!(
451        0x69, 0x00, 0x18, 0x18, 0x00, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E,
452        0x00, 0x00
453    );
454    glyph!(
455        0x6A, 0x00, 0x06, 0x06, 0x00, 0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0xC6,
456        0x7C, 0x00
457    );
458    glyph!(
459        0x6B, 0x00, 0xE0, 0x60, 0x60, 0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66, 0x63, 0x63, 0xF3,
460        0x00, 0x00
461    );
462    glyph!(
463        0x6C, 0x00, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E,
464        0x00, 0x00
465    );
466    glyph!(
467        0x6D, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB,
468        0x00, 0x00
469    );
470    glyph!(
471        0x6E, 0x00, 0x00, 0x00, 0x00, 0xDE, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
472        0x00, 0x00
473    );
474    glyph!(
475        0x6F, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E,
476        0x00, 0x00
477    );
478    glyph!(
479        0x70, 0x00, 0x00, 0x00, 0x00, 0xDE, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x66, 0x7C,
480        0x60, 0xF0
481    );
482    glyph!(
483        0x71, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7E, 0x06, 0x06,
484        0x0F, 0x00
485    );
486    glyph!(
487        0x72, 0x00, 0x00, 0x00, 0x00, 0xDE, 0x73, 0x63, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0xF0,
488        0x00, 0x00
489    );
490    glyph!(
491        0x73, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xC3, 0xC0, 0x7E, 0x03, 0x03, 0x03, 0xC3, 0xC3, 0x7E,
492        0x00, 0x00
493    );
494    glyph!(
495        0x74, 0x00, 0x30, 0x30, 0x30, 0xFC, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x1E,
496        0x00, 0x00
497    );
498    glyph!(
499        0x75, 0x00, 0x00, 0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7E,
500        0x00, 0x00
501    );
502    glyph!(
503        0x76, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x66, 0x66, 0x3C, 0x18, 0x00,
504        0x00, 0x00
505    );
506    glyph!(
507        0x77, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xC3, 0xC3, 0xDB, 0xDB, 0xDB, 0xFF, 0x66, 0x66, 0x00,
508        0x00, 0x00
509    );
510    glyph!(
511        0x78, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xC3, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0xC3, 0xC3, 0x00,
512        0x00, 0x00
513    );
514    glyph!(
515        0x79, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7F, 0x03, 0x03,
516        0x7E, 0x00
517    );
518    glyph!(
519        0x7A, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xC3, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC3, 0xC3, 0xFF,
520        0x00, 0x00
521    );
522
523    // { (0x7B)
524    glyph!(
525        0x7B, 0x00, 0x0E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x70, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0E,
526        0x00, 0x00
527    );
528    // | (0x7C)
529    glyph!(
530        0x7C, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
531        0x00, 0x00
532    );
533    // } (0x7D)
534    glyph!(
535        0x7D, 0x00, 0x70, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x70,
536        0x00, 0x00
537    );
538    // ~ (0x7E)
539    glyph!(
540        0x7E, 0x00, 0x76, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
541        0x00, 0x00
542    );
543
544    font
545}
546
547/// Draw a single character at (x, y) using the panic font.
548/// `fg` and `bg` are packed pixel-colour values for the current pixel format.
549fn panic_draw_char(
550    fb: *mut u8,
551    pitch: usize,
552    x: usize,
553    y: usize,
554    ch: u8,
555    fg: u32,
556    bg: u32,
557    fmt: u64,
558) {
559    let bpp = (fmt >> 32) as u16;
560    let bytes_per_pixel = (bpp / 8) as usize;
561    if bytes_per_pixel == 0 {
562        return;
563    }
564    let idx = if ch < 0x20 || ch > 0x7E {
565        0usize
566    } else {
567        (ch - 0x20) as usize
568    };
569    let glyph_start = idx * 16;
570    for row in 0..16 {
571        let mask = PANIC_FONT_8X16[glyph_start + row];
572        if mask == 0 && bg == 0 {
573            continue; // skip all-bg rows for speed
574        }
575        let row_y = y + row;
576        for col in 0..8 {
577            let px = x + col;
578            let pixel = if (mask >> (7 - col)) & 1 != 0 { fg } else { bg };
579            // SAFETY: caller ensures (x,y) + (8,16) is within framebuffer bounds.
580            unsafe {
581                let offset = row_y * pitch + px * bytes_per_pixel;
582                let ptr = fb.add(offset) as *mut u32;
583                if bytes_per_pixel == 4 {
584                    core::ptr::write_volatile(ptr, pixel);
585                } else if bytes_per_pixel == 3 {
586                    // 24bpp : write three bytes
587                    core::ptr::write_volatile(ptr as *mut u8, pixel as u8);
588                    core::ptr::write_volatile(ptr.add(1) as *mut u8, (pixel >> 8) as u8);
589                    core::ptr::write_volatile(ptr.add(2) as *mut u8, (pixel >> 16) as u8);
590                }
591            }
592        }
593    }
594}
595
596/// Pack RGB into a u32 using the pixel-format globals.
597pub(crate) fn panic_pack_rgb(fmt: u64, r: u8, g: u8, b: u8) -> u32 {
598    let red_shift = ((fmt >> 24) & 0xFF) as u8;
599    let green_shift = ((fmt >> 16) & 0xFF) as u8;
600    let blue_shift = ((fmt >> 8) & 0xFF) as u8;
601    (r as u32) << red_shift | (g as u32) << green_shift | (b as u32) << blue_shift
602}
603
604/// Draw a NUL-terminated ASCII string at (x, y) using the panic framebuffer.
605/// Returns the X position after the last character.
606fn panic_draw_str(
607    fb: *mut u8,
608    pitch: usize,
609    mut x: usize,
610    y: usize,
611    s: &str,
612    fg: u32,
613    bg: u32,
614    fmt: u64,
615    max_x: usize,
616) -> usize {
617    for &byte in s.as_bytes() {
618        if byte == 0 {
619            break;
620        }
621        if x + 8 > max_x {
622            break;
623        }
624        if byte == b'\n' {
625            break;
626        }
627        panic_draw_char(fb, pitch, x, y, byte, fg, bg, fmt);
628        x += 8;
629    }
630    x
631}
632
633/// Maximum number of lines for the panic screen.
634const PANIC_SCREEN_MAX_LINES: usize = 40;
635
636/// Draw the full panic screen directly to the framebuffer (no locking).
637///
638/// This is the fallback path used when `VGA_WRITER` is locked by the
639/// faulting context.  It writes pixels directly to the framebuffer
640/// using the global param snapshots set by `init_panic_fb_globals()`.
641pub fn panic_draw_direct(lines: &[&str]) {
642    let fb_addr = PANIC_FB_ADDR.load(Ordering::Acquire);
643    if fb_addr == 0 {
644        return;
645    }
646    let w = PANIC_FB_WIDTH.load(Ordering::Relaxed) as usize;
647    let h = PANIC_FB_HEIGHT.load(Ordering::Relaxed) as usize;
648    let pitch = PANIC_FB_PITCH.load(Ordering::Relaxed) as usize;
649    let fmt = PANIC_FB_FORMAT.load(Ordering::Relaxed);
650    if w == 0 || h == 0 || pitch == 0 {
651        return;
652    }
653
654    let fb = fb_addr as *mut u8;
655    let bg_bg = panic_pack_rgb(fmt, 0x12, 0x16, 0x1E); // dark
656    let fg_txt = panic_pack_rgb(fmt, 0xFF, 0xE7, 0xA0); // amber
657    let fg_err = panic_pack_rgb(fmt, 0xFF, 0x55, 0x55); // red
658    let fg_title = panic_pack_rgb(fmt, 0x4F, 0xB3, 0xB3); // teal
659    let _fg_dim = panic_pack_rgb(fmt, 0x88, 0x88, 0x88); // grey
660
661    // Fill the entire framebuffer with background colour (16/24/32 bpp safe).
662    unsafe {
663        let bytes_per_pixel = ((fmt >> 32) as u16 / 8) as usize;
664        if bytes_per_pixel == 4 {
665            let fill = bg_bg;
666            for row in 0..h {
667                for col in 0..w {
668                    core::ptr::write_volatile(
669                        fb.add(row * pitch + col * bytes_per_pixel) as *mut u32,
670                        fill,
671                    );
672                }
673            }
674        } else if bytes_per_pixel == 3 {
675            let fill = bg_bg;
676            for row in 0..h {
677                for col in 0..w {
678                    let offset = row * pitch + col * bytes_per_pixel;
679                    core::ptr::write_volatile(fb.add(offset) as *mut u8, fill as u8);
680                    core::ptr::write_volatile(fb.add(offset + 1) as *mut u8, (fill >> 8) as u8);
681                    core::ptr::write_volatile(fb.add(offset + 2) as *mut u8, (fill >> 16) as u8);
682                }
683            }
684        } else if bytes_per_pixel == 2 {
685            // 16bpp (e.g. RGB565) : pack fill as 16-bit value
686            let r = ((bg_bg >> 16) & 0xFF) as u16;
687            let g = ((bg_bg >> 8) & 0xFF) as u16;
688            let b = (bg_bg & 0xFF) as u16;
689            let fill16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
690            for row in 0..h {
691                for col in 0..w {
692                    let offset = row * pitch + col * bytes_per_pixel;
693                    core::ptr::write_volatile(fb.add(offset) as *mut u8, fill16 as u8);
694                    core::ptr::write_volatile(fb.add(offset + 1) as *mut u8, (fill16 >> 8) as u8);
695                }
696            }
697        }
698    }
699
700    let line_h = 16;
701    let margin_x = 8;
702    let margin_y = 8;
703    let max_x = w.saturating_sub(margin_x);
704
705    for (i, &line) in lines.iter().enumerate().take(PANIC_SCREEN_MAX_LINES) {
706        let y = margin_y + i * line_h;
707        let fg = if i == 0 {
708            fg_title
709        } else if line.starts_with("!!") || line.starts_with("PANIC") {
710            fg_err
711        } else {
712            fg_txt
713        };
714        panic_draw_str(fb, pitch, margin_x, y, line, fg, bg_bg, fmt, max_x);
715    }
716}