Commit | Line | Data |
---|---|---|
a3ad4421 MW |
1 | /// -*- mode: asm; asm-comment-char: ?/ -*- |
2 | /// | |
3 | /// Random-number support for x86 | |
4 | /// | |
5 | /// (c) 2019 Straylight/Edgeware | |
6 | /// | |
7 | ||
8 | ///----- Licensing notice --------------------------------------------------- | |
9 | /// | |
10 | /// This file is part of Catacomb. | |
11 | /// | |
12 | /// Catacomb is free software: you can redistribute it and/or modify it | |
13 | /// under the terms of the GNU Library General Public License as published | |
14 | /// by the Free Software Foundation; either version 2 of the License, or | |
15 | /// (at your option) any later version. | |
16 | /// | |
17 | /// Catacomb is distributed in the hope that it will be useful, but | |
18 | /// WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | /// Library General Public License for more details. | |
21 | /// | |
22 | /// You should have received a copy of the GNU Library General Public | |
23 | /// License along with Catacomb. If not, write to the Free Software | |
24 | /// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
25 | /// USA. | |
26 | ||
27 | ///-------------------------------------------------------------------------- | |
28 | /// Preliminaries. | |
29 | ||
30 | #include "config.h" | |
31 | #include "asm-common.h" | |
32 | ||
33 | .extern F(rand_add) | |
34 | ||
35 | .text | |
36 | ||
37 | ///-------------------------------------------------------------------------- | |
38 | /// Quick random generation. | |
39 | ||
40 | FUNC(rand_quick_x86ish_rdrand) | |
41 | // Enter with a pointer to the random context in the first argument. | |
42 | // Return zero on success, or -1 on error. | |
43 | ||
44 | #if CPUFAM_X86 | |
a90d420c | 45 | mov edx, [SP + 4] |
a3ad4421 MW |
46 | stalloc 28 |
47 | # define COUNT ecx | |
48 | #endif | |
49 | #if CPUFAM_AMD64 && ABI_SYSV | |
50 | stalloc 8 | |
51 | # define COUNT ecx | |
52 | #endif | |
53 | #if CPUFAM_AMD64 && ABI_WIN | |
54 | stalloc 40 | |
55 | # define COUNT r8d | |
56 | #endif | |
57 | endprologue | |
58 | ||
59 | // Try to fetch a random number. | |
60 | mov COUNT, 16 | |
a90d420c | 61 | 0: rdrand AX |
a3ad4421 MW |
62 | jc 1f |
63 | dec COUNT | |
64 | jnz 0b | |
65 | ||
66 | // Failed. | |
67 | mov eax, -1 | |
68 | jmp 9f | |
69 | ||
70 | // Success. | |
71 | 1: | |
72 | #if CPUFAM_X86 | |
a90d420c MW |
73 | mov [SP + 16], AX |
74 | lea ecx, [SP + 16] | |
75 | mov dword ptr [SP + 12], 32 | |
76 | mov dword ptr [SP + 8], 4 | |
77 | mov [SP + 4], ecx | |
78 | mov [SP + 0], edx | |
a3ad4421 MW |
79 | #endif |
80 | #if CPUFAM_AMD64 && ABI_SYSV | |
a90d420c MW |
81 | mov [SP + 0], AX |
82 | mov rsi, SP | |
a3ad4421 MW |
83 | mov edx, 8 |
84 | mov ecx, 64 | |
85 | #endif | |
86 | #if CPUFAM_AMD64 && ABI_WIN | |
a90d420c MW |
87 | mov [SP + 32], AX |
88 | lea rdx, [SP + 32] | |
a3ad4421 MW |
89 | mov r8d, 8 |
90 | mov r9d, 64 | |
91 | #endif | |
92 | callext F(rand_add) | |
93 | xor eax, eax | |
94 | ||
95 | // Done. | |
96 | 9: | |
97 | #if CPUFAM_X86 | |
98 | stfree 28 | |
99 | #endif | |
100 | #if CPUFAM_AMD64 && ABI_SYSV | |
101 | stfree 8 | |
102 | #endif | |
103 | #if CPUFAM_AMD64 && ABI_WIN | |
104 | stfree 40 | |
105 | #endif | |
106 | ret | |
107 | ENDFUNC | |
108 | ||
109 | ///----- That's all, folks -------------------------------------------------- |