Skip to content

Commit

Permalink
Merge pull request #1536 from hermit-os/rust-2024
Browse files Browse the repository at this point in the history
feat: upgrade to Rust 2024
  • Loading branch information
mkroening authored Jan 2, 2025
2 parents 8bd7052 + f2ae8b5 commit f7d3d1f
Show file tree
Hide file tree
Showing 91 changed files with 586 additions and 542 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ efc3f0a39209d1cc0fb0907842559cd0ba7f2626
6ffef254f5004176840a4ba5fa8a4ab78c9cb0b4
1980a19b283e405c12f172dda0d61f6e7392cbb3
12261cd05d69974fa470974b84fc46c73e3e4e07
# Upgrade to Rust 2024 style edition
52a7dc889c0c5b44598117b96e153d00b64a2ff3
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ keywords = ["unikernel", "libos"]
categories = ["os"]
repository = "https://github.com/hermit-os/kernel"
documentation = "https://hermit-os.github.io/kernel/hermit/"
edition = "2021"
edition = "2024"
description = "A Rust-based library operating system"
exclude = [
"/.github/*",
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};

use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result, anyhow};
use llvm_tools::LlvmTools;

fn main() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion hermit-builtins/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hermit-builtins"
version = "0.1.0"
edition = "2021"
edition = "2024"

