summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/nativent/sysos.inc
blob: a12b6be41c391a3dfc36c73cca63fce665bd3486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{
    Basic stuff for NativeNT RTLs

    This file is part of the Free Pascal run time library.
    Copyright (c) 2009-2010 by Sven Barth

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

const
{$ifdef kmode}
  ntdll = 'ntoskrnl.exe';
{$else}
  ntdll = 'ntdll.dll';
{$endif}

type
  PHandle = ^THandle;

  TNtUnicodeString = packed record
    Length: Word;        // used characters in buffer
    MaximumLength: Word; // maximum characters in buffer
    Buffer: PWideChar;
  end;
  PNtUnicodeString = ^TNtUnicodeString;

  // using Int64 is an alternative (QWord might have unintended side effects)
  TLargeInteger = packed record
    case Boolean of
      True:(LowPart: LongWord;
            HighPart: LongInt);
      False:(QuadPart: Int64);
  end;
  PLargeInteger = ^TLargeInteger;


  TObjectAttributes = record
    Length: LongWord;
    RootDirectory: THandle;
    ObjectName: PNtUnicodeString;
    Attributes: LongWord;
    SecurityDescriptor: Pointer;       // Points to type SECURITY_DESCRIPTOR
    SecurityQualityOfService: Pointer; // Points to type SECURITY_QUALITY_OF_SERVICE
  end;
  PObjectAttributes = ^TObjectAttributes;

  TRtlDriveLetterCurDir = packed record
    Flags: Word;
    Length: Word;
    TimeStamp: LongWord;
    DosPath: TNtUnicodeString;
  end;

  TCurDir = packed record
    DosPath: TNtUnicodeString;
    Handle: THandle;
  end;

  TRtlUserProcessParameters = packed record
    MaximumLength: LongWord;
    Length: LongWord;
    Flags: LongWord;
    DebugFlags: LongWord;
    ConsoleHandle: THandle;
    ConsoleFlags: LongWord;
    StandardInput: THandle;
    StandardOutput: THandle;
    StandardError: THandle;
    CurrentDirectory: TCurDir;
    DllPath: TNtUnicodeString;
    ImagePathName: TNtUnicodeString;
    CommandLine: TNtUnicodeString;
    Environment: ^Word; // PWSTR
    StartingX: LongWord;
    StartingY: LongWord;
    CountX: LongWord;
    CountY: LongWord;
    CountCharsX: LongWord;
    CountCharsY: LongWord;
    FillAttribute: LongWord;
    WindowFlags: LongWord;
    ShowWindowFlags: LongWord;
    WindowTitle: TNtUnicodeString;
    DesktopInfo: TNtUnicodeString;
    ShellInfo: TNtUnicodeString;
    RuntimeData: TNtUnicodeString;
    CurrentDirectories: array[0..31] of TRtlDriveLetterCurDir;
  end;
  PRtlUserProcessParameters = ^TRtlUserProcessParameters;

  // a simple version of the PEB that contains the common stuff
  TSimplePEB = packed record
    InheritedAddressSpace: Byte;
    ReadImageFileExecOptions: Byte;
    BeingDebugged: Byte;
    SpareBool: Byte;
    Mutant: THandle;
    ImageBaseAddress: Pointer;
    Ldr: Pointer;
    ProcessParameters: PRtlUserProcessParameters;
    SubSystemData: Pointer;
    ProcessHeap: Pointer;
    FastPebLock: Pointer;
    FastPebLockRoutine: Pointer;
    FastPebUnlockRoutine: Pointer;
    EnvironmentUpdateCount: LongWord;
    KernelCallbackTable: Pointer;
    EventLogSection: Pointer;
    EventLog: Pointer;
    FreeList: Pointer;
    TlsExpansionCounter: LongWord;
    TlsBitmap: Pointer;
    TlsBitmapBits: array[0..1] of LongWord;
    ReadOnlySharedMemoryBase: Pointer;
    ReadOnlySharedMemoryHeap: Pointer;
    ReadOnlyStaticServerData: Pointer;
    AnsiCodePageData: Pointer;
    OemCodePageData: Pointer;
    UnicodeCaseTableData: Pointer;
    NumberOfProcessors: LongWord;
    NtGlobalFlag: LongWord;
    CriticalSectionTimeout: TLargeInteger;
    HeapSegmentReserve: LongWord;
    HeapSegmentCommit: LongWord;
    HeapDeCommitTotalFreeThreshold: LongWord;
    HeapDeCommitFreeBlockThreshold: LongWord;
    NumberOfHeaps: LongWord;
    MaximumNumberOfHeaps: LongWord;
    ProcessHeaps: Pointer;
    GdiSharedHandleTable: Pointer;
    ProcessStarterHelper: Pointer;
    GdiDCAttributeList: LongWord;
    LoaderLock: Pointer;
    OSMajorVersion: LongWord;
    OSMinorVersion: LongWord;
    OSBuildNumber: Word;
    OSCSDVersion: Word;
    OSPlatformId: LongWord;
    ImageSubSystem: LongWord;
    ImageSubSystemMajorVersion: LongWord;
    ImageSubSystemMinorVersion: LongWord;
    ImageProcessAffinityMask: LongWord;
    GdiHandleBuffer: array[0..$21] of LongWord;
    PostProcessInitRoutine: Pointer;
    TlsExpansionBitmap: Pointer;
    TlsExpansionBitmapBits: array[0..$19] of Word;
    SessionId: LongWord;
  end;
  PSimplePEB = ^TSimplePEB;

  PExceptionRegistrationRecord = ^TExceptionRegistrationRecord;
  TExceptionRegistrationRecord = packed record
    Next: PExceptionRegistrationRecord;
    Handler: Pointer; //PExceptionRoutine;
  end;

  PNTTIB = ^TNTTIB;
  TNTTIB = packed record
    ExceptionList: PExceptionRegistrationRecord;
    StackBase: Pointer;
    StackLimit: Pointer;
    SubSystemTib: Pointer;
    union1: record
              case Boolean of
                True: (FiberData: Pointer);
                False: (Version: DWord);
            end;
    ArbitraryUserPointer: Pointer;
    Self: PNTTIB;
  end;

  TClientID = packed record
    UniqueProcess: LongWord;
    UniqueThread: LongWord;
  end;
  PClientID = ^TClientID;

  TSimpleTEB = packed record
    NtTib: TNTTIB;
    EnvironmentPointer: Pointer;
    ClientId: TClientID;
  end;
  PSimpleTEB = ^TSimpleTEB;

const
  STATUS_SUCCESS = LongInt($00000000);
  STATUS_PENDING = LongInt($00000103);
  STATUS_END_OF_FILE = LongInt($C0000011);
  STATUS_ACCESS_DENIED = LongInt($C0000022);
  STATUS_OBJECT_TYPE_MISMATCH = LongInt($C0000024);
  STATUS_PIPE_BROKEN = LongInt($C000014B);
  STATUS_OBJECT_NAME_NOT_FOUND = LongInt($C0000034);
  STATUS_FILE_IS_A_DIRECTORY = LongInt($C00000BA);

  OBJ_INHERIT = $00000002;
  OBJ_PERMANENT = $00000010;

  FILE_DIRECTORY_FILE = $00000001;
  FILE_NON_DIRECTORY_FILE = $00000040;
  FILE_SYNCHRONOUS_IO_NONALERT = $00000020;
  FILE_OPEN_FOR_BACKUP_INTENT = $00004000;
  FILE_OPEN_REMOTE_INSTANCE = $00000400;

  STANDARD_RIGHTS_REQUIRED = $000F0000;

  FILE_SHARE_READ = $00000001;
  FILE_SHARE_WRITE = $00000002;
  FILE_SHARE_DELETE = $00000004;

  FILE_OPEN = $00000001;
  FILE_CREATE = $00000002;
  FILE_OVERWRITE_IF = $00000005;

  FILE_ATTRIBUTE_NORMAL = $00000080;

  NT_SYNCHRONIZE = $00100000; // normally called SYNCHRONIZE
  NT_DELETE = $00010000; // normally called DELETE
  GENERIC_READ = LongWord($80000000);
  GENERIC_WRITE = $40000000;
  GENERIC_ALL = $10000000;
  FILE_READ_ATTRIBUTES = $00000080;

  FileStandardInformation = 5;
  FileRenameInformation = 10;
  FileDispositionInformation = 13;
  FilePositionInformation = 14;
  FileAllocationInformation = 19;
  FileEndOfFileInformation = 20;

  { Share mode open }
  fmShareCompat    = $00000000;
  fmShareExclusive = $10;
  fmShareDenyWrite = $20;
  fmShareDenyRead  = $30;
  fmShareDenyNone  = $40;

type
  TIoStatusBlock = record
    Status: LongInt;
    Information: PLongWord;
  end;
  PIoStatusBlock = ^TIoStatusBlock;

  TFileDispositionInformation = record
    DeleteFile: LongBool;
  end;

  TFileStandardInformation = record
    AllocationSize: TLargeInteger;
    EndOfFile: TLargeInteger;
    NumberOfLinks: LongWord;
    DeletePending: ByteBool;
    Directory: ByteBool;
  end;

  TFilePositionInformation = record
    CurrentByteOffset: TLargeInteger;
  end;

  TFileEndOfFileInformation = record
    EndOfFile: TLargeInteger;
  end;

  TFileAllocationInformation = record
    AllocationSize: TLargeInteger;
  end;

  TFileRenameInformation = record
    ReplaceIfExists: ByteBool;
    RootDirectory: THandle;
    FileNameLength: LongWord;
    FileName: array[0..0] of WideChar;
  end;
  PFileRenameInformation = ^TFileRenameInformation;

threadvar
  errno: LongInt;

procedure Errno2InoutRes;
var
  r: Word;
begin
{$message warning 'Correctly implement Errno2InoutRes'}
  case errno of
    STATUS_OBJECT_NAME_NOT_FOUND:
      r := 2;
    STATUS_OBJECT_TYPE_MISMATCH:
      r := 3;
    STATUS_ACCESS_DENIED:
      r := 5;
    STATUS_END_OF_FILE:
      r := 100;
    else
      r := errno;
  end;
  errno := 0;
  InOutRes := r;
end;

function NtCreateFile(FileHandle: PHandle; DesiredAccess: LongWord;
  ObjectAttributes: PObjectAttributes; IoStatusBlock: PIOStatusBlock;
  AllocationSize: PLargeInteger; FileAttributes: LongWord;
  ShareAccess: LongWord; CreateDisposition: LongWord; CreateOptions: LongWord;
  EaBuffer: Pointer; EaLength: LongWord): LongInt; stdcall; external ntdll;
function  NtCreateDirectoryObject(DirectoryHandle: PHandle;
  DesiredAccess: LongWord; ObjectAttributes: PObjectAttributes): LongInt;
  stdcall; external ntdll;
function  NtOpenDirectoryObject(DirectoryHandle: PHandle;
  DesiredAccess: LongWord; ObjectAttributes: PObjectAttributes): LongInt;
  stdcall; external ntdll;
function NtClose(Handle: THandle): LongInt; stdcall; external ntdll;
function NtMakeTemporaryObject(Handle: THandle): LongInt; stdcall;
  external ntdll;
function  NtSetInformationFile(FileHandle: THandle;
  IoStatusBlock: PIoStatusBlock; FileInformation: Pointer;
  FileInformationLength: LongWord; FileInformationClass: LongWord):
  LongInt; stdcall; external ntdll;
function  NtQueryInformationFile(FileHandle: THandle;
  IoStatusBlock: PIoStatusBlock; FileInformation: Pointer;
  FileInformationLength: LongWord; FileInformationClass: LongWord):
  LongInt; stdcall; external ntdll;
function NtReadFile(FileHandle: THandle; Event: THandle; ApcRoutine: Pointer;
  ApcContext: Pointer; IoStatusBlock: PIOStatusBlock; Buffer: Pointer;
  Length: LongWord; ByteOffset: PLargeInteger; Key: PLongWord): LongInt;
  stdcall; external ntdll;
function NtWriteFile(FileHandle: THandle; Event: THandle;
  ApcRoutine: Pointer; ApcContext: Pointer; IoStatusBlock: PIOStatusBlock;
  Buffer: Pointer; Length: LongWord; ByteOffset: PLargeInteger; Key: PLongWord):
  LongInt; stdcall; external ntdll;
function NtWaitForSingleObject(Handle: THandle; Alertable: ByteBool;
  Timeout: PLargeInteger): LongInt; stdcall; external ntdll;
function NtDisplayString(aString: PNtUnicodeString): LongInt; stdcall; external ntdll;

{ TODO : move to platform specific file }
function NtCurrentTEB: PSimpleTEB; assembler;
asm
  movl %fs:(0x18),%eax
end;

(* from NDKUtils *)
procedure SysInitializeObjectAttributes(var aObjectAttr: TObjectAttributes;
  aName: PNtUnicodeString; aAttributes: LongWord; aRootDir: THandle;
  aSecurity: Pointer);
begin
  with aObjectAttr do begin
    Length := SizeOf(TObjectAttributes);
    RootDirectory := aRootDir;
    Attributes := aAttributes;
    ObjectName := aName;
    SecurityDescriptor := aSecurity;
    SecurityQualityOfService := Nil;
  end;
end;

procedure SysPCharToNtStr(var aNtStr: TNtUnicodeString; aText: PChar;
  aLen: LongWord);
var
  i: Integer;
begin
  if (aLen = 0) and (aText <> Nil) and (aText^ <> #0) then
    aLen := StrLen(aText);
  aNtStr.Length := aLen * SizeOf(WideChar);
  aNtStr.MaximumLength := aNtStr.Length;
  aNtStr.Buffer := GetMem(aNtStr.Length);
  for i := 0 to aLen-1 do
    aNtStr.Buffer[i] := aText[i];
end;

procedure SysFreeNtStr(var aNtStr: TNtUnicodeString);
begin
  if aNtStr.Buffer <> Nil then begin
    FreeMem(aNtStr.Buffer);
    aNtStr.Buffer := Nil;
  end;
  aNtStr.Length := 0;
  aNtStr.MaximumLength := 0;
end;