Statistics
| Branch: | Revision:

root / cpus.c @ c1568af5

History | View | Annotate | Download (18.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

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

    
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
int cpu_is_stopped(CPUState *env)
95
{
96
    return !vm_running || env->stopped;
97
}
98

    
99
static void do_vm_stop(int reason)
100
{
101
    if (vm_running) {
102
        cpu_disable_ticks();
103
        vm_running = 0;
104
        pause_all_vcpus();
105
        vm_state_notify(0, reason);
106
        monitor_protocol_event(QEVENT_STOP, NULL);
107
    }
108
}
109

    
110
static int cpu_can_run(CPUState *env)
111
{
112
    if (env->stop)
113
        return 0;
114
    if (env->stopped || !vm_running)
115
        return 0;
116
    return 1;
117
}
118

    
119
static int cpu_has_work(CPUState *env)
120
{
121
    if (env->stop)
122
        return 1;
123
    if (env->queued_work_first)
124
        return 1;
125
    if (env->stopped || !vm_running)
126
        return 0;
127
    if (!env->halted)
128
        return 1;
129
    if (qemu_cpu_has_work(env))
130
        return 1;
131
    return 0;
132
}
133

    
134
static int any_cpu_has_work(void)
135
{
136
    CPUState *env;
137

    
138
    for (env = first_cpu; env != NULL; env = env->next_cpu)
139
        if (cpu_has_work(env))
140
            return 1;
141
    return 0;
142
}
143

    
144
static void cpu_debug_handler(CPUState *env)
145
{
146
    gdb_set_stop_cpu(env);
147
    debug_requested = EXCP_DEBUG;
148
    vm_stop(EXCP_DEBUG);
149
}
150

    
151
#ifndef _WIN32
152
static int io_thread_fd = -1;
153

    
154
static void qemu_event_increment(void)
155
{
156
    /* Write 8 bytes to be compatible with eventfd.  */
157
    static const uint64_t val = 1;
158
    ssize_t ret;
159

    
160
    if (io_thread_fd == -1)
161
        return;
162

    
163
    do {
164
        ret = write(io_thread_fd, &val, sizeof(val));
165
    } while (ret < 0 && errno == EINTR);
166

    
167
    /* EAGAIN is fine, a read must be pending.  */
168
    if (ret < 0 && errno != EAGAIN) {
169
        fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
170
                strerror(errno));
171
        exit (1);
172
    }
173
}
174

    
175
static void qemu_event_read(void *opaque)
176
{
177
    int fd = (unsigned long)opaque;
178
    ssize_t len;
179
    char buffer[512];
180

    
181
    /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
182
    do {
183
        len = read(fd, buffer, sizeof(buffer));
184
    } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
185
}
186

    
187
static int qemu_event_init(void)
188
{
189
    int err;
190
    int fds[2];
191

    
192
    err = qemu_eventfd(fds);
193
    if (err == -1)
194
        return -errno;
195

    
196
    err = fcntl_setfl(fds[0], O_NONBLOCK);
197
    if (err < 0)
198
        goto fail;
199

    
200
    err = fcntl_setfl(fds[1], O_NONBLOCK);
201
    if (err < 0)
202
        goto fail;
203

    
204
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
205
                         (void *)(unsigned long)fds[0]);
206

    
207
    io_thread_fd = fds[1];
208
    return 0;
209

    
210
fail:
211
    close(fds[0]);
212
    close(fds[1]);
213
    return err;
214
}
215
#else
216
HANDLE qemu_event_handle;
217

    
218
static void dummy_event_handler(void *opaque)
219
{
220
}
221

    
222
static int qemu_event_init(void)
223
{
224
    qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
225
    if (!qemu_event_handle) {
226
        fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
227
        return -1;
228
    }
229
    qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
230
    return 0;
231
}
232

    
233
static void qemu_event_increment(void)
234
{
235
    if (!SetEvent(qemu_event_handle)) {
236
        fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
237
                GetLastError());
238
        exit (1);
239
    }
