linux.pp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. EPOLLIN = $01; { The associated file is available for read(2) operations. }
  55. EPOLLPRI = $02; { There is urgent data available for read(2) operations. }
  56. EPOLLOUT = $04; { The associated file is available for write(2) operations. }
  57. EPOLLERR = $08; { Error condition happened on the associated file descriptor. }
  58. EPOLLHUP = $10; { Hang up happened on the associated file descriptor. }
  59. EPOLLET = $80000000; { Sets the Edge Triggered behaviour for the associated file descriptor. }
  60. { Valid opcodes ( "op" parameter ) to issue to epoll_ctl }
  61. EPOLL_CTL_ADD = 1;
  62. EPOLL_CTL_DEL = 2;
  63. EPOLL_CTL_MOD = 3;
  64. {Some console iotcl's.}
  65. GIO_FONT = $4B60; {gets font in expanded form}
  66. PIO_FONT = $4B61; {use font in expanded form}
  67. GIO_FONTX = $4B6B; {get font using struct consolefontdesc}
  68. PIO_FONTX = $4B6C; {set font using struct consolefontdesc}
  69. PIO_FONTRESET = $4B6D; {reset to default font}
  70. GIO_CMAP = $4B70; {gets colour palette on VGA+}
  71. PIO_CMAP = $4B71; {sets colour palette on VGA+}
  72. KIOCSOUND = $4B2F; {start sound generation (0 for off)}
  73. KDMKTONE = $4B30; {generate tone}
  74. KDGETLED = $4B31; {return current led state}
  75. KDSETLED = $4B32; {set led state [lights, not flags]}
  76. KDGKBTYPE = $4B33; {get keyboard type}
  77. KDADDIO = $4B34; {add i/o port as valid}
  78. KDDELIO = $4B35; {del i/o port as valid}
  79. KDENABIO = $4B36; {enable i/o to video board}
  80. KDDISABIO = $4B37; {disable i/o to video board}
  81. KDSETMODE = $4B3A; {set text/graphics mode}
  82. KDGETMODE = $4B3B; {get current mode}
  83. KDMAPDISP = $4B3C; {map display into address space}
  84. KDUNMAPDISP = $4B3D; {unmap display from address space}
  85. GIO_SCRNMAP = $4B40; {get screen mapping from kernel}
  86. PIO_SCRNMAP = $4B41; {put screen mapping table in kernel}
  87. GIO_UNISCRNMAP = $4B69; {get full Unicode screen mapping}
  88. PIO_UNISCRNMAP = $4B6A; {set full Unicode screen mapping}
  89. GIO_UNIMAP = $4B66; {get unicode-to-font mapping from kernel}
  90. PIO_UNIMAP = $4B67; {put unicode-to-font mapping in kernel}
  91. PIO_UNIMAPCLR = $4B68; {clear table, possibly advise hash algorithm}
  92. KDGKBDIACR = $4B4A; {read kernel accent table}
  93. KDSKBDIACR = $4B4B; {write kernel accent table}
  94. KDGETKEYCODE = $4B4C; {read kernel keycode table entry}
  95. KDSETKEYCODE = $4B4D; {write kernel keycode table entry}
  96. KDSIGACCEPT = $4B4E; {accept kbd generated signals}
  97. KDFONTOP = $4B72; {font operations}
  98. {Keyboard types (for KDGKBTYPE)}
  99. KB_84 = 1;
  100. KB_101 = 2; {Normal PC keyboard.}
  101. KB_OTHER = 3;
  102. {Keyboard LED constants.}
  103. LED_SCR = 1; {scroll lock led}
  104. LED_NUM = 2; {num lock led}
  105. LED_CAP = 4; {caps lock led}
  106. {Tty modes. (for KDSETMODE)}
  107. KD_TEXT = 0;
  108. KD_GRAPHICS = 1;
  109. KD_TEXT0 = 2; {obsolete}
  110. KD_TEXT1 = 3; {obsolete}
  111. type
  112. TCloneFunc=function(args:pointer):longint;cdecl;
  113. EPoll_Data = record
  114. case integer of
  115. 0: (ptr: pointer);
  116. 1: (fd: cint);
  117. 2: (u32: cuint);
  118. 3: (u64: cuint64);
  119. end;
  120. TEPoll_Data = Epoll_Data;
  121. PEPoll_Data = ^Epoll_Data;
  122. EPoll_Event = {$ifdef cpu64} packed {$endif} record
  123. Events: cuint32;
  124. Data : TEpoll_Data;
  125. end;
  126. TEPoll_Event = Epoll_Event;
  127. PEpoll_Event = ^Epoll_Event;
  128. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  129. { open an epoll file descriptor }
  130. function epoll_create(size: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_create'; {$endif}
  131. { control interface for an epoll descriptor }
  132. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_ctl'; {$endif}
  133. { wait for an I/O event on an epoll file descriptor }
  134. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint; {$ifdef FPC_USE_LIBC} cdecl; external name 'epoll_wait'; {$endif}
  135. implementation
  136. {$ifndef FPC_USE_LIBC}
  137. Uses Syscall;
  138. Function Sysinfo(var Info:TSysinfo):Boolean;
  139. {
  140. Get system info
  141. }
  142. Begin
  143. Sysinfo:=do_SysCall(SysCall_nr_Sysinfo,TSysParam(@info))=0;
  144. End;
  145. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  146. begin
  147. if (pointer(func)=nil) or (sp=nil) then
  148. exit(-1); // give an error result
  149. {$ifdef cpui386}
  150. {$ASMMODE ATT}
  151. asm
  152. { Insert the argument onto the new stack. }
  153. movl sp,%ecx
  154. subl $8,%ecx
  155. movl args,%eax
  156. movl %eax,4(%ecx)
  157. { Save the function pointer as the zeroth argument.
  158. It will be popped off in the child in the ebx frobbing below. }
  159. movl func,%eax
  160. movl %eax,0(%ecx)
  161. { Do the system call }
  162. pushl %ebx
  163. movl flags,%ebx
  164. movl SysCall_nr_clone,%eax
  165. int $0x80
  166. popl %ebx
  167. test %eax,%eax
  168. jnz .Lclone_end
  169. { We're in the new thread }
  170. subl %ebp,%ebp { terminate the stack frame }
  171. call *%ebx
  172. { exit process }
  173. movl %eax,%ebx
  174. movl $1,%eax
  175. int $0x80
  176. .Lclone_end:
  177. movl %eax,__RESULT
  178. end;
  179. {$endif cpui386}
  180. {$ifdef cpum68k}
  181. { No yet translated, my m68k assembler is too weak for such things PM }
  182. (*
  183. asm
  184. { Insert the argument onto the new stack. }
  185. movl sp,%ecx
  186. subl $8,%ecx
  187. movl args,%eax
  188. movl %eax,4(%ecx)
  189. { Save the function pointer as the zeroth argument.
  190. It will be popped off in the child in the ebx frobbing below. }
  191. movl func,%eax
  192. movl %eax,0(%ecx)
  193. { Do the system call }
  194. pushl %ebx
  195. movl flags,%ebx
  196. movl SysCall_nr_clone,%eax
  197. int $0x80
  198. popl %ebx
  199. test %eax,%eax
  200. jnz .Lclone_end
  201. { We're in the new thread }
  202. subl %ebp,%ebp { terminate the stack frame }
  203. call *%ebx
  204. { exit process }
  205. movl %eax,%ebx
  206. movl $1,%eax
  207. int $0x80
  208. .Lclone_end:
  209. movl %eax,__RESULT
  210. end;
  211. *)
  212. {$endif cpum68k}
  213. end;
  214. function epoll_create(size: cint): cint;
  215. begin
  216. epoll_create := do_syscall(syscall_nr_epoll_create,tsysparam(size));
  217. end;
  218. function epoll_ctl(epfd, op, fd: cint; event: pepoll_event): cint;
  219. begin
  220. epoll_ctl := do_syscall(syscall_nr_epoll_ctl, tsysparam(epfd),
  221. tsysparam(op), tsysparam(fd), tsysparam(event));
  222. end;
  223. function epoll_wait(epfd: cint; events: pepoll_event; maxevents, timeout: cint): cint;
  224. begin
  225. epoll_wait := do_syscall(syscall_nr_epoll_wait, tsysparam(epfd),
  226. tsysparam(events), tsysparam(maxevents), tsysparam(timeout));
  227. end;
  228. {$endif}
  229. end.