strat9_kernel/arch/x86_64/vga/fastfb.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//! Wide-store helpers for the linear framebuffer.
24//!
25//! The early boot console (live debug overlay) and the panic screen draw
26//! without the `VgaWriter`, so they cannot use its SIMD blit path. They used
27//! to write one pixel at a time with `write_volatile`, and to scroll with
28//! `core::ptr::copy` (which lowers to `rep movsb`).
29//!
30//! On real hardware the framebuffer is MMIO behind PCIe: a byte/word store is
31//! one bus transaction, so `rep movsb` to uncached memory runs at a few MB/s
32//! and the panel visibly repaints from top to bottom. These helpers emit
33//! 8-byte stores, which is the widest portable width without target features
34//! (the pinned nightly miscompiles `movntdq` on `x86_64-unknown-none`), and
35//! keeps the write-combining buffers fed.
36
37/// Bytes per pixel for a supported GOP format (24 or 32).
38#[inline]
39#[allow(dead_code)] // Retained GOP format helper.
40pub const fn bytes_per_pixel(bpp: u16) -> usize {
41 (bpp / 8) as usize
42}
43
44/// Fill `pixels` consecutive pixels with `color`.
45///
46/// # Safety
47/// `dst` must point to `pixels * bytes_per_pixel(bpp)` writable bytes inside a
48/// mapped framebuffer row.
49pub unsafe fn fill_pixels(dst: *mut u8, bpp_bytes: usize, color: u32, pixels: usize) {
50 if pixels == 0 {
51 return;
52 }
53 match bpp_bytes {
54 4 => {
55 let pair = (color as u64) | ((color as u64) << 32);
56 let mut i = 0usize;
57 while i + 2 <= pixels {
58 // SAFETY: two pixels fit before `pixels`; unaligned is fine.
59 core::ptr::write_unaligned(dst.add(i * 4) as *mut u64, pair);
60 i += 2;
61 }
62 if i < pixels {
63 core::ptr::write_unaligned(dst.add(i * 4) as *mut u32, color);
64 }
65 }
66 3 => {
67 // Pack eight 24-bit pixels (24 bytes) per iteration.
68 let b = [color as u8, (color >> 8) as u8, (color >> 16) as u8];
69 let mut i = 0usize;
70 while i + 8 <= pixels {
71 let mut chunk = [0u8; 24];
72 for px in 0..8 {
73 chunk[px * 3..px * 3 + 3].copy_from_slice(&b);
74 }
75 core::ptr::copy_nonoverlapping(chunk.as_ptr(), dst.add(i * 3), 24);
76 i += 8;
77 }
78 while i < pixels {
79 let p = dst.add(i * 3);
80 core::ptr::write_unaligned(p, b[0]);
81 core::ptr::write_unaligned(p.add(1), b[1]);
82 core::ptr::write_unaligned(p.add(2), b[2]);
83 i += 1;
84 }
85 }
86 _ => {}
87 }
88}
89
90/// Store a row of `colors` (one u32 per pixel) into the framebuffer.
91///
92/// # Safety
93/// `dst` must point to `colors.len() * bytes_per_pixel(bpp)` writable bytes
94/// inside a mapped framebuffer row.
95pub unsafe fn store_row(dst: *mut u8, bpp_bytes: usize, colors: &[u32]) {
96 match bpp_bytes {
97 4 => {
98 let mut i = 0usize;
99 while i + 2 <= colors.len() {
100 let pair = (colors[i] as u64) | ((colors[i + 1] as u64) << 32);
101 core::ptr::write_unaligned(dst.add(i * 4) as *mut u64, pair);
102 i += 2;
103 }
104 if i < colors.len() {
105 core::ptr::write_unaligned(dst.add(i * 4) as *mut u32, colors[i]);
106 }
107 }
108 3 => {
109 let mut i = 0usize;
110 while i < colors.len() {
111 let c = colors[i];
112 let p = dst.add(i * 3);
113 core::ptr::write_unaligned(p, c as u8);
114 core::ptr::write_unaligned(p.add(1), (c >> 8) as u8);
115 core::ptr::write_unaligned(p.add(2), (c >> 16) as u8);
116 i += 1;
117 }
118 }
119 _ => {}
120 }
121}
122
123/// Forward copy of `bytes` bytes with 8-byte granularity.
124///
125/// Use only when `dst < src` (or when the ranges do not overlap): the copy is
126/// strictly forward, so an overlapping move-up is safe but a move-down is not.
127///
128/// # Safety
129/// Both ranges must be valid for `bytes` bytes and must not require backward
130/// copying.
131pub unsafe fn copy_forward(dst: *mut u8, src: *const u8, bytes: usize) {
132 let mut i = 0usize;
133 while i + 8 <= bytes {
134 let v = core::ptr::read_unaligned(src.add(i) as *const u64);
135 core::ptr::write_unaligned(dst.add(i) as *mut u64, v);
136 i += 8;
137 }
138 while i < bytes {
139 core::ptr::write_unaligned(dst.add(i), core::ptr::read_unaligned(src.add(i)));
140 i += 1;
141 }
142}
143
144/// Backward copy of `bytes` bytes with 8-byte granularity.
145///
146/// Required when `dst > src` and the ranges overlap (scrolling down): a
147/// forward copy would overwrite source bytes before they are read.
148///
149/// # Safety
150/// Both ranges must be valid for `bytes` bytes.
151pub unsafe fn copy_backward(dst: *mut u8, src: *const u8, bytes: usize) {
152 let mut i = bytes;
153 while i >= 8 {
154 i -= 8;
155 let v = core::ptr::read_unaligned(src.add(i) as *const u64);
156 core::ptr::write_unaligned(dst.add(i) as *mut u64, v);
157 }
158 while i > 0 {
159 i -= 1;
160 core::ptr::write_unaligned(dst.add(i), core::ptr::read_unaligned(src.add(i)));
161 }
162}
163
164/// Fence write-combining stores so they become visible on the panel.
165#[inline]
166pub fn fence() {
167 #[cfg(target_arch = "x86_64")]
168 unsafe {
169 core::arch::asm!("sfence", options(nostack, preserves_flags));
170 }
171}