[dependencies]
libm = "0.2"
Expand Down
8 changes: 4 additions & 4 deletions hermit-builtins/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
macro_rules! export {
($(fn $fn:ident($($arg:ident: $argty:ty),+) -> $retty:ty;)+) => {
$(
#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn $fn($($arg: $argty),+) -> $retty {
::libm::$fn($($arg),+)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ export! {
macro_rules! export_out_param {
($(fn $fn:ident($($arg:ident: $argty:ty),+; $out:ident: $outty:ty) -> $retty:ty;)+) => {
$(
#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn $fn($($arg: $argty),+, $out: $outty) -> $retty {
let (ret, out) = ::libm::$fn($($arg),+);
*$out = out;
Expand All @@ -154,12 +154,12 @@ export_out_param! {
fn remquof(x: f32, y: f32; n: &mut i32) -> f32;
}

#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn sincos(x: f64, s: &mut f64, c: &mut f64) {
(*s, *c) = libm::sincos(x);
}

#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn sincosf(x: f32, s: &mut f32, c: &mut f32) {
(*s, *c) = libm::sincosf(x);
}
2 changes: 1 addition & 1 deletion hermit-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hermit-macro"
version = "0.1.0"
edition = "2021"
edition = "2024"

[lib]
proc-macro = true
Expand Down
31 changes: 17 additions & 14 deletions hermit-macro/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{parse_quote, Abi, Attribute, FnArg, Item, ItemFn, Pat, Result, Signature, Visibility};
use syn::{Abi, Attribute, FnArg, Item, ItemFn, Pat, Result, Signature, Visibility, parse_quote};

fn validate_vis(vis: &Visibility) -> Result<()> {
if !matches!(vis, Visibility::Public(_)) {
Expand Down Expand Up @@ -61,24 +61,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")
&& !attr.path().is_ident("no_mangle")
{
if attr.path().is_ident("unsafe") {
if let Ok(ident) = attr.parse_args::<Ident>() {
if ident == "no_mangle" {
no_mangle_found = true;
continue;
}
}
}

if !attr.path().is_ident("cfg") && !attr.path().is_ident("doc") {
bail!(
attr,
"#[system] functions may only have `#[doc]`, `#[no_mangle]` and `#[cfg]` attributes"
"#[system] functions may only have `#[doc]`, `#[unsafe(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"
"#[system] functions must have `#[unsafe(no_mangle)]` attribute"
);
}

Expand Down Expand Up @@ -174,7 +177,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
Expand All @@ -186,7 +189,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn sys_test(a: i8, b: i16) -> i32 {
extern "C" fn __sys_test(a: i8, b: i16) -> i32 {
#[allow(unreachable_code)]
Expand Down Expand Up @@ -222,7 +225,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
Expand All @@ -234,7 +237,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_test(a: i8, b: i16) -> i32 {
unsafe extern "C" fn __sys_test(a: i8, b: i16) -> i32 {
#[allow(unreachable_code)]
Expand Down
2 changes: 1 addition & 1 deletion src/arch/aarch64/kernel/core_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use core::sync::atomic::Ordering;
#[cfg(feature = "smp")]
use hermit_sync::InterruptTicketMutex;

use super::interrupts::{IrqStatistics, IRQ_COUNTERS};
use super::CPU_ONLINE;
use super::interrupts::{IRQ_COUNTERS, IrqStatistics};
use crate::executor::task::AsyncTask;
#[cfg(feature = "smp")]
use crate::scheduler::SchedulerInput;
Expand Down
10 changes: 5 additions & 5 deletions src/arch/aarch64/kernel/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub(crate) fn install_handlers() {
INTERRUPT_HANDLERS.set(handlers).unwrap();
}

#[no_mangle]
#[unsafe(no_mangle)]
pub(crate) extern "C" fn do_fiq(_state: &State) -> *mut usize {
if let Some(irqid) = GicV3::get_and_acknowledge_interrupt() {
let vector: u8 = u32::from(irqid).try_into().unwrap();
Expand Down Expand Up @@ -142,7 +142,7 @@ pub(crate) extern "C" fn do_fiq(_state: &State) -> *mut usize {
core::ptr::null_mut()
}

#[no_mangle]
#[unsafe(no_mangle)]
pub(crate) extern "C" fn do_irq(_state: &State) -> *mut usize {
if let Some(irqid) = GicV3::get_and_acknowledge_interrupt() {
let vector: u8 = u32::from(irqid).try_into().unwrap();
Expand Down Expand Up @@ -170,7 +170,7 @@ pub(crate) extern "C" fn do_irq(_state: &State) -> *mut usize {
core::ptr::null_mut()
}

#[no_mangle]
#[unsafe(no_mangle)]
pub(crate) extern "C" fn do_sync(state: &State) {
let irqid = GicV3::get_and_acknowledge_interrupt().unwrap();
let esr = ESR_EL1.get();
Expand Down Expand Up @@ -206,14 +206,14 @@ pub(crate) extern "C" fn do_sync(state: &State) {
}
}

#[no_mangle]
#[unsafe(no_mangle)]
pub(crate) extern "C" fn do_bad_mode(_state: &State, reason: u32) -> ! {
error!("Receive unhandled exception: {}", reason);

scheduler::abort()
}

#[no_mangle]
#[unsafe(no_mangle)]
pub(crate) extern "C" fn do_error(_state: &State) -> ! {
error!("Receive error interrupt");

Expand Down
37 changes: 16 additions & 21 deletions src/arch/aarch64/kernel/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use bit_field::BitField;
use hermit_dtb::Dtb;
use memory_addresses::arch::aarch64::{PhysAddr, VirtAddr};
use pci_types::{
Bar, CommandRegister, ConfigRegionAccess, InterruptLine, InterruptPin, PciAddress, PciHeader,
MAX_BARS,
Bar, CommandRegister, ConfigRegionAccess, InterruptLine, InterruptPin, MAX_BARS, PciAddress,
PciHeader,
};

use crate::arch::aarch64::kernel::interrupts::GIC;
use crate::arch::aarch64::mm::paging::{self, BasePageSize, PageSize, PageTableEntryFlags};
use crate::arch::aarch64::mm::virtualmem;
use crate::drivers::pci::{PciDevice, PCI_DEVICES};
use crate::drivers::pci::{PCI_DEVICES, PciDevice};
use crate::env;

const PCI_MAX_DEVICE_NUMBER: u8 = 32;
Expand Down Expand Up @@ -196,9 +196,7 @@ fn detect_interrupt(

trace!(
"Interrupt type {:#x}, number {:#x} flags {:#x}",
irq_type,
irq_number,
irq_flags
irq_type, irq_number, irq_flags
);

if high.get_bits(0..24) == addr {
Expand Down Expand Up @@ -250,7 +248,10 @@ pub fn init() {

let pci_address =
virtualmem::allocate_aligned(size.try_into().unwrap(), 0x1000_0000).unwrap();
info!("Mapping PCI Enhanced Configuration Space interface to virtual address {:p} (size {:#X})", pci_address, size);
info!(
"Mapping PCI Enhanced Configuration Space interface to virtual address {:p} (size {:#X})",
pci_address, size
);

let mut flags = PageTableEntryFlags::empty();
flags.device().writable().execute_disable();
Expand Down Expand Up @@ -292,12 +293,9 @@ pub fn init() {
if let Some(bar) = dev.get_bar(i.try_into().unwrap()) {
match bar {
Bar::Io { .. } => {
dev.set_bar(
i.try_into().unwrap(),
Bar::Io {
port: io_start.try_into().unwrap(),
},
);
dev.set_bar(i.try_into().unwrap(), Bar::Io {
port: io_start.try_into().unwrap(),
});
io_start += 0x20;
cmd |= CommandRegister::IO_ENABLE
| CommandRegister::BUS_MASTER_ENABLE;
Expand All @@ -313,14 +311,11 @@ pub fn init() {
size,
prefetchable,
} => {
dev.set_bar(
i.try_into().unwrap(),
Bar::Memory64 {
address: mem64_start,
size,
prefetchable,
},
);
dev.set_bar(i.try_into().unwrap(), Bar::Memory64 {
address: mem64_start,
size,
prefetchable,
});
mem64_start += size;
cmd |= CommandRegister::MEMORY_ENABLE
| CommandRegister::BUS_MASTER_ENABLE;
Expand Down
4 changes: 2 additions & 2 deletions src/arch/aarch64/kernel/processor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::arch::asm;
use core::{fmt, str};

use aarch64::regs::{Readable, CNTFRQ_EL0};
use aarch64::regs::{CNTFRQ_EL0, Readable};
use hermit_dtb::Dtb;
use hermit_sync::{without_interrupts, Lazy};
use hermit_sync::{Lazy, without_interrupts};

use crate::env;

Expand Down
18 changes: 9 additions & 9 deletions src/arch/aarch64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Architecture dependent interface to initialize a task
#[cfg(not(feature = "common-os"))]
use alloc::alloc::{alloc_zeroed, Layout};
use alloc::alloc::{Layout, alloc_zeroed};
#[cfg(not(feature = "common-os"))]
use alloc::boxed::Box;
use core::arch::naked_asm;
Expand All @@ -13,14 +13,14 @@ use core::{mem, ptr};
use align_address::Align;
use memory_addresses::arch::aarch64::{PhysAddr, VirtAddr};

use crate::arch::aarch64::kernel::core_local::core_scheduler;
use crate::arch::aarch64::kernel::CURRENT_STACK_ADDRESS;
use crate::arch::aarch64::kernel::core_local::core_scheduler;
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
#[cfg(not(feature = "common-os"))]
use crate::env;
use crate::scheduler::task::{Task, TaskFrame};
#[cfg(target_os = "none")]
use crate::scheduler::PerCoreSchedulerExt;
use crate::scheduler::task::{Task, TaskFrame};
use crate::{DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE};

#[derive(Debug)]
Expand Down Expand Up @@ -297,15 +297,15 @@ impl TaskTLS {
let raw = ptr::slice_from_raw_parts_mut(data, block_len) as *mut TaskTLS;

let addr = (*raw).block.as_ptr().add(off).cast::<()>();
(*raw).dtv.as_mut_ptr().write(Box::new([
Dtv { counter: 1 },
Dtv {
(*raw)
.dtv
.as_mut_ptr()
.write(Box::new([Dtv { counter: 1 }, Dtv {
pointer: DtvPointer {
val: addr,
to_free: ptr::null(),
},
},
]));
}]));

Box::from_raw(raw)
};
Expand Down Expand Up @@ -401,7 +401,7 @@ impl TaskFrame for Task {
}
}

#[no_mangle]
#[unsafe(no_mangle)]
pub(crate) extern "C" fn get_last_stack_pointer() -> u64 {
core_scheduler().get_last_stack_pointer().as_u64()
}
10 changes: 5 additions & 5 deletions src/arch/aarch64/kernel/start.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use core::arch::{asm, naked_asm};

use hermit_entry::boot_info::RawBootInfo;
use hermit_entry::Entry;
use hermit_entry::boot_info::RawBootInfo;

use crate::arch::aarch64::kernel::scheduler::TaskStacks;
use crate::{env, KERNEL_STACK_SIZE};
use crate::{KERNEL_STACK_SIZE, env};

extern "C" {
unsafe extern "C" {
static vector_table: u8;
}

/// Entrypoint - Initialize Stack pointer and Exception Table
#[no_mangle]
#[unsafe(no_mangle)]
#[naked]
pub unsafe extern "C" fn _start(boot_info: Option<&'static RawBootInfo>, cpu_id: u32) -> ! {
// validate signatures
Expand Down Expand Up @@ -50,7 +50,7 @@ pub unsafe extern "C" fn _start(boot_info: Option<&'static RawBootInfo>, cpu_id:
}

#[inline(never)]
#[no_mangle]
#[unsafe(no_mangle)]
unsafe extern "C" fn pre_init(boot_info: Option<&'static RawBootInfo>, cpu_id: u32) -> ! {
// set exception table
unsafe {
Expand Down
Loading

0 comments on commit f7d3d1f

Please sign in to comment.