From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (EUR04-DB3-obe.outbound.protection.outlook.com [40.107.6.46]) by mx.groups.io with SMTP id smtpd.web10.274.1620162564905192531 for ; Tue, 04 May 2021 14:09:25 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@armh.onmicrosoft.com header.s=selector2-armh-onmicrosoft-com header.b=2YYljK8F; spf=pass (domain: arm.com, ip: 40.107.6.46, mailfrom: sami.mujawar@arm.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=U4UKLZhDGCXJoZ4VO1M9Y+Rb1kPfsV60BdOT6tkP7uM=; b=2YYljK8F5OPs363tksb5QAc37eixCyIu50PrzYCGnpTyYCLgxYkN0z9CDcE9Gk7fTIoDZC6u/bvchfy+YRJg+92Aa7kkI/sY2MG7ls0MomRS3eAeaa69yd9ufTz23I/8rSBgan+Tb6f85OXC+LcgpUBeHC3Wokg7E6slKaYuIx4= Received: from AM7PR02CA0006.eurprd02.prod.outlook.com (2603:10a6:20b:100::16) by AM4PR08MB2930.eurprd08.prod.outlook.com (2603:10a6:205:b::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.25; Tue, 4 May 2021 21:09:21 +0000 Received: from AM5EUR03FT058.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:100:cafe::88) by AM7PR02CA0006.outlook.office365.com (2603:10a6:20b:100::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.27 via Frontend Transport; Tue, 4 May 2021 21:09:21 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; edk2.groups.io; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;edk2.groups.io; dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT058.mail.protection.outlook.com (10.152.17.48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.27 via Frontend Transport; Tue, 4 May 2021 21:09:21 +0000 Received: ("Tessian outbound 8ca198b738d3:v91"); Tue, 04 May 2021 21:09:20 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 27f779a519e15451 X-CR-MTA-TID: 64aa7808 Received: from 2ff50e5e8752.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 267723AE-EF41-4F28-8183-E8BBC9553DE3.1; Tue, 04 May 2021 21:09:15 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 2ff50e5e8752.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Tue, 04 May 2021 21:09:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n9XmmPWcdqfQhcYU7LsI0FWpET7iZKVFErdCsA8EMayM81+12PQtjczR5+zfDKlJbssFMYG3gc4du8PLkcZzrw6cLMzW8QY9YWqid8nIiq7kyie7AOV8a39sopl6Y3sC0jd+imdbOJ4XMWgTiZ3Wjb41KSG2lImcmSjMBkOix7Jd1smpW57X4JXI8Orqomi+uDmAkCjQsvXjnPH20tkUe/GIU/Pv/2jpGm5A9CxOUF6QFE9QuSUuaqF63Nl8d2pqdN6f08GmFGz+sJ4RjbtCkis4onURC5XI4IscEHfLdWBCTovzrl5xJbELEzx6yi6j1ti2k26QzWoPG76rBmqUjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=U4UKLZhDGCXJoZ4VO1M9Y+Rb1kPfsV60BdOT6tkP7uM=; b=OIH6Om9lN+sQBgGCF6O8NmhPak9BOacBM1nXqtHiNadB/Pq/wj+0SCNo4fQhzYXPHaKphqglJmqs+I2TT2IiKXCNXEsLAS0UHg4maUTZhir9jVDHA7J36SsnnI3T8cnAAlPklQ0I1GkuRedtR/rzXu+mEM7KlOLCV4+gz2ttzqyvXMCfT0i5tpGk5kp2hR6Cyl0oHnWI7bDh2Wi4AvxqaFNVwqRWj+FJweK8J05+1Sa367nS33d3HPBz6WHDMQCOfOb2zMxTjiIWNCpvSEFXt4XHsIRpxrjBsoblcgvM+pQCMmIa9buH70oHkYZUC6tNPct0CeV8AJ4ng0V4E7OS8A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=U4UKLZhDGCXJoZ4VO1M9Y+Rb1kPfsV60BdOT6tkP7uM=; b=2YYljK8F5OPs363tksb5QAc37eixCyIu50PrzYCGnpTyYCLgxYkN0z9CDcE9Gk7fTIoDZC6u/bvchfy+YRJg+92Aa7kkI/sY2MG7ls0MomRS3eAeaa69yd9ufTz23I/8rSBgan+Tb6f85OXC+LcgpUBeHC3Wokg7E6slKaYuIx4= Authentication-Results-Original: arm.com; dkim=none (message not signed) header.d=none;arm.com; dmarc=none action=none header.from=arm.com; Received: from AS8PR08MB6806.eurprd08.prod.outlook.com (2603:10a6:20b:39b::12) by AM5PR0801MB1841.eurprd08.prod.outlook.com (2603:10a6:203:2e::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.44; Tue, 4 May 2021 21:09:10 +0000 Received: from AS8PR08MB6806.eurprd08.prod.outlook.com ([fe80::80cb:878d:c8f1:2688]) by AS8PR08MB6806.eurprd08.prod.outlook.com ([fe80::80cb:878d:c8f1:2688%7]) with mapi id 15.20.4087.044; Tue, 4 May 2021 21:09:10 +0000 Subject: Re: [PATCH 2/3] MdePkg: Refactor BaseRngLib to support AARCH64 in addition to X86 To: Rebecca Cran , devel@edk2.groups.io Cc: Jiewen Yao , Jian J Wang , Michael D Kinney , Liming Gao , Zhiguang Liu , Ard Biesheuvel , nd@arm.com References: <20210428204415.25454-1-rebecca@nuviainc.com> <20210428204415.25454-3-rebecca@nuviainc.com> From: "Sami Mujawar" Message-ID: Date: Tue, 4 May 2021 22:09:06 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.0.1 In-Reply-To: <20210428204415.25454-3-rebecca@nuviainc.com> X-Originating-IP: [217.140.106.52] X-ClientProxiedBy: LNXP265CA0036.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:5c::24) To AS8PR08MB6806.eurprd08.prod.outlook.com (2603:10a6:20b:39b::12) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [10.1.196.43] (217.140.106.52) by LNXP265CA0036.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:5c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.43 via Frontend Transport; Tue, 4 May 2021 21:09:08 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 56bd78f1-2737-42f4-8304-08d90f40e30c X-MS-TrafficTypeDiagnostic: AM5PR0801MB1841:|AM4PR08MB2930: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:8882;OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 3JQlhAt2vfQFYl1MYl/RYfO1XYcy7j7e4sHDA8uM+p4/1SbSef0c09lnmyHegb9XN0XnuoX5wdwYKeP+lvQtGgfMIHKtNy4qM5CG9W39l9f8fQTRwMCsHp8Wz3PKbjmzy0FRrrDMyVb4Nz/pCTZxiqLEEzogzwBk4OaaLAGCL7D8G+8Qel4FXxiHqwjx6UqYbHPMCw/4eM0VrRZlEUCnD/r0QNBU4ld3CGx3eRZrP32Ohg2x3cDfjbz3iyJgxbCk1cumN+iD3Wl7Yxg2q/H+zUEg2Ry8zyjkn9iG9gsjwrorhCBHy7zayz3h2PHeF/RJFYED+K7c+o0umIrmZPMIWJ4yb5OuAUX+WVFvlxaGLHo0pCzvgWrTVoLLDHdHmAm8Ft7iYWz0uEB+x68AhKILiSREBJN464Ie+Ld/fEo2VphdPZgJ/OerRZJghVk2c1IqtsDPOAzskhTvx58gSbq32SZewyZ4HFcMl2+Y2Zz3Aj3g1YoNEABBNA/xefKcfHNdqumilGh1qULFhir/oetaM8Xjv9DeMbPPe7UBRNTygHav0VXDyQ6zAe9vx2a/rBf/3eTOOZx8xfga8uz20RXmdv4P9In1Hnuq1atLFkVudgHFflpf4cl/z3hPzQmPkfm7Jg61nDkKw+83lzjW4b1k41pxAdM9npCK6ehr8nuBMRfLVqIJGkmqwDOmltXPDTfzp6dgipt09rt0oodm1XJ8QWZwCr2H3Zu2afWmQD9ZXM64ycscZ4/J15/P9wiSIO4513tSYgEu82i6qfWjT0Cq6oXK7DPLciBGkURk38pRGGpSmIbEJp6d3Foqo4yT0mc/mXt8DkZ8pRTVxJ52NPXcbQ== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AS8PR08MB6806.eurprd08.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(39830400003)(8676002)(16526019)(86362001)(166002)(966005)(44832011)(36756003)(38350700002)(53546011)(16576012)(83380400001)(4326008)(38100700002)(5660300002)(66946007)(2906002)(31696002)(8936002)(31686004)(508600001)(26005)(66556008)(30864003)(956004)(2616005)(33964004)(52116002)(186003)(54906003)(19627235002)(66476007)(34490700003)(6486002)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?utf-8?B?RTdlMHMxUXdyL0FiSUFQR2hZQkQ0YUdCWEZ0UVB3TzkxMFhpUndFVk4zcElU?= =?utf-8?B?TVk0NWY3dmhFU3A5OFlYTEdUOWM2bkFWYmlOUjg5bmZnRWhyQjNTNnhpYlhY?= =?utf-8?B?V1VIWFI5bUJLdFJlQUdGZW91SDBZaXRFeXVVWlUyRS9VRWgvMnZ3Z1lHMHpo?= =?utf-8?B?dC9wbys4bVNJaGlrNElHNDgzdEs5d0pCS0xCT0grR2hkeTlOb0F5bnJuQ003?= =?utf-8?B?OVMzY1lZMEY2MC8rME9rTzM5Rlh1TTdrUWtLTG5iUVk5QjQrQm9JRzYrQkxq?= =?utf-8?B?S0JvMlNxQ2J1eEt1NzdocHc1cUQ1dzRKL2UxbE5BNUwzNGh2SUd2RDhGUGZp?= =?utf-8?B?dHNpdTZLTVhUVyt5Vk5CYkRITi82OEVNazdPaVREbklIMk5PY1F1dEN2QzdC?= =?utf-8?B?RmxHL1hwSFUyMDhWNXIvaDFFZHN1MmZGakxDTlVkRWRtRkdsaXlwUVFwTVo2?= =?utf-8?B?ek5VNGdnSWdYMC96VUlJQ0pKVjlVaWZZYU1FdjNDcmMyMnY3WkhIcVV3eDhu?= =?utf-8?B?cExabkc2cXl3OHpkL0c4WUhGNHJxUmp3UEZIa1hXeWR5N212Vm92VzZQaitB?= =?utf-8?B?amNTR1R4YktweWtEa1p3dU1zT05nTnBwYy9xYjZFWFdicjhOYldHdWJiZWcv?= =?utf-8?B?U3hjaVJwcHlrdjBMeXB5eHYyK3F5WXdLQTV0d29ieWVUR1ZSdTZXN1NQbkRU?= =?utf-8?B?QndvbDZDNVh6ejI2cjkzbURCSW5Rd1dxbGdqM1FWTmhmRUUyLzFTZTFTaDhk?= =?utf-8?B?MktzM2VRUFBPUXU1ZWtjaFh1cnlKendadEIwR2lER04vTElnT0lTRHNUL1ZX?= =?utf-8?B?aHVIelFUWTRoTjNUY3R0NDFMWmRlc3FrRUowQXNqVlQ0OFljTjdxMUFpTDhH?= =?utf-8?B?ZUpqZ3VEN3hCenlUdDlXRnhNLzh4emhEbmZQWnJJUnBVK1gxUWtTdzFrVXh5?= =?utf-8?B?Z3dkU1lWUlM4YWtkTVJnckFvQnEyMWtOdWVPdFJIeXByS1AySmN2ZnFXTnlI?= =?utf-8?B?RmU5NXhwWEtUSGV4WVVodHZqdW5CTVp6Zms3bVlyMVptRnNHZWovWFk4MjlW?= =?utf-8?B?TXRFclBLeHJrUFlYRzFobWZwVGdlOTZFUEl0WE53cUl2bzhTNE9DVGRjR09B?= =?utf-8?B?SFpxV1RsT0YrQ1V3Y3F5cWNJTmNKczhrNzFkdzE1UVYrY21RSWROc0lCWk11?= =?utf-8?B?cWh3c3h0ZlJ1dTBqSkY1NHd2UDRFc04rNFgyM2pLNzM0MVpLMFp4emtoZXQr?= =?utf-8?B?czUxMkNNY3A4UmZEbGZka0d5OTNKWjB2Ynl3YjVGNTBMeTdPRFZYeUd5eHdR?= =?utf-8?B?ZWhMeStPMlFId3krRTJDdDFSSW5GUmxYd09aeXAwcjJPcmtCOHgyQk0yZjAw?= =?utf-8?B?NzlYZ01NVE5ibzlpZUN0RVB5ZEdnNTQ5K1JUcmttek83OWw2eXdna2ZPSFJo?= =?utf-8?B?QmV2VnRSU05jVkRYRFJoeXlxL2s2UVA0MHJMSHlGajJOU1FFdjBOenplTUx3?= =?utf-8?B?dVdzUFdkSE1DYUVWdkRxRUEzT04zcStwdHhqRVZxU2VWTi9uaUt1NlZtL1RY?= =?utf-8?B?OUZIOVk3ZnV0UGtYalJmaGpMS3YvNjQ2M2FHUEVpRWFmdUVpUVdNOXJTaGZz?= =?utf-8?B?cVpUUnJzK1pXcnkwUHMvdGs2R2dSazdKenRFZC93cCtSeHQ1bE02NUtPVG5l?= =?utf-8?B?aWZDNWdZbE8xeWZUMlhobllCRTJyVnUyMXl2V29yYmVCdXVxUlJWNytjUS9s?= =?utf-8?Q?7snyVhDIV58lmayYdiyAUwe0FRYZ3+54nxSBgxJ?= X-MS-Exchange-Transport-Forked: True X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0801MB1841 Original-Authentication-Results: arm.com; dkim=none (message not signed) header.d=none;arm.com; dmarc=none action=none header.from=arm.com; Return-Path: Sami.Mujawar@arm.com X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT058.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 46da757c-9ec0-488c-8d18-08d90f40dbad X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lv4U1ctkISoGlf/0jCv4Dcu58CKrir97s2CGFQcs2vICQ//N24JfvD77QPUeD6/hHS7ln+M9ReBlkzdmDTkW7OWO1bjtLwAfTVQ2CoNEN6cZh6PAGJulKqw6aWqj8IEcqes4yxW+NZvUje6Jp6Ot+Pk/s8lSeMzfH1p/AUEmnukaYqfvXD5yJScD4zzIGhDqnKkvWTcbAOL9VQeQCeOyjGfHMpcBtwOcI2qZOrfj4GG4ZM/S0IqEyt7YoqGgwoSHda4UMTjvslNPUvx7RlOZeNUhWoADQl3luz9iwh3RSrq/dR4PudxqZ/K2Eoo3lc1kC48DOByS/xVEHJCHHYlyWlFBebswhcv/qPJMB1Iv+KUoY6Qloq6Yc/iZzRZUuTDAoe1MH/7qTr3LWEMfeFAeFrmbypsJ6Vy3fx/V52HrfwlZ4cOMplAtgCXrEFvP3DLa6w5nFY9FQ3NwU98fp59CvbIM20bpQLRfJX4II/eU2Fd+hhZ/AvHCnDR9tni+D25QATihyQDVYEajNTf9YjJL+McsNyQvDKz9QXWkP3QcyLgrgKBTwCxzpX0md7oB5RwEQB2jHoG44zjyAkIAmNYPHTBR+vOi+uqr/NQDdvGHNLrGhRk4tJbF1Tyxh/ZaOgW3c6U97UihdlkMaTMnPJSOy2BqmjFbJlcIJefb6YQN2sVMEfBO7jjMFtEBpEjKuVIedJRvyxKXCgYzXoWsxqyw3SLXnwVRDs4eBhEv5utZgOLt8ulrfSDxu49NuWzMPZWHfh/2D9aJ2Ah6cKKjKBlB8g== X-Forefront-Antispam-Report: CIP:63.35.35.123;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:64aa7808-outbound-1.mta.getcheckrecipient.com;PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com;CAT:NONE;SFS:(4636009)(396003)(376002)(346002)(39830400003)(46966006)(36840700001)(36756003)(86362001)(6486002)(33964004)(70206006)(31686004)(19627235002)(2616005)(44832011)(82310400003)(53546011)(186003)(47076005)(36860700001)(70586007)(81166007)(26005)(2906002)(8936002)(956004)(356005)(166002)(4326008)(83380400001)(16576012)(508600001)(54906003)(336012)(16526019)(30864003)(31696002)(5660300002)(966005)(8676002)(43740500002);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2021 21:09:21.0477 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 56bd78f1-2737-42f4-8304-08d90f40e30c X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[63.35.35.123];Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM5EUR03FT058.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR08MB2930 Content-Type: multipart/alternative; boundary="------------FB924235584EF581F5CE4BA6" Content-Language: en-GB --------------FB924235584EF581F5CE4BA6 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi Rebecca, Please see my response inline marked [SAMI]. Regards, Sami Mujawar On 28/04/2021 09:44 PM, Rebecca Cran wrote: > Make BaseRngLib more generic by moving x86 specific functionality from > BaseRng.c into Rand/RdRand.c, and adding AArch64/Rndr.c, which supports > the optional ARMv8.5 RNG instructions RNDR and RNDRRS that are a part of > FEAT_RNG. > > Signed-off-by: Rebecca Cran > --- > MdePkg/MdePkg.dec | 9 +- > MdePkg/MdePkg.dsc | 4 +- > MdePkg/Library/BaseRngLib/BaseRngLib.inf | 16 ++- > MdePkg/Library/BaseRngLib/BaseRngLibInternals.h | 31 +++++ > MdePkg/Library/BaseRngLib/AArch64/Rndr.c | 121 ++++++++++++++++++++ > MdePkg/Library/BaseRngLib/BaseRng.c | 55 +++------ > MdePkg/Library/BaseRngLib/Rand/RdRand.c | 103 +++++++++++++++++ > MdePkg/Library/BaseRngLib/BaseRngLib.uni | 6 +- > 8 files changed, 291 insertions(+), 54 deletions(-) > > diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec > index 8965e903e093..b49f88d8e18f 100644 > --- a/MdePkg/MdePkg.dec > +++ b/MdePkg/MdePkg.dec > @@ -267,6 +267,11 @@ [LibraryClasses] > # > RegisterFilterLib|Include/Library/RegisterFilterLib.h > > +[LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64] > + ## @libraryclass Provides services to generate random number. > + # > + RngLib|Include/Library/RngLib.h > + > [LibraryClasses.IA32, LibraryClasses.X64] > ## @libraryclass Abstracts both S/W SMI generation and detection. > ## > @@ -288,10 +293,6 @@ [LibraryClasses.IA32, LibraryClasses.X64] > # > SmmPeriodicSmiLib|Include/Library/SmmPeriodicSmiLib.h > > - ## @libraryclass Provides services to generate random number. > - # > - RngLib|Include/Library/RngLib.h > - > ## @libraryclass Provides services to log the SMI handler registration. > SmiHandlerProfileLib|Include/Library/SmiHandlerProfileLib.h > > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc > index d363419006ea..a94959169b2f 100644 > --- a/MdePkg/MdePkg.dsc > +++ b/MdePkg/MdePkg.dsc > @@ -145,6 +145,9 @@ [Components.IA32, Components.X64, Components.ARM, Components.AARCH64] > MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf > MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf > > +[Components.IA32, Components.X64, Components.AARCH64] > + MdePkg/Library/BaseRngLib/BaseRngLib.inf > + > [Components.IA32, Components.X64] > MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf > MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf > @@ -168,7 +171,6 @@ [Components.IA32, Components.X64] > MdePkg/Library/BaseS3StallLib/BaseS3StallLib.inf > MdePkg/Library/SmmMemLib/SmmMemLib.inf > MdePkg/Library/SmmIoLib/SmmIoLib.inf > - MdePkg/Library/BaseRngLib/BaseRngLib.inf > MdePkg/Library/SmmPciExpressLib/SmmPciExpressLib.inf > MdePkg/Library/SmiHandlerProfileLibNull/SmiHandlerProfileLibNull.inf > MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf > diff --git a/MdePkg/Library/BaseRngLib/BaseRngLib.inf b/MdePkg/Library/BaseRngLib/BaseRngLib.inf > index 31740751c69c..1dc3249a8c20 100644 > --- a/MdePkg/Library/BaseRngLib/BaseRngLib.inf > +++ b/MdePkg/Library/BaseRngLib/BaseRngLib.inf > @@ -1,9 +1,10 @@ > ## @file > # Instance of RNG (Random Number Generator) Library. > # > -# BaseRng Library that uses CPU RdRand instruction access to provide > -# high-quality random numbers. > +# BaseRng Library that uses CPU RNG instructions (e.g. RdRand) to > +# provide high-quality random numbers. > # > +# Copyright (c) 2020, NUVIA Inc. All rights reserved.
> # Copyright (c) 2015, Intel Corporation. All rights reserved.
> # > # SPDX-License-Identifier: BSD-2-Clause-Patent > @@ -22,11 +23,18 @@ [Defines] > CONSTRUCTOR = BaseRngLibConstructor > > # > -# VALID_ARCHITECTURES = IA32 X64 > +# VALID_ARCHITECTURES = IA32 X64 AARCH64 > # > > -[Sources.Ia32, Sources.X64] > +[Sources] > BaseRng.c > + BaseRngLibInternals.h > + > +[Sources.Ia32, Sources.X64] > + Rand/RdRand.c > + > +[Sources.AARCH64] > + AArch64/Rndr.c > > [Packages] > MdePkg/MdePkg.dec > diff --git a/MdePkg/Library/BaseRngLib/BaseRngLibInternals.h b/MdePkg/Library/BaseRngLib/BaseRngLibInternals.h > new file mode 100644 > index 000000000000..44fda69c9eec > --- /dev/null > +++ b/MdePkg/Library/BaseRngLib/BaseRngLibInternals.h > @@ -0,0 +1,31 @@ > +/** @file > + > + Architecture specific interface to RNG functionality. > + > +Copyright (c) 2020, NUVIA Inc. All rights reserved.
> + > +SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#ifndef BASE_RNGLIB_INTERNALS_H_ > + > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber16 ( > + OUT UINT16 *Rand > + ); > + > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber32 ( > + OUT UINT32 *Rand > + ); > + > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber64 ( > + OUT UINT64 *Rand > + ); [SAMI] Please add doxygen headers for the above functions. [/SAMI] > + > +#endif // BASE_RNGLIB_INTERNALS_H_ > diff --git a/MdePkg/Library/BaseRngLib/AArch64/Rndr.c b/MdePkg/Library/BaseRngLib/AArch64/Rndr.c > new file mode 100644 > index 000000000000..19643237923a > --- /dev/null > +++ b/MdePkg/Library/BaseRngLib/AArch64/Rndr.c > @@ -0,0 +1,121 @@ > +/** @file > + Random number generator service that uses the RNDR instruction > + to provide high-quality random numbers. > + > + Copyright (c) 2020, NUVIA Inc. All rights reserved.
> + Copyright (c) 2015, Intel Corporation. All rights reserved.
> + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > + > +#include > + > +#include "BaseRngLibInternals.h" > + > +// > +// Bit mask used to determine if RNDR instruction is supported. > +// > +#define RNDR_MASK ((UINT64)MAX_UINT16 << 60U) > + > +/** > + The constructor function checks whether or not RNDR instruction is supported > + by the host hardware. > + > + The constructor function checks whether or not RNDR instruction is supported. > + It will ASSERT() if RNDR instruction is not supported. > + It will always return RETURN_SUCCESS. > + > + @retval RETURN_SUCCESS The constructor always returns EFI_SUCCESS. > + > +**/ > +RETURN_STATUS [SAMI] Should the return type be EFI_STATUS? See https://edk2-docs.gitbook.io/edk-ii-module-writer-s-guide/3_module_development/33_additional_steps_for_library_instances#3-3-2-1-types-of-library-constructor-functions > +EFIAPI > +BaseRngLibConstructor ( > + VOID > + ) > +{ > + UINT64 Isar0; > + // > + // Determine RNDR support by examining bits 63:60 of the ISAR0 register returned by > + // MSR. A non-zero value indicates that the processor supports the RNDR instruction. > + // > + Isar0 = ArmReadIdIsar0 (); > + ASSERT ((Isar0 & RNDR_MASK) != 0); > + (void)Isar0; [SAMI] ASSERTs will vanish in the release builds. So, I think this needs to be an if condition. If RNDR is not supported RETURN_UNSUPPORTED should be returned. However, it appears thatthe auto generated function ProcessLibraryConstructorList() disregards the error code returned by the constructor (see Build\...\AutoGen.c files). So it looks like the loading operation would continue in release builds despite of an error. I am not aware if this is the desired behavior or why the status code returned by the constructor is disregarded. However, this would be a probem in the current case as subsequent calls to generate random numbers will result in an undefined instruction exception. To prevent this, I think the above check should be done in either - ArmRndr()/ArmRndrrs() or - preferably in ArchGetRandomNumberXX(), which should return an error code EFI_UNSUPPORTED, EFI_NOT_READY or EFI_SUCCESS. However, the impact on IA32/x64 code needs to be evaluated. [/SAMI] > + > + return RETURN_SUCCESS; > +} > + > + > +/** > + Generates a 16-bit random number. > + > + @param[out] Rand Buffer pointer to store the 16-bit random value. > + > + @retval TRUE Random number generated successfully. > + @retval FALSE Failed to generate the random number. > + > +**/ > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber16 ( > + OUT UINT16 *Rand > + ) > +{ > + UINT64 Rand64; > + > + if (ArchGetRandomNumber64 (&Rand64)) { > + *Rand = Rand64 & MAX_UINT16; > + return TRUE; > + } > + > + return FALSE; > +} > + > +/** > + Generates a 32-bit random number. > + > + @param[out] Rand Buffer pointer to store the 32-bit random value. > + > + @retval TRUE Random number generated successfully. > + @retval FALSE Failed to generate the random number. > + > +**/ > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber32 ( > + OUT UINT32 *Rand > + ) > +{ > + UINT64 Rand64; > + > + if (ArchGetRandomNumber64 (&Rand64)) { > + *Rand = Rand64 & MAX_UINT32; > + return TRUE; > + } > + > + return FALSE; > +} > + > +/** > + Generates a 64-bit random number. > + > + @param[out] Rand Buffer pointer to store the 64-bit random value. > + > + @retval TRUE Random number generated successfully. > + @retval FALSE Failed to generate the random number. > + > +**/ > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber64 ( > + OUT UINT64 *Rand > + ) > +{ > + return ArmRndr (Rand); > +} > + > diff --git a/MdePkg/Library/BaseRngLib/BaseRng.c b/MdePkg/Library/BaseRngLib/BaseRng.c > index 7ad7aec9d38f..072fa37d3394 100644 > --- a/MdePkg/Library/BaseRngLib/BaseRng.c > +++ b/MdePkg/Library/BaseRngLib/BaseRng.c > @@ -1,8 +1,10 @@ > /** @file > - Random number generator services that uses RdRand instruction access > - to provide high-quality random numbers. > + Random number generator services that uses CPU RNG instructions to > + provide high-quality random numbers. > > +Copyright (c) 2021, NUVIA Inc. All rights reserved.
> Copyright (c) 2015, Intel Corporation. All rights reserved.
> + > SPDX-License-Identifier: BSD-2-Clause-Patent > > **/ > @@ -10,46 +12,15 @@ SPDX-License-Identifier: BSD-2-Clause-Patent > #include > #include > > -// > -// Bit mask used to determine if RdRand instruction is supported. > -// > -#define RDRAND_MASK BIT30 > +#include "BaseRngLibInternals.h" > > // > // Limited retry number when valid random data is returned. > // Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32 > -// Architectures Software Developer's Mannual". > +// Architectures Software Developer's Manual". > // > -#define RDRAND_RETRY_LIMIT 10 > +#define GETRANDOM_RETRY_LIMIT 10 > > -/** > - The constructor function checks whether or not RDRAND instruction is supported > - by the host hardware. > - > - The constructor function checks whether or not RDRAND instruction is supported. > - It will ASSERT() if RDRAND instruction is not supported. > - It will always return RETURN_SUCCESS. > - > - @retval RETURN_SUCCESS The constructor always returns EFI_SUCCESS. > - > -**/ > -RETURN_STATUS > -EFIAPI > -BaseRngLibConstructor ( > - VOID > - ) > -{ > - UINT32 RegEcx; > - > - // > - // Determine RDRAND support by examining bit 30 of the ECX register returned by > - // CPUID. A value of 1 indicates that processor support RDRAND instruction. > - // > - AsmCpuid (1, 0, 0, &RegEcx, 0); > - ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK); > - > - return RETURN_SUCCESS; > -} > > /** > Generates a 16-bit random number. > @@ -75,8 +46,8 @@ GetRandomNumber16 ( > // > // A loop to fetch a 16 bit random value with a retry count limit. > // > - for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) { > - if (AsmRdRand16 (Rand)) { > + for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) { > + if (ArchGetRandomNumber16 (Rand)) { > return TRUE; > } > } > @@ -108,8 +79,8 @@ GetRandomNumber32 ( > // > // A loop to fetch a 32 bit random value with a retry count limit. > // > - for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) { > - if (AsmRdRand32 (Rand)) { > + for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) { > + if (ArchGetRandomNumber32 (Rand)) { > return TRUE; > } > } > @@ -141,8 +112,8 @@ GetRandomNumber64 ( > // > // A loop to fetch a 64 bit random value with a retry count limit. > // > - for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) { > - if (AsmRdRand64 (Rand)) { > + for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) { > + if (ArchGetRandomNumber64 (Rand)) { > return TRUE; > } > } > diff --git a/MdePkg/Library/BaseRngLib/Rand/RdRand.c b/MdePkg/Library/BaseRngLib/Rand/RdRand.c > new file mode 100644 > index 000000000000..3f1378064b4c > --- /dev/null > +++ b/MdePkg/Library/BaseRngLib/Rand/RdRand.c > @@ -0,0 +1,103 @@ > +/** @file > + Random number generator services that uses RdRand instruction access > + to provide high-quality random numbers. > + > +Copyright (c) 2015, Intel Corporation. All rights reserved.
> + > +SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > + > +#include "BaseRngLibInternals.h" > + > +// > +// Bit mask used to determine if RdRand instruction is supported. > +// > +#define RDRAND_MASK BIT30 > + > +/** > + The constructor function checks whether or not RDRAND instruction is supported > + by the host hardware. > + > + The constructor function checks whether or not RDRAND instruction is supported. > + It will ASSERT() if RDRAND instruction is not supported. > + It will always return RETURN_SUCCESS. > + > + @retval RETURN_SUCCESS The constructor always returns EFI_SUCCESS. > + > +**/ > +RETURN_STATUS > +EFIAPI > +BaseRngLibConstructor ( > + VOID > + ) > +{ > + UINT32 RegEcx; > + > + // > + // Determine RDRAND support by examining bit 30 of the ECX register returned by > + // CPUID. A value of 1 indicates that processor support RDRAND instruction. > + // > + AsmCpuid (1, 0, 0, &RegEcx, 0); > + ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK); > + > + return RETURN_SUCCESS; > +} > + > +/** > + Generates a 16-bit random number. > + > + @param[out] Rand Buffer pointer to store the 16-bit random value. > + > + @retval TRUE Random number generated successfully. > + @retval FALSE Failed to generate the random number. > + > +**/ > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber16 ( > + OUT UINT16 *Rand > + ) > +{ > + return AsmRdRand16 (Rand); > +} > + > +/** > + Generates a 32-bit random number. > + > + @param[out] Rand Buffer pointer to store the 32-bit random value. > + > + @retval TRUE Random number generated successfully. > + @retval FALSE Failed to generate the random number. > + > +**/ > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber32 ( > + OUT UINT32 *Rand > + ) > +{ > + return AsmRdRand32 (Rand); > +} > + > +/** > + Generates a 64-bit random number. > + > + @param[out] Rand Buffer pointer to store the 64-bit random value. > + > + @retval TRUE Random number generated successfully. > + @retval FALSE Failed to generate the random number. > + > +**/ > +BOOLEAN > +EFIAPI > +ArchGetRandomNumber64 ( > + OUT UINT64 *Rand > + ) > +{ > + return AsmRdRand64 (Rand); > +} > + > diff --git a/MdePkg/Library/BaseRngLib/BaseRngLib.uni b/MdePkg/Library/BaseRngLib/BaseRngLib.uni > index f3ed954c5209..8c7fe1219450 100644 > --- a/MdePkg/Library/BaseRngLib/BaseRngLib.uni > +++ b/MdePkg/Library/BaseRngLib/BaseRngLib.uni > @@ -1,8 +1,8 @@ > // /** @file > // Instance of RNG (Random Number Generator) Library. > // > -// BaseRng Library that uses CPU RdRand instruction access to provide > -// high-quality random numbers. > +// BaseRng Library that uses CPU RNG instructions to provide high-quality > +// random numbers. > // > // Copyright (c) 2015, Intel Corporation. All rights reserved.
> // > @@ -13,5 +13,5 @@ > > #string STR_MODULE_ABSTRACT #language en-US "Instance of RNG Library" > > -#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library that uses CPU RdRand instruction access to provide high-quality random numbers" > +#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library that uses CPU RNG instructions to provide high-quality random numbers" > --------------FB924235584EF581F5CE4BA6 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

Hi Rebecca,

Please see my response inline marked [SAMI].

Regards,

Sami Mujawar

On 28/04/2021 09:44 PM, Rebecca Cran wrote:
Make BaseRngLib more generic by moving x86 specific functionality from
BaseRng.c into Rand/RdRand.c, and adding AArch64/Rndr.c, which supports
the optional ARMv8.5 RNG instructions RNDR and RNDRRS that are a part of
FEAT_RNG.

Signed-off-by: Rebecca Cran <rebecca@nuviainc.com>
---
 MdePkg/MdePkg.dec                               |   9 +-
 MdePkg/MdePkg.dsc                               |   4 +-
 MdePkg/Library/BaseRngLib/BaseRngLib.inf        |  16 ++-
 MdePkg/Library/BaseRngLib/BaseRngLibInternals.h |  31 +++++
 MdePkg/Library/BaseRngLib/AArch64/Rndr.c        | 121 ++++++++++++++++++++
 MdePkg/Library/BaseRngLib/BaseRng.c             |  55 +++------
 MdePkg/Library/BaseRngLib/Rand/RdRand.c         | 103 +++++++++++++++++
 MdePkg/Library/BaseRngLib/BaseRngLib.uni        |   6 +-
 8 files changed, 291 insertions(+), 54 deletions(-)

diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec
index 8965e903e093..b49f88d8e18f 100644
--- a/MdePkg/MdePkg.dec
+++ b/MdePkg/MdePkg.dec
@@ -267,6 +267,11 @@ [LibraryClasses]
   #
   RegisterFilterLib|Include/Library/RegisterFilterLib.h
 
+[LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64]
+  ##  @libraryclass  Provides services to generate random number.
+  #
+  RngLib|Include/Library/RngLib.h
+
 [LibraryClasses.IA32, LibraryClasses.X64]
   ##  @libraryclass  Abstracts both S/W SMI generation and detection.
   ##
@@ -288,10 +293,6 @@ [LibraryClasses.IA32, LibraryClasses.X64]
   #
   SmmPeriodicSmiLib|Include/Library/SmmPeriodicSmiLib.h
 
-  ##  @libraryclass  Provides services to generate random number.
-  #
-  RngLib|Include/Library/RngLib.h
-
   ##  @libraryclass  Provides services to log the SMI handler registration.
   SmiHandlerProfileLib|Include/Library/SmiHandlerProfileLib.h
 
diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index d363419006ea..a94959169b2f 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -145,6 +145,9 @@ [Components.IA32, Components.X64, Components.ARM, Components.AARCH64]
   MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf
   MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf
 
+[Components.IA32, Components.X64, Components.AARCH64]
+  MdePkg/Library/BaseRngLib/BaseRngLib.inf
+
 [Components.IA32, Components.X64]
   MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
   MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf
@@ -168,7 +171,6 @@ [Components.IA32, Components.X64]
   MdePkg/Library/BaseS3StallLib/BaseS3StallLib.inf
   MdePkg/Library/SmmMemLib/SmmMemLib.inf
   MdePkg/Library/SmmIoLib/SmmIoLib.inf
-  MdePkg/Library/BaseRngLib/BaseRngLib.inf
   MdePkg/Library/SmmPciExpressLib/SmmPciExpressLib.inf
   MdePkg/Library/SmiHandlerProfileLibNull/SmiHandlerProfileLibNull.inf
   MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
diff --git a/MdePkg/Library/BaseRngLib/BaseRngLib.inf b/MdePkg/Library/BaseRngLib/BaseRngLib.inf
index 31740751c69c..1dc3249a8c20 100644
--- a/MdePkg/Library/BaseRngLib/BaseRngLib.inf
+++ b/MdePkg/Library/BaseRngLib/BaseRngLib.inf
@@ -1,9 +1,10 @@
 ## @file
 #  Instance of RNG (Random Number Generator) Library.
 #
-#  BaseRng Library that uses CPU RdRand instruction access to provide
-#  high-quality random numbers.
+#  BaseRng Library that uses CPU RNG instructions (e.g. RdRand) to
+#  provide high-quality random numbers.
 #
+#  Copyright (c) 2020, NUVIA Inc. All rights reserved.<BR>
 #  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -22,11 +23,18 @@ [Defines]
   CONSTRUCTOR                    = BaseRngLibConstructor
 
 #
-#  VALID_ARCHITECTURES           = IA32 X64
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
 #
 
-[Sources.Ia32, Sources.X64]
+[Sources]
   BaseRng.c
+  BaseRngLibInternals.h
+
+[Sources.Ia32, Sources.X64]
+  Rand/RdRand.c
+
+[Sources.AARCH64]
+  AArch64/Rndr.c
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/MdePkg/Library/BaseRngLib/BaseRngLibInternals.h b/MdePkg/Library/BaseRngLib/BaseRngLibInternals.h
new file mode 100644
index 000000000000..44fda69c9eec
--- /dev/null
+++ b/MdePkg/Library/BaseRngLib/BaseRngLibInternals.h
@@ -0,0 +1,31 @@
+/** @file
+
+  Architecture specific interface to RNG functionality.
+
+Copyright (c) 2020, NUVIA Inc. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef BASE_RNGLIB_INTERNALS_H_
+
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber16 (
+  OUT UINT16 *Rand
+  );
+
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber32 (
+  OUT UINT32 *Rand
+  );
+
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber64 (
+  OUT UINT64 *Rand
+  );
[SAMI] Please add doxygen headers for the above functions.
[/SAMI]
+
+#endif    // BASE_RNGLIB_INTERNALS_H_
diff --git a/MdePkg/Library/BaseRngLib/AArch64/Rndr.c b/MdePkg/Library/BaseRngLib/AArch64/Rndr.c
new file mode 100644
index 000000000000..19643237923a
--- /dev/null
+++ b/MdePkg/Library/BaseRngLib/AArch64/Rndr.c
@@ -0,0 +1,121 @@
+/** @file
+  Random number generator service that uses the RNDR instruction
+  to provide high-quality random numbers.
+
+  Copyright (c) 2020, NUVIA Inc. All rights reserved.<BR>
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+
+#include <Library/RngLib.h>
+
+#include "BaseRngLibInternals.h"
+
+//
+// Bit mask used to determine if RNDR instruction is supported.
+//
+#define RNDR_MASK                  ((UINT64)MAX_UINT16 << 60U)
+
+/**
+  The constructor function checks whether or not RNDR instruction is supported
+  by the host hardware.
+
+  The constructor function checks whether or not RNDR instruction is supported.
+  It will ASSERT() if RNDR instruction is not supported.
+  It will always return RETURN_SUCCESS.
+
+  @retval RETURN_SUCCESS   The constructor always returns EFI_SUCCESS.
+
+**/
+RETURN_STATUS
[SAMI] Should the return type be EFI_STATUS? See https://edk2-docs.gitbook.io/edk-ii-module-writer-s-guide/3_module_development/33_additional_steps_for_library_instances#3-3-2-1-types-of-library-constructor-functions
+EFIAPI
+BaseRngLibConstructor (
+  VOID
+  )
+{
+  UINT64 Isar0;
+  //
+  // Determine RNDR support by examining bits 63:60 of the ISAR0 register returned by
+  // MSR. A non-zero value indicates that the processor supports the RNDR instruction.
+  //
+  Isar0 = ArmReadIdIsar0 ();
+  ASSERT ((Isar0 & RNDR_MASK) != 0);
+  (void)Isar0;
[SAMI] ASSERTs will vanish in the release builds. So, I think this needs to be an if condition. If RNDR is not supported RETURN_UNSUPPORTED should be returned.
However, it appears that the auto generated function ProcessLibraryConstructorList() disregards the error code returned by the constructor (see Build\...\AutoGen.c files). So it looks like the loading operation would continue in release builds despite of an error.
I am not aware if this is the desired behavior or why the status code returned by the constructor is disregarded.

However, this would be a probem in the current case as s
ubsequent calls to generate random numbers will result in an undefined instruction exception.
To prevent this, I think the above check should be done in either
   - ArmRndr()/ArmRndrrs()
  or
   - preferably in ArchGetRandomNumberXX(), which should return an error code EFI_UNSUPPORTED, EFI_NOT_READY or EFI_SUCCESS. However, the impact on IA32/x64 code needs to be evaluated.

[/SAMI]
+
+  return RETURN_SUCCESS;
+}
+
+
+/**
+  Generates a 16-bit random number.
+
+  @param[out] Rand     Buffer pointer to store the 16-bit random value.
+
+  @retval TRUE         Random number generated successfully.
+  @retval FALSE        Failed to generate the random number.
+
+**/
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber16 (
+  OUT     UINT16                    *Rand
+  )
+{
+  UINT64 Rand64;
+
+  if (ArchGetRandomNumber64 (&Rand64)) {
+    *Rand = Rand64 & MAX_UINT16;
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/**
+  Generates a 32-bit random number.
+
+  @param[out] Rand     Buffer pointer to store the 32-bit random value.
+
+  @retval TRUE         Random number generated successfully.
+  @retval FALSE        Failed to generate the random number.
+
+**/
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber32 (
+  OUT     UINT32                    *Rand
+  )
+{
+  UINT64 Rand64;
+
+  if (ArchGetRandomNumber64 (&Rand64)) {
+    *Rand = Rand64 & MAX_UINT32;
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/**
+  Generates a 64-bit random number.
+
+  @param[out] Rand     Buffer pointer to store the 64-bit random value.
+
+  @retval TRUE         Random number generated successfully.
+  @retval FALSE        Failed to generate the random number.
+
+**/
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber64 (
+  OUT     UINT64                    *Rand
+  )
+{
+  return ArmRndr (Rand);
+}
+
diff --git a/MdePkg/Library/BaseRngLib/BaseRng.c b/MdePkg/Library/BaseRngLib/BaseRng.c
index 7ad7aec9d38f..072fa37d3394 100644
--- a/MdePkg/Library/BaseRngLib/BaseRng.c
+++ b/MdePkg/Library/BaseRngLib/BaseRng.c
@@ -1,8 +1,10 @@
 /** @file
-  Random number generator services that uses RdRand instruction access
-  to provide high-quality random numbers.
+  Random number generator services that uses CPU RNG instructions to
+  provide high-quality random numbers.
 
+Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -10,46 +12,15 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/BaseLib.h>
 #include <Library/DebugLib.h>
 
-//
-// Bit mask used to determine if RdRand instruction is supported.
-//
-#define RDRAND_MASK                  BIT30
+#include "BaseRngLibInternals.h"
 
 //
 // Limited retry number when valid random data is returned.
 // Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32
-// Architectures Software Developer's Mannual".
+// Architectures Software Developer's Manual".
 //
-#define RDRAND_RETRY_LIMIT           10
+#define GETRANDOM_RETRY_LIMIT           10
 
-/**
-  The constructor function checks whether or not RDRAND instruction is supported
-  by the host hardware.
-
-  The constructor function checks whether or not RDRAND instruction is supported.
-  It will ASSERT() if RDRAND instruction is not supported.
-  It will always return RETURN_SUCCESS.
-
-  @retval RETURN_SUCCESS   The constructor always returns EFI_SUCCESS.
-
-**/
-RETURN_STATUS
-EFIAPI
-BaseRngLibConstructor (
-  VOID
-  )
-{
-  UINT32  RegEcx;
-
-  //
-  // Determine RDRAND support by examining bit 30 of the ECX register returned by
-  // CPUID. A value of 1 indicates that processor support RDRAND instruction.
-  //
-  AsmCpuid (1, 0, 0, &RegEcx, 0);
-  ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
-
-  return RETURN_SUCCESS;
-}
 
 /**
   Generates a 16-bit random number.
@@ -75,8 +46,8 @@ GetRandomNumber16 (
   //
   // A loop to fetch a 16 bit random value with a retry count limit.
   //
-  for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
-    if (AsmRdRand16 (Rand)) {
+  for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
+    if (ArchGetRandomNumber16 (Rand)) {
       return TRUE;
     }
   }
@@ -108,8 +79,8 @@ GetRandomNumber32 (
   //
   // A loop to fetch a 32 bit random value with a retry count limit.
   //
-  for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
-    if (AsmRdRand32 (Rand)) {
+  for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
+    if (ArchGetRandomNumber32 (Rand)) {
       return TRUE;
     }
   }
@@ -141,8 +112,8 @@ GetRandomNumber64 (
   //
   // A loop to fetch a 64 bit random value with a retry count limit.
   //
-  for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
-    if (AsmRdRand64 (Rand)) {
+  for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
+    if (ArchGetRandomNumber64 (Rand)) {
       return TRUE;
     }
   }
diff --git a/MdePkg/Library/BaseRngLib/Rand/RdRand.c b/MdePkg/Library/BaseRngLib/Rand/RdRand.c
new file mode 100644
index 000000000000..3f1378064b4c
--- /dev/null
+++ b/MdePkg/Library/BaseRngLib/Rand/RdRand.c
@@ -0,0 +1,103 @@
+/** @file
+  Random number generator services that uses RdRand instruction access
+  to provide high-quality random numbers.
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+
+#include "BaseRngLibInternals.h"
+
+//
+// Bit mask used to determine if RdRand instruction is supported.
+//
+#define RDRAND_MASK                  BIT30
+
+/**
+  The constructor function checks whether or not RDRAND instruction is supported
+  by the host hardware.
+
+  The constructor function checks whether or not RDRAND instruction is supported.
+  It will ASSERT() if RDRAND instruction is not supported.
+  It will always return RETURN_SUCCESS.
+
+  @retval RETURN_SUCCESS   The constructor always returns EFI_SUCCESS.
+
+**/
+RETURN_STATUS
+EFIAPI
+BaseRngLibConstructor (
+  VOID
+  )
+{
+  UINT32  RegEcx;
+
+  //
+  // Determine RDRAND support by examining bit 30 of the ECX register returned by
+  // CPUID. A value of 1 indicates that processor support RDRAND instruction.
+  //
+  AsmCpuid (1, 0, 0, &RegEcx, 0);
+  ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
+
+  return RETURN_SUCCESS;
+}
+
+/**
+  Generates a 16-bit random number.
+
+  @param[out] Rand     Buffer pointer to store the 16-bit random value.
+
+  @retval TRUE         Random number generated successfully.
+  @retval FALSE        Failed to generate the random number.
+
+**/
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber16 (
+  OUT     UINT16                    *Rand
+  )
+{
+  return AsmRdRand16 (Rand);
+}
+
+/**
+  Generates a 32-bit random number.
+
+  @param[out] Rand     Buffer pointer to store the 32-bit random value.
+
+  @retval TRUE         Random number generated successfully.
+  @retval FALSE        Failed to generate the random number.
+
+**/
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber32 (
+  OUT     UINT32                    *Rand
+  )
+{
+  return AsmRdRand32 (Rand);
+}
+
+/**
+  Generates a 64-bit random number.
+
+  @param[out] Rand     Buffer pointer to store the 64-bit random value.
+
+  @retval TRUE         Random number generated successfully.
+  @retval FALSE        Failed to generate the random number.
+
+**/
+BOOLEAN
+EFIAPI
+ArchGetRandomNumber64 (
+  OUT     UINT64                    *Rand
+  )
+{
+  return AsmRdRand64 (Rand);
+}
+
diff --git a/MdePkg/Library/BaseRngLib/BaseRngLib.uni b/MdePkg/Library/BaseRngLib/BaseRngLib.uni
index f3ed954c5209..8c7fe1219450 100644
--- a/MdePkg/Library/BaseRngLib/BaseRngLib.uni
+++ b/MdePkg/Library/BaseRngLib/BaseRngLib.uni
@@ -1,8 +1,8 @@
 // /** @file
 // Instance of RNG (Random Number Generator) Library.
 //
-// BaseRng Library that uses CPU RdRand instruction access to provide
-// high-quality random numbers.
+// BaseRng Library that uses CPU RNG instructions to provide high-quality
+// random numbers.
 //
 // Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
 //
@@ -13,5 +13,5 @@
 
 #string STR_MODULE_ABSTRACT             #language en-US "Instance of RNG Library"
 
-#string STR_MODULE_DESCRIPTION          #language en-US "BaseRng Library that uses CPU RdRand instruction access to provide high-quality random numbers"
+#string STR_MODULE_DESCRIPTION          #language en-US "BaseRng Library that uses CPU RNG instructions to provide high-quality random numbers"
 

--------------FB924235584EF581F5CE4BA6--