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