aboutsummaryrefslogtreecommitdiff
path: root/sim/common/sim-module.c
blob: 297b13335089688917bf1bd77d035db2e920ba61 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* Module support.
   Copyright (C) 1996, 1997, 1998 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-io.h"
#include "sim-options.h"
#include "sim-assert.h"

#include "libiberty.h"

/* List of all modules.  */
static MODULE_INSTALL_FN * const modules[] = {
  standard_install,
  sim_events_install,
#if WITH_ENGINE
  sim_engine_install,
#endif
#if WITH_TRACE
  trace_install,
#endif
#if WITH_PROFILE
  profile_install,
#endif
  sim_core_install,
#ifndef SIM_HAVE_FLATMEM
  /* FIXME: should handle flatmem as well FLATMEM */
  sim_memopt_install,
#endif
#if WITH_WATCHPOINTS
  sim_watchpoint_install,
#endif
#if WITH_SCACHE
  scache_install,
#endif
#ifdef SIM_HAVE_MODEL
  model_install,
#endif
#ifdef SIM_HAVE_BREAKPOINTS
  sim_break_install,
#endif
  /* Configured in [simulator specific] additional modules.  */
#ifdef MODULE_LIST
  MODULE_LIST
#endif
  0
};

/* Functions called from sim_open.  */

/* Initialize common parts before argument processing.  */

SIM_RC
sim_pre_argv_init (SIM_DESC sd, const char *myname)
{
  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) == NULL);

  STATE_MY_NAME (sd) = myname + strlen (myname);
  while (STATE_MY_NAME (sd) > myname && STATE_MY_NAME (sd)[-1] != '/')
    --STATE_MY_NAME (sd);

  /* Set the cpu names to default values.  */
  {
    int i;
    for (i = 0; i < MAX_NR_PROCESSORS; ++i)
      {
	char *name;
	asprintf (&name, "cpu%d", i);
	CPU_NAME (STATE_CPU (sd, i)) = name;
      }
  }

  /* Install all configured in modules.  */
  if (sim_module_install (sd) != SIM_RC_OK)
    return SIM_RC_FAIL;

  return SIM_RC_OK;
}

/* Initialize common parts after argument processing.  */

SIM_RC
sim_post_argv_init (SIM_DESC sd)
{
  int i;
  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  if (sim_module_init (sd) != SIM_RC_OK)
    return SIM_RC_FAIL;

  /* Set the cpu->state backlinks for each cpu.  */
  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    CPU_STATE (STATE_CPU (sd, i)) = sd;

  return SIM_RC_OK;
}

/* Install all modules.
   If this fails, no modules are left installed.  */

SIM_RC
sim_module_install (SIM_DESC sd)
{
  MODULE_INSTALL_FN * const *modp;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) == NULL);

  STATE_MODULES (sd) = ZALLOC (struct module_list);
  for (modp = modules; *modp != NULL; ++modp)
    {
      if ((*modp) (sd) != SIM_RC_OK)
	{
	  sim_module_uninstall (sd);
	  SIM_ASSERT (STATE_MODULES (sd) == NULL);
	  return SIM_RC_FAIL;
	}
    }
  return SIM_RC_OK;
}

/* Called after all modules have been installed and after argv
   has been processed.  */

SIM_RC
sim_module_init (SIM_DESC sd)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_INIT_LIST *modp;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  for (modp = modules->init_list; modp != NULL; modp = modp->next)
    {
      if ((*modp->fn) (sd) != SIM_RC_OK)
	return SIM_RC_FAIL;
    }
  return SIM_RC_OK;
}

/* Called when ever the simulator is resumed */

SIM_RC
sim_module_resume (SIM_DESC sd)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_RESUME_LIST *modp;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  for (modp = modules->resume_list; modp != NULL; modp = modp->next)
    {
      if ((*modp->fn) (sd) != SIM_RC_OK)
	return SIM_RC_FAIL;
    }
  return SIM_RC_OK;
}

/* Called when ever the simulator is suspended */

