Skip to content

Commit

Permalink
Merge branch 'main' into iovec
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes authored May 22, 2024
2 parents 015aedf + 791221b commit b0e6ce3
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 18 deletions.
21 changes: 18 additions & 3 deletions hermit-macro/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,27 @@ fn parse_sig(sig: &Signature) -> Result<ParsedSig> {
}

fn validate_attrs(attrs: &[Attribute]) -> Result<()> {
let mut no_mangle_found = false;
for attr in attrs {
if !attr.path().is_ident("cfg") && !attr.path().is_ident("doc") {
if !attr.path().is_ident("cfg")
&& !attr.path().is_ident("doc")
&& !attr.path().is_ident("no_mangle")
{
bail!(
attr,
"#[system] functions may only have `#[doc]` and `#[cfg]` attributes"
"#[system] functions may only have `#[doc]`, `#[no_mangle]` and `#[cfg]` attributes"
);
}
if attr.path().is_ident("no_mangle") {
no_mangle_found = true;
}
}

if !no_mangle_found {
bail!(
attrs.first(),
"#[system] functions must have `#[no_mangle]` attribute"
);
}

Ok(())
Expand Down Expand Up @@ -96,7 +110,6 @@ fn emit_func(mut func: ItemFn, sig: &ParsedSig) -> Result<ItemFn> {

let func = parse_quote! {
#(#attrs)*
#[no_mangle]
#vis #sig {
#func

Expand Down Expand Up @@ -128,6 +141,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
pub extern "C" fn sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
Expand Down Expand Up @@ -164,6 +178,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
pub unsafe extern "C" fn sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
Expand Down
33 changes: 18 additions & 15 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,10 @@ impl NotifCfg {

// Assumes the cap_len is a multiple of 8
// This read MIGHT be slow, as it does NOT ensure 32 bit alignment.
let notify_off_multiplier = cap.device.read_register(
u16::try_from(cap.origin.cfg_ptr).unwrap() + u16::from(cap.origin.cap_struct.cap_len),
);
let notify_off_multiplier_ptr =
cap.origin.cfg_ptr + u32::try_from(mem::size_of::<PciCapRaw>()).unwrap();
let notify_off_multiplier_ptr = u16::try_from(notify_off_multiplier_ptr).unwrap();
let notify_off_multiplier = cap.device.read_register(notify_off_multiplier_ptr);

// define base memory address from which the actual Queue Notify address can be derived via
// base_addr + queue_notify_off * notify_off_multiplier.
Expand Down Expand Up @@ -921,8 +922,8 @@ impl PciCfgAlt {
// #[repr(C)]
// struct PciCap64 {
// pci_cap: PciCap,
// offset_high: u32,
// length_high: u32
// offset_hi: u32,
// length_hi: u32
pub struct ShMemCfg {
mem_addr: VirtMemAddr,
length: MemLen,
Expand All @@ -947,24 +948,25 @@ impl ShMemCfg {

// Assumes the cap_len is a multiple of 8
// This read MIGHT be slow, as it does NOT ensure 32 bit alignment.
let offset_high = cap.device.read_register(
u16::try_from(cap.origin.cfg_ptr).unwrap() + u16::from(cap.origin.cap_struct.cap_len),
);
let offset_hi_ptr =
cap.origin.cfg_ptr + u32::try_from(mem::size_of::<PciCapRaw>()).unwrap();
let offset_hi_ptr = u16::try_from(offset_hi_ptr).unwrap();
let offset_hi = cap.device.read_register(offset_hi_ptr);

// Create 64 bit offset from high and low 32 bit values
let offset =
MemOff::from((u64::from(offset_high) << 32) ^ u64::from(cap.origin.cap_struct.offset));
MemOff::from((u64::from(offset_hi) << 32) ^ u64::from(cap.origin.cap_struct.offset));

// Assumes the cap_len is a multiple of 8
// This read MIGHT be slow, as it does NOT ensure 32 bit alignment.
let length_high = cap.device.read_register(
u16::try_from(cap.origin.cfg_ptr).unwrap()
+ u16::from(cap.origin.cap_struct.cap_len + 4),
);
let length_hi_ptr = cap.origin.cfg_ptr
+ u32::try_from(mem::size_of::<PciCapRaw>() + mem::size_of::<u32>()).unwrap();
let length_hi_ptr = u16::try_from(length_hi_ptr).unwrap();
let length_hi = cap.device.read_register(length_hi_ptr);

// Create 64 bit length from high and low 32 bit values
let length =
MemLen::from((u64::from(length_high) << 32) ^ u64::from(cap.origin.cap_struct.length));
MemLen::from((u64::from(length_hi) << 32) ^ u64::from(cap.origin.cap_struct.length));

let virt_addr_raw = cap.bar.mem_addr + offset;
let raw_ptr = ptr::with_exposed_provenance_mut::<u8>(virt_addr_raw.into());
Expand Down Expand Up @@ -1118,6 +1120,7 @@ fn read_caps(

let mut iter = bars.iter();

let cfg_ptr = next_ptr;
// Set next pointer for next iteration of `caplist.
next_ptr = u32::from(cap_raw.cap_next);

Expand Down Expand Up @@ -1151,7 +1154,7 @@ fn read_caps(
length: MemLen::from(cap_raw.length),
device: *device,
origin: Origin {
cfg_ptr: next_ptr,
cfg_ptr,
dev_id: device_id,
cap_struct: cap_raw,
},
Expand Down
5 changes: 5 additions & 0 deletions src/syscalls/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl CondQueue {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_destroy_queue(ptr: usize) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand All @@ -42,6 +43,7 @@ pub unsafe extern "C" fn sys_destroy_queue(ptr: usize) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_notify(ptr: usize, count: i32) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance::<usize>(ptr);
Expand Down Expand Up @@ -79,6 +81,7 @@ pub unsafe extern "C" fn sys_notify(ptr: usize, count: i32) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_init_queue(ptr: usize) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand All @@ -98,6 +101,7 @@ pub unsafe extern "C" fn sys_init_queue(ptr: usize) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand Down Expand Up @@ -126,6 +130,7 @@ pub unsafe extern "C" fn sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_wait(ptr: usize) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand Down
4 changes: 4 additions & 0 deletions src/syscalls/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub unsafe extern "C" fn sys_read_entropy(buf: *mut u8, len: usize, flags: u32)
/// the function returns `-1`.
#[cfg(not(feature = "newlib"))]
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_secure_rand32(value: *mut u32) -> i32 {
let mut buf = value.cast();
let mut len = size_of::<u32>();
Expand All @@ -84,6 +85,7 @@ pub unsafe extern "C" fn sys_secure_rand32(value: *mut u32) -> i32 {
/// the function returns -1.
#[cfg(not(feature = "newlib"))]
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_secure_rand64(value: *mut u64) -> i32 {
let mut buf = value.cast();
let mut len = size_of::<u64>();
Expand All @@ -103,13 +105,15 @@ pub unsafe extern "C" fn sys_secure_rand64(value: *mut u64) -> i32 {
/// The function computes a sequence of pseudo-random integers
/// in the range of 0 to RAND_MAX
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_rand() -> u32 {
generate_park_miller_lehmer_random_number()
}

/// The function sets its argument as the seed for a new sequence
/// of pseudo-random numbers to be returned by rand()
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_srand(seed: u32) {
*(PARK_MILLER_LEHMER_SEED.lock()) = seed;
}
Expand Down
2 changes: 2 additions & 0 deletions src/syscalls/futex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::time::timespec;
/// * `timeout` is negative
/// * `flags` contains unknown flags
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_futex_wait(
address: *mut u32,
expected: u32,
Expand Down Expand Up @@ -42,6 +43,7 @@ pub unsafe extern "C" fn sys_futex_wait(
///
/// Returns -EINVAL if `address` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_futex_wake(address: *mut u32, count: i32) -> i32 {
if address.is_null() {
return -EINVAL;
Expand Down
5 changes: 5 additions & 0 deletions src/syscalls/lwip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@ use crate::arch::core_local::core_scheduler;
use crate::{arch, console};

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_lwip_get_errno() -> i32 {
core_scheduler().get_lwip_errno()
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_lwip_set_errno(errno: i32) {
core_scheduler().set_lwip_errno(errno);
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_acquire_putchar_lock() {
// FIXME: use core-local storage instead
// better yet: remove and replace all of this
MutexGuard::leak(console::CONSOLE.lock());
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_putchar(character: u8) {
arch::output_message_buf(&[character]);
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_release_putchar_lock() {
unsafe {
console::CONSOLE.force_unlock();
Expand Down
Loading

0 comments on commit b0e6ce3

Please sign in to comment.