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/qemu-print.h"
22 #include "qapi/error.h"
23 #include "qapi/type-helpers.h"
24 #include "hw/core/tcg-cpu-ops.h"
26 #include "disas/disas.h"
27 #include "exec/exec-all.h"
29 #include "qemu/atomic.h"
32 #include "qemu/main-loop.h"
33 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
34 #include "hw/i386/apic.h"
36 #include "sysemu/cpus.h"
37 #include "exec/cpu-all.h"
38 #include "sysemu/cpu-timers.h"
39 #include "exec/replay-core.h"
40 #include "sysemu/tcg.h"
41 #include "exec/helper-proto-common.h"
42 #include "tb-jmp-cache.h"
44 #include "tb-context.h"
47 /* -icount align implementation. */
49 typedef struct SyncClocks
{
51 int64_t last_cpu_icount
;
52 int64_t realtime_clock
;
55 #if !defined(CONFIG_USER_ONLY)
56 /* Allow the guest to have a max 3ms advance.
57 * The difference between the 2 clocks could therefore
60 #define VM_CLOCK_ADVANCE 3000000
61 #define THRESHOLD_REDUCE 1.5
62 #define MAX_DELAY_PRINT_RATE 2000000000LL
63 #define MAX_NB_PRINTS 100
68 static void align_clocks(SyncClocks
*sc
, CPUState
*cpu
)
72 if (!icount_align_option
) {
76 cpu_icount
= cpu
->icount_extra
+ cpu
->neg
.icount_decr
.u16
.low
;
77 sc
->diff_clk
+= icount_to_ns(sc
->last_cpu_icount
- cpu_icount
);
78 sc
->last_cpu_icount
= cpu_icount
;
80 if (sc
->diff_clk
> VM_CLOCK_ADVANCE
) {
82 struct timespec sleep_delay
, rem_delay
;
83 sleep_delay
.tv_sec
= sc
->diff_clk
/ 1000000000LL;
84 sleep_delay
.tv_nsec
= sc
->diff_clk
% 1000000000LL;
85 if (nanosleep(&sleep_delay
, &rem_delay
) < 0) {
86 sc
->diff_clk
= rem_delay
.tv_sec
* 1000000000LL + rem_delay
.tv_nsec
;
91 Sleep(sc
->diff_clk
/ SCALE_MS
);
97 static void print_delay(const SyncClocks
*sc
)
99 static float threshold_delay
;
100 static int64_t last_realtime_clock
;
101 static int nb_prints
;
103 if (icount_align_option
&&
104 sc
->realtime_clock
- last_realtime_clock
>= MAX_DELAY_PRINT_RATE
&&
105 nb_prints
< MAX_NB_PRINTS
) {
106 if ((-sc
->diff_clk
/ (float)1000000000LL > threshold_delay
) ||
107 (-sc
->diff_clk
/ (float)1000000000LL <
108 (threshold_delay
- THRESHOLD_REDUCE
))) {
109 threshold_delay
= (-sc
->diff_clk
/ 1000000000LL) + 1;
110 qemu_printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
114 last_realtime_clock
= sc
->realtime_clock
;
119 static void init_delay_params(SyncClocks
*sc
, CPUState
*cpu
)
121 if (!icount_align_option
) {
124 sc
->realtime_clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
);
125 sc
->diff_clk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - sc
->realtime_clock
;
127 = cpu
->icount_extra
+ cpu
->neg
.icount_decr
.u16
.low
;
128 if (sc
->diff_clk
< max_delay
) {
129 max_delay
= sc
->diff_clk
;
131 if (sc
->diff_clk
> max_advance
) {
132 max_advance
= sc
->diff_clk
;
135 /* Print every 2s max if the guest is late. We limit the number
136 of printed messages to NB_PRINT_MAX(currently 100) */
140 static void align_clocks(SyncClocks
*sc
, const CPUState
*cpu
)
144 static void init_delay_params(SyncClocks
*sc
, const CPUState
*cpu
)
147 #endif /* CONFIG USER ONLY */
149 uint32_t curr_cflags(CPUState
*cpu
)
151 uint32_t cflags
= cpu
->tcg_cflags
;
154 * Record gdb single-step. We should be exiting the TB by raising
155 * EXCP_DEBUG, but to simplify other tests, disable chaining too.
157 * For singlestep and -d nochain, suppress goto_tb so that
158 * we can log -d cpu,exec after every TB.
160 if (unlikely(cpu
->singlestep_enabled
)) {
161 cflags
|= CF_NO_GOTO_TB
| CF_NO_GOTO_PTR
| CF_SINGLE_STEP
| 1;
162 } else if (qatomic_read(&one_insn_per_tb
)) {
163 cflags
|= CF_NO_GOTO_TB
| 1;
164 } else if (qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN
)) {
165 cflags
|= CF_NO_GOTO_TB
;
175 tb_page_addr_t page_addr0
;
180 static bool tb_lookup_cmp(const void *p
, const void *d
)
182 const TranslationBlock
*tb
= p
;
183 const struct tb_desc
*desc
= d
;
185 if ((tb_cflags(tb
) & CF_PCREL
|| tb
->pc
== desc
->pc
) &&
186 tb_page_addr0(tb
) == desc
->page_addr0
&&
187 tb
->cs_base
== desc
->cs_base
&&
188 tb
->flags
== desc
->flags
&&
189 tb_cflags(tb
) == desc
->cflags
) {
190 /* check next page if needed */
191 tb_page_addr_t tb_phys_page1
= tb_page_addr1(tb
);
192 if (tb_phys_page1
== -1) {
195 tb_page_addr_t phys_page1
;
199 * We know that the first page matched, and an otherwise valid TB
200 * encountered an incomplete instruction at the end of that page,
201 * therefore we know that generating a new TB from the current PC
202 * must also require reading from the next page -- even if the
203 * second pages do not match, and therefore the resulting insn
204 * is different for the new TB. Therefore any exception raised
205 * here by the faulting lookup is not premature.
207 virt_page1
= TARGET_PAGE_ALIGN(desc
->pc
);
208 phys_page1
= get_page_addr_code(desc
->env
, virt_page1
);
209 if (tb_phys_page1
== phys_page1
) {
217 static TranslationBlock
*tb_htable_lookup(CPUState
*cpu
, vaddr pc
,
218 uint64_t cs_base
, uint32_t flags
,
221 tb_page_addr_t phys_pc
;
225 desc
.env
= cpu
->env_ptr
;
226 desc
.cs_base
= cs_base
;
228 desc
.cflags
= cflags
;
230 phys_pc
= get_page_addr_code(desc
.env
, pc
);
234 desc
.page_addr0
= phys_pc
;
235 h
= tb_hash_func(phys_pc
, (cflags
& CF_PCREL
? 0 : pc
),
236 flags
, cs_base
, cflags
);
237 return qht_lookup_custom(&tb_ctx
.htable
, &desc
, h
, tb_lookup_cmp
);
240 /* Might cause an exception, so have a longjmp destination ready */
241 static inline TranslationBlock
*tb_lookup(CPUState
*cpu
, vaddr pc
,
242 uint64_t cs_base
, uint32_t flags
,
245 TranslationBlock
*tb
;
249 /* we should never be trying to look up an INVALID tb */
250 tcg_debug_assert(!(cflags
& CF_INVALID
));
252 hash
= tb_jmp_cache_hash_func(pc
);
253 jc
= cpu
->tb_jmp_cache
;
255 if (cflags
& CF_PCREL
) {
256 /* Use acquire to ensure current load of pc from jc. */
257 tb
= qatomic_load_acquire(&jc
->array
[hash
].tb
);
260 jc
->array
[hash
].pc
== pc
&&
261 tb
->cs_base
== cs_base
&&
262 tb
->flags
== flags
&&
263 tb_cflags(tb
) == cflags
)) {
266 tb
= tb_htable_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
270 jc
->array
[hash
].pc
= pc
;
271 /* Ensure pc is written first. */
272 qatomic_store_release(&jc
->array
[hash
].tb
, tb
);
274 /* Use rcu_read to ensure current load of pc from *tb. */
275 tb
= qatomic_rcu_read(&jc
->array
[hash
].tb
);
279 tb
->cs_base
== cs_base
&&
280 tb
->flags
== flags
&&
281 tb_cflags(tb
) == cflags
)) {
284 tb
= tb_htable_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
288 /* Use the pc value already stored in tb->pc. */
289 qatomic_set(&jc
->array
[hash
].tb
, tb
);
295 static void log_cpu_exec(vaddr pc
, CPUState
*cpu
,
296 const TranslationBlock
*tb
)
298 if (qemu_log_in_addr_range(pc
)) {
299 qemu_log_mask(CPU_LOG_EXEC
,
300 "Trace %d: %p [%08" PRIx64
301 "/%016" VADDR_PRIx
"/%08x/%08x] %s\n",
302 cpu
->cpu_index
, tb
->tc
.ptr
, tb
->cs_base
, pc
,
303 tb
->flags
, tb
->cflags
, lookup_symbol(pc
));
305 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
)) {
306 FILE *logfile
= qemu_log_trylock();
310 if (qemu_loglevel_mask(CPU_LOG_TB_FPU
)) {
311 flags
|= CPU_DUMP_FPU
;
313 #if defined(TARGET_I386)
314 flags
|= CPU_DUMP_CCOP
;
316 if (qemu_loglevel_mask(CPU_LOG_TB_VPU
)) {
317 flags
|= CPU_DUMP_VPU
;
319 cpu_dump_state(cpu
, logfile
, flags
);
320 qemu_log_unlock(logfile
);
326 static bool check_for_breakpoints_slow(CPUState
*cpu
, vaddr pc
,
330 bool match_page
= false;
333 * Singlestep overrides breakpoints.
334 * This requirement is visible in the record-replay tests, where
335 * we would fail to make forward progress in reverse-continue.
337 * TODO: gdb singlestep should only override gdb breakpoints,
338 * so that one could (gdb) singlestep into the guest kernel's
339 * architectural breakpoint handler.
341 if (cpu
->singlestep_enabled
) {
345 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
347 * If we have an exact pc match, trigger the breakpoint.
348 * Otherwise, note matches within the page.
351 bool match_bp
= false;
353 if (bp
->flags
& BP_GDB
) {
355 } else if (bp
->flags
& BP_CPU
) {
356 #ifdef CONFIG_USER_ONLY
357 g_assert_not_reached();
359 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
360 assert(cc
->tcg_ops
->debug_check_breakpoint
);
361 match_bp
= cc
->tcg_ops
->debug_check_breakpoint(cpu
);
366 cpu
->exception_index
= EXCP_DEBUG
;
369 } else if (((pc
^ bp
->pc
) & TARGET_PAGE_MASK
) == 0) {
375 * Within the same page as a breakpoint, single-step,
376 * returning to helper_lookup_tb_ptr after each insn looking
377 * for the actual breakpoint.
379 * TODO: Perhaps better to record all of the TBs associated
380 * with a given virtual page that contains a breakpoint, and
381 * then invalidate them when a new overlapping breakpoint is
382 * set on the page. Non-overlapping TBs would not be
383 * invalidated, nor would any TB need to be invalidated as
384 * breakpoints are removed.
387 *cflags
= (*cflags
& ~CF_COUNT_MASK
) | CF_NO_GOTO_TB
| 1;
392 static inline bool check_for_breakpoints(CPUState
*cpu
, vaddr pc
,
395 return unlikely(!QTAILQ_EMPTY(&cpu
->breakpoints
)) &&
396 check_for_breakpoints_slow(cpu
, pc
, cflags
);
400 * helper_lookup_tb_ptr: quick check for next tb
401 * @env: current cpu state
403 * Look for an existing TB matching the current cpu state.
404 * If found, return the code pointer. If not found, return
405 * the tcg epilogue so that we return into cpu_tb_exec.
407 const void *HELPER(lookup_tb_ptr
)(CPUArchState
*env
)
409 CPUState
*cpu
= env_cpu(env
);
410 TranslationBlock
*tb
;
413 uint32_t flags
, cflags
;
415 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
417 cflags
= curr_cflags(cpu
);
418 if (check_for_breakpoints(cpu
, pc
, &cflags
)) {
422 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
424 return tcg_code_gen_epilogue
;
427 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
| CPU_LOG_EXEC
)) {
428 log_cpu_exec(pc
, cpu
, tb
);
434 /* Execute a TB, and fix up the CPU state afterwards if necessary */
436 * Disable CFI checks.
437 * TCG creates binary blobs at runtime, with the transformed code.
438 * A TB is a blob of binary code, created at runtime and called with an
439 * indirect function call. Since such function did not exist at compile time,
440 * the CFI runtime has no way to verify its signature and would fail.
441 * TCG is not considered a security-sensitive part of QEMU so this does not
442 * affect the impact of CFI in environment with high security requirements
444 static inline TranslationBlock
* QEMU_DISABLE_CFI
445 cpu_tb_exec(CPUState
*cpu
, TranslationBlock
*itb
, int *tb_exit
)
447 CPUArchState
*env
= cpu
->env_ptr
;
449 TranslationBlock
*last_tb
;
450 const void *tb_ptr
= itb
->tc
.ptr
;
452 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
| CPU_LOG_EXEC
)) {
453 log_cpu_exec(log_pc(cpu
, itb
), cpu
, itb
);
456 qemu_thread_jit_execute();
457 ret
= tcg_qemu_tb_exec(env
, tb_ptr
);
458 cpu
->neg
.can_do_io
= true;
459 qemu_plugin_disable_mem_helpers(cpu
);
461 * TODO: Delay swapping back to the read-write region of the TB
462 * until we actually need to modify the TB. The read-only copy,
463 * coming from the rx region, shares the same host TLB entry as
464 * the code that executed the exit_tb opcode that arrived here.
465 * If we insist on touching both the RX and the RW pages, we
466 * double the host TLB pressure.
468 last_tb
= tcg_splitwx_to_rw((void *)(ret
& ~TB_EXIT_MASK
));
469 *tb_exit
= ret
& TB_EXIT_MASK
;
471 trace_exec_tb_exit(last_tb
, *tb_exit
);
473 if (*tb_exit
> TB_EXIT_IDX1
) {
474 /* We didn't start executing this TB (eg because the instruction
475 * counter hit zero); we must restore the guest PC to the address
476 * of the start of the TB.
478 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
480 if (cc
->tcg_ops
->synchronize_from_tb
) {
481 cc
->tcg_ops
->synchronize_from_tb(cpu
, last_tb
);
483 tcg_debug_assert(!(tb_cflags(last_tb
) & CF_PCREL
));
485 cc
->set_pc(cpu
, last_tb
->pc
);
487 if (qemu_loglevel_mask(CPU_LOG_EXEC
)) {
488 vaddr pc
= log_pc(cpu
, last_tb
);
489 if (qemu_log_in_addr_range(pc
)) {
490 qemu_log("Stopped execution of TB chain before %p [%016"
492 last_tb
->tc
.ptr
, pc
, lookup_symbol(pc
));
498 * If gdb single-step, and we haven't raised another exception,
499 * raise a debug exception. Single-step with another exception
500 * is handled in cpu_handle_exception.
502 if (unlikely(cpu
->singlestep_enabled
) && cpu
->exception_index
== -1) {
503 cpu
->exception_index
= EXCP_DEBUG
;
511 static void cpu_exec_enter(CPUState
*cpu
)
513 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
515 if (cc
->tcg_ops
->cpu_exec_enter
) {
516 cc
->tcg_ops
->cpu_exec_enter(cpu
);
520 static void cpu_exec_exit(CPUState
*cpu
)
522 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
524 if (cc
->tcg_ops
->cpu_exec_exit
) {
525 cc
->tcg_ops
->cpu_exec_exit(cpu
);
529 static void cpu_exec_longjmp_cleanup(CPUState
*cpu
)
531 /* Non-buggy compilers preserve this; assert the correct value. */
532 g_assert(cpu
== current_cpu
);
534 #ifdef CONFIG_USER_ONLY
535 clear_helper_retaddr();
536 if (have_mmap_lock()) {
541 * For softmmu, a tlb_fill fault during translation will land here,
542 * and we need to release any page locks held. In system mode we
543 * have one tcg_ctx per thread, so we know it was this cpu doing
546 * Alternative 1: Install a cleanup to be called via an exception
547 * handling safe longjmp. It seems plausible that all our hosts
548 * support such a thing. We'd have to properly register unwind info
549 * for the JIT for EH, rather that just for GDB.
551 * Alternative 2: Set and restore cpu->jmp_env in tb_gen_code to
552 * capture the cpu_loop_exit longjmp, perform the cleanup, and
553 * jump again to arrive here.
555 if (tcg_ctx
->gen_tb
) {
556 tb_unlock_pages(tcg_ctx
->gen_tb
);
557 tcg_ctx
->gen_tb
= NULL
;
560 if (qemu_mutex_iothread_locked()) {
561 qemu_mutex_unlock_iothread();
563 assert_no_pages_locked();
566 void cpu_exec_step_atomic(CPUState
*cpu
)
568 CPUArchState
*env
= cpu
->env_ptr
;
569 TranslationBlock
*tb
;
572 uint32_t flags
, cflags
;
575 if (sigsetjmp(cpu
->jmp_env
, 0) == 0) {
577 g_assert(cpu
== current_cpu
);
578 g_assert(!cpu
->running
);
581 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
583 cflags
= curr_cflags(cpu
);
584 /* Execute in a serial context. */
585 cflags
&= ~CF_PARALLEL
;
586 /* After 1 insn, return and release the exclusive lock. */
587 cflags
|= CF_NO_GOTO_TB
| CF_NO_GOTO_PTR
| 1;
589 * No need to check_for_breakpoints here.
590 * We only arrive in cpu_exec_step_atomic after beginning execution
591 * of an insn that includes an atomic operation we can't handle.
592 * Any breakpoint for this insn will have been recognized earlier.
595 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
598 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, cflags
);
603 /* execute the generated code */
604 trace_exec_tb(tb
, pc
);
605 cpu_tb_exec(cpu
, tb
, &tb_exit
);
608 cpu_exec_longjmp_cleanup(cpu
);
612 * As we start the exclusive region before codegen we must still
613 * be in the region if we longjump out of either the codegen or
616 g_assert(cpu_in_exclusive_context(cpu
));
617 cpu
->running
= false;
621 void tb_set_jmp_target(TranslationBlock
*tb
, int n
, uintptr_t addr
)
624 * Get the rx view of the structure, from which we find the
625 * executable code address, and tb_target_set_jmp_target can
626 * produce a pc-relative displacement to jmp_target_addr[n].
628 const TranslationBlock
*c_tb
= tcg_splitwx_to_rx(tb
);
629 uintptr_t offset
= tb
->jmp_insn_offset
[n
];
630 uintptr_t jmp_rx
= (uintptr_t)tb
->tc
.ptr
+ offset
;
631 uintptr_t jmp_rw
= jmp_rx
- tcg_splitwx_diff
;
633 tb
->jmp_target_addr
[n
] = addr
;
634 tb_target_set_jmp_target(c_tb
, n
, jmp_rx
, jmp_rw
);
637 static inline void tb_add_jump(TranslationBlock
*tb
, int n
,
638 TranslationBlock
*tb_next
)
642 qemu_thread_jit_write();
643 assert(n
< ARRAY_SIZE(tb
->jmp_list_next
));
644 qemu_spin_lock(&tb_next
->jmp_lock
);
646 /* make sure the destination TB is valid */
647 if (tb_next
->cflags
& CF_INVALID
) {
648 goto out_unlock_next
;
650 /* Atomically claim the jump destination slot only if it was NULL */
651 old
= qatomic_cmpxchg(&tb
->jmp_dest
[n
], (uintptr_t)NULL
,
654 goto out_unlock_next
;
657 /* patch the native jump address */
658 tb_set_jmp_target(tb
, n
, (uintptr_t)tb_next
->tc
.ptr
);
660 /* add in TB jmp list */
661 tb
->jmp_list_next
[n
] = tb_next
->jmp_list_head
;
662 tb_next
->jmp_list_head
= (uintptr_t)tb
| n
;
664 qemu_spin_unlock(&tb_next
->jmp_lock
);
666 qemu_log_mask(CPU_LOG_EXEC
, "Linking TBs %p index %d -> %p\n",
667 tb
->tc
.ptr
, n
, tb_next
->tc
.ptr
);
671 qemu_spin_unlock(&tb_next
->jmp_lock
);
675 static inline bool cpu_handle_halt(CPUState
*cpu
)
677 #ifndef CONFIG_USER_ONLY
679 #if defined(TARGET_I386)
680 if (cpu
->interrupt_request
& CPU_INTERRUPT_POLL
) {
681 X86CPU
*x86_cpu
= X86_CPU(cpu
);
682 qemu_mutex_lock_iothread();
683 apic_poll_irq(x86_cpu
->apic_state
);
684 cpu_reset_interrupt(cpu
, CPU_INTERRUPT_POLL
);
685 qemu_mutex_unlock_iothread();
687 #endif /* TARGET_I386 */
688 if (!cpu_has_work(cpu
)) {
694 #endif /* !CONFIG_USER_ONLY */
699 static inline void cpu_handle_debug_exception(CPUState
*cpu
)
701 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
704 if (!cpu
->watchpoint_hit
) {
705 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
706 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
710 if (cc
->tcg_ops
->debug_excp_handler
) {
711 cc
->tcg_ops
->debug_excp_handler(cpu
);
715 static inline bool cpu_handle_exception(CPUState
*cpu
, int *ret
)
717 if (cpu
->exception_index
< 0) {
718 #ifndef CONFIG_USER_ONLY
719 if (replay_has_exception()
720 && cpu
->neg
.icount_decr
.u16
.low
+ cpu
->icount_extra
== 0) {
721 /* Execute just one insn to trigger exception pending in the log */
722 cpu
->cflags_next_tb
= (curr_cflags(cpu
) & ~CF_USE_ICOUNT
)
723 | CF_LAST_IO
| CF_NOIRQ
| 1;
728 if (cpu
->exception_index
>= EXCP_INTERRUPT
) {
729 /* exit request from the cpu execution loop */
730 *ret
= cpu
->exception_index
;
731 if (*ret
== EXCP_DEBUG
) {
732 cpu_handle_debug_exception(cpu
);
734 cpu
->exception_index
= -1;
737 #if defined(CONFIG_USER_ONLY)
738 /* if user mode only, we simulate a fake exception
739 which will be handled outside the cpu execution
741 #if defined(TARGET_I386)
742 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
743 cc
->tcg_ops
->fake_user_interrupt(cpu
);
744 #endif /* TARGET_I386 */
745 *ret
= cpu
->exception_index
;
746 cpu
->exception_index
= -1;
749 if (replay_exception()) {
750 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
751 qemu_mutex_lock_iothread();
752 cc
->tcg_ops
->do_interrupt(cpu
);
753 qemu_mutex_unlock_iothread();
754 cpu
->exception_index
= -1;
756 if (unlikely(cpu
->singlestep_enabled
)) {
758 * After processing the exception, ensure an EXCP_DEBUG is
759 * raised when single-stepping so that GDB doesn't miss the
763 cpu_handle_debug_exception(cpu
);
766 } else if (!replay_has_interrupt()) {
767 /* give a chance to iothread in replay mode */
768 *ret
= EXCP_INTERRUPT
;
777 #ifndef CONFIG_USER_ONLY
779 * CPU_INTERRUPT_POLL is a virtual event which gets converted into a
780 * "real" interrupt event later. It does not need to be recorded for
783 static inline bool need_replay_interrupt(int interrupt_request
)
785 #if defined(TARGET_I386)
786 return !(interrupt_request
& CPU_INTERRUPT_POLL
);
791 #endif /* !CONFIG_USER_ONLY */
793 static inline bool cpu_handle_interrupt(CPUState
*cpu
,
794 TranslationBlock
**last_tb
)
797 * If we have requested custom cflags with CF_NOIRQ we should
798 * skip checking here. Any pending interrupts will get picked up
799 * by the next TB we execute under normal cflags.
801 if (cpu
->cflags_next_tb
!= -1 && cpu
->cflags_next_tb
& CF_NOIRQ
) {
805 /* Clear the interrupt flag now since we're processing
806 * cpu->interrupt_request and cpu->exit_request.
807 * Ensure zeroing happens before reading cpu->exit_request or
808 * cpu->interrupt_request (see also smp_wmb in cpu_exit())
810 qatomic_set_mb(&cpu
->neg
.icount_decr
.u16
.high
, 0);
812 if (unlikely(qatomic_read(&cpu
->interrupt_request
))) {
813 int interrupt_request
;
814 qemu_mutex_lock_iothread();
815 interrupt_request
= cpu
->interrupt_request
;
816 if (unlikely(cpu
->singlestep_enabled
& SSTEP_NOIRQ
)) {
817 /* Mask out external interrupts for this step. */
818 interrupt_request
&= ~CPU_INTERRUPT_SSTEP_MASK
;
820 if (interrupt_request
& CPU_INTERRUPT_DEBUG
) {
821 cpu
->interrupt_request
&= ~CPU_INTERRUPT_DEBUG
;
822 cpu
->exception_index
= EXCP_DEBUG
;
823 qemu_mutex_unlock_iothread();
826 #if !defined(CONFIG_USER_ONLY)
827 if (replay_mode
== REPLAY_MODE_PLAY
&& !replay_has_interrupt()) {
829 } else if (interrupt_request
& CPU_INTERRUPT_HALT
) {
831 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HALT
;
833 cpu
->exception_index
= EXCP_HLT
;
834 qemu_mutex_unlock_iothread();
837 #if defined(TARGET_I386)
838 else if (interrupt_request
& CPU_INTERRUPT_INIT
) {
839 X86CPU
*x86_cpu
= X86_CPU(cpu
);
840 CPUArchState
*env
= &x86_cpu
->env
;
842 cpu_svm_check_intercept_param(env
, SVM_EXIT_INIT
, 0, 0);
843 do_cpu_init(x86_cpu
);
844 cpu
->exception_index
= EXCP_HALTED
;
845 qemu_mutex_unlock_iothread();
849 else if (interrupt_request
& CPU_INTERRUPT_RESET
) {
852 qemu_mutex_unlock_iothread();
855 #endif /* !TARGET_I386 */
856 /* The target hook has 3 exit conditions:
857 False when the interrupt isn't processed,
858 True when it is, and we should restart on a new TB,
859 and via longjmp via cpu_loop_exit. */
861 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
863 if (cc
->tcg_ops
->cpu_exec_interrupt
&&
864 cc
->tcg_ops
->cpu_exec_interrupt(cpu
, interrupt_request
)) {
865 if (need_replay_interrupt(interrupt_request
)) {
869 * After processing the interrupt, ensure an EXCP_DEBUG is
870 * raised when single-stepping so that GDB doesn't miss the
873 if (unlikely(cpu
->singlestep_enabled
)) {
874 cpu
->exception_index
= EXCP_DEBUG
;
875 qemu_mutex_unlock_iothread();
878 cpu
->exception_index
= -1;
881 /* The target hook may have updated the 'cpu->interrupt_request';
882 * reload the 'interrupt_request' value */
883 interrupt_request
= cpu
->interrupt_request
;
885 #endif /* !CONFIG_USER_ONLY */
886 if (interrupt_request
& CPU_INTERRUPT_EXITTB
) {
887 cpu
->interrupt_request
&= ~CPU_INTERRUPT_EXITTB
;
888 /* ensure that no TB jump will be modified as
889 the program flow was changed */
893 /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
894 qemu_mutex_unlock_iothread();
897 /* Finally, check if we need to exit to the main loop. */
898 if (unlikely(qatomic_read(&cpu
->exit_request
))
900 && (cpu
->cflags_next_tb
== -1 || cpu
->cflags_next_tb
& CF_USE_ICOUNT
)
901 && cpu
->neg
.icount_decr
.u16
.low
+ cpu
->icount_extra
== 0)) {
902 qatomic_set(&cpu
->exit_request
, 0);
903 if (cpu
->exception_index
== -1) {
904 cpu
->exception_index
= EXCP_INTERRUPT
;
912 static inline void cpu_loop_exec_tb(CPUState
*cpu
, TranslationBlock
*tb
,
913 vaddr pc
, TranslationBlock
**last_tb
,
918 trace_exec_tb(tb
, pc
);
919 tb
= cpu_tb_exec(cpu
, tb
, tb_exit
);
920 if (*tb_exit
!= TB_EXIT_REQUESTED
) {
926 insns_left
= qatomic_read(&cpu
->neg
.icount_decr
.u32
);
927 if (insns_left
< 0) {
928 /* Something asked us to stop executing chained TBs; just
929 * continue round the main loop. Whatever requested the exit
930 * will also have set something else (eg exit_request or
931 * interrupt_request) which will be handled by
932 * cpu_handle_interrupt. cpu_handle_interrupt will also
933 * clear cpu->icount_decr.u16.high.
938 /* Instruction counter expired. */
939 assert(icount_enabled());
940 #ifndef CONFIG_USER_ONLY
941 /* Ensure global icount has gone forward */
943 /* Refill decrementer and continue execution. */
944 insns_left
= MIN(0xffff, cpu
->icount_budget
);
945 cpu
->neg
.icount_decr
.u16
.low
= insns_left
;
946 cpu
->icount_extra
= cpu
->icount_budget
- insns_left
;
949 * If the next tb has more instructions than we have left to
950 * execute we need to ensure we find/generate a TB with exactly
951 * insns_left instructions in it.
953 if (insns_left
> 0 && insns_left
< tb
->icount
) {
954 assert(insns_left
<= CF_COUNT_MASK
);
955 assert(cpu
->icount_extra
== 0);
956 cpu
->cflags_next_tb
= (tb
->cflags
& ~CF_COUNT_MASK
) | insns_left
;
961 /* main execution loop */
963 static int __attribute__((noinline
))
964 cpu_exec_loop(CPUState
*cpu
, SyncClocks
*sc
)
968 /* if an exception is pending, we execute it here */
969 while (!cpu_handle_exception(cpu
, &ret
)) {
970 TranslationBlock
*last_tb
= NULL
;
973 while (!cpu_handle_interrupt(cpu
, &last_tb
)) {
974 TranslationBlock
*tb
;
977 uint32_t flags
, cflags
;
979 cpu_get_tb_cpu_state(cpu
->env_ptr
, &pc
, &cs_base
, &flags
);
982 * When requested, use an exact setting for cflags for the next
983 * execution. This is used for icount, precise smc, and stop-
984 * after-access watchpoints. Since this request should never
985 * have CF_INVALID set, -1 is a convenient invalid value that
986 * does not require tcg headers for cpu_common_reset.
988 cflags
= cpu
->cflags_next_tb
;
990 cflags
= curr_cflags(cpu
);
992 cpu
->cflags_next_tb
= -1;
995 if (check_for_breakpoints(cpu
, pc
, &cflags
)) {
999 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
1005 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, cflags
);
1009 * We add the TB in the virtual pc hash table
1010 * for the fast lookup
1012 h
= tb_jmp_cache_hash_func(pc
);
1013 jc
= cpu
->tb_jmp_cache
;
1014 if (cflags
& CF_PCREL
) {
1015 jc
->array
[h
].pc
= pc
;
1016 /* Ensure pc is written first. */
1017 qatomic_store_release(&jc
->array
[h
].tb
, tb
);
1019 /* Use the pc value already stored in tb->pc. */
1020 qatomic_set(&jc
->array
[h
].tb
, tb
);
1024 #ifndef CONFIG_USER_ONLY
1026 * We don't take care of direct jumps when address mapping
1027 * changes in system emulation. So it's not safe to make a
1028 * direct jump to a TB spanning two pages because the mapping
1029 * for the second page can change.
1031 if (tb_page_addr1(tb
) != -1) {
1035 /* See if we can patch the calling TB. */
1037 tb_add_jump(last_tb
, tb_exit
, tb
);
1040 cpu_loop_exec_tb(cpu
, tb
, pc
, &last_tb
, &tb_exit
);
1042 /* Try to align the host and virtual clocks
1043 if the guest is in advance */
1044 align_clocks(sc
, cpu
);
1050 static int cpu_exec_setjmp(CPUState
*cpu
, SyncClocks
*sc
)
1052 /* Prepare setjmp context for exception handling. */
1053 if (unlikely(sigsetjmp(cpu
->jmp_env
, 0) != 0)) {
1054 cpu_exec_longjmp_cleanup(cpu
);
1057 return cpu_exec_loop(cpu
, sc
);
1060 int cpu_exec(CPUState
*cpu
)
1063 SyncClocks sc
= { 0 };
1065 /* replay_interrupt may need current_cpu */
1068 if (cpu_handle_halt(cpu
)) {
1073 cpu_exec_enter(cpu
);
1076 * Calculate difference between guest clock and host clock.
1077 * This delay includes the delay of the last cycle, so
1078 * what we have to do is sleep until it is 0. As for the
1079 * advance/delay we gain here, we try to fix it next time.
1081 init_delay_params(&sc
, cpu
);
1083 ret
= cpu_exec_setjmp(cpu
, &sc
);
1091 bool tcg_exec_realizefn(CPUState
*cpu
, Error
**errp
)
1093 static bool tcg_target_initialized
;
1094 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1096 if (!tcg_target_initialized
) {
1097 cc
->tcg_ops
->initialize();
1098 tcg_target_initialized
= true;
1101 cpu
->tb_jmp_cache
= g_new0(CPUJumpCache
, 1);
1103 #ifndef CONFIG_USER_ONLY
1104 tcg_iommu_init_notifier_list(cpu
);
1105 #endif /* !CONFIG_USER_ONLY */
1106 /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */
1111 /* undo the initializations in reverse order */
1112 void tcg_exec_unrealizefn(CPUState
*cpu
)
1114 #ifndef CONFIG_USER_ONLY
1115 tcg_iommu_free_notifier_list(cpu
);
1116 #endif /* !CONFIG_USER_ONLY */
1119 g_free_rcu(cpu
->tb_jmp_cache
, rcu
);