Re: [edk2-devel] [PATCH v3 1/2] MdePkg/BaseRngLib: Add support for ARMv8.5 RNG instructions

2021-05-11 Thread Sami Mujawar

This patch looks good to me.

Reviewed-by: Sami Mujawar 

Regards,

Sami Mujawar


On 10/05/2021 10:53 PM, Rebecca Cran wrote:

Make BaseRngLib more generic by moving x86-specific functionality into
'Rand' and adding files under 'AArch64' to support the optional ARMv8.5
RNG instruction RNDR that is a part of FEAT_RNG.

Signed-off-by: Rebecca Cran 
---
  MdePkg/MdePkg.dec|   9 +-
  MdePkg/MdePkg.dsc|   4 +-
  MdePkg/Library/BaseRngLib/BaseRngLib.inf |  23 +++-
  MdePkg/Library/BaseRngLib/AArch64/ArmRng.h   |  43 ++
  MdePkg/Library/BaseRngLib/BaseRngLibInternals.h  |  78 +++
  MdePkg/Library/BaseRngLib/AArch64/Rndr.c | 139 

  MdePkg/Library/BaseRngLib/BaseRng.c  |  87 ++--
  MdePkg/Library/BaseRngLib/Rand/RdRand.c  | 131 ++
  MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.S   |  31 +
  MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.asm |  30 +
  MdePkg/Library/BaseRngLib/AArch64/ArmRng.S   |  37 ++
  MdePkg/Library/BaseRngLib/AArch64/ArmRng.asm |  39 ++
  MdePkg/Library/BaseRngLib/BaseRngLib.uni |   6 +-
  13 files changed, 603 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..1fcceb941495 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 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
@@ -22,11 +23,25 @@ [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
+  AArch64/ArmRng.h
+
+  AArch64/ArmReadIdIsar0.S   | GCC
+  AArch64/ArmRng.S   | GCC
+
+  AArch64/ArmReadIdIsar0.asm | MSFT
+  AArch64/ArmRng.asm | MSFT

  [Packages]
MdePkg/MdePkg.dec
diff --git a/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h 
b/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h
new file mode 100644
index ..a597e98bf0d5
--- /dev/null
+++ b/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h
@@ -0,0 +1,43 @@
+/** @file
+  Random number generator service that uses the RNDR instruction
+  to provide pseudorandom numbers.
+
+  Copyright (c) 2021, NUVIA Inc. All rights reserved.
+
+  SPDX-Lice

[edk2-devel] [PATCH v3 1/2] MdePkg/BaseRngLib: Add support for ARMv8.5 RNG instructions

2021-05-10 Thread Rebecca Cran
Make BaseRngLib more generic by moving x86-specific functionality into
'Rand' and adding files under 'AArch64' to support the optional ARMv8.5
RNG instruction RNDR that is a part of FEAT_RNG.

Signed-off-by: Rebecca Cran 
---
 MdePkg/MdePkg.dec|   9 +-
 MdePkg/MdePkg.dsc|   4 +-
 MdePkg/Library/BaseRngLib/BaseRngLib.inf |  23 +++-
 MdePkg/Library/BaseRngLib/AArch64/ArmRng.h   |  43 ++
 MdePkg/Library/BaseRngLib/BaseRngLibInternals.h  |  78 +++
 MdePkg/Library/BaseRngLib/AArch64/Rndr.c | 139 
 MdePkg/Library/BaseRngLib/BaseRng.c  |  87 ++--
 MdePkg/Library/BaseRngLib/Rand/RdRand.c  | 131 ++
 MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.S   |  31 +
 MdePkg/Library/BaseRngLib/AArch64/ArmReadIdIsar0.asm |  30 +
 MdePkg/Library/BaseRngLib/AArch64/ArmRng.S   |  37 ++
 MdePkg/Library/BaseRngLib/AArch64/ArmRng.asm |  39 ++
 MdePkg/Library/BaseRngLib/BaseRngLib.uni |   6 +-
 13 files changed, 603 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..1fcceb941495 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 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
@@ -22,11 +23,25 @@ [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
+  AArch64/ArmRng.h
+
+  AArch64/ArmReadIdIsar0.S   | GCC
+  AArch64/ArmRng.S   | GCC
+
+  AArch64/ArmReadIdIsar0.asm | MSFT
+  AArch64/ArmRng.asm | MSFT
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h 
b/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h
new file mode 100644
index ..a597e98bf0d5
--- /dev/null
+++ b/MdePkg/Library/BaseRngLib/AArch64/ArmRng.h
@@ -0,0 +1,43 @@
+/** @file
+  Random number generator service that uses the RNDR instruction
+  to provide pseudorandom numbers.
+
+  Copyright (c) 2021, NUVIA Inc. All rights reserved.
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef ARM_RNG_H_
+#define ARM_RNG_H_
+
+/**
+  Generates a random number using RNDR.
+  Returns TRUE on success; FALSE on