system.pp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. function get_cmdline:Pchar;
  26. property cmdline:Pchar read get_cmdline;
  27. {$if defined(CPUARM) or defined(CPUM68K)}
  28. {$define fpc_softfpu_interface}
  29. {$i softfpu.pp}
  30. {$undef fpc_softfpu_interface}
  31. {$endif defined(CPUARM) or defined(CPUM68K)}
  32. {*****************************************************************************}
  33. implementation
  34. {*****************************************************************************}
  35. {$if defined(CPUI386) and not defined(FPC_USE_LIBC)}
  36. var
  37. sysenter_supported: LongInt = 0;
  38. {$endif}
  39. const calculated_cmdline:Pchar=nil;
  40. {$if defined(CPUARM) or defined(CPUM68K)}
  41. {$define fpc_softfpu_implementation}
  42. {$i softfpu.pp}
  43. {$undef fpc_softfpu_implementation}
  44. { we get these functions and types from the softfpu code }
  45. {$define FPC_SYSTEM_HAS_float64}
  46. {$define FPC_SYSTEM_HAS_float32}
  47. {$define FPC_SYSTEM_HAS_flag}
  48. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  49. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  50. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  51. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  52. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  53. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  54. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  55. {$endif defined(CPUARM) or defined(CPUM68K)}
  56. {$I system.inc}
  57. {*****************************************************************************
  58. Misc. System Dependent Functions
  59. *****************************************************************************}
  60. {$if defined(CPUARM) and defined(FPC_ABI_EABI)}
  61. procedure haltproc(e:longint);cdecl;external name '_haltproc_eabi';
  62. {$else}
  63. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  64. {$endif}
  65. procedure System_exit;
  66. begin
  67. haltproc(ExitCode);
  68. End;
  69. Function ParamCount: Longint;
  70. Begin
  71. Paramcount:=argc-1
  72. End;
  73. {function BackPos(c:char; const s: shortstring): integer;
  74. var
  75. i: integer;
  76. Begin
  77. for i:=length(s) downto 0 do
  78. if s[i] = c then break;
  79. if i=0 then
  80. BackPos := 0
  81. else
  82. BackPos := i;
  83. end;}
  84. { variable where full path and filename and executable is stored }
  85. { is setup by the startup of the system unit. }
  86. var
  87. execpathstr : shortstring;
  88. function paramstr(l: longint) : string;
  89. begin
  90. { stricly conforming POSIX applications }
  91. { have the executing filename as argv[0] }
  92. if l=0 then
  93. begin
  94. paramstr := execpathstr;
  95. end
  96. else if (l < argc) then
  97. paramstr:=strpas(argv[l])
  98. else
  99. paramstr:='';
  100. end;
  101. Procedure Randomize;
  102. Begin
  103. randseed:=longint(Fptime(nil));
  104. End;
  105. {*****************************************************************************
  106. cmdline
  107. *****************************************************************************}
  108. procedure SetupCmdLine;
  109. var
  110. bufsize,
  111. len,j,
  112. size,i : longint;
  113. found : boolean;
  114. buf : pchar;
  115. procedure AddBuf;
  116. begin
  117. reallocmem(calculated_cmdline,size+bufsize);
  118. move(buf^,calculated_cmdline[size],bufsize);
  119. inc(size,bufsize);
  120. bufsize:=0;
  121. end;
  122. begin
  123. if argc<=0 then
  124. exit;
  125. GetMem(buf,ARG_MAX);
  126. size:=0;
  127. bufsize:=0;
  128. i:=0;
  129. while (i<argc) do
  130. begin
  131. len:=strlen(argv[i]);
  132. if len>ARG_MAX-2 then
  133. len:=ARG_MAX-2;
  134. found:=false;
  135. for j:=1 to len do
  136. if argv[i][j]=' ' then
  137. begin
  138. found:=true;
  139. break;
  140. end;
  141. if bufsize+len>=ARG_MAX-2 then
  142. AddBuf;
  143. if found then
  144. begin
  145. buf[bufsize]:='"';
  146. inc(bufsize);
  147. end;
  148. move(argv[i]^,buf[bufsize],len);
  149. inc(bufsize,len);
  150. if found then
  151. begin
  152. buf[bufsize]:='"';
  153. inc(bufsize);
  154. end;
  155. if i<argc then
  156. buf[bufsize]:=' '
  157. else
  158. buf[bufsize]:=#0;
  159. inc(bufsize);
  160. inc(i);
  161. end;
  162. AddBuf;
  163. FreeMem(buf,ARG_MAX);
  164. end;
  165. function get_cmdline:Pchar;
  166. begin
  167. if calculated_cmdline=nil then
  168. setupcmdline;
  169. get_cmdline:=calculated_cmdline;
  170. end;
  171. {*****************************************************************************
  172. SystemUnit Initialization
  173. *****************************************************************************}
  174. function reenable_signal(sig : longint) : boolean;
  175. var
  176. e : TSigSet;
  177. i,j : byte;
  178. olderrno: cint;
  179. begin
  180. fillchar(e,sizeof(e),#0);
  181. { set is 1 based PM }
  182. dec(sig);
  183. i:=sig mod (sizeof(cuLong) * 8);
  184. j:=sig div (sizeof(cuLong) * 8);
  185. e[j]:=1 shl i;
  186. { this routine is called from a signal handler, so must not change errno }
  187. olderrno:=geterrno;
  188. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  189. reenable_signal:=geterrno=0;
  190. seterrno(olderrno);
  191. end;
  192. // signal handler is arch dependant due to processorexception to language
  193. // exception translation
  194. {$i sighnd.inc}
  195. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  196. var
  197. act: SigActionRec;
  198. begin
  199. { Initialize the sigaction structure }
  200. { all flags and information set to zero }
  201. FillChar(act, sizeof(SigActionRec),0);
  202. { initialize handler }
  203. act.sa_handler := SigActionHandler(@SignalToRunError);
  204. act.sa_flags:=SA_SIGINFO;
  205. FpSigAction(signum,@act,@oldact);
  206. end;
  207. var
  208. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  209. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  210. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  211. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  212. Procedure InstallSignals;
  213. begin
  214. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  215. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  216. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  217. InstallDefaultSignalHandler(SIGILL,oldsigill);
  218. end;
  219. procedure SysInitStdIO;
  220. begin
  221. OpenStdIO(Input,fmInput,StdInputHandle);
  222. OpenStdIO(Output,fmOutput,StdOutputHandle);
  223. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  224. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  225. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  226. end;
  227. Procedure RestoreOldSignalHandlers;
  228. begin
  229. FpSigAction(SIGFPE,@oldsigfpe,nil);
  230. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  231. FpSigAction(SIGBUS,@oldsigbus,nil);
  232. FpSigAction(SIGILL,@oldsigill,nil);
  233. end;
  234. procedure SysInitExecPath;
  235. var
  236. i : longint;
  237. begin
  238. execpathstr[0]:=#0;
  239. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  240. { it must also be an absolute filename, linux 2.0 points to a memory
  241. location so this will skip that }
  242. if (i>0) and (execpathstr[1]='/') then
  243. execpathstr[0]:=char(i);
  244. end;
  245. function GetProcessID: SizeUInt;
  246. begin
  247. GetProcessID := SizeUInt (fpGetPID);
  248. end;
  249. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  250. var
  251. limits : TRLimit;
  252. success : boolean;
  253. begin
  254. success := false;
  255. fillchar(limits, sizeof(limits), 0);
  256. {$ifdef has_ugetrlimit}
  257. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  258. {$endif}
  259. if (not success) then
  260. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  261. if (success) and (limits.rlim_cur < stklen) then
  262. result := limits.rlim_cur
  263. else
  264. result := stklen;
  265. end;
  266. var
  267. initialstkptr : Pointer;external name '__stkptr';
  268. begin
  269. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  270. InitSyscallIntf;
  271. {$endif}
  272. {$ifndef FPUNONE}
  273. SysResetFPU;
  274. if not(IsLibrary) then
  275. SysInitFPU;
  276. {$if defined(cpupowerpc)}
  277. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  278. // disabling all FPU exceptions. Enable them again.
  279. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  280. {$endif}
  281. {$endif}
  282. IsConsole := TRUE;
  283. StackLength := CheckInitialStkLen(initialStkLen);
  284. StackBottom := initialstkptr - StackLength;
  285. { Set up signals handlers (may be needed by init code to test cpu features) }
  286. InstallSignals;
  287. {$if defined(cpui386) or defined(cpuarm)}
  288. fpc_cpucodeinit;
  289. {$endif cpui386}
  290. { Setup heap }
  291. InitHeap;
  292. SysInitExceptions;
  293. { Setup stdin, stdout and stderr }
  294. SysInitStdIO;
  295. { Arguments }
  296. SysInitExecPath;
  297. { Reset IO Error }
  298. InOutRes:=0;
  299. { threading }
  300. InitSystemThreads;
  301. initvariantmanager;
  302. {$ifdef VER2_2}
  303. initwidestringmanager;
  304. {$else VER2_2}
  305. initunicodestringmanager;
  306. {$endif VER2_2}
  307. { restore original signal handlers in case this is a library }
  308. if IsLibrary then
  309. RestoreOldSignalHandlers;
  310. end.