Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I compile a c program such that the binary can be run/simulated by an RTL config? #816

Closed
JL102 opened this issue Mar 5, 2021 · 13 comments
Labels

Comments

@JL102
Copy link
Contributor

JL102 commented Mar 5, 2021

Hello,

I am attempting to use riscv64-unknown-elf-gcc to compile a simple test program, and have it run on an RTL config (from section 2.1 in the docs).

This is the test file, hello.c. I initially had it print a "hello world" statement, but changed it such that it does not need any imports or libraries:

int main() {
   int x = 5;
   return 0;
}

This is what I am doing:

  • To compile: riscv64-unknown-elf-gcc -g hello.c -o hello-riscv
  • I am able to simulate it with Spike successfully: spike pk hello-riscv runs without errors. (When my "hello world" program had a printf statement, it printed correctly as well.)
  • Then, I attempt to run the executable with an RTL config. Inside chipyard/sims/verilator, I run: ./simulator-chipyard-RocketConfig ~/hello-riscv
    This is the output I get:
This emulator compiled with JTAG Remote Bitbang client. To enable, use +jtag_rbb_enable=1.
Listening on port 36041
[UART] UART0 is here (stdin/stdout).
warning: tohost and fromhost symbols not in ELF; can't communicate with target

and then it hangs indefinitely. Upon examining top, I can see that simulator-chipy continues to utilize 100% of a CPU thread.

Is there anything I'm doing wrong? How do I compile the program such that it runs correctly? I could not find any information about tohost and fromhost, aside from chapter 6.12, which talks about booting a Linux kernel. I'm not attempting to boot Linux via the RISC-V cpu; I just want it to run a certain binary like in the default examples.

@JL102 JL102 added the question label Mar 5, 2021
@alonamid
Copy link
Contributor

alonamid commented Mar 7, 2021

Could you try running make run-binary CONFIG=RocketConfig BINARY=/full/path/hello-riscv instead of ./simulator-chipyard-RocketConfig ~/hello-riscv ?

@JL102
Copy link
Contributor Author

JL102 commented Mar 8, 2021

Just did now. Same thing, except I don't get the "Listening on port", "UART0", and "warning: tohost and fromhost" messages.

drak@JBOOK-PRIMARY:~/chipyard/sims/verilator$ make run-binary CONFIG=RocketConfig BINARY=/home/drak/hello-riscv
Running with RISCV=/home/drak/chipyard/esp-tools-install
(set -o pipefail &&  /home/drak/chipyard/sims/verilator/simulator-chipyard-RocketConfig +permissive +dramsim +dramsim_ini_dir=/home/drak/chipyard/generators/testchipip/src/main/resources/dramsim2_ini +max-cycles=10000000   +verbose +permissive-off /home/drak/hello-riscv </dev/null 2> >(spike-dasm > /home/drak/chipyard/sims/verilator/output/chipyard.TestHarness.RocketConfig/hello-riscv.out) | tee /home/drak/chipyard/sims/verilator/output/chipyard.TestHarness.RocketConfig/hello-riscv.log)

The process hangs and continues to utilize 100% of a CPU thread, as before.

If I attempt to pass it a file that does not exist, or if I pass it an executable that was compiled for x64, the process terminates with error 255.

@lxu28973
Copy link

I encountered the same problem. I compile my c program with riscv64-unknown-elf-gcc, and get the ELF file a.out. I run ./simulator-chipyard-MegaBoomConfig pk ~/H/hello-world/Gslexample/a.out, and it just hangs. After some search, I try ./simulator-chipyard-MegaBoomConfig pk ~/H/hello-world/Gslexample/a.out +dramsim. It seems like stuck, but after a long time(about 30 min), it gets the results. So, maybe just be patient.

@RaoShri
Copy link

RaoShri commented Mar 31, 2021

Hi,
I tried to run the binary from pre-built riscv-tests, using the following command:
make run-binary BINARY=$RISCV/riscv64-unknown-elf/share/riscv-tests/isa/rv64ui-p-simple

I am getting the below error:

