Statistics
| Branch: | Revision:

root / cpus.c @ e07bbac5

History | View | Annotate | Download (24.9 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
    }
136
    if (env->stopped || !vm_running) {
137
        return 0;
138
    }
139
    return 1;
140
}
141

    
142
static bool cpu_thread_is_idle(CPUState *env)
143
{
144
    if (env->stop || env->queued_work_first) {
145
        return false;
146
    }
147
    if (env->stopped || !vm_running) {
148
        return true;
149
    }
150
    if (!env->halted || qemu_cpu_has_work(env)) {
151
        return false;
152
    }
153
    return true;
154
}
155

    
156
static bool all_cpu_threads_idle(void)
157
{
158
    CPUState *env;
159

    
160
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
161
        if (!cpu_thread_is_idle(env)) {
162
            return false;
163
        }
164
    }
165
    return true;
166
}
167

    
168
static void cpu_debug_handler(CPUState *env)
169
{
170
    gdb_set_stop_cpu(env);
171
    debug_requested = VMSTOP_DEBUG;
172
    vm_stop(VMSTOP_DEBUG);
173
}
174

    
175
#ifdef CONFIG_LINUX
176
static void sigbus_reraise(void)
177
{
178
    sigset_t set;
179
    struct sigaction action;
180

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

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

    
202
static void qemu_init_sigbus(void)
203
{
204
    struct sigaction action;
205

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

    
211
    prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
212
}
213

    
214
#else /* !CONFIG_LINUX */
215

    
216
static void qemu_init_sigbus(void)
217
{
218
}
219
#endif /* !CONFIG_LINUX */
220

    
221
#ifndef _WIN32
222
static int io_thread_fd = -1;
223

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

    
230
    if (io_thread_fd == -1) {
231
        return;
232
    }
233
    do {
234
        ret = write(io_thread_fd, &val, sizeof(val));
235
    } while (ret < 0 && errno == EINTR);
236

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

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

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

    
257
static int qemu_event_init(void)
258
{
259
    int err;
260
    int fds[2];
261

    
262
    err = qemu_eventfd(fds);
263
    if (err == -1) {
264
        return -errno;
265
    }
266
    err = fcntl_setfl(fds[0], O_NONBLOCK);
267
    if (err < 0) {
268
        goto fail;
269
    }
270
    err = fcntl_setfl(fds[1], O_NONBLOCK);
271
    if (err < 0) {
272
        goto fail;
273
    }
274
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
275
                         (void *)(unsigned long)fds[0]);
276

    
277
    io_thread_fd = fds[1];
278
    return 0;
279

    
280
fail:
281
    close(fds[0]);
282
    close(fds[1]);
283
    return err;
284
}
285

    
286
static void dummy_signal(int sig)
287
{
288
}
289

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

    
301
    while (1) {
302
        do {
303
            len = read(fd, &info, sizeof(info));
304
        } while (len == -1 && errno == EINTR);
305

    
306
        if (len == -1 && errno == EAGAIN) {
307
            break;
308
        }
309

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

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

    
325
static int qemu_signalfd_init(sigset_t mask)
326
{
327
    int sigfd;
328

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

    
335
    fcntl_setfl(sigfd, O_NONBLOCK);
336

    
337
    qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
338
                         (void *)(unsigned long) sigfd);
339

    
340
    return 0;
341
}
342

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

    
351
    sigemptyset(&waitset);
352
    sigaddset(&waitset, SIG_IPI);
353
    sigaddset(&waitset, SIGBUS);
354

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

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

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

    
379
#ifndef CONFIG_IOTHREAD
380
    if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
381
        qemu_notify_event();
382
    }
383
#endif
384
}
385

    
386
#else /* _WIN32 */
387

    
388
HANDLE qemu_event_handle;
389

    
390
static void dummy_event_handler(void *opaque)
391
{
392
}
393

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

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

    
414
static void qemu_kvm_eat_signals(CPUState *env)
415
{
416
}
417
#endif /* _WIN32 */
418

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

    
427
    memset(&sigact, 0, sizeof(sigact));
