convert DefaultSetInfoBufferCheck and DefaultQueryInfoBufferCheck to inlined functions
[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 /*
87 * Inlined Probing Macros
88 */
89 static __inline
90 NTSTATUS
91 NTAPI
92 ProbeAndCaptureUnicodeString(OUT PUNICODE_STRING Dest,
93 KPROCESSOR_MODE CurrentMode,
94 IN PUNICODE_STRING UnsafeSrc)
95 {
96 NTSTATUS Status = STATUS_SUCCESS;
97 PVOID Buffer;
98 ASSERT(Dest != NULL);
99
100 /* Probe the structure and buffer*/
101 if(CurrentMode != KernelMode)
102 {
103 _SEH_TRY
104 {
105 ProbeForRead(UnsafeSrc,
106 sizeof(UNICODE_STRING),
107 sizeof(ULONG));
108 *Dest = *UnsafeSrc;
109 if(Dest->Length > 0)
110 {
111 ProbeForRead(Dest->Buffer,
112 Dest->Length,
113 sizeof(WCHAR));
114 }
115 }
116 _SEH_HANDLE
117 {
118 Status = _SEH_GetExceptionCode();
119 }
120 _SEH_END;
121
122 if (!NT_SUCCESS(Status)) return Status;
123 }
124 else
125 {
126 /* Just copy it directly */
127 *Dest = *UnsafeSrc;
128 }
129
130 /* Allocate space for the buffer */
131 Buffer = ExAllocatePool(PagedPool, Dest->MaximumLength);
132
133 if (Buffer != NULL)
134 {
135 /* Copy it */
136 RtlCopyMemory(Buffer, Dest->Buffer, Dest->MaximumLength);
137
138 /* Set it as the buffer */
139 Dest->Buffer = Buffer;
140 }
141 else
142 Status = STATUS_INSUFFICIENT_RESOURCES;
143
144 /* Return */
145 return Status;
146 }
147
148 static __inline
149 VOID
150 NTAPI
151 ReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString,
152 KPROCESSOR_MODE CurrentMode)
153 {
154 if(CurrentMode != KernelMode) ExFreePool(CapturedString->Buffer);
155 }
156
157 /*
158 * NOTE: Alignment of the pointers is not verified!
159 */
160 #define ProbeForWriteGenericType(Ptr, Type) \
161 do { \
162 if ((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
163 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) { \
164 RtlRaiseStatus (STATUS_ACCESS_VIOLATION); \
165 } \
166 *(volatile Type *)(Ptr) = *(volatile Type *)(Ptr); \
167 } while (0)
168
169 #define ProbeForWriteBoolean(Ptr) ProbeForWriteGenericType(Ptr, BOOLEAN)
170 #define ProbeForWriteUchar(Ptr) ProbeForWriteGenericType(Ptr, UCHAR)
171 #define ProbeForWriteChar(Ptr) ProbeForWriteGenericType(Ptr, Char)
172 #define ProbeForWriteUshort(Ptr) ProbeForWriteGenericType(Ptr, USHORT)
173 #define ProbeForWriteShort(Ptr) ProbeForWriteGenericType(Ptr, SHORT)
174 #define ProbeForWriteUlong(Ptr) ProbeForWriteGenericType(Ptr, ULONG)
175 #define ProbeForWriteLong(Ptr) ProbeForWriteGenericType(Ptr, LONG)
176 #define ProbeForWriteUint(Ptr) ProbeForWriteGenericType(Ptr, UINT)
177 #define ProbeForWriteInt(Ptr) ProbeForWriteGenericType(Ptr, INT)
178 #define ProbeForWriteUlonglong(Ptr) ProbeForWriteGenericType(Ptr, ULONGLONG)
179 #define ProbeForWriteLonglong(Ptr) ProbeForWriteGenericType(Ptr, LONGLONG)
180 #define ProbeForWriteLonglong(Ptr) ProbeForWriteGenericType(Ptr, LONGLONG)
181 #define ProbeForWritePointer(Ptr) ProbeForWriteGenericType(Ptr, PVOID)
182 #define ProbeForWriteHandle(Ptr) ProbeForWriteGenericType(Ptr, HANDLE)
183 #define ProbeForWriteLangid(Ptr) ProbeForWriteGenericType(Ptr, LANGID)
184 #define ProbeForWriteLargeInteger(Ptr) ProbeForWriteGenericType(&(Ptr)->QuadPart, LONGLONG)
185 #define ProbeForWriteUlargeInteger(Ptr) ProbeForWriteGenericType(&(Ptr)->QuadPart, ULONGLONG)
186
187 #define ProbeForReadGenericType(Ptr, Type, Default) \
188 (((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
189 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) ? \
190 ExRaiseStatus (STATUS_ACCESS_VIOLATION), Default : \
191 *(Type *)(Ptr))
192
193 #define ProbeForReadBoolean(Ptr) ProbeForReadGenericType(Ptr, BOOLEAN, FALSE)
194 #define ProbeForReadUchar(Ptr) ProbeForReadGenericType(Ptr, UCHAR, 0)
195 #define ProbeForReadChar(Ptr) ProbeForReadGenericType(Ptr, CHAR, 0)
196 #define ProbeForReadUshort(Ptr) ProbeForReadGenericType(Ptr, USHORT, 0)
197 #define ProbeForReadShort(Ptr) ProbeForReadGenericType(Ptr, SHORT, 0)
198 #define ProbeForReadUlong(Ptr) ProbeForReadGenericType(Ptr, ULONG, 0)
199 #define ProbeForReadLong(Ptr) ProbeForReadGenericType(Ptr, LONG, 0)
200 #define ProbeForReadUint(Ptr) ProbeForReadGenericType(Ptr, UINT, 0)
201 #define ProbeForReadInt(Ptr) ProbeForReadGenericType(Ptr, INT, 0)
202 #define ProbeForReadUlonglong(Ptr) ProbeForReadGenericType(Ptr, ULONGLONG, 0)
203 #define ProbeForReadLonglong(Ptr) ProbeForReadGenericType(Ptr, LONGLONG, 0)
204 #define ProbeForReadPointer(Ptr) ProbeForReadGenericType(Ptr, PVOID, NULL)
205 #define ProbeForReadHandle(Ptr) ProbeForReadGenericType(Ptr, HANDLE, NULL)
206 #define ProbeForReadLangid(Ptr) ProbeForReadGenericType(Ptr, LANGID, 0)
207 #define ProbeForReadLargeInteger(Ptr) ((LARGE_INTEGER)ProbeForReadGenericType(&(Ptr)->QuadPart, LONGLONG, 0))
208 #define ProbeForReadUlargeInteger(Ptr) ((ULARGE_INTEGER)ProbeForReadGenericType(&(Ptr)->QuadPart, ULONGLONG, 0))
209
210 /*
211 * generic information class probing code
212 */
213
214 #define ICIF_QUERY 0x1
215 #define ICIF_SET 0x2
216 #define ICIF_QUERY_SIZE_VARIABLE 0x4
217 #define ICIF_SET_SIZE_VARIABLE 0x8
218 #define ICIF_SIZE_VARIABLE (ICIF_QUERY_SIZE_VARIABLE | ICIF_SET_SIZE_VARIABLE)
219
220 typedef struct _INFORMATION_CLASS_INFO
221 {
222 ULONG RequiredSizeQUERY;
223 ULONG RequiredSizeSET;
224 ULONG AlignmentSET;
225 ULONG AlignmentQUERY;
226 ULONG Flags;
227 } INFORMATION_CLASS_INFO, *PINFORMATION_CLASS_INFO;
228
229 #define ICI_SQ_SAME(Size, Alignment, Flags) \
230 { Size, Size, Alignment, Alignment, Flags }
231
232 #define ICI_SQ(SizeQuery, SizeSet, AlignmentQuery, AlignmentSet, Flags) \
233 { SizeQuery, SizeSet, AlignmentQuery, AlignmentSet, Flags }
234
235 static inline NTSTATUS
236 DefaultSetInfoBufferCheck(UINT Class,
237 const INFORMATION_CLASS_INFO *ClassList,
238 UINT ClassListEntries,
239 PVOID Buffer,
240 ULONG BufferLength,
241 KPROCESSOR_MODE PreviousMode)
242 {
243 NTSTATUS Status = STATUS_SUCCESS;
244
245 if (Class >= 0 && Class < ClassListEntries)
246 {
247 if (!(ClassList[Class].Flags & ICIF_SET))
248 {
249 Status = STATUS_INVALID_INFO_CLASS;
250 }
251 else if (ClassList[Class].RequiredSizeSET > 0 &&
252 BufferLength != ClassList[Class].RequiredSizeSET)
253 {
254 if (!(ClassList[Class].Flags & ICIF_SET_SIZE_VARIABLE))
255 {
256 Status = STATUS_INFO_LENGTH_MISMATCH;
257 }
258 }
259
260 if (NT_SUCCESS(Status))
261 {
262 if (PreviousMode != KernelMode)
263 {
264 _SEH_TRY
265 {
266 ProbeForRead(Buffer,
267 BufferLength,
268 ClassList[Class].AlignmentSET);
269 }
270 _SEH_HANDLE
271 {
272 Status = _SEH_GetExceptionCode();
273 }
274 _SEH_END;
275 }
276 }
277 }
278 else
279 Status = STATUS_INVALID_INFO_CLASS;
280
281 return Status;
282 }
283
284 static inline NTSTATUS
285 DefaultQueryInfoBufferCheck(UINT Class,
286 const INFORMATION_CLASS_INFO *ClassList,
287 UINT ClassListEntries,
288 PVOID Buffer,
289 ULONG BufferLength,
290 PULONG ReturnLength,
291 KPROCESSOR_MODE PreviousMode)
292 {
293 NTSTATUS Status = STATUS_SUCCESS;
294
295 if (Class >= 0 && Class < ClassListEntries)
296 {
297 if (!(ClassList[Class].Flags & ICIF_QUERY))
298 {
299 Status = STATUS_INVALID_INFO_CLASS;
300 }
301 else if (ClassList[Class].RequiredSizeQUERY > 0 &&
302 BufferLength != ClassList[Class].RequiredSizeQUERY)
303 {
304 if (!(ClassList[Class].Flags & ICIF_QUERY_SIZE_VARIABLE))
305 {
306 Status = STATUS_INFO_LENGTH_MISMATCH;
307 }
308 }
309
310 if (NT_SUCCESS(Status))
311 {
312 if (PreviousMode != KernelMode)
313 {
314 _SEH_TRY
315 {
316 if (Buffer != NULL)
317 {
318 ProbeForWrite(Buffer,
319 BufferLength,
320 ClassList[Class].AlignmentQUERY);
321 }
322
323 if (ReturnLength != NULL)
324 {
325 ProbeForWriteUlong(ReturnLength);
326 }
327 }
328 _SEH_HANDLE
329 {
330 Status = _SEH_GetExceptionCode();
331 }
332 _SEH_END;
333 }
334 }
335 }
336 else
337 Status = STATUS_INVALID_INFO_CLASS;
338
339 return Status;
340 }
341
342 /*
343 * Use IsPointerOffset to test whether a pointer should be interpreted as an offset
344 * or as a pointer
345 */
346 #if defined(_X86_) || defined(_M_AMD64)
347
348 /* for x86 and x86-64 the MSB is 1 so we can simply test on that */
349 #define IsPointerOffset(Ptr) ((LONG_PTR)(Ptr) >= 0)
350
351 #elif defined(_IA64_)
352
353 /* on Itanium if the 24 most significant bits are set, we're not dealing with
354 offsets anymore. */
355 #define IsPointerOffset(Ptr) (((ULONG_PTR)(Ptr) & 0xFFFFFF0000000000ULL) == 0)
356
357 #else
358 #error IsPointerOffset() needs to be defined for this architecture
359 #endif
360
361 #endif
362 /*
363 *
364 */
365 #define MM_STACK_SIZE (3*4096)
366
367 #endif /* INCLUDE_INTERNAL_NTOSKRNL_H */