[NTOS:KD] Protect against invalid user arguments in KdpPrintString. CORE-14057
[reactos.git] / ntoskrnl / include / internal / kd.h
1 #pragma once
2
3 #include <cportlib/cportlib.h>
4
5 //
6 // Kernel Debugger Port Definition
7 //
8 struct _KD_DISPATCH_TABLE;
9 extern CPPORT GdbPortInfo;
10 extern BOOLEAN KdBreakAfterSymbolLoad;
11 extern BOOLEAN KdPitchDebugger;
12 extern BOOLEAN KdIgnoreUmExceptions;
13
14 BOOLEAN
15 NTAPI
16 KdPortInitializeEx(
17 PCPPORT PortInformation,
18 ULONG ComPortNumber
19 );
20
21 BOOLEAN
22 NTAPI
23 KdPortGetByteEx(
24 PCPPORT PortInformation,
25 PUCHAR ByteReceived);
26
27 VOID
28 NTAPI
29 KdPortPutByteEx(
30 PCPPORT PortInformation,
31 UCHAR ByteToSend
32 );
33
34 /* SYMBOL ROUTINES **********************************************************/
35 #ifdef __NTOSKRNL__
36
37 #if defined(KDBG) || DBG
38
39 #if defined(KDBG)
40 typedef
41 BOOLEAN
42 (NTAPI *PKDBG_CLI_ROUTINE)(
43 IN PCHAR Command,
44 IN ULONG Argc,
45 IN PCH Argv[]);
46
47 BOOLEAN
48 NTAPI
49 KdbRegisterCliCallback(
50 PVOID Callback,
51 BOOLEAN Deregister);
52 #endif
53
54 VOID
55 KdbSymProcessSymbols(
56 IN PLDR_DATA_TABLE_ENTRY LdrEntry);
57
58
59 BOOLEAN
60 KdbSymPrintAddress(
61 IN PVOID Address,
62 IN PKTRAP_FRAME Context
63 );
64
65 NTSTATUS
66 KdbSymGetAddressInformation(
67 IN PROSSYM_INFO RosSymInfo,
68 IN ULONG_PTR RelativeAddress,
69 #ifdef __ROS_DWARF__
70 IN PROSSYM_LINEINFO RosSymLineInfo
71 #else
72 OUT PULONG LineNumber OPTIONAL,
73 OUT PCH FileName OPTIONAL,
74 OUT PCH FunctionName OPTIONAL
75 #endif
76 );
77 #endif
78
79 #ifdef KDBG
80 # define KdbInit() KdbpCliInit()
81 # define KdbModuleLoaded(FILENAME) KdbpCliModuleLoaded(FILENAME)
82 #else
83 # define KdbEnterDebuggerException(ER, PM, C, TF, F) kdHandleException
84 # define KdbInit() do { } while (0)
85 # define KdbEnter() do { } while (0)
86 # define KdbModuleLoaded(X) do { } while (0)
87 #endif
88
89 /* KD ROUTINES ***************************************************************/
90
91 typedef enum _KD_CONTINUE_TYPE
92 {
93 kdContinue = 0,
94 kdDoNotHandleException,
95 kdHandleException
96 }
97 KD_CONTINUE_TYPE;
98
99 typedef
100 VOID
101 (NTAPI*PKDP_INIT_ROUTINE)(
102 struct _KD_DISPATCH_TABLE *DispatchTable,
103 ULONG BootPhase
104 );
105
106 typedef
107 VOID
108 (NTAPI*PKDP_PRINT_ROUTINE)(
109 LPSTR String,
110 ULONG Length
111 );
112
113 typedef
114 VOID
115 (NTAPI*PKDP_PROMPT_ROUTINE)(PCH String);
116
117 typedef
118 KD_CONTINUE_TYPE
119 (NTAPI*PKDP_EXCEPTION_ROUTINE)(
120 PEXCEPTION_RECORD ExceptionRecord,
121 PCONTEXT Context,
122 PKTRAP_FRAME TrapFrame
123 );
124
125 BOOLEAN
126 NTAPI
127 KdIsThisAKdTrap(
128 IN PEXCEPTION_RECORD ExceptionRecord,
129 IN PCONTEXT Context,
130 IN KPROCESSOR_MODE PreviousMode
131 );
132
133 /* INIT ROUTINES *************************************************************/
134
135 BOOLEAN
136 NTAPI
137 KdInitSystem(
138 ULONG Reserved,
139 PLOADER_PARAMETER_BLOCK LoaderBlock
140 );
141
142 VOID
143 NTAPI
144 KdpScreenInit(
145 struct _KD_DISPATCH_TABLE *DispatchTable,
146 ULONG BootPhase
147 );
148
149 VOID
150 NTAPI
151 KdpSerialInit(
152 struct _KD_DISPATCH_TABLE *DispatchTable,
153 ULONG BootPhase
154 );
155
156 VOID
157 NTAPI
158 KdpInitDebugLog(
159 struct _KD_DISPATCH_TABLE *DispatchTable,
160 ULONG BootPhase
161 );
162
163 VOID
164 NTAPI
165 KdpBochsInit(
166 struct _KD_DISPATCH_TABLE *DispatchTable,
167 ULONG BootPhase
168 );
169
170 VOID
171 NTAPI
172 KdpGdbStubInit(
173 struct _KD_DISPATCH_TABLE *DispatchTable,
174 ULONG BootPhase);
175
176 VOID
177 NTAPI
178 KdpKdbgInit(
179 struct _KD_DISPATCH_TABLE *DispatchTable,
180 ULONG BootPhase);
181
182
183 /* KD ROUTINES ***************************************************************/
184
185 BOOLEAN
186 NTAPI
187 KdpCallGdb(
188 IN PKTRAP_FRAME TrapFrame,
189 IN PEXCEPTION_RECORD ExceptionRecord,
190 IN PCONTEXT Context
191 );
192
193 ULONG
194 NTAPI
195 KdpPrintString(
196 _In_reads_bytes_(Length) PCHAR UnsafeString,
197 _In_ ULONG Length);
198
199 ULONG
200 NTAPI
201 KdpPrompt(
202 _In_reads_bytes_(InStringLength) PCHAR UnsafeInString,
203 _In_ USHORT InStringLength,
204 _Out_writes_bytes_(OutStringLength) PCHAR UnsafeOutString,
205 _In_ USHORT OutStringLength,
206 _In_ KPROCESSOR_MODE PreviousMode
207 );
208
209 BOOLEAN
210 NTAPI
211 KdpDetectConflicts(PCM_RESOURCE_LIST DriverList);
212
213 VOID
214 NTAPI
215 KdpBochsDebugPrint(
216 IN PCH Message,
217 IN ULONG Length
218 );
219
220 BOOLEAN
221 NTAPI
222 KdpSafeReadMemory(
223 IN ULONG_PTR Addr,
224 IN LONG Len,
225 OUT PVOID Value
226 );
227
228 BOOLEAN
229 NTAPI
230 KdpSafeWriteMemory(
231 IN ULONG_PTR Addr,
232 IN LONG Len,
233 IN ULONGLONG Value
234 );
235
236 VOID
237 NTAPI
238 KdpEnableSafeMem(VOID);
239
240
241 /* KD GLOBALS ***************************************************************/
242
243 typedef
244 BOOLEAN
245 (NTAPI *PKDEBUG_ROUTINE)(
246 IN PKTRAP_FRAME TrapFrame,
247 IN PKEXCEPTION_FRAME ExceptionFrame,
248 IN PEXCEPTION_RECORD ExceptionRecord,
249 IN PCONTEXT Context,
250 IN KPROCESSOR_MODE PreviousMode,
251 IN BOOLEAN SecondChance
252 );
253
254 /* serial debug connection */
255 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
256 #define DEFAULT_DEBUG_COM1_IRQ 4 /* COM1 IRQ */
257 #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */
258 #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */
259
260 /* KD Native Modes */
261 #define KdScreen 0
262 #define KdSerial 1
263 #define KdFile 2
264 #define KdBochs 3
265 #define KdKdbg 4
266 #define KdMax 5
267
268 /* KD Private Debug Modes */
269 typedef struct _KDP_DEBUG_MODE
270 {
271 union
272 {
273 struct
274 {
275 /* Native Modes */
276 UCHAR Screen :1;
277 UCHAR Serial :1;
278 UCHAR File :1;
279 UCHAR Bochs :1;
280
281 /* Currently Supported Wrappers */
282 UCHAR Pice :1;
283 UCHAR Gdb :1;
284 };
285
286 /* Generic Value */
287 ULONG Value;
288 };
289 }
290 KDP_DEBUG_MODE;
291
292 /* KD Internal Debug Services */
293 typedef enum _KDP_DEBUG_SERVICE
294 {
295 DumpNonPagedPool = 0x1e, /* a */
296 ManualBugCheck = 0x30, /* b */
297 DumpNonPagedPoolStats = 0x2e, /* c */
298 DumpNewNonPagedPool = 0x20, /* d */
299 DumpNewNonPagedPoolStats = 0x12, /* e */
300 DumpAllThreads = 0x21, /* f */
301 DumpUserThreads = 0x22, /* g */
302 KdSpare1 = 0x23, /* h */
303 KdSpare2 = 0x17, /* i */
304 KdSpare3 = 0x24, /* j */
305 EnterDebugger = 0x25, /* k */
306 ThatsWhatSheSaid = 69 /* FIGURE IT OUT */
307 }
308 KDP_DEBUG_SERVICE;
309
310 /* Dispatch Table for Wrapper Functions */
311 typedef struct _KD_DISPATCH_TABLE
312 {
313 LIST_ENTRY KdProvidersList;
314 PKDP_INIT_ROUTINE KdpInitRoutine;
315 PKDP_PRINT_ROUTINE KdpPrintRoutine;
316 PKDP_PROMPT_ROUTINE KdpPromptRoutine;
317 PKDP_EXCEPTION_ROUTINE KdpExceptionRoutine;
318 }
319 KD_DISPATCH_TABLE, *PKD_DISPATCH_TABLE;
320
321 /* The current Debugging Mode */
322 extern KDP_DEBUG_MODE KdpDebugMode;
323
324 /* The current Port IRQ */
325 extern ULONG KdpPortIrq;
326
327 /* The current Port */
328 extern ULONG KdpPort;
329
330 /* Port Information for the Serial Native Mode */
331 extern ULONG SerialPortNumber;
332 extern CPPORT SerialPortInfo;
333
334 /* Init Functions for Native Providers */
335 extern PKDP_INIT_ROUTINE InitRoutines[KdMax];
336
337 /* Wrapper Init Function */
338 extern PKDP_INIT_ROUTINE WrapperInitRoutine;
339
340 /* Dispatch Tables for Native Providers */
341 extern KD_DISPATCH_TABLE DispatchTable[KdMax];
342
343 /* Dispatch Table for the Wrapper */
344 extern KD_DISPATCH_TABLE WrapperTable;
345
346 /* The KD Native Provider List */
347 extern LIST_ENTRY KdProviders;
348
349 /* Whether to enter KDB as early as possible or not */
350 extern BOOLEAN KdpEarlyBreak;
351
352 extern PKDEBUG_ROUTINE KiDebugRoutine;
353 extern KD_CONTEXT KdpContext;
354 extern ULONG Kd_WIN2000_Mask;
355
356 #endif
357
358 #if DBG && defined(_M_IX86) && !defined(_WINKD_) // See ke/i386/traphdlr.c
359 #define ID_Win32PreServiceHook 'WSH0'
360 #define ID_Win32PostServiceHook 'WSH1'
361 typedef void (NTAPI *PKDBG_PRESERVICEHOOK)(ULONG, PULONG_PTR);
362 typedef ULONG_PTR (NTAPI *PKDBG_POSTSERVICEHOOK)(ULONG, ULONG_PTR);
363 extern PKDBG_PRESERVICEHOOK KeWin32PreServiceHook;
364 extern PKDBG_POSTSERVICEHOOK KeWin32PostServiceHook;
365 #endif