Statistics
| Branch: | Revision:

root / cpus.c @ d0f294ce

History | View | Annotate | Download (23.4 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
#ifdef CONFIG_LINUX
38
#include <sys/prctl.h>
39
#endif
40

    
41
#ifdef SIGRTMIN
42
#define SIG_IPI (SIGRTMIN+4)
43
#else
44
#define SIG_IPI SIGUSR1
45
#endif
46

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

    
51
static CPUState *next_cpu;
52

    
53
/***********************************************************/
54
void hw_error(const char *fmt, ...)
55
{
56
    va_list ap;
57
    CPUState *env;
58

    
59
    va_start(ap, fmt);
60
    fprintf(stderr, "qemu: hardware error: ");
61
    vfprintf(stderr, fmt, ap);
62
    fprintf(stderr, "\n");
63
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
64
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
65
#ifdef TARGET_I386
66
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
67
#else
68
        cpu_dump_state(env, stderr, fprintf, 0);
69
#endif
70
    }
71
    va_end(ap);
72
    abort();
73
}
74

    
75
void cpu_synchronize_all_states(void)
76
{
77
    CPUState *cpu;
78

    
79
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
80
        cpu_synchronize_state(cpu);
81
    }
82
}
83

    
84
void cpu_synchronize_all_post_reset(void)
85
{
86
    CPUState *cpu;
87

    
88
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
89
        cpu_synchronize_post_reset(cpu);
90
    }
91
}
92

    
93
void cpu_synchronize_all_post_init(void)
94
{
95
    CPUState *cpu;
96

    
97
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
98
        cpu_synchronize_post_init(cpu);
99
    }
100
}
101

    
102
int cpu_is_stopped(CPUState *env)
103
{
104
    return !vm_running || env->stopped;
105
}
106

    
107
static void do_vm_stop(int reason)
108
{
109
    if (vm_running) {
110
        cpu_disable_ticks();
111
        vm_running = 0;
112
        pause_all_vcpus();
113
        vm_state_notify(0, reason);
114
        qemu_aio_flush();
115
        bdrv_flush_all();
116
        monitor_protocol_event(QEVENT_STOP, NULL);
117
    }
118
}
119

    
120
static int cpu_can_run(CPUState *env)
121
{
122
    if (env->stop)
123
        return 0;
124
    if (env->stopped || !vm_running)
125
        return 0;
126
    return 1;
127
}
128

    
129
static int cpu_has_work(CPUState *env)
130
{
131
    if (env->stop)
132
        return 1;
133
    if (env->queued_work_first)
134
        return 1;
135
    if (env->stopped || !vm_running)
136
        return 0;
137
    if (!env->halted)
138
        return 1;
139
    if (qemu_cpu_has_work(env))
140
        return 1;
141
    return 0;
142
}
143

    
144
static int any_cpu_has_work(void)
145
{
146
    CPUState *env;
147

    
148
    for (env = first_cpu; env != NULL; env = env->next_cpu)
149
        if (cpu_has_work(env))
150
            return 1;
151
    return 0;
152
}
153

    
154
static void cpu_debug_handler(CPUState *env)
155
{
156
    gdb_set_stop_cpu(env);
157
    debug_requested = EXCP_DEBUG;
158
    vm_stop(EXCP_DEBUG);
159
}
160

    
161
#ifndef _WIN32
162
static int io_thread_fd = -1;
163

    
164
static void qemu_event_increment(void)
165
{
166
    /* Write 8 bytes to be compatible with eventfd.  */
167
    static const uint64_t val = 1;
168
    ssize_t ret;
169

    
170
    if (io_thread_fd == -1)
171
        return;
172

    
173
    do {
174
        ret = write(io_thread_fd, &val, sizeof(val));
175
    } while (ret < 0 && errno == EINTR);
176

    
177
    /* EAGAIN is fine, a read must be pending.  */
178
    if (ret < 0 && errno != EAGAIN) {
179
        fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
180
                strerror(errno));
181
        exit (1);
182
    }
