system.pp 19 KB

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