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