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