MdePkg/BaseLib: Add one wrapper on RdRand access for parameter check.

Add one wrapper implementation over assembly RdRand access to check
possible NULL parameter, for better alignment with comment description.

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qin Long <qin.long@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Qin Long 2016-11-04 15:31:28 +08:00
parent b3775af277
commit 9ec9a7a597
9 changed files with 188 additions and 61 deletions

View File

@ -428,6 +428,7 @@
X86EnablePaging32.c
X86DisablePaging64.c
X86DisablePaging32.c
X86RdRand.c
[Sources.X64]
X64/Thunk16.nasm
@ -752,6 +753,7 @@
X86EnablePaging32.c
X86DisablePaging64.c
X86DisablePaging32.c
X86RdRand.c
X64/GccInline.c | GCC
X64/Thunk16.S | XCODE
X64/SwitchStack.nasm| GCC

View File

@ -1,7 +1,7 @@
/** @file
Declaration of internal functions in BaseLib.
Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -728,6 +728,52 @@ InternalX86DisablePaging64 (
IN UINT32 NewStack
);
/**
Generates a 16-bit random number through RDRAND instruction.
@param[out] Rand Buffer pointer to store the random result.
@retval TRUE RDRAND call was successful.
@retval FALSE Failed attempts to call RDRAND.
**/
BOOLEAN
EFIAPI
InternalX86RdRand16 (
OUT UINT16 *Rand
);
/**
Generates a 32-bit random number through RDRAND instruction.
@param[out] Rand Buffer pointer to store the random result.
@retval TRUE RDRAND call was successful.
@retval FALSE Failed attempts to call RDRAND.
**/
BOOLEAN
EFIAPI
InternalX86RdRand32 (
OUT UINT32 *Rand
);
/**
Generates a 64-bit random number through RDRAND instruction.
@param[out] Rand Buffer pointer to store the random result.
@retval TRUE RDRAND call was successful.
@retval FALSE Failed attempts to call RDRAND.
**/
BOOLEAN
EFIAPI
InternalX86RdRand64 (
OUT UINT64 *Rand
);
#elif defined (MDE_CPU_IPF)
//

View File

@ -1,5 +1,5 @@
#------------------------------------------------------------------------------ ;
# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
@ -24,10 +24,10 @@
// Generates a 16 bit random number through RDRAND instruction.
// Return TRUE if Rand generated successfully, or FALSE if not.
//
// BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);
// BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
//------------------------------------------------------------------------------
ASM_GLOBAL ASM_PFX(AsmRdRand16)
ASM_PFX(AsmRdRand16):
ASM_GLOBAL ASM_PFX(InternalX86RdRand16)
ASM_PFX(InternalX86RdRand16):
.byte 0x0f, 0xc7, 0xf0 // rdrand r16: "0f c7 /6 ModRM:r/m(w)"
jc rn16_ok // jmp if CF=1
xor %eax, %eax // reg=0 if CF=0
@ -42,10 +42,10 @@ rn16_ok:
// Generates a 32 bit random number through RDRAND instruction.
// Return TRUE if Rand generated successfully, or FALSE if not.
//
// BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);
// BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
//------------------------------------------------------------------------------
ASM_GLOBAL ASM_PFX(AsmRdRand32)
ASM_PFX(AsmRdRand32):
ASM_GLOBAL ASM_PFX(InternalX86RdRand32)
ASM_PFX(InternalX86RdRand32):
.byte 0x0f, 0xc7, 0xf0 // rdrand r32: "0f c7 /6 ModRM:r/m(w)"
jc rn32_ok // jmp if CF=1
xor %eax, %eax // reg=0 if CF=0
@ -60,10 +60,10 @@ rn32_ok:
// Generates a 64 bit random number through RDRAND instruction.
// Return TRUE if Rand generated successfully, or FALSE if not.
//
// BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Rand);
// BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
//------------------------------------------------------------------------------
ASM_GLOBAL ASM_PFX(AsmRdRand64)
ASM_PFX(AsmRdRand64):
ASM_GLOBAL ASM_PFX(InternalX86RdRand64)
ASM_PFX(InternalX86RdRand64):
.byte 0x0f, 0xc7, 0xf0 // rdrand r32: "0f c7 /6 ModRM:r/m(w)"
jnc rn64_ret // jmp if CF=0
mov 0x4(%esp), %edx

View File

