]> git.proxmox.com Git - mirror_qemu.git/blame - accel/tcg/cpu-exec.c
accel/tcg: Replace `TARGET_TB_PCREL` with `CF_PCREL`
[mirror_qemu.git] / accel / tcg / cpu-exec.c
CommitLineData
7d13299d 1/*
e965fc38 2 * emulator main execution loop
5fafdf24 3 *
66321a11 4 * Copyright (c) 2003-2005 Fabrice Bellard
7d13299d 5 *
3ef693a0
FB
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
fb0343d5 9 * version 2.1 of the License, or (at your option) any later version.
7d13299d 10 *
3ef693a0
FB
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.
7d13299d 15 *
3ef693a0 16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
7d13299d 18 */
a8d25326 19
7b31bbc2 20#include "qemu/osdep.h"
740b1759 21#include "qemu/qemu-print.h"
3a841ab5 22#include "qapi/error.h"
3a841ab5 23#include "qapi/type-helpers.h"
78271684 24#include "hw/core/tcg-cpu-ops.h"
d9bb58e5 25#include "trace.h"
76cad711 26#include "disas/disas.h"
63c91552 27#include "exec/exec-all.h"
dcb32f1d 28#include "tcg/tcg.h"
1de7afc9 29#include "qemu/atomic.h"
79e2b9ae 30#include "qemu/rcu.h"
508127e2 31#include "exec/log.h"
8d04fb55 32#include "qemu/main-loop.h"
6220e900
PD
33#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
34#include "hw/i386/apic.h"
35#endif
d2528bdc 36#include "sysemu/cpus.h"
740b1759
CF
37#include "exec/cpu-all.h"
38#include "sysemu/cpu-timers.h"
5b5968c4 39#include "exec/replay-core.h"
3a841ab5 40#include "sysemu/tcg.h"
4288eb26 41#include "exec/helper-proto.h"
a976a99a 42#include "tb-jmp-cache.h"
e5ceadff 43#include "tb-hash.h"
e5ceadff 44#include "tb-context.h"
c03f041f 45#include "internal.h"
c2aa5f81
ST
46
47/* -icount align implementation. */
48
49typedef struct SyncClocks {
50 int64_t diff_clk;
51 int64_t last_cpu_icount;
7f7bc144 52 int64_t realtime_clock;
c2aa5f81
ST
53} SyncClocks;
54
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
58 * oscillate around 0.
59 */
60#define VM_CLOCK_ADVANCE 3000000
7f7bc144
ST
61#define THRESHOLD_REDUCE 1.5
62#define MAX_DELAY_PRINT_RATE 2000000000LL
63#define MAX_NB_PRINTS 100
c2aa5f81 64
00c9a5c2
PMD
65int64_t max_delay;
66int64_t max_advance;
740b1759 67
5e140196 68static void align_clocks(SyncClocks *sc, CPUState *cpu)
c2aa5f81
ST
69{
70 int64_t cpu_icount;
71
72 if (!icount_align_option) {
73 return;
74 }
75
5e140196 76 cpu_icount = cpu->icount_extra + cpu_neg(cpu)->icount_decr.u16.low;
8191d368 77 sc->diff_clk += icount_to_ns(sc->last_cpu_icount - cpu_icount);
c2aa5f81
ST
78 sc->last_cpu_icount = cpu_icount;
79
80 if (sc->diff_clk > VM_CLOCK_ADVANCE) {
81#ifndef _WIN32
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) {
a498d0ef 86 sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec;
c2aa5f81
ST
87 } else {
88 sc->diff_clk = 0;
89 }
90#else
91 Sleep(sc->diff_clk / SCALE_MS);
92 sc->diff_clk = 0;
93#endif
94 }
95}
96
7f7bc144
ST
97static void print_delay(const SyncClocks *sc)
98{
99 static float threshold_delay;
100 static int64_t last_realtime_clock;
101 static int nb_prints;
102
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;
740b1759
CF
110 qemu_printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
111 threshold_delay - 1,
112 threshold_delay);
7f7bc144
ST
113 nb_prints++;
114 last_realtime_clock = sc->realtime_clock;
115 }
116 }
117}
118
5e140196 119static void init_delay_params(SyncClocks *sc, CPUState *cpu)
c2aa5f81
ST
120{
121 if (!icount_align_option) {
122 return;
123 }
2e91cc62
PB
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;
5e140196
RH
126 sc->last_cpu_icount
127 = cpu->icount_extra + cpu_neg(cpu)->icount_decr.u16.low;
27498bef
ST
128 if (sc->diff_clk < max_delay) {
129 max_delay = sc->diff_clk;
130 }
131 if (sc->diff_clk > max_advance) {
132 max_advance = sc->diff_clk;
133 }
7f7bc144
ST
134
135 /* Print every 2s max if the guest is late. We limit the number
136 of printed messages to NB_PRINT_MAX(currently 100) */
137 print_delay(sc);
c2aa5f81
ST
138}
139#else
140static void align_clocks(SyncClocks *sc, const CPUState *cpu)
141{
142}
143
144static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
145{
146}
147#endif /* CONFIG USER ONLY */
7d13299d 148
043e35d9
RH
149uint32_t curr_cflags(CPUState *cpu)
150{
84f15616
RH
151 uint32_t cflags = cpu->tcg_cflags;
152
04f5b647 153 /*
c2ffd754
RH
154 * Record gdb single-step. We should be exiting the TB by raising
155 * EXCP_DEBUG, but to simplify other tests, disable chaining too.
156 *
04f5b647
RH
157 * For singlestep and -d nochain, suppress goto_tb so that
158 * we can log -d cpu,exec after every TB.
159 */
c2ffd754
RH
160 if (unlikely(cpu->singlestep_enabled)) {
161 cflags |= CF_NO_GOTO_TB | CF_NO_GOTO_PTR | CF_SINGLE_STEP | 1;
162 } else if (singlestep) {
04f5b647
RH
163 cflags |= CF_NO_GOTO_TB | 1;
164 } else if (qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) {
fb957011 165 cflags |= CF_NO_GOTO_TB;
84f15616
RH
166 }
167
168 return cflags;
043e35d9
RH
169}
170
0c90ba16
RH
171struct tb_desc {
172 target_ulong pc;
173 target_ulong cs_base;
174 CPUArchState *env;
93b99616 175 tb_page_addr_t page_addr0;
0c90ba16
RH
176 uint32_t flags;
177 uint32_t cflags;
178 uint32_t trace_vcpu_dstate;
179};
180
181static bool tb_lookup_cmp(const void *p, const void *d)
182{
183 const TranslationBlock *tb = p;
184 const struct tb_desc *desc = d;
185
4be79026 186 if ((tb_cflags(tb) & CF_PCREL || tb_pc(tb) == desc->pc) &&
28905cfb 187 tb_page_addr0(tb) == desc->page_addr0 &&
0c90ba16
RH
188 tb->cs_base == desc->cs_base &&
189 tb->flags == desc->flags &&
190 tb->trace_vcpu_dstate == desc->trace_vcpu_dstate &&
191 tb_cflags(tb) == desc->cflags) {
192 /* check next page if needed */
28905cfb
RH
193 tb_page_addr_t tb_phys_page1 = tb_page_addr1(tb);
194 if (tb_phys_page1 == -1) {
0c90ba16
RH
195 return true;
196 } else {
93b99616
RH
197 tb_page_addr_t phys_page1;
198 target_ulong virt_page1;
0c90ba16 199
9867b302
RH
200 /*
201 * We know that the first page matched, and an otherwise valid TB
202 * encountered an incomplete instruction at the end of that page,
203 * therefore we know that generating a new TB from the current PC
204 * must also require reading from the next page -- even if the
205 * second pages do not match, and therefore the resulting insn
206 * is different for the new TB. Therefore any exception raised
207 * here by the faulting lookup is not premature.
208 */
93b99616
RH
209 virt_page1 = TARGET_PAGE_ALIGN(desc->pc);
210 phys_page1 = get_page_addr_code(desc->env, virt_page1);
28905cfb 211 if (tb_phys_page1 == phys_page1) {
0c90ba16
RH
212 return true;
213 }
214 }
215 }
216 return false;
217}
218
219static TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
220 target_ulong cs_base, uint32_t flags,
221 uint32_t cflags)
222{
223 tb_page_addr_t phys_pc;
224 struct tb_desc desc;
225 uint32_t h;
226
227 desc.env = cpu->env_ptr;
228 desc.cs_base = cs_base;
229 desc.flags = flags;
230 desc.cflags = cflags;
231 desc.trace_vcpu_dstate = *cpu->trace_dstate;
232 desc.pc = pc;
233 phys_pc = get_page_addr_code(desc.env, pc);
234 if (phys_pc == -1) {
235 return NULL;
236 }
93b99616 237 desc.page_addr0 = phys_pc;
4be79026 238 h = tb_hash_func(phys_pc, (cflags & CF_PCREL ? 0 : pc),
8ed558ec 239 flags, cflags, *cpu->trace_dstate);
0c90ba16
RH
240 return qht_lookup_custom(&tb_ctx.htable, &desc, h, tb_lookup_cmp);
241}
242
632cb63d
RH
243/* Might cause an exception, so have a longjmp destination ready */
244static inline TranslationBlock *tb_lookup(CPUState *cpu, target_ulong pc,
245 target_ulong cs_base,
246 uint32_t flags, uint32_t cflags)
247{
248 TranslationBlock *tb;
8ed558ec 249 CPUJumpCache *jc;
632cb63d
RH
250 uint32_t hash;
251
252 /* we should never be trying to look up an INVALID tb */
253 tcg_debug_assert(!(cflags & CF_INVALID));
254
255 hash = tb_jmp_cache_hash_func(pc);
8ed558ec 256 jc = cpu->tb_jmp_cache;
4be79026 257 tb = tb_jmp_cache_get_tb(jc, cflags, hash);
632cb63d
RH
258
259 if (likely(tb &&
8ed558ec 260 tb_jmp_cache_get_pc(jc, hash, tb) == pc &&
632cb63d
RH
261 tb->cs_base == cs_base &&
262 tb->flags == flags &&
263 tb->trace_vcpu_dstate == *cpu->trace_dstate &&
264 tb_cflags(tb) == cflags)) {
265 return tb;
266 }
267 tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags);
268 if (tb == NULL) {
269 return NULL;
270 }
8ed558ec 271 tb_jmp_cache_set(jc, hash, tb, pc);
632cb63d
RH
272 return tb;
273}
274
fbf59aad
RH
275static void log_cpu_exec(target_ulong pc, CPUState *cpu,
276 const TranslationBlock *tb)
abb0cd93 277{
fbf59aad 278 if (qemu_log_in_addr_range(pc)) {
abb0cd93
RH
279 qemu_log_mask(CPU_LOG_EXEC,
280 "Trace %d: %p [" TARGET_FMT_lx
7eabad36
RH
281 "/" TARGET_FMT_lx "/%08x/%08x] %s\n",
282 cpu->cpu_index, tb->tc.ptr, tb->cs_base, pc,
283 tb->flags, tb->cflags, lookup_symbol(pc));
abb0cd93
RH
284
285#if defined(DEBUG_DISAS)
286 if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
c60f599b 287 FILE *logfile = qemu_log_trylock();
78b54858
RH
288 if (logfile) {
289 int flags = 0;
abb0cd93 290
78b54858
RH
291 if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) {
292 flags |= CPU_DUMP_FPU;
293 }
abb0cd93 294#if defined(TARGET_I386)
78b54858 295 flags |= CPU_DUMP_CCOP;
abb0cd93 296#endif
c769fbd7 297 cpu_dump_state(cpu, logfile, flags);
78b54858
RH
298 qemu_log_unlock(logfile);
299 }
abb0cd93
RH
300 }
301#endif /* DEBUG_DISAS */
302 }
303}
304
69993c4e
LL
305static bool check_for_breakpoints_slow(CPUState *cpu, target_ulong pc,
306 uint32_t *cflags)
10c37828
RH
307{
308 CPUBreakpoint *bp;
309 bool match_page = false;
310
10c37828
RH
311 /*
312 * Singlestep overrides breakpoints.
313 * This requirement is visible in the record-replay tests, where
314 * we would fail to make forward progress in reverse-continue.
315 *
316 * TODO: gdb singlestep should only override gdb breakpoints,
317 * so that one could (gdb) singlestep into the guest kernel's
318 * architectural breakpoint handler.
319 */
320 if (cpu->singlestep_enabled) {
321 return false;
322 }
323
324 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
325 /*
326 * If we have an exact pc match, trigger the breakpoint.
327 * Otherwise, note matches within the page.
328 */
329 if (pc == bp->pc) {
330 bool match_bp = false;
331
332 if (bp->flags & BP_GDB) {
333 match_bp = true;
334 } else if (bp->flags & BP_CPU) {
335#ifdef CONFIG_USER_ONLY
336 g_assert_not_reached();
337#else
338 CPUClass *cc = CPU_GET_CLASS(cpu);
339 assert(cc->tcg_ops->debug_check_breakpoint);
340 match_bp = cc->tcg_ops->debug_check_breakpoint(cpu);
341#endif
342 }
343
344 if (match_bp) {
345 cpu->exception_index = EXCP_DEBUG;
346 return true;
347 }
348 } else if (((pc ^ bp->pc) & TARGET_PAGE_MASK) == 0) {
349 match_page = true;
350 }
351 }
352
353 /*
354 * Within the same page as a breakpoint, single-step,
355 * returning to helper_lookup_tb_ptr after each insn looking
356 * for the actual breakpoint.
357 *
358 * TODO: Perhaps better to record all of the TBs associated
359 * with a given virtual page that contains a breakpoint, and
360 * then invalidate them when a new overlapping breakpoint is
361 * set on the page. Non-overlapping TBs would not be
362 * invalidated, nor would any TB need to be invalidated as
363 * breakpoints are removed.
364 */
365 if (match_page) {
366 *cflags = (*cflags & ~CF_COUNT_MASK) | CF_NO_GOTO_TB | 1;
367 }
368 return false;
369}
370
69993c4e
LL
371static inline bool check_for_breakpoints(CPUState *cpu, target_ulong pc,
372 uint32_t *cflags)
373{
374 return unlikely(!QTAILQ_EMPTY(&cpu->breakpoints)) &&
375 check_for_breakpoints_slow(cpu, pc, cflags);
376}
377
4288eb26
RH
378/**
379 * helper_lookup_tb_ptr: quick check for next tb
380 * @env: current cpu state
381 *
382 * Look for an existing TB matching the current cpu state.
383 * If found, return the code pointer. If not found, return
384 * the tcg epilogue so that we return into cpu_tb_exec.
385 */
386const void *HELPER(lookup_tb_ptr)(CPUArchState *env)
387{
388 CPUState *cpu = env_cpu(env);
389 TranslationBlock *tb;
390 target_ulong cs_base, pc;
10c37828 391 uint32_t flags, cflags;
4288eb26
RH
392
393 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
394
10c37828
RH
395 cflags = curr_cflags(cpu);
396 if (check_for_breakpoints(cpu, pc, &cflags)) {
397 cpu_loop_exit(cpu);
398 }
399
400 tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
4288eb26
RH
401 if (tb == NULL) {
402 return tcg_code_gen_epilogue;
403 }
abb0cd93 404
fbf59aad
RH
405 if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
406 log_cpu_exec(pc, cpu, tb);
407 }
abb0cd93 408
4288eb26
RH
409 return tb->tc.ptr;
410}
411
77211379 412/* Execute a TB, and fix up the CPU state afterwards if necessary */
c905a368
DB
413/*
414 * Disable CFI checks.
415 * TCG creates binary blobs at runtime, with the transformed code.
416 * A TB is a blob of binary code, created at runtime and called with an
417 * indirect function call. Since such function did not exist at compile time,
418 * the CFI runtime has no way to verify its signature and would fail.
419 * TCG is not considered a security-sensitive part of QEMU so this does not
420 * affect the impact of CFI in environment with high security requirements
421 */
eba40358
RH
422static inline TranslationBlock * QEMU_DISABLE_CFI
423cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit)
77211379
PM
424{
425 CPUArchState *env = cpu->env_ptr;
819af24b
SF
426 uintptr_t ret;
427 TranslationBlock *last_tb;
db0c51a3 428 const void *tb_ptr = itb->tc.ptr;
1a830635 429
fbf59aad
RH
430 if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
431 log_cpu_exec(log_pc(cpu, itb), cpu, itb);
432 }
03afa5f8 433
653b87eb 434 qemu_thread_jit_execute();
819af24b 435 ret = tcg_qemu_tb_exec(env, tb_ptr);
626cf8f4 436 cpu->can_do_io = 1;
eba40358
RH
437 /*
438 * TODO: Delay swapping back to the read-write region of the TB
439 * until we actually need to modify the TB. The read-only copy,
440 * coming from the rx region, shares the same host TLB entry as
441 * the code that executed the exit_tb opcode that arrived here.
442 * If we insist on touching both the RX and the RW pages, we
443 * double the host TLB pressure.
444 */
445 last_tb = tcg_splitwx_to_rw((void *)(ret & ~TB_EXIT_MASK));
446 *tb_exit = ret & TB_EXIT_MASK;
447
448 trace_exec_tb_exit(last_tb, *tb_exit);
6db8b538 449
eba40358 450 if (*tb_exit > TB_EXIT_IDX1) {
77211379
PM
451 /* We didn't start executing this TB (eg because the instruction
452 * counter hit zero); we must restore the guest PC to the address
453 * of the start of the TB.
454 */
bdf7ae5b 455 CPUClass *cc = CPU_GET_CLASS(cpu);
fbf59aad 456
78271684
CF
457 if (cc->tcg_ops->synchronize_from_tb) {
458 cc->tcg_ops->synchronize_from_tb(cpu, last_tb);
bdf7ae5b 459 } else {
4be79026 460 tcg_debug_assert(!(tb_cflags(last_tb) & CF_PCREL));
bdf7ae5b 461 assert(cc->set_pc);
fbf59aad
RH
462 cc->set_pc(cpu, tb_pc(last_tb));
463 }
464 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
465 target_ulong pc = log_pc(cpu, last_tb);
466 if (qemu_log_in_addr_range(pc)) {
467 qemu_log("Stopped execution of TB chain before %p ["
468 TARGET_FMT_lx "] %s\n",
469 last_tb->tc.ptr, pc, lookup_symbol(pc));
470 }
bdf7ae5b 471 }
77211379 472 }
c9460d75
RH
473
474 /*
475 * If gdb single-step, and we haven't raised another exception,
476 * raise a debug exception. Single-step with another exception
477 * is handled in cpu_handle_exception.
478 */
479 if (unlikely(cpu->singlestep_enabled) && cpu->exception_index == -1) {
480 cpu->exception_index = EXCP_DEBUG;
481 cpu_loop_exit(cpu);
482 }
483
eba40358 484 return last_tb;
77211379
PM
485}
486
2e70f6ef 487
035ba06c
EH
488static void cpu_exec_enter(CPUState *cpu)
489{
490 CPUClass *cc = CPU_GET_CLASS(cpu);
491
78271684
CF
492 if (cc->tcg_ops->cpu_exec_enter) {
493 cc->tcg_ops->cpu_exec_enter(cpu);
80c4750b 494 }
035ba06c
EH
495}
496
497static void cpu_exec_exit(CPUState *cpu)
fdbc2b57 498{
08e73c48 499 CPUClass *cc = CPU_GET_CLASS(cpu);
035ba06c 500
78271684
CF
501 if (cc->tcg_ops->cpu_exec_exit) {
502 cc->tcg_ops->cpu_exec_exit(cpu);
80c4750b 503 }
882f5b1b 504 QEMU_PLUGIN_ASSERT(cpu->plugin_mem_cbs == NULL);
035ba06c
EH
505}
506
507void cpu_exec_step_atomic(CPUState *cpu)
508{
61deada4 509 CPUArchState *env = cpu->env_ptr;
fdbc2b57
RH
510 TranslationBlock *tb;
511 target_ulong cs_base, pc;
258afb48 512 uint32_t flags, cflags;
eba40358 513 int tb_exit;
fdbc2b57 514
08e73c48 515 if (sigsetjmp(cpu->jmp_env, 0) == 0) {
886cc689 516 start_exclusive();
bfff072c
DC
517 g_assert(cpu == current_cpu);
518 g_assert(!cpu->running);
519 cpu->running = true;
886cc689 520
6f04cb1c 521 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
6f04cb1c 522
258afb48
RH
523 cflags = curr_cflags(cpu);
524 /* Execute in a serial context. */
525 cflags &= ~CF_PARALLEL;
526 /* After 1 insn, return and release the exclusive lock. */
527 cflags |= CF_NO_GOTO_TB | CF_NO_GOTO_PTR | 1;
10c37828
RH
528 /*
529 * No need to check_for_breakpoints here.
530 * We only arrive in cpu_exec_step_atomic after beginning execution
531 * of an insn that includes an atomic operation we can't handle.
532 * Any breakpoint for this insn will have been recognized earlier.
533 */
258afb48
RH
534
535 tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
4e2ca83e
EC
536 if (tb == NULL) {
537 mmap_lock();
95590e24 538 tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
4e2ca83e
EC
539 mmap_unlock();
540 }
08e73c48 541
035ba06c 542 cpu_exec_enter(cpu);
08e73c48 543 /* execute the generated code */
4e2ca83e 544 trace_exec_tb(tb, pc);
eba40358 545 cpu_tb_exec(cpu, tb, &tb_exit);
035ba06c 546 cpu_exec_exit(cpu);
08e73c48 547 } else {
08e73c48 548#ifndef CONFIG_SOFTMMU
f920ffdd 549 clear_helper_retaddr();
297368c7
RH
550 if (have_mmap_lock()) {
551 mmap_unlock();
552 }
08e73c48 553#endif
6aaa24f9
EC
554 if (qemu_mutex_iothread_locked()) {
555 qemu_mutex_unlock_iothread();
556 }
faa9372c 557 assert_no_pages_locked();
e6d86bed 558 qemu_plugin_disable_mem_helpers(cpu);
08e73c48 559 }
426eeecd 560
886cc689
AB
561 /*
562 * As we start the exclusive region before codegen we must still
563 * be in the region if we longjump out of either the codegen or
564 * the execution.
565 */
566 g_assert(cpu_in_exclusive_context(cpu));
bfff072c 567 cpu->running = false;
886cc689 568 end_exclusive();
fdbc2b57
RH
569}
570
a8583393
RH
571void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr)
572{
2fd2e78d
RH
573 /*
574 * Get the rx view of the structure, from which we find the
575 * executable code address, and tb_target_set_jmp_target can
576 * produce a pc-relative displacement to jmp_target_addr[n].
577 */
578 const TranslationBlock *c_tb = tcg_splitwx_to_rx(tb);
579 uintptr_t offset = tb->jmp_insn_offset[n];
580 uintptr_t jmp_rx = (uintptr_t)tb->tc.ptr + offset;
581 uintptr_t jmp_rw = jmp_rx - tcg_splitwx_diff;
582
9da6079b 583 tb->jmp_target_addr[n] = addr;
2fd2e78d 584 tb_target_set_jmp_target(c_tb, n, jmp_rx, jmp_rw);
a8583393
RH
585}
586
a8583393
RH
587static inline void tb_add_jump(TranslationBlock *tb, int n,
588 TranslationBlock *tb_next)
589{
194125e3
EC
590 uintptr_t old;
591
653b87eb 592 qemu_thread_jit_write();
a8583393 593 assert(n < ARRAY_SIZE(tb->jmp_list_next));
194125e3
EC
594 qemu_spin_lock(&tb_next->jmp_lock);
595
596 /* make sure the destination TB is valid */
597 if (tb_next->cflags & CF_INVALID) {
598 goto out_unlock_next;
599 }
600 /* Atomically claim the jump destination slot only if it was NULL */
d73415a3
SH
601 old = qatomic_cmpxchg(&tb->jmp_dest[n], (uintptr_t)NULL,
602 (uintptr_t)tb_next);
194125e3
EC
603 if (old) {
604 goto out_unlock_next;
a8583393 605 }
194125e3
EC
606
607 /* patch the native jump address */
608 tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc.ptr);
609
610 /* add in TB jmp list */
611 tb->jmp_list_next[n] = tb_next->jmp_list_head;
612 tb_next->jmp_list_head = (uintptr_t)tb | n;
613
614 qemu_spin_unlock(&tb_next->jmp_lock);
615
fbf59aad
RH
616 qemu_log_mask(CPU_LOG_EXEC, "Linking TBs %p index %d -> %p\n",
617 tb->tc.ptr, n, tb_next->tc.ptr);
194125e3 618 return;
a8583393 619
194125e3
EC
620 out_unlock_next:
621 qemu_spin_unlock(&tb_next->jmp_lock);
622 return;
a8583393
RH
623}
624
8b2d34e9
SF
625static inline bool cpu_handle_halt(CPUState *cpu)
626{
0596fa11 627#ifndef CONFIG_USER_ONLY
8b2d34e9 628 if (cpu->halted) {
0596fa11 629#if defined(TARGET_I386)
4084893d 630 if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
8b2d34e9 631 X86CPU *x86_cpu = X86_CPU(cpu);
8d04fb55 632 qemu_mutex_lock_iothread();
8b2d34e9
SF
633 apic_poll_irq(x86_cpu->apic_state);
634 cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
8d04fb55 635 qemu_mutex_unlock_iothread();
8b2d34e9 636 }
0596fa11 637#endif /* TARGET_I386 */
8b2d34e9 638 if (!cpu_has_work(cpu)) {
8b2d34e9
SF
639 return true;
640 }
641
642 cpu->halted = 0;
643 }
0596fa11 644#endif /* !CONFIG_USER_ONLY */
8b2d34e9
SF
645
646 return false;
647}
648
ea284766 649static inline void cpu_handle_debug_exception(CPUState *cpu)
1009d2ed 650{
86025ee4 651 CPUClass *cc = CPU_GET_CLASS(cpu);
1009d2ed
JK
652 CPUWatchpoint *wp;
653
ff4700b0
AF
654 if (!cpu->watchpoint_hit) {
655 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
1009d2ed
JK
656 wp->flags &= ~BP_WATCHPOINT_HIT;
657 }
658 }
86025ee4 659
78271684
CF
660 if (cc->tcg_ops->debug_excp_handler) {
661 cc->tcg_ops->debug_excp_handler(cpu);
710384d0 662 }
1009d2ed
JK
663}
664
ea284766
SF
665static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
666{
17b50b0c
PD
667 if (cpu->exception_index < 0) {
668#ifndef CONFIG_USER_ONLY
669 if (replay_has_exception()
5e140196 670 && cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra == 0) {
a11bbb6a 671 /* Execute just one insn to trigger exception pending in the log */
c3e97f64
PD
672 cpu->cflags_next_tb = (curr_cflags(cpu) & ~CF_USE_ICOUNT)
673 | CF_NOIRQ | 1;
17b50b0c
PD
674 }
675#endif
a11bbb6a 676 return false;
17b50b0c 677 }
17b50b0c
PD
678 if (cpu->exception_index >= EXCP_INTERRUPT) {
679 /* exit request from the cpu execution loop */
680 *ret = cpu->exception_index;
681 if (*ret == EXCP_DEBUG) {
682 cpu_handle_debug_exception(cpu);
683 }
684 cpu->exception_index = -1;
685 return true;
686 } else {
ea284766 687#if defined(CONFIG_USER_ONLY)
17b50b0c
PD
688 /* if user mode only, we simulate a fake exception
689 which will be handled outside the cpu execution
690 loop */
ea284766 691#if defined(TARGET_I386)
17b50b0c 692 CPUClass *cc = CPU_GET_CLASS(cpu);
12096421
PMD
693 cc->tcg_ops->fake_user_interrupt(cpu);
694#endif /* TARGET_I386 */
17b50b0c
PD
695 *ret = cpu->exception_index;
696 cpu->exception_index = -1;
697 return true;
698#else
699 if (replay_exception()) {
ea284766 700 CPUClass *cc = CPU_GET_CLASS(cpu);
17b50b0c 701 qemu_mutex_lock_iothread();
78271684 702 cc->tcg_ops->do_interrupt(cpu);
17b50b0c 703 qemu_mutex_unlock_iothread();
ea284766 704 cpu->exception_index = -1;
a7ba744f
LM
705
706 if (unlikely(cpu->singlestep_enabled)) {
707 /*
708 * After processing the exception, ensure an EXCP_DEBUG is
709 * raised when single-stepping so that GDB doesn't miss the
710 * next instruction.
711 */
712 *ret = EXCP_DEBUG;
713 cpu_handle_debug_exception(cpu);
714 return true;
715 }
17b50b0c
PD
716 } else if (!replay_has_interrupt()) {
717 /* give a chance to iothread in replay mode */
718 *ret = EXCP_INTERRUPT;
ea284766 719 return true;
ea284766 720 }
ea284766
SF
721#endif
722 }
723
724 return false;
725}
726
77c0fc4e 727#ifndef CONFIG_USER_ONLY
4084893d
PD
728/*
729 * CPU_INTERRUPT_POLL is a virtual event which gets converted into a
730 * "real" interrupt event later. It does not need to be recorded for
731 * replay purposes.
732 */
733static inline bool need_replay_interrupt(int interrupt_request)
734{
735#if defined(TARGET_I386)
736 return !(interrupt_request & CPU_INTERRUPT_POLL);
737#else
738 return true;
739#endif
740}
77c0fc4e 741#endif /* !CONFIG_USER_ONLY */
4084893d 742
209b71b6 743static inline bool cpu_handle_interrupt(CPUState *cpu,
c385e6e4
SF
744 TranslationBlock **last_tb)
745{
aff0e204
AB
746 /*
747 * If we have requested custom cflags with CF_NOIRQ we should
748 * skip checking here. Any pending interrupts will get picked up
749 * by the next TB we execute under normal cflags.
750 */
751 if (cpu->cflags_next_tb != -1 && cpu->cflags_next_tb & CF_NOIRQ) {
752 return false;
753 }
754
17b50b0c
PD
755 /* Clear the interrupt flag now since we're processing
756 * cpu->interrupt_request and cpu->exit_request.
d84be02d
DH
757 * Ensure zeroing happens before reading cpu->exit_request or
758 * cpu->interrupt_request (see also smp_wmb in cpu_exit())
17b50b0c 759 */
d73415a3 760 qatomic_mb_set(&cpu_neg(cpu)->icount_decr.u16.high, 0);
c385e6e4 761
d73415a3 762 if (unlikely(qatomic_read(&cpu->interrupt_request))) {
8d04fb55
JK
763 int interrupt_request;
764 qemu_mutex_lock_iothread();
765 interrupt_request = cpu->interrupt_request;
c385e6e4
SF
766 if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
767 /* Mask out external interrupts for this step. */
768 interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
769 }
770 if (interrupt_request & CPU_INTERRUPT_DEBUG) {
771 cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
772 cpu->exception_index = EXCP_DEBUG;
8d04fb55 773 qemu_mutex_unlock_iothread();
209b71b6 774 return true;
c385e6e4 775 }
77c0fc4e 776#if !defined(CONFIG_USER_ONLY)
c385e6e4
SF
777 if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
778 /* Do nothing */
779 } else if (interrupt_request & CPU_INTERRUPT_HALT) {
780 replay_interrupt();
781 cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
782 cpu->halted = 1;
783 cpu->exception_index = EXCP_HLT;
8d04fb55 784 qemu_mutex_unlock_iothread();
209b71b6 785 return true;
c385e6e4
SF
786 }
787#if defined(TARGET_I386)
788 else if (interrupt_request & CPU_INTERRUPT_INIT) {
789 X86CPU *x86_cpu = X86_CPU(cpu);
790 CPUArchState *env = &x86_cpu->env;
791 replay_interrupt();
65c9d60a 792 cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0, 0);
c385e6e4
SF
793 do_cpu_init(x86_cpu);
794 cpu->exception_index = EXCP_HALTED;
8d04fb55 795 qemu_mutex_unlock_iothread();
209b71b6 796 return true;
c385e6e4
SF
797 }
798#else
799 else if (interrupt_request & CPU_INTERRUPT_RESET) {
800 replay_interrupt();
801 cpu_reset(cpu);
8d04fb55 802 qemu_mutex_unlock_iothread();
209b71b6 803 return true;
c385e6e4 804 }
77c0fc4e 805#endif /* !TARGET_I386 */
c385e6e4
SF
806 /* The target hook has 3 exit conditions:
807 False when the interrupt isn't processed,
808 True when it is, and we should restart on a new TB,
809 and via longjmp via cpu_loop_exit. */
810 else {
77c0fc4e
PMD
811 CPUClass *cc = CPU_GET_CLASS(cpu);
812
78271684
CF
813 if (cc->tcg_ops->cpu_exec_interrupt &&
814 cc->tcg_ops->cpu_exec_interrupt(cpu, interrupt_request)) {
4084893d
PD
815 if (need_replay_interrupt(interrupt_request)) {
816 replay_interrupt();
817 }
ba3c35d9
RH
818 /*
819 * After processing the interrupt, ensure an EXCP_DEBUG is
820 * raised when single-stepping so that GDB doesn't miss the
821 * next instruction.
822 */
5b7b197c
LM
823 if (unlikely(cpu->singlestep_enabled)) {
824 cpu->exception_index = EXCP_DEBUG;
825 qemu_mutex_unlock_iothread();
826 return true;
827 }
828 cpu->exception_index = -1;
c385e6e4
SF
829 *last_tb = NULL;
830 }
8b1fe3f4
SF
831 /* The target hook may have updated the 'cpu->interrupt_request';
832 * reload the 'interrupt_request' value */
833 interrupt_request = cpu->interrupt_request;
c385e6e4 834 }
77c0fc4e 835#endif /* !CONFIG_USER_ONLY */
8b1fe3f4 836 if (interrupt_request & CPU_INTERRUPT_EXITTB) {
c385e6e4
SF
837 cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
838 /* ensure that no TB jump will be modified as
839 the program flow was changed */
840 *last_tb = NULL;
841 }
8d04fb55
JK
842
843 /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
844 qemu_mutex_unlock_iothread();
c385e6e4 845 }
8d04fb55 846
cfb2d02b 847 /* Finally, check if we need to exit to the main loop. */
d73415a3 848 if (unlikely(qatomic_read(&cpu->exit_request))
740b1759 849 || (icount_enabled()
a11bbb6a 850 && (cpu->cflags_next_tb == -1 || cpu->cflags_next_tb & CF_USE_ICOUNT)
5e140196 851 && cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra == 0)) {
d73415a3 852 qatomic_set(&cpu->exit_request, 0);
5f3bdfd4
PD
853 if (cpu->exception_index == -1) {
854 cpu->exception_index = EXCP_INTERRUPT;
855 }
209b71b6 856 return true;
c385e6e4 857 }
209b71b6
PB
858
859 return false;
c385e6e4
SF
860}
861
928de9ee 862static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
fbf59aad 863 target_ulong pc,
cfb2d02b 864 TranslationBlock **last_tb, int *tb_exit)
928de9ee 865{
1aab16c2 866 int32_t insns_left;
928de9ee 867
fbf59aad 868 trace_exec_tb(tb, pc);
eba40358 869 tb = cpu_tb_exec(cpu, tb, tb_exit);
1aab16c2
PB
870 if (*tb_exit != TB_EXIT_REQUESTED) {
871 *last_tb = tb;
872 return;
873 }
874
875 *last_tb = NULL;
d73415a3 876 insns_left = qatomic_read(&cpu_neg(cpu)->icount_decr.u32);
1aab16c2 877 if (insns_left < 0) {
e5143e30
AB
878 /* Something asked us to stop executing chained TBs; just
879 * continue round the main loop. Whatever requested the exit
30f3dda2 880 * will also have set something else (eg exit_request or
17b50b0c
PD
881 * interrupt_request) which will be handled by
882 * cpu_handle_interrupt. cpu_handle_interrupt will also
883 * clear cpu->icount_decr.u16.high.
928de9ee 884 */
1aab16c2 885 return;
928de9ee 886 }
1aab16c2
PB
887
888 /* Instruction counter expired. */
740b1759 889 assert(icount_enabled());
1aab16c2 890#ifndef CONFIG_USER_ONLY
eda5f7c6 891 /* Ensure global icount has gone forward */
8191d368 892 icount_update(cpu);
eda5f7c6 893 /* Refill decrementer and continue execution. */
df3a2de5 894 insns_left = MIN(0xffff, cpu->icount_budget);
5e140196 895 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
eda5f7c6 896 cpu->icount_extra = cpu->icount_budget - insns_left;
bc662a33
AB
897
898 /*
899 * If the next tb has more instructions than we have left to
900 * execute we need to ensure we find/generate a TB with exactly
901 * insns_left instructions in it.
902 */
c8cf47a9
PM
903 if (insns_left > 0 && insns_left < tb->icount) {
904 assert(insns_left <= CF_COUNT_MASK);
905 assert(cpu->icount_extra == 0);
bc662a33 906 cpu->cflags_next_tb = (tb->cflags & ~CF_COUNT_MASK) | insns_left;
928de9ee 907 }
1aab16c2 908#endif
928de9ee
SF
909}
910
7d13299d
FB
911/* main execution loop */
912
61710a7e
RH
913static int __attribute__((noinline))
914cpu_exec_loop(CPUState *cpu, SyncClocks *sc)
7d13299d 915{
c385e6e4 916 int ret;
4515e58d
PB
917
918 /* if an exception is pending, we execute it here */
919 while (!cpu_handle_exception(cpu, &ret)) {
920 TranslationBlock *last_tb = NULL;
921 int tb_exit = 0;
922
923 while (!cpu_handle_interrupt(cpu, &last_tb)) {
9b990ee5 924 TranslationBlock *tb;
11c1d5f8
RH
925 target_ulong cs_base, pc;
926 uint32_t flags, cflags;
927
10c37828
RH
928 cpu_get_tb_cpu_state(cpu->env_ptr, &pc, &cs_base, &flags);
929
11c1d5f8
RH
930 /*
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.
936 */
937 cflags = cpu->cflags_next_tb;
9b990ee5 938 if (cflags == -1) {
c0ae396a 939 cflags = curr_cflags(cpu);
9b990ee5
RH
940 } else {
941 cpu->cflags_next_tb = -1;
942 }
943
10c37828
RH
944 if (check_for_breakpoints(cpu, pc, &cflags)) {
945 break;
946 }
11c1d5f8
RH
947
948 tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
949 if (tb == NULL) {
a976a99a
RH
950 uint32_t h;
951
11c1d5f8
RH
952 mmap_lock();
953 tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
954 mmap_unlock();
955 /*
956 * We add the TB in the virtual pc hash table
957 * for the fast lookup
958 */
a976a99a 959 h = tb_jmp_cache_hash_func(pc);
8ed558ec 960 tb_jmp_cache_set(cpu->tb_jmp_cache, h, tb, pc);
11c1d5f8
RH
961 }
962
963#ifndef CONFIG_USER_ONLY
964 /*
965 * We don't take care of direct jumps when address mapping
966 * changes in system emulation. So it's not safe to make a
967 * direct jump to a TB spanning two pages because the mapping
968 * for the second page can change.
969 */
28905cfb 970 if (tb_page_addr1(tb) != -1) {
11c1d5f8
RH
971 last_tb = NULL;
972 }
973#endif
974 /* See if we can patch the calling TB. */
975 if (last_tb) {
976 tb_add_jump(last_tb, tb_exit, tb);
977 }
978
fbf59aad 979 cpu_loop_exec_tb(cpu, tb, pc, &last_tb, &tb_exit);
11c1d5f8 980
882f5b1b 981 QEMU_PLUGIN_ASSERT(cpu->plugin_mem_cbs == NULL);
4515e58d
PB
982 /* Try to align the host and virtual clocks
983 if the guest is in advance */
61710a7e
RH
984 align_clocks(sc, cpu);
985 }
986 }
987 return ret;
988}
989
990static int cpu_exec_setjmp(CPUState *cpu, SyncClocks *sc)
991{
992 /* Prepare setjmp context for exception handling. */
993 if (unlikely(sigsetjmp(cpu->jmp_env, 0) != 0)) {
994 /* Non-buggy compilers preserve this; assert the correct value. */
995 g_assert(cpu == current_cpu);
996
997#ifndef CONFIG_SOFTMMU
998 clear_helper_retaddr();
999 if (have_mmap_lock()) {
1000 mmap_unlock();
7d13299d 1001 }
61710a7e
RH
1002#endif
1003 if (qemu_mutex_iothread_locked()) {
1004 qemu_mutex_unlock_iothread();
1005 }
1006 qemu_plugin_disable_mem_helpers(cpu);
1007
1008 assert_no_pages_locked();
1009 }
1010
1011 return cpu_exec_loop(cpu, sc);
1012}
1013
1014int cpu_exec(CPUState *cpu)
1015{
1016 int ret;
1017 SyncClocks sc = { 0 };
1018
1019 /* replay_interrupt may need current_cpu */
1020 current_cpu = cpu;
1021
1022 if (cpu_handle_halt(cpu)) {
1023 return EXCP_HALTED;
4515e58d 1024 }
3fb2ded1 1025
61710a7e
RH
1026 rcu_read_lock();
1027 cpu_exec_enter(cpu);
1028
1029 /*
1030 * Calculate difference between guest clock and host clock.
1031 * This delay includes the delay of the last cycle, so
1032 * what we have to do is sleep until it is 0. As for the
1033 * advance/delay we gain here, we try to fix it next time.
1034 */
1035 init_delay_params(&sc, cpu);
1036
1037 ret = cpu_exec_setjmp(cpu, &sc);
1038
035ba06c 1039 cpu_exec_exit(cpu);
79e2b9ae 1040 rcu_read_unlock();
1057eaa7 1041
7d13299d
FB
1042 return ret;
1043}
740b1759 1044
7df5e3d6
CF
1045void tcg_exec_realizefn(CPUState *cpu, Error **errp)
1046{
1047 static bool tcg_target_initialized;
1048 CPUClass *cc = CPU_GET_CLASS(cpu);
1049
1050 if (!tcg_target_initialized) {
78271684 1051 cc->tcg_ops->initialize();
7df5e3d6
CF
1052 tcg_target_initialized = true;
1053 }
7df5e3d6 1054
4e4fa6c1
RH
1055 cpu->tb_jmp_cache = g_new0(CPUJumpCache, 1);
1056 tlb_init(cpu);
7df5e3d6
CF
1057#ifndef CONFIG_USER_ONLY
1058 tcg_iommu_init_notifier_list(cpu);
1059#endif /* !CONFIG_USER_ONLY */
4e4fa6c1 1060 /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */
7df5e3d6
CF
1061}
1062
1063/* undo the initializations in reverse order */
1064void tcg_exec_unrealizefn(CPUState *cpu)
1065{
1066#ifndef CONFIG_USER_ONLY
1067 tcg_iommu_free_notifier_list(cpu);
1068#endif /* !CONFIG_USER_ONLY */
1069
7df5e3d6 1070 tlb_destroy(cpu);
4731f89b 1071 g_free_rcu(cpu->tb_jmp_cache, rcu);
7df5e3d6 1072}