xref: /openbmc/qemu/accel/tcg/cpu-exec.c (revision 71bce0e1fb1a866dde4a4b6016fc18b09f317338)
1d9bb58e5SYang Zhong /*
2d9bb58e5SYang Zhong  *  emulator main execution loop
3d9bb58e5SYang Zhong  *
4d9bb58e5SYang Zhong  *  Copyright (c) 2003-2005 Fabrice Bellard
5d9bb58e5SYang Zhong  *
6d9bb58e5SYang Zhong  * This library is free software; you can redistribute it and/or
7d9bb58e5SYang Zhong  * modify it under the terms of the GNU Lesser General Public
8d9bb58e5SYang Zhong  * License as published by the Free Software Foundation; either
9fb0343d5SThomas Huth  * version 2.1 of the License, or (at your option) any later version.
10d9bb58e5SYang Zhong  *
11d9bb58e5SYang Zhong  * This library is distributed in the hope that it will be useful,
12d9bb58e5SYang Zhong  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13d9bb58e5SYang Zhong  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14d9bb58e5SYang Zhong  * Lesser General Public License for more details.
15d9bb58e5SYang Zhong  *
16d9bb58e5SYang Zhong  * You should have received a copy of the GNU Lesser General Public
17d9bb58e5SYang Zhong  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18d9bb58e5SYang Zhong  */
19a8d25326SMarkus Armbruster 
20d9bb58e5SYang Zhong #include "qemu/osdep.h"
21740b1759SClaudio Fontana #include "qemu/qemu-print.h"
223a841ab5SDaniel P. Berrangé #include "qapi/error.h"
233a841ab5SDaniel P. Berrangé #include "qapi/type-helpers.h"
2478271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
25d9bb58e5SYang Zhong #include "trace.h"
26d9bb58e5SYang Zhong #include "disas/disas.h"
27d9bb58e5SYang Zhong #include "exec/exec-all.h"
28dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg.h"
29d9bb58e5SYang Zhong #include "qemu/atomic.h"
30d9bb58e5SYang Zhong #include "qemu/rcu.h"
31d9bb58e5SYang Zhong #include "exec/log.h"
32d9bb58e5SYang Zhong #include "qemu/main-loop.h"
33d9bb58e5SYang Zhong #include "sysemu/cpus.h"
34740b1759SClaudio Fontana #include "exec/cpu-all.h"
35740b1759SClaudio Fontana #include "sysemu/cpu-timers.h"
365b5968c4SPhilippe Mathieu-Daudé #include "exec/replay-core.h"
373a841ab5SDaniel P. Berrangé #include "sysemu/tcg.h"
38a3e7f702SPhilippe Mathieu-Daudé #include "exec/helper-proto-common.h"
39a976a99aSRichard Henderson #include "tb-jmp-cache.h"
40e5ceadffSPhilippe Mathieu-Daudé #include "tb-hash.h"
41e5ceadffSPhilippe Mathieu-Daudé #include "tb-context.h"
425934660fSPhilippe Mathieu-Daudé #include "internal-common.h"
434c268d6dSPhilippe Mathieu-Daudé #include "internal-target.h"
44d9bb58e5SYang Zhong 
45d9bb58e5SYang Zhong /* -icount align implementation. */
46d9bb58e5SYang Zhong 
47d9bb58e5SYang Zhong typedef struct SyncClocks {
48d9bb58e5SYang Zhong     int64_t diff_clk;
49d9bb58e5SYang Zhong     int64_t last_cpu_icount;
50d9bb58e5SYang Zhong     int64_t realtime_clock;
51d9bb58e5SYang Zhong } SyncClocks;
52d9bb58e5SYang Zhong 
53d9bb58e5SYang Zhong #if !defined(CONFIG_USER_ONLY)
54d9bb58e5SYang Zhong /* Allow the guest to have a max 3ms advance.
55d9bb58e5SYang Zhong  * The difference between the 2 clocks could therefore
56d9bb58e5SYang Zhong  * oscillate around 0.
57d9bb58e5SYang Zhong  */
58d9bb58e5SYang Zhong #define VM_CLOCK_ADVANCE 3000000
59d9bb58e5SYang Zhong #define THRESHOLD_REDUCE 1.5
60d9bb58e5SYang Zhong #define MAX_DELAY_PRINT_RATE 2000000000LL
61d9bb58e5SYang Zhong #define MAX_NB_PRINTS 100
62d9bb58e5SYang Zhong 
6300c9a5c2SPhilippe Mathieu-Daudé int64_t max_delay;
6400c9a5c2SPhilippe Mathieu-Daudé int64_t max_advance;
65740b1759SClaudio Fontana 
align_clocks(SyncClocks * sc,CPUState * cpu)665e140196SRichard Henderson static void align_clocks(SyncClocks *sc, CPUState *cpu)
67d9bb58e5SYang Zhong {
68d9bb58e5SYang Zhong     int64_t cpu_icount;
69d9bb58e5SYang Zhong 
70d9bb58e5SYang Zhong     if (!icount_align_option) {
71d9bb58e5SYang Zhong         return;
72d9bb58e5SYang Zhong     }
73d9bb58e5SYang Zhong 
74a953b5faSRichard Henderson     cpu_icount = cpu->icount_extra + cpu->neg.icount_decr.u16.low;
758191d368SClaudio Fontana     sc->diff_clk += icount_to_ns(sc->last_cpu_icount - cpu_icount);
76d9bb58e5SYang Zhong     sc->last_cpu_icount = cpu_icount;
77d9bb58e5SYang Zhong 
78d9bb58e5SYang Zhong     if (sc->diff_clk > VM_CLOCK_ADVANCE) {
79d9bb58e5SYang Zhong #ifndef _WIN32
80d9bb58e5SYang Zhong         struct timespec sleep_delay, rem_delay;
81d9bb58e5SYang Zhong         sleep_delay.tv_sec = sc->diff_clk / 1000000000LL;
82d9bb58e5SYang Zhong         sleep_delay.tv_nsec = sc->diff_clk % 1000000000LL;
83d9bb58e5SYang Zhong         if (nanosleep(&sleep_delay, &rem_delay) < 0) {
84d9bb58e5SYang Zhong             sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec;
85d9bb58e5SYang Zhong         } else {
86d9bb58e5SYang Zhong             sc->diff_clk = 0;
87d9bb58e5SYang Zhong         }
88d9bb58e5SYang Zhong #else
89d9bb58e5SYang Zhong         Sleep(sc->diff_clk / SCALE_MS);
90d9bb58e5SYang Zhong         sc->diff_clk = 0;
91d9bb58e5SYang Zhong #endif
92d9bb58e5SYang Zhong     }
93d9bb58e5SYang Zhong }
94d9bb58e5SYang Zhong 
print_delay(const SyncClocks * sc)95d9bb58e5SYang Zhong static void print_delay(const SyncClocks *sc)
96d9bb58e5SYang Zhong {
97d9bb58e5SYang Zhong     static float threshold_delay;
98d9bb58e5SYang Zhong     static int64_t last_realtime_clock;
99d9bb58e5SYang Zhong     static int nb_prints;
100d9bb58e5SYang Zhong 
101d9bb58e5SYang Zhong     if (icount_align_option &&
102d9bb58e5SYang Zhong         sc->realtime_clock - last_realtime_clock >= MAX_DELAY_PRINT_RATE &&
103d9bb58e5SYang Zhong         nb_prints < MAX_NB_PRINTS) {
104d9bb58e5SYang Zhong         if ((-sc->diff_clk / (float)1000000000LL > threshold_delay) ||
105d9bb58e5SYang Zhong             (-sc->diff_clk / (float)1000000000LL <
106d9bb58e5SYang Zhong              (threshold_delay - THRESHOLD_REDUCE))) {
107d9bb58e5SYang Zhong             threshold_delay = (-sc->diff_clk / 1000000000LL) + 1;
108740b1759SClaudio Fontana             qemu_printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
109d9bb58e5SYang Zhong                         threshold_delay - 1,
110d9bb58e5SYang Zhong                         threshold_delay);
111d9bb58e5SYang Zhong             nb_prints++;
112d9bb58e5SYang Zhong             last_realtime_clock = sc->realtime_clock;
113d9bb58e5SYang Zhong         }
114d9bb58e5SYang Zhong     }
115d9bb58e5SYang Zhong }
116d9bb58e5SYang Zhong 
init_delay_params(SyncClocks * sc,CPUState * cpu)1175e140196SRichard Henderson static void init_delay_params(SyncClocks *sc, CPUState *cpu)
118d9bb58e5SYang Zhong {
119d9bb58e5SYang Zhong     if (!icount_align_option) {
120d9bb58e5SYang Zhong         return;
121d9bb58e5SYang Zhong     }
122d9bb58e5SYang Zhong     sc->realtime_clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
123d9bb58e5SYang Zhong     sc->diff_clk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - sc->realtime_clock;
1245e140196SRichard Henderson     sc->last_cpu_icount
125a953b5faSRichard Henderson         = cpu->icount_extra + cpu->neg.icount_decr.u16.low;
126d9bb58e5SYang Zhong     if (sc->diff_clk < max_delay) {
127d9bb58e5SYang Zhong         max_delay = sc->diff_clk;
128d9bb58e5SYang Zhong     }
129d9bb58e5SYang Zhong     if (sc->diff_clk > max_advance) {
130d9bb58e5SYang Zhong         max_advance = sc->diff_clk;
131d9bb58e5SYang Zhong     }
132d9bb58e5SYang Zhong 
133d9bb58e5SYang Zhong     /* Print every 2s max if the guest is late. We limit the number
134d9bb58e5SYang Zhong        of printed messages to NB_PRINT_MAX(currently 100) */
135d9bb58e5SYang Zhong     print_delay(sc);
136d9bb58e5SYang Zhong }
137d9bb58e5SYang Zhong #else
align_clocks(SyncClocks * sc,const CPUState * cpu)138d9bb58e5SYang Zhong static void align_clocks(SyncClocks *sc, const CPUState *cpu)
139d9bb58e5SYang Zhong {
140d9bb58e5SYang Zhong }
141d9bb58e5SYang Zhong 
init_delay_params(SyncClocks * sc,const CPUState * cpu)142d9bb58e5SYang Zhong static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
143d9bb58e5SYang Zhong {
144d9bb58e5SYang Zhong }
145d9bb58e5SYang Zhong #endif /* CONFIG USER ONLY */
146d9bb58e5SYang Zhong 
tcg_cflags_has(CPUState * cpu,uint32_t flags)147b254c342SPhilippe Mathieu-Daudé bool tcg_cflags_has(CPUState *cpu, uint32_t flags)
148b254c342SPhilippe Mathieu-Daudé {
149b254c342SPhilippe Mathieu-Daudé     return cpu->tcg_cflags & flags;
150b254c342SPhilippe Mathieu-Daudé }
151b254c342SPhilippe Mathieu-Daudé 
tcg_cflags_set(CPUState * cpu,uint32_t flags)152b254c342SPhilippe Mathieu-Daudé void tcg_cflags_set(CPUState *cpu, uint32_t flags)
153b254c342SPhilippe Mathieu-Daudé {
154b254c342SPhilippe Mathieu-Daudé     cpu->tcg_cflags |= flags;
155b254c342SPhilippe Mathieu-Daudé }
156b254c342SPhilippe Mathieu-Daudé 
curr_cflags(CPUState * cpu)157043e35d9SRichard Henderson uint32_t curr_cflags(CPUState *cpu)
158043e35d9SRichard Henderson {
15984f15616SRichard Henderson     uint32_t cflags = cpu->tcg_cflags;
16084f15616SRichard Henderson 
16104f5b647SRichard Henderson     /*
162c2ffd754SRichard Henderson      * Record gdb single-step.  We should be exiting the TB by raising
163c2ffd754SRichard Henderson      * EXCP_DEBUG, but to simplify other tests, disable chaining too.
164c2ffd754SRichard Henderson      *
16504f5b647SRichard Henderson      * For singlestep and -d nochain, suppress goto_tb so that
16604f5b647SRichard Henderson      * we can log -d cpu,exec after every TB.
16704f5b647SRichard Henderson      */
168c2ffd754SRichard Henderson     if (unlikely(cpu->singlestep_enabled)) {
169c2ffd754SRichard Henderson         cflags |= CF_NO_GOTO_TB | CF_NO_GOTO_PTR | CF_SINGLE_STEP | 1;
1700e33928cSPeter Maydell     } else if (qatomic_read(&one_insn_per_tb)) {
17104f5b647SRichard Henderson         cflags |= CF_NO_GOTO_TB | 1;
17204f5b647SRichard Henderson     } else if (qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) {
173fb957011SRichard Henderson         cflags |= CF_NO_GOTO_TB;
17484f15616SRichard Henderson     }
17584f15616SRichard Henderson 
17684f15616SRichard Henderson     return cflags;
177043e35d9SRichard Henderson }
178043e35d9SRichard Henderson 
1790c90ba16SRichard Henderson struct tb_desc {
180f0a08b09SAnton Johansson     vaddr pc;
181f0a08b09SAnton Johansson     uint64_t cs_base;
1820c90ba16SRichard Henderson     CPUArchState *env;
18393b99616SRichard Henderson     tb_page_addr_t page_addr0;
1840c90ba16SRichard Henderson     uint32_t flags;
1850c90ba16SRichard Henderson     uint32_t cflags;
1860c90ba16SRichard Henderson };
1870c90ba16SRichard Henderson 
tb_lookup_cmp(const void * p,const void * d)1880c90ba16SRichard Henderson static bool tb_lookup_cmp(const void *p, const void *d)
1890c90ba16SRichard Henderson {
1900c90ba16SRichard Henderson     const TranslationBlock *tb = p;
1910c90ba16SRichard Henderson     const struct tb_desc *desc = d;
1920c90ba16SRichard Henderson 
193279513c7SAnton Johansson     if ((tb_cflags(tb) & CF_PCREL || tb->pc == desc->pc) &&
19428905cfbSRichard Henderson         tb_page_addr0(tb) == desc->page_addr0 &&
1950c90ba16SRichard Henderson         tb->cs_base == desc->cs_base &&
1960c90ba16SRichard Henderson         tb->flags == desc->flags &&
1970c90ba16SRichard Henderson         tb_cflags(tb) == desc->cflags) {
1980c90ba16SRichard Henderson         /* check next page if needed */
19928905cfbSRichard Henderson         tb_page_addr_t tb_phys_page1 = tb_page_addr1(tb);
20028905cfbSRichard Henderson         if (tb_phys_page1 == -1) {
2010c90ba16SRichard Henderson             return true;
2020c90ba16SRichard Henderson         } else {
20393b99616SRichard Henderson             tb_page_addr_t phys_page1;
204f0a08b09SAnton Johansson             vaddr virt_page1;
2050c90ba16SRichard Henderson 
2069867b302SRichard Henderson             /*
2079867b302SRichard Henderson              * We know that the first page matched, and an otherwise valid TB
2089867b302SRichard Henderson              * encountered an incomplete instruction at the end of that page,
2099867b302SRichard Henderson              * therefore we know that generating a new TB from the current PC
2109867b302SRichard Henderson              * must also require reading from the next page -- even if the
2119867b302SRichard Henderson              * second pages do not match, and therefore the resulting insn
2129867b302SRichard Henderson              * is different for the new TB.  Therefore any exception raised
2139867b302SRichard Henderson              * here by the faulting lookup is not premature.
2149867b302SRichard Henderson              */
21593b99616SRichard Henderson             virt_page1 = TARGET_PAGE_ALIGN(desc->pc);
21693b99616SRichard Henderson             phys_page1 = get_page_addr_code(desc->env, virt_page1);
21728905cfbSRichard Henderson             if (tb_phys_page1 == phys_page1) {
2180c90ba16SRichard Henderson                 return true;
2190c90ba16SRichard Henderson             }
2200c90ba16SRichard Henderson         }
2210c90ba16SRichard Henderson     }
2220c90ba16SRichard Henderson     return false;
2230c90ba16SRichard Henderson }
2240c90ba16SRichard Henderson 
tb_htable_lookup(CPUState * cpu,vaddr pc,uint64_t cs_base,uint32_t flags,uint32_t cflags)225f0a08b09SAnton Johansson static TranslationBlock *tb_htable_lookup(CPUState *cpu, vaddr pc,
226f0a08b09SAnton Johansson                                           uint64_t cs_base, uint32_t flags,
2270c90ba16SRichard Henderson                                           uint32_t cflags)
2280c90ba16SRichard Henderson {
2290c90ba16SRichard Henderson     tb_page_addr_t phys_pc;
2300c90ba16SRichard Henderson     struct tb_desc desc;
2310c90ba16SRichard Henderson     uint32_t h;
2320c90ba16SRichard Henderson 
233b77af26eSRichard Henderson     desc.env = cpu_env(cpu);
2340c90ba16SRichard Henderson     desc.cs_base = cs_base;
2350c90ba16SRichard Henderson     desc.flags = flags;
2360c90ba16SRichard Henderson     desc.cflags = cflags;
2370c90ba16SRichard Henderson     desc.pc = pc;
2380c90ba16SRichard Henderson     phys_pc = get_page_addr_code(desc.env, pc);
2390c90ba16SRichard Henderson     if (phys_pc == -1) {
2400c90ba16SRichard Henderson         return NULL;
2410c90ba16SRichard Henderson     }
24293b99616SRichard Henderson     desc.page_addr0 = phys_pc;
2434be79026SAnton Johansson     h = tb_hash_func(phys_pc, (cflags & CF_PCREL ? 0 : pc),
244367189efSAlex Bennée                      flags, cs_base, cflags);
2450c90ba16SRichard Henderson     return qht_lookup_custom(&tb_ctx.htable, &desc, h, tb_lookup_cmp);
2460c90ba16SRichard Henderson }
2470c90ba16SRichard Henderson 
248632cb63dSRichard Henderson /* Might cause an exception, so have a longjmp destination ready */
tb_lookup(CPUState * cpu,vaddr pc,uint64_t cs_base,uint32_t flags,uint32_t cflags)249f0a08b09SAnton Johansson static inline TranslationBlock *tb_lookup(CPUState *cpu, vaddr pc,
250f0a08b09SAnton Johansson                                           uint64_t cs_base, uint32_t flags,
251f0a08b09SAnton Johansson                                           uint32_t cflags)
252632cb63dSRichard Henderson {
253632cb63dSRichard Henderson     TranslationBlock *tb;
2548ed558ecSRichard Henderson     CPUJumpCache *jc;
255632cb63dSRichard Henderson     uint32_t hash;
256632cb63dSRichard Henderson 
257632cb63dSRichard Henderson     /* we should never be trying to look up an INVALID tb */
258632cb63dSRichard Henderson     tcg_debug_assert(!(cflags & CF_INVALID));
259632cb63dSRichard Henderson 
260632cb63dSRichard Henderson     hash = tb_jmp_cache_hash_func(pc);
2618ed558ecSRichard Henderson     jc = cpu->tb_jmp_cache;
2622dd5b7a1SAnton Johansson 
263d157e540SPaolo Bonzini     tb = qatomic_read(&jc->array[hash].tb);
264632cb63dSRichard Henderson     if (likely(tb &&
2652dd5b7a1SAnton Johansson                jc->array[hash].pc == pc &&
266632cb63dSRichard Henderson                tb->cs_base == cs_base &&
267632cb63dSRichard Henderson                tb->flags == flags &&
268632cb63dSRichard Henderson                tb_cflags(tb) == cflags)) {
269d157e540SPaolo Bonzini         goto hit;
270632cb63dSRichard Henderson     }
271d157e540SPaolo Bonzini 
272632cb63dSRichard Henderson     tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags);
273632cb63dSRichard Henderson     if (tb == NULL) {
274632cb63dSRichard Henderson         return NULL;
275632cb63dSRichard Henderson     }
276d157e540SPaolo Bonzini 
2772dd5b7a1SAnton Johansson     jc->array[hash].pc = pc;
2782dd5b7a1SAnton Johansson     qatomic_set(&jc->array[hash].tb, tb);
2792dd5b7a1SAnton Johansson 
280d157e540SPaolo Bonzini hit:
281d157e540SPaolo Bonzini     /*
282d157e540SPaolo Bonzini      * As long as tb is not NULL, the contents are consistent.  Therefore,
283d157e540SPaolo Bonzini      * the virtual PC has to match for non-CF_PCREL translations.
284d157e540SPaolo Bonzini      */
285d157e540SPaolo Bonzini     assert((tb_cflags(tb) & CF_PCREL) || tb->pc == pc);
286632cb63dSRichard Henderson     return tb;
287632cb63dSRichard Henderson }
288632cb63dSRichard Henderson 
log_cpu_exec(vaddr pc,CPUState * cpu,const TranslationBlock * tb)289f0a08b09SAnton Johansson static void log_cpu_exec(vaddr pc, CPUState *cpu,
290abb0cd93SRichard Henderson                          const TranslationBlock *tb)
291abb0cd93SRichard Henderson {
292fbf59aadSRichard Henderson     if (qemu_log_in_addr_range(pc)) {
293abb0cd93SRichard Henderson         qemu_log_mask(CPU_LOG_EXEC,
29485314e13SRichard Henderson                       "Trace %d: %p [%08" PRIx64
295e60a7d0dSPeter Maydell                       "/%016" VADDR_PRIx "/%08x/%08x] %s\n",
2967eabad36SRichard Henderson                       cpu->cpu_index, tb->tc.ptr, tb->cs_base, pc,
2977eabad36SRichard Henderson                       tb->flags, tb->cflags, lookup_symbol(pc));
298abb0cd93SRichard Henderson 
299abb0cd93SRichard Henderson         if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
300c60f599bSRichard Henderson             FILE *logfile = qemu_log_trylock();
30178b54858SRichard Henderson             if (logfile) {
302abb0cd93SRichard Henderson                 int flags = 0;
303abb0cd93SRichard Henderson 
304abb0cd93SRichard Henderson                 if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) {
305abb0cd93SRichard Henderson                     flags |= CPU_DUMP_FPU;
306abb0cd93SRichard Henderson                 }
307abb0cd93SRichard Henderson #if defined(TARGET_I386)
308abb0cd93SRichard Henderson                 flags |= CPU_DUMP_CCOP;
309abb0cd93SRichard Henderson #endif
310b84694deSIvan Klokov                 if (qemu_loglevel_mask(CPU_LOG_TB_VPU)) {
311b84694deSIvan Klokov                     flags |= CPU_DUMP_VPU;
312b84694deSIvan Klokov                 }
313c769fbd7SRichard Henderson                 cpu_dump_state(cpu, logfile, flags);
314abb0cd93SRichard Henderson                 qemu_log_unlock(logfile);
315abb0cd93SRichard Henderson             }
31678b54858SRichard Henderson         }
317abb0cd93SRichard Henderson     }
318abb0cd93SRichard Henderson }
319abb0cd93SRichard Henderson 
check_for_breakpoints_slow(CPUState * cpu,vaddr pc,uint32_t * cflags)320f0a08b09SAnton Johansson static bool check_for_breakpoints_slow(CPUState *cpu, vaddr pc,
32110c37828SRichard Henderson                                        uint32_t *cflags)
32210c37828SRichard Henderson {
32310c37828SRichard Henderson     CPUBreakpoint *bp;
32410c37828SRichard Henderson     bool match_page = false;
32510c37828SRichard Henderson 
32610c37828SRichard Henderson     /*
32710c37828SRichard Henderson      * Singlestep overrides breakpoints.
32810c37828SRichard Henderson      * This requirement is visible in the record-replay tests, where
32910c37828SRichard Henderson      * we would fail to make forward progress in reverse-continue.
33010c37828SRichard Henderson      *
33110c37828SRichard Henderson      * TODO: gdb singlestep should only override gdb breakpoints,
33210c37828SRichard Henderson      * so that one could (gdb) singlestep into the guest kernel's
33310c37828SRichard Henderson      * architectural breakpoint handler.
33410c37828SRichard Henderson      */
33510c37828SRichard Henderson     if (cpu->singlestep_enabled) {
33610c37828SRichard Henderson         return false;
33710c37828SRichard Henderson     }
33810c37828SRichard Henderson 
33910c37828SRichard Henderson     QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
34010c37828SRichard Henderson         /*
34110c37828SRichard Henderson          * If we have an exact pc match, trigger the breakpoint.
34210c37828SRichard Henderson          * Otherwise, note matches within the page.
34310c37828SRichard Henderson          */
34410c37828SRichard Henderson         if (pc == bp->pc) {
34510c37828SRichard Henderson             bool match_bp = false;
34610c37828SRichard Henderson 
34710c37828SRichard Henderson             if (bp->flags & BP_GDB) {
34810c37828SRichard Henderson                 match_bp = true;
34910c37828SRichard Henderson             } else if (bp->flags & BP_CPU) {
35010c37828SRichard Henderson #ifdef CONFIG_USER_ONLY
35110c37828SRichard Henderson                 g_assert_not_reached();
35210c37828SRichard Henderson #else
353991bd65dSRichard Henderson                 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
354991bd65dSRichard Henderson                 assert(tcg_ops->debug_check_breakpoint);
355991bd65dSRichard Henderson                 match_bp = tcg_ops->debug_check_breakpoint(cpu);
35610c37828SRichard Henderson #endif
35710c37828SRichard Henderson             }
35810c37828SRichard Henderson 
35910c37828SRichard Henderson             if (match_bp) {
36010c37828SRichard Henderson                 cpu->exception_index = EXCP_DEBUG;
36110c37828SRichard Henderson                 return true;
36210c37828SRichard Henderson             }
36310c37828SRichard Henderson         } else if (((pc ^ bp->pc) & TARGET_PAGE_MASK) == 0) {
36410c37828SRichard Henderson             match_page = true;
36510c37828SRichard Henderson         }
36610c37828SRichard Henderson     }
36710c37828SRichard Henderson 
36810c37828SRichard Henderson     /*
36910c37828SRichard Henderson      * Within the same page as a breakpoint, single-step,
37010c37828SRichard Henderson      * returning to helper_lookup_tb_ptr after each insn looking
37110c37828SRichard Henderson      * for the actual breakpoint.
37210c37828SRichard Henderson      *
37310c37828SRichard Henderson      * TODO: Perhaps better to record all of the TBs associated
37410c37828SRichard Henderson      * with a given virtual page that contains a breakpoint, and
37510c37828SRichard Henderson      * then invalidate them when a new overlapping breakpoint is
37610c37828SRichard Henderson      * set on the page.  Non-overlapping TBs would not be
37710c37828SRichard Henderson      * invalidated, nor would any TB need to be invalidated as
37810c37828SRichard Henderson      * breakpoints are removed.
37910c37828SRichard Henderson      */
38010c37828SRichard Henderson     if (match_page) {
381d828b92bSRichard Henderson         *cflags = (*cflags & ~CF_COUNT_MASK) | CF_NO_GOTO_TB | CF_BP_PAGE | 1;
38210c37828SRichard Henderson     }
38310c37828SRichard Henderson     return false;
38410c37828SRichard Henderson }
38510c37828SRichard Henderson 
check_for_breakpoints(CPUState * cpu,vaddr pc,uint32_t * cflags)386f0a08b09SAnton Johansson static inline bool check_for_breakpoints(CPUState *cpu, vaddr pc,
38769993c4eSLeandro Lupori                                          uint32_t *cflags)
38869993c4eSLeandro Lupori {
38969993c4eSLeandro Lupori     return unlikely(!QTAILQ_EMPTY(&cpu->breakpoints)) &&
39069993c4eSLeandro Lupori         check_for_breakpoints_slow(cpu, pc, cflags);
39169993c4eSLeandro Lupori }
39269993c4eSLeandro Lupori 
3934288eb26SRichard Henderson /**
3944288eb26SRichard Henderson  * helper_lookup_tb_ptr: quick check for next tb
3954288eb26SRichard Henderson  * @env: current cpu state
3964288eb26SRichard Henderson  *
3974288eb26SRichard Henderson  * Look for an existing TB matching the current cpu state.
3984288eb26SRichard Henderson  * If found, return the code pointer.  If not found, return
3994288eb26SRichard Henderson  * the tcg epilogue so that we return into cpu_tb_exec.
4004288eb26SRichard Henderson  */
HELPER(lookup_tb_ptr)4014288eb26SRichard Henderson const void *HELPER(lookup_tb_ptr)(CPUArchState *env)
4024288eb26SRichard Henderson {
4034288eb26SRichard Henderson     CPUState *cpu = env_cpu(env);
4044288eb26SRichard Henderson     TranslationBlock *tb;
405bb5de525SAnton Johansson     vaddr pc;
406bb5de525SAnton Johansson     uint64_t cs_base;
40710c37828SRichard Henderson     uint32_t flags, cflags;
4084288eb26SRichard Henderson 
40962bcba83SPeter Maydell     /*
41062bcba83SPeter Maydell      * By definition we've just finished a TB, so I/O is OK.
41162bcba83SPeter Maydell      * Avoid the possibility of calling cpu_io_recompile() if
41262bcba83SPeter Maydell      * a page table walk triggered by tb_lookup() calling
41362bcba83SPeter Maydell      * probe_access_internal() happens to touch an MMIO device.
41462bcba83SPeter Maydell      * The next TB, if we chain to it, will clear the flag again.
41562bcba83SPeter Maydell      */
41662bcba83SPeter Maydell     cpu->neg.can_do_io = true;
4174288eb26SRichard Henderson     cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
4184288eb26SRichard Henderson 
41910c37828SRichard Henderson     cflags = curr_cflags(cpu);
42010c37828SRichard Henderson     if (check_for_breakpoints(cpu, pc, &cflags)) {
42110c37828SRichard Henderson         cpu_loop_exit(cpu);
42210c37828SRichard Henderson     }
42310c37828SRichard Henderson 
42410c37828SRichard Henderson     tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
4254288eb26SRichard Henderson     if (tb == NULL) {
4264288eb26SRichard Henderson         return tcg_code_gen_epilogue;
4274288eb26SRichard Henderson     }
428abb0cd93SRichard Henderson 
429fbf59aadSRichard Henderson     if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
430abb0cd93SRichard Henderson         log_cpu_exec(pc, cpu, tb);
431fbf59aadSRichard Henderson     }
432abb0cd93SRichard Henderson 
4334288eb26SRichard Henderson     return tb->tc.ptr;
4344288eb26SRichard Henderson }
4354288eb26SRichard Henderson 
436d9bb58e5SYang Zhong /* Execute a TB, and fix up the CPU state afterwards if necessary */
437c905a368SDaniele Buono /*
438c905a368SDaniele Buono  * Disable CFI checks.
439c905a368SDaniele Buono  * TCG creates binary blobs at runtime, with the transformed code.
440c905a368SDaniele Buono  * A TB is a blob of binary code, created at runtime and called with an
441c905a368SDaniele Buono  * indirect function call. Since such function did not exist at compile time,
442c905a368SDaniele Buono  * the CFI runtime has no way to verify its signature and would fail.
443c905a368SDaniele Buono  * TCG is not considered a security-sensitive part of QEMU so this does not
444c905a368SDaniele Buono  * affect the impact of CFI in environment with high security requirements
445c905a368SDaniele Buono  */
446eba40358SRichard Henderson static inline TranslationBlock * QEMU_DISABLE_CFI
cpu_tb_exec(CPUState * cpu,TranslationBlock * itb,int * tb_exit)447eba40358SRichard Henderson cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit)
448d9bb58e5SYang Zhong {
449d9bb58e5SYang Zhong     uintptr_t ret;
450d9bb58e5SYang Zhong     TranslationBlock *last_tb;
451db0c51a3SRichard Henderson     const void *tb_ptr = itb->tc.ptr;
452d9bb58e5SYang Zhong 
453fbf59aadSRichard Henderson     if (qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC)) {
454fbf59aadSRichard Henderson         log_cpu_exec(log_pc(cpu, itb), cpu, itb);
455fbf59aadSRichard Henderson     }
456d9bb58e5SYang Zhong 
457653b87ebSRoman Bolshakov     qemu_thread_jit_execute();
45894956d7bSPhilippe Mathieu-Daudé     ret = tcg_qemu_tb_exec(cpu_env(cpu), tb_ptr);
459464dacf6SRichard Henderson     cpu->neg.can_do_io = true;
460e04660afSRichard Henderson     qemu_plugin_disable_mem_helpers(cpu);
461eba40358SRichard Henderson     /*
462eba40358SRichard Henderson      * TODO: Delay swapping back to the read-write region of the TB
463eba40358SRichard Henderson      * until we actually need to modify the TB.  The read-only copy,
464eba40358SRichard Henderson      * coming from the rx region, shares the same host TLB entry as
465eba40358SRichard Henderson      * the code that executed the exit_tb opcode that arrived here.
466eba40358SRichard Henderson      * If we insist on touching both the RX and the RW pages, we
467eba40358SRichard Henderson      * double the host TLB pressure.
468eba40358SRichard Henderson      */
469eba40358SRichard Henderson     last_tb = tcg_splitwx_to_rw((void *)(ret & ~TB_EXIT_MASK));
470eba40358SRichard Henderson     *tb_exit = ret & TB_EXIT_MASK;
471d9bb58e5SYang Zhong 
472eba40358SRichard Henderson     trace_exec_tb_exit(last_tb, *tb_exit);
473eba40358SRichard Henderson 
474eba40358SRichard Henderson     if (*tb_exit > TB_EXIT_IDX1) {
475d9bb58e5SYang Zhong         /* We didn't start executing this TB (eg because the instruction
476d9bb58e5SYang Zhong          * counter hit zero); we must restore the guest PC to the address
477d9bb58e5SYang Zhong          * of the start of the TB.
478d9bb58e5SYang Zhong          */
479991bd65dSRichard Henderson         CPUClass *cc = cpu->cc;
480991bd65dSRichard Henderson         const TCGCPUOps *tcg_ops = cc->tcg_ops;
481fbf59aadSRichard Henderson 
482991bd65dSRichard Henderson         if (tcg_ops->synchronize_from_tb) {
483991bd65dSRichard Henderson             tcg_ops->synchronize_from_tb(cpu, last_tb);
484d9bb58e5SYang Zhong         } else {
4854be79026SAnton Johansson             tcg_debug_assert(!(tb_cflags(last_tb) & CF_PCREL));
486d9bb58e5SYang Zhong             assert(cc->set_pc);
487279513c7SAnton Johansson             cc->set_pc(cpu, last_tb->pc);
488fbf59aadSRichard Henderson         }
489fbf59aadSRichard Henderson         if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
490f0a08b09SAnton Johansson             vaddr pc = log_pc(cpu, last_tb);
491fbf59aadSRichard Henderson             if (qemu_log_in_addr_range(pc)) {
492e60a7d0dSPeter Maydell                 qemu_log("Stopped execution of TB chain before %p [%016"
493f0a08b09SAnton Johansson                          VADDR_PRIx "] %s\n",
494fbf59aadSRichard Henderson                          last_tb->tc.ptr, pc, lookup_symbol(pc));
495fbf59aadSRichard Henderson             }
496d9bb58e5SYang Zhong         }
497d9bb58e5SYang Zhong     }
498c9460d75SRichard Henderson 
499c9460d75SRichard Henderson     /*
500c9460d75SRichard Henderson      * If gdb single-step, and we haven't raised another exception,
501c9460d75SRichard Henderson      * raise a debug exception.  Single-step with another exception
502c9460d75SRichard Henderson      * is handled in cpu_handle_exception.
503c9460d75SRichard Henderson      */
504c9460d75SRichard Henderson     if (unlikely(cpu->singlestep_enabled) && cpu->exception_index == -1) {
505c9460d75SRichard Henderson         cpu->exception_index = EXCP_DEBUG;
506c9460d75SRichard Henderson         cpu_loop_exit(cpu);
507c9460d75SRichard Henderson     }
508c9460d75SRichard Henderson 
509eba40358SRichard Henderson     return last_tb;
510d9bb58e5SYang Zhong }
511d9bb58e5SYang Zhong 
512d9bb58e5SYang Zhong 
cpu_exec_enter(CPUState * cpu)513035ba06cSEduardo Habkost static void cpu_exec_enter(CPUState *cpu)
514d9bb58e5SYang Zhong {
515991bd65dSRichard Henderson     const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
516035ba06cSEduardo Habkost 
517991bd65dSRichard Henderson     if (tcg_ops->cpu_exec_enter) {
518991bd65dSRichard Henderson         tcg_ops->cpu_exec_enter(cpu);
519035ba06cSEduardo Habkost     }
52080c4750bSEduardo Habkost }
521035ba06cSEduardo Habkost 
cpu_exec_exit(CPUState * cpu)522035ba06cSEduardo Habkost static void cpu_exec_exit(CPUState *cpu)
523035ba06cSEduardo Habkost {
524991bd65dSRichard Henderson     const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
525035ba06cSEduardo Habkost 
526991bd65dSRichard Henderson     if (tcg_ops->cpu_exec_exit) {
527991bd65dSRichard Henderson         tcg_ops->cpu_exec_exit(cpu);
528035ba06cSEduardo Habkost     }
52980c4750bSEduardo Habkost }
530035ba06cSEduardo Habkost 
cpu_exec_longjmp_cleanup(CPUState * cpu)531cb62bd15SRichard Henderson static void cpu_exec_longjmp_cleanup(CPUState *cpu)
532cb62bd15SRichard Henderson {
533cb62bd15SRichard Henderson     /* Non-buggy compilers preserve this; assert the correct value. */
534cb62bd15SRichard Henderson     g_assert(cpu == current_cpu);
535cb62bd15SRichard Henderson 
536cb62bd15SRichard Henderson #ifdef CONFIG_USER_ONLY
537cb62bd15SRichard Henderson     clear_helper_retaddr();
538cb62bd15SRichard Henderson     if (have_mmap_lock()) {
539cb62bd15SRichard Henderson         mmap_unlock();
540cb62bd15SRichard Henderson     }
541deba7870SRichard Henderson #else
542deba7870SRichard Henderson     /*
543deba7870SRichard Henderson      * For softmmu, a tlb_fill fault during translation will land here,
544deba7870SRichard Henderson      * and we need to release any page locks held.  In system mode we
545deba7870SRichard Henderson      * have one tcg_ctx per thread, so we know it was this cpu doing
546deba7870SRichard Henderson      * the translation.
547deba7870SRichard Henderson      *
548deba7870SRichard Henderson      * Alternative 1: Install a cleanup to be called via an exception
549deba7870SRichard Henderson      * handling safe longjmp.  It seems plausible that all our hosts
550deba7870SRichard Henderson      * support such a thing.  We'd have to properly register unwind info
551deba7870SRichard Henderson      * for the JIT for EH, rather that just for GDB.
552deba7870SRichard Henderson      *
553deba7870SRichard Henderson      * Alternative 2: Set and restore cpu->jmp_env in tb_gen_code to
554deba7870SRichard Henderson      * capture the cpu_loop_exit longjmp, perform the cleanup, and
555deba7870SRichard Henderson      * jump again to arrive here.
556deba7870SRichard Henderson      */
557deba7870SRichard Henderson     if (tcg_ctx->gen_tb) {
558deba7870SRichard Henderson         tb_unlock_pages(tcg_ctx->gen_tb);
559deba7870SRichard Henderson         tcg_ctx->gen_tb = NULL;
560deba7870SRichard Henderson     }
561cb62bd15SRichard Henderson #endif
562195801d7SStefan Hajnoczi     if (bql_locked()) {
563195801d7SStefan Hajnoczi         bql_unlock();
564cb62bd15SRichard Henderson     }
565cb62bd15SRichard Henderson     assert_no_pages_locked();
566cb62bd15SRichard Henderson }
567cb62bd15SRichard Henderson 
cpu_exec_step_atomic(CPUState * cpu)568035ba06cSEduardo Habkost void cpu_exec_step_atomic(CPUState *cpu)
569035ba06cSEduardo Habkost {
570b77af26eSRichard Henderson     CPUArchState *env = cpu_env(cpu);
571d9bb58e5SYang Zhong     TranslationBlock *tb;
572bb5de525SAnton Johansson     vaddr pc;
573bb5de525SAnton Johansson     uint64_t cs_base;
574258afb48SRichard Henderson     uint32_t flags, cflags;
575eba40358SRichard Henderson     int tb_exit;
576d9bb58e5SYang Zhong 
577d9bb58e5SYang Zhong     if (sigsetjmp(cpu->jmp_env, 0) == 0) {
578886cc689SAlex Bennée         start_exclusive();
579bfff072cSDouglas Crosher         g_assert(cpu == current_cpu);
580bfff072cSDouglas Crosher         g_assert(!cpu->running);
581bfff072cSDouglas Crosher         cpu->running = true;
582886cc689SAlex Bennée 
5836f04cb1cSAlex Bennée         cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
5846f04cb1cSAlex Bennée 
585258afb48SRichard Henderson         cflags = curr_cflags(cpu);
586258afb48SRichard Henderson         /* Execute in a serial context. */
587258afb48SRichard Henderson         cflags &= ~CF_PARALLEL;
588258afb48SRichard Henderson         /* After 1 insn, return and release the exclusive lock. */
589258afb48SRichard Henderson         cflags |= CF_NO_GOTO_TB | CF_NO_GOTO_PTR | 1;
59010c37828SRichard Henderson         /*
59110c37828SRichard Henderson          * No need to check_for_breakpoints here.
59210c37828SRichard Henderson          * We only arrive in cpu_exec_step_atomic after beginning execution
59310c37828SRichard Henderson          * of an insn that includes an atomic operation we can't handle.
59410c37828SRichard Henderson          * Any breakpoint for this insn will have been recognized earlier.
59510c37828SRichard Henderson          */
596258afb48SRichard Henderson 
597258afb48SRichard Henderson         tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
5984e2ca83eSEmilio G. Cota         if (tb == NULL) {
599d9bb58e5SYang Zhong             mmap_lock();
6004e2ca83eSEmilio G. Cota             tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
601d9bb58e5SYang Zhong             mmap_unlock();
6024e2ca83eSEmilio G. Cota         }
603d9bb58e5SYang Zhong 
604035ba06cSEduardo Habkost         cpu_exec_enter(cpu);
605d9bb58e5SYang Zhong         /* execute the generated code */
6064e2ca83eSEmilio G. Cota         trace_exec_tb(tb, pc);
607eba40358SRichard Henderson         cpu_tb_exec(cpu, tb, &tb_exit);
608035ba06cSEduardo Habkost         cpu_exec_exit(cpu);
609d9bb58e5SYang Zhong     } else {
610cb62bd15SRichard Henderson         cpu_exec_longjmp_cleanup(cpu);
611d9bb58e5SYang Zhong     }
612426eeecdSPeter Maydell 
613886cc689SAlex Bennée     /*
614886cc689SAlex Bennée      * As we start the exclusive region before codegen we must still
615886cc689SAlex Bennée      * be in the region if we longjump out of either the codegen or
616886cc689SAlex Bennée      * the execution.
617426eeecdSPeter Maydell      */
618886cc689SAlex Bennée     g_assert(cpu_in_exclusive_context(cpu));
619bfff072cSDouglas Crosher     cpu->running = false;
620426eeecdSPeter Maydell     end_exclusive();
621426eeecdSPeter Maydell }
622d9bb58e5SYang Zhong 
tb_set_jmp_target(TranslationBlock * tb,int n,uintptr_t addr)623a8583393SRichard Henderson void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr)
624a8583393SRichard Henderson {
6250fe1c98dSRichard Henderson     /*
6260fe1c98dSRichard Henderson      * Get the rx view of the structure, from which we find the
6270fe1c98dSRichard Henderson      * executable code address, and tb_target_set_jmp_target can
6280fe1c98dSRichard Henderson      * produce a pc-relative displacement to jmp_target_addr[n].
6290fe1c98dSRichard Henderson      */
6300fe1c98dSRichard Henderson     const TranslationBlock *c_tb = tcg_splitwx_to_rx(tb);
6319da6079bSRichard Henderson     uintptr_t offset = tb->jmp_insn_offset[n];
6320fe1c98dSRichard Henderson     uintptr_t jmp_rx = (uintptr_t)tb->tc.ptr + offset;
6331acbad0fSRichard Henderson     uintptr_t jmp_rw = jmp_rx - tcg_splitwx_diff;
6342fd2e78dSRichard Henderson 
6352fd2e78dSRichard Henderson     tb->jmp_target_addr[n] = addr;
6360fe1c98dSRichard Henderson     tb_target_set_jmp_target(c_tb, n, jmp_rx, jmp_rw);
637a8583393SRichard Henderson }
638a8583393SRichard Henderson 
tb_add_jump(TranslationBlock * tb,int n,TranslationBlock * tb_next)639a8583393SRichard Henderson static inline void tb_add_jump(TranslationBlock *tb, int n,
640a8583393SRichard Henderson                                TranslationBlock *tb_next)
641a8583393SRichard Henderson {
642194125e3SEmilio G. Cota     uintptr_t old;
643194125e3SEmilio G. Cota 
644653b87ebSRoman Bolshakov     qemu_thread_jit_write();
645a8583393SRichard Henderson     assert(n < ARRAY_SIZE(tb->jmp_list_next));
646194125e3SEmilio G. Cota     qemu_spin_lock(&tb_next->jmp_lock);
647194125e3SEmilio G. Cota 
648194125e3SEmilio G. Cota     /* make sure the destination TB is valid */
649194125e3SEmilio G. Cota     if (tb_next->cflags & CF_INVALID) {
650194125e3SEmilio G. Cota         goto out_unlock_next;
651a8583393SRichard Henderson     }
652194125e3SEmilio G. Cota     /* Atomically claim the jump destination slot only if it was NULL */
653d73415a3SStefan Hajnoczi     old = qatomic_cmpxchg(&tb->jmp_dest[n], (uintptr_t)NULL,
654d73415a3SStefan Hajnoczi                           (uintptr_t)tb_next);
655194125e3SEmilio G. Cota     if (old) {
656194125e3SEmilio G. Cota         goto out_unlock_next;
657194125e3SEmilio G. Cota     }
658194125e3SEmilio G. Cota 
659194125e3SEmilio G. Cota     /* patch the native jump address */
660194125e3SEmilio G. Cota     tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc.ptr);
661194125e3SEmilio G. Cota 
662194125e3SEmilio G. Cota     /* add in TB jmp list */
663194125e3SEmilio G. Cota     tb->jmp_list_next[n] = tb_next->jmp_list_head;
664194125e3SEmilio G. Cota     tb_next->jmp_list_head = (uintptr_t)tb | n;
665194125e3SEmilio G. Cota 
666194125e3SEmilio G. Cota     qemu_spin_unlock(&tb_next->jmp_lock);
667194125e3SEmilio G. Cota 
668fbf59aadSRichard Henderson     qemu_log_mask(CPU_LOG_EXEC, "Linking TBs %p index %d -> %p\n",
669fbf59aadSRichard Henderson                   tb->tc.ptr, n, tb_next->tc.ptr);
670194125e3SEmilio G. Cota     return;
671a8583393SRichard Henderson 
672194125e3SEmilio G. Cota  out_unlock_next:
673194125e3SEmilio G. Cota     qemu_spin_unlock(&tb_next->jmp_lock);
674194125e3SEmilio G. Cota     return;
675a8583393SRichard Henderson }
676a8583393SRichard Henderson 
cpu_handle_halt(CPUState * cpu)677d9bb58e5SYang Zhong static inline bool cpu_handle_halt(CPUState *cpu)
678d9bb58e5SYang Zhong {
6790596fa11SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
680d9bb58e5SYang Zhong     if (cpu->halted) {
681aa6fb657SPhilippe Mathieu-Daudé         const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
6820487c631SPeter Maydell         bool leave_halt = tcg_ops->cpu_exec_halt(cpu);
683aa6fb657SPhilippe Mathieu-Daudé 
684408b2b3dSPeter Maydell         if (!leave_halt) {
685d9bb58e5SYang Zhong             return true;
686d9bb58e5SYang Zhong         }
687d9bb58e5SYang Zhong 
688d9bb58e5SYang Zhong         cpu->halted = 0;
689d9bb58e5SYang Zhong     }
6900596fa11SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
691d9bb58e5SYang Zhong 
692d9bb58e5SYang Zhong     return false;
693d9bb58e5SYang Zhong }
694d9bb58e5SYang Zhong 
cpu_handle_debug_exception(CPUState * cpu)695d9bb58e5SYang Zhong static inline void cpu_handle_debug_exception(CPUState *cpu)
696d9bb58e5SYang Zhong {
697991bd65dSRichard Henderson     const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
698d9bb58e5SYang Zhong     CPUWatchpoint *wp;
699d9bb58e5SYang Zhong 
700d9bb58e5SYang Zhong     if (!cpu->watchpoint_hit) {
701d9bb58e5SYang Zhong         QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
702d9bb58e5SYang Zhong             wp->flags &= ~BP_WATCHPOINT_HIT;
703d9bb58e5SYang Zhong         }
704d9bb58e5SYang Zhong     }
705d9bb58e5SYang Zhong 
706991bd65dSRichard Henderson     if (tcg_ops->debug_excp_handler) {
707991bd65dSRichard Henderson         tcg_ops->debug_excp_handler(cpu);
708d9bb58e5SYang Zhong     }
709710384d0SEduardo Habkost }
710d9bb58e5SYang Zhong 
cpu_handle_exception(CPUState * cpu,int * ret)711d9bb58e5SYang Zhong static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
712d9bb58e5SYang Zhong {
71317b50b0cSPavel Dovgalyuk     if (cpu->exception_index < 0) {
71417b50b0cSPavel Dovgalyuk #ifndef CONFIG_USER_ONLY
71517b50b0cSPavel Dovgalyuk         if (replay_has_exception()
716a953b5faSRichard Henderson             && cpu->neg.icount_decr.u16.low + cpu->icount_extra == 0) {
717a11bbb6aSAlex Bennée             /* Execute just one insn to trigger exception pending in the log */
718c3e97f64SPavel Dovgalyuk             cpu->cflags_next_tb = (curr_cflags(cpu) & ~CF_USE_ICOUNT)
719cf9b5790SRichard Henderson                 | CF_NOIRQ | 1;
72017b50b0cSPavel Dovgalyuk         }
72117b50b0cSPavel Dovgalyuk #endif
72217b50b0cSPavel Dovgalyuk         return false;
72317b50b0cSPavel Dovgalyuk     }
724991bd65dSRichard Henderson 
725d9bb58e5SYang Zhong     if (cpu->exception_index >= EXCP_INTERRUPT) {
726d9bb58e5SYang Zhong         /* exit request from the cpu execution loop */
727d9bb58e5SYang Zhong         *ret = cpu->exception_index;
728d9bb58e5SYang Zhong         if (*ret == EXCP_DEBUG) {
729d9bb58e5SYang Zhong             cpu_handle_debug_exception(cpu);
730d9bb58e5SYang Zhong         }
731d9bb58e5SYang Zhong         cpu->exception_index = -1;
732d9bb58e5SYang Zhong         return true;
733991bd65dSRichard Henderson     }
734991bd65dSRichard Henderson 
735d9bb58e5SYang Zhong #if defined(CONFIG_USER_ONLY)
736991bd65dSRichard Henderson     /*
737991bd65dSRichard Henderson      * If user mode only, we simulate a fake exception which will be
738991bd65dSRichard Henderson      * handled outside the cpu execution loop.
739991bd65dSRichard Henderson      */
740d9bb58e5SYang Zhong #if defined(TARGET_I386)
741991bd65dSRichard Henderson     const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
742991bd65dSRichard Henderson     tcg_ops->fake_user_interrupt(cpu);
74312096421SPhilippe Mathieu-Daudé #endif /* TARGET_I386 */
744d9bb58e5SYang Zhong     *ret = cpu->exception_index;
745d9bb58e5SYang Zhong     cpu->exception_index = -1;
746d9bb58e5SYang Zhong     return true;
747d9bb58e5SYang Zhong #else
748d9bb58e5SYang Zhong     if (replay_exception()) {
749991bd65dSRichard Henderson         const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
750991bd65dSRichard Henderson 
751195801d7SStefan Hajnoczi         bql_lock();
752991bd65dSRichard Henderson         tcg_ops->do_interrupt(cpu);
753195801d7SStefan Hajnoczi         bql_unlock();
754d9bb58e5SYang Zhong         cpu->exception_index = -1;
755a7ba744fSLuc Michel 
756a7ba744fSLuc Michel         if (unlikely(cpu->singlestep_enabled)) {
757a7ba744fSLuc Michel             /*
758a7ba744fSLuc Michel              * After processing the exception, ensure an EXCP_DEBUG is
759a7ba744fSLuc Michel              * raised when single-stepping so that GDB doesn't miss the
760a7ba744fSLuc Michel              * next instruction.
761a7ba744fSLuc Michel              */
762a7ba744fSLuc Michel             *ret = EXCP_DEBUG;
763a7ba744fSLuc Michel             cpu_handle_debug_exception(cpu);
764a7ba744fSLuc Michel             return true;
765a7ba744fSLuc Michel         }
766d9bb58e5SYang Zhong     } else if (!replay_has_interrupt()) {
767d9bb58e5SYang Zhong         /* give a chance to iothread in replay mode */
768d9bb58e5SYang Zhong         *ret = EXCP_INTERRUPT;
769d9bb58e5SYang Zhong         return true;
770d9bb58e5SYang Zhong     }
771d9bb58e5SYang Zhong #endif
772d9bb58e5SYang Zhong 
773d9bb58e5SYang Zhong     return false;
774d9bb58e5SYang Zhong }
775d9bb58e5SYang Zhong 
icount_exit_request(CPUState * cpu)77693c6091bSPhilippe Mathieu-Daudé static inline bool icount_exit_request(CPUState *cpu)
77793c6091bSPhilippe Mathieu-Daudé {
77893c6091bSPhilippe Mathieu-Daudé     if (!icount_enabled()) {
77993c6091bSPhilippe Mathieu-Daudé         return false;
78093c6091bSPhilippe Mathieu-Daudé     }
78193c6091bSPhilippe Mathieu-Daudé     if (cpu->cflags_next_tb != -1 && !(cpu->cflags_next_tb & CF_USE_ICOUNT)) {
78293c6091bSPhilippe Mathieu-Daudé         return false;
78393c6091bSPhilippe Mathieu-Daudé     }
78493c6091bSPhilippe Mathieu-Daudé     return cpu->neg.icount_decr.u16.low + cpu->icount_extra == 0;
78593c6091bSPhilippe Mathieu-Daudé }
78693c6091bSPhilippe Mathieu-Daudé 
cpu_handle_interrupt(CPUState * cpu,TranslationBlock ** last_tb)787d9bb58e5SYang Zhong static inline bool cpu_handle_interrupt(CPUState *cpu,
788d9bb58e5SYang Zhong                                         TranslationBlock **last_tb)
789d9bb58e5SYang Zhong {
790aff0e204SAlex Bennée     /*
791aff0e204SAlex Bennée      * If we have requested custom cflags with CF_NOIRQ we should
792aff0e204SAlex Bennée      * skip checking here. Any pending interrupts will get picked up
793aff0e204SAlex Bennée      * by the next TB we execute under normal cflags.
794aff0e204SAlex Bennée      */
795aff0e204SAlex Bennée     if (cpu->cflags_next_tb != -1 && cpu->cflags_next_tb & CF_NOIRQ) {
796aff0e204SAlex Bennée         return false;
797aff0e204SAlex Bennée     }
798aff0e204SAlex Bennée 
79917b50b0cSPavel Dovgalyuk     /* Clear the interrupt flag now since we're processing
80017b50b0cSPavel Dovgalyuk      * cpu->interrupt_request and cpu->exit_request.
801d84be02dSDavid Hildenbrand      * Ensure zeroing happens before reading cpu->exit_request or
802d84be02dSDavid Hildenbrand      * cpu->interrupt_request (see also smp_wmb in cpu_exit())
80317b50b0cSPavel Dovgalyuk      */
804a953b5faSRichard Henderson     qatomic_set_mb(&cpu->neg.icount_decr.u16.high, 0);
805d9bb58e5SYang Zhong 
806d73415a3SStefan Hajnoczi     if (unlikely(qatomic_read(&cpu->interrupt_request))) {
807d9bb58e5SYang Zhong         int interrupt_request;
808195801d7SStefan Hajnoczi         bql_lock();
809d9bb58e5SYang Zhong         interrupt_request = cpu->interrupt_request;
810d9bb58e5SYang Zhong         if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
811d9bb58e5SYang Zhong             /* Mask out external interrupts for this step. */
812d9bb58e5SYang Zhong             interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
813d9bb58e5SYang Zhong         }
814d9bb58e5SYang Zhong         if (interrupt_request & CPU_INTERRUPT_DEBUG) {
815d9bb58e5SYang Zhong             cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
816d9bb58e5SYang Zhong             cpu->exception_index = EXCP_DEBUG;
817195801d7SStefan Hajnoczi             bql_unlock();
818d9bb58e5SYang Zhong             return true;
819d9bb58e5SYang Zhong         }
82077c0fc4eSPhilippe Mathieu-Daudé #if !defined(CONFIG_USER_ONLY)
821d9bb58e5SYang Zhong         if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
822d9bb58e5SYang Zhong             /* Do nothing */
823d9bb58e5SYang Zhong         } else if (interrupt_request & CPU_INTERRUPT_HALT) {
824d9bb58e5SYang Zhong             replay_interrupt();
825d9bb58e5SYang Zhong             cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
826d9bb58e5SYang Zhong             cpu->halted = 1;
827d9bb58e5SYang Zhong             cpu->exception_index = EXCP_HLT;
828195801d7SStefan Hajnoczi             bql_unlock();
829d9bb58e5SYang Zhong             return true;
830d9bb58e5SYang Zhong         }
831d9bb58e5SYang Zhong #if defined(TARGET_I386)
832d9bb58e5SYang Zhong         else if (interrupt_request & CPU_INTERRUPT_INIT) {
833d9bb58e5SYang Zhong             X86CPU *x86_cpu = X86_CPU(cpu);
834d9bb58e5SYang Zhong             CPUArchState *env = &x86_cpu->env;
835d9bb58e5SYang Zhong             replay_interrupt();
836d9bb58e5SYang Zhong             cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0, 0);
837d9bb58e5SYang Zhong             do_cpu_init(x86_cpu);
838d9bb58e5SYang Zhong             cpu->exception_index = EXCP_HALTED;
839195801d7SStefan Hajnoczi             bql_unlock();
840d9bb58e5SYang Zhong             return true;
841d9bb58e5SYang Zhong         }
842d9bb58e5SYang Zhong #else
843d9bb58e5SYang Zhong         else if (interrupt_request & CPU_INTERRUPT_RESET) {
844d9bb58e5SYang Zhong             replay_interrupt();
845d9bb58e5SYang Zhong             cpu_reset(cpu);
846195801d7SStefan Hajnoczi             bql_unlock();
847d9bb58e5SYang Zhong             return true;
848d9bb58e5SYang Zhong         }
84977c0fc4eSPhilippe Mathieu-Daudé #endif /* !TARGET_I386 */
850d9bb58e5SYang Zhong         /* The target hook has 3 exit conditions:
851d9bb58e5SYang Zhong            False when the interrupt isn't processed,
852d9bb58e5SYang Zhong            True when it is, and we should restart on a new TB,
853d9bb58e5SYang Zhong            and via longjmp via cpu_loop_exit.  */
854d9bb58e5SYang Zhong         else {
855991bd65dSRichard Henderson             const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
85677c0fc4eSPhilippe Mathieu-Daudé 
857*de680286SPeter Maydell             if (tcg_ops->cpu_exec_interrupt(cpu, interrupt_request)) {
858b7e9a4a9SRichard Henderson                 if (!tcg_ops->need_replay_interrupt ||
859b7e9a4a9SRichard Henderson                     tcg_ops->need_replay_interrupt(interrupt_request)) {
860d9bb58e5SYang Zhong                     replay_interrupt();
8614084893dSPavel Dovgalyuk                 }
862ba3c35d9SRichard Henderson                 /*
863ba3c35d9SRichard Henderson                  * After processing the interrupt, ensure an EXCP_DEBUG is
864ba3c35d9SRichard Henderson                  * raised when single-stepping so that GDB doesn't miss the
865ba3c35d9SRichard Henderson                  * next instruction.
866ba3c35d9SRichard Henderson                  */
8675b7b197cSLuc Michel                 if (unlikely(cpu->singlestep_enabled)) {
8685b7b197cSLuc Michel                     cpu->exception_index = EXCP_DEBUG;
869195801d7SStefan Hajnoczi                     bql_unlock();
8705b7b197cSLuc Michel                     return true;
8715b7b197cSLuc Michel                 }
8725b7b197cSLuc Michel                 cpu->exception_index = -1;
873d9bb58e5SYang Zhong                 *last_tb = NULL;
874d9bb58e5SYang Zhong             }
875d9bb58e5SYang Zhong             /* The target hook may have updated the 'cpu->interrupt_request';
876d9bb58e5SYang Zhong              * reload the 'interrupt_request' value */
877d9bb58e5SYang Zhong             interrupt_request = cpu->interrupt_request;
878d9bb58e5SYang Zhong         }
87977c0fc4eSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
880d9bb58e5SYang Zhong         if (interrupt_request & CPU_INTERRUPT_EXITTB) {
881d9bb58e5SYang Zhong             cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
882d9bb58e5SYang Zhong             /* ensure that no TB jump will be modified as
883d9bb58e5SYang Zhong                the program flow was changed */
884d9bb58e5SYang Zhong             *last_tb = NULL;
885d9bb58e5SYang Zhong         }
886d9bb58e5SYang Zhong 
887d9bb58e5SYang Zhong         /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
888195801d7SStefan Hajnoczi         bql_unlock();
889d9bb58e5SYang Zhong     }
890d9bb58e5SYang Zhong 
891d9bb58e5SYang Zhong     /* Finally, check if we need to exit to the main loop.  */
89293c6091bSPhilippe Mathieu-Daudé     if (unlikely(qatomic_read(&cpu->exit_request)) || icount_exit_request(cpu)) {
893d73415a3SStefan Hajnoczi         qatomic_set(&cpu->exit_request, 0);
8945f3bdfd4SPavel Dovgalyuk         if (cpu->exception_index == -1) {
895d9bb58e5SYang Zhong             cpu->exception_index = EXCP_INTERRUPT;
8965f3bdfd4SPavel Dovgalyuk         }
897d9bb58e5SYang Zhong         return true;
898d9bb58e5SYang Zhong     }
899d9bb58e5SYang Zhong 
900d9bb58e5SYang Zhong     return false;
901d9bb58e5SYang Zhong }
902d9bb58e5SYang Zhong 
cpu_loop_exec_tb(CPUState * cpu,TranslationBlock * tb,vaddr pc,TranslationBlock ** last_tb,int * tb_exit)903d9bb58e5SYang Zhong static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
904f0a08b09SAnton Johansson                                     vaddr pc, TranslationBlock **last_tb,
905f0a08b09SAnton Johansson                                     int *tb_exit)
906d9bb58e5SYang Zhong {
907fbf59aadSRichard Henderson     trace_exec_tb(tb, pc);
908eba40358SRichard Henderson     tb = cpu_tb_exec(cpu, tb, tb_exit);
909d9bb58e5SYang Zhong     if (*tb_exit != TB_EXIT_REQUESTED) {
910d9bb58e5SYang Zhong         *last_tb = tb;
911d9bb58e5SYang Zhong         return;
912d9bb58e5SYang Zhong     }
913d9bb58e5SYang Zhong 
914d9bb58e5SYang Zhong     *last_tb = NULL;
9150650fc1eSPhilippe Mathieu-Daudé     if (cpu_loop_exit_requested(cpu)) {
916d9bb58e5SYang Zhong         /* Something asked us to stop executing chained TBs; just
917d9bb58e5SYang Zhong          * continue round the main loop. Whatever requested the exit
918d9bb58e5SYang Zhong          * will also have set something else (eg exit_request or
91917b50b0cSPavel Dovgalyuk          * interrupt_request) which will be handled by
92017b50b0cSPavel Dovgalyuk          * cpu_handle_interrupt.  cpu_handle_interrupt will also
92117b50b0cSPavel Dovgalyuk          * clear cpu->icount_decr.u16.high.
922d9bb58e5SYang Zhong          */
923d9bb58e5SYang Zhong         return;
924d9bb58e5SYang Zhong     }
925d9bb58e5SYang Zhong 
926d9bb58e5SYang Zhong     /* Instruction counter expired.  */
927740b1759SClaudio Fontana     assert(icount_enabled());
928d9bb58e5SYang Zhong #ifndef CONFIG_USER_ONLY
929d9bb58e5SYang Zhong     /* Ensure global icount has gone forward */
9308191d368SClaudio Fontana     icount_update(cpu);
931d9bb58e5SYang Zhong     /* Refill decrementer and continue execution.  */
9320650fc1eSPhilippe Mathieu-Daudé     int32_t insns_left = MIN(0xffff, cpu->icount_budget);
933a953b5faSRichard Henderson     cpu->neg.icount_decr.u16.low = insns_left;
934d9bb58e5SYang Zhong     cpu->icount_extra = cpu->icount_budget - insns_left;
935bc662a33SAlex Bennée 
936bc662a33SAlex Bennée     /*
937bc662a33SAlex Bennée      * If the next tb has more instructions than we have left to
938bc662a33SAlex Bennée      * execute we need to ensure we find/generate a TB with exactly
939bc662a33SAlex Bennée      * insns_left instructions in it.
940d9bb58e5SYang Zhong      */
941c8cf47a9SPeter Maydell     if (insns_left > 0 && insns_left < tb->icount)  {
942c8cf47a9SPeter Maydell         assert(insns_left <= CF_COUNT_MASK);
943c8cf47a9SPeter Maydell         assert(cpu->icount_extra == 0);
944bc662a33SAlex Bennée         cpu->cflags_next_tb = (tb->cflags & ~CF_COUNT_MASK) | insns_left;
945d9bb58e5SYang Zhong     }
946d9bb58e5SYang Zhong #endif
947d9bb58e5SYang Zhong }
948d9bb58e5SYang Zhong 
949d9bb58e5SYang Zhong /* main execution loop */
950d9bb58e5SYang Zhong 
95161710a7eSRichard Henderson static int __attribute__((noinline))
cpu_exec_loop(CPUState * cpu,SyncClocks * sc)95261710a7eSRichard Henderson cpu_exec_loop(CPUState *cpu, SyncClocks *sc)
953d9bb58e5SYang Zhong {
954d9bb58e5SYang Zhong     int ret;
955d9bb58e5SYang Zhong 
956d9bb58e5SYang Zhong     /* if an exception is pending, we execute it here */
957d9bb58e5SYang Zhong     while (!cpu_handle_exception(cpu, &ret)) {
958d9bb58e5SYang Zhong         TranslationBlock *last_tb = NULL;
959d9bb58e5SYang Zhong         int tb_exit = 0;
960d9bb58e5SYang Zhong 
961d9bb58e5SYang Zhong         while (!cpu_handle_interrupt(cpu, &last_tb)) {
9629b990ee5SRichard Henderson             TranslationBlock *tb;
963bb5de525SAnton Johansson             vaddr pc;
964bb5de525SAnton Johansson             uint64_t cs_base;
96511c1d5f8SRichard Henderson             uint32_t flags, cflags;
9669b990ee5SRichard Henderson 
967b77af26eSRichard Henderson             cpu_get_tb_cpu_state(cpu_env(cpu), &pc, &cs_base, &flags);
96810c37828SRichard Henderson 
96911c1d5f8SRichard Henderson             /*
97011c1d5f8SRichard Henderson              * When requested, use an exact setting for cflags for the next
97111c1d5f8SRichard Henderson              * execution.  This is used for icount, precise smc, and stop-
97211c1d5f8SRichard Henderson              * after-access watchpoints.  Since this request should never
97311c1d5f8SRichard Henderson              * have CF_INVALID set, -1 is a convenient invalid value that
97411c1d5f8SRichard Henderson              * does not require tcg headers for cpu_common_reset.
97511c1d5f8SRichard Henderson              */
97611c1d5f8SRichard Henderson             cflags = cpu->cflags_next_tb;
9779b990ee5SRichard Henderson             if (cflags == -1) {
978c0ae396aSAlex Bennée                 cflags = curr_cflags(cpu);
9799b990ee5SRichard Henderson             } else {
9809b990ee5SRichard Henderson                 cpu->cflags_next_tb = -1;
9819b990ee5SRichard Henderson             }
9829b990ee5SRichard Henderson 
98310c37828SRichard Henderson             if (check_for_breakpoints(cpu, pc, &cflags)) {
98410c37828SRichard Henderson                 break;
98510c37828SRichard Henderson             }
98611c1d5f8SRichard Henderson 
98711c1d5f8SRichard Henderson             tb = tb_lookup(cpu, pc, cs_base, flags, cflags);
98811c1d5f8SRichard Henderson             if (tb == NULL) {
9893371802fSRichard Henderson                 CPUJumpCache *jc;
990a976a99aSRichard Henderson                 uint32_t h;
991a976a99aSRichard Henderson 
99211c1d5f8SRichard Henderson                 mmap_lock();
99311c1d5f8SRichard Henderson                 tb = tb_gen_code(cpu, pc, cs_base, flags, cflags);
99411c1d5f8SRichard Henderson                 mmap_unlock();
9953371802fSRichard Henderson 
99611c1d5f8SRichard Henderson                 /*
99711c1d5f8SRichard Henderson                  * We add the TB in the virtual pc hash table
99811c1d5f8SRichard Henderson                  * for the fast lookup
99911c1d5f8SRichard Henderson                  */
1000a976a99aSRichard Henderson                 h = tb_jmp_cache_hash_func(pc);
10013371802fSRichard Henderson                 jc = cpu->tb_jmp_cache;
10023371802fSRichard Henderson                 jc->array[h].pc = pc;
10033371802fSRichard Henderson                 qatomic_set(&jc->array[h].tb, tb);
10043371802fSRichard Henderson             }
100511c1d5f8SRichard Henderson 
100611c1d5f8SRichard Henderson #ifndef CONFIG_USER_ONLY
100711c1d5f8SRichard Henderson             /*
100811c1d5f8SRichard Henderson              * We don't take care of direct jumps when address mapping
100911c1d5f8SRichard Henderson              * changes in system emulation.  So it's not safe to make a
101011c1d5f8SRichard Henderson              * direct jump to a TB spanning two pages because the mapping
101111c1d5f8SRichard Henderson              * for the second page can change.
101211c1d5f8SRichard Henderson              */
101328905cfbSRichard Henderson             if (tb_page_addr1(tb) != -1) {
101411c1d5f8SRichard Henderson                 last_tb = NULL;
101511c1d5f8SRichard Henderson             }
101611c1d5f8SRichard Henderson #endif
101711c1d5f8SRichard Henderson             /* See if we can patch the calling TB. */
101811c1d5f8SRichard Henderson             if (last_tb) {
101911c1d5f8SRichard Henderson                 tb_add_jump(last_tb, tb_exit, tb);
102011c1d5f8SRichard Henderson             }
102111c1d5f8SRichard Henderson 
1022fbf59aadSRichard Henderson             cpu_loop_exec_tb(cpu, tb, pc, &last_tb, &tb_exit);
102311c1d5f8SRichard Henderson 
1024d9bb58e5SYang Zhong             /* Try to align the host and virtual clocks
1025d9bb58e5SYang Zhong                if the guest is in advance */
102661710a7eSRichard Henderson             align_clocks(sc, cpu);
1027d9bb58e5SYang Zhong         }
1028d9bb58e5SYang Zhong     }
102961710a7eSRichard Henderson     return ret;
103061710a7eSRichard Henderson }
103161710a7eSRichard Henderson 
cpu_exec_setjmp(CPUState * cpu,SyncClocks * sc)103261710a7eSRichard Henderson static int cpu_exec_setjmp(CPUState *cpu, SyncClocks *sc)
103361710a7eSRichard Henderson {
103461710a7eSRichard Henderson     /* Prepare setjmp context for exception handling. */
103561710a7eSRichard Henderson     if (unlikely(sigsetjmp(cpu->jmp_env, 0) != 0)) {
1036cb62bd15SRichard Henderson         cpu_exec_longjmp_cleanup(cpu);
103761710a7eSRichard Henderson     }
103861710a7eSRichard Henderson 
103961710a7eSRichard Henderson     return cpu_exec_loop(cpu, sc);
104061710a7eSRichard Henderson }
104161710a7eSRichard Henderson 
cpu_exec(CPUState * cpu)104261710a7eSRichard Henderson int cpu_exec(CPUState *cpu)
104361710a7eSRichard Henderson {
104461710a7eSRichard Henderson     int ret;
104561710a7eSRichard Henderson     SyncClocks sc = { 0 };
104661710a7eSRichard Henderson 
104761710a7eSRichard Henderson     /* replay_interrupt may need current_cpu */
104861710a7eSRichard Henderson     current_cpu = cpu;
104961710a7eSRichard Henderson 
105061710a7eSRichard Henderson     if (cpu_handle_halt(cpu)) {
105161710a7eSRichard Henderson         return EXCP_HALTED;
105261710a7eSRichard Henderson     }
105361710a7eSRichard Henderson 
1054f5e9362aSPhilippe Mathieu-Daudé     RCU_READ_LOCK_GUARD();
105561710a7eSRichard Henderson     cpu_exec_enter(cpu);
105661710a7eSRichard Henderson 
105761710a7eSRichard Henderson     /*
105861710a7eSRichard Henderson      * Calculate difference between guest clock and host clock.
105961710a7eSRichard Henderson      * This delay includes the delay of the last cycle, so
106061710a7eSRichard Henderson      * what we have to do is sleep until it is 0. As for the
106161710a7eSRichard Henderson      * advance/delay we gain here, we try to fix it next time.
106261710a7eSRichard Henderson      */
106361710a7eSRichard Henderson     init_delay_params(&sc, cpu);
106461710a7eSRichard Henderson 
106561710a7eSRichard Henderson     ret = cpu_exec_setjmp(cpu, &sc);
1066d9bb58e5SYang Zhong 
1067035ba06cSEduardo Habkost     cpu_exec_exit(cpu);
1068d9bb58e5SYang Zhong     return ret;
1069d9bb58e5SYang Zhong }
1070740b1759SClaudio Fontana 
tcg_exec_realizefn(CPUState * cpu,Error ** errp)1071fa312f2eSPhilippe Mathieu-Daudé bool tcg_exec_realizefn(CPUState *cpu, Error **errp)
10727df5e3d6SClaudio Fontana {
10737df5e3d6SClaudio Fontana     static bool tcg_target_initialized;
10747df5e3d6SClaudio Fontana 
10757df5e3d6SClaudio Fontana     if (!tcg_target_initialized) {
10760487c631SPeter Maydell         /* Check mandatory TCGCPUOps handlers */
10770487c631SPeter Maydell #ifndef CONFIG_USER_ONLY
10780487c631SPeter Maydell         assert(cpu->cc->tcg_ops->cpu_exec_halt);
1079*de680286SPeter Maydell         assert(cpu->cc->tcg_ops->cpu_exec_interrupt);
10800487c631SPeter Maydell #endif /* !CONFIG_USER_ONLY */
1081991bd65dSRichard Henderson         cpu->cc->tcg_ops->initialize();
10827df5e3d6SClaudio Fontana         tcg_target_initialized = true;
10837df5e3d6SClaudio Fontana     }
10847df5e3d6SClaudio Fontana 
10854e4fa6c1SRichard Henderson     cpu->tb_jmp_cache = g_new0(CPUJumpCache, 1);
10864e4fa6c1SRichard Henderson     tlb_init(cpu);
10877df5e3d6SClaudio Fontana #ifndef CONFIG_USER_ONLY
10887df5e3d6SClaudio Fontana     tcg_iommu_init_notifier_list(cpu);
10897df5e3d6SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
10904e4fa6c1SRichard Henderson     /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */
1091fa312f2eSPhilippe Mathieu-Daudé 
1092fa312f2eSPhilippe Mathieu-Daudé     return true;
10937df5e3d6SClaudio Fontana }
10947df5e3d6SClaudio Fontana 
10957df5e3d6SClaudio Fontana /* undo the initializations in reverse order */
tcg_exec_unrealizefn(CPUState * cpu)10967df5e3d6SClaudio Fontana void tcg_exec_unrealizefn(CPUState *cpu)
10977df5e3d6SClaudio Fontana {
10987df5e3d6SClaudio Fontana #ifndef CONFIG_USER_ONLY
10997df5e3d6SClaudio Fontana     tcg_iommu_free_notifier_list(cpu);
11007df5e3d6SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
11017df5e3d6SClaudio Fontana 
11027df5e3d6SClaudio Fontana     tlb_destroy(cpu);
11034731f89bSEmilio Cota     g_free_rcu(cpu->tb_jmp_cache, rcu);
11047df5e3d6SClaudio Fontana }
1105