183
}
184

    
185
static void qemu_event_read(void *opaque)
186
{
187
    int fd = (unsigned long)opaque;
188
    ssize_t len;
189
    char buffer[512];
190

    
191
    /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
192
    do {
193
        len = read(fd, buffer, sizeof(buffer));
194
    } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
195
}
196

    
197
static int qemu_event_init(void)
198
{
199
    int err;
200
    int fds[2];
201

    
202
    err = qemu_eventfd(fds);
203
    if (err == -1)
204
        return -errno;
205

    
206
    err = fcntl_setfl(fds[0], O_NONBLOCK);
207
    if (err < 0)
208
        goto fail;
209

    
210
    err = fcntl_setfl(fds[1], O_NONBLOCK);
211
    if (err < 0)
212
        goto fail;
213

    
214
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
215
                         (void *)(unsigned long)fds[0]);
216

    
217
    io_thread_fd = fds[1];
218
    return 0;
219

    
220
fail:
221
    close(fds[0]);
222
    close(fds[1]);
223
    return err;
224
}
225

    
226
static void dummy_signal(int sig)
227
{
228
}
229

    
230
/* If we have signalfd, we mask out the signals we want to handle and then
231
 * use signalfd to listen for them.  We rely on whatever the current signal
232
 * handler is to dispatch the signals when we receive them.
233
 */
234
static void sigfd_handler(void *opaque)
235
{
236
    int fd = (unsigned long) opaque;
237
    struct qemu_signalfd_siginfo info;
238
    struct sigaction action;
239
    ssize_t len;
240

    
241
    while (1) {
242
        do {
243
            len = read(fd, &info, sizeof(info));
244
        } while (len == -1 && errno == EINTR);
245

    
246
        if (len == -1 && errno == EAGAIN) {
247
            break;
248
        }
249

    
250
        if (len != sizeof(info)) {
251
            printf("read from sigfd returned %zd: %m\n", len);
252
            return;
253
        }
254

    
255
        sigaction(info.ssi_signo, NULL, &action);
256
        if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
257
            action.sa_sigaction(info.ssi_signo,
258
                                (siginfo_t *)&info, NULL);
259
        } else if (action.sa_handler) {
260
            action.sa_handler(info.ssi_signo);
261
        }
262
    }
263
}
264

    
265
static int qemu_signalfd_init(sigset_t mask)
266
{
267
    int sigfd;
268

    
269
    sigfd = qemu_signalfd(&mask);
270
    if (sigfd == -1) {
271
        fprintf(stderr, "failed to create signalfd\n");
272
        return -errno;
273
    }
274

    
275
    fcntl_setfl(sigfd, O_NONBLOCK);
276

    
277
    qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
278
                         (void *)(unsigned long) sigfd);
279

    
280
    return 0;
281
}
282

    
283
static void sigbus_reraise(void);
284

    
285
static void qemu_kvm_eat_signals(CPUState *env)
286
{
287
    struct timespec ts = { 0, 0 };
288
    siginfo_t siginfo;
289
    sigset_t waitset;
290
    sigset_t chkset;
291
    int r;
292

    
293
    sigemptyset(&waitset);
294
    sigaddset(&waitset, SIG_IPI);
295
    sigaddset(&waitset, SIGBUS);
296

    
297
    do {
298
        r = sigtimedwait(&waitset, &siginfo, &ts);
299
        if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
300
            perror("sigtimedwait");
301
            exit(1);
302
        }
303

    
304
        switch (r) {
305
#ifdef CONFIG_IOTHREAD
306
        case SIGBUS:
307
            if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
308
                sigbus_reraise();
309
            }
310
            break;
311
#endif
312
        default:
313
            break;
314
        }
315

    
316
        r = sigpending(&chkset);
317
        if (r == -1) {
318
            perror("sigpending");
319
            exit(1);
320
        }
321
    } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
322
}
323

    
324
#else /* _WIN32 */
325

    
326
HANDLE qemu_event_handle;
327

    
328
static void dummy_event_handler(void *opaque)
329
{
330
}
331

    
332
static int qemu_event_init(void)
333
{
334
    qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
335
    if (!qemu_event_handle) {
336
        fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
337
        return -1;
338
    }
339
    qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
340
    return 0;
341
}
342

    
343
static void qemu_event_increment(void)
344
{
345
    if (!SetEvent(qemu_event_handle)) {
346
        fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
347
                GetLastError());
348
        exit (1);
349
    }
