HyperDbg Debugger
Loading...
Searching...
No Matches
platform-intrinsics.c File Reference

Implementation of cross platform APIs for intrinsic functions (x86 instructions). More...

#include "pch.h"

Functions

VOID CpuCpuId (INT32 *CpuInfo, INT32 FunctionId)
 Execute CPUID.
VOID CpuCpuIdEx (INT32 *CpuInfo, INT32 FunctionId, INT32 SubFunctionId)
 Execute CPUID with sub-leaf.
UINT64 CpuReadTsc (VOID)
 Read Time-Stamp Counter.
UINT64 CpuReadTscp (UINT32 *Aux)
 Read Time-Stamp Counter (serializing).
VOID CpuPause (VOID)
 Execute PAUSE (spin-wait hint).
INT64 CpuInterlockedExchange64 (INT64 volatile *Target, INT64 Value)
 Atomic 64-bit exchange.
INT64 CpuInterlockedExchangeAdd64 (INT64 volatile *Addend, INT64 Value)
 Atomic 64-bit exchange-add.
INT64 CpuInterlockedIncrement64 (INT64 volatile *Addend)
 Atomic 64-bit increment.
INT64 CpuInterlockedDecrement64 (INT64 volatile *Addend)
 Atomic 64-bit decrement.
INT64 CpuInterlockedCompareExchange64 (INT64 volatile *Destination, INT64 ExChange, INT64 Comparand)
 Atomic 64-bit compare-exchange.
UCHAR CpuInterlockedBitTestAndSet (volatile LONG *Base, LONG Bit)

Detailed Description

Implementation of cross platform APIs for intrinsic functions (x86 instructions).

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.19
Date
2026-05-06

Function Documentation

◆ CpuCpuId()

VOID CpuCpuId ( INT32 * CpuInfo,
INT32 FunctionId )
inline

Execute CPUID.

Parameters
CpuInfo
FunctionId
30{
31#if defined(_WIN32) || defined(_WIN64)
32 __cpuid(CpuInfo, FunctionId);
33#elif defined(__linux__)
34 __asm__ __volatile__("cpuid" : "=a"(CpuInfo[0]), "=b"(CpuInfo[1]), "=c"(CpuInfo[2]), "=d"(CpuInfo[3]) : "a"(FunctionId), "c"(0));
35#else
36# error "Unsupported platform"
37#endif
38}

◆ CpuCpuIdEx()

VOID CpuCpuIdEx ( INT32 * CpuInfo,
INT32 FunctionId,
INT32 SubFunctionId )
inline

Execute CPUID with sub-leaf.

Parameters
CpuInfo
FunctionId
SubFunctionId
49{
50#if defined(_WIN32) || defined(_WIN64)
51 __cpuidex(CpuInfo, FunctionId, SubFunctionId);
52#elif defined(__linux__)
53 __asm__ __volatile__("cpuid" : "=a"(CpuInfo[0]), "=b"(CpuInfo[1]), "=c"(CpuInfo[2]), "=d"(CpuInfo[3]) : "a"(FunctionId), "c"(SubFunctionId));
54#else
55# error "Unsupported platform"
56#endif
57}

◆ CpuInterlockedBitTestAndSet()

UCHAR CpuInterlockedBitTestAndSet ( volatile LONG * Base,
LONG Bit )
191{
192#if defined(_WIN32) || defined(_WIN64)
193 return _interlockedbittestandset(Base, Bit);
194#elif defined(__linux__)
195 LONG Mask = (1L << Bit);
196 LONG Old = __atomic_fetch_or(Base, Mask, __ATOMIC_SEQ_CST);
197 return (UCHAR)((Old >> Bit) & 1);
198#else
199# error "Unsupported platform"
200#endif
201}
long LONG
Definition BasicTypes.h:28
unsigned char UCHAR
Definition BasicTypes.h:34

◆ CpuInterlockedCompareExchange64()

INT64 CpuInterlockedCompareExchange64 ( INT64 volatile * Destination,
INT64 ExChange,
INT64 Comparand )
inline

Atomic 64-bit compare-exchange.

