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" |
Jan Kiszka | 262ea18 | 2010-07-06 10:49:57 +0200 | [diff] [blame] | 33 | #include "exec-all.h" |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 34 | |
| 35 | #include "cpus.h" |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 36 | #include "compatfd.h" |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 37 | #ifdef CONFIG_LINUX |
| 38 | #include <sys/prctl.h> |
| 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 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 47 | #ifndef PR_MCE_KILL |
| 48 | #define PR_MCE_KILL 33 |
| 49 | #endif |
| 50 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 51 | static CPUState *next_cpu; |
| 52 | |
| 53 | /***********************************************************/ |
| 54 | void hw_error(const char *fmt, ...) |
| 55 | { |
| 56 | va_list ap; |
| 57 | CPUState *env; |
| 58 | |
| 59 | va_start(ap, fmt); |
| 60 | fprintf(stderr, "qemu: hardware error: "); |
| 61 | vfprintf(stderr, fmt, ap); |
| 62 | fprintf(stderr, "\n"); |
| 63 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 64 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
| 65 | #ifdef TARGET_I386 |
| 66 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
| 67 | #else |
| 68 | cpu_dump_state(env, stderr, fprintf, 0); |
| 69 | #endif |
| 70 | } |
| 71 | va_end(ap); |
| 72 | abort(); |
| 73 | } |
| 74 | |
| 75 | void cpu_synchronize_all_states(void) |
| 76 | { |
| 77 | CPUState *cpu; |
| 78 | |
| 79 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { |
| 80 | cpu_synchronize_state(cpu); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void cpu_synchronize_all_post_reset(void) |
| 85 | { |
| 86 | CPUState *cpu; |
| 87 | |
| 88 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { |
| 89 | cpu_synchronize_post_reset(cpu); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void cpu_synchronize_all_post_init(void) |
| 94 | { |
| 95 | CPUState *cpu; |
| 96 | |
| 97 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { |
| 98 | cpu_synchronize_post_init(cpu); |
| 99 | } |
| 100 | } |
| 101 | |
Marcelo Tosatti | 3ae9501 | 2010-05-04 09:45:24 -0300 | [diff] [blame] | 102 | int cpu_is_stopped(CPUState *env) |
| 103 | { |
| 104 | return !vm_running || env->stopped; |
| 105 | } |
| 106 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 107 | static void do_vm_stop(int reason) |
| 108 | { |
| 109 | if (vm_running) { |
| 110 | cpu_disable_ticks(); |
| 111 | vm_running = 0; |
| 112 | pause_all_vcpus(); |
| 113 | vm_state_notify(0, reason); |
Michael S. Tsirkin | 55df6f3 | 2010-11-22 19:52:22 +0200 | [diff] [blame] | 114 | qemu_aio_flush(); |
| 115 | bdrv_flush_all(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 116 | monitor_protocol_event(QEVENT_STOP, NULL); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static int cpu_can_run(CPUState *env) |
| 121 | { |
| 122 | if (env->stop) |
| 123 | return 0; |
Paolo Bonzini | 55274a3 | 2010-04-07 00:11:09 +0200 | [diff] [blame] | 124 | if (env->stopped || !vm_running) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 125 | return 0; |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | static int cpu_has_work(CPUState *env) |
| 130 | { |
| 131 | if (env->stop) |
| 132 | return 1; |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 133 | if (env->queued_work_first) |
| 134 | return 1; |
Paolo Bonzini | 55274a3 | 2010-04-07 00:11:09 +0200 | [diff] [blame] | 135 | if (env->stopped || !vm_running) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | if (!env->halted) |
| 138 | return 1; |
| 139 | if (qemu_cpu_has_work(env)) |
| 140 | return 1; |
| 141 | return 0; |
| 142 | } |
| 143 | |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 144 | static int any_cpu_has_work(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 145 | { |
| 146 | CPUState *env; |
| 147 | |
| 148 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 149 | if (cpu_has_work(env)) |
| 150 | return 1; |
| 151 | return 0; |
| 152 | } |
| 153 | |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 154 | static void cpu_debug_handler(CPUState *env) |
| 155 | { |
| 156 | gdb_set_stop_cpu(env); |
| 157 | debug_requested = EXCP_DEBUG; |
| 158 | vm_stop(EXCP_DEBUG); |
| 159 | } |
| 160 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 161 | #ifndef _WIN32 |
| 162 | static int io_thread_fd = -1; |
| 163 | |
| 164 | static void qemu_event_increment(void) |
| 165 | { |
| 166 | /* Write 8 bytes to be compatible with eventfd. */ |
Blue Swirl | 26a8233 | 2010-05-14 19:32:21 +0000 | [diff] [blame] | 167 | static const uint64_t val = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 168 | ssize_t ret; |
| 169 | |
| 170 | if (io_thread_fd == -1) |
| 171 | return; |
| 172 | |
| 173 | do { |
| 174 | ret = write(io_thread_fd, &val, sizeof(val)); |
| 175 | } while (ret < 0 && errno == EINTR); |
| 176 | |
| 177 | /* EAGAIN is fine, a read must be pending. */ |
| 178 | if (ret < 0 && errno != EAGAIN) { |
| 179 | fprintf(stderr, "qemu_event_increment: write() filed: %s\n", |
| 180 | strerror(errno)); |
| 181 | exit (1); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static void qemu_event_read(void *opaque) |
| 186 | { |
| 187 | int fd = (unsigned long)opaque; |
| 188 | ssize_t len; |
| 189 | char buffer[512]; |
| 190 | |
| 191 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
| 192 | do { |
| 193 | len = read(fd, buffer, sizeof(buffer)); |
| 194 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
| 195 | } |
| 196 | |
| 197 | static int qemu_event_init(void) |
| 198 | { |
| 199 | int err; |
| 200 | int fds[2]; |
| 201 | |
| 202 | err = qemu_eventfd(fds); |
| 203 | if (err == -1) |
| 204 | return -errno; |
| 205 | |
| 206 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
| 207 | if (err < 0) |
| 208 | goto fail; |
| 209 | |
| 210 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
| 211 | if (err < 0) |
| 212 | goto fail; |
| 213 | |
| 214 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
| 215 | (void *)(unsigned long)fds[0]); |
| 216 | |
| 217 | io_thread_fd = fds[1]; |
| 218 | return 0; |
| 219 | |
| 220 | fail: |
| 221 | close(fds[0]); |
| 222 | close(fds[1]); |
| 223 | return err; |
| 224 | } |
| 225 | #else |
| 226 | HANDLE qemu_event_handle; |
| 227 | |
| 228 | static void dummy_event_handler(void *opaque) |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | static int qemu_event_init(void) |
| 233 | { |
| 234 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 235 | if (!qemu_event_handle) { |
| 236 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
| 237 | return -1; |
| 238 | } |
| 239 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static void qemu_event_increment(void) |
| 244 | { |
| 245 | if (!SetEvent(qemu_event_handle)) { |
| 246 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", |
| 247 | GetLastError()); |
| 248 | exit (1); |
| 249 | } |
| 250 | } |
| 251 | #endif |
| 252 | |
| 253 | #ifndef CONFIG_IOTHREAD |
| 254 | int qemu_init_main_loop(void) |
| 255 | { |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 256 | cpu_set_debug_excp_handler(cpu_debug_handler); |
| 257 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 258 | return qemu_event_init(); |
| 259 | } |
| 260 | |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 261 | void qemu_main_loop_start(void) |
| 262 | { |
| 263 | } |
| 264 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 265 | void qemu_init_vcpu(void *_env) |
| 266 | { |
| 267 | CPUState *env = _env; |
| 268 | |
| 269 | env->nr_cores = smp_cores; |
| 270 | env->nr_threads = smp_threads; |
| 271 | if (kvm_enabled()) |
| 272 | kvm_init_vcpu(env); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | int qemu_cpu_self(void *env) |
| 277 | { |
| 278 | return 1; |
| 279 | } |
| 280 | |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 281 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
| 282 | { |
| 283 | func(data); |
| 284 | } |
| 285 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 286 | void resume_all_vcpus(void) |
| 287 | { |
| 288 | } |
| 289 | |
| 290 | void pause_all_vcpus(void) |
| 291 | { |
| 292 | } |
| 293 | |
| 294 | void qemu_cpu_kick(void *env) |
| 295 | { |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | void qemu_notify_event(void) |
| 300 | { |
| 301 | CPUState *env = cpu_single_env; |
| 302 | |
| 303 | qemu_event_increment (); |
| 304 | if (env) { |
| 305 | cpu_exit(env); |
| 306 | } |
| 307 | if (next_cpu && env != next_cpu) { |
| 308 | cpu_exit(next_cpu); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void qemu_mutex_lock_iothread(void) {} |
| 313 | void qemu_mutex_unlock_iothread(void) {} |
| 314 | |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 315 | void cpu_stop_current(void) |
| 316 | { |
| 317 | } |
| 318 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 319 | void vm_stop(int reason) |
| 320 | { |
| 321 | do_vm_stop(reason); |
| 322 | } |
| 323 | |
| 324 | #else /* CONFIG_IOTHREAD */ |
| 325 | |
| 326 | #include "qemu-thread.h" |
| 327 | |
| 328 | QemuMutex qemu_global_mutex; |
| 329 | static QemuMutex qemu_fair_mutex; |
| 330 | |
| 331 | static QemuThread io_thread; |
| 332 | |
| 333 | static QemuThread *tcg_cpu_thread; |
| 334 | static QemuCond *tcg_halt_cond; |
| 335 | |
| 336 | static int qemu_system_ready; |
| 337 | /* cpu creation */ |
| 338 | static QemuCond qemu_cpu_cond; |
| 339 | /* system init */ |
| 340 | static QemuCond qemu_system_cond; |
| 341 | static QemuCond qemu_pause_cond; |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 342 | static QemuCond qemu_work_cond; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 343 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 344 | static void tcg_init_ipi(void); |
| 345 | static void kvm_init_ipi(CPUState *env); |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 346 | static sigset_t block_io_signals(void); |
| 347 | |
| 348 | /* If we have signalfd, we mask out the signals we want to handle and then |
| 349 | * use signalfd to listen for them. We rely on whatever the current signal |
| 350 | * handler is to dispatch the signals when we receive them. |
| 351 | */ |
| 352 | static void sigfd_handler(void *opaque) |
| 353 | { |
| 354 | int fd = (unsigned long) opaque; |
| 355 | struct qemu_signalfd_siginfo info; |
| 356 | struct sigaction action; |
| 357 | ssize_t len; |
| 358 | |
| 359 | while (1) { |
| 360 | do { |
| 361 | len = read(fd, &info, sizeof(info)); |
| 362 | } while (len == -1 && errno == EINTR); |
| 363 | |
| 364 | if (len == -1 && errno == EAGAIN) { |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | if (len != sizeof(info)) { |
| 369 | printf("read from sigfd returned %zd: %m\n", len); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | sigaction(info.ssi_signo, NULL, &action); |
| 374 | if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) { |
| 375 | action.sa_sigaction(info.ssi_signo, |
| 376 | (siginfo_t *)&info, NULL); |
| 377 | } else if (action.sa_handler) { |
| 378 | action.sa_handler(info.ssi_signo); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | static int qemu_signalfd_init(sigset_t mask) |
| 384 | { |
| 385 | int sigfd; |
| 386 | |
| 387 | sigfd = qemu_signalfd(&mask); |
| 388 | if (sigfd == -1) { |
| 389 | fprintf(stderr, "failed to create signalfd\n"); |
| 390 | return -errno; |
| 391 | } |
| 392 | |
| 393 | fcntl_setfl(sigfd, O_NONBLOCK); |
| 394 | |
| 395 | qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL, |
| 396 | (void *)(unsigned long) sigfd); |
| 397 | |
| 398 | return 0; |
| 399 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 400 | |
| 401 | int qemu_init_main_loop(void) |
| 402 | { |
| 403 | int ret; |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 404 | sigset_t blocked_signals; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 405 | |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 406 | cpu_set_debug_excp_handler(cpu_debug_handler); |
| 407 | |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 408 | blocked_signals = block_io_signals(); |
| 409 | |
| 410 | ret = qemu_signalfd_init(blocked_signals); |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | |
| 414 | /* Note eventfd must be drained before signalfd handlers run */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 415 | ret = qemu_event_init(); |
| 416 | if (ret) |
| 417 | return ret; |
| 418 | |
| 419 | qemu_cond_init(&qemu_pause_cond); |
Jan Kiszka | f8ca7b4 | 2010-06-25 16:56:51 +0200 | [diff] [blame] | 420 | qemu_cond_init(&qemu_system_cond); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 421 | qemu_mutex_init(&qemu_fair_mutex); |
| 422 | qemu_mutex_init(&qemu_global_mutex); |
| 423 | qemu_mutex_lock(&qemu_global_mutex); |
| 424 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 425 | qemu_thread_self(&io_thread); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 430 | void qemu_main_loop_start(void) |
| 431 | { |
| 432 | qemu_system_ready = 1; |
| 433 | qemu_cond_broadcast(&qemu_system_cond); |
| 434 | } |
| 435 | |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 436 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
| 437 | { |
| 438 | struct qemu_work_item wi; |
| 439 | |
| 440 | if (qemu_cpu_self(env)) { |
| 441 | func(data); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | wi.func = func; |
| 446 | wi.data = data; |
| 447 | if (!env->queued_work_first) |
| 448 | env->queued_work_first = &wi; |
| 449 | else |
| 450 | env->queued_work_last->next = &wi; |
| 451 | env->queued_work_last = &wi; |
| 452 | wi.next = NULL; |
| 453 | wi.done = false; |
| 454 | |
| 455 | qemu_cpu_kick(env); |
| 456 | while (!wi.done) { |
| 457 | CPUState *self_env = cpu_single_env; |
| 458 | |
| 459 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); |
| 460 | cpu_single_env = self_env; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | static void flush_queued_work(CPUState *env) |
| 465 | { |
| 466 | struct qemu_work_item *wi; |
| 467 | |
| 468 | if (!env->queued_work_first) |
| 469 | return; |
| 470 | |
| 471 | while ((wi = env->queued_work_first)) { |
| 472 | env->queued_work_first = wi->next; |
| 473 | wi->func(wi->data); |
| 474 | wi->done = true; |
| 475 | } |
| 476 | env->queued_work_last = NULL; |
| 477 | qemu_cond_broadcast(&qemu_work_cond); |
| 478 | } |
| 479 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 480 | static void qemu_wait_io_event_common(CPUState *env) |
| 481 | { |
| 482 | if (env->stop) { |
| 483 | env->stop = 0; |
| 484 | env->stopped = 1; |
| 485 | qemu_cond_signal(&qemu_pause_cond); |
| 486 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 487 | flush_queued_work(env); |
Jan Kiszka | aa2c364 | 2011-02-01 22:15:42 +0100 | [diff] [blame] | 488 | env->thread_kicked = false; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 491 | static void qemu_tcg_wait_io_event(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 492 | { |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 493 | CPUState *env; |
| 494 | |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 495 | while (!any_cpu_has_work()) |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 496 | qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 497 | |
| 498 | qemu_mutex_unlock(&qemu_global_mutex); |
| 499 | |
| 500 | /* |
| 501 | * Users of qemu_global_mutex can be starved, having no chance |
| 502 | * to acquire it since this path will get to it first. |
| 503 | * So use another lock to provide fairness. |
| 504 | */ |
| 505 | qemu_mutex_lock(&qemu_fair_mutex); |
| 506 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 507 | |
| 508 | qemu_mutex_lock(&qemu_global_mutex); |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 509 | |
| 510 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 511 | qemu_wait_io_event_common(env); |
| 512 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 515 | static void sigbus_reraise(void) |
| 516 | { |
| 517 | sigset_t set; |
| 518 | struct sigaction action; |
| 519 | |
| 520 | memset(&action, 0, sizeof(action)); |
| 521 | action.sa_handler = SIG_DFL; |
| 522 | if (!sigaction(SIGBUS, &action, NULL)) { |
| 523 | raise(SIGBUS); |
| 524 | sigemptyset(&set); |
| 525 | sigaddset(&set, SIGBUS); |
| 526 | sigprocmask(SIG_UNBLOCK, &set, NULL); |
| 527 | } |
| 528 | perror("Failed to re-raise SIGBUS!\n"); |
| 529 | abort(); |
| 530 | } |
| 531 | |
| 532 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, |
| 533 | void *ctx) |
| 534 | { |
| 535 | #if defined(TARGET_I386) |
| 536 | if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) |
| 537 | #endif |
| 538 | sigbus_reraise(); |
| 539 | } |
| 540 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 541 | static void qemu_kvm_eat_signal(CPUState *env, int timeout) |
| 542 | { |
| 543 | struct timespec ts; |
| 544 | int r, e; |
| 545 | siginfo_t siginfo; |
| 546 | sigset_t waitset; |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 547 | sigset_t chkset; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 548 | |
| 549 | ts.tv_sec = timeout / 1000; |
| 550 | ts.tv_nsec = (timeout % 1000) * 1000000; |
| 551 | |
| 552 | sigemptyset(&waitset); |
| 553 | sigaddset(&waitset, SIG_IPI); |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 554 | sigaddset(&waitset, SIGBUS); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 555 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 556 | do { |
| 557 | qemu_mutex_unlock(&qemu_global_mutex); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 558 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 559 | r = sigtimedwait(&waitset, &siginfo, &ts); |
| 560 | e = errno; |
| 561 | |
| 562 | qemu_mutex_lock(&qemu_global_mutex); |
| 563 | |
| 564 | if (r == -1 && !(e == EAGAIN || e == EINTR)) { |
| 565 | fprintf(stderr, "sigtimedwait: %s\n", strerror(e)); |
| 566 | exit(1); |
| 567 | } |
| 568 | |
| 569 | switch (r) { |
| 570 | case SIGBUS: |
| 571 | #ifdef TARGET_I386 |
| 572 | if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) |
| 573 | #endif |
| 574 | sigbus_reraise(); |
| 575 | break; |
| 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | r = sigpending(&chkset); |
| 581 | if (r == -1) { |
| 582 | fprintf(stderr, "sigpending: %s\n", strerror(e)); |
| 583 | exit(1); |
| 584 | } |
| 585 | } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS)); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static void qemu_kvm_wait_io_event(CPUState *env) |
| 589 | { |
| 590 | while (!cpu_has_work(env)) |
| 591 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 592 | |
| 593 | qemu_kvm_eat_signal(env, 0); |
| 594 | qemu_wait_io_event_common(env); |
| 595 | } |
| 596 | |
| 597 | static int qemu_cpu_exec(CPUState *env); |
| 598 | |
| 599 | static void *kvm_cpu_thread_fn(void *arg) |
| 600 | { |
| 601 | CPUState *env = arg; |
| 602 | |
Marcelo Tosatti | 6164e6d | 2010-03-23 13:37:13 -0300 | [diff] [blame] | 603 | qemu_mutex_lock(&qemu_global_mutex); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 604 | qemu_thread_self(env->thread); |
| 605 | if (kvm_enabled()) |
| 606 | kvm_init_vcpu(env); |
| 607 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 608 | kvm_init_ipi(env); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 609 | |
| 610 | /* signal CPU creation */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 611 | env->created = 1; |
| 612 | qemu_cond_signal(&qemu_cpu_cond); |
| 613 | |
| 614 | /* and wait for machine initialization */ |
| 615 | while (!qemu_system_ready) |
| 616 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 617 | |
| 618 | while (1) { |
| 619 | if (cpu_can_run(env)) |
| 620 | qemu_cpu_exec(env); |
| 621 | qemu_kvm_wait_io_event(env); |
| 622 | } |
| 623 | |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | static void *tcg_cpu_thread_fn(void *arg) |
| 628 | { |
| 629 | CPUState *env = arg; |
| 630 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 631 | tcg_init_ipi(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 632 | qemu_thread_self(env->thread); |
| 633 | |
| 634 | /* signal CPU creation */ |
| 635 | qemu_mutex_lock(&qemu_global_mutex); |
| 636 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 637 | env->created = 1; |
| 638 | qemu_cond_signal(&qemu_cpu_cond); |
| 639 | |
| 640 | /* and wait for machine initialization */ |
| 641 | while (!qemu_system_ready) |
| 642 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 643 | |
| 644 | while (1) { |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 645 | cpu_exec_all(); |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 646 | qemu_tcg_wait_io_event(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | void qemu_cpu_kick(void *_env) |
| 653 | { |
| 654 | CPUState *env = _env; |
| 655 | qemu_cond_broadcast(env->halt_cond); |
Jan Kiszka | aa2c364 | 2011-02-01 22:15:42 +0100 | [diff] [blame] | 656 | if (!env->thread_kicked) { |
| 657 | qemu_thread_signal(env->thread, SIG_IPI); |
| 658 | env->thread_kicked = true; |
| 659 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | int qemu_cpu_self(void *_env) |
| 663 | { |
| 664 | CPUState *env = _env; |
| 665 | QemuThread this; |
| 666 | |
| 667 | qemu_thread_self(&this); |
| 668 | |
| 669 | return qemu_thread_equal(&this, env->thread); |
| 670 | } |
| 671 | |
| 672 | static void cpu_signal(int sig) |
| 673 | { |
| 674 | if (cpu_single_env) |
| 675 | cpu_exit(cpu_single_env); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 676 | exit_request = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 679 | static void tcg_init_ipi(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 680 | { |
| 681 | sigset_t set; |
| 682 | struct sigaction sigact; |
| 683 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 684 | memset(&sigact, 0, sizeof(sigact)); |
| 685 | sigact.sa_handler = cpu_signal; |
| 686 | sigaction(SIG_IPI, &sigact, NULL); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 687 | |
| 688 | sigemptyset(&set); |
| 689 | sigaddset(&set, SIG_IPI); |
| 690 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static void dummy_signal(int sig) |
| 694 | { |
| 695 | } |
| 696 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 697 | static void kvm_init_ipi(CPUState *env) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 698 | { |
| 699 | int r; |
| 700 | sigset_t set; |
| 701 | struct sigaction sigact; |
| 702 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 703 | memset(&sigact, 0, sizeof(sigact)); |
| 704 | sigact.sa_handler = dummy_signal; |
| 705 | sigaction(SIG_IPI, &sigact, NULL); |
| 706 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 707 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
| 708 | sigdelset(&set, SIG_IPI); |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 709 | sigdelset(&set, SIGBUS); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 710 | r = kvm_set_signal_mask(env, &set); |
| 711 | if (r) { |
| 712 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r)); |
| 713 | exit(1); |
| 714 | } |
| 715 | } |
| 716 | |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 717 | static sigset_t block_io_signals(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 718 | { |
| 719 | sigset_t set; |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 720 | struct sigaction action; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 721 | |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 722 | /* SIGUSR2 used by posix-aio-compat.c */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 723 | sigemptyset(&set); |
| 724 | sigaddset(&set, SIGUSR2); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 725 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 726 | |
| 727 | sigemptyset(&set); |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 728 | sigaddset(&set, SIGIO); |
| 729 | sigaddset(&set, SIGALRM); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 730 | sigaddset(&set, SIG_IPI); |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 731 | sigaddset(&set, SIGBUS); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 732 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 733 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 734 | memset(&action, 0, sizeof(action)); |
| 735 | action.sa_flags = SA_SIGINFO; |
| 736 | action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler; |
| 737 | sigaction(SIGBUS, &action, NULL); |
| 738 | prctl(PR_MCE_KILL, 1, 1, 0, 0); |
| 739 | |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 740 | return set; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 743 | void qemu_mutex_lock_iothread(void) |
| 744 | { |
| 745 | if (kvm_enabled()) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 746 | qemu_mutex_lock(&qemu_global_mutex); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 747 | } else { |
| 748 | qemu_mutex_lock(&qemu_fair_mutex); |
| 749 | if (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 750 | qemu_thread_signal(tcg_cpu_thread, SIG_IPI); |
| 751 | qemu_mutex_lock(&qemu_global_mutex); |
| 752 | } |
| 753 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 754 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | void qemu_mutex_unlock_iothread(void) |
| 758 | { |
| 759 | qemu_mutex_unlock(&qemu_global_mutex); |
| 760 | } |
| 761 | |
| 762 | static int all_vcpus_paused(void) |
| 763 | { |
| 764 | CPUState *penv = first_cpu; |
| 765 | |
| 766 | while (penv) { |
| 767 | if (!penv->stopped) |
| 768 | return 0; |
| 769 | penv = (CPUState *)penv->next_cpu; |
| 770 | } |
| 771 | |
| 772 | return 1; |
| 773 | } |
| 774 | |
| 775 | void pause_all_vcpus(void) |
| 776 | { |
| 777 | CPUState *penv = first_cpu; |
| 778 | |
| 779 | while (penv) { |
| 780 | penv->stop = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 781 | qemu_cpu_kick(penv); |
| 782 | penv = (CPUState *)penv->next_cpu; |
| 783 | } |
| 784 | |
| 785 | while (!all_vcpus_paused()) { |
| 786 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 787 | penv = first_cpu; |
| 788 | while (penv) { |
Marcelo Tosatti | 1fbb22e | 2010-05-04 09:45:21 -0300 | [diff] [blame] | 789 | qemu_cpu_kick(penv); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 790 | penv = (CPUState *)penv->next_cpu; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void resume_all_vcpus(void) |
| 796 | { |
| 797 | CPUState *penv = first_cpu; |
| 798 | |
| 799 | while (penv) { |
| 800 | penv->stop = 0; |
| 801 | penv->stopped = 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 802 | qemu_cpu_kick(penv); |
| 803 | penv = (CPUState *)penv->next_cpu; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | static void tcg_init_vcpu(void *_env) |
| 808 | { |
| 809 | CPUState *env = _env; |
| 810 | /* share a single thread for all cpus with TCG */ |
| 811 | if (!tcg_cpu_thread) { |
| 812 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 813 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 814 | qemu_cond_init(env->halt_cond); |
| 815 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 816 | while (env->created == 0) |
| 817 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 818 | tcg_cpu_thread = env->thread; |
| 819 | tcg_halt_cond = env->halt_cond; |
| 820 | } else { |
| 821 | env->thread = tcg_cpu_thread; |
| 822 | env->halt_cond = tcg_halt_cond; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | static void kvm_start_vcpu(CPUState *env) |
| 827 | { |
| 828 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 829 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 830 | qemu_cond_init(env->halt_cond); |
| 831 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 832 | while (env->created == 0) |
| 833 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 834 | } |
| 835 | |
| 836 | void qemu_init_vcpu(void *_env) |
| 837 | { |
| 838 | CPUState *env = _env; |
| 839 | |
| 840 | env->nr_cores = smp_cores; |
| 841 | env->nr_threads = smp_threads; |
| 842 | if (kvm_enabled()) |
| 843 | kvm_start_vcpu(env); |
| 844 | else |
| 845 | tcg_init_vcpu(env); |
| 846 | } |
| 847 | |
| 848 | void qemu_notify_event(void) |
| 849 | { |
| 850 | qemu_event_increment(); |
| 851 | } |
| 852 | |
| 853 | static void qemu_system_vmstop_request(int reason) |
| 854 | { |
| 855 | vmstop_requested = reason; |
| 856 | qemu_notify_event(); |
| 857 | } |
| 858 | |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 859 | void cpu_stop_current(void) |
| 860 | { |
| 861 | if (cpu_single_env) { |
| 862 | cpu_single_env->stopped = 1; |
| 863 | cpu_exit(cpu_single_env); |
| 864 | } |
| 865 | } |
| 866 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 867 | void vm_stop(int reason) |
| 868 | { |
| 869 | QemuThread me; |
| 870 | qemu_thread_self(&me); |
| 871 | |
| 872 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 873 | qemu_system_vmstop_request(reason); |
| 874 | /* |
| 875 | * FIXME: should not return to device code in case |
| 876 | * vm_stop() has been requested. |
| 877 | */ |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 878 | cpu_stop_current(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | do_vm_stop(reason); |
| 882 | } |
| 883 | |
| 884 | #endif |
| 885 | |
| 886 | static int qemu_cpu_exec(CPUState *env) |
| 887 | { |
| 888 | int ret; |
| 889 | #ifdef CONFIG_PROFILER |
| 890 | int64_t ti; |
| 891 | #endif |
| 892 | |
| 893 | #ifdef CONFIG_PROFILER |
| 894 | ti = profile_getclock(); |
| 895 | #endif |
| 896 | if (use_icount) { |
| 897 | int64_t count; |
| 898 | int decr; |
| 899 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 900 | env->icount_decr.u16.low = 0; |
| 901 | env->icount_extra = 0; |
| 902 | count = qemu_icount_round (qemu_next_deadline()); |
| 903 | qemu_icount += count; |
| 904 | decr = (count > 0xffff) ? 0xffff : count; |
| 905 | count -= decr; |
| 906 | env->icount_decr.u16.low = decr; |
| 907 | env->icount_extra = count; |
| 908 | } |
| 909 | ret = cpu_exec(env); |
| 910 | #ifdef CONFIG_PROFILER |
| 911 | qemu_time += profile_getclock() - ti; |
| 912 | #endif |
| 913 | if (use_icount) { |
| 914 | /* Fold pending instructions back into the |
| 915 | instruction counter, and clear the interrupt flag. */ |
| 916 | qemu_icount -= (env->icount_decr.u16.low |
| 917 | + env->icount_extra); |
| 918 | env->icount_decr.u32 = 0; |
| 919 | env->icount_extra = 0; |
| 920 | } |
| 921 | return ret; |
| 922 | } |
| 923 | |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 924 | bool cpu_exec_all(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 925 | { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 926 | if (next_cpu == NULL) |
| 927 | next_cpu = first_cpu; |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 928 | for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) { |
Jan Kiszka | 345f442 | 2010-06-25 16:56:54 +0200 | [diff] [blame] | 929 | CPUState *env = next_cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 930 | |
| 931 | qemu_clock_enable(vm_clock, |
Jan Kiszka | 345f442 | 2010-06-25 16:56:54 +0200 | [diff] [blame] | 932 | (env->singlestep_enabled & SSTEP_NOTIMER) == 0); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 933 | |
| 934 | if (qemu_alarm_pending()) |
| 935 | break; |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 936 | if (cpu_can_run(env)) { |
| 937 | if (qemu_cpu_exec(env) == EXCP_DEBUG) { |
| 938 | break; |
| 939 | } |
| 940 | } else if (env->stop) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 941 | break; |
| 942 | } |
| 943 | } |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 944 | exit_request = 0; |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 945 | return any_cpu_has_work(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | void set_numa_modes(void) |
| 949 | { |
| 950 | CPUState *env; |
| 951 | int i; |
| 952 | |
| 953 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 954 | for (i = 0; i < nb_numa_nodes; i++) { |
| 955 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 956 | env->numa_node = i; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | void set_cpu_log(const char *optarg) |
| 963 | { |
| 964 | int mask; |
| 965 | const CPULogItem *item; |
| 966 | |
| 967 | mask = cpu_str_to_log_mask(optarg); |
| 968 | if (!mask) { |
| 969 | printf("Log items (comma separated):\n"); |
| 970 | for (item = cpu_log_items; item->mask != 0; item++) { |
| 971 | printf("%-10s %s\n", item->name, item->help); |
| 972 | } |
| 973 | exit(1); |
| 974 | } |
| 975 | cpu_set_log(mask); |
| 976 | } |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 977 | |
| 978 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 979 | int64_t cpu_get_icount(void) |
| 980 | { |
| 981 | int64_t icount; |
| 982 | CPUState *env = cpu_single_env;; |
| 983 | |
| 984 | icount = qemu_icount; |
| 985 | if (env) { |
| 986 | if (!can_do_io(env)) { |
| 987 | fprintf(stderr, "Bad clock read\n"); |
| 988 | } |
| 989 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 990 | } |
| 991 | return qemu_icount_bias + (icount << icount_time_shift); |
| 992 | } |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 993 | |
Stefan Weil | 9a78eea | 2010-10-22 23:03:33 +0200 | [diff] [blame] | 994 | void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 995 | { |
| 996 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 997 | #if defined(cpu_list_id) |
| 998 | cpu_list_id(f, cpu_fprintf, optarg); |
| 999 | #elif defined(cpu_list) |
| 1000 | cpu_list(f, cpu_fprintf); /* deprecated */ |
| 1001 | #endif |
| 1002 | } |