| 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 |  | 
|---|
| 61 | /* Mapping of host/target values.  */ | 
|---|
| 62 | /* ??? For debugging purposes, one might want to add a string of the | 
|---|
| 63 | name of the symbol.  */ | 
|---|
| 64 |  | 
|---|
| 65 | typedef struct { | 
|---|
| 66 | int host_val; | 
|---|
| 67 | int target_val; | 
|---|
| 68 | } CB_TARGET_DEFS_MAP; | 
|---|
| 69 |  | 
|---|
| 70 | #define MAX_CALLBACK_FDS 10 | 
|---|
| 71 |  | 
|---|
| 72 | /* Forward decl for stat/fstat.  */ | 
|---|
| 73 | struct stat; | 
|---|
| 74 |  | 
|---|
| 75 | typedef struct host_callback_struct host_callback; | 
|---|
| 76 |  | 
|---|
| 77 | struct host_callback_struct | 
|---|
| 78 | { | 
|---|
| 79 | int (*close) PARAMS ((host_callback *,int)); | 
|---|
| 80 | int (*get_errno) PARAMS ((host_callback *)); | 
|---|
| 81 | int (*isatty) PARAMS ((host_callback *, int)); | 
|---|
| 82 | int (*lseek) PARAMS ((host_callback *, int, long , int)); | 
|---|
| 83 | int (*open) PARAMS ((host_callback *, const char*, int mode)); | 
|---|
| 84 | int (*read) PARAMS ((host_callback *,int,  char *, int)); | 
|---|
| 85 | int (*read_stdin) PARAMS (( host_callback *, char *, int)); | 
|---|
| 86 | int (*rename) PARAMS ((host_callback *, const char *, const char *)); | 
|---|
| 87 | int (*system) PARAMS ((host_callback *, const char *)); | 
|---|
| 88 | long (*time) PARAMS ((host_callback *, long *)); | 
|---|
| 89 | int (*unlink) PARAMS ((host_callback *, const char *)); | 
|---|
| 90 | int (*write) PARAMS ((host_callback *,int, const char *, int)); | 
|---|
| 91 | int (*write_stdout) PARAMS ((host_callback *, const char *, int)); | 
|---|
| 92 | void (*flush_stdout) PARAMS ((host_callback *)); | 
|---|
| 93 | int (*write_stderr) PARAMS ((host_callback *, const char *, int)); | 
|---|
| 94 | void (*flush_stderr) PARAMS ((host_callback *)); | 
|---|
| 95 | int (*stat) PARAMS ((host_callback *, const char *, struct stat *)); | 
|---|
| 96 | int (*fstat) PARAMS ((host_callback *, int, struct stat *)); | 
|---|
| 97 |  | 
|---|
| 98 | /* When present, call to the client to give it the oportunity to | 
|---|
| 99 | poll any io devices for a request to quit (indicated by a nonzero | 
|---|
| 100 | return value). */ | 
|---|
| 101 | int (*poll_quit) PARAMS ((host_callback *)); | 
|---|
| 102 |  | 
|---|
| 103 | /* Used when the target has gone away, so we can close open | 
|---|
| 104 | handles and free memory etc etc.  */ | 
|---|
| 105 | int (*shutdown) PARAMS ((host_callback *)); | 
|---|
| 106 | int (*init)     PARAMS ((host_callback *)); | 
|---|
| 107 |  | 
|---|
| 108 | /* depreciated, use vprintf_filtered - Talk to the user on a console.  */ | 
|---|
| 109 | void (*printf_filtered) PARAMS ((host_callback *, const char *, ...)); | 
|---|
| 110 |  | 
|---|
| 111 | /* Talk to the user on a console.  */ | 
|---|
| 112 | void (*vprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); | 
|---|
| 113 |  | 
|---|
| 114 | /* Same as vprintf_filtered but to stderr.  */ | 
|---|
| 115 | void (*evprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); | 
|---|
| 116 |  | 
|---|
| 117 | /* Print an error message and "exit". | 
|---|
| 118 | In the case of gdb "exiting" means doing a longjmp back to the main | 
|---|
| 119 | command loop.  */ | 
|---|
| 120 | void (*error) PARAMS ((host_callback *, const char *, ...)); | 
|---|
| 121 |  | 
|---|
| 122 | int last_errno;               /* host format */ | 
|---|
| 123 |  | 
|---|
| 124 | int fdmap[MAX_CALLBACK_FDS]; | 
|---|
| 125 | char fdopen[MAX_CALLBACK_FDS]; | 
|---|
| 126 | char alwaysopen[MAX_CALLBACK_FDS]; | 
|---|
| 127 |  | 
|---|
| 128 | /* System call numbers.  */ | 
|---|
| 129 | CB_TARGET_DEFS_MAP *syscall_map; | 
|---|
| 130 | /* Errno values.  */ | 
|---|
| 131 | CB_TARGET_DEFS_MAP *errno_map; | 
|---|
| 132 | /* Flags to the open system call.  */ | 
|---|
| 133 | CB_TARGET_DEFS_MAP *open_map; | 
|---|
| 134 | /* Signal numbers.  */ | 
|---|
| 135 | CB_TARGET_DEFS_MAP *signal_map; | 
|---|
| 136 | /* Layout of `stat' struct. | 
|---|
| 137 | The format is a series of "name,length" pairs separated by colons. | 
|---|
| 138 | Empty space is indicated with a `name' of "space". | 
|---|
| 139 | All padding must be explicitly mentioned. | 
|---|
| 140 | Lengths are in bytes.  If this needs to be extended to bits, | 
|---|
| 141 | use "name.bits". | 
|---|
| 142 | Example: "st_dev,4:st_ino,4:st_mode,4:..."  */ | 
|---|
| 143 | const char *stat_map; | 
|---|
| 144 |  | 
|---|
| 145 | /* Marker for those wanting to do sanity checks. | 
|---|
| 146 | This should remain the last member of this struct to help catch | 
|---|
| 147 | miscompilation errors. */ | 
|---|
| 148 | #define HOST_CALLBACK_MAGIC 4705 /* teds constant */ | 
|---|
| 149 | int magic; | 
|---|
| 150 | }; | 
|---|
| 151 |  | 
|---|
| 152 | extern host_callback default_callback; | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | /* Canonical versions of system call numbers. | 
|---|
| 156 | It's not intended to willy-nilly throw every system call ever heard | 
|---|
| 157 | of in here.  Only include those that have an important use. | 
|---|
| 158 | ??? One can certainly start a discussion over the ones that are currently | 
|---|
| 159 | here, but that will always be true.  */ | 
|---|
| 160 |  | 
|---|
| 161 | /* These are used by the ANSI C support of libc.  */ | 
|---|
| 162 | #define CB_SYS_exit     1 | 
|---|
| 163 | #define CB_SYS_open     2 | 
|---|
| 164 | #define CB_SYS_close    3 | 
|---|
| 165 | #define CB_SYS_read     4 | 
|---|
| 166 | #define CB_SYS_write    5 | 
|---|
| 167 | #define CB_SYS_lseek    6 | 
|---|
| 168 | #define CB_SYS_unlink   7 | 
|---|
| 169 | #define CB_SYS_getpid   8 | 
|---|
| 170 | #define CB_SYS_kill     9 | 
|---|
| 171 | #define CB_SYS_fstat    10 | 
|---|
| 172 | /*#define CB_SYS_sbrk   11 - not currently a system call, but reserved.  */ | 
|---|
| 173 |  | 
|---|
| 174 | /* ARGV support.  */ | 
|---|
| 175 | #define CB_SYS_argvlen  12 | 
|---|
| 176 | #define CB_SYS_argv     13 | 
|---|
| 177 |  | 
|---|
| 178 | /* These are extras added for one reason or another.  */ | 
|---|
| 179 | #define CB_SYS_chdir    14 | 
|---|
| 180 | #define CB_SYS_stat     15 | 
|---|
| 181 | #define CB_SYS_chmod    16 | 
|---|
| 182 | #define CB_SYS_utime    17 | 
|---|
| 183 | #define CB_SYS_time     18 | 
|---|
| 184 |  | 
|---|
| 185 |  | 
|---|
| 186 | /* Struct use to pass and return information necessary to perform a | 
|---|
| 187 | system call.  */ | 
|---|
| 188 | /* FIXME: Need to consider target word size.  */ | 
|---|
| 189 |  | 
|---|
| 190 | typedef struct cb_syscall { | 
|---|
| 191 | /* The target's value of what system call to perform.  */ | 
|---|
| 192 | int func; | 
|---|
| 193 | /* The arguments to the syscall.  */ | 
|---|
| 194 | long arg1, arg2, arg3, arg4; | 
|---|
| 195 |  | 
|---|
| 196 | /* The result.  */ | 
|---|
| 197 | long result; | 
|---|
| 198 | /* Some system calls have two results.  */ | 
|---|
| 199 | long result2; | 
|---|
| 200 | /* The target's errno value, or 0 if success. | 
|---|
| 201 | This is converted to the target's value with host_to_target_errno.  */ | 
|---|
| 202 | int errcode; | 
|---|
| 203 |  | 
|---|
| 204 | /* Working space to be used by memory read/write callbacks.  */ | 
|---|
| 205 | PTR p1; | 
|---|
| 206 | PTR p2; | 
|---|
| 207 | long x1,x2; | 
|---|
| 208 |  | 
|---|
| 209 | /* Callbacks for reading/writing memory (e.g. for read/write syscalls). | 
|---|
| 210 | ??? long or unsigned long might be better to use for the `count' | 
|---|
| 211 | argument here.  We mimic sim_{read,write} for now.  Be careful to | 
|---|
| 212 | test any changes with -Wall -Werror, mixed signed comparisons | 
|---|
| 213 | will get you.  */ | 
|---|
| 214 | int (*read_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, | 
|---|
| 215 | unsigned long /*taddr*/, char * /*buf*/, | 
|---|
| 216 | int /*bytes*/)); | 
|---|
| 217 | int (*write_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, | 
|---|
| 218 | unsigned long /*taddr*/, const char * /*buf*/, | 
|---|
| 219 | int /*bytes*/)); | 
|---|
| 220 |  | 
|---|
| 221 | /* For sanity checking, should be last entry.  */ | 
|---|
| 222 | int magic; | 
|---|
| 223 | } CB_SYSCALL; | 
|---|
| 224 |  | 
|---|
| 225 | /* Magic number sanity checker.  */ | 
|---|
| 226 | #define CB_SYSCALL_MAGIC 0x12344321 | 
|---|
| 227 |  | 
|---|
| 228 | /* Macro to initialize CB_SYSCALL.  Called first, before filling in | 
|---|
| 229 | any fields.  */ | 
|---|
| 230 | #define CB_SYSCALL_INIT(sc) \ | 
|---|
| 231 | do { \ | 
|---|
| 232 | memset ((sc), 0, sizeof (*(sc))); \ | 
|---|
| 233 | (sc)->magic = CB_SYSCALL_MAGIC; \ | 
|---|
| 234 | } while (0) | 
|---|
| 235 |  | 
|---|
| 236 |  | 
|---|
| 237 | /* Return codes for various interface routines.  */ | 
|---|
| 238 |  | 
|---|
| 239 | typedef enum { | 
|---|
| 240 | CB_RC_OK = 0, | 
|---|
| 241 | /* generic error */ | 
|---|
| 242 | CB_RC_ERR, | 
|---|
| 243 | /* either file not found or no read access */ | 
|---|
| 244 | CB_RC_ACCESS, | 
|---|
| 245 | CB_RC_NO_MEM | 
|---|
| 246 | } CB_RC; | 
|---|
| 247 |  | 
|---|
| 248 | /* Read in target values for system call numbers, errno values, signals.  */ | 
|---|
| 249 | CB_RC cb_read_target_syscall_maps PARAMS ((host_callback *, const char *)); | 
|---|
| 250 |  | 
|---|
| 251 | /* Translate target to host syscall function numbers.  */ | 
|---|
| 252 | int cb_target_to_host_syscall PARAMS ((host_callback *, int)); | 
|---|
| 253 |  | 
|---|
| 254 | /* Translate host to target errno value.  */ | 
|---|
| 255 | int cb_host_to_target_errno PARAMS ((host_callback *, int)); | 
|---|
| 256 |  | 
|---|
| 257 | /* Translate target to host open flags.  */ | 
|---|
| 258 | int cb_target_to_host_open PARAMS ((host_callback *, int)); | 
|---|
| 259 |  | 
|---|
| 260 | /* Translate target signal number to host.  */ | 
|---|
| 261 | int cb_target_to_host_signal PARAMS ((host_callback *, int)); | 
|---|
| 262 |  | 
|---|
| 263 | /* Translate host signal number to target.  */ | 
|---|
| 264 | int cb_host_to_target_signal PARAMS ((host_callback *, int)); | 
|---|
| 265 |  | 
|---|
| 266 | /* Translate host stat struct to target. | 
|---|
| 267 | If stat struct ptr is NULL, just compute target stat struct size. | 
|---|
| 268 | Result is size of target stat struct or 0 if error.  */ | 
|---|
| 269 | int cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR)); | 
|---|
| 270 |  | 
|---|
| 271 | /* Perform a system call.  */ | 
|---|
| 272 | CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *)); | 
|---|
| 273 |  | 
|---|
| 274 | #endif | 
|---|