Skip to content

Commit 53289a6

Browse files
committed
Merged PR #87
1 parent a2f3dc3 commit 53289a6

File tree

6 files changed

+66
-52
lines changed

6 files changed

+66
-52
lines changed

os/src/drivers/block/virtio_blk.rs

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
use super::BlockDevice;
2-
use crate::mm::{
3-
frame_alloc, frame_dealloc, kernel_token, FrameTracker, PageTable, PhysAddr, PhysPageNum,
4-
StepByOne, VirtAddr,
5-
};
62
use crate::sync::{Condvar, UPIntrFreeCell};
73
use crate::task::schedule;
84
use crate::DEV_NON_BLOCKING_ACCESS;
95
use alloc::collections::BTreeMap;
10-
use alloc::vec::Vec;
11-
use lazy_static::*;
126
use virtio_drivers::{BlkResp, RespStatus, VirtIOBlk, VirtIOHeader};
7+
use crate::drivers::bus::virtio::VirtioHal;
138

149
#[allow(unused)]
1510
const VIRTIO0: usize = 0x10008000;
1611

1712
pub struct VirtIOBlock {
18-
virtio_blk: UPIntrFreeCell<VirtIOBlk<'static>>,
13+
virtio_blk: UPIntrFreeCell<VirtIOBlk<'static, VirtioHal>>,
1914
condvars: BTreeMap<u16, Condvar>,
2015
}
2116

