linux.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. ctypes;
  19. type
  20. TSysinfo = packed record
  21. uptime : longint;
  22. loads : array[1..3] of longint;
  23. totalram,
  24. freeram,
  25. sharedram,
  26. bufferram,
  27. totalswap,
  28. freeswap : longint;
  29. procs : integer;
  30. s : string[18];
  31. end;
  32. PSysInfo = ^TSysInfo;
  33. function Sysinfo(var Info:TSysinfo):Boolean; {$ifdef FPC_USE_LIBC} cdecl; external name 'sysinfo'; {$endif}
  34. const
  35. CSIGNAL = $000000ff; // signal mask to be sent at exit
  36. CLONE_VM = $00000100; // set if VM shared between processes
  37. CLONE_FS = $00000200; // set if fs info shared between processes
  38. CLONE_FILES = $00000400; // set if open files shared between processes
  39. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  40. CLONE_PID = $00001000; // set if pid shared
  41. CLONE_PTRACE = $00002000; // Set if tracing continues on the child.
  42. CLONE_VFORK = $00004000; // Set if the parent wants the child to wake it up on mm_release.
  43. CLONE_PARENT = $00008000; // Set if we want to have the same parent as the cloner.
  44. CLONE_THREAD = $00010000; // Set to add to same thread group.
  45. CLONE_NEWNS = $00020000; // Set to create new namespace.
  46. CLONE_SYSVSEM = $00040000; // Set to shared SVID SEM_UNDO semantics.
  47. CLONE_SETTLS = $00080000; // Set TLS info.
  48. CLONE_PARENT_SETTID = $00100000; // Store TID in userlevel buffer before MM copy.
  49. CLONE_CHILD_CLEARTID = $00200000; // Register exit futex and memory location to clear.
  50. CLONE_DETACHED = $00400000; // Create clone detached.
  51. CLONE_UNTRACED = $00800000; // Set if the tracing process can't force CLONE_PTRACE on this clone.
  52. CLONE_CHILD_SETTID = $01000000; // Store TID in userlevel buffer in the child.
  53. CLONE_STOPPED = $02000000; // Start in stopped state.
  54. FUTEX_WAIT = 0;
  55. FUTEX_WAKE = 1;
  56. FUTEX_FD = 2;
  57. FUTEX_REQUEUE = 3;
  58. FUTEX_CMP_REQUEUE = 4;
  59. FUTEX_WAKE_OP = 5;
  60. FUTEX_LOCK_PI = 6;
  61. FUTEX_UNLOCK_PI = 7;
  62. FUTEX_TRYLOCK_PI = 8;
  63. FUTEX_OP_SET = 0; // *(int *)UADDR2 = OPARG;
  64. FUTEX_OP_ADD = 1; // *(int *)UADDR2 += OPARG;
  65. FUTEX_OP_OR = 2; // *(int *)UADDR2 |= OPARG;
  66. FUTEX_OP_ANDN = 3; // *(int *)UADDR2 &= ~OPARG;
  67. FUTEX_OP_XOR = 4; // *(int *)UADDR2 ^= OPARG;
  68. FUTEX_OP_OPARG_SHIFT = 8; // Use (1 << OPARG) instead of OPARG.
  69. FUTEX_OP_CMP_EQ = 0; // if (oldval == CMPARG) wake
  70. FUTEX_OP_CMP_NE = 1; // if (oldval != CMPARG) wake
  71. FUTEX_OP_CMP_LT = 2; // if (oldval < CMPARG) wake
  72. FUTEX_OP_CMP_LE = 3; // if (oldval <= CMPARG) wake
  73. FUTEX_OP_CMP_GT = 4; // if (oldval > CMPARG) wake
  74. FUTEX_OP_CMP_GE = 5; // if (oldval >= CMPARG) wake
  75. { FUTEX_WAKE_OP will perform atomically
  76. int oldval = *(int *)UADDR2;
  77. *(int *)UADDR2 = oldval OP OPARG;
  78. if (oldval CMP CMPARG)
  79. wake UADDR2; }
  80. {$ifndef FPC_USE_LIBC}
  81. function futex_op(op, oparg, cmp, cmparg: cint): cint; {$ifdef SYSTEMINLINE}inline;{$endif}
  82. {$endif}
  83. const
  84. EPOLLIN = $01; { The associated file is available for read(2) operations. }
  85. EPOLLPRI = $02; { There is urgent data available for read(2) operations. }
  86. EPOLLOUT = $04; { The associated file is available for write(2) operations. }
  87. EPOLLERR = $08; { Error condition happened on the associated file descriptor. }
  88. EPOLLHUP = $10; { Hang up happened on the associated file descriptor. }
  89. EPOLLET = $80000000; { Sets the Edge Triggered behaviour for the associated file descriptor. }
  90. { Valid opcodes ( "op" parameter ) to issue to epoll_ctl }
  91. EPOLL_CTL_ADD = 1;
  92. EPOLL_CTL_DEL = 2;
  93. EPOLL_CTL_MOD = 3;
  94. {Some console iotcl's.}
  95. GIO_FONT = $4B60; {gets font in expanded form}
  96. PIO_FONT = $4B61; {use font in expanded form}
  97. GIO_FONTX = $4B6B; {get font using struct consolefontdesc}
  98. PIO_FONTX = $4B6C; {set font using struct consolefontdesc}
  99. PIO_FONTRESET = $4B6D; {reset to default font}
  100. GIO_CMAP = $4B70; {gets colour palette on VGA+}
  101. PIO_CMAP = $4B71; {sets colour palette on VGA+}
  102. KIOCSOUND = $4B2F; {start sound generation (0 for off)}
  103. KDMKTONE = $4B30; {generate tone}
  104. KDGETLED = $4B31; {return current led state}
  105. KDSETLED = $4B32; {set led state [lights, not flags]}
  106. KDGKBTYPE = $4B33; {get keyboard type}
  107. KDADDIO = $4B34; {add i/o port as valid}
  108. KDDELIO = $4B35; {del i/o port as valid}
  109. KDENABIO = $4B36; {enable i/o to video board}
  110. KDDISABIO = $4B37; {disable i/o to video board}
  111. KDSETMODE = $4B3A; {set text/graphics mode}
  112. KDGETMODE = $4B3B; {get current mode}
  113. KDMAPDISP = $4B3C; {map display into address space}
  114. KDUNMAPDISP = $4B3D; {unmap display from address space}
  115. GIO_SCRNMAP = $4B40; {get screen mapping from kernel}
  116. PIO_SCRNMAP = $4B41; {put screen mapping table in kernel}
  117. GIO_UNISCRNMAP = $4B69; {get full Unicode screen mapping}
  118. PIO_UNISCRNMAP = $4B6A; {set full Unicode screen mapping}
  119. GIO_UNIMAP = $4B66; {get unicode-to-font mapping from kernel}
  120. PIO_UNIMAP = $4B67; {put unicode-to-font mapping in kernel}
  121. PIO_UNIMAPCLR = $4B68; {clear table, possibly advise hash algorithm}
  122. KDGKBDIACR = $4B4A; {read kernel accent table}
  123. KDSKBDIACR = $4B4B; {write kernel accent table}
  124. KDGETKEYCODE = $4B4C; {read kernel keycode table entry}
  125. KDSETKEYCODE = $4B4D; {write kernel keycode table entry}
  126. KDSIGACCEPT = $4B4E; {accept kbd generated signals}
  127. KDFONTOP = $4B72; {font operations}
  128. {Keyboard types (for KDGKBTYPE)}
  129. KB_84 = 1;
  130. KB_101 = 2; {Normal PC keyboard.}
  131. KB_OTHER = 3;
  132. {Keyboard LED constants.}
  133. LED_SCR = 1; {scroll lock led}
  134. LED_NUM = 2; {num lock led}
  135. LED_CAP = 4; {caps lock led}
  136. {Tty modes. (for KDSETMODE)}
  137. KD_TEXT = 0;
  138. KD_GRAPHICS = 1;
  139. KD_TEXT0 = 2; {obsolete}
  140. KD_TEXT1 = 3; {obsolete}
  141. MAP_GROWSDOWN = $100; { stack-like segment }
  142. MAP_DENYWRITE = $800; { ETXTBSY }
  143. MAP_EXECUTABLE = $1000; { mark it as an executable }
  144. MAP_LOCKED = $2000; { pages are locked }
  145. MAP_NORESERVE = $4000; { don't check for reservations }
  146. type
  147. TCloneFunc = function(args:pointer):longint;cdecl;
  148. {$ifdef cpui386}
  149. {$define clone_implemented}
  150. {$endif}
  151. {$ifdef cpum68k}
  152. {$define clone_implemented}
  153. {$endif}
  154. {$ifdef clone_implemented}
  155. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  156. {$endif}
  157. const
  158. MODIFY_LDT_CONTENTS_DATA = 0;
  159. MODIFY_LDT_CONTENTS_STACK = 1;
  160. MODIFY_LDT_CONTENTS_CODE = 2;
  161. { Flags for user_desc.flags }
  162. UD_SEG_32BIT = $01;
  163. UD_CONTENTS_DATA = MODIFY_LDT_CONTENTS_DATA shl 1;
  164. UD_CONTENTS_STACK = MODIFY_LDT_CONTENTS_STACK shl 1;
  165. UD_CONTENTS_CODE = MODIFY_LDT_CONTENTS_CODE shl 1;
  166. UD_READ_EXEC_ONLY = $08;
  167. UD_LIMIT_IN_PAGES = $10;
  168. UD_SEG_NOT_PRESENT = $20;
  169. UD_USEABLE = $40;
  170. UD_LM = $80;
  171. type
  172. user_desc = packed record
  173. entry_number : cint;
  174. base_addr : cuint;
  175. limit : cuint;
  176. flags : cuint;
  177. end;
  178. TUser_Desc = user_desc;
  179. PUser_Desc = ^user_desc;
  180. type
  181. EPoll_Data = record
  182. case integer of
  183. 0: (ptr: pointer);
  184. 1: (fd: cint);
  185. 2: (u32: cuint);
  186. 3: (u64: cuint64);
  187. end;
  188. TEPoll_Data = Epoll_Data;
  189. PEPoll_Data = ^Epoll_Data;
  190. EPoll_Event = {$ifdef cpu64} packed {$endif} record
  191. Events: cuint32;
  192. Data : TEpoll_Data;
  193. end;
  194. TEPoll_Event = Epoll_Event;
  195. PEpoll_Event = ^Epoll_Event;
  196. { open an epoll file descriptor }
  197. function epoll_create(size: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_create'; {$endif}
  198. { control interface for an epoll descriptor }
  199. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_ctl'; {$endif}
  200. { wait for an I/O event on an epoll file descriptor }
  201. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_wait'; {$endif}
  202. type Puser_cap_header=^user_cap_header;
  203. user_cap_header=packed record
  204. version,pid:cardinal;
  205. end;
  206. Puser_cap_data=^user_cap_data;
  207. user_cap_data=packed record
  208. effective,permitted,inheritable:cardinal;
  209. end;
  210. {Get a capability.}
  211. function capget(header:Puser_cap_header;data:Puser_cap_data):cint;{$ifdef FPC_USE_LIBC} cdecl; external name 'capget'; {$endif}
  212. {Set a capability.}
  213. function capset(header:Puser_cap_header;data:Puser_cap_data):cint;{$ifdef FPC_USE_LIBC} cdecl; external name 'capset'; {$endif}
  214. const CAP_CHOWN = 0;
  215. CAP_DAC_OVERRIDE = 1;
  216. CAP_DAC_READ_SEARCH = 2;
  217. CAP_FOWNER = 3;
  218. CAP_FSETID = 4;
  219. CAP_FS_MASK = $1f;
  220. CAP_KILL = 5;
  221. CAP_SETGID = 6;
  222. CAP_SETUID = 7;
  223. CAP_SETPCAP = 8;
  224. CAP_LINUX_IMMUTABLE = 9;
  225. CAP_NET_BIND_SERVICE = 10;
  226. CAP_NET_BROADCAST = 11;
  227. CAP_NET_ADMIN = 12;
  228. CAP_NET_RAW = 13;
  229. CAP_IPC_LOCK = 14;
  230. CAP_IPC_OWNER = 15;
  231. CAP_SYS_MODULE = 16;
  232. CAP_SYS_RAWIO = 17;
  233. CAP_SYS_CHROOT = 18;
  234. CAP_SYS_PTRACE = 19;
  235. CAP_SYS_PACCT = 20;
  236. CAP_SYS_ADMIN = 21;
  237. CAP_SYS_BOOT = 22;
  238. CAP_SYS_NICE = 23;
  239. CAP_SYS_RESOURCE = 24;
  240. CAP_SYS_TIME = 25;
  241. CAP_SYS_TTY_CONFIG = 26;
  242. CAP_MKNOD = 27;
  243. CAP_LEASE = 28;
  244. CAP_AUDIT_WRITE = 29;
  245. CAP_AUDIT_CONTROL = 30;
  246. LINUX_CAPABILITY_VERSION = $19980330;
  247. implementation
  248. {$ifndef FPC_USE_LIBC}
  249. Uses Syscall;
  250. Function Sysinfo(var Info:TSysinfo):Boolean;
  251. {
  252. Get system info
  253. }
  254. Begin
  255. Sysinfo:=do_SysCall(SysCall_nr_Sysinfo,TSysParam(@info))=0;
  256. End;
  257. {$ifdef clone_implemented}
  258. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  259. begin
  260. if (pointer(func)=nil) or (sp=nil) then
  261. exit(-1); // give an error result
  262. {$ifdef cpui386}
  263. {$ASMMODE ATT}
  264. asm
  265. { Insert the argument onto the new stack. }
  266. movl sp,%ecx
  267. subl $8,%ecx
  268. movl args,%eax
  269. movl %eax,4(%ecx)
  270. { Save the function pointer as the zeroth argument.
  271. It will be popped off in the child in the ebx frobbing below. }
  272. movl func,%eax
  273. movl %eax,0(%ecx)
  274. { Do the system call }
  275. pushl %ebx
  276. movl flags,%ebx
  277. movl SysCall_nr_clone,%eax
  278. int $0x80
  279. popl %ebx
  280. test %eax,%eax
  281. jnz .Lclone_end
  282. { We're in the new thread }
  283. subl %ebp,%ebp { terminate the stack frame }
  284. call *%ebx
  285. { exit process }
  286. movl %eax,%ebx
  287. movl $1,%eax
  288. int $0x80
  289. .Lclone_end:
  290. movl %eax,__RESULT
  291. end;
  292. {$endif cpui386}
  293. {$ifdef cpum68k}
  294. { No yet translated, my m68k assembler is too weak for such things PM }
  295. (*
  296. asm
  297. { Insert the argument onto the new stack. }
  298. movl sp,%ecx
  299. subl $8,%ecx
  300. movl args,%eax
  301. movl %eax,4(%ecx)
  302. { Save the function pointer as the zeroth argument.
  303. It will be popped off in the child in the ebx frobbing below. }
  304. movl func,%eax
  305. movl %eax,0(%ecx)
  306. { Do the system call }
  307. pushl %ebx
  308. movl flags,%ebx
  309. movl SysCall_nr_clone,%eax
  310. int $0x80
  311. popl %ebx
  312. test %eax,%eax
  313. jnz .Lclone_end
  314. { We're in the new thread }
  315. subl %ebp,%ebp { terminate the stack frame }
  316. call *%ebx
  317. { exit process }
  318. movl %eax,%ebx
  319. movl $1,%eax
  320. int $0x80
  321. .Lclone_end:
  322. movl %eax,__RESULT
  323. end;
  324. *)
  325. {$endif cpum68k}
  326. end;
  327. {$endif}
  328. function epoll_create(size: cint): cint;
  329. begin
  330. epoll_create := do_syscall(syscall_nr_epoll_create,tsysparam(size));
  331. end;
  332. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint;
  333. begin
  334. epoll_ctl := do_syscall(syscall_nr_epoll_ctl, tsysparam(epfd),
  335. tsysparam(op), tsysparam(fd), tsysparam(event));
  336. end;
  337. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint;
  338. begin
  339. epoll_wait := do_syscall(syscall_nr_epoll_wait, tsysparam(epfd),
  340. tsysparam(events), tsysparam(maxevents), tsysparam(timeout));
  341. end;
  342. function capget(header:Puser_cap_header;data:Puser_cap_data):cint;
  343. begin
  344. capget:=do_syscall(syscall_nr_capget,Tsysparam(header),Tsysparam(data));
  345. end;
  346. function capset(header:Puser_cap_header;data:Puser_cap_data):cint;
  347. begin
  348. capset:=do_syscall(syscall_nr_capset,Tsysparam(header),Tsysparam(data));
  349. end;
  350. {$endif}
  351. { FUTEX_OP is a macro, doesn't exist in libC as function}
  352. function FUTEX_OP(op, oparg, cmp, cmparg: cint): cint; {$ifdef SYSTEMINLINE}inline;{$endif}
  353. begin
  354. FUTEX_OP := ((op and $F) shl 28) or ((cmp and $F) shl 24) or ((oparg and $FFF) shl 12) or (cmparg and $FFF);
  355. end;
  356. end.