linux.pp 8.2 KB

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