* Implement --memory-fill and fix --memory-clear options,
[external/binutils.git] / sim / common / sim-memopt.c
1 /* Simulator memory option handling.
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
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)
10 any later version.
11
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.
16
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.  */
20
21 #include "sim-main.h"
22 #include "sim-assert.h"
23 #include "sim-options.h"
24
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #else
28 #ifdef HAVE_STRINGS_H
29 #include <strings.h>
30 #endif
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35
36 /* Memory fill byte */
37 static unsigned8 fill_byte_value;
38 static int fill_byte_flag = 0;
39
40 /* Memory command line options. */
41
42 enum {
43   OPTION_MEMORY_DELETE = OPTION_START,
44   OPTION_MEMORY_REGION,
45   OPTION_MEMORY_SIZE,
46   OPTION_MEMORY_INFO,
47   OPTION_MEMORY_ALIAS,
48   OPTION_MEMORY_CLEAR,
49   OPTION_MEMORY_FILL
50 };
51
52 static DECLARE_OPTION_HANDLER (memory_option_handler);
53
54 static const OPTION memory_options[] =
55 {
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 },
62
63   { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
64       '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
65       memory_option_handler },
66
67   { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
68       '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
69       memory_option_handler },
70
71   { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
72       '\0', "SIZE", "Add memory at address zero",
73       memory_option_handler },
74
75   { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
76       '\0', "VALUE", "Fill subsequently added memory regions",
77       memory_option_handler },
78
79   { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
80       '\0', NULL, "Clear subsequently added memory regions",
81       memory_option_handler },
82
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 },
87       '\0', NULL, NULL,
88       memory_option_handler },
89
90   { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
91 };
92
93
94 static sim_memopt *
95 do_memopt_add (SIM_DESC sd,
96                int level,
97                int space,
98                address_word addr,
99                address_word nr_bytes,
100                unsigned modulo,
101                sim_memopt **entry,
102                void *buffer)
103 {
104   void *fill_buffer;
105   unsigned fill_length;
106   void *free_buffer;
107
108   if (buffer != NULL)
109     {
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);
114
115       free_buffer = buffer;
116       fill_buffer = buffer;
117       fill_length = (modulo == 0) ? nr_bytes : modulo;
118     }
119   else
120     {
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;
125
126       free_buffer = zalloc (bytes);
127       aligned_buffer = (char*) free_buffer + padding;
128
129       sim_core_attach (sd, NULL,
130                        level, access_read_write_exec, space,
131                        addr, nr_bytes, modulo, NULL, aligned_buffer);
132
133       fill_buffer = aligned_buffer;
134       fill_length = (modulo == 0) ? nr_bytes : modulo;
135     }
136
137   if (fill_byte_flag)
138     {
139       ASSERT (fill_buffer != 0);
140       memset ((char*) fill_buffer, fill_byte_value, fill_length);
141     }
142
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;
152
153   return (*entry);
154 }
155
156 static SIM_RC
157 do_memopt_delete (SIM_DESC sd,
158                   int level,
159                   int space,
160                   address_word addr)
161 {
162   sim_memopt **entry = &STATE_MEMOPT (sd);
163   sim_memopt *alias;
164   while ((*entry) != NULL
165          && ((*entry)->level != level
166               || (*entry)->space != space
167               || (*entry)->addr != addr))
168     entry = &(*entry)->next;
169   if ((*entry) == NULL)
170     {
171       sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
172                       (long) addr);
173       return SIM_RC_FAIL;
174     }
175   /* delete any buffer */
176   if ((*entry)->buffer != NULL)
177     zfree ((*entry)->buffer);
178   /* delete it and its aliases */
179   alias = *entry;
180   *entry = (*entry)->next;
181   while (alias != NULL)
182     {
183       sim_memopt *dead = alias;
184       alias = alias->alias;
185       sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
186       zfree (dead);
187     }
188   return SIM_RC_OK;
189 }
190
191
192 static char *
193 parse_size (char *chp,
194             address_word *nr_bytes,
195             unsigned *modulo)
196 {
197   /* <nr_bytes> [ "%" <modulo> ] */
198   *nr_bytes = strtoul (chp, &chp, 0);
199   if (*chp == '%')
200     {
201       *modulo = strtoul (chp + 1, &chp, 0);
202     }
203   return chp;
204 }
205
206 static char *
207 parse_ulong_value (char *chp,
208                      unsigned long *value)
209 {
210   *value = strtoul (chp, &chp, 0);
211   return chp;
212 }
213
214 static char *
215 parse_addr (char *chp,
216             int *level,
217             int *space,
218             address_word *addr)
219 {
220   /* [ <space> ": " ] <addr> [ "@" <level> ] */
221   *addr = (unsigned long) strtoul (chp, &chp, 0);
222   if (*chp == ':')
223     {
224       *space = *addr;
225       *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
226     }
227   if (*chp == '@')
228     {
229       *level = strtoul (chp + 1, &chp, 0);
230     }
231   return chp;
232 }
233
234
235 static SIM_RC
236 memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
237                        char *arg, int is_command)
238 {
239   switch (opt)
240     {
241
242     case OPTION_MEMORY_DELETE:
243       if (strcasecmp (arg, "all") == 0)
244         {
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);
250           return SIM_RC_OK;
251         }
252       else
253         {
254           int level = 0;
255           int space = 0;
256           address_word addr = 0;
257           parse_addr (arg, &level, &space, &addr);
258           return do_memopt_delete (sd, level, space, addr);
259         }
260     
261     case OPTION_MEMORY_REGION:
262       {
263         char *chp = arg;
264         int level = 0;
265         int space = 0;
266         address_word addr = 0;
267         address_word nr_bytes = 0;
268         unsigned modulo = 0;
269         /* parse the arguments */
270         chp = parse_addr (chp, &level, &space, &addr);
271         if (*chp != ',')
272           {
273             sim_io_eprintf (sd, "Missing size for memory-region\n");
274             return SIM_RC_FAIL;
275           }
276         chp = parse_size (chp + 1, &nr_bytes, &modulo);
277         /* old style */
278         if (*chp == ',')
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);
283         return SIM_RC_OK;
284       }
285
286     case OPTION_MEMORY_ALIAS:
287       {
288         char *chp = arg;
289         int level = 0;
290         int space = 0;
291         address_word addr = 0;
292         address_word nr_bytes = 0;
293         unsigned modulo = 0;
294         sim_memopt *entry;
295         /* parse the arguments */
296         chp = parse_addr (chp, &level, &space, &addr);
297         if (*chp != ',')
298           {
299             sim_io_eprintf (sd, "Missing size for memory-region\n");
300             return SIM_RC_FAIL;
301           }
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,
305                                &STATE_MEMOPT (sd),
306                                NULL);
307         /* now attach all the aliases */
308         while (*chp == ',')
309           {
310             int a_level = level;
311             int a_space = space;
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);
316           }
317         return SIM_RC_OK;
318       }
319
320     case OPTION_MEMORY_SIZE:
321       {
322         int level = 0;
323         int space = 0;
324         address_word addr = 0;
325         address_word nr_bytes = 0;
326         unsigned modulo = 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);
332         return SIM_RC_OK;
333       }
334
335     case OPTION_MEMORY_CLEAR:
336       {
337         fill_byte_value = (unsigned8) 0;
338         fill_byte_flag = 1;
339         return SIM_RC_OK;
340         break;
341       }
342
343     case OPTION_MEMORY_FILL:
344       {
345         unsigned long fill_value;
346         parse_ulong_value (arg, &fill_value);
347         if (fill_value > 255)
348           {
349             sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
350             return SIM_RC_FAIL;
351           }
352         fill_byte_value = (unsigned8) fill_value;
353         fill_byte_flag = 1;
354         return SIM_RC_OK;
355         break;
356       }
357
358     case OPTION_MEMORY_INFO:
359       {
360         sim_memopt *entry;
361         sim_io_printf (sd, "Memory maps:\n");
362         for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
363           {
364             sim_memopt *alias;
365             sim_io_printf (sd, " memory");
366             if (entry->alias == NULL)
367               sim_io_printf (sd, " region ");
368             else
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;
380                  alias != NULL;
381                  alias = alias->next)
382               {
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);
388               }
389             sim_io_printf (sd, "\n");
390           }
391         return SIM_RC_OK;
392         break;
393       }
394
395     default:
396       sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
397       return SIM_RC_FAIL;
398
399     }
400
401   return SIM_RC_FAIL;
402 }
403
404
405 /* "memory" module install handler.
406
407    This is called via sim_module_install to install the "memory" subsystem
408    into the simulator.  */
409
410 static MODULE_INIT_FN sim_memory_init;
411 static MODULE_UNINSTALL_FN sim_memory_uninstall;
412
413 SIM_RC
414 sim_memopt_install (SIM_DESC sd)
415 {
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);
420   return SIM_RC_OK;
421 }
422
423
424 /* Uninstall the "memory" subsystem from the simulator.  */
425
426 static void
427 sim_memory_uninstall (SIM_DESC sd)
428 {
429   sim_memopt **entry = &STATE_MEMOPT (sd);
430   sim_memopt *alias;
431
432   while ((*entry) != NULL)
433     {
434       /* delete any buffer */
435       if ((*entry)->buffer != NULL)
436         zfree ((*entry)->buffer);
437
438       /* delete it and its aliases */
439       alias = *entry;
440       while (alias != NULL)
441         {
442           sim_memopt *dead = alias;
443           alias = alias->alias;
444           sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
445           zfree (dead);
446         }
447
448       /* next victim */
449       *entry = (*entry)->next;
450     }
451 }
452
453
454 static SIM_RC
455 sim_memory_init (SIM_DESC sd)
456 {
457   /* FIXME: anything needed? */
458   return SIM_RC_OK;
459 }