]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/RdRand.nasm
MdePkg/BaseLib: Add one wrapper on RdRand access for parameter check.
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / RdRand.nasm
CommitLineData
55745c24
JJ
1;------------------------------------------------------------------------------\r
2;\r
9ec9a7a5 3; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
55745c24
JJ
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.nasm\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
24SECTION .text\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
55745c24 31;------------------------------------------------------------------------------\r
9ec9a7a5
QL
32global ASM_PFX(InternalX86RdRand16)\r
33ASM_PFX(InternalX86RdRand16):\r
55745c24
JJ
34 ; rdrand ax ; generate a 16 bit RN into ax\r
35 ; CF=1 if RN generated ok, otherwise CF=0\r
36 db 0xf, 0xc7, 0xf0 ; rdrand r16: "0f c7 /6 ModRM:r/m(w)"\r
37 jc rn16_ok ; jmp if CF=1\r
38 xor eax, eax ; reg=0 if CF=0\r
39 ret ; return with failure status\r
40rn16_ok:\r
41 mov edx, dword [esp + 4]\r
42 mov [edx], ax\r
43 mov eax, 1\r
44 ret\r
45\r
46;------------------------------------------------------------------------------\r
47; Generates a 32 bit random number through RDRAND instruction.\r
48; Return TRUE if Rand generated successfully, or FALSE if not.\r
49;\r
9ec9a7a5 50; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);\r
55745c24 51;------------------------------------------------------------------------------\r
9ec9a7a5
QL
52global ASM_PFX(InternalX86RdRand32)\r
53ASM_PFX(InternalX86RdRand32):\r
55745c24
JJ
54 ; rdrand eax ; generate a 32 bit RN into eax\r
55 ; CF=1 if RN generated ok, otherwise CF=0\r
56 db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r
57 jc rn32_ok ; jmp if CF=1\r
58 xor eax, eax ; reg=0 if CF=0\r
59 ret ; return with failure status\r
60rn32_ok:\r
61 mov edx, dword [esp + 4]\r
62 mov [edx], eax\r
63 mov eax, 1\r
64 ret\r
65\r
66;------------------------------------------------------------------------------\r
67; Generates a 64 bit random number through RDRAND instruction.\r
68; Return TRUE if Rand generated successfully, or FALSE if not.\r
69;\r
9ec9a7a5 70; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);\r
55745c24 71;------------------------------------------------------------------------------\r
9ec9a7a5
QL
72global ASM_PFX(InternalX86RdRand64)\r
73ASM_PFX(InternalX86RdRand64):\r
55745c24
JJ
74 ; rdrand eax ; generate a 32 bit RN into eax\r
75 ; CF=1 if RN generated ok, otherwise CF=0\r
76 db 0xf, 0xc7, 0xf0 ; rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r
77 jnc rn64_ret ; jmp if CF=0\r
78 mov edx, dword [esp + 4]\r
79 mov [edx], eax\r
80\r
81 db 0xf, 0xc7, 0xf0 ; generate another 32 bit RN\r
82 jnc rn64_ret ; jmp if CF=0\r
83 mov [edx + 4], eax\r
84\r
85 mov eax, 1\r
86 ret\r
87rn64_ret:\r
88 xor eax, eax\r
89 ret ; return with failure status\r
90\r