Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | /* Needed early for CONFIG_BSD etc. */ |
| 26 | #include "config-host.h" |
| 27 | |
| 28 | #include "monitor.h" |
| 29 | #include "sysemu.h" |
| 30 | #include "gdbstub.h" |
| 31 | #include "dma.h" |
| 32 | #include "kvm.h" |
| 33 | |
Paolo Bonzini | 96284e8 | 2011-03-12 17:43:53 +0100 | [diff] [blame] | 34 | #include "qemu-thread.h" |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 35 | #include "cpus.h" |
Jan Kiszka | 0ff0fc1 | 2011-06-23 10:15:55 +0200 | [diff] [blame] | 36 | |
| 37 | #ifndef _WIN32 |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 38 | #include "compatfd.h" |
Jan Kiszka | 0ff0fc1 | 2011-06-23 10:15:55 +0200 | [diff] [blame] | 39 | #endif |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 40 | |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 41 | #ifdef SIGRTMIN |
| 42 | #define SIG_IPI (SIGRTMIN+4) |
| 43 | #else |
| 44 | #define SIG_IPI SIGUSR1 |
| 45 | #endif |
| 46 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 47 | #ifdef CONFIG_LINUX |
| 48 | |
| 49 | #include <sys/prctl.h> |
| 50 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 51 | #ifndef PR_MCE_KILL |
| 52 | #define PR_MCE_KILL 33 |
| 53 | #endif |
| 54 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 55 | #ifndef PR_MCE_KILL_SET |
| 56 | #define PR_MCE_KILL_SET 1 |
| 57 | #endif |
| 58 | |
| 59 | #ifndef PR_MCE_KILL_EARLY |
| 60 | #define PR_MCE_KILL_EARLY 1 |
| 61 | #endif |
| 62 | |
| 63 | #endif /* CONFIG_LINUX */ |
| 64 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 65 | static CPUState *next_cpu; |
| 66 | |
| 67 | /***********************************************************/ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 68 | /* guest cycle counter */ |
| 69 | |
| 70 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
| 71 | static int icount_time_shift; |
| 72 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
| 73 | #define MAX_ICOUNT_SHIFT 10 |
| 74 | /* Compensate for varying guest execution speed. */ |
| 75 | static int64_t qemu_icount_bias; |
| 76 | static QEMUTimer *icount_rt_timer; |
| 77 | static QEMUTimer *icount_vm_timer; |
| 78 | static QEMUTimer *icount_warp_timer; |
| 79 | static int64_t vm_clock_warp_start; |
| 80 | static int64_t qemu_icount; |
| 81 | |
| 82 | typedef struct TimersState { |
| 83 | int64_t cpu_ticks_prev; |
| 84 | int64_t cpu_ticks_offset; |
| 85 | int64_t cpu_clock_offset; |
| 86 | int32_t cpu_ticks_enabled; |
| 87 | int64_t dummy; |
| 88 | } TimersState; |
| 89 | |
| 90 | TimersState timers_state; |
| 91 | |
| 92 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 93 | int64_t cpu_get_icount(void) |
| 94 | { |
| 95 | int64_t icount; |
| 96 | CPUState *env = cpu_single_env;; |
| 97 | |
| 98 | icount = qemu_icount; |
| 99 | if (env) { |
| 100 | if (!can_do_io(env)) { |
| 101 | fprintf(stderr, "Bad clock read\n"); |
| 102 | } |
| 103 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 104 | } |
| 105 | return qemu_icount_bias + (icount << icount_time_shift); |
| 106 | } |
| 107 | |
| 108 | /* return the host CPU cycle counter and handle stop/restart */ |
| 109 | int64_t cpu_get_ticks(void) |
| 110 | { |
| 111 | if (use_icount) { |
| 112 | return cpu_get_icount(); |
| 113 | } |
| 114 | if (!timers_state.cpu_ticks_enabled) { |
| 115 | return timers_state.cpu_ticks_offset; |
| 116 | } else { |
| 117 | int64_t ticks; |
| 118 | ticks = cpu_get_real_ticks(); |
| 119 | if (timers_state.cpu_ticks_prev > ticks) { |
| 120 | /* Note: non increasing ticks may happen if the host uses |
| 121 | software suspend */ |
| 122 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
| 123 | } |
| 124 | timers_state.cpu_ticks_prev = ticks; |
| 125 | return ticks + timers_state.cpu_ticks_offset; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 130 | int64_t cpu_get_clock(void) |
| 131 | { |
| 132 | int64_t ti; |
| 133 | if (!timers_state.cpu_ticks_enabled) { |
| 134 | return timers_state.cpu_clock_offset; |
| 135 | } else { |
| 136 | ti = get_clock(); |
| 137 | return ti + timers_state.cpu_clock_offset; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* enable cpu_get_ticks() */ |
| 142 | void cpu_enable_ticks(void) |
| 143 | { |
| 144 | if (!timers_state.cpu_ticks_enabled) { |
| 145 | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
| 146 | timers_state.cpu_clock_offset -= get_clock(); |
| 147 | timers_state.cpu_ticks_enabled = 1; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 152 | cpu_get_ticks() after that. */ |
| 153 | void cpu_disable_ticks(void) |
| 154 | { |
| 155 | if (timers_state.cpu_ticks_enabled) { |
| 156 | timers_state.cpu_ticks_offset = cpu_get_ticks(); |
| 157 | timers_state.cpu_clock_offset = cpu_get_clock(); |
| 158 | timers_state.cpu_ticks_enabled = 0; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* Correlation between real and virtual time is always going to be |
| 163 | fairly approximate, so ignore small variation. |
| 164 | When the guest is idle real and virtual time will be aligned in |
| 165 | the IO wait loop. */ |
| 166 | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
| 167 | |
| 168 | static void icount_adjust(void) |
| 169 | { |
| 170 | int64_t cur_time; |
| 171 | int64_t cur_icount; |
| 172 | int64_t delta; |
| 173 | static int64_t last_delta; |
| 174 | /* If the VM is not running, then do nothing. */ |
| 175 | if (!runstate_is_running()) { |
| 176 | return; |
| 177 | } |
| 178 | cur_time = cpu_get_clock(); |
| 179 | cur_icount = qemu_get_clock_ns(vm_clock); |
| 180 | delta = cur_icount - cur_time; |
| 181 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 182 | if (delta > 0 |
| 183 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 184 | && icount_time_shift > 0) { |
| 185 | /* The guest is getting too far ahead. Slow time down. */ |
| 186 | icount_time_shift--; |
| 187 | } |
| 188 | if (delta < 0 |
| 189 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 190 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 191 | /* The guest is getting too far behind. Speed time up. */ |
| 192 | icount_time_shift++; |
| 193 | } |
| 194 | last_delta = delta; |
| 195 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 196 | } |
| 197 | |
| 198 | static void icount_adjust_rt(void *opaque) |
| 199 | { |
| 200 | qemu_mod_timer(icount_rt_timer, |
| 201 | qemu_get_clock_ms(rt_clock) + 1000); |
| 202 | icount_adjust(); |
| 203 | } |
| 204 | |
| 205 | static void icount_adjust_vm(void *opaque) |
| 206 | { |
| 207 | qemu_mod_timer(icount_vm_timer, |
| 208 | qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10); |
| 209 | icount_adjust(); |
| 210 | } |
| 211 | |
| 212 | static int64_t qemu_icount_round(int64_t count) |
| 213 | { |
| 214 | return (count + (1 << icount_time_shift) - 1) >> icount_time_shift; |
| 215 | } |
| 216 | |
| 217 | static void icount_warp_rt(void *opaque) |
| 218 | { |
| 219 | if (vm_clock_warp_start == -1) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | if (runstate_is_running()) { |
| 224 | int64_t clock = qemu_get_clock_ns(rt_clock); |
| 225 | int64_t warp_delta = clock - vm_clock_warp_start; |
| 226 | if (use_icount == 1) { |
| 227 | qemu_icount_bias += warp_delta; |
| 228 | } else { |
| 229 | /* |
| 230 | * In adaptive mode, do not let the vm_clock run too |
| 231 | * far ahead of real time. |
| 232 | */ |
| 233 | int64_t cur_time = cpu_get_clock(); |
| 234 | int64_t cur_icount = qemu_get_clock_ns(vm_clock); |
| 235 | int64_t delta = cur_time - cur_icount; |
| 236 | qemu_icount_bias += MIN(warp_delta, delta); |
| 237 | } |
| 238 | if (qemu_clock_expired(vm_clock)) { |
| 239 | qemu_notify_event(); |
| 240 | } |
| 241 | } |
| 242 | vm_clock_warp_start = -1; |
| 243 | } |
| 244 | |
| 245 | void qemu_clock_warp(QEMUClock *clock) |
| 246 | { |
| 247 | int64_t deadline; |
| 248 | |
| 249 | /* |
| 250 | * There are too many global variables to make the "warp" behavior |
| 251 | * applicable to other clocks. But a clock argument removes the |
| 252 | * need for if statements all over the place. |
| 253 | */ |
| 254 | if (clock != vm_clock || !use_icount) { |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * If the CPUs have been sleeping, advance the vm_clock timer now. This |
| 260 | * ensures that the deadline for the timer is computed correctly below. |
| 261 | * This also makes sure that the insn counter is synchronized before the |
| 262 | * CPU starts running, in case the CPU is woken by an event other than |
| 263 | * the earliest vm_clock timer. |
| 264 | */ |
| 265 | icount_warp_rt(NULL); |
| 266 | if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock)) { |
| 267 | qemu_del_timer(icount_warp_timer); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | vm_clock_warp_start = qemu_get_clock_ns(rt_clock); |
| 272 | deadline = qemu_clock_deadline(vm_clock); |
| 273 | if (deadline > 0) { |
| 274 | /* |
| 275 | * Ensure the vm_clock proceeds even when the virtual CPU goes to |
| 276 | * sleep. Otherwise, the CPU might be waiting for a future timer |
| 277 | * interrupt to wake it up, but the interrupt never comes because |
| 278 | * the vCPU isn't running any insns and thus doesn't advance the |
| 279 | * vm_clock. |
| 280 | * |
| 281 | * An extreme solution for this problem would be to never let VCPUs |
| 282 | * sleep in icount mode if there is a pending vm_clock timer; rather |
| 283 | * time could just advance to the next vm_clock event. Instead, we |
| 284 | * do stop VCPUs and only advance vm_clock after some "real" time, |
| 285 | * (related to the time left until the next event) has passed. This |
| 286 | * rt_clock timer will do this. This avoids that the warps are too |
| 287 | * visible externally---for example, you will not be sending network |
| 288 | * packets continously instead of every 100ms. |
| 289 | */ |
| 290 | qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline); |
| 291 | } else { |
| 292 | qemu_notify_event(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static const VMStateDescription vmstate_timers = { |
| 297 | .name = "timer", |
| 298 | .version_id = 2, |
| 299 | .minimum_version_id = 1, |
| 300 | .minimum_version_id_old = 1, |
| 301 | .fields = (VMStateField[]) { |
| 302 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
| 303 | VMSTATE_INT64(dummy, TimersState), |
| 304 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
| 305 | VMSTATE_END_OF_LIST() |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | void configure_icount(const char *option) |
| 310 | { |
| 311 | vmstate_register(NULL, 0, &vmstate_timers, &timers_state); |
| 312 | if (!option) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | icount_warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL); |
| 317 | if (strcmp(option, "auto") != 0) { |
| 318 | icount_time_shift = strtol(option, NULL, 0); |
| 319 | use_icount = 1; |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | use_icount = 2; |
| 324 | |
| 325 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 326 | It will be corrected fairly quickly anyway. */ |
| 327 | icount_time_shift = 3; |
| 328 | |
| 329 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 330 | The realtime trigger catches emulated time passing too slowly, |
| 331 | the virtual time trigger catches emulated time passing too fast. |
| 332 | Realtime triggers occur even when idle, so use them less frequently |
| 333 | than VM triggers. */ |
| 334 | icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL); |
| 335 | qemu_mod_timer(icount_rt_timer, |
| 336 | qemu_get_clock_ms(rt_clock) + 1000); |
| 337 | icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL); |
| 338 | qemu_mod_timer(icount_vm_timer, |
| 339 | qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10); |
| 340 | } |
| 341 | |
| 342 | /***********************************************************/ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 343 | void hw_error(const char *fmt, ...) |
| 344 | { |
| 345 | va_list ap; |
| 346 | CPUState *env; |
| 347 | |
| 348 | va_start(ap, fmt); |
| 349 | fprintf(stderr, "qemu: hardware error: "); |
| 350 | vfprintf(stderr, fmt, ap); |
| 351 | fprintf(stderr, "\n"); |
| 352 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 353 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
| 354 | #ifdef TARGET_I386 |
| 355 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
| 356 | #else |
| 357 | cpu_dump_state(env, stderr, fprintf, 0); |
| 358 | #endif |
| 359 | } |
| 360 | va_end(ap); |
| 361 | abort(); |
| 362 | } |
| 363 | |
| 364 | void cpu_synchronize_all_states(void) |
| 365 | { |
| 366 | CPUState *cpu; |
| 367 | |
| 368 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { |
| 369 | cpu_synchronize_state(cpu); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void cpu_synchronize_all_post_reset(void) |
| 374 | { |
| 375 | CPUState *cpu; |
| 376 | |
| 377 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { |
| 378 | cpu_synchronize_post_reset(cpu); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void cpu_synchronize_all_post_init(void) |
| 383 | { |
| 384 | CPUState *cpu; |
| 385 | |
| 386 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { |
| 387 | cpu_synchronize_post_init(cpu); |
| 388 | } |
| 389 | } |
| 390 | |
Marcelo Tosatti | 3ae9501 | 2010-05-04 09:45:24 -0300 | [diff] [blame] | 391 | int cpu_is_stopped(CPUState *env) |
| 392 | { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 393 | return !runstate_is_running() || env->stopped; |
Marcelo Tosatti | 3ae9501 | 2010-05-04 09:45:24 -0300 | [diff] [blame] | 394 | } |
| 395 | |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 396 | static void do_vm_stop(RunState state) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 397 | { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 398 | if (runstate_is_running()) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 399 | cpu_disable_ticks(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 400 | pause_all_vcpus(); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 401 | runstate_set(state); |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 402 | vm_state_notify(0, state); |
Michael S. Tsirkin | 55df6f3 | 2010-11-22 19:52:22 +0200 | [diff] [blame] | 403 | qemu_aio_flush(); |
| 404 | bdrv_flush_all(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 405 | monitor_protocol_event(QEVENT_STOP, NULL); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | static int cpu_can_run(CPUState *env) |
| 410 | { |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 411 | if (env->stop) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 412 | return 0; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 413 | } |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 414 | if (env->stopped || !runstate_is_running()) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 415 | return 0; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 416 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 417 | return 1; |
| 418 | } |
| 419 | |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 420 | static bool cpu_thread_is_idle(CPUState *env) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 421 | { |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 422 | if (env->stop || env->queued_work_first) { |
| 423 | return false; |
| 424 | } |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 425 | if (env->stopped || !runstate_is_running()) { |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 426 | return true; |
| 427 | } |
Jan Kiszka | f2c1cc8 | 2011-03-15 12:26:18 +0100 | [diff] [blame] | 428 | if (!env->halted || qemu_cpu_has_work(env) || |
| 429 | (kvm_enabled() && kvm_irqchip_in_kernel())) { |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | return true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 435 | bool all_cpu_threads_idle(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 436 | { |
| 437 | CPUState *env; |
| 438 | |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 439 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 440 | if (!cpu_thread_is_idle(env)) { |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | return true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Jan Kiszka | 1009d2e | 2011-03-15 12:26:13 +0100 | [diff] [blame] | 447 | static void cpu_handle_guest_debug(CPUState *env) |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 448 | { |
| 449 | gdb_set_stop_cpu(env); |
Jan Kiszka | 8cf7171 | 2011-02-07 12:19:16 +0100 | [diff] [blame] | 450 | qemu_system_debug_request(); |
Jan Kiszka | 83f338f | 2011-02-07 12:19:17 +0100 | [diff] [blame] | 451 | env->stopped = 1; |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 454 | static void cpu_signal(int sig) |
| 455 | { |
| 456 | if (cpu_single_env) { |
| 457 | cpu_exit(cpu_single_env); |
| 458 | } |
| 459 | exit_request = 1; |
| 460 | } |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 461 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 462 | #ifdef CONFIG_LINUX |
| 463 | static void sigbus_reraise(void) |
| 464 | { |
| 465 | sigset_t set; |
| 466 | struct sigaction action; |
| 467 | |
| 468 | memset(&action, 0, sizeof(action)); |
| 469 | action.sa_handler = SIG_DFL; |
| 470 | if (!sigaction(SIGBUS, &action, NULL)) { |
| 471 | raise(SIGBUS); |
| 472 | sigemptyset(&set); |
| 473 | sigaddset(&set, SIGBUS); |
| 474 | sigprocmask(SIG_UNBLOCK, &set, NULL); |
| 475 | } |
| 476 | perror("Failed to re-raise SIGBUS!\n"); |
| 477 | abort(); |
| 478 | } |
| 479 | |
| 480 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, |
| 481 | void *ctx) |
| 482 | { |
| 483 | if (kvm_on_sigbus(siginfo->ssi_code, |
| 484 | (void *)(intptr_t)siginfo->ssi_addr)) { |
| 485 | sigbus_reraise(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | static void qemu_init_sigbus(void) |
| 490 | { |
| 491 | struct sigaction action; |
| 492 | |
| 493 | memset(&action, 0, sizeof(action)); |
| 494 | action.sa_flags = SA_SIGINFO; |
| 495 | action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler; |
| 496 | sigaction(SIGBUS, &action, NULL); |
| 497 | |
| 498 | prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); |
| 499 | } |
| 500 | |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 501 | static void qemu_kvm_eat_signals(CPUState *env) |
| 502 | { |
| 503 | struct timespec ts = { 0, 0 }; |
| 504 | siginfo_t siginfo; |
| 505 | sigset_t waitset; |
| 506 | sigset_t chkset; |
| 507 | int r; |
| 508 | |
| 509 | sigemptyset(&waitset); |
| 510 | sigaddset(&waitset, SIG_IPI); |
| 511 | sigaddset(&waitset, SIGBUS); |
| 512 | |
| 513 | do { |
| 514 | r = sigtimedwait(&waitset, &siginfo, &ts); |
| 515 | if (r == -1 && !(errno == EAGAIN || errno == EINTR)) { |
| 516 | perror("sigtimedwait"); |
| 517 | exit(1); |
| 518 | } |
| 519 | |
| 520 | switch (r) { |
| 521 | case SIGBUS: |
| 522 | if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) { |
| 523 | sigbus_reraise(); |
| 524 | } |
| 525 | break; |
| 526 | default: |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | r = sigpending(&chkset); |
| 531 | if (r == -1) { |
| 532 | perror("sigpending"); |
| 533 | exit(1); |
| 534 | } |
| 535 | } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS)); |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 538 | #else /* !CONFIG_LINUX */ |
| 539 | |
| 540 | static void qemu_init_sigbus(void) |
| 541 | { |
| 542 | } |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 543 | |
| 544 | static void qemu_kvm_eat_signals(CPUState *env) |
| 545 | { |
| 546 | } |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 547 | #endif /* !CONFIG_LINUX */ |
| 548 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 549 | #ifndef _WIN32 |
| 550 | static int io_thread_fd = -1; |
| 551 | |
| 552 | static void qemu_event_increment(void) |
| 553 | { |
| 554 | /* Write 8 bytes to be compatible with eventfd. */ |
Blue Swirl | 26a8233 | 2010-05-14 19:32:21 +0000 | [diff] [blame] | 555 | static const uint64_t val = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 556 | ssize_t ret; |
| 557 | |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 558 | if (io_thread_fd == -1) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 559 | return; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 560 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 561 | do { |
| 562 | ret = write(io_thread_fd, &val, sizeof(val)); |
| 563 | } while (ret < 0 && errno == EINTR); |
| 564 | |
| 565 | /* EAGAIN is fine, a read must be pending. */ |
| 566 | if (ret < 0 && errno != EAGAIN) { |
Alexandre Raymond | 77bec68 | 2011-06-13 22:16:36 -0400 | [diff] [blame] | 567 | fprintf(stderr, "qemu_event_increment: write() failed: %s\n", |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 568 | strerror(errno)); |
| 569 | exit (1); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | static void qemu_event_read(void *opaque) |
| 574 | { |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 575 | int fd = (intptr_t)opaque; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 576 | ssize_t len; |
| 577 | char buffer[512]; |
| 578 | |
| 579 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
| 580 | do { |
| 581 | len = read(fd, buffer, sizeof(buffer)); |
| 582 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
| 583 | } |
| 584 | |
| 585 | static int qemu_event_init(void) |
| 586 | { |
| 587 | int err; |
| 588 | int fds[2]; |
| 589 | |
| 590 | err = qemu_eventfd(fds); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 591 | if (err == -1) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 592 | return -errno; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 593 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 594 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 595 | if (err < 0) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 596 | goto fail; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 597 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 598 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 599 | if (err < 0) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 600 | goto fail; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 601 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 602 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 603 | (void *)(intptr_t)fds[0]); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 604 | |
| 605 | io_thread_fd = fds[1]; |
| 606 | return 0; |
| 607 | |
| 608 | fail: |
| 609 | close(fds[0]); |
| 610 | close(fds[1]); |
| 611 | return err; |
| 612 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 613 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 614 | static void dummy_signal(int sig) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 615 | { |
| 616 | } |
| 617 | |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 618 | /* If we have signalfd, we mask out the signals we want to handle and then |
| 619 | * use signalfd to listen for them. We rely on whatever the current signal |
| 620 | * handler is to dispatch the signals when we receive them. |
| 621 | */ |
| 622 | static void sigfd_handler(void *opaque) |
| 623 | { |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 624 | int fd = (intptr_t)opaque; |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 625 | struct qemu_signalfd_siginfo info; |
| 626 | struct sigaction action; |
| 627 | ssize_t len; |
| 628 | |
| 629 | while (1) { |
| 630 | do { |
| 631 | len = read(fd, &info, sizeof(info)); |
| 632 | } while (len == -1 && errno == EINTR); |
| 633 | |
| 634 | if (len == -1 && errno == EAGAIN) { |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | if (len != sizeof(info)) { |
| 639 | printf("read from sigfd returned %zd: %m\n", len); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | sigaction(info.ssi_signo, NULL, &action); |
| 644 | if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) { |
| 645 | action.sa_sigaction(info.ssi_signo, |
| 646 | (siginfo_t *)&info, NULL); |
| 647 | } else if (action.sa_handler) { |
| 648 | action.sa_handler(info.ssi_signo); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
Paolo Bonzini | 712ae48 | 2011-03-12 17:44:05 +0100 | [diff] [blame] | 653 | static int qemu_signal_init(void) |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 654 | { |
| 655 | int sigfd; |
Paolo Bonzini | 712ae48 | 2011-03-12 17:44:05 +0100 | [diff] [blame] | 656 | sigset_t set; |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 657 | |
Alexandre Raymond | 89b9ba6 | 2011-06-15 01:20:31 -0400 | [diff] [blame] | 658 | /* |
| 659 | * SIG_IPI must be blocked in the main thread and must not be caught |
| 660 | * by sigwait() in the signal thread. Otherwise, the cpu thread will |
| 661 | * not catch it reliably. |
| 662 | */ |
| 663 | sigemptyset(&set); |
| 664 | sigaddset(&set, SIG_IPI); |
| 665 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 666 | |
Paolo Bonzini | 712ae48 | 2011-03-12 17:44:05 +0100 | [diff] [blame] | 667 | sigemptyset(&set); |
| 668 | sigaddset(&set, SIGIO); |
| 669 | sigaddset(&set, SIGALRM); |
Paolo Bonzini | 712ae48 | 2011-03-12 17:44:05 +0100 | [diff] [blame] | 670 | sigaddset(&set, SIGBUS); |
Alexandre Raymond | 5664aed | 2011-06-14 10:05:36 -0400 | [diff] [blame] | 671 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
Paolo Bonzini | 712ae48 | 2011-03-12 17:44:05 +0100 | [diff] [blame] | 672 | |
| 673 | sigfd = qemu_signalfd(&set); |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 674 | if (sigfd == -1) { |
| 675 | fprintf(stderr, "failed to create signalfd\n"); |
| 676 | return -errno; |
| 677 | } |
| 678 | |
| 679 | fcntl_setfl(sigfd, O_NONBLOCK); |
| 680 | |
| 681 | qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL, |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 682 | (void *)(intptr_t)sigfd); |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 683 | |
| 684 | return 0; |
| 685 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 686 | |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 687 | static void qemu_kvm_init_cpu_signals(CPUState *env) |
| 688 | { |
| 689 | int r; |
| 690 | sigset_t set; |
| 691 | struct sigaction sigact; |
| 692 | |
| 693 | memset(&sigact, 0, sizeof(sigact)); |
| 694 | sigact.sa_handler = dummy_signal; |
| 695 | sigaction(SIG_IPI, &sigact, NULL); |
| 696 | |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 697 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
| 698 | sigdelset(&set, SIG_IPI); |
| 699 | sigdelset(&set, SIGBUS); |
| 700 | r = kvm_set_signal_mask(env, &set); |
| 701 | if (r) { |
| 702 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); |
| 703 | exit(1); |
| 704 | } |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 705 | |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 706 | sigdelset(&set, SIG_IPI); |
| 707 | sigdelset(&set, SIGBUS); |
| 708 | r = kvm_set_signal_mask(env, &set); |
| 709 | if (r) { |
| 710 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); |
| 711 | exit(1); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | static void qemu_tcg_init_cpu_signals(void) |
| 716 | { |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 717 | sigset_t set; |
| 718 | struct sigaction sigact; |
| 719 | |
| 720 | memset(&sigact, 0, sizeof(sigact)); |
| 721 | sigact.sa_handler = cpu_signal; |
| 722 | sigaction(SIG_IPI, &sigact, NULL); |
| 723 | |
| 724 | sigemptyset(&set); |
| 725 | sigaddset(&set, SIG_IPI); |
| 726 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 729 | #else /* _WIN32 */ |
| 730 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 731 | HANDLE qemu_event_handle; |
| 732 | |
| 733 | static void dummy_event_handler(void *opaque) |
| 734 | { |
| 735 | } |
| 736 | |
| 737 | static int qemu_event_init(void) |
| 738 | { |
| 739 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 740 | if (!qemu_event_handle) { |
| 741 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
| 742 | return -1; |
| 743 | } |
| 744 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static void qemu_event_increment(void) |
| 749 | { |
| 750 | if (!SetEvent(qemu_event_handle)) { |
| 751 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", |
| 752 | GetLastError()); |
| 753 | exit (1); |
| 754 | } |
| 755 | } |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 756 | |
Paolo Bonzini | 712ae48 | 2011-03-12 17:44:05 +0100 | [diff] [blame] | 757 | static int qemu_signal_init(void) |
| 758 | { |
| 759 | return 0; |
| 760 | } |
| 761 | |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 762 | static void qemu_kvm_init_cpu_signals(CPUState *env) |
| 763 | { |
| 764 | abort(); |
| 765 | } |
| 766 | |
| 767 | static void qemu_tcg_init_cpu_signals(void) |
| 768 | { |
| 769 | } |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 770 | #endif /* _WIN32 */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 771 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 772 | QemuMutex qemu_global_mutex; |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 773 | static QemuCond qemu_io_proceeded_cond; |
| 774 | static bool iothread_requesting_mutex; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 775 | |
| 776 | static QemuThread io_thread; |
| 777 | |
| 778 | static QemuThread *tcg_cpu_thread; |
| 779 | static QemuCond *tcg_halt_cond; |
| 780 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 781 | /* cpu creation */ |
| 782 | static QemuCond qemu_cpu_cond; |
| 783 | /* system init */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 784 | static QemuCond qemu_pause_cond; |
| 785 | static QemuCond qemu_work_cond; |
| 786 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 787 | int qemu_init_main_loop(void) |
| 788 | { |
| 789 | int ret; |
| 790 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 791 | qemu_init_sigbus(); |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 792 | |
Paolo Bonzini | 712ae48 | 2011-03-12 17:44:05 +0100 | [diff] [blame] | 793 | ret = qemu_signal_init(); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 794 | if (ret) { |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 795 | return ret; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 796 | } |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 797 | |
| 798 | /* Note eventfd must be drained before signalfd handlers run */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 799 | ret = qemu_event_init(); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 800 | if (ret) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 801 | return ret; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 802 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 803 | |
Anthony Liguori | ed94592 | 2011-02-08 18:18:18 +0100 | [diff] [blame] | 804 | qemu_cond_init(&qemu_cpu_cond); |
Anthony Liguori | ed94592 | 2011-02-08 18:18:18 +0100 | [diff] [blame] | 805 | qemu_cond_init(&qemu_pause_cond); |
| 806 | qemu_cond_init(&qemu_work_cond); |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 807 | qemu_cond_init(&qemu_io_proceeded_cond); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 808 | qemu_mutex_init(&qemu_global_mutex); |
| 809 | qemu_mutex_lock(&qemu_global_mutex); |
| 810 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 811 | qemu_thread_get_self(&io_thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 816 | void qemu_main_loop_start(void) |
| 817 | { |
Jan Kiszka | fa7d186 | 2011-08-22 18:35:25 +0200 | [diff] [blame] | 818 | resume_all_vcpus(); |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 821 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
| 822 | { |
| 823 | struct qemu_work_item wi; |
| 824 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 825 | if (qemu_cpu_is_self(env)) { |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 826 | func(data); |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | wi.func = func; |
| 831 | wi.data = data; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 832 | if (!env->queued_work_first) { |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 833 | env->queued_work_first = &wi; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 834 | } else { |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 835 | env->queued_work_last->next = &wi; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 836 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 837 | env->queued_work_last = &wi; |
| 838 | wi.next = NULL; |
| 839 | wi.done = false; |
| 840 | |
| 841 | qemu_cpu_kick(env); |
| 842 | while (!wi.done) { |
| 843 | CPUState *self_env = cpu_single_env; |
| 844 | |
| 845 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); |
| 846 | cpu_single_env = self_env; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | static void flush_queued_work(CPUState *env) |
| 851 | { |
| 852 | struct qemu_work_item *wi; |
| 853 | |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 854 | if (!env->queued_work_first) { |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 855 | return; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 856 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 857 | |
| 858 | while ((wi = env->queued_work_first)) { |
| 859 | env->queued_work_first = wi->next; |
| 860 | wi->func(wi->data); |
| 861 | wi->done = true; |
| 862 | } |
| 863 | env->queued_work_last = NULL; |
| 864 | qemu_cond_broadcast(&qemu_work_cond); |
| 865 | } |
| 866 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 867 | static void qemu_wait_io_event_common(CPUState *env) |
| 868 | { |
| 869 | if (env->stop) { |
| 870 | env->stop = 0; |
| 871 | env->stopped = 1; |
| 872 | qemu_cond_signal(&qemu_pause_cond); |
| 873 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 874 | flush_queued_work(env); |
Jan Kiszka | aa2c364 | 2011-02-01 22:15:42 +0100 | [diff] [blame] | 875 | env->thread_kicked = false; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 878 | static void qemu_tcg_wait_io_event(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 879 | { |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 880 | CPUState *env; |
| 881 | |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 882 | while (all_cpu_threads_idle()) { |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 883 | /* Start accounting real time to the virtual clock if the CPUs |
| 884 | are idle. */ |
| 885 | qemu_clock_warp(vm_clock); |
Paolo Bonzini | 9705fbb | 2011-03-12 17:44:00 +0100 | [diff] [blame] | 886 | qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex); |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 887 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 888 | |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 889 | while (iothread_requesting_mutex) { |
| 890 | qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex); |
| 891 | } |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 892 | |
| 893 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 894 | qemu_wait_io_event_common(env); |
| 895 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 898 | static void qemu_kvm_wait_io_event(CPUState *env) |
| 899 | { |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 900 | while (cpu_thread_is_idle(env)) { |
Paolo Bonzini | 9705fbb | 2011-03-12 17:44:00 +0100 | [diff] [blame] | 901 | qemu_cond_wait(env->halt_cond, &qemu_global_mutex); |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 902 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 903 | |
Jan Kiszka | 5db5bda | 2011-02-01 22:15:54 +0100 | [diff] [blame] | 904 | qemu_kvm_eat_signals(env); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 905 | qemu_wait_io_event_common(env); |
| 906 | } |
| 907 | |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 908 | static void *qemu_kvm_cpu_thread_fn(void *arg) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 909 | { |
| 910 | CPUState *env = arg; |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 911 | int r; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 912 | |
Marcelo Tosatti | 6164e6d | 2010-03-23 13:37:13 -0300 | [diff] [blame] | 913 | qemu_mutex_lock(&qemu_global_mutex); |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 914 | qemu_thread_get_self(env->thread); |
Jan Kiszka | dc7a09c | 2011-03-15 12:26:31 +0100 | [diff] [blame] | 915 | env->thread_id = qemu_get_thread_id(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 916 | |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 917 | r = kvm_init_vcpu(env); |
| 918 | if (r < 0) { |
| 919 | fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r)); |
| 920 | exit(1); |
| 921 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 922 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 923 | qemu_kvm_init_cpu_signals(env); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 924 | |
| 925 | /* signal CPU creation */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 926 | env->created = 1; |
| 927 | qemu_cond_signal(&qemu_cpu_cond); |
| 928 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 929 | while (1) { |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 930 | if (cpu_can_run(env)) { |
Jan Kiszka | 6792a57 | 2011-02-07 12:19:18 +0100 | [diff] [blame] | 931 | r = kvm_cpu_exec(env); |
Jan Kiszka | 83f338f | 2011-02-07 12:19:17 +0100 | [diff] [blame] | 932 | if (r == EXCP_DEBUG) { |
Jan Kiszka | 1009d2e | 2011-03-15 12:26:13 +0100 | [diff] [blame] | 933 | cpu_handle_guest_debug(env); |
Jan Kiszka | 83f338f | 2011-02-07 12:19:17 +0100 | [diff] [blame] | 934 | } |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 935 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 936 | qemu_kvm_wait_io_event(env); |
| 937 | } |
| 938 | |
| 939 | return NULL; |
| 940 | } |
| 941 | |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 942 | static void *qemu_tcg_cpu_thread_fn(void *arg) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 943 | { |
| 944 | CPUState *env = arg; |
| 945 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 946 | qemu_tcg_init_cpu_signals(); |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 947 | qemu_thread_get_self(env->thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 948 | |
| 949 | /* signal CPU creation */ |
| 950 | qemu_mutex_lock(&qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 951 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
Jan Kiszka | dc7a09c | 2011-03-15 12:26:31 +0100 | [diff] [blame] | 952 | env->thread_id = qemu_get_thread_id(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 953 | env->created = 1; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 954 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 955 | qemu_cond_signal(&qemu_cpu_cond); |
| 956 | |
Jan Kiszka | fa7d186 | 2011-08-22 18:35:25 +0200 | [diff] [blame] | 957 | /* wait for initial kick-off after machine start */ |
| 958 | while (first_cpu->stopped) { |
| 959 | qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 960 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 961 | |
| 962 | while (1) { |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 963 | cpu_exec_all(); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 964 | if (use_icount && qemu_clock_deadline(vm_clock) <= 0) { |
Paolo Bonzini | 3b2319a | 2011-04-13 10:03:43 +0200 | [diff] [blame] | 965 | qemu_notify_event(); |
| 966 | } |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 967 | qemu_tcg_wait_io_event(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | return NULL; |
| 971 | } |
| 972 | |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 973 | static void qemu_cpu_kick_thread(CPUState *env) |
| 974 | { |
| 975 | #ifndef _WIN32 |
| 976 | int err; |
| 977 | |
| 978 | err = pthread_kill(env->thread->thread, SIG_IPI); |
| 979 | if (err) { |
| 980 | fprintf(stderr, "qemu:%s: %s", __func__, strerror(err)); |
| 981 | exit(1); |
| 982 | } |
| 983 | #else /* _WIN32 */ |
| 984 | if (!qemu_cpu_is_self(env)) { |
| 985 | SuspendThread(env->thread->thread); |
| 986 | cpu_signal(0); |
| 987 | ResumeThread(env->thread->thread); |
| 988 | } |
| 989 | #endif |
| 990 | } |
| 991 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 992 | void qemu_cpu_kick(void *_env) |
| 993 | { |
| 994 | CPUState *env = _env; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 995 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 996 | qemu_cond_broadcast(env->halt_cond); |
Jan Kiszka | eae74cf | 2011-08-22 17:46:03 +0200 | [diff] [blame] | 997 | if (kvm_enabled() && !env->thread_kicked) { |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 998 | qemu_cpu_kick_thread(env); |
Jan Kiszka | aa2c364 | 2011-02-01 22:15:42 +0100 | [diff] [blame] | 999 | env->thread_kicked = true; |
| 1000 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Jan Kiszka | 46d62fa | 2011-02-01 22:15:59 +0100 | [diff] [blame] | 1003 | void qemu_cpu_kick_self(void) |
| 1004 | { |
Paolo Bonzini | b55c22c | 2011-03-12 17:44:07 +0100 | [diff] [blame] | 1005 | #ifndef _WIN32 |
Jan Kiszka | 46d62fa | 2011-02-01 22:15:59 +0100 | [diff] [blame] | 1006 | assert(cpu_single_env); |
| 1007 | |
| 1008 | if (!cpu_single_env->thread_kicked) { |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1009 | qemu_cpu_kick_thread(cpu_single_env); |
Jan Kiszka | 46d62fa | 2011-02-01 22:15:59 +0100 | [diff] [blame] | 1010 | cpu_single_env->thread_kicked = true; |
| 1011 | } |
Paolo Bonzini | b55c22c | 2011-03-12 17:44:07 +0100 | [diff] [blame] | 1012 | #else |
| 1013 | abort(); |
| 1014 | #endif |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 1017 | int qemu_cpu_is_self(void *_env) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1018 | { |
| 1019 | CPUState *env = _env; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1020 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 1021 | return qemu_thread_is_self(env->thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1024 | void qemu_mutex_lock_iothread(void) |
| 1025 | { |
| 1026 | if (kvm_enabled()) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1027 | qemu_mutex_lock(&qemu_global_mutex); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1028 | } else { |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 1029 | iothread_requesting_mutex = true; |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1030 | if (qemu_mutex_trylock(&qemu_global_mutex)) { |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1031 | qemu_cpu_kick_thread(first_cpu); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1032 | qemu_mutex_lock(&qemu_global_mutex); |
| 1033 | } |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 1034 | iothread_requesting_mutex = false; |
| 1035 | qemu_cond_broadcast(&qemu_io_proceeded_cond); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1036 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | void qemu_mutex_unlock_iothread(void) |
| 1040 | { |
| 1041 | qemu_mutex_unlock(&qemu_global_mutex); |
| 1042 | } |
| 1043 | |
| 1044 | static int all_vcpus_paused(void) |
| 1045 | { |
| 1046 | CPUState *penv = first_cpu; |
| 1047 | |
| 1048 | while (penv) { |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1049 | if (!penv->stopped) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1050 | return 0; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1051 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1052 | penv = (CPUState *)penv->next_cpu; |
| 1053 | } |
| 1054 | |
| 1055 | return 1; |
| 1056 | } |
| 1057 | |
| 1058 | void pause_all_vcpus(void) |
| 1059 | { |
| 1060 | CPUState *penv = first_cpu; |
| 1061 | |
Paolo Bonzini | a5c57d6 | 2011-09-12 14:40:36 +0200 | [diff] [blame^] | 1062 | qemu_clock_enable(vm_clock, false); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1063 | while (penv) { |
| 1064 | penv->stop = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1065 | qemu_cpu_kick(penv); |
| 1066 | penv = (CPUState *)penv->next_cpu; |
| 1067 | } |
| 1068 | |
| 1069 | while (!all_vcpus_paused()) { |
Paolo Bonzini | be7d6c5 | 2011-03-12 17:44:02 +0100 | [diff] [blame] | 1070 | qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1071 | penv = first_cpu; |
| 1072 | while (penv) { |
Marcelo Tosatti | 1fbb22e | 2010-05-04 09:45:21 -0300 | [diff] [blame] | 1073 | qemu_cpu_kick(penv); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1074 | penv = (CPUState *)penv->next_cpu; |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | void resume_all_vcpus(void) |
| 1080 | { |
| 1081 | CPUState *penv = first_cpu; |
| 1082 | |
| 1083 | while (penv) { |
| 1084 | penv->stop = 0; |
| 1085 | penv->stopped = 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1086 | qemu_cpu_kick(penv); |
| 1087 | penv = (CPUState *)penv->next_cpu; |
| 1088 | } |
| 1089 | } |
| 1090 | |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1091 | static void qemu_tcg_init_vcpu(void *_env) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1092 | { |
| 1093 | CPUState *env = _env; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1094 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1095 | /* share a single thread for all cpus with TCG */ |
| 1096 | if (!tcg_cpu_thread) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1097 | env->thread = g_malloc0(sizeof(QemuThread)); |
| 1098 | env->halt_cond = g_malloc0(sizeof(QemuCond)); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1099 | qemu_cond_init(env->halt_cond); |
Jan Kiszka | fa7d186 | 2011-08-22 18:35:25 +0200 | [diff] [blame] | 1100 | tcg_halt_cond = env->halt_cond; |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1101 | qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1102 | while (env->created == 0) { |
Paolo Bonzini | 18a8572 | 2011-03-12 17:44:03 +0100 | [diff] [blame] | 1103 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1104 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1105 | tcg_cpu_thread = env->thread; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1106 | } else { |
| 1107 | env->thread = tcg_cpu_thread; |
| 1108 | env->halt_cond = tcg_halt_cond; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1112 | static void qemu_kvm_start_vcpu(CPUState *env) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1113 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1114 | env->thread = g_malloc0(sizeof(QemuThread)); |
| 1115 | env->halt_cond = g_malloc0(sizeof(QemuCond)); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1116 | qemu_cond_init(env->halt_cond); |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1117 | qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1118 | while (env->created == 0) { |
Paolo Bonzini | 18a8572 | 2011-03-12 17:44:03 +0100 | [diff] [blame] | 1119 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1120 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void qemu_init_vcpu(void *_env) |
| 1124 | { |
| 1125 | CPUState *env = _env; |
| 1126 | |
| 1127 | env->nr_cores = smp_cores; |
| 1128 | env->nr_threads = smp_threads; |
Jan Kiszka | fa7d186 | 2011-08-22 18:35:25 +0200 | [diff] [blame] | 1129 | env->stopped = 1; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1130 | if (kvm_enabled()) { |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1131 | qemu_kvm_start_vcpu(env); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1132 | } else { |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1133 | qemu_tcg_init_vcpu(env); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1134 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | void qemu_notify_event(void) |
| 1138 | { |
| 1139 | qemu_event_increment(); |
| 1140 | } |
| 1141 | |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1142 | void cpu_stop_current(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1143 | { |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1144 | if (cpu_single_env) { |
Paolo Bonzini | 67bb172 | 2011-03-12 17:43:59 +0100 | [diff] [blame] | 1145 | cpu_single_env->stop = 0; |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1146 | cpu_single_env->stopped = 1; |
| 1147 | cpu_exit(cpu_single_env); |
Paolo Bonzini | 67bb172 | 2011-03-12 17:43:59 +0100 | [diff] [blame] | 1148 | qemu_cond_signal(&qemu_pause_cond); |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1149 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1152 | void vm_stop(RunState state) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1153 | { |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 1154 | if (!qemu_thread_is_self(&io_thread)) { |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1155 | qemu_system_vmstop_request(state); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1156 | /* |
| 1157 | * FIXME: should not return to device code in case |
| 1158 | * vm_stop() has been requested. |
| 1159 | */ |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1160 | cpu_stop_current(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1161 | return; |
| 1162 | } |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1163 | do_vm_stop(state); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 1166 | /* does a state transition even if the VM is already stopped, |
| 1167 | current state is forgotten forever */ |
| 1168 | void vm_stop_force_state(RunState state) |
| 1169 | { |
| 1170 | if (runstate_is_running()) { |
| 1171 | vm_stop(state); |
| 1172 | } else { |
| 1173 | runstate_set(state); |
| 1174 | } |
| 1175 | } |
| 1176 | |
Jan Kiszka | 6792a57 | 2011-02-07 12:19:18 +0100 | [diff] [blame] | 1177 | static int tcg_cpu_exec(CPUState *env) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1178 | { |
| 1179 | int ret; |
| 1180 | #ifdef CONFIG_PROFILER |
| 1181 | int64_t ti; |
| 1182 | #endif |
| 1183 | |
| 1184 | #ifdef CONFIG_PROFILER |
| 1185 | ti = profile_getclock(); |
| 1186 | #endif |
| 1187 | if (use_icount) { |
| 1188 | int64_t count; |
| 1189 | int decr; |
| 1190 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 1191 | env->icount_decr.u16.low = 0; |
| 1192 | env->icount_extra = 0; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 1193 | count = qemu_icount_round(qemu_clock_deadline(vm_clock)); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1194 | qemu_icount += count; |
| 1195 | decr = (count > 0xffff) ? 0xffff : count; |
| 1196 | count -= decr; |
| 1197 | env->icount_decr.u16.low = decr; |
| 1198 | env->icount_extra = count; |
| 1199 | } |
| 1200 | ret = cpu_exec(env); |
| 1201 | #ifdef CONFIG_PROFILER |
| 1202 | qemu_time += profile_getclock() - ti; |
| 1203 | #endif |
| 1204 | if (use_icount) { |
| 1205 | /* Fold pending instructions back into the |
| 1206 | instruction counter, and clear the interrupt flag. */ |
| 1207 | qemu_icount -= (env->icount_decr.u16.low |
| 1208 | + env->icount_extra); |
| 1209 | env->icount_decr.u32 = 0; |
| 1210 | env->icount_extra = 0; |
| 1211 | } |
| 1212 | return ret; |
| 1213 | } |
| 1214 | |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 1215 | bool cpu_exec_all(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1216 | { |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 1217 | int r; |
| 1218 | |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 1219 | /* Account partial waits to the vm_clock. */ |
| 1220 | qemu_clock_warp(vm_clock); |
| 1221 | |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1222 | if (next_cpu == NULL) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1223 | next_cpu = first_cpu; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1224 | } |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 1225 | for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) { |
Jan Kiszka | 345f442 | 2010-06-25 16:56:54 +0200 | [diff] [blame] | 1226 | CPUState *env = next_cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1227 | |
| 1228 | qemu_clock_enable(vm_clock, |
Jan Kiszka | 345f442 | 2010-06-25 16:56:54 +0200 | [diff] [blame] | 1229 | (env->singlestep_enabled & SSTEP_NOTIMER) == 0); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1230 | |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 1231 | if (cpu_can_run(env)) { |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 1232 | if (kvm_enabled()) { |
Jan Kiszka | 6792a57 | 2011-02-07 12:19:18 +0100 | [diff] [blame] | 1233 | r = kvm_cpu_exec(env); |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 1234 | qemu_kvm_eat_signals(env); |
Jan Kiszka | 6792a57 | 2011-02-07 12:19:18 +0100 | [diff] [blame] | 1235 | } else { |
| 1236 | r = tcg_cpu_exec(env); |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 1237 | } |
| 1238 | if (r == EXCP_DEBUG) { |
Jan Kiszka | 1009d2e | 2011-03-15 12:26:13 +0100 | [diff] [blame] | 1239 | cpu_handle_guest_debug(env); |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 1240 | break; |
| 1241 | } |
Paolo Bonzini | df646df | 2011-03-12 17:43:58 +0100 | [diff] [blame] | 1242 | } else if (env->stop || env->stopped) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | } |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 1246 | exit_request = 0; |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 1247 | return !all_cpu_threads_idle(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | void set_numa_modes(void) |
| 1251 | { |
| 1252 | CPUState *env; |
| 1253 | int i; |
| 1254 | |
| 1255 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 1256 | for (i = 0; i < nb_numa_nodes; i++) { |
| 1257 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 1258 | env->numa_node = i; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | void set_cpu_log(const char *optarg) |
| 1265 | { |
| 1266 | int mask; |
| 1267 | const CPULogItem *item; |
| 1268 | |
| 1269 | mask = cpu_str_to_log_mask(optarg); |
| 1270 | if (!mask) { |
| 1271 | printf("Log items (comma separated):\n"); |
| 1272 | for (item = cpu_log_items; item->mask != 0; item++) { |
| 1273 | printf("%-10s %s\n", item->name, item->help); |
| 1274 | } |
| 1275 | exit(1); |
| 1276 | } |
| 1277 | cpu_set_log(mask); |
| 1278 | } |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 1279 | |
Matthew Fernandez | c235d73 | 2011-06-07 16:32:40 +0000 | [diff] [blame] | 1280 | void set_cpu_log_filename(const char *optarg) |
| 1281 | { |
| 1282 | cpu_set_log_filename(optarg); |
| 1283 | } |
| 1284 | |
Stefan Weil | 9a78eea | 2010-10-22 23:03:33 +0200 | [diff] [blame] | 1285 | void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 1286 | { |
| 1287 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 1288 | #if defined(cpu_list_id) |
| 1289 | cpu_list_id(f, cpu_fprintf, optarg); |
| 1290 | #elif defined(cpu_list) |
| 1291 | cpu_list(f, cpu_fprintf); /* deprecated */ |
| 1292 | #endif |
| 1293 | } |