syscalls.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. {BSD version of the syscalls required to implement SysLinux.}
  13. {No debugging for syslinux include !}
  14. {$IFDEF SYS_LINUX}
  15. {$UNDEF SYSCALL_DEBUG}
  16. {$ENDIF SYS_LINUX}
  17. {*****************************************************************************
  18. --- Main:The System Call Self ---
  19. *****************************************************************************}
  20. { The system designed for Linux can't be used for FreeBSD so easily, since
  21. FreeBSD pushes arguments, instead of loading them to registers.
  22. For now I do them in assembler, which makes it easier to test them (copy and
  23. paste to and AS source). Ultimately I hope to design something like this}
  24. {actualsyscall:
  25. _actualsyscall : int $0x80
  26. jb someerror
  27. ret
  28. someerror: storeerrorsomewhere
  29. ret
  30. }
  31. procedure actualsyscall; cdecl; EXTERNAL NAME '_actualsyscall';
  32. function Do_SysCall(sysnr:LONGINT):longint;
  33. var retval:longint;
  34. begin
  35. asm
  36. movl sysnr,%eax
  37. call actualsyscall
  38. mov %eax,Retval
  39. end;
  40. if RetVal<0 then
  41. begin
  42. ErrNo:=-RetVal;
  43. do_syscall:=-1;
  44. end
  45. else
  46. begin
  47. do_syscall:=Retval;
  48. errno:=0
  49. end;
  50. end;
  51. function Do_SysCall(sysnr,param1:LONGINT):longint;
  52. var retval:longint;
  53. begin
  54. asm
  55. movl sysnr,%eax
  56. pushl Param1
  57. call actualsyscall
  58. addl $4,%esp
  59. mov %eax,Retval
  60. end;
  61. if RetVal<0 then
  62. begin
  63. ErrNo:=-RetVal;
  64. do_syscall:=-1;
  65. end
  66. else
  67. begin
  68. do_syscall:=Retval;
  69. errno:=0
  70. end;
  71. end;
  72. function Do_SysCall(sysnr:longint;param1:integer):longint;
  73. var retval:longint;
  74. begin
  75. asm
  76. movl sysnr,%eax
  77. pushw Param1
  78. call actualsyscall
  79. addl $2,%esp
  80. mov %eax,Retval
  81. end;
  82. if RetVal<0 then
  83. begin
  84. ErrNo:=-RetVal;
  85. do_syscall:=-1;
  86. end
  87. else
  88. begin
  89. do_syscall:=Retval;
  90. errno:=0
  91. end;
  92. end;
  93. function Do_SysCall(sysnr,param1,param2:LONGINT):longint;
  94. var retval:longint;
  95. begin
  96. asm
  97. movl sysnr,%eax
  98. pushl param2
  99. pushl Param1
  100. call actualsyscall
  101. addl $8,%esp
  102. mov %eax,Retval
  103. end;
  104. if RetVal<0 then
  105. begin
  106. ErrNo:=-RetVal;
  107. do_syscall:=-1;
  108. end
  109. else
  110. begin
  111. do_syscall:=Retval;
  112. errno:=0
  113. end;
  114. end;
  115. function Do_SysCall(sysnr,param1,param2,param3:LONGINT):longint;
  116. var retval:longint;
  117. begin
  118. asm
  119. movl sysnr,%eax
  120. pushl param3
  121. pushl param2
  122. pushl Param1
  123. call actualsyscall
  124. addl $12,%esp
  125. mov %eax,Retval
  126. end;
  127. if RetVal<0 then
  128. begin
  129. ErrNo:=-RetVal;
  130. do_syscall:=-1;
  131. end
  132. else
  133. begin
  134. do_syscall:=Retval;
  135. errno:=0
  136. end;
  137. end;
  138. function Do_SysCall(sysnr,param1,param2:longint;param3:word):longint;
  139. var retval:longint;
  140. begin
  141. asm
  142. movl sysnr,%eax
  143. pushw param3
  144. pushl param2
  145. pushl Param1
  146. call actualsyscall
  147. addl $12,%esp
  148. mov %eax,Retval
  149. end;
  150. if RetVal<0 then
  151. begin
  152. ErrNo:=-RetVal;
  153. do_syscall:=-1;
  154. end
  155. else
  156. begin
  157. do_syscall:=Retval;
  158. errno:=0
  159. end;
  160. end;
  161. function Do_SysCall(sysnr,param1,param2,param3,param4:LONGINT):longint;
  162. var retval:longint;
  163. begin
  164. asm
  165. movl sysnr,%eax
  166. pushl param4
  167. pushl param3
  168. pushl param2
  169. pushl Param1
  170. call actualsyscall
  171. addl $16,%esp
  172. mov %eax,Retval
  173. end;
  174. if RetVal<0 then
  175. begin
  176. ErrNo:=-RetVal;
  177. do_syscall:=-1;
  178. end
  179. else
  180. begin
  181. do_syscall:=Retval;
  182. errno:=0
  183. end;
  184. end;
  185. function Do_SysCall(sysnr,param1,param2,param3,param4,param5:LONGINT):longint;
  186. var retval:longint;
  187. begin
  188. asm
  189. movl sysnr,%eax
  190. pushl param5
  191. pushl param4
  192. pushl param3
  193. pushl param2
  194. pushl Param1
  195. call actualsyscall
  196. addl $20,%esp
  197. mov %eax,Retval
  198. end;
  199. if RetVal<0 then
  200. begin
  201. ErrNo:=-RetVal;
  202. do_syscall:=-1;
  203. end
  204. else
  205. begin
  206. do_syscall:=Retval;
  207. errno:=0
  208. end;
  209. end;
  210. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6,param7:LONGINT):longint;
  211. var retval:longint;
  212. begin
  213. asm
  214. movl sysnr,%eax
  215. pushl param7
  216. pushl param6
  217. pushl param5
  218. pushl param4
  219. pushl param3
  220. pushl param2
  221. pushl Param1
  222. call actualsyscall
  223. addl $28,%esp
  224. mov %eax,Retval
  225. end;
  226. if RetVal<0 then
  227. begin
  228. ErrNo:=-RetVal;
  229. do_syscall:=-1;
  230. end
  231. else
  232. begin
  233. do_syscall:=Retval;
  234. errno:=0
  235. end;
  236. end;
  237. Function Sys_Time:longint;
  238. VAR tv : timeval;
  239. tz : timezone;
  240. retval : longint;
  241. begin
  242. Retval:=do_syscall(116,longint(@tv),longint(@tz));
  243. If retval=-1 then
  244. sys_time:=-1
  245. else
  246. sys_time:=tv.sec;
  247. end;
  248. {*****************************************************************************
  249. --- File:File handling related calls ---
  250. *****************************************************************************}
  251. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  252. Begin
  253. sys_open:=do_syscall(syscall_nr_open,longint(f),flags,mode);
  254. End;
  255. Function Sys_Close(f:longint):longint;
  256. begin
  257. sys_close:=do_syscall(syscall_nr_close,f);
  258. end;
  259. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  260. begin
  261. sys_lseek:=do_syscall(syscall_nr_lseek,F,Off,Whence);
  262. end;
  263. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  264. begin
  265. sys_read:=do_syscall(syscall_nr_read,F,longint(buffer),count);
  266. end;
  267. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  268. begin
  269. sys_write:=do_syscall(syscall_nr_write,F,longint(buffer),count);
  270. end;
  271. Function Sys_Unlink(Filename:pchar):longint;
  272. begin
  273. sys_unlink:=do_syscall(syscall_nr_unlink,longint(Filename));
  274. end;
  275. Function Sys_Rename(Oldname,Newname:pchar):longint;
  276. begin
  277. sys_rename:=do_syscallsyscall_nr_rename,longint(oldname),longint(newname));
  278. end;
  279. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  280. {
  281. We need this for getcwd
  282. }
  283. begin
  284. sys_stat:=do_syscall(syscall_nr_stat,longint(filename),longint(@buffer));
  285. end;
  286. Function Sys_Symlink(oldname,newname:pchar):longint;
  287. {
  288. We need this for erase
  289. }
  290. begin
  291. sys_symlink:=do_syscall(syscall_nr_symlink,longint(oldname),longint(newname));
  292. end;
  293. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint;
  294. begin
  295. sys_readlink:=do_syscall(syscall_nr_readlink, longint(name),longint(linkname),maxlen);
  296. end;
  297. {*****************************************************************************
  298. --- Directory:Directory related calls ---
  299. *****************************************************************************}
  300. Function Sys_Chdir(Filename:pchar):longint;
  301. begin
  302. sys_chdir:=do_syscall(syscall_nr_chdir,longint(filename));
  303. end;
  304. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  305. begin {Mode is 16-bit on F-BSD}
  306. sys_mkdir:=do_syscall(syscall_nr_mkdir,longint(filename),mode shl 8);
  307. end;
  308. Function Sys_Rmdir(Filename:pchar):longint;
  309. begin
  310. sys_rmdir:=do_syscall(syscall_nr_rmdir,longint(filename));
  311. end;
  312. { we need this for getcwd, NOT touched for BSD version }
  313. Function OpenDir(f:pchar):pdir;
  314. var
  315. fd:longint;
  316. st:stat;
  317. ptr:pdir;
  318. begin
  319. opendir:=nil;
  320. if sys_stat(f,st)<0 then
  321. exit;
  322. { Is it a dir ? }
  323. if not((st.mode and $f000)=$4000)then
  324. begin
  325. errno:=sys_enotdir;
  326. exit
  327. end;
  328. { Open it}
  329. fd:=sys_open(f,OPEN_RDONLY,438);
  330. if fd<0 then
  331. exit;
  332. new(ptr);
  333. if ptr=nil then
  334. exit;
  335. new(ptr^.buf);
  336. if ptr^.buf=nil then
  337. exit;
  338. ptr^.fd:=fd;
  339. ptr^.loc:=0;
  340. ptr^.size:=0;
  341. ptr^.dd_max:=sizeof(ptr^.buf^);
  342. opendir:=ptr;
  343. end;
  344. function CloseDir(p:pdir):integer;
  345. begin
  346. closedir:=sys_close(p^.fd);
  347. dispose(p^.buf);
  348. dispose(p);
  349. end;
  350. Function Sys_ReadDir(p:pdir):pdirent;
  351. var
  352. retval : longint;
  353. begin
  354. retval:=do_syscall(syscall_nr_getdents,longint(p^.fd),longint(p^.buf),sizeof(dirent));
  355. if retval=0 then
  356. sys_readdir:=nil
  357. else
  358. sys_readdir:=p^.buf
  359. end;
  360. {*****************************************************************************
  361. --- Process:Process & program handling - related calls ---
  362. *****************************************************************************}
  363. Function sys_GetPid:LongInt;
  364. {
  365. Get Process ID.
  366. }
  367. begin
  368. sys_GetPID:=do_syscall(syscall_nr_getpid);
  369. end;
  370. Procedure Sys_Exit(ExitCode:longint);
  371. begin
  372. do_syscall(syscall_nr_,exitcode);
  373. end;
  374. {
  375. $Log$
  376. Revision 1.11 2000-04-05 13:58:40 marco
  377. * syscall variablenames reintroduced.
  378. Revision 1.10 2000/03/16 16:18:12 marco
  379. * Last changes before next test. ppc386 -h works with these srcs.
  380. Revision 1.9 2000/03/02 15:34:07 marco
  381. * added a syscall for 5 longints
  382. Revision 1.8 2000/03/01 20:03:57 marco
  383. * small fixes for syslinux
  384. Revision 1.7 2000/03/01 17:28:40 marco
  385. * some changes due to updating linux.pp to new syscall
  386. Revision 1.6 2000/02/27 23:45:39 marco
  387. * Redone the syscalls
  388. Revision 1.5 2000/02/04 16:53:26 marco
  389. * Finished Linux (and rest syscalls) roughly. Some things still need to be
  390. tested, and checked (off_t calls specially)
  391. Revision 1.4 2000/02/03 17:04:47 marco
  392. * additions fixes due to port linux
  393. Revision 1.3 2000/02/02 18:07:27 marco
  394. * Ported except for readdir which is 200 lines C code in FBSD linux
  395. emulator
  396. Revision 1.2 2000/02/02 16:35:10 marco
  397. * Ported more functions. Half done now.
  398. Revision 1.1 2000/02/02 15:41:56 marco
  399. * Initial BSD version. Still needs a lot of work.
  400. }