From c1c8f2876b2c67ea89481605568a764a9a5542c1 Mon Sep 17 00:00:00 2001 From: Stephen Marz Date: Mon, 18 May 2020 09:07:12 -0400 Subject: [PATCH] Remove talloc and tfree, instead we will use Rusts built-in Box --- risc_v/src/block.rs | 43 ++++++++++++++++++++++--------------------- risc_v/src/fs.rs | 27 ++++++++++++--------------- risc_v/src/kmem.rs | 12 ------------ 3 files changed, 34 insertions(+), 48 deletions(-) diff --git a/risc_v/src/block.rs b/risc_v/src/block.rs index db3ce49..a36b227 100755 --- a/risc_v/src/block.rs +++ b/risc_v/src/block.rs @@ -3,7 +3,7 @@ // Stephen Marz // 10 March 2020 -use crate::{kmem::{kfree, kmalloc, talloc, tfree}, +use crate::{kmem::{kfree, kmalloc}, page::{zalloc, PAGE_SIZE}, process::{add_kernel_process_args, get_by_pid, @@ -16,6 +16,7 @@ use crate::{kmem::{kfree, kmalloc, talloc, tfree}, StatusField, VIRTIO_RING_SIZE}}; use core::mem::size_of; +use alloc::boxed::Box; #[repr(C)] pub struct Geometry { @@ -461,8 +462,7 @@ struct ProcArgs { /// This will be a fn read_proc(args_addr: usize) { - let args_ptr = args_addr as *mut ProcArgs; - let args = unsafe { args_ptr.as_ref().unwrap() }; + let args = unsafe { Box::from_raw(args_addr as *mut ProcArgs) }; let _ = block_op( args.dev, args.buffer, @@ -471,7 +471,6 @@ fn read_proc(args_addr: usize) { false, args.pid, ); - tfree(args_ptr); // This should be handled by the RA now. // syscall_exit(); } @@ -484,22 +483,23 @@ pub fn process_read(pid: u16, { // println!("Block read {}, {}, 0x{:x}, {}, {}", pid, dev, buffer as // usize, size, offset); - let args = talloc::().unwrap(); - args.pid = pid; - args.dev = dev; - args.buffer = buffer; - args.size = size; - args.offset = offset; + let args = ProcArgs { + pid, + dev, + buffer, + size, + offset, + }; + let boxed_args = Box::new(args); set_waiting(pid); let _ = add_kernel_process_args( read_proc, - args as *mut ProcArgs as usize, + Box::into_raw(boxed_args) as usize, ); } fn write_proc(args_addr: usize) { - let args_ptr = args_addr as *mut ProcArgs; - let args = unsafe { args_ptr.as_ref().unwrap() }; + let args = unsafe { Box::from_raw(args_addr as *mut ProcArgs) }; let _ = block_op( args.dev, @@ -509,7 +509,6 @@ fn write_proc(args_addr: usize) { true, args.pid, ); - tfree(args_ptr); // syscall_exit(); } @@ -519,15 +518,17 @@ pub fn process_write(pid: u16, size: u32, offset: u64) { - let args = talloc::().unwrap(); - args.pid = pid; - args.dev = dev; - args.buffer = buffer; - args.size = size; - args.offset = offset; + let args = ProcArgs { + pid, + dev, + buffer, + size, + offset, + }; + let boxed_args = Box::new(args); set_waiting(pid); let _ = add_kernel_process_args( write_proc, - args as *mut ProcArgs as usize, + Box::into_raw(boxed_args) as usize, ); } diff --git a/risc_v/src/fs.rs b/risc_v/src/fs.rs index 708a10b..76e7753 100755 --- a/risc_v/src/fs.rs +++ b/risc_v/src/fs.rs @@ -4,7 +4,6 @@ // 16 March 2020 use crate::{cpu::Registers, - kmem::{talloc, tfree}, process::{add_kernel_process_args, get_by_pid, set_running, @@ -12,7 +11,7 @@ use crate::{cpu::Registers, syscall::syscall_block_read}; use crate::{buffer::Buffer, cpu::memcpy}; -use alloc::{collections::BTreeMap, string::String}; +use alloc::{collections::BTreeMap, string::String, boxed::Box}; use core::mem::size_of; pub const MAGIC: u16 = 0x4d5a; @@ -563,8 +562,7 @@ struct ProcArgs { // This is the actual code ran inside of the read process. fn read_proc(args_addr: usize) { - let args_ptr = args_addr as *mut ProcArgs; - let args = unsafe { args_ptr.as_ref().unwrap() }; + let args = unsafe { Box::from_raw(args_addr as *mut ProcArgs) }; // Start the read! Since we're in a kernel process, we can block by putting this // process into a waiting state and wait until the block driver returns. @@ -589,9 +587,6 @@ fn read_proc(args_addr: usize) { // the process and get it ready to go. The only thing this process needs to clean up is the // tfree(), but the user process doesn't care about that. set_running(args.pid); - - // tfree() is used to free a pointer created by talloc. - tfree(args_ptr); } /// System calls will call process_read, which will spawn off a kernel process to read @@ -604,17 +599,19 @@ pub fn process_read(pid: u16, offset: u32) { // println!("FS read {}, {}, 0x{:x}, {}, {}", pid, dev, buffer as usize, size, offset); - let args = talloc::().unwrap(); - args.pid = pid; - args.dev = dev; - args.buffer = buffer; - args.size = size; - args.offset = offset; - args.node = node; + let args = ProcArgs { + pid, + dev, + buffer, + size, + offset, + node, + }; + let boxed_args = Box::new(args); set_waiting(pid); let _ = add_kernel_process_args( read_proc, - args as *mut ProcArgs as usize + Box::into_raw(boxed_args) as usize ); } diff --git a/risc_v/src/kmem.rs b/risc_v/src/kmem.rs index 7b08630..1f00184 100644 --- a/risc_v/src/kmem.rs +++ b/risc_v/src/kmem.rs @@ -89,18 +89,6 @@ pub fn init() { } } -/// talloc and tfree will allocate a reference to a given type -/// This helps when creating structures. -pub fn talloc() -> Option<&'static mut T> { - unsafe { - (kzmalloc(size_of::()) as *mut T).as_mut() - } -} - -pub fn tfree(p: *mut T) { - kfree(p as *mut u8); -} - /// Allocate sub-page level allocation based on bytes and zero the memory pub fn kzmalloc(sz: usize) -> *mut u8 { let size = align_val(sz, 3);