xref: /openbmc/qemu/bsd-user/syscall_defs.h (revision 2df1eb27)
1 /*
2  *  System call related declarations
3  *
4  *  Copyright (c) 2013-15 Stacey D. Son (sson at FreeBSD)
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef SYSCALL_DEFS_H
21 #define SYSCALL_DEFS_H
22 
23 #include <sys/syscall.h>
24 #include <sys/resource.h>
25 
26 #include "errno_defs.h"
27 
28 #include "freebsd/syscall_nr.h"
29 #include "netbsd/syscall_nr.h"
30 #include "openbsd/syscall_nr.h"
31 
32 /*
33  * machine/_types.h
34  * or x86/_types.h
35  */
36 
37 /*
38  * time_t seems to be very inconsistly defined for the different *BSD's...
39  *
40  * FreeBSD uses a 64bits time_t except on i386
41  * so we have to add a special case here.
42  *
43  * On NetBSD time_t is always defined as an int64_t.  On OpenBSD time_t
44  * is always defined as an int.
45  *
46  */
47 #if (!defined(TARGET_I386))
48 typedef int64_t target_time_t;
49 #else
50 typedef int32_t target_time_t;
51 #endif
52 
53 struct target_iovec {
54     abi_long iov_base;   /* Starting address */
55     abi_long iov_len;   /* Number of bytes */
56 };
57 
58 /*
59  * sys/ipc.h
60  */
61 struct target_ipc_perm {
62     uint32_t    cuid;       /* creator user id */
63     uint32_t    cgid;       /* creator group id */
64     uint32_t    uid;        /* user id */
65     uint32_t    gid;        /* group id */
66     uint16_t    mode;       /* r/w permission */
67     uint16_t    seq;        /* sequence # */
68     abi_long    key;        /* user specified msg/sem/shm key */
69 };
70 
71 #define TARGET_IPC_RMID 0   /* remove identifier */
72 #define TARGET_IPC_SET  1   /* set options */
73 #define TARGET_IPC_STAT 2   /* get options */
74 
75 /*
76  * sys/shm.h
77  */
78 struct target_shmid_ds {
79     struct  target_ipc_perm shm_perm; /* peration permission structure */
80     abi_ulong   shm_segsz;  /* size of segment in bytes */
81     int32_t     shm_lpid;   /* process ID of last shared memory op */
82     int32_t     shm_cpid;   /* process ID of creator */
83     int32_t     shm_nattch; /* number of current attaches */
84     target_time_t shm_atime;  /* time of last shmat() */
85     target_time_t shm_dtime;  /* time of last shmdt() */
86     target_time_t shm_ctime;  /* time of last change by shmctl() */
87 };
88 
89 #define N_BSD_SHM_REGIONS   32
90 struct bsd_shm_regions {
91     abi_long start;
92     abi_long size;
93 };
94 
95 /*
96  *  sys/mman.h
97  */
98 #define TARGET_MADV_DONTNEED            4       /* dont need these pages */
99 
100 #define TARGET_FREEBSD_MAP_RESERVED0080 0x0080  /* previously misimplemented */
101                                                 /* MAP_INHERIT */
102 #define TARGET_FREEBSD_MAP_RESERVED0100 0x0100  /* previously unimplemented */
103                                                 /* MAP_NOEXTEND */
104 #define TARGET_FREEBSD_MAP_STACK        0x0400  /* region grows down, like a */
105                                                 /* stack */
106 #define TARGET_FREEBSD_MAP_NOSYNC       0x0800  /* page to but do not sync */
107                                                 /* underlying file */
108 
109 #define TARGET_FREEBSD_MAP_FLAGMASK     0x1ff7
110 
111 #define TARGET_NETBSD_MAP_INHERIT       0x0080  /* region is retained after */
112                                                 /* exec */
113 #define TARGET_NETBSD_MAP_TRYFIXED      0x0400  /* attempt hint address, even */
114                                                 /* within break */
115 #define TARGET_NETBSD_MAP_WIRED         0x0800  /* mlock() mapping when it is */
116                                                 /* established */
117 
118 #define TARGET_NETBSD_MAP_STACK         0x2000  /* allocated from memory, */
119                                                 /* swap space (stack) */
120 
121 #define TARGET_NETBSD_MAP_FLAGMASK      0x3ff7
122 
123 #define TARGET_OPENBSD_MAP_INHERIT      0x0080  /* region is retained after */
124                                                 /* exec */
125 #define TARGET_OPENBSD_MAP_NOEXTEND     0x0100  /* for MAP_FILE, don't change */
126                                                 /* file size */
127 #define TARGET_OPENBSD_MAP_TRYFIXED     0x0400  /* attempt hint address, */
128                                                 /* even within heap */
129 
130 #define TARGET_OPENBSD_MAP_FLAGMASK     0x17f7
131 
132 /* XXX */
133 #define TARGET_BSD_MAP_FLAGMASK         0x3ff7
134 
135 /*
136  * sys/time.h
137  * sys/timex.h
138  */
139 
140 typedef abi_long target_freebsd_suseconds_t;
141 
142 /* compare to sys/timespec.h */
143 struct target_freebsd_timespec {
144     target_time_t   tv_sec;     /* seconds */
145     abi_long                tv_nsec;    /* and nanoseconds */
146 #if !defined(TARGET_I386) && TARGET_ABI_BITS == 32
147     abi_long _pad;
148 #endif
149 };
150 
151 #define TARGET_CPUCLOCK_WHICH_PID   0
152 #define TARGET_CPUCLOCK_WHICH_TID   1
153 
154 /* sys/umtx.h */
155 struct target_freebsd__umtx_time {
156     struct target_freebsd_timespec  _timeout;
157     uint32_t    _flags;
158     uint32_t    _clockid;
159 };
160 
161 struct target_freebsd_timeval {
162     target_time_t       tv_sec; /* seconds */
163     target_freebsd_suseconds_t  tv_usec;/* and microseconds */
164 #if !defined(TARGET_I386) && TARGET_ABI_BITS == 32
165     abi_long _pad;
166 #endif
167 };
168 
169 /*
170  *  sys/resource.h
171  */
172 #define TARGET_RLIM_INFINITY    RLIM_INFINITY
173 
174 #define TARGET_RLIMIT_CPU       0
175 #define TARGET_RLIMIT_FSIZE     1
176 #define TARGET_RLIMIT_DATA      2
177 #define TARGET_RLIMIT_STACK     3
178 #define TARGET_RLIMIT_CORE      4
179 #define TARGET_RLIMIT_RSS       5
180 #define TARGET_RLIMIT_MEMLOCK   6
181 #define TARGET_RLIMIT_NPROC     7
182 #define TARGET_RLIMIT_NOFILE    8
183 #define TARGET_RLIMIT_SBSIZE    9
184 #define TARGET_RLIMIT_AS        10
185 #define TARGET_RLIMIT_NPTS      11
186 #define TARGET_RLIMIT_SWAP      12
187 
188 struct target_rlimit {
189     uint64_t rlim_cur;
190     uint64_t rlim_max;
191 };
192 
193 struct target_freebsd_rusage {
194     struct target_freebsd_timeval ru_utime; /* user time used */
195     struct target_freebsd_timeval ru_stime; /* system time used */
196     abi_long    ru_maxrss;      /* maximum resident set size */
197     abi_long    ru_ixrss;       /* integral shared memory size */
198     abi_long    ru_idrss;       /* integral unshared data size */
199     abi_long    ru_isrss;       /* integral unshared stack size */
200     abi_long    ru_minflt;      /* page reclaims */
201     abi_long    ru_majflt;      /* page faults */
202     abi_long    ru_nswap;       /* swaps */
203     abi_long    ru_inblock;     /* block input operations */
204     abi_long    ru_oublock;     /* block output operations */
205     abi_long    ru_msgsnd;      /* messages sent */
206     abi_long    ru_msgrcv;      /* messages received */
207     abi_long    ru_nsignals;    /* signals received */
208     abi_long    ru_nvcsw;       /* voluntary context switches */
209     abi_long    ru_nivcsw;      /* involuntary context switches */
210 };
211 
212 struct target_freebsd__wrusage {
213     struct target_freebsd_rusage wru_self;
214     struct target_freebsd_rusage wru_children;
215 };
216 
217 /*
218  * sys/stat.h
219  */
220 struct target_freebsd11_stat {
221     uint32_t  st_dev;       /* inode's device */
222     uint32_t  st_ino;       /* inode's number */
223     int16_t   st_mode;      /* inode protection mode */
224     int16_t   st_nlink;     /* number of hard links */
225     uint32_t  st_uid;       /* user ID of the file's owner */
226     uint32_t  st_gid;       /* group ID of the file's group */
227     uint32_t  st_rdev;      /* device type */
228     struct  target_freebsd_timespec st_atim; /* time last accessed */
229     struct  target_freebsd_timespec st_mtim; /* time last data modification */
230     struct  target_freebsd_timespec st_ctim; /* time last file status change */
231     int64_t    st_size;     /* file size, in bytes */
232     int64_t    st_blocks;   /* blocks allocated for file */
233     uint32_t   st_blksize;  /* optimal blocksize for I/O */
234     uint32_t   st_flags;    /* user defined flags for file */
235     uint32_t   st_gen;      /* file generation number */
236     int32_t    st_lspare;
237     struct target_freebsd_timespec st_birthtim; /* time of file creation */
238     /*
239      * Explicitly pad st_birthtim to 16 bytes so that the size of
240      * struct stat is backwards compatible.  We use bitfields instead
241      * of an array of chars so that this doesn't require a C99 compiler
242      * to compile if the size of the padding is 0.  We use 2 bitfields
243      * to cover up to 64 bits on 32-bit machines.  We assume that
244      * CHAR_BIT is 8...
245      */
246     unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
247     unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
248 } __packed;
249 
250 #if defined(__i386__)
251 #define TARGET_HAS_STAT_TIME_T_EXT       1
252 #endif
253 
254 struct target_stat {
255     uint64_t  st_dev;               /* inode's device */
256     uint64_t  st_ino;               /* inode's number */
257     uint64_t  st_nlink;             /* number of hard links */
258     int16_t   st_mode;              /* inode protection mode */
259     int16_t   st_padding0;
260     uint32_t  st_uid;               /* user ID of the file's owner */
261     uint32_t  st_gid;               /* group ID of the file's group */
262     int32_t   st_padding1;
263     uint64_t  st_rdev;              /* device type */
264 #ifdef TARGET_HAS_STAT_TIME_T_EXT
265     int32_t   st_atim_ext;
266 #endif
267     struct  target_freebsd_timespec st_atim; /* time of last access */
268 #ifdef TARGET_HAS_STAT_TIME_T_EXT
269     int32_t   st_mtim_ext;
270 #endif
271     struct  target_freebsd_timespec st_mtim; /* time of last data modification */
272 #ifdef TARGET_HAS_STAT_TIME_T_EXT
273     int32_t st_ctim_ext;
274 #endif
275     struct  target_freebsd_timespec st_ctim;/* time of last file status change */
276 #ifdef TARGET_HAS_STAT_TIME_T_EXT
277     int32_t st_btim_ext;
278 #endif
279     struct  target_freebsd_timespec st_birthtim;   /* time of file creation */
280     int64_t   st_size;              /* file size, in bytes */
281     int64_t   st_blocks;            /* blocks allocated for file */
282     uint32_t  st_blksize;           /* optimal blocksize for I/O */
283     uint32_t  st_flags;             /* user defined flags for file */
284     uint64_t  st_gen;               /* file generation number */
285     uint64_t  st_spare[10];
286 };
287 
288 
289 /* struct nstat is the same as stat above but without the st_lspare field */
290 struct target_freebsd11_nstat {
291     uint32_t  st_dev;       /* inode's device */
292     uint32_t  st_ino;       /* inode's number */
293     int16_t   st_mode;      /* inode protection mode */
294     int16_t   st_nlink;     /* number of hard links */
295     uint32_t  st_uid;       /* user ID of the file's owner */
296     uint32_t  st_gid;       /* group ID of the file's group */
297     uint32_t  st_rdev;      /* device type */
298     struct  target_freebsd_timespec st_atim; /* time last accessed */
299     struct  target_freebsd_timespec st_mtim; /* time last data modification */
300     struct  target_freebsd_timespec st_ctim; /* time last file status change */
301     int64_t    st_size;     /* file size, in bytes */
302     int64_t    st_blocks;   /* blocks allocated for file */
303     uint32_t   st_blksize;  /* optimal blocksize for I/O */
304     uint32_t   st_flags;    /* user defined flags for file */
305     uint32_t   st_gen;      /* file generation number */
306     struct target_freebsd_timespec st_birthtim; /* time of file creation */
307     /*
308      * Explicitly pad st_birthtim to 16 bytes so that the size of
309      * struct stat is backwards compatible.  We use bitfields instead
310      * of an array of chars so that this doesn't require a C99 compiler
311      * to compile if the size of the padding is 0.  We use 2 bitfields
312      * to cover up to 64 bits on 32-bit machines.  We assume that
313      * CHAR_BIT is 8...
314      */
315     unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
316     unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
317 } __packed;
318 
319 /*
320  * sys/mount.h
321  */
322 
323 /* filesystem id type */
324 typedef struct target_freebsd_fsid { int32_t val[2]; } target_freebsd_fsid_t;
325 
326 /* filesystem statistics */
327 struct target_freebsd11_statfs {
328     uint32_t f_version; /* structure version number */
329     uint32_t f_type;    /* type of filesystem */
330     uint64_t f_flags;   /* copy of mount exported flags */
331     uint64_t f_bsize;   /* filesystem fragment size */
332     uint64_t f_iosize;  /* optimal transfer block size */
333     uint64_t f_blocks;  /* total data blocks in filesystem */
334     uint64_t f_bfree;   /* free blocks in filesystem */
335     int64_t  f_bavail;  /* free blocks avail to non-superuser */
336     uint64_t f_files;   /* total file nodes in filesystem */
337     int64_t  f_ffree;   /* free nodes avail to non-superuser */
338     uint64_t f_syncwrites;  /* count of sync writes since mount */
339     uint64_t f_asyncwrites; /* count of async writes since mount */
340     uint64_t f_syncreads;   /* count of sync reads since mount */
341     uint64_t f_asyncreads;  /* count of async reads since mount */
342     uint64_t f_spare[10];   /* unused spare */
343     uint32_t f_namemax; /* maximum filename length */
344     uint32_t f_owner;   /* user that mounted the filesystem */
345     target_freebsd_fsid_t   f_fsid; /* filesystem id */
346     char     f_charspare[80];           /* spare string space */
347     char     f_fstypename[16];   /* filesys type name */
348     char     f_mntfromname[88];    /* mount filesystem */
349     char     f_mntonname[88];      /* dir on which mounted*/
350 };
351 
352 struct target_statfs {
353     uint32_t f_version;             /* structure version number */
354     uint32_t f_type;                /* type of filesystem */
355     uint64_t f_flags;               /* copy of mount exported flags */
356     uint64_t f_bsize;               /* filesystem fragment size */
357     uint64_t f_iosize;              /* optimal transfer block size */
358     uint64_t f_blocks;              /* total data blocks in filesystem */
359     uint64_t f_bfree;               /* free blocks in filesystem */
360     int64_t  f_bavail;              /* free blocks avail to non-superuser */
361     uint64_t f_files;               /* total file nodes in filesystem */
362     int64_t  f_ffree;               /* free nodes avail to non-superuser */
363     uint64_t f_syncwrites;          /* count of sync writes since mount */
364     uint64_t f_asyncwrites;         /* count of async writes since mount */
365     uint64_t f_syncreads;           /* count of sync reads since mount */
366     uint64_t f_asyncreads;          /* count of async reads since mount */
367     uint64_t f_spare[10];           /* unused spare */
368     uint32_t f_namemax;             /* maximum filename length */
369     uint32_t f_owner;               /* user that mounted the filesystem */
370     target_freebsd_fsid_t f_fsid;   /* filesystem id */
371     char      f_charspare[80];      /* spare string space */
372     char      f_fstypename[16];     /* filesystem type name */
373     char      f_mntfromname[1024];  /* mounted filesystem */
374     char      f_mntonname[1024];    /* directory on which mounted */
375 };
376 
377 /* File identifier. These are unique per filesystem on a single machine. */
378 #define TARGET_MAXFIDSZ     16
379 
380 struct target_freebsd_fid {
381     uint16_t    fid_len;            /* len of data in bytes */
382     uint16_t    fid_data0;          /* force longword align */
383     char        fid_data[TARGET_MAXFIDSZ];  /* data (variable len) */
384 };
385 
386 /* Generic file handle */
387 struct target_freebsd_fhandle {
388     target_freebsd_fsid_t   fh_fsid;    /* Filesystem id of mount point */
389     struct target_freebsd_fid fh_fid;   /* Filesys specific id */
390 };
391 typedef struct target_freebsd_fhandle target_freebsd_fhandle_t;
392 
393 /*
394  * sys/fcntl.h
395  */
396 #define TARGET_F_DUPFD              0
397 #define TARGET_F_GETFD              1
398 #define TARGET_F_SETFD              2
399 #define TARGET_F_GETFL              3
400 #define TARGET_F_SETFL              4
401 #define TARGET_F_GETOWN             5
402 #define TARGET_F_SETOWN             6
403 #define TARGET_F_OGETLK             7
404 #define TARGET_F_OSETLK             8
405 #define TARGET_F_OSETLKW            9
406 #define TARGET_F_DUP2FD             10
407 #define TARGET_F_GETLK              11
408 #define TARGET_F_SETLK              12
409 #define TARGET_F_SETLKW             13
410 #define TARGET_F_SETLK_REMOTE       14
411 #define TARGET_F_READAHEAD          15
412 #define TARGET_F_RDAHEAD            16
413 #define TARGET_F_DUPFD_CLOEXEC     17
414 #define TARGET_F_DUP2FD_CLOEXEC    18
415 /* FreeBSD-specific */
416 #define TARGET_F_ADD_SEALS          19
417 #define TARGET_F_GET_SEALS          20
418 
419 struct target_freebsd_flock {
420     int64_t l_start;
421     int64_t l_len;
422     int32_t l_pid;
423     int16_t l_type;
424     int16_t l_whence;
425     int32_t l_sysid;
426 } QEMU_PACKED;
427 
428 /* sys/unistd.h */
429 /* user: vfork(2) semantics, clear signals */
430 #define TARGET_RFSPAWN (1U << 31)
431 
432 /*
433  * from sys/procctl.h
434  */
435 #define TARGET_PROC_SPROTECT            1
436 #define TARGET_PROC_REAP_ACQUIRE        2
437 #define TARGET_PROC_REAP_RELEASE        3
438 #define TARGET_PROC_REAP_STATUS         4
439 #define TARGET_PROC_REAP_GETPIDS        5
440 #define TARGET_PROC_REAP_KILL           6
441 
442 struct target_procctl_reaper_status {
443     uint32_t rs_flags;
444     uint32_t rs_children;
445     uint32_t rs_descendants;
446     uint32_t rs_reaper;
447     uint32_t rs_pid;
448     uint32_t rs_pad0[15];
449 };
450 
451 struct target_procctl_reaper_pidinfo {
452     uint32_t pi_pid;
453     uint32_t pi_subtree;
454     uint32_t pi_flags;
455     uint32_t pi_pad0[15];
456 };
457 
458 struct target_procctl_reaper_pids {
459     uint32_t rp_count;
460     uint32_t rp_pad0[15];
461     abi_ulong rp_pids;
462 };
463 
464 struct target_procctl_reaper_kill {
465     int32_t  rk_sig;
466     uint32_t rk_flags;
467     uint32_t rk_subtree;
468     uint32_t rk_killed;
469     uint32_t rk_fpid;
470     uint32_t rk_pad0[15];
471 };
472 
473 
474 #define safe_syscall0(type, name) \
475 type safe_##name(void) \
476 { \
477     return safe_syscall(SYS_##name); \
478 }
479 
480 #define safe_syscall1(type, name, type1, arg1) \
481 type safe_##name(type1 arg1) \
482 { \
483     return safe_syscall(SYS_##name, arg1); \
484 }
485 
486 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
487 type safe_##name(type1 arg1, type2 arg2) \
488 { \
489     return safe_syscall(SYS_##name, arg1, arg2); \
490 }
491 
492 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
493 type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
494 { \
495     return safe_syscall(SYS_##name, arg1, arg2, arg3); \
496 }
497 
498 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
499     type4, arg4) \
500 type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
501 { \
502     return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4); \
503 }
504 
505 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
506     type4, arg4, type5, arg5) \
507 type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
508     type5 arg5) \
509 { \
510     return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4, arg5); \
511 }
512 
513 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
514     type4, arg4, type5, arg5, type6, arg6) \
515 type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
516     type5 arg5, type6 arg6) \
517 { \
518     return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
519 }
520 
521 #define safe_fcntl(...) safe_syscall(SYS_fcntl, __VA_ARGS__)
522 
523 /* So far all target and host bitmasks are the same */
524 #undef  target_to_host_bitmask
525 #define target_to_host_bitmask(x, tbl) (x)
526 #undef  host_to_target_bitmask
527 #define host_to_target_bitmask(x, tbl) (x)
528 
529 #endif /* SYSCALL_DEFS_H */
530