sim: allow memory maps to default to mapped files

I find it annoying when using --memory-mapfile that I also need to look
up and manually specify the file size to the following --memory-region
option.  So make a length of 0 in the following --memory-region trigger
an auto-sizing of the map to the length of the file being mapped.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
This commit is contained in:
Mike Frysinger 2011-01-11 17:58:56 +00:00
parent 9b20d036b6
commit 3143e5a930
2 changed files with 38 additions and 10 deletions

View File

@ -1,3 +1,11 @@
2011-01-11 Mike Frysinger <vapier@gentoo.org>
* sim-memopt.c (do_memopt_add): Set nr_bytes to s.st_size before
bytes has been calculated and when mmap_next_fd is valid and
nr_bytes is 0.
(memory_option_handler): Allow missing size when mmap_next_fd is
valid.
2011-01-10 Mike Frysinger <vapier@gentoo.org> 2011-01-10 Mike Frysinger <vapier@gentoo.org>
* aclocal.m4 (SIM_AC_OPTION_HARDWARE): Set $hardware to $2 when $2 is * aclocal.m4 (SIM_AC_OPTION_HARDWARE): Set $hardware to $2 when $2 is

View File

@ -154,7 +154,27 @@ do_memopt_add (SIM_DESC sd,
/* Allocate new well-aligned buffer, just as sim_core_attach(). */ /* Allocate new well-aligned buffer, just as sim_core_attach(). */
void *aligned_buffer; void *aligned_buffer;
int padding = (addr % sizeof (unsigned64)); int padding = (addr % sizeof (unsigned64));
unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding; unsigned long bytes;
#ifdef HAVE_MMAP
struct stat s;
if (mmap_next_fd >= 0)
{
/* Check that given file is big enough. */
int rc = fstat (mmap_next_fd, &s);
if (rc < 0)
sim_io_error (sd, "Error, unable to stat file: %s\n",
strerror (errno));
/* Autosize the mapping to the file length. */
if (nr_bytes == 0)
nr_bytes = s.st_size;
}
#endif
bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
free_buffer = NULL; free_buffer = NULL;
free_length = bytes; free_length = bytes;
@ -163,14 +183,9 @@ do_memopt_add (SIM_DESC sd,
/* Memory map or malloc(). */ /* Memory map or malloc(). */
if (mmap_next_fd >= 0) if (mmap_next_fd >= 0)
{ {
/* Check that given file is big enough. */
struct stat s;
int rc;
/* Some kernels will SIGBUS the application if mmap'd file /* Some kernels will SIGBUS the application if mmap'd file
is not large enough. */ is not large enough. */
rc = fstat (mmap_next_fd, &s); if (s.st_size < bytes)
if (rc < 0 || s.st_size < bytes)
{ {
sim_io_error (sd, sim_io_error (sd,
"Error, cannot confirm that mmap file is large enough " "Error, cannot confirm that mmap file is large enough "
@ -383,10 +398,15 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
chp = parse_addr (chp, &level, &space, &addr); chp = parse_addr (chp, &level, &space, &addr);
if (*chp != ',') if (*chp != ',')
{ {
sim_io_eprintf (sd, "Missing size for memory-region\n"); /* let the file autosize */
return SIM_RC_FAIL; if (mmap_next_fd == -1)
{
sim_io_eprintf (sd, "Missing size for memory-region\n");
return SIM_RC_FAIL;
}
} }
chp = parse_size (chp + 1, &nr_bytes, &modulo); else
chp = parse_size (chp + 1, &nr_bytes, &modulo);
/* old style */ /* old style */
if (*chp == ',') if (*chp == ',')
modulo = strtoul (chp + 1, &chp, 0); modulo = strtoul (chp + 1, &chp, 0);