22-
lazy_static! {
23-
static ref QUEUE_FRAMES: UPIntrFreeCell<Vec<FrameTracker>> =
24-
unsafe { UPIntrFreeCell::new(Vec::new()) };
25-
}
26-
2717
impl BlockDevice for VirtIOBlock {
2818
fn read_block(&self, block_id: usize, buf: &mut [u8]) {
2919
let nb = *DEV_NON_BLOCKING_ACCESS.exclusive_access();
@@ -79,7 +69,7 @@ impl BlockDevice for VirtIOBlock {
7969
impl VirtIOBlock {
8070
pub fn new() -> Self {
8171
let virtio_blk = unsafe {
82-
UPIntrFreeCell::new(VirtIOBlk::new(&mut *(VIRTIO0 as *mut VirtIOHeader)).unwrap())
72+
UPIntrFreeCell::new(VirtIOBlk::<VirtioHal>::new(&mut *(VIRTIO0 as *mut VirtIOHeader)).unwrap())
8373
};
8474
let mut condvars = BTreeMap::new();
8575
let channels = virtio_blk.exclusive_access().virt_queue_size();
@@ -94,38 +84,3 @@ impl VirtIOBlock {
9484
}
9585
}
9686

97-
#[no_mangle]
98-
pub extern "C" fn virtio_dma_alloc(pages: usize) -> PhysAddr {
99-
let mut ppn_base = PhysPageNum(0);
100-
for i in 0..pages {
101-
let frame = frame_alloc().unwrap();
102-
if i == 0 {
103-
ppn_base = frame.ppn;
104-
}
105-
assert_eq!(frame.ppn.0, ppn_base.0 + i);
106-
QUEUE_FRAMES.exclusive_access().push(frame);
107-
}
108-
ppn_base.into()
109-
}
110-
111-
#[no_mangle]
112-
pub extern "C" fn virtio_dma_dealloc(pa: PhysAddr, pages: usize) -> i32 {
113-
let mut ppn_base: PhysPageNum = pa.into();
114-
for _ in 0..pages {
115-
frame_dealloc(ppn_base);
116-
ppn_base.step();
117-
}
118-
0
119-
}
120-
121-
#[no_mangle]
122-
pub extern "C" fn virtio_phys_to_virt(paddr: PhysAddr) -> VirtAddr {
123-
VirtAddr(paddr.0)
124-
}
125-
126-
#[no_mangle]
127-
pub extern "C" fn virtio_virt_to_phys(vaddr: VirtAddr) -> PhysAddr {
128-
PageTable::from_token(kernel_token())
129-
.translate_va(vaddr)
130-
.unwrap()
131-
}

os/src/drivers/bus/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod virtio;

os/src/drivers/bus/virtio.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use alloc::vec::Vec;
2+
use crate::mm::{
3+
frame_alloc, frame_dealloc, kernel_token, FrameTracker, PageTable, PhysAddr, PhysPageNum,
4+
StepByOne, VirtAddr,
5+
};
6+
use crate::sync::UPIntrFreeCell;
7+
use lazy_static::*;
8+
use virtio_drivers::Hal;
9+
10+
lazy_static! {
11+
static ref QUEUE_FRAMES: UPIntrFreeCell<Vec<FrameTracker>> =
12+
unsafe { UPIntrFreeCell::new(Vec::new()) };
13+
}
14+
15+
pub struct VirtioHal;
16+
17+
impl Hal for VirtioHal {
18+
fn dma_alloc(pages: usize) -> usize {
19+
let mut ppn_base = PhysPageNum(0);
20+
for i in 0..pages {
21+
let frame = frame_alloc().unwrap();
22+
if i == 0 {
23+
ppn_base = frame.ppn;
24+
}
25+
assert_eq!(frame.ppn.0, ppn_base.0 + i);
26+
QUEUE_FRAMES.exclusive_access().push(frame);
27+
}
28+
let pa: PhysAddr = ppn_base.into();
29+
pa.0
30+
}
31+
32+
fn dma_dealloc(pa: usize, pages: usize) -> i32 {
33+
let pa = PhysAddr::from(pa);
34+
let mut ppn_base: PhysPageNum = pa.into();
35+
for _ in 0..pages {
36+
frame_dealloc(ppn_base);
37+
ppn_base.step();
38+
}
39+
0
40+
}
41+
42+
fn phys_to_virt(addr: usize) -> usize {
43+
addr
44+
}
45+
46+
fn virt_to_phys(vaddr: usize) -> usize {
47+
PageTable::from_token(kernel_token())
48+
.translate_va(VirtAddr::from(vaddr))
49+
.unwrap()
50+
.0
51+
}
52+
}

os/src/drivers/gpu/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::any::Any;
44
use embedded_graphics::pixelcolor::Rgb888;
55
use tinybmp::Bmp;
66
use virtio_drivers::{VirtIOGpu, VirtIOHeader};
7+
use crate::drivers::bus::virtio::VirtioHal;
78
const VIRTIO7: usize = 0x10007000;
89
pub trait GPUDevice: Send + Sync + Any {
910
fn update_cursor(&self);
@@ -16,14 +17,14 @@ lazy_static::lazy_static!(
1617
);
1718

1819
pub struct VirtIOGPU {
19-
gpu: UPIntrFreeCell<VirtIOGpu<'static>>,
20+
gpu: UPIntrFreeCell<VirtIOGpu<'static, VirtioHal>>,
2021
fb: &'static [u8],
2122
}
2223
static BMP_DATA: &[u8] = include_bytes!("../../assert/mouse.bmp");
2324
impl VirtIOGPU {
2425
pub fn new() -> Self {
2526
unsafe {
26-
let mut virtio = VirtIOGpu::new(&mut *(VIRTIO7 as *mut VirtIOHeader)).unwrap();
27+
let mut virtio = VirtIOGpu::<VirtioHal>::new(&mut *(VIRTIO7 as *mut VirtIOHeader)).unwrap();
2728

2829
let fbuffer = virtio.setup_framebuffer().unwrap();
2930
let len = fbuffer.len();

os/src/drivers/input/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ use embedded_graphics::{
1111
};
1212
use k210_hal::cache::Uncache;
1313
use virtio_drivers::{VirtIOHeader, VirtIOInput};
14+
use crate::drivers::bus::virtio::VirtioHal;
1415
use virtio_input_decoder::{Decoder, Key, KeyType};
1516

1617
use super::GPU_DEVICE;
1718

1819
const VIRTIO5: usize = 0x10005000;
1920
const VIRTIO6: usize = 0x10006000;
2021

21-
struct VirtIOINPUT(UPIntrFreeCell<VirtIOInput<'static>>);
22+
struct VirtIOINPUT(UPIntrFreeCell<VirtIOInput<'static, VirtioHal>>);
2223

2324
pub trait INPUTDevice: Send + Sync + Any {
2425
fn handle_irq(&self);
@@ -32,7 +33,7 @@ lazy_static::lazy_static!(
3233
impl VirtIOINPUT {
3334
pub fn new(addr: usize) -> Self {
3435
Self(unsafe {
35-
UPIntrFreeCell::new(VirtIOInput::new(&mut *(addr as *mut VirtIOHeader)).unwrap())
36+
UPIntrFreeCell::new(VirtIOInput::<VirtioHal>::new(&mut *(addr as *mut VirtIOHeader)).unwrap())
3637
})
3738
}
3839
}

os/src/drivers/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub mod chardev;
44
pub mod gpu;
55
#[cfg(feature = "board_qemu")]
66
pub mod input;
7+
#[cfg(feature = "board_qemu")]
8+
pub mod bus;
79
pub mod plic;
810
pub use block::BLOCK_DEVICE;
911
#[cfg(feature = "board_qemu")]
@@ -12,3 +14,5 @@ pub use chardev::UART;
1214
pub use gpu::*;
1315
#[cfg(feature = "board_qemu")]
1416
pub use input::*;
17+
#[cfg(feature = "board_qemu")]
18+
pub use bus::*;

0 commit comments

Comments
 (0)