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 #include "sysemu/cpus.h"
34 #include "exec/cpu-all.h"
35 #include "sysemu/cpu-timers.h"
36 #include "exec/replay-core.h"
37 #include "sysemu/tcg.h"
38 #include "exec/helper-proto-common.h"
39 #include "tb-jmp-cache.h"
41 #include "tb-context.h"
42 #include "internal-common.h"
43 #include "internal-target.h"
45 /* -icount align implementation. */
47 typedef struct SyncClocks
{
49 int64_t last_cpu_icount
;
50 int64_t realtime_clock
;
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
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
66 static void align_clocks(SyncClocks
*sc
, CPUState
*cpu
)
70 if (!icount_align_option
) {
74 cpu_icount
= cpu
->icount_extra
+ cpu
->neg
.icount_decr
.u16
.low
;
75 sc
->diff_clk
+= icount_to_ns(sc
->last_cpu_icount
- cpu_icount
);
76 sc
->last_cpu_icount
= cpu_icount
;
78 if (sc
->diff_clk
> VM_CLOCK_ADVANCE
) {
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
;
89 Sleep(sc
->diff_clk
/ SCALE_MS
);
95 static void print_delay(const SyncClocks
*sc
)
97 static float threshold_delay
;
98 static int64_t last_realtime_clock
;
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",
112 last_realtime_clock
= sc
->realtime_clock
;
117 static void init_delay_params(SyncClocks
*sc
, CPUState
*cpu
)
119 if (!icount_align_option
) {
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
;
125 = cpu
->icount_extra
+ cpu
->neg
.icount_decr
.u16
.low
;
126 if (sc
->diff_clk
< max_delay
) {
127 max_delay
= sc
->diff_clk
;
129 if (sc
->diff_clk
> max_advance
) {
130 max_advance
= sc
->diff_clk
;
133 /* Print every 2s max if the guest is late. We limit the number
134 of printed messages to NB_PRINT_MAX(currently 100) */
138 static void align_clocks(SyncClocks
*sc
, const CPUState
*cpu
)
142 static void init_delay_params(SyncClocks
*sc
, const CPUState
*cpu
)
145 #endif /* CONFIG USER ONLY */
147 bool tcg_cflags_has(CPUState
*cpu
, uint32_t flags
)
149 return cpu
->tcg_cflags
& flags
;
152 void tcg_cflags_set(CPUState
*cpu
, uint32_t flags
)
154 cpu
->tcg_cflags
|= flags
;
157 uint32_t curr_cflags(CPUState
*cpu
)
159 uint32_t cflags
= cpu
->tcg_cflags
;
162 * Record gdb single-step. We should be exiting the TB by raising
163 * EXCP_DEBUG, but to simplify other tests, disable chaining too.
165 * For singlestep and -d nochain, suppress goto_tb so that
166 * we can log -d cpu,exec after every TB.
168 if (unlikely(cpu
->singlestep_enabled
)) {
169 cflags
|= CF_NO_GOTO_TB
| CF_NO_GOTO_PTR
| CF_SINGLE_STEP
| 1;
170 } else if (qatomic_read(&one_insn_per_tb
)) {
171 cflags
|= CF_NO_GOTO_TB
| 1;
172 } else if (qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN
)) {
173 cflags
|= CF_NO_GOTO_TB
;
183 tb_page_addr_t page_addr0
;
188 static bool tb_lookup_cmp(const void *p
, const void *d
)
190 const TranslationBlock
*tb
= p
;
191 const struct tb_desc
*desc
= d
;
193 if ((tb_cflags(tb
) & CF_PCREL
|| tb
->pc
== desc
->pc
) &&
194 tb_page_addr0(tb
) == desc
->page_addr0
&&
195 tb
->cs_base
== desc
->cs_base
&&
196 tb
->flags
== desc
->flags
&&
197 tb_cflags(tb
) == desc
->cflags
) {
198 /* check next page if needed */
199 tb_page_addr_t tb_phys_page1
= tb_page_addr1(tb
);
200 if (tb_phys_page1
== -1) {
203 tb_page_addr_t phys_page1
;
207 * We know that the first page matched, and an otherwise valid TB
208 * encountered an incomplete instruction at the end of that page,
209 * therefore we know that generating a new TB from the current PC
210 * must also require reading from the next page -- even if the
211 * second pages do not match, and therefore the resulting insn
212 * is different for the new TB. Therefore any exception raised
213 * here by the faulting lookup is not premature.
215 virt_page1
= TARGET_PAGE_ALIGN(desc
->pc
);
216 phys_page1
= get_page_addr_code(desc
->env
, virt_page1
);
217 if (tb_phys_page1
== phys_page1
) {
225 static TranslationBlock
*tb_htable_lookup(CPUState
*cpu
, vaddr pc
,
226 uint64_t cs_base
, uint32_t flags
,
229 tb_page_addr_t phys_pc
;
233 desc
.env
= cpu_env(cpu
);
234 desc
.cs_base
= cs_base
;
236 desc
.cflags
= cflags
;
238 phys_pc
= get_page_addr_code(desc
.env
, pc
);
242 desc
.page_addr0
= phys_pc
;
243 h
= tb_hash_func(phys_pc
, (cflags
& CF_PCREL
? 0 : pc
),
244 flags
, cs_base
, cflags
);
245 return qht_lookup_custom(&tb_ctx
.htable
, &desc
, h
, tb_lookup_cmp
);
248 /* Might cause an exception, so have a longjmp destination ready */
249 static inline TranslationBlock
*tb_lookup(CPUState
*cpu
, vaddr pc
,
250 uint64_t cs_base
, uint32_t flags
,
253 TranslationBlock
*tb
;
257 /* we should never be trying to look up an INVALID tb */
258 tcg_debug_assert(!(cflags
& CF_INVALID
));
260 hash
= tb_jmp_cache_hash_func(pc
);
261 jc
= cpu
->tb_jmp_cache
;
263 tb
= qatomic_read(&jc
->array
[hash
].tb
);
265 jc
->array
[hash
].pc
== pc
&&
266 tb
->cs_base
== cs_base
&&
267 tb
->flags
== flags
&&
268 tb_cflags(tb
) == cflags
)) {
272 tb
= tb_htable_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
277 jc
->array
[hash
].pc
= pc
;
278 qatomic_set(&jc
->array
[hash
].tb
, tb
);
282 * As long as tb is not NULL, the contents are consistent. Therefore,
283 * the virtual PC has to match for non-CF_PCREL translations.
285 assert((tb_cflags(tb
) & CF_PCREL
) || tb
->pc
== pc
);
289 static void log_cpu_exec(vaddr pc
, CPUState
*cpu
,
290 const TranslationBlock
*tb
)
292 if (qemu_log_in_addr_range(pc
)) {
293 qemu_log_mask(CPU_LOG_EXEC
,
294 "Trace %d: %p [%08" PRIx64
295 "/%016" VADDR_PRIx
"/%08x/%08x] %s\n",
296 cpu
->cpu_index
, tb
->tc
.ptr
, tb
->cs_base
, pc
,
297 tb
->flags
, tb
->cflags
, lookup_symbol(pc
));
299 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
)) {
300 FILE *logfile
= qemu_log_trylock();
304 if (qemu_loglevel_mask(CPU_LOG_TB_FPU
)) {
305 flags
|= CPU_DUMP_FPU
;
307 #if defined(TARGET_I386)
308 flags
|= CPU_DUMP_CCOP
;
310 if (qemu_loglevel_mask(CPU_LOG_TB_VPU
)) {
311 flags
|= CPU_DUMP_VPU
;
313 cpu_dump_state(cpu
, logfile
, flags
);
314 qemu_log_unlock(logfile
);
320 static bool check_for_breakpoints_slow(CPUState
*cpu
, vaddr pc
,
324 bool match_page
= false;
327 * Singlestep overrides breakpoints.
328 * This requirement is visible in the record-replay tests, where
329 * we would fail to make forward progress in reverse-continue.
331 * TODO: gdb singlestep should only override gdb breakpoints,
332 * so that one could (gdb) singlestep into the guest kernel's
333 * architectural breakpoint handler.
335 if (cpu
->singlestep_enabled
) {
339 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
341 * If we have an exact pc match, trigger the breakpoint.
342 * Otherwise, note matches within the page.
345 bool match_bp
= false;
347 if (bp
->flags
& BP_GDB
) {
349 } else if (bp
->flags
& BP_CPU
) {
350 #ifdef CONFIG_USER_ONLY
351 g_assert_not_reached();
353 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
354 assert(tcg_ops
->debug_check_breakpoint
);
355 match_bp
= tcg_ops
->debug_check_breakpoint(cpu
);
360 cpu
->exception_index
= EXCP_DEBUG
;
363 } else if (((pc
^ bp
->pc
) & TARGET_PAGE_MASK
) == 0) {
369 * Within the same page as a breakpoint, single-step,
370 * returning to helper_lookup_tb_ptr after each insn looking
371 * for the actual breakpoint.
373 * TODO: Perhaps better to record all of the TBs associated
374 * with a given virtual page that contains a breakpoint, and
375 * then invalidate them when a new overlapping breakpoint is
376 * set on the page. Non-overlapping TBs would not be
377 * invalidated, nor would any TB need to be invalidated as
378 * breakpoints are removed.
381 *cflags
= (*cflags
& ~CF_COUNT_MASK
) | CF_NO_GOTO_TB
| CF_BP_PAGE
| 1;
386 static inline bool check_for_breakpoints(CPUState
*cpu
, vaddr pc
,
389 return unlikely(!QTAILQ_EMPTY(&cpu
->breakpoints
)) &&
390 check_for_breakpoints_slow(cpu
, pc
, cflags
);
394 * helper_lookup_tb_ptr: quick check for next tb
395 * @env: current cpu state
397 * Look for an existing TB matching the current cpu state.
398 * If found, return the code pointer. If not found, return
399 * the tcg epilogue so that we return into cpu_tb_exec.
401 const void *HELPER(lookup_tb_ptr
)(CPUArchState
*env
)
403 CPUState
*cpu
= env_cpu(env
);
404 TranslationBlock
*tb
;
407 uint32_t flags
, cflags
;
410 * By definition we've just finished a TB, so I/O is OK.
411 * Avoid the possibility of calling cpu_io_recompile() if
412 * a page table walk triggered by tb_lookup() calling
413 * probe_access_internal() happens to touch an MMIO device.
414 * The next TB, if we chain to it, will clear the flag again.
416 cpu
->neg
.can_do_io
= true;
417 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
419 cflags
= curr_cflags(cpu
);
420 if (check_for_breakpoints(cpu
, pc
, &cflags
)) {
424 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
426 return tcg_code_gen_epilogue
;
429 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
| CPU_LOG_EXEC
)) {
430 log_cpu_exec(pc
, cpu
, tb
);
436 /* Execute a TB, and fix up the CPU state afterwards if necessary */
438 * Disable CFI checks.
439 * TCG creates binary blobs at runtime, with the transformed code.
440 * A TB is a blob of binary code, created at runtime and called with an
441 * indirect function call. Since such function did not exist at compile time,
442 * the CFI runtime has no way to verify its signature and would fail.
443 * TCG is not considered a security-sensitive part of QEMU so this does not
444 * affect the impact of CFI in environment with high security requirements
446 static inline TranslationBlock
* QEMU_DISABLE_CFI
447 cpu_tb_exec(CPUState
*cpu
, TranslationBlock
*itb
, int *tb_exit
)
450 TranslationBlock
*last_tb
;
451 const void *tb_ptr
= itb
->tc
.ptr
;
453 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
| CPU_LOG_EXEC
)) {
454 log_cpu_exec(log_pc(cpu
, itb
), cpu
, itb
);
457 qemu_thread_jit_execute();
458 ret
= tcg_qemu_tb_exec(cpu_env(cpu
), tb_ptr
);
459 cpu
->neg
.can_do_io
= true;
460 qemu_plugin_disable_mem_helpers(cpu
);
462 * TODO: Delay swapping back to the read-write region of the TB
463 * until we actually need to modify the TB. The read-only copy,
464 * coming from the rx region, shares the same host TLB entry as
465 * the code that executed the exit_tb opcode that arrived here.
466 * If we insist on touching both the RX and the RW pages, we
467 * double the host TLB pressure.
469 last_tb
= tcg_splitwx_to_rw((void *)(ret
& ~TB_EXIT_MASK
));
470 *tb_exit
= ret
& TB_EXIT_MASK
;
472 trace_exec_tb_exit(last_tb
, *tb_exit
);
474 if (*tb_exit
> TB_EXIT_IDX1
) {
475 /* We didn't start executing this TB (eg because the instruction
476 * counter hit zero); we must restore the guest PC to the address
477 * of the start of the TB.
479 CPUClass
*cc
= cpu
->cc
;
480 const TCGCPUOps
*tcg_ops
= cc
->tcg_ops
;
482 if (tcg_ops
->synchronize_from_tb
) {
483 tcg_ops
->synchronize_from_tb(cpu
, last_tb
);
485 tcg_debug_assert(!(tb_cflags(last_tb
) & CF_PCREL
));
487 cc
->set_pc(cpu
, last_tb
->pc
);
489 if (qemu_loglevel_mask(CPU_LOG_EXEC
)) {
490 vaddr pc
= log_pc(cpu
, last_tb
);
491 if (qemu_log_in_addr_range(pc
)) {
492 qemu_log("Stopped execution of TB chain before %p [%016"
494 last_tb
->tc
.ptr
, pc
, lookup_symbol(pc
));
500 * If gdb single-step, and we haven't raised another exception,
501 * raise a debug exception. Single-step with another exception
502 * is handled in cpu_handle_exception.
504 if (unlikely(cpu
->singlestep_enabled
) && cpu
->exception_index
== -1) {
505 cpu
->exception_index
= EXCP_DEBUG
;
513 static void cpu_exec_enter(CPUState
*cpu
)
515 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
517 if (tcg_ops
->cpu_exec_enter
) {
518 tcg_ops
->cpu_exec_enter(cpu
);
522 static void cpu_exec_exit(CPUState
*cpu
)
524 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
526 if (tcg_ops
->cpu_exec_exit
) {
527 tcg_ops
->cpu_exec_exit(cpu
);
531 static void cpu_exec_longjmp_cleanup(CPUState
*cpu
)
533 /* Non-buggy compilers preserve this; assert the correct value. */
534 g_assert(cpu
== current_cpu
);
536 #ifdef CONFIG_USER_ONLY
537 clear_helper_retaddr();
538 if (have_mmap_lock()) {
543 * For softmmu, a tlb_fill fault during translation will land here,
544 * and we need to release any page locks held. In system mode we
545 * have one tcg_ctx per thread, so we know it was this cpu doing
548 * Alternative 1: Install a cleanup to be called via an exception
549 * handling safe longjmp. It seems plausible that all our hosts
550 * support such a thing. We'd have to properly register unwind info
551 * for the JIT for EH, rather that just for GDB.
553 * Alternative 2: Set and restore cpu->jmp_env in tb_gen_code to
554 * capture the cpu_loop_exit longjmp, perform the cleanup, and
555 * jump again to arrive here.
557 if (tcg_ctx
->gen_tb
) {
558 tb_unlock_pages(tcg_ctx
->gen_tb
);
559 tcg_ctx
->gen_tb
= NULL
;
565 assert_no_pages_locked();
568 void cpu_exec_step_atomic(CPUState
*cpu
)
570 CPUArchState
*env
= cpu_env(cpu
);
571 TranslationBlock
*tb
;
574 uint32_t flags
, cflags
;
577 if (sigsetjmp(cpu
->jmp_env
, 0) == 0) {
579 g_assert(cpu
== current_cpu
);
580 g_assert(!cpu
->running
);
583 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
585 cflags
= curr_cflags(cpu
);
586 /* Execute in a serial context. */
587 cflags
&= ~CF_PARALLEL
;
588 /* After 1 insn, return and release the exclusive lock. */
589 cflags
|= CF_NO_GOTO_TB
| CF_NO_GOTO_PTR
| 1;
591 * No need to check_for_breakpoints here.
592 * We only arrive in cpu_exec_step_atomic after beginning execution
593 * of an insn that includes an atomic operation we can't handle.
594 * Any breakpoint for this insn will have been recognized earlier.
597 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
600 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, cflags
);
605 /* execute the generated code */
606 trace_exec_tb(tb
, pc
);
607 cpu_tb_exec(cpu
, tb
, &tb_exit
);
610 cpu_exec_longjmp_cleanup(cpu
);
614 * As we start the exclusive region before codegen we must still
615 * be in the region if we longjump out of either the codegen or
618 g_assert(cpu_in_exclusive_context(cpu
));
619 cpu
->running
= false;
623 void tb_set_jmp_target(TranslationBlock
*tb
, int n
, uintptr_t addr
)
626 * Get the rx view of the structure, from which we find the
627 * executable code address, and tb_target_set_jmp_target can
628 * produce a pc-relative displacement to jmp_target_addr[n].
630 const TranslationBlock
*c_tb
= tcg_splitwx_to_rx(tb
);
631 uintptr_t offset
= tb
->jmp_insn_offset
[n
];
632 uintptr_t jmp_rx
= (uintptr_t)tb
->tc
.ptr
+ offset
;
633 uintptr_t jmp_rw
= jmp_rx
- tcg_splitwx_diff
;
635 tb
->jmp_target_addr
[n
] = addr
;
636 tb_target_set_jmp_target(c_tb
, n
, jmp_rx
, jmp_rw
);
639 static inline void tb_add_jump(TranslationBlock
*tb
, int n
,
640 TranslationBlock
*tb_next
)
644 qemu_thread_jit_write();
645 assert(n
< ARRAY_SIZE(tb
->jmp_list_next
));
646 qemu_spin_lock(&tb_next
->jmp_lock
);
648 /* make sure the destination TB is valid */
649 if (tb_next
->cflags
& CF_INVALID
) {
650 goto out_unlock_next
;
652 /* Atomically claim the jump destination slot only if it was NULL */
653 old
= qatomic_cmpxchg(&tb
->jmp_dest
[n
], (uintptr_t)NULL
,
656 goto out_unlock_next
;
659 /* patch the native jump address */
660 tb_set_jmp_target(tb
, n
, (uintptr_t)tb_next
->tc
.ptr
);
662 /* add in TB jmp list */
663 tb
->jmp_list_next
[n
] = tb_next
->jmp_list_head
;
664 tb_next
->jmp_list_head
= (uintptr_t)tb
| n
;
666 qemu_spin_unlock(&tb_next
->jmp_lock
);
668 qemu_log_mask(CPU_LOG_EXEC
, "Linking TBs %p index %d -> %p\n",
669 tb
->tc
.ptr
, n
, tb_next
->tc
.ptr
);
673 qemu_spin_unlock(&tb_next
->jmp_lock
);
677 static inline bool cpu_handle_halt(CPUState
*cpu
)
679 #ifndef CONFIG_USER_ONLY
681 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
682 bool leave_halt
= tcg_ops
->cpu_exec_halt(cpu
);
690 #endif /* !CONFIG_USER_ONLY */
695 static inline void cpu_handle_debug_exception(CPUState
*cpu
)
697 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
700 if (!cpu
->watchpoint_hit
) {
701 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
702 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
706 if (tcg_ops
->debug_excp_handler
) {
707 tcg_ops
->debug_excp_handler(cpu
);
711 static inline bool cpu_handle_exception(CPUState
*cpu
, int *ret
)
713 if (cpu
->exception_index
< 0) {
714 #ifndef CONFIG_USER_ONLY
715 if (replay_has_exception()
716 && cpu
->neg
.icount_decr
.u16
.low
+ cpu
->icount_extra
== 0) {
717 /* Execute just one insn to trigger exception pending in the log */
718 cpu
->cflags_next_tb
= (curr_cflags(cpu
) & ~CF_USE_ICOUNT
)
725 if (cpu
->exception_index
>= EXCP_INTERRUPT
) {
726 /* exit request from the cpu execution loop */
727 *ret
= cpu
->exception_index
;
728 if (*ret
== EXCP_DEBUG
) {
729 cpu_handle_debug_exception(cpu
);
731 cpu
->exception_index
= -1;
735 #if defined(CONFIG_USER_ONLY)
737 * If user mode only, we simulate a fake exception which will be
738 * handled outside the cpu execution loop.
740 #if defined(TARGET_I386)
741 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
742 tcg_ops
->fake_user_interrupt(cpu
);
743 #endif /* TARGET_I386 */
744 *ret
= cpu
->exception_index
;
745 cpu
->exception_index
= -1;
748 if (replay_exception()) {
749 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
752 tcg_ops
->do_interrupt(cpu
);
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
;
776 static inline bool icount_exit_request(CPUState
*cpu
)
778 if (!icount_enabled()) {
781 if (cpu
->cflags_next_tb
!= -1 && !(cpu
->cflags_next_tb
& CF_USE_ICOUNT
)) {
784 return cpu
->neg
.icount_decr
.u16
.low
+ cpu
->icount_extra
== 0;
787 static inline bool cpu_handle_interrupt(CPUState
*cpu
,
788 TranslationBlock
**last_tb
)
791 * If we have requested custom cflags with CF_NOIRQ we should
792 * skip checking here. Any pending interrupts will get picked up
793 * by the next TB we execute under normal cflags.
795 if (cpu
->cflags_next_tb
!= -1 && cpu
->cflags_next_tb
& CF_NOIRQ
) {
799 /* Clear the interrupt flag now since we're processing
800 * cpu->interrupt_request and cpu->exit_request.
801 * Ensure zeroing happens before reading cpu->exit_request or
802 * cpu->interrupt_request (see also smp_wmb in cpu_exit())
804 qatomic_set_mb(&cpu
->neg
.icount_decr
.u16
.high
, 0);
806 if (unlikely(qatomic_read(&cpu
->interrupt_request
))) {
807 int interrupt_request
;
809 interrupt_request
= cpu
->interrupt_request
;
810 if (unlikely(cpu
->singlestep_enabled
& SSTEP_NOIRQ
)) {
811 /* Mask out external interrupts for this step. */
812 interrupt_request
&= ~CPU_INTERRUPT_SSTEP_MASK
;
814 if (interrupt_request
& CPU_INTERRUPT_DEBUG
) {
815 cpu
->interrupt_request
&= ~CPU_INTERRUPT_DEBUG
;
816 cpu
->exception_index
= EXCP_DEBUG
;
820 #if !defined(CONFIG_USER_ONLY)
821 if (replay_mode
== REPLAY_MODE_PLAY
&& !replay_has_interrupt()) {
823 } else if (interrupt_request
& CPU_INTERRUPT_HALT
) {
825 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HALT
;
827 cpu
->exception_index
= EXCP_HLT
;
831 #if defined(TARGET_I386)
832 else if (interrupt_request
& CPU_INTERRUPT_INIT
) {
833 X86CPU
*x86_cpu
= X86_CPU(cpu
);
834 CPUArchState
*env
= &x86_cpu
->env
;
836 cpu_svm_check_intercept_param(env
, SVM_EXIT_INIT
, 0, 0);
837 do_cpu_init(x86_cpu
);
838 cpu
->exception_index
= EXCP_HALTED
;
843 else if (interrupt_request
& CPU_INTERRUPT_RESET
) {
849 #endif /* !TARGET_I386 */
850 /* The target hook has 3 exit conditions:
851 False when the interrupt isn't processed,
852 True when it is, and we should restart on a new TB,
853 and via longjmp via cpu_loop_exit. */
855 const TCGCPUOps
*tcg_ops
= cpu
->cc
->tcg_ops
;
857 if (tcg_ops
->cpu_exec_interrupt(cpu
, interrupt_request
)) {
858 if (!tcg_ops
->need_replay_interrupt
||
859 tcg_ops
->need_replay_interrupt(interrupt_request
)) {
863 * After processing the interrupt, ensure an EXCP_DEBUG is
864 * raised when single-stepping so that GDB doesn't miss the
867 if (unlikely(cpu
->singlestep_enabled
)) {
868 cpu
->exception_index
= EXCP_DEBUG
;
872 cpu
->exception_index
= -1;
875 /* The target hook may have updated the 'cpu->interrupt_request';
876 * reload the 'interrupt_request' value */
877 interrupt_request
= cpu
->interrupt_request
;
879 #endif /* !CONFIG_USER_ONLY */
880 if (interrupt_request
& CPU_INTERRUPT_EXITTB
) {
881 cpu
->interrupt_request
&= ~CPU_INTERRUPT_EXITTB
;
882 /* ensure that no TB jump will be modified as
883 the program flow was changed */
887 /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
891 /* Finally, check if we need to exit to the main loop. */
892 if (unlikely(qatomic_read(&cpu
->exit_request
)) || icount_exit_request(cpu
)) {
893 qatomic_set(&cpu
->exit_request
, 0);
894 if (cpu
->exception_index
== -1) {
895 cpu
->exception_index
= EXCP_INTERRUPT
;
903 static inline void cpu_loop_exec_tb(CPUState
*cpu
, TranslationBlock
*tb
,
904 vaddr pc
, TranslationBlock
**last_tb
,
907 trace_exec_tb(tb
, pc
);
908 tb
= cpu_tb_exec(cpu
, tb
, tb_exit
);
909 if (*tb_exit
!= TB_EXIT_REQUESTED
) {
915 if (cpu_loop_exit_requested(cpu
)) {
916 /* Something asked us to stop executing chained TBs; just
917 * continue round the main loop. Whatever requested the exit
918 * will also have set something else (eg exit_request or
919 * interrupt_request) which will be handled by
920 * cpu_handle_interrupt. cpu_handle_interrupt will also
921 * clear cpu->icount_decr.u16.high.
926 /* Instruction counter expired. */
927 assert(icount_enabled());
928 #ifndef CONFIG_USER_ONLY
929 /* Ensure global icount has gone forward */
931 /* Refill decrementer and continue execution. */
932 int32_t insns_left
= MIN(0xffff, cpu
->icount_budget
);
933 cpu
->neg
.icount_decr
.u16
.low
= insns_left
;
934 cpu
->icount_extra
= cpu
->icount_budget
- insns_left
;
937 * If the next tb has more instructions than we have left to
938 * execute we need to ensure we find/generate a TB with exactly
939 * insns_left instructions in it.
941 if (insns_left
> 0 && insns_left
< tb
->icount
) {
942 assert(insns_left
<= CF_COUNT_MASK
);
943 assert(cpu
->icount_extra
== 0);
944 cpu
->cflags_next_tb
= (tb
->cflags
& ~CF_COUNT_MASK
) | insns_left
;
949 /* main execution loop */
951 static int __attribute__((noinline
))
952 cpu_exec_loop(CPUState
*cpu
, SyncClocks
*sc
)
956 /* if an exception is pending, we execute it here */
957 while (!cpu_handle_exception(cpu
, &ret
)) {
958 TranslationBlock
*last_tb
= NULL
;
961 while (!cpu_handle_interrupt(cpu
, &last_tb
)) {
962 TranslationBlock
*tb
;
965 uint32_t flags
, cflags
;
967 cpu_get_tb_cpu_state(cpu_env(cpu
), &pc
, &cs_base
, &flags
);
970 * When requested, use an exact setting for cflags for the next
971 * execution. This is used for icount, precise smc, and stop-
972 * after-access watchpoints. Since this request should never
973 * have CF_INVALID set, -1 is a convenient invalid value that
974 * does not require tcg headers for cpu_common_reset.
976 cflags
= cpu
->cflags_next_tb
;
978 cflags
= curr_cflags(cpu
);
980 cpu
->cflags_next_tb
= -1;
983 if (check_for_breakpoints(cpu
, pc
, &cflags
)) {
987 tb
= tb_lookup(cpu
, pc
, cs_base
, flags
, cflags
);
993 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, cflags
);
997 * We add the TB in the virtual pc hash table
998 * for the fast lookup
1000 h
= tb_jmp_cache_hash_func(pc
);
1001 jc
= cpu
->tb_jmp_cache
;
1002 jc
->array
[h
].pc
= pc
;
1003 qatomic_set(&jc
->array
[h
].tb
, tb
);
1006 #ifndef CONFIG_USER_ONLY
1008 * We don't take care of direct jumps when address mapping
1009 * changes in system emulation. So it's not safe to make a
1010 * direct jump to a TB spanning two pages because the mapping
1011 * for the second page can change.
1013 if (tb_page_addr1(tb
) != -1) {
1017 /* See if we can patch the calling TB. */
1019 tb_add_jump(last_tb
, tb_exit
, tb
);
1022 cpu_loop_exec_tb(cpu
, tb
, pc
, &last_tb
, &tb_exit
);
1024 /* Try to align the host and virtual clocks
1025 if the guest is in advance */
1026 align_clocks(sc
, cpu
);
1032 static int cpu_exec_setjmp(CPUState
*cpu
, SyncClocks
*sc
)
1034 /* Prepare setjmp context for exception handling. */
1035 if (unlikely(sigsetjmp(cpu
->jmp_env
, 0) != 0)) {
1036 cpu_exec_longjmp_cleanup(cpu
);
1039 return cpu_exec_loop(cpu
, sc
);
1042 int cpu_exec(CPUState
*cpu
)
1045 SyncClocks sc
= { 0 };
1047 /* replay_interrupt may need current_cpu */
1050 if (cpu_handle_halt(cpu
)) {
1054 RCU_READ_LOCK_GUARD();
1055 cpu_exec_enter(cpu
);
1058 * Calculate difference between guest clock and host clock.
1059 * This delay includes the delay of the last cycle, so
1060 * what we have to do is sleep until it is 0. As for the
1061 * advance/delay we gain here, we try to fix it next time.
1063 init_delay_params(&sc
, cpu
);
1065 ret
= cpu_exec_setjmp(cpu
, &sc
);
1071 bool tcg_exec_realizefn(CPUState
*cpu
, Error
**errp
)
1073 static bool tcg_target_initialized
;
1075 if (!tcg_target_initialized
) {
1076 /* Check mandatory TCGCPUOps handlers */
1077 #ifndef CONFIG_USER_ONLY
1078 assert(cpu
->cc
->tcg_ops
->cpu_exec_halt
);
1079 assert(cpu
->cc
->tcg_ops
->cpu_exec_interrupt
);
1080 #endif /* !CONFIG_USER_ONLY */
1081 cpu
->cc
->tcg_ops
->initialize();
1082 tcg_target_initialized
= true;
1085 cpu
->tb_jmp_cache
= g_new0(CPUJumpCache
, 1);
1087 #ifndef CONFIG_USER_ONLY
1088 tcg_iommu_init_notifier_list(cpu
);
1089 #endif /* !CONFIG_USER_ONLY */
1090 /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */
1095 /* undo the initializations in reverse order */
1096 void tcg_exec_unrealizefn(CPUState
*cpu
)
1098 #ifndef CONFIG_USER_ONLY
1099 tcg_iommu_free_notifier_list(cpu
);
1100 #endif /* !CONFIG_USER_ONLY */
1103 g_free_rcu(cpu
->tb_jmp_cache
, rcu
);