2
0

system.pp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 the *BSD's.
  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. Interface
  18. {$DEFINE SYSTEM_HAS_FEATURE_MONITOR}
  19. {$define FPC_USE_SIGPROCMASK}
  20. {$define FPC_USE_SIGALTSTACK}
  21. {$ifndef FPC_USE_LIBC}
  22. {$define FPC_USE_SYSCALL}
  23. {$endif}
  24. {$define FPC_IS_SYSTEM}
  25. {$I sysunixh.inc}
  26. {$ifdef Darwin}
  27. var argc:longint;
  28. argv:PPAnsiChar;
  29. envp:PPAnsiChar;
  30. {$endif}
  31. CONST SIGSTKSZ = 40960;
  32. {$if defined(CPUARM) or defined(CPUM68K)}
  33. {$define fpc_softfpu_interface}
  34. {$i softfpu.pp}
  35. {$undef fpc_softfpu_interface}
  36. {$endif defined(CPUARM) or defined(CPUM68K)}
  37. Implementation
  38. {$if defined(CPUARM) or defined(CPUM68K)}
  39. {$define fpc_softfpu_implementation}
  40. {$i softfpu.pp}
  41. {$undef fpc_softfpu_implementation}
  42. { we get these functions and types from the softfpu code }
  43. {$define FPC_SYSTEM_HAS_float64}
  44. {$define FPC_SYSTEM_HAS_float32}
  45. {$define FPC_SYSTEM_HAS_flag}
  46. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  47. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  48. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  49. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  50. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  51. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  52. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  53. {$endif defined(CPUARM) or defined(CPUM68K)}
  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. {$I system.inc}
  59. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  60. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  61. begin
  62. argc := info.OS.argc;
  63. argv := info.OS.argv;
  64. envp := info.OS.envp;
  65. initialstklen := info.OS.stklen;
  66. end;
  67. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  68. {$ifdef FPC_HAS_SETSYSNR_INC}
  69. {$I setsysnr.inc}
  70. {$endif FPC_HAS_SETSYSNR_INC}
  71. {*****************************************************************************
  72. Misc. System Dependent Functions
  73. *****************************************************************************}
  74. {$ifdef darwin}
  75. procedure normalexit(status: cint); cdecl; external 'c' name 'exit';
  76. {$endif}
  77. {$if defined(openbsd)}
  78. procedure haltproc; cdecl; external name '_haltproc';
  79. {$endif}
  80. procedure System_exit;
  81. {$if defined(darwin)}
  82. begin
  83. { make sure the libc atexit handlers are called, needed for e.g. profiling }
  84. normalexit(cint(ExitCode));
  85. end;
  86. {$elseif defined(openbsd)}
  87. begin
  88. haltproc;
  89. end;
  90. {$else}
  91. begin
  92. Fpexit(cint(ExitCode));
  93. end;
  94. {$endif}
  95. Function ParamCount: Longint;
  96. Begin
  97. Paramcount:=argc-1
  98. End;
  99. function BackPos(c:AnsiChar; const s: shortstring): integer;
  100. var
  101. i: integer;
  102. Begin
  103. for i:=length(s) downto 0 do
  104. if s[i] = c then break;
  105. if i=0 then
  106. BackPos := 0
  107. else
  108. BackPos := i;
  109. end;
  110. { variable where full path and filename and executable is stored }
  111. { is setup by the startup of the system unit. }
  112. //var
  113. // execpathstr : shortstring;
  114. function paramstr(l: longint) : shortstring;
  115. begin
  116. { stricly conforming POSIX applications }
  117. { have the executing filename as argv[0] }
  118. // if l=0 then
  119. // begin
  120. // paramstr := execpathstr;
  121. // end
  122. // else
  123. if (l >= 0) and (l < argc) then
  124. paramstr:=strpas(argv[l])
  125. else
  126. paramstr:='';
  127. end;
  128. Procedure Randomize;
  129. Begin
  130. randseed:=longint(Fptime(nil));
  131. End;
  132. {*****************************************************************************
  133. System Unit Initialization
  134. *****************************************************************************}
  135. function reenable_signal(sig : longint) : boolean;
  136. var
  137. e,oe : TSigSet;
  138. i,j : byte;
  139. olderrno: cint;
  140. begin
  141. fillchar(e,sizeof(e),#0);
  142. fillchar(oe,sizeof(oe),#0);
  143. { set is 1 based PM }
  144. dec(sig);
  145. i:=sig mod 32;
  146. j:=sig div 32;
  147. e[j]:=1 shl i;
  148. { this routine is called from a signal handler, so must not change errno }
  149. olderrno:=geterrno;
  150. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  151. reenable_signal:=geterrno=0;
  152. seterrno(olderrno);
  153. end;
  154. {$ifdef DEBUG}
  155. { Declare InstallDefaultSignalHandler as forward to be able
  156. to test aclling fpsigaction again within SignalToRunError
  157. function implemented within sighnd.inc inlcude file }
  158. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); forward;
  159. {$endif}
  160. {$i sighnd.inc}
  161. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  162. var
  163. act: SigActionRec;
  164. begin
  165. { Initialize the sigaction structure }
  166. { all flags and information set to zero }
  167. FillChar(act,sizeof(SigActionRec),0);
  168. { initialize handler }
  169. act.sa_handler:=@SignalToRunError;
  170. {$if defined(darwin) and defined(cpu64)}
  171. act.sa_flags:=SA_SIGINFO or SA_64REGSET;
  172. {$else}
  173. act.sa_flags:=SA_SIGINFO;
  174. {$endif}
  175. FpSigAction(signum,@act,@oldact);
  176. end;
  177. var
  178. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  179. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  180. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  181. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  182. Procedure InstallSignals;
  183. begin
  184. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  185. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  186. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  187. InstallDefaultSignalHandler(SIGILL,oldsigill);
  188. end;
  189. Procedure RestoreOldSignalHandlers;
  190. begin
  191. FpSigAction(SIGFPE,@oldsigfpe,nil);
  192. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  193. FpSigAction(SIGBUS,@oldsigbus,nil);
  194. FpSigAction(SIGILL,@oldsigill,nil);
  195. end;
  196. procedure SetupCmdLine;
  197. var
  198. bufsize,
  199. len,j,
  200. size,i : longint;
  201. found : boolean;
  202. buf : PAnsiChar;
  203. procedure AddBuf;
  204. begin
  205. reallocmem(cmdline,size+bufsize);
  206. move(buf^,cmdline[size],bufsize);
  207. inc(size,bufsize);
  208. bufsize:=0;
  209. end;
  210. begin
  211. GetMem(buf,ARG_MAX);
  212. size:=0;
  213. bufsize:=0;
  214. i:=0;
  215. while (i<argc) do
  216. begin
  217. len:=strlen(argv[i]);
  218. if len>ARG_MAX-2 then
  219. len:=ARG_MAX-2;
  220. found:=false;
  221. for j:=1 to len do
  222. if argv[i][j]=' ' then
  223. begin
  224. found:=true;
  225. break;
  226. end;
  227. if bufsize+len>=ARG_MAX-2 then
  228. AddBuf;
  229. if found then
  230. begin
  231. buf[bufsize]:='"';
  232. inc(bufsize);
  233. end;
  234. move(argv[i]^,buf[bufsize],len);
  235. inc(bufsize,len);
  236. if found then
  237. begin
  238. buf[bufsize]:='"';
  239. inc(bufsize);
  240. end;
  241. if i<argc-1 then
  242. buf[bufsize]:=' '
  243. else
  244. buf[bufsize]:=#0;
  245. inc(bufsize);
  246. inc(i);
  247. end;
  248. AddBuf;
  249. FreeMem(buf,ARG_MAX);
  250. end;
  251. procedure SysInitStdIO;
  252. begin
  253. OpenStdIO(Input,fmInput,StdInputHandle);
  254. OpenStdIO(Output,fmOutput,StdOutputHandle);
  255. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  256. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  257. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  258. end;
  259. {$ifdef FPC_USE_LIBC}
  260. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  261. {$ifdef Darwin}
  262. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  263. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  264. begin
  265. SetupEntryInformation(info);
  266. info.PascalMain();
  267. end;
  268. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  269. procedure pascalmain;external name '_PASCALMAIN';
  270. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: PPAnsiChar; envpparam: PPAnsiChar); cdecl; [public];
  271. begin
  272. argc:= argcparam;
  273. argv:= argvparam;
  274. envp:= envpparam;
  275. {$ifdef cpui386}
  276. Set8087CW(Default8087CW);
  277. {$endif cpui386}
  278. pascalmain; {run the pascal main program}
  279. end;
  280. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  281. {$endif Darwin}
  282. {$endif FPC_USE_LIBC}
  283. function GetProcessID: SizeUInt;
  284. begin
  285. GetProcessID := SizeUInt (fpGetPID);
  286. end;
  287. function InternalPageSize: SizeUInt; inline;
  288. begin
  289. {$ifndef darwin}
  290. InternalPageSize := 4096;
  291. {$else not darwin}
  292. InternalPageSize := darwin_page_size;
  293. {$endif not darwin}
  294. end;
  295. function AlignedStackTop: Pointer;
  296. begin
  297. AlignedStackTop:=Pointer((ptruint(sptr) + InternalPageSize - 1) and not(InternalPageSize - 1));
  298. end;
  299. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  300. var
  301. stackpointer: ptruint;
  302. begin
  303. stackpointer := ptruint(AlignedStackTop);
  304. if stklen > stackpointer then
  305. stklen := stackpointer - InternalPageSize;
  306. result := stklen;
  307. end;
  308. Begin
  309. {$ifdef darwin}
  310. darwin_init_page_size;
  311. {$endif darwin}
  312. IsConsole := TRUE;
  313. StackLength := CheckInitialStkLen(InitialStkLen);
  314. StackBottom := AlignedStackTop - StackLength;
  315. {$ifdef FPC_HAS_SETSYSNR_INC}
  316. { This procedure is needed for openbsd system which re-uses
  317. the same syscall numbers depending on OS version }
  318. SetSyscallNumbers;
  319. {$endif FPC_HAS_SETSYSNR_INC}
  320. { Set up signals handlers (may be needed by init code to test cpu features) }
  321. InstallSignals;
  322. {$if defined(cpui386) or defined(cpuarm)}
  323. fpc_cpucodeinit;
  324. {$endif cpui386}
  325. { Setup heap }
  326. InitHeap;
  327. SysInitExceptions;
  328. initunicodestringmanager;
  329. { Setup stdin, stdout and stderr }
  330. SysInitStdIO;
  331. { Reset IO Error }
  332. InOutRes:=0;
  333. { Arguments }
  334. SetupCmdLine;
  335. { threading }
  336. InitSystemThreads;
  337. InitSystemDynLibs;
  338. { restore original signal handlers in case this is a library }
  339. if IsLibrary then
  340. RestoreOldSignalHandlers;
  341. End.