00001 /* 00002 * Copyright 2003, 2004, 2005, 2006 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* Remote target system call callback support. 00006 Copyright 1997 Free Software Foundation, Inc. 00007 Contributed by Cygnus Solutions. 00008 00009 This file is part of GDB. 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with this program; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 00024 00025 /* This interface isn't intended to be specific to any particular kind 00026 of remote (hardware, simulator, whatever). As such, support for it 00027 (e.g. sim/common/callback.c) should *not* live in the simulator source 00028 tree, nor should it live in the gdb source tree. */ 00029 00030 /* There are various ways to handle system calls: 00031 00032 1) Have a simulator intercept the appropriate trap instruction and 00033 directly perform the system call on behalf of the target program. 00034 This is the typical way of handling system calls for embedded targets. 00035 [Handling system calls for embedded targets isn't that much of an 00036 oxymoron as running compiler testsuites make use of the capability.] 00037 00038 This method of system call handling is done when STATE_ENVIRONMENT 00039 is ENVIRONMENT_USER. 00040 00041 2) Have a simulator emulate the hardware as much as possible. 00042 If the program running on the real hardware communicates with some sort 00043 of target manager, one would want to be able to run this program on the 00044 simulator as well. 00045 00046 This method of system call handling is done when STATE_ENVIRONMENT 00047 is ENVIRONMENT_OPERATING. 00048 */ 00049 00050 #ifndef CALLBACK_H 00051 #define CALLBACK_H 00052 00053 /* ??? The reason why we check for va_start here should be documented. */ 00054 00055 #ifndef va_start 00056 #include <ansidecl.h> 00057 #ifdef ANSI_PROTOTYPES 00058 #include <stdarg.h> 00059 #else 00060 #include <varargs.h> 00061 #endif 00062 #endif 00063 /* Needed for enum bfd_endian. */ 00064 #include "bfd.h" 00065 00066 /* Mapping of host/target values. */ 00067 /* ??? For debugging purposes, one might want to add a string of the 00068 name of the symbol. */ 00069 00070 typedef struct { 00071 int host_val; 00072 int target_val; 00073 } CB_TARGET_DEFS_MAP; 00074 00075 #define MAX_CALLBACK_FDS 10 00076 00077 /* Forward decl for stat/fstat. */ 00078 struct stat; 00079 00080 typedef struct host_callback_struct host_callback; 00081 00082 struct host_callback_struct 00083 { 00084 int (*close) PARAMS ((host_callback *,int)); 00085 int (*get_errno) PARAMS ((host_callback *)); 00086 int (*isatty) PARAMS ((host_callback *, int)); 00087 int (*lseek) PARAMS ((host_callback *, int, long , int)); 00088 int (*open) PARAMS ((host_callback *, const char*, int mode)); 00089 int (*read) PARAMS ((host_callback *,int, char *, int)); 00090 int (*read_stdin) PARAMS (( host_callback *, char *, int)); 00091 int (*rename) PARAMS ((host_callback *, const char *, const char *)); 00092 int (*system) PARAMS ((host_callback *, const char *)); 00093 long (*time) PARAMS ((host_callback *, long *)); 00094 int (*unlink) PARAMS ((host_callback *, const char *)); 00095 int (*write) PARAMS ((host_callback *,int, const char *, int)); 00096 int (*write_stdout) PARAMS ((host_callback *, const char *, int)); 00097 void (*flush_stdout) PARAMS ((host_callback *)); 00098 int (*write_stderr) PARAMS ((host_callback *, const char *, int)); 00099 void (*flush_stderr) PARAMS ((host_callback *)); 00100 int (*stat) PARAMS ((host_callback *, const char *, struct stat *)); 00101 int (*fstat) PARAMS ((host_callback *, int, struct stat *)); 00102 int (*lstat) PARAMS ((host_callback *, const char *, struct stat *)); 00103 int (*ftruncate) PARAMS ((host_callback *, int, long)); 00104 int (*truncate) PARAMS ((host_callback *, const char *, long)); 00105 int (*pipe) PARAMS ((host_callback *, int *)); 00106 00107 /* Called by the framework when a read call has emptied a pipe buffer. */ 00108 void (*pipe_empty) PARAMS ((host_callback *, int read_fd, int write_fd)); 00109 00110 /* Called by the framework when a write call makes a pipe buffer 00111 non-empty. */ 00112 void (*pipe_nonempty) PARAMS ((host_callback *, int read_fd, int write_fd)); 00113 00114 /* When present, call to the client to give it the oportunity to 00115 poll any io devices for a request to quit (indicated by a nonzero 00116 return value). */ 00117 int (*poll_quit) PARAMS ((host_callback *)); 00118 00119 /* Used when the target has gone away, so we can close open 00120 handles and free memory etc etc. */ 00121 int (*shutdown) PARAMS ((host_callback *)); 00122 int (*init) PARAMS ((host_callback *)); 00123 00124 /* depreciated, use vprintf_filtered - Talk to the user on a console. */ 00125 void (*printf_filtered) PARAMS ((host_callback *, const char *, ...)); 00126 00127 /* Talk to the user on a console. */ 00128 void (*vprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); 00129 00130 /* Same as vprintf_filtered but to stderr. */ 00131 void (*evprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); 00132 00133 /* Print an error message and "exit". 00134 In the case of gdb "exiting" means doing a longjmp back to the main 00135 command loop. */ 00136 void (*error) PARAMS ((host_callback *, const char *, ...)); 00137 00138 int last_errno; /* host format */ 00139 00140 int fdmap[MAX_CALLBACK_FDS]; 00141 /* fd_buddy is used to contruct circular lists of target fds that point to 00142 the same host fd. A uniquely mapped fd points to itself; for a closed 00143 one, fd_buddy has the value -1. The host file descriptors for stdin / 00144 stdout / stderr are never closed by the simulators, so they are put 00145 in a special fd_buddy circular list which also has MAX_CALLBACK_FDS 00146 as a member. */ 00147 /* ??? We don't have a callback entry for dup, although it is trival to 00148 implement now. */ 00149 short fd_buddy[MAX_CALLBACK_FDS+1]; 00150 00151 /* 0 = none, >0 = reader (index of writer), 00152 <0 = writer (negative index of reader). 00153 If abs (ispipe[N]) == N, then N is an end of a pipe whose other 00154 end is closed. */ 00155 short ispipe[MAX_CALLBACK_FDS]; 00156 00157 /* A writer stores the buffer at its index. Consecutive writes 00158 realloc the buffer and add to the size. The reader indicates the 00159 read part in its .size, until it has consumed it all, at which 00160 point it deallocates the buffer and zeroes out both sizes. */ 00161 struct pipe_write_buffer 00162 { 00163 int size; 00164 char *buffer; 00165 } pipe_buffer[MAX_CALLBACK_FDS]; 00166 00167 /* System call numbers. */ 00168 CB_TARGET_DEFS_MAP *syscall_map; 00169 /* Errno values. */ 00170 CB_TARGET_DEFS_MAP *errno_map; 00171 /* Flags to the open system call. */ 00172 CB_TARGET_DEFS_MAP *open_map; 00173 /* Signal numbers. */ 00174 CB_TARGET_DEFS_MAP *signal_map; 00175 /* Layout of `stat' struct. 00176 The format is a series of "name,length" pairs separated by colons. 00177 Empty space is indicated with a `name' of "space". 00178 All padding must be explicitly mentioned. 00179 Lengths are in bytes. If this needs to be extended to bits, 00180 use "name.bits". 00181 Example: "st_dev,4:st_ino,4:st_mode,4:..." */ 00182 const char *stat_map; 00183 00184 enum bfd_endian target_endian; 00185 00186 /* Size of an "int" on the target (for syscalls whose ABI uses "int"). 00187 This must include padding, and only padding-at-higher-address is 00188 supported. For example, a 64-bit target with 32-bit int:s which 00189 are padded to 64 bits when in an array, should supposedly set this 00190 to 8. The default is 4 which matches ILP32 targets and 64-bit 00191 targets with 32-bit ints and no padding. */ 00192 int target_sizeof_int; 00193 00194 /* Marker for those wanting to do sanity checks. 00195 This should remain the last member of this struct to help catch 00196 miscompilation errors. */ 00197 #define HOST_CALLBACK_MAGIC 4705 /* teds constant */ 00198 int magic; 00199 }; 00200 00201 extern host_callback default_callback; 00202 00203 /* Canonical versions of system call numbers. 00204 It's not intended to willy-nilly throw every system call ever heard 00205 of in here. Only include those that have an important use. 00206 ??? One can certainly start a discussion over the ones that are currently 00207 here, but that will always be true. */ 00208 00209 /* These are used by the ANSI C support of libc. */ 00210 #define CB_SYS_exit 1 00211 #define CB_SYS_open 2 00212 #define CB_SYS_close 3 00213 #define CB_SYS_read 4 00214 #define CB_SYS_write 5 00215 #define CB_SYS_lseek 6 00216 #define CB_SYS_unlink 7 00217 #define CB_SYS_getpid 8 00218 #define CB_SYS_kill 9 00219 #define CB_SYS_fstat 10 00220 /*#define CB_SYS_sbrk 11 - not currently a system call, but reserved. */ 00221 00222 /* ARGV support. */ 00223 #define CB_SYS_argvlen 12 00224 #define CB_SYS_argv 13 00225 00226 /* These are extras added for one reason or another. */ 00227 #define CB_SYS_chdir 14 00228 #define CB_SYS_stat 15 00229 #define CB_SYS_chmod 16 00230 #define CB_SYS_utime 17 00231 #define CB_SYS_time 18 00232 00233 /* More standard syscalls. */ 00234 #define CB_SYS_lstat 19 00235 #define CB_SYS_rename 20 00236 #define CB_SYS_truncate 21 00237 #define CB_SYS_ftruncate 22 00238 #define CB_SYS_pipe 23 00239 00240 /* Struct use to pass and return information necessary to perform a 00241 system call. */ 00242 /* FIXME: Need to consider target word size. */ 00243 00244 typedef struct cb_syscall { 00245 /* The target's value of what system call to perform. */ 00246 int func; 00247 /* The arguments to the syscall. */ 00248 long arg1, arg2, arg3, arg4; 00249 00250 /* The result. */ 00251 long result; 00252 /* Some system calls have two results. */ 00253 long result2; 00254 /* The target's errno value, or 0 if success. 00255 This is converted to the target's value with host_to_target_errno. */ 00256 int errcode; 00257 00258 /* Working space to be used by memory read/write callbacks. */ 00259 PTR p1; 00260 PTR p2; 00261 long x1,x2; 00262 00263 /* Callbacks for reading/writing memory (e.g. for read/write syscalls). 00264 ??? long or unsigned long might be better to use for the `count' 00265 argument here. We mimic sim_{read,write} for now. Be careful to 00266 test any changes with -Wall -Werror, mixed signed comparisons 00267 will get you. */ 00268 int (*read_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, 00269 unsigned long /*taddr*/, char * /*buf*/, 00270 int /*bytes*/)); 00271 int (*write_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, 00272 unsigned long /*taddr*/, const char * /*buf*/, 00273 int /*bytes*/)); 00274 00275 /* For sanity checking, should be last entry. */ 00276 int magic; 00277 } CB_SYSCALL; 00278 00279 /* Magic number sanity checker. */ 00280 #define CB_SYSCALL_MAGIC 0x12344321 00281 00282 /* Macro to initialize CB_SYSCALL. Called first, before filling in 00283 any fields. */ 00284 #define CB_SYSCALL_INIT(sc) \ 00285 do { \ 00286 memset ((sc), 0, sizeof (*(sc))); \ 00287 (sc)->magic = CB_SYSCALL_MAGIC; \ 00288 } while (0) 00289 00290 /* Return codes for various interface routines. */ 00291 00292 typedef enum { 00293 CB_RC_OK = 0, 00294 /* generic error */ 00295 CB_RC_ERR, 00296 /* either file not found or no read access */ 00297 CB_RC_ACCESS, 00298 CB_RC_NO_MEM 00299 } CB_RC; 00300 00301 /* Read in target values for system call numbers, errno values, signals. */ 00302 CB_RC cb_read_target_syscall_maps PARAMS ((host_callback *, const char *)); 00303 00304 /* Translate target to host syscall function numbers. */ 00305 int cb_target_to_host_syscall PARAMS ((host_callback *, int)); 00306 00307 /* Translate host to target errno value. */ 00308 int cb_host_to_target_errno PARAMS ((host_callback *, int)); 00309 00310 /* Translate target to host open flags. */ 00311 int cb_target_to_host_open PARAMS ((host_callback *, int)); 00312 00313 /* Translate target signal number to host. */ 00314 int cb_target_to_host_signal PARAMS ((host_callback *, int)); 00315 00316 /* Translate host signal number to target. */ 00317 int cb_host_to_target_signal PARAMS ((host_callback *, int)); 00318 00319 /* Translate host stat struct to target. 00320 If stat struct ptr is NULL, just compute target stat struct size. 00321 Result is size of target stat struct or 0 if error. */ 00322 int cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR)); 00323 00324 /* Translate a value to target endian. */ 00325 void cb_store_target_endian PARAMS ((host_callback *, char *, int, long)); 00326 00327 /* Perform a system call. */ 00328 CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *)); 00329 00330 #endif
1.5.6