OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [sim/] [common/] [sim-memopt.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
/* Simulator memory option handling.
2
   Copyright (C) 1996-1999 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 "cconfig.h"
22
 
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
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
49
#ifdef HAVE_UNISTD_H
50
#include <unistd.h>
51
#endif
52
 
53
/* Memory fill byte. */
54
static unsigned8 fill_byte_value;
55
static int fill_byte_flag = 0;
56
 
57
/* Memory mapping; see OPTION_MEMORY_MAPFILE. */
58
static int mmap_next_fd = -1;
59
 
60
/* Memory command line options. */
61
 
62
enum {
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,
69
  OPTION_MEMORY_FILL,
70
  OPTION_MEMORY_MAPFILE
71
};
72
 
73
static DECLARE_OPTION_HANDLER (memory_option_handler);
74
 
75
static 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 },
93
      '\0', "SIZE", "Add memory at address zero",
94
      memory_option_handler },
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
 
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
 
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
 
121
static sim_memopt *
122
do_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;
134
  unsigned long free_length;
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;
144
      free_length = 0;
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
 
155
      free_buffer = NULL;
156
      free_length = bytes;
157
 
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 "
173
                            "(>= %ld bytes)\n", bytes);
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
        }
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
 
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
 
237
  return (*entry);
238
}
239
 
240
static SIM_RC
241
do_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)
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
 
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
 
284
static char *
285
parse_size (char *chp,
286
            address_word *nr_bytes,
287
            unsigned *modulo)
288
{
289
  /* <nr_bytes> [ "%" <modulo> ] */
290
  *nr_bytes = strtoul (chp, &chp, 0);
291
  if (*chp == '%')
292
    {
293
      *modulo = strtoul (chp + 1, &chp, 0);
294
    }
295
  return chp;
296
}
297
 
298
static char *
299
parse_ulong_value (char *chp,
300
                     unsigned long *value)
301
{
302
  *value = strtoul (chp, &chp, 0);
303
  return chp;
304
}
305
 
306
static char *
307
parse_addr (char *chp,
308
            int *level,
309
            int *space,
310
            address_word *addr)
311
{
312
  /* [ <space> ": " ] <addr> [ "@" <level> ] */
313
  *addr = (unsigned long) strtoul (chp, &chp, 0);
314
  if (*chp == ':')
315
    {
316
      *space = *addr;
317
      *addr = (unsigned long) strtoul (chp + 1, &chp, 0);
318
    }
319
  if (*chp == '@')
320
    {
321
      *level = strtoul (chp + 1, &chp, 0);
322
    }
323
  return chp;
324
}
325
 
326
 
327
static SIM_RC
328
memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
329
                       char *arg, int is_command)
330
{
331
  switch (opt)
332
    {
333
 
334
    case OPTION_MEMORY_DELETE:
335
      if (strcasecmp (arg, "all") == 0)
336
        {
337
          while (STATE_MEMOPT (sd) != NULL)
338
            do_memopt_delete (sd,
339
                              STATE_MEMOPT (sd)->level,
340
                              STATE_MEMOPT (sd)->space,
341
                              STATE_MEMOPT (sd)->addr);
342
          return SIM_RC_OK;
343
        }
344
      else
345
        {
346
          int level = 0;
347
          int space = 0;
348
          address_word addr = 0;
349
          parse_addr (arg, &level, &space, &addr);
350
          return do_memopt_delete (sd, level, space, addr);
351
        }
352
 
353
    case OPTION_MEMORY_REGION:
354
      {
355
        char *chp = arg;
356
        int level = 0;
357
        int space = 0;
358
        address_word addr = 0;
359
        address_word nr_bytes = 0;
360
        unsigned modulo = 0;
361
        /* parse the arguments */
362
        chp = parse_addr (chp, &level, &space, &addr);
363
        if (*chp != ',')
364
          {
365
            sim_io_eprintf (sd, "Missing size for memory-region\n");
366
            return SIM_RC_FAIL;
367
          }
368
        chp = parse_size (chp + 1, &nr_bytes, &modulo);
369
        /* old style */
370
        if (*chp == ',')
371
          modulo = strtoul (chp + 1, &chp, 0);
372
        /* try to attach/insert it */
373
        do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
374
                       &STATE_MEMOPT (sd), NULL);
375
        return SIM_RC_OK;
376
      }
377
 
378
    case OPTION_MEMORY_ALIAS:
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
        sim_memopt *entry;
387
        /* parse the arguments */
388
        chp = parse_addr (chp, &level, &space, &addr);
389
        if (*chp != ',')
390
          {
391
            sim_io_eprintf (sd, "Missing size for memory-region\n");
392
            return SIM_RC_FAIL;
393
          }
394
        chp = parse_size (chp + 1, &nr_bytes, &modulo);
395
        /* try to attach/insert the main record */
396
        entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
397
                               &STATE_MEMOPT (sd),
398
                               NULL);
399
        /* now attach all the aliases */
400
        while (*chp == ',')
401
          {
402
            int a_level = level;
403
            int a_space = space;
404
            address_word a_addr = addr;
405
            chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
406
            do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
407
                           &entry->alias, entry->buffer);
408
          }
409
        return SIM_RC_OK;
410
      }
411
 
