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