blob: 8163295f34bc2bbbf593e8cd56bb0d67af121c80 [file] [log] [blame]
bellard7d132992003-03-06 23:23:54 +00001/*
陳韋任e965fc32012-02-06 14:02:55 +08002 * emulator main execution loop
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard66321a12005-04-06 20:47:48 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellard7d132992003-03-06 23:23:54 +00005 *
bellard3ef693a2003-03-23 20:17:16 +00006 * 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
Thomas Huthfb0343d2019-01-23 15:08:56 +01009 * version 2.1 of the License, or (at your option) any later version.
bellard7d132992003-03-06 23:23:54 +000010 *
bellard3ef693a2003-03-23 20:17:16 +000011 * 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.
bellard7d132992003-03-06 23:23:54 +000015 *
bellard3ef693a2003-03-23 20:17:16 +000016 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellard7d132992003-03-06 23:23:54 +000018 */
Markus Armbrustera8d25322019-05-23 16:35:08 +020019
Peter Maydell7b31bbc2016-01-26 18:16:56 +000020#include "qemu/osdep.h"
Claudio Fontana740b1752020-08-19 13:17:19 +020021#include "qemu/qemu-print.h"
Daniel P. Berrangé3a841ab2021-09-08 10:35:43 +010022#include "qapi/error.h"
Daniel P. Berrangé3a841ab2021-09-08 10:35:43 +010023#include "qapi/type-helpers.h"
Claudio Fontana78271682021-02-04 17:39:23 +010024#include "hw/core/tcg-cpu-ops.h"
Yang Zhongd9bb58e2017-06-02 14:06:44 +080025#include "trace.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020026#include "disas/disas.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010027#include "exec/exec-all.h"
Philippe Mathieu-Daudédcb32f12020-01-01 12:23:00 +010028#include "tcg/tcg.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/atomic.h"
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +010030#include "qemu/rcu.h"
Paolo Bonzini508127e2016-01-07 16:55:28 +030031#include "exec/log.h"
Jan Kiszka8d04fb52017-02-23 18:29:11 +000032#include "qemu/main-loop.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010033#include "sysemu/cpus.h"
Claudio Fontana740b1752020-08-19 13:17:19 +020034#include "exec/cpu-all.h"
35#include "sysemu/cpu-timers.h"
Philippe Mathieu-Daudé5b5968c2022-12-19 18:09:43 +010036#include "exec/replay-core.h"
Daniel P. Berrangé3a841ab2021-09-08 10:35:43 +010037#include "sysemu/tcg.h"
Philippe Mathieu-Daudéa3e7f702023-06-11 10:58:22 +020038#include "exec/helper-proto-common.h"
Richard Hendersona976a992022-08-15 15:13:05 -050039#include "tb-jmp-cache.h"
Philippe Mathieu-Daudée5ceadf2021-05-24 19:04:53 +020040#include "tb-hash.h"
Philippe Mathieu-Daudée5ceadf2021-05-24 19:04:53 +020041#include "tb-context.h"
Philippe Mathieu-Daudé59346602023-09-14 20:57:15 +020042#include "internal-common.h"
Philippe Mathieu-Daudé4c268d62023-09-14 20:57:14 +020043#include "internal-target.h"
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +020044
45/* -icount align implementation. */
46
47typedef struct SyncClocks {
48 int64_t diff_clk;
49 int64_t last_cpu_icount;
Sebastian Tanase7f7bc142014-07-25 11:56:32 +020050 int64_t realtime_clock;
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +020051} SyncClocks;
52
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
56 * oscillate around 0.
57 */
58#define VM_CLOCK_ADVANCE 3000000
Sebastian Tanase7f7bc142014-07-25 11:56:32 +020059#define THRESHOLD_REDUCE 1.5
60#define MAX_DELAY_PRINT_RATE 2000000000LL
61#define MAX_NB_PRINTS 100
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +020062
Philippe Mathieu-Daudé00c9a5c2022-12-19 18:09:40 +010063int64_t max_delay;
64int64_t max_advance;
Claudio Fontana740b1752020-08-19 13:17:19 +020065
Richard Henderson5e140192019-03-28 11:54:23 -100066static void align_clocks(SyncClocks *sc, CPUState *cpu)
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +020067{
68 int64_t cpu_icount;
69
70 if (!icount_align_option) {
71 return;
72 }
73
Richard Hendersona953b5f2023-09-13 15:46:45 -070074 cpu_icount = cpu->icount_extra + cpu->neg.icount_decr.u16.low;
Claudio Fontana8191d362020-08-31 16:18:34 +020075 sc->diff_clk += icount_to_ns(sc->last_cpu_icount - cpu_icount);
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +020076 sc->last_cpu_icount = cpu_icount;
77
78 if (sc->diff_clk > VM_CLOCK_ADVANCE) {
79#ifndef _WIN32
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) {
Paolo Bonzinia498d0e2015-01-28 10:09:55 +010084 sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec;
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +020085 } else {
86 sc->diff_clk = 0;
87 }
88#else
89 Sleep(sc->diff_clk / SCALE_MS);
90 sc->diff_clk = 0;
91#endif
92 }
93}
94
Sebastian Tanase7f7bc142014-07-25 11:56:32 +020095static void print_delay(const SyncClocks *sc)
96{
97 static float threshold_delay;
98 static int64_t last_realtime_clock;
99 static int nb_prints;
100
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;
Claudio Fontana740b1752020-08-19 13:17:19 +0200108 qemu_printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
109 threshold_delay - 1,
110 threshold_delay);
Sebastian Tanase7f7bc142014-07-25 11:56:32 +0200111 nb_prints++;
112 last_realtime_clock = sc->realtime_clock;
113 }
114 }
115}
116
Richard Henderson5e140192019-03-28 11:54:23 -1000117static void init_delay_params(SyncClocks *sc, CPUState *cpu)
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +0200118{
119 if (!icount_align_option) {
120 return;
121 }
Paolo Bonzini2e91cc62015-01-28 10:16:37 +0100122 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;
Richard Henderson5e140192019-03-28 11:54:23 -1000124 sc->last_cpu_icount
Richard Hendersona953b5f2023-09-13 15:46:45 -0700125 = cpu->icount_extra + cpu->neg.icount_decr.u16.low;
Sebastian Tanase27498be2014-07-25 11:56:33 +0200126 if (sc->diff_clk < max_delay) {
127 max_delay = sc->diff_clk;
128 }
129 if (sc->diff_clk > max_advance) {
130 max_advance = sc->diff_clk;
131 }
Sebastian Tanase7f7bc142014-07-25 11:56:32 +0200132
133 /* Print every 2s max if the guest is late. We limit the number
134 of printed messages to NB_PRINT_MAX(currently 100) */
135 print_delay(sc);
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +0200136}
137#else
138static void align_clocks(SyncClocks *sc, const CPUState *cpu)
139{
140}
141
142static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
143{
144}
145#endif /* CONFIG USER ONLY */
bellard7d132992003-03-06 23:23:54 +0000146
Philippe Mathieu-Daudéb254c342024-01-10 18:09:56 +0100147bool tcg_cflags_has(CPUState *cpu, uint32_t flags)
148{
149 return cpu->tcg_cflags & flags;
150}
151
152void tcg_cflags_set(CPUState *cpu, uint32_t flags)
153{
154 cpu->tcg_cflags |= flags;
155}
156
Richard Henderson043e35d2021-07-17 15:18:40 -0700157uint32_t curr_cflags(CPUState *cpu)
158{
Richard Henderson84f156162021-07-17 15:18:41 -0700159 uint32_t cflags = cpu->tcg_cflags;
160
Richard Henderson04f5b642021-07-17 15:18:43 -0700161 /*
Richard Hendersonc2ffd752021-07-19 10:43:46 -1000162 * Record gdb single-step. We should be exiting the TB by raising
163 * EXCP_DEBUG, but to simplify other tests, disable chaining too.
164 *
Richard Henderson04f5b642021-07-17 15:18:43 -0700165 * For singlestep and -d nochain, suppress goto_tb so that
166 * we can log -d cpu,exec after every TB.
167 */
Richard Hendersonc2ffd752021-07-19 10:43:46 -1000168 if (unlikely(cpu->singlestep_enabled)) {
169 cflags |= CF_NO_GOTO_TB | CF_NO_GOTO_PTR | CF_SINGLE_STEP | 1;
Peter Maydell0e339282023-04-17 17:40:34 +0100170 } else if (qatomic_read(&one_insn_per_tb)) {
Richard Henderson04f5b642021-07-17 15:18:43 -0700171 cflags |= CF_NO_GOTO_TB | 1;
172 } else if (qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) {
Richard Hendersonfb957012021-07-17 15:18:42 -0700173 cflags |= CF_NO_GOTO_TB;
Richard Henderson84f156162021-07-17 15:18:41 -0700174 }
175
176 return cflags;
Richard Henderson043e35d2021-07-17 15:18:40 -0700177}
178
Richard Henderson0c90ba12022-08-16 13:53:18 -0500179struct tb_desc {
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200180 vaddr pc;
181 uint64_t cs_base;
Richard Henderson0c90ba12022-08-16 13:53:18 -0500182 CPUArchState *env;
Richard Henderson93b99612022-08-15 15:00:57 -0500183 tb_page_addr_t page_addr0;
Richard Henderson0c90ba12022-08-16 13:53:18 -0500184 uint32_t flags;
185 uint32_t cflags;
Richard Henderson0c90ba12022-08-16 13:53:18 -0500186};
187
188static bool tb_lookup_cmp(const void *p, const void *d)
189{
190 const TranslationBlock *tb = p;
191 const struct tb_desc *desc = d;
192
Anton Johansson279513c2023-02-27 14:51:47 +0100193 if ((tb_cflags(tb) & CF_PCREL || tb->pc == desc->pc) &&
Richard Henderson28905cf2022-09-20 13:21:40 +0200194 tb_page_addr0(tb) == desc->page_addr0 &&
Richard Henderson0c90ba12022-08-16 13:53:18 -0500195 tb->cs_base == desc->cs_base &&
196 tb->flags == desc->flags &&
Richard Henderson0c90ba12022-08-16 13:53:18 -0500197 tb_cflags(tb) == desc->cflags) {
198 /* check next page if needed */
Richard Henderson28905cf2022-09-20 13:21:40 +0200199 tb_page_addr_t tb_phys_page1 = tb_page_addr1(tb);
200 if (tb_phys_page1 == -1) {
Richard Henderson0c90ba12022-08-16 13:53:18 -0500201 return true;
202 } else {
Richard Henderson93b99612022-08-15 15:00:57 -0500203 tb_page_addr_t phys_page1;
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200204 vaddr virt_page1;
Richard Henderson0c90ba12022-08-16 13:53:18 -0500205
Richard Henderson9867b302022-08-22 18:50:46 -0700206 /*
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.
214 */
Richard Henderson93b99612022-08-15 15:00:57 -0500215 virt_page1 = TARGET_PAGE_ALIGN(desc->pc);
216 phys_page1 = get_page_addr_code(desc->env, virt_page1);
Richard Henderson28905cf2022-09-20 13:21:40 +0200217 if (tb_phys_page1 == phys_page1) {
Richard Henderson0c90ba12022-08-16 13:53:18 -0500218 return true;
219 }
220 }
221 }
222 return false;
223}
224
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200225static TranslationBlock *tb_htable_lookup(CPUState *cpu, vaddr pc,
226 uint64_t cs_base, uint32_t flags,
Richard Henderson0c90ba12022-08-16 13:53:18 -0500227 uint32_t cflags)
228{
229 tb_page_addr_t phys_pc;
230 struct tb_desc desc;
231 uint32_t h;
232
Richard Hendersonb77af262023-09-13 17:22:49 -0700233 desc.env = cpu_env(cpu);
Richard Henderson0c90ba12022-08-16 13:53:18 -0500234 desc.cs_base = cs_base;
235 desc.flags = flags;
236 desc.cflags = cflags;
Richard Henderson0c90ba12022-08-16 13:53:18 -0500237 desc.pc = pc;
238 phys_pc = get_page_addr_code(desc.env, pc);
239 if (phys_pc == -1) {
240 return NULL;
241 }
Richard Henderson93b99612022-08-15 15:00:57 -0500242 desc.page_addr0 = phys_pc;
Anton Johansson4be79022023-02-27 14:51:39 +0100243 h = tb_hash_func(phys_pc, (cflags & CF_PCREL ? 0 : pc),
Alex Bennée367189e2023-05-26 17:54:01 +0100244 flags, cs_base, cflags);
Richard Henderson0c90ba12022-08-16 13:53:18 -0500245 return qht_lookup_custom(&tb_ctx.htable, &desc, h, tb_lookup_cmp);
246}
247
Richard Henderson632cb632021-06-29 12:31:19 -0700248/* Might cause an exception, so have a longjmp destination ready */
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200249static inline TranslationBlock *tb_lookup(CPUState *cpu, vaddr pc,
250 uint64_t cs_base, uint32_t flags,
251 uint32_t cflags)
Richard Henderson632cb632021-06-29 12:31:19 -0700252{
253 TranslationBlock *tb;
Richard Henderson8ed558e2022-08-12 09:53:53 -0700254 CPUJumpCache *jc;
Richard Henderson632cb632021-06-29 12:31:19 -0700255 uint32_t hash;
256
257 /* we should never be trying to look up an INVALID tb */
258 tcg_debug_assert(!(cflags & CF_INVALID));
259
260 hash = tb_jmp_cache_hash_func(pc);
Richard Henderson8ed558e2022-08-12 09:53:53 -0700261 jc = cpu->tb_jmp_cache;
Richard Henderson632cb632021-06-29 12:31:19 -0700262
Paolo Bonzinid157e542024-01-22 16:34:09 +0100263 tb = qatomic_read(&jc->array[hash].tb);
264 if (likely(tb &&
265 jc->array[hash].pc == pc &&
266 tb->cs_base == cs_base &&
267 tb->flags == flags &&
268 tb_cflags(tb) == cflags)) {
269 goto hit;
Richard Henderson632cb632021-06-29 12:31:19 -0700270 }
Anton Johansson2dd5b7a2023-02-27 14:51:46 +0100271
Paolo Bonzinid157e542024-01-22 16:34:09 +0100272 tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags);
273 if (tb == NULL) {
274 return NULL;
275 }
276
277 jc->array[hash].pc = pc;
278 qatomic_set(&jc->array[hash].tb, tb);
279
280hit:
281 /*
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.
284 */
285 assert((tb_cflags(tb) & CF_PCREL) || tb->pc == pc);
Richard Henderson632cb632021-06-29 12:31:19 -0700286 return tb;
287}
288
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200289static void log_cpu_exec(vaddr pc, CPUState *cpu,
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500290 const TranslationBlock *tb)
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700291{
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500292 if (qemu_log_in_addr_range(pc)) {
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700293 qemu_log_mask(CPU_LOG_EXEC,
Richard Henderson85314e12023-04-01 15:28:18 -0700294 "Trace %d: %p [%08" PRIx64
Peter Maydelle60a7d02023-07-17 11:05:08 +0100295 "/%016" VADDR_PRIx "/%08x/%08x] %s\n",
Richard Henderson7eabad32021-06-30 08:31:46 -0700296 cpu->cpu_index, tb->tc.ptr, tb->cs_base, pc,
297 tb->flags, tb->cflags, lookup_symbol(pc));
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700298
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700299 if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
Richard Hendersonc60f5992022-04-17 11:29:47 -0700300 FILE *logfile = qemu_log_trylock();
Richard Henderson78b54852022-04-17 11:29:49 -0700301 if (logfile) {
302 int flags = 0;
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700303
Richard Henderson78b54852022-04-17 11:29:49 -0700304 if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) {
305 flags |= CPU_DUMP_FPU;
306 }
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700307#if defined(TARGET_I386)
Richard Henderson78b54852022-04-17 11:29:49 -0700308 flags |= CPU_DUMP_CCOP;
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700309#endif
Ivan Klokovb84694d2023-04-10 15:44:50 +0300310 if (qemu_loglevel_mask(CPU_LOG_TB_VPU)) {
311 flags |= CPU_DUMP_VPU;
312 }
Richard Hendersonc769fbd2022-04-17 11:29:54 -0700313 cpu_dump_state(cpu, logfile, flags);
Richard Henderson78b54852022-04-17 11:29:49 -0700314 qemu_log_unlock(logfile);
315 }
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700316 }
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700317 }
318}
319
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200320static bool check_for_breakpoints_slow(CPUState *cpu, vaddr pc,
Leandro Lupori69993c42022-10-25 17:24:22 -0300321 uint32_t *cflags)
Richard Henderson10c37822021-07-19 09:03:21 -1000322{
323 CPUBreakpoint *bp;
324 bool match_page = false;
325
Richard Henderson10c37822021-07-19 09:03:21 -1000326 /*
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.
330 *
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.
334 */
335 if (cpu->singlestep_enabled) {
336 return false;
337 }
338
339 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
340 /*
341 * If we have an exact pc match, trigger the breakpoint.
342 * Otherwise, note matches within the page.
343 */
344 if (pc == bp->pc) {
345 bool match_bp = false;
346
347 if (bp->flags & BP_GDB) {
348 match_bp = true;
349 } else if (bp->flags & BP_CPU) {
350#ifdef CONFIG_USER_ONLY
351 g_assert_not_reached();
352#else
Richard Henderson991bd652024-01-28 12:57:59 +1000353 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
354 assert(tcg_ops->debug_check_breakpoint);
355 match_bp = tcg_ops->debug_check_breakpoint(cpu);
Richard Henderson10c37822021-07-19 09:03:21 -1000356#endif
357 }
358
359 if (match_bp) {
360 cpu->exception_index = EXCP_DEBUG;
361 return true;
362 }
363 } else if (((pc ^ bp->pc) & TARGET_PAGE_MASK) == 0) {
364 match_page = true;
365 }
366 }
367
368 /*
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.
372 *
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.
379 */
380 if (match_page) {
Richard Hendersond828b922024-03-21 16:54:11 -1000381 *cflags = (*cflags & ~CF_COUNT_MASK) | CF_NO_GOTO_TB | CF_BP_PAGE | 1;
Richard Henderson10c37822021-07-19 09:03:21 -1000382 }
383 return false;
384}
385
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200386static inline bool check_for_breakpoints(CPUState *cpu, vaddr pc,
Leandro Lupori69993c42022-10-25 17:24:22 -0300387 uint32_t *cflags)
388{
389 return unlikely(!QTAILQ_EMPTY(&cpu->breakpoints)) &&
390 check_for_breakpoints_slow(cpu, pc, cflags);
391}
392
Richard Henderson4288eb22021-06-29 12:28:29 -0700393/**
394 * helper_lookup_tb_ptr: quick check for next tb
395 * @env: current cpu state
396 *
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.
400 */
401const void *HELPER(lookup_tb_ptr)(CPUArchState *env)
402{
403 CPUState *cpu = env_cpu(env);
404 TranslationBlock *tb;
Anton Johanssonbb5de522023-06-21 15:56:24 +0200405 vaddr pc;
406 uint64_t cs_base;
Richard Henderson10c37822021-07-19 09:03:21 -1000407 uint32_t flags, cflags;
Richard Henderson4288eb22021-06-29 12:28:29 -0700408
Peter Maydell62bcba82024-02-19 17:31:51 +0000409 /*
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.
415 */
416 cpu->neg.can_do_io = true;
Richard Henderson4288eb22021-06-29 12:28:29 -0700417 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
418
Richard Henderson10c37822021-07-19 09:03:21 -1000419 cflags = curr_cflags(cpu);
420 if (check_for_breakpoints(cpu, pc, &cflags)) {
421 cpu_loop_exit(cpu);
422 }
423
424 tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
Richard Henderson4288eb22021-06-29 12:28:29 -0700425 if (tb == NULL) {
426 return tcg_code_gen_epilogue;
427 }
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700428
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500429 if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
430 log_cpu_exec(pc, cpu, tb);
431 }
Richard Hendersonabb0cd92021-06-29 13:17:18 -0700432
Richard Henderson4288eb22021-06-29 12:28:29 -0700433 return tb->tc.ptr;
434}
435
Peter Maydell77211372013-02-22 18:10:02 +0000436/* Execute a TB, and fix up the CPU state afterwards if necessary */
Daniele Buonoc905a362020-12-04 18:06:12 -0500437/*
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
445 */
Richard Hendersoneba40352020-10-29 13:18:12 -0700446static inline TranslationBlock * QEMU_DISABLE_CFI
447cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit)
Peter Maydell77211372013-02-22 18:10:02 +0000448{
Sergey Fedorov819af242016-04-21 15:58:23 +0300449 uintptr_t ret;
450 TranslationBlock *last_tb;
Richard Hendersondb0c51a2020-10-28 12:05:44 -0700451 const void *tb_ptr = itb->tc.ptr;
Peter Maydell1a830632016-03-15 14:30:19 +0000452
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500453 if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
454 log_cpu_exec(log_pc(cpu, itb), cpu, itb);
455 }
Richard Henderson03afa5f2013-11-06 17:29:39 +1000456
Roman Bolshakov653b87e2021-01-13 06:28:07 +0300457 qemu_thread_jit_execute();
Philippe Mathieu-Daudé94956d72024-01-29 17:44:46 +0100458 ret = tcg_qemu_tb_exec(cpu_env(cpu), tb_ptr);
Richard Henderson464dacf2023-09-15 15:41:39 -0700459 cpu->neg.can_do_io = true;
Richard Hendersone04660a2023-03-15 17:43:10 +0000460 qemu_plugin_disable_mem_helpers(cpu);
Richard Hendersoneba40352020-10-29 13:18:12 -0700461 /*
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.
468 */
469 last_tb = tcg_splitwx_to_rw((void *)(ret & ~TB_EXIT_MASK));
470 *tb_exit = ret & TB_EXIT_MASK;
Alex Bennée6db8b532014-08-01 17:08:57 +0100471
Richard Hendersoneba40352020-10-29 13:18:12 -0700472 trace_exec_tb_exit(last_tb, *tb_exit);
473
474 if (*tb_exit > TB_EXIT_IDX1) {
Peter Maydell77211372013-02-22 18:10:02 +0000475 /* 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.
478 */
Richard Henderson991bd652024-01-28 12:57:59 +1000479 CPUClass *cc = cpu->cc;
480 const TCGCPUOps *tcg_ops = cc->tcg_ops;
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500481
Richard Henderson991bd652024-01-28 12:57:59 +1000482 if (tcg_ops->synchronize_from_tb) {
483 tcg_ops->synchronize_from_tb(cpu, last_tb);
Andreas Färberbdf7ae52013-06-28 19:31:32 +0200484 } else {
Anton Johansson4be79022023-02-27 14:51:39 +0100485 tcg_debug_assert(!(tb_cflags(last_tb) & CF_PCREL));
Andreas Färberbdf7ae52013-06-28 19:31:32 +0200486 assert(cc->set_pc);
Anton Johansson279513c2023-02-27 14:51:47 +0100487 cc->set_pc(cpu, last_tb->pc);
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500488 }
489 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200490 vaddr pc = log_pc(cpu, last_tb);
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500491 if (qemu_log_in_addr_range(pc)) {
Peter Maydelle60a7d02023-07-17 11:05:08 +0100492 qemu_log("Stopped execution of TB chain before %p [%016"
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200493 VADDR_PRIx "] %s\n",
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500494 last_tb->tc.ptr, pc, lookup_symbol(pc));
495 }
Andreas Färberbdf7ae52013-06-28 19:31:32 +0200496 }
Peter Maydell77211372013-02-22 18:10:02 +0000497 }
Richard Hendersonc9460d72021-07-18 15:12:12 -1000498
499 /*
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.
503 */
504 if (unlikely(cpu->singlestep_enabled) && cpu->exception_index == -1) {
505 cpu->exception_index = EXCP_DEBUG;
506 cpu_loop_exit(cpu);
507 }
508
Richard Hendersoneba40352020-10-29 13:18:12 -0700509 return last_tb;
Peter Maydell77211372013-02-22 18:10:02 +0000510}
511
pbrook2e70f6e2008-06-29 01:03:05 +0000512
Eduardo Habkost035ba062020-12-12 16:55:16 +0100513static void cpu_exec_enter(CPUState *cpu)
Richard Hendersonfdbc2b52016-06-29 22:12:55 -0700514{
Richard Henderson991bd652024-01-28 12:57:59 +1000515 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
Eduardo Habkost035ba062020-12-12 16:55:16 +0100516
Richard Henderson991bd652024-01-28 12:57:59 +1000517 if (tcg_ops->cpu_exec_enter) {
518 tcg_ops->cpu_exec_enter(cpu);
Eduardo Habkost80c47502020-12-12 16:55:17 +0100519 }
Eduardo Habkost035ba062020-12-12 16:55:16 +0100520}
521
522static void cpu_exec_exit(CPUState *cpu)
523{
Richard Henderson991bd652024-01-28 12:57:59 +1000524 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
Eduardo Habkost035ba062020-12-12 16:55:16 +0100525
Richard Henderson991bd652024-01-28 12:57:59 +1000526 if (tcg_ops->cpu_exec_exit) {
527 tcg_ops->cpu_exec_exit(cpu);
Eduardo Habkost80c47502020-12-12 16:55:17 +0100528 }
Eduardo Habkost035ba062020-12-12 16:55:16 +0100529}
530
Richard Hendersoncb62bd12023-07-06 08:45:13 +0100531static void cpu_exec_longjmp_cleanup(CPUState *cpu)
532{
533 /* Non-buggy compilers preserve this; assert the correct value. */
534 g_assert(cpu == current_cpu);
535
536#ifdef CONFIG_USER_ONLY
537 clear_helper_retaddr();
538 if (have_mmap_lock()) {
539 mmap_unlock();
540 }
Richard Hendersondeba7872023-07-06 17:55:48 +0100541#else
542 /*
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
546 * the translation.
547 *
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.
552 *
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.
556 */
557 if (tcg_ctx->gen_tb) {
558 tb_unlock_pages(tcg_ctx->gen_tb);
559 tcg_ctx->gen_tb = NULL;
560 }
Richard Hendersoncb62bd12023-07-06 08:45:13 +0100561#endif
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500562 if (bql_locked()) {
563 bql_unlock();
Richard Hendersoncb62bd12023-07-06 08:45:13 +0100564 }
565 assert_no_pages_locked();
566}
567
Eduardo Habkost035ba062020-12-12 16:55:16 +0100568void cpu_exec_step_atomic(CPUState *cpu)
569{
Richard Hendersonb77af262023-09-13 17:22:49 -0700570 CPUArchState *env = cpu_env(cpu);
Richard Hendersonfdbc2b52016-06-29 22:12:55 -0700571 TranslationBlock *tb;
Anton Johanssonbb5de522023-06-21 15:56:24 +0200572 vaddr pc;
573 uint64_t cs_base;
Richard Henderson258afb42021-07-17 15:18:44 -0700574 uint32_t flags, cflags;
Richard Hendersoneba40352020-10-29 13:18:12 -0700575 int tb_exit;
Richard Hendersonfdbc2b52016-06-29 22:12:55 -0700576
Pranith Kumar08e73c42017-02-23 18:29:15 +0000577 if (sigsetjmp(cpu->jmp_env, 0) == 0) {
Alex Bennée886cc682020-02-14 14:49:52 +0000578 start_exclusive();
Douglas Crosherbfff0722020-09-22 17:42:41 +1000579 g_assert(cpu == current_cpu);
580 g_assert(!cpu->running);
581 cpu->running = true;
Alex Bennée886cc682020-02-14 14:49:52 +0000582
Alex Bennée6f04cb12021-02-24 16:58:07 +0000583 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
Alex Bennée6f04cb12021-02-24 16:58:07 +0000584
Richard Henderson258afb42021-07-17 15:18:44 -0700585 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;
Richard Henderson10c37822021-07-19 09:03:21 -1000590 /*
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.
595 */
Richard Henderson258afb42021-07-17 15:18:44 -0700596
597 tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
Emilio G. Cota4e2ca832017-07-11 14:29:37 -0400598 if (tb == NULL) {
599 mmap_lock();
Emilio G. Cota95590e22017-08-01 15:40:16 -0400600 tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
Emilio G. Cota4e2ca832017-07-11 14:29:37 -0400601 mmap_unlock();
602 }
Pranith Kumar08e73c42017-02-23 18:29:15 +0000603
Eduardo Habkost035ba062020-12-12 16:55:16 +0100604 cpu_exec_enter(cpu);
Pranith Kumar08e73c42017-02-23 18:29:15 +0000605 /* execute the generated code */
Emilio G. Cota4e2ca832017-07-11 14:29:37 -0400606 trace_exec_tb(tb, pc);
Richard Hendersoneba40352020-10-29 13:18:12 -0700607 cpu_tb_exec(cpu, tb, &tb_exit);
Eduardo Habkost035ba062020-12-12 16:55:16 +0100608 cpu_exec_exit(cpu);
Pranith Kumar08e73c42017-02-23 18:29:15 +0000609 } else {
Richard Hendersoncb62bd12023-07-06 08:45:13 +0100610 cpu_exec_longjmp_cleanup(cpu);
Pranith Kumar08e73c42017-02-23 18:29:15 +0000611 }
Peter Maydell426eeec2017-11-02 16:35:36 +0000612
Alex Bennée886cc682020-02-14 14:49:52 +0000613 /*
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
616 * the execution.
617 */
618 g_assert(cpu_in_exclusive_context(cpu));
Douglas Crosherbfff0722020-09-22 17:42:41 +1000619 cpu->running = false;
Alex Bennée886cc682020-02-14 14:49:52 +0000620 end_exclusive();
Richard Hendersonfdbc2b52016-06-29 22:12:55 -0700621}
622
Richard Hendersona8583392017-07-31 22:02:31 -0700623void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr)
624{
Richard Henderson2fd2e782022-12-05 16:55:40 -0600625 /*
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].
629 */
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;
634
Richard Henderson9da60792022-11-26 18:54:23 -0800635 tb->jmp_target_addr[n] = addr;
Richard Henderson2fd2e782022-12-05 16:55:40 -0600636 tb_target_set_jmp_target(c_tb, n, jmp_rx, jmp_rw);
Richard Hendersona8583392017-07-31 22:02:31 -0700637}
638
Richard Hendersona8583392017-07-31 22:02:31 -0700639static inline void tb_add_jump(TranslationBlock *tb, int n,
640 TranslationBlock *tb_next)
641{
Emilio G. Cota194125e2017-08-02 20:34:06 -0400642 uintptr_t old;
643
Roman Bolshakov653b87e2021-01-13 06:28:07 +0300644 qemu_thread_jit_write();
Richard Hendersona8583392017-07-31 22:02:31 -0700645 assert(n < ARRAY_SIZE(tb->jmp_list_next));
Emilio G. Cota194125e2017-08-02 20:34:06 -0400646 qemu_spin_lock(&tb_next->jmp_lock);
647
648 /* make sure the destination TB is valid */
649 if (tb_next->cflags & CF_INVALID) {
650 goto out_unlock_next;
Richard Hendersona8583392017-07-31 22:02:31 -0700651 }
Emilio G. Cota194125e2017-08-02 20:34:06 -0400652 /* Atomically claim the jump destination slot only if it was NULL */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100653 old = qatomic_cmpxchg(&tb->jmp_dest[n], (uintptr_t)NULL,
654 (uintptr_t)tb_next);
Emilio G. Cota194125e2017-08-02 20:34:06 -0400655 if (old) {
656 goto out_unlock_next;
657 }
658
659 /* patch the native jump address */
660 tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc.ptr);
661
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;
665
666 qemu_spin_unlock(&tb_next->jmp_lock);
667
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500668 qemu_log_mask(CPU_LOG_EXEC, "Linking TBs %p index %d -> %p\n",
669 tb->tc.ptr, n, tb_next->tc.ptr);
Emilio G. Cota194125e2017-08-02 20:34:06 -0400670 return;
Richard Hendersona8583392017-07-31 22:02:31 -0700671
Emilio G. Cota194125e2017-08-02 20:34:06 -0400672 out_unlock_next:
673 qemu_spin_unlock(&tb_next->jmp_lock);
674 return;
Richard Hendersona8583392017-07-31 22:02:31 -0700675}
676
Sergey Fedorov8b2d34e2016-05-11 13:21:47 +0300677static inline bool cpu_handle_halt(CPUState *cpu)
678{
Philippe Mathieu-Daudé0596fa12021-09-12 19:27:02 +0200679#ifndef CONFIG_USER_ONLY
Sergey Fedorov8b2d34e2016-05-11 13:21:47 +0300680 if (cpu->halted) {
Philippe Mathieu-Daudéaa6fb652024-01-24 11:16:38 +0100681 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
Peter Maydell0487c632024-07-04 16:57:10 +0100682 bool leave_halt = tcg_ops->cpu_exec_halt(cpu);
Philippe Mathieu-Daudéaa6fb652024-01-24 11:16:38 +0100683
Peter Maydell408b2b32024-04-30 15:00:34 +0100684 if (!leave_halt) {
Sergey Fedorov8b2d34e2016-05-11 13:21:47 +0300685 return true;
686 }
687
688 cpu->halted = 0;
689 }
Philippe Mathieu-Daudé0596fa12021-09-12 19:27:02 +0200690#endif /* !CONFIG_USER_ONLY */
Sergey Fedorov8b2d34e2016-05-11 13:21:47 +0300691
692 return false;
693}
694
Sergey Fedorovea284762016-05-11 13:21:48 +0300695static inline void cpu_handle_debug_exception(CPUState *cpu)
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100696{
Richard Henderson991bd652024-01-28 12:57:59 +1000697 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100698 CPUWatchpoint *wp;
699
Andreas Färberff4700b2013-08-26 18:23:18 +0200700 if (!cpu->watchpoint_hit) {
701 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100702 wp->flags &= ~BP_WATCHPOINT_HIT;
703 }
704 }
Peter Maydell86025ee2014-09-12 14:06:48 +0100705
Richard Henderson991bd652024-01-28 12:57:59 +1000706 if (tcg_ops->debug_excp_handler) {
707 tcg_ops->debug_excp_handler(cpu);
Eduardo Habkost710384d2020-12-12 16:55:18 +0100708 }
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100709}
710
Sergey Fedorovea284762016-05-11 13:21:48 +0300711static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
712{
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300713 if (cpu->exception_index < 0) {
Sergey Fedorovea284762016-05-11 13:21:48 +0300714#ifndef CONFIG_USER_ONLY
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300715 if (replay_has_exception()
Richard Hendersona953b5f2023-09-13 15:46:45 -0700716 && cpu->neg.icount_decr.u16.low + cpu->icount_extra == 0) {
Alex Bennéea11bbb62021-02-13 13:03:19 +0000717 /* Execute just one insn to trigger exception pending in the log */
Pavel Dovgalyukc3e97f62022-01-31 14:25:40 +0300718 cpu->cflags_next_tb = (curr_cflags(cpu) & ~CF_USE_ICOUNT)
Richard Hendersoncf9b5792023-11-10 08:21:23 -0800719 | CF_NOIRQ | 1;
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300720 }
721#endif
Alex Bennéea11bbb62021-02-13 13:03:19 +0000722 return false;
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300723 }
Richard Henderson991bd652024-01-28 12:57:59 +1000724
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300725 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);
730 }
731 cpu->exception_index = -1;
Sergey Fedorovea284762016-05-11 13:21:48 +0300732 return true;
Richard Henderson991bd652024-01-28 12:57:59 +1000733 }
Luc Michela7ba7442020-07-16 21:39:47 +0200734
Richard Henderson991bd652024-01-28 12:57:59 +1000735#if defined(CONFIG_USER_ONLY)
736 /*
737 * If user mode only, we simulate a fake exception which will be
738 * handled outside the cpu execution loop.
739 */
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;
746 return true;
747#else
748 if (replay_exception()) {
749 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
750
751 bql_lock();
752 tcg_ops->do_interrupt(cpu);
753 bql_unlock();
754 cpu->exception_index = -1;
755
756 if (unlikely(cpu->singlestep_enabled)) {
757 /*
758 * After processing the exception, ensure an EXCP_DEBUG is
759 * raised when single-stepping so that GDB doesn't miss the
760 * next instruction.
761 */
762 *ret = EXCP_DEBUG;
763 cpu_handle_debug_exception(cpu);
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300764 return true;
765 }
Richard Henderson991bd652024-01-28 12:57:59 +1000766 } else if (!replay_has_interrupt()) {
767 /* give a chance to iothread in replay mode */
768 *ret = EXCP_INTERRUPT;
769 return true;
Sergey Fedorovea284762016-05-11 13:21:48 +0300770 }
Richard Henderson991bd652024-01-28 12:57:59 +1000771#endif
Sergey Fedorovea284762016-05-11 13:21:48 +0300772
773 return false;
774}
775
Philippe Mathieu-Daudé93c60912024-01-24 11:16:34 +0100776static inline bool icount_exit_request(CPUState *cpu)
777{
778 if (!icount_enabled()) {
779 return false;
780 }
781 if (cpu->cflags_next_tb != -1 && !(cpu->cflags_next_tb & CF_USE_ICOUNT)) {
782 return false;
783 }
784 return cpu->neg.icount_decr.u16.low + cpu->icount_extra == 0;
785}
786
Paolo Bonzini209b71b2017-01-27 10:57:18 +0100787static inline bool cpu_handle_interrupt(CPUState *cpu,
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300788 TranslationBlock **last_tb)
789{
Alex Bennéeaff0e202021-11-29 14:09:26 +0000790 /*
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.
794 */
795 if (cpu->cflags_next_tb != -1 && cpu->cflags_next_tb & CF_NOIRQ) {
796 return false;
797 }
798
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300799 /* Clear the interrupt flag now since we're processing
800 * cpu->interrupt_request and cpu->exit_request.
David Hildenbrandd84be022017-11-29 20:13:19 +0100801 * Ensure zeroing happens before reading cpu->exit_request or
802 * cpu->interrupt_request (see also smp_wmb in cpu_exit())
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300803 */
Richard Hendersona953b5f2023-09-13 15:46:45 -0700804 qatomic_set_mb(&cpu->neg.icount_decr.u16.high, 0);
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300805
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100806 if (unlikely(qatomic_read(&cpu->interrupt_request))) {
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000807 int interrupt_request;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500808 bql_lock();
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000809 interrupt_request = cpu->interrupt_request;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300810 if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
811 /* Mask out external interrupts for this step. */
812 interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
813 }
814 if (interrupt_request & CPU_INTERRUPT_DEBUG) {
815 cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
816 cpu->exception_index = EXCP_DEBUG;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500817 bql_unlock();
Paolo Bonzini209b71b2017-01-27 10:57:18 +0100818 return true;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300819 }
Philippe Mathieu-Daudé77c0fc42021-09-11 18:54:33 +0200820#if !defined(CONFIG_USER_ONLY)
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300821 if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
822 /* Do nothing */
823 } else if (interrupt_request & CPU_INTERRUPT_HALT) {
824 replay_interrupt();
825 cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
826 cpu->halted = 1;
827 cpu->exception_index = EXCP_HLT;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500828 bql_unlock();
Paolo Bonzini209b71b2017-01-27 10:57:18 +0100829 return true;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300830 }
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;
835 replay_interrupt();
Paolo Bonzini65c9d602017-02-16 12:30:05 +0100836 cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0, 0);
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300837 do_cpu_init(x86_cpu);
838 cpu->exception_index = EXCP_HALTED;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500839 bql_unlock();
Paolo Bonzini209b71b2017-01-27 10:57:18 +0100840 return true;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300841 }
842#else
843 else if (interrupt_request & CPU_INTERRUPT_RESET) {
844 replay_interrupt();
845 cpu_reset(cpu);
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500846 bql_unlock();
Paolo Bonzini209b71b2017-01-27 10:57:18 +0100847 return true;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300848 }
Philippe Mathieu-Daudé77c0fc42021-09-11 18:54:33 +0200849#endif /* !TARGET_I386 */
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300850 /* 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. */
854 else {
Richard Henderson991bd652024-01-28 12:57:59 +1000855 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
Philippe Mathieu-Daudé77c0fc42021-09-11 18:54:33 +0200856
Peter Maydellde680282024-07-12 12:39:49 +0100857 if (tcg_ops->cpu_exec_interrupt(cpu, interrupt_request)) {
Richard Hendersonb7e9a4a2024-01-28 13:12:54 +1000858 if (!tcg_ops->need_replay_interrupt ||
859 tcg_ops->need_replay_interrupt(interrupt_request)) {
Pavel Dovgalyuk40848932020-10-03 20:12:51 +0300860 replay_interrupt();
861 }
Richard Hendersonba3c35d2020-07-17 09:26:59 -0700862 /*
863 * After processing the interrupt, ensure an EXCP_DEBUG is
864 * raised when single-stepping so that GDB doesn't miss the
865 * next instruction.
866 */
Luc Michel5b7b1972022-02-24 14:52:42 -1000867 if (unlikely(cpu->singlestep_enabled)) {
868 cpu->exception_index = EXCP_DEBUG;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500869 bql_unlock();
Luc Michel5b7b1972022-02-24 14:52:42 -1000870 return true;
871 }
872 cpu->exception_index = -1;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300873 *last_tb = NULL;
874 }
Sergey Fedorov8b1fe3f2016-05-12 19:52:17 +0300875 /* The target hook may have updated the 'cpu->interrupt_request';
876 * reload the 'interrupt_request' value */
877 interrupt_request = cpu->interrupt_request;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300878 }
Philippe Mathieu-Daudé77c0fc42021-09-11 18:54:33 +0200879#endif /* !CONFIG_USER_ONLY */
Sergey Fedorov8b1fe3f2016-05-12 19:52:17 +0300880 if (interrupt_request & CPU_INTERRUPT_EXITTB) {
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300881 cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
882 /* ensure that no TB jump will be modified as
883 the program flow was changed */
884 *last_tb = NULL;
885 }
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000886
887 /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500888 bql_unlock();
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300889 }
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000890
Pavel Dovgalyukcfb2d022017-02-07 09:54:57 +0300891 /* Finally, check if we need to exit to the main loop. */
Philippe Mathieu-Daudé93c60912024-01-24 11:16:34 +0100892 if (unlikely(qatomic_read(&cpu->exit_request)) || icount_exit_request(cpu)) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100893 qatomic_set(&cpu->exit_request, 0);
Pavel Dovgalyuk5f3bdfd2018-02-27 12:51:41 +0300894 if (cpu->exception_index == -1) {
895 cpu->exception_index = EXCP_INTERRUPT;
896 }
Paolo Bonzini209b71b2017-01-27 10:57:18 +0100897 return true;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300898 }
Paolo Bonzini209b71b2017-01-27 10:57:18 +0100899
900 return false;
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300901}
902
Sergey Fedorov928de9e2016-05-11 13:21:50 +0300903static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
Anton Johanssonf0a08b02023-06-21 15:56:27 +0200904 vaddr pc, TranslationBlock **last_tb,
905 int *tb_exit)
Sergey Fedorov928de9e2016-05-11 13:21:50 +0300906{
Richard Hendersonfbf59aa2022-08-15 15:16:06 -0500907 trace_exec_tb(tb, pc);
Richard Hendersoneba40352020-10-29 13:18:12 -0700908 tb = cpu_tb_exec(cpu, tb, tb_exit);
Paolo Bonzini1aab16c2017-01-27 11:25:33 +0100909 if (*tb_exit != TB_EXIT_REQUESTED) {
910 *last_tb = tb;
911 return;
912 }
913
914 *last_tb = NULL;
Philippe Mathieu-Daudé0650fc12024-04-24 11:25:52 +0200915 if (cpu_loop_exit_requested(cpu)) {
Alex Bennéee5143e32017-02-23 18:29:12 +0000916 /* Something asked us to stop executing chained TBs; just
917 * continue round the main loop. Whatever requested the exit
Paolo Bonzini30f3dda2017-03-03 16:39:18 +0100918 * will also have set something else (eg exit_request or
Pavel Dovgalyuk17b50b02017-11-14 11:18:18 +0300919 * interrupt_request) which will be handled by
920 * cpu_handle_interrupt. cpu_handle_interrupt will also
921 * clear cpu->icount_decr.u16.high.
Sergey Fedorov928de9e2016-05-11 13:21:50 +0300922 */
Paolo Bonzini1aab16c2017-01-27 11:25:33 +0100923 return;
924 }
925
926 /* Instruction counter expired. */
Claudio Fontana740b1752020-08-19 13:17:19 +0200927 assert(icount_enabled());
Paolo Bonzini1aab16c2017-01-27 11:25:33 +0100928#ifndef CONFIG_USER_ONLY
Alex Bennéeeda5f7c2017-04-05 12:35:48 +0100929 /* Ensure global icount has gone forward */
Claudio Fontana8191d362020-08-31 16:18:34 +0200930 icount_update(cpu);
Alex Bennéeeda5f7c2017-04-05 12:35:48 +0100931 /* Refill decrementer and continue execution. */
Philippe Mathieu-Daudé0650fc12024-04-24 11:25:52 +0200932 int32_t insns_left = MIN(0xffff, cpu->icount_budget);
Richard Hendersona953b5f2023-09-13 15:46:45 -0700933 cpu->neg.icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +0100934 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéebc662a32021-02-13 13:03:18 +0000935
936 /*
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.
940 */
Peter Maydellc8cf47a2021-07-25 18:44:05 +0100941 if (insns_left > 0 && insns_left < tb->icount) {
942 assert(insns_left <= CF_COUNT_MASK);
943 assert(cpu->icount_extra == 0);
Alex Bennéebc662a32021-02-13 13:03:18 +0000944 cpu->cflags_next_tb = (tb->cflags & ~CF_COUNT_MASK) | insns_left;
Paolo Bonzini1aab16c2017-01-27 11:25:33 +0100945 }
Sergey Fedorov928de9e2016-05-11 13:21:50 +0300946#endif
Sergey Fedorov928de9e2016-05-11 13:21:50 +0300947}
948
bellard7d132992003-03-06 23:23:54 +0000949/* main execution loop */
950
Richard Henderson61710a72023-01-07 10:12:51 -0800951static int __attribute__((noinline))
952cpu_exec_loop(CPUState *cpu, SyncClocks *sc)
bellard7d132992003-03-06 23:23:54 +0000953{
Sergey Fedorovc385e6e2016-05-11 13:21:49 +0300954 int ret;
Paolo Bonzini4515e582017-01-29 10:55:14 +0100955
956 /* if an exception is pending, we execute it here */
957 while (!cpu_handle_exception(cpu, &ret)) {
958 TranslationBlock *last_tb = NULL;
959 int tb_exit = 0;
960
961 while (!cpu_handle_interrupt(cpu, &last_tb)) {
Richard Henderson9b990ee2017-10-13 10:50:02 -0700962 TranslationBlock *tb;
Anton Johanssonbb5de522023-06-21 15:56:24 +0200963 vaddr pc;
964 uint64_t cs_base;
Richard Henderson11c1d5f2021-07-19 12:40:57 -1000965 uint32_t flags, cflags;
Richard Henderson9b990ee2017-10-13 10:50:02 -0700966
Richard Hendersonb77af262023-09-13 17:22:49 -0700967 cpu_get_tb_cpu_state(cpu_env(cpu), &pc, &cs_base, &flags);
Richard Henderson10c37822021-07-19 09:03:21 -1000968
Richard Henderson11c1d5f2021-07-19 12:40:57 -1000969 /*
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.
975 */
976 cflags = cpu->cflags_next_tb;
Richard Henderson9b990ee2017-10-13 10:50:02 -0700977 if (cflags == -1) {
Alex Bennéec0ae3962021-02-24 16:58:08 +0000978 cflags = curr_cflags(cpu);
Richard Henderson9b990ee2017-10-13 10:50:02 -0700979 } else {
980 cpu->cflags_next_tb = -1;
981 }
982
Richard Henderson10c37822021-07-19 09:03:21 -1000983 if (check_for_breakpoints(cpu, pc, &cflags)) {
984 break;
985 }
Richard Henderson11c1d5f2021-07-19 12:40:57 -1000986
987 tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
988 if (tb == NULL) {
Richard Henderson33718022023-03-31 18:52:33 -0700989 CPUJumpCache *jc;
Richard Hendersona976a992022-08-15 15:13:05 -0500990 uint32_t h;
991
Richard Henderson11c1d5f2021-07-19 12:40:57 -1000992 mmap_lock();
993 tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
994 mmap_unlock();
Richard Henderson33718022023-03-31 18:52:33 -0700995
Richard Henderson11c1d5f2021-07-19 12:40:57 -1000996 /*
997 * We add the TB in the virtual pc hash table
998 * for the fast lookup
999 */
Richard Hendersona976a992022-08-15 15:13:05 -05001000 h = tb_jmp_cache_hash_func(pc);
Richard Henderson33718022023-03-31 18:52:33 -07001001 jc = cpu->tb_jmp_cache;
Paolo Bonzinid157e542024-01-22 16:34:09 +01001002 jc->array[h].pc = pc;
1003 qatomic_set(&jc->array[h].tb, tb);
Richard Henderson11c1d5f2021-07-19 12:40:57 -10001004 }
1005
1006#ifndef CONFIG_USER_ONLY
1007 /*
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.
1012 */
Richard Henderson28905cf2022-09-20 13:21:40 +02001013 if (tb_page_addr1(tb) != -1) {
Richard Henderson11c1d5f2021-07-19 12:40:57 -10001014 last_tb = NULL;
1015 }
1016#endif
1017 /* See if we can patch the calling TB. */
1018 if (last_tb) {
1019 tb_add_jump(last_tb, tb_exit, tb);
1020 }
1021
Richard Hendersonfbf59aa2022-08-15 15:16:06 -05001022 cpu_loop_exec_tb(cpu, tb, pc, &last_tb, &tb_exit);
Richard Henderson11c1d5f2021-07-19 12:40:57 -10001023
Paolo Bonzini4515e582017-01-29 10:55:14 +01001024 /* Try to align the host and virtual clocks
1025 if the guest is in advance */
Richard Henderson61710a72023-01-07 10:12:51 -08001026 align_clocks(sc, cpu);
bellard7d132992003-03-06 23:23:54 +00001027 }
Paolo Bonzini4515e582017-01-29 10:55:14 +01001028 }
Richard Henderson61710a72023-01-07 10:12:51 -08001029 return ret;
1030}
1031
1032static int cpu_exec_setjmp(CPUState *cpu, SyncClocks *sc)
1033{
1034 /* Prepare setjmp context for exception handling. */
1035 if (unlikely(sigsetjmp(cpu->jmp_env, 0) != 0)) {
Richard Hendersoncb62bd12023-07-06 08:45:13 +01001036 cpu_exec_longjmp_cleanup(cpu);
Richard Henderson61710a72023-01-07 10:12:51 -08001037 }
1038
1039 return cpu_exec_loop(cpu, sc);
1040}
1041
1042int cpu_exec(CPUState *cpu)
1043{
1044 int ret;
1045 SyncClocks sc = { 0 };
1046
1047 /* replay_interrupt may need current_cpu */
1048 current_cpu = cpu;
1049
1050 if (cpu_handle_halt(cpu)) {
1051 return EXCP_HALTED;
1052 }
1053
Philippe Mathieu-Daudéf5e93622024-01-24 08:41:56 +01001054 RCU_READ_LOCK_GUARD();
Richard Henderson61710a72023-01-07 10:12:51 -08001055 cpu_exec_enter(cpu);
1056
1057 /*
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.
1062 */
1063 init_delay_params(&sc, cpu);
1064
1065 ret = cpu_exec_setjmp(cpu, &sc);
bellard3fb2ded2003-06-24 13:22:59 +00001066
Eduardo Habkost035ba062020-12-12 16:55:16 +01001067 cpu_exec_exit(cpu);
bellard7d132992003-03-06 23:23:54 +00001068 return ret;
1069}
Claudio Fontana740b1752020-08-19 13:17:19 +02001070
Philippe Mathieu-Daudéfa312f22023-10-03 14:30:24 +02001071bool tcg_exec_realizefn(CPUState *cpu, Error **errp)
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001072{
1073 static bool tcg_target_initialized;
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001074
1075 if (!tcg_target_initialized) {
Peter Maydell0487c632024-07-04 16:57:10 +01001076 /* Check mandatory TCGCPUOps handlers */
1077#ifndef CONFIG_USER_ONLY
1078 assert(cpu->cc->tcg_ops->cpu_exec_halt);
Peter Maydellde680282024-07-12 12:39:49 +01001079 assert(cpu->cc->tcg_ops->cpu_exec_interrupt);
Peter Maydell0487c632024-07-04 16:57:10 +01001080#endif /* !CONFIG_USER_ONLY */
Richard Henderson991bd652024-01-28 12:57:59 +10001081 cpu->cc->tcg_ops->initialize();
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001082 tcg_target_initialized = true;
1083 }
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001084
Richard Henderson4e4fa6c2022-10-31 13:26:36 +11001085 cpu->tb_jmp_cache = g_new0(CPUJumpCache, 1);
1086 tlb_init(cpu);
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001087#ifndef CONFIG_USER_ONLY
1088 tcg_iommu_init_notifier_list(cpu);
1089#endif /* !CONFIG_USER_ONLY */
Richard Henderson4e4fa6c2022-10-31 13:26:36 +11001090 /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */
Philippe Mathieu-Daudéfa312f22023-10-03 14:30:24 +02001091
1092 return true;
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001093}
1094
1095/* undo the initializations in reverse order */
1096void tcg_exec_unrealizefn(CPUState *cpu)
1097{
1098#ifndef CONFIG_USER_ONLY
1099 tcg_iommu_free_notifier_list(cpu);
1100#endif /* !CONFIG_USER_ONLY */
1101
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001102 tlb_destroy(cpu);
Emilio Cota4731f892023-01-24 18:01:18 +00001103 g_free_rcu(cpu->tb_jmp_cache, rcu);
Claudio Fontana7df5e3d2021-02-04 17:39:11 +01001104}