system.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 Linux.
  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. {*****************************************************************************}
  18. interface
  19. {*****************************************************************************}
  20. {$define FPC_IS_SYSTEM}
  21. {$define HAS_CMDLINE}
  22. {$define USE_NOTHREADMANAGER}
  23. {$ifdef CPUM68K}
  24. {$define FPC_68K_SYSTEM_HAS_FPU_EXCEPTIONS}
  25. {$endif}
  26. {$i osdefs.inc}
  27. {$I sysunixh.inc}
  28. {$if defined(VER3_0) and defined(CPUX86_64)}
  29. {$define FPC_BOOTSTRAP_INDIRECT_ENTRY}
  30. const
  31. { this constant only exists during bootstrapping of the RTL with FPC 3.0.x,
  32. so that the whole condition doesn't need to be repeated in si_intf }
  33. indirect_bootstrap = true;
  34. {$endif defined(VER3_0) and defined(CPUX86_64)}
  35. function get_cmdline:Pchar; deprecated 'use paramstr' ;
  36. property cmdline:Pchar read get_cmdline;
  37. {$if defined(CPURISCV32) or defined(CPURISCV64) or defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6)) or defined(CPUXTENSA)}
  38. {$define FPC_LOAD_SOFTFPU}
  39. {$endif defined(CPURISCV32) or defined(CPURISCV64) or defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6)) or defined(CPUXTENSA)}
  40. {$ifdef FPC_SOFT_FPUX80}
  41. {$define FPC_SOFTFLOAT_FLOATX80}
  42. {$define LOAD_SOFTFPU}
  43. {$endif}
  44. {$ifdef FPC_SOFT_FPU128}
  45. {$define FPC_SOFTFLOAT_FLOAT128}
  46. {$define FPC_LOAD_SOFTFPU}
  47. {$endif}
  48. {$ifdef FPC_LOAD_SOFTFPU}
  49. {$define fpc_softfpu_interface}
  50. {$i softfpu.pp}
  51. {$undef fpc_softfpu_interface}
  52. {$endif FPC_LOAD_SOFTFPU}
  53. {$ifdef android}
  54. {$I sysandroidh.inc}
  55. {$endif android}
  56. {*****************************************************************************}
  57. implementation
  58. {*****************************************************************************}
  59. {$if defined(CPUI386) and not defined(FPC_USE_LIBC)}
  60. var
  61. sysenter_supported: LongInt = 0;
  62. {$endif}
  63. const
  64. calculated_cmdline:Pchar=nil;
  65. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  66. {$define FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  67. procedure OsSetupEntryInformation(constref info: TEntryInformation); forward;
  68. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  69. {$ifdef FPC_LOAD_SOFTFPU}
  70. {$define fpc_softfpu_implementation}
  71. {$if defined(CPUM68K)}
  72. {$define softfpu_compiler_mul32to64}
  73. {$define softfpu_inline}
  74. {$endif}
  75. {$i softfpu.pp}
  76. {$undef fpc_softfpu_implementation}
  77. { we get these functions and types from the softfpu code }
  78. {$define FPC_SYSTEM_HAS_float64}
  79. {$define FPC_SYSTEM_HAS_float32}
  80. {$define FPC_SYSTEM_HAS_flag}
  81. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  82. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  83. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  84. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  85. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  86. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  87. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  88. {$endif FPC_LOAD_SOFTFPU}
  89. {$define HAS_GETCPUCOUNT}
  90. {$I system.inc}
  91. {$ifdef android}
  92. {$I sysandroid.inc}
  93. {$endif android}
  94. {*****************************************************************************
  95. TLS handling
  96. *****************************************************************************}
  97. { TLS initialization is not required if linking against libc }
  98. {$if not defined(FPC_USE_LIBC)}
  99. {$if defined(CPUARM)}
  100. {$define INITTLS}
  101. Function fpset_tls(p : pointer;size : SizeUInt):cint;
  102. begin
  103. Result:=do_syscall(syscall_nr___ARM_NR_set_tls,TSysParam(p));
  104. end;
  105. {$endif defined(CPUARM)}
  106. {$if defined(CPUI386)}
  107. {$define INITTLS}
  108. Function fpset_tls(p : pointer;size : SizeUInt):cint;
  109. var
  110. desc : record
  111. entry_number : dword;
  112. base_addr : dword;
  113. limit : dword;
  114. flags : dword;
  115. end;
  116. selector : word;
  117. begin
  118. // get descriptor from the kernel
  119. desc.entry_number:=$ffffffff;
  120. // TLS is accessed by negative offsets, only the TCB pointer is at offset 0
  121. desc.base_addr:=dword(p)+size-SizeOf(Pointer);
  122. // 4 GB, limit is given in pages
  123. desc.limit:=$fffff;
  124. // seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1
  125. desc.flags:=%1010001;
  126. Result:=do_syscall(syscall_nr_set_thread_area,TSysParam(@desc));
  127. if Result=0 then
  128. begin
  129. selector:=desc.entry_number*8+3;
  130. asm
  131. movw selector,%gs
  132. movl desc.base_addr,%eax
  133. movl %eax,%gs:0
  134. end;
  135. end;
  136. end;
  137. {$endif defined(CPUI386)}
  138. {$if defined(CPUX86_64)}
  139. {$define INITTLS}
  140. const
  141. ARCH_SET_FS = $1002;
  142. Function fpset_tls(p : pointer;size : SizeUInt):cint;
  143. begin
  144. p:=pointer(qword(p)+size-SizeOf(Pointer));
  145. Result:=do_syscall(syscall_nr_arch_prctl,TSysParam(ARCH_SET_FS),TSysParam(p));
  146. if Result=0 then
  147. begin
  148. asm
  149. movq p,%rax
  150. movq %rax,%fs:0
  151. end;
  152. end;
  153. end;
  154. {$endif defined(CPUX86_64)}
  155. {$endif not FPC_USE_LIBC}
  156. {$ifdef INITTLS}
  157. { This code initialized the TLS segment for single threaded and static programs.
  158. In case of multithreaded and/or dynamically linked programs it is assumed that they
  159. dependent anyways on glibc which initializes tls properly.
  160. As soon as a purely FPC dynamic loading or multithreading is implemented, the code
  161. needs to be extended to handle these cases as well.
  162. }
  163. procedure InitTLS; [public,alias:'FPC_INITTLS'];
  164. const
  165. PT_TLS = 7;
  166. PT_DYNAMIC = 2;
  167. type
  168. {$ifdef CPU64}
  169. tphdr = record
  170. p_type,
  171. p_flags : dword;
  172. p_offset,
  173. p_vaddr,
  174. p_paddr,
  175. p_filesz,
  176. p_memsz,
  177. p_align : qword;
  178. end;
  179. {$else CPU64}
  180. tphdr = record
  181. p_type,
  182. p_offset,
  183. p_vaddr,
  184. p_paddr,
  185. p_filesz,
  186. p_memsz,
  187. p_flags,
  188. p_align : dword;
  189. end;
  190. {$endif CPU64}
  191. pphdr = ^tphdr;
  192. var
  193. phdr : pphdr;
  194. phnum : dword;
  195. i : integer;
  196. tls : pointer;
  197. auxp : ppointer;
  198. found : boolean;
  199. size : SizeUInt;
  200. begin
  201. auxp:=ppointer(envp);
  202. { skip environment }
  203. while assigned(auxp^) do
  204. inc(auxp);
  205. inc(auxp);
  206. phdr:=nil;
  207. phnum:=0;
  208. { now we are at the auxillary vector }
  209. while assigned(auxp^) do
  210. begin
  211. case plongint(auxp)^ of
  212. 3:
  213. phdr:=pphdr(ppointer(auxp+1)^);
  214. 5:
  215. phnum:=pdword(auxp+1)^;
  216. end;
  217. inc(auxp,2);
  218. end;
  219. found:=false;
  220. size:=0;
  221. for i:=1 to phnum do
  222. begin
  223. case phdr^.p_type of
  224. PT_TLS:
  225. begin
  226. found:=true;
  227. inc(size,phdr^.p_memsz);
  228. size:=Align(size,phdr^.p_align);
  229. end;
  230. PT_DYNAMIC:
  231. { if the program header contains a dynamic section, the program
  232. is linked dynamically so the dynamic linker takes care of the
  233. allocation of the TLS segment.
  234. We cannot allocate it by ourself anyways as PT_TLS provides only
  235. the size of TLS data of the executable itself
  236. }
  237. exit;
  238. end;
  239. inc(phdr);
  240. end;
  241. if found then
  242. begin
  243. {$ifdef CPUI386}
  244. { threadvars should start at a page boundary,
  245. add extra space for the TCB }
  246. size:=Align(size,4096)+sizeof(Pointer);
  247. {$endif CPUI386}
  248. {$ifdef CPUX86_64}
  249. { threadvars should start at a page boundary,
  250. add extra space for the TCB }
  251. size:=Align(size,4096)+sizeof(Pointer);
  252. {$endif CPUX86_64}
  253. tls:=Fpmmap(nil,size,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0);
  254. fpset_tls(tls,size);
  255. end;
  256. end;
  257. {$endif INITTLS}
  258. {*****************************************************************************
  259. Indirect Entry Point
  260. *****************************************************************************}
  261. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  262. var
  263. initialstkptr : Pointer;
  264. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  265. begin
  266. argc := info.OS.argc;
  267. argv := info.OS.argv;
  268. envp := info.OS.envp;
  269. initialstkptr := info.OS.stkptr;
  270. initialstklen := info.OS.stklen;
  271. end;
  272. { we need two variants here because TLS must be initialized by FPC only if no libc is linked however,
  273. InitTLS cannot be called from the start up files because when they are run, envp is not setup yet.}
  274. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  275. begin
  276. SetupEntryInformation(info);
  277. {$ifdef cpui386}
  278. Set8087CW(Default8087CW);
  279. {$endif cpui386}
  280. info.PascalMain();
  281. end;
  282. {$ifndef FPC_USE_LIBC}
  283. procedure SysEntry_InitTLS(constref info: TEntryInformation);[public,alias:'FPC_SysEntry_InitTLS'];
  284. begin
  285. SetupEntryInformation(info);
  286. {$ifdef INITTLS}
  287. InitTLS;
  288. {$endif INITTLS}
  289. {$ifdef cpui386}
  290. Set8087CW(Default8087CW);
  291. {$endif cpui386}
  292. info.PascalMain();
  293. end;
  294. {$endif FPC_USE_LIBC}
  295. {$else}
  296. var
  297. {$ifndef FPC_BOOTSTRAP_INDIRECT_ENTRY}
  298. initialstkptr : Pointer;external name '__stkptr';
  299. {$else FPC_BOOTSTRAP_INDIRECT_ENTRY}
  300. initialstkptr : Pointer; public name '__stkptr';
  301. operatingsystem_parameter_envp : Pointer; public name 'operatingsystem_parameter_envp';
  302. operatingsystem_parameter_argc : LongInt; public name 'operatingsystem_parameter_argc';
  303. operatingsystem_parameter_argv : Pointer; public name 'operatingsystem_parameter_argv';
  304. { we need two variants here because TLS must be initialized by FPC only if no libc is linked however,
  305. InitTLS cannot be called from the start up files because when they are run, envp is not setup yet.}
  306. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  307. begin
  308. initialstkptr := info.OS.stkptr;
  309. operatingsystem_parameter_envp := info.OS.envp;
  310. operatingsystem_parameter_argc := info.OS.argc;
  311. operatingsystem_parameter_argv := info.OS.argv;
  312. {$ifdef cpui386}
  313. Set8087CW(Default8087CW);
  314. {$endif cpui386}
  315. info.PascalMain();
  316. end;
  317. {$ifdef FPC_USE_LIBC}
  318. procedure SysEntry_InitTLS(constref info: TEntryInformation);[public,alias:'FPC_SysEntry_InitTLS'];
  319. begin
  320. initialstkptr := info.OS.stkptr;
  321. operatingsystem_parameter_envp := info.OS.envp;
  322. operatingsystem_parameter_argc := info.OS.argc;
  323. operatingsystem_parameter_argv := info.OS.argv;
  324. {$ifdef INITTLS}
  325. InitTLS;
  326. {$endif INITTLS}
  327. {$ifdef cpui386}
  328. Set8087CW(Default8087CW);
  329. {$endif cpui386}
  330. info.PascalMain();
  331. end;
  332. {$endif FPC_USE_LIBC}
  333. {$endif FPC_BOOTSTRAP_INDIRECT_ENTRY}
  334. {$if defined(CPUARM) and defined(FPC_ABI_EABI)}
  335. procedure haltproc(e:longint);cdecl;external name '_haltproc_eabi';
  336. {$else}
  337. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  338. {$endif}
  339. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  340. {*****************************************************************************
  341. Misc. System Dependent Functions
  342. *****************************************************************************}
  343. {$ifdef FPC_USE_LIBC}
  344. function FpPrCtl(options : cInt; const args : ptruint) : cint; cdecl; external clib name 'prctl';
  345. {$endif}
  346. procedure System_exit;
  347. begin
  348. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  349. EntryInformation.OS.haltproc(ExitCode);
  350. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  351. haltproc(ExitCode);
  352. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  353. End;
  354. Function ParamCount: Longint;
  355. Begin
  356. Paramcount:=argc-1
  357. End;
  358. {function BackPos(c:char; const s: shortstring): integer;
  359. var
  360. i: integer;
  361. Begin
  362. for i:=length(s) downto 0 do
  363. if s[i] = c then break;
  364. if i=0 then
  365. BackPos := 0
  366. else
  367. BackPos := i;
  368. end;}
  369. { variable where full path and filename and executable is stored }
  370. { is setup by the startup of the system unit. }
  371. var
  372. execpathstr : shortstring;
  373. procedure SysInitExecPath;
  374. var
  375. i : longint;
  376. begin
  377. execpathstr[0]:=#0;
  378. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  379. { it must also be an absolute filename, linux 2.0 points to a memory
  380. location so this will skip that }
  381. if (i>0) and (execpathstr[1]='/') then
  382. execpathstr[0]:=char(i);
  383. end;
  384. function paramstr(l: longint) : string;
  385. begin
  386. { stricly conforming POSIX applications }
  387. { have the executing filename as argv[0] }
  388. if l=0 then
  389. begin
  390. if execpathstr='' then
  391. SysInitExecPath;
  392. paramstr := execpathstr;
  393. end
  394. else if (l > 0) and (l < argc) then
  395. paramstr:=strpas(argv[l])
  396. else
  397. paramstr:='';
  398. end;
  399. Procedure Randomize;
  400. Begin
  401. randseed:=longint(Fptime(nil));
  402. End;
  403. function GetCPUCount: LongWord;
  404. var
  405. cpus : tcpu_set_t;
  406. BytesWritten,i : cint;
  407. begin
  408. Result := 1;
  409. { same approach as nproc uses:
  410. we return the number of available CPUs }
  411. BytesWritten:=FpSchedGetAffinity(0,sizeof(cpus),@cpus);
  412. if BytesWritten>0 then
  413. begin
  414. Result := 0;
  415. for i:=0 to BytesWritten-1 do
  416. Result:=Result+Popcnt((PByte(@cpus)+i)^);
  417. end;
  418. end;
  419. {*****************************************************************************
  420. cmdline
  421. *****************************************************************************}
  422. procedure SetupCmdLine;
  423. var
  424. bufsize,
  425. len,j,
  426. size,i : longint;
  427. found : boolean;
  428. buf : pchar;
  429. procedure AddBuf;
  430. var
  431. p : Pchar;
  432. begin
  433. p:=SysGetmem(size+bufsize);
  434. move(calculated_cmdline^,p^,size);
  435. move(buf^,p[size],bufsize);
  436. inc(size,bufsize);
  437. sysfreemem(calculated_cmdline);
  438. calculated_cmdline:=p;
  439. bufsize:=0;
  440. end;
  441. begin
  442. if argc<=0 then
  443. exit;
  444. Buf:=SysGetMem(ARG_MAX);
  445. size:=0;
  446. bufsize:=0;
  447. i:=0;
  448. while (i<argc) do
  449. begin
  450. len:=strlen(argv[i]);
  451. if len>ARG_MAX-2 then
  452. len:=ARG_MAX-2;
  453. found:=false;
  454. for j:=1 to len do
  455. if argv[i][j]=' ' then
  456. begin
  457. found:=true;
  458. break;
  459. end;
  460. found:=found or (len=0); // also quote if len=0, bug 19114
  461. if bufsize+len>=ARG_MAX-2 then
  462. AddBuf;
  463. if found then
  464. begin
  465. buf[bufsize]:='"';
  466. inc(bufsize);
  467. end;
  468. if len>0 then
  469. begin
  470. move(argv[i]^,buf[bufsize],len);
  471. inc(bufsize,len);
  472. end;
  473. if found then
  474. begin
  475. buf[bufsize]:='"';
  476. inc(bufsize);
  477. end;
  478. if i<argc-1 then
  479. buf[bufsize]:=' '
  480. else
  481. buf[bufsize]:=#0;
  482. inc(bufsize);
  483. inc(i);
  484. end;
  485. AddBuf;
  486. SysFreeMem(buf);
  487. end;
  488. function get_cmdline:Pchar;
  489. begin
  490. if calculated_cmdline=nil then
  491. setupcmdline;
  492. get_cmdline:=calculated_cmdline;
  493. end;
  494. {*****************************************************************************
  495. SystemUnit Initialization
  496. *****************************************************************************}
  497. function reenable_signal(sig : longint) : boolean;
  498. var
  499. e : TSigSet;
  500. i,j : byte;
  501. olderrno: cint;
  502. begin
  503. fillchar(e,sizeof(e),#0);
  504. { set is 1 based PM }
  505. dec(sig);
  506. i:=sig mod (sizeof(cuLong) * 8);
  507. j:=sig div (sizeof(cuLong) * 8);
  508. e[j]:=1 shl i;
  509. { this routine is called from a signal handler, so must not change errno }
  510. olderrno:=geterrno;
  511. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  512. reenable_signal:=geterrno=0;
  513. seterrno(olderrno);
  514. end;
  515. // signal handler is arch dependant due to processorexception to language
  516. // exception translation
  517. {$i sighnd.inc}
  518. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  519. var
  520. act: SigActionRec;
  521. begin
  522. { Initialize the sigaction structure }
  523. { all flags and information set to zero }
  524. FillChar(act, sizeof(SigActionRec),0);
  525. { initialize handler }
  526. act.sa_handler := SigActionHandler(@SignalToRunError);
  527. act.sa_flags:=SA_SIGINFO;
  528. FpSigAction(signum,@act,@oldact);
  529. end;
  530. var
  531. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  532. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  533. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  534. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  535. Procedure InstallSignals;
  536. begin
  537. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  538. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  539. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  540. InstallDefaultSignalHandler(SIGILL,oldsigill);
  541. end;
  542. procedure SysInitStdIO;
  543. begin
  544. OpenStdIO(Input,fmInput,StdInputHandle);
  545. OpenStdIO(Output,fmOutput,StdOutputHandle);
  546. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  547. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  548. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  549. {$ifdef android}
  550. InitStdIOAndroid;
  551. {$endif android}
  552. end;
  553. Procedure RestoreOldSignalHandlers;
  554. begin
  555. FpSigAction(SIGFPE,@oldsigfpe,nil);
  556. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  557. FpSigAction(SIGBUS,@oldsigbus,nil);
  558. FpSigAction(SIGILL,@oldsigill,nil);
  559. end;
  560. function GetProcessID: SizeUInt;
  561. begin
  562. GetProcessID := SizeUInt (fpGetPID);
  563. end;
  564. {$ifdef FPC_USE_LIBC}
  565. {$ifdef HAS_UGETRLIMIT}
  566. { there is no ugetrlimit libc call, just map it to the getrlimit call in these cases }
  567. function FpUGetRLimit(resource : cInt; rlim : PRLimit) : cInt; cdecl; external clib name 'getrlimit';
  568. {$endif}
  569. {$endif}
  570. {$if defined(CPUPOWERPC) or defined(CPUPOWERPC64)}
  571. const
  572. page_size = $10000;
  573. {$define LAST_PAGE_GENERATES_SIGNAL}
  574. {$else}
  575. const
  576. page_size = $1000;
  577. {$endif}
  578. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  579. var
  580. limits : TRLimit;
  581. success : boolean;
  582. begin
  583. success := false;
  584. fillchar(limits, sizeof(limits), 0);
  585. {$ifdef has_ugetrlimit}
  586. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  587. {$endif}
  588. {$ifndef NO_SYSCALL_GETRLIMIT}
  589. if (not success) then
  590. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  591. {$endif}
  592. if (success) and (limits.rlim_cur < stklen) then
  593. result := limits.rlim_cur
  594. else
  595. result := stklen;
  596. end;
  597. {$if FPC_FULLVERSION>30300}
  598. {$if defined(CPUI386) or defined(CPUARM)}
  599. {$I abitag.inc}
  600. {$endif defined(CPUI386) or defined(CPUARM)}
  601. {$endif FPC_FULLVERSION>30300}
  602. begin
  603. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  604. InitSyscallIntf;
  605. {$endif}
  606. {$ifndef FPUNONE}
  607. {$if defined(cpupowerpc)}
  608. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  609. // disabling all FPU exceptions. Enable them again.
  610. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  611. {$endif}
  612. {$endif}
  613. IsConsole := TRUE;
  614. StackLength := CheckInitialStkLen(initialStkLen);
  615. StackBottom := pointer(ptruint((ptruint(initialstkptr) or (page_size - 1)) + 1 - StackLength));
  616. {$ifdef LAST_PAGE_GENERATES_SIGNAL}
  617. StackBottom:=StackBottom + page_size;
  618. {$endif}
  619. { Set up signals handlers (may be needed by init code to test cpu features) }
  620. InstallSignals;
  621. {$if defined(cpui386) or defined(cpuarm)}
  622. fpc_cpucodeinit;
  623. {$endif cpui386}
  624. { Setup heap }
  625. InitHeap;
  626. SysInitExceptions;
  627. initunicodestringmanager;
  628. { Setup stdin, stdout and stderr }
  629. SysInitStdIO;
  630. { Reset IO Error }
  631. InOutRes:=0;
  632. { threading }
  633. InitSystemThreads;
  634. { dynamic libraries }
  635. InitSystemDynLibs;
  636. {$ifdef android}
  637. InitAndroid;
  638. {$endif android}
  639. { restore original signal handlers in case this is a library }
  640. if IsLibrary then
  641. RestoreOldSignalHandlers;
  642. end.