Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use from_{ref,mut} to eliminate casts to pointers #272

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod paging;
pub mod serial;

use core::arch::asm;
use core::ptr;

use align_address::Align;
use goblin::elf::header::header64::{Header, EI_DATA, ELFDATA2LSB, ELFMAG, SELFMAG};
Expand All @@ -11,6 +12,7 @@ use hermit_entry::boot_info::{BootInfo, HardwareInfo, PlatformInfo, RawBootInfo,
use hermit_entry::elf::LoadedKernel;
use hermit_entry::Entry;
use log::info;
use sptr::Strict;

use crate::arch::paging::*;
use crate::arch::serial::SerialPort;
Expand Down Expand Up @@ -79,7 +81,7 @@ pub fn output_message_byte(byte: u8) {
}

pub unsafe fn get_memory(_memory_size: u64) -> u64 {
(unsafe { &kernel_end } as *const u8 as u64).align_up(LargePageSize::SIZE as u64)
(ptr::from_ref(unsafe { &kernel_end }).addr() as u64).align_up(LargePageSize::SIZE as u64)
}

pub fn find_kernel() -> &'static [u8] {
Expand Down Expand Up @@ -153,45 +155,46 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
let uart_address: u32 = unsafe { COM1.get_port() };
info!("Detect UART at {:#x}", uart_address);

let pgt_slice = unsafe { core::slice::from_raw_parts_mut(&mut l0_pgtable as *mut u64, 512) };
let pgt_slice = unsafe { core::slice::from_raw_parts_mut(ptr::from_mut(&mut l0_pgtable), 512) };
for i in pgt_slice.iter_mut() {
*i = 0;
}
pgt_slice[0] = unsafe { &l1_pgtable as *const u64 as u64 + PT_PT };
pgt_slice[511] = unsafe { &l0_pgtable as *const u64 as u64 + PT_PT + PT_SELF };
pgt_slice[0] = ptr::from_ref(unsafe { &l1_pgtable }).addr() as u64 + PT_PT;
pgt_slice[511] = ptr::from_ref(unsafe { &l0_pgtable }).addr() as u64 + PT_PT + PT_SELF;

let pgt_slice = unsafe { core::slice::from_raw_parts_mut(&mut l1_pgtable as *mut u64, 512) };
let pgt_slice = unsafe { core::slice::from_raw_parts_mut(ptr::from_mut(&mut l1_pgtable), 512) };
for i in pgt_slice.iter_mut() {
*i = 0;
}
pgt_slice[0] = unsafe { &l2_pgtable as *const _ as u64 + PT_PT };
pgt_slice[1] = unsafe { &l2k_pgtable as *const _ as u64 + PT_PT };
pgt_slice[0] = ptr::from_ref(unsafe { &l2_pgtable }).addr() as u64 + PT_PT;
pgt_slice[1] = ptr::from_ref(unsafe { &l2k_pgtable }).addr() as u64 + PT_PT;

let pgt_slice = unsafe { core::slice::from_raw_parts_mut(&mut l2_pgtable as *mut u64, 512) };
let pgt_slice = unsafe { core::slice::from_raw_parts_mut(ptr::from_mut(&mut l2_pgtable), 512) };
for i in pgt_slice.iter_mut() {
*i = 0;
}
pgt_slice[0] = unsafe { &l3_pgtable as *const u64 as u64 + PT_PT };
pgt_slice[0] = ptr::from_ref(unsafe { &l3_pgtable }).addr() as u64 + PT_PT;

let pgt_slice = unsafe { core::slice::from_raw_parts_mut(&mut l3_pgtable as *mut u64, 512) };
let pgt_slice = unsafe { core::slice::from_raw_parts_mut(ptr::from_mut(&mut l3_pgtable), 512) };
for i in pgt_slice.iter_mut() {
*i = 0;
}
pgt_slice[1] = uart_address as u64 + PT_MEM_CD;

// map kernel to KERNEL_START and stack below the kernel
let pgt_slice = unsafe { core::slice::from_raw_parts_mut(&mut l2k_pgtable as *mut u64, 512) };
let pgt_slice =
unsafe { core::slice::from_raw_parts_mut(ptr::from_mut(&mut l2k_pgtable), 512) };
for i in pgt_slice.iter_mut() {
*i = 0;
}
for (i, pgt_slice) in pgt_slice.iter_mut().enumerate().take(10) {
*pgt_slice = unsafe { &mut L0mib_pgtable } as *mut _ as u64
*pgt_slice = ptr::from_mut(unsafe { &mut L0mib_pgtable }).addr() as u64
+ (i * BasePageSize::SIZE) as u64
+ PT_PT;
}

let pgt_slice =
unsafe { core::slice::from_raw_parts_mut(&mut L0mib_pgtable as *mut u64, 10 * 512) };
unsafe { core::slice::from_raw_parts_mut(ptr::from_mut(&mut L0mib_pgtable), 10 * 512) };
for (i, entry) in pgt_slice.iter_mut().enumerate() {
*entry = RAM_START + (i * BasePageSize::SIZE) as u64 + PT_MEM;
}
Expand All @@ -207,10 +210,10 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
"msr ttbr0_el1, {}",
"dsb sy",
"isb",
in(reg) &l0_pgtable as *const _ as u64,
in(reg) ptr::from_ref(&l0_pgtable).addr() as u64,
options(nostack),
);
}
)
};

