noteed/riscv-hello-asm

Can't run (requested regions overlap)

telmotrooper opened this issue · 5 comments

I'm using the toolchain binaries from SiFive.

I can compile the program just fine with the command:

riscv64-unknown-elf-gcc -march=rv64g -mabi=lp64 -static -mcmodel=medany \
  -fvisibility=hidden -nostdlib -nostartfiles -Thello.ld hello.s -o hello

And it generates the expected file:

file hello

hello: ELF 64-bit LSB executable, UCB RISC-V, version 1 (SYSV), statically linked, not stripped

But when I try to run the program in QEMU I get an error:

qemu-system-riscv64 -nographic -machine sifive_u -kernel hello

rom: requested regions overlap (rom phdr #0: hello. free=0x000000008000e250, addr=0x0000000080000000)
qemu-system-riscv64: rom check and register reset failed

My QEMU version is:

qemu-system-riscv64 --versionQEMU emulator version 5.1.0

Copyright (c) 2003-2020 Fabrice Bellard and the QEMU Project developers

Can you please help me understand why is the program not working?

I'm not sure I can help (I don't know much about assembly, qemu, ...) and I don't have notes about how I installed the toolchain. Anyway, I will note here what I found.

I have still my hello program from May 2019, I tried to run it. I have a warning about a missing -bios option, and the Hello. string is not printed...

$ ls -lrt
total 20
-rw-r--r-- 1 thu users  529 May 13  2019 hello.s
-rw-r--r-- 1 thu users  386 May 13  2019 hello.ld
-rwxr-xr-x 1 thu users 5080 May 13  2019 hello
-rw-r--r-- 1 thu users 2287 May 13  2019 README.md

$ qemu-system-riscv64 -nographic -machine sifive_u -kernel hello
qemu-system-riscv64: warning: No -bios option specified. Not loading a firmware.
qemu-system-riscv64: warning: This default will change in a future QEMU release. Please use the -bios option to avoid breakages when this happens.
qemu-system-riscv64: warning: See QEMU's deprecation documentation for details.
QEMU: Terminated

$ qemu-system-riscv64 --version
QEMU emulator version 4.2.0
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers

This seems to be a new option, the default behavior has changed. It is documented here: https://www.qemu.org/docs/master/system/deprecated.html#risc-v-bios-since-5-1 and is also discussed here: https://patchew.org/QEMU/cover.1560904640.git.alistair.francis@wdc.com/, and the error message is even described.

Using -bios none removes the warning but doesn't change the behavior (no Hello. string is produced), but using -bios default result in the reported problem above:

$ qemu-system-riscv64 -nographic -machine sifive_u -bios none -kernel hello
QEMU: Terminated

[thu@tank:~/projects/riscv-hello-asm]$ qemu-system-riscv64 -nographic -machine sifive_u -bios default -kernel hello
rom: requested regions overlap (rom phdr #0: hello. free=0x000000008000c008, addr=0x0000000080000000)
qemu-system-riscv64: rom check and register reset failed

Note that the exact values are different.

I think the -bios none is actually what we need (to restore the previously default behavior), and maybe I don't see the string appear because some flushing would now be needed or we need to disable some buffering ?

Or maybe the UART base address has changed ? https://patchwork.ozlabs.org/project/qemu-devel/patch/1567786819-22142-26-git-send-email-bmeng.cn@gmail.com/

Oh yes, changing the UART base address seems to work !

$ git diff
diff --git a/hello.s b/hello.s
index ea63b86..8e3ec90 100644
--- a/hello.s
+++ b/hello.s
@@ -1,5 +1,5 @@
 .align 2
-.equ UART_BASE,         0x10013000
+.equ UART_BASE,         0x10010000
 .equ UART_REG_TXFIFO,   0
 
 .section .text
$ qemu-system-riscv64 -nographic -machine sifive_u -bios none -kernel hello
Hello.
Hello.
QEMU: Terminated

Note that the string Hello. is displayed twice; some times the two lines are interleaved.

I have pushed 7d38373, which adds the -bios none option, and changes the base UART address as mentioned above, and fix this issue.

I'll keep it open though, because the string Hello. is displayed twice, which should be fixed too.

I met the same issue. The "hello" can not be printed out.
At the beginning, I compiled the "riscv-hello-asm" with the below 2 toolchains, but both can not work.

1) Cross compiler on Ubuntu 20.04 host machine installed by apt install
user@Ubuntu2004:~/riscv/riscv-gnu-toolchain$ riscv64-linux-gnu-gcc --version
riscv64-linux-gnu-gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
2) Native compiler on Fedora 32 guest VM installed by yum install
[root@fedora-riscv riscv-hello-asm]# gcc --version
gcc (GCC) 9.2.1 20191120 (Red Hat 9.2.1-2)

At the end, I tried with the toolchain from "https://github.com/riscv/riscv-gnu-toolchain" on Ubuntu 20.04 host. It can work.
The compiled riscv-gnu-toolchain is located in /opt/riscv/bin/.
Logs:

user@Ubuntu2004:~/riscv/test/riscv-hello-asm$ qemu-system-riscv64 --version
QEMU emulator version 5.0.0
Copyright (c) 2003-2020 Fabrice Bellard and the QEMU Project developers
user@Ubuntu2004:~/riscv/riscv-gnu-toolchain$ git log -1
commit 7553f354bf107ef98f88b824845f61a875502a72 (HEAD -> master, origin/master, origin/HEAD)
......
user@Ubuntu2004:~/riscv/test/riscv-hello-asm$ /opt/riscv/bin/riscv64-unknown-elf-gcc -march=rv64g -mabi=lp64 -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles -Thello.ld hello.s -o hello
user@Ubuntu2004:~/riscv/test/riscv-hello-asm$ qemu-system-riscv64 -nographic -machine sifive_u -bios none -kernel hello
Hello.
Hello.
QEMU: Terminated

But, the same, I got 2 "Hello" printed out.

Just open my experiences here to help those guys who will try the example to save their time.
@noteed , Thanks for your example! It helped me a lot!

Thanks for the comment @McKnight22 .

With #2 merge, the remaining problem of the double output is gone.