]>
Commit | Line | Data |
---|---|---|
296af7c9 BS |
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" | |
262ea18e | 33 | #include "exec-all.h" |
296af7c9 BS |
34 | |
35 | #include "cpus.h" | |
a8486bc9 | 36 | #include "compatfd.h" |
c0532a76 MT |
37 | #ifdef CONFIG_LINUX |
38 | #include <sys/prctl.h> | |
39 | #endif | |
296af7c9 | 40 | |
7277e027 BS |
41 | #ifdef SIGRTMIN |
42 | #define SIG_IPI (SIGRTMIN+4) | |
43 | #else | |
44 | #define SIG_IPI SIGUSR1 | |
45 | #endif | |
46 | ||
c0532a76 MT |
47 | #ifndef PR_MCE_KILL |
48 | #define PR_MCE_KILL 33 | |
49 | #endif | |
50 | ||
296af7c9 BS |
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 | ||
3ae9501c MT |
102 | int cpu_is_stopped(CPUState *env) |
103 | { | |
104 | return !vm_running || env->stopped; | |
105 | } | |
106 | ||
296af7c9 BS |
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); | |
55df6f33 MT |
114 | qemu_aio_flush(); |
115 | bdrv_flush_all(); | |
296af7c9 BS |
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; | |
55274a30 | 124 | if (env->stopped || !vm_running) |
296af7c9 BS |
125 | return 0; |
126 | return 1; | |
127 | } | |
128 | ||
129 | static int cpu_has_work(CPUState *env) | |
130 | { | |
131 | if (env->stop) | |
132 | return 1; | |
e82bcec2 MT |
133 | if (env->queued_work_first) |
134 | return 1; | |
55274a30 | 135 | if (env->stopped || !vm_running) |
296af7c9 BS |
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 | ||
472fb0c4 | 144 | static int any_cpu_has_work(void) |
296af7c9 BS |
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 | ||
3c638d06 JK |
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 | ||
296af7c9 BS |
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. */ | |
26a82330 | 167 | static const uint64_t val = 1; |
296af7c9 BS |
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 | } | |
55f8d6ac | 225 | |
55f8d6ac JK |
226 | static void dummy_signal(int sig) |
227 | { | |
228 | } | |
55f8d6ac | 229 | |
d0f294ce JK |
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 | ||
9a36085b JK |
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)); | |
de758970 JK |
322 | |
323 | #ifndef CONFIG_IOTHREAD | |
324 | if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) { | |
325 | qemu_notify_event(); | |
326 | } | |
327 | #endif | |
9a36085b JK |
328 | } |
329 | ||
55f8d6ac JK |
330 | #else /* _WIN32 */ |
331 | ||
296af7c9 BS |
332 | HANDLE qemu_event_handle; |
333 | ||
334 | static void dummy_event_handler(void *opaque) | |
335 | { | |
336 | } | |
337 | ||
338 | static int qemu_event_init(void) | |
339 | { | |
340 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); | |
341 | if (!qemu_event_handle) { | |
342 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); | |
343 | return -1; | |
344 | } | |
345 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); | |
346 | return 0; | |
347 | } | |
348 | ||
349 | static void qemu_event_increment(void) | |
350 | { | |
351 | if (!SetEvent(qemu_event_handle)) { | |
352 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", | |
353 | GetLastError()); | |
354 | exit (1); | |
355 | } | |
356 | } | |
9a36085b JK |
357 | |
358 | static void qemu_kvm_eat_signals(CPUState *env) | |
359 | { | |
360 | } | |
55f8d6ac | 361 | #endif /* _WIN32 */ |
296af7c9 BS |
362 | |
363 | #ifndef CONFIG_IOTHREAD | |
ff48eb5f JK |
364 | static void qemu_kvm_init_cpu_signals(CPUState *env) |
365 | { | |
366 | #ifndef _WIN32 | |
367 | int r; | |
368 | sigset_t set; | |
369 | struct sigaction sigact; | |
370 | ||
371 | memset(&sigact, 0, sizeof(sigact)); | |
372 | sigact.sa_handler = dummy_signal; | |
373 | sigaction(SIG_IPI, &sigact, NULL); | |
374 | ||
375 | sigemptyset(&set); | |
376 | sigaddset(&set, SIG_IPI); | |
de758970 JK |
377 | sigaddset(&set, SIGIO); |
378 | sigaddset(&set, SIGALRM); | |
ff48eb5f JK |
379 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
380 | ||
381 | pthread_sigmask(SIG_BLOCK, NULL, &set); | |
382 | sigdelset(&set, SIG_IPI); | |
383 | sigdelset(&set, SIGBUS); | |
de758970 JK |
384 | sigdelset(&set, SIGIO); |
385 | sigdelset(&set, SIGALRM); | |
ff48eb5f JK |
386 | r = kvm_set_signal_mask(env, &set); |
387 | if (r) { | |
388 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); | |
389 | exit(1); | |
390 | } | |
391 | #endif | |
392 | } | |
393 | ||
de758970 JK |
394 | #ifndef _WIN32 |
395 | static sigset_t block_synchronous_signals(void) | |
396 | { | |
397 | sigset_t set; | |
398 | ||
399 | sigemptyset(&set); | |
400 | if (kvm_enabled()) { | |
401 | /* | |
402 | * We need to process timer signals synchronously to avoid a race | |
403 | * between exit_request check and KVM vcpu entry. | |
404 | */ | |
405 | sigaddset(&set, SIGIO); | |
406 | sigaddset(&set, SIGALRM); | |
407 | } | |
408 | ||
409 | return set; | |
410 | } | |
411 | #endif | |
412 | ||
296af7c9 BS |
413 | int qemu_init_main_loop(void) |
414 | { | |
d0f294ce JK |
415 | #ifndef _WIN32 |
416 | sigset_t blocked_signals; | |
417 | int ret; | |
418 | ||
de758970 | 419 | blocked_signals = block_synchronous_signals(); |
d0f294ce JK |
420 | |
421 | ret = qemu_signalfd_init(blocked_signals); | |
422 | if (ret) { | |
423 | return ret; | |
424 | } | |
425 | #endif | |
3c638d06 JK |
426 | cpu_set_debug_excp_handler(cpu_debug_handler); |
427 | ||
296af7c9 BS |
428 | return qemu_event_init(); |
429 | } | |
430 | ||
7277e027 BS |
431 | void qemu_main_loop_start(void) |
432 | { | |
433 | } | |
434 | ||
296af7c9 BS |
435 | void qemu_init_vcpu(void *_env) |
436 | { | |
437 | CPUState *env = _env; | |
84b4915d | 438 | int r; |
296af7c9 BS |
439 | |
440 | env->nr_cores = smp_cores; | |
441 | env->nr_threads = smp_threads; | |
84b4915d JK |
442 | |
443 | if (kvm_enabled()) { | |
444 | r = kvm_init_vcpu(env); | |
445 | if (r < 0) { | |
446 | fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r)); | |
447 | exit(1); | |
448 | } | |
ff48eb5f | 449 | qemu_kvm_init_cpu_signals(env); |
84b4915d | 450 | } |
296af7c9 BS |
451 | } |
452 | ||
453 | int qemu_cpu_self(void *env) | |
454 | { | |
455 | return 1; | |
456 | } | |
457 | ||
e82bcec2 MT |
458 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
459 | { | |
460 | func(data); | |
461 | } | |
462 | ||
296af7c9 BS |
463 | void resume_all_vcpus(void) |
464 | { | |
465 | } | |
466 | ||
467 | void pause_all_vcpus(void) | |
468 | { | |
469 | } | |
470 | ||
471 | void qemu_cpu_kick(void *env) | |
472 | { | |
473 | return; | |
474 | } | |
475 | ||
476 | void qemu_notify_event(void) | |
477 | { | |
478 | CPUState *env = cpu_single_env; | |
479 | ||
480 | qemu_event_increment (); | |
481 | if (env) { | |
482 | cpu_exit(env); | |
483 | } | |
484 | if (next_cpu && env != next_cpu) { | |
485 | cpu_exit(next_cpu); | |
486 | } | |
38145df2 | 487 | exit_request = 1; |
296af7c9 BS |
488 | } |
489 | ||
490 | void qemu_mutex_lock_iothread(void) {} | |
491 | void qemu_mutex_unlock_iothread(void) {} | |
492 | ||
b4a3d965 JK |
493 | void cpu_stop_current(void) |
494 | { | |
495 | } | |
496 | ||
296af7c9 BS |
497 | void vm_stop(int reason) |
498 | { | |
499 | do_vm_stop(reason); | |
500 | } | |
501 | ||
502 | #else /* CONFIG_IOTHREAD */ | |
503 | ||
504 | #include "qemu-thread.h" | |
505 | ||
506 | QemuMutex qemu_global_mutex; | |
507 | static QemuMutex qemu_fair_mutex; | |
508 | ||
509 | static QemuThread io_thread; | |
510 | ||
511 | static QemuThread *tcg_cpu_thread; | |
512 | static QemuCond *tcg_halt_cond; | |
513 | ||
514 | static int qemu_system_ready; | |
515 | /* cpu creation */ | |
516 | static QemuCond qemu_cpu_cond; | |
517 | /* system init */ | |
518 | static QemuCond qemu_system_cond; | |
519 | static QemuCond qemu_pause_cond; | |
e82bcec2 | 520 | static QemuCond qemu_work_cond; |
296af7c9 | 521 | |
55f8d6ac JK |
522 | static void cpu_signal(int sig) |
523 | { | |
524 | if (cpu_single_env) { | |
525 | cpu_exit(cpu_single_env); | |
526 | } | |
527 | exit_request = 1; | |
528 | } | |
529 | ||
530 | static void qemu_kvm_init_cpu_signals(CPUState *env) | |
531 | { | |
532 | int r; | |
533 | sigset_t set; | |
534 | struct sigaction sigact; | |
535 | ||
536 | memset(&sigact, 0, sizeof(sigact)); | |
537 | sigact.sa_handler = dummy_signal; | |
538 | sigaction(SIG_IPI, &sigact, NULL); | |
539 | ||
540 | pthread_sigmask(SIG_BLOCK, NULL, &set); | |
541 | sigdelset(&set, SIG_IPI); | |
542 | sigdelset(&set, SIGBUS); | |
543 | r = kvm_set_signal_mask(env, &set); | |
544 | if (r) { | |
545 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); | |
546 | exit(1); | |
547 | } | |
548 | } | |
549 | ||
550 | static void qemu_tcg_init_cpu_signals(void) | |
551 | { | |
552 | sigset_t set; | |
553 | struct sigaction sigact; | |
554 | ||
555 | memset(&sigact, 0, sizeof(sigact)); | |
556 | sigact.sa_handler = cpu_signal; | |
557 | sigaction(SIG_IPI, &sigact, NULL); | |
558 | ||
559 | sigemptyset(&set); | |
560 | sigaddset(&set, SIG_IPI); | |
561 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
562 | } | |
563 | ||
564 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, | |
565 | void *ctx); | |
566 | ||
567 | static sigset_t block_io_signals(void) | |
568 | { | |
569 | sigset_t set; | |
570 | struct sigaction action; | |
571 | ||
572 | /* SIGUSR2 used by posix-aio-compat.c */ | |
573 | sigemptyset(&set); | |
574 | sigaddset(&set, SIGUSR2); | |
575 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
576 | ||
577 | sigemptyset(&set); | |
578 | sigaddset(&set, SIGIO); | |
579 | sigaddset(&set, SIGALRM); | |
580 | sigaddset(&set, SIG_IPI); | |
581 | sigaddset(&set, SIGBUS); | |
582 | pthread_sigmask(SIG_BLOCK, &set, NULL); | |
583 | ||
584 | memset(&action, 0, sizeof(action)); | |
585 | action.sa_flags = SA_SIGINFO; | |
586 | action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler; | |
587 | sigaction(SIGBUS, &action, NULL); | |
588 | prctl(PR_MCE_KILL, 1, 1, 0, 0); | |
589 | ||
590 | return set; | |
591 | } | |
592 | ||
296af7c9 BS |
593 | int qemu_init_main_loop(void) |
594 | { | |
595 | int ret; | |
a8486bc9 | 596 | sigset_t blocked_signals; |
296af7c9 | 597 | |
3c638d06 JK |
598 | cpu_set_debug_excp_handler(cpu_debug_handler); |
599 | ||
a8486bc9 MT |
600 | blocked_signals = block_io_signals(); |
601 | ||
602 | ret = qemu_signalfd_init(blocked_signals); | |
603 | if (ret) | |
604 | return ret; | |
605 | ||
606 | /* Note eventfd must be drained before signalfd handlers run */ | |
296af7c9 BS |
607 | ret = qemu_event_init(); |
608 | if (ret) | |
609 | return ret; | |
610 | ||
611 | qemu_cond_init(&qemu_pause_cond); | |
f8ca7b43 | 612 | qemu_cond_init(&qemu_system_cond); |
296af7c9 BS |
613 | qemu_mutex_init(&qemu_fair_mutex); |
614 | qemu_mutex_init(&qemu_global_mutex); | |
615 | qemu_mutex_lock(&qemu_global_mutex); | |
616 | ||
296af7c9 BS |
617 | qemu_thread_self(&io_thread); |
618 | ||
619 | return 0; | |
620 | } | |
621 | ||
7277e027 BS |
622 | void qemu_main_loop_start(void) |
623 | { | |
624 | qemu_system_ready = 1; | |
625 | qemu_cond_broadcast(&qemu_system_cond); | |
626 | } | |
627 | ||
e82bcec2 MT |
628 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
629 | { | |
630 | struct qemu_work_item wi; | |
631 | ||
632 | if (qemu_cpu_self(env)) { | |
633 | func(data); | |
634 | return; | |
635 | } | |
636 | ||
637 | wi.func = func; | |
638 | wi.data = data; | |
639 | if (!env->queued_work_first) | |
640 | env->queued_work_first = &wi; | |
641 | else | |
642 | env->queued_work_last->next = &wi; | |
643 | env->queued_work_last = &wi; | |
644 | wi.next = NULL; | |
645 | wi.done = false; | |
646 | ||
647 | qemu_cpu_kick(env); | |
648 | while (!wi.done) { | |
649 | CPUState *self_env = cpu_single_env; | |
650 | ||
651 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); | |
652 | cpu_single_env = self_env; | |
653 | } | |
654 | } | |
655 | ||
656 | static void flush_queued_work(CPUState *env) | |
657 | { | |
658 | struct qemu_work_item *wi; | |
659 | ||
660 | if (!env->queued_work_first) | |
661 | return; | |
662 | ||
663 | while ((wi = env->queued_work_first)) { | |
664 | env->queued_work_first = wi->next; | |
665 | wi->func(wi->data); | |
666 | wi->done = true; | |
667 | } | |
668 | env->queued_work_last = NULL; | |
669 | qemu_cond_broadcast(&qemu_work_cond); | |
670 | } | |
671 | ||
296af7c9 BS |
672 | static void qemu_wait_io_event_common(CPUState *env) |
673 | { | |
674 | if (env->stop) { | |
675 | env->stop = 0; | |
676 | env->stopped = 1; | |
677 | qemu_cond_signal(&qemu_pause_cond); | |
678 | } | |
e82bcec2 | 679 | flush_queued_work(env); |
aa2c364b | 680 | env->thread_kicked = false; |
296af7c9 BS |
681 | } |
682 | ||
6cabe1f3 | 683 | static void qemu_tcg_wait_io_event(void) |
296af7c9 | 684 | { |
6cabe1f3 JK |
685 | CPUState *env; |
686 | ||
472fb0c4 | 687 | while (!any_cpu_has_work()) |
6cabe1f3 | 688 | qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000); |
296af7c9 BS |
689 | |
690 | qemu_mutex_unlock(&qemu_global_mutex); | |
691 | ||
692 | /* | |
693 | * Users of qemu_global_mutex can be starved, having no chance | |
694 | * to acquire it since this path will get to it first. | |
695 | * So use another lock to provide fairness. | |
696 | */ | |
697 | qemu_mutex_lock(&qemu_fair_mutex); | |
698 | qemu_mutex_unlock(&qemu_fair_mutex); | |
699 | ||
700 | qemu_mutex_lock(&qemu_global_mutex); | |
6cabe1f3 JK |
701 | |
702 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
703 | qemu_wait_io_event_common(env); | |
704 | } | |
296af7c9 BS |
705 | } |
706 | ||
c0532a76 MT |
707 | static void sigbus_reraise(void) |
708 | { | |
709 | sigset_t set; | |
710 | struct sigaction action; | |
711 | ||
712 | memset(&action, 0, sizeof(action)); | |
713 | action.sa_handler = SIG_DFL; | |
714 | if (!sigaction(SIGBUS, &action, NULL)) { | |
715 | raise(SIGBUS); | |
716 | sigemptyset(&set); | |
717 | sigaddset(&set, SIGBUS); | |
718 | sigprocmask(SIG_UNBLOCK, &set, NULL); | |
719 | } | |
720 | perror("Failed to re-raise SIGBUS!\n"); | |
721 | abort(); | |
722 | } | |
723 | ||
724 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, | |
725 | void *ctx) | |
726 | { | |
a1b87fe0 | 727 | if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) { |
c0532a76 | 728 | sigbus_reraise(); |
a1b87fe0 | 729 | } |
c0532a76 MT |
730 | } |
731 | ||
296af7c9 BS |
732 | static void qemu_kvm_wait_io_event(CPUState *env) |
733 | { | |
734 | while (!cpu_has_work(env)) | |
735 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); | |
736 | ||
5db5bdac | 737 | qemu_kvm_eat_signals(env); |
296af7c9 BS |
738 | qemu_wait_io_event_common(env); |
739 | } | |
740 | ||
741 | static int qemu_cpu_exec(CPUState *env); | |
742 | ||
743 | static void *kvm_cpu_thread_fn(void *arg) | |
744 | { | |
745 | CPUState *env = arg; | |
84b4915d | 746 | int r; |
296af7c9 | 747 | |
6164e6d6 | 748 | qemu_mutex_lock(&qemu_global_mutex); |
296af7c9 | 749 | qemu_thread_self(env->thread); |
d31ae052 | 750 | |
84b4915d JK |
751 | r = kvm_init_vcpu(env); |
752 | if (r < 0) { | |
753 | fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r)); | |
754 | exit(1); | |
755 | } | |
296af7c9 | 756 | |
55f8d6ac | 757 | qemu_kvm_init_cpu_signals(env); |
296af7c9 BS |
758 | |
759 | /* signal CPU creation */ | |
296af7c9 BS |
760 | env->created = 1; |
761 | qemu_cond_signal(&qemu_cpu_cond); | |
762 | ||
763 | /* and wait for machine initialization */ | |
764 | while (!qemu_system_ready) | |
765 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
766 | ||
767 | while (1) { | |
768 | if (cpu_can_run(env)) | |
769 | qemu_cpu_exec(env); | |
770 | qemu_kvm_wait_io_event(env); | |
771 | } | |
772 | ||
773 | return NULL; | |
774 | } | |
775 | ||
776 | static void *tcg_cpu_thread_fn(void *arg) | |
777 | { | |
778 | CPUState *env = arg; | |
779 | ||
55f8d6ac | 780 | qemu_tcg_init_cpu_signals(); |
296af7c9 BS |
781 | qemu_thread_self(env->thread); |
782 | ||
783 | /* signal CPU creation */ | |
784 | qemu_mutex_lock(&qemu_global_mutex); | |
785 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
786 | env->created = 1; | |
787 | qemu_cond_signal(&qemu_cpu_cond); | |
788 | ||
789 | /* and wait for machine initialization */ | |
790 | while (!qemu_system_ready) | |
791 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
792 | ||
793 | while (1) { | |
472fb0c4 | 794 | cpu_exec_all(); |
6cabe1f3 | 795 | qemu_tcg_wait_io_event(); |
296af7c9 BS |
796 | } |
797 | ||
798 | return NULL; | |
799 | } | |
800 | ||
801 | void qemu_cpu_kick(void *_env) | |
802 | { | |
803 | CPUState *env = _env; | |
804 | qemu_cond_broadcast(env->halt_cond); | |
aa2c364b JK |
805 | if (!env->thread_kicked) { |
806 | qemu_thread_signal(env->thread, SIG_IPI); | |
807 | env->thread_kicked = true; | |
808 | } | |
296af7c9 BS |
809 | } |
810 | ||
811 | int qemu_cpu_self(void *_env) | |
812 | { | |
813 | CPUState *env = _env; | |
814 | QemuThread this; | |
815 | ||
816 | qemu_thread_self(&this); | |
817 | ||
818 | return qemu_thread_equal(&this, env->thread); | |
819 | } | |
820 | ||
296af7c9 BS |
821 | void qemu_mutex_lock_iothread(void) |
822 | { | |
823 | if (kvm_enabled()) { | |
296af7c9 | 824 | qemu_mutex_lock(&qemu_global_mutex); |
1a28cac3 MT |
825 | } else { |
826 | qemu_mutex_lock(&qemu_fair_mutex); | |
827 | if (qemu_mutex_trylock(&qemu_global_mutex)) { | |
828 | qemu_thread_signal(tcg_cpu_thread, SIG_IPI); | |
829 | qemu_mutex_lock(&qemu_global_mutex); | |
830 | } | |
831 | qemu_mutex_unlock(&qemu_fair_mutex); | |
832 | } | |
296af7c9 BS |
833 | } |
834 | ||
835 | void qemu_mutex_unlock_iothread(void) | |
836 | { | |
837 | qemu_mutex_unlock(&qemu_global_mutex); | |
838 | } | |
839 | ||
840 | static int all_vcpus_paused(void) | |
841 | { | |
842 | CPUState *penv = first_cpu; | |
843 | ||
844 | while (penv) { | |
845 | if (!penv->stopped) | |
846 | return 0; | |
847 | penv = (CPUState *)penv->next_cpu; | |
848 | } | |
849 | ||
850 | return 1; | |
851 | } | |
852 | ||
853 | void pause_all_vcpus(void) | |
854 | { | |
855 | CPUState *penv = first_cpu; | |
856 | ||
857 | while (penv) { | |
858 | penv->stop = 1; | |
296af7c9 BS |
859 | qemu_cpu_kick(penv); |
860 | penv = (CPUState *)penv->next_cpu; | |
861 | } | |
862 | ||
863 | while (!all_vcpus_paused()) { | |
864 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); | |
865 | penv = first_cpu; | |
866 | while (penv) { | |
1fbb22e5 | 867 | qemu_cpu_kick(penv); |
296af7c9 BS |
868 | penv = (CPUState *)penv->next_cpu; |
869 | } | |
870 | } | |
871 | } | |
872 | ||
873 | void resume_all_vcpus(void) | |
874 | { | |
875 | CPUState *penv = first_cpu; | |
876 | ||
877 | while (penv) { | |
878 | penv->stop = 0; | |
879 | penv->stopped = 0; | |
296af7c9 BS |
880 | qemu_cpu_kick(penv); |
881 | penv = (CPUState *)penv->next_cpu; | |
882 | } | |
883 | } | |
884 | ||
885 | static void tcg_init_vcpu(void *_env) | |
886 | { | |
887 | CPUState *env = _env; | |
888 | /* share a single thread for all cpus with TCG */ | |
889 | if (!tcg_cpu_thread) { | |
890 | env->thread = qemu_mallocz(sizeof(QemuThread)); | |
891 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
892 | qemu_cond_init(env->halt_cond); | |
893 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); | |
894 | while (env->created == 0) | |
895 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
896 | tcg_cpu_thread = env->thread; | |
897 | tcg_halt_cond = env->halt_cond; | |
898 | } else { | |
899 | env->thread = tcg_cpu_thread; | |
900 | env->halt_cond = tcg_halt_cond; | |
901 | } | |
902 | } | |
903 | ||
904 | static void kvm_start_vcpu(CPUState *env) | |
905 | { | |
906 | env->thread = qemu_mallocz(sizeof(QemuThread)); | |
907 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
908 | qemu_cond_init(env->halt_cond); | |
909 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); | |
910 | while (env->created == 0) | |
911 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
912 | } | |
913 | ||
914 | void qemu_init_vcpu(void *_env) | |
915 | { | |
916 | CPUState *env = _env; | |
917 | ||
918 | env->nr_cores = smp_cores; | |
919 | env->nr_threads = smp_threads; | |
920 | if (kvm_enabled()) | |
921 | kvm_start_vcpu(env); | |
922 | else | |
923 | tcg_init_vcpu(env); | |
924 | } | |
925 | ||
926 | void qemu_notify_event(void) | |
927 | { | |
928 | qemu_event_increment(); | |
929 | } | |
930 | ||
931 | static void qemu_system_vmstop_request(int reason) | |
932 | { | |
933 | vmstop_requested = reason; | |
934 | qemu_notify_event(); | |
935 | } | |
936 | ||
b4a3d965 JK |
937 | void cpu_stop_current(void) |
938 | { | |
939 | if (cpu_single_env) { | |
940 | cpu_single_env->stopped = 1; | |
941 | cpu_exit(cpu_single_env); | |
942 | } | |
943 | } | |
944 | ||
296af7c9 BS |
945 | void vm_stop(int reason) |
946 | { | |
947 | QemuThread me; | |
948 | qemu_thread_self(&me); | |
949 | ||
950 | if (!qemu_thread_equal(&me, &io_thread)) { | |
951 | qemu_system_vmstop_request(reason); | |
952 | /* | |
953 | * FIXME: should not return to device code in case | |
954 | * vm_stop() has been requested. | |
955 | */ | |
b4a3d965 | 956 | cpu_stop_current(); |
296af7c9 BS |
957 | return; |
958 | } | |
959 | do_vm_stop(reason); | |
960 | } | |
961 | ||
962 | #endif | |
963 | ||
964 | static int qemu_cpu_exec(CPUState *env) | |
965 | { | |
966 | int ret; | |
967 | #ifdef CONFIG_PROFILER | |
968 | int64_t ti; | |
969 | #endif | |
970 | ||
971 | #ifdef CONFIG_PROFILER | |
972 | ti = profile_getclock(); | |
973 | #endif | |
974 | if (use_icount) { | |
975 | int64_t count; | |
976 | int decr; | |
977 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); | |
978 | env->icount_decr.u16.low = 0; | |
979 | env->icount_extra = 0; | |
980 | count = qemu_icount_round (qemu_next_deadline()); | |
981 | qemu_icount += count; | |
982 | decr = (count > 0xffff) ? 0xffff : count; | |
983 | count -= decr; | |
984 | env->icount_decr.u16.low = decr; | |
985 | env->icount_extra = count; | |
986 | } | |
987 | ret = cpu_exec(env); | |
988 | #ifdef CONFIG_PROFILER | |
989 | qemu_time += profile_getclock() - ti; | |
990 | #endif | |
991 | if (use_icount) { | |
992 | /* Fold pending instructions back into the | |
993 | instruction counter, and clear the interrupt flag. */ | |
994 | qemu_icount -= (env->icount_decr.u16.low | |
995 | + env->icount_extra); | |
996 | env->icount_decr.u32 = 0; | |
997 | env->icount_extra = 0; | |
998 | } | |
999 | return ret; | |
1000 | } | |
1001 | ||
472fb0c4 | 1002 | bool cpu_exec_all(void) |
296af7c9 | 1003 | { |
9a36085b JK |
1004 | int r; |
1005 | ||
296af7c9 BS |
1006 | if (next_cpu == NULL) |
1007 | next_cpu = first_cpu; | |
c629a4bc | 1008 | for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) { |
345f4426 | 1009 | CPUState *env = next_cpu; |
296af7c9 BS |
1010 | |
1011 | qemu_clock_enable(vm_clock, | |
345f4426 | 1012 | (env->singlestep_enabled & SSTEP_NOTIMER) == 0); |
296af7c9 BS |
1013 | |
1014 | if (qemu_alarm_pending()) | |
1015 | break; | |
3c638d06 | 1016 | if (cpu_can_run(env)) { |
9a36085b JK |
1017 | r = qemu_cpu_exec(env); |
1018 | if (kvm_enabled()) { | |
1019 | qemu_kvm_eat_signals(env); | |
1020 | } | |
1021 | if (r == EXCP_DEBUG) { | |
3c638d06 JK |
1022 | break; |
1023 | } | |
1024 | } else if (env->stop) { | |
296af7c9 BS |
1025 | break; |
1026 | } | |
1027 | } | |
c629a4bc | 1028 | exit_request = 0; |
472fb0c4 | 1029 | return any_cpu_has_work(); |
296af7c9 BS |
1030 | } |
1031 | ||
1032 | void set_numa_modes(void) | |
1033 | { | |
1034 | CPUState *env; | |
1035 | int i; | |
1036 | ||
1037 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
1038 | for (i = 0; i < nb_numa_nodes; i++) { | |
1039 | if (node_cpumask[i] & (1 << env->cpu_index)) { | |
1040 | env->numa_node = i; | |
1041 | } | |
1042 | } | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | void set_cpu_log(const char *optarg) | |
1047 | { | |
1048 | int mask; | |
1049 | const CPULogItem *item; | |
1050 | ||
1051 | mask = cpu_str_to_log_mask(optarg); | |
1052 | if (!mask) { | |
1053 | printf("Log items (comma separated):\n"); | |
1054 | for (item = cpu_log_items; item->mask != 0; item++) { | |
1055 | printf("%-10s %s\n", item->name, item->help); | |
1056 | } | |
1057 | exit(1); | |
1058 | } | |
1059 | cpu_set_log(mask); | |
1060 | } | |
29e922b6 BS |
1061 | |
1062 | /* Return the virtual CPU time, based on the instruction counter. */ | |
1063 | int64_t cpu_get_icount(void) | |
1064 | { | |
1065 | int64_t icount; | |
1066 | CPUState *env = cpu_single_env;; | |
1067 | ||
1068 | icount = qemu_icount; | |
1069 | if (env) { | |
1070 | if (!can_do_io(env)) { | |
1071 | fprintf(stderr, "Bad clock read\n"); | |
1072 | } | |
1073 | icount -= (env->icount_decr.u16.low + env->icount_extra); | |
1074 | } | |
1075 | return qemu_icount_bias + (icount << icount_time_shift); | |
1076 | } | |
262353cb | 1077 | |
9a78eead | 1078 | void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
262353cb BS |
1079 | { |
1080 | /* XXX: implement xxx_cpu_list for targets that still miss it */ | |
1081 | #if defined(cpu_list_id) | |
1082 | cpu_list_id(f, cpu_fprintf, optarg); | |
1083 | #elif defined(cpu_list) | |
1084 | cpu_list(f, cpu_fprintf); /* deprecated */ | |
1085 | #endif | |
1086 | } |