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