syscalls.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. {$i cpusys.inc }
  25. Function Sys_Time:longint;
  26. VAR tv : timeval;
  27. tz : timezone;
  28. retval : longint;
  29. begin
  30. Retval:=do_syscall(116,longint(@tv),longint(@tz));
  31. If retval=-1 then
  32. sys_time:=-1
  33. else
  34. sys_time:=tv.sec;
  35. end;
  36. {*****************************************************************************
  37. --- File:File handling related calls ---
  38. *****************************************************************************}
  39. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  40. Begin
  41. sys_open:=do_syscall(syscall_nr_open,longint(f),flags,mode);
  42. End;
  43. Function Sys_Close(f:longint):longint;
  44. begin
  45. sys_close:=do_syscall(syscall_nr_close,f);
  46. end;
  47. {$ifndef m68k}
  48. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  49. var returnvalue64 : int64;
  50. begin
  51. {Lseek's offset is 64-bit, the highword is the 0}
  52. // do_syscall(syscall_nr_lseek,longint(@returnvalue64),F,Off,0,Whence);
  53. do_syscall(syscall_nr___syscall,syscall_nr_lseek,0,f,0,off,0,whence);
  54. asm
  55. lea returnvalue64,%ebx
  56. movl %eax,(%ebx)
  57. movl %edx,4(%ebx)
  58. end;
  59. sys_lseek:=longint(returnvalue64);
  60. end;
  61. {$endif m68k}
  62. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  63. begin
  64. sys_read:=do_syscall(syscall_nr_read,F,longint(buffer),count);
  65. end;
  66. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  67. begin
  68. sys_write:=do_syscall(syscall_nr_write,F,longint(buffer),count);
  69. end;
  70. Function Sys_Unlink(Filename:pchar):longint;
  71. begin
  72. sys_unlink:=do_syscall(syscall_nr_unlink,longint(Filename));
  73. end;
  74. Function Sys_Rename(Oldname,Newname:pchar):longint;
  75. begin
  76. sys_rename:=do_syscall(syscall_nr_rename,longint(oldname),longint(newname));
  77. end;
  78. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  79. {
  80. We need this for getcwd
  81. }
  82. begin
  83. sys_stat:=do_syscall(syscall_nr_stat,longint(filename),longint(@buffer));
  84. end;
  85. Function Sys_Symlink(oldname,newname:pchar):longint;
  86. {
  87. We need this for erase
  88. }
  89. begin
  90. sys_symlink:=do_syscall(syscall_nr_symlink,longint(oldname),longint(newname));
  91. end;
  92. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint;
  93. begin
  94. sys_readlink:=do_syscall(syscall_nr_readlink, longint(name),longint(linkname),maxlen);
  95. end;
  96. {*****************************************************************************
  97. --- Directory:Directory related calls ---
  98. *****************************************************************************}
  99. Function Sys_Chdir(Filename:pchar):longint;
  100. begin
  101. sys_chdir:=do_syscall(syscall_nr_chdir,longint(filename));
  102. end;
  103. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  104. begin {Mode is 16-bit on F-BSD}
  105. sys_mkdir:=do_syscall(syscall_nr_mkdir,longint(filename),mode );
  106. end;
  107. Function Sys_Rmdir(Filename:pchar):longint;
  108. begin
  109. sys_rmdir:=do_syscall(syscall_nr_rmdir,longint(filename));
  110. end;
  111. {$ifndef NewReaddir}
  112. const DIRBLKSIZ=1024;
  113. { we need this for getcwd, NOT touched for BSD version }
  114. Function OpenDir(f:pchar):pdir;
  115. var
  116. fd:longint;
  117. st:stat;
  118. ptr:pdir;
  119. begin
  120. opendir:=nil;
  121. if sys_stat(f,st)<0 then
  122. exit;
  123. { Is it a dir ? }
  124. if not((st.mode and $f000)=$4000)then
  125. begin
  126. errno:=Esysenotdir;
  127. exit
  128. end;
  129. { Open it}
  130. fd:=sys_open(f,OPEN_RDONLY,438);
  131. if fd<0 then
  132. exit;
  133. new(ptr);
  134. if ptr=nil then
  135. exit;
  136. Getmem(ptr^.buf,2*DIRBLKSIZ);
  137. if ptr^.buf=nil then
  138. exit;
  139. ptr^.fd:=fd;
  140. ptr^.loc:=-1;
  141. ptr^.rewind:=longint(ptr^.buf);
  142. ptr^.size:=0;
  143. // ptr^.dd_max:=sizeof(ptr^.buf^);
  144. opendir:=ptr;
  145. end;
  146. function CloseDir(p:pdir):integer;
  147. begin
  148. closedir:=sys_close(p^.fd);
  149. Freemem(p^.buf);
  150. dispose(p);
  151. end;
  152. Function Sys_ReadDir(p:pdir):pdirent;
  153. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  154. missing of the readdir syscall.
  155. Getdents requires the buffer to be larger than the blocksize.
  156. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  157. with blockmode have this higher?}
  158. function readbuffer:longint;
  159. var retval :longint;
  160. begin
  161. retval:=do_syscall(syscall_nr_getdents,longint(p^.fd),longint(@p^.buf^),DIRBLKSIZ {sizeof(getdentsbuffer)});
  162. p^.rewind:=longint(p^.buf);
  163. if retval=0 then
  164. begin
  165. p^.rewind:=0;
  166. p^.loc:=0;
  167. end
  168. else
  169. P^.loc:=retval;
  170. readbuffer:=retval;
  171. end;
  172. var
  173. l : pdirent;
  174. novalid : boolean;
  175. begin
  176. if (p^.buf=nil) or (p^.loc=0) THEN
  177. exit(nil);
  178. if p^.loc=cardinal(-1) then {First readdir on this pdir. Initial fill of buffer}
  179. begin
  180. if readbuffer()=0 Then {nothing to be read}
  181. exit(nil)
  182. end;
  183. l:=nil;
  184. repeat
  185. novalid:=false;
  186. if (pdirent(p^.rewind)^.reclen<>0) then
  187. begin {valid direntry?}
  188. if pdirent(P^.rewind)^.ino<>0 then
  189. l:=pdirent(p^.rewind);
  190. inc(p^.rewind,pdirent(p^.rewind)^.reclen);
  191. if p^.rewind>=(cardinal(p^.buf)+dirblksiz) then
  192. novalid:=true;
  193. end
  194. else
  195. novalid:=true;
  196. if novalid then
  197. begin {block entirely searched or reclen=0}
  198. if p^.loc<>0 THEN {blocks left?}
  199. if readbuffer()<>0 then {succesful read?}
  200. novalid:=false;
  201. end;
  202. until (l<>nil) or novalid;
  203. If novalid then
  204. l:=nil;
  205. Sys_ReadDir:=l;
  206. end;
  207. {$endif}
  208. {*****************************************************************************
  209. --- Process:Process & program handling - related calls ---
  210. *****************************************************************************}
  211. Function sys_GetPid:LongInt;
  212. {
  213. Get Process ID.
  214. }
  215. begin
  216. sys_GetPID:=do_syscall(syscall_nr_getpid);
  217. end;
  218. Procedure Sys_Exit(ExitCode:longint);
  219. begin
  220. do_syscall(syscall_nr_exit,exitcode);
  221. end;
  222. {
  223. Change action of process upon receipt of a signal.
  224. Signum specifies the signal (all except SigKill and SigStop).
  225. If Act is non-nil, it is used to specify the new action.
  226. If OldAct is non-nil the previous action is saved there.
  227. }
  228. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  229. {
  230. Change action of process upon receipt of a signal.
  231. Signum specifies the signal (all except SigKill and SigStop).
  232. If Act is non-nil, it is used to specify the new action.
  233. If OldAct is non-nil the previous action is saved there.
  234. }
  235. begin
  236. do_syscall(syscall_nr_sigaction,longint(signum),longint(act),longint(oldact));
  237. {$ifdef linuxunit}
  238. LinuxError:=Errno;
  239. {$endif}
  240. end;
  241. (*=================== MOVED from syslinux.inc ========================*)
  242. Function Sys_FTruncate(Handle,Pos:longint):longint; //moved from sysunix.inc Do_Truncate
  243. begin
  244. Sys_FTruncate:=do_syscall(syscall_nr___syscall, syscall_nr_ftruncate,0,handle,0,pos,0);
  245. end;
  246. Function Sys_fstat(fd : longint;var Info:stat):Longint; // This was missing here, instead an fstat call was included in Do_FileSize
  247. begin
  248. Sys_FStat:=do_SysCall(syscall_nr_fstat,fd,longint(@info));
  249. end;
  250. {$ifdef NewReaddir}
  251. {$I readdir.inc}
  252. {$endif}
  253. {
  254. Interface to Unix ioctl call.
  255. Performs various operations on the filedescriptor Handle.
  256. Ndx describes the operation to perform.
  257. Data points to data needed for the Ndx function. The structure of this
  258. data is function-dependent.
  259. }
  260. Function Sys_IOCtl(Handle,Ndx: Longint;Data: Pointer):LongInt; // This was missing here, instead hardcoded in Do_IsDevice
  261. begin
  262. Sys_IOCtl:=do_SysCall(syscall_nr_ioctl,handle,Ndx,longint(data));
  263. end;
  264. Function Sys_mmap(adr,len,prot,flags,fdes,off:longint):longint; // moved from sysunix.inc, used in sbrk
  265. begin
  266. Sys_mmap:=do_syscall(syscall_nr_mmap,Adr,Len,Prot,Flags,fdes,off,0);
  267. end;
  268. {
  269. $Log$
  270. Revision 1.3 2003-01-21 15:39:45 marco
  271. * NetBSD first rtl. Still not 100%, but close
  272. Revision 1.2 2003/01/17 22:13:47 marco
  273. * some updates
  274. Revision 1.1.2.4 2002/09/20 07:04:44 pierre
  275. * avoid compiler warning
  276. Revision 1.1.2.3 2002/01/23 09:10:05 marco
  277. * Similar truncate test. truncate tests from testsuite now seem to pass
  278. Revision 1.1.2.2 2001/08/29 09:43:07 marco
  279. * first lseek fix. Still not 100%, but don't know exact problem yet.
  280. Revision 1.1.2.1 2001/08/14 20:22:53 pierre
  281. New file
  282. Revision 1.1.2.7 2001/04/04 10:38:36 marco
  283. * Small fix to readdir. Is this *the* bug?
  284. Revision 1.1.2.6 2001/03/14 17:19:10 marco
  285. * Readdir compiles (some conflicts in Linux). Untested. Use NewReaddir to enable
  286. Revision 1.1.2.5 2001/03/12 20:37:50 marco
  287. * [Solaris] Now cycles for FreeBSD (wrong version Linux unit commited)
  288. Revision 1.1.2.4 2001/03/12 14:57:38 marco
  289. * [solaris] added some sys functions to decrease amount of ifdefs needed
  290. Revision 1.1.2.3 2000/09/19 09:58:49 marco
  291. * Mkdir fix
  292. Revision 1.1.2.2 2000/09/18 12:14:41 marco
  293. * An addw in the do_syscall(integer) caused warnings. Fixed
  294. Revision 1.1.2.1 2000/09/16 11:19:08 marco
  295. * Moved files from BSD to FreeBSD directory, with some small changes
  296. Revision 1.1.2.1 2000/09/10 16:12:14 marco
  297. Initial signals, sockets and clone
  298. Revision 1.1 2000/07/13 06:30:32 michael
  299. + Initial import
  300. Revision 1.15 2000/04/16 16:08:53 marco
  301. * Fixes (mainly opendir/Readdir/closedir)
  302. Revision 1.14 2000/04/14 17:04:13 marco
  303. * Working!
  304. Revision 1.13 2000/04/10 15:46:52 marco
  305. * worked all day. probably a lot changed
  306. Revision 1.11 2000/04/05 13:58:40 marco
  307. * syscall variablenames reintroduced.
  308. Revision 1.10 2000/03/16 16:18:12 marco
  309. * Last changes before next test. ppc386 -h works with these srcs.
  310. Revision 1.9 2000/03/02 15:34:07 marco
  311. * added a syscall for 5 longints
  312. Revision 1.8 2000/03/01 20:03:57 marco
  313. * small fixes for syslinux
  314. Revision 1.7 2000/03/01 17:28:40 marco
  315. * some changes due to updating linux.pp to new syscall
  316. Revision 1.6 2000/02/27 23:45:39 marco
  317. * Redone the syscalls
  318. Revision 1.5 2000/02/04 16:53:26 marco
  319. * Finished Linux (and rest syscalls) roughly. Some things still need to be
  320. tested, and checked (off_t calls specially)
  321. Revision 1.4 2000/02/03 17:04:47 marco
  322. * additions fixes due to port linux
  323. Revision 1.3 2000/02/02 18:07:27 marco
  324. * Ported except for readdir which is 200 lines C code in FBSD linux
  325. emulator
  326. Revision 1.2 2000/02/02 16:35:10 marco
  327. * Ported more functions. Half done now.
  328. Revision 1.1 2000/02/02 15:41:56 marco
  329. * Initial BSD version. Still needs a lot of work.
  330. }