ReactOS  0.4.14-dev-323-g6fe6a88
kdx86.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: ntoskrnl/kd64/i386/kdx86.c
5  * PURPOSE: KD support routines for x86
6  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7  * Stefan Ginsberg (stefan.ginsberg@reactos.org)
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* FUNCTIONS *****************************************************************/
17 
18 VOID
19 NTAPI
22 {
23  PKPRCB Prcb;
24  ULONG i;
25 
26  /* Check for success */
27  if (NT_SUCCESS(State->u.Continue2.ContinueStatus))
28  {
29  /* Check if we're tracing */
30  if (State->u.Continue2.ControlSet.TraceFlag)
31  {
32  /* Enable TF */
33  Context->EFlags |= EFLAGS_TF;
34  }
35  else
36  {
37  /* Remove it */
38  Context->EFlags &= ~EFLAGS_TF;
39  }
40 
41  /* Loop all processors */
42  for (i = 0; i < KeNumberProcessors; i++)
43  {
44  /* Get the PRCB and update DR7 and DR6 */
45  Prcb = KiProcessorBlock[i];
47  State->u.Continue2.ControlSet.Dr7;
49  }
50 
51  /* Check if we have new symbol information */
52  if (State->u.Continue2.ControlSet.CurrentSymbolStart != 1)
53  {
54  /* Update it */
56  State->u.Continue2.ControlSet.CurrentSymbolStart;
57  KdpCurrentSymbolEnd= State->u.Continue2.ControlSet.CurrentSymbolEnd;
58  }
59  }
60 }
61 
62 VOID
63 NTAPI
66 {
67  PKPRCB Prcb = KeGetCurrentPrcb();
68 
69  /* Copy i386 specific debug registers */
70  WaitStateChange->ControlReport.Dr6 = Prcb->ProcessorState.SpecialRegisters.
71  KernelDr6;
72  WaitStateChange->ControlReport.Dr7 = Prcb->ProcessorState.SpecialRegisters.
73  KernelDr7;
74 
75  /* Copy i386 specific segments */
76  WaitStateChange->ControlReport.SegCs = (USHORT)Context->SegCs;
77  WaitStateChange->ControlReport.SegDs = (USHORT)Context->SegDs;
78  WaitStateChange->ControlReport.SegEs = (USHORT)Context->SegEs;
79  WaitStateChange->ControlReport.SegFs = (USHORT)Context->SegFs;
80 
81  /* Copy EFlags */
82  WaitStateChange->ControlReport.EFlags = Context->EFlags;
83 
84  /* Set Report Flags */
85  WaitStateChange->ControlReport.ReportFlags = REPORT_INCLUDES_SEGS;
86  if (WaitStateChange->ControlReport.SegCs == KGDT_R0_CODE)
87  {
88  WaitStateChange->ControlReport.ReportFlags |= REPORT_STANDARD_CS;
89  }
90 }
91 
93 NTAPI
95  OUT PLARGE_INTEGER MsrValue)
96 {
97  /* Wrap this in SEH in case the MSR doesn't exist */
98  _SEH2_TRY
99  {
100  /* Read from the MSR */
101  MsrValue->QuadPart = __readmsr(Msr);
102  }
104  {
105  /* Invalid MSR */
107  }
108  _SEH2_END;
109 
110  /* Success */
111  return STATUS_SUCCESS;
112 }
113 
114 NTSTATUS
115 NTAPI
117  IN PLARGE_INTEGER MsrValue)
118 {
119  /* Wrap this in SEH in case the MSR doesn't exist */
120  _SEH2_TRY
121  {
122  /* Write to the MSR */
123  __writemsr(Msr, MsrValue->QuadPart);
124  }
126  {
127  /* Invalid MSR */
129  }
130  _SEH2_END;
131 
132  /* Success */
133  return STATUS_SUCCESS;
134 }
135 
136 NTSTATUS
137 NTAPI
141  IN ULONG Offset,
142  IN PVOID Buffer,
143  IN ULONG Length,
144  OUT PULONG ActualLength)
145 {
146  /* Just forward to HAL */
147  *ActualLength = HalGetBusDataByOffset(BusDataType,
148  BusNumber,
149  SlotNumber,
150  Buffer,
151  Offset,
152  Length);
153 
154  /* Return status */
155  return *ActualLength != 0 ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
156 }
157 
158 NTSTATUS
159 NTAPI
163  IN ULONG Offset,
164  IN PVOID Buffer,
165  IN ULONG Length,
166  OUT PULONG ActualLength)
167 {
168  /* Just forward to HAL */
169  *ActualLength = HalSetBusDataByOffset(BusDataType,
170  BusNumber,
171  SlotNumber,
172  Buffer,
173  Offset,
174  Length);
175 
176  /* Return status */
177  return *ActualLength != 0 ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
178 }
179 
180 NTSTATUS
181 NTAPI
184  IN PVOID Buffer,
185  IN ULONG Length,
186  OUT PULONG ActualLength)
187 {
188  PVOID ControlStart;
189  ULONG RealLength;
190 
191  /* Make sure that this is a valid request */
192  if ((BaseAddress < sizeof(KPROCESSOR_STATE)) &&
194  {
195  /* Get the actual length */
196  RealLength = sizeof(KPROCESSOR_STATE) - (ULONG_PTR)BaseAddress;
197  if (RealLength < Length) Length = RealLength;
198 
199  /* Set the proper address */
200  ControlStart = (PVOID)((ULONG_PTR)BaseAddress +
202  ProcessorState);
203 
204  /* Read the control state safely */
206  ControlStart,
207  Length,
208  0,
210  ActualLength);
211  }
212  else
213  {
214  /* Invalid request */
215  *ActualLength = 0;
216  return STATUS_UNSUCCESSFUL;
217  }
218 }
219 
220 NTSTATUS
221 NTAPI
224  IN PVOID Buffer,
225  IN ULONG Length,
226  OUT PULONG ActualLength)
227 {
228  PVOID ControlStart;
229 
230  /* Make sure that this is a valid request */
231  if (((BaseAddress + Length) <= sizeof(KPROCESSOR_STATE)) &&
233  {
234  /* Set the proper address */
235  ControlStart = (PVOID)((ULONG_PTR)BaseAddress +
237  ProcessorState);
238 
239  /* Write the control state safely */
241  ControlStart,
242  Length,
243  0,
245  ActualLength);
246  }
247  else
248  {
249  /* Invalid request */
250  *ActualLength = 0;
251  return STATUS_UNSUCCESSFUL;
252  }
253 }
254 
255 NTSTATUS
256 NTAPI
257 KdpSysReadIoSpace(IN ULONG InterfaceType,
260  IN ULONG64 IoAddress,
261  IN PVOID DataValue,
262  IN ULONG DataSize,
263  OUT PULONG ActualDataSize)
264 {
266 
267  /* Verify parameters */
268  if ((InterfaceType != Isa) ||
269  (BusNumber != 0) ||
270  (AddressSpace != 1))
271  {
272  /* Fail, we don't support this */
273  *ActualDataSize = 0;
274  return STATUS_UNSUCCESSFUL;
275  }
276 
277  /* Check the size */
278  switch (DataSize)
279  {
280  case sizeof(UCHAR):
281 
282  /* Read 1 byte */
283  *(PUCHAR)DataValue =
284  READ_PORT_UCHAR((PUCHAR)(ULONG_PTR)IoAddress);
285  *ActualDataSize = sizeof(UCHAR);
287  break;
288 
289  case sizeof(USHORT):
290 
291  /* Make sure the address is aligned */
292  if ((IoAddress & (sizeof(USHORT) - 1)) != 0)
293  {
294  /* It isn't, bail out */
295  *ActualDataSize = 0;
297  break;
298  }
299 
300  /* Read 2 bytes */
301  *(PUSHORT)DataValue =
302  READ_PORT_USHORT((PUSHORT)(ULONG_PTR)IoAddress);
303  *ActualDataSize = sizeof(USHORT);
305  break;
306 
307  case sizeof(ULONG):
308 
309  /* Make sure the address is aligned */
310  if ((IoAddress & (sizeof(ULONG) - 1)) != 0)
311  {
312  /* It isn't, bail out */
313  *ActualDataSize = 0;
315  break;
316  }
317 
318  /* Read 4 bytes */
319  *(PULONG)DataValue =
320  READ_PORT_ULONG((PULONG)(ULONG_PTR)IoAddress);
321  *ActualDataSize = sizeof(ULONG);
323  break;
324 
325  default:
326 
327  /* Invalid size, fail */
328  *ActualDataSize = 0;
330  }
331 
332  /* Return status */
333  return Status;
334 }
335 
336 NTSTATUS
337 NTAPI
341  IN ULONG64 IoAddress,
342  IN PVOID DataValue,
343  IN ULONG DataSize,
344  OUT PULONG ActualDataSize)
345 {
347 
348  /* Verify parameters */
349  if ((InterfaceType != Isa) ||
350  (BusNumber != 0) ||
351  (AddressSpace != 1))
352  {
353  /* Fail, we don't support this */
354  *ActualDataSize = 0;
355  return STATUS_UNSUCCESSFUL;
356  }
357 
358  /* Check the size */
359  switch (DataSize)
360  {
361  case sizeof(UCHAR):
362 
363  /* Write 1 byte */
364  WRITE_PORT_UCHAR((PUCHAR)(ULONG_PTR)IoAddress,
365  *(PUCHAR)DataValue);
366  *ActualDataSize = sizeof(UCHAR);
368  break;
369 
370  case sizeof(USHORT):
371 
372  /* Make sure the address is aligned */
373  if ((IoAddress & (sizeof(USHORT) - 1)) != 0)
374  {
375  /* It isn't, bail out */
376  *ActualDataSize = 0;
378  break;
379  }
380 
381  /* Write 2 bytes */
382  WRITE_PORT_USHORT((PUSHORT)(ULONG_PTR)IoAddress,
383  *(PUSHORT)DataValue);
384  *ActualDataSize = sizeof(USHORT);
386  break;
387 
388  case sizeof(ULONG):
389 
390  /* Make sure the address is aligned */
391  if ((IoAddress & (sizeof(ULONG) - 1)) != 0)
392  {
393  /* It isn't, bail out */
394  *ActualDataSize = 0;
396  break;
397  }
398 
399  /* Write 4 bytes */
400  WRITE_PORT_ULONG((PULONG)(ULONG_PTR)IoAddress,
401  *(PULONG)DataValue);
402  *ActualDataSize = sizeof(ULONG);
404  break;
405 
406  default:
407 
408  /* Invalid size, fail */
409  *ActualDataSize = 0;
411  }
412 
413  /* Return status */
414  return Status;
415 }
416 
417 NTSTATUS
418 NTAPI
420 {
421  /* Stubbed as we don't support PAE */
422  return STATUS_UNSUCCESSFUL;
423 }
424 
425 NTSTATUS
426 NTAPI
428 {
429  LONG i;
430  ULONG Dr7;
431 
432  /* Loop every processor */
433  for (i = 0; i < KeNumberProcessors; i++)
434  {
435  /* Get its DR7 */
437 
438  /* Check if any processor breakpoints are active */
439  if (Dr7 != 0)
440  {
441  /* We can't allow running without a debugger then */
442  return STATUS_ACCESS_DENIED;
443  }
444  }
445 
446  /* No processor breakpoints; allow disabling the debugger */
447  return STATUS_SUCCESS;
448 }
NTSTATUS NTAPI KdpAllowDisable(VOID)
Definition: kdx86.c:427
#define IN
Definition: typedefs.h:38
NTSTATUS NTAPI KdpSysWriteIoSpace(IN ULONG InterfaceType, IN ULONG BusNumber, IN ULONG AddressSpace, IN ULONG64 IoAddress, IN PVOID DataValue, IN ULONG DataSize, OUT PULONG ActualDataSize)
Definition: kdx86.c:338
NTSTATUS NTAPI KdpSysCheckLowMemory(IN ULONG Flags)
Definition: kdx86.c:419
VOID NTAPI KdpSetContextState(IN PDBGKD_ANY_WAIT_STATE_CHANGE WaitStateChange, IN PCONTEXT Context)
Definition: kdx86.c:64
VOID NTAPI WRITE_PORT_USHORT(IN PUSHORT Port, IN USHORT Value)
Definition: portio.c:115
ULONG64 KernelDr7
Definition: ketypes.h:505
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_In_ ULONG _In_ PHYSICAL_ADDRESS _Inout_ PULONG AddressSpace
Definition: iofuncs.h:2268
unsigned char * PUCHAR
Definition: retypes.h:3
UCHAR NTAPI READ_PORT_UCHAR(PUCHAR Address)
Definition: mach.c:528
LONG NTSTATUS
Definition: precomp.h:26
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1062
ULONG NTAPI READ_PORT_ULONG(IN PULONG Port)
Definition: portio.c:70
#define KGDT_R0_CODE
Definition: ketypes.h:75
NTSTATUS NTAPI KdpSysReadControlSpace(IN ULONG Processor, IN ULONG64 BaseAddress, IN PVOID Buffer, IN ULONG Length, OUT PULONG ActualLength)
Definition: kdx86.c:182
_In_ UCHAR Processor
Definition: kefuncs.h:695
KSPECIAL_REGISTERS SpecialRegisters
Definition: ketypes.h:530
#define REPORT_STANDARD_CS
Definition: windbgkd.h:148
_SEH2_TRY
Definition: create.c:4250
NTSTATUS NTAPI KdpSysReadIoSpace(IN ULONG InterfaceType, IN ULONG BusNumber, IN ULONG AddressSpace, IN ULONG64 IoAddress, IN PVOID DataValue, IN ULONG DataSize, OUT PULONG ActualDataSize)
Definition: kdx86.c:257
uint32_t ULONG_PTR
Definition: typedefs.h:63
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
VOID NTAPI WRITE_PORT_ULONG(IN PULONG Port, IN ULONG Value)
Definition: portio.c:123
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
long LONG
Definition: pedump.c:60
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
ULONG_PTR KdpCurrentSymbolEnd
Definition: kddata.c:105
PPC_QUAL unsigned long long __readmsr()
Definition: intrin_ppc.h:741
Definition: bufpool.h:45
NTSTATUS NTAPI KdpSysReadMsr(IN ULONG Msr, OUT PLARGE_INTEGER MsrValue)
Definition: kdx86.c:94
#define MMDBG_COPY_WRITE
Definition: mm.h:53
void * PVOID
Definition: retypes.h:9
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG BusNumber
Definition: halfuncs.h:156
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_NO_SUCH_DEVICE
Definition: udferr_usr.h:136
NTSTATUS NTAPI KdpSysWriteControlSpace(IN ULONG Processor, IN ULONG64 BaseAddress, IN PVOID Buffer, IN ULONG Length, OUT PULONG ActualLength)
Definition: kdx86.c:222
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
KPROCESSOR_STATE ProcessorState
Definition: ketypes.h:579
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG _In_ ULONG SlotNumber
Definition: halfuncs.h:156
unsigned __int64 ULONG64
Definition: imports.h:198
unsigned char UCHAR
Definition: xmlstorage.h:181
ULONG NTAPI HalGetBusDataByOffset(IN BUS_DATA_TYPE BusDataType, IN ULONG BusNumber, IN ULONG SlotNumber, IN PVOID Buffer, IN ULONG Offset, IN ULONG Length)
Definition: bus.c:73
ULONG_PTR KdpCurrentSymbolStart
Definition: kddata.c:105
#define REPORT_INCLUDES_SEGS
Definition: windbgkd.h:147
#define EFLAGS_TF
Definition: ketypes.h:125
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
NTSTATUS NTAPI KdpSysWriteMsr(IN ULONG Msr, IN PLARGE_INTEGER MsrValue)
Definition: kdx86.c:116
PPC_QUAL void __writemsr(const unsigned long Value)
Definition: intrin_ppc.h:748
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
unsigned short USHORT
Definition: pedump.c:61
PKPRCB KiProcessorBlock[]
Definition: krnlinit.c:32
#define MMDBG_COPY_UNSAFE
Definition: mm.h:55
unsigned int * PULONG
Definition: retypes.h:1
VOID NTAPI KdpGetStateChange(IN PDBGKD_MANIPULATE_STATE64 State, IN PCONTEXT Context)
Definition: kdx86.c:20
NTSTATUS NTAPI KdpSysReadBusData(IN ULONG BusDataType, IN ULONG BusNumber, IN ULONG SlotNumber, IN ULONG Offset, IN PVOID Buffer, IN ULONG Length, OUT PULONG ActualLength)
Definition: kdx86.c:138
NTSTATUS NTAPI KdpSysWriteBusData(IN ULONG BusDataType, IN ULONG BusNumber, IN ULONG SlotNumber, IN ULONG Offset, IN PVOID Buffer, IN ULONG Length, OUT PULONG ActualLength)
Definition: kdx86.c:160
#define OUT
Definition: typedefs.h:39
#define STATUS_DATATYPE_MISALIGNMENT
Definition: ntstatus.h:171
unsigned int ULONG
Definition: retypes.h:1
ULONG64 KernelDr6
Definition: ketypes.h:504
void WRITE_PORT_UCHAR(PUCHAR Address, UCHAR Value)
Definition: mach.c:532
#define ULONG_PTR
Definition: config.h:101
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
USHORT NTAPI READ_PORT_USHORT(IN PUSHORT Port)
Definition: portio.c:63
ULONG NTAPI HalSetBusDataByOffset(IN BUS_DATA_TYPE BusDataType, IN ULONG BusNumber, IN ULONG SlotNumber, IN PVOID Buffer, IN ULONG Offset, IN ULONG Length)
Definition: bus.c:123
return STATUS_SUCCESS
Definition: btrfs.c:2938
struct _KPROCESSOR_STATE KPROCESSOR_STATE
_In_ NDIS_STATUS _In_ ULONG _In_ USHORT _In_opt_ PVOID _In_ ULONG DataSize
Definition: ndis.h:4751
NTSTATUS NTAPI KdpCopyMemoryChunks(IN ULONG64 Address, IN PVOID Buffer, IN ULONG TotalSize, IN ULONG ChunkSize, IN ULONG Flags, OUT PULONG ActualSize OPTIONAL)
Definition: kdapi.c:46
unsigned short * PUSHORT
Definition: retypes.h:2