1 /* Simulator memory option handling.
2 Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "sim-assert.h"
24 #include "sim-options.h"
42 #ifdef HAVE_SYS_MMAN_H
45 #ifdef HAVE_SYS_STAT_H
52 /* Memory fill byte. */
53 static unsigned8 fill_byte_value;
54 static int fill_byte_flag = 0;
56 /* Memory mapping; see OPTION_MEMORY_MAPFILE. */
57 static int mmap_next_fd = -1;
59 /* Memory command line options. */
62 OPTION_MEMORY_DELETE = OPTION_START,
69 OPTION_MEMORY_MAPFILE,
73 static DECLARE_OPTION_HANDLER (memory_option_handler);
75 static const OPTION memory_options[] =
77 { {"memory-delete", required_argument, NULL, OPTION_MEMORY_DELETE },
78 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
79 memory_option_handler },
80 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
81 '\0', "ADDRESS", NULL,
82 memory_option_handler },
84 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
85 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
86 memory_option_handler },
88 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
89 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
90 memory_option_handler },
92 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
93 '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
94 "Add memory at address zero", memory_option_handler },
96 { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
97 '\0', "VALUE", "Fill subsequently added memory regions",
98 memory_option_handler },
100 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
101 '\0', NULL, "Clear subsequently added memory regions",
102 memory_option_handler },
104 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
105 { {"memory-mapfile", required_argument, NULL, OPTION_MEMORY_MAPFILE },
106 '\0', "FILE", "Memory-map next memory region from file",
107 memory_option_handler },
110 { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
111 '\0', NULL, "List configurable memory regions",
112 memory_option_handler },
113 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
115 memory_option_handler },
116 { {"map-info", no_argument, NULL, OPTION_MAP_INFO },
117 '\0', NULL, "List mapped regions",
118 memory_option_handler },
120 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
125 do_memopt_add (SIM_DESC sd,
129 address_word nr_bytes,
135 unsigned fill_length;
137 unsigned long free_length;
141 /* Buffer already given. sim_memory_uninstall will free it. */
142 sim_core_attach (sd, NULL,
143 level, access_read_write_exec, space,
144 addr, nr_bytes, modulo, NULL, buffer);
146 free_buffer = buffer;
148 fill_buffer = buffer;
149 fill_length = (modulo == 0) ? nr_bytes : modulo;
153 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
154 void *aligned_buffer;
155 int padding = (addr % sizeof (unsigned64));
161 if (mmap_next_fd >= 0)
163 /* Check that given file is big enough. */
164 int rc = fstat (mmap_next_fd, &s);
167 sim_io_error (sd, "Error, unable to stat file: %s\n",
170 /* Autosize the mapping to the file length. */
172 nr_bytes = s.st_size;
176 bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
182 /* Memory map or malloc(). */
183 if (mmap_next_fd >= 0)
185 /* Some kernels will SIGBUS the application if mmap'd file
186 is not large enough. */
187 if (s.st_size < bytes)
190 "Error, cannot confirm that mmap file is large enough "
191 "(>= %ld bytes)\n", bytes);
194 free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
195 if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
197 sim_io_error (sd, "Error, cannot mmap file (%s).\n",
203 /* Need heap allocation? */
204 if (free_buffer == NULL)
206 /* If filling with non-zero value, do not use clearing allocator. */
207 if (fill_byte_flag && fill_byte_value != 0)
208 free_buffer = xmalloc (bytes); /* don't clear */
210 free_buffer = zalloc (bytes); /* clear */
213 aligned_buffer = (char*) free_buffer + padding;
215 sim_core_attach (sd, NULL,
216 level, access_read_write_exec, space,
217 addr, nr_bytes, modulo, NULL, aligned_buffer);
219 fill_buffer = aligned_buffer;
220 fill_length = (modulo == 0) ? nr_bytes : modulo;
222 /* If we just used a clearing allocator, and are about to fill with
223 zero, truncate the redundant fill operation. */
225 if (fill_byte_flag && fill_byte_value == 0)
226 fill_length = 1; /* avoid boundary length=0 case */
231 ASSERT (fill_buffer != 0);
232 memset ((char*) fill_buffer, fill_byte_value, fill_length);
235 while ((*entry) != NULL)
236 entry = &(*entry)->next;
237 (*entry) = ZALLOC (sim_memopt);
238 (*entry)->level = level;
239 (*entry)->space = space;
240 (*entry)->addr = addr;
241 (*entry)->nr_bytes = nr_bytes;
242 (*entry)->modulo = modulo;
243 (*entry)->buffer = free_buffer;
245 /* Record memory unmapping info. */
246 if (mmap_next_fd >= 0)
248 (*entry)->munmap_length = free_length;
249 close (mmap_next_fd);
253 (*entry)->munmap_length = 0;
259 do_memopt_delete (SIM_DESC sd,
264 sim_memopt **entry = &STATE_MEMOPT (sd);
266 while ((*entry) != NULL
267 && ((*entry)->level != level
268 || (*entry)->space != space
269 || (*entry)->addr != addr))
270 entry = &(*entry)->next;
271 if ((*entry) == NULL)
273 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
277 /* delete any buffer */
278 if ((*entry)->buffer != NULL)
281 if ((*entry)->munmap_length > 0)
282 munmap ((*entry)->buffer, (*entry)->munmap_length);
285 free ((*entry)->buffer);
288 /* delete it and its aliases */
290 *entry = (*entry)->next;
291 while (alias != NULL)
293 sim_memopt *dead = alias;
294 alias = alias->alias;
295 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
303 parse_size (char *chp,
304 address_word *nr_bytes,
307 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
308 *nr_bytes = strtoul (chp, &chp, 0);
312 *modulo = strtoul (chp + 1, &chp, 0);
314 case 'g': case 'G': /* Gigabyte suffix. */
317 case 'm': case 'M': /* Megabyte suffix. */
320 case 'k': case 'K': /* Kilobyte suffix. */
322 /* Check for a modulo specifier after the suffix. */
324 if (* chp == 'b' || * chp == 'B')
327 *modulo = strtoul (chp + 1, &chp, 0);
334 parse_ulong_value (char *chp,
335 unsigned long *value)
337 *value = strtoul (chp, &chp, 0);
342 parse_addr (char *chp,
347 /* [ <space> ": " ] <addr> [ "@" <level> ] */
348 *addr = (unsigned long) strtoul (chp, &chp, 0);
352 *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
356 *level = strtoul (chp + 1, &chp, 0);
363 memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
364 char *arg, int is_command)
369 case OPTION_MEMORY_DELETE:
370 if (strcasecmp (arg, "all") == 0)
372 while (STATE_MEMOPT (sd) != NULL)
373 do_memopt_delete (sd,
374 STATE_MEMOPT (sd)->level,
375 STATE_MEMOPT (sd)->space,
376 STATE_MEMOPT (sd)->addr);
383 address_word addr = 0;
384 parse_addr (arg, &level, &space, &addr);
385 return do_memopt_delete (sd, level, space, addr);
388 case OPTION_MEMORY_REGION:
393 address_word addr = 0;
394 address_word nr_bytes = 0;
396 /* parse the arguments */
397 chp = parse_addr (chp, &level, &space, &addr);
400 /* let the file autosize */
401 if (mmap_next_fd == -1)
403 sim_io_eprintf (sd, "Missing size for memory-region\n");
408 chp = parse_size (chp + 1, &nr_bytes, &modulo);
411 modulo = strtoul (chp + 1, &chp, 0);
412 /* try to attach/insert it */
413 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
414 &STATE_MEMOPT (sd), NULL);
418 case OPTION_MEMORY_ALIAS:
423 address_word addr = 0;
424 address_word nr_bytes = 0;
427 /* parse the arguments */
428 chp = parse_addr (chp, &level, &space, &addr);
431 sim_io_eprintf (sd, "Missing size for memory-region\n");
434 chp = parse_size (chp + 1, &nr_bytes, &modulo);
435 /* try to attach/insert the main record */
436 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
439 /* now attach all the aliases */
444 address_word a_addr = addr;
445 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
446 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
447 &entry->alias, entry->buffer);
452 case OPTION_MEMORY_SIZE:
456 address_word addr = 0;
457 address_word nr_bytes = 0;
459 /* parse the arguments */
460 parse_size (arg, &nr_bytes, &modulo);
461 /* try to attach/insert it */
462 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
463 &STATE_MEMOPT (sd), NULL);
467 case OPTION_MEMORY_CLEAR:
469 fill_byte_value = (unsigned8) 0;
475 case OPTION_MEMORY_FILL:
477 unsigned long fill_value;
478 parse_ulong_value (arg, &fill_value);
479 if (fill_value > 255)
481 sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
484 fill_byte_value = (unsigned8) fill_value;
490 case OPTION_MEMORY_MAPFILE:
492 if (mmap_next_fd >= 0)
494 sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
498 mmap_next_fd = open (arg, O_RDWR);
499 if (mmap_next_fd < 0)
501 sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
502 arg, strerror (errno));
509 case OPTION_MEMORY_INFO:
512 sim_io_printf (sd, "Memory maps:\n");
513 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
516 sim_io_printf (sd, " memory");
517 if (entry->alias == NULL)
518 sim_io_printf (sd, " region ");
520 sim_io_printf (sd, " alias ");
521 if (entry->space != 0)
522 sim_io_printf (sd, "0x%lx:", (long) entry->space);
523 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
524 if (entry->level != 0)
525 sim_io_printf (sd, "@0x%lx", (long) entry->level);
526 sim_io_printf (sd, ",0x%lx",
527 (long) entry->nr_bytes);
528 if (entry->modulo != 0)
529 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
530 for (alias = entry->alias;
534 if (alias->space != 0)
535 sim_io_printf (sd, "0x%lx:", (long) alias->space);
536 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
537 if (alias->level != 0)
538 sim_io_printf (sd, "@0x%lx", (long) alias->level);
540 sim_io_printf (sd, "\n");
546 case OPTION_MAP_INFO:
548 sim_core *memory = STATE_CORE (sd);
551 for (nr_map = 0; nr_map < nr_maps; ++nr_map)
553 sim_core_map *map = &memory->common.map[nr_map];
554 sim_core_mapping *mapping = map->first;
559 sim_io_printf (sd, "%s maps:\n", map_to_str (nr_map));
564 sim_io_printf (sd, " map ");
565 if (mapping->space != 0)
566 sim_io_printf (sd, "0x%x:", mapping->space);
567 sim_io_printf (sd, "0x%08lx", (long) mapping->base);
568 if (mapping->level != 0)
569 sim_io_printf (sd, "@0x%x", mapping->level);
570 sim_io_printf (sd, ",0x%lx", (long) mapping->nr_bytes);
571 modulo = mapping->mask + 1;
573 sim_io_printf (sd, "%%0x%x", modulo);
574 sim_io_printf (sd, "\n");
576 mapping = mapping->next;
586 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
595 /* "memory" module install handler.
597 This is called via sim_module_install to install the "memory" subsystem
598 into the simulator. */
600 static MODULE_INIT_FN sim_memory_init;
601 static MODULE_UNINSTALL_FN sim_memory_uninstall;
604 sim_memopt_install (SIM_DESC sd)
606 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
607 sim_add_option_table (sd, NULL, memory_options);
608 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
609 sim_module_add_init_fn (sd, sim_memory_init);
614 /* Uninstall the "memory" subsystem from the simulator. */
617 sim_memory_uninstall (SIM_DESC sd)
619 sim_memopt **entry = &STATE_MEMOPT (sd);
622 while ((*entry) != NULL)
624 /* delete any buffer */
625 if ((*entry)->buffer != NULL)
628 if ((*entry)->munmap_length > 0)
629 munmap ((*entry)->buffer, (*entry)->munmap_length);
632 free ((*entry)->buffer);
635 /* delete it and its aliases */
639 *entry = (*entry)->next;
641 while (alias != NULL)
643 sim_memopt *dead = alias;
644 alias = alias->alias;
645 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
653 sim_memory_init (SIM_DESC sd)
655 /* Reinitialize option modifier flags, in case they were left
656 over from a previous sim startup event. */