1#------------------------------------------------------------------------------ ;
2# Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
3# This program and the accompanying materials
4# are licensed and made available under the terms and conditions of the BSD License
5# which accompanies this distribution.  The full text of the license may be found at
6# http://opensource.org/licenses/bsd-license.php.
7#
8# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10#
11# Module Name:
12#
13#   RdRand.S
14#
15# Abstract:
16#
17#   Generates random number through CPU RdRand instruction under 64-bit platform.
18#
19# Notes:
20#
21#------------------------------------------------------------------------------
22
23//------------------------------------------------------------------------------
24//  Generates a 16 bit random number through RDRAND instruction.
25//  Return TRUE if Rand generated successfully, or FALSE if not.
26//
27//  BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);
28//------------------------------------------------------------------------------
29ASM_GLOBAL ASM_PFX(InternalX86RdRand16)
30ASM_PFX(InternalX86RdRand16):
31    .byte  0x0f, 0xc7, 0xf0        // rdrand r16: "0f c7 /6  ModRM:r/m(w)"
32    jc     rn16_ok                 // jmp if CF=1
33    xor    %rax, %rax              // reg=0 if CF=0
34    ret                            // return with failure status
35rn16_ok:
36    mov    %ax, (%rcx)
37    mov    $0x1, %rax
38    ret
39
40//------------------------------------------------------------------------------
41//  Generates a 32 bit random number through RDRAND instruction.
42//  Return TRUE if Rand generated successfully, or FALSE if not.
43//
44//  BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);
45//------------------------------------------------------------------------------
46ASM_GLOBAL ASM_PFX(InternalX86RdRand32)
47ASM_PFX(InternalX86RdRand32):
48    .byte  0x0f, 0xc7, 0xf0        // rdrand r32: "0f c7 /6  ModRM:r/m(w)"
49    jc     rn32_ok                 // jmp if CF=1
50    xor    %rax, %rax              // reg=0 if CF=0
51    ret                            // return with failure status
52rn32_ok:
53    mov    %eax, (%rcx)
54    mov    $0x1, %rax
55    ret
56
57//------------------------------------------------------------------------------
58//  Generates a 64 bit random number through RDRAND instruction.
59//  Return TRUE if Rand generated successfully, or FALSE if not.
60//
61//  BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);
62//------------------------------------------------------------------------------
63ASM_GLOBAL ASM_PFX(InternalX86RdRand64)
64ASM_PFX(InternalX86RdRand64):
65    .byte  0x48, 0x0f, 0xc7, 0xf0  // rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)"
66    jc     rn64_ok                 // jmp if CF=1
67    xor    %rax, %rax              // reg=0 if CF=0
68    ret                            // return with failure status
69rn64_ok:
70    mov    %rax, (%rcx)
71    mov    $0x1, %rax
72    ret
73