audk/OvmfPkg/VirtioNetDxe
Laszlo Ersek 71914406e8 OvmfPkg: Virtio drivers: fix incorrect casts in init functions
The recent patch

  OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL

was fixed up at commit time, in order to silence warnings issued by the
Visual Studio compiler. Differences between the posted and committed
patch:

>  diff --git a/OvmfPkg/VirtioBlkDxe/VirtioBlk.c b/OvmfPkg/VirtioBlkDxe/VirtioBlk.c
> -index 17b9f71..96a0d9f 100644
> +index 17b9f71..f09b0d1 100644
>  --- a/OvmfPkg/VirtioBlkDxe/VirtioBlk.c
>  +++ b/OvmfPkg/VirtioBlkDxe/VirtioBlk.c
>  @@ -23,7 +23,6 @@
> @@ -994,7 +998,7 @@
>  +  // step 4c -- Report GPFN (guest-physical frame number) of queue.
>  +  //
>  +  Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo,
> -+      (UINTN) Dev->Ring.Base >> EFI_PAGE_SHIFT);
> ++      (UINT32)(UINTN) Dev->Ring.Base >> EFI_PAGE_SHIFT);
>  +  if (EFI_ERROR (Status)) {
>  +    goto ReleaseQueue;
>  +  }
> @@ -1495,7 +1499,7 @@
>         goto Exit;
>       }
>  diff --git a/OvmfPkg/VirtioNetDxe/SnpInitialize.c b/OvmfPkg/VirtioNetDxe/SnpInitialize.c
> -index 6cee014..8dcf9da 100644
> +index 6cee014..4203fbd 100644
>  --- a/OvmfPkg/VirtioNetDxe/SnpInitialize.c
>  +++ b/OvmfPkg/VirtioNetDxe/SnpInitialize.c
>  @@ -57,14 +57,15 @@ VirtioNetInitRing (
> @@ -1539,7 +1543,7 @@
>  -  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueAddress,
>  -             (UINTN) Ring->Base >> EFI_PAGE_SHIFT);
>  +  Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo,
> -+      (UINTN) Ring->Base >> EFI_PAGE_SHIFT);
> ++      (UINT32)(UINTN) Ring->Base >> EFI_PAGE_SHIFT);
>     if (EFI_ERROR (Status)) {
>  -    VirtioRingUninit (Ring);
>  +    goto ReleaseQueue;
> @@ -1721,7 +1725,7 @@
>   Exit:
>     gBS->RestoreTPL (OldTpl);
>  diff --git a/OvmfPkg/VirtioScsiDxe/VirtioScsi.c b/OvmfPkg/VirtioScsiDxe/VirtioScsi.c
> -index b836fb3..bcec676 100644
> +index b836fb3..2223c9c 100644
>  --- a/OvmfPkg/VirtioScsiDxe/VirtioScsi.c
>  +++ b/OvmfPkg/VirtioScsiDxe/VirtioScsi.c
>  @@ -38,7 +38,6 @@
> @@ -1908,7 +1912,7 @@
>  +  // step 4c -- Report GPFN (guest-physical frame number) of queue.
>  +  //
>  +  Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo,
> -+      (UINTN) Dev->Ring.Base >> EFI_PAGE_SHIFT);
> ++      (UINT32)(UINTN) Dev->Ring.Base >> EFI_PAGE_SHIFT);
>     if (EFI_ERROR (Status)) {
>       goto ReleaseQueue;
>     }

These casts are incorrect -- they throw away address bits >=32 before
shifting, which can break the drivers in guests with more than 4GB RAM.

The bug is clearly an artifact of the edk2 coding style, which requires
cast expressions to be written as

  (type) expression

rather than the usual

  (type)expression

The latter correctly reflects that casts have one of the strongest
bindings in C. The former actively obscures that fact. Cf.

  (type) expr1 >> expr2

vs.

  (type)expr1 >> expr2

Make sure we shift before we truncate.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14970 6f19259b-4bc3-4df7-8a09-765794883524
2013-12-12 17:28:05 +00:00
..
ComponentName.c OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00
DriverBinding.c OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00
EntryPoint.c OvmfPkg: VirtioNetDxe: add entry point 2013-06-14 07:39:40 +00:00
Events.c OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00
SnpGetStatus.c OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00
SnpInitialize.c OvmfPkg: Virtio drivers: fix incorrect casts in init functions 2013-12-12 17:28:05 +00:00
SnpMcastIpToMac.c OvmfPkg: VirtioNetDxe: map multicast IP to MAC: SNP.McastIpToMac 2013-06-14 07:40:30 +00:00
SnpReceive.c OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00
SnpReceiveFilters.c OvmfPkg: VirtioNetDxe: emulate Rx filter configuration: SNP.ReceiveFilters 2013-06-14 07:40:36 +00:00
SnpSharedHelpers.c OvmfPkg: VirtioNetDxe: add SNP.Initialize and shared dependencies 2013-06-14 07:40:05 +00:00
SnpShutdown.c OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00
SnpStart.c OvmfPkg: VirtioNetDxe: Simple Network Protocol members Start and Stop 2013-06-14 07:39:58 +00:00
SnpStop.c OvmfPkg: VirtioNetDxe: Simple Network Protocol members Start and Stop 2013-06-14 07:39:58 +00:00
SnpTransmit.c OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00
SnpUnsupported.c OvmfPkg: VirtioNetDxe: definitions of unsupported SNP member functions 2013-06-14 07:40:42 +00:00
TechNotes.txt OvmfPkg: VirtioNetDxe: add technical notes 2013-06-14 07:39:25 +00:00
VirtioNet.h OvmfPkg/Virtio: Removed VirtioReadDevice() / VirtIoWriteDevice() functions 2013-12-11 16:58:39 +00:00
VirtioNet.inf OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL 2013-12-11 16:58:22 +00:00