350
}
351

    
352
static void qemu_kvm_eat_signals(CPUState *env)
353
{
354
}
355
#endif /* _WIN32 */
356

    
357
#ifndef CONFIG_IOTHREAD
358
static void qemu_kvm_init_cpu_signals(CPUState *env)
359
{
360
#ifndef _WIN32
361
    int r;
362
    sigset_t set;
363
    struct sigaction sigact;
364

    
365
    memset(&sigact, 0, sizeof(sigact));
366
    sigact.sa_handler = dummy_signal;
367
    sigaction(SIG_IPI, &sigact, NULL);
368

    
369
    sigemptyset(&set);
370
    sigaddset(&set, SIG_IPI);
371
    pthread_sigmask(SIG_BLOCK, &set, NULL);
372

    
373
    pthread_sigmask(SIG_BLOCK, NULL, &set);
374
    sigdelset(&set, SIG_IPI);
375
    sigdelset(&set, SIGBUS);
376
    r = kvm_set_signal_mask(env, &set);
377
    if (r) {
378
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
379
        exit(1);
380
    }
381
#endif
382
}
383

    
384
int qemu_init_main_loop(void)
385
{
386
#ifndef _WIN32
387
    sigset_t blocked_signals;
388
    int ret;
389

    
390
    sigemptyset(&blocked_signals);
391

    
392
    ret = qemu_signalfd_init(blocked_signals);
393
    if (ret) {
394
        return ret;
395
    }
396
#endif
397
    cpu_set_debug_excp_handler(cpu_debug_handler);
398

    
399
    return qemu_event_init();
400
}
401

    
402
void qemu_main_loop_start(void)
403
{
404
}
405

    
406
void qemu_init_vcpu(void *_env)
407
{
408
    CPUState *env = _env;
409
    int r;
410

    
411
    env->nr_cores = smp_cores;
412
    env->nr_threads = smp_threads;
413

    
414
    if (kvm_enabled()) {
415
        r = kvm_init_vcpu(env);
416
        if (r < 0) {
417
            fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
418
            exit(1);
419
        }
420
        qemu_kvm_init_cpu_signals(env);
421
    }
422
}
423

    
424
int qemu_cpu_self(void *env)
425
{
426
    return 1;
427
}
428

    
429
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
430
{
431
    func(data);
432
}
433

    
434
void resume_all_vcpus(void)
435
{
436
}
437

    
438
void pause_all_vcpus(void)
439
{
440
}
441

    
442
void qemu_cpu_kick(void *env)
443
{
444
    return;
445
}
446

    
447
void qemu_notify_event(void)
448
{
449
    CPUState *env = cpu_single_env;
450

    
451
    qemu_event_increment ();
452
    if (env) {
453
        cpu_exit(env);
454
    }
455
    if (next_cpu && env != next_cpu) {
456
        cpu_exit(next_cpu);
457
    }
458
    exit_request = 1;
459
}
460

    
461
void qemu_mutex_lock_iothread(void) {}
462
void qemu_mutex_unlock_iothread(void) {}
463

    
464
void cpu_stop_current(void)
465
{
466
}
467

    
468
void vm_stop(int reason)
469
{
470
    do_vm_stop(reason);
471
}
472

    
473
#else /* CONFIG_IOTHREAD */
474

    
475
#include "qemu-thread.h"
476

    
477
QemuMutex qemu_global_mutex;
478
static QemuMutex qemu_fair_mutex;
479

    
480
static QemuThread io_thread;
481

    
482
static QemuThread *tcg_cpu_thread;
483
static QemuCond *tcg_halt_cond;
484

    
485
static int qemu_system_ready;
486
/* cpu creation */
487
static QemuCond qemu_cpu_cond;
488
/* system init */
489
static QemuCond qemu_system_cond;
490
static QemuCond qemu_pause_cond;
491
static QemuCond qemu_work_cond;
492

    
493
static void cpu_signal(int sig)
494
{
495
    if (cpu_single_env) {
496
        cpu_exit(cpu_single_env);
497
    }
498
    exit_request = 1;
499
}
500

    
501
static void qemu_kvm_init_cpu_signals(CPUState *env)
502
{
503
    int r;
504
    sigset_t set;
505
    struct sigaction sigact;
506

    
507
    memset(&sigact, 0, sizeof(sigact));
508
    sigact.sa_handler = dummy_signal;
509
    sigaction(SIG_IPI, &sigact, NULL);
510

    
511
    pthread_sigmask(SIG_BLOCK, NULL, &set);
512
    sigdelset(&set, SIG_IPI);
513
    sigdelset(&set, SIGBUS);
514
    r = kvm_set_signal_mask(env, &set);
515
    if (r) {
516
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
517
        exit(1);
518
    }
519
}
520

    
521
static void qemu_tcg_init_cpu_signals(void)
522
{
523
    sigset_t set;
524
    struct sigaction sigact;
525

    
526
    memset(&sigact, 0, sizeof(sigact));
527
    sigact.sa_handler = cpu_signal;
528
    sigaction(SIG_IPI, &sigact, NULL);
529

    
530
    sigemptyset(&set);
531
    sigaddset(&set, SIG_IPI);
532
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
533
}
534

    
535
static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
536
                           void *ctx);
