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 /* If filling with non-zero value, do not use clearing allocator. */
128 if (fill_byte_flag
&& fill_byte_value
!= 0)
129 free_buffer
= xmalloc (bytes
); /* don't clear */
131 free_buffer
= zalloc (bytes
); /* clear */
133 aligned_buffer
= (char*) free_buffer
+ padding
;
135 sim_core_attach (sd
, NULL
,
136 level
, access_read_write_exec
, space
,
137 addr
, nr_bytes
, modulo
, NULL
, aligned_buffer
);
139 fill_buffer
= aligned_buffer
;
140 fill_length
= (modulo
== 0) ? nr_bytes
: modulo
;
142 /* If we just used a clearing allocator, and are about to fill with
143 zero, truncate the redundant fill operation. */
145 if (fill_byte_flag
&& fill_byte_value
== 0)
146 fill_length
= 1; /* avoid boundary length=0 case */
151 ASSERT (fill_buffer
!= 0);
152 memset ((char*) fill_buffer
, fill_byte_value
, fill_length
);
155 while ((*entry
) != NULL
)
156 entry
= &(*entry
)->next
;
157 (*entry
) = ZALLOC (sim_memopt
);
158 (*entry
)->level
= level
;
159 (*entry
)->space
= space
;
160 (*entry
)->addr
= addr
;
161 (*entry
)->nr_bytes
= nr_bytes
;
162 (*entry
)->modulo
= modulo
;
163 (*entry
)->buffer
= free_buffer
;
169 do_memopt_delete (SIM_DESC sd
,
174 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
176 while ((*entry
) != NULL
177 && ((*entry
)->level
!= level
178 || (*entry
)->space
!= space
179 || (*entry
)->addr
!= addr
))
180 entry
= &(*entry
)->next
;
181 if ((*entry
) == NULL
)
183 sim_io_eprintf (sd
, "Memory at 0x%lx not found, not deleted\n",
187 /* delete any buffer */
188 if ((*entry
)->buffer
!= NULL
)
189 zfree ((*entry
)->buffer
);
190 /* delete it and its aliases */
192 *entry
= (*entry
)->next
;
193 while (alias
!= NULL
)
195 sim_memopt
*dead
= alias
;
196 alias
= alias
->alias
;
197 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
205 parse_size (char *chp
,
206 address_word
*nr_bytes
,
209 /* <nr_bytes> [ "%" <modulo> ] */
210 *nr_bytes
= strtoul (chp
, &chp
, 0);
213 *modulo
= strtoul (chp
+ 1, &chp
, 0);
219 parse_ulong_value (char *chp
,
220 unsigned long *value
)
222 *value
= strtoul (chp
, &chp
, 0);
227 parse_addr (char *chp
,
232 /* [ <space> ": " ] <addr> [ "@" <level> ] */
233 *addr
= (unsigned long) strtoul (chp
, &chp
, 0);
237 *addr
= (unsigned long) strtoul (chp
+ 1, &chp
, 0);
241 *level
= strtoul (chp
+ 1, &chp
, 0);
248 memory_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
249 char *arg
, int is_command
)
254 case OPTION_MEMORY_DELETE
:
255 if (strcasecmp (arg
, "all") == 0)
257 while (STATE_MEMOPT (sd
) != NULL
)
258 do_memopt_delete (sd
,
259 STATE_MEMOPT (sd
)->level
,
260 STATE_MEMOPT (sd
)->space
,
261 STATE_MEMOPT (sd
)->addr
);
268 address_word addr
= 0;
269 parse_addr (arg
, &level
, &space
, &addr
);
270 return do_memopt_delete (sd
, level
, space
, addr
);
273 case OPTION_MEMORY_REGION
:
278 address_word addr
= 0;
279 address_word nr_bytes
= 0;
281 /* parse the arguments */
282 chp
= parse_addr (chp
, &level
, &space
, &addr
);
285 sim_io_eprintf (sd
, "Missing size for memory-region\n");
288 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
291 modulo
= strtoul (chp
+ 1, &chp
, 0);
292 /* try to attach/insert it */
293 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
294 &STATE_MEMOPT (sd
), NULL
);
298 case OPTION_MEMORY_ALIAS
:
303 address_word addr
= 0;
304 address_word nr_bytes
= 0;
307 /* parse the arguments */
308 chp
= parse_addr (chp
, &level
, &space
, &addr
);
311 sim_io_eprintf (sd
, "Missing size for memory-region\n");
314 chp
= parse_size (chp
+ 1, &nr_bytes
, &modulo
);
315 /* try to attach/insert the main record */
316 entry
= do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
319 /* now attach all the aliases */
324 address_word a_addr
= addr
;
325 chp
= parse_addr (chp
+ 1, &a_level
, &a_space
, &a_addr
);
326 do_memopt_add (sd
, a_level
, a_space
, a_addr
, nr_bytes
, modulo
,
327 &entry
->alias
, entry
->buffer
);
332 case OPTION_MEMORY_SIZE
:
336 address_word addr
= 0;
337 address_word nr_bytes
= 0;
339 /* parse the arguments */
340 parse_size (arg
, &nr_bytes
, &modulo
);
341 /* try to attach/insert it */
342 do_memopt_add (sd
, level
, space
, addr
, nr_bytes
, modulo
,
343 &STATE_MEMOPT (sd
), NULL
);
347 case OPTION_MEMORY_CLEAR
:
349 fill_byte_value
= (unsigned8
) 0;
355 case OPTION_MEMORY_FILL
:
357 unsigned long fill_value
;
358 parse_ulong_value (arg
, &fill_value
);
359 if (fill_value
> 255)
361 sim_io_eprintf (sd
, "Missing fill value between 0 and 255\n");
364 fill_byte_value
= (unsigned8
) fill_value
;
370 case OPTION_MEMORY_INFO
:
373 sim_io_printf (sd
, "Memory maps:\n");
374 for (entry
= STATE_MEMOPT (sd
); entry
!= NULL
; entry
= entry
->next
)
377 sim_io_printf (sd
, " memory");
378 if (entry
->alias
== NULL
)
379 sim_io_printf (sd
, " region ");
381 sim_io_printf (sd
, " alias ");
382 if (entry
->space
!= 0)
383 sim_io_printf (sd
, "0x%lx:", (long) entry
->space
);
384 sim_io_printf (sd
, "0x%08lx", (long) entry
->addr
);
385 if (entry
->level
!= 0)
386 sim_io_printf (sd
, "@0x%lx", (long) entry
->level
);
387 sim_io_printf (sd
, ",0x%lx",
388 (long) entry
->nr_bytes
);
389 if (entry
->modulo
!= 0)
390 sim_io_printf (sd
, "%%0x%lx", (long) entry
->modulo
);
391 for (alias
= entry
->alias
;
395 if (alias
->space
!= 0)
396 sim_io_printf (sd
, "0x%lx:", (long) alias
->space
);
397 sim_io_printf (sd
, ",0x%08lx", (long) alias
->addr
);
398 if (alias
->level
!= 0)
399 sim_io_printf (sd
, "@0x%lx", (long) alias
->level
);
401 sim_io_printf (sd
, "\n");
408 sim_io_eprintf (sd
, "Unknown memory option %d\n", opt
);
417 /* "memory" module install handler.
419 This is called via sim_module_install to install the "memory" subsystem
420 into the simulator. */
422 static MODULE_INIT_FN sim_memory_init
;
423 static MODULE_UNINSTALL_FN sim_memory_uninstall
;
426 sim_memopt_install (SIM_DESC sd
)
428 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
429 sim_add_option_table (sd
, NULL
, memory_options
);
430 sim_module_add_uninstall_fn (sd
, sim_memory_uninstall
);
431 sim_module_add_init_fn (sd
, sim_memory_init
);
436 /* Uninstall the "memory" subsystem from the simulator. */
439 sim_memory_uninstall (SIM_DESC sd
)
441 sim_memopt
**entry
= &STATE_MEMOPT (sd
);
444 while ((*entry
) != NULL
)
446 /* delete any buffer */
447 if ((*entry
)->buffer
!= NULL
)
448 zfree ((*entry
)->buffer
);
450 /* delete it and its aliases */
454 *entry
= (*entry
)->next
;
456 while (alias
!= NULL
)
458 sim_memopt
*dead
= alias
;
459 alias
= alias
->alias
;
460 sim_core_detach (sd
, NULL
, dead
->level
, dead
->space
, dead
->addr
);
468 sim_memory_init (SIM_DESC sd
)
470 /* FIXME: anything needed? */