1/* This file defines the interface between the simulator and gdb.
2
3   Copyright (C) 1993-2022 Free Software Foundation, Inc.
4
5   This file is part of GDB.
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 3 of the License, or
10   (at your option) 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
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20#ifndef SIM_SIM_H
21#define SIM_SIM_H 1
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/* This file is used when building stand-alone simulators, so isolate this
28   file from gdb.  */
29
30typedef unsigned int SIM_ADDR;
31
32
33/* Semi-opaque type used as result of sim_open and passed back to all
34   other routines.  "desc" is short for "descriptor".
35   It is up to each simulator to define `sim_state'.  */
36
37typedef struct sim_state *SIM_DESC;
38
39
40/* Values for `kind' arg to sim_open.  */
41
42typedef enum {
43  SIM_OPEN_STANDALONE, /* simulator used standalone (run.c) */
44  SIM_OPEN_DEBUG       /* simulator used by debugger (gdb) */
45} SIM_OPEN_KIND;
46
47
48/* Return codes from various functions.  */
49
50typedef enum {
51  SIM_RC_FAIL = 0,
52  SIM_RC_OK = 1
53} SIM_RC;
54
55
56/* Some structs, as opaque types.  */
57
58struct bfd;
59struct host_callback_struct;
60
61
62/* Main simulator entry points.  */
63
64
65/* Create a fully initialized simulator instance.
66
67   (This function is called when the simulator is selected from the
68   gdb command line.)
69
70   KIND specifies how the simulator shall be used.  Currently there
71   are only two kinds: stand-alone and debug.
72
73   CALLBACK specifies a standard host callback (defined in callback.h).
74
75   ABFD, when non NULL, designates a target program.  The program is
76   not loaded.
77
78   ARGV is a standard ARGV pointer such as that passed from the
79   command line.  The syntax of the argument list is is assumed to be
80   ``SIM-PROG { SIM-OPTION } [ TARGET-PROGRAM { TARGET-OPTION } ]''.
81   The trailing TARGET-PROGRAM and args are only valid for a
82   stand-alone simulator.
83
84   On success, the result is a non NULL descriptor that shall be
85   passed to the other sim_foo functions.  While the simulator
86   configuration can be parameterized by (in decreasing precedence)
87   ARGV's SIM-OPTION, ARGV's TARGET-PROGRAM and the ABFD argument, the
88   successful creation of the simulator shall not dependent on the
89   presence of any of these arguments/options.
90
91   Hardware simulator: The created simulator shall be sufficiently
92   initialized to handle, with out restrictions any client requests
93   (including memory reads/writes, register fetch/stores and a
94   resume).
95
96   Process simulator: that process is not created until a call to
97   sim_create_inferior.  FIXME: What should the state of the simulator
98   be? */
99
100SIM_DESC sim_open (SIM_OPEN_KIND kind, struct host_callback_struct *callback,
101		   struct bfd *abfd, char * const *argv);
102
103
104/* Destory a simulator instance.
105
106   QUITTING is non-zero if we cannot hang on errors.
107
108   This may involve freeing target memory and closing any open files
109   and mmap'd areas.  You cannot assume sim_kill has already been
110   called. */
111
112void sim_close (SIM_DESC sd, int quitting);
113
114
115/* Load program PROG into the simulators memory.
116
117   If ABFD is non-NULL, the bfd for the file has already been opened.
118   The result is a return code indicating success.
119
120   Hardware simulator: Normally, each program section is written into
121   memory according to that sections LMA using physical (direct)
122   addressing.  The exception being systems, such as PPC/CHRP, which
123   support more complicated program loaders.  A call to this function
124   should not effect the state of the processor registers.  Multiple
125   calls to this function are permitted and have an accumulative
126   effect.
127
128   Process simulator: Calls to this function may be ignored.
129
130   FIXME: Most hardware simulators load the image at the VMA using
131   virtual addressing.
132
133   FIXME: For some hardware targets, before a loaded program can be
134   executed, it requires the manipulation of VM registers and tables.
135   Such manipulation should probably (?) occure in
136   sim_create_inferior. */
137
138SIM_RC sim_load (SIM_DESC sd, const char *prog, struct bfd *abfd, int from_tty);
139
140
141/* Prepare to run the simulated program.
142
143   ABFD, if not NULL, provides initial processor state information.
144   ARGV and ENV, if non NULL, are NULL terminated lists of pointers.
145
146   Hardware simulator: This function shall initialize the processor
147   registers to a known value.  The program counter and possibly stack
148   pointer shall be set using information obtained from ABFD (or
149   hardware reset defaults).  ARGV and ENV, dependant on the target
150   ABI, may be written to memory.
151
152   Process simulator: After a call to this function, a new process
153   instance shall exist. The TEXT, DATA, BSS and stack regions shall
154   all be initialized, ARGV and ENV shall be written to process
155   address space (according to the applicable ABI) and the program
156   counter and stack pointer set accordingly. */
157
158SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
159			    char * const *argv, char * const *env);
160
161
162/* Fetch LENGTH bytes of the simulated program's memory.  Start fetch
163   at virtual address MEM and store in BUF.  Result is number of bytes
164   read, or zero if error.  */
165
166int sim_read (SIM_DESC sd, SIM_ADDR mem, void *buf, int length);
167
168
169/* Store LENGTH bytes from BUF into the simulated program's
170   memory. Store bytes starting at virtual address MEM. Result is
171   number of bytes write, or zero if error.  */
172
173int sim_write (SIM_DESC sd, SIM_ADDR mem, const void *buf, int length);
174
175
176/* Fetch register REGNO storing its raw (target endian) value in the
177   LENGTH byte buffer BUF.  Return the actual size of the register or
178   zero if REGNO is not applicable.
179
180   Legacy implementations ignore LENGTH and always return -1.
181
182   If LENGTH does not match the size of REGNO no data is transfered
183   (the actual register size is still returned). */
184
185int sim_fetch_register (SIM_DESC sd, int regno, void *buf, int length);
186
187
188/* Store register REGNO from the raw (target endian) value in BUF.
189
190   Return the actual size of the register, any size not equal to
191   LENGTH indicates the register was not updated correctly.
192
193   Return a LENGTH of -1 to indicate the register was not updated
194   and an error has occurred.
195
196   Return a LENGTH of 0 to indicate the register was not updated
197   but no error has occurred. */
198
199int sim_store_register (SIM_DESC sd, int regno, const void *buf, int length);
200
201
202/* Print whatever statistics the simulator has collected.
203
204   VERBOSE is currently unused and must always be zero.  */
205
206void sim_info (SIM_DESC sd, int verbose);
207
208
209/* Return a memory map in XML format.
210
211   The caller must free the returned string.
212
213   For details on the format, see GDB's Memory Map Format documentation.  */
214
215char *sim_memory_map (SIM_DESC sd);
216
217
218/* Run (or resume) the simulated program.
219
220   STEP, when non-zero indicates that only a single simulator cycle
221   should be emulated.
222
223   SIGGNAL, if non-zero is a (HOST) SIGRC value indicating the type of
224   event (hardware interrupt, signal) to be delivered to the simulated
225   program.
226
227   Hardware simulator: If the SIGRC value returned by
228   sim_stop_reason() is passed back to the simulator via SIGGNAL then
229   the hardware simulator shall correctly deliver the hardware event
230   indicated by that signal.  If a value of zero is passed in then the
231   simulation will continue as if there were no outstanding signal.
232   The effect of any other SIGGNAL value is is implementation
233   dependant.
234
235   Process simulator: If SIGRC is non-zero then the corresponding
236   signal is delivered to the simulated program and execution is then
237   continued.  A zero SIGRC value indicates that the program should
238   continue as normal. */
239
240void sim_resume (SIM_DESC sd, int step, int siggnal);
241
242
243/* Asynchronous request to stop the simulation.
244   A nonzero return indicates that the simulator is able to handle
245   the request */
246
247int sim_stop (SIM_DESC sd);
248
249
250/* Fetch the REASON why the program stopped.
251
252   SIM_EXITED: The program has terminated. SIGRC indicates the target
253   dependant exit status.
254
255   SIM_STOPPED: The program has stopped.  SIGRC uses the host's signal
256   numbering as a way of identifying the reaon: program interrupted by
257   user via a sim_stop request (SIGINT); a breakpoint instruction
258   (SIGTRAP); a completed single step (SIGTRAP); an internal error
259   condition (SIGABRT); an illegal instruction (SIGILL); Access to an
260   undefined memory region (SIGSEGV); Mis-aligned memory access
261   (SIGBUS).  For some signals information in addition to the signal
262   number may be retained by the simulator (e.g. offending address),
263   that information is not directly accessable via this interface.
264
265   SIM_SIGNALLED: The program has been terminated by a signal. The
266   simulator has encountered target code that causes the program
267   to exit with signal SIGRC.
268
269   SIM_RUNNING, SIM_POLLING: The return of one of these values
270   indicates a problem internal to the simulator. */
271
272enum sim_stop { sim_running, sim_polling, sim_exited, sim_stopped, sim_signalled };
273
274void sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc);
275
276
277/* Passthru for other commands that the simulator might support.
278   Simulators should be prepared to deal with any combination of NULL
279   or empty CMD. */
280
281void sim_do_command (SIM_DESC sd, const char *cmd);
282
283/* Complete a command based on the available sim commands.  Returns an
284   array of possible matches.  */
285
286char **sim_complete_command (SIM_DESC sd, const char *text, const char *word);
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif /* !defined (SIM_SIM_H) */
293