1/* Simulator pseudo baseclass.
2
3   Copyright 1997-2023 Free Software Foundation, Inc.
4
5   Contributed by Cygnus Support.
6
7This file is part of GDB, the GNU debugger.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 3 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22
23/* Simulator state pseudo baseclass.
24
25   Each simulator is required to have the file ``sim-main.h''.  That
26   file includes ``sim-basics.h'', defines the base type ``sim_cia''
27   (the data type that contains complete current instruction address
28   information), include ``sim-base.h'':
29
30     #include "sim-basics.h"
31     /-* If `sim_cia' is not an integral value (e.g. a struct), define
32         CIA_ADDR to return the integral value.  *-/
33     /-* typedef struct {...} sim_cia; *-/
34     /-* #define CIA_ADDR(cia) (...) *-/
35     #include "sim-base.h"
36
37   finally, two data types `struct _sim_cpu' and `struct sim_state'
38   are defined:
39
40     struct _sim_cpu {
41        ... simulator specific members ...
42        sim_cpu_base base;
43     };
44
45   If your sim needs to allocate sim-wide state, use STATE_ARCH_DATA.  */
46
47
48#ifndef SIM_BASE_H
49#define SIM_BASE_H
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/* Pre-declare certain types. */
56
57/* typedef <target-dependant> sim_cia; */
58#ifndef NULL_CIA
59#define NULL_CIA ((sim_cia) 0)
60#endif
61/* Return the current instruction address as a number.
62   Some targets treat the current instruction address as a struct
63   (e.g. for delay slot handling).  */
64#ifndef CIA_ADDR
65#define CIA_ADDR(cia) (cia)
66typedef address_word sim_cia;
67#endif
68#ifndef INVALID_INSTRUCTION_ADDRESS
69#define INVALID_INSTRUCTION_ADDRESS ((address_word)0 - 1)
70#endif
71
72/* TODO: Probably should just delete SIM_CPU.  */
73typedef struct _sim_cpu SIM_CPU;
74typedef struct _sim_cpu sim_cpu;
75
76#include "sim-module.h"
77
78#include "sim-arange.h"
79#include "sim-trace.h"
80#include "sim-core.h"
81#include "sim-events.h"
82#include "sim-profile.h"
83#include "sim-model.h"
84#include "sim-io.h"
85#include "sim-engine.h"
86#include "sim-watch.h"
87#include "sim-memopt.h"
88#include "sim-cpu.h"
89#include "sim-assert.h"
90
91struct sim_state {
92  /* All the cpus for this instance.  */
93  sim_cpu *cpu[MAX_NR_PROCESSORS];
94#if (WITH_SMP)
95# define STATE_CPU(sd, n) ((sd)->cpu[n])
96#else
97# define STATE_CPU(sd, n) ((sd)->cpu[0])
98#endif
99
100  /* Simulator's argv[0].  */
101  const char *my_name;
102#define STATE_MY_NAME(sd) ((sd)->my_name)
103
104  /* Who opened the simulator.  */
105  SIM_OPEN_KIND open_kind;
106#define STATE_OPEN_KIND(sd) ((sd)->open_kind)
107
108  /* The host callbacks.  */
109  struct host_callback_struct *callback;
110#define STATE_CALLBACK(sd) ((sd)->callback)
111
112  /* The type of simulation environment (user/operating).  */
113  enum sim_environment environment;
114#define STATE_ENVIRONMENT(sd) ((sd)->environment)
115
116#if 0 /* FIXME: Not ready yet.  */
117  /* Stuff defined in sim-config.h.  */
118  struct sim_config config;
119#define STATE_CONFIG(sd) ((sd)->config)
120#endif
121
122  /* List of installed module `init' handlers.  */
123  struct module_list *modules;
124#define STATE_MODULES(sd) ((sd)->modules)
125
126  /* Supported options.  */
127  struct option_list *options;
128#define STATE_OPTIONS(sd) ((sd)->options)
129
130  /* Non-zero if -v specified.  */
131  int verbose_p;
132#define STATE_VERBOSE_P(sd) ((sd)->verbose_p)
133
134  /* Non cpu-specific trace data.  See sim-trace.h.  */
135  TRACE_DATA trace_data;
136#define STATE_TRACE_DATA(sd) (& (sd)->trace_data)
137
138  /* If non NULL, the BFD architecture specified on the command line */
139  const struct bfd_arch_info *architecture;
140#define STATE_ARCHITECTURE(sd) ((sd)->architecture)
141
142  /* If non NULL, the bfd target specified on the command line */
143  const char *target;
144#define STATE_TARGET(sd) ((sd)->target)
145
146  /* List of machs available.  */
147  const SIM_MACH * const *machs;
148#define STATE_MACHS(sd) ((sd)->machs)
149
150  /* If non-NULL, the model to select for CPUs.  */
151  const char *model_name;
152#define STATE_MODEL_NAME(sd) ((sd)->model_name)
153
154  /* In standalone simulator, this is the program to run.  Not to be confused
155     with argv which are the strings passed to the program itself.  */
156  char *prog_file;
157#define STATE_PROG_FILE(sd) ((sd)->prog_file)
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)->prog_argv)
163
164  /* Thie is the program's argv[0] override.  */
165  char *prog_argv0;
166#define STATE_PROG_ARGV0(sd) ((sd)->prog_argv0)
167
168  /* The program's environment.  */
169  char **prog_envp;
170#define STATE_PROG_ENVP(sd) ((sd)->prog_envp)
171
172  /* The program's bfd.  */
173  struct bfd *prog_bfd;
174#define STATE_PROG_BFD(sd) ((sd)->prog_bfd)
175
176  /* Symbol table for prog_bfd */
177  struct bfd_symbol **prog_syms;
178#define STATE_PROG_SYMS(sd) ((sd)->prog_syms)
179
180  /* Number of prog_syms symbols.  */
181  long prog_syms_count;
182#define STATE_PROG_SYMS_COUNT(sd) ((sd)->prog_syms_count)
183
184  /* The program's text section.  */
185  struct bfd_section *text_section;
186  /* Starting and ending text section addresses from the bfd.  */
187  bfd_vma text_start, text_end;
188#define STATE_TEXT_SECTION(sd) ((sd)->text_section)
189#define STATE_TEXT_START(sd) ((sd)->text_start)
190#define STATE_TEXT_END(sd) ((sd)->text_end)
191
192  /* Start address, set when the program is loaded from the bfd.  */
193  bfd_vma start_addr;
194#define STATE_START_ADDR(sd) ((sd)->start_addr)
195
196  /* Size of the simulator's cache, if any.
197     This is not the target's cache.  It is the cache the simulator uses
198     to process instructions.  */
199  unsigned int scache_size;
200#define STATE_SCACHE_SIZE(sd) ((sd)->scache_size)
201
202  /* core memory bus */
203#define STATE_CORE(sd) (&(sd)->core)
204  sim_core core;
205
206  /* Record of memory sections added via the memory-options interface.  */
207#define STATE_MEMOPT(sd) ((sd)->memopt)
208  sim_memopt *memopt;
209
210  /* event handler */
211#define STATE_EVENTS(sd) (&(sd)->events)
212  sim_events events;
213
214  /* generic halt/resume engine */
215  sim_engine engine;
216#define STATE_ENGINE(sd) (&(sd)->engine)
217
218  /* generic watchpoint support */
219  sim_watchpoints watchpoints;
220#define STATE_WATCHPOINTS(sd) (&(sd)->watchpoints)
221
222#if WITH_HW
223  struct sim_hw *hw;
224#define STATE_HW(sd) ((sd)->hw)
225#endif
226
227  /* Should image loads be performed using the LMA or VMA?  Older
228     simulators use the VMA while newer simulators prefer the LMA. */
229  int load_at_lma_p;
230#define STATE_LOAD_AT_LMA_P(SD) ((SD)->load_at_lma_p)
231
232  /* Pointer for sim target to store arbitrary state data.  Normally the
233     target should define a struct and use it here.  */
234  void *arch_data;
235#define STATE_ARCH_DATA(sd) ((sd)->arch_data)
236
237  /* Marker for those wanting to do sanity checks.
238     This should remain the last member of this struct to help catch
239     miscompilation errors.  */
240  int magic;
241#define SIM_MAGIC_NUMBER 0x4242
242#define STATE_MAGIC(sd) ((sd)->magic)
243};
244
245/* Functions for allocating/freeing a sim_state.  */
246SIM_DESC sim_state_alloc_extra (SIM_OPEN_KIND kind, host_callback *callback,
247				size_t extra_bytes);
248#define sim_state_alloc(kind, callback) sim_state_alloc_extra(kind, callback, 0)
249
250void sim_state_free (SIM_DESC);
251
252#ifdef __cplusplus
253}
254#endif
255
256#endif /* SIM_BASE_H */
257