]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/RdRand.nasm
MdePkg: Add helper functions for Tdx guest in BaseIoLibIntrinsic
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / RdRand.nasm
CommitLineData
55745c24
JJ
1;------------------------------------------------------------------------------\r
2;\r
d3febfd9 3; Copyright (c) 2015 - 2022, Intel Corporation. All rights reserved.<BR>\r
9344f092 4; SPDX-License-Identifier: BSD-2-Clause-Patent\r
55745c24
JJ
5;\r
6; Module Name:\r
7;\r
8; RdRand.nasm\r
9;\r
10; Abstract:\r
11;\r
12; Generates random number through CPU RdRand instruction under 32-bit platform.\r
13;\r
14; Notes:\r
15;\r
16;------------------------------------------------------------------------------\r
17\r
18SECTION .text\r
19\r
20;------------------------------------------------------------------------------\r
21; Generates a 16 bit random number through RDRAND instruction.\r
22; Return TRUE if Rand generated successfully, or FALSE if not.\r
23;\r
9ec9a7a5 24; BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand);\r
55745c24 25;------------------------------------------------------------------------------\r
9ec9a7a5
QL
26global ASM_PFX(InternalX86RdRand16)\r
27ASM_PFX(InternalX86RdRand16):\r
d3febfd9 28 rdrand eax ; generate a 16 bit RN into ax\r
55745c24 29 ; CF=1 if RN generated ok, otherwise CF=0\r
55745c24
JJ
30 jc rn16_ok ; jmp if CF=1\r
31 xor eax, eax ; reg=0 if CF=0\r
32 ret ; return with failure status\r
33rn16_ok:\r
34 mov edx, dword [esp + 4]\r
35 mov [edx], ax\r
36 mov eax, 1\r
37 ret\r
38\r
39;------------------------------------------------------------------------------\r
40; Generates a 32 bit random number through RDRAND instruction.\r
41; Return TRUE if Rand generated successfully, or FALSE if not.\r
42;\r
9ec9a7a5 43; BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand);\r
55745c24 44;------------------------------------------------------------------------------\r
9ec9a7a5
QL
45global ASM_PFX(InternalX86RdRand32)\r
46ASM_PFX(InternalX86RdRand32):\r
d3febfd9 47 rdrand eax ; generate a 32 bit RN into eax\r
55745c24 48 ; CF=1 if RN generated ok, otherwise CF=0\r
55745c24
JJ
49 jc rn32_ok ; jmp if CF=1\r
50 xor eax, eax ; reg=0 if CF=0\r
51 ret ; return with failure status\r
52rn32_ok:\r
53 mov edx, dword [esp + 4]\r
54 mov [edx], eax\r
55 mov eax, 1\r
56 ret\r
57\r
58;------------------------------------------------------------------------------\r
59; Generates a 64 bit random number through RDRAND instruction.\r
60; Return TRUE if Rand generated successfully, or FALSE if not.\r
61;\r
9ec9a7a5 62; BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand);\r
55745c24 63;------------------------------------------------------------------------------\r
9ec9a7a5
QL
64global ASM_PFX(InternalX86RdRand64)\r
65ASM_PFX(InternalX86RdRand64):\r
d3febfd9 66 rdrand eax ; generate a 32 bit RN into eax\r
55745c24 67 ; CF=1 if RN generated ok, otherwise CF=0\r
55745c24
JJ
68 jnc rn64_ret ; jmp if CF=0\r
69 mov edx, dword [esp + 4]\r
70 mov [edx], eax\r
71\r
d3febfd9 72 rdrand eax ; generate another 32 bit RN\r
55745c24
JJ
73 jnc rn64_ret ; jmp if CF=0\r
74 mov [edx + 4], eax\r
75\r
76 mov eax, 1\r
77 ret\r
78rn64_ret:\r
79 xor eax, eax\r
80 ret ; return with failure status\r
81\r