From 6a571f710ee7f8a6a107176ac5b7dd8a9f14cf0b Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Mon, 13 Apr 2020 19:00:56 -0700 Subject: Handle misaligned memories by aligning them, rather than erroring Resolves #442 --- spike_main/spike.cc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/spike_main/spike.cc b/spike_main/spike.cc index 31c0cd7..9056f31 100644 --- a/spike_main/spike.cc +++ b/spike_main/spike.cc @@ -112,8 +112,23 @@ static std::vector> make_mems(const char* arg) if (!*p || *p != ':') help(); auto size = strtoull(p + 1, &p, 0); - if ((size | base) % PGSIZE != 0) + + // page-align base and size + auto base0 = base, size0 = size; + size += base0 % PGSIZE; + base -= base0 % PGSIZE; + if (size % PGSIZE != 0) + size += PGSIZE - size % PGSIZE; + + if (base + size < base) help(); + + if (size != size0) { + fprintf(stderr, "Warning: the memory at [0x%llX, 0x%llX] has been realigned\n" + "to the %ld KiB page size: [0x%llX, 0x%llX]\n", + base0, base0 + size0 - 1, PGSIZE / 1024, base, base + size - 1); + } + res.push_back(std::make_pair(reg_t(base), new mem_t(size))); if (!*p) break; -- cgit v1.1