blob: f74eda143d671b67786873deb9604eb9209644bd [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) \
730 ((~(int)(pid) << 3) | (int)(clock))
731#endif
732#ifndef MAKE_THREAD_CPUCLOCK
733#define MAKE_THREAD_CPUCLOCK(tid, clock) \
734 ((~(int)(tid) << 3) | (int)((clock) | CPUCLOCK_PERTHREAD_MASK))
735#endif
736
737#ifndef FUTEX_WAIT
738#define FUTEX_WAIT 0
739#endif
740#ifndef FUTEX_WAKE
741#define FUTEX_WAKE 1
742#endif
743#ifndef FUTEX_FD
744#define FUTEX_FD 2
745#endif
746#ifndef FUTEX_REQUEUE
747#define FUTEX_REQUEUE 3
748#endif
749#ifndef FUTEX_CMP_REQUEUE
750#define FUTEX_CMP_REQUEUE 4
751#endif
752#ifndef FUTEX_WAKE_OP
753#define FUTEX_WAKE_OP 5
754#endif
755#ifndef FUTEX_LOCK_PI
756#define FUTEX_LOCK_PI 6
757#endif
758#ifndef FUTEX_UNLOCK_PI
759#define FUTEX_UNLOCK_PI 7
760#endif
761#ifndef FUTEX_TRYLOCK_PI
762#define FUTEX_TRYLOCK_PI 8
763#endif
764#ifndef FUTEX_PRIVATE_FLAG
765#define FUTEX_PRIVATE_FLAG 128
766#endif
767#ifndef FUTEX_CMD_MASK
768#define FUTEX_CMD_MASK ~FUTEX_PRIVATE_FLAG
769#endif
770#ifndef FUTEX_WAIT_PRIVATE
771#define FUTEX_WAIT_PRIVATE (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
772#endif
773#ifndef FUTEX_WAKE_PRIVATE
774#define FUTEX_WAKE_PRIVATE (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
775#endif
776#ifndef FUTEX_REQUEUE_PRIVATE
777#define FUTEX_REQUEUE_PRIVATE (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
778#endif
779#ifndef FUTEX_CMP_REQUEUE_PRIVATE
780#define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
781#endif
782#ifndef FUTEX_WAKE_OP_PRIVATE
783#define FUTEX_WAKE_OP_PRIVATE (FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
784#endif
785#ifndef FUTEX_LOCK_PI_PRIVATE
786#define FUTEX_LOCK_PI_PRIVATE (FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
787#endif
788#ifndef FUTEX_UNLOCK_PI_PRIVATE
789#define FUTEX_UNLOCK_PI_PRIVATE (FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
790#endif
791#ifndef FUTEX_TRYLOCK_PI_PRIVATE
792#define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
793#endif
794
795
796#if defined(__x86_64__)
797#ifndef ARCH_SET_GS
798#define ARCH_SET_GS 0x1001
799#endif
800#ifndef ARCH_GET_GS
801#define ARCH_GET_GS 0x1004
802#endif
803#endif
804
805#if defined(__i386__)
806#ifndef __NR_quotactl
807#define __NR_quotactl 131
808#endif
809#ifndef __NR_setresuid
810#define __NR_setresuid 164
811#define __NR_getresuid 165
812#define __NR_setresgid 170
813#define __NR_getresgid 171
814#endif
815#ifndef __NR_rt_sigaction
816#define __NR_rt_sigreturn 173
817#define __NR_rt_sigaction 174
818#define __NR_rt_sigprocmask 175
819#define __NR_rt_sigpending 176
820#define __NR_rt_sigsuspend 179
821#endif
822#ifndef __NR_pread64
823#define __NR_pread64 180
824#endif
825#ifndef __NR_pwrite64
826#define __NR_pwrite64 181
827#endif
828#ifndef __NR_ugetrlimit
829#define __NR_ugetrlimit 191
830#endif
831#ifndef __NR_stat64
832#define __NR_stat64 195
833#endif
834#ifndef __NR_fstat64
835#define __NR_fstat64 197
836#endif
837#ifndef __NR_setresuid32
838#define __NR_setresuid32 208
839#define __NR_getresuid32 209
840#define __NR_setresgid32 210
841#define __NR_getresgid32 211
842#endif
843#ifndef __NR_setfsuid32
844#define __NR_setfsuid32 215
845#define __NR_setfsgid32 216
846#endif
847#ifndef __NR_getdents64
848#define __NR_getdents64 220
849#endif
850#ifndef __NR_gettid
851#define __NR_gettid 224
852#endif
853#ifndef __NR_readahead
854#define __NR_readahead 225
855#endif
856#ifndef __NR_setxattr
857#define __NR_setxattr 226
858#endif
859#ifndef __NR_lsetxattr
860#define __NR_lsetxattr 227
861#endif
862#ifndef __NR_getxattr
863#define __NR_getxattr 229
864#endif
865#ifndef __NR_lgetxattr
866#define __NR_lgetxattr 230
867#endif
868#ifndef __NR_listxattr
869#define __NR_listxattr 232
870#endif
871#ifndef __NR_llistxattr
872#define __NR_llistxattr 233
873#endif
874#ifndef __NR_tkill
875#define __NR_tkill 238
876#endif
877#ifndef __NR_futex
878#define __NR_futex 240
879#endif
880#ifndef __NR_sched_setaffinity
881#define __NR_sched_setaffinity 241
882#define __NR_sched_getaffinity 242
883#endif
884#ifndef __NR_set_tid_address
885#define __NR_set_tid_address 258
886#endif
887#ifndef __NR_clock_gettime
888#define __NR_clock_gettime 265
889#endif
890#ifndef __NR_clock_getres
891#define __NR_clock_getres 266
892#endif
893#ifndef __NR_statfs64
894#define __NR_statfs64 268
895#endif
896#ifndef __NR_fstatfs64
897#define __NR_fstatfs64 269
898#endif
899#ifndef __NR_fadvise64_64
900#define __NR_fadvise64_64 272
901#endif
902#ifndef __NR_ioprio_set
903#define __NR_ioprio_set 289
904#endif
905#ifndef __NR_ioprio_get
906#define __NR_ioprio_get 290
907#endif
908#ifndef __NR_openat
909#define __NR_openat 295
910#endif
911#ifndef __NR_fstatat64
912#define __NR_fstatat64 300
913#endif
914#ifndef __NR_unlinkat
915#define __NR_unlinkat 301
916#endif
917#ifndef __NR_move_pages
918#define __NR_move_pages 317
919#endif
920#ifndef __NR_getcpu
921#define __NR_getcpu 318
922#endif
923#ifndef __NR_fallocate
924#define __NR_fallocate 324
925#endif
926/* End of i386 definitions */
927#elif defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
928#ifndef __NR_setresuid
929#define __NR_setresuid (__NR_SYSCALL_BASE + 164)
930#define __NR_getresuid (__NR_SYSCALL_BASE + 165)
931#define __NR_setresgid (__NR_SYSCALL_BASE + 170)
932#define __NR_getresgid (__NR_SYSCALL_BASE + 171)
933#endif
934#ifndef __NR_rt_sigaction
935#define __NR_rt_sigreturn (__NR_SYSCALL_BASE + 173)
936#define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
937#define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
938#define __NR_rt_sigpending (__NR_SYSCALL_BASE + 176)
939#define __NR_rt_sigsuspend (__NR_SYSCALL_BASE + 179)
940#endif
941#ifndef __NR_pread64
942#define __NR_pread64 (__NR_SYSCALL_BASE + 180)
943#endif
944#ifndef __NR_pwrite64
945#define __NR_pwrite64 (__NR_SYSCALL_BASE + 181)
946#endif
947#ifndef __NR_ugetrlimit
948#define __NR_ugetrlimit (__NR_SYSCALL_BASE + 191)
949#endif
950#ifndef __NR_stat64
951#define __NR_stat64 (__NR_SYSCALL_BASE + 195)
952#endif
953#ifndef __NR_fstat64
954#define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
955#endif
956#ifndef __NR_setresuid32
957#define __NR_setresuid32 (__NR_SYSCALL_BASE + 208)
958#define __NR_getresuid32 (__NR_SYSCALL_BASE + 209)
959#define __NR_setresgid32 (__NR_SYSCALL_BASE + 210)
960#define __NR_getresgid32 (__NR_SYSCALL_BASE + 211)
961#endif
962#ifndef __NR_setfsuid32
963#define __NR_setfsuid32 (__NR_SYSCALL_BASE + 215)
964#define __NR_setfsgid32 (__NR_SYSCALL_BASE + 216)
965#endif
966#ifndef __NR_getdents64
967#define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
968#endif
969#ifndef __NR_gettid
970#define __NR_gettid (__NR_SYSCALL_BASE + 224)
971#endif
972#ifndef __NR_readahead
973#define __NR_readahead (__NR_SYSCALL_BASE + 225)
974#endif
975#ifndef __NR_setxattr
976#define __NR_setxattr (__NR_SYSCALL_BASE + 226)
977#endif
978#ifndef __NR_lsetxattr
979#define __NR_lsetxattr (__NR_SYSCALL_BASE + 227)
980#endif
981#ifndef __NR_getxattr
982#define __NR_getxattr (__NR_SYSCALL_BASE + 229)
983#endif
984#ifndef __NR_lgetxattr
985#define __NR_lgetxattr (__NR_SYSCALL_BASE + 230)
986#endif
987#ifndef __NR_listxattr
988#define __NR_listxattr (__NR_SYSCALL_BASE + 232)
989#endif
990#ifndef __NR_llistxattr
991#define __NR_llistxattr (__NR_SYSCALL_BASE + 233)
992#endif
993#ifndef __NR_tkill
994#define __NR_tkill (__NR_SYSCALL_BASE + 238)
995#endif
996#ifndef __NR_futex
997#define __NR_futex (__NR_SYSCALL_BASE + 240)
998#endif
999#ifndef __NR_sched_setaffinity
1000#define __NR_sched_setaffinity (__NR_SYSCALL_BASE + 241)
1001#define __NR_sched_getaffinity (__NR_SYSCALL_BASE + 242)
1002#endif
1003#ifndef __NR_set_tid_address
1004#define __NR_set_tid_address (__NR_SYSCALL_BASE + 256)
1005#endif
1006#ifndef __NR_clock_gettime
1007#define __NR_clock_gettime (__NR_SYSCALL_BASE + 263)
1008#endif
1009#ifndef __NR_clock_getres
1010#define __NR_clock_getres (__NR_SYSCALL_BASE + 264)
1011#endif
1012#ifndef __NR_statfs64
1013#define __NR_statfs64 (__NR_SYSCALL_BASE + 266)
1014#endif
1015#ifndef __NR_fstatfs64
1016#define __NR_fstatfs64 (__NR_SYSCALL_BASE + 267)
1017#endif
1018#ifndef __NR_ioprio_set
1019#define __NR_ioprio_set (__NR_SYSCALL_BASE + 314)
1020#endif
1021#ifndef __NR_ioprio_get
1022#define __NR_ioprio_get (__NR_SYSCALL_BASE + 315)
1023#endif
1024#ifndef __NR_move_pages
1025#define __NR_move_pages (__NR_SYSCALL_BASE + 344)
1026#endif
1027#ifndef __NR_getcpu
1028#define __NR_getcpu (__NR_SYSCALL_BASE + 345)
1029#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04001030/* End of ARM 3/EABI definitions */
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001031#elif defined(__aarch64__)
1032#ifndef __NR_setxattr
1033#define __NR_setxattr 5
1034#endif
1035#ifndef __NR_lsetxattr
1036#define __NR_lsetxattr 6
1037#endif
1038#ifndef __NR_getxattr
1039#define __NR_getxattr 8
1040#endif
1041#ifndef __NR_lgetxattr
1042#define __NR_lgetxattr 9
1043#endif
1044#ifndef __NR_listxattr
1045#define __NR_listxattr 11
1046#endif
1047#ifndef __NR_llistxattr
1048#define __NR_llistxattr 12
1049#endif
1050#ifndef __NR_ioprio_set
1051#define __NR_ioprio_set 30
1052#endif
1053#ifndef __NR_ioprio_get
1054#define __NR_ioprio_get 31
1055#endif
1056#ifndef __NR_unlinkat
1057#define __NR_unlinkat 35
1058#endif
1059#ifndef __NR_fallocate
1060#define __NR_fallocate 47
1061#endif
1062#ifndef __NR_openat
1063#define __NR_openat 56
1064#endif
1065#ifndef __NR_quotactl
1066#define __NR_quotactl 60
1067#endif
1068#ifndef __NR_getdents64
1069#define __NR_getdents64 61
1070#endif
1071#ifndef __NR_getdents
1072#define __NR_getdents __NR_getdents64
1073#endif
1074#ifndef __NR_pread64
1075#define __NR_pread64 67
1076#endif
1077#ifndef __NR_pwrite64
1078#define __NR_pwrite64 68
1079#endif
1080#ifndef __NR_ppoll
1081#define __NR_ppoll 73
1082#endif
1083#ifndef __NR_readlinkat
1084#define __NR_readlinkat 78
1085#endif
1086#ifndef __NR_newfstatat
1087#define __NR_newfstatat 79
1088#endif
1089#ifndef __NR_set_tid_address
1090#define __NR_set_tid_address 96
1091#endif
1092#ifndef __NR_futex
1093#define __NR_futex 98
1094#endif
1095#ifndef __NR_clock_gettime
1096#define __NR_clock_gettime 113
1097#endif
1098#ifndef __NR_clock_getres
1099#define __NR_clock_getres 114
1100#endif
1101#ifndef __NR_sched_setaffinity
1102#define __NR_sched_setaffinity 122
1103#define __NR_sched_getaffinity 123
1104#endif
1105#ifndef __NR_tkill
1106#define __NR_tkill 130
1107#endif
1108#ifndef __NR_setresuid
1109#define __NR_setresuid 147
1110#define __NR_getresuid 148
1111#define __NR_setresgid 149
1112#define __NR_getresgid 150
1113#endif
1114#ifndef __NR_gettid
1115#define __NR_gettid 178
1116#endif
1117#ifndef __NR_readahead
1118#define __NR_readahead 213
1119#endif
1120#ifndef __NR_fadvise64
1121#define __NR_fadvise64 223
1122#endif
1123#ifndef __NR_move_pages
1124#define __NR_move_pages 239
1125#endif
1126/* End of aarch64 definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001127#elif defined(__x86_64__)
1128#ifndef __NR_pread64
1129#define __NR_pread64 17
1130#endif
1131#ifndef __NR_pwrite64
1132#define __NR_pwrite64 18
1133#endif
1134#ifndef __NR_setresuid
1135#define __NR_setresuid 117
1136#define __NR_getresuid 118
1137#define __NR_setresgid 119
1138#define __NR_getresgid 120
1139#endif
1140#ifndef __NR_quotactl
1141#define __NR_quotactl 179
1142#endif
1143#ifndef __NR_gettid
1144#define __NR_gettid 186
1145#endif
1146#ifndef __NR_readahead
1147#define __NR_readahead 187
1148#endif
1149#ifndef __NR_setxattr
1150#define __NR_setxattr 188
1151#endif
1152#ifndef __NR_lsetxattr
1153#define __NR_lsetxattr 189
1154#endif
1155#ifndef __NR_getxattr
1156#define __NR_getxattr 191
1157#endif
1158#ifndef __NR_lgetxattr
1159#define __NR_lgetxattr 192
1160#endif
1161#ifndef __NR_listxattr
1162#define __NR_listxattr 194
1163#endif
1164#ifndef __NR_llistxattr
1165#define __NR_llistxattr 195
1166#endif
1167#ifndef __NR_tkill
1168#define __NR_tkill 200
1169#endif
1170#ifndef __NR_futex
1171#define __NR_futex 202
1172#endif
1173#ifndef __NR_sched_setaffinity
1174#define __NR_sched_setaffinity 203
1175#define __NR_sched_getaffinity 204
1176#endif
1177#ifndef __NR_getdents64
1178#define __NR_getdents64 217
1179#endif
1180#ifndef __NR_set_tid_address
1181#define __NR_set_tid_address 218
1182#endif
1183#ifndef __NR_fadvise64
1184#define __NR_fadvise64 221
1185#endif
1186#ifndef __NR_clock_gettime
1187#define __NR_clock_gettime 228
1188#endif
1189#ifndef __NR_clock_getres
1190#define __NR_clock_getres 229
1191#endif
1192#ifndef __NR_ioprio_set
1193#define __NR_ioprio_set 251
1194#endif
1195#ifndef __NR_ioprio_get
1196#define __NR_ioprio_get 252
1197#endif
1198#ifndef __NR_openat
1199#define __NR_openat 257
1200#endif
1201#ifndef __NR_newfstatat
1202#define __NR_newfstatat 262
1203#endif
1204#ifndef __NR_unlinkat
1205#define __NR_unlinkat 263
1206#endif
1207#ifndef __NR_move_pages
1208#define __NR_move_pages 279
1209#endif
1210#ifndef __NR_fallocate
1211#define __NR_fallocate 285
1212#endif
1213/* End of x86-64 definitions */
1214#elif defined(__mips__)
1215#if _MIPS_SIM == _MIPS_SIM_ABI32
1216#ifndef __NR_setresuid
1217#define __NR_setresuid (__NR_Linux + 185)
1218#define __NR_getresuid (__NR_Linux + 186)
1219#define __NR_setresgid (__NR_Linux + 190)
1220#define __NR_getresgid (__NR_Linux + 191)
1221#endif
1222#ifndef __NR_rt_sigaction
1223#define __NR_rt_sigreturn (__NR_Linux + 193)
1224#define __NR_rt_sigaction (__NR_Linux + 194)
1225#define __NR_rt_sigprocmask (__NR_Linux + 195)
1226#define __NR_rt_sigpending (__NR_Linux + 196)
1227#define __NR_rt_sigsuspend (__NR_Linux + 199)
1228#endif
1229#ifndef __NR_pread64
1230#define __NR_pread64 (__NR_Linux + 200)
1231#endif
1232#ifndef __NR_pwrite64
1233#define __NR_pwrite64 (__NR_Linux + 201)
1234#endif
1235#ifndef __NR_stat64
1236#define __NR_stat64 (__NR_Linux + 213)
1237#endif
1238#ifndef __NR_fstat64
1239#define __NR_fstat64 (__NR_Linux + 215)
1240#endif
1241#ifndef __NR_getdents64
1242#define __NR_getdents64 (__NR_Linux + 219)
1243#endif
1244#ifndef __NR_gettid
1245#define __NR_gettid (__NR_Linux + 222)
1246#endif
1247#ifndef __NR_readahead
1248#define __NR_readahead (__NR_Linux + 223)
1249#endif
1250#ifndef __NR_setxattr
1251#define __NR_setxattr (__NR_Linux + 224)
1252#endif
1253#ifndef __NR_lsetxattr
1254#define __NR_lsetxattr (__NR_Linux + 225)
1255#endif
1256#ifndef __NR_getxattr
1257#define __NR_getxattr (__NR_Linux + 227)
1258#endif
1259#ifndef __NR_lgetxattr
1260#define __NR_lgetxattr (__NR_Linux + 228)
1261#endif
1262#ifndef __NR_listxattr
1263#define __NR_listxattr (__NR_Linux + 230)
1264#endif
1265#ifndef __NR_llistxattr
1266#define __NR_llistxattr (__NR_Linux + 231)
1267#endif
1268#ifndef __NR_tkill
1269#define __NR_tkill (__NR_Linux + 236)
1270#endif
1271#ifndef __NR_futex
1272#define __NR_futex (__NR_Linux + 238)
1273#endif
1274#ifndef __NR_sched_setaffinity
1275#define __NR_sched_setaffinity (__NR_Linux + 239)
1276#define __NR_sched_getaffinity (__NR_Linux + 240)
1277#endif
1278#ifndef __NR_set_tid_address
1279#define __NR_set_tid_address (__NR_Linux + 252)
1280#endif
1281#ifndef __NR_statfs64
1282#define __NR_statfs64 (__NR_Linux + 255)
1283#endif
1284#ifndef __NR_fstatfs64
1285#define __NR_fstatfs64 (__NR_Linux + 256)
1286#endif
1287#ifndef __NR_clock_gettime
1288#define __NR_clock_gettime (__NR_Linux + 263)
1289#endif
1290#ifndef __NR_clock_getres
1291#define __NR_clock_getres (__NR_Linux + 264)
1292#endif
1293#ifndef __NR_openat
1294#define __NR_openat (__NR_Linux + 288)
1295#endif
1296#ifndef __NR_fstatat
1297#define __NR_fstatat (__NR_Linux + 293)
1298#endif
1299#ifndef __NR_unlinkat
1300#define __NR_unlinkat (__NR_Linux + 294)
1301#endif
1302#ifndef __NR_move_pages
1303#define __NR_move_pages (__NR_Linux + 308)
1304#endif
1305#ifndef __NR_getcpu
1306#define __NR_getcpu (__NR_Linux + 312)
1307#endif
1308#ifndef __NR_ioprio_set
1309#define __NR_ioprio_set (__NR_Linux + 314)
1310#endif
1311#ifndef __NR_ioprio_get
1312#define __NR_ioprio_get (__NR_Linux + 315)
1313#endif
1314/* End of MIPS (old 32bit API) definitions */
1315#elif _MIPS_SIM == _MIPS_SIM_ABI64
1316#ifndef __NR_pread64
1317#define __NR_pread64 (__NR_Linux + 16)
1318#endif
1319#ifndef __NR_pwrite64
1320#define __NR_pwrite64 (__NR_Linux + 17)
1321#endif
1322#ifndef __NR_setresuid
1323#define __NR_setresuid (__NR_Linux + 115)
1324#define __NR_getresuid (__NR_Linux + 116)
1325#define __NR_setresgid (__NR_Linux + 117)
1326#define __NR_getresgid (__NR_Linux + 118)
1327#endif
1328#ifndef __NR_gettid
1329#define __NR_gettid (__NR_Linux + 178)
1330#endif
1331#ifndef __NR_readahead
1332#define __NR_readahead (__NR_Linux + 179)
1333#endif
1334#ifndef __NR_setxattr
1335#define __NR_setxattr (__NR_Linux + 180)
1336#endif
1337#ifndef __NR_lsetxattr
1338#define __NR_lsetxattr (__NR_Linux + 181)
1339#endif
1340#ifndef __NR_getxattr
1341#define __NR_getxattr (__NR_Linux + 183)
1342#endif
1343#ifndef __NR_lgetxattr
1344#define __NR_lgetxattr (__NR_Linux + 184)
1345#endif
1346#ifndef __NR_listxattr
1347#define __NR_listxattr (__NR_Linux + 186)
1348#endif
1349#ifndef __NR_llistxattr
1350#define __NR_llistxattr (__NR_Linux + 187)
1351#endif
1352#ifndef __NR_tkill
1353#define __NR_tkill (__NR_Linux + 192)
1354#endif
1355#ifndef __NR_futex
1356#define __NR_futex (__NR_Linux + 194)
1357#endif
1358#ifndef __NR_sched_setaffinity
1359#define __NR_sched_setaffinity (__NR_Linux + 195)
1360#define __NR_sched_getaffinity (__NR_Linux + 196)
1361#endif
1362#ifndef __NR_set_tid_address
1363#define __NR_set_tid_address (__NR_Linux + 212)
1364#endif
1365#ifndef __NR_clock_gettime
1366#define __NR_clock_gettime (__NR_Linux + 222)
1367#endif
1368#ifndef __NR_clock_getres
1369#define __NR_clock_getres (__NR_Linux + 223)
1370#endif
1371#ifndef __NR_openat
1372#define __NR_openat (__NR_Linux + 247)
1373#endif
1374#ifndef __NR_fstatat
1375#define __NR_fstatat (__NR_Linux + 252)
1376#endif
1377#ifndef __NR_unlinkat
1378#define __NR_unlinkat (__NR_Linux + 253)
1379#endif
1380#ifndef __NR_move_pages
1381#define __NR_move_pages (__NR_Linux + 267)
1382#endif
1383#ifndef __NR_getcpu
1384#define __NR_getcpu (__NR_Linux + 271)
1385#endif
1386#ifndef __NR_ioprio_set
1387#define __NR_ioprio_set (__NR_Linux + 273)
1388#endif
1389#ifndef __NR_ioprio_get
1390#define __NR_ioprio_get (__NR_Linux + 274)
1391#endif
1392/* End of MIPS (64bit API) definitions */
1393#else
1394#ifndef __NR_setresuid
1395#define __NR_setresuid (__NR_Linux + 115)
1396#define __NR_getresuid (__NR_Linux + 116)
1397#define __NR_setresgid (__NR_Linux + 117)
1398#define __NR_getresgid (__NR_Linux + 118)
1399#endif
1400#ifndef __NR_gettid
1401#define __NR_gettid (__NR_Linux + 178)
1402#endif
1403#ifndef __NR_readahead
1404#define __NR_readahead (__NR_Linux + 179)
1405#endif
1406#ifndef __NR_setxattr
1407#define __NR_setxattr (__NR_Linux + 180)
1408#endif
1409#ifndef __NR_lsetxattr
1410#define __NR_lsetxattr (__NR_Linux + 181)
1411#endif
1412#ifndef __NR_getxattr
1413#define __NR_getxattr (__NR_Linux + 183)
1414#endif
1415#ifndef __NR_lgetxattr
1416#define __NR_lgetxattr (__NR_Linux + 184)
1417#endif
1418#ifndef __NR_listxattr
1419#define __NR_listxattr (__NR_Linux + 186)
1420#endif
1421#ifndef __NR_llistxattr
1422#define __NR_llistxattr (__NR_Linux + 187)
1423#endif
1424#ifndef __NR_tkill
1425#define __NR_tkill (__NR_Linux + 192)
1426#endif
1427#ifndef __NR_futex
1428#define __NR_futex (__NR_Linux + 194)
1429#endif
1430#ifndef __NR_sched_setaffinity
1431#define __NR_sched_setaffinity (__NR_Linux + 195)
1432#define __NR_sched_getaffinity (__NR_Linux + 196)
1433#endif
1434#ifndef __NR_set_tid_address
1435#define __NR_set_tid_address (__NR_Linux + 213)
1436#endif
1437#ifndef __NR_statfs64
1438#define __NR_statfs64 (__NR_Linux + 217)
1439#endif
1440#ifndef __NR_fstatfs64
1441#define __NR_fstatfs64 (__NR_Linux + 218)
1442#endif
1443#ifndef __NR_clock_gettime
1444#define __NR_clock_gettime (__NR_Linux + 226)
1445#endif
1446#ifndef __NR_clock_getres
1447#define __NR_clock_getres (__NR_Linux + 227)
1448#endif
1449#ifndef __NR_openat
1450#define __NR_openat (__NR_Linux + 251)
1451#endif
1452#ifndef __NR_fstatat
1453#define __NR_fstatat (__NR_Linux + 256)
1454#endif
1455#ifndef __NR_unlinkat
1456#define __NR_unlinkat (__NR_Linux + 257)
1457#endif
1458#ifndef __NR_move_pages
1459#define __NR_move_pages (__NR_Linux + 271)
1460#endif
1461#ifndef __NR_getcpu
1462#define __NR_getcpu (__NR_Linux + 275)
1463#endif
1464#ifndef __NR_ioprio_set
1465#define __NR_ioprio_set (__NR_Linux + 277)
1466#endif
1467#ifndef __NR_ioprio_get
1468#define __NR_ioprio_get (__NR_Linux + 278)
1469#endif
1470/* End of MIPS (new 32bit API) definitions */
1471#endif
1472/* End of MIPS definitions */
1473#elif defined(__PPC__)
1474#ifndef __NR_setfsuid
1475#define __NR_setfsuid 138
1476#define __NR_setfsgid 139
1477#endif
1478#ifndef __NR_setresuid
1479#define __NR_setresuid 164
1480#define __NR_getresuid 165
1481#define __NR_setresgid 169
1482#define __NR_getresgid 170
1483#endif
1484#ifndef __NR_rt_sigaction
1485#define __NR_rt_sigreturn 172
1486#define __NR_rt_sigaction 173
1487#define __NR_rt_sigprocmask 174
1488#define __NR_rt_sigpending 175
1489#define __NR_rt_sigsuspend 178
1490#endif
1491#ifndef __NR_pread64
1492#define __NR_pread64 179
1493#endif
1494#ifndef __NR_pwrite64
1495#define __NR_pwrite64 180
1496#endif
1497#ifndef __NR_ugetrlimit
1498#define __NR_ugetrlimit 190
1499#endif
1500#ifndef __NR_readahead
1501#define __NR_readahead 191
1502#endif
1503#ifndef __NR_stat64
1504#define __NR_stat64 195
1505#endif
1506#ifndef __NR_fstat64
1507#define __NR_fstat64 197
1508#endif
1509#ifndef __NR_getdents64
1510#define __NR_getdents64 202
1511#endif
1512#ifndef __NR_gettid
1513#define __NR_gettid 207
1514#endif
1515#ifndef __NR_tkill
1516#define __NR_tkill 208
1517#endif
1518#ifndef __NR_setxattr
1519#define __NR_setxattr 209
1520#endif
1521#ifndef __NR_lsetxattr
1522#define __NR_lsetxattr 210
1523#endif
1524#ifndef __NR_getxattr
1525#define __NR_getxattr 212
1526#endif
1527#ifndef __NR_lgetxattr
1528#define __NR_lgetxattr 213
1529#endif
1530#ifndef __NR_listxattr
1531#define __NR_listxattr 215
1532#endif
1533#ifndef __NR_llistxattr
1534#define __NR_llistxattr 216
1535#endif
1536#ifndef __NR_futex
1537#define __NR_futex 221
1538#endif
1539#ifndef __NR_sched_setaffinity
1540#define __NR_sched_setaffinity 222
1541#define __NR_sched_getaffinity 223
1542#endif
1543#ifndef __NR_set_tid_address
1544#define __NR_set_tid_address 232
1545#endif
1546#ifndef __NR_clock_gettime
1547#define __NR_clock_gettime 246
1548#endif
1549#ifndef __NR_clock_getres
1550#define __NR_clock_getres 247
1551#endif
1552#ifndef __NR_statfs64
1553#define __NR_statfs64 252
1554#endif
1555#ifndef __NR_fstatfs64
1556#define __NR_fstatfs64 253
1557#endif
1558#ifndef __NR_fadvise64_64
1559#define __NR_fadvise64_64 254
1560#endif
1561#ifndef __NR_ioprio_set
1562#define __NR_ioprio_set 273
1563#endif
1564#ifndef __NR_ioprio_get
1565#define __NR_ioprio_get 274
1566#endif
1567#ifndef __NR_openat
1568#define __NR_openat 286
1569#endif
1570#ifndef __NR_fstatat64
1571#define __NR_fstatat64 291
1572#endif
1573#ifndef __NR_unlinkat
1574#define __NR_unlinkat 292
1575#endif
1576#ifndef __NR_move_pages
1577#define __NR_move_pages 301
1578#endif
1579#ifndef __NR_getcpu
1580#define __NR_getcpu 302
1581#endif
1582/* End of powerpc defininitions */
Bryan Chan3f6478a2016-06-14 08:38:17 -04001583#elif defined(__s390__)
1584#ifndef __NR_quotactl
1585#define __NR_quotactl 131
1586#endif
1587#ifndef __NR_rt_sigreturn
1588#define __NR_rt_sigreturn 173
1589#endif
1590#ifndef __NR_rt_sigaction
1591#define __NR_rt_sigaction 174
1592#endif
1593#ifndef __NR_rt_sigprocmask
1594#define __NR_rt_sigprocmask 175
1595#endif
1596#ifndef __NR_rt_sigpending
1597#define __NR_rt_sigpending 176
1598#endif
1599#ifndef __NR_rt_sigsuspend
1600#define __NR_rt_sigsuspend 179
1601#endif
1602#ifndef __NR_pread64
1603#define __NR_pread64 180
1604#endif
1605#ifndef __NR_pwrite64
1606#define __NR_pwrite64 181
1607#endif
1608#ifndef __NR_getdents64
1609#define __NR_getdents64 220
1610#endif
1611#ifndef __NR_readahead
1612#define __NR_readahead 222
1613#endif
1614#ifndef __NR_setxattr
1615#define __NR_setxattr 224
1616#endif
1617#ifndef __NR_lsetxattr
1618#define __NR_lsetxattr 225
1619#endif
1620#ifndef __NR_getxattr
1621#define __NR_getxattr 227
1622#endif
1623#ifndef __NR_lgetxattr
1624#define __NR_lgetxattr 228
1625#endif
1626#ifndef __NR_listxattr
1627#define __NR_listxattr 230
1628#endif
1629#ifndef __NR_llistxattr
1630#define __NR_llistxattr 231
1631#endif
1632#ifndef __NR_gettid
1633#define __NR_gettid 236
1634#endif
1635#ifndef __NR_tkill
1636#define __NR_tkill 237
1637#endif
1638#ifndef __NR_futex
1639#define __NR_futex 238
1640#endif
1641#ifndef __NR_sched_setaffinity
1642#define __NR_sched_setaffinity 239
1643#endif
1644#ifndef __NR_sched_getaffinity
1645#define __NR_sched_getaffinity 240
1646#endif
1647#ifndef __NR_set_tid_address
1648#define __NR_set_tid_address 252
1649#endif
1650#ifndef __NR_clock_gettime
1651#define __NR_clock_gettime 260
1652#endif
1653#ifndef __NR_clock_getres
1654#define __NR_clock_getres 261
1655#endif
1656#ifndef __NR_statfs64
1657#define __NR_statfs64 265
1658#endif
1659#ifndef __NR_fstatfs64
1660#define __NR_fstatfs64 266
1661#endif
1662#ifndef __NR_ioprio_set
1663#define __NR_ioprio_set 282
1664#endif
1665#ifndef __NR_ioprio_get
1666#define __NR_ioprio_get 283
1667#endif
1668#ifndef __NR_openat
1669#define __NR_openat 288
1670#endif
1671#ifndef __NR_unlinkat
1672#define __NR_unlinkat 294
1673#endif
1674#ifndef __NR_move_pages
1675#define __NR_move_pages 310
1676#endif
1677#ifndef __NR_getcpu
1678#define __NR_getcpu 311
1679#endif
1680#ifndef __NR_fallocate
1681#define __NR_fallocate 314
1682#endif
1683/* Some syscalls are named/numbered differently between s390 and s390x. */
1684#ifdef __s390x__
1685# ifndef __NR_getrlimit
1686# define __NR_getrlimit 191
1687# endif
1688# ifndef __NR_setresuid
1689# define __NR_setresuid 208
1690# endif
1691# ifndef __NR_getresuid
1692# define __NR_getresuid 209
1693# endif
1694# ifndef __NR_setresgid
1695# define __NR_setresgid 210
1696# endif
1697# ifndef __NR_getresgid
1698# define __NR_getresgid 211
1699# endif
1700# ifndef __NR_setfsuid
1701# define __NR_setfsuid 215
1702# endif
1703# ifndef __NR_setfsgid
1704# define __NR_setfsgid 216
1705# endif
1706# ifndef __NR_fadvise64
1707# define __NR_fadvise64 253
1708# endif
1709# ifndef __NR_newfstatat
1710# define __NR_newfstatat 293
1711# endif
1712#else /* __s390x__ */
1713# ifndef __NR_getrlimit
1714# define __NR_getrlimit 76
1715# endif
1716# ifndef __NR_setfsuid
1717# define __NR_setfsuid 138
1718# endif
1719# ifndef __NR_setfsgid
1720# define __NR_setfsgid 139
1721# endif
1722# ifndef __NR_setresuid
1723# define __NR_setresuid 164
1724# endif
1725# ifndef __NR_getresuid
1726# define __NR_getresuid 165
1727# endif
1728# ifndef __NR_setresgid
1729# define __NR_setresgid 170
1730# endif
1731# ifndef __NR_getresgid
1732# define __NR_getresgid 171
1733# endif
1734# ifndef __NR_ugetrlimit
1735# define __NR_ugetrlimit 191
1736# endif
1737# ifndef __NR_mmap2
1738# define __NR_mmap2 192
1739# endif
1740# ifndef __NR_setresuid32
1741# define __NR_setresuid32 208
1742# endif
1743# ifndef __NR_getresuid32
1744# define __NR_getresuid32 209
1745# endif
1746# ifndef __NR_setresgid32
1747# define __NR_setresgid32 210
1748# endif
1749# ifndef __NR_getresgid32
1750# define __NR_getresgid32 211
1751# endif
1752# ifndef __NR_setfsuid32
1753# define __NR_setfsuid32 215
1754# endif
1755# ifndef __NR_setfsgid32
1756# define __NR_setfsgid32 216
1757# endif
1758# ifndef __NR_fadvise64_64
1759# define __NR_fadvise64_64 264
1760# endif
1761# ifndef __NR_fstatat64
1762# define __NR_fstatat64 293
1763# endif
1764#endif /* __s390__ */
1765/* End of s390/s390x definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001766#endif
1767
1768
1769/* After forking, we must make sure to only call system calls. */
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001770#if defined(__BOUNDED_POINTERS__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001771 #error "Need to port invocations of syscalls for bounded ptrs"
1772#else
1773 /* The core dumper and the thread lister get executed after threads
1774 * have been suspended. As a consequence, we cannot call any functions
1775 * that acquire locks. Unfortunately, libc wraps most system calls
1776 * (e.g. in order to implement pthread_atfork, and to make calls
1777 * cancellable), which means we cannot call these functions. Instead,
1778 * we have to call syscall() directly.
1779 */
1780 #undef LSS_ERRNO
1781 #ifdef SYS_ERRNO
1782 /* Allow the including file to override the location of errno. This can
1783 * be useful when using clone() with the CLONE_VM option.
1784 */
1785 #define LSS_ERRNO SYS_ERRNO
1786 #else
1787 #define LSS_ERRNO errno
1788 #endif
1789
1790 #undef LSS_INLINE
1791 #ifdef SYS_INLINE
1792 #define LSS_INLINE SYS_INLINE
1793 #else
1794 #define LSS_INLINE static inline
1795 #endif
1796
1797 /* Allow the including file to override the prefix used for all new
1798 * system calls. By default, it will be set to "sys_".
1799 */
1800 #undef LSS_NAME
1801 #ifndef SYS_PREFIX
1802 #define LSS_NAME(name) sys_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001803 #elif defined(SYS_PREFIX) && SYS_PREFIX < 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001804 #define LSS_NAME(name) name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001805 #elif defined(SYS_PREFIX) && SYS_PREFIX == 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001806 #define LSS_NAME(name) sys0_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001807 #elif defined(SYS_PREFIX) && SYS_PREFIX == 1
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001808 #define LSS_NAME(name) sys1_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001809 #elif defined(SYS_PREFIX) && SYS_PREFIX == 2
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001810 #define LSS_NAME(name) sys2_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001811 #elif defined(SYS_PREFIX) && SYS_PREFIX == 3
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001812 #define LSS_NAME(name) sys3_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001813 #elif defined(SYS_PREFIX) && SYS_PREFIX == 4
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001814 #define LSS_NAME(name) sys4_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001815 #elif defined(SYS_PREFIX) && SYS_PREFIX == 5
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001816 #define LSS_NAME(name) sys5_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001817 #elif defined(SYS_PREFIX) && SYS_PREFIX == 6
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001818 #define LSS_NAME(name) sys6_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001819 #elif defined(SYS_PREFIX) && SYS_PREFIX == 7
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001820 #define LSS_NAME(name) sys7_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001821 #elif defined(SYS_PREFIX) && SYS_PREFIX == 8
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001822 #define LSS_NAME(name) sys8_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001823 #elif defined(SYS_PREFIX) && SYS_PREFIX == 9
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001824 #define LSS_NAME(name) sys9_##name
1825 #endif
1826
1827 #undef LSS_RETURN
1828 #if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) \
Bryan Chan3f6478a2016-06-14 08:38:17 -04001829 || defined(__ARM_EABI__) || defined(__aarch64__) || defined(__s390__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001830 /* Failing system calls return a negative result in the range of
1831 * -1..-4095. These are "errno" values with the sign inverted.
1832 */
1833 #define LSS_RETURN(type, res) \
1834 do { \
1835 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
1836 LSS_ERRNO = -(res); \
1837 res = -1; \
1838 } \
1839 return (type) (res); \
1840 } while (0)
1841 #elif defined(__mips__)
1842 /* On MIPS, failing system calls return -1, and set errno in a
1843 * separate CPU register.
1844 */
1845 #define LSS_RETURN(type, res, err) \
1846 do { \
1847 if (err) { \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00001848 unsigned long __errnovalue = (res); \
1849 LSS_ERRNO = __errnovalue; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001850 res = -1; \
1851 } \
1852 return (type) (res); \
1853 } while (0)
1854 #elif defined(__PPC__)
1855 /* On PPC, failing system calls return -1, and set errno in a
1856 * separate CPU register. See linux/unistd.h.
1857 */
1858 #define LSS_RETURN(type, res, err) \
1859 do { \
1860 if (err & 0x10000000 ) { \
1861 LSS_ERRNO = (res); \
1862 res = -1; \
1863 } \
1864 return (type) (res); \
1865 } while (0)
1866 #endif
1867 #if defined(__i386__)
1868 /* In PIC mode (e.g. when building shared libraries), gcc for i386
1869 * reserves ebx. Unfortunately, most distribution ship with implementations
1870 * of _syscallX() which clobber ebx.
1871 * Also, most definitions of _syscallX() neglect to mark "memory" as being
1872 * clobbered. This causes problems with compilers, that do a better job
1873 * at optimizing across __asm__ calls.
1874 * So, we just have to redefine all of the _syscallX() macros.
1875 */
1876 #undef LSS_ENTRYPOINT
1877 #ifdef SYS_SYSCALL_ENTRYPOINT
1878 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
1879 void (**entrypoint)(void);
1880 asm volatile(".bss\n"
1881 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001882 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
1883 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001884 ".previous\n"
1885 /* This logically does 'lea "SYS_SYSCALL_ENTRYPOINT", %0' */
1886 "call 0f\n"
1887 "0:pop %0\n"
1888 "add $_GLOBAL_OFFSET_TABLE_+[.-0b], %0\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001889 "mov " SYS_SYSCALL_ENTRYPOINT "@GOT(%0), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001890 : "=r"(entrypoint));
1891 return entrypoint;
1892 }
1893
1894 #define LSS_ENTRYPOINT ".bss\n" \
1895 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001896 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
1897 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001898 ".previous\n" \
1899 /* Check the SYS_SYSCALL_ENTRYPOINT vector */ \
1900 "push %%eax\n" \
1901 "call 10000f\n" \
1902 "10000:pop %%eax\n" \
1903 "add $_GLOBAL_OFFSET_TABLE_+[.-10000b], %%eax\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001904 "mov " SYS_SYSCALL_ENTRYPOINT \
1905 "@GOT(%%eax), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001906 "mov 0(%%eax), %%eax\n" \
1907 "test %%eax, %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001908 "jz 10002f\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001909 "push %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001910 "call 10001f\n" \
1911 "10001:pop %%eax\n" \
1912 "add $(10003f-10001b), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001913 "xchg 4(%%esp), %%eax\n" \
1914 "ret\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001915 "10002:pop %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001916 "int $0x80\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001917 "10003:\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001918 #else
1919 #define LSS_ENTRYPOINT "int $0x80\n"
1920 #endif
1921 #undef LSS_BODY
1922 #define LSS_BODY(type,args...) \
1923 long __res; \
1924 __asm__ __volatile__("push %%ebx\n" \
1925 "movl %2,%%ebx\n" \
1926 LSS_ENTRYPOINT \
1927 "pop %%ebx" \
1928 args \
1929 : "esp", "memory"); \
1930 LSS_RETURN(type,__res)
1931 #undef _syscall0
1932 #define _syscall0(type,name) \
1933 type LSS_NAME(name)(void) { \
1934 long __res; \
1935 __asm__ volatile(LSS_ENTRYPOINT \
1936 : "=a" (__res) \
1937 : "0" (__NR_##name) \
1938 : "esp", "memory"); \
1939 LSS_RETURN(type,__res); \
1940 }
1941 #undef _syscall1
1942 #define _syscall1(type,name,type1,arg1) \
1943 type LSS_NAME(name)(type1 arg1) { \
1944 LSS_BODY(type, \
1945 : "=a" (__res) \
1946 : "0" (__NR_##name), "ri" ((long)(arg1))); \
1947 }
1948 #undef _syscall2
1949 #define _syscall2(type,name,type1,arg1,type2,arg2) \
1950 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
1951 LSS_BODY(type, \
1952 : "=a" (__res) \
1953 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
1954 }
1955 #undef _syscall3
1956 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
1957 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
1958 LSS_BODY(type, \
1959 : "=a" (__res) \
1960 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1961 "d" ((long)(arg3))); \
1962 }
1963 #undef _syscall4
1964 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1965 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1966 LSS_BODY(type, \
1967 : "=a" (__res) \
1968 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1969 "d" ((long)(arg3)),"S" ((long)(arg4))); \
1970 }
1971 #undef _syscall5
1972 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1973 type5,arg5) \
1974 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1975 type5 arg5) { \
1976 long __res; \
1977 __asm__ __volatile__("push %%ebx\n" \
1978 "movl %2,%%ebx\n" \
1979 "movl %1,%%eax\n" \
1980 LSS_ENTRYPOINT \
1981 "pop %%ebx" \
1982 : "=a" (__res) \
1983 : "i" (__NR_##name), "ri" ((long)(arg1)), \
1984 "c" ((long)(arg2)), "d" ((long)(arg3)), \
1985 "S" ((long)(arg4)), "D" ((long)(arg5)) \
1986 : "esp", "memory"); \
1987 LSS_RETURN(type,__res); \
1988 }
1989 #undef _syscall6
1990 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1991 type5,arg5,type6,arg6) \
1992 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1993 type5 arg5, type6 arg6) { \
1994 long __res; \
1995 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
1996 __asm__ __volatile__("push %%ebp\n" \
1997 "push %%ebx\n" \
mseaborn@chromium.orge96ade32012-10-27 17:47:38 +00001998 "movl 4(%2),%%ebp\n" \
1999 "movl 0(%2), %%ebx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002000 "movl %1,%%eax\n" \
2001 LSS_ENTRYPOINT \
2002 "pop %%ebx\n" \
2003 "pop %%ebp" \
2004 : "=a" (__res) \
2005 : "i" (__NR_##name), "0" ((long)(&__s)), \
2006 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2007 "S" ((long)(arg4)), "D" ((long)(arg5)) \
2008 : "esp", "memory"); \
2009 LSS_RETURN(type,__res); \
2010 }
2011 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2012 int flags, void *arg, int *parent_tidptr,
2013 void *newtls, int *child_tidptr) {
2014 long __res;
2015 __asm__ __volatile__(/* if (fn == NULL)
2016 * return -EINVAL;
2017 */
2018 "movl %3,%%ecx\n"
2019 "jecxz 1f\n"
2020
2021 /* if (child_stack == NULL)
2022 * return -EINVAL;
2023 */
2024 "movl %4,%%ecx\n"
2025 "jecxz 1f\n"
2026
2027 /* Set up alignment of the child stack:
2028 * child_stack = (child_stack & ~0xF) - 20;
2029 */
2030 "andl $-16,%%ecx\n"
2031 "subl $20,%%ecx\n"
2032
2033 /* Push "arg" and "fn" onto the stack that will be
2034 * used by the child.
2035 */
2036 "movl %6,%%eax\n"
2037 "movl %%eax,4(%%ecx)\n"
2038 "movl %3,%%eax\n"
2039 "movl %%eax,(%%ecx)\n"
2040
2041 /* %eax = syscall(%eax = __NR_clone,
2042 * %ebx = flags,
2043 * %ecx = child_stack,
2044 * %edx = parent_tidptr,
2045 * %esi = newtls,
2046 * %edi = child_tidptr)
2047 * Also, make sure that %ebx gets preserved as it is
2048 * used in PIC mode.
2049 */
2050 "movl %8,%%esi\n"
2051 "movl %7,%%edx\n"
2052 "movl %5,%%eax\n"
2053 "movl %9,%%edi\n"
2054 "pushl %%ebx\n"
2055 "movl %%eax,%%ebx\n"
2056 "movl %2,%%eax\n"
2057 LSS_ENTRYPOINT
2058
2059 /* In the parent: restore %ebx
2060 * In the child: move "fn" into %ebx
2061 */
2062 "popl %%ebx\n"
2063
2064 /* if (%eax != 0)
2065 * return %eax;
2066 */
2067 "test %%eax,%%eax\n"
2068 "jnz 1f\n"
2069
2070 /* In the child, now. Terminate frame pointer chain.
2071 */
2072 "movl $0,%%ebp\n"
2073
2074 /* Call "fn". "arg" is already on the stack.
2075 */
2076 "call *%%ebx\n"
2077
2078 /* Call _exit(%ebx). Unfortunately older versions
2079 * of gcc restrict the number of arguments that can
2080 * be passed to asm(). So, we need to hard-code the
2081 * system call number.
2082 */
2083 "movl %%eax,%%ebx\n"
2084 "movl $1,%%eax\n"
2085 LSS_ENTRYPOINT
2086
2087 /* Return to parent.
2088 */
2089 "1:\n"
2090 : "=a" (__res)
2091 : "0"(-EINVAL), "i"(__NR_clone),
2092 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
2093 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
2094 : "esp", "memory", "ecx", "edx", "esi", "edi");
2095 LSS_RETURN(int, __res);
2096 }
2097
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002098 LSS_INLINE _syscall1(int, set_thread_area, void *, u)
2099 LSS_INLINE _syscall1(int, get_thread_area, void *, u)
2100
2101 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2102 /* On i386, the kernel does not know how to return from a signal
2103 * handler. Instead, it relies on user space to provide a
2104 * restorer function that calls the {rt_,}sigreturn() system call.
2105 * Unfortunately, we cannot just reference the glibc version of this
2106 * function, as glibc goes out of its way to make it inaccessible.
2107 */
2108 void (*res)(void);
2109 __asm__ __volatile__("call 2f\n"
2110 "0:.align 16\n"
2111 "1:movl %1,%%eax\n"
2112 LSS_ENTRYPOINT
2113 "2:popl %0\n"
2114 "addl $(1b-0b),%0\n"
2115 : "=a" (res)
2116 : "i" (__NR_rt_sigreturn));
2117 return res;
2118 }
2119 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
2120 /* On i386, the kernel does not know how to return from a signal
2121 * handler. Instead, it relies on user space to provide a
2122 * restorer function that calls the {rt_,}sigreturn() system call.
2123 * Unfortunately, we cannot just reference the glibc version of this
2124 * function, as glibc goes out of its way to make it inaccessible.
2125 */
2126 void (*res)(void);
2127 __asm__ __volatile__("call 2f\n"
2128 "0:.align 16\n"
2129 "1:pop %%eax\n"
2130 "movl %1,%%eax\n"
2131 LSS_ENTRYPOINT
2132 "2:popl %0\n"
2133 "addl $(1b-0b),%0\n"
2134 : "=a" (res)
2135 : "i" (__NR_sigreturn));
2136 return res;
2137 }
2138 #elif defined(__x86_64__)
2139 /* There are no known problems with any of the _syscallX() macros
2140 * currently shipping for x86_64, but we still need to be able to define
2141 * our own version so that we can override the location of the errno
2142 * location (e.g. when using the clone() system call with the CLONE_VM
2143 * option).
2144 */
2145 #undef LSS_ENTRYPOINT
2146 #ifdef SYS_SYSCALL_ENTRYPOINT
2147 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2148 void (**entrypoint)(void);
2149 asm volatile(".bss\n"
2150 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002151 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2152 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002153 ".previous\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002154 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002155 : "=r"(entrypoint));
2156 return entrypoint;
2157 }
2158
2159 #define LSS_ENTRYPOINT \
2160 ".bss\n" \
2161 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002162 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2163 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002164 ".previous\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002165 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %%rcx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002166 "mov 0(%%rcx), %%rcx\n" \
2167 "test %%rcx, %%rcx\n" \
2168 "jz 10001f\n" \
2169 "call *%%rcx\n" \
2170 "jmp 10002f\n" \
2171 "10001:syscall\n" \
2172 "10002:\n"
2173
2174 #else
2175 #define LSS_ENTRYPOINT "syscall\n"
2176 #endif
vapier@chromium.org2273e812013-04-01 17:52:44 +00002177
2178 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
2179 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
2180 * sign extension. We can't cast pointers directly because those are
2181 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
2182 * to an integer of a different size.
2183 */
2184 #undef LSS_SYSCALL_ARG
2185 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
2186 #undef _LSS_RETURN
2187 #define _LSS_RETURN(type, res, cast) \
2188 do { \
2189 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
2190 LSS_ERRNO = -(res); \
2191 res = -1; \
2192 } \
2193 return (type)(cast)(res); \
2194 } while (0)
2195 #undef LSS_RETURN
2196 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
2197
2198 #undef _LSS_BODY
2199 #define _LSS_BODY(nr, type, name, cast, ...) \
2200 long long __res; \
2201 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
2202 : "=a" (__res) \
2203 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
2204 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
2205 _LSS_RETURN(type, __res, cast)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002206 #undef LSS_BODY
vapier@chromium.org2273e812013-04-01 17:52:44 +00002207 #define LSS_BODY(nr, type, name, args...) \
2208 _LSS_BODY(nr, type, name, uintptr_t, ## args)
2209
2210 #undef LSS_BODY_ASM0
2211 #undef LSS_BODY_ASM1
2212 #undef LSS_BODY_ASM2
2213 #undef LSS_BODY_ASM3
2214 #undef LSS_BODY_ASM4
2215 #undef LSS_BODY_ASM5
2216 #undef LSS_BODY_ASM6
2217 #define LSS_BODY_ASM0
2218 #define LSS_BODY_ASM1 LSS_BODY_ASM0
2219 #define LSS_BODY_ASM2 LSS_BODY_ASM1
2220 #define LSS_BODY_ASM3 LSS_BODY_ASM2
2221 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
2222 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
2223 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
2224
2225 #undef LSS_BODY_CLOBBER0
2226 #undef LSS_BODY_CLOBBER1
2227 #undef LSS_BODY_CLOBBER2
2228 #undef LSS_BODY_CLOBBER3
2229 #undef LSS_BODY_CLOBBER4
2230 #undef LSS_BODY_CLOBBER5
2231 #undef LSS_BODY_CLOBBER6
2232 #define LSS_BODY_CLOBBER0
2233 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
2234 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
2235 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
2236 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
2237 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
2238 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
2239
2240 #undef LSS_BODY_ARG0
2241 #undef LSS_BODY_ARG1
2242 #undef LSS_BODY_ARG2
2243 #undef LSS_BODY_ARG3
2244 #undef LSS_BODY_ARG4
2245 #undef LSS_BODY_ARG5
2246 #undef LSS_BODY_ARG6
2247 #define LSS_BODY_ARG0()
2248 #define LSS_BODY_ARG1(arg1) \
2249 LSS_BODY_ARG0(), "D" (arg1)
2250 #define LSS_BODY_ARG2(arg1, arg2) \
2251 LSS_BODY_ARG1(arg1), "S" (arg2)
2252 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
2253 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
2254 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
2255 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
2256 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
2257 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
2258 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
2259 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
2260
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002261 #undef _syscall0
2262 #define _syscall0(type,name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002263 type LSS_NAME(name)(void) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002264 LSS_BODY(0, type, name); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002265 }
2266 #undef _syscall1
2267 #define _syscall1(type,name,type1,arg1) \
2268 type LSS_NAME(name)(type1 arg1) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002269 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002270 }
2271 #undef _syscall2
2272 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2273 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002274 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002275 }
2276 #undef _syscall3
2277 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2278 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002279 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2280 LSS_SYSCALL_ARG(arg3)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002281 }
2282 #undef _syscall4
2283 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2284 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002285 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2286 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002287 }
2288 #undef _syscall5
2289 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2290 type5,arg5) \
2291 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2292 type5 arg5) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002293 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2294 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2295 LSS_SYSCALL_ARG(arg5)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002296 }
2297 #undef _syscall6
2298 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2299 type5,arg5,type6,arg6) \
2300 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2301 type5 arg5, type6 arg6) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002302 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2303 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2304 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002305 }
2306 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2307 int flags, void *arg, int *parent_tidptr,
2308 void *newtls, int *child_tidptr) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00002309 long long __res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002310 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002311 __asm__ __volatile__(/* if (fn == NULL)
2312 * return -EINVAL;
2313 */
2314 "testq %4,%4\n"
2315 "jz 1f\n"
2316
2317 /* if (child_stack == NULL)
2318 * return -EINVAL;
2319 */
2320 "testq %5,%5\n"
2321 "jz 1f\n"
2322
2323 /* childstack -= 2*sizeof(void *);
2324 */
2325 "subq $16,%5\n"
2326
2327 /* Push "arg" and "fn" onto the stack that will be
2328 * used by the child.
2329 */
2330 "movq %7,8(%5)\n"
2331 "movq %4,0(%5)\n"
2332
2333 /* %rax = syscall(%rax = __NR_clone,
2334 * %rdi = flags,
2335 * %rsi = child_stack,
2336 * %rdx = parent_tidptr,
2337 * %r8 = new_tls,
2338 * %r10 = child_tidptr)
2339 */
2340 "movq %2,%%rax\n"
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002341 "movq %9,%%r8\n"
2342 "movq %10,%%r10\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002343 LSS_ENTRYPOINT
2344
2345 /* if (%rax != 0)
2346 * return;
2347 */
2348 "testq %%rax,%%rax\n"
2349 "jnz 1f\n"
2350
2351 /* In the child. Terminate frame pointer chain.
2352 */
2353 "xorq %%rbp,%%rbp\n"
2354
2355 /* Call "fn(arg)".
2356 */
2357 "popq %%rax\n"
2358 "popq %%rdi\n"
2359 "call *%%rax\n"
2360
2361 /* Call _exit(%ebx).
2362 */
2363 "movq %%rax,%%rdi\n"
2364 "movq %3,%%rax\n"
2365 LSS_ENTRYPOINT
2366
2367 /* Return to parent.
2368 */
2369 "1:\n"
2370 : "=a" (__res)
2371 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
vapier@chromium.org2273e812013-04-01 17:52:44 +00002372 "r"(LSS_SYSCALL_ARG(fn)),
2373 "S"(LSS_SYSCALL_ARG(child_stack)),
2374 "D"(LSS_SYSCALL_ARG(flags)),
2375 "r"(LSS_SYSCALL_ARG(arg)),
2376 "d"(LSS_SYSCALL_ARG(parent_tidptr)),
2377 "r"(LSS_SYSCALL_ARG(newtls)),
2378 "r"(LSS_SYSCALL_ARG(child_tidptr))
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002379 : "rsp", "memory", "r8", "r10", "r11", "rcx");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002380 }
2381 LSS_RETURN(int, __res);
2382 }
2383 LSS_INLINE _syscall2(int, arch_prctl, int, c, void *, a)
vapier@chromium.org2273e812013-04-01 17:52:44 +00002384
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002385 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2386 /* On x86-64, the kernel does not know how to return from
2387 * a signal handler. Instead, it relies on user space to provide a
2388 * restorer function that calls the rt_sigreturn() system call.
2389 * Unfortunately, we cannot just reference the glibc version of this
2390 * function, as glibc goes out of its way to make it inaccessible.
2391 */
vapier@chromium.org2273e812013-04-01 17:52:44 +00002392 long long res;
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002393 __asm__ __volatile__("jmp 2f\n"
2394 ".align 16\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002395 "1:movq %1,%%rax\n"
2396 LSS_ENTRYPOINT
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002397 "2:leaq 1b(%%rip),%0\n"
2398 : "=r" (res)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002399 : "i" (__NR_rt_sigreturn));
vapier@chromium.org833a10e2013-04-02 19:34:26 +00002400 return (void (*)(void))(uintptr_t)res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002401 }
2402 #elif defined(__ARM_ARCH_3__)
2403 /* Most definitions of _syscallX() neglect to mark "memory" as being
2404 * clobbered. This causes problems with compilers, that do a better job
2405 * at optimizing across __asm__ calls.
2406 * So, we just have to redefine all of the _syscallX() macros.
2407 */
2408 #undef LSS_REG
2409 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2410 #undef LSS_BODY
2411 #define LSS_BODY(type,name,args...) \
2412 register long __res_r0 __asm__("r0"); \
2413 long __res; \
2414 __asm__ __volatile__ (__syscall(name) \
2415 : "=r"(__res_r0) : args : "lr", "memory"); \
2416 __res = __res_r0; \
2417 LSS_RETURN(type, __res)
2418 #undef _syscall0
2419 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002420 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002421 LSS_BODY(type, name); \
2422 }
2423 #undef _syscall1
2424 #define _syscall1(type, name, type1, arg1) \
2425 type LSS_NAME(name)(type1 arg1) { \
2426 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2427 }
2428 #undef _syscall2
2429 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2430 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2431 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2432 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2433 }
2434 #undef _syscall3
2435 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2436 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2437 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2438 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2439 }
2440 #undef _syscall4
2441 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2442 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2443 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2444 LSS_REG(3, arg4); \
2445 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2446 }
2447 #undef _syscall5
2448 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2449 type5,arg5) \
2450 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2451 type5 arg5) { \
2452 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2453 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2454 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2455 "r"(__r4)); \
2456 }
2457 #undef _syscall6
2458 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2459 type5,arg5,type6,arg6) \
2460 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2461 type5 arg5, type6 arg6) { \
2462 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2463 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2464 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2465 "r"(__r4), "r"(__r5)); \
2466 }
2467 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2468 int flags, void *arg, int *parent_tidptr,
2469 void *newtls, int *child_tidptr) {
2470 long __res;
2471 {
2472 register int __flags __asm__("r0") = flags;
2473 register void *__stack __asm__("r1") = child_stack;
2474 register void *__ptid __asm__("r2") = parent_tidptr;
2475 register void *__tls __asm__("r3") = newtls;
2476 register int *__ctid __asm__("r4") = child_tidptr;
2477 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2478 * return -EINVAL;
2479 */
2480 "cmp %2,#0\n"
2481 "cmpne %3,#0\n"
2482 "moveq %0,%1\n"
2483 "beq 1f\n"
2484
2485 /* Push "arg" and "fn" onto the stack that will be
2486 * used by the child.
2487 */
2488 "str %5,[%3,#-4]!\n"
2489 "str %2,[%3,#-4]!\n"
2490
2491 /* %r0 = syscall(%r0 = flags,
2492 * %r1 = child_stack,
2493 * %r2 = parent_tidptr,
2494 * %r3 = newtls,
2495 * %r4 = child_tidptr)
2496 */
2497 __syscall(clone)"\n"
2498
2499 /* if (%r0 != 0)
2500 * return %r0;
2501 */
2502 "movs %0,r0\n"
2503 "bne 1f\n"
2504
2505 /* In the child, now. Call "fn(arg)".
2506 */
2507 "ldr r0,[sp, #4]\n"
2508 "mov lr,pc\n"
2509 "ldr pc,[sp]\n"
2510
2511 /* Call _exit(%r0).
2512 */
2513 __syscall(exit)"\n"
2514 "1:\n"
2515 : "=r" (__res)
2516 : "i"(-EINVAL),
2517 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2518 "r"(__ptid), "r"(__tls), "r"(__ctid)
2519 : "cc", "lr", "memory");
2520 }
2521 LSS_RETURN(int, __res);
2522 }
2523 #elif defined(__ARM_EABI__)
2524 /* Most definitions of _syscallX() neglect to mark "memory" as being
2525 * clobbered. This causes problems with compilers, that do a better job
2526 * at optimizing across __asm__ calls.
2527 * So, we just have to redefine all fo the _syscallX() macros.
2528 */
2529 #undef LSS_REG
2530 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2531 #undef LSS_BODY
2532 #define LSS_BODY(type,name,args...) \
2533 register long __res_r0 __asm__("r0"); \
2534 long __res; \
2535 __asm__ __volatile__ ("push {r7}\n" \
2536 "mov r7, %1\n" \
2537 "swi 0x0\n" \
2538 "pop {r7}\n" \
2539 : "=r"(__res_r0) \
2540 : "i"(__NR_##name) , ## args \
2541 : "lr", "memory"); \
2542 __res = __res_r0; \
2543 LSS_RETURN(type, __res)
2544 #undef _syscall0
2545 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002546 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002547 LSS_BODY(type, name); \
2548 }
2549 #undef _syscall1
2550 #define _syscall1(type, name, type1, arg1) \
2551 type LSS_NAME(name)(type1 arg1) { \
2552 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2553 }
2554 #undef _syscall2
2555 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2556 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2557 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2558 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2559 }
2560 #undef _syscall3
2561 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2562 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2563 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2564 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2565 }
2566 #undef _syscall4
2567 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2568 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2569 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2570 LSS_REG(3, arg4); \
2571 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2572 }
2573 #undef _syscall5
2574 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2575 type5,arg5) \
2576 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2577 type5 arg5) { \
2578 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2579 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2580 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2581 "r"(__r4)); \
2582 }
2583 #undef _syscall6
2584 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2585 type5,arg5,type6,arg6) \
2586 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2587 type5 arg5, type6 arg6) { \
2588 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2589 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2590 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2591 "r"(__r4), "r"(__r5)); \
2592 }
2593 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2594 int flags, void *arg, int *parent_tidptr,
2595 void *newtls, int *child_tidptr) {
2596 long __res;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002597 if (fn == NULL || child_stack == NULL) {
2598 __res = -EINVAL;
2599 } else {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002600 register int __flags __asm__("r0") = flags;
2601 register void *__stack __asm__("r1") = child_stack;
2602 register void *__ptid __asm__("r2") = parent_tidptr;
2603 register void *__tls __asm__("r3") = newtls;
2604 register int *__ctid __asm__("r4") = child_tidptr;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002605 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002606 * used by the child.
2607 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002608 "str %4,[%2,#-4]!\n"
2609 "str %1,[%2,#-4]!\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002610
2611 /* %r0 = syscall(%r0 = flags,
2612 * %r1 = child_stack,
2613 * %r2 = parent_tidptr,
2614 * %r3 = newtls,
2615 * %r4 = child_tidptr)
2616 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002617 "mov r7, %8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002618 "swi 0x0\n"
2619
2620 /* if (%r0 != 0)
2621 * return %r0;
2622 */
2623 "movs %0,r0\n"
2624 "bne 1f\n"
2625
2626 /* In the child, now. Call "fn(arg)".
2627 */
2628 "ldr r0,[sp, #4]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002629
2630 /* When compiling for Thumb-2 the "MOV LR,PC" here
2631 * won't work because it loads PC+4 into LR,
2632 * whereas the LDR is a 4-byte instruction.
2633 * This results in the child thread always
2634 * crashing with an "Illegal Instruction" when it
2635 * returned into the middle of the LDR instruction
2636 * The instruction sequence used instead was
2637 * recommended by
2638 * "https://wiki.edubuntu.org/ARM/Thumb2PortingHowto#Quick_Reference".
2639 */
2640 #ifdef __thumb2__
2641 "ldr r7,[sp]\n"
2642 "blx r7\n"
2643 #else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002644 "mov lr,pc\n"
2645 "ldr pc,[sp]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002646 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002647
2648 /* Call _exit(%r0).
2649 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002650 "mov r7, %9\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002651 "swi 0x0\n"
2652 "1:\n"
2653 : "=r" (__res)
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002654 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002655 "r"(__ptid), "r"(__tls), "r"(__ctid),
2656 "i"(__NR_clone), "i"(__NR_exit)
2657 : "cc", "r7", "lr", "memory");
2658 }
2659 LSS_RETURN(int, __res);
2660 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002661 #elif defined(__aarch64__)
2662 /* Most definitions of _syscallX() neglect to mark "memory" as being
2663 * clobbered. This causes problems with compilers, that do a better job
2664 * at optimizing across __asm__ calls.
2665 * So, we just have to redefine all of the _syscallX() macros.
2666 */
2667 #undef LSS_REG
2668 #define LSS_REG(r,a) register int64_t __r##r __asm__("x"#r) = (int64_t)a
2669 #undef LSS_BODY
2670 #define LSS_BODY(type,name,args...) \
2671 register int64_t __res_x0 __asm__("x0"); \
2672 int64_t __res; \
2673 __asm__ __volatile__ ("mov x8, %1\n" \
2674 "svc 0x0\n" \
2675 : "=r"(__res_x0) \
2676 : "i"(__NR_##name) , ## args \
2677 : "x8", "memory"); \
2678 __res = __res_x0; \
2679 LSS_RETURN(type, __res)
2680 #undef _syscall0
2681 #define _syscall0(type, name) \
2682 type LSS_NAME(name)(void) { \
2683 LSS_BODY(type, name); \
2684 }
2685 #undef _syscall1
2686 #define _syscall1(type, name, type1, arg1) \
2687 type LSS_NAME(name)(type1 arg1) { \
2688 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2689 }
2690 #undef _syscall2
2691 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2692 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2693 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2694 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2695 }
2696 #undef _syscall3
2697 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2698 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2699 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2700 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2701 }
2702 #undef _syscall4
2703 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2704 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2705 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2706 LSS_REG(3, arg4); \
2707 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2708 }
2709 #undef _syscall5
2710 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2711 type5,arg5) \
2712 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2713 type5 arg5) { \
2714 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2715 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2716 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2717 "r"(__r4)); \
2718 }
2719 #undef _syscall6
2720 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2721 type5,arg5,type6,arg6) \
2722 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2723 type5 arg5, type6 arg6) { \
2724 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2725 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2726 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2727 "r"(__r4), "r"(__r5)); \
2728 }
2729
2730 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2731 int flags, void *arg, int *parent_tidptr,
2732 void *newtls, int *child_tidptr) {
2733 int64_t __res;
2734 {
2735 register uint64_t __flags __asm__("x0") = flags;
2736 register void *__stack __asm__("x1") = child_stack;
2737 register void *__ptid __asm__("x2") = parent_tidptr;
2738 register void *__tls __asm__("x3") = newtls;
2739 register int *__ctid __asm__("x4") = child_tidptr;
2740 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
2741 * used by the child.
2742 */
2743 "stp %1, %4, [%2, #-16]!\n"
2744
2745 /* %x0 = syscall(%x0 = flags,
2746 * %x1 = child_stack,
2747 * %x2 = parent_tidptr,
2748 * %x3 = newtls,
2749 * %x4 = child_tidptr)
2750 */
2751 "mov x8, %8\n"
2752 "svc 0x0\n"
2753
2754 /* if (%r0 != 0)
2755 * return %r0;
2756 */
2757 "mov %0, x0\n"
2758 "cbnz x0, 1f\n"
2759
2760 /* In the child, now. Call "fn(arg)".
2761 */
2762 "ldp x1, x0, [sp], #16\n"
2763 "blr x1\n"
2764
2765 /* Call _exit(%r0).
2766 */
2767 "mov x8, %9\n"
2768 "svc 0x0\n"
2769 "1:\n"
2770 : "=r" (__res)
2771 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2772 "r"(__ptid), "r"(__tls), "r"(__ctid),
2773 "i"(__NR_clone), "i"(__NR_exit)
2774 : "cc", "x8", "memory");
2775 }
2776 LSS_RETURN(int, __res);
2777 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002778 #elif defined(__mips__)
2779 #undef LSS_REG
2780 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
2781 (unsigned long)(a)
2782 #undef LSS_BODY
thestig@chromium.org952107f2014-08-01 02:22:56 +00002783 #undef LSS_SYSCALL_CLOBBERS
2784 #if _MIPS_SIM == _MIPS_SIM_ABI32
2785 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", \
2786 "$11", "$12", "$13", "$14", "$15", \
2787 "$24", "$25", "hi", "lo", "memory"
2788 #else
2789 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", \
2790 "$13", "$14", "$15", "$24", "$25", \
2791 "hi", "lo", "memory"
2792 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002793 #define LSS_BODY(type,name,r7,...) \
2794 register unsigned long __v0 __asm__("$2") = __NR_##name; \
2795 __asm__ __volatile__ ("syscall\n" \
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00002796 : "=r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002797 : "0"(__v0), ##__VA_ARGS__ \
thestig@chromium.org952107f2014-08-01 02:22:56 +00002798 : LSS_SYSCALL_CLOBBERS); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002799 LSS_RETURN(type, __v0, __r7)
2800 #undef _syscall0
2801 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002802 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002803 register unsigned long __r7 __asm__("$7"); \
2804 LSS_BODY(type, name, "=r"); \
2805 }
2806 #undef _syscall1
2807 #define _syscall1(type, name, type1, arg1) \
2808 type LSS_NAME(name)(type1 arg1) { \
2809 register unsigned long __r7 __asm__("$7"); \
2810 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
2811 }
2812 #undef _syscall2
2813 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2814 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2815 register unsigned long __r7 __asm__("$7"); \
2816 LSS_REG(4, arg1); LSS_REG(5, arg2); \
2817 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
2818 }
2819 #undef _syscall3
2820 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2821 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2822 register unsigned long __r7 __asm__("$7"); \
2823 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2824 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2825 }
2826 #undef _syscall4
2827 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2828 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2829 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2830 LSS_REG(7, arg4); \
2831 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2832 }
2833 #undef _syscall5
2834 #if _MIPS_SIM == _MIPS_SIM_ABI32
2835 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2836 * on the stack, whereas the new APIs use registers "r8" and "r9".
2837 */
2838 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2839 type5,arg5) \
2840 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2841 type5 arg5) { \
2842 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2843 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002844 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002845 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002846 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002847 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002848 "syscall\n" \
2849 "addiu $29, 32\n" \
2850 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002851 : "+r"(__v0), "+r" (__r7) \
2852 : "r"(__r4), "r"(__r5), \
2853 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002854 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002855 "$13", "$14", "$15", "$24", "$25", \
2856 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002857 LSS_RETURN(type, __v0, __r7); \
2858 }
2859 #else
2860 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2861 type5,arg5) \
2862 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2863 type5 arg5) { \
2864 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2865 LSS_REG(7, arg4); LSS_REG(8, arg5); \
2866 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2867 "r"(__r8)); \
2868 }
2869 #endif
2870 #undef _syscall6
2871 #if _MIPS_SIM == _MIPS_SIM_ABI32
2872 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2873 * on the stack, whereas the new APIs use registers "r8" and "r9".
2874 */
2875 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2876 type5,arg5,type6,arg6) \
2877 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2878 type5 arg5, type6 arg6) { \
2879 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2880 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002881 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002882 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002883 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002884 "sw %5, 16($29)\n" \
2885 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002886 "syscall\n" \
2887 "addiu $29, 32\n" \
2888 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002889 : "+r"(__v0), "+r" (__r7) \
2890 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002891 "r"(__r6), "r" ((unsigned long)arg5), \
2892 "r" ((unsigned long)arg6) \
2893 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002894 "$13", "$14", "$15", "$24", "$25", \
2895 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002896 LSS_RETURN(type, __v0, __r7); \
2897 }
2898 #else
2899 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2900 type5,arg5,type6,arg6) \
2901 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2902 type5 arg5,type6 arg6) { \
2903 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2904 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
2905 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2906 "r"(__r8), "r"(__r9)); \
2907 }
2908 #endif
2909 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2910 int flags, void *arg, int *parent_tidptr,
2911 void *newtls, int *child_tidptr) {
vapier@chromium.orge0797682015-02-20 20:45:56 +00002912 register unsigned long __v0 __asm__("$2") = -EINVAL;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002913 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
2914 {
2915 register int __flags __asm__("$4") = flags;
2916 register void *__stack __asm__("$5") = child_stack;
2917 register void *__ptid __asm__("$6") = parent_tidptr;
2918 register int *__ctid __asm__("$8") = child_tidptr;
2919 __asm__ __volatile__(
2920 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2921 "subu $29,24\n"
2922 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2923 "sub $29,16\n"
2924 #else
2925 "dsubu $29,16\n"
2926 #endif
2927
2928 /* if (fn == NULL || child_stack == NULL)
2929 * return -EINVAL;
2930 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002931 "beqz %4,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002932 "beqz %5,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002933
2934 /* Push "arg" and "fn" onto the stack that will be
2935 * used by the child.
2936 */
2937 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002938 "subu %5,32\n"
2939 "sw %4,0(%5)\n"
2940 "sw %7,4(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002941 #elif _MIPS_SIM == _MIPS_SIM_NABI32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002942 "sub %5,32\n"
2943 "sw %4,0(%5)\n"
2944 "sw %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002945 #else
vapier@chromium.orge0797682015-02-20 20:45:56 +00002946 "dsubu %5,32\n"
2947 "sd %4,0(%5)\n"
2948 "sd %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002949 #endif
2950
2951 /* $7 = syscall($4 = flags,
2952 * $5 = child_stack,
2953 * $6 = parent_tidptr,
2954 * $7 = newtls,
2955 * $8 = child_tidptr)
2956 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002957 "li $2,%2\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002958 "syscall\n"
2959
2960 /* if ($7 != 0)
2961 * return $2;
2962 */
2963 "bnez $7,1f\n"
2964 "bnez $2,1f\n"
2965
2966 /* In the child, now. Call "fn(arg)".
2967 */
2968 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2969 "lw $25,0($29)\n"
2970 "lw $4,4($29)\n"
2971 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2972 "lw $25,0($29)\n"
2973 "lw $4,8($29)\n"
2974 #else
2975 "ld $25,0($29)\n"
2976 "ld $4,8($29)\n"
2977 #endif
2978 "jalr $25\n"
2979
2980 /* Call _exit($2)
2981 */
2982 "move $4,$2\n"
vapier@chromium.orge0797682015-02-20 20:45:56 +00002983 "li $2,%3\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002984 "syscall\n"
2985
2986 "1:\n"
2987 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2988 "addu $29, 24\n"
2989 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2990 "add $29, 16\n"
2991 #else
2992 "daddu $29,16\n"
2993 #endif
petarj@mips.com0ece1c62013-04-10 00:28:04 +00002994 : "+r" (__v0), "+r" (__r7)
vapier@chromium.orge0797682015-02-20 20:45:56 +00002995 : "i"(__NR_clone), "i"(__NR_exit), "r"(fn),
2996 "r"(__stack), "r"(__flags), "r"(arg),
2997 "r"(__ptid), "r"(__ctid)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002998 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002999 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003000 }
3001 LSS_RETURN(int, __v0, __r7);
3002 }
3003 #elif defined (__PPC__)
3004 #undef LSS_LOADARGS_0
3005 #define LSS_LOADARGS_0(name, dummy...) \
3006 __sc_0 = __NR_##name
3007 #undef LSS_LOADARGS_1
3008 #define LSS_LOADARGS_1(name, arg1) \
3009 LSS_LOADARGS_0(name); \
3010 __sc_3 = (unsigned long) (arg1)
3011 #undef LSS_LOADARGS_2
3012 #define LSS_LOADARGS_2(name, arg1, arg2) \
3013 LSS_LOADARGS_1(name, arg1); \
3014 __sc_4 = (unsigned long) (arg2)
3015 #undef LSS_LOADARGS_3
3016 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
3017 LSS_LOADARGS_2(name, arg1, arg2); \
3018 __sc_5 = (unsigned long) (arg3)
3019 #undef LSS_LOADARGS_4
3020 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
3021 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
3022 __sc_6 = (unsigned long) (arg4)
3023 #undef LSS_LOADARGS_5
3024 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
3025 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
3026 __sc_7 = (unsigned long) (arg5)
3027 #undef LSS_LOADARGS_6
3028 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
3029 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
3030 __sc_8 = (unsigned long) (arg6)
3031 #undef LSS_ASMINPUT_0
3032 #define LSS_ASMINPUT_0 "0" (__sc_0)
3033 #undef LSS_ASMINPUT_1
3034 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
3035 #undef LSS_ASMINPUT_2
3036 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
3037 #undef LSS_ASMINPUT_3
3038 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
3039 #undef LSS_ASMINPUT_4
3040 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
3041 #undef LSS_ASMINPUT_5
3042 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
3043 #undef LSS_ASMINPUT_6
3044 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
3045 #undef LSS_BODY
3046 #define LSS_BODY(nr, type, name, args...) \
3047 long __sc_ret, __sc_err; \
3048 { \
3049 register unsigned long __sc_0 __asm__ ("r0"); \
3050 register unsigned long __sc_3 __asm__ ("r3"); \
3051 register unsigned long __sc_4 __asm__ ("r4"); \
3052 register unsigned long __sc_5 __asm__ ("r5"); \
3053 register unsigned long __sc_6 __asm__ ("r6"); \
3054 register unsigned long __sc_7 __asm__ ("r7"); \
3055 register unsigned long __sc_8 __asm__ ("r8"); \
3056 \
3057 LSS_LOADARGS_##nr(name, args); \
3058 __asm__ __volatile__ \
3059 ("sc\n\t" \
3060 "mfcr %0" \
3061 : "=&r" (__sc_0), \
3062 "=&r" (__sc_3), "=&r" (__sc_4), \
3063 "=&r" (__sc_5), "=&r" (__sc_6), \
3064 "=&r" (__sc_7), "=&r" (__sc_8) \
3065 : LSS_ASMINPUT_##nr \
3066 : "cr0", "ctr", "memory", \
3067 "r9", "r10", "r11", "r12"); \
3068 __sc_ret = __sc_3; \
3069 __sc_err = __sc_0; \
3070 } \
3071 LSS_RETURN(type, __sc_ret, __sc_err)
3072 #undef _syscall0
3073 #define _syscall0(type, name) \
3074 type LSS_NAME(name)(void) { \
3075 LSS_BODY(0, type, name); \
3076 }
3077 #undef _syscall1
3078 #define _syscall1(type, name, type1, arg1) \
3079 type LSS_NAME(name)(type1 arg1) { \
3080 LSS_BODY(1, type, name, arg1); \
3081 }
3082 #undef _syscall2
3083 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3084 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3085 LSS_BODY(2, type, name, arg1, arg2); \
3086 }
3087 #undef _syscall3
3088 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3089 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3090 LSS_BODY(3, type, name, arg1, arg2, arg3); \
3091 }
3092 #undef _syscall4
3093 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3094 type4, arg4) \
3095 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3096 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
3097 }
3098 #undef _syscall5
3099 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3100 type4, arg4, type5, arg5) \
3101 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3102 type5 arg5) { \
3103 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
3104 }
3105 #undef _syscall6
3106 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3107 type4, arg4, type5, arg5, type6, arg6) \
3108 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3109 type5 arg5, type6 arg6) { \
3110 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
3111 }
3112 /* clone function adapted from glibc 2.3.6 clone.S */
3113 /* TODO(csilvers): consider wrapping some args up in a struct, like we
3114 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
3115 */
3116 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3117 int flags, void *arg, int *parent_tidptr,
3118 void *newtls, int *child_tidptr) {
3119 long __ret, __err;
3120 {
3121 register int (*__fn)(void *) __asm__ ("r8") = fn;
3122 register void *__cstack __asm__ ("r4") = child_stack;
3123 register int __flags __asm__ ("r3") = flags;
3124 register void * __arg __asm__ ("r9") = arg;
3125 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
3126 register void * __newtls __asm__ ("r6") = newtls;
3127 register int * __ctidptr __asm__ ("r7") = child_tidptr;
3128 __asm__ __volatile__(
3129 /* check for fn == NULL
3130 * and child_stack == NULL
3131 */
3132 "cmpwi cr0, %6, 0\n\t"
3133 "cmpwi cr1, %7, 0\n\t"
3134 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
3135 "beq- cr0, 1f\n\t"
3136
3137 /* set up stack frame for child */
3138 "clrrwi %7, %7, 4\n\t"
3139 "li 0, 0\n\t"
3140 "stwu 0, -16(%7)\n\t"
3141
3142 /* fn, arg, child_stack are saved across the syscall: r28-30 */
3143 "mr 28, %6\n\t"
3144 "mr 29, %7\n\t"
3145 "mr 27, %9\n\t"
3146
3147 /* syscall */
3148 "li 0, %4\n\t"
3149 /* flags already in r3
3150 * child_stack already in r4
3151 * ptidptr already in r5
3152 * newtls already in r6
3153 * ctidptr already in r7
3154 */
3155 "sc\n\t"
3156
3157 /* Test if syscall was successful */
3158 "cmpwi cr1, 3, 0\n\t"
3159 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
3160 "bne- cr1, 1f\n\t"
3161
3162 /* Do the function call */
3163 "mtctr 28\n\t"
3164 "mr 3, 27\n\t"
3165 "bctrl\n\t"
3166
3167 /* Call _exit(r3) */
3168 "li 0, %5\n\t"
3169 "sc\n\t"
3170
3171 /* Return to parent */
3172 "1:\n"
3173 "mfcr %1\n\t"
3174 "mr %0, 3\n\t"
3175 : "=r" (__ret), "=r" (__err)
3176 : "0" (-1), "1" (EINVAL),
3177 "i" (__NR_clone), "i" (__NR_exit),
3178 "r" (__fn), "r" (__cstack), "r" (__flags),
3179 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
3180 "r" (__ctidptr)
3181 : "cr0", "cr1", "memory", "ctr",
3182 "r0", "r29", "r27", "r28");
3183 }
3184 LSS_RETURN(int, __ret, __err);
3185 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003186 #elif defined(__s390__)
3187 #undef LSS_REG
3188 #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
3189 #undef LSS_BODY
3190 #define LSS_BODY(type, name, args...) \
3191 register unsigned long __nr __asm__("r1") \
3192 = (unsigned long)(__NR_##name); \
3193 register long __res_r2 __asm__("r2"); \
3194 long __res; \
3195 __asm__ __volatile__ \
3196 ("svc 0\n\t" \
3197 : "=d"(__res_r2) \
3198 : "d"(__nr), ## args \
3199 : "memory"); \
3200 __res = __res_r2; \
3201 LSS_RETURN(type, __res)
3202 #undef _syscall0
3203 #define _syscall0(type, name) \
3204 type LSS_NAME(name)(void) { \
3205 LSS_BODY(type, name); \
3206 }
3207 #undef _syscall1
3208 #define _syscall1(type, name, type1, arg1) \
3209 type LSS_NAME(name)(type1 arg1) { \
3210 LSS_REG(2, arg1); \
3211 LSS_BODY(type, name, "0"(__r2)); \
3212 }
3213 #undef _syscall2
3214 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3215 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3216 LSS_REG(2, arg1); LSS_REG(3, arg2); \
3217 LSS_BODY(type, name, "0"(__r2), "d"(__r3)); \
3218 }
3219 #undef _syscall3
3220 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3221 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3222 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3223 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4)); \
3224 }
3225 #undef _syscall4
3226 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3227 type4, arg4) \
3228 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3229 type4 arg4) { \
3230 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3231 LSS_REG(5, arg4); \
3232 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3233 "d"(__r5)); \
3234 }
3235 #undef _syscall5
3236 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3237 type4, arg4, type5, arg5) \
3238 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3239 type4 arg4, type5 arg5) { \
3240 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3241 LSS_REG(5, arg4); LSS_REG(6, arg5); \
3242 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3243 "d"(__r5), "d"(__r6)); \
3244 }
3245 #undef _syscall6
3246 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3247 type4, arg4, type5, arg5, type6, arg6) \
3248 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3249 type4 arg4, type5 arg5, type6 arg6) { \
3250 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3251 LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
3252 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3253 "d"(__r5), "d"(__r6), "d"(__r7)); \
3254 }
3255 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3256 int flags, void *arg, int *parent_tidptr,
3257 void *newtls, int *child_tidptr) {
3258 long __ret;
3259 {
3260 register int (*__fn)(void *) __asm__ ("r1") = fn;
3261 register void *__cstack __asm__ ("r2") = child_stack;
3262 register int __flags __asm__ ("r3") = flags;
3263 register void *__arg __asm__ ("r0") = arg;
3264 register int *__ptidptr __asm__ ("r4") = parent_tidptr;
3265 register void *__newtls __asm__ ("r6") = newtls;
3266 register int *__ctidptr __asm__ ("r5") = child_tidptr;
3267 __asm__ __volatile__ (
3268 #ifndef __s390x__
3269 /* arg already in r0 */
3270 "ltr %4, %4\n\t" /* check fn, which is already in r1 */
3271 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3272 "ltr %5, %5\n\t" /* check child_stack, which is already in r2 */
3273 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3274 /* flags already in r3 */
3275 /* parent_tidptr already in r4 */
3276 /* child_tidptr already in r5 */
3277 /* newtls already in r6 */
3278 "svc %2\n\t" /* invoke clone syscall */
3279 "ltr %0,%%r2\n\t" /* load return code into __ret and test */
3280 "jnz 1f\n\t" /* return to parent if non-zero */
3281 /* start child thread */
3282 "lr %%r2, %7\n\t" /* set first parameter to void *arg */
3283 "ahi %%r15, -96\n\t" /* make room on the stack for the save area */
3284 "xc 0(4,%%r15), 0(%%r15)\n\t"
3285 "basr %%r14, %4\n\t" /* jump to fn */
3286 "svc %3\n" /* invoke exit syscall */
3287 "1:\n"
3288 #else
3289 /* arg already in r0 */
3290 "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
3291 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3292 "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
3293 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3294 /* flags already in r3 */
3295 /* parent_tidptr already in r4 */
3296 /* child_tidptr already in r5 */
3297 /* newtls already in r6 */
3298 "svc %2\n\t" /* invoke clone syscall */
3299 "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
3300 "jnz 1f\n\t" /* return to parent if non-zero */
3301 /* start child thread */
3302 "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
3303 "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
3304 "xc 0(8,%%r15), 0(%%r15)\n\t"
3305 "basr %%r14, %4\n\t" /* jump to fn */
3306 "svc %3\n" /* invoke exit syscall */
3307 "1:\n"
3308 #endif
3309 : "=r" (__ret)
3310 : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
3311 "d" (__fn), "d" (__cstack), "d" (__flags), "d" (__arg),
3312 "d" (__ptidptr), "d" (__newtls), "d" (__ctidptr)
3313 : "cc", "r14", "memory"
3314 );
3315 }
3316 LSS_RETURN(int, __ret);
3317 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003318 #endif
3319 #define __NR__exit __NR_exit
3320 #define __NR__gettid __NR_gettid
3321 #define __NR__mremap __NR_mremap
phosek@chromium.orga9c02722013-08-16 17:31:42 +00003322 LSS_INLINE _syscall1(void *, brk, void *, e)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003323 LSS_INLINE _syscall1(int, chdir, const char *,p)
3324 LSS_INLINE _syscall1(int, close, int, f)
3325 LSS_INLINE _syscall2(int, clock_getres, int, c,
3326 struct kernel_timespec*, t)
3327 LSS_INLINE _syscall2(int, clock_gettime, int, c,
3328 struct kernel_timespec*, t)
3329 LSS_INLINE _syscall1(int, dup, int, f)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003330 #if !defined(__aarch64__)
3331 // The dup2 syscall has been deprecated on aarch64. We polyfill it below.
3332 LSS_INLINE _syscall2(int, dup2, int, s,
3333 int, d)
3334 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003335 LSS_INLINE _syscall3(int, execve, const char*, f,
3336 const char*const*,a,const char*const*, e)
3337 LSS_INLINE _syscall1(int, _exit, int, e)
3338 LSS_INLINE _syscall1(int, exit_group, int, e)
3339 LSS_INLINE _syscall3(int, fcntl, int, f,
3340 int, c, long, a)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003341 #if !defined(__aarch64__)
3342 // The fork syscall has been deprecated on aarch64. We polyfill it below.
3343 LSS_INLINE _syscall0(pid_t, fork)
3344 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003345 LSS_INLINE _syscall2(int, fstat, int, f,
3346 struct kernel_stat*, b)
3347 LSS_INLINE _syscall2(int, fstatfs, int, f,
3348 struct kernel_statfs*, b)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003349 #if defined(__x86_64__)
3350 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3351 LSS_INLINE int LSS_NAME(ftruncate)(int f, off_t l) {
3352 LSS_BODY(2, int, ftruncate, LSS_SYSCALL_ARG(f), (uint64_t)(l));
3353 }
3354 #else
3355 LSS_INLINE _syscall2(int, ftruncate, int, f,
3356 off_t, l)
3357 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003358 LSS_INLINE _syscall4(int, futex, int*, a,
3359 int, o, int, v,
3360 struct kernel_timespec*, t)
3361 LSS_INLINE _syscall3(int, getdents, int, f,
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003362 struct kernel_dirent*, d, int, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003363 LSS_INLINE _syscall3(int, getdents64, int, f,
3364 struct kernel_dirent64*, d, int, c)
3365 LSS_INLINE _syscall0(gid_t, getegid)
3366 LSS_INLINE _syscall0(uid_t, geteuid)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003367 #if !defined(__aarch64__)
3368 // The getgprp syscall has been deprecated on aarch64.
3369 LSS_INLINE _syscall0(pid_t, getpgrp)
3370 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003371 LSS_INLINE _syscall0(pid_t, getpid)
3372 LSS_INLINE _syscall0(pid_t, getppid)
3373 LSS_INLINE _syscall2(int, getpriority, int, a,
3374 int, b)
3375 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
3376 gid_t *, e, gid_t *, s)
3377 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
3378 uid_t *, e, uid_t *, s)
3379#if !defined(__ARM_EABI__)
3380 LSS_INLINE _syscall2(int, getrlimit, int, r,
3381 struct kernel_rlimit*, l)
3382#endif
3383 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
3384 LSS_INLINE _syscall0(pid_t, _gettid)
3385 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
3386 void*, tz)
3387 LSS_INLINE _syscall5(int, setxattr, const char *,p,
3388 const char *, n, const void *,v,
3389 size_t, s, int, f)
3390 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
3391 const char *, n, const void *,v,
3392 size_t, s, int, f)
3393 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
3394 const char *, n, void *, v, size_t, s)
3395 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
3396 const char *, n, void *, v, size_t, s)
3397 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
3398 char *, l, size_t, s)
3399 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
3400 char *, l, size_t, s)
3401 LSS_INLINE _syscall3(int, ioctl, int, d,
3402 int, r, void *, a)
3403 LSS_INLINE _syscall2(int, ioprio_get, int, which,
3404 int, who)
3405 LSS_INLINE _syscall3(int, ioprio_set, int, which,
3406 int, who, int, ioprio)
3407 LSS_INLINE _syscall2(int, kill, pid_t, p,
3408 int, s)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003409 #if defined(__x86_64__)
3410 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3411 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
3412 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
3413 LSS_SYSCALL_ARG(w));
3414 }
3415 #else
3416 LSS_INLINE _syscall3(off_t, lseek, int, f,
3417 off_t, o, int, w)
3418 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003419 LSS_INLINE _syscall2(int, munmap, void*, s,
3420 size_t, l)
3421 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
3422 unsigned long, n, void **,g, int *, d,
3423 int *, s, int, f)
3424 LSS_INLINE _syscall3(int, mprotect, const void *,a,
3425 size_t, l, int, p)
3426 LSS_INLINE _syscall5(void*, _mremap, void*, o,
3427 size_t, os, size_t, ns,
3428 unsigned long, f, void *, a)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003429 #if !defined(__aarch64__)
3430 // The open and poll syscalls have been deprecated on aarch64. We polyfill
3431 // them below.
3432 LSS_INLINE _syscall3(int, open, const char*, p,
3433 int, f, int, m)
3434 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
3435 unsigned int, n, int, t)
3436 #endif
mseaborn@chromium.orge6c76822013-08-31 00:08:44 +00003437 LSS_INLINE _syscall5(int, prctl, int, option,
3438 unsigned long, arg2,
3439 unsigned long, arg3,
3440 unsigned long, arg4,
3441 unsigned long, arg5)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003442 LSS_INLINE _syscall4(long, ptrace, int, r,
3443 pid_t, p, void *, a, void *, d)
3444 #if defined(__NR_quotactl)
3445 // Defined on x86_64 / i386 only
3446 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
3447 int, id, caddr_t, addr)
3448 #endif
3449 LSS_INLINE _syscall3(ssize_t, read, int, f,
3450 void *, b, size_t, c)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003451 #if !defined(__aarch64__)
3452 // The readlink syscall has been deprecated on aarch64. We polyfill below.
3453 LSS_INLINE _syscall3(int, readlink, const char*, p,
3454 char*, b, size_t, s)
3455 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003456 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
3457 const struct kernel_sigaction*, a,
3458 struct kernel_sigaction*, o, size_t, c)
3459 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
3460 size_t, c)
3461 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
3462 const struct kernel_sigset_t*, s,
3463 struct kernel_sigset_t*, o, size_t, c)
3464 LSS_INLINE _syscall2(int, rt_sigsuspend,
3465 const struct kernel_sigset_t*, s, size_t, c)
3466 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
3467 unsigned int, l, unsigned long *, m)
3468 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
3469 unsigned int, l, unsigned long *, m)
3470 LSS_INLINE _syscall0(int, sched_yield)
3471 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
3472 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
3473 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
3474 LSS_INLINE _syscall1(int, setuid, uid_t, u)
3475 LSS_INLINE _syscall1(int, setgid, gid_t, g)
3476 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
3477 pid_t, g)
3478 LSS_INLINE _syscall3(int, setpriority, int, a,
3479 int, b, int, p)
3480 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
3481 gid_t, e, gid_t, s)
3482 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
3483 uid_t, e, uid_t, s)
3484 LSS_INLINE _syscall2(int, setrlimit, int, r,
3485 const struct kernel_rlimit*, l)
3486 LSS_INLINE _syscall0(pid_t, setsid)
3487 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
3488 const stack_t*, o)
3489 #if defined(__NR_sigreturn)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003490 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003491 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003492 #if !defined(__aarch64__)
3493 // The stat syscall has been deprecated on aarch64. We polyfill it below.
3494 LSS_INLINE _syscall2(int, stat, const char*, f,
3495 struct kernel_stat*, b)
3496 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003497 LSS_INLINE _syscall2(int, statfs, const char*, f,
3498 struct kernel_statfs*, b)
3499 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
3500 pid_t, t, int, s)
3501 LSS_INLINE _syscall2(int, tkill, pid_t, p,
3502 int, s)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003503 #if !defined(__aarch64__)
3504 // The unlink syscall has been deprecated on aarch64. We polyfill it below.
3505 LSS_INLINE _syscall1(int, unlink, const char*, f)
3506 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003507 LSS_INLINE _syscall3(ssize_t, write, int, f,
3508 const void *, b, size_t, c)
3509 LSS_INLINE _syscall3(ssize_t, writev, int, f,
3510 const struct kernel_iovec*, v, size_t, c)
3511 #if defined(__NR_getcpu)
3512 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00003513 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003514 #endif
3515 #if defined(__x86_64__) || \
3516 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
3517 LSS_INLINE _syscall3(int, recvmsg, int, s,
3518 struct kernel_msghdr*, m, int, f)
3519 LSS_INLINE _syscall3(int, sendmsg, int, s,
3520 const struct kernel_msghdr*, m, int, f)
3521 LSS_INLINE _syscall6(int, sendto, int, s,
3522 const void*, m, size_t, l,
3523 int, f,
3524 const struct kernel_sockaddr*, a, int, t)
3525 LSS_INLINE _syscall2(int, shutdown, int, s,
3526 int, h)
3527 LSS_INLINE _syscall3(int, socket, int, d,
3528 int, t, int, p)
3529 LSS_INLINE _syscall4(int, socketpair, int, d,
3530 int, t, int, p, int*, s)
3531 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003532 #if defined(__NR_fadvise64)
3533 #if defined(__x86_64__)
3534 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3535 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset, loff_t len,
3536 int advice) {
3537 LSS_BODY(4, int, fadvise64, LSS_SYSCALL_ARG(fd), (uint64_t)(offset),
3538 (uint64_t)(len), LSS_SYSCALL_ARG(advice));
3539 }
3540 #else
3541 LSS_INLINE _syscall4(int, fadvise64,
3542 int, fd, loff_t, offset, loff_t, len, int, advice)
3543 #endif
3544 #elif defined(__i386__)
3545 #define __NR__fadvise64_64 __NR_fadvise64_64
3546 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
3547 unsigned, offset_lo, unsigned, offset_hi,
3548 unsigned, len_lo, unsigned, len_hi,
3549 int, advice)
3550
3551 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3552 loff_t len, int advice) {
3553 return LSS_NAME(_fadvise64_64)(fd,
3554 (unsigned)offset, (unsigned)(offset >>32),
3555 (unsigned)len, (unsigned)(len >> 32),
3556 advice);
3557 }
3558
3559 #elif defined(__s390__) && !defined(__s390x__)
3560 #define __NR__fadvise64_64 __NR_fadvise64_64
3561 struct kernel_fadvise64_64_args {
3562 int fd;
3563 long long offset;
3564 long long len;
3565 int advice;
3566 };
3567
3568 LSS_INLINE _syscall1(int, _fadvise64_64,
3569 struct kernel_fadvise64_64_args *args)
3570
3571 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3572 loff_t len, int advice) {
3573 struct kernel_fadvise64_64_args args = { fd, offset, len, advice };
3574 return LSS_NAME(_fadvise64_64)(&args);
3575 }
3576 #endif
3577 #if defined(__NR_fallocate)
3578 #if defined(__x86_64__)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003579 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3580 LSS_INLINE int LSS_NAME(fallocate)(int f, int mode, loff_t offset,
3581 loff_t len) {
3582 LSS_BODY(4, int, fallocate, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(mode),
3583 (uint64_t)(offset), (uint64_t)(len));
3584 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003585 #elif defined(__i386__) || (defined(__s390__) && !defined(__s390x__))
3586 #define __NR__fallocate __NR_fallocate
3587 LSS_INLINE _syscall6(int, _fallocate, int, fd,
3588 int, mode,
3589 unsigned, offset_lo, unsigned, offset_hi,
3590 unsigned, len_lo, unsigned, len_hi)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003591
Bryan Chan3f6478a2016-06-14 08:38:17 -04003592 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
3593 loff_t offset, loff_t len) {
3594 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
3595 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
3596 }
3597 #else
3598 LSS_INLINE _syscall4(int, fallocate,
3599 int, f, int, mode, loff_t, offset, loff_t, len)
3600 #endif
3601 #endif
3602 #if defined(__x86_64__) || defined(__s390x__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003603 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3604 gid_t *egid,
3605 gid_t *sgid) {
3606 return LSS_NAME(getresgid)(rgid, egid, sgid);
3607 }
3608
3609 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3610 uid_t *euid,
3611 uid_t *suid) {
3612 return LSS_NAME(getresuid)(ruid, euid, suid);
3613 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003614 LSS_INLINE _syscall4(int, newfstatat, int, d,
3615 const char *, p,
3616 struct kernel_stat*, b, int, f)
3617
3618 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3619 return LSS_NAME(setfsgid)(gid);
3620 }
3621
3622 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3623 return LSS_NAME(setfsuid)(uid);
3624 }
3625
3626 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3627 return LSS_NAME(setresgid)(rgid, egid, sgid);
3628 }
3629
3630 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3631 return LSS_NAME(setresuid)(ruid, euid, suid);
3632 }
3633
3634 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3635 const struct kernel_sigaction *act,
3636 struct kernel_sigaction *oldact) {
Bryan Chan3f6478a2016-06-14 08:38:17 -04003637 #if defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003638 /* On x86_64, the kernel requires us to always set our own
3639 * SA_RESTORER in order to be able to return from a signal handler.
3640 * This function must have a "magic" signature that the "gdb"
3641 * (and maybe the kernel?) can recognize.
3642 */
3643 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
3644 struct kernel_sigaction a = *act;
3645 a.sa_flags |= SA_RESTORER;
3646 a.sa_restorer = LSS_NAME(restore_rt)();
3647 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
3648 (KERNEL_NSIG+7)/8);
Bryan Chan3f6478a2016-06-14 08:38:17 -04003649 } else
3650 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003651 return LSS_NAME(rt_sigaction)(signum, act, oldact,
3652 (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003653 }
3654
3655 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3656 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3657 }
3658
3659 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3660 const struct kernel_sigset_t *set,
3661 struct kernel_sigset_t *oldset) {
3662 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3663 }
3664
3665 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3666 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3667 }
3668 #endif
3669 #if defined(__x86_64__) || defined(__ARM_ARCH_3__) || \
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003670 defined(__ARM_EABI__) || defined(__aarch64__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04003671 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32) || \
3672 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003673 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
3674 int*, s, int, o,
3675 struct kernel_rusage*, r)
3676
3677 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){
3678 return LSS_NAME(wait4)(pid, status, options, 0);
3679 }
3680 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003681 #if defined(__NR_openat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003682 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003683 #endif
3684 #if defined(__NR_unlinkat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003685 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
3686 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003687 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3688 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003689 #define __NR__getresgid32 __NR_getresgid32
3690 #define __NR__getresuid32 __NR_getresuid32
3691 #define __NR__setfsgid32 __NR_setfsgid32
3692 #define __NR__setfsuid32 __NR_setfsuid32
3693 #define __NR__setresgid32 __NR_setresgid32
3694 #define __NR__setresuid32 __NR_setresuid32
3695#if defined(__ARM_EABI__)
3696 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
3697 struct kernel_rlimit*, l)
3698#endif
3699 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
3700 gid_t *, e, gid_t *, s)
3701 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
3702 uid_t *, e, uid_t *, s)
3703 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
3704 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
3705 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
3706 gid_t, e, gid_t, s)
3707 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
3708 uid_t, e, uid_t, s)
3709
3710 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3711 gid_t *egid,
3712 gid_t *sgid) {
3713 int rc;
3714 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
3715 LSS_ERRNO == ENOSYS) {
3716 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
3717 return EFAULT;
3718 }
3719 // Clear the high bits first, since getresgid only sets 16 bits
3720 *rgid = *egid = *sgid = 0;
3721 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
3722 }
3723 return rc;
3724 }
3725
3726 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3727 uid_t *euid,
3728 uid_t *suid) {
3729 int rc;
3730 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
3731 LSS_ERRNO == ENOSYS) {
3732 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
3733 return EFAULT;
3734 }
3735 // Clear the high bits first, since getresuid only sets 16 bits
3736 *ruid = *euid = *suid = 0;
3737 rc = LSS_NAME(getresuid)(ruid, euid, suid);
3738 }
3739 return rc;
3740 }
3741
3742 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3743 int rc;
3744 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
3745 LSS_ERRNO == ENOSYS) {
3746 if ((unsigned int)gid & ~0xFFFFu) {
3747 rc = EINVAL;
3748 } else {
3749 rc = LSS_NAME(setfsgid)(gid);
3750 }
3751 }
3752 return rc;
3753 }
3754
3755 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3756 int rc;
3757 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
3758 LSS_ERRNO == ENOSYS) {
3759 if ((unsigned int)uid & ~0xFFFFu) {
3760 rc = EINVAL;
3761 } else {
3762 rc = LSS_NAME(setfsuid)(uid);
3763 }
3764 }
3765 return rc;
3766 }
3767
3768 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3769 int rc;
3770 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
3771 LSS_ERRNO == ENOSYS) {
3772 if ((unsigned int)rgid & ~0xFFFFu ||
3773 (unsigned int)egid & ~0xFFFFu ||
3774 (unsigned int)sgid & ~0xFFFFu) {
3775 rc = EINVAL;
3776 } else {
3777 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
3778 }
3779 }
3780 return rc;
3781 }
3782
3783 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3784 int rc;
3785 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
3786 LSS_ERRNO == ENOSYS) {
3787 if ((unsigned int)ruid & ~0xFFFFu ||
3788 (unsigned int)euid & ~0xFFFFu ||
3789 (unsigned int)suid & ~0xFFFFu) {
3790 rc = EINVAL;
3791 } else {
3792 rc = LSS_NAME(setresuid)(ruid, euid, suid);
3793 }
3794 }
3795 return rc;
3796 }
3797 #endif
3798 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
3799 memset(&set->sig, 0, sizeof(set->sig));
3800 return 0;
3801 }
3802
3803 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
3804 memset(&set->sig, -1, sizeof(set->sig));
3805 return 0;
3806 }
3807
3808 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
3809 int signum) {
3810 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3811 LSS_ERRNO = EINVAL;
3812 return -1;
3813 } else {
3814 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3815 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
3816 return 0;
3817 }
3818 }
3819
3820 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
3821 int signum) {
3822 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3823 LSS_ERRNO = EINVAL;
3824 return -1;
3825 } else {
3826 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3827 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
3828 return 0;
3829 }
3830 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003831
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003832 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
3833 int signum) {
3834 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3835 LSS_ERRNO = EINVAL;
3836 return -1;
3837 } else {
3838 return !!(set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] &
3839 (1UL << ((signum - 1) % (8*sizeof(set->sig[0])))));
3840 }
3841 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003842 #if defined(__i386__) || \
3843 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3844 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
3845 defined(__PPC__) || \
3846 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003847 #define __NR__sigaction __NR_sigaction
3848 #define __NR__sigpending __NR_sigpending
3849 #define __NR__sigprocmask __NR_sigprocmask
3850 #define __NR__sigsuspend __NR_sigsuspend
3851 #define __NR__socketcall __NR_socketcall
3852 LSS_INLINE _syscall2(int, fstat64, int, f,
3853 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003854 LSS_INLINE _syscall5(int, _llseek, uint, fd,
3855 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003856 loff_t *, res, uint, wh)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003857#if defined(__s390__) && !defined(__s390x__)
3858 /* On s390, mmap2() arguments are passed in memory. */
3859 LSS_INLINE void* LSS_NAME(_mmap2)(void *s, size_t l, int p, int f, int d,
3860 off_t o) {
3861 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
3862 (unsigned long) p, (unsigned long) f,
3863 (unsigned long) d, (unsigned long) o };
3864 LSS_REG(2, buf);
3865 LSS_BODY(void*, mmap2, "0"(__r2));
3866 }
3867#else
3868 #define __NR__mmap2 __NR_mmap2
3869 LSS_INLINE _syscall6(void*, _mmap2, void*, s,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003870 size_t, l, int, p,
3871 int, f, int, d,
Bryan Chan3f6478a2016-06-14 08:38:17 -04003872 off_t, o)
3873#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003874 LSS_INLINE _syscall3(int, _sigaction, int, s,
3875 const struct kernel_old_sigaction*, a,
3876 struct kernel_old_sigaction*, o)
3877 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
3878 LSS_INLINE _syscall3(int, _sigprocmask, int, h,
3879 const unsigned long*, s,
3880 unsigned long*, o)
3881 #ifdef __PPC__
3882 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
3883 #else
3884 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
3885 int, b,
3886 unsigned long, s)
3887 #endif
3888 LSS_INLINE _syscall2(int, stat64, const char *, p,
3889 struct kernel_stat64 *, b)
3890
3891 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3892 const struct kernel_sigaction *act,
3893 struct kernel_sigaction *oldact) {
3894 int old_errno = LSS_ERRNO;
3895 int rc;
3896 struct kernel_sigaction a;
3897 if (act != NULL) {
3898 a = *act;
3899 #ifdef __i386__
3900 /* On i386, the kernel requires us to always set our own
3901 * SA_RESTORER when using realtime signals. Otherwise, it does not
3902 * know how to return from a signal handler. This function must have
3903 * a "magic" signature that the "gdb" (and maybe the kernel?) can
3904 * recognize.
3905 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
3906 * using non-realtime signals.
3907 *
3908 * TODO: Test whether ARM needs a restorer
3909 */
3910 if (!(a.sa_flags & SA_RESTORER)) {
3911 a.sa_flags |= SA_RESTORER;
3912 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
3913 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
3914 }
3915 #endif
3916 }
3917 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
3918 (KERNEL_NSIG+7)/8);
3919 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3920 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
3921 if (!act) {
3922 ptr_a = NULL;
3923 } else {
3924 oa.sa_handler_ = act->sa_handler_;
3925 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
3926 #ifndef __mips__
3927 oa.sa_restorer = act->sa_restorer;
3928 #endif
3929 oa.sa_flags = act->sa_flags;
3930 }
3931 if (!oldact) {
3932 ptr_oa = NULL;
3933 }
3934 LSS_ERRNO = old_errno;
3935 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
3936 if (rc == 0 && oldact) {
3937 if (act) {
3938 memcpy(oldact, act, sizeof(*act));
3939 } else {
3940 memset(oldact, 0, sizeof(*oldact));
3941 }
3942 oldact->sa_handler_ = ptr_oa->sa_handler_;
3943 oldact->sa_flags = ptr_oa->sa_flags;
3944 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
3945 #ifndef __mips__
3946 oldact->sa_restorer = ptr_oa->sa_restorer;
3947 #endif
3948 }
3949 }
3950 return rc;
3951 }
3952
3953 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3954 int old_errno = LSS_ERRNO;
3955 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3956 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3957 LSS_ERRNO = old_errno;
3958 LSS_NAME(sigemptyset)(set);
3959 rc = LSS_NAME(_sigpending)(&set->sig[0]);
3960 }
3961 return rc;
3962 }
3963
3964 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3965 const struct kernel_sigset_t *set,
3966 struct kernel_sigset_t *oldset) {
3967 int olderrno = LSS_ERRNO;
3968 int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3969 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3970 LSS_ERRNO = olderrno;
3971 if (oldset) {
3972 LSS_NAME(sigemptyset)(oldset);
3973 }
3974 rc = LSS_NAME(_sigprocmask)(how,
3975 set ? &set->sig[0] : NULL,
3976 oldset ? &oldset->sig[0] : NULL);
3977 }
3978 return rc;
3979 }
3980
3981 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3982 int olderrno = LSS_ERRNO;
3983 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3984 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3985 LSS_ERRNO = olderrno;
3986 rc = LSS_NAME(_sigsuspend)(
3987 #ifndef __PPC__
3988 set, 0,
3989 #endif
3990 set->sig[0]);
3991 }
3992 return rc;
3993 }
3994 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003995 #if defined(__i386__) || \
3996 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3997 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
3998 defined(__PPC__) || \
3999 (defined(__s390__) && !defined(__s390x__))
4000 /* On these architectures, implement mmap() with mmap2(). */
4001 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4002 int64_t o) {
4003 if (o % 4096) {
4004 LSS_ERRNO = EINVAL;
4005 return (void *) -1;
4006 }
4007 return LSS_NAME(_mmap2)(s, l, p, f, d, (o / 4096));
4008 }
4009 #elif defined(__s390x__)
4010 /* On s390x, mmap() arguments are passed in memory. */
4011 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4012 int64_t o) {
4013 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4014 (unsigned long) p, (unsigned long) f,
4015 (unsigned long) d, (unsigned long) o };
4016 LSS_REG(2, buf);
4017 LSS_BODY(void*, mmap, "0"(__r2));
4018 }
4019 #elif defined(__x86_64__)
4020 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
4021 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4022 int64_t o) {
4023 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
4024 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
4025 LSS_SYSCALL_ARG(d), (uint64_t)(o));
4026 }
4027 #else
4028 /* Remaining 64-bit architectures. */
4029 LSS_INLINE _syscall6(void*, mmap, void*, addr, size_t, length, int, prot,
4030 int, flags, int, fd, int64_t, offset)
4031 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004032 #if defined(__PPC__)
4033 #undef LSS_SC_LOADARGS_0
4034 #define LSS_SC_LOADARGS_0(dummy...)
4035 #undef LSS_SC_LOADARGS_1
4036 #define LSS_SC_LOADARGS_1(arg1) \
4037 __sc_4 = (unsigned long) (arg1)
4038 #undef LSS_SC_LOADARGS_2
4039 #define LSS_SC_LOADARGS_2(arg1, arg2) \
4040 LSS_SC_LOADARGS_1(arg1); \
4041 __sc_5 = (unsigned long) (arg2)
4042 #undef LSS_SC_LOADARGS_3
4043 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
4044 LSS_SC_LOADARGS_2(arg1, arg2); \
4045 __sc_6 = (unsigned long) (arg3)
4046 #undef LSS_SC_LOADARGS_4
4047 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
4048 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
4049 __sc_7 = (unsigned long) (arg4)
4050 #undef LSS_SC_LOADARGS_5
4051 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
4052 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
4053 __sc_8 = (unsigned long) (arg5)
4054 #undef LSS_SC_BODY
4055 #define LSS_SC_BODY(nr, type, opt, args...) \
4056 long __sc_ret, __sc_err; \
4057 { \
4058 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
4059 register unsigned long __sc_3 __asm__ ("r3") = opt; \
4060 register unsigned long __sc_4 __asm__ ("r4"); \
4061 register unsigned long __sc_5 __asm__ ("r5"); \
4062 register unsigned long __sc_6 __asm__ ("r6"); \
4063 register unsigned long __sc_7 __asm__ ("r7"); \
4064 register unsigned long __sc_8 __asm__ ("r8"); \
4065 LSS_SC_LOADARGS_##nr(args); \
4066 __asm__ __volatile__ \
4067 ("stwu 1, -48(1)\n\t" \
4068 "stw 4, 20(1)\n\t" \
4069 "stw 5, 24(1)\n\t" \
4070 "stw 6, 28(1)\n\t" \
4071 "stw 7, 32(1)\n\t" \
4072 "stw 8, 36(1)\n\t" \
4073 "addi 4, 1, 20\n\t" \
4074 "sc\n\t" \
4075 "mfcr %0" \
4076 : "=&r" (__sc_0), \
4077 "=&r" (__sc_3), "=&r" (__sc_4), \
4078 "=&r" (__sc_5), "=&r" (__sc_6), \
4079 "=&r" (__sc_7), "=&r" (__sc_8) \
4080 : LSS_ASMINPUT_##nr \
4081 : "cr0", "ctr", "memory"); \
4082 __sc_ret = __sc_3; \
4083 __sc_err = __sc_0; \
4084 } \
4085 LSS_RETURN(type, __sc_ret, __sc_err)
4086
4087 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4088 int flags){
4089 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
4090 }
4091
4092 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4093 const struct kernel_msghdr *msg,
4094 int flags) {
4095 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
4096 }
4097
4098 // TODO(csilvers): why is this ifdef'ed out?
4099#if 0
4100 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4101 int flags,
4102 const struct kernel_sockaddr *to,
4103 unsigned int tolen) {
4104 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
4105 }
4106#endif
4107
4108 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4109 LSS_SC_BODY(2, int, 13, s, how);
4110 }
4111
4112 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4113 LSS_SC_BODY(3, int, 1, domain, type, protocol);
4114 }
4115
4116 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4117 int sv[2]) {
4118 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
4119 }
4120 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004121 #if defined(__ARM_EABI__) || defined (__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004122 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
4123 int, flags)
4124 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
4125 msg, int, flags)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004126 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
4127 int, flags, const struct kernel_sockaddr*, to,
4128 unsigned int, tolen)
4129 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
4130 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
4131 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
4132 int*, sv)
4133 #endif
4134 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004135 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4136 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004137 #define __NR__socketcall __NR_socketcall
4138 LSS_INLINE _syscall2(int, _socketcall, int, c,
4139 va_list, a)
4140 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
4141 int rc;
4142 va_list ap;
4143 va_start(ap, op);
4144 rc = LSS_NAME(_socketcall)(op, ap);
4145 va_end(ap);
4146 return rc;
4147 }
4148
4149 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4150 int flags){
4151 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
4152 }
4153
4154 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4155 const struct kernel_msghdr *msg,
4156 int flags) {
4157 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
4158 }
4159
4160 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4161 int flags,
4162 const struct kernel_sockaddr *to,
4163 unsigned int tolen) {
4164 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
4165 }
4166
4167 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4168 return LSS_NAME(socketcall)(13, s, how);
4169 }
4170
4171 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4172 return LSS_NAME(socketcall)(1, domain, type, protocol);
4173 }
4174
4175 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4176 int sv[2]) {
4177 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
4178 }
4179 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004180 #if defined(__NR_fstatat64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004181 LSS_INLINE _syscall4(int, fstatat64, int, d,
4182 const char *, p,
4183 struct kernel_stat64 *, b, int, f)
4184 #endif
4185 #if defined(__i386__) || defined(__PPC__) || \
4186 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
4187 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
4188 int*, s, int, o)
4189 #endif
4190 #if defined(__mips__)
4191 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
4192 * both file handles through CPU registers.
4193 */
4194 LSS_INLINE int LSS_NAME(pipe)(int *p) {
4195 register unsigned long __v0 __asm__("$2") = __NR_pipe;
4196 register unsigned long __v1 __asm__("$3");
4197 register unsigned long __r7 __asm__("$7");
4198 __asm__ __volatile__ ("syscall\n"
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00004199 : "=r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004200 : "0"(__v0)
4201 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004202 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004203 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004204 unsigned long __errnovalue = __v0;
4205 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004206 return -1;
4207 } else {
4208 p[0] = __v0;
4209 p[1] = __v1;
4210 return 0;
4211 }
4212 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004213 #elif !defined(__aarch64__)
4214 // The unlink syscall has been deprecated on aarch64. We polyfill it below.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004215 LSS_INLINE _syscall1(int, pipe, int *, p)
4216 #endif
4217 /* TODO(csilvers): see if ppc can/should support this as well */
4218 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004219 defined(__ARM_EABI__) || \
4220 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) || \
4221 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004222 #define __NR__statfs64 __NR_statfs64
4223 #define __NR__fstatfs64 __NR_fstatfs64
4224 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
4225 size_t, s,struct kernel_statfs64*, b)
4226 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
4227 size_t, s,struct kernel_statfs64*, b)
4228 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
4229 struct kernel_statfs64 *b) {
4230 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
4231 }
4232 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
4233 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
4234 }
4235 #endif
4236
4237 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
4238 extern char **environ;
4239 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
4240 }
4241
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004242 LSS_INLINE pid_t LSS_NAME(gettid)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004243 pid_t tid = LSS_NAME(_gettid)();
4244 if (tid != -1) {
4245 return tid;
4246 }
4247 return LSS_NAME(getpid)();
4248 }
4249
4250 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
4251 size_t new_size, int flags, ...) {
4252 va_list ap;
4253 void *new_address, *rc;
4254 va_start(ap, flags);
4255 new_address = va_arg(ap, void *);
4256 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
4257 flags, new_address);
4258 va_end(ap);
4259 return rc;
4260 }
4261
4262 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
4263 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
4264 * then sends job control signals to the real parent, rather than to
4265 * the tracer. We reduce the risk of this happening by starting a
4266 * whole new time slice, and then quickly sending a SIGCONT signal
4267 * right after detaching from the tracee.
4268 *
4269 * We use tkill to ensure that we only issue a wakeup for the thread being
4270 * detached. Large multi threaded apps can take a long time in the kernel
4271 * processing SIGCONT.
4272 */
4273 int rc, err;
4274 LSS_NAME(sched_yield)();
4275 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
4276 err = LSS_ERRNO;
4277 LSS_NAME(tkill)(pid, SIGCONT);
4278 /* Old systems don't have tkill */
4279 if (LSS_ERRNO == ENOSYS)
4280 LSS_NAME(kill)(pid, SIGCONT);
4281 LSS_ERRNO = err;
4282 return rc;
4283 }
4284
4285 LSS_INLINE int LSS_NAME(raise)(int sig) {
4286 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
4287 }
4288
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004289 LSS_INLINE int LSS_NAME(setpgrp)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004290 return LSS_NAME(setpgid)(0, 0);
4291 }
4292
4293 LSS_INLINE int LSS_NAME(sysconf)(int name) {
4294 extern int __getpagesize(void);
4295 switch (name) {
4296 case _SC_OPEN_MAX: {
4297 struct kernel_rlimit limit;
4298#if defined(__ARM_EABI__)
4299 return LSS_NAME(ugetrlimit)(RLIMIT_NOFILE, &limit) < 0
4300 ? 8192 : limit.rlim_cur;
4301#else
4302 return LSS_NAME(getrlimit)(RLIMIT_NOFILE, &limit) < 0
4303 ? 8192 : limit.rlim_cur;
4304#endif
4305 }
4306 case _SC_PAGESIZE:
4307 return __getpagesize();
4308 default:
4309 LSS_ERRNO = ENOSYS;
4310 return -1;
4311 }
4312 }
vapier@chromium.org2273e812013-04-01 17:52:44 +00004313 #if defined(__x86_64__)
4314 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4315 LSS_INLINE ssize_t LSS_NAME(pread64)(int f, void *b, size_t c, loff_t o) {
4316 LSS_BODY(4, ssize_t, pread64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4317 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4318 }
4319
4320 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int f, const void *b, size_t c,
4321 loff_t o) {
4322 LSS_BODY(4, ssize_t, pwrite64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4323 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4324 }
4325
4326 LSS_INLINE int LSS_NAME(readahead)(int f, loff_t o, unsigned c) {
4327 LSS_BODY(3, int, readahead, LSS_SYSCALL_ARG(f), (uint64_t)(o),
4328 LSS_SYSCALL_ARG(c));
4329 }
4330 #elif defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004331 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
4332 void *, b, size_t, c,
4333 loff_t, o)
4334 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
4335 const void *, b, size_t, c,
4336 loff_t, o)
4337 LSS_INLINE _syscall3(int, readahead, int, f,
4338 loff_t, o, unsigned, c)
4339 #else
4340 #define __NR__pread64 __NR_pread64
4341 #define __NR__pwrite64 __NR_pwrite64
4342 #define __NR__readahead __NR_readahead
mseaborn@chromium.org2c73abf2012-09-15 03:46:48 +00004343 #if defined(__ARM_EABI__) || defined(__mips__)
4344 /* On ARM and MIPS, a 64-bit parameter has to be in an even-odd register
4345 * pair. Hence these calls ignore their fourth argument (r3) so that their
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004346 * fifth and sixth make such a pair (r4,r5).
4347 */
4348 #define LSS_LLARG_PAD 0,
4349 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
4350 void *, b, size_t, c,
4351 unsigned, skip, unsigned, o1, unsigned, o2)
4352 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
4353 const void *, b, size_t, c,
4354 unsigned, skip, unsigned, o1, unsigned, o2)
4355 LSS_INLINE _syscall5(int, _readahead, int, f,
4356 unsigned, skip,
4357 unsigned, o1, unsigned, o2, size_t, c)
4358 #else
4359 #define LSS_LLARG_PAD
4360 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
4361 void *, b, size_t, c, unsigned, o1,
4362 unsigned, o2)
4363 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
4364 const void *, b, size_t, c, unsigned, o1,
4365 long, o2)
4366 LSS_INLINE _syscall4(int, _readahead, int, f,
4367 unsigned, o1, unsigned, o2, size_t, c)
4368 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004369 /* We force 64bit-wide parameters onto the stack, then access each
4370 * 32-bit component individually. This guarantees that we build the
4371 * correct parameters independent of the native byte-order of the
4372 * underlying architecture.
4373 */
4374 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
4375 loff_t off) {
4376 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004377 return LSS_NAME(_pread64)(fd, buf, count,
4378 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004379 }
4380 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
4381 size_t count, loff_t off) {
4382 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004383 return LSS_NAME(_pwrite64)(fd, buf, count,
4384 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004385 }
4386 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, int len) {
4387 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004388 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], len);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004389 }
4390 #endif
4391#endif
4392
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004393#if defined(__aarch64__)
4394 LSS_INLINE _syscall3(int, dup3, int, s, int, d, int, f)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004395 LSS_INLINE _syscall4(int, newfstatat, int, dirfd, const char *, pathname,
4396 struct kernel_stat *, buf, int, flags)
4397 LSS_INLINE _syscall2(int, pipe2, int *, pipefd, int, flags)
4398 LSS_INLINE _syscall5(int, ppoll, struct kernel_pollfd *, u,
4399 unsigned int, n, const struct kernel_timespec *, t,
vapier@chromium.orgdb1e07d2015-01-16 14:14:42 +00004400 const struct kernel_sigset_t *, sigmask, size_t, s)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004401 LSS_INLINE _syscall4(int, readlinkat, int, d, const char *, p, char *, b,
4402 size_t, s)
4403#endif
4404
4405/*
4406 * Polyfills for deprecated syscalls.
4407 */
4408
4409#if defined(__aarch64__)
4410 LSS_INLINE int LSS_NAME(dup2)(int s, int d) {
4411 return LSS_NAME(dup3)(s, d, 0);
4412 }
4413
4414 LSS_INLINE int LSS_NAME(open)(const char *pathname, int flags, int mode) {
4415 return LSS_NAME(openat)(AT_FDCWD, pathname, flags, mode);
4416 }
4417
4418 LSS_INLINE int LSS_NAME(unlink)(const char *pathname) {
4419 return LSS_NAME(unlinkat)(AT_FDCWD, pathname, 0);
4420 }
4421
4422 LSS_INLINE int LSS_NAME(readlink)(const char *pathname, char *buffer,
4423 size_t size) {
4424 return LSS_NAME(readlinkat)(AT_FDCWD, pathname, buffer, size);
4425 }
4426
4427 LSS_INLINE pid_t LSS_NAME(pipe)(int *pipefd) {
4428 return LSS_NAME(pipe2)(pipefd, 0);
4429 }
4430
4431 LSS_INLINE int LSS_NAME(poll)(struct kernel_pollfd *fds, unsigned int nfds,
4432 int timeout) {
4433 struct kernel_timespec timeout_ts;
4434 struct kernel_timespec *timeout_ts_p = NULL;
4435
4436 if (timeout >= 0) {
4437 timeout_ts.tv_sec = timeout / 1000;
4438 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
4439 timeout_ts_p = &timeout_ts;
4440 }
4441 return LSS_NAME(ppoll)(fds, nfds, timeout_ts_p, NULL, 0);
4442 }
4443
4444 LSS_INLINE int LSS_NAME(stat)(const char *pathname,
4445 struct kernel_stat *buf) {
4446 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, 0);
4447 }
4448
4449 LSS_INLINE pid_t LSS_NAME(fork)(void) {
4450 // No fork syscall on aarch64 - implement by means of the clone syscall.
4451 // Note that this does not reset glibc's cached view of the PID/TID, so
4452 // some glibc interfaces might go wrong in the forked subprocess.
4453 int flags = SIGCHLD;
4454 void *child_stack = NULL;
4455 void *parent_tidptr = NULL;
4456 void *newtls = NULL;
4457 void *child_tidptr = NULL;
4458
4459 LSS_REG(0, flags);
4460 LSS_REG(1, child_stack);
4461 LSS_REG(2, parent_tidptr);
4462 LSS_REG(3, newtls);
4463 LSS_REG(4, child_tidptr);
4464 LSS_BODY(pid_t, clone, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),
4465 "r"(__r4));
4466 }
4467#endif
4468
mseaborn@chromium.orgca749372012-09-05 18:26:20 +00004469#ifdef __ANDROID__
4470 /* These restore the original values of these macros saved by the
4471 * corresponding #pragma push_macro near the top of this file. */
4472# pragma pop_macro("stat64")
4473# pragma pop_macro("fstat64")
4474# pragma pop_macro("lstat64")
4475#endif
4476
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004477#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
4478}
4479#endif
4480
4481#endif
4482#endif