syscalls.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. {No debugging for syslinux include !}
  13. {$IFDEF SYS_LINUX}
  14. {$UNDEF SYSCALL_DEBUG}
  15. {$ENDIF SYS_LINUX}
  16. {*****************************************************************************
  17. --- Main:The System Call Self ---
  18. *****************************************************************************}
  19. Procedure Do_SysCall( callnr:longint;var regs : SysCallregs );assembler;
  20. {
  21. This function puts the registers in place, does the call, and then
  22. copies back the registers as they are after the SysCall.
  23. }
  24. {$ifdef i386}
  25. {$ASMMODE ATT}
  26. asm
  27. { load the registers... }
  28. movl 12(%ebp),%eax
  29. movl 4(%eax),%ebx
  30. movl 8(%eax),%ecx
  31. movl 12(%eax),%edx
  32. movl 16(%eax),%esi
  33. movl 20(%eax),%edi
  34. { set the call number }
  35. movl 8(%ebp),%eax
  36. { Go ! }
  37. int $0x80
  38. { Put back the registers... }
  39. pushl %eax
  40. movl 12(%ebp),%eax
  41. movl %edi,20(%eax)
  42. movl %esi,16(%eax)
  43. movl %edx,12(%eax)
  44. movl %ecx,8(%eax)
  45. movl %ebx,4(%eax)
  46. popl %ebx
  47. movl %ebx,(%eax)
  48. end;
  49. {$ASMMODE DEFAULT}
  50. {$else}
  51. {$ifdef m68k}
  52. asm
  53. { load the registers... }
  54. move.l 12(a6),a0
  55. move.l 4(a0),d1
  56. move.l 8(a0),d2
  57. move.l 12(a0),d3
  58. move.l 16(a0),d4
  59. move.l 20(a0),d5
  60. { set the call number }
  61. move.l 8(a6),d0
  62. { Go ! }
  63. trap #0
  64. { Put back the registers... }
  65. move.l d0,-(sp)
  66. move.l 12(a6),a0
  67. move.l d5,20(a0)
  68. move.l d4,16(a0)
  69. move.l d3,12(a0)
  70. move.l d2,8(a0)
  71. move.l d1,4(a0)
  72. move.l (sp)+,d1
  73. move.l d1,(a0)
  74. end;
  75. {$else}
  76. {$error Cannot decide which processor you have ! define i386 or m68k }
  77. {$endif}
  78. {$endif}
  79. {$IFDEF SYSCALL_DEBUG}
  80. Const
  81. DoSysCallDebug : Boolean = False;
  82. var
  83. LastCnt,
  84. LastEax,
  85. LastCall : longint;
  86. DebugTxt : string[20];
  87. {$ENDIF}
  88. Function SysCall( callnr:longint;var regs : SysCallregs ):longint;
  89. {
  90. This function serves as an interface to do_SysCall.
  91. If the SysCall returned a negative number, it returns -1, and puts the
  92. SysCall result in errno. Otherwise, it returns the SysCall return value
  93. }
  94. begin
  95. do_SysCall(callnr,regs);
  96. if regs.reg1<0 then
  97. begin
  98. {$IFDEF SYSCALL_DEBUG}
  99. If DoSysCallDebug then
  100. debugtxt:=' syscall error: ';
  101. {$endif}
  102. ErrNo:=-regs.reg1;
  103. SysCall:=-1;
  104. end
  105. else
  106. begin
  107. {$IFDEF SYSCALL_DEBUG}
  108. if DoSysCallDebug then
  109. debugtxt:=' syscall returned: ';
  110. {$endif}
  111. SysCall:=regs.reg1;
  112. errno:=0
  113. end;
  114. {$IFDEF SYSCALL_DEBUG}
  115. if DoSysCallDebug then
  116. begin
  117. inc(lastcnt);
  118. if (callnr<>lastcall) or (regs.reg1<>lasteax) then
  119. begin
  120. if lastcnt>1 then
  121. writeln(sys_nr_txt[lastcall],debugtxt,lasteax,' (',lastcnt,'x)');
  122. lastcall:=callnr;
  123. lasteax:=regs.reg1;
  124. lastcnt:=0;
  125. writeln(sys_nr_txt[lastcall],debugtxt,lasteax);
  126. end;
  127. end;
  128. {$endif}
  129. end;
  130. Function Sys_Time:longint;
  131. var
  132. regs : SysCallregs;
  133. begin
  134. regs.reg2:=0;
  135. Sys_Time:=SysCall(SysCall_nr_time,regs);
  136. end;
  137. {*****************************************************************************
  138. --- File:File handling related calls ---
  139. *****************************************************************************}
  140. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  141. var
  142. regs : SysCallregs;
  143. Begin
  144. regs.reg2:=longint(f);
  145. regs.reg3:=flags;
  146. regs.reg4:=mode;
  147. Sys_Open:=SysCall(SysCall_nr_open,regs);
  148. End;
  149. Function Sys_Close(f:longint):longint;
  150. var
  151. regs : SysCallregs;
  152. begin
  153. regs.reg2:=f;
  154. Sys_Close:=SysCall(SysCall_nr_close,regs);
  155. end;
  156. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  157. var
  158. regs : SysCallregs;
  159. begin
  160. regs.reg2:=f;
  161. regs.reg3:=off;
  162. regs.reg4:=Whence;
  163. Sys_lseek:=SysCall(SysCall_nr_lseek,regs);
  164. end;
  165. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  166. var
  167. regs : SysCallregs;
  168. begin
  169. regs.reg2:=f;
  170. regs.reg3:=longint(buffer);
  171. regs.reg4:=count;
  172. Sys_Read:=SysCall(SysCall_nr_read,regs);
  173. end;
  174. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  175. var
  176. regs : SysCallregs;
  177. begin
  178. regs.reg2:=f;
  179. regs.reg3:=longint(buffer);
  180. regs.reg4:=count;
  181. Sys_Write:=SysCall(SysCall_nr_write,regs);
  182. end;
  183. Function Sys_Unlink(Filename:pchar):longint;
  184. var
  185. regs : SysCallregs;
  186. begin
  187. regs.reg2:=longint(filename);
  188. Sys_Unlink:=SysCall(SysCall_nr_unlink,regs);
  189. end;
  190. Function Sys_Rename(Oldname,Newname:pchar):longint;
  191. var
  192. regs : SysCallregs;
  193. begin
  194. regs.reg2:=longint(oldname);
  195. regs.reg3:=longint(newname);
  196. Sys_Rename:=SysCall(SysCall_nr_rename,regs);
  197. end;
  198. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  199. {
  200. We need this for getcwd
  201. }
  202. var
  203. regs : SysCallregs;
  204. begin
  205. regs.reg2:=longint(filename);
  206. regs.reg3:=longint(@buffer);
  207. Sys_Stat:=SysCall(SysCall_nr_stat,regs);
  208. end;
  209. Function Sys_Symlink(oldname,newname:pchar):longint;
  210. {
  211. We need this for erase
  212. }
  213. var
  214. regs : SysCallregs;
  215. begin
  216. regs.reg2:=longint(oldname);
  217. regs.reg3:=longint(newname);
  218. Sys_symlink:=SysCall(SysCall_nr_symlink,regs);
  219. end;
  220. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint;
  221. var
  222. regs : SysCallRegs;
  223. begin
  224. regs.reg2:=longint(name);
  225. regs.reg3:=longint(linkname);
  226. regs.reg4:=maxlen;
  227. Sys_ReadLink:=SysCall(Syscall_nr_readlink,regs);
  228. end;
  229. {*****************************************************************************
  230. --- Directory:Directory related calls ---
  231. *****************************************************************************}
  232. Function Sys_Chdir(Filename:pchar):longint;
  233. var
  234. regs : SysCallregs;
  235. begin
  236. regs.reg2:=longint(filename);
  237. Sys_ChDir:=SysCall(SysCall_nr_chdir,regs);
  238. end;
  239. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  240. var
  241. regs : SysCallregs;
  242. begin
  243. regs.reg2:=longint(filename);
  244. regs.reg3:=mode;
  245. Sys_MkDir:=SysCall(SysCall_nr_mkdir,regs);
  246. end;
  247. Function Sys_Rmdir(Filename:pchar):longint;
  248. var
  249. regs : SysCallregs;
  250. begin
  251. regs.reg2:=longint(filename);
  252. Sys_Rmdir:=SysCall(SysCall_nr_rmdir,regs);
  253. end;
  254. { we need this for getcwd }
  255. Function OpenDir(f:pchar):pdir;
  256. var
  257. fd:integer;
  258. st:stat;
  259. ptr:pdir;
  260. begin
  261. opendir:=nil;
  262. if sys_stat(f,st)<0 then
  263. exit;
  264. { Is it a dir ? }
  265. if not((st.mode and $f000)=$4000)then
  266. begin
  267. errno:=sys_enotdir;
  268. exit
  269. end;
  270. { Open it}
  271. fd:=sys_open(f,OPEN_RDONLY,438);
  272. if fd<0 then
  273. exit;
  274. new(ptr);
  275. if ptr=nil then
  276. exit;
  277. new(ptr^.buf);
  278. if ptr^.buf=nil then
  279. exit;
  280. ptr^.fd:=fd;
  281. ptr^.loc:=0;
  282. ptr^.size:=0;
  283. ptr^.dd_max:=sizeof(ptr^.buf^);
  284. opendir:=ptr;
  285. end;
  286. function CloseDir(p:pdir):integer;
  287. begin
  288. closedir:=sys_close(p^.fd);
  289. dispose(p^.buf);
  290. dispose(p);
  291. end;
  292. Function Sys_ReadDir(p:pdir):pdirent;
  293. var
  294. regs :SysCallregs;
  295. dummy:longint;
  296. begin
  297. regs.reg3:=longint(p^.buf);
  298. regs.reg2:=p^.fd;
  299. regs.reg4:=1;
  300. dummy:=SysCall(SysCall_nr_readdir,regs);
  301. { the readdir system call returns the number of bytes written }
  302. if dummy=0 then
  303. sys_readdir:=nil
  304. else
  305. sys_readdir:=p^.buf
  306. end;
  307. {*****************************************************************************
  308. --- Process:Process & program handling - related calls ---
  309. *****************************************************************************}
  310. Function Sys_GetPid:LongInt;
  311. var
  312. regs : SysCallregs;
  313. begin
  314. Sys_GetPid:=SysCall(SysCall_nr_getpid,regs);
  315. end;
  316. Procedure Sys_Exit(ExitCode:Integer);
  317. var
  318. regs : SysCallregs;
  319. begin
  320. regs.reg2:=exitcode;
  321. SysCall(SysCall_nr_exit,regs)
  322. end;
  323. {
  324. $Log$
  325. Revision 1.10 2000-02-09 16:59:32 peter
  326. * truncated log
  327. Revision 1.9 2000/02/08 11:47:09 peter
  328. * paramstr(0) support
  329. Revision 1.8 2000/01/07 16:41:41 daniel
  330. * copyright 2000
  331. Revision 1.7 2000/01/07 16:32:28 daniel
  332. * copyright 2000 added
  333. Revision 1.6 1999/07/28 17:37:06 michael
  334. * forgot ;
  335. Revision 1.5 1999/07/28 12:15:16 michael
  336. * Memory leak fixed in CloseDir, by Sebastian Guenther
  337. Revision 1.4 1999/07/28 12:14:37 michael
  338. * Memory leak fixed in CloseDir, by Sebastian Guenther
  339. }