dl-syscall.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
  3. *
  4. * GNU Lesser General Public License version 2.1 or later.
  5. */
  6. #ifndef _DL_SYSCALL_H
  7. #define _DL_SYSCALL_H
  8. /* We can't use the real errno in ldso, since it has not yet
  9. * been dynamicly linked in yet. */
  10. #include "sys/syscall.h"
  11. extern int _dl_errno;
  12. #ifdef UCLIBC_LDSO
  13. #undef __set_errno
  14. #define __set_errno(X) {(_dl_errno) = (X);}
  15. #endif
  16. #include <linux/version.h>
  17. /* Pull in the arch specific syscall implementation */
  18. #include <dl-syscalls.h>
  19. /* For MAP_ANONYMOUS -- differs between platforms */
  20. #define _SYS_MMAN_H 1
  21. #include <bits/mman.h>
  22. #if defined(__ARCH_HAS_DEPRECATED_SYSCALLS__) && (!defined(__UCLIBC_USE_TIME64__) || defined(__sparc__))
  23. /* Pull in whatever this particular arch's kernel thinks the kernel version of
  24. * struct stat should look like. It turns out that each arch has a different
  25. * opinion on the subject, and different kernel revs use different names... */
  26. #define kernel_stat stat
  27. #include <bits/kernel_stat.h>
  28. #include <bits/kernel_types.h>
  29. /* Protection bits. */
  30. #define S_ISUID 04000 /* Set user ID on execution. */
  31. #define S_ISGID 02000 /* Set group ID on execution. */
  32. #else
  33. /* 1. common-generic ABI doesn't need kernel_stat translation
  34. * 3. S_IS?ID already provided by stat.h
  35. */
  36. #include <fcntl.h>
  37. #include <sys/stat.h>
  38. #include <dl-string.h>
  39. #include <bits/uClibc_arch_features.h>
  40. #if defined __UCLIBC_HAVE_STATX__
  41. static __always_inline void
  42. __cp_stat_statx (struct stat *to, struct statx *from)
  43. {
  44. _dl_memset (to, 0, sizeof (struct stat));
  45. to->st_dev = ((from->stx_dev_minor & 0xff) | (from->stx_dev_major << 8)
  46. | ((from->stx_dev_minor & ~0xff) << 12));
  47. to->st_rdev = ((from->stx_rdev_minor & 0xff) | (from->stx_rdev_major << 8)
  48. | ((from->stx_rdev_minor & ~0xff) << 12));
  49. to->st_ino = from->stx_ino;
  50. to->st_mode = from->stx_mode;
  51. to->st_nlink = from->stx_nlink;
  52. to->st_uid = from->stx_uid;
  53. to->st_gid = from->stx_gid;
  54. to->st_atime = from->stx_atime.tv_sec;
  55. to->st_atim.tv_nsec = from->stx_atime.tv_nsec;
  56. to->st_mtime = from->stx_mtime.tv_sec;
  57. to->st_mtim.tv_nsec = from->stx_mtime.tv_nsec;
  58. to->st_ctime = from->stx_ctime.tv_sec;
  59. to->st_ctim.tv_nsec = from->stx_ctime.tv_nsec;
  60. to->st_size = from->stx_size;
  61. to->st_blocks = from->stx_blocks;
  62. to->st_blksize = from->stx_blksize;
  63. }
  64. #endif
  65. #endif
  66. #define AT_NO_AUTOMOUNT 0x800
  67. #define AT_EMPTY_PATH 0x1000
  68. /* Here are the definitions for some syscalls that are used
  69. by the dynamic linker. The idea is that we want to be able
  70. to call these before the errno symbol is dynamicly linked, so
  71. we use our own version here. Note that we cannot assume any
  72. dynamic linking at all, so we cannot return any error codes.
  73. We just punt if there is an error. */
  74. #define __NR__dl_exit __NR_exit
  75. static __always_inline attribute_noreturn __cold void _dl_exit(int status)
  76. {
  77. INLINE_SYSCALL(_dl_exit, 1, status);
  78. #if __GNUC_PREREQ(4, 5)
  79. __builtin_unreachable(); /* shut up warning: 'noreturn' function does return*/
  80. #else
  81. while (1);
  82. #endif
  83. }
  84. #define __NR__dl_close __NR_close
  85. static __always_inline _syscall1(int, _dl_close, int, fd)
  86. #if defined __NR_openat && !defined __NR_open
  87. static __always_inline int _dl_open(const char *fn,
  88. int flags, __kernel_mode_t mode)
  89. {
  90. return INLINE_SYSCALL(openat, 4, AT_FDCWD, fn, flags, mode);
  91. }
  92. #elif defined __NR_open
  93. # define __NR__dl_open __NR_open
  94. static __always_inline _syscall3(int, _dl_open, const char *, fn, int, flags,
  95. __kernel_mode_t, mode)
  96. #endif
  97. #define __NR__dl_write __NR_write
  98. static __always_inline _syscall3(unsigned long, _dl_write, int, fd,
  99. const void *, buf, unsigned long, count)
  100. #define __NR__dl_read __NR_read
  101. static __always_inline _syscall3(unsigned long, _dl_read, int, fd,
  102. const void *, buf, unsigned long, count)
  103. #define __NR__dl_mprotect __NR_mprotect
  104. static __always_inline _syscall3(int, _dl_mprotect, const void *, addr,
  105. unsigned long, len, int, prot)
  106. #if defined __NR_fstatat64 && !defined __NR_stat && (!defined(__UCLIBC_USE_TIME64__) || defined(__sparc__))
  107. # define __NR__dl_fstatat64 __NR_fstatat64
  108. static __always_inline _syscall4(int, _dl_fstatat64, int, fd, const char *,
  109. fn, struct stat *, stat, int, flags)
  110. static __always_inline int _dl_stat(const char *file_name,
  111. struct stat *buf)
  112. {
  113. return _dl_fstatat64(AT_FDCWD, file_name, buf, 0);
  114. }
  115. #elif defined __NR_newfstatat && !defined __NR_stat && (!defined(__UCLIBC_USE_TIME64__) || defined(__sparc__))
  116. # define __NR__dl_newfstatat __NR_newfstatat
  117. static __always_inline _syscall4(int, _dl_newfstatat, int, fd, const char *,
  118. fn, struct stat *, stat, int, flags)
  119. static __always_inline int _dl_stat(const char *file_name,
  120. struct stat *buf)
  121. {
  122. return _dl_newfstatat(AT_FDCWD, file_name, buf, 0);
  123. }
  124. #elif defined __NR_stat && (!defined(__UCLIBC_USE_TIME64__) || defined(__sparc__) || (LINUX_VERSION_CODE <= KERNEL_VERSION(5,1,0)))
  125. # define __NR__dl_stat __NR_stat
  126. static __always_inline _syscall2(int, _dl_stat, const char *, file_name,
  127. struct stat *, buf)
  128. #elif defined __NR_statx && defined __UCLIBC_HAVE_STATX__
  129. # define __NR__dl_statx __NR_statx
  130. # include <fcntl.h>
  131. # include <statx_cp.h>
  132. static __always_inline _syscall5(int, _dl_statx, int, fd, const char *, file_name, int, flags,
  133. unsigned int, mask, struct statx *, buf);
  134. static __always_inline int _dl_stat(const char *file_name,
  135. struct stat *buf)
  136. {
  137. struct statx tmp;
  138. int rc = _dl_statx(AT_FDCWD, file_name, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &tmp);
  139. if (rc == 0)
  140. __cp_stat_statx ((struct stat *)buf, &tmp);
  141. return rc;
  142. }
  143. #endif
  144. #if defined __NR_fstat64 && !defined __NR_fstat && (!defined(__UCLIBC_USE_TIME64__) || defined(__sparc__))
  145. # define __NR__dl_fstat __NR_fstat64
  146. static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
  147. #elif defined __NR_fstat && !defined __UCLIBC_USE_TIME64__ || defined(__sparc__)
  148. # define __NR__dl_fstat __NR_fstat
  149. static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
  150. #elif defined __NR_statx && defined __UCLIBC_HAVE_STATX__
  151. # define __NR__dl_fstatx __NR_statx
  152. static __always_inline _syscall5(int, _dl_fstatx, int, fd, const char *, file_name, int, flags, unsigned int, mask, struct statx *, buf);
  153. static __always_inline int _dl_fstat(int fd,
  154. struct stat *buf)
  155. {
  156. struct statx tmp;
  157. int rc = _dl_fstatx(fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &tmp);
  158. if (rc == 0)
  159. __cp_stat_statx ((struct stat *)buf, &tmp);
  160. return rc;
  161. }
  162. #else
  163. static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
  164. #endif
  165. #define __NR__dl_munmap __NR_munmap
  166. static __always_inline _syscall2(int, _dl_munmap, void *, start, unsigned long, length)
  167. #ifdef __NR_getxuid
  168. # define __NR_getuid __NR_getxuid
  169. #endif
  170. #define __NR__dl_getuid __NR_getuid
  171. static __always_inline _syscall0(uid_t, _dl_getuid)
  172. #ifndef __NR_geteuid
  173. # define __NR_geteuid __NR_getuid
  174. #endif
  175. #define __NR__dl_geteuid __NR_geteuid
  176. static __always_inline _syscall0(uid_t, _dl_geteuid)
  177. #ifdef __NR_getxgid
  178. # define __NR_getgid __NR_getxgid
  179. #endif
  180. #define __NR__dl_getgid __NR_getgid
  181. static __always_inline _syscall0(gid_t, _dl_getgid)
  182. #ifndef __NR_getegid
  183. # define __NR_getegid __NR_getgid
  184. #endif
  185. #define __NR__dl_getegid __NR_getegid
  186. static __always_inline _syscall0(gid_t, _dl_getegid)
  187. #ifdef __NR_getxpid
  188. # define __NR_getpid __NR_getxpid
  189. #endif
  190. #define __NR__dl_getpid __NR_getpid
  191. static __always_inline _syscall0(gid_t, _dl_getpid)
  192. #if defined __NR_readlinkat
  193. # define __NR__dl_readlink __NR_readlinkat
  194. static __always_inline _syscall4(int, _dl_readlink, int, id, const char *, path,
  195. char *, buf, size_t, bufsiz)
  196. #endif
  197. #ifdef __NR_pread64
  198. #define __NR___syscall_pread __NR_pread64
  199. #ifdef __UCLIBC_SYSCALL_ALIGN_64BIT__
  200. static __always_inline _syscall6(ssize_t, __syscall_pread, int, fd, void *, buf, size_t, dummy,
  201. size_t, count, off_t, offset_hi, off_t, offset_lo)
  202. static __always_inline ssize_t
  203. _dl_pread(int fd, void *buf, size_t count, off_t offset)
  204. {
  205. return __syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR((offset >> 32), (offset & 0xffffffff)));
  206. }
  207. #else
  208. static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
  209. size_t, count, off_t, offset_hi, off_t, offset_lo)
  210. static __always_inline ssize_t
  211. _dl_pread(int fd, void *buf, size_t count, off_t offset)
  212. {
  213. return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
  214. }
  215. #endif
  216. #elif defined __NR_pread
  217. #define __NR___syscall_pread __NR_pread
  218. static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
  219. size_t, count, off_t, offset_hi, off_t, offset_lo)
  220. static __always_inline ssize_t
  221. _dl_pread(int fd, void *buf, size_t count, off_t offset)
  222. {
  223. return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
  224. }
  225. #endif
  226. #ifdef __UCLIBC_HAS_SSP__
  227. # include <sys/time.h>
  228. # define __NR__dl_gettimeofday __NR_gettimeofday
  229. static __always_inline _syscall2(int, _dl_gettimeofday, struct timeval *, tv,
  230. # ifdef __USE_BSD
  231. struct timezone *
  232. # else
  233. void *
  234. # endif
  235. , tz)
  236. #endif
  237. /* Some architectures always use 12 as page shift for mmap2() eventhough the
  238. * real PAGE_SHIFT != 12. Other architectures use the same value as
  239. * PAGE_SHIFT...
  240. */
  241. #ifndef MMAP2_PAGE_SHIFT
  242. # define MMAP2_PAGE_SHIFT 12
  243. #endif
  244. #define MAP_FAILED ((void *) -1)
  245. #define _dl_mmap_check_error(X) (((void *)X) == MAP_FAILED)
  246. static __always_inline
  247. void *_dl_mmap(void *addr, unsigned long size, int prot,
  248. int flags, int fd, unsigned long offset)
  249. {
  250. #if defined(__UCLIBC_MMAP_HAS_6_ARGS__) && defined(__NR_mmap)
  251. /* first try mmap(), syscall6() style */
  252. return (void *)INLINE_SYSCALL(mmap, 6, addr, size, prot, flags, fd, offset);
  253. #elif defined(__NR_mmap2) && !defined (__mcoldfire__)
  254. /* then try mmap2() */
  255. unsigned long shifted;
  256. if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1))
  257. return MAP_FAILED;
  258. /* gcc needs help with putting things onto the stack */
  259. shifted = offset >> MMAP2_PAGE_SHIFT;
  260. return (void *)INLINE_SYSCALL(mmap2, 6, addr, size, prot, flags, fd, shifted);
  261. #elif defined(__NR_mmap)
  262. /* finally, fall back to mmap(), syscall1() style */
  263. unsigned long buffer[6];
  264. buffer[0] = (unsigned long) addr;
  265. buffer[1] = (unsigned long) size;
  266. buffer[2] = (unsigned long) prot;
  267. buffer[3] = (unsigned long) flags;
  268. buffer[4] = (unsigned long) fd;
  269. buffer[5] = (unsigned long) offset;
  270. return (void *)INLINE_SYSCALL(mmap, 1, buffer);
  271. #else
  272. # error "Your architecture doesn't seem to provide mmap() !?"
  273. #endif
  274. }
  275. #endif /* _DL_SYSCALL_H */