syscalls.inc 12 KB

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