Skip to content

Commit 1b33b54

Browse files
committed
Factor out create_mem_region from parse_mem_layout
1 parent 0a2c3b6 commit 1b33b54

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

spike_main/spike.cc

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,46 @@ merge_overlapping_memory_regions(std::vector<mem_cfg_t> mems)
191191
return merged_mem;
192192
}
193193

194+
static mem_cfg_t create_mem_region(unsigned long long base, unsigned long long size)
195+
{
196+
// page-align base and size
197+
auto base0 = base, size0 = size;
198+
size += base0 % PGSIZE;
199+
base -= base0 % PGSIZE;
200+
if (size % PGSIZE != 0)
201+
size += PGSIZE - size % PGSIZE;
202+
203+
if (size != size0) {
204+
fprintf(stderr, "Warning: the memory at [0x%llX, 0x%llX] has been realigned\n"
205+
"to the %ld KiB page size: [0x%llX, 0x%llX]\n",
206+
base0, base0 + size0 - 1, long(PGSIZE / 1024), base, base + size - 1);
207+
}
208+
209+
if (!mem_cfg_t::check_if_supported(base, size)) {
210+
fprintf(stderr, "Unsupported memory region "
211+
"{base = 0x%llX, size = 0x%llX} specified\n",
212+
base, size);
213+
exit(EXIT_FAILURE);
214+
}
215+
216+
const unsigned long long max_allowed_pa = (1ull << MAX_PADDR_BITS) - 1ull;
217+
assert(max_allowed_pa <= std::numeric_limits<reg_t>::max());
218+
mem_cfg_t mem_region(base, size);
219+
if (mem_region.get_inclusive_end() > max_allowed_pa) {
220+
int bits_required = 64 - clz(mem_region.get_inclusive_end());
221+
fprintf(stderr, "Unsupported memory region "
222+
"{base = 0x%" PRIX64 ", size = 0x%" PRIX64 "} specified,"
223+
" which requires %d bits of physical address\n"
224+
" The largest accessible physical address "
225+
"is 0x%llX (defined by MAX_PADDR_BITS constant, which is %d)\n",
226+
mem_region.get_base(), mem_region.get_size(), bits_required,
227+
max_allowed_pa, MAX_PADDR_BITS);
228+
exit(EXIT_FAILURE);
229+
}
230+
231+
return mem_region;
232+
}
233+
194234
static std::vector<mem_cfg_t> parse_mem_layout(const char* arg)
195235
{
196236
std::vector<mem_cfg_t> res;
@@ -213,42 +253,7 @@ static std::vector<mem_cfg_t> parse_mem_layout(const char* arg)
213253
help();
214254
auto size = strtoull(p + 1, &p, 0);
215255

216-
// page-align base and size
217-
auto base0 = base, size0 = size;
218-
size += base0 % PGSIZE;
219-
base -= base0 % PGSIZE;
220-
if (size % PGSIZE != 0)
221-
size += PGSIZE - size % PGSIZE;
222-
223-
if (size != size0) {
224-
fprintf(stderr, "Warning: the memory at [0x%llX, 0x%llX] has been realigned\n"
225-
"to the %ld KiB page size: [0x%llX, 0x%llX]\n",
226-
base0, base0 + size0 - 1, long(PGSIZE / 1024), base, base + size - 1);
227-
}
228-
229-
if (!mem_cfg_t::check_if_supported(base, size)) {
230-
fprintf(stderr, "Unsupported memory region "
231-
"{base = 0x%llX, size = 0x%llX} specified\n",
232-
base, size);
233-
exit(EXIT_FAILURE);
234-
}
235-
236-
const unsigned long long max_allowed_pa = (1ull << MAX_PADDR_BITS) - 1ull;
237-
assert(max_allowed_pa <= std::numeric_limits<reg_t>::max());
238-
mem_cfg_t mem_region(base, size);
239-
if (mem_region.get_inclusive_end() > max_allowed_pa) {
240-
int bits_required = 64 - clz(mem_region.get_inclusive_end());
241-
fprintf(stderr, "Unsupported memory region "
242-
"{base = 0x%" PRIX64 ", size = 0x%" PRIX64 "} specified,"
243-
" which requires %d bits of physical address\n"
244-
" The largest accessible physical address "
245-
"is 0x%llX (defined by MAX_PADDR_BITS constant, which is %d)\n",
246-
mem_region.get_base(), mem_region.get_size(), bits_required,
247-
max_allowed_pa, MAX_PADDR_BITS);
248-
exit(EXIT_FAILURE);
249-
}
250-
251-
res.push_back(mem_region);
256+
res.push_back(create_mem_region(base, size));
252257

253258
if (!*p)
254259
break;

0 commit comments

Comments
 (0)