system.pp 12 KB

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