Skip to content
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

Add Virtio-console #143

Merged
merged 1 commit into from
Nov 14, 2024
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
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"crates/driver_block",
"crates/driver_common",
"crates/driver_display",
"crates/driver_console",
"crates/driver_net",
"crates/driver_pci",
"crates/driver_virtio",
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ FEATURES ?=
APP_FEATURES ?=

# QEMU options
CONSOLE ?= n
BLK ?= n
NET ?= n
GRAPHIC ?= n
Expand Down
3 changes: 3 additions & 0 deletions api/ruxfeat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ virtio-9p = [
]
net-9p = ["9pfs", "net", "rux9p/net-9p", "ruxruntime/net-9p"]

# virtio console
virtio_console = ["ruxhal/virtio_console", "ruxruntime/virtio_console", "alloc", "ruxdriver/virtio_console", "ruxdriver/virtio"]

# Device drivers
bus-mmio = ["ruxdriver?/bus-mmio"]
bus-pci = ["ruxdriver?/bus-pci"]
Expand Down
18 changes: 18 additions & 0 deletions crates/driver_console/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "driver_console"
version = "0.1.0"
edition = "2021"
authors = [
"Hangqi Ren <[email protected]>"
]
description = "Common traits and types for console drivers"
license = "GPL-3.0-or-later OR Apache-2.0"
homepage = "https://github.com/syswonder/ruxos"
repository = "https://github.com/syswonder/ruxos/tree/main/crates/driver_console"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = []

