feat: ✨ Add UnsafeCell trick
It replaces old "C" style linker symbol references.
This commit is contained in:
		
							parent
							
								
									29d61f4bdb
								
							
						
					
					
						commit
						113b4abbc5
					
				| 
						 | 
					@ -9,6 +9,7 @@ core::arch::global_asm!(include_str!("boot.s"));
 | 
				
			||||||
#[cfg(not(feature = "asm"))]
 | 
					#[cfg(not(feature = "asm"))]
 | 
				
			||||||
pub unsafe extern "C" fn _start() -> ! {
 | 
					pub unsafe extern "C" fn _start() -> ! {
 | 
				
			||||||
    use {
 | 
					    use {
 | 
				
			||||||
 | 
					        core::cell::UnsafeCell,
 | 
				
			||||||
        cortex_a::registers::{MPIDR_EL1, SP},
 | 
					        cortex_a::registers::{MPIDR_EL1, SP},
 | 
				
			||||||
        machine::endless_sleep,
 | 
					        machine::endless_sleep,
 | 
				
			||||||
        tock_registers::interfaces::{Readable, Writeable},
 | 
					        tock_registers::interfaces::{Readable, Writeable},
 | 
				
			||||||
| 
						 | 
					@ -24,31 +25,30 @@ pub unsafe extern "C" fn _start() -> ! {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // These are a problem, because they are not interpreted as constants here.
 | 
					    // These are a problem, because they are not interpreted as constants here.
 | 
				
			||||||
    // Subsequently, this code tries to read values from not-yet-existing data locations.
 | 
					    // Subsequently, this code tries to read values from not-yet-existing data locations.
 | 
				
			||||||
    extern "C" {
 | 
					    extern "Rust" {
 | 
				
			||||||
        // Boundaries of the .bss section, provided by the linker script
 | 
					        // Boundaries of the .bss section, provided by the linker script
 | 
				
			||||||
        static mut __bss_start: u64;
 | 
					        static __bss_start: UnsafeCell<()>;
 | 
				
			||||||
        static mut __bss_end_exclusive: u64;
 | 
					        static __bss_end_exclusive: UnsafeCell<()>;
 | 
				
			||||||
        // Load address of the kernel binary
 | 
					        // Load address of the kernel binary
 | 
				
			||||||
        static mut __binary_nonzero_lma: u64;
 | 
					        static __binary_nonzero_lma: UnsafeCell<()>;
 | 
				
			||||||
        // Address to relocate to and image size
 | 
					        // Address to relocate to and image size
 | 
				
			||||||
        static mut __binary_nonzero_vma: u64;
 | 
					        static __binary_nonzero_vma: UnsafeCell<()>;
 | 
				
			||||||
        static mut __binary_nonzero_vma_end_exclusive: u64;
 | 
					        static __binary_nonzero_vma_end_exclusive: UnsafeCell<()>;
 | 
				
			||||||
        // Stack top
 | 
					        // Stack top
 | 
				
			||||||
        static mut __boot_core_stack_end_exclusive: u64;
 | 
					        static __boot_core_stack_end_exclusive: UnsafeCell<()>;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Set stack pointer.
 | 
					    // Set stack pointer.
 | 
				
			||||||
    SP.set(&mut __boot_core_stack_end_exclusive as *mut u64 as u64);
 | 
					    SP.set(__boot_core_stack_end_exclusive.get() as u64);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Zeroes the .bss section
 | 
					    // Zeroes the .bss section
 | 
				
			||||||
    r0::zero_bss(&mut __bss_start, &mut __bss_end_exclusive);
 | 
					    r0::zero_bss(__bss_start.get() as u64, __bss_end_exclusive.get() as u64);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Relocate the code
 | 
					    // Relocate the code
 | 
				
			||||||
    core::ptr::copy_nonoverlapping(
 | 
					    core::ptr::copy_nonoverlapping(
 | 
				
			||||||
        &mut __binary_nonzero_lma as *const u64,
 | 
					        __binary_nonzero_lma.get() as *const u64,
 | 
				
			||||||
        &mut __binary_nonzero_vma as *mut u64,
 | 
					        __binary_nonzero_vma.get() as *mut u64,
 | 
				
			||||||
        (&mut __binary_nonzero_vma_end_exclusive as *mut u64 as u64
 | 
					        (__binary_nonzero_vma_end_exclusive.get() as usize - __binary_nonzero_vma.get() as usize),
 | 
				
			||||||
            - &mut __binary_nonzero_vma as *mut u64 as u64) as usize,
 | 
					 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _start_rust();
 | 
					    _start_rust();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +23,7 @@ use core::panic::PanicInfo;
 | 
				
			||||||
use machine::devices::SerialOps;
 | 
					use machine::devices::SerialOps;
 | 
				
			||||||
use {
 | 
					use {
 | 
				
			||||||
    cfg_if::cfg_if,
 | 
					    cfg_if::cfg_if,
 | 
				
			||||||
 | 
					    core::cell::UnsafeCell,
 | 
				
			||||||
    machine::{
 | 
					    machine::{
 | 
				
			||||||
        arch, entry, memory,
 | 
					        arch, entry, memory,
 | 
				
			||||||
        platform::rpi3::{
 | 
					        platform::rpi3::{
 | 
				
			||||||
| 
						 | 
					@ -55,14 +56,12 @@ fn init_mmu() {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn init_exception_traps() {
 | 
					fn init_exception_traps() {
 | 
				
			||||||
    extern "C" {
 | 
					    extern "Rust" {
 | 
				
			||||||
        static __exception_vectors_start: u64;
 | 
					        static __exception_vectors_start: UnsafeCell<()>;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    unsafe {
 | 
					    unsafe {
 | 
				
			||||||
        let exception_vectors_start: u64 = &__exception_vectors_start as *const _ as u64;
 | 
					        arch::traps::set_vbar_el1_checked(__exception_vectors_start.get() as u64)
 | 
				
			||||||
 | 
					 | 
				
			||||||
        arch::traps::set_vbar_el1_checked(exception_vectors_start)
 | 
					 | 
				
			||||||
            .expect("Vector table properly aligned!");
 | 
					            .expect("Vector table properly aligned!");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    println!("[!] Exception traps set up");
 | 
					    println!("[!] Exception traps set up");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue