2 /*--------------------------------------------------------------------*/
3 /*--- The translation table and cache. ---*/
4 /*--- pub_core_transtab.h ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2017 Julian Seward
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. */
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
;
93 # error "VG_TT_FAST_HASH: unknown platform"
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
)) {
105 if (LIKELY(set
->guest1
== guest
)) {
106 // hit at way 1; swap upwards
108 Addr tH
= set
->host1
;
109 set
->guest1
= set
->guest0
;
110 set
->host1
= set
->host0
;
116 if (LIKELY(set
->guest2
== guest
)) {
117 // hit at way 2; swap upwards
119 Addr tH
= set
->host2
;
120 set
->guest2
= set
->guest1
;
121 set
->host2
= set
->host1
;
127 if (LIKELY(set
->guest3
== guest
)) {
128 // hit at way 3; swap upwards
130 Addr tH
= set
->host3
;
131 set
->guest3
= set
->guest2
;
132 set
->host3
= set
->host2
;
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
167 # define N_SECTORS_DEFAULT 32
171 void VG_(add_to_transtab
)( const VexGuestExtents
* vge
,
175 Bool is_self_checking
,
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)
187 void VG_(tt_tc_do_chaining
) ( void* from__patch_addr
,
192 extern Bool
VG_(search_transtab
) ( /*OUT*/Addr
* res_hcode
,
193 /*OUT*/SECno
* res_sNo
,
194 /*OUT*/TTEno
* res_tteNo
,
198 extern void VG_(discard_translations
) ( Addr start
, ULong range
,
201 extern void VG_(print_tt_tc_stats
) ( void );
203 extern UInt
VG_(get_bbs_translated
) ( void );
204 extern UInt
VG_(get_bbs_discarded_or_dumped
) ( void );
206 /* Add to / search the auxiliary, small, unredirected translation
210 void VG_(add_to_unredir_transtab
)( const VexGuestExtents
* vge
,
215 Bool
VG_(search_unredir_transtab
) ( /*OUT*/Addr
* result
,
218 // SB profiling stuff
220 typedef struct _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 /*--------------------------------------------------------------------*/
234 /*--------------------------------------------------------------------*/