Merge trunk head (r43756)
[reactos.git] / reactos / ntoskrnl / kd64 / amd64 / kdsup.c
index 58b981d..c8119d9 100644 (file)
@@ -71,7 +71,7 @@ KdpGetStateChange(IN PDBGKD_MANIPULATE_STATE64 State,
 
 VOID
 NTAPI
-KdpSetContextState(IN PDBGKD_WAIT_STATE_CHANGE64 WaitStateChange,
+KdpSetContextState(IN PDBGKD_ANY_WAIT_STATE_CHANGE WaitStateChange,
                    IN PCONTEXT Context)
 {
     PKPRCB Prcb = KeGetCurrentPrcb();
@@ -104,7 +104,17 @@ NTAPI
 KdpSysReadMsr(IN ULONG Msr,
               OUT PLARGE_INTEGER MsrValue)
 {
-    MsrValue->QuadPart = __readmsr(Msr);
+    /* Use SEH to protect from invalid MSRs */
+    _SEH2_TRY
+    {
+        MsrValue->QuadPart = __readmsr(Msr);
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        _SEH2_YIELD(return STATUS_NO_SUCH_DEVICE);
+    }
+    _SEH2_END
+
     return STATUS_SUCCESS;
 }
 
@@ -113,7 +123,17 @@ NTAPI
 KdpSysWriteMsr(IN ULONG Msr,
                IN PLARGE_INTEGER MsrValue)
 {
-    __writemsr(Msr, MsrValue->QuadPart);
+    /* Use SEH to protect from invalid MSRs */
+    _SEH2_TRY
+    {
+        __writemsr(Msr, MsrValue->QuadPart);
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        _SEH2_YIELD(return STATUS_NO_SUCH_DEVICE);
+    }
+    _SEH2_END
+
     return STATUS_SUCCESS;
 }
 
@@ -122,8 +142,8 @@ NTAPI
 KdpSysReadBusData(IN ULONG BusDataType,
                   IN ULONG BusNumber,
                   IN ULONG SlotNumber,
-                  IN PVOID Buffer,
                   IN ULONG Offset,
+                  IN PVOID Buffer,
                   IN ULONG Length,
                   OUT PULONG ActualLength)
 {
@@ -137,8 +157,8 @@ NTAPI
 KdpSysWriteBusData(IN ULONG BusDataType,
                    IN ULONG BusNumber,
                    IN ULONG SlotNumber,
-                   IN PVOID Buffer,
                    IN ULONG Offset,
+                   IN PVOID Buffer,
                    IN ULONG Length,
                    OUT PULONG ActualLength)
 {
@@ -156,54 +176,46 @@ KdpSysReadControlSpace(IN ULONG Processor,
                        OUT PULONG ActualLength)
 {
     PVOID ControlStart;
-    ULONG RealLength;
+    PKPRCB Prcb = KiProcessorBlock[Processor];
+    PKIPCR Pcr = CONTAINING_RECORD(Prcb, KIPCR, Prcb);
 
-    if ((ULONG)BaseAddress <= 2)
+    switch (BaseAddress)
     {
-        PKPRCB Prcb = KiProcessorBlock[Processor];
-        PKIPCR Pcr = CONTAINING_RECORD(Prcb, KIPCR, Prcb);
+        case AMD64_DEBUG_CONTROL_SPACE_KPCR:
+            /* Copy a pointer to the Pcr */
+            ControlStart = &Pcr;
+            *ActualLength = sizeof(PVOID);
+            break;
 
-        switch ((ULONG_PTR)BaseAddress)
-        {
-            case 0:
-                /* Copy a pointer to the Pcr */
-                ControlStart = &Pcr;
-                RealLength = sizeof(PVOID);
-                break;
-
-            case 1:
-                /* Copy a pointer to the Prcb */
-                ControlStart = &Prcb;
-                RealLength = sizeof(PVOID);
-                break;
-
-            case 2:
-                /* Copy SpecialRegisters */
-                ControlStart = &Prcb->ProcessorState.SpecialRegisters;
-                RealLength = sizeof(KSPECIAL_REGISTERS);
-                break;
-
-            default:
-                RealLength = 0;
-                ControlStart = NULL;
-                ASSERT(FALSE);
-        }
+        case AMD64_DEBUG_CONTROL_SPACE_KPRCB:
+            /* Copy a pointer to the Prcb */
+            ControlStart = &Prcb;
+            *ActualLength = sizeof(PVOID);
+            break;
 
-        if (RealLength < Length) Length = RealLength;
+        case AMD64_DEBUG_CONTROL_SPACE_KSPECIAL:
+            /* Copy SpecialRegisters */
+            ControlStart = &Prcb->ProcessorState.SpecialRegisters;
+            *ActualLength = sizeof(KSPECIAL_REGISTERS);
+            break;
 
-        /* Copy the memory */
-        RtlCopyMemory(Buffer, ControlStart, Length);
-        *ActualLength = Length;
+        case AMD64_DEBUG_CONTROL_SPACE_KTHREAD:
+            /* Copy a pointer to the current Thread */
+            ControlStart = &Prcb->CurrentThread;
+            *ActualLength = sizeof(PVOID);
+            break;
 
-        /* Finish up */
-        return STATUS_SUCCESS;
-    }
-    else
-    {
-        /* Invalid request */
-        *ActualLength = 0;
-        return STATUS_UNSUCCESSFUL;
+        default:
+            *ActualLength = 0;
+            ASSERT(FALSE);
+            return STATUS_UNSUCCESSFUL;
     }
+
+    /* Copy the memory */
+    RtlCopyMemory(Buffer, ControlStart, min(Length, *ActualLength));
+
+    /* Finish up */
+    return STATUS_SUCCESS;
 }
 
 NTSTATUS
@@ -225,28 +237,111 @@ KdpSysReadIoSpace(IN ULONG InterfaceType,
                   IN ULONG BusNumber,
                   IN ULONG AddressSpace,
                   IN ULONG64 IoAddress,
-                  IN PULONG DataValue,
+                  OUT PVOID DataValue,
                   IN ULONG DataSize,
                   OUT PULONG ActualDataSize)
 {
-    UNIMPLEMENTED;
-    while (TRUE);
-    return STATUS_UNSUCCESSFUL;
+    /* Verify parameters */
+    if (InterfaceType != Isa || BusNumber != 0 || AddressSpace != 1)
+    {
+        /* No data was read */
+        *ActualDataSize = 0;
+        return STATUS_INVALID_PARAMETER;
+    }
+
+    /* Check for correct alignment */
+    if ((IoAddress & (DataSize - 1)))
+    {
+        /* Invalid alignment */
+        *ActualDataSize = 0;
+        return STATUS_DATATYPE_MISALIGNMENT;
+    }
+
+    switch (DataSize)
+    {
+        case sizeof(UCHAR):
+            /* Read one UCHAR */
+            *(PUCHAR)DataValue = READ_PORT_UCHAR((PUCHAR)IoAddress);
+            break;
+
+        case sizeof(USHORT):
+            /* Read one USHORT */
+            *(PUSHORT)DataValue = READ_PORT_USHORT((PUSHORT)IoAddress);
+            break;
+
+        case sizeof(ULONG):
+            /* Read one ULONG */
+            *(PULONG)DataValue = READ_PORT_ULONG((PULONG)IoAddress);
+            break;
+
+        default:
+            /* Invalid data size */
+             *ActualDataSize = 0;
+            return STATUS_INVALID_PARAMETER;
+    }
+
+    /* Return the size of the data */
+    *ActualDataSize = DataSize;
+
+    /* Success! */
+    return STATUS_SUCCESS;
 }
 
+
 NTSTATUS
 NTAPI
 KdpSysWriteIoSpace(IN ULONG InterfaceType,
                    IN ULONG BusNumber,
                    IN ULONG AddressSpace,
                    IN ULONG64 IoAddress,
-                   IN PULONG DataValue,
+                   IN PVOID DataValue,
                    IN ULONG DataSize,
                    OUT PULONG ActualDataSize)
 {
-    UNIMPLEMENTED;
-    while (TRUE);
-    return STATUS_UNSUCCESSFUL;
+    /* Verify parameters */
+    if (InterfaceType != Isa || BusNumber != 0 || AddressSpace != 1)
+    {
+        /* No data was written */
+        *ActualDataSize = 0;
+        return STATUS_INVALID_PARAMETER;
+    }
+
+    /* Check for correct alignment */
+    if ((IoAddress & (DataSize - 1)))
+    {
+        /* Invalid alignment */
+        *ActualDataSize = 0;
+        return STATUS_DATATYPE_MISALIGNMENT;
+    }
+
+    switch (DataSize)
+    {
+        case sizeof(UCHAR):
+            /* Write one UCHAR */
+            WRITE_PORT_UCHAR((PUCHAR)IoAddress, *(PUCHAR)DataValue);
+            break;
+
+        case sizeof(USHORT):
+            /* Write one USHORT */
+            WRITE_PORT_USHORT((PUSHORT)IoAddress, *(PUSHORT)DataValue);
+            break;
+
+        case sizeof(ULONG):
+            /* Write one ULONG */
+            WRITE_PORT_ULONG((PULONG)IoAddress, *(PULONG)DataValue);
+            break;
+
+        default:
+            /* Invalid data size */
+             *ActualDataSize = 0;
+            return STATUS_INVALID_PARAMETER;
+    }
+
+    /* Return the size of the data */
+    *ActualDataSize = DataSize;
+
+    /* Success! */
+    return STATUS_SUCCESS;
 }
 
 NTSTATUS