syscall.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2004 Marco van de Voort
  4. member of the Free Pascal development team.
  5. The syscalls for the *BSD AMD64 rtl
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. Origin of this file: copied from linux/x86_64 dir, blended with the
  14. freebsd x86 changes and checked against objdump of a
  15. x86_64/freebsdprog
  16. - jge directly behind the syscall to branch on non-error
  17. - rcx is used as scratch reg (fpc/Linux-x86_64 uses edx)
  18. - More 6 and 7 param dosyscall because of the __syscall problem
  19. }
  20. {$ASMMODE GAS}
  21. function fpsysCall(sysnr:TSysParam):TSysResult; assembler;[public,alias:'FPC_DOSYS0'];
  22. asm
  23. movq sysnr, %rax { Syscall number -> rax. }
  24. syscall { Do the system call. }
  25. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  26. movq %rax,%rdx
  27. {$ifdef FPC_PIC}
  28. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  29. movq (%rax),%rax
  30. movq Errno@GOTPCREL(%rip),%r11
  31. {$else FPC_PIC}
  32. movq fpc_threadvar_relocate_proc,%rax
  33. leaq Errno,%r11
  34. {$endif FPC_PIC}
  35. testq %rax,%rax
  36. jne .LThread
  37. movl %edx,8(%r11)
  38. jmp .LNoThread
  39. .LThread:
  40. pushq %rdx
  41. movq (%r11),%rdi
  42. call *%rax
  43. popq %rdx
  44. movl %edx,(%rax)
  45. .LNoThread:
  46. movq $-1,%rax
  47. .LSyscOK:
  48. end;
  49. function fpsysCall(sysnr,param1 : TSysParam):TSysResult; assembler;[public,alias:'FPC_DOSYS1'];
  50. asm
  51. movq sysnr, %rax { Syscall number -> rax. }
  52. movq param1, %rdi { shift arg1 - arg1. }
  53. syscall { Do the system call. }
  54. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  55. movq %rax,%rdx
  56. {$ifdef FPC_PIC}
  57. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  58. movq (%rax),%rax
  59. movq Errno@GOTPCREL(%rip),%r11
  60. {$else FPC_PIC}
  61. movq fpc_threadvar_relocate_proc,%rax
  62. leaq Errno,%r11
  63. {$endif FPC_PIC}
  64. testq %rax,%rax
  65. jne .LThread
  66. movl %edx,8(%r11)
  67. jmp .LNoThread
  68. .LThread:
  69. pushq %rdx
  70. movq (%r11),%rdi
  71. call *%rax
  72. popq %rdx
  73. movl %edx,(%rax)
  74. .LNoThread:
  75. movq $-1,%rax
  76. .LSyscOK:
  77. end;
  78. function fpsysCall(sysnr,param1,param2 : TSysParam):TSysResult; assembler;[public,alias:'FPC_DOSYS2'];
  79. asm
  80. movq sysnr, %rax { Syscall number -> rax. }
  81. movq param1, %rdi { shift arg1 - arg2. }
  82. movq param2, %rsi
  83. syscall { Do the system call. }
  84. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  85. movq %rax,%rdx
  86. {$ifdef FPC_PIC}
  87. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  88. movq (%rax),%rax
  89. movq Errno@GOTPCREL(%rip),%r11
  90. {$else FPC_PIC}
  91. movq fpc_threadvar_relocate_proc,%rax
  92. leaq Errno,%r11
  93. {$endif FPC_PIC}
  94. testq %rax,%rax
  95. jne .LThread
  96. movl %edx,8(%r11)
  97. jmp .LNoThread
  98. .LThread:
  99. pushq %rdx
  100. movq (%r11),%rdi
  101. call *%rax
  102. popq %rdx
  103. movl %edx,(%rax)
  104. .LNoThread:
  105. movq $-1,%rax
  106. .LSyscOK:
  107. end;
  108. function fpsysCall(sysnr,param1,param2,param3:TSysParam):TSysResult; assembler;[public,alias:'FPC_DOSYS3'];
  109. asm
  110. movq sysnr, %rax { Syscall number -> rax. }
  111. movq param1, %rdi { shift arg1 - arg3. }
  112. movq param2, %rsi
  113. movq param3, %rdx
  114. syscall { Do the system call. }
  115. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  116. movq %rax,%rdx
  117. {$ifdef FPC_PIC}
  118. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  119. movq (%rax),%rax
  120. movq Errno@GOTPCREL(%rip),%r11
  121. {$else FPC_PIC}
  122. movq fpc_threadvar_relocate_proc,%rax
  123. leaq Errno,%r11
  124. {$endif FPC_PIC}
  125. testq %rax,%rax
  126. jne .LThread
  127. movl %edx,8(%r11)
  128. jmp .LNoThread
  129. .LThread:
  130. pushq %rdx
  131. movq (%r11),%rdi
  132. call *%rax
  133. popq %rdx
  134. movl %edx,(%rax)
  135. .LNoThread:
  136. movq $-1,%rax
  137. .LSyscOK:
  138. end;
  139. function fpsysCall(sysnr,param1,param2,param3,param4:TSysParam):TSysResult; assembler;[public,alias:'FPC_DOSYS4'];
  140. asm
  141. movq sysnr, %rax { Syscall number -> rax. }
  142. movq param1, %rdi { shift arg1 - arg5. }
  143. movq param2, %rsi
  144. movq param3, %rdx
  145. movq param4, %r10
  146. syscall { Do the system call. }
  147. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  148. movq %rax,%rdx
  149. {$ifdef FPC_PIC}
  150. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  151. movq (%rax),%rax
  152. movq Errno@GOTPCREL(%rip),%r11
  153. {$else FPC_PIC}
  154. movq fpc_threadvar_relocate_proc,%rax
  155. leaq Errno,%r11
  156. {$endif FPC_PIC}
  157. testq %rax,%rax
  158. jne .LThread
  159. movl %edx,8(%r11)
  160. jmp .LNoThread
  161. .LThread:
  162. pushq %rdx
  163. movq (%r11),%rdi
  164. call *%rax
  165. popq %rdx
  166. movl %edx,(%rax)
  167. .LNoThread:
  168. movq $-1,%rax
  169. .LSyscOK:
  170. end;
  171. function fpsysCall(sysnr,param1,param2,param3,param4,param5 : TSysParam):TSysResult; assembler;[public,alias:'FPC_DOSYS5'];
  172. asm
  173. movq sysnr, %rax { Syscall number -> rax. }
  174. movq param1, %rdi { shift arg1 - arg5. }
  175. movq param2, %rsi
  176. movq param3, %rdx
  177. movq param4, %r10
  178. movq param5, %r8
  179. syscall { Do the system call. }
  180. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  181. movq %rax,%rdx
  182. {$ifdef FPC_PIC}
  183. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  184. movq (%rax),%rax
  185. movq Errno@GOTPCREL(%rip),%r11
  186. {$else FPC_PIC}
  187. movq fpc_threadvar_relocate_proc,%rax
  188. leaq Errno,%r11
  189. {$endif FPC_PIC}
  190. testq %rax,%rax
  191. jne .LThread
  192. movl %edx,8(%r11)
  193. jmp .LNoThread
  194. .LThread:
  195. pushq %rdx
  196. movq (%r11),%rdi
  197. call *%rax
  198. popq %rdx
  199. movl %edx,(%rax)
  200. .LNoThread:
  201. movq $-1,%rax
  202. .LSyscOK:
  203. end;
  204. function fpsysCall(sysnr,param1,param2,param3,param4,param5,param6 : TSysParam):TSysResult; assembler;[public,alias:'FPC_DOSYS6'];
  205. asm
  206. movq sysnr, %rax { Syscall number -> rax. }
  207. movq param1, %rdi { shift arg1 - arg6. }
  208. movq param2, %rsi
  209. movq param3, %rdx
  210. movq param4, %r10
  211. movq param5, %r8
  212. movq param6, %r9
  213. syscall { Do the system call. }
  214. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  215. movq %rax,%rdx
  216. {$ifdef FPC_PIC}
  217. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  218. movq (%rax),%rax
  219. movq Errno@GOTPCREL(%rip),%r11
  220. {$else FPC_PIC}
  221. movq fpc_threadvar_relocate_proc,%rax
  222. leaq Errno,%r11
  223. {$endif FPC_PIC}
  224. testq %rax,%rax
  225. jne .LThread
  226. movl %edx,8(%r11)
  227. jmp .LNoThread
  228. .LThread:
  229. pushq %rdx
  230. movq (%r11),%rdi
  231. call *%rax
  232. popq %rdx
  233. movl %edx,(%rax)
  234. .LNoThread:
  235. movq $-1,%rax
  236. .LSyscOK:
  237. end;
  238. // edi esi edx ecx r8 r9 stack
  239. function fp_sysCall(sysnr,param1,param2,param3,param4,param5,param6 : TSysParam):TSysResult; assembler;[public,alias:'FPC__DOSYS'];
  240. asm
  241. sub $0x18,%rsp
  242. movq param6,%rax // from caller stack to mine.
  243. movq %rax,0x8(%rsp)
  244. movl $0, 0(%rsp) // dummy or caller frame because ?
  245. mov $0xc6,%rax // __syscall
  246. mov %rcx,%r10
  247. syscall { Do the system call. }
  248. jge .LSyscOK { branch to exit if ok, errorhandler otherwise}
  249. movq %rax,%rdx
  250. {$ifdef FPC_PIC}
  251. movq fpc_threadvar_relocate_proc@GOTPCREL(%rip),%rax
  252. movq (%rax),%rax
  253. movq Errno@GOTPCREL(%rip),%r11
  254. {$else FPC_PIC}
  255. movq fpc_threadvar_relocate_proc,%rax
  256. leaq Errno,%r11
  257. {$endif FPC_PIC}
  258. testq %rax,%rax
  259. jne .LThread
  260. movl %edx,8(%r11)
  261. jmp .LNoThread
  262. .LThread:
  263. pushq %rdx
  264. movq (%r11),%rdi
  265. call *%rax
  266. popq %rdx
  267. movl %edx,(%rax)
  268. .LNoThread:
  269. movq $-1,%rax
  270. .LSyscOK:
  271. add $0x18,%rsp
  272. end;