240
}
241
#endif
242

    
243
#ifndef CONFIG_IOTHREAD
244
int qemu_init_main_loop(void)
245
{
246
    cpu_set_debug_excp_handler(cpu_debug_handler);
247

    
248
    return qemu_event_init();
249
}
250

    
251
void qemu_main_loop_start(void)
252
{
253
}
254

    
255
void qemu_init_vcpu(void *_env)
256
{
257
    CPUState *env = _env;
258

    
259
    env->nr_cores = smp_cores;
260
    env->nr_threads = smp_threads;
261
    if (kvm_enabled())
262
        kvm_init_vcpu(env);
263
    return;
264
}
265

    
266
int qemu_cpu_self(void *env)
267
{
268
    return 1;
269
}
270

    
271
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
272
{
273
    func(data);
274
}
275

    
276
void resume_all_vcpus(void)
277
{
278
}
279

    
280
void pause_all_vcpus(void)
281
{
282
}
283

    
284
void qemu_cpu_kick(void *env)
285
{
286
    return;
287
}
288

    
289
void qemu_notify_event(void)
290
{
291
    CPUState *env = cpu_single_env;
292

    
293
    qemu_event_increment ();
294
    if (env) {
295
        cpu_exit(env);
296
    }
297
    if (next_cpu && env != next_cpu) {
298
        cpu_exit(next_cpu);
299
    }
300
}
301

    
302
void qemu_mutex_lock_iothread(void) {}
303
void qemu_mutex_unlock_iothread(void) {}
304

    
305
void vm_stop(int reason)
306
{
307
    do_vm_stop(reason);
308
}
309

    
310
#else /* CONFIG_IOTHREAD */
311

    
312
#include "qemu-thread.h"
313

    
314
QemuMutex qemu_global_mutex;
315
static QemuMutex qemu_fair_mutex;
316

    
317
static QemuThread io_thread;
318

    
319
static QemuThread *tcg_cpu_thread;
320
static QemuCond *tcg_halt_cond;
321

    
322
static int qemu_system_ready;
323
/* cpu creation */
324
static QemuCond qemu_cpu_cond;
325
/* system init */
326
static QemuCond qemu_system_cond;
327
static QemuCond qemu_pause_cond;
328
static QemuCond qemu_work_cond;
329

    
330
static void tcg_init_ipi(void);
331
static void kvm_init_ipi(CPUState *env);
332
static void unblock_io_signals(void);
333

    
334
int qemu_init_main_loop(void)
335
{
336
    int ret;
337

    
338
    cpu_set_debug_excp_handler(cpu_debug_handler);
339

    
340
    ret = qemu_event_init();
341
    if (ret)
342
        return ret;
343

    
344
    qemu_cond_init(&qemu_pause_cond);
345
    qemu_cond_init(&qemu_system_cond);
346
    qemu_mutex_init(&qemu_fair_mutex);
347
    qemu_mutex_init(&qemu_global_mutex);
348
    qemu_mutex_lock(&qemu_global_mutex);
349

    
350
    unblock_io_signals();
351
    qemu_thread_self(&io_thread);
352

    
353
    return 0;
354
}
355

    
356
void qemu_main_loop_start(void)
357
{
358
    qemu_system_ready = 1;
359
    qemu_cond_broadcast(&qemu_system_cond);
360
}
361

    
362
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
363
{
364
    struct qemu_work_item wi;
365

    
366
    if (qemu_cpu_self(env)) {
367
        func(data);
368
        return;
369
    }
370

    
371
    wi.func = func;
372
    wi.data = data;
373
    if (!env->queued_work_first)
374
        env->queued_work_first = &wi;
375
    else
376
        env->queued_work_last->next = &wi;
377
    env->queued_work_last = &wi;
378
    wi.next = NULL;
379
    wi.done = false;
380

    
381
    qemu_cpu_kick(env);
382
    while (!wi.done) {
383
        CPUState *self_env = cpu_single_env;
384

    
385
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
386
        cpu_single_env = self_env;
387
    }
388
}
389

    
390
static void flush_queued_work(CPUState *env)
391
{
392
    struct qemu_work_item *wi;
393

    
394
    if (!env->queued_work_first)
395
        return;
396

    
397
    while ((wi = env->queued_work_first)) {
398
        env->queued_work_first = wi->next;
399
        wi->func(wi->data);
400
        wi->done = true;
401
    }
402
    env->queued_work_last = NULL;
403
    qemu_cond_broadcast(&qemu_work_cond);
404
}
405

    
406
static void qemu_wait_io_event_common(CPUState *env)
407
{
408
    if (env->stop) {
409
        env->stop = 0;
410
        env->stopped = 1;
411
        qemu_cond_signal(&qemu_pause_cond);
412
    }
413
    flush_queued_work(env);
414
}
415

    
416
static void qemu_tcg_wait_io_event(void)
417
{
418
    CPUState *env;
419

    
420
    while (!any_cpu_has_work())
421
        qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
422

    
423
    qemu_mutex_unlock(&qemu_global_mutex);
424

    
425
    /*
426
     * Users of qemu_global_mutex can be starved, having no chance
427
     * to acquire it since this path will get to it first.
428
     * So use another lock to provide fairness.
429
     */
430
    qemu_mutex_lock(&qemu_fair_mutex);
431
    qemu_mutex_unlock(&qemu_fair_mutex);
432

    
433
    qemu_mutex_lock(&qemu_global_mutex);
434

    
435
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
436
        qemu_wait_io_event_common(env);
437
    }
