linux_syscall_support.h 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. /* Copyright (c) 2005-2008, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * ---
  32. * Author: Markus Gutschke
  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. * This file defines a few internal symbols that all start with "LSS_".
  70. * Do not access these symbols from outside this file. They are not part
  71. * of the supported API.
  72. *
  73. * NOTE: This is a stripped down version of the official opensource
  74. * version of linux_syscall_support.h, which lives at
  75. * http://code.google.com/p/linux-syscall-support/
  76. * It includes only the syscalls that are used in perftools, plus a
  77. * few extra. Here's the breakdown:
  78. * 1) Perftools uses these: grep -rho 'sys_[a-z0-9_A-Z]* *(' src | sort -u
  79. * sys__exit(
  80. * sys_clone(
  81. * sys_close(
  82. * sys_fcntl(
  83. * sys_fstat(
  84. * sys_futex(
  85. * sys_getcpu(
  86. * sys_getdents64(
  87. * sys_getppid(
  88. * sys_gettid(
  89. * sys_lseek(
  90. * sys_mmap(
  91. * sys_mremap(
  92. * sys_munmap(
  93. * sys_open(
  94. * sys_pipe(
  95. * sys_prctl(
  96. * sys_ptrace(
  97. * sys_ptrace_detach(
  98. * sys_read(
  99. * sys_sched_yield(
  100. * sys_sigaction(
  101. * sys_sigaltstack(
  102. * sys_sigdelset(
  103. * sys_sigfillset(
  104. * sys_sigprocmask(
  105. * sys_socket(
  106. * sys_stat(
  107. * sys_waitpid(
  108. * 2) These are used as subroutines of the above:
  109. * sys_getpid -- gettid
  110. * sys_kill -- ptrace_detach
  111. * sys_restore -- sigaction
  112. * sys_restore_rt -- sigaction
  113. * sys_socketcall -- socket
  114. * sys_wait4 -- waitpid
  115. * 3) I left these in even though they're not used. They either
  116. * complement the above (write vs read) or are variants (rt_sigaction):
  117. * sys_fstat64
  118. * sys_llseek
  119. * sys_mmap2
  120. * sys_openat
  121. * sys_getdents
  122. * sys_rt_sigaction
  123. * sys_rt_sigprocmask
  124. * sys_sigaddset
  125. * sys_sigemptyset
  126. * sys_stat64
  127. * sys_write
  128. */
  129. #ifndef SYS_LINUX_SYSCALL_SUPPORT_H
  130. #define SYS_LINUX_SYSCALL_SUPPORT_H
  131. /* We currently only support x86-32, x86-64, ARM, MIPS, PPC/PPC64, Aarch64 and s390x on Linux.
  132. * Porting to other related platforms should not be difficult.
  133. */
  134. #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__) || \
  135. defined(__mips__) || defined(__PPC__) || \
  136. defined(__aarch64__) || defined(__s390x__)) \
  137. && (defined(__linux))
  138. #ifndef SYS_CPLUSPLUS
  139. #ifdef __cplusplus
  140. /* Some system header files in older versions of gcc neglect to properly
  141. * handle being included from C++. As it appears to be harmless to have
  142. * multiple nested 'extern "C"' blocks, just add another one here.
  143. */
  144. extern "C" {
  145. #endif
  146. #include <errno.h>
  147. #include <signal.h>
  148. #include <stdarg.h>
  149. #include <stddef.h>
  150. #include <stdint.h>
  151. #include <string.h>
  152. #include <sys/ptrace.h>
  153. #include <sys/resource.h>
  154. #include <sys/time.h>
  155. #include <sys/types.h>
  156. #include <syscall.h>
  157. #include <unistd.h>
  158. #include <linux/unistd.h>
  159. #include <endian.h>
  160. #ifdef __mips__
  161. /* Include definitions of the ABI currently in use. */
  162. #include <sgidefs.h>
  163. #endif
  164. #endif
  165. /* As glibc often provides subtly incompatible data structures (and implicit
  166. * wrapper functions that convert them), we provide our own kernel data
  167. * structures for use by the system calls.
  168. * These structures have been developed by using Linux 2.6.23 headers for
  169. * reference. Note though, we do not care about exact API compatibility
  170. * with the kernel, and in fact the kernel often does not have a single
  171. * API that works across architectures. Instead, we try to mimic the glibc
  172. * API where reasonable, and only guarantee ABI compatibility with the
  173. * kernel headers.
  174. * Most notably, here are a few changes that were made to the structures
  175. * defined by kernel headers:
  176. *
  177. * - we only define structures, but not symbolic names for kernel data
  178. * types. For the latter, we directly use the native C datatype
  179. * (i.e. "unsigned" instead of "mode_t").
  180. * - in a few cases, it is possible to define identical structures for
  181. * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
  182. * standardizing on the 64bit version of the data types. In particular,
  183. * this means that we use "unsigned" where the 32bit headers say
  184. * "unsigned long".
  185. * - overall, we try to minimize the number of cases where we need to
  186. * conditionally define different structures.
  187. * - the "struct kernel_sigaction" class of structures have been
  188. * modified to more closely mimic glibc's API by introducing an
  189. * anonymous union for the function pointer.
  190. * - a small number of field names had to have an underscore appended to
  191. * them, because glibc defines a global macro by the same name.
  192. */
  193. /* include/linux/dirent.h */
  194. struct kernel_dirent64 {
  195. unsigned long long d_ino;
  196. long long d_off;
  197. unsigned short d_reclen;
  198. unsigned char d_type;
  199. char d_name[256];
  200. };
  201. /* include/linux/dirent.h */
  202. struct kernel_dirent {
  203. long d_ino;
  204. long d_off;
  205. unsigned short d_reclen;
  206. char d_name[256];
  207. };
  208. /* include/linux/time.h */
  209. struct kernel_timespec {
  210. long tv_sec;
  211. long tv_nsec;
  212. };
  213. /* include/linux/time.h */
  214. struct kernel_timeval {
  215. long tv_sec;
  216. long tv_usec;
  217. };
  218. /* include/linux/resource.h */
  219. struct kernel_rusage {
  220. struct kernel_timeval ru_utime;
  221. struct kernel_timeval ru_stime;
  222. long ru_maxrss;
  223. long ru_ixrss;
  224. long ru_idrss;
  225. long ru_isrss;
  226. long ru_minflt;
  227. long ru_majflt;
  228. long ru_nswap;
  229. long ru_inblock;
  230. long ru_oublock;
  231. long ru_msgsnd;
  232. long ru_msgrcv;
  233. long ru_nsignals;
  234. long ru_nvcsw;
  235. long ru_nivcsw;
  236. };
  237. #if defined(__i386__) || defined(__arm__) || defined(__PPC__)
  238. /* include/asm-{arm,i386,mips,ppc}/signal.h */
  239. struct kernel_old_sigaction {
  240. union {
  241. void (*sa_handler_)(int);
  242. void (*sa_sigaction_)(int, siginfo_t *, void *);
  243. };
  244. unsigned long sa_mask;
  245. unsigned long sa_flags;
  246. void (*sa_restorer)(void);
  247. } __attribute__((packed,aligned(4)));
  248. #elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
  249. #define kernel_old_sigaction kernel_sigaction
  250. #elif defined(__aarch64__) || defined(__s390x__)
  251. // No kernel_old_sigaction defined for arm64 or s390x.
  252. #endif
  253. /* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
  254. * exactly match the size of the signal set, even though the API was
  255. * intended to be extensible. We define our own KERNEL_NSIG to deal with
  256. * this.
  257. * Please note that glibc provides signals [1.._NSIG-1], whereas the
  258. * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
  259. * actual number of signals is obviously the same, but the constants
  260. * differ by one.
  261. */
  262. #ifdef __mips__
  263. #define KERNEL_NSIG 128
  264. #else
  265. #define KERNEL_NSIG 64
  266. #endif
  267. /* include/asm-{arm,i386,mips,x86_64}/signal.h */
  268. struct kernel_sigset_t {
  269. unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/
  270. (8*sizeof(unsigned long))];
  271. };
  272. /* include/asm-{arm,generic,i386,mips,x86_64,ppc}/signal.h */
  273. struct kernel_sigaction {
  274. #ifdef __mips__
  275. unsigned long sa_flags;
  276. union {
  277. void (*sa_handler_)(int);
  278. void (*sa_sigaction_)(int, siginfo_t *, void *);
  279. };
  280. struct kernel_sigset_t sa_mask;
  281. #else
  282. union {
  283. void (*sa_handler_)(int);
  284. void (*sa_sigaction_)(int, siginfo_t *, void *);
  285. };
  286. unsigned long sa_flags;
  287. void (*sa_restorer)(void);
  288. struct kernel_sigset_t sa_mask;
  289. #endif
  290. };
  291. /* include/asm-{arm,i386,mips,ppc}/stat.h */
  292. #ifdef __mips__
  293. #if _MIPS_SIM == _MIPS_SIM_ABI64
  294. struct kernel_stat {
  295. #else
  296. struct kernel_stat64 {
  297. #endif
  298. unsigned st_dev;
  299. unsigned __pad0[3];
  300. unsigned long long st_ino;
  301. unsigned st_mode;
  302. unsigned st_nlink;
  303. unsigned st_uid;
  304. unsigned st_gid;
  305. unsigned st_rdev;
  306. unsigned __pad1[3];
  307. long long st_size;
  308. unsigned st_atime_;
  309. unsigned st_atime_nsec_;
  310. unsigned st_mtime_;
  311. unsigned st_mtime_nsec_;
  312. unsigned st_ctime_;
  313. unsigned st_ctime_nsec_;
  314. unsigned st_blksize;
  315. unsigned __pad2;
  316. unsigned long long st_blocks;
  317. };
  318. #elif defined __PPC__
  319. struct kernel_stat64 {
  320. unsigned long long st_dev;
  321. unsigned long long st_ino;
  322. unsigned st_nlink;
  323. unsigned st_mode;
  324. unsigned st_uid;
  325. unsigned st_gid;
  326. int __pad2;
  327. unsigned long long st_rdev;
  328. long long st_size;
  329. long long st_blksize;
  330. long long st_blocks;
  331. kernel_timespec st_atim;
  332. kernel_timespec st_mtim;
  333. kernel_timespec st_ctim;
  334. unsigned long __unused4;
  335. unsigned long __unused5;
  336. unsigned long __unused6;
  337. };
  338. #else
  339. struct kernel_stat64 {
  340. unsigned long long st_dev;
  341. unsigned char __pad0[4];
  342. unsigned __st_ino;
  343. unsigned st_mode;
  344. unsigned st_nlink;
  345. unsigned st_uid;
  346. unsigned st_gid;
  347. unsigned long long st_rdev;
  348. unsigned char __pad3[4];
  349. long long st_size;
  350. unsigned st_blksize;
  351. unsigned long long st_blocks;
  352. unsigned st_atime_;
  353. unsigned st_atime_nsec_;
  354. unsigned st_mtime_;
  355. unsigned st_mtime_nsec_;
  356. unsigned st_ctime_;
  357. unsigned st_ctime_nsec_;
  358. unsigned long long st_ino;
  359. };
  360. #endif
  361. /* include/asm-{arm,generic,i386,mips,x86_64,ppc,s390}/stat.h */
  362. #if defined(__i386__) || defined(__arm__)
  363. struct kernel_stat {
  364. /* The kernel headers suggest that st_dev and st_rdev should be 32bit
  365. * quantities encoding 12bit major and 20bit minor numbers in an interleaved
  366. * format. In reality, we do not see useful data in the top bits. So,
  367. * we'll leave the padding in here, until we find a better solution.
  368. */
  369. unsigned short st_dev;
  370. short pad1;
  371. unsigned st_ino;
  372. unsigned short st_mode;
  373. unsigned short st_nlink;
  374. unsigned short st_uid;
  375. unsigned short st_gid;
  376. unsigned short st_rdev;
  377. short pad2;
  378. unsigned st_size;
  379. unsigned st_blksize;
  380. unsigned st_blocks;
  381. unsigned st_atime_;
  382. unsigned st_atime_nsec_;
  383. unsigned st_mtime_;
  384. unsigned st_mtime_nsec_;
  385. unsigned st_ctime_;
  386. unsigned st_ctime_nsec_;
  387. unsigned __unused4;
  388. unsigned __unused5;
  389. };
  390. #elif defined(__x86_64__)
  391. struct kernel_stat {
  392. uint64_t st_dev;
  393. uint64_t st_ino;
  394. uint64_t st_nlink;
  395. unsigned st_mode;
  396. unsigned st_uid;
  397. unsigned st_gid;
  398. unsigned __pad0;
  399. uint64_t st_rdev;
  400. int64_t st_size;
  401. int64_t st_blksize;
  402. int64_t st_blocks;
  403. uint64_t st_atime_;
  404. uint64_t st_atime_nsec_;
  405. uint64_t st_mtime_;
  406. uint64_t st_mtime_nsec_;
  407. uint64_t st_ctime_;
  408. uint64_t st_ctime_nsec_;
  409. int64_t __unused[3];
  410. };
  411. #elif defined(__PPC__)
  412. struct kernel_stat {
  413. unsigned long long st_dev;
  414. unsigned long st_ino;
  415. unsigned long st_nlink;
  416. unsigned long st_mode;
  417. unsigned st_uid;
  418. unsigned st_gid;
  419. int __pad2;
  420. unsigned long long st_rdev;
  421. long st_size;
  422. unsigned long st_blksize;
  423. unsigned long st_blocks;
  424. kernel_timespec st_atim;
  425. kernel_timespec st_mtim;
  426. kernel_timespec st_ctim;
  427. unsigned long __unused4;
  428. unsigned long __unused5;
  429. unsigned long __unused6;
  430. };
  431. #elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
  432. struct kernel_stat {
  433. unsigned st_dev;
  434. int st_pad1[3];
  435. unsigned st_ino;
  436. unsigned st_mode;
  437. unsigned st_nlink;
  438. unsigned st_uid;
  439. unsigned st_gid;
  440. unsigned st_rdev;
  441. int st_pad2[2];
  442. long st_size;
  443. int st_pad3;
  444. long st_atime_;
  445. long st_atime_nsec_;
  446. long st_mtime_;
  447. long st_mtime_nsec_;
  448. long st_ctime_;
  449. long st_ctime_nsec_;
  450. int st_blksize;
  451. int st_blocks;
  452. int st_pad4[14];
  453. };
  454. #elif defined(__aarch64__)
  455. struct kernel_stat {
  456. unsigned long st_dev;
  457. unsigned long st_ino;
  458. unsigned int st_mode;
  459. unsigned int st_nlink;
  460. unsigned int st_uid;
  461. unsigned int st_gid;
  462. unsigned long st_rdev;
  463. unsigned long __pad1;
  464. long st_size;
  465. int st_blksize;
  466. int __pad2;
  467. long st_blocks;
  468. long st_atime_;
  469. unsigned long st_atime_nsec_;
  470. long st_mtime_;
  471. unsigned long st_mtime_nsec_;
  472. long st_ctime_;
  473. unsigned long st_ctime_nsec_;
  474. unsigned int __unused4;
  475. unsigned int __unused5;
  476. };
  477. #elif defined(__s390x__)
  478. struct kernel_stat {
  479. unsigned long st_dev;
  480. unsigned long st_ino;
  481. unsigned long st_nlink;
  482. unsigned int st_mode;
  483. unsigned int st_uid;
  484. unsigned int st_gid;
  485. unsigned int __pad1;
  486. unsigned long st_rdev;
  487. unsigned long st_size;
  488. unsigned long st_atime_;
  489. unsigned long st_atime_nsec_;
  490. unsigned long st_mtime_;
  491. unsigned long st_mtime_nsec_;
  492. unsigned long st_ctime_;
  493. unsigned long st_ctime_nsec_;
  494. unsigned long st_blksize;
  495. long st_blocks;
  496. unsigned long __unused[3];
  497. };
  498. #endif
  499. /* Definitions missing from the standard header files */
  500. #ifndef O_DIRECTORY
  501. #if defined(__arm__)
  502. #define O_DIRECTORY 0040000
  503. #else
  504. #define O_DIRECTORY 0200000
  505. #endif
  506. #endif
  507. #ifndef PR_GET_DUMPABLE
  508. #define PR_GET_DUMPABLE 3
  509. #endif
  510. #ifndef PR_SET_DUMPABLE
  511. #define PR_SET_DUMPABLE 4
  512. #endif
  513. #ifndef AT_FDCWD
  514. #define AT_FDCWD (-100)
  515. #endif
  516. #ifndef AT_SYMLINK_NOFOLLOW
  517. #define AT_SYMLINK_NOFOLLOW 0x100
  518. #endif
  519. #ifndef AT_REMOVEDIR
  520. #define AT_REMOVEDIR 0x200
  521. #endif
  522. #ifndef MREMAP_FIXED
  523. #define MREMAP_FIXED 2
  524. #endif
  525. #ifndef SA_RESTORER
  526. #define SA_RESTORER 0x04000000
  527. #endif
  528. #if defined(__i386__)
  529. #ifndef __NR_rt_sigaction
  530. #define __NR_rt_sigaction 174
  531. #define __NR_rt_sigprocmask 175
  532. #endif
  533. #ifndef __NR_stat64
  534. #define __NR_stat64 195
  535. #endif
  536. #ifndef __NR_fstat64
  537. #define __NR_fstat64 197
  538. #endif
  539. #ifndef __NR_getdents64
  540. #define __NR_getdents64 220
  541. #endif
  542. #ifndef __NR_gettid
  543. #define __NR_gettid 224
  544. #endif
  545. #ifndef __NR_futex
  546. #define __NR_futex 240
  547. #endif
  548. #ifndef __NR_openat
  549. #define __NR_openat 295
  550. #endif
  551. #ifndef __NR_getcpu
  552. #define __NR_getcpu 318
  553. #endif
  554. /* End of i386 definitions */
  555. #elif defined(__arm__)
  556. #ifndef __syscall
  557. #if defined(__thumb__) || defined(__ARM_EABI__)
  558. #define __SYS_REG(name) register long __sysreg __asm__("r6") = __NR_##name;
  559. #define __SYS_REG_LIST(regs...) [sysreg] "r" (__sysreg) , ##regs
  560. #define __syscall(name) "swi\t0"
  561. #define __syscall_safe(name) \
  562. "push {r7}\n" \
  563. "mov r7,%[sysreg]\n" \
  564. __syscall(name)"\n" \
  565. "pop {r7}"
  566. #else
  567. #define __SYS_REG(name)
  568. #define __SYS_REG_LIST(regs...) regs
  569. #define __syscall(name) "swi\t" __sys1(__NR_##name) ""
  570. #define __syscall_safe(name) __syscall(name)
  571. #endif
  572. #endif
  573. #ifndef __NR_rt_sigaction
  574. #define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
  575. #define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
  576. #endif
  577. #ifndef __NR_stat64
  578. #define __NR_stat64 (__NR_SYSCALL_BASE + 195)
  579. #endif
  580. #ifndef __NR_fstat64
  581. #define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
  582. #endif
  583. #ifndef __NR_getdents64
  584. #define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
  585. #endif
  586. #ifndef __NR_gettid
  587. #define __NR_gettid (__NR_SYSCALL_BASE + 224)
  588. #endif
  589. #ifndef __NR_futex
  590. #define __NR_futex (__NR_SYSCALL_BASE + 240)
  591. #endif
  592. /* End of ARM definitions */
  593. #elif defined(__x86_64__)
  594. #ifndef __NR_gettid
  595. #define __NR_gettid 186
  596. #endif
  597. #ifndef __NR_futex
  598. #define __NR_futex 202
  599. #endif
  600. #ifndef __NR_getdents64
  601. #define __NR_getdents64 217
  602. #endif
  603. #ifndef __NR_openat
  604. #define __NR_openat 257
  605. #endif
  606. /* End of x86-64 definitions */
  607. #elif defined(__mips__)
  608. #if _MIPS_SIM == _MIPS_SIM_ABI32
  609. #ifndef __NR_rt_sigaction
  610. #define __NR_rt_sigaction (__NR_Linux + 194)
  611. #define __NR_rt_sigprocmask (__NR_Linux + 195)
  612. #endif
  613. #ifndef __NR_stat64
  614. #define __NR_stat64 (__NR_Linux + 213)
  615. #endif
  616. #ifndef __NR_fstat64
  617. #define __NR_fstat64 (__NR_Linux + 215)
  618. #endif
  619. #ifndef __NR_getdents64
  620. #define __NR_getdents64 (__NR_Linux + 219)
  621. #endif
  622. #ifndef __NR_gettid
  623. #define __NR_gettid (__NR_Linux + 222)
  624. #endif
  625. #ifndef __NR_futex
  626. #define __NR_futex (__NR_Linux + 238)
  627. #endif
  628. #ifndef __NR_openat
  629. #define __NR_openat (__NR_Linux + 288)
  630. #endif
  631. #ifndef __NR_fstatat
  632. #define __NR_fstatat (__NR_Linux + 293)
  633. #endif
  634. #ifndef __NR_getcpu
  635. #define __NR_getcpu (__NR_Linux + 312)
  636. #endif
  637. /* End of MIPS (old 32bit API) definitions */
  638. #elif _MIPS_SIM == _MIPS_SIM_ABI64
  639. #ifndef __NR_gettid
  640. #define __NR_gettid (__NR_Linux + 178)
  641. #endif
  642. #ifndef __NR_futex
  643. #define __NR_futex (__NR_Linux + 194)
  644. #endif
  645. #ifndef __NR_openat
  646. #define __NR_openat (__NR_Linux + 247)
  647. #endif
  648. #ifndef __NR_fstatat
  649. #define __NR_fstatat (__NR_Linux + 252)
  650. #endif
  651. #ifndef __NR_getcpu
  652. #define __NR_getcpu (__NR_Linux + 271)
  653. #endif
  654. /* End of MIPS (64bit API) definitions */
  655. #else
  656. #ifndef __NR_gettid
  657. #define __NR_gettid (__NR_Linux + 178)
  658. #endif
  659. #ifndef __NR_futex
  660. #define __NR_futex (__NR_Linux + 194)
  661. #endif
  662. #ifndef __NR_openat
  663. #define __NR_openat (__NR_Linux + 251)
  664. #endif
  665. #ifndef __NR_fstatat
  666. #define __NR_fstatat (__NR_Linux + 256)
  667. #endif
  668. #ifndef __NR_getcpu
  669. #define __NR_getcpu (__NR_Linux + 275)
  670. #endif
  671. /* End of MIPS (new 32bit API) definitions */
  672. #endif
  673. /* End of MIPS definitions */
  674. #elif defined(__PPC__)
  675. #ifndef __NR_rt_sigaction
  676. #define __NR_rt_sigaction 173
  677. #define __NR_rt_sigprocmask 174
  678. #endif
  679. #ifndef __NR_stat64
  680. #define __NR_stat64 195
  681. #endif
  682. #ifndef __NR_fstat64
  683. #define __NR_fstat64 197
  684. #endif
  685. #ifndef __NR_socket
  686. #define __NR_socket 198
  687. #endif
  688. #ifndef __NR_getdents64
  689. #define __NR_getdents64 202
  690. #endif
  691. #ifndef __NR_gettid
  692. #define __NR_gettid 207
  693. #endif
  694. #ifndef __NR_futex
  695. #define __NR_futex 221
  696. #endif
  697. #ifndef __NR_openat
  698. #define __NR_openat 286
  699. #endif
  700. #ifndef __NR_getcpu
  701. #define __NR_getcpu 302
  702. #endif
  703. /* End of powerpc defininitions */
  704. #elif defined(__aarch64__)
  705. #ifndef __NR_fstatat
  706. #define __NR_fstatat 79
  707. #endif
  708. /* End of aarch64 defininitions */
  709. #elif defined(__s390x__)
  710. #ifndef __NR_quotactl
  711. #define __NR_quotactl 131
  712. #endif
  713. #ifndef __NR_rt_sigreturn
  714. #define __NR_rt_sigreturn 173
  715. #endif
  716. #ifndef __NR_rt_sigaction
  717. #define __NR_rt_sigaction 174
  718. #endif
  719. #ifndef __NR_rt_sigprocmask
  720. #define __NR_rt_sigprocmask 175
  721. #endif
  722. #ifndef __NR_rt_sigpending
  723. #define __NR_rt_sigpending 176
  724. #endif
  725. #ifndef __NR_rt_sigsuspend
  726. #define __NR_rt_sigsuspend 179
  727. #endif
  728. #ifndef __NR_pread64
  729. #define __NR_pread64 180
  730. #endif
  731. #ifndef __NR_pwrite64
  732. #define __NR_pwrite64 181
  733. #endif
  734. #ifndef __NR_getrlimit
  735. #define __NR_getrlimit 191
  736. #endif
  737. #ifndef __NR_setresuid
  738. #define __NR_setresuid 208
  739. #endif
  740. #ifndef __NR_getresuid
  741. #define __NR_getresuid 209
  742. #endif
  743. #ifndef __NR_setresgid
  744. #define __NR_setresgid 210
  745. #endif
  746. #ifndef __NR_getresgid
  747. #define __NR_getresgid 211
  748. #endif
  749. #ifndef __NR_setfsuid
  750. #define __NR_setfsuid 215
  751. #endif
  752. #ifndef __NR_setfsgid
  753. #define __NR_setfsgid 216
  754. #endif
  755. #ifndef __NR_getdents64
  756. #define __NR_getdents64 220
  757. #endif
  758. #ifndef __NR_readahead
  759. #define __NR_readahead 222
  760. #endif
  761. #ifndef __NR_setxattr
  762. #define __NR_setxattr 224
  763. #endif
  764. #ifndef __NR_lsetxattr
  765. #define __NR_lsetxattr 225
  766. #endif
  767. #ifndef __NR_getxattr
  768. #define __NR_getxattr 227
  769. #endif
  770. #ifndef __NR_lgetxattr
  771. #define __NR_lgetxattr 228
  772. #endif
  773. #ifndef __NR_listxattr
  774. #define __NR_listxattr 230
  775. #endif
  776. #ifndef __NR_llistxattr
  777. #define __NR_llistxattr 231
  778. #endif
  779. #ifndef __NR_gettid
  780. #define __NR_gettid 236
  781. #endif
  782. #ifndef __NR_tkill
  783. #define __NR_tkill 237
  784. #endif
  785. #ifndef __NR_futex
  786. #define __NR_futex 238
  787. #endif
  788. #ifndef __NR_sched_setaffinity
  789. #define __NR_sched_setaffinity 239
  790. #endif
  791. #ifndef __NR_sched_getaffinity
  792. #define __NR_sched_getaffinity 240
  793. #endif
  794. #ifndef __NR_set_tid_address
  795. #define __NR_set_tid_address 252
  796. #endif
  797. #ifndef __NR_fadvise64
  798. #define __NR_fadvise64 253
  799. #endif
  800. #ifndef __NR_clock_gettime
  801. #define __NR_clock_gettime 260
  802. #endif
  803. #ifndef __NR_clock_getres
  804. #define __NR_clock_getres 261
  805. #endif
  806. #ifndef __NR_statfs64
  807. #define __NR_statfs64 265
  808. #endif
  809. #ifndef __NR_fstatfs64
  810. #define __NR_fstatfs64 266
  811. #endif
  812. #ifndef __NR_ioprio_set
  813. #define __NR_ioprio_set 282
  814. #endif
  815. #ifndef __NR_ioprio_get
  816. #define __NR_ioprio_get 283
  817. #endif
  818. #ifndef __NR_openat
  819. #define __NR_openat 288
  820. #endif
  821. #ifndef __NR_newfstatat
  822. #define __NR_newfstatat 293
  823. #endif
  824. #ifndef __NR_unlinkat
  825. #define __NR_unlinkat 294
  826. #endif
  827. #ifndef __NR_move_pages
  828. #define __NR_move_pages 310
  829. #endif
  830. #ifndef __NR_getcpu
  831. #define __NR_getcpu 311
  832. #endif
  833. #ifndef __NR_fallocate
  834. #define __NR_fallocate 314
  835. #endif
  836. /* End of s390x definitions */
  837. #endif
  838. /* After forking, we must make sure to only call system calls. */
  839. #if __BOUNDED_POINTERS__
  840. #error "Need to port invocations of syscalls for bounded ptrs"
  841. #else
  842. /* The core dumper and the thread lister get executed after threads
  843. * have been suspended. As a consequence, we cannot call any functions
  844. * that acquire locks. Unfortunately, libc wraps most system calls
  845. * (e.g. in order to implement pthread_atfork, and to make calls
  846. * cancellable), which means we cannot call these functions. Instead,
  847. * we have to call syscall() directly.
  848. */
  849. #undef LSS_ERRNO
  850. #ifdef SYS_ERRNO
  851. /* Allow the including file to override the location of errno. This can
  852. * be useful when using clone() with the CLONE_VM option.
  853. */
  854. #define LSS_ERRNO SYS_ERRNO
  855. #else
  856. #define LSS_ERRNO errno
  857. #endif
  858. #undef LSS_INLINE
  859. #ifdef SYS_INLINE
  860. #define LSS_INLINE SYS_INLINE
  861. #else
  862. #define LSS_INLINE static inline
  863. #endif
  864. /* Allow the including file to override the prefix used for all new
  865. * system calls. By default, it will be set to "sys_".
  866. */
  867. #undef LSS_NAME
  868. #ifndef SYS_PREFIX
  869. #define LSS_NAME(name) sys_##name
  870. #elif SYS_PREFIX < 0
  871. #define LSS_NAME(name) name
  872. #elif SYS_PREFIX == 0
  873. #define LSS_NAME(name) sys0_##name
  874. #elif SYS_PREFIX == 1
  875. #define LSS_NAME(name) sys1_##name
  876. #elif SYS_PREFIX == 2
  877. #define LSS_NAME(name) sys2_##name
  878. #elif SYS_PREFIX == 3
  879. #define LSS_NAME(name) sys3_##name
  880. #elif SYS_PREFIX == 4
  881. #define LSS_NAME(name) sys4_##name
  882. #elif SYS_PREFIX == 5
  883. #define LSS_NAME(name) sys5_##name
  884. #elif SYS_PREFIX == 6
  885. #define LSS_NAME(name) sys6_##name
  886. #elif SYS_PREFIX == 7
  887. #define LSS_NAME(name) sys7_##name
  888. #elif SYS_PREFIX == 8
  889. #define LSS_NAME(name) sys8_##name
  890. #elif SYS_PREFIX == 9
  891. #define LSS_NAME(name) sys9_##name
  892. #endif
  893. #undef LSS_RETURN
  894. #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__) || \
  895. defined(__aarch64__) || defined(__s390x__))
  896. /* Failing system calls return a negative result in the range of
  897. * -1..-4095. These are "errno" values with the sign inverted.
  898. */
  899. #define LSS_RETURN(type, res) \
  900. do { \
  901. if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
  902. LSS_ERRNO = -(res); \
  903. res = -1; \
  904. } \
  905. return (type) (res); \
  906. } while (0)
  907. #elif defined(__mips__)
  908. /* On MIPS, failing system calls return -1, and set errno in a
  909. * separate CPU register.
  910. */
  911. #define LSS_RETURN(type, res, err) \
  912. do { \
  913. if (err) { \
  914. LSS_ERRNO = (res); \
  915. res = -1; \
  916. } \
  917. return (type) (res); \
  918. } while (0)
  919. #elif defined(__PPC__)
  920. /* On PPC, failing system calls return -1, and set errno in a
  921. * separate CPU register. See linux/unistd.h.
  922. */
  923. #define LSS_RETURN(type, res, err) \
  924. do { \
  925. if (err & 0x10000000 ) { \
  926. LSS_ERRNO = (res); \
  927. res = -1; \
  928. } \
  929. return (type) (res); \
  930. } while (0)
  931. #endif
  932. #if defined(__i386__)
  933. #if defined(NO_FRAME_POINTER) && (100 * __GNUC__ + __GNUC_MINOR__ >= 404)
  934. /* This only works for GCC-4.4 and above -- the first version to use
  935. .cfi directives for dwarf unwind info. */
  936. #define CFI_ADJUST_CFA_OFFSET(adjust) \
  937. ".cfi_adjust_cfa_offset " #adjust "\n"
  938. #else
  939. #define CFI_ADJUST_CFA_OFFSET(adjust) /**/
  940. #endif
  941. /* In PIC mode (e.g. when building shared libraries), gcc for i386
  942. * reserves ebx. Unfortunately, most distribution ship with implementations
  943. * of _syscallX() which clobber ebx.
  944. * Also, most definitions of _syscallX() neglect to mark "memory" as being
  945. * clobbered. This causes problems with compilers, that do a better job
  946. * at optimizing across __asm__ calls.
  947. * So, we just have to redefine all of the _syscallX() macros.
  948. */
  949. #undef LSS_BODY
  950. #define LSS_BODY(type,args...) \
  951. long __res; \
  952. __asm__ __volatile__("push %%ebx\n" \
  953. CFI_ADJUST_CFA_OFFSET(4) \
  954. "movl %2,%%ebx\n" \
  955. "int $0x80\n" \
  956. "pop %%ebx\n" \
  957. CFI_ADJUST_CFA_OFFSET(-4) \
  958. args \
  959. : "esp", "memory"); \
  960. LSS_RETURN(type,__res)
  961. #undef _syscall0
  962. #define _syscall0(type,name) \
  963. type LSS_NAME(name)(void) { \
  964. long __res; \
  965. __asm__ volatile("int $0x80" \
  966. : "=a" (__res) \
  967. : "0" (__NR_##name) \
  968. : "memory"); \
  969. LSS_RETURN(type,__res); \
  970. }
  971. #undef _syscall1
  972. #define _syscall1(type,name,type1,arg1) \
  973. type LSS_NAME(name)(type1 arg1) { \
  974. LSS_BODY(type, \
  975. : "=a" (__res) \
  976. : "0" (__NR_##name), "ri" ((long)(arg1))); \
  977. }
  978. #undef _syscall2
  979. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  980. type LSS_NAME(name)(type1 arg1,type2 arg2) { \
  981. LSS_BODY(type, \
  982. : "=a" (__res) \
  983. : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
  984. }
  985. #undef _syscall3
  986. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  987. type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
  988. LSS_BODY(type, \
  989. : "=a" (__res) \
  990. : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
  991. "d" ((long)(arg3))); \
  992. }
  993. #undef _syscall4
  994. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  995. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
  996. LSS_BODY(type, \
  997. : "=a" (__res) \
  998. : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
  999. "d" ((long)(arg3)),"S" ((long)(arg4))); \
  1000. }
  1001. #undef _syscall5
  1002. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1003. type5,arg5) \
  1004. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1005. type5 arg5) { \
  1006. long __res; \
  1007. __asm__ __volatile__("push %%ebx\n" \
  1008. "movl %2,%%ebx\n" \
  1009. "movl %1,%%eax\n" \
  1010. "int $0x80\n" \
  1011. "pop %%ebx" \
  1012. : "=a" (__res) \
  1013. : "i" (__NR_##name), "ri" ((long)(arg1)), \
  1014. "c" ((long)(arg2)), "d" ((long)(arg3)), \
  1015. "S" ((long)(arg4)), "D" ((long)(arg5)) \
  1016. : "esp", "memory"); \
  1017. LSS_RETURN(type,__res); \
  1018. }
  1019. #undef _syscall6
  1020. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1021. type5,arg5,type6,arg6) \
  1022. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1023. type5 arg5, type6 arg6) { \
  1024. long __res; \
  1025. struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
  1026. __asm__ __volatile__("push %%ebp\n" \
  1027. "push %%ebx\n" \
  1028. "movl 4(%2),%%ebp\n" \
  1029. "movl 0(%2), %%ebx\n" \
  1030. "movl %1,%%eax\n" \
  1031. "int $0x80\n" \
  1032. "pop %%ebx\n" \
  1033. "pop %%ebp" \
  1034. : "=a" (__res) \
  1035. : "i" (__NR_##name), "0" ((long)(&__s)), \
  1036. "c" ((long)(arg2)), "d" ((long)(arg3)), \
  1037. "S" ((long)(arg4)), "D" ((long)(arg5)) \
  1038. : "esp", "memory"); \
  1039. LSS_RETURN(type,__res); \
  1040. }
  1041. LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
  1042. int flags, void *arg, int *parent_tidptr,
  1043. void *newtls, int *child_tidptr) {
  1044. long __res;
  1045. __asm__ __volatile__(/* if (fn == NULL)
  1046. * return -EINVAL;
  1047. */
  1048. "movl %3,%%ecx\n"
  1049. "jecxz 1f\n"
  1050. /* if (child_stack == NULL)
  1051. * return -EINVAL;
  1052. */
  1053. "movl %4,%%ecx\n"
  1054. "jecxz 1f\n"
  1055. /* Set up alignment of the child stack:
  1056. * child_stack = (child_stack & ~0xF) - 20;
  1057. */
  1058. "andl $-16,%%ecx\n"
  1059. "subl $20,%%ecx\n"
  1060. /* Push "arg" and "fn" onto the stack that will be
  1061. * used by the child.
  1062. */
  1063. "movl %6,%%eax\n"
  1064. "movl %%eax,4(%%ecx)\n"
  1065. "movl %3,%%eax\n"
  1066. "movl %%eax,(%%ecx)\n"
  1067. /* %eax = syscall(%eax = __NR_clone,
  1068. * %ebx = flags,
  1069. * %ecx = child_stack,
  1070. * %edx = parent_tidptr,
  1071. * %esi = newtls,
  1072. * %edi = child_tidptr)
  1073. * Also, make sure that %ebx gets preserved as it is
  1074. * used in PIC mode.
  1075. */
  1076. "movl %8,%%esi\n"
  1077. "movl %7,%%edx\n"
  1078. "movl %5,%%eax\n"
  1079. "movl %9,%%edi\n"
  1080. "pushl %%ebx\n"
  1081. "movl %%eax,%%ebx\n"
  1082. "movl %2,%%eax\n"
  1083. "int $0x80\n"
  1084. /* In the parent: restore %ebx
  1085. * In the child: move "fn" into %ebx
  1086. */
  1087. "popl %%ebx\n"
  1088. /* if (%eax != 0)
  1089. * return %eax;
  1090. */
  1091. "test %%eax,%%eax\n"
  1092. "jnz 1f\n"
  1093. /* In the child, now. Terminate frame pointer chain.
  1094. */
  1095. "movl $0,%%ebp\n"
  1096. /* Call "fn". "arg" is already on the stack.
  1097. */
  1098. "call *%%ebx\n"
  1099. /* Call _exit(%ebx). Unfortunately older versions
  1100. * of gcc restrict the number of arguments that can
  1101. * be passed to asm(). So, we need to hard-code the
  1102. * system call number.
  1103. */
  1104. "movl %%eax,%%ebx\n"
  1105. "movl $1,%%eax\n"
  1106. "int $0x80\n"
  1107. /* Return to parent.
  1108. */
  1109. "1:\n"
  1110. : "=a" (__res)
  1111. : "0"(-EINVAL), "i"(__NR_clone),
  1112. "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
  1113. "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
  1114. : "esp", "memory", "ecx", "edx", "esi", "edi");
  1115. LSS_RETURN(int, __res);
  1116. }
  1117. LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
  1118. /* On i386, the kernel does not know how to return from a signal
  1119. * handler. Instead, it relies on user space to provide a
  1120. * restorer function that calls the {rt_,}sigreturn() system call.
  1121. * Unfortunately, we cannot just reference the glibc version of this
  1122. * function, as glibc goes out of its way to make it inaccessible.
  1123. */
  1124. void (*res)(void);
  1125. __asm__ __volatile__("call 2f\n"
  1126. "0:.align 16\n"
  1127. "1:movl %1,%%eax\n"
  1128. "int $0x80\n"
  1129. "2:popl %0\n"
  1130. "addl $(1b-0b),%0\n"
  1131. : "=a" (res)
  1132. : "i" (__NR_rt_sigreturn));
  1133. return res;
  1134. }
  1135. LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
  1136. /* On i386, the kernel does not know how to return from a signal
  1137. * handler. Instead, it relies on user space to provide a
  1138. * restorer function that calls the {rt_,}sigreturn() system call.
  1139. * Unfortunately, we cannot just reference the glibc version of this
  1140. * function, as glibc goes out of its way to make it inaccessible.
  1141. */
  1142. void (*res)(void);
  1143. __asm__ __volatile__("call 2f\n"
  1144. "0:.align 16\n"
  1145. "1:pop %%eax\n"
  1146. "movl %1,%%eax\n"
  1147. "int $0x80\n"
  1148. "2:popl %0\n"
  1149. "addl $(1b-0b),%0\n"
  1150. : "=a" (res)
  1151. : "i" (__NR_sigreturn));
  1152. return res;
  1153. }
  1154. #elif defined(__x86_64__)
  1155. /* There are no known problems with any of the _syscallX() macros
  1156. * currently shipping for x86_64, but we still need to be able to define
  1157. * our own version so that we can override the location of the errno
  1158. * location (e.g. when using the clone() system call with the CLONE_VM
  1159. * option).
  1160. */
  1161. #undef LSS_ENTRYPOINT
  1162. #define LSS_ENTRYPOINT "syscall\n"
  1163. /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
  1164. * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
  1165. * sign extension. We can't cast pointers directly because those are
  1166. * 32 bits, and gcc will dump ugly warnings about casting from a pointer
  1167. * to an integer of a different size.
  1168. */
  1169. #undef LSS_SYSCALL_ARG
  1170. #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
  1171. #undef _LSS_RETURN
  1172. #define _LSS_RETURN(type, res, cast) \
  1173. do { \
  1174. if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
  1175. LSS_ERRNO = -(res); \
  1176. res = -1; \
  1177. } \
  1178. return (type)(cast)(res); \
  1179. } while (0)
  1180. #undef LSS_RETURN
  1181. #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
  1182. #undef _LSS_BODY
  1183. #define _LSS_BODY(nr, type, name, cast, ...) \
  1184. long long __res; \
  1185. __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
  1186. : "=a" (__res) \
  1187. : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
  1188. : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
  1189. _LSS_RETURN(type, __res, cast)
  1190. #undef LSS_BODY
  1191. #define LSS_BODY(nr, type, name, args...) \
  1192. _LSS_BODY(nr, type, name, uintptr_t, ## args)
  1193. #undef LSS_BODY_ASM0
  1194. #undef LSS_BODY_ASM1
  1195. #undef LSS_BODY_ASM2
  1196. #undef LSS_BODY_ASM3
  1197. #undef LSS_BODY_ASM4
  1198. #undef LSS_BODY_ASM5
  1199. #undef LSS_BODY_ASM6
  1200. #define LSS_BODY_ASM0
  1201. #define LSS_BODY_ASM1 LSS_BODY_ASM0
  1202. #define LSS_BODY_ASM2 LSS_BODY_ASM1
  1203. #define LSS_BODY_ASM3 LSS_BODY_ASM2
  1204. #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
  1205. #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
  1206. #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
  1207. #undef LSS_BODY_CLOBBER0
  1208. #undef LSS_BODY_CLOBBER1
  1209. #undef LSS_BODY_CLOBBER2
  1210. #undef LSS_BODY_CLOBBER3
  1211. #undef LSS_BODY_CLOBBER4
  1212. #undef LSS_BODY_CLOBBER5
  1213. #undef LSS_BODY_CLOBBER6
  1214. #define LSS_BODY_CLOBBER0
  1215. #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
  1216. #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
  1217. #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
  1218. #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
  1219. #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
  1220. #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
  1221. #undef LSS_BODY_ARG0
  1222. #undef LSS_BODY_ARG1
  1223. #undef LSS_BODY_ARG2
  1224. #undef LSS_BODY_ARG3
  1225. #undef LSS_BODY_ARG4
  1226. #undef LSS_BODY_ARG5
  1227. #undef LSS_BODY_ARG6
  1228. #define LSS_BODY_ARG0()
  1229. #define LSS_BODY_ARG1(arg1) \
  1230. LSS_BODY_ARG0(), "D" (arg1)
  1231. #define LSS_BODY_ARG2(arg1, arg2) \
  1232. LSS_BODY_ARG1(arg1), "S" (arg2)
  1233. #define LSS_BODY_ARG3(arg1, arg2, arg3) \
  1234. LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
  1235. #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
  1236. LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
  1237. #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
  1238. LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
  1239. #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
  1240. LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
  1241. #undef _syscall0
  1242. #define _syscall0(type,name) \
  1243. type LSS_NAME(name)() { \
  1244. LSS_BODY(0, type, name); \
  1245. }
  1246. #undef _syscall1
  1247. #define _syscall1(type,name,type1,arg1) \
  1248. type LSS_NAME(name)(type1 arg1) { \
  1249. LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
  1250. }
  1251. #undef _syscall2
  1252. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  1253. type LSS_NAME(name)(type1 arg1, type2 arg2) { \
  1254. LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
  1255. }
  1256. #undef _syscall3
  1257. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  1258. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
  1259. LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
  1260. LSS_SYSCALL_ARG(arg3)); \
  1261. }
  1262. #undef _syscall4
  1263. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  1264. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
  1265. LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
  1266. LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
  1267. }
  1268. #undef _syscall5
  1269. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1270. type5,arg5) \
  1271. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1272. type5 arg5) { \
  1273. LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
  1274. LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
  1275. LSS_SYSCALL_ARG(arg5)); \
  1276. }
  1277. #undef _syscall6
  1278. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1279. type5,arg5,type6,arg6) \
  1280. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1281. type5 arg5, type6 arg6) { \
  1282. LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
  1283. LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
  1284. LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
  1285. }
  1286. LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
  1287. int flags, void *arg, int *parent_tidptr,
  1288. void *newtls, int *child_tidptr) {
  1289. long long __res;
  1290. {
  1291. __asm__ __volatile__(/* if (fn == NULL)
  1292. * return -EINVAL;
  1293. */
  1294. "testq %4,%4\n"
  1295. "jz 1f\n"
  1296. /* if (child_stack == NULL)
  1297. * return -EINVAL;
  1298. */
  1299. "testq %5,%5\n"
  1300. "jz 1f\n"
  1301. /* Set up alignment of the child stack:
  1302. * child_stack = (child_stack & ~0xF) - 16;
  1303. */
  1304. "andq $-16,%5\n"
  1305. "subq $16,%5\n"
  1306. /* Push "arg" and "fn" onto the stack that will be
  1307. * used by the child.
  1308. */
  1309. "movq %7,8(%5)\n"
  1310. "movq %4,0(%5)\n"
  1311. /* %rax = syscall(%rax = __NR_clone,
  1312. * %rdi = flags,
  1313. * %rsi = child_stack,
  1314. * %rdx = parent_tidptr,
  1315. * %r8 = new_tls,
  1316. * %r10 = child_tidptr)
  1317. */
  1318. "movq %2,%%rax\n"
  1319. "movq %9,%%r8\n"
  1320. "movq %10,%%r10\n"
  1321. "syscall\n"
  1322. /* if (%rax != 0)
  1323. * return;
  1324. */
  1325. "testq %%rax,%%rax\n"
  1326. "jnz 1f\n"
  1327. /* In the child. Terminate frame pointer chain.
  1328. */
  1329. "xorq %%rbp,%%rbp\n"
  1330. /* Call "fn(arg)".
  1331. */
  1332. "popq %%rax\n"
  1333. "popq %%rdi\n"
  1334. "call *%%rax\n"
  1335. /* Call _exit(%ebx).
  1336. */
  1337. "movq %%rax,%%rdi\n"
  1338. "movq %3,%%rax\n"
  1339. "syscall\n"
  1340. /* Return to parent.
  1341. */
  1342. "1:\n"
  1343. : "=a" (__res)
  1344. : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
  1345. "r"(LSS_SYSCALL_ARG(fn)),
  1346. "S"(LSS_SYSCALL_ARG(child_stack)),
  1347. "D"(LSS_SYSCALL_ARG(flags)),
  1348. "r"(LSS_SYSCALL_ARG(arg)),
  1349. "d"(LSS_SYSCALL_ARG(parent_tidptr)),
  1350. "r"(LSS_SYSCALL_ARG(newtls)),
  1351. "r"(LSS_SYSCALL_ARG(child_tidptr))
  1352. : "rsp", "memory", "r8", "r10", "r11", "rcx");
  1353. }
  1354. LSS_RETURN(int, __res);
  1355. }
  1356. LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
  1357. /* On x86-64, the kernel does not know how to return from
  1358. * a signal handler. Instead, it relies on user space to provide a
  1359. * restorer function that calls the rt_sigreturn() system call.
  1360. * Unfortunately, we cannot just reference the glibc version of this
  1361. * function, as glibc goes out of its way to make it inaccessible.
  1362. */
  1363. long long res;
  1364. __asm__ __volatile__("call 2f\n"
  1365. "0:.align 16\n"
  1366. "1:movq %1,%%rax\n"
  1367. "syscall\n"
  1368. "2:popq %0\n"
  1369. "addq $(1b-0b),%0\n"
  1370. : "=a" (res)
  1371. : "i" (__NR_rt_sigreturn));
  1372. return (void (*)(void))(uintptr_t)res;
  1373. }
  1374. #elif defined(__arm__)
  1375. /* Most definitions of _syscallX() neglect to mark "memory" as being
  1376. * clobbered. This causes problems with compilers, that do a better job
  1377. * at optimizing across __asm__ calls.
  1378. * So, we just have to redefine all fo the _syscallX() macros.
  1379. */
  1380. #undef LSS_REG
  1381. #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
  1382. /* r0..r3 are scratch registers and not preserved across function
  1383. * calls. We need to first evaluate the first 4 syscall arguments
  1384. * and store them on stack. They must be loaded into r0..r3 after
  1385. * all function calls to avoid r0..r3 being clobbered.
  1386. */
  1387. #undef LSS_SAVE_ARG
  1388. #define LSS_SAVE_ARG(r,a) long __tmp##r = (long)a
  1389. #undef LSS_LOAD_ARG
  1390. #define LSS_LOAD_ARG(r) register long __r##r __asm__("r"#r) = __tmp##r
  1391. #undef LSS_BODY
  1392. #define LSS_BODY(type, name, args...) \
  1393. register long __res_r0 __asm__("r0"); \
  1394. long __res; \
  1395. __SYS_REG(name) \
  1396. __asm__ __volatile__ (__syscall_safe(name) \
  1397. : "=r"(__res_r0) \
  1398. : __SYS_REG_LIST(args) \
  1399. : "lr", "memory"); \
  1400. __res = __res_r0; \
  1401. LSS_RETURN(type, __res)
  1402. #undef _syscall0
  1403. #define _syscall0(type, name) \
  1404. type LSS_NAME(name)() { \
  1405. LSS_BODY(type, name); \
  1406. }
  1407. #undef _syscall1
  1408. #define _syscall1(type, name, type1, arg1) \
  1409. type LSS_NAME(name)(type1 arg1) { \
  1410. /* There is no need for using a volatile temp. */ \
  1411. LSS_REG(0, arg1); \
  1412. LSS_BODY(type, name, "r"(__r0)); \
  1413. }
  1414. #undef _syscall2
  1415. #define _syscall2(type, name, type1, arg1, type2, arg2) \
  1416. type LSS_NAME(name)(type1 arg1, type2 arg2) { \
  1417. LSS_SAVE_ARG(0, arg1); \
  1418. LSS_SAVE_ARG(1, arg2); \
  1419. LSS_LOAD_ARG(0); \
  1420. LSS_LOAD_ARG(1); \
  1421. LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
  1422. }
  1423. #undef _syscall3
  1424. #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  1425. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
  1426. LSS_SAVE_ARG(0, arg1); \
  1427. LSS_SAVE_ARG(1, arg2); \
  1428. LSS_SAVE_ARG(2, arg3); \
  1429. LSS_LOAD_ARG(0); \
  1430. LSS_LOAD_ARG(1); \
  1431. LSS_LOAD_ARG(2); \
  1432. LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
  1433. }
  1434. #undef _syscall4
  1435. #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  1436. type4, arg4) \
  1437. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
  1438. LSS_SAVE_ARG(0, arg1); \
  1439. LSS_SAVE_ARG(1, arg2); \
  1440. LSS_SAVE_ARG(2, arg3); \
  1441. LSS_SAVE_ARG(3, arg4); \
  1442. LSS_LOAD_ARG(0); \
  1443. LSS_LOAD_ARG(1); \
  1444. LSS_LOAD_ARG(2); \
  1445. LSS_LOAD_ARG(3); \
  1446. LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
  1447. }
  1448. #undef _syscall5
  1449. #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  1450. type4, arg4, type5, arg5) \
  1451. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1452. type5 arg5) { \
  1453. LSS_SAVE_ARG(0, arg1); \
  1454. LSS_SAVE_ARG(1, arg2); \
  1455. LSS_SAVE_ARG(2, arg3); \
  1456. LSS_SAVE_ARG(3, arg4); \
  1457. LSS_REG(4, arg5); \
  1458. LSS_LOAD_ARG(0); \
  1459. LSS_LOAD_ARG(1); \
  1460. LSS_LOAD_ARG(2); \
  1461. LSS_LOAD_ARG(3); \
  1462. LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
  1463. "r"(__r4)); \
  1464. }
  1465. #undef _syscall6
  1466. #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  1467. type4, arg4, type5, arg5, type6, arg6) \
  1468. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1469. type5 arg5, type6 arg6) { \
  1470. LSS_SAVE_ARG(0, arg1); \
  1471. LSS_SAVE_ARG(1, arg2); \
  1472. LSS_SAVE_ARG(2, arg3); \
  1473. LSS_SAVE_ARG(3, arg4); \
  1474. LSS_REG(4, arg5); \
  1475. LSS_REG(5, arg6); \
  1476. LSS_LOAD_ARG(0); \
  1477. LSS_LOAD_ARG(1); \
  1478. LSS_LOAD_ARG(2); \
  1479. LSS_LOAD_ARG(3); \
  1480. LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
  1481. "r"(__r4), "r"(__r5)); \
  1482. }
  1483. LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
  1484. int flags, void *arg, int *parent_tidptr,
  1485. void *newtls, int *child_tidptr) {
  1486. register long __res __asm__("r5");
  1487. {
  1488. if (fn == NULL || child_stack == NULL) {
  1489. __res = -EINVAL;
  1490. goto clone_exit;
  1491. }
  1492. /* stash first 4 arguments on stack first because we can only load
  1493. * them after all function calls.
  1494. */
  1495. int tmp_flags = flags;
  1496. int * tmp_stack = (int*) child_stack;
  1497. void * tmp_ptid = parent_tidptr;
  1498. void * tmp_tls = newtls;
  1499. register int *__ctid __asm__("r4") = child_tidptr;
  1500. /* Push "arg" and "fn" onto the stack that will be
  1501. * used by the child.
  1502. */
  1503. *(--tmp_stack) = (int) arg;
  1504. *(--tmp_stack) = (int) fn;
  1505. /* We must load r0..r3 last after all possible function calls. */
  1506. register int __flags __asm__("r0") = tmp_flags;
  1507. register void *__stack __asm__("r1") = tmp_stack;
  1508. register void *__ptid __asm__("r2") = tmp_ptid;
  1509. register void *__tls __asm__("r3") = tmp_tls;
  1510. /* %r0 = syscall(%r0 = flags,
  1511. * %r1 = child_stack,
  1512. * %r2 = parent_tidptr,
  1513. * %r3 = newtls,
  1514. * %r4 = child_tidptr)
  1515. */
  1516. __SYS_REG(clone)
  1517. __asm__ __volatile__(/* %r0 = syscall(%r0 = flags,
  1518. * %r1 = child_stack,
  1519. * %r2 = parent_tidptr,
  1520. * %r3 = newtls,
  1521. * %r4 = child_tidptr)
  1522. */
  1523. "push {r7}\n"
  1524. "mov r7,%1\n"
  1525. __syscall(clone)"\n"
  1526. /* if (%r0 != 0)
  1527. * return %r0;
  1528. */
  1529. "movs %0,r0\n"
  1530. "bne 1f\n"
  1531. /* In the child, now. Call "fn(arg)".
  1532. */
  1533. "ldr r0,[sp, #4]\n"
  1534. "mov lr,pc\n"
  1535. "ldr pc,[sp]\n"
  1536. /* Call _exit(%r0), which never returns. We only
  1537. * need to set r7 for EABI syscall ABI but we do
  1538. * this always to simplify code sharing between
  1539. * old and new syscall ABIs.
  1540. */
  1541. "mov r7,%2\n"
  1542. __syscall(exit)"\n"
  1543. /* Pop r7 from the stack only in the parent.
  1544. */
  1545. "1: pop {r7}\n"
  1546. : "=r" (__res)
  1547. : "r"(__sysreg),
  1548. "i"(__NR_exit), "r"(__stack), "r"(__flags),
  1549. "r"(__ptid), "r"(__tls), "r"(__ctid)
  1550. : "cc", "lr", "memory");
  1551. }
  1552. clone_exit:
  1553. LSS_RETURN(int, __res);
  1554. }
  1555. #elif defined(__mips__)
  1556. #undef LSS_REG
  1557. #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
  1558. (unsigned long)(a)
  1559. #if _MIPS_SIM == _MIPS_SIM_ABI32
  1560. // See http://sources.redhat.com/ml/libc-alpha/2004-10/msg00050.html
  1561. // or http://www.linux-mips.org/archives/linux-mips/2004-10/msg00142.html
  1562. #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", "$11", "$12",\
  1563. "$13", "$14", "$15", "$24", "$25", "memory"
  1564. #else
  1565. #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", "$13", \
  1566. "$14", "$15", "$24", "$25", "memory"
  1567. #endif
  1568. #undef LSS_BODY
  1569. #define LSS_BODY(type,name,r7,...) \
  1570. register unsigned long __v0 __asm__("$2") = __NR_##name; \
  1571. __asm__ __volatile__ ("syscall\n" \
  1572. : "=&r"(__v0), r7 (__r7) \
  1573. : "0"(__v0), ##__VA_ARGS__ \
  1574. : MIPS_SYSCALL_CLOBBERS); \
  1575. LSS_RETURN(type, __v0, __r7)
  1576. #undef _syscall0
  1577. #define _syscall0(type, name) \
  1578. type LSS_NAME(name)() { \
  1579. register unsigned long __r7 __asm__("$7"); \
  1580. LSS_BODY(type, name, "=r"); \
  1581. }
  1582. #undef _syscall1
  1583. #define _syscall1(type, name, type1, arg1) \
  1584. type LSS_NAME(name)(type1 arg1) { \
  1585. register unsigned long __r7 __asm__("$7"); \
  1586. LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
  1587. }
  1588. #undef _syscall2
  1589. #define _syscall2(type, name, type1, arg1, type2, arg2) \
  1590. type LSS_NAME(name)(type1 arg1, type2 arg2) { \
  1591. register unsigned long __r7 __asm__("$7"); \
  1592. LSS_REG(4, arg1); LSS_REG(5, arg2); \
  1593. LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
  1594. }
  1595. #undef _syscall3
  1596. #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  1597. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
  1598. register unsigned long __r7 __asm__("$7"); \
  1599. LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
  1600. LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
  1601. }
  1602. #undef _syscall4
  1603. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  1604. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
  1605. LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
  1606. LSS_REG(7, arg4); \
  1607. LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
  1608. }
  1609. #undef _syscall5
  1610. #if _MIPS_SIM == _MIPS_SIM_ABI32
  1611. /* The old 32bit MIPS system call API passes the fifth and sixth argument
  1612. * on the stack, whereas the new APIs use registers "r8" and "r9".
  1613. */
  1614. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1615. type5,arg5) \
  1616. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1617. type5 arg5) { \
  1618. LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
  1619. LSS_REG(7, arg4); \
  1620. register unsigned long __v0 __asm__("$2"); \
  1621. __asm__ __volatile__ (".set noreorder\n" \
  1622. "lw $2, %6\n" \
  1623. "subu $29, 32\n" \
  1624. "sw $2, 16($29)\n" \
  1625. "li $2, %2\n" \
  1626. "syscall\n" \
  1627. "addiu $29, 32\n" \
  1628. ".set reorder\n" \
  1629. : "=&r"(__v0), "+r" (__r7) \
  1630. : "i" (__NR_##name), "r"(__r4), "r"(__r5), \
  1631. "r"(__r6), "m" ((unsigned long)arg5) \
  1632. : MIPS_SYSCALL_CLOBBERS); \
  1633. LSS_RETURN(type, __v0, __r7); \
  1634. }
  1635. #else
  1636. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1637. type5,arg5) \
  1638. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1639. type5 arg5) { \
  1640. LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
  1641. LSS_REG(7, arg4); LSS_REG(8, arg5); \
  1642. LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
  1643. "r"(__r8)); \
  1644. }
  1645. #endif
  1646. #undef _syscall6
  1647. #if _MIPS_SIM == _MIPS_SIM_ABI32
  1648. /* The old 32bit MIPS system call API passes the fifth and sixth argument
  1649. * on the stack, whereas the new APIs use registers "r8" and "r9".
  1650. */
  1651. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1652. type5,arg5,type6,arg6) \
  1653. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1654. type5 arg5, type6 arg6) { \
  1655. LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
  1656. LSS_REG(7, arg4); \
  1657. register unsigned long __v0 __asm__("$2"); \
  1658. __asm__ __volatile__ (".set noreorder\n" \
  1659. "lw $2, %6\n" \
  1660. "lw $8, %7\n" \
  1661. "subu $29, 32\n" \
  1662. "sw $2, 16($29)\n" \
  1663. "sw $8, 20($29)\n" \
  1664. "li $2, %2\n" \
  1665. "syscall\n" \
  1666. "addiu $29, 32\n" \
  1667. ".set reorder\n" \
  1668. : "=&r"(__v0), "+r" (__r7) \
  1669. : "i" (__NR_##name), "r"(__r4), "r"(__r5), \
  1670. "r"(__r6), "m" ((unsigned long)arg5), \
  1671. "m" ((unsigned long)arg6) \
  1672. : MIPS_SYSCALL_CLOBBERS); \
  1673. LSS_RETURN(type, __v0, __r7); \
  1674. }
  1675. #else
  1676. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  1677. type5,arg5,type6,arg6) \
  1678. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1679. type5 arg5,type6 arg6) { \
  1680. LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
  1681. LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
  1682. LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
  1683. "r"(__r8), "r"(__r9)); \
  1684. }
  1685. #endif
  1686. LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
  1687. int flags, void *arg, int *parent_tidptr,
  1688. void *newtls, int *child_tidptr) {
  1689. register unsigned long __v0 __asm__("$2");
  1690. register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
  1691. {
  1692. register int __flags __asm__("$4") = flags;
  1693. register void *__stack __asm__("$5") = child_stack;
  1694. register void *__ptid __asm__("$6") = parent_tidptr;
  1695. register int *__ctid __asm__("$8") = child_tidptr;
  1696. __asm__ __volatile__(
  1697. #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
  1698. "subu $29,24\n"
  1699. #elif _MIPS_SIM == _MIPS_SIM_NABI32
  1700. "sub $29,16\n"
  1701. #else
  1702. "dsubu $29,16\n"
  1703. #endif
  1704. /* if (fn == NULL || child_stack == NULL)
  1705. * return -EINVAL;
  1706. */
  1707. "li %0,%2\n"
  1708. "beqz %5,1f\n"
  1709. "beqz %6,1f\n"
  1710. /* Push "arg" and "fn" onto the stack that will be
  1711. * used by the child.
  1712. */
  1713. #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
  1714. "subu %6,32\n"
  1715. "sw %5,0(%6)\n"
  1716. "sw %8,4(%6)\n"
  1717. #elif _MIPS_SIM == _MIPS_SIM_NABI32
  1718. "sub %6,32\n"
  1719. "sw %5,0(%6)\n"
  1720. "sw %8,8(%6)\n"
  1721. #else
  1722. "dsubu %6,32\n"
  1723. "sd %5,0(%6)\n"
  1724. "sd %8,8(%6)\n"
  1725. #endif
  1726. /* $7 = syscall($4 = flags,
  1727. * $5 = child_stack,
  1728. * $6 = parent_tidptr,
  1729. * $7 = newtls,
  1730. * $8 = child_tidptr)
  1731. */
  1732. "li $2,%3\n"
  1733. "syscall\n"
  1734. /* if ($7 != 0)
  1735. * return $2;
  1736. */
  1737. "bnez $7,1f\n"
  1738. "bnez $2,1f\n"
  1739. /* In the child, now. Call "fn(arg)".
  1740. */
  1741. #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
  1742. "lw $25,0($29)\n"
  1743. "lw $4,4($29)\n"
  1744. #elif _MIPS_SIM == _MIPS_SIM_NABI32
  1745. "lw $25,0($29)\n"
  1746. "lw $4,8($29)\n"
  1747. #else
  1748. "ld $25,0($29)\n"
  1749. "ld $4,8($29)\n"
  1750. #endif
  1751. "jalr $25\n"
  1752. /* Call _exit($2)
  1753. */
  1754. "move $4,$2\n"
  1755. "li $2,%4\n"
  1756. "syscall\n"
  1757. "1:\n"
  1758. #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
  1759. "addu $29, 24\n"
  1760. #elif _MIPS_SIM == _MIPS_SIM_NABI32
  1761. "add $29, 16\n"
  1762. #else
  1763. "daddu $29,16\n"
  1764. #endif
  1765. : "=&r" (__v0), "=r" (__r7)
  1766. : "i"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
  1767. "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
  1768. "r"(__ptid), "r"(__r7), "r"(__ctid)
  1769. : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
  1770. "$24", "memory");
  1771. }
  1772. LSS_RETURN(int, __v0, __r7);
  1773. }
  1774. #elif defined (__PPC__)
  1775. #undef LSS_LOADARGS_0
  1776. #define LSS_LOADARGS_0(name, dummy...) \
  1777. __sc_0 = __NR_##name
  1778. #undef LSS_LOADARGS_1
  1779. #define LSS_LOADARGS_1(name, arg1) \
  1780. LSS_LOADARGS_0(name); \
  1781. __sc_3 = (unsigned long) (arg1)
  1782. #undef LSS_LOADARGS_2
  1783. #define LSS_LOADARGS_2(name, arg1, arg2) \
  1784. LSS_LOADARGS_1(name, arg1); \
  1785. __sc_4 = (unsigned long) (arg2)
  1786. #undef LSS_LOADARGS_3
  1787. #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
  1788. LSS_LOADARGS_2(name, arg1, arg2); \
  1789. __sc_5 = (unsigned long) (arg3)
  1790. #undef LSS_LOADARGS_4
  1791. #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
  1792. LSS_LOADARGS_3(name, arg1, arg2, arg3); \
  1793. __sc_6 = (unsigned long) (arg4)
  1794. #undef LSS_LOADARGS_5
  1795. #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
  1796. LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
  1797. __sc_7 = (unsigned long) (arg5)
  1798. #undef LSS_LOADARGS_6
  1799. #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
  1800. LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
  1801. __sc_8 = (unsigned long) (arg6)
  1802. #undef LSS_ASMINPUT_0
  1803. #define LSS_ASMINPUT_0 "0" (__sc_0)
  1804. #undef LSS_ASMINPUT_1
  1805. #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
  1806. #undef LSS_ASMINPUT_2
  1807. #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
  1808. #undef LSS_ASMINPUT_3
  1809. #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
  1810. #undef LSS_ASMINPUT_4
  1811. #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
  1812. #undef LSS_ASMINPUT_5
  1813. #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
  1814. #undef LSS_ASMINPUT_6
  1815. #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
  1816. #undef LSS_BODY
  1817. #define LSS_BODY(nr, type, name, args...) \
  1818. long __sc_ret, __sc_err; \
  1819. { \
  1820. register unsigned long __sc_0 __asm__ ("r0"); \
  1821. register unsigned long __sc_3 __asm__ ("r3"); \
  1822. register unsigned long __sc_4 __asm__ ("r4"); \
  1823. register unsigned long __sc_5 __asm__ ("r5"); \
  1824. register unsigned long __sc_6 __asm__ ("r6"); \
  1825. register unsigned long __sc_7 __asm__ ("r7"); \
  1826. register unsigned long __sc_8 __asm__ ("r8"); \
  1827. \
  1828. LSS_LOADARGS_##nr(name, args); \
  1829. __asm__ __volatile__ \
  1830. ("sc\n\t" \
  1831. "mfcr %0" \
  1832. : "=&r" (__sc_0), \
  1833. "=&r" (__sc_3), "=&r" (__sc_4), \
  1834. "=&r" (__sc_5), "=&r" (__sc_6), \
  1835. "=&r" (__sc_7), "=&r" (__sc_8) \
  1836. : LSS_ASMINPUT_##nr \
  1837. : "cr0", "ctr", "memory", \
  1838. "r9", "r10", "r11", "r12"); \
  1839. __sc_ret = __sc_3; \
  1840. __sc_err = __sc_0; \
  1841. } \
  1842. LSS_RETURN(type, __sc_ret, __sc_err)
  1843. #undef _syscall0
  1844. #define _syscall0(type, name) \
  1845. type LSS_NAME(name)(void) { \
  1846. LSS_BODY(0, type, name); \
  1847. }
  1848. #undef _syscall1
  1849. #define _syscall1(type, name, type1, arg1) \
  1850. type LSS_NAME(name)(type1 arg1) { \
  1851. LSS_BODY(1, type, name, arg1); \
  1852. }
  1853. #undef _syscall2
  1854. #define _syscall2(type, name, type1, arg1, type2, arg2) \
  1855. type LSS_NAME(name)(type1 arg1, type2 arg2) { \
  1856. LSS_BODY(2, type, name, arg1, arg2); \
  1857. }
  1858. #undef _syscall3
  1859. #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  1860. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
  1861. LSS_BODY(3, type, name, arg1, arg2, arg3); \
  1862. }
  1863. #undef _syscall4
  1864. #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  1865. type4, arg4) \
  1866. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
  1867. LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
  1868. }
  1869. #undef _syscall5
  1870. #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  1871. type4, arg4, type5, arg5) \
  1872. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1873. type5 arg5) { \
  1874. LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
  1875. }
  1876. #undef _syscall6
  1877. #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  1878. type4, arg4, type5, arg5, type6, arg6) \
  1879. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  1880. type5 arg5, type6 arg6) { \
  1881. LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
  1882. }
  1883. /* clone function adapted from glibc 2.18 clone.S */
  1884. LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
  1885. int flags, void *arg, int *parent_tidptr,
  1886. void *newtls, int *child_tidptr) {
  1887. long __ret, __err;
  1888. {
  1889. #if defined(__PPC64__)
  1890. /* Stack frame offsets. */
  1891. #if _CALL_ELF != 2
  1892. #define FRAME_MIN_SIZE 112
  1893. #define FRAME_TOC_SAVE 40
  1894. #else
  1895. #define FRAME_MIN_SIZE 32
  1896. #define FRAME_TOC_SAVE 24
  1897. #endif
  1898. register int (*__fn)(void *) __asm__ ("r3") = fn;
  1899. register void *__cstack __asm__ ("r4") = child_stack;
  1900. register int __flags __asm__ ("r5") = flags;
  1901. register void * __arg __asm__ ("r6") = arg;
  1902. register int * __ptidptr __asm__ ("r7") = parent_tidptr;
  1903. register void * __newtls __asm__ ("r8") = newtls;
  1904. register int * __ctidptr __asm__ ("r9") = child_tidptr;
  1905. __asm__ __volatile__(
  1906. /* check for fn == NULL
  1907. * and child_stack == NULL
  1908. */
  1909. "cmpdi cr0, %6, 0\n\t"
  1910. "cmpdi cr1, %7, 0\n\t"
  1911. "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
  1912. "beq- cr0, 1f\n\t"
  1913. /* set up stack frame for child */
  1914. "clrrdi %7, %7, 4\n\t"
  1915. "li 0, 0\n\t"
  1916. "stdu 0, -%13(%7)\n\t"
  1917. /* fn, arg, child_stack are saved acrVoss the syscall */
  1918. "mr 28, %6\n\t"
  1919. "mr 29, %7\n\t"
  1920. "mr 27, %9\n\t"
  1921. /* syscall
  1922. r3 == flags
  1923. r4 == child_stack
  1924. r5 == parent_tidptr
  1925. r6 == newtls
  1926. r7 == child_tidptr */
  1927. "mr 3, %8\n\t"
  1928. "mr 5, %10\n\t"
  1929. "mr 6, %11\n\t"
  1930. "mr 7, %12\n\t"
  1931. "li 0, %4\n\t"
  1932. "sc\n\t"
  1933. /* Test if syscall was successful */
  1934. "cmpdi cr1, 3, 0\n\t"
  1935. "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
  1936. "bne- cr1, 1f\n\t"
  1937. /* Do the function call */
  1938. "std 2, %14(1)\n\t"
  1939. #if _CALL_ELF != 2
  1940. "ld 0, 0(28)\n\t"
  1941. "ld 2, 8(28)\n\t"
  1942. "mtctr 0\n\t"
  1943. #else
  1944. "mr 12, 28\n\t"
  1945. "mtctr 12\n\t"
  1946. #endif
  1947. "mr 3, 27\n\t"
  1948. "bctrl\n\t"
  1949. "ld 2, %14(1)\n\t"
  1950. /* Call _exit(r3) */
  1951. "li 0, %5\n\t"
  1952. "sc\n\t"
  1953. /* Return to parent */
  1954. "1:\n\t"
  1955. "mr %0, 3\n\t"
  1956. : "=r" (__ret), "=r" (__err)
  1957. : "0" (-1), "i" (EINVAL),
  1958. "i" (__NR_clone), "i" (__NR_exit),
  1959. "r" (__fn), "r" (__cstack), "r" (__flags),
  1960. "r" (__arg), "r" (__ptidptr), "r" (__newtls),
  1961. "r" (__ctidptr), "i" (FRAME_MIN_SIZE), "i" (FRAME_TOC_SAVE)
  1962. : "cr0", "cr1", "memory", "ctr",
  1963. "r0", "r29", "r27", "r28");
  1964. #else
  1965. register int (*__fn)(void *) __asm__ ("r8") = fn;
  1966. register void *__cstack __asm__ ("r4") = child_stack;
  1967. register int __flags __asm__ ("r3") = flags;
  1968. register void * __arg __asm__ ("r9") = arg;
  1969. register int * __ptidptr __asm__ ("r5") = parent_tidptr;
  1970. register void * __newtls __asm__ ("r6") = newtls;
  1971. register int * __ctidptr __asm__ ("r7") = child_tidptr;
  1972. __asm__ __volatile__(
  1973. /* check for fn == NULL
  1974. * and child_stack == NULL
  1975. */
  1976. "cmpwi cr0, %6, 0\n\t"
  1977. "cmpwi cr1, %7, 0\n\t"
  1978. "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
  1979. "beq- cr0, 1f\n\t"
  1980. /* set up stack frame for child */
  1981. "clrrwi %7, %7, 4\n\t"
  1982. "li 0, 0\n\t"
  1983. "stwu 0, -16(%7)\n\t"
  1984. /* fn, arg, child_stack are saved across the syscall: r28-30 */
  1985. "mr 28, %6\n\t"
  1986. "mr 29, %7\n\t"
  1987. "mr 27, %9\n\t"
  1988. /* syscall */
  1989. "li 0, %4\n\t"
  1990. /* flags already in r3
  1991. * child_stack already in r4
  1992. * ptidptr already in r5
  1993. * newtls already in r6
  1994. * ctidptr already in r7
  1995. */
  1996. "sc\n\t"
  1997. /* Test if syscall was successful */
  1998. "cmpwi cr1, 3, 0\n\t"
  1999. "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
  2000. "bne- cr1, 1f\n\t"
  2001. /* Do the function call */
  2002. "mtctr 28\n\t"
  2003. "mr 3, 27\n\t"
  2004. "bctrl\n\t"
  2005. /* Call _exit(r3) */
  2006. "li 0, %5\n\t"
  2007. "sc\n\t"
  2008. /* Return to parent */
  2009. "1:\n"
  2010. "mfcr %1\n\t"
  2011. "mr %0, 3\n\t"
  2012. : "=r" (__ret), "=r" (__err)
  2013. : "0" (-1), "1" (EINVAL),
  2014. "i" (__NR_clone), "i" (__NR_exit),
  2015. "r" (__fn), "r" (__cstack), "r" (__flags),
  2016. "r" (__arg), "r" (__ptidptr), "r" (__newtls),
  2017. "r" (__ctidptr)
  2018. : "cr0", "cr1", "memory", "ctr",
  2019. "r0", "r29", "r27", "r28");
  2020. #endif
  2021. }
  2022. LSS_RETURN(int, __ret, __err);
  2023. }
  2024. #elif defined(__aarch64__)
  2025. #undef LSS_REG
  2026. #define LSS_REG(r,a) register long __x##r __asm__("x"#r) = (long)a
  2027. #undef LSS_BODY
  2028. #define LSS_BODY(type,name,args...) \
  2029. register long __res_x0 __asm__("x0"); \
  2030. long __res; \
  2031. __asm__ __volatile__ ("mov x8, %1\n" \
  2032. "svc 0x0\n" \
  2033. : "=r"(__res_x0) \
  2034. : "i"(__NR_##name) , ## args \
  2035. : "memory"); \
  2036. __res = __res_x0; \
  2037. LSS_RETURN(type, __res)
  2038. #undef _syscall0
  2039. #define _syscall0(type, name) \
  2040. type LSS_NAME(name)(void) { \
  2041. LSS_BODY(type, name); \
  2042. }
  2043. #undef _syscall1
  2044. #define _syscall1(type, name, type1, arg1) \
  2045. type LSS_NAME(name)(type1 arg1) { \
  2046. LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__x0)); \
  2047. }
  2048. #undef _syscall2
  2049. #define _syscall2(type, name, type1, arg1, type2, arg2) \
  2050. type LSS_NAME(name)(type1 arg1, type2 arg2) { \
  2051. LSS_REG(0, arg1); LSS_REG(1, arg2); \
  2052. LSS_BODY(type, name, "r"(__x0), "r"(__x1)); \
  2053. }
  2054. #undef _syscall3
  2055. #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  2056. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
  2057. LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
  2058. LSS_BODY(type, name, "r"(__x0), "r"(__x1), "r"(__x2)); \
  2059. }
  2060. #undef _syscall4
  2061. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  2062. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
  2063. LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
  2064. LSS_REG(3, arg4); \
  2065. LSS_BODY(type, name, "r"(__x0), "r"(__x1), "r"(__x2), "r"(__x3)); \
  2066. }
  2067. #undef _syscall5
  2068. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  2069. type5,arg5) \
  2070. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  2071. type5 arg5) { \
  2072. LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
  2073. LSS_REG(3, arg4); LSS_REG(4, arg5); \
  2074. LSS_BODY(type, name, "r"(__x0), "r"(__x1), "r"(__x2), "r"(__x3), \
  2075. "r"(__x4)); \
  2076. }
  2077. #undef _syscall6
  2078. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  2079. type5,arg5,type6,arg6) \
  2080. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  2081. type5 arg5, type6 arg6) { \
  2082. LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
  2083. LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
  2084. LSS_BODY(type, name, "r"(__x0), "r"(__x1), "x"(__x2), "r"(__x3), \
  2085. "r"(__x4), "r"(__x5)); \
  2086. }
  2087. /* clone function adapted from glibc 2.18 clone.S */
  2088. LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
  2089. int flags, void *arg, int *parent_tidptr,
  2090. void *newtls, int *child_tidptr) {
  2091. long __res;
  2092. {
  2093. register int (*__fn)(void *) __asm__("x0") = fn;
  2094. register void *__stack __asm__("x1") = child_stack;
  2095. register int __flags __asm__("x2") = flags;
  2096. register void *__arg __asm__("x3") = arg;
  2097. register int *__ptid __asm__("x4") = parent_tidptr;
  2098. register void *__tls __asm__("x5") = newtls;
  2099. register int *__ctid __asm__("x6") = child_tidptr;
  2100. __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
  2101. * return -EINVAL;
  2102. */
  2103. "cbz x0,1f\n"
  2104. "cbz x1,1f\n"
  2105. /* Push "arg" and "fn" onto the stack that will be
  2106. * used by the child.
  2107. */
  2108. "stp x0,x3, [x1, #-16]!\n"
  2109. "mov x0,x2\n" /* flags */
  2110. "mov x2,x4\n" /* ptid */
  2111. "mov x3,x5\n" /* tls */
  2112. "mov x4,x6\n" /* ctid */
  2113. "mov x8,%9\n" /* clone */
  2114. "svc 0x0\n"
  2115. /* if (%r0 != 0)
  2116. * return %r0;
  2117. */
  2118. "cmp x0, #0\n"
  2119. "bne 2f\n"
  2120. /* In the child, now. Call "fn(arg)".
  2121. */
  2122. "ldp x1, x0, [sp], #16\n"
  2123. "blr x1\n"
  2124. /* Call _exit(%r0).
  2125. */
  2126. "mov x8, %10\n"
  2127. "svc 0x0\n"
  2128. "1:\n"
  2129. "mov x8, %1\n"
  2130. "2:\n"
  2131. : "=r" (__res)
  2132. : "i"(-EINVAL),
  2133. "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
  2134. "r"(__ptid), "r"(__tls), "r"(__ctid),
  2135. "i"(__NR_clone), "i"(__NR_exit)
  2136. : "x30", "memory");
  2137. }
  2138. LSS_RETURN(int, __res);
  2139. }
  2140. #elif defined(__s390x__)
  2141. #undef LSS_REG
  2142. #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
  2143. #undef LSS_BODY
  2144. #define LSS_BODY(type, name, args...) \
  2145. register long __res_r2 __asm__("r2"); \
  2146. long __res; \
  2147. __asm__ __volatile__ \
  2148. ("lgfi %%r1, %1\n\t" \
  2149. "svc 0\n\t" \
  2150. : "=&r"(__res_r2) \
  2151. : "i"(__NR_##name), ## args \
  2152. : "r1", "memory"); \
  2153. __res = __res_r2; \
  2154. LSS_RETURN(type, __res)
  2155. #undef _syscall0
  2156. #define _syscall0(type, name) \
  2157. type LSS_NAME(name)(void) { \
  2158. LSS_BODY(type, name); \
  2159. }
  2160. #undef _syscall1
  2161. #define _syscall1(type, name, type1, arg1) \
  2162. type LSS_NAME(name)(type1 arg1) { \
  2163. LSS_REG(2, arg1); \
  2164. LSS_BODY(type, name, "0"(__r2)); \
  2165. }
  2166. #undef _syscall2
  2167. #define _syscall2(type, name, type1, arg1, type2, arg2) \
  2168. type LSS_NAME(name)(type1 arg1, type2 arg2) { \
  2169. LSS_REG(2, arg1); LSS_REG(3, arg2); \
  2170. LSS_BODY(type, name, "0"(__r2), "r"(__r3)); \
  2171. }
  2172. #undef _syscall3
  2173. #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  2174. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
  2175. LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
  2176. LSS_BODY(type, name, "0"(__r2), "r"(__r3), "r"(__r4)); \
  2177. }
  2178. #undef _syscall4
  2179. #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  2180. type4, arg4) \
  2181. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
  2182. type4 arg4) { \
  2183. LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
  2184. LSS_REG(5, arg4); \
  2185. LSS_BODY(type, name, "0"(__r2), "r"(__r3), "r"(__r4), \
  2186. "r"(__r5)); \
  2187. }
  2188. #undef _syscall5
  2189. #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  2190. type4, arg4, type5, arg5) \
  2191. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
  2192. type4 arg4, type5 arg5) { \
  2193. LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
  2194. LSS_REG(5, arg4); LSS_REG(6, arg5); \
  2195. LSS_BODY(type, name, "0"(__r2), "r"(__r3), "r"(__r4), \
  2196. "r"(__r5), "r"(__r6)); \
  2197. }
  2198. #undef _syscall6
  2199. #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  2200. type4, arg4, type5, arg5, type6, arg6) \
  2201. type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
  2202. type4 arg4, type5 arg5, type6 arg6) { \
  2203. LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
  2204. LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
  2205. LSS_BODY(type, name, "0"(__r2), "r"(__r3), "r"(__r4), \
  2206. "r"(__r5), "r"(__r6), "r"(__r7)); \
  2207. }
  2208. LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
  2209. int flags, void *arg, int *parent_tidptr,
  2210. void *newtls, int *child_tidptr) {
  2211. long __ret;
  2212. {
  2213. register int (*__fn)(void *) __asm__ ("r1") = fn;
  2214. register void *__cstack __asm__ ("r2") = child_stack;
  2215. register int __flags __asm__ ("r3") = flags;
  2216. register void *__arg __asm__ ("r0") = arg;
  2217. register int *__ptidptr __asm__ ("r4") = parent_tidptr;
  2218. register void *__newtls __asm__ ("r6") = newtls;
  2219. register int *__ctidptr __asm__ ("r5") = child_tidptr;
  2220. __asm__ __volatile__ (
  2221. /* arg already in r0 */
  2222. "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
  2223. "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
  2224. "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
  2225. "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
  2226. /* flags already in r3 */
  2227. /* parent_tidptr already in r4 */
  2228. /* child_tidptr already in r5 */
  2229. /* newtls already in r6 */
  2230. "svc %2\n\t" /* invoke clone syscall */
  2231. "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
  2232. "jnz 1f\n\t" /* return to parent if non-zero */
  2233. /* start child thread */
  2234. "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
  2235. "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
  2236. "xc 0(8,%%r15), 0(%%r15)\n\t"
  2237. "basr %%r14, %4\n\t" /* jump to fn */
  2238. "svc %3\n" /* invoke exit syscall */
  2239. "1:\n"
  2240. : "=r" (__ret)
  2241. : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
  2242. "r" (__fn), "r" (__cstack), "r" (__flags), "r" (__arg),
  2243. "r" (__ptidptr), "r" (__newtls), "r" (__ctidptr)
  2244. : "cc", "r14", "memory"
  2245. );
  2246. }
  2247. LSS_RETURN(int, __ret);
  2248. }
  2249. #endif
  2250. #define __NR__exit __NR_exit
  2251. #define __NR__gettid __NR_gettid
  2252. #define __NR__mremap __NR_mremap
  2253. LSS_INLINE _syscall1(int, close, int, f)
  2254. LSS_INLINE _syscall1(int, _exit, int, e)
  2255. LSS_INLINE _syscall3(int, fcntl, int, f,
  2256. int, c, long, a)
  2257. LSS_INLINE _syscall2(int, fstat, int, f,
  2258. struct kernel_stat*, b)
  2259. LSS_INLINE _syscall6(int, futex, int*, a,
  2260. int, o, int, v,
  2261. struct kernel_timespec*, t,
  2262. int*, a2,
  2263. int, v3)
  2264. #ifdef __NR_getdents64
  2265. LSS_INLINE _syscall3(int, getdents64, int, f,
  2266. struct kernel_dirent64*, d, int, c)
  2267. #define KERNEL_DIRENT kernel_dirent64
  2268. #define GETDENTS sys_getdents64
  2269. #else
  2270. LSS_INLINE _syscall3(int, getdents, int, f,
  2271. struct kernel_dirent*, d, int, c)
  2272. #define KERNEL_DIRENT kernel_dirent
  2273. #define GETDENTS sys_getdents
  2274. #endif
  2275. LSS_INLINE _syscall0(pid_t, getpid)
  2276. LSS_INLINE _syscall0(pid_t, getppid)
  2277. LSS_INLINE _syscall0(pid_t, _gettid)
  2278. LSS_INLINE _syscall2(int, kill, pid_t, p,
  2279. int, s)
  2280. #if defined(__x86_64__)
  2281. /* Need to make sure off_t isn't truncated to 32-bits under x32. */
  2282. LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
  2283. _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
  2284. LSS_SYSCALL_ARG(w));
  2285. }
  2286. #else
  2287. LSS_INLINE _syscall3(off_t, lseek, int, f,
  2288. off_t, o, int, w)
  2289. #endif
  2290. LSS_INLINE _syscall2(int, munmap, void*, s,
  2291. size_t, l)
  2292. LSS_INLINE _syscall5(void*, _mremap, void*, o,
  2293. size_t, os, size_t, ns,
  2294. unsigned long, f, void *, a)
  2295. LSS_INLINE _syscall2(int, prctl, int, o,
  2296. long, a)
  2297. LSS_INLINE _syscall4(long, ptrace, int, r,
  2298. pid_t, p, void *, a, void *, d)
  2299. LSS_INLINE _syscall3(ssize_t, read, int, f,
  2300. void *, b, size_t, c)
  2301. LSS_INLINE _syscall4(int, rt_sigaction, int, s,
  2302. const struct kernel_sigaction*, a,
  2303. struct kernel_sigaction*, o, size_t, c)
  2304. LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
  2305. const struct kernel_sigset_t*, s,
  2306. struct kernel_sigset_t*, o, size_t, c);
  2307. LSS_INLINE _syscall0(int, sched_yield)
  2308. LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
  2309. const stack_t*, o)
  2310. #if defined(__NR_fstatat)
  2311. LSS_INLINE _syscall4(int, fstatat, int, d, const char *, p,
  2312. struct kernel_stat*, b, int, flags)
  2313. LSS_INLINE int LSS_NAME(stat)(const char* p, struct kernel_stat* b) {
  2314. return LSS_NAME(fstatat)(AT_FDCWD,p,b,0);
  2315. }
  2316. #else
  2317. LSS_INLINE _syscall2(int, stat, const char*, f,
  2318. struct kernel_stat*, b)
  2319. #endif
  2320. LSS_INLINE _syscall3(ssize_t, write, int, f,
  2321. const void *, b, size_t, c)
  2322. #if defined(__NR_getcpu)
  2323. LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
  2324. unsigned *, node, void *, unused);
  2325. #endif
  2326. #if defined(__x86_64__) || defined(__aarch64__) || \
  2327. (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
  2328. LSS_INLINE _syscall3(int, socket, int, d,
  2329. int, t, int, p)
  2330. #endif
  2331. #if defined(__x86_64__) || defined(__s390x__)
  2332. #if defined(__s390x__)
  2333. LSS_INLINE _syscall1(void*, mmap, void*, a)
  2334. #else
  2335. /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
  2336. LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
  2337. __off64_t o) {
  2338. LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
  2339. LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
  2340. LSS_SYSCALL_ARG(d), (uint64_t)(o));
  2341. }
  2342. #endif
  2343. LSS_INLINE int LSS_NAME(sigaction)(int signum,
  2344. const struct kernel_sigaction *act,
  2345. struct kernel_sigaction *oldact) {
  2346. #if defined(__x86_64__)
  2347. /* On x86_64, the kernel requires us to always set our own
  2348. * SA_RESTORER in order to be able to return from a signal handler.
  2349. * This function must have a "magic" signature that the "gdb"
  2350. * (and maybe the kernel?) can recognize.
  2351. */
  2352. if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
  2353. struct kernel_sigaction a = *act;
  2354. a.sa_flags |= SA_RESTORER;
  2355. a.sa_restorer = LSS_NAME(restore_rt)();
  2356. return LSS_NAME(rt_sigaction)(signum, &a, oldact,
  2357. (KERNEL_NSIG+7)/8);
  2358. } else
  2359. #endif
  2360. {
  2361. return LSS_NAME(rt_sigaction)(signum, act, oldact,
  2362. (KERNEL_NSIG+7)/8);
  2363. }
  2364. }
  2365. LSS_INLINE int LSS_NAME(sigprocmask)(int how,
  2366. const struct kernel_sigset_t *set,
  2367. struct kernel_sigset_t *oldset) {
  2368. return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
  2369. }
  2370. #endif
  2371. #if (defined(__aarch64__)) || \
  2372. (defined(__mips__) && (_MIPS_ISA == _MIPS_ISA_MIPS64))
  2373. LSS_INLINE _syscall6(void*, mmap, void*, s,
  2374. size_t, l, int, p,
  2375. int, f, int, d,
  2376. __off64_t, o)
  2377. LSS_INLINE int LSS_NAME(sigaction)(int signum,
  2378. const struct kernel_sigaction *act,
  2379. struct kernel_sigaction *oldact) {
  2380. return LSS_NAME(rt_sigaction)(signum, act, oldact, (KERNEL_NSIG+7)/8);
  2381. }
  2382. LSS_INLINE int LSS_NAME(sigprocmask)(int how,
  2383. const struct kernel_sigset_t *set,
  2384. struct kernel_sigset_t *oldset) {
  2385. return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
  2386. }
  2387. #endif
  2388. #ifdef __NR_wait4
  2389. LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
  2390. int*, s, int, o,
  2391. struct kernel_rusage*, r)
  2392. LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){
  2393. return LSS_NAME(wait4)(pid, status, options, 0);
  2394. }
  2395. #else
  2396. LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
  2397. int*, s, int, o)
  2398. #endif
  2399. #ifdef __NR_openat
  2400. LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
  2401. LSS_INLINE int LSS_NAME(open)(const char* p, int f, int m) {
  2402. return LSS_NAME(openat)(AT_FDCWD,p,f,m );
  2403. }
  2404. #else
  2405. LSS_INLINE _syscall3(int, open, const char*, p,
  2406. int, f, int, m)
  2407. #endif
  2408. LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
  2409. memset(&set->sig, 0, sizeof(set->sig));
  2410. return 0;
  2411. }
  2412. LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
  2413. memset(&set->sig, -1, sizeof(set->sig));
  2414. return 0;
  2415. }
  2416. LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
  2417. int signum) {
  2418. if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
  2419. LSS_ERRNO = EINVAL;
  2420. return -1;
  2421. } else {
  2422. set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
  2423. |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
  2424. return 0;
  2425. }
  2426. }
  2427. LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
  2428. int signum) {
  2429. if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
  2430. LSS_ERRNO = EINVAL;
  2431. return -1;
  2432. } else {
  2433. set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
  2434. &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
  2435. return 0;
  2436. }
  2437. }
  2438. #if defined(__i386__) || \
  2439. defined(__arm__) || \
  2440. (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || defined(__PPC__)
  2441. #define __NR__sigaction __NR_sigaction
  2442. #define __NR__sigprocmask __NR_sigprocmask
  2443. LSS_INLINE _syscall2(int, fstat64, int, f,
  2444. struct kernel_stat64 *, b)
  2445. LSS_INLINE _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  2446. loff_t *, res, uint, wh)
  2447. #ifdef __PPC64__
  2448. LSS_INLINE _syscall6(void*, mmap, void*, s,
  2449. size_t, l, int, p,
  2450. int, f, int, d,
  2451. off_t, o)
  2452. #else
  2453. #ifndef __ARM_EABI__
  2454. /* Not available on ARM EABI Linux. */
  2455. LSS_INLINE _syscall1(void*, mmap, void*, a)
  2456. #endif
  2457. LSS_INLINE _syscall6(void*, mmap2, void*, s,
  2458. size_t, l, int, p,
  2459. int, f, int, d,
  2460. off_t, o)
  2461. #endif
  2462. LSS_INLINE _syscall3(int, _sigaction, int, s,
  2463. const struct kernel_old_sigaction*, a,
  2464. struct kernel_old_sigaction*, o)
  2465. LSS_INLINE _syscall3(int, _sigprocmask, int, h,
  2466. const unsigned long*, s,
  2467. unsigned long*, o)
  2468. LSS_INLINE _syscall2(int, stat64, const char *, p,
  2469. struct kernel_stat64 *, b)
  2470. LSS_INLINE int LSS_NAME(sigaction)(int signum,
  2471. const struct kernel_sigaction *act,
  2472. struct kernel_sigaction *oldact) {
  2473. int old_errno = LSS_ERRNO;
  2474. int rc;
  2475. struct kernel_sigaction a;
  2476. if (act != NULL) {
  2477. a = *act;
  2478. #ifdef __i386__
  2479. /* On i386, the kernel requires us to always set our own
  2480. * SA_RESTORER when using realtime signals. Otherwise, it does not
  2481. * know how to return from a signal handler. This function must have
  2482. * a "magic" signature that the "gdb" (and maybe the kernel?) can
  2483. * recognize.
  2484. * Apparently, a SA_RESTORER is implicitly set by the kernel, when
  2485. * using non-realtime signals.
  2486. *
  2487. * TODO: Test whether ARM needs a restorer
  2488. */
  2489. if (!(a.sa_flags & SA_RESTORER)) {
  2490. a.sa_flags |= SA_RESTORER;
  2491. a.sa_restorer = (a.sa_flags & SA_SIGINFO)
  2492. ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
  2493. }
  2494. #endif
  2495. }
  2496. rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
  2497. (KERNEL_NSIG+7)/8);
  2498. if (rc < 0 && LSS_ERRNO == ENOSYS) {
  2499. struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
  2500. if (!act) {
  2501. ptr_a = NULL;
  2502. } else {
  2503. oa.sa_handler_ = act->sa_handler_;
  2504. memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
  2505. #ifndef __mips__
  2506. oa.sa_restorer = act->sa_restorer;
  2507. #endif
  2508. oa.sa_flags = act->sa_flags;
  2509. }
  2510. if (!oldact) {
  2511. ptr_oa = NULL;
  2512. }
  2513. LSS_ERRNO = old_errno;
  2514. rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
  2515. if (rc == 0 && oldact) {
  2516. if (act) {
  2517. memcpy(oldact, act, sizeof(*act));
  2518. } else {
  2519. memset(oldact, 0, sizeof(*oldact));
  2520. }
  2521. oldact->sa_handler_ = ptr_oa->sa_handler_;
  2522. oldact->sa_flags = ptr_oa->sa_flags;
  2523. memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
  2524. #ifndef __mips__
  2525. oldact->sa_restorer = ptr_oa->sa_restorer;
  2526. #endif
  2527. }
  2528. }
  2529. return rc;
  2530. }
  2531. LSS_INLINE int LSS_NAME(sigprocmask)(int how,
  2532. const struct kernel_sigset_t *set,
  2533. struct kernel_sigset_t *oldset) {
  2534. int olderrno = LSS_ERRNO;
  2535. int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
  2536. if (rc < 0 && LSS_ERRNO == ENOSYS) {
  2537. LSS_ERRNO = olderrno;
  2538. if (oldset) {
  2539. LSS_NAME(sigemptyset)(oldset);
  2540. }
  2541. rc = LSS_NAME(_sigprocmask)(how,
  2542. set ? &set->sig[0] : NULL,
  2543. oldset ? &oldset->sig[0] : NULL);
  2544. }
  2545. return rc;
  2546. }
  2547. #endif
  2548. #if defined(__i386__) || \
  2549. defined(__PPC__) || \
  2550. (defined(__arm__) && !defined(__ARM_EABI__)) || \
  2551. (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
  2552. defined(__s390x__)
  2553. /* See sys_socketcall in net/socket.c in kernel source.
  2554. * It de-multiplexes on its first arg and unpacks the arglist
  2555. * array in its second arg.
  2556. */
  2557. LSS_INLINE _syscall2(int, socketcall, int, c, unsigned long*, a)
  2558. LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
  2559. unsigned long args[3] = {
  2560. (unsigned long) domain,
  2561. (unsigned long) type,
  2562. (unsigned long) protocol
  2563. };
  2564. return LSS_NAME(socketcall)(1, args);
  2565. }
  2566. #elif defined(__ARM_EABI__)
  2567. LSS_INLINE _syscall3(int, socket, int, d,
  2568. int, t, int, p)
  2569. #endif
  2570. #if defined(__mips__)
  2571. /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
  2572. * both file handles through CPU registers.
  2573. */
  2574. LSS_INLINE int LSS_NAME(pipe)(int *p) {
  2575. register unsigned long __v0 __asm__("$2") = __NR_pipe;
  2576. register unsigned long __v1 __asm__("$3");
  2577. register unsigned long __r7 __asm__("$7");
  2578. __asm__ __volatile__ ("syscall\n"
  2579. : "=&r"(__v0), "=&r"(__v1), "+r" (__r7)
  2580. : "0"(__v0)
  2581. : "$8", "$9", "$10", "$11", "$12",
  2582. "$13", "$14", "$15", "$24", "memory");
  2583. if (__r7) {
  2584. LSS_ERRNO = __v0;
  2585. return -1;
  2586. } else {
  2587. p[0] = __v0;
  2588. p[1] = __v1;
  2589. return 0;
  2590. }
  2591. }
  2592. #elif defined(__NR_pipe2)
  2593. LSS_INLINE _syscall2(int, pipe2, int *, p,
  2594. int, f )
  2595. LSS_INLINE int LSS_NAME(pipe)( int * p) {
  2596. return LSS_NAME(pipe2)(p, 0);
  2597. }
  2598. #else
  2599. LSS_INLINE _syscall1(int, pipe, int *, p)
  2600. #endif
  2601. LSS_INLINE pid_t LSS_NAME(gettid)() {
  2602. pid_t tid = LSS_NAME(_gettid)();
  2603. if (tid != -1) {
  2604. return tid;
  2605. }
  2606. return LSS_NAME(getpid)();
  2607. }
  2608. LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
  2609. size_t new_size, int flags, ...) {
  2610. va_list ap;
  2611. void *new_address, *rc;
  2612. va_start(ap, flags);
  2613. new_address = va_arg(ap, void *);
  2614. rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
  2615. flags, new_address);
  2616. va_end(ap);
  2617. return rc;
  2618. }
  2619. LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
  2620. /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
  2621. * then sends job control signals to the real parent, rather than to
  2622. * the tracer. We reduce the risk of this happening by starting a
  2623. * whole new time slice, and then quickly sending a SIGCONT signal
  2624. * right after detaching from the tracee.
  2625. */
  2626. int rc, err;
  2627. LSS_NAME(sched_yield)();
  2628. rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
  2629. err = LSS_ERRNO;
  2630. LSS_NAME(kill)(pid, SIGCONT);
  2631. LSS_ERRNO = err;
  2632. return rc;
  2633. }
  2634. #endif
  2635. #if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
  2636. }
  2637. #endif
  2638. #endif
  2639. #endif