[UART] UART0 is here (stdin/stdout).
make: *** [/home/shrinidhi/Project/chipyard/common.mk:162: run-binary] Error 255

When I try to run tests this way:
└─$ ./simulator-chipyard-BoomCustomConfig $RISCV/bin/target/share/riscv-tests/isa/rv64ui-p-add

I get the UART0 print but nothing after that:
This emulator compiled with JTAG Remote Bitbang client. To enable, use +jtag_rbb_enable=1.
Listening on port 34083
[UART] UART0 is here (stdin/stdout).

Am I missing anything here? Please help out.

@alonamid
Copy link
Contributor

@JL102 , I noticed that when you ran your program on Spike, you ran it with pk. Was it able to run on Spike without pk as well?

@RaoShri , It would be useful to run the first make command (make run-binary) in verbose mode, so we can see where it actually fails. Regarding calling the simulator directly (your second method), have you tried running the same test program with RocketConfig? That would be a good sanity check to confirm whether the issue is with running the program, or rather with some internal modification in your CustomConfig

@RaoShri
Copy link

RaoShri commented Mar 31, 2021

@alonamid ,
Yes I get the same result when using second method with RocketConfig as well :

└─$ ./simulator-chipyard-RocketConfig $RISCV/bin/target/share/riscv-tests/isa/rv64ui-p-add
This emulator compiled with JTAG Remote Bitbang client. To enable, use +jtag_rbb_enable=1.
Listening on port 46735
[UART] UART0 is here (stdin/stdout).

About adding the verbose to the first command, how do I do it? Enabling verbose would help me debug a lot of things faster. Might seem like a trivial question, bear with me, I am new to this.

@alonamid
Copy link
Contributor

alonamid commented Apr 1, 2021

You can run make run-binary-debug instead of make run-binary, and that should enable one of the verbose flags. It should generate a .out and .log file which will hopefully have some additional information.

@RaoShri
Copy link

RaoShri commented Apr 1, 2021

@alonamid When running with run-make-binary, I was able to see the errors and fix it.

└─$ make run-binary-debug BINARY=$RISCV/bin/target/share/riscv-tests/isa/rv64ui-p-add CONFIG=ShrinidhiCustomConfig
Running with RISCV=/opt/riscv/bin
(set -o pipefail && /home/shrinidhi/Project/chipyard/sims/verilator/simulator-chipyard-ShrinidhiCustomConfig-debug +permissive +dramsim +dramsim_ini_dir=/home/shrinidhi/Project/chipyard/generators/testchipip/src/main/resources/dramsim2_ini +max-cycles=10000000 +verbose -v/home/shrinidhi/Project/chipyard/sims/verilator/output/chipyard.TestHarness.ShrinidhiCustomConfig/rv64ui-p-add.vcd +permissive-off /opt/riscv/bin/bin/target/share/riscv-tests/isa/rv64ui-p-add </dev/null 2> >(spike-dasm > /home/shrinidhi/Project/chipyard/sims/verilator/output/chipyard.TestHarness.ShrinidhiCustomConfig/rv64ui-p-add.out) | tee /home/shrinidhi/Project/chipyard/sims/verilator/output/chipyard.TestHarness.ShrinidhiCustomConfig/rv64ui-p-add.log)
[UART] UART0 is here (stdin/stdout).

Again terminal print is blank, but I see the test has passed in the log.
How do I add verbose to the below command?

└─$ ./simulator-chipyard-ShrinidhiCustomConfig $RISCV/bin/target/share/riscv-tests/isa/rv64ui-p-bltu
This emulator compiled with JTAG Remote Bitbang client. To enable, use +jtag_rbb_enable=1.
Listening on port 46485
[UART] UART0 is here (stdin/stdout).

I see this, but no corresponding log/out file in output/chipyard.TestHarness.ShrinidhiCustomConfig. I am guessing this test also has passed but I have no print in terminal nor any log file for it.

@lpppeipei
Copy link

Hi, I tried to run the binary from pre-built riscv-tests, using the following command: make run-binary BINARY=$RISCV/riscv64-unknown-elf/share/riscv-tests/isa/rv64ui-p-simple

