]>
Commit | Line | Data |
---|---|---|
d19893da FB |
1 | /* |
2 | * Host code generation | |
5fafdf24 | 3 | * |
d19893da FB |
4 | * Copyright (c) 2003 Fabrice Bellard |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
d19893da | 18 | */ |
5b6dd868 BS |
19 | #ifdef _WIN32 |
20 | #include <windows.h> | |
5b6dd868 | 21 | #endif |
7b31bbc2 | 22 | #include "qemu/osdep.h" |
d19893da | 23 | |
2054396a | 24 | |
5b6dd868 | 25 | #include "qemu-common.h" |
af5ad107 | 26 | #define NO_CPU_IO_DEFS |
d3eead2e | 27 | #include "cpu.h" |
6db8b538 | 28 | #include "trace.h" |
76cad711 | 29 | #include "disas/disas.h" |
63c91552 | 30 | #include "exec/exec-all.h" |
57fec1fe | 31 | #include "tcg.h" |
5b6dd868 BS |
32 | #if defined(CONFIG_USER_ONLY) |
33 | #include "qemu.h" | |
301e40ed | 34 | #include "exec/exec-all.h" |
5b6dd868 BS |
35 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
36 | #include <sys/param.h> | |
37 | #if __FreeBSD_version >= 700104 | |
38 | #define HAVE_KINFO_GETVMMAP | |
39 | #define sigqueue sigqueue_freebsd /* avoid redefinition */ | |
5b6dd868 BS |
40 | #include <sys/proc.h> |
41 | #include <machine/profile.h> | |
42 | #define _KERNEL | |
43 | #include <sys/user.h> | |
44 | #undef _KERNEL | |
45 | #undef sigqueue | |
46 | #include <libutil.h> | |
47 | #endif | |
48 | #endif | |
0bc3cd62 PB |
49 | #else |
50 | #include "exec/address-spaces.h" | |
5b6dd868 BS |
51 | #endif |
52 | ||
022c62cb | 53 | #include "exec/cputlb.h" |
e1b89321 | 54 | #include "exec/tb-hash.h" |
5b6dd868 | 55 | #include "translate-all.h" |
510a647f | 56 | #include "qemu/bitmap.h" |
0aa09897 | 57 | #include "qemu/timer.h" |
508127e2 | 58 | #include "exec/log.h" |
5b6dd868 | 59 | |
955939a2 AB |
60 | /* #define DEBUG_TB_INVALIDATE */ |
61 | /* #define DEBUG_TB_FLUSH */ | |
301e40ed | 62 | /* #define DEBUG_LOCKING */ |
5b6dd868 | 63 | /* make various TB consistency checks */ |
955939a2 | 64 | /* #define DEBUG_TB_CHECK */ |
5b6dd868 BS |
65 | |
66 | #if !defined(CONFIG_USER_ONLY) | |
67 | /* TB consistency checks only implemented for usermode emulation. */ | |
68 | #undef DEBUG_TB_CHECK | |
69 | #endif | |
70 | ||
301e40ed AB |
71 | /* Access to the various translations structures need to be serialised via locks |
72 | * for consistency. This is automatic for SoftMMU based system | |
73 | * emulation due to its single threaded nature. In user-mode emulation | |
74 | * access to the memory related structures are protected with the | |
75 | * mmap_lock. | |
76 | */ | |
77 | #ifdef DEBUG_LOCKING | |
78 | #define DEBUG_MEM_LOCKS 1 | |
79 | #else | |
80 | #define DEBUG_MEM_LOCKS 0 | |
81 | #endif | |
82 | ||
83 | #ifdef CONFIG_SOFTMMU | |
84 | #define assert_memory_lock() do { /* nothing */ } while (0) | |
85 | #else | |
86 | #define assert_memory_lock() do { \ | |
87 | if (DEBUG_MEM_LOCKS) { \ | |
88 | g_assert(have_mmap_lock()); \ | |
89 | } \ | |
90 | } while (0) | |
91 | #endif | |
92 | ||
5b6dd868 BS |
93 | #define SMC_BITMAP_USE_THRESHOLD 10 |
94 | ||
5b6dd868 BS |
95 | typedef struct PageDesc { |
96 | /* list of TBs intersecting this ram page */ | |
97 | TranslationBlock *first_tb; | |
6fad459c | 98 | #ifdef CONFIG_SOFTMMU |
5b6dd868 BS |
99 | /* in order to optimize self modifying code, we count the number |
100 | of lookups we do to a given page to use a bitmap */ | |
101 | unsigned int code_write_count; | |
510a647f | 102 | unsigned long *code_bitmap; |
6fad459c | 103 | #else |
5b6dd868 BS |
104 | unsigned long flags; |
105 | #endif | |
106 | } PageDesc; | |
107 | ||
108 | /* In system mode we want L1_MAP to be based on ram offsets, | |
109 | while in user mode we want it to be based on virtual addresses. */ | |
110 | #if !defined(CONFIG_USER_ONLY) | |
111 | #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS | |
112 | # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS | |
113 | #else | |
114 | # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS | |
115 | #endif | |
116 | #else | |
117 | # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS | |
118 | #endif | |
119 | ||
03f49957 PB |
120 | /* Size of the L2 (and L3, etc) page tables. */ |
121 | #define V_L2_BITS 10 | |
122 | #define V_L2_SIZE (1 << V_L2_BITS) | |
123 | ||
5b6dd868 | 124 | uintptr_t qemu_host_page_size; |
0c2d70c4 | 125 | intptr_t qemu_host_page_mask; |
5b6dd868 | 126 | |
66ec9f49 VK |
127 | /* |
128 | * L1 Mapping properties | |
129 | */ | |
130 | static int v_l1_size; | |
131 | static int v_l1_shift; | |
132 | static int v_l2_levels; | |
133 | ||
134 | /* The bottom level has pointers to PageDesc, and is indexed by | |
135 | * anything from 4 to (V_L2_BITS + 3) bits, depending on target page size. | |
136 | */ | |
137 | #define V_L1_MIN_BITS 4 | |
138 | #define V_L1_MAX_BITS (V_L2_BITS + 3) | |
139 | #define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS) | |
140 | ||
141 | static void *l1_map[V_L1_MAX_SIZE]; | |
5b6dd868 | 142 | |
57fec1fe FB |
143 | /* code generation context */ |
144 | TCGContext tcg_ctx; | |
fdbc2b57 | 145 | bool parallel_cpus; |
d19893da | 146 | |
677ef623 FK |
147 | /* translation block context */ |
148 | #ifdef CONFIG_USER_ONLY | |
149 | __thread int have_tb_lock; | |
150 | #endif | |
151 | ||
66ec9f49 VK |
152 | static void page_table_config_init(void) |
153 | { | |
154 | uint32_t v_l1_bits; | |
155 | ||
156 | assert(TARGET_PAGE_BITS); | |
157 | /* The bits remaining after N lower levels of page tables. */ | |
158 | v_l1_bits = (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS; | |
159 | if (v_l1_bits < V_L1_MIN_BITS) { | |
160 | v_l1_bits += V_L2_BITS; | |
161 | } | |
162 | ||
163 | v_l1_size = 1 << v_l1_bits; | |
164 | v_l1_shift = L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - v_l1_bits; | |
165 | v_l2_levels = v_l1_shift / V_L2_BITS - 1; | |
166 | ||
167 | assert(v_l1_bits <= V_L1_MAX_BITS); | |
168 | assert(v_l1_shift % V_L2_BITS == 0); | |
169 | assert(v_l2_levels >= 0); | |
170 | } | |
171 | ||
677ef623 FK |
172 | void tb_lock(void) |
173 | { | |
174 | #ifdef CONFIG_USER_ONLY | |
175 | assert(!have_tb_lock); | |
176 | qemu_mutex_lock(&tcg_ctx.tb_ctx.tb_lock); | |
177 | have_tb_lock++; | |
178 | #endif | |
179 | } | |
180 | ||
181 | void tb_unlock(void) | |
182 | { | |
183 | #ifdef CONFIG_USER_ONLY | |
184 | assert(have_tb_lock); | |
185 | have_tb_lock--; | |
186 | qemu_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock); | |
187 | #endif | |
188 | } | |
189 | ||
190 | void tb_lock_reset(void) | |
191 | { | |
192 | #ifdef CONFIG_USER_ONLY | |
193 | if (have_tb_lock) { | |
194 | qemu_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock); | |
195 | have_tb_lock = 0; | |
196 | } | |
197 | #endif | |
198 | } | |
199 | ||
301e40ed AB |
200 | #ifdef DEBUG_LOCKING |
201 | #define DEBUG_TB_LOCKS 1 | |
202 | #else | |
203 | #define DEBUG_TB_LOCKS 0 | |
204 | #endif | |
205 | ||
206 | #ifdef CONFIG_SOFTMMU | |
207 | #define assert_tb_lock() do { /* nothing */ } while (0) | |
208 | #else | |
209 | #define assert_tb_lock() do { \ | |
210 | if (DEBUG_TB_LOCKS) { \ | |
211 | g_assert(have_tb_lock); \ | |
212 | } \ | |
213 | } while (0) | |
214 | #endif | |
215 | ||
216 | ||
a8a826a3 | 217 | static TranslationBlock *tb_find_pc(uintptr_t tc_ptr); |
5b6dd868 | 218 | |
57fec1fe FB |
219 | void cpu_gen_init(void) |
220 | { | |
221 | tcg_context_init(&tcg_ctx); | |
57fec1fe FB |
222 | } |
223 | ||
fca8a500 RH |
224 | /* Encode VAL as a signed leb128 sequence at P. |
225 | Return P incremented past the encoded value. */ | |
226 | static uint8_t *encode_sleb128(uint8_t *p, target_long val) | |
227 | { | |
228 | int more, byte; | |
229 | ||
230 | do { | |
231 | byte = val & 0x7f; | |
232 | val >>= 7; | |
233 | more = !((val == 0 && (byte & 0x40) == 0) | |
234 | || (val == -1 && (byte & 0x40) != 0)); | |
235 | if (more) { | |
236 | byte |= 0x80; | |
237 | } | |
238 | *p++ = byte; | |
239 | } while (more); | |
240 | ||
241 | return p; | |
242 | } | |
243 | ||
244 | /* Decode a signed leb128 sequence at *PP; increment *PP past the | |
245 | decoded value. Return the decoded value. */ | |
246 | static target_long decode_sleb128(uint8_t **pp) | |
247 | { | |
248 | uint8_t *p = *pp; | |
249 | target_long val = 0; | |
250 | int byte, shift = 0; | |
251 | ||
252 | do { | |
253 | byte = *p++; | |
254 | val |= (target_ulong)(byte & 0x7f) << shift; | |
255 | shift += 7; | |
256 | } while (byte & 0x80); | |
257 | if (shift < TARGET_LONG_BITS && (byte & 0x40)) { | |
258 | val |= -(target_ulong)1 << shift; | |
259 | } | |
260 | ||
261 | *pp = p; | |
262 | return val; | |
263 | } | |
264 | ||
265 | /* Encode the data collected about the instructions while compiling TB. | |
266 | Place the data at BLOCK, and return the number of bytes consumed. | |
267 | ||
268 | The logical table consisits of TARGET_INSN_START_WORDS target_ulong's, | |
269 | which come from the target's insn_start data, followed by a uintptr_t | |
270 | which comes from the host pc of the end of the code implementing the insn. | |
271 | ||
272 | Each line of the table is encoded as sleb128 deltas from the previous | |
273 | line. The seed for the first line is { tb->pc, 0..., tb->tc_ptr }. | |
274 | That is, the first column is seeded with the guest pc, the last column | |
275 | with the host pc, and the middle columns with zeros. */ | |
276 | ||
277 | static int encode_search(TranslationBlock *tb, uint8_t *block) | |
278 | { | |
b125f9dc | 279 | uint8_t *highwater = tcg_ctx.code_gen_highwater; |
fca8a500 RH |
280 | uint8_t *p = block; |
281 | int i, j, n; | |
282 | ||
283 | tb->tc_search = block; | |
284 | ||
285 | for (i = 0, n = tb->icount; i < n; ++i) { | |
286 | target_ulong prev; | |
287 | ||
288 | for (j = 0; j < TARGET_INSN_START_WORDS; ++j) { | |
289 | if (i == 0) { | |
290 | prev = (j == 0 ? tb->pc : 0); | |
291 | } else { | |
292 | prev = tcg_ctx.gen_insn_data[i - 1][j]; | |
293 | } | |
294 | p = encode_sleb128(p, tcg_ctx.gen_insn_data[i][j] - prev); | |
295 | } | |
296 | prev = (i == 0 ? 0 : tcg_ctx.gen_insn_end_off[i - 1]); | |
297 | p = encode_sleb128(p, tcg_ctx.gen_insn_end_off[i] - prev); | |
b125f9dc RH |
298 | |
299 | /* Test for (pending) buffer overflow. The assumption is that any | |
300 | one row beginning below the high water mark cannot overrun | |
301 | the buffer completely. Thus we can test for overflow after | |
302 | encoding a row without having to check during encoding. */ | |
303 | if (unlikely(p > highwater)) { | |
304 | return -1; | |
305 | } | |
fca8a500 RH |
306 | } |
307 | ||
308 | return p - block; | |
309 | } | |
310 | ||
7d7500d9 PB |
311 | /* The cpu state corresponding to 'searched_pc' is restored. |
312 | * Called with tb_lock held. | |
313 | */ | |
74f10515 | 314 | static int cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb, |
a8a826a3 | 315 | uintptr_t searched_pc) |
d19893da | 316 | { |
fca8a500 RH |
317 | target_ulong data[TARGET_INSN_START_WORDS] = { tb->pc }; |
318 | uintptr_t host_pc = (uintptr_t)tb->tc_ptr; | |
74f10515 | 319 | CPUArchState *env = cpu->env_ptr; |
fca8a500 RH |
320 | uint8_t *p = tb->tc_search; |
321 | int i, j, num_insns = tb->icount; | |
57fec1fe | 322 | #ifdef CONFIG_PROFILER |
fca8a500 | 323 | int64_t ti = profile_getclock(); |
57fec1fe FB |
324 | #endif |
325 | ||
01ecaf43 RH |
326 | searched_pc -= GETPC_ADJ; |
327 | ||
fca8a500 RH |
328 | if (searched_pc < host_pc) { |
329 | return -1; | |
330 | } | |
d19893da | 331 | |
fca8a500 RH |
332 | /* Reconstruct the stored insn data while looking for the point at |
333 | which the end of the insn exceeds the searched_pc. */ | |
334 | for (i = 0; i < num_insns; ++i) { | |
335 | for (j = 0; j < TARGET_INSN_START_WORDS; ++j) { | |
336 | data[j] += decode_sleb128(&p); | |
337 | } | |
338 | host_pc += decode_sleb128(&p); | |
339 | if (host_pc > searched_pc) { | |
340 | goto found; | |
341 | } | |
342 | } | |
343 | return -1; | |
3b46e624 | 344 | |
fca8a500 | 345 | found: |
bd79255d | 346 | if (tb->cflags & CF_USE_ICOUNT) { |
414b15c9 | 347 | assert(use_icount); |
2e70f6ef | 348 | /* Reset the cycle counter to the start of the block. */ |
fca8a500 | 349 | cpu->icount_decr.u16.low += num_insns; |
2e70f6ef | 350 | /* Clear the IO flag. */ |
99df7dce | 351 | cpu->can_do_io = 0; |
2e70f6ef | 352 | } |
fca8a500 RH |
353 | cpu->icount_decr.u16.low -= i; |
354 | restore_state_to_opc(env, tb, data); | |
57fec1fe FB |
355 | |
356 | #ifdef CONFIG_PROFILER | |
fca8a500 RH |
357 | tcg_ctx.restore_time += profile_getclock() - ti; |
358 | tcg_ctx.restore_count++; | |
57fec1fe | 359 | #endif |
d19893da FB |
360 | return 0; |
361 | } | |
5b6dd868 | 362 | |
3f38f309 | 363 | bool cpu_restore_state(CPUState *cpu, uintptr_t retaddr) |
a8a826a3 BS |
364 | { |
365 | TranslationBlock *tb; | |
a5e99826 | 366 | bool r = false; |
a8a826a3 | 367 | |
a5e99826 | 368 | tb_lock(); |
a8a826a3 BS |
369 | tb = tb_find_pc(retaddr); |
370 | if (tb) { | |
74f10515 | 371 | cpu_restore_state_from_tb(cpu, tb, retaddr); |
d8a499f1 PD |
372 | if (tb->cflags & CF_NOCACHE) { |
373 | /* one-shot translation, invalidate it immediately */ | |
d8a499f1 PD |
374 | tb_phys_invalidate(tb, -1); |
375 | tb_free(tb); | |
376 | } | |
a5e99826 | 377 | r = true; |
a8a826a3 | 378 | } |
a5e99826 FK |
379 | tb_unlock(); |
380 | ||
381 | return r; | |
a8a826a3 BS |
382 | } |
383 | ||
47c16ed5 | 384 | void page_size_init(void) |
5b6dd868 BS |
385 | { |
386 | /* NOTE: we can always suppose that qemu_host_page_size >= | |
387 | TARGET_PAGE_SIZE */ | |
5b6dd868 | 388 | qemu_real_host_page_size = getpagesize(); |
0c2d70c4 | 389 | qemu_real_host_page_mask = -(intptr_t)qemu_real_host_page_size; |
5b6dd868 BS |
390 | if (qemu_host_page_size == 0) { |
391 | qemu_host_page_size = qemu_real_host_page_size; | |
392 | } | |
393 | if (qemu_host_page_size < TARGET_PAGE_SIZE) { | |
394 | qemu_host_page_size = TARGET_PAGE_SIZE; | |
395 | } | |
0c2d70c4 | 396 | qemu_host_page_mask = -(intptr_t)qemu_host_page_size; |
47c16ed5 | 397 | } |
5b6dd868 | 398 | |
47c16ed5 AK |
399 | static void page_init(void) |
400 | { | |
401 | page_size_init(); | |
66ec9f49 VK |
402 | page_table_config_init(); |
403 | ||
5b6dd868 BS |
404 | #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY) |
405 | { | |
406 | #ifdef HAVE_KINFO_GETVMMAP | |
407 | struct kinfo_vmentry *freep; | |
408 | int i, cnt; | |
409 | ||
410 | freep = kinfo_getvmmap(getpid(), &cnt); | |
411 | if (freep) { | |
412 | mmap_lock(); | |
413 | for (i = 0; i < cnt; i++) { | |
414 | unsigned long startaddr, endaddr; | |
415 | ||
416 | startaddr = freep[i].kve_start; | |
417 | endaddr = freep[i].kve_end; | |
418 | if (h2g_valid(startaddr)) { | |
419 | startaddr = h2g(startaddr) & TARGET_PAGE_MASK; | |
420 | ||
421 | if (h2g_valid(endaddr)) { | |
422 | endaddr = h2g(endaddr); | |
423 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); | |
424 | } else { | |
425 | #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS | |
426 | endaddr = ~0ul; | |
427 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); | |
428 | #endif | |
429 | } | |
430 | } | |
431 | } | |
432 | free(freep); | |
433 | mmap_unlock(); | |
434 | } | |
435 | #else | |
436 | FILE *f; | |
437 | ||
438 | last_brk = (unsigned long)sbrk(0); | |
439 | ||
440 | f = fopen("/compat/linux/proc/self/maps", "r"); | |
441 | if (f) { | |
442 | mmap_lock(); | |
443 | ||
444 | do { | |
445 | unsigned long startaddr, endaddr; | |
446 | int n; | |
447 | ||
448 | n = fscanf(f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr); | |
449 | ||
450 | if (n == 2 && h2g_valid(startaddr)) { | |
451 | startaddr = h2g(startaddr) & TARGET_PAGE_MASK; | |
452 | ||
453 | if (h2g_valid(endaddr)) { | |
454 | endaddr = h2g(endaddr); | |
455 | } else { | |
456 | endaddr = ~0ul; | |
457 | } | |
458 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); | |
459 | } | |
460 | } while (!feof(f)); | |
461 | ||
462 | fclose(f); | |
463 | mmap_unlock(); | |
464 | } | |
465 | #endif | |
466 | } | |
467 | #endif | |
468 | } | |
469 | ||
75692087 | 470 | /* If alloc=1: |
7d7500d9 | 471 | * Called with tb_lock held for system emulation. |
75692087 PB |
472 | * Called with mmap_lock held for user-mode emulation. |
473 | */ | |
5b6dd868 BS |
474 | static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc) |
475 | { | |
476 | PageDesc *pd; | |
477 | void **lp; | |
478 | int i; | |
479 | ||
e505a063 AB |
480 | if (alloc) { |
481 | assert_memory_lock(); | |
482 | } | |
483 | ||
5b6dd868 | 484 | /* Level 1. Always allocated. */ |
66ec9f49 | 485 | lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1)); |
5b6dd868 BS |
486 | |
487 | /* Level 2..N-1. */ | |
66ec9f49 | 488 | for (i = v_l2_levels; i > 0; i--) { |
6940fab8 | 489 | void **p = atomic_rcu_read(lp); |
5b6dd868 BS |
490 | |
491 | if (p == NULL) { | |
492 | if (!alloc) { | |
493 | return NULL; | |
494 | } | |
e3a0abfd | 495 | p = g_new0(void *, V_L2_SIZE); |
6940fab8 | 496 | atomic_rcu_set(lp, p); |
5b6dd868 BS |
497 | } |
498 | ||
03f49957 | 499 | lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1)); |
5b6dd868 BS |
500 | } |
501 | ||
6940fab8 | 502 | pd = atomic_rcu_read(lp); |
5b6dd868 BS |
503 | if (pd == NULL) { |
504 | if (!alloc) { | |
505 | return NULL; | |
506 | } | |
e3a0abfd | 507 | pd = g_new0(PageDesc, V_L2_SIZE); |
6940fab8 | 508 | atomic_rcu_set(lp, pd); |
5b6dd868 BS |
509 | } |
510 | ||
03f49957 | 511 | return pd + (index & (V_L2_SIZE - 1)); |
5b6dd868 BS |
512 | } |
513 | ||
514 | static inline PageDesc *page_find(tb_page_addr_t index) | |
515 | { | |
516 | return page_find_alloc(index, 0); | |
517 | } | |
518 | ||
5b6dd868 BS |
519 | #if defined(CONFIG_USER_ONLY) |
520 | /* Currently it is not recommended to allocate big chunks of data in | |
521 | user mode. It will change when a dedicated libc will be used. */ | |
522 | /* ??? 64-bit hosts ought to have no problem mmaping data outside the | |
523 | region in which the guest needs to run. Revisit this. */ | |
524 | #define USE_STATIC_CODE_GEN_BUFFER | |
525 | #endif | |
526 | ||
5b6dd868 BS |
527 | /* Minimum size of the code gen buffer. This number is randomly chosen, |
528 | but not so small that we can't have a fair number of TB's live. */ | |
529 | #define MIN_CODE_GEN_BUFFER_SIZE (1024u * 1024) | |
530 | ||
531 | /* Maximum size of the code gen buffer we'd like to use. Unless otherwise | |
532 | indicated, this is constrained by the range of direct branches on the | |
533 | host cpu, as used by the TCG implementation of goto_tb. */ | |
534 | #if defined(__x86_64__) | |
535 | # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024) | |
536 | #elif defined(__sparc__) | |
537 | # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024) | |
5bfd75a3 RH |
538 | #elif defined(__powerpc64__) |
539 | # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024) | |
399f1648 SF |
540 | #elif defined(__powerpc__) |
541 | # define MAX_CODE_GEN_BUFFER_SIZE (32u * 1024 * 1024) | |
4a136e0a CF |
542 | #elif defined(__aarch64__) |
543 | # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024) | |
5b6dd868 BS |
544 | #elif defined(__arm__) |
545 | # define MAX_CODE_GEN_BUFFER_SIZE (16u * 1024 * 1024) | |
546 | #elif defined(__s390x__) | |
547 | /* We have a +- 4GB range on the branches; leave some slop. */ | |
548 | # define MAX_CODE_GEN_BUFFER_SIZE (3ul * 1024 * 1024 * 1024) | |
479eb121 RH |
549 | #elif defined(__mips__) |
550 | /* We have a 256MB branch region, but leave room to make sure the | |
551 | main executable is also within that region. */ | |
552 | # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024) | |
5b6dd868 BS |
553 | #else |
554 | # define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1) | |
555 | #endif | |
556 | ||
557 | #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32u * 1024 * 1024) | |
558 | ||
559 | #define DEFAULT_CODE_GEN_BUFFER_SIZE \ | |
560 | (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \ | |
561 | ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE) | |
562 | ||
563 | static inline size_t size_code_gen_buffer(size_t tb_size) | |
564 | { | |
565 | /* Size the buffer. */ | |
566 | if (tb_size == 0) { | |
567 | #ifdef USE_STATIC_CODE_GEN_BUFFER | |
568 | tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE; | |
569 | #else | |
570 | /* ??? Needs adjustments. */ | |
571 | /* ??? If we relax the requirement that CONFIG_USER_ONLY use the | |
572 | static buffer, we could size this on RESERVED_VA, on the text | |
573 | segment size of the executable, or continue to use the default. */ | |
574 | tb_size = (unsigned long)(ram_size / 4); | |
575 | #endif | |
576 | } | |
577 | if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) { | |
578 | tb_size = MIN_CODE_GEN_BUFFER_SIZE; | |
579 | } | |
580 | if (tb_size > MAX_CODE_GEN_BUFFER_SIZE) { | |
581 | tb_size = MAX_CODE_GEN_BUFFER_SIZE; | |
582 | } | |
5b6dd868 BS |
583 | return tb_size; |
584 | } | |
585 | ||
483c76e1 RH |
586 | #ifdef __mips__ |
587 | /* In order to use J and JAL within the code_gen_buffer, we require | |
588 | that the buffer not cross a 256MB boundary. */ | |
589 | static inline bool cross_256mb(void *addr, size_t size) | |
590 | { | |
7ba6a512 | 591 | return ((uintptr_t)addr ^ ((uintptr_t)addr + size)) & ~0x0ffffffful; |
483c76e1 RH |
592 | } |
593 | ||
594 | /* We weren't able to allocate a buffer without crossing that boundary, | |
595 | so make do with the larger portion of the buffer that doesn't cross. | |
596 | Returns the new base of the buffer, and adjusts code_gen_buffer_size. */ | |
597 | static inline void *split_cross_256mb(void *buf1, size_t size1) | |
598 | { | |
7ba6a512 | 599 | void *buf2 = (void *)(((uintptr_t)buf1 + size1) & ~0x0ffffffful); |
483c76e1 RH |
600 | size_t size2 = buf1 + size1 - buf2; |
601 | ||
602 | size1 = buf2 - buf1; | |
603 | if (size1 < size2) { | |
604 | size1 = size2; | |
605 | buf1 = buf2; | |
606 | } | |
607 | ||
608 | tcg_ctx.code_gen_buffer_size = size1; | |
609 | return buf1; | |
610 | } | |
611 | #endif | |
612 | ||
5b6dd868 BS |
613 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
614 | static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE] | |
615 | __attribute__((aligned(CODE_GEN_ALIGN))); | |
616 | ||
f293709c RH |
617 | # ifdef _WIN32 |
618 | static inline void do_protect(void *addr, long size, int prot) | |
619 | { | |
620 | DWORD old_protect; | |
621 | VirtualProtect(addr, size, prot, &old_protect); | |
622 | } | |
623 | ||
624 | static inline void map_exec(void *addr, long size) | |
625 | { | |
626 | do_protect(addr, size, PAGE_EXECUTE_READWRITE); | |
627 | } | |
628 | ||
629 | static inline void map_none(void *addr, long size) | |
630 | { | |
631 | do_protect(addr, size, PAGE_NOACCESS); | |
632 | } | |
633 | # else | |
634 | static inline void do_protect(void *addr, long size, int prot) | |
635 | { | |
636 | uintptr_t start, end; | |
637 | ||
638 | start = (uintptr_t)addr; | |
639 | start &= qemu_real_host_page_mask; | |
640 | ||
641 | end = (uintptr_t)addr + size; | |
642 | end = ROUND_UP(end, qemu_real_host_page_size); | |
643 | ||
644 | mprotect((void *)start, end - start, prot); | |
645 | } | |
646 | ||
647 | static inline void map_exec(void *addr, long size) | |
648 | { | |
649 | do_protect(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC); | |
650 | } | |
651 | ||
652 | static inline void map_none(void *addr, long size) | |
653 | { | |
654 | do_protect(addr, size, PROT_NONE); | |
655 | } | |
656 | # endif /* WIN32 */ | |
657 | ||
5b6dd868 BS |
658 | static inline void *alloc_code_gen_buffer(void) |
659 | { | |
483c76e1 | 660 | void *buf = static_code_gen_buffer; |
f293709c RH |
661 | size_t full_size, size; |
662 | ||
663 | /* The size of the buffer, rounded down to end on a page boundary. */ | |
664 | full_size = (((uintptr_t)buf + sizeof(static_code_gen_buffer)) | |
665 | & qemu_real_host_page_mask) - (uintptr_t)buf; | |
666 | ||
667 | /* Reserve a guard page. */ | |
668 | size = full_size - qemu_real_host_page_size; | |
669 | ||
670 | /* Honor a command-line option limiting the size of the buffer. */ | |
671 | if (size > tcg_ctx.code_gen_buffer_size) { | |
672 | size = (((uintptr_t)buf + tcg_ctx.code_gen_buffer_size) | |
673 | & qemu_real_host_page_mask) - (uintptr_t)buf; | |
674 | } | |
675 | tcg_ctx.code_gen_buffer_size = size; | |
676 | ||
483c76e1 | 677 | #ifdef __mips__ |
f293709c RH |
678 | if (cross_256mb(buf, size)) { |
679 | buf = split_cross_256mb(buf, size); | |
680 | size = tcg_ctx.code_gen_buffer_size; | |
483c76e1 RH |
681 | } |
682 | #endif | |
f293709c RH |
683 | |
684 | map_exec(buf, size); | |
685 | map_none(buf + size, qemu_real_host_page_size); | |
686 | qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE); | |
687 | ||
483c76e1 | 688 | return buf; |
5b6dd868 | 689 | } |
f293709c RH |
690 | #elif defined(_WIN32) |
691 | static inline void *alloc_code_gen_buffer(void) | |
692 | { | |
693 | size_t size = tcg_ctx.code_gen_buffer_size; | |
694 | void *buf1, *buf2; | |
695 | ||
696 | /* Perform the allocation in two steps, so that the guard page | |
697 | is reserved but uncommitted. */ | |
698 | buf1 = VirtualAlloc(NULL, size + qemu_real_host_page_size, | |
699 | MEM_RESERVE, PAGE_NOACCESS); | |
700 | if (buf1 != NULL) { | |
701 | buf2 = VirtualAlloc(buf1, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); | |
702 | assert(buf1 == buf2); | |
703 | } | |
704 | ||
705 | return buf1; | |
706 | } | |
707 | #else | |
5b6dd868 BS |
708 | static inline void *alloc_code_gen_buffer(void) |
709 | { | |
710 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; | |
711 | uintptr_t start = 0; | |
f293709c | 712 | size_t size = tcg_ctx.code_gen_buffer_size; |
5b6dd868 BS |
713 | void *buf; |
714 | ||
715 | /* Constrain the position of the buffer based on the host cpu. | |
716 | Note that these addresses are chosen in concert with the | |
717 | addresses assigned in the relevant linker script file. */ | |
718 | # if defined(__PIE__) || defined(__PIC__) | |
719 | /* Don't bother setting a preferred location if we're building | |
720 | a position-independent executable. We're more likely to get | |
721 | an address near the main executable if we let the kernel | |
722 | choose the address. */ | |
723 | # elif defined(__x86_64__) && defined(MAP_32BIT) | |
724 | /* Force the memory down into low memory with the executable. | |
725 | Leave the choice of exact location with the kernel. */ | |
726 | flags |= MAP_32BIT; | |
727 | /* Cannot expect to map more than 800MB in low memory. */ | |
f293709c RH |
728 | if (size > 800u * 1024 * 1024) { |
729 | tcg_ctx.code_gen_buffer_size = size = 800u * 1024 * 1024; | |
5b6dd868 BS |
730 | } |
731 | # elif defined(__sparc__) | |
732 | start = 0x40000000ul; | |
733 | # elif defined(__s390x__) | |
734 | start = 0x90000000ul; | |
479eb121 | 735 | # elif defined(__mips__) |
f293709c | 736 | # if _MIPS_SIM == _ABI64 |
479eb121 RH |
737 | start = 0x128000000ul; |
738 | # else | |
739 | start = 0x08000000ul; | |
740 | # endif | |
5b6dd868 BS |
741 | # endif |
742 | ||
f293709c RH |
743 | buf = mmap((void *)start, size + qemu_real_host_page_size, |
744 | PROT_NONE, flags, -1, 0); | |
483c76e1 RH |
745 | if (buf == MAP_FAILED) { |
746 | return NULL; | |
747 | } | |
748 | ||
749 | #ifdef __mips__ | |
f293709c | 750 | if (cross_256mb(buf, size)) { |
5d831be2 | 751 | /* Try again, with the original still mapped, to avoid re-acquiring |
483c76e1 | 752 | that 256mb crossing. This time don't specify an address. */ |
f293709c RH |
753 | size_t size2; |
754 | void *buf2 = mmap(NULL, size + qemu_real_host_page_size, | |
755 | PROT_NONE, flags, -1, 0); | |
756 | switch (buf2 != MAP_FAILED) { | |
757 | case 1: | |
758 | if (!cross_256mb(buf2, size)) { | |
483c76e1 | 759 | /* Success! Use the new buffer. */ |
8bdf4997 | 760 | munmap(buf, size + qemu_real_host_page_size); |
f293709c | 761 | break; |
483c76e1 RH |
762 | } |
763 | /* Failure. Work with what we had. */ | |
8bdf4997 | 764 | munmap(buf2, size + qemu_real_host_page_size); |
f293709c RH |
765 | /* fallthru */ |
766 | default: | |
767 | /* Split the original buffer. Free the smaller half. */ | |
768 | buf2 = split_cross_256mb(buf, size); | |
769 | size2 = tcg_ctx.code_gen_buffer_size; | |
770 | if (buf == buf2) { | |
771 | munmap(buf + size2 + qemu_real_host_page_size, size - size2); | |
772 | } else { | |
773 | munmap(buf, size - size2); | |
774 | } | |
775 | size = size2; | |
776 | break; | |
483c76e1 | 777 | } |
f293709c | 778 | buf = buf2; |
483c76e1 RH |
779 | } |
780 | #endif | |
781 | ||
f293709c RH |
782 | /* Make the final buffer accessible. The guard page at the end |
783 | will remain inaccessible with PROT_NONE. */ | |
784 | mprotect(buf, size, PROT_WRITE | PROT_READ | PROT_EXEC); | |
483c76e1 | 785 | |
f293709c RH |
786 | /* Request large pages for the buffer. */ |
787 | qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE); | |
483c76e1 | 788 | |
5b6dd868 BS |
789 | return buf; |
790 | } | |
f293709c | 791 | #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */ |
5b6dd868 BS |
792 | |
793 | static inline void code_gen_alloc(size_t tb_size) | |
794 | { | |
0b0d3320 EV |
795 | tcg_ctx.code_gen_buffer_size = size_code_gen_buffer(tb_size); |
796 | tcg_ctx.code_gen_buffer = alloc_code_gen_buffer(); | |
797 | if (tcg_ctx.code_gen_buffer == NULL) { | |
5b6dd868 BS |
798 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); |
799 | exit(1); | |
800 | } | |
801 | ||
8163b749 RH |
802 | /* Estimate a good size for the number of TBs we can support. We |
803 | still haven't deducted the prologue from the buffer size here, | |
804 | but that's minimal and won't affect the estimate much. */ | |
805 | tcg_ctx.code_gen_max_blocks | |
806 | = tcg_ctx.code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; | |
807 | tcg_ctx.tb_ctx.tbs = g_new(TranslationBlock, tcg_ctx.code_gen_max_blocks); | |
808 | ||
677ef623 | 809 | qemu_mutex_init(&tcg_ctx.tb_ctx.tb_lock); |
5b6dd868 BS |
810 | } |
811 | ||
909eaac9 EC |
812 | static void tb_htable_init(void) |
813 | { | |
814 | unsigned int mode = QHT_MODE_AUTO_RESIZE; | |
815 | ||
816 | qht_init(&tcg_ctx.tb_ctx.htable, CODE_GEN_HTABLE_SIZE, mode); | |
817 | } | |
818 | ||
5b6dd868 BS |
819 | /* Must be called before using the QEMU cpus. 'tb_size' is the size |
820 | (in bytes) allocated to the translation buffer. Zero means default | |
821 | size. */ | |
822 | void tcg_exec_init(unsigned long tb_size) | |
823 | { | |
824 | cpu_gen_init(); | |
5b6dd868 | 825 | page_init(); |
909eaac9 | 826 | tb_htable_init(); |
f293709c | 827 | code_gen_alloc(tb_size); |
4cbea598 | 828 | #if defined(CONFIG_SOFTMMU) |
5b6dd868 BS |
829 | /* There's no guest base to take into account, so go ahead and |
830 | initialize the prologue now. */ | |
831 | tcg_prologue_init(&tcg_ctx); | |
832 | #endif | |
833 | } | |
834 | ||
835 | bool tcg_enabled(void) | |
836 | { | |
0b0d3320 | 837 | return tcg_ctx.code_gen_buffer != NULL; |
5b6dd868 BS |
838 | } |
839 | ||
7d7500d9 PB |
840 | /* |
841 | * Allocate a new translation block. Flush the translation buffer if | |
842 | * too many translation blocks or too much generated code. | |
843 | * | |
844 | * Called with tb_lock held. | |
845 | */ | |
5b6dd868 BS |
846 | static TranslationBlock *tb_alloc(target_ulong pc) |
847 | { | |
848 | TranslationBlock *tb; | |
849 | ||
e505a063 AB |
850 | assert_tb_lock(); |
851 | ||
b125f9dc | 852 | if (tcg_ctx.tb_ctx.nb_tbs >= tcg_ctx.code_gen_max_blocks) { |
5b6dd868 BS |
853 | return NULL; |
854 | } | |
5e5f07e0 | 855 | tb = &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs++]; |
5b6dd868 BS |
856 | tb->pc = pc; |
857 | tb->cflags = 0; | |
6d21e420 | 858 | tb->invalid = false; |
5b6dd868 BS |
859 | return tb; |
860 | } | |
861 | ||
7d7500d9 | 862 | /* Called with tb_lock held. */ |
5b6dd868 BS |
863 | void tb_free(TranslationBlock *tb) |
864 | { | |
e505a063 AB |
865 | assert_tb_lock(); |
866 | ||
5b6dd868 BS |
867 | /* In practice this is mostly used for single use temporary TB |
868 | Ignore the hard cases and just back up if this TB happens to | |
869 | be the last one generated. */ | |
5e5f07e0 EV |
870 | if (tcg_ctx.tb_ctx.nb_tbs > 0 && |
871 | tb == &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs - 1]) { | |
0b0d3320 | 872 | tcg_ctx.code_gen_ptr = tb->tc_ptr; |
5e5f07e0 | 873 | tcg_ctx.tb_ctx.nb_tbs--; |
5b6dd868 BS |
874 | } |
875 | } | |
876 | ||
877 | static inline void invalidate_page_bitmap(PageDesc *p) | |
878 | { | |
6fad459c | 879 | #ifdef CONFIG_SOFTMMU |
012aef07 MA |
880 | g_free(p->code_bitmap); |
881 | p->code_bitmap = NULL; | |
5b6dd868 | 882 | p->code_write_count = 0; |
6fad459c | 883 | #endif |
5b6dd868 BS |
884 | } |
885 | ||
886 | /* Set to NULL all the 'first_tb' fields in all PageDescs. */ | |
887 | static void page_flush_tb_1(int level, void **lp) | |
888 | { | |
889 | int i; | |
890 | ||
891 | if (*lp == NULL) { | |
892 | return; | |
893 | } | |
894 | if (level == 0) { | |
895 | PageDesc *pd = *lp; | |
896 | ||
03f49957 | 897 | for (i = 0; i < V_L2_SIZE; ++i) { |
5b6dd868 BS |
898 | pd[i].first_tb = NULL; |
899 | invalidate_page_bitmap(pd + i); | |
900 | } | |
901 | } else { | |
902 | void **pp = *lp; | |
903 | ||
03f49957 | 904 | for (i = 0; i < V_L2_SIZE; ++i) { |
5b6dd868 BS |
905 | page_flush_tb_1(level - 1, pp + i); |
906 | } | |
907 | } | |
908 | } | |
909 | ||
910 | static void page_flush_tb(void) | |
911 | { | |
66ec9f49 | 912 | int i, l1_sz = v_l1_size; |
5b6dd868 | 913 | |
66ec9f49 VK |
914 | for (i = 0; i < l1_sz; i++) { |
915 | page_flush_tb_1(v_l2_levels, l1_map + i); | |
5b6dd868 BS |
916 | } |
917 | } | |
918 | ||
919 | /* flush all the translation blocks */ | |
3359baad | 920 | static void do_tb_flush(CPUState *cpu, void *data) |
5b6dd868 | 921 | { |
3359baad SF |
922 | unsigned tb_flush_req = (unsigned) (uintptr_t) data; |
923 | ||
924 | tb_lock(); | |
925 | ||
926 | /* If it's already been done on request of another CPU, | |
927 | * just retry. | |
928 | */ | |
929 | if (tcg_ctx.tb_ctx.tb_flush_count != tb_flush_req) { | |
930 | goto done; | |
135a972b | 931 | } |
3359baad | 932 | |
955939a2 | 933 | #if defined(DEBUG_TB_FLUSH) |
5b6dd868 | 934 | printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n", |
0b0d3320 | 935 | (unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer), |
5e5f07e0 | 936 | tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.tb_ctx.nb_tbs > 0 ? |
0b0d3320 | 937 | ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer)) / |
5e5f07e0 | 938 | tcg_ctx.tb_ctx.nb_tbs : 0); |
5b6dd868 | 939 | #endif |
0b0d3320 EV |
940 | if ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer) |
941 | > tcg_ctx.code_gen_buffer_size) { | |
a47dddd7 | 942 | cpu_abort(cpu, "Internal error: code buffer overflow\n"); |
5b6dd868 | 943 | } |
5b6dd868 | 944 | |
bdc44640 | 945 | CPU_FOREACH(cpu) { |
89a16b1e SF |
946 | int i; |
947 | ||
948 | for (i = 0; i < TB_JMP_CACHE_SIZE; ++i) { | |
949 | atomic_set(&cpu->tb_jmp_cache[i], NULL); | |
950 | } | |
5b6dd868 BS |
951 | } |
952 | ||
118b0730 | 953 | tcg_ctx.tb_ctx.nb_tbs = 0; |
909eaac9 | 954 | qht_reset_size(&tcg_ctx.tb_ctx.htable, CODE_GEN_HTABLE_SIZE); |
5b6dd868 BS |
955 | page_flush_tb(); |
956 | ||
0b0d3320 | 957 | tcg_ctx.code_gen_ptr = tcg_ctx.code_gen_buffer; |
5b6dd868 BS |
958 | /* XXX: flush processor icache at this point if cache flush is |
959 | expensive */ | |
3359baad SF |
960 | atomic_mb_set(&tcg_ctx.tb_ctx.tb_flush_count, |
961 | tcg_ctx.tb_ctx.tb_flush_count + 1); | |
962 | ||
963 | done: | |
964 | tb_unlock(); | |
965 | } | |
966 | ||
967 | void tb_flush(CPUState *cpu) | |
968 | { | |
969 | if (tcg_enabled()) { | |
970 | uintptr_t tb_flush_req = atomic_mb_read(&tcg_ctx.tb_ctx.tb_flush_count); | |
971 | async_safe_run_on_cpu(cpu, do_tb_flush, (void *) tb_flush_req); | |
972 | } | |
5b6dd868 BS |
973 | } |
974 | ||
975 | #ifdef DEBUG_TB_CHECK | |
976 | ||
909eaac9 EC |
977 | static void |
978 | do_tb_invalidate_check(struct qht *ht, void *p, uint32_t hash, void *userp) | |
5b6dd868 | 979 | { |
909eaac9 EC |
980 | TranslationBlock *tb = p; |
981 | target_ulong addr = *(target_ulong *)userp; | |
982 | ||
983 | if (!(addr + TARGET_PAGE_SIZE <= tb->pc || addr >= tb->pc + tb->size)) { | |
984 | printf("ERROR invalidate: address=" TARGET_FMT_lx | |
985 | " PC=%08lx size=%04x\n", addr, (long)tb->pc, tb->size); | |
986 | } | |
987 | } | |
5b6dd868 | 988 | |
7d7500d9 PB |
989 | /* verify that all the pages have correct rights for code |
990 | * | |
991 | * Called with tb_lock held. | |
992 | */ | |
909eaac9 EC |
993 | static void tb_invalidate_check(target_ulong address) |
994 | { | |
5b6dd868 | 995 | address &= TARGET_PAGE_MASK; |
909eaac9 EC |
996 | qht_iter(&tcg_ctx.tb_ctx.htable, do_tb_invalidate_check, &address); |
997 | } | |
998 | ||
999 | static void | |
1000 | do_tb_page_check(struct qht *ht, void *p, uint32_t hash, void *userp) | |
1001 | { | |
1002 | TranslationBlock *tb = p; | |
1003 | int flags1, flags2; | |
1004 | ||
1005 | flags1 = page_get_flags(tb->pc); | |
1006 | flags2 = page_get_flags(tb->pc + tb->size - 1); | |
1007 | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) { | |
1008 | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n", | |
1009 | (long)tb->pc, tb->size, flags1, flags2); | |
5b6dd868 BS |
1010 | } |
1011 | } | |
1012 | ||
1013 | /* verify that all the pages have correct rights for code */ | |
1014 | static void tb_page_check(void) | |
1015 | { | |
909eaac9 | 1016 | qht_iter(&tcg_ctx.tb_ctx.htable, do_tb_page_check, NULL); |
5b6dd868 BS |
1017 | } |
1018 | ||
1019 | #endif | |
1020 | ||
5b6dd868 BS |
1021 | static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) |
1022 | { | |
1023 | TranslationBlock *tb1; | |
1024 | unsigned int n1; | |
1025 | ||
1026 | for (;;) { | |
1027 | tb1 = *ptb; | |
1028 | n1 = (uintptr_t)tb1 & 3; | |
1029 | tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3); | |
1030 | if (tb1 == tb) { | |
1031 | *ptb = tb1->page_next[n1]; | |
1032 | break; | |
1033 | } | |
1034 | ptb = &tb1->page_next[n1]; | |
1035 | } | |
1036 | } | |
1037 | ||
13362678 SF |
1038 | /* remove the TB from a list of TBs jumping to the n-th jump target of the TB */ |
1039 | static inline void tb_remove_from_jmp_list(TranslationBlock *tb, int n) | |
5b6dd868 | 1040 | { |
c37e6d7e SF |
1041 | TranslationBlock *tb1; |
1042 | uintptr_t *ptb, ntb; | |
5b6dd868 BS |
1043 | unsigned int n1; |
1044 | ||
f309101c | 1045 | ptb = &tb->jmp_list_next[n]; |
c37e6d7e | 1046 | if (*ptb) { |
5b6dd868 BS |
1047 | /* find tb(n) in circular list */ |
1048 | for (;;) { | |
c37e6d7e SF |
1049 | ntb = *ptb; |
1050 | n1 = ntb & 3; | |
1051 | tb1 = (TranslationBlock *)(ntb & ~3); | |
5b6dd868 BS |
1052 | if (n1 == n && tb1 == tb) { |
1053 | break; | |
1054 | } | |
1055 | if (n1 == 2) { | |
f309101c | 1056 | ptb = &tb1->jmp_list_first; |
5b6dd868 | 1057 | } else { |
f309101c | 1058 | ptb = &tb1->jmp_list_next[n1]; |
5b6dd868 BS |
1059 | } |
1060 | } | |
1061 | /* now we can suppress tb(n) from the list */ | |
f309101c | 1062 | *ptb = tb->jmp_list_next[n]; |
5b6dd868 | 1063 | |
c37e6d7e | 1064 | tb->jmp_list_next[n] = (uintptr_t)NULL; |
5b6dd868 BS |
1065 | } |
1066 | } | |
1067 | ||
1068 | /* reset the jump entry 'n' of a TB so that it is not chained to | |
1069 | another TB */ | |
1070 | static inline void tb_reset_jump(TranslationBlock *tb, int n) | |
1071 | { | |
f309101c SF |
1072 | uintptr_t addr = (uintptr_t)(tb->tc_ptr + tb->jmp_reset_offset[n]); |
1073 | tb_set_jmp_target(tb, n, addr); | |
5b6dd868 BS |
1074 | } |
1075 | ||
89bba496 SF |
1076 | /* remove any jumps to the TB */ |
1077 | static inline void tb_jmp_unlink(TranslationBlock *tb) | |
1078 | { | |
f9c5b66f SF |
1079 | TranslationBlock *tb1; |
1080 | uintptr_t *ptb, ntb; | |
89bba496 SF |
1081 | unsigned int n1; |
1082 | ||
f9c5b66f | 1083 | ptb = &tb->jmp_list_first; |
89bba496 | 1084 | for (;;) { |
f9c5b66f SF |
1085 | ntb = *ptb; |
1086 | n1 = ntb & 3; | |
1087 | tb1 = (TranslationBlock *)(ntb & ~3); | |
89bba496 SF |
1088 | if (n1 == 2) { |
1089 | break; | |
1090 | } | |
f9c5b66f SF |
1091 | tb_reset_jump(tb1, n1); |
1092 | *ptb = tb1->jmp_list_next[n1]; | |
1093 | tb1->jmp_list_next[n1] = (uintptr_t)NULL; | |
89bba496 | 1094 | } |
89bba496 SF |
1095 | } |
1096 | ||
7d7500d9 PB |
1097 | /* invalidate one TB |
1098 | * | |
1099 | * Called with tb_lock held. | |
1100 | */ | |
5b6dd868 BS |
1101 | void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) |
1102 | { | |
182735ef | 1103 | CPUState *cpu; |
5b6dd868 | 1104 | PageDesc *p; |
42bd3228 | 1105 | uint32_t h; |
5b6dd868 | 1106 | tb_page_addr_t phys_pc; |
5b6dd868 | 1107 | |
e505a063 AB |
1108 | assert_tb_lock(); |
1109 | ||
6d21e420 PB |
1110 | atomic_set(&tb->invalid, true); |
1111 | ||
5b6dd868 BS |
1112 | /* remove the TB from the hash list */ |
1113 | phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); | |
42bd3228 | 1114 | h = tb_hash_func(phys_pc, tb->pc, tb->flags); |
909eaac9 | 1115 | qht_remove(&tcg_ctx.tb_ctx.htable, tb, h); |
5b6dd868 BS |
1116 | |
1117 | /* remove the TB from the page list */ | |
1118 | if (tb->page_addr[0] != page_addr) { | |
1119 | p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS); | |
1120 | tb_page_remove(&p->first_tb, tb); | |
1121 | invalidate_page_bitmap(p); | |
1122 | } | |
1123 | if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) { | |
1124 | p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS); | |
1125 | tb_page_remove(&p->first_tb, tb); | |
1126 | invalidate_page_bitmap(p); | |
1127 | } | |
1128 | ||
5b6dd868 BS |
1129 | /* remove the TB from the hash list */ |
1130 | h = tb_jmp_cache_hash_func(tb->pc); | |
bdc44640 | 1131 | CPU_FOREACH(cpu) { |
89a16b1e SF |
1132 | if (atomic_read(&cpu->tb_jmp_cache[h]) == tb) { |
1133 | atomic_set(&cpu->tb_jmp_cache[h], NULL); | |
5b6dd868 BS |
1134 | } |
1135 | } | |
1136 | ||
1137 | /* suppress this TB from the two jump lists */ | |
13362678 SF |
1138 | tb_remove_from_jmp_list(tb, 0); |
1139 | tb_remove_from_jmp_list(tb, 1); | |
5b6dd868 BS |
1140 | |
1141 | /* suppress any remaining jumps to this TB */ | |
89bba496 | 1142 | tb_jmp_unlink(tb); |
5b6dd868 | 1143 | |
5e5f07e0 | 1144 | tcg_ctx.tb_ctx.tb_phys_invalidate_count++; |
5b6dd868 BS |
1145 | } |
1146 | ||
6fad459c | 1147 | #ifdef CONFIG_SOFTMMU |
5b6dd868 BS |
1148 | static void build_page_bitmap(PageDesc *p) |
1149 | { | |
1150 | int n, tb_start, tb_end; | |
1151 | TranslationBlock *tb; | |
1152 | ||
510a647f | 1153 | p->code_bitmap = bitmap_new(TARGET_PAGE_SIZE); |
5b6dd868 BS |
1154 | |
1155 | tb = p->first_tb; | |
1156 | while (tb != NULL) { | |
1157 | n = (uintptr_t)tb & 3; | |
1158 | tb = (TranslationBlock *)((uintptr_t)tb & ~3); | |
1159 | /* NOTE: this is subtle as a TB may span two physical pages */ | |
1160 | if (n == 0) { | |
1161 | /* NOTE: tb_end may be after the end of the page, but | |
1162 | it is not a problem */ | |
1163 | tb_start = tb->pc & ~TARGET_PAGE_MASK; | |
1164 | tb_end = tb_start + tb->size; | |
1165 | if (tb_end > TARGET_PAGE_SIZE) { | |
1166 | tb_end = TARGET_PAGE_SIZE; | |
e505a063 | 1167 | } |
5b6dd868 BS |
1168 | } else { |
1169 | tb_start = 0; | |
1170 | tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); | |
1171 | } | |
510a647f | 1172 | bitmap_set(p->code_bitmap, tb_start, tb_end - tb_start); |
5b6dd868 BS |
1173 | tb = tb->page_next[n]; |
1174 | } | |
1175 | } | |
6fad459c | 1176 | #endif |
5b6dd868 | 1177 | |
e90d96b1 SF |
1178 | /* add the tb in the target page and protect it if necessary |
1179 | * | |
1180 | * Called with mmap_lock held for user-mode emulation. | |
1181 | */ | |
1182 | static inline void tb_alloc_page(TranslationBlock *tb, | |
1183 | unsigned int n, tb_page_addr_t page_addr) | |
1184 | { | |
1185 | PageDesc *p; | |
1186 | #ifndef CONFIG_USER_ONLY | |
1187 | bool page_already_protected; | |
1188 | #endif | |
1189 | ||
e505a063 AB |
1190 | assert_memory_lock(); |
1191 | ||
e90d96b1 SF |
1192 | tb->page_addr[n] = page_addr; |
1193 | p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1); | |
1194 | tb->page_next[n] = p->first_tb; | |
1195 | #ifndef CONFIG_USER_ONLY | |
1196 | page_already_protected = p->first_tb != NULL; | |
1197 | #endif | |
1198 | p->first_tb = (TranslationBlock *)((uintptr_t)tb | n); | |
1199 | invalidate_page_bitmap(p); | |
1200 | ||
1201 | #if defined(CONFIG_USER_ONLY) | |
1202 | if (p->flags & PAGE_WRITE) { | |
1203 | target_ulong addr; | |
1204 | PageDesc *p2; | |
1205 | int prot; | |
1206 | ||
1207 | /* force the host page as non writable (writes will have a | |
1208 | page fault + mprotect overhead) */ | |
1209 | page_addr &= qemu_host_page_mask; | |
1210 | prot = 0; | |
1211 | for (addr = page_addr; addr < page_addr + qemu_host_page_size; | |
1212 | addr += TARGET_PAGE_SIZE) { | |
1213 | ||
1214 | p2 = page_find(addr >> TARGET_PAGE_BITS); | |
1215 | if (!p2) { | |
1216 | continue; | |
1217 | } | |
1218 | prot |= p2->flags; | |
1219 | p2->flags &= ~PAGE_WRITE; | |
1220 | } | |
1221 | mprotect(g2h(page_addr), qemu_host_page_size, | |
1222 | (prot & PAGE_BITS) & ~PAGE_WRITE); | |
1223 | #ifdef DEBUG_TB_INVALIDATE | |
1224 | printf("protecting code page: 0x" TARGET_FMT_lx "\n", | |
1225 | page_addr); | |
1226 | #endif | |
1227 | } | |
1228 | #else | |
1229 | /* if some code is already present, then the pages are already | |
1230 | protected. So we handle the case where only the first TB is | |
1231 | allocated in a physical page */ | |
1232 | if (!page_already_protected) { | |
1233 | tlb_protect_code(page_addr); | |
1234 | } | |
1235 | #endif | |
1236 | } | |
1237 | ||
1238 | /* add a new TB and link it to the physical page tables. phys_page2 is | |
1239 | * (-1) to indicate that only one page contains the TB. | |
1240 | * | |
1241 | * Called with mmap_lock held for user-mode emulation. | |
1242 | */ | |
1243 | static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc, | |
1244 | tb_page_addr_t phys_page2) | |
1245 | { | |
42bd3228 | 1246 | uint32_t h; |
e90d96b1 | 1247 | |
e505a063 AB |
1248 | assert_memory_lock(); |
1249 | ||
e90d96b1 SF |
1250 | /* add in the page list */ |
1251 | tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK); | |
1252 | if (phys_page2 != -1) { | |
1253 | tb_alloc_page(tb, 1, phys_page2); | |
1254 | } else { | |
1255 | tb->page_addr[1] = -1; | |
1256 | } | |
1257 | ||
2e1ae44a AB |
1258 | /* add in the hash table */ |
1259 | h = tb_hash_func(phys_pc, tb->pc, tb->flags); | |
1260 | qht_insert(&tcg_ctx.tb_ctx.htable, tb, h); | |
1261 | ||
e90d96b1 SF |
1262 | #ifdef DEBUG_TB_CHECK |
1263 | tb_page_check(); | |
1264 | #endif | |
1265 | } | |
1266 | ||
75692087 | 1267 | /* Called with mmap_lock held for user mode emulation. */ |
648f034c | 1268 | TranslationBlock *tb_gen_code(CPUState *cpu, |
5b6dd868 | 1269 | target_ulong pc, target_ulong cs_base, |
89fee74a | 1270 | uint32_t flags, int cflags) |
5b6dd868 | 1271 | { |
648f034c | 1272 | CPUArchState *env = cpu->env_ptr; |
5b6dd868 | 1273 | TranslationBlock *tb; |
5b6dd868 BS |
1274 | tb_page_addr_t phys_pc, phys_page2; |
1275 | target_ulong virt_page2; | |
fec88f64 | 1276 | tcg_insn_unit *gen_code_buf; |
fca8a500 | 1277 | int gen_code_size, search_size; |
fec88f64 RH |
1278 | #ifdef CONFIG_PROFILER |
1279 | int64_t ti; | |
1280 | #endif | |
e505a063 | 1281 | assert_memory_lock(); |
5b6dd868 BS |
1282 | |
1283 | phys_pc = get_page_addr_code(env, pc); | |
56c0269a | 1284 | if (use_icount && !(cflags & CF_IGNORE_ICOUNT)) { |
0266359e PB |
1285 | cflags |= CF_USE_ICOUNT; |
1286 | } | |
b125f9dc | 1287 | |
5b6dd868 | 1288 | tb = tb_alloc(pc); |
b125f9dc RH |
1289 | if (unlikely(!tb)) { |
1290 | buffer_overflow: | |
5b6dd868 | 1291 | /* flush must be done */ |
bbd77c18 | 1292 | tb_flush(cpu); |
3359baad SF |
1293 | mmap_unlock(); |
1294 | cpu_loop_exit(cpu); | |
5b6dd868 | 1295 | } |
fec88f64 RH |
1296 | |
1297 | gen_code_buf = tcg_ctx.code_gen_ptr; | |
1298 | tb->tc_ptr = gen_code_buf; | |
5b6dd868 BS |
1299 | tb->cs_base = cs_base; |
1300 | tb->flags = flags; | |
1301 | tb->cflags = cflags; | |
fec88f64 RH |
1302 | |
1303 | #ifdef CONFIG_PROFILER | |
1304 | tcg_ctx.tb_count1++; /* includes aborted translations because of | |
1305 | exceptions */ | |
1306 | ti = profile_getclock(); | |
1307 | #endif | |
1308 | ||
1309 | tcg_func_start(&tcg_ctx); | |
1310 | ||
7c255043 | 1311 | tcg_ctx.cpu = ENV_GET_CPU(env); |
fec88f64 | 1312 | gen_intermediate_code(env, tb); |
7c255043 | 1313 | tcg_ctx.cpu = NULL; |
fec88f64 RH |
1314 | |
1315 | trace_translate_block(tb, tb->pc, tb->tc_ptr); | |
1316 | ||
1317 | /* generate machine code */ | |
f309101c SF |
1318 | tb->jmp_reset_offset[0] = TB_JMP_RESET_OFFSET_INVALID; |
1319 | tb->jmp_reset_offset[1] = TB_JMP_RESET_OFFSET_INVALID; | |
1320 | tcg_ctx.tb_jmp_reset_offset = tb->jmp_reset_offset; | |
fec88f64 | 1321 | #ifdef USE_DIRECT_JUMP |
f309101c SF |
1322 | tcg_ctx.tb_jmp_insn_offset = tb->jmp_insn_offset; |
1323 | tcg_ctx.tb_jmp_target_addr = NULL; | |
fec88f64 | 1324 | #else |
f309101c SF |
1325 | tcg_ctx.tb_jmp_insn_offset = NULL; |
1326 | tcg_ctx.tb_jmp_target_addr = tb->jmp_target_addr; | |
fec88f64 RH |
1327 | #endif |
1328 | ||
1329 | #ifdef CONFIG_PROFILER | |
1330 | tcg_ctx.tb_count++; | |
1331 | tcg_ctx.interm_time += profile_getclock() - ti; | |
1332 | tcg_ctx.code_time -= profile_getclock(); | |
1333 | #endif | |
1334 | ||
b125f9dc RH |
1335 | /* ??? Overflow could be handled better here. In particular, we |
1336 | don't need to re-do gen_intermediate_code, nor should we re-do | |
1337 | the tcg optimization currently hidden inside tcg_gen_code. All | |
1338 | that should be required is to flush the TBs, allocate a new TB, | |
1339 | re-initialize it per above, and re-do the actual code generation. */ | |
5bd2ec3d | 1340 | gen_code_size = tcg_gen_code(&tcg_ctx, tb); |
b125f9dc RH |
1341 | if (unlikely(gen_code_size < 0)) { |
1342 | goto buffer_overflow; | |
1343 | } | |
fca8a500 | 1344 | search_size = encode_search(tb, (void *)gen_code_buf + gen_code_size); |
b125f9dc RH |
1345 | if (unlikely(search_size < 0)) { |
1346 | goto buffer_overflow; | |
1347 | } | |
fec88f64 RH |
1348 | |
1349 | #ifdef CONFIG_PROFILER | |
1350 | tcg_ctx.code_time += profile_getclock(); | |
1351 | tcg_ctx.code_in_len += tb->size; | |
1352 | tcg_ctx.code_out_len += gen_code_size; | |
fca8a500 | 1353 | tcg_ctx.search_out_len += search_size; |
fec88f64 RH |
1354 | #endif |
1355 | ||
1356 | #ifdef DEBUG_DISAS | |
d977e1c2 AB |
1357 | if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) && |
1358 | qemu_log_in_addr_range(tb->pc)) { | |
fec88f64 RH |
1359 | qemu_log("OUT: [size=%d]\n", gen_code_size); |
1360 | log_disas(tb->tc_ptr, gen_code_size); | |
1361 | qemu_log("\n"); | |
1362 | qemu_log_flush(); | |
1363 | } | |
1364 | #endif | |
1365 | ||
fca8a500 RH |
1366 | tcg_ctx.code_gen_ptr = (void *) |
1367 | ROUND_UP((uintptr_t)gen_code_buf + gen_code_size + search_size, | |
1368 | CODE_GEN_ALIGN); | |
5b6dd868 | 1369 | |
901bc3de SF |
1370 | /* init jump list */ |
1371 | assert(((uintptr_t)tb & 3) == 0); | |
1372 | tb->jmp_list_first = (uintptr_t)tb | 2; | |
1373 | tb->jmp_list_next[0] = (uintptr_t)NULL; | |
1374 | tb->jmp_list_next[1] = (uintptr_t)NULL; | |
1375 | ||
1376 | /* init original jump addresses wich has been set during tcg_gen_code() */ | |
1377 | if (tb->jmp_reset_offset[0] != TB_JMP_RESET_OFFSET_INVALID) { | |
1378 | tb_reset_jump(tb, 0); | |
1379 | } | |
1380 | if (tb->jmp_reset_offset[1] != TB_JMP_RESET_OFFSET_INVALID) { | |
1381 | tb_reset_jump(tb, 1); | |
1382 | } | |
1383 | ||
5b6dd868 BS |
1384 | /* check next page if needed */ |
1385 | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK; | |
1386 | phys_page2 = -1; | |
1387 | if ((pc & TARGET_PAGE_MASK) != virt_page2) { | |
1388 | phys_page2 = get_page_addr_code(env, virt_page2); | |
1389 | } | |
901bc3de SF |
1390 | /* As long as consistency of the TB stuff is provided by tb_lock in user |
1391 | * mode and is implicit in single-threaded softmmu emulation, no explicit | |
1392 | * memory barrier is required before tb_link_page() makes the TB visible | |
1393 | * through the physical hash table and physical page list. | |
1394 | */ | |
5b6dd868 BS |
1395 | tb_link_page(tb, phys_pc, phys_page2); |
1396 | return tb; | |
1397 | } | |
1398 | ||
1399 | /* | |
1400 | * Invalidate all TBs which intersect with the target physical address range | |
1401 | * [start;end[. NOTE: start and end may refer to *different* physical pages. | |
1402 | * 'is_cpu_write_access' should be true if called from a real cpu write | |
1403 | * access: the virtual CPU will exit the current TB if code is modified inside | |
1404 | * this TB. | |
75692087 PB |
1405 | * |
1406 | * Called with mmap_lock held for user-mode emulation | |
5b6dd868 | 1407 | */ |
35865339 | 1408 | void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end) |
5b6dd868 | 1409 | { |
e505a063 AB |
1410 | assert_memory_lock(); |
1411 | ||
5b6dd868 | 1412 | while (start < end) { |
35865339 | 1413 | tb_invalidate_phys_page_range(start, end, 0); |
5b6dd868 BS |
1414 | start &= TARGET_PAGE_MASK; |
1415 | start += TARGET_PAGE_SIZE; | |
1416 | } | |
1417 | } | |
1418 | ||
1419 | /* | |
1420 | * Invalidate all TBs which intersect with the target physical address range | |
1421 | * [start;end[. NOTE: start and end must refer to the *same* physical page. | |
1422 | * 'is_cpu_write_access' should be true if called from a real cpu write | |
1423 | * access: the virtual CPU will exit the current TB if code is modified inside | |
1424 | * this TB. | |
75692087 PB |
1425 | * |
1426 | * Called with mmap_lock held for user-mode emulation | |
5b6dd868 BS |
1427 | */ |
1428 | void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end, | |
1429 | int is_cpu_write_access) | |
1430 | { | |
3213525f | 1431 | TranslationBlock *tb, *tb_next; |
baea4fae | 1432 | #if defined(TARGET_HAS_PRECISE_SMC) |
3213525f | 1433 | CPUState *cpu = current_cpu; |
4917cf44 AF |
1434 | CPUArchState *env = NULL; |
1435 | #endif | |
5b6dd868 BS |
1436 | tb_page_addr_t tb_start, tb_end; |
1437 | PageDesc *p; | |
1438 | int n; | |
1439 | #ifdef TARGET_HAS_PRECISE_SMC | |
1440 | int current_tb_not_found = is_cpu_write_access; | |
1441 | TranslationBlock *current_tb = NULL; | |
1442 | int current_tb_modified = 0; | |
1443 | target_ulong current_pc = 0; | |
1444 | target_ulong current_cs_base = 0; | |
89fee74a | 1445 | uint32_t current_flags = 0; |
5b6dd868 BS |
1446 | #endif /* TARGET_HAS_PRECISE_SMC */ |
1447 | ||
e505a063 AB |
1448 | assert_memory_lock(); |
1449 | ||
5b6dd868 BS |
1450 | p = page_find(start >> TARGET_PAGE_BITS); |
1451 | if (!p) { | |
1452 | return; | |
1453 | } | |
baea4fae | 1454 | #if defined(TARGET_HAS_PRECISE_SMC) |
4917cf44 AF |
1455 | if (cpu != NULL) { |
1456 | env = cpu->env_ptr; | |
d77953b9 | 1457 | } |
4917cf44 | 1458 | #endif |
5b6dd868 BS |
1459 | |
1460 | /* we remove all the TBs in the range [start, end[ */ | |
1461 | /* XXX: see if in some cases it could be faster to invalidate all | |
1462 | the code */ | |
a5e99826 | 1463 | tb_lock(); |
5b6dd868 BS |
1464 | tb = p->first_tb; |
1465 | while (tb != NULL) { | |
1466 | n = (uintptr_t)tb & 3; | |
1467 | tb = (TranslationBlock *)((uintptr_t)tb & ~3); | |
1468 | tb_next = tb->page_next[n]; | |
1469 | /* NOTE: this is subtle as a TB may span two physical pages */ | |
1470 | if (n == 0) { | |
1471 | /* NOTE: tb_end may be after the end of the page, but | |
1472 | it is not a problem */ | |
1473 | tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); | |
1474 | tb_end = tb_start + tb->size; | |
1475 | } else { | |
1476 | tb_start = tb->page_addr[1]; | |
1477 | tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); | |
1478 | } | |
1479 | if (!(tb_end <= start || tb_start >= end)) { | |
1480 | #ifdef TARGET_HAS_PRECISE_SMC | |
1481 | if (current_tb_not_found) { | |
1482 | current_tb_not_found = 0; | |
1483 | current_tb = NULL; | |
93afeade | 1484 | if (cpu->mem_io_pc) { |
5b6dd868 | 1485 | /* now we have a real cpu fault */ |
93afeade | 1486 | current_tb = tb_find_pc(cpu->mem_io_pc); |
5b6dd868 BS |
1487 | } |
1488 | } | |
1489 | if (current_tb == tb && | |
1490 | (current_tb->cflags & CF_COUNT_MASK) != 1) { | |
1491 | /* If we are modifying the current TB, we must stop | |
1492 | its execution. We could be more precise by checking | |
1493 | that the modification is after the current PC, but it | |
1494 | would require a specialized function to partially | |
1495 | restore the CPU state */ | |
1496 | ||
1497 | current_tb_modified = 1; | |
74f10515 | 1498 | cpu_restore_state_from_tb(cpu, current_tb, cpu->mem_io_pc); |
5b6dd868 BS |
1499 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
1500 | ¤t_flags); | |
1501 | } | |
1502 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
5b6dd868 | 1503 | tb_phys_invalidate(tb, -1); |
5b6dd868 BS |
1504 | } |
1505 | tb = tb_next; | |
1506 | } | |
1507 | #if !defined(CONFIG_USER_ONLY) | |
1508 | /* if no code remaining, no need to continue to use slow writes */ | |
1509 | if (!p->first_tb) { | |
1510 | invalidate_page_bitmap(p); | |
fc377bcf | 1511 | tlb_unprotect_code(start); |
5b6dd868 BS |
1512 | } |
1513 | #endif | |
1514 | #ifdef TARGET_HAS_PRECISE_SMC | |
1515 | if (current_tb_modified) { | |
1516 | /* we generate a block containing just the instruction | |
1517 | modifying the memory. It will ensure that it cannot modify | |
1518 | itself */ | |
648f034c | 1519 | tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1); |
6886b980 | 1520 | cpu_loop_exit_noexc(cpu); |
5b6dd868 BS |
1521 | } |
1522 | #endif | |
a5e99826 | 1523 | tb_unlock(); |
5b6dd868 BS |
1524 | } |
1525 | ||
6fad459c | 1526 | #ifdef CONFIG_SOFTMMU |
5b6dd868 BS |
1527 | /* len must be <= 8 and start must be a multiple of len */ |
1528 | void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len) | |
1529 | { | |
1530 | PageDesc *p; | |
5b6dd868 BS |
1531 | |
1532 | #if 0 | |
1533 | if (1) { | |
1534 | qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n", | |
1535 | cpu_single_env->mem_io_vaddr, len, | |
1536 | cpu_single_env->eip, | |
1537 | cpu_single_env->eip + | |
1538 | (intptr_t)cpu_single_env->segs[R_CS].base); | |
1539 | } | |
1540 | #endif | |
1541 | p = page_find(start >> TARGET_PAGE_BITS); | |
1542 | if (!p) { | |
1543 | return; | |
1544 | } | |
fc377bcf PB |
1545 | if (!p->code_bitmap && |
1546 | ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD) { | |
7d7500d9 PB |
1547 | /* build code bitmap. FIXME: writes should be protected by |
1548 | * tb_lock, reads by tb_lock or RCU. | |
1549 | */ | |
fc377bcf PB |
1550 | build_page_bitmap(p); |
1551 | } | |
5b6dd868 | 1552 | if (p->code_bitmap) { |
510a647f EC |
1553 | unsigned int nr; |
1554 | unsigned long b; | |
1555 | ||
1556 | nr = start & ~TARGET_PAGE_MASK; | |
1557 | b = p->code_bitmap[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG - 1)); | |
5b6dd868 BS |
1558 | if (b & ((1 << len) - 1)) { |
1559 | goto do_invalidate; | |
1560 | } | |
1561 | } else { | |
1562 | do_invalidate: | |
1563 | tb_invalidate_phys_page_range(start, start + len, 1); | |
1564 | } | |
1565 | } | |
6fad459c | 1566 | #else |
75809229 PM |
1567 | /* Called with mmap_lock held. If pc is not 0 then it indicates the |
1568 | * host PC of the faulting store instruction that caused this invalidate. | |
1569 | * Returns true if the caller needs to abort execution of the current | |
1570 | * TB (because it was modified by this store and the guest CPU has | |
1571 | * precise-SMC semantics). | |
1572 | */ | |
1573 | static bool tb_invalidate_phys_page(tb_page_addr_t addr, uintptr_t pc) | |
5b6dd868 BS |
1574 | { |
1575 | TranslationBlock *tb; | |
1576 | PageDesc *p; | |
1577 | int n; | |
1578 | #ifdef TARGET_HAS_PRECISE_SMC | |
1579 | TranslationBlock *current_tb = NULL; | |
4917cf44 AF |
1580 | CPUState *cpu = current_cpu; |
1581 | CPUArchState *env = NULL; | |
5b6dd868 BS |
1582 | int current_tb_modified = 0; |
1583 | target_ulong current_pc = 0; | |
1584 | target_ulong current_cs_base = 0; | |
89fee74a | 1585 | uint32_t current_flags = 0; |
5b6dd868 BS |
1586 | #endif |
1587 | ||
1588 | addr &= TARGET_PAGE_MASK; | |
1589 | p = page_find(addr >> TARGET_PAGE_BITS); | |
1590 | if (!p) { | |
75809229 | 1591 | return false; |
5b6dd868 | 1592 | } |
a5e99826 FK |
1593 | |
1594 | tb_lock(); | |
5b6dd868 BS |
1595 | tb = p->first_tb; |
1596 | #ifdef TARGET_HAS_PRECISE_SMC | |
1597 | if (tb && pc != 0) { | |
1598 | current_tb = tb_find_pc(pc); | |
1599 | } | |
4917cf44 AF |
1600 | if (cpu != NULL) { |
1601 | env = cpu->env_ptr; | |
d77953b9 | 1602 | } |
5b6dd868 BS |
1603 | #endif |
1604 | while (tb != NULL) { | |
1605 | n = (uintptr_t)tb & 3; | |
1606 | tb = (TranslationBlock *)((uintptr_t)tb & ~3); | |
1607 | #ifdef TARGET_HAS_PRECISE_SMC | |
1608 | if (current_tb == tb && | |
1609 | (current_tb->cflags & CF_COUNT_MASK) != 1) { | |
1610 | /* If we are modifying the current TB, we must stop | |
1611 | its execution. We could be more precise by checking | |
1612 | that the modification is after the current PC, but it | |
1613 | would require a specialized function to partially | |
1614 | restore the CPU state */ | |
1615 | ||
1616 | current_tb_modified = 1; | |
74f10515 | 1617 | cpu_restore_state_from_tb(cpu, current_tb, pc); |
5b6dd868 BS |
1618 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
1619 | ¤t_flags); | |
1620 | } | |
1621 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
1622 | tb_phys_invalidate(tb, addr); | |
1623 | tb = tb->page_next[n]; | |
1624 | } | |
1625 | p->first_tb = NULL; | |
1626 | #ifdef TARGET_HAS_PRECISE_SMC | |
1627 | if (current_tb_modified) { | |
1628 | /* we generate a block containing just the instruction | |
1629 | modifying the memory. It will ensure that it cannot modify | |
1630 | itself */ | |
648f034c | 1631 | tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1); |
a5e99826 FK |
1632 | /* tb_lock will be reset after cpu_loop_exit_noexc longjmps |
1633 | * back into the cpu_exec loop. */ | |
75809229 | 1634 | return true; |
5b6dd868 BS |
1635 | } |
1636 | #endif | |
a5e99826 FK |
1637 | tb_unlock(); |
1638 | ||
75809229 | 1639 | return false; |
5b6dd868 BS |
1640 | } |
1641 | #endif | |
1642 | ||
5b6dd868 BS |
1643 | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < |
1644 | tb[1].tc_ptr. Return NULL if not found */ | |
a8a826a3 | 1645 | static TranslationBlock *tb_find_pc(uintptr_t tc_ptr) |
5b6dd868 BS |
1646 | { |
1647 | int m_min, m_max, m; | |
1648 | uintptr_t v; | |
1649 | TranslationBlock *tb; | |
1650 | ||
5e5f07e0 | 1651 | if (tcg_ctx.tb_ctx.nb_tbs <= 0) { |
5b6dd868 BS |
1652 | return NULL; |
1653 | } | |
0b0d3320 EV |
1654 | if (tc_ptr < (uintptr_t)tcg_ctx.code_gen_buffer || |
1655 | tc_ptr >= (uintptr_t)tcg_ctx.code_gen_ptr) { | |
5b6dd868 BS |
1656 | return NULL; |
1657 | } | |
1658 | /* binary search (cf Knuth) */ | |
1659 | m_min = 0; | |
5e5f07e0 | 1660 | m_max = tcg_ctx.tb_ctx.nb_tbs - 1; |
5b6dd868 BS |
1661 | while (m_min <= m_max) { |
1662 | m = (m_min + m_max) >> 1; | |
5e5f07e0 | 1663 | tb = &tcg_ctx.tb_ctx.tbs[m]; |
5b6dd868 BS |
1664 | v = (uintptr_t)tb->tc_ptr; |
1665 | if (v == tc_ptr) { | |
1666 | return tb; | |
1667 | } else if (tc_ptr < v) { | |
1668 | m_max = m - 1; | |
1669 | } else { | |
1670 | m_min = m + 1; | |
1671 | } | |
1672 | } | |
5e5f07e0 | 1673 | return &tcg_ctx.tb_ctx.tbs[m_max]; |
5b6dd868 BS |
1674 | } |
1675 | ||
ec53b45b | 1676 | #if !defined(CONFIG_USER_ONLY) |
29d8ec7b | 1677 | void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr) |
5b6dd868 BS |
1678 | { |
1679 | ram_addr_t ram_addr; | |
5c8a00ce | 1680 | MemoryRegion *mr; |
149f54b5 | 1681 | hwaddr l = 1; |
5b6dd868 | 1682 | |
41063e1e | 1683 | rcu_read_lock(); |
29d8ec7b | 1684 | mr = address_space_translate(as, addr, &addr, &l, false); |
5c8a00ce PB |
1685 | if (!(memory_region_is_ram(mr) |
1686 | || memory_region_is_romd(mr))) { | |
41063e1e | 1687 | rcu_read_unlock(); |
5b6dd868 BS |
1688 | return; |
1689 | } | |
e4e69794 | 1690 | ram_addr = memory_region_get_ram_addr(mr) + addr; |
5b6dd868 | 1691 | tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0); |
41063e1e | 1692 | rcu_read_unlock(); |
5b6dd868 | 1693 | } |
ec53b45b | 1694 | #endif /* !defined(CONFIG_USER_ONLY) */ |
5b6dd868 | 1695 | |
7d7500d9 | 1696 | /* Called with tb_lock held. */ |
239c51a5 | 1697 | void tb_check_watchpoint(CPUState *cpu) |
5b6dd868 BS |
1698 | { |
1699 | TranslationBlock *tb; | |
1700 | ||
93afeade | 1701 | tb = tb_find_pc(cpu->mem_io_pc); |
8d302e76 AJ |
1702 | if (tb) { |
1703 | /* We can use retranslation to find the PC. */ | |
1704 | cpu_restore_state_from_tb(cpu, tb, cpu->mem_io_pc); | |
1705 | tb_phys_invalidate(tb, -1); | |
1706 | } else { | |
1707 | /* The exception probably happened in a helper. The CPU state should | |
1708 | have been saved before calling it. Fetch the PC from there. */ | |
1709 | CPUArchState *env = cpu->env_ptr; | |
1710 | target_ulong pc, cs_base; | |
1711 | tb_page_addr_t addr; | |
89fee74a | 1712 | uint32_t flags; |
8d302e76 AJ |
1713 | |
1714 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); | |
1715 | addr = get_page_addr_code(env, pc); | |
1716 | tb_invalidate_phys_range(addr, addr + 1); | |
5b6dd868 | 1717 | } |
5b6dd868 BS |
1718 | } |
1719 | ||
1720 | #ifndef CONFIG_USER_ONLY | |
5b6dd868 BS |
1721 | /* in deterministic execution mode, instructions doing device I/Os |
1722 | must be at the end of the TB */ | |
90b40a69 | 1723 | void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr) |
5b6dd868 | 1724 | { |
a47dddd7 | 1725 | #if defined(TARGET_MIPS) || defined(TARGET_SH4) |
90b40a69 | 1726 | CPUArchState *env = cpu->env_ptr; |
a47dddd7 | 1727 | #endif |
5b6dd868 BS |
1728 | TranslationBlock *tb; |
1729 | uint32_t n, cflags; | |
1730 | target_ulong pc, cs_base; | |
89fee74a | 1731 | uint32_t flags; |
5b6dd868 | 1732 | |
a5e99826 | 1733 | tb_lock(); |
5b6dd868 BS |
1734 | tb = tb_find_pc(retaddr); |
1735 | if (!tb) { | |
a47dddd7 | 1736 | cpu_abort(cpu, "cpu_io_recompile: could not find TB for pc=%p", |
5b6dd868 BS |
1737 | (void *)retaddr); |
1738 | } | |
28ecfd7a | 1739 | n = cpu->icount_decr.u16.low + tb->icount; |
74f10515 | 1740 | cpu_restore_state_from_tb(cpu, tb, retaddr); |
5b6dd868 BS |
1741 | /* Calculate how many instructions had been executed before the fault |
1742 | occurred. */ | |
28ecfd7a | 1743 | n = n - cpu->icount_decr.u16.low; |
5b6dd868 BS |
1744 | /* Generate a new TB ending on the I/O insn. */ |
1745 | n++; | |
1746 | /* On MIPS and SH, delay slot instructions can only be restarted if | |
1747 | they were already the first instruction in the TB. If this is not | |
1748 | the first instruction in a TB then re-execute the preceding | |
1749 | branch. */ | |
1750 | #if defined(TARGET_MIPS) | |
1751 | if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { | |
c3577479 | 1752 | env->active_tc.PC -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4); |
28ecfd7a | 1753 | cpu->icount_decr.u16.low++; |
5b6dd868 BS |
1754 | env->hflags &= ~MIPS_HFLAG_BMASK; |
1755 | } | |
1756 | #elif defined(TARGET_SH4) | |
1757 | if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 | |
1758 | && n > 1) { | |
1759 | env->pc -= 2; | |
28ecfd7a | 1760 | cpu->icount_decr.u16.low++; |
5b6dd868 BS |
1761 | env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); |
1762 | } | |
1763 | #endif | |
1764 | /* This should never happen. */ | |
1765 | if (n > CF_COUNT_MASK) { | |
a47dddd7 | 1766 | cpu_abort(cpu, "TB too big during recompile"); |
5b6dd868 BS |
1767 | } |
1768 | ||
1769 | cflags = n | CF_LAST_IO; | |
1770 | pc = tb->pc; | |
1771 | cs_base = tb->cs_base; | |
1772 | flags = tb->flags; | |
1773 | tb_phys_invalidate(tb, -1); | |
02d57ea1 SF |
1774 | if (tb->cflags & CF_NOCACHE) { |
1775 | if (tb->orig_tb) { | |
1776 | /* Invalidate original TB if this TB was generated in | |
1777 | * cpu_exec_nocache() */ | |
1778 | tb_phys_invalidate(tb->orig_tb, -1); | |
1779 | } | |
1780 | tb_free(tb); | |
1781 | } | |
5b6dd868 BS |
1782 | /* FIXME: In theory this could raise an exception. In practice |
1783 | we have already translated the block once so it's probably ok. */ | |
648f034c | 1784 | tb_gen_code(cpu, pc, cs_base, flags, cflags); |
a5e99826 | 1785 | |
5b6dd868 | 1786 | /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not |
a5e99826 FK |
1787 | * the first in the TB) then we end up generating a whole new TB and |
1788 | * repeating the fault, which is horribly inefficient. | |
1789 | * Better would be to execute just this insn uncached, or generate a | |
1790 | * second new TB. | |
1791 | * | |
1792 | * cpu_loop_exit_noexc will longjmp back to cpu_exec where the | |
1793 | * tb_lock gets reset. | |
1794 | */ | |
6886b980 | 1795 | cpu_loop_exit_noexc(cpu); |
5b6dd868 BS |
1796 | } |
1797 | ||
611d4f99 | 1798 | void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr) |
5b6dd868 BS |
1799 | { |
1800 | unsigned int i; | |
1801 | ||
1802 | /* Discard jump cache entries for any tb which might potentially | |
1803 | overlap the flushed page. */ | |
1804 | i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE); | |
8cd70437 | 1805 | memset(&cpu->tb_jmp_cache[i], 0, |
5b6dd868 BS |
1806 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
1807 | ||
1808 | i = tb_jmp_cache_hash_page(addr); | |
8cd70437 | 1809 | memset(&cpu->tb_jmp_cache[i], 0, |
5b6dd868 BS |
1810 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
1811 | } | |
1812 | ||
7266ae91 EC |
1813 | static void print_qht_statistics(FILE *f, fprintf_function cpu_fprintf, |
1814 | struct qht_stats hst) | |
1815 | { | |
1816 | uint32_t hgram_opts; | |
1817 | size_t hgram_bins; | |
1818 | char *hgram; | |
1819 | ||
1820 | if (!hst.head_buckets) { | |
1821 | return; | |
1822 | } | |
1823 | cpu_fprintf(f, "TB hash buckets %zu/%zu (%0.2f%% head buckets used)\n", | |
1824 | hst.used_head_buckets, hst.head_buckets, | |
1825 | (double)hst.used_head_buckets / hst.head_buckets * 100); | |
1826 | ||
1827 | hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS; | |
1828 | hgram_opts |= QDIST_PR_100X | QDIST_PR_PERCENT; | |
1829 | if (qdist_xmax(&hst.occupancy) - qdist_xmin(&hst.occupancy) == 1) { | |
1830 | hgram_opts |= QDIST_PR_NODECIMAL; | |
1831 | } | |
1832 | hgram = qdist_pr(&hst.occupancy, 10, hgram_opts); | |
1833 | cpu_fprintf(f, "TB hash occupancy %0.2f%% avg chain occ. Histogram: %s\n", | |
1834 | qdist_avg(&hst.occupancy) * 100, hgram); | |
1835 | g_free(hgram); | |
1836 | ||
1837 | hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS; | |
1838 | hgram_bins = qdist_xmax(&hst.chain) - qdist_xmin(&hst.chain); | |
1839 | if (hgram_bins > 10) { | |
1840 | hgram_bins = 10; | |
1841 | } else { | |
1842 | hgram_bins = 0; | |
1843 | hgram_opts |= QDIST_PR_NODECIMAL | QDIST_PR_NOBINRANGE; | |
1844 | } | |
1845 | hgram = qdist_pr(&hst.chain, hgram_bins, hgram_opts); | |
1846 | cpu_fprintf(f, "TB hash avg chain %0.3f buckets. Histogram: %s\n", | |
1847 | qdist_avg(&hst.chain), hgram); | |
1848 | g_free(hgram); | |
1849 | } | |
1850 | ||
5b6dd868 BS |
1851 | void dump_exec_info(FILE *f, fprintf_function cpu_fprintf) |
1852 | { | |
1853 | int i, target_code_size, max_target_code_size; | |
1854 | int direct_jmp_count, direct_jmp2_count, cross_page; | |
1855 | TranslationBlock *tb; | |
329844d4 | 1856 | struct qht_stats hst; |
5b6dd868 | 1857 | |
a5e99826 FK |
1858 | tb_lock(); |
1859 | ||
5b6dd868 BS |
1860 | target_code_size = 0; |
1861 | max_target_code_size = 0; | |
1862 | cross_page = 0; | |
1863 | direct_jmp_count = 0; | |
1864 | direct_jmp2_count = 0; | |
5e5f07e0 EV |
1865 | for (i = 0; i < tcg_ctx.tb_ctx.nb_tbs; i++) { |
1866 | tb = &tcg_ctx.tb_ctx.tbs[i]; | |
5b6dd868 BS |
1867 | target_code_size += tb->size; |
1868 | if (tb->size > max_target_code_size) { | |
1869 | max_target_code_size = tb->size; | |
1870 | } | |
1871 | if (tb->page_addr[1] != -1) { | |
1872 | cross_page++; | |
1873 | } | |
f309101c | 1874 | if (tb->jmp_reset_offset[0] != TB_JMP_RESET_OFFSET_INVALID) { |
5b6dd868 | 1875 | direct_jmp_count++; |
f309101c | 1876 | if (tb->jmp_reset_offset[1] != TB_JMP_RESET_OFFSET_INVALID) { |
5b6dd868 BS |
1877 | direct_jmp2_count++; |
1878 | } | |
1879 | } | |
1880 | } | |
1881 | /* XXX: avoid using doubles ? */ | |
1882 | cpu_fprintf(f, "Translation buffer state:\n"); | |
1883 | cpu_fprintf(f, "gen code size %td/%zd\n", | |
0b0d3320 | 1884 | tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer, |
b125f9dc | 1885 | tcg_ctx.code_gen_highwater - tcg_ctx.code_gen_buffer); |
5b6dd868 | 1886 | cpu_fprintf(f, "TB count %d/%d\n", |
5e5f07e0 | 1887 | tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.code_gen_max_blocks); |
5b6dd868 | 1888 | cpu_fprintf(f, "TB avg target size %d max=%d bytes\n", |
5e5f07e0 EV |
1889 | tcg_ctx.tb_ctx.nb_tbs ? target_code_size / |
1890 | tcg_ctx.tb_ctx.nb_tbs : 0, | |
1891 | max_target_code_size); | |
5b6dd868 | 1892 | cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n", |
5e5f07e0 EV |
1893 | tcg_ctx.tb_ctx.nb_tbs ? (tcg_ctx.code_gen_ptr - |
1894 | tcg_ctx.code_gen_buffer) / | |
1895 | tcg_ctx.tb_ctx.nb_tbs : 0, | |
1896 | target_code_size ? (double) (tcg_ctx.code_gen_ptr - | |
1897 | tcg_ctx.code_gen_buffer) / | |
1898 | target_code_size : 0); | |
1899 | cpu_fprintf(f, "cross page TB count %d (%d%%)\n", cross_page, | |
1900 | tcg_ctx.tb_ctx.nb_tbs ? (cross_page * 100) / | |
1901 | tcg_ctx.tb_ctx.nb_tbs : 0); | |
5b6dd868 BS |
1902 | cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n", |
1903 | direct_jmp_count, | |
5e5f07e0 EV |
1904 | tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp_count * 100) / |
1905 | tcg_ctx.tb_ctx.nb_tbs : 0, | |
5b6dd868 | 1906 | direct_jmp2_count, |
5e5f07e0 EV |
1907 | tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp2_count * 100) / |
1908 | tcg_ctx.tb_ctx.nb_tbs : 0); | |
329844d4 EC |
1909 | |
1910 | qht_statistics_init(&tcg_ctx.tb_ctx.htable, &hst); | |
7266ae91 | 1911 | print_qht_statistics(f, cpu_fprintf, hst); |
329844d4 EC |
1912 | qht_statistics_destroy(&hst); |
1913 | ||
5b6dd868 | 1914 | cpu_fprintf(f, "\nStatistics:\n"); |
3359baad SF |
1915 | cpu_fprintf(f, "TB flush count %u\n", |
1916 | atomic_read(&tcg_ctx.tb_ctx.tb_flush_count)); | |
5e5f07e0 EV |
1917 | cpu_fprintf(f, "TB invalidate count %d\n", |
1918 | tcg_ctx.tb_ctx.tb_phys_invalidate_count); | |
5b6dd868 BS |
1919 | cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count); |
1920 | tcg_dump_info(f, cpu_fprintf); | |
a5e99826 FK |
1921 | |
1922 | tb_unlock(); | |
5b6dd868 BS |
1923 | } |
1924 | ||
246ae24d MF |
1925 | void dump_opcount_info(FILE *f, fprintf_function cpu_fprintf) |
1926 | { | |
1927 | tcg_dump_op_count(f, cpu_fprintf); | |
1928 | } | |
1929 | ||
5b6dd868 BS |
1930 | #else /* CONFIG_USER_ONLY */ |
1931 | ||
c3affe56 | 1932 | void cpu_interrupt(CPUState *cpu, int mask) |
5b6dd868 | 1933 | { |
259186a7 | 1934 | cpu->interrupt_request |= mask; |
378df4b2 | 1935 | cpu->tcg_exit_req = 1; |
5b6dd868 BS |
1936 | } |
1937 | ||
1938 | /* | |
1939 | * Walks guest process memory "regions" one by one | |
1940 | * and calls callback function 'fn' for each region. | |
1941 | */ | |
1942 | struct walk_memory_regions_data { | |
1943 | walk_memory_regions_fn fn; | |
1944 | void *priv; | |
1a1c4db9 | 1945 | target_ulong start; |
5b6dd868 BS |
1946 | int prot; |
1947 | }; | |
1948 | ||
1949 | static int walk_memory_regions_end(struct walk_memory_regions_data *data, | |
1a1c4db9 | 1950 | target_ulong end, int new_prot) |
5b6dd868 | 1951 | { |
1a1c4db9 | 1952 | if (data->start != -1u) { |
5b6dd868 BS |
1953 | int rc = data->fn(data->priv, data->start, end, data->prot); |
1954 | if (rc != 0) { | |
1955 | return rc; | |
1956 | } | |
1957 | } | |
1958 | ||
1a1c4db9 | 1959 | data->start = (new_prot ? end : -1u); |
5b6dd868 BS |
1960 | data->prot = new_prot; |
1961 | ||
1962 | return 0; | |
1963 | } | |
1964 | ||
1965 | static int walk_memory_regions_1(struct walk_memory_regions_data *data, | |
1a1c4db9 | 1966 | target_ulong base, int level, void **lp) |
5b6dd868 | 1967 | { |
1a1c4db9 | 1968 | target_ulong pa; |
5b6dd868 BS |
1969 | int i, rc; |
1970 | ||
1971 | if (*lp == NULL) { | |
1972 | return walk_memory_regions_end(data, base, 0); | |
1973 | } | |
1974 | ||
1975 | if (level == 0) { | |
1976 | PageDesc *pd = *lp; | |
1977 | ||
03f49957 | 1978 | for (i = 0; i < V_L2_SIZE; ++i) { |
5b6dd868 BS |
1979 | int prot = pd[i].flags; |
1980 | ||
1981 | pa = base | (i << TARGET_PAGE_BITS); | |
1982 | if (prot != data->prot) { | |
1983 | rc = walk_memory_regions_end(data, pa, prot); | |
1984 | if (rc != 0) { | |
1985 | return rc; | |
1986 | } | |
1987 | } | |
1988 | } | |
1989 | } else { | |
1990 | void **pp = *lp; | |
1991 | ||
03f49957 | 1992 | for (i = 0; i < V_L2_SIZE; ++i) { |
1a1c4db9 | 1993 | pa = base | ((target_ulong)i << |
03f49957 | 1994 | (TARGET_PAGE_BITS + V_L2_BITS * level)); |
5b6dd868 BS |
1995 | rc = walk_memory_regions_1(data, pa, level - 1, pp + i); |
1996 | if (rc != 0) { | |
1997 | return rc; | |
1998 | } | |
1999 | } | |
2000 | } | |
2001 | ||
2002 | return 0; | |
2003 | } | |
2004 | ||
2005 | int walk_memory_regions(void *priv, walk_memory_regions_fn fn) | |
2006 | { | |
2007 | struct walk_memory_regions_data data; | |
66ec9f49 | 2008 | uintptr_t i, l1_sz = v_l1_size; |
5b6dd868 BS |
2009 | |
2010 | data.fn = fn; | |
2011 | data.priv = priv; | |
1a1c4db9 | 2012 | data.start = -1u; |
5b6dd868 BS |
2013 | data.prot = 0; |
2014 | ||
66ec9f49 VK |
2015 | for (i = 0; i < l1_sz; i++) { |
2016 | target_ulong base = i << (v_l1_shift + TARGET_PAGE_BITS); | |
2017 | int rc = walk_memory_regions_1(&data, base, v_l2_levels, l1_map + i); | |
5b6dd868 BS |
2018 | if (rc != 0) { |
2019 | return rc; | |
2020 | } | |
2021 | } | |
2022 | ||
2023 | return walk_memory_regions_end(&data, 0, 0); | |
2024 | } | |
2025 | ||
1a1c4db9 MI |
2026 | static int dump_region(void *priv, target_ulong start, |
2027 | target_ulong end, unsigned long prot) | |
5b6dd868 BS |
2028 | { |
2029 | FILE *f = (FILE *)priv; | |
2030 | ||
1a1c4db9 MI |
2031 | (void) fprintf(f, TARGET_FMT_lx"-"TARGET_FMT_lx |
2032 | " "TARGET_FMT_lx" %c%c%c\n", | |
5b6dd868 BS |
2033 | start, end, end - start, |
2034 | ((prot & PAGE_READ) ? 'r' : '-'), | |
2035 | ((prot & PAGE_WRITE) ? 'w' : '-'), | |
2036 | ((prot & PAGE_EXEC) ? 'x' : '-')); | |
2037 | ||
2038 | return 0; | |
2039 | } | |
2040 | ||
2041 | /* dump memory mappings */ | |
2042 | void page_dump(FILE *f) | |
2043 | { | |
1a1c4db9 | 2044 | const int length = sizeof(target_ulong) * 2; |
227b8175 SW |
2045 | (void) fprintf(f, "%-*s %-*s %-*s %s\n", |
2046 | length, "start", length, "end", length, "size", "prot"); | |
5b6dd868 BS |
2047 | walk_memory_regions(f, dump_region); |
2048 | } | |
2049 | ||
2050 | int page_get_flags(target_ulong address) | |
2051 | { | |
2052 | PageDesc *p; | |
2053 | ||
2054 | p = page_find(address >> TARGET_PAGE_BITS); | |
2055 | if (!p) { | |
2056 | return 0; | |
2057 | } | |
2058 | return p->flags; | |
2059 | } | |
2060 | ||
2061 | /* Modify the flags of a page and invalidate the code if necessary. | |
2062 | The flag PAGE_WRITE_ORG is positioned automatically depending | |
2063 | on PAGE_WRITE. The mmap_lock should already be held. */ | |
2064 | void page_set_flags(target_ulong start, target_ulong end, int flags) | |
2065 | { | |
2066 | target_ulong addr, len; | |
2067 | ||
2068 | /* This function should never be called with addresses outside the | |
2069 | guest address space. If this assert fires, it probably indicates | |
2070 | a missing call to h2g_valid. */ | |
2071 | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS | |
1a1c4db9 | 2072 | assert(end < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS)); |
5b6dd868 BS |
2073 | #endif |
2074 | assert(start < end); | |
e505a063 | 2075 | assert_memory_lock(); |
5b6dd868 BS |
2076 | |
2077 | start = start & TARGET_PAGE_MASK; | |
2078 | end = TARGET_PAGE_ALIGN(end); | |
2079 | ||
2080 | if (flags & PAGE_WRITE) { | |
2081 | flags |= PAGE_WRITE_ORG; | |
2082 | } | |
2083 | ||
2084 | for (addr = start, len = end - start; | |
2085 | len != 0; | |
2086 | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { | |
2087 | PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1); | |
2088 | ||
2089 | /* If the write protection bit is set, then we invalidate | |
2090 | the code inside. */ | |
2091 | if (!(p->flags & PAGE_WRITE) && | |
2092 | (flags & PAGE_WRITE) && | |
2093 | p->first_tb) { | |
75809229 | 2094 | tb_invalidate_phys_page(addr, 0); |
5b6dd868 BS |
2095 | } |
2096 | p->flags = flags; | |
2097 | } | |
2098 | } | |
2099 | ||
2100 | int page_check_range(target_ulong start, target_ulong len, int flags) | |
2101 | { | |
2102 | PageDesc *p; | |
2103 | target_ulong end; | |
2104 | target_ulong addr; | |
2105 | ||
2106 | /* This function should never be called with addresses outside the | |
2107 | guest address space. If this assert fires, it probably indicates | |
2108 | a missing call to h2g_valid. */ | |
2109 | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS | |
1a1c4db9 | 2110 | assert(start < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS)); |
5b6dd868 BS |
2111 | #endif |
2112 | ||
2113 | if (len == 0) { | |
2114 | return 0; | |
2115 | } | |
2116 | if (start + len - 1 < start) { | |
2117 | /* We've wrapped around. */ | |
2118 | return -1; | |
2119 | } | |
2120 | ||
2121 | /* must do before we loose bits in the next step */ | |
2122 | end = TARGET_PAGE_ALIGN(start + len); | |
2123 | start = start & TARGET_PAGE_MASK; | |
2124 | ||
2125 | for (addr = start, len = end - start; | |
2126 | len != 0; | |
2127 | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { | |
2128 | p = page_find(addr >> TARGET_PAGE_BITS); | |
2129 | if (!p) { | |
2130 | return -1; | |
2131 | } | |
2132 | if (!(p->flags & PAGE_VALID)) { | |
2133 | return -1; | |
2134 | } | |
2135 | ||
2136 | if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) { | |
2137 | return -1; | |
2138 | } | |
2139 | if (flags & PAGE_WRITE) { | |
2140 | if (!(p->flags & PAGE_WRITE_ORG)) { | |
2141 | return -1; | |
2142 | } | |
2143 | /* unprotect the page if it was put read-only because it | |
2144 | contains translated code */ | |
2145 | if (!(p->flags & PAGE_WRITE)) { | |
f213e72f | 2146 | if (!page_unprotect(addr, 0)) { |
5b6dd868 BS |
2147 | return -1; |
2148 | } | |
2149 | } | |
5b6dd868 BS |
2150 | } |
2151 | } | |
2152 | return 0; | |
2153 | } | |
2154 | ||
2155 | /* called from signal handler: invalidate the code and unprotect the | |
f213e72f PM |
2156 | * page. Return 0 if the fault was not handled, 1 if it was handled, |
2157 | * and 2 if it was handled but the caller must cause the TB to be | |
2158 | * immediately exited. (We can only return 2 if the 'pc' argument is | |
2159 | * non-zero.) | |
2160 | */ | |
2161 | int page_unprotect(target_ulong address, uintptr_t pc) | |
5b6dd868 BS |
2162 | { |
2163 | unsigned int prot; | |
7399a337 | 2164 | bool current_tb_invalidated; |
5b6dd868 BS |
2165 | PageDesc *p; |
2166 | target_ulong host_start, host_end, addr; | |
2167 | ||
2168 | /* Technically this isn't safe inside a signal handler. However we | |
2169 | know this only ever happens in a synchronous SEGV handler, so in | |
2170 | practice it seems to be ok. */ | |
2171 | mmap_lock(); | |
2172 | ||
2173 | p = page_find(address >> TARGET_PAGE_BITS); | |
2174 | if (!p) { | |
2175 | mmap_unlock(); | |
2176 | return 0; | |
2177 | } | |
2178 | ||
2179 | /* if the page was really writable, then we change its | |
2180 | protection back to writable */ | |
2181 | if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) { | |
2182 | host_start = address & qemu_host_page_mask; | |
2183 | host_end = host_start + qemu_host_page_size; | |
2184 | ||
2185 | prot = 0; | |
7399a337 | 2186 | current_tb_invalidated = false; |
5b6dd868 BS |
2187 | for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) { |
2188 | p = page_find(addr >> TARGET_PAGE_BITS); | |
2189 | p->flags |= PAGE_WRITE; | |
2190 | prot |= p->flags; | |
2191 | ||
2192 | /* and since the content will be modified, we must invalidate | |
2193 | the corresponding translated code. */ | |
7399a337 | 2194 | current_tb_invalidated |= tb_invalidate_phys_page(addr, pc); |
5b6dd868 BS |
2195 | #ifdef DEBUG_TB_CHECK |
2196 | tb_invalidate_check(addr); | |
2197 | #endif | |
2198 | } | |
2199 | mprotect((void *)g2h(host_start), qemu_host_page_size, | |
2200 | prot & PAGE_BITS); | |
2201 | ||
2202 | mmap_unlock(); | |
7399a337 SS |
2203 | /* If current TB was invalidated return to main loop */ |
2204 | return current_tb_invalidated ? 2 : 1; | |
5b6dd868 BS |
2205 | } |
2206 | mmap_unlock(); | |
2207 | return 0; | |
2208 | } | |
2209 | #endif /* CONFIG_USER_ONLY */ |