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
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", "Add memory at address zero",
94 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> [ "%" <modulo> ] */
290 *nr_bytes
= strtoul (chp
, &chp
, 0);
293 *modulo
= strtoul (chp
+ 1, &chp
, 0);
299 parse_ulong_value (char *chp
,
300 unsigned long *value
)
302 *value
= strtoul (chp
, &chp
, 0);
307 parse_addr (char *chp
,
312 /* [ <space> ": " ] <addr> [ "@" <level> ] */
313 *addr
= (unsigned long) strtoul (chp
, &chp
, 0);
317 *addr
= (unsigned long) strtoul (chp
+ 1, &chp
, 0);
321 *level
= strtoul (chp
+ 1, &chp
, 0);
328 memory_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
329 char *arg
, int is_command
)
334 case OPTION_MEMORY_DELETE
:
335 if (strcasecmp (arg
, "all") == 0)
337 while (STATE_MEMOPT (sd
) != NULL
)
338 do_memopt_delete (sd
,
339 STATE_MEMOPT (sd
)->level
,
340 STATE_MEMOPT (sd
)->space
,
341 STATE_MEMOPT (sd
)->addr
);
348 address_word addr
= 0;
349 parse_addr (arg
, &level
, &space
, &addr
);
350 return do_memopt_delete (sd
, level
, space
, addr
);
353 case OPTION_MEMORY_REGION
:
358 address_word addr
= 0;
359 address_word nr_bytes
= 0;
361 /* parse the arguments */
362 chp
= parse_addr (chp
, &level
, &space
, &addr
);
365 sim_io_eprintf (sd
, "Missing size for memory-region\n");
368 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
371 modulo
= strtoul (chp
+ 1, &chp
, 0);
372 /* try to attach/insert it */
373 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
374 &STATE_MEMOPT (sd
), NULL
);
378 case OPTION_MEMORY_ALIAS
:
383 address_word addr
= 0;
384 address_word nr_bytes
= 0;
387 /* parse the arguments */
388 chp
= parse_addr (chp
, &level
, &space
, &addr
);
391 sim_io_eprintf (sd
, "Missing size for memory-region\n");
394 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
395 /* try to attach/insert the main record */
396 entry
= do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
399 /* now attach all the aliases */
404 address_word a_addr
= addr
;
405 chp
= parse_addr (chp
+ 1, &a_level
, &a_space
, &a_addr
);
406 do_memopt_add (sd
, a_level
, a_space
, a_addr
, nr_bytes
, modulo
,
407 &entry
->alias
, entry
->buffer
);
412 case OPTION_MEMORY_SIZE
:
416 address_word addr
= 0;
417 address_word nr_bytes
= 0;
419 /* parse the arguments */
420 parse_size (arg
, &nr_bytes
, &modulo
);
421 /* try to attach/insert it */
422 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
423 &STATE_MEMOPT (sd
), NULL
);
427 case OPTION_MEMORY_CLEAR
:
429 fill_byte_value
= (unsigned8
) 0;
435 case OPTION_MEMORY_FILL
:
437 unsigned long fill_value
;
438 parse_ulong_value (arg
, &fill_value
);
439 if (fill_value
> 255)
441 sim_io_eprintf (sd
, "Missing fill value between 0 and 255\n");
444 fill_byte_value
= (unsigned8
) fill_value
;
450 case OPTION_MEMORY_MAPFILE
:
452 if (mmap_next_fd
>= 0)
454 sim_io_eprintf (sd
, "Duplicate memory-mapfile option\n");
458 mmap_next_fd
= open (arg
, O_RDWR
);
459 if (mmap_next_fd
< 0)
461 sim_io_eprintf (sd
, "Cannot open file `%s': %s\n",
462 arg
, strerror(errno
));
469 case OPTION_MEMORY_INFO
:
472 sim_io_printf (sd
, "Memory maps:\n");
473 for (entry
= STATE_MEMOPT (sd
); entry
!= NULL
; entry
= entry
->next
)
476 sim_io_printf (sd
, " memory");
477 if (entry
->alias
== NULL
)
478 sim_io_printf (sd
, " region ");
480 sim_io_printf (sd
, " alias ");
481 if (entry
->space
!= 0)
482 sim_io_printf (sd
, "0x%lx:", (long) entry
->space
);
483 sim_io_printf (sd
, "0x%08lx", (long) entry
->addr
);
484 if (entry
->level
!= 0)
485 sim_io_printf (sd
, "@0x%lx", (long) entry
->level
);
486 sim_io_printf (sd
, ",0x%lx",
487 (long) entry
->nr_bytes
);
488 if (entry
->modulo
!= 0)
489 sim_io_printf (sd
, "%%0x%lx", (long) entry
->modulo
);
490 for (alias
= entry
->alias
;
494 if (alias
->space
!= 0)
495 sim_io_printf (sd
, "0x%lx:", (long) alias
->space
);
496 sim_io_printf (sd
, ",0x%08lx", (long) alias
->addr
);
497 if (alias
->level
!= 0)
498 sim_io_printf (sd
, "@0x%lx", (long) alias
->level
);
500 sim_io_printf (sd
, "\n");
507 sim_io_eprintf (sd
, "Unknown memory option %d\n", opt
);
516 /* "memory" module install handler.
518 This is called via sim_module_install to install the "memory" subsystem
519 into the simulator. */
521 static MODULE_INIT_FN sim_memory_init
;
522 static MODULE_UNINSTALL_FN sim_memory_uninstall
;
525 sim_memopt_install (SIM_DESC sd
)
527 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
528 sim_add_option_table (sd
, NULL
, memory_options
);
529 sim_module_add_uninstall_fn (sd
, sim_memory_uninstall
);
530 sim_module_add_init_fn (sd
, sim_memory_init
);
535 /* Uninstall the "memory" subsystem from the simulator. */
538 sim_memory_uninstall (SIM_DESC sd
)
540 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
543 while ((*entry
) != NULL
)
545 /* delete any buffer */
546 if ((*entry
)->buffer
!= NULL
)
549 if ((*entry
)->munmap_length
> 0)
550 munmap ((*entry
)->buffer
, (*entry
)->munmap_length
);
553 zfree ((*entry
)->buffer
);
556 /* delete it and its aliases */
560 *entry
= (*entry
)->next
;
562 while (alias
!= NULL
)
564 sim_memopt
*dead
= alias
;
565 alias
= alias
->alias
;
566 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
574 sim_memory_init (SIM_DESC sd
)
576 /* Reinitialize option modifier flags, in case they were left
577 over from a previous sim startup event. */