Skip to content

Commit 78e3f7c

Browse files
committed
[PACKAGE:RANDGEN] Use PF_RDRAND_INSTRUCTION_AVAILABLE to check rdrand availability
1 parent 002d67e commit 78e3f7c

File tree

1 file changed

+9
-56
lines changed

1 file changed

+9
-56
lines changed

Source/Include/KNSoft/NDK/Package/RandGen.inl

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -112,59 +112,20 @@ Rand_RdRand16(
112112

113113
#endif /* _RAND_HAS_RDRAND_ */
114114

115-
static int g_iFallbackToSW = -1;
116-
117-
__forceinline
118-
LOGICAL
119-
_Rand_FallbackToSW(VOID)
120-
{
121-
if (g_iFallbackToSW != -1)
122-
{
123-
return !!g_iFallbackToSW;
124-
}
125-
126-
/*
127-
* RDRAND is not emulated on WoA yet,
128-
* and SharedUserData->NativeProcessorArchitecture is avaliable since NT 6.2.
129-
*/
130-
if (SharedUserData->NtMajorVersion < 10)
131-
{
132-
/* WoA since NT 10 */
133-
g_iFallbackToSW = FALSE;
134-
} else if (NtReadTebPVOID(WowTebOffset) == NULL)
135-
{
136-
/* Not in WOW */
137-
g_iFallbackToSW = FALSE;
138-
} else if (SharedUserData->NativeProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
139-
SharedUserData->NativeProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
140-
{
141-
/* RDRAND is supported natively */
142-
g_iFallbackToSW = FALSE;
143-
} else
144-
{
145-
g_iFallbackToSW = TRUE;
146-
}
147-
return !!g_iFallbackToSW;
148-
}
149-
150115
__forceinline
151116
_Success_(return != FALSE)
152117
LOGICAL
153118
Rand_HW32(
154119
_Out_ unsigned __int32* Random)
155120
{
156121
#if _RAND_HAS_RDRAND_
157-
if (_Rand_FallbackToSW())
158-
{
159-
#endif
160-
*Random = Rand_SW32();
161-
return TRUE;
162-
#if _RAND_HAS_RDRAND_
163-
} else
122+
if (SharedUserData->ProcessorFeatures[PF_RDRAND_INSTRUCTION_AVAILABLE])
164123
{
165124
return Rand_RdRand32(Random);
166125
}
167126
#endif
127+
*Random = Rand_SW32();
128+
return TRUE;
168129
}
169130

170131
__forceinline
@@ -174,17 +135,13 @@ Rand_HW64(
174135
_Out_ unsigned __int64* Random)
175136
{
176137
#if _RAND_HAS_RDRAND_
177-
if (_Rand_FallbackToSW())
178-
{
179-
#endif
180-
*Random = Rand_SW64();
181-
return TRUE;
182-
#if _RAND_HAS_RDRAND_
183-
} else
138+
if (SharedUserData->ProcessorFeatures[PF_RDRAND_INSTRUCTION_AVAILABLE])
184139
{
185140
return Rand_RdRand64(Random);
186141
}
187142
#endif
143+
*Random = Rand_SW64();
144+
return TRUE;
188145
}
189146

190147
__forceinline
@@ -194,17 +151,13 @@ Rand_HW16(
194151
_Out_ unsigned __int16* Random)
195152
{
196153
#if _RAND_HAS_RDRAND_
197-
if (_Rand_FallbackToSW())
198-
{
199-
#endif
200-
*Random = Rand_SW16();
201-
return TRUE;
202-
#if _RAND_HAS_RDRAND_
203-
} else
154+
if (SharedUserData->ProcessorFeatures[PF_RDRAND_INSTRUCTION_AVAILABLE])
204155
{
205156
return Rand_RdRand16(Random);
206157
}
207158
#endif
159+
*Random = Rand_SW16();
160+
return TRUE;
208161
}
209162

210163
__forceinline

0 commit comments

Comments
 (0)