Statistics
| Branch: | Revision:

root / cpus.c @ 6d9cb73c

History | View | Annotate | Download (24.2 kB)

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
#include "exec-all.h"
34

    
35
#include "cpus.h"
36
#include "compatfd.h"
37

    
38
#ifdef SIGRTMIN
39
#define SIG_IPI (SIGRTMIN+4)
40
#else
41
#define SIG_IPI SIGUSR1
42
#endif
43

    
44
#ifdef CONFIG_LINUX
45

    
46
#include <sys/prctl.h>
47

    
48
#ifndef PR_MCE_KILL
49
#define PR_MCE_KILL 33
50
#endif
51

    
52
#ifndef PR_MCE_KILL_SET
53
#define PR_MCE_KILL_SET 1
54
#endif
55

    
56
#ifndef PR_MCE_KILL_EARLY
57
#define PR_MCE_KILL_EARLY 1
58
#endif
59

    
60
#endif /* CONFIG_LINUX */
61

    
62
static CPUState *next_cpu;
63

    
64
/***********************************************************/
65
void hw_error(const char *fmt, ...)
66
{
67
    va_list ap;
68
    CPUState *env;
69

    
70
    va_start(ap, fmt);
71
    fprintf(stderr, "qemu: hardware error: ");
72
    vfprintf(stderr, fmt, ap);
73
    fprintf(stderr, "\n");
74
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
75
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
76
#ifdef TARGET_I386
77
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
78
#else
79
        cpu_dump_state(env, stderr, fprintf, 0);
80
#endif
81
    }
82
    va_end(ap);
83
    abort();
84
}
85

    
86
void cpu_synchronize_all_states(void)
87
{
88
    CPUState *cpu;
89

    
90
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
91
        cpu_synchronize_state(cpu);
92
    }
93
}
94

    
95
void cpu_synchronize_all_post_reset(void)
96
{
97
    CPUState *cpu;
98

    
99
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
100
        cpu_synchronize_post_reset(cpu);
101
    }
102
}
103

    
104
void cpu_synchronize_all_post_init(void)
105
{
106
    CPUState *cpu;
107

    
108
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
109
        cpu_synchronize_post_init(cpu);
110
    }
111
}
112

    
113
int cpu_is_stopped(CPUState *env)
114
{
115
    return !vm_running || env->stopped;
116
}
117

    
118
static void do_vm_stop(int reason)
119
{
120
    if (vm_running) {
121
        cpu_disable_ticks();
122
        vm_running = 0;
123
        pause_all_vcpus();
124
        vm_state_notify(0, reason);
125
        qemu_aio_flush();
126
        bdrv_flush_all();
127
        monitor_protocol_event(QEVENT_STOP, NULL);
128
    }
129
}
130

    
131
static int cpu_can_run(CPUState *env)
132
{
133
    if (env->stop)
134
        return 0;
135
    if (env->stopped || !vm_running)
136
        return 0;
137
    return 1;
138
}
139

    
140
static int cpu_has_work(CPUState *env)
141
{
142
    if (env->stop)
143
        return 1;
144
    if (env->queued_work_first)
145
        return 1;
146
    if (env->stopped || !vm_running)
147
        return 0;
148
    if (!env->halted)
149
        return 1;
150
    if (qemu_cpu_has_work(env))
151
        return 1;
152
    return 0;
153
}
154

    
155
static int any_cpu_has_work(void)
156
{
157
    CPUState *env;
158

    
159
    for (env = first_cpu; env != NULL; env = env->next_cpu)
160
        if (cpu_has_work(env))
161
            return 1;
162
    return 0;
163
}
164

    
165
static void cpu_debug_handler(CPUState *env)
166
{
167
    gdb_set_stop_cpu(env);
168
    debug_requested = EXCP_DEBUG;
169
    vm_stop(EXCP_DEBUG);
170
}
171

    
172
#ifdef CONFIG_LINUX
173
static void sigbus_reraise(void)
174
{
175
    sigset_t set;
176
    struct sigaction action;
177

    
178
    memset(&action, 0, sizeof(action));
179
    action.sa_handler = SIG_DFL;
180
    if (!sigaction(SIGBUS, &action, NULL)) {
181
        raise(SIGBUS);
182
        sigemptyset(&set);
183
        sigaddset(&set, SIGBUS);
184
        sigprocmask(SIG_UNBLOCK, &set, NULL);
185
    }
186
    perror("Failed to re-raise SIGBUS!\n");
187
    abort();
188
}
189

    
190
static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
191
                           void *ctx)
