system.pp 19 KB

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