Skip to content

Commit 2bfe5ad

Browse files
fix(kernel): fix warnings about any intermediate references created
Signed-off-by: Anhad Singh <[email protected]>
1 parent a6f265e commit 2bfe5ad

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

src/aero_kernel/src/arch/x86_64/cpu_local.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@
1818
use core::alloc::Layout;
1919
use core::ops::{Deref, DerefMut};
2020

21+
use crate::extern_sym;
2122
use crate::mem::paging::VirtAddr;
22-
use crate::utils::LinkerSymbol;
2323

2424
use super::io;
2525

26-
extern "C" {
27-
static __cpu_local_start: LinkerSymbol<u8>;
28-
static __cpu_local_end: LinkerSymbol<u8>;
29-
}
30-
3126
#[repr(C)]
3227
pub struct CpuLocal<T>(T);
3328

@@ -49,7 +44,7 @@ impl<T> CpuLocal<T> {
4944
}
5045

5146
let self_addr = VirtAddr::new(self as *const _ as u64);
52-
let section_addr = VirtAddr::new(unsafe { &__cpu_local_start as *const _ as u64 });
47+
let section_addr = VirtAddr::new(extern_sym!(__cpu_local_start) as u64);
5348

5449
let offset = self_addr - section_addr;
5550
VirtAddr::new(val) + offset
@@ -81,9 +76,10 @@ static SELF_PTR: u64 = 0;
8176
static mut CPUID: usize = 0;
8277

8378
pub fn init(cpu_id: usize) {
79+
let start = VirtAddr::new(extern_sym!(__cpu_local_start).addr() as u64);
80+
let end = VirtAddr::new(extern_sym!(__cpu_local_end).addr() as u64);
81+
8482
unsafe {
85-
let start = VirtAddr::new(&__cpu_local_start as *const _ as u64);
86-
let end = VirtAddr::new(&__cpu_local_end as *const _ as u64);
8783
let size = end - start;
8884

8985
let layout = Layout::from_size_align_unchecked(size as _, 64);

src/aero_kernel/src/arch/x86_64/gdt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
2727
use core::alloc::Layout;
2828
use core::mem;
29+
use core::ptr::addr_of;
2930

3031
use alloc::alloc::alloc_zeroed;
3132

@@ -302,7 +303,7 @@ pub fn init_boot() {
302303
unsafe {
303304
let gdt_descriptor = GdtDescriptor::new(
304305
(mem::size_of::<[GdtEntry; BOOT_GDT_ENTRY_COUNT]>() - 1) as u16,
305-
(&BOOT_GDT as *const _) as u64,
306+
addr_of!(BOOT_GDT).addr() as u64,
306307
);
307308

308309
load_gdt(&gdt_descriptor);

src/aero_kernel/src/arch/x86_64/interrupts/idt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const IDT_ENTRIES: usize = 256;
2424
pub(super) static mut IDT: [IdtEntry; IDT_ENTRIES] = [IdtEntry::NULL; IDT_ENTRIES];
2525

2626
use core::mem::size_of;
27+
use core::ptr::addr_of;
2728

2829
use crate::arch::gdt::SegmentSelector;
2930
use crate::utils::sync::Mutex;
@@ -216,7 +217,7 @@ pub fn init() {
216217
unsafe {
217218
let idt_descriptor = IdtDescriptor::new(
218219
((IDT.len() * size_of::<IdtEntry>()) - 1) as u16,
219-
(&IDT as *const _) as u64,
220+
addr_of!(IDT).addr() as u64,
220221
);
221222

222223
load_idt(&idt_descriptor);

src/aero_kernel/src/modules.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@
2929
//! aero_kernel::module_exit!(hello_exit);
3030
//! ```
3131
32-
use crate::{drivers, fs};
32+
use core::mem::size_of;
33+
34+
use crate::{drivers, extern_sym, fs};
3335

3436
/// Inner helper function to make sure the function provided to the [module_init] macro
3537
/// has a valid function signature. This function returns the passed module init function as
3638
/// a const void pointer.
3739
38-
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
40+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
3941
#[repr(C)]
4042
pub enum ModuleType {
4143
Block = 0,
4244
Other = 1,
4345
}
4446

45-
#[derive(Debug)]
47+
#[derive(Debug, Clone)]
4648
#[repr(C)]
4749
pub struct Module {
4850
pub init: *const (),
@@ -69,20 +71,15 @@ macro_rules! module_init {
6971
/// we cannot read the ext2 root filesystem, we link all of the kernel modules into the kernel
7072
/// itself (this is temporary and modules will be loaded from the filesystem in the future).
7173
pub(crate) fn init() {
72-
extern "C" {
73-
static mut __kernel_modules_start: u8;
74-
static mut __kernel_modules_end: u8;
75-
}
74+
let modules_start = extern_sym!(__kernel_modules_start).cast::<Module>();
75+
let modules_end = extern_sym!(__kernel_modules_end).cast::<Module>();
7676

77-
unsafe {
78-
let size = &__kernel_modules_end as *const u8 as usize
79-
- &__kernel_modules_start as *const u8 as usize;
80-
81-
let modules = core::slice::from_raw_parts_mut(
82-
&mut __kernel_modules_start as *mut u8 as *mut Module,
83-
size / core::mem::size_of::<Module>(),
84-
);
77+
let size = (modules_end.addr() - modules_start.addr()) / size_of::<Module>();
78+
let modules = unsafe { core::slice::from_raw_parts(modules_start, size) };
8579

80+
unsafe {
81+
// TODO: refactor this out
82+
let mut modules = modules.to_vec();
8683
modules.sort_by(|e, a| e.ty.cmp(&a.ty));
8784

8885
let mut launched_fs = false;

src/aero_kernel/src/utils/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,15 @@ impl<'a> StackHelper<'a> {
223223
}
224224
}
225225

226-
#[repr(transparent)]
227-
pub struct LinkerSymbol<T: Copy>(core::cell::UnsafeCell<T>);
226+
#[macro_export]
227+
macro_rules! extern_sym {
228+
($sym:ident) => {{
229+
extern "C" {
230+
static $sym: ::core::ffi::c_void;
231+
}
232+
233+
// SAFETY: The value is not accessed, we only take its address. The `addr_of!()` ensures
234+
// that no intermediate references is created.
235+
unsafe { ::core::ptr::addr_of!($sym) }
236+
}};
237+
}

0 commit comments

Comments
 (0)