14b169a6bSchristos /* Remote target system call callback support. 2*02f41505Schristos Copyright (C) 1997-2024 Free Software Foundation, Inc. 34b169a6bSchristos Contributed by Cygnus Solutions. 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 /* This interface isn't intended to be specific to any particular kind 214b169a6bSchristos of remote (hardware, simulator, whatever). As such, support for it 224b169a6bSchristos (e.g. sim/common/callback.c) should *not* live in the simulator source 234b169a6bSchristos tree, nor should it live in the gdb source tree. */ 244b169a6bSchristos 254b169a6bSchristos /* There are various ways to handle system calls: 264b169a6bSchristos 274b169a6bSchristos 1) Have a simulator intercept the appropriate trap instruction and 284b169a6bSchristos directly perform the system call on behalf of the target program. 294b169a6bSchristos This is the typical way of handling system calls for embedded targets. 304b169a6bSchristos [Handling system calls for embedded targets isn't that much of an 314b169a6bSchristos oxymoron as running compiler testsuites make use of the capability.] 324b169a6bSchristos 334b169a6bSchristos This method of system call handling is done when STATE_ENVIRONMENT 344b169a6bSchristos is ENVIRONMENT_USER. 354b169a6bSchristos 364b169a6bSchristos 2) Have a simulator emulate the hardware as much as possible. 374b169a6bSchristos If the program running on the real hardware communicates with some sort 384b169a6bSchristos of target manager, one would want to be able to run this program on the 394b169a6bSchristos simulator as well. 404b169a6bSchristos 414b169a6bSchristos This method of system call handling is done when STATE_ENVIRONMENT 424b169a6bSchristos is ENVIRONMENT_OPERATING. 434b169a6bSchristos */ 444b169a6bSchristos 454b169a6bSchristos #ifndef SIM_CALLBACK_H 464b169a6bSchristos #define SIM_CALLBACK_H 474b169a6bSchristos 484b169a6bSchristos #include <stdarg.h> 494b169a6bSchristos #include <stdint.h> 504b169a6bSchristos 514b169a6bSchristos #include <ansidecl.h> 524b169a6bSchristos /* Needed for enum bfd_endian. */ 534b169a6bSchristos #include <bfd.h> 544b169a6bSchristos 554b169a6bSchristos /* Mapping of host/target values. */ 564b169a6bSchristos /* ??? For debugging purposes, one might want to add a string of the 574b169a6bSchristos name of the symbol. */ 584b169a6bSchristos 594b169a6bSchristos typedef struct { 604b169a6bSchristos const char *name; 614b169a6bSchristos int host_val; 624b169a6bSchristos int target_val; 634b169a6bSchristos } CB_TARGET_DEFS_MAP; 644b169a6bSchristos 654b169a6bSchristos #define MAX_CALLBACK_FDS 10 664b169a6bSchristos 674b169a6bSchristos /* Forward decl for stat/fstat. */ 684b169a6bSchristos struct stat; 694b169a6bSchristos 704b169a6bSchristos typedef struct host_callback_struct host_callback; 714b169a6bSchristos 724b169a6bSchristos struct host_callback_struct 734b169a6bSchristos { 744b169a6bSchristos int (*close) (host_callback *,int); 754b169a6bSchristos int (*get_errno) (host_callback *); 764b169a6bSchristos int (*isatty) (host_callback *, int); 774b169a6bSchristos int64_t (*lseek) (host_callback *, int, int64_t, int); 784b169a6bSchristos int (*open) (host_callback *, const char*, int mode); 794b169a6bSchristos int (*read) (host_callback *,int, char *, int); 804b169a6bSchristos int (*read_stdin) ( host_callback *, char *, int); 814b169a6bSchristos int (*rename) (host_callback *, const char *, const char *); 824b169a6bSchristos int (*system) (host_callback *, const char *); 834b169a6bSchristos int64_t (*time) (host_callback *); 844b169a6bSchristos int (*unlink) (host_callback *, const char *); 854b169a6bSchristos int (*write) (host_callback *,int, const char *, int); 864b169a6bSchristos int (*write_stdout) (host_callback *, const char *, int); 874b169a6bSchristos void (*flush_stdout) (host_callback *); 884b169a6bSchristos int (*write_stderr) (host_callback *, const char *, int); 894b169a6bSchristos void (*flush_stderr) (host_callback *); 904b169a6bSchristos int (*to_stat) (host_callback *, const char *, struct stat *); 914b169a6bSchristos int (*to_fstat) (host_callback *, int, struct stat *); 924b169a6bSchristos int (*to_lstat) (host_callback *, const char *, struct stat *); 934b169a6bSchristos int (*ftruncate) (host_callback *, int, int64_t); 944b169a6bSchristos int (*truncate) (host_callback *, const char *, int64_t); 954b169a6bSchristos int (*getpid) (host_callback *); 964b169a6bSchristos int (*kill) (host_callback *, int, int); 974b169a6bSchristos int (*pipe) (host_callback *, int *); 984b169a6bSchristos 994b169a6bSchristos /* Called by the framework when a read call has emptied a pipe buffer. */ 1004b169a6bSchristos void (*pipe_empty) (host_callback *, int read_fd, int write_fd); 1014b169a6bSchristos 1024b169a6bSchristos /* Called by the framework when a write call makes a pipe buffer 1034b169a6bSchristos non-empty. */ 1044b169a6bSchristos void (*pipe_nonempty) (host_callback *, int read_fd, int write_fd); 1054b169a6bSchristos 1064b169a6bSchristos /* When present, call to the client to give it the oportunity to 1074b169a6bSchristos poll any io devices for a request to quit (indicated by a nonzero 1084b169a6bSchristos return value). */ 1094b169a6bSchristos int (*poll_quit) (host_callback *); 1104b169a6bSchristos 1114b169a6bSchristos /* Used when the target has gone away, so we can close open 1124b169a6bSchristos handles and free memory etc etc. */ 1134b169a6bSchristos int (*shutdown) (host_callback *); 1144b169a6bSchristos int (*init) (host_callback *); 1154b169a6bSchristos 1164b169a6bSchristos /* depreciated, use vprintf_filtered - Talk to the user on a console. */ 1174b169a6bSchristos void (*printf_filtered) (host_callback *, const char *, ...) 1184b169a6bSchristos ATTRIBUTE_PRINTF_2; 1194b169a6bSchristos 1204b169a6bSchristos /* Talk to the user on a console. */ 1214b169a6bSchristos void (*vprintf_filtered) (host_callback *, const char *, va_list) 1224b169a6bSchristos ATTRIBUTE_PRINTF (2, 0); 1234b169a6bSchristos 1244b169a6bSchristos /* Same as vprintf_filtered but to stderr. */ 1254b169a6bSchristos void (*evprintf_filtered) (host_callback *, const char *, va_list) 1264b169a6bSchristos ATTRIBUTE_PRINTF (2, 0); 1274b169a6bSchristos 1284b169a6bSchristos /* Print an error message and "exit". 1294b169a6bSchristos In the case of gdb "exiting" means doing a longjmp back to the main 1304b169a6bSchristos command loop. */ 1314b169a6bSchristos void (*error) (host_callback *, const char *, ...) 1324b169a6bSchristos ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_2; 1334b169a6bSchristos 1344b169a6bSchristos int last_errno; /* host format */ 1354b169a6bSchristos 1364b169a6bSchristos int fdmap[MAX_CALLBACK_FDS]; 1374b169a6bSchristos /* fd_buddy is used to contruct circular lists of target fds that point to 1384b169a6bSchristos the same host fd. A uniquely mapped fd points to itself; for a closed 1394b169a6bSchristos one, fd_buddy has the value -1. The host file descriptors for stdin / 1404b169a6bSchristos stdout / stderr are never closed by the simulators, so they are put 1414b169a6bSchristos in a special fd_buddy circular list which also has MAX_CALLBACK_FDS 1424b169a6bSchristos as a member. */ 1434b169a6bSchristos /* ??? We don't have a callback entry for dup, although it is trival to 1444b169a6bSchristos implement now. */ 1454b169a6bSchristos short fd_buddy[MAX_CALLBACK_FDS+1]; 1464b169a6bSchristos 1474b169a6bSchristos /* 0 = none, >0 = reader (index of writer), 1484b169a6bSchristos <0 = writer (negative index of reader). 1494b169a6bSchristos If abs (ispipe[N]) == N, then N is an end of a pipe whose other 1504b169a6bSchristos end is closed. */ 1514b169a6bSchristos short ispipe[MAX_CALLBACK_FDS]; 1524b169a6bSchristos 1534b169a6bSchristos /* A writer stores the buffer at its index. Consecutive writes 1544b169a6bSchristos realloc the buffer and add to the size. The reader indicates the 1554b169a6bSchristos read part in its .size, until it has consumed it all, at which 1564b169a6bSchristos point it deallocates the buffer and zeroes out both sizes. */ 1574b169a6bSchristos struct pipe_write_buffer 1584b169a6bSchristos { 1594b169a6bSchristos int size; 1604b169a6bSchristos char *buffer; 1614b169a6bSchristos } pipe_buffer[MAX_CALLBACK_FDS]; 1624b169a6bSchristos 1634b169a6bSchristos /* System call numbers. */ 1644b169a6bSchristos CB_TARGET_DEFS_MAP *syscall_map; 1654b169a6bSchristos /* Errno values. */ 1664b169a6bSchristos CB_TARGET_DEFS_MAP *errno_map; 1674b169a6bSchristos /* Flags to the open system call. */ 1684b169a6bSchristos CB_TARGET_DEFS_MAP *open_map; 1694b169a6bSchristos /* Signal numbers. */ 1704b169a6bSchristos CB_TARGET_DEFS_MAP *signal_map; 1714b169a6bSchristos /* Layout of `stat' struct. 1724b169a6bSchristos The format is a series of "name,length" pairs separated by colons. 1734b169a6bSchristos Empty space is indicated with a `name' of "space". 1744b169a6bSchristos All padding must be explicitly mentioned. 1754b169a6bSchristos Lengths are in bytes. If this needs to be extended to bits, 1764b169a6bSchristos use "name.bits". 1774b169a6bSchristos Example: "st_dev,4:st_ino,4:st_mode,4:..." */ 1784b169a6bSchristos const char *stat_map; 1794b169a6bSchristos 1804b169a6bSchristos enum bfd_endian target_endian; 1814b169a6bSchristos 1824b169a6bSchristos /* Program command line options. */ 1834b169a6bSchristos char **argv; 1844b169a6bSchristos 1854b169a6bSchristos /* Program environment. */ 1864b169a6bSchristos char **envp; 1874b169a6bSchristos 1884b169a6bSchristos /* Size of an "int" on the target (for syscalls whose ABI uses "int"). 1894b169a6bSchristos This must include padding, and only padding-at-higher-address is 1904b169a6bSchristos supported. For example, a 64-bit target with 32-bit int:s which 1914b169a6bSchristos are padded to 64 bits when in an array, should supposedly set this 1924b169a6bSchristos to 8. The default is 4 which matches ILP32 targets and 64-bit 1934b169a6bSchristos targets with 32-bit ints and no padding. */ 1944b169a6bSchristos int target_sizeof_int; 1954b169a6bSchristos 1964b169a6bSchristos /* Marker for those wanting to do sanity checks. 1974b169a6bSchristos This should remain the last member of this struct to help catch 1984b169a6bSchristos miscompilation errors. */ 1994b169a6bSchristos #define HOST_CALLBACK_MAGIC 4705 /* teds constant */ 2004b169a6bSchristos int magic; 2014b169a6bSchristos }; 2024b169a6bSchristos 2034b169a6bSchristos extern host_callback default_callback; 2044b169a6bSchristos 2054b169a6bSchristos /* Canonical versions of system call numbers. 2064b169a6bSchristos It's not intended to willy-nilly throw every system call ever heard 2074b169a6bSchristos of in here. Only include those that have an important use. 2084b169a6bSchristos ??? One can certainly start a discussion over the ones that are currently 2094b169a6bSchristos here, but that will always be true. */ 2104b169a6bSchristos 2114b169a6bSchristos /* These are used by the ANSI C support of libc. */ 2124b169a6bSchristos #define CB_SYS_exit 1 2134b169a6bSchristos #define CB_SYS_open 2 2144b169a6bSchristos #define CB_SYS_close 3 2154b169a6bSchristos #define CB_SYS_read 4 2164b169a6bSchristos #define CB_SYS_write 5 2174b169a6bSchristos #define CB_SYS_lseek 6 2184b169a6bSchristos #define CB_SYS_unlink 7 2194b169a6bSchristos #define CB_SYS_getpid 8 2204b169a6bSchristos #define CB_SYS_kill 9 2214b169a6bSchristos #define CB_SYS_fstat 10 2224b169a6bSchristos /*#define CB_SYS_sbrk 11 - not currently a system call, but reserved. */ 2234b169a6bSchristos 2244b169a6bSchristos /* ARGV support. */ 2254b169a6bSchristos #define CB_SYS_argvlen 12 2264b169a6bSchristos #define CB_SYS_argv 13 2274b169a6bSchristos 2284b169a6bSchristos /* These are extras added for one reason or another. */ 2294b169a6bSchristos #define CB_SYS_chdir 14 2304b169a6bSchristos #define CB_SYS_stat 15 2314b169a6bSchristos #define CB_SYS_chmod 16 2324b169a6bSchristos #define CB_SYS_utime 17 2334b169a6bSchristos #define CB_SYS_time 18 2344b169a6bSchristos 2354b169a6bSchristos /* More standard syscalls. */ 2364b169a6bSchristos #define CB_SYS_lstat 19 2374b169a6bSchristos #define CB_SYS_rename 20 2384b169a6bSchristos #define CB_SYS_truncate 21 2394b169a6bSchristos #define CB_SYS_ftruncate 22 2404b169a6bSchristos #define CB_SYS_pipe 23 2414b169a6bSchristos 2424b169a6bSchristos /* New ARGV support. */ 2434b169a6bSchristos #define CB_SYS_argc 24 2444b169a6bSchristos #define CB_SYS_argnlen 25 2454b169a6bSchristos #define CB_SYS_argn 26 2464b169a6bSchristos 2474b169a6bSchristos /* Struct use to pass and return information necessary to perform a 2484b169a6bSchristos system call. */ 2494b169a6bSchristos /* FIXME: Need to consider target word size. */ 2504b169a6bSchristos 2514b169a6bSchristos typedef struct cb_syscall { 2524b169a6bSchristos /* The target's value of what system call to perform. */ 2534b169a6bSchristos int func; 2544b169a6bSchristos /* The arguments to the syscall. */ 2554b169a6bSchristos long arg1, arg2, arg3, arg4, arg5, arg6, arg7; 2564b169a6bSchristos 2574b169a6bSchristos /* The result. */ 2584b169a6bSchristos long result; 2594b169a6bSchristos /* Some system calls have two results. */ 2604b169a6bSchristos long result2; 2614b169a6bSchristos /* The target's errno value, or 0 if success. 2624b169a6bSchristos This is converted to the target's value with host_to_target_errno. */ 2634b169a6bSchristos int errcode; 2644b169a6bSchristos 2654b169a6bSchristos /* Working space to be used by memory read/write callbacks. */ 2664b169a6bSchristos void *p1; 2674b169a6bSchristos void *p2; 2684b169a6bSchristos long x1,x2; 2694b169a6bSchristos 2704b169a6bSchristos /* Callbacks for reading/writing memory (e.g. for read/write syscalls). 2714b169a6bSchristos ??? long or unsigned long might be better to use for the `count' 2724b169a6bSchristos argument here. We mimic sim_{read,write} for now. Be careful to 2734b169a6bSchristos test any changes with -Wall -Werror, mixed signed comparisons 2744b169a6bSchristos will get you. */ 2754b169a6bSchristos int (*read_mem) (host_callback * /*cb*/, struct cb_syscall * /*sc*/, 2764b169a6bSchristos unsigned long /*taddr*/, char * /*buf*/, 2774b169a6bSchristos int /*bytes*/); 2784b169a6bSchristos int (*write_mem) (host_callback * /*cb*/, struct cb_syscall * /*sc*/, 2794b169a6bSchristos unsigned long /*taddr*/, const char * /*buf*/, 2804b169a6bSchristos int /*bytes*/); 2814b169a6bSchristos 2824b169a6bSchristos /* For sanity checking, should be last entry. */ 2834b169a6bSchristos int magic; 2844b169a6bSchristos } CB_SYSCALL; 2854b169a6bSchristos 2864b169a6bSchristos /* Magic number sanity checker. */ 2874b169a6bSchristos #define CB_SYSCALL_MAGIC 0x12344321 2884b169a6bSchristos 2894b169a6bSchristos /* Macro to initialize CB_SYSCALL. Called first, before filling in 2904b169a6bSchristos any fields. */ 2914b169a6bSchristos #define CB_SYSCALL_INIT(sc) \ 2924b169a6bSchristos do { \ 2934b169a6bSchristos memset ((sc), 0, sizeof (*(sc))); \ 2944b169a6bSchristos (sc)->magic = CB_SYSCALL_MAGIC; \ 2954b169a6bSchristos } while (0) 2964b169a6bSchristos 2974b169a6bSchristos /* Return codes for various interface routines. */ 2984b169a6bSchristos 2994b169a6bSchristos typedef enum { 3004b169a6bSchristos CB_RC_OK = 0, 3014b169a6bSchristos /* generic error */ 3024b169a6bSchristos CB_RC_ERR, 3034b169a6bSchristos /* either file not found or no read access */ 3044b169a6bSchristos CB_RC_ACCESS, 3054b169a6bSchristos CB_RC_NO_MEM 3064b169a6bSchristos } CB_RC; 3074b169a6bSchristos 3084b169a6bSchristos /* Read in target values for system call numbers, errno values, signals. */ 3094b169a6bSchristos CB_RC cb_read_target_syscall_maps (host_callback *, const char *); 3104b169a6bSchristos 3114b169a6bSchristos /* Translate target to host syscall function numbers. */ 3124b169a6bSchristos int cb_target_to_host_syscall (host_callback *, int); 3134b169a6bSchristos 3144b169a6bSchristos /* Translate host to target errno value. */ 3154b169a6bSchristos int cb_host_to_target_errno (host_callback *, int); 3164b169a6bSchristos 3174b169a6bSchristos /* Translate target to host open flags. */ 3184b169a6bSchristos int cb_target_to_host_open (host_callback *, int); 3194b169a6bSchristos 3204b169a6bSchristos /* Translate target signal number to host. */ 3214b169a6bSchristos int cb_target_to_host_signal (host_callback *, int); 3224b169a6bSchristos 3234b169a6bSchristos /* Translate host signal number to target. */ 3244b169a6bSchristos int cb_host_to_gdb_signal (host_callback *, int); 3254b169a6bSchristos 3264b169a6bSchristos /* Translate symbols into human readable strings. */ 3274b169a6bSchristos const char *cb_host_str_syscall (host_callback *, int); 3284b169a6bSchristos const char *cb_host_str_errno (host_callback *, int); 3294b169a6bSchristos const char *cb_host_str_signal (host_callback *, int); 3304b169a6bSchristos const char *cb_target_str_syscall (host_callback *, int); 3314b169a6bSchristos const char *cb_target_str_errno (host_callback *, int); 3324b169a6bSchristos const char *cb_target_str_signal (host_callback *, int); 3334b169a6bSchristos 3344b169a6bSchristos /* Translate host stat struct to target. 3354b169a6bSchristos If stat struct ptr is NULL, just compute target stat struct size. 3364b169a6bSchristos Result is size of target stat struct or 0 if error. */ 3374b169a6bSchristos int cb_host_to_target_stat (host_callback *, const struct stat *, void *); 3384b169a6bSchristos 3394b169a6bSchristos /* Translate a value to target endian. */ 3404b169a6bSchristos void cb_store_target_endian (host_callback *, char *, int, long); 3414b169a6bSchristos 3424b169a6bSchristos /* Tests for special fds. */ 3434b169a6bSchristos int cb_is_stdin (host_callback *, int); 3444b169a6bSchristos int cb_is_stdout (host_callback *, int); 3454b169a6bSchristos int cb_is_stderr (host_callback *, int); 3464b169a6bSchristos 3474b169a6bSchristos /* Read a string out of the target. */ 3484b169a6bSchristos int cb_get_string (host_callback *, CB_SYSCALL *, char *, int, unsigned long); 3494b169a6bSchristos 3504b169a6bSchristos /* Perform a system call. */ 3514b169a6bSchristos CB_RC cb_syscall (host_callback *, CB_SYSCALL *); 3524b169a6bSchristos 3534b169a6bSchristos #endif 354