1 /* Simulator memory option handling.
2 Copyright (C) 1996-1999 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 2, or (at your option)
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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "sim-assert.h"
25 #include "sim-options.h"
43 #ifdef HAVE_SYS_MMAN_H
46 #ifdef HAVE_SYS_STAT_H
50 /* Memory fill byte. */
51 static unsigned8 fill_byte_value;
52 static int fill_byte_flag = 0;
54 /* Memory mapping; see OPTION_MEMORY_MAPFILE. */
55 static int mmap_next_fd = -1;
57 /* Memory command line options. */
60 OPTION_MEMORY_DELETE = OPTION_START,
70 static DECLARE_OPTION_HANDLER (memory_option_handler);
72 static const OPTION memory_options[] =
74 { {"memory-delete", required_argument, NULL, OPTION_MEMORY_DELETE },
75 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
76 memory_option_handler },
77 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
78 '\0', "ADDRESS", NULL,
79 memory_option_handler },
81 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
82 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
83 memory_option_handler },
85 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
86 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
87 memory_option_handler },
89 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
90 '\0', "SIZE", "Add memory at address zero",
91 memory_option_handler },
93 { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
94 '\0', "VALUE", "Fill subsequently added memory regions",
95 memory_option_handler },
97 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
98 '\0', NULL, "Clear subsequently added memory regions",
99 memory_option_handler },
101 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
102 { {"memory-mapfile", required_argument, NULL, OPTION_MEMORY_MAPFILE },
103 '\0', "FILE", "Memory-map next memory region from file",
104 memory_option_handler },
107 { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
108 '\0', NULL, "List configurable memory regions",
109 memory_option_handler },
110 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
112 memory_option_handler },
114 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
119 do_memopt_add (SIM_DESC sd,
123 address_word nr_bytes,
129 unsigned fill_length;
131 unsigned long free_length;
135 /* Buffer already given. sim_memory_uninstall will free it. */
136 sim_core_attach (sd, NULL,
137 level, access_read_write_exec, space,
138 addr, nr_bytes, modulo, NULL, buffer);
140 free_buffer = buffer;
142 fill_buffer = buffer;
143 fill_length = (modulo == 0) ? nr_bytes : modulo;
147 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
148 void *aligned_buffer;
149 int padding = (addr % sizeof (unsigned64));
150 unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
156 /* Memory map or malloc(). */
157 if (mmap_next_fd >= 0)
159 /* Check that given file is big enough. */
163 /* Some kernels will SIGBUS the application if mmap'd file
164 is not large enough. */
165 rc = fstat (mmap_next_fd, &s);
166 if (rc < 0 || s.st_size < bytes)
169 "Error, cannot confirm that mmap file is large enough "
170 "(>= %d bytes)\n", bytes);
173 free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
174 if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
176 sim_io_error (sd, "Error, cannot mmap file (%s).\n",
182 /* Need heap allocation? */
183 if (free_buffer == NULL)
185 /* If filling with non-zero value, do not use clearing allocator. */
186 if (fill_byte_flag && fill_byte_value != 0)
187 free_buffer = xmalloc (bytes); /* don't clear */
189 free_buffer = zalloc (bytes); /* clear */
192 aligned_buffer = (char*) free_buffer + padding;
194 sim_core_attach (sd, NULL,
195 level, access_read_write_exec, space,
196 addr, nr_bytes, modulo, NULL, aligned_buffer);
198 fill_buffer = aligned_buffer;
199 fill_length = (modulo == 0) ? nr_bytes : modulo;
201 /* If we just used a clearing allocator, and are about to fill with
202 zero, truncate the redundant fill operation. */
204 if (fill_byte_flag && fill_byte_value == 0)
205 fill_length = 1; /* avoid boundary length=0 case */
210 ASSERT (fill_buffer != 0);
211 memset ((char*) fill_buffer, fill_byte_value, fill_length);
214 while ((*entry) != NULL)
215 entry = &(*entry)->next;
216 (*entry) = ZALLOC (sim_memopt);
217 (*entry)->level = level;
218 (*entry)->space = space;
219 (*entry)->addr = addr;
220 (*entry)->nr_bytes = nr_bytes;
221 (*entry)->modulo = modulo;
222 (*entry)->buffer = free_buffer;
224 /* Record memory unmapping info. */
225 if (mmap_next_fd >= 0)
227 (*entry)->munmap_length = free_length;
228 close (mmap_next_fd);
232 (*entry)->munmap_length = 0;
238 do_memopt_delete (SIM_DESC sd,
243 sim_memopt **entry = &STATE_MEMOPT (sd);
245 while ((*entry) != NULL
246 && ((*entry)->level != level
247 || (*entry)->space != space
248 || (*entry)->addr != addr))
249 entry = &(*entry)->next;
250 if ((*entry) == NULL)
252 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
256 /* delete any buffer */
257 if ((*entry)->buffer != NULL)
260 if ((*entry)->munmap_length > 0)
261 munmap ((*entry)->buffer, (*entry)->munmap_length);
264 zfree ((*entry)->buffer);
267 /* delete it and its aliases */
269 *entry = (*entry)->next;
270 while (alias != NULL)
272 sim_memopt *dead = alias;
273 alias = alias->alias;
274 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
282 parse_size (char *chp,
283 address_word *nr_bytes,
286 /* <nr_bytes> [ "%" <modulo> ] */
287 *nr_bytes = strtoul (chp, &chp, 0);
290 *modulo = strtoul (chp + 1, &chp, 0);
296 parse_ulong_value (char *chp,
297 unsigned long *value)
299 *value = strtoul (chp, &chp, 0);
304 parse_addr (char *chp,
309 /* [ <space> ": " ] <addr> [ "@" <level> ] */
310 *addr = (unsigned long) strtoul (chp, &chp, 0);
314 *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
318 *level = strtoul (chp + 1, &chp, 0);
325 memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
326 char *arg, int is_command)
331 case OPTION_MEMORY_DELETE:
332 if (strcasecmp (arg, "all") == 0)
334 while (STATE_MEMOPT (sd) != NULL)
335 do_memopt_delete (sd,
336 STATE_MEMOPT (sd)->level,
337 STATE_MEMOPT (sd)->space,
338 STATE_MEMOPT (sd)->addr);
345 address_word addr = 0;
346 parse_addr (arg, &level, &space, &addr);
347 return do_memopt_delete (sd, level, space, addr);
350 case OPTION_MEMORY_REGION:
355 address_word addr = 0;
356 address_word nr_bytes = 0;
358 /* parse the arguments */
359 chp = parse_addr (chp, &level, &space, &addr);
362 sim_io_eprintf (sd, "Missing size for memory-region\n");
365 chp = parse_size (chp + 1, &nr_bytes, &modulo);
368 modulo = strtoul (chp + 1, &chp, 0);
369 /* try to attach/insert it */
370 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
371 &STATE_MEMOPT (sd), NULL);
375 case OPTION_MEMORY_ALIAS:
380 address_word addr = 0;
381 address_word nr_bytes = 0;
384 /* parse the arguments */
385 chp = parse_addr (chp, &level, &space, &addr);
388 sim_io_eprintf (sd, "Missing size for memory-region\n");
391 chp = parse_size (chp + 1, &nr_bytes, &modulo);
392 /* try to attach/insert the main record */
393 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
396 /* now attach all the aliases */
401 address_word a_addr = addr;
402 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
403 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
404 &entry->alias, entry->buffer);
409 case OPTION_MEMORY_SIZE:
413 address_word addr = 0;
414 address_word nr_bytes = 0;
416 /* parse the arguments */
417 parse_size (arg, &nr_bytes, &modulo);
418 /* try to attach/insert it */
419 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
420 &STATE_MEMOPT (sd), NULL);
424 case OPTION_MEMORY_CLEAR:
426 fill_byte_value = (unsigned8) 0;
432 case OPTION_MEMORY_FILL:
434 unsigned long fill_value;
435 parse_ulong_value (arg, &fill_value);
436 if (fill_value > 255)
438 sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
441 fill_byte_value = (unsigned8) fill_value;
447 case OPTION_MEMORY_MAPFILE:
449 if (mmap_next_fd >= 0)
451 sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
455 mmap_next_fd = open (arg, O_RDWR);
456 if (mmap_next_fd < 0)
458 sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
459 arg, strerror(errno));
466 case OPTION_MEMORY_INFO:
469 sim_io_printf (sd, "Memory maps:\n");
470 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
473 sim_io_printf (sd, " memory");
474 if (entry->alias == NULL)
475 sim_io_printf (sd, " region ");
477 sim_io_printf (sd, " alias ");
478 if (entry->space != 0)
479 sim_io_printf (sd, "0x%lx:", (long) entry->space);
480 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
481 if (entry->level != 0)
482 sim_io_printf (sd, "@0x%lx", (long) entry->level);
483 sim_io_printf (sd, ",0x%lx",
484 (long) entry->nr_bytes);
485 if (entry->modulo != 0)
486 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
487 for (alias = entry->alias;
491 if (alias->space != 0)
492 sim_io_printf (sd, "0x%lx:", (long) alias->space);
493 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
494 if (alias->level != 0)
495 sim_io_printf (sd, "@0x%lx", (long) alias->level);
497 sim_io_printf (sd, "\n");
504 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
513 /* "memory" module install handler.
515 This is called via sim_module_install to install the "memory" subsystem
516 into the simulator. */
518 static MODULE_INIT_FN sim_memory_init;
519 static MODULE_UNINSTALL_FN sim_memory_uninstall;
522 sim_memopt_install (SIM_DESC sd)
524 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
525 sim_add_option_table (sd, NULL, memory_options);
526 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
527 sim_module_add_init_fn (sd, sim_memory_init);
532 /* Uninstall the "memory" subsystem from the simulator. */
535 sim_memory_uninstall (SIM_DESC sd)
537 sim_memopt **entry = &STATE_MEMOPT (sd);
540 while ((*entry) != NULL)
542 /* delete any buffer */
543 if ((*entry)->buffer != NULL)
546 if ((*entry)->munmap_length > 0)
547 munmap ((*entry)->buffer, (*entry)->munmap_length);
550 zfree ((*entry)->buffer);
553 /* delete it and its aliases */
557 *entry = (*entry)->next;
559 while (alias != NULL)
561 sim_memopt *dead = alias;
562 alias = alias->alias;
563 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
571 sim_memory_init (SIM_DESC sd)
573 /* Reinitialize option modifier flags, in case they were left
574 over from a previous sim startup event. */