1 /* 2 * internal execution defines for qemu 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.1 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 20 #ifndef EXEC_ALL_H 21 #define EXEC_ALL_H 22 23 #include "cpu.h" 24 #ifdef CONFIG_TCG 25 #include "exec/cpu_ldst.h" 26 #endif 27 #include "qemu/interval-tree.h" 28 #include "qemu/clang-tsa.h" 29 30 /* allow to see translation results - the slowdown should be negligible, so we leave it */ 31 #define DEBUG_DISAS 32 33 /* Page tracking code uses ram addresses in system mode, and virtual 34 addresses in userspace mode. Define tb_page_addr_t to be an appropriate 35 type. */ 36 #if defined(CONFIG_USER_ONLY) 37 typedef abi_ulong tb_page_addr_t; 38 #define TB_PAGE_ADDR_FMT TARGET_ABI_FMT_lx 39 #else 40 typedef ram_addr_t tb_page_addr_t; 41 #define TB_PAGE_ADDR_FMT RAM_ADDR_FMT 42 #endif 43 44 /** 45 * cpu_unwind_state_data: 46 * @cpu: the cpu context 47 * @host_pc: the host pc within the translation 48 * @data: output data 49 * 50 * Attempt to load the the unwind state for a host pc occurring in 51 * translated code. If @host_pc is not in translated code, the 52 * function returns false; otherwise @data is loaded. 53 * This is the same unwind info as given to restore_state_to_opc. 54 */ 55 bool cpu_unwind_state_data(CPUState *cpu, uintptr_t host_pc, uint64_t *data); 56 57 /** 58 * cpu_restore_state: 59 * @cpu: the cpu context 60 * @host_pc: the host pc within the translation 61 * @return: true if state was restored, false otherwise 62 * 63 * Attempt to restore the state for a fault occurring in translated 64 * code. If @host_pc is not in translated code no state is 65 * restored and the function returns false. 66 */ 67 bool cpu_restore_state(CPUState *cpu, uintptr_t host_pc); 68 69 G_NORETURN void cpu_loop_exit_noexc(CPUState *cpu); 70 G_NORETURN void cpu_loop_exit(CPUState *cpu); 71 G_NORETURN void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc); 72 G_NORETURN void cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc); 73 74 /** 75 * cpu_loop_exit_requested: 76 * @cpu: The CPU state to be tested 77 * 78 * Indicate if somebody asked for a return of the CPU to the main loop 79 * (e.g., via cpu_exit() or cpu_interrupt()). 80 * 81 * This is helpful for architectures that support interruptible 82 * instructions. After writing back all state to registers/memory, this 83 * call can be used to check if it makes sense to return to the main loop 84 * or to continue executing the interruptible instruction. 85 */ 86 static inline bool cpu_loop_exit_requested(CPUState *cpu) 87 { 88 return (int32_t)qatomic_read(&cpu_neg(cpu)->icount_decr.u32) < 0; 89 } 90 91 #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG) 92 /* cputlb.c */ 93 /** 94 * tlb_init - initialize a CPU's TLB 95 * @cpu: CPU whose TLB should be initialized 96 */ 97 void tlb_init(CPUState *cpu); 98 /** 99 * tlb_destroy - destroy a CPU's TLB 100 * @cpu: CPU whose TLB should be destroyed 101 */ 102 void tlb_destroy(CPUState *cpu); 103 /** 104 * tlb_flush_page: 105 * @cpu: CPU whose TLB should be flushed 106 * @addr: virtual address of page to be flushed 107 * 108 * Flush one page from the TLB of the specified CPU, for all 109 * MMU indexes. 110 */ 111 void tlb_flush_page(CPUState *cpu, target_ulong addr); 112 /** 113 * tlb_flush_page_all_cpus: 114 * @cpu: src CPU of the flush 115 * @addr: virtual address of page to be flushed 116 * 117 * Flush one page from the TLB of the specified CPU, for all 118 * MMU indexes. 119 */ 120 void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr); 121 /** 122 * tlb_flush_page_all_cpus_synced: 123 * @cpu: src CPU of the flush 124 * @addr: virtual address of page to be flushed 125 * 126 * Flush one page from the TLB of the specified CPU, for all MMU 127 * indexes like tlb_flush_page_all_cpus except the source vCPUs work 128 * is scheduled as safe work meaning all flushes will be complete once 129 * the source vCPUs safe work is complete. This will depend on when 130 * the guests translation ends the TB. 131 */ 132 void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr); 133 /** 134 * tlb_flush: 135 * @cpu: CPU whose TLB should be flushed 136 * 137 * Flush the entire TLB for the specified CPU. Most CPU architectures 138 * allow the implementation to drop entries from the TLB at any time 139 * so this is generally safe. If more selective flushing is required 140 * use one of the other functions for efficiency. 141 */ 142 void tlb_flush(CPUState *cpu); 143 /** 144 * tlb_flush_all_cpus: 145 * @cpu: src CPU of the flush 146 */ 147 void tlb_flush_all_cpus(CPUState *src_cpu); 148 /** 149 * tlb_flush_all_cpus_synced: 150 * @cpu: src CPU of the flush 151 * 152 * Like tlb_flush_all_cpus except this except the source vCPUs work is 153 * scheduled as safe work meaning all flushes will be complete once 154 * the source vCPUs safe work is complete. This will depend on when 155 * the guests translation ends the TB. 156 */ 157 void tlb_flush_all_cpus_synced(CPUState *src_cpu); 158 /** 159 * tlb_flush_page_by_mmuidx: 160 * @cpu: CPU whose TLB should be flushed 161 * @addr: virtual address of page to be flushed 162 * @idxmap: bitmap of MMU indexes to flush 163 * 164 * Flush one page from the TLB of the specified CPU, for the specified 165 * MMU indexes. 166 */ 167 void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr, 168 uint16_t idxmap); 169 /** 170 * tlb_flush_page_by_mmuidx_all_cpus: 171 * @cpu: Originating CPU of the flush 172 * @addr: virtual address of page to be flushed 173 * @idxmap: bitmap of MMU indexes to flush 174 * 175 * Flush one page from the TLB of all CPUs, for the specified 176 * MMU indexes. 177 */ 178 void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu, target_ulong addr, 179 uint16_t idxmap); 180 /** 181 * tlb_flush_page_by_mmuidx_all_cpus_synced: 182 * @cpu: Originating CPU of the flush 183 * @addr: virtual address of page to be flushed 184 * @idxmap: bitmap of MMU indexes to flush 185 * 186 * Flush one page from the TLB of all CPUs, for the specified MMU 187 * indexes like tlb_flush_page_by_mmuidx_all_cpus except the source 188 * vCPUs work is scheduled as safe work meaning all flushes will be 189 * complete once the source vCPUs safe work is complete. This will 190 * depend on when the guests translation ends the TB. 191 */ 192 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu, target_ulong addr, 193 uint16_t idxmap); 194 /** 195 * tlb_flush_by_mmuidx: 196 * @cpu: CPU whose TLB should be flushed 197 * @wait: If true ensure synchronisation by exiting the cpu_loop 198 * @idxmap: bitmap of MMU indexes to flush 199 * 200 * Flush all entries from the TLB of the specified CPU, for the specified 201 * MMU indexes. 202 */ 203 void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap); 204 /** 205 * tlb_flush_by_mmuidx_all_cpus: 206 * @cpu: Originating CPU of the flush 207 * @idxmap: bitmap of MMU indexes to flush 208 * 209 * Flush all entries from all TLBs of all CPUs, for the specified 210 * MMU indexes. 211 */ 212 void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap); 213 /** 214 * tlb_flush_by_mmuidx_all_cpus_synced: 215 * @cpu: Originating CPU of the flush 216 * @idxmap: bitmap of MMU indexes to flush 217 * 218 * Flush all entries from all TLBs of all CPUs, for the specified 219 * MMU indexes like tlb_flush_by_mmuidx_all_cpus except except the source 220 * vCPUs work is scheduled as safe work meaning all flushes will be 221 * complete once the source vCPUs safe work is complete. This will 222 * depend on when the guests translation ends the TB. 223 */ 224 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu, uint16_t idxmap); 225 226 /** 227 * tlb_flush_page_bits_by_mmuidx 228 * @cpu: CPU whose TLB should be flushed 229 * @addr: virtual address of page to be flushed 230 * @idxmap: bitmap of mmu indexes to flush 231 * @bits: number of significant bits in address 232 * 233 * Similar to tlb_flush_page_mask, but with a bitmap of indexes. 234 */ 235 void tlb_flush_page_bits_by_mmuidx(CPUState *cpu, target_ulong addr, 236 uint16_t idxmap, unsigned bits); 237 238 /* Similarly, with broadcast and syncing. */ 239 void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState *cpu, target_ulong addr, 240 uint16_t idxmap, unsigned bits); 241 void tlb_flush_page_bits_by_mmuidx_all_cpus_synced 242 (CPUState *cpu, target_ulong addr, uint16_t idxmap, unsigned bits); 243 244 /** 245 * tlb_flush_range_by_mmuidx 246 * @cpu: CPU whose TLB should be flushed 247 * @addr: virtual address of the start of the range to be flushed 248 * @len: length of range to be flushed 249 * @idxmap: bitmap of mmu indexes to flush 250 * @bits: number of significant bits in address 251 * 252 * For each mmuidx in @idxmap, flush all pages within [@addr,@addr+@len), 253 * comparing only the low @bits worth of each virtual page. 254 */ 255 void tlb_flush_range_by_mmuidx(CPUState *cpu, target_ulong addr, 256 target_ulong len, uint16_t idxmap, 257 unsigned bits); 258 259 /* Similarly, with broadcast and syncing. */ 260 void tlb_flush_range_by_mmuidx_all_cpus(CPUState *cpu, target_ulong addr, 261 target_ulong len, uint16_t idxmap, 262 unsigned bits); 263 void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *cpu, 264 target_ulong addr, 265 target_ulong len, 266 uint16_t idxmap, 267 unsigned bits); 268 269 /** 270 * tlb_set_page_full: 271 * @cpu: CPU context 272 * @mmu_idx: mmu index of the tlb to modify 273 * @vaddr: virtual address of the entry to add 274 * @full: the details of the tlb entry 275 * 276 * Add an entry to @cpu tlb index @mmu_idx. All of the fields of 277 * @full must be filled, except for xlat_section, and constitute 278 * the complete description of the translated page. 279 * 280 * This is generally called by the target tlb_fill function after 281 * having performed a successful page table walk to find the physical 282 * address and attributes for the translation. 283 * 284 * At most one entry for a given virtual address is permitted. Only a 285 * single TARGET_PAGE_SIZE region is mapped; @full->lg_page_size is only 286 * used by tlb_flush_page. 287 */ 288 void tlb_set_page_full(CPUState *cpu, int mmu_idx, target_ulong vaddr, 289 CPUTLBEntryFull *full); 290 291 /** 292 * tlb_set_page_with_attrs: 293 * @cpu: CPU to add this TLB entry for 294 * @vaddr: virtual address of page to add entry for 295 * @paddr: physical address of the page 296 * @attrs: memory transaction attributes 297 * @prot: access permissions (PAGE_READ/PAGE_WRITE/PAGE_EXEC bits) 298 * @mmu_idx: MMU index to insert TLB entry for 299 * @size: size of the page in bytes 300 * 301 * Add an entry to this CPU's TLB (a mapping from virtual address 302 * @vaddr to physical address @paddr) with the specified memory 303 * transaction attributes. This is generally called by the target CPU 304 * specific code after it has been called through the tlb_fill() 305 * entry point and performed a successful page table walk to find 306 * the physical address and attributes for the virtual address 307 * which provoked the TLB miss. 308 * 309 * At most one entry for a given virtual address is permitted. Only a 310 * single TARGET_PAGE_SIZE region is mapped; the supplied @size is only 311 * used by tlb_flush_page. 312 */ 313 void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr, 314 hwaddr paddr, MemTxAttrs attrs, 315 int prot, int mmu_idx, target_ulong size); 316 /* tlb_set_page: 317 * 318 * This function is equivalent to calling tlb_set_page_with_attrs() 319 * with an @attrs argument of MEMTXATTRS_UNSPECIFIED. It's provided 320 * as a convenience for CPUs which don't use memory transaction attributes. 321 */ 322 void tlb_set_page(CPUState *cpu, target_ulong vaddr, 323 hwaddr paddr, int prot, 324 int mmu_idx, target_ulong size); 325 #else 326 static inline void tlb_init(CPUState *cpu) 327 { 328 } 329 static inline void tlb_destroy(CPUState *cpu) 330 { 331 } 332 static inline void tlb_flush_page(CPUState *cpu, target_ulong addr) 333 { 334 } 335 static inline void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr) 336 { 337 } 338 static inline void tlb_flush_page_all_cpus_synced(CPUState *src, 339 target_ulong addr) 340 { 341 } 342 static inline void tlb_flush(CPUState *cpu) 343 { 344 } 345 static inline void tlb_flush_all_cpus(CPUState *src_cpu) 346 { 347 } 348 static inline void tlb_flush_all_cpus_synced(CPUState *src_cpu) 349 { 350 } 351 static inline void tlb_flush_page_by_mmuidx(CPUState *cpu, 352 target_ulong addr, uint16_t idxmap) 353 { 354 } 355 356 static inline void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap) 357 { 358 } 359 static inline void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu, 360 target_ulong addr, 361 uint16_t idxmap) 362 { 363 } 364 static inline void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu, 365 target_ulong addr, 366 uint16_t idxmap) 367 { 368 } 369 static inline void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap) 370 { 371 } 372 373 static inline void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu, 374 uint16_t idxmap) 375 { 376 } 377 static inline void tlb_flush_page_bits_by_mmuidx(CPUState *cpu, 378 target_ulong addr, 379 uint16_t idxmap, 380 unsigned bits) 381 { 382 } 383 static inline void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState *cpu, 384 target_ulong addr, 385 uint16_t idxmap, 386 unsigned bits) 387 { 388 } 389 static inline void 390 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState *cpu, target_ulong addr, 391 uint16_t idxmap, unsigned bits) 392 { 393 } 394 static inline void tlb_flush_range_by_mmuidx(CPUState *cpu, target_ulong addr, 395 target_ulong len, uint16_t idxmap, 396 unsigned bits) 397 { 398 } 399 static inline void tlb_flush_range_by_mmuidx_all_cpus(CPUState *cpu, 400 target_ulong addr, 401 target_ulong len, 402 uint16_t idxmap, 403 unsigned bits) 404 { 405 } 406 static inline void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *cpu, 407 target_ulong addr, 408 target_long len, 409 uint16_t idxmap, 410 unsigned bits) 411 { 412 } 413 #endif 414 /** 415 * probe_access: 416 * @env: CPUArchState 417 * @addr: guest virtual address to look up 418 * @size: size of the access 419 * @access_type: read, write or execute permission 420 * @mmu_idx: MMU index to use for lookup 421 * @retaddr: return address for unwinding 422 * 423 * Look up the guest virtual address @addr. Raise an exception if the 424 * page does not satisfy @access_type. Raise an exception if the 425 * access (@addr, @size) hits a watchpoint. For writes, mark a clean 426 * page as dirty. 427 * 428 * Finally, return the host address for a page that is backed by RAM, 429 * or NULL if the page requires I/O. 430 */ 431 void *probe_access(CPUArchState *env, target_ulong addr, int size, 432 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr); 433 434 static inline void *probe_write(CPUArchState *env, target_ulong addr, int size, 435 int mmu_idx, uintptr_t retaddr) 436 { 437 return probe_access(env, addr, size, MMU_DATA_STORE, mmu_idx, retaddr); 438 } 439 440 static inline void *probe_read(CPUArchState *env, target_ulong addr, int size, 441 int mmu_idx, uintptr_t retaddr) 442 { 443 return probe_access(env, addr, size, MMU_DATA_LOAD, mmu_idx, retaddr); 444 } 445 446 /** 447 * probe_access_flags: 448 * @env: CPUArchState 449 * @addr: guest virtual address to look up 450 * @size: size of the access 451 * @access_type: read, write or execute permission 452 * @mmu_idx: MMU index to use for lookup 453 * @nonfault: suppress the fault 454 * @phost: return value for host address 455 * @retaddr: return address for unwinding 456 * 457 * Similar to probe_access, loosely returning the TLB_FLAGS_MASK for 458 * the page, and storing the host address for RAM in @phost. 459 * 460 * If @nonfault is set, do not raise an exception but return TLB_INVALID_MASK. 461 * Do not handle watchpoints, but include TLB_WATCHPOINT in the returned flags. 462 * Do handle clean pages, so exclude TLB_NOTDIRY from the returned flags. 463 * For simplicity, all "mmio-like" flags are folded to TLB_MMIO. 464 */ 465 int probe_access_flags(CPUArchState *env, target_ulong addr, int size, 466 MMUAccessType access_type, int mmu_idx, 467 bool nonfault, void **phost, uintptr_t retaddr); 468 469 #ifndef CONFIG_USER_ONLY 470 /** 471 * probe_access_full: 472 * Like probe_access_flags, except also return into @pfull. 473 * 474 * The CPUTLBEntryFull structure returned via @pfull is transient 475 * and must be consumed or copied immediately, before any further 476 * access or changes to TLB @mmu_idx. 477 */ 478 int probe_access_full(CPUArchState *env, target_ulong addr, int size, 479 MMUAccessType access_type, int mmu_idx, 480 bool nonfault, void **phost, 481 CPUTLBEntryFull **pfull, uintptr_t retaddr); 482 #endif 483 484 #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */ 485 486 /* Estimated block size for TB allocation. */ 487 /* ??? The following is based on a 2015 survey of x86_64 host output. 488 Better would seem to be some sort of dynamically sized TB array, 489 adapting to the block sizes actually being produced. */ 490 #if defined(CONFIG_SOFTMMU) 491 #define CODE_GEN_AVG_BLOCK_SIZE 400 492 #else 493 #define CODE_GEN_AVG_BLOCK_SIZE 150 494 #endif 495 496 /* 497 * Translation Cache-related fields of a TB. 498 * This struct exists just for convenience; we keep track of TB's in a binary 499 * search tree, and the only fields needed to compare TB's in the tree are 500 * @ptr and @size. 501 * Note: the address of search data can be obtained by adding @size to @ptr. 502 */ 503 struct tb_tc { 504 const void *ptr; /* pointer to the translated code */ 505 size_t size; 506 }; 507 508 struct TranslationBlock { 509 /* 510 * Guest PC corresponding to this block. This must be the true 511 * virtual address. Therefore e.g. x86 stores EIP + CS_BASE, and 512 * targets like Arm, MIPS, HP-PA, which reuse low bits for ISA or 513 * privilege, must store those bits elsewhere. 514 * 515 * If CF_PCREL, the opcodes for the TranslationBlock are written 516 * such that the TB is associated only with the physical page and 517 * may be run in any virtual address context. In this case, PC 518 * must always be taken from ENV in a target-specific manner. 519 * Unwind information is taken as offsets from the page, to be 520 * deposited into the "current" PC. 521 */ 522 target_ulong pc; 523 524 /* 525 * Target-specific data associated with the TranslationBlock, e.g.: 526 * x86: the original user, the Code Segment virtual base, 527 * arm: an extension of tb->flags, 528 * s390x: instruction data for EXECUTE, 529 * sparc: the next pc of the instruction queue (for delay slots). 530 */ 531 target_ulong cs_base; 532 533 uint32_t flags; /* flags defining in which context the code was generated */ 534 uint32_t cflags; /* compile flags */ 535 536 /* Note that TCG_MAX_INSNS is 512; we validate this match elsewhere. */ 537 #define CF_COUNT_MASK 0x000001ff 538 #define CF_NO_GOTO_TB 0x00000200 /* Do not chain with goto_tb */ 539 #define CF_NO_GOTO_PTR 0x00000400 /* Do not chain with goto_ptr */ 540 #define CF_SINGLE_STEP 0x00000800 /* gdbstub single-step in effect */ 541 #define CF_LAST_IO 0x00008000 /* Last insn may be an IO access. */ 542 #define CF_MEMI_ONLY 0x00010000 /* Only instrument memory ops */ 543 #define CF_USE_ICOUNT 0x00020000 544 #define CF_INVALID 0x00040000 /* TB is stale. Set with @jmp_lock held */ 545 #define CF_PARALLEL 0x00080000 /* Generate code for a parallel context */ 546 #define CF_NOIRQ 0x00100000 /* Generate an uninterruptible TB */ 547 #define CF_PCREL 0x00200000 /* Opcodes in TB are PC-relative */ 548 #define CF_CLUSTER_MASK 0xff000000 /* Top 8 bits are cluster ID */ 549 #define CF_CLUSTER_SHIFT 24 550 551 /* Per-vCPU dynamic tracing state used to generate this TB */ 552 uint32_t trace_vcpu_dstate; 553 554 /* 555 * Above fields used for comparing 556 */ 557 558 /* size of target code for this block (1 <= size <= TARGET_PAGE_SIZE) */ 559 uint16_t size; 560 uint16_t icount; 561 562 struct tb_tc tc; 563 564 /* 565 * Track tb_page_addr_t intervals that intersect this TB. 566 * For user-only, the virtual addresses are always contiguous, 567 * and we use a unified interval tree. For system, we use a 568 * linked list headed in each PageDesc. Within the list, the lsb 569 * of the previous pointer tells the index of page_next[], and the 570 * list is protected by the PageDesc lock(s). 571 */ 572 #ifdef CONFIG_USER_ONLY 573 IntervalTreeNode itree; 574 #else 575 uintptr_t page_next[2]; 576 tb_page_addr_t page_addr[2]; 577 #endif 578 579 /* jmp_lock placed here to fill a 4-byte hole. Its documentation is below */ 580 QemuSpin jmp_lock; 581 582 /* The following data are used to directly call another TB from 583 * the code of this one. This can be done either by emitting direct or 584 * indirect native jump instructions. These jumps are reset so that the TB 585 * just continues its execution. The TB can be linked to another one by 586 * setting one of the jump targets (or patching the jump instruction). Only 587 * two of such jumps are supported. 588 */ 589 #define TB_JMP_OFFSET_INVALID 0xffff /* indicates no jump generated */ 590 uint16_t jmp_reset_offset[2]; /* offset of original jump target */ 591 uint16_t jmp_insn_offset[2]; /* offset of direct jump insn */ 592 uintptr_t jmp_target_addr[2]; /* target address */ 593 594 /* 595 * Each TB has a NULL-terminated list (jmp_list_head) of incoming jumps. 596 * Each TB can have two outgoing jumps, and therefore can participate 597 * in two lists. The list entries are kept in jmp_list_next[2]. The least 598 * significant bit (LSB) of the pointers in these lists is used to encode 599 * which of the two list entries is to be used in the pointed TB. 600 * 601 * List traversals are protected by jmp_lock. The destination TB of each 602 * outgoing jump is kept in jmp_dest[] so that the appropriate jmp_lock 603 * can be acquired from any origin TB. 604 * 605 * jmp_dest[] are tagged pointers as well. The LSB is set when the TB is 606 * being invalidated, so that no further outgoing jumps from it can be set. 607 * 608 * jmp_lock also protects the CF_INVALID cflag; a jump must not be chained 609 * to a destination TB that has CF_INVALID set. 610 */ 611 uintptr_t jmp_list_head; 612 uintptr_t jmp_list_next[2]; 613 uintptr_t jmp_dest[2]; 614 }; 615 616 /* Hide the qatomic_read to make code a little easier on the eyes */ 617 static inline uint32_t tb_cflags(const TranslationBlock *tb) 618 { 619 return qatomic_read(&tb->cflags); 620 } 621 622 static inline tb_page_addr_t tb_page_addr0(const TranslationBlock *tb) 623 { 624 #ifdef CONFIG_USER_ONLY 625 return tb->itree.start; 626 #else 627 return tb->page_addr[0]; 628 #endif 629 } 630 631 static inline tb_page_addr_t tb_page_addr1(const TranslationBlock *tb) 632 { 633 #ifdef CONFIG_USER_ONLY 634 tb_page_addr_t next = tb->itree.last & TARGET_PAGE_MASK; 635 return next == (tb->itree.start & TARGET_PAGE_MASK) ? -1 : next; 636 #else 637 return tb->page_addr[1]; 638 #endif 639 } 640 641 static inline void tb_set_page_addr0(TranslationBlock *tb, 642 tb_page_addr_t addr) 643 { 644 #ifdef CONFIG_USER_ONLY 645 tb->itree.start = addr; 646 /* 647 * To begin, we record an interval of one byte. When the translation 648 * loop encounters a second page, the interval will be extended to 649 * include the first byte of the second page, which is sufficient to 650 * allow tb_page_addr1() above to work properly. The final corrected 651 * interval will be set by tb_page_add() from tb->size before the 652 * node is added to the interval tree. 653 */ 654 tb->itree.last = addr; 655 #else 656 tb->page_addr[0] = addr; 657 #endif 658 } 659 660 static inline void tb_set_page_addr1(TranslationBlock *tb, 661 tb_page_addr_t addr) 662 { 663 #ifdef CONFIG_USER_ONLY 664 /* Extend the interval to the first byte of the second page. See above. */ 665 tb->itree.last = addr; 666 #else 667 tb->page_addr[1] = addr; 668 #endif 669 } 670 671 /* current cflags for hashing/comparison */ 672 uint32_t curr_cflags(CPUState *cpu); 673 674 /* TranslationBlock invalidate API */ 675 #if defined(CONFIG_USER_ONLY) 676 void tb_invalidate_phys_addr(target_ulong addr); 677 #else 678 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr, MemTxAttrs attrs); 679 #endif 680 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr); 681 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last); 682 void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr); 683 684 /* GETPC is the true target of the return instruction that we'll execute. */ 685 #if defined(CONFIG_TCG_INTERPRETER) 686 extern __thread uintptr_t tci_tb_ptr; 687 # define GETPC() tci_tb_ptr 688 #else 689 # define GETPC() \ 690 ((uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0))) 691 #endif 692 693 /* The true return address will often point to a host insn that is part of 694 the next translated guest insn. Adjust the address backward to point to 695 the middle of the call insn. Subtracting one would do the job except for 696 several compressed mode architectures (arm, mips) which set the low bit 697 to indicate the compressed mode; subtracting two works around that. It 698 is also the case that there are no host isas that contain a call insn 699 smaller than 4 bytes, so we don't worry about special-casing this. */ 700 #define GETPC_ADJ 2 701 702 #if !defined(CONFIG_USER_ONLY) 703 704 /** 705 * iotlb_to_section: 706 * @cpu: CPU performing the access 707 * @index: TCG CPU IOTLB entry 708 * 709 * Given a TCG CPU IOTLB entry, return the MemoryRegionSection that 710 * it refers to. @index will have been initially created and returned 711 * by memory_region_section_get_iotlb(). 712 */ 713 struct MemoryRegionSection *iotlb_to_section(CPUState *cpu, 714 hwaddr index, MemTxAttrs attrs); 715 #endif 716 717 /** 718 * get_page_addr_code_hostp() 719 * @env: CPUArchState 720 * @addr: guest virtual address of guest code 721 * 722 * See get_page_addr_code() (full-system version) for documentation on the 723 * return value. 724 * 725 * Sets *@hostp (when @hostp is non-NULL) as follows. 726 * If the return value is -1, sets *@hostp to NULL. Otherwise, sets *@hostp 727 * to the host address where @addr's content is kept. 728 * 729 * Note: this function can trigger an exception. 730 */ 731 tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr, 732 void **hostp); 733 734 /** 735 * get_page_addr_code() 736 * @env: CPUArchState 737 * @addr: guest virtual address of guest code 738 * 739 * If we cannot translate and execute from the entire RAM page, or if 740 * the region is not backed by RAM, returns -1. Otherwise, returns the 741 * ram_addr_t corresponding to the guest code at @addr. 742 * 743 * Note: this function can trigger an exception. 744 */ 745 static inline tb_page_addr_t get_page_addr_code(CPUArchState *env, 746 target_ulong addr) 747 { 748 return get_page_addr_code_hostp(env, addr, NULL); 749 } 750 751 #if defined(CONFIG_USER_ONLY) 752 void TSA_NO_TSA mmap_lock(void); 753 void TSA_NO_TSA mmap_unlock(void); 754 bool have_mmap_lock(void); 755 756 /** 757 * adjust_signal_pc: 758 * @pc: raw pc from the host signal ucontext_t. 759 * @is_write: host memory operation was write, or read-modify-write. 760 * 761 * Alter @pc as required for unwinding. Return the type of the 762 * guest memory access -- host reads may be for guest execution. 763 */ 764 MMUAccessType adjust_signal_pc(uintptr_t *pc, bool is_write); 765 766 /** 767 * handle_sigsegv_accerr_write: 768 * @cpu: the cpu context 769 * @old_set: the sigset_t from the signal ucontext_t 770 * @host_pc: the host pc, adjusted for the signal 771 * @host_addr: the host address of the fault 772 * 773 * Return true if the write fault has been handled, and should be re-tried. 774 */ 775 bool handle_sigsegv_accerr_write(CPUState *cpu, sigset_t *old_set, 776 uintptr_t host_pc, abi_ptr guest_addr); 777 778 /** 779 * cpu_loop_exit_sigsegv: 780 * @cpu: the cpu context 781 * @addr: the guest address of the fault 782 * @access_type: access was read/write/execute 783 * @maperr: true for invalid page, false for permission fault 784 * @ra: host pc for unwinding 785 * 786 * Use the TCGCPUOps hook to record cpu state, do guest operating system 787 * specific things to raise SIGSEGV, and jump to the main cpu loop. 788 */ 789 G_NORETURN void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr, 790 MMUAccessType access_type, 791 bool maperr, uintptr_t ra); 792 793 /** 794 * cpu_loop_exit_sigbus: 795 * @cpu: the cpu context 796 * @addr: the guest address of the alignment fault 797 * @access_type: access was read/write/execute 798 * @ra: host pc for unwinding 799 * 800 * Use the TCGCPUOps hook to record cpu state, do guest operating system 801 * specific things to raise SIGBUS, and jump to the main cpu loop. 802 */ 803 G_NORETURN void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, 804 MMUAccessType access_type, 805 uintptr_t ra); 806 807 #else 808 static inline void mmap_lock(void) {} 809 static inline void mmap_unlock(void) {} 810 811 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length); 812 void tlb_set_dirty(CPUState *cpu, target_ulong vaddr); 813 814 MemoryRegionSection * 815 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr, 816 hwaddr *xlat, hwaddr *plen, 817 MemTxAttrs attrs, int *prot); 818 hwaddr memory_region_section_get_iotlb(CPUState *cpu, 819 MemoryRegionSection *section); 820 #endif 821 822 #endif 823