428
    sigact.sa_handler = dummy_signal;
429
    sigaction(SIG_IPI, &sigact, NULL);
430

    
431
    sigemptyset(&set);
432
    sigaddset(&set, SIG_IPI);
433
    sigaddset(&set, SIGIO);
434
    sigaddset(&set, SIGALRM);
435
    pthread_sigmask(SIG_BLOCK, &set, NULL);
436

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

    
450
#ifndef _WIN32
451
static sigset_t block_synchronous_signals(void)
452
{
453
    sigset_t set;
454

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

    
466
    return set;
467
}
468
#endif
469

    
470
int qemu_init_main_loop(void)
471
{
472
#ifndef _WIN32
473
    sigset_t blocked_signals;
474
    int ret;
475

    
476
    blocked_signals = block_synchronous_signals();
477

    
478
    ret = qemu_signalfd_init(blocked_signals);
479
    if (ret) {
480
        return ret;
481
    }
482
#endif
483
    cpu_set_debug_excp_handler(cpu_debug_handler);
484

    
485
    qemu_init_sigbus();
486

    
487
    return qemu_event_init();
488
}
489

    
490
void qemu_main_loop_start(void)
491
{
492
}
493

    
494
void qemu_init_vcpu(void *_env)
495
{
496
    CPUState *env = _env;
497
    int r;
498

    
499
    env->nr_cores = smp_cores;
500
    env->nr_threads = smp_threads;
501

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

    
512
int qemu_cpu_self(void *env)
513
{
514
    return 1;
515
}
516

    
517
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
518
{
519
    func(data);
520
}
521

    
522
void resume_all_vcpus(void)
523
{
524
}
525

    
526
void pause_all_vcpus(void)
527
{
528
}
529

    
530
void qemu_cpu_kick(void *env)
531
{
532
}
533

    
534
void qemu_cpu_kick_self(void)
535
{
536
#ifndef _WIN32
537
    assert(cpu_single_env);
538

    
539
    raise(SIG_IPI);
540
#else
541
    abort();
542
#endif
543
}
544

    
545
void qemu_notify_event(void)
546
{
547
    CPUState *env = cpu_single_env;
548

    
549
    qemu_event_increment ();
550
    if (env) {
551
        cpu_exit(env);
552
    }
553
    if (next_cpu && env != next_cpu) {
554
        cpu_exit(next_cpu);
555
    }
556
    exit_request = 1;
557
}
558

    
559
void qemu_mutex_lock_iothread(void) {}
560
void qemu_mutex_unlock_iothread(void) {}
561

    
562
void cpu_stop_current(void)
563
{
564
}
565

    
566
void vm_stop(int reason)
567
{
568
    do_vm_stop(reason);
569
}
570

    
571
#else /* CONFIG_IOTHREAD */
572

    
573
#include "qemu-thread.h"
574

    
575
QemuMutex qemu_global_mutex;
576
static QemuMutex qemu_fair_mutex;
577

    
578
static QemuThread io_thread;
579

    
580
static QemuThread *tcg_cpu_thread;
581
static QemuCond *tcg_halt_cond;
582

    
583
static int qemu_system_ready;
584
/* cpu creation */
585
static QemuCond qemu_cpu_cond;
586
/* system init */
587
static QemuCond qemu_system_cond;
588
static QemuCond qemu_pause_cond;
589
static QemuCond qemu_work_cond;
590

    
591
static void cpu_signal(int sig)
592
{
593
    if (cpu_single_env) {
594
        cpu_exit(cpu_single_env);
595
    }
596
    exit_request = 1;
597
}
598

    
599
static void qemu_kvm_init_cpu_signals(CPUState *env)
600
{
601
    int r;
602
    sigset_t set;
603
    struct sigaction sigact;
604

    
605
    memset(&sigact, 0, sizeof(sigact));
606
    sigact.sa_handler = dummy_signal;
607
    sigaction(SIG_IPI, &sigact, NULL);
608

    
609
    pthread_sigmask(SIG_BLOCK, NULL, &set);
610
    sigdelset(&set, SIG_IPI);
611
    sigdelset(&set, SIGBUS);
612
    r = kvm_set_signal_mask(env, &set);
613
    if (r) {
614
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
615
        exit(1);
616
    }
617
}
618

    
619
static void qemu_tcg_init_cpu_signals(void)
620
{
621
    sigset_t set;
622
    struct sigaction sigact;
623

    
624
    memset(&sigact, 0, sizeof(sigact));
625
    sigact.sa_handler = cpu_signal;
626
    sigaction(SIG_IPI, &sigact, NULL);
627

    
628
    sigemptyset(&set);
629
    sigaddset(&set, SIG_IPI);
630
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
631
}
632

    
633
static sigset_t block_io_signals(void)
634
{
635
    sigset_t set;
636

    
637
    /* SIGUSR2 used by posix-aio-compat.c */
638
    sigemptyset(&set);
639
    sigaddset(&set, SIGUSR2);
640
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
641

    
642
    sigemptyset(&set);
643
    sigaddset(&set, SIGIO);
644
    sigaddset(&set, SIGALRM);
645
    sigaddset(&set, SIG_IPI);
646
    sigaddset(&set, SIGBUS);
647
    pthread_sigmask(SIG_BLOCK, &set, NULL);
648

    
649
    return set;
650
}
651

    
652
int qemu_init_main_loop(void)
653
{
654
    int ret;
655
    sigset_t blocked_signals;
656

    
657
    cpu_set_debug_excp_handler(cpu_debug_handler);
658

    
659
    qemu_init_sigbus();
660

    
661
    blocked_signals = block_io_signals();
662

    
663
    ret = qemu_signalfd_init(blocked_signals);
664
    if (ret) {
665
        return ret;
666
    }
667

    
668
    /* Note eventfd must be drained before signalfd handlers run */
669
    ret = qemu_event_init();
670
    if (ret) {
671
        return ret;
672
    }
673

    
674
    qemu_cond_init(&qemu_pause_cond);
675
    qemu_cond_init(&qemu_system_cond);
676
    qemu_mutex_init(&qemu_fair_mutex);
677
    qemu_mutex_init(&qemu_global_mutex);
678
    qemu_mutex_lock(&qemu_global_mutex);
679

    
680
    qemu_thread_self(&io_thread);
681

    
682
    return 0;
683
}
684

    
685
void qemu_main_loop_start(void)
686
{
687
    qemu_system_ready = 1;
688
    qemu_cond_broadcast(&qemu_system_cond);
689
}
690

    
691
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
692
{
693
    struct qemu_work_item wi;
694

    
695
    if (qemu_cpu_self(env)) {
696
        func(data);
697
        return;
698
    }
699

    
700
    wi.func = func;
701
    wi.data = data;
702
    if (!env->queued_work_first) {
703
        env->queued_work_first = &wi;
704
    } else {
705
        env->queued_work_last->next = &wi;
706
    }
707
    env->queued_work_last = &wi;
708
    wi.next = NULL;
709
    wi.done = false;
710

    
711
    qemu_cpu_kick(env);
712
    while (!wi.done) {
713
        CPUState *self_env = cpu_single_env;
714

    
715
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
716
        cpu_single_env = self_env;
717
    }
718
}
719

    
720
static void flush_queued_work(CPUState *env)
721
{
722
    struct qemu_work_item *wi;
723

    
724
    if (!env->queued_work_first) {
725
        return;
726
    }
727

    
728
    while ((wi = env->queued_work_first)) {
729
        env->queued_work_first = wi->next;
730
        wi->func(wi->data);
731
        wi->done = true;
732
    }
733
    env->queued_work_last = NULL;
734
    qemu_cond_broadcast(&qemu_work_cond);
735
}
736

    
737
static void qemu_wait_io_event_common(CPUState *env)
738
{
739
    if (env->stop) {
740
        env->stop = 0;
741
        env->stopped = 1;
742
        qemu_cond_signal(&qemu_pause_cond);
743
    }
744
    flush_queued_work(env);
745
    env->thread_kicked = false;
746
}
747

    
748
static void qemu_tcg_wait_io_event(void)
749
{
750
    CPUState *env;
751

    
752
    while (all_cpu_threads_idle()) {
753
        qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
754
    }
755

    
756
    qemu_mutex_unlock(&qemu_global_mutex);
757

    
758
    /*
759
     * Users of qemu_global_mutex can be starved, having no chance
760
     * to acquire it since this path will get to it first.
761
     * So use another lock to provide fairness.
762
     */
763
    qemu_mutex_lock(&qemu_fair_mutex);
764
    qemu_mutex_unlock(&qemu_fair_mutex);
765

    
766
    qemu_mutex_lock(&qemu_global_mutex);
767

    
768
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
769
        qemu_wait_io_event_common(env);
770
    }
771
}
772

    
773
static void qemu_kvm_wait_io_event(CPUState *env)
774
{
775
    while (cpu_thread_is_idle(env)) {
776
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
777
    }
778

    
779
    qemu_kvm_eat_signals(env);
780
    qemu_wait_io_event_common(env);
781
}
782

    
783
static int qemu_cpu_exec(CPUState *env);
784

    
785
static void *qemu_kvm_cpu_thread_fn(void *arg)
786
{
787
    CPUState *env = arg;
788
    int r;
789

    
790
    qemu_mutex_lock(&qemu_global_mutex);
791
    qemu_thread_self(env->thread);
792

    
793
    r = kvm_init_vcpu(env);
794
    if (r < 0) {
795
        fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
796
        exit(1);
797
    }
798

    
799
    qemu_kvm_init_cpu_signals(env);
800

    
801
    /* signal CPU creation */
802
    env->created = 1;
803
    qemu_cond_signal(&qemu_cpu_cond);
804

    
805
    /* and wait for machine initialization */
806
    while (!qemu_system_ready) {
807
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
808
    }
809

    
810
    while (1) {
811
        if (cpu_can_run(env)) {
812
            qemu_cpu_exec(env);
813
        }
814
        qemu_kvm_wait_io_event(env);
815
    }
816

    
817
    return NULL;
818
}
819

    
820
static void *qemu_tcg_cpu_thread_fn(void *arg)
821
{
822
    CPUState *env = arg;
823

    
824
    qemu_tcg_init_cpu_signals();
825
    qemu_thread_self(env->thread);
826

    
827
    /* signal CPU creation */
828
    qemu_mutex_lock(&qemu_global_mutex);
829
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
830
        env->created = 1;
831
    }
