blob: f97720a757731ebce56da70e0b315f36b1e57e49 [file] [log] [blame]
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24#include "config-host.h"
25
David 'Digit' Turner9b3a4b02014-01-23 00:52:54 +010026#include "cpu.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +010027#include "monitor/monitor.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010028#include "sysemu/sysemu.h"
David 'Digit' Turnera889d352014-03-20 12:35:32 +010029#include "cpu.h"
David 'Digit' Turner0e051542014-01-23 02:41:42 +010030#include "exec/exec-all.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +010031#include "exec/gdbstub.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010032#include "sysemu/dma.h"
33#include "sysemu/kvm.h"
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +010034#include "exec/exec-all.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +010035#include "exec/hax.h"
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020036
David 'Digit' Turner11822842013-12-17 09:16:47 +010037#include "sysemu/cpus.h"
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020038
David 'Digit' Turner66576782014-03-24 16:57:57 +010039static CPUState *cur_cpu;
40static CPUState *next_cpu;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020041
42/***********************************************************/
43void hw_error(const char *fmt, ...)
44{
45 va_list ap;
David 'Digit' Turner66576782014-03-24 16:57:57 +010046 CPUState *cpu;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020047
48 va_start(ap, fmt);
49 fprintf(stderr, "qemu: hardware error: ");
50 vfprintf(stderr, fmt, ap);
51 fprintf(stderr, "\n");
David 'Digit' Turner66576782014-03-24 16:57:57 +010052 CPU_FOREACH(cpu) {
53 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020054#ifdef TARGET_I386
David 'Digit' Turner66576782014-03-24 16:57:57 +010055 cpu_dump_state(cpu->env_ptr, stderr, fprintf, X86_DUMP_FPU);
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020056#else
David 'Digit' Turner66576782014-03-24 16:57:57 +010057 cpu_dump_state(cpu->env_ptr, stderr, fprintf, 0);
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020058#endif
59 }
60 va_end(ap);
61 abort();
62}
63
64static void do_vm_stop(int reason)
65{
66 if (vm_running) {
67 cpu_disable_ticks();
68 vm_running = 0;
69 pause_all_vcpus();
70 vm_state_notify(0, reason);
71 }
72}
73
David 'Digit' Turner66576782014-03-24 16:57:57 +010074static int cpu_can_run(CPUArchState *env)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020075{
David 'Digit' Turner66576782014-03-24 16:57:57 +010076 CPUState *cpu = ENV_GET_CPU(env);
77 if (cpu->stop)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020078 return 0;
David 'Digit' Turner66576782014-03-24 16:57:57 +010079 if (cpu->stopped)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020080 return 0;
81 return 1;
82}
83
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020084int tcg_has_work(void)
85{
David 'Digit' Turner66576782014-03-24 16:57:57 +010086 CPUState *cpu;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020087
David 'Digit' Turner66576782014-03-24 16:57:57 +010088 CPU_FOREACH(cpu) {
89 if (cpu->stop)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020090 return 1;
David 'Digit' Turner66576782014-03-24 16:57:57 +010091 if (cpu->stopped)
David 'Digit' Turner4ab12252014-03-24 11:29:53 +010092 return 0;
David 'Digit' Turner66576782014-03-24 16:57:57 +010093 if (!cpu->halted)
David 'Digit' Turner4ab12252014-03-24 11:29:53 +010094 return 1;
David 'Digit' Turner66576782014-03-24 16:57:57 +010095 if (cpu_has_work(cpu))
David 'Digit' Turner4ab12252014-03-24 11:29:53 +010096 return 1;
97 return 0;
98 }
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +020099 return 0;
100}
101
David 'Digit' Turnerbf7a22f2014-03-25 16:36:03 +0100102void qemu_init_vcpu(CPUState *cpu)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200103{
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200104 if (kvm_enabled())
David 'Digit' Turnere36a6832014-03-25 18:02:39 +0100105 kvm_init_vcpu(cpu);
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800106#ifdef CONFIG_HAX
107 if (hax_enabled())
yucheng yuaf1a7202014-04-29 16:50:27 -0700108 hax_init_vcpu(cpu);
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800109#endif
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200110 return;
111}
112
David 'Digit' Turnerbf7a22f2014-03-25 16:36:03 +0100113bool qemu_cpu_is_self(CPUState *cpu)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200114{
David 'Digit' Turnerbf7a22f2014-03-25 16:36:03 +0100115 return true;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200116}
117
118void resume_all_vcpus(void)
119{
120}
121
122void pause_all_vcpus(void)
123{
124}
125
David 'Digit' Turnerbf7a22f2014-03-25 16:36:03 +0100126void qemu_cpu_kick(CPUState *cpu)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200127{
128 return;
129}
130
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +0100131// In main-loop.c
132#ifdef _WIN32
133extern HANDLE qemu_event_handle;
134#endif
135
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200136void qemu_notify_event(void)
137{
David 'Digit' Turner66576782014-03-24 16:57:57 +0100138 CPUState *cpu = current_cpu;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200139
David 'Digit' Turner66576782014-03-24 16:57:57 +0100140 if (cpu) {
David 'Digit' Turnerbf7a22f2014-03-25 16:36:03 +0100141 cpu_exit(cpu);
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800142 /*
143 * This is mainly for the Windows host, where the timer may be in
144 * a different thread with vcpu. Thus the timer function needs to
145 * notify the vcpu thread of more than simply cpu_exit. If env is
146 * not NULL, it means that the vcpu is in execute state, we need
147 * only to set the flags. If the guest is in execute state, the
148 * HAX kernel module will exit to qemu. If env is NULL, vcpu is
149 * in main_loop_wait, and we need a event to notify it.
150 */
151#ifdef CONFIG_HAX
152 if (hax_enabled())
David 'Digit' Turner7f38c7f2014-03-26 10:20:11 +0100153 hax_raise_event(cpu);
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800154 } else {
155#ifdef _WIN32
156 if(hax_enabled())
157 SetEvent(qemu_event_handle);
158#endif
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200159 }
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800160#else
161 }
162#endif
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200163}
164
165void qemu_mutex_lock_iothread(void)
166{
167}
168
169void qemu_mutex_unlock_iothread(void)
170{
171}
172
173void vm_stop(int reason)
174{
175 do_vm_stop(reason);
176}
177
David 'Digit' Turnere2678e12014-01-16 15:56:43 +0100178static int qemu_cpu_exec(CPUOldState *env)
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200179{
180 int ret;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200181
182#ifdef CONFIG_PROFILER
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +0100183 int64_t ti = profile_getclock();
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200184#endif
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +0100185#ifndef CONFIG_ANDROID
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200186 if (use_icount) {
187 int64_t count;
188 int decr;
189 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
190 env->icount_decr.u16.low = 0;
191 env->icount_extra = 0;
192 count = qemu_next_icount_deadline();
193 count = (count + (1 << icount_time_shift) - 1)
194 >> icount_time_shift;
195 qemu_icount += count;
196 decr = (count > 0xffff) ? 0xffff : count;
197 count -= decr;
198 env->icount_decr.u16.low = decr;
199 env->icount_extra = count;
200 }
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +0100201#endif
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200202 ret = cpu_exec(env);
203#ifdef CONFIG_PROFILER
204 qemu_time += profile_getclock() - ti;
205#endif
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +0100206#ifndef CONFIG_ANDROID
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200207 if (use_icount) {
208 /* Fold pending instructions back into the
209 instruction counter, and clear the interrupt flag. */
210 qemu_icount -= (env->icount_decr.u16.low
211 + env->icount_extra);
212 env->icount_decr.u32 = 0;
213 env->icount_extra = 0;
214 }
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +0100215#endif
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200216 return ret;
217}
218
219void tcg_cpu_exec(void)
220{
221 int ret = 0;
222
223 if (next_cpu == NULL)
David 'Digit' Turner4ab12252014-03-24 11:29:53 +0100224 next_cpu = QTAILQ_FIRST(&cpus);
David 'Digit' Turner66576782014-03-24 16:57:57 +0100225 for (; next_cpu != NULL; next_cpu = QTAILQ_NEXT(next_cpu, node)) {\
226 cur_cpu = next_cpu;
227 CPUOldState *env = cur_cpu->env_ptr;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200228
229 if (!vm_running)
230 break;
231 if (qemu_timer_alarm_pending()) {
232 break;
233 }
234 if (cpu_can_run(env))
235 ret = qemu_cpu_exec(env);
236 if (ret == EXCP_DEBUG) {
David 'Digit' Turner0b1a8452014-03-25 17:37:52 +0100237 gdb_set_stop_cpu(cur_cpu);
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200238 debug_requested = 1;
239 break;
240 }
241 }
242}
243
David 'Digit' Turnerf0183552014-02-16 14:20:45 +0100244/***********************************************************/
245/* guest cycle counter */
246
247typedef struct TimersState {
248 int64_t cpu_ticks_prev;
249 int64_t cpu_ticks_offset;
250 int64_t cpu_clock_offset;
251 int32_t cpu_ticks_enabled;
252 int64_t dummy;
253} TimersState;
254
255static void timer_save(QEMUFile *f, void *opaque)
256{
257 TimersState *s = opaque;
258
259 if (s->cpu_ticks_enabled) {
260 hw_error("cannot save state if virtual timers are running");
261 }
262 qemu_put_be64(f, s->cpu_ticks_prev);
263 qemu_put_be64(f, s->cpu_ticks_offset);
264 qemu_put_be64(f, s->cpu_clock_offset);
265 }
266
267static int timer_load(QEMUFile *f, void *opaque, int version_id)
268{
269 TimersState *s = opaque;
270
271 if (version_id != 1 && version_id != 2)
272 return -EINVAL;
273 if (s->cpu_ticks_enabled) {
274 return -EINVAL;
275 }
276 s->cpu_ticks_prev = qemu_get_sbe64(f);
277 s->cpu_ticks_offset = qemu_get_sbe64(f);
278 if (version_id == 2) {
279 s->cpu_clock_offset = qemu_get_sbe64(f);
280 }
281 return 0;
282}
283
284
285TimersState timers_state;
286
287void qemu_timer_register_savevm(void) {
David 'Digit' Turner5cb5c0b2014-02-17 16:04:03 +0100288 register_savevm(NULL,
289 "timer",
290 0,
291 2,
292 timer_save,
293 timer_load,
294 &timers_state);
David 'Digit' Turnerf0183552014-02-16 14:20:45 +0100295}
296
David 'Digit' Turner9b3a4b02014-01-23 00:52:54 +0100297/* Return the virtual CPU time, based on the instruction counter. */
298int64_t cpu_get_icount(void)
299{
300 int64_t icount;
301 CPUOldState *env = cpu_single_env;;
302
303 icount = qemu_icount;
304 if (env) {
305 if (!can_do_io(env)) {
306 fprintf(stderr, "Bad clock read\n");
307 }
308 icount -= (env->icount_decr.u16.low + env->icount_extra);
309 }
310 return qemu_icount_bias + (icount << icount_time_shift);
311}
David 'Digit' Turnerf0183552014-02-16 14:20:45 +0100312
313/* return the host CPU cycle counter and handle stop/restart */
314int64_t cpu_get_ticks(void)
315{
316 if (use_icount) {
317 return cpu_get_icount();
318 }
319 if (!timers_state.cpu_ticks_enabled) {
320 return timers_state.cpu_ticks_offset;
321 } else {
322 int64_t ticks;
323 ticks = cpu_get_real_ticks();
324 if (timers_state.cpu_ticks_prev > ticks) {
325 /* Note: non increasing ticks may happen if the host uses
326 software suspend */
327 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
328 }
329 timers_state.cpu_ticks_prev = ticks;
330 return ticks + timers_state.cpu_ticks_offset;
331 }
332}
333
334/* return the host CPU monotonic timer and handle stop/restart */
335int64_t cpu_get_clock(void)
336{
337 int64_t ti;
338 if (!timers_state.cpu_ticks_enabled) {
339 return timers_state.cpu_clock_offset;
340 } else {
341 ti = get_clock();
342 return ti + timers_state.cpu_clock_offset;
343 }
344}
345
346/* enable cpu_get_ticks() */
347void cpu_enable_ticks(void)
348{
349 if (!timers_state.cpu_ticks_enabled) {
350 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
351 timers_state.cpu_clock_offset -= get_clock();
352 timers_state.cpu_ticks_enabled = 1;
353 }
354}
355
356/* disable cpu_get_ticks() : the clock is stopped. You must not call
357 cpu_get_ticks() after that. */
358void cpu_disable_ticks(void)
359{
360 if (timers_state.cpu_ticks_enabled) {
361 timers_state.cpu_ticks_offset = cpu_get_ticks();
362 timers_state.cpu_clock_offset = cpu_get_clock();
363 timers_state.cpu_ticks_enabled = 0;
364 }
365}
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +0100366
367void qemu_clock_warp(QEMUClockType clock) {
368}