syscalls.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. {
  238. Function SysCall( callnr:longint;var regs : SysCallregs ):longint;
  239. {
  240. This function serves as an interface to do_SysCall.
  241. If the SysCall returned a negative number, it returns -1, and puts the
  242. SysCall result in errno. Otherwise, it returns the SysCall return value
  243. }
  244. begin
  245. do_SysCall(callnr,regs);
  246. if regs.reg1<0 then
  247. begin
  248. ErrNo:=-regs.reg1;
  249. SysCall:=-1;
  250. end
  251. else
  252. begin
  253. SysCall:=regs.reg1;
  254. errno:=0
  255. end;
  256. end;
  257. }
  258. {$PACKRECORDS C}
  259. {
  260. TYPE timeval=RECORD
  261. tv_sec,
  262. tv_used : int64;
  263. END;
  264. timezone=RECORD
  265. tz_minuteswest,
  266. tz_dsttime : LONGINT;
  267. END;
  268. }
  269. function checkreturnvalue(retval:LONGINT;value:LONGINT):LONGINT;
  270. begin
  271. if retval<0 THEN
  272. begin
  273. errno:=-retval;
  274. checkreturnvalue:=-1;
  275. end
  276. else
  277. begin
  278. checkreturnvalue:=value;
  279. errno:=0
  280. end;
  281. end;
  282. Function Sys_Time:longint;
  283. VAR tv : timeval;
  284. tz : timezone;
  285. retval : longint;
  286. begin
  287. Retval:=do_syscall(116,longint(@tv),longint(@tz));
  288. If retval=-1 then
  289. sys_time:=-1
  290. else
  291. sys_time:=tv.sec;
  292. end;
  293. {*****************************************************************************
  294. --- File:File handling related calls ---
  295. *****************************************************************************}
  296. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  297. Begin
  298. sys_open:=do_syscall(5,longint(f),flags,mode);
  299. End;
  300. Function Sys_Close(f:longint):longint;
  301. begin
  302. sys_close:=do_syscall(6,f);
  303. end;
  304. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  305. begin
  306. sys_lseek:=do_syscall(199,F,Off,Whence);
  307. end;
  308. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  309. begin
  310. sys_read:=do_syscall(3,F,longint(buffer),count);
  311. end;
  312. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  313. begin
  314. sys_write:=do_syscall(4,F,longint(buffer),count);
  315. end;
  316. Function Sys_Unlink(Filename:pchar):longint;
  317. begin
  318. sys_unlink:=do_syscall(10,longint(Filename));
  319. end;
  320. Function Sys_Rename(Oldname,Newname:pchar):longint;
  321. begin
  322. sys_rename:=do_syscall(38,longint(oldname),longint(newname));
  323. end;
  324. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  325. {
  326. We need this for getcwd
  327. }
  328. begin
  329. sys_stat:=do_syscall(188,longint(filename),longint(@buffer));
  330. end;
  331. Function Sys_Symlink(oldname,newname:pchar):longint;
  332. {
  333. We need this for erase
  334. }
  335. begin
  336. sys_symlink:=do_syscall(57,longint(oldname),longint(newname));
  337. end;
  338. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint;
  339. begin
  340. sys_readlink:=do_syscall(58, longint(name),longint(linkname),maxlen);
  341. end;
  342. {*****************************************************************************
  343. --- Directory:Directory related calls ---
  344. *****************************************************************************}
  345. Function Sys_Chdir(Filename:pchar):longint;
  346. begin
  347. sys_chdir:=do_syscall(12,longint(filename));
  348. end;
  349. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  350. begin {Mode is 16-bit on F-BSD}
  351. sys_mkdir:=do_syscall(longint(filename),mode shl 8);
  352. end;
  353. Function Sys_Rmdir(Filename:pchar):longint;
  354. begin
  355. sys_rmdir:=do_syscall(137,longint(filename));
  356. end;
  357. { we need this for getcwd, NOT touched for BSD version }
  358. Function OpenDir(f:pchar):pdir;
  359. var
  360. fd:integer;
  361. st:stat;
  362. ptr:pdir;
  363. begin
  364. opendir:=nil;
  365. if sys_stat(f,st)<0 then
  366. exit;
  367. { Is it a dir ? }
  368. if not((st.mode and $f000)=$4000)then
  369. begin
  370. errno:=sys_enotdir;
  371. exit
  372. end;
  373. { Open it}
  374. fd:=sys_open(f,OPEN_RDONLY,438);
  375. if fd<0 then
  376. exit;
  377. new(ptr);
  378. if ptr=nil then
  379. exit;
  380. new(ptr^.buf);
  381. if ptr^.buf=nil then
  382. exit;
  383. ptr^.fd:=fd;
  384. ptr^.loc:=0;
  385. ptr^.size:=0;
  386. ptr^.dd_max:=sizeof(ptr^.buf^);
  387. opendir:=ptr;
  388. end;
  389. function CloseDir(p:pdir):integer;
  390. begin
  391. closedir:=sys_close(p^.fd);
  392. dispose(p^.buf);
  393. dispose(p);
  394. end;
  395. Function Sys_ReadDir(p:pdir):pdirent;
  396. var
  397. retval : longint;
  398. begin
  399. retval:=do_syscall(272,longint(p^.fd),longint(p^.buf),sizeof(dirent));
  400. if retval=0 then
  401. sys_readdir:=nil
  402. else
  403. sys_readdir:=p^.buf
  404. end;
  405. {*****************************************************************************
  406. --- Process:Process & program handling - related calls ---
  407. *****************************************************************************}
  408. Function sys_GetPid:LongInt;
  409. {
  410. Get Process ID.
  411. }
  412. begin
  413. sys_GetPID:=do_syscall(20);
  414. end;
  415. Procedure Sys_Exit(ExitCode:longint);
  416. begin
  417. do_syscall(1,exitcode);
  418. end;
  419. {
  420. $Log$
  421. Revision 1.10 2000-03-16 16:18:12 marco
  422. * Last changes before next test. ppc386 -h works with these srcs.
  423. Revision 1.9 2000/03/02 15:34:07 marco
  424. * added a syscall for 5 longints
  425. Revision 1.8 2000/03/01 20:03:57 marco
  426. * small fixes for syslinux
  427. Revision 1.7 2000/03/01 17:28:40 marco
  428. * some changes due to updating linux.pp to new syscall
  429. Revision 1.6 2000/02/27 23:45:39 marco
  430. * Redone the syscalls
  431. Revision 1.5 2000/02/04 16:53:26 marco
  432. * Finished Linux (and rest syscalls) roughly. Some things still need to be
  433. tested, and checked (off_t calls specially)
  434. Revision 1.4 2000/02/03 17:04:47 marco
  435. * additions fixes due to port linux
  436. Revision 1.3 2000/02/02 18:07:27 marco
  437. * Ported except for readdir which is 200 lines C code in FBSD linux
  438. emulator
  439. Revision 1.2 2000/02/02 16:35:10 marco
  440. * Ported more functions. Half done now.
  441. Revision 1.1 2000/02/02 15:41:56 marco
  442. * Initial BSD version. Still needs a lot of work.
  443. }