通过使用内联汇编的Raspberry Pi3的Rust启动代码

时间:2018-09-23 21:07:29

标签: linker raspberry-pi rust

我在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成为要执行的第一条指令?

1 个答案:

答案 0 :(得分:2)

经过漫长的夜晚奋斗,我想出了办法。

首先在链接器中创建一个新部分:

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