From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-co1nam03on0085.outbound.protection.outlook.com [104.47.40.85]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 457F521E49BA5 for ; Wed, 23 Aug 2017 05:20:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=gKTaD4odvk7hMu8Yj+XReAgk88UGJSVVp9N09Vh5Zzc=; b=mfCrhWk88RiOuro01UP6I9eOYIFvHQFdJgUjjuLwpie/0GlzaOx2PHUI5bFJLy5Z9NQejVX/c+kTltbiQ8+auKqEbHWN85JLf1oDygqIjQ3S+e4eU/reBrZZba9K5vuiYbZdp+pgFX6YXezpfx3SJhb2WKheTdH226KsjPeRVV8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from brijesh-build-machine.amd.com (165.204.77.1) by DM2PR12MB0155.namprd12.prod.outlook.com (2a01:111:e400:50ce::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.1362.18; Wed, 23 Aug 2017 12:23:03 +0000 From: Brijesh Singh To: edk2-devel@lists.01.org Cc: Brijesh Singh , Ard Biesheuvel , Jordan Justen , Tom Lendacky , Laszlo Ersek Date: Wed, 23 Aug 2017 08:22:37 -0400 Message-Id: <1503490967-5559-14-git-send-email-brijesh.singh@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1503490967-5559-1-git-send-email-brijesh.singh@amd.com> References: <1503490967-5559-1-git-send-email-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: DM5PR12CA0069.namprd12.prod.outlook.com (2603:10b6:3:103::31) To DM2PR12MB0155.namprd12.prod.outlook.com (2a01:111:e400:50ce::18) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ccf496de-0b08-45d6-1129-08d4ea21b3f1 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:DM2PR12MB0155; X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0155; 3:981w4ilpq4pbd9DxVO2EFzIV8CCIDgTXIqLTh0lep1BFgXhs27LEslAnL/bxMwEVaGEm7kZIcwBHCZZeJjXq2GlAEZ2cRIt+ybx2cz+kAWJL+6vIwxHUoiGYQZSywOVxnb3Ot2JKNww8oJd8eZ2nlGkwCUIA/AQNOgGQ+5kHAqOY5EyVc0lpf0BOfoyd6y2E0fSnqtGYB2PfAHpPWuGFwl9YjGfO66/0YKwTwp3VxmbtAJ4EQkT8ZuHVulsS3E6X; 25:uS9U2gFqGup8dF4FMdq5DBd6WFeXXNArCfWnmeS304uRIF2P7T+CgL9RdDBNMAh8lbfGxyHqqOfpKxHEyPX3gc7qUi4dvi14mbBYbt/106G3Jz/QWRlTRUtIdvr1D/P5j5hn48g27kfSKLXVEN+rjcRiqgzwLyqQpdDUBJq3sschMXZKIxShdeHB5R0JB2Nl3bW0SjK4I0e4XGgcr+zD16SEzvCWV9qNvMNJC5vSpZfF5ttxrgLwfCQvhqj6WZRNysDZeJP+q0YTzYZ/yOQTuikH1+NK1rmjvpRbNanyckIBpsBdYvpgQzzu4xw8FqdM2gW0KuC3wti+rAxSNFRltw==; 31:jF8OprMQvCf7ETSjWkisCicKVS8m40lX8bhB2oBZBNuw791t4WcrJRMT1UVklKCB4kbQ6qO+o9BoibfTBsd7W7kRUsPvr5InDMuvHW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ange-Antispam-Report-Test: UriScan:(767451399110)(228905959029699)(17755550239193); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(10201501046)(93006095)(93001095)(100000703101)(100105400095)(3002001)(6055026)(6041248)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123560025)(20161123564025)(20161123558100)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:DM2PR12MB0155; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:DM2PR12MB0155; X-Forefront-PRVS: 040866B734 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(7370300001)(4630300001)(6009001)(39860400002)(189002)(199003)(51234002)(575784001)(305945005)(47776003)(97736004)(101416001)(110136004)(6666003)(189998001)(478600001)(7736002)(3846002)(66066001)(5003940100001)(6116002)(6486002)(48376002)(86362001)(105586002)(2950100002)(4326008)(106356001)(36756003)(6916009)(50466002)(2906002)(50226002)(42186005)(2351001)(33646002)(53416004)(25786009)(7350300001)(81156014)(76176999)(5660300001)(68736007)(54906002)(8676002)(53936002)(50986999)(81166006)(2361001); DIR:OUT; SFP:1101; SCL:1; SRVR:DM2PR12MB0155; H:brijesh-build-machine.amd.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DM2PR12MB0155; 23:IBJkKbUgcNdUCVnbyqfURpypG77X97bx4usUkK8fE?= =?us-ascii?Q?4EPqVBhKPoQe0/BAWD1pcDb8Fx8L8P77K3ndHer0gRRluEUWoQA+PWDfXxQr?= =?us-ascii?Q?9GEeoRMr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Sc8B4E4LE/MfO2SWbaQ56zSgTLPHL7QIVnB8FHpQVmfuLg0hpds2kqC3e57svgta1V94Nz65yBCuW2oaIPNYmy4= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0155; 20:7QoeBBrHUjqtkZKBeMijqwbxeKpVlpUA+JtVoDlf4q3sOTConGZPoiVKfYkjo1jCZU8oH4XBeMeC3DzZu+ydVON/rk+MlYgew/oFOpau/LBSqnaDFSnff6jr6+ForLieTwA361OGwnhQFUDxxgluIy63UsFW1XtgE9ACKfPjWmEmSoIt8Nw+eGWgh6E9MLgSqAfDr6NR9DHzjrDJqofbAHo8MlSpJZEFR7jB9VtqkdO38+RhwWvOQAdXwMHpuKrZ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Aug 2017 12:23:03.8103 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR12MB0155 Subject: [PATCH v3 13/23] OvmfPkg/VirtioBlkDxe: map host address to device address X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Aug 2017 12:20:31 -0000 Content-Type: text/plain The SynchronousRequest() function, programs the vring descriptor with the buffers pointed-by virtio-blk requests, status and memory that is referenced inside the request header. The patch uses VIRTIO_DEVICE_PROTOCOL.MapSharedBuffer() function to map host address to device address and programs the vring descriptor with device addresses. Cc: Ard Biesheuvel Cc: Jordan Justen Cc: Tom Lendacky Cc: Laszlo Ersek Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Brijesh Singh --- OvmfPkg/VirtioBlkDxe/VirtioBlk.h | 1 + OvmfPkg/VirtioBlkDxe/VirtioBlk.c | 201 +++++++++++++++++--- 2 files changed, 180 insertions(+), 22 deletions(-) diff --git a/OvmfPkg/VirtioBlkDxe/VirtioBlk.h b/OvmfPkg/VirtioBlkDxe/VirtioBlk.h index 6c402ca88ea4..9ec0b956b818 100644 --- a/OvmfPkg/VirtioBlkDxe/VirtioBlk.h +++ b/OvmfPkg/VirtioBlkDxe/VirtioBlk.h @@ -41,6 +41,7 @@ typedef struct { VRING Ring; // VirtioRingInit 2 EFI_BLOCK_IO_PROTOCOL BlockIo; // VirtioBlkInit 1 EFI_BLOCK_IO_MEDIA BlockIoMedia; // VirtioBlkInit 1 + VOID *RingMap; // VirtioRingMap 2 } VBLK_DEV; #define VIRTIO_BLK_FROM_BLOCK_IO(BlockIoPointer) \ diff --git a/OvmfPkg/VirtioBlkDxe/VirtioBlk.c b/OvmfPkg/VirtioBlkDxe/VirtioBlk.c index bff15fe3add1..e23762743f75 100644 --- a/OvmfPkg/VirtioBlkDxe/VirtioBlk.c +++ b/OvmfPkg/VirtioBlkDxe/VirtioBlk.c @@ -232,7 +232,8 @@ VerifyReadWriteRequest ( @retval EFI_DEVICE_ERROR Failed to notify host side via VirtIo write, or unable to parse host response, or host response - is not VIRTIO_BLK_S_OK. + is not VIRTIO_BLK_S_OK or failed to map Buffer + for a bus master operation. **/ @@ -249,8 +250,16 @@ SynchronousRequest ( { UINT32 BlockSize; volatile VIRTIO_BLK_REQ Request; - volatile UINT8 HostStatus; + volatile UINT8 *HostStatus; + VOID *HostStatusBuffer; DESC_INDICES Indices; + VOID *RequestMapping; + VOID *StatusMapping; + VOID *BufferMapping; + EFI_PHYSICAL_ADDRESS BufferDeviceAddress; + EFI_PHYSICAL_ADDRESS HostStatusDeviceAddress; + EFI_PHYSICAL_ADDRESS RequestDeviceAddress; + EFI_STATUS Status, Ret; BlockSize = Dev->BlockIoMedia.BlockSize; @@ -278,9 +287,88 @@ SynchronousRequest ( VirtioPrepare (&Dev->Ring, &Indices); // + // Host status is bi-directional (we preset with a value and expect the device + // to update it). Allocate a host status buffer which can be mapped to + // access equally by both processor and the device. + // + Status = Dev->VirtIo->AllocateSharedPages ( + Dev->VirtIo, + EFI_SIZE_TO_PAGES (sizeof *HostStatus), + &HostStatusBuffer + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + // + // Map virtio-blk request header + // + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterRead, + (VOID *) &Request, + sizeof Request, + &RequestDeviceAddress, + &RequestMapping + ); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto FreeHostStatusBuffer; + } + + // + // Map data buffer + // + if (BufferSize > 0) { + if (RequestIsWrite) { + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterRead, + (VOID *) Buffer, + BufferSize, + &BufferDeviceAddress, + &BufferMapping + ); + } else { + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterWrite, + (VOID *) Buffer, + BufferSize, + &BufferDeviceAddress, + &BufferMapping + ); + } + + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto UnmapRequestBuffer; + } + } + + // + // Map the Status Buffer with VirtioOperationBusMasterCommonBuffer so that + // both processor and device can access it. + // + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterCommonBuffer, + HostStatusBuffer, + sizeof *HostStatus, + &HostStatusDeviceAddress, + &StatusMapping + ); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto UnmapDataBuffer; + } + + HostStatus = HostStatusBuffer; + + // // preset a host status for ourselves that we do not accept as success // - HostStatus = VIRTIO_BLK_S_IOERR; + *HostStatus = VIRTIO_BLK_S_IOERR; // // ensured by VirtioBlkInit() -- this predicate, in combination with the @@ -291,8 +379,13 @@ SynchronousRequest ( // // virtio-blk header in first desc // - VirtioAppendDesc (&Dev->Ring, (UINTN) &Request, sizeof Request, - VRING_DESC_F_NEXT, &Indices); + VirtioAppendDesc ( + &Dev->Ring, + RequestDeviceAddress, + sizeof Request, + VRING_DESC_F_NEXT, + &Indices + ); // // data buffer for read/write in second desc @@ -311,27 +404,62 @@ SynchronousRequest ( // // VRING_DESC_F_WRITE is interpreted from the host's point of view. // - VirtioAppendDesc (&Dev->Ring, (UINTN) Buffer, (UINT32) BufferSize, + VirtioAppendDesc ( + &Dev->Ring, + BufferDeviceAddress, + (UINT32) BufferSize, VRING_DESC_F_NEXT | (RequestIsWrite ? 0 : VRING_DESC_F_WRITE), - &Indices); + &Indices + ); } // // host status in last (second or third) desc // - VirtioAppendDesc (&Dev->Ring, (UINTN) &HostStatus, sizeof HostStatus, - VRING_DESC_F_WRITE, &Indices); + VirtioAppendDesc ( + &Dev->Ring, + HostStatusDeviceAddress, + sizeof *HostStatus, + VRING_DESC_F_WRITE, + &Indices + ); // // virtio-blk's only virtqueue is #0, called "requestq" (see Appendix D). // - if (VirtioFlush (Dev->VirtIo, 0, &Dev->Ring, &Indices, - NULL) == EFI_SUCCESS && - HostStatus == VIRTIO_BLK_S_OK) { - return EFI_SUCCESS; + Status = VirtioFlush (Dev->VirtIo, 0, &Dev->Ring, &Indices, NULL); + + // + // Unmap the HostStatus buffer before accessing it + // + Ret = Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, StatusMapping); + if (EFI_ERROR (Ret)) { + Status = EFI_DEVICE_ERROR; + } + + if (!EFI_ERROR (Status) && + *HostStatus == VIRTIO_BLK_S_OK) { + Status = EFI_SUCCESS; + } else { + Status = EFI_DEVICE_ERROR; } - return EFI_DEVICE_ERROR; +UnmapDataBuffer: + if (BufferSize > 0) { + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, BufferMapping); + } + +UnmapRequestBuffer: + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, RequestMapping); + +FreeHostStatusBuffer: + Dev->VirtIo->FreeSharedPages ( + Dev->VirtIo, + EFI_SIZE_TO_PAGES (sizeof *HostStatus), + HostStatusBuffer + ); + + return Status; } @@ -580,7 +708,8 @@ VirtioBlkDriverBindingSupported ( virtio-blk attributes the host provides. @return Error codes from VirtioRingInit() or - VIRTIO_CFG_READ() / VIRTIO_CFG_WRITE(). + VIRTIO_CFG_READ() / VIRTIO_CFG_WRITE or + VirtioRingMap(). **/ @@ -601,6 +730,7 @@ VirtioBlkInit ( UINT8 AlignmentOffset; UINT32 OptIoSize; UINT16 QueueSize; + UINT64 RingBaseShift; PhysicalBlockExp = 0; AlignmentOffset = 0; @@ -729,25 +859,42 @@ VirtioBlkInit ( } // + // If anything fails from here on, we must release the ring resources + // + Status = VirtioRingMap ( + Dev->VirtIo, + &Dev->Ring, + &RingBaseShift, + &Dev->RingMap + ); + if (EFI_ERROR (Status)) { + goto ReleaseQueue; + } + + // // Additional steps for MMIO: align the queue appropriately, and set the - // size. If anything fails from here on, we must release the ring resources. + // size. If anything fails from here on, we must unmap the ring resources. // Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } // // step 4c -- Report GPFN (guest-physical frame number) of queue. // - Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo, &Dev->Ring, 0); + Status = Dev->VirtIo->SetQueueAddress ( + Dev->VirtIo, + &Dev->Ring, + RingBaseShift + ); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } @@ -758,7 +905,7 @@ VirtioBlkInit ( Features &= ~(UINT64)VIRTIO_F_VERSION_1; Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } } @@ -768,7 +915,7 @@ VirtioBlkInit ( NextDevStat |= VSTAT_DRIVER_OK; Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } // @@ -811,6 +958,9 @@ VirtioBlkInit ( } return EFI_SUCCESS; +UnmapQueue: + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap); + ReleaseQueue: VirtioRingUninit (Dev->VirtIo, &Dev->Ring); @@ -849,6 +999,7 @@ VirtioBlkUninit ( // Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0); + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap); VirtioRingUninit (Dev->VirtIo, &Dev->Ring); SetMem (&Dev->BlockIo, sizeof Dev->BlockIo, 0x00); @@ -885,6 +1036,12 @@ VirtioBlkExitBoot ( // Dev = Context; Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0); + + // + // Unmap the ring buffer so that hypervisor will not be able to get + // readable data after device is reset. + // + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap); } /** -- 2.7.4