- Pass pointer to debugprint function to the kernel in LoaderBlock->u.I386.CommonDataArea
[reactos.git] / reactos / ntoskrnl / kd64 / amd64 / kdsup.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/kd64/amd64/kdsup.c
5 * PURPOSE: KD support routines for AMD64
6 * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
7 */
8
9 /* INCLUDES *****************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 #undef UNIMPLEMENTED
16 #define UNIMPLEMENTED KdpDprintf("%s is unimplemented\n", __FUNCTION__)
17
18 /* FUNCTIONS *****************************************************************/
19
20 VOID
21 NTAPI
22 KdpSysGetVersion(IN PDBGKD_GET_VERSION64 Version)
23 {
24 /* Copy the version block */
25 RtlCopyMemory(Version, &KdVersionBlock, sizeof(DBGKD_GET_VERSION64));
26 }
27
28 VOID
29 NTAPI
30 KdpGetStateChange(IN PDBGKD_MANIPULATE_STATE64 State,
31 IN PCONTEXT Context)
32 {
33 PKPRCB Prcb;
34 ULONG i;
35
36 /* Check for success */
37 if (NT_SUCCESS(State->u.Continue2.ContinueStatus))
38 {
39 /* Check if we're tracing */
40 if (State->u.Continue2.ControlSet.TraceFlag)
41 {
42 /* Enable TF */
43 Context->EFlags |= EFLAGS_TF;
44 }
45 else
46 {
47 /* Remove it */
48 Context->EFlags &= ~EFLAGS_TF;
49 }
50
51 /* Loop all processors */
52 for (i = 0; i < KeNumberProcessors; i++)
53 {
54 /* Get the PRCB and update DR7 and DR6 */
55 Prcb = KiProcessorBlock[i];
56 Prcb->ProcessorState.SpecialRegisters.KernelDr7 =
57 State->u.Continue2.ControlSet.Dr7;
58 Prcb->ProcessorState.SpecialRegisters.KernelDr6 = 0;
59 }
60
61 /* Check if we have new symbol information */
62 if (State->u.Continue2.ControlSet.CurrentSymbolStart != 1)
63 {
64 /* Update it */
65 KdpCurrentSymbolStart =
66 State->u.Continue2.ControlSet.CurrentSymbolStart;
67 KdpCurrentSymbolEnd= State->u.Continue2.ControlSet.CurrentSymbolEnd;
68 }
69 }
70 }
71
72 VOID
73 NTAPI
74 KdpSetContextState(IN PDBGKD_WAIT_STATE_CHANGE64 WaitStateChange,
75 IN PCONTEXT Context)
76 {
77 PKPRCB Prcb = KeGetCurrentPrcb();
78
79 /* Copy i386 specific debug registers */
80 WaitStateChange->ControlReport.Dr6 = Prcb->ProcessorState.SpecialRegisters.
81 KernelDr6;
82 WaitStateChange->ControlReport.Dr7 = Prcb->ProcessorState.SpecialRegisters.
83 KernelDr7;
84
85 /* Copy i386 specific segments */
86 WaitStateChange->ControlReport.SegCs = (USHORT)Context->SegCs;
87 WaitStateChange->ControlReport.SegDs = (USHORT)Context->SegDs;
88 WaitStateChange->ControlReport.SegEs = (USHORT)Context->SegEs;
89 WaitStateChange->ControlReport.SegFs = (USHORT)Context->SegFs;
90
91 /* Copy EFlags */
92 WaitStateChange->ControlReport.EFlags = Context->EFlags;
93
94 /* Set Report Flags */
95 WaitStateChange->ControlReport.ReportFlags = REPORT_INCLUDES_SEGS;
96 if (WaitStateChange->ControlReport.SegCs == KGDT_64_R0_CODE)
97 {
98 WaitStateChange->ControlReport.ReportFlags |= REPORT_STANDARD_CS;
99 }
100 }
101
102 NTSTATUS
103 NTAPI
104 KdpSysReadMsr(IN ULONG Msr,
105 OUT PLARGE_INTEGER MsrValue)
106 {
107 /* Use SEH to protect from invalid MSRs */
108 _SEH2_TRY
109 {
110 MsrValue->QuadPart = __readmsr(Msr);
111 }
112 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
113 {
114 _SEH2_YIELD(return STATUS_NO_SUCH_DEVICE);
115 }
116 _SEH2_END
117
118 return STATUS_SUCCESS;
119 }
120
121 NTSTATUS
122 NTAPI
123 KdpSysWriteMsr(IN ULONG Msr,
124 IN PLARGE_INTEGER MsrValue)
125 {
126 /* Use SEH to protect from invalid MSRs */
127 _SEH2_TRY
128 {
129 __writemsr(Msr, MsrValue->QuadPart);
130 }
131 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
132 {
133 _SEH2_YIELD(return STATUS_NO_SUCH_DEVICE);
134 }
135 _SEH2_END
136
137 return STATUS_SUCCESS;
138 }
139
140 NTSTATUS
141 NTAPI
142 KdpSysReadBusData(IN ULONG BusDataType,
143 IN ULONG BusNumber,
144 IN ULONG SlotNumber,
145 IN ULONG Offset,
146 IN PVOID Buffer,
147 IN ULONG Length,
148 OUT PULONG ActualLength)
149 {
150 UNIMPLEMENTED;
151 while (TRUE);
152 return STATUS_UNSUCCESSFUL;
153 }
154
155 NTSTATUS
156 NTAPI
157 KdpSysWriteBusData(IN ULONG BusDataType,
158 IN ULONG BusNumber,
159 IN ULONG SlotNumber,
160 IN ULONG Offset,
161 IN PVOID Buffer,
162 IN ULONG Length,
163 OUT PULONG ActualLength)
164 {
165 UNIMPLEMENTED;
166 while (TRUE);
167 return STATUS_UNSUCCESSFUL;
168 }
169
170 NTSTATUS
171 NTAPI
172 KdpSysReadControlSpace(IN ULONG Processor,
173 IN ULONG64 BaseAddress,
174 IN PVOID Buffer,
175 IN ULONG Length,
176 OUT PULONG ActualLength)
177 {
178 PVOID ControlStart;
179 PKPRCB Prcb = KiProcessorBlock[Processor];
180 PKIPCR Pcr = CONTAINING_RECORD(Prcb, KIPCR, Prcb);
181
182 switch (BaseAddress)
183 {
184 case AMD64_DEBUG_CONTROL_SPACE_KPCR:
185 /* Copy a pointer to the Pcr */
186 ControlStart = &Pcr;
187 *ActualLength = sizeof(PVOID);
188 break;
189
190 case AMD64_DEBUG_CONTROL_SPACE_KPRCB:
191 /* Copy a pointer to the Prcb */
192 ControlStart = &Prcb;
193 *ActualLength = sizeof(PVOID);
194 break;
195
196 case AMD64_DEBUG_CONTROL_SPACE_KSPECIAL:
197 /* Copy SpecialRegisters */
198 ControlStart = &Prcb->ProcessorState.SpecialRegisters;
199 *ActualLength = sizeof(KSPECIAL_REGISTERS);
200 break;
201
202 case AMD64_DEBUG_CONTROL_SPACE_KTHREAD:
203 /* Copy a pointer to the current Thread */
204 ControlStart = &Prcb->CurrentThread;
205 *ActualLength = sizeof(PVOID);
206 break;
207
208 default:
209 *ActualLength = 0;
210 ASSERT(FALSE);
211 return STATUS_UNSUCCESSFUL;
212 }
213
214 /* Copy the memory */
215 RtlCopyMemory(Buffer, ControlStart, min(Length, *ActualLength));
216
217 /* Finish up */
218 return STATUS_SUCCESS;
219 }
220
221 NTSTATUS
222 NTAPI
223 KdpSysWriteControlSpace(IN ULONG Processor,
224 IN ULONG64 BaseAddress,
225 IN PVOID Buffer,
226 IN ULONG Length,
227 OUT PULONG ActualLength)
228 {
229 UNIMPLEMENTED;
230 while (TRUE);
231 return STATUS_UNSUCCESSFUL;
232 }
233
234 NTSTATUS
235 NTAPI
236 KdpSysReadIoSpace(IN ULONG InterfaceType,
237 IN ULONG BusNumber,
238 IN ULONG AddressSpace,
239 IN ULONG64 IoAddress,
240 OUT PVOID DataValue,
241 IN ULONG DataSize,
242 OUT PULONG ActualDataSize)
243 {
244 /* Verify parameters */
245 if (InterfaceType != Isa || BusNumber != 0 || AddressSpace != 1)
246 {
247 /* No data was read */
248 *ActualDataSize = 0;
249 return STATUS_INVALID_PARAMETER;
250 }
251
252 /* Check for correct alignment */
253 if ((IoAddress & (DataSize - 1)))
254 {
255 /* Invalid alignment */
256 *ActualDataSize = 0;
257 return STATUS_DATATYPE_MISALIGNMENT;
258 }
259
260 switch (DataSize)
261 {
262 case sizeof(UCHAR):
263 /* Read one UCHAR */
264 *(PUCHAR)DataValue = READ_PORT_UCHAR((PUCHAR)IoAddress);
265 break;
266
267 case sizeof(USHORT):
268 /* Read one USHORT */
269 *(PUSHORT)DataValue = READ_PORT_USHORT((PUSHORT)IoAddress);
270 break;
271
272 case sizeof(ULONG):
273 /* Read one ULONG */
274 *(PULONG)DataValue = READ_PORT_ULONG((PULONG)IoAddress);
275 break;
276
277 default:
278 /* Invalid data size */
279 *ActualDataSize = 0;
280 return STATUS_INVALID_PARAMETER;
281 }
282
283 /* Return the size of the data */
284 *ActualDataSize = DataSize;
285
286 /* Success! */
287 return STATUS_SUCCESS;
288 }
289
290
291 NTSTATUS
292 NTAPI
293 KdpSysWriteIoSpace(IN ULONG InterfaceType,
294 IN ULONG BusNumber,
295 IN ULONG AddressSpace,
296 IN ULONG64 IoAddress,
297 IN PVOID DataValue,
298 IN ULONG DataSize,
299 OUT PULONG ActualDataSize)
300 {
301 /* Verify parameters */
302 if (InterfaceType != Isa || BusNumber != 0 || AddressSpace != 1)
303 {
304 /* No data was written */
305 *ActualDataSize = 0;
306 return STATUS_INVALID_PARAMETER;
307 }
308
309 /* Check for correct alignment */
310 if ((IoAddress & (DataSize - 1)))
311 {
312 /* Invalid alignment */
313 *ActualDataSize = 0;
314 return STATUS_DATATYPE_MISALIGNMENT;
315 }
316
317 switch (DataSize)
318 {
319 case sizeof(UCHAR):
320 /* Write one UCHAR */
321 WRITE_PORT_UCHAR((PUCHAR)IoAddress, *(PUCHAR)DataValue);
322 break;
323
324 case sizeof(USHORT):
325 /* Write one USHORT */
326 WRITE_PORT_USHORT((PUSHORT)IoAddress, *(PUSHORT)DataValue);
327 break;
328
329 case sizeof(ULONG):
330 /* Write one ULONG */
331 WRITE_PORT_ULONG((PULONG)IoAddress, *(PULONG)DataValue);
332 break;
333
334 default:
335 /* Invalid data size */
336 *ActualDataSize = 0;
337 return STATUS_INVALID_PARAMETER;
338 }
339
340 /* Return the size of the data */
341 *ActualDataSize = DataSize;
342
343 /* Success! */
344 return STATUS_SUCCESS;
345 }
346
347 NTSTATUS
348 NTAPI
349 KdpSysCheckLowMemory(IN ULONG Flags)
350 {
351 UNIMPLEMENTED;
352 while (TRUE);
353 return STATUS_UNSUCCESSFUL;
354 }