537

    
538
static sigset_t block_io_signals(void)
539
{
540
    sigset_t set;
541
    struct sigaction action;
542

    
543
    /* SIGUSR2 used by posix-aio-compat.c */
544
    sigemptyset(&set);
545
    sigaddset(&set, SIGUSR2);
546
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
547

    
548
    sigemptyset(&set);
549
    sigaddset(&set, SIGIO);
550
    sigaddset(&set, SIGALRM);
551
    sigaddset(&set, SIG_IPI);
552
    sigaddset(&set, SIGBUS);
553
    pthread_sigmask(SIG_BLOCK, &set, NULL);
554

    
555
    memset(&action, 0, sizeof(action));
556
    action.sa_flags = SA_SIGINFO;
557
    action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
558
    sigaction(SIGBUS, &action, NULL);
559
    prctl(PR_MCE_KILL, 1, 1, 0, 0);
560

    
561
    return set;
562
}
563

    
564
int qemu_init_main_loop(void)
565
{
566
    int ret;
567
    sigset_t blocked_signals;
568

    
569
    cpu_set_debug_excp_handler(cpu_debug_handler);
570

    
571
    blocked_signals = block_io_signals();
572

    
573
    ret = qemu_signalfd_init(blocked_signals);
574
    if (ret)
575
        return ret;
576

    
577
    /* Note eventfd must be drained before signalfd handlers run */
578
    ret = qemu_event_init();
579
    if (ret)
580
        return ret;
581

    
582
    qemu_cond_init(&qemu_pause_cond);
583
    qemu_cond_init(&qemu_system_cond);
584
    qemu_mutex_init(&qemu_fair_mutex);
585
    qemu_mutex_init(&qemu_global_mutex);
586
    qemu_mutex_lock(&qemu_global_mutex);
587

    
588
    qemu_thread_self(&io_thread);
589

    
590
    return 0;
591
}
592

    
593
void qemu_main_loop_start(void)
594
{
595
    qemu_system_ready = 1;
596
    qemu_cond_broadcast(&qemu_system_cond);
597
}
598

    
599
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
600
{
601
    struct qemu_work_item wi;
602

    
603
    if (qemu_cpu_self(env)) {
604
        func(data);
605
        return;
606
    }
607

    
608
    wi.func = func;
609
    wi.data = data;
610
    if (!env->queued_work_first)
611
        env->queued_work_first = &wi;
612
    else
613
        env->queued_work_last->next = &wi;
614
    env->queued_work_last = &wi;
615
    wi.next = NULL;
616
    wi.done = false;
617

    
618
    qemu_cpu_kick(env);
619
    while (!wi.done) {
620
        CPUState *self_env = cpu_single_env;
621

    
622
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
623
        cpu_single_env = self_env;
624
    }
625
}
626

    
627
static void flush_queued_work(CPUState *env)
628
{
629
    struct qemu_work_item *wi;
630

    
631
    if (!env->queued_work_first)
632
        return;
633

    
634
    while ((wi = env->queued_work_first)) {
635
        env->queued_work_first = wi->next;
636
        wi->func(wi->data);
637
        wi->done = true;
638
    }
639
    env->queued_work_last = NULL;
640
    qemu_cond_broadcast(&qemu_work_cond);
641
}
642

    
643
static void qemu_wait_io_event_common(CPUState *env)
644
{
645
    if (env->stop) {
646
        env->stop = 0;
647
        env->stopped = 1;
648
        qemu_cond_signal(&qemu_pause_cond);
649
    }
650
    flush_queued_work(env);
651
    env->thread_kicked = false;
652
}
653

    
654
static void qemu_tcg_wait_io_event(void)
655
{
656
    CPUState *env;
657

    
658
    while (!any_cpu_has_work())
659
        qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
660

    
661
    qemu_mutex_unlock(&qemu_global_mutex);
662

    
663
    /*
664
     * Users of qemu_global_mutex can be starved, having no chance
665
     * to acquire it since this path will get to it first.
666
     * So use another lock to provide fairness.
667
     */
668
    qemu_mutex_lock(&qemu_fair_mutex);
669
    qemu_mutex_unlock(&qemu_fair_mutex);
670

    
671
    qemu_mutex_lock(&qemu_global_mutex);
672

    
673
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
674
        qemu_wait_io_event_common(env);
675
    }
