Add support for suspending/resumeing the simulator in sim-modules.
[external/binutils.git] / sim / common / sim-base.h
1 /* Simulator pseudo baseclass.
2    Copyright (C) 1997 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
22 /* Simulator state pseudo baseclass.
23
24    Each simulator is required to have the file ``sim-main.h''.  That
25    file includes ``sim-basics.h'', defines the base type ``sim_cia''
26    (the data type that contains complete current instruction address
27    information), include ``sim-base.h'':
28
29      #include "sim-basics.h"
30      typedef address_word sim_cia;
31      #include "sim-base.h"
32    
33    finally, two data types ``struct _sim_cpu' and ``struct sim_state'
34    are defined:
35
36      struct _sim_cpu {
37         ... simulator specific members ...
38         sim_cpu_base base;
39      };
40
41      struct sim_state {
42        sim_cpu cpu[MAX_NR_PROCESSORS];
43      #if (WITH_SMP)
44      #define STATE_CPU(sd,n) (&(sd)->cpu[n])
45      #else
46      #define STATE_CPU(sd,n) (&(sd)->cpu[0])
47      #endif
48        ... simulator specific members ...
49        sim_state_base base;
50      };
51
52    Note that `base' appears last.  This makes `base.magic' appear last
53    in the entire struct and helps catch miscompilation errors. */
54
55
56 #ifndef SIM_BASE_H
57 #define SIM_BASE_H
58
59 /* Pre-declare certain types. */
60
61 /* typedef <target-dependant> sim_cia; */
62 #ifndef NULL_CIA
63 #define NULL_CIA ((sim_cia) 0)
64 #endif
65 #ifndef INVALID_INSTRUCTION_ADDRESS
66 #define INVALID_INSTRUCTION_ADDRESS ((address_word)0 - 1)
67 #endif
68 typedef struct _sim_cpu sim_cpu;
69
70 #include "sim-module.h"
71
72 #include "sim-trace.h"
73 #include "sim-profile.h"
74 #include "sim-model.h"
75 #include "sim-core.h"
76 #include "sim-events.h"
77 #include "sim-io.h"
78 #include "sim-engine.h"
79 #include "sim-watch.h"
80
81
82 /* Global pointer to current state while sim_resume is running.
83    On a machine with lots of registers, it might be possible to reserve
84    one of them for current_state.  However on a machine with few registers
85    current_state can't permanently live in one and indirecting through it
86    will be slower [in which case one can have sim_resume set globals from
87    current_state for faster access].
88    If CURRENT_STATE_REG is defined, it means current_state is living in
89    a global register.  */
90
91
92 #ifdef CURRENT_STATE_REG
93 /* FIXME: wip */
94 #else
95 extern struct sim_state *current_state;
96 #endif
97
98
99 /* The simulator may provide different (and faster) definition.  */
100 #ifndef CURRENT_STATE
101 #define CURRENT_STATE current_state
102 #endif
103
104
105 typedef struct {
106
107   /* Simulator's argv[0].  */
108   const char *my_name;
109 #define STATE_MY_NAME(sd) ((sd)->base.my_name)
110
111   /* Who opened the simulator.  */
112   SIM_OPEN_KIND open_kind;
113 #define STATE_OPEN_KIND(sd) ((sd)->base.open_kind)
114
115   /* The host callbacks.  */
116   struct host_callback_struct *callback;
117 #define STATE_CALLBACK(sd) ((sd)->base.callback)
118
119 #if 0 /* FIXME: Not ready yet.  */
120   /* Stuff defined in sim-config.h.  */
121   struct sim_config config;
122 #define STATE_CONFIG(sd) ((sd)->base.config)
123 #endif
124
125   /* List of installed module `init' handlers.  */
126   MODULE_INIT_LIST *init_list;
127 #define STATE_INIT_LIST(sd) ((sd)->base.init_list)
128   /* List of installed module `uninstall' handlers.  */
129   MODULE_UNINSTALL_LIST *uninstall_list;
130 #define STATE_UNINSTALL_LIST(sd) ((sd)->base.uninstall_list)
131   /* List of installed module `resume' handlers.  */
132   MODULE_RESUME_LIST *resume_list;
133 #define STATE_RESUME_LIST(sd) ((sd)->base.resume_list)
134   /* List of installed module `suspend' handlers.  */
135   MODULE_SUSPEND_LIST *suspend_list;
136 #define STATE_SUSPEND_LIST(sd) ((sd)->base.suspend_list)
137
138   /* ??? This might be more appropriate in sim_cpu.  */
139   /* Machine tables for this cpu.  See sim-model.h.  */
140   const MODEL *model;
141 #define STATE_MODEL(sd) ((sd)->base.model)
142
143   /* Supported options.  */
144   struct option_list *options;
145 #define STATE_OPTIONS(sd) ((sd)->base.options)
146
147   /* Non-zero if -v specified.  */
148   int verbose_p;
149 #define STATE_VERBOSE_P(sd) ((sd)->base.verbose_p)
150
151   /* If non NULL, the BFD architecture specified on the command line */
152   const struct bfd_arch_info *architecture;
153 #define STATE_ARCHITECTURE(sd) ((sd)->base.architecture)
154
155   /* If non NULL, the bfd target specified on the command line */
156   const char *target;
157 #define STATE_TARGET(sd) ((sd)->base.target)
158
159   /* In standalone simulator, this is the program's arguments passed
160      on the command line.  */
161   char **prog_argv;
162 #define STATE_PROG_ARGV(sd) ((sd)->base.prog_argv)
163
164   /* The program's bfd.  */
165   struct _bfd *prog_bfd;
166 #define STATE_PROG_BFD(sd) ((sd)->base.prog_bfd)
167
168   /* The program's text section.  */
169   struct sec *text_section;
170   /* Starting and ending text section addresses from the bfd.  */
171   SIM_ADDR text_start, text_end;
172 #define STATE_TEXT_SECTION(sd) ((sd)->base.text_section)
173 #define STATE_TEXT_START(sd) ((sd)->base.text_start)
174 #define STATE_TEXT_END(sd) ((sd)->base.text_end)
175
176   /* Start address, set when the program is loaded from the bfd.  */
177   SIM_ADDR start_addr;
178 #define STATE_START_ADDR(sd) ((sd)->base.start_addr)
179
180 #if WITH_SCACHE
181   /* Size of the simulator's cache, if any.
182      This is not the target's cache.  It is the cache the simulator uses
183      to process instructions.  */
184   unsigned int scache_size;
185 #define STATE_SCACHE_SIZE(sd) ((sd)->base.scache_size)
186 #endif
187
188   /* FIXME: Move to top level sim_state struct (as some struct)?  */
189 #ifdef SIM_HAVE_FLATMEM
190   unsigned int mem_size;
191 #define STATE_MEM_SIZE(sd) ((sd)->base.mem_size)
192   unsigned int mem_base;
193 #define STATE_MEM_BASE(sd) ((sd)->base.mem_base)
194   unsigned char *memory;
195 #define STATE_MEMORY(sd) ((sd)->base.memory)
196 #endif
197
198   /* core memory bus */
199 #define STATE_CORE(sd) (&(sd)->base.core)
200   sim_core core;
201
202   /* event handler */
203 #define STATE_EVENTS(sd) (&(sd)->base.events)
204   sim_events events;
205
206   /* generic halt/resume engine */
207   sim_engine engine;
208 #define STATE_ENGINE(sd) (&(sd)->base.engine)
209
210   /* generic watchpoint support */
211   sim_watchpoints watchpoints;
212 #define STATE_WATCHPOINTS(sd) (&(sd)->base.watchpoints)
213
214   /* Marker for those wanting to do sanity checks.
215      This should remain the last member of this struct to help catch
216      miscompilation errors.  */
217   int magic;
218 #define SIM_MAGIC_NUMBER 0x4242
219 #define STATE_MAGIC(sd) ((sd)->base.magic)
220
221 } sim_state_base;
222
223
224 /* Pseudo baseclass for each cpu.  */
225
226 typedef struct {
227
228   /* Backlink to main state struct.  */
229   SIM_DESC state;
230 #define CPU_STATE(cpu) ((cpu)->base.state)
231
232   /* Processor specific core data */
233   sim_cpu_core core;
234 #define CPU_CORE(cpu) (& (cpu)->base.core)
235
236   /* Trace data.  See sim-trace.h.  */
237   TRACE_DATA trace_data;
238 #define CPU_TRACE_DATA(cpu) (& (cpu)->base.trace_data)
239
240   /* Maximum number of debuggable entities.
241      This debugging is not intended for normal use.
242      It is only enabled when the simulator is configured with --with-debug
243      which shouldn't normally be specified.  */
244 #ifndef MAX_DEBUG_VALUES
245 #define MAX_DEBUG_VALUES 4
246 #endif
247
248   /* Boolean array of specified debugging flags.  */
249   char debug_flags[MAX_DEBUG_VALUES];
250 #define CPU_DEBUG_FLAGS(cpu) ((cpu)->base.debug_flags)
251   /* Standard values.  */
252 #define DEBUG_INSN_IDX 0
253 #define DEBUG_NEXT_IDX 2 /* simulator specific debug bits begin here */
254
255   /* Debugging output goes to this or stderr if NULL.
256      We can't store `stderr' here as stderr goes through a callback.  */
257   FILE *debug_file;
258 #define CPU_DEBUG_FILE(cpu) ((cpu)->base.debug_file)
259
260   /* Profile data.  See sim-profile.h.  */
261   PROFILE_DATA profile_data;
262 #define CPU_PROFILE_DATA(cpu) (& (cpu)->base.profile_data)
263
264 } sim_cpu_base;
265
266
267 /* Functions for allocating/freeing a sim_state.  */
268 SIM_DESC sim_state_alloc PARAMS ((SIM_OPEN_KIND kind, host_callback *callback));
269 void sim_state_free PARAMS ((SIM_DESC));
270
271
272 #endif /* SIM_BASE_H */