Statistics
| Branch: | Revision:

root / cpus.c @ 3d78499a

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

    
34
#include "cpus.h"
35

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

    
42
static CPUState *cur_cpu;
43
static CPUState *next_cpu;
44

    
45
/***********************************************************/
46
void hw_error(const char *fmt, ...)
47
{
48
    va_list ap;
49
    CPUState *env;
50

    
51
    va_start(ap, fmt);
52
    fprintf(stderr, "qemu: hardware error: ");
53
    vfprintf(stderr, fmt, ap);
54
    fprintf(stderr, "\n");
55
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
56
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
57
#ifdef TARGET_I386
58
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
59
#else
60
        cpu_dump_state(env, stderr, fprintf, 0);
61
#endif
62
    }
63
    va_end(ap);
64
    abort();
65
}
66

    
67
void cpu_synchronize_all_states(void)
68
{
69
    CPUState *cpu;
70

    
71
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
72
        cpu_synchronize_state(cpu);
73
    }
74
}
75

    
76
void cpu_synchronize_all_post_reset(void)
77
{
78
    CPUState *cpu;
79

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

    
85
void cpu_synchronize_all_post_init(void)
86
{
87
    CPUState *cpu;
88

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

    
94
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 tcg_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
#ifndef _WIN32
145
static int io_thread_fd = -1;
146

    
147
static void qemu_event_increment(void)
148
{
149
    /* Write 8 bytes to be compatible with eventfd.  */
150
    static const uint64_t val = 1;
151
    ssize_t ret;
152

    
153
    if (io_thread_fd == -1)
154
        return;
155

    
156
    do {
157
        ret = write(io_thread_fd, &val, sizeof(val));
158
    } while (ret < 0 && errno == EINTR);
159

    
160
    /* EAGAIN is fine, a read must be pending.  */
161
    if (ret < 0 && errno != EAGAIN) {
162
        fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
163
                strerror(errno));
164
        exit (1);
165
    }
166
}
167

    
168
static void qemu_event_read(void *opaque)
169
{
170
    int fd = (unsigned long)opaque;
171
    ssize_t len;
172
    char buffer[512];
173

    
174
    /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
175
    do {
176
        len = read(fd, buffer, sizeof(buffer));
177
    } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
178
}
179

    
180
static int qemu_event_init(void)
181
{
182
    int err;
183
    int fds[2];
184

    
185
    err = qemu_eventfd(fds);
186
    if (err == -1)
187
        return -errno;
188

    
189
    err = fcntl_setfl(fds[0], O_NONBLOCK);
190
    if (err < 0)
191
        goto fail;
192

    
193
    err = fcntl_setfl(fds[1], O_NONBLOCK);
194
    if (err < 0)
195
        goto fail;
196

    
197
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
198
                         (void *)(unsigned long)fds[0]);
199

    
200
    io_thread_fd = fds[1];
201
    return 0;
202

    
203
fail:
204
    close(fds[0]);
205
    close(fds[1]);
206
    return err;
207
}
208
#else
209
HANDLE qemu_event_handle;
210

    
211
static void dummy_event_handler(void *opaque)
212
{
213
}
214

    
215
static int qemu_event_init(void)
216
{
217
    qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
218
    if (!qemu_event_handle) {
219
        fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
220
        return -1;
221
    }
222
    qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
223
    return 0;
224
}
225

    
226
static void qemu_event_increment(void)
227
{
228
    if (!SetEvent(qemu_event_handle)) {
229
        fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
230
                GetLastError());
231
        exit (1);
232
    }