676
}
677

    
678
static void sigbus_reraise(void)
679
{
680
    sigset_t set;
681
    struct sigaction action;
682

    
683
    memset(&action, 0, sizeof(action));
684
    action.sa_handler = SIG_DFL;
685
    if (!sigaction(SIGBUS, &action, NULL)) {
686
        raise(SIGBUS);
687
        sigemptyset(&set);
688
        sigaddset(&set, SIGBUS);
689
        sigprocmask(SIG_UNBLOCK, &set, NULL);
690
    }
691
    perror("Failed to re-raise SIGBUS!\n");
692
    abort();
693
}
694

    
695
static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
696
                           void *ctx)
697
{
698
    if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) {
699
        sigbus_reraise();
700
    }
701
}
702

    
703
static void qemu_kvm_wait_io_event(CPUState *env)
704
{
705
    while (!cpu_has_work(env))
706
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
707

    
708
    qemu_kvm_eat_signals(env);
709
    qemu_wait_io_event_common(env);
710
}
711

    
712
static int qemu_cpu_exec(CPUState *env);
713

    
714
static void *kvm_cpu_thread_fn(void *arg)
715
{
716
    CPUState *env = arg;
717
    int r;
718

    
719
    qemu_mutex_lock(&qemu_global_mutex);
720
    qemu_thread_self(env->thread);
721

    
722
    r = kvm_init_vcpu(env);
723
    if (r < 0) {
724
        fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
725
        exit(1);
726
    }
727

    
728
    qemu_kvm_init_cpu_signals(env);
729

    
730
    /* signal CPU creation */
731
    env->created = 1;
732
    qemu_cond_signal(&qemu_cpu_cond);
733

    
734
    /* and wait for machine initialization */
735
    while (!qemu_system_ready)
736
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
737

    
738
    while (1) {
739
        if (cpu_can_run(env))
740
            qemu_cpu_exec(env);
741
        qemu_kvm_wait_io_event(env);
742
    }
743

    
744
    return NULL;
745
}
746

    
747
static void *tcg_cpu_thread_fn(void *arg)
748
{
749
    CPUState *env = arg;
750

    
751
    qemu_tcg_init_cpu_signals();
752
    qemu_thread_self(env->thread);
753

    
754
    /* signal CPU creation */
755
    qemu_mutex_lock(&qemu_global_mutex);
756
    for (env = first_cpu; env != NULL; env = env->next_cpu)
757
        env->created = 1;
758
    qemu_cond_signal(&qemu_cpu_cond);
759

    
760
    /* and wait for machine initialization */
761
    while (!qemu_system_ready)
762
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
763

    
764
    while (1) {
765
        cpu_exec_all();
766
        qemu_tcg_wait_io_event();
767
    }
768

    
769
    return NULL;
770
}
771

    
772
void qemu_cpu_kick(void *_env)
773
{
774
    CPUState *env = _env;
775
    qemu_cond_broadcast(env->halt_cond);
776
    if (!env->thread_kicked) {
777
        qemu_thread_signal(env->thread, SIG_IPI);
778
        env->thread_kicked = true;
779
    }
780
}
781

    
782
int qemu_cpu_self(void *_env)
783
{
784
    CPUState *env = _env;
785
    QemuThread this;
786

    
787
    qemu_thread_self(&this);
788

    
789
    return qemu_thread_equal(&this, env->thread);
790
}
791

    
792
void qemu_mutex_lock_iothread(void)
793
{
794
    if (kvm_enabled()) {
795
        qemu_mutex_lock(&qemu_global_mutex);
796
    } else {
797
        qemu_mutex_lock(&qemu_fair_mutex);
798
        if (qemu_mutex_trylock(&qemu_global_mutex)) {
799
            qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
800
            qemu_mutex_lock(&qemu_global_mutex);
801
        }
802
        qemu_mutex_unlock(&qemu_fair_mutex);
803
    }
804
}
805

    
806
void qemu_mutex_unlock_iothread(void)
807
{
808
    qemu_mutex_unlock(&qemu_global_mutex);
809
}
810

    
811
static int all_vcpus_paused(void)
812
{
813
    CPUState *penv = first_cpu;
814

    
815
    while (penv) {
816
        if (!penv->stopped)
817
            return 0;
818
        penv = (CPUState *)penv->next_cpu;
819
    }
820

    
821
    return 1;
822
}
823

    
824
void pause_all_vcpus(void)
825
{
826
    CPUState *penv = first_cpu;
827

    
828
    while (penv) {
829
        penv->stop = 1;
830
        qemu_cpu_kick(penv);
831
        penv = (CPUState *)penv->next_cpu;
832
    }
833

    
834
    while (!all_vcpus_paused()) {
835
        qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
836
        penv = first_cpu;
837
        while (penv) {
838
            qemu_cpu_kick(penv);
839
            penv = (CPUState *)penv->next_cpu;
840
        }
841
    }
842
}
843

    
844
void resume_all_vcpus(void)
845
{
846
    CPUState *penv = first_cpu;
847

    
848
    while (penv) {
849
        penv->stop = 0;
850
        penv->stopped = 0;
851
        qemu_cpu_kick(penv);
852
        penv = (CPUState *)penv->next_cpu;
853
    }
854
}
855

    
856
static void tcg_init_vcpu(void *_env)
857
{
858
    CPUState *env = _env;
859
    /* share a single thread for all cpus with TCG */
860
    if (!tcg_cpu_thread) {
861
        env->thread = qemu_mallocz(sizeof(QemuThread));
862
        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
863
        qemu_cond_init(env->halt_cond);
864
        qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
865
        while (env->created == 0)
866
            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
867
        tcg_cpu_thread = env->thread;
868
        tcg_halt_cond = env->halt_cond;
869
    } else {
870
        env->thread = tcg_cpu_thread;
871
        env->halt_cond = tcg_halt_cond;
872
    }
873
}
874

    
875
static void kvm_start_vcpu(CPUState *env)
876
{
877
    env->thread = qemu_mallocz(sizeof(QemuThread));
878
    env->halt_cond = qemu_mallocz(sizeof(QemuCond));
879
    qemu_cond_init(env->halt_cond);
880
    qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
881
    while (env->created == 0)
882
        qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
883
}
884

    
885
void qemu_init_vcpu(void *_env)
886
{
887
    CPUState *env = _env;
888

    
889
    env->nr_cores = smp_cores;
890
    env->nr_threads = smp_threads;
891
    if (kvm_enabled())
892
        kvm_start_vcpu(env);
893
    else
894
        tcg_init_vcpu(env);
895
}
896

    
897
void qemu_notify_event(void)
898
{
899
    qemu_event_increment();
900
}
901

    
902
static void qemu_system_vmstop_request(int reason)
903
{
904
    vmstop_requested = reason;
905
    qemu_notify_event();
906
}
907

    
908
void cpu_stop_current(void)
909
{
910
    if (cpu_single_env) {
911
        cpu_single_env->stopped = 1;
912
        cpu_exit(cpu_single_env);
913
    }
914
}
915

    
916
void vm_stop(int reason)
917
{
918
    QemuThread me;
919
    qemu_thread_self(&me);
920

    
921
    if (!qemu_thread_equal(&me, &io_thread)) {
922
        qemu_system_vmstop_request(reason);
923
        /*
924
         * FIXME: should not return to device code in case
925
         * vm_stop() has been requested.
926
         */
927
        cpu_stop_current();
928
        return;
929
    }
930
    do_vm_stop(reason);
931
}
932

    
933
#endif
934

    
935
static int qemu_cpu_exec(CPUState *env)
936
{
937
    int ret;
938
#ifdef CONFIG_PROFILER
939
    int64_t ti;
940
#endif
941

    
942
#ifdef CONFIG_PROFILER
943
    ti = profile_getclock();
944
#endif
945
    if (use_icount) {
946
        int64_t count;
947
        int decr;
948
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
949
        env->icount_decr.u16.low = 0;
950
        env->icount_extra = 0;
951
        count = qemu_icount_round (qemu_next_deadline());
952
        qemu_icount += count;
953
        decr = (count > 0xffff) ? 0xffff : count;
954
        count -= decr;
955
        env->icount_decr.u16.low = decr;
956
        env->icount_extra = count;
957
    }
958
    ret = cpu_exec(env);
959
#ifdef CONFIG_PROFILER
960
    qemu_time += profile_getclock() - ti;
961
#endif
962
    if (use_icount) {
963
        /* Fold pending instructions back into the
964
           instruction counter, and clear the interrupt flag.  */
965
        qemu_icount -= (env->icount_decr.u16.low
966
                        + env->icount_extra);
967
        env->icount_decr.u32 = 0;
968
        env->icount_extra = 0;
969
    }
970
    return ret;
971
}
972

    
973
bool cpu_exec_all(void)
974
{
975
    int r;
976

    
977
    if (next_cpu == NULL)
978
        next_cpu = first_cpu;
979
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
980
        CPUState *env = next_cpu;
981

    
982
        qemu_clock_enable(vm_clock,
983
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
984

    
985
        if (qemu_alarm_pending())
986
            break;
987
        if (cpu_can_run(env)) {
988
            r = qemu_cpu_exec(env);
989
            if (kvm_enabled()) {
990
                qemu_kvm_eat_signals(env);
991
            }
992
            if (r == EXCP_DEBUG) {
993
                break;
994
            }
995
        } else if (env->stop) {
996
            break;
997
        }
998
    }
999
    exit_request = 0;
1000
    return any_cpu_has_work();
1001
}
1002

    
1003
void set_numa_modes(void)
1004
{
1005
    CPUState *env;
1006
    int i;
1007

    
1008
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1009
        for (i = 0; i < nb_numa_nodes; i++) {
1010
            if (node_cpumask[i] & (1 << env->cpu_index)) {
1011
                env->numa_node = i;
1012
            }
1013
        }
1014
    }