177{
178#if defined(_WIN32) || defined(_WIN64)
179 return InterlockedCompareExchange64(Destination, ExChange, Comparand);
180#elif defined(__linux__)
181 INT64 Expected = Comparand;
182 __atomic_compare_exchange_n(Destination, &Expected, ExChange, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
183 return Expected;
184#else
185# error "Unsupported platform"
186#endif
187}

◆ CpuInterlockedDecrement64()

INT64 CpuInterlockedDecrement64 ( INT64 volatile * Addend)
inline

Atomic 64-bit decrement.

165{
166#if defined(_WIN32) || defined(_WIN64)
167 return InterlockedDecrement64(Addend);
168#elif defined(__linux__)
169 return __atomic_sub_fetch(Addend, 1LL, __ATOMIC_SEQ_CST);
170#else
171# error "Unsupported platform"
172#endif
173}

◆ CpuInterlockedExchange64()

INT64 CpuInterlockedExchange64 ( INT64 volatile * Target,
INT64 Value )
inline

Atomic 64-bit exchange.

129{
130#if defined(_WIN32) || defined(_WIN64)
131 return InterlockedExchange64(Target, Value);
132#elif defined(__linux__)
133 return __atomic_exchange_n(Target, Value, __ATOMIC_SEQ_CST);
134#else
135# error "Unsupported platform"
136#endif
137}
RequestedActionOfThePacket Value(0x1) 00000000

◆ CpuInterlockedExchangeAdd64()

INT64 CpuInterlockedExchangeAdd64 ( INT64 volatile * Addend,
INT64 Value )
inline

Atomic 64-bit exchange-add.

141{
142#if defined(_WIN32) || defined(_WIN64)
143 return InterlockedExchangeAdd64(Addend, Value);
144#elif defined(__linux__)
145 return __atomic_fetch_add(Addend, Value, __ATOMIC_SEQ_CST);
146#else
147# error "Unsupported platform"
148#endif
149}

◆ CpuInterlockedIncrement64()

INT64 CpuInterlockedIncrement64 ( INT64 volatile * Addend)
inline

Atomic 64-bit increment.

153{
154#if defined(_WIN32) || defined(_WIN64)
155 return InterlockedIncrement64(Addend);
156#elif defined(__linux__)
157 return __atomic_add_fetch(Addend, 1LL, __ATOMIC_SEQ_CST);
158#else
159# error "Unsupported platform"
160#endif
161}

◆ CpuPause()

VOID CpuPause ( VOID )
inline

Execute PAUSE (spin-wait hint).

113{
114#if defined(_WIN32) || defined(_WIN64)
115 _mm_pause();
116#elif defined(__linux__)
117 __asm__ __volatile__("pause");
118#else
119# error "Unsupported platform"
120#endif
121}

◆ CpuReadTsc()

UINT64 CpuReadTsc ( VOID )
inline

Read Time-Stamp Counter.

Returns
UINT64
70{
71#if defined(_WIN32) || defined(_WIN64)
72 return __rdtsc();
73#elif defined(__linux__)
74 UINT32 __lo, __hi;
75 __asm__ __volatile__("rdtsc" : "=a"(__lo), "=d"(__hi));
76 return ((UINT64)__hi << 32) | __lo;
77#else
78# error "Unsupported platform"
79#endif
80}
unsigned int UINT32
Definition BasicTypes.h:54

◆ CpuReadTscp()

UINT64 CpuReadTscp ( UINT32 * Aux)
inline

Read Time-Stamp Counter (serializing).

Read Time-Stamp Counter and Processor ID.

Parameters
Auxprocessor ID output (may be NULL)
Returns
UINT64
Parameters
Aux
Returns
UINT64
94{
95#if defined(_WIN32) || defined(_WIN64)
96 return __rdtscp(Aux);
97#elif defined(__linux__)
98 UINT32 __lo, __hi, __aux;
99 __asm__ __volatile__("rdtscp" : "=a"(__lo), "=d"(__hi), "=c"(__aux));
100 if (Aux)
101 *Aux = __aux;
102 return ((UINT64)__hi << 32) | __lo;
103#else
104# error "Unsupported platform"
105#endif
106}