refactor file.rs
This commit is contained in:
parent
f3e31d869a
commit
52cad30dad
@ -55,7 +55,7 @@ pub unsafe fn devintr() -> i32 {
|
|||||||
|
|
||||||
// IRQ indicates which device interrupted.
|
// IRQ indicates which device interrupted.
|
||||||
let irq = interrupt::handle_interrupt();
|
let irq = interrupt::handle_interrupt();
|
||||||
|
|
||||||
let mut uart_interrupt = false;
|
let mut uart_interrupt = false;
|
||||||
for (uart_irq, uart) in &crate::hardware::UARTS {
|
for (uart_irq, uart) in &crate::hardware::UARTS {
|
||||||
if irq == *uart_irq {
|
if irq == *uart_irq {
|
||||||
@ -63,7 +63,7 @@ pub unsafe fn devintr() -> i32 {
|
|||||||
uart.interrupt();
|
uart.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !uart_interrupt {
|
if !uart_interrupt {
|
||||||
if irq == VIRTIO0_IRQ {
|
if irq == VIRTIO0_IRQ {
|
||||||
virtio_disk_intr();
|
virtio_disk_intr();
|
||||||
|
@ -12,12 +12,12 @@ pub mod printf;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
fs::file::{devsw, CONSOLE},
|
fs::file::{devsw, CONSOLE},
|
||||||
|
hardware::uart::Uart,
|
||||||
proc::{
|
proc::{
|
||||||
process::{procdump, Process},
|
process::{procdump, Process},
|
||||||
scheduler::wakeup,
|
scheduler::wakeup,
|
||||||
},
|
},
|
||||||
sync::mutex::Mutex,
|
sync::mutex::Mutex,
|
||||||
hardware::uart::Uart,
|
|
||||||
};
|
};
|
||||||
use core::{ffi::c_void, ptr::addr_of_mut};
|
use core::{ffi::c_void, ptr::addr_of_mut};
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ extern "C" {
|
|||||||
fn either_copyout(user_dst: i32, dst: u64, src: *mut c_void, len: u64) -> i32;
|
fn either_copyout(user_dst: i32, dst: u64, src: *mut c_void, len: u64) -> i32;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static UART0: &'static Uart = &crate::hardware::UARTS[0].1;
|
pub static UART0: &Uart = &crate::hardware::UARTS[0].1;
|
||||||
|
|
||||||
pub const BACKSPACE: u8 = 0x00;
|
pub const BACKSPACE: u8 = 0x00;
|
||||||
pub const INPUT_BUF_SIZE: usize = 128;
|
pub const INPUT_BUF_SIZE: usize = 128;
|
||||||
|
@ -5,7 +5,7 @@ use crate::{
|
|||||||
fs::{log, stat::Stat},
|
fs::{log, stat::Stat},
|
||||||
io::pipe::Pipe,
|
io::pipe::Pipe,
|
||||||
proc::process::Process,
|
proc::process::Process,
|
||||||
sync::{sleeplock::Sleeplock, spinlock::Spinlock},
|
sync::{mutex::Mutex, sleeplock::Sleeplock, spinlock::Spinlock},
|
||||||
};
|
};
|
||||||
use core::ptr::{addr_of_mut, null_mut};
|
use core::ptr::{addr_of_mut, null_mut};
|
||||||
|
|
||||||
@ -36,8 +36,9 @@ pub struct File {
|
|||||||
/// FileType::Device
|
/// FileType::Device
|
||||||
pub major: i16,
|
pub major: i16,
|
||||||
}
|
}
|
||||||
|
unsafe impl Send for File {}
|
||||||
impl File {
|
impl File {
|
||||||
pub const unsafe fn uninitialized() -> File {
|
pub const fn uninitialized() -> File {
|
||||||
File {
|
File {
|
||||||
kind: FileType::None,
|
kind: FileType::None,
|
||||||
references: 0,
|
references: 0,
|
||||||
@ -121,33 +122,15 @@ pub struct FileTable {
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub static mut devsw: [Devsw; crate::NDEV] = [Devsw::new(); crate::NDEV];
|
pub static mut devsw: [Devsw; crate::NDEV] = [Devsw::new(); crate::NDEV];
|
||||||
#[no_mangle]
|
pub static FILES: Mutex<[File; crate::NFILE]> = Mutex::new([File::uninitialized(); crate::NFILE]);
|
||||||
pub static mut ftable: FileTable = FileTable {
|
|
||||||
lock: Spinlock::new(),
|
|
||||||
files: unsafe { [File::uninitialized(); crate::NFILE] },
|
|
||||||
};
|
|
||||||
pub const CONSOLE: usize = 1;
|
pub const CONSOLE: usize = 1;
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
// pub fn fileinit();
|
|
||||||
// pub fn filealloc() -> *mut File;
|
|
||||||
// pub fn filedup(file: *mut File) -> *mut File;
|
|
||||||
// pub fn fileclose(file: *mut File);
|
|
||||||
// pub fn filestat(file: *mut File, addr: u64) -> i32;
|
|
||||||
// pub fn fileread(file: *mut File, addr: u64, n: i32) -> i32;
|
|
||||||
// pub fn filewrite(file: *mut File, addr: u64, n: i32) -> i32;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub unsafe fn fileinit() {
|
|
||||||
ftable.lock = Spinlock::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Allocate a file structure.
|
/// Allocate a file structure.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn filealloc() -> *mut File {
|
pub unsafe extern "C" fn filealloc() -> *mut File {
|
||||||
let _guard = ftable.lock.lock();
|
let mut files = FILES.lock_spinning();
|
||||||
|
|
||||||
for file in &mut ftable.files {
|
for file in files.as_mut() {
|
||||||
if file.references == 0 {
|
if file.references == 0 {
|
||||||
file.references = 1;
|
file.references = 1;
|
||||||
return addr_of_mut!(*file);
|
return addr_of_mut!(*file);
|
||||||
@ -158,9 +141,8 @@ pub unsafe extern "C" fn filealloc() -> *mut File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Increment reference count for file `file`.
|
/// Increment reference count for file `file`.
|
||||||
#[no_mangle]
|
pub unsafe fn filedup(file: *mut File) -> *mut File {
|
||||||
pub unsafe extern "C" fn filedup(file: *mut File) -> *mut File {
|
let _guard = FILES.lock_spinning();
|
||||||
let _guard = ftable.lock.lock();
|
|
||||||
|
|
||||||
if (*file).references < 1 {
|
if (*file).references < 1 {
|
||||||
panic!("filedup");
|
panic!("filedup");
|
||||||
@ -176,7 +158,7 @@ pub unsafe extern "C" fn filedup(file: *mut File) -> *mut File {
|
|||||||
/// Decrement reference count, and close when reaching 0.
|
/// Decrement reference count, and close when reaching 0.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn fileclose(file: *mut File) {
|
pub unsafe extern "C" fn fileclose(file: *mut File) {
|
||||||
let guard = ftable.lock.lock();
|
let guard = FILES.lock_spinning();
|
||||||
|
|
||||||
if (*file).references < 1 {
|
if (*file).references < 1 {
|
||||||
panic!("fileclose");
|
panic!("fileclose");
|
||||||
@ -204,8 +186,7 @@ pub unsafe extern "C" fn fileclose(file: *mut File) {
|
|||||||
/// Get metadata about file `file`.
|
/// Get metadata about file `file`.
|
||||||
///
|
///
|
||||||
/// `addr` is a user virtual address, pointing to a Stat.
|
/// `addr` is a user virtual address, pointing to a Stat.
|
||||||
#[no_mangle]
|
pub unsafe fn filestat(file: *mut File, addr: u64) -> i32 {
|
||||||
pub unsafe extern "C" fn filestat(file: *mut File, addr: u64) -> i32 {
|
|
||||||
let proc = Process::current().unwrap();
|
let proc = Process::current().unwrap();
|
||||||
let mut stat = Stat::default();
|
let mut stat = Stat::default();
|
||||||
|
|
||||||
@ -234,8 +215,7 @@ pub unsafe extern "C" fn filestat(file: *mut File, addr: u64) -> i32 {
|
|||||||
/// Read from file `file`.
|
/// Read from file `file`.
|
||||||
///
|
///
|
||||||
/// `addr` is a user virtual address.
|
/// `addr` is a user virtual address.
|
||||||
#[no_mangle]
|
pub unsafe fn fileread(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
|
||||||
pub unsafe extern "C" fn fileread(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
|
|
||||||
if (*file).readable == 0 {
|
if (*file).readable == 0 {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -270,8 +250,7 @@ pub unsafe extern "C" fn fileread(file: *mut File, addr: u64, num_bytes: i32) ->
|
|||||||
/// Write to file `file`.
|
/// Write to file `file`.
|
||||||
///
|
///
|
||||||
/// `addr` is as user virtual address.
|
/// `addr` is as user virtual address.
|
||||||
#[no_mangle]
|
pub unsafe fn filewrite(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
|
||||||
pub unsafe extern "C" fn filewrite(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
|
|
||||||
if (*file).writable == 0 {
|
if (*file).writable == 0 {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,4 @@ pub mod virtio_disk;
|
|||||||
|
|
||||||
use uart::Uart;
|
use uart::Uart;
|
||||||
|
|
||||||
pub static UARTS: [(usize, Uart); 1] = [
|
pub static UARTS: [(usize, Uart); 1] = [(10, Uart::new(0x1000_0000))];
|
||||||
(10, Uart::new(0x1000_0000)),
|
|
||||||
];
|
|
||||||
|
@ -68,7 +68,6 @@ pub unsafe fn main() -> ! {
|
|||||||
arch::interrupt::inithart();
|
arch::interrupt::inithart();
|
||||||
io::bio::binit();
|
io::bio::binit();
|
||||||
fs::iinit();
|
fs::iinit();
|
||||||
fs::file::fileinit();
|
|
||||||
hardware::virtio_disk::virtio_disk_init();
|
hardware::virtio_disk::virtio_disk_init();
|
||||||
proc::process::userinit();
|
proc::process::userinit();
|
||||||
STARTED = true;
|
STARTED = true;
|
||||||
|
@ -80,6 +80,7 @@ pub enum ProcessError {
|
|||||||
|
|
||||||
/// Per-process state.
|
/// Per-process state.
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Process {
|
pub struct Process {
|
||||||
pub lock: Spinlock,
|
pub lock: Spinlock,
|
||||||
|
|
||||||
|
@ -42,7 +42,10 @@ impl<T> Mutex<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl<T> Sync for Mutex<T> where T: Send {}
|
unsafe impl<T> Sync for Mutex<T> where T: Send {}
|
||||||
impl<T> Clone for Mutex<T> where T: Clone {
|
impl<T> Clone for Mutex<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
let value: T = self.lock_spinning().as_ref().clone();
|
let value: T = self.lock_spinning().as_ref().clone();
|
||||||
Mutex::new(value)
|
Mutex::new(value)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user