aboutsummaryrefslogtreecommitdiff
path: root/sim/common/sim-memopt.c
blob: be98a4a710087e9135c46bf111992d45b22ab5b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Simulator memory option handling.
   Copyright (C) 1996, 1997 Free Software Foundation, Inc.
   Contributed by Cygnus Support.

This file is part of GDB, the GNU debugger.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include "sim-main.h"
#include "sim-assert.h"
#include "sim-options.h"

#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

/* "core" command line options. */

enum {
  OPTION_MEMORY_DELETE = OPTION_START,
  OPTION_MEMORY_REGION,
  OPTION_MEMORY_SIZE,
  OPTION_MEMORY_INFO,
  OPTION_MEMORY_ALIAS,
  OPTION_MEMORY_CLEAR,
};

static DECLARE_OPTION_HANDLER (memory_option_handler);

static const OPTION memory_options[] =
{
  { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
      '\0', "ADDRESS", "Delete memory at ADDRESS",
      memory_option_handler },

  { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
      '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
      memory_option_handler },

  { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
      '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
      memory_option_handler },

  { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
      '\0', "SIZE", "Add memory at address zero",
      memory_option_handler },

  { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
      '\0', NULL, "Clear all memory regions",
      memory_option_handler },

  { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
      '\0', NULL, "Add memory at address zero",
      memory_option_handler },

  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
};


static SIM_RC
memory_option_handler (sd, opt, arg, is_command)
     SIM_DESC sd;
     int opt;
     char *arg;
     int is_command;
{
  switch (opt)
    {

    case OPTION_MEMORY_DELETE:
      {
	address_word addr = strtoul (arg, NULL, 0);
	sim_memopt **entry = &STATE_MEMOPT (sd);
	sim_memopt *alias;
	while ((*entry) != NULL && (*entry)->addr != addr)
	  entry = &(*entry)->next;
	if ((*entry) == NULL)
	  {
	    sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
			    (long) addr);
	    return SIM_RC_FAIL;
	  }
	/* delete any buffer */
	if ((*entry)->buf != NULL)
	  zfree ((*entry)->buf);
	/* delete it and its aliases */
	alias = *entry;
	*entry = alias->next;
	while (alias != NULL)
	  {
	    sim_memopt *dead = alias;
	    alias = alias->alias;
	    sim_core_detach (sd, NULL, attach_raw_memory, 0, dead->addr);
	    zfree (dead);
	  }
	return SIM_RC_OK;
      }
    
    case OPTION_MEMORY_REGION:
      {
	char *chp = arg;
	address_word addr = 0;
	address_word nr_bytes = 0;
	unsigned modulo = 0;
	sim_memopt **entry = &STATE_MEMOPT (sd);
	/* parse the arguments */
	addr = strtoul (chp, &chp, 0);
	if (*chp != ',')
	  {
	    sim_io_eprintf (sd, "Missing size for memory-region\n");
	    return SIM_RC_FAIL;
	  }
	chp++;
	nr_bytes = strtoul (chp, &chp, 0);
	if (*chp == ',')
	  modulo = strtoul (chp + 1, NULL, 0);
	/* try to attach it */
	sim_core_attach (sd, NULL,
			 attach_raw_memory, access_read_write_exec, 0,
			 addr, nr_bytes, modulo, NULL, NULL);
	/* ok, so insert it */
	while ((*entry) != NULL)
	  entry = &(*entry)->next;
	(*entry) = ZALLOC (sim_memopt);
	(*entry)->addr = addr;
	(*entry)->nr_bytes = nr_bytes;
	(*entry)->modulo = modulo;
	return SIM_RC_OK;
      }

    case OPTION_MEMORY_ALIAS:
      {
	sim_io_eprintf (sd, "memory-alias not supported for for this simulator\n");
	break;
      }

    case OPTION_MEMORY_SIZE:
      {
	sim_io_eprintf (sd, "memory-size not supported for for this simulator\n");
	return SIM_RC_FAIL;
	break;
      }

    case OPTION_MEMORY_CLEAR:
      {
	sim_memopt *entry;
	for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
	  {
	    sim_memopt *alias;
	    for (alias = entry; alias != NULL; alias = alias->next)
	      {
		unsigned8 zero = 0;
		address_word nr_bytes;
		if (alias->modulo != 0)
		  nr_bytes = alias->modulo;
		else
		  nr_bytes = alias->nr_bytes;
		sim_core_write_buffer (sd, NULL, sim_core_write_map,
				       &zero,
				       alias->addr + nr_bytes,
				       sizeof (zero));
		
	      }
	  }
	return SIM_RC_OK;
	break;
      }

    case OPTION_MEMORY_INFO:
      {
	sim_memopt *entry;
	sim_io_printf (sd, "Memory maps:\n");
	for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
	  {
	    sim_memopt *alias;
	    sim_io_printf (sd, " memory");
	    if (entry->alias == NULL)
	      {
		sim_io_printf (sd, " region 0x%08lx,0x%lx",
			       (long) entry->addr,
			       (long) entry->nr_bytes);
		if (entry->modulo != 0)
		  sim_io_printf (sd, ",0x%lx", (long) entry->modulo);
	      }
	    else
	      {
		sim_io_printf (sd, " alias 0x%08lx,0x%lx",
			       (long) entry->addr,
			       (long) entry->nr_bytes);
		for (alias = entry->alias; alias != NULL; alias = alias->next)
		  sim_io_printf (sd, ",0x%08lx", entry->addr);
	      }
	    sim_io_printf (sd, "\n");
	  }
	return SIM_RC_OK;
	break;
      }

    default:
      sim_io_eprintf (sd, "Unknown watch option %d\n", opt);
      return SIM_RC_FAIL;

    }

  return SIM_RC_FAIL;
}


/* "memory" module install handler.

   This is called via sim_module_install to install the "memory" subsystem
   into the simulator.  */

static MODULE_INIT_FN sim_memory_init;
static MODULE_UNINSTALL_FN sim_memory_uninstall;

SIM_RC
sim_memopt_install (SIM_DESC sd)
{
  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  sim_add_option_table (sd, memory_options);
  sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
  sim_module_add_init_fn (sd, sim_memory_init);
  return SIM_RC_OK;
}


/* Uninstall the "memory" subsystem from the simulator.  */

static void
sim_memory_uninstall (SIM_DESC sd)
{
  /* FIXME: free buffers, etc. */
}


static SIM_RC
sim_memory_init (SIM_DESC sd)
{
  /* FIXME: anything needed? */
  return SIM_RC_OK;
}