1 /* Simulator memory option handling.
2 Copyright (C) 1996-1999, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
6 This file is part of GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "sim-assert.h"
25 #include "sim-options.h"
43 #ifdef HAVE_SYS_MMAN_H
46 #ifdef HAVE_SYS_STAT_H
53 /* Memory fill byte. */
54 static unsigned8 fill_byte_value;
55 static int fill_byte_flag = 0;
57 /* Memory mapping; see OPTION_MEMORY_MAPFILE. */
58 static int mmap_next_fd = -1;
60 /* Memory command line options. */
63 OPTION_MEMORY_DELETE = OPTION_START,
70 OPTION_MEMORY_MAPFILE,
74 static DECLARE_OPTION_HANDLER (memory_option_handler);
76 static const OPTION memory_options[] =
78 { {"memory-delete", required_argument, NULL, OPTION_MEMORY_DELETE },
79 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
80 memory_option_handler },
81 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
82 '\0', "ADDRESS", NULL,
83 memory_option_handler },
85 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
86 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
87 memory_option_handler },
89 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
90 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
91 memory_option_handler },
93 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
94 '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
95 "Add memory at address zero", memory_option_handler },
97 { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
98 '\0', "VALUE", "Fill subsequently added memory regions",
99 memory_option_handler },
101 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
102 '\0', NULL, "Clear subsequently added memory regions",
103 memory_option_handler },
105 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
106 { {"memory-mapfile", required_argument, NULL, OPTION_MEMORY_MAPFILE },
107 '\0', "FILE", "Memory-map next memory region from file",
108 memory_option_handler },
111 { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
112 '\0', NULL, "List configurable memory regions",
113 memory_option_handler },
114 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
116 memory_option_handler },
117 { {"map-info", no_argument, NULL, OPTION_MAP_INFO },
118 '\0', NULL, "List mapped regions",
119 memory_option_handler },
121 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
126 do_memopt_add (SIM_DESC sd,
130 address_word nr_bytes,
136 unsigned fill_length;
138 unsigned long free_length;
142 /* Buffer already given. sim_memory_uninstall will free it. */
143 sim_core_attach (sd, NULL,
144 level, access_read_write_exec, space,
145 addr, nr_bytes, modulo, NULL, buffer);
147 free_buffer = buffer;
149 fill_buffer = buffer;
150 fill_length = (modulo == 0) ? nr_bytes : modulo;
154 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
155 void *aligned_buffer;
156 int padding = (addr % sizeof (unsigned64));
162 if (mmap_next_fd >= 0)
164 /* Check that given file is big enough. */
165 int rc = fstat (mmap_next_fd, &s);
168 sim_io_error (sd, "Error, unable to stat file: %s\n",
171 /* Autosize the mapping to the file length. */
173 nr_bytes = s.st_size;
177 bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
183 /* Memory map or malloc(). */
184 if (mmap_next_fd >= 0)
186 /* Some kernels will SIGBUS the application if mmap'd file
187 is not large enough. */
188 if (s.st_size < bytes)
191 "Error, cannot confirm that mmap file is large enough "
192 "(>= %ld bytes)\n", bytes);
195 free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
196 if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
198 sim_io_error (sd, "Error, cannot mmap file (%s).\n",
204 /* Need heap allocation? */
205 if (free_buffer == NULL)
207 /* If filling with non-zero value, do not use clearing allocator. */
208 if (fill_byte_flag && fill_byte_value != 0)
209 free_buffer = xmalloc (bytes); /* don't clear */
211 free_buffer = zalloc (bytes); /* clear */
214 aligned_buffer = (char*) free_buffer + padding;
216 sim_core_attach (sd, NULL,
217 level, access_read_write_exec, space,
218 addr, nr_bytes, modulo, NULL, aligned_buffer);
220 fill_buffer = aligned_buffer;
221 fill_length = (modulo == 0) ? nr_bytes : modulo;
223 /* If we just used a clearing allocator, and are about to fill with
224 zero, truncate the redundant fill operation. */
226 if (fill_byte_flag && fill_byte_value == 0)
227 fill_length = 1; /* avoid boundary length=0 case */
232 ASSERT (fill_buffer != 0);
233 memset ((char*) fill_buffer, fill_byte_value, fill_length);
236 while ((*entry) != NULL)
237 entry = &(*entry)->next;
238 (*entry) = ZALLOC (sim_memopt);
239 (*entry)->level = level;
240 (*entry)->space = space;
241 (*entry)->addr = addr;
242 (*entry)->nr_bytes = nr_bytes;
243 (*entry)->modulo = modulo;
244 (*entry)->buffer = free_buffer;
246 /* Record memory unmapping info. */
247 if (mmap_next_fd >= 0)
249 (*entry)->munmap_length = free_length;
250 close (mmap_next_fd);
254 (*entry)->munmap_length = 0;
260 do_memopt_delete (SIM_DESC sd,
265 sim_memopt **entry = &STATE_MEMOPT (sd);
267 while ((*entry) != NULL
268 && ((*entry)->level != level
269 || (*entry)->space != space
270 || (*entry)->addr != addr))
271 entry = &(*entry)->next;
272 if ((*entry) == NULL)
274 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
278 /* delete any buffer */
279 if ((*entry)->buffer != NULL)
282 if ((*entry)->munmap_length > 0)
283 munmap ((*entry)->buffer, (*entry)->munmap_length);
286 free ((*entry)->buffer);
289 /* delete it and its aliases */
291 *entry = (*entry)->next;
292 while (alias != NULL)
294 sim_memopt *dead = alias;
295 alias = alias->alias;
296 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
304 parse_size (char *chp,
305 address_word *nr_bytes,
308 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
309 *nr_bytes = strtoul (chp, &chp, 0);
313 *modulo = strtoul (chp + 1, &chp, 0);
315 case 'g': case 'G': /* Gigabyte suffix. */
318 case 'm': case 'M': /* Megabyte suffix. */
321 case 'k': case 'K': /* Kilobyte suffix. */
323 /* Check for a modulo specifier after the suffix. */
325 if (* chp == 'b' || * chp == 'B')
328 *modulo = strtoul (chp + 1, &chp, 0);
335 parse_ulong_value (char *chp,
336 unsigned long *value)
338 *value = strtoul (chp, &chp, 0);
343 parse_addr (char *chp,
348 /* [ <space> ": " ] <addr> [ "@" <level> ] */
349 *addr = (unsigned long) strtoul (chp, &chp, 0);
353 *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
357 *level = strtoul (chp + 1, &chp, 0);
364 memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
365 char *arg, int is_command)
370 case OPTION_MEMORY_DELETE:
371 if (strcasecmp (arg, "all") == 0)
373 while (STATE_MEMOPT (sd) != NULL)
374 do_memopt_delete (sd,
375 STATE_MEMOPT (sd)->level,
376 STATE_MEMOPT (sd)->space,
377 STATE_MEMOPT (sd)->addr);
384 address_word addr = 0;
385 parse_addr (arg, &level, &space, &addr);
386 return do_memopt_delete (sd, level, space, addr);
389 case OPTION_MEMORY_REGION:
394 address_word addr = 0;
395 address_word nr_bytes = 0;
397 /* parse the arguments */
398 chp = parse_addr (chp, &level, &space, &addr);
401 /* let the file autosize */
402 if (mmap_next_fd == -1)
404 sim_io_eprintf (sd, "Missing size for memory-region\n");
409 chp = parse_size (chp + 1, &nr_bytes, &modulo);
412 modulo = strtoul (chp + 1, &chp, 0);
413 /* try to attach/insert it */
414 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
415 &STATE_MEMOPT (sd), NULL);
419 case OPTION_MEMORY_ALIAS:
424 address_word addr = 0;
425 address_word nr_bytes = 0;
428 /* parse the arguments */
429 chp = parse_addr (chp, &level, &space, &addr);
432 sim_io_eprintf (sd, "Missing size for memory-region\n");
435 chp = parse_size (chp + 1, &nr_bytes, &modulo);
436 /* try to attach/insert the main record */
437 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
440 /* now attach all the aliases */
445 address_word a_addr = addr;
446 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
447 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
448 &entry->alias, entry->buffer);
453 case OPTION_MEMORY_SIZE:
457 address_word addr = 0;
458 address_word nr_bytes = 0;
460 /* parse the arguments */
461 parse_size (arg, &nr_bytes, &modulo);
462 /* try to attach/insert it */
463 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
464 &STATE_MEMOPT (sd), NULL);
468 case OPTION_MEMORY_CLEAR:
470 fill_byte_value = (unsigned8) 0;
476 case OPTION_MEMORY_FILL:
478 unsigned long fill_value;
479 parse_ulong_value (arg, &fill_value);
480 if (fill_value > 255)
482 sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
485 fill_byte_value = (unsigned8) fill_value;
491 case OPTION_MEMORY_MAPFILE:
493 if (mmap_next_fd >= 0)
495 sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
499 mmap_next_fd = open (arg, O_RDWR);
500 if (mmap_next_fd < 0)
502 sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
503 arg, strerror (errno));
510 case OPTION_MEMORY_INFO:
513 sim_io_printf (sd, "Memory maps:\n");
514 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
517 sim_io_printf (sd, " memory");
518 if (entry->alias == NULL)
519 sim_io_printf (sd, " region ");
521 sim_io_printf (sd, " alias ");
522 if (entry->space != 0)
523 sim_io_printf (sd, "0x%lx:", (long) entry->space);
524 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
525 if (entry->level != 0)
526 sim_io_printf (sd, "@0x%lx", (long) entry->level);
527 sim_io_printf (sd, ",0x%lx",
528 (long) entry->nr_bytes);
529 if (entry->modulo != 0)
530 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
531 for (alias = entry->alias;
535 if (alias->space != 0)
536 sim_io_printf (sd, "0x%lx:", (long) alias->space);
537 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
538 if (alias->level != 0)
539 sim_io_printf (sd, "@0x%lx", (long) alias->level);
541 sim_io_printf (sd, "\n");
547 case OPTION_MAP_INFO:
549 sim_core *memory = STATE_CORE (sd);
552 for (nr_map = 0; nr_map < nr_maps; ++nr_map)
554 sim_core_map *map = &memory->common.map[nr_map];
555 sim_core_mapping *mapping = map->first;
560 sim_io_printf (sd, "%s maps:\n", map_to_str (nr_map));
565 sim_io_printf (sd, " map ");
566 if (mapping->space != 0)
567 sim_io_printf (sd, "0x%x:", mapping->space);
568 sim_io_printf (sd, "0x%08lx", (long) mapping->base);
569 if (mapping->level != 0)
570 sim_io_printf (sd, "@0x%x", mapping->level);
571 sim_io_printf (sd, ",0x%lx", (long) mapping->nr_bytes);
572 modulo = mapping->mask + 1;
574 sim_io_printf (sd, "%%0x%x", modulo);
575 sim_io_printf (sd, "\n");
577 mapping = mapping->next;
587 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
596 /* "memory" module install handler.
598 This is called via sim_module_install to install the "memory" subsystem
599 into the simulator. */
601 static MODULE_INIT_FN sim_memory_init;
602 static MODULE_UNINSTALL_FN sim_memory_uninstall;
605 sim_memopt_install (SIM_DESC sd)
607 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
608 sim_add_option_table (sd, NULL, memory_options);
609 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
610 sim_module_add_init_fn (sd, sim_memory_init);
615 /* Uninstall the "memory" subsystem from the simulator. */
618 sim_memory_uninstall (SIM_DESC sd)
620 sim_memopt **entry = &STATE_MEMOPT (sd);
623 while ((*entry) != NULL)
625 /* delete any buffer */
626 if ((*entry)->buffer != NULL)
629 if ((*entry)->munmap_length > 0)
630 munmap ((*entry)->buffer, (*entry)->munmap_length);
633 free ((*entry)->buffer);
636 /* delete it and its aliases */
640 *entry = (*entry)->next;
642 while (alias != NULL)
644 sim_memopt *dead = alias;
645 alias = alias->alias;
646 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
654 sim_memory_init (SIM_DESC sd)
656 /* Reinitialize option modifier flags, in case they were left
657 over from a previous sim startup event. */