- Add STATUS_SUCCESS to asm.h and make use of it.
[reactos.git] / reactos / ntoskrnl / kd64 / i386 / kdsup.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/kd64/i386/kdsup.c
5 * PURPOSE: KD support routines for x86
6 * PROGRAMMERS: Stefan Ginsberg (stefan.ginsberg@reactos.org)
7 */
8
9 /* INCLUDES *****************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* FUNCTIONS *****************************************************************/
16
17 VOID
18 NTAPI
19 KdpSysGetVersion(IN PDBGKD_GET_VERSION64 Version)
20 {
21 /* Copy the version block */
22 RtlCopyMemory(Version, &KdVersionBlock, sizeof(DBGKD_GET_VERSION64));
23 }
24
25 VOID
26 NTAPI
27 KdpGetStateChange(IN PDBGKD_MANIPULATE_STATE64 State,
28 IN PCONTEXT Context)
29 {
30 PKPRCB Prcb;
31 ULONG i;
32
33 /* Check for success */
34 if (NT_SUCCESS(State->u.Continue2.ContinueStatus))
35 {
36 /* Check if we're tracing */
37 if (State->u.Continue2.ControlSet.TraceFlag)
38 {
39 /* Enable TF */
40 Context->EFlags |= EFLAGS_TF;
41 }
42 else
43 {
44 /* Remove it */
45 Context->EFlags &= ~EFLAGS_TF;
46 }
47
48 /* Loop all processors */
49 for (i = 0; i < KeNumberProcessors; i++)
50 {
51 /* Get the PRCB and update DR7 and DR6 */
52 Prcb = KiProcessorBlock[i];
53 Prcb->ProcessorState.SpecialRegisters.KernelDr7 =
54 State->u.Continue2.ControlSet.Dr7;
55 Prcb->ProcessorState.SpecialRegisters.KernelDr6 = 0;
56 }
57
58 /* Check if we have new symbol information */
59 if (State->u.Continue2.ControlSet.CurrentSymbolStart != 1)
60 {
61 /* Update it */
62 KdpCurrentSymbolStart =
63 State->u.Continue2.ControlSet.CurrentSymbolStart;
64 KdpCurrentSymbolEnd= State->u.Continue2.ControlSet.CurrentSymbolEnd;
65 }
66 }
67 }
68
69 VOID
70 NTAPI
71 KdpSetContextState(IN PDBGKD_WAIT_STATE_CHANGE64 WaitStateChange,
72 IN PCONTEXT Context)
73 {
74 PKPRCB Prcb = KeGetCurrentPrcb();
75
76 /* Copy i386 specific debug registers */
77 WaitStateChange->ControlReport.Dr6 = Prcb->ProcessorState.SpecialRegisters.
78 KernelDr6;
79 WaitStateChange->ControlReport.Dr7 = Prcb->ProcessorState.SpecialRegisters.
80 KernelDr7;
81
82 /* Copy i386 specific segments */
83 WaitStateChange->ControlReport.SegCs = (USHORT)Context->SegCs;
84 WaitStateChange->ControlReport.SegDs = (USHORT)Context->SegDs;
85 WaitStateChange->ControlReport.SegEs = (USHORT)Context->SegEs;
86 WaitStateChange->ControlReport.SegFs = (USHORT)Context->SegFs;
87
88 /* Copy EFlags */
89 WaitStateChange->ControlReport.EFlags = Context->EFlags;
90
91 /* Set Report Flags */
92 WaitStateChange->ControlReport.ReportFlags = REPORT_INCLUDES_SEGS;
93 if (WaitStateChange->ControlReport.SegCs == KGDT_R0_CODE)
94 {
95 WaitStateChange->ControlReport.ReportFlags |= REPORT_STANDARD_CS;
96 }
97 }
98
99 NTSTATUS
100 NTAPI
101 KdpSysReadMsr(IN ULONG Msr,
102 OUT PLARGE_INTEGER MsrValue)
103 {
104 /* Wrap this in SEH in case the MSR doesn't exist */
105 //_SEH2_TRY
106 {
107 /* Read from the MSR */
108 MsrValue->QuadPart = RDMSR(Msr);
109 }
110 //_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
111 {
112 /* Invalid MSR */
113 //_SEH2_YIELD(return STATUS_NO_SUCH_DEVICE);
114 }
115 //_SEH2_END;
116
117 /* Success */
118 return STATUS_SUCCESS;
119 }
120
121 NTSTATUS
122 NTAPI
123 KdpSysWriteMsr(IN ULONG Msr,
124 IN PLARGE_INTEGER MsrValue)
125 {
126 /* Wrap this in SEH in case the MSR doesn't exist */
127 //_SEH2_TRY
128 {
129 /* Write to the MSR */
130 WRMSR(Msr, MsrValue->QuadPart);
131 }
132 //_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
133 {
134 /* Invalid MSR */
135 //_SEH2_YIELD(return STATUS_NO_SUCH_DEVICE);
136 }
137 //_SEH2_END;
138
139 /* Success */
140 return STATUS_SUCCESS;
141 }
142
143 NTSTATUS
144 NTAPI
145 KdpSysReadBusData(IN ULONG BusDataType,
146 IN ULONG BusNumber,
147 IN ULONG SlotNumber,
148 IN ULONG Offset,
149 IN PVOID Buffer,
150 IN ULONG Length,
151 OUT PULONG ActualLength)
152 {
153 /* Just forward to HAL */
154 *ActualLength = HalGetBusDataByOffset(BusDataType,
155 BusNumber,
156 SlotNumber,
157 Buffer,
158 Offset,
159 Length);
160
161 /* Return status */
162 return (*ActualLength != 0) ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
163 }
164
165 NTSTATUS
166 NTAPI
167 KdpSysWriteBusData(IN ULONG BusDataType,
168 IN ULONG BusNumber,
169 IN ULONG SlotNumber,
170 IN ULONG Offset,
171 IN PVOID Buffer,
172 IN ULONG Length,
173 OUT PULONG ActualLength)
174 {
175 /* Just forward to HAL */
176 *ActualLength = HalSetBusDataByOffset(BusDataType,
177 BusNumber,
178 SlotNumber,
179 Buffer,
180 Offset,
181 Length);
182
183 /* Return status */
184 return (*ActualLength != 0) ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
185 }
186
187 NTSTATUS
188 NTAPI
189 KdpSysReadControlSpace(IN ULONG Processor,
190 IN ULONG64 BaseAddress,
191 IN PVOID Buffer,
192 IN ULONG Length,
193 OUT PULONG ActualLength)
194 {
195 PVOID ControlStart;
196 ULONG RealLength;
197
198 /* Make sure that this is a valid request */
199 if ((BaseAddress < sizeof(KPROCESSOR_STATE)) &&
200 (Processor < KeNumberProcessors))
201 {
202 /* Get the actual length */
203 RealLength = sizeof(KPROCESSOR_STATE) - (ULONG_PTR)BaseAddress;
204 if (RealLength < Length) Length = RealLength;
205
206 /* Set the proper address */
207 ControlStart = (PVOID)((ULONG_PTR)BaseAddress +
208 (ULONG_PTR)&KiProcessorBlock[Processor]->
209 ProcessorState);
210
211 /* Copy the memory */
212 RtlCopyMemory(Buffer, ControlStart, Length);
213
214 /* Finish up */
215 *ActualLength = Length;
216 return STATUS_SUCCESS;
217 }
218 else
219 {
220 /* Invalid request */
221 *ActualLength = 0;
222 return STATUS_UNSUCCESSFUL;
223 }
224 }
225
226 NTSTATUS
227 NTAPI
228 KdpSysWriteControlSpace(IN ULONG Processor,
229 IN ULONG64 BaseAddress,
230 IN PVOID Buffer,
231 IN ULONG Length,
232 OUT PULONG ActualLength)
233 {
234 PVOID ControlStart;
235
236 /* Make sure that this is a valid request */
237 if (((BaseAddress + Length) <= sizeof(KPROCESSOR_STATE)) &&
238 (Processor < KeNumberProcessors))
239 {
240 /* Set the proper address */
241 ControlStart = (PVOID)((ULONG_PTR)BaseAddress +
242 (ULONG_PTR)&KiProcessorBlock[Processor]->
243 ProcessorState);
244
245 /* Copy the memory */
246 RtlCopyMemory(ControlStart, Buffer, Length);
247
248 /* Finish up */
249 *ActualLength = Length;
250 return STATUS_SUCCESS;
251 }
252 else
253 {
254 /* Invalid request */
255 *ActualLength = 0;
256 return STATUS_UNSUCCESSFUL;
257 }
258 }
259
260 NTSTATUS
261 NTAPI
262 KdpSysReadIoSpace(IN ULONG InterfaceType,
263 IN ULONG BusNumber,
264 IN ULONG AddressSpace,
265 IN ULONG64 IoAddress,
266 IN PVOID DataValue,
267 IN ULONG DataSize,
268 OUT PULONG ActualDataSize)
269 {
270 NTSTATUS Status;
271
272 /* Verify parameters */
273 if ((InterfaceType != Isa) ||
274 (BusNumber != 0) ||
275 (AddressSpace != 1))
276 {
277 /* Fail, we don't support this */
278 *ActualDataSize = 0;
279 return STATUS_UNSUCCESSFUL;
280 }
281
282 /* Check the size */
283 switch (DataSize)
284 {
285 case sizeof(UCHAR):
286
287 /* Read 1 byte */
288 *(PUCHAR)DataValue =
289 READ_PORT_UCHAR((PUCHAR)(ULONG_PTR)IoAddress);
290 *ActualDataSize = sizeof(UCHAR);
291 Status = STATUS_SUCCESS;
292 break;
293
294 case sizeof(USHORT):
295
296 /* Make sure the address is aligned */
297 if ((IoAddress & (sizeof(USHORT) - 1)) != 0)
298 {
299 /* It isn't, bail out */
300 *ActualDataSize = 0;
301 Status = STATUS_DATATYPE_MISALIGNMENT;
302 break;
303 }
304
305 /* Read 2 bytes */
306 *(PUSHORT)DataValue =
307 READ_PORT_USHORT((PUSHORT)(ULONG_PTR)IoAddress);
308 *ActualDataSize = sizeof(USHORT);
309 Status = STATUS_SUCCESS;
310 break;
311
312 case sizeof(ULONG):
313
314 /* Make sure the address is aligned */
315 if ((IoAddress & (sizeof(ULONG) - 1)) != 0)
316 {
317 /* It isn't, bail out */
318 *ActualDataSize = 0;
319 Status = STATUS_DATATYPE_MISALIGNMENT;
320 break;
321 }
322
323 /* Read 4 bytes */
324 *(PULONG)DataValue =
325 READ_PORT_ULONG((PULONG)(ULONG_PTR)IoAddress);
326 *ActualDataSize = sizeof(ULONG);
327 Status = STATUS_SUCCESS;
328 break;
329
330 default:
331
332 /* Invalid size, fail */
333 *ActualDataSize = 0;
334 Status = STATUS_INVALID_PARAMETER;
335 }
336
337 /* Return status */
338 return Status;
339 }
340
341 NTSTATUS
342 NTAPI
343 KdpSysWriteIoSpace(IN ULONG InterfaceType,
344 IN ULONG BusNumber,
345 IN ULONG AddressSpace,
346 IN ULONG64 IoAddress,
347 IN PVOID DataValue,
348 IN ULONG DataSize,
349 OUT PULONG ActualDataSize)
350 {
351 NTSTATUS Status;
352
353 /* Verify parameters */
354 if ((InterfaceType != Isa) ||
355 (BusNumber != 0) ||
356 (AddressSpace != 1))
357 {
358 /* Fail, we don't support this */
359 *ActualDataSize = 0;
360 return STATUS_UNSUCCESSFUL;
361 }
362
363 /* Check the size */
364 switch (DataSize)
365 {
366 case sizeof(UCHAR):
367
368 /* Write 1 byte */
369 WRITE_PORT_UCHAR((PUCHAR)(ULONG_PTR)IoAddress,
370 *(PUCHAR)DataValue);
371 *ActualDataSize = sizeof(UCHAR);
372 Status = STATUS_SUCCESS;
373 break;
374
375 case sizeof(USHORT):
376
377 /* Make sure the address is aligned */
378 if ((IoAddress & (sizeof(USHORT) - 1)) != 0)
379 {
380 /* It isn't, bail out */
381 *ActualDataSize = 0;
382 Status = STATUS_DATATYPE_MISALIGNMENT;
383 break;
384 }
385
386 /* Write 2 bytes */
387 WRITE_PORT_USHORT((PUSHORT)(ULONG_PTR)IoAddress,
388 *(PUSHORT)DataValue);
389 *ActualDataSize = sizeof(USHORT);
390 Status = STATUS_SUCCESS;
391 break;
392
393 case sizeof(ULONG):
394
395 /* Make sure the address is aligned */
396 if ((IoAddress & (sizeof(ULONG) - 1)) != 0)
397 {
398 /* It isn't, bail out */
399 *ActualDataSize = 0;
400 Status = STATUS_DATATYPE_MISALIGNMENT;
401 break;
402 }
403
404 /* Write 4 bytes */
405 WRITE_PORT_ULONG((PULONG)(ULONG_PTR)IoAddress,
406 *(PULONG)DataValue);
407 *ActualDataSize = sizeof(ULONG);
408 Status = STATUS_SUCCESS;
409 break;
410
411 default:
412
413 /* Invalid size, fail */
414 *ActualDataSize = 0;
415 Status = STATUS_INVALID_PARAMETER;
416 }
417
418 /* Return status */
419 return Status;
420 }
421
422 NTSTATUS
423 NTAPI
424 KdpSysCheckLowMemory(IN ULONG Flags)
425 {
426 /* Stubbed as we don't support PAE */
427 return STATUS_UNSUCCESSFUL;
428 }