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