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