[dependencies]
driver_common = { path = "../driver_common" }
27 changes: 27 additions & 0 deletions crates/driver_console/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) [2023] [Syswonder Community]
* [Ruxos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

//! Common traits and types for block storage device drivers (i.e. disk).

#![no_std]
#![feature(doc_auto_cfg)]
#![feature(const_trait_impl)]

#[doc(no_inline)]
pub use driver_common::{BaseDriverOps, DevError, DevResult, DeviceType};

/// Operations that require a console device driver to implement.
pub trait ConsoleDriverOps: BaseDriverOps {
/// Writes a single byte to the console.
fn putchar(&mut self, c: u8);
/// Reads a single byte from the console.
fn getchar(&mut self) -> Option<u8>;
/// Acknowledge an interrupt from the console.
fn ack_interrupt(&mut self) -> DevResult<bool>;
}
2 changes: 2 additions & 0 deletions crates/driver_virtio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ block = ["driver_block"]
net = ["driver_net"]
gpu = ["driver_display"]
v9p = ["driver_9p"]
console = ["driver_console"]

[dependencies]
log = "0.4"
Expand All @@ -23,4 +24,5 @@ driver_block = { path = "../driver_block", optional = true }
driver_net = { path = "../driver_net", optional = true }
driver_display = { path = "../driver_display", optional = true}
driver_9p = { path = "../driver_9p", optional = true}
driver_console = { path = "../driver_console", optional = true}
virtio-drivers = { git = "https://github.com/syswonder/virtio-drivers.git", rev = "62dbe5a" }
59 changes: 59 additions & 0 deletions crates/driver_virtio/src/console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright (c) [2023] [Syswonder Community]
* [Ruxos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

use crate::as_dev_err;
use driver_common::{BaseDriverOps, DevResult, DeviceType};
use driver_console::ConsoleDriverOps;
use virtio_drivers::{device::console::VirtIOConsole as InnerDev, transport::Transport, Hal};

/// VirtIO console device
pub struct VirtIoConsoleDev<H: Hal, T: Transport> {
inner: InnerDev<'static, H, T>,
}

unsafe impl<H: Hal, T: Transport> Send for VirtIoConsoleDev<H, T> {}
unsafe impl<H: Hal, T: Transport> Sync for VirtIoConsoleDev<H, T> {}

impl<H: Hal, T: Transport> VirtIoConsoleDev<H, T> {
/// Creates a new driver instance and initializes the device, or returns
/// an error if any step fails.
pub fn try_new(transport: T) -> DevResult<Self> {
Ok(Self {
inner: InnerDev::new(transport).map_err(as_dev_err)?,
})
}
}

impl<H: Hal, T: Transport> BaseDriverOps for VirtIoConsoleDev<H, T> {
fn device_name(&self) -> &str {
"virtio-console"
}

fn device_type(&self) -> DeviceType {
DeviceType::Char
}
}

impl<H: Hal, T: Transport> ConsoleDriverOps for VirtIoConsoleDev<H, T> {
fn putchar(&mut self, c: u8) {
self.inner
.send(c)
.expect("VirtConsole: failed to send char");
}

fn getchar(&mut self) -> Option<u8> {
self.inner
.recv(true)
.expect("VirtConsole: failed to recv char")
}

fn ack_interrupt(&mut self) -> DevResult<bool> {
self.inner.ack_interrupt().map_err(as_dev_err)
}
}
5 changes: 5 additions & 0 deletions crates/driver_virtio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#[cfg(feature = "block")]
mod blk;
#[cfg(feature = "console")]
mod console;
#[cfg(feature = "gpu")]
mod gpu;
#[cfg(feature = "net")]
Expand All @@ -34,6 +36,8 @@ mod v9p;

#[cfg(feature = "block")]
pub use self::blk::VirtIoBlkDev;
#[cfg(feature = "console")]
pub use self::console::VirtIoConsoleDev;
#[cfg(feature = "gpu")]
pub use self::gpu::VirtIoGpuDev;
#[cfg(feature = "net")]
Expand Down Expand Up @@ -89,6 +93,7 @@ const fn as_dev_type(t: VirtIoDevType) -> Option<DeviceType> {
Network => Some(DeviceType::Net),
GPU => Some(DeviceType::Display),
_9P => Some(DeviceType::_9P),
Console => Some(DeviceType::Char),
_ => None,
}
}
Expand Down
15 changes: 9 additions & 6 deletions modules/ruxdriver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ repository = "https://github.com/syswonder/ruxos/tree/main/modules/ruxdriver"
[features]
dyn = []
bus-mmio = []
bus-pci = ["dep:driver_pci", "dep:ruxhal", "dep:ruxconfig"]
bus-pci = ["dep:driver_pci", "dep:ruxhal", "dep:ruxconfig", "virtio_hal"]
net = ["driver_net"]
block = ["driver_block"]
display = ["driver_display"]
_9p = ["driver_9p"]



# Enabled by features `virtio-*`
virtio = ["driver_virtio", "dep:axalloc", "dep:ruxhal", "dep:ruxconfig"]

virtio_hal = ["ruxhal/virtio_hal"]
# various types of drivers
virtio-blk = ["block", "virtio", "driver_virtio/block"]
virtio-net = ["net", "virtio", "driver_virtio/net"]
virtio-gpu = ["display", "virtio", "driver_virtio/gpu"]
virtio-9p = ["_9p","virtio", "driver_virtio/v9p"]
virtio-blk = ["block", "virtio", "driver_virtio/block","virtio_hal"]
virtio-net = ["net", "virtio", "driver_virtio/net", "virtio_hal"]
virtio-gpu = ["display", "virtio", "driver_virtio/gpu", "virtio_hal"]
virtio-9p = ["_9p","virtio", "driver_virtio/v9p", "virtio_hal"]
virtio_console = ["virtio"]
ramdisk = ["block", "driver_block/ramdisk"]
bcm2835-sdhci = ["block", "driver_block/bcm2835-sdhci"]
ixgbe = ["net", "driver_net/ixgbe", "dep:axalloc", "dep:ruxhal"]
Expand Down
7 changes: 7 additions & 0 deletions modules/ruxdriver/src/bus/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@

#[allow(unused_imports)]
use crate::{prelude::*, AllDevices};
#[cfg(all(feature = "virtio_console", feature = "virtio"))]
use ruxhal::virtio::virtio_console;

impl AllDevices {
pub(crate) fn probe_bus_devices(&mut self) {
// TODO: parse device tree
#[cfg(feature = "virtio")]
for reg in ruxconfig::VIRTIO_MMIO_REGIONS {
#[cfg(feature = "virtio_console")]
if virtio_console::is_probe(reg.0) {
warn!("Avoiding virtio-console probe again");
continue;
}
for_each_drivers!(type Driver, {
if let Some(dev) = Driver::probe_mmio(reg.0, reg.1) {
info!(
Expand Down
47 changes: 6 additions & 41 deletions modules/ruxdriver/src/virtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
* See the Mulan PSL v2 for more details.
*/

use core::marker::PhantomData;
use core::ptr::NonNull;

use axalloc::global_allocator;
use crate::{drivers::DriverProbe, AxDeviceEnum};
use cfg_if::cfg_if;
use core::marker::PhantomData;
use driver_common::{BaseDriverOps, DevResult, DeviceType};
use driver_virtio::{BufferDirection, PhysAddr, VirtIoHal};
use ruxhal::mem::{direct_virt_to_phys, phys_to_virt, virt_to_phys};
#[cfg(bus = "mmio")]
use ruxhal::mem::phys_to_virt;

use crate::{drivers::DriverProbe, AxDeviceEnum};
#[cfg(feature = "virtio_hal")]
use ruxhal::virtio::virtio_hal::VirtIoHalImpl;

cfg_if! {
if #[cfg(bus = "pci")] {
Expand Down Expand Up @@ -161,37 +160,3 @@ impl<D: VirtIoDevMeta> DriverProbe for VirtIoDriver<D> {
None
}
}

pub struct VirtIoHalImpl;

unsafe impl VirtIoHal for VirtIoHalImpl {
fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) {
let vaddr = if let Ok(vaddr) = global_allocator().alloc_pages(pages, 0x1000) {
vaddr
} else {
return (0, NonNull::dangling());
};
let paddr = direct_virt_to_phys(vaddr.into());
let ptr = NonNull::new(vaddr as _).unwrap();
(paddr.as_usize(), ptr)
}

unsafe fn dma_dealloc(_paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32 {
global_allocator().dealloc_pages(vaddr.as_ptr() as usize, pages);
0
}

#[inline]
unsafe fn mmio_phys_to_virt(paddr: PhysAddr, _size: usize) -> NonNull<u8> {
NonNull::new(phys_to_virt(paddr.into()).as_mut_ptr()).unwrap()
}

#[inline]
unsafe fn share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr {
let vaddr = buffer.as_ptr() as *mut u8 as usize;
virt_to_phys(vaddr.into()).into()
}

#[inline]
unsafe fn unshare(_paddr: PhysAddr, _buffer: NonNull<[u8]>, _direction: BufferDirection) {}
}
8 changes: 8 additions & 0 deletions modules/ruxhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ homepage = "https://github.com/syswonder/ruxos"
repository = "https://github.com/syswonder/ruxos/tree/main/modules/ruxhal"

[features]
virtio_hal = ["driver_virtio","virtio-drivers","axalloc"]
smp = []
alloc = []
fp_simd = []
Expand All @@ -22,6 +23,8 @@ tls = ["alloc"]
default = []
musl = []
signal = []
virtio_console = ["driver_console", "driver_virtio", "driver_virtio/console", "driver_common", "virtio-drivers", "axalloc", "lazy_static", "alloc", "virtio_hal"]


[dependencies]
log = "0.4"
Expand All @@ -39,6 +42,11 @@ lazy_init = { path = "../../crates/lazy_init" }
page_table = { path = "../../crates/page_table", optional = true }
page_table_entry = { path = "../../crates/page_table_entry" }
percpu = { path = "../../crates/percpu" }
driver_console = { path = "../../crates/driver_console", optional = true }
driver_virtio = { path = "../../crates/driver_virtio", optional = true }
driver_common = { path = "../../crates/driver_common", optional = true }
virtio-drivers = { git = "https://github.com/syswonder/virtio-drivers.git", rev = "62dbe5a", optional = true }
lazy_static = { version = "1.4", features = ["spin_no_std"], optional = true }
memory_addr = "0.1.0"
handler_table = "0.1.0"
crate_interface = "0.1.1"
Expand Down
4 changes: 2 additions & 2 deletions modules/ruxhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
#[macro_use]
extern crate log;

mod platform;

pub mod arch;
pub mod cpu;
pub mod mem;
mod platform;
pub mod time;
pub mod trap;
pub mod virtio;

#[cfg(feature = "tls")]
pub mod tls;
Expand Down
6 changes: 6 additions & 0 deletions modules/ruxhal/src/platform/aarch64_common/gic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ pub const MAX_IRQ_COUNT: usize = 1024;
/// The timer IRQ number.
pub const TIMER_IRQ_NUM: usize = translate_irq(14, InterruptType::PPI).unwrap();

#[cfg(not(feature = "virtio_console"))]
/// The UART IRQ number.
pub const UART_IRQ_NUM: usize = translate_irq(ruxconfig::UART_IRQ, InterruptType::SPI).unwrap();

#[cfg(all(feature = "irq", feature = "virtio_console"))]
/// The Virtio-console IRQ number
pub const VIRTIO_CONSOLE_IRQ_NUM: usize =
translate_irq(ruxconfig::VIRTIO_CONSOLE_IRQ, InterruptType::SPI).unwrap();

const GICD_BASE: PhysAddr = PhysAddr::from(ruxconfig::GICD_PADDR);
const GICC_BASE: PhysAddr = PhysAddr::from(ruxconfig::GICC_PADDR);

Expand Down
Loading
Loading