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. */
22 #include "sim-assert.h"
23 #include "sim-options.h"
36 /* Memory fill byte */
37 static unsigned8 fill_byte_value
;
38 static int fill_byte_flag
= 0;
40 /* Memory command line options. */
43 OPTION_MEMORY_DELETE
= OPTION_START
,
52 static DECLARE_OPTION_HANDLER (memory_option_handler
);
54 static const OPTION memory_options
[] =
56 { {"memory-delete", required_argument
, NULL
, OPTION_MEMORY_DELETE
},
57 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
58 memory_option_handler
},
59 { {"delete-memory", required_argument
, NULL
, OPTION_MEMORY_DELETE
},
60 '\0', "ADDRESS", NULL
,
61 memory_option_handler
},
63 { {"memory-region", required_argument
, NULL
, OPTION_MEMORY_REGION
},
64 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
65 memory_option_handler
},
67 { {"memory-alias", required_argument
, NULL
, OPTION_MEMORY_ALIAS
},
68 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
69 memory_option_handler
},
71 { {"memory-size", required_argument
, NULL
, OPTION_MEMORY_SIZE
},
72 '\0', "SIZE", "Add memory at address zero",
73 memory_option_handler
},
75 { {"memory-fill", required_argument
, NULL
, OPTION_MEMORY_FILL
},
76 '\0', "VALUE", "Fill subsequently added memory regions",
77 memory_option_handler
},
79 { {"memory-clear", no_argument
, NULL
, OPTION_MEMORY_CLEAR
},
80 '\0', NULL
, "Clear subsequently added memory regions",
81 memory_option_handler
},
83 { {"memory-info", no_argument
, NULL
, OPTION_MEMORY_INFO
},
84 '\0', NULL
, "List configurable memory regions",
85 memory_option_handler
},
86 { {"info-memory", no_argument
, NULL
, OPTION_MEMORY_INFO
},
88 memory_option_handler
},
90 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
95 do_memopt_add (SIM_DESC sd
,
99 address_word nr_bytes
,
105 unsigned fill_length
;
110 /* Buffer already given. sim_memory_uninstall will free it. */
111 sim_core_attach (sd
, NULL
,
112 level
, access_read_write_exec
, space
,
113 addr
, nr_bytes
, modulo
, NULL
, buffer
);
115 free_buffer
= buffer
;
116 fill_buffer
= buffer
;
117 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
121 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
122 void *aligned_buffer
;
123 int padding
= (addr
% sizeof (unsigned64
));
124 unsigned long bytes
= (modulo
== 0 ? nr_bytes
: modulo
) + padding
;
126 free_buffer
= zalloc (bytes
);
127 aligned_buffer
= (char*) free_buffer
+ padding
;
129 sim_core_attach (sd
, NULL
,
130 level
, access_read_write_exec
, space
,
131 addr
, nr_bytes
, modulo
, NULL
, aligned_buffer
);
133 fill_buffer
= aligned_buffer
;
134 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
139 ASSERT (fill_buffer
!= 0);
140 memset ((char*) fill_buffer
, fill_byte_value
, fill_length
);
143 while ((*entry
) != NULL
)
144 entry
= &(*entry
)->next
;
145 (*entry
) = ZALLOC (sim_memopt
);
146 (*entry
)->level
= level
;
147 (*entry
)->space
= space
;
148 (*entry
)->addr
= addr
;
149 (*entry
)->nr_bytes
= nr_bytes
;
150 (*entry
)->modulo
= modulo
;
151 (*entry
)->buffer
= free_buffer
;
157 do_memopt_delete (SIM_DESC sd
,
162 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
164 while ((*entry
) != NULL
165 && ((*entry
)->level
!= level
166 || (*entry
)->space
!= space
167 || (*entry
)->addr
!= addr
))
168 entry
= &(*entry
)->next
;
169 if ((*entry
) == NULL
)
171 sim_io_eprintf (sd
, "Memory at 0x%lx not found, not deleted\n",
175 /* delete any buffer */
176 if ((*entry
)->buffer
!= NULL
)
177 zfree ((*entry
)->buffer
);
178 /* delete it and its aliases */
180 *entry
= (*entry
)->next
;
181 while (alias
!= NULL
)
183 sim_memopt
*dead
= alias
;
184 alias
= alias
->alias
;
185 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
193 parse_size (char *chp
,
194 address_word
*nr_bytes
,
197 /* <nr_bytes> [ "%" <modulo> ] */
198 *nr_bytes
= strtoul (chp
, &chp
, 0);
201 *modulo
= strtoul (chp
+ 1, &chp
, 0);
207 parse_ulong_value (char *chp
,
208 unsigned long *value
)
210 *value
= strtoul (chp
, &chp
, 0);
215 parse_addr (char *chp
,
220 /* [ <space> ": " ] <addr> [ "@" <level> ] */
221 *addr
= (unsigned long) strtoul (chp
, &chp
, 0);
225 *addr
= (unsigned long) strtoul (chp
+ 1, &chp
, 0);
229 *level
= strtoul (chp
+ 1, &chp
, 0);
236 memory_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
237 char *arg
, int is_command
)
242 case OPTION_MEMORY_DELETE
:
243 if (strcasecmp (arg
, "all") == 0)
245 while (STATE_MEMOPT (sd
) != NULL
)
246 do_memopt_delete (sd
,
247 STATE_MEMOPT (sd
)->level
,
248 STATE_MEMOPT (sd
)->space
,
249 STATE_MEMOPT (sd
)->addr
);
256 address_word addr
= 0;
257 parse_addr (arg
, &level
, &space
, &addr
);
258 return do_memopt_delete (sd
, level
, space
, addr
);
261 case OPTION_MEMORY_REGION
:
266 address_word addr
= 0;
267 address_word nr_bytes
= 0;
269 /* parse the arguments */
270 chp
= parse_addr (chp
, &level
, &space
, &addr
);
273 sim_io_eprintf (sd
, "Missing size for memory-region\n");
276 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
279 modulo
= strtoul (chp
+ 1, &chp
, 0);
280 /* try to attach/insert it */
281 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
282 &STATE_MEMOPT (sd
), NULL
);
286 case OPTION_MEMORY_ALIAS
:
291 address_word addr
= 0;
292 address_word nr_bytes
= 0;
295 /* parse the arguments */
296 chp
= parse_addr (chp
, &level
, &space
, &addr
);
299 sim_io_eprintf (sd
, "Missing size for memory-region\n");
302 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
303 /* try to attach/insert the main record */
304 entry
= do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
307 /* now attach all the aliases */
312 address_word a_addr
= addr
;
313 chp
= parse_addr (chp
+ 1, &a_level
, &a_space
, &a_addr
);
314 do_memopt_add (sd
, a_level
, a_space
, a_addr
, nr_bytes
, modulo
,
315 &entry
->alias
, entry
->buffer
);
320 case OPTION_MEMORY_SIZE
:
324 address_word addr
= 0;
325 address_word nr_bytes
= 0;
327 /* parse the arguments */
328 parse_size (arg
, &nr_bytes
, &modulo
);
329 /* try to attach/insert it */
330 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
331 &STATE_MEMOPT (sd
), NULL
);
335 case OPTION_MEMORY_CLEAR
:
337 fill_byte_value
= (unsigned8
) 0;
343 case OPTION_MEMORY_FILL
:
345 unsigned long fill_value
;
346 parse_ulong_value (arg
, &fill_value
);
347 if (fill_value
> 255)
349 sim_io_eprintf (sd
, "Missing fill value between 0 and 255\n");
352 fill_byte_value
= (unsigned8
) fill_value
;
358 case OPTION_MEMORY_INFO
:
361 sim_io_printf (sd
, "Memory maps:\n");
362 for (entry
= STATE_MEMOPT (sd
); entry
!= NULL
; entry
= entry
->next
)
365 sim_io_printf (sd
, " memory");
366 if (entry
->alias
== NULL
)
367 sim_io_printf (sd
, " region ");
369 sim_io_printf (sd
, " alias ");
370 if (entry
->space
!= 0)
371 sim_io_printf (sd
, "0x%lx:", (long) entry
->space
);
372 sim_io_printf (sd
, "0x%08lx", (long) entry
->addr
);
373 if (entry
->level
!= 0)
374 sim_io_printf (sd
, "@0x%lx", (long) entry
->level
);
375 sim_io_printf (sd
, ",0x%lx",
376 (long) entry
->nr_bytes
);
377 if (entry
->modulo
!= 0)
378 sim_io_printf (sd
, "%%0x%lx", (long) entry
->modulo
);
379 for (alias
= entry
->alias
;
383 if (alias
->space
!= 0)
384 sim_io_printf (sd
, "0x%lx:", (long) alias
->space
);
385 sim_io_printf (sd
, ",0x%08lx", (long) alias
->addr
);
386 if (alias
->level
!= 0)
387 sim_io_printf (sd
, "@0x%lx", (long) alias
->level
);
389 sim_io_printf (sd
, "\n");
396 sim_io_eprintf (sd
, "Unknown memory option %d\n", opt
);
405 /* "memory" module install handler.
407 This is called via sim_module_install to install the "memory" subsystem
408 into the simulator. */
410 static MODULE_INIT_FN sim_memory_init
;
411 static MODULE_UNINSTALL_FN sim_memory_uninstall
;
414 sim_memopt_install (SIM_DESC sd
)
416 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
417 sim_add_option_table (sd
, NULL
, memory_options
);
418 sim_module_add_uninstall_fn (sd
, sim_memory_uninstall
);
419 sim_module_add_init_fn (sd
, sim_memory_init
);
424 /* Uninstall the "memory" subsystem from the simulator. */
427 sim_memory_uninstall (SIM_DESC sd
)
429 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
432 while ((*entry
) != NULL
)
434 /* delete any buffer */
435 if ((*entry
)->buffer
!= NULL
)
436 zfree ((*entry
)->buffer
);
438 /* delete it and its aliases */
440 while (alias
!= NULL
)
442 sim_memopt
*dead
= alias
;
443 alias
= alias
->alias
;
444 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
449 *entry
= (*entry
)->next
;
455 sim_memory_init (SIM_DESC sd
)
457 /* FIXME: anything needed? */