syscalls.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. { Include syscall itself }
  13. {$i syscallo.inc}
  14. {*****************************************************************************
  15. --- Time:Time handling related calls ---
  16. *****************************************************************************}
  17. Function Sys_Time:longint;
  18. var
  19. regs : SysCallregs;
  20. begin
  21. regs.reg2:=0;
  22. Sys_Time:=SysCall(SysCall_nr_time,regs);
  23. end;
  24. {*****************************************************************************
  25. --- File:File handling related calls ---
  26. *****************************************************************************}
  27. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  28. var
  29. regs : SysCallregs;
  30. Begin
  31. regs.reg2:=longint(f);
  32. regs.reg3:=flags;
  33. regs.reg4:=mode;
  34. Sys_Open:=SysCall(SysCall_nr_open,regs);
  35. End;
  36. Function Sys_Close(f:longint):longint;
  37. var
  38. regs : SysCallregs;
  39. begin
  40. regs.reg2:=f;
  41. Sys_Close:=SysCall(SysCall_nr_close,regs);
  42. end;
  43. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  44. var
  45. regs : SysCallregs;
  46. begin
  47. regs.reg2:=f;
  48. regs.reg3:=off;
  49. regs.reg4:=Whence;
  50. Sys_lseek:=SysCall(SysCall_nr_lseek,regs);
  51. end;
  52. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  53. var
  54. regs : SysCallregs;
  55. begin
  56. regs.reg2:=f;
  57. regs.reg3:=longint(buffer);
  58. regs.reg4:=count;
  59. Sys_Read:=SysCall(SysCall_nr_read,regs);
  60. end;
  61. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  62. var
  63. regs : SysCallregs;
  64. begin
  65. regs.reg2:=f;
  66. regs.reg3:=longint(buffer);
  67. regs.reg4:=count;
  68. Sys_Write:=SysCall(SysCall_nr_write,regs);
  69. end;
  70. Function Sys_Unlink(Filename:pchar):longint;
  71. var
  72. regs : SysCallregs;
  73. begin
  74. regs.reg2:=longint(filename);
  75. Sys_Unlink:=SysCall(SysCall_nr_unlink,regs);
  76. end;
  77. Function Sys_fstat(fd : longint;var Info:stat):Longint;
  78. var
  79. regs : SysCallregs;
  80. begin
  81. regs.reg2:=fd;
  82. regs.reg3:=longint(@Info);
  83. Sys_fStat:=SysCall(SysCall_nr_fstat,regs);
  84. end;
  85. Function Sys_Rename(Oldname,Newname:pchar):longint;
  86. var
  87. regs : SysCallregs;
  88. begin
  89. regs.reg2:=longint(oldname);
  90. regs.reg3:=longint(newname);
  91. Sys_Rename:=SysCall(SysCall_nr_rename,regs);
  92. end;
  93. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  94. {
  95. We need this for getcwd
  96. }
  97. var
  98. regs : SysCallregs;
  99. begin
  100. regs.reg2:=longint(filename);
  101. regs.reg3:=longint(@buffer);
  102. Sys_Stat:=SysCall(SysCall_nr_stat,regs);
  103. end;
  104. Function Sys_Symlink(oldname,newname:pchar):longint;
  105. {
  106. We need this for erase
  107. }
  108. var
  109. regs : SysCallregs;
  110. begin
  111. regs.reg2:=longint(oldname);
  112. regs.reg3:=longint(newname);
  113. Sys_symlink:=SysCall(SysCall_nr_symlink,regs);
  114. end;
  115. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint;
  116. var
  117. regs : SysCallRegs;
  118. begin
  119. regs.reg2:=longint(name);
  120. regs.reg3:=longint(linkname);
  121. regs.reg4:=maxlen;
  122. Sys_ReadLink:=SysCall(Syscall_nr_readlink,regs);
  123. end;
  124. {*****************************************************************************
  125. --- Directory:Directory related calls ---
  126. *****************************************************************************}
  127. Function Sys_Chdir(Filename:pchar):longint;
  128. var
  129. regs : SysCallregs;
  130. begin
  131. regs.reg2:=longint(filename);
  132. Sys_ChDir:=SysCall(SysCall_nr_chdir,regs);
  133. end;
  134. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  135. var
  136. regs : SysCallregs;
  137. begin
  138. regs.reg2:=longint(filename);
  139. regs.reg3:=mode;
  140. Sys_MkDir:=SysCall(SysCall_nr_mkdir,regs);
  141. end;
  142. Function Sys_Rmdir(Filename:pchar):longint;
  143. var
  144. regs : SysCallregs;
  145. begin
  146. regs.reg2:=longint(filename);
  147. Sys_Rmdir:=SysCall(SysCall_nr_rmdir,regs);
  148. end;
  149. { we need this for getcwd }
  150. Function OpenDir(f:pchar):pdir;
  151. var
  152. fd:integer;
  153. st:stat;
  154. ptr:pdir;
  155. begin
  156. opendir:=nil;
  157. if sys_stat(f,st)<0 then
  158. exit;
  159. { Is it a dir ? }
  160. if not((st.mode and $f000)=$4000)then
  161. begin
  162. errno:=ESysENOTDIR;
  163. exit
  164. end;
  165. { Open it}
  166. fd:=sys_open(f,OPEN_RDONLY,438);
  167. if fd<0 then
  168. exit;
  169. new(ptr);
  170. if ptr=nil then
  171. exit;
  172. new(ptr^.buf);
  173. if ptr^.buf=nil then
  174. exit;
  175. ptr^.fd:=fd;
  176. ptr^.loc:=0;
  177. ptr^.size:=0;
  178. ptr^.dd_max:=sizeof(ptr^.buf^);
  179. opendir:=ptr;
  180. end;
  181. function CloseDir(p:pdir):integer;
  182. begin
  183. closedir:=sys_close(p^.fd);
  184. dispose(p^.buf);
  185. dispose(p);
  186. end;
  187. Function Sys_ReadDir(p:pdir):pdirent;
  188. var
  189. regs :SysCallregs;
  190. dummy:longint;
  191. begin
  192. regs.reg3:=longint(p^.buf);
  193. regs.reg2:=p^.fd;
  194. regs.reg4:=1;
  195. dummy:=SysCall(SysCall_nr_readdir,regs);
  196. { the readdir system call returns the number of bytes written }
  197. if dummy=0 then
  198. sys_readdir:=nil
  199. else
  200. sys_readdir:=p^.buf
  201. end;
  202. {*****************************************************************************
  203. --- Process:Process & program handling - related calls ---
  204. *****************************************************************************}
  205. Function Sys_GetPid:LongInt;
  206. var
  207. regs : SysCallregs;
  208. begin
  209. Sys_GetPid:=SysCall(SysCall_nr_getpid,regs);
  210. end;
  211. Procedure Sys_Exit(ExitCode:Integer);
  212. var
  213. regs : SysCallregs;
  214. begin
  215. regs.reg2:=exitcode;
  216. SysCall(SysCall_nr_exit,regs)
  217. end;
  218. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  219. {
  220. Change action of process upon receipt of a signal.
  221. Signum specifies the signal (all except SigKill and SigStop).
  222. If Act is non-nil, it is used to specify the new action.
  223. If OldAct is non-nil the previous action is saved there.
  224. }
  225. Var
  226. sr : Syscallregs;
  227. begin
  228. sr.reg2:=Signum;
  229. sr.reg3:=Longint(act);
  230. sr.reg4:=Longint(oldact);
  231. SysCall(Syscall_nr_sigaction,sr);
  232. end;
  233. function Sys_FTruncate(Handle,Pos:longint):longint; //moved from sysunix.inc Do_Truncate
  234. var
  235. sr : syscallregs;
  236. begin
  237. sr.reg2:=Handle;
  238. sr.reg3:=Pos;
  239. Sys_FTruncate:=syscall(syscall_nr_ftruncate,sr);
  240. end;
  241. Function Sys_mmap(adr,len,prot,flags,fdes,off:longint):longint; // moved from sysunix.inc, used in sbrk
  242. type
  243. tmmapargs=packed record
  244. address : longint;
  245. size : longint;
  246. prot : longint;
  247. flags : longint;
  248. fd : longint;
  249. offset : longint;
  250. end;
  251. var
  252. t : syscallregs;
  253. mmapargs : tmmapargs;
  254. begin
  255. mmapargs.address:=adr;
  256. mmapargs.size:=len;
  257. mmapargs.prot:=prot;
  258. mmapargs.flags:=flags;
  259. mmapargs.fd:=fdes;
  260. mmapargs.offset:=off;
  261. t.reg2:=longint(@mmapargs);
  262. Sys_mmap:=syscall(syscall_nr_mmap,t);
  263. end;
  264. Function Sys_munmap(adr,len:longint):longint; // moved from sysunix.inc, used in sbrk
  265. var
  266. t : syscallregs;
  267. begin
  268. t.reg2:=adr;
  269. t.reg3:=len;
  270. Sys_munmap:=syscall(syscall_nr_munmap,t);
  271. end;
  272. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  273. begin
  274. if (pointer(func)=nil) or (sp=nil) then
  275. exit(-1); // give an error result
  276. {$ifdef i386}
  277. asm
  278. { Insert the argument onto the new stack. }
  279. movl sp,%ecx
  280. subl $8,%ecx
  281. movl args,%eax
  282. movl %eax,4(%ecx)
  283. { Save the function pointer as the zeroth argument.
  284. It will be popped off in the child in the ebx frobbing below. }
  285. movl func,%eax
  286. movl %eax,0(%ecx)
  287. { Do the system call }
  288. pushl %ebx
  289. movl flags,%ebx
  290. movl SysCall_nr_clone,%eax
  291. int $0x80
  292. popl %ebx
  293. test %eax,%eax
  294. jnz .Lclone_end
  295. { We're in the new thread }
  296. subl %ebp,%ebp { terminate the stack frame }
  297. call *%ebx
  298. { exit process }
  299. movl %eax,%ebx
  300. movl $1,%eax
  301. int $0x80
  302. .Lclone_end:
  303. movl %eax,__RESULT
  304. end;
  305. {$endif i386}
  306. {$ifdef m68k}
  307. { No yet translated, my m68k assembler is too weak for such things PM }
  308. (*
  309. asm
  310. { Insert the argument onto the new stack. }
  311. movl sp,%ecx
  312. subl $8,%ecx
  313. movl args,%eax
  314. movl %eax,4(%ecx)
  315. { Save the function pointer as the zeroth argument.
  316. It will be popped off in the child in the ebx frobbing below. }
  317. movl func,%eax
  318. movl %eax,0(%ecx)
  319. { Do the system call }
  320. pushl %ebx
  321. movl flags,%ebx
  322. movl SysCall_nr_clone,%eax
  323. int $0x80
  324. popl %ebx
  325. test %eax,%eax
  326. jnz .Lclone_end
  327. { We're in the new thread }
  328. subl %ebp,%ebp { terminate the stack frame }
  329. call *%ebx
  330. { exit process }
  331. movl %eax,%ebx
  332. movl $1,%eax
  333. int $0x80
  334. .Lclone_end:
  335. movl %eax,__RESULT
  336. end;
  337. *)
  338. {$endif m68k}
  339. end;
  340. {
  341. Interface to Unix ioctl call.
  342. Performs various operations on the filedescriptor Handle.
  343. Ndx describes the operation to perform.
  344. Data points to data needed for the Ndx function. The structure of this
  345. data is function-dependent.
  346. }
  347. Function Sys_IOCtl(Handle,Ndx: Longint;Data: Pointer):LongInt; // This was missing here, instead hardcode in Do_IsDevice
  348. var
  349. sr: SysCallRegs;
  350. begin
  351. sr.reg2:=Handle;
  352. sr.reg3:=Ndx;
  353. sr.reg4:=Longint(Data);
  354. Sys_IOCtl:=SysCall(Syscall_nr_ioctl,sr);
  355. end;
  356. {
  357. $Log$
  358. Revision 1.17 2002-12-18 16:43:26 marco
  359. * new unix rtl, linux part.....
  360. Revision 1.16 2002/11/11 21:40:26 marco
  361. * rename syscall.inc -> syscallo.inc
  362. Revision 1.15 2002/10/14 19:39:17 peter
  363. * threads unit added for thread support
  364. Revision 1.14 2002/09/10 21:32:14 jonas
  365. + added "nop" after sc instruction, since normally in case of success,
  366. sc returns to the second instruction after itself
  367. Revision 1.13 2002/09/07 16:01:19 peter
  368. * old logs removed and tabs fixed
  369. Revision 1.12 2002/09/07 13:14:04 florian
  370. * hopefully final fix for ppc syscall BTW: The regX numbering is somehow messy
  371. Revision 1.11 2002/09/03 21:37:54 florian
  372. * hopefully final fix for ppc syscall
  373. Revision 1.10 2002/09/02 20:42:22 florian
  374. * another ppc syscall fix
  375. Revision 1.9 2002/09/02 20:03:20 florian
  376. * ppc syscall code fixed
  377. Revision 1.8 2002/08/19 18:24:05 jonas
  378. + ppc support for do_syscall
  379. Revision 1.7 2002/07/29 21:28:17 florian
  380. * several fixes to get further with linux/ppc system unit compilation
  381. Revision 1.6 2002/07/28 20:43:48 florian
  382. * several fixes for linux/powerpc
  383. * several fixes to MT
  384. }