Revert "display: move display functionality to Qt5 GUI"
[sdk/emulator/qemu.git] / cpu-exec.c
1 /*
2  *  emulator main execution loop
3  *
4  *  Copyright (c) 2003-2005 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 #include "cpu.h"
21 #include "trace.h"
22 #include "disas/disas.h"
23 #include "tcg.h"
24 #include "qemu/atomic.h"
25 #include "sysemu/qtest.h"
26 #include "qemu/timer.h"
27 #include "exec/address-spaces.h"
28 #include "qemu/rcu.h"
29 #include "exec/tb-hash.h"
30 #include "exec/log.h"
31 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
32 #include "hw/i386/apic.h"
33 #endif
34 #include "sysemu/replay.h"
35
36 #include "sysemu/hax.h"
37
38 /* -icount align implementation. */
39
40 typedef struct SyncClocks {
41     int64_t diff_clk;
42     int64_t last_cpu_icount;
43     int64_t realtime_clock;
44 } SyncClocks;
45
46 #if !defined(CONFIG_USER_ONLY)
47 /* Allow the guest to have a max 3ms advance.
48  * The difference between the 2 clocks could therefore
49  * oscillate around 0.
50  */
51 #define VM_CLOCK_ADVANCE 3000000
52 #define THRESHOLD_REDUCE 1.5
53 #define MAX_DELAY_PRINT_RATE 2000000000LL
54 #define MAX_NB_PRINTS 100
55
56 static void align_clocks(SyncClocks *sc, const CPUState *cpu)
57 {
58     int64_t cpu_icount;
59
60     if (!icount_align_option) {
61         return;
62     }
63
64     cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
65     sc->diff_clk += cpu_icount_to_ns(sc->last_cpu_icount - cpu_icount);
66     sc->last_cpu_icount = cpu_icount;
67
68     if (sc->diff_clk > VM_CLOCK_ADVANCE) {
69 #ifndef _WIN32
70         struct timespec sleep_delay, rem_delay;
71         sleep_delay.tv_sec = sc->diff_clk / 1000000000LL;
72         sleep_delay.tv_nsec = sc->diff_clk % 1000000000LL;
73         if (nanosleep(&sleep_delay, &rem_delay) < 0) {
74             sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec;
75         } else {
76             sc->diff_clk = 0;
77         }
78 #else
79         Sleep(sc->diff_clk / SCALE_MS);
80         sc->diff_clk = 0;
81 #endif
82     }
83 }
84
85 static void print_delay(const SyncClocks *sc)
86 {
87     static float threshold_delay;
88     static int64_t last_realtime_clock;
89     static int nb_prints;
90
91     if (icount_align_option &&
92         sc->realtime_clock - last_realtime_clock >= MAX_DELAY_PRINT_RATE &&
93         nb_prints < MAX_NB_PRINTS) {
94         if ((-sc->diff_clk / (float)1000000000LL > threshold_delay) ||
95             (-sc->diff_clk / (float)1000000000LL <
96              (threshold_delay - THRESHOLD_REDUCE))) {
97             threshold_delay = (-sc->diff_clk / 1000000000LL) + 1;
98             printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
99                    threshold_delay - 1,
100                    threshold_delay);
101             nb_prints++;
102             last_realtime_clock = sc->realtime_clock;
103         }
104     }
105 }
106
107 static void init_delay_params(SyncClocks *sc,
108                               const CPUState *cpu)
109 {
110     if (!icount_align_option) {
111         return;
112     }
113     sc->realtime_clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
114     sc->diff_clk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - sc->realtime_clock;
115     sc->last_cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
116     if (sc->diff_clk < max_delay) {
117         max_delay = sc->diff_clk;
118     }
119     if (sc->diff_clk > max_advance) {
120         max_advance = sc->diff_clk;
121     }
122
123     /* Print every 2s max if the guest is late. We limit the number
124        of printed messages to NB_PRINT_MAX(currently 100) */
125     print_delay(sc);
126 }
127 #else
128 static void align_clocks(SyncClocks *sc, const CPUState *cpu)
129 {
130 }
131
132 static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
133 {
134 }
135 #endif /* CONFIG USER ONLY */
136
137 /* Execute a TB, and fix up the CPU state afterwards if necessary */
138 static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb)
139 {
140     CPUArchState *env = cpu->env_ptr;
141     uintptr_t next_tb;
142     uint8_t *tb_ptr = itb->tc_ptr;
143
144     qemu_log_mask_and_addr(CPU_LOG_EXEC, itb->pc,
145                            "Trace %p [" TARGET_FMT_lx "] %s\n",
146                            itb->tc_ptr, itb->pc, lookup_symbol(itb->pc));
147
148 #if defined(DEBUG_DISAS)
149     if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
150 #if defined(TARGET_I386)
151         log_cpu_state(cpu, CPU_DUMP_CCOP);
152 #elif defined(TARGET_M68K)
153         /* ??? Should not modify env state for dumping.  */
154         cpu_m68k_flush_flags(env, env->cc_op);
155         env->cc_op = CC_OP_FLAGS;
156         env->sr = (env->sr & 0xffe0) | env->cc_dest | (env->cc_x << 4);
157         log_cpu_state(cpu, 0);
158 #else
159         log_cpu_state(cpu, 0);
160 #endif
161     }
162 #endif /* DEBUG_DISAS */
163
164     cpu->can_do_io = !use_icount;
165     next_tb = tcg_qemu_tb_exec(env, tb_ptr);
166     cpu->can_do_io = 1;
167     trace_exec_tb_exit((void *) (next_tb & ~TB_EXIT_MASK),
168                        next_tb & TB_EXIT_MASK);
169
170     if ((next_tb & TB_EXIT_MASK) > TB_EXIT_IDX1) {
171         /* We didn't start executing this TB (eg because the instruction
172          * counter hit zero); we must restore the guest PC to the address
173          * of the start of the TB.
174          */
175         CPUClass *cc = CPU_GET_CLASS(cpu);
176         TranslationBlock *tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
177         qemu_log_mask_and_addr(CPU_LOG_EXEC, itb->pc,
178                                "Stopped execution of TB chain before %p ["
179                                TARGET_FMT_lx "] %s\n",
180                                itb->tc_ptr, itb->pc, lookup_symbol(itb->pc));
181         if (cc->synchronize_from_tb) {
182             cc->synchronize_from_tb(cpu, tb);
183         } else {
184             assert(cc->set_pc);
185             cc->set_pc(cpu, tb->pc);
186         }
187     }
188     if ((next_tb & TB_EXIT_MASK) == TB_EXIT_REQUESTED) {
189         /* We were asked to stop executing TBs (probably a pending
190          * interrupt. We've now stopped, so clear the flag.
191          */
192         cpu->tcg_exit_req = 0;
193     }
194     return next_tb;
195 }
196
197 /* Execute the code without caching the generated code. An interpreter
198    could be used if available. */
199 static void cpu_exec_nocache(CPUState *cpu, int max_cycles,
200                              TranslationBlock *orig_tb, bool ignore_icount)
201 {
202     TranslationBlock *tb;
203
204     /* Should never happen.
205        We only end up here when an existing TB is too long.  */
206     if (max_cycles > CF_COUNT_MASK)
207         max_cycles = CF_COUNT_MASK;
208
209     tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
210                      max_cycles | CF_NOCACHE
211                          | (ignore_icount ? CF_IGNORE_ICOUNT : 0));
212     tb->orig_tb = tcg_ctx.tb_ctx.tb_invalidated_flag ? NULL : orig_tb;
213     cpu->current_tb = tb;
214     /* execute the generated code */
215     trace_exec_tb_nocache(tb, tb->pc);
216     cpu_tb_exec(cpu, tb);
217     cpu->current_tb = NULL;
218     tb_phys_invalidate(tb, -1);
219     tb_free(tb);
220 }
221
222 static TranslationBlock *tb_find_physical(CPUState *cpu,
223                                           target_ulong pc,
224                                           target_ulong cs_base,
225                                           uint64_t flags)
226 {
227     CPUArchState *env = (CPUArchState *)cpu->env_ptr;
228     TranslationBlock *tb, **ptb1;
229     unsigned int h;
230     tb_page_addr_t phys_pc, phys_page1;
231     target_ulong virt_page2;
232
233     tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
234
235     /* find translated block using physical mappings */
236     phys_pc = get_page_addr_code(env, pc);
237     phys_page1 = phys_pc & TARGET_PAGE_MASK;
238     h = tb_phys_hash_func(phys_pc);
239     ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
240     for(;;) {
241         tb = *ptb1;
242         if (!tb) {
243             return NULL;
244         }
245         if (tb->pc == pc &&
246             tb->page_addr[0] == phys_page1 &&
247             tb->cs_base == cs_base &&
248             tb->flags == flags) {
249             /* check next page if needed */
250             if (tb->page_addr[1] != -1) {
251                 tb_page_addr_t phys_page2;
252
253                 virt_page2 = (pc & TARGET_PAGE_MASK) +
254                     TARGET_PAGE_SIZE;
255                 phys_page2 = get_page_addr_code(env, virt_page2);
256                 if (tb->page_addr[1] == phys_page2) {
257                     break;
258                 }
259             } else {
260                 break;
261             }
262         }
263         ptb1 = &tb->phys_hash_next;
264     }
265
266     /* Move the TB to the head of the list */
267     *ptb1 = tb->phys_hash_next;
268     tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
269     tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
270     return tb;
271 }
272
273 static TranslationBlock *tb_find_slow(CPUState *cpu,
274                                       target_ulong pc,
275                                       target_ulong cs_base,
276                                       uint64_t flags)
277 {
278     TranslationBlock *tb;
279
280     tb = tb_find_physical(cpu, pc, cs_base, flags);
281     if (tb) {
282         goto found;
283     }
284
285 #ifdef CONFIG_USER_ONLY
286     /* mmap_lock is needed by tb_gen_code, and mmap_lock must be
287      * taken outside tb_lock.  Since we're momentarily dropping
288      * tb_lock, there's a chance that our desired tb has been
289      * translated.
290      */
291     tb_unlock();
292     mmap_lock();
293     tb_lock();
294     tb = tb_find_physical(cpu, pc, cs_base, flags);
295     if (tb) {
296         mmap_unlock();
297         goto found;
298     }
299 #endif
300
301     /* if no translated code available, then translate it now */
302     tb = tb_gen_code(cpu, pc, cs_base, flags, 0);
303
304 #ifdef CONFIG_USER_ONLY
305     mmap_unlock();
306 #endif
307
308 found:
309     /* we add the TB in the virtual pc hash table */
310     cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb;
311     return tb;
312 }
313
314 static inline TranslationBlock *tb_find_fast(CPUState *cpu)
315 {
316     CPUArchState *env = (CPUArchState *)cpu->env_ptr;
317     TranslationBlock *tb;
318     target_ulong cs_base, pc;
319     int flags;
320
321     /* we record a subset of the CPU state. It will
322        always be the same before a given translated block
323        is executed. */
324     cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
325     tb = cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];
326     if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base ||
327                  tb->flags != flags)) {
328         tb = tb_find_slow(cpu, pc, cs_base, flags);
329     }
330     return tb;
331 }
332
333 static void cpu_handle_debug_exception(CPUState *cpu)
334 {
335     CPUClass *cc = CPU_GET_CLASS(cpu);
336     CPUWatchpoint *wp;
337
338     if (!cpu->watchpoint_hit) {
339         QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
340             wp->flags &= ~BP_WATCHPOINT_HIT;
341         }
342     }
343
344     cc->debug_excp_handler(cpu);
345 }
346
347 /* main execution loop */
348
349 int cpu_exec(CPUState *cpu)
350 {
351     CPUClass *cc = CPU_GET_CLASS(cpu);
352 #ifdef TARGET_I386
353     X86CPU *x86_cpu = X86_CPU(cpu);
354     CPUArchState *env = &x86_cpu->env;
355 #endif
356     int ret, interrupt_request;
357     TranslationBlock *tb;
358     uintptr_t next_tb;
359     SyncClocks sc;
360
361     /* replay_interrupt may need current_cpu */
362     current_cpu = cpu;
363
364     if (cpu->halted) {
365 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
366         if ((cpu->interrupt_request & CPU_INTERRUPT_POLL)
367             && replay_interrupt()) {
368             apic_poll_irq(x86_cpu->apic_state);
369             cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
370         }
371 #endif
372         if (!cpu_has_work(cpu)) {
373             current_cpu = NULL;
374             return EXCP_HALTED;
375         }
376
377         cpu->halted = 0;
378     }
379
380     atomic_mb_set(&tcg_current_cpu, cpu);
381     rcu_read_lock();
382
383     if (unlikely(atomic_mb_read(&exit_request))) {
384         cpu->exit_request = 1;
385     }
386
387     cc->cpu_exec_enter(cpu);
388
389     /* Calculate difference between guest clock and host clock.
390      * This delay includes the delay of the last cycle, so
391      * what we have to do is sleep until it is 0. As for the
392      * advance/delay we gain here, we try to fix it next time.
393      */
394     init_delay_params(&sc, cpu);
395
396     /* prepare setjmp context for exception handling */
397     for(;;) {
398         if (sigsetjmp(cpu->jmp_env, 0) == 0) {
399             /* if an exception is pending, we execute it here */
400             if (cpu->exception_index >= 0) {
401                 if (cpu->exception_index >= EXCP_INTERRUPT) {
402                     /* exit request from the cpu execution loop */
403                     ret = cpu->exception_index;
404                     if (ret == EXCP_DEBUG) {
405                         cpu_handle_debug_exception(cpu);
406                     }
407                     cpu->exception_index = -1;
408                     break;
409                 } else {
410 #if defined(CONFIG_USER_ONLY)
411                     /* if user mode only, we simulate a fake exception
412                        which will be handled outside the cpu execution
413                        loop */
414 #if defined(TARGET_I386)
415                     cc->do_interrupt(cpu);
416 #endif
417                     ret = cpu->exception_index;
418                     cpu->exception_index = -1;
419                     break;
420 #else
421                     if (replay_exception()) {
422                         cc->do_interrupt(cpu);
423                         cpu->exception_index = -1;
424                     } else if (!replay_has_interrupt()) {
425                         /* give a chance to iothread in replay mode */
426                         ret = EXCP_INTERRUPT;
427                         break;
428                     }
429 #endif
430                 }
431             } else if (replay_has_exception()
432                        && cpu->icount_decr.u16.low + cpu->icount_extra == 0) {
433                 /* try to cause an exception pending in the log */
434                 cpu_exec_nocache(cpu, 1, tb_find_fast(cpu), true);
435                 ret = -1;
436                 break;
437             }
438
439 #ifdef CONFIG_HAX
440             if (hax_enabled() && !hax_vcpu_exec(cpu))
441                 longjmp(cpu->jmp_env, 1);
442 #endif
443
444             next_tb = 0; /* force lookup of first TB */
445             for(;;) {
446                 interrupt_request = cpu->interrupt_request;
447                 if (unlikely(interrupt_request)) {
448                     if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
449                         /* Mask out external interrupts for this step. */
450                         interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
451                     }
452                     if (interrupt_request & CPU_INTERRUPT_DEBUG) {
453                         cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
454                         cpu->exception_index = EXCP_DEBUG;
455                         cpu_loop_exit(cpu);
456                     }
457                     if (replay_mode == REPLAY_MODE_PLAY
458                         && !replay_has_interrupt()) {
459                         /* Do nothing */
460                     } else if (interrupt_request & CPU_INTERRUPT_HALT) {
461                         replay_interrupt();
462                         cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
463                         cpu->halted = 1;
464                         cpu->exception_index = EXCP_HLT;
465                         cpu_loop_exit(cpu);
466                     }
467 #if defined(TARGET_I386)
468                     else if (interrupt_request & CPU_INTERRUPT_INIT) {
469                         replay_interrupt();
470                         cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0);
471                         do_cpu_init(x86_cpu);
472                         cpu->exception_index = EXCP_HALTED;
473                         cpu_loop_exit(cpu);
474                     }
475 #else
476                     else if (interrupt_request & CPU_INTERRUPT_RESET) {
477                         replay_interrupt();
478                         cpu_reset(cpu);
479                         cpu_loop_exit(cpu);
480                     }
481 #endif
482                     /* The target hook has 3 exit conditions:
483                        False when the interrupt isn't processed,
484                        True when it is, and we should restart on a new TB,
485                        and via longjmp via cpu_loop_exit.  */
486                     else {
487                         replay_interrupt();
488                         if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
489                             next_tb = 0;
490                         }
491                     }
492                     /* Don't use the cached interrupt_request value,
493                        do_interrupt may have updated the EXITTB flag. */
494                     if (cpu->interrupt_request & CPU_INTERRUPT_EXITTB) {
495                         cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
496                         /* ensure that no TB jump will be modified as
497                            the program flow was changed */
498                         next_tb = 0;
499                     }
500                 }
501                 if (unlikely(cpu->exit_request
502                              || replay_has_interrupt())) {
503                     cpu->exit_request = 0;
504                     cpu->exception_index = EXCP_INTERRUPT;
505                     cpu_loop_exit(cpu);
506                 }
507                 tb_lock();
508                 tb = tb_find_fast(cpu);
509                 /* Note: we do it here to avoid a gcc bug on Mac OS X when
510                    doing it in tb_find_slow */
511                 if (tcg_ctx.tb_ctx.tb_invalidated_flag) {
512                     /* as some TB could have been invalidated because
513                        of memory exceptions while generating the code, we
514                        must recompute the hash index here */
515                     next_tb = 0;
516                     tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
517                 }
518                 /* see if we can patch the calling TB. When the TB
519                    spans two pages, we cannot safely do a direct
520                    jump. */
521                 if (next_tb != 0 && tb->page_addr[1] == -1
522                     && !qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) {
523                     tb_add_jump((TranslationBlock *)(next_tb & ~TB_EXIT_MASK),
524                                 next_tb & TB_EXIT_MASK, tb);
525                 }
526                 tb_unlock();
527                 if (likely(!cpu->exit_request)) {
528                     trace_exec_tb(tb, tb->pc);
529                     /* execute the generated code */
530                     cpu->current_tb = tb;
531                     next_tb = cpu_tb_exec(cpu, tb);
532                     cpu->current_tb = NULL;
533                     switch (next_tb & TB_EXIT_MASK) {
534                     case TB_EXIT_REQUESTED:
535                         /* Something asked us to stop executing
536                          * chained TBs; just continue round the main
537                          * loop. Whatever requested the exit will also
538                          * have set something else (eg exit_request or
539                          * interrupt_request) which we will handle
540                          * next time around the loop.  But we need to
541                          * ensure the tcg_exit_req read in generated code
542                          * comes before the next read of cpu->exit_request
543                          * or cpu->interrupt_request.
544                          */
545                         smp_rmb();
546                         next_tb = 0;
547                         break;
548                     case TB_EXIT_ICOUNT_EXPIRED:
549                     {
550                         /* Instruction counter expired.  */
551                         int insns_left = cpu->icount_decr.u32;
552                         if (cpu->icount_extra && insns_left >= 0) {
553                             /* Refill decrementer and continue execution.  */
554                             cpu->icount_extra += insns_left;
555                             insns_left = MIN(0xffff, cpu->icount_extra);
556                             cpu->icount_extra -= insns_left;
557                             cpu->icount_decr.u16.low = insns_left;
558                         } else {
559                             if (insns_left > 0) {
560                                 /* Execute remaining instructions.  */
561                                 tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK);
562                                 cpu_exec_nocache(cpu, insns_left, tb, false);
563                                 align_clocks(&sc, cpu);
564                             }
565                             cpu->exception_index = EXCP_INTERRUPT;
566                             next_tb = 0;
567                             cpu_loop_exit(cpu);
568                         }
569                         break;
570                     }
571                     default:
572                         break;
573                     }
574                 }
575 #ifdef CONFIG_HAX
576                 if (hax_enabled() && hax_stop_emulation(cpu))
577                     cpu_loop_exit(cpu);
578 #endif
579                 /* Try to align the host and virtual clocks
580                    if the guest is in advance */
581                 align_clocks(&sc, cpu);
582                 /* reset soft MMU for next block (it can currently
583                    only be set by a memory fault) */
584             } /* for(;;) */
585         } else {
586 #if defined(__clang__) || !QEMU_GNUC_PREREQ(4, 6)
587             /* Some compilers wrongly smash all local variables after
588              * siglongjmp. There were bug reports for gcc 4.5.0 and clang.
589              * Reload essential local variables here for those compilers.
590              * Newer versions of gcc would complain about this code (-Wclobbered). */
591             cpu = current_cpu;
592             cc = CPU_GET_CLASS(cpu);
593 #ifdef TARGET_I386
594             x86_cpu = X86_CPU(cpu);
595             env = &x86_cpu->env;
596 #endif
597 #else /* buggy compiler */
598             /* Assert that the compiler does not smash local variables. */
599             g_assert(cpu == current_cpu);
600             g_assert(cc == CPU_GET_CLASS(cpu));
601 #ifdef TARGET_I386
602             g_assert(x86_cpu == X86_CPU(cpu));
603             g_assert(env == &x86_cpu->env);
604 #endif
605 #endif /* buggy compiler */
606             cpu->can_do_io = 1;
607             tb_lock_reset();
608         }
609     } /* for(;;) */
610
611     cc->cpu_exec_exit(cpu);
612     rcu_read_unlock();
613
614     /* fail safe : never use current_cpu outside cpu_exec() */
615     current_cpu = NULL;
616
617     /* Does not need atomic_mb_set because a spurious wakeup is okay.  */
618     atomic_set(&tcg_current_cpu, NULL);
619     return ret;
620 }