system.pp 19 KB

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