From 351236159fe3df806ee8195affebab5348755bc0 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 25 Apr 2022 22:40:13 +0200 Subject: [PATCH 1/5] chore: fmt --- ableos/src/scratchpad.rs | 91 ++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 50 deletions(-) diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index 6c36f288..93b55149 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -1,14 +1,49 @@ -use core::alloc::Layout; - use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2}; -use crate::devices::{pci, pci_inner}; -use crate::encoding::bin; -use crate::port_io::{read32, read8, write8}; +use crate::filesystem::FILE_SYSTEM; use crate::rhai_shell::shell; +use crate::rhai_shell::KEYBUFF; use crate::wasm_jumploader::run_program; use acpi::{AcpiTables, PlatformInfo}; +use cpuio::inb; use cpuio::outb; use genfs::Fs; +use genfs::OpenOptions; + +// TODO: move to a better place +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct AcpiStruct {} +impl acpi::AcpiHandler for AcpiStruct { + unsafe fn map_physical_region( + &self, + physical_address: usize, + size: usize, + ) -> acpi::PhysicalMapping { + info!("PHYS ADDR: {:?}", physical_address); + info!("Size: {:?}", size); + todo!("map_physical_region"); + } + fn unmap_physical_region(_region: &acpi::PhysicalMapping) { + todo!("unmap_physical_region"); + } +} + +pub struct Path { + pub path: Vec, +} + +impl Path { + pub fn new(path: String) -> Self { + let mut path_vec_string = vec![]; + + for part in path.split(&['\\', '/'][..]) { + path_vec_string.push(part.to_string()); + } + + Path { + path: path_vec_string, + } + } +} /// Experimental scratchpad for testing. pub fn scratchpad() { @@ -50,7 +85,6 @@ pub fn scratchpad() { real_shell(); } -use crate::port_io::write16; pub fn acpi() { let acpi_handler = AcpiStruct {}; @@ -62,33 +96,13 @@ pub fn acpi() { PlatformInfo { power_profile, interrupt_model, - processor_info, - pm_timer, + .. } => { info!("{:?}", power_profile); info!("{:?}", interrupt_model); // info!("{:?}", processor_info.unwrap()); // info!("{:?}", pm_timer.unwrap()); } - - _ => todo!(), - } -} -// TODO: move to a better place -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct AcpiStruct {} -impl acpi::AcpiHandler for AcpiStruct { - unsafe fn map_physical_region( - &self, - physical_address: usize, - size: usize, - ) -> acpi::PhysicalMapping { - info!("PHYS ADDR: {:?}", physical_address); - info!("Size: {:?}", size); - todo!("map_physical_region"); - } - fn unmap_physical_region(_region: &acpi::PhysicalMapping) { - todo!("unmap_physical_region"); } } @@ -130,7 +144,6 @@ pub fn real_shell() { } } } -use crate::rhai_shell::KEYBUFF; pub fn command_parser(user: String, command: String) { let fs = &*FILE_SYSTEM.lock(); @@ -201,9 +214,6 @@ pub fn command_parser(user: String, command: String) { } } -use crate::filesystem::FILE_SYSTEM; -use genfs::OpenOptions; - pub fn sound(n_frequency: u32) { let div: u32; let tmp: u8; @@ -229,22 +239,3 @@ pub fn sound_off() { }; reset_pit_for_cpu(); } - -use cpuio::inb; - -pub struct Path { - pub path: Vec, -} -impl Path { - pub fn new(path: String) -> Self { - let mut path_vec_string = vec![]; - - for part in path.split(&['\\', '/'][..]) { - path_vec_string.push(part.to_string()); - } - - Path { - path: path_vec_string, - } - } -} From ae70548cc0ba771eb4874145585d254305fe3f72 Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 7 May 2022 14:08:34 +0200 Subject: [PATCH 2/5] Moved allocator to the kernel --- Cargo.lock | 1 + ableos/src/arch/x86_64/drivers/allocator.rs | 4 ++-- ableos/src/experiments/kinfo.rs | 3 ++- ableos/src/lib.rs | 2 -- ableos/src/rhai_shell/mod.rs | 2 +- kernel/Cargo.toml | 3 ++- kernel/src/allocator.rs | 24 +++++++++++++++++++++ kernel/src/lib.rs | 10 ++++++--- 8 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 kernel/src/allocator.rs diff --git a/Cargo.lock b/Cargo.lock index 850b6b4b..32c5f2aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -384,6 +384,7 @@ dependencies = [ name = "kernel" version = "0.1.2" dependencies = [ + "linked_list_allocator", "log", "versioning", ] diff --git a/ableos/src/arch/x86_64/drivers/allocator.rs b/ableos/src/arch/x86_64/drivers/allocator.rs index b1057be9..14da4063 100644 --- a/ableos/src/arch/x86_64/drivers/allocator.rs +++ b/ableos/src/arch/x86_64/drivers/allocator.rs @@ -1,4 +1,4 @@ -use crate::allocator::{HEAP_SIZE, HEAP_START}; +use kernel::allocator::{HEAP_SIZE, HEAP_START}; use alloc::alloc::{GlobalAlloc, Layout}; use core::ptr::null_mut; use x86_64::{ @@ -39,7 +39,7 @@ pub fn init_heap( } unsafe { - crate::allocator::ALLOCATOR + kernel::allocator::ALLOCATOR .lock() .init(HEAP_START, HEAP_SIZE); } diff --git a/ableos/src/experiments/kinfo.rs b/ableos/src/experiments/kinfo.rs index 46646db5..fdacbb26 100644 --- a/ableos/src/experiments/kinfo.rs +++ b/ableos/src/experiments/kinfo.rs @@ -1,6 +1,7 @@ use super::systeminfo::SystemMemory; -use crate::{arch::drivers::sysinfo::master, ALLOCATOR}; +use crate::arch::drivers::sysinfo::master; use core::fmt::Display; +use kernel::allocator::ALLOCATOR; use versioning::Version; use x86_64::instructions::interrupts::{disable, enable}; diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 911b3e33..98892da3 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -40,7 +40,6 @@ pub mod print; #[macro_use] pub mod serial_print; -pub mod allocator; pub mod boot_conf; pub mod devices; pub mod driver_traits; @@ -68,7 +67,6 @@ mod unicode_utils; #[prelude_import] pub use prelude::rust_2021::*; -pub use allocator::*; pub use driver_traits::*; pub use experiments::*; pub use graphics::*; diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs index aae557bc..7886826a 100644 --- a/ableos/src/rhai_shell/mod.rs +++ b/ableos/src/rhai_shell/mod.rs @@ -2,13 +2,13 @@ use crate::arch::drivers::sysinfo::master; use crate::filesystem::FILE_SYSTEM; use crate::time::fetch_time; use crate::wasm_jumploader::interp; -use crate::ALLOCATOR; use crate::{ arch::{shutdown, sloop}, systeminfo::{KERNEL_VERSION, RELEASE_TYPE}, KERNEL_STATE, }; use genfs::{Fs, OpenOptions}; +use kernel::allocator::ALLOCATOR; use rhai::Engine; use spin::Lazy; use x86_64::instructions::interrupts::{disable, enable}; diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index b6b5df25..8a86d749 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -4,7 +4,8 @@ name = "kernel" version = "0.1.2" [dependencies] +linked_list_allocator = "0.9" log = "0.4.14" [dependencies.versioning] -git = "https://git.ablecorp.us/able/aos_userland" \ No newline at end of file +git = "https://git.ablecorp.us/able/aos_userland" diff --git a/kernel/src/allocator.rs b/kernel/src/allocator.rs new file mode 100644 index 00000000..3c177d48 --- /dev/null +++ b/kernel/src/allocator.rs @@ -0,0 +1,24 @@ +//! Memory allocator + +use linked_list_allocator::LockedHeap; + +/// +pub const HEAP_START: usize = 0x_4444_4444_0000; + +/// +pub const HEAP_MULTIPLIER: usize = 100000; + +/// +pub const HEAP_BASE: usize = 100; + +/// +pub const HEAP_SIZE: usize = HEAP_BASE * HEAP_MULTIPLIER; + +/// Global allocator +#[global_allocator] +pub static ALLOCATOR: LockedHeap = LockedHeap::empty(); + +#[alloc_error_handler] +fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { + panic!("allocation error: {:?}", layout) +} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 42902107..53f1f734 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -1,10 +1,14 @@ //! The ableOS kernel. -#![deny(missing_docs)] -#![no_std] -#![feature(prelude_import)] +#![feature(alloc_error_handler)] #![feature(arbitrary_enum_discriminant)] +#![feature(prelude_import)] +#![no_std] +#![deny(missing_docs)] +extern crate alloc; + +pub mod allocator; pub mod device_interface; pub mod messaging; pub mod panic; From 86f466cdb02ea1a81c0d6bca96dcb1fc054e818d Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 8 May 2022 13:32:19 +0200 Subject: [PATCH 3/5] Simplified signature checking in wasm jumploader --- ableos/src/wasm_jumploader/host_functions.rs | 118 +++---------------- 1 file changed, 17 insertions(+), 101 deletions(-) diff --git a/ableos/src/wasm_jumploader/host_functions.rs b/ableos/src/wasm_jumploader/host_functions.rs index 04c50550..a4f4d44e 100644 --- a/ableos/src/wasm_jumploader/host_functions.rs +++ b/ableos/src/wasm_jumploader/host_functions.rs @@ -14,108 +14,24 @@ const SEND_SIGNAL_INDEX: usize = 1; pub struct HostExternals; impl HostExternals { fn check_signature(&self, index: usize, signature: &Signature) -> bool { - match index { - ADD_FUNC_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } + let (params, ret_ty): (&[_], _) = match index { + ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), + SEND_SIGNAL_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), + GET_TIME_INDEX => (&[], Some(ValueType::I32)), + GET_RANDOM_INDEX => (&[], Some(ValueType::I32)), + GET_INPUT_INDEX => (&[], Some(ValueType::I32)), + PRINT_CHAR => (&[ValueType::I32], None), + _ => return false, + }; - SEND_SIGNAL_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - GET_TIME_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - GET_RANDOM_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - GET_INPUT_INDEX => { - let (params, ret_ty): (&[ValueType], Option) = - (&[], Some(ValueType::I32)); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - - PRINT_CHAR => { - let (params, ret_ty): (&[ValueType], Option) = (&[ValueType::I32], None); - if params.len() != signature.params().len() { - return false; - } - if ret_ty != signature.return_type() { - return false; - } - for (ty, param) in params.iter().zip(signature.params()) { - if *ty != *param { - return false; - } - } - true - } - _ => false, + if params.len() != signature.params().len() || ret_ty != signature.return_type() { + false + } else { + params + .iter() + .zip(signature.params()) + .find(|(ty, param)| ty != param) + .is_none() } } } From 0775577d31e7e24c265467d727bf1b39cb60096f Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 10 May 2022 18:41:45 +0200 Subject: [PATCH 4/5] Added convenience macro for defining host externals --- ableos/src/wasm_jumploader/host_functions.rs | 268 +++++++++++-------- 1 file changed, 149 insertions(+), 119 deletions(-) diff --git a/ableos/src/wasm_jumploader/host_functions.rs b/ableos/src/wasm_jumploader/host_functions.rs index a4f4d44e..2e6157b7 100644 --- a/ableos/src/wasm_jumploader/host_functions.rs +++ b/ableos/src/wasm_jumploader/host_functions.rs @@ -4,135 +4,165 @@ use wasmi::{ Signature, Trap, ValueType, }; -const ADD_FUNC_INDEX: usize = 0; -const GET_TIME_INDEX: usize = 2; -const GET_RANDOM_INDEX: usize = 3; -const GET_INPUT_INDEX: usize = 4; -const PRINT_CHAR: usize = 6; -const SEND_SIGNAL_INDEX: usize = 1; - -pub struct HostExternals; -impl HostExternals { - fn check_signature(&self, index: usize, signature: &Signature) -> bool { - let (params, ret_ty): (&[_], _) = match index { - ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), - SEND_SIGNAL_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), - GET_TIME_INDEX => (&[], Some(ValueType::I32)), - GET_RANDOM_INDEX => (&[], Some(ValueType::I32)), - GET_INPUT_INDEX => (&[], Some(ValueType::I32)), - PRINT_CHAR => (&[ValueType::I32], None), - _ => return false, - }; - - if params.len() != signature.params().len() || ret_ty != signature.return_type() { - false - } else { - params - .iter() - .zip(signature.params()) - .find(|(ty, param)| ty != param) - .is_none() - } - } +macro_rules! wasmi_type { + (bool) => { + ValueType::I32 + }; + (u8) => { + ValueType::I32 + }; + (u16) => { + ValueType::I32 + }; + (u32) => { + ValueType::I32 + }; + (u64) => { + ValueType::I64 + }; + (i8) => { + ValueType::I32 + }; + (i16) => { + ValueType::I32 + }; + (i32) => { + ValueType::I32 + }; + (i64) => { + ValueType::I64 + }; + (f32) => { + ValueType::F32 + }; + (f64) => { + ValueType::F64 + }; } -impl Externals for HostExternals { - fn invoke_index( - &mut self, - index: usize, - args: RuntimeArgs, - ) -> Result, Trap> { - match index { - ADD_FUNC_INDEX => { - let a: u32 = args.nth_checked(0)?; - let b: u32 = args.nth_checked(1)?; - let result = a + b; - trace!("SYSCALL: {} + {} = {}", a, b, result); - Ok(Some(RuntimeValue::I32(result as i32))) - } +macro_rules! wasmi_return_type { + () => { + None + }; + ($type: ident) => { + Some(wasmi_type!($type)) + }; +} - SEND_SIGNAL_INDEX => { - let pid: u32 = args.nth_checked(0)?; - let signal: u32 = args.nth_checked(1)?; - - trace!("SYSCALL: send signal {} to pid {}", signal, pid); - let ret = RuntimeValue::I32(0); - Ok(Some(ret)) - } - - GET_TIME_INDEX => { - use core::sync::atomic::Ordering::*; - trace!("SYSCALL: get time"); - - x86_64::instructions::interrupts::disable(); - let tick_time = kernel::TICK.load(Relaxed); - x86_64::instructions::interrupts::enable(); - - let ret = RuntimeValue::I32(tick_time.try_into().unwrap()); - Ok(Some(ret)) - } - - GET_RANDOM_INDEX => { - trace!("SYSCALL: get random"); - let rand = generate_process_pass(); - - let ret = RuntimeValue::I32(rand as i32); - - // let ret = RuntimeValue::I32(rand.try_into().unwrap()); - Ok(Some(ret)) - } - - GET_INPUT_INDEX => { - let input = None; - x86_64::instructions::interrupts::without_interrupts(|| KEYBUFF.lock().pop()); - if let Some(chr) = input { - trace!("SYSCALL: input: {}", chr); +macro_rules! host_externals { + ( + $($index: literal: $name: ident ($($arg_name: ident: $arg_type: ident),* $(,)?) $(-> $return_type: ident)? $block: block)* + ) => { + pub struct HostExternals; + impl Externals for HostExternals { + fn invoke_index( + &mut self, + index: usize, + args: RuntimeArgs + ) -> Result, Trap> { + match index { + $( + $index => match args.as_ref() { + [$($arg_name),*] => { + $( + let $arg_name: $arg_type = (*$arg_name) + .try_into() + .ok_or(wasmi::TrapKind::UnexpectedSignature)?; + )* + $block + }, + _ => return Err(wasmi::TrapKind::UnexpectedSignature.into()), + } + ),* + _ => { + error!("Unimplemented function at {index}"); + Err(Trap::new(wasmi::TrapKind::Unreachable)) + } } - - let ret = RuntimeValue::I32(input.unwrap_or(0x00 as char) as i32); - - Ok(Some(ret)) - } - - PRINT_CHAR => { - let chr: u8 = args.nth_checked(0)?; - trace!("SYSCALL: print: {}", chr); - print!("{}", char::from(chr)); - Ok(None) - } - _ => { - error!("Unimplemented function at {}", index); - Err(Trap::new(wasmi::TrapKind::Unreachable)) } } - } + + impl ModuleImportResolver for HostExternals { + fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result { + let (index, param_types, return_type): (usize, &[ValueType], Option) = + match field_name { + $( + stringify!($name) => ( + $index, + &[$(wasmi_type!($arg_type)),*], + wasmi_return_type!($($return_type)?), + ), + )* + _ => { + return Err(Error::Instantiation(format!( + "Export {field_name} not found", + ))); + }, + }; + + if !(param_types.len() != signature.params().len() || return_type != signature.return_type()) + && param_types + .iter() + .zip(signature.params()) + .find(|(ty, param)| ty != param) + .is_none() + { + trace!("Resolved export {field_name} at func {index}"); + Ok(FuncInstance::alloc_host(signature.clone(), index)) + } else { + Err(Error::Instantiation(format!( + "Export {field_name} has a bad signature {signature:?}", + ))) + } + } + } + }; } -impl ModuleImportResolver for HostExternals { - fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result { - let index = match field_name { - "add" => ADD_FUNC_INDEX, - "send_signal" => SEND_SIGNAL_INDEX, - "get_time" => GET_TIME_INDEX, - "get_random" => GET_RANDOM_INDEX, - "get_input" => GET_INPUT_INDEX, - "print_char" => PRINT_CHAR, - _ => { - return Err(Error::Instantiation(format!( - "Export {} not found", - field_name - ))) - } - }; +host_externals! { + 0: add(a: u32, b: u32) -> u32 { + let result = a + b; + trace!("SYSCALL: {} + {} = {}", a, b, result); + Ok(Some(result.into())) + } - if !self.check_signature(index, signature) { - return Err(Error::Instantiation(format!( - "Export {} has a bad signature {:?}", - field_name, signature - ))); + 1: send_signal(pid: u32, signal: u32) -> i32 { + trace!("SYSCALL: send signal {} to pid {}", signal, pid); + Ok(Some(0.into())) + } + + 2: get_time() -> u32 { + use core::sync::atomic::Ordering::*; + trace!("SYSCALL: get time"); + + x86_64::instructions::interrupts::disable(); + let tick_time = kernel::TICK.load(Relaxed); + x86_64::instructions::interrupts::enable(); + + let ret: u32 = tick_time.try_into().unwrap(); + Ok(Some(ret.into())) + } + + 3: get_random() -> i32 { + trace!("SYSCALL: get random"); + let rand = generate_process_pass() as i32; + Ok(Some(rand.into())) + } + + 4: get_input() -> i32 { + let input = None; + x86_64::instructions::interrupts::without_interrupts(|| KEYBUFF.lock().pop()); + if let Some(chr) = input { + trace!("SYSCALL: input: {}", chr); } - trace!("Resolved export {} as func {}", field_name, index); - Ok(FuncInstance::alloc_host(signature.clone(), index)) + let ret = input.unwrap_or(0x00 as char) as i32; + Ok(Some(ret.into())) + } + + 5: print_char(chr: u8) { + trace!("SYSCALL: print: {}", chr); + print!("{}", char::from(chr)); + Ok(None) } } From fce7b2a4bbea468062eb6497ae09c31e6b4bf8fd Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 10 May 2022 18:41:53 +0200 Subject: [PATCH 5/5] fmt --- ableos/src/arch/x86_64/drivers/allocator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ableos/src/arch/x86_64/drivers/allocator.rs b/ableos/src/arch/x86_64/drivers/allocator.rs index 14da4063..8db70e52 100644 --- a/ableos/src/arch/x86_64/drivers/allocator.rs +++ b/ableos/src/arch/x86_64/drivers/allocator.rs @@ -1,6 +1,6 @@ -use kernel::allocator::{HEAP_SIZE, HEAP_START}; use alloc::alloc::{GlobalAlloc, Layout}; use core::ptr::null_mut; +use kernel::allocator::{HEAP_SIZE, HEAP_START}; use x86_64::{ structures::paging::{ mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,