mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-30 10:23:29 +00:00
Reorganize the codebase
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
888853a6de
commit
271a16d492
22
services/comps/block/Cargo.toml
Normal file
22
services/comps/block/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "jinux-block"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bitflags = "1.3"
|
||||
spin = "0.9.4"
|
||||
jinux-frame = { path = "../../../framework/jinux-frame" }
|
||||
jinux-pci = { path = "../pci" }
|
||||
jinux-virtio = { path = "../virtio" }
|
||||
jinux-util = { path = "../../libs/jinux-util" }
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
log = "0.4"
|
||||
|
||||
[features]
|
||||
|
||||
[dependencies.lazy_static]
|
||||
version = "1.0"
|
||||
features = ["spin_no_std"]
|
71
services/comps/block/src/lib.rs
Normal file
71
services/comps/block/src/lib.rs
Normal file
@ -0,0 +1,71 @@
|
||||
//! The block device of jinux
|
||||
#![no_std]
|
||||
#![forbid(unsafe_code)]
|
||||
#![feature(fn_traits)]
|
||||
|
||||
mod virtio;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use core::any::Any;
|
||||
|
||||
use alloc::string::ToString;
|
||||
use alloc::sync::Arc;
|
||||
use component::init_component;
|
||||
use component::ComponentInitError;
|
||||
use jinux_virtio::VirtioDeviceType;
|
||||
use spin::Once;
|
||||
use virtio::VirtioBlockDevice;
|
||||
|
||||
pub const BLK_SIZE: usize = 512;
|
||||
|
||||
pub trait BlockDevice: Send + Sync + Any {
|
||||
fn init(&self) {}
|
||||
fn read_block(&self, block_id: usize, buf: &mut [u8]);
|
||||
fn write_block(&self, block_id: usize, buf: &[u8]);
|
||||
fn handle_irq(&self);
|
||||
}
|
||||
|
||||
pub static BLK_COMPONENT: Once<BLKComponent> = Once::new();
|
||||
|
||||
#[init_component]
|
||||
fn blk_component_init() -> Result<(), ComponentInitError> {
|
||||
let a = BLKComponent::init()?;
|
||||
BLK_COMPONENT.call_once(|| a);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub struct BLKComponent {
|
||||
/// Input device map, key is the irq number, value is the Input device
|
||||
blk_device: Arc<dyn BlockDevice>,
|
||||
}
|
||||
|
||||
impl BLKComponent {
|
||||
pub fn init() -> Result<Self, ComponentInitError> {
|
||||
let virtio = jinux_virtio::VIRTIO_COMPONENT.get().unwrap();
|
||||
let devices = virtio.get_device(VirtioDeviceType::Block);
|
||||
for device in devices {
|
||||
let v_device = VirtioBlockDevice::new(device);
|
||||
return Ok(Self {
|
||||
blk_device: Arc::new(v_device),
|
||||
});
|
||||
}
|
||||
Err(ComponentInitError::UninitializedDependencies(
|
||||
"Virtio".to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
pub const fn name() -> &'static str {
|
||||
"Block device"
|
||||
}
|
||||
// 0~65535
|
||||
pub const fn priority() -> u16 {
|
||||
8192
|
||||
}
|
||||
}
|
||||
|
||||
impl BLKComponent {
|
||||
pub fn get_device(self: &Self) -> Arc<dyn BlockDevice> {
|
||||
self.blk_device.clone()
|
||||
}
|
||||
}
|
54
services/comps/block/src/virtio.rs
Normal file
54
services/comps/block/src/virtio.rs
Normal file
@ -0,0 +1,54 @@
|
||||
//! Block device based on Virtio
|
||||
|
||||
use jinux_frame::trap::TrapFrame;
|
||||
use jinux_pci::msix::MSIX;
|
||||
use jinux_util::frame_ptr::InFramePtr;
|
||||
use jinux_virtio::{device::block::device::BLKDevice, PCIVirtioDevice, VitrioPciCommonCfg};
|
||||
use log::debug;
|
||||
use spin::Mutex;
|
||||
|
||||
use crate::{BlockDevice, BLK_COMPONENT};
|
||||
|
||||
pub struct VirtioBlockDevice {
|
||||
blk_device: Mutex<BLKDevice>,
|
||||
pub common_cfg: InFramePtr<VitrioPciCommonCfg>,
|
||||
msix: MSIX,
|
||||
}
|
||||
|
||||
impl BlockDevice for VirtioBlockDevice {
|
||||
fn read_block(&self, block_id: usize, buf: &mut [u8]) {
|
||||
self.blk_device.lock().read_block(block_id, buf);
|
||||
}
|
||||
|
||||
/// it is blocking now
|
||||
fn write_block(&self, block_id: usize, buf: &[u8]) {
|
||||
self.blk_device.lock().write_block(block_id, buf);
|
||||
}
|
||||
|
||||
fn handle_irq(&self) {
|
||||
debug!("block device handle irq");
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtioBlockDevice {
|
||||
pub(crate) fn new(mut virtio_device: PCIVirtioDevice) -> Self {
|
||||
fn handle_block_device(_: &TrapFrame) {
|
||||
BLK_COMPONENT.get().unwrap().blk_device.handle_irq()
|
||||
}
|
||||
fn config_space_change(_: &TrapFrame) {
|
||||
debug!("block device config space change");
|
||||
}
|
||||
virtio_device.register_interrupt_functions(&config_space_change, &handle_block_device);
|
||||
let blk_device = Mutex::new(match virtio_device.device {
|
||||
jinux_virtio::device::VirtioDevice::Block(blk) => blk,
|
||||
_ => {
|
||||
panic!("Error when creating new block device, the input device is other type of virtio device");
|
||||
}
|
||||
});
|
||||
Self {
|
||||
blk_device,
|
||||
common_cfg: virtio_device.common_cfg,
|
||||
msix: virtio_device.msix,
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user