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 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/>.
19 #include "qemu/osdep.h"
22 #include "disas/disas.h"
24 #include "qemu/atomic.h"
25 #include "sysemu/qtest.h"
26 #include "qemu/timer.h"
27 #include "exec/address-spaces.h"
29 #include "exec/tb-hash.h"
30 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
31 #include "hw/i386/apic.h"
33 #include "sysemu/replay.h"
35 /* -icount align implementation. */
37 typedef struct SyncClocks
{
39 int64_t last_cpu_icount
;
40 int64_t realtime_clock
;
43 #if !defined(CONFIG_USER_ONLY)
44 /* Allow the guest to have a max 3ms advance.
45 * The difference between the 2 clocks could therefore
48 #define VM_CLOCK_ADVANCE 3000000
49 #define THRESHOLD_REDUCE 1.5
50 #define MAX_DELAY_PRINT_RATE 2000000000LL
51 #define MAX_NB_PRINTS 100
53 static void align_clocks(SyncClocks
*sc
, const CPUState
*cpu
)
57 if (!icount_align_option
) {
61 cpu_icount
= cpu
->icount_extra
+ cpu
->icount_decr
.u16
.low
;
62 sc
->diff_clk
+= cpu_icount_to_ns(sc
->last_cpu_icount
- cpu_icount
);
63 sc
->last_cpu_icount
= cpu_icount
;
65 if (sc
->diff_clk
> VM_CLOCK_ADVANCE
) {
67 struct timespec sleep_delay
, rem_delay
;
68 sleep_delay
.tv_sec
= sc
->diff_clk
/ 1000000000LL;
69 sleep_delay
.tv_nsec
= sc
->diff_clk
% 1000000000LL;
70 if (nanosleep(&sleep_delay
, &rem_delay
) < 0) {
71 sc
->diff_clk
= rem_delay
.tv_sec
* 1000000000LL + rem_delay
.tv_nsec
;
76 Sleep(sc
->diff_clk
/ SCALE_MS
);
82 static void print_delay(const SyncClocks
*sc
)
84 static float threshold_delay
;
85 static int64_t last_realtime_clock
;
88 if (icount_align_option
&&
89 sc
->realtime_clock
- last_realtime_clock
>= MAX_DELAY_PRINT_RATE
&&
90 nb_prints
< MAX_NB_PRINTS
) {
91 if ((-sc
->diff_clk
/ (float)1000000000LL > threshold_delay
) ||
92 (-sc
->diff_clk
/ (float)1000000000LL <
93 (threshold_delay
- THRESHOLD_REDUCE
))) {
94 threshold_delay
= (-sc
->diff_clk
/ 1000000000LL) + 1;
95 printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
99 last_realtime_clock
= sc
->realtime_clock
;
104 static void init_delay_params(SyncClocks
*sc
,
107 if (!icount_align_option
) {
110 sc
->realtime_clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
);
111 sc
->diff_clk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - sc
->realtime_clock
;
112 sc
->last_cpu_icount
= cpu
->icount_extra
+ cpu
->icount_decr
.u16
.low
;
113 if (sc
->diff_clk
< max_delay
) {
114 max_delay
= sc
->diff_clk
;
116 if (sc
->diff_clk
> max_advance
) {
117 max_advance
= sc
->diff_clk
;
120 /* Print every 2s max if the guest is late. We limit the number
121 of printed messages to NB_PRINT_MAX(currently 100) */
125 static void align_clocks(SyncClocks
*sc
, const CPUState
*cpu
)
129 static void init_delay_params(SyncClocks
*sc
, const CPUState
*cpu
)
132 #endif /* CONFIG USER ONLY */
134 /* Execute a TB, and fix up the CPU state afterwards if necessary */
135 static inline tcg_target_ulong
cpu_tb_exec(CPUState
*cpu
, uint8_t *tb_ptr
)
137 CPUArchState
*env
= cpu
->env_ptr
;
140 #if defined(DEBUG_DISAS)
141 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
)) {
142 #if defined(TARGET_I386)
143 log_cpu_state(cpu
, CPU_DUMP_CCOP
);
144 #elif defined(TARGET_M68K)
145 /* ??? Should not modify env state for dumping. */
146 cpu_m68k_flush_flags(env
, env
->cc_op
);
147 env
->cc_op
= CC_OP_FLAGS
;
148 env
->sr
= (env
->sr
& 0xffe0) | env
->cc_dest
| (env
->cc_x
<< 4);
149 log_cpu_state(cpu
, 0);
151 log_cpu_state(cpu
, 0);
154 #endif /* DEBUG_DISAS */
156 cpu
->can_do_io
= !use_icount
;
157 next_tb
= tcg_qemu_tb_exec(env
, tb_ptr
);
159 trace_exec_tb_exit((void *) (next_tb
& ~TB_EXIT_MASK
),
160 next_tb
& TB_EXIT_MASK
);
162 if ((next_tb
& TB_EXIT_MASK
) > TB_EXIT_IDX1
) {
163 /* We didn't start executing this TB (eg because the instruction
164 * counter hit zero); we must restore the guest PC to the address
165 * of the start of the TB.
167 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
168 TranslationBlock
*tb
= (TranslationBlock
*)(next_tb
& ~TB_EXIT_MASK
);
169 if (cc
->synchronize_from_tb
) {
170 cc
->synchronize_from_tb(cpu
, tb
);
173 cc
->set_pc(cpu
, tb
->pc
);
176 if ((next_tb
& TB_EXIT_MASK
) == TB_EXIT_REQUESTED
) {
177 /* We were asked to stop executing TBs (probably a pending
178 * interrupt. We've now stopped, so clear the flag.
180 cpu
->tcg_exit_req
= 0;
185 /* Execute the code without caching the generated code. An interpreter
186 could be used if available. */
187 static void cpu_exec_nocache(CPUState
*cpu
, int max_cycles
,
188 TranslationBlock
*orig_tb
, bool ignore_icount
)
190 TranslationBlock
*tb
;
192 /* Should never happen.
193 We only end up here when an existing TB is too long. */
194 if (max_cycles
> CF_COUNT_MASK
)
195 max_cycles
= CF_COUNT_MASK
;
197 tb
= tb_gen_code(cpu
, orig_tb
->pc
, orig_tb
->cs_base
, orig_tb
->flags
,
198 max_cycles
| CF_NOCACHE
199 | (ignore_icount
? CF_IGNORE_ICOUNT
: 0));
200 tb
->orig_tb
= tcg_ctx
.tb_ctx
.tb_invalidated_flag
? NULL
: orig_tb
;
201 cpu
->current_tb
= tb
;
202 /* execute the generated code */
203 trace_exec_tb_nocache(tb
, tb
->pc
);
204 cpu_tb_exec(cpu
, tb
->tc_ptr
);
205 cpu
->current_tb
= NULL
;
206 tb_phys_invalidate(tb
, -1);
210 static TranslationBlock
*tb_find_physical(CPUState
*cpu
,
212 target_ulong cs_base
,
215 CPUArchState
*env
= (CPUArchState
*)cpu
->env_ptr
;
216 TranslationBlock
*tb
, **ptb1
;
218 tb_page_addr_t phys_pc
, phys_page1
;
219 target_ulong virt_page2
;
221 tcg_ctx
.tb_ctx
.tb_invalidated_flag
= 0;
223 /* find translated block using physical mappings */
224 phys_pc
= get_page_addr_code(env
, pc
);
225 phys_page1
= phys_pc
& TARGET_PAGE_MASK
;
226 h
= tb_phys_hash_func(phys_pc
);
227 ptb1
= &tcg_ctx
.tb_ctx
.tb_phys_hash
[h
];
234 tb
->page_addr
[0] == phys_page1
&&
235 tb
->cs_base
== cs_base
&&
236 tb
->flags
== flags
) {
237 /* check next page if needed */
238 if (tb
->page_addr
[1] != -1) {
239 tb_page_addr_t phys_page2
;
241 virt_page2
= (pc
& TARGET_PAGE_MASK
) +
243 phys_page2
= get_page_addr_code(env
, virt_page2
);
244 if (tb
->page_addr
[1] == phys_page2
) {
251 ptb1
= &tb
->phys_hash_next
;
254 /* Move the TB to the head of the list */
255 *ptb1
= tb
->phys_hash_next
;
256 tb
->phys_hash_next
= tcg_ctx
.tb_ctx
.tb_phys_hash
[h
];
257 tcg_ctx
.tb_ctx
.tb_phys_hash
[h
] = tb
;
261 static TranslationBlock
*tb_find_slow(CPUState
*cpu
,
263 target_ulong cs_base
,
266 TranslationBlock
*tb
;
268 tb
= tb_find_physical(cpu
, pc
, cs_base
, flags
);
273 #ifdef CONFIG_USER_ONLY
274 /* mmap_lock is needed by tb_gen_code, and mmap_lock must be
275 * taken outside tb_lock. Since we're momentarily dropping
276 * tb_lock, there's a chance that our desired tb has been
282 tb
= tb_find_physical(cpu
, pc
, cs_base
, flags
);
289 /* if no translated code available, then translate it now */
290 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, 0);
292 #ifdef CONFIG_USER_ONLY
297 /* we add the TB in the virtual pc hash table */
298 cpu
->tb_jmp_cache
[tb_jmp_cache_hash_func(pc
)] = tb
;
302 static inline TranslationBlock
*tb_find_fast(CPUState
*cpu
)
304 CPUArchState
*env
= (CPUArchState
*)cpu
->env_ptr
;
305 TranslationBlock
*tb
;
306 target_ulong cs_base
, pc
;
309 /* we record a subset of the CPU state. It will
310 always be the same before a given translated block
312 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
313 tb
= cpu
->tb_jmp_cache
[tb_jmp_cache_hash_func(pc
)];
314 if (unlikely(!tb
|| tb
->pc
!= pc
|| tb
->cs_base
!= cs_base
||
315 tb
->flags
!= flags
)) {
316 tb
= tb_find_slow(cpu
, pc
, cs_base
, flags
);
321 static void cpu_handle_debug_exception(CPUState
*cpu
)
323 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
326 if (!cpu
->watchpoint_hit
) {
327 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
328 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
332 cc
->debug_excp_handler(cpu
);
335 /* main execution loop */
337 int cpu_exec(CPUState
*cpu
)
339 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
341 X86CPU
*x86_cpu
= X86_CPU(cpu
);
342 CPUArchState
*env
= &x86_cpu
->env
;
344 int ret
, interrupt_request
;
345 TranslationBlock
*tb
;
350 /* replay_interrupt may need current_cpu */
354 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
355 if ((cpu
->interrupt_request
& CPU_INTERRUPT_POLL
)
356 && replay_interrupt()) {
357 apic_poll_irq(x86_cpu
->apic_state
);
358 cpu_reset_interrupt(cpu
, CPU_INTERRUPT_POLL
);
361 if (!cpu_has_work(cpu
)) {
369 atomic_mb_set(&tcg_current_cpu
, cpu
);
372 if (unlikely(atomic_mb_read(&exit_request
))) {
373 cpu
->exit_request
= 1;
376 cc
->cpu_exec_enter(cpu
);
378 /* Calculate difference between guest clock and host clock.
379 * This delay includes the delay of the last cycle, so
380 * what we have to do is sleep until it is 0. As for the
381 * advance/delay we gain here, we try to fix it next time.
383 init_delay_params(&sc
, cpu
);
385 /* prepare setjmp context for exception handling */
387 if (sigsetjmp(cpu
->jmp_env
, 0) == 0) {
388 /* if an exception is pending, we execute it here */
389 if (cpu
->exception_index
>= 0) {
390 if (cpu
->exception_index
>= EXCP_INTERRUPT
) {
391 /* exit request from the cpu execution loop */
392 ret
= cpu
->exception_index
;
393 if (ret
== EXCP_DEBUG
) {
394 cpu_handle_debug_exception(cpu
);
396 cpu
->exception_index
= -1;
399 #if defined(CONFIG_USER_ONLY)
400 /* if user mode only, we simulate a fake exception
401 which will be handled outside the cpu execution
403 #if defined(TARGET_I386)
404 cc
->do_interrupt(cpu
);
406 ret
= cpu
->exception_index
;
407 cpu
->exception_index
= -1;
410 if (replay_exception()) {
411 cc
->do_interrupt(cpu
);
412 cpu
->exception_index
= -1;
413 } else if (!replay_has_interrupt()) {
414 /* give a chance to iothread in replay mode */
415 ret
= EXCP_INTERRUPT
;
420 } else if (replay_has_exception()
421 && cpu
->icount_decr
.u16
.low
+ cpu
->icount_extra
== 0) {
422 /* try to cause an exception pending in the log */
423 cpu_exec_nocache(cpu
, 1, tb_find_fast(cpu
), true);
428 next_tb
= 0; /* force lookup of first TB */
430 interrupt_request
= cpu
->interrupt_request
;
431 if (unlikely(interrupt_request
)) {
432 if (unlikely(cpu
->singlestep_enabled
& SSTEP_NOIRQ
)) {
433 /* Mask out external interrupts for this step. */
434 interrupt_request
&= ~CPU_INTERRUPT_SSTEP_MASK
;
436 if (interrupt_request
& CPU_INTERRUPT_DEBUG
) {
437 cpu
->interrupt_request
&= ~CPU_INTERRUPT_DEBUG
;
438 cpu
->exception_index
= EXCP_DEBUG
;
441 if (replay_mode
== REPLAY_MODE_PLAY
442 && !replay_has_interrupt()) {
444 } else if (interrupt_request
& CPU_INTERRUPT_HALT
) {
446 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HALT
;
448 cpu
->exception_index
= EXCP_HLT
;
451 #if defined(TARGET_I386)
452 else if (interrupt_request
& CPU_INTERRUPT_INIT
) {
454 cpu_svm_check_intercept_param(env
, SVM_EXIT_INIT
, 0);
455 do_cpu_init(x86_cpu
);
456 cpu
->exception_index
= EXCP_HALTED
;
460 else if (interrupt_request
& CPU_INTERRUPT_RESET
) {
466 /* The target hook has 3 exit conditions:
467 False when the interrupt isn't processed,
468 True when it is, and we should restart on a new TB,
469 and via longjmp via cpu_loop_exit. */
472 if (cc
->cpu_exec_interrupt(cpu
, interrupt_request
)) {
476 /* Don't use the cached interrupt_request value,
477 do_interrupt may have updated the EXITTB flag. */
478 if (cpu
->interrupt_request
& CPU_INTERRUPT_EXITTB
) {
479 cpu
->interrupt_request
&= ~CPU_INTERRUPT_EXITTB
;
480 /* ensure that no TB jump will be modified as
481 the program flow was changed */
485 if (unlikely(cpu
->exit_request
486 || replay_has_interrupt())) {
487 cpu
->exit_request
= 0;
488 cpu
->exception_index
= EXCP_INTERRUPT
;
492 tb
= tb_find_fast(cpu
);
493 /* Note: we do it here to avoid a gcc bug on Mac OS X when
494 doing it in tb_find_slow */
495 if (tcg_ctx
.tb_ctx
.tb_invalidated_flag
) {
496 /* as some TB could have been invalidated because
497 of memory exceptions while generating the code, we
498 must recompute the hash index here */
500 tcg_ctx
.tb_ctx
.tb_invalidated_flag
= 0;
502 if (qemu_loglevel_mask(CPU_LOG_EXEC
)) {
503 qemu_log("Trace %p [" TARGET_FMT_lx
"] %s\n",
504 tb
->tc_ptr
, tb
->pc
, lookup_symbol(tb
->pc
));
506 /* see if we can patch the calling TB. When the TB
507 spans two pages, we cannot safely do a direct
509 if (next_tb
!= 0 && tb
->page_addr
[1] == -1
510 && !qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN
)) {
511 tb_add_jump((TranslationBlock
*)(next_tb
& ~TB_EXIT_MASK
),
512 next_tb
& TB_EXIT_MASK
, tb
);
515 if (likely(!cpu
->exit_request
)) {
516 trace_exec_tb(tb
, tb
->pc
);
518 /* execute the generated code */
519 cpu
->current_tb
= tb
;
520 next_tb
= cpu_tb_exec(cpu
, tc_ptr
);
521 cpu
->current_tb
= NULL
;
522 switch (next_tb
& TB_EXIT_MASK
) {
523 case TB_EXIT_REQUESTED
:
524 /* Something asked us to stop executing
525 * chained TBs; just continue round the main
526 * loop. Whatever requested the exit will also
527 * have set something else (eg exit_request or
528 * interrupt_request) which we will handle
529 * next time around the loop. But we need to
530 * ensure the tcg_exit_req read in generated code
531 * comes before the next read of cpu->exit_request
532 * or cpu->interrupt_request.
537 case TB_EXIT_ICOUNT_EXPIRED
:
539 /* Instruction counter expired. */
540 int insns_left
= cpu
->icount_decr
.u32
;
541 if (cpu
->icount_extra
&& insns_left
>= 0) {
542 /* Refill decrementer and continue execution. */
543 cpu
->icount_extra
+= insns_left
;
544 insns_left
= MIN(0xffff, cpu
->icount_extra
);
545 cpu
->icount_extra
-= insns_left
;
546 cpu
->icount_decr
.u16
.low
= insns_left
;
548 if (insns_left
> 0) {
549 /* Execute remaining instructions. */
550 tb
= (TranslationBlock
*)(next_tb
& ~TB_EXIT_MASK
);
551 cpu_exec_nocache(cpu
, insns_left
, tb
, false);
552 align_clocks(&sc
, cpu
);
554 cpu
->exception_index
= EXCP_INTERRUPT
;
564 /* Try to align the host and virtual clocks
565 if the guest is in advance */
566 align_clocks(&sc
, cpu
);
567 /* reset soft MMU for next block (it can currently
568 only be set by a memory fault) */
571 #if defined(__clang__) || !QEMU_GNUC_PREREQ(4, 6)
572 /* Some compilers wrongly smash all local variables after
573 * siglongjmp. There were bug reports for gcc 4.5.0 and clang.
574 * Reload essential local variables here for those compilers.
575 * Newer versions of gcc would complain about this code (-Wclobbered). */
577 cc
= CPU_GET_CLASS(cpu
);
579 x86_cpu
= X86_CPU(cpu
);
582 #else /* buggy compiler */
583 /* Assert that the compiler does not smash local variables. */
584 g_assert(cpu
== current_cpu
);
585 g_assert(cc
== CPU_GET_CLASS(cpu
));
587 g_assert(x86_cpu
== X86_CPU(cpu
));
588 g_assert(env
== &x86_cpu
->env
);
590 #endif /* buggy compiler */
596 cc
->cpu_exec_exit(cpu
);
599 /* fail safe : never use current_cpu outside cpu_exec() */
602 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
603 atomic_set(&tcg_current_cpu
, NULL
);