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); |
Jan Kiszka | f8ca7b4 | 2010-06-25 16:56:51 +0200 | [diff] [blame] | 335 | qemu_cond_init(&qemu_system_cond); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 336 | qemu_mutex_init(&qemu_fair_mutex); |
| 337 | qemu_mutex_init(&qemu_global_mutex); |
| 338 | qemu_mutex_lock(&qemu_global_mutex); |
| 339 | |
| 340 | unblock_io_signals(); |
| 341 | qemu_thread_self(&io_thread); |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 346 | void qemu_main_loop_start(void) |
| 347 | { |
| 348 | qemu_system_ready = 1; |
| 349 | qemu_cond_broadcast(&qemu_system_cond); |
| 350 | } |
| 351 | |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 352 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
| 353 | { |
| 354 | struct qemu_work_item wi; |
| 355 | |
| 356 | if (qemu_cpu_self(env)) { |
| 357 | func(data); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | wi.func = func; |
| 362 | wi.data = data; |
| 363 | if (!env->queued_work_first) |
| 364 | env->queued_work_first = &wi; |
| 365 | else |
| 366 | env->queued_work_last->next = &wi; |
| 367 | env->queued_work_last = &wi; |
| 368 | wi.next = NULL; |
| 369 | wi.done = false; |
| 370 | |
| 371 | qemu_cpu_kick(env); |
| 372 | while (!wi.done) { |
| 373 | CPUState *self_env = cpu_single_env; |
| 374 | |
| 375 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); |
| 376 | cpu_single_env = self_env; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | static void flush_queued_work(CPUState *env) |
| 381 | { |
| 382 | struct qemu_work_item *wi; |
| 383 | |
| 384 | if (!env->queued_work_first) |
| 385 | return; |
| 386 | |
| 387 | while ((wi = env->queued_work_first)) { |
| 388 | env->queued_work_first = wi->next; |
| 389 | wi->func(wi->data); |
| 390 | wi->done = true; |
| 391 | } |
| 392 | env->queued_work_last = NULL; |
| 393 | qemu_cond_broadcast(&qemu_work_cond); |
| 394 | } |
| 395 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 396 | static void qemu_wait_io_event_common(CPUState *env) |
| 397 | { |
| 398 | if (env->stop) { |
| 399 | env->stop = 0; |
| 400 | env->stopped = 1; |
| 401 | qemu_cond_signal(&qemu_pause_cond); |
| 402 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 403 | flush_queued_work(env); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame^] | 406 | static void qemu_tcg_wait_io_event(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 407 | { |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame^] | 408 | CPUState *env; |
| 409 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 410 | while (!tcg_has_work()) |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame^] | 411 | qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 412 | |
| 413 | qemu_mutex_unlock(&qemu_global_mutex); |
| 414 | |
| 415 | /* |
| 416 | * Users of qemu_global_mutex can be starved, having no chance |
| 417 | * to acquire it since this path will get to it first. |
| 418 | * So use another lock to provide fairness. |
| 419 | */ |
| 420 | qemu_mutex_lock(&qemu_fair_mutex); |
| 421 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 422 | |
| 423 | qemu_mutex_lock(&qemu_global_mutex); |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame^] | 424 | |
| 425 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 426 | qemu_wait_io_event_common(env); |
| 427 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static void qemu_kvm_eat_signal(CPUState *env, int timeout) |
| 431 | { |
| 432 | struct timespec ts; |
| 433 | int r, e; |
| 434 | siginfo_t siginfo; |
| 435 | sigset_t waitset; |
| 436 | |
| 437 | ts.tv_sec = timeout / 1000; |
| 438 | ts.tv_nsec = (timeout % 1000) * 1000000; |
| 439 | |
| 440 | sigemptyset(&waitset); |
| 441 | sigaddset(&waitset, SIG_IPI); |
| 442 | |
| 443 | qemu_mutex_unlock(&qemu_global_mutex); |
| 444 | r = sigtimedwait(&waitset, &siginfo, &ts); |
| 445 | e = errno; |
| 446 | qemu_mutex_lock(&qemu_global_mutex); |
| 447 | |
| 448 | if (r == -1 && !(e == EAGAIN || e == EINTR)) { |
| 449 | fprintf(stderr, "sigtimedwait: %s\n", strerror(e)); |
| 450 | exit(1); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static void qemu_kvm_wait_io_event(CPUState *env) |
| 455 | { |
| 456 | while (!cpu_has_work(env)) |
| 457 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 458 | |
| 459 | qemu_kvm_eat_signal(env, 0); |
| 460 | qemu_wait_io_event_common(env); |
| 461 | } |
| 462 | |
| 463 | static int qemu_cpu_exec(CPUState *env); |
| 464 | |
| 465 | static void *kvm_cpu_thread_fn(void *arg) |
| 466 | { |
| 467 | CPUState *env = arg; |
| 468 | |
Marcelo Tosatti | 6164e6d | 2010-03-23 13:37:13 -0300 | [diff] [blame] | 469 | qemu_mutex_lock(&qemu_global_mutex); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 470 | qemu_thread_self(env->thread); |
| 471 | if (kvm_enabled()) |
| 472 | kvm_init_vcpu(env); |
| 473 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 474 | kvm_init_ipi(env); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 475 | |
| 476 | /* signal CPU creation */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 477 | env->created = 1; |
| 478 | qemu_cond_signal(&qemu_cpu_cond); |
| 479 | |
| 480 | /* and wait for machine initialization */ |
| 481 | while (!qemu_system_ready) |
| 482 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 483 | |
| 484 | while (1) { |
| 485 | if (cpu_can_run(env)) |
| 486 | qemu_cpu_exec(env); |
| 487 | qemu_kvm_wait_io_event(env); |
| 488 | } |
| 489 | |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | static void *tcg_cpu_thread_fn(void *arg) |
| 494 | { |
| 495 | CPUState *env = arg; |
| 496 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 497 | tcg_init_ipi(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 498 | qemu_thread_self(env->thread); |
| 499 | |
| 500 | /* signal CPU creation */ |
| 501 | qemu_mutex_lock(&qemu_global_mutex); |
| 502 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 503 | env->created = 1; |
| 504 | qemu_cond_signal(&qemu_cpu_cond); |
| 505 | |
| 506 | /* and wait for machine initialization */ |
| 507 | while (!qemu_system_ready) |
| 508 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 509 | |
| 510 | while (1) { |
| 511 | tcg_cpu_exec(); |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame^] | 512 | qemu_tcg_wait_io_event(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | void qemu_cpu_kick(void *_env) |
| 519 | { |
| 520 | CPUState *env = _env; |
| 521 | qemu_cond_broadcast(env->halt_cond); |
Marcelo Tosatti | 1fbb22e | 2010-05-04 09:45:21 -0300 | [diff] [blame] | 522 | qemu_thread_signal(env->thread, SIG_IPI); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | int qemu_cpu_self(void *_env) |
| 526 | { |
| 527 | CPUState *env = _env; |
| 528 | QemuThread this; |
| 529 | |
| 530 | qemu_thread_self(&this); |
| 531 | |
| 532 | return qemu_thread_equal(&this, env->thread); |
| 533 | } |
| 534 | |
| 535 | static void cpu_signal(int sig) |
| 536 | { |
| 537 | if (cpu_single_env) |
| 538 | cpu_exit(cpu_single_env); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 539 | exit_request = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 542 | static void tcg_init_ipi(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 543 | { |
| 544 | sigset_t set; |
| 545 | struct sigaction sigact; |
| 546 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 547 | memset(&sigact, 0, sizeof(sigact)); |
| 548 | sigact.sa_handler = cpu_signal; |
| 549 | sigaction(SIG_IPI, &sigact, NULL); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 550 | |
| 551 | sigemptyset(&set); |
| 552 | sigaddset(&set, SIG_IPI); |
| 553 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static void dummy_signal(int sig) |
| 557 | { |
| 558 | } |
| 559 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 560 | static void kvm_init_ipi(CPUState *env) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 561 | { |
| 562 | int r; |
| 563 | sigset_t set; |
| 564 | struct sigaction sigact; |
| 565 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 566 | memset(&sigact, 0, sizeof(sigact)); |
| 567 | sigact.sa_handler = dummy_signal; |
| 568 | sigaction(SIG_IPI, &sigact, NULL); |
| 569 | |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 570 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
| 571 | sigdelset(&set, SIG_IPI); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 572 | r = kvm_set_signal_mask(env, &set); |
| 573 | if (r) { |
| 574 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r)); |
| 575 | exit(1); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static void unblock_io_signals(void) |
| 580 | { |
| 581 | sigset_t set; |
| 582 | |
| 583 | sigemptyset(&set); |
| 584 | sigaddset(&set, SIGUSR2); |
| 585 | sigaddset(&set, SIGIO); |
| 586 | sigaddset(&set, SIGALRM); |
| 587 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 588 | |
| 589 | sigemptyset(&set); |
| 590 | sigaddset(&set, SIG_IPI); |
| 591 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 592 | } |
| 593 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 594 | void qemu_mutex_lock_iothread(void) |
| 595 | { |
| 596 | if (kvm_enabled()) { |
| 597 | qemu_mutex_lock(&qemu_fair_mutex); |
| 598 | qemu_mutex_lock(&qemu_global_mutex); |
| 599 | qemu_mutex_unlock(&qemu_fair_mutex); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 600 | } else { |
| 601 | qemu_mutex_lock(&qemu_fair_mutex); |
| 602 | if (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 603 | qemu_thread_signal(tcg_cpu_thread, SIG_IPI); |
| 604 | qemu_mutex_lock(&qemu_global_mutex); |
| 605 | } |
| 606 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 607 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void qemu_mutex_unlock_iothread(void) |
| 611 | { |
| 612 | qemu_mutex_unlock(&qemu_global_mutex); |
| 613 | } |
| 614 | |
| 615 | static int all_vcpus_paused(void) |
| 616 | { |
| 617 | CPUState *penv = first_cpu; |
| 618 | |
| 619 | while (penv) { |
| 620 | if (!penv->stopped) |
| 621 | return 0; |
| 622 | penv = (CPUState *)penv->next_cpu; |
| 623 | } |
| 624 | |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | void pause_all_vcpus(void) |
| 629 | { |
| 630 | CPUState *penv = first_cpu; |
| 631 | |
| 632 | while (penv) { |
| 633 | penv->stop = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 634 | qemu_cpu_kick(penv); |
| 635 | penv = (CPUState *)penv->next_cpu; |
| 636 | } |
| 637 | |
| 638 | while (!all_vcpus_paused()) { |
| 639 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 640 | penv = first_cpu; |
| 641 | while (penv) { |
Marcelo Tosatti | 1fbb22e | 2010-05-04 09:45:21 -0300 | [diff] [blame] | 642 | qemu_cpu_kick(penv); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 643 | penv = (CPUState *)penv->next_cpu; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | void resume_all_vcpus(void) |
| 649 | { |
| 650 | CPUState *penv = first_cpu; |
| 651 | |
| 652 | while (penv) { |
| 653 | penv->stop = 0; |
| 654 | penv->stopped = 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 655 | qemu_cpu_kick(penv); |
| 656 | penv = (CPUState *)penv->next_cpu; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | static void tcg_init_vcpu(void *_env) |
| 661 | { |
| 662 | CPUState *env = _env; |
| 663 | /* share a single thread for all cpus with TCG */ |
| 664 | if (!tcg_cpu_thread) { |
| 665 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 666 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 667 | qemu_cond_init(env->halt_cond); |
| 668 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 669 | while (env->created == 0) |
| 670 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 671 | tcg_cpu_thread = env->thread; |
| 672 | tcg_halt_cond = env->halt_cond; |
| 673 | } else { |
| 674 | env->thread = tcg_cpu_thread; |
| 675 | env->halt_cond = tcg_halt_cond; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static void kvm_start_vcpu(CPUState *env) |
| 680 | { |
| 681 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 682 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 683 | qemu_cond_init(env->halt_cond); |
| 684 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 685 | while (env->created == 0) |
| 686 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 687 | } |
| 688 | |
| 689 | void qemu_init_vcpu(void *_env) |
| 690 | { |
| 691 | CPUState *env = _env; |
| 692 | |
| 693 | env->nr_cores = smp_cores; |
| 694 | env->nr_threads = smp_threads; |
| 695 | if (kvm_enabled()) |
| 696 | kvm_start_vcpu(env); |
| 697 | else |
| 698 | tcg_init_vcpu(env); |
| 699 | } |
| 700 | |
| 701 | void qemu_notify_event(void) |
| 702 | { |
| 703 | qemu_event_increment(); |
| 704 | } |
| 705 | |
| 706 | static void qemu_system_vmstop_request(int reason) |
| 707 | { |
| 708 | vmstop_requested = reason; |
| 709 | qemu_notify_event(); |
| 710 | } |
| 711 | |
| 712 | void vm_stop(int reason) |
| 713 | { |
| 714 | QemuThread me; |
| 715 | qemu_thread_self(&me); |
| 716 | |
| 717 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 718 | qemu_system_vmstop_request(reason); |
| 719 | /* |
| 720 | * FIXME: should not return to device code in case |
| 721 | * vm_stop() has been requested. |
| 722 | */ |
| 723 | if (cpu_single_env) { |
| 724 | cpu_exit(cpu_single_env); |
| 725 | cpu_single_env->stop = 1; |
| 726 | } |
| 727 | return; |
| 728 | } |
| 729 | do_vm_stop(reason); |
| 730 | } |
| 731 | |
| 732 | #endif |
| 733 | |
| 734 | static int qemu_cpu_exec(CPUState *env) |
| 735 | { |
| 736 | int ret; |
| 737 | #ifdef CONFIG_PROFILER |
| 738 | int64_t ti; |
| 739 | #endif |
| 740 | |
| 741 | #ifdef CONFIG_PROFILER |
| 742 | ti = profile_getclock(); |
| 743 | #endif |
| 744 | if (use_icount) { |
| 745 | int64_t count; |
| 746 | int decr; |
| 747 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 748 | env->icount_decr.u16.low = 0; |
| 749 | env->icount_extra = 0; |
| 750 | count = qemu_icount_round (qemu_next_deadline()); |
| 751 | qemu_icount += count; |
| 752 | decr = (count > 0xffff) ? 0xffff : count; |
| 753 | count -= decr; |
| 754 | env->icount_decr.u16.low = decr; |
| 755 | env->icount_extra = count; |
| 756 | } |
| 757 | ret = cpu_exec(env); |
| 758 | #ifdef CONFIG_PROFILER |
| 759 | qemu_time += profile_getclock() - ti; |
| 760 | #endif |
| 761 | if (use_icount) { |
| 762 | /* Fold pending instructions back into the |
| 763 | instruction counter, and clear the interrupt flag. */ |
| 764 | qemu_icount -= (env->icount_decr.u16.low |
| 765 | + env->icount_extra); |
| 766 | env->icount_decr.u32 = 0; |
| 767 | env->icount_extra = 0; |
| 768 | } |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | bool tcg_cpu_exec(void) |
| 773 | { |
| 774 | int ret = 0; |
| 775 | |
| 776 | if (next_cpu == NULL) |
| 777 | next_cpu = first_cpu; |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 778 | for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 779 | CPUState *env = cur_cpu = next_cpu; |
| 780 | |
| 781 | qemu_clock_enable(vm_clock, |
| 782 | (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); |
| 783 | |
| 784 | if (qemu_alarm_pending()) |
| 785 | break; |
| 786 | if (cpu_can_run(env)) |
| 787 | ret = qemu_cpu_exec(env); |
| 788 | else if (env->stop) |
| 789 | break; |
| 790 | |
| 791 | if (ret == EXCP_DEBUG) { |
| 792 | gdb_set_stop_cpu(env); |
| 793 | debug_requested = EXCP_DEBUG; |
| 794 | break; |
| 795 | } |
| 796 | } |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 797 | exit_request = 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 798 | return tcg_has_work(); |
| 799 | } |
| 800 | |
| 801 | void set_numa_modes(void) |
| 802 | { |
| 803 | CPUState *env; |
| 804 | int i; |
| 805 | |
| 806 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 807 | for (i = 0; i < nb_numa_nodes; i++) { |
| 808 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 809 | env->numa_node = i; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | void set_cpu_log(const char *optarg) |
| 816 | { |
| 817 | int mask; |
| 818 | const CPULogItem *item; |
| 819 | |
| 820 | mask = cpu_str_to_log_mask(optarg); |
| 821 | if (!mask) { |
| 822 | printf("Log items (comma separated):\n"); |
| 823 | for (item = cpu_log_items; item->mask != 0; item++) { |
| 824 | printf("%-10s %s\n", item->name, item->help); |
| 825 | } |
| 826 | exit(1); |
| 827 | } |
| 828 | cpu_set_log(mask); |
| 829 | } |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 830 | |
| 831 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 832 | int64_t cpu_get_icount(void) |
| 833 | { |
| 834 | int64_t icount; |
| 835 | CPUState *env = cpu_single_env;; |
| 836 | |
| 837 | icount = qemu_icount; |
| 838 | if (env) { |
| 839 | if (!can_do_io(env)) { |
| 840 | fprintf(stderr, "Bad clock read\n"); |
| 841 | } |
| 842 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 843 | } |
| 844 | return qemu_icount_bias + (icount << icount_time_shift); |
| 845 | } |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 846 | |
| 847 | void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...), |
| 848 | const char *optarg) |
| 849 | { |
| 850 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 851 | #if defined(cpu_list_id) |
| 852 | cpu_list_id(f, cpu_fprintf, optarg); |
| 853 | #elif defined(cpu_list) |
| 854 | cpu_list(f, cpu_fprintf); /* deprecated */ |
| 855 | #endif |
| 856 | } |