1 /* Simulator memory option handling.
2 Copyright (C) 1996-1999, 2007, 2008, 2009 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"
42 #ifdef HAVE_SYS_MMAN_H
45 #ifdef HAVE_SYS_STAT_H
52 /* Memory fill byte. */
53 static unsigned8 fill_byte_value
;
54 static int fill_byte_flag
= 0;
56 /* Memory mapping; see OPTION_MEMORY_MAPFILE. */
57 static int mmap_next_fd
= -1;
59 /* Memory command line options. */
62 OPTION_MEMORY_DELETE
= OPTION_START
,
72 static DECLARE_OPTION_HANDLER (memory_option_handler
);
74 static const OPTION memory_options
[] =
76 { {"memory-delete", required_argument
, NULL
, OPTION_MEMORY_DELETE
},
77 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
78 memory_option_handler
},
79 { {"delete-memory", required_argument
, NULL
, OPTION_MEMORY_DELETE
},
80 '\0', "ADDRESS", NULL
,
81 memory_option_handler
},
83 { {"memory-region", required_argument
, NULL
, OPTION_MEMORY_REGION
},
84 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
85 memory_option_handler
},
87 { {"memory-alias", required_argument
, NULL
, OPTION_MEMORY_ALIAS
},
88 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
89 memory_option_handler
},
91 { {"memory-size", required_argument
, NULL
, OPTION_MEMORY_SIZE
},
92 '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
93 "Add memory at address zero", memory_option_handler
},
95 { {"memory-fill", required_argument
, NULL
, OPTION_MEMORY_FILL
},
96 '\0', "VALUE", "Fill subsequently added memory regions",
97 memory_option_handler
},
99 { {"memory-clear", no_argument
, NULL
, OPTION_MEMORY_CLEAR
},
100 '\0', NULL
, "Clear subsequently added memory regions",
101 memory_option_handler
},
103 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
104 { {"memory-mapfile", required_argument
, NULL
, OPTION_MEMORY_MAPFILE
},
105 '\0', "FILE", "Memory-map next memory region from file",
106 memory_option_handler
},
109 { {"memory-info", no_argument
, NULL
, OPTION_MEMORY_INFO
},
110 '\0', NULL
, "List configurable memory regions",
111 memory_option_handler
},
112 { {"info-memory", no_argument
, NULL
, OPTION_MEMORY_INFO
},
114 memory_option_handler
},
116 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
121 do_memopt_add (SIM_DESC sd
,
125 address_word nr_bytes
,
131 unsigned fill_length
;
133 unsigned long free_length
;
137 /* Buffer already given. sim_memory_uninstall will free it. */
138 sim_core_attach (sd
, NULL
,
139 level
, access_read_write_exec
, space
,
140 addr
, nr_bytes
, modulo
, NULL
, buffer
);
142 free_buffer
= buffer
;
144 fill_buffer
= buffer
;
145 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
149 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
150 void *aligned_buffer
;
151 int padding
= (addr
% sizeof (unsigned64
));
152 unsigned long bytes
= (modulo
== 0 ? nr_bytes
: modulo
) + padding
;
158 /* Memory map or malloc(). */
159 if (mmap_next_fd
>= 0)
161 /* Check that given file is big enough. */
165 /* Some kernels will SIGBUS the application if mmap'd file
166 is not large enough. */
167 rc
= fstat (mmap_next_fd
, &s
);
168 if (rc
< 0 || s
.st_size
< bytes
)
171 "Error, cannot confirm that mmap file is large enough "
172 "(>= %ld bytes)\n", bytes
);
175 free_buffer
= mmap (0, bytes
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, mmap_next_fd
, 0);
176 if (free_buffer
== 0 || free_buffer
== (char*)-1) /* MAP_FAILED */
178 sim_io_error (sd
, "Error, cannot mmap file (%s).\n",
184 /* Need heap allocation? */
185 if (free_buffer
== NULL
)
187 /* If filling with non-zero value, do not use clearing allocator. */
188 if (fill_byte_flag
&& fill_byte_value
!= 0)
189 free_buffer
= xmalloc (bytes
); /* don't clear */
191 free_buffer
= zalloc (bytes
); /* clear */
194 aligned_buffer
= (char*) free_buffer
+ padding
;
196 sim_core_attach (sd
, NULL
,
197 level
, access_read_write_exec
, space
,
198 addr
, nr_bytes
, modulo
, NULL
, aligned_buffer
);
200 fill_buffer
= aligned_buffer
;
201 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
203 /* If we just used a clearing allocator, and are about to fill with
204 zero, truncate the redundant fill operation. */
206 if (fill_byte_flag
&& fill_byte_value
== 0)
207 fill_length
= 1; /* avoid boundary length=0 case */
212 ASSERT (fill_buffer
!= 0);
213 memset ((char*) fill_buffer
, fill_byte_value
, fill_length
);
216 while ((*entry
) != NULL
)
217 entry
= &(*entry
)->next
;
218 (*entry
) = ZALLOC (sim_memopt
);
219 (*entry
)->level
= level
;
220 (*entry
)->space
= space
;
221 (*entry
)->addr
= addr
;
222 (*entry
)->nr_bytes
= nr_bytes
;
223 (*entry
)->modulo
= modulo
;
224 (*entry
)->buffer
= free_buffer
;
226 /* Record memory unmapping info. */
227 if (mmap_next_fd
>= 0)
229 (*entry
)->munmap_length
= free_length
;
230 close (mmap_next_fd
);
234 (*entry
)->munmap_length
= 0;
240 do_memopt_delete (SIM_DESC sd
,
245 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
247 while ((*entry
) != NULL
248 && ((*entry
)->level
!= level
249 || (*entry
)->space
!= space
250 || (*entry
)->addr
!= addr
))
251 entry
= &(*entry
)->next
;
252 if ((*entry
) == NULL
)
254 sim_io_eprintf (sd
, "Memory at 0x%lx not found, not deleted\n",
258 /* delete any buffer */
259 if ((*entry
)->buffer
!= NULL
)
262 if ((*entry
)->munmap_length
> 0)
263 munmap ((*entry
)->buffer
, (*entry
)->munmap_length
);
266 zfree ((*entry
)->buffer
);
269 /* delete it and its aliases */
271 *entry
= (*entry
)->next
;
272 while (alias
!= NULL
)
274 sim_memopt
*dead
= alias
;
275 alias
= alias
->alias
;
276 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
284 parse_size (char *chp
,
285 address_word
*nr_bytes
,
288 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
289 *nr_bytes
= strtoul (chp
, &chp
, 0);
293 *modulo
= strtoul (chp
+ 1, &chp
, 0);
295 case 'g': case 'G': /* Gigabyte suffix. */
298 case 'm': case 'M': /* Megabyte suffix. */
301 case 'k': case 'K': /* Kilobyte suffix. */
303 /* Check for a modulo specifier after the suffix. */
305 if (* chp
== 'b' || * chp
== 'B')
308 *modulo
= strtoul (chp
+ 1, &chp
, 0);
315 parse_ulong_value (char *chp
,
316 unsigned long *value
)
318 *value
= strtoul (chp
, &chp
, 0);
323 parse_addr (char *chp
,
328 /* [ <space> ": " ] <addr> [ "@" <level> ] */
329 *addr
= (unsigned long) strtoul (chp
, &chp
, 0);
333 *addr
= (unsigned long) strtoul (chp
+ 1, &chp
, 0);
337 *level
= strtoul (chp
+ 1, &chp
, 0);
344 memory_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
345 char *arg
, int is_command
)
350 case OPTION_MEMORY_DELETE
:
351 if (strcasecmp (arg
, "all") == 0)
353 while (STATE_MEMOPT (sd
) != NULL
)
354 do_memopt_delete (sd
,
355 STATE_MEMOPT (sd
)->level
,
356 STATE_MEMOPT (sd
)->space
,
357 STATE_MEMOPT (sd
)->addr
);
364 address_word addr
= 0;
365 parse_addr (arg
, &level
, &space
, &addr
);
366 return do_memopt_delete (sd
, level
, space
, addr
);
369 case OPTION_MEMORY_REGION
:
374 address_word addr
= 0;
375 address_word nr_bytes
= 0;
377 /* parse the arguments */
378 chp
= parse_addr (chp
, &level
, &space
, &addr
);
381 sim_io_eprintf (sd
, "Missing size for memory-region\n");
384 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
387 modulo
= strtoul (chp
+ 1, &chp
, 0);
388 /* try to attach/insert it */
389 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
390 &STATE_MEMOPT (sd
), NULL
);
394 case OPTION_MEMORY_ALIAS
:
399 address_word addr
= 0;
400 address_word nr_bytes
= 0;
403 /* parse the arguments */
404 chp
= parse_addr (chp
, &level
, &space
, &addr
);
407 sim_io_eprintf (sd
, "Missing size for memory-region\n");
410 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
411 /* try to attach/insert the main record */
412 entry
= do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
415 /* now attach all the aliases */
420 address_word a_addr
= addr
;
421 chp
= parse_addr (chp
+ 1, &a_level
, &a_space
, &a_addr
);
422 do_memopt_add (sd
, a_level
, a_space
, a_addr
, nr_bytes
, modulo
,
423 &entry
->alias
, entry
->buffer
);
428 case OPTION_MEMORY_SIZE
:
432 address_word addr
= 0;
433 address_word nr_bytes
= 0;
435 /* parse the arguments */
436 parse_size (arg
, &nr_bytes
, &modulo
);
437 /* try to attach/insert it */
438 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
439 &STATE_MEMOPT (sd
), NULL
);
443 case OPTION_MEMORY_CLEAR
:
445 fill_byte_value
= (unsigned8
) 0;
451 case OPTION_MEMORY_FILL
:
453 unsigned long fill_value
;
454 parse_ulong_value (arg
, &fill_value
);
455 if (fill_value
> 255)
457 sim_io_eprintf (sd
, "Missing fill value between 0 and 255\n");
460 fill_byte_value
= (unsigned8
) fill_value
;
466 case OPTION_MEMORY_MAPFILE
:
468 if (mmap_next_fd
>= 0)
470 sim_io_eprintf (sd
, "Duplicate memory-mapfile option\n");
474 mmap_next_fd
= open (arg
, O_RDWR
);
475 if (mmap_next_fd
< 0)
477 sim_io_eprintf (sd
, "Cannot open file `%s': %s\n",
478 arg
, strerror(errno
));
485 case OPTION_MEMORY_INFO
:
488 sim_io_printf (sd
, "Memory maps:\n");
489 for (entry
= STATE_MEMOPT (sd
); entry
!= NULL
; entry
= entry
->next
)
492 sim_io_printf (sd
, " memory");
493 if (entry
->alias
== NULL
)
494 sim_io_printf (sd
, " region ");
496 sim_io_printf (sd
, " alias ");
497 if (entry
->space
!= 0)
498 sim_io_printf (sd
, "0x%lx:", (long) entry
->space
);
499 sim_io_printf (sd
, "0x%08lx", (long) entry
->addr
);
500 if (entry
->level
!= 0)
501 sim_io_printf (sd
, "@0x%lx", (long) entry
->level
);
502 sim_io_printf (sd
, ",0x%lx",
503 (long) entry
->nr_bytes
);
504 if (entry
->modulo
!= 0)
505 sim_io_printf (sd
, "%%0x%lx", (long) entry
->modulo
);
506 for (alias
= entry
->alias
;
510 if (alias
->space
!= 0)
511 sim_io_printf (sd
, "0x%lx:", (long) alias
->space
);
512 sim_io_printf (sd
, ",0x%08lx", (long) alias
->addr
);
513 if (alias
->level
!= 0)
514 sim_io_printf (sd
, "@0x%lx", (long) alias
->level
);
516 sim_io_printf (sd
, "\n");
523 sim_io_eprintf (sd
, "Unknown memory option %d\n", opt
);
532 /* "memory" module install handler.
534 This is called via sim_module_install to install the "memory" subsystem
535 into the simulator. */
537 static MODULE_INIT_FN sim_memory_init
;
538 static MODULE_UNINSTALL_FN sim_memory_uninstall
;
541 sim_memopt_install (SIM_DESC sd
)
543 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
544 sim_add_option_table (sd
, NULL
, memory_options
);
545 sim_module_add_uninstall_fn (sd
, sim_memory_uninstall
);
546 sim_module_add_init_fn (sd
, sim_memory_init
);
551 /* Uninstall the "memory" subsystem from the simulator. */
554 sim_memory_uninstall (SIM_DESC sd
)
556 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
559 while ((*entry
) != NULL
)
561 /* delete any buffer */
562 if ((*entry
)->buffer
!= NULL
)
565 if ((*entry
)->munmap_length
> 0)
566 munmap ((*entry
)->buffer
, (*entry
)->munmap_length
);
569 zfree ((*entry
)->buffer
);
572 /* delete it and its aliases */
576 *entry
= (*entry
)->next
;
578 while (alias
!= NULL
)
580 sim_memopt
*dead
= alias
;
581 alias
= alias
->alias
;
582 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
590 sim_memory_init (SIM_DESC sd
)
592 /* Reinitialize option modifier flags, in case they were left
593 over from a previous sim startup event. */