system.pp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. System unit for the *BSD's.
  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. { These things are set in the makefile, }
  13. { But you can override them here.}
  14. { If you use an aout system, set the conditional AOUT}
  15. { $Define AOUT}
  16. Unit System;
  17. Interface
  18. {$IFNDEF FPC_DISABLE_MONITOR}
  19. {$DEFINE SYSTEM_HAS_FEATURE_MONITOR}
  20. {$ENDIF}
  21. {$define FPC_USE_SIGPROCMASK}
  22. {$define FPC_USE_SIGALTSTACK}
  23. {$ifndef FPC_USE_LIBC}
  24. {$define FPC_USE_SYSCALL}
  25. {$endif}
  26. {$define FPC_IS_SYSTEM}
  27. {$I sysunixh.inc}
  28. {$ifdef Darwin}
  29. var argc:longint;
  30. argv:PPAnsiChar;
  31. envp:PPAnsiChar;
  32. {$endif}
  33. CONST SIGSTKSZ = 40960;
  34. {$if defined(CPUARM) or defined(CPUM68K)}
  35. {$define fpc_softfpu_interface}
  36. {$i softfpu.pp}
  37. {$undef fpc_softfpu_interface}
  38. {$endif defined(CPUARM) or defined(CPUM68K)}
  39. Implementation
  40. {$if defined(CPUARM) or defined(CPUM68K)}
  41. {$define fpc_softfpu_implementation}
  42. {$i softfpu.pp}
  43. {$undef fpc_softfpu_implementation}
  44. { we get these functions and types from the softfpu code }
  45. {$define FPC_SYSTEM_HAS_float64}
  46. {$define FPC_SYSTEM_HAS_float32}
  47. {$define FPC_SYSTEM_HAS_flag}
  48. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  49. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  50. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  51. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  52. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  53. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  54. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  55. {$endif defined(CPUARM) or defined(CPUM68K)}
  56. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  57. {$define FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  58. procedure OsSetupEntryInformation(constref info: TEntryInformation); forward;
  59. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  60. {$I system.inc}
  61. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  62. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  63. begin
  64. argc := info.OS.argc;
  65. argv := info.OS.argv;
  66. envp := info.OS.envp;
  67. initialstklen := info.OS.stklen;
  68. end;
  69. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  70. {$ifdef FPC_HAS_SETSYSNR_INC}
  71. {$I setsysnr.inc}
  72. {$endif FPC_HAS_SETSYSNR_INC}
  73. {*****************************************************************************
  74. Misc. System Dependent Functions
  75. *****************************************************************************}
  76. {$ifdef darwin}
  77. procedure normalexit(status: cint); cdecl; external 'c' name 'exit';
  78. {$endif}
  79. {$if defined(openbsd)}
  80. procedure haltproc; cdecl; external name '_haltproc';
  81. {$endif}
  82. procedure System_exit;
  83. {$if defined(darwin)}
  84. begin
  85. { make sure the libc atexit handlers are called, needed for e.g. profiling }
  86. normalexit(cint(ExitCode));
  87. end;
  88. {$elseif defined(openbsd)}
  89. begin
  90. haltproc;
  91. end;
  92. {$else}
  93. begin
  94. Fpexit(cint(ExitCode));
  95. end;
  96. {$endif}
  97. Function ParamCount: Longint;
  98. Begin
  99. Paramcount:=argc-1
  100. End;
  101. function BackPos(c:AnsiChar; const s: shortstring): integer;
  102. var
  103. i: integer;
  104. Begin
  105. for i:=length(s) downto 0 do
  106. if s[i] = c then break;
  107. if i=0 then
  108. BackPos := 0
  109. else
  110. BackPos := i;
  111. end;
  112. { variable where full path and filename and executable is stored }
  113. { is setup by the startup of the system unit. }
  114. //var
  115. // execpathstr : shortstring;
  116. function paramstr(l: longint) : shortstring;
  117. begin
  118. { stricly conforming POSIX applications }
  119. { have the executing filename as argv[0] }
  120. // if l=0 then
  121. // begin
  122. // paramstr := execpathstr;
  123. // end
  124. // else
  125. if (l >= 0) and (l < argc) then
  126. paramstr:=strpas(argv[l])
  127. else
  128. paramstr:='';
  129. end;
  130. Procedure Randomize;
  131. Begin
  132. randseed:=longint(Fptime(nil));
  133. End;
  134. {*****************************************************************************
  135. System Unit Initialization
  136. *****************************************************************************}
  137. function reenable_signal(sig : longint) : boolean;
  138. var
  139. e,oe : TSigSet;
  140. i,j : byte;
  141. olderrno: cint;
  142. begin
  143. fillchar(e,sizeof(e),#0);
  144. fillchar(oe,sizeof(oe),#0);
  145. { set is 1 based PM }
  146. dec(sig);
  147. i:=sig mod 32;
  148. j:=sig div 32;
  149. e[j]:=1 shl i;
  150. { this routine is called from a signal handler, so must not change errno }
  151. olderrno:=geterrno;
  152. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  153. reenable_signal:=geterrno=0;
  154. seterrno(olderrno);
  155. end;
  156. {$ifdef DEBUG}
  157. { Declare InstallDefaultSignalHandler as forward to be able
  158. to test aclling fpsigaction again within SignalToRunError
  159. function implemented within sighnd.inc inlcude file }
  160. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); forward;
  161. {$endif}
  162. {$i sighnd.inc}
  163. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  164. var
  165. act: SigActionRec;
  166. begin
  167. { Initialize the sigaction structure }
  168. { all flags and information set to zero }
  169. FillChar(act,sizeof(SigActionRec),0);
  170. { initialize handler }
  171. act.sa_handler:=@SignalToRunError;
  172. {$if defined(darwin) and defined(cpu64)}
  173. act.sa_flags:=SA_SIGINFO or SA_64REGSET;
  174. {$else}
  175. act.sa_flags:=SA_SIGINFO;
  176. {$endif}
  177. FpSigAction(signum,@act,@oldact);
  178. end;
  179. var
  180. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  181. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  182. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  183. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  184. Procedure InstallSignals;
  185. begin
  186. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  187. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  188. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  189. InstallDefaultSignalHandler(SIGILL,oldsigill);
  190. end;
  191. Procedure RestoreOldSignalHandlers;
  192. begin
  193. FpSigAction(SIGFPE,@oldsigfpe,nil);
  194. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  195. FpSigAction(SIGBUS,@oldsigbus,nil);
  196. FpSigAction(SIGILL,@oldsigill,nil);
  197. end;
  198. procedure SetupCmdLine;
  199. var
  200. bufsize,
  201. len,j,
  202. size,i : longint;
  203. found : boolean;
  204. buf : PAnsiChar;
  205. procedure AddBuf;
  206. begin
  207. reallocmem(cmdline,size+bufsize);
  208. move(buf^,cmdline[size],bufsize);
  209. inc(size,bufsize);
  210. bufsize:=0;
  211. end;
  212. begin
  213. GetMem(buf,ARG_MAX);
  214. size:=0;
  215. bufsize:=0;
  216. i:=0;
  217. while (i<argc) do
  218. begin
  219. len:=strlen(argv[i]);
  220. if len>ARG_MAX-2 then
  221. len:=ARG_MAX-2;
  222. found:=false;
  223. for j:=1 to len do
  224. if argv[i][j]=' ' then
  225. begin
  226. found:=true;
  227. break;
  228. end;
  229. if bufsize+len>=ARG_MAX-2 then
  230. AddBuf;
  231. if found then
  232. begin
  233. buf[bufsize]:='"';
  234. inc(bufsize);
  235. end;
  236. move(argv[i]^,buf[bufsize],len);
  237. inc(bufsize,len);
  238. if found then
  239. begin
  240. buf[bufsize]:='"';
  241. inc(bufsize);
  242. end;
  243. if i<argc-1 then
  244. buf[bufsize]:=' '
  245. else
  246. buf[bufsize]:=#0;
  247. inc(bufsize);
  248. inc(i);
  249. end;
  250. AddBuf;
  251. FreeMem(buf,ARG_MAX);
  252. end;
  253. procedure SysInitStdIO;
  254. begin
  255. OpenStdIO(Input,fmInput,StdInputHandle);
  256. OpenStdIO(Output,fmOutput,StdOutputHandle);
  257. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  258. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  259. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  260. end;
  261. {$ifdef FPC_USE_LIBC}
  262. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  263. {$ifdef Darwin}
  264. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  265. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  266. begin
  267. SetupEntryInformation(info);
  268. info.PascalMain();
  269. end;
  270. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  271. procedure pascalmain;external name '_PASCALMAIN';
  272. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: PPAnsiChar; envpparam: PPAnsiChar); cdecl; [public];
  273. begin
  274. argc:= argcparam;
  275. argv:= argvparam;
  276. envp:= envpparam;
  277. {$ifdef cpui386}
  278. Set8087CW(Default8087CW);
  279. {$endif cpui386}
  280. pascalmain; {run the pascal main program}
  281. end;
  282. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  283. {$endif Darwin}
  284. {$endif FPC_USE_LIBC}
  285. function GetProcessID: SizeUInt;
  286. begin
  287. GetProcessID := SizeUInt (fpGetPID);
  288. end;
  289. function InternalPageSize: SizeUInt; inline;
  290. begin
  291. {$ifndef darwin}
  292. InternalPageSize := 4096;
  293. {$else not darwin}
  294. InternalPageSize := darwin_page_size;
  295. {$endif not darwin}
  296. end;
  297. function AlignedStackTop: Pointer;
  298. begin
  299. AlignedStackTop:=Pointer((ptruint(sptr) + InternalPageSize - 1) and not(InternalPageSize - 1));
  300. end;
  301. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  302. var
  303. stackpointer: ptruint;
  304. begin
  305. stackpointer := ptruint(AlignedStackTop);
  306. if stklen > stackpointer then
  307. stklen := stackpointer - InternalPageSize;
  308. result := stklen;
  309. end;
  310. Begin
  311. {$ifdef darwin}
  312. darwin_init_page_size;
  313. {$endif darwin}
  314. IsConsole := TRUE;
  315. StackLength := CheckInitialStkLen(InitialStkLen);
  316. StackBottom := AlignedStackTop - StackLength;
  317. {$ifdef FPC_HAS_SETSYSNR_INC}
  318. { This procedure is needed for openbsd system which re-uses
  319. the same syscall numbers depending on OS version }
  320. SetSyscallNumbers;
  321. {$endif FPC_HAS_SETSYSNR_INC}
  322. { Set up signals handlers (may be needed by init code to test cpu features) }
  323. InstallSignals;
  324. {$if defined(cpui386) or defined(cpuarm)}
  325. fpc_cpucodeinit;
  326. {$endif cpui386}
  327. { Setup heap }
  328. InitHeap;
  329. SysInitExceptions;
  330. initunicodestringmanager;
  331. { Setup stdin, stdout and stderr }
  332. SysInitStdIO;
  333. { Reset IO Error }
  334. InOutRes:=0;
  335. { Arguments }
  336. SetupCmdLine;
  337. { threading }
  338. InitSystemThreads;
  339. InitSystemDynLibs;
  340. { restore original signal handlers in case this is a library }
  341. if IsLibrary then
  342. RestoreOldSignalHandlers;
  343. End.