Skip to contentSkip to conversation

usb:xhci: no DMA on HC reset

PATCH In review Roman Kagan 3 participants 4 messages May 4, 2016 – May 11, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb:xhci: no DMA on HC reset

This patch is a rough fix to a memory corruption we are observing when running VMs with xhci USB controller and OVMF firmware.

Specifically, on the following call chain

xhci_reset
xhci_disable_slot
xhci_disable_ep
xhci_set_ep_state

QEMU overwrites guest memory using stale guest addresses.

This doesn't happen when the guest (firmware) driver sets up xhci for the first time as there are no slots configured yet. However when the firmware hands over the control to the OS some slots and endpoints are already set up with their context in the guest RAM. Now the OS' driver resets the controller again and xhci_set_ep_state then reads and writes that memory which is now owned by the OS.

As a quick fix, skip calling xhci_set_ep_state in xhci_disable_ep if the device context base address array pointer is zero (indicating we're in the HC reset and no DMA is possible).

---
 hw/usb/hcd-xhci.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/usb/hcd-xhci.c +4 -1
index bcde8a2..43ba615 100644
@@ -1531,7 +1531,10 @@ static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
         usb_packet_cleanup(&epctx->transfers[i].packet);
     }
 
-    xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
+    /* only touch guest RAM if we're not resetting the HC */
+    if (xhci->dcbaap_low || xhci->dcbaap_high) {
+        xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
+    }
 
     timer_free(epctx->kick_timer);
     g_free(epctx);
Signature · 1 line
2.5.5
#1 lore raw

Re: [Qemu-devel] (for 2.6?) [PATCH] usb:xhci: no DMA on HC reset

Quoted 35 lines · Roman Kagan

This patch is a rough fix to a memory corruption we are observing when running VMs with xhci USB controller and OVMF firmware.

Specifically, on the following call chain

xhci_reset
xhci_disable_slot
xhci_disable_ep
xhci_set_ep_state

QEMU overwrites guest memory using stale guest addresses.

This doesn't happen when the guest (firmware) driver sets up xhci for the first time as there are no slots configured yet. However when the firmware hands over the control to the OS some slots and endpoints are already set up with their context in the guest RAM. Now the OS' driver resets the controller again and xhci_set_ep_state then reads and writes that memory which is now owned by the OS.

As a quick fix, skip calling xhci_set_ep_state in xhci_disable_ep if the device context base address array pointer is zero (indicating we're in the HC reset and no DMA is possible).

---
  hw/usb/hcd-xhci.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/usb/hcd-xhci.c +0 -0
index bcde8a2..43ba615 100644
@@ -1531,7 +1531,10 @@ static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
          usb_packet_cleanup(&epctx->transfers[i].packet);

hw/usb/hcd-xhci.c static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid, · line 1540

Quoted 10 lines
hw/usb/hcd-xhci.c +4 -1
      }
 
-    xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
+    /* only touch guest RAM if we're not resetting the HC */
+    if (xhci->dcbaap_low || xhci->dcbaap_high) {
+        xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
+    }
 
      timer_free(epctx->kick_timer);
      g_free(epctx);

the situation is quite reliable with win2k12 and EFI guest. Kernel image gets corrupted and the guest crashes on boot.

The problem is serious and really severe.

Den

#2 lore raw
In reply to Roman Kagan
Quoted 35 lines · Roman Kagan

This patch is a rough fix to a memory corruption we are observing when running VMs with xhci USB controller and OVMF firmware.

Specifically, on the following call chain

xhci_reset
xhci_disable_slot
xhci_disable_ep
xhci_set_ep_state

QEMU overwrites guest memory using stale guest addresses.

This doesn't happen when the guest (firmware) driver sets up xhci for the first time as there are no slots configured yet. However when the firmware hands over the control to the OS some slots and endpoints are already set up with their context in the guest RAM. Now the OS' driver resets the controller again and xhci_set_ep_state then reads and writes that memory which is now owned by the OS.

As a quick fix, skip calling xhci_set_ep_state in xhci_disable_ep if the device context base address array pointer is zero (indicating we're in the HC reset and no DMA is possible).

---
  hw/usb/hcd-xhci.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/usb/hcd-xhci.c +0 -0
index bcde8a2..43ba615 100644
@@ -1531,7 +1531,10 @@ static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
          usb_packet_cleanup(&epctx->transfers[i].packet);

hw/usb/hcd-xhci.c static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid, · line 1540

Quoted 10 lines
hw/usb/hcd-xhci.c +4 -1
      }
 
-    xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
+    /* only touch guest RAM if we're not resetting the HC */
+    if (xhci->dcbaap_low || xhci->dcbaap_high) {
+        xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
+    }
 
      timer_free(epctx->kick_timer);
      g_free(epctx);

ping

#3 lore raw
In reply to Roman Kagan
Quoted 2 lines · Roman Kagan

This patch is a rough fix to a memory corruption we are observing when running VMs with xhci USB controller and OVMF firmware.

Added to usb patch queue.

thanks,
Gerd