syscall.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. 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. {No debugging for syslinux include !}
  13. {$IFDEF SYS_LINUX}
  14. {$UNDEF SYSCALL_DEBUG}
  15. {$ENDIF SYS_LINUX}
  16. {*****************************************************************************
  17. --- Main:The System Call Self ---
  18. *****************************************************************************}
  19. function FpSysCall(sysnr:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL0'];
  20. {
  21. This function puts the registers in place, does the call, and then
  22. copies back the registers as they are after the SysCall.
  23. }
  24. var
  25. temp, retaddress: longint;
  26. asm
  27. mr r0,r3
  28. sc
  29. bns .LDone
  30. lis r10,(fpc_threadvar_relocate_proc)@ha
  31. lwz r10,(fpc_threadvar_relocate_proc)@l(r10)
  32. cmpwi r10,0
  33. bne .LThreaded
  34. lis r4,(Errno+4)@ha
  35. stw r3,(Errno+4)@l(r4)
  36. b .LFailed
  37. .LThreaded:
  38. stw r3,temp
  39. mflr r3
  40. mtctr r10
  41. lis r4,(errno)@ha
  42. stw r3,retaddress
  43. lwz r3,(errno)@l(r4)
  44. bctrl
  45. lwz r4,temp
  46. lwz r5,retaddress
  47. stw r4,0(r3)
  48. mtlr r5
  49. .LFailed:
  50. li r3,-1
  51. .LDone:
  52. end;
  53. function FpSysCall(sysnr,param1:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL1'];
  54. {
  55. This function puts the registers in place, does the call, and then
  56. copies back the registers as they are after the SysCall.
  57. }
  58. var
  59. temp, retaddress: longint;
  60. asm
  61. mr r0,r3
  62. mr r3,r4
  63. sc
  64. bns .LDone
  65. lis r10,(fpc_threadvar_relocate_proc)@ha
  66. lwz r10,(fpc_threadvar_relocate_proc)@l(r10)
  67. cmpwi r10,0
  68. bne .LThreaded
  69. lis r4,(Errno+4)@ha
  70. stw r3,(Errno+4)@l(r4)
  71. b .LFailed
  72. .LThreaded:
  73. stw r3,temp
  74. mflr r3
  75. mtctr r10
  76. lis r4,(errno)@ha
  77. stw r3,retaddress
  78. lwz r3,(errno)@l(r4)
  79. bctrl
  80. lwz r4,temp
  81. lwz r5,retaddress
  82. stw r4,0(r3)
  83. mtlr r5
  84. .LFailed:
  85. li r3,-1
  86. .LDone:
  87. end;
  88. function FpSysCall(sysnr,param1,param2:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL2'];
  89. {
  90. This function puts the registers in place, does the call, and then
  91. copies back the registers as they are after the SysCall.
  92. }
  93. var
  94. temp, retaddress: longint;
  95. asm
  96. mr r0,r3
  97. mr r3,r4
  98. mr r4,r5
  99. sc
  100. bns .LDone
  101. lis r10,(fpc_threadvar_relocate_proc)@ha
  102. lwz r10,(fpc_threadvar_relocate_proc)@l(r10)
  103. cmpwi r10,0
  104. bne .LThreaded
  105. lis r4,(Errno+4)@ha
  106. stw r3,(Errno+4)@l(r4)
  107. b .LFailed
  108. .LThreaded:
  109. stw r3,temp
  110. mflr r3
  111. mtctr r10
  112. lis r4,(errno)@ha
  113. stw r3,retaddress
  114. lwz r3,(errno)@l(r4)
  115. bctrl
  116. lwz r4,temp
  117. lwz r5,retaddress
  118. stw r4,0(r3)
  119. mtlr r5
  120. .LFailed:
  121. li r3,-1
  122. .LDone:
  123. end;
  124. function FpSysCall(sysnr,param1,param2,param3:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL3'];
  125. {
  126. This function puts the registers in place, does the call, and then
  127. copies back the registers as they are after the SysCall.
  128. }
  129. var
  130. temp, retaddress: longint;
  131. asm
  132. mr r0,r3
  133. mr r3,r4
  134. mr r4,r5
  135. mr r5,r6
  136. sc
  137. bns .LDone
  138. lis r10,(fpc_threadvar_relocate_proc)@ha
  139. lwz r10,(fpc_threadvar_relocate_proc)@l(r10)
  140. cmpwi r10,0
  141. bne .LThreaded
  142. lis r4,(Errno+4)@ha
  143. stw r3,(Errno+4)@l(r4)
  144. b .LFailed
  145. .LThreaded:
  146. stw r3,temp
  147. mflr r3
  148. mtctr r10
  149. lis r4,(errno)@ha
  150. stw r3,retaddress
  151. lwz r3,(errno)@l(r4)
  152. bctrl
  153. lwz r4,temp
  154. lwz r5,retaddress
  155. stw r4,0(r3)
  156. mtlr r5
  157. .LFailed:
  158. li r3,-1
  159. .LDone:
  160. end;
  161. function FpSysCall(sysnr,param1,param2,param3,param4:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL4'];
  162. {
  163. This function puts the registers in place, does the call, and then
  164. copies back the registers as they are after the SysCall.
  165. }
  166. var
  167. temp, retaddress: longint;
  168. asm
  169. mr r0,r3
  170. mr r3,r4
  171. mr r4,r5
  172. mr r5,r6
  173. mr r6,r7
  174. sc
  175. bns .LDone
  176. lis r10,(fpc_threadvar_relocate_proc)@ha
  177. lwz r10,(fpc_threadvar_relocate_proc)@l(r10)
  178. cmpwi r10,0
  179. bne .LThreaded
  180. lis r4,(Errno+4)@ha
  181. stw r3,(Errno+4)@l(r4)
  182. b .LFailed
  183. .LThreaded:
  184. stw r3,temp
  185. mflr r3
  186. mtctr r10
  187. lis r4,(errno)@ha
  188. stw r3,retaddress
  189. lwz r3,(errno)@l(r4)
  190. bctrl
  191. lwz r4,temp
  192. lwz r5,retaddress
  193. stw r4,0(r3)
  194. mtlr r5
  195. .LFailed:
  196. li r3,-1
  197. .LDone:
  198. end;
  199. function FpSysCall(sysnr,param1,param2,param3,param4,param5:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL5'];
  200. {
  201. This function puts the registers in place, does the call, and then
  202. copies back the registers as they are after the SysCall.
  203. }
  204. var
  205. temp, retaddress: longint;
  206. asm
  207. mr r0,r3
  208. mr r3,r4
  209. mr r4,r5
  210. mr r5,r6
  211. mr r6,r7
  212. mr r7,r8
  213. sc
  214. bns .LDone
  215. lis r10,(fpc_threadvar_relocate_proc)@ha
  216. lwz r10,(fpc_threadvar_relocate_proc)@l(r10)
  217. cmpwi r10,0
  218. bne .LThreaded
  219. lis r4,(Errno+4)@ha
  220. stw r3,(Errno+4)@l(r4)
  221. b .LFailed
  222. .LThreaded:
  223. stw r3,temp
  224. mflr r3
  225. mtctr r10
  226. lis r4,(errno)@ha
  227. stw r3,retaddress
  228. lwz r3,(errno)@l(r4)
  229. bctrl
  230. lwz r4,temp
  231. lwz r5,retaddress
  232. stw r4,0(r3)
  233. mtlr r5
  234. .LFailed:
  235. li r3,-1
  236. .LDone:
  237. end;
  238. function FpSysCall(sysnr,param1,param2,param3,param4,param5,param6:TSysParam):TSysResult; assembler;[public,alias:'FPC_SYSCALL6'];
  239. {
  240. This function puts the registers in place, does the call, and then
  241. copies back the registers as they are after the SysCall.
  242. }
  243. var
  244. temp, retaddress: longint;
  245. asm
  246. mr r0,r3
  247. mr r3,r4
  248. mr r4,r5
  249. mr r5,r6
  250. mr r6,r7
  251. mr r7,r8
  252. mr r8,r9
  253. sc
  254. bns .LDone
  255. lis r10,(fpc_threadvar_relocate_proc)@ha
  256. lwz r10,(fpc_threadvar_relocate_proc)@l(r10)
  257. cmpwi r10,0
  258. bne .LThreaded
  259. lis r4,(Errno+4)@ha
  260. stw r3,(Errno+4)@l(r4)
  261. b .LFailed
  262. .LThreaded:
  263. stw r3,temp
  264. mflr r3
  265. mtctr r10
  266. lis r4,(errno)@ha
  267. stw r3,retaddress
  268. lwz r3,(errno)@l(r4)
  269. bctrl
  270. lwz r4,temp
  271. lwz r5,retaddress
  272. stw r4,0(r3)
  273. mtlr r5
  274. .LFailed:
  275. li r3,-1
  276. .LDone:
  277. end;
  278. // Old style syscall:
  279. // Better use ktrace/strace/gdb for debugging.
  280. Procedure FpSysCall( callnr:longint;var regs : SysCallregs );assembler;
  281. {
  282. This function puts the registers in place, does the call, and then
  283. copies back the registers as they are after the SysCall.
  284. }
  285. asm
  286. { load the registers... }
  287. lwz r5, 12(r4)
  288. lwz r6, 16(r4)
  289. lwz r7, 20(r4)
  290. mr r0, r3
  291. lwz r3, 4(r4)
  292. mr r8, r4
  293. lwz r4, 8(r4)
  294. { Go ! }
  295. sc
  296. bns .Lsyscall_ok
  297. .Lsyscall_ok:
  298. { Put back the registers... }
  299. stw r3, 0(r8)
  300. end;
  301. {$IFDEF SYSCALL_DEBUG}
  302. Const
  303. DoSysCallDebug : Boolean = False;
  304. var
  305. LastCnt,
  306. LastEax,
  307. LastCall : longint;
  308. DebugTxt : string[20];
  309. {$ENDIF}
  310. Function SysCall( callnr:longint;var regs : SysCallregs ):longint;
  311. {
  312. This function serves as an interface to do_SysCall.
  313. If the SysCall returned a negative number, it returns -1, and puts the
  314. SysCall result in errno. Otherwise, it returns the SysCall return value
  315. }
  316. begin
  317. FpSysCall(callnr,regs);
  318. if regs.reg1<0 then
  319. begin
  320. {$IFDEF SYSCALL_DEBUG}
  321. If DoSysCallDebug then
  322. debugtxt:=' syscall error: ';
  323. {$endif}
  324. ErrNo:=-regs.reg1;
  325. SysCall:=-1;
  326. end
  327. else
  328. begin
  329. {$IFDEF SYSCALL_DEBUG}
  330. if DoSysCallDebug then
  331. debugtxt:=' syscall returned: ';
  332. {$endif}
  333. SysCall:=regs.reg1;
  334. errno:=0
  335. end;
  336. {$IFDEF SYSCALL_DEBUG}
  337. if DoSysCallDebug then
  338. begin
  339. inc(lastcnt);
  340. if (callnr<>lastcall) or (regs.reg1<>lasteax) then
  341. begin
  342. if lastcnt>1 then
  343. writeln(sys_nr_txt[lastcall],debugtxt,lasteax,' (',lastcnt,'x)');
  344. lastcall:=callnr;
  345. lasteax:=regs.reg1;
  346. lastcnt:=0;
  347. writeln(sys_nr_txt[lastcall],debugtxt,lasteax);
  348. end;
  349. end;
  350. {$endif}
  351. end;
  352. {
  353. $Log$
  354. Revision 1.11 2003-12-28 20:55:10 jonas
  355. * fixed result of failed syscalls (no extra neg is needed)
  356. + support multi-threaded programs
  357. Revision 1.10 2003/11/29 22:54:32 jonas
  358. * more ppc fixes, hello world works again under linuxppc
  359. Revision 1.8 2003/11/15 19:01:27 florian
  360. * fixed rtl to work with the integrated fpc ppc assembler reader
  361. Revision 1.7 2003/06/17 16:39:58 jonas
  362. * fixed old syscall handling for ppc
  363. Revision 1.6 2003/05/23 21:58:30 jonas
  364. * fixed storing to errno for single threaded programs
  365. Revision 1.5 2003/05/11 16:07:55 jonas
  366. * fixed mmap for non-i386 non-m68k architectures (not sure about
  367. x86-64)
  368. Revision 1.4 2003/04/22 17:07:55 florian
  369. * there where two SYSCALL1 procedures for the powerpc, fixed
  370. Revision 1.3 2003/01/09 13:38:26 florian
  371. * syscall stuff fixed
  372. Revision 1.2 2002/12/22 16:00:28 jonas
  373. + added syscallh.inc, adapted syscall.inc
  374. Revision 1.1 2002/11/09 20:32:14 marco
  375. * powerpc version. Threadsafe errno access not yet done.
  376. Revision 1.1 2002/10/14 19:39:44 peter
  377. * syscall moved into seperate include
  378. }