438
}
439

    
440
static void qemu_kvm_eat_signal(CPUState *env, int timeout)
441
{
442
    struct timespec ts;
443
    int r, e;
444
    siginfo_t siginfo;
445
    sigset_t waitset;
446

    
447
    ts.tv_sec = timeout / 1000;
448
    ts.tv_nsec = (timeout % 1000) * 1000000;
449

    
450
    sigemptyset(&waitset);
451
    sigaddset(&waitset, SIG_IPI);
452

    
453
    qemu_mutex_unlock(&qemu_global_mutex);
454
    r = sigtimedwait(&waitset, &siginfo, &ts);
455
    e = errno;
456
    qemu_mutex_lock(&qemu_global_mutex);
457

    
458
    if (r == -1 && !(e == EAGAIN || e == EINTR)) {
459
        fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
460
        exit(1);
461
    }
462
}
463

    
464
static void qemu_kvm_wait_io_event(CPUState *env)
465
{
466
    while (!cpu_has_work(env))
467
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
468

    
469
    qemu_kvm_eat_signal(env, 0);
470
    qemu_wait_io_event_common(env);
471
}
472

    
473
static int qemu_cpu_exec(CPUState *env);
474

    
475
static void *kvm_cpu_thread_fn(void *arg)
476
{
477
    CPUState *env = arg;
478

    
479
    qemu_mutex_lock(&qemu_global_mutex);
480
    qemu_thread_self(env->thread);
481
    if (kvm_enabled())
482
        kvm_init_vcpu(env);
483

    
484
    kvm_init_ipi(env);
485

    
486
    /* signal CPU creation */
487
    env->created = 1;
488
    qemu_cond_signal(&qemu_cpu_cond);
489

    
490
    /* and wait for machine initialization */
491
    while (!qemu_system_ready)
492
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
493

    
494
    while (1) {
495
        if (cpu_can_run(env))
496
            qemu_cpu_exec(env);
497
        qemu_kvm_wait_io_event(env);
498
    }
499

    
500
    return NULL;
501
}
502

    
503
static void *tcg_cpu_thread_fn(void *arg)
504
{
505
    CPUState *env = arg;
506

    
507
    tcg_init_ipi();
508
    qemu_thread_self(env->thread);
509

    
510
    /* signal CPU creation */
511
    qemu_mutex_lock(&qemu_global_mutex);
512
    for (env = first_cpu; env != NULL; env = env->next_cpu)
513
        env->created = 1;
514
    qemu_cond_signal(&qemu_cpu_cond);
515

    
516
    /* and wait for machine initialization */
517
    while (!qemu_system_ready)
518
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
519

    
520
    while (1) {
521
        cpu_exec_all();
522
        qemu_tcg_wait_io_event();
523
    }
524

    
525
    return NULL;
526
}
527

    
528
void qemu_cpu_kick(void *_env)
529
{
530
    CPUState *env = _env;
531
    qemu_cond_broadcast(env->halt_cond);
532
    qemu_thread_signal(env->thread, SIG_IPI);
533
}
534

    
535
int qemu_cpu_self(void *_env)
536
{
537
    CPUState *env = _env;
538
    QemuThread this;
539

    
540
    qemu_thread_self(&this);
541

    
542
    return qemu_thread_equal(&this, env->thread);
543
}
544

    
545
static void cpu_signal(int sig)
546
{
547
    if (cpu_single_env)
548
        cpu_exit(cpu_single_env);
549
    exit_request = 1;
550
}
551

    
552
static void tcg_init_ipi(void)
553
{
554
    sigset_t set;
555
    struct sigaction sigact;
556

    
557
    memset(&sigact, 0, sizeof(sigact));
558
    sigact.sa_handler = cpu_signal;
559
    sigaction(SIG_IPI, &sigact, NULL);
560

    
561
    sigemptyset(&set);
562
    sigaddset(&set, SIG_IPI);
563
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
564
}
565

    
566
static void dummy_signal(int sig)
567
{
568
}
569

    
570
static void kvm_init_ipi(CPUState *env)
571
{
572
    int r;
573
    sigset_t set;
574
    struct sigaction sigact;
575

    
576
    memset(&sigact, 0, sizeof(sigact));
577
    sigact.sa_handler = dummy_signal;
578
    sigaction(SIG_IPI, &sigact, NULL);
579

    
580
    pthread_sigmask(SIG_BLOCK, NULL, &set);
581
    sigdelset(&set, SIG_IPI);
582
    r = kvm_set_signal_mask(env, &set);
583
    if (r) {
584
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
585
        exit(1);
586
    }
587
}
588

    
589
static void unblock_io_signals(void)
590
{
591
    sigset_t set;
592

    
593
    sigemptyset(&set);
594
    sigaddset(&set, SIGUSR2);
595
    sigaddset(&set, SIGIO);
596
    sigaddset(&set, SIGALRM);
597
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
598

    
599
    sigemptyset(&set);
600
    sigaddset(&set, SIG_IPI);
601
    pthread_sigmask(SIG_BLOCK, &set, NULL);
602
}
603

    
604
void qemu_mutex_lock_iothread(void)
605
{
606
    if (kvm_enabled()) {
607
        qemu_mutex_lock(&qemu_fair_mutex);
608
        qemu_mutex_lock(&qemu_global_mutex);
609
        qemu_mutex_unlock(&qemu_fair_mutex);
610
    } else {
611
        qemu_mutex_lock(&qemu_fair_mutex);
612
        if (qemu_mutex_trylock(&qemu_global_mutex)) {
613
            qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
614
            qemu_mutex_lock(&qemu_global_mutex);
615
        }
616
        qemu_mutex_unlock(&qemu_fair_mutex);
617
    }
618
}
619

    
620
void qemu_mutex_unlock_iothread(void)
621
{
622
    qemu_mutex_unlock(&qemu_global_mutex);
623
}
624

    
625
static int all_vcpus_paused(void)
626
{
627
    CPUState *penv = first_cpu;
628

    
629
    while (penv) {
630
        if (!penv->stopped)
631
            return 0;
632
        penv = (CPUState *)penv->next_cpu;
633
    }
634

    
635
    return 1;
636
}
637

    
638
void pause_all_vcpus(void)
639
{
640
    CPUState *penv = first_cpu;
641

    
642
    while (penv) {
643
        penv->stop = 1;
644
        qemu_cpu_kick(penv);
645
        penv = (CPUState *)penv->next_cpu;
646
    }
647

    
648
    while (!all_vcpus_paused()) {
649
        qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
650
        penv = first_cpu;
651
        while (penv) {
652
            qemu_cpu_kick(penv);
653
            penv = (CPUState *)penv->next_cpu;
654
        }
655
    }
656
}
657

    
658
void resume_all_vcpus(void)
659
{
660
    CPUState *penv = first_cpu;
661

    
662
    while (penv) {
663
        penv->stop = 0;
664
        penv->stopped = 0;
665
        qemu_cpu_kick(penv);
666
        penv = (CPUState *)penv->next_cpu;
667
    }
668
}
669

    
670
static void tcg_init_vcpu(void *_env)
671
{
672
    CPUState *env = _env;
673
    /* share a single thread for all cpus with TCG */
674
    if (!tcg_cpu_thread) {
675
        env->thread = qemu_mallocz(sizeof(QemuThread));
676
        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
677
        qemu_cond_init(env->halt_cond);
678
        qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
679
        while (env->created == 0)
680
            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
681
        tcg_cpu_thread = env->thread;
682
        tcg_halt_cond = env->halt_cond;
683
    } else {
684
        env->thread = tcg_cpu_thread;
685
        env->halt_cond = tcg_halt_cond;
686
    }
687
}
688

    
689
static void kvm_start_vcpu(CPUState *env)
690
{
691
    env->thread = qemu_mallocz(sizeof(QemuThread));
692
    env->halt_cond = qemu_mallocz(sizeof(QemuCond));
693
    qemu_cond_init(env->halt_cond);
694
    qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
695
    while (env->created == 0)
696
        qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
697
}
698

    
699
void qemu_init_vcpu(void *_env)
700
{
701
    CPUState *env = _env;
702

    
703
    env->nr_cores = smp_cores;
704
    env->nr_threads = smp_threads;
705
    if (kvm_enabled())
706
        kvm_start_vcpu(env);
707
    else
708
        tcg_init_vcpu(env);
709
}
710

    
711
void qemu_notify_event(void)
712
{
713
    qemu_event_increment();
714
}
715

    
716
static void qemu_system_vmstop_request(int reason)
717
{
718
    vmstop_requested = reason;
719
    qemu_notify_event();
720
}
721

    
722
void vm_stop(int reason)
723
{
724
    QemuThread me;
725
    qemu_thread_self(&me);
726

    
727
    if (!qemu_thread_equal(&me, &io_thread)) {
728
        qemu_system_vmstop_request(reason);
729
        /*
730
         * FIXME: should not return to device code in case
731
         * vm_stop() has been requested.
732
         */
733
        if (cpu_single_env) {
734
            cpu_exit(cpu_single_env);
735
            cpu_single_env->stop = 1;
736
        }
737
        return;
738
    }
739
    do_vm_stop(reason);
740
}
741

    
742
#endif
743

    
744
static int qemu_cpu_exec(CPUState *env)
745
{
746
    int ret;
747
#ifdef CONFIG_PROFILER
748
    int64_t ti;
749
#endif
750

    
751
#ifdef CONFIG_PROFILER
752
    ti = profile_getclock();
753
#endif
754
    if (use_icount) {
755
        int64_t count;
756
        int decr;
757
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
758
        env->icount_decr.u16.low = 0;
759
        env->icount_extra = 0;
760
        count = qemu_icount_round (qemu_next_deadline());
761
        qemu_icount += count;
762
        decr = (count > 0xffff) ? 0xffff : count;
763
        count -= decr;
764
        env->icount_decr.u16.low = decr;
765
        env->icount_extra = count;
766
    }
767
    ret = cpu_exec(env);
768
#ifdef CONFIG_PROFILER
769
    qemu_time += profile_getclock() - ti;
770
#endif
771
    if (use_icount) {
772
        /* Fold pending instructions back into the
773
           instruction counter, and clear the interrupt flag.  */
774
        qemu_icount -= (env->icount_decr.u16.low
775
                        + env->icount_extra);
776
        env->icount_decr.u32 = 0;
777
        env->icount_extra = 0;
778
    }
779
    return ret;
780
}
781

    
782
bool cpu_exec_all(void)
783
{
784
    if (next_cpu == NULL)
785
        next_cpu = first_cpu;
786
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
787
        CPUState *env = next_cpu;
788

    
789
        qemu_clock_enable(vm_clock,
790
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
791

    
792
        if (qemu_alarm_pending())
793
            break;
794
        if (cpu_can_run(env)) {
795
            if (qemu_cpu_exec(env) == EXCP_DEBUG) {
796
                break;
797
            }
798
        } else if (env->stop) {
799
            break;
800
        }
801
    }
802
    exit_request = 0;
803
    return any_cpu_has_work();
804
}
805

    
806
void set_numa_modes(void)
807
{
808
    CPUState *env;
809
    int i;
810

    
811
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
812
        for (i = 0; i < nb_numa_nodes; i++) {
813
            if (node_cpumask[i] & (1 << env->cpu_index)) {
814
                env->numa_node = i;
815
            }
816
        }
817
    }
