refactor: ByteOffset newtype for window/register/ring offsets; named virtio-gpu cmd fields

This commit is contained in:
2026-10-27 21:52:33 +01:00
parent 842d14987c
commit 7b8258b231
13 changed files with 145 additions and 93 deletions

View File

@@ -5,7 +5,7 @@ use hw_virtio::block::{self, DmaAddrs};
use hw_virtio::mmio::{MmioAccess, Registers};
use hw_virtio::queue::VirtioQueue;
use port_block_device::{BlockDevice, SectorId};
use types::address::{PhysicalAddress, VirtualAddress};
use types::address::{ByteOffset, PhysicalAddress, VirtualAddress};
use types::bounded::PageCount;
const MMIO_VA: VirtualAddress = VirtualAddress::from_raw(0x20000);
@@ -38,11 +38,11 @@ impl VirtioBlockDevice {
let at = |offset: usize| PhysicalAddress::from_raw(req.physical.raw() + offset);
let dma = DmaAddrs {
header_pa: at(0),
header: req.window.sub(0, 16),
header: req.window.sub(ByteOffset::new(0), 16),
status_pa: at(16),
status: req.window.sub(16, 1),
status: req.window.sub(ByteOffset::new(16), 1),
data_pa: at(32),
data: req.window.sub(32, SECTOR_SIZE),
data: req.window.sub(ByteOffset::new(32), SECTOR_SIZE),
};
let capacity = regs.read(block::CONFIG_CAPACITY) as u64
| (regs.read(block::CONFIG_CAPACITY + 4) as u64) << 32;

View File

@@ -6,7 +6,7 @@ use hw_virtio::mmio::{MmioAccess, Registers};
use hw_virtio::queue::VirtioQueue;
use libkos::Window;
use port_framebuffer::Framebuffer;
use types::address::{PhysicalAddress, VirtualAddress};
use types::address::{ByteOffset, PhysicalAddress, VirtualAddress};
use types::bounded::PageCount;
use types::display::{Color, Dimension, ScreenCoord};
@@ -50,7 +50,7 @@ impl VirtioFramebuffer {
let dma = GpuDmaAddrs {
cmd_pa: cmd.physical,
cmd: cmd.window,
resp_pa: PhysicalAddress::from_raw(cmd.physical.raw() + gpu::RESPONSE),
resp_pa: PhysicalAddress::from_raw(cmd.physical.raw() + gpu::RESPONSE.raw()),
};
let (w, h) = gpu::get_display_info(&regs, &queue, &dma)
.and_then(|(w, h)| Some((u16::try_from(w).ok()?, u16::try_from(h).ok()?)))
@@ -93,7 +93,7 @@ impl Framebuffer for VirtioFramebuffer {
return;
}
let offset = (y.raw() as usize) * self.stride + (x.raw() as usize) * FB_BPP;
self.pixels.write(offset, color.raw());
self.pixels.write(ByteOffset::new(offset), color.raw());
}
fn fill_rect(&mut self, x: ScreenCoord, y: ScreenCoord, w: Dimension, h: Dimension, color: Color) {
@@ -106,7 +106,7 @@ impl Framebuffer for VirtioFramebuffer {
fn clear(&mut self, color: Color) {
let total = self.width.raw() as usize * self.height.raw() as usize;
self.pixels.fill_u32(0, total, color.raw());
self.pixels.fill_u32(ByteOffset::ZERO, total, color.raw());
}
fn flush(&mut self) {

View File

@@ -3,11 +3,12 @@
use libkos::Window;
use port_input_device::{InputDevice, InputEvent, Key};
use types::address::VirtualAddress;
use types::address::{ByteOffset, VirtualAddress};
const UART_VA: VirtualAddress = VirtualAddress::from_raw(0x20000);
const UART_IER_OFFSET: usize = 1;
const UART_LSR_OFFSET: usize = 5;
const UART_RBR: ByteOffset = ByteOffset::new(0);
const UART_IER_OFFSET: ByteOffset = ByteOffset::new(1);
const UART_LSR_OFFSET: ByteOffset = ByteOffset::new(5);
/// IER bit: interrupt when a received byte is waiting (cleared by reading
/// it, so `acknowledge` has nothing to do).
const IER_RX_READY: u8 = 1;
@@ -27,7 +28,7 @@ impl UartInputDevice {
}
fn read_byte(&self) -> Option<u8> {
self.has_event().then(|| self.regs.read(0))
self.has_event().then(|| self.regs.read(UART_RBR))
}
fn byte_to_key(byte: u8) -> Key {

View File

@@ -7,7 +7,7 @@ use core::sync::atomic::{Ordering, fence};
use hw_virtio::mmio::{MmioAccess, Registers};
use libkos::Window;
use port_input_device::{InputDevice, InputEvent};
use types::address::{PhysicalAddress, VirtualAddress};
use types::address::{ByteOffset, PhysicalAddress, VirtualAddress};
use types::bounded::PageCount;
const INPUT_MMIO_VA: VirtualAddress = VirtualAddress::from_raw(0x25000);
@@ -18,8 +18,8 @@ const QUEUE_SIZE: usize = 32;
const EVENT_SIZE: usize = 8;
const DESC_SIZE: usize = 16;
/// Where the legacy layout puts the available and used rings.
const AVAIL: usize = QUEUE_SIZE * DESC_SIZE;
const USED: usize = 4096;
const AVAIL: ByteOffset = ByteOffset::new(QUEUE_SIZE * DESC_SIZE);
const USED: ByteOffset = ByteOffset::new(4096);
const EV_KEY: u16 = 1;
const KEY_PRESS: u32 = 1;
@@ -67,7 +67,7 @@ impl VirtioInputDevice {
/// Every buffer goes to the device, one event each.
fn fill_queue(&mut self) {
for i in 0..QUEUE_SIZE {
let desc = i * DESC_SIZE;
let desc = ByteOffset::new(i * DESC_SIZE);
self.rings.write(desc, (self.events_pa.raw() + i * EVENT_SIZE) as u64);
self.rings.write(desc + 8, EVENT_SIZE as u32);
self.rings.write(desc + 12, 2u16); // WRITE flag
@@ -92,7 +92,7 @@ impl VirtioInputDevice {
}
fn read_event(&self, desc_idx: u16) -> (u16, u16, u32) {
let at = desc_idx as usize % QUEUE_SIZE * EVENT_SIZE;
let at = ByteOffset::new(desc_idx as usize % QUEUE_SIZE * EVENT_SIZE);
(self.events.read(at), self.events.read(at + 2), self.events.read(at + 4))
}