1 /* Simulator memory option handling.
2 Copyright (C) 1996-1999, 2007, 2008, 2009, 2010
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,
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 },
117 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
122 do_memopt_add (SIM_DESC sd,
126 address_word nr_bytes,
132 unsigned fill_length;
134 unsigned long free_length;
138 /* Buffer already given. sim_memory_uninstall will free it. */
139 sim_core_attach (sd, NULL,
140 level, access_read_write_exec, space,
141 addr, nr_bytes, modulo, NULL, buffer);
143 free_buffer = buffer;
145 fill_buffer = buffer;
146 fill_length = (modulo == 0) ? nr_bytes : modulo;
150 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
151 void *aligned_buffer;
152 int padding = (addr % sizeof (unsigned64));
153 unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
159 /* Memory map or malloc(). */
160 if (mmap_next_fd >= 0)
162 /* Check that given file is big enough. */
166 /* Some kernels will SIGBUS the application if mmap'd file
167 is not large enough. */
168 rc = fstat (mmap_next_fd, &s);
169 if (rc < 0 || s.st_size < bytes)
172 "Error, cannot confirm that mmap file is large enough "
173 "(>= %ld bytes)\n", bytes);
176 free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
177 if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
179 sim_io_error (sd, "Error, cannot mmap file (%s).\n",
185 /* Need heap allocation? */
186 if (free_buffer == NULL)
188 /* If filling with non-zero value, do not use clearing allocator. */
189 if (fill_byte_flag && fill_byte_value != 0)
190 free_buffer = xmalloc (bytes); /* don't clear */
192 free_buffer = zalloc (bytes); /* clear */
195 aligned_buffer = (char*) free_buffer + padding;
197 sim_core_attach (sd, NULL,
198 level, access_read_write_exec, space,
199 addr, nr_bytes, modulo, NULL, aligned_buffer);
201 fill_buffer = aligned_buffer;
202 fill_length = (modulo == 0) ? nr_bytes : modulo;
204 /* If we just used a clearing allocator, and are about to fill with
205 zero, truncate the redundant fill operation. */
207 if (fill_byte_flag && fill_byte_value == 0)
208 fill_length = 1; /* avoid boundary length=0 case */
213 ASSERT (fill_buffer != 0);
214 memset ((char*) fill_buffer, fill_byte_value, fill_length);
217 while ((*entry) != NULL)
218 entry = &(*entry)->next;
219 (*entry) = ZALLOC (sim_memopt);
220 (*entry)->level = level;
221 (*entry)->space = space;
222 (*entry)->addr = addr;
223 (*entry)->nr_bytes = nr_bytes;
224 (*entry)->modulo = modulo;
225 (*entry)->buffer = free_buffer;
227 /* Record memory unmapping info. */
228 if (mmap_next_fd >= 0)
230 (*entry)->munmap_length = free_length;
231 close (mmap_next_fd);
235 (*entry)->munmap_length = 0;
241 do_memopt_delete (SIM_DESC sd,
246 sim_memopt **entry = &STATE_MEMOPT (sd);
248 while ((*entry) != NULL
249 && ((*entry)->level != level
250 || (*entry)->space != space
251 || (*entry)->addr != addr))
252 entry = &(*entry)->next;
253 if ((*entry) == NULL)
255 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
259 /* delete any buffer */
260 if ((*entry)->buffer != NULL)
263 if ((*entry)->munmap_length > 0)
264 munmap ((*entry)->buffer, (*entry)->munmap_length);
267 zfree ((*entry)->buffer);
270 /* delete it and its aliases */
272 *entry = (*entry)->next;
273 while (alias != NULL)
275 sim_memopt *dead = alias;
276 alias = alias->alias;
277 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
285 parse_size (char *chp,
286 address_word *nr_bytes,
289 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
290 *nr_bytes = strtoul (chp, &chp, 0);
294 *modulo = strtoul (chp + 1, &chp, 0);
296 case 'g': case 'G': /* Gigabyte suffix. */
299 case 'm': case 'M': /* Megabyte suffix. */
302 case 'k': case 'K': /* Kilobyte suffix. */
304 /* Check for a modulo specifier after the suffix. */
306 if (* chp == 'b' || * chp == 'B')
309 *modulo = strtoul (chp + 1, &chp, 0);
316 parse_ulong_value (char *chp,
317 unsigned long *value)
319 *value = strtoul (chp, &chp, 0);
324 parse_addr (char *chp,
329 /* [ <space> ": " ] <addr> [ "@" <level> ] */
330 *addr = (unsigned long) strtoul (chp, &chp, 0);
334 *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
338 *level = strtoul (chp + 1, &chp, 0);
345 memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
346 char *arg, int is_command)
351 case OPTION_MEMORY_DELETE:
352 if (strcasecmp (arg, "all") == 0)
354 while (STATE_MEMOPT (sd) != NULL)
355 do_memopt_delete (sd,
356 STATE_MEMOPT (sd)->level,
357 STATE_MEMOPT (sd)->space,
358 STATE_MEMOPT (sd)->addr);
365 address_word addr = 0;
366 parse_addr (arg, &level, &space, &addr);
367 return do_memopt_delete (sd, level, space, addr);
370 case OPTION_MEMORY_REGION:
375 address_word addr = 0;
376 address_word nr_bytes = 0;
378 /* parse the arguments */
379 chp = parse_addr (chp, &level, &space, &addr);
382 sim_io_eprintf (sd, "Missing size for memory-region\n");
385 chp = parse_size (chp + 1, &nr_bytes, &modulo);
388 modulo = strtoul (chp + 1, &chp, 0);
389 /* try to attach/insert it */
390 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
391 &STATE_MEMOPT (sd), NULL);
395 case OPTION_MEMORY_ALIAS:
400 address_word addr = 0;
401 address_word nr_bytes = 0;
404 /* parse the arguments */
405 chp = parse_addr (chp, &level, &space, &addr);
408 sim_io_eprintf (sd, "Missing size for memory-region\n");
411 chp = parse_size (chp + 1, &nr_bytes, &modulo);
412 /* try to attach/insert the main record */
413 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
416 /* now attach all the aliases */
421 address_word a_addr = addr;
422 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
423 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
424 &entry->alias, entry->buffer);
429 case OPTION_MEMORY_SIZE:
433 address_word addr = 0;
434 address_word nr_bytes = 0;
436 /* parse the arguments */
437 parse_size (arg, &nr_bytes, &modulo);
438 /* try to attach/insert it */
439 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
440 &STATE_MEMOPT (sd), NULL);
444 case OPTION_MEMORY_CLEAR:
446 fill_byte_value = (unsigned8) 0;
452 case OPTION_MEMORY_FILL:
454 unsigned long fill_value;
455 parse_ulong_value (arg, &fill_value);
456 if (fill_value > 255)
458 sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
461 fill_byte_value = (unsigned8) fill_value;
467 case OPTION_MEMORY_MAPFILE:
469 if (mmap_next_fd >= 0)
471 sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
475 mmap_next_fd = open (arg, O_RDWR);
476 if (mmap_next_fd < 0)
478 sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
479 arg, strerror(errno));
486 case OPTION_MEMORY_INFO:
489 sim_io_printf (sd, "Memory maps:\n");
490 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
493 sim_io_printf (sd, " memory");
494 if (entry->alias == NULL)
495 sim_io_printf (sd, " region ");
497 sim_io_printf (sd, " alias ");
498 if (entry->space != 0)
499 sim_io_printf (sd, "0x%lx:", (long) entry->space);
500 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
501 if (entry->level != 0)
502 sim_io_printf (sd, "@0x%lx", (long) entry->level);
503 sim_io_printf (sd, ",0x%lx",
504 (long) entry->nr_bytes);
505 if (entry->modulo != 0)
506 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
507 for (alias = entry->alias;
511 if (alias->space != 0)
512 sim_io_printf (sd, "0x%lx:", (long) alias->space);
513 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
514 if (alias->level != 0)
515 sim_io_printf (sd, "@0x%lx", (long) alias->level);
517 sim_io_printf (sd, "\n");
524 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
533 /* "memory" module install handler.
535 This is called via sim_module_install to install the "memory" subsystem
536 into the simulator. */
538 static MODULE_INIT_FN sim_memory_init;
539 static MODULE_UNINSTALL_FN sim_memory_uninstall;
542 sim_memopt_install (SIM_DESC sd)
544 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
545 sim_add_option_table (sd, NULL, memory_options);
546 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
547 sim_module_add_init_fn (sd, sim_memory_init);
552 /* Uninstall the "memory" subsystem from the simulator. */
555 sim_memory_uninstall (SIM_DESC sd)
557 sim_memopt **entry = &STATE_MEMOPT (sd);
560 while ((*entry) != NULL)
562 /* delete any buffer */
563 if ((*entry)->buffer != NULL)
566 if ((*entry)->munmap_length > 0)
567 munmap ((*entry)->buffer, (*entry)->munmap_length);
570 zfree ((*entry)->buffer);
573 /* delete it and its aliases */
577 *entry = (*entry)->next;
579 while (alias != NULL)
581 sim_memopt *dead = alias;
582 alias = alias->alias;
583 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
591 sim_memory_init (SIM_DESC sd)
593 /* Reinitialize option modifier flags, in case they were left
594 over from a previous sim startup event. */