sysos.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. {
  2. Basic stuff for NativeNT RTLs
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2009-2010 by Sven Barth
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. const
  12. {$ifdef kmode}
  13. ntdll = 'ntoskrnl.exe';
  14. {$else}
  15. ntdll = 'ntdll.dll';
  16. {$endif}
  17. type
  18. PHandle = ^THandle;
  19. TNtUnicodeString = packed record
  20. Length: Word; // used characters in buffer
  21. MaximumLength: Word; // maximum characters in buffer
  22. Buffer: PWideChar;
  23. end;
  24. PNtUnicodeString = ^TNtUnicodeString;
  25. // using Int64 is an alternative (QWord might have unintended side effects)
  26. TLargeInteger = packed record
  27. case Boolean of
  28. True:(LowPart: LongWord;
  29. HighPart: LongInt);
  30. False:(QuadPart: Int64);
  31. end;
  32. PLargeInteger = ^TLargeInteger;
  33. TObjectAttributes = record
  34. Length: LongWord;
  35. RootDirectory: THandle;
  36. ObjectName: PNtUnicodeString;
  37. Attributes: LongWord;
  38. SecurityDescriptor: Pointer; // Points to type SECURITY_DESCRIPTOR
  39. SecurityQualityOfService: Pointer; // Points to type SECURITY_QUALITY_OF_SERVICE
  40. end;
  41. PObjectAttributes = ^TObjectAttributes;
  42. TRtlDriveLetterCurDir = packed record
  43. Flags: Word;
  44. Length: Word;
  45. TimeStamp: LongWord;
  46. DosPath: TNtUnicodeString;
  47. end;
  48. TCurDir = packed record
  49. DosPath: TNtUnicodeString;
  50. Handle: THandle;
  51. end;
  52. TRtlUserProcessParameters = packed record
  53. MaximumLength: LongWord;
  54. Length: LongWord;
  55. Flags: LongWord;
  56. DebugFlags: LongWord;
  57. ConsoleHandle: THandle;
  58. ConsoleFlags: LongWord;
  59. StandardInput: THandle;
  60. StandardOutput: THandle;
  61. StandardError: THandle;
  62. CurrentDirectory: TCurDir;
  63. DllPath: TNtUnicodeString;
  64. ImagePathName: TNtUnicodeString;
  65. CommandLine: TNtUnicodeString;
  66. Environment: ^Word; // PWSTR
  67. StartingX: LongWord;
  68. StartingY: LongWord;
  69. CountX: LongWord;
  70. CountY: LongWord;
  71. CountCharsX: LongWord;
  72. CountCharsY: LongWord;
  73. FillAttribute: LongWord;
  74. WindowFlags: LongWord;
  75. ShowWindowFlags: LongWord;
  76. WindowTitle: TNtUnicodeString;
  77. DesktopInfo: TNtUnicodeString;
  78. ShellInfo: TNtUnicodeString;
  79. RuntimeData: TNtUnicodeString;
  80. CurrentDirectories: array[0..31] of TRtlDriveLetterCurDir;
  81. end;
  82. PRtlUserProcessParameters = ^TRtlUserProcessParameters;
  83. // a simple version of the PEB that contains the common stuff
  84. TSimplePEB = packed record
  85. InheritedAddressSpace: Byte;
  86. ReadImageFileExecOptions: Byte;
  87. BeingDebugged: Byte;
  88. SpareBool: Byte;
  89. Mutant: THandle;
  90. ImageBaseAddress: Pointer;
  91. Ldr: Pointer;
  92. ProcessParameters: PRtlUserProcessParameters;
  93. SubSystemData: Pointer;
  94. ProcessHeap: Pointer;
  95. FastPebLock: Pointer;
  96. FastPebLockRoutine: Pointer;
  97. FastPebUnlockRoutine: Pointer;
  98. EnvironmentUpdateCount: LongWord;
  99. KernelCallbackTable: Pointer;
  100. EventLogSection: Pointer;
  101. EventLog: Pointer;
  102. FreeList: Pointer;
  103. TlsExpansionCounter: LongWord;
  104. TlsBitmap: Pointer;
  105. TlsBitmapBits: array[0..1] of LongWord;
  106. ReadOnlySharedMemoryBase: Pointer;
  107. ReadOnlySharedMemoryHeap: Pointer;
  108. ReadOnlyStaticServerData: Pointer;
  109. AnsiCodePageData: Pointer;
  110. OemCodePageData: Pointer;
  111. UnicodeCaseTableData: Pointer;
  112. NumberOfProcessors: LongWord;
  113. NtGlobalFlag: LongWord;
  114. CriticalSectionTimeout: TLargeInteger;
  115. HeapSegmentReserve: LongWord;
  116. HeapSegmentCommit: LongWord;
  117. HeapDeCommitTotalFreeThreshold: LongWord;
  118. HeapDeCommitFreeBlockThreshold: LongWord;
  119. NumberOfHeaps: LongWord;
  120. MaximumNumberOfHeaps: LongWord;
  121. ProcessHeaps: Pointer;
  122. GdiSharedHandleTable: Pointer;
  123. ProcessStarterHelper: Pointer;
  124. GdiDCAttributeList: LongWord;
  125. LoaderLock: Pointer;
  126. OSMajorVersion: LongWord;
  127. OSMinorVersion: LongWord;
  128. OSBuildNumber: Word;
  129. OSCSDVersion: Word;
  130. OSPlatformId: LongWord;
  131. ImageSubSystem: LongWord;
  132. ImageSubSystemMajorVersion: LongWord;
  133. ImageSubSystemMinorVersion: LongWord;
  134. ImageProcessAffinityMask: LongWord;
  135. GdiHandleBuffer: array[0..$21] of LongWord;
  136. PostProcessInitRoutine: Pointer;
  137. TlsExpansionBitmap: Pointer;
  138. TlsExpansionBitmapBits: array[0..$19] of Word;
  139. SessionId: LongWord;
  140. end;
  141. PSimplePEB = ^TSimplePEB;
  142. PExceptionRegistrationRecord = ^TExceptionRegistrationRecord;
  143. TExceptionRegistrationRecord = packed record
  144. Next: PExceptionRegistrationRecord;
  145. Handler: Pointer; //PExceptionRoutine;
  146. end;
  147. PNTTIB = ^TNTTIB;
  148. TNTTIB = packed record
  149. ExceptionList: PExceptionRegistrationRecord;
  150. StackBase: Pointer;
  151. StackLimit: Pointer;
  152. SubSystemTib: Pointer;
  153. union1: record
  154. case Boolean of
  155. True: (FiberData: Pointer);
  156. False: (Version: DWord);
  157. end;
  158. ArbitraryUserPointer: Pointer;
  159. Self: PNTTIB;
  160. end;
  161. TClientID = packed record
  162. UniqueProcess: LongWord;
  163. UniqueThread: LongWord;
  164. end;
  165. PClientID = ^TClientID;
  166. TSimpleTEB = packed record
  167. NtTib: TNTTIB;
  168. EnvironmentPointer: Pointer;
  169. ClientId: TClientID;
  170. end;
  171. PSimpleTEB = ^TSimpleTEB;
  172. const
  173. STATUS_SUCCESS = LongInt($00000000);
  174. STATUS_PENDING = LongInt($00000103);
  175. STATUS_END_OF_FILE = LongInt($C0000011);
  176. STATUS_ACCESS_DENIED = LongInt($C0000022);
  177. STATUS_OBJECT_TYPE_MISMATCH = LongInt($C0000024);
  178. STATUS_PIPE_BROKEN = LongInt($C000014B);
  179. STATUS_OBJECT_NAME_NOT_FOUND = LongInt($C0000034);
  180. STATUS_FILE_IS_A_DIRECTORY = LongInt($C00000BA);
  181. OBJ_INHERIT = $00000002;
  182. OBJ_PERMANENT = $00000010;
  183. FILE_DIRECTORY_FILE = $00000001;
  184. FILE_NON_DIRECTORY_FILE = $00000040;
  185. FILE_SYNCHRONOUS_IO_NONALERT = $00000020;
  186. FILE_OPEN_FOR_BACKUP_INTENT = $00004000;
  187. FILE_OPEN_REMOTE_INSTANCE = $00000400;
  188. STANDARD_RIGHTS_REQUIRED = $000F0000;
  189. FILE_SHARE_READ = $00000001;
  190. FILE_SHARE_WRITE = $00000002;
  191. FILE_SHARE_DELETE = $00000004;
  192. FILE_OPEN = $00000001;
  193. FILE_CREATE = $00000002;
  194. FILE_OVERWRITE_IF = $00000005;
  195. FILE_ATTRIBUTE_NORMAL = $00000080;
  196. NT_SYNCHRONIZE = $00100000; // normally called SYNCHRONIZE
  197. NT_DELETE = $00010000; // normally called DELETE
  198. GENERIC_READ = LongWord($80000000);
  199. GENERIC_WRITE = $40000000;
  200. GENERIC_ALL = $10000000;
  201. FILE_READ_ATTRIBUTES = $00000080;
  202. FileStandardInformation = 5;
  203. FileRenameInformation = 10;
  204. FileDispositionInformation = 13;
  205. FilePositionInformation = 14;
  206. FileAllocationInformation = 19;
  207. FileEndOfFileInformation = 20;
  208. { Share mode open }
  209. fmShareCompat = $00000000;
  210. fmShareExclusive = $10;
  211. fmShareDenyWrite = $20;
  212. fmShareDenyRead = $30;
  213. fmShareDenyNone = $40;
  214. type
  215. TIoStatusBlock = record
  216. Status: LongInt;
  217. Information: PLongWord;
  218. end;
  219. PIoStatusBlock = ^TIoStatusBlock;
  220. TFileDispositionInformation = record
  221. DeleteFile: LongBool;
  222. end;
  223. TFileStandardInformation = record
  224. AllocationSize: TLargeInteger;
  225. EndOfFile: TLargeInteger;
  226. NumberOfLinks: LongWord;
  227. DeletePending: ByteBool;
  228. Directory: ByteBool;
  229. end;
  230. TFilePositionInformation = record
  231. CurrentByteOffset: TLargeInteger;
  232. end;
  233. TFileEndOfFileInformation = record
  234. EndOfFile: TLargeInteger;
  235. end;
  236. TFileAllocationInformation = record
  237. AllocationSize: TLargeInteger;
  238. end;
  239. TFileRenameInformation = record
  240. ReplaceIfExists: ByteBool;
  241. RootDirectory: THandle;
  242. FileNameLength: LongWord;
  243. FileName: array[0..0] of WideChar;
  244. end;
  245. PFileRenameInformation = ^TFileRenameInformation;
  246. threadvar
  247. errno: LongInt;
  248. procedure Errno2InoutRes;
  249. var
  250. r: Word;
  251. begin
  252. {$message warning 'Correctly implement Errno2InoutRes'}
  253. case errno of
  254. STATUS_OBJECT_NAME_NOT_FOUND:
  255. r := 2;
  256. STATUS_OBJECT_TYPE_MISMATCH:
  257. r := 3;
  258. STATUS_ACCESS_DENIED:
  259. r := 5;
  260. STATUS_END_OF_FILE:
  261. r := 100;
  262. else
  263. r := errno;
  264. end;
  265. errno := 0;
  266. InOutRes := r;
  267. end;
  268. function NtCreateFile(FileHandle: PHandle; DesiredAccess: LongWord;
  269. ObjectAttributes: PObjectAttributes; IoStatusBlock: PIOStatusBlock;
  270. AllocationSize: PLargeInteger; FileAttributes: LongWord;
  271. ShareAccess: LongWord; CreateDisposition: LongWord; CreateOptions: LongWord;
  272. EaBuffer: Pointer; EaLength: LongWord): LongInt; stdcall; external ntdll;
  273. function NtCreateDirectoryObject(DirectoryHandle: PHandle;
  274. DesiredAccess: LongWord; ObjectAttributes: PObjectAttributes): LongInt;
  275. stdcall; external ntdll;
  276. function NtOpenDirectoryObject(DirectoryHandle: PHandle;
  277. DesiredAccess: LongWord; ObjectAttributes: PObjectAttributes): LongInt;
  278. stdcall; external ntdll;
  279. function NtClose(Handle: THandle): LongInt; stdcall; external ntdll;
  280. function NtMakeTemporaryObject(Handle: THandle): LongInt; stdcall;
  281. external ntdll;
  282. function NtSetInformationFile(FileHandle: THandle;
  283. IoStatusBlock: PIoStatusBlock; FileInformation: Pointer;
  284. FileInformationLength: LongWord; FileInformationClass: LongWord):
  285. LongInt; stdcall; external ntdll;
  286. function NtQueryInformationFile(FileHandle: THandle;
  287. IoStatusBlock: PIoStatusBlock; FileInformation: Pointer;
  288. FileInformationLength: LongWord; FileInformationClass: LongWord):
  289. LongInt; stdcall; external ntdll;
  290. function NtReadFile(FileHandle: THandle; Event: THandle; ApcRoutine: Pointer;
  291. ApcContext: Pointer; IoStatusBlock: PIOStatusBlock; Buffer: Pointer;
  292. Length: LongWord; ByteOffset: PLargeInteger; Key: PLongWord): LongInt;
  293. stdcall; external ntdll;
  294. function NtWriteFile(FileHandle: THandle; Event: THandle;
  295. ApcRoutine: Pointer; ApcContext: Pointer; IoStatusBlock: PIOStatusBlock;
  296. Buffer: Pointer; Length: LongWord; ByteOffset: PLargeInteger; Key: PLongWord):
  297. LongInt; stdcall; external ntdll;
  298. function NtWaitForSingleObject(Handle: THandle; Alertable: ByteBool;
  299. Timeout: PLargeInteger): LongInt; stdcall; external ntdll;
  300. function NtDisplayString(aString: PNtUnicodeString): LongInt; stdcall; external ntdll;
  301. { TODO : move to platform specific file }
  302. function NtCurrentTEB: PSimpleTEB; assembler;
  303. asm
  304. movl %fs:(0x18),%eax
  305. end;
  306. (* from NDKUtils *)
  307. procedure SysInitializeObjectAttributes(var aObjectAttr: TObjectAttributes;
  308. aName: PNtUnicodeString; aAttributes: LongWord; aRootDir: THandle;
  309. aSecurity: Pointer);
  310. begin
  311. with aObjectAttr do begin
  312. Length := SizeOf(TObjectAttributes);
  313. RootDirectory := aRootDir;
  314. Attributes := aAttributes;
  315. ObjectName := aName;
  316. SecurityDescriptor := aSecurity;
  317. SecurityQualityOfService := Nil;
  318. end;
  319. end;
  320. procedure SysPCharToNtStr(var aNtStr: TNtUnicodeString; aText: PChar;
  321. aLen: LongWord);
  322. var
  323. i: Integer;
  324. begin
  325. if (aLen = 0) and (aText <> Nil) and (aText^ <> #0) then
  326. aLen := StrLen(aText);
  327. aNtStr.Length := aLen * SizeOf(WideChar);
  328. aNtStr.MaximumLength := aNtStr.Length;
  329. aNtStr.Buffer := GetMem(aNtStr.Length);
  330. for i := 0 to aLen-1 do
  331. aNtStr.Buffer[i] := aText[i];
  332. end;
  333. procedure SysPWideCharToNtStr(var aNtStr: TNtUnicodeString; aText: PWideChar;
  334. aLen: LongWord);
  335. var
  336. i: Integer;
  337. begin
  338. if (aLen = 0) and (aText <> Nil) and (aText^ <> #0) then
  339. aLen := Length(aText);
  340. aNtStr.Length := aLen * SizeOf(WideChar);
  341. aNtStr.MaximumLength := aNtStr.Length;
  342. aNtStr.Buffer := GetMem(aNtStr.Length);
  343. Move(aText[0],aNtStr.Buffer[0],aLen);
  344. end;
  345. procedure SysUnicodeStringToNtStr(var aNtStr: TNtUnicodeString; const s: UnicodeString);
  346. var
  347. i: Integer;
  348. begin
  349. aNtStr.Length := Length(s) * SizeOf(WideChar);
  350. aNtStr.MaximumLength := aNtStr.Length;
  351. aNtStr.Buffer := GetMem(aNtStr.Length);
  352. if aNtStr.Length<>0 then
  353. Move(s[1],aNtStr.Buffer[0],aNtStr.Length);
  354. end;
  355. procedure SysFreeNtStr(var aNtStr: TNtUnicodeString);
  356. begin
  357. if aNtStr.Buffer <> Nil then begin
  358. FreeMem(aNtStr.Buffer);
  359. aNtStr.Buffer := Nil;
  360. end;
  361. aNtStr.Length := 0;
  362. aNtStr.MaximumLength := 0;
  363. end;