2010-12-15 Kai Tietz <kai.tietz@onevision.com>
[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
FCE
69 OPTION_MEMORY_FILL,
70 OPTION_MEMORY_MAPFILE
c906108c
SS
71};
72
73static DECLARE_OPTION_HANDLER (memory_option_handler);
74
75static const OPTION memory_options[] =
76{
77 { {"memory-delete", required_argument, NULL, OPTION_MEMORY_DELETE },
78 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
79 memory_option_handler },
80 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
81 '\0', "ADDRESS", NULL,
82 memory_option_handler },
83
84 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
85 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
86 memory_option_handler },
87
88 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
89 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
90 memory_option_handler },
91
92 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
f40f1a01
NC
93 '\0', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
94 "Add memory at address zero", memory_option_handler },
c906108c
SS
95
96 { {"memory-fill", required_argument, NULL, OPTION_MEMORY_FILL },
97 '\0', "VALUE", "Fill subsequently added memory regions",
98 memory_option_handler },
99
100 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
101 '\0', NULL, "Clear subsequently added memory regions",
102 memory_option_handler },
103
764f1408
FCE
104#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
105 { {"memory-mapfile", required_argument, NULL, OPTION_MEMORY_MAPFILE },
106 '\0', "FILE", "Memory-map next memory region from file",
107 memory_option_handler },
108#endif
109
c906108c
SS
110 { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
111 '\0', NULL, "List configurable memory regions",
112 memory_option_handler },
113 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
114 '\0', NULL, NULL,
115 memory_option_handler },
116
117 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
118};
119
120
121static sim_memopt *
122do_memopt_add (SIM_DESC sd,
123 int level,
124 int space,
125 address_word addr,
126 address_word nr_bytes,
127 unsigned modulo,
128 sim_memopt **entry,
129 void *buffer)
130{
131 void *fill_buffer;
132 unsigned fill_length;
133 void *free_buffer;
764f1408 134 unsigned long free_length;
c906108c
SS
135
136 if (buffer != NULL)
137 {
138 /* Buffer already given. sim_memory_uninstall will free it. */
139 sim_core_attach (sd, NULL,
140 level, access_read_write_exec, space,
141 addr, nr_bytes, modulo, NULL, buffer);
142
143 free_buffer = buffer;
764f1408 144 free_length = 0;
c906108c
SS
145 fill_buffer = buffer;
146 fill_length = (modulo == 0) ? nr_bytes : modulo;
147 }
148 else
149 {
150 /* Allocate new well-aligned buffer, just as sim_core_attach(). */
151 void *aligned_buffer;
152 int padding = (addr % sizeof (unsigned64));
153 unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
154
764f1408
FCE
155 free_buffer = NULL;
156 free_length = bytes;
c906108c 157
764f1408
FCE
158#ifdef HAVE_MMAP
159 /* Memory map or malloc(). */
160 if (mmap_next_fd >= 0)
161 {
162 /* Check that given file is big enough. */
163 struct stat s;
164 int rc;
165
166 /* Some kernels will SIGBUS the application if mmap'd file
167 is not large enough. */
168 rc = fstat (mmap_next_fd, &s);
169 if (rc < 0 || s.st_size < bytes)
170 {
171 sim_io_error (sd,
172 "Error, cannot confirm that mmap file is large enough "
bf962092 173 "(>= %ld bytes)\n", bytes);
764f1408
FCE
174 }
175
176 free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
177 if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
178 {
179 sim_io_error (sd, "Error, cannot mmap file (%s).\n",
180 strerror(errno));
181 }
182 }
183#endif
184
185 /* Need heap allocation? */
186 if (free_buffer == NULL)
187 {
188 /* If filling with non-zero value, do not use clearing allocator. */
189 if (fill_byte_flag && fill_byte_value != 0)
190 free_buffer = xmalloc (bytes); /* don't clear */
191 else
192 free_buffer = zalloc (bytes); /* clear */
193 }
c906108c
SS
194
195 aligned_buffer = (char*) free_buffer + padding;
196
197 sim_core_attach (sd, NULL,
198 level, access_read_write_exec, space,
199 addr, nr_bytes, modulo, NULL, aligned_buffer);
200
201 fill_buffer = aligned_buffer;
202 fill_length = (modulo == 0) ? nr_bytes : modulo;
203
204 /* If we just used a clearing allocator, and are about to fill with
205 zero, truncate the redundant fill operation. */
206
207 if (fill_byte_flag && fill_byte_value == 0)
208 fill_length = 1; /* avoid boundary length=0 case */
209 }
210
211 if (fill_byte_flag)
212 {
213 ASSERT (fill_buffer != 0);
214 memset ((char*) fill_buffer, fill_byte_value, fill_length);
215 }
216
217 while ((*entry) != NULL)
218 entry = &(*entry)->next;
219 (*entry) = ZALLOC (sim_memopt);
220 (*entry)->level = level;
221 (*entry)->space = space;
222 (*entry)->addr = addr;
223 (*entry)->nr_bytes = nr_bytes;
224 (*entry)->modulo = modulo;
225 (*entry)->buffer = free_buffer;
226
764f1408
FCE
227 /* Record memory unmapping info. */
228 if (mmap_next_fd >= 0)
229 {
230 (*entry)->munmap_length = free_length;
231 close (mmap_next_fd);
232 mmap_next_fd = -1;
233 }
234 else
235 (*entry)->munmap_length = 0;
236
c906108c
SS
237 return (*entry);
238}
239
240static SIM_RC
241do_memopt_delete (SIM_DESC sd,
242 int level,
243 int space,
244 address_word addr)
245{
246 sim_memopt **entry = &STATE_MEMOPT (sd);
247 sim_memopt *alias;
248 while ((*entry) != NULL
249 && ((*entry)->level != level
250 || (*entry)->space != space
251 || (*entry)->addr != addr))
252 entry = &(*entry)->next;
253 if ((*entry) == NULL)
254 {
255 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
256 (long) addr);
257 return SIM_RC_FAIL;
258 }
259 /* delete any buffer */
260 if ((*entry)->buffer != NULL)
764f1408
FCE
261 {
262#ifdef HAVE_MUNMAP
263 if ((*entry)->munmap_length > 0)
264 munmap ((*entry)->buffer, (*entry)->munmap_length);
265 else
266#endif
267 zfree ((*entry)->buffer);
268 }
269
c906108c
SS
270 /* delete it and its aliases */
271 alias = *entry;
272 *entry = (*entry)->next;
273 while (alias != NULL)
274 {
275 sim_memopt *dead = alias;
276 alias = alias->alias;
277 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
278 zfree (dead);
279 }
280 return SIM_RC_OK;
281}
282
283
284static char *
285parse_size (char *chp,
286 address_word *nr_bytes,
287 unsigned *modulo)
288{
f40f1a01 289 /* <nr_bytes>[K|M|G] [ "%" <modulo> ] */
c906108c 290 *nr_bytes = strtoul (chp, &chp, 0);
f40f1a01 291 switch (*chp)
c906108c 292 {
f40f1a01 293 case '%':
c906108c 294 *modulo = strtoul (chp + 1, &chp, 0);
f40f1a01
NC
295 break;
296 case 'g': case 'G': /* Gigabyte suffix. */
297 *nr_bytes <<= 10;
298 /* Fall through. */
299 case 'm': case 'M': /* Megabyte suffix. */
300 *nr_bytes <<= 10;
301 /* Fall through. */
302 case 'k': case 'K': /* Kilobyte suffix. */
303 *nr_bytes <<= 10;
304 /* Check for a modulo specifier after the suffix. */
305 ++ chp;
306 if (* chp == 'b' || * chp == 'B')
307 ++ chp;
308 if (* chp == '%')
309 *modulo = strtoul (chp + 1, &chp, 0);
310 break;
c906108c
SS
311 }
312 return chp;
313}
314
315static char *
316parse_ulong_value (char *chp,
317 unsigned long *value)
318{
319 *value = strtoul (chp, &chp, 0);
320 return chp;
321}
322
323static char *
324parse_addr (char *chp,
325 int *level,
326 int *space,
327 address_word *addr)
328{
329 /* [ <space> ": " ] <addr> [ "@" <level> ] */
330 *addr = (unsigned long) strtoul (chp, &chp, 0);
331 if (*chp == ':')
332 {
333 *space = *addr;
334 *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
335 }
336 if (*chp == '@')
337 {
338 *level = strtoul (chp + 1, &chp, 0);
339 }
340 return chp;
341}
342
343
344static SIM_RC
345memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
346 char *arg, int is_command)
347{
348 switch (opt)
349 {
350
351 case OPTION_MEMORY_DELETE:
352 if (strcasecmp (arg, "all") == 0)
353 {
354 while (STATE_MEMOPT (sd) != NULL)
355 do_memopt_delete (sd,
356 STATE_MEMOPT (sd)->level,
357 STATE_MEMOPT (sd)->space,
358 STATE_MEMOPT (sd)->addr);
359 return SIM_RC_OK;
360 }
361 else
362 {
363 int level = 0;
364 int space = 0;
365 address_word addr = 0;
366 parse_addr (arg, &level, &space, &addr);
367 return do_memopt_delete (sd, level, space, addr);
368 }
369
370 case OPTION_MEMORY_REGION:
371 {
372 char *chp = arg;
373 int level = 0;
374 int space = 0;
375 address_word addr = 0;
376 address_word nr_bytes = 0;
377 unsigned modulo = 0;
378 /* parse the arguments */
379 chp = parse_addr (chp, &level, &space, &addr);
380 if (*chp != ',')
381 {
382 sim_io_eprintf (sd, "Missing size for memory-region\n");
383 return SIM_RC_FAIL;
384 }
385 chp = parse_size (chp + 1, &nr_bytes, &modulo);
386 /* old style */
387 if (*chp == ',')
388 modulo = strtoul (chp + 1, &chp, 0);
389 /* try to attach/insert it */
390 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
391 &STATE_MEMOPT (sd), NULL);
392 return SIM_RC_OK;
393 }
394
395 case OPTION_MEMORY_ALIAS:
396 {
397 char *chp = arg;
398 int level = 0;
399 int space = 0;
400 address_word addr = 0;
401 address_word nr_bytes = 0;
402 unsigned modulo = 0;
403 sim_memopt *entry;
404 /* parse the arguments */
405 chp = parse_addr (chp, &level, &space, &addr);
406 if (*chp != ',')
407 {
408 sim_io_eprintf (sd, "Missing size for memory-region\n");
409 return SIM_RC_FAIL;
410 }
411 chp = parse_size (chp + 1, &nr_bytes, &modulo);
412 /* try to attach/insert the main record */
413 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
414 &STATE_MEMOPT (sd),
415 NULL);
416 /* now attach all the aliases */
417 while (*chp == ',')
418 {
419 int a_level = level;
420 int a_space = space;
421 address_word a_addr = addr;
422 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
423 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
424 &entry->alias, entry->buffer);
425 }
426 return SIM_RC_OK;
427 }
428
429 case OPTION_MEMORY_SIZE:
430 {
431 int level = 0;
432 int space = 0;
433 address_word addr = 0;
434 address_word nr_bytes = 0;
435 unsigned modulo = 0;
436 /* parse the arguments */
437 parse_size (arg, &nr_bytes, &modulo);
438 /* try to attach/insert it */
439 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
440 &STATE_MEMOPT (sd), NULL);
441 return SIM_RC_OK;
442 }
443
444 case OPTION_MEMORY_CLEAR:
445 {
446 fill_byte_value = (unsigned8) 0;
447 fill_byte_flag = 1;
448 return SIM_RC_OK;
449 break;
450 }
451
452 case OPTION_MEMORY_FILL:
453 {
454 unsigned long fill_value;
455 parse_ulong_value (arg, &fill_value);
456 if (fill_value > 255)
457 {
458 sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
459 return SIM_RC_FAIL;
460 }
461 fill_byte_value = (unsigned8) fill_value;
462 fill_byte_flag = 1;
463 return SIM_RC_OK;
464 break;
465 }
466
764f1408
FCE
467 case OPTION_MEMORY_MAPFILE:
468 {
469 if (mmap_next_fd >= 0)
470 {
471 sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
472 return SIM_RC_FAIL;
473 }
474
475 mmap_next_fd = open (arg, O_RDWR);
476 if (mmap_next_fd < 0)
477 {
478 sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
479 arg, strerror(errno));
480 return SIM_RC_FAIL;
481 }
482
483 return SIM_RC_OK;
484 }
485
c906108c
SS
486 case OPTION_MEMORY_INFO:
487 {
488 sim_memopt *entry;
489 sim_io_printf (sd, "Memory maps:\n");
490 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
491 {
492 sim_memopt *alias;
493 sim_io_printf (sd, " memory");
494 if (entry->alias == NULL)
495 sim_io_printf (sd, " region ");
496 else
497 sim_io_printf (sd, " alias ");
498 if (entry->space != 0)
499 sim_io_printf (sd, "0x%lx:", (long) entry->space);
500 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
501 if (entry->level != 0)
502 sim_io_printf (sd, "@0x%lx", (long) entry->level);
503 sim_io_printf (sd, ",0x%lx",
504 (long) entry->nr_bytes);
505 if (entry->modulo != 0)
506 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
507 for (alias = entry->alias;
508 alias != NULL;
509 alias = alias->next)
510 {
511 if (alias->space != 0)
512 sim_io_printf (sd, "0x%lx:", (long) alias->space);
513 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
514 if (alias->level != 0)
515 sim_io_printf (sd, "@0x%lx", (long) alias->level);
516 }
517 sim_io_printf (sd, "\n");
518 }
519 return SIM_RC_OK;
520 break;
521 }
522
523 default:
524 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
525 return SIM_RC_FAIL;
526
527 }
528
529 return SIM_RC_FAIL;
530}
531
532
533/* "memory" module install handler.
534
535 This is called via sim_module_install to install the "memory" subsystem
536 into the simulator. */
537
538static MODULE_INIT_FN sim_memory_init;
539static MODULE_UNINSTALL_FN sim_memory_uninstall;
540
541SIM_RC
542sim_memopt_install (SIM_DESC sd)
543{
544 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
545 sim_add_option_table (sd, NULL, memory_options);
546 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
547 sim_module_add_init_fn (sd, sim_memory_init);
548 return SIM_RC_OK;
549}
550
551
552/* Uninstall the "memory" subsystem from the simulator. */
553
554static void
555sim_memory_uninstall (SIM_DESC sd)
556{
557 sim_memopt **entry = &STATE_MEMOPT (sd);
558 sim_memopt *alias;
559
560 while ((*entry) != NULL)
561 {
562 /* delete any buffer */
563 if ((*entry)->buffer != NULL)
764f1408
FCE
564 {
565#ifdef HAVE_MUNMAP
566 if ((*entry)->munmap_length > 0)
567 munmap ((*entry)->buffer, (*entry)->munmap_length);
568 else
569#endif
570 zfree ((*entry)->buffer);
571 }
c906108c
SS
572
573 /* delete it and its aliases */
574 alias = *entry;
7a292a7a
SS
575
576 /* next victim */
577 *entry = (*entry)->next;
578
c906108c
SS
579 while (alias != NULL)
580 {
581 sim_memopt *dead = alias;
582 alias = alias->alias;
583 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
584 zfree (dead);
585 }
c906108c
SS
586 }
587}
588
589
590static SIM_RC
591sim_memory_init (SIM_DESC sd)
592{
764f1408
FCE
593 /* Reinitialize option modifier flags, in case they were left
594 over from a previous sim startup event. */
595 fill_byte_flag = 0;
596 mmap_next_fd = -1;
597
c906108c
SS
598 return SIM_RC_OK;
599}
This page took 0.513285 seconds and 4 git commands to generate.