Skip to content

Commit

Permalink
Merge branch 'enhancement-ikc-faster' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ppenna committed Sep 5, 2024
2 parents 5cc2255 + c605365 commit 20f6387
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[package]
name = "kernel"
version = "0.3.4"
version = "0.4.0"
license-file = "LICENSE.txt"
edition = "2021"
authors = ["The Maintainers of Nanvix"]
Expand All @@ -29,7 +29,7 @@ bitmap = { git = "https://github.com/nanvix/bitmap", branch = "releases/v0.0.5"
error = { git = "https://github.com/nanvix/error", branch = "releases/v0.0.5" }
raw-array = { git = "https://github.com/nanvix/raw-array", branch = "releases/v0.0.5" }
slab = { git = "https://github.com/nanvix/slab", branch = "releases/v0.0.5" }
sys = { git = "https://github.com/nanvix/sys", branch = "releases/v1.3.1" }
sys = { git = "https://github.com/nanvix/sys", branch = "releases/v1.4.0" }

[features]
default = ["qemu-pc"]
Expand Down
30 changes: 21 additions & 9 deletions src/hal/platform/microvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub struct Platform {
/// Bootloader magic number.
pub const MICROVM_BOOT_MAGIC: u32 = 0x0c00ffee;

/// Port number used for write requests to the standard output.
#[cfg(feature = "stdio")]
const STDOUT_PORT: u16 = 0xe9;

/// Port number used read requests from the standard input.
#[cfg(feature = "stdio")]
const STDIN_PORT: u16 = 0xea;

//==================================================================================================
// Standalone Functions
//==================================================================================================
Expand Down Expand Up @@ -80,11 +88,11 @@ pub unsafe fn putb(b: u8) {
///
/// # Description
///
/// Writes the 32 bit value `val` to the platform's standard output device.
/// Places a write request to the platform's standard output device.
///
/// # Parameters
///
/// - `val`: Value to write.
/// - `addr`: Address where data should be written from.
///
/// # Safety
///
Expand All @@ -94,18 +102,20 @@ pub unsafe fn putb(b: u8) {
/// - It does not prevent concurrent access to the standard output device.
///
#[cfg(feature = "stdio")]
pub unsafe fn out32(val: u32) {
::arch::io::out32(0xe9, val);
pub unsafe fn vmbus_write(addr: *const u8) {
use core::hint;

hint::black_box(::arch::io::out32(STDOUT_PORT, addr as u32));
}

///
/// # Description
///
/// Reads a 32-bit value from the platform's standard input device.
/// Places a read request to the platform's standard input device.
///
/// # Return
/// # Parameters
///
/// The 32-bit value read from the standard input device.
/// - `addr`: Address where data should be read into.
///
/// # Safety
///
Expand All @@ -115,8 +125,10 @@ pub unsafe fn out32(val: u32) {
/// - It does not prevent concurrent access to the standard input device.
///
#[cfg(feature = "stdio")]
pub unsafe fn in32() -> u32 {
::arch::io::in32(0xe9)
pub unsafe fn vmbus_read(addr: *mut u8) {
use core::hint;

hint::black_box(::arch::io::out32(STDIN_PORT, addr as u32))
}

///
Expand Down
58 changes: 21 additions & 37 deletions src/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Imports
//==================================================================================================

use crate::hal::platform;
use ::core::mem;
use ::error::{
Error,
Expand All @@ -15,13 +16,6 @@ use ::sys::ipc::{
MessageType,
};

//==================================================================================================
// Constants
//==================================================================================================

/// End of file flag.
const EOF_FLAG: u32 = 1 << 31;

//==================================================================================================
// Standalone Functions
//==================================================================================================
Expand Down Expand Up @@ -59,11 +53,8 @@ pub fn write(message: Message) -> Result<(), Error> {
// Write message to the kernel's standard output.
// SAFETY: The standard output is present, initialized and thread-safe to write.
unsafe {
for b in bytes {
crate::hal::platform::out32(b as u32);
}
let eof: u32 = EOF_FLAG;
crate::hal::platform::out32(eof);
// NOTE: we assume that page is tagged as writethrough-enabled and cache-disabled.
platform::vmbus_write(&bytes as *const u8);
}

Ok(())
Expand All @@ -80,37 +71,30 @@ pub fn write(message: Message) -> Result<(), Error> {
/// messages. Upon failure, an error is returned instead.
///
pub fn read() -> Result<Option<Message>, Error> {
// Read first byte.
let value: u32 = unsafe { crate::hal::platform::in32() };

// Check for EOF.
if value & EOF_FLAG != 0 {
return Ok(None);
}

// NOTE: trace command after reading the first byte, to avoid flooding the log.
trace!("read()");

const NBYTES: usize = core::mem::size_of::<Message>();
let mut message: [u8; NBYTES] = [0; NBYTES];

message[0] = (value & 0xff) as u8;

// Read message from platform.
for byte in message[1..].iter_mut() {
let value: u32 = unsafe { crate::hal::platform::in32() };

// Check for EOF.
if value & EOF_FLAG != 0 {
break;
}

*byte = (value & 0xff) as u8;
}
// Read message from the kernel's standard input.
// SAFETY: The standard input is present, initialized and thread-safe to read.
unsafe {
// NOTE: we assume that page is tagged as writethrough-enabled and cache-disabled.
platform::vmbus_read(&mut message as *mut u8);
};

// Convert message to Message struct.
match Message::try_from_bytes(message) {
Ok(message) => Ok(Some(message)),
Ok(message) => {
// Check if message is empty.
if message.message_type == MessageType::Empty {
Ok(None)
} else {
// NOTE: trace command after reading the first byte, to avoid flooding the log.
trace!("read()");
Ok(Some(message))
}
},
// No message available.
Err(e) if e.code == ErrorCode::NoMessageAvailable => Ok(None),
Err(e) => {
warn!("read(): {:?} ", e);
Err(e)
Expand Down

0 comments on commit 20f6387

Please sign in to comment.