1d2201f2fSdrahn /* Remote target system call callback support. 2d2201f2fSdrahn Copyright 1997 Free Software Foundation, Inc. 3d2201f2fSdrahn Contributed by Cygnus Solutions. 4d2201f2fSdrahn 5d2201f2fSdrahn This file is part of GDB. 6d2201f2fSdrahn 7d2201f2fSdrahn This program is free software; you can redistribute it and/or modify 8d2201f2fSdrahn it under the terms of the GNU General Public License as published by 9d2201f2fSdrahn the Free Software Foundation; either version 2 of the License, or 10d2201f2fSdrahn (at your option) any later version. 11d2201f2fSdrahn 12d2201f2fSdrahn This program is distributed in the hope that it will be useful, 13d2201f2fSdrahn but WITHOUT ANY WARRANTY; without even the implied warranty of 14d2201f2fSdrahn MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15d2201f2fSdrahn GNU General Public License for more details. 16d2201f2fSdrahn 17d2201f2fSdrahn You should have received a copy of the GNU General Public License 18d2201f2fSdrahn along with this program; if not, write to the Free Software 19d2201f2fSdrahn Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 20d2201f2fSdrahn 21d2201f2fSdrahn /* This interface isn't intended to be specific to any particular kind 22d2201f2fSdrahn of remote (hardware, simulator, whatever). As such, support for it 23d2201f2fSdrahn (e.g. sim/common/callback.c) should *not* live in the simulator source 24d2201f2fSdrahn tree, nor should it live in the gdb source tree. */ 25d2201f2fSdrahn 26d2201f2fSdrahn /* There are various ways to handle system calls: 27d2201f2fSdrahn 28d2201f2fSdrahn 1) Have a simulator intercept the appropriate trap instruction and 29d2201f2fSdrahn directly perform the system call on behalf of the target program. 30d2201f2fSdrahn This is the typical way of handling system calls for embedded targets. 31d2201f2fSdrahn [Handling system calls for embedded targets isn't that much of an 32d2201f2fSdrahn oxymoron as running compiler testsuites make use of the capability.] 33d2201f2fSdrahn 34d2201f2fSdrahn This method of system call handling is done when STATE_ENVIRONMENT 35d2201f2fSdrahn is ENVIRONMENT_USER. 36d2201f2fSdrahn 37d2201f2fSdrahn 2) Have a simulator emulate the hardware as much as possible. 38d2201f2fSdrahn If the program running on the real hardware communicates with some sort 39d2201f2fSdrahn of target manager, one would want to be able to run this program on the 40d2201f2fSdrahn simulator as well. 41d2201f2fSdrahn 42d2201f2fSdrahn This method of system call handling is done when STATE_ENVIRONMENT 43d2201f2fSdrahn is ENVIRONMENT_OPERATING. 44d2201f2fSdrahn */ 45d2201f2fSdrahn 46d2201f2fSdrahn #ifndef CALLBACK_H 47d2201f2fSdrahn #define CALLBACK_H 48d2201f2fSdrahn 49d2201f2fSdrahn /* ??? The reason why we check for va_start here should be documented. */ 50d2201f2fSdrahn 51d2201f2fSdrahn #ifndef va_start 52d2201f2fSdrahn #include <ansidecl.h> 53d2201f2fSdrahn #ifdef ANSI_PROTOTYPES 54d2201f2fSdrahn #include <stdarg.h> 55d2201f2fSdrahn #else 56d2201f2fSdrahn #include <varargs.h> 57d2201f2fSdrahn #endif 58d2201f2fSdrahn #endif 59d2201f2fSdrahn 60d2201f2fSdrahn /* Mapping of host/target values. */ 61d2201f2fSdrahn /* ??? For debugging purposes, one might want to add a string of the 62d2201f2fSdrahn name of the symbol. */ 63d2201f2fSdrahn 64d2201f2fSdrahn typedef struct { 65d2201f2fSdrahn int host_val; 66d2201f2fSdrahn int target_val; 67d2201f2fSdrahn } CB_TARGET_DEFS_MAP; 68d2201f2fSdrahn 69d2201f2fSdrahn #define MAX_CALLBACK_FDS 10 70d2201f2fSdrahn 71d2201f2fSdrahn /* Forward decl for stat/fstat. */ 72d2201f2fSdrahn struct stat; 73d2201f2fSdrahn 74d2201f2fSdrahn typedef struct host_callback_struct host_callback; 75d2201f2fSdrahn 76d2201f2fSdrahn struct host_callback_struct 77d2201f2fSdrahn { 78d2201f2fSdrahn int (*close) PARAMS ((host_callback *,int)); 79d2201f2fSdrahn int (*get_errno) PARAMS ((host_callback *)); 80d2201f2fSdrahn int (*isatty) PARAMS ((host_callback *, int)); 81d2201f2fSdrahn int (*lseek) PARAMS ((host_callback *, int, long , int)); 82d2201f2fSdrahn int (*open) PARAMS ((host_callback *, const char*, int mode)); 83d2201f2fSdrahn int (*read) PARAMS ((host_callback *,int, char *, int)); 84d2201f2fSdrahn int (*read_stdin) PARAMS (( host_callback *, char *, int)); 85d2201f2fSdrahn int (*rename) PARAMS ((host_callback *, const char *, const char *)); 86d2201f2fSdrahn int (*system) PARAMS ((host_callback *, const char *)); 87d2201f2fSdrahn long (*time) PARAMS ((host_callback *, long *)); 88d2201f2fSdrahn int (*unlink) PARAMS ((host_callback *, const char *)); 89d2201f2fSdrahn int (*write) PARAMS ((host_callback *,int, const char *, int)); 90d2201f2fSdrahn int (*write_stdout) PARAMS ((host_callback *, const char *, int)); 91d2201f2fSdrahn void (*flush_stdout) PARAMS ((host_callback *)); 92d2201f2fSdrahn int (*write_stderr) PARAMS ((host_callback *, const char *, int)); 93d2201f2fSdrahn void (*flush_stderr) PARAMS ((host_callback *)); 94d2201f2fSdrahn int (*stat) PARAMS ((host_callback *, const char *, struct stat *)); 95d2201f2fSdrahn int (*fstat) PARAMS ((host_callback *, int, struct stat *)); 96b725ae77Skettenis int (*ftruncate) PARAMS ((host_callback *, int, long)); 97b725ae77Skettenis int (*truncate) PARAMS ((host_callback *, const char *, long)); 98d2201f2fSdrahn 99d2201f2fSdrahn /* When present, call to the client to give it the oportunity to 100d2201f2fSdrahn poll any io devices for a request to quit (indicated by a nonzero 101d2201f2fSdrahn return value). */ 102d2201f2fSdrahn int (*poll_quit) PARAMS ((host_callback *)); 103d2201f2fSdrahn 104d2201f2fSdrahn /* Used when the target has gone away, so we can close open 105d2201f2fSdrahn handles and free memory etc etc. */ 106d2201f2fSdrahn int (*shutdown) PARAMS ((host_callback *)); 107d2201f2fSdrahn int (*init) PARAMS ((host_callback *)); 108d2201f2fSdrahn 109d2201f2fSdrahn /* depreciated, use vprintf_filtered - Talk to the user on a console. */ 110d2201f2fSdrahn void (*printf_filtered) PARAMS ((host_callback *, const char *, ...)); 111d2201f2fSdrahn 112d2201f2fSdrahn /* Talk to the user on a console. */ 113d2201f2fSdrahn void (*vprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); 114d2201f2fSdrahn 115d2201f2fSdrahn /* Same as vprintf_filtered but to stderr. */ 116d2201f2fSdrahn void (*evprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); 117d2201f2fSdrahn 118d2201f2fSdrahn /* Print an error message and "exit". 119d2201f2fSdrahn In the case of gdb "exiting" means doing a longjmp back to the main 120d2201f2fSdrahn command loop. */ 121d2201f2fSdrahn void (*error) PARAMS ((host_callback *, const char *, ...)); 122d2201f2fSdrahn 123d2201f2fSdrahn int last_errno; /* host format */ 124d2201f2fSdrahn 125d2201f2fSdrahn int fdmap[MAX_CALLBACK_FDS]; 126*11efff7fSkettenis /* fd_buddy is used to contruct circular lists of target fds that point to 127*11efff7fSkettenis the same host fd. A uniquely mapped fd points to itself; for a closed 128*11efff7fSkettenis one, fd_buddy has the value -1. The host file descriptors for stdin / 129*11efff7fSkettenis stdout / stderr are never closed by the simulators, so they are put 130*11efff7fSkettenis in a special fd_buddy circular list which also has MAX_CALLBACK_FDS 131*11efff7fSkettenis as a member. */ 132*11efff7fSkettenis /* ??? We don't have a callback entry for dup, although it is trival to 133*11efff7fSkettenis implement now. */ 134*11efff7fSkettenis short fd_buddy[MAX_CALLBACK_FDS+1]; 135d2201f2fSdrahn 136d2201f2fSdrahn /* System call numbers. */ 137d2201f2fSdrahn CB_TARGET_DEFS_MAP *syscall_map; 138d2201f2fSdrahn /* Errno values. */ 139d2201f2fSdrahn CB_TARGET_DEFS_MAP *errno_map; 140d2201f2fSdrahn /* Flags to the open system call. */ 141d2201f2fSdrahn CB_TARGET_DEFS_MAP *open_map; 142d2201f2fSdrahn /* Signal numbers. */ 143d2201f2fSdrahn CB_TARGET_DEFS_MAP *signal_map; 144d2201f2fSdrahn /* Layout of `stat' struct. 145d2201f2fSdrahn The format is a series of "name,length" pairs separated by colons. 146d2201f2fSdrahn Empty space is indicated with a `name' of "space". 147d2201f2fSdrahn All padding must be explicitly mentioned. 148d2201f2fSdrahn Lengths are in bytes. If this needs to be extended to bits, 149d2201f2fSdrahn use "name.bits". 150d2201f2fSdrahn Example: "st_dev,4:st_ino,4:st_mode,4:..." */ 151d2201f2fSdrahn const char *stat_map; 152d2201f2fSdrahn 153d2201f2fSdrahn /* Marker for those wanting to do sanity checks. 154d2201f2fSdrahn This should remain the last member of this struct to help catch 155d2201f2fSdrahn miscompilation errors. */ 156d2201f2fSdrahn #define HOST_CALLBACK_MAGIC 4705 /* teds constant */ 157d2201f2fSdrahn int magic; 158d2201f2fSdrahn }; 159d2201f2fSdrahn 160d2201f2fSdrahn extern host_callback default_callback; 161d2201f2fSdrahn 162d2201f2fSdrahn /* Canonical versions of system call numbers. 163d2201f2fSdrahn It's not intended to willy-nilly throw every system call ever heard 164d2201f2fSdrahn of in here. Only include those that have an important use. 165d2201f2fSdrahn ??? One can certainly start a discussion over the ones that are currently 166d2201f2fSdrahn here, but that will always be true. */ 167d2201f2fSdrahn 168d2201f2fSdrahn /* These are used by the ANSI C support of libc. */ 169d2201f2fSdrahn #define CB_SYS_exit 1 170d2201f2fSdrahn #define CB_SYS_open 2 171d2201f2fSdrahn #define CB_SYS_close 3 172d2201f2fSdrahn #define CB_SYS_read 4 173d2201f2fSdrahn #define CB_SYS_write 5 174d2201f2fSdrahn #define CB_SYS_lseek 6 175d2201f2fSdrahn #define CB_SYS_unlink 7 176d2201f2fSdrahn #define CB_SYS_getpid 8 177d2201f2fSdrahn #define CB_SYS_kill 9 178d2201f2fSdrahn #define CB_SYS_fstat 10 179d2201f2fSdrahn /*#define CB_SYS_sbrk 11 - not currently a system call, but reserved. */ 180d2201f2fSdrahn 181d2201f2fSdrahn /* ARGV support. */ 182d2201f2fSdrahn #define CB_SYS_argvlen 12 183d2201f2fSdrahn #define CB_SYS_argv 13 184d2201f2fSdrahn 185d2201f2fSdrahn /* These are extras added for one reason or another. */ 186d2201f2fSdrahn #define CB_SYS_chdir 14 187d2201f2fSdrahn #define CB_SYS_stat 15 188d2201f2fSdrahn #define CB_SYS_chmod 16 189d2201f2fSdrahn #define CB_SYS_utime 17 190d2201f2fSdrahn #define CB_SYS_time 18 191d2201f2fSdrahn 192d2201f2fSdrahn /* Struct use to pass and return information necessary to perform a 193d2201f2fSdrahn system call. */ 194d2201f2fSdrahn /* FIXME: Need to consider target word size. */ 195d2201f2fSdrahn 196d2201f2fSdrahn typedef struct cb_syscall { 197d2201f2fSdrahn /* The target's value of what system call to perform. */ 198d2201f2fSdrahn int func; 199d2201f2fSdrahn /* The arguments to the syscall. */ 200d2201f2fSdrahn long arg1, arg2, arg3, arg4; 201d2201f2fSdrahn 202d2201f2fSdrahn /* The result. */ 203d2201f2fSdrahn long result; 204d2201f2fSdrahn /* Some system calls have two results. */ 205d2201f2fSdrahn long result2; 206d2201f2fSdrahn /* The target's errno value, or 0 if success. 207d2201f2fSdrahn This is converted to the target's value with host_to_target_errno. */ 208d2201f2fSdrahn int errcode; 209d2201f2fSdrahn 210d2201f2fSdrahn /* Working space to be used by memory read/write callbacks. */ 211d2201f2fSdrahn PTR p1; 212d2201f2fSdrahn PTR p2; 213d2201f2fSdrahn long x1,x2; 214d2201f2fSdrahn 215d2201f2fSdrahn /* Callbacks for reading/writing memory (e.g. for read/write syscalls). 216d2201f2fSdrahn ??? long or unsigned long might be better to use for the `count' 217d2201f2fSdrahn argument here. We mimic sim_{read,write} for now. Be careful to 218d2201f2fSdrahn test any changes with -Wall -Werror, mixed signed comparisons 219d2201f2fSdrahn will get you. */ 220d2201f2fSdrahn int (*read_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, 221d2201f2fSdrahn unsigned long /*taddr*/, char * /*buf*/, 222d2201f2fSdrahn int /*bytes*/)); 223d2201f2fSdrahn int (*write_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, 224d2201f2fSdrahn unsigned long /*taddr*/, const char * /*buf*/, 225d2201f2fSdrahn int /*bytes*/)); 226d2201f2fSdrahn 227d2201f2fSdrahn /* For sanity checking, should be last entry. */ 228d2201f2fSdrahn int magic; 229d2201f2fSdrahn } CB_SYSCALL; 230d2201f2fSdrahn 231d2201f2fSdrahn /* Magic number sanity checker. */ 232d2201f2fSdrahn #define CB_SYSCALL_MAGIC 0x12344321 233d2201f2fSdrahn 234d2201f2fSdrahn /* Macro to initialize CB_SYSCALL. Called first, before filling in 235d2201f2fSdrahn any fields. */ 236d2201f2fSdrahn #define CB_SYSCALL_INIT(sc) \ 237d2201f2fSdrahn do { \ 238d2201f2fSdrahn memset ((sc), 0, sizeof (*(sc))); \ 239d2201f2fSdrahn (sc)->magic = CB_SYSCALL_MAGIC; \ 240d2201f2fSdrahn } while (0) 241d2201f2fSdrahn 242d2201f2fSdrahn /* Return codes for various interface routines. */ 243d2201f2fSdrahn 244d2201f2fSdrahn typedef enum { 245d2201f2fSdrahn CB_RC_OK = 0, 246d2201f2fSdrahn /* generic error */ 247d2201f2fSdrahn CB_RC_ERR, 248d2201f2fSdrahn /* either file not found or no read access */ 249d2201f2fSdrahn CB_RC_ACCESS, 250d2201f2fSdrahn CB_RC_NO_MEM 251d2201f2fSdrahn } CB_RC; 252d2201f2fSdrahn 253d2201f2fSdrahn /* Read in target values for system call numbers, errno values, signals. */ 254d2201f2fSdrahn CB_RC cb_read_target_syscall_maps PARAMS ((host_callback *, const char *)); 255d2201f2fSdrahn 256d2201f2fSdrahn /* Translate target to host syscall function numbers. */ 257d2201f2fSdrahn int cb_target_to_host_syscall PARAMS ((host_callback *, int)); 258d2201f2fSdrahn 259d2201f2fSdrahn /* Translate host to target errno value. */ 260d2201f2fSdrahn int cb_host_to_target_errno PARAMS ((host_callback *, int)); 261d2201f2fSdrahn 262d2201f2fSdrahn /* Translate target to host open flags. */ 263d2201f2fSdrahn int cb_target_to_host_open PARAMS ((host_callback *, int)); 264d2201f2fSdrahn 265d2201f2fSdrahn /* Translate target signal number to host. */ 266d2201f2fSdrahn int cb_target_to_host_signal PARAMS ((host_callback *, int)); 267d2201f2fSdrahn 268d2201f2fSdrahn /* Translate host signal number to target. */ 269d2201f2fSdrahn int cb_host_to_target_signal PARAMS ((host_callback *, int)); 270d2201f2fSdrahn 271d2201f2fSdrahn /* Translate host stat struct to target. 272d2201f2fSdrahn If stat struct ptr is NULL, just compute target stat struct size. 273d2201f2fSdrahn Result is size of target stat struct or 0 if error. */ 274d2201f2fSdrahn int cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR)); 275d2201f2fSdrahn 276d2201f2fSdrahn /* Perform a system call. */ 277d2201f2fSdrahn CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *)); 278d2201f2fSdrahn 279d2201f2fSdrahn #endif 280