1 /* Simulator memory option handling.
2 Copyright (C) 1996-2023 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/>. */
20 /* This must come before any other includes. */
32 #ifdef HAVE_SYS_MMAN_H
35 #ifdef HAVE_SYS_STAT_H
40 #include "sim-assert.h"
41 #include "sim-options.h"
43 /* Memory fill byte. */
44 static uint8_t fill_byte_value;
45 static int fill_byte_flag = 0;
47 /* Memory mapping; see OPTION_MEMORY_MAPFILE. */
48 static int mmap_next_fd = -1;
50 /* Memory command line options. */
53 OPTION_MEMORY_DELETE = OPTION_START,
60 OPTION_MEMORY_MAPFILE,
64 static DECLARE_OPTION_HANDLER (memory_option_handler);
66 static const OPTION memory_options[] =
68 { {"memory-delete", required_argument, NULL, OPTION_MEMORY_DELETE },
69 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
70 memory_option_handler },
71 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
72 '\0', "ADDRESS", NULL,
73 memory_option_handler },
75 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
76 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
77 memory_option_handler },
79 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
80 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
81 memory_option_handler },
83 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
84 '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
85 "Add memory at address zero", memory_option_handler },
87 { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
88 '\0', "VALUE", "Fill subsequently added memory regions",
89 memory_option_handler },
91 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
92 '\0', NULL, "Clear subsequently added memory regions",
93 memory_option_handler },
95 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
96 { {"memory-mapfile", required_argument, NULL, OPTION_MEMORY_MAPFILE },
97 '\0', "FILE", "Memory-map next memory region from file",
98 memory_option_handler },
101 { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
102 '\0', NULL, "List configurable memory regions",
103 memory_option_handler },
104 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
106 memory_option_handler },
107 { {"map-info", no_argument, NULL, OPTION_MAP_INFO },
108 '\0', NULL, "List mapped regions",
109 memory_option_handler },
111 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
116 do_memopt_add (SIM_DESC sd,
120 address_word nr_bytes,
126 unsigned fill_length;
128 unsigned long free_length;
132 /* Buffer already given. sim_memory_uninstall will free it. */
133 sim_core_attach (sd, NULL,
134 level, access_read_write_exec, space,
135 addr, nr_bytes, modulo, NULL, buffer);
137 free_buffer = buffer;
139 fill_buffer = buffer;
140 fill_length = (modulo == 0) ? nr_bytes : modulo;
144 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
145 void *aligned_buffer;
146 int padding = (addr % sizeof (uint64_t));
152 if (mmap_next_fd >= 0)
154 /* Check that given file is big enough. */
155 int rc = fstat (mmap_next_fd, &s);
158 sim_io_error (sd, "Error, unable to stat file: %s\n",
161 /* Autosize the mapping to the file length. */
163 nr_bytes = s.st_size;
167 bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
173 /* Memory map or malloc(). */
174 if (mmap_next_fd >= 0)
176 /* Some kernels will SIGBUS the application if mmap'd file
177 is not large enough. */
178 if (s.st_size < bytes)
181 "Error, cannot confirm that mmap file is large enough "
182 "(>= %ld bytes)\n", bytes);
185 free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
186 if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
188 sim_io_error (sd, "Error, cannot mmap file (%s).\n",
194 /* Need heap allocation? */
195 if (free_buffer == NULL)
197 /* If filling with non-zero value, do not use clearing allocator. */
198 if (fill_byte_flag && fill_byte_value != 0)
199 free_buffer = xmalloc (bytes); /* don't clear */
201 free_buffer = zalloc (bytes); /* clear */
204 aligned_buffer = (char*) free_buffer + padding;
206 sim_core_attach (sd, NULL,
207 level, access_read_write_exec, space,
208 addr, nr_bytes, modulo, NULL, aligned_buffer);
210 fill_buffer = aligned_buffer;
211 fill_length = (modulo == 0) ? nr_bytes : modulo;
213 /* If we just used a clearing allocator, and are about to fill with
214 zero, truncate the redundant fill operation. */
216 if (fill_byte_flag && fill_byte_value == 0)
217 fill_length = 1; /* avoid boundary length=0 case */
222 ASSERT (fill_buffer != 0);
223 memset ((char*) fill_buffer, fill_byte_value, fill_length);
226 while ((*entry) != NULL)
227 entry = &(*entry)->next;
228 (*entry) = ZALLOC (sim_memopt);
229 (*entry)->level = level;
230 (*entry)->space = space;
231 (*entry)->addr = addr;
232 (*entry)->nr_bytes = nr_bytes;
233 (*entry)->modulo = modulo;
234 (*entry)->buffer = free_buffer;
236 /* Record memory unmapping info. */
237 if (mmap_next_fd >= 0)
239 (*entry)->munmap_length = free_length;
240 close (mmap_next_fd);
244 (*entry)->munmap_length = 0;
250 do_memopt_delete (SIM_DESC sd,
255 sim_memopt **entry = &STATE_MEMOPT (sd);
257 while ((*entry) != NULL
258 && ((*entry)->level != level
259 || (*entry)->space != space
260 || (*entry)->addr != addr))
261 entry = &(*entry)->next;
262 if ((*entry) == NULL)
264 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
268 /* delete any buffer */
269 if ((*entry)->buffer != NULL)
272 if ((*entry)->munmap_length > 0)
273 munmap ((*entry)->buffer, (*entry)->munmap_length);
276 free ((*entry)->buffer);
279 /* delete it and its aliases */
281 *entry = (*entry)->next;
282 while (alias != NULL)
284 sim_memopt *dead = alias;
285 alias = alias->alias;
286 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
294 parse_size (char *chp,
295 address_word *nr_bytes,
298 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
299 *nr_bytes = strtoul (chp, &chp, 0);
303 *modulo = strtoul (chp + 1, &chp, 0);
305 case 'g': case 'G': /* Gigabyte suffix. */
308 case 'm': case 'M': /* Megabyte suffix. */
311 case 'k': case 'K': /* Kilobyte suffix. */
313 /* Check for a modulo specifier after the suffix. */
315 if (* chp == 'b' || * chp == 'B')
318 *modulo = strtoul (chp + 1, &chp, 0);
325 parse_ulong_value (char *chp,
326 unsigned long *value)
328 *value = strtoul (chp, &chp, 0);
333 parse_addr (char *chp,
338 /* [ <space> ": " ] <addr> [ "@" <level> ] */
339 *addr = (unsigned long) strtoul (chp, &chp, 0);
343 *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
347 *level = strtoul (chp + 1, &chp, 0);
354 memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
355 char *arg, int is_command)
360 case OPTION_MEMORY_DELETE:
361 if (strcasecmp (arg, "all") == 0)
363 while (STATE_MEMOPT (sd) != NULL)
364 do_memopt_delete (sd,
365 STATE_MEMOPT (sd)->level,
366 STATE_MEMOPT (sd)->space,
367 STATE_MEMOPT (sd)->addr);
374 address_word addr = 0;
375 parse_addr (arg, &level, &space, &addr);
376 return do_memopt_delete (sd, level, space, addr);
379 case OPTION_MEMORY_REGION:
384 address_word addr = 0;
385 address_word nr_bytes = 0;
387 /* parse the arguments */
388 chp = parse_addr (chp, &level, &space, &addr);
391 /* let the file autosize */
392 if (mmap_next_fd == -1)
394 sim_io_eprintf (sd, "Missing size for memory-region\n");
399 chp = parse_size (chp + 1, &nr_bytes, &modulo);
402 modulo = strtoul (chp + 1, &chp, 0);
403 /* try to attach/insert it */
404 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
405 &STATE_MEMOPT (sd), NULL);
409 case OPTION_MEMORY_ALIAS:
414 address_word addr = 0;
415 address_word nr_bytes = 0;
418 /* parse the arguments */
419 chp = parse_addr (chp, &level, &space, &addr);
422 sim_io_eprintf (sd, "Missing size for memory-region\n");
425 chp = parse_size (chp + 1, &nr_bytes, &modulo);
426 /* try to attach/insert the main record */
427 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
430 /* now attach all the aliases */
435 address_word a_addr = addr;
436 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
437 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
438 &entry->alias, entry->buffer);
443 case OPTION_MEMORY_SIZE:
447 address_word addr = 0;
448 address_word nr_bytes = 0;
450 /* parse the arguments */
451 parse_size (arg, &nr_bytes, &modulo);
452 /* try to attach/insert it */
453 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
454 &STATE_MEMOPT (sd), NULL);
458 case OPTION_MEMORY_CLEAR:
460 fill_byte_value = (uint8_t) 0;
466 case OPTION_MEMORY_FILL:
468 unsigned long fill_value;
469 parse_ulong_value (arg, &fill_value);
470 if (fill_value > 255)
472 sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
475 fill_byte_value = (uint8_t) fill_value;
481 case OPTION_MEMORY_MAPFILE:
483 if (mmap_next_fd >= 0)
485 sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
489 mmap_next_fd = open (arg, O_RDWR);
490 if (mmap_next_fd < 0)
492 sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
493 arg, strerror (errno));
500 case OPTION_MEMORY_INFO:
503 sim_io_printf (sd, "Memory maps:\n");
504 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
507 sim_io_printf (sd, " memory");
508 if (entry->alias == NULL)
509 sim_io_printf (sd, " region ");
511 sim_io_printf (sd, " alias ");
512 if (entry->space != 0)
513 sim_io_printf (sd, "0x%lx:", (long) entry->space);
514 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
515 if (entry->level != 0)
516 sim_io_printf (sd, "@0x%lx", (long) entry->level);
517 sim_io_printf (sd, ",0x%lx",
518 (long) entry->nr_bytes);
519 if (entry->modulo != 0)
520 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
521 for (alias = entry->alias;
525 if (alias->space != 0)
526 sim_io_printf (sd, "0x%lx:", (long) alias->space);
527 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
528 if (alias->level != 0)
529 sim_io_printf (sd, "@0x%lx", (long) alias->level);
531 sim_io_printf (sd, "\n");
537 case OPTION_MAP_INFO:
539 sim_core *memory = STATE_CORE (sd);
542 for (nr_map = 0; nr_map < nr_maps; ++nr_map)
544 sim_core_map *map = &memory->common.map[nr_map];
545 sim_core_mapping *mapping = map->first;
550 sim_io_printf (sd, "%s maps:\n", map_to_str (nr_map));
555 sim_io_printf (sd, " map ");
556 if (mapping->space != 0)
557 sim_io_printf (sd, "0x%x:", mapping->space);
558 sim_io_printf (sd, "0x%08lx", (long) mapping->base);
559 if (mapping->level != 0)
560 sim_io_printf (sd, "@0x%x", mapping->level);
561 sim_io_printf (sd, ",0x%lx", (long) mapping->nr_bytes);
562 modulo = mapping->mask + 1;
564 sim_io_printf (sd, "%%0x%x", modulo);
565 sim_io_printf (sd, "\n");
567 mapping = mapping->next;
577 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
586 /* "memory" module install handler.
588 This is called via sim_module_install to install the "memory" subsystem
589 into the simulator. */
591 static MODULE_INIT_FN sim_memory_init;
592 static MODULE_UNINSTALL_FN sim_memory_uninstall;
595 sim_memopt_install (SIM_DESC sd)
597 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
598 sim_add_option_table (sd, NULL, memory_options);
599 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
600 sim_module_add_init_fn (sd, sim_memory_init);
605 /* Uninstall the "memory" subsystem from the simulator. */
608 sim_memory_uninstall (SIM_DESC sd)
610 sim_memopt **entry = &STATE_MEMOPT (sd);
613 while ((*entry) != NULL)
615 /* delete any buffer */
616 if ((*entry)->buffer != NULL)
619 if ((*entry)->munmap_length > 0)
620 munmap ((*entry)->buffer, (*entry)->munmap_length);
623 free ((*entry)->buffer);
626 /* delete it and its aliases */
630 *entry = (*entry)->next;
632 while (alias != NULL)
634 sim_memopt *dead = alias;
635 alias = alias->alias;
636 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
642 void sim_dump_memory (SIM_DESC sd);
644 /* Convenience function for use when debugging the simulator, to be
645 called from within e.g. gdb. */
648 sim_dump_memory (SIM_DESC sd)
650 memory_option_handler (sd, NULL, OPTION_MEMORY_INFO, NULL, 0);
651 memory_option_handler (sd, NULL, OPTION_MAP_INFO, NULL, 0);
655 sim_memory_init (SIM_DESC sd)
657 /* Reinitialize option modifier flags, in case they were left
658 over from a previous sim startup event. */