syscalls.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 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. {*****************************************************************************
  221. --- Directory:Directory related calls ---
  222. *****************************************************************************}
  223. Function Sys_Chdir(Filename:pchar):longint;
  224. var
  225. regs : SysCallregs;
  226. begin
  227. regs.reg2:=longint(filename);
  228. Sys_ChDir:=SysCall(SysCall_nr_chdir,regs);
  229. end;
  230. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  231. var
  232. regs : SysCallregs;
  233. begin
  234. regs.reg2:=longint(filename);
  235. regs.reg3:=mode;
  236. Sys_MkDir:=SysCall(SysCall_nr_mkdir,regs);
  237. end;
  238. Function Sys_Rmdir(Filename:pchar):longint;
  239. var
  240. regs : SysCallregs;
  241. begin
  242. regs.reg2:=longint(filename);
  243. Sys_Rmdir:=SysCall(SysCall_nr_rmdir,regs);
  244. end;
  245. { we need this for getcwd }
  246. Function OpenDir(f:pchar):pdir;
  247. var
  248. fd:integer;
  249. st:stat;
  250. ptr:pdir;
  251. begin
  252. opendir:=nil;
  253. if sys_stat(f,st)<0 then
  254. exit;
  255. { Is it a dir ? }
  256. if not((st.mode and $f000)=$4000)then
  257. begin
  258. errno:=sys_enotdir;
  259. exit
  260. end;
  261. { Open it}
  262. fd:=sys_open(f,OPEN_RDONLY,438);
  263. if fd<0 then
  264. exit;
  265. new(ptr);
  266. if ptr=nil then
  267. exit;
  268. new(ptr^.buf);
  269. if ptr^.buf=nil then
  270. exit;
  271. ptr^.fd:=fd;
  272. ptr^.loc:=0;
  273. ptr^.size:=0;
  274. ptr^.dd_max:=sizeof(ptr^.buf^);
  275. opendir:=ptr;
  276. end;
  277. function CloseDir(p:pdir):integer;
  278. begin
  279. closedir:=sys_close(p^.fd)
  280. end;
  281. Function Sys_ReadDir(p:pdir):pdirent;
  282. var
  283. regs :SysCallregs;
  284. dummy:longint;
  285. begin
  286. regs.reg3:=longint(p^.buf);
  287. regs.reg2:=p^.fd;
  288. regs.reg4:=1;
  289. dummy:=SysCall(SysCall_nr_readdir,regs);
  290. { the readdir system call returns the number of bytes written }
  291. if dummy=0 then
  292. sys_readdir:=nil
  293. else
  294. sys_readdir:=p^.buf
  295. end;
  296. {*****************************************************************************
  297. --- Process:Process & program handling - related calls ---
  298. *****************************************************************************}
  299. Procedure Sys_Exit(ExitCode:Integer);
  300. var
  301. regs : SysCallregs;
  302. begin
  303. regs.reg2:=exitcode;
  304. SysCall(SysCall_nr_exit,regs)
  305. end;
  306. {
  307. $Log$
  308. Revision 1.3 1998-05-30 14:18:42 peter
  309. * fixed to remake with -Rintel in the ppc386.cfg
  310. Revision 1.2 1998/05/06 12:38:22 michael
  311. + Removed log from before restored version.
  312. Revision 1.1.1.1 1998/03/25 11:18:43 root
  313. * Restored version
  314. }