Skip to content

Commit

Permalink
Add read_disk that trigger loop read and interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
yodalee committed May 9, 2023
1 parent 10ab4e6 commit 088d612
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
31 changes: 28 additions & 3 deletions src/disk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::virtio::header::VirtioHeader;
use crate::virtio::block::VirtioBlock;
use crate::memorylayout::VIRTIO0;
use crate::virtio::block::{BlockRequest, RequestType, VirtioBlock};
use crate::virtio::header::VirtioHeader;
use crate::virtio::queue::DescriptorFlag;

use core::mem::size_of;
use rv64::asm::sync_synchronize;
use spin::Mutex;

static mut DISK: Mutex<Option<VirtioBlock>> = Mutex::new(None);
Expand All @@ -16,5 +19,27 @@ pub fn init_disk() {
}

pub fn read_disk() {
let disk = unsafe { DISK.lock().as_mut().unwrap() };
let mut disk = unsafe { DISK.lock() };

let request = BlockRequest {
typ: RequestType::In,
reserved: 0,
sector: 0,
};

let block = disk.as_mut().unwrap();
let mut descriptor = unsafe { block.queue.desc.as_mut() };
let mut available = unsafe { block.queue.avail.as_mut() };

// setup block request
descriptor.addr = &request as *const _ as u64;
descriptor.len = size_of::<BlockRequest>() as u32;
descriptor.flags = DescriptorFlag::NEXT;
descriptor.next = 0;

available.idx += 1;

sync_synchronize();

block.header.set_queue_notify(0);
}
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ mod virtio;
mod vm;

use crate::cpu::{get_cpuid, init_cpu};
use crate::disk::init_disk;
use crate::disk::{init_disk, read_disk};
use crate::kalloc::init_heap;
use crate::kvm::{init_kvm, init_page};
use crate::plic::{init_plic, init_hartplic};
use crate::print::println;
use crate::proc::{init_proc, init_userproc};
use crate::scheduler::{init_scheduler, get_scheduler};
use crate::trap::init_harttrap;
use crate::trap::{init_harttrap, intr_on, intr_off};

use linked_list_allocator::LockedHeap;
use alloc::alloc::Layout;
Expand Down Expand Up @@ -80,6 +80,12 @@ pub fn main() -> ! {
init_hartplic(); // ask PLIC for device interrupt
}

// experimentally trigger the read function with interrupt
// uncomment to see the effect
// intr_on();
// read_disk();
// intr_off();

let scheduler = get_scheduler();
// start scheduling, this function shall not return
scheduler.schedule();
Expand Down
20 changes: 18 additions & 2 deletions src/virtio/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use bitflags::bitflags;
///
/// ref: 5.2 Block Device
pub struct VirtioBlock {
header: &'static mut VirtioHeader,
queue: VirtioQueue,
pub header: &'static mut VirtioHeader,
// FIXME: expose method instead of public access
pub queue: VirtioQueue,
}

impl VirtioBlock {
Expand Down Expand Up @@ -81,3 +82,18 @@ bitflags! {
const NOTIFICATION_DATA = 1 << 38;
}
}

#[repr(C)]
#[derive(Debug)]
pub struct BlockRequest {
pub typ: RequestType,
pub reserved: u32,
pub sector: u64,
}

#[repr(u32)]
#[derive(Debug)]
pub enum RequestType {
In = 0,
Out = 1,
}
26 changes: 14 additions & 12 deletions src/virtio/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub struct VirtioQueue {
size: u16,

/// address of descriptor
desc: NonNull<Descriptor>,
pub desc: NonNull<Descriptor>,

/// address of available ring
avail: NonNull<AvailRing>,
pub avail: NonNull<AvailRing>,

/// address of used ring
used: NonNull<UsedRing>,
Expand Down Expand Up @@ -69,15 +69,16 @@ impl VirtioQueue {
}
}

struct Descriptor {
// FIXME: expose interface instead of public access
pub struct Descriptor {
/// Address
addr: u64,
pub addr: u64,
/// Length
len: u32,
pub len: u32,
/// The flags
flags: DescriptorFlag,
pub flags: DescriptorFlag,
/// Next field if flags & NEXT
next: u16,
pub next: u16,
}

bitflags! {
Expand All @@ -91,11 +92,12 @@ bitflags! {
}
}

struct AvailRing {
flags: AvailRingFlag,
idx: u16,
ring: [u16; 32],
used_event: u16,
// FIXME: expose interface instead of public access
pub struct AvailRing {
pub flags: AvailRingFlag,
pub idx: u16,
pub ring: [u16; 32],
pub used_event: u16,
}

bitflags! {
Expand Down

0 comments on commit 088d612

Please sign in to comment.