syscalls.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. TYPE timeval=RECORD
  70. tv_sec,
  71. tv_used : int64;
  72. END;
  73. timezone=RECORD
  74. tz_minuteswest,
  75. tz_dsttime : LONGINT;
  76. END;
  77. function checkreturnvalue(retval:LONGINT;value:LONGINT):LONGINT;
  78. begin
  79. if retval<0 THEN
  80. begin
  81. errno:=-retval;
  82. checkreturnvalue:=-1;
  83. end
  84. else
  85. begin
  86. checkreturnvalue:=value;
  87. errno:=0
  88. end;
  89. end;
  90. Function Sys_Time:longint;
  91. VAR tv : timeval;
  92. tz : timezone;
  93. retval : longint;
  94. begin
  95. asm
  96. lea tv,%ebx
  97. pushl %ebx
  98. lea tz,%ecx
  99. pushl %ecx
  100. mov $116,%eax
  101. int $0x80
  102. add $8,%esp
  103. mov %eax,retval
  104. end;
  105. sys_time:=checkreturnvalue(retval,tv.tv_sec);
  106. end;
  107. {*****************************************************************************
  108. --- File:File handling related calls ---
  109. *****************************************************************************}
  110. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  111. var retval: LONGINT;
  112. Begin
  113. asm
  114. pushl mode
  115. pushl flags
  116. pushl f
  117. movl $5,%eax
  118. int $0x80
  119. add $12,%esp
  120. mov %eax,retval
  121. end;
  122. sys_open:=checkreturnvalue(retval,retval);
  123. End;
  124. Function Sys_Close(f:longint):longint;
  125. var retval: LONGINT;
  126. begin
  127. asm
  128. pushl f
  129. movl $6,%eax
  130. int $0x80
  131. addl $4,%esp
  132. mov %eax,retval
  133. end;
  134. Sys_Close:=checkreturnvalue(retval,retval);
  135. end;
  136. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  137. var retval: LONGINT;
  138. begin
  139. asm
  140. pushl Whence
  141. pushl Off
  142. pushl F
  143. mov $199,%eax
  144. int $0x80
  145. addl $12,%eax
  146. mov %eax,retval
  147. end;
  148. Sys_Lseek:=checkreturnvalue(retval,retval);
  149. end;
  150. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  151. var retval: LONGINT;
  152. begin
  153. asm
  154. pushl Count
  155. pushl Buffer
  156. pushl F
  157. mov $3,%eax
  158. int $0x80
  159. addl $12,%eax
  160. mov %eax,retval
  161. end;
  162. Sys_Read:=checkreturnvalue(retval,retval);
  163. end;
  164. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  165. var retval: LONGINT;
  166. begin
  167. asm
  168. pushl Count
  169. pushl Buffer
  170. pushl F
  171. mov $4,%eax
  172. int $0x80
  173. addl $12,%eax
  174. mov %eax,retval
  175. end;
  176. Sys_Write:=checkreturnvalue(retval,retval);
  177. end;
  178. Function Sys_Unlink(Filename:pchar):longint;
  179. var retval: LONGINT;
  180. begin
  181. asm
  182. pushl FileName
  183. mov $10,%eax
  184. int $0x80
  185. addl $4,%eax
  186. mov %eax,retval
  187. end;
  188. Sys_UnLink:=checkreturnvalue(retval,retval);
  189. end;
  190. Function Sys_Rename(Oldname,Newname:pchar):longint;
  191. var retval: LONGINT;
  192. begin
  193. asm
  194. pushl NewName
  195. pushl OldName
  196. mov $38,%eax
  197. int $0x80
  198. addl $8,%eax
  199. mov %eax,retval
  200. end;
  201. Sys_Rename:=checkreturnvalue(retval,retval);
  202. end;
  203. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  204. {
  205. We need this for getcwd
  206. }
  207. var retval: LONGINT;
  208. begin
  209. asm
  210. pushl buffer
  211. pushl FileName
  212. mov $188,%eax
  213. int $0x80
  214. addl $8,%eax
  215. mov %eax,retval
  216. end;
  217. Sys_Stat:=checkreturnvalue(retval,retval);
  218. end;
  219. Function Sys_Symlink(oldname,newname:pchar):longint;
  220. {
  221. We need this for erase
  222. }
  223. var retval : longint;
  224. begin
  225. asm
  226. pushl newname
  227. pushl oldname
  228. mov $57,%eax
  229. int $0x80
  230. addl $8,%eax
  231. mov %eax,retval
  232. end;
  233. Sys_Symlink:=checkreturnvalue(retval,retval);
  234. end;
  235. {*****************************************************************************
  236. --- Directory:Directory related calls ---
  237. *****************************************************************************}
  238. Function Sys_Chdir(Filename:pchar):longint;
  239. var retval : longint;
  240. begin
  241. asm
  242. pushl FileName
  243. mov $12,%eax
  244. int $0x80
  245. addl $4,%eax
  246. mov %eax,retval
  247. end;
  248. Sys_ChDir:=checkreturnvalue(retval,retval);
  249. end;
  250. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  251. var retval : longint;
  252. begin {Mode is 16-bit on F-BSD}
  253. asm
  254. mov mode,%eax
  255. pushw %ax
  256. pushl FileName
  257. mov $136,%eax
  258. int $0x80
  259. addl $6,%eax
  260. mov %eax,retval
  261. end;
  262. Sys_MkDir:=checkreturnvalue(retval,retval);
  263. end;
  264. Function Sys_Rmdir(Filename:pchar):longint;
  265. var retval : longint;
  266. begin
  267. asm
  268. pushl FileName
  269. mov $137,%eax
  270. int $0x80
  271. addl $4,%eax
  272. mov %eax,retval
  273. end;
  274. Sys_ChDir:=checkreturnvalue(retval,retval);
  275. end;
  276. { we need this for getcwd, NOT touched for BSD version }
  277. Function OpenDir(f:pchar):pdir;
  278. var
  279. fd:integer;
  280. st:stat;
  281. ptr:pdir;
  282. begin
  283. opendir:=nil;
  284. if sys_stat(f,st)<0 then
  285. exit;
  286. { Is it a dir ? }
  287. if not((st.mode and $f000)=$4000)then
  288. begin
  289. errno:=sys_enotdir;
  290. exit
  291. end;
  292. { Open it}
  293. fd:=sys_open(f,OPEN_RDONLY,438);
  294. if fd<0 then
  295. exit;
  296. new(ptr);
  297. if ptr=nil then
  298. exit;
  299. new(ptr^.buf);
  300. if ptr^.buf=nil then
  301. exit;
  302. ptr^.fd:=fd;
  303. ptr^.loc:=0;
  304. ptr^.size:=0;
  305. ptr^.dd_max:=sizeof(ptr^.buf^);
  306. opendir:=ptr;
  307. end;
  308. function CloseDir(p:pdir):integer;
  309. begin
  310. closedir:=sys_close(p^.fd);
  311. dispose(p^.buf);
  312. dispose(p);
  313. end;
  314. Function Sys_ReadDir(p:pdir):pdirent;
  315. var
  316. regs :SysCallregs;
  317. dummy:longint;
  318. begin
  319. regs.reg3:=longint(p^.buf);
  320. regs.reg2:=p^.fd;
  321. regs.reg4:=1;
  322. dummy:=SysCall(SysCall_nr_readdir,regs);
  323. { the readdir system call returns the number of bytes written }
  324. if dummy=0 then
  325. sys_readdir:=nil
  326. else
  327. sys_readdir:=p^.buf
  328. end;
  329. {*****************************************************************************
  330. --- Process:Process & program handling - related calls ---
  331. *****************************************************************************}
  332. Procedure Sys_Exit(ExitCode:Integer);
  333. var retval : longint;
  334. begin
  335. asm
  336. pushl ExitCode
  337. mov $1,%eax
  338. int $0x80
  339. addl $4,%eax
  340. mov %eax,retval
  341. end;
  342. Sys_Exit:=checkreturnvalue(retval,retval);
  343. end;
  344. {
  345. $Log$
  346. Revision 1.3 2000-02-02 18:07:27 marco
  347. * Ported except for readdir which is 200 lines C code in FBSD linux
  348. emulator
  349. Revision 1.2 2000/02/02 16:35:10 marco
  350. * Ported more functions. Half done now.
  351. Revision 1.1 2000/02/02 15:41:56 marco
  352. * Initial BSD version. Still needs a lot of work.
  353. }