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
,
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
));
157 unsigned long bytes
= (modulo
== 0 ? nr_bytes
: modulo
) + padding
;
163 /* Memory map or malloc(). */
164 if (mmap_next_fd
>= 0)
166 /* Check that given file is big enough. */
170 /* Some kernels will SIGBUS the application if mmap'd file
171 is not large enough. */
172 rc
= fstat (mmap_next_fd
, &s
);
173 if (rc
< 0 || s
.st_size
< bytes
)
176 "Error, cannot confirm that mmap file is large enough "
177 "(>= %ld bytes)\n", bytes
);
180 free_buffer
= mmap (0, bytes
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, mmap_next_fd
, 0);
181 if (free_buffer
== 0 || free_buffer
== (char*)-1) /* MAP_FAILED */
183 sim_io_error (sd
, "Error, cannot mmap file (%s).\n",
189 /* Need heap allocation? */
190 if (free_buffer
== NULL
)
192 /* If filling with non-zero value, do not use clearing allocator. */
193 if (fill_byte_flag
&& fill_byte_value
!= 0)
194 free_buffer
= xmalloc (bytes
); /* don't clear */
196 free_buffer
= zalloc (bytes
); /* clear */
199 aligned_buffer
= (char*) free_buffer
+ padding
;
201 sim_core_attach (sd
, NULL
,
202 level
, access_read_write_exec
, space
,
203 addr
, nr_bytes
, modulo
, NULL
, aligned_buffer
);
205 fill_buffer
= aligned_buffer
;
206 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
208 /* If we just used a clearing allocator, and are about to fill with
209 zero, truncate the redundant fill operation. */
211 if (fill_byte_flag
&& fill_byte_value
== 0)
212 fill_length
= 1; /* avoid boundary length=0 case */
217 ASSERT (fill_buffer
!= 0);
218 memset ((char*) fill_buffer
, fill_byte_value
, fill_length
);
221 while ((*entry
) != NULL
)
222 entry
= &(*entry
)->next
;
223 (*entry
) = ZALLOC (sim_memopt
);
224 (*entry
)->level
= level
;
225 (*entry
)->space
= space
;
226 (*entry
)->addr
= addr
;
227 (*entry
)->nr_bytes
= nr_bytes
;
228 (*entry
)->modulo
= modulo
;
229 (*entry
)->buffer
= free_buffer
;
231 /* Record memory unmapping info. */
232 if (mmap_next_fd
>= 0)
234 (*entry
)->munmap_length
= free_length
;
235 close (mmap_next_fd
);
239 (*entry
)->munmap_length
= 0;
245 do_memopt_delete (SIM_DESC sd
,
250 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
252 while ((*entry
) != NULL
253 && ((*entry
)->level
!= level
254 || (*entry
)->space
!= space
255 || (*entry
)->addr
!= addr
))
256 entry
= &(*entry
)->next
;
257 if ((*entry
) == NULL
)
259 sim_io_eprintf (sd
, "Memory at 0x%lx not found, not deleted\n",
263 /* delete any buffer */
264 if ((*entry
)->buffer
!= NULL
)
267 if ((*entry
)->munmap_length
> 0)
268 munmap ((*entry
)->buffer
, (*entry
)->munmap_length
);
271 zfree ((*entry
)->buffer
);
274 /* delete it and its aliases */
276 *entry
= (*entry
)->next
;
277 while (alias
!= NULL
)
279 sim_memopt
*dead
= alias
;
280 alias
= alias
->alias
;
281 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
289 parse_size (char *chp
,
290 address_word
*nr_bytes
,
293 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
294 *nr_bytes
= strtoul (chp
, &chp
, 0);
298 *modulo
= strtoul (chp
+ 1, &chp
, 0);
300 case 'g': case 'G': /* Gigabyte suffix. */
303 case 'm': case 'M': /* Megabyte suffix. */
306 case 'k': case 'K': /* Kilobyte suffix. */
308 /* Check for a modulo specifier after the suffix. */
310 if (* chp
== 'b' || * chp
== 'B')
313 *modulo
= strtoul (chp
+ 1, &chp
, 0);
320 parse_ulong_value (char *chp
,
321 unsigned long *value
)
323 *value
= strtoul (chp
, &chp
, 0);
328 parse_addr (char *chp
,
333 /* [ <space> ": " ] <addr> [ "@" <level> ] */
334 *addr
= (unsigned long) strtoul (chp
, &chp
, 0);
338 *addr
= (unsigned long) strtoul (chp
+ 1, &chp
, 0);
342 *level
= strtoul (chp
+ 1, &chp
, 0);
349 memory_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
350 char *arg
, int is_command
)
355 case OPTION_MEMORY_DELETE
:
356 if (strcasecmp (arg
, "all") == 0)
358 while (STATE_MEMOPT (sd
) != NULL
)
359 do_memopt_delete (sd
,
360 STATE_MEMOPT (sd
)->level
,
361 STATE_MEMOPT (sd
)->space
,
362 STATE_MEMOPT (sd
)->addr
);
369 address_word addr
= 0;
370 parse_addr (arg
, &level
, &space
, &addr
);
371 return do_memopt_delete (sd
, level
, space
, addr
);
374 case OPTION_MEMORY_REGION
:
379 address_word addr
= 0;
380 address_word nr_bytes
= 0;
382 /* parse the arguments */
383 chp
= parse_addr (chp
, &level
, &space
, &addr
);
386 sim_io_eprintf (sd
, "Missing size for memory-region\n");
389 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
392 modulo
= strtoul (chp
+ 1, &chp
, 0);
393 /* try to attach/insert it */
394 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
395 &STATE_MEMOPT (sd
), NULL
);
399 case OPTION_MEMORY_ALIAS
:
404 address_word addr
= 0;
405 address_word nr_bytes
= 0;
408 /* parse the arguments */
409 chp
= parse_addr (chp
, &level
, &space
, &addr
);
412 sim_io_eprintf (sd
, "Missing size for memory-region\n");
415 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
416 /* try to attach/insert the main record */
417 entry
= do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
420 /* now attach all the aliases */
425 address_word a_addr
= addr
;
426 chp
= parse_addr (chp
+ 1, &a_level
, &a_space
, &a_addr
);
427 do_memopt_add (sd
, a_level
, a_space
, a_addr
, nr_bytes
, modulo
,
428 &entry
->alias
, entry
->buffer
);
433 case OPTION_MEMORY_SIZE
:
437 address_word addr
= 0;
438 address_word nr_bytes
= 0;
440 /* parse the arguments */
441 parse_size (arg
, &nr_bytes
, &modulo
);
442 /* try to attach/insert it */
443 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
444 &STATE_MEMOPT (sd
), NULL
);
448 case OPTION_MEMORY_CLEAR
:
450 fill_byte_value
= (unsigned8
) 0;
456 case OPTION_MEMORY_FILL
:
458 unsigned long fill_value
;
459 parse_ulong_value (arg
, &fill_value
);
460 if (fill_value
> 255)
462 sim_io_eprintf (sd
, "Missing fill value between 0 and 255\n");
465 fill_byte_value
= (unsigned8
) fill_value
;
471 case OPTION_MEMORY_MAPFILE
:
473 if (mmap_next_fd
>= 0)
475 sim_io_eprintf (sd
, "Duplicate memory-mapfile option\n");
479 mmap_next_fd
= open (arg
, O_RDWR
);
480 if (mmap_next_fd
< 0)
482 sim_io_eprintf (sd
, "Cannot open file `%s': %s\n",
483 arg
, strerror(errno
));
490 case OPTION_MEMORY_INFO
:
493 sim_io_printf (sd
, "Memory maps:\n");
494 for (entry
= STATE_MEMOPT (sd
); entry
!= NULL
; entry
= entry
->next
)
497 sim_io_printf (sd
, " memory");
498 if (entry
->alias
== NULL
)
499 sim_io_printf (sd
, " region ");
501 sim_io_printf (sd
, " alias ");
502 if (entry
->space
!= 0)
503 sim_io_printf (sd
, "0x%lx:", (long) entry
->space
);
504 sim_io_printf (sd
, "0x%08lx", (long) entry
->addr
);
505 if (entry
->level
!= 0)
506 sim_io_printf (sd
, "@0x%lx", (long) entry
->level
);
507 sim_io_printf (sd
, ",0x%lx",
508 (long) entry
->nr_bytes
);
509 if (entry
->modulo
!= 0)
510 sim_io_printf (sd
, "%%0x%lx", (long) entry
->modulo
);
511 for (alias
= entry
->alias
;
515 if (alias
->space
!= 0)
516 sim_io_printf (sd
, "0x%lx:", (long) alias
->space
);
517 sim_io_printf (sd
, ",0x%08lx", (long) alias
->addr
);
518 if (alias
->level
!= 0)
519 sim_io_printf (sd
, "@0x%lx", (long) alias
->level
);
521 sim_io_printf (sd
, "\n");
527 case OPTION_MAP_INFO
:
529 sim_core
*memory
= STATE_CORE (sd
);
532 for (nr_map
= 0; nr_map
< nr_maps
; ++nr_map
)
534 sim_core_map
*map
= &memory
->common
.map
[nr_map
];
535 sim_core_mapping
*mapping
= map
->first
;
540 sim_io_printf (sd
, "%s maps:\n", map_to_str (nr_map
));
545 sim_io_printf (sd
, " map ");
546 if (mapping
->space
!= 0)
547 sim_io_printf (sd
, "0x%x:", mapping
->space
);
548 sim_io_printf (sd
, "0x%08lx", (long) mapping
->base
);
549 if (mapping
->level
!= 0)
550 sim_io_printf (sd
, "@0x%x", mapping
->level
);
551 sim_io_printf (sd
, ",0x%lx", (long) mapping
->nr_bytes
);
552 modulo
= mapping
->mask
+ 1;
554 sim_io_printf (sd
, "%%0x%x", modulo
);
555 sim_io_printf (sd
, "\n");
557 mapping
= mapping
->next
;
567 sim_io_eprintf (sd
, "Unknown memory option %d\n", opt
);
576 /* "memory" module install handler.
578 This is called via sim_module_install to install the "memory" subsystem
579 into the simulator. */
581 static MODULE_INIT_FN sim_memory_init
;
582 static MODULE_UNINSTALL_FN sim_memory_uninstall
;
585 sim_memopt_install (SIM_DESC sd
)
587 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
588 sim_add_option_table (sd
, NULL
, memory_options
);
589 sim_module_add_uninstall_fn (sd
, sim_memory_uninstall
);
590 sim_module_add_init_fn (sd
, sim_memory_init
);
595 /* Uninstall the "memory" subsystem from the simulator. */
598 sim_memory_uninstall (SIM_DESC sd
)
600 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
603 while ((*entry
) != NULL
)
605 /* delete any buffer */
606 if ((*entry
)->buffer
!= NULL
)
609 if ((*entry
)->munmap_length
> 0)
610 munmap ((*entry
)->buffer
, (*entry
)->munmap_length
);
613 zfree ((*entry
)->buffer
);
616 /* delete it and its aliases */
620 *entry
= (*entry
)->next
;
622 while (alias
!= NULL
)
624 sim_memopt
*dead
= alias
;
625 alias
= alias
->alias
;
626 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
634 sim_memory_init (SIM_DESC sd
)
636 /* Reinitialize option modifier flags, in case they were left
637 over from a previous sim startup event. */