簡體   English   中英

通過使用內聯匯編的Raspberry Pi3的Rust啟動代碼

[英]Rust startup code for Raspberry Pi3 through use of inline assembly

我正在用Rust為Raspberry Pi 3寫一個裸機代碼,但是,我將代碼放置在0x80000問題,因為它不是_start函數。

編譯器針​​對AArch64架構設置,我使用LLD作為鏈接器。

# .cargo/config
[build]
target = "aarch64-unknown-none"

[target.aarch64-unknown-none]
rustflags = [
  # uncomment to use rustc LLD linker
   "-C", "link-arg=-Tlayout.ld",
   "-C", "linker=lld-link",
   "-Z", "linker-flavor=ld.lld",
]

啟動后要調用的第一個函數:(獲取核心ID並僅讓主線程繼續運行,其他線程停止;為主內存和初始化內存設置堆棧)

#[link_section = ".reset_vector"]
#[no_mangle]
pub extern "C" fn _start() -> !{

    unsafe {
        // Halt all cores but the primary
        asm!(" mrs x1, mpidr_el1
                    and x1, x1, #3
                    cmp x1, #0
                    bne halt"::::"volatile");

        // Setup stack pointer
        asm!(" mov sp, #0x80000"::::"volatile");
    }

  init_runtime();

  main();
  loop{}
}

fn init_runtime() {
    extern "C" {
        static mut _sbss: u64;
        static mut _ebss: u64;

        static mut _sdata: u64;
        static mut _edata: u64;

        static _sidata: u64;
    }

    unsafe{
        // Zero the BSS section in RAM
        r0::zero_bss(&mut _sbss, &mut _ebss);
        // Copy variables in DATA section in FLASH to RAM
        r0::init_data(&mut _sdata, &mut _edata, &_sidata);
    } 
}

停止除主要節點之外的核心的功能:

#[no_mangle]
pub fn halt() {
    unsafe {asm!("wfe"::::"volatile");}
}

我正在使用r0板條箱初始化內存:

fn init_runtime() {
    extern "C" {
        static mut _sbss: u64;
        static mut _ebss: u64;

        static mut _sdata: u64;
        static mut _edata: u64;

        static _sidata: u64;
    }

    unsafe{
        // Zero the BSS section in RAM
        r0::zero_bss(&mut _sbss, &mut _ebss);
        // Copy variables in DATA section in FLASH to RAM
        r0::init_data(&mut _sdata, &mut _edata, &_sidata);
    } 
}

最后是鏈接描述文件:

ENTRY(_start)

SECTIONS {
  . = 0x80000;

  .text : {
      KEEP(*(.reset_vector));
      __reset_vector = ABSOLUTE(.);
      *(.text .text.* .gnu.linkonce.t*)
  }

  .rodata : {
    *(.rodata .rodata.* .gnu.linkonce.r*)
  }

  .data : {
    _sdata = .;
    *(.data .data.* .gnu.linkonce.d*)
    _edata = ALIGN(8);
  }

  .bss (NOLOAD) : {
    . = ALIGN(32);
    _bss = .;
    *(.bss .bss.*)
    *(COMMON)
    _ebss = ALIGN(8);
  }

  __bss_length = (__bss_end - __bss_start);

  /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) }
}

這是反匯編:

(gdb) disassemble 0x0000000000080000, 0x000000000008035c
Dump of assembler code from 0x80000 to 0x8035c:
=> 0x0000000000080000 <core::mem::uninitialized+0>:     sub     sp, sp, #0x10
   0x0000000000080004 <core::mem::uninitialized+4>:     ldr     x0, [sp, #8]
   0x0000000000080008 <core::mem::uninitialized+8>:     str     x0, [sp]
   0x000000000008000c <core::mem::uninitialized+12>:    ldr     x0, [sp]
   0x0000000000080010 <core::mem::uninitialized+16>:    add     sp, sp, #0x10
   0x0000000000080014 <core::mem::uninitialized+20>:    ret

來自函數_start的指令需要放置在@ 0x80000上,但這不是事實,因為存在core::mem::uninitialized的指令。

如何修改鏈接描述文件,使mrs x1, mpidr_el1成為要執行的第一條指令?

經過漫長的夜晚奮斗,我想出了辦法。

首先在鏈接器中創建一個新部分:

ENTRY(reset)
SECTIONS {

  . = 0x80000;
  .reset : {
      KEEP(*(.reset))
      . = ALIGN(8);
    }

  .text  : {
      *(.text .text.* .gnu.linkonce.t*)
  }
  ...

並修改代碼:

#[link_section=".reset"]
#[no_mangle]
#[naked]
pub extern "C" fn reset () {
unsafe {
        // Halt all cores but the primary and set stack pointer
        asm!(" mrs x1, mpidr_el1
               and x1, x1, #3
               cmp x1, #0
               bne halt
               mov sp, #0x80000
               b init
             "::::"volatile");
    }
 }

其余的:

#[naked]
#[no_mangle]
pub fn init() { 
    extern "C" {
        static mut _sbss: u64;
        static mut _ebss: u64;

        static mut _sdata: u64;
        static mut _edata: u64;

        static _sidata: u64;
    }

    unsafe{
        // Zero the BSS section in RAM
        r0::zero_bss(&mut _sbss, &mut _ebss);
        // Copy variables in DATA section in FLASH to RAM
        r0::init_data(&mut _sdata, &mut _edata, &_sidata);
    }

    extern "Rust" {
        fn main() -> !;
    }

    unsafe { main(); }
}

通過分割代碼,復位中的初始化保持在0x80000

Disassembly of section .reset:

0000000000080000 <reset>:
   80000:       d53800a1        mrs     x1, mpidr_el1
   80004:       92400421        and     x1, x1, #0x3
   80008:       f100003f        cmp     x1, #0x0
   8000c:       54001481        b.ne    8029c <halt>  // b.any
   80010:       b26d03ff        mov     sp, #0x80000                    // #524288
   80014:       1400008d        b       80248 <init>
   80018:       d65f03c0        ret
   8001c:       00000000        .inst   0x00000000 ; undefined

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM