]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/X64/RdRand.asm
ArmPkg/ArmDisassemblerLib: fix check for MSR instruction
[mirror_edk2.git] / MdePkg / Library / BaseLib / X64 / RdRand.asm
CommitLineData
3cfc7813
QL
1;------------------------------------------------------------------------------\r
2;\r
9ec9a7a5 3; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
3cfc7813
QL
4; This program and the accompanying materials\r
5; are licensed and made available under the terms and conditions of the BSD License\r
6; which accompanies this distribution. The full text of the license may be found at\r
7; http://opensource.org/licenses/bsd-license.php.\r
8;\r
9; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11;\r
12; Module Name:\r
13;\r
14; RdRand.asm\r
15;\r
16; Abstract:\r
17;\r
18; Generates random number through CPU RdRand instruction under 64-bit platform.\r
19;\r
20; Notes:\r
21;\r
22;------------------------------------------------------------------------------\r
23\r
24 .code\r
25\r
26;------------------------------------------------------------------------------\r
27; Generates a 16 bit random number through RDRAND instruction.\r
28; Return TRUE if Rand generated successfully, or FALSE if not.\r
29;\r
9ec9a7a5 30; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);\r
3cfc7813 31;------------------------------------------------------------------------------\r
9ec9a7a5 32InternalX86RdRand16 PROC\r
3cfc7813
QL
33 ; rdrand ax ; generate a 16 bit RN into eax,\r
34 ; CF=1 if RN generated ok, otherwise CF=0\r
35 db 0fh, 0c7h, 0f0h ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"\r
36 jc rn16_ok ; jmp if CF=1\r
37 xor rax, rax ; reg=0 if CF=0\r
38 ret ; return with failure status\r
39rn16_ok:\r
40 mov [rcx], ax\r
41 mov rax, 1\r
42 ret\r
9ec9a7a5 43InternalX86RdRand16 ENDP\r
3cfc7813
QL
44\r
45;------------------------------------------------------------------------------\r
46; Generates a 32 bit random number through RDRAND instruction.\r
47; Return TRUE if Rand generated successfully, or FALSE if not.\r
48;\r
9ec9a7a5 49; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);\r
3cfc7813 50;------------------------------------------------------------------------------\r
9ec9a7a5 51InternalX86RdRand32 PROC\r
3cfc7813
QL
52 ; rdrand eax ; generate a 32 bit RN into eax,\r
53 ; CF=1 if RN generated ok, otherwise CF=0\r
54 db 0fh, 0c7h, 0f0h ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r
55 jc rn32_ok ; jmp if CF=1\r
56 xor rax, rax ; reg=0 if CF=0\r
57 ret ; return with failure status\r
58rn32_ok:\r
59 mov [rcx], eax\r
60 mov rax, 1\r
61 ret\r
9ec9a7a5 62InternalX86RdRand32 ENDP\r
3cfc7813
QL
63\r
64;------------------------------------------------------------------------------\r
65; Generates a 64 bit random number through one RDRAND instruction.\r
66; Return TRUE if Rand generated successfully, or FALSE if not.\r
67;\r
9ec9a7a5 68; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Random);\r
3cfc7813 69;------------------------------------------------------------------------------\r
9ec9a7a5 70InternalX86RdRand64 PROC\r
3cfc7813
QL
71 ; rdrand rax ; generate a 64 bit RN into rax,\r
72 ; CF=1 if RN generated ok, otherwise CF=0\r
73 db 048h, 0fh, 0c7h, 0f0h ; rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)"\r
74 jc rn64_ok ; jmp if CF=1\r
75 xor rax, rax ; reg=0 if CF=0\r
76 ret ; return with failure status\r
77rn64_ok:\r
78 mov [rcx], rax\r
79 mov rax, 1\r
80 ret\r
9ec9a7a5 81InternalX86RdRand64 ENDP\r
3cfc7813
QL
82\r
83 END\r