14b169a6bSchristos /* This file defines the interface between the simulator and gdb. 24b169a6bSchristos 3*02f41505Schristos Copyright (C) 1993-2024 Free Software Foundation, Inc. 44b169a6bSchristos 54b169a6bSchristos This file is part of GDB. 64b169a6bSchristos 74b169a6bSchristos This program is free software; you can redistribute it and/or modify 84b169a6bSchristos it under the terms of the GNU General Public License as published by 94b169a6bSchristos the Free Software Foundation; either version 3 of the License, or 104b169a6bSchristos (at your option) any later version. 114b169a6bSchristos 124b169a6bSchristos This program is distributed in the hope that it will be useful, 134b169a6bSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 144b169a6bSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 154b169a6bSchristos GNU General Public License for more details. 164b169a6bSchristos 174b169a6bSchristos You should have received a copy of the GNU General Public License 184b169a6bSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 194b169a6bSchristos 204b169a6bSchristos #ifndef SIM_SIM_H 214b169a6bSchristos #define SIM_SIM_H 1 224b169a6bSchristos 23*02f41505Schristos #include <stdbool.h> 24*02f41505Schristos #include <stdint.h> 25*02f41505Schristos 264b169a6bSchristos #ifdef __cplusplus 274b169a6bSchristos extern "C" { 284b169a6bSchristos #endif 294b169a6bSchristos 304b169a6bSchristos /* Semi-opaque type used as result of sim_open and passed back to all 314b169a6bSchristos other routines. "desc" is short for "descriptor". 324b169a6bSchristos It is up to each simulator to define `sim_state'. */ 334b169a6bSchristos 344b169a6bSchristos typedef struct sim_state *SIM_DESC; 354b169a6bSchristos 364b169a6bSchristos 374b169a6bSchristos /* Values for `kind' arg to sim_open. */ 384b169a6bSchristos 394b169a6bSchristos typedef enum { 404b169a6bSchristos SIM_OPEN_STANDALONE, /* simulator used standalone (run.c) */ 414b169a6bSchristos SIM_OPEN_DEBUG /* simulator used by debugger (gdb) */ 424b169a6bSchristos } SIM_OPEN_KIND; 434b169a6bSchristos 444b169a6bSchristos 454b169a6bSchristos /* Return codes from various functions. */ 464b169a6bSchristos 474b169a6bSchristos typedef enum { 484b169a6bSchristos SIM_RC_FAIL = 0, 494b169a6bSchristos SIM_RC_OK = 1 504b169a6bSchristos } SIM_RC; 514b169a6bSchristos 524b169a6bSchristos 534b169a6bSchristos /* Some structs, as opaque types. */ 544b169a6bSchristos 554b169a6bSchristos struct bfd; 564b169a6bSchristos struct host_callback_struct; 574b169a6bSchristos 584b169a6bSchristos 594b169a6bSchristos /* Main simulator entry points. */ 604b169a6bSchristos 614b169a6bSchristos 624b169a6bSchristos /* Create a fully initialized simulator instance. 634b169a6bSchristos 644b169a6bSchristos (This function is called when the simulator is selected from the 654b169a6bSchristos gdb command line.) 664b169a6bSchristos 674b169a6bSchristos KIND specifies how the simulator shall be used. Currently there 684b169a6bSchristos are only two kinds: stand-alone and debug. 694b169a6bSchristos 704b169a6bSchristos CALLBACK specifies a standard host callback (defined in callback.h). 714b169a6bSchristos 724b169a6bSchristos ABFD, when non NULL, designates a target program. The program is 734b169a6bSchristos not loaded. 744b169a6bSchristos 754b169a6bSchristos ARGV is a standard ARGV pointer such as that passed from the 764b169a6bSchristos command line. The syntax of the argument list is is assumed to be 774b169a6bSchristos ``SIM-PROG { SIM-OPTION } [ TARGET-PROGRAM { TARGET-OPTION } ]''. 784b169a6bSchristos The trailing TARGET-PROGRAM and args are only valid for a 794b169a6bSchristos stand-alone simulator. 804b169a6bSchristos 814b169a6bSchristos On success, the result is a non NULL descriptor that shall be 824b169a6bSchristos passed to the other sim_foo functions. While the simulator 834b169a6bSchristos configuration can be parameterized by (in decreasing precedence) 844b169a6bSchristos ARGV's SIM-OPTION, ARGV's TARGET-PROGRAM and the ABFD argument, the 854b169a6bSchristos successful creation of the simulator shall not dependent on the 864b169a6bSchristos presence of any of these arguments/options. 874b169a6bSchristos 884b169a6bSchristos Hardware simulator: The created simulator shall be sufficiently 894b169a6bSchristos initialized to handle, with out restrictions any client requests 904b169a6bSchristos (including memory reads/writes, register fetch/stores and a 914b169a6bSchristos resume). 924b169a6bSchristos 934b169a6bSchristos Process simulator: that process is not created until a call to 944b169a6bSchristos sim_create_inferior. FIXME: What should the state of the simulator 954b169a6bSchristos be? */ 964b169a6bSchristos 974b169a6bSchristos SIM_DESC sim_open (SIM_OPEN_KIND kind, struct host_callback_struct *callback, 984b169a6bSchristos struct bfd *abfd, char * const *argv); 994b169a6bSchristos 1004b169a6bSchristos 1014b169a6bSchristos /* Destory a simulator instance. 1024b169a6bSchristos 1034b169a6bSchristos QUITTING is non-zero if we cannot hang on errors. 1044b169a6bSchristos 1054b169a6bSchristos This may involve freeing target memory and closing any open files 1064b169a6bSchristos and mmap'd areas. You cannot assume sim_kill has already been 1074b169a6bSchristos called. */ 1084b169a6bSchristos 1094b169a6bSchristos void sim_close (SIM_DESC sd, int quitting); 1104b169a6bSchristos 1114b169a6bSchristos 1124b169a6bSchristos /* Load program PROG into the simulators memory. 1134b169a6bSchristos 1144b169a6bSchristos If ABFD is non-NULL, the bfd for the file has already been opened. 1154b169a6bSchristos The result is a return code indicating success. 1164b169a6bSchristos 1174b169a6bSchristos Hardware simulator: Normally, each program section is written into 1184b169a6bSchristos memory according to that sections LMA using physical (direct) 1194b169a6bSchristos addressing. The exception being systems, such as PPC/CHRP, which 1204b169a6bSchristos support more complicated program loaders. A call to this function 1214b169a6bSchristos should not effect the state of the processor registers. Multiple 1224b169a6bSchristos calls to this function are permitted and have an accumulative 1234b169a6bSchristos effect. 1244b169a6bSchristos 1254b169a6bSchristos Process simulator: Calls to this function may be ignored. 1264b169a6bSchristos 1274b169a6bSchristos FIXME: Most hardware simulators load the image at the VMA using 1284b169a6bSchristos virtual addressing. 1294b169a6bSchristos 1304b169a6bSchristos FIXME: For some hardware targets, before a loaded program can be 1314b169a6bSchristos executed, it requires the manipulation of VM registers and tables. 1324b169a6bSchristos Such manipulation should probably (?) occure in 1334b169a6bSchristos sim_create_inferior. */ 1344b169a6bSchristos 1354b169a6bSchristos SIM_RC sim_load (SIM_DESC sd, const char *prog, struct bfd *abfd, int from_tty); 1364b169a6bSchristos 1374b169a6bSchristos 1384b169a6bSchristos /* Prepare to run the simulated program. 1394b169a6bSchristos 1404b169a6bSchristos ABFD, if not NULL, provides initial processor state information. 1414b169a6bSchristos ARGV and ENV, if non NULL, are NULL terminated lists of pointers. 1424b169a6bSchristos 1434b169a6bSchristos Hardware simulator: This function shall initialize the processor 1444b169a6bSchristos registers to a known value. The program counter and possibly stack 1454b169a6bSchristos pointer shall be set using information obtained from ABFD (or 1464b169a6bSchristos hardware reset defaults). ARGV and ENV, dependant on the target 1474b169a6bSchristos ABI, may be written to memory. 1484b169a6bSchristos 1494b169a6bSchristos Process simulator: After a call to this function, a new process 1504b169a6bSchristos instance shall exist. The TEXT, DATA, BSS and stack regions shall 1514b169a6bSchristos all be initialized, ARGV and ENV shall be written to process 1524b169a6bSchristos address space (according to the applicable ABI) and the program 1534b169a6bSchristos counter and stack pointer set accordingly. */ 1544b169a6bSchristos 1554b169a6bSchristos SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, 1564b169a6bSchristos char * const *argv, char * const *env); 1574b169a6bSchristos 1584b169a6bSchristos 1594b169a6bSchristos /* Fetch LENGTH bytes of the simulated program's memory. Start fetch 1604b169a6bSchristos at virtual address MEM and store in BUF. Result is number of bytes 1614b169a6bSchristos read, or zero if error. */ 1624b169a6bSchristos 163*02f41505Schristos uint64_t sim_read (SIM_DESC sd, uint64_t addr, void *buf, uint64_t length); 1644b169a6bSchristos 1654b169a6bSchristos 1664b169a6bSchristos /* Store LENGTH bytes from BUF into the simulated program's 1674b169a6bSchristos memory. Store bytes starting at virtual address MEM. Result is 1684b169a6bSchristos number of bytes write, or zero if error. */ 1694b169a6bSchristos 170*02f41505Schristos uint64_t sim_write (SIM_DESC sd, uint64_t addr, const void *buf, uint64_t length); 1714b169a6bSchristos 1724b169a6bSchristos 1734b169a6bSchristos /* Fetch register REGNO storing its raw (target endian) value in the 1744b169a6bSchristos LENGTH byte buffer BUF. Return the actual size of the register or 1754b169a6bSchristos zero if REGNO is not applicable. 1764b169a6bSchristos 1774b169a6bSchristos Legacy implementations ignore LENGTH and always return -1. 1784b169a6bSchristos 1794b169a6bSchristos If LENGTH does not match the size of REGNO no data is transfered 1804b169a6bSchristos (the actual register size is still returned). */ 1814b169a6bSchristos 1824b169a6bSchristos int sim_fetch_register (SIM_DESC sd, int regno, void *buf, int length); 1834b169a6bSchristos 1844b169a6bSchristos 1854b169a6bSchristos /* Store register REGNO from the raw (target endian) value in BUF. 1864b169a6bSchristos 1874b169a6bSchristos Return the actual size of the register, any size not equal to 1884b169a6bSchristos LENGTH indicates the register was not updated correctly. 1894b169a6bSchristos 1904b169a6bSchristos Return a LENGTH of -1 to indicate the register was not updated 1914b169a6bSchristos and an error has occurred. 1924b169a6bSchristos 1934b169a6bSchristos Return a LENGTH of 0 to indicate the register was not updated 1944b169a6bSchristos but no error has occurred. */ 1954b169a6bSchristos 1964b169a6bSchristos int sim_store_register (SIM_DESC sd, int regno, const void *buf, int length); 1974b169a6bSchristos 1984b169a6bSchristos 1994b169a6bSchristos /* Print whatever statistics the simulator has collected. 2004b169a6bSchristos 201*02f41505Schristos When VERBOSE is enabled, extra details will be shown. */ 2024b169a6bSchristos 203*02f41505Schristos void sim_info (SIM_DESC sd, bool verbose); 2044b169a6bSchristos 2054b169a6bSchristos 2064b169a6bSchristos /* Return a memory map in XML format. 2074b169a6bSchristos 2084b169a6bSchristos The caller must free the returned string. 2094b169a6bSchristos 2104b169a6bSchristos For details on the format, see GDB's Memory Map Format documentation. */ 2114b169a6bSchristos 2124b169a6bSchristos char *sim_memory_map (SIM_DESC sd); 2134b169a6bSchristos 2144b169a6bSchristos 2154b169a6bSchristos /* Run (or resume) the simulated program. 2164b169a6bSchristos 2174b169a6bSchristos STEP, when non-zero indicates that only a single simulator cycle 2184b169a6bSchristos should be emulated. 2194b169a6bSchristos 2204b169a6bSchristos SIGGNAL, if non-zero is a (HOST) SIGRC value indicating the type of 2214b169a6bSchristos event (hardware interrupt, signal) to be delivered to the simulated 2224b169a6bSchristos program. 2234b169a6bSchristos 2244b169a6bSchristos Hardware simulator: If the SIGRC value returned by 2254b169a6bSchristos sim_stop_reason() is passed back to the simulator via SIGGNAL then 2264b169a6bSchristos the hardware simulator shall correctly deliver the hardware event 2274b169a6bSchristos indicated by that signal. If a value of zero is passed in then the 2284b169a6bSchristos simulation will continue as if there were no outstanding signal. 2294b169a6bSchristos The effect of any other SIGGNAL value is is implementation 2304b169a6bSchristos dependant. 2314b169a6bSchristos 2324b169a6bSchristos Process simulator: If SIGRC is non-zero then the corresponding 2334b169a6bSchristos signal is delivered to the simulated program and execution is then 2344b169a6bSchristos continued. A zero SIGRC value indicates that the program should 2354b169a6bSchristos continue as normal. */ 2364b169a6bSchristos 2374b169a6bSchristos void sim_resume (SIM_DESC sd, int step, int siggnal); 2384b169a6bSchristos 2394b169a6bSchristos 2404b169a6bSchristos /* Asynchronous request to stop the simulation. 2414b169a6bSchristos A nonzero return indicates that the simulator is able to handle 2424b169a6bSchristos the request */ 2434b169a6bSchristos 2444b169a6bSchristos int sim_stop (SIM_DESC sd); 2454b169a6bSchristos 2464b169a6bSchristos 2474b169a6bSchristos /* Fetch the REASON why the program stopped. 2484b169a6bSchristos 2494b169a6bSchristos SIM_EXITED: The program has terminated. SIGRC indicates the target 2504b169a6bSchristos dependant exit status. 2514b169a6bSchristos 2524b169a6bSchristos SIM_STOPPED: The program has stopped. SIGRC uses the host's signal 2534b169a6bSchristos numbering as a way of identifying the reaon: program interrupted by 2544b169a6bSchristos user via a sim_stop request (SIGINT); a breakpoint instruction 2554b169a6bSchristos (SIGTRAP); a completed single step (SIGTRAP); an internal error 2564b169a6bSchristos condition (SIGABRT); an illegal instruction (SIGILL); Access to an 2574b169a6bSchristos undefined memory region (SIGSEGV); Mis-aligned memory access 2584b169a6bSchristos (SIGBUS). For some signals information in addition to the signal 2594b169a6bSchristos number may be retained by the simulator (e.g. offending address), 2604b169a6bSchristos that information is not directly accessable via this interface. 2614b169a6bSchristos 2624b169a6bSchristos SIM_SIGNALLED: The program has been terminated by a signal. The 2634b169a6bSchristos simulator has encountered target code that causes the program 2644b169a6bSchristos to exit with signal SIGRC. 2654b169a6bSchristos 2664b169a6bSchristos SIM_RUNNING, SIM_POLLING: The return of one of these values 2674b169a6bSchristos indicates a problem internal to the simulator. */ 2684b169a6bSchristos 2694b169a6bSchristos enum sim_stop { sim_running, sim_polling, sim_exited, sim_stopped, sim_signalled }; 2704b169a6bSchristos 2714b169a6bSchristos void sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc); 2724b169a6bSchristos 2734b169a6bSchristos 2744b169a6bSchristos /* Passthru for other commands that the simulator might support. 2754b169a6bSchristos Simulators should be prepared to deal with any combination of NULL 2764b169a6bSchristos or empty CMD. */ 2774b169a6bSchristos 2784b169a6bSchristos void sim_do_command (SIM_DESC sd, const char *cmd); 2794b169a6bSchristos 2804b169a6bSchristos /* Complete a command based on the available sim commands. Returns an 2814b169a6bSchristos array of possible matches. */ 2824b169a6bSchristos 2834b169a6bSchristos char **sim_complete_command (SIM_DESC sd, const char *text, const char *word); 2844b169a6bSchristos 2854b169a6bSchristos #ifdef __cplusplus 2864b169a6bSchristos } 2874b169a6bSchristos #endif 2884b169a6bSchristos 2894b169a6bSchristos #endif /* !defined (SIM_SIM_H) */ 290