virtio: introduce virtio_queue_reset()
[qemu.git] / accel / tcg / tcg-accel-ops-icount.c
blob84cc7421be88fa42132264da7329d4ee9882f7e1
1 /*
2 * QEMU TCG Single Threaded vCPUs implementation using instruction counting
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2014 Red Hat Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "sysemu/replay.h"
28 #include "sysemu/cpu-timers.h"
29 #include "qemu/main-loop.h"
30 #include "qemu/guest-random.h"
31 #include "exec/exec-all.h"
33 #include "tcg-accel-ops.h"
34 #include "tcg-accel-ops-icount.h"
35 #include "tcg-accel-ops-rr.h"
37 static int64_t icount_get_limit(void)
39 int64_t deadline;
41 if (replay_mode != REPLAY_MODE_PLAY) {
43 * Include all the timers, because they may need an attention.
44 * Too long CPU execution may create unnecessary delay in UI.
46 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
47 QEMU_TIMER_ATTR_ALL);
48 /* Check realtime timers, because they help with input processing */
49 deadline = qemu_soonest_timeout(deadline,
50 qemu_clock_deadline_ns_all(QEMU_CLOCK_REALTIME,
51 QEMU_TIMER_ATTR_ALL));
54 * Maintain prior (possibly buggy) behaviour where if no deadline
55 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
56 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
57 * nanoseconds.
59 if ((deadline < 0) || (deadline > INT32_MAX)) {
60 deadline = INT32_MAX;
63 return icount_round(deadline);
64 } else {
65 return replay_get_instructions();
69 static void icount_notify_aio_contexts(void)
71 /* Wake up other AioContexts. */
72 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
73 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
76 void icount_handle_deadline(void)
78 assert(qemu_in_vcpu_thread());
79 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
80 QEMU_TIMER_ATTR_ALL);
83 * Instructions, interrupts, and exceptions are processed in cpu-exec.
84 * Don't interrupt cpu thread, when these events are waiting
85 * (i.e., there is no checkpoint)
87 if (deadline == 0) {
88 icount_notify_aio_contexts();
92 void icount_prepare_for_run(CPUState *cpu)
94 int insns_left;
97 * These should always be cleared by icount_process_data after
98 * each vCPU execution. However u16.high can be raised
99 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
101 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
102 g_assert(cpu->icount_extra == 0);
104 cpu->icount_budget = icount_get_limit();
105 insns_left = MIN(0xffff, cpu->icount_budget);
106 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
107 cpu->icount_extra = cpu->icount_budget - insns_left;
109 replay_mutex_lock();
111 if (cpu->icount_budget == 0) {
113 * We're called without the iothread lock, so must take it while
114 * we're calling timer handlers.
116 qemu_mutex_lock_iothread();
117 icount_notify_aio_contexts();
118 qemu_mutex_unlock_iothread();
122 void icount_process_data(CPUState *cpu)
124 /* Account for executed instructions */
125 icount_update(cpu);
127 /* Reset the counters */
128 cpu_neg(cpu)->icount_decr.u16.low = 0;
129 cpu->icount_extra = 0;
130 cpu->icount_budget = 0;
132 replay_account_executed_instructions();
134 replay_mutex_unlock();
137 void icount_handle_interrupt(CPUState *cpu, int mask)
139 int old_mask = cpu->interrupt_request;
141 tcg_handle_interrupt(cpu, mask);
142 if (qemu_cpu_is_self(cpu) &&
143 !cpu->can_do_io
144 && (mask & ~old_mask) != 0) {
145 cpu_abort(cpu, "Raised interrupt while not in I/O function");