412
    case OPTION_MEMORY_SIZE:
413
      {
414
        int level = 0;
415
        int space = 0;
416
        address_word addr = 0;
417
        address_word nr_bytes = 0;
418
        unsigned modulo = 0;
419
        /* parse the arguments */
420
        parse_size (arg, &nr_bytes, &modulo);
421
        /* try to attach/insert it */
422
        do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
423
                       &STATE_MEMOPT (sd), NULL);
424
        return SIM_RC_OK;
425
      }
426
 
427
    case OPTION_MEMORY_CLEAR:
428
      {
429
        fill_byte_value = (unsigned8) 0;
430
        fill_byte_flag = 1;
431
        return SIM_RC_OK;
432
        break;
433
      }
434
 
435
    case OPTION_MEMORY_FILL:
436
      {
437
        unsigned long fill_value;
438
        parse_ulong_value (arg, &fill_value);
439
        if (fill_value > 255)
440
          {
441
            sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
442
            return SIM_RC_FAIL;
443
          }
444
        fill_byte_value = (unsigned8) fill_value;
445
        fill_byte_flag = 1;
446
        return SIM_RC_OK;
447
        break;
448
      }
449
 
450
    case OPTION_MEMORY_MAPFILE:
451
      {
452
        if (mmap_next_fd >= 0)
453
          {
454
            sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
455
            return SIM_RC_FAIL;
456
          }
457
 
458
        mmap_next_fd = open (arg, O_RDWR);
459
        if (mmap_next_fd < 0)
460
          {
461
            sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
462
                            arg, strerror(errno));
463
            return SIM_RC_FAIL;
464
          }
465
 
466
        return SIM_RC_OK;
467
      }
468
 
469
    case OPTION_MEMORY_INFO:
470
      {
471
        sim_memopt *entry;
472
        sim_io_printf (sd, "Memory maps:\n");
473
        for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
474
          {
475
            sim_memopt *alias;
476
            sim_io_printf (sd, " memory");
477
            if (entry->alias == NULL)
478
              sim_io_printf (sd, " region ");
479
            else
480
              sim_io_printf (sd, " alias ");
481
            if (entry->space != 0)
482
              sim_io_printf (sd, "0x%lx:", (long) entry->space);
483
            sim_io_printf (sd, "0x%08lx", (long) entry->addr);
484
            if (entry->level != 0)
485
              sim_io_printf (sd, "@0x%lx", (long) entry->level);
486
            sim_io_printf (sd, ",0x%lx",
487
                           (long) entry->nr_bytes);
488
            if (entry->modulo != 0)
489
              sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
490
            for (alias = entry->alias;
491
                 alias != NULL;
492
                 alias = alias->next)
493
              {
494
                if (alias->space != 0)
495
                  sim_io_printf (sd, "0x%lx:", (long) alias->space);
496
                sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
497
                if (alias->level != 0)
498
                  sim_io_printf (sd, "@0x%lx", (long) alias->level);
499
              }
500
            sim_io_printf (sd, "\n");
501
          }
502
        return SIM_RC_OK;
503
        break;
504
      }
505
 
506
    default:
507
      sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
508
      return SIM_RC_FAIL;
509
 
510
    }
511
 
512
  return SIM_RC_FAIL;
513
}
514
 
515
 
516
/* "memory" module install handler.
517
 
518
   This is called via sim_module_install to install the "memory" subsystem
519
   into the simulator.  */
520
 
521
static MODULE_INIT_FN sim_memory_init;
522
static MODULE_UNINSTALL_FN sim_memory_uninstall;
523
 
524
SIM_RC
525
sim_memopt_install (SIM_DESC sd)
526
{
527
  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
528
  sim_add_option_table (sd, NULL, memory_options);
529
  sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
530
  sim_module_add_init_fn (sd, sim_memory_init);
531
  return SIM_RC_OK;
532
}
533
 
534
 
535
/* Uninstall the "memory" subsystem from the simulator.  */
536
 
537
static void
538
sim_memory_uninstall (SIM_DESC sd)
539
{
540
  sim_memopt **entry = &STATE_MEMOPT (sd);
541
  sim_memopt *alias;
542
 
543
  while ((*entry) != NULL)
544
    {
545
      /* delete any buffer */
546
      if ((*entry)->buffer != NULL)
547
        {
548
#ifdef HAVE_MUNMAP
549
          if ((*entry)->munmap_length > 0)
550
            munmap ((*entry)->buffer, (*entry)->munmap_length);
551
          else
552
#endif
553
            zfree ((*entry)->buffer);
554
        }
555
 
556
      /* delete it and its aliases */
557
      alias = *entry;
558
 
559
      /* next victim */
560
      *entry = (*entry)->next;
561
 
562
      while (alias != NULL)
563
        {
564
          sim_memopt *dead = alias;
565
          alias = alias->alias;
566
          sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
567
          zfree (dead);
568
        }
569
    }
570
}
571
 
572
 
573
static SIM_RC
574
sim_memory_init (SIM_DESC sd)
575
{
576
  /* Reinitialize option modifier flags, in case they were left
577
     over from a previous sim startup event.  */
578
  fill_byte_flag = 0;
579
  mmap_next_fd = -1;
580
 
581
  return SIM_RC_OK;
582
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.