Skip to content

add support of virtio console according to the VirtIO standard #1781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ jobs:
if: matrix.arch == 'x86_64'
- run: cargo xtask ci rs --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} --package stdin qemu
if: matrix.arch == 'x86_64'
- run: cargo xtask ci rs --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} --package stdin --features hermit/console qemu --devices virtio-console-pci
if: matrix.arch != 'riscv64'
- run: cargo xtask ci rs --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} --package stdin --features hermit/console --no-default-features qemu --devices virtio-console-mmio
if: matrix.arch != 'x86_64'
- run: cargo xtask ci rs --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} --package rusty_demo --features fs qemu ${{ matrix.flags }} --devices virtio-fs-pci
if: matrix.arch == 'x86_64'
- run: cargo xtask ci rs --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} --package rusty_demo --features fs --smp 4 qemu ${{ matrix.flags }} --devices virtio-fs-pci
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ harness = false
default = ["pci", "pci-ids", "acpi", "fsgsbase", "smp", "tcp", "dhcpv4", "fuse", "vsock"]
acpi = []
common-os = []
console = []
dhcpv4 = ["smoltcp", "smoltcp/proto-dhcpv4", "smoltcp/socket-dhcpv4"]
dns = ["smoltcp", "smoltcp/socket-dns"]
fs = ["fuse"]
Expand Down
153 changes: 140 additions & 13 deletions src/arch/aarch64/kernel/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ use alloc::vec::Vec;
use core::ptr::NonNull;

use align_address::Align;
use arm_gic::{IntId, Trigger};
use hermit_sync::{InterruptTicketMutex, without_interrupts};
use virtio::mmio::{DeviceRegisters, DeviceRegistersVolatileFieldAccess};
use volatile::VolatileRef;

use crate::arch::aarch64::kernel::interrupts::GIC;
use crate::arch::aarch64::mm::paging::{self, PageSize};
#[cfg(feature = "console")]
use crate::console::IoDevice;
#[cfg(feature = "console")]
use crate::drivers::console::VirtioConsoleDriver;
#[cfg(feature = "console")]
use crate::drivers::console::VirtioUART;
#[cfg(any(feature = "tcp", feature = "udp"))]
use crate::drivers::net::virtio::VirtioNetDriver;
use crate::drivers::virtio::transport::mmio::{self as mmio_virtio, VirtioDriver};
use crate::init_cell::InitCell;
Expand All @@ -17,13 +26,26 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
pub(crate) enum MmioDriver {
#[cfg(any(feature = "tcp", feature = "udp"))]
VirtioNet(InterruptTicketMutex<VirtioNetDriver>),
#[cfg(feature = "console")]
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver>),
}

impl MmioDriver {
#[cfg(any(feature = "tcp", feature = "udp"))]
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<VirtioNetDriver>> {
match self {
Self::VirtioNet(drv) => Some(drv),
#[cfg(feature = "console")]
_ => None,
}
}

#[cfg(feature = "console")]
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver>> {
match self {
Self::VirtioConsole(drv) => Some(drv),
#[cfg(any(feature = "tcp", feature = "udp"))]
_ => None,
}
}
}
Expand All @@ -40,6 +62,14 @@ pub(crate) fn get_network_driver() -> Option<&'static InterruptTicketMutex<Virti
.find_map(|drv| drv.get_network_driver())
}

#[cfg(feature = "console")]
pub(crate) fn get_console_driver() -> Option<&'static InterruptTicketMutex<VirtioConsoleDriver>> {
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_console_driver())
}

