1 /* 2 * emulator main execution loop 3 * 4 * Copyright (c) 2003-2005 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 #include "qemu/osdep.h" 21 #include "qemu-common.h" 22 #include "qemu/qemu-print.h" 23 #include "hw/core/tcg-cpu-ops.h" 24 #include "trace.h" 25 #include "disas/disas.h" 26 #include "exec/exec-all.h" 27 #include "tcg/tcg.h" 28 #include "qemu/atomic.h" 29 #include "qemu/compiler.h" 30 #include "qemu/timer.h" 31 #include "qemu/rcu.h" 32 #include "exec/log.h" 33 #include "qemu/main-loop.h" 34 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY) 35 #include "hw/i386/apic.h" 36 #endif 37 #include "sysemu/cpus.h" 38 #include "exec/cpu-all.h" 39 #include "sysemu/cpu-timers.h" 40 #include "sysemu/replay.h" 41 #include "exec/helper-proto.h" 42 #include "tb-hash.h" 43 #include "tb-context.h" 44 #include "internal.h" 45 46 /* -icount align implementation. */ 47 48 typedef struct SyncClocks { 49 int64_t diff_clk; 50 int64_t last_cpu_icount; 51 int64_t realtime_clock; 52 } SyncClocks; 53 54 #if !defined(CONFIG_USER_ONLY) 55 /* Allow the guest to have a max 3ms advance. 56 * The difference between the 2 clocks could therefore 57 * oscillate around 0. 58 */ 59 #define VM_CLOCK_ADVANCE 3000000 60 #define THRESHOLD_REDUCE 1.5 61 #define MAX_DELAY_PRINT_RATE 2000000000LL 62 #define MAX_NB_PRINTS 100 63 64 static int64_t max_delay; 65 static int64_t max_advance; 66 67 static void align_clocks(SyncClocks *sc, CPUState *cpu) 68 { 69 int64_t cpu_icount; 70 71 if (!icount_align_option) { 72 return; 73 } 74 75 cpu_icount = cpu->icount_extra + cpu_neg(cpu)->icount_decr.u16.low; 76 sc->diff_clk += icount_to_ns(sc->last_cpu_icount - cpu_icount); 77 sc->last_cpu_icount = cpu_icount; 78 79 if (sc->diff_clk > VM_CLOCK_ADVANCE) { 80 #ifndef _WIN32 81 struct timespec sleep_delay, rem_delay; 82 sleep_delay.tv_sec = sc->diff_clk / 1000000000LL; 83 sleep_delay.tv_nsec = sc->diff_clk % 1000000000LL; 84 if (nanosleep(&sleep_delay, &rem_delay) < 0) { 85 sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec; 86 } else { 87 sc->diff_clk = 0; 88 } 89 #else 90 Sleep(sc->diff_clk / SCALE_MS); 91 sc->diff_clk = 0; 92 #endif 93 } 94 } 95 96 static void print_delay(const SyncClocks *sc) 97 { 98 static float threshold_delay; 99 static int64_t last_realtime_clock; 100 static int nb_prints; 101 102 if (icount_align_option && 103 sc->realtime_clock - last_realtime_clock >= MAX_DELAY_PRINT_RATE && 104 nb_prints < MAX_NB_PRINTS) { 105 if ((-sc->diff_clk / (float)1000000000LL > threshold_delay) || 106 (-sc->diff_clk / (float)1000000000LL < 107 (threshold_delay - THRESHOLD_REDUCE))) { 108 threshold_delay = (-sc->diff_clk / 1000000000LL) + 1; 109 qemu_printf("Warning: The guest is now late by %.1f to %.1f seconds\n", 110 threshold_delay - 1, 111 threshold_delay); 112 nb_prints++; 113 last_realtime_clock = sc->realtime_clock; 114 } 115 } 116 } 117 118 static void init_delay_params(SyncClocks *sc, CPUState *cpu) 119 { 120 if (!icount_align_option) { 121 return; 122 } 123 sc->realtime_clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT); 124 sc->diff_clk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - sc->realtime_clock; 125 sc->last_cpu_icount 126 = cpu->icount_extra + cpu_neg(cpu)->icount_decr.u16.low; 127 if (sc->diff_clk < max_delay) { 128 max_delay = sc->diff_clk; 129 } 130 if (sc->diff_clk > max_advance) { 131 max_advance = sc->diff_clk; 132 } 133 134 /* Print every 2s max if the guest is late. We limit the number 135 of printed messages to NB_PRINT_MAX(currently 100) */ 136 print_delay(sc); 137 } 138 #else 139 static void align_clocks(SyncClocks *sc, const CPUState *cpu) 140 { 141 } 142 143 static void init_delay_params(SyncClocks *sc, const CPUState *cpu) 144 { 145 } 146 #endif /* CONFIG USER ONLY */ 147 148 /* Might cause an exception, so have a longjmp destination ready */ 149 static inline TranslationBlock *tb_lookup(CPUState *cpu, target_ulong pc, 150 target_ulong cs_base, 151 uint32_t flags, uint32_t cflags) 152 { 153 TranslationBlock *tb; 154 uint32_t hash; 155 156 /* we should never be trying to look up an INVALID tb */ 157 tcg_debug_assert(!(cflags & CF_INVALID)); 158 159 hash = tb_jmp_cache_hash_func(pc); 160 tb = qatomic_rcu_read(&cpu->tb_jmp_cache[hash]); 161 162 if (likely(tb && 163 tb->pc == pc && 164 tb->cs_base == cs_base && 165 tb->flags == flags && 166 tb->trace_vcpu_dstate == *cpu->trace_dstate && 167 tb_cflags(tb) == cflags)) { 168 return tb; 169 } 170 tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags); 171 if (tb == NULL) { 172 return NULL; 173 } 174 qatomic_set(&cpu->tb_jmp_cache[hash], tb); 175 return tb; 176 } 177 178 static inline void log_cpu_exec(target_ulong pc, CPUState *cpu, 179 const TranslationBlock *tb) 180 { 181 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) 182 && qemu_log_in_addr_range(pc)) { 183 184 qemu_log_mask(CPU_LOG_EXEC, 185 "Trace %d: %p [" TARGET_FMT_lx 186 "/" TARGET_FMT_lx "/%08x/%08x] %s\n", 187 cpu->cpu_index, tb->tc.ptr, tb->cs_base, pc, 188 tb->flags, tb->cflags, lookup_symbol(pc)); 189 190 #if defined(DEBUG_DISAS) 191 if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) { 192 FILE *logfile = qemu_log_lock(); 193 int flags = 0; 194 195 if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) { 196 flags |= CPU_DUMP_FPU; 197 } 198 #if defined(TARGET_I386) 199 flags |= CPU_DUMP_CCOP; 200 #endif 201 log_cpu_state(cpu, flags); 202 qemu_log_unlock(logfile); 203 } 204 #endif /* DEBUG_DISAS */ 205 } 206 } 207 208 /** 209 * helper_lookup_tb_ptr: quick check for next tb 210 * @env: current cpu state 211 * 212 * Look for an existing TB matching the current cpu state. 213 * If found, return the code pointer. If not found, return 214 * the tcg epilogue so that we return into cpu_tb_exec. 215 */ 216 const void *HELPER(lookup_tb_ptr)(CPUArchState *env) 217 { 218 CPUState *cpu = env_cpu(env); 219 TranslationBlock *tb; 220 target_ulong cs_base, pc; 221 uint32_t flags; 222 223 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); 224 225 tb = tb_lookup(cpu, pc, cs_base, flags, curr_cflags(cpu)); 226 if (tb == NULL) { 227 return tcg_code_gen_epilogue; 228 } 229 230 log_cpu_exec(pc, cpu, tb); 231 232 return tb->tc.ptr; 233 } 234 235 /* Execute a TB, and fix up the CPU state afterwards if necessary */ 236 /* 237 * Disable CFI checks. 238 * TCG creates binary blobs at runtime, with the transformed code. 239 * A TB is a blob of binary code, created at runtime and called with an 240 * indirect function call. Since such function did not exist at compile time, 241 * the CFI runtime has no way to verify its signature and would fail. 242 * TCG is not considered a security-sensitive part of QEMU so this does not 243 * affect the impact of CFI in environment with high security requirements 244 */ 245 static inline TranslationBlock * QEMU_DISABLE_CFI 246 cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit) 247 { 248 CPUArchState *env = cpu->env_ptr; 249 uintptr_t ret; 250 TranslationBlock *last_tb; 251 const void *tb_ptr = itb->tc.ptr; 252 253 log_cpu_exec(itb->pc, cpu, itb); 254 255 qemu_thread_jit_execute(); 256 ret = tcg_qemu_tb_exec(env, tb_ptr); 257 cpu->can_do_io = 1; 258 /* 259 * TODO: Delay swapping back to the read-write region of the TB 260 * until we actually need to modify the TB. The read-only copy, 261 * coming from the rx region, shares the same host TLB entry as 262 * the code that executed the exit_tb opcode that arrived here. 263 * If we insist on touching both the RX and the RW pages, we 264 * double the host TLB pressure. 265 */ 266 last_tb = tcg_splitwx_to_rw((void *)(ret & ~TB_EXIT_MASK)); 267 *tb_exit = ret & TB_EXIT_MASK; 268 269 trace_exec_tb_exit(last_tb, *tb_exit); 270 271 if (*tb_exit > TB_EXIT_IDX1) { 272 /* We didn't start executing this TB (eg because the instruction 273 * counter hit zero); we must restore the guest PC to the address 274 * of the start of the TB. 275 */ 276 CPUClass *cc = CPU_GET_CLASS(cpu); 277 qemu_log_mask_and_addr(CPU_LOG_EXEC, last_tb->pc, 278 "Stopped execution of TB chain before %p [" 279 TARGET_FMT_lx "] %s\n", 280 last_tb->tc.ptr, last_tb->pc, 281 lookup_symbol(last_tb->pc)); 282 if (cc->tcg_ops->synchronize_from_tb) { 283 cc->tcg_ops->synchronize_from_tb(cpu, last_tb); 284 } else { 285 assert(cc->set_pc); 286 cc->set_pc(cpu, last_tb->pc); 287 } 288 } 289 return last_tb; 290 } 291 292 293 static void cpu_exec_enter(CPUState *cpu) 294 { 295 CPUClass *cc = CPU_GET_CLASS(cpu); 296 297 if (cc->tcg_ops->cpu_exec_enter) { 298 cc->tcg_ops->cpu_exec_enter(cpu); 299 } 300 } 301 302 static void cpu_exec_exit(CPUState *cpu) 303 { 304 CPUClass *cc = CPU_GET_CLASS(cpu); 305 306 if (cc->tcg_ops->cpu_exec_exit) { 307 cc->tcg_ops->cpu_exec_exit(cpu); 308 } 309 } 310 311 void cpu_exec_step_atomic(CPUState *cpu) 312 { 313 CPUArchState *env = (CPUArchState *)cpu->env_ptr; 314 TranslationBlock *tb; 315 target_ulong cs_base, pc; 316 uint32_t flags; 317 uint32_t cflags = (curr_cflags(cpu) & ~CF_PARALLEL) | 1; 318 int tb_exit; 319 320 if (sigsetjmp(cpu->jmp_env, 0) == 0) { 321 start_exclusive(); 322 g_assert(cpu == current_cpu); 323 g_assert(!cpu->running); 324 cpu->running = true; 325 326 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); 327 tb = tb_lookup(cpu, pc, cs_base, flags, cflags); 328 329 if (tb == NULL) { 330 mmap_lock(); 331 tb = tb_gen_code(cpu, pc, cs_base, flags, cflags); 332 mmap_unlock(); 333 } 334 335 cpu_exec_enter(cpu); 336 /* execute the generated code */ 337 trace_exec_tb(tb, pc); 338 cpu_tb_exec(cpu, tb, &tb_exit); 339 cpu_exec_exit(cpu); 340 } else { 341 /* 342 * The mmap_lock is dropped by tb_gen_code if it runs out of 343 * memory. 344 */ 345 #ifndef CONFIG_SOFTMMU 346 tcg_debug_assert(!have_mmap_lock()); 347 #endif 348 if (qemu_mutex_iothread_locked()) { 349 qemu_mutex_unlock_iothread(); 350 } 351 assert_no_pages_locked(); 352 qemu_plugin_disable_mem_helpers(cpu); 353 } 354 355 356 /* 357 * As we start the exclusive region before codegen we must still 358 * be in the region if we longjump out of either the codegen or 359 * the execution. 360 */ 361 g_assert(cpu_in_exclusive_context(cpu)); 362 cpu->running = false; 363 end_exclusive(); 364 } 365 366 struct tb_desc { 367 target_ulong pc; 368 target_ulong cs_base; 369 CPUArchState *env; 370 tb_page_addr_t phys_page1; 371 uint32_t flags; 372 uint32_t cflags; 373 uint32_t trace_vcpu_dstate; 374 }; 375 376 static bool tb_lookup_cmp(const void *p, const void *d) 377 { 378 const TranslationBlock *tb = p; 379 const struct tb_desc *desc = d; 380 381 if (tb->pc == desc->pc && 382 tb->page_addr[0] == desc->phys_page1 && 383 tb->cs_base == desc->cs_base && 384 tb->flags == desc->flags && 385 tb->trace_vcpu_dstate == desc->trace_vcpu_dstate && 386 tb_cflags(tb) == desc->cflags) { 387 /* check next page if needed */ 388 if (tb->page_addr[1] == -1) { 389 return true; 390 } else { 391 tb_page_addr_t phys_page2; 392 target_ulong virt_page2; 393 394 virt_page2 = (desc->pc & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE; 395 phys_page2 = get_page_addr_code(desc->env, virt_page2); 396 if (tb->page_addr[1] == phys_page2) { 397 return true; 398 } 399 } 400 } 401 return false; 402 } 403 404 TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, 405 target_ulong cs_base, uint32_t flags, 406 uint32_t cflags) 407 { 408 tb_page_addr_t phys_pc; 409 struct tb_desc desc; 410 uint32_t h; 411 412 desc.env = (CPUArchState *)cpu->env_ptr; 413 desc.cs_base = cs_base; 414 desc.flags = flags; 415 desc.cflags = cflags; 416 desc.trace_vcpu_dstate = *cpu->trace_dstate; 417 desc.pc = pc; 418 phys_pc = get_page_addr_code(desc.env, pc); 419 if (phys_pc == -1) { 420 return NULL; 421 } 422 desc.phys_page1 = phys_pc & TARGET_PAGE_MASK; 423 h = tb_hash_func(phys_pc, pc, flags, cflags, *cpu->trace_dstate); 424 return qht_lookup_custom(&tb_ctx.htable, &desc, h, tb_lookup_cmp); 425 } 426 427 void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr) 428 { 429 if (TCG_TARGET_HAS_direct_jump) { 430 uintptr_t offset = tb->jmp_target_arg[n]; 431 uintptr_t tc_ptr = (uintptr_t)tb->tc.ptr; 432 uintptr_t jmp_rx = tc_ptr + offset; 433 uintptr_t jmp_rw = jmp_rx - tcg_splitwx_diff; 434 tb_target_set_jmp_target(tc_ptr, jmp_rx, jmp_rw, addr); 435 } else { 436 tb->jmp_target_arg[n] = addr; 437 } 438 } 439 440 static inline void tb_add_jump(TranslationBlock *tb, int n, 441 TranslationBlock *tb_next) 442 { 443 uintptr_t old; 444 445 qemu_thread_jit_write(); 446 assert(n < ARRAY_SIZE(tb->jmp_list_next)); 447 qemu_spin_lock(&tb_next->jmp_lock); 448 449 /* make sure the destination TB is valid */ 450 if (tb_next->cflags & CF_INVALID) { 451 goto out_unlock_next; 452 } 453 /* Atomically claim the jump destination slot only if it was NULL */ 454 old = qatomic_cmpxchg(&tb->jmp_dest[n], (uintptr_t)NULL, 455 (uintptr_t)tb_next); 456 if (old) { 457 goto out_unlock_next; 458 } 459 460 /* patch the native jump address */ 461 tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc.ptr); 462 463 /* add in TB jmp list */ 464 tb->jmp_list_next[n] = tb_next->jmp_list_head; 465 tb_next->jmp_list_head = (uintptr_t)tb | n; 466 467 qemu_spin_unlock(&tb_next->jmp_lock); 468 469 qemu_log_mask_and_addr(CPU_LOG_EXEC, tb->pc, 470 "Linking TBs %p [" TARGET_FMT_lx 471 "] index %d -> %p [" TARGET_FMT_lx "]\n", 472 tb->tc.ptr, tb->pc, n, 473 tb_next->tc.ptr, tb_next->pc); 474 return; 475 476 out_unlock_next: 477 qemu_spin_unlock(&tb_next->jmp_lock); 478 return; 479 } 480 481 static inline TranslationBlock *tb_find(CPUState *cpu, 482 TranslationBlock *last_tb, 483 int tb_exit, uint32_t cflags) 484 { 485 CPUArchState *env = (CPUArchState *)cpu->env_ptr; 486 TranslationBlock *tb; 487 target_ulong cs_base, pc; 488 uint32_t flags; 489 490 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); 491 492 tb = tb_lookup(cpu, pc, cs_base, flags, cflags); 493 if (tb == NULL) { 494 mmap_lock(); 495 tb = tb_gen_code(cpu, pc, cs_base, flags, cflags); 496 mmap_unlock(); 497 /* We add the TB in the virtual pc hash table for the fast lookup */ 498 qatomic_set(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)], tb); 499 } 500 #ifndef CONFIG_USER_ONLY 501 /* We don't take care of direct jumps when address mapping changes in 502 * system emulation. So it's not safe to make a direct jump to a TB 503 * spanning two pages because the mapping for the second page can change. 504 */ 505 if (tb->page_addr[1] != -1) { 506 last_tb = NULL; 507 } 508 #endif 509 /* See if we can patch the calling TB. */ 510 if (last_tb) { 511 tb_add_jump(last_tb, tb_exit, tb); 512 } 513 return tb; 514 } 515 516 static inline bool cpu_handle_halt(CPUState *cpu) 517 { 518 if (cpu->halted) { 519 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY) 520 if (cpu->interrupt_request & CPU_INTERRUPT_POLL) { 521 X86CPU *x86_cpu = X86_CPU(cpu); 522 qemu_mutex_lock_iothread(); 523 apic_poll_irq(x86_cpu->apic_state); 524 cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL); 525 qemu_mutex_unlock_iothread(); 526 } 527 #endif 528 if (!cpu_has_work(cpu)) { 529 return true; 530 } 531 532 cpu->halted = 0; 533 } 534 535 return false; 536 } 537 538 static inline void cpu_handle_debug_exception(CPUState *cpu) 539 { 540 CPUClass *cc = CPU_GET_CLASS(cpu); 541 CPUWatchpoint *wp; 542 543 if (!cpu->watchpoint_hit) { 544 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) { 545 wp->flags &= ~BP_WATCHPOINT_HIT; 546 } 547 } 548 549 if (cc->tcg_ops->debug_excp_handler) { 550 cc->tcg_ops->debug_excp_handler(cpu); 551 } 552 } 553 554 static inline bool cpu_handle_exception(CPUState *cpu, int *ret) 555 { 556 if (cpu->exception_index < 0) { 557 #ifndef CONFIG_USER_ONLY 558 if (replay_has_exception() 559 && cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra == 0) { 560 /* Execute just one insn to trigger exception pending in the log */ 561 cpu->cflags_next_tb = (curr_cflags(cpu) & ~CF_USE_ICOUNT) | 1; 562 } 563 #endif 564 return false; 565 } 566 if (cpu->exception_index >= EXCP_INTERRUPT) { 567 /* exit request from the cpu execution loop */ 568 *ret = cpu->exception_index; 569 if (*ret == EXCP_DEBUG) { 570 cpu_handle_debug_exception(cpu); 571 } 572 cpu->exception_index = -1; 573 return true; 574 } else { 575 #if defined(CONFIG_USER_ONLY) 576 /* if user mode only, we simulate a fake exception 577 which will be handled outside the cpu execution 578 loop */ 579 #if defined(TARGET_I386) 580 CPUClass *cc = CPU_GET_CLASS(cpu); 581 cc->tcg_ops->do_interrupt(cpu); 582 #endif 583 *ret = cpu->exception_index; 584 cpu->exception_index = -1; 585 return true; 586 #else 587 if (replay_exception()) { 588 CPUClass *cc = CPU_GET_CLASS(cpu); 589 qemu_mutex_lock_iothread(); 590 cc->tcg_ops->do_interrupt(cpu); 591 qemu_mutex_unlock_iothread(); 592 cpu->exception_index = -1; 593 594 if (unlikely(cpu->singlestep_enabled)) { 595 /* 596 * After processing the exception, ensure an EXCP_DEBUG is 597 * raised when single-stepping so that GDB doesn't miss the 598 * next instruction. 599 */ 600 *ret = EXCP_DEBUG; 601 cpu_handle_debug_exception(cpu); 602 return true; 603 } 604 } else if (!replay_has_interrupt()) { 605 /* give a chance to iothread in replay mode */ 606 *ret = EXCP_INTERRUPT; 607 return true; 608 } 609 #endif 610 } 611 612 return false; 613 } 614 615 /* 616 * CPU_INTERRUPT_POLL is a virtual event which gets converted into a 617 * "real" interrupt event later. It does not need to be recorded for 618 * replay purposes. 619 */ 620 static inline bool need_replay_interrupt(int interrupt_request) 621 { 622 #if defined(TARGET_I386) 623 return !(interrupt_request & CPU_INTERRUPT_POLL); 624 #else 625 return true; 626 #endif 627 } 628 629 static inline bool cpu_handle_interrupt(CPUState *cpu, 630 TranslationBlock **last_tb) 631 { 632 CPUClass *cc = CPU_GET_CLASS(cpu); 633 634 /* Clear the interrupt flag now since we're processing 635 * cpu->interrupt_request and cpu->exit_request. 636 * Ensure zeroing happens before reading cpu->exit_request or 637 * cpu->interrupt_request (see also smp_wmb in cpu_exit()) 638 */ 639 qatomic_mb_set(&cpu_neg(cpu)->icount_decr.u16.high, 0); 640 641 if (unlikely(qatomic_read(&cpu->interrupt_request))) { 642 int interrupt_request; 643 qemu_mutex_lock_iothread(); 644 interrupt_request = cpu->interrupt_request; 645 if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) { 646 /* Mask out external interrupts for this step. */ 647 interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK; 648 } 649 if (interrupt_request & CPU_INTERRUPT_DEBUG) { 650 cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG; 651 cpu->exception_index = EXCP_DEBUG; 652 qemu_mutex_unlock_iothread(); 653 return true; 654 } 655 if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) { 656 /* Do nothing */ 657 } else if (interrupt_request & CPU_INTERRUPT_HALT) { 658 replay_interrupt(); 659 cpu->interrupt_request &= ~CPU_INTERRUPT_HALT; 660 cpu->halted = 1; 661 cpu->exception_index = EXCP_HLT; 662 qemu_mutex_unlock_iothread(); 663 return true; 664 } 665 #if defined(TARGET_I386) 666 else if (interrupt_request & CPU_INTERRUPT_INIT) { 667 X86CPU *x86_cpu = X86_CPU(cpu); 668 CPUArchState *env = &x86_cpu->env; 669 replay_interrupt(); 670 cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0, 0); 671 do_cpu_init(x86_cpu); 672 cpu->exception_index = EXCP_HALTED; 673 qemu_mutex_unlock_iothread(); 674 return true; 675 } 676 #else 677 else if (interrupt_request & CPU_INTERRUPT_RESET) { 678 replay_interrupt(); 679 cpu_reset(cpu); 680 qemu_mutex_unlock_iothread(); 681 return true; 682 } 683 #endif 684 /* The target hook has 3 exit conditions: 685 False when the interrupt isn't processed, 686 True when it is, and we should restart on a new TB, 687 and via longjmp via cpu_loop_exit. */ 688 else { 689 if (cc->tcg_ops->cpu_exec_interrupt && 690 cc->tcg_ops->cpu_exec_interrupt(cpu, interrupt_request)) { 691 if (need_replay_interrupt(interrupt_request)) { 692 replay_interrupt(); 693 } 694 /* 695 * After processing the interrupt, ensure an EXCP_DEBUG is 696 * raised when single-stepping so that GDB doesn't miss the 697 * next instruction. 698 */ 699 cpu->exception_index = 700 (cpu->singlestep_enabled ? EXCP_DEBUG : -1); 701 *last_tb = NULL; 702 } 703 /* The target hook may have updated the 'cpu->interrupt_request'; 704 * reload the 'interrupt_request' value */ 705 interrupt_request = cpu->interrupt_request; 706 } 707 if (interrupt_request & CPU_INTERRUPT_EXITTB) { 708 cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB; 709 /* ensure that no TB jump will be modified as 710 the program flow was changed */ 711 *last_tb = NULL; 712 } 713 714 /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */ 715 qemu_mutex_unlock_iothread(); 716 } 717 718 /* Finally, check if we need to exit to the main loop. */ 719 if (unlikely(qatomic_read(&cpu->exit_request)) 720 || (icount_enabled() 721 && (cpu->cflags_next_tb == -1 || cpu->cflags_next_tb & CF_USE_ICOUNT) 722 && cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra == 0)) { 723 qatomic_set(&cpu->exit_request, 0); 724 if (cpu->exception_index == -1) { 725 cpu->exception_index = EXCP_INTERRUPT; 726 } 727 return true; 728 } 729 730 return false; 731 } 732 733 static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb, 734 TranslationBlock **last_tb, int *tb_exit) 735 { 736 int32_t insns_left; 737 738 trace_exec_tb(tb, tb->pc); 739 tb = cpu_tb_exec(cpu, tb, tb_exit); 740 if (*tb_exit != TB_EXIT_REQUESTED) { 741 *last_tb = tb; 742 return; 743 } 744 745 *last_tb = NULL; 746 insns_left = qatomic_read(&cpu_neg(cpu)->icount_decr.u32); 747 if (insns_left < 0) { 748 /* Something asked us to stop executing chained TBs; just 749 * continue round the main loop. Whatever requested the exit 750 * will also have set something else (eg exit_request or 751 * interrupt_request) which will be handled by 752 * cpu_handle_interrupt. cpu_handle_interrupt will also 753 * clear cpu->icount_decr.u16.high. 754 */ 755 return; 756 } 757 758 /* Instruction counter expired. */ 759 assert(icount_enabled()); 760 #ifndef CONFIG_USER_ONLY 761 /* Ensure global icount has gone forward */ 762 icount_update(cpu); 763 /* Refill decrementer and continue execution. */ 764 insns_left = MIN(CF_COUNT_MASK, cpu->icount_budget); 765 cpu_neg(cpu)->icount_decr.u16.low = insns_left; 766 cpu->icount_extra = cpu->icount_budget - insns_left; 767 768 /* 769 * If the next tb has more instructions than we have left to 770 * execute we need to ensure we find/generate a TB with exactly 771 * insns_left instructions in it. 772 */ 773 if (!cpu->icount_extra && insns_left > 0 && insns_left < tb->icount) { 774 cpu->cflags_next_tb = (tb->cflags & ~CF_COUNT_MASK) | insns_left; 775 } 776 #endif 777 } 778 779 /* main execution loop */ 780 781 int cpu_exec(CPUState *cpu) 782 { 783 CPUClass *cc = CPU_GET_CLASS(cpu); 784 int ret; 785 SyncClocks sc = { 0 }; 786 787 /* replay_interrupt may need current_cpu */ 788 current_cpu = cpu; 789 790 if (cpu_handle_halt(cpu)) { 791 return EXCP_HALTED; 792 } 793 794 rcu_read_lock(); 795 796 cpu_exec_enter(cpu); 797 798 /* Calculate difference between guest clock and host clock. 799 * This delay includes the delay of the last cycle, so 800 * what we have to do is sleep until it is 0. As for the 801 * advance/delay we gain here, we try to fix it next time. 802 */ 803 init_delay_params(&sc, cpu); 804 805 /* prepare setjmp context for exception handling */ 806 if (sigsetjmp(cpu->jmp_env, 0) != 0) { 807 #if defined(__clang__) 808 /* 809 * Some compilers wrongly smash all local variables after 810 * siglongjmp (the spec requires that only non-volatile locals 811 * which are changed between the sigsetjmp and siglongjmp are 812 * permitted to be trashed). There were bug reports for gcc 813 * 4.5.0 and clang. The bug is fixed in all versions of gcc 814 * that we support, but is still unfixed in clang: 815 * https://bugs.llvm.org/show_bug.cgi?id=21183 816 * 817 * Reload essential local variables here for those compilers. 818 * Newer versions of gcc would complain about this code (-Wclobbered), 819 * so we only perform the workaround for clang. 820 */ 821 cpu = current_cpu; 822 cc = CPU_GET_CLASS(cpu); 823 #else 824 /* 825 * Non-buggy compilers preserve these locals; assert that 826 * they have the correct value. 827 */ 828 g_assert(cpu == current_cpu); 829 g_assert(cc == CPU_GET_CLASS(cpu)); 830 #endif 831 832 #ifndef CONFIG_SOFTMMU 833 tcg_debug_assert(!have_mmap_lock()); 834 #endif 835 if (qemu_mutex_iothread_locked()) { 836 qemu_mutex_unlock_iothread(); 837 } 838 qemu_plugin_disable_mem_helpers(cpu); 839 840 assert_no_pages_locked(); 841 } 842 843 /* if an exception is pending, we execute it here */ 844 while (!cpu_handle_exception(cpu, &ret)) { 845 TranslationBlock *last_tb = NULL; 846 int tb_exit = 0; 847 848 while (!cpu_handle_interrupt(cpu, &last_tb)) { 849 uint32_t cflags = cpu->cflags_next_tb; 850 TranslationBlock *tb; 851 852 /* When requested, use an exact setting for cflags for the next 853 execution. This is used for icount, precise smc, and stop- 854 after-access watchpoints. Since this request should never 855 have CF_INVALID set, -1 is a convenient invalid value that 856 does not require tcg headers for cpu_common_reset. */ 857 if (cflags == -1) { 858 cflags = curr_cflags(cpu); 859 } else { 860 cpu->cflags_next_tb = -1; 861 } 862 863 tb = tb_find(cpu, last_tb, tb_exit, cflags); 864 cpu_loop_exec_tb(cpu, tb, &last_tb, &tb_exit); 865 /* Try to align the host and virtual clocks 866 if the guest is in advance */ 867 align_clocks(&sc, cpu); 868 } 869 } 870 871 cpu_exec_exit(cpu); 872 rcu_read_unlock(); 873 874 return ret; 875 } 876 877 void tcg_exec_realizefn(CPUState *cpu, Error **errp) 878 { 879 static bool tcg_target_initialized; 880 CPUClass *cc = CPU_GET_CLASS(cpu); 881 882 if (!tcg_target_initialized) { 883 cc->tcg_ops->initialize(); 884 tcg_target_initialized = true; 885 } 886 tlb_init(cpu); 887 qemu_plugin_vcpu_init_hook(cpu); 888 889 #ifndef CONFIG_USER_ONLY 890 tcg_iommu_init_notifier_list(cpu); 891 #endif /* !CONFIG_USER_ONLY */ 892 } 893 894 /* undo the initializations in reverse order */ 895 void tcg_exec_unrealizefn(CPUState *cpu) 896 { 897 #ifndef CONFIG_USER_ONLY 898 tcg_iommu_free_notifier_list(cpu); 899 #endif /* !CONFIG_USER_ONLY */ 900 901 qemu_plugin_vcpu_exit_hook(cpu); 902 tlb_destroy(cpu); 903 } 904 905 #ifndef CONFIG_USER_ONLY 906 907 void dump_drift_info(void) 908 { 909 if (!icount_enabled()) { 910 return; 911 } 912 913 qemu_printf("Host - Guest clock %"PRIi64" ms\n", 914 (cpu_get_clock() - icount_get()) / SCALE_MS); 915 if (icount_align_option) { 916 qemu_printf("Max guest delay %"PRIi64" ms\n", 917 -max_delay / SCALE_MS); 918 qemu_printf("Max guest advance %"PRIi64" ms\n", 919 max_advance / SCALE_MS); 920 } else { 921 qemu_printf("Max guest delay NA\n"); 922 qemu_printf("Max guest advance NA\n"); 923 } 924 } 925 926 #endif /* !CONFIG_USER_ONLY */ 927