0

I'm working on a project based on Rocket-Chip tools. I made a simple baremetal program that works well on Spike (even with multiple cores ...etc).

The problem is that when I run it in the C++ Emulator, the latter stops in the first printf call.

My question is: Is it possible to call syscalls (like printf, putchar ..) from the C Emulator? Or is there any way to print the results of a program out of the simulation like fetching the data memory or something? (I struggled with that and I didn't find where it saves the data variables).

PS: The program is based on riscv-tests/benchmarks the syscalls are already defined there.

noureddine-as
  • 453
  • 4
  • 12

1 Answers1

1

Is it possible to call syscalls (like printf, putchar ..) from the C Emulator?

Yes these functions already defined here in code base

As per the code base, data address pointing by global external variable, from this address you need to read 4 index of 64bit data

extern volatile uint64_t tohost;
extern volatile uint64_t fromhost;

The printf(), putchar() implemented using this syscall

static uintptr_t syscall(uintptr_t which, uint64_t arg0, uint64_t arg1, uint64_t arg2)
{
  volatile uint64_t magic_mem[8] __attribute__((aligned(64)));
  magic_mem[0] = which;
  magic_mem[1] = arg0;
  magic_mem[2] = arg1;
  magic_mem[3] = arg2;
  __sync_synchronize();

  tohost = (uintptr_t)magic_mem;
  while (fromhost == 0)
    ;
  fromhost = 0;

  __sync_synchronize();
  return magic_mem[0];
}
ntshetty
  • 1,293
  • 9
  • 20
  • I know this. I made a little example here: https://github.com/noureddine-as/riscv-baremetal-DefaultConfig Executing this on the Emulator prints the hello message correctly. Then I made another one, but this time it gives me `*** FAILED *** (tohost = 1337) *** FAILED *** via dtm (code = 1337, seed 1523460801) after 208588 cycles` in the C++ Emulator even if it works well in Spike simulation ! Any idea? – noureddine-as Apr 11 '18 at 13:44
  • 1
    here `uint64_t magic_mem[8] __attribute__((aligned(64)));` array size is `8 * 8`, so may be it's overflow causing to fail after `208588` cycle – ntshetty Apr 12 '18 at 01:36
  • I resolved this. I remarked than if I put for example `printf("something");` without a \n it doesn't print in both cases with pk or baremetal. I didn't investigate the real source but I thing it's because printf is reimplemented in the syscalls and i don't use the Standard one. Moreover I guess that Spike still waits for the \n to print the text. Anyway it works now. What I would like to know @Thiru is how to debug programs using Spike and especially the C++ Emulator? – noureddine-as Apr 12 '18 at 07:11