pub fn init_drivers() {
without_interrupts(|| {
if let Some(fdt) = crate::env::fdt() {
Expand All @@ -53,12 +83,16 @@ pub fn init_drivers() {
.next()
.unwrap();
let mut irq = 0;
let mut irqtype = 0;
let mut irqflags = 0;

for prop in node.properties() {
if prop.name == "interrupts" {
irq = u32::from_be_bytes(prop.value[4..8].try_into().unwrap())
.try_into()
.unwrap();
irqtype =
u32::from_be_bytes(prop.value[0..4].try_into().unwrap());
irq = u32::from_be_bytes(prop.value[4..8].try_into().unwrap());
irqflags =
u32::from_be_bytes(prop.value[8..12].try_into().unwrap());
break;
}
}
Expand Down Expand Up @@ -95,17 +129,100 @@ pub fn init_drivers() {

// Verify the device-ID to find the network card
let id = mmio.as_ptr().device_id().read();

#[cfg(any(feature = "tcp", feature = "udp"))]
if id == virtio::Id::Net {
trace!("Found network card at {mmio:p}, irq: {irq}");
if let Ok(VirtioDriver::Network(drv)) =
mmio_virtio::init_device(mmio, irq.try_into().unwrap())
{
register_driver(MmioDriver::VirtioNet(
hermit_sync::InterruptTicketMutex::new(drv),
));
let cpu_id: usize = 0;

match id {
#[cfg(any(feature = "tcp", feature = "udp"))]
virtio::Id::Net => {
debug!(
"Found network card at {mmio:p}, irq: {irq}, type: {irqtype}, flags: {irqflags}"
);
if let Ok(VirtioDriver::Network(drv)) =
mmio_virtio::init_device(mmio, irq.try_into().unwrap())
&& let Some(gic) = GIC.lock().as_mut()
{
// enable timer interrupt
let virtio_irqid = if irqtype == 1 {
IntId::ppi(irq)
} else if irqtype == 0 {
IntId::spi(irq)
} else {
panic!("Invalid interrupt type");
};
gic.set_interrupt_priority(
virtio_irqid,
Some(cpu_id),
0x00,
);
if (irqflags & 0xf) == 4 || (irqflags & 0xf) == 8 {
gic.set_trigger(
virtio_irqid,
Some(cpu_id),
Trigger::Level,
);
} else if (irqflags & 0xf) == 2 || (irqflags & 0xf) == 1 {
gic.set_trigger(
virtio_irqid,
Some(cpu_id),
Trigger::Edge,
);
} else {
panic!("Invalid interrupt level!");
}
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true);

register_driver(MmioDriver::VirtioNet(
hermit_sync::InterruptTicketMutex::new(drv),
));
}
}
#[cfg(feature = "console")]
virtio::Id::Console => {
debug!(
"Found console at {mmio:p}, irq: {irq}, type: {irqtype}, flags: {irqflags}"
);
if let Ok(VirtioDriver::Console(drv)) =
mmio_virtio::init_device(mmio, irq.try_into().unwrap())
{
if let Some(gic) = GIC.lock().as_mut() {
// enable timer interrupt
let virtio_irqid = if irqtype == 1 {
IntId::ppi(irq)
} else if irqtype == 0 {
IntId::spi(irq)
} else {
panic!("Invalid interrupt type");
};
gic.set_interrupt_priority(
virtio_irqid,
Some(cpu_id),
0x00,
);
if (irqflags & 0xf) == 4 || (irqflags & 0xf) == 8 {
gic.set_trigger(
virtio_irqid,
Some(cpu_id),
Trigger::Level,
);
} else if (irqflags & 0xf) == 2 || (irqflags & 0xf) == 1
{
gic.set_trigger(
virtio_irqid,
Some(cpu_id),
Trigger::Edge,
);
} else {
panic!("Invalid interrupt level!");
}
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true);

register_driver(MmioDriver::VirtioConsole(
hermit_sync::InterruptTicketMutex::new(*drv),
));
}
}
}
_ => {}
}
}
}
Expand All @@ -117,4 +234,14 @@ pub fn init_drivers() {
});

MMIO_DRIVERS.finalize();

#[cfg(feature = "console")]
{
if get_console_driver().is_some() {
info!("Switch to virtio console");
crate::console::CONSOLE
.lock()
.replace_device(IoDevice::Virtio(VirtioUART::new()));
}
}
}
53 changes: 4 additions & 49 deletions src/arch/aarch64/kernel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod core_local;
pub mod interrupts;
#[cfg(all(not(feature = "pci"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(feature = "pci"),
any(feature = "tcp", feature = "udp", feature = "console")
))]
pub mod mmio;
#[cfg(feature = "pci")]
pub mod pci;
Expand All @@ -15,63 +18,15 @@ pub mod systemtime;
use alloc::alloc::{Layout, alloc};
use core::arch::global_asm;
use core::sync::atomic::{AtomicPtr, AtomicU32, Ordering};
use core::task::Waker;
use core::{ptr, str};

use memory_addresses::arch::aarch64::{PhysAddr, VirtAddr};

use crate::arch::aarch64::kernel::core_local::*;
use crate::arch::aarch64::kernel::serial::SerialPort;
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
use crate::config::*;
use crate::env;

const SERIAL_PORT_BAUDRATE: u32 = 115_200;

pub(crate) struct Console {
serial_port: SerialPort,
}

impl Console {
pub fn new() -> Self {
CoreLocal::install();

let base = env::boot_info()
.hardware_info
.serial_port_base
.map(|uartport| uartport.get())
.unwrap_or_default()
.try_into()
.unwrap();

let serial_port = SerialPort::new(base);

serial_port.init(SERIAL_PORT_BAUDRATE);

Self { serial_port }
}

pub fn write(&mut self, buf: &[u8]) {
self.serial_port.write_buf(buf);
}

pub fn read(&mut self) -> Option<u8> {
None
}

pub fn is_empty(&self) -> bool {
true
}

pub fn register_waker(&mut self, _waker: &Waker) {}
}

impl Default for Console {
fn default() -> Self {
Self::new()
}
}

#[repr(align(8))]
pub(crate) struct AlignedAtomicU32(AtomicU32);

Expand Down
Loading