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
|
/* This is a test program that checks the parsing of instructions with
xacquire and xrelease prefixes. The tested insns are, afaics,
exactly those listed in the Intel description for the two prefixes
("XACQUIRE/XRELEASE -- Hardware Lock Elision Prefix Hints"). */
#include <stdio.h>
typedef unsigned long long int ULong;
#define CAT2(_x,_y) _x##_y
#define CAT(_x,_y) CAT2(_x,_y)
#define GEN_BINARY(_insn) \
void CAT(do_,_insn) ( void ) \
{ \
volatile ULong n = 0x5555555555555555ULL; \
ULong some = 0x271831415927D459ULL; \
__asm__ __volatile__( \
"\t" \
"stc" "\n\t" \
"xacquire lock " #_insn "q $123456789, (%0)" "\n\t" \
"xrelease lock " #_insn "q $123456789, (%0)" "\n\t" \
"xacquire lock " #_insn "l $0x12345FE, (%0)" "\n\t" \
"xrelease lock " #_insn "l $0x12345FE, (%0)" "\n\t" \
"xacquire lock " #_insn "w $0x9876, (%0)" "\n\t" \
"xrelease lock " #_insn "w $0x9876, (%0)" "\n\t" \
"xacquire lock " #_insn "b $0x45, (%0)" "\n\t" \
"xrelease lock " #_insn "b $0x45, (%0)" "\n\t" \
"xacquire lock " #_insn "q %1, (%0)" "\n\t" \
"xrelease lock " #_insn "q %1, (%0)" "\n\t" \
"xacquire lock " #_insn "l %k1, (%0)" "\n\t" \
"xrelease lock " #_insn "l %k1, (%0)" "\n\t" \
"xacquire lock " #_insn "w %w1, (%0)" "\n\t" \
"xrelease lock " #_insn "w %w1, (%0)" "\n\t" \
"xacquire lock " #_insn "b %b1, (%0)" "\n\t" \
"xrelease lock " #_insn "b %b1, (%0)" "\n\t" \
: : "r"(&n), "r"(some) : "cc", "memory" \
); \
printf("result for '%-3s' is %016llx\n", #_insn, n); \
}
GEN_BINARY(add)
GEN_BINARY(adc)
GEN_BINARY(and)
GEN_BINARY(or)
GEN_BINARY(sbb)
GEN_BINARY(sub)
GEN_BINARY(xor)
#define GEN_UNARY(_insn) \
void CAT(do_,_insn) ( void ) \
{ \
volatile ULong n = 0x5555555555555555ULL; \
__asm__ __volatile__( \
"\t" \
"stc" "\n\t" \
"xacquire lock " #_insn "q (%0)" "\n\t" \
"xrelease lock " #_insn "q (%0)" "\n\t" \
"xacquire lock " #_insn "l (%0)" "\n\t" \
"xrelease lock " #_insn "l (%0)" "\n\t" \
"xacquire lock " #_insn "w (%0)" "\n\t" \
"xrelease lock " #_insn "w (%0)" "\n\t" \
"xacquire lock " #_insn "b (%0)" "\n\t" \
"xrelease lock " #_insn "b (%0)" "\n\t" \
: : "r"(&n) : "cc", "memory" \
); \
printf("result for '%-3s' is %016llx\n", #_insn, n); \
}
GEN_UNARY(dec)
GEN_UNARY(inc)
GEN_UNARY(neg)
GEN_UNARY(not)
void do_btc ( void )
{
volatile ULong n = 0x5555555555555555ULL;
__asm__ __volatile__(
"xacquire lock btcq %1, (%0)" "\n\t"
"xacquire lock btcq $57, (%0)" "\n\t"
"xrelease lock btcq %1, (%0)" "\n\t"
"xrelease lock btcq $55, (%0)" "\n\t"
"xacquire lock btcl %k1, (%0)" "\n\t"
"xacquire lock btcl $27, (%0)" "\n\t"
"xrelease lock btcl %k1, (%0)" "\n\t"
"xrelease lock btcl $25, (%0)" "\n\t"
"xacquire lock btcw %w1, (%0)" "\n\t"
"xacquire lock btcw $12, (%0)" "\n\t"
"xrelease lock btcw %w1, (%0)" "\n\t"
"xrelease lock btcw $11, (%0)" "\n\t"
: : "r"(&n), "r"(6ULL) : "cc", "memory"
);
printf("result for '%-3s' is %016llx\n", "btc", n); \
}
void do_btr ( void )
{
volatile ULong n = 0x5555555555555555ULL;
__asm__ __volatile__(
"xacquire lock btrq %1, (%0)" "\n\t"
"xacquire lock btrq $57, (%0)" "\n\t"
"xrelease lock btrq %1, (%0)" "\n\t"
"xrelease lock btrq $55, (%0)" "\n\t"
"xacquire lock btrl %k1, (%0)" "\n\t"
"xacquire lock btrl $27, (%0)" "\n\t"
"xrelease lock btrl %k1, (%0)" "\n\t"
"xrelease lock btrl $25, (%0)" "\n\t"
"xacquire lock btrw %w1, (%0)" "\n\t"
"xacquire lock btrw $12, (%0)" "\n\t"
"xrelease lock btrw %w1, (%0)" "\n\t"
"xrelease lock btrw $11, (%0)" "\n\t"
: : "r"(&n), "r"(6ULL) : "cc", "memory"
);
printf("result for '%-3s' is %016llx\n", "btr", n); \
}
void do_bts ( void )
{
volatile ULong n = 0x5555555555555555ULL;
__asm__ __volatile__(
"xacquire lock btsq %1, (%0)" "\n\t"
"xacquire lock btsq $57, (%0)" "\n\t"
"xrelease lock btsq %1, (%0)" "\n\t"
"xrelease lock btsq $55, (%0)" "\n\t"
"xacquire lock btsl %k1, (%0)" "\n\t"
"xacquire lock btsl $27, (%0)" "\n\t"
"xrelease lock btsl %k1, (%0)" "\n\t"
"xrelease lock btsl $25, (%0)" "\n\t"
"xacquire lock btsw %w1, (%0)" "\n\t"
"xacquire lock btsw $12, (%0)" "\n\t"
"xrelease lock btsw %w1, (%0)" "\n\t"
"xrelease lock btsw $11, (%0)" "\n\t"
: : "r"(&n), "r"(6ULL) : "cc", "memory"
);
printf("result for '%-3s' is %016llx\n", "bts", n); \
}
void do_cmpxchg ( void )
{
volatile ULong n = 0x5555555555555555ULL;
ULong some = 0x271831415927D459ULL;
__asm__ __volatile__(
"\t"
"stc" "\n\t"
// zero out rax and get the flags in a known state
"xorq %%rax, %%rax" "\n\t"
"xacquire lock cmpxchgq %1, (%0)" "\n\t"
"xrelease lock cmpxchgq %1, (%0)" "\n\t"
"xacquire lock cmpxchgl %k1, (%0)" "\n\t"
"xrelease lock cmpxchgl %k1, (%0)" "\n\t"
"xacquire lock cmpxchgw %w1, (%0)" "\n\t"
"xrelease lock cmpxchgw %w1, (%0)" "\n\t"
"xacquire lock cmpxchgb %b1, (%0)" "\n\t"
"xrelease lock cmpxchgb %b1, (%0)" "\n\t"
: : "r"(&n), "r"(some) : "cc", "memory", "rax"
);
printf("result for '%-3s' is %016llx\n", "cmpxchg", n);
}
void do_cmpxchg8b ( void )
{
volatile ULong n = 0x5555555555555555ULL;
__asm__ __volatile__(
"xorq %%rax, %%rax" "\n\t"
"xorq %%rdx, %%rdx" "\n\t"
"movabsq $0x1122334455667788, %%rcx" "\n\t"
"movabsq $0xffeeddccbbaa9988, %%rbx" "\n\t"
"xacquire lock cmpxchg8b (%0)" "\n\t"
"xrelease lock cmpxchg8b (%0)" "\n\t"
: : "r"(&n) : "cc", "memory", "rax", "rbx", "rcx", "rdx"
);
printf("result for '%-3s' is %016llx\n", "cmpxchg8b", n);
}
void do_xadd ( void )
{
volatile ULong n = 0x5555555555555555ULL;
ULong some = 0x271831415927D459ULL;
__asm__ __volatile__(
"\t"
"stc" "\n\t"
// zero out rax and get the flags in a known state
"xorq %%rax, %%rax" "\n\t"
"xacquire lock xaddq %1, (%0)" "\n\t"
"xrelease lock xaddq %1, (%0)" "\n\t"
"xacquire lock xaddl %k1, (%0)" "\n\t"
"xrelease lock xaddl %k1, (%0)" "\n\t"
"xacquire lock xaddw %w1, (%0)" "\n\t"
"xrelease lock xaddw %w1, (%0)" "\n\t"
"xacquire lock xaddb %b1, (%0)" "\n\t"
"xrelease lock xaddb %b1, (%0)" "\n\t"
: : "r"(&n), "r"(some) : "cc", "memory", "rax"
// not sure this constraint string is really correct, since %1
// is written as well as read, in this case. But I can't figure
// out how to tell gcc that.
);
printf("result for '%-3s' is %016llx\n", "xadd", n);
}
void do_xchg ( void )
{
volatile ULong n = 0x5555555555555555ULL;
ULong some = 0x271831415927D459ULL;
__asm__ __volatile__(
"\t"
"stc" "\n\t"
// zero out rax and get the flags in a known state
"xorq %%rax, %%rax" "\n\t"
"xacquire lock xchgq %1, (%0)" "\n\t"
"xrelease lock xchgq %1, (%0)" "\n\t"
"xacquire lock xchgl %k1, (%0)" "\n\t"
"xrelease lock xchgl %k1, (%0)" "\n\t"
"xacquire lock xchgw %w1, (%0)" "\n\t"
"xrelease lock xchgw %w1, (%0)" "\n\t"
"xacquire lock xchgb %b1, (%0)" "\n\t"
"xrelease lock xchgb %b1, (%0)" "\n\t"
: : "r"(&n), "r"(some) : "cc", "memory", "rax"
// not sure this constraint string is really correct, since %1
// is written as well as read, in this case. But I can't figure
// out how to tell gcc that.
);
printf("result for '%-3s' is %016llx\n", "xchg", n);
}
void do_xchg_no_lock ( void )
{
volatile ULong n = 0x5555555555555555ULL;
ULong some = 0x271831415927D459ULL;
__asm__ __volatile__(
"\t"
"stc" "\n\t"
// zero out rax and get the flags in a known state
"xorq %%rax, %%rax" "\n\t"
"xacquire xchgq %1, (%0)" "\n\t"
"xrelease xchgq %1, (%0)" "\n\t"
"xacquire xchgl %k1, (%0)" "\n\t"
"xrelease xchgl %k1, (%0)" "\n\t"
"xacquire xchgw %w1, (%0)" "\n\t"
"xrelease xchgw %w1, (%0)" "\n\t"
"xacquire xchgb %b1, (%0)" "\n\t"
"xrelease xchgb %b1, (%0)" "\n\t"
: : "r"(&n), "r"(some) : "cc", "memory", "rax"
// not sure this constraint string is really correct, since %1
// is written as well as read, in this case. But I can't figure
// out how to tell gcc that.
);
printf("result for '%-3s' is %016llx\n", "xchg-no-lock", n);
}
void do_mov ( void )
{
// According to the Intel docs, we only need to allow xrelease here.
volatile ULong n = 0x5555555555555555ULL;
ULong some = 0x271831415927D459ULL;
__asm__ __volatile__(
"\t"
"xrelease movq %1, 0(%0)" "\n\t"
"xrelease movl %k1, 1(%0)" "\n\t"
"xrelease movw %w1, 3(%0)" "\n\t"
"xrelease movb %b1, 7(%0)" "\n\t"
: : "r"(&n), "r"(some) : "cc", "memory"
);
printf("result for '%-3s' is %016llx\n", "mov-reg", n);
n = 0xAAAAAAAAAAAAAAAAULL;
__asm__ __volatile__(
"\t"
"xrelease movq $-0x79876543, 0(%0)" "\n\t"
"xrelease movl $0xEFCDAB89, 1(%0)" "\n\t"
"xrelease movw $0xF00D, 3(%0)" "\n\t"
"xrelease movb $0x42, 7(%0)" "\n\t"
: : "r"(&n) : "cc", "memory"
);
printf("result for '%-3s' is %016llx\n", "mov-imm", n);
}
int main ( void )
{
do_add();
do_adc();
do_and();
do_or();
do_sbb();
do_sub();
do_xor();
do_dec();
do_inc();
do_neg();
do_not();
do_btc();
do_btr();
do_bts();
do_cmpxchg();
do_cmpxchg8b();
do_xadd();
do_xchg();
do_xchg_no_lock();
do_mov();
return 0;
}
|