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