syscalls.inc 9.4 KB

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