]> git.proxmox.com Git - mirror_qemu.git/blob - translate-all.c
tcg-mips: Layout executable and code_gen_buffer
[mirror_qemu.git] / translate-all.c
1 /*
2 * Host code generation
3 *
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
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifdef _WIN32
20 #include <windows.h>
21 #else
22 #include <sys/types.h>
23 #include <sys/mman.h>
24 #endif
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <inttypes.h>
30
31 #include "config.h"
32
33 #include "qemu-common.h"
34 #define NO_CPU_IO_DEFS
35 #include "cpu.h"
36 #include "disas/disas.h"
37 #include "tcg.h"
38 #if defined(CONFIG_USER_ONLY)
39 #include "qemu.h"
40 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
41 #include <sys/param.h>
42 #if __FreeBSD_version >= 700104
43 #define HAVE_KINFO_GETVMMAP
44 #define sigqueue sigqueue_freebsd /* avoid redefinition */
45 #include <sys/time.h>
46 #include <sys/proc.h>
47 #include <machine/profile.h>
48 #define _KERNEL
49 #include <sys/user.h>
50 #undef _KERNEL
51 #undef sigqueue
52 #include <libutil.h>
53 #endif
54 #endif
55 #else
56 #include "exec/address-spaces.h"
57 #endif
58
59 #include "exec/cputlb.h"
60 #include "translate-all.h"
61 #include "qemu/timer.h"
62
63 //#define DEBUG_TB_INVALIDATE
64 //#define DEBUG_FLUSH
65 /* make various TB consistency checks */
66 //#define DEBUG_TB_CHECK
67
68 #if !defined(CONFIG_USER_ONLY)
69 /* TB consistency checks only implemented for usermode emulation. */
70 #undef DEBUG_TB_CHECK
71 #endif
72
73 #define SMC_BITMAP_USE_THRESHOLD 10
74
75 typedef struct PageDesc {
76 /* list of TBs intersecting this ram page */
77 TranslationBlock *first_tb;
78 /* in order to optimize self modifying code, we count the number
79 of lookups we do to a given page to use a bitmap */
80 unsigned int code_write_count;
81 uint8_t *code_bitmap;
82 #if defined(CONFIG_USER_ONLY)
83 unsigned long flags;
84 #endif
85 } PageDesc;
86
87 /* In system mode we want L1_MAP to be based on ram offsets,
88 while in user mode we want it to be based on virtual addresses. */
89 #if !defined(CONFIG_USER_ONLY)
90 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
91 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
92 #else
93 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
94 #endif
95 #else
96 # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
97 #endif
98
99 /* Size of the L2 (and L3, etc) page tables. */
100 #define V_L2_BITS 10
101 #define V_L2_SIZE (1 << V_L2_BITS)
102
103 /* The bits remaining after N lower levels of page tables. */
104 #define V_L1_BITS_REM \
105 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS)
106
107 #if V_L1_BITS_REM < 4
108 #define V_L1_BITS (V_L1_BITS_REM + V_L2_BITS)
109 #else
110 #define V_L1_BITS V_L1_BITS_REM
111 #endif
112
113 #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
114
115 #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
116
117 uintptr_t qemu_real_host_page_size;
118 uintptr_t qemu_host_page_size;
119 uintptr_t qemu_host_page_mask;
120
121 /* This is a multi-level map on the virtual address space.
122 The bottom level has pointers to PageDesc. */
123 static void *l1_map[V_L1_SIZE];
124
125 /* code generation context */
126 TCGContext tcg_ctx;
127
128 static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
129 tb_page_addr_t phys_page2);
130 static TranslationBlock *tb_find_pc(uintptr_t tc_ptr);
131
132 void cpu_gen_init(void)
133 {
134 tcg_context_init(&tcg_ctx);
135 }
136
137 /* return non zero if the very first instruction is invalid so that
138 the virtual CPU can trigger an exception.
139
140 '*gen_code_size_ptr' contains the size of the generated code (host
141 code).
142 */
143 int cpu_gen_code(CPUArchState *env, TranslationBlock *tb, int *gen_code_size_ptr)
144 {
145 TCGContext *s = &tcg_ctx;
146 tcg_insn_unit *gen_code_buf;
147 int gen_code_size;
148 #ifdef CONFIG_PROFILER
149 int64_t ti;
150 #endif
151
152 #ifdef CONFIG_PROFILER
153 s->tb_count1++; /* includes aborted translations because of
154 exceptions */
155 ti = profile_getclock();
156 #endif
157 tcg_func_start(s);
158
159 gen_intermediate_code(env, tb);
160
161 /* generate machine code */
162 gen_code_buf = tb->tc_ptr;
163 tb->tb_next_offset[0] = 0xffff;
164 tb->tb_next_offset[1] = 0xffff;
165 s->tb_next_offset = tb->tb_next_offset;
166 #ifdef USE_DIRECT_JUMP
167 s->tb_jmp_offset = tb->tb_jmp_offset;
168 s->tb_next = NULL;
169 #else
170 s->tb_jmp_offset = NULL;
171 s->tb_next = tb->tb_next;
172 #endif
173
174 #ifdef CONFIG_PROFILER
175 s->tb_count++;
176 s->interm_time += profile_getclock() - ti;
177 s->code_time -= profile_getclock();
178 #endif
179 gen_code_size = tcg_gen_code(s, gen_code_buf);
180 *gen_code_size_ptr = gen_code_size;
181 #ifdef CONFIG_PROFILER
182 s->code_time += profile_getclock();
183 s->code_in_len += tb->size;
184 s->code_out_len += gen_code_size;
185 #endif
186
187 #ifdef DEBUG_DISAS
188 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
189 qemu_log("OUT: [size=%d]\n", gen_code_size);
190 log_disas(tb->tc_ptr, gen_code_size);
191 qemu_log("\n");
192 qemu_log_flush();
193 }
194 #endif
195 return 0;
196 }
197
198 /* The cpu state corresponding to 'searched_pc' is restored.
199 */
200 static int cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
201 uintptr_t searched_pc)
202 {
203 CPUArchState *env = cpu->env_ptr;
204 TCGContext *s = &tcg_ctx;
205 int j;
206 uintptr_t tc_ptr;
207 #ifdef CONFIG_PROFILER
208 int64_t ti;
209 #endif
210
211 #ifdef CONFIG_PROFILER
212 ti = profile_getclock();
213 #endif
214 tcg_func_start(s);
215
216 gen_intermediate_code_pc(env, tb);
217
218 if (use_icount) {
219 /* Reset the cycle counter to the start of the block. */
220 cpu->icount_decr.u16.low += tb->icount;
221 /* Clear the IO flag. */
222 cpu->can_do_io = 0;
223 }
224
225 /* find opc index corresponding to search_pc */
226 tc_ptr = (uintptr_t)tb->tc_ptr;
227 if (searched_pc < tc_ptr)
228 return -1;
229
230 s->tb_next_offset = tb->tb_next_offset;
231 #ifdef USE_DIRECT_JUMP
232 s->tb_jmp_offset = tb->tb_jmp_offset;
233 s->tb_next = NULL;
234 #else
235 s->tb_jmp_offset = NULL;
236 s->tb_next = tb->tb_next;
237 #endif
238 j = tcg_gen_code_search_pc(s, (tcg_insn_unit *)tc_ptr,
239 searched_pc - tc_ptr);
240 if (j < 0)
241 return -1;
242 /* now find start of instruction before */
243 while (s->gen_opc_instr_start[j] == 0) {
244 j--;
245 }
246 cpu->icount_decr.u16.low -= s->gen_opc_icount[j];
247
248 restore_state_to_opc(env, tb, j);
249
250 #ifdef CONFIG_PROFILER
251 s->restore_time += profile_getclock() - ti;
252 s->restore_count++;
253 #endif
254 return 0;
255 }
256
257 bool cpu_restore_state(CPUState *cpu, uintptr_t retaddr)
258 {
259 TranslationBlock *tb;
260
261 tb = tb_find_pc(retaddr);
262 if (tb) {
263 cpu_restore_state_from_tb(cpu, tb, retaddr);
264 return true;
265 }
266 return false;
267 }
268
269 #ifdef _WIN32
270 static inline void map_exec(void *addr, long size)
271 {
272 DWORD old_protect;
273 VirtualProtect(addr, size,
274 PAGE_EXECUTE_READWRITE, &old_protect);
275 }
276 #else
277 static inline void map_exec(void *addr, long size)
278 {
279 unsigned long start, end, page_size;
280
281 page_size = getpagesize();
282 start = (unsigned long)addr;
283 start &= ~(page_size - 1);
284
285 end = (unsigned long)addr + size;
286 end += page_size - 1;
287 end &= ~(page_size - 1);
288
289 mprotect((void *)start, end - start,
290 PROT_READ | PROT_WRITE | PROT_EXEC);
291 }
292 #endif
293
294 void page_size_init(void)
295 {
296 /* NOTE: we can always suppose that qemu_host_page_size >=
297 TARGET_PAGE_SIZE */
298 #ifdef _WIN32
299 SYSTEM_INFO system_info;
300
301 GetSystemInfo(&system_info);
302 qemu_real_host_page_size = system_info.dwPageSize;
303 #else
304 qemu_real_host_page_size = getpagesize();
305 #endif
306 if (qemu_host_page_size == 0) {
307 qemu_host_page_size = qemu_real_host_page_size;
308 }
309 if (qemu_host_page_size < TARGET_PAGE_SIZE) {
310 qemu_host_page_size = TARGET_PAGE_SIZE;
311 }
312 qemu_host_page_mask = ~(qemu_host_page_size - 1);
313 }
314
315 static void page_init(void)
316 {
317 page_size_init();
318 #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
319 {
320 #ifdef HAVE_KINFO_GETVMMAP
321 struct kinfo_vmentry *freep;
322 int i, cnt;
323
324 freep = kinfo_getvmmap(getpid(), &cnt);
325 if (freep) {
326 mmap_lock();
327 for (i = 0; i < cnt; i++) {
328 unsigned long startaddr, endaddr;
329
330 startaddr = freep[i].kve_start;
331 endaddr = freep[i].kve_end;
332 if (h2g_valid(startaddr)) {
333 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
334
335 if (h2g_valid(endaddr)) {
336 endaddr = h2g(endaddr);
337 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
338 } else {
339 #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
340 endaddr = ~0ul;
341 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
342 #endif
343 }
344 }
345 }
346 free(freep);
347 mmap_unlock();
348 }
349 #else
350 FILE *f;
351
352 last_brk = (unsigned long)sbrk(0);
353
354 f = fopen("/compat/linux/proc/self/maps", "r");
355 if (f) {
356 mmap_lock();
357
358 do {
359 unsigned long startaddr, endaddr;
360 int n;
361
362 n = fscanf(f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
363
364 if (n == 2 && h2g_valid(startaddr)) {
365 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
366
367 if (h2g_valid(endaddr)) {
368 endaddr = h2g(endaddr);
369 } else {
370 endaddr = ~0ul;
371 }
372 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
373 }
374 } while (!feof(f));
375
376 fclose(f);
377 mmap_unlock();
378 }
379 #endif
380 }
381 #endif
382 }
383
384 static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
385 {
386 PageDesc *pd;
387 void **lp;
388 int i;
389
390 #if defined(CONFIG_USER_ONLY)
391 /* We can't use g_malloc because it may recurse into a locked mutex. */
392 # define ALLOC(P, SIZE) \
393 do { \
394 P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
395 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
396 } while (0)
397 #else
398 # define ALLOC(P, SIZE) \
399 do { P = g_malloc0(SIZE); } while (0)
400 #endif
401
402 /* Level 1. Always allocated. */
403 lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
404
405 /* Level 2..N-1. */
406 for (i = V_L1_SHIFT / V_L2_BITS - 1; i > 0; i--) {
407 void **p = *lp;
408
409 if (p == NULL) {
410 if (!alloc) {
411 return NULL;
412 }
413 ALLOC(p, sizeof(void *) * V_L2_SIZE);
414 *lp = p;
415 }
416
417 lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
418 }
419
420 pd = *lp;
421 if (pd == NULL) {
422 if (!alloc) {
423 return NULL;
424 }
425 ALLOC(pd, sizeof(PageDesc) * V_L2_SIZE);
426 *lp = pd;
427 }
428
429 #undef ALLOC
430
431 return pd + (index & (V_L2_SIZE - 1));
432 }
433
434 static inline PageDesc *page_find(tb_page_addr_t index)
435 {
436 return page_find_alloc(index, 0);
437 }
438
439 #if !defined(CONFIG_USER_ONLY)
440 #define mmap_lock() do { } while (0)
441 #define mmap_unlock() do { } while (0)
442 #endif
443
444 #if defined(CONFIG_USER_ONLY)
445 /* Currently it is not recommended to allocate big chunks of data in
446 user mode. It will change when a dedicated libc will be used. */
447 /* ??? 64-bit hosts ought to have no problem mmaping data outside the
448 region in which the guest needs to run. Revisit this. */
449 #define USE_STATIC_CODE_GEN_BUFFER
450 #endif
451
452 /* ??? Should configure for this, not list operating systems here. */
453 #if (defined(__linux__) \
454 || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
455 || defined(__DragonFly__) || defined(__OpenBSD__) \
456 || defined(__NetBSD__))
457 # define USE_MMAP
458 #endif
459
460 /* Minimum size of the code gen buffer. This number is randomly chosen,
461 but not so small that we can't have a fair number of TB's live. */
462 #define MIN_CODE_GEN_BUFFER_SIZE (1024u * 1024)
463
464 /* Maximum size of the code gen buffer we'd like to use. Unless otherwise
465 indicated, this is constrained by the range of direct branches on the
466 host cpu, as used by the TCG implementation of goto_tb. */
467 #if defined(__x86_64__)
468 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
469 #elif defined(__sparc__)
470 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
471 #elif defined(__aarch64__)
472 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
473 #elif defined(__arm__)
474 # define MAX_CODE_GEN_BUFFER_SIZE (16u * 1024 * 1024)
475 #elif defined(__s390x__)
476 /* We have a +- 4GB range on the branches; leave some slop. */
477 # define MAX_CODE_GEN_BUFFER_SIZE (3ul * 1024 * 1024 * 1024)
478 #elif defined(__mips__)
479 /* We have a 256MB branch region, but leave room to make sure the
480 main executable is also within that region. */
481 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
482 #else
483 # define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1)
484 #endif
485
486 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32u * 1024 * 1024)
487
488 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
489 (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
490 ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
491
492 static inline size_t size_code_gen_buffer(size_t tb_size)
493 {
494 /* Size the buffer. */
495 if (tb_size == 0) {
496 #ifdef USE_STATIC_CODE_GEN_BUFFER
497 tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
498 #else
499 /* ??? Needs adjustments. */
500 /* ??? If we relax the requirement that CONFIG_USER_ONLY use the
501 static buffer, we could size this on RESERVED_VA, on the text
502 segment size of the executable, or continue to use the default. */
503 tb_size = (unsigned long)(ram_size / 4);
504 #endif
505 }
506 if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
507 tb_size = MIN_CODE_GEN_BUFFER_SIZE;
508 }
509 if (tb_size > MAX_CODE_GEN_BUFFER_SIZE) {
510 tb_size = MAX_CODE_GEN_BUFFER_SIZE;
511 }
512 tcg_ctx.code_gen_buffer_size = tb_size;
513 return tb_size;
514 }
515
516 #ifdef USE_STATIC_CODE_GEN_BUFFER
517 static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
518 __attribute__((aligned(CODE_GEN_ALIGN)));
519
520 static inline void *alloc_code_gen_buffer(void)
521 {
522 map_exec(static_code_gen_buffer, tcg_ctx.code_gen_buffer_size);
523 return static_code_gen_buffer;
524 }
525 #elif defined(USE_MMAP)
526 static inline void *alloc_code_gen_buffer(void)
527 {
528 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
529 uintptr_t start = 0;
530 void *buf;
531
532 /* Constrain the position of the buffer based on the host cpu.
533 Note that these addresses are chosen in concert with the
534 addresses assigned in the relevant linker script file. */
535 # if defined(__PIE__) || defined(__PIC__)
536 /* Don't bother setting a preferred location if we're building
537 a position-independent executable. We're more likely to get
538 an address near the main executable if we let the kernel
539 choose the address. */
540 # elif defined(__x86_64__) && defined(MAP_32BIT)
541 /* Force the memory down into low memory with the executable.
542 Leave the choice of exact location with the kernel. */
543 flags |= MAP_32BIT;
544 /* Cannot expect to map more than 800MB in low memory. */
545 if (tcg_ctx.code_gen_buffer_size > 800u * 1024 * 1024) {
546 tcg_ctx.code_gen_buffer_size = 800u * 1024 * 1024;
547 }
548 # elif defined(__sparc__)
549 start = 0x40000000ul;
550 # elif defined(__s390x__)
551 start = 0x90000000ul;
552 # elif defined(__mips__)
553 /* ??? We ought to more explicitly manage layout for softmmu too. */
554 # ifdef CONFIG_USER_ONLY
555 start = 0x68000000ul;
556 # elif _MIPS_SIM == _ABI64
557 start = 0x128000000ul;
558 # else
559 start = 0x08000000ul;
560 # endif
561 # endif
562
563 buf = mmap((void *)start, tcg_ctx.code_gen_buffer_size,
564 PROT_WRITE | PROT_READ | PROT_EXEC, flags, -1, 0);
565 return buf == MAP_FAILED ? NULL : buf;
566 }
567 #else
568 static inline void *alloc_code_gen_buffer(void)
569 {
570 void *buf = g_malloc(tcg_ctx.code_gen_buffer_size);
571
572 if (buf) {
573 map_exec(buf, tcg_ctx.code_gen_buffer_size);
574 }
575 return buf;
576 }
577 #endif /* USE_STATIC_CODE_GEN_BUFFER, USE_MMAP */
578
579 static inline void code_gen_alloc(size_t tb_size)
580 {
581 tcg_ctx.code_gen_buffer_size = size_code_gen_buffer(tb_size);
582 tcg_ctx.code_gen_buffer = alloc_code_gen_buffer();
583 if (tcg_ctx.code_gen_buffer == NULL) {
584 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
585 exit(1);
586 }
587
588 qemu_madvise(tcg_ctx.code_gen_buffer, tcg_ctx.code_gen_buffer_size,
589 QEMU_MADV_HUGEPAGE);
590
591 /* Steal room for the prologue at the end of the buffer. This ensures
592 (via the MAX_CODE_GEN_BUFFER_SIZE limits above) that direct branches
593 from TB's to the prologue are going to be in range. It also means
594 that we don't need to mark (additional) portions of the data segment
595 as executable. */
596 tcg_ctx.code_gen_prologue = tcg_ctx.code_gen_buffer +
597 tcg_ctx.code_gen_buffer_size - 1024;
598 tcg_ctx.code_gen_buffer_size -= 1024;
599
600 tcg_ctx.code_gen_buffer_max_size = tcg_ctx.code_gen_buffer_size -
601 (TCG_MAX_OP_SIZE * OPC_BUF_SIZE);
602 tcg_ctx.code_gen_max_blocks = tcg_ctx.code_gen_buffer_size /
603 CODE_GEN_AVG_BLOCK_SIZE;
604 tcg_ctx.tb_ctx.tbs =
605 g_malloc(tcg_ctx.code_gen_max_blocks * sizeof(TranslationBlock));
606 }
607
608 /* Must be called before using the QEMU cpus. 'tb_size' is the size
609 (in bytes) allocated to the translation buffer. Zero means default
610 size. */
611 void tcg_exec_init(unsigned long tb_size)
612 {
613 cpu_gen_init();
614 code_gen_alloc(tb_size);
615 tcg_ctx.code_gen_ptr = tcg_ctx.code_gen_buffer;
616 tcg_register_jit(tcg_ctx.code_gen_buffer, tcg_ctx.code_gen_buffer_size);
617 page_init();
618 #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
619 /* There's no guest base to take into account, so go ahead and
620 initialize the prologue now. */
621 tcg_prologue_init(&tcg_ctx);
622 #endif
623 }
624
625 bool tcg_enabled(void)
626 {
627 return tcg_ctx.code_gen_buffer != NULL;
628 }
629
630 /* Allocate a new translation block. Flush the translation buffer if
631 too many translation blocks or too much generated code. */
632 static TranslationBlock *tb_alloc(target_ulong pc)
633 {
634 TranslationBlock *tb;
635
636 if (tcg_ctx.tb_ctx.nb_tbs >= tcg_ctx.code_gen_max_blocks ||
637 (tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer) >=
638 tcg_ctx.code_gen_buffer_max_size) {
639 return NULL;
640 }
641 tb = &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs++];
642 tb->pc = pc;
643 tb->cflags = 0;
644 return tb;
645 }
646
647 void tb_free(TranslationBlock *tb)
648 {
649 /* In practice this is mostly used for single use temporary TB
650 Ignore the hard cases and just back up if this TB happens to
651 be the last one generated. */
652 if (tcg_ctx.tb_ctx.nb_tbs > 0 &&
653 tb == &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs - 1]) {
654 tcg_ctx.code_gen_ptr = tb->tc_ptr;
655 tcg_ctx.tb_ctx.nb_tbs--;
656 }
657 }
658
659 static inline void invalidate_page_bitmap(PageDesc *p)
660 {
661 if (p->code_bitmap) {
662 g_free(p->code_bitmap);
663 p->code_bitmap = NULL;
664 }
665 p->code_write_count = 0;
666 }
667
668 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
669 static void page_flush_tb_1(int level, void **lp)
670 {
671 int i;
672
673 if (*lp == NULL) {
674 return;
675 }
676 if (level == 0) {
677 PageDesc *pd = *lp;
678
679 for (i = 0; i < V_L2_SIZE; ++i) {
680 pd[i].first_tb = NULL;
681 invalidate_page_bitmap(pd + i);
682 }
683 } else {
684 void **pp = *lp;
685
686 for (i = 0; i < V_L2_SIZE; ++i) {
687 page_flush_tb_1(level - 1, pp + i);
688 }
689 }
690 }
691
692 static void page_flush_tb(void)
693 {
694 int i;
695
696 for (i = 0; i < V_L1_SIZE; i++) {
697 page_flush_tb_1(V_L1_SHIFT / V_L2_BITS - 1, l1_map + i);
698 }
699 }
700
701 /* flush all the translation blocks */
702 /* XXX: tb_flush is currently not thread safe */
703 void tb_flush(CPUArchState *env1)
704 {
705 CPUState *cpu = ENV_GET_CPU(env1);
706
707 #if defined(DEBUG_FLUSH)
708 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
709 (unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer),
710 tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.tb_ctx.nb_tbs > 0 ?
711 ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer)) /
712 tcg_ctx.tb_ctx.nb_tbs : 0);
713 #endif
714 if ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer)
715 > tcg_ctx.code_gen_buffer_size) {
716 cpu_abort(cpu, "Internal error: code buffer overflow\n");
717 }
718 tcg_ctx.tb_ctx.nb_tbs = 0;
719
720 CPU_FOREACH(cpu) {
721 memset(cpu->tb_jmp_cache, 0, sizeof(cpu->tb_jmp_cache));
722 }
723
724 memset(tcg_ctx.tb_ctx.tb_phys_hash, 0, sizeof(tcg_ctx.tb_ctx.tb_phys_hash));
725 page_flush_tb();
726
727 tcg_ctx.code_gen_ptr = tcg_ctx.code_gen_buffer;
728 /* XXX: flush processor icache at this point if cache flush is
729 expensive */
730 tcg_ctx.tb_ctx.tb_flush_count++;
731 }
732
733 #ifdef DEBUG_TB_CHECK
734
735 static void tb_invalidate_check(target_ulong address)
736 {
737 TranslationBlock *tb;
738 int i;
739
740 address &= TARGET_PAGE_MASK;
741 for (i = 0; i < CODE_GEN_PHYS_HASH_SIZE; i++) {
742 for (tb = tb_ctx.tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
743 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
744 address >= tb->pc + tb->size)) {
745 printf("ERROR invalidate: address=" TARGET_FMT_lx
746 " PC=%08lx size=%04x\n",
747 address, (long)tb->pc, tb->size);
748 }
749 }
750 }
751 }
752
753 /* verify that all the pages have correct rights for code */
754 static void tb_page_check(void)
755 {
756 TranslationBlock *tb;
757 int i, flags1, flags2;
758
759 for (i = 0; i < CODE_GEN_PHYS_HASH_SIZE; i++) {
760 for (tb = tcg_ctx.tb_ctx.tb_phys_hash[i]; tb != NULL;
761 tb = tb->phys_hash_next) {
762 flags1 = page_get_flags(tb->pc);
763 flags2 = page_get_flags(tb->pc + tb->size - 1);
764 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
765 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
766 (long)tb->pc, tb->size, flags1, flags2);
767 }
768 }
769 }
770 }
771
772 #endif
773
774 static inline void tb_hash_remove(TranslationBlock **ptb, TranslationBlock *tb)
775 {
776 TranslationBlock *tb1;
777
778 for (;;) {
779 tb1 = *ptb;
780 if (tb1 == tb) {
781 *ptb = tb1->phys_hash_next;
782 break;
783 }
784 ptb = &tb1->phys_hash_next;
785 }
786 }
787
788 static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
789 {
790 TranslationBlock *tb1;
791 unsigned int n1;
792
793 for (;;) {
794 tb1 = *ptb;
795 n1 = (uintptr_t)tb1 & 3;
796 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
797 if (tb1 == tb) {
798 *ptb = tb1->page_next[n1];
799 break;
800 }
801 ptb = &tb1->page_next[n1];
802 }
803 }
804
805 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
806 {
807 TranslationBlock *tb1, **ptb;
808 unsigned int n1;
809
810 ptb = &tb->jmp_next[n];
811 tb1 = *ptb;
812 if (tb1) {
813 /* find tb(n) in circular list */
814 for (;;) {
815 tb1 = *ptb;
816 n1 = (uintptr_t)tb1 & 3;
817 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
818 if (n1 == n && tb1 == tb) {
819 break;
820 }
821 if (n1 == 2) {
822 ptb = &tb1->jmp_first;
823 } else {
824 ptb = &tb1->jmp_next[n1];
825 }
826 }
827 /* now we can suppress tb(n) from the list */
828 *ptb = tb->jmp_next[n];
829
830 tb->jmp_next[n] = NULL;
831 }
832 }
833
834 /* reset the jump entry 'n' of a TB so that it is not chained to
835 another TB */
836 static inline void tb_reset_jump(TranslationBlock *tb, int n)
837 {
838 tb_set_jmp_target(tb, n, (uintptr_t)(tb->tc_ptr + tb->tb_next_offset[n]));
839 }
840
841 /* invalidate one TB */
842 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
843 {
844 CPUState *cpu;
845 PageDesc *p;
846 unsigned int h, n1;
847 tb_page_addr_t phys_pc;
848 TranslationBlock *tb1, *tb2;
849
850 /* remove the TB from the hash list */
851 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
852 h = tb_phys_hash_func(phys_pc);
853 tb_hash_remove(&tcg_ctx.tb_ctx.tb_phys_hash[h], tb);
854
855 /* remove the TB from the page list */
856 if (tb->page_addr[0] != page_addr) {
857 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
858 tb_page_remove(&p->first_tb, tb);
859 invalidate_page_bitmap(p);
860 }
861 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
862 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
863 tb_page_remove(&p->first_tb, tb);
864 invalidate_page_bitmap(p);
865 }
866
867 tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
868
869 /* remove the TB from the hash list */
870 h = tb_jmp_cache_hash_func(tb->pc);
871 CPU_FOREACH(cpu) {
872 if (cpu->tb_jmp_cache[h] == tb) {
873 cpu->tb_jmp_cache[h] = NULL;
874 }
875 }
876
877 /* suppress this TB from the two jump lists */
878 tb_jmp_remove(tb, 0);
879 tb_jmp_remove(tb, 1);
880
881 /* suppress any remaining jumps to this TB */
882 tb1 = tb->jmp_first;
883 for (;;) {
884 n1 = (uintptr_t)tb1 & 3;
885 if (n1 == 2) {
886 break;
887 }
888 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
889 tb2 = tb1->jmp_next[n1];
890 tb_reset_jump(tb1, n1);
891 tb1->jmp_next[n1] = NULL;
892 tb1 = tb2;
893 }
894 tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2); /* fail safe */
895
896 tcg_ctx.tb_ctx.tb_phys_invalidate_count++;
897 }
898
899 static inline void set_bits(uint8_t *tab, int start, int len)
900 {
901 int end, mask, end1;
902
903 end = start + len;
904 tab += start >> 3;
905 mask = 0xff << (start & 7);
906 if ((start & ~7) == (end & ~7)) {
907 if (start < end) {
908 mask &= ~(0xff << (end & 7));
909 *tab |= mask;
910 }
911 } else {
912 *tab++ |= mask;
913 start = (start + 8) & ~7;
914 end1 = end & ~7;
915 while (start < end1) {
916 *tab++ = 0xff;
917 start += 8;
918 }
919 if (start < end) {
920 mask = ~(0xff << (end & 7));
921 *tab |= mask;
922 }
923 }
924 }
925
926 static void build_page_bitmap(PageDesc *p)
927 {
928 int n, tb_start, tb_end;
929 TranslationBlock *tb;
930
931 p->code_bitmap = g_malloc0(TARGET_PAGE_SIZE / 8);
932
933 tb = p->first_tb;
934 while (tb != NULL) {
935 n = (uintptr_t)tb & 3;
936 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
937 /* NOTE: this is subtle as a TB may span two physical pages */
938 if (n == 0) {
939 /* NOTE: tb_end may be after the end of the page, but
940 it is not a problem */
941 tb_start = tb->pc & ~TARGET_PAGE_MASK;
942 tb_end = tb_start + tb->size;
943 if (tb_end > TARGET_PAGE_SIZE) {
944 tb_end = TARGET_PAGE_SIZE;
945 }
946 } else {
947 tb_start = 0;
948 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
949 }
950 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
951 tb = tb->page_next[n];
952 }
953 }
954
955 TranslationBlock *tb_gen_code(CPUState *cpu,
956 target_ulong pc, target_ulong cs_base,
957 int flags, int cflags)
958 {
959 CPUArchState *env = cpu->env_ptr;
960 TranslationBlock *tb;
961 tb_page_addr_t phys_pc, phys_page2;
962 target_ulong virt_page2;
963 int code_gen_size;
964
965 phys_pc = get_page_addr_code(env, pc);
966 tb = tb_alloc(pc);
967 if (!tb) {
968 /* flush must be done */
969 tb_flush(env);
970 /* cannot fail at this point */
971 tb = tb_alloc(pc);
972 /* Don't forget to invalidate previous TB info. */
973 tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
974 }
975 tb->tc_ptr = tcg_ctx.code_gen_ptr;
976 tb->cs_base = cs_base;
977 tb->flags = flags;
978 tb->cflags = cflags;
979 cpu_gen_code(env, tb, &code_gen_size);
980 tcg_ctx.code_gen_ptr = (void *)(((uintptr_t)tcg_ctx.code_gen_ptr +
981 code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
982
983 /* check next page if needed */
984 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
985 phys_page2 = -1;
986 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
987 phys_page2 = get_page_addr_code(env, virt_page2);
988 }
989 tb_link_page(tb, phys_pc, phys_page2);
990 return tb;
991 }
992
993 /*
994 * Invalidate all TBs which intersect with the target physical address range
995 * [start;end[. NOTE: start and end may refer to *different* physical pages.
996 * 'is_cpu_write_access' should be true if called from a real cpu write
997 * access: the virtual CPU will exit the current TB if code is modified inside
998 * this TB.
999 */
1000 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end,
1001 int is_cpu_write_access)
1002 {
1003 while (start < end) {
1004 tb_invalidate_phys_page_range(start, end, is_cpu_write_access);
1005 start &= TARGET_PAGE_MASK;
1006 start += TARGET_PAGE_SIZE;
1007 }
1008 }
1009
1010 /*
1011 * Invalidate all TBs which intersect with the target physical address range
1012 * [start;end[. NOTE: start and end must refer to the *same* physical page.
1013 * 'is_cpu_write_access' should be true if called from a real cpu write
1014 * access: the virtual CPU will exit the current TB if code is modified inside
1015 * this TB.
1016 */
1017 void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
1018 int is_cpu_write_access)
1019 {
1020 TranslationBlock *tb, *tb_next, *saved_tb;
1021 CPUState *cpu = current_cpu;
1022 #if defined(TARGET_HAS_PRECISE_SMC)
1023 CPUArchState *env = NULL;
1024 #endif
1025 tb_page_addr_t tb_start, tb_end;
1026 PageDesc *p;
1027 int n;
1028 #ifdef TARGET_HAS_PRECISE_SMC
1029 int current_tb_not_found = is_cpu_write_access;
1030 TranslationBlock *current_tb = NULL;
1031 int current_tb_modified = 0;
1032 target_ulong current_pc = 0;
1033 target_ulong current_cs_base = 0;
1034 int current_flags = 0;
1035 #endif /* TARGET_HAS_PRECISE_SMC */
1036
1037 p = page_find(start >> TARGET_PAGE_BITS);
1038 if (!p) {
1039 return;
1040 }
1041 if (!p->code_bitmap &&
1042 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
1043 is_cpu_write_access) {
1044 /* build code bitmap */
1045 build_page_bitmap(p);
1046 }
1047 #if defined(TARGET_HAS_PRECISE_SMC)
1048 if (cpu != NULL) {
1049 env = cpu->env_ptr;
1050 }
1051 #endif
1052
1053 /* we remove all the TBs in the range [start, end[ */
1054 /* XXX: see if in some cases it could be faster to invalidate all
1055 the code */
1056 tb = p->first_tb;
1057 while (tb != NULL) {
1058 n = (uintptr_t)tb & 3;
1059 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1060 tb_next = tb->page_next[n];
1061 /* NOTE: this is subtle as a TB may span two physical pages */
1062 if (n == 0) {
1063 /* NOTE: tb_end may be after the end of the page, but
1064 it is not a problem */
1065 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1066 tb_end = tb_start + tb->size;
1067 } else {
1068 tb_start = tb->page_addr[1];
1069 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1070 }
1071 if (!(tb_end <= start || tb_start >= end)) {
1072 #ifdef TARGET_HAS_PRECISE_SMC
1073 if (current_tb_not_found) {
1074 current_tb_not_found = 0;
1075 current_tb = NULL;
1076 if (cpu->mem_io_pc) {
1077 /* now we have a real cpu fault */
1078 current_tb = tb_find_pc(cpu->mem_io_pc);
1079 }
1080 }
1081 if (current_tb == tb &&
1082 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1083 /* If we are modifying the current TB, we must stop
1084 its execution. We could be more precise by checking
1085 that the modification is after the current PC, but it
1086 would require a specialized function to partially
1087 restore the CPU state */
1088
1089 current_tb_modified = 1;
1090 cpu_restore_state_from_tb(cpu, current_tb, cpu->mem_io_pc);
1091 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1092 &current_flags);
1093 }
1094 #endif /* TARGET_HAS_PRECISE_SMC */
1095 /* we need to do that to handle the case where a signal
1096 occurs while doing tb_phys_invalidate() */
1097 saved_tb = NULL;
1098 if (cpu != NULL) {
1099 saved_tb = cpu->current_tb;
1100 cpu->current_tb = NULL;
1101 }
1102 tb_phys_invalidate(tb, -1);
1103 if (cpu != NULL) {
1104 cpu->current_tb = saved_tb;
1105 if (cpu->interrupt_request && cpu->current_tb) {
1106 cpu_interrupt(cpu, cpu->interrupt_request);
1107 }
1108 }
1109 }
1110 tb = tb_next;
1111 }
1112 #if !defined(CONFIG_USER_ONLY)
1113 /* if no code remaining, no need to continue to use slow writes */
1114 if (!p->first_tb) {
1115 invalidate_page_bitmap(p);
1116 if (is_cpu_write_access) {
1117 tlb_unprotect_code_phys(cpu, start, cpu->mem_io_vaddr);
1118 }
1119 }
1120 #endif
1121 #ifdef TARGET_HAS_PRECISE_SMC
1122 if (current_tb_modified) {
1123 /* we generate a block containing just the instruction
1124 modifying the memory. It will ensure that it cannot modify
1125 itself */
1126 cpu->current_tb = NULL;
1127 tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1);
1128 cpu_resume_from_signal(cpu, NULL);
1129 }
1130 #endif
1131 }
1132
1133 /* len must be <= 8 and start must be a multiple of len */
1134 void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
1135 {
1136 PageDesc *p;
1137 int offset, b;
1138
1139 #if 0
1140 if (1) {
1141 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1142 cpu_single_env->mem_io_vaddr, len,
1143 cpu_single_env->eip,
1144 cpu_single_env->eip +
1145 (intptr_t)cpu_single_env->segs[R_CS].base);
1146 }
1147 #endif
1148 p = page_find(start >> TARGET_PAGE_BITS);
1149 if (!p) {
1150 return;
1151 }
1152 if (p->code_bitmap) {
1153 offset = start & ~TARGET_PAGE_MASK;
1154 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1155 if (b & ((1 << len) - 1)) {
1156 goto do_invalidate;
1157 }
1158 } else {
1159 do_invalidate:
1160 tb_invalidate_phys_page_range(start, start + len, 1);
1161 }
1162 }
1163
1164 #if !defined(CONFIG_SOFTMMU)
1165 static void tb_invalidate_phys_page(tb_page_addr_t addr,
1166 uintptr_t pc, void *puc,
1167 bool locked)
1168 {
1169 TranslationBlock *tb;
1170 PageDesc *p;
1171 int n;
1172 #ifdef TARGET_HAS_PRECISE_SMC
1173 TranslationBlock *current_tb = NULL;
1174 CPUState *cpu = current_cpu;
1175 CPUArchState *env = NULL;
1176 int current_tb_modified = 0;
1177 target_ulong current_pc = 0;
1178 target_ulong current_cs_base = 0;
1179 int current_flags = 0;
1180 #endif
1181
1182 addr &= TARGET_PAGE_MASK;
1183 p = page_find(addr >> TARGET_PAGE_BITS);
1184 if (!p) {
1185 return;
1186 }
1187 tb = p->first_tb;
1188 #ifdef TARGET_HAS_PRECISE_SMC
1189 if (tb && pc != 0) {
1190 current_tb = tb_find_pc(pc);
1191 }
1192 if (cpu != NULL) {
1193 env = cpu->env_ptr;
1194 }
1195 #endif
1196 while (tb != NULL) {
1197 n = (uintptr_t)tb & 3;
1198 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1199 #ifdef TARGET_HAS_PRECISE_SMC
1200 if (current_tb == tb &&
1201 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1202 /* If we are modifying the current TB, we must stop
1203 its execution. We could be more precise by checking
1204 that the modification is after the current PC, but it
1205 would require a specialized function to partially
1206 restore the CPU state */
1207
1208 current_tb_modified = 1;
1209 cpu_restore_state_from_tb(cpu, current_tb, pc);
1210 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1211 &current_flags);
1212 }
1213 #endif /* TARGET_HAS_PRECISE_SMC */
1214 tb_phys_invalidate(tb, addr);
1215 tb = tb->page_next[n];
1216 }
1217 p->first_tb = NULL;
1218 #ifdef TARGET_HAS_PRECISE_SMC
1219 if (current_tb_modified) {
1220 /* we generate a block containing just the instruction
1221 modifying the memory. It will ensure that it cannot modify
1222 itself */
1223 cpu->current_tb = NULL;
1224 tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1);
1225 if (locked) {
1226 mmap_unlock();
1227 }
1228 cpu_resume_from_signal(cpu, puc);
1229 }
1230 #endif
1231 }
1232 #endif
1233
1234 /* add the tb in the target page and protect it if necessary */
1235 static inline void tb_alloc_page(TranslationBlock *tb,
1236 unsigned int n, tb_page_addr_t page_addr)
1237 {
1238 PageDesc *p;
1239 #ifndef CONFIG_USER_ONLY
1240 bool page_already_protected;
1241 #endif
1242
1243 tb->page_addr[n] = page_addr;
1244 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
1245 tb->page_next[n] = p->first_tb;
1246 #ifndef CONFIG_USER_ONLY
1247 page_already_protected = p->first_tb != NULL;
1248 #endif
1249 p->first_tb = (TranslationBlock *)((uintptr_t)tb | n);
1250 invalidate_page_bitmap(p);
1251
1252 #if defined(TARGET_HAS_SMC) || 1
1253
1254 #if defined(CONFIG_USER_ONLY)
1255 if (p->flags & PAGE_WRITE) {
1256 target_ulong addr;
1257 PageDesc *p2;
1258 int prot;
1259
1260 /* force the host page as non writable (writes will have a
1261 page fault + mprotect overhead) */
1262 page_addr &= qemu_host_page_mask;
1263 prot = 0;
1264 for (addr = page_addr; addr < page_addr + qemu_host_page_size;
1265 addr += TARGET_PAGE_SIZE) {
1266
1267 p2 = page_find(addr >> TARGET_PAGE_BITS);
1268 if (!p2) {
1269 continue;
1270 }
1271 prot |= p2->flags;
1272 p2->flags &= ~PAGE_WRITE;
1273 }
1274 mprotect(g2h(page_addr), qemu_host_page_size,
1275 (prot & PAGE_BITS) & ~PAGE_WRITE);
1276 #ifdef DEBUG_TB_INVALIDATE
1277 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
1278 page_addr);
1279 #endif
1280 }
1281 #else
1282 /* if some code is already present, then the pages are already
1283 protected. So we handle the case where only the first TB is
1284 allocated in a physical page */
1285 if (!page_already_protected) {
1286 tlb_protect_code(page_addr);
1287 }
1288 #endif
1289
1290 #endif /* TARGET_HAS_SMC */
1291 }
1292
1293 /* add a new TB and link it to the physical page tables. phys_page2 is
1294 (-1) to indicate that only one page contains the TB. */
1295 static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
1296 tb_page_addr_t phys_page2)
1297 {
1298 unsigned int h;
1299 TranslationBlock **ptb;
1300
1301 /* Grab the mmap lock to stop another thread invalidating this TB
1302 before we are done. */
1303 mmap_lock();
1304 /* add in the physical hash table */
1305 h = tb_phys_hash_func(phys_pc);
1306 ptb = &tcg_ctx.tb_ctx.tb_phys_hash[h];
1307 tb->phys_hash_next = *ptb;
1308 *ptb = tb;
1309
1310 /* add in the page list */
1311 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1312 if (phys_page2 != -1) {
1313 tb_alloc_page(tb, 1, phys_page2);
1314 } else {
1315 tb->page_addr[1] = -1;
1316 }
1317
1318 tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2);
1319 tb->jmp_next[0] = NULL;
1320 tb->jmp_next[1] = NULL;
1321
1322 /* init original jump addresses */
1323 if (tb->tb_next_offset[0] != 0xffff) {
1324 tb_reset_jump(tb, 0);
1325 }
1326 if (tb->tb_next_offset[1] != 0xffff) {
1327 tb_reset_jump(tb, 1);
1328 }
1329
1330 #ifdef DEBUG_TB_CHECK
1331 tb_page_check();
1332 #endif
1333 mmap_unlock();
1334 }
1335
1336 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1337 tb[1].tc_ptr. Return NULL if not found */
1338 static TranslationBlock *tb_find_pc(uintptr_t tc_ptr)
1339 {
1340 int m_min, m_max, m;
1341 uintptr_t v;
1342 TranslationBlock *tb;
1343
1344 if (tcg_ctx.tb_ctx.nb_tbs <= 0) {
1345 return NULL;
1346 }
1347 if (tc_ptr < (uintptr_t)tcg_ctx.code_gen_buffer ||
1348 tc_ptr >= (uintptr_t)tcg_ctx.code_gen_ptr) {
1349 return NULL;
1350 }
1351 /* binary search (cf Knuth) */
1352 m_min = 0;
1353 m_max = tcg_ctx.tb_ctx.nb_tbs - 1;
1354 while (m_min <= m_max) {
1355 m = (m_min + m_max) >> 1;
1356 tb = &tcg_ctx.tb_ctx.tbs[m];
1357 v = (uintptr_t)tb->tc_ptr;
1358 if (v == tc_ptr) {
1359 return tb;
1360 } else if (tc_ptr < v) {
1361 m_max = m - 1;
1362 } else {
1363 m_min = m + 1;
1364 }
1365 }
1366 return &tcg_ctx.tb_ctx.tbs[m_max];
1367 }
1368
1369 #if defined(TARGET_HAS_ICE) && !defined(CONFIG_USER_ONLY)
1370 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr)
1371 {
1372 ram_addr_t ram_addr;
1373 MemoryRegion *mr;
1374 hwaddr l = 1;
1375
1376 mr = address_space_translate(as, addr, &addr, &l, false);
1377 if (!(memory_region_is_ram(mr)
1378 || memory_region_is_romd(mr))) {
1379 return;
1380 }
1381 ram_addr = (memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK)
1382 + addr;
1383 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
1384 }
1385 #endif /* TARGET_HAS_ICE && !defined(CONFIG_USER_ONLY) */
1386
1387 void tb_check_watchpoint(CPUState *cpu)
1388 {
1389 TranslationBlock *tb;
1390
1391 tb = tb_find_pc(cpu->mem_io_pc);
1392 if (!tb) {
1393 cpu_abort(cpu, "check_watchpoint: could not find TB for pc=%p",
1394 (void *)cpu->mem_io_pc);
1395 }
1396 cpu_restore_state_from_tb(cpu, tb, cpu->mem_io_pc);
1397 tb_phys_invalidate(tb, -1);
1398 }
1399
1400 #ifndef CONFIG_USER_ONLY
1401 /* mask must never be zero, except for A20 change call */
1402 static void tcg_handle_interrupt(CPUState *cpu, int mask)
1403 {
1404 int old_mask;
1405
1406 old_mask = cpu->interrupt_request;
1407 cpu->interrupt_request |= mask;
1408
1409 /*
1410 * If called from iothread context, wake the target cpu in
1411 * case its halted.
1412 */
1413 if (!qemu_cpu_is_self(cpu)) {
1414 qemu_cpu_kick(cpu);
1415 return;
1416 }
1417
1418 if (use_icount) {
1419 cpu->icount_decr.u16.high = 0xffff;
1420 if (!cpu_can_do_io(cpu)
1421 && (mask & ~old_mask) != 0) {
1422 cpu_abort(cpu, "Raised interrupt while not in I/O function");
1423 }
1424 } else {
1425 cpu->tcg_exit_req = 1;
1426 }
1427 }
1428
1429 CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
1430
1431 /* in deterministic execution mode, instructions doing device I/Os
1432 must be at the end of the TB */
1433 void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr)
1434 {
1435 #if defined(TARGET_MIPS) || defined(TARGET_SH4)
1436 CPUArchState *env = cpu->env_ptr;
1437 #endif
1438 TranslationBlock *tb;
1439 uint32_t n, cflags;
1440 target_ulong pc, cs_base;
1441 uint64_t flags;
1442
1443 tb = tb_find_pc(retaddr);
1444 if (!tb) {
1445 cpu_abort(cpu, "cpu_io_recompile: could not find TB for pc=%p",
1446 (void *)retaddr);
1447 }
1448 n = cpu->icount_decr.u16.low + tb->icount;
1449 cpu_restore_state_from_tb(cpu, tb, retaddr);
1450 /* Calculate how many instructions had been executed before the fault
1451 occurred. */
1452 n = n - cpu->icount_decr.u16.low;
1453 /* Generate a new TB ending on the I/O insn. */
1454 n++;
1455 /* On MIPS and SH, delay slot instructions can only be restarted if
1456 they were already the first instruction in the TB. If this is not
1457 the first instruction in a TB then re-execute the preceding
1458 branch. */
1459 #if defined(TARGET_MIPS)
1460 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
1461 env->active_tc.PC -= 4;
1462 cpu->icount_decr.u16.low++;
1463 env->hflags &= ~MIPS_HFLAG_BMASK;
1464 }
1465 #elif defined(TARGET_SH4)
1466 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
1467 && n > 1) {
1468 env->pc -= 2;
1469 cpu->icount_decr.u16.low++;
1470 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
1471 }
1472 #endif
1473 /* This should never happen. */
1474 if (n > CF_COUNT_MASK) {
1475 cpu_abort(cpu, "TB too big during recompile");
1476 }
1477
1478 cflags = n | CF_LAST_IO;
1479 pc = tb->pc;
1480 cs_base = tb->cs_base;
1481 flags = tb->flags;
1482 tb_phys_invalidate(tb, -1);
1483 /* FIXME: In theory this could raise an exception. In practice
1484 we have already translated the block once so it's probably ok. */
1485 tb_gen_code(cpu, pc, cs_base, flags, cflags);
1486 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
1487 the first in the TB) then we end up generating a whole new TB and
1488 repeating the fault, which is horribly inefficient.
1489 Better would be to execute just this insn uncached, or generate a
1490 second new TB. */
1491 cpu_resume_from_signal(cpu, NULL);
1492 }
1493
1494 void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr)
1495 {
1496 unsigned int i;
1497
1498 /* Discard jump cache entries for any tb which might potentially
1499 overlap the flushed page. */
1500 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1501 memset(&cpu->tb_jmp_cache[i], 0,
1502 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1503
1504 i = tb_jmp_cache_hash_page(addr);
1505 memset(&cpu->tb_jmp_cache[i], 0,
1506 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1507 }
1508
1509 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
1510 {
1511 int i, target_code_size, max_target_code_size;
1512 int direct_jmp_count, direct_jmp2_count, cross_page;
1513 TranslationBlock *tb;
1514
1515 target_code_size = 0;
1516 max_target_code_size = 0;
1517 cross_page = 0;
1518 direct_jmp_count = 0;
1519 direct_jmp2_count = 0;
1520 for (i = 0; i < tcg_ctx.tb_ctx.nb_tbs; i++) {
1521 tb = &tcg_ctx.tb_ctx.tbs[i];
1522 target_code_size += tb->size;
1523 if (tb->size > max_target_code_size) {
1524 max_target_code_size = tb->size;
1525 }
1526 if (tb->page_addr[1] != -1) {
1527 cross_page++;
1528 }
1529 if (tb->tb_next_offset[0] != 0xffff) {
1530 direct_jmp_count++;
1531 if (tb->tb_next_offset[1] != 0xffff) {
1532 direct_jmp2_count++;
1533 }
1534 }
1535 }
1536 /* XXX: avoid using doubles ? */
1537 cpu_fprintf(f, "Translation buffer state:\n");
1538 cpu_fprintf(f, "gen code size %td/%zd\n",
1539 tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer,
1540 tcg_ctx.code_gen_buffer_max_size);
1541 cpu_fprintf(f, "TB count %d/%d\n",
1542 tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.code_gen_max_blocks);
1543 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
1544 tcg_ctx.tb_ctx.nb_tbs ? target_code_size /
1545 tcg_ctx.tb_ctx.nb_tbs : 0,
1546 max_target_code_size);
1547 cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
1548 tcg_ctx.tb_ctx.nb_tbs ? (tcg_ctx.code_gen_ptr -
1549 tcg_ctx.code_gen_buffer) /
1550 tcg_ctx.tb_ctx.nb_tbs : 0,
1551 target_code_size ? (double) (tcg_ctx.code_gen_ptr -
1552 tcg_ctx.code_gen_buffer) /
1553 target_code_size : 0);
1554 cpu_fprintf(f, "cross page TB count %d (%d%%)\n", cross_page,
1555 tcg_ctx.tb_ctx.nb_tbs ? (cross_page * 100) /
1556 tcg_ctx.tb_ctx.nb_tbs : 0);
1557 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
1558 direct_jmp_count,
1559 tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp_count * 100) /
1560 tcg_ctx.tb_ctx.nb_tbs : 0,
1561 direct_jmp2_count,
1562 tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp2_count * 100) /
1563 tcg_ctx.tb_ctx.nb_tbs : 0);
1564 cpu_fprintf(f, "\nStatistics:\n");
1565 cpu_fprintf(f, "TB flush count %d\n", tcg_ctx.tb_ctx.tb_flush_count);
1566 cpu_fprintf(f, "TB invalidate count %d\n",
1567 tcg_ctx.tb_ctx.tb_phys_invalidate_count);
1568 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
1569 tcg_dump_info(f, cpu_fprintf);
1570 }
1571
1572 #else /* CONFIG_USER_ONLY */
1573
1574 void cpu_interrupt(CPUState *cpu, int mask)
1575 {
1576 cpu->interrupt_request |= mask;
1577 cpu->tcg_exit_req = 1;
1578 }
1579
1580 /*
1581 * Walks guest process memory "regions" one by one
1582 * and calls callback function 'fn' for each region.
1583 */
1584 struct walk_memory_regions_data {
1585 walk_memory_regions_fn fn;
1586 void *priv;
1587 uintptr_t start;
1588 int prot;
1589 };
1590
1591 static int walk_memory_regions_end(struct walk_memory_regions_data *data,
1592 abi_ulong end, int new_prot)
1593 {
1594 if (data->start != -1ul) {
1595 int rc = data->fn(data->priv, data->start, end, data->prot);
1596 if (rc != 0) {
1597 return rc;
1598 }
1599 }
1600
1601 data->start = (new_prot ? end : -1ul);
1602 data->prot = new_prot;
1603
1604 return 0;
1605 }
1606
1607 static int walk_memory_regions_1(struct walk_memory_regions_data *data,
1608 abi_ulong base, int level, void **lp)
1609 {
1610 abi_ulong pa;
1611 int i, rc;
1612
1613 if (*lp == NULL) {
1614 return walk_memory_regions_end(data, base, 0);
1615 }
1616
1617 if (level == 0) {
1618 PageDesc *pd = *lp;
1619
1620 for (i = 0; i < V_L2_SIZE; ++i) {
1621 int prot = pd[i].flags;
1622
1623 pa = base | (i << TARGET_PAGE_BITS);
1624 if (prot != data->prot) {
1625 rc = walk_memory_regions_end(data, pa, prot);
1626 if (rc != 0) {
1627 return rc;
1628 }
1629 }
1630 }
1631 } else {
1632 void **pp = *lp;
1633
1634 for (i = 0; i < V_L2_SIZE; ++i) {
1635 pa = base | ((abi_ulong)i <<
1636 (TARGET_PAGE_BITS + V_L2_BITS * level));
1637 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
1638 if (rc != 0) {
1639 return rc;
1640 }
1641 }
1642 }
1643
1644 return 0;
1645 }
1646
1647 int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
1648 {
1649 struct walk_memory_regions_data data;
1650 uintptr_t i;
1651
1652 data.fn = fn;
1653 data.priv = priv;
1654 data.start = -1ul;
1655 data.prot = 0;
1656
1657 for (i = 0; i < V_L1_SIZE; i++) {
1658 int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
1659 V_L1_SHIFT / V_L2_BITS - 1, l1_map + i);
1660
1661 if (rc != 0) {
1662 return rc;
1663 }
1664 }
1665
1666 return walk_memory_regions_end(&data, 0, 0);
1667 }
1668
1669 static int dump_region(void *priv, abi_ulong start,
1670 abi_ulong end, unsigned long prot)
1671 {
1672 FILE *f = (FILE *)priv;
1673
1674 (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
1675 " "TARGET_ABI_FMT_lx" %c%c%c\n",
1676 start, end, end - start,
1677 ((prot & PAGE_READ) ? 'r' : '-'),
1678 ((prot & PAGE_WRITE) ? 'w' : '-'),
1679 ((prot & PAGE_EXEC) ? 'x' : '-'));
1680
1681 return 0;
1682 }
1683
1684 /* dump memory mappings */
1685 void page_dump(FILE *f)
1686 {
1687 const int length = sizeof(abi_ulong) * 2;
1688 (void) fprintf(f, "%-*s %-*s %-*s %s\n",
1689 length, "start", length, "end", length, "size", "prot");
1690 walk_memory_regions(f, dump_region);
1691 }
1692
1693 int page_get_flags(target_ulong address)
1694 {
1695 PageDesc *p;
1696
1697 p = page_find(address >> TARGET_PAGE_BITS);
1698 if (!p) {
1699 return 0;
1700 }
1701 return p->flags;
1702 }
1703
1704 /* Modify the flags of a page and invalidate the code if necessary.
1705 The flag PAGE_WRITE_ORG is positioned automatically depending
1706 on PAGE_WRITE. The mmap_lock should already be held. */
1707 void page_set_flags(target_ulong start, target_ulong end, int flags)
1708 {
1709 target_ulong addr, len;
1710
1711 /* This function should never be called with addresses outside the
1712 guest address space. If this assert fires, it probably indicates
1713 a missing call to h2g_valid. */
1714 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
1715 assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
1716 #endif
1717 assert(start < end);
1718
1719 start = start & TARGET_PAGE_MASK;
1720 end = TARGET_PAGE_ALIGN(end);
1721
1722 if (flags & PAGE_WRITE) {
1723 flags |= PAGE_WRITE_ORG;
1724 }
1725
1726 for (addr = start, len = end - start;
1727 len != 0;
1728 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
1729 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
1730
1731 /* If the write protection bit is set, then we invalidate
1732 the code inside. */
1733 if (!(p->flags & PAGE_WRITE) &&
1734 (flags & PAGE_WRITE) &&
1735 p->first_tb) {
1736 tb_invalidate_phys_page(addr, 0, NULL, false);
1737 }
1738 p->flags = flags;
1739 }
1740 }
1741
1742 int page_check_range(target_ulong start, target_ulong len, int flags)
1743 {
1744 PageDesc *p;
1745 target_ulong end;
1746 target_ulong addr;
1747
1748 /* This function should never be called with addresses outside the
1749 guest address space. If this assert fires, it probably indicates
1750 a missing call to h2g_valid. */
1751 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
1752 assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
1753 #endif
1754
1755 if (len == 0) {
1756 return 0;
1757 }
1758 if (start + len - 1 < start) {
1759 /* We've wrapped around. */
1760 return -1;
1761 }
1762
1763 /* must do before we loose bits in the next step */
1764 end = TARGET_PAGE_ALIGN(start + len);
1765 start = start & TARGET_PAGE_MASK;
1766
1767 for (addr = start, len = end - start;
1768 len != 0;
1769 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
1770 p = page_find(addr >> TARGET_PAGE_BITS);
1771 if (!p) {
1772 return -1;
1773 }
1774 if (!(p->flags & PAGE_VALID)) {
1775 return -1;
1776 }
1777
1778 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) {
1779 return -1;
1780 }
1781 if (flags & PAGE_WRITE) {
1782 if (!(p->flags & PAGE_WRITE_ORG)) {
1783 return -1;
1784 }
1785 /* unprotect the page if it was put read-only because it
1786 contains translated code */
1787 if (!(p->flags & PAGE_WRITE)) {
1788 if (!page_unprotect(addr, 0, NULL)) {
1789 return -1;
1790 }
1791 }
1792 }
1793 }
1794 return 0;
1795 }
1796
1797 /* called from signal handler: invalidate the code and unprotect the
1798 page. Return TRUE if the fault was successfully handled. */
1799 int page_unprotect(target_ulong address, uintptr_t pc, void *puc)
1800 {
1801 unsigned int prot;
1802 PageDesc *p;
1803 target_ulong host_start, host_end, addr;
1804
1805 /* Technically this isn't safe inside a signal handler. However we
1806 know this only ever happens in a synchronous SEGV handler, so in
1807 practice it seems to be ok. */
1808 mmap_lock();
1809
1810 p = page_find(address >> TARGET_PAGE_BITS);
1811 if (!p) {
1812 mmap_unlock();
1813 return 0;
1814 }
1815
1816 /* if the page was really writable, then we change its
1817 protection back to writable */
1818 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
1819 host_start = address & qemu_host_page_mask;
1820 host_end = host_start + qemu_host_page_size;
1821
1822 prot = 0;
1823 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
1824 p = page_find(addr >> TARGET_PAGE_BITS);
1825 p->flags |= PAGE_WRITE;
1826 prot |= p->flags;
1827
1828 /* and since the content will be modified, we must invalidate
1829 the corresponding translated code. */
1830 tb_invalidate_phys_page(addr, pc, puc, true);
1831 #ifdef DEBUG_TB_CHECK
1832 tb_invalidate_check(addr);
1833 #endif
1834 }
1835 mprotect((void *)g2h(host_start), qemu_host_page_size,
1836 prot & PAGE_BITS);
1837
1838 mmap_unlock();
1839 return 1;
1840 }
1841 mmap_unlock();
1842 return 0;
1843 }
1844 #endif /* CONFIG_USER_ONLY */