1015
}
1016

    
1017
void set_cpu_log(const char *optarg)
1018
{
1019
    int mask;
1020
    const CPULogItem *item;
1021

    
1022
    mask = cpu_str_to_log_mask(optarg);
1023
    if (!mask) {
1024
        printf("Log items (comma separated):\n");
1025
        for (item = cpu_log_items; item->mask != 0; item++) {
1026
            printf("%-10s %s\n", item->name, item->help);
1027
        }
1028
        exit(1);
1029
    }
1030
    cpu_set_log(mask);
1031
}
1032

    
1033
/* Return the virtual CPU time, based on the instruction counter.  */
1034
int64_t cpu_get_icount(void)
1035
{
1036
    int64_t icount;
1037
    CPUState *env = cpu_single_env;;
1038

    
1039
    icount = qemu_icount;
1040
    if (env) {
1041
        if (!can_do_io(env)) {
1042
            fprintf(stderr, "Bad clock read\n");
1043
        }
1044
        icount -= (env->icount_decr.u16.low + env->icount_extra);
1045
    }
1046
    return qemu_icount_bias + (icount << icount_time_shift);
1047
}
1048

    
1049
void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1050
{
1051
    /* XXX: implement xxx_cpu_list for targets that still miss it */
1052
#if defined(cpu_list_id)
1053
    cpu_list_id(f, cpu_fprintf, optarg);
1054
#elif defined(cpu_list)
1055
    cpu_list(f, cpu_fprintf); /* deprecated */
1056
#endif
1057
}