blob: 126d7b71e19d4dd5d7ba8da2c5d4dc9a82b6906e [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 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002609 "str %4,[%2,#-4]!\n"
2610 "str %1,[%2,#-4]!\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002611
2612 /* %r0 = syscall(%r0 = flags,
2613 * %r1 = child_stack,
2614 * %r2 = parent_tidptr,
2615 * %r3 = newtls,
2616 * %r4 = child_tidptr)
2617 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002618 "mov r7, %8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002619 "swi 0x0\n"
2620
2621 /* if (%r0 != 0)
2622 * return %r0;
2623 */
2624 "movs %0,r0\n"
2625 "bne 1f\n"
2626
2627 /* In the child, now. Call "fn(arg)".
2628 */
2629 "ldr r0,[sp, #4]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002630
2631 /* When compiling for Thumb-2 the "MOV LR,PC" here
2632 * won't work because it loads PC+4 into LR,
2633 * whereas the LDR is a 4-byte instruction.
2634 * This results in the child thread always
2635 * crashing with an "Illegal Instruction" when it
2636 * returned into the middle of the LDR instruction
2637 * The instruction sequence used instead was
2638 * recommended by
2639 * "https://wiki.edubuntu.org/ARM/Thumb2PortingHowto#Quick_Reference".
2640 */
2641 #ifdef __thumb2__
2642 "ldr r7,[sp]\n"
2643 "blx r7\n"
2644 #else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002645 "mov lr,pc\n"
2646 "ldr pc,[sp]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002647 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002648
2649 /* Call _exit(%r0).
2650 */
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002651 "mov r7, %9\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002652 "swi 0x0\n"
2653 "1:\n"
2654 : "=r" (__res)
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002655 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002656 "r"(__ptid), "r"(__tls), "r"(__ctid),
2657 "i"(__NR_clone), "i"(__NR_exit)
2658 : "cc", "r7", "lr", "memory");
2659 }
2660 LSS_RETURN(int, __res);
2661 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002662 #elif defined(__aarch64__)
2663 /* Most definitions of _syscallX() neglect to mark "memory" as being
2664 * clobbered. This causes problems with compilers, that do a better job
2665 * at optimizing across __asm__ calls.
2666 * So, we just have to redefine all of the _syscallX() macros.
2667 */
2668 #undef LSS_REG
2669 #define LSS_REG(r,a) register int64_t __r##r __asm__("x"#r) = (int64_t)a
2670 #undef LSS_BODY
2671 #define LSS_BODY(type,name,args...) \
2672 register int64_t __res_x0 __asm__("x0"); \
2673 int64_t __res; \
2674 __asm__ __volatile__ ("mov x8, %1\n" \
2675 "svc 0x0\n" \
2676 : "=r"(__res_x0) \
2677 : "i"(__NR_##name) , ## args \
2678 : "x8", "memory"); \
2679 __res = __res_x0; \
2680 LSS_RETURN(type, __res)
2681 #undef _syscall0
2682 #define _syscall0(type, name) \
2683 type LSS_NAME(name)(void) { \
2684 LSS_BODY(type, name); \
2685 }
2686 #undef _syscall1
2687 #define _syscall1(type, name, type1, arg1) \
2688 type LSS_NAME(name)(type1 arg1) { \
2689 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2690 }
2691 #undef _syscall2
2692 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2693 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2694 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2695 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2696 }
2697 #undef _syscall3
2698 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2699 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2700 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2701 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2702 }
2703 #undef _syscall4
2704 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2705 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2706 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2707 LSS_REG(3, arg4); \
2708 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2709 }
2710 #undef _syscall5
2711 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2712 type5,arg5) \
2713 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2714 type5 arg5) { \
2715 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2716 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2717 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2718 "r"(__r4)); \
2719 }
2720 #undef _syscall6
2721 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2722 type5,arg5,type6,arg6) \
2723 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2724 type5 arg5, type6 arg6) { \
2725 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2726 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2727 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2728 "r"(__r4), "r"(__r5)); \
2729 }
2730
2731 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2732 int flags, void *arg, int *parent_tidptr,
2733 void *newtls, int *child_tidptr) {
2734 int64_t __res;
2735 {
2736 register uint64_t __flags __asm__("x0") = flags;
2737 register void *__stack __asm__("x1") = child_stack;
2738 register void *__ptid __asm__("x2") = parent_tidptr;
2739 register void *__tls __asm__("x3") = newtls;
2740 register int *__ctid __asm__("x4") = child_tidptr;
2741 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
2742 * used by the child.
2743 */
2744 "stp %1, %4, [%2, #-16]!\n"
2745
2746 /* %x0 = syscall(%x0 = flags,
2747 * %x1 = child_stack,
2748 * %x2 = parent_tidptr,
2749 * %x3 = newtls,
2750 * %x4 = child_tidptr)
2751 */
2752 "mov x8, %8\n"
2753 "svc 0x0\n"
2754
2755 /* if (%r0 != 0)
2756 * return %r0;
2757 */
2758 "mov %0, x0\n"
2759 "cbnz x0, 1f\n"
2760
2761 /* In the child, now. Call "fn(arg)".
2762 */
2763 "ldp x1, x0, [sp], #16\n"
2764 "blr x1\n"
2765
2766 /* Call _exit(%r0).
2767 */
2768 "mov x8, %9\n"
2769 "svc 0x0\n"
2770 "1:\n"
2771 : "=r" (__res)
2772 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2773 "r"(__ptid), "r"(__tls), "r"(__ctid),
2774 "i"(__NR_clone), "i"(__NR_exit)
2775 : "cc", "x8", "memory");
2776 }
2777 LSS_RETURN(int, __res);
2778 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002779 #elif defined(__mips__)
2780 #undef LSS_REG
2781 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
2782 (unsigned long)(a)
2783 #undef LSS_BODY
thestig@chromium.org952107f2014-08-01 02:22:56 +00002784 #undef LSS_SYSCALL_CLOBBERS
2785 #if _MIPS_SIM == _MIPS_SIM_ABI32
2786 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", \
2787 "$11", "$12", "$13", "$14", "$15", \
2788 "$24", "$25", "hi", "lo", "memory"
2789 #else
2790 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", \
2791 "$13", "$14", "$15", "$24", "$25", \
2792 "hi", "lo", "memory"
2793 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002794 #define LSS_BODY(type,name,r7,...) \
2795 register unsigned long __v0 __asm__("$2") = __NR_##name; \
2796 __asm__ __volatile__ ("syscall\n" \
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00002797 : "=r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002798 : "0"(__v0), ##__VA_ARGS__ \
thestig@chromium.org952107f2014-08-01 02:22:56 +00002799 : LSS_SYSCALL_CLOBBERS); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002800 LSS_RETURN(type, __v0, __r7)
2801 #undef _syscall0
2802 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002803 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002804 register unsigned long __r7 __asm__("$7"); \
2805 LSS_BODY(type, name, "=r"); \
2806 }
2807 #undef _syscall1
2808 #define _syscall1(type, name, type1, arg1) \
2809 type LSS_NAME(name)(type1 arg1) { \
2810 register unsigned long __r7 __asm__("$7"); \
2811 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
2812 }
2813 #undef _syscall2
2814 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2815 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2816 register unsigned long __r7 __asm__("$7"); \
2817 LSS_REG(4, arg1); LSS_REG(5, arg2); \
2818 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
2819 }
2820 #undef _syscall3
2821 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2822 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2823 register unsigned long __r7 __asm__("$7"); \
2824 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2825 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2826 }
2827 #undef _syscall4
2828 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2829 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2830 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2831 LSS_REG(7, arg4); \
2832 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2833 }
2834 #undef _syscall5
2835 #if _MIPS_SIM == _MIPS_SIM_ABI32
2836 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2837 * on the stack, whereas the new APIs use registers "r8" and "r9".
2838 */
2839 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2840 type5,arg5) \
2841 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2842 type5 arg5) { \
2843 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2844 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002845 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002846 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002847 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002848 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002849 "syscall\n" \
2850 "addiu $29, 32\n" \
2851 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002852 : "+r"(__v0), "+r" (__r7) \
2853 : "r"(__r4), "r"(__r5), \
2854 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002855 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002856 "$13", "$14", "$15", "$24", "$25", \
2857 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002858 LSS_RETURN(type, __v0, __r7); \
2859 }
2860 #else
2861 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2862 type5,arg5) \
2863 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2864 type5 arg5) { \
2865 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2866 LSS_REG(7, arg4); LSS_REG(8, arg5); \
2867 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2868 "r"(__r8)); \
2869 }
2870 #endif
2871 #undef _syscall6
2872 #if _MIPS_SIM == _MIPS_SIM_ABI32
2873 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2874 * on the stack, whereas the new APIs use registers "r8" and "r9".
2875 */
2876 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2877 type5,arg5,type6,arg6) \
2878 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2879 type5 arg5, type6 arg6) { \
2880 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2881 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002882 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002883 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002884 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002885 "sw %5, 16($29)\n" \
2886 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002887 "syscall\n" \
2888 "addiu $29, 32\n" \
2889 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002890 : "+r"(__v0), "+r" (__r7) \
2891 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002892 "r"(__r6), "r" ((unsigned long)arg5), \
2893 "r" ((unsigned long)arg6) \
2894 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002895 "$13", "$14", "$15", "$24", "$25", \
2896 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002897 LSS_RETURN(type, __v0, __r7); \
2898 }
2899 #else
2900 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2901 type5,arg5,type6,arg6) \
2902 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2903 type5 arg5,type6 arg6) { \
2904 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2905 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
2906 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2907 "r"(__r8), "r"(__r9)); \
2908 }
2909 #endif
2910 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2911 int flags, void *arg, int *parent_tidptr,
2912 void *newtls, int *child_tidptr) {
vapier@chromium.orge0797682015-02-20 20:45:56 +00002913 register unsigned long __v0 __asm__("$2") = -EINVAL;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002914 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
2915 {
2916 register int __flags __asm__("$4") = flags;
2917 register void *__stack __asm__("$5") = child_stack;
2918 register void *__ptid __asm__("$6") = parent_tidptr;
2919 register int *__ctid __asm__("$8") = child_tidptr;
2920 __asm__ __volatile__(
2921 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2922 "subu $29,24\n"
2923 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2924 "sub $29,16\n"
2925 #else
2926 "dsubu $29,16\n"
2927 #endif
2928
2929 /* if (fn == NULL || child_stack == NULL)
2930 * return -EINVAL;
2931 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002932 "beqz %4,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002933 "beqz %5,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002934
2935 /* Push "arg" and "fn" onto the stack that will be
2936 * used by the child.
2937 */
2938 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002939 "subu %5,32\n"
2940 "sw %4,0(%5)\n"
2941 "sw %7,4(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002942 #elif _MIPS_SIM == _MIPS_SIM_NABI32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002943 "sub %5,32\n"
2944 "sw %4,0(%5)\n"
2945 "sw %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002946 #else
vapier@chromium.orge0797682015-02-20 20:45:56 +00002947 "dsubu %5,32\n"
2948 "sd %4,0(%5)\n"
2949 "sd %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002950 #endif
2951
2952 /* $7 = syscall($4 = flags,
2953 * $5 = child_stack,
2954 * $6 = parent_tidptr,
2955 * $7 = newtls,
2956 * $8 = child_tidptr)
2957 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002958 "li $2,%2\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002959 "syscall\n"
2960
2961 /* if ($7 != 0)
2962 * return $2;
2963 */
2964 "bnez $7,1f\n"
2965 "bnez $2,1f\n"
2966
2967 /* In the child, now. Call "fn(arg)".
2968 */
2969 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2970 "lw $25,0($29)\n"
2971 "lw $4,4($29)\n"
2972 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2973 "lw $25,0($29)\n"
2974 "lw $4,8($29)\n"
2975 #else
2976 "ld $25,0($29)\n"
2977 "ld $4,8($29)\n"
2978 #endif
2979 "jalr $25\n"
2980
2981 /* Call _exit($2)
2982 */
2983 "move $4,$2\n"
vapier@chromium.orge0797682015-02-20 20:45:56 +00002984 "li $2,%3\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002985 "syscall\n"
2986
2987 "1:\n"
2988 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2989 "addu $29, 24\n"
2990 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2991 "add $29, 16\n"
2992 #else
2993 "daddu $29,16\n"
2994 #endif
petarj@mips.com0ece1c62013-04-10 00:28:04 +00002995 : "+r" (__v0), "+r" (__r7)
vapier@chromium.orge0797682015-02-20 20:45:56 +00002996 : "i"(__NR_clone), "i"(__NR_exit), "r"(fn),
2997 "r"(__stack), "r"(__flags), "r"(arg),
2998 "r"(__ptid), "r"(__ctid)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002999 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003000 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003001 }
3002 LSS_RETURN(int, __v0, __r7);
3003 }
3004 #elif defined (__PPC__)
3005 #undef LSS_LOADARGS_0
3006 #define LSS_LOADARGS_0(name, dummy...) \
3007 __sc_0 = __NR_##name
3008 #undef LSS_LOADARGS_1
3009 #define LSS_LOADARGS_1(name, arg1) \
3010 LSS_LOADARGS_0(name); \
3011 __sc_3 = (unsigned long) (arg1)
3012 #undef LSS_LOADARGS_2
3013 #define LSS_LOADARGS_2(name, arg1, arg2) \
3014 LSS_LOADARGS_1(name, arg1); \
3015 __sc_4 = (unsigned long) (arg2)
3016 #undef LSS_LOADARGS_3
3017 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
3018 LSS_LOADARGS_2(name, arg1, arg2); \
3019 __sc_5 = (unsigned long) (arg3)
3020 #undef LSS_LOADARGS_4
3021 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
3022 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
3023 __sc_6 = (unsigned long) (arg4)
3024 #undef LSS_LOADARGS_5
3025 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
3026 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
3027 __sc_7 = (unsigned long) (arg5)
3028 #undef LSS_LOADARGS_6
3029 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
3030 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
3031 __sc_8 = (unsigned long) (arg6)
3032 #undef LSS_ASMINPUT_0
3033 #define LSS_ASMINPUT_0 "0" (__sc_0)
3034 #undef LSS_ASMINPUT_1
3035 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
3036 #undef LSS_ASMINPUT_2
3037 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
3038 #undef LSS_ASMINPUT_3
3039 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
3040 #undef LSS_ASMINPUT_4
3041 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
3042 #undef LSS_ASMINPUT_5
3043 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
3044 #undef LSS_ASMINPUT_6
3045 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
3046 #undef LSS_BODY
3047 #define LSS_BODY(nr, type, name, args...) \
3048 long __sc_ret, __sc_err; \
3049 { \
3050 register unsigned long __sc_0 __asm__ ("r0"); \
3051 register unsigned long __sc_3 __asm__ ("r3"); \
3052 register unsigned long __sc_4 __asm__ ("r4"); \
3053 register unsigned long __sc_5 __asm__ ("r5"); \
3054 register unsigned long __sc_6 __asm__ ("r6"); \
3055 register unsigned long __sc_7 __asm__ ("r7"); \
3056 register unsigned long __sc_8 __asm__ ("r8"); \
3057 \
3058 LSS_LOADARGS_##nr(name, args); \
3059 __asm__ __volatile__ \
3060 ("sc\n\t" \
3061 "mfcr %0" \
3062 : "=&r" (__sc_0), \
3063 "=&r" (__sc_3), "=&r" (__sc_4), \
3064 "=&r" (__sc_5), "=&r" (__sc_6), \
3065 "=&r" (__sc_7), "=&r" (__sc_8) \
3066 : LSS_ASMINPUT_##nr \
3067 : "cr0", "ctr", "memory", \
3068 "r9", "r10", "r11", "r12"); \
3069 __sc_ret = __sc_3; \
3070 __sc_err = __sc_0; \
3071 } \
3072 LSS_RETURN(type, __sc_ret, __sc_err)
3073 #undef _syscall0
3074 #define _syscall0(type, name) \
3075 type LSS_NAME(name)(void) { \
3076 LSS_BODY(0, type, name); \
3077 }
3078 #undef _syscall1
3079 #define _syscall1(type, name, type1, arg1) \
3080 type LSS_NAME(name)(type1 arg1) { \
3081 LSS_BODY(1, type, name, arg1); \
3082 }
3083 #undef _syscall2
3084 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3085 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3086 LSS_BODY(2, type, name, arg1, arg2); \
3087 }
3088 #undef _syscall3
3089 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3090 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3091 LSS_BODY(3, type, name, arg1, arg2, arg3); \
3092 }
3093 #undef _syscall4
3094 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3095 type4, arg4) \
3096 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3097 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
3098 }
3099 #undef _syscall5
3100 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3101 type4, arg4, type5, arg5) \
3102 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3103 type5 arg5) { \
3104 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
3105 }
3106 #undef _syscall6
3107 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3108 type4, arg4, type5, arg5, type6, arg6) \
3109 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3110 type5 arg5, type6 arg6) { \
3111 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
3112 }
3113 /* clone function adapted from glibc 2.3.6 clone.S */
3114 /* TODO(csilvers): consider wrapping some args up in a struct, like we
3115 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
3116 */
3117 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3118 int flags, void *arg, int *parent_tidptr,
3119 void *newtls, int *child_tidptr) {
3120 long __ret, __err;
3121 {
3122 register int (*__fn)(void *) __asm__ ("r8") = fn;
3123 register void *__cstack __asm__ ("r4") = child_stack;
3124 register int __flags __asm__ ("r3") = flags;
3125 register void * __arg __asm__ ("r9") = arg;
3126 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
3127 register void * __newtls __asm__ ("r6") = newtls;
3128 register int * __ctidptr __asm__ ("r7") = child_tidptr;
3129 __asm__ __volatile__(
3130 /* check for fn == NULL
3131 * and child_stack == NULL
3132 */
3133 "cmpwi cr0, %6, 0\n\t"
3134 "cmpwi cr1, %7, 0\n\t"
3135 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
3136 "beq- cr0, 1f\n\t"
3137
3138 /* set up stack frame for child */
3139 "clrrwi %7, %7, 4\n\t"
3140 "li 0, 0\n\t"
3141 "stwu 0, -16(%7)\n\t"
3142
3143 /* fn, arg, child_stack are saved across the syscall: r28-30 */
3144 "mr 28, %6\n\t"
3145 "mr 29, %7\n\t"
3146 "mr 27, %9\n\t"
3147
3148 /* syscall */
3149 "li 0, %4\n\t"
3150 /* flags already in r3
3151 * child_stack already in r4
3152 * ptidptr already in r5
3153 * newtls already in r6
3154 * ctidptr already in r7
3155 */
3156 "sc\n\t"
3157
3158 /* Test if syscall was successful */
3159 "cmpwi cr1, 3, 0\n\t"
3160 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
3161 "bne- cr1, 1f\n\t"
3162
3163 /* Do the function call */
3164 "mtctr 28\n\t"
3165 "mr 3, 27\n\t"
3166 "bctrl\n\t"
3167
3168 /* Call _exit(r3) */
3169 "li 0, %5\n\t"
3170 "sc\n\t"
3171
3172 /* Return to parent */
3173 "1:\n"
3174 "mfcr %1\n\t"
3175 "mr %0, 3\n\t"
3176 : "=r" (__ret), "=r" (__err)
3177 : "0" (-1), "1" (EINVAL),
3178 "i" (__NR_clone), "i" (__NR_exit),
3179 "r" (__fn), "r" (__cstack), "r" (__flags),
3180 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
3181 "r" (__ctidptr)
3182 : "cr0", "cr1", "memory", "ctr",
3183 "r0", "r29", "r27", "r28");
3184 }
3185 LSS_RETURN(int, __ret, __err);
3186 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003187 #elif defined(__s390__)
3188 #undef LSS_REG
3189 #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
3190 #undef LSS_BODY
3191 #define LSS_BODY(type, name, args...) \
3192 register unsigned long __nr __asm__("r1") \
3193 = (unsigned long)(__NR_##name); \
3194 register long __res_r2 __asm__("r2"); \
3195 long __res; \
3196 __asm__ __volatile__ \
3197 ("svc 0\n\t" \
3198 : "=d"(__res_r2) \
3199 : "d"(__nr), ## args \
3200 : "memory"); \
3201 __res = __res_r2; \
3202 LSS_RETURN(type, __res)
3203 #undef _syscall0
3204 #define _syscall0(type, name) \
3205 type LSS_NAME(name)(void) { \
3206 LSS_BODY(type, name); \
3207 }
3208 #undef _syscall1
3209 #define _syscall1(type, name, type1, arg1) \
3210 type LSS_NAME(name)(type1 arg1) { \
3211 LSS_REG(2, arg1); \
3212 LSS_BODY(type, name, "0"(__r2)); \
3213 }
3214 #undef _syscall2
3215 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3216 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3217 LSS_REG(2, arg1); LSS_REG(3, arg2); \
3218 LSS_BODY(type, name, "0"(__r2), "d"(__r3)); \
3219 }
3220 #undef _syscall3
3221 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3222 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3223 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3224 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4)); \
3225 }
3226 #undef _syscall4
3227 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3228 type4, arg4) \
3229 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3230 type4 arg4) { \
3231 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3232 LSS_REG(5, arg4); \
3233 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3234 "d"(__r5)); \
3235 }
3236 #undef _syscall5
3237 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3238 type4, arg4, type5, arg5) \
3239 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3240 type4 arg4, type5 arg5) { \
3241 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3242 LSS_REG(5, arg4); LSS_REG(6, arg5); \
3243 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3244 "d"(__r5), "d"(__r6)); \
3245 }
3246 #undef _syscall6
3247 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3248 type4, arg4, type5, arg5, type6, arg6) \
3249 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3250 type4 arg4, type5 arg5, type6 arg6) { \
3251 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3252 LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
3253 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3254 "d"(__r5), "d"(__r6), "d"(__r7)); \
3255 }
3256 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3257 int flags, void *arg, int *parent_tidptr,
3258 void *newtls, int *child_tidptr) {
3259 long __ret;
3260 {
3261 register int (*__fn)(void *) __asm__ ("r1") = fn;
3262 register void *__cstack __asm__ ("r2") = child_stack;
3263 register int __flags __asm__ ("r3") = flags;
3264 register void *__arg __asm__ ("r0") = arg;
3265 register int *__ptidptr __asm__ ("r4") = parent_tidptr;
3266 register void *__newtls __asm__ ("r6") = newtls;
3267 register int *__ctidptr __asm__ ("r5") = child_tidptr;
3268 __asm__ __volatile__ (
3269 #ifndef __s390x__
3270 /* arg already in r0 */
3271 "ltr %4, %4\n\t" /* check fn, which is already in r1 */
3272 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3273 "ltr %5, %5\n\t" /* check child_stack, which is already in r2 */
3274 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3275 /* flags already in r3 */
3276 /* parent_tidptr already in r4 */
3277 /* child_tidptr already in r5 */
3278 /* newtls already in r6 */
3279 "svc %2\n\t" /* invoke clone syscall */
3280 "ltr %0,%%r2\n\t" /* load return code into __ret and test */
3281 "jnz 1f\n\t" /* return to parent if non-zero */
3282 /* start child thread */
3283 "lr %%r2, %7\n\t" /* set first parameter to void *arg */
3284 "ahi %%r15, -96\n\t" /* make room on the stack for the save area */
3285 "xc 0(4,%%r15), 0(%%r15)\n\t"
3286 "basr %%r14, %4\n\t" /* jump to fn */
3287 "svc %3\n" /* invoke exit syscall */
3288 "1:\n"
3289 #else
3290 /* arg already in r0 */
3291 "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
3292 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3293 "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
3294 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3295 /* flags already in r3 */
3296 /* parent_tidptr already in r4 */
3297 /* child_tidptr already in r5 */
3298 /* newtls already in r6 */
3299 "svc %2\n\t" /* invoke clone syscall */
3300 "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
3301 "jnz 1f\n\t" /* return to parent if non-zero */
3302 /* start child thread */
3303 "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
3304 "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
3305 "xc 0(8,%%r15), 0(%%r15)\n\t"
3306 "basr %%r14, %4\n\t" /* jump to fn */
3307 "svc %3\n" /* invoke exit syscall */
3308 "1:\n"
3309 #endif
3310 : "=r" (__ret)
3311 : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
3312 "d" (__fn), "d" (__cstack), "d" (__flags), "d" (__arg),
3313 "d" (__ptidptr), "d" (__newtls), "d" (__ctidptr)
3314 : "cc", "r14", "memory"
3315 );
3316 }
3317 LSS_RETURN(int, __ret);
3318 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003319 #endif
3320 #define __NR__exit __NR_exit
3321 #define __NR__gettid __NR_gettid
3322 #define __NR__mremap __NR_mremap
phosek@chromium.orga9c02722013-08-16 17:31:42 +00003323 LSS_INLINE _syscall1(void *, brk, void *, e)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003324 LSS_INLINE _syscall1(int, chdir, const char *,p)
3325 LSS_INLINE _syscall1(int, close, int, f)
3326 LSS_INLINE _syscall2(int, clock_getres, int, c,
3327 struct kernel_timespec*, t)
3328 LSS_INLINE _syscall2(int, clock_gettime, int, c,
3329 struct kernel_timespec*, t)
3330 LSS_INLINE _syscall1(int, dup, int, f)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003331 #if !defined(__aarch64__)
3332 // The dup2 syscall has been deprecated on aarch64. We polyfill it below.
3333 LSS_INLINE _syscall2(int, dup2, int, s,
3334 int, d)
3335 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003336 LSS_INLINE _syscall3(int, execve, const char*, f,
3337 const char*const*,a,const char*const*, e)
3338 LSS_INLINE _syscall1(int, _exit, int, e)
3339 LSS_INLINE _syscall1(int, exit_group, int, e)
3340 LSS_INLINE _syscall3(int, fcntl, int, f,
3341 int, c, long, a)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003342 #if !defined(__aarch64__)
3343 // The fork syscall has been deprecated on aarch64. We polyfill it below.
3344 LSS_INLINE _syscall0(pid_t, fork)
3345 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003346 LSS_INLINE _syscall2(int, fstat, int, f,
3347 struct kernel_stat*, b)
3348 LSS_INLINE _syscall2(int, fstatfs, int, f,
3349 struct kernel_statfs*, b)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003350 #if defined(__x86_64__)
3351 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3352 LSS_INLINE int LSS_NAME(ftruncate)(int f, off_t l) {
3353 LSS_BODY(2, int, ftruncate, LSS_SYSCALL_ARG(f), (uint64_t)(l));
3354 }
3355 #else
3356 LSS_INLINE _syscall2(int, ftruncate, int, f,
3357 off_t, l)
3358 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003359 LSS_INLINE _syscall4(int, futex, int*, a,
3360 int, o, int, v,
3361 struct kernel_timespec*, t)
3362 LSS_INLINE _syscall3(int, getdents, int, f,
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003363 struct kernel_dirent*, d, int, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003364 LSS_INLINE _syscall3(int, getdents64, int, f,
3365 struct kernel_dirent64*, d, int, c)
3366 LSS_INLINE _syscall0(gid_t, getegid)
3367 LSS_INLINE _syscall0(uid_t, geteuid)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003368 #if !defined(__aarch64__)
3369 // The getgprp syscall has been deprecated on aarch64.
3370 LSS_INLINE _syscall0(pid_t, getpgrp)
3371 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003372 LSS_INLINE _syscall0(pid_t, getpid)
3373 LSS_INLINE _syscall0(pid_t, getppid)
3374 LSS_INLINE _syscall2(int, getpriority, int, a,
3375 int, b)
3376 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
3377 gid_t *, e, gid_t *, s)
3378 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
3379 uid_t *, e, uid_t *, s)
3380#if !defined(__ARM_EABI__)
3381 LSS_INLINE _syscall2(int, getrlimit, int, r,
3382 struct kernel_rlimit*, l)
3383#endif
3384 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
3385 LSS_INLINE _syscall0(pid_t, _gettid)
3386 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
3387 void*, tz)
3388 LSS_INLINE _syscall5(int, setxattr, const char *,p,
3389 const char *, n, const void *,v,
3390 size_t, s, int, f)
3391 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
3392 const char *, n, const void *,v,
3393 size_t, s, int, f)
3394 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
3395 const char *, n, void *, v, size_t, s)
3396 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
3397 const char *, n, void *, v, size_t, s)
3398 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
3399 char *, l, size_t, s)
3400 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
3401 char *, l, size_t, s)
3402 LSS_INLINE _syscall3(int, ioctl, int, d,
3403 int, r, void *, a)
3404 LSS_INLINE _syscall2(int, ioprio_get, int, which,
3405 int, who)
3406 LSS_INLINE _syscall3(int, ioprio_set, int, which,
3407 int, who, int, ioprio)
3408 LSS_INLINE _syscall2(int, kill, pid_t, p,
3409 int, s)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003410 #if defined(__x86_64__)
3411 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3412 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
3413 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
3414 LSS_SYSCALL_ARG(w));
3415 }
3416 #else
3417 LSS_INLINE _syscall3(off_t, lseek, int, f,
3418 off_t, o, int, w)
3419 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003420 LSS_INLINE _syscall2(int, munmap, void*, s,
3421 size_t, l)
3422 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
3423 unsigned long, n, void **,g, int *, d,
3424 int *, s, int, f)
3425 LSS_INLINE _syscall3(int, mprotect, const void *,a,
3426 size_t, l, int, p)
3427 LSS_INLINE _syscall5(void*, _mremap, void*, o,
3428 size_t, os, size_t, ns,
3429 unsigned long, f, void *, a)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003430 #if !defined(__aarch64__)
3431 // The open and poll syscalls have been deprecated on aarch64. We polyfill
3432 // them below.
3433 LSS_INLINE _syscall3(int, open, const char*, p,
3434 int, f, int, m)
3435 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
3436 unsigned int, n, int, t)
3437 #endif
mseaborn@chromium.orge6c76822013-08-31 00:08:44 +00003438 LSS_INLINE _syscall5(int, prctl, int, option,
3439 unsigned long, arg2,
3440 unsigned long, arg3,
3441 unsigned long, arg4,
3442 unsigned long, arg5)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003443 LSS_INLINE _syscall4(long, ptrace, int, r,
3444 pid_t, p, void *, a, void *, d)
3445 #if defined(__NR_quotactl)
3446 // Defined on x86_64 / i386 only
3447 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
3448 int, id, caddr_t, addr)
3449 #endif
3450 LSS_INLINE _syscall3(ssize_t, read, int, f,
3451 void *, b, size_t, c)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003452 #if !defined(__aarch64__)
3453 // The readlink syscall has been deprecated on aarch64. We polyfill below.
3454 LSS_INLINE _syscall3(int, readlink, const char*, p,
3455 char*, b, size_t, s)
3456 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003457 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
3458 const struct kernel_sigaction*, a,
3459 struct kernel_sigaction*, o, size_t, c)
3460 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
3461 size_t, c)
3462 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
3463 const struct kernel_sigset_t*, s,
3464 struct kernel_sigset_t*, o, size_t, c)
3465 LSS_INLINE _syscall2(int, rt_sigsuspend,
3466 const struct kernel_sigset_t*, s, size_t, c)
3467 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
3468 unsigned int, l, unsigned long *, m)
3469 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
3470 unsigned int, l, unsigned long *, m)
3471 LSS_INLINE _syscall0(int, sched_yield)
3472 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
3473 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
3474 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
3475 LSS_INLINE _syscall1(int, setuid, uid_t, u)
3476 LSS_INLINE _syscall1(int, setgid, gid_t, g)
3477 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
3478 pid_t, g)
3479 LSS_INLINE _syscall3(int, setpriority, int, a,
3480 int, b, int, p)
3481 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
3482 gid_t, e, gid_t, s)
3483 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
3484 uid_t, e, uid_t, s)
3485 LSS_INLINE _syscall2(int, setrlimit, int, r,
3486 const struct kernel_rlimit*, l)
3487 LSS_INLINE _syscall0(pid_t, setsid)
3488 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
3489 const stack_t*, o)
3490 #if defined(__NR_sigreturn)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003491 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003492 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003493 #if !defined(__aarch64__)
3494 // The stat syscall has been deprecated on aarch64. We polyfill it below.
3495 LSS_INLINE _syscall2(int, stat, const char*, f,
3496 struct kernel_stat*, b)
3497 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003498 LSS_INLINE _syscall2(int, statfs, const char*, f,
3499 struct kernel_statfs*, b)
3500 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
3501 pid_t, t, int, s)
3502 LSS_INLINE _syscall2(int, tkill, pid_t, p,
3503 int, s)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003504 #if !defined(__aarch64__)
3505 // The unlink syscall has been deprecated on aarch64. We polyfill it below.
3506 LSS_INLINE _syscall1(int, unlink, const char*, f)
3507 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003508 LSS_INLINE _syscall3(ssize_t, write, int, f,
3509 const void *, b, size_t, c)
3510 LSS_INLINE _syscall3(ssize_t, writev, int, f,
3511 const struct kernel_iovec*, v, size_t, c)
3512 #if defined(__NR_getcpu)
3513 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00003514 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003515 #endif
3516 #if defined(__x86_64__) || \
3517 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
3518 LSS_INLINE _syscall3(int, recvmsg, int, s,
3519 struct kernel_msghdr*, m, int, f)
3520 LSS_INLINE _syscall3(int, sendmsg, int, s,
3521 const struct kernel_msghdr*, m, int, f)
3522 LSS_INLINE _syscall6(int, sendto, int, s,
3523 const void*, m, size_t, l,
3524 int, f,
3525 const struct kernel_sockaddr*, a, int, t)
3526 LSS_INLINE _syscall2(int, shutdown, int, s,
3527 int, h)
3528 LSS_INLINE _syscall3(int, socket, int, d,
3529 int, t, int, p)
3530 LSS_INLINE _syscall4(int, socketpair, int, d,
3531 int, t, int, p, int*, s)
3532 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003533 #if defined(__NR_fadvise64)
3534 #if defined(__x86_64__)
3535 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3536 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset, loff_t len,
3537 int advice) {
3538 LSS_BODY(4, int, fadvise64, LSS_SYSCALL_ARG(fd), (uint64_t)(offset),
3539 (uint64_t)(len), LSS_SYSCALL_ARG(advice));
3540 }
3541 #else
3542 LSS_INLINE _syscall4(int, fadvise64,
3543 int, fd, loff_t, offset, loff_t, len, int, advice)
3544 #endif
3545 #elif defined(__i386__)
3546 #define __NR__fadvise64_64 __NR_fadvise64_64
3547 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
3548 unsigned, offset_lo, unsigned, offset_hi,
3549 unsigned, len_lo, unsigned, len_hi,
3550 int, advice)
3551
3552 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3553 loff_t len, int advice) {
3554 return LSS_NAME(_fadvise64_64)(fd,
3555 (unsigned)offset, (unsigned)(offset >>32),
3556 (unsigned)len, (unsigned)(len >> 32),
3557 advice);
3558 }
3559
3560 #elif defined(__s390__) && !defined(__s390x__)
3561 #define __NR__fadvise64_64 __NR_fadvise64_64
3562 struct kernel_fadvise64_64_args {
3563 int fd;
3564 long long offset;
3565 long long len;
3566 int advice;
3567 };
3568
3569 LSS_INLINE _syscall1(int, _fadvise64_64,
3570 struct kernel_fadvise64_64_args *args)
3571
3572 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3573 loff_t len, int advice) {
3574 struct kernel_fadvise64_64_args args = { fd, offset, len, advice };
3575 return LSS_NAME(_fadvise64_64)(&args);
3576 }
3577 #endif
3578 #if defined(__NR_fallocate)
3579 #if defined(__x86_64__)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003580 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3581 LSS_INLINE int LSS_NAME(fallocate)(int f, int mode, loff_t offset,
3582 loff_t len) {
3583 LSS_BODY(4, int, fallocate, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(mode),
3584 (uint64_t)(offset), (uint64_t)(len));
3585 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003586 #elif defined(__i386__) || (defined(__s390__) && !defined(__s390x__))
3587 #define __NR__fallocate __NR_fallocate
3588 LSS_INLINE _syscall6(int, _fallocate, int, fd,
3589 int, mode,
3590 unsigned, offset_lo, unsigned, offset_hi,
3591 unsigned, len_lo, unsigned, len_hi)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003592
Bryan Chan3f6478a2016-06-14 08:38:17 -04003593 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
3594 loff_t offset, loff_t len) {
3595 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
3596 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
3597 }
3598 #else
3599 LSS_INLINE _syscall4(int, fallocate,
3600 int, f, int, mode, loff_t, offset, loff_t, len)
3601 #endif
3602 #endif
3603 #if defined(__x86_64__) || defined(__s390x__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003604 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3605 gid_t *egid,
3606 gid_t *sgid) {
3607 return LSS_NAME(getresgid)(rgid, egid, sgid);
3608 }
3609
3610 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3611 uid_t *euid,
3612 uid_t *suid) {
3613 return LSS_NAME(getresuid)(ruid, euid, suid);
3614 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003615 LSS_INLINE _syscall4(int, newfstatat, int, d,
3616 const char *, p,
3617 struct kernel_stat*, b, int, f)
3618
3619 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3620 return LSS_NAME(setfsgid)(gid);
3621 }
3622
3623 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3624 return LSS_NAME(setfsuid)(uid);
3625 }
3626
3627 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3628 return LSS_NAME(setresgid)(rgid, egid, sgid);
3629 }
3630
3631 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3632 return LSS_NAME(setresuid)(ruid, euid, suid);
3633 }
3634
3635 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3636 const struct kernel_sigaction *act,
3637 struct kernel_sigaction *oldact) {
Bryan Chan3f6478a2016-06-14 08:38:17 -04003638 #if defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003639 /* On x86_64, the kernel requires us to always set our own
3640 * SA_RESTORER in order to be able to return from a signal handler.
3641 * This function must have a "magic" signature that the "gdb"
3642 * (and maybe the kernel?) can recognize.
3643 */
3644 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
3645 struct kernel_sigaction a = *act;
3646 a.sa_flags |= SA_RESTORER;
3647 a.sa_restorer = LSS_NAME(restore_rt)();
3648 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
3649 (KERNEL_NSIG+7)/8);
Bryan Chan3f6478a2016-06-14 08:38:17 -04003650 } else
3651 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003652 return LSS_NAME(rt_sigaction)(signum, act, oldact,
3653 (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003654 }
3655
3656 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3657 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3658 }
3659
3660 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3661 const struct kernel_sigset_t *set,
3662 struct kernel_sigset_t *oldset) {
3663 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3664 }
3665
3666 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3667 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3668 }
3669 #endif
3670 #if defined(__x86_64__) || defined(__ARM_ARCH_3__) || \
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003671 defined(__ARM_EABI__) || defined(__aarch64__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04003672 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32) || \
3673 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003674 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
3675 int*, s, int, o,
3676 struct kernel_rusage*, r)
3677
3678 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){
3679 return LSS_NAME(wait4)(pid, status, options, 0);
3680 }
3681 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003682 #if defined(__NR_openat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003683 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003684 #endif
3685 #if defined(__NR_unlinkat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003686 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
3687 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003688 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3689 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003690 #define __NR__getresgid32 __NR_getresgid32
3691 #define __NR__getresuid32 __NR_getresuid32
3692 #define __NR__setfsgid32 __NR_setfsgid32
3693 #define __NR__setfsuid32 __NR_setfsuid32
3694 #define __NR__setresgid32 __NR_setresgid32
3695 #define __NR__setresuid32 __NR_setresuid32
3696#if defined(__ARM_EABI__)
3697 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
3698 struct kernel_rlimit*, l)
3699#endif
3700 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
3701 gid_t *, e, gid_t *, s)
3702 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
3703 uid_t *, e, uid_t *, s)
3704 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
3705 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
3706 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
3707 gid_t, e, gid_t, s)
3708 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
3709 uid_t, e, uid_t, s)
3710
3711 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3712 gid_t *egid,
3713 gid_t *sgid) {
3714 int rc;
3715 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
3716 LSS_ERRNO == ENOSYS) {
3717 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
3718 return EFAULT;
3719 }
3720 // Clear the high bits first, since getresgid only sets 16 bits
3721 *rgid = *egid = *sgid = 0;
3722 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
3723 }
3724 return rc;
3725 }
3726
3727 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3728 uid_t *euid,
3729 uid_t *suid) {
3730 int rc;
3731 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
3732 LSS_ERRNO == ENOSYS) {
3733 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
3734 return EFAULT;
3735 }
3736 // Clear the high bits first, since getresuid only sets 16 bits
3737 *ruid = *euid = *suid = 0;
3738 rc = LSS_NAME(getresuid)(ruid, euid, suid);
3739 }
3740 return rc;
3741 }
3742
3743 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3744 int rc;
3745 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
3746 LSS_ERRNO == ENOSYS) {
3747 if ((unsigned int)gid & ~0xFFFFu) {
3748 rc = EINVAL;
3749 } else {
3750 rc = LSS_NAME(setfsgid)(gid);
3751 }
3752 }
3753 return rc;
3754 }
3755
3756 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3757 int rc;
3758 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
3759 LSS_ERRNO == ENOSYS) {
3760 if ((unsigned int)uid & ~0xFFFFu) {
3761 rc = EINVAL;
3762 } else {
3763 rc = LSS_NAME(setfsuid)(uid);
3764 }
3765 }
3766 return rc;
3767 }
3768
3769 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3770 int rc;
3771 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
3772 LSS_ERRNO == ENOSYS) {
3773 if ((unsigned int)rgid & ~0xFFFFu ||
3774 (unsigned int)egid & ~0xFFFFu ||
3775 (unsigned int)sgid & ~0xFFFFu) {
3776 rc = EINVAL;
3777 } else {
3778 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
3779 }
3780 }
3781 return rc;
3782 }
3783
3784 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3785 int rc;
3786 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
3787 LSS_ERRNO == ENOSYS) {
3788 if ((unsigned int)ruid & ~0xFFFFu ||
3789 (unsigned int)euid & ~0xFFFFu ||
3790 (unsigned int)suid & ~0xFFFFu) {
3791 rc = EINVAL;
3792 } else {
3793 rc = LSS_NAME(setresuid)(ruid, euid, suid);
3794 }
3795 }
3796 return rc;
3797 }
3798 #endif
3799 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
3800 memset(&set->sig, 0, sizeof(set->sig));
3801 return 0;
3802 }
3803
3804 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
3805 memset(&set->sig, -1, sizeof(set->sig));
3806 return 0;
3807 }
3808
3809 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
3810 int signum) {
3811 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3812 LSS_ERRNO = EINVAL;
3813 return -1;
3814 } else {
3815 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3816 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
3817 return 0;
3818 }
3819 }
3820
3821 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
3822 int signum) {
3823 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3824 LSS_ERRNO = EINVAL;
3825 return -1;
3826 } else {
3827 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3828 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
3829 return 0;
3830 }
3831 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003832
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003833 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
3834 int signum) {
3835 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3836 LSS_ERRNO = EINVAL;
3837 return -1;
3838 } else {
3839 return !!(set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] &
3840 (1UL << ((signum - 1) % (8*sizeof(set->sig[0])))));
3841 }
3842 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003843 #if defined(__i386__) || \
3844 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3845 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
3846 defined(__PPC__) || \
3847 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003848 #define __NR__sigaction __NR_sigaction
3849 #define __NR__sigpending __NR_sigpending
3850 #define __NR__sigprocmask __NR_sigprocmask
3851 #define __NR__sigsuspend __NR_sigsuspend
3852 #define __NR__socketcall __NR_socketcall
3853 LSS_INLINE _syscall2(int, fstat64, int, f,
3854 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003855 LSS_INLINE _syscall5(int, _llseek, uint, fd,
3856 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003857 loff_t *, res, uint, wh)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003858#if defined(__s390__) && !defined(__s390x__)
3859 /* On s390, mmap2() arguments are passed in memory. */
3860 LSS_INLINE void* LSS_NAME(_mmap2)(void *s, size_t l, int p, int f, int d,
3861 off_t o) {
3862 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
3863 (unsigned long) p, (unsigned long) f,
3864 (unsigned long) d, (unsigned long) o };
3865 LSS_REG(2, buf);
3866 LSS_BODY(void*, mmap2, "0"(__r2));
3867 }
3868#else
3869 #define __NR__mmap2 __NR_mmap2
3870 LSS_INLINE _syscall6(void*, _mmap2, void*, s,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003871 size_t, l, int, p,
3872 int, f, int, d,
Bryan Chan3f6478a2016-06-14 08:38:17 -04003873 off_t, o)
3874#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003875 LSS_INLINE _syscall3(int, _sigaction, int, s,
3876 const struct kernel_old_sigaction*, a,
3877 struct kernel_old_sigaction*, o)
3878 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
3879 LSS_INLINE _syscall3(int, _sigprocmask, int, h,
3880 const unsigned long*, s,
3881 unsigned long*, o)
3882 #ifdef __PPC__
3883 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
3884 #else
3885 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
3886 int, b,
3887 unsigned long, s)
3888 #endif
3889 LSS_INLINE _syscall2(int, stat64, const char *, p,
3890 struct kernel_stat64 *, b)
3891
3892 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3893 const struct kernel_sigaction *act,
3894 struct kernel_sigaction *oldact) {
3895 int old_errno = LSS_ERRNO;
3896 int rc;
3897 struct kernel_sigaction a;
3898 if (act != NULL) {
3899 a = *act;
3900 #ifdef __i386__
3901 /* On i386, the kernel requires us to always set our own
3902 * SA_RESTORER when using realtime signals. Otherwise, it does not
3903 * know how to return from a signal handler. This function must have
3904 * a "magic" signature that the "gdb" (and maybe the kernel?) can
3905 * recognize.
3906 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
3907 * using non-realtime signals.
3908 *
3909 * TODO: Test whether ARM needs a restorer
3910 */
3911 if (!(a.sa_flags & SA_RESTORER)) {
3912 a.sa_flags |= SA_RESTORER;
3913 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
3914 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
3915 }
3916 #endif
3917 }
3918 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
3919 (KERNEL_NSIG+7)/8);
3920 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3921 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
3922 if (!act) {
3923 ptr_a = NULL;
3924 } else {
3925 oa.sa_handler_ = act->sa_handler_;
3926 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
3927 #ifndef __mips__
3928 oa.sa_restorer = act->sa_restorer;
3929 #endif
3930 oa.sa_flags = act->sa_flags;
3931 }
3932 if (!oldact) {
3933 ptr_oa = NULL;
3934 }
3935 LSS_ERRNO = old_errno;
3936 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
3937 if (rc == 0 && oldact) {
3938 if (act) {
3939 memcpy(oldact, act, sizeof(*act));
3940 } else {
3941 memset(oldact, 0, sizeof(*oldact));
3942 }
3943 oldact->sa_handler_ = ptr_oa->sa_handler_;
3944 oldact->sa_flags = ptr_oa->sa_flags;
3945 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
3946 #ifndef __mips__
3947 oldact->sa_restorer = ptr_oa->sa_restorer;
3948 #endif
3949 }
3950 }
3951 return rc;
3952 }
3953
3954 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3955 int old_errno = LSS_ERRNO;
3956 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3957 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3958 LSS_ERRNO = old_errno;
3959 LSS_NAME(sigemptyset)(set);
3960 rc = LSS_NAME(_sigpending)(&set->sig[0]);
3961 }
3962 return rc;
3963 }
3964
3965 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3966 const struct kernel_sigset_t *set,
3967 struct kernel_sigset_t *oldset) {
3968 int olderrno = LSS_ERRNO;
3969 int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3970 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3971 LSS_ERRNO = olderrno;
3972 if (oldset) {
3973 LSS_NAME(sigemptyset)(oldset);
3974 }
3975 rc = LSS_NAME(_sigprocmask)(how,
3976 set ? &set->sig[0] : NULL,
3977 oldset ? &oldset->sig[0] : NULL);
3978 }
3979 return rc;
3980 }
3981
3982 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3983 int olderrno = LSS_ERRNO;
3984 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3985 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3986 LSS_ERRNO = olderrno;
3987 rc = LSS_NAME(_sigsuspend)(
3988 #ifndef __PPC__
3989 set, 0,
3990 #endif
3991 set->sig[0]);
3992 }
3993 return rc;
3994 }
3995 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003996 #if defined(__i386__) || \
3997 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3998 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
3999 defined(__PPC__) || \
4000 (defined(__s390__) && !defined(__s390x__))
4001 /* On these architectures, implement mmap() with mmap2(). */
4002 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4003 int64_t o) {
4004 if (o % 4096) {
4005 LSS_ERRNO = EINVAL;
4006 return (void *) -1;
4007 }
4008 return LSS_NAME(_mmap2)(s, l, p, f, d, (o / 4096));
4009 }
4010 #elif defined(__s390x__)
4011 /* On s390x, mmap() arguments are passed in memory. */
4012 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4013 int64_t o) {
4014 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4015 (unsigned long) p, (unsigned long) f,
4016 (unsigned long) d, (unsigned long) o };
4017 LSS_REG(2, buf);
4018 LSS_BODY(void*, mmap, "0"(__r2));
4019 }
4020 #elif defined(__x86_64__)
4021 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
4022 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4023 int64_t o) {
4024 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
4025 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
4026 LSS_SYSCALL_ARG(d), (uint64_t)(o));
4027 }
4028 #else
4029 /* Remaining 64-bit architectures. */
4030 LSS_INLINE _syscall6(void*, mmap, void*, addr, size_t, length, int, prot,
4031 int, flags, int, fd, int64_t, offset)
4032 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004033 #if defined(__PPC__)
4034 #undef LSS_SC_LOADARGS_0
4035 #define LSS_SC_LOADARGS_0(dummy...)
4036 #undef LSS_SC_LOADARGS_1
4037 #define LSS_SC_LOADARGS_1(arg1) \
4038 __sc_4 = (unsigned long) (arg1)
4039 #undef LSS_SC_LOADARGS_2
4040 #define LSS_SC_LOADARGS_2(arg1, arg2) \
4041 LSS_SC_LOADARGS_1(arg1); \
4042 __sc_5 = (unsigned long) (arg2)
4043 #undef LSS_SC_LOADARGS_3
4044 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
4045 LSS_SC_LOADARGS_2(arg1, arg2); \
4046 __sc_6 = (unsigned long) (arg3)
4047 #undef LSS_SC_LOADARGS_4
4048 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
4049 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
4050 __sc_7 = (unsigned long) (arg4)
4051 #undef LSS_SC_LOADARGS_5
4052 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
4053 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
4054 __sc_8 = (unsigned long) (arg5)
4055 #undef LSS_SC_BODY
4056 #define LSS_SC_BODY(nr, type, opt, args...) \
4057 long __sc_ret, __sc_err; \
4058 { \
4059 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
4060 register unsigned long __sc_3 __asm__ ("r3") = opt; \
4061 register unsigned long __sc_4 __asm__ ("r4"); \
4062 register unsigned long __sc_5 __asm__ ("r5"); \
4063 register unsigned long __sc_6 __asm__ ("r6"); \
4064 register unsigned long __sc_7 __asm__ ("r7"); \
4065 register unsigned long __sc_8 __asm__ ("r8"); \
4066 LSS_SC_LOADARGS_##nr(args); \
4067 __asm__ __volatile__ \
4068 ("stwu 1, -48(1)\n\t" \
4069 "stw 4, 20(1)\n\t" \
4070 "stw 5, 24(1)\n\t" \
4071 "stw 6, 28(1)\n\t" \
4072 "stw 7, 32(1)\n\t" \
4073 "stw 8, 36(1)\n\t" \
4074 "addi 4, 1, 20\n\t" \
4075 "sc\n\t" \
4076 "mfcr %0" \
4077 : "=&r" (__sc_0), \
4078 "=&r" (__sc_3), "=&r" (__sc_4), \
4079 "=&r" (__sc_5), "=&r" (__sc_6), \
4080 "=&r" (__sc_7), "=&r" (__sc_8) \
4081 : LSS_ASMINPUT_##nr \
4082 : "cr0", "ctr", "memory"); \
4083 __sc_ret = __sc_3; \
4084 __sc_err = __sc_0; \
4085 } \
4086 LSS_RETURN(type, __sc_ret, __sc_err)
4087
4088 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4089 int flags){
4090 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
4091 }
4092
4093 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4094 const struct kernel_msghdr *msg,
4095 int flags) {
4096 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
4097 }
4098
4099 // TODO(csilvers): why is this ifdef'ed out?
4100#if 0
4101 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4102 int flags,
4103 const struct kernel_sockaddr *to,
4104 unsigned int tolen) {
4105 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
4106 }
4107#endif
4108
4109 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4110 LSS_SC_BODY(2, int, 13, s, how);
4111 }
4112
4113 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4114 LSS_SC_BODY(3, int, 1, domain, type, protocol);
4115 }
4116
4117 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4118 int sv[2]) {
4119 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
4120 }
4121 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004122 #if defined(__ARM_EABI__) || defined (__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004123 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
4124 int, flags)
4125 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
4126 msg, int, flags)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004127 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
4128 int, flags, const struct kernel_sockaddr*, to,
4129 unsigned int, tolen)
4130 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
4131 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
4132 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
4133 int*, sv)
4134 #endif
4135 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004136 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4137 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004138 #define __NR__socketcall __NR_socketcall
4139 LSS_INLINE _syscall2(int, _socketcall, int, c,
4140 va_list, a)
4141 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
4142 int rc;
4143 va_list ap;
4144 va_start(ap, op);
4145 rc = LSS_NAME(_socketcall)(op, ap);
4146 va_end(ap);
4147 return rc;
4148 }
4149
4150 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4151 int flags){
4152 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
4153 }
4154
4155 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4156 const struct kernel_msghdr *msg,
4157 int flags) {
4158 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
4159 }
4160
4161 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4162 int flags,
4163 const struct kernel_sockaddr *to,
4164 unsigned int tolen) {
4165 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
4166 }
4167
4168 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4169 return LSS_NAME(socketcall)(13, s, how);
4170 }
4171
4172 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4173 return LSS_NAME(socketcall)(1, domain, type, protocol);
4174 }
4175
4176 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4177 int sv[2]) {
4178 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
4179 }
4180 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004181 #if defined(__NR_fstatat64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004182 LSS_INLINE _syscall4(int, fstatat64, int, d,
4183 const char *, p,
4184 struct kernel_stat64 *, b, int, f)
4185 #endif
4186 #if defined(__i386__) || defined(__PPC__) || \
4187 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
4188 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
4189 int*, s, int, o)
4190 #endif
4191 #if defined(__mips__)
4192 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
4193 * both file handles through CPU registers.
4194 */
4195 LSS_INLINE int LSS_NAME(pipe)(int *p) {
4196 register unsigned long __v0 __asm__("$2") = __NR_pipe;
4197 register unsigned long __v1 __asm__("$3");
4198 register unsigned long __r7 __asm__("$7");
4199 __asm__ __volatile__ ("syscall\n"
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00004200 : "=r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004201 : "0"(__v0)
4202 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004203 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004204 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004205 unsigned long __errnovalue = __v0;
4206 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004207 return -1;
4208 } else {
4209 p[0] = __v0;
4210 p[1] = __v1;
4211 return 0;
4212 }
4213 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004214 #elif !defined(__aarch64__)
4215 // The unlink syscall has been deprecated on aarch64. We polyfill it below.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004216 LSS_INLINE _syscall1(int, pipe, int *, p)
4217 #endif
4218 /* TODO(csilvers): see if ppc can/should support this as well */
4219 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004220 defined(__ARM_EABI__) || \
4221 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) || \
4222 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004223 #define __NR__statfs64 __NR_statfs64
4224 #define __NR__fstatfs64 __NR_fstatfs64
4225 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
4226 size_t, s,struct kernel_statfs64*, b)
4227 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
4228 size_t, s,struct kernel_statfs64*, b)
4229 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
4230 struct kernel_statfs64 *b) {
4231 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
4232 }
4233 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
4234 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
4235 }
4236 #endif
4237
4238 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
4239 extern char **environ;
4240 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
4241 }
4242
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004243 LSS_INLINE pid_t LSS_NAME(gettid)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004244 pid_t tid = LSS_NAME(_gettid)();
4245 if (tid != -1) {
4246 return tid;
4247 }
4248 return LSS_NAME(getpid)();
4249 }
4250
4251 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
4252 size_t new_size, int flags, ...) {
4253 va_list ap;
4254 void *new_address, *rc;
4255 va_start(ap, flags);
4256 new_address = va_arg(ap, void *);
4257 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
4258 flags, new_address);
4259 va_end(ap);
4260 return rc;
4261 }
4262
4263 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
4264 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
4265 * then sends job control signals to the real parent, rather than to
4266 * the tracer. We reduce the risk of this happening by starting a
4267 * whole new time slice, and then quickly sending a SIGCONT signal
4268 * right after detaching from the tracee.
4269 *
4270 * We use tkill to ensure that we only issue a wakeup for the thread being
4271 * detached. Large multi threaded apps can take a long time in the kernel
4272 * processing SIGCONT.
4273 */
4274 int rc, err;
4275 LSS_NAME(sched_yield)();
4276 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
4277 err = LSS_ERRNO;
4278 LSS_NAME(tkill)(pid, SIGCONT);
4279 /* Old systems don't have tkill */
4280 if (LSS_ERRNO == ENOSYS)
4281 LSS_NAME(kill)(pid, SIGCONT);
4282 LSS_ERRNO = err;
4283 return rc;
4284 }
4285
4286 LSS_INLINE int LSS_NAME(raise)(int sig) {
4287 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
4288 }
4289
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004290 LSS_INLINE int LSS_NAME(setpgrp)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004291 return LSS_NAME(setpgid)(0, 0);
4292 }
4293
4294 LSS_INLINE int LSS_NAME(sysconf)(int name) {
4295 extern int __getpagesize(void);
4296 switch (name) {
4297 case _SC_OPEN_MAX: {
4298 struct kernel_rlimit limit;
4299#if defined(__ARM_EABI__)
4300 return LSS_NAME(ugetrlimit)(RLIMIT_NOFILE, &limit) < 0
4301 ? 8192 : limit.rlim_cur;
4302#else
4303 return LSS_NAME(getrlimit)(RLIMIT_NOFILE, &limit) < 0
4304 ? 8192 : limit.rlim_cur;
4305#endif
4306 }
4307 case _SC_PAGESIZE:
4308 return __getpagesize();
4309 default:
4310 LSS_ERRNO = ENOSYS;
4311 return -1;
4312 }
4313 }
vapier@chromium.org2273e812013-04-01 17:52:44 +00004314 #if defined(__x86_64__)
4315 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4316 LSS_INLINE ssize_t LSS_NAME(pread64)(int f, void *b, size_t c, loff_t o) {
4317 LSS_BODY(4, ssize_t, pread64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4318 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4319 }
4320
4321 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int f, const void *b, size_t c,
4322 loff_t o) {
4323 LSS_BODY(4, ssize_t, pwrite64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4324 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4325 }
4326
4327 LSS_INLINE int LSS_NAME(readahead)(int f, loff_t o, unsigned c) {
4328 LSS_BODY(3, int, readahead, LSS_SYSCALL_ARG(f), (uint64_t)(o),
4329 LSS_SYSCALL_ARG(c));
4330 }
4331 #elif defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004332 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
4333 void *, b, size_t, c,
4334 loff_t, o)
4335 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
4336 const void *, b, size_t, c,
4337 loff_t, o)
4338 LSS_INLINE _syscall3(int, readahead, int, f,
4339 loff_t, o, unsigned, c)
4340 #else
4341 #define __NR__pread64 __NR_pread64
4342 #define __NR__pwrite64 __NR_pwrite64
4343 #define __NR__readahead __NR_readahead
mseaborn@chromium.org2c73abf2012-09-15 03:46:48 +00004344 #if defined(__ARM_EABI__) || defined(__mips__)
4345 /* On ARM and MIPS, a 64-bit parameter has to be in an even-odd register
4346 * pair. Hence these calls ignore their fourth argument (r3) so that their
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004347 * fifth and sixth make such a pair (r4,r5).
4348 */
4349 #define LSS_LLARG_PAD 0,
4350 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
4351 void *, b, size_t, c,
4352 unsigned, skip, unsigned, o1, unsigned, o2)
4353 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
4354 const void *, b, size_t, c,
4355 unsigned, skip, unsigned, o1, unsigned, o2)
4356 LSS_INLINE _syscall5(int, _readahead, int, f,
4357 unsigned, skip,
4358 unsigned, o1, unsigned, o2, size_t, c)
4359 #else
4360 #define LSS_LLARG_PAD
4361 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
4362 void *, b, size_t, c, unsigned, o1,
4363 unsigned, o2)
4364 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
4365 const void *, b, size_t, c, unsigned, o1,
4366 long, o2)
4367 LSS_INLINE _syscall4(int, _readahead, int, f,
4368 unsigned, o1, unsigned, o2, size_t, c)
4369 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004370 /* We force 64bit-wide parameters onto the stack, then access each
4371 * 32-bit component individually. This guarantees that we build the
4372 * correct parameters independent of the native byte-order of the
4373 * underlying architecture.
4374 */
4375 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
4376 loff_t off) {
4377 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004378 return LSS_NAME(_pread64)(fd, buf, count,
4379 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004380 }
4381 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
4382 size_t count, loff_t off) {
4383 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004384 return LSS_NAME(_pwrite64)(fd, buf, count,
4385 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004386 }
4387 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, int len) {
4388 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004389 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], len);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004390 }
4391 #endif
4392#endif
4393
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004394#if defined(__aarch64__)
4395 LSS_INLINE _syscall3(int, dup3, int, s, int, d, int, f)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004396 LSS_INLINE _syscall4(int, newfstatat, int, dirfd, const char *, pathname,
4397 struct kernel_stat *, buf, int, flags)
4398 LSS_INLINE _syscall2(int, pipe2, int *, pipefd, int, flags)
4399 LSS_INLINE _syscall5(int, ppoll, struct kernel_pollfd *, u,
4400 unsigned int, n, const struct kernel_timespec *, t,
vapier@chromium.orgdb1e07d2015-01-16 14:14:42 +00004401 const struct kernel_sigset_t *, sigmask, size_t, s)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004402 LSS_INLINE _syscall4(int, readlinkat, int, d, const char *, p, char *, b,
4403 size_t, s)
4404#endif
4405
4406/*
4407 * Polyfills for deprecated syscalls.
4408 */
4409
4410#if defined(__aarch64__)
4411 LSS_INLINE int LSS_NAME(dup2)(int s, int d) {
4412 return LSS_NAME(dup3)(s, d, 0);
4413 }
4414
4415 LSS_INLINE int LSS_NAME(open)(const char *pathname, int flags, int mode) {
4416 return LSS_NAME(openat)(AT_FDCWD, pathname, flags, mode);
4417 }
4418
4419 LSS_INLINE int LSS_NAME(unlink)(const char *pathname) {
4420 return LSS_NAME(unlinkat)(AT_FDCWD, pathname, 0);
4421 }
4422
4423 LSS_INLINE int LSS_NAME(readlink)(const char *pathname, char *buffer,
4424 size_t size) {
4425 return LSS_NAME(readlinkat)(AT_FDCWD, pathname, buffer, size);
4426 }
4427
4428 LSS_INLINE pid_t LSS_NAME(pipe)(int *pipefd) {
4429 return LSS_NAME(pipe2)(pipefd, 0);
4430 }
4431
4432 LSS_INLINE int LSS_NAME(poll)(struct kernel_pollfd *fds, unsigned int nfds,
4433 int timeout) {
4434 struct kernel_timespec timeout_ts;
4435 struct kernel_timespec *timeout_ts_p = NULL;
4436
4437 if (timeout >= 0) {
4438 timeout_ts.tv_sec = timeout / 1000;
4439 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
4440 timeout_ts_p = &timeout_ts;
4441 }
4442 return LSS_NAME(ppoll)(fds, nfds, timeout_ts_p, NULL, 0);
4443 }
4444
4445 LSS_INLINE int LSS_NAME(stat)(const char *pathname,
4446 struct kernel_stat *buf) {
4447 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, 0);
4448 }
4449
4450 LSS_INLINE pid_t LSS_NAME(fork)(void) {
4451 // No fork syscall on aarch64 - implement by means of the clone syscall.
4452 // Note that this does not reset glibc's cached view of the PID/TID, so
4453 // some glibc interfaces might go wrong in the forked subprocess.
4454 int flags = SIGCHLD;
4455 void *child_stack = NULL;
4456 void *parent_tidptr = NULL;
4457 void *newtls = NULL;
4458 void *child_tidptr = NULL;
4459
4460 LSS_REG(0, flags);
4461 LSS_REG(1, child_stack);
4462 LSS_REG(2, parent_tidptr);
4463 LSS_REG(3, newtls);
4464 LSS_REG(4, child_tidptr);
4465 LSS_BODY(pid_t, clone, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),
4466 "r"(__r4));
4467 }
4468#endif
4469
mseaborn@chromium.orgca749372012-09-05 18:26:20 +00004470#ifdef __ANDROID__
4471 /* These restore the original values of these macros saved by the
4472 * corresponding #pragma push_macro near the top of this file. */
4473# pragma pop_macro("stat64")
4474# pragma pop_macro("fstat64")
4475# pragma pop_macro("lstat64")
4476#endif
4477
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004478#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
4479}
4480#endif
4481
4482#endif
4483#endif