818
}
819

    
820
void set_cpu_log(const char *optarg)
821
{
822
    int mask;
823
    const CPULogItem *item;
824

    
825
    mask = cpu_str_to_log_mask(optarg);
826
    if (!mask) {
827
        printf("Log items (comma separated):\n");
828
        for (item = cpu_log_items; item->mask != 0; item++) {
829
            printf("%-10s %s\n", item->name, item->help);
830
        }
831
        exit(1);
832
    }
833
    cpu_set_log(mask);
834
}
835

    
836
/* Return the virtual CPU time, based on the instruction counter.  */
837
int64_t cpu_get_icount(void)
838
{
839
    int64_t icount;
840
    CPUState *env = cpu_single_env;;
841

    
842
    icount = qemu_icount;
843
    if (env) {
844
        if (!can_do_io(env)) {
845
            fprintf(stderr, "Bad clock read\n");
846
        }
847
        icount -= (env->icount_decr.u16.low + env->icount_extra);
848
    }
849
    return qemu_icount_bias + (icount << icount_time_shift);
850
}
851

    
852
void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
853
               const char *optarg)
854
{
855
    /* XXX: implement xxx_cpu_list for targets that still miss it */
856
#if defined(cpu_list_id)
857
    cpu_list_id(f, cpu_fprintf, optarg);
858
#elif defined(cpu_list)
859
    cpu_list(f, cpu_fprintf); /* deprecated */
860
#endif
861
}