]>
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" | |
36 | ||
7277e027 BS |
37 | #ifdef SIGRTMIN |
38 | #define SIG_IPI (SIGRTMIN+4) | |
39 | #else | |
40 | #define SIG_IPI SIGUSR1 | |
41 | #endif | |
42 | ||
296af7c9 BS |
43 | static CPUState *next_cpu; |
44 | ||
45 | /***********************************************************/ | |
46 | void hw_error(const char *fmt, ...) | |
47 | { | |
48 | va_list ap; | |
49 | CPUState *env; | |
50 | ||
51 | va_start(ap, fmt); | |
52 | fprintf(stderr, "qemu: hardware error: "); | |
53 | vfprintf(stderr, fmt, ap); | |
54 | fprintf(stderr, "\n"); | |
55 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
56 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); | |
57 | #ifdef TARGET_I386 | |
58 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); | |
59 | #else | |
60 | cpu_dump_state(env, stderr, fprintf, 0); | |
61 | #endif | |
62 | } | |
63 | va_end(ap); | |
64 | abort(); | |
65 | } | |
66 | ||
67 | void cpu_synchronize_all_states(void) | |
68 | { | |
69 | CPUState *cpu; | |
70 | ||
71 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { | |
72 | cpu_synchronize_state(cpu); | |
73 | } | |
74 | } | |
75 | ||
76 | void cpu_synchronize_all_post_reset(void) | |
77 | { | |
78 | CPUState *cpu; | |
79 | ||
80 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { | |
81 | cpu_synchronize_post_reset(cpu); | |
82 | } | |
83 | } | |
84 | ||
85 | void cpu_synchronize_all_post_init(void) | |
86 | { | |
87 | CPUState *cpu; | |
88 | ||
89 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { | |
90 | cpu_synchronize_post_init(cpu); | |
91 | } | |
92 | } | |
93 | ||
3ae9501c MT |
94 | int cpu_is_stopped(CPUState *env) |
95 | { | |
96 | return !vm_running || env->stopped; | |
97 | } | |
98 | ||
296af7c9 BS |
99 | static void do_vm_stop(int reason) |
100 | { | |
101 | if (vm_running) { | |
102 | cpu_disable_ticks(); | |
103 | vm_running = 0; | |
104 | pause_all_vcpus(); | |
105 | vm_state_notify(0, reason); | |
106 | monitor_protocol_event(QEVENT_STOP, NULL); | |
107 | } | |
108 | } | |
109 | ||
110 | static int cpu_can_run(CPUState *env) | |
111 | { | |
112 | if (env->stop) | |
113 | return 0; | |
55274a30 | 114 | if (env->stopped || !vm_running) |
296af7c9 BS |
115 | return 0; |
116 | return 1; | |
117 | } | |
118 | ||
119 | static int cpu_has_work(CPUState *env) | |
120 | { | |
121 | if (env->stop) | |
122 | return 1; | |
e82bcec2 MT |
123 | if (env->queued_work_first) |
124 | return 1; | |
55274a30 | 125 | if (env->stopped || !vm_running) |
296af7c9 BS |
126 | return 0; |
127 | if (!env->halted) | |
128 | return 1; | |
129 | if (qemu_cpu_has_work(env)) | |
130 | return 1; | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static int tcg_has_work(void) | |
135 | { | |
136 | CPUState *env; | |
137 | ||
138 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
139 | if (cpu_has_work(env)) | |
140 | return 1; | |
141 | return 0; | |
142 | } | |
143 | ||
144 | #ifndef _WIN32 | |
145 | static int io_thread_fd = -1; | |
146 | ||
147 | static void qemu_event_increment(void) | |
148 | { | |
149 | /* Write 8 bytes to be compatible with eventfd. */ | |
26a82330 | 150 | static const uint64_t val = 1; |
296af7c9 BS |
151 | ssize_t ret; |
152 | ||
153 | if (io_thread_fd == -1) | |
154 | return; | |
155 | ||
156 | do { | |
157 | ret = write(io_thread_fd, &val, sizeof(val)); | |
158 | } while (ret < 0 && errno == EINTR); | |
159 | ||
160 | /* EAGAIN is fine, a read must be pending. */ | |
161 | if (ret < 0 && errno != EAGAIN) { | |
162 | fprintf(stderr, "qemu_event_increment: write() filed: %s\n", | |
163 | strerror(errno)); | |
164 | exit (1); | |
165 | } | |
166 | } | |
167 | ||
168 | static void qemu_event_read(void *opaque) | |
169 | { | |
170 | int fd = (unsigned long)opaque; | |
171 | ssize_t len; | |
172 | char buffer[512]; | |
173 | ||
174 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ | |
175 | do { | |
176 | len = read(fd, buffer, sizeof(buffer)); | |
177 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); | |
178 | } | |
179 | ||
180 | static int qemu_event_init(void) | |
181 | { | |
182 | int err; | |
183 | int fds[2]; | |
184 | ||
185 | err = qemu_eventfd(fds); | |
186 | if (err == -1) | |
187 | return -errno; | |
188 | ||
189 | err = fcntl_setfl(fds[0], O_NONBLOCK); | |
190 | if (err < 0) | |
191 | goto fail; | |
192 | ||
193 | err = fcntl_setfl(fds[1], O_NONBLOCK); | |
194 | if (err < 0) | |
195 | goto fail; | |
196 | ||
197 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, | |
198 | (void *)(unsigned long)fds[0]); | |
199 | ||
200 | io_thread_fd = fds[1]; | |
201 | return 0; | |
202 | ||
203 | fail: | |
204 | close(fds[0]); | |
205 | close(fds[1]); | |
206 | return err; | |
207 | } | |
208 | #else | |
209 | HANDLE qemu_event_handle; | |
210 | ||
211 | static void dummy_event_handler(void *opaque) | |
212 | { | |
213 | } | |
214 | ||
215 | static int qemu_event_init(void) | |
216 | { | |
217 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); | |
218 | if (!qemu_event_handle) { | |
219 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); | |
220 | return -1; | |
221 | } | |
222 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | static void qemu_event_increment(void) | |
227 | { | |
228 | if (!SetEvent(qemu_event_handle)) { | |
229 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", | |
230 | GetLastError()); | |
231 | exit (1); | |
232 | } | |
233 | } | |
234 | #endif | |
235 | ||
236 | #ifndef CONFIG_IOTHREAD | |
237 | int qemu_init_main_loop(void) | |
238 | { | |
239 | return qemu_event_init(); | |
240 | } | |
241 | ||
7277e027 BS |
242 | void qemu_main_loop_start(void) |
243 | { | |
244 | } | |
245 | ||
296af7c9 BS |
246 | void qemu_init_vcpu(void *_env) |
247 | { | |
248 | CPUState *env = _env; | |
249 | ||
250 | env->nr_cores = smp_cores; | |
251 | env->nr_threads = smp_threads; | |
252 | if (kvm_enabled()) | |
253 | kvm_init_vcpu(env); | |
254 | return; | |
255 | } | |
256 | ||
257 | int qemu_cpu_self(void *env) | |
258 | { | |
259 | return 1; | |
260 | } | |
261 | ||
e82bcec2 MT |
262 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
263 | { | |
264 | func(data); | |
265 | } | |
266 | ||
296af7c9 BS |
267 | void resume_all_vcpus(void) |
268 | { | |
269 | } | |
270 | ||
271 | void pause_all_vcpus(void) | |
272 | { | |
273 | } | |
274 | ||
275 | void qemu_cpu_kick(void *env) | |
276 | { | |
277 | return; | |
278 | } | |
279 | ||
280 | void qemu_notify_event(void) | |
281 | { | |
282 | CPUState *env = cpu_single_env; | |
283 | ||
284 | qemu_event_increment (); | |
285 | if (env) { | |
286 | cpu_exit(env); | |
287 | } | |
288 | if (next_cpu && env != next_cpu) { | |
289 | cpu_exit(next_cpu); | |
290 | } | |
291 | } | |
292 | ||
293 | void qemu_mutex_lock_iothread(void) {} | |
294 | void qemu_mutex_unlock_iothread(void) {} | |
295 | ||
296 | void vm_stop(int reason) | |
297 | { | |
298 | do_vm_stop(reason); | |
299 | } | |
300 | ||
301 | #else /* CONFIG_IOTHREAD */ | |
302 | ||
303 | #include "qemu-thread.h" | |
304 | ||
305 | QemuMutex qemu_global_mutex; | |
306 | static QemuMutex qemu_fair_mutex; | |
307 | ||
308 | static QemuThread io_thread; | |
309 | ||
310 | static QemuThread *tcg_cpu_thread; | |
311 | static QemuCond *tcg_halt_cond; | |
312 | ||
313 | static int qemu_system_ready; | |
314 | /* cpu creation */ | |
315 | static QemuCond qemu_cpu_cond; | |
316 | /* system init */ | |
317 | static QemuCond qemu_system_cond; | |
318 | static QemuCond qemu_pause_cond; | |
e82bcec2 | 319 | static QemuCond qemu_work_cond; |
296af7c9 | 320 | |
55541c8a PB |
321 | static void tcg_init_ipi(void); |
322 | static void kvm_init_ipi(CPUState *env); | |
296af7c9 BS |
323 | static void unblock_io_signals(void); |
324 | ||
325 | int qemu_init_main_loop(void) | |
326 | { | |
327 | int ret; | |
328 | ||
329 | ret = qemu_event_init(); | |
330 | if (ret) | |
331 | return ret; | |
332 | ||
333 | qemu_cond_init(&qemu_pause_cond); | |
f8ca7b43 | 334 | qemu_cond_init(&qemu_system_cond); |
296af7c9 BS |
335 | qemu_mutex_init(&qemu_fair_mutex); |
336 | qemu_mutex_init(&qemu_global_mutex); | |
337 | qemu_mutex_lock(&qemu_global_mutex); | |
338 | ||
339 | unblock_io_signals(); | |
340 | qemu_thread_self(&io_thread); | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
7277e027 BS |
345 | void qemu_main_loop_start(void) |
346 | { | |
347 | qemu_system_ready = 1; | |
348 | qemu_cond_broadcast(&qemu_system_cond); | |
349 | } | |
350 | ||
e82bcec2 MT |
351 | void run_on_cpu(CPUState *env, void (*func)(void *data), void *data) |
352 | { | |
353 | struct qemu_work_item wi; | |
354 | ||
355 | if (qemu_cpu_self(env)) { | |
356 | func(data); | |
357 | return; | |
358 | } | |
359 | ||
360 | wi.func = func; | |
361 | wi.data = data; | |
362 | if (!env->queued_work_first) | |
363 | env->queued_work_first = &wi; | |
364 | else | |
365 | env->queued_work_last->next = &wi; | |
366 | env->queued_work_last = &wi; | |
367 | wi.next = NULL; | |
368 | wi.done = false; | |
369 | ||
370 | qemu_cpu_kick(env); | |
371 | while (!wi.done) { | |
372 | CPUState *self_env = cpu_single_env; | |
373 | ||
374 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); | |
375 | cpu_single_env = self_env; | |
376 | } | |
377 | } | |
378 | ||
379 | static void flush_queued_work(CPUState *env) | |
380 | { | |
381 | struct qemu_work_item *wi; | |
382 | ||
383 | if (!env->queued_work_first) | |
384 | return; | |
385 | ||
386 | while ((wi = env->queued_work_first)) { | |
387 | env->queued_work_first = wi->next; | |
388 | wi->func(wi->data); | |
389 | wi->done = true; | |
390 | } | |
391 | env->queued_work_last = NULL; | |
392 | qemu_cond_broadcast(&qemu_work_cond); | |
393 | } | |
394 | ||
296af7c9 BS |
395 | static void qemu_wait_io_event_common(CPUState *env) |
396 | { | |
397 | if (env->stop) { | |
398 | env->stop = 0; | |
399 | env->stopped = 1; | |
400 | qemu_cond_signal(&qemu_pause_cond); | |
401 | } | |
e82bcec2 | 402 | flush_queued_work(env); |
296af7c9 BS |
403 | } |
404 | ||
6cabe1f3 | 405 | static void qemu_tcg_wait_io_event(void) |
296af7c9 | 406 | { |
6cabe1f3 JK |
407 | CPUState *env; |
408 | ||
296af7c9 | 409 | while (!tcg_has_work()) |
6cabe1f3 | 410 | qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000); |
296af7c9 BS |
411 | |
412 | qemu_mutex_unlock(&qemu_global_mutex); | |
413 | ||
414 | /* | |
415 | * Users of qemu_global_mutex can be starved, having no chance | |
416 | * to acquire it since this path will get to it first. | |
417 | * So use another lock to provide fairness. | |
418 | */ | |
419 | qemu_mutex_lock(&qemu_fair_mutex); | |
420 | qemu_mutex_unlock(&qemu_fair_mutex); | |
421 | ||
422 | qemu_mutex_lock(&qemu_global_mutex); | |
6cabe1f3 JK |
423 | |
424 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
425 | qemu_wait_io_event_common(env); | |
426 | } | |
296af7c9 BS |
427 | } |
428 | ||
429 | static void qemu_kvm_eat_signal(CPUState *env, int timeout) | |
430 | { | |
431 | struct timespec ts; | |
432 | int r, e; | |
433 | siginfo_t siginfo; | |
434 | sigset_t waitset; | |
435 | ||
436 | ts.tv_sec = timeout / 1000; | |
437 | ts.tv_nsec = (timeout % 1000) * 1000000; | |
438 | ||
439 | sigemptyset(&waitset); | |
440 | sigaddset(&waitset, SIG_IPI); | |
441 | ||
442 | qemu_mutex_unlock(&qemu_global_mutex); | |
443 | r = sigtimedwait(&waitset, &siginfo, &ts); | |
444 | e = errno; | |
445 | qemu_mutex_lock(&qemu_global_mutex); | |
446 | ||
447 | if (r == -1 && !(e == EAGAIN || e == EINTR)) { | |
448 | fprintf(stderr, "sigtimedwait: %s\n", strerror(e)); | |
449 | exit(1); | |
450 | } | |
451 | } | |
452 | ||
453 | static void qemu_kvm_wait_io_event(CPUState *env) | |
454 | { | |
455 | while (!cpu_has_work(env)) | |
456 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); | |
457 | ||
458 | qemu_kvm_eat_signal(env, 0); | |
459 | qemu_wait_io_event_common(env); | |
460 | } | |
461 | ||
462 | static int qemu_cpu_exec(CPUState *env); | |
463 | ||
464 | static void *kvm_cpu_thread_fn(void *arg) | |
465 | { | |
466 | CPUState *env = arg; | |
467 | ||
6164e6d6 | 468 | qemu_mutex_lock(&qemu_global_mutex); |
296af7c9 BS |
469 | qemu_thread_self(env->thread); |
470 | if (kvm_enabled()) | |
471 | kvm_init_vcpu(env); | |
472 | ||
55541c8a | 473 | kvm_init_ipi(env); |
296af7c9 BS |
474 | |
475 | /* signal CPU creation */ | |
296af7c9 BS |
476 | env->created = 1; |
477 | qemu_cond_signal(&qemu_cpu_cond); | |
478 | ||
479 | /* and wait for machine initialization */ | |
480 | while (!qemu_system_ready) | |
481 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
482 | ||
483 | while (1) { | |
484 | if (cpu_can_run(env)) | |
485 | qemu_cpu_exec(env); | |
486 | qemu_kvm_wait_io_event(env); | |
487 | } | |
488 | ||
489 | return NULL; | |
490 | } | |
491 | ||
492 | static void *tcg_cpu_thread_fn(void *arg) | |
493 | { | |
494 | CPUState *env = arg; | |
495 | ||
55541c8a | 496 | tcg_init_ipi(); |
296af7c9 BS |
497 | qemu_thread_self(env->thread); |
498 | ||
499 | /* signal CPU creation */ | |
500 | qemu_mutex_lock(&qemu_global_mutex); | |
501 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
502 | env->created = 1; | |
503 | qemu_cond_signal(&qemu_cpu_cond); | |
504 | ||
505 | /* and wait for machine initialization */ | |
506 | while (!qemu_system_ready) | |
507 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
508 | ||
509 | while (1) { | |
510 | tcg_cpu_exec(); | |
6cabe1f3 | 511 | qemu_tcg_wait_io_event(); |
296af7c9 BS |
512 | } |
513 | ||
514 | return NULL; | |
515 | } | |
516 | ||
517 | void qemu_cpu_kick(void *_env) | |
518 | { | |
519 | CPUState *env = _env; | |
520 | qemu_cond_broadcast(env->halt_cond); | |
1fbb22e5 | 521 | qemu_thread_signal(env->thread, SIG_IPI); |
296af7c9 BS |
522 | } |
523 | ||
524 | int qemu_cpu_self(void *_env) | |
525 | { | |
526 | CPUState *env = _env; | |
527 | QemuThread this; | |
528 | ||
529 | qemu_thread_self(&this); | |
530 | ||
531 | return qemu_thread_equal(&this, env->thread); | |
532 | } | |
533 | ||
534 | static void cpu_signal(int sig) | |
535 | { | |
536 | if (cpu_single_env) | |
537 | cpu_exit(cpu_single_env); | |
1a28cac3 | 538 | exit_request = 1; |
296af7c9 BS |
539 | } |
540 | ||
55541c8a | 541 | static void tcg_init_ipi(void) |
296af7c9 BS |
542 | { |
543 | sigset_t set; | |
544 | struct sigaction sigact; | |
545 | ||
55541c8a PB |
546 | memset(&sigact, 0, sizeof(sigact)); |
547 | sigact.sa_handler = cpu_signal; | |
548 | sigaction(SIG_IPI, &sigact, NULL); | |
296af7c9 BS |
549 | |
550 | sigemptyset(&set); | |
551 | sigaddset(&set, SIG_IPI); | |
552 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
296af7c9 BS |
553 | } |
554 | ||
555 | static void dummy_signal(int sig) | |
556 | { | |
557 | } | |
558 | ||
55541c8a | 559 | static void kvm_init_ipi(CPUState *env) |
296af7c9 BS |
560 | { |
561 | int r; | |
562 | sigset_t set; | |
563 | struct sigaction sigact; | |
564 | ||
296af7c9 BS |
565 | memset(&sigact, 0, sizeof(sigact)); |
566 | sigact.sa_handler = dummy_signal; | |
567 | sigaction(SIG_IPI, &sigact, NULL); | |
568 | ||
55541c8a PB |
569 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
570 | sigdelset(&set, SIG_IPI); | |
296af7c9 BS |
571 | r = kvm_set_signal_mask(env, &set); |
572 | if (r) { | |
573 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r)); | |
574 | exit(1); | |
575 | } | |
576 | } | |
577 | ||
578 | static void unblock_io_signals(void) | |
579 | { | |
580 | sigset_t set; | |
581 | ||
582 | sigemptyset(&set); | |
583 | sigaddset(&set, SIGUSR2); | |
584 | sigaddset(&set, SIGIO); | |
585 | sigaddset(&set, SIGALRM); | |
586 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
587 | ||
588 | sigemptyset(&set); | |
589 | sigaddset(&set, SIG_IPI); | |
590 | pthread_sigmask(SIG_BLOCK, &set, NULL); | |
591 | } | |
592 | ||
296af7c9 BS |
593 | void qemu_mutex_lock_iothread(void) |
594 | { | |
595 | if (kvm_enabled()) { | |
596 | qemu_mutex_lock(&qemu_fair_mutex); | |
597 | qemu_mutex_lock(&qemu_global_mutex); | |
598 | qemu_mutex_unlock(&qemu_fair_mutex); | |
1a28cac3 MT |
599 | } else { |
600 | qemu_mutex_lock(&qemu_fair_mutex); | |
601 | if (qemu_mutex_trylock(&qemu_global_mutex)) { | |
602 | qemu_thread_signal(tcg_cpu_thread, SIG_IPI); | |
603 | qemu_mutex_lock(&qemu_global_mutex); | |
604 | } | |
605 | qemu_mutex_unlock(&qemu_fair_mutex); | |
606 | } | |
296af7c9 BS |
607 | } |
608 | ||
609 | void qemu_mutex_unlock_iothread(void) | |
610 | { | |
611 | qemu_mutex_unlock(&qemu_global_mutex); | |
612 | } | |
613 | ||
614 | static int all_vcpus_paused(void) | |
615 | { | |
616 | CPUState *penv = first_cpu; | |
617 | ||
618 | while (penv) { | |
619 | if (!penv->stopped) | |
620 | return 0; | |
621 | penv = (CPUState *)penv->next_cpu; | |
622 | } | |
623 | ||
624 | return 1; | |
625 | } | |
626 | ||
627 | void pause_all_vcpus(void) | |
628 | { | |
629 | CPUState *penv = first_cpu; | |
630 | ||
631 | while (penv) { | |
632 | penv->stop = 1; | |
296af7c9 BS |
633 | qemu_cpu_kick(penv); |
634 | penv = (CPUState *)penv->next_cpu; | |
635 | } | |
636 | ||
637 | while (!all_vcpus_paused()) { | |
638 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); | |
639 | penv = first_cpu; | |
640 | while (penv) { | |
1fbb22e5 | 641 | qemu_cpu_kick(penv); |
296af7c9 BS |
642 | penv = (CPUState *)penv->next_cpu; |
643 | } | |
644 | } | |
645 | } | |
646 | ||
647 | void resume_all_vcpus(void) | |
648 | { | |
649 | CPUState *penv = first_cpu; | |
650 | ||
651 | while (penv) { | |
652 | penv->stop = 0; | |
653 | penv->stopped = 0; | |
296af7c9 BS |
654 | qemu_cpu_kick(penv); |
655 | penv = (CPUState *)penv->next_cpu; | |
656 | } | |
657 | } | |
658 | ||
659 | static void tcg_init_vcpu(void *_env) | |
660 | { | |
661 | CPUState *env = _env; | |
662 | /* share a single thread for all cpus with TCG */ | |
663 | if (!tcg_cpu_thread) { | |
664 | env->thread = qemu_mallocz(sizeof(QemuThread)); | |
665 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
666 | qemu_cond_init(env->halt_cond); | |
667 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); | |
668 | while (env->created == 0) | |
669 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
670 | tcg_cpu_thread = env->thread; | |
671 | tcg_halt_cond = env->halt_cond; | |
672 | } else { | |
673 | env->thread = tcg_cpu_thread; | |
674 | env->halt_cond = tcg_halt_cond; | |
675 | } | |
676 | } | |
677 | ||
678 | static void kvm_start_vcpu(CPUState *env) | |
679 | { | |
680 | env->thread = qemu_mallocz(sizeof(QemuThread)); | |
681 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
682 | qemu_cond_init(env->halt_cond); | |
683 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); | |
684 | while (env->created == 0) | |
685 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
686 | } | |
687 | ||
688 | void qemu_init_vcpu(void *_env) | |
689 | { | |
690 | CPUState *env = _env; | |
691 | ||
692 | env->nr_cores = smp_cores; | |
693 | env->nr_threads = smp_threads; | |
694 | if (kvm_enabled()) | |
695 | kvm_start_vcpu(env); | |
696 | else | |
697 | tcg_init_vcpu(env); | |
698 | } | |
699 | ||
700 | void qemu_notify_event(void) | |
701 | { | |
702 | qemu_event_increment(); | |
703 | } | |
704 | ||
705 | static void qemu_system_vmstop_request(int reason) | |
706 | { | |
707 | vmstop_requested = reason; | |
708 | qemu_notify_event(); | |
709 | } | |
710 | ||
711 | void vm_stop(int reason) | |
712 | { | |
713 | QemuThread me; | |
714 | qemu_thread_self(&me); | |
715 | ||
716 | if (!qemu_thread_equal(&me, &io_thread)) { | |
717 | qemu_system_vmstop_request(reason); | |
718 | /* | |
719 | * FIXME: should not return to device code in case | |
720 | * vm_stop() has been requested. | |
721 | */ | |
722 | if (cpu_single_env) { | |
723 | cpu_exit(cpu_single_env); | |
724 | cpu_single_env->stop = 1; | |
725 | } | |
726 | return; | |
727 | } | |
728 | do_vm_stop(reason); | |
729 | } | |
730 | ||
731 | #endif | |
732 | ||
733 | static int qemu_cpu_exec(CPUState *env) | |
734 | { | |
735 | int ret; | |
736 | #ifdef CONFIG_PROFILER | |
737 | int64_t ti; | |
738 | #endif | |
739 | ||
740 | #ifdef CONFIG_PROFILER | |
741 | ti = profile_getclock(); | |
742 | #endif | |
743 | if (use_icount) { | |
744 | int64_t count; | |
745 | int decr; | |
746 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); | |
747 | env->icount_decr.u16.low = 0; | |
748 | env->icount_extra = 0; | |
749 | count = qemu_icount_round (qemu_next_deadline()); | |
750 | qemu_icount += count; | |
751 | decr = (count > 0xffff) ? 0xffff : count; | |
752 | count -= decr; | |
753 | env->icount_decr.u16.low = decr; | |
754 | env->icount_extra = count; | |
755 | } | |
756 | ret = cpu_exec(env); | |
757 | #ifdef CONFIG_PROFILER | |
758 | qemu_time += profile_getclock() - ti; | |
759 | #endif | |
760 | if (use_icount) { | |
761 | /* Fold pending instructions back into the | |
762 | instruction counter, and clear the interrupt flag. */ | |
763 | qemu_icount -= (env->icount_decr.u16.low | |
764 | + env->icount_extra); | |
765 | env->icount_decr.u32 = 0; | |
766 | env->icount_extra = 0; | |
767 | } | |
768 | return ret; | |
769 | } | |
770 | ||
771 | bool tcg_cpu_exec(void) | |
772 | { | |
773 | int ret = 0; | |
774 | ||
775 | if (next_cpu == NULL) | |
776 | next_cpu = first_cpu; | |
c629a4bc | 777 | for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) { |
345f4426 | 778 | CPUState *env = next_cpu; |
296af7c9 BS |
779 | |
780 | qemu_clock_enable(vm_clock, | |
345f4426 | 781 | (env->singlestep_enabled & SSTEP_NOTIMER) == 0); |
296af7c9 BS |
782 | |
783 | if (qemu_alarm_pending()) | |
784 | break; | |
785 | if (cpu_can_run(env)) | |
786 | ret = qemu_cpu_exec(env); | |
787 | else if (env->stop) | |
788 | break; | |
789 | ||
790 | if (ret == EXCP_DEBUG) { | |
791 | gdb_set_stop_cpu(env); | |
792 | debug_requested = EXCP_DEBUG; | |
793 | break; | |
794 | } | |
795 | } | |
c629a4bc | 796 | exit_request = 0; |
296af7c9 BS |
797 | return tcg_has_work(); |
798 | } | |
799 | ||
800 | void set_numa_modes(void) | |
801 | { | |
802 | CPUState *env; | |
803 | int i; | |
804 | ||
805 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
806 | for (i = 0; i < nb_numa_nodes; i++) { | |
807 | if (node_cpumask[i] & (1 << env->cpu_index)) { | |
808 | env->numa_node = i; | |
809 | } | |
810 | } | |
811 | } | |
812 | } | |
813 | ||
814 | void set_cpu_log(const char *optarg) | |
815 | { | |
816 | int mask; | |
817 | const CPULogItem *item; | |
818 | ||
819 | mask = cpu_str_to_log_mask(optarg); | |
820 | if (!mask) { | |
821 | printf("Log items (comma separated):\n"); | |
822 | for (item = cpu_log_items; item->mask != 0; item++) { | |
823 | printf("%-10s %s\n", item->name, item->help); | |
824 | } | |
825 | exit(1); | |
826 | } | |
827 | cpu_set_log(mask); | |
828 | } | |
29e922b6 BS |
829 | |
830 | /* Return the virtual CPU time, based on the instruction counter. */ | |
831 | int64_t cpu_get_icount(void) | |
832 | { | |
833 | int64_t icount; | |
834 | CPUState *env = cpu_single_env;; | |
835 | ||
836 | icount = qemu_icount; | |
837 | if (env) { | |
838 | if (!can_do_io(env)) { | |
839 | fprintf(stderr, "Bad clock read\n"); | |
840 | } | |
841 | icount -= (env->icount_decr.u16.low + env->icount_extra); | |
842 | } | |
843 | return qemu_icount_bias + (icount << icount_time_shift); | |
844 | } | |
262353cb BS |
845 | |
846 | void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...), | |
847 | const char *optarg) | |
848 | { | |
849 | /* XXX: implement xxx_cpu_list for targets that still miss it */ | |
850 | #if defined(cpu_list_id) | |
851 | cpu_list_id(f, cpu_fprintf, optarg); | |
852 | #elif defined(cpu_list) | |
853 | cpu_list(f, cpu_fprintf); /* deprecated */ | |
854 | #endif | |
855 | } |