2 * emulator main execution loop
4 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qemu/qemu-print.h"
23 #include "hw/core/tcg-cpu-ops.h"
25 #include "disas/disas.h"
26 #include "exec/exec-all.h"
28 #include "qemu/atomic.h"
29 #include "qemu/compiler.h"
30 #include "qemu/timer.h"
33 #include "qemu/main-loop.h"
34 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
35 #include "hw/i386/apic.h"
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"
43 #include "tb-context.h"
46 /* -icount align implementation. */
48 typedef struct SyncClocks
{
50 int64_t last_cpu_icount
;
51 int64_t realtime_clock
;
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
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
64 static int64_t max_delay
;
65 static int64_t max_advance
;
67 static void align_clocks(SyncClocks
*sc
, CPUState
*cpu
)
71 if (!icount_align_option
) {
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
;
79 if (sc
->diff_clk
> VM_CLOCK_ADVANCE
) {
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
;
90 Sleep(sc
->diff_clk
/ SCALE_MS
);
96 static void print_delay(const SyncClocks
*sc
)
98 static float threshold_delay
;
99 static int64_t last_realtime_clock
;
100 static int nb_prints
;
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",
113 last_realtime_clock
= sc
->realtime_clock
;
118 static void init_delay_params(SyncClocks
*sc
, CPUState
*cpu
)
120 if (!icount_align_option
) {
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
;
126 = cpu
->icount_extra
+ cpu_neg(cpu
)->icount_decr
.u16
.low
;
127 if (sc
->diff_clk
< max_delay
) {
128 max_delay
= sc
->diff_clk
;
130 if (sc
->diff_clk
> max_advance
) {
131 max_advance
= sc
->diff_clk
;
134 /* Print every 2s max if the guest is late. We limit the number
135 of printed messages to NB_PRINT_MAX(currently 100) */
139 static void align_clocks(SyncClocks
*sc
, const CPUState
*cpu
)
143 static void init_delay_params(SyncClocks
*sc
, const CPUState
*cpu
)
146 #endif /* CONFIG USER ONLY */
148 uint32_t curr_cflags(CPUState
*cpu
)
150 uint32_t cflags
= cpu
->tcg_cflags
;
153 * Record gdb single-step. We should be exiting the TB by raising
154 * EXCP_DEBUG, but to simplify other tests, disable chaining too.
156 * For singlestep and -d nochain, suppress goto_tb so that
157 * we can log -d cpu,exec after every TB.
159 if (unlikely(cpu
->singlestep_enabled
)) {
160 cflags
|= CF_NO_GOTO_TB
| CF_NO_GOTO_PTR
| CF_SINGLE_STEP
| 1;
161 } else if (singlestep
) {
162 cflags
|= CF_NO_GOTO_TB
| 1;
163 } else if (qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN
)) {
164 cflags
|= CF_NO_GOTO_TB
;
170 /* Might cause an exception, so have a longjmp destination ready */
171 static inline TranslationBlock
*tb_lookup(CPUState
*cpu
, target_ulong pc
,
172 target_ulong cs_base
,
173 uint32_t flags
, uint32_t cflags
)
175 TranslationBlock
*tb
;
178 /* we should never be trying to look up an INVALID tb */
179 tcg_debug_assert(!(cflags
& CF_INVALID
));
181 hash
= tb_jmp_cache_hash_func(pc
);
182 tb
= qatomic_rcu_read(&cpu
->tb_jmp_cache
[hash
]);
186 tb
->cs_base
== cs_base
&&
187 tb
->flags
== flags
&&
188 tb
->trace_vcpu_dstate
== *cpu
->trace_dstate
&&
189 tb_cflags(tb
) == cflags
)) {
192 tb
= tb_htable_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
196 qatomic_set(&cpu
->tb_jmp_cache
[hash
], tb
);
200 static inline void log_cpu_exec(target_ulong pc
, CPUState
*cpu
,
201 const TranslationBlock
*tb
)
203 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_CPU
| CPU_LOG_EXEC
))
204 && qemu_log_in_addr_range(pc
)) {
206 qemu_log_mask(CPU_LOG_EXEC
,
207 "Trace %d: %p [" TARGET_FMT_lx
208 "/" TARGET_FMT_lx
"/%08x/%08x] %s\n",
209 cpu
->cpu_index
, tb
->tc
.ptr
, tb
->cs_base
, pc
,
210 tb
->flags
, tb
->cflags
, lookup_symbol(pc
));
212 #if defined(DEBUG_DISAS)
213 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
)) {
214 FILE *logfile
= qemu_log_lock();
217 if (qemu_loglevel_mask(CPU_LOG_TB_FPU
)) {
218 flags
|= CPU_DUMP_FPU
;
220 #if defined(TARGET_I386)
221 flags
|= CPU_DUMP_CCOP
;
223 log_cpu_state(cpu
, flags
);
224 qemu_log_unlock(logfile
);
226 #endif /* DEBUG_DISAS */
230 static bool check_for_breakpoints(CPUState
*cpu
, target_ulong pc
,
234 bool match_page
= false;
236 if (likely(QTAILQ_EMPTY(&cpu
->breakpoints
))) {
241 * Singlestep overrides breakpoints.
242 * This requirement is visible in the record-replay tests, where
243 * we would fail to make forward progress in reverse-continue.
245 * TODO: gdb singlestep should only override gdb breakpoints,
246 * so that one could (gdb) singlestep into the guest kernel's
247 * architectural breakpoint handler.
249 if (cpu
->singlestep_enabled
) {
253 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
255 * If we have an exact pc match, trigger the breakpoint.
256 * Otherwise, note matches within the page.
259 bool match_bp
= false;
261 if (bp
->flags
& BP_GDB
) {
263 } else if (bp
->flags
& BP_CPU
) {
264 #ifdef CONFIG_USER_ONLY
265 g_assert_not_reached();
267 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
268 assert(cc
->tcg_ops
->debug_check_breakpoint
);
269 match_bp
= cc
->tcg_ops
->debug_check_breakpoint(cpu
);
274 cpu
->exception_index
= EXCP_DEBUG
;
277 } else if (((pc
^ bp
->pc
) & TARGET_PAGE_MASK
) == 0) {
283 * Within the same page as a breakpoint, single-step,
284 * returning to helper_lookup_tb_ptr after each insn looking
285 * for the actual breakpoint.
287 * TODO: Perhaps better to record all of the TBs associated
288 * with a given virtual page that contains a breakpoint, and
289 * then invalidate them when a new overlapping breakpoint is
290 * set on the page. Non-overlapping TBs would not be
291 * invalidated, nor would any TB need to be invalidated as
292 * breakpoints are removed.
295 *cflags
= (*cflags
& ~CF_COUNT_MASK
) | CF_NO_GOTO_TB
| 1;
301 * helper_lookup_tb_ptr: quick check for next tb
302 * @env: current cpu state
304 * Look for an existing TB matching the current cpu state.
305 * If found, return the code pointer. If not found, return
306 * the tcg epilogue so that we return into cpu_tb_exec.
308 const void *HELPER(lookup_tb_ptr
)(CPUArchState
*env
)
310 CPUState
*cpu
= env_cpu(env
);
311 TranslationBlock
*tb
;
312 target_ulong cs_base
, pc
;
313 uint32_t flags
, cflags
;
315 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
317 cflags
= curr_cflags(cpu
);
318 if (check_for_breakpoints(cpu
, pc
, &cflags
)) {
322 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
324 return tcg_code_gen_epilogue
;
327 log_cpu_exec(pc
, cpu
, tb
);
332 /* Execute a TB, and fix up the CPU state afterwards if necessary */
334 * Disable CFI checks.
335 * TCG creates binary blobs at runtime, with the transformed code.
336 * A TB is a blob of binary code, created at runtime and called with an
337 * indirect function call. Since such function did not exist at compile time,
338 * the CFI runtime has no way to verify its signature and would fail.
339 * TCG is not considered a security-sensitive part of QEMU so this does not
340 * affect the impact of CFI in environment with high security requirements
342 static inline TranslationBlock
* QEMU_DISABLE_CFI
343 cpu_tb_exec(CPUState
*cpu
, TranslationBlock
*itb
, int *tb_exit
)
345 CPUArchState
*env
= cpu
->env_ptr
;
347 TranslationBlock
*last_tb
;
348 const void *tb_ptr
= itb
->tc
.ptr
;
350 log_cpu_exec(itb
->pc
, cpu
, itb
);
352 qemu_thread_jit_execute();
353 ret
= tcg_qemu_tb_exec(env
, tb_ptr
);
356 * TODO: Delay swapping back to the read-write region of the TB
357 * until we actually need to modify the TB. The read-only copy,
358 * coming from the rx region, shares the same host TLB entry as
359 * the code that executed the exit_tb opcode that arrived here.
360 * If we insist on touching both the RX and the RW pages, we
361 * double the host TLB pressure.
363 last_tb
= tcg_splitwx_to_rw((void *)(ret
& ~TB_EXIT_MASK
));
364 *tb_exit
= ret
& TB_EXIT_MASK
;
366 trace_exec_tb_exit(last_tb
, *tb_exit
);
368 if (*tb_exit
> TB_EXIT_IDX1
) {
369 /* We didn't start executing this TB (eg because the instruction
370 * counter hit zero); we must restore the guest PC to the address
371 * of the start of the TB.
373 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
374 qemu_log_mask_and_addr(CPU_LOG_EXEC
, last_tb
->pc
,
375 "Stopped execution of TB chain before %p ["
376 TARGET_FMT_lx
"] %s\n",
377 last_tb
->tc
.ptr
, last_tb
->pc
,
378 lookup_symbol(last_tb
->pc
));
379 if (cc
->tcg_ops
->synchronize_from_tb
) {
380 cc
->tcg_ops
->synchronize_from_tb(cpu
, last_tb
);
383 cc
->set_pc(cpu
, last_tb
->pc
);
390 static void cpu_exec_enter(CPUState
*cpu
)
392 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
394 if (cc
->tcg_ops
->cpu_exec_enter
) {
395 cc
->tcg_ops
->cpu_exec_enter(cpu
);
399 static void cpu_exec_exit(CPUState
*cpu
)
401 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
403 if (cc
->tcg_ops
->cpu_exec_exit
) {
404 cc
->tcg_ops
->cpu_exec_exit(cpu
);
408 void cpu_exec_step_atomic(CPUState
*cpu
)
410 CPUArchState
*env
= (CPUArchState
*)cpu
->env_ptr
;
411 TranslationBlock
*tb
;
412 target_ulong cs_base
, pc
;
413 uint32_t flags
, cflags
;
416 if (sigsetjmp(cpu
->jmp_env
, 0) == 0) {
418 g_assert(cpu
== current_cpu
);
419 g_assert(!cpu
->running
);
422 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
424 cflags
= curr_cflags(cpu
);
425 /* Execute in a serial context. */
426 cflags
&= ~CF_PARALLEL
;
427 /* After 1 insn, return and release the exclusive lock. */
428 cflags
|= CF_NO_GOTO_TB
| CF_NO_GOTO_PTR
| 1;
430 * No need to check_for_breakpoints here.
431 * We only arrive in cpu_exec_step_atomic after beginning execution
432 * of an insn that includes an atomic operation we can't handle.
433 * Any breakpoint for this insn will have been recognized earlier.
436 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
439 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, cflags
);
444 /* execute the generated code */
445 trace_exec_tb(tb
, pc
);
446 cpu_tb_exec(cpu
, tb
, &tb_exit
);
450 * The mmap_lock is dropped by tb_gen_code if it runs out of
453 #ifndef CONFIG_SOFTMMU
454 tcg_debug_assert(!have_mmap_lock());
456 if (qemu_mutex_iothread_locked()) {
457 qemu_mutex_unlock_iothread();
459 assert_no_pages_locked();
460 qemu_plugin_disable_mem_helpers(cpu
);
465 * As we start the exclusive region before codegen we must still
466 * be in the region if we longjump out of either the codegen or
469 g_assert(cpu_in_exclusive_context(cpu
));
470 cpu
->running
= false;
476 target_ulong cs_base
;
478 tb_page_addr_t phys_page1
;
481 uint32_t trace_vcpu_dstate
;
484 static bool tb_lookup_cmp(const void *p
, const void *d
)
486 const TranslationBlock
*tb
= p
;
487 const struct tb_desc
*desc
= d
;
489 if (tb
->pc
== desc
->pc
&&
490 tb
->page_addr
[0] == desc
->phys_page1
&&
491 tb
->cs_base
== desc
->cs_base
&&
492 tb
->flags
== desc
->flags
&&
493 tb
->trace_vcpu_dstate
== desc
->trace_vcpu_dstate
&&
494 tb_cflags(tb
) == desc
->cflags
) {
495 /* check next page if needed */
496 if (tb
->page_addr
[1] == -1) {
499 tb_page_addr_t phys_page2
;
500 target_ulong virt_page2
;
502 virt_page2
= (desc
->pc
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
503 phys_page2
= get_page_addr_code(desc
->env
, virt_page2
);
504 if (tb
->page_addr
[1] == phys_page2
) {
512 TranslationBlock
*tb_htable_lookup(CPUState
*cpu
, target_ulong pc
,
513 target_ulong cs_base
, uint32_t flags
,
516 tb_page_addr_t phys_pc
;
520 desc
.env
= (CPUArchState
*)cpu
->env_ptr
;
521 desc
.cs_base
= cs_base
;
523 desc
.cflags
= cflags
;
524 desc
.trace_vcpu_dstate
= *cpu
->trace_dstate
;
526 phys_pc
= get_page_addr_code(desc
.env
, pc
);
530 desc
.phys_page1
= phys_pc
& TARGET_PAGE_MASK
;
531 h
= tb_hash_func(phys_pc
, pc
, flags
, cflags
, *cpu
->trace_dstate
);
532 return qht_lookup_custom(&tb_ctx
.htable
, &desc
, h
, tb_lookup_cmp
);
535 void tb_set_jmp_target(TranslationBlock
*tb
, int n
, uintptr_t addr
)
537 if (TCG_TARGET_HAS_direct_jump
) {
538 uintptr_t offset
= tb
->jmp_target_arg
[n
];
539 uintptr_t tc_ptr
= (uintptr_t)tb
->tc
.ptr
;
540 uintptr_t jmp_rx
= tc_ptr
+ offset
;
541 uintptr_t jmp_rw
= jmp_rx
- tcg_splitwx_diff
;
542 tb_target_set_jmp_target(tc_ptr
, jmp_rx
, jmp_rw
, addr
);
544 tb
->jmp_target_arg
[n
] = addr
;
548 static inline void tb_add_jump(TranslationBlock
*tb
, int n
,
549 TranslationBlock
*tb_next
)
553 qemu_thread_jit_write();
554 assert(n
< ARRAY_SIZE(tb
->jmp_list_next
));
555 qemu_spin_lock(&tb_next
->jmp_lock
);
557 /* make sure the destination TB is valid */
558 if (tb_next
->cflags
& CF_INVALID
) {
559 goto out_unlock_next
;
561 /* Atomically claim the jump destination slot only if it was NULL */
562 old
= qatomic_cmpxchg(&tb
->jmp_dest
[n
], (uintptr_t)NULL
,
565 goto out_unlock_next
;
568 /* patch the native jump address */
569 tb_set_jmp_target(tb
, n
, (uintptr_t)tb_next
->tc
.ptr
);
571 /* add in TB jmp list */
572 tb
->jmp_list_next
[n
] = tb_next
->jmp_list_head
;
573 tb_next
->jmp_list_head
= (uintptr_t)tb
| n
;
575 qemu_spin_unlock(&tb_next
->jmp_lock
);
577 qemu_log_mask_and_addr(CPU_LOG_EXEC
, tb
->pc
,
578 "Linking TBs %p [" TARGET_FMT_lx
579 "] index %d -> %p [" TARGET_FMT_lx
"]\n",
580 tb
->tc
.ptr
, tb
->pc
, n
,
581 tb_next
->tc
.ptr
, tb_next
->pc
);
585 qemu_spin_unlock(&tb_next
->jmp_lock
);
589 static inline bool cpu_handle_halt(CPUState
*cpu
)
591 #ifndef CONFIG_USER_ONLY
593 #if defined(TARGET_I386)
594 if (cpu
->interrupt_request
& CPU_INTERRUPT_POLL
) {
595 X86CPU
*x86_cpu
= X86_CPU(cpu
);
596 qemu_mutex_lock_iothread();
597 apic_poll_irq(x86_cpu
->apic_state
);
598 cpu_reset_interrupt(cpu
, CPU_INTERRUPT_POLL
);
599 qemu_mutex_unlock_iothread();
601 #endif /* TARGET_I386 */
602 if (!cpu_has_work(cpu
)) {
608 #endif /* !CONFIG_USER_ONLY */
613 static inline void cpu_handle_debug_exception(CPUState
*cpu
)
615 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
618 if (!cpu
->watchpoint_hit
) {
619 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
620 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
624 if (cc
->tcg_ops
->debug_excp_handler
) {
625 cc
->tcg_ops
->debug_excp_handler(cpu
);
629 static inline bool cpu_handle_exception(CPUState
*cpu
, int *ret
)
631 if (cpu
->exception_index
< 0) {
632 #ifndef CONFIG_USER_ONLY
633 if (replay_has_exception()
634 && cpu_neg(cpu
)->icount_decr
.u16
.low
+ cpu
->icount_extra
== 0) {
635 /* Execute just one insn to trigger exception pending in the log */
636 cpu
->cflags_next_tb
= (curr_cflags(cpu
) & ~CF_USE_ICOUNT
) | 1;
641 if (cpu
->exception_index
>= EXCP_INTERRUPT
) {
642 /* exit request from the cpu execution loop */
643 *ret
= cpu
->exception_index
;
644 if (*ret
== EXCP_DEBUG
) {
645 cpu_handle_debug_exception(cpu
);
647 cpu
->exception_index
= -1;
650 #if defined(CONFIG_USER_ONLY)
651 /* if user mode only, we simulate a fake exception
652 which will be handled outside the cpu execution
654 #if defined(TARGET_I386)
655 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
656 cc
->tcg_ops
->fake_user_interrupt(cpu
);
657 #endif /* TARGET_I386 */
658 *ret
= cpu
->exception_index
;
659 cpu
->exception_index
= -1;
662 if (replay_exception()) {
663 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
664 qemu_mutex_lock_iothread();
665 cc
->tcg_ops
->do_interrupt(cpu
);
666 qemu_mutex_unlock_iothread();
667 cpu
->exception_index
= -1;
669 if (unlikely(cpu
->singlestep_enabled
)) {
671 * After processing the exception, ensure an EXCP_DEBUG is
672 * raised when single-stepping so that GDB doesn't miss the
676 cpu_handle_debug_exception(cpu
);
679 } else if (!replay_has_interrupt()) {
680 /* give a chance to iothread in replay mode */
681 *ret
= EXCP_INTERRUPT
;
690 #ifndef CONFIG_USER_ONLY
692 * CPU_INTERRUPT_POLL is a virtual event which gets converted into a
693 * "real" interrupt event later. It does not need to be recorded for
696 static inline bool need_replay_interrupt(int interrupt_request
)
698 #if defined(TARGET_I386)
699 return !(interrupt_request
& CPU_INTERRUPT_POLL
);
704 #endif /* !CONFIG_USER_ONLY */
706 static inline bool cpu_handle_interrupt(CPUState
*cpu
,
707 TranslationBlock
**last_tb
)
709 /* Clear the interrupt flag now since we're processing
710 * cpu->interrupt_request and cpu->exit_request.
711 * Ensure zeroing happens before reading cpu->exit_request or
712 * cpu->interrupt_request (see also smp_wmb in cpu_exit())
714 qatomic_mb_set(&cpu_neg(cpu
)->icount_decr
.u16
.high
, 0);
716 if (unlikely(qatomic_read(&cpu
->interrupt_request
))) {
717 int interrupt_request
;
718 qemu_mutex_lock_iothread();
719 interrupt_request
= cpu
->interrupt_request
;
720 if (unlikely(cpu
->singlestep_enabled
& SSTEP_NOIRQ
)) {
721 /* Mask out external interrupts for this step. */
722 interrupt_request
&= ~CPU_INTERRUPT_SSTEP_MASK
;
724 if (interrupt_request
& CPU_INTERRUPT_DEBUG
) {
725 cpu
->interrupt_request
&= ~CPU_INTERRUPT_DEBUG
;
726 cpu
->exception_index
= EXCP_DEBUG
;
727 qemu_mutex_unlock_iothread();
730 #if !defined(CONFIG_USER_ONLY)
731 if (replay_mode
== REPLAY_MODE_PLAY
&& !replay_has_interrupt()) {
733 } else if (interrupt_request
& CPU_INTERRUPT_HALT
) {
735 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HALT
;
737 cpu
->exception_index
= EXCP_HLT
;
738 qemu_mutex_unlock_iothread();
741 #if defined(TARGET_I386)
742 else if (interrupt_request
& CPU_INTERRUPT_INIT
) {
743 X86CPU
*x86_cpu
= X86_CPU(cpu
);
744 CPUArchState
*env
= &x86_cpu
->env
;
746 cpu_svm_check_intercept_param(env
, SVM_EXIT_INIT
, 0, 0);
747 do_cpu_init(x86_cpu
);
748 cpu
->exception_index
= EXCP_HALTED
;
749 qemu_mutex_unlock_iothread();
753 else if (interrupt_request
& CPU_INTERRUPT_RESET
) {
756 qemu_mutex_unlock_iothread();
759 #endif /* !TARGET_I386 */
760 /* The target hook has 3 exit conditions:
761 False when the interrupt isn't processed,
762 True when it is, and we should restart on a new TB,
763 and via longjmp via cpu_loop_exit. */
765 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
767 if (cc
->tcg_ops
->cpu_exec_interrupt
&&
768 cc
->tcg_ops
->cpu_exec_interrupt(cpu
, interrupt_request
)) {
769 if (need_replay_interrupt(interrupt_request
)) {
773 * After processing the interrupt, ensure an EXCP_DEBUG is
774 * raised when single-stepping so that GDB doesn't miss the
777 cpu
->exception_index
=
778 (cpu
->singlestep_enabled
? EXCP_DEBUG
: -1);
781 /* The target hook may have updated the 'cpu->interrupt_request';
782 * reload the 'interrupt_request' value */
783 interrupt_request
= cpu
->interrupt_request
;
785 #endif /* !CONFIG_USER_ONLY */
786 if (interrupt_request
& CPU_INTERRUPT_EXITTB
) {
787 cpu
->interrupt_request
&= ~CPU_INTERRUPT_EXITTB
;
788 /* ensure that no TB jump will be modified as
789 the program flow was changed */
793 /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
794 qemu_mutex_unlock_iothread();
797 /* Finally, check if we need to exit to the main loop. */
798 if (unlikely(qatomic_read(&cpu
->exit_request
))
800 && (cpu
->cflags_next_tb
== -1 || cpu
->cflags_next_tb
& CF_USE_ICOUNT
)
801 && cpu_neg(cpu
)->icount_decr
.u16
.low
+ cpu
->icount_extra
== 0)) {
802 qatomic_set(&cpu
->exit_request
, 0);
803 if (cpu
->exception_index
== -1) {
804 cpu
->exception_index
= EXCP_INTERRUPT
;
812 static inline void cpu_loop_exec_tb(CPUState
*cpu
, TranslationBlock
*tb
,
813 TranslationBlock
**last_tb
, int *tb_exit
)
817 trace_exec_tb(tb
, tb
->pc
);
818 tb
= cpu_tb_exec(cpu
, tb
, tb_exit
);
819 if (*tb_exit
!= TB_EXIT_REQUESTED
) {
825 insns_left
= qatomic_read(&cpu_neg(cpu
)->icount_decr
.u32
);
826 if (insns_left
< 0) {
827 /* Something asked us to stop executing chained TBs; just
828 * continue round the main loop. Whatever requested the exit
829 * will also have set something else (eg exit_request or
830 * interrupt_request) which will be handled by
831 * cpu_handle_interrupt. cpu_handle_interrupt will also
832 * clear cpu->icount_decr.u16.high.
837 /* Instruction counter expired. */
838 assert(icount_enabled());
839 #ifndef CONFIG_USER_ONLY
840 /* Ensure global icount has gone forward */
842 /* Refill decrementer and continue execution. */
843 insns_left
= MIN(0xffff, cpu
->icount_budget
);
844 cpu_neg(cpu
)->icount_decr
.u16
.low
= insns_left
;
845 cpu
->icount_extra
= cpu
->icount_budget
- insns_left
;
848 * If the next tb has more instructions than we have left to
849 * execute we need to ensure we find/generate a TB with exactly
850 * insns_left instructions in it.
852 if (insns_left
> 0 && insns_left
< tb
->icount
) {
853 assert(insns_left
<= CF_COUNT_MASK
);
854 assert(cpu
->icount_extra
== 0);
855 cpu
->cflags_next_tb
= (tb
->cflags
& ~CF_COUNT_MASK
) | insns_left
;
860 /* main execution loop */
862 int cpu_exec(CPUState
*cpu
)
865 SyncClocks sc
= { 0 };
867 /* replay_interrupt may need current_cpu */
870 if (cpu_handle_halt(cpu
)) {
878 /* Calculate difference between guest clock and host clock.
879 * This delay includes the delay of the last cycle, so
880 * what we have to do is sleep until it is 0. As for the
881 * advance/delay we gain here, we try to fix it next time.
883 init_delay_params(&sc
, cpu
);
885 /* prepare setjmp context for exception handling */
886 if (sigsetjmp(cpu
->jmp_env
, 0) != 0) {
887 #if defined(__clang__)
889 * Some compilers wrongly smash all local variables after
890 * siglongjmp (the spec requires that only non-volatile locals
891 * which are changed between the sigsetjmp and siglongjmp are
892 * permitted to be trashed). There were bug reports for gcc
893 * 4.5.0 and clang. The bug is fixed in all versions of gcc
894 * that we support, but is still unfixed in clang:
895 * https://bugs.llvm.org/show_bug.cgi?id=21183
897 * Reload an essential local variable here for those compilers.
898 * Newer versions of gcc would complain about this code (-Wclobbered),
899 * so we only perform the workaround for clang.
903 /* Non-buggy compilers preserve this; assert the correct value. */
904 g_assert(cpu
== current_cpu
);
907 #ifndef CONFIG_SOFTMMU
908 tcg_debug_assert(!have_mmap_lock());
910 if (qemu_mutex_iothread_locked()) {
911 qemu_mutex_unlock_iothread();
913 qemu_plugin_disable_mem_helpers(cpu
);
915 assert_no_pages_locked();
918 /* if an exception is pending, we execute it here */
919 while (!cpu_handle_exception(cpu
, &ret
)) {
920 TranslationBlock
*last_tb
= NULL
;
923 while (!cpu_handle_interrupt(cpu
, &last_tb
)) {
924 TranslationBlock
*tb
;
925 target_ulong cs_base
, pc
;
926 uint32_t flags
, cflags
;
928 cpu_get_tb_cpu_state(cpu
->env_ptr
, &pc
, &cs_base
, &flags
);
931 * When requested, use an exact setting for cflags for the next
932 * execution. This is used for icount, precise smc, and stop-
933 * after-access watchpoints. Since this request should never
934 * have CF_INVALID set, -1 is a convenient invalid value that
935 * does not require tcg headers for cpu_common_reset.
937 cflags
= cpu
->cflags_next_tb
;
939 cflags
= curr_cflags(cpu
);
941 cpu
->cflags_next_tb
= -1;
944 if (check_for_breakpoints(cpu
, pc
, &cflags
)) {
948 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
951 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, cflags
);
954 * We add the TB in the virtual pc hash table
955 * for the fast lookup
957 qatomic_set(&cpu
->tb_jmp_cache
[tb_jmp_cache_hash_func(pc
)], tb
);
960 #ifndef CONFIG_USER_ONLY
962 * We don't take care of direct jumps when address mapping
963 * changes in system emulation. So it's not safe to make a
964 * direct jump to a TB spanning two pages because the mapping
965 * for the second page can change.
967 if (tb
->page_addr
[1] != -1) {
971 /* See if we can patch the calling TB. */
973 tb_add_jump(last_tb
, tb_exit
, tb
);
976 cpu_loop_exec_tb(cpu
, tb
, &last_tb
, &tb_exit
);
978 /* Try to align the host and virtual clocks
979 if the guest is in advance */
980 align_clocks(&sc
, cpu
);
990 void tcg_exec_realizefn(CPUState
*cpu
, Error
**errp
)
992 static bool tcg_target_initialized
;
993 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
995 if (!tcg_target_initialized
) {
996 cc
->tcg_ops
->initialize();
997 tcg_target_initialized
= true;
1000 qemu_plugin_vcpu_init_hook(cpu
);
1002 #ifndef CONFIG_USER_ONLY
1003 tcg_iommu_init_notifier_list(cpu
);
1004 #endif /* !CONFIG_USER_ONLY */
1007 /* undo the initializations in reverse order */
1008 void tcg_exec_unrealizefn(CPUState
*cpu
)
1010 #ifndef CONFIG_USER_ONLY
1011 tcg_iommu_free_notifier_list(cpu
);
1012 #endif /* !CONFIG_USER_ONLY */
1014 qemu_plugin_vcpu_exit_hook(cpu
);
1018 #ifndef CONFIG_USER_ONLY
1020 void dump_drift_info(void)
1022 if (!icount_enabled()) {
1026 qemu_printf("Host - Guest clock %"PRIi64
" ms\n",
1027 (cpu_get_clock() - icount_get()) / SCALE_MS
);
1028 if (icount_align_option
) {
1029 qemu_printf("Max guest delay %"PRIi64
" ms\n",
1030 -max_delay
/ SCALE_MS
);
1031 qemu_printf("Max guest advance %"PRIi64
" ms\n",
1032 max_advance
/ SCALE_MS
);
1034 qemu_printf("Max guest delay NA\n");
1035 qemu_printf("Max guest advance NA\n");
1039 #endif /* !CONFIG_USER_ONLY */