system.pp 19 KB

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