// Enable paging
unsafe {
Expand Down
14 changes: 9 additions & 5 deletions src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod physicalmem;
use core::arch::asm;
#[cfg(all(target_os = "none", not(feature = "fc")))]
use core::mem;
use core::ptr;
#[cfg(target_os = "none")]
use core::ptr::write_bytes;
#[cfg(target_os = "none")]
Expand All @@ -28,6 +29,7 @@ use log::info;
use multiboot::information::MemoryManagement;
#[cfg(all(target_os = "none", not(feature = "fc")))]
use multiboot::information::{Multiboot, PAddr};
use sptr::Strict;
use uart_16550::SerialPort;
use x86_64::structures::paging::{PageSize, PageTableFlags, Size2MiB, Size4KiB};

Expand Down Expand Up @@ -171,8 +173,9 @@ pub fn find_kernel() -> &'static [u8] {
let elf_start = ramdisk_address as usize;
let elf_len = ramdisk_size as usize;

let free_memory_address =
(unsafe { &kernel_end } as *const u8 as usize).align_up(Size2MiB::SIZE as usize);
let free_memory_address = ptr::from_ref(unsafe { &kernel_end })
.addr()
.align_up(Size2MiB::SIZE as usize);
// TODO: Workaround for https://github.com/hermitcore/loader/issues/96
let free_memory_address = cmp::max(free_memory_address, 0x800000);
info!("Intialize PhysAlloc with {:#x}", free_memory_address);
Expand Down Expand Up @@ -283,7 +286,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {

// determine boot stack address
let new_stack =
(unsafe { &kernel_end } as *const u8 as usize + 0x1000).align_up(Size4KiB::SIZE as usize);
(ptr::from_ref(unsafe { &kernel_end }).addr() + 0x1000).align_up(Size4KiB::SIZE as usize);

let cmdline_ptr = unsafe {
*(sptr::from_exposed_addr(boot_params + LINUX_SETUP_HEADER_OFFSET + CMD_LINE_PTR_OFFSET))
Expand Down Expand Up @@ -441,8 +444,9 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
let multiboot = unsafe { Multiboot::from_ptr(mb_info as u64, &mut MEM).unwrap() };

// determine boot stack address
let mut new_stack =
(unsafe { &kernel_end } as *const u8 as usize).align_up(Size4KiB::SIZE as usize);
let mut new_stack = ptr::from_ref(unsafe { &kernel_end })
.addr()
.align_up(Size4KiB::SIZE as usize);

if new_stack + KERNEL_STACK_SIZE as usize > unsafe { mb_info } {
new_stack = (unsafe { mb_info } + mem::size_of::<Multiboot<'_, '_>>())
Expand Down