@ -1,6 +1,6 @@
;------------------------------------------------------------------------------
;
; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
; This program and the accompanying materials
; are licensed and made available under the terms and conditions of the BSD License
; which accompanies this distribution. The full text of the license may be found at
@ -30,9 +30,9 @@
; Generates a 16 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
;------------------------------------------------------------------------------
AsmRdRand16 PROC
InternalX86RdRand16 PROC
; rdrand ax ; generate a 16 bit RN into ax
; CF=1 if RN generated ok, otherwise CF=0
db 0fh, 0c7h, 0f0h ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"
@ -44,15 +44,15 @@ rn16_ok:
mov [edx], ax
mov eax, 1
ret
AsmRdRand16 ENDP
InternalX86RdRand16 ENDP
;------------------------------------------------------------------------------
; Generates a 32 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
;------------------------------------------------------------------------------
AsmRdRand32 PROC
InternalX86RdRand32 PROC
; rdrand eax ; generate a 32 bit RN into eax
; CF=1 if RN generated ok, otherwise CF=0
db 0fh, 0c7h, 0f0h ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"
@ -64,15 +64,15 @@ rn32_ok:
mov [edx], eax
mov eax, 1
ret
AsmRdRand32 ENDP
InternalX86RdRand32 ENDP
;------------------------------------------------------------------------------
; Generates a 64 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
;------------------------------------------------------------------------------
AsmRdRand64 PROC
InternalX86RdRand64 PROC
; rdrand eax ; generate a 32 bit RN into eax
; CF=1 if RN generated ok, otherwise CF=0
db 0fh, 0c7h, 0f0h ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"
@ -89,6 +89,6 @@ AsmRdRand64 PROC
rn64_ret:
xor eax, eax
ret ; return with failure status
AsmRdRand64 ENDP
InternalX86RdRand64 ENDP
END

View File

@ -1,6 +1,6 @@
;------------------------------------------------------------------------------
;
; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
; This program and the accompanying materials
; are licensed and made available under the terms and conditions of the BSD License
; which accompanies this distribution. The full text of the license may be found at
@ -27,10 +27,10 @@ SECTION .text
; Generates a 16 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
;------------------------------------------------------------------------------
global ASM_PFX(AsmRdRand16)
ASM_PFX(AsmRdRand16):
global ASM_PFX(InternalX86RdRand16)
ASM_PFX(InternalX86RdRand16):
; rdrand ax ; generate a 16 bit RN into ax
; CF=1 if RN generated ok, otherwise CF=0
db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"
@ -47,10 +47,10 @@ rn16_ok:
; Generates a 32 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
;------------------------------------------------------------------------------
global ASM_PFX(AsmRdRand32)
ASM_PFX(AsmRdRand32):
global ASM_PFX(InternalX86RdRand32)
ASM_PFX(InternalX86RdRand32):
; rdrand eax ; generate a 32 bit RN into eax
; CF=1 if RN generated ok, otherwise CF=0
db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"
@ -67,10 +67,10 @@ rn32_ok:
; Generates a 64 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
;------------------------------------------------------------------------------
global ASM_PFX(AsmRdRand64)
ASM_PFX(AsmRdRand64):
global ASM_PFX(InternalX86RdRand64)
ASM_PFX(InternalX86RdRand64):
; rdrand eax ; generate a 32 bit RN into eax
; CF=1 if RN generated ok, otherwise CF=0
db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"

View File

@ -1,5 +1,5 @@
#------------------------------------------------------------------------------ ;
# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
@ -24,10 +24,10 @@
// Generates a 16 bit random number through RDRAND instruction.
// Return TRUE if Rand generated successfully, or FALSE if not.
//
// BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);
// BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
//------------------------------------------------------------------------------
ASM_GLOBAL ASM_PFX(AsmRdRand16)
ASM_PFX(AsmRdRand16):
ASM_GLOBAL ASM_PFX(InternalX86RdRand16)
ASM_PFX(InternalX86RdRand16):
.byte 0x0f, 0xc7, 0xf0 // rdrand r16: "0f c7 /6 ModRM:r/m(w)"
jc rn16_ok // jmp if CF=1
xor %rax, %rax // reg=0 if CF=0
@ -41,10 +41,10 @@ rn16_ok:
// Generates a 32 bit random number through RDRAND instruction.
// Return TRUE if Rand generated successfully, or FALSE if not.
//
// BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);
// BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
//------------------------------------------------------------------------------
ASM_GLOBAL ASM_PFX(AsmRdRand32)
ASM_PFX(AsmRdRand32):
ASM_GLOBAL ASM_PFX(InternalX86RdRand32)
ASM_PFX(InternalX86RdRand32):
.byte 0x0f, 0xc7, 0xf0 // rdrand r32: "0f c7 /6 ModRM:r/m(w)"
jc rn32_ok // jmp if CF=1
xor %rax, %rax // reg=0 if CF=0
@ -58,10 +58,10 @@ rn32_ok:
// Generates a 64 bit random number through RDRAND instruction.
// Return TRUE if Rand generated successfully, or FALSE if not.
//
// BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Rand);
// BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
//------------------------------------------------------------------------------
ASM_GLOBAL ASM_PFX(AsmRdRand64)
ASM_PFX(AsmRdRand64):
ASM_GLOBAL ASM_PFX(InternalX86RdRand64)
ASM_PFX(InternalX86RdRand64):
.byte 0x48, 0x0f, 0xc7, 0xf0 // rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)"
jc rn64_ok // jmp if CF=1
xor %rax, %rax // reg=0 if CF=0

View File