832
    qemu_cond_signal(&qemu_cpu_cond);
833

    
834
    /* and wait for machine initialization */
835
    while (!qemu_system_ready) {
836
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
837
    }
838

    
839
    while (1) {
840
        cpu_exec_all();
841
        qemu_tcg_wait_io_event();
842
    }
843

    
844
    return NULL;
845
}
846

    
847
void qemu_cpu_kick(void *_env)
848
{
849
    CPUState *env = _env;
850

    
851
    qemu_cond_broadcast(env->halt_cond);
852
    if (!env->thread_kicked) {
853
        qemu_thread_signal(env->thread, SIG_IPI);
854
        env->thread_kicked = true;
855
    }
856
}
857

    
858
void qemu_cpu_kick_self(void)
859
{
860
    assert(cpu_single_env);
861

    
862
    if (!cpu_single_env->thread_kicked) {
863
        qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
864
        cpu_single_env->thread_kicked = true;
865
    }
866
}
867

    
868
int qemu_cpu_self(void *_env)
869
{
870
    CPUState *env = _env;
871
    QemuThread this;
872

    
873
    qemu_thread_self(&this);
874

    
875
    return qemu_thread_equal(&this, env->thread);
876
}
877

    
878
void qemu_mutex_lock_iothread(void)
879
{
880
    if (kvm_enabled()) {
881
        qemu_mutex_lock(&qemu_global_mutex);
882
    } else {
883
        qemu_mutex_lock(&qemu_fair_mutex);
884
        if (qemu_mutex_trylock(&qemu_global_mutex)) {
885
            qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
886
            qemu_mutex_lock(&qemu_global_mutex);
887
        }
888
        qemu_mutex_unlock(&qemu_fair_mutex);
889
    }
890
}
891

    
892
void qemu_mutex_unlock_iothread(void)
893
{
894
    qemu_mutex_unlock(&qemu_global_mutex);
895
}
896

    
897
static int all_vcpus_paused(void)
898
{
899
    CPUState *penv = first_cpu;
900

    
901
    while (penv) {
902
        if (!penv->stopped) {
903
            return 0;
904
        }
905
        penv = (CPUState *)penv->next_cpu;
906
    }
907

    
908
    return 1;
909
}
910

    
911
void pause_all_vcpus(void)
912
{
913
    CPUState *penv = first_cpu;
914

    
915
    while (penv) {
916
        penv->stop = 1;
917
        qemu_cpu_kick(penv);
918
        penv = (CPUState *)penv->next_cpu;
919
    }
920

    
921
    while (!all_vcpus_paused()) {
922
        qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
923
        penv = first_cpu;
924
        while (penv) {
925
            qemu_cpu_kick(penv);
926
            penv = (CPUState *)penv->next_cpu;
927
        }
928
    }
929
}
930

    
931
void resume_all_vcpus(void)
932
{
933
    CPUState *penv = first_cpu;
934

    
935
    while (penv) {
936
        penv->stop = 0;
937
        penv->stopped = 0;
938
        qemu_cpu_kick(penv);
939
        penv = (CPUState *)penv->next_cpu;
940
    }
941
}
942

    
943
static void qemu_tcg_init_vcpu(void *_env)
944
{
945
    CPUState *env = _env;
946

    
947
    /* share a single thread for all cpus with TCG */
948
    if (!tcg_cpu_thread) {
949
        env->thread = qemu_mallocz(sizeof(QemuThread));
950
        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
951
        qemu_cond_init(env->halt_cond);
952
        qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
953
        while (env->created == 0) {
954
            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
955
        }
956
        tcg_cpu_thread = env->thread;
957
        tcg_halt_cond = env->halt_cond;
958
    } else {
959
        env->thread = tcg_cpu_thread;
960
        env->halt_cond = tcg_halt_cond;
961
    }
962
}
963

    
964
static void qemu_kvm_start_vcpu(CPUState *env)
965
{
966
    env->thread = qemu_mallocz(sizeof(QemuThread));
967
    env->halt_cond = qemu_mallocz(sizeof(QemuCond));
968
    qemu_cond_init(env->halt_cond);
969
    qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
970
    while (env->created == 0) {
971
        qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
972
    }
973
}
974

    
975
void qemu_init_vcpu(void *_env)
976
{
977
    CPUState *env = _env;
978

    
979
    env->nr_cores = smp_cores;
980
    env->nr_threads = smp_threads;
981
    if (kvm_enabled()) {
982
        qemu_kvm_start_vcpu(env);
983
    } else {
984
        qemu_tcg_init_vcpu(env);
985
    }
986
}
987

    
988
void qemu_notify_event(void)
989
{
990
    qemu_event_increment();
991
}
992

    
993
static void qemu_system_vmstop_request(int reason)
994
{
995
    vmstop_requested = reason;
996
    qemu_notify_event();
997
}
998

    
999
void cpu_stop_current(void)
1000
{
1001
    if (cpu_single_env) {
1002
        cpu_single_env->stopped = 1;
1003
        cpu_exit(cpu_single_env);
1004
    }
1005
}
1006

    
1007
void vm_stop(int reason)
1008
{
1009
    QemuThread me;
1010
    qemu_thread_self(&me);
1011

    
1012
    if (!qemu_thread_equal(&me, &io_thread)) {
1013
        qemu_system_vmstop_request(reason);
1014
        /*
1015
         * FIXME: should not return to device code in case
1016
         * vm_stop() has been requested.
1017
         */
1018
        cpu_stop_current();
1019
        return;
1020
    }
1021
    do_vm_stop(reason);
1022
}
1023

    
1024
#endif
1025

    
1026
static int qemu_cpu_exec(CPUState *env)
1027
{
1028
    int ret;
1029
#ifdef CONFIG_PROFILER
1030
    int64_t ti;
1031
#endif
1032

    
1033
#ifdef CONFIG_PROFILER
1034
    ti = profile_getclock();
1035
#endif
1036
    if (use_icount) {
1037
        int64_t count;
1038
        int decr;
1039
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1040
        env->icount_decr.u16.low = 0;
1041
        env->icount_extra = 0;
1042
        count = qemu_icount_round (qemu_next_deadline());
1043
        qemu_icount += count;
1044
        decr = (count > 0xffff) ? 0xffff : count;
1045
        count -= decr;
1046
        env->icount_decr.u16.low = decr;
1047
        env->icount_extra = count;
1048
    }
1049
    ret = cpu_exec(env);
1050
#ifdef CONFIG_PROFILER
1051
    qemu_time += profile_getclock() - ti;
1052
#endif
1053
    if (use_icount) {
1054
        /* Fold pending instructions back into the
1055
           instruction counter, and clear the interrupt flag.  */
1056
        qemu_icount -= (env->icount_decr.u16.low
1057
                        + env->icount_extra);
1058
        env->icount_decr.u32 = 0;
1059
        env->icount_extra = 0;
1060
    }
1061
    return ret;
1062
}
1063

    
1064
bool cpu_exec_all(void)
1065
{
1066
    int r;
1067

    
1068
    if (next_cpu == NULL) {
1069
        next_cpu = first_cpu;
1070
    }
1071
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1072
        CPUState *env = next_cpu;
1073

    
1074
        qemu_clock_enable(vm_clock,
1075
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1076

    
1077
        if (qemu_alarm_pending()) {
1078
            break;
1079
        }
1080
        if (cpu_can_run(env)) {
1081
            r = qemu_cpu_exec(env);
1082
            if (kvm_enabled()) {
1083
                qemu_kvm_eat_signals(env);
1084
            }
1085
            if (r == EXCP_DEBUG) {
1086
                break;
1087
            }
1088
        } else if (env->stop) {
1089
            break;
1090
        }
1091
    }
1092
    exit_request = 0;
1093
    return !all_cpu_threads_idle();
1094
}
1095

    
1096
void set_numa_modes(void)
1097
{
1098
    CPUState *env;
1099
    int i;
1100

    
1101
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1102
        for (i = 0; i < nb_numa_nodes; i++) {
1103
            if (node_cpumask[i] & (1 << env->cpu_index)) {
1104
                env->numa_node = i;
1105
            }
1106
        }
1107
    }