192
{
193
    if (kvm_on_sigbus(siginfo->ssi_code,
194
                      (void *)(intptr_t)siginfo->ssi_addr)) {
195
        sigbus_reraise();
196
    }
197
}
198

    
199
static void qemu_init_sigbus(void)
200
{
201
    struct sigaction action;
202

    
203
    memset(&action, 0, sizeof(action));
204
    action.sa_flags = SA_SIGINFO;
205
    action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
206
    sigaction(SIGBUS, &action, NULL);
207

    
208
    prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
209
}
210

    
211
#else /* !CONFIG_LINUX */
212

    
213
static void qemu_init_sigbus(void)
214
{
215
}
216
#endif /* !CONFIG_LINUX */
217

    
218
#ifndef _WIN32
219
static int io_thread_fd = -1;
220

    
221
static void qemu_event_increment(void)
222
{
223
    /* Write 8 bytes to be compatible with eventfd.  */
224
    static const uint64_t val = 1;
225
    ssize_t ret;
226

    
227
    if (io_thread_fd == -1)
228
        return;
229

    
230
    do {
231
        ret = write(io_thread_fd, &val, sizeof(val));
232
    } while (ret < 0 && errno == EINTR);
233

    
234
    /* EAGAIN is fine, a read must be pending.  */
235
    if (ret < 0 && errno != EAGAIN) {
236
        fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
237
                strerror(errno));
238
        exit (1);
239
    }
240
}
241

    
242
static void qemu_event_read(void *opaque)
243
{
244
    int fd = (unsigned long)opaque;
245
    ssize_t len;
246
    char buffer[512];
247

    
248
    /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
249
    do {
250
        len = read(fd, buffer, sizeof(buffer));
251
    } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
252
}
253

    
254
static int qemu_event_init(void)
255
{
256
    int err;
257
    int fds[2];
258

    
259
    err = qemu_eventfd(fds);
260
    if (err == -1)
261
        return -errno;
262

    
263
    err = fcntl_setfl(fds[0], O_NONBLOCK);
264
    if (err < 0)
265
        goto fail;
266

    
267
    err = fcntl_setfl(fds[1], O_NONBLOCK);
268
    if (err < 0)
269
        goto fail;
270

    
271
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
272
                         (void *)(unsigned long)fds[0]);
273

    
274
    io_thread_fd = fds[1];
275
    return 0;
276

    
277
fail:
278
    close(fds[0]);
279
    close(fds[1]);
280
    return err;
281
}
282

    
283
static void dummy_signal(int sig)
284
{
285
}
286

    
287
/* If we have signalfd, we mask out the signals we want to handle and then
288
 * use signalfd to listen for them.  We rely on whatever the current signal
289
 * handler is to dispatch the signals when we receive them.
290
 */
291
static void sigfd_handler(void *opaque)
292
{
293
    int fd = (unsigned long) opaque;
294
    struct qemu_signalfd_siginfo info;
295
    struct sigaction action;
296
    ssize_t len;
297

    
298
    while (1) {
299
        do {
300
            len = read(fd, &info, sizeof(info));
301
        } while (len == -1 && errno == EINTR);
302

    
303
        if (len == -1 && errno == EAGAIN) {
304
            break;
305
        }
306

    
307
        if (len != sizeof(info)) {
308
            printf("read from sigfd returned %zd: %m\n", len);
309
            return;
310
        }
311

    
312
        sigaction(info.ssi_signo, NULL, &action);
313
        if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
314
            action.sa_sigaction(info.ssi_signo,
315
                                (siginfo_t *)&info, NULL);
316
        } else if (action.sa_handler) {
317
            action.sa_handler(info.ssi_signo);
318
        }
319
    }
