system.pp 19 KB

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