Jelajahi Sumber

Now this function works

luojia65 4 tahun lalu
induk
melakukan
9badf809f5
1 mengubah file dengan 37 tambahan dan 42 penghapusan
  1. 37 42
      platform/qemu/src/main.rs

+ 37 - 42
platform/qemu/src/main.rs

@@ -78,10 +78,11 @@ pub extern "C" fn mp_hook() -> bool {
 }
 
 #[export_name = "_start"]
-#[link_section = ".text.entry"]
+#[link_section = ".text.entry"] // this is stable
 #[naked]
-unsafe extern "C" fn start() -> ! {
-    asm!(
+fn main() -> ! {
+    unsafe {
+        llvm_asm!(
             "
         csrr    a2, mhartid
         lui     t0, %hi(_max_hart_id)
@@ -104,25 +105,23 @@ unsafe extern "C" fn start() -> ! {
     .endif
         sub     sp, sp, t0
         csrw    mscratch, zero
-        j       main
+        j _start_success
         
     _start_abort:
         wfi
         j _start_abort
-    ", options(noreturn))
-}
-
-#[export_name = "main"]
-#[link_section = ".text"]
-fn main() {
+    _start_success:
+        
+    "
+        )
+    };
     // Ref: https://github.com/qemu/qemu/blob/aeb07b5f6e69ce93afea71027325e3e7a22d2149/hw/riscv/boot.c#L243
-    #[cfg(riscv)] 
     let dtb_pa = unsafe {
         let dtb_pa: usize;
-        asm!("", in("a1") dtb_pa);
+        llvm_asm!("":"={a1}"(dtb_pa));
         dtb_pa
     };
-    
+
     if mp_hook() {
         // init
     }
@@ -130,13 +129,13 @@ fn main() {
     /* setup trap */
 
     extern "C" {
-        fn _start_trap_asm();
+        fn _start_trap();
     }
-
     unsafe {
-        mtvec::write(_start_trap_asm as usize, TrapMode::Direct);
+        mtvec::write(_start_trap as usize, TrapMode::Direct);
     }
 
+
     /* main function start */
 
     extern "C" {
@@ -212,36 +211,34 @@ fn main() {
         println!("[rustsbi] Kernel entry: 0x80200000");
     }
 
+    extern "C" {
+        fn _s_mode_start();
+    }
     unsafe {
-        mepc::write(s_mode_start as usize);
+        mepc::write(_s_mode_start as usize);
         mstatus::set_mpp(MPP::Supervisor);
         #[cfg(riscv)] 
-        rustsbi::enter_privileged(mhartid::read(), dtb_pa)
+        return rustsbi::enter_privileged(mhartid::read(), dtb_pa);
+        #[cfg(not(riscv))] 
+        unreachable!()
     }
 }
 
-#[naked]
-#[link_section = ".text"]
-unsafe extern "C" fn s_mode_start() -> ! {
-    asm!(
-        "
-    .option push
-    .option norelax
-1:
-    auipc ra, %pcrel_hi(1f)
+global_asm!(
+    "
+    .section .text
+    .globl _s_mode_start
+_s_mode_start:
+1:  auipc ra, %pcrel_hi(1f)
     ld ra, %pcrel_lo(1b)(ra)
     jr ra
-    .align  3
-1:
-    .dword 0x80200000
-    .option pop
-    ", options(noreturn))
-}
+.align  3
+1:  .dword 0x80200000
+"
+);
 
-#[naked]
-pub unsafe extern "C" fn start_trap() {
-    asm!(
-        "
+global_asm!(
+    "
     .equ REGBYTES, 8
     .macro STORE reg, offset
         sd  \\reg, \\offset*REGBYTES(sp)
@@ -249,8 +246,7 @@ pub unsafe extern "C" fn start_trap() {
     .macro LOAD reg, offset
         ld  \\reg, \\offset*REGBYTES(sp)
     .endm
-
-    .pushsection .text
+    .section .text
     .global _start_trap
     .p2align 2
 _start_trap:
@@ -297,9 +293,8 @@ _start_trap:
     addi    sp, sp, 16 * REGBYTES
     csrrw   sp, mscratch, sp
     mret
-    .popsection
-    ", options(noreturn));
-}
+    "
+);
 
 // #[doc(hidden)]
 // #[export_name = "_mp_hook"]