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