Home | History | Annotate | Line # | Download | only in sim
callback.h revision 1.1
      1 /* Remote target system call callback support.
      2    Copyright (C) 1997-2022 Free Software Foundation, Inc.
      3    Contributed by Cygnus Solutions.
      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 /* This interface isn't intended to be specific to any particular kind
     21    of remote (hardware, simulator, whatever).  As such, support for it
     22    (e.g. sim/common/callback.c) should *not* live in the simulator source
     23    tree, nor should it live in the gdb source tree.  */
     24 
     25 /* There are various ways to handle system calls:
     26 
     27    1) Have a simulator intercept the appropriate trap instruction and
     28    directly perform the system call on behalf of the target program.
     29    This is the typical way of handling system calls for embedded targets.
     30    [Handling system calls for embedded targets isn't that much of an
     31    oxymoron as running compiler testsuites make use of the capability.]
     32 
     33    This method of system call handling is done when STATE_ENVIRONMENT
     34    is ENVIRONMENT_USER.
     35 
     36    2) Have a simulator emulate the hardware as much as possible.
     37    If the program running on the real hardware communicates with some sort
     38    of target manager, one would want to be able to run this program on the
     39    simulator as well.
     40 
     41    This method of system call handling is done when STATE_ENVIRONMENT
     42    is ENVIRONMENT_OPERATING.
     43 */
     44 
     45 #ifndef SIM_CALLBACK_H
     46 #define SIM_CALLBACK_H
     47 
     48 #include <stdarg.h>
     49 #include <stdint.h>
     50 
     51 #include <ansidecl.h>
     52 /* Needed for enum bfd_endian.  */
     53 #include <bfd.h>
     54 
     55 /* Mapping of host/target values.  */
     57 /* ??? For debugging purposes, one might want to add a string of the
     58    name of the symbol.  */
     59 
     60 typedef struct {
     61   const char *name;
     62   int host_val;
     63   int target_val;
     64 } CB_TARGET_DEFS_MAP;
     65 
     66 #define MAX_CALLBACK_FDS 10
     67 
     68 /* Forward decl for stat/fstat.  */
     69 struct stat;
     70 
     71 typedef struct host_callback_struct host_callback;
     72 
     73 struct host_callback_struct
     74 {
     75   int (*close) (host_callback *,int);
     76   int (*get_errno) (host_callback *);
     77   int (*isatty) (host_callback *, int);
     78   int64_t (*lseek) (host_callback *, int, int64_t, int);
     79   int (*open) (host_callback *, const char*, int mode);
     80   int (*read) (host_callback *,int,  char *, int);
     81   int (*read_stdin) ( host_callback *, char *, int);
     82   int (*rename) (host_callback *, const char *, const char *);
     83   int (*system) (host_callback *, const char *);
     84   int64_t (*time) (host_callback *);
     85   int (*unlink) (host_callback *, const char *);
     86   int (*write) (host_callback *,int, const char *, int);
     87   int (*write_stdout) (host_callback *, const char *, int);
     88   void (*flush_stdout) (host_callback *);
     89   int (*write_stderr) (host_callback *, const char *, int);
     90   void (*flush_stderr) (host_callback *);
     91   int (*to_stat) (host_callback *, const char *, struct stat *);
     92   int (*to_fstat) (host_callback *, int, struct stat *);
     93   int (*to_lstat) (host_callback *, const char *, struct stat *);
     94   int (*ftruncate) (host_callback *, int, int64_t);
     95   int (*truncate) (host_callback *, const char *, int64_t);
     96   int (*getpid) (host_callback *);
     97   int (*kill) (host_callback *, int, int);
     98   int (*pipe) (host_callback *, int *);
     99 
    100   /* Called by the framework when a read call has emptied a pipe buffer.  */
    101   void (*pipe_empty) (host_callback *, int read_fd, int write_fd);
    102 
    103   /* Called by the framework when a write call makes a pipe buffer
    104      non-empty.  */
    105   void (*pipe_nonempty) (host_callback *, int read_fd, int write_fd);
    106 
    107   /* When present, call to the client to give it the oportunity to
    108      poll any io devices for a request to quit (indicated by a nonzero
    109      return value). */
    110   int (*poll_quit) (host_callback *);
    111 
    112   /* Used when the target has gone away, so we can close open
    113      handles and free memory etc etc.  */
    114   int (*shutdown) (host_callback *);
    115   int (*init)     (host_callback *);
    116 
    117   /* depreciated, use vprintf_filtered - Talk to the user on a console.  */
    118   void (*printf_filtered) (host_callback *, const char *, ...)
    119     ATTRIBUTE_PRINTF_2;
    120 
    121   /* Talk to the user on a console.  */
    122   void (*vprintf_filtered) (host_callback *, const char *, va_list)
    123     ATTRIBUTE_PRINTF (2, 0);
    124 
    125   /* Same as vprintf_filtered but to stderr.  */
    126   void (*evprintf_filtered) (host_callback *, const char *, va_list)
    127     ATTRIBUTE_PRINTF (2, 0);
    128 
    129   /* Print an error message and "exit".
    130      In the case of gdb "exiting" means doing a longjmp back to the main
    131      command loop.  */
    132   void (*error) (host_callback *, const char *, ...)
    133     ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_2;
    134 
    135   int last_errno;		/* host format */
    136 
    137   int fdmap[MAX_CALLBACK_FDS];
    138   /* fd_buddy is used to contruct circular lists of target fds that point to
    139      the same host fd.  A uniquely mapped fd points to itself; for a closed
    140      one, fd_buddy has the value -1.  The host file descriptors for stdin /
    141      stdout / stderr are never closed by the simulators, so they are put
    142      in a special fd_buddy circular list which also has MAX_CALLBACK_FDS
    143      as a member.  */
    144   /* ??? We don't have a callback entry for dup, although it is trival to
    145      implement now.  */
    146   short fd_buddy[MAX_CALLBACK_FDS+1];
    147 
    148   /* 0 = none, >0 = reader (index of writer),
    149      <0 = writer (negative index of reader).
    150      If abs (ispipe[N]) == N, then N is an end of a pipe whose other
    151      end is closed.  */
    152   short ispipe[MAX_CALLBACK_FDS];
    153 
    154   /* A writer stores the buffer at its index.  Consecutive writes
    155      realloc the buffer and add to the size.  The reader indicates the
    156      read part in its .size, until it has consumed it all, at which
    157      point it deallocates the buffer and zeroes out both sizes.  */
    158   struct pipe_write_buffer
    159   {
    160     int size;
    161     char *buffer;
    162   } pipe_buffer[MAX_CALLBACK_FDS];
    163 
    164   /* System call numbers.  */
    165   CB_TARGET_DEFS_MAP *syscall_map;
    166   /* Errno values.  */
    167   CB_TARGET_DEFS_MAP *errno_map;
    168   /* Flags to the open system call.  */
    169   CB_TARGET_DEFS_MAP *open_map;
    170   /* Signal numbers.  */
    171   CB_TARGET_DEFS_MAP *signal_map;
    172   /* Layout of `stat' struct.
    173      The format is a series of "name,length" pairs separated by colons.
    174      Empty space is indicated with a `name' of "space".
    175      All padding must be explicitly mentioned.
    176      Lengths are in bytes.  If this needs to be extended to bits,
    177      use "name.bits".
    178      Example: "st_dev,4:st_ino,4:st_mode,4:..."  */
    179   const char *stat_map;
    180 
    181   enum bfd_endian target_endian;
    182 
    183   /* Program command line options.  */
    184   char **argv;
    185 
    186   /* Program environment.  */
    187   char **envp;
    188 
    189   /* Size of an "int" on the target (for syscalls whose ABI uses "int").
    190      This must include padding, and only padding-at-higher-address is
    191      supported.  For example, a 64-bit target with 32-bit int:s which
    192      are padded to 64 bits when in an array, should supposedly set this
    193      to 8.  The default is 4 which matches ILP32 targets and 64-bit
    194      targets with 32-bit ints and no padding.  */
    195   int target_sizeof_int;
    196 
    197   /* Marker for those wanting to do sanity checks.
    198      This should remain the last member of this struct to help catch
    199      miscompilation errors. */
    200 #define HOST_CALLBACK_MAGIC 4705 /* teds constant */
    201   int magic;
    202 };
    203 
    204 extern host_callback default_callback;
    205 
    206 /* Canonical versions of system call numbers.
    208    It's not intended to willy-nilly throw every system call ever heard
    209    of in here.  Only include those that have an important use.
    210    ??? One can certainly start a discussion over the ones that are currently
    211    here, but that will always be true.  */
    212 
    213 /* These are used by the ANSI C support of libc.  */
    214 #define	CB_SYS_exit	1
    215 #define	CB_SYS_open	2
    216 #define	CB_SYS_close	3
    217 #define	CB_SYS_read	4
    218 #define	CB_SYS_write	5
    219 #define	CB_SYS_lseek	6
    220 #define	CB_SYS_unlink	7
    221 #define	CB_SYS_getpid	8
    222 #define	CB_SYS_kill	9
    223 #define CB_SYS_fstat    10
    224 /*#define CB_SYS_sbrk	11 - not currently a system call, but reserved.  */
    225 
    226 /* ARGV support.  */
    227 #define CB_SYS_argvlen	12
    228 #define CB_SYS_argv	13
    229 
    230 /* These are extras added for one reason or another.  */
    231 #define CB_SYS_chdir	14
    232 #define CB_SYS_stat	15
    233 #define CB_SYS_chmod 	16
    234 #define CB_SYS_utime 	17
    235 #define CB_SYS_time 	18
    236 
    237 /* More standard syscalls.  */
    238 #define CB_SYS_lstat    19
    239 #define CB_SYS_rename	20
    240 #define CB_SYS_truncate	21
    241 #define CB_SYS_ftruncate 22
    242 #define CB_SYS_pipe 	23
    243 
    244 /* New ARGV support.  */
    245 #define CB_SYS_argc	24
    246 #define CB_SYS_argnlen	25
    247 #define CB_SYS_argn	26
    248 
    249 /* Struct use to pass and return information necessary to perform a
    251    system call.  */
    252 /* FIXME: Need to consider target word size.  */
    253 
    254 typedef struct cb_syscall {
    255   /* The target's value of what system call to perform.  */
    256   int func;
    257   /* The arguments to the syscall.  */
    258   long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
    259 
    260   /* The result.  */
    261   long result;
    262   /* Some system calls have two results.  */
    263   long result2;
    264   /* The target's errno value, or 0 if success.
    265      This is converted to the target's value with host_to_target_errno.  */
    266   int errcode;
    267 
    268   /* Working space to be used by memory read/write callbacks.  */
    269   void *p1;
    270   void *p2;
    271   long x1,x2;
    272 
    273   /* Callbacks for reading/writing memory (e.g. for read/write syscalls).
    274      ??? long or unsigned long might be better to use for the `count'
    275      argument here.  We mimic sim_{read,write} for now.  Be careful to
    276      test any changes with -Wall -Werror, mixed signed comparisons
    277      will get you.  */
    278   int (*read_mem) (host_callback * /*cb*/, struct cb_syscall * /*sc*/,
    279 		   unsigned long /*taddr*/, char * /*buf*/,
    280 		   int /*bytes*/);
    281   int (*write_mem) (host_callback * /*cb*/, struct cb_syscall * /*sc*/,
    282 		    unsigned long /*taddr*/, const char * /*buf*/,
    283 		    int /*bytes*/);
    284 
    285   /* For sanity checking, should be last entry.  */
    286   int magic;
    287 } CB_SYSCALL;
    288 
    289 /* Magic number sanity checker.  */
    290 #define CB_SYSCALL_MAGIC 0x12344321
    291 
    292 /* Macro to initialize CB_SYSCALL.  Called first, before filling in
    293    any fields.  */
    294 #define CB_SYSCALL_INIT(sc) \
    295 do { \
    296   memset ((sc), 0, sizeof (*(sc))); \
    297   (sc)->magic = CB_SYSCALL_MAGIC; \
    298 } while (0)
    299 
    300 /* Return codes for various interface routines.  */
    302 
    303 typedef enum {
    304   CB_RC_OK = 0,
    305   /* generic error */
    306   CB_RC_ERR,
    307   /* either file not found or no read access */
    308   CB_RC_ACCESS,
    309   CB_RC_NO_MEM
    310 } CB_RC;
    311 
    312 /* Read in target values for system call numbers, errno values, signals.  */
    313 CB_RC cb_read_target_syscall_maps (host_callback *, const char *);
    314 
    315 /* Translate target to host syscall function numbers.  */
    316 int cb_target_to_host_syscall (host_callback *, int);
    317 
    318 /* Translate host to target errno value.  */
    319 int cb_host_to_target_errno (host_callback *, int);
    320 
    321 /* Translate target to host open flags.  */
    322 int cb_target_to_host_open (host_callback *, int);
    323 
    324 /* Translate target signal number to host.  */
    325 int cb_target_to_host_signal (host_callback *, int);
    326 
    327 /* Translate host signal number to target.  */
    328 int cb_host_to_gdb_signal (host_callback *, int);
    329 
    330 /* Translate symbols into human readable strings.  */
    331 const char *cb_host_str_syscall (host_callback *, int);
    332 const char *cb_host_str_errno (host_callback *, int);
    333 const char *cb_host_str_signal (host_callback *, int);
    334 const char *cb_target_str_syscall (host_callback *, int);
    335 const char *cb_target_str_errno (host_callback *, int);
    336 const char *cb_target_str_signal (host_callback *, int);
    337 
    338 /* Translate host stat struct to target.
    339    If stat struct ptr is NULL, just compute target stat struct size.
    340    Result is size of target stat struct or 0 if error.  */
    341 int cb_host_to_target_stat (host_callback *, const struct stat *, void *);
    342 
    343 /* Translate a value to target endian.  */
    344 void cb_store_target_endian (host_callback *, char *, int, long);
    345 
    346 /* Tests for special fds.  */
    347 int cb_is_stdin (host_callback *, int);
    348 int cb_is_stdout (host_callback *, int);
    349 int cb_is_stderr (host_callback *, int);
    350 
    351 /* Read a string out of the target.  */
    352 int cb_get_string (host_callback *, CB_SYSCALL *, char *, int, unsigned long);
    353 
    354 /* Perform a system call.  */
    355 CB_RC cb_syscall (host_callback *, CB_SYSCALL *);
    356 
    357 #endif
    358