Siqing Fu
Siqing Fu

Reputation: 73

How to use m5 in gem5-20 linking it with my own C++ program?

In gem5-20, I can build the m5 utility by using.

scons build/<arch>/out/m5

But actually I don’t know how to link M5 to my C++ code.

Some necessary operations are mentioned at the end of this document, but I hope to get more specific guidance. http://www.gem5.org/documentation/general_docs/m5ops/ Has anyone done it,please help me. Thanks! Best wishes!

Upvotes: 2

Views: 1838

Answers (1)

Ciro Santilli
Ciro Santilli

Reputation: 4073

On gem5 046645a4db646ec30cc36b0f5433114e8777dc44 I can do:

scons -C util/m5 build/x86/out/m5
gcc -static -I include -o main.out main.c util/m5/build/x86/out/libm5.a

with:

main.c

#include <gem5/m5ops.h>

int main(void) {
    m5_exit(0);
}

Or for ARM:

sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
scons -C util/m5 build/aarch64/out/m5
aarch64-linux-gnu-gcc -static -I include -o main.out main.c \
  util/m5/build/aarch64/out/libm5.a

An official example can also be found at: gem5-resources/src/simplem5_exit.c with instructions at on the README.

And here is one using the m5_exit_addr variant, which uses the memory version of the m5op instead of instruction, which can also be used from KVM for example: https://gem5-review.googlesource.com/c/public/gem5/+/31219/7

But in practice, I often just don't have the patience for this business, so I just misbehave and add raw assembly directly as shown here muahahaha e.g.:

#if defined(__x86_64__)

#define LKMC_M5OPS_CHECKPOINT __asm__ __volatile__ (".word 0x040F; .word 0x0043;" : : "D" (0), "S" (0) :)
#define LKMC_M5OPS_DUMPSTATS  __asm__ __volatile__ (".word 0x040F; .word 0x0041;" : : "D" (0), "S" (0) :)

#elif defined(__aarch64__)

#define LKMC_M5OPS_CHECKPOINT __asm__ __volatile__ ("mov x0, 0; mov x1, 0; .inst 0xFF000110 | (0x43 << 16);" : : : "x0", "x1")
#define LKMC_M5OPS_DUMPSTATS  __asm__ __volatile__ ("mov x0, 0; mov x1, 0; .inst 0xFF000110 | (0x41 << 16);" : : : "x0", "x1")

More general m5op information can also be found at: What are pseudo-instructions for in gem5?

Related:

Tested on Ubuntu 20.04.

Upvotes: 8

Related Questions