@ -1,6 +1,6 @@
;------------------------------------------------------------------------------
;
; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
; This program and the accompanying materials
; are licensed and made available under the terms and conditions of the BSD License
; which accompanies this distribution. The full text of the license may be found at
@ -27,9 +27,9 @@
; Generates a 16 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
;------------------------------------------------------------------------------
AsmRdRand16 PROC
InternalX86RdRand16 PROC
; rdrand ax ; generate a 16 bit RN into eax,
; CF=1 if RN generated ok, otherwise CF=0
db 0fh, 0c7h, 0f0h ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"
@ -40,15 +40,15 @@ rn16_ok:
mov [rcx], ax
mov rax, 1
ret
AsmRdRand16 ENDP
InternalX86RdRand16 ENDP
;------------------------------------------------------------------------------
; Generates a 32 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
;------------------------------------------------------------------------------
AsmRdRand32 PROC
InternalX86RdRand32 PROC
; rdrand eax ; generate a 32 bit RN into eax,
; CF=1 if RN generated ok, otherwise CF=0
db 0fh, 0c7h, 0f0h ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"
@ -59,15 +59,15 @@ rn32_ok:
mov [rcx], eax
mov rax, 1
ret
AsmRdRand32 ENDP
InternalX86RdRand32 ENDP
;------------------------------------------------------------------------------
; Generates a 64 bit random number through one RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Random);
; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Random);
;------------------------------------------------------------------------------
AsmRdRand64 PROC
InternalX86RdRand64 PROC
; rdrand rax ; generate a 64 bit RN into rax,
; CF=1 if RN generated ok, otherwise CF=0
db 048h, 0fh, 0c7h, 0f0h ; rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)"
@ -78,6 +78,6 @@ rn64_ok:
mov [rcx], rax
mov rax, 1
ret
AsmRdRand64 ENDP
InternalX86RdRand64 ENDP
END

View File

@ -1,6 +1,6 @@
;------------------------------------------------------------------------------
;
; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
; This program and the accompanying materials
; are licensed and made available under the terms and conditions of the BSD License
; which accompanies this distribution. The full text of the license may be found at
@ -28,10 +28,10 @@
; Generates a 16 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
;------------------------------------------------------------------------------
global ASM_PFX(AsmRdRand16)
ASM_PFX(AsmRdRand16):
global ASM_PFX(InternalX86RdRand16)
ASM_PFX(InternalX86RdRand16):
; rdrand ax ; generate a 16 bit RN into eax,
; CF=1 if RN generated ok, otherwise CF=0
db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"
@ -47,10 +47,10 @@ rn16_ok:
; Generates a 32 bit random number through RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);
; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
;------------------------------------------------------------------------------
global ASM_PFX(AsmRdRand32)
ASM_PFX(AsmRdRand32):
global ASM_PFX(InternalX86RdRand32)
ASM_PFX(InternalX86RdRand32):
; rdrand eax ; generate a 32 bit RN into eax,
; CF=1 if RN generated ok, otherwise CF=0
db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"
@ -66,10 +66,10 @@ rn32_ok:
; Generates a 64 bit random number through one RDRAND instruction.
; Return TRUE if Rand generated successfully, or FALSE if not.
;
; BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Random);
; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Random);
;------------------------------------------------------------------------------
global ASM_PFX(AsmRdRand64)
ASM_PFX(AsmRdRand64):
global ASM_PFX(InternalX86RdRand64)
ASM_PFX(InternalX86RdRand64):
; rdrand rax ; generate a 64 bit RN into rax,
; CF=1 if RN generated ok, otherwise CF=0
db 0x48, 0xf, 0xc7, 0xf0 ; rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)"

View File

@ -0,0 +1,79 @@
/** @file
IA-32/x64 AsmRdRandxx()
Generates random number through CPU RdRand instruction.
Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "BaseLibInternals.h"
/**
Generates a 16-bit random number through RDRAND instruction.
if Rand is NULL, then ASSERT().
@param[out] Rand Buffer pointer to store the random result.
@retval TRUE RDRAND call was successful.
@retval FALSE Failed attempts to call RDRAND.
**/
BOOLEAN
EFIAPI
AsmRdRand16 (
OUT UINT16 *Rand
)
{
ASSERT (Rand != NULL);
return InternalX86RdRand16 (Rand);
}
/**
Generates a 32-bit random number through RDRAND instruction.
if Rand is NULL, then ASSERT().
@param[out] Rand Buffer pointer to store the random result.
@retval TRUE RDRAND call was successful.
@retval FALSE Failed attempts to call RDRAND.
**/
BOOLEAN
EFIAPI
AsmRdRand32 (
OUT UINT32 *Rand
)
{
ASSERT (Rand != NULL);
return InternalX86RdRand32 (Rand);
}
/**
Generates a 64-bit random number through RDRAND instruction.
if Rand is NULL, then ASSERT().
@param[out] Rand Buffer pointer to store the random result.
@retval TRUE RDRAND call was successful.
@retval FALSE Failed attempts to call RDRAND.
**/
BOOLEAN
EFIAPI
AsmRdRand64 (
OUT UINT64 *Rand
)
{
ASSERT (Rand != NULL);
return InternalX86RdRand64 (Rand);
}