tests/qtest/cmsdk-apb-watchdog-test: Test clock changes
[qemu/ar7.git] / accel / tcg / tcg-cpus-icount.c
blob9f45432275d7c9a7e2c346dbd98317e277ca431c
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 "qemu-common.h"
28 #include "sysemu/tcg.h"
29 #include "sysemu/replay.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/guest-random.h"
32 #include "exec/exec-all.h"
33 #include "hw/boards.h"
35 #include "tcg-cpus.h"
36 #include "tcg-cpus-icount.h"
37 #include "tcg-cpus-rr.h"
39 static int64_t icount_get_limit(void)
41 int64_t deadline;
43 if (replay_mode != REPLAY_MODE_PLAY) {
45 * Include all the timers, because they may need an attention.
46 * Too long CPU execution may create unnecessary delay in UI.
48 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
49 QEMU_TIMER_ATTR_ALL);
50 /* Check realtime timers, because they help with input processing */
51 deadline = qemu_soonest_timeout(deadline,
52 qemu_clock_deadline_ns_all(QEMU_CLOCK_REALTIME,
53 QEMU_TIMER_ATTR_ALL));
56 * Maintain prior (possibly buggy) behaviour where if no deadline
57 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
58 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
59 * nanoseconds.
61 if ((deadline < 0) || (deadline > INT32_MAX)) {
62 deadline = INT32_MAX;
65 return icount_round(deadline);
66 } else {
67 return replay_get_instructions();
71 static void icount_notify_aio_contexts(void)
73 /* Wake up other AioContexts. */
74 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
75 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
78 void icount_handle_deadline(void)
80 assert(qemu_in_vcpu_thread());
81 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
82 QEMU_TIMER_ATTR_ALL);
84 if (deadline == 0) {
85 icount_notify_aio_contexts();
89 void icount_prepare_for_run(CPUState *cpu)
91 int insns_left;
94 * These should always be cleared by icount_process_data after
95 * each vCPU execution. However u16.high can be raised
96 * asynchronously by cpu_exit/cpu_interrupt/tcg_cpus_handle_interrupt
98 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
99 g_assert(cpu->icount_extra == 0);
101 cpu->icount_budget = icount_get_limit();
102 insns_left = MIN(0xffff, cpu->icount_budget);
103 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
104 cpu->icount_extra = cpu->icount_budget - insns_left;
106 replay_mutex_lock();
108 if (cpu->icount_budget == 0 && replay_has_checkpoint()) {
109 icount_notify_aio_contexts();
113 void icount_process_data(CPUState *cpu)
115 /* Account for executed instructions */
116 icount_update(cpu);
118 /* Reset the counters */
119 cpu_neg(cpu)->icount_decr.u16.low = 0;
120 cpu->icount_extra = 0;
121 cpu->icount_budget = 0;
123 replay_account_executed_instructions();
125 replay_mutex_unlock();
128 static void icount_handle_interrupt(CPUState *cpu, int mask)
130 int old_mask = cpu->interrupt_request;
132 tcg_cpus_handle_interrupt(cpu, mask);
133 if (qemu_cpu_is_self(cpu) &&
134 !cpu->can_do_io
135 && (mask & ~old_mask) != 0) {
136 cpu_abort(cpu, "Raised interrupt while not in I/O function");
140 const CpusAccel tcg_cpus_icount = {
141 .create_vcpu_thread = rr_start_vcpu_thread,
142 .kick_vcpu_thread = rr_kick_vcpu_thread,
144 .handle_interrupt = icount_handle_interrupt,
145 .get_virtual_clock = icount_get,
146 .get_elapsed_ticks = icount_get,