SIM_RC
sim_module_suspend (SIM_DESC sd)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_SUSPEND_LIST *modp;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  for (modp = modules->suspend_list; modp != NULL; modp = modp->next)
    {
      if ((*modp->fn) (sd) != SIM_RC_OK)
	return SIM_RC_FAIL;
    }
  return SIM_RC_OK;
}

/* Uninstall installed modules, called by sim_close.  */

void
sim_module_uninstall (SIM_DESC sd)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_UNINSTALL_LIST *modp;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  /* Uninstall the modules.  */
  for (modp = modules->uninstall_list; modp != NULL; modp = modp->next)
    (*modp->fn) (sd);

  /* clean-up init list */
  {
    MODULE_INIT_LIST *n, *d;
    for (d = modules->init_list; d != NULL; d = n)
      {
	n = d->next;
	zfree (d);
      }
  }

  /* clean-up resume list */
  {
    MODULE_RESUME_LIST *n, *d;
    for (d = modules->resume_list; d != NULL; d = n)
      {
	n = d->next;
	zfree (d);
      }
  }

  /* clean-up suspend list */
  {
    MODULE_SUSPEND_LIST *n, *d;
    for (d = modules->suspend_list; d != NULL; d = n)
      {
	n = d->next;
	zfree (d);
      }
  }

  /* clean-up uninstall list */
  {
    MODULE_UNINSTALL_LIST *n, *d;
    for (d = modules->uninstall_list; d != NULL; d = n)
      {
	n = d->next;
	zfree (d);
      }
  }

  /* clean-up info list */
  {
    MODULE_INFO_LIST *n, *d;
    for (d = modules->info_list; d != NULL; d = n)
      {
	n = d->next;
	zfree (d);
      }
  }

  zfree (modules);
  STATE_MODULES (sd) = NULL;
}

/* Called when ever simulator info is needed */

void
sim_module_info (SIM_DESC sd, int verbose)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_INFO_LIST *modp;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  for (modp = modules->info_list; modp != NULL; modp = modp->next)
    {
      (*modp->fn) (sd, verbose);
    }
}

/* Add FN to the init handler list.
   init in the same order as the install. */

void
sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_INIT_LIST *l = ZALLOC (MODULE_INIT_LIST);
  MODULE_INIT_LIST **last;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  last = &modules->init_list;
  while (*last != NULL)
    last = &((*last)->next);

  l->fn = fn;
  l->next = NULL;
  *last = l;
}

/* Add FN to the resume handler list.
   resume in the same order as the install. */

void
sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_RESUME_LIST *l = ZALLOC (MODULE_RESUME_LIST);
  MODULE_RESUME_LIST **last;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  last = &modules->resume_list;
  while (*last != NULL)
    last = &((*last)->next);

  l->fn = fn;
  l->next = NULL;
  *last = l;
}

/* Add FN to the init handler list.
   suspend in the reverse order to install. */

void
sim_module_add_suspend_fn (SIM_DESC sd, MODULE_SUSPEND_FN fn)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_SUSPEND_LIST *l = ZALLOC (MODULE_SUSPEND_LIST);
  MODULE_SUSPEND_LIST **last;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  last = &modules->suspend_list;
  while (*last != NULL)
    last = &((*last)->next);

  l->fn = fn;
  l->next = modules->suspend_list;
  modules->suspend_list = l;
}

/* Add FN to the uninstall handler list.
   Uninstall in reverse order to install.  */

void
sim_module_add_uninstall_fn (SIM_DESC sd, MODULE_UNINSTALL_FN fn)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_UNINSTALL_LIST *l = ZALLOC (MODULE_UNINSTALL_LIST);

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  l->fn = fn;
  l->next = modules->uninstall_list;
  modules->uninstall_list = l;
}

/* Add FN to the info handler list.
   Report info in the same order as the install. */

void
sim_module_add_info_fn (SIM_DESC sd, MODULE_INFO_FN fn)
{
  struct module_list *modules = STATE_MODULES (sd);
  MODULE_INFO_LIST *l = ZALLOC (MODULE_INFO_LIST);
  MODULE_INFO_LIST **last;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MODULES (sd) != NULL);

  last = &modules->info_list;
  while (*last != NULL)
    last = &((*last)->next);

  l->fn = fn;
  l->next = NULL;
  *last = l;
}