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