1 /* This is an example of a program which does cavium atomic memory operations
2 between two processes which share a page. This test is based on :
3 memcheck/tests/atomic_incs.c */
11 #include "tests/sys_mman.h"
14 #define NNN 3456987 // Number of repetition.
17 int p1_expd
[N
] = { 2156643710, 2156643710, 3456986, 6913974,
18 4288053322, 0, 4294967295,
22 4294967295, 4288053323, // Test 14
23 4288053322, 4273190185, // Test 16
26 long long int p2_expd
[N
] = { 12633614303292, 12633614303292, 3555751, 6913974,
28 6913974, 23901514779351,
29 3456986, 11950752204196,
30 6913974, 23901514779351,
31 -1, -6913973, // Test 15
32 -6913974, -23901514779351, // Test 17
35 #define IS_8_ALIGNED(_ptr) (0 == (((unsigned long)(_ptr)) & 7))
37 __attribute__((noinline
)) void atomic_saa ( int* p
, int n
)
39 #if (_MIPS_ARCH_OCTEON2)
40 unsigned long block
[2] = { (unsigned long)p
, (unsigned long)n
};
43 "ld $t1, 0($t0)" "\n\t" // p
44 "ld $t2, 8($t0)" "\n\t" // n
45 "saa $t2, ($t1)" "\n\t"
47 : /*in*/ "r"(&block
[0])
48 : /*trash*/ "memory", "t0", "t1", "t2"
53 __attribute__((noinline
)) void atomic_saad ( long long int* p
, int n
)
55 #if (_MIPS_ARCH_OCTEON2)
56 unsigned long block
[2] = { (unsigned long)p
, (unsigned long)n
};
59 "ld $t1, 0($t0)" "\n\t" // p
60 "ld $t2, 8($t0)" "\n\t" // n
61 "saad $t2, ($t1)" "\n\t"
63 : /*in*/ "r"(&block
[0])
64 : /*trash*/ "memory", "t0", "t1", "t2"
69 __attribute__((noinline
)) void atomic_laa ( int* p
, int n
)
71 #if (_MIPS_ARCH_OCTEON2)
72 unsigned long block
[2] = { (unsigned long)p
, (unsigned long)n
};
75 "ld $t1, 0($t0)" "\n\t" // p
76 "ld $t2, 8($t0)" "\n\t" // n
77 "laa $t3, ($t1), $t2" "\n\t"
79 : /*in*/ "r"(&block
[0])
80 : /*trash*/ "memory", "t0", "t1", "t2"
85 __attribute__((noinline
)) void atomic_laad ( long long int* p
, int n
)
87 #if (_MIPS_ARCH_OCTEON2)
88 unsigned long block
[2] = { (unsigned long)p
, (unsigned long)n
};
91 "ld $t1, 0($t0)" "\n\t" // p
92 "ld $t2, 8($t0)" "\n\t" // n
93 "laad $t3, ($t1), $t2" "\n\t"
95 : /*in*/ "r"(&block
[0])
96 : /*trash*/ "memory", "t0", "t1", "t2", "t3"
101 __attribute__((noinline
)) void atomic_law ( int* p
, int n
)
103 #if (_MIPS_ARCH_OCTEON2)
104 unsigned long block
[2] = { (unsigned long)p
, (unsigned long)n
};
105 __asm__
__volatile__(
106 "move $t0, %0" "\n\t"
107 "ld $t1, 0($t0)" "\n\t" // p
108 "ld $t2, 8($t0)" "\n\t" // n
109 "law $t3, ($t1), $t2" "\n\t"
111 : /*in*/ "r"(&block
[0])
112 : /*trash*/ "memory", "t0", "t1", "t2"
117 __attribute__((noinline
)) void atomic_lawd ( long long int* p
, int n
)
119 #if (_MIPS_ARCH_OCTEON2)
120 unsigned long block
[2] = { (unsigned long)p
, (unsigned long)n
};
121 __asm__
__volatile__(
122 "move $t0, %0" "\n\t"
123 "ld $t1, 0($t0)" "\n\t" // p
124 "ld $t2, 8($t0)" "\n\t" // n
125 "lawd $t3, ($t1), $t2" "\n\t"
127 : /*in*/ "r"(&block
[0])
128 : /*trash*/ "memory", "t0", "t1", "t2", "t3"
133 __attribute__((noinline
)) void atomic_lai ( int* p
)
135 #if (_MIPS_ARCH_OCTEON2)
136 unsigned long block
[2] = { (unsigned long)p
};
137 __asm__
__volatile__(
138 "move $t0, %0" "\n\t"
139 "ld $t1, 0($t0)" "\n\t" // p
140 "ld $t2, 8($t0)" "\n\t" // n
141 "lai $t2, ($t1)" "\n\t"
143 : /*in*/ "r"(&block
[0])
144 : /*trash*/ "memory", "t0", "t1", "t2"
149 __attribute__((noinline
)) void atomic_laid ( long long int* p
)
151 #if (_MIPS_ARCH_OCTEON2)
152 unsigned long block
[2] = { (unsigned long)p
};
153 __asm__
__volatile__(
154 "move $t0, %0" "\n\t"
155 "ld $t1, 0($t0)" "\n\t" // p
156 "ld $t2, 8($t0)" "\n\t" // n
157 "laid $t2, ($t1)" "\n\t"
159 : /*in*/ "r"(&block
[0])
160 : /*trash*/ "memory", "t0", "t1", "t2"
165 __attribute__((noinline
)) void atomic_lad ( int* p
)
167 #if (_MIPS_ARCH_OCTEON2)
168 unsigned long block
[2] = { (unsigned long)p
};
169 __asm__
__volatile__(
170 "move $t0, %0" "\n\t"
171 "ld $t1, 0($t0)" "\n\t" // p
172 "ld $t2, 8($t0)" "\n\t" // n
173 "lad $t2, ($t1)" "\n\t"
175 : /*in*/ "r"(&block
[0])
176 : /*trash*/ "memory", "t0", "t1", "t2"
181 __attribute__((noinline
)) void atomic_ladd ( long long int* p
)
183 #if (_MIPS_ARCH_OCTEON2)
184 unsigned long block
[2] = { (unsigned long)p
};
185 __asm__
__volatile__(
186 "move $t0, %0" "\n\t"
187 "ld $t1, 0($t0)" "\n\t" // p
188 "ld $t2, 8($t0)" "\n\t" // n
189 "ladd $t2, ($t1)" "\n\t"
191 : /*in*/ "r"(&block
[0])
192 : /*trash*/ "memory", "t0", "t1", "t2"
197 __attribute__((noinline
)) void atomic_lac ( int* p
)
199 #if (_MIPS_ARCH_OCTEON2)
200 unsigned long block
[2] = { (unsigned long)p
};
201 __asm__
__volatile__(
202 "move $t0, %0" "\n\t"
203 "ld $t1, 0($t0)" "\n\t" // p
204 "ld $t2, 8($t0)" "\n\t" // n
205 "lac $t2, ($t1)" "\n\t"
207 : /*in*/ "r"(&block
[0])
208 : /*trash*/ "memory", "t0", "t1", "t2"
213 __attribute__((noinline
)) void atomic_lacd ( long long int* p
)
215 #if (_MIPS_ARCH_OCTEON2)
216 unsigned long block
[2] = { (unsigned long)p
};
217 __asm__
__volatile__(
218 "move $t0, %0" "\n\t"
219 "ld $t1, 0($t0)" "\n\t" // p
220 "ld $t2, 8($t0)" "\n\t" // n
221 "lacd $t2, ($t1)" "\n\t"
223 : /*in*/ "r"(&block
[0])
224 : /*trash*/ "memory", "t0", "t1", "t2"
229 __attribute__((noinline
)) void atomic_las ( int* p
)
231 #if (_MIPS_ARCH_OCTEON2)
232 unsigned long block
[2] = { (unsigned long)p
};
233 __asm__
__volatile__(
234 "move $t0, %0" "\n\t"
235 "ld $t1, 0($t0)" "\n\t" // p
236 "ld $t2, 8($t0)" "\n\t" // n
237 "las $t2, ($t1)" "\n\t"
239 : /*in*/ "r"(&block
[0])
240 : /*trash*/ "memory", "t0", "t1", "t2"
245 __attribute__((noinline
)) void atomic_lasd ( long long int* p
)
247 #if (_MIPS_ARCH_OCTEON2)
248 unsigned long block
[2] = { (unsigned long)p
};
249 __asm__
__volatile__(
250 "move $t0, %0" "\n\t"
251 "ld $t1, 0($t0)" "\n\t" // p
252 "ld $t2, 8($t0)" "\n\t" // n
253 "lasd $t2, ($t1)" "\n\t"
255 : /*in*/ "r"(&block
[0])
256 : /*trash*/ "memory", "t0", "t1", "t2"
261 #define TRIOP_AND_SAA(instruction, base1, base2, n) \
263 __asm__ __volatile__( \
264 instruction" $t0, (%0), %2" "\n\t" \
265 "saa $t0, (%1)" "\n\t" \
267 : /*in*/ "r"(base1), "r"(base2), "r"(n) \
268 : /*trash*/ "memory", "t0" \
272 #define TRIOP_AND_SAAD(instruction, base1, base2, n) \
274 __asm__ __volatile__( \
275 instruction" $t0, (%0), %2" "\n\t" \
276 "saad $t0, (%1)" "\n\t" \
278 : /*in*/ "r"(base1), "r"(base2), "r"(n) \
279 : /*trash*/ "memory", "t0" \
283 #define BINOP_AND_SAA(instruction, base1, base2) \
285 __asm__ __volatile__( \
286 instruction" $t0, (%0)" "\n\t" \
287 "saa $t0, (%1)" "\n\t" \
289 : /*in*/ "r"(base1), "r"(base2) \
290 : /*trash*/ "memory", "t0" \
294 #define BINOP_AND_SAAD(instruction, base1, base2) \
296 __asm__ __volatile__( \
297 instruction" $t0, (%0)" "\n\t" \
298 "saad $t0, (%1)" "\n\t" \
300 : /*in*/ "r"(base1), "r"(base2) \
301 : /*trash*/ "memory", "t0" \
305 int main ( int argc
, char** argv
)
307 #if (_MIPS_ARCH_OCTEON2)
311 long long int* p2
[N
];
314 for (i
= 0; i
< N
; i
++) {
315 page
[i
] = mmap( 0, sysconf(_SC_PAGESIZE
),
316 PROT_READ
|PROT_WRITE
,
317 MAP_ANONYMOUS
|MAP_SHARED
, -1, 0 );
318 if (page
[i
] == MAP_FAILED
) {
319 perror("mmap failed");
322 p1
[i
] = (int*)(page
[i
] + 0);
323 p2
[i
] = (long long int*)(page
[i
] + 256);
325 assert( IS_8_ALIGNED(p1
[i
]) );
326 assert( IS_8_ALIGNED(p2
[i
]) );
328 memset(page
[i
], 0, 1024);
329 memset(page
[i
], 0, 1024);
337 perror("fork() failed\n");
343 for (i
= 0; i
< NNN
; i
++) {
344 atomic_saa(p1
[0], i
);
345 atomic_saad(p2
[0], i
+ 98765 ); /* ensure we hit the upper 32 bits */
346 atomic_laa(p1
[1], i
);
347 atomic_laad(p2
[1], i
+ 98765 ); /* ensure we hit the upper 32 bits */
348 atomic_law(p1
[2], i
);
349 atomic_lawd(p2
[2], i
+ 98765 ); /* ensure we hit the upper 32 bits */
358 TRIOP_AND_SAA("laa ", p1
[7], p1
[8], 1)
359 TRIOP_AND_SAAD("laad ", p2
[7], p2
[8], 1)
360 TRIOP_AND_SAA("law ", p1
[9], p1
[10], i
)
361 TRIOP_AND_SAAD("lawd ", p2
[9], p2
[10], i
)
362 BINOP_AND_SAA("lai ", p1
[11], p1
[12])
363 BINOP_AND_SAAD("laid ", p2
[11], p2
[12])
364 BINOP_AND_SAA("las ", p1
[13], p1
[14])
365 BINOP_AND_SAAD("lasd ", p2
[13], p2
[14])
366 BINOP_AND_SAA("lad ", p1
[15], p1
[16])
367 BINOP_AND_SAAD("ladd ", p2
[15], p2
[16])
368 BINOP_AND_SAA("lac ", p1
[17], p1
[18])
369 BINOP_AND_SAAD("lacd ", p2
[17], p2
[18])
377 for (i
= 0; i
< NNN
; i
++) {
378 atomic_saa(p1
[0], i
);
379 atomic_saad(p2
[0], i
+ 98765); /* ensure we hit the upper 32 bits */
380 atomic_laa(p1
[1], i
);
381 atomic_laad(p2
[1], i
+ 98765); /* ensure we hit the upper 32 bits */
382 atomic_law(p1
[2], i
);
383 atomic_lawd(p2
[2], i
+ 98765 ); /* ensure we hit the upper 32 bits */
392 TRIOP_AND_SAA("laa ", p1
[7], p1
[8], 1)
393 TRIOP_AND_SAAD("laad ", p2
[7], p2
[8], 1)
394 TRIOP_AND_SAA("law ", p1
[9], p1
[10], i
)
395 TRIOP_AND_SAAD("lawd ", p2
[9], p2
[10], i
)
396 BINOP_AND_SAA("lai ", p1
[11], p1
[12])
397 BINOP_AND_SAAD("laid ", p2
[11], p2
[12])
398 BINOP_AND_SAA("las ", p1
[13], p1
[14])
399 BINOP_AND_SAAD("lasd ", p2
[13], p2
[14])
400 BINOP_AND_SAA("lad ", p1
[15], p1
[16])
401 BINOP_AND_SAAD("ladd ", p2
[15], p2
[16])
402 BINOP_AND_SAA("lac ", p1
[17], p1
[18])
403 BINOP_AND_SAAD("lacd ", p2
[17], p2
[18])
406 pc2
= waitpid(child
, &status
, 0);
407 assert(pc2
== child
);
409 /* assert that child finished normally */
410 assert(WIFEXITED(status
));
412 printf("Store Atomic Add: 32 bit %u, 64 bit %lld\n", *p1
[0], *p2
[0]);
413 printf("Load Atomic Add: 32 bit %u, 64 bit %lld\n", *p1
[1], *p2
[1]);
414 printf("Load Atomic Swap: 32 bit %u, 64 bit %lld\n", *p1
[2], *p2
[2]);
415 printf("Load Atomic Increment: 32 bit %u, 64 bit %lld\n", *p1
[3], *p2
[3]);
416 printf("Load Atomic Decrement: 32 bit %u, 64 bit %lld\n", *p1
[4], *p2
[4]);
417 printf("Load Atomic Clear: 32 bit %u, 64 bit %lld\n", *p1
[5], *p2
[5]);
418 printf("Load Atomic Set: 32 bit %u, 64 bit %lld\n", *p1
[6], *p2
[6]);
419 printf("laa and saa: base1: %u, base2: %u\n", *p1
[7], *p1
[8]);
420 printf("laad and saad: base1: %lld, base2: %lld\n", *p2
[7], *p2
[8]);
421 printf("law and saa: base1: %u, base2: %u\n", *p1
[9], *p1
[10]);
422 printf("lawd and saad: base1: %lld, base2: %lld\n", *p2
[9], *p2
[10]);
423 printf("lai and saa: base1: %u, base2: %u\n", *p1
[11], *p1
[12]);
424 printf("laid and saad: base1: %lld, base2: %lld\n", *p2
[11], *p2
[12]);
425 printf("las and saa: base1: %u, base2: %u\n", *p1
[13], *p1
[14]);
426 printf("lasd and saad: base1: %lld, base2: %lld\n", *p2
[13], *p2
[14]);
427 printf("lad and saa: base1: %u, base2: %u\n", *p1
[15], *p1
[16]);
428 printf("ladd and saad: base1: %lld, base2: %lld\n", *p2
[15], *p2
[16]);
429 printf("lac and saa: base1: %u, base2: %u\n", *p1
[17], *p1
[18]);
430 printf("lacd and saad: base1: %lld, base2: %lld\n", *p2
[17], *p2
[18]);
432 for (i
= 0; i
< N
; i
++) {
433 if (p1_expd
[i
] == *p1
[i
] && p2_expd
[i
] == *p2
[i
]) {
434 printf("PASS %d\n", i
+1);
436 printf("FAIL %d -- see source code for expected values\n", i
+1);
440 printf("parent exits\n");