320
}
321

    
322
static int qemu_signalfd_init(sigset_t mask)
323
{
324
    int sigfd;
325

    
326
    sigfd = qemu_signalfd(&mask);
327
    if (sigfd == -1) {
328
        fprintf(stderr, "failed to create signalfd\n");
329
        return -errno;
330
    }
331

    
332
    fcntl_setfl(sigfd, O_NONBLOCK);
333

    
334
    qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
335
                         (void *)(unsigned long) sigfd);
336

    
337
    return 0;
338
}
339

    
340
static void qemu_kvm_eat_signals(CPUState *env)
341
{
342
    struct timespec ts = { 0, 0 };
343
    siginfo_t siginfo;
344
    sigset_t waitset;
345
    sigset_t chkset;
346
    int r;
347

    
348
    sigemptyset(&waitset);
349
    sigaddset(&waitset, SIG_IPI);
350
    sigaddset(&waitset, SIGBUS);
351

    
352
    do {
353
        r = sigtimedwait(&waitset, &siginfo, &ts);
354
        if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
355
            perror("sigtimedwait");
356
            exit(1);
357
        }
358

    
359
        switch (r) {
360
        case SIGBUS:
361
            if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
362
                sigbus_reraise();
363
            }
364
            break;
365
        default:
366
            break;
367
        }
368

    
369
        r = sigpending(&chkset);
370
        if (r == -1) {
371
            perror("sigpending");
372
            exit(1);
373
        }
374
    } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
375

    
376
#ifndef CONFIG_IOTHREAD
377
    if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
378
        qemu_notify_event();
379
    }
380
#endif
381
}
382

    
383
#else /* _WIN32 */
384

    
385
HANDLE qemu_event_handle;
386

    
387
static void dummy_event_handler(void *opaque)
388
{
389
}
390

    
391
static int qemu_event_init(void)
392
{
393
    qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
394
    if (!qemu_event_handle) {
395
        fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
396
        return -1;
397
    }
398
    qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
399
    return 0;
400
}
401

    
402
static void qemu_event_increment(void)
403
{
404
    if (!SetEvent(qemu_event_handle)) {
405
        fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
406
                GetLastError());
407
        exit (1);
408
    }
