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