HPCToolkit
perf_barrier.h
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // --------------------------------------------------------------------------
6 // Part of HPCToolkit (hpctoolkit.org)
7 //
8 // Information about sources of support for research and development of
9 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
10 // --------------------------------------------------------------------------
11 //
12 // Copyright ((c)) 2002-2019, Rice University
13 // All rights reserved.
14 //
15 // Redistribution and use in source and binary forms, with or without
16 // modification, are permitted provided that the following conditions are
17 // met:
18 //
19 // * Redistributions of source code must retain the above copyright
20 // notice, this list of conditions and the following disclaimer.
21 //
22 // * Redistributions in binary form must reproduce the above copyright
23 // notice, this list of conditions and the following disclaimer in the
24 // documentation and/or other materials provided with the distribution.
25 //
26 // * Neither the name of Rice University (RICE) nor the names of its
27 // contributors may be used to endorse or promote products derived from
28 // this software without specific prior written permission.
29 //
30 // This software is provided by RICE and contributors "as is" and any
31 // express or implied warranties, including, but not limited to, the
32 // implied warranties of merchantability and fitness for a particular
33 // purpose are disclaimed. In no event shall RICE or contributors be
34 // liable for any direct, indirect, incidental, special, exemplary, or
35 // consequential damages (including, but not limited to, procurement of
36 // substitute goods or services; loss of use, data, or profits; or
37 // business interruption) however caused and on any theory of liability,
38 // whether in contract, strict liability, or tort (including negligence
39 // or otherwise) arising in any way out of the use of this software, even
40 // if advised of the possibility of such damage.
41 //
42 // ******************************************************* EndRiceCopyright *
43 
44 /***
45  *** The code in this file is mainly taken from
46  *** https://github.com/deater/perf_event_tests/blob/master/include/perf_barrier.h
47  ***/
48 
49 #include <stdio.h>
50 #ifdef __powerpc__
51 #define rmb() asm volatile ("sync" : : : "memory")
52 
53 #elif defined (__s390__)
54 #define rmb() asm volatile("bcr 15,0" ::: "memory")
55 
56 #elif defined (__sh__)
57 #if defined(__SH4A__) || defined(__SH5__)
58 #define rmb() asm volatile("synco" ::: "memory")
59 #else
60 #define rmb() asm volatile("" ::: "memory")
61 #endif
62 
63 #elif defined (__hppa__)
64 #define rmb() asm volatile("" ::: "memory")
65 
66 #elif defined (__sparc__)
67 #define rmb() asm volatile("":::"memory")
68 
69 #elif defined (__alpha__)
70 #define rmb() asm volatile("mb" ::: "memory")
71 
72 #elif defined(__ia64__)
73 #define rmb() asm volatile ("mf" ::: "memory")
74 
75 #elif defined(__arm__)
76 /*
77  * Use the __kuser_memory_barrier helper in the CPU helper page. See
78  * arch/arm/kernel/entry-armv.S in the kernel source for details.
79  */
80 #define rmb() ((void(*)(void))0xffff0fa0)()
81 
82 //#if __LINUX_ARM_ARCH__ >= 7
83 //#define rmb() asm volatile("dsb " "" : : : "memory")
84 //#else
85 //#define rmb() asm volatile("mcr p15, 0, %0, c7, c10, 4" : : "r" (0) : "memory")
86 //#endif
87 
88 #elif defined(__aarch64__)
89 #define rmb() asm volatile("dmb ld" ::: "memory")
90 //#define rmb() asm volatile("dsb " "ld" : : : "memory")
91 
92 #elif defined(__mips__)
93 #define rmb() asm volatile( \
94  ".set mips2\n\t" \
95  "sync\n\t" \
96  ".set mips0" \
97  : /* no output */ \
98  : /* no input */ \
99  : "memory")
100 
101 #elif defined(__i386__)
102 #define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
103 
104 #elif defined(__x86_64)
105 
106 #if defined(__KNC__)
107 #define rmb() __sync_synchronize()
108 
109 #else
110 #define rmb() asm volatile("lfence":::"memory")
111 #endif
112 
113 #else
114 #error Need to define rmb for this architecture!
115 #error See the kernel source directory: tools/perf/perf.h file
116 #endif
117