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