Run Sparc v9 64-bit executable on bare metal QEMU

441 Views Asked by At

I tried this recipe : How to run a bare metal ELF file on QEMU? to run bare metal AArch64 executables on QEMU (and it worked). I'd like to do the same for 64-bit SPARCv9 executables (preferably starting from C/C++, not assembly) - I tried the examples in this link : Run SPARC assembly in QEMU, but that one runs in user-mode Linux, does syscall translation to the host system, etc.

Example :

int main(int argc, char **argv) {
  volatile int a = 11, b = 13, c = 7, d = 5; 
  return a + b - c - d;
} 

How do I run this example as a bare metal Sparcv9 executable AND connect gdb to it ?

Any of the available 64-bit boards in qemu/hw/sparc64/ would be fine. Thank you.

2

There are 2 best solutions below

0
On

Basically it's similar to running an AArch64 bare-metal ELF:

qemu-system-sparc64 -nographic -kernel test64.elf -serial mon:stdio -s

or

qemu-system-sparc64 -nographic -bios test64.elf -serial mon:stdio -s

Both -kernel and -bios options in sun4u emulation may load ELF files, the difference is that in the first example the hardware is initialized by OpenBIOS, and in the second example you are running in a PROM right after the cold start. The -s option will make QEMU listen for an incoming connection from gdb on TCP port 1234.

In the second example you probably also would like to add the -S option in oder to make QEMU not to start the execution of your ELF file until you tell it to from gdb. In gdb you do the following:

(gdb) set architecture sparc:v9
The target architecture is assumed to be sparc:v9
(gdb)  target remote localhost:1234
3
On

I believe what you are after is the qemu-system-sparc64 command. Just something to note is that the general form for bare-metal emulation is qemu-system-{ARCH} and the general command for user mode emulation is qemu-{ARCH}.

An example from the qemu docs.

qemu-system-sparc64 \
  -drive file=hd.qcow2,if=none,id=hd \
  -device virtio-blk-pci,bus=pciB,drive=hd,bootindex=0

As for building your own c/c++ bare-metal executables, that's a relatively broad topic, but I'd recommend starting with GCC and newlib-nano (for libc).