409
}
410

    
411
static void qemu_kvm_eat_signals(CPUState *env)
412
{
413
}
414
#endif /* _WIN32 */
415

    
416
#ifndef CONFIG_IOTHREAD
417
static void qemu_kvm_init_cpu_signals(CPUState *env)
418
{
419
#ifndef _WIN32
420
    int r;
421
    sigset_t set;
422
    struct sigaction sigact;
423

    
424
    memset(&sigact, 0, sizeof(sigact));
425
    sigact.sa_handler = dummy_signal;
426
    sigaction(SIG_IPI, &sigact, NULL);
427

    
428
    sigemptyset(&set);
429
    sigaddset(&set, SIG_IPI);
430
    sigaddset(&set, SIGIO);
431
    sigaddset(&set, SIGALRM);
432
    pthread_sigmask(SIG_BLOCK, &set, NULL);
433

    
434
    pthread_sigmask(SIG_BLOCK, NULL, &set);
435
    sigdelset(&set, SIG_IPI);
436
    sigdelset(&set, SIGBUS);
437
    sigdelset(&set, SIGIO);
438
    sigdelset(&set, SIGALRM);
439
    r = kvm_set_signal_mask(env, &set);
440
    if (r) {
441
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
442
        exit(1);
443
    }
444
#endif
445
}
446

    
447
#ifndef _WIN32
448
static sigset_t block_synchronous_signals(void)
449
{
450
    sigset_t set;
451

    
452
    sigemptyset(&set);
453
    sigaddset(&set, SIGBUS);
454
    if (kvm_enabled()) {
455
        /*
456
         * We need to process timer signals synchronously to avoid a race
457
         * between exit_request check and KVM vcpu entry.
458
         */
459
        sigaddset(&set, SIGIO);
460
        sigaddset(&set, SIGALRM);
461
    }
462

    
463
    return set;
464
}
465
#endif
466

    
467
int qemu_init_main_loop(void)
468
{
469
#ifndef _WIN32
470
    sigset_t blocked_signals;
471
    int ret;
472

    
473
    blocked_signals = block_synchronous_signals();
474

    
475
    ret = qemu_signalfd_init(blocked_signals);
476
    if (ret) {
477
        return ret;
478
    }
479
#endif
480
    cpu_set_debug_excp_handler(cpu_debug_handler);
481

    
482
    qemu_init_sigbus();
483

    
484
    return qemu_event_init();
485
}
486

    
487
void qemu_main_loop_start(void)
488
{
489
}
490

    
491
void qemu_init_vcpu(void *_env)
492
{
493
    CPUState *env = _env;
494
    int r;
495

    
496
    env->nr_cores = smp_cores;
497
    env->nr_threads = smp_threads;
498

    
499
    if (kvm_enabled()) {
500
        r = kvm_init_vcpu(env);
501
        if (r < 0) {
502
            fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
503
            exit(1);
504
        }
505
        qemu_kvm_init_cpu_signals(env);
506
    }
507
}
508

    
509
int qemu_cpu_self(void *env)
510
{
511
    return 1;
512
}
513

    
514
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
515
{
516
    func(data);
517
}
518

    
519
void resume_all_vcpus(void)
520
{
521
}
522

    
523
void pause_all_vcpus(void)
524
{
525
}
526

    
527
void qemu_cpu_kick(void *env)
528
{
529
    return;
530
}
531

    
532
void qemu_notify_event(void)
533
{
534
    CPUState *env = cpu_single_env;
535

    
536
    qemu_event_increment ();
537
    if (env) {
538
        cpu_exit(env);
539
    }
540
    if (next_cpu && env != next_cpu) {
541
        cpu_exit(next_cpu);
542
    }
543
    exit_request = 1;
544
}
545

    
546
void qemu_mutex_lock_iothread(void) {}
547
void qemu_mutex_unlock_iothread(void) {}
548

    
549
void cpu_stop_current(void)
550
{
551
}
552

    
553
void vm_stop(int reason)
554
{
555
    do_vm_stop(reason);
556
}
557

    
558
#else /* CONFIG_IOTHREAD */
559

    
560
#include "qemu-thread.h"
561

    
562
QemuMutex qemu_global_mutex;
563
static QemuMutex qemu_fair_mutex;
564

    
565
static QemuThread io_thread;
566

    
567
static QemuThread *tcg_cpu_thread;
568
static QemuCond *tcg_halt_cond;
569

    
570
static int qemu_system_ready;
571
/* cpu creation */
572
static QemuCond qemu_cpu_cond;
573
/* system init */
574
static QemuCond qemu_system_cond;
575
static QemuCond qemu_pause_cond;
576
static QemuCond qemu_work_cond;
577

    
578
static void cpu_signal(int sig)
579
{
580
    if (cpu_single_env) {
581
        cpu_exit(cpu_single_env);
582
    }
583
    exit_request = 1;
584
}
585

    
586
static void qemu_kvm_init_cpu_signals(CPUState *env)
587
{
588
    int r;
589
    sigset_t set;
590
    struct sigaction sigact;
591

    
592
    memset(&sigact, 0, sizeof(sigact));
593
    sigact.sa_handler = dummy_signal;
594
    sigaction(SIG_IPI, &sigact, NULL);
595

    
596
    pthread_sigmask(SIG_BLOCK, NULL, &set);
597
    sigdelset(&set, SIG_IPI);
598
    sigdelset(&set, SIGBUS);
599
    r = kvm_set_signal_mask(env, &set);
600
    if (r) {
601
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
602
        exit(1);
603
    }
604
}
605

    
606
static void qemu_tcg_init_cpu_signals(void)
607
{
608
    sigset_t set;
609
    struct sigaction sigact;
610

    
611
    memset(&sigact, 0, sizeof(sigact));
612
    sigact.sa_handler = cpu_signal;
613
    sigaction(SIG_IPI, &sigact, NULL);
614

    
615
    sigemptyset(&set);
616
    sigaddset(&set, SIG_IPI);
617
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
618
}
619

    
620
static sigset_t block_io_signals(void)
621
{
622
    sigset_t set;
623

    
624
    /* SIGUSR2 used by posix-aio-compat.c */
625
    sigemptyset(&set);
626
    sigaddset(&set, SIGUSR2);
627
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
628

    
629
    sigemptyset(&set);
630
    sigaddset(&set, SIGIO);
631
    sigaddset(&set, SIGALRM);
632
    sigaddset(&set, SIG_IPI);
633
    sigaddset(&set, SIGBUS);
634
    pthread_sigmask(SIG_BLOCK, &set, NULL);
635

    
636
    return set;
637
}
638

    
639
int qemu_init_main_loop(void)
640
{
641
    int ret;
642
    sigset_t blocked_signals;
643

    
644
    cpu_set_debug_excp_handler(cpu_debug_handler);
645

    
646
    qemu_init_sigbus();
647

    
648
    blocked_signals = block_io_signals();
649

    
650
    ret = qemu_signalfd_init(blocked_signals);
651
    if (ret)
652
        return ret;
653

    
654
    /* Note eventfd must be drained before signalfd handlers run */
655
    ret = qemu_event_init();
656
    if (ret)
657
        return ret;
658

    
659
    qemu_cond_init(&qemu_pause_cond);
660
    qemu_cond_init(&qemu_system_cond);
661
    qemu_mutex_init(&qemu_fair_mutex);
662
    qemu_mutex_init(&qemu_global_mutex);
663
    qemu_mutex_lock(&qemu_global_mutex);
664

    
665
    qemu_thread_self(&io_thread);
666

    
667
    return 0;
668
}
669

    
670
void qemu_main_loop_start(void)
671
{
672
    qemu_system_ready = 1;
673
    qemu_cond_broadcast(&qemu_system_cond);
674
}
675

    
676
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
677
{
678
    struct qemu_work_item wi;
679

    
680
    if (qemu_cpu_self(env)) {
681
        func(data);
682
        return;
683
    }
684

    
685
    wi.func = func;
686
    wi.data = data;
687
    if (!env->queued_work_first)
688
        env->queued_work_first = &wi;
689
    else
690
        env->queued_work_last->next = &wi;
691
    env->queued_work_last = &wi;
692
    wi.next = NULL;
693
    wi.done = false;
694

    
695
    qemu_cpu_kick(env);
696
    while (!wi.done) {
697
        CPUState *self_env = cpu_single_env;
698

    
699
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
700
        cpu_single_env = self_env;
701
    }
702
}
703

    
704
static void flush_queued_work(CPUState *env)
705
{
706
    struct qemu_work_item *wi;
707

    
708
    if (!env->queued_work_first)
709
        return;
710

    
711
    while ((wi = env->queued_work_first)) {
712
        env->queued_work_first = wi->next;
713
        wi->func(wi->data);
714
        wi->done = true;
715
    }
716
    env->queued_work_last = NULL;
717
    qemu_cond_broadcast(&qemu_work_cond);
718
}
719

    
720
static void qemu_wait_io_event_common(CPUState *env)
721
{
722
    if (env->stop) {
723
        env->stop = 0;
724
        env->stopped = 1;
725
        qemu_cond_signal(&qemu_pause_cond);
726
    }
727
    flush_queued_work(env);
728
    env->thread_kicked = false;
729
}
730

    
731
static void qemu_tcg_wait_io_event(void)
732
{
733
    CPUState *env;
734

    
735
    while (!any_cpu_has_work())
736
        qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
737

    
738
    qemu_mutex_unlock(&qemu_global_mutex);
739

    
740
    /*
741
     * Users of qemu_global_mutex can be starved, having no chance
742
     * to acquire it since this path will get to it first.
743
     * So use another lock to provide fairness.
744
     */
745
    qemu_mutex_lock(&qemu_fair_mutex);
746
    qemu_mutex_unlock(&qemu_fair_mutex);
747

    
748
    qemu_mutex_lock(&qemu_global_mutex);
749

    
750
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
751
        qemu_wait_io_event_common(env);
752
    }
