f66a91de5633c17fd39a712ded6a7fffc3a16ccd
[reactos.git] / reactos / ntoskrnl / include / internal / ntoskrnl.h
1 #ifndef __INCLUDE_INTERNAL_NTOSKRNL_H
2 #define __INCLUDE_INTERNAL_NTOSKRNL_H
3
4 /*
5 * Use these to place a function in a specific section of the executable
6 */
7 #define PLACE_IN_SECTION(s) __attribute__((section (s)))
8 #define INIT_FUNCTION PLACE_IN_SECTION("init")
9 #define PAGE_LOCKED_FUNCTION PLACE_IN_SECTION("pagelk")
10 #define PAGE_UNLOCKED_FUNCTION PLACE_IN_SECTION("pagepo")
11
12 #ifdef _NTOSKRNL_
13
14 #include "ke.h"
15 #include "i386/mm.h"
16 #include "i386/fpu.h"
17 #include "ob.h"
18 #include "mm.h"
19 #include "ps.h"
20 #include "cc.h"
21 #include "io.h"
22 #include "po.h"
23 #include "se.h"
24 #include "ldr.h"
25 #include "kd.h"
26 #include "ex.h"
27 #include "fsrtl.h"
28 #include "lpc.h"
29 #include "rtl.h"
30 #ifdef KDBG
31 #include "../kdbg/kdb.h"
32 #endif
33 #include "dbgk.h"
34 #include "tag.h"
35 #include "test.h"
36 #include "inbv.h"
37
38 #include <pshpack1.h>
39 /*
40 * Defines a descriptor as it appears in the processor tables
41 */
42 typedef struct __DESCRIPTOR
43 {
44 ULONG a;
45 ULONG b;
46 } IDT_DESCRIPTOR, GDT_DESCRIPTOR;
47
48 #include <poppack.h>
49
50 extern IDT_DESCRIPTOR KiIdt[256];
51 //extern GDT_DESCRIPTOR KiGdt[256];
52
53 /*
54 * Initalization functions (called once by main())
55 */
56 VOID MmInitSystem(ULONG Phase, PLOADER_PARAMETER_BLOCK LoaderBlock, ULONG LastKernelAddress);
57 VOID IoInit(VOID);
58 VOID IoInit2(BOOLEAN BootLog);
59 VOID STDCALL IoInit3(VOID);
60 VOID ObInit(VOID);
61 VOID PsInit(VOID);
62 VOID CmInitializeRegistry(VOID);
63 VOID STDCALL CmInitHives(BOOLEAN SetupBoot);
64 VOID CmInit2(PCHAR CommandLine);
65 VOID CmShutdownRegistry(VOID);
66 BOOLEAN CmImportSystemHive(PCHAR ChunkBase, ULONG ChunkSize);
67 BOOLEAN CmImportHardwareHive(PCHAR ChunkBase, ULONG ChunkSize);
68 VOID KdInitSystem(ULONG Reserved, PLOADER_PARAMETER_BLOCK LoaderBlock);
69
70 /* FIXME - RtlpCreateUnicodeString is obsolete and should be removed ASAP! */
71 BOOLEAN FASTCALL
72 RtlpCreateUnicodeString(
73 IN OUT PUNICODE_STRING UniDest,
74 IN PCWSTR Source,
75 IN POOL_TYPE PoolType);
76
77 VOID
78 NTAPI
79 RtlpLogException(IN PEXCEPTION_RECORD ExceptionRecord,
80 IN PCONTEXT ContextRecord,
81 IN PVOID ContextData,
82 IN ULONG Size);
83
84 #define ExRaiseStatus RtlRaiseStatus
85
86 static const UNICODE_STRING __emptyUnicodeString = {0};
87
88 /*
89 * NOTE: Alignment of the pointers is not verified!
90 */
91 #define ProbeForWriteGenericType(Ptr, Type) \
92 do { \
93 if ((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
94 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) { \
95 RtlRaiseStatus (STATUS_ACCESS_VIOLATION); \
96 } \
97 *(volatile Type *)(Ptr) = *(volatile Type *)(Ptr); \
98 } while (0)
99
100 #define ProbeForWriteBoolean(Ptr) ProbeForWriteGenericType(Ptr, BOOLEAN)
101 #define ProbeForWriteUchar(Ptr) ProbeForWriteGenericType(Ptr, UCHAR)
102 #define ProbeForWriteChar(Ptr) ProbeForWriteGenericType(Ptr, Char)
103 #define ProbeForWriteUshort(Ptr) ProbeForWriteGenericType(Ptr, USHORT)
104 #define ProbeForWriteShort(Ptr) ProbeForWriteGenericType(Ptr, SHORT)
105 #define ProbeForWriteUlong(Ptr) ProbeForWriteGenericType(Ptr, ULONG)
106 #define ProbeForWriteLong(Ptr) ProbeForWriteGenericType(Ptr, LONG)
107 #define ProbeForWriteUint(Ptr) ProbeForWriteGenericType(Ptr, UINT)
108 #define ProbeForWriteInt(Ptr) ProbeForWriteGenericType(Ptr, INT)
109 #define ProbeForWriteUlonglong(Ptr) ProbeForWriteGenericType(Ptr, ULONGLONG)
110 #define ProbeForWriteLonglong(Ptr) ProbeForWriteGenericType(Ptr, LONGLONG)
111 #define ProbeForWriteLonglong(Ptr) ProbeForWriteGenericType(Ptr, LONGLONG)
112 #define ProbeForWritePointer(Ptr) ProbeForWriteGenericType(Ptr, PVOID)
113 #define ProbeForWriteHandle(Ptr) ProbeForWriteGenericType(Ptr, HANDLE)
114 #define ProbeForWriteLangid(Ptr) ProbeForWriteGenericType(Ptr, LANGID)
115 #define ProbeForWriteLargeInteger(Ptr) ProbeForWriteGenericType(&(Ptr)->QuadPart, LONGLONG)
116 #define ProbeForWriteUlargeInteger(Ptr) ProbeForWriteGenericType(&(Ptr)->QuadPart, ULONGLONG)
117 #define ProbeForWriteUnicodeString(Ptr) ProbeForWriteGenericType(Ptr, UNICODE_STRING)
118
119 #define ProbeForReadGenericType(Ptr, Type, Default) \
120 (((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
121 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) ? \
122 ExRaiseStatus (STATUS_ACCESS_VIOLATION), Default : \
123 *(volatile Type *)(Ptr))
124
125 #define ProbeForReadBoolean(Ptr) ProbeForReadGenericType(Ptr, BOOLEAN, FALSE)
126 #define ProbeForReadUchar(Ptr) ProbeForReadGenericType(Ptr, UCHAR, 0)
127 #define ProbeForReadChar(Ptr) ProbeForReadGenericType(Ptr, CHAR, 0)
128 #define ProbeForReadUshort(Ptr) ProbeForReadGenericType(Ptr, USHORT, 0)
129 #define ProbeForReadShort(Ptr) ProbeForReadGenericType(Ptr, SHORT, 0)
130 #define ProbeForReadUlong(Ptr) ProbeForReadGenericType(Ptr, ULONG, 0)
131 #define ProbeForReadLong(Ptr) ProbeForReadGenericType(Ptr, LONG, 0)
132 #define ProbeForReadUint(Ptr) ProbeForReadGenericType(Ptr, UINT, 0)
133 #define ProbeForReadInt(Ptr) ProbeForReadGenericType(Ptr, INT, 0)
134 #define ProbeForReadUlonglong(Ptr) ProbeForReadGenericType(Ptr, ULONGLONG, 0)
135 #define ProbeForReadLonglong(Ptr) ProbeForReadGenericType(Ptr, LONGLONG, 0)
136 #define ProbeForReadPointer(Ptr) ProbeForReadGenericType(Ptr, PVOID, NULL)
137 #define ProbeForReadHandle(Ptr) ProbeForReadGenericType(Ptr, HANDLE, NULL)
138 #define ProbeForReadLangid(Ptr) ProbeForReadGenericType(Ptr, LANGID, 0)
139 #define ProbeForReadLargeInteger(Ptr) ((LARGE_INTEGER)ProbeForReadGenericType(&(Ptr)->QuadPart, LONGLONG, 0))
140 #define ProbeForReadUlargeInteger(Ptr) ((ULARGE_INTEGER)ProbeForReadGenericType(&(Ptr)->QuadPart, ULONGLONG, 0))
141 #define ProbeForReadUnicodeString(Ptr) ProbeForReadGenericType(Ptr, UNICODE_STRING, __emptyUnicodeString)
142
143 /*
144 * Inlined Probing Macros
145 */
146 static __inline
147 NTSTATUS
148 NTAPI
149 ProbeAndCaptureUnicodeString(OUT PUNICODE_STRING Dest,
150 IN KPROCESSOR_MODE CurrentMode,
151 IN PUNICODE_STRING UnsafeSrc)
152 {
153 NTSTATUS Status = STATUS_SUCCESS;
154 WCHAR *Buffer;
155 ASSERT(Dest != NULL);
156
157 /* Probe the structure and buffer*/
158 if(CurrentMode != KernelMode)
159 {
160 _SEH_TRY
161 {
162 *Dest = ProbeForReadUnicodeString(UnsafeSrc);
163 if(Dest->Buffer != NULL)
164 {
165 if (Dest->Length != 0)
166 {
167 ProbeForRead(Dest->Buffer,
168 Dest->Length,
169 sizeof(WCHAR));
170
171 /* Allocate space for the buffer */
172 Buffer = ExAllocatePoolWithTag(PagedPool,
173 Dest->Length + sizeof(WCHAR),
174 TAG('U', 'S', 'T', 'R'));
175 if (Buffer == NULL)
176 {
177 Status = STATUS_INSUFFICIENT_RESOURCES;
178 _SEH_LEAVE;
179 }
180
181 /* Copy it */
182 RtlCopyMemory(Buffer, Dest->Buffer, Dest->Length);
183 Buffer[Dest->Length / sizeof(WCHAR)] = UNICODE_NULL;
184
185 /* Set it as the buffer */
186 Dest->Buffer = Buffer;
187 }
188 else
189 {
190 /* sanitize structure */
191 Dest->MaximumLength = 0;
192 Dest->Buffer = NULL;
193 }
194 }
195 else
196 {
197 /* sanitize structure */
198 Dest->Length = 0;
199 Dest->MaximumLength = 0;
200 }
201 }
202 _SEH_HANDLE
203 {
204 Status = _SEH_GetExceptionCode();
205 }
206 _SEH_END;
207 }
208 else
209 {
210 /* Just copy the UNICODE_STRING structure, don't allocate new memory!
211 We trust the caller to supply valid pointers and data. */
212 *Dest = *UnsafeSrc;
213 }
214
215 /* Return */
216 return Status;
217 }
218
219 static __inline
220 VOID
221 NTAPI
222 ReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString,
223 IN KPROCESSOR_MODE CurrentMode)
224 {
225 if(CurrentMode != KernelMode && CapturedString->Buffer != NULL)
226 {
227 ExFreePool(CapturedString->Buffer);
228 }
229 }
230
231 /*
232 * generic information class probing code
233 */
234
235 #define ICIF_QUERY 0x1
236 #define ICIF_SET 0x2
237 #define ICIF_QUERY_SIZE_VARIABLE 0x4
238 #define ICIF_SET_SIZE_VARIABLE 0x8
239 #define ICIF_SIZE_VARIABLE (ICIF_QUERY_SIZE_VARIABLE | ICIF_SET_SIZE_VARIABLE)
240
241 typedef struct _INFORMATION_CLASS_INFO
242 {
243 ULONG RequiredSizeQUERY;
244 ULONG RequiredSizeSET;
245 ULONG AlignmentSET;
246 ULONG AlignmentQUERY;
247 ULONG Flags;
248 } INFORMATION_CLASS_INFO, *PINFORMATION_CLASS_INFO;
249
250 #define ICI_SQ_SAME(Size, Alignment, Flags) \
251 { Size, Size, Alignment, Alignment, Flags }
252
253 #define ICI_SQ(SizeQuery, SizeSet, AlignmentQuery, AlignmentSet, Flags) \
254 { SizeQuery, SizeSet, AlignmentQuery, AlignmentSet, Flags }
255
256 static inline NTSTATUS
257 DefaultSetInfoBufferCheck(UINT Class,
258 const INFORMATION_CLASS_INFO *ClassList,
259 UINT ClassListEntries,
260 PVOID Buffer,
261 ULONG BufferLength,
262 KPROCESSOR_MODE PreviousMode)
263 {
264 NTSTATUS Status = STATUS_SUCCESS;
265
266 if (Class >= 0 && Class < ClassListEntries)
267 {
268 if (!(ClassList[Class].Flags & ICIF_SET))
269 {
270 Status = STATUS_INVALID_INFO_CLASS;
271 }
272 else if (ClassList[Class].RequiredSizeSET > 0 &&
273 BufferLength != ClassList[Class].RequiredSizeSET)
274 {
275 if (!(ClassList[Class].Flags & ICIF_SET_SIZE_VARIABLE))
276 {
277 Status = STATUS_INFO_LENGTH_MISMATCH;
278 }
279 }
280
281 if (NT_SUCCESS(Status))
282 {
283 if (PreviousMode != KernelMode)
284 {
285 _SEH_TRY
286 {
287 ProbeForRead(Buffer,
288 BufferLength,
289 ClassList[Class].AlignmentSET);
290 }
291 _SEH_HANDLE
292 {
293 Status = _SEH_GetExceptionCode();
294 }
295 _SEH_END;
296 }
297 }
298 }
299 else
300 Status = STATUS_INVALID_INFO_CLASS;
301
302 return Status;
303 }
304
305 static inline NTSTATUS
306 DefaultQueryInfoBufferCheck(UINT Class,
307 const INFORMATION_CLASS_INFO *ClassList,
308 UINT ClassListEntries,
309 PVOID Buffer,
310 ULONG BufferLength,
311 PULONG ReturnLength,
312 KPROCESSOR_MODE PreviousMode)
313 {
314 NTSTATUS Status = STATUS_SUCCESS;
315
316 if (Class >= 0 && Class < ClassListEntries)
317 {
318 if (!(ClassList[Class].Flags & ICIF_QUERY))
319 {
320 Status = STATUS_INVALID_INFO_CLASS;
321 }
322 else if (ClassList[Class].RequiredSizeQUERY > 0 &&
323 BufferLength != ClassList[Class].RequiredSizeQUERY)
324 {
325 if (!(ClassList[Class].Flags & ICIF_QUERY_SIZE_VARIABLE))
326 {
327 Status = STATUS_INFO_LENGTH_MISMATCH;
328 }
329 }
330
331 if (NT_SUCCESS(Status))
332 {
333 if (PreviousMode != KernelMode)
334 {
335 _SEH_TRY
336 {
337 if (Buffer != NULL)
338 {
339 ProbeForWrite(Buffer,
340 BufferLength,
341 ClassList[Class].AlignmentQUERY);
342 }
343
344 if (ReturnLength != NULL)
345 {
346 ProbeForWriteUlong(ReturnLength);
347 }
348 }
349 _SEH_HANDLE
350 {
351 Status = _SEH_GetExceptionCode();
352 }
353 _SEH_END;
354 }
355 }
356 }
357 else
358 Status = STATUS_INVALID_INFO_CLASS;
359
360 return Status;
361 }
362
363 /*
364 * Use IsPointerOffset to test whether a pointer should be interpreted as an offset
365 * or as a pointer
366 */
367 #if defined(_X86_) || defined(_M_AMD64)
368
369 /* for x86 and x86-64 the MSB is 1 so we can simply test on that */
370 #define IsPointerOffset(Ptr) ((LONG_PTR)(Ptr) >= 0)
371
372 #elif defined(_IA64_)
373
374 /* on Itanium if the 24 most significant bits are set, we're not dealing with
375 offsets anymore. */
376 #define IsPointerOffset(Ptr) (((ULONG_PTR)(Ptr) & 0xFFFFFF0000000000ULL) == 0)
377
378 #else
379 #error IsPointerOffset() needs to be defined for this architecture
380 #endif
381
382 #endif
383 /*
384 *
385 */
386 #define MM_STACK_SIZE (3*4096)
387
388 #endif /* INCLUDE_INTERNAL_NTOSKRNL_H */