system.pp 9.5 KB

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