target/ppc: Split out ppc_env_mmu_index
[qemu/kevin.git] / system / cpu-timers.c
blob0b31c9a1b6a8c784279eeeb091f04764a22ce494
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/cutils.h"
27 #include "migration/vmstate.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "sysemu/cpus.h"
31 #include "qemu/main-loop.h"
32 #include "qemu/option.h"
33 #include "qemu/seqlock.h"
34 #include "sysemu/replay.h"
35 #include "sysemu/runstate.h"
36 #include "hw/core/cpu.h"
37 #include "sysemu/cpu-timers.h"
38 #include "sysemu/cpu-throttle.h"
39 #include "sysemu/cpu-timers-internal.h"
41 /* clock and ticks */
43 static int64_t cpu_get_ticks_locked(void)
45 int64_t ticks = timers_state.cpu_ticks_offset;
46 if (timers_state.cpu_ticks_enabled) {
47 ticks += cpu_get_host_ticks();
50 if (timers_state.cpu_ticks_prev > ticks) {
51 /* Non increasing ticks may happen if the host uses software suspend. */
52 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
53 ticks = timers_state.cpu_ticks_prev;
56 timers_state.cpu_ticks_prev = ticks;
57 return ticks;
61 * return the time elapsed in VM between vm_start and vm_stop.
62 * cpu_get_ticks() uses units of the host CPU cycle counter.
64 int64_t cpu_get_ticks(void)
66 int64_t ticks;
68 qemu_spin_lock(&timers_state.vm_clock_lock);
69 ticks = cpu_get_ticks_locked();
70 qemu_spin_unlock(&timers_state.vm_clock_lock);
71 return ticks;
74 int64_t cpu_get_clock_locked(void)
76 int64_t time;
78 time = timers_state.cpu_clock_offset;
79 if (timers_state.cpu_ticks_enabled) {
80 time += get_clock();
83 return time;
87 * Return the monotonic time elapsed in VM, i.e.,
88 * the time between vm_start and vm_stop
90 int64_t cpu_get_clock(void)
92 int64_t ti;
93 unsigned start;
95 do {
96 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
97 ti = cpu_get_clock_locked();
98 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
100 return ti;
104 * enable cpu_get_ticks()
105 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
107 void cpu_enable_ticks(void)
109 seqlock_write_lock(&timers_state.vm_clock_seqlock,
110 &timers_state.vm_clock_lock);
111 if (!timers_state.cpu_ticks_enabled) {
112 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
113 timers_state.cpu_clock_offset -= get_clock();
114 timers_state.cpu_ticks_enabled = 1;
116 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
117 &timers_state.vm_clock_lock);
121 * disable cpu_get_ticks() : the clock is stopped. You must not call
122 * cpu_get_ticks() after that.
123 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
125 void cpu_disable_ticks(void)
127 seqlock_write_lock(&timers_state.vm_clock_seqlock,
128 &timers_state.vm_clock_lock);
129 if (timers_state.cpu_ticks_enabled) {
130 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
131 timers_state.cpu_clock_offset = cpu_get_clock_locked();
132 timers_state.cpu_ticks_enabled = 0;
134 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
135 &timers_state.vm_clock_lock);
138 static bool icount_state_needed(void *opaque)
140 return icount_enabled();
143 static bool warp_timer_state_needed(void *opaque)
145 TimersState *s = opaque;
146 return s->icount_warp_timer != NULL;
149 static bool adjust_timers_state_needed(void *opaque)
151 TimersState *s = opaque;
152 return s->icount_rt_timer != NULL;
155 static bool icount_shift_state_needed(void *opaque)
157 return icount_enabled() == ICOUNT_ADAPTATIVE;
161 * Subsection for warp timer migration is optional, because may not be created
163 static const VMStateDescription icount_vmstate_warp_timer = {
164 .name = "timer/icount/warp_timer",
165 .version_id = 1,
166 .minimum_version_id = 1,
167 .needed = warp_timer_state_needed,
168 .fields = (const VMStateField[]) {
169 VMSTATE_INT64(vm_clock_warp_start, TimersState),
170 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
171 VMSTATE_END_OF_LIST()
175 static const VMStateDescription icount_vmstate_adjust_timers = {
176 .name = "timer/icount/timers",
177 .version_id = 1,
178 .minimum_version_id = 1,
179 .needed = adjust_timers_state_needed,
180 .fields = (const VMStateField[]) {
181 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
182 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
183 VMSTATE_END_OF_LIST()
187 static const VMStateDescription icount_vmstate_shift = {
188 .name = "timer/icount/shift",
189 .version_id = 2,
190 .minimum_version_id = 2,
191 .needed = icount_shift_state_needed,
192 .fields = (const VMStateField[]) {
193 VMSTATE_INT16(icount_time_shift, TimersState),
194 VMSTATE_INT64(last_delta, TimersState),
195 VMSTATE_END_OF_LIST()
200 * This is a subsection for icount migration.
202 static const VMStateDescription icount_vmstate_timers = {
203 .name = "timer/icount",
204 .version_id = 1,
205 .minimum_version_id = 1,
206 .needed = icount_state_needed,
207 .fields = (const VMStateField[]) {
208 VMSTATE_INT64(qemu_icount_bias, TimersState),
209 VMSTATE_INT64(qemu_icount, TimersState),
210 VMSTATE_END_OF_LIST()
212 .subsections = (const VMStateDescription * const []) {
213 &icount_vmstate_warp_timer,
214 &icount_vmstate_adjust_timers,
215 &icount_vmstate_shift,
216 NULL
220 static const VMStateDescription vmstate_timers = {
221 .name = "timer",
222 .version_id = 2,
223 .minimum_version_id = 1,
224 .fields = (const VMStateField[]) {
225 VMSTATE_INT64(cpu_ticks_offset, TimersState),
226 VMSTATE_UNUSED(8),
227 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
228 VMSTATE_END_OF_LIST()
230 .subsections = (const VMStateDescription * const []) {
231 &icount_vmstate_timers,
232 NULL
236 static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
240 void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
242 if (!icount_enabled() || type != QEMU_CLOCK_VIRTUAL) {
243 qemu_notify_event();
244 return;
247 if (qemu_in_vcpu_thread()) {
249 * A CPU is currently running; kick it back out to the
250 * tcg_cpu_exec() loop so it will recalculate its
251 * icount deadline immediately.
253 qemu_cpu_kick(current_cpu);
254 } else if (first_cpu) {
256 * qemu_cpu_kick is not enough to kick a halted CPU out of
257 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
258 * causes cpu_thread_is_idle to return false. This way,
259 * handle_icount_deadline can run.
260 * If we have no CPUs at all for some reason, we don't
261 * need to do anything.
263 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
267 TimersState timers_state;
269 /* initialize timers state and the cpu throttle for convenience */
270 void cpu_timers_init(void)
272 seqlock_init(&timers_state.vm_clock_seqlock);
273 qemu_spin_init(&timers_state.vm_clock_lock);
274 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
276 cpu_throttle_init();