1108
}
1109

    
1110
void set_cpu_log(const char *optarg)
1111
{
1112
    int mask;
1113
    const CPULogItem *item;
1114

    
1115
    mask = cpu_str_to_log_mask(optarg);
1116
    if (!mask) {
1117
        printf("Log items (comma separated):\n");
1118
        for (item = cpu_log_items; item->mask != 0; item++) {
1119
            printf("%-10s %s\n", item->name, item->help);
1120
        }
1121
        exit(1);
1122
    }
1123
    cpu_set_log(mask);
1124
}
1125

    
1126
/* Return the virtual CPU time, based on the instruction counter.  */
1127
int64_t cpu_get_icount(void)
1128
{
1129
    int64_t icount;
1130
    CPUState *env = cpu_single_env;;
1131

    
1132
    icount = qemu_icount;
1133
    if (env) {
1134
        if (!can_do_io(env)) {
1135
            fprintf(stderr, "Bad clock read\n");
1136
        }
1137
        icount -= (env->icount_decr.u16.low + env->icount_extra);
1138
    }
1139
    return qemu_icount_bias + (icount << icount_time_shift);
1140
}
1141

    
1142
void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1143
{
1144
    /* XXX: implement xxx_cpu_list for targets that still miss it */
1145
#if defined(cpu_list_id)
1146
    cpu_list_id(f, cpu_fprintf, optarg);
1147
#elif defined(cpu_list)
1148
    cpu_list(f, cpu_fprintf); /* deprecated */
1149
#endif
1150
}