syscall.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  6. The syscalls for the new RTL, moved to platform dependant dir.
  7. Old linux calling convention is stil kept.
  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. {$ASMMODE GAS}
  15. function FpSysCall(sysnr:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL0'];
  16. asm
  17. movq sysnr, %rax { Syscall number -> rax. }
  18. movq %rsi, %rdi { shift arg1 - arg5. }
  19. movq %rdx, %rsi
  20. movq %rcx, %rdx
  21. movq %r8, %r10
  22. movq %r9, %r8
  23. syscall { Do the system call. }
  24. cmpq $-4095, %rax { Check %rax for error. }
  25. jnae .LSyscOK { Jump to error handler if error. }
  26. negq %rax
  27. movq %rax,%rdx
  28. movq FPC_THREADVAR_RELOCATE,%rax
  29. testq %rax,%rax
  30. jne .LThread
  31. movq %rdx,Errno+4
  32. jmp .LNoThread
  33. .LThread:
  34. pushq %rdx
  35. pushq Errno
  36. call *%rax
  37. popq %rdx
  38. movq %rdx,(%rax)
  39. .LNoThread:
  40. movq $-1,%rax
  41. .LSyscOK:
  42. end;
  43. function FpSysCall(sysnr,param1 : TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL1'];
  44. asm
  45. movq sysnr, %rax { Syscall number -> rax. }
  46. movq param1, %rdi { shift arg1 - arg5. }
  47. syscall { Do the system call. }
  48. cmpq $-4095, %rax { Check %rax for error. }
  49. jnae .LSyscOK { Jump to error handler if error. }
  50. negq %rax
  51. movq %rax,%rdx
  52. movq FPC_THREADVAR_RELOCATE,%rax
  53. testq %rax,%rax
  54. jne .LThread
  55. movq %rdx,Errno+4
  56. jmp .LNoThread
  57. .LThread:
  58. pushq %rdx
  59. pushq Errno
  60. call *%rax
  61. popq %rdx
  62. movq %rdx,(%rax)
  63. .LNoThread:
  64. movq $-1,%rax
  65. .LSyscOK:
  66. end;
  67. function FpSysCall(sysnr,param1,param2 : TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL2'];
  68. asm
  69. movq sysnr, %rax { Syscall number -> rax. }
  70. movq param1, %rdi { shift arg1 - arg5. }
  71. movq param2, %rsi
  72. syscall { Do the system call. }
  73. cmpq $-4095, %rax { Check %rax for error. }
  74. jnae .LSyscOK { Jump to error handler if error. }
  75. negq %rax
  76. movq %rax,%rdx
  77. movq FPC_THREADVAR_RELOCATE,%rax
  78. testq %rax,%rax
  79. jne .LThread
  80. movq %rdx,Errno+4
  81. jmp .LNoThread
  82. .LThread:
  83. pushq %rdx
  84. pushq Errno
  85. call *%rax
  86. popq %rdx
  87. movq %rdx,(%rax)
  88. .LNoThread:
  89. movq $-1,%rax
  90. .LSyscOK:
  91. end;
  92. function FpSysCall(sysnr,param1,param2,param3:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL3'];
  93. asm
  94. movq sysnr, %rax { Syscall number -> rax. }
  95. movq param1, %rdi { shift arg1 - arg5. }
  96. movq param2, %rsi
  97. movq param3, %rdx
  98. syscall { Do the system call. }
  99. cmpq $-4095, %rax { Check %rax for error. }
  100. jnae .LSyscOK { Jump to error handler if error. }
  101. negq %rax
  102. movq %rax,%rdx
  103. movq FPC_THREADVAR_RELOCATE,%rax
  104. testq %rax,%rax
  105. jne .LThread
  106. movq %rdx,Errno+4
  107. jmp .LNoThread
  108. .LThread:
  109. pushq %rdx
  110. pushq Errno
  111. call *%rax
  112. popq %rdx
  113. movq %rdx,(%rax)
  114. .LNoThread:
  115. movq $-1,%rax
  116. .LSyscOK:
  117. end;
  118. function FpSysCall(sysnr,param1,param2,param3,param4:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL4'];
  119. asm
  120. movq sysnr, %rax { Syscall number -> rax. }
  121. movq param1, %rdi { shift arg1 - arg5. }
  122. movq param2, %rsi
  123. movq param3, %rdx
  124. movq param4, %r10
  125. syscall { Do the system call. }
  126. cmpq $-4095, %rax { Check %rax for error. }
  127. jnae .LSyscOK { Jump to error handler if error. }
  128. negq %rax
  129. movq %rax,%rdx
  130. movq FPC_THREADVAR_RELOCATE,%rax
  131. testq %rax,%rax
  132. jne .LThread
  133. movq %rdx,Errno+4
  134. jmp .LNoThread
  135. .LThread:
  136. pushq %rdx
  137. pushq Errno
  138. call *%rax
  139. popq %rdx
  140. movq %rdx,(%rax)
  141. .LNoThread:
  142. movq $-1,%rax
  143. .LSyscOK:
  144. end;
  145. function FpSysCall(sysnr,param1,param2,param3,param4,param5 : TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL5'];
  146. asm
  147. movq sysnr, %rax { Syscall number -> rax. }
  148. movq param1, %rdi { shift arg1 - arg5. }
  149. movq param2, %rsi
  150. movq param3, %rdx
  151. movq param4, %r10
  152. movq param5, %r8
  153. syscall { Do the system call. }
  154. cmpq $-4095, %rax { Check %rax for error. }
  155. jnae .LSyscOK { Jump to error handler if error. }
  156. negq %rax
  157. movq %rax,%rdx
  158. movq FPC_THREADVAR_RELOCATE,%rax
  159. testq %rax,%rax
  160. jne .LThread
  161. movq %rdx,Errno+4
  162. jmp .LNoThread
  163. .LThread:
  164. pushq %rdx
  165. pushq Errno
  166. call *%rax
  167. popq %rdx
  168. movq %rdx,(%rax)
  169. .LNoThread:
  170. movq $-1,%rax
  171. .LSyscOK:
  172. end;
  173. {$ifdef notsupported}
  174. { Only 5 params are pushed, so it'll not work as expected (PFV) }
  175. function FpSysCall(sysnr,param1,param2,param3,param4,param5,param6 : TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL6'];
  176. asm
  177. { load the registers... }
  178. movl sysnr,%eax
  179. movl param1,%ebx
  180. movl param2,%ecx
  181. movl param3,%edx
  182. movl param4,%esi
  183. movl param5,%edi
  184. int $0x80
  185. testl %eax,%eax
  186. jns .LSyscOK
  187. negl %eax
  188. {$ifdef VER1_0}
  189. movl %eax,Errno
  190. {$else}
  191. movl %eax,%edx
  192. movl FPC_THREADVAR_RELOCATE,%eax
  193. testl %eax,%eax
  194. jne .LThread
  195. movl %edx,Errno+4
  196. jmp .LNoThread
  197. .LThread:
  198. pushl %edx
  199. pushl Errno
  200. call *%eax
  201. popl %edx
  202. movl %edx,(%eax)
  203. .LNoThread:
  204. movl $-1,%eax
  205. {$endif}
  206. .LSyscOK:
  207. end;
  208. {$endif notsupported}
  209. {No debugging for syslinux include !}
  210. {$IFDEF SYS_LINUX}
  211. {$UNDEF SYSCALL_DEBUG}
  212. {$ENDIF SYS_LINUX}
  213. {*****************************************************************************
  214. --- Main:The System Call Self ---
  215. *****************************************************************************}
  216. Procedure FpSysCall( callnr:TSysParam;var regs : SysCallregs );assembler;
  217. {
  218. This function puts the registers in place, does the call, and then
  219. copies back the registers as they are after the SysCall.
  220. }
  221. {$define fpc_syscall_ok}
  222. asm
  223. pushq %rdi
  224. movq sysnr, %rax { Syscall number -> rax. }
  225. movq 8(%rdi),%rsi { load paras }
  226. movq 16(%rdi),%rdx
  227. movq 24(%rdi),%r10
  228. movq 32(%rdi),%r8
  229. movq (%rdi),%rdi
  230. syscall { Do the system call. }
  231. popq %rdi
  232. movq %rax,(%rdi)
  233. end;
  234. {$ASMMODE DEFAULT}
  235. Function SysCall( callnr:longint;var regs : SysCallregs ):longint;
  236. {
  237. This function serves as an interface to do_SysCall.
  238. If the SysCall returned a negative number, it returns -1, and puts the
  239. SysCall result in errno. Otherwise, it returns the SysCall return value
  240. }
  241. begin
  242. FpSysCall(callnr,regs);
  243. if regs.reg1>=$fffffffffffff001 then
  244. begin
  245. {$IFDEF SYSCALL_DEBUG}
  246. If DoSysCallDebug then
  247. debugtxt:=' syscall error: ';
  248. {$endif}
  249. setErrNo(-regs.reg1);
  250. SysCall:=-1;
  251. end
  252. else
  253. begin
  254. {$IFDEF SYSCALL_DEBUG}
  255. if DoSysCallDebug then
  256. debugtxt:=' syscall returned: ';
  257. {$endif}
  258. SysCall:=regs.reg1;
  259. seterrno(0);
  260. end;
  261. {$IFDEF SYSCALL_DEBUG}
  262. if DoSysCallDebug then
  263. begin
  264. inc(lastcnt);
  265. if (callnr<>lastcall) or (regs.reg1<>lasteax) then
  266. begin
  267. if lastcnt>1 then
  268. writeln(sys_nr_txt[lastcall],debugtxt,lasteax,' (',lastcnt,'x)');
  269. lastcall:=callnr;
  270. lasteax:=regs.reg1;
  271. lastcnt:=0;
  272. writeln(sys_nr_txt[lastcall],debugtxt,lasteax);
  273. end;
  274. end;
  275. {$endif}
  276. end;
  277. {
  278. $Log$
  279. Revision 1.3 2004-02-06 15:58:21 florian
  280. * fixed x86-64 assembler problems
  281. Revision 1.2 2004/02/05 01:16:12 florian
  282. + completed x86-64/linux system unit
  283. Revision 1.1 2003/04/30 22:11:06 florian
  284. + for a lot of x86-64 dependend files mostly dummies added
  285. }