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