system.pp 9.9 KB

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