system.pp 19 KB

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