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