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/>.
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"
32 /* On w64, sigsetjmp is implemented by _setjmp which needs a second parameter.
33 * If this parameter is NULL, longjump does no stack unwinding.
34 * That is what we need for QEMU. Passing the value of register rsp (default)
35 * lets longjmp try a stack unwinding which will crash with generated code. */
36 #define sigsetjmp(env, savesigs) _setjmp(env, NULL)
39 /* -icount align implementation. */
41 typedef struct SyncClocks
{
43 int64_t last_cpu_icount
;
44 int64_t realtime_clock
;
47 #if !defined(CONFIG_USER_ONLY)
48 /* Allow the guest to have a max 3ms advance.
49 * The difference between the 2 clocks could therefore
52 #define VM_CLOCK_ADVANCE 3000000
53 #define THRESHOLD_REDUCE 1.5
54 #define MAX_DELAY_PRINT_RATE 2000000000LL
55 #define MAX_NB_PRINTS 100
57 static void align_clocks(SyncClocks
*sc
, const CPUState
*cpu
)
61 if (!icount_align_option
) {
65 cpu_icount
= cpu
->icount_extra
+ cpu
->icount_decr
.u16
.low
;
66 sc
->diff_clk
+= cpu_icount_to_ns(sc
->last_cpu_icount
- cpu_icount
);
67 sc
->last_cpu_icount
= cpu_icount
;
69 if (sc
->diff_clk
> VM_CLOCK_ADVANCE
) {
71 struct timespec sleep_delay
, rem_delay
;
72 sleep_delay
.tv_sec
= sc
->diff_clk
/ 1000000000LL;
73 sleep_delay
.tv_nsec
= sc
->diff_clk
% 1000000000LL;
74 if (nanosleep(&sleep_delay
, &rem_delay
) < 0) {
75 sc
->diff_clk
= rem_delay
.tv_sec
* 1000000000LL + rem_delay
.tv_nsec
;
80 Sleep(sc
->diff_clk
/ SCALE_MS
);
86 static void print_delay(const SyncClocks
*sc
)
88 static float threshold_delay
;
89 static int64_t last_realtime_clock
;
92 if (icount_align_option
&&
93 sc
->realtime_clock
- last_realtime_clock
>= MAX_DELAY_PRINT_RATE
&&
94 nb_prints
< MAX_NB_PRINTS
) {
95 if ((-sc
->diff_clk
/ (float)1000000000LL > threshold_delay
) ||
96 (-sc
->diff_clk
/ (float)1000000000LL <
97 (threshold_delay
- THRESHOLD_REDUCE
))) {
98 threshold_delay
= (-sc
->diff_clk
/ 1000000000LL) + 1;
99 printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
103 last_realtime_clock
= sc
->realtime_clock
;
108 static void init_delay_params(SyncClocks
*sc
,
111 if (!icount_align_option
) {
114 sc
->realtime_clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
);
115 sc
->diff_clk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - sc
->realtime_clock
;
116 sc
->last_cpu_icount
= cpu
->icount_extra
+ cpu
->icount_decr
.u16
.low
;
117 if (sc
->diff_clk
< max_delay
) {
118 max_delay
= sc
->diff_clk
;
120 if (sc
->diff_clk
> max_advance
) {
121 max_advance
= sc
->diff_clk
;
124 /* Print every 2s max if the guest is late. We limit the number
125 of printed messages to NB_PRINT_MAX(currently 100) */
129 static void align_clocks(SyncClocks
*sc
, const CPUState
*cpu
)
133 static void init_delay_params(SyncClocks
*sc
, const CPUState
*cpu
)
136 #endif /* CONFIG USER ONLY */
138 /* Execute a TB, and fix up the CPU state afterwards if necessary */
139 static inline tcg_target_ulong
cpu_tb_exec(CPUState
*cpu
, uint8_t *tb_ptr
)
141 CPUArchState
*env
= cpu
->env_ptr
;
144 #if defined(DEBUG_DISAS)
145 if (qemu_loglevel_mask(CPU_LOG_TB_CPU
)) {
146 #if defined(TARGET_I386)
147 log_cpu_state(cpu
, CPU_DUMP_CCOP
);
148 #elif defined(TARGET_M68K)
149 /* ??? Should not modify env state for dumping. */
150 cpu_m68k_flush_flags(env
, env
->cc_op
);
151 env
->cc_op
= CC_OP_FLAGS
;
152 env
->sr
= (env
->sr
& 0xffe0) | env
->cc_dest
| (env
->cc_x
<< 4);
153 log_cpu_state(cpu
, 0);
155 log_cpu_state(cpu
, 0);
158 #endif /* DEBUG_DISAS */
160 cpu
->can_do_io
= !use_icount
;
161 next_tb
= tcg_qemu_tb_exec(env
, tb_ptr
);
163 trace_exec_tb_exit((void *) (next_tb
& ~TB_EXIT_MASK
),
164 next_tb
& TB_EXIT_MASK
);
166 if ((next_tb
& TB_EXIT_MASK
) > TB_EXIT_IDX1
) {
167 /* We didn't start executing this TB (eg because the instruction
168 * counter hit zero); we must restore the guest PC to the address
169 * of the start of the TB.
171 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
172 TranslationBlock
*tb
= (TranslationBlock
*)(next_tb
& ~TB_EXIT_MASK
);
173 if (cc
->synchronize_from_tb
) {
174 cc
->synchronize_from_tb(cpu
, tb
);
177 cc
->set_pc(cpu
, tb
->pc
);
180 if ((next_tb
& TB_EXIT_MASK
) == TB_EXIT_REQUESTED
) {
181 /* We were asked to stop executing TBs (probably a pending
182 * interrupt. We've now stopped, so clear the flag.
184 cpu
->tcg_exit_req
= 0;
189 /* Execute the code without caching the generated code. An interpreter
190 could be used if available. */
191 static void cpu_exec_nocache(CPUState
*cpu
, int max_cycles
,
192 TranslationBlock
*orig_tb
)
194 TranslationBlock
*tb
;
196 /* Should never happen.
197 We only end up here when an existing TB is too long. */
198 if (max_cycles
> CF_COUNT_MASK
)
199 max_cycles
= CF_COUNT_MASK
;
201 tb
= tb_gen_code(cpu
, orig_tb
->pc
, orig_tb
->cs_base
, orig_tb
->flags
,
202 max_cycles
| CF_NOCACHE
);
203 tb
->orig_tb
= tcg_ctx
.tb_ctx
.tb_invalidated_flag
? NULL
: orig_tb
;
204 cpu
->current_tb
= tb
;
205 /* execute the generated code */
206 trace_exec_tb_nocache(tb
, tb
->pc
);
207 cpu_tb_exec(cpu
, tb
->tc_ptr
);
208 cpu
->current_tb
= NULL
;
209 tb_phys_invalidate(tb
, -1);
213 static TranslationBlock
*tb_find_physical(CPUState
*cpu
,
215 target_ulong cs_base
,
218 CPUArchState
*env
= (CPUArchState
*)cpu
->env_ptr
;
219 TranslationBlock
*tb
, **ptb1
;
221 tb_page_addr_t phys_pc
, phys_page1
;
222 target_ulong virt_page2
;
224 tcg_ctx
.tb_ctx
.tb_invalidated_flag
= 0;
226 /* find translated block using physical mappings */
227 phys_pc
= get_page_addr_code(env
, pc
);
228 phys_page1
= phys_pc
& TARGET_PAGE_MASK
;
229 h
= tb_phys_hash_func(phys_pc
);
230 ptb1
= &tcg_ctx
.tb_ctx
.tb_phys_hash
[h
];
237 tb
->page_addr
[0] == phys_page1
&&
238 tb
->cs_base
== cs_base
&&
239 tb
->flags
== flags
) {
240 /* check next page if needed */
241 if (tb
->page_addr
[1] != -1) {
242 tb_page_addr_t phys_page2
;
244 virt_page2
= (pc
& TARGET_PAGE_MASK
) +
246 phys_page2
= get_page_addr_code(env
, virt_page2
);
247 if (tb
->page_addr
[1] == phys_page2
) {
254 ptb1
= &tb
->phys_hash_next
;
257 /* Move the TB to the head of the list */
258 *ptb1
= tb
->phys_hash_next
;
259 tb
->phys_hash_next
= tcg_ctx
.tb_ctx
.tb_phys_hash
[h
];
260 tcg_ctx
.tb_ctx
.tb_phys_hash
[h
] = tb
;
264 static TranslationBlock
*tb_find_slow(CPUState
*cpu
,
266 target_ulong cs_base
,
269 TranslationBlock
*tb
;
271 tb
= tb_find_physical(cpu
, pc
, cs_base
, flags
);
276 #ifdef CONFIG_USER_ONLY
277 /* mmap_lock is needed by tb_gen_code, and mmap_lock must be
278 * taken outside tb_lock. Since we're momentarily dropping
279 * tb_lock, there's a chance that our desired tb has been
285 tb
= tb_find_physical(cpu
, pc
, cs_base
, flags
);
292 /* if no translated code available, then translate it now */
293 tb
= tb_gen_code(cpu
, pc
, cs_base
, flags
, 0);
295 #ifdef CONFIG_USER_ONLY
300 /* we add the TB in the virtual pc hash table */
301 cpu
->tb_jmp_cache
[tb_jmp_cache_hash_func(pc
)] = tb
;
305 static inline TranslationBlock
*tb_find_fast(CPUState
*cpu
)
307 CPUArchState
*env
= (CPUArchState
*)cpu
->env_ptr
;
308 TranslationBlock
*tb
;
309 target_ulong cs_base
, pc
;
312 /* we record a subset of the CPU state. It will
313 always be the same before a given translated block
315 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &flags
);
316 tb
= cpu
->tb_jmp_cache
[tb_jmp_cache_hash_func(pc
)];
317 if (unlikely(!tb
|| tb
->pc
!= pc
|| tb
->cs_base
!= cs_base
||
318 tb
->flags
!= flags
)) {
319 tb
= tb_find_slow(cpu
, pc
, cs_base
, flags
);
324 static void cpu_handle_debug_exception(CPUState
*cpu
)
326 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
329 if (!cpu
->watchpoint_hit
) {
330 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
331 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
335 cc
->debug_excp_handler(cpu
);
338 /* main execution loop */
340 int cpu_exec(CPUState
*cpu
)
342 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
344 X86CPU
*x86_cpu
= X86_CPU(cpu
);
345 CPUArchState
*env
= &x86_cpu
->env
;
347 int ret
, interrupt_request
;
348 TranslationBlock
*tb
;
354 if (!cpu_has_work(cpu
)) {
362 atomic_mb_set(&tcg_current_cpu
, cpu
);
365 if (unlikely(atomic_mb_read(&exit_request
))) {
366 cpu
->exit_request
= 1;
369 cc
->cpu_exec_enter(cpu
);
371 /* Calculate difference between guest clock and host clock.
372 * This delay includes the delay of the last cycle, so
373 * what we have to do is sleep until it is 0. As for the
374 * advance/delay we gain here, we try to fix it next time.
376 init_delay_params(&sc
, cpu
);
378 /* prepare setjmp context for exception handling */
380 if (sigsetjmp(cpu
->jmp_env
, 0) == 0) {
381 /* if an exception is pending, we execute it here */
382 if (cpu
->exception_index
>= 0) {
383 if (cpu
->exception_index
>= EXCP_INTERRUPT
) {
384 /* exit request from the cpu execution loop */
385 ret
= cpu
->exception_index
;
386 if (ret
== EXCP_DEBUG
) {
387 cpu_handle_debug_exception(cpu
);
389 cpu
->exception_index
= -1;
392 #if defined(CONFIG_USER_ONLY)
393 /* if user mode only, we simulate a fake exception
394 which will be handled outside the cpu execution
396 #if defined(TARGET_I386)
397 cc
->do_interrupt(cpu
);
399 ret
= cpu
->exception_index
;
400 cpu
->exception_index
= -1;
403 cc
->do_interrupt(cpu
);
404 cpu
->exception_index
= -1;
409 next_tb
= 0; /* force lookup of first TB */
411 interrupt_request
= cpu
->interrupt_request
;
412 if (unlikely(interrupt_request
)) {
413 if (unlikely(cpu
->singlestep_enabled
& SSTEP_NOIRQ
)) {
414 /* Mask out external interrupts for this step. */
415 interrupt_request
&= ~CPU_INTERRUPT_SSTEP_MASK
;
417 if (interrupt_request
& CPU_INTERRUPT_DEBUG
) {
418 cpu
->interrupt_request
&= ~CPU_INTERRUPT_DEBUG
;
419 cpu
->exception_index
= EXCP_DEBUG
;
422 if (interrupt_request
& CPU_INTERRUPT_HALT
) {
423 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HALT
;
425 cpu
->exception_index
= EXCP_HLT
;
428 #if defined(TARGET_I386)
429 if (interrupt_request
& CPU_INTERRUPT_INIT
) {
430 cpu_svm_check_intercept_param(env
, SVM_EXIT_INIT
, 0);
431 do_cpu_init(x86_cpu
);
432 cpu
->exception_index
= EXCP_HALTED
;
436 if (interrupt_request
& CPU_INTERRUPT_RESET
) {
440 /* The target hook has 3 exit conditions:
441 False when the interrupt isn't processed,
442 True when it is, and we should restart on a new TB,
443 and via longjmp via cpu_loop_exit. */
444 if (cc
->cpu_exec_interrupt(cpu
, interrupt_request
)) {
447 /* Don't use the cached interrupt_request value,
448 do_interrupt may have updated the EXITTB flag. */
449 if (cpu
->interrupt_request
& CPU_INTERRUPT_EXITTB
) {
450 cpu
->interrupt_request
&= ~CPU_INTERRUPT_EXITTB
;
451 /* ensure that no TB jump will be modified as
452 the program flow was changed */
456 if (unlikely(cpu
->exit_request
)) {
457 cpu
->exit_request
= 0;
458 cpu
->exception_index
= EXCP_INTERRUPT
;
462 tb
= tb_find_fast(cpu
);
463 /* Note: we do it here to avoid a gcc bug on Mac OS X when
464 doing it in tb_find_slow */
465 if (tcg_ctx
.tb_ctx
.tb_invalidated_flag
) {
466 /* as some TB could have been invalidated because
467 of memory exceptions while generating the code, we
468 must recompute the hash index here */
470 tcg_ctx
.tb_ctx
.tb_invalidated_flag
= 0;
472 if (qemu_loglevel_mask(CPU_LOG_EXEC
)) {
473 qemu_log("Trace %p [" TARGET_FMT_lx
"] %s\n",
474 tb
->tc_ptr
, tb
->pc
, lookup_symbol(tb
->pc
));
476 /* see if we can patch the calling TB. When the TB
477 spans two pages, we cannot safely do a direct
479 if (next_tb
!= 0 && tb
->page_addr
[1] == -1) {
480 tb_add_jump((TranslationBlock
*)(next_tb
& ~TB_EXIT_MASK
),
481 next_tb
& TB_EXIT_MASK
, tb
);
484 if (likely(!cpu
->exit_request
)) {
485 trace_exec_tb(tb
, tb
->pc
);
487 /* execute the generated code */
488 cpu
->current_tb
= tb
;
489 next_tb
= cpu_tb_exec(cpu
, tc_ptr
);
490 cpu
->current_tb
= NULL
;
491 switch (next_tb
& TB_EXIT_MASK
) {
492 case TB_EXIT_REQUESTED
:
493 /* Something asked us to stop executing
494 * chained TBs; just continue round the main
495 * loop. Whatever requested the exit will also
496 * have set something else (eg exit_request or
497 * interrupt_request) which we will handle
498 * next time around the loop. But we need to
499 * ensure the tcg_exit_req read in generated code
500 * comes before the next read of cpu->exit_request
501 * or cpu->interrupt_request.
506 case TB_EXIT_ICOUNT_EXPIRED
:
508 /* Instruction counter expired. */
509 int insns_left
= cpu
->icount_decr
.u32
;
510 if (cpu
->icount_extra
&& insns_left
>= 0) {
511 /* Refill decrementer and continue execution. */
512 cpu
->icount_extra
+= insns_left
;
513 insns_left
= MIN(0xffff, cpu
->icount_extra
);
514 cpu
->icount_extra
-= insns_left
;
515 cpu
->icount_decr
.u16
.low
= insns_left
;
517 if (insns_left
> 0) {
518 /* Execute remaining instructions. */
519 tb
= (TranslationBlock
*)(next_tb
& ~TB_EXIT_MASK
);
520 cpu_exec_nocache(cpu
, insns_left
, tb
);
521 align_clocks(&sc
, cpu
);
523 cpu
->exception_index
= EXCP_INTERRUPT
;
533 /* Try to align the host and virtual clocks
534 if the guest is in advance */
535 align_clocks(&sc
, cpu
);
536 /* reset soft MMU for next block (it can currently
537 only be set by a memory fault) */
540 /* Reload env after longjmp - the compiler may have smashed all
541 * local variables as longjmp is marked 'noreturn'. */
542 /* TODO: Fix comment in 2014 - then it was wrong. */
543 g_assert(cpu
== current_cpu
);
544 g_assert(cc
== CPU_GET_CLASS(cpu
));
547 g_assert(x86_cpu
== X86_CPU(cpu
));
548 g_assert(env
== cpu
->env_ptr
);
554 cc
->cpu_exec_exit(cpu
);
557 /* fail safe : never use current_cpu outside cpu_exec() */
560 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
561 atomic_set(&tcg_current_cpu
, NULL
);