753
}
754

    
755
static void qemu_kvm_wait_io_event(CPUState *env)
756
{
757
    while (!cpu_has_work(env))
758
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
759

    
760
    qemu_kvm_eat_signals(env);
761
    qemu_wait_io_event_common(env);
762
}
763

    
764
static int qemu_cpu_exec(CPUState *env);
765

    
766
static void *kvm_cpu_thread_fn(void *arg)
767
{
768
    CPUState *env = arg;
769
    int r;
770

    
771
    qemu_mutex_lock(&qemu_global_mutex);
772
    qemu_thread_self(env->thread);
773

    
774
    r = kvm_init_vcpu(env);
775
    if (r < 0) {
776
        fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
777
        exit(1);
778
    }
779

    
780
    qemu_kvm_init_cpu_signals(env);
781

    
782
    /* signal CPU creation */
783
    env->created = 1;
784
    qemu_cond_signal(&qemu_cpu_cond);
785

    
786
    /* and wait for machine initialization */
787
    while (!qemu_system_ready)
788
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
789

    
790
    while (1) {
791
        if (cpu_can_run(env))
792
            qemu_cpu_exec(env);
793
        qemu_kvm_wait_io_event(env);
794
    }
795

    
796
    return NULL;
797
}
798

    
799
static void *tcg_cpu_thread_fn(void *arg)
800
{
801
    CPUState *env = arg;
802

    
803
    qemu_tcg_init_cpu_signals();
804
    qemu_thread_self(env->thread);
805

    
806
    /* signal CPU creation */
807
    qemu_mutex_lock(&qemu_global_mutex);
808
    for (env = first_cpu; env != NULL; env = env->next_cpu)
809
        env->created = 1;
810
    qemu_cond_signal(&qemu_cpu_cond);
811

    
812
    /* and wait for machine initialization */
813
    while (!qemu_system_ready)
814
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
815

    
816
    while (1) {
817
        cpu_exec_all();
818
        qemu_tcg_wait_io_event();
819
    }
820

    
821
    return NULL;
822
}
823

    
824
void qemu_cpu_kick(void *_env)
825
{
826
    CPUState *env = _env;
827
    qemu_cond_broadcast(env->halt_cond);
828
    if (!env->thread_kicked) {
829
        qemu_thread_signal(env->thread, SIG_IPI);
830
        env->thread_kicked = true;
831
    }
832
}
833

    
834
int qemu_cpu_self(void *_env)
835
{
836
    CPUState *env = _env;
837
    QemuThread this;
838

    
839
    qemu_thread_self(&this);
840

    
841
    return qemu_thread_equal(&this, env->thread);
842
}
843

    
844
void qemu_mutex_lock_iothread(void)
845
{
846
    if (kvm_enabled()) {
847
        qemu_mutex_lock(&qemu_global_mutex);
848
    } else {
849
        qemu_mutex_lock(&qemu_fair_mutex);
850
        if (qemu_mutex_trylock(&qemu_global_mutex)) {
851
            qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
852
            qemu_mutex_lock(&qemu_global_mutex);
853
        }
854
        qemu_mutex_unlock(&qemu_fair_mutex);
855
    }
856
}
857

    
858
void qemu_mutex_unlock_iothread(void)
859
{
860
    qemu_mutex_unlock(&qemu_global_mutex);
861
}
862

    
863
static int all_vcpus_paused(void)
864
{
865
    CPUState *penv = first_cpu;
866

    
867
    while (penv) {
868
        if (!penv->stopped)
869
            return 0;
870
        penv = (CPUState *)penv->next_cpu;
871
    }
872

    
873
    return 1;
874
}
875

    
876
void pause_all_vcpus(void)
877
{
878
    CPUState *penv = first_cpu;
879

    
880
    while (penv) {
881
        penv->stop = 1;
882
        qemu_cpu_kick(penv);
883
        penv = (CPUState *)penv->next_cpu;
884
    }
885

    
886
    while (!all_vcpus_paused()) {
887
        qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
888
        penv = first_cpu;
889
        while (penv) {
890
            qemu_cpu_kick(penv);
891
            penv = (CPUState *)penv->next_cpu;
892
        }
893
    }
894
}
895

    
896
void resume_all_vcpus(void)
897
{
898
    CPUState *penv = first_cpu;
899

    
900
    while (penv) {
901
        penv->stop = 0;
902
        penv->stopped = 0;
903
        qemu_cpu_kick(penv);
904
        penv = (CPUState *)penv->next_cpu;
905
    }
906
}
907

    
908
static void tcg_init_vcpu(void *_env)
909
{
910
    CPUState *env = _env;
911
    /* share a single thread for all cpus with TCG */
912
    if (!tcg_cpu_thread) {
913
        env->thread = qemu_mallocz(sizeof(QemuThread));
914
        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
915
        qemu_cond_init(env->halt_cond);
916
        qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
917
        while (env->created == 0)
918
            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
919
        tcg_cpu_thread = env->thread;
920
        tcg_halt_cond = env->halt_cond;
921
    } else {
922
        env->thread = tcg_cpu_thread;
923
        env->halt_cond = tcg_halt_cond;
924
    }
925
}
926

    
927
static void kvm_start_vcpu(CPUState *env)
928
{
929
    env->thread = qemu_mallocz(sizeof(QemuThread));
930
    env->halt_cond = qemu_mallocz(sizeof(QemuCond));
931
    qemu_cond_init(env->halt_cond);
932
    qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
933
    while (env->created == 0)
934
        qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
935
}
936

    
937
void qemu_init_vcpu(void *_env)
938
{
939
    CPUState *env = _env;
940

    
941
    env->nr_cores = smp_cores;
942
    env->nr_threads = smp_threads;
943
    if (kvm_enabled())
944
        kvm_start_vcpu(env);
945
    else
946
        tcg_init_vcpu(env);
947
}
948

    
949
void qemu_notify_event(void)
950
{
951
    qemu_event_increment();
952
}
953

    
954
static void qemu_system_vmstop_request(int reason)
955
{
956
    vmstop_requested = reason;
957
    qemu_notify_event();
958
}
959

    
960
void cpu_stop_current(void)
961
{
962
    if (cpu_single_env) {
963
        cpu_single_env->stopped = 1;
964
        cpu_exit(cpu_single_env);
965
    }
966
}
967

    
968
void vm_stop(int reason)
969
{
970
    QemuThread me;
971
    qemu_thread_self(&me);
972

    
973
    if (!qemu_thread_equal(&me, &io_thread)) {
974
        qemu_system_vmstop_request(reason);
975
        /*
976
         * FIXME: should not return to device code in case
977
         * vm_stop() has been requested.
978
         */
979
        cpu_stop_current();
980
        return;
981
    }
982
    do_vm_stop(reason);
983
}
984

    
985
#endif
986

    
987
static int qemu_cpu_exec(CPUState *env)
988
{
989
    int ret;
990
#ifdef CONFIG_PROFILER
991
    int64_t ti;
992
#endif
993

    
994
#ifdef CONFIG_PROFILER
995
    ti = profile_getclock();
996
#endif
997
    if (use_icount) {
998
        int64_t count;
999
        int decr;
1000
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1001
        env->icount_decr.u16.low = 0;
1002
        env->icount_extra = 0;
1003
        count = qemu_icount_round (qemu_next_deadline());
1004
        qemu_icount += count;
1005
        decr = (count > 0xffff) ? 0xffff : count;
1006
        count -= decr;
1007
        env->icount_decr.u16.low = decr;
1008
        env->icount_extra = count;
1009
    }
1010
    ret = cpu_exec(env);
1011
#ifdef CONFIG_PROFILER
1012
    qemu_time += profile_getclock() - ti;
1013
#endif
1014
    if (use_icount) {
1015
        /* Fold pending instructions back into the
1016
           instruction counter, and clear the interrupt flag.  */
1017
        qemu_icount -= (env->icount_decr.u16.low
1018
                        + env->icount_extra);
1019
        env->icount_decr.u32 = 0;
1020
        env->icount_extra = 0;
1021
    }
1022
    return ret;
1023
}
1024

    
1025
bool cpu_exec_all(void)
1026
{
1027
    int r;
1028

    
1029
    if (next_cpu == NULL)
1030
        next_cpu = first_cpu;
1031
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1032
        CPUState *env = next_cpu;
1033

    
1034
        qemu_clock_enable(vm_clock,
1035
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1036

    
1037
        if (qemu_alarm_pending())
1038
            break;
1039
        if (cpu_can_run(env)) {
1040
            r = qemu_cpu_exec(env);
1041
            if (kvm_enabled()) {
1042
                qemu_kvm_eat_signals(env);
1043
            }
1044
            if (r == EXCP_DEBUG) {
1045
                break;
1046
            }
1047
        } else if (env->stop) {
1048
            break;
1049
        }
1050
    }
1051
    exit_request = 0;
1052
    return any_cpu_has_work();
1053
}
1054

    
1055
void set_numa_modes(void)
1056
{
1057
    CPUState *env;
1058
    int i;
1059

    
1060
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1061
        for (i = 0; i < nb_numa_nodes; i++) {
1062
            if (node_cpumask[i] & (1 << env->cpu_index)) {
1063
                env->numa_node = i;
1064
            }
1065
        }
1066
    }
