@@ -191,6 +191,31 @@ 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+ return mem_cfg_t (base, size);
217+ }
218+
194219static std::vector<mem_cfg_t > parse_mem_layout (const char * arg)
195220{
196221 std::vector<mem_cfg_t > res;
@@ -200,9 +225,9 @@ static std::vector<mem_cfg_t> parse_mem_layout(const char* arg)
200225 auto mb = strtoull (arg, &p, 0 );
201226 if (*p == 0 ) {
202227 reg_t size = reg_t (mb) << 20 ;
203- if (size != ( size_t )size )
204- throw std::runtime_error (" Size would overflow size_t " );
205- res.push_back (mem_cfg_t ( reg_t ( DRAM_BASE) , size));
228+ if (( size >> 20 ) != mb )
229+ throw std::runtime_error (" Memory size too large " );
230+ res.push_back (create_mem_region ( DRAM_BASE, size));
206231 return res;
207232 }
208233
@@ -213,42 +238,7 @@ static std::vector<mem_cfg_t> parse_mem_layout(const char* arg)
213238 help ();
214239 auto size = strtoull (p + 1 , &p, 0 );
215240
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);
241+ res.push_back (create_mem_region (base, size));
252242
253243 if (!*p)
254244 break ;
0 commit comments