system.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. {$i osdefs.inc}
  24. {$I sysunixh.inc}
  25. {$if defined(VER3_0) and defined(CPUX86_64)}
  26. {$define FPC_BOOTSTRAP_INDIRECT_ENTRY}
  27. const
  28. { this constant only exists during bootstrapping of the RTL with FPC 3.0.x,
  29. so that the whole condition doesn't need to be repeated in si_intf }
  30. indirect_bootstrap = true;
  31. {$endif defined(VER3_0) and defined(CPUX86_64)}
  32. function get_cmdline:Pchar; deprecated 'use paramstr' ;
  33. property cmdline:Pchar read get_cmdline;
  34. {$if defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6))}
  35. {$define FPC_LOAD_SOFTFPU}
  36. {$endif}
  37. {$ifdef FPC_SOFT_FPUX80}
  38. {$define FPC_SOFTFLOAT_FLOATX80}
  39. {$define LOAD_SOFTFPU}
  40. {$endif}
  41. {$ifdef FPC_SOFT_FPU128}
  42. {$define FPC_SOFTFLOAT_FLOAT128}
  43. {$define FPC_LOAD_SOFTFPU}
  44. {$endif}
  45. {$ifdef FPC_LOAD_SOFTFPU}
  46. {$define fpc_softfpu_interface}
  47. {$i softfpu.pp}
  48. {$undef fpc_softfpu_interface}
  49. {$endif FPC_LOAD_SOFTFPU}
  50. {$ifdef android}
  51. {$I sysandroidh.inc}
  52. {$endif android}
  53. {*****************************************************************************}
  54. implementation
  55. {*****************************************************************************}
  56. {$if defined(CPUI386) and not defined(FPC_USE_LIBC)}
  57. var
  58. sysenter_supported: LongInt = 0;
  59. {$endif}
  60. const
  61. calculated_cmdline:Pchar=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. {$I system.inc}
  87. {$ifdef android}
  88. {$I sysandroid.inc}
  89. {$endif android}
  90. {*****************************************************************************
  91. Indirect Entry Point
  92. *****************************************************************************}
  93. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  94. var
  95. initialstkptr : Pointer;
  96. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  97. begin
  98. argc := info.OS.argc;
  99. argv := info.OS.argv;
  100. envp := info.OS.envp;
  101. initialstkptr := info.OS.stkptr;
  102. initialstklen := info.OS.stklen;
  103. end;
  104. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  105. begin
  106. SetupEntryInformation(info);
  107. {$ifdef cpui386}
  108. Set8087CW(Default8087CW);
  109. {$endif cpui386}
  110. info.PascalMain();
  111. end;
  112. {$else}
  113. var
  114. {$ifndef FPC_BOOTSTRAP_INDIRECT_ENTRY}
  115. initialstkptr : Pointer;external name '__stkptr';
  116. {$else FPC_BOOTSTRAP_INDIRECT_ENTRY}
  117. initialstkptr : Pointer; public name '__stkptr';
  118. operatingsystem_parameter_envp : Pointer; public name 'operatingsystem_parameter_envp';
  119. operatingsystem_parameter_argc : LongInt; public name 'operatingsystem_parameter_argc';
  120. operatingsystem_parameter_argv : Pointer; public name 'operatingsystem_parameter_argv';
  121. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  122. begin
  123. initialstkptr := info.OS.stkptr;
  124. operatingsystem_parameter_envp := info.OS.envp;
  125. operatingsystem_parameter_argc := info.OS.argc;
  126. operatingsystem_parameter_argv := info.OS.argv;
  127. {$ifdef cpui386}
  128. Set8087CW(Default8087CW);
  129. {$endif cpui386}
  130. info.PascalMain();
  131. end;
  132. {$endif FPC_BOOTSTRAP_INDIRECT_ENTRY}
  133. {$if defined(CPUARM) and defined(FPC_ABI_EABI)}
  134. procedure haltproc(e:longint);cdecl;external name '_haltproc_eabi';
  135. {$else}
  136. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  137. {$endif}
  138. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  139. {*****************************************************************************
  140. Misc. System Dependent Functions
  141. *****************************************************************************}
  142. {$ifdef FPC_USE_LIBC}
  143. function FpPrCtl(options : cInt; const args : ptruint) : cint; cdecl; external clib name 'prctl';
  144. {$endif}
  145. procedure System_exit;
  146. begin
  147. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  148. EntryInformation.OS.haltproc(ExitCode);
  149. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  150. haltproc(ExitCode);
  151. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  152. End;
  153. Function ParamCount: Longint;
  154. Begin
  155. Paramcount:=argc-1
  156. End;
  157. {function BackPos(c:char; const s: shortstring): integer;
  158. var
  159. i: integer;
  160. Begin
  161. for i:=length(s) downto 0 do
  162. if s[i] = c then break;
  163. if i=0 then
  164. BackPos := 0
  165. else
  166. BackPos := i;
  167. end;}
  168. { variable where full path and filename and executable is stored }
  169. { is setup by the startup of the system unit. }
  170. var
  171. execpathstr : shortstring;
  172. function paramstr(l: longint) : string;
  173. begin
  174. { stricly conforming POSIX applications }
  175. { have the executing filename as argv[0] }
  176. if l=0 then
  177. begin
  178. paramstr := execpathstr;
  179. end
  180. else if (l < argc) then
  181. paramstr:=strpas(argv[l])
  182. else
  183. paramstr:='';
  184. end;
  185. Procedure Randomize;
  186. Begin
  187. randseed:=longint(Fptime(nil));
  188. End;
  189. {*****************************************************************************
  190. cmdline
  191. *****************************************************************************}
  192. procedure SetupCmdLine;
  193. var
  194. bufsize,
  195. len,j,
  196. size,i : longint;
  197. found : boolean;
  198. buf : pchar;
  199. procedure AddBuf;
  200. var
  201. p : Pchar;
  202. begin
  203. p:=SysGetmem(size+bufsize);
  204. move(calculated_cmdline^,p^,size);
  205. move(buf^,p[size],bufsize);
  206. inc(size,bufsize);
  207. sysfreemem(calculated_cmdline);
  208. calculated_cmdline:=p;
  209. bufsize:=0;
  210. end;
  211. begin
  212. if argc<=0 then
  213. exit;
  214. Buf:=SysGetMem(ARG_MAX);
  215. size:=0;
  216. bufsize:=0;
  217. i:=0;
  218. while (i<argc) do
  219. begin
  220. len:=strlen(argv[i]);
  221. if len>ARG_MAX-2 then
  222. len:=ARG_MAX-2;
  223. found:=false;
  224. for j:=1 to len do
  225. if argv[i][j]=' ' then
  226. begin
  227. found:=true;
  228. break;
  229. end;
  230. found:=found or (len=0); // also quote if len=0, bug 19114
  231. if bufsize+len>=ARG_MAX-2 then
  232. AddBuf;
  233. if found then
  234. begin
  235. buf[bufsize]:='"';
  236. inc(bufsize);
  237. end;
  238. if len>0 then
  239. begin
  240. move(argv[i]^,buf[bufsize],len);
  241. inc(bufsize,len);
  242. end;
  243. if found then
  244. begin
  245. buf[bufsize]:='"';
  246. inc(bufsize);
  247. end;
  248. if i<argc-1 then
  249. buf[bufsize]:=' '
  250. else
  251. buf[bufsize]:=#0;
  252. inc(bufsize);
  253. inc(i);
  254. end;
  255. AddBuf;
  256. SysFreeMem(buf);
  257. end;
  258. function get_cmdline:Pchar;
  259. begin
  260. if calculated_cmdline=nil then
  261. setupcmdline;
  262. get_cmdline:=calculated_cmdline;
  263. end;
  264. {*****************************************************************************
  265. SystemUnit Initialization
  266. *****************************************************************************}
  267. function reenable_signal(sig : longint) : boolean;
  268. var
  269. e : TSigSet;
  270. i,j : byte;
  271. olderrno: cint;
  272. begin
  273. fillchar(e,sizeof(e),#0);
  274. { set is 1 based PM }
  275. dec(sig);
  276. i:=sig mod (sizeof(cuLong) * 8);
  277. j:=sig div (sizeof(cuLong) * 8);
  278. e[j]:=1 shl i;
  279. { this routine is called from a signal handler, so must not change errno }
  280. olderrno:=geterrno;
  281. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  282. reenable_signal:=geterrno=0;
  283. seterrno(olderrno);
  284. end;
  285. // signal handler is arch dependant due to processorexception to language
  286. // exception translation
  287. {$i sighnd.inc}
  288. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  289. var
  290. act: SigActionRec;
  291. begin
  292. { Initialize the sigaction structure }
  293. { all flags and information set to zero }
  294. FillChar(act, sizeof(SigActionRec),0);
  295. { initialize handler }
  296. act.sa_handler := SigActionHandler(@SignalToRunError);
  297. act.sa_flags:=SA_SIGINFO;
  298. FpSigAction(signum,@act,@oldact);
  299. end;
  300. var
  301. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  302. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  303. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  304. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  305. Procedure InstallSignals;
  306. begin
  307. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  308. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  309. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  310. InstallDefaultSignalHandler(SIGILL,oldsigill);
  311. end;
  312. procedure SysInitStdIO;
  313. begin
  314. OpenStdIO(Input,fmInput,StdInputHandle);
  315. OpenStdIO(Output,fmOutput,StdOutputHandle);
  316. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  317. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  318. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  319. {$ifdef android}
  320. InitStdIOAndroid;
  321. {$endif android}
  322. end;
  323. Procedure RestoreOldSignalHandlers;
  324. begin
  325. FpSigAction(SIGFPE,@oldsigfpe,nil);
  326. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  327. FpSigAction(SIGBUS,@oldsigbus,nil);
  328. FpSigAction(SIGILL,@oldsigill,nil);
  329. end;
  330. procedure SysInitExecPath;
  331. var
  332. i : longint;
  333. begin
  334. execpathstr[0]:=#0;
  335. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  336. { it must also be an absolute filename, linux 2.0 points to a memory
  337. location so this will skip that }
  338. if (i>0) and (execpathstr[1]='/') then
  339. execpathstr[0]:=char(i);
  340. end;
  341. function GetProcessID: SizeUInt;
  342. begin
  343. GetProcessID := SizeUInt (fpGetPID);
  344. end;
  345. {$ifdef FPC_USE_LIBC}
  346. {$ifdef HAS_UGETRLIMIT}
  347. { there is no ugetrlimit libc call, just map it to the getrlimit call in these cases }
  348. function FpUGetRLimit(resource : cInt; rlim : PRLimit) : cInt; cdecl; external clib name 'getrlimit';
  349. {$endif}
  350. {$endif}
  351. {$if defined(CPUPOWERPC) or defined(CPUPOWERPC64)}
  352. const
  353. page_size = $10000;
  354. {$define LAST_PAGE_GENERATES_SIGNAL}
  355. {$else}
  356. const
  357. page_size = $1000;
  358. {$endif}
  359. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  360. var
  361. limits : TRLimit;
  362. success : boolean;
  363. begin
  364. success := false;
  365. fillchar(limits, sizeof(limits), 0);
  366. {$ifdef has_ugetrlimit}
  367. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  368. {$endif}
  369. {$ifndef NO_SYSCALL_GETRLIMIT}
  370. if (not success) then
  371. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  372. {$endif}
  373. if (success) and (limits.rlim_cur < stklen) then
  374. result := limits.rlim_cur
  375. else
  376. result := stklen;
  377. end;
  378. begin
  379. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  380. InitSyscallIntf;
  381. {$endif}
  382. {$ifndef FPUNONE}
  383. {$if defined(cpupowerpc)}
  384. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  385. // disabling all FPU exceptions. Enable them again.
  386. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  387. {$endif}
  388. {$endif}
  389. IsConsole := TRUE;
  390. StackLength := CheckInitialStkLen(initialStkLen);
  391. StackBottom := pointer((ptruint(initialstkptr) or (page_size - 1)) + 1 - StackLength);
  392. {$ifdef LAST_PAGE_GENERATES_SIGNAL}
  393. StackBottom:=StackBottom + page_size;
  394. {$endif}
  395. { Set up signals handlers (may be needed by init code to test cpu features) }
  396. InstallSignals;
  397. {$if defined(cpui386) or defined(cpuarm)}
  398. fpc_cpucodeinit;
  399. {$endif cpui386}
  400. { Setup heap }
  401. InitHeap;
  402. SysInitExceptions;
  403. initunicodestringmanager;
  404. { Setup stdin, stdout and stderr }
  405. SysInitStdIO;
  406. { Arguments }
  407. SysInitExecPath;
  408. { Reset IO Error }
  409. InOutRes:=0;
  410. { threading }
  411. InitSystemThreads;
  412. { dynamic libraries }
  413. InitSystemDynLibs;
  414. {$ifdef android}
  415. InitAndroid;
  416. {$endif android}
  417. { restore original signal handlers in case this is a library }
  418. if IsLibrary then
  419. RestoreOldSignalHandlers;
  420. end.