rcore-os / rCore-Tutorial-v3

Let's write an OS which can run on RISC-V in Rust from scratch!
https://rcore-os.github.io/rCore-Tutorial-Book-v3/index.html
GNU General Public License v3.0
1.58k stars 452 forks source link

第一章实现控制台输出字符串后运行内核,qemu没有任何输出 #113

Closed ZephyrusZhang closed 1 year ago

ZephyrusZhang commented 1 year ago

请问按照这节写下来后,用qemu运行内核,但是一直没有输出可能是什么原因啊?

运行内核后的输出

[rustsbi] RustSBI version 0.3.0-alpha.4, adapting to RISC-V SBI v1.0.0
.______       __    __      _______.___________.  _______..______   __
|   _  \     |  |  |  |    /       |           | /       ||   _  \ |  |
|  |_)  |    |  |  |  |   |   (----`---|  |----`|   (----`|  |_)  ||  |
|      /     |  |  |  |    \   \       |  |      \   \    |   _  < |  |
|  |\  \----.|  `--'  |.----)   |      |  |  .----)   |   |  |_)  ||  |
| _| `._____| \______/ |_______/       |__|  |_______/    |______/ |__|
[rustsbi] Implementation     : RustSBI-QEMU Version 0.2.0-alpha.2
[rustsbi] Platform Name      : riscv-virtio,qemu
[rustsbi] Platform SMP       : 1
[rustsbi] Platform Memory    : 0x80000000..0x88000000
[rustsbi] Boot HART          : 0
[rustsbi] Device Tree Region : 0x87000000..0x87000ef2
[rustsbi] Firmware Address   : 0x80000000
[rustsbi] Supervisor Address : 0x80200000
[rustsbi] pmp01: 0x00000000..0x80000000 (-wr)
[rustsbi] pmp02: 0x80000000..0x80200000 (---)
[rustsbi] pmp03: 0x80200000..0x88000000 (xwr)
[rustsbi] pmp04: 0x88000000..0x00000000 (-wr)

运行内核所使用的命令

cargo build --release

rust-objcopy --strip-all target/riscv64gc-unknown-none-elf/release/os -O binary target/riscv64gc-unknown-none-elf/release/os.bin

qemu-system-riscv64 \
  machine virt \
  -nographic \
  -bios ../bootloader/rustsbi-qemu.bin \
  -device loader,file=target/riscv64gc-unknown-none-elf/release/os.bin,addr=0x80200000

其中,使用rust-objcopy得到的文件os.bin查看其信息,如下所示

stat os/target/riscv64gc-unknown-none-elf/release/os.bin

# 输出
文件:os/target/riscv64gc-unknown-none-elf/release/os.bin
大小:0               块:0          IO 块大小:4096   普通空文件
设备:803h/2051d        Inode:1108612     硬链接:1
权限:(0775/-rwxrwxr-x)  Uid: ( 1000/zephyrus)   Gid: ( 1000/zephyrus)
访问时间:2023-03-09 15:53:14.616402778 +0800
修改时间:2023-03-09 15:53:05.904545932 +0800
变更时间:2023-03-09 15:53:05.912545800 +0800
创建时间:2023-03-09 15:53:05.904545932 +0800

反汇编的话也是空的

# 执行
rust-objdump -all target/riscv64gc-unknown-none-elf/release/os

# 输出

target/riscv64gc-unknown-none-elf/release/os:   file format elf64-littleriscv

使用gdb进行调试,当运行到位于0x80200000这条指令时,查看查看$pc及后面几条指令,发现都是空

Remote debugging using localhost:1234
0x0000000000001000 in ?? ()
(gdb) b *0x80200000
Breakpoint 1 at 0x80200000
(gdb) c
Continuing.

Breakpoint 1, 0x0000000080200000 in ?? ()
(gdb) x/10i $pc
=> 0x80200000:  unimp
   0x80200002:  unimp
   0x80200004:  unimp
   0x80200006:  unimp
   0x80200008:  unimp
   0x8020000a:  unimp
   0x8020000c:  unimp
   0x8020000e:  unimp
   0x80200010:  unimp
   0x80200012:  unimp

似乎是链接的时候内存布局有问题,导致入口代码没有被排在0x80200000

我已经尝试过评论区中说的重新cargo build --release,以及rust-objcopy --strip-all <> -O binary <>这两个方法,但是还是没有输出。我个人感觉是链接的时候除了问题,但是还是不知道怎么解决这个问题。代码在[ZephyrusZhang/rCore](https://github.com/ZephyrusZhang/rCore/tree/5cb772cb6bad16cb9a6e988d568f16e5bba8e4dc)。求大佬赐教。

ZephyrusZhang commented 1 year ago

问题出在没有把entry.asm嵌入到rust_main中(x