1067
}
1068

    
1069
void set_cpu_log(const char *optarg)
1070
{
1071
    int mask;
1072
    const CPULogItem *item;
1073

    
1074
    mask = cpu_str_to_log_mask(optarg);
1075
    if (!mask) {
1076
        printf("Log items (comma separated):\n");
1077
        for (item = cpu_log_items; item->mask != 0; item++) {
1078
            printf("%-10s %s\n", item->name, item->help);
1079
        }
1080
        exit(1);
1081
    }
1082
    cpu_set_log(mask);
1083
}
1084

    
1085
/* Return the virtual CPU time, based on the instruction counter.  */
1086
int64_t cpu_get_icount(void)
1087
{
1088
    int64_t icount;
1089
    CPUState *env = cpu_single_env;;
1090

    
1091
    icount = qemu_icount;
1092
    if (env) {
1093
        if (!can_do_io(env)) {
1094
            fprintf(stderr, "Bad clock read\n");
1095
        }
1096
        icount -= (env->icount_decr.u16.low + env->icount_extra);
1097
    }
1098
    return qemu_icount_bias + (icount << icount_time_shift);
1099
}
1100

    
1101
void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1102
{
1103
    /* XXX: implement xxx_cpu_list for targets that still miss it */
1104
#if defined(cpu_list_id)
1105
    cpu_list_id(f, cpu_fprintf, optarg);
1106
#elif defined(cpu_list)
1107
    cpu_list(f, cpu_fprintf); /* deprecated */
1108
#endif
1109
}