]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/X64/RdRand.S
MdePkg: Add CPU RdRand access APIs for random number generation
[mirror_edk2.git] / MdePkg / Library / BaseLib / X64 / RdRand.S
1 #------------------------------------------------------------------------------ ;
2 # Copyright (c) 2015, 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 AsmRdRand16 (UINT16 *Rand);
28 //------------------------------------------------------------------------------
29 ASM_GLOBAL ASM_PFX(AsmRdRand16)
30 ASM_PFX(AsmRdRand16):
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
35 rn16_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 AsmRdRand32 (UINT32 *Rand);
45 //------------------------------------------------------------------------------
46 ASM_GLOBAL ASM_PFX(AsmRdRand32)
47 ASM_PFX(AsmRdRand32):
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
52 rn32_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 AsmRdRand64 (UINT64 *Rand);
62 //------------------------------------------------------------------------------
63 ASM_GLOBAL ASM_PFX(AsmRdRand64)
64 ASM_PFX(AsmRdRand64):
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
69 rn64_ok:
70 mov %rax, (%rcx)
71 mov $0x1, %rax
72 ret