From 90879d492001bbd63e0984f24d56182888e8043e Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Wed, 25 Mar 2020 16:41:33 -0700 Subject: [PATCH] util: Refactor the string->register packing function in the m5 utility. This change removes the responsibility for checking the number of arguments and handing the default of no string back into init_param and out of the function which packs strings into registers. It also renames the function to more closely match its purpose, and rewrites it to be a bit simpler and (IMHO) easier to follow. Importantly, rather than doing a hand implemented strcpy which would follow the endianness of the target/simulated platform, this change makes this function pack the registers explicitly in little endian byte order. This way on the consuming end in gem5, the initParam function doesn't have to care what the guest endianness is, it can just translate them from little endian to whatever the host endianness is (very likely also little endian). Change-Id: Ie9f79ecb8d4584c6e47a2793a31ccaa8c7c15986 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/27229 Reviewed-by: Bobby R. Bruce Maintainer: Gabe Black Tested-by: kokoro --- util/m5/src/m5.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/util/m5/src/m5.c b/util/m5/src/m5.c index 83174d07f..ae9068bd1 100644 --- a/util/m5/src/m5.c +++ b/util/m5/src/m5.c @@ -76,23 +76,24 @@ parse_int_args(int argc, char *argv[], uint64_t ints[], int len) } void -parse_str_args_to_regs(int argc, char *argv[], uint64_t regs[], int len) +pack_str_into_regs(const char *str, uint64_t regs[], int num_regs) { - if (argc > 1 || (argc > 0 && strlen(argv[0]) > len * sizeof(uint64_t))) - usage(); + const size_t RegSize = sizeof(regs[0]); + const size_t MaxLen = num_regs * RegSize; - int i; - for (i = 0; i < len; i++) - regs[i] = 0; + size_t len = strlen(str); - if (argc == 0) - return; + if (len > MaxLen) + usage(); + + memset(regs, 0, MaxLen); - int n; - for (n = 0, i = 0; i < len && n < strlen(argv[0]); n++) { - *((char *)(®s[i]) + (n % 8)) = argv[0][n]; - if ((n % 8) == 7) - i++; + while (len) { + for (int offset = 0; offset < RegSize && len; offset++, len--) { + int shift = offset * 8; + *regs |= (uint64_t)(uint8_t)*str++ << shift; + } + regs++; } } @@ -277,7 +278,7 @@ do_initparam(int argc, char *argv[]) usage(); uint64_t key_str[2]; - parse_str_args_to_regs(argc, argv, key_str, 2); + pack_str_into_regs(argc == 0 ? "" : argv[0], key_str, 2); uint64_t val = m5_init_param(key_str[0], key_str[1]); printf("%"PRIu64, val); } -- 2.30.2