Skip to content

Commit

Permalink
Merge pull request #1525 from hermit-os/common-os-clippy
Browse files Browse the repository at this point in the history
fix(common-os): fix and run clippy
  • Loading branch information
mkroening authored Dec 20, 2024
2 parents fc15262 + f4d810c commit 9a70716
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 14 deletions.
16 changes: 14 additions & 2 deletions src/arch/aarch64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
//! Architecture dependent interface to initialize a task
#[cfg(not(feature = "common-os"))]
use alloc::alloc::{alloc_zeroed, Layout};
#[cfg(not(feature = "common-os"))]
use alloc::boxed::Box;
use core::arch::naked_asm;
#[cfg(not(feature = "common-os"))]
use core::slice;
use core::sync::atomic::Ordering;
use core::{mem, ptr, slice};
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::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::{env, DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE};
use crate::{DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE};

#[derive(Debug)]
#[repr(C, packed)]
Expand Down Expand Up @@ -243,26 +249,30 @@ impl Drop for TaskStacks {
* of the TLS implementations.
*/

#[cfg(not(feature = "common-os"))]
#[derive(Copy, Clone)]
#[repr(C)]
struct DtvPointer {
val: *const (),
to_free: *const (),
}

#[cfg(not(feature = "common-os"))]
#[repr(C)]
union Dtv {
counter: usize,
pointer: DtvPointer,
}

#[cfg(not(feature = "common-os"))]
#[repr(C)]
pub struct TaskTLS {
dtv: mem::MaybeUninit<Box<[Dtv; 2]>>,
_private: usize,
block: [u8],
}

#[cfg(not(feature = "common-os"))]
impl TaskTLS {
fn from_environment() -> Option<Box<Self>> {
let tls_info = env::boot_info().load_info.tls_info?;
Expand Down Expand Up @@ -346,6 +356,7 @@ extern "C" fn task_start(_f: extern "C" fn(usize), _arg: usize) -> ! {
impl TaskFrame for Task {
fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize) {
// Check if TLS is allocated already and if the task uses thread-local storage.
#[cfg(not(feature = "common-os"))]
if self.tls.is_none() {
self.tls = TaskTLS::from_environment();
}
Expand All @@ -360,6 +371,7 @@ impl TaskFrame for Task {
stack -= mem::size_of::<State>();

let state = stack.as_mut_ptr::<State>();
#[cfg(not(feature = "common-os"))]
if let Some(tls) = &self.tls {
(*state).tpidr_el0 = tls.thread_ptr() as u64;
}
Expand Down
1 change: 1 addition & 0 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cfg_if::cfg_if! {
pub(crate) use self::aarch64::kernel::processor;
pub(crate) use self::aarch64::kernel::processor::set_oneshot_timer;
pub(crate) use self::aarch64::kernel::scheduler;
#[cfg(not(feature = "common-os"))]
pub(crate) use self::aarch64::kernel::switch;
#[cfg(feature = "smp")]
pub(crate) use self::aarch64::kernel::application_processor_init;
Expand Down
12 changes: 11 additions & 1 deletion src/arch/riscv64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[cfg(not(feature = "common-os"))]
use alloc::alloc::{alloc, dealloc, Layout};
#[cfg(not(feature = "common-os"))]
use alloc::boxed::Box;
#[cfg(not(feature = "common-os"))]
use core::convert::TryInto;
use core::{mem, ptr};

Expand All @@ -9,8 +12,10 @@ use memory_addresses::{PhysAddr, VirtAddr};
use crate::arch::riscv64::kernel::core_local::core_scheduler;
use crate::arch::riscv64::kernel::processor::set_oneshot_timer;
use crate::arch::riscv64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
#[cfg(not(feature = "common-os"))]
use crate::env;
use crate::scheduler::task::{Task, TaskFrame};
use crate::{env, DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE};
use crate::{DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE};

#[repr(C, packed)]
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -257,12 +262,14 @@ impl Drop for TaskStacks {
}
}

#[cfg(not(feature = "common-os"))]
pub struct TaskTLS {
address: VirtAddr,
tp: VirtAddr,
layout: Layout,
}

#[cfg(not(feature = "common-os"))]
impl TaskTLS {
pub fn from_environment() -> Option<Box<Self>> {
let tls_info = env::boot_info().load_info.tls_info?;
Expand Down Expand Up @@ -320,6 +327,7 @@ impl TaskTLS {
}
}

#[cfg(not(feature = "common-os"))]
impl Drop for TaskTLS {
fn drop(&mut self) {
debug!(
Expand Down Expand Up @@ -377,6 +385,7 @@ impl TaskFrame for Task {
fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize) {
// Check if the task (process or thread) uses Thread-Local-Storage.
// check is TLS is already allocated
#[cfg(not(feature = "common-os"))]
if self.tls.is_none() {
self.tls = TaskTLS::from_environment();
}
Expand All @@ -391,6 +400,7 @@ impl TaskFrame for Task {
stack -= mem::size_of::<State>();

let state = stack.as_mut_ptr::<State>();
#[cfg(not(feature = "common-os"))]
if let Some(tls) = &self.tls {
(*state).tp = tls.tp().as_usize();
}
Expand Down
14 changes: 6 additions & 8 deletions src/arch/x86_64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ unsafe extern "C" fn pre_init(boot_info: Option<&'static RawBootInfo>, cpu_id: u
}

#[cfg(feature = "common-os")]
const LOADER_START: usize = 0x10000000000;
const LOADER_START: usize = 0x0100_0000_0000;
#[cfg(feature = "common-os")]
const LOADER_STACK_SIZE: usize = 0x8000;

#[cfg(feature = "common-os")]
pub fn load_application<F>(code_size: u64, tls_size: u64, func: F) -> Result<(), ()>
pub fn load_application<F, T>(code_size: u64, tls_size: u64, func: F) -> T
where
F: FnOnce(&'static mut [u8], Option<&'static mut [u8]>) -> Result<(), ()>,
F: FnOnce(&'static mut [u8], Option<&'static mut [u8]>) -> T,
{
use core::ptr::slice_from_raw_parts_mut;

Expand All @@ -300,8 +300,7 @@ where
use crate::arch::x86_64::mm::physicalmem;

let code_size = (code_size as usize + LOADER_STACK_SIZE).align_up(BasePageSize::SIZE as usize);
let physaddr =
physicalmem::allocate_aligned(code_size as usize, BasePageSize::SIZE as usize).unwrap();
let physaddr = physicalmem::allocate_aligned(code_size, BasePageSize::SIZE as usize).unwrap();

let mut flags = PageTableEntryFlags::empty();
flags.normal().writable().user().execute_enable();
Expand Down Expand Up @@ -335,9 +334,8 @@ where
tls_memsz / BasePageSize::SIZE as usize,
flags,
);
let block = unsafe {
&mut *slice_from_raw_parts_mut(tls_virt.as_mut_ptr() as *mut u8, tls_offset + tcb_size)
};
let block =
unsafe { &mut *slice_from_raw_parts_mut(tls_virt.as_mut_ptr(), tls_offset + tcb_size) };
for elem in block.iter_mut() {
*elem = 0;
}
Expand Down
4 changes: 3 additions & 1 deletion src/arch/x86_64/kernel/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,9 @@ pub fn configure() {
wrmsr(IA32_STAR, (0x1bu64 << 48) | (0x08u64 << 32));
wrmsr(
IA32_LSTAR,
crate::arch::x86_64::kernel::syscall::syscall_handler as u64,
(crate::arch::x86_64::kernel::syscall::syscall_handler as usize)
.try_into()
.unwrap(),
);
wrmsr(IA32_FMASK, 1 << 9); // clear IF flag during system call
}
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn create_new_root_page_table() -> usize {
paging::map::<BasePageSize>(slice_addr, physaddr, 1, flags);

unsafe {
let pml4 = core::slice::from_raw_parts_mut(slice_addr.as_mut_ptr() as *mut u64, 512);
let pml4 = core::slice::from_raw_parts_mut(slice_addr.as_mut_ptr(), 512);

// clear PML4
for elem in pml4.iter_mut() {
Expand Down
1 change: 1 addition & 0 deletions src/arch/x86_64/mm/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub trait PageTableEntryFlagsExt {
#[cfg(feature = "common-os")]
fn user(&mut self) -> &mut Self;

#[expect(dead_code)]
#[cfg(feature = "common-os")]
fn kernel(&mut self) -> &mut Self;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub(crate) fn init() {
// we reserve at least 75% of the memory for the user space
let reserve: usize = (avail_mem * 75) / 100;
// 64 MB is enough as kernel heap
let reserve = core::cmp::min(reserve, 0x4000000);
let reserve = core::cmp::min(reserve, 0x0400_0000);

let virt_size: usize = reserve.align_down(LargePageSize::SIZE as usize);
let virt_addr =
Expand Down
1 change: 1 addition & 0 deletions xtask/src/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl Clippy {

let triple = arch.triple();
cmd!(sh, "cargo clippy --target={triple}").run()?;
cmd!(sh, "cargo clippy --target={triple} --features common-os").run()?;
cmd!(sh, "cargo clippy --target={triple}")
.arg("--features=acpi,dns,fsgsbase,pci,smp,vga")
.run()?;
Expand Down

0 comments on commit 9a70716

Please sign in to comment.