[gem5-dev] Change in gem5/gem5[develop]: util: Allow overriding the magic address in the m5 utility.

2020-06-22 Thread Gabe Black (Gerrit) via gem5-dev
Gabe Black has submitted this change. (  
https://gem5-review.googlesource.com/c/public/gem5/+/27244 )


Change subject: util: Allow overriding the magic address in the m5 utility.
..

util: Allow overriding the magic address in the m5 utility.

This is useful in situations where the address is hard to know ahead of
time, for instance on ARM systems where the address map is hard to
predict.

The default address is now M5OP_ADDR, or 0 if that's not defined.

Change-Id: I3140e05b04365c1a76e52f8c3dc85f472c230ae4
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/27244
Maintainer: Gabe Black 
Tested-by: kokoro 
Reviewed-by: Giacomo Travaglini 
---
M util/m5/src/addr_call_type.c
M util/m5/src/addr_call_type.h
M util/m5/src/m5.c
M util/m5/src/m5_mmap.c
M util/m5/src/m5_mmap.h
5 files changed, 57 insertions(+), 8 deletions(-)

Approvals:
  Giacomo Travaglini: Looks good to me, approved
  Gabe Black: Looks good to me, approved
  kokoro: Regressions pass



diff --git a/util/m5/src/addr_call_type.c b/util/m5/src/addr_call_type.c
index cb269dc..0b3d1fc 100644
--- a/util/m5/src/addr_call_type.c
+++ b/util/m5/src/addr_call_type.c
@@ -28,6 +28,8 @@
 #include 

 #include "addr_call_type.h"
+#include "args.h"
+#include "m5_mmap.h"

 #define M5OP(name, func) __typeof__(name) M5OP_MERGE_TOKENS(name, _addr);
 M5OP_FOREACH
@@ -42,9 +44,40 @@
 int
 addr_call_type_detect(int *argc, char **argv[])
 {
-if (*argc > 0 && strcmp((*argv)[0], "--addr") == 0) {
+static const char *prefix = "--addr";
+const size_t prefix_len = strlen(prefix);
+uint64_t addr_override;
+
+// If the first argument starts with --addr...
+if (*argc > 0 && memcmp((*argv)[0], prefix, prefix_len) == 0) {
+char *argv0 = (*argv)[0];
 (*argc)--;
 (*argv)++;
+
+// If there's more text in this argument...
+if (strlen(argv0) != prefix_len) {
+// If it doesn't start with '=', it's malformed.
+if (argv0[prefix_len] != '=')
+return -1;
+// Attempt to extract an address after the '='.
+char *temp_argv[] = { [prefix_len + 1] };
+if (!parse_int_args(1, temp_argv, _override, 1))
+return -1;
+// If we found an address, use it to override m5op_addr.
+m5op_addr = addr_override;
+return 1;
+}
+// If an address override wasn't part of the first argument, check  
if

+// it's the second argument. If not, then there's no override.
+if (*argc > 0 && parse_int_args(1, *argv, _override, 1)) {
+m5op_addr = addr_override;
+(*argc)--;
+(*argv)++;
+return 1;
+}
+// If the default address was zero, an override is required.
+if (!m5op_addr)
+return -1;
 return 1;
 }
 return 0;
diff --git a/util/m5/src/addr_call_type.h b/util/m5/src/addr_call_type.h
index d1fbfa6..6dcdb5b 100644
--- a/util/m5/src/addr_call_type.h
+++ b/util/m5/src/addr_call_type.h
@@ -30,6 +30,7 @@

 #include "dispatch_table.h"

+// Returns 0 if not detected, 1 if detected successfully, and -1 on error.
 int addr_call_type_detect(int *argc, char **argv[]);
 DispatchTable *addr_call_type_init();

diff --git a/util/m5/src/m5.c b/util/m5/src/m5.c
index 11e7d60..644acd0 100644
--- a/util/m5/src/m5.c
+++ b/util/m5/src/m5.c
@@ -290,10 +290,16 @@
 fprintf(stderr, "\n");
 fprintf(stderr, "Call types:\n");
 #   if ENABLE_CT_addr
-fprintf(stderr, "--addr%s\n", DEFAULT_CT_addr ? " (default)" : "");
+fprintf(stderr, "--addr %s%s\n",
+#   if defined(M5OP_ADDR)
+"[address override]",
+#   else
+"",
+#   endif
+DEFAULT_CT_addr ? " (default)" : "");
 fprintf(stderr, "Use the address based invocation method.\n");
 #   if defined(M5OP_ADDR)
-fprintf(stderr, "The address is %#"PRIx64".\n",
+fprintf(stderr, "The default address is %#"PRIx64".\n",
 (uint64_t)M5OP_ADDR);
 #   endif
 #   endif
@@ -331,8 +337,12 @@
 }
 #   endif
 #   if ENABLE_CT_addr
-if (!dt && addr_call_type_detect(, )) {
-dt = addr_call_type_init();
+if (!dt) {
+int detect = addr_call_type_detect(, );
+if (detect < 0)
+usage();
+if (detect > 0)
+dt = addr_call_type_init();
 }
 #   endif
 #   if ENABLE_CT_semi
diff --git a/util/m5/src/m5_mmap.c b/util/m5/src/m5_mmap.c
index 79de59b..4a5aa0f 100644
--- a/util/m5/src/m5_mmap.c
+++ b/util/m5/src/m5_mmap.c
@@ -49,10 +49,14 @@

 void *m5_mem = NULL;

+#ifndef M5OP_ADDR
+#define M5OP_ADDR 0
+#endif
+uint64_t m5op_addr = M5OP_ADDR;
+
 void
 map_m5_mem()
 {
-#ifdef M5OP_ADDR
 int fd;

 fd = open("/dev/mem", O_RDWR | O_SYNC);
@@ -62,10 +66,9 @@
 }

 m5_mem = mmap(NULL, 0x1, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
-  

[gem5-dev] Change in gem5/gem5[develop]: util: Allow overriding the magic address in the m5 utility.

2020-03-27 Thread Gabe Black (Gerrit)
Gabe Black has uploaded this change for review. (  
https://gem5-review.googlesource.com/c/public/gem5/+/27244 )



Change subject: util: Allow overriding the magic address in the m5 utility.
..

util: Allow overriding the magic address in the m5 utility.

This is useful in situations where the address is hard to know ahead of
time, for instance on ARM systems where the address map is hard to
predict.

The default address is now M5OP_ADDR, or 0 if that's not defined.

Change-Id: I3140e05b04365c1a76e52f8c3dc85f472c230ae4
---
M util/m5/src/addr_call_type.c
M util/m5/src/m5.c
M util/m5/src/m5_mmap.c
M util/m5/src/m5_mmap.h
4 files changed, 44 insertions(+), 6 deletions(-)



diff --git a/util/m5/src/addr_call_type.c b/util/m5/src/addr_call_type.c
index cb269dc..5e803bd 100644
--- a/util/m5/src/addr_call_type.c
+++ b/util/m5/src/addr_call_type.c
@@ -28,6 +28,8 @@
 #include 

 #include "addr_call_type.h"
+#include "args.h"
+#include "m5_mmap.h"

 #define M5OP(name, func) __typeof__(name) M5OP_MERGE_TOKENS(name, _addr);
 M5OP_FOREACH
@@ -42,9 +44,36 @@
 int
 addr_call_type_detect(int *argc, char **argv[])
 {
-if (*argc > 0 && strcmp((*argv)[0], "--addr") == 0) {
+static const char *prefix = "--addr";
+size_t prefix_len = strlen(prefix);
+uint64_t addr_override;
+
+// If the first argument starts with --addr...
+if (*argc > 0 && memcmp((*argv)[0], prefix, prefix_len) == 0) {
+char *argv0 = (*argv)[0];
 (*argc)--;
 (*argv)++;
+
+// If there's more text in this argument...
+if (strlen(argv0) != prefix_len) {
+// If it doesn't start with '=', it's malformed.
+if (argv0[prefix_len] != '=')
+return -1;
+// Attempt to extract an address after the '='.
+char *temp_argv[] = { [prefix_len + 1] };
+if (!parse_int_args(1, temp_argv, _override, 1))
+return -1;
+// If we found an address, use it to override m5op_addr.
+m5op_addr = addr_override;
+return 1;
+}
+// If an address override wasn't part of the first argument, check  
if

+// it's the second argument. If not, then there's no override.
+if (*argc > 0 && parse_int_args(1, *argv, _override, 1)) {
+m5op_addr = addr_override;
+(*argc)--;
+(*argv)++;
+}
 return 1;
 }
 return 0;
diff --git a/util/m5/src/m5.c b/util/m5/src/m5.c
index 5597966..0665759 100644
--- a/util/m5/src/m5.c
+++ b/util/m5/src/m5.c
@@ -314,8 +314,12 @@
 }
 #   endif
 #   if ENABLE_CT_addr
-if (!dt && addr_call_type_detect(, )) {
-dt = addr_call_type_init();
+if (!dt) {
+int detect = addr_call_type_detect(, );
+if (detect < 0)
+usage();
+if (detect > 0)
+dt = addr_call_type_init();
 }
 #   endif
 #   if ENABLE_CT_semi
diff --git a/util/m5/src/m5_mmap.c b/util/m5/src/m5_mmap.c
index 79de59b..4a5aa0f 100644
--- a/util/m5/src/m5_mmap.c
+++ b/util/m5/src/m5_mmap.c
@@ -49,10 +49,14 @@

 void *m5_mem = NULL;

+#ifndef M5OP_ADDR
+#define M5OP_ADDR 0
+#endif
+uint64_t m5op_addr = M5OP_ADDR;
+
 void
 map_m5_mem()
 {
-#ifdef M5OP_ADDR
 int fd;

 fd = open("/dev/mem", O_RDWR | O_SYNC);
@@ -62,10 +66,9 @@
 }

 m5_mem = mmap(NULL, 0x1, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
-  M5OP_ADDR);
+  m5op_addr);
 if (!m5_mem) {
 perror("Can't mmap /dev/mem");
 exit(1);
 }
-#endif
 }
diff --git a/util/m5/src/m5_mmap.h b/util/m5/src/m5_mmap.h
index d32857f..552b400 100644
--- a/util/m5/src/m5_mmap.h
+++ b/util/m5/src/m5_mmap.h
@@ -42,9 +42,11 @@
 #define __UTIL_M5_MMAP_H__

 #include 
+#include 
 #include 

 extern void *m5_mem;
+extern uint64_t m5op_addr;

 void map_m5_mem();


--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/27244
To unsubscribe, or for help writing mail filters, visit  
https://gem5-review.googlesource.com/settings


Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I3140e05b04365c1a76e52f8c3dc85f472c230ae4
Gerrit-Change-Number: 27244
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black 
Gerrit-MessageType: newchange
___
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev