Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ fn set_linker_script() {
let cfg_feature = env::var("CARGO_CFG_FEATURE").unwrap();

let linker_script = match cfg_target_arch.as_str() {
"aarch64" if cfg_feature.contains("elf") => "link.ld",
"riscv64" if cfg_feature.contains("sbi") => "link.ld",
"x86_64" if cfg_feature.contains("multiboot") => "platform/multiboot/link.ld",
_ => return,
};
Expand Down
3 changes: 3 additions & 0 deletions src/arch/aarch64/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ use aarch64_cpu::registers::{
use log::info;
use tock_registers::fields::{FieldValue, TryFromValue};

use crate::stack::STACK;

/// Number of virtual address bits for 4KB page
const VA_BITS: u64 = 48;

global_asm!(
include_str!("entry.s"),
BOOT_STACK = sym STACK,
start_rust = sym start_rust,
);

Expand Down
4 changes: 2 additions & 2 deletions src/arch/aarch64/entry.s
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ _start:

// This loads the physical address of the stack end. For details see
// https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/blob/master/16_virtual_mem_part4_higher_half_kernel/src/bsp/raspberrypi/link.ld
adrp x4, __boot_core_stack_end_exclusive
add x4, x4, #:lo12:__boot_core_stack_end_exclusive
adrp x4, {BOOT_STACK}
add x4, x4, #:lo12:{BOOT_STACK}
mov sp, x4

// Set correct Exception level!
Expand Down
49 changes: 0 additions & 49 deletions src/arch/aarch64/link.ld

This file was deleted.

7 changes: 2 additions & 5 deletions src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ use hermit_entry::boot_info::{BootInfo, HardwareInfo, PlatformInfo, RawBootInfo,
use hermit_entry::elf::LoadedKernel;
use log::info;

use crate::BootInfoExt;
use crate::arch::paging::*;
use crate::fdt_ext::FdtExt;
use crate::os::CONSOLE;
use crate::{BootInfoExt, stack};

/// start address of the RAM at Qemu's virt emulation
const RAM_START: u64 = 0x40000000;
/// Default stack size of the kernel
const KERNEL_STACK_SIZE: usize = 32_768;
/// Qemu assumes for ELF kernel that the fdt is located at
/// start of RAM (0x4000_0000)
/// see <https://qemu.readthedocs.io/en/latest/system/arm/virt.html>
Expand Down Expand Up @@ -102,8 +100,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
platform_info: PlatformInfo::LinuxBoot,
};

let stack = boot_info.load_info.kernel_image_addr_range.start as usize - KERNEL_STACK_SIZE;
let stack = ptr::with_exposed_provenance_mut(stack);
let stack = stack::get_stack_ptr();
let entry = ptr::with_exposed_provenance(entry_point.try_into().unwrap());
let raw_boot_info = boot_info.write();

Expand Down
11 changes: 0 additions & 11 deletions src/arch/riscv64/link.ld

This file was deleted.

4 changes: 2 additions & 2 deletions src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use hermit_entry::boot_info::{
use hermit_entry::elf::LoadedKernel;
use log::info;

use crate::BootInfoExt;
use crate::fdt_ext::FdtExt;
use crate::{BootInfoExt, stack};

pub fn find_kernel() -> &'static [u8] {
let fdt = start::get_fdt();
Expand Down Expand Up @@ -92,7 +92,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
platform_info: PlatformInfo::LinuxBoot,
};

let stack = start::get_stack_ptr();
let stack = stack::get_stack_ptr();
let entry = ptr::with_exposed_provenance(entry_point.try_into().unwrap());
let hart_id = start::get_hart_id();
let raw_boot_info = boot_info.write();
Expand Down
26 changes: 5 additions & 21 deletions src/arch/riscv64/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};

use fdt::Fdt;

static mut STACK: Stack = Stack::new();
use crate::stack::{STACK, Stack};

static HART_ID: AtomicUsize = AtomicUsize::new(0);
static FDT: AtomicPtr<u8> = AtomicPtr::new(ptr::null_mut());

Expand All @@ -20,29 +21,24 @@ pub fn get_fdt() -> Fdt<'static> {
unsafe { Fdt::from_ptr(get_fdt_ptr()).unwrap() }
}

pub fn get_stack_ptr() -> *mut u8 {
let stack_top = &raw mut STACK;
// SAFETY: Pointing directly past the object is allowed
let stack_bottom = unsafe { stack_top.add(1) };
stack_bottom.cast::<u8>()
}

// TODO: Migrate to Constrained Naked Functions once stabilized
// https://github.com/rust-lang/rust/issues/90957
// TODO: Migrate to asm_const for Stack::SIZE once stabilized
// https://github.com/rust-lang/rust/issues/93332
#[no_mangle]
#[naked_function::naked]
#[link_section = ".init"]
pub unsafe extern "C" fn _start(hart_id: usize, fdt: *const u8) -> ! {
asm!(
// Initialize stack
"la sp, {BOOT_STACK}",
"li t0, 0x8000",
"li t0, {STACK_SIZE}",
"add sp, sp, t0",

"j {start}",

BOOT_STACK = sym STACK,
STACK_SIZE = const Stack::SIZE,
start = sym start,
)
}
Expand All @@ -54,15 +50,3 @@ extern "C" fn start(hart_id: usize, fdt: *const u8) -> ! {

unsafe { crate::os::loader_main() }
}

// Align to page size
#[repr(C, align(0x1000))]
pub struct Stack([u8; Self::SIZE]);

impl Stack {
const SIZE: usize = 0x8000;

pub const fn new() -> Self {
Self([0; Self::SIZE])
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mod fdt;
mod fdt_ext;
mod log;
mod os;
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
mod stack;

#[cfg(any(target_os = "uefi", all(target_arch = "x86_64", target_os = "none")))]
extern crate alloc;
Expand Down
20 changes: 20 additions & 0 deletions src/stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub static mut STACK: Stack = Stack::new();

// Align to page size
#[repr(C, align(0x1000))]
pub struct Stack([u8; Self::SIZE]);

impl Stack {
pub const SIZE: usize = 0x8000;

pub const fn new() -> Self {
Self([0; Self::SIZE])
}
}

pub fn get_stack_ptr() -> *mut u8 {
let stack_top = &raw mut STACK;
// SAFETY: Pointing directly past the object is allowed
let stack_bottom = unsafe { stack_top.add(1) };
stack_bottom.cast::<u8>()
}
9 changes: 9 additions & 0 deletions xtask/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ impl Target {

pub fn rustflags(&self) -> &'static [&'static str] {
match self {
Self::Aarch64Elf | Self::Aarch64BeElf => &[
"-Crelocation-model=static",
"-Clink-arg=--image-base=0x40400000",
],
Self::Riscv64Sbi => &[
"-Crelocation-model=static",
"-Clink-arg=--image-base=0x801ffe00",
"-Clink-arg=--section-start=.init=0x80200000",
],
Self::X86_64Linux | Self::X86_64Multiboot => &["-Crelocation-model=static"],
_ => &[],
}
Expand Down