linux.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. BSD parts (c) 2000 by Marco van de Voort
  5. members of the Free Pascal development team.
  6. New linux unit. Linux only calls only. Will be renamed to linux.pp
  7. when 1.0.x support is killed off.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY;without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit Linux;
  15. {$packrecords c}
  16. interface
  17. uses
  18. BaseUnix, unixtype;
  19. type
  20. TSysInfo = record
  21. uptime: clong; //* Seconds since boot */
  22. loads: array[0..2] of culong; //* 1, 5, and 15 minute load averages */
  23. totalram: culong; //* Total usable main memory size */
  24. freeram: culong; //* Available memory size */
  25. sharedram: culong; //* Amount of shared memory */
  26. bufferram: culong; //* Memory used by buffers */
  27. totalswap: culong; //* Total swap space size */
  28. freeswap: culong; //* swap space still available */
  29. procs: cushort; //* Number of current processes */
  30. pad: cushort; //* explicit padding for m68k */
  31. totalhigh: culong; //* Total high memory size */
  32. freehigh: culong; //* Available high memory size */
  33. mem_unit: cuint; //* Memory unit size in bytes */
  34. {$ifndef cpu64}
  35. { the upper bound of the array below is negative for 64 bit cpus }
  36. _f: array[0..19-2*sizeof(clong)-sizeof(cint)] of cChar; //* Padding: libc5 uses this.. */
  37. {$endif cpu64}
  38. end;
  39. PSysInfo = ^TSysInfo;
  40. function Sysinfo(Info: PSysinfo): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'sysinfo'; {$endif}
  41. const
  42. CSIGNAL = $000000ff; // signal mask to be sent at exit
  43. CLONE_VM = $00000100; // set if VM shared between processes
  44. CLONE_FS = $00000200; // set if fs info shared between processes
  45. CLONE_FILES = $00000400; // set if open files shared between processes
  46. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  47. CLONE_PID = $00001000; // set if pid shared
  48. CLONE_PTRACE = $00002000; // Set if tracing continues on the child.
  49. CLONE_VFORK = $00004000; // Set if the parent wants the child to wake it up on mm_release.
  50. CLONE_PARENT = $00008000; // Set if we want to have the same parent as the cloner.
  51. CLONE_THREAD = $00010000; // Set to add to same thread group.
  52. CLONE_NEWNS = $00020000; // Set to create new namespace.
  53. CLONE_SYSVSEM = $00040000; // Set to shared SVID SEM_UNDO semantics.
  54. CLONE_SETTLS = $00080000; // Set TLS info.
  55. CLONE_PARENT_SETTID = $00100000; // Store TID in userlevel buffer before MM copy.
  56. CLONE_CHILD_CLEARTID = $00200000; // Register exit futex and memory location to clear.
  57. CLONE_DETACHED = $00400000; // Create clone detached.
  58. CLONE_UNTRACED = $00800000; // Set if the tracing process can't force CLONE_PTRACE on this clone.
  59. CLONE_CHILD_SETTID = $01000000; // Store TID in userlevel buffer in the child.
  60. CLONE_STOPPED = $02000000; // Start in stopped state.
  61. FUTEX_WAIT = 0;
  62. FUTEX_WAKE = 1;
  63. FUTEX_FD = 2;
  64. FUTEX_REQUEUE = 3;
  65. FUTEX_CMP_REQUEUE = 4;
  66. FUTEX_WAKE_OP = 5;
  67. FUTEX_LOCK_PI = 6;
  68. FUTEX_UNLOCK_PI = 7;
  69. FUTEX_TRYLOCK_PI = 8;
  70. FUTEX_OP_SET = 0; // *(int *)UADDR2 = OPARG;
  71. FUTEX_OP_ADD = 1; // *(int *)UADDR2 += OPARG;
  72. FUTEX_OP_OR = 2; // *(int *)UADDR2 |= OPARG;
  73. FUTEX_OP_ANDN = 3; // *(int *)UADDR2 &= ~OPARG;
  74. FUTEX_OP_XOR = 4; // *(int *)UADDR2 ^= OPARG;
  75. FUTEX_OP_OPARG_SHIFT = 8; // Use (1 << OPARG) instead of OPARG.
  76. FUTEX_OP_CMP_EQ = 0; // if (oldval == CMPARG) wake
  77. FUTEX_OP_CMP_NE = 1; // if (oldval != CMPARG) wake
  78. FUTEX_OP_CMP_LT = 2; // if (oldval < CMPARG) wake
  79. FUTEX_OP_CMP_LE = 3; // if (oldval <= CMPARG) wake
  80. FUTEX_OP_CMP_GT = 4; // if (oldval > CMPARG) wake
  81. FUTEX_OP_CMP_GE = 5; // if (oldval >= CMPARG) wake
  82. { FUTEX_WAKE_OP will perform atomically
  83. int oldval = *(int *)UADDR2;
  84. *(int *)UADDR2 = oldval OP OPARG;
  85. if (oldval CMP CMPARG)
  86. wake UADDR2; }
  87. {$ifndef FPC_USE_LIBC}
  88. function futex_op(op, oparg, cmp, cmparg: cint): cint; {$ifdef SYSTEMINLINE}inline;{$endif}
  89. {$endif}
  90. const
  91. EPOLLIN = $01; { The associated file is available for read(2) operations. }
  92. EPOLLPRI = $02; { There is urgent data available for read(2) operations. }
  93. EPOLLOUT = $04; { The associated file is available for write(2) operations. }
  94. EPOLLERR = $08; { Error condition happened on the associated file descriptor. }
  95. EPOLLHUP = $10; { Hang up happened on the associated file descriptor. }
  96. EPOLLET = $80000000; { Sets the Edge Triggered behaviour for the associated file descriptor. }
  97. { Valid opcodes ( "op" parameter ) to issue to epoll_ctl }
  98. EPOLL_CTL_ADD = 1;
  99. EPOLL_CTL_DEL = 2;
  100. EPOLL_CTL_MOD = 3;
  101. {Some console iotcl's.}
  102. GIO_FONT = $4B60; {gets font in expanded form}
  103. PIO_FONT = $4B61; {use font in expanded form}
  104. GIO_FONTX = $4B6B; {get font using struct consolefontdesc}
  105. PIO_FONTX = $4B6C; {set font using struct consolefontdesc}
  106. PIO_FONTRESET = $4B6D; {reset to default font}
  107. GIO_CMAP = $4B70; {gets colour palette on VGA+}
  108. PIO_CMAP = $4B71; {sets colour palette on VGA+}
  109. KIOCSOUND = $4B2F; {start sound generation (0 for off)}
  110. KDMKTONE = $4B30; {generate tone}
  111. KDGETLED = $4B31; {return current led state}
  112. KDSETLED = $4B32; {set led state [lights, not flags]}
  113. KDGKBTYPE = $4B33; {get keyboard type}
  114. KDADDIO = $4B34; {add i/o port as valid}
  115. KDDELIO = $4B35; {del i/o port as valid}
  116. KDENABIO = $4B36; {enable i/o to video board}
  117. KDDISABIO = $4B37; {disable i/o to video board}
  118. KDSETMODE = $4B3A; {set text/graphics mode}
  119. KDGETMODE = $4B3B; {get current mode}
  120. KDMAPDISP = $4B3C; {map display into address space}
  121. KDUNMAPDISP = $4B3D; {unmap display from address space}
  122. GIO_SCRNMAP = $4B40; {get screen mapping from kernel}
  123. PIO_SCRNMAP = $4B41; {put screen mapping table in kernel}
  124. GIO_UNISCRNMAP = $4B69; {get full Unicode screen mapping}
  125. PIO_UNISCRNMAP = $4B6A; {set full Unicode screen mapping}
  126. GIO_UNIMAP = $4B66; {get unicode-to-font mapping from kernel}
  127. PIO_UNIMAP = $4B67; {put unicode-to-font mapping in kernel}
  128. PIO_UNIMAPCLR = $4B68; {clear table, possibly advise hash algorithm}
  129. KDGKBDIACR = $4B4A; {read kernel accent table}
  130. KDSKBDIACR = $4B4B; {write kernel accent table}
  131. KDGETKEYCODE = $4B4C; {read kernel keycode table entry}
  132. KDSETKEYCODE = $4B4D; {write kernel keycode table entry}
  133. KDSIGACCEPT = $4B4E; {accept kbd generated signals}
  134. KDFONTOP = $4B72; {font operations}
  135. {Keyboard types (for KDGKBTYPE)}
  136. KB_84 = 1;
  137. KB_101 = 2; {Normal PC keyboard.}
  138. KB_OTHER = 3;
  139. {Keyboard LED constants.}
  140. LED_SCR = 1; {scroll lock led}
  141. LED_NUM = 2; {num lock led}
  142. LED_CAP = 4; {caps lock led}
  143. {Tty modes. (for KDSETMODE)}
  144. KD_TEXT = 0;
  145. KD_GRAPHICS = 1;
  146. KD_TEXT0 = 2; {obsolete}
  147. KD_TEXT1 = 3; {obsolete}
  148. MAP_GROWSDOWN = $100; { stack-like segment }
  149. MAP_DENYWRITE = $800; { ETXTBSY }
  150. MAP_EXECUTABLE = $1000; { mark it as an executable }
  151. MAP_LOCKED = $2000; { pages are locked }
  152. MAP_NORESERVE = $4000; { don't check for reservations }
  153. type
  154. TCloneFunc = function(args:pointer):longint;cdecl;
  155. {$ifdef cpui386}
  156. {$define clone_implemented}
  157. {$endif}
  158. {$ifdef cpum68k}
  159. {$define clone_implemented}
  160. {$endif}
  161. {$ifdef clone_implemented}
  162. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  163. {$endif}
  164. const
  165. MODIFY_LDT_CONTENTS_DATA = 0;
  166. MODIFY_LDT_CONTENTS_STACK = 1;
  167. MODIFY_LDT_CONTENTS_CODE = 2;
  168. { Flags for user_desc.flags }
  169. UD_SEG_32BIT = $01;
  170. UD_CONTENTS_DATA = MODIFY_LDT_CONTENTS_DATA shl 1;
  171. UD_CONTENTS_STACK = MODIFY_LDT_CONTENTS_STACK shl 1;
  172. UD_CONTENTS_CODE = MODIFY_LDT_CONTENTS_CODE shl 1;
  173. UD_READ_EXEC_ONLY = $08;
  174. UD_LIMIT_IN_PAGES = $10;
  175. UD_SEG_NOT_PRESENT = $20;
  176. UD_USEABLE = $40;
  177. UD_LM = $80;
  178. type
  179. user_desc = packed record
  180. entry_number : cint;
  181. base_addr : cuint;
  182. limit : cuint;
  183. flags : cuint;
  184. end;
  185. TUser_Desc = user_desc;
  186. PUser_Desc = ^user_desc;
  187. type
  188. EPoll_Data = record
  189. case integer of
  190. 0: (ptr: pointer);
  191. 1: (fd: cint);
  192. 2: (u32: cuint);
  193. 3: (u64: cuint64);
  194. end;
  195. TEPoll_Data = Epoll_Data;
  196. PEPoll_Data = ^Epoll_Data;
  197. EPoll_Event = {$ifdef cpu64} packed {$endif} record
  198. Events: cuint32;
  199. Data : TEpoll_Data;
  200. end;
  201. TEPoll_Event = Epoll_Event;
  202. PEpoll_Event = ^Epoll_Event;
  203. { open an epoll file descriptor }
  204. function epoll_create(size: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_create'; {$endif}
  205. { control interface for an epoll descriptor }
  206. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_ctl'; {$endif}
  207. { wait for an I/O event on an epoll file descriptor }
  208. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_wait'; {$endif}
  209. type Puser_cap_header=^user_cap_header;
  210. user_cap_header=packed record
  211. version,pid:cardinal;
  212. end;
  213. Puser_cap_data=^user_cap_data;
  214. user_cap_data=packed record
  215. effective,permitted,inheritable:cardinal;
  216. end;
  217. {Get a capability.}
  218. function capget(header:Puser_cap_header;data:Puser_cap_data):cint;{$ifdef FPC_USE_LIBC} cdecl; external name 'capget'; {$endif}
  219. {Set a capability.}
  220. function capset(header:Puser_cap_header;data:Puser_cap_data):cint;{$ifdef FPC_USE_LIBC} cdecl; external name 'capset'; {$endif}
  221. const CAP_CHOWN = 0;
  222. CAP_DAC_OVERRIDE = 1;
  223. CAP_DAC_READ_SEARCH = 2;
  224. CAP_FOWNER = 3;
  225. CAP_FSETID = 4;
  226. CAP_FS_MASK = $1f;
  227. CAP_KILL = 5;
  228. CAP_SETGID = 6;
  229. CAP_SETUID = 7;
  230. CAP_SETPCAP = 8;
  231. CAP_LINUX_IMMUTABLE = 9;
  232. CAP_NET_BIND_SERVICE = 10;
  233. CAP_NET_BROADCAST = 11;
  234. CAP_NET_ADMIN = 12;
  235. CAP_NET_RAW = 13;
  236. CAP_IPC_LOCK = 14;
  237. CAP_IPC_OWNER = 15;
  238. CAP_SYS_MODULE = 16;
  239. CAP_SYS_RAWIO = 17;
  240. CAP_SYS_CHROOT = 18;
  241. CAP_SYS_PTRACE = 19;
  242. CAP_SYS_PACCT = 20;
  243. CAP_SYS_ADMIN = 21;
  244. CAP_SYS_BOOT = 22;
  245. CAP_SYS_NICE = 23;
  246. CAP_SYS_RESOURCE = 24;
  247. CAP_SYS_TIME = 25;
  248. CAP_SYS_TTY_CONFIG = 26;
  249. CAP_MKNOD = 27;
  250. CAP_LEASE = 28;
  251. CAP_AUDIT_WRITE = 29;
  252. CAP_AUDIT_CONTROL = 30;
  253. LINUX_CAPABILITY_VERSION = $19980330;
  254. //***********************************************SPLICE from kernel 2.6.17+****************************************
  255. const
  256. {* Flags for SPLICE and VMSPLICE. *}
  257. SPLICE_F_MOVE = 1; { Move pages instead of copying. }
  258. SPLICE_F_NONBLOCK = 2; {* Don't block on the pipe splicing
  259. (but we may still block on the fd
  260. we splice from/to). *}
  261. SPLICE_F_MORE = 4; {* Expect more data. *}
  262. SPLICE_F_GIFT = 8; {* Pages passed in are a gift. *}
  263. {$ifdef cpu86}
  264. {* Splice address range into a pipe. *}
  265. function vmsplice (fdout: cInt; iov: PIOVec; count: size_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'vmsplice'; {$ENDIF}
  266. {* Splice two files together. *}
  267. function splice (fdin: cInt; offin: off64_t; fdout: cInt;
  268. offout: off64_t; len: size_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'splice'; {$ENDIF}
  269. function tee(fd_in: cInt; fd_out: cInt; len: size_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'tee'; {$ENDIF}
  270. {$endif} // x86
  271. const
  272. { flags for sync_file_range }
  273. SYNC_FILE_RANGE_WAIT_BEFORE = 1;
  274. SYNC_FILE_RANGE_WRITE = 2;
  275. SYNC_FILE_RANGE_WAIT_AFTER = 4;
  276. function sync_file_range(fd: cInt; offset, nbytes: off64_t; flags: cuInt): cInt; {$ifdef FPC_USE_LIBC} cdecl; external name 'sync_file_range'; {$ENDIF}
  277. implementation
  278. {$ifndef FPC_USE_LIBC}
  279. Uses Syscall;
  280. function Sysinfo(Info: PSysinfo): cInt;
  281. begin
  282. Sysinfo := do_SysCall(SysCall_nr_Sysinfo, TSysParam(info));
  283. end;
  284. {$ifdef clone_implemented}
  285. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  286. begin
  287. if (pointer(func)=nil) or (sp=nil) then
  288. exit(-1); // give an error result
  289. {$ifdef cpui386}
  290. {$ASMMODE ATT}
  291. asm
  292. { Insert the argument onto the new stack. }
  293. movl sp,%ecx
  294. subl $8,%ecx
  295. movl args,%eax
  296. movl %eax,4(%ecx)
  297. { Save the function pointer as the zeroth argument.
  298. It will be popped off in the child in the ebx frobbing below. }
  299. movl func,%eax
  300. movl %eax,0(%ecx)
  301. { Do the system call }
  302. pushl %ebx
  303. movl flags,%ebx
  304. movl SysCall_nr_clone,%eax
  305. int $0x80
  306. popl %ebx
  307. test %eax,%eax
  308. jnz .Lclone_end
  309. { We're in the new thread }
  310. subl %ebp,%ebp { terminate the stack frame }
  311. call *%ebx
  312. { exit process }
  313. movl %eax,%ebx
  314. movl $1,%eax
  315. int $0x80
  316. .Lclone_end:
  317. movl %eax,__RESULT
  318. end;
  319. {$endif cpui386}
  320. {$ifdef cpum68k}
  321. { No yet translated, my m68k assembler is too weak for such things PM }
  322. (*
  323. asm
  324. { Insert the argument onto the new stack. }
  325. movl sp,%ecx
  326. subl $8,%ecx
  327. movl args,%eax
  328. movl %eax,4(%ecx)
  329. { Save the function pointer as the zeroth argument.
  330. It will be popped off in the child in the ebx frobbing below. }
  331. movl func,%eax
  332. movl %eax,0(%ecx)
  333. { Do the system call }
  334. pushl %ebx
  335. movl flags,%ebx
  336. movl SysCall_nr_clone,%eax
  337. int $0x80
  338. popl %ebx
  339. test %eax,%eax
  340. jnz .Lclone_end
  341. { We're in the new thread }
  342. subl %ebp,%ebp { terminate the stack frame }
  343. call *%ebx
  344. { exit process }
  345. movl %eax,%ebx
  346. movl $1,%eax
  347. int $0x80
  348. .Lclone_end:
  349. movl %eax,__RESULT
  350. end;
  351. *)
  352. {$endif cpum68k}
  353. end;
  354. {$endif}
  355. function epoll_create(size: cint): cint;
  356. begin
  357. epoll_create := do_syscall(syscall_nr_epoll_create,tsysparam(size));
  358. end;
  359. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint;
  360. begin
  361. epoll_ctl := do_syscall(syscall_nr_epoll_ctl, tsysparam(epfd),
  362. tsysparam(op), tsysparam(fd), tsysparam(event));
  363. end;
  364. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint;
  365. begin
  366. epoll_wait := do_syscall(syscall_nr_epoll_wait, tsysparam(epfd),
  367. tsysparam(events), tsysparam(maxevents), tsysparam(timeout));
  368. end;
  369. function capget(header:Puser_cap_header;data:Puser_cap_data):cint;
  370. begin
  371. capget:=do_syscall(syscall_nr_capget,Tsysparam(header),Tsysparam(data));
  372. end;
  373. function capset(header:Puser_cap_header;data:Puser_cap_data):cint;
  374. begin
  375. capset:=do_syscall(syscall_nr_capset,Tsysparam(header),Tsysparam(data));
  376. end;
  377. // TODO: update also on non x86!
  378. {$ifdef cpu86} // didn't update syscall_nr on others yet
  379. function vmsplice (fdout: cInt; iov: PIOVec; count: size_t; flags: cuInt): cInt;
  380. begin
  381. vmsplice := do_syscall(syscall_nr_vmsplice, TSysParam(fdout), TSysParam(iov),
  382. TSysParam(count), TSysParam(flags));
  383. end;
  384. function splice (fdin: cInt; offin: off64_t; fdout: cInt; offout: off64_t; len: size_t; flags: cuInt): cInt;
  385. begin
  386. splice := do_syscall(syscall_nr_splice, TSysParam(fdin), TSysParam(@offin),
  387. TSysParam(fdout), TSysParam(@offout), TSysParam(len), TSysParam(flags));
  388. end;
  389. function tee(fd_in: cInt; fd_out: cInt; len: size_t; flags: cuInt): cInt;
  390. begin
  391. tee := do_syscall(syscall_nr_tee, TSysParam(fd_in), TSysParam(fd_out),
  392. TSysParam(len), TSysParam(flags));
  393. end;
  394. {$endif} // x86
  395. function sync_file_range(fd: cInt; offset: off64_t; nbytes: off64_t; flags: cuInt): cInt;
  396. begin
  397. {$ifdef cpu64}
  398. sync_file_range := do_syscall(syscall_nr_sync_file_range, TSysParam(fd), TSysParam(offset),
  399. TSysParam(nbytes), TSysParam(flags));
  400. {$else}
  401. {$if defined(cpupowerpc) or defined(cpuarm)}
  402. sync_file_range := do_syscall(syscall_nr_sync_file_range2, TSysParam(fd), TSysParam(flags),
  403. TSysParam(hi(offset)), TSysParam(lo(offset)), TSysParam(hi(nbytes)), TSysParam(lo(nbytes)));
  404. {$else}
  405. sync_file_range := do_syscall(syscall_nr_sync_file_range, TSysParam(fd), TSysParam(lo(offset)),
  406. TSysParam(hi(offset)), TSysParam(lo(nbytes)), TSysParam(hi(nbytes)), TSysParam(flags));
  407. {$endif}
  408. {$endif}
  409. end;
  410. {$endif} // non-libc
  411. { FUTEX_OP is a macro, doesn't exist in libC as function}
  412. function FUTEX_OP(op, oparg, cmp, cmparg: cint): cint; {$ifdef SYSTEMINLINE}inline;{$endif}
  413. begin
  414. FUTEX_OP := ((op and $F) shl 28) or ((cmp and $F) shl 24) or ((oparg and $FFF) shl 12) or (cmparg and $FFF);
  415. end;
  416. end.