Update copyright year in version of output (gdb/gdbserver/gdbreplay)
[deliverable/binutils-gdb.git] / sim / common / sim-memopt.c
CommitLineData
c906108c 1/* Simulator memory option handling.
dc3cf14f
JB
2 Copyright (C) 1996-1999, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
c906108c
SS
4 Contributed by Cygnus Support.
5
6This file is part of GDB, the GNU debugger.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
4744ac1b
JB
10the Free Software Foundation; either version 3 of the License, or
11(at your option) any later version.
c906108c
SS
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
4744ac1b
JB
18You should have received a copy of the GNU General Public License
19along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 20
764f1408
FCE
21#include "cconfig.h"
22
c906108c
SS
23#include "sim-main.h"
24#include "sim-assert.h"
25#include "sim-options.h"
26
27#ifdef HAVE_STRING_H
28#include <string.h>
29#else
30#ifdef HAVE_STRINGS_H
31#include <strings.h>
32#endif
33#endif
34#ifdef HAVE_STDLIB_H
35#include <stdlib.h>
36#endif
764f1408
FCE
37#ifdef HAVE_ERRNO_H
38#include <errno.h>
39#endif
40#ifdef HAVE_FCNTL_H
41#include <fcntl.h>
42#endif
43#ifdef HAVE_SYS_MMAN_H
44#include <sys/mman.h>
45#endif
46#ifdef HAVE_SYS_STAT_H
47#include <sys/stat.h>
48#endif
bf962092
AC
49#ifdef HAVE_UNISTD_H
50#include <unistd.h>
51#endif
c906108c 52
764f1408 53/* Memory fill byte. */
c906108c
SS
54static unsigned8 fill_byte_value;
55static int fill_byte_flag = 0;
56
764f1408
FCE
57/* Memory mapping; see OPTION_MEMORY_MAPFILE. */
58static int mmap_next_fd = -1;
59
c906108c
SS
60/* Memory command line options. */
61
62enum {
63 OPTION_MEMORY_DELETE = OPTION_START,
64 OPTION_MEMORY_REGION,
65 OPTION_MEMORY_SIZE,
66 OPTION_MEMORY_INFO,
67 OPTION_MEMORY_ALIAS,
68 OPTION_MEMORY_CLEAR,
764f1408 69 OPTION_MEMORY_FILL,
bef6be3d
MF
70 OPTION_MEMORY_MAPFILE,
71 OPTION_MAP_INFO
c906108c
SS
72};
73
74static DECLARE_OPTION_HANDLER (memory_option_handler);
75
76static const OPTION memory_options[] =
77{
78 { {"memory-delete", required_argument, NULL, OPTION_MEMORY_DELETE },
79 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
80 memory_option_handler },
81 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
82 '\0', "ADDRESS", NULL,
83 memory_option_handler },
84
85 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
86 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
87 memory_option_handler },
88
89 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
90 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
91 memory_option_handler },
92
93 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
f40f1a01
NC
94 '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
95 "Add memory at address zero", memory_option_handler },
c906108c
SS
96
97 { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
98 '\0', "VALUE", "Fill subsequently added memory regions",
99 memory_option_handler },
100
101 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
102 '\0', NULL, "Clear subsequently added memory regions",
103 memory_option_handler },
104
764f1408
FCE
105#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
106 { {"memory-mapfile", required_argument, NULL, OPTION_MEMORY_MAPFILE },
107 '\0', "FILE", "Memory-map next memory region from file",
108 memory_option_handler },
109#endif
110
c906108c
SS
111 { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
112 '\0', NULL, "List configurable memory regions",
113 memory_option_handler },
114 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
115 '\0', NULL, NULL,
116 memory_option_handler },
bef6be3d
MF
117 { {"map-info", no_argument, NULL, OPTION_MAP_INFO },
118 '\0', NULL, "List mapped regions",
119 memory_option_handler },
c906108c
SS
120
121 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
122};
123
124
125static sim_memopt *
126do_memopt_add (SIM_DESC sd,
127 int level,
128 int space,
129 address_word addr,
130 address_word nr_bytes,
131 unsigned modulo,
132 sim_memopt **entry,
133 void *buffer)
134{
135 void *fill_buffer;
136 unsigned fill_length;
137 void *free_buffer;
764f1408 138 unsigned long free_length;
c906108c
SS
139
140 if (buffer != NULL)
141 {
142 /* Buffer already given. sim_memory_uninstall will free it. */
143 sim_core_attach (sd, NULL,
144 level, access_read_write_exec, space,
145 addr, nr_bytes, modulo, NULL, buffer);
146
147 free_buffer = buffer;
764f1408 148 free_length = 0;
c906108c
SS
149 fill_buffer = buffer;
150 fill_length = (modulo == 0) ? nr_bytes : modulo;
151 }
152 else
153 {
154 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
155 void *aligned_buffer;
156 int padding = (addr % sizeof (unsigned64));
157 unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
158
764f1408
FCE
159 free_buffer = NULL;
160 free_length = bytes;
c906108c 161
764f1408
FCE
162#ifdef HAVE_MMAP
163 /* Memory map or malloc(). */
164 if (mmap_next_fd >= 0)
165 {
166 /* Check that given file is big enough. */
167 struct stat s;
168 int rc;
169
170 /* Some kernels will SIGBUS the application if mmap'd file
171 is not large enough. */
172 rc = fstat (mmap_next_fd, &s);
173 if (rc < 0 || s.st_size < bytes)
174 {
175 sim_io_error (sd,
176 "Error, cannot confirm that mmap file is large enough "
bf962092 177 "(>= %ld bytes)\n", bytes);
764f1408
FCE
178 }
179
180 free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
181 if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
182 {
183 sim_io_error (sd, "Error, cannot mmap file (%s).\n",
184 strerror(errno));
185 }
186 }
187#endif
188
189 /* Need heap allocation? */
190 if (free_buffer == NULL)
191 {
192 /* If filling with non-zero value, do not use clearing allocator. */
193 if (fill_byte_flag && fill_byte_value != 0)
194 free_buffer = xmalloc (bytes); /* don't clear */
195 else
196 free_buffer = zalloc (bytes); /* clear */
197 }
c906108c
SS
198
199 aligned_buffer = (char*) free_buffer + padding;
200
201 sim_core_attach (sd, NULL,
202 level, access_read_write_exec, space,
203 addr, nr_bytes, modulo, NULL, aligned_buffer);
204
205 fill_buffer = aligned_buffer;
206 fill_length = (modulo == 0) ? nr_bytes : modulo;
207
208 /* If we just used a clearing allocator, and are about to fill with
209 zero, truncate the redundant fill operation. */
210
211 if (fill_byte_flag && fill_byte_value == 0)
212 fill_length = 1; /* avoid boundary length=0 case */
213 }
214
215 if (fill_byte_flag)
216 {
217 ASSERT (fill_buffer != 0);
218 memset ((char*) fill_buffer, fill_byte_value, fill_length);
219 }
220
221 while ((*entry) != NULL)
222 entry = &(*entry)->next;
223 (*entry) = ZALLOC (sim_memopt);
224 (*entry)->level = level;
225 (*entry)->space = space;
226 (*entry)->addr = addr;
227 (*entry)->nr_bytes = nr_bytes;
228 (*entry)->modulo = modulo;
229 (*entry)->buffer = free_buffer;
230
764f1408
FCE
231 /* Record memory unmapping info. */
232 if (mmap_next_fd >= 0)
233 {
234 (*entry)->munmap_length = free_length;
235 close (mmap_next_fd);
236 mmap_next_fd = -1;
237 }
238 else
239 (*entry)->munmap_length = 0;
240
c906108c
SS
241 return (*entry);
242}
243
244static SIM_RC
245do_memopt_delete (SIM_DESC sd,
246 int level,
247 int space,
248 address_word addr)
249{
250 sim_memopt **entry = &STATE_MEMOPT (sd);
251 sim_memopt *alias;
252 while ((*entry) != NULL
253 && ((*entry)->level != level
254 || (*entry)->space != space
255 || (*entry)->addr != addr))
256 entry = &(*entry)->next;
257 if ((*entry) == NULL)
258 {
259 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
260 (long) addr);
261 return SIM_RC_FAIL;
262 }
263 /* delete any buffer */
264 if ((*entry)->buffer != NULL)
764f1408
FCE
265 {
266#ifdef HAVE_MUNMAP
267 if ((*entry)->munmap_length > 0)
268 munmap ((*entry)->buffer, (*entry)->munmap_length);
269 else
270#endif
271 zfree ((*entry)->buffer);
272 }
273
c906108c
SS
274 /* delete it and its aliases */
275 alias = *entry;
276 *entry = (*entry)->next;
277 while (alias != NULL)
278 {
279 sim_memopt *dead = alias;
280 alias = alias->alias;
281 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
282 zfree (dead);
283 }
284 return SIM_RC_OK;
285}
286
287
288static char *
289parse_size (char *chp,
290 address_word *nr_bytes,
291 unsigned *modulo)
292{
f40f1a01 293 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
c906108c 294 *nr_bytes = strtoul (chp, &chp, 0);
f40f1a01 295 switch (*chp)
c906108c 296 {
f40f1a01 297 case '%':
c906108c 298 *modulo = strtoul (chp + 1, &chp, 0);
f40f1a01
NC
299 break;
300 case 'g': case 'G': /* Gigabyte suffix. */
301 *nr_bytes <<= 10;
302 /* Fall through. */
303 case 'm': case 'M': /* Megabyte suffix. */
304 *nr_bytes <<= 10;
305 /* Fall through. */
306 case 'k': case 'K': /* Kilobyte suffix. */
307 *nr_bytes <<= 10;
308 /* Check for a modulo specifier after the suffix. */
309 ++ chp;
310 if (* chp == 'b' || * chp == 'B')
311 ++ chp;
312 if (* chp == '%')
313 *modulo = strtoul (chp + 1, &chp, 0);
314 break;
c906108c
SS
315 }
316 return chp;
317}
318
319static char *
320parse_ulong_value (char *chp,
321 unsigned long *value)
322{
323 *value = strtoul (chp, &chp, 0);
324 return chp;
325}
326
327static char *
328parse_addr (char *chp,
329 int *level,
330 int *space,
331 address_word *addr)
332{
333 /* [ <space> ": " ] <addr> [ "@" <level> ] */
334 *addr = (unsigned long) strtoul (chp, &chp, 0);
335 if (*chp == ':')
336 {
337 *space = *addr;
338 *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
339 }
340 if (*chp == '@')
341 {
342 *level = strtoul (chp + 1, &chp, 0);
343 }
344 return chp;
345}
346
347
348static SIM_RC
349memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
350 char *arg, int is_command)
351{
352 switch (opt)
353 {
354
355 case OPTION_MEMORY_DELETE:
356 if (strcasecmp (arg, "all") == 0)
357 {
358 while (STATE_MEMOPT (sd) != NULL)
359 do_memopt_delete (sd,
360 STATE_MEMOPT (sd)->level,
361 STATE_MEMOPT (sd)->space,
362 STATE_MEMOPT (sd)->addr);
363 return SIM_RC_OK;
364 }
365 else
366 {
367 int level = 0;
368 int space = 0;
369 address_word addr = 0;
370 parse_addr (arg, &level, &space, &addr);
371 return do_memopt_delete (sd, level, space, addr);
372 }
373
374 case OPTION_MEMORY_REGION:
375 {
376 char *chp = arg;
377 int level = 0;
378 int space = 0;
379 address_word addr = 0;
380 address_word nr_bytes = 0;
381 unsigned modulo = 0;
382 /* parse the arguments */
383 chp = parse_addr (chp, &level, &space, &addr);
384 if (*chp != ',')
385 {
386 sim_io_eprintf (sd, "Missing size for memory-region\n");
387 return SIM_RC_FAIL;
388 }
389 chp = parse_size (chp + 1, &nr_bytes, &modulo);
390 /* old style */
391 if (*chp == ',')
392 modulo = strtoul (chp + 1, &chp, 0);
393 /* try to attach/insert it */
394 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
395 &STATE_MEMOPT (sd), NULL);
396 return SIM_RC_OK;
397 }
398
399 case OPTION_MEMORY_ALIAS:
400 {
401 char *chp = arg;
402 int level = 0;
403 int space = 0;
404 address_word addr = 0;
405 address_word nr_bytes = 0;
406 unsigned modulo = 0;
407 sim_memopt *entry;
408 /* parse the arguments */
409 chp = parse_addr (chp, &level, &space, &addr);
410 if (*chp != ',')
411 {
412 sim_io_eprintf (sd, "Missing size for memory-region\n");
413 return SIM_RC_FAIL;
414 }
415 chp = parse_size (chp + 1, &nr_bytes, &modulo);
416 /* try to attach/insert the main record */
417 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
418 &STATE_MEMOPT (sd),
419 NULL);
420 /* now attach all the aliases */
421 while (*chp == ',')
422 {
423 int a_level = level;
424 int a_space = space;
425 address_word a_addr = addr;
426 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
427 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
428 &entry->alias, entry->buffer);
429 }
430 return SIM_RC_OK;
431 }
432
433 case OPTION_MEMORY_SIZE:
434 {
435 int level = 0;
436 int space = 0;
437 address_word addr = 0;
438 address_word nr_bytes = 0;
439 unsigned modulo = 0;
440 /* parse the arguments */
441 parse_size (arg, &nr_bytes, &modulo);
442 /* try to attach/insert it */
443 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
444 &STATE_MEMOPT (sd), NULL);
445 return SIM_RC_OK;
446 }
447
448 case OPTION_MEMORY_CLEAR:
449 {
450 fill_byte_value = (unsigned8) 0;
451 fill_byte_flag = 1;
452 return SIM_RC_OK;
453 break;
454 }
455
456 case OPTION_MEMORY_FILL:
457 {
458 unsigned long fill_value;
459 parse_ulong_value (arg, &fill_value);
460 if (fill_value > 255)
461 {
462 sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
463 return SIM_RC_FAIL;
464 }
465 fill_byte_value = (unsigned8) fill_value;
466 fill_byte_flag = 1;
467 return SIM_RC_OK;
468 break;
469 }
470
764f1408
FCE
471 case OPTION_MEMORY_MAPFILE:
472 {
473 if (mmap_next_fd >= 0)
474 {
475 sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
476 return SIM_RC_FAIL;
477 }
478
479 mmap_next_fd = open (arg, O_RDWR);
480 if (mmap_next_fd < 0)
481 {
482 sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
483 arg, strerror(errno));
484 return SIM_RC_FAIL;
485 }
486
487 return SIM_RC_OK;
488 }
489
c906108c
SS
490 case OPTION_MEMORY_INFO:
491 {
492 sim_memopt *entry;
493 sim_io_printf (sd, "Memory maps:\n");
494 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
495 {
496 sim_memopt *alias;
497 sim_io_printf (sd, " memory");
498 if (entry->alias == NULL)
499 sim_io_printf (sd, " region ");
500 else
501 sim_io_printf (sd, " alias ");
502 if (entry->space != 0)
503 sim_io_printf (sd, "0x%lx:", (long) entry->space);
504 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
505 if (entry->level != 0)
506 sim_io_printf (sd, "@0x%lx", (long) entry->level);
507 sim_io_printf (sd, ",0x%lx",
508 (long) entry->nr_bytes);
509 if (entry->modulo != 0)
510 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
511 for (alias = entry->alias;
512 alias != NULL;
513 alias = alias->next)
514 {
515 if (alias->space != 0)
516 sim_io_printf (sd, "0x%lx:", (long) alias->space);
517 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
518 if (alias->level != 0)
519 sim_io_printf (sd, "@0x%lx", (long) alias->level);
520 }
521 sim_io_printf (sd, "\n");
522 }
523 return SIM_RC_OK;
524 break;
525 }
526
bef6be3d
MF
527 case OPTION_MAP_INFO:
528 {
529 sim_core *memory = STATE_CORE (sd);
530 unsigned nr_map;
531
532 for (nr_map = 0; nr_map < nr_maps; ++nr_map)
533 {
534 sim_core_map *map = &memory->common.map[nr_map];
535 sim_core_mapping *mapping = map->first;
536
537 if (!mapping)
538 continue;
539
540 sim_io_printf (sd, "%s maps:\n", map_to_str (nr_map));
541 do
542 {
543 unsigned modulo;
544
545 sim_io_printf (sd, " map ");
546 if (mapping->space != 0)
547 sim_io_printf (sd, "0x%x:", mapping->space);
548 sim_io_printf (sd, "0x%08lx", (long) mapping->base);
549 if (mapping->level != 0)
550 sim_io_printf (sd, "@0x%x", mapping->level);
551 sim_io_printf (sd, ",0x%lx", (long) mapping->nr_bytes);
552 modulo = mapping->mask + 1;
553 if (modulo != 0)
554 sim_io_printf (sd, "%%0x%x", modulo);
555 sim_io_printf (sd, "\n");
556
557 mapping = mapping->next;
558 }
559 while (mapping);
560 }
561
562 return SIM_RC_OK;
563 break;
564 }
565
c906108c
SS
566 default:
567 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
568 return SIM_RC_FAIL;
569
570 }
571
572 return SIM_RC_FAIL;
573}
574
575
576/* "memory" module install handler.
577
578 This is called via sim_module_install to install the "memory" subsystem
579 into the simulator. */
580
581static MODULE_INIT_FN sim_memory_init;
582static MODULE_UNINSTALL_FN sim_memory_uninstall;
583
584SIM_RC
585sim_memopt_install (SIM_DESC sd)
586{
587 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
588 sim_add_option_table (sd, NULL, memory_options);
589 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
590 sim_module_add_init_fn (sd, sim_memory_init);
591 return SIM_RC_OK;
592}
593
594
595/* Uninstall the "memory" subsystem from the simulator. */
596
597static void
598sim_memory_uninstall (SIM_DESC sd)
599{
600 sim_memopt **entry = &STATE_MEMOPT (sd);
601 sim_memopt *alias;
602
603 while ((*entry) != NULL)
604 {
605 /* delete any buffer */
606 if ((*entry)->buffer != NULL)
764f1408
FCE
607 {
608#ifdef HAVE_MUNMAP
609 if ((*entry)->munmap_length > 0)
610 munmap ((*entry)->buffer, (*entry)->munmap_length);
611 else
612#endif
613 zfree ((*entry)->buffer);
614 }
c906108c
SS
615
616 /* delete it and its aliases */
617 alias = *entry;
7a292a7a
SS
618
619 /* next victim */
620 *entry = (*entry)->next;
621
c906108c
SS
622 while (alias != NULL)
623 {
624 sim_memopt *dead = alias;
625 alias = alias->alias;
626 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
627 zfree (dead);
628 }
c906108c
SS
629 }
630}
631
632
633static SIM_RC
634sim_memory_init (SIM_DESC sd)
635{
764f1408
FCE
636 /* Reinitialize option modifier flags, in case they were left
637 over from a previous sim startup event. */
638 fill_byte_flag = 0;
639 mmap_next_fd = -1;
640
c906108c
SS
641 return SIM_RC_OK;
642}
This page took 0.480966 seconds and 4 git commands to generate.