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 | } |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 225 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 226 | static void dummy_signal(int sig) |
| 227 | { |
| 228 | } |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 229 | |
Jan Kiszka | d0f294c | 2011-02-01 22:15:56 +0100 | [diff] [blame^] | 230 | /* If we have signalfd, we mask out the signals we want to handle and then |
| 231 | * use signalfd to listen for them. We rely on whatever the current signal |
| 232 | * handler is to dispatch the signals when we receive them. |
| 233 | */ |
| 234 | static void sigfd_handler(void *opaque) |
| 235 | { |
| 236 | int fd = (unsigned long) opaque; |
| 237 | struct qemu_signalfd_siginfo info; |
| 238 | struct sigaction action; |
| 239 | ssize_t len; |
| 240 | |
| 241 | while (1) { |
| 242 | do { |
| 243 | len = read(fd, &info, sizeof(info)); |
| 244 | } while (len == -1 && errno == EINTR); |
| 245 | |
| 246 | if (len == -1 && errno == EAGAIN) { |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | if (len != sizeof(info)) { |
| 251 | printf("read from sigfd returned %zd: %m\n", len); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | sigaction(info.ssi_signo, NULL, &action); |
| 256 | if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) { |
| 257 | action.sa_sigaction(info.ssi_signo, |
| 258 | (siginfo_t *)&info, NULL); |
| 259 | } else if (action.sa_handler) { |
| 260 | action.sa_handler(info.ssi_signo); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static int qemu_signalfd_init(sigset_t mask) |
| 266 | { |
| 267 | int sigfd; |
| 268 | |
| 269 | sigfd = qemu_signalfd(&mask); |
| 270 | if (sigfd == -1) { |
| 271 | fprintf(stderr, "failed to create signalfd\n"); |
| 272 | return -errno; |
| 273 | } |
| 274 | |
| 275 | fcntl_setfl(sigfd, O_NONBLOCK); |
| 276 | |
| 277 | qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL, |
| 278 | (void *)(unsigned long) sigfd); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 283 | static void sigbus_reraise(void); |
| 284 | |
| 285 | static void qemu_kvm_eat_signals(CPUState *env) |
| 286 | { |
| 287 | struct timespec ts = { 0, 0 }; |
| 288 | siginfo_t siginfo; |
| 289 | sigset_t waitset; |
| 290 | sigset_t chkset; |
| 291 | int r; |
| 292 | |
| 293 | sigemptyset(&waitset); |
| 294 | sigaddset(&waitset, SIG_IPI); |
| 295 | sigaddset(&waitset, SIGBUS); |
| 296 | |
| 297 | do { |
| 298 | r = sigtimedwait(&waitset, &siginfo, &ts); |
| 299 | if (r == -1 && !(errno == EAGAIN || errno == EINTR)) { |
| 300 | perror("sigtimedwait"); |
| 301 | exit(1); |
| 302 | } |
| 303 | |
| 304 | switch (r) { |
| 305 | #ifdef CONFIG_IOTHREAD |
| 306 | case SIGBUS: |
| 307 | if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) { |
| 308 | sigbus_reraise(); |
| 309 | } |
| 310 | break; |
| 311 | #endif |
| 312 | default: |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | r = sigpending(&chkset); |
| 317 | if (r == -1) { |
| 318 | perror("sigpending"); |
| 319 | exit(1); |
| 320 | } |
| 321 | } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS)); |
| 322 | } |
| 323 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 324 | #else /* _WIN32 */ |
| 325 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 326 | HANDLE qemu_event_handle; |
| 327 | |
| 328 | static void dummy_event_handler(void *opaque) |
| 329 | { |
| 330 | } |
| 331 | |
| 332 | static int qemu_event_init(void) |
| 333 | { |
| 334 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 335 | if (!qemu_event_handle) { |
| 336 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
| 337 | return -1; |
| 338 | } |
| 339 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static void qemu_event_increment(void) |
| 344 | { |
| 345 | if (!SetEvent(qemu_event_handle)) { |
| 346 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", |
| 347 | GetLastError()); |
| 348 | exit (1); |
| 349 | } |
| 350 | } |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 351 | |
| 352 | static void qemu_kvm_eat_signals(CPUState *env) |
| 353 | { |
| 354 | } |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 355 | #endif /* _WIN32 */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 356 | |
| 357 | #ifndef CONFIG_IOTHREAD |
Jan Kiszka | ff48eb5 | 2011-02-01 22:15:53 +0100 | [diff] [blame] | 358 | static void qemu_kvm_init_cpu_signals(CPUState *env) |
| 359 | { |
| 360 | #ifndef _WIN32 |
| 361 | int r; |
| 362 | sigset_t set; |
| 363 | struct sigaction sigact; |
| 364 | |
| 365 | memset(&sigact, 0, sizeof(sigact)); |
| 366 | sigact.sa_handler = dummy_signal; |
| 367 | sigaction(SIG_IPI, &sigact, NULL); |
| 368 | |
| 369 | sigemptyset(&set); |
| 370 | sigaddset(&set, SIG_IPI); |
| 371 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 372 | |
| 373 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
| 374 | sigdelset(&set, SIG_IPI); |
| 375 | sigdelset(&set, SIGBUS); |
| 376 | r = kvm_set_signal_mask(env, &set); |
| 377 | if (r) { |
| 378 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); |
| 379 | exit(1); |
| 380 | } |
| 381 | #endif |
| 382 | } |
| 383 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 384 | int qemu_init_main_loop(void) |
| 385 | { |
Jan Kiszka | d0f294c | 2011-02-01 22:15:56 +0100 | [diff] [blame^] | 386 | #ifndef _WIN32 |
| 387 | sigset_t blocked_signals; |
| 388 | int ret; |
| 389 | |
| 390 | sigemptyset(&blocked_signals); |
| 391 | |
| 392 | ret = qemu_signalfd_init(blocked_signals); |
| 393 | if (ret) { |
| 394 | return ret; |
| 395 | } |
| 396 | #endif |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 397 | cpu_set_debug_excp_handler(cpu_debug_handler); |
| 398 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 399 | return qemu_event_init(); |
| 400 | } |
| 401 | |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 402 | void qemu_main_loop_start(void) |
| 403 | { |
| 404 | } |
| 405 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 406 | void qemu_init_vcpu(void *_env) |
| 407 | { |
| 408 | CPUState *env = _env; |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 409 | int r; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 410 | |
| 411 | env->nr_cores = smp_cores; |
| 412 | env->nr_threads = smp_threads; |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 413 | |
| 414 | if (kvm_enabled()) { |
| 415 | r = kvm_init_vcpu(env); |
| 416 | if (r < 0) { |
| 417 | fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r)); |
| 418 | exit(1); |
| 419 | } |
Jan Kiszka | ff48eb5 | 2011-02-01 22:15:53 +0100 | [diff] [blame] | 420 | qemu_kvm_init_cpu_signals(env); |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 421 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | int qemu_cpu_self(void *env) |
| 425 | { |
| 426 | return 1; |
| 427 | } |
| 428 | |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 429 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
| 430 | { |
| 431 | func(data); |
| 432 | } |
| 433 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 434 | void resume_all_vcpus(void) |
| 435 | { |
| 436 | } |
| 437 | |
| 438 | void pause_all_vcpus(void) |
| 439 | { |
| 440 | } |
| 441 | |
| 442 | void qemu_cpu_kick(void *env) |
| 443 | { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | void qemu_notify_event(void) |
| 448 | { |
| 449 | CPUState *env = cpu_single_env; |
| 450 | |
| 451 | qemu_event_increment (); |
| 452 | if (env) { |
| 453 | cpu_exit(env); |
| 454 | } |
| 455 | if (next_cpu && env != next_cpu) { |
| 456 | cpu_exit(next_cpu); |
| 457 | } |
Jan Kiszka | 38145df | 2011-02-01 22:15:45 +0100 | [diff] [blame] | 458 | exit_request = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void qemu_mutex_lock_iothread(void) {} |
| 462 | void qemu_mutex_unlock_iothread(void) {} |
| 463 | |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 464 | void cpu_stop_current(void) |
| 465 | { |
| 466 | } |
| 467 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 468 | void vm_stop(int reason) |
| 469 | { |
| 470 | do_vm_stop(reason); |
| 471 | } |
| 472 | |
| 473 | #else /* CONFIG_IOTHREAD */ |
| 474 | |
| 475 | #include "qemu-thread.h" |
| 476 | |
| 477 | QemuMutex qemu_global_mutex; |
| 478 | static QemuMutex qemu_fair_mutex; |
| 479 | |
| 480 | static QemuThread io_thread; |
| 481 | |
| 482 | static QemuThread *tcg_cpu_thread; |
| 483 | static QemuCond *tcg_halt_cond; |
| 484 | |
| 485 | static int qemu_system_ready; |
| 486 | /* cpu creation */ |
| 487 | static QemuCond qemu_cpu_cond; |
| 488 | /* system init */ |
| 489 | static QemuCond qemu_system_cond; |
| 490 | static QemuCond qemu_pause_cond; |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 491 | static QemuCond qemu_work_cond; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 492 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 493 | static void cpu_signal(int sig) |
| 494 | { |
| 495 | if (cpu_single_env) { |
| 496 | cpu_exit(cpu_single_env); |
| 497 | } |
| 498 | exit_request = 1; |
| 499 | } |
| 500 | |
| 501 | static void qemu_kvm_init_cpu_signals(CPUState *env) |
| 502 | { |
| 503 | int r; |
| 504 | sigset_t set; |
| 505 | struct sigaction sigact; |
| 506 | |
| 507 | memset(&sigact, 0, sizeof(sigact)); |
| 508 | sigact.sa_handler = dummy_signal; |
| 509 | sigaction(SIG_IPI, &sigact, NULL); |
| 510 | |
| 511 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
| 512 | sigdelset(&set, SIG_IPI); |
| 513 | sigdelset(&set, SIGBUS); |
| 514 | r = kvm_set_signal_mask(env, &set); |
| 515 | if (r) { |
| 516 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); |
| 517 | exit(1); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static void qemu_tcg_init_cpu_signals(void) |
| 522 | { |
| 523 | sigset_t set; |
| 524 | struct sigaction sigact; |
| 525 | |
| 526 | memset(&sigact, 0, sizeof(sigact)); |
| 527 | sigact.sa_handler = cpu_signal; |
| 528 | sigaction(SIG_IPI, &sigact, NULL); |
| 529 | |
| 530 | sigemptyset(&set); |
| 531 | sigaddset(&set, SIG_IPI); |
| 532 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 533 | } |
| 534 | |
| 535 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, |
| 536 | void *ctx); |
| 537 | |
| 538 | static sigset_t block_io_signals(void) |
| 539 | { |
| 540 | sigset_t set; |
| 541 | struct sigaction action; |
| 542 | |
| 543 | /* SIGUSR2 used by posix-aio-compat.c */ |
| 544 | sigemptyset(&set); |
| 545 | sigaddset(&set, SIGUSR2); |
| 546 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 547 | |
| 548 | sigemptyset(&set); |
| 549 | sigaddset(&set, SIGIO); |
| 550 | sigaddset(&set, SIGALRM); |
| 551 | sigaddset(&set, SIG_IPI); |
| 552 | sigaddset(&set, SIGBUS); |
| 553 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 554 | |
| 555 | memset(&action, 0, sizeof(action)); |
| 556 | action.sa_flags = SA_SIGINFO; |
| 557 | action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler; |
| 558 | sigaction(SIGBUS, &action, NULL); |
| 559 | prctl(PR_MCE_KILL, 1, 1, 0, 0); |
| 560 | |
| 561 | return set; |
| 562 | } |
| 563 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 564 | int qemu_init_main_loop(void) |
| 565 | { |
| 566 | int ret; |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 567 | sigset_t blocked_signals; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 568 | |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 569 | cpu_set_debug_excp_handler(cpu_debug_handler); |
| 570 | |
Marcelo Tosatti | a8486bc | 2010-10-11 15:31:16 -0300 | [diff] [blame] | 571 | blocked_signals = block_io_signals(); |
| 572 | |
| 573 | ret = qemu_signalfd_init(blocked_signals); |
| 574 | if (ret) |
| 575 | return ret; |
| 576 | |
| 577 | /* Note eventfd must be drained before signalfd handlers run */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 578 | ret = qemu_event_init(); |
| 579 | if (ret) |
| 580 | return ret; |
| 581 | |
| 582 | qemu_cond_init(&qemu_pause_cond); |
Jan Kiszka | f8ca7b4 | 2010-06-25 16:56:51 +0200 | [diff] [blame] | 583 | qemu_cond_init(&qemu_system_cond); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 584 | qemu_mutex_init(&qemu_fair_mutex); |
| 585 | qemu_mutex_init(&qemu_global_mutex); |
| 586 | qemu_mutex_lock(&qemu_global_mutex); |
| 587 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 588 | qemu_thread_self(&io_thread); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
Blue Swirl | 7277e02 | 2010-04-12 17:19:06 +0000 | [diff] [blame] | 593 | void qemu_main_loop_start(void) |
| 594 | { |
| 595 | qemu_system_ready = 1; |
| 596 | qemu_cond_broadcast(&qemu_system_cond); |
| 597 | } |
| 598 | |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 599 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
| 600 | { |
| 601 | struct qemu_work_item wi; |
| 602 | |
| 603 | if (qemu_cpu_self(env)) { |
| 604 | func(data); |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | wi.func = func; |
| 609 | wi.data = data; |
| 610 | if (!env->queued_work_first) |
| 611 | env->queued_work_first = &wi; |
| 612 | else |
| 613 | env->queued_work_last->next = &wi; |
| 614 | env->queued_work_last = &wi; |
| 615 | wi.next = NULL; |
| 616 | wi.done = false; |
| 617 | |
| 618 | qemu_cpu_kick(env); |
| 619 | while (!wi.done) { |
| 620 | CPUState *self_env = cpu_single_env; |
| 621 | |
| 622 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); |
| 623 | cpu_single_env = self_env; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | static void flush_queued_work(CPUState *env) |
| 628 | { |
| 629 | struct qemu_work_item *wi; |
| 630 | |
| 631 | if (!env->queued_work_first) |
| 632 | return; |
| 633 | |
| 634 | while ((wi = env->queued_work_first)) { |
| 635 | env->queued_work_first = wi->next; |
| 636 | wi->func(wi->data); |
| 637 | wi->done = true; |
| 638 | } |
| 639 | env->queued_work_last = NULL; |
| 640 | qemu_cond_broadcast(&qemu_work_cond); |
| 641 | } |
| 642 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 643 | static void qemu_wait_io_event_common(CPUState *env) |
| 644 | { |
| 645 | if (env->stop) { |
| 646 | env->stop = 0; |
| 647 | env->stopped = 1; |
| 648 | qemu_cond_signal(&qemu_pause_cond); |
| 649 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 650 | flush_queued_work(env); |
Jan Kiszka | aa2c364 | 2011-02-01 22:15:42 +0100 | [diff] [blame] | 651 | env->thread_kicked = false; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 654 | static void qemu_tcg_wait_io_event(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 655 | { |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 656 | CPUState *env; |
| 657 | |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 658 | while (!any_cpu_has_work()) |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 659 | qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 660 | |
| 661 | qemu_mutex_unlock(&qemu_global_mutex); |
| 662 | |
| 663 | /* |
| 664 | * Users of qemu_global_mutex can be starved, having no chance |
| 665 | * to acquire it since this path will get to it first. |
| 666 | * So use another lock to provide fairness. |
| 667 | */ |
| 668 | qemu_mutex_lock(&qemu_fair_mutex); |
| 669 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 670 | |
| 671 | qemu_mutex_lock(&qemu_global_mutex); |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 672 | |
| 673 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 674 | qemu_wait_io_event_common(env); |
| 675 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 678 | static void sigbus_reraise(void) |
| 679 | { |
| 680 | sigset_t set; |
| 681 | struct sigaction action; |
| 682 | |
| 683 | memset(&action, 0, sizeof(action)); |
| 684 | action.sa_handler = SIG_DFL; |
| 685 | if (!sigaction(SIGBUS, &action, NULL)) { |
| 686 | raise(SIGBUS); |
| 687 | sigemptyset(&set); |
| 688 | sigaddset(&set, SIGBUS); |
| 689 | sigprocmask(SIG_UNBLOCK, &set, NULL); |
| 690 | } |
| 691 | perror("Failed to re-raise SIGBUS!\n"); |
| 692 | abort(); |
| 693 | } |
| 694 | |
| 695 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, |
| 696 | void *ctx) |
| 697 | { |
Jan Kiszka | a1b87fe | 2011-02-01 22:15:51 +0100 | [diff] [blame] | 698 | if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) { |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 699 | sigbus_reraise(); |
Jan Kiszka | a1b87fe | 2011-02-01 22:15:51 +0100 | [diff] [blame] | 700 | } |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 701 | } |
| 702 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 703 | static void qemu_kvm_wait_io_event(CPUState *env) |
| 704 | { |
| 705 | while (!cpu_has_work(env)) |
| 706 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 707 | |
Jan Kiszka | 5db5bda | 2011-02-01 22:15:54 +0100 | [diff] [blame] | 708 | qemu_kvm_eat_signals(env); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 709 | qemu_wait_io_event_common(env); |
| 710 | } |
| 711 | |
| 712 | static int qemu_cpu_exec(CPUState *env); |
| 713 | |
| 714 | static void *kvm_cpu_thread_fn(void *arg) |
| 715 | { |
| 716 | CPUState *env = arg; |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 717 | int r; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 718 | |
Marcelo Tosatti | 6164e6d | 2010-03-23 13:37:13 -0300 | [diff] [blame] | 719 | qemu_mutex_lock(&qemu_global_mutex); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 720 | qemu_thread_self(env->thread); |
Jan Kiszka | d31ae05 | 2011-02-01 22:15:49 +0100 | [diff] [blame] | 721 | |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 722 | r = kvm_init_vcpu(env); |
| 723 | if (r < 0) { |
| 724 | fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r)); |
| 725 | exit(1); |
| 726 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 727 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 728 | qemu_kvm_init_cpu_signals(env); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 729 | |
| 730 | /* signal CPU creation */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 731 | env->created = 1; |
| 732 | qemu_cond_signal(&qemu_cpu_cond); |
| 733 | |
| 734 | /* and wait for machine initialization */ |
| 735 | while (!qemu_system_ready) |
| 736 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 737 | |
| 738 | while (1) { |
| 739 | if (cpu_can_run(env)) |
| 740 | qemu_cpu_exec(env); |
| 741 | qemu_kvm_wait_io_event(env); |
| 742 | } |
| 743 | |
| 744 | return NULL; |
| 745 | } |
| 746 | |
| 747 | static void *tcg_cpu_thread_fn(void *arg) |
| 748 | { |
| 749 | CPUState *env = arg; |
| 750 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 751 | qemu_tcg_init_cpu_signals(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 752 | qemu_thread_self(env->thread); |
| 753 | |
| 754 | /* signal CPU creation */ |
| 755 | qemu_mutex_lock(&qemu_global_mutex); |
| 756 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 757 | env->created = 1; |
| 758 | qemu_cond_signal(&qemu_cpu_cond); |
| 759 | |
| 760 | /* and wait for machine initialization */ |
| 761 | while (!qemu_system_ready) |
| 762 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 763 | |
| 764 | while (1) { |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 765 | cpu_exec_all(); |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 766 | qemu_tcg_wait_io_event(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | void qemu_cpu_kick(void *_env) |
| 773 | { |
| 774 | CPUState *env = _env; |
| 775 | qemu_cond_broadcast(env->halt_cond); |
Jan Kiszka | aa2c364 | 2011-02-01 22:15:42 +0100 | [diff] [blame] | 776 | if (!env->thread_kicked) { |
| 777 | qemu_thread_signal(env->thread, SIG_IPI); |
| 778 | env->thread_kicked = true; |
| 779 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | int qemu_cpu_self(void *_env) |
| 783 | { |
| 784 | CPUState *env = _env; |
| 785 | QemuThread this; |
| 786 | |
| 787 | qemu_thread_self(&this); |
| 788 | |
| 789 | return qemu_thread_equal(&this, env->thread); |
| 790 | } |
| 791 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 792 | void qemu_mutex_lock_iothread(void) |
| 793 | { |
| 794 | if (kvm_enabled()) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 795 | qemu_mutex_lock(&qemu_global_mutex); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 796 | } else { |
| 797 | qemu_mutex_lock(&qemu_fair_mutex); |
| 798 | if (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 799 | qemu_thread_signal(tcg_cpu_thread, SIG_IPI); |
| 800 | qemu_mutex_lock(&qemu_global_mutex); |
| 801 | } |
| 802 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 803 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | void qemu_mutex_unlock_iothread(void) |
| 807 | { |
| 808 | qemu_mutex_unlock(&qemu_global_mutex); |
| 809 | } |
| 810 | |
| 811 | static int all_vcpus_paused(void) |
| 812 | { |
| 813 | CPUState *penv = first_cpu; |
| 814 | |
| 815 | while (penv) { |
| 816 | if (!penv->stopped) |
| 817 | return 0; |
| 818 | penv = (CPUState *)penv->next_cpu; |
| 819 | } |
| 820 | |
| 821 | return 1; |
| 822 | } |
| 823 | |
| 824 | void pause_all_vcpus(void) |
| 825 | { |
| 826 | CPUState *penv = first_cpu; |
| 827 | |
| 828 | while (penv) { |
| 829 | penv->stop = 1; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 830 | qemu_cpu_kick(penv); |
| 831 | penv = (CPUState *)penv->next_cpu; |
| 832 | } |
| 833 | |
| 834 | while (!all_vcpus_paused()) { |
| 835 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 836 | penv = first_cpu; |
| 837 | while (penv) { |
Marcelo Tosatti | 1fbb22e | 2010-05-04 09:45:21 -0300 | [diff] [blame] | 838 | qemu_cpu_kick(penv); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 839 | penv = (CPUState *)penv->next_cpu; |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | void resume_all_vcpus(void) |
| 845 | { |
| 846 | CPUState *penv = first_cpu; |
| 847 | |
| 848 | while (penv) { |
| 849 | penv->stop = 0; |
| 850 | penv->stopped = 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 851 | qemu_cpu_kick(penv); |
| 852 | penv = (CPUState *)penv->next_cpu; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | static void tcg_init_vcpu(void *_env) |
| 857 | { |
| 858 | CPUState *env = _env; |
| 859 | /* share a single thread for all cpus with TCG */ |
| 860 | if (!tcg_cpu_thread) { |
| 861 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 862 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 863 | qemu_cond_init(env->halt_cond); |
| 864 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 865 | while (env->created == 0) |
| 866 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 867 | tcg_cpu_thread = env->thread; |
| 868 | tcg_halt_cond = env->halt_cond; |
| 869 | } else { |
| 870 | env->thread = tcg_cpu_thread; |
| 871 | env->halt_cond = tcg_halt_cond; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | static void kvm_start_vcpu(CPUState *env) |
| 876 | { |
| 877 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 878 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 879 | qemu_cond_init(env->halt_cond); |
| 880 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 881 | while (env->created == 0) |
| 882 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 883 | } |
| 884 | |
| 885 | void qemu_init_vcpu(void *_env) |
| 886 | { |
| 887 | CPUState *env = _env; |
| 888 | |
| 889 | env->nr_cores = smp_cores; |
| 890 | env->nr_threads = smp_threads; |
| 891 | if (kvm_enabled()) |
| 892 | kvm_start_vcpu(env); |
| 893 | else |
| 894 | tcg_init_vcpu(env); |
| 895 | } |
| 896 | |
| 897 | void qemu_notify_event(void) |
| 898 | { |
| 899 | qemu_event_increment(); |
| 900 | } |
| 901 | |
| 902 | static void qemu_system_vmstop_request(int reason) |
| 903 | { |
| 904 | vmstop_requested = reason; |
| 905 | qemu_notify_event(); |
| 906 | } |
| 907 | |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 908 | void cpu_stop_current(void) |
| 909 | { |
| 910 | if (cpu_single_env) { |
| 911 | cpu_single_env->stopped = 1; |
| 912 | cpu_exit(cpu_single_env); |
| 913 | } |
| 914 | } |
| 915 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 916 | void vm_stop(int reason) |
| 917 | { |
| 918 | QemuThread me; |
| 919 | qemu_thread_self(&me); |
| 920 | |
| 921 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 922 | qemu_system_vmstop_request(reason); |
| 923 | /* |
| 924 | * FIXME: should not return to device code in case |
| 925 | * vm_stop() has been requested. |
| 926 | */ |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 927 | cpu_stop_current(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | do_vm_stop(reason); |
| 931 | } |
| 932 | |
| 933 | #endif |
| 934 | |
| 935 | static int qemu_cpu_exec(CPUState *env) |
| 936 | { |
| 937 | int ret; |
| 938 | #ifdef CONFIG_PROFILER |
| 939 | int64_t ti; |
| 940 | #endif |
| 941 | |
| 942 | #ifdef CONFIG_PROFILER |
| 943 | ti = profile_getclock(); |
| 944 | #endif |
| 945 | if (use_icount) { |
| 946 | int64_t count; |
| 947 | int decr; |
| 948 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 949 | env->icount_decr.u16.low = 0; |
| 950 | env->icount_extra = 0; |
| 951 | count = qemu_icount_round (qemu_next_deadline()); |
| 952 | qemu_icount += count; |
| 953 | decr = (count > 0xffff) ? 0xffff : count; |
| 954 | count -= decr; |
| 955 | env->icount_decr.u16.low = decr; |
| 956 | env->icount_extra = count; |
| 957 | } |
| 958 | ret = cpu_exec(env); |
| 959 | #ifdef CONFIG_PROFILER |
| 960 | qemu_time += profile_getclock() - ti; |
| 961 | #endif |
| 962 | if (use_icount) { |
| 963 | /* Fold pending instructions back into the |
| 964 | instruction counter, and clear the interrupt flag. */ |
| 965 | qemu_icount -= (env->icount_decr.u16.low |
| 966 | + env->icount_extra); |
| 967 | env->icount_decr.u32 = 0; |
| 968 | env->icount_extra = 0; |
| 969 | } |
| 970 | return ret; |
| 971 | } |
| 972 | |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 973 | bool cpu_exec_all(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 974 | { |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 975 | int r; |
| 976 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 977 | if (next_cpu == NULL) |
| 978 | next_cpu = first_cpu; |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 979 | for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) { |
Jan Kiszka | 345f442 | 2010-06-25 16:56:54 +0200 | [diff] [blame] | 980 | CPUState *env = next_cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 981 | |
| 982 | qemu_clock_enable(vm_clock, |
Jan Kiszka | 345f442 | 2010-06-25 16:56:54 +0200 | [diff] [blame] | 983 | (env->singlestep_enabled & SSTEP_NOTIMER) == 0); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 984 | |
| 985 | if (qemu_alarm_pending()) |
| 986 | break; |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 987 | if (cpu_can_run(env)) { |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 988 | r = qemu_cpu_exec(env); |
| 989 | if (kvm_enabled()) { |
| 990 | qemu_kvm_eat_signals(env); |
| 991 | } |
| 992 | if (r == EXCP_DEBUG) { |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 993 | break; |
| 994 | } |
| 995 | } else if (env->stop) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | } |
Jan Kiszka | c629a4b | 2010-06-25 16:56:52 +0200 | [diff] [blame] | 999 | exit_request = 0; |
Jan Kiszka | 472fb0c | 2010-06-25 16:56:55 +0200 | [diff] [blame] | 1000 | return any_cpu_has_work(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | void set_numa_modes(void) |
| 1004 | { |
| 1005 | CPUState *env; |
| 1006 | int i; |
| 1007 | |
| 1008 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 1009 | for (i = 0; i < nb_numa_nodes; i++) { |
| 1010 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 1011 | env->numa_node = i; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | void set_cpu_log(const char *optarg) |
| 1018 | { |
| 1019 | int mask; |
| 1020 | const CPULogItem *item; |
| 1021 | |
| 1022 | mask = cpu_str_to_log_mask(optarg); |
| 1023 | if (!mask) { |
| 1024 | printf("Log items (comma separated):\n"); |
| 1025 | for (item = cpu_log_items; item->mask != 0; item++) { |
| 1026 | printf("%-10s %s\n", item->name, item->help); |
| 1027 | } |
| 1028 | exit(1); |
| 1029 | } |
| 1030 | cpu_set_log(mask); |
| 1031 | } |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 1032 | |
| 1033 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 1034 | int64_t cpu_get_icount(void) |
| 1035 | { |
| 1036 | int64_t icount; |
| 1037 | CPUState *env = cpu_single_env;; |
| 1038 | |
| 1039 | icount = qemu_icount; |
| 1040 | if (env) { |
| 1041 | if (!can_do_io(env)) { |
| 1042 | fprintf(stderr, "Bad clock read\n"); |
| 1043 | } |
| 1044 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 1045 | } |
| 1046 | return qemu_icount_bias + (icount << icount_time_shift); |
| 1047 | } |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 1048 | |
Stefan Weil | 9a78eea | 2010-10-22 23:03:33 +0200 | [diff] [blame] | 1049 | void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 1050 | { |
| 1051 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 1052 | #if defined(cpu_list_id) |
| 1053 | cpu_list_id(f, cpu_fprintf, optarg); |
| 1054 | #elif defined(cpu_list) |
| 1055 | cpu_list(f, cpu_fprintf); /* deprecated */ |
| 1056 | #endif |
| 1057 | } |