I am getting the below error:

[UART] UART0 is here (stdin/stdout). make: *** [/home/shrinidhi/Project/chipyard/common.mk:162: run-binary] Error 255

When I try to run tests this way: └─$ ./simulator-chipyard-BoomCustomConfig $RISCV/bin/target/share/riscv-tests/isa/rv64ui-p-add

I get the UART0 print but nothing after that: This emulator compiled with JTAG Remote Bitbang client. To enable, use +jtag_rbb_enable=1. Listening on port 34083 [UART] UART0 is here (stdin/stdout).

Am I missing anything here? Please help out.

I got the same question with you , have you saved your problem ? And how ? Thanks!

@scottzhang
Copy link

Please read the document carefully.

https://chipyard.readthedocs.io/en/latest/Software/Baremetal.html

#include <stdio.h>

int main(void)
{
printf("Hello, World!\n");
return 0;
}

$ riscv64-unknown-elf-gcc -fno-common -fno-builtin-printf -specs=htif_nano.specs -c hello.c
$ riscv64-unknown-elf-gcc -static -specs=htif_nano.specs hello.o -o hello.riscv
$ spike hello.riscv
Hello, World!

root@51df727fcb26:/chipyard/sims/verilator# make run-binary BINARY=./hello.riscv
Running with RISCV=/root/chipyard/esp-tools-install
(set -o pipefail && /root/chipyard/sims/verilator/simulator-chipyard-RocketConfig +permissive +dramsim +dramsim_ini_dir=/root/chipyard/generators/testchipip/src/main/resources/dramsim2_ini +max-cycles=10000000 +verbose +permissive-off ./hello.riscv </dev/null 2> >(spike-dasm > /root/chipyard/sims/verilator/output/chipyard.TestHarness.RocketConfig/hello.out) | tee /root/chipyard/sims/verilator/output/chipyard.TestHarness.RocketConfig/hello.log)
[UART] UART0 is here (stdin/stdout).
Hello, World!
root@51df727fcb26:
/chipyard/sims/verilator#

@JL102
Copy link
Contributor Author

JL102 commented Jan 17, 2022

Thanks, I hadn't even seen that page myself. Probably because the title ("Baremetal RISC-V Programs") didn't stick out to me as what I had been looking for.

The way I had solved the problem was by copying the contents of the riscv-tests/benchmarks Makefile, as well as the env folder for includes - https://github.com/ucb-bar/esp-tests/blob/7c3dfd3205fa90104db06346e797a8eeea149207/benchmarks/Makefile

All the command-line arguments that they use is black magic to me, but the resulting executable takes even less time to simulate.

Simulating executable with the riscv-tests black magic:

drak@JDESK:~/chipyard/sims/verilator$ ./simulator-chipyard-RocketAndCustomConfig-debug -c ~/chipyard/generators/customtensor/benchmarks/temp.riscv
This emulator compiled with JTAG Remote Bitbang client. To enable, use +jtag_rbb_enable=1.
Listening on port 33163
[UART] UART0 is here (stdin/stdout).
Hello, World!
*** PASSED *** Completed after 90774 cycles

Simulating executable with the suggested command line arguments:

drak@JDESK:~/chipyard/sims/verilator$ ./simulator-chipyard-RocketAndCustomConfig-debug -c ~/chipyard/generators/customtensor/benchmarks/temp/hello.riscv
This emulator compiled with JTAG Remote Bitbang client. To enable, use +jtag_rbb_enable=1.
Listening on port 41369
[UART] UART0 is here (stdin/stdout).
Hello, World!
*** PASSED *** Completed after 117518 cycles

Here's my benchmarks folder with that modified Makefile: https://github.com/JL102/rowan-riscv-tensor/tree/main/benchmarks

@scottzhang
Copy link

well. Without linux or any other os is Bare Metal. And chipyard doesn't ship with an booting/running linux, it is baremetal.

@JL102
Copy link
Contributor Author

JL102 commented Feb 2, 2022

Closing as the question has been answered. (Thanks again)

@JL102 JL102 closed this as completed Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants