fixed ProbeAndCaptureUnicodeString to properly capture unicode strings
[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 IN KPROCESSOR_MODE CurrentMode,
94 IN PUNICODE_STRING UnsafeSrc)
95 {
96 NTSTATUS Status = STATUS_SUCCESS;
97 WCHAR *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->Buffer != NULL)
110 {
111 if (Dest->Length != 0)
112 {
113 ProbeForRead(Dest->Buffer,
114 Dest->Length,
115 sizeof(WCHAR));
116
117 /* Allocate space for the buffer */
118 Buffer = ExAllocatePoolWithTag(PagedPool,
119 Dest->Length + sizeof(WCHAR),
120 TAG('U', 'S', 'T', 'R'));
121 if (Buffer == NULL)
122 {
123 Status = STATUS_INSUFFICIENT_RESOURCES;
124 _SEH_LEAVE;
125 }
126
127 /* Copy it */
128 RtlCopyMemory(Buffer, Dest->Buffer, Dest->Length);
129 Buffer[Dest->Length / sizeof(WCHAR)] = UNICODE_NULL;
130
131 /* Set it as the buffer */
132 Dest->Buffer = Buffer;
133 }
134 else
135 {
136 /* sanitize structure */
137 Dest->Length = 0;
138 Dest->MaximumLength = 0;
139 Dest->Buffer = NULL;
140 }
141 }
142 else
143 {
144 /* sanitize structure */
145 Dest->Length = 0;
146 Dest->MaximumLength = 0;
147 }
148 }
149 _SEH_HANDLE
150 {
151 Status = _SEH_GetExceptionCode();
152 }
153 _SEH_END;
154 }
155 else
156 {
157 /* Just copy the UNICODE_STRING structure, don't allocate new memory!
158 We trust the caller to supply valid pointers and data. */
159 *Dest = *UnsafeSrc;
160 }
161
162 /* Return */
163 return Status;
164 }
165
166 static __inline
167 VOID
168 NTAPI
169 ReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString,
170 IN KPROCESSOR_MODE CurrentMode)
171 {
172 if(CurrentMode != KernelMode && CapturedString->Buffer != NULL)
173 {
174 ExFreePool(CapturedString->Buffer);
175 }
176 }
177
178 /*
179 * NOTE: Alignment of the pointers is not verified!
180 */
181 #define ProbeForWriteGenericType(Ptr, Type) \
182 do { \
183 if ((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
184 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) { \
185 RtlRaiseStatus (STATUS_ACCESS_VIOLATION); \
186 } \
187 *(volatile Type *)(Ptr) = *(volatile Type *)(Ptr); \
188 } while (0)
189
190 #define ProbeForWriteBoolean(Ptr) ProbeForWriteGenericType(Ptr, BOOLEAN)
191 #define ProbeForWriteUchar(Ptr) ProbeForWriteGenericType(Ptr, UCHAR)
192 #define ProbeForWriteChar(Ptr) ProbeForWriteGenericType(Ptr, Char)
193 #define ProbeForWriteUshort(Ptr) ProbeForWriteGenericType(Ptr, USHORT)
194 #define ProbeForWriteShort(Ptr) ProbeForWriteGenericType(Ptr, SHORT)
195 #define ProbeForWriteUlong(Ptr) ProbeForWriteGenericType(Ptr, ULONG)
196 #define ProbeForWriteLong(Ptr) ProbeForWriteGenericType(Ptr, LONG)
197 #define ProbeForWriteUint(Ptr) ProbeForWriteGenericType(Ptr, UINT)
198 #define ProbeForWriteInt(Ptr) ProbeForWriteGenericType(Ptr, INT)
199 #define ProbeForWriteUlonglong(Ptr) ProbeForWriteGenericType(Ptr, ULONGLONG)
200 #define ProbeForWriteLonglong(Ptr) ProbeForWriteGenericType(Ptr, LONGLONG)
201 #define ProbeForWriteLonglong(Ptr) ProbeForWriteGenericType(Ptr, LONGLONG)
202 #define ProbeForWritePointer(Ptr) ProbeForWriteGenericType(Ptr, PVOID)
203 #define ProbeForWriteHandle(Ptr) ProbeForWriteGenericType(Ptr, HANDLE)
204 #define ProbeForWriteLangid(Ptr) ProbeForWriteGenericType(Ptr, LANGID)
205 #define ProbeForWriteLargeInteger(Ptr) ProbeForWriteGenericType(&(Ptr)->QuadPart, LONGLONG)
206 #define ProbeForWriteUlargeInteger(Ptr) ProbeForWriteGenericType(&(Ptr)->QuadPart, ULONGLONG)
207
208 #define ProbeForReadGenericType(Ptr, Type, Default) \
209 (((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
210 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) ? \
211 ExRaiseStatus (STATUS_ACCESS_VIOLATION), Default : \
212 *(Type *)(Ptr))
213
214 #define ProbeForReadBoolean(Ptr) ProbeForReadGenericType(Ptr, BOOLEAN, FALSE)
215 #define ProbeForReadUchar(Ptr) ProbeForReadGenericType(Ptr, UCHAR, 0)
216 #define ProbeForReadChar(Ptr) ProbeForReadGenericType(Ptr, CHAR, 0)
217 #define ProbeForReadUshort(Ptr) ProbeForReadGenericType(Ptr, USHORT, 0)
218 #define ProbeForReadShort(Ptr) ProbeForReadGenericType(Ptr, SHORT, 0)
219 #define ProbeForReadUlong(Ptr) ProbeForReadGenericType(Ptr, ULONG, 0)
220 #define ProbeForReadLong(Ptr) ProbeForReadGenericType(Ptr, LONG, 0)
221 #define ProbeForReadUint(Ptr) ProbeForReadGenericType(Ptr, UINT, 0)
222 #define ProbeForReadInt(Ptr) ProbeForReadGenericType(Ptr, INT, 0)
223 #define ProbeForReadUlonglong(Ptr) ProbeForReadGenericType(Ptr, ULONGLONG, 0)
224 #define ProbeForReadLonglong(Ptr) ProbeForReadGenericType(Ptr, LONGLONG, 0)
225 #define ProbeForReadPointer(Ptr) ProbeForReadGenericType(Ptr, PVOID, NULL)
226 #define ProbeForReadHandle(Ptr) ProbeForReadGenericType(Ptr, HANDLE, NULL)
227 #define ProbeForReadLangid(Ptr) ProbeForReadGenericType(Ptr, LANGID, 0)
228 #define ProbeForReadLargeInteger(Ptr) ((LARGE_INTEGER)ProbeForReadGenericType(&(Ptr)->QuadPart, LONGLONG, 0))
229 #define ProbeForReadUlargeInteger(Ptr) ((ULARGE_INTEGER)ProbeForReadGenericType(&(Ptr)->QuadPart, ULONGLONG, 0))
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 */