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