daily update
[external/binutils.git] / sim / common / sim-memopt.c
index a8263b1..89a08bc 100644 (file)
@@ -1,22 +1,24 @@
 /* Simulator memory option handling.
-   Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+   Copyright (C) 1996-1999, 2007, 2008, 2009, 2010, 2011
+   Free Software Foundation, Inc.
    Contributed by Cygnus Support.
 
 This file is part of GDB, the GNU debugger.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "cconfig.h"
 
 #include "sim-main.h"
 #include "sim-assert.h"
@@ -32,6 +34,28 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+/* Memory fill byte. */
+static unsigned8 fill_byte_value;
+static int fill_byte_flag = 0;
+
+/* Memory mapping; see OPTION_MEMORY_MAPFILE. */
+static int mmap_next_fd = -1;
 
 /* Memory command line options. */
 
@@ -42,6 +66,9 @@ enum {
   OPTION_MEMORY_INFO,
   OPTION_MEMORY_ALIAS,
   OPTION_MEMORY_CLEAR,
+  OPTION_MEMORY_FILL,
+  OPTION_MEMORY_MAPFILE,
+  OPTION_MAP_INFO
 };
 
 static DECLARE_OPTION_HANDLER (memory_option_handler);
@@ -64,12 +91,22 @@ static const OPTION memory_options[] =
       memory_option_handler },
 
   { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
-      '\0', "SIZE", "Add memory at address zero",
+      '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
+     "Add memory at address zero", memory_option_handler },
+
+  { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
+      '\0', "VALUE", "Fill subsequently added memory regions",
       memory_option_handler },
 
   { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
-      '\0', NULL, "Clear all memory regions",
+      '\0', NULL, "Clear subsequently added memory regions",
+      memory_option_handler },
+
+#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
+  { {"memory-mapfile", required_argument, NULL, OPTION_MEMORY_MAPFILE },
+      '\0', "FILE", "Memory-map next memory region from file",
       memory_option_handler },
