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