[edk2] [PATCH v1 10/14] OvmfPkg/VirtioNetDxe: Allocate Tx and Rx ring using AllocateSharedPage()

Brijesh Singh posted 14 patches 7 years, 4 months ago
[edk2] [PATCH v1 10/14] OvmfPkg/VirtioNetDxe: Allocate Tx and Rx ring using AllocateSharedPage()
Posted by Brijesh Singh 7 years, 4 months ago
The Tx and Rx rings are accessed by both guest and hypervisor, allocate
the rings using newly added VirtIo->AllocateSharedPages() and map it with
BusMasterCommonBuffer so that it can be accessed by both guest and
hypervisor.

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
---
 OvmfPkg/VirtioNetDxe/VirtioNet.h     |  2 ++
 OvmfPkg/VirtioNetDxe/Events.c        | 14 ++++++++++++++
 OvmfPkg/VirtioNetDxe/SnpInitialize.c | 19 +++++++++++++++++++
 OvmfPkg/VirtioNetDxe/SnpShutdown.c   | 11 +++++++++++
 4 files changed, 46 insertions(+)

diff --git a/OvmfPkg/VirtioNetDxe/VirtioNet.h b/OvmfPkg/VirtioNetDxe/VirtioNet.h
index 710859bc6115..2964c946e26e 100644
--- a/OvmfPkg/VirtioNetDxe/VirtioNet.h
+++ b/OvmfPkg/VirtioNetDxe/VirtioNet.h
@@ -82,10 +82,12 @@ typedef struct {
   EFI_HANDLE                  MacHandle;         // VirtioNetDriverBindingStart
 
   VRING                       RxRing;            // VirtioNetInitRing
+  VOID                        *RxRingMap;        // VirtioNetInitRing
   UINT8                       *RxBuf;            // VirtioNetInitRx
   UINT16                      RxLastUsed;        // VirtioNetInitRx
 
   VRING                       TxRing;            // VirtioNetInitRing
+  VOID                        *TxRingMap;        // VirtioNetInitRing
   UINT16                      TxMaxPending;      // VirtioNetInitTx
   UINT16                      TxCurPending;      // VirtioNetInitTx
   UINT16                      *TxFreeStack;      // VirtioNetInitTx
diff --git a/OvmfPkg/VirtioNetDxe/Events.c b/OvmfPkg/VirtioNetDxe/Events.c
index 5be1af6ffbee..9eb129ca2006 100644
--- a/OvmfPkg/VirtioNetDxe/Events.c
+++ b/OvmfPkg/VirtioNetDxe/Events.c
@@ -88,4 +88,18 @@ VirtioNetExitBoot (
   if (Dev->Snm.State == EfiSimpleNetworkInitialized) {
     Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
   }
+
+  //
+  // If Rx and Tx Ring exist then unmap it so that hypervisor is not able to
+  // get readable data after device is reset.
+  //
+  if (Dev->TxRingMap != NULL) {
+    VirtioRingUnmap (Dev->VirtIo, &Dev->TxRing, Dev->TxRingMap);
+    Dev->TxRingMap = NULL;
+  }
+
+  if (Dev->RxRingMap != NULL) {
+    VirtioRingUnmap (Dev->VirtIo, &Dev->RxRing, Dev->RxRingMap);
+    Dev->RxRingMap = NULL;
+  }
 }
diff --git a/OvmfPkg/VirtioNetDxe/SnpInitialize.c b/OvmfPkg/VirtioNetDxe/SnpInitialize.c
index 6d9b81a9f939..cbc9c51cb643 100644
--- a/OvmfPkg/VirtioNetDxe/SnpInitialize.c
+++ b/OvmfPkg/VirtioNetDxe/SnpInitialize.c
@@ -461,11 +461,21 @@ VirtioNetInitialize (
     goto DeviceFailed;
   }
 
+  Status = VirtioRingMap (Dev->VirtIo, &Dev->RxRing, &Dev->RxRingMap);
+  if (EFI_ERROR (Status)) {
+    goto ReleaseRxRing;
+  }
+
   Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_TX, &Dev->TxRing);
   if (EFI_ERROR (Status)) {
     goto ReleaseRxRing;
   }
 
+  Status = VirtioRingMap (Dev->VirtIo, &Dev->TxRing, &Dev->TxRingMap);
+  if (EFI_ERROR (Status)) {
+    goto ReleaseTxRing;
+  }
+
   //
   // step 5 -- keep only the features we want
   //
@@ -510,9 +520,18 @@ AbortDevice:
   Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
 
 ReleaseTxRing:
+  if (Dev->TxRingMap != NULL) {
+    VirtioRingUnmap (Dev->VirtIo, &Dev->TxRing, Dev->TxRingMap);
+    Dev->TxRingMap = NULL;
+  }
+
   VirtioRingUninit (Dev->VirtIo, &Dev->TxRing);
 
 ReleaseRxRing:
+  if (Dev->RxRingMap != NULL) {
+    VirtioRingUnmap (Dev->VirtIo, &Dev->TxRing, Dev->RxRingMap);
+    Dev->TxRingMap = NULL;
+  }
   VirtioRingUninit (Dev->VirtIo, &Dev->RxRing);
 
 DeviceFailed:
diff --git a/OvmfPkg/VirtioNetDxe/SnpShutdown.c b/OvmfPkg/VirtioNetDxe/SnpShutdown.c
index 5e84191fbbdd..08524ab94006 100644
--- a/OvmfPkg/VirtioNetDxe/SnpShutdown.c
+++ b/OvmfPkg/VirtioNetDxe/SnpShutdown.c
@@ -65,6 +65,17 @@ VirtioNetShutdown (
   }
 
   Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
+
+  if (Dev->RxRingMap != NULL) {
+    VirtioRingUnmap (Dev->VirtIo, &Dev->RxRing, Dev->RxRingMap);
+    Dev->RxRingMap = NULL;
+  }
+
+  if (Dev->TxRingMap != NULL) {
+    VirtioRingUnmap (Dev->VirtIo, &Dev->TxRing, Dev->TxRingMap);
+    Dev->TxRingMap = NULL;
+  }
+
   VirtioNetShutdownRx (Dev);
   VirtioNetShutdownTx (Dev);
   VirtioRingUninit (Dev->VirtIo, &Dev->TxRing);
-- 
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel