1 /* Remote target system call callback support.
2    Copyright (C) 1997-2021 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 3 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, see <http://www.gnu.org/licenses/>.  */
19 
20 /* This interface isn't intended to be specific to any particular kind
21    of remote (hardware, simulator, whatever).  As such, support for it
22    (e.g. sim/common/callback.c) should *not* live in the simulator source
23    tree, nor should it live in the gdb source tree.  */
24 
25 /* There are various ways to handle system calls:
26 
27    1) Have a simulator intercept the appropriate trap instruction and
28    directly perform the system call on behalf of the target program.
29    This is the typical way of handling system calls for embedded targets.
30    [Handling system calls for embedded targets isn't that much of an
31    oxymoron as running compiler testsuites make use of the capability.]
32 
33    This method of system call handling is done when STATE_ENVIRONMENT
34    is ENVIRONMENT_USER.
35 
36    2) Have a simulator emulate the hardware as much as possible.
37    If the program running on the real hardware communicates with some sort
38    of target manager, one would want to be able to run this program on the
39    simulator as well.
40 
41    This method of system call handling is done when STATE_ENVIRONMENT
42    is ENVIRONMENT_OPERATING.
43 */
44 
45 #ifndef SIM_CALLBACK_H
46 #define SIM_CALLBACK_H
47 
48 #include <ansidecl.h>
49 #include <stdarg.h>
50 #include <stdint.h>
51 /* Needed for enum bfd_endian.  */
52 #include "bfd.h"
53 
54 /* Mapping of host/target values.  */
55 /* ??? For debugging purposes, one might want to add a string of the
56    name of the symbol.  */
57 
58 typedef struct {
59   const char *name;
60   int host_val;
61   int target_val;
62 } CB_TARGET_DEFS_MAP;
63 
64 #define MAX_CALLBACK_FDS 10
65 
66 /* Forward decl for stat/fstat.  */
67 struct stat;
68 
69 typedef struct host_callback_struct host_callback;
70 
71 struct host_callback_struct
72 {
73   int (*close) (host_callback *,int);
74   int (*get_errno) (host_callback *);
75   int (*isatty) (host_callback *, int);
76   int64_t (*lseek) (host_callback *, int, int64_t, int);
77   int (*open) (host_callback *, const char*, int mode);
78   int (*read) (host_callback *,int,  char *, int);
79   int (*read_stdin) ( host_callback *, char *, int);
80   int (*rename) (host_callback *, const char *, const char *);
81   int (*system) (host_callback *, const char *);
82   int64_t (*time) (host_callback *);
83   int (*unlink) (host_callback *, const char *);
84   int (*write) (host_callback *,int, const char *, int);
85   int (*write_stdout) (host_callback *, const char *, int);
86   void (*flush_stdout) (host_callback *);
87   int (*write_stderr) (host_callback *, const char *, int);
88   void (*flush_stderr) (host_callback *);
89   int (*to_stat) (host_callback *, const char *, struct stat *);
90   int (*to_fstat) (host_callback *, int, struct stat *);
91   int (*to_lstat) (host_callback *, const char *, struct stat *);
92   int (*ftruncate) (host_callback *, int, int64_t);
93   int (*truncate) (host_callback *, const char *, int64_t);
94   int (*getpid) (host_callback *);
95   int (*kill) (host_callback *, int, int);
96   int (*pipe) (host_callback *, int *);
97 
98   /* Called by the framework when a read call has emptied a pipe buffer.  */
99   void (*pipe_empty) (host_callback *, int read_fd, int write_fd);
100 
101   /* Called by the framework when a write call makes a pipe buffer
102      non-empty.  */
103   void (*pipe_nonempty) (host_callback *, int read_fd, int write_fd);
104 
105   /* When present, call to the client to give it the oportunity to
106      poll any io devices for a request to quit (indicated by a nonzero
107      return value). */
108   int (*poll_quit) (host_callback *);
109 
110   /* Used when the target has gone away, so we can close open
111      handles and free memory etc etc.  */
112   int (*shutdown) (host_callback *);
113   int (*init)     (host_callback *);
114 
115   /* depreciated, use vprintf_filtered - Talk to the user on a console.  */
116   void (*printf_filtered) (host_callback *, const char *, ...)
117     ATTRIBUTE_PRINTF_2;
118 
119   /* Talk to the user on a console.  */
120   void (*vprintf_filtered) (host_callback *, const char *, va_list)
121     ATTRIBUTE_PRINTF (2, 0);
122 
123   /* Same as vprintf_filtered but to stderr.  */
124   void (*evprintf_filtered) (host_callback *, const char *, va_list)
125     ATTRIBUTE_PRINTF (2, 0);
126 
127   /* Print an error message and "exit".
128      In the case of gdb "exiting" means doing a longjmp back to the main
129      command loop.  */
130   void (*error) (host_callback *, const char *, ...)
131     ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_2;
132 
133   int last_errno;		/* host format */
134 
135   int fdmap[MAX_CALLBACK_FDS];
136   /* fd_buddy is used to contruct circular lists of target fds that point to
137      the same host fd.  A uniquely mapped fd points to itself; for a closed
138      one, fd_buddy has the value -1.  The host file descriptors for stdin /
139      stdout / stderr are never closed by the simulators, so they are put
140      in a special fd_buddy circular list which also has MAX_CALLBACK_FDS
141      as a member.  */
142   /* ??? We don't have a callback entry for dup, although it is trival to
143      implement now.  */
144   short fd_buddy[MAX_CALLBACK_FDS+1];
145 
146   /* 0 = none, >0 = reader (index of writer),
147      <0 = writer (negative index of reader).
148      If abs (ispipe[N]) == N, then N is an end of a pipe whose other
149      end is closed.  */
150   short ispipe[MAX_CALLBACK_FDS];
151 
152   /* A writer stores the buffer at its index.  Consecutive writes
153      realloc the buffer and add to the size.  The reader indicates the
154      read part in its .size, until it has consumed it all, at which
155      point it deallocates the buffer and zeroes out both sizes.  */
156   struct pipe_write_buffer
157   {
158     int size;
159     char *buffer;
160   } pipe_buffer[MAX_CALLBACK_FDS];
161 
162   /* System call numbers.  */
163   CB_TARGET_DEFS_MAP *syscall_map;
164   /* Errno values.  */
165   CB_TARGET_DEFS_MAP *errno_map;
166   /* Flags to the open system call.  */
167   CB_TARGET_DEFS_MAP *open_map;
168   /* Signal numbers.  */
169   CB_TARGET_DEFS_MAP *signal_map;
170   /* Layout of `stat' struct.
171      The format is a series of "name,length" pairs separated by colons.
172      Empty space is indicated with a `name' of "space".
173      All padding must be explicitly mentioned.
174      Lengths are in bytes.  If this needs to be extended to bits,
175      use "name.bits".
176      Example: "st_dev,4:st_ino,4:st_mode,4:..."  */
177   const char *stat_map;
178 
179   enum bfd_endian target_endian;
180 
181   /* Size of an "int" on the target (for syscalls whose ABI uses "int").
182      This must include padding, and only padding-at-higher-address is
183      supported.  For example, a 64-bit target with 32-bit int:s which
184      are padded to 64 bits when in an array, should supposedly set this
185      to 8.  The default is 4 which matches ILP32 targets and 64-bit
186      targets with 32-bit ints and no padding.  */
187   int target_sizeof_int;
188 
189   /* Marker for those wanting to do sanity checks.
190      This should remain the last member of this struct to help catch
191      miscompilation errors. */
192 #define HOST_CALLBACK_MAGIC 4705 /* teds constant */
193   int magic;
194 };
195 
196 extern host_callback default_callback;
197 
198 /* Canonical versions of system call numbers.
199    It's not intended to willy-nilly throw every system call ever heard
200    of in here.  Only include those that have an important use.
201    ??? One can certainly start a discussion over the ones that are currently
202    here, but that will always be true.  */
203 
204 /* These are used by the ANSI C support of libc.  */
205 #define	CB_SYS_exit	1
206 #define	CB_SYS_open	2
207 #define	CB_SYS_close	3
208 #define	CB_SYS_read	4
209 #define	CB_SYS_write	5
210 #define	CB_SYS_lseek	6
211 #define	CB_SYS_unlink	7
212 #define	CB_SYS_getpid	8
213 #define	CB_SYS_kill	9
214 #define CB_SYS_fstat    10
215 /*#define CB_SYS_sbrk	11 - not currently a system call, but reserved.  */
216 
217 /* ARGV support.  */
218 #define CB_SYS_argvlen	12
219 #define CB_SYS_argv	13
220 
221 /* These are extras added for one reason or another.  */
222 #define CB_SYS_chdir	14
223 #define CB_SYS_stat	15
224 #define CB_SYS_chmod 	16
225 #define CB_SYS_utime 	17
226 #define CB_SYS_time 	18
227 
228 /* More standard syscalls.  */
229 #define CB_SYS_lstat    19
230 #define CB_SYS_rename	20
231 #define CB_SYS_truncate	21
232 #define CB_SYS_ftruncate 22
233 #define CB_SYS_pipe 	23
234 
235 /* New ARGV support.  */
236 #define CB_SYS_argc	24
237 #define CB_SYS_argnlen	25
238 #define CB_SYS_argn	26
239 
240 /* Struct use to pass and return information necessary to perform a
241    system call.  */
242 /* FIXME: Need to consider target word size.  */
243 
244 typedef struct cb_syscall {
245   /* The target's value of what system call to perform.  */
246   int func;
247   /* The arguments to the syscall.  */
248   long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
249 
250   /* The result.  */
251   long result;
252   /* Some system calls have two results.  */
253   long result2;
254   /* The target's errno value, or 0 if success.
255      This is converted to the target's value with host_to_target_errno.  */
256   int errcode;
257 
258   /* Working space to be used by memory read/write callbacks.  */
259   void *p1;
260   void *p2;
261   long x1,x2;
262 
263   /* Callbacks for reading/writing memory (e.g. for read/write syscalls).
264      ??? long or unsigned long might be better to use for the `count'
265      argument here.  We mimic sim_{read,write} for now.  Be careful to
266      test any changes with -Wall -Werror, mixed signed comparisons
267      will get you.  */
268   int (*read_mem) (host_callback * /*cb*/, struct cb_syscall * /*sc*/,
269 		   unsigned long /*taddr*/, char * /*buf*/,
270 		   int /*bytes*/);
271   int (*write_mem) (host_callback * /*cb*/, struct cb_syscall * /*sc*/,
272 		    unsigned long /*taddr*/, const char * /*buf*/,
273 		    int /*bytes*/);
274 
275   /* For sanity checking, should be last entry.  */
276   int magic;
277 } CB_SYSCALL;
278 
279 /* Magic number sanity checker.  */
280 #define CB_SYSCALL_MAGIC 0x12344321
281 
282 /* Macro to initialize CB_SYSCALL.  Called first, before filling in
283    any fields.  */
284 #define CB_SYSCALL_INIT(sc) \
285 do { \
286   memset ((sc), 0, sizeof (*(sc))); \
287   (sc)->magic = CB_SYSCALL_MAGIC; \
288 } while (0)
289 
290 /* Return codes for various interface routines.  */
291 
292 typedef enum {
293   CB_RC_OK = 0,
294   /* generic error */
295   CB_RC_ERR,
296   /* either file not found or no read access */
297   CB_RC_ACCESS,
298   CB_RC_NO_MEM
299 } CB_RC;
300 
301 /* Read in target values for system call numbers, errno values, signals.  */
302 CB_RC cb_read_target_syscall_maps (host_callback *, const char *);
303 
304 /* Translate target to host syscall function numbers.  */
305 int cb_target_to_host_syscall (host_callback *, int);
306 
307 /* Translate host to target errno value.  */
308 int cb_host_to_target_errno (host_callback *, int);
309 
310 /* Translate target to host open flags.  */
311 int cb_target_to_host_open (host_callback *, int);
312 
313 /* Translate target signal number to host.  */
314 int cb_target_to_host_signal (host_callback *, int);
315 
316 /* Translate host signal number to target.  */
317 int cb_host_to_gdb_signal (host_callback *, int);
318 
319 /* Translate symbols into human readable strings.  */
320 const char *cb_host_str_syscall (host_callback *, int);
321 const char *cb_host_str_errno (host_callback *, int);
322 const char *cb_host_str_signal (host_callback *, int);
323 const char *cb_target_str_syscall (host_callback *, int);
324 const char *cb_target_str_errno (host_callback *, int);
325 const char *cb_target_str_signal (host_callback *, int);
326 
327 /* Translate host stat struct to target.
328    If stat struct ptr is NULL, just compute target stat struct size.
329    Result is size of target stat struct or 0 if error.  */
330 int cb_host_to_target_stat (host_callback *, const struct stat *, void *);
331 
332 /* Translate a value to target endian.  */
333 void cb_store_target_endian (host_callback *, char *, int, long);
334 
335 /* Tests for special fds.  */
336 int cb_is_stdin (host_callback *, int);
337 int cb_is_stdout (host_callback *, int);
338 int cb_is_stderr (host_callback *, int);
339 
340 /* Read a string out of the target.  */
341 int cb_get_string (host_callback *, CB_SYSCALL *, char *, int, unsigned long);
342 
343 /* Perform a system call.  */
344 CB_RC cb_syscall (host_callback *, CB_SYSCALL *);
345 
346 #endif
347