233
}
234
#endif
235

    
236
#ifndef CONFIG_IOTHREAD
237
int qemu_init_main_loop(void)
238
{
239
    return qemu_event_init();
240
}
241

    
242
void qemu_main_loop_start(void)
243
{
244
}
245

    
246
void qemu_init_vcpu(void *_env)
247
{
248
    CPUState *env = _env;
249

    
250
    env->nr_cores = smp_cores;
251
    env->nr_threads = smp_threads;
252
    if (kvm_enabled())
253
        kvm_init_vcpu(env);
254
    return;
255
}
256

    
257
int qemu_cpu_self(void *env)
258
{
259
    return 1;
260
}
261

    
262
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
263
{
264
    func(data);
265
}
266

    
267
void resume_all_vcpus(void)
268
{
269
}
270

    
271
void pause_all_vcpus(void)
272
{
273
}
274

    
275
void qemu_cpu_kick(void *env)
276
{
277
    return;
278
}
279

    
280
void qemu_notify_event(void)
281
{
282
    CPUState *env = cpu_single_env;
283

    
284
    qemu_event_increment ();
285
    if (env) {
286
        cpu_exit(env);
287
    }
288
    if (next_cpu && env != next_cpu) {
289
        cpu_exit(next_cpu);
290
    }
291
}
292

    
293
void qemu_mutex_lock_iothread(void) {}
294
void qemu_mutex_unlock_iothread(void) {}
295

    
296
void vm_stop(int reason)
297
{
298
    do_vm_stop(reason);
299
}
300

    
301
#else /* CONFIG_IOTHREAD */
302

    
303
#include "qemu-thread.h"
304

    
305
QemuMutex qemu_global_mutex;
306
static QemuMutex qemu_fair_mutex;
307

    
308
static QemuThread io_thread;
309

    
310
static QemuThread *tcg_cpu_thread;
311
static QemuCond *tcg_halt_cond;
312

    
313
static int qemu_system_ready;
314
/* cpu creation */
315
static QemuCond qemu_cpu_cond;
316
/* system init */
317
static QemuCond qemu_system_cond;
318
static QemuCond qemu_pause_cond;
319
static QemuCond qemu_work_cond;
320

    
321
static void tcg_block_io_signals(void);
322
static void kvm_block_io_signals(CPUState *env);
323
static void unblock_io_signals(void);
324

    
325
int qemu_init_main_loop(void)
326
{
327
    int ret;
328

    
329
    ret = qemu_event_init();
330
    if (ret)
331
        return ret;
332

    
333
    qemu_cond_init(&qemu_pause_cond);
334
    qemu_mutex_init(&qemu_fair_mutex);
335
    qemu_mutex_init(&qemu_global_mutex);
336
    qemu_mutex_lock(&qemu_global_mutex);
337

    
338
    unblock_io_signals();
339
    qemu_thread_self(&io_thread);
340

    
341
    return 0;
342
}
343

    
344
void qemu_main_loop_start(void)
345
{
346
    qemu_system_ready = 1;
347
    qemu_cond_broadcast(&qemu_system_cond);
348
}
349

    
350
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
351
{
352
    struct qemu_work_item wi;
353

    
354
    if (qemu_cpu_self(env)) {
355
        func(data);
356
        return;
357
    }
358

    
359
    wi.func = func;
360
    wi.data = data;
361
    if (!env->queued_work_first)
362
        env->queued_work_first = &wi;
363
    else
364
        env->queued_work_last->next = &wi;
365
    env->queued_work_last = &wi;
366
    wi.next = NULL;
367
    wi.done = false;
368

    
369
    qemu_cpu_kick(env);
370
    while (!wi.done) {
371
        CPUState *self_env = cpu_single_env;
372

    
373
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
374
        cpu_single_env = self_env;
375
    }
376
}
377

    
378
static void flush_queued_work(CPUState *env)
379
{
380
    struct qemu_work_item *wi;
381

    
382
    if (!env->queued_work_first)
383
        return;
384

    
385
    while ((wi = env->queued_work_first)) {
386
        env->queued_work_first = wi->next;
387
        wi->func(wi->data);
388
        wi->done = true;
389
    }
390
    env->queued_work_last = NULL;
391
    qemu_cond_broadcast(&qemu_work_cond);
392
}
393

    
394
static void qemu_wait_io_event_common(CPUState *env)
395
{
396
    if (env->stop) {
397
        env->stop = 0;
398
        env->stopped = 1;
399
        qemu_cond_signal(&qemu_pause_cond);
400
    }
401
    flush_queued_work(env);
402
}
403

    
404
static void qemu_wait_io_event(CPUState *env)
405
{
406
    while (!tcg_has_work())
407
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
408

    
409
    qemu_mutex_unlock(&qemu_global_mutex);
410

    
411
    /*
412
     * Users of qemu_global_mutex can be starved, having no chance
413
     * to acquire it since this path will get to it first.
414
     * So use another lock to provide fairness.
415
     */
416
    qemu_mutex_lock(&qemu_fair_mutex);
417
    qemu_mutex_unlock(&qemu_fair_mutex);
418

    
419
    qemu_mutex_lock(&qemu_global_mutex);
420
    qemu_wait_io_event_common(env);
421
}
422

    
423
static void qemu_kvm_eat_signal(CPUState *env, int timeout)
424
{
425
    struct timespec ts;
426
    int r, e;
427
    siginfo_t siginfo;
428
    sigset_t waitset;
429

    
430
    ts.tv_sec = timeout / 1000;
431
    ts.tv_nsec = (timeout % 1000) * 1000000;
432

    
433
    sigemptyset(&waitset);
434
    sigaddset(&waitset, SIG_IPI);
435

    
436
    qemu_mutex_unlock(&qemu_global_mutex);
437
    r = sigtimedwait(&waitset, &siginfo, &ts);
438
    e = errno;
439
    qemu_mutex_lock(&qemu_global_mutex);
440

    
441
    if (r == -1 && !(e == EAGAIN || e == EINTR)) {
442
        fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
443
        exit(1);
444
    }
445
}
446

    
447
static void qemu_kvm_wait_io_event(CPUState *env)
448
{
449
    while (!cpu_has_work(env))
450
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
451

    
452
    qemu_kvm_eat_signal(env, 0);
453
    qemu_wait_io_event_common(env);
454
}
455

    
456
static int qemu_cpu_exec(CPUState *env);
457

    
458
static void *kvm_cpu_thread_fn(void *arg)
459
{
460
    CPUState *env = arg;
461

    
462
    qemu_mutex_lock(&qemu_global_mutex);
463
    qemu_thread_self(env->thread);
464
    if (kvm_enabled())
465
        kvm_init_vcpu(env);
466

    
467
    kvm_block_io_signals(env);
468

    
469
    /* signal CPU creation */
470
    env->created = 1;
471
    qemu_cond_signal(&qemu_cpu_cond);
472

    
473
    /* and wait for machine initialization */
474
    while (!qemu_system_ready)
475
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
476

    
477
    while (1) {
478
        if (cpu_can_run(env))
479
            qemu_cpu_exec(env);
480
        qemu_kvm_wait_io_event(env);
481
    }
482

    
483
    return NULL;
484
}
485

    
486
static void *tcg_cpu_thread_fn(void *arg)
487
{
488
    CPUState *env = arg;
489

    
490
    tcg_block_io_signals();
491
    qemu_thread_self(env->thread);
492

    
493
    /* signal CPU creation */
494
    qemu_mutex_lock(&qemu_global_mutex);
495
    for (env = first_cpu; env != NULL; env = env->next_cpu)
496
        env->created = 1;
497
    qemu_cond_signal(&qemu_cpu_cond);
498

    
499
    /* and wait for machine initialization */
500
    while (!qemu_system_ready)
501
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
502

    
503
    while (1) {
504
        tcg_cpu_exec();
505
        qemu_wait_io_event(cur_cpu);
506
    }
507

    
508
    return NULL;
509
}
510

    
511
void qemu_cpu_kick(void *_env)
512
{
513
    CPUState *env = _env;
514
    qemu_cond_broadcast(env->halt_cond);
515
    qemu_thread_signal(env->thread, SIG_IPI);
516
}
517

    
518
int qemu_cpu_self(void *_env)
519
{
520
    CPUState *env = _env;
521
    QemuThread this;
522

    
523
    qemu_thread_self(&this);
524

    
525
    return qemu_thread_equal(&this, env->thread);
526
}
527

    
528
static void cpu_signal(int sig)
529
{
530
    if (cpu_single_env)
531
        cpu_exit(cpu_single_env);
532
    exit_request = 1;
533
}
534

    
535
static void tcg_block_io_signals(void)
536
{
537
    sigset_t set;
538
    struct sigaction sigact;
539

    
540
    sigemptyset(&set);
541
    sigaddset(&set, SIGUSR2);
542
    sigaddset(&set, SIGIO);
543
    sigaddset(&set, SIGALRM);
544
    sigaddset(&set, SIGCHLD);
545
    pthread_sigmask(SIG_BLOCK, &set, NULL);
546

    
547
    sigemptyset(&set);
548
    sigaddset(&set, SIG_IPI);
549
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
550

    
551
    memset(&sigact, 0, sizeof(sigact));
552
    sigact.sa_handler = cpu_signal;
553
    sigaction(SIG_IPI, &sigact, NULL);
554
}
555

    
556
static void dummy_signal(int sig)
557
{
558
}
559

    
560
static void kvm_block_io_signals(CPUState *env)
561
{
562
    int r;
563
    sigset_t set;
564
    struct sigaction sigact;
565

    
566
    sigemptyset(&set);
567
    sigaddset(&set, SIGUSR2);
568
    sigaddset(&set, SIGIO);
569
    sigaddset(&set, SIGALRM);
570
    sigaddset(&set, SIGCHLD);
571
    sigaddset(&set, SIG_IPI);
572
    pthread_sigmask(SIG_BLOCK, &set, NULL);
573

    
574
    pthread_sigmask(SIG_BLOCK, NULL, &set);
575
    sigdelset(&set, SIG_IPI);
576

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

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

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

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

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

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

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

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

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

    
634
    return 1;
635
}
636

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

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

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

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

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

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

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

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

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

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

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

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

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

    
741
#endif
742

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

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

    
781
bool tcg_cpu_exec(void)
782
{
783
    int ret = 0;
784

    
785
    if (next_cpu == NULL)
786
        next_cpu = first_cpu;
787
    for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
788
        CPUState *env = cur_cpu = next_cpu;
789

    
790
        qemu_clock_enable(vm_clock,
791
                          (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
792

    
793
        if (qemu_alarm_pending())
794
            break;
795
        if (cpu_can_run(env))
796
            ret = qemu_cpu_exec(env);
797
        else if (env->stop)
798
            break;
799

    
800
        if (ret == EXCP_DEBUG) {
801
            gdb_set_stop_cpu(env);
802
            debug_requested = EXCP_DEBUG;
803
            break;
804
        }
805
    }
806
    return tcg_has_work();
807
}
808

    
809
void set_numa_modes(void)
810
{
811
    CPUState *env;
812
    int i;
813

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

    
823
void set_cpu_log(const char *optarg)
824
{
825
    int mask;
826
    const CPULogItem *item;
827

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

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

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

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