+#endif
 
   { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
       '\0', NULL, "List configurable memory regions",
@@ -77,6 +114,9 @@ static const OPTION memory_options[] =
   { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
       '\0', NULL, NULL,
       memory_option_handler },
+  { {"map-info", no_argument, NULL, OPTION_MAP_INFO },
+      '\0', NULL, "List mapped regions",
+      memory_option_handler },
 
   { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
 };
@@ -92,9 +132,107 @@ do_memopt_add (SIM_DESC sd,
               sim_memopt **entry,
               void *buffer)
 {
-  sim_core_attach (sd, NULL,
-                  level, access_read_write_exec, space,
-                  addr, nr_bytes, modulo, NULL, buffer);
+  void *fill_buffer;
+  unsigned fill_length;
+  void *free_buffer;
+  unsigned long free_length;
+
+  if (buffer != NULL)
+    {
+      /* Buffer already given.  sim_memory_uninstall will free it. */
+      sim_core_attach (sd, NULL,
+                      level, access_read_write_exec, space,
+                      addr, nr_bytes, modulo, NULL, buffer);
+
+      free_buffer = buffer;
+      free_length = 0;
+      fill_buffer = buffer;
+      fill_length = (modulo == 0) ? nr_bytes : modulo;
+    }
+  else
+    {
+      /* Allocate new well-aligned buffer, just as sim_core_attach(). */
+      void *aligned_buffer;
+      int padding = (addr % sizeof (unsigned64));
+      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_length = bytes;
+
+#ifdef HAVE_MMAP
+      /* Memory map or malloc(). */
+      if (mmap_next_fd >= 0)
+       {
+         /* Some kernels will SIGBUS the application if mmap'd file
+            is not large enough.  */
+         if (s.st_size < bytes)
+           {
+             sim_io_error (sd,
+                           "Error, cannot confirm that mmap file is large enough "
+                           "(>= %ld bytes)\n", bytes);
+           }
+
+         free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
+         if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
+           {
+             sim_io_error (sd, "Error, cannot mmap file (%s).\n",
+                           strerror (errno));
+           }
+       }
+#endif
+
+      /* Need heap allocation? */
+      if (free_buffer == NULL)
+       {
+         /* If filling with non-zero value, do not use clearing allocator. */
+         if (fill_byte_flag && fill_byte_value != 0)
+           free_buffer = xmalloc (bytes); /* don't clear */
+         else
+           free_buffer = zalloc (bytes); /* clear */
+       }
+
+      aligned_buffer = (char*) free_buffer + padding;
+
+      sim_core_attach (sd, NULL,
+                      level, access_read_write_exec, space,
+                      addr, nr_bytes, modulo, NULL, aligned_buffer);
+
+      fill_buffer = aligned_buffer;
+      fill_length = (modulo == 0) ? nr_bytes : modulo;
+
+      /* If we just used a clearing allocator, and are about to fill with
+         zero, truncate the redundant fill operation. */
+
+      if (fill_byte_flag && fill_byte_value == 0)
+         fill_length = 1; /* avoid boundary length=0 case */
+    }
+
+  if (fill_byte_flag)
+    {
+      ASSERT (fill_buffer != 0);
+      memset ((char*) fill_buffer, fill_byte_value, fill_length);
+    }
+
   while ((*entry) != NULL)
     entry = &(*entry)->next;
   (*entry) = ZALLOC (sim_memopt);
@@ -103,7 +241,18 @@ do_memopt_add (SIM_DESC sd,
   (*entry)->addr = addr;
   (*entry)->nr_bytes = nr_bytes;
   (*entry)->modulo = modulo;
-  (*entry)->buffer = buffer;
+  (*entry)->buffer = free_buffer;
+
+  /* Record memory unmapping info.  */
+  if (mmap_next_fd >= 0)
+    {
+      (*entry)->munmap_length = free_length;
+      close (mmap_next_fd);
+      mmap_next_fd = -1;
+    }
+  else
+    (*entry)->munmap_length = 0;
+
   return (*entry);
 }
 
@@ -128,7 +277,15 @@ do_memopt_delete (SIM_DESC sd,
     }
   /* delete any buffer */
   if ((*entry)->buffer != NULL)
-    zfree ((*entry)->buffer);
+    {
+#ifdef HAVE_MUNMAP
+      if ((*entry)->munmap_length > 0)
+       munmap ((*entry)->buffer, (*entry)->munmap_length);
+      else
+#endif
+       free ((*entry)->buffer);
+    }
+
   /* delete it and its aliases */
   alias = *entry;
   *entry = (*entry)->next;
@@ -137,7 +294,7 @@ do_memopt_delete (SIM_DESC sd,
       sim_memopt *dead = alias;
       alias = alias->alias;
       sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
-      zfree (dead);
+      free (dead);
     }
   return SIM_RC_OK;
 }
@@ -148,15 +305,39 @@ parse_size (char *chp,
            address_word *nr_bytes,
            unsigned *modulo)
 {
-  /* <nr_bytes> [ "%" <modulo> ] */
+  /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
   *nr_bytes = strtoul (chp, &chp, 0);
-  if (*chp == '%')
+  switch (*chp)
     {
+    case '%':
       *modulo = strtoul (chp + 1, &chp, 0);
+      break;
+    case 'g': case 'G': /* Gigabyte suffix.  */
+      *nr_bytes <<= 10;
+      /* Fall through.  */
+    case 'm': case 'M': /* Megabyte suffix.  */
+      *nr_bytes <<= 10;
+      /* Fall through.  */
+    case 'k': case 'K': /* Kilobyte suffix.  */
+      *nr_bytes <<= 10;
+      /* Check for a modulo specifier after the suffix.  */
+      ++ chp;
+      if (* chp == 'b' || * chp == 'B')
+       ++ chp;
+      if (* chp == '%')
+       *modulo = strtoul (chp + 1, &chp, 0);
+      break;
     }
   return chp;
 }
 
+static char *
+parse_ulong_value (char *chp,
+                    unsigned long *value)
+{
+  *value = strtoul (chp, &chp, 0);
+  return chp;
+}
 
 static char *
 parse_addr (char *chp,
@@ -165,11 +346,11 @@ parse_addr (char *chp,
            address_word *addr)
 {
   /* [ <space> ": " ] <addr> [ "@" <level> ] */
-  *addr = strtoul (chp, &chp, 0);
+  *addr = (unsigned long) strtoul (chp, &chp, 0);
   if (*chp == ':')
     {
       *space = *addr;
-      *addr = strtoul (chp + 1, &chp, 0);
+      *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
     }
   if (*chp == '@')
     {
@@ -204,7 +385,7 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
          parse_addr (arg, &level, &space, &addr);
          return do_memopt_delete (sd, level, space, addr);
        }
-    
+
     case OPTION_MEMORY_REGION:
       {
        char *chp = arg;
@@ -217,10 +398,15 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
        chp = parse_addr (chp, &level, &space, &addr);
        if (*chp != ',')
          {
-           sim_io_eprintf (sd, "Missing size for memory-region\n");
-           return SIM_RC_FAIL;
+           /* let the file autosize */
+           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 */
        if (*chp == ',')
          modulo = strtoul (chp + 1, &chp, 0);
@@ -250,7 +436,7 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
        /* try to attach/insert the main record */
        entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
                               &STATE_MEMOPT (sd),
-                              zalloc (modulo ? modulo : nr_bytes));
+                              NULL);
        /* now attach all the aliases */
        while (*chp == ',')
          {
@@ -281,29 +467,46 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
 
     case OPTION_MEMORY_CLEAR:
       {
-       sim_memopt *entry;
-       for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
+       fill_byte_value = (unsigned8) 0;
+       fill_byte_flag = 1;
+       return SIM_RC_OK;
+       break;
+      }
+
+    case OPTION_MEMORY_FILL:
+      {
+       unsigned long fill_value;
+       parse_ulong_value (arg, &fill_value);
+       if (fill_value > 255)
          {
-           sim_memopt *alias;
-           for (alias = entry; alias != NULL; alias = alias->next)
-             {
-               unsigned8 zero = 0;
-               address_word nr_bytes;
-               if (alias->modulo != 0)
-                 nr_bytes = alias->modulo;
-               else
-                 nr_bytes = alias->nr_bytes;
-               sim_core_write_buffer (sd, NULL, sim_core_write_map,
-                                      &zero,
-                                      alias->addr + nr_bytes,
-                                      sizeof (zero));
-               
-             }
+           sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
+           return SIM_RC_FAIL;
          }
+       fill_byte_value = (unsigned8) fill_value;
+       fill_byte_flag = 1;
        return SIM_RC_OK;
        break;
       }
 
+    case OPTION_MEMORY_MAPFILE:
+      {
+       if (mmap_next_fd >= 0)
+         {
+           sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
+           return SIM_RC_FAIL;
+         }
+
+       mmap_next_fd = open (arg, O_RDWR);
+       if (mmap_next_fd < 0)
+         {
+           sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
+                           arg, strerror (errno));
+           return SIM_RC_FAIL;
+         }
+
+       return SIM_RC_OK;
+      }
+
     case OPTION_MEMORY_INFO:
       {
        sim_memopt *entry;
@@ -341,6 +544,45 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
        break;
       }
 
+    case OPTION_MAP_INFO:
+      {
+       sim_core *memory = STATE_CORE (sd);
+       unsigned nr_map;
+
+       for (nr_map = 0; nr_map < nr_maps; ++nr_map)
+         {
+           sim_core_map *map = &memory->common.map[nr_map];
+           sim_core_mapping *mapping = map->first;
+
+           if (!mapping)
+             continue;
+
+           sim_io_printf (sd, "%s maps:\n", map_to_str (nr_map));
+           do
+             {
+               unsigned modulo;
+
+               sim_io_printf (sd, " map ");
+               if (mapping->space != 0)
+                 sim_io_printf (sd, "0x%x:", mapping->space);
+               sim_io_printf (sd, "0x%08lx", (long) mapping->base);
+               if (mapping->level != 0)
+                 sim_io_printf (sd, "@0x%x", mapping->level);
+               sim_io_printf (sd, ",0x%lx", (long) mapping->nr_bytes);
+               modulo = mapping->mask + 1;
+               if (modulo != 0)
+                 sim_io_printf (sd, "%%0x%x", modulo);
+               sim_io_printf (sd, "\n");
+
+               mapping = mapping->next;
+             }
+           while (mapping);
+         }
+
+       return SIM_RC_OK;
+       break;
+      }
+
     default:
       sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
       return SIM_RC_FAIL;
@@ -375,13 +617,46 @@ sim_memopt_install (SIM_DESC sd)
 static void
 sim_memory_uninstall (SIM_DESC sd)
 {
-  /* FIXME: free buffers, etc. */
+  sim_memopt **entry = &STATE_MEMOPT (sd);
+  sim_memopt *alias;
+
+  while ((*entry) != NULL)
+    {
+      /* delete any buffer */
+      if ((*entry)->buffer != NULL)
+       {
+#ifdef HAVE_MUNMAP
+         if ((*entry)->munmap_length > 0)
+           munmap ((*entry)->buffer, (*entry)->munmap_length);
+         else
+#endif
+           free ((*entry)->buffer);
+       }
+
+      /* delete it and its aliases */
+      alias = *entry;
+
+      /* next victim */
+      *entry = (*entry)->next;
+
+      while (alias != NULL)
+       {
+         sim_memopt *dead = alias;
+         alias = alias->alias;
+         sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
+         free (dead);
+       }
+    }
 }
 
 
 static SIM_RC
 sim_memory_init (SIM_DESC sd)
 {
-  /* FIXME: anything needed? */
+  /* Reinitialize option modifier flags, in case they were left
+     over from a previous sim startup event.  */
+  fill_byte_flag = 0;
+  mmap_next_fd = -1;
+
   return SIM_RC_OK;
 }