forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-asm-x86.h
324 lines (280 loc) · 6.86 KB
/
core-asm-x86.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (C) 2023 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef CORE_ASM_X86_H
#define CORE_ASM_X86_H
#include "stress-ng.h"
#include "core-arch.h"
#if defined(STRESS_ARCH_X86)
#define stress_asm_x86_lock_add(ptr, inc) \
do { \
__asm__ __volatile__( \
"lock addl %1,%0" \
: "+m" (*ptr) \
: "ir" (inc)); \
} while (0)
#if defined(STRESS_ARCH_X86_32) && !NEED_GNUC(5, 0, 0) && defined(__PIC__)
#define stress_asm_x86_cpuid(a, b, c, d) \
do { \
__asm__ __volatile__ ( \
"cpuid\n" \
: "=a"(a), \
"=b"(b), \
"=c"(c), \
"=d"(d) \
: "0"(a),"2"(c)); \
} while (0)
#else
#define stress_asm_x86_cpuid(a, b, c, d) \
do { \
__asm__ __volatile__ ( \
"cpuid\n" \
: "=a"(a), \
"=b"(b), \
"=c"(c), \
"=d"(d) \
: "0"(a),"2"(c)); \
} while (0)
#endif
#if defined(HAVE_ASM_X86_PAUSE)
static inline void stress_asm_x86_pause(void)
{
__asm__ __volatile__("pause;\n" ::: "memory");
}
#endif
#if defined(HAVE_ASM_X86_SERIALIZE)
static inline void stress_asm_x86_serialize(void)
{
__asm__ __volatile__("serialize");
}
#endif
/*
* x86 read TSC
*/
static inline uint64_t stress_asm_x86_rdtsc(void)
{
#if defined(STRESS_ARCH_X86_64)
uint32_t lo, hi;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
#elif defined(STRESS_ARCH_X86_32)
uint64_t tsc;
__asm__ __volatile__("rdtsc" : "=A" (tsc));
return tsc;
#else
return 0;
#endif
}
#if defined(STRESS_ARCH_X86_64) && \
defined(HAVE_ASM_X86_RDRAND)
/*
* stress_asm_x86_rdrand()
* read 64 bit random value
*/
static inline uint64_t stress_asm_x86_rdrand(void)
{
uint64_t ret;
__asm__ __volatile__(
"1:;\n\
rdrand %0;\n\
jnc 1b;\n"
: "=r"(ret));
return ret;
}
#elif defined(STRESS_ARCH_X86_32) && \
defined(HAVE_ASM_X86_RDRAND)
/*
* stress_asm_x86_rdrand()
* read 2 x 32 bit random value
*/
static inline uint64_t stress_asm_x86_rdrand(void)
{
uint32_t ret;
uint64_t ret64;
__asm__ __volatile__("1:;\n\
rdrand %0;\n\
jnc 1b;\n":"=r"(ret));
ret64 = (uint64_t)ret << 32;
__asm__ __volatile__("1:;\n\
rdrand %0;\n\
jnc 1b;\n":"=r"(ret));
return ret64 | ret;
}
#endif
#if defined(STRESS_ARCH_X86_64) && \
defined(HAVE_ASM_X86_RDSEED)
/*
* stress_asm_x86_rdseed()
* read 64 bit random value
*/
static inline uint64_t stress_asm_x86_rdseed(void)
{
uint64_t ret;
__asm__ __volatile__(
"1:;\n\
rdseed %0;\n\
jnc 1b;\n"
: "=r"(ret));
return ret;
}
#elif defined(STRESS_ARCH_X86_32) && \
defined(HAVE_ASM_X86_RDSEED)
/*
* stress_asm_x86_rdseed()
* read 2 x 32 bit random value
*/
static inline uint64_t stress_asm_x86_rdseed(void)
{
uint32_t ret;
uint64_t ret64;
__asm__ __volatile__(
"1:;\n\
rdseed %0;\n\
jnc 1b;\n"
: "=r"(ret));
ret64 = (uint64_t)ret << 32;
__asm__ __volatile__(
"1:;\n\
rdseed %0;\n\
jnc 1b;\n"
: "=r"(ret));
return ret64 | ret;
}
#endif
/* #if defined(STRESS_ARCH_X86) */
#endif
#if defined(HAVE_ASM_X86_TPAUSE) && \
!defined(HAVE_COMPILER_PCC)
static inline int stress_asm_x86_tpause__(int state, uint32_t hi, uint32_t lo)
{
uint8_t cflags;
__asm__ __volatile__(
"mov %1, %%edx;\n"
"mov %2, %%eax;\n"
"mov %3, %%edi;\n"
".byte 0x66,0x0f,0xae,0xf7;\n" /* tpause %%edi; */
"setb %0;\n"
: "=r" (cflags)
: "r" (hi), "r" (lo), "r"(state)
: "cc", "eax", "edx", "edi");
return cflags;
}
static inline int stress_asm_x86_tpause(const int state, const uint64_t delay)
{
register uint32_t lo = delay & 0xffffffff;
register uint32_t hi = (uint32_t)(delay >> 32);
return stress_asm_x86_tpause__(state, hi, lo);
}
#endif
#if defined(HAVE_ASM_X86_CLFLUSH)
static inline void stress_asm_x86_clflush(volatile void *p)
{
__asm__ __volatile__("clflush (%0)\n" : : "r"(p) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_CLFLUSHOPT)
static inline void stress_asm_x86_clflushopt(void *p)
{
__asm__ __volatile__("clflushopt (%0)\n" : : "r"(p) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_CLDEMOTE)
static inline void stress_asm_x86_cldemote(void *p)
{
__asm__ __volatile__("cldemote (%0)\n" : : "r"(p) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_CLWB)
static inline void stress_asm_x86_clwb(void *p)
{
__asm__ __volatile__("clwb (%0)\n" : : "r"(p) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_LFENCE)
static inline void stress_asm_x86_lfence(void)
{
__asm__ __volatile__("lfence" : : : "memory");
}
#endif
#if defined(HAVE_ASM_X86_MFENCE)
static inline void stress_asm_x86_mfence(void)
{
__asm__ __volatile__("mfence" : : : "memory");
}
#endif
#if defined(HAVE_ASM_X86_SFENCE)
static inline void stress_asm_x86_sfence(void)
{
__asm__ __volatile__("sfence" : : : "memory");
}
#endif
#if defined(HAVE_ASM_X86_PREFETCHT0)
static inline void stress_asm_x86_prefetcht0(void *p)
{
__asm__ __volatile__("prefetcht0 (%0)\n" : : "r"(p) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_PREFETCHT1)
static inline void stress_asm_x86_prefetcht1(void *p)
{
__asm__ __volatile__("prefetcht1 (%0)\n" : : "r"(p) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_PREFETCHT2)
static inline void stress_asm_x86_prefetcht2(void *p)
{
__asm__ __volatile__("prefetcht2 (%0)\n" : : "r"(p) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_PREFETCHNTA)
static inline void stress_asm_x86_prefetchnta(void *p)
{
__asm__ __volatile__("prefetchnta (%0)\n" : : "r"(p) : "memory");
}
#endif
#if !defined(HAVE_COMPILER_PCC) && \
defined(HAVE_ARCH_X86_64)
static inline int stress_asm_x86_umwait__(int state, uint32_t hi, uint32_t lo)
{
uint8_t cflags;
__asm__ __volatile__(
"mov %1, %%edx;\n"
"mov %2, %%eax;\n"
"mov %3, %%edi;\n"
".byte 0xf2, 0x0f, 0xae, 0xf7;\n" /* umwait %edi */
"setb %0;\n"
: "=r" (cflags)
: "r" (hi), "r" (lo), "r"(state)
: "cc", "eax", "edx", "edi");
return cflags;
}
static inline int stress_asm_x86_umwait(const int state, const uint64_t delay)
{
register uint32_t lo = delay & 0xffffffff;
register uint32_t hi = (uint32_t)(delay >> 32);
return stress_asm_x86_umwait__(state, hi, lo);
}
static inline void stress_asm_x86_umonitor(void *addr)
{
__asm__ __volatile__("mov %0, %%rdi\t\n"
".byte 0xf3, 0x0f, 0xae, 0xf7\t\n"
: : "r" (addr));
}
#endif
/* #ifndef CORE_ASM_X86_H */
#endif