FreeBSD: add file descriptor tracking for _umtx_op
[valgrind.git] / coregrind / pub_core_transtab.h
blob6cc11f65806cfeadce2654437ae7b08ce6152fcd
2 /*--------------------------------------------------------------------*/
3 /*--- The translation table and cache. ---*/
4 /*--- pub_core_transtab.h ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2000-2017 Julian Seward
12 jseward@acm.org
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 #ifndef __PUB_CORE_TRANSTAB_H
31 #define __PUB_CORE_TRANSTAB_H
33 //--------------------------------------------------------------------
34 // PURPOSE: This module is responsible for caching translations, and
35 // enabling fast look-ups of them.
36 //--------------------------------------------------------------------
38 #include "pub_core_transtab_asm.h"
39 #include "pub_tool_transtab.h"
40 #include "libvex.h" // VexGuestExtents
42 /* The fast-cache for tt-lookup. Unused entries are denoted by
43 .guest == TRANSTAB_BOGUS_GUEST_ADDR (viz, 1), which is assumed
44 to be a bogus address for all guest code. See pub_core_transtab_asm.h
45 for further description. */
46 typedef
47 struct {
48 Addr guest0;
49 Addr host0;
50 Addr guest1;
51 Addr host1;
52 Addr guest2;
53 Addr host2;
54 Addr guest3;
55 Addr host3;
57 FastCacheSet;
59 STATIC_ASSERT(sizeof(Addr) == sizeof(UWord));
60 STATIC_ASSERT(sizeof(FastCacheSet) == sizeof(Addr) * 8);
62 extern __attribute__((aligned(64)))
63 FastCacheSet VG_(tt_fast) [VG_TT_FAST_SETS];
65 #define TRANSTAB_BOGUS_GUEST_ADDR ((Addr)1)
67 #if defined(VGA_x86) || defined(VGA_amd64)
68 static inline UWord VG_TT_FAST_HASH ( Addr guest ) {
69 // There's no minimum insn alignment on these targets.
70 UWord merged = ((UWord)guest) >> 0;
71 merged = (merged >> VG_TT_FAST_BITS) ^ merged;
72 return merged & VG_TT_FAST_MASK;
75 #elif defined(VGA_s390x) || defined(VGA_arm) || defined(VGA_nanomips)
76 static inline UWord VG_TT_FAST_HASH ( Addr guest ) {
77 // Instructions are 2-byte aligned.
78 UWord merged = ((UWord)guest) >> 1;
79 merged = (merged >> VG_TT_FAST_BITS) ^ merged;
80 return merged & VG_TT_FAST_MASK;
83 #elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
84 || defined(VGA_mips32) || defined(VGA_mips64) || defined(VGA_arm64)
85 static inline UWord VG_TT_FAST_HASH ( Addr guest ) {
86 // Instructions are 4-byte aligned.
87 UWord merged = ((UWord)guest) >> 2;
88 merged = (merged >> VG_TT_FAST_BITS) ^ merged;
89 return merged & VG_TT_FAST_MASK;
92 #else
93 # error "VG_TT_FAST_HASH: unknown platform"
94 #endif
96 static inline Bool VG_(lookupInFastCache)( /*MB_OUT*/Addr* host, Addr guest )
98 UWord setNo = (UInt)VG_TT_FAST_HASH(guest);
99 FastCacheSet* set = &VG_(tt_fast)[setNo];
100 if (LIKELY(set->guest0 == guest)) {
101 // hit at way 0
102 *host = set->host0;
103 return True;
105 if (LIKELY(set->guest1 == guest)) {
106 // hit at way 1; swap upwards
107 Addr tG = guest;
108 Addr tH = set->host1;
109 set->guest1 = set->guest0;
110 set->host1 = set->host0;
111 set->guest0 = tG;
112 set->host0 = tH;
113 *host = tH;
114 return True;
116 if (LIKELY(set->guest2 == guest)) {
117 // hit at way 2; swap upwards
118 Addr tG = guest;
119 Addr tH = set->host2;
120 set->guest2 = set->guest1;
121 set->host2 = set->host1;
122 set->guest1 = tG;
123 set->host1 = tH;
124 *host = tH;
125 return True;
127 if (LIKELY(set->guest3 == guest)) {
128 // hit at way 3; swap upwards
129 Addr tG = guest;
130 Addr tH = set->host3;
131 set->guest3 = set->guest2;
132 set->host3 = set->host2;
133 set->guest2 = tG;
134 set->host2 = tH;
135 *host = tH;
136 return True;
138 // Not found
139 *host = 0;
140 return False;
144 /* Initialises the TC, using VG_(clo_num_transtab_sectors)
145 and VG_(clo_avg_transtab_entry_size).
146 VG_(clo_num_transtab_sectors) must be >= MIN_N_SECTORS
147 and <= MAX_N_SECTORS. */
148 extern void VG_(init_tt_tc) ( void );
151 /* Limits for number of sectors the TC is divided into. If you need a larger
152 overall translation cache, increase MAX_N_SECTORS. */
153 #define MIN_N_SECTORS 2
154 #define MAX_N_SECTORS 48
156 /* Default for the nr of sectors, if not overridden by command line.
157 On Android, space is limited, so try to get by with fewer sectors.
158 On other platforms we can go to town. 32 sectors gives theoretical
159 capacity of about 880MB of JITted code in 2.1 million translations
160 (realistically, about 2/3 of that) for Memcheck. */
161 #if defined(VGPV_arm_linux_android) \
162 || defined(VGPV_x86_linux_android) \
163 || defined(VGPV_mips32_linux_android) \
164 || defined(VGPV_arm64_linux_android)
165 # define N_SECTORS_DEFAULT 12
166 #else
167 # define N_SECTORS_DEFAULT 32
168 #endif
170 extern
171 void VG_(add_to_transtab)( const VexGuestExtents* vge,
172 Addr entry,
173 Addr code,
174 UInt code_len,
175 Bool is_self_checking,
176 Int offs_profInc,
177 UInt n_guest_instrs );
179 typedef UShort SECno; // SECno type identifies a sector
180 typedef UShort TTEno; // TTEno type identifies a TT entry in a sector.
182 // 2 constants that indicates Invalid entries.
183 #define INV_SNO ((SECno)0xFFFF)
184 #define INV_TTE ((TTEno)0xFFFF)
186 extern
187 void VG_(tt_tc_do_chaining) ( void* from__patch_addr,
188 SECno to_sNo,
189 TTEno to_tteNo,
190 Bool to_fastEP );
192 extern Bool VG_(search_transtab) ( /*OUT*/Addr* res_hcode,
193 /*OUT*/SECno* res_sNo,
194 /*OUT*/TTEno* res_tteNo,
195 Addr guest_addr,
196 Bool upd_cache );
198 extern void VG_(discard_translations) ( Addr start, ULong range,
199 const HChar* who );
201 extern void VG_(print_tt_tc_stats) ( void );
203 extern ULong VG_(get_bbs_translated) ( void );
204 extern ULong VG_(get_bbs_discarded_or_dumped) ( void );
206 /* Add to / search the auxiliary, small, unredirected translation
207 table. */
209 extern
210 void VG_(add_to_unredir_transtab)( const VexGuestExtents* vge,
211 Addr entry,
212 Addr code,
213 UInt code_len );
214 extern
215 Bool VG_(search_unredir_transtab) ( /*OUT*/Addr* result,
216 Addr guest_addr );
218 // SB profiling stuff
220 typedef struct _SBProfEntry {
221 Addr addr;
222 ULong score;
223 } SBProfEntry;
225 extern ULong VG_(get_SB_profile) ( SBProfEntry tops[], UInt n_tops );
227 // Exported variables
228 extern Bool VG_(ok_to_discard_translations);
230 #endif // __PUB_CORE_TRANSTAB_H
232 /*--------------------------------------------------------------------*/
233 /*--- end ---*/
234 /*--------------------------------------------------------------------*/