]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/RdRand.S
MdePkg BaseLib: Convert Ia32/ReadFs.asm to NASM
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / RdRand.S
CommitLineData
3cfc7813
QL
1#------------------------------------------------------------------------------ ;\r
2# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
3# This program and the accompanying materials\r
4# are licensed and made available under the terms and conditions of the BSD License\r
5# which accompanies this distribution. The full text of the license may be found at\r
6# http://opensource.org/licenses/bsd-license.php.\r
7#\r
8# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
9# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
10#\r
11# Module Name:\r
12#\r
13# RdRand.S\r
14#\r
15# Abstract:\r
16#\r
17# Generates random number through CPU RdRand instruction under 32-bit platform.\r
18#\r
19# Notes:\r
20#\r
21#------------------------------------------------------------------------------\r
22\r
23//------------------------------------------------------------------------------\r
24// Generates a 16 bit random number through RDRAND instruction.\r
25// Return TRUE if Rand generated successfully, or FALSE if not.\r
26//\r
27// BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);\r
28//------------------------------------------------------------------------------\r
29ASM_GLOBAL ASM_PFX(AsmRdRand16)\r
30ASM_PFX(AsmRdRand16):\r
31 .byte 0x0f, 0xc7, 0xf0 // rdrand r16: "0f c7 /6 ModRM:r/m(w)"\r
32 jc rn16_ok // jmp if CF=1\r
33 xor %eax, %eax // reg=0 if CF=0\r
34 ret // return with failure status\r
35rn16_ok:\r
36 mov 0x4(%esp), %edx\r
37 mov %ax, (%edx)\r
38 mov $0x1, %eax\r
39 ret\r
40\r
41//------------------------------------------------------------------------------\r
42// Generates a 32 bit random number through RDRAND instruction.\r
43// Return TRUE if Rand generated successfully, or FALSE if not.\r
44//\r
45// BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);\r
46//------------------------------------------------------------------------------\r
47ASM_GLOBAL ASM_PFX(AsmRdRand32)\r
48ASM_PFX(AsmRdRand32):\r
49 .byte 0x0f, 0xc7, 0xf0 // rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r
50 jc rn32_ok // jmp if CF=1\r
51 xor %eax, %eax // reg=0 if CF=0\r
52 ret // return with failure status\r
53rn32_ok:\r
54 mov 0x4(%esp), %edx\r
55 mov %eax, (%edx)\r
56 mov $0x1, %eax\r
57 ret\r
58\r
59//------------------------------------------------------------------------------\r
60// Generates a 64 bit random number through RDRAND instruction.\r
61// Return TRUE if Rand generated successfully, or FALSE if not.\r
62//\r
63// BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Rand);\r
64//------------------------------------------------------------------------------\r
65ASM_GLOBAL ASM_PFX(AsmRdRand64)\r
66ASM_PFX(AsmRdRand64):\r
67 .byte 0x0f, 0xc7, 0xf0 // rdrand r32: "0f c7 /6 ModRM:r/m(w)"\r
68 jnc rn64_ret // jmp if CF=0\r
69 mov 0x4(%esp), %edx\r
70 mov %eax, (%edx)\r
71\r
72 .byte 0x0f, 0xc7, 0xf0 // generate another 32 bit RN\r
73 jnc rn64_ret // jmp if CF=0\r
74 mov %eax, 0x4(%edx)\r
75\r
76 mov $0x1, %eax\r
77 ret\r
78rn64_ret:\r
79 xor %eax, %eax\r
80 ret // return with failure status\r