linuxnew.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. interface
  16. Type
  17. TSysinfo = packed record
  18. uptime : longint;
  19. loads : array[1..3] of longint;
  20. totalram,
  21. freeram,
  22. sharedram,
  23. bufferram,
  24. totalswap,
  25. freeswap : longint;
  26. procs : integer;
  27. s : string[18];
  28. end;
  29. PSysInfo = ^TSysInfo;
  30. Function Sysinfo(var Info:TSysinfo):Boolean; {$ifdef FPC_USE_LIBC} cdecl; external name 'sysinfo'; {$endif}
  31. Const
  32. CSIGNAL = $000000ff; // signal mask to be sent at exit
  33. CLONE_VM = $00000100; // set if VM shared between processes
  34. CLONE_FS = $00000200; // set if fs info shared between processes
  35. CLONE_FILES = $00000400; // set if open files shared between processes
  36. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  37. CLONE_PID = $00001000; // set if pid shared
  38. type
  39. TCloneFunc=function(args:pointer):longint;cdecl;
  40. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint; {$ifdef FPC_USE_LIBC} cdecl; external name 'clone'; {$endif}
  41. implementation
  42. {$ifndef FPC_USE_LIBC}
  43. Uses Syscall;
  44. Function Sysinfo(var Info:TSysinfo):Boolean;
  45. {
  46. Get system info
  47. }
  48. Begin
  49. Sysinfo:=do_SysCall(SysCall_nr_Sysinfo,TSysParam(@info))=0;
  50. End;
  51. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  52. begin
  53. if (pointer(func)=nil) or (sp=nil) then
  54. exit(-1); // give an error result
  55. {$ifdef cpui386}
  56. {$ASMMODE ATT}
  57. asm
  58. { Insert the argument onto the new stack. }
  59. movl sp,%ecx
  60. subl $8,%ecx
  61. movl args,%eax
  62. movl %eax,4(%ecx)
  63. { Save the function pointer as the zeroth argument.
  64. It will be popped off in the child in the ebx frobbing below. }
  65. movl func,%eax
  66. movl %eax,0(%ecx)
  67. { Do the system call }
  68. pushl %ebx
  69. movl flags,%ebx
  70. movl SysCall_nr_clone,%eax
  71. int $0x80
  72. popl %ebx
  73. test %eax,%eax
  74. jnz .Lclone_end
  75. { We're in the new thread }
  76. subl %ebp,%ebp { terminate the stack frame }
  77. call *%ebx
  78. { exit process }
  79. movl %eax,%ebx
  80. movl $1,%eax
  81. int $0x80
  82. .Lclone_end:
  83. movl %eax,__RESULT
  84. end;
  85. {$endif cpui386}
  86. {$ifdef cpum68k}
  87. { No yet translated, my m68k assembler is too weak for such things PM }
  88. (*
  89. asm
  90. { Insert the argument onto the new stack. }
  91. movl sp,%ecx
  92. subl $8,%ecx
  93. movl args,%eax
  94. movl %eax,4(%ecx)
  95. { Save the function pointer as the zeroth argument.
  96. It will be popped off in the child in the ebx frobbing below. }
  97. movl func,%eax
  98. movl %eax,0(%ecx)
  99. { Do the system call }
  100. pushl %ebx
  101. movl flags,%ebx
  102. movl SysCall_nr_clone,%eax
  103. int $0x80
  104. popl %ebx
  105. test %eax,%eax
  106. jnz .Lclone_end
  107. { We're in the new thread }
  108. subl %ebp,%ebp { terminate the stack frame }
  109. call *%ebx
  110. { exit process }
  111. movl %eax,%ebx
  112. movl $1,%eax
  113. int $0x80
  114. .Lclone_end:
  115. movl %eax,__RESULT
  116. end;
  117. *)
  118. {$endif cpum68k}
  119. end;
  120. {$endif}
  121. end.