syscalls.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. {
  32. procedure actualsyscall; cdecl; EXTERNAL NAME '_actualsyscall';
  33. }
  34. procedure actualsyscall; assembler; {inline requires a dummy push IIRC}
  35. asm
  36. int $0x80
  37. jb .LErrorcode
  38. xor %ebx,%ebx
  39. ret
  40. .LErrorcode:
  41. mov %eax,%ebx
  42. mov $-1,%eax
  43. end;
  44. function Do_SysCall(sysnr:LONGINT):longint; assembler;
  45. asm
  46. movl sysnr,%eax
  47. call actualsyscall
  48. movw %bx,Errno
  49. end;
  50. function Do_SysCall(sysnr,param1:longint):longint; assembler;
  51. asm
  52. movl sysnr,%eax
  53. pushl Param1
  54. call actualsyscall
  55. addl $4,%esp
  56. movw %bx,Errno
  57. end;
  58. function Do_SysCall(sysnr,param1:integer):longint; assembler;
  59. asm
  60. movl sysnr,%eax
  61. pushw Param1
  62. call actualsyscall
  63. add $2,%esp
  64. movw %bx,Errno
  65. end;
  66. function Do_SysCall(sysnr,param1,param2:LONGINT):longint; assembler;
  67. asm
  68. movl sysnr,%eax
  69. pushl param2
  70. pushl Param1
  71. call actualsyscall
  72. addl $8,%esp
  73. movw %bx,Errno
  74. end;
  75. function Do_SysCall(sysnr,param1,param2,param3:LONGINT):longint; assembler;
  76. asm
  77. movl sysnr,%eax
  78. pushl param3
  79. pushl param2
  80. pushl Param1
  81. call actualsyscall
  82. addl $12,%esp
  83. movw %bx,Errno
  84. end;
  85. function Do_SysCall(sysnr,param1,param2,param3,param4:LONGINT):longint; assembler;
  86. asm
  87. movl sysnr,%eax
  88. pushl param4
  89. pushl param3
  90. pushl param2
  91. pushl Param1
  92. call actualsyscall
  93. addl $16,%esp
  94. movw %bx,Errno
  95. end;
  96. function Do_SysCall(sysnr,param1,param2,param3,param4,param5:LONGINT):longint; assembler;
  97. asm
  98. movl sysnr,%eax
  99. pushl param5
  100. pushl param4
  101. pushl param3
  102. pushl param2
  103. pushl Param1
  104. call actualsyscall
  105. addl $20,%esp
  106. movw %bx,Errno
  107. end;
  108. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6:LONGINT):longint; assembler;
  109. asm
  110. movl sysnr,%eax
  111. pushl param6
  112. pushl param5
  113. pushl param4
  114. pushl param3
  115. pushl param2
  116. pushl Param1
  117. call actualsyscall
  118. addl $24,%esp
  119. movw %bx,Errno
  120. end;
  121. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6,param7:LONGINT):longint; assembler;
  122. asm
  123. movl sysnr,%eax
  124. pushl param7
  125. pushl param6
  126. pushl param5
  127. pushl param4
  128. pushl param3
  129. pushl param2
  130. pushl Param1
  131. call actualsyscall
  132. addl $28,%esp
  133. movw %bx,Errno
  134. end;
  135. Function Sys_Time:longint;
  136. VAR tv : timeval;
  137. tz : timezone;
  138. retval : longint;
  139. begin
  140. Retval:=do_syscall(116,longint(@tv),longint(@tz));
  141. If retval=-1 then
  142. sys_time:=-1
  143. else
  144. sys_time:=tv.sec;
  145. end;
  146. {*****************************************************************************
  147. --- File:File handling related calls ---
  148. *****************************************************************************}
  149. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  150. Begin
  151. sys_open:=do_syscall(syscall_nr_open,longint(f),flags,mode);
  152. End;
  153. Function Sys_Close(f:longint):longint;
  154. begin
  155. sys_close:=do_syscall(syscall_nr_close,f);
  156. end;
  157. {
  158. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  159. var returnvalue64 : array[0..1] of longint;
  160. begin
  161. {Lseek's offset is 64-bit, the highword is the 0}
  162. do_syscall(syscall_nr_lseek,longint(@returnvalue64),F,Off,0,Whence);
  163. sys_lseek:=returnvalue64[0];
  164. end;
  165. }
  166. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint; assembler;
  167. {this one is special for the return value being 64-bit..}
  168. asm
  169. pushl Whence
  170. pushl $0 // high dword
  171. pushl Off
  172. pushl $0
  173. pushl F
  174. pushl $0 // Your guess is as good as mine.
  175. pushl $0xc7 // Actual lseek syscall number.
  176. movl $0xc6,%eax
  177. call actualsyscall
  178. addl $28,%esp
  179. mov %ebx,Errno
  180. end;
  181. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  182. begin
  183. sys_read:=do_syscall(syscall_nr_read,F,longint(buffer),count);
  184. end;
  185. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  186. begin
  187. sys_write:=do_syscall(syscall_nr_write,F,longint(buffer),count);
  188. end;
  189. Function Sys_Unlink(Filename:pchar):longint;
  190. begin
  191. sys_unlink:=do_syscall(syscall_nr_unlink,longint(Filename));
  192. end;
  193. Function Sys_Rename(Oldname,Newname:pchar):longint;
  194. begin
  195. sys_rename:=do_syscall(syscall_nr_rename,longint(oldname),longint(newname));
  196. end;
  197. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  198. {
  199. We need this for getcwd
  200. }
  201. begin
  202. sys_stat:=do_syscall(syscall_nr_stat,longint(filename),longint(@buffer));
  203. end;
  204. Function Sys_Symlink(oldname,newname:pchar):longint;
  205. {
  206. We need this for erase
  207. }
  208. begin
  209. sys_symlink:=do_syscall(syscall_nr_symlink,longint(oldname),longint(newname));
  210. end;
  211. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint;
  212. begin
  213. sys_readlink:=do_syscall(syscall_nr_readlink, longint(name),longint(linkname),maxlen);
  214. end;
  215. {*****************************************************************************
  216. --- Directory:Directory related calls ---
  217. *****************************************************************************}
  218. Function Sys_Chdir(Filename:pchar):longint;
  219. begin
  220. sys_chdir:=do_syscall(syscall_nr_chdir,longint(filename));
  221. end;
  222. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  223. begin {Mode is 16-bit on F-BSD}
  224. sys_mkdir:=do_syscall(syscall_nr_mkdir,longint(filename),mode);
  225. end;
  226. Function Sys_Rmdir(Filename:pchar):longint;
  227. begin
  228. sys_rmdir:=do_syscall(syscall_nr_rmdir,longint(filename));
  229. end;
  230. {$ifndef NewReaddir}
  231. const DIRBLKSIZ=1024;
  232. { we need this for getcwd, NOT touched for BSD version }
  233. Function OpenDir(f:pchar):pdir;
  234. var
  235. fd:longint;
  236. st:stat;
  237. ptr:pdir;
  238. begin
  239. opendir:=nil;
  240. if sys_stat(f,st)<0 then
  241. exit;
  242. { Is it a dir ? }
  243. if not((st.mode and $f000)=$4000)then
  244. begin
  245. errno:=Esysenotdir;
  246. exit
  247. end;
  248. { Open it}
  249. fd:=sys_open(f,OPEN_RDONLY,438);
  250. if fd<0 then
  251. exit;
  252. new(ptr);
  253. if ptr=nil then
  254. exit;
  255. Getmem(ptr^.buf,2*DIRBLKSIZ);
  256. if ptr^.buf=nil then
  257. exit;
  258. ptr^.fd:=fd;
  259. ptr^.loc:=-1;
  260. ptr^.rewind:=longint(ptr^.buf);
  261. ptr^.size:=0;
  262. // ptr^.dd_max:=sizeof(ptr^.buf^);
  263. opendir:=ptr;
  264. end;
  265. function CloseDir(p:pdir):integer;
  266. begin
  267. closedir:=sys_close(p^.fd);
  268. Freemem(p^.buf);
  269. dispose(p);
  270. end;
  271. Function Sys_ReadDir(p:pdir):pdirent;
  272. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  273. missing of the readdir syscall.
  274. Getdents requires the buffer to be larger than the blocksize.
  275. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  276. with blockmode have this higher?}
  277. function readbuffer:longint;
  278. var retval :longint;
  279. begin
  280. retval:=do_syscall(syscall_nr_getdents,longint(p^.fd),longint(@p^.buf^),DIRBLKSIZ {sizeof(getdentsbuffer)});
  281. p^.rewind:=longint(p^.buf);
  282. if retval=0 then
  283. begin
  284. p^.rewind:=0;
  285. p^.loc:=0;
  286. end
  287. else
  288. P^.loc:=retval;
  289. readbuffer:=retval;
  290. end;
  291. var
  292. FinalEntry : pdirent;
  293. novalid : boolean;
  294. Reclen : Longint;
  295. CurEntry : PDirent;
  296. begin
  297. if (p^.buf=nil) or (p^.loc=0) THEN
  298. exit(nil);
  299. if (p^.loc=-1) OR {First readdir on this pdir. Initial fill of buffer}
  300. (p^.rewind>=(longint(p^.buf)+dirblksiz)) then {no more entries left?}
  301. Begin
  302. if readbuffer=0 then {succesful read?}
  303. Exit(NIL); {No more data}
  304. End;
  305. FinalEntry:=NIL;
  306. CurEntry:=nil;
  307. repeat
  308. novalid:=false;
  309. CurEntry:=pdirent(p^.rewind);
  310. RecLen:=CurEntry^.reclen;
  311. if RecLen<>0 Then
  312. begin {valid direntry?}
  313. if CurEntry^.ino<>0 then
  314. FinalEntry:=CurEntry;
  315. inc(p^.rewind,Reclen);
  316. end
  317. else
  318. begin {block entirely searched or reclen=0}
  319. Novalid:=True;
  320. if p^.loc<>0 THEN {blocks left?}
  321. if readbuffer()<>0 then {succesful read?}
  322. novalid:=false;
  323. end;
  324. until (FinalEntry<>nil) or novalid;
  325. If novalid then
  326. FinalEntry:=nil;
  327. Sys_ReadDir:=FinalEntry;
  328. end;
  329. {$endif}
  330. {*****************************************************************************
  331. --- Process:Process & program handling - related calls ---
  332. *****************************************************************************}
  333. Function sys_GetPid:LongInt;
  334. {
  335. Get Process ID.
  336. }
  337. begin
  338. sys_GetPID:=do_syscall(syscall_nr_getpid);
  339. end;
  340. Procedure Sys_Exit(ExitCode:longint);
  341. begin
  342. do_syscall(syscall_nr_exit,exitcode);
  343. end;
  344. {
  345. Change action of process upon receipt of a signal.
  346. Signum specifies the signal (all except SigKill and SigStop).
  347. If Act is non-nil, it is used to specify the new action.
  348. If OldAct is non-nil the previous action is saved there.
  349. }
  350. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  351. {
  352. Change action of process upon receipt of a signal.
  353. Signum specifies the signal (all except SigKill and SigStop).
  354. If Act is non-nil, it is used to specify the new action.
  355. If OldAct is non-nil the previous action is saved there.
  356. }
  357. begin
  358. do_syscall(syscall_nr_sigaction,longint(signum),longint(act),longint(oldact));
  359. {$ifdef linuxunit}
  360. LinuxError:=Errno;
  361. {$endif}
  362. end;
  363. (*=================== MOVED from syslinux.inc ========================*)
  364. {
  365. Function Sys_FTruncate(Handle,Pos:longint):longint; //moved from sysunix.inc Do_Truncate
  366. begin
  367. Sys_FTruncate:=do_syscall(syscall_nr_ftruncate,handle,pos,0);
  368. end;
  369. }
  370. Function Sys_ftruncate(handle,pos:longint):longint; assembler;
  371. {this one is special for the return value being 64-bit..}
  372. asm
  373. pushl $0
  374. pushl pos
  375. pushl $0 // Your guess is as good as mine.
  376. pushl handle
  377. pushl $0
  378. pushl $0xc9 // Actual lseek syscall number.
  379. movl $0xc6,%eax
  380. call actualsyscall
  381. addl $24,%esp
  382. mov %ebx,Errno
  383. end;
  384. Function Sys_fstat(fd : longint;var Info:stat):Longint; // This was missing here, instead an fstat call was included in Do_FileSize
  385. begin
  386. Sys_FStat:=do_SysCall(syscall_nr_fstat,fd,longint(@info));
  387. end;
  388. {$ifdef NewReaddir}
  389. {$I readdir.inc}
  390. {$endif}
  391. {
  392. Interface to Unix ioctl call.
  393. Performs various operations on the filedescriptor Handle.
  394. Ndx describes the operation to perform.
  395. Data points to data needed for the Ndx function. The structure of this
  396. data is function-dependent.
  397. }
  398. Function Sys_IOCtl(Handle,Ndx: Longint;Data: Pointer):LongInt; // This was missing here, instead hardcoded in Do_IsDevice
  399. begin
  400. Sys_IOCtl:=do_SysCall(syscall_nr_ioctl,handle,Ndx,longint(data));
  401. end;
  402. Function Sys_mmap(adr,len,prot,flags,fdes,off:longint):longint; // moved from sysunix.inc, used in sbrk
  403. begin
  404. Sys_mmap:=do_syscall(syscall_nr_mmap,Adr,Len,Prot,Flags,fdes,off,0);
  405. end;
  406. {
  407. $Log$
  408. Revision 1.10 2003-01-05 19:02:29 marco
  409. * Should now work with baseunx. (gmake all works)
  410. Revision 1.9 2002/09/07 16:01:17 peter
  411. * old logs removed and tabs fixed
  412. Revision 1.8 2002/05/06 07:27:39 marco
  413. * Fixed a readdir bug, already fixed in januari in 1.0.x
  414. }