1 /* Simulator memory option handling.
2 Copyright (C) 1996-2021 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"
32 #ifdef HAVE_SYS_MMAN_H
35 #ifdef HAVE_SYS_STAT_H
42 /* Memory fill byte. */
43 static unsigned8 fill_byte_value
;
44 static int fill_byte_flag
= 0;
46 /* Memory mapping; see OPTION_MEMORY_MAPFILE. */
47 static int mmap_next_fd
= -1;
49 /* Memory command line options. */
52 OPTION_MEMORY_DELETE
= OPTION_START
,
59 OPTION_MEMORY_MAPFILE
,
63 static DECLARE_OPTION_HANDLER (memory_option_handler
);
65 static const OPTION memory_options
[] =
67 { {"memory-delete", required_argument
, NULL
, OPTION_MEMORY_DELETE
},
68 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
69 memory_option_handler
},
70 { {"delete-memory", required_argument
, NULL
, OPTION_MEMORY_DELETE
},
71 '\0', "ADDRESS", NULL
,
72 memory_option_handler
},
74 { {"memory-region", required_argument
, NULL
, OPTION_MEMORY_REGION
},
75 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
76 memory_option_handler
},
78 { {"memory-alias", required_argument
, NULL
, OPTION_MEMORY_ALIAS
},
79 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
80 memory_option_handler
},
82 { {"memory-size", required_argument
, NULL
, OPTION_MEMORY_SIZE
},
83 '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
84 "Add memory at address zero", memory_option_handler
},
86 { {"memory-fill", required_argument
, NULL
, OPTION_MEMORY_FILL
},
87 '\0', "VALUE", "Fill subsequently added memory regions",
88 memory_option_handler
},
90 { {"memory-clear", no_argument
, NULL
, OPTION_MEMORY_CLEAR
},
91 '\0', NULL
, "Clear subsequently added memory regions",
92 memory_option_handler
},
94 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
95 { {"memory-mapfile", required_argument
, NULL
, OPTION_MEMORY_MAPFILE
},
96 '\0', "FILE", "Memory-map next memory region from file",
97 memory_option_handler
},
100 { {"memory-info", no_argument
, NULL
, OPTION_MEMORY_INFO
},
101 '\0', NULL
, "List configurable memory regions",
102 memory_option_handler
},
103 { {"info-memory", no_argument
, NULL
, OPTION_MEMORY_INFO
},
105 memory_option_handler
},
106 { {"map-info", no_argument
, NULL
, OPTION_MAP_INFO
},
107 '\0', NULL
, "List mapped regions",
108 memory_option_handler
},
110 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
115 do_memopt_add (SIM_DESC sd
,
119 address_word nr_bytes
,
125 unsigned fill_length
;
127 unsigned long free_length
;
131 /* Buffer already given. sim_memory_uninstall will free it. */
132 sim_core_attach (sd
, NULL
,
133 level
, access_read_write_exec
, space
,
134 addr
, nr_bytes
, modulo
, NULL
, buffer
);
136 free_buffer
= buffer
;
138 fill_buffer
= buffer
;
139 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
143 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
144 void *aligned_buffer
;
145 int padding
= (addr
% sizeof (unsigned64
));
151 if (mmap_next_fd
>= 0)
153 /* Check that given file is big enough. */
154 int rc
= fstat (mmap_next_fd
, &s
);
157 sim_io_error (sd
, "Error, unable to stat file: %s\n",
160 /* Autosize the mapping to the file length. */
162 nr_bytes
= s
.st_size
;
166 bytes
= (modulo
== 0 ? nr_bytes
: modulo
) + padding
;
172 /* Memory map or malloc(). */
173 if (mmap_next_fd
>= 0)
175 /* Some kernels will SIGBUS the application if mmap'd file
176 is not large enough. */
177 if (s
.st_size
< bytes
)
180 "Error, cannot confirm that mmap file is large enough "
181 "(>= %ld bytes)\n", bytes
);
184 free_buffer
= mmap (0, bytes
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, mmap_next_fd
, 0);
185 if (free_buffer
== 0 || free_buffer
== (char*)-1) /* MAP_FAILED */
187 sim_io_error (sd
, "Error, cannot mmap file (%s).\n",
193 /* Need heap allocation? */
194 if (free_buffer
== NULL
)
196 /* If filling with non-zero value, do not use clearing allocator. */
197 if (fill_byte_flag
&& fill_byte_value
!= 0)
198 free_buffer
= xmalloc (bytes
); /* don't clear */
200 free_buffer
= zalloc (bytes
); /* clear */
203 aligned_buffer
= (char*) free_buffer
+ padding
;
205 sim_core_attach (sd
, NULL
,
206 level
, access_read_write_exec
, space
,
207 addr
, nr_bytes
, modulo
, NULL
, aligned_buffer
);
209 fill_buffer
= aligned_buffer
;
210 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
212 /* If we just used a clearing allocator, and are about to fill with
213 zero, truncate the redundant fill operation. */
215 if (fill_byte_flag
&& fill_byte_value
== 0)
216 fill_length
= 1; /* avoid boundary length=0 case */
221 ASSERT (fill_buffer
!= 0);
222 memset ((char*) fill_buffer
, fill_byte_value
, fill_length
);
225 while ((*entry
) != NULL
)
226 entry
= &(*entry
)->next
;
227 (*entry
) = ZALLOC (sim_memopt
);
228 (*entry
)->level
= level
;
229 (*entry
)->space
= space
;
230 (*entry
)->addr
= addr
;
231 (*entry
)->nr_bytes
= nr_bytes
;
232 (*entry
)->modulo
= modulo
;
233 (*entry
)->buffer
= free_buffer
;
235 /* Record memory unmapping info. */
236 if (mmap_next_fd
>= 0)
238 (*entry
)->munmap_length
= free_length
;
239 close (mmap_next_fd
);
243 (*entry
)->munmap_length
= 0;
249 do_memopt_delete (SIM_DESC sd
,
254 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
256 while ((*entry
) != NULL
257 && ((*entry
)->level
!= level
258 || (*entry
)->space
!= space
259 || (*entry
)->addr
!= addr
))
260 entry
= &(*entry
)->next
;
261 if ((*entry
) == NULL
)
263 sim_io_eprintf (sd
, "Memory at 0x%lx not found, not deleted\n",
267 /* delete any buffer */
268 if ((*entry
)->buffer
!= NULL
)
271 if ((*entry
)->munmap_length
> 0)
272 munmap ((*entry
)->buffer
, (*entry
)->munmap_length
);
275 free ((*entry
)->buffer
);
278 /* delete it and its aliases */
280 *entry
= (*entry
)->next
;
281 while (alias
!= NULL
)
283 sim_memopt
*dead
= alias
;
284 alias
= alias
->alias
;
285 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
293 parse_size (char *chp
,
294 address_word
*nr_bytes
,
297 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
298 *nr_bytes
= strtoul (chp
, &chp
, 0);
302 *modulo
= strtoul (chp
+ 1, &chp
, 0);
304 case 'g': case 'G': /* Gigabyte suffix. */
307 case 'm': case 'M': /* Megabyte suffix. */
310 case 'k': case 'K': /* Kilobyte suffix. */
312 /* Check for a modulo specifier after the suffix. */
314 if (* chp
== 'b' || * chp
== 'B')
317 *modulo
= strtoul (chp
+ 1, &chp
, 0);
324 parse_ulong_value (char *chp
,
325 unsigned long *value
)
327 *value
= strtoul (chp
, &chp
, 0);
332 parse_addr (char *chp
,
337 /* [ <space> ": " ] <addr> [ "@" <level> ] */
338 *addr
= (unsigned long) strtoul (chp
, &chp
, 0);
342 *addr
= (unsigned long) strtoul (chp
+ 1, &chp
, 0);
346 *level
= strtoul (chp
+ 1, &chp
, 0);
353 memory_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
354 char *arg
, int is_command
)
359 case OPTION_MEMORY_DELETE
:
360 if (strcasecmp (arg
, "all") == 0)
362 while (STATE_MEMOPT (sd
) != NULL
)
363 do_memopt_delete (sd
,
364 STATE_MEMOPT (sd
)->level
,
365 STATE_MEMOPT (sd
)->space
,
366 STATE_MEMOPT (sd
)->addr
);
373 address_word addr
= 0;
374 parse_addr (arg
, &level
, &space
, &addr
);
375 return do_memopt_delete (sd
, level
, space
, addr
);
378 case OPTION_MEMORY_REGION
:
383 address_word addr
= 0;
384 address_word nr_bytes
= 0;
386 /* parse the arguments */
387 chp
= parse_addr (chp
, &level
, &space
, &addr
);
390 /* let the file autosize */
391 if (mmap_next_fd
== -1)
393 sim_io_eprintf (sd
, "Missing size for memory-region\n");
398 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
401 modulo
= strtoul (chp
+ 1, &chp
, 0);
402 /* try to attach/insert it */
403 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
404 &STATE_MEMOPT (sd
), NULL
);
408 case OPTION_MEMORY_ALIAS
:
413 address_word addr
= 0;
414 address_word nr_bytes
= 0;
417 /* parse the arguments */
418 chp
= parse_addr (chp
, &level
, &space
, &addr
);
421 sim_io_eprintf (sd
, "Missing size for memory-region\n");
424 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
425 /* try to attach/insert the main record */
426 entry
= do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
429 /* now attach all the aliases */
434 address_word a_addr
= addr
;
435 chp
= parse_addr (chp
+ 1, &a_level
, &a_space
, &a_addr
);
436 do_memopt_add (sd
, a_level
, a_space
, a_addr
, nr_bytes
, modulo
,
437 &entry
->alias
, entry
->buffer
);
442 case OPTION_MEMORY_SIZE
:
446 address_word addr
= 0;
447 address_word nr_bytes
= 0;
449 /* parse the arguments */
450 parse_size (arg
, &nr_bytes
, &modulo
);
451 /* try to attach/insert it */
452 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
453 &STATE_MEMOPT (sd
), NULL
);
457 case OPTION_MEMORY_CLEAR
:
459 fill_byte_value
= (unsigned8
) 0;
465 case OPTION_MEMORY_FILL
:
467 unsigned long fill_value
;
468 parse_ulong_value (arg
, &fill_value
);
469 if (fill_value
> 255)
471 sim_io_eprintf (sd
, "Missing fill value between 0 and 255\n");
474 fill_byte_value
= (unsigned8
) fill_value
;
480 case OPTION_MEMORY_MAPFILE
:
482 if (mmap_next_fd
>= 0)
484 sim_io_eprintf (sd
, "Duplicate memory-mapfile option\n");
488 mmap_next_fd
= open (arg
, O_RDWR
);
489 if (mmap_next_fd
< 0)
491 sim_io_eprintf (sd
, "Cannot open file `%s': %s\n",
492 arg
, strerror (errno
));
499 case OPTION_MEMORY_INFO
:
502 sim_io_printf (sd
, "Memory maps:\n");
503 for (entry
= STATE_MEMOPT (sd
); entry
!= NULL
; entry
= entry
->next
)
506 sim_io_printf (sd
, " memory");
507 if (entry
->alias
== NULL
)
508 sim_io_printf (sd
, " region ");
510 sim_io_printf (sd
, " alias ");
511 if (entry
->space
!= 0)
512 sim_io_printf (sd
, "0x%lx:", (long) entry
->space
);
513 sim_io_printf (sd
, "0x%08lx", (long) entry
->addr
);
514 if (entry
->level
!= 0)
515 sim_io_printf (sd
, "@0x%lx", (long) entry
->level
);
516 sim_io_printf (sd
, ",0x%lx",
517 (long) entry
->nr_bytes
);
518 if (entry
->modulo
!= 0)
519 sim_io_printf (sd
, "%%0x%lx", (long) entry
->modulo
);
520 for (alias
= entry
->alias
;
524 if (alias
->space
!= 0)
525 sim_io_printf (sd
, "0x%lx:", (long) alias
->space
);
526 sim_io_printf (sd
, ",0x%08lx", (long) alias
->addr
);
527 if (alias
->level
!= 0)
528 sim_io_printf (sd
, "@0x%lx", (long) alias
->level
);
530 sim_io_printf (sd
, "\n");
536 case OPTION_MAP_INFO
:
538 sim_core
*memory
= STATE_CORE (sd
);
541 for (nr_map
= 0; nr_map
< nr_maps
; ++nr_map
)
543 sim_core_map
*map
= &memory
->common
.map
[nr_map
];
544 sim_core_mapping
*mapping
= map
->first
;
549 sim_io_printf (sd
, "%s maps:\n", map_to_str (nr_map
));
554 sim_io_printf (sd
, " map ");
555 if (mapping
->space
!= 0)
556 sim_io_printf (sd
, "0x%x:", mapping
->space
);
557 sim_io_printf (sd
, "0x%08lx", (long) mapping
->base
);
558 if (mapping
->level
!= 0)
559 sim_io_printf (sd
, "@0x%x", mapping
->level
);
560 sim_io_printf (sd
, ",0x%lx", (long) mapping
->nr_bytes
);
561 modulo
= mapping
->mask
+ 1;
563 sim_io_printf (sd
, "%%0x%x", modulo
);
564 sim_io_printf (sd
, "\n");
566 mapping
= mapping
->next
;
576 sim_io_eprintf (sd
, "Unknown memory option %d\n", opt
);
585 /* "memory" module install handler.
587 This is called via sim_module_install to install the "memory" subsystem
588 into the simulator. */
590 static MODULE_INIT_FN sim_memory_init
;
591 static MODULE_UNINSTALL_FN sim_memory_uninstall
;
594 sim_memopt_install (SIM_DESC sd
)
596 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
597 sim_add_option_table (sd
, NULL
, memory_options
);
598 sim_module_add_uninstall_fn (sd
, sim_memory_uninstall
);
599 sim_module_add_init_fn (sd
, sim_memory_init
);
604 /* Uninstall the "memory" subsystem from the simulator. */
607 sim_memory_uninstall (SIM_DESC sd
)
609 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
612 while ((*entry
) != NULL
)
614 /* delete any buffer */
615 if ((*entry
)->buffer
!= NULL
)
618 if ((*entry
)->munmap_length
> 0)
619 munmap ((*entry
)->buffer
, (*entry
)->munmap_length
);
622 free ((*entry
)->buffer
);
625 /* delete it and its aliases */
629 *entry
= (*entry
)->next
;
631 while (alias
!= NULL
)
633 sim_memopt
*dead
= alias
;
634 alias
= alias
->alias
;
635 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
643 sim_memory_init (SIM_DESC sd
)
645 /* Reinitialize option modifier flags, in case they were left
646 over from a previous sim startup event. */