blob: 80a3e56f44a42791c73a5a4f2f3af2135e960489 [file] [log] [blame]
mcgrathr@google.coma7999932011-11-21 22:26:20 +00001/* Copyright (c) 2005-2011, Google Inc.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * ---
31 * Author: Markus Gutschke
32 */
33
34/* This file includes Linux-specific support functions common to the
35 * coredumper and the thread lister; primarily, this is a collection
36 * of direct system calls, and a couple of symbols missing from
37 * standard header files.
38 * There are a few options that the including file can set to control
39 * the behavior of this file:
40 *
41 * SYS_CPLUSPLUS:
42 * The entire header file will normally be wrapped in 'extern "C" { }",
43 * making it suitable for compilation as both C and C++ source. If you
44 * do not want to do this, you can set the SYS_CPLUSPLUS macro to inhibit
45 * the wrapping. N.B. doing so will suppress inclusion of all prerequisite
46 * system header files, too. It is the caller's responsibility to provide
47 * the necessary definitions.
48 *
49 * SYS_ERRNO:
50 * All system calls will update "errno" unless overriden by setting the
51 * SYS_ERRNO macro prior to including this file. SYS_ERRNO should be
52 * an l-value.
53 *
54 * SYS_INLINE:
55 * New symbols will be defined "static inline", unless overridden by
56 * the SYS_INLINE macro.
57 *
58 * SYS_LINUX_SYSCALL_SUPPORT_H
59 * This macro is used to avoid multiple inclusions of this header file.
60 * If you need to include this file more than once, make sure to
61 * unset SYS_LINUX_SYSCALL_SUPPORT_H before each inclusion.
62 *
63 * SYS_PREFIX:
64 * New system calls will have a prefix of "sys_" unless overridden by
65 * the SYS_PREFIX macro. Valid values for this macro are [0..9] which
66 * results in prefixes "sys[0..9]_". It is also possible to set this
67 * macro to -1, which avoids all prefixes.
68 *
69 * SYS_SYSCALL_ENTRYPOINT:
70 * Some applications (such as sandboxes that filter system calls), need
71 * to be able to run custom-code each time a system call is made. If this
72 * macro is defined, it expands to the name of a "common" symbol. If
73 * this symbol is assigned a non-NULL pointer value, it is used as the
74 * address of the system call entrypoint.
75 * A pointer to this symbol can be obtained by calling
76 * get_syscall_entrypoint()
77 *
78 * This file defines a few internal symbols that all start with "LSS_".
79 * Do not access these symbols from outside this file. They are not part
80 * of the supported API.
81 */
82#ifndef SYS_LINUX_SYSCALL_SUPPORT_H
83#define SYS_LINUX_SYSCALL_SUPPORT_H
84
Bryan Chan3f6478a2016-06-14 08:38:17 -040085/* We currently only support x86-32, x86-64, ARM, MIPS, PPC, s390 and s390x
86 * on Linux.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +000087 * Porting to other related platforms should not be difficult.
88 */
89#if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) || \
anton@chromium.org2f724fc2014-04-15 13:05:20 +000090 defined(__mips__) || defined(__PPC__) || defined(__ARM_EABI__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -040091 defined(__aarch64__) || defined(__s390__)) \
zodiac@gmail.com4f470182010-10-13 03:47:54 +000092 && (defined(__linux) || defined(__ANDROID__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +000093
94#ifndef SYS_CPLUSPLUS
95#ifdef __cplusplus
96/* Some system header files in older versions of gcc neglect to properly
97 * handle being included from C++. As it appears to be harmless to have
98 * multiple nested 'extern "C"' blocks, just add another one here.
99 */
100extern "C" {
101#endif
102
103#include <errno.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000104#include <fcntl.h>
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000105#include <sched.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000106#include <signal.h>
107#include <stdarg.h>
108#include <stddef.h>
vapier@chromium.org2273e812013-04-01 17:52:44 +0000109#include <stdint.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000110#include <string.h>
111#include <sys/ptrace.h>
112#include <sys/resource.h>
113#include <sys/time.h>
114#include <sys/types.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000115#include <sys/syscall.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000116#include <unistd.h>
117#include <linux/unistd.h>
118#include <endian.h>
119
120#ifdef __mips__
121/* Include definitions of the ABI currently in use. */
mseaborn@chromium.org4fc94222015-08-11 21:15:24 +0000122#ifdef __ANDROID__
123/* Android doesn't have sgidefs.h, but does have asm/sgidefs.h,
124 * which has the definitions we need.
125 */
126#include <asm/sgidefs.h>
127#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000128#include <sgidefs.h>
129#endif
130#endif
mseaborn@chromium.org4fc94222015-08-11 21:15:24 +0000131#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000132
mseaborn@chromium.orgca749372012-09-05 18:26:20 +0000133/* The Android NDK's <sys/stat.h> #defines these macros as aliases
134 * to their non-64 counterparts. To avoid naming conflict, remove them. */
135#ifdef __ANDROID__
136 /* These are restored by the corresponding #pragma pop_macro near
137 * the end of this file. */
138# pragma push_macro("stat64")
139# pragma push_macro("fstat64")
140# pragma push_macro("lstat64")
141# undef stat64
142# undef fstat64
143# undef lstat64
144#endif
145
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000146/* As glibc often provides subtly incompatible data structures (and implicit
147 * wrapper functions that convert them), we provide our own kernel data
148 * structures for use by the system calls.
149 * These structures have been developed by using Linux 2.6.23 headers for
150 * reference. Note though, we do not care about exact API compatibility
151 * with the kernel, and in fact the kernel often does not have a single
152 * API that works across architectures. Instead, we try to mimic the glibc
153 * API where reasonable, and only guarantee ABI compatibility with the
154 * kernel headers.
155 * Most notably, here are a few changes that were made to the structures
156 * defined by kernel headers:
157 *
158 * - we only define structures, but not symbolic names for kernel data
159 * types. For the latter, we directly use the native C datatype
160 * (i.e. "unsigned" instead of "mode_t").
161 * - in a few cases, it is possible to define identical structures for
162 * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
163 * standardizing on the 64bit version of the data types. In particular,
164 * this means that we use "unsigned" where the 32bit headers say
165 * "unsigned long".
166 * - overall, we try to minimize the number of cases where we need to
167 * conditionally define different structures.
168 * - the "struct kernel_sigaction" class of structures have been
169 * modified to more closely mimic glibc's API by introducing an
170 * anonymous union for the function pointer.
171 * - a small number of field names had to have an underscore appended to
172 * them, because glibc defines a global macro by the same name.
173 */
174
175/* include/linux/dirent.h */
176struct kernel_dirent64 {
177 unsigned long long d_ino;
178 long long d_off;
179 unsigned short d_reclen;
180 unsigned char d_type;
181 char d_name[256];
182};
183
184/* include/linux/dirent.h */
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000185#if defined(__aarch64__)
186// aarch64 only defines dirent64, just uses that for dirent too.
187#define kernel_dirent kernel_dirent64
188#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000189struct kernel_dirent {
190 long d_ino;
191 long d_off;
192 unsigned short d_reclen;
193 char d_name[256];
194};
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000195#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000196
197/* include/linux/uio.h */
198struct kernel_iovec {
199 void *iov_base;
200 unsigned long iov_len;
201};
202
203/* include/linux/socket.h */
204struct kernel_msghdr {
205 void *msg_name;
206 int msg_namelen;
207 struct kernel_iovec*msg_iov;
208 unsigned long msg_iovlen;
209 void *msg_control;
210 unsigned long msg_controllen;
211 unsigned msg_flags;
212};
213
214/* include/asm-generic/poll.h */
215struct kernel_pollfd {
216 int fd;
217 short events;
218 short revents;
219};
220
221/* include/linux/resource.h */
222struct kernel_rlimit {
223 unsigned long rlim_cur;
224 unsigned long rlim_max;
225};
226
227/* include/linux/time.h */
228struct kernel_timespec {
229 long tv_sec;
230 long tv_nsec;
231};
232
233/* include/linux/time.h */
234struct kernel_timeval {
235 long tv_sec;
236 long tv_usec;
237};
238
239/* include/linux/resource.h */
240struct kernel_rusage {
241 struct kernel_timeval ru_utime;
242 struct kernel_timeval ru_stime;
243 long ru_maxrss;
244 long ru_ixrss;
245 long ru_idrss;
246 long ru_isrss;
247 long ru_minflt;
248 long ru_majflt;
249 long ru_nswap;
250 long ru_inblock;
251 long ru_oublock;
252 long ru_msgsnd;
253 long ru_msgrcv;
254 long ru_nsignals;
255 long ru_nvcsw;
256 long ru_nivcsw;
257};
258
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000259#if defined(__i386__) || defined(__ARM_EABI__) || defined(__ARM_ARCH_3__) \
Bryan Chan3f6478a2016-06-14 08:38:17 -0400260 || defined(__PPC__) || (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000261
262/* include/asm-{arm,i386,mips,ppc}/signal.h */
263struct kernel_old_sigaction {
264 union {
265 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000266 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000267 };
268 unsigned long sa_mask;
269 unsigned long sa_flags;
270 void (*sa_restorer)(void);
271} __attribute__((packed,aligned(4)));
272#elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
273 #define kernel_old_sigaction kernel_sigaction
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000274#elif defined(__aarch64__)
275 // No kernel_old_sigaction defined for arm64.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000276#endif
277
278/* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
279 * exactly match the size of the signal set, even though the API was
280 * intended to be extensible. We define our own KERNEL_NSIG to deal with
281 * this.
282 * Please note that glibc provides signals [1.._NSIG-1], whereas the
283 * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
284 * actual number of signals is obviously the same, but the constants
285 * differ by one.
286 */
287#ifdef __mips__
288#define KERNEL_NSIG 128
289#else
290#define KERNEL_NSIG 64
291#endif
292
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000293/* include/asm-{arm,aarch64,i386,mips,x86_64}/signal.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000294struct kernel_sigset_t {
295 unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/
296 (8*sizeof(unsigned long))];
297};
298
299/* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h */
300struct kernel_sigaction {
301#ifdef __mips__
302 unsigned long sa_flags;
303 union {
304 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000305 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000306 };
307 struct kernel_sigset_t sa_mask;
308#else
309 union {
310 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000311 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000312 };
313 unsigned long sa_flags;
314 void (*sa_restorer)(void);
315 struct kernel_sigset_t sa_mask;
316#endif
317};
318
319/* include/linux/socket.h */
320struct kernel_sockaddr {
321 unsigned short sa_family;
322 char sa_data[14];
323};
324
Bryan Chan3f6478a2016-06-14 08:38:17 -0400325/* include/asm-{arm,aarch64,i386,mips,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000326#ifdef __mips__
327#if _MIPS_SIM == _MIPS_SIM_ABI64
328struct kernel_stat {
329#else
330struct kernel_stat64 {
331#endif
332 unsigned st_dev;
333 unsigned __pad0[3];
334 unsigned long long st_ino;
335 unsigned st_mode;
336 unsigned st_nlink;
337 unsigned st_uid;
338 unsigned st_gid;
339 unsigned st_rdev;
340 unsigned __pad1[3];
341 long long st_size;
342 unsigned st_atime_;
343 unsigned st_atime_nsec_;
344 unsigned st_mtime_;
345 unsigned st_mtime_nsec_;
346 unsigned st_ctime_;
347 unsigned st_ctime_nsec_;
348 unsigned st_blksize;
349 unsigned __pad2;
350 unsigned long long st_blocks;
351};
352#elif defined __PPC__
353struct kernel_stat64 {
354 unsigned long long st_dev;
355 unsigned long long st_ino;
356 unsigned st_mode;
357 unsigned st_nlink;
358 unsigned st_uid;
359 unsigned st_gid;
360 unsigned long long st_rdev;
361 unsigned short int __pad2;
362 long long st_size;
363 long st_blksize;
364 long long st_blocks;
365 long st_atime_;
366 unsigned long st_atime_nsec_;
367 long st_mtime_;
368 unsigned long st_mtime_nsec_;
369 long st_ctime_;
370 unsigned long st_ctime_nsec_;
371 unsigned long __unused4;
372 unsigned long __unused5;
373};
374#else
375struct kernel_stat64 {
376 unsigned long long st_dev;
377 unsigned char __pad0[4];
378 unsigned __st_ino;
379 unsigned st_mode;
380 unsigned st_nlink;
381 unsigned st_uid;
382 unsigned st_gid;
383 unsigned long long st_rdev;
384 unsigned char __pad3[4];
385 long long st_size;
386 unsigned st_blksize;
387 unsigned long long st_blocks;
388 unsigned st_atime_;
389 unsigned st_atime_nsec_;
390 unsigned st_mtime_;
391 unsigned st_mtime_nsec_;
392 unsigned st_ctime_;
393 unsigned st_ctime_nsec_;
394 unsigned long long st_ino;
395};
396#endif
397
Bryan Chan3f6478a2016-06-14 08:38:17 -0400398/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000399#if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
400struct kernel_stat {
401 /* The kernel headers suggest that st_dev and st_rdev should be 32bit
402 * quantities encoding 12bit major and 20bit minor numbers in an interleaved
403 * format. In reality, we do not see useful data in the top bits. So,
404 * we'll leave the padding in here, until we find a better solution.
405 */
406 unsigned short st_dev;
407 short pad1;
408 unsigned st_ino;
409 unsigned short st_mode;
410 unsigned short st_nlink;
411 unsigned short st_uid;
412 unsigned short st_gid;
413 unsigned short st_rdev;
414 short pad2;
415 unsigned st_size;
416 unsigned st_blksize;
417 unsigned st_blocks;
418 unsigned st_atime_;
419 unsigned st_atime_nsec_;
420 unsigned st_mtime_;
421 unsigned st_mtime_nsec_;
422 unsigned st_ctime_;
423 unsigned st_ctime_nsec_;
424 unsigned __unused4;
425 unsigned __unused5;
426};
427#elif defined(__x86_64__)
428struct kernel_stat {
vapier@chromium.org2273e812013-04-01 17:52:44 +0000429 uint64_t st_dev;
430 uint64_t st_ino;
431 uint64_t st_nlink;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000432 unsigned st_mode;
433 unsigned st_uid;
434 unsigned st_gid;
435 unsigned __pad0;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000436 uint64_t st_rdev;
437 int64_t st_size;
438 int64_t st_blksize;
439 int64_t st_blocks;
440 uint64_t st_atime_;
441 uint64_t st_atime_nsec_;
442 uint64_t st_mtime_;
443 uint64_t st_mtime_nsec_;
444 uint64_t st_ctime_;
445 uint64_t st_ctime_nsec_;
anton@chromium.org43de0522014-04-04 11:20:46 +0000446 int64_t __unused4[3];
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000447};
448#elif defined(__PPC__)
449struct kernel_stat {
450 unsigned st_dev;
451 unsigned long st_ino; // ino_t
452 unsigned long st_mode; // mode_t
453 unsigned short st_nlink; // nlink_t
454 unsigned st_uid; // uid_t
455 unsigned st_gid; // gid_t
456 unsigned st_rdev;
457 long st_size; // off_t
458 unsigned long st_blksize;
459 unsigned long st_blocks;
460 unsigned long st_atime_;
461 unsigned long st_atime_nsec_;
462 unsigned long st_mtime_;
463 unsigned long st_mtime_nsec_;
464 unsigned long st_ctime_;
465 unsigned long st_ctime_nsec_;
466 unsigned long __unused4;
467 unsigned long __unused5;
468};
469#elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
470struct kernel_stat {
471 unsigned st_dev;
472 int st_pad1[3];
473 unsigned st_ino;
474 unsigned st_mode;
475 unsigned st_nlink;
476 unsigned st_uid;
477 unsigned st_gid;
478 unsigned st_rdev;
479 int st_pad2[2];
480 long st_size;
481 int st_pad3;
482 long st_atime_;
483 long st_atime_nsec_;
484 long st_mtime_;
485 long st_mtime_nsec_;
486 long st_ctime_;
487 long st_ctime_nsec_;
488 int st_blksize;
489 int st_blocks;
490 int st_pad4[14];
491};
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000492#elif defined(__aarch64__)
493struct kernel_stat {
494 unsigned long st_dev;
495 unsigned long st_ino;
496 unsigned int st_mode;
497 unsigned int st_nlink;
498 unsigned int st_uid;
499 unsigned int st_gid;
500 unsigned long st_rdev;
501 unsigned long __pad1;
502 long st_size;
503 int st_blksize;
504 int __pad2;
505 long st_blocks;
506 long st_atime_;
507 unsigned long st_atime_nsec_;
508 long st_mtime_;
509 unsigned long st_mtime_nsec_;
510 long st_ctime_;
511 unsigned long st_ctime_nsec_;
512 unsigned int __unused4;
513 unsigned int __unused5;
514};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400515#elif defined(__s390x__)
516struct kernel_stat {
517 unsigned long st_dev;
518 unsigned long st_ino;
519 unsigned long st_nlink;
520 unsigned int st_mode;
521 unsigned int st_uid;
522 unsigned int st_gid;
523 unsigned int __pad1;
524 unsigned long st_rdev;
525 unsigned long st_size;
526 unsigned long st_atime_;
527 unsigned long st_atime_nsec_;
528 unsigned long st_mtime_;
529 unsigned long st_mtime_nsec_;
530 unsigned long st_ctime_;
531 unsigned long st_ctime_nsec_;
532 unsigned long st_blksize;
533 long st_blocks;
534 unsigned long __unused[3];
535};
536#elif defined(__s390__)
537struct kernel_stat {
538 unsigned short st_dev;
539 unsigned short __pad1;
540 unsigned long st_ino;
541 unsigned short st_mode;
542 unsigned short st_nlink;
543 unsigned short st_uid;
544 unsigned short st_gid;
545 unsigned short st_rdev;
546 unsigned short __pad2;
547 unsigned long st_size;
548 unsigned long st_blksize;
549 unsigned long st_blocks;
550 unsigned long st_atime_;
551 unsigned long st_atime_nsec_;
552 unsigned long st_mtime_;
553 unsigned long st_mtime_nsec_;
554 unsigned long st_ctime_;
555 unsigned long st_ctime_nsec_;
556 unsigned long __unused4;
557 unsigned long __unused5;
558};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000559#endif
560
Bryan Chan3f6478a2016-06-14 08:38:17 -0400561/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000562#ifdef __mips__
563#if _MIPS_SIM != _MIPS_SIM_ABI64
564struct kernel_statfs64 {
565 unsigned long f_type;
566 unsigned long f_bsize;
567 unsigned long f_frsize;
568 unsigned long __pad;
569 unsigned long long f_blocks;
570 unsigned long long f_bfree;
571 unsigned long long f_files;
572 unsigned long long f_ffree;
573 unsigned long long f_bavail;
574 struct { int val[2]; } f_fsid;
575 unsigned long f_namelen;
576 unsigned long f_spare[6];
577};
578#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -0400579#elif defined(__s390__)
580/* See also arch/s390/include/asm/compat.h */
581struct kernel_statfs64 {
582 unsigned int f_type;
583 unsigned int f_bsize;
584 unsigned long long f_blocks;
585 unsigned long long f_bfree;
586 unsigned long long f_bavail;
587 unsigned long long f_files;
588 unsigned long long f_ffree;
589 struct { int val[2]; } f_fsid;
590 unsigned int f_namelen;
591 unsigned int f_frsize;
592 unsigned int f_flags;
593 unsigned int f_spare[4];
594};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000595#elif !defined(__x86_64__)
596struct kernel_statfs64 {
597 unsigned long f_type;
598 unsigned long f_bsize;
599 unsigned long long f_blocks;
600 unsigned long long f_bfree;
601 unsigned long long f_bavail;
602 unsigned long long f_files;
603 unsigned long long f_ffree;
604 struct { int val[2]; } f_fsid;
605 unsigned long f_namelen;
606 unsigned long f_frsize;
607 unsigned long f_spare[5];
608};
609#endif
610
Bryan Chan3f6478a2016-06-14 08:38:17 -0400611/* include/asm-{arm,i386,mips,x86_64,ppc,generic,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000612#ifdef __mips__
613struct kernel_statfs {
614 long f_type;
615 long f_bsize;
616 long f_frsize;
617 long f_blocks;
618 long f_bfree;
619 long f_files;
620 long f_ffree;
621 long f_bavail;
622 struct { int val[2]; } f_fsid;
623 long f_namelen;
624 long f_spare[6];
625};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000626#elif defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000627struct kernel_statfs {
628 /* x86_64 actually defines all these fields as signed, whereas all other */
629 /* platforms define them as unsigned. Leaving them at unsigned should not */
vapier@chromium.org2273e812013-04-01 17:52:44 +0000630 /* cause any problems. Make sure these are 64-bit even on x32. */
631 uint64_t f_type;
632 uint64_t f_bsize;
633 uint64_t f_blocks;
634 uint64_t f_bfree;
635 uint64_t f_bavail;
636 uint64_t f_files;
637 uint64_t f_ffree;
638 struct { int val[2]; } f_fsid;
639 uint64_t f_namelen;
640 uint64_t f_frsize;
641 uint64_t f_spare[5];
642};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400643#elif defined(__s390__)
644struct kernel_statfs {
645 unsigned int f_type;
646 unsigned int f_bsize;
647 unsigned long f_blocks;
648 unsigned long f_bfree;
649 unsigned long f_bavail;
650 unsigned long f_files;
651 unsigned long f_ffree;
652 struct { int val[2]; } f_fsid;
653 unsigned int f_namelen;
654 unsigned int f_frsize;
655 unsigned int f_flags;
656 unsigned int f_spare[4];
657};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000658#else
659struct kernel_statfs {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000660 unsigned long f_type;
661 unsigned long f_bsize;
662 unsigned long f_blocks;
663 unsigned long f_bfree;
664 unsigned long f_bavail;
665 unsigned long f_files;
666 unsigned long f_ffree;
667 struct { int val[2]; } f_fsid;
668 unsigned long f_namelen;
669 unsigned long f_frsize;
670 unsigned long f_spare[5];
671};
672#endif
673
674
675/* Definitions missing from the standard header files */
676#ifndef O_DIRECTORY
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000677#if defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || defined(__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000678#define O_DIRECTORY 0040000
679#else
680#define O_DIRECTORY 0200000
681#endif
682#endif
683#ifndef NT_PRXFPREG
684#define NT_PRXFPREG 0x46e62b7f
685#endif
686#ifndef PTRACE_GETFPXREGS
687#define PTRACE_GETFPXREGS ((enum __ptrace_request)18)
688#endif
689#ifndef PR_GET_DUMPABLE
690#define PR_GET_DUMPABLE 3
691#endif
692#ifndef PR_SET_DUMPABLE
693#define PR_SET_DUMPABLE 4
694#endif
695#ifndef PR_GET_SECCOMP
696#define PR_GET_SECCOMP 21
697#endif
698#ifndef PR_SET_SECCOMP
699#define PR_SET_SECCOMP 22
700#endif
701#ifndef AT_FDCWD
702#define AT_FDCWD (-100)
703#endif
704#ifndef AT_SYMLINK_NOFOLLOW
705#define AT_SYMLINK_NOFOLLOW 0x100
706#endif
707#ifndef AT_REMOVEDIR
708#define AT_REMOVEDIR 0x200
709#endif
710#ifndef MREMAP_FIXED
711#define MREMAP_FIXED 2
712#endif
713#ifndef SA_RESTORER
714#define SA_RESTORER 0x04000000
715#endif
716#ifndef CPUCLOCK_PROF
717#define CPUCLOCK_PROF 0
718#endif
719#ifndef CPUCLOCK_VIRT
720#define CPUCLOCK_VIRT 1
721#endif
722#ifndef CPUCLOCK_SCHED
723#define CPUCLOCK_SCHED 2
724#endif
725#ifndef CPUCLOCK_PERTHREAD_MASK
726#define CPUCLOCK_PERTHREAD_MASK 4
727#endif
728#ifndef MAKE_PROCESS_CPUCLOCK
729#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400730 ((int)(~(unsigned)(pid) << 3) | (int)(clock))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000731#endif
732#ifndef MAKE_THREAD_CPUCLOCK
733#define MAKE_THREAD_CPUCLOCK(tid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400734 ((int)(~(unsigned)(tid) << 3) | \
735 (int)((clock) | CPUCLOCK_PERTHREAD_MASK))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000736#endif
737
738#ifndef FUTEX_WAIT
739#define FUTEX_WAIT 0
740#endif
741#ifndef FUTEX_WAKE
742#define FUTEX_WAKE 1
743#endif
744#ifndef FUTEX_FD
745#define FUTEX_FD 2
746#endif
747#ifndef FUTEX_REQUEUE
748#define FUTEX_REQUEUE 3
749#endif
750#ifndef FUTEX_CMP_REQUEUE
751#define FUTEX_CMP_REQUEUE 4
752#endif
753#ifndef FUTEX_WAKE_OP
754#define FUTEX_WAKE_OP 5
755#endif
756#ifndef FUTEX_LOCK_PI
757#define FUTEX_LOCK_PI 6
758#endif
759#ifndef FUTEX_UNLOCK_PI
760#define FUTEX_UNLOCK_PI 7
761#endif
762#ifndef FUTEX_TRYLOCK_PI
763#define FUTEX_TRYLOCK_PI 8
764#endif
765#ifndef FUTEX_PRIVATE_FLAG
766#define FUTEX_PRIVATE_FLAG 128
767#endif
768#ifndef FUTEX_CMD_MASK
769#define FUTEX_CMD_MASK ~FUTEX_PRIVATE_FLAG
770#endif
771#ifndef FUTEX_WAIT_PRIVATE
772#define FUTEX_WAIT_PRIVATE (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
773#endif
774#ifndef FUTEX_WAKE_PRIVATE
775#define FUTEX_WAKE_PRIVATE (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
776#endif
777#ifndef FUTEX_REQUEUE_PRIVATE
778#define FUTEX_REQUEUE_PRIVATE (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
779#endif
780#ifndef FUTEX_CMP_REQUEUE_PRIVATE
781#define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
782#endif
783#ifndef FUTEX_WAKE_OP_PRIVATE
784#define FUTEX_WAKE_OP_PRIVATE (FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
785#endif
786#ifndef FUTEX_LOCK_PI_PRIVATE
787#define FUTEX_LOCK_PI_PRIVATE (FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
788#endif
789#ifndef FUTEX_UNLOCK_PI_PRIVATE
790#define FUTEX_UNLOCK_PI_PRIVATE (FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
791#endif
792#ifndef FUTEX_TRYLOCK_PI_PRIVATE
793#define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
794#endif
795
796
797#if defined(__x86_64__)
798#ifndef ARCH_SET_GS
799#define ARCH_SET_GS 0x1001
800#endif
801#ifndef ARCH_GET_GS
802#define ARCH_GET_GS 0x1004
803#endif
804#endif
805
806#if defined(__i386__)
807#ifndef __NR_quotactl
808#define __NR_quotactl 131
809#endif
810#ifndef __NR_setresuid
811#define __NR_setresuid 164
812#define __NR_getresuid 165
813#define __NR_setresgid 170
814#define __NR_getresgid 171
815#endif
816#ifndef __NR_rt_sigaction
817#define __NR_rt_sigreturn 173
818#define __NR_rt_sigaction 174
819#define __NR_rt_sigprocmask 175
820#define __NR_rt_sigpending 176
821#define __NR_rt_sigsuspend 179
822#endif
823#ifndef __NR_pread64
824#define __NR_pread64 180
825#endif
826#ifndef __NR_pwrite64
827#define __NR_pwrite64 181
828#endif
829#ifndef __NR_ugetrlimit
830#define __NR_ugetrlimit 191
831#endif
832#ifndef __NR_stat64
833#define __NR_stat64 195
834#endif
835#ifndef __NR_fstat64
836#define __NR_fstat64 197
837#endif
838#ifndef __NR_setresuid32
839#define __NR_setresuid32 208
840#define __NR_getresuid32 209
841#define __NR_setresgid32 210
842#define __NR_getresgid32 211
843#endif
844#ifndef __NR_setfsuid32
845#define __NR_setfsuid32 215
846#define __NR_setfsgid32 216
847#endif
848#ifndef __NR_getdents64
849#define __NR_getdents64 220
850#endif
851#ifndef __NR_gettid
852#define __NR_gettid 224
853#endif
854#ifndef __NR_readahead
855#define __NR_readahead 225
856#endif
857#ifndef __NR_setxattr
858#define __NR_setxattr 226
859#endif
860#ifndef __NR_lsetxattr
861#define __NR_lsetxattr 227
862#endif
863#ifndef __NR_getxattr
864#define __NR_getxattr 229
865#endif
866#ifndef __NR_lgetxattr
867#define __NR_lgetxattr 230
868#endif
869#ifndef __NR_listxattr
870#define __NR_listxattr 232
871#endif
872#ifndef __NR_llistxattr
873#define __NR_llistxattr 233
874#endif
875#ifndef __NR_tkill
876#define __NR_tkill 238
877#endif
878#ifndef __NR_futex
879#define __NR_futex 240
880#endif
881#ifndef __NR_sched_setaffinity
882#define __NR_sched_setaffinity 241
883#define __NR_sched_getaffinity 242
884#endif
885#ifndef __NR_set_tid_address
886#define __NR_set_tid_address 258
887#endif
888#ifndef __NR_clock_gettime
889#define __NR_clock_gettime 265
890#endif
891#ifndef __NR_clock_getres
892#define __NR_clock_getres 266
893#endif
894#ifndef __NR_statfs64
895#define __NR_statfs64 268
896#endif
897#ifndef __NR_fstatfs64
898#define __NR_fstatfs64 269
899#endif
900#ifndef __NR_fadvise64_64
901#define __NR_fadvise64_64 272
902#endif
903#ifndef __NR_ioprio_set
904#define __NR_ioprio_set 289
905#endif
906#ifndef __NR_ioprio_get
907#define __NR_ioprio_get 290
908#endif
909#ifndef __NR_openat
910#define __NR_openat 295
911#endif
912#ifndef __NR_fstatat64
913#define __NR_fstatat64 300
914#endif
915#ifndef __NR_unlinkat
916#define __NR_unlinkat 301
917#endif
918#ifndef __NR_move_pages
919#define __NR_move_pages 317
920#endif
921#ifndef __NR_getcpu
922#define __NR_getcpu 318
923#endif
924#ifndef __NR_fallocate
925#define __NR_fallocate 324
926#endif
927/* End of i386 definitions */
928#elif defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
929#ifndef __NR_setresuid
930#define __NR_setresuid (__NR_SYSCALL_BASE + 164)
931#define __NR_getresuid (__NR_SYSCALL_BASE + 165)
932#define __NR_setresgid (__NR_SYSCALL_BASE + 170)
933#define __NR_getresgid (__NR_SYSCALL_BASE + 171)
934#endif
935#ifndef __NR_rt_sigaction
936#define __NR_rt_sigreturn (__NR_SYSCALL_BASE + 173)
937#define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
938#define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
939#define __NR_rt_sigpending (__NR_SYSCALL_BASE + 176)
940#define __NR_rt_sigsuspend (__NR_SYSCALL_BASE + 179)
941#endif
942#ifndef __NR_pread64
943#define __NR_pread64 (__NR_SYSCALL_BASE + 180)
944#endif
945#ifndef __NR_pwrite64
946#define __NR_pwrite64 (__NR_SYSCALL_BASE + 181)
947#endif
948#ifndef __NR_ugetrlimit
949#define __NR_ugetrlimit (__NR_SYSCALL_BASE + 191)
950#endif
951#ifndef __NR_stat64
952#define __NR_stat64 (__NR_SYSCALL_BASE + 195)
953#endif
954#ifndef __NR_fstat64
955#define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
956#endif
957#ifndef __NR_setresuid32
958#define __NR_setresuid32 (__NR_SYSCALL_BASE + 208)
959#define __NR_getresuid32 (__NR_SYSCALL_BASE + 209)
960#define __NR_setresgid32 (__NR_SYSCALL_BASE + 210)
961#define __NR_getresgid32 (__NR_SYSCALL_BASE + 211)
962#endif
963#ifndef __NR_setfsuid32
964#define __NR_setfsuid32 (__NR_SYSCALL_BASE + 215)
965#define __NR_setfsgid32 (__NR_SYSCALL_BASE + 216)
966#endif
967#ifndef __NR_getdents64
968#define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
969#endif
970#ifndef __NR_gettid
971#define __NR_gettid (__NR_SYSCALL_BASE + 224)
972#endif
973#ifndef __NR_readahead
974#define __NR_readahead (__NR_SYSCALL_BASE + 225)
975#endif
976#ifndef __NR_setxattr
977#define __NR_setxattr (__NR_SYSCALL_BASE + 226)
978#endif
979#ifndef __NR_lsetxattr
980#define __NR_lsetxattr (__NR_SYSCALL_BASE + 227)
981#endif
982#ifndef __NR_getxattr
983#define __NR_getxattr (__NR_SYSCALL_BASE + 229)
984#endif
985#ifndef __NR_lgetxattr
986#define __NR_lgetxattr (__NR_SYSCALL_BASE + 230)
987#endif
988#ifndef __NR_listxattr
989#define __NR_listxattr (__NR_SYSCALL_BASE + 232)
990#endif
991#ifndef __NR_llistxattr
992#define __NR_llistxattr (__NR_SYSCALL_BASE + 233)
993#endif
994#ifndef __NR_tkill
995#define __NR_tkill (__NR_SYSCALL_BASE + 238)
996#endif
997#ifndef __NR_futex
998#define __NR_futex (__NR_SYSCALL_BASE + 240)
999#endif
1000#ifndef __NR_sched_setaffinity
1001#define __NR_sched_setaffinity (__NR_SYSCALL_BASE + 241)
1002#define __NR_sched_getaffinity (__NR_SYSCALL_BASE + 242)
1003#endif
1004#ifndef __NR_set_tid_address
1005#define __NR_set_tid_address (__NR_SYSCALL_BASE + 256)
1006#endif
1007#ifndef __NR_clock_gettime
1008#define __NR_clock_gettime (__NR_SYSCALL_BASE + 263)
1009#endif
1010#ifndef __NR_clock_getres
1011#define __NR_clock_getres (__NR_SYSCALL_BASE + 264)
1012#endif
1013#ifndef __NR_statfs64
1014#define __NR_statfs64 (__NR_SYSCALL_BASE + 266)
1015#endif
1016#ifndef __NR_fstatfs64
1017#define __NR_fstatfs64 (__NR_SYSCALL_BASE + 267)
1018#endif
1019#ifndef __NR_ioprio_set
1020#define __NR_ioprio_set (__NR_SYSCALL_BASE + 314)
1021#endif
1022#ifndef __NR_ioprio_get
1023#define __NR_ioprio_get (__NR_SYSCALL_BASE + 315)
1024#endif
1025#ifndef __NR_move_pages
1026#define __NR_move_pages (__NR_SYSCALL_BASE + 344)
1027#endif
1028#ifndef __NR_getcpu
1029#define __NR_getcpu (__NR_SYSCALL_BASE + 345)
1030#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04001031/* End of ARM 3/EABI definitions */
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001032#elif defined(__aarch64__)
1033#ifndef __NR_setxattr
1034#define __NR_setxattr 5
1035#endif
1036#ifndef __NR_lsetxattr
1037#define __NR_lsetxattr 6
1038#endif
1039#ifndef __NR_getxattr
1040#define __NR_getxattr 8
1041#endif
1042#ifndef __NR_lgetxattr
1043#define __NR_lgetxattr 9
1044#endif
1045#ifndef __NR_listxattr
1046#define __NR_listxattr 11
1047#endif
1048#ifndef __NR_llistxattr
1049#define __NR_llistxattr 12
1050#endif
1051#ifndef __NR_ioprio_set
1052#define __NR_ioprio_set 30
1053#endif
1054#ifndef __NR_ioprio_get
1055#define __NR_ioprio_get 31
1056#endif
1057#ifndef __NR_unlinkat
1058#define __NR_unlinkat 35
1059#endif
1060#ifndef __NR_fallocate
1061#define __NR_fallocate 47
1062#endif
1063#ifndef __NR_openat
1064#define __NR_openat 56
1065#endif
1066#ifndef __NR_quotactl
1067#define __NR_quotactl 60
1068#endif
1069#ifndef __NR_getdents64
1070#define __NR_getdents64 61
1071#endif
1072#ifndef __NR_getdents
1073#define __NR_getdents __NR_getdents64
1074#endif
1075#ifndef __NR_pread64
1076#define __NR_pread64 67
1077#endif
1078#ifndef __NR_pwrite64
1079#define __NR_pwrite64 68
1080#endif
1081#ifndef __NR_ppoll
1082#define __NR_ppoll 73
1083#endif
1084#ifndef __NR_readlinkat
1085#define __NR_readlinkat 78
1086#endif
1087#ifndef __NR_newfstatat
1088#define __NR_newfstatat 79
1089#endif
1090#ifndef __NR_set_tid_address
1091#define __NR_set_tid_address 96
1092#endif
1093#ifndef __NR_futex
1094#define __NR_futex 98
1095#endif
1096#ifndef __NR_clock_gettime
1097#define __NR_clock_gettime 113
1098#endif
1099#ifndef __NR_clock_getres
1100#define __NR_clock_getres 114
1101#endif
1102#ifndef __NR_sched_setaffinity
1103#define __NR_sched_setaffinity 122
1104#define __NR_sched_getaffinity 123
1105#endif
1106#ifndef __NR_tkill
1107#define __NR_tkill 130
1108#endif
1109#ifndef __NR_setresuid
1110#define __NR_setresuid 147
1111#define __NR_getresuid 148
1112#define __NR_setresgid 149
1113#define __NR_getresgid 150
1114#endif
1115#ifndef __NR_gettid
1116#define __NR_gettid 178
1117#endif
1118#ifndef __NR_readahead
1119#define __NR_readahead 213
1120#endif
1121#ifndef __NR_fadvise64
1122#define __NR_fadvise64 223
1123#endif
1124#ifndef __NR_move_pages
1125#define __NR_move_pages 239
1126#endif
1127/* End of aarch64 definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001128#elif defined(__x86_64__)
1129#ifndef __NR_pread64
1130#define __NR_pread64 17
1131#endif
1132#ifndef __NR_pwrite64
1133#define __NR_pwrite64 18
1134#endif
1135#ifndef __NR_setresuid
1136#define __NR_setresuid 117
1137#define __NR_getresuid 118
1138#define __NR_setresgid 119
1139#define __NR_getresgid 120
1140#endif
1141#ifndef __NR_quotactl
1142#define __NR_quotactl 179
1143#endif
1144#ifndef __NR_gettid
1145#define __NR_gettid 186
1146#endif
1147#ifndef __NR_readahead
1148#define __NR_readahead 187
1149#endif
1150#ifndef __NR_setxattr
1151#define __NR_setxattr 188
1152#endif
1153#ifndef __NR_lsetxattr
1154#define __NR_lsetxattr 189
1155#endif
1156#ifndef __NR_getxattr
1157#define __NR_getxattr 191
1158#endif
1159#ifndef __NR_lgetxattr
1160#define __NR_lgetxattr 192
1161#endif
1162#ifndef __NR_listxattr
1163#define __NR_listxattr 194
1164#endif
1165#ifndef __NR_llistxattr
1166#define __NR_llistxattr 195
1167#endif
1168#ifndef __NR_tkill
1169#define __NR_tkill 200
1170#endif
1171#ifndef __NR_futex
1172#define __NR_futex 202
1173#endif
1174#ifndef __NR_sched_setaffinity
1175#define __NR_sched_setaffinity 203
1176#define __NR_sched_getaffinity 204
1177#endif
1178#ifndef __NR_getdents64
1179#define __NR_getdents64 217
1180#endif
1181#ifndef __NR_set_tid_address
1182#define __NR_set_tid_address 218
1183#endif
1184#ifndef __NR_fadvise64
1185#define __NR_fadvise64 221
1186#endif
1187#ifndef __NR_clock_gettime
1188#define __NR_clock_gettime 228
1189#endif
1190#ifndef __NR_clock_getres
1191#define __NR_clock_getres 229
1192#endif
1193#ifndef __NR_ioprio_set
1194#define __NR_ioprio_set 251
1195#endif
1196#ifndef __NR_ioprio_get
1197#define __NR_ioprio_get 252
1198#endif
1199#ifndef __NR_openat
1200#define __NR_openat 257
1201#endif
1202#ifndef __NR_newfstatat
1203#define __NR_newfstatat 262
1204#endif
1205#ifndef __NR_unlinkat
1206#define __NR_unlinkat 263
1207#endif
1208#ifndef __NR_move_pages
1209#define __NR_move_pages 279
1210#endif
1211#ifndef __NR_fallocate
1212#define __NR_fallocate 285
1213#endif
1214/* End of x86-64 definitions */
1215#elif defined(__mips__)
1216#if _MIPS_SIM == _MIPS_SIM_ABI32
1217#ifndef __NR_setresuid
1218#define __NR_setresuid (__NR_Linux + 185)
1219#define __NR_getresuid (__NR_Linux + 186)
1220#define __NR_setresgid (__NR_Linux + 190)
1221#define __NR_getresgid (__NR_Linux + 191)
1222#endif
1223#ifndef __NR_rt_sigaction
1224#define __NR_rt_sigreturn (__NR_Linux + 193)
1225#define __NR_rt_sigaction (__NR_Linux + 194)
1226#define __NR_rt_sigprocmask (__NR_Linux + 195)
1227#define __NR_rt_sigpending (__NR_Linux + 196)
1228#define __NR_rt_sigsuspend (__NR_Linux + 199)
1229#endif
1230#ifndef __NR_pread64
1231#define __NR_pread64 (__NR_Linux + 200)
1232#endif
1233#ifndef __NR_pwrite64
1234#define __NR_pwrite64 (__NR_Linux + 201)
1235#endif
1236#ifndef __NR_stat64
1237#define __NR_stat64 (__NR_Linux + 213)
1238#endif
1239#ifndef __NR_fstat64
1240#define __NR_fstat64 (__NR_Linux + 215)
1241#endif
1242#ifndef __NR_getdents64
1243#define __NR_getdents64 (__NR_Linux + 219)
1244#endif
1245#ifndef __NR_gettid
1246#define __NR_gettid (__NR_Linux + 222)
1247#endif
1248#ifndef __NR_readahead
1249#define __NR_readahead (__NR_Linux + 223)
1250#endif
1251#ifndef __NR_setxattr
1252#define __NR_setxattr (__NR_Linux + 224)
1253#endif
1254#ifndef __NR_lsetxattr
1255#define __NR_lsetxattr (__NR_Linux + 225)
1256#endif
1257#ifndef __NR_getxattr
1258#define __NR_getxattr (__NR_Linux + 227)
1259#endif
1260#ifndef __NR_lgetxattr
1261#define __NR_lgetxattr (__NR_Linux + 228)
1262#endif
1263#ifndef __NR_listxattr
1264#define __NR_listxattr (__NR_Linux + 230)
1265#endif
1266#ifndef __NR_llistxattr
1267#define __NR_llistxattr (__NR_Linux + 231)
1268#endif
1269#ifndef __NR_tkill
1270#define __NR_tkill (__NR_Linux + 236)
1271#endif
1272#ifndef __NR_futex
1273#define __NR_futex (__NR_Linux + 238)
1274#endif
1275#ifndef __NR_sched_setaffinity
1276#define __NR_sched_setaffinity (__NR_Linux + 239)
1277#define __NR_sched_getaffinity (__NR_Linux + 240)
1278#endif
1279#ifndef __NR_set_tid_address
1280#define __NR_set_tid_address (__NR_Linux + 252)
1281#endif
1282#ifndef __NR_statfs64
1283#define __NR_statfs64 (__NR_Linux + 255)
1284#endif
1285#ifndef __NR_fstatfs64
1286#define __NR_fstatfs64 (__NR_Linux + 256)
1287#endif
1288#ifndef __NR_clock_gettime
1289#define __NR_clock_gettime (__NR_Linux + 263)
1290#endif
1291#ifndef __NR_clock_getres
1292#define __NR_clock_getres (__NR_Linux + 264)
1293#endif
1294#ifndef __NR_openat
1295#define __NR_openat (__NR_Linux + 288)
1296#endif
1297#ifndef __NR_fstatat
1298#define __NR_fstatat (__NR_Linux + 293)
1299#endif
1300#ifndef __NR_unlinkat
1301#define __NR_unlinkat (__NR_Linux + 294)
1302#endif
1303#ifndef __NR_move_pages
1304#define __NR_move_pages (__NR_Linux + 308)
1305#endif
1306#ifndef __NR_getcpu
1307#define __NR_getcpu (__NR_Linux + 312)
1308#endif
1309#ifndef __NR_ioprio_set
1310#define __NR_ioprio_set (__NR_Linux + 314)
1311#endif
1312#ifndef __NR_ioprio_get
1313#define __NR_ioprio_get (__NR_Linux + 315)
1314#endif
1315/* End of MIPS (old 32bit API) definitions */
1316#elif _MIPS_SIM == _MIPS_SIM_ABI64
1317#ifndef __NR_pread64
1318#define __NR_pread64 (__NR_Linux + 16)
1319#endif
1320#ifndef __NR_pwrite64
1321#define __NR_pwrite64 (__NR_Linux + 17)
1322#endif
1323#ifndef __NR_setresuid
1324#define __NR_setresuid (__NR_Linux + 115)
1325#define __NR_getresuid (__NR_Linux + 116)
1326#define __NR_setresgid (__NR_Linux + 117)
1327#define __NR_getresgid (__NR_Linux + 118)
1328#endif
1329#ifndef __NR_gettid
1330#define __NR_gettid (__NR_Linux + 178)
1331#endif
1332#ifndef __NR_readahead
1333#define __NR_readahead (__NR_Linux + 179)
1334#endif
1335#ifndef __NR_setxattr
1336#define __NR_setxattr (__NR_Linux + 180)
1337#endif
1338#ifndef __NR_lsetxattr
1339#define __NR_lsetxattr (__NR_Linux + 181)
1340#endif
1341#ifndef __NR_getxattr
1342#define __NR_getxattr (__NR_Linux + 183)
1343#endif
1344#ifndef __NR_lgetxattr
1345#define __NR_lgetxattr (__NR_Linux + 184)
1346#endif
1347#ifndef __NR_listxattr
1348#define __NR_listxattr (__NR_Linux + 186)
1349#endif
1350#ifndef __NR_llistxattr
1351#define __NR_llistxattr (__NR_Linux + 187)
1352#endif
1353#ifndef __NR_tkill
1354#define __NR_tkill (__NR_Linux + 192)
1355#endif
1356#ifndef __NR_futex
1357#define __NR_futex (__NR_Linux + 194)
1358#endif
1359#ifndef __NR_sched_setaffinity
1360#define __NR_sched_setaffinity (__NR_Linux + 195)
1361#define __NR_sched_getaffinity (__NR_Linux + 196)
1362#endif
1363#ifndef __NR_set_tid_address
1364#define __NR_set_tid_address (__NR_Linux + 212)
1365#endif
1366#ifndef __NR_clock_gettime
1367#define __NR_clock_gettime (__NR_Linux + 222)
1368#endif
1369#ifndef __NR_clock_getres
1370#define __NR_clock_getres (__NR_Linux + 223)
1371#endif
1372#ifndef __NR_openat
1373#define __NR_openat (__NR_Linux + 247)
1374#endif
1375#ifndef __NR_fstatat
1376#define __NR_fstatat (__NR_Linux + 252)
1377#endif
1378#ifndef __NR_unlinkat
1379#define __NR_unlinkat (__NR_Linux + 253)
1380#endif
1381#ifndef __NR_move_pages
1382#define __NR_move_pages (__NR_Linux + 267)
1383#endif
1384#ifndef __NR_getcpu
1385#define __NR_getcpu (__NR_Linux + 271)
1386#endif
1387#ifndef __NR_ioprio_set
1388#define __NR_ioprio_set (__NR_Linux + 273)
1389#endif
1390#ifndef __NR_ioprio_get
1391#define __NR_ioprio_get (__NR_Linux + 274)
1392#endif
1393/* End of MIPS (64bit API) definitions */
1394#else
1395#ifndef __NR_setresuid
1396#define __NR_setresuid (__NR_Linux + 115)
1397#define __NR_getresuid (__NR_Linux + 116)
1398#define __NR_setresgid (__NR_Linux + 117)
1399#define __NR_getresgid (__NR_Linux + 118)
1400#endif
1401#ifndef __NR_gettid
1402#define __NR_gettid (__NR_Linux + 178)
1403#endif
1404#ifndef __NR_readahead
1405#define __NR_readahead (__NR_Linux + 179)
1406#endif
1407#ifndef __NR_setxattr
1408#define __NR_setxattr (__NR_Linux + 180)
1409#endif
1410#ifndef __NR_lsetxattr
1411#define __NR_lsetxattr (__NR_Linux + 181)
1412#endif
1413#ifndef __NR_getxattr
1414#define __NR_getxattr (__NR_Linux + 183)
1415#endif
1416#ifndef __NR_lgetxattr
1417#define __NR_lgetxattr (__NR_Linux + 184)
1418#endif
1419#ifndef __NR_listxattr
1420#define __NR_listxattr (__NR_Linux + 186)
1421#endif
1422#ifndef __NR_llistxattr
1423#define __NR_llistxattr (__NR_Linux + 187)
1424#endif
1425#ifndef __NR_tkill
1426#define __NR_tkill (__NR_Linux + 192)
1427#endif
1428#ifndef __NR_futex
1429#define __NR_futex (__NR_Linux + 194)
1430#endif
1431#ifndef __NR_sched_setaffinity
1432#define __NR_sched_setaffinity (__NR_Linux + 195)
1433#define __NR_sched_getaffinity (__NR_Linux + 196)
1434#endif
1435#ifndef __NR_set_tid_address
1436#define __NR_set_tid_address (__NR_Linux + 213)
1437#endif
1438#ifndef __NR_statfs64
1439#define __NR_statfs64 (__NR_Linux + 217)
1440#endif
1441#ifndef __NR_fstatfs64
1442#define __NR_fstatfs64 (__NR_Linux + 218)
1443#endif
1444#ifndef __NR_clock_gettime
1445#define __NR_clock_gettime (__NR_Linux + 226)
1446#endif
1447#ifndef __NR_clock_getres
1448#define __NR_clock_getres (__NR_Linux + 227)
1449#endif
1450#ifndef __NR_openat
1451#define __NR_openat (__NR_Linux + 251)
1452#endif
1453#ifndef __NR_fstatat
1454#define __NR_fstatat (__NR_Linux + 256)
1455#endif
1456#ifndef __NR_unlinkat
1457#define __NR_unlinkat (__NR_Linux + 257)
1458#endif
1459#ifndef __NR_move_pages
1460#define __NR_move_pages (__NR_Linux + 271)
1461#endif
1462#ifndef __NR_getcpu
1463#define __NR_getcpu (__NR_Linux + 275)
1464#endif
1465#ifndef __NR_ioprio_set
1466#define __NR_ioprio_set (__NR_Linux + 277)
1467#endif
1468#ifndef __NR_ioprio_get
1469#define __NR_ioprio_get (__NR_Linux + 278)
1470#endif
1471/* End of MIPS (new 32bit API) definitions */
1472#endif
1473/* End of MIPS definitions */
1474#elif defined(__PPC__)
1475#ifndef __NR_setfsuid
1476#define __NR_setfsuid 138
1477#define __NR_setfsgid 139
1478#endif
1479#ifndef __NR_setresuid
1480#define __NR_setresuid 164
1481#define __NR_getresuid 165
1482#define __NR_setresgid 169
1483#define __NR_getresgid 170
1484#endif
1485#ifndef __NR_rt_sigaction
1486#define __NR_rt_sigreturn 172
1487#define __NR_rt_sigaction 173
1488#define __NR_rt_sigprocmask 174
1489#define __NR_rt_sigpending 175
1490#define __NR_rt_sigsuspend 178
1491#endif
1492#ifndef __NR_pread64
1493#define __NR_pread64 179
1494#endif
1495#ifndef __NR_pwrite64
1496#define __NR_pwrite64 180
1497#endif
1498#ifndef __NR_ugetrlimit
1499#define __NR_ugetrlimit 190
1500#endif
1501#ifndef __NR_readahead
1502#define __NR_readahead 191
1503#endif
1504#ifndef __NR_stat64
1505#define __NR_stat64 195
1506#endif
1507#ifndef __NR_fstat64
1508#define __NR_fstat64 197
1509#endif
1510#ifndef __NR_getdents64
1511#define __NR_getdents64 202
1512#endif
1513#ifndef __NR_gettid
1514#define __NR_gettid 207
1515#endif
1516#ifndef __NR_tkill
1517#define __NR_tkill 208
1518#endif
1519#ifndef __NR_setxattr
1520#define __NR_setxattr 209
1521#endif
1522#ifndef __NR_lsetxattr
1523#define __NR_lsetxattr 210
1524#endif
1525#ifndef __NR_getxattr
1526#define __NR_getxattr 212
1527#endif
1528#ifndef __NR_lgetxattr
1529#define __NR_lgetxattr 213
1530#endif
1531#ifndef __NR_listxattr
1532#define __NR_listxattr 215
1533#endif
1534#ifndef __NR_llistxattr
1535#define __NR_llistxattr 216
1536#endif
1537#ifndef __NR_futex
1538#define __NR_futex 221
1539#endif
1540#ifndef __NR_sched_setaffinity
1541#define __NR_sched_setaffinity 222
1542#define __NR_sched_getaffinity 223
1543#endif
1544#ifndef __NR_set_tid_address
1545#define __NR_set_tid_address 232
1546#endif
1547#ifndef __NR_clock_gettime
1548#define __NR_clock_gettime 246
1549#endif
1550#ifndef __NR_clock_getres
1551#define __NR_clock_getres 247
1552#endif
1553#ifndef __NR_statfs64
1554#define __NR_statfs64 252
1555#endif
1556#ifndef __NR_fstatfs64
1557#define __NR_fstatfs64 253
1558#endif
1559#ifndef __NR_fadvise64_64
1560#define __NR_fadvise64_64 254
1561#endif
1562#ifndef __NR_ioprio_set
1563#define __NR_ioprio_set 273
1564#endif
1565#ifndef __NR_ioprio_get
1566#define __NR_ioprio_get 274
1567#endif
1568#ifndef __NR_openat
1569#define __NR_openat 286
1570#endif
1571#ifndef __NR_fstatat64
1572#define __NR_fstatat64 291
1573#endif
1574#ifndef __NR_unlinkat
1575#define __NR_unlinkat 292
1576#endif
1577#ifndef __NR_move_pages
1578#define __NR_move_pages 301
1579#endif
1580#ifndef __NR_getcpu
1581#define __NR_getcpu 302
1582#endif
1583/* End of powerpc defininitions */
Bryan Chan3f6478a2016-06-14 08:38:17 -04001584#elif defined(__s390__)
1585#ifndef __NR_quotactl
1586#define __NR_quotactl 131
1587#endif
1588#ifndef __NR_rt_sigreturn
1589#define __NR_rt_sigreturn 173
1590#endif
1591#ifndef __NR_rt_sigaction
1592#define __NR_rt_sigaction 174
1593#endif
1594#ifndef __NR_rt_sigprocmask
1595#define __NR_rt_sigprocmask 175
1596#endif
1597#ifndef __NR_rt_sigpending
1598#define __NR_rt_sigpending 176
1599#endif
1600#ifndef __NR_rt_sigsuspend
1601#define __NR_rt_sigsuspend 179
1602#endif
1603#ifndef __NR_pread64
1604#define __NR_pread64 180
1605#endif
1606#ifndef __NR_pwrite64
1607#define __NR_pwrite64 181
1608#endif
1609#ifndef __NR_getdents64
1610#define __NR_getdents64 220
1611#endif
1612#ifndef __NR_readahead
1613#define __NR_readahead 222
1614#endif
1615#ifndef __NR_setxattr
1616#define __NR_setxattr 224
1617#endif
1618#ifndef __NR_lsetxattr
1619#define __NR_lsetxattr 225
1620#endif
1621#ifndef __NR_getxattr
1622#define __NR_getxattr 227
1623#endif
1624#ifndef __NR_lgetxattr
1625#define __NR_lgetxattr 228
1626#endif
1627#ifndef __NR_listxattr
1628#define __NR_listxattr 230
1629#endif
1630#ifndef __NR_llistxattr
1631#define __NR_llistxattr 231
1632#endif
1633#ifndef __NR_gettid
1634#define __NR_gettid 236
1635#endif
1636#ifndef __NR_tkill
1637#define __NR_tkill 237
1638#endif
1639#ifndef __NR_futex
1640#define __NR_futex 238
1641#endif
1642#ifndef __NR_sched_setaffinity
1643#define __NR_sched_setaffinity 239
1644#endif
1645#ifndef __NR_sched_getaffinity
1646#define __NR_sched_getaffinity 240
1647#endif
1648#ifndef __NR_set_tid_address
1649#define __NR_set_tid_address 252
1650#endif
1651#ifndef __NR_clock_gettime
1652#define __NR_clock_gettime 260
1653#endif
1654#ifndef __NR_clock_getres
1655#define __NR_clock_getres 261
1656#endif
1657#ifndef __NR_statfs64
1658#define __NR_statfs64 265
1659#endif
1660#ifndef __NR_fstatfs64
1661#define __NR_fstatfs64 266
1662#endif
1663#ifndef __NR_ioprio_set
1664#define __NR_ioprio_set 282
1665#endif
1666#ifndef __NR_ioprio_get
1667#define __NR_ioprio_get 283
1668#endif
1669#ifndef __NR_openat
1670#define __NR_openat 288
1671#endif
1672#ifndef __NR_unlinkat
1673#define __NR_unlinkat 294
1674#endif
1675#ifndef __NR_move_pages
1676#define __NR_move_pages 310
1677#endif
1678#ifndef __NR_getcpu
1679#define __NR_getcpu 311
1680#endif
1681#ifndef __NR_fallocate
1682#define __NR_fallocate 314
1683#endif
1684/* Some syscalls are named/numbered differently between s390 and s390x. */
1685#ifdef __s390x__
1686# ifndef __NR_getrlimit
1687# define __NR_getrlimit 191
1688# endif
1689# ifndef __NR_setresuid
1690# define __NR_setresuid 208
1691# endif
1692# ifndef __NR_getresuid
1693# define __NR_getresuid 209
1694# endif
1695# ifndef __NR_setresgid
1696# define __NR_setresgid 210
1697# endif
1698# ifndef __NR_getresgid
1699# define __NR_getresgid 211
1700# endif
1701# ifndef __NR_setfsuid
1702# define __NR_setfsuid 215
1703# endif
1704# ifndef __NR_setfsgid
1705# define __NR_setfsgid 216
1706# endif
1707# ifndef __NR_fadvise64
1708# define __NR_fadvise64 253
1709# endif
1710# ifndef __NR_newfstatat
1711# define __NR_newfstatat 293
1712# endif
1713#else /* __s390x__ */
1714# ifndef __NR_getrlimit
1715# define __NR_getrlimit 76
1716# endif
1717# ifndef __NR_setfsuid
1718# define __NR_setfsuid 138
1719# endif
1720# ifndef __NR_setfsgid
1721# define __NR_setfsgid 139
1722# endif
1723# ifndef __NR_setresuid
1724# define __NR_setresuid 164
1725# endif
1726# ifndef __NR_getresuid
1727# define __NR_getresuid 165
1728# endif
1729# ifndef __NR_setresgid
1730# define __NR_setresgid 170
1731# endif
1732# ifndef __NR_getresgid
1733# define __NR_getresgid 171
1734# endif
1735# ifndef __NR_ugetrlimit
1736# define __NR_ugetrlimit 191
1737# endif
1738# ifndef __NR_mmap2
1739# define __NR_mmap2 192
1740# endif
1741# ifndef __NR_setresuid32
1742# define __NR_setresuid32 208
1743# endif
1744# ifndef __NR_getresuid32
1745# define __NR_getresuid32 209
1746# endif
1747# ifndef __NR_setresgid32
1748# define __NR_setresgid32 210
1749# endif
1750# ifndef __NR_getresgid32
1751# define __NR_getresgid32 211
1752# endif
1753# ifndef __NR_setfsuid32
1754# define __NR_setfsuid32 215
1755# endif
1756# ifndef __NR_setfsgid32
1757# define __NR_setfsgid32 216
1758# endif
1759# ifndef __NR_fadvise64_64
1760# define __NR_fadvise64_64 264
1761# endif
1762# ifndef __NR_fstatat64
1763# define __NR_fstatat64 293
1764# endif
1765#endif /* __s390__ */
1766/* End of s390/s390x definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001767#endif
1768
1769
1770/* After forking, we must make sure to only call system calls. */
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001771#if defined(__BOUNDED_POINTERS__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001772 #error "Need to port invocations of syscalls for bounded ptrs"
1773#else
1774 /* The core dumper and the thread lister get executed after threads
1775 * have been suspended. As a consequence, we cannot call any functions
1776 * that acquire locks. Unfortunately, libc wraps most system calls
1777 * (e.g. in order to implement pthread_atfork, and to make calls
1778 * cancellable), which means we cannot call these functions. Instead,
1779 * we have to call syscall() directly.
1780 */
1781 #undef LSS_ERRNO
1782 #ifdef SYS_ERRNO
1783 /* Allow the including file to override the location of errno. This can
1784 * be useful when using clone() with the CLONE_VM option.
1785 */
1786 #define LSS_ERRNO SYS_ERRNO
1787 #else
1788 #define LSS_ERRNO errno
1789 #endif
1790
1791 #undef LSS_INLINE
1792 #ifdef SYS_INLINE
1793 #define LSS_INLINE SYS_INLINE
1794 #else
1795 #define LSS_INLINE static inline
1796 #endif
1797
1798 /* Allow the including file to override the prefix used for all new
1799 * system calls. By default, it will be set to "sys_".
1800 */
1801 #undef LSS_NAME
1802 #ifndef SYS_PREFIX
1803 #define LSS_NAME(name) sys_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001804 #elif defined(SYS_PREFIX) && SYS_PREFIX < 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001805 #define LSS_NAME(name) name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001806 #elif defined(SYS_PREFIX) && SYS_PREFIX == 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001807 #define LSS_NAME(name) sys0_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001808 #elif defined(SYS_PREFIX) && SYS_PREFIX == 1
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001809 #define LSS_NAME(name) sys1_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001810 #elif defined(SYS_PREFIX) && SYS_PREFIX == 2
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001811 #define LSS_NAME(name) sys2_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001812 #elif defined(SYS_PREFIX) && SYS_PREFIX == 3
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001813 #define LSS_NAME(name) sys3_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001814 #elif defined(SYS_PREFIX) && SYS_PREFIX == 4
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001815 #define LSS_NAME(name) sys4_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001816 #elif defined(SYS_PREFIX) && SYS_PREFIX == 5
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001817 #define LSS_NAME(name) sys5_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001818 #elif defined(SYS_PREFIX) && SYS_PREFIX == 6
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001819 #define LSS_NAME(name) sys6_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001820 #elif defined(SYS_PREFIX) && SYS_PREFIX == 7
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001821 #define LSS_NAME(name) sys7_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001822 #elif defined(SYS_PREFIX) && SYS_PREFIX == 8
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001823 #define LSS_NAME(name) sys8_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001824 #elif defined(SYS_PREFIX) && SYS_PREFIX == 9
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001825 #define LSS_NAME(name) sys9_##name
1826 #endif
1827
1828 #undef LSS_RETURN
1829 #if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) \
Bryan Chan3f6478a2016-06-14 08:38:17 -04001830 || defined(__ARM_EABI__) || defined(__aarch64__) || defined(__s390__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001831 /* Failing system calls return a negative result in the range of
1832 * -1..-4095. These are "errno" values with the sign inverted.
1833 */
1834 #define LSS_RETURN(type, res) \
1835 do { \
1836 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
1837 LSS_ERRNO = -(res); \
1838 res = -1; \
1839 } \
1840 return (type) (res); \
1841 } while (0)
1842 #elif defined(__mips__)
1843 /* On MIPS, failing system calls return -1, and set errno in a
1844 * separate CPU register.
1845 */
1846 #define LSS_RETURN(type, res, err) \
1847 do { \
1848 if (err) { \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00001849 unsigned long __errnovalue = (res); \
1850 LSS_ERRNO = __errnovalue; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001851 res = -1; \
1852 } \
1853 return (type) (res); \
1854 } while (0)
1855 #elif defined(__PPC__)
1856 /* On PPC, failing system calls return -1, and set errno in a
1857 * separate CPU register. See linux/unistd.h.
1858 */
1859 #define LSS_RETURN(type, res, err) \
1860 do { \
1861 if (err & 0x10000000 ) { \
1862 LSS_ERRNO = (res); \
1863 res = -1; \
1864 } \
1865 return (type) (res); \
1866 } while (0)
1867 #endif
1868 #if defined(__i386__)
1869 /* In PIC mode (e.g. when building shared libraries), gcc for i386
1870 * reserves ebx. Unfortunately, most distribution ship with implementations
1871 * of _syscallX() which clobber ebx.
1872 * Also, most definitions of _syscallX() neglect to mark "memory" as being
1873 * clobbered. This causes problems with compilers, that do a better job
1874 * at optimizing across __asm__ calls.
1875 * So, we just have to redefine all of the _syscallX() macros.
1876 */
1877 #undef LSS_ENTRYPOINT
1878 #ifdef SYS_SYSCALL_ENTRYPOINT
1879 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
1880 void (**entrypoint)(void);
1881 asm volatile(".bss\n"
1882 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001883 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
1884 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001885 ".previous\n"
1886 /* This logically does 'lea "SYS_SYSCALL_ENTRYPOINT", %0' */
1887 "call 0f\n"
1888 "0:pop %0\n"
1889 "add $_GLOBAL_OFFSET_TABLE_+[.-0b], %0\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001890 "mov " SYS_SYSCALL_ENTRYPOINT "@GOT(%0), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001891 : "=r"(entrypoint));
1892 return entrypoint;
1893 }
1894
1895 #define LSS_ENTRYPOINT ".bss\n" \
1896 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001897 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
1898 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001899 ".previous\n" \
1900 /* Check the SYS_SYSCALL_ENTRYPOINT vector */ \
1901 "push %%eax\n" \
1902 "call 10000f\n" \
1903 "10000:pop %%eax\n" \
1904 "add $_GLOBAL_OFFSET_TABLE_+[.-10000b], %%eax\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001905 "mov " SYS_SYSCALL_ENTRYPOINT \
1906 "@GOT(%%eax), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001907 "mov 0(%%eax), %%eax\n" \
1908 "test %%eax, %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001909 "jz 10002f\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001910 "push %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001911 "call 10001f\n" \
1912 "10001:pop %%eax\n" \
1913 "add $(10003f-10001b), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001914 "xchg 4(%%esp), %%eax\n" \
1915 "ret\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001916 "10002:pop %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001917 "int $0x80\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001918 "10003:\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001919 #else
1920 #define LSS_ENTRYPOINT "int $0x80\n"
1921 #endif
1922 #undef LSS_BODY
1923 #define LSS_BODY(type,args...) \
1924 long __res; \
1925 __asm__ __volatile__("push %%ebx\n" \
1926 "movl %2,%%ebx\n" \
1927 LSS_ENTRYPOINT \
1928 "pop %%ebx" \
1929 args \
1930 : "esp", "memory"); \
1931 LSS_RETURN(type,__res)
1932 #undef _syscall0
1933 #define _syscall0(type,name) \
1934 type LSS_NAME(name)(void) { \
1935 long __res; \
1936 __asm__ volatile(LSS_ENTRYPOINT \
1937 : "=a" (__res) \
1938 : "0" (__NR_##name) \
1939 : "esp", "memory"); \
1940 LSS_RETURN(type,__res); \
1941 }
1942 #undef _syscall1
1943 #define _syscall1(type,name,type1,arg1) \
1944 type LSS_NAME(name)(type1 arg1) { \
1945 LSS_BODY(type, \
1946 : "=a" (__res) \
1947 : "0" (__NR_##name), "ri" ((long)(arg1))); \
1948 }
1949 #undef _syscall2
1950 #define _syscall2(type,name,type1,arg1,type2,arg2) \
1951 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
1952 LSS_BODY(type, \
1953 : "=a" (__res) \
1954 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
1955 }
1956 #undef _syscall3
1957 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
1958 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
1959 LSS_BODY(type, \
1960 : "=a" (__res) \
1961 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1962 "d" ((long)(arg3))); \
1963 }
1964 #undef _syscall4
1965 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1966 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1967 LSS_BODY(type, \
1968 : "=a" (__res) \
1969 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1970 "d" ((long)(arg3)),"S" ((long)(arg4))); \
1971 }
1972 #undef _syscall5
1973 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1974 type5,arg5) \
1975 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1976 type5 arg5) { \
1977 long __res; \
1978 __asm__ __volatile__("push %%ebx\n" \
1979 "movl %2,%%ebx\n" \
1980 "movl %1,%%eax\n" \
1981 LSS_ENTRYPOINT \
1982 "pop %%ebx" \
1983 : "=a" (__res) \
1984 : "i" (__NR_##name), "ri" ((long)(arg1)), \
1985 "c" ((long)(arg2)), "d" ((long)(arg3)), \
1986 "S" ((long)(arg4)), "D" ((long)(arg5)) \
1987 : "esp", "memory"); \
1988 LSS_RETURN(type,__res); \
1989 }
1990 #undef _syscall6
1991 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1992 type5,arg5,type6,arg6) \
1993 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1994 type5 arg5, type6 arg6) { \
1995 long __res; \
1996 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
1997 __asm__ __volatile__("push %%ebp\n" \
1998 "push %%ebx\n" \
mseaborn@chromium.orge96ade32012-10-27 17:47:38 +00001999 "movl 4(%2),%%ebp\n" \
2000 "movl 0(%2), %%ebx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002001 "movl %1,%%eax\n" \
2002 LSS_ENTRYPOINT \
2003 "pop %%ebx\n" \
2004 "pop %%ebp" \
2005 : "=a" (__res) \
2006 : "i" (__NR_##name), "0" ((long)(&__s)), \
2007 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2008 "S" ((long)(arg4)), "D" ((long)(arg5)) \
2009 : "esp", "memory"); \
2010 LSS_RETURN(type,__res); \
2011 }
2012 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2013 int flags, void *arg, int *parent_tidptr,
2014 void *newtls, int *child_tidptr) {
2015 long __res;
2016 __asm__ __volatile__(/* if (fn == NULL)
2017 * return -EINVAL;
2018 */
2019 "movl %3,%%ecx\n"
2020 "jecxz 1f\n"
2021
2022 /* if (child_stack == NULL)
2023 * return -EINVAL;
2024 */
2025 "movl %4,%%ecx\n"
2026 "jecxz 1f\n"
2027
2028 /* Set up alignment of the child stack:
2029 * child_stack = (child_stack & ~0xF) - 20;
2030 */
2031 "andl $-16,%%ecx\n"
2032 "subl $20,%%ecx\n"
2033
2034 /* Push "arg" and "fn" onto the stack that will be
2035 * used by the child.
2036 */
2037 "movl %6,%%eax\n"
2038 "movl %%eax,4(%%ecx)\n"
2039 "movl %3,%%eax\n"
2040 "movl %%eax,(%%ecx)\n"
2041
2042 /* %eax = syscall(%eax = __NR_clone,
2043 * %ebx = flags,
2044 * %ecx = child_stack,
2045 * %edx = parent_tidptr,
2046 * %esi = newtls,
2047 * %edi = child_tidptr)
2048 * Also, make sure that %ebx gets preserved as it is
2049 * used in PIC mode.
2050 */
2051 "movl %8,%%esi\n"
2052 "movl %7,%%edx\n"
2053 "movl %5,%%eax\n"
2054 "movl %9,%%edi\n"
2055 "pushl %%ebx\n"
2056 "movl %%eax,%%ebx\n"
2057 "movl %2,%%eax\n"
2058 LSS_ENTRYPOINT
2059
2060 /* In the parent: restore %ebx
2061 * In the child: move "fn" into %ebx
2062 */
2063 "popl %%ebx\n"
2064
2065 /* if (%eax != 0)
2066 * return %eax;
2067 */
2068 "test %%eax,%%eax\n"
2069 "jnz 1f\n"
2070
2071 /* In the child, now. Terminate frame pointer chain.
2072 */
2073 "movl $0,%%ebp\n"
2074
2075 /* Call "fn". "arg" is already on the stack.
2076 */
2077 "call *%%ebx\n"
2078
2079 /* Call _exit(%ebx). Unfortunately older versions
2080 * of gcc restrict the number of arguments that can
2081 * be passed to asm(). So, we need to hard-code the
2082 * system call number.
2083 */
2084 "movl %%eax,%%ebx\n"
2085 "movl $1,%%eax\n"
2086 LSS_ENTRYPOINT
2087
2088 /* Return to parent.
2089 */
2090 "1:\n"
2091 : "=a" (__res)
2092 : "0"(-EINVAL), "i"(__NR_clone),
2093 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
2094 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
2095 : "esp", "memory", "ecx", "edx", "esi", "edi");
2096 LSS_RETURN(int, __res);
2097 }
2098
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002099 LSS_INLINE _syscall1(int, set_thread_area, void *, u)
2100 LSS_INLINE _syscall1(int, get_thread_area, void *, u)
2101
2102 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2103 /* On i386, the kernel does not know how to return from a signal
2104 * handler. Instead, it relies on user space to provide a
2105 * restorer function that calls the {rt_,}sigreturn() system call.
2106 * Unfortunately, we cannot just reference the glibc version of this
2107 * function, as glibc goes out of its way to make it inaccessible.
2108 */
2109 void (*res)(void);
2110 __asm__ __volatile__("call 2f\n"
2111 "0:.align 16\n"
2112 "1:movl %1,%%eax\n"
2113 LSS_ENTRYPOINT
2114 "2:popl %0\n"
2115 "addl $(1b-0b),%0\n"
2116 : "=a" (res)
2117 : "i" (__NR_rt_sigreturn));
2118 return res;
2119 }
2120 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
2121 /* On i386, the kernel does not know how to return from a signal
2122 * handler. Instead, it relies on user space to provide a
2123 * restorer function that calls the {rt_,}sigreturn() system call.
2124 * Unfortunately, we cannot just reference the glibc version of this
2125 * function, as glibc goes out of its way to make it inaccessible.
2126 */
2127 void (*res)(void);
2128 __asm__ __volatile__("call 2f\n"
2129 "0:.align 16\n"
2130 "1:pop %%eax\n"
2131 "movl %1,%%eax\n"
2132 LSS_ENTRYPOINT
2133 "2:popl %0\n"
2134 "addl $(1b-0b),%0\n"
2135 : "=a" (res)
2136 : "i" (__NR_sigreturn));
2137 return res;
2138 }
2139 #elif defined(__x86_64__)
2140 /* There are no known problems with any of the _syscallX() macros
2141 * currently shipping for x86_64, but we still need to be able to define
2142 * our own version so that we can override the location of the errno
2143 * location (e.g. when using the clone() system call with the CLONE_VM
2144 * option).
2145 */
2146 #undef LSS_ENTRYPOINT
2147 #ifdef SYS_SYSCALL_ENTRYPOINT
2148 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2149 void (**entrypoint)(void);
2150 asm volatile(".bss\n"
2151 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002152 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2153 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002154 ".previous\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002155 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002156 : "=r"(entrypoint));
2157 return entrypoint;
2158 }
2159
2160 #define LSS_ENTRYPOINT \
2161 ".bss\n" \
2162 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002163 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2164 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002165 ".previous\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002166 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %%rcx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002167 "mov 0(%%rcx), %%rcx\n" \
2168 "test %%rcx, %%rcx\n" \
2169 "jz 10001f\n" \
2170 "call *%%rcx\n" \
2171 "jmp 10002f\n" \
2172 "10001:syscall\n" \
2173 "10002:\n"
2174
2175 #else
2176 #define LSS_ENTRYPOINT "syscall\n"
2177 #endif
vapier@chromium.org2273e812013-04-01 17:52:44 +00002178
2179 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
2180 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
2181 * sign extension. We can't cast pointers directly because those are
2182 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
2183 * to an integer of a different size.
2184 */
2185 #undef LSS_SYSCALL_ARG
2186 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
2187 #undef _LSS_RETURN
2188 #define _LSS_RETURN(type, res, cast) \
2189 do { \
2190 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
2191 LSS_ERRNO = -(res); \
2192 res = -1; \
2193 } \
2194 return (type)(cast)(res); \
2195 } while (0)
2196 #undef LSS_RETURN
2197 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
2198
2199 #undef _LSS_BODY
2200 #define _LSS_BODY(nr, type, name, cast, ...) \
2201 long long __res; \
2202 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
2203 : "=a" (__res) \
2204 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
2205 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
2206 _LSS_RETURN(type, __res, cast)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002207 #undef LSS_BODY
vapier@chromium.org2273e812013-04-01 17:52:44 +00002208 #define LSS_BODY(nr, type, name, args...) \
2209 _LSS_BODY(nr, type, name, uintptr_t, ## args)
2210
2211 #undef LSS_BODY_ASM0
2212 #undef LSS_BODY_ASM1
2213 #undef LSS_BODY_ASM2
2214 #undef LSS_BODY_ASM3
2215 #undef LSS_BODY_ASM4
2216 #undef LSS_BODY_ASM5
2217 #undef LSS_BODY_ASM6
2218 #define LSS_BODY_ASM0
2219 #define LSS_BODY_ASM1 LSS_BODY_ASM0
2220 #define LSS_BODY_ASM2 LSS_BODY_ASM1
2221 #define LSS_BODY_ASM3 LSS_BODY_ASM2
2222 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
2223 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
2224 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
2225
2226 #undef LSS_BODY_CLOBBER0
2227 #undef LSS_BODY_CLOBBER1
2228 #undef LSS_BODY_CLOBBER2
2229 #undef LSS_BODY_CLOBBER3
2230 #undef LSS_BODY_CLOBBER4
2231 #undef LSS_BODY_CLOBBER5
2232 #undef LSS_BODY_CLOBBER6
2233 #define LSS_BODY_CLOBBER0
2234 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
2235 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
2236 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
2237 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
2238 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
2239 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
2240
2241 #undef LSS_BODY_ARG0
2242 #undef LSS_BODY_ARG1
2243 #undef LSS_BODY_ARG2
2244 #undef LSS_BODY_ARG3
2245 #undef LSS_BODY_ARG4
2246 #undef LSS_BODY_ARG5
2247 #undef LSS_BODY_ARG6
2248 #define LSS_BODY_ARG0()
2249 #define LSS_BODY_ARG1(arg1) \
2250 LSS_BODY_ARG0(), "D" (arg1)
2251 #define LSS_BODY_ARG2(arg1, arg2) \
2252 LSS_BODY_ARG1(arg1), "S" (arg2)
2253 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
2254 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
2255 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
2256 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
2257 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
2258 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
2259 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
2260 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
2261
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002262 #undef _syscall0
2263 #define _syscall0(type,name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002264 type LSS_NAME(name)(void) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002265 LSS_BODY(0, type, name); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002266 }
2267 #undef _syscall1
2268 #define _syscall1(type,name,type1,arg1) \
2269 type LSS_NAME(name)(type1 arg1) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002270 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002271 }
2272 #undef _syscall2
2273 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2274 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002275 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002276 }
2277 #undef _syscall3
2278 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2279 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002280 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2281 LSS_SYSCALL_ARG(arg3)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002282 }
2283 #undef _syscall4
2284 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2285 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002286 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2287 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002288 }
2289 #undef _syscall5
2290 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2291 type5,arg5) \
2292 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2293 type5 arg5) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002294 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2295 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2296 LSS_SYSCALL_ARG(arg5)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002297 }
2298 #undef _syscall6
2299 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2300 type5,arg5,type6,arg6) \
2301 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2302 type5 arg5, type6 arg6) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002303 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2304 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2305 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002306 }
2307 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2308 int flags, void *arg, int *parent_tidptr,
2309 void *newtls, int *child_tidptr) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00002310 long long __res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002311 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002312 __asm__ __volatile__(/* if (fn == NULL)
2313 * return -EINVAL;
2314 */
2315 "testq %4,%4\n"
2316 "jz 1f\n"
2317
2318 /* if (child_stack == NULL)
2319 * return -EINVAL;
2320 */
2321 "testq %5,%5\n"
2322 "jz 1f\n"
2323
2324 /* childstack -= 2*sizeof(void *);
2325 */
2326 "subq $16,%5\n"
2327
2328 /* Push "arg" and "fn" onto the stack that will be
2329 * used by the child.
2330 */
2331 "movq %7,8(%5)\n"
2332 "movq %4,0(%5)\n"
2333
2334 /* %rax = syscall(%rax = __NR_clone,
2335 * %rdi = flags,
2336 * %rsi = child_stack,
2337 * %rdx = parent_tidptr,
2338 * %r8 = new_tls,
2339 * %r10 = child_tidptr)
2340 */
2341 "movq %2,%%rax\n"
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002342 "movq %9,%%r8\n"
2343 "movq %10,%%r10\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002344 LSS_ENTRYPOINT
2345
2346 /* if (%rax != 0)
2347 * return;
2348 */
2349 "testq %%rax,%%rax\n"
2350 "jnz 1f\n"
2351
2352 /* In the child. Terminate frame pointer chain.
2353 */
2354 "xorq %%rbp,%%rbp\n"
2355
2356 /* Call "fn(arg)".
2357 */
2358 "popq %%rax\n"
2359 "popq %%rdi\n"
2360 "call *%%rax\n"
2361
2362 /* Call _exit(%ebx).
2363 */
2364 "movq %%rax,%%rdi\n"
2365 "movq %3,%%rax\n"
2366 LSS_ENTRYPOINT
2367
2368 /* Return to parent.
2369 */
2370 "1:\n"
2371 : "=a" (__res)
2372 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
vapier@chromium.org2273e812013-04-01 17:52:44 +00002373 "r"(LSS_SYSCALL_ARG(fn)),
2374 "S"(LSS_SYSCALL_ARG(child_stack)),
2375 "D"(LSS_SYSCALL_ARG(flags)),
2376 "r"(LSS_SYSCALL_ARG(arg)),
2377 "d"(LSS_SYSCALL_ARG(parent_tidptr)),
2378 "r"(LSS_SYSCALL_ARG(newtls)),
2379 "r"(LSS_SYSCALL_ARG(child_tidptr))
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002380 : "rsp", "memory", "r8", "r10", "r11", "rcx");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002381 }
2382 LSS_RETURN(int, __res);
2383 }
2384 LSS_INLINE _syscall2(int, arch_prctl, int, c, void *, a)
vapier@chromium.org2273e812013-04-01 17:52:44 +00002385
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002386 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2387 /* On x86-64, the kernel does not know how to return from
2388 * a signal handler. Instead, it relies on user space to provide a
2389 * restorer function that calls the rt_sigreturn() system call.
2390 * Unfortunately, we cannot just reference the glibc version of this
2391 * function, as glibc goes out of its way to make it inaccessible.
2392 */
vapier@chromium.org2273e812013-04-01 17:52:44 +00002393 long long res;
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002394 __asm__ __volatile__("jmp 2f\n"
2395 ".align 16\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002396 "1:movq %1,%%rax\n"
2397 LSS_ENTRYPOINT
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002398 "2:leaq 1b(%%rip),%0\n"
2399 : "=r" (res)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002400 : "i" (__NR_rt_sigreturn));
vapier@chromium.org833a10e2013-04-02 19:34:26 +00002401 return (void (*)(void))(uintptr_t)res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002402 }
2403 #elif defined(__ARM_ARCH_3__)
2404 /* Most definitions of _syscallX() neglect to mark "memory" as being
2405 * clobbered. This causes problems with compilers, that do a better job
2406 * at optimizing across __asm__ calls.
2407 * So, we just have to redefine all of the _syscallX() macros.
2408 */
2409 #undef LSS_REG
2410 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2411 #undef LSS_BODY
2412 #define LSS_BODY(type,name,args...) \
2413 register long __res_r0 __asm__("r0"); \
2414 long __res; \
2415 __asm__ __volatile__ (__syscall(name) \
2416 : "=r"(__res_r0) : args : "lr", "memory"); \
2417 __res = __res_r0; \
2418 LSS_RETURN(type, __res)
2419 #undef _syscall0
2420 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002421 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002422 LSS_BODY(type, name); \
2423 }
2424 #undef _syscall1
2425 #define _syscall1(type, name, type1, arg1) \
2426 type LSS_NAME(name)(type1 arg1) { \
2427 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2428 }
2429 #undef _syscall2
2430 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2431 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2432 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2433 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2434 }
2435 #undef _syscall3
2436 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2437 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2438 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2439 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2440 }
2441 #undef _syscall4
2442 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2443 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2444 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2445 LSS_REG(3, arg4); \
2446 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2447 }
2448 #undef _syscall5
2449 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2450 type5,arg5) \
2451 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2452 type5 arg5) { \
2453 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2454 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2455 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2456 "r"(__r4)); \
2457 }
2458 #undef _syscall6
2459 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2460 type5,arg5,type6,arg6) \
2461 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2462 type5 arg5, type6 arg6) { \
2463 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2464 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2465 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2466 "r"(__r4), "r"(__r5)); \
2467 }
2468 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2469 int flags, void *arg, int *parent_tidptr,
2470 void *newtls, int *child_tidptr) {
2471 long __res;
2472 {
2473 register int __flags __asm__("r0") = flags;
2474 register void *__stack __asm__("r1") = child_stack;
2475 register void *__ptid __asm__("r2") = parent_tidptr;
2476 register void *__tls __asm__("r3") = newtls;
2477 register int *__ctid __asm__("r4") = child_tidptr;
2478 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2479 * return -EINVAL;
2480 */
2481 "cmp %2,#0\n"
2482 "cmpne %3,#0\n"
2483 "moveq %0,%1\n"
2484 "beq 1f\n"
2485
2486 /* Push "arg" and "fn" onto the stack that will be
2487 * used by the child.
2488 */
2489 "str %5,[%3,#-4]!\n"
2490 "str %2,[%3,#-4]!\n"
2491
2492 /* %r0 = syscall(%r0 = flags,
2493 * %r1 = child_stack,
2494 * %r2 = parent_tidptr,
2495 * %r3 = newtls,
2496 * %r4 = child_tidptr)
2497 */
2498 __syscall(clone)"\n"
2499
2500 /* if (%r0 != 0)
2501 * return %r0;
2502 */
2503 "movs %0,r0\n"
2504 "bne 1f\n"
2505
2506 /* In the child, now. Call "fn(arg)".
2507 */
2508 "ldr r0,[sp, #4]\n"
2509 "mov lr,pc\n"
2510 "ldr pc,[sp]\n"
2511
2512 /* Call _exit(%r0).
2513 */
2514 __syscall(exit)"\n"
2515 "1:\n"
2516 : "=r" (__res)
2517 : "i"(-EINVAL),
2518 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2519 "r"(__ptid), "r"(__tls), "r"(__ctid)
2520 : "cc", "lr", "memory");
2521 }
2522 LSS_RETURN(int, __res);
2523 }
2524 #elif defined(__ARM_EABI__)
2525 /* Most definitions of _syscallX() neglect to mark "memory" as being
2526 * clobbered. This causes problems with compilers, that do a better job
2527 * at optimizing across __asm__ calls.
2528 * So, we just have to redefine all fo the _syscallX() macros.
2529 */
2530 #undef LSS_REG
2531 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2532 #undef LSS_BODY
2533 #define LSS_BODY(type,name,args...) \
2534 register long __res_r0 __asm__("r0"); \
2535 long __res; \
2536 __asm__ __volatile__ ("push {r7}\n" \
2537 "mov r7, %1\n" \
2538 "swi 0x0\n" \
2539 "pop {r7}\n" \
2540 : "=r"(__res_r0) \
2541 : "i"(__NR_##name) , ## args \
2542 : "lr", "memory"); \
2543 __res = __res_r0; \
2544 LSS_RETURN(type, __res)
2545 #undef _syscall0
2546 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002547 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002548 LSS_BODY(type, name); \
2549 }
2550 #undef _syscall1
2551 #define _syscall1(type, name, type1, arg1) \
2552 type LSS_NAME(name)(type1 arg1) { \
2553 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2554 }
2555 #undef _syscall2
2556 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2557 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2558 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2559 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2560 }
2561 #undef _syscall3
2562 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2563 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2564 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2565 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2566 }
2567 #undef _syscall4
2568 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2569 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2570 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2571 LSS_REG(3, arg4); \
2572 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2573 }
2574 #undef _syscall5
2575 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2576 type5,arg5) \
2577 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2578 type5 arg5) { \
2579 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2580 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2581 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2582 "r"(__r4)); \
2583 }
2584 #undef _syscall6
2585 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2586 type5,arg5,type6,arg6) \
2587 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2588 type5 arg5, type6 arg6) { \
2589 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2590 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2591 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2592 "r"(__r4), "r"(__r5)); \
2593 }
2594 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2595 int flags, void *arg, int *parent_tidptr,
2596 void *newtls, int *child_tidptr) {
2597 long __res;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002598 if (fn == NULL || child_stack == NULL) {
2599 __res = -EINVAL;
2600 } else {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002601 register int __flags __asm__("r0") = flags;
2602 register void *__stack __asm__("r1") = child_stack;
2603 register void *__ptid __asm__("r2") = parent_tidptr;
2604 register void *__tls __asm__("r3") = newtls;
2605 register int *__ctid __asm__("r4") = child_tidptr;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002606 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002607 * used by the child.
2608 */
Nico Weber63f24c82017-03-30 13:37:06 -04002609#ifdef __thumb2__
2610 "push {r7}\n"
2611#endif
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002612 "str %4,[%2,#-4]!\n"
2613 "str %1,[%2,#-4]!\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002614
2615 /* %r0 = syscall(%r0 = flags,
2616 * %r1 = child_stack,
2617 * %r2 = parent_tidptr,
2618 * %r3 = newtls,
2619 * %r4 = child_tidptr)
2620 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002621 "mov r7, %8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002622 "swi 0x0\n"
2623
2624 /* if (%r0 != 0)
2625 * return %r0;
2626 */
2627 "movs %0,r0\n"
2628 "bne 1f\n"
2629
2630 /* In the child, now. Call "fn(arg)".
2631 */
2632 "ldr r0,[sp, #4]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002633
2634 /* When compiling for Thumb-2 the "MOV LR,PC" here
2635 * won't work because it loads PC+4 into LR,
2636 * whereas the LDR is a 4-byte instruction.
2637 * This results in the child thread always
2638 * crashing with an "Illegal Instruction" when it
2639 * returned into the middle of the LDR instruction
2640 * The instruction sequence used instead was
2641 * recommended by
2642 * "https://wiki.edubuntu.org/ARM/Thumb2PortingHowto#Quick_Reference".
2643 */
2644 #ifdef __thumb2__
2645 "ldr r7,[sp]\n"
2646 "blx r7\n"
2647 #else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002648 "mov lr,pc\n"
2649 "ldr pc,[sp]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002650 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002651
2652 /* Call _exit(%r0).
2653 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002654 "mov r7, %9\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002655 "swi 0x0\n"
2656 "1:\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002657#ifdef __thumb2__
2658 "pop {r7}"
2659#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002660 : "=r" (__res)
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002661 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002662 "r"(__ptid), "r"(__tls), "r"(__ctid),
2663 "i"(__NR_clone), "i"(__NR_exit)
Nico Weber63f24c82017-03-30 13:37:06 -04002664#ifdef __thumb2__
2665 : "cc", "lr", "memory");
2666#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002667 : "cc", "r7", "lr", "memory");
Nico Weber63f24c82017-03-30 13:37:06 -04002668#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002669 }
2670 LSS_RETURN(int, __res);
2671 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002672 #elif defined(__aarch64__)
2673 /* Most definitions of _syscallX() neglect to mark "memory" as being
2674 * clobbered. This causes problems with compilers, that do a better job
2675 * at optimizing across __asm__ calls.
2676 * So, we just have to redefine all of the _syscallX() macros.
2677 */
2678 #undef LSS_REG
2679 #define LSS_REG(r,a) register int64_t __r##r __asm__("x"#r) = (int64_t)a
2680 #undef LSS_BODY
2681 #define LSS_BODY(type,name,args...) \
2682 register int64_t __res_x0 __asm__("x0"); \
2683 int64_t __res; \
2684 __asm__ __volatile__ ("mov x8, %1\n" \
2685 "svc 0x0\n" \
2686 : "=r"(__res_x0) \
2687 : "i"(__NR_##name) , ## args \
2688 : "x8", "memory"); \
2689 __res = __res_x0; \
2690 LSS_RETURN(type, __res)
2691 #undef _syscall0
2692 #define _syscall0(type, name) \
2693 type LSS_NAME(name)(void) { \
2694 LSS_BODY(type, name); \
2695 }
2696 #undef _syscall1
2697 #define _syscall1(type, name, type1, arg1) \
2698 type LSS_NAME(name)(type1 arg1) { \
2699 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2700 }
2701 #undef _syscall2
2702 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2703 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2704 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2705 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2706 }
2707 #undef _syscall3
2708 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2709 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2710 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2711 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2712 }
2713 #undef _syscall4
2714 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2715 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2716 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2717 LSS_REG(3, arg4); \
2718 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2719 }
2720 #undef _syscall5
2721 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2722 type5,arg5) \
2723 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2724 type5 arg5) { \
2725 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2726 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2727 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2728 "r"(__r4)); \
2729 }
2730 #undef _syscall6
2731 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2732 type5,arg5,type6,arg6) \
2733 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2734 type5 arg5, type6 arg6) { \
2735 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2736 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2737 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2738 "r"(__r4), "r"(__r5)); \
2739 }
2740
2741 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2742 int flags, void *arg, int *parent_tidptr,
2743 void *newtls, int *child_tidptr) {
2744 int64_t __res;
2745 {
2746 register uint64_t __flags __asm__("x0") = flags;
2747 register void *__stack __asm__("x1") = child_stack;
2748 register void *__ptid __asm__("x2") = parent_tidptr;
2749 register void *__tls __asm__("x3") = newtls;
2750 register int *__ctid __asm__("x4") = child_tidptr;
2751 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
2752 * used by the child.
2753 */
2754 "stp %1, %4, [%2, #-16]!\n"
2755
2756 /* %x0 = syscall(%x0 = flags,
2757 * %x1 = child_stack,
2758 * %x2 = parent_tidptr,
2759 * %x3 = newtls,
2760 * %x4 = child_tidptr)
2761 */
2762 "mov x8, %8\n"
2763 "svc 0x0\n"
2764
2765 /* if (%r0 != 0)
2766 * return %r0;
2767 */
2768 "mov %0, x0\n"
2769 "cbnz x0, 1f\n"
2770
2771 /* In the child, now. Call "fn(arg)".
2772 */
2773 "ldp x1, x0, [sp], #16\n"
2774 "blr x1\n"
2775
2776 /* Call _exit(%r0).
2777 */
2778 "mov x8, %9\n"
2779 "svc 0x0\n"
2780 "1:\n"
2781 : "=r" (__res)
2782 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2783 "r"(__ptid), "r"(__tls), "r"(__ctid),
2784 "i"(__NR_clone), "i"(__NR_exit)
2785 : "cc", "x8", "memory");
2786 }
2787 LSS_RETURN(int, __res);
2788 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002789 #elif defined(__mips__)
2790 #undef LSS_REG
2791 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
2792 (unsigned long)(a)
2793 #undef LSS_BODY
thestig@chromium.org952107f2014-08-01 02:22:56 +00002794 #undef LSS_SYSCALL_CLOBBERS
2795 #if _MIPS_SIM == _MIPS_SIM_ABI32
2796 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", \
2797 "$11", "$12", "$13", "$14", "$15", \
2798 "$24", "$25", "hi", "lo", "memory"
2799 #else
2800 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", \
2801 "$13", "$14", "$15", "$24", "$25", \
2802 "hi", "lo", "memory"
2803 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002804 #define LSS_BODY(type,name,r7,...) \
2805 register unsigned long __v0 __asm__("$2") = __NR_##name; \
2806 __asm__ __volatile__ ("syscall\n" \
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00002807 : "=r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002808 : "0"(__v0), ##__VA_ARGS__ \
thestig@chromium.org952107f2014-08-01 02:22:56 +00002809 : LSS_SYSCALL_CLOBBERS); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002810 LSS_RETURN(type, __v0, __r7)
2811 #undef _syscall0
2812 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002813 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002814 register unsigned long __r7 __asm__("$7"); \
2815 LSS_BODY(type, name, "=r"); \
2816 }
2817 #undef _syscall1
2818 #define _syscall1(type, name, type1, arg1) \
2819 type LSS_NAME(name)(type1 arg1) { \
2820 register unsigned long __r7 __asm__("$7"); \
2821 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
2822 }
2823 #undef _syscall2
2824 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2825 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2826 register unsigned long __r7 __asm__("$7"); \
2827 LSS_REG(4, arg1); LSS_REG(5, arg2); \
2828 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
2829 }
2830 #undef _syscall3
2831 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2832 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2833 register unsigned long __r7 __asm__("$7"); \
2834 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2835 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2836 }
2837 #undef _syscall4
2838 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2839 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2840 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2841 LSS_REG(7, arg4); \
2842 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2843 }
2844 #undef _syscall5
2845 #if _MIPS_SIM == _MIPS_SIM_ABI32
2846 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2847 * on the stack, whereas the new APIs use registers "r8" and "r9".
2848 */
2849 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2850 type5,arg5) \
2851 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2852 type5 arg5) { \
2853 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2854 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002855 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002856 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002857 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002858 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002859 "syscall\n" \
2860 "addiu $29, 32\n" \
2861 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002862 : "+r"(__v0), "+r" (__r7) \
2863 : "r"(__r4), "r"(__r5), \
2864 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002865 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002866 "$13", "$14", "$15", "$24", "$25", \
2867 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002868 LSS_RETURN(type, __v0, __r7); \
2869 }
2870 #else
2871 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2872 type5,arg5) \
2873 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2874 type5 arg5) { \
2875 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2876 LSS_REG(7, arg4); LSS_REG(8, arg5); \
2877 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2878 "r"(__r8)); \
2879 }
2880 #endif
2881 #undef _syscall6
2882 #if _MIPS_SIM == _MIPS_SIM_ABI32
2883 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2884 * on the stack, whereas the new APIs use registers "r8" and "r9".
2885 */
2886 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2887 type5,arg5,type6,arg6) \
2888 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2889 type5 arg5, type6 arg6) { \
2890 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2891 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002892 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002893 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002894 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002895 "sw %5, 16($29)\n" \
2896 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002897 "syscall\n" \
2898 "addiu $29, 32\n" \
2899 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002900 : "+r"(__v0), "+r" (__r7) \
2901 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002902 "r"(__r6), "r" ((unsigned long)arg5), \
2903 "r" ((unsigned long)arg6) \
2904 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002905 "$13", "$14", "$15", "$24", "$25", \
2906 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002907 LSS_RETURN(type, __v0, __r7); \
2908 }
2909 #else
2910 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2911 type5,arg5,type6,arg6) \
2912 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2913 type5 arg5,type6 arg6) { \
2914 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2915 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
2916 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2917 "r"(__r8), "r"(__r9)); \
2918 }
2919 #endif
2920 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2921 int flags, void *arg, int *parent_tidptr,
2922 void *newtls, int *child_tidptr) {
vapier@chromium.orge0797682015-02-20 20:45:56 +00002923 register unsigned long __v0 __asm__("$2") = -EINVAL;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002924 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
2925 {
2926 register int __flags __asm__("$4") = flags;
2927 register void *__stack __asm__("$5") = child_stack;
2928 register void *__ptid __asm__("$6") = parent_tidptr;
2929 register int *__ctid __asm__("$8") = child_tidptr;
2930 __asm__ __volatile__(
2931 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2932 "subu $29,24\n"
2933 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2934 "sub $29,16\n"
2935 #else
2936 "dsubu $29,16\n"
2937 #endif
2938
2939 /* if (fn == NULL || child_stack == NULL)
2940 * return -EINVAL;
2941 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002942 "beqz %4,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002943 "beqz %5,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002944
2945 /* Push "arg" and "fn" onto the stack that will be
2946 * used by the child.
2947 */
2948 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002949 "subu %5,32\n"
2950 "sw %4,0(%5)\n"
2951 "sw %7,4(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002952 #elif _MIPS_SIM == _MIPS_SIM_NABI32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002953 "sub %5,32\n"
2954 "sw %4,0(%5)\n"
2955 "sw %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002956 #else
vapier@chromium.orge0797682015-02-20 20:45:56 +00002957 "dsubu %5,32\n"
2958 "sd %4,0(%5)\n"
2959 "sd %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002960 #endif
2961
2962 /* $7 = syscall($4 = flags,
2963 * $5 = child_stack,
2964 * $6 = parent_tidptr,
2965 * $7 = newtls,
2966 * $8 = child_tidptr)
2967 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002968 "li $2,%2\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002969 "syscall\n"
2970
2971 /* if ($7 != 0)
2972 * return $2;
2973 */
2974 "bnez $7,1f\n"
2975 "bnez $2,1f\n"
2976
2977 /* In the child, now. Call "fn(arg)".
2978 */
2979 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2980 "lw $25,0($29)\n"
2981 "lw $4,4($29)\n"
2982 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2983 "lw $25,0($29)\n"
2984 "lw $4,8($29)\n"
2985 #else
2986 "ld $25,0($29)\n"
2987 "ld $4,8($29)\n"
2988 #endif
2989 "jalr $25\n"
2990
2991 /* Call _exit($2)
2992 */
2993 "move $4,$2\n"
vapier@chromium.orge0797682015-02-20 20:45:56 +00002994 "li $2,%3\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002995 "syscall\n"
2996
2997 "1:\n"
2998 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2999 "addu $29, 24\n"
3000 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3001 "add $29, 16\n"
3002 #else
3003 "daddu $29,16\n"
3004 #endif
petarj@mips.com0ece1c62013-04-10 00:28:04 +00003005 : "+r" (__v0), "+r" (__r7)
vapier@chromium.orge0797682015-02-20 20:45:56 +00003006 : "i"(__NR_clone), "i"(__NR_exit), "r"(fn),
3007 "r"(__stack), "r"(__flags), "r"(arg),
3008 "r"(__ptid), "r"(__ctid)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003009 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003010 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003011 }
3012 LSS_RETURN(int, __v0, __r7);
3013 }
3014 #elif defined (__PPC__)
3015 #undef LSS_LOADARGS_0
3016 #define LSS_LOADARGS_0(name, dummy...) \
3017 __sc_0 = __NR_##name
3018 #undef LSS_LOADARGS_1
3019 #define LSS_LOADARGS_1(name, arg1) \
3020 LSS_LOADARGS_0(name); \
3021 __sc_3 = (unsigned long) (arg1)
3022 #undef LSS_LOADARGS_2
3023 #define LSS_LOADARGS_2(name, arg1, arg2) \
3024 LSS_LOADARGS_1(name, arg1); \
3025 __sc_4 = (unsigned long) (arg2)
3026 #undef LSS_LOADARGS_3
3027 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
3028 LSS_LOADARGS_2(name, arg1, arg2); \
3029 __sc_5 = (unsigned long) (arg3)
3030 #undef LSS_LOADARGS_4
3031 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
3032 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
3033 __sc_6 = (unsigned long) (arg4)
3034 #undef LSS_LOADARGS_5
3035 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
3036 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
3037 __sc_7 = (unsigned long) (arg5)
3038 #undef LSS_LOADARGS_6
3039 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
3040 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
3041 __sc_8 = (unsigned long) (arg6)
3042 #undef LSS_ASMINPUT_0
3043 #define LSS_ASMINPUT_0 "0" (__sc_0)
3044 #undef LSS_ASMINPUT_1
3045 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
3046 #undef LSS_ASMINPUT_2
3047 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
3048 #undef LSS_ASMINPUT_3
3049 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
3050 #undef LSS_ASMINPUT_4
3051 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
3052 #undef LSS_ASMINPUT_5
3053 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
3054 #undef LSS_ASMINPUT_6
3055 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
3056 #undef LSS_BODY
3057 #define LSS_BODY(nr, type, name, args...) \
3058 long __sc_ret, __sc_err; \
3059 { \
3060 register unsigned long __sc_0 __asm__ ("r0"); \
3061 register unsigned long __sc_3 __asm__ ("r3"); \
3062 register unsigned long __sc_4 __asm__ ("r4"); \
3063 register unsigned long __sc_5 __asm__ ("r5"); \
3064 register unsigned long __sc_6 __asm__ ("r6"); \
3065 register unsigned long __sc_7 __asm__ ("r7"); \
3066 register unsigned long __sc_8 __asm__ ("r8"); \
3067 \
3068 LSS_LOADARGS_##nr(name, args); \
3069 __asm__ __volatile__ \
3070 ("sc\n\t" \
3071 "mfcr %0" \
3072 : "=&r" (__sc_0), \
3073 "=&r" (__sc_3), "=&r" (__sc_4), \
3074 "=&r" (__sc_5), "=&r" (__sc_6), \
3075 "=&r" (__sc_7), "=&r" (__sc_8) \
3076 : LSS_ASMINPUT_##nr \
3077 : "cr0", "ctr", "memory", \
3078 "r9", "r10", "r11", "r12"); \
3079 __sc_ret = __sc_3; \
3080 __sc_err = __sc_0; \
3081 } \
3082 LSS_RETURN(type, __sc_ret, __sc_err)
3083 #undef _syscall0
3084 #define _syscall0(type, name) \
3085 type LSS_NAME(name)(void) { \
3086 LSS_BODY(0, type, name); \
3087 }
3088 #undef _syscall1
3089 #define _syscall1(type, name, type1, arg1) \
3090 type LSS_NAME(name)(type1 arg1) { \
3091 LSS_BODY(1, type, name, arg1); \
3092 }
3093 #undef _syscall2
3094 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3095 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3096 LSS_BODY(2, type, name, arg1, arg2); \
3097 }
3098 #undef _syscall3
3099 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3100 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3101 LSS_BODY(3, type, name, arg1, arg2, arg3); \
3102 }
3103 #undef _syscall4
3104 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3105 type4, arg4) \
3106 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3107 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
3108 }
3109 #undef _syscall5
3110 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3111 type4, arg4, type5, arg5) \
3112 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3113 type5 arg5) { \
3114 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
3115 }
3116 #undef _syscall6
3117 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3118 type4, arg4, type5, arg5, type6, arg6) \
3119 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3120 type5 arg5, type6 arg6) { \
3121 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
3122 }
3123 /* clone function adapted from glibc 2.3.6 clone.S */
3124 /* TODO(csilvers): consider wrapping some args up in a struct, like we
3125 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
3126 */
3127 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3128 int flags, void *arg, int *parent_tidptr,
3129 void *newtls, int *child_tidptr) {
3130 long __ret, __err;
3131 {
3132 register int (*__fn)(void *) __asm__ ("r8") = fn;
3133 register void *__cstack __asm__ ("r4") = child_stack;
3134 register int __flags __asm__ ("r3") = flags;
3135 register void * __arg __asm__ ("r9") = arg;
3136 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
3137 register void * __newtls __asm__ ("r6") = newtls;
3138 register int * __ctidptr __asm__ ("r7") = child_tidptr;
3139 __asm__ __volatile__(
3140 /* check for fn == NULL
3141 * and child_stack == NULL
3142 */
3143 "cmpwi cr0, %6, 0\n\t"
3144 "cmpwi cr1, %7, 0\n\t"
3145 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
3146 "beq- cr0, 1f\n\t"
3147
3148 /* set up stack frame for child */
3149 "clrrwi %7, %7, 4\n\t"
3150 "li 0, 0\n\t"
3151 "stwu 0, -16(%7)\n\t"
3152
3153 /* fn, arg, child_stack are saved across the syscall: r28-30 */
3154 "mr 28, %6\n\t"
3155 "mr 29, %7\n\t"
3156 "mr 27, %9\n\t"
3157
3158 /* syscall */
3159 "li 0, %4\n\t"
3160 /* flags already in r3
3161 * child_stack already in r4
3162 * ptidptr already in r5
3163 * newtls already in r6
3164 * ctidptr already in r7
3165 */
3166 "sc\n\t"
3167
3168 /* Test if syscall was successful */
3169 "cmpwi cr1, 3, 0\n\t"
3170 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
3171 "bne- cr1, 1f\n\t"
3172
3173 /* Do the function call */
3174 "mtctr 28\n\t"
3175 "mr 3, 27\n\t"
3176 "bctrl\n\t"
3177
3178 /* Call _exit(r3) */
3179 "li 0, %5\n\t"
3180 "sc\n\t"
3181
3182 /* Return to parent */
3183 "1:\n"
3184 "mfcr %1\n\t"
3185 "mr %0, 3\n\t"
3186 : "=r" (__ret), "=r" (__err)
3187 : "0" (-1), "1" (EINVAL),
3188 "i" (__NR_clone), "i" (__NR_exit),
3189 "r" (__fn), "r" (__cstack), "r" (__flags),
3190 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
3191 "r" (__ctidptr)
3192 : "cr0", "cr1", "memory", "ctr",
3193 "r0", "r29", "r27", "r28");
3194 }
3195 LSS_RETURN(int, __ret, __err);
3196 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003197 #elif defined(__s390__)
3198 #undef LSS_REG
3199 #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
3200 #undef LSS_BODY
3201 #define LSS_BODY(type, name, args...) \
3202 register unsigned long __nr __asm__("r1") \
3203 = (unsigned long)(__NR_##name); \
3204 register long __res_r2 __asm__("r2"); \
3205 long __res; \
3206 __asm__ __volatile__ \
3207 ("svc 0\n\t" \
3208 : "=d"(__res_r2) \
3209 : "d"(__nr), ## args \
3210 : "memory"); \
3211 __res = __res_r2; \
3212 LSS_RETURN(type, __res)
3213 #undef _syscall0
3214 #define _syscall0(type, name) \
3215 type LSS_NAME(name)(void) { \
3216 LSS_BODY(type, name); \
3217 }
3218 #undef _syscall1
3219 #define _syscall1(type, name, type1, arg1) \
3220 type LSS_NAME(name)(type1 arg1) { \
3221 LSS_REG(2, arg1); \
3222 LSS_BODY(type, name, "0"(__r2)); \
3223 }
3224 #undef _syscall2
3225 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3226 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3227 LSS_REG(2, arg1); LSS_REG(3, arg2); \
3228 LSS_BODY(type, name, "0"(__r2), "d"(__r3)); \
3229 }
3230 #undef _syscall3
3231 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3232 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3233 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3234 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4)); \
3235 }
3236 #undef _syscall4
3237 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3238 type4, arg4) \
3239 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3240 type4 arg4) { \
3241 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3242 LSS_REG(5, arg4); \
3243 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3244 "d"(__r5)); \
3245 }
3246 #undef _syscall5
3247 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3248 type4, arg4, type5, arg5) \
3249 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3250 type4 arg4, type5 arg5) { \
3251 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3252 LSS_REG(5, arg4); LSS_REG(6, arg5); \
3253 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3254 "d"(__r5), "d"(__r6)); \
3255 }
3256 #undef _syscall6
3257 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3258 type4, arg4, type5, arg5, type6, arg6) \
3259 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3260 type4 arg4, type5 arg5, type6 arg6) { \
3261 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3262 LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
3263 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3264 "d"(__r5), "d"(__r6), "d"(__r7)); \
3265 }
3266 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3267 int flags, void *arg, int *parent_tidptr,
3268 void *newtls, int *child_tidptr) {
3269 long __ret;
3270 {
3271 register int (*__fn)(void *) __asm__ ("r1") = fn;
3272 register void *__cstack __asm__ ("r2") = child_stack;
3273 register int __flags __asm__ ("r3") = flags;
3274 register void *__arg __asm__ ("r0") = arg;
3275 register int *__ptidptr __asm__ ("r4") = parent_tidptr;
3276 register void *__newtls __asm__ ("r6") = newtls;
3277 register int *__ctidptr __asm__ ("r5") = child_tidptr;
3278 __asm__ __volatile__ (
3279 #ifndef __s390x__
3280 /* arg already in r0 */
3281 "ltr %4, %4\n\t" /* check fn, which is already in r1 */
3282 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3283 "ltr %5, %5\n\t" /* check child_stack, which is already in r2 */
3284 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3285 /* flags already in r3 */
3286 /* parent_tidptr already in r4 */
3287 /* child_tidptr already in r5 */
3288 /* newtls already in r6 */
3289 "svc %2\n\t" /* invoke clone syscall */
3290 "ltr %0,%%r2\n\t" /* load return code into __ret and test */
3291 "jnz 1f\n\t" /* return to parent if non-zero */
3292 /* start child thread */
3293 "lr %%r2, %7\n\t" /* set first parameter to void *arg */
3294 "ahi %%r15, -96\n\t" /* make room on the stack for the save area */
3295 "xc 0(4,%%r15), 0(%%r15)\n\t"
3296 "basr %%r14, %4\n\t" /* jump to fn */
3297 "svc %3\n" /* invoke exit syscall */
3298 "1:\n"
3299 #else
3300 /* arg already in r0 */
3301 "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
3302 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3303 "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
3304 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3305 /* flags already in r3 */
3306 /* parent_tidptr already in r4 */
3307 /* child_tidptr already in r5 */
3308 /* newtls already in r6 */
3309 "svc %2\n\t" /* invoke clone syscall */
3310 "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
3311 "jnz 1f\n\t" /* return to parent if non-zero */
3312 /* start child thread */
3313 "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
3314 "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
3315 "xc 0(8,%%r15), 0(%%r15)\n\t"
3316 "basr %%r14, %4\n\t" /* jump to fn */
3317 "svc %3\n" /* invoke exit syscall */
3318 "1:\n"
3319 #endif
3320 : "=r" (__ret)
3321 : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
3322 "d" (__fn), "d" (__cstack), "d" (__flags), "d" (__arg),
3323 "d" (__ptidptr), "d" (__newtls), "d" (__ctidptr)
3324 : "cc", "r14", "memory"
3325 );
3326 }
3327 LSS_RETURN(int, __ret);
3328 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003329 #endif
3330 #define __NR__exit __NR_exit
3331 #define __NR__gettid __NR_gettid
3332 #define __NR__mremap __NR_mremap
phosek@chromium.orga9c02722013-08-16 17:31:42 +00003333 LSS_INLINE _syscall1(void *, brk, void *, e)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003334 LSS_INLINE _syscall1(int, chdir, const char *,p)
3335 LSS_INLINE _syscall1(int, close, int, f)
3336 LSS_INLINE _syscall2(int, clock_getres, int, c,
3337 struct kernel_timespec*, t)
3338 LSS_INLINE _syscall2(int, clock_gettime, int, c,
3339 struct kernel_timespec*, t)
3340 LSS_INLINE _syscall1(int, dup, int, f)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003341 #if !defined(__aarch64__)
3342 // The dup2 syscall has been deprecated on aarch64. We polyfill it below.
3343 LSS_INLINE _syscall2(int, dup2, int, s,
3344 int, d)
3345 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003346 LSS_INLINE _syscall3(int, execve, const char*, f,
3347 const char*const*,a,const char*const*, e)
3348 LSS_INLINE _syscall1(int, _exit, int, e)
3349 LSS_INLINE _syscall1(int, exit_group, int, e)
3350 LSS_INLINE _syscall3(int, fcntl, int, f,
3351 int, c, long, a)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003352 #if !defined(__aarch64__)
3353 // The fork syscall has been deprecated on aarch64. We polyfill it below.
3354 LSS_INLINE _syscall0(pid_t, fork)
3355 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003356 LSS_INLINE _syscall2(int, fstat, int, f,
3357 struct kernel_stat*, b)
3358 LSS_INLINE _syscall2(int, fstatfs, int, f,
3359 struct kernel_statfs*, b)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003360 #if defined(__x86_64__)
3361 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3362 LSS_INLINE int LSS_NAME(ftruncate)(int f, off_t l) {
3363 LSS_BODY(2, int, ftruncate, LSS_SYSCALL_ARG(f), (uint64_t)(l));
3364 }
3365 #else
3366 LSS_INLINE _syscall2(int, ftruncate, int, f,
3367 off_t, l)
3368 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003369 LSS_INLINE _syscall4(int, futex, int*, a,
3370 int, o, int, v,
3371 struct kernel_timespec*, t)
3372 LSS_INLINE _syscall3(int, getdents, int, f,
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003373 struct kernel_dirent*, d, int, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003374 LSS_INLINE _syscall3(int, getdents64, int, f,
3375 struct kernel_dirent64*, d, int, c)
3376 LSS_INLINE _syscall0(gid_t, getegid)
3377 LSS_INLINE _syscall0(uid_t, geteuid)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003378 #if !defined(__aarch64__)
3379 // The getgprp syscall has been deprecated on aarch64.
3380 LSS_INLINE _syscall0(pid_t, getpgrp)
3381 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003382 LSS_INLINE _syscall0(pid_t, getpid)
3383 LSS_INLINE _syscall0(pid_t, getppid)
3384 LSS_INLINE _syscall2(int, getpriority, int, a,
3385 int, b)
3386 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
3387 gid_t *, e, gid_t *, s)
3388 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
3389 uid_t *, e, uid_t *, s)
3390#if !defined(__ARM_EABI__)
3391 LSS_INLINE _syscall2(int, getrlimit, int, r,
3392 struct kernel_rlimit*, l)
3393#endif
3394 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
3395 LSS_INLINE _syscall0(pid_t, _gettid)
3396 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
3397 void*, tz)
3398 LSS_INLINE _syscall5(int, setxattr, const char *,p,
3399 const char *, n, const void *,v,
3400 size_t, s, int, f)
3401 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
3402 const char *, n, const void *,v,
3403 size_t, s, int, f)
3404 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
3405 const char *, n, void *, v, size_t, s)
3406 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
3407 const char *, n, void *, v, size_t, s)
3408 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
3409 char *, l, size_t, s)
3410 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
3411 char *, l, size_t, s)
3412 LSS_INLINE _syscall3(int, ioctl, int, d,
3413 int, r, void *, a)
3414 LSS_INLINE _syscall2(int, ioprio_get, int, which,
3415 int, who)
3416 LSS_INLINE _syscall3(int, ioprio_set, int, which,
3417 int, who, int, ioprio)
3418 LSS_INLINE _syscall2(int, kill, pid_t, p,
3419 int, s)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003420 #if defined(__x86_64__)
3421 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3422 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
3423 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
3424 LSS_SYSCALL_ARG(w));
3425 }
3426 #else
3427 LSS_INLINE _syscall3(off_t, lseek, int, f,
3428 off_t, o, int, w)
3429 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003430 LSS_INLINE _syscall2(int, munmap, void*, s,
3431 size_t, l)
3432 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
3433 unsigned long, n, void **,g, int *, d,
3434 int *, s, int, f)
3435 LSS_INLINE _syscall3(int, mprotect, const void *,a,
3436 size_t, l, int, p)
3437 LSS_INLINE _syscall5(void*, _mremap, void*, o,
3438 size_t, os, size_t, ns,
3439 unsigned long, f, void *, a)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003440 #if !defined(__aarch64__)
3441 // The open and poll syscalls have been deprecated on aarch64. We polyfill
3442 // them below.
3443 LSS_INLINE _syscall3(int, open, const char*, p,
3444 int, f, int, m)
3445 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
3446 unsigned int, n, int, t)
3447 #endif
mseaborn@chromium.orge6c76822013-08-31 00:08:44 +00003448 LSS_INLINE _syscall5(int, prctl, int, option,
3449 unsigned long, arg2,
3450 unsigned long, arg3,
3451 unsigned long, arg4,
3452 unsigned long, arg5)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003453 LSS_INLINE _syscall4(long, ptrace, int, r,
3454 pid_t, p, void *, a, void *, d)
3455 #if defined(__NR_quotactl)
3456 // Defined on x86_64 / i386 only
3457 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
3458 int, id, caddr_t, addr)
3459 #endif
3460 LSS_INLINE _syscall3(ssize_t, read, int, f,
3461 void *, b, size_t, c)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003462 #if !defined(__aarch64__)
3463 // The readlink syscall has been deprecated on aarch64. We polyfill below.
3464 LSS_INLINE _syscall3(int, readlink, const char*, p,
3465 char*, b, size_t, s)
3466 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003467 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
3468 const struct kernel_sigaction*, a,
3469 struct kernel_sigaction*, o, size_t, c)
3470 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
3471 size_t, c)
3472 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
3473 const struct kernel_sigset_t*, s,
3474 struct kernel_sigset_t*, o, size_t, c)
3475 LSS_INLINE _syscall2(int, rt_sigsuspend,
3476 const struct kernel_sigset_t*, s, size_t, c)
3477 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
3478 unsigned int, l, unsigned long *, m)
3479 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
3480 unsigned int, l, unsigned long *, m)
3481 LSS_INLINE _syscall0(int, sched_yield)
3482 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
3483 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
3484 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
3485 LSS_INLINE _syscall1(int, setuid, uid_t, u)
3486 LSS_INLINE _syscall1(int, setgid, gid_t, g)
3487 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
3488 pid_t, g)
3489 LSS_INLINE _syscall3(int, setpriority, int, a,
3490 int, b, int, p)
3491 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
3492 gid_t, e, gid_t, s)
3493 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
3494 uid_t, e, uid_t, s)
3495 LSS_INLINE _syscall2(int, setrlimit, int, r,
3496 const struct kernel_rlimit*, l)
3497 LSS_INLINE _syscall0(pid_t, setsid)
3498 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
3499 const stack_t*, o)
3500 #if defined(__NR_sigreturn)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003501 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003502 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003503 #if !defined(__aarch64__)
3504 // The stat syscall has been deprecated on aarch64. We polyfill it below.
3505 LSS_INLINE _syscall2(int, stat, const char*, f,
3506 struct kernel_stat*, b)
3507 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003508 LSS_INLINE _syscall2(int, statfs, const char*, f,
3509 struct kernel_statfs*, b)
3510 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
3511 pid_t, t, int, s)
3512 LSS_INLINE _syscall2(int, tkill, pid_t, p,
3513 int, s)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003514 #if !defined(__aarch64__)
3515 // The unlink syscall has been deprecated on aarch64. We polyfill it below.
3516 LSS_INLINE _syscall1(int, unlink, const char*, f)
3517 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003518 LSS_INLINE _syscall3(ssize_t, write, int, f,
3519 const void *, b, size_t, c)
3520 LSS_INLINE _syscall3(ssize_t, writev, int, f,
3521 const struct kernel_iovec*, v, size_t, c)
3522 #if defined(__NR_getcpu)
3523 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00003524 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003525 #endif
3526 #if defined(__x86_64__) || \
3527 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
3528 LSS_INLINE _syscall3(int, recvmsg, int, s,
3529 struct kernel_msghdr*, m, int, f)
3530 LSS_INLINE _syscall3(int, sendmsg, int, s,
3531 const struct kernel_msghdr*, m, int, f)
3532 LSS_INLINE _syscall6(int, sendto, int, s,
3533 const void*, m, size_t, l,
3534 int, f,
3535 const struct kernel_sockaddr*, a, int, t)
3536 LSS_INLINE _syscall2(int, shutdown, int, s,
3537 int, h)
3538 LSS_INLINE _syscall3(int, socket, int, d,
3539 int, t, int, p)
3540 LSS_INLINE _syscall4(int, socketpair, int, d,
3541 int, t, int, p, int*, s)
3542 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003543 #if defined(__NR_fadvise64)
3544 #if defined(__x86_64__)
3545 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3546 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset, loff_t len,
3547 int advice) {
3548 LSS_BODY(4, int, fadvise64, LSS_SYSCALL_ARG(fd), (uint64_t)(offset),
3549 (uint64_t)(len), LSS_SYSCALL_ARG(advice));
3550 }
3551 #else
3552 LSS_INLINE _syscall4(int, fadvise64,
3553 int, fd, loff_t, offset, loff_t, len, int, advice)
3554 #endif
3555 #elif defined(__i386__)
3556 #define __NR__fadvise64_64 __NR_fadvise64_64
3557 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
3558 unsigned, offset_lo, unsigned, offset_hi,
3559 unsigned, len_lo, unsigned, len_hi,
3560 int, advice)
3561
3562 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3563 loff_t len, int advice) {
3564 return LSS_NAME(_fadvise64_64)(fd,
3565 (unsigned)offset, (unsigned)(offset >>32),
3566 (unsigned)len, (unsigned)(len >> 32),
3567 advice);
3568 }
3569
3570 #elif defined(__s390__) && !defined(__s390x__)
3571 #define __NR__fadvise64_64 __NR_fadvise64_64
3572 struct kernel_fadvise64_64_args {
3573 int fd;
3574 long long offset;
3575 long long len;
3576 int advice;
3577 };
3578
3579 LSS_INLINE _syscall1(int, _fadvise64_64,
3580 struct kernel_fadvise64_64_args *args)
3581
3582 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3583 loff_t len, int advice) {
3584 struct kernel_fadvise64_64_args args = { fd, offset, len, advice };
3585 return LSS_NAME(_fadvise64_64)(&args);
3586 }
3587 #endif
3588 #if defined(__NR_fallocate)
3589 #if defined(__x86_64__)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003590 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3591 LSS_INLINE int LSS_NAME(fallocate)(int f, int mode, loff_t offset,
3592 loff_t len) {
3593 LSS_BODY(4, int, fallocate, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(mode),
3594 (uint64_t)(offset), (uint64_t)(len));
3595 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003596 #elif defined(__i386__) || (defined(__s390__) && !defined(__s390x__))
3597 #define __NR__fallocate __NR_fallocate
3598 LSS_INLINE _syscall6(int, _fallocate, int, fd,
3599 int, mode,
3600 unsigned, offset_lo, unsigned, offset_hi,
3601 unsigned, len_lo, unsigned, len_hi)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003602
Bryan Chan3f6478a2016-06-14 08:38:17 -04003603 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
3604 loff_t offset, loff_t len) {
3605 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
3606 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
3607 }
3608 #else
3609 LSS_INLINE _syscall4(int, fallocate,
3610 int, f, int, mode, loff_t, offset, loff_t, len)
3611 #endif
3612 #endif
3613 #if defined(__x86_64__) || defined(__s390x__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003614 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3615 gid_t *egid,
3616 gid_t *sgid) {
3617 return LSS_NAME(getresgid)(rgid, egid, sgid);
3618 }
3619
3620 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3621 uid_t *euid,
3622 uid_t *suid) {
3623 return LSS_NAME(getresuid)(ruid, euid, suid);
3624 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003625 LSS_INLINE _syscall4(int, newfstatat, int, d,
3626 const char *, p,
3627 struct kernel_stat*, b, int, f)
3628
3629 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3630 return LSS_NAME(setfsgid)(gid);
3631 }
3632
3633 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3634 return LSS_NAME(setfsuid)(uid);
3635 }
3636
3637 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3638 return LSS_NAME(setresgid)(rgid, egid, sgid);
3639 }
3640
3641 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3642 return LSS_NAME(setresuid)(ruid, euid, suid);
3643 }
3644
3645 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3646 const struct kernel_sigaction *act,
3647 struct kernel_sigaction *oldact) {
Bryan Chan3f6478a2016-06-14 08:38:17 -04003648 #if defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003649 /* On x86_64, the kernel requires us to always set our own
3650 * SA_RESTORER in order to be able to return from a signal handler.
3651 * This function must have a "magic" signature that the "gdb"
3652 * (and maybe the kernel?) can recognize.
3653 */
3654 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
3655 struct kernel_sigaction a = *act;
3656 a.sa_flags |= SA_RESTORER;
3657 a.sa_restorer = LSS_NAME(restore_rt)();
3658 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
3659 (KERNEL_NSIG+7)/8);
Bryan Chan3f6478a2016-06-14 08:38:17 -04003660 } else
3661 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003662 return LSS_NAME(rt_sigaction)(signum, act, oldact,
3663 (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003664 }
3665
3666 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3667 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3668 }
3669
3670 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3671 const struct kernel_sigset_t *set,
3672 struct kernel_sigset_t *oldset) {
3673 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3674 }
3675
3676 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3677 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3678 }
3679 #endif
3680 #if defined(__x86_64__) || defined(__ARM_ARCH_3__) || \
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003681 defined(__ARM_EABI__) || defined(__aarch64__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04003682 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32) || \
3683 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003684 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
3685 int*, s, int, o,
3686 struct kernel_rusage*, r)
3687
3688 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){
3689 return LSS_NAME(wait4)(pid, status, options, 0);
3690 }
3691 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003692 #if defined(__NR_openat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003693 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003694 #endif
3695 #if defined(__NR_unlinkat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003696 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
3697 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003698 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3699 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003700 #define __NR__getresgid32 __NR_getresgid32
3701 #define __NR__getresuid32 __NR_getresuid32
3702 #define __NR__setfsgid32 __NR_setfsgid32
3703 #define __NR__setfsuid32 __NR_setfsuid32
3704 #define __NR__setresgid32 __NR_setresgid32
3705 #define __NR__setresuid32 __NR_setresuid32
3706#if defined(__ARM_EABI__)
3707 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
3708 struct kernel_rlimit*, l)
3709#endif
3710 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
3711 gid_t *, e, gid_t *, s)
3712 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
3713 uid_t *, e, uid_t *, s)
3714 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
3715 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
3716 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
3717 gid_t, e, gid_t, s)
3718 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
3719 uid_t, e, uid_t, s)
3720
3721 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3722 gid_t *egid,
3723 gid_t *sgid) {
3724 int rc;
3725 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
3726 LSS_ERRNO == ENOSYS) {
3727 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
3728 return EFAULT;
3729 }
3730 // Clear the high bits first, since getresgid only sets 16 bits
3731 *rgid = *egid = *sgid = 0;
3732 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
3733 }
3734 return rc;
3735 }
3736
3737 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3738 uid_t *euid,
3739 uid_t *suid) {
3740 int rc;
3741 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
3742 LSS_ERRNO == ENOSYS) {
3743 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
3744 return EFAULT;
3745 }
3746 // Clear the high bits first, since getresuid only sets 16 bits
3747 *ruid = *euid = *suid = 0;
3748 rc = LSS_NAME(getresuid)(ruid, euid, suid);
3749 }
3750 return rc;
3751 }
3752
3753 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3754 int rc;
3755 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
3756 LSS_ERRNO == ENOSYS) {
3757 if ((unsigned int)gid & ~0xFFFFu) {
3758 rc = EINVAL;
3759 } else {
3760 rc = LSS_NAME(setfsgid)(gid);
3761 }
3762 }
3763 return rc;
3764 }
3765
3766 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3767 int rc;
3768 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
3769 LSS_ERRNO == ENOSYS) {
3770 if ((unsigned int)uid & ~0xFFFFu) {
3771 rc = EINVAL;
3772 } else {
3773 rc = LSS_NAME(setfsuid)(uid);
3774 }
3775 }
3776 return rc;
3777 }
3778
3779 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3780 int rc;
3781 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
3782 LSS_ERRNO == ENOSYS) {
3783 if ((unsigned int)rgid & ~0xFFFFu ||
3784 (unsigned int)egid & ~0xFFFFu ||
3785 (unsigned int)sgid & ~0xFFFFu) {
3786 rc = EINVAL;
3787 } else {
3788 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
3789 }
3790 }
3791 return rc;
3792 }
3793
3794 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3795 int rc;
3796 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
3797 LSS_ERRNO == ENOSYS) {
3798 if ((unsigned int)ruid & ~0xFFFFu ||
3799 (unsigned int)euid & ~0xFFFFu ||
3800 (unsigned int)suid & ~0xFFFFu) {
3801 rc = EINVAL;
3802 } else {
3803 rc = LSS_NAME(setresuid)(ruid, euid, suid);
3804 }
3805 }
3806 return rc;
3807 }
3808 #endif
3809 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
3810 memset(&set->sig, 0, sizeof(set->sig));
3811 return 0;
3812 }
3813
3814 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
3815 memset(&set->sig, -1, sizeof(set->sig));
3816 return 0;
3817 }
3818
3819 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
3820 int signum) {
3821 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3822 LSS_ERRNO = EINVAL;
3823 return -1;
3824 } else {
3825 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3826 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
3827 return 0;
3828 }
3829 }
3830
3831 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
3832 int signum) {
3833 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3834 LSS_ERRNO = EINVAL;
3835 return -1;
3836 } else {
3837 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3838 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
3839 return 0;
3840 }
3841 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003842
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003843 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
3844 int signum) {
3845 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3846 LSS_ERRNO = EINVAL;
3847 return -1;
3848 } else {
3849 return !!(set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] &
3850 (1UL << ((signum - 1) % (8*sizeof(set->sig[0])))));
3851 }
3852 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003853 #if defined(__i386__) || \
3854 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3855 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
3856 defined(__PPC__) || \
3857 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003858 #define __NR__sigaction __NR_sigaction
3859 #define __NR__sigpending __NR_sigpending
3860 #define __NR__sigprocmask __NR_sigprocmask
3861 #define __NR__sigsuspend __NR_sigsuspend
3862 #define __NR__socketcall __NR_socketcall
3863 LSS_INLINE _syscall2(int, fstat64, int, f,
3864 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003865 LSS_INLINE _syscall5(int, _llseek, uint, fd,
3866 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003867 loff_t *, res, uint, wh)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003868#if defined(__s390__) && !defined(__s390x__)
3869 /* On s390, mmap2() arguments are passed in memory. */
3870 LSS_INLINE void* LSS_NAME(_mmap2)(void *s, size_t l, int p, int f, int d,
3871 off_t o) {
3872 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
3873 (unsigned long) p, (unsigned long) f,
3874 (unsigned long) d, (unsigned long) o };
3875 LSS_REG(2, buf);
3876 LSS_BODY(void*, mmap2, "0"(__r2));
3877 }
3878#else
3879 #define __NR__mmap2 __NR_mmap2
3880 LSS_INLINE _syscall6(void*, _mmap2, void*, s,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003881 size_t, l, int, p,
3882 int, f, int, d,
Bryan Chan3f6478a2016-06-14 08:38:17 -04003883 off_t, o)
3884#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003885 LSS_INLINE _syscall3(int, _sigaction, int, s,
3886 const struct kernel_old_sigaction*, a,
3887 struct kernel_old_sigaction*, o)
3888 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
3889 LSS_INLINE _syscall3(int, _sigprocmask, int, h,
3890 const unsigned long*, s,
3891 unsigned long*, o)
3892 #ifdef __PPC__
3893 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
3894 #else
3895 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
3896 int, b,
3897 unsigned long, s)
3898 #endif
3899 LSS_INLINE _syscall2(int, stat64, const char *, p,
3900 struct kernel_stat64 *, b)
3901
3902 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3903 const struct kernel_sigaction *act,
3904 struct kernel_sigaction *oldact) {
3905 int old_errno = LSS_ERRNO;
3906 int rc;
3907 struct kernel_sigaction a;
3908 if (act != NULL) {
3909 a = *act;
3910 #ifdef __i386__
3911 /* On i386, the kernel requires us to always set our own
3912 * SA_RESTORER when using realtime signals. Otherwise, it does not
3913 * know how to return from a signal handler. This function must have
3914 * a "magic" signature that the "gdb" (and maybe the kernel?) can
3915 * recognize.
3916 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
3917 * using non-realtime signals.
3918 *
3919 * TODO: Test whether ARM needs a restorer
3920 */
3921 if (!(a.sa_flags & SA_RESTORER)) {
3922 a.sa_flags |= SA_RESTORER;
3923 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
3924 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
3925 }
3926 #endif
3927 }
3928 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
3929 (KERNEL_NSIG+7)/8);
3930 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3931 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
3932 if (!act) {
3933 ptr_a = NULL;
3934 } else {
3935 oa.sa_handler_ = act->sa_handler_;
3936 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
3937 #ifndef __mips__
3938 oa.sa_restorer = act->sa_restorer;
3939 #endif
3940 oa.sa_flags = act->sa_flags;
3941 }
3942 if (!oldact) {
3943 ptr_oa = NULL;
3944 }
3945 LSS_ERRNO = old_errno;
3946 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
3947 if (rc == 0 && oldact) {
3948 if (act) {
3949 memcpy(oldact, act, sizeof(*act));
3950 } else {
3951 memset(oldact, 0, sizeof(*oldact));
3952 }
3953 oldact->sa_handler_ = ptr_oa->sa_handler_;
3954 oldact->sa_flags = ptr_oa->sa_flags;
3955 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
3956 #ifndef __mips__
3957 oldact->sa_restorer = ptr_oa->sa_restorer;
3958 #endif
3959 }
3960 }
3961 return rc;
3962 }
3963
3964 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3965 int old_errno = LSS_ERRNO;
3966 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3967 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3968 LSS_ERRNO = old_errno;
3969 LSS_NAME(sigemptyset)(set);
3970 rc = LSS_NAME(_sigpending)(&set->sig[0]);
3971 }
3972 return rc;
3973 }
3974
3975 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3976 const struct kernel_sigset_t *set,
3977 struct kernel_sigset_t *oldset) {
3978 int olderrno = LSS_ERRNO;
3979 int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3980 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3981 LSS_ERRNO = olderrno;
3982 if (oldset) {
3983 LSS_NAME(sigemptyset)(oldset);
3984 }
3985 rc = LSS_NAME(_sigprocmask)(how,
3986 set ? &set->sig[0] : NULL,
3987 oldset ? &oldset->sig[0] : NULL);
3988 }
3989 return rc;
3990 }
3991
3992 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3993 int olderrno = LSS_ERRNO;
3994 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3995 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3996 LSS_ERRNO = olderrno;
3997 rc = LSS_NAME(_sigsuspend)(
3998 #ifndef __PPC__
3999 set, 0,
4000 #endif
4001 set->sig[0]);
4002 }
4003 return rc;
4004 }
4005 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004006 #if defined(__i386__) || \
4007 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4008 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4009 defined(__PPC__) || \
4010 (defined(__s390__) && !defined(__s390x__))
4011 /* On these architectures, implement mmap() with mmap2(). */
4012 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4013 int64_t o) {
4014 if (o % 4096) {
4015 LSS_ERRNO = EINVAL;
4016 return (void *) -1;
4017 }
4018 return LSS_NAME(_mmap2)(s, l, p, f, d, (o / 4096));
4019 }
4020 #elif defined(__s390x__)
4021 /* On s390x, mmap() arguments are passed in memory. */
4022 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4023 int64_t o) {
4024 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4025 (unsigned long) p, (unsigned long) f,
4026 (unsigned long) d, (unsigned long) o };
4027 LSS_REG(2, buf);
4028 LSS_BODY(void*, mmap, "0"(__r2));
4029 }
4030 #elif defined(__x86_64__)
4031 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
4032 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4033 int64_t o) {
4034 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
4035 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
4036 LSS_SYSCALL_ARG(d), (uint64_t)(o));
4037 }
4038 #else
4039 /* Remaining 64-bit architectures. */
4040 LSS_INLINE _syscall6(void*, mmap, void*, addr, size_t, length, int, prot,
4041 int, flags, int, fd, int64_t, offset)
4042 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004043 #if defined(__PPC__)
4044 #undef LSS_SC_LOADARGS_0
4045 #define LSS_SC_LOADARGS_0(dummy...)
4046 #undef LSS_SC_LOADARGS_1
4047 #define LSS_SC_LOADARGS_1(arg1) \
4048 __sc_4 = (unsigned long) (arg1)
4049 #undef LSS_SC_LOADARGS_2
4050 #define LSS_SC_LOADARGS_2(arg1, arg2) \
4051 LSS_SC_LOADARGS_1(arg1); \
4052 __sc_5 = (unsigned long) (arg2)
4053 #undef LSS_SC_LOADARGS_3
4054 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
4055 LSS_SC_LOADARGS_2(arg1, arg2); \
4056 __sc_6 = (unsigned long) (arg3)
4057 #undef LSS_SC_LOADARGS_4
4058 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
4059 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
4060 __sc_7 = (unsigned long) (arg4)
4061 #undef LSS_SC_LOADARGS_5
4062 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
4063 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
4064 __sc_8 = (unsigned long) (arg5)
4065 #undef LSS_SC_BODY
4066 #define LSS_SC_BODY(nr, type, opt, args...) \
4067 long __sc_ret, __sc_err; \
4068 { \
4069 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
4070 register unsigned long __sc_3 __asm__ ("r3") = opt; \
4071 register unsigned long __sc_4 __asm__ ("r4"); \
4072 register unsigned long __sc_5 __asm__ ("r5"); \
4073 register unsigned long __sc_6 __asm__ ("r6"); \
4074 register unsigned long __sc_7 __asm__ ("r7"); \
4075 register unsigned long __sc_8 __asm__ ("r8"); \
4076 LSS_SC_LOADARGS_##nr(args); \
4077 __asm__ __volatile__ \
4078 ("stwu 1, -48(1)\n\t" \
4079 "stw 4, 20(1)\n\t" \
4080 "stw 5, 24(1)\n\t" \
4081 "stw 6, 28(1)\n\t" \
4082 "stw 7, 32(1)\n\t" \
4083 "stw 8, 36(1)\n\t" \
4084 "addi 4, 1, 20\n\t" \
4085 "sc\n\t" \
4086 "mfcr %0" \
4087 : "=&r" (__sc_0), \
4088 "=&r" (__sc_3), "=&r" (__sc_4), \
4089 "=&r" (__sc_5), "=&r" (__sc_6), \
4090 "=&r" (__sc_7), "=&r" (__sc_8) \
4091 : LSS_ASMINPUT_##nr \
4092 : "cr0", "ctr", "memory"); \
4093 __sc_ret = __sc_3; \
4094 __sc_err = __sc_0; \
4095 } \
4096 LSS_RETURN(type, __sc_ret, __sc_err)
4097
4098 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4099 int flags){
4100 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
4101 }
4102
4103 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4104 const struct kernel_msghdr *msg,
4105 int flags) {
4106 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
4107 }
4108
4109 // TODO(csilvers): why is this ifdef'ed out?
4110#if 0
4111 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4112 int flags,
4113 const struct kernel_sockaddr *to,
4114 unsigned int tolen) {
4115 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
4116 }
4117#endif
4118
4119 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4120 LSS_SC_BODY(2, int, 13, s, how);
4121 }
4122
4123 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4124 LSS_SC_BODY(3, int, 1, domain, type, protocol);
4125 }
4126
4127 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4128 int sv[2]) {
4129 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
4130 }
4131 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004132 #if defined(__ARM_EABI__) || defined (__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004133 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
4134 int, flags)
4135 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
4136 msg, int, flags)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004137 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
4138 int, flags, const struct kernel_sockaddr*, to,
4139 unsigned int, tolen)
4140 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
4141 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
4142 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
4143 int*, sv)
4144 #endif
4145 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004146 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4147 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004148 #define __NR__socketcall __NR_socketcall
4149 LSS_INLINE _syscall2(int, _socketcall, int, c,
4150 va_list, a)
4151 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
4152 int rc;
4153 va_list ap;
4154 va_start(ap, op);
4155 rc = LSS_NAME(_socketcall)(op, ap);
4156 va_end(ap);
4157 return rc;
4158 }
4159
4160 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4161 int flags){
4162 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
4163 }
4164
4165 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4166 const struct kernel_msghdr *msg,
4167 int flags) {
4168 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
4169 }
4170
4171 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4172 int flags,
4173 const struct kernel_sockaddr *to,
4174 unsigned int tolen) {
4175 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
4176 }
4177
4178 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4179 return LSS_NAME(socketcall)(13, s, how);
4180 }
4181
4182 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4183 return LSS_NAME(socketcall)(1, domain, type, protocol);
4184 }
4185
4186 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4187 int sv[2]) {
4188 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
4189 }
4190 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004191 #if defined(__NR_fstatat64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004192 LSS_INLINE _syscall4(int, fstatat64, int, d,
4193 const char *, p,
4194 struct kernel_stat64 *, b, int, f)
4195 #endif
4196 #if defined(__i386__) || defined(__PPC__) || \
4197 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
4198 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
4199 int*, s, int, o)
4200 #endif
4201 #if defined(__mips__)
4202 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
4203 * both file handles through CPU registers.
4204 */
4205 LSS_INLINE int LSS_NAME(pipe)(int *p) {
4206 register unsigned long __v0 __asm__("$2") = __NR_pipe;
4207 register unsigned long __v1 __asm__("$3");
4208 register unsigned long __r7 __asm__("$7");
4209 __asm__ __volatile__ ("syscall\n"
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00004210 : "=r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004211 : "0"(__v0)
4212 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004213 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004214 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004215 unsigned long __errnovalue = __v0;
4216 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004217 return -1;
4218 } else {
4219 p[0] = __v0;
4220 p[1] = __v1;
4221 return 0;
4222 }
4223 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004224 #elif !defined(__aarch64__)
4225 // The unlink syscall has been deprecated on aarch64. We polyfill it below.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004226 LSS_INLINE _syscall1(int, pipe, int *, p)
4227 #endif
4228 /* TODO(csilvers): see if ppc can/should support this as well */
4229 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004230 defined(__ARM_EABI__) || \
4231 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) || \
4232 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004233 #define __NR__statfs64 __NR_statfs64
4234 #define __NR__fstatfs64 __NR_fstatfs64
4235 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
4236 size_t, s,struct kernel_statfs64*, b)
4237 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
4238 size_t, s,struct kernel_statfs64*, b)
4239 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
4240 struct kernel_statfs64 *b) {
4241 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
4242 }
4243 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
4244 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
4245 }
4246 #endif
4247
4248 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
4249 extern char **environ;
4250 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
4251 }
4252
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004253 LSS_INLINE pid_t LSS_NAME(gettid)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004254 pid_t tid = LSS_NAME(_gettid)();
4255 if (tid != -1) {
4256 return tid;
4257 }
4258 return LSS_NAME(getpid)();
4259 }
4260
4261 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
4262 size_t new_size, int flags, ...) {
4263 va_list ap;
4264 void *new_address, *rc;
4265 va_start(ap, flags);
4266 new_address = va_arg(ap, void *);
4267 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
4268 flags, new_address);
4269 va_end(ap);
4270 return rc;
4271 }
4272
4273 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
4274 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
4275 * then sends job control signals to the real parent, rather than to
4276 * the tracer. We reduce the risk of this happening by starting a
4277 * whole new time slice, and then quickly sending a SIGCONT signal
4278 * right after detaching from the tracee.
4279 *
4280 * We use tkill to ensure that we only issue a wakeup for the thread being
4281 * detached. Large multi threaded apps can take a long time in the kernel
4282 * processing SIGCONT.
4283 */
4284 int rc, err;
4285 LSS_NAME(sched_yield)();
4286 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
4287 err = LSS_ERRNO;
4288 LSS_NAME(tkill)(pid, SIGCONT);
4289 /* Old systems don't have tkill */
4290 if (LSS_ERRNO == ENOSYS)
4291 LSS_NAME(kill)(pid, SIGCONT);
4292 LSS_ERRNO = err;
4293 return rc;
4294 }
4295
4296 LSS_INLINE int LSS_NAME(raise)(int sig) {
4297 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
4298 }
4299
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004300 LSS_INLINE int LSS_NAME(setpgrp)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004301 return LSS_NAME(setpgid)(0, 0);
4302 }
4303
4304 LSS_INLINE int LSS_NAME(sysconf)(int name) {
4305 extern int __getpagesize(void);
4306 switch (name) {
4307 case _SC_OPEN_MAX: {
4308 struct kernel_rlimit limit;
4309#if defined(__ARM_EABI__)
4310 return LSS_NAME(ugetrlimit)(RLIMIT_NOFILE, &limit) < 0
4311 ? 8192 : limit.rlim_cur;
4312#else
4313 return LSS_NAME(getrlimit)(RLIMIT_NOFILE, &limit) < 0
4314 ? 8192 : limit.rlim_cur;
4315#endif
4316 }
4317 case _SC_PAGESIZE:
4318 return __getpagesize();
4319 default:
4320 LSS_ERRNO = ENOSYS;
4321 return -1;
4322 }
4323 }
vapier@chromium.org2273e812013-04-01 17:52:44 +00004324 #if defined(__x86_64__)
4325 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4326 LSS_INLINE ssize_t LSS_NAME(pread64)(int f, void *b, size_t c, loff_t o) {
4327 LSS_BODY(4, ssize_t, pread64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4328 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4329 }
4330
4331 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int f, const void *b, size_t c,
4332 loff_t o) {
4333 LSS_BODY(4, ssize_t, pwrite64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4334 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4335 }
4336
4337 LSS_INLINE int LSS_NAME(readahead)(int f, loff_t o, unsigned c) {
4338 LSS_BODY(3, int, readahead, LSS_SYSCALL_ARG(f), (uint64_t)(o),
4339 LSS_SYSCALL_ARG(c));
4340 }
4341 #elif defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004342 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
4343 void *, b, size_t, c,
4344 loff_t, o)
4345 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
4346 const void *, b, size_t, c,
4347 loff_t, o)
4348 LSS_INLINE _syscall3(int, readahead, int, f,
4349 loff_t, o, unsigned, c)
4350 #else
4351 #define __NR__pread64 __NR_pread64
4352 #define __NR__pwrite64 __NR_pwrite64
4353 #define __NR__readahead __NR_readahead
mseaborn@chromium.org2c73abf2012-09-15 03:46:48 +00004354 #if defined(__ARM_EABI__) || defined(__mips__)
4355 /* On ARM and MIPS, a 64-bit parameter has to be in an even-odd register
4356 * pair. Hence these calls ignore their fourth argument (r3) so that their
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004357 * fifth and sixth make such a pair (r4,r5).
4358 */
4359 #define LSS_LLARG_PAD 0,
4360 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
4361 void *, b, size_t, c,
4362 unsigned, skip, unsigned, o1, unsigned, o2)
4363 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
4364 const void *, b, size_t, c,
4365 unsigned, skip, unsigned, o1, unsigned, o2)
4366 LSS_INLINE _syscall5(int, _readahead, int, f,
4367 unsigned, skip,
4368 unsigned, o1, unsigned, o2, size_t, c)
4369 #else
4370 #define LSS_LLARG_PAD
4371 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
4372 void *, b, size_t, c, unsigned, o1,
4373 unsigned, o2)
4374 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
4375 const void *, b, size_t, c, unsigned, o1,
4376 long, o2)
4377 LSS_INLINE _syscall4(int, _readahead, int, f,
4378 unsigned, o1, unsigned, o2, size_t, c)
4379 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004380 /* We force 64bit-wide parameters onto the stack, then access each
4381 * 32-bit component individually. This guarantees that we build the
4382 * correct parameters independent of the native byte-order of the
4383 * underlying architecture.
4384 */
4385 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
4386 loff_t off) {
4387 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004388 return LSS_NAME(_pread64)(fd, buf, count,
4389 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004390 }
4391 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
4392 size_t count, loff_t off) {
4393 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004394 return LSS_NAME(_pwrite64)(fd, buf, count,
4395 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004396 }
4397 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, int len) {
4398 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004399 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], len);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004400 }
4401 #endif
4402#endif
4403
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004404#if defined(__aarch64__)
4405 LSS_INLINE _syscall3(int, dup3, int, s, int, d, int, f)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004406 LSS_INLINE _syscall4(int, newfstatat, int, dirfd, const char *, pathname,
4407 struct kernel_stat *, buf, int, flags)
4408 LSS_INLINE _syscall2(int, pipe2, int *, pipefd, int, flags)
4409 LSS_INLINE _syscall5(int, ppoll, struct kernel_pollfd *, u,
4410 unsigned int, n, const struct kernel_timespec *, t,
vapier@chromium.orgdb1e07d2015-01-16 14:14:42 +00004411 const struct kernel_sigset_t *, sigmask, size_t, s)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004412 LSS_INLINE _syscall4(int, readlinkat, int, d, const char *, p, char *, b,
4413 size_t, s)
4414#endif
4415
4416/*
4417 * Polyfills for deprecated syscalls.
4418 */
4419
4420#if defined(__aarch64__)
4421 LSS_INLINE int LSS_NAME(dup2)(int s, int d) {
4422 return LSS_NAME(dup3)(s, d, 0);
4423 }
4424
4425 LSS_INLINE int LSS_NAME(open)(const char *pathname, int flags, int mode) {
4426 return LSS_NAME(openat)(AT_FDCWD, pathname, flags, mode);
4427 }
4428
4429 LSS_INLINE int LSS_NAME(unlink)(const char *pathname) {
4430 return LSS_NAME(unlinkat)(AT_FDCWD, pathname, 0);
4431 }
4432
4433 LSS_INLINE int LSS_NAME(readlink)(const char *pathname, char *buffer,
4434 size_t size) {
4435 return LSS_NAME(readlinkat)(AT_FDCWD, pathname, buffer, size);
4436 }
4437
4438 LSS_INLINE pid_t LSS_NAME(pipe)(int *pipefd) {
4439 return LSS_NAME(pipe2)(pipefd, 0);
4440 }
4441
4442 LSS_INLINE int LSS_NAME(poll)(struct kernel_pollfd *fds, unsigned int nfds,
4443 int timeout) {
4444 struct kernel_timespec timeout_ts;
4445 struct kernel_timespec *timeout_ts_p = NULL;
4446
4447 if (timeout >= 0) {
4448 timeout_ts.tv_sec = timeout / 1000;
4449 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
4450 timeout_ts_p = &timeout_ts;
4451 }
4452 return LSS_NAME(ppoll)(fds, nfds, timeout_ts_p, NULL, 0);
4453 }
4454
4455 LSS_INLINE int LSS_NAME(stat)(const char *pathname,
4456 struct kernel_stat *buf) {
4457 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, 0);
4458 }
4459
4460 LSS_INLINE pid_t LSS_NAME(fork)(void) {
4461 // No fork syscall on aarch64 - implement by means of the clone syscall.
4462 // Note that this does not reset glibc's cached view of the PID/TID, so
4463 // some glibc interfaces might go wrong in the forked subprocess.
4464 int flags = SIGCHLD;
4465 void *child_stack = NULL;
4466 void *parent_tidptr = NULL;
4467 void *newtls = NULL;
4468 void *child_tidptr = NULL;
4469
4470 LSS_REG(0, flags);
4471 LSS_REG(1, child_stack);
4472 LSS_REG(2, parent_tidptr);
4473 LSS_REG(3, newtls);
4474 LSS_REG(4, child_tidptr);
4475 LSS_BODY(pid_t, clone, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),
4476 "r"(__r4));
4477 }
4478#endif
4479
mseaborn@chromium.orgca749372012-09-05 18:26:20 +00004480#ifdef __ANDROID__
4481 /* These restore the original values of these macros saved by the
4482 * corresponding #pragma push_macro near the top of this file. */
4483# pragma pop_macro("stat64")
4484# pragma pop_macro("fstat64")
4485# pragma pop_macro("lstat64")
4486#endif
4487
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004488#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
4489}
4490#endif
4491
4492#endif
4493#endif