From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-co1nam03on0049.outbound.protection.outlook.com [104.47.40.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 2A78D21D1825F for ; Mon, 14 Aug 2017 04:35:01 -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=vFDz9BS5WE906YFtz8yBbeF6Xai9IMvKDbL0+vIYCto=; b=F2LiLejeIhyouQqFY7zxNbhSokhZ0e9l8qYN/L4Wd4vaUV7uabrKcr8xhvIqqtDuudhVvAW/SyHS5TGibITfAhYe1oKhvvAnf91BxV0t99E+kzKciONK+6PhUSnAMWgURIeD+apjlAAMOWmCFAB2h8qcryGUGGp3KRSSqV8JlYk= 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 BY2PR12MB0145.namprd12.prod.outlook.com (10.162.82.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1341.21; Mon, 14 Aug 2017 11:37:21 +0000 From: Brijesh Singh To: edk2-devel@lists.01.org Cc: Brijesh Singh , Ard Biesheuvel , Jordan Justen , Tom Lendacky , Laszlo Ersek Date: Mon, 14 Aug 2017 07:36:38 -0400 Message-Id: <1502710605-8058-17-git-send-email-brijesh.singh@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1502710605-8058-1-git-send-email-brijesh.singh@amd.com> References: <1502710605-8058-1-git-send-email-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: MWHPR15CA0043.namprd15.prod.outlook.com (10.173.226.157) To BY2PR12MB0145.namprd12.prod.outlook.com (10.162.82.18) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1a90b9ee-a0a0-4637-a0bf-08d4e308d3bc 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:BY2PR12MB0145; X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0145; 3:KHooZpJXI/XKYVhW4nKzxYx4XW7oXgu+XXHZ9r3BzbPSKeyUPzCjWNqV1oGvYmud+Lq+YE7M+u3iH9ZlGEnpCdWBzlVmca7qPWGhLeqAmIYrtz1iOZYN16mqlHnVejFDGYH82eUlBfO2eektDppRJD6Pnzz1xWS7fk8pklYPOQTma77dQ4iVsYn2Jb71ZhRTnCwdSffPdVhcCw1Cvxip6qo0KY1J464vpdYRYwpUOmcgihRnzc6vlewMrTDi7cVk; 25:5P426GdHRwCJEFeWzEF1OnlbNZbfVfLDUSN5N9Xkc7/5iIhCp/rz6nmSTMCC4NwBg9Cs3CyW+gKjB3FxoJ1Xo473EHR8eJU94j0UrhtWQtKpB1Ib887/JTPAib4FkeYX1rK7M+b2WitiLWxWvoNdrPlCyZUfRzHhtLlqA2J/NISW6Ok4cXyX3nPEtcSYcSUYbf1ic84mTfTNl4jp1E3wCL+dTlRUMiCdWeUu9Pcv2VzXEUiEzjajcQEy+Z0dtAJpXCxBj0lgTHEaYg45BOU3aGsRJrMH2ExJ1MqIRXhqg9q1Ye3xoPjqZe88JiI/G7H7YiSvvY7htGGczHAOsliI3Q==; 31:Si/9UoZHn6L7ZvdX/6UXZHqeqQKIdyvii06V5QFeagpgd+YwnXPYNLGU7Z7uWCHHL2fOuR28VOcG7jy3USfs10v3H6Z90udvSQ57Bq6FEvoNs6Swlv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UriScan:(767451399110)(228905959029699); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(100000703101)(100105400095)(3002001)(6055026)(6041248)(20161123558100)(20161123560025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123555025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:BY2PR12MB0145; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:BY2PR12MB0145; X-Forefront-PRVS: 039975700A X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(7370300001)(4630300001)(6009001)(39860400002)(199003)(189002)(51234002)(54906002)(5003940100001)(6116002)(3846002)(53936002)(97736004)(86362001)(36756003)(81156014)(81166006)(110136004)(8676002)(189998001)(50986999)(76176999)(5660300001)(2361001)(25786009)(2351001)(50226002)(4326008)(7350300001)(33646002)(478600001)(53416004)(42186005)(2906002)(105586002)(7736002)(305945005)(106356001)(48376002)(50466002)(47776003)(66066001)(6486002)(6916009)(2950100002)(6666003)(68736007)(101416001); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR12MB0145; H:brijesh-build-machine.amd.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX: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; BY2PR12MB0145; 23:mkNtNphE3aoVJ2m3nhpgwLc0+DjGPZvmYQoPItHc7?= =?us-ascii?Q?pK+DWm3feV3dgcbPvmvJGeAXVYI+4C5sovitXk5TmZLvdEuk0XGN6GIQAH+I?= =?us-ascii?Q?ea39aUP3pRJCQg5ljj/XXWfdf5O9OXUJ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QhmC+TOFnymGUCJPCifDfbYUJBrhLXnHzZg= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0145; 20:Ro1k0x+hYGS3eYUIdHb3iWffev5fMezROwLIMx4oHa24L3boBGmqFo+iWAe+OQWmknGdNJ/94uE5WKAWyQk3kAQiqFIha2j5CoBFJsSIHUBAKS6slM8GMiqAJHryGO2IwPBrtu7qe50vdJgu1xVMSY5cH0uvbmZcSdmrcQHdE7AKaeM8ZmCw+sFzbie/uSUKpg8booP30vicl2kKpR9jecFhTCFVkpLW91azJBzk9oQYuowxRUkPhPmFSxxiaudO X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2017 11:37:21.1586 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR12MB0145 Subject: [PATCH v2 16/23] OvmfPkg/VirtioRngDxe: 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: Mon, 14 Aug 2017 11:35:01 -0000 Content-Type: text/plain patch maps the host address to a device address for buffers (including rings, device specifc request and response pointed by vring descriptor, and any further memory reference by those request and response). 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/VirtioRngDxe/VirtioRng.h | 1 + OvmfPkg/VirtioRngDxe/VirtioRng.c | 65 +++++++++++++++++--- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/OvmfPkg/VirtioRngDxe/VirtioRng.h b/OvmfPkg/VirtioRngDxe/VirtioRng.h index 998f9fae48c2..b372d84c1ebc 100644 --- a/OvmfPkg/VirtioRngDxe/VirtioRng.h +++ b/OvmfPkg/VirtioRngDxe/VirtioRng.h @@ -38,6 +38,7 @@ typedef struct { EFI_EVENT ExitBoot; // DriverBindingStart 0 VRING Ring; // VirtioRingInit 2 EFI_RNG_PROTOCOL Rng; // VirtioRngInit 1 + VOID *RingMap; // VirtioRngInit 1 } VIRTIO_RNG_DEV; #define VIRTIO_ENTROPY_SOURCE_FROM_RNG(RngPointer) \ diff --git a/OvmfPkg/VirtioRngDxe/VirtioRng.c b/OvmfPkg/VirtioRngDxe/VirtioRng.c index 0abca488e6cd..fc01f1996654 100644 --- a/OvmfPkg/VirtioRngDxe/VirtioRng.c +++ b/OvmfPkg/VirtioRngDxe/VirtioRng.c @@ -140,6 +140,8 @@ VirtioRngGetRNG ( UINT32 Len; UINT32 BufferSize; EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS DeviceAddress; + VOID *Mapping; if (This == NULL || RNGValueLength == 0 || RNGValue == NULL) { return EFI_INVALID_PARAMETER; @@ -159,6 +161,20 @@ VirtioRngGetRNG ( } Dev = VIRTIO_ENTROPY_SOURCE_FROM_RNG (This); + // + // Map the Buffers system phyiscal address to device address + // + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterWrite, + (VOID *)Buffer, + RNGValueLength, + &DeviceAddress, + &Mapping + ); + if (EFI_ERROR (Status)) { + goto FreeBuffer; + } // // The Virtio RNG device may return less data than we asked it to, and can @@ -170,7 +186,7 @@ VirtioRngGetRNG ( VirtioPrepare (&Dev->Ring, &Indices); VirtioAppendDesc (&Dev->Ring, - (UINTN)Buffer + Index, + (UINTN)DeviceAddress + Index, BufferSize, VRING_DESC_F_WRITE, &Indices); @@ -178,17 +194,30 @@ VirtioRngGetRNG ( if (VirtioFlush (Dev->VirtIo, 0, &Dev->Ring, &Indices, &Len) != EFI_SUCCESS) { Status = EFI_DEVICE_ERROR; - goto FreeBuffer; + goto UnmapBuffer; } ASSERT (Len > 0); ASSERT (Len <= BufferSize); } + // + // Unmap the device buffer before accesing it. + // + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Mapping); + for (Index = 0; Index < RNGValueLength; Index++) { RNGValue[Index] = Buffer[Index]; } Status = EFI_SUCCESS; + // + // Buffer is already Unmaped(), goto free it. + // + goto FreeBuffer; + +UnmapBuffer: + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Mapping); + FreeBuffer: FreePool ((VOID *)Buffer); return Status; @@ -205,6 +234,7 @@ VirtioRngInit ( EFI_STATUS Status; UINT16 QueueSize; UINT64 Features; + UINT64 RingBaseShift; // // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence. @@ -281,26 +311,33 @@ VirtioRngInit ( goto Failed; } + 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. // 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; } // @@ -310,7 +347,7 @@ VirtioRngInit ( Features &= ~(UINT64)VIRTIO_F_VERSION_1; Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } } @@ -320,7 +357,7 @@ VirtioRngInit ( NextDevStat |= VSTAT_DRIVER_OK; Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } // @@ -331,6 +368,9 @@ VirtioRngInit ( return EFI_SUCCESS; +UnmapQueue: + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap); + ReleaseQueue: VirtioRingUninit (Dev->VirtIo, &Dev->Ring); @@ -359,6 +399,9 @@ VirtioRngUninit ( // the old comms area. // Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0); + + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap); + VirtioRingUninit (Dev->VirtIo, &Dev->Ring); } @@ -385,6 +428,12 @@ VirtioRngExitBoot ( // Dev = Context; Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0); + + // + // Umap the ring buffer so that hypervisor will not able to get readable data + // after device reset. + // + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap); } -- 2.7.4