1130803Smarcel/* Remote target system call callback support. 2130803Smarcel Copyright 1997 Free Software Foundation, Inc. 3130803Smarcel Contributed by Cygnus Solutions. 4130803Smarcel 5130803SmarcelThis file is part of GDB. 6130803Smarcel 7130803SmarcelThis program is free software; you can redistribute it and/or modify 8130803Smarcelit under the terms of the GNU General Public License as published by 9130803Smarcelthe Free Software Foundation; either version 2 of the License, or 10130803Smarcel(at your option) any later version. 11130803Smarcel 12130803SmarcelThis program is distributed in the hope that it will be useful, 13130803Smarcelbut WITHOUT ANY WARRANTY; without even the implied warranty of 14130803SmarcelMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15130803SmarcelGNU General Public License for more details. 16130803Smarcel 17130803SmarcelYou should have received a copy of the GNU General Public License 18130803Smarcelalong with this program; if not, write to the Free Software 19130803SmarcelFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 20130803Smarcel 21130803Smarcel/* This interface isn't intended to be specific to any particular kind 22130803Smarcel of remote (hardware, simulator, whatever). As such, support for it 23130803Smarcel (e.g. sim/common/callback.c) should *not* live in the simulator source 24130803Smarcel tree, nor should it live in the gdb source tree. */ 25130803Smarcel 26130803Smarcel/* There are various ways to handle system calls: 27130803Smarcel 28130803Smarcel 1) Have a simulator intercept the appropriate trap instruction and 29130803Smarcel directly perform the system call on behalf of the target program. 30130803Smarcel This is the typical way of handling system calls for embedded targets. 31130803Smarcel [Handling system calls for embedded targets isn't that much of an 32130803Smarcel oxymoron as running compiler testsuites make use of the capability.] 33130803Smarcel 34130803Smarcel This method of system call handling is done when STATE_ENVIRONMENT 35130803Smarcel is ENVIRONMENT_USER. 36130803Smarcel 37130803Smarcel 2) Have a simulator emulate the hardware as much as possible. 38130803Smarcel If the program running on the real hardware communicates with some sort 39130803Smarcel of target manager, one would want to be able to run this program on the 40130803Smarcel simulator as well. 41130803Smarcel 42130803Smarcel This method of system call handling is done when STATE_ENVIRONMENT 43130803Smarcel is ENVIRONMENT_OPERATING. 44130803Smarcel*/ 45130803Smarcel 46130803Smarcel#ifndef CALLBACK_H 47130803Smarcel#define CALLBACK_H 48130803Smarcel 49130803Smarcel/* ??? The reason why we check for va_start here should be documented. */ 50130803Smarcel 51130803Smarcel#ifndef va_start 52130803Smarcel#include <ansidecl.h> 53130803Smarcel#ifdef ANSI_PROTOTYPES 54130803Smarcel#include <stdarg.h> 55130803Smarcel#else 56130803Smarcel#include <varargs.h> 57130803Smarcel#endif 58130803Smarcel#endif 59130803Smarcel 60130803Smarcel/* Mapping of host/target values. */ 61130803Smarcel/* ??? For debugging purposes, one might want to add a string of the 62130803Smarcel name of the symbol. */ 63130803Smarcel 64130803Smarceltypedef struct { 65130803Smarcel int host_val; 66130803Smarcel int target_val; 67130803Smarcel} CB_TARGET_DEFS_MAP; 68130803Smarcel 69130803Smarcel#define MAX_CALLBACK_FDS 10 70130803Smarcel 71130803Smarcel/* Forward decl for stat/fstat. */ 72130803Smarcelstruct stat; 73130803Smarcel 74130803Smarceltypedef struct host_callback_struct host_callback; 75130803Smarcel 76130803Smarcelstruct host_callback_struct 77130803Smarcel{ 78130803Smarcel int (*close) PARAMS ((host_callback *,int)); 79130803Smarcel int (*get_errno) PARAMS ((host_callback *)); 80130803Smarcel int (*isatty) PARAMS ((host_callback *, int)); 81130803Smarcel int (*lseek) PARAMS ((host_callback *, int, long , int)); 82130803Smarcel int (*open) PARAMS ((host_callback *, const char*, int mode)); 83130803Smarcel int (*read) PARAMS ((host_callback *,int, char *, int)); 84130803Smarcel int (*read_stdin) PARAMS (( host_callback *, char *, int)); 85130803Smarcel int (*rename) PARAMS ((host_callback *, const char *, const char *)); 86130803Smarcel int (*system) PARAMS ((host_callback *, const char *)); 87130803Smarcel long (*time) PARAMS ((host_callback *, long *)); 88130803Smarcel int (*unlink) PARAMS ((host_callback *, const char *)); 89130803Smarcel int (*write) PARAMS ((host_callback *,int, const char *, int)); 90130803Smarcel int (*write_stdout) PARAMS ((host_callback *, const char *, int)); 91130803Smarcel void (*flush_stdout) PARAMS ((host_callback *)); 92130803Smarcel int (*write_stderr) PARAMS ((host_callback *, const char *, int)); 93130803Smarcel void (*flush_stderr) PARAMS ((host_callback *)); 94130803Smarcel int (*stat) PARAMS ((host_callback *, const char *, struct stat *)); 95130803Smarcel int (*fstat) PARAMS ((host_callback *, int, struct stat *)); 96130803Smarcel int (*ftruncate) PARAMS ((host_callback *, int, long)); 97130803Smarcel int (*truncate) PARAMS ((host_callback *, const char *, long)); 98130803Smarcel 99130803Smarcel /* When present, call to the client to give it the oportunity to 100130803Smarcel poll any io devices for a request to quit (indicated by a nonzero 101130803Smarcel return value). */ 102130803Smarcel int (*poll_quit) PARAMS ((host_callback *)); 103130803Smarcel 104130803Smarcel /* Used when the target has gone away, so we can close open 105130803Smarcel handles and free memory etc etc. */ 106130803Smarcel int (*shutdown) PARAMS ((host_callback *)); 107130803Smarcel int (*init) PARAMS ((host_callback *)); 108130803Smarcel 109130803Smarcel /* depreciated, use vprintf_filtered - Talk to the user on a console. */ 110130803Smarcel void (*printf_filtered) PARAMS ((host_callback *, const char *, ...)); 111130803Smarcel 112130803Smarcel /* Talk to the user on a console. */ 113130803Smarcel void (*vprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); 114130803Smarcel 115130803Smarcel /* Same as vprintf_filtered but to stderr. */ 116130803Smarcel void (*evprintf_filtered) PARAMS ((host_callback *, const char *, va_list)); 117130803Smarcel 118130803Smarcel /* Print an error message and "exit". 119130803Smarcel In the case of gdb "exiting" means doing a longjmp back to the main 120130803Smarcel command loop. */ 121130803Smarcel void (*error) PARAMS ((host_callback *, const char *, ...)); 122130803Smarcel 123130803Smarcel int last_errno; /* host format */ 124130803Smarcel 125130803Smarcel int fdmap[MAX_CALLBACK_FDS]; 126130803Smarcel char fdopen[MAX_CALLBACK_FDS]; 127130803Smarcel char alwaysopen[MAX_CALLBACK_FDS]; 128130803Smarcel 129130803Smarcel /* System call numbers. */ 130130803Smarcel CB_TARGET_DEFS_MAP *syscall_map; 131130803Smarcel /* Errno values. */ 132130803Smarcel CB_TARGET_DEFS_MAP *errno_map; 133130803Smarcel /* Flags to the open system call. */ 134130803Smarcel CB_TARGET_DEFS_MAP *open_map; 135130803Smarcel /* Signal numbers. */ 136130803Smarcel CB_TARGET_DEFS_MAP *signal_map; 137130803Smarcel /* Layout of `stat' struct. 138130803Smarcel The format is a series of "name,length" pairs separated by colons. 139130803Smarcel Empty space is indicated with a `name' of "space". 140130803Smarcel All padding must be explicitly mentioned. 141130803Smarcel Lengths are in bytes. If this needs to be extended to bits, 142130803Smarcel use "name.bits". 143130803Smarcel Example: "st_dev,4:st_ino,4:st_mode,4:..." */ 144130803Smarcel const char *stat_map; 145130803Smarcel 146130803Smarcel /* Marker for those wanting to do sanity checks. 147130803Smarcel This should remain the last member of this struct to help catch 148130803Smarcel miscompilation errors. */ 149130803Smarcel#define HOST_CALLBACK_MAGIC 4705 /* teds constant */ 150130803Smarcel int magic; 151130803Smarcel}; 152130803Smarcel 153130803Smarcelextern host_callback default_callback; 154130803Smarcel 155130803Smarcel/* Canonical versions of system call numbers. 156130803Smarcel It's not intended to willy-nilly throw every system call ever heard 157130803Smarcel of in here. Only include those that have an important use. 158130803Smarcel ??? One can certainly start a discussion over the ones that are currently 159130803Smarcel here, but that will always be true. */ 160130803Smarcel 161130803Smarcel/* These are used by the ANSI C support of libc. */ 162130803Smarcel#define CB_SYS_exit 1 163130803Smarcel#define CB_SYS_open 2 164130803Smarcel#define CB_SYS_close 3 165130803Smarcel#define CB_SYS_read 4 166130803Smarcel#define CB_SYS_write 5 167130803Smarcel#define CB_SYS_lseek 6 168130803Smarcel#define CB_SYS_unlink 7 169130803Smarcel#define CB_SYS_getpid 8 170130803Smarcel#define CB_SYS_kill 9 171130803Smarcel#define CB_SYS_fstat 10 172130803Smarcel/*#define CB_SYS_sbrk 11 - not currently a system call, but reserved. */ 173130803Smarcel 174130803Smarcel/* ARGV support. */ 175130803Smarcel#define CB_SYS_argvlen 12 176130803Smarcel#define CB_SYS_argv 13 177130803Smarcel 178130803Smarcel/* These are extras added for one reason or another. */ 179130803Smarcel#define CB_SYS_chdir 14 180130803Smarcel#define CB_SYS_stat 15 181130803Smarcel#define CB_SYS_chmod 16 182130803Smarcel#define CB_SYS_utime 17 183130803Smarcel#define CB_SYS_time 18 184130803Smarcel 185130803Smarcel/* Struct use to pass and return information necessary to perform a 186130803Smarcel system call. */ 187130803Smarcel/* FIXME: Need to consider target word size. */ 188130803Smarcel 189130803Smarceltypedef struct cb_syscall { 190130803Smarcel /* The target's value of what system call to perform. */ 191130803Smarcel int func; 192130803Smarcel /* The arguments to the syscall. */ 193130803Smarcel long arg1, arg2, arg3, arg4; 194130803Smarcel 195130803Smarcel /* The result. */ 196130803Smarcel long result; 197130803Smarcel /* Some system calls have two results. */ 198130803Smarcel long result2; 199130803Smarcel /* The target's errno value, or 0 if success. 200130803Smarcel This is converted to the target's value with host_to_target_errno. */ 201130803Smarcel int errcode; 202130803Smarcel 203130803Smarcel /* Working space to be used by memory read/write callbacks. */ 204130803Smarcel PTR p1; 205130803Smarcel PTR p2; 206130803Smarcel long x1,x2; 207130803Smarcel 208130803Smarcel /* Callbacks for reading/writing memory (e.g. for read/write syscalls). 209130803Smarcel ??? long or unsigned long might be better to use for the `count' 210130803Smarcel argument here. We mimic sim_{read,write} for now. Be careful to 211130803Smarcel test any changes with -Wall -Werror, mixed signed comparisons 212130803Smarcel will get you. */ 213130803Smarcel int (*read_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, 214130803Smarcel unsigned long /*taddr*/, char * /*buf*/, 215130803Smarcel int /*bytes*/)); 216130803Smarcel int (*write_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/, 217130803Smarcel unsigned long /*taddr*/, const char * /*buf*/, 218130803Smarcel int /*bytes*/)); 219130803Smarcel 220130803Smarcel /* For sanity checking, should be last entry. */ 221130803Smarcel int magic; 222130803Smarcel} CB_SYSCALL; 223130803Smarcel 224130803Smarcel/* Magic number sanity checker. */ 225130803Smarcel#define CB_SYSCALL_MAGIC 0x12344321 226130803Smarcel 227130803Smarcel/* Macro to initialize CB_SYSCALL. Called first, before filling in 228130803Smarcel any fields. */ 229130803Smarcel#define CB_SYSCALL_INIT(sc) \ 230130803Smarceldo { \ 231130803Smarcel memset ((sc), 0, sizeof (*(sc))); \ 232130803Smarcel (sc)->magic = CB_SYSCALL_MAGIC; \ 233130803Smarcel} while (0) 234130803Smarcel 235130803Smarcel/* Return codes for various interface routines. */ 236130803Smarcel 237130803Smarceltypedef enum { 238130803Smarcel CB_RC_OK = 0, 239130803Smarcel /* generic error */ 240130803Smarcel CB_RC_ERR, 241130803Smarcel /* either file not found or no read access */ 242130803Smarcel CB_RC_ACCESS, 243130803Smarcel CB_RC_NO_MEM 244130803Smarcel} CB_RC; 245130803Smarcel 246130803Smarcel/* Read in target values for system call numbers, errno values, signals. */ 247130803SmarcelCB_RC cb_read_target_syscall_maps PARAMS ((host_callback *, const char *)); 248130803Smarcel 249130803Smarcel/* Translate target to host syscall function numbers. */ 250130803Smarcelint cb_target_to_host_syscall PARAMS ((host_callback *, int)); 251130803Smarcel 252130803Smarcel/* Translate host to target errno value. */ 253130803Smarcelint cb_host_to_target_errno PARAMS ((host_callback *, int)); 254130803Smarcel 255130803Smarcel/* Translate target to host open flags. */ 256130803Smarcelint cb_target_to_host_open PARAMS ((host_callback *, int)); 257130803Smarcel 258130803Smarcel/* Translate target signal number to host. */ 259130803Smarcelint cb_target_to_host_signal PARAMS ((host_callback *, int)); 260130803Smarcel 261130803Smarcel/* Translate host signal number to target. */ 262130803Smarcelint cb_host_to_target_signal PARAMS ((host_callback *, int)); 263130803Smarcel 264130803Smarcel/* Translate host stat struct to target. 265130803Smarcel If stat struct ptr is NULL, just compute target stat struct size. 266130803Smarcel Result is size of target stat struct or 0 if error. */ 267130803Smarcelint cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR)); 268130803Smarcel 269130803Smarcel/* Perform a system call. */ 270130803SmarcelCB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *)); 271130803Smarcel 272130803Smarcel#endif 273