]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/RdRand.asm
ArmPkg/ArmDisassemblerLib: fix check for MSR instruction
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / 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 32-bit platform.\r
19;\r
20; Notes:\r
21;\r
22;------------------------------------------------------------------------------\r
23\r
24.686P\r
25.model flat, C\r
26\r
27.code\r
28\r
29;------------------------------------------------------------------------------\r
30; Generates a 16 bit random number through RDRAND instruction.\r
31; Return TRUE if Rand generated successfully, or FALSE if not.\r
32;\r
9ec9a7a5 33; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);\r
3cfc7813 34;------------------------------------------------------------------------------\r
9ec9a7a5 35InternalX86RdRand16 PROC\r
3cfc7813
QL
36 ; rdrand ax ; generate a 16 bit RN into ax\r
37 ; CF=1 if RN generated ok, otherwise CF=0\r
38 db 0fh, 0c7h, 0f0h ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"\r
39 jc rn16_ok ; jmp if CF=1\r
40 xor eax, eax ; reg=0 if CF=0\r
41 ret ; return with failure status\r
42rn16_ok:\r
43 mov edx, dword ptr [esp + 4]\r
44 mov [edx], ax\r
45 mov eax, 1\r
46 ret\r
9ec9a7a5 47InternalX86RdRand16 ENDP\r
3cfc7813
QL
48\r
49;------------------------------------------------------------------------------\r
50; Generates a 32 bit random number through RDRAND instruction.\r
51; Return TRUE if Rand generated successfully, or FALSE if not.\r
52;\r
9ec9a7a5 53; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);\r
3cfc7813 54;------------------------------------------------------------------------------\r
9ec9a7a5 55InternalX86RdRand32 PROC\r
3cfc7813
QL
56 ; rdrand eax ; generate a 32 bit RN into eax\r
57 ; CF=1 if RN generated ok, otherwise CF=0\r
58 db 0fh, 0c7h, 0f0h ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r
59 jc rn32_ok ; jmp if CF=1\r
60 xor eax, eax ; reg=0 if CF=0\r
61 ret ; return with failure status\r
62rn32_ok:\r
63 mov edx, dword ptr [esp + 4]\r
64 mov [edx], eax\r
65 mov eax, 1\r
66 ret\r
9ec9a7a5 67InternalX86RdRand32 ENDP\r
3cfc7813
QL
68\r
69;------------------------------------------------------------------------------\r
70; Generates a 64 bit random number through RDRAND instruction.\r
71; Return TRUE if Rand generated successfully, or FALSE if not.\r
72;\r
9ec9a7a5 73; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);\r
3cfc7813 74;------------------------------------------------------------------------------\r
9ec9a7a5 75InternalX86RdRand64 PROC\r
3cfc7813
QL
76 ; rdrand eax ; generate a 32 bit RN into eax\r
77 ; CF=1 if RN generated ok, otherwise CF=0\r
78 db 0fh, 0c7h, 0f0h ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r
79 jnc rn64_ret ; jmp if CF=0\r
80 mov edx, dword ptr [esp + 4]\r
81 mov [edx], eax\r
82\r
83 db 0fh, 0c7h, 0f0h ; generate another 32 bit RN\r
84 jnc rn64_ret ; jmp if CF=0\r
85 mov [edx + 4], eax\r
86\r
87 mov eax, 1\r
88 ret\r
89rn64_ret:\r
90 xor eax, eax\r
91 ret ; return with failure status\r
9ec9a7a5 92InternalX86RdRand64 ENDP\r
3cfc7813
QL
93\r
94 END\r