~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to cpus.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
 
61
61
#endif /* CONFIG_LINUX */
62
62
 
63
 
static CPUArchState *next_cpu;
 
63
static CPUState *next_cpu;
64
64
 
65
 
static bool cpu_thread_is_idle(CPUArchState *env)
 
65
static bool cpu_thread_is_idle(CPUState *cpu)
66
66
{
67
 
    CPUState *cpu = ENV_GET_CPU(env);
68
 
 
69
67
    if (cpu->stop || cpu->queued_work_first) {
70
68
        return false;
71
69
    }
73
71
        return true;
74
72
    }
75
73
    if (!cpu->halted || qemu_cpu_has_work(cpu) ||
76
 
        kvm_async_interrupts_enabled()) {
 
74
        kvm_halt_in_kernel()) {
77
75
        return false;
78
76
    }
79
77
    return true;
81
79
 
82
80
static bool all_cpu_threads_idle(void)
83
81
{
84
 
    CPUArchState *env;
 
82
    CPUState *cpu;
85
83
 
86
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
87
 
        if (!cpu_thread_is_idle(env)) {
 
84
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
85
        if (!cpu_thread_is_idle(cpu)) {
88
86
            return false;
89
87
        }
90
88
    }
114
112
    int64_t dummy;
115
113
} TimersState;
116
114
 
117
 
TimersState timers_state;
 
115
static TimersState timers_state;
118
116
 
119
117
/* Return the virtual CPU time, based on the instruction counter.  */
120
118
int64_t cpu_get_icount(void)
121
119
{
122
120
    int64_t icount;
123
 
    CPUArchState *env = cpu_single_env;
 
121
    CPUState *cpu = current_cpu;
124
122
 
125
123
    icount = qemu_icount;
126
 
    if (env) {
 
124
    if (cpu) {
 
125
        CPUArchState *env = cpu->env_ptr;
127
126
        if (!can_do_io(env)) {
128
127
            fprintf(stderr, "Bad clock read\n");
129
128
        }
389
388
void hw_error(const char *fmt, ...)
390
389
{
391
390
    va_list ap;
392
 
    CPUArchState *env;
393
391
    CPUState *cpu;
394
392
 
395
393
    va_start(ap, fmt);
396
394
    fprintf(stderr, "qemu: hardware error: ");
397
395
    vfprintf(stderr, fmt, ap);
398
396
    fprintf(stderr, "\n");
399
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
400
 
        cpu = ENV_GET_CPU(env);
 
397
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
401
398
        fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
402
 
        cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU);
 
399
        cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
403
400
    }
404
401
    va_end(ap);
405
402
    abort();
407
404
 
408
405
void cpu_synchronize_all_states(void)
409
406
{
410
 
    CPUArchState *cpu;
 
407
    CPUState *cpu;
411
408
 
412
409
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
413
410
        cpu_synchronize_state(cpu);
416
413
 
417
414
void cpu_synchronize_all_post_reset(void)
418
415
{
419
 
    CPUArchState *cpu;
 
416
    CPUState *cpu;
420
417
 
421
418
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
422
 
        cpu_synchronize_post_reset(ENV_GET_CPU(cpu));
 
419
        cpu_synchronize_post_reset(cpu);
423
420
    }
424
421
}
425
422
 
426
423
void cpu_synchronize_all_post_init(void)
427
424
{
428
 
    CPUArchState *cpu;
 
425
    CPUState *cpu;
429
426
 
430
427
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
431
 
        cpu_synchronize_post_init(ENV_GET_CPU(cpu));
 
428
        cpu_synchronize_post_init(cpu);
432
429
    }
433
430
}
434
431
 
437
434
    return !runstate_is_running() || cpu->stopped;
438
435
}
439
436
 
440
 
static void do_vm_stop(RunState state)
 
437
static int do_vm_stop(RunState state)
441
438
{
 
439
    int ret = 0;
 
440
 
442
441
    if (runstate_is_running()) {
443
442
        cpu_disable_ticks();
444
443
        pause_all_vcpus();
445
444
        runstate_set(state);
446
445
        vm_state_notify(0, state);
447
 
        bdrv_drain_all();
448
 
        bdrv_flush_all();
449
446
        monitor_protocol_event(QEVENT_STOP, NULL);
450
447
    }
 
448
 
 
449
    bdrv_drain_all();
 
450
    ret = bdrv_flush_all();
 
451
 
 
452
    return ret;
451
453
}
452
454
 
453
455
static bool cpu_can_run(CPUState *cpu)
461
463
    return true;
462
464
}
463
465
 
464
 
static void cpu_handle_guest_debug(CPUArchState *env)
 
466
static void cpu_handle_guest_debug(CPUState *cpu)
465
467
{
466
 
    CPUState *cpu = ENV_GET_CPU(env);
467
 
 
468
 
    gdb_set_stop_cpu(env);
 
468
    gdb_set_stop_cpu(cpu);
469
469
    qemu_system_debug_request();
470
470
    cpu->stopped = true;
471
471
}
472
472
 
473
473
static void cpu_signal(int sig)
474
474
{
475
 
    if (cpu_single_env) {
476
 
        cpu_exit(cpu_single_env);
 
475
    if (current_cpu) {
 
476
        cpu_exit(current_cpu);
477
477
    }
478
478
    exit_request = 1;
479
479
}
570
570
{
571
571
}
572
572
 
573
 
static void qemu_kvm_init_cpu_signals(CPUArchState *env)
 
573
static void qemu_kvm_init_cpu_signals(CPUState *cpu)
574
574
{
575
575
    int r;
576
576
    sigset_t set;
583
583
    pthread_sigmask(SIG_BLOCK, NULL, &set);
584
584
    sigdelset(&set, SIG_IPI);
585
585
    sigdelset(&set, SIGBUS);
586
 
    r = kvm_set_signal_mask(env, &set);
 
586
    r = kvm_set_signal_mask(cpu, &set);
587
587
    if (r) {
588
588
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
589
589
        exit(1);
605
605
}
606
606
 
607
607
#else /* _WIN32 */
608
 
static void qemu_kvm_init_cpu_signals(CPUArchState *env)
 
608
static void qemu_kvm_init_cpu_signals(CPUState *cpu)
609
609
{
610
610
    abort();
611
611
}
653
653
 
654
654
    wi.func = func;
655
655
    wi.data = data;
 
656
    wi.free = false;
656
657
    if (cpu->queued_work_first == NULL) {
657
658
        cpu->queued_work_first = &wi;
658
659
    } else {
664
665
 
665
666
    qemu_cpu_kick(cpu);
666
667
    while (!wi.done) {
667
 
        CPUArchState *self_env = cpu_single_env;
 
668
        CPUState *self_cpu = current_cpu;
668
669
 
669
670
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
670
 
        cpu_single_env = self_env;
671
 
    }
 
671
        current_cpu = self_cpu;
 
672
    }
 
673
}
 
674
 
 
675
void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
 
676
{
 
677
    struct qemu_work_item *wi;
 
678
 
 
679
    if (qemu_cpu_is_self(cpu)) {
 
680
        func(data);
 
681
        return;
 
682
    }
 
683
 
 
684
    wi = g_malloc0(sizeof(struct qemu_work_item));
 
685
    wi->func = func;
 
686
    wi->data = data;
 
687
    wi->free = true;
 
688
    if (cpu->queued_work_first == NULL) {
 
689
        cpu->queued_work_first = wi;
 
690
    } else {
 
691
        cpu->queued_work_last->next = wi;
 
692
    }
 
693
    cpu->queued_work_last = wi;
 
694
    wi->next = NULL;
 
695
    wi->done = false;
 
696
 
 
697
    qemu_cpu_kick(cpu);
672
698
}
673
699
 
674
700
static void flush_queued_work(CPUState *cpu)
683
709
        cpu->queued_work_first = wi->next;
684
710
        wi->func(wi->data);
685
711
        wi->done = true;
 
712
        if (wi->free) {
 
713
            g_free(wi);
 
714
        }
686
715
    }
687
716
    cpu->queued_work_last = NULL;
688
717
    qemu_cond_broadcast(&qemu_work_cond);
701
730
 
702
731
static void qemu_tcg_wait_io_event(void)
703
732
{
704
 
    CPUArchState *env;
 
733
    CPUState *cpu;
705
734
 
706
735
    while (all_cpu_threads_idle()) {
707
736
       /* Start accounting real time to the virtual clock if the CPUs
714
743
        qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
715
744
    }
716
745
 
717
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
718
 
        qemu_wait_io_event_common(ENV_GET_CPU(env));
 
746
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
747
        qemu_wait_io_event_common(cpu);
719
748
    }
720
749
}
721
750
 
722
 
static void qemu_kvm_wait_io_event(CPUArchState *env)
 
751
static void qemu_kvm_wait_io_event(CPUState *cpu)
723
752
{
724
 
    CPUState *cpu = ENV_GET_CPU(env);
725
 
 
726
 
    while (cpu_thread_is_idle(env)) {
 
753
    while (cpu_thread_is_idle(cpu)) {
727
754
        qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
728
755
    }
729
756
 
733
760
 
734
761
static void *qemu_kvm_cpu_thread_fn(void *arg)
735
762
{
736
 
    CPUArchState *env = arg;
737
 
    CPUState *cpu = ENV_GET_CPU(env);
 
763
    CPUState *cpu = arg;
738
764
    int r;
739
765
 
740
766
    qemu_mutex_lock(&qemu_global_mutex);
741
767
    qemu_thread_get_self(cpu->thread);
742
768
    cpu->thread_id = qemu_get_thread_id();
743
 
    cpu_single_env = env;
 
769
    current_cpu = cpu;
744
770
 
745
771
    r = kvm_init_vcpu(cpu);
746
772
    if (r < 0) {
748
774
        exit(1);
749
775
    }
750
776
 
751
 
    qemu_kvm_init_cpu_signals(env);
 
777
    qemu_kvm_init_cpu_signals(cpu);
752
778
 
753
779
    /* signal CPU creation */
754
780
    cpu->created = true;
756
782
 
757
783
    while (1) {
758
784
        if (cpu_can_run(cpu)) {
759
 
            r = kvm_cpu_exec(env);
 
785
            r = kvm_cpu_exec(cpu);
760
786
            if (r == EXCP_DEBUG) {
761
 
                cpu_handle_guest_debug(env);
 
787
                cpu_handle_guest_debug(cpu);
762
788
            }
763
789
        }
764
 
        qemu_kvm_wait_io_event(env);
 
790
        qemu_kvm_wait_io_event(cpu);
765
791
    }
766
792
 
767
793
    return NULL;
773
799
    fprintf(stderr, "qtest is not supported under Windows\n");
774
800
    exit(1);
775
801
#else
776
 
    CPUArchState *env = arg;
777
 
    CPUState *cpu = ENV_GET_CPU(env);
 
802
    CPUState *cpu = arg;
778
803
    sigset_t waitset;
779
804
    int r;
780
805
 
789
814
    cpu->created = true;
790
815
    qemu_cond_signal(&qemu_cpu_cond);
791
816
 
792
 
    cpu_single_env = env;
 
817
    current_cpu = cpu;
793
818
    while (1) {
794
 
        cpu_single_env = NULL;
 
819
        current_cpu = NULL;
795
820
        qemu_mutex_unlock_iothread();
796
821
        do {
797
822
            int sig;
802
827
            exit(1);
803
828
        }
804
829
        qemu_mutex_lock_iothread();
805
 
        cpu_single_env = env;
 
830
        current_cpu = cpu;
806
831
        qemu_wait_io_event_common(cpu);
807
832
    }
808
833
 
821
846
static void *qemu_tcg_cpu_thread_fn(void *arg)
822
847
{
823
848
    CPUState *cpu = arg;
824
 
    CPUArchState *env;
825
849
 
826
850
    qemu_tcg_init_cpu_signals();
827
851
    qemu_thread_get_self(cpu->thread);
831
855
    qemu_cond_signal(&qemu_cpu_cond);
832
856
 
833
857
    /* wait for initial kick-off after machine start */
834
 
    while (ENV_GET_CPU(first_cpu)->stopped) {
 
858
    while (first_cpu->stopped) {
835
859
        qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
836
860
 
837
861
        /* process any pending work */
838
 
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
839
 
            qemu_wait_io_event_common(ENV_GET_CPU(env));
 
862
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
863
            qemu_wait_io_event_common(cpu);
840
864
        }
841
865
    }
842
866
 
902
926
void qemu_cpu_kick_self(void)
903
927
{
904
928
#ifndef _WIN32
905
 
    assert(cpu_single_env);
906
 
    CPUState *cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
 
929
    assert(current_cpu);
907
930
 
908
 
    if (!cpu_single_cpu->thread_kicked) {
909
 
        qemu_cpu_kick_thread(cpu_single_cpu);
910
 
        cpu_single_cpu->thread_kicked = true;
 
931
    if (!current_cpu->thread_kicked) {
 
932
        qemu_cpu_kick_thread(current_cpu);
 
933
        current_cpu->thread_kicked = true;
911
934
    }
912
935
#else
913
936
    abort();
921
944
 
922
945
static bool qemu_in_vcpu_thread(void)
923
946
{
924
 
    return cpu_single_env && qemu_cpu_is_self(ENV_GET_CPU(cpu_single_env));
 
947
    return current_cpu && qemu_cpu_is_self(current_cpu);
925
948
}
926
949
 
927
950
void qemu_mutex_lock_iothread(void)
931
954
    } else {
932
955
        iothread_requesting_mutex = true;
933
956
        if (qemu_mutex_trylock(&qemu_global_mutex)) {
934
 
            qemu_cpu_kick_thread(ENV_GET_CPU(first_cpu));
 
957
            qemu_cpu_kick_thread(first_cpu);
935
958
            qemu_mutex_lock(&qemu_global_mutex);
936
959
        }
937
960
        iothread_requesting_mutex = false;
946
969
 
947
970
static int all_vcpus_paused(void)
948
971
{
949
 
    CPUArchState *penv = first_cpu;
 
972
    CPUState *cpu = first_cpu;
950
973
 
951
 
    while (penv) {
952
 
        CPUState *pcpu = ENV_GET_CPU(penv);
953
 
        if (!pcpu->stopped) {
 
974
    while (cpu) {
 
975
        if (!cpu->stopped) {
954
976
            return 0;
955
977
        }
956
 
        penv = penv->next_cpu;
 
978
        cpu = cpu->next_cpu;
957
979
    }
958
980
 
959
981
    return 1;
961
983
 
962
984
void pause_all_vcpus(void)
963
985
{
964
 
    CPUArchState *penv = first_cpu;
 
986
    CPUState *cpu = first_cpu;
965
987
 
966
988
    qemu_clock_enable(vm_clock, false);
967
 
    while (penv) {
968
 
        CPUState *pcpu = ENV_GET_CPU(penv);
969
 
        pcpu->stop = true;
970
 
        qemu_cpu_kick(pcpu);
971
 
        penv = penv->next_cpu;
 
989
    while (cpu) {
 
990
        cpu->stop = true;
 
991
        qemu_cpu_kick(cpu);
 
992
        cpu = cpu->next_cpu;
972
993
    }
973
994
 
974
995
    if (qemu_in_vcpu_thread()) {
975
996
        cpu_stop_current();
976
997
        if (!kvm_enabled()) {
977
 
            penv = first_cpu;
978
 
            while (penv) {
979
 
                CPUState *pcpu = ENV_GET_CPU(penv);
980
 
                pcpu->stop = false;
981
 
                pcpu->stopped = true;
982
 
                penv = penv->next_cpu;
 
998
            cpu = first_cpu;
 
999
            while (cpu) {
 
1000
                cpu->stop = false;
 
1001
                cpu->stopped = true;
 
1002
                cpu = cpu->next_cpu;
983
1003
            }
984
1004
            return;
985
1005
        }
987
1007
 
988
1008
    while (!all_vcpus_paused()) {
989
1009
        qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
990
 
        penv = first_cpu;
991
 
        while (penv) {
992
 
            qemu_cpu_kick(ENV_GET_CPU(penv));
993
 
            penv = penv->next_cpu;
 
1010
        cpu = first_cpu;
 
1011
        while (cpu) {
 
1012
            qemu_cpu_kick(cpu);
 
1013
            cpu = cpu->next_cpu;
994
1014
        }
995
1015
    }
996
1016
}
1004
1024
 
1005
1025
void resume_all_vcpus(void)
1006
1026
{
1007
 
    CPUArchState *penv = first_cpu;
 
1027
    CPUState *cpu = first_cpu;
1008
1028
 
1009
1029
    qemu_clock_enable(vm_clock, true);
1010
 
    while (penv) {
1011
 
        CPUState *pcpu = ENV_GET_CPU(penv);
1012
 
        cpu_resume(pcpu);
1013
 
        penv = penv->next_cpu;
 
1030
    while (cpu) {
 
1031
        cpu_resume(cpu);
 
1032
        cpu = cpu->next_cpu;
1014
1033
    }
1015
1034
}
1016
1035
 
1037
1056
    }
1038
1057
}
1039
1058
 
1040
 
static void qemu_kvm_start_vcpu(CPUArchState *env)
1041
 
{
1042
 
    CPUState *cpu = ENV_GET_CPU(env);
1043
 
 
1044
 
    cpu->thread = g_malloc0(sizeof(QemuThread));
1045
 
    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1046
 
    qemu_cond_init(cpu->halt_cond);
1047
 
    qemu_thread_create(cpu->thread, qemu_kvm_cpu_thread_fn, env,
1048
 
                       QEMU_THREAD_JOINABLE);
1049
 
    while (!cpu->created) {
1050
 
        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1051
 
    }
1052
 
}
1053
 
 
1054
 
static void qemu_dummy_start_vcpu(CPUArchState *env)
1055
 
{
1056
 
    CPUState *cpu = ENV_GET_CPU(env);
1057
 
 
1058
 
    cpu->thread = g_malloc0(sizeof(QemuThread));
1059
 
    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1060
 
    qemu_cond_init(cpu->halt_cond);
1061
 
    qemu_thread_create(cpu->thread, qemu_dummy_cpu_thread_fn, env,
1062
 
                       QEMU_THREAD_JOINABLE);
1063
 
    while (!cpu->created) {
1064
 
        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1065
 
    }
1066
 
}
1067
 
 
1068
 
void qemu_init_vcpu(void *_env)
1069
 
{
1070
 
    CPUArchState *env = _env;
1071
 
    CPUState *cpu = ENV_GET_CPU(env);
1072
 
 
 
1059
static void qemu_kvm_start_vcpu(CPUState *cpu)
 
1060
{
 
1061
    cpu->thread = g_malloc0(sizeof(QemuThread));
 
1062
    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
 
1063
    qemu_cond_init(cpu->halt_cond);
 
1064
    qemu_thread_create(cpu->thread, qemu_kvm_cpu_thread_fn, cpu,
 
1065
                       QEMU_THREAD_JOINABLE);
 
1066
    while (!cpu->created) {
 
1067
        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
 
1068
    }
 
1069
}
 
1070
 
 
1071
static void qemu_dummy_start_vcpu(CPUState *cpu)
 
1072
{
 
1073
    cpu->thread = g_malloc0(sizeof(QemuThread));
 
1074
    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
 
1075
    qemu_cond_init(cpu->halt_cond);
 
1076
    qemu_thread_create(cpu->thread, qemu_dummy_cpu_thread_fn, cpu,
 
1077
                       QEMU_THREAD_JOINABLE);
 
1078
    while (!cpu->created) {
 
1079
        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
 
1080
    }
 
1081
}
 
1082
 
 
1083
void qemu_init_vcpu(CPUState *cpu)
 
1084
{
1073
1085
    cpu->nr_cores = smp_cores;
1074
1086
    cpu->nr_threads = smp_threads;
1075
1087
    cpu->stopped = true;
1076
1088
    if (kvm_enabled()) {
1077
 
        qemu_kvm_start_vcpu(env);
 
1089
        qemu_kvm_start_vcpu(cpu);
1078
1090
    } else if (tcg_enabled()) {
1079
1091
        qemu_tcg_init_vcpu(cpu);
1080
1092
    } else {
1081
 
        qemu_dummy_start_vcpu(env);
 
1093
        qemu_dummy_start_vcpu(cpu);
1082
1094
    }
1083
1095
}
1084
1096
 
1085
1097
void cpu_stop_current(void)
1086
1098
{
1087
 
    if (cpu_single_env) {
1088
 
        CPUState *cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
1089
 
        cpu_single_cpu->stop = false;
1090
 
        cpu_single_cpu->stopped = true;
1091
 
        cpu_exit(cpu_single_env);
 
1099
    if (current_cpu) {
 
1100
        current_cpu->stop = false;
 
1101
        current_cpu->stopped = true;
 
1102
        cpu_exit(current_cpu);
1092
1103
        qemu_cond_signal(&qemu_pause_cond);
1093
1104
    }
1094
1105
}
1095
1106
 
1096
 
void vm_stop(RunState state)
 
1107
int vm_stop(RunState state)
1097
1108
{
1098
1109
    if (qemu_in_vcpu_thread()) {
1099
1110
        qemu_system_vmstop_request(state);
1102
1113
         * vm_stop() has been requested.
1103
1114
         */
1104
1115
        cpu_stop_current();
1105
 
        return;
 
1116
        return 0;
1106
1117
    }
1107
 
    do_vm_stop(state);
 
1118
 
 
1119
    return do_vm_stop(state);
1108
1120
}
1109
1121
 
1110
1122
/* does a state transition even if the VM is already stopped,
1111
1123
   current state is forgotten forever */
1112
 
void vm_stop_force_state(RunState state)
 
1124
int vm_stop_force_state(RunState state)
1113
1125
{
1114
1126
    if (runstate_is_running()) {
1115
 
        vm_stop(state);
 
1127
        return vm_stop(state);
1116
1128
    } else {
1117
1129
        runstate_set(state);
 
1130
        /* Make sure to return an error if the flush in a previous vm_stop()
 
1131
         * failed. */
 
1132
        return bdrv_flush_all();
1118
1133
    }
1119
1134
}
1120
1135
 
1167
1182
        next_cpu = first_cpu;
1168
1183
    }
1169
1184
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1170
 
        CPUArchState *env = next_cpu;
1171
 
        CPUState *cpu = ENV_GET_CPU(env);
 
1185
        CPUState *cpu = next_cpu;
 
1186
        CPUArchState *env = cpu->env_ptr;
1172
1187
 
1173
1188
        qemu_clock_enable(vm_clock,
1174
 
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
 
1189
                          (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1175
1190
 
1176
1191
        if (cpu_can_run(cpu)) {
1177
1192
            r = tcg_cpu_exec(env);
1178
1193
            if (r == EXCP_DEBUG) {
1179
 
                cpu_handle_guest_debug(env);
 
1194
                cpu_handle_guest_debug(cpu);
1180
1195
                break;
1181
1196
            }
1182
1197
        } else if (cpu->stop || cpu->stopped) {
1188
1203
 
1189
1204
void set_numa_modes(void)
1190
1205
{
1191
 
    CPUArchState *env;
1192
1206
    CPUState *cpu;
1193
1207
    int i;
1194
1208
 
1195
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1196
 
        cpu = ENV_GET_CPU(env);
 
1209
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1197
1210
        for (i = 0; i < nb_numa_nodes; i++) {
1198
1211
            if (test_bit(cpu->cpu_index, node_cpumask[i])) {
1199
1212
                cpu->numa_node = i;
1213
1226
CpuInfoList *qmp_query_cpus(Error **errp)
1214
1227
{
1215
1228
    CpuInfoList *head = NULL, *cur_item = NULL;
1216
 
    CPUArchState *env;
 
1229
    CPUState *cpu;
1217
1230
 
1218
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1219
 
        CPUState *cpu = ENV_GET_CPU(env);
 
1231
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1220
1232
        CpuInfoList *info;
 
1233
#if defined(TARGET_I386)
 
1234
        X86CPU *x86_cpu = X86_CPU(cpu);
 
1235
        CPUX86State *env = &x86_cpu->env;
 
1236
#elif defined(TARGET_PPC)
 
1237
        PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
 
1238
        CPUPPCState *env = &ppc_cpu->env;
 
1239
#elif defined(TARGET_SPARC)
 
1240
        SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
 
1241
        CPUSPARCState *env = &sparc_cpu->env;
 
1242
#elif defined(TARGET_MIPS)
 
1243
        MIPSCPU *mips_cpu = MIPS_CPU(cpu);
 
1244
        CPUMIPSState *env = &mips_cpu->env;
 
1245
#endif
1221
1246
 
1222
 
        cpu_synchronize_state(env);
 
1247
        cpu_synchronize_state(cpu);
1223
1248
 
1224
1249
        info = g_malloc0(sizeof(*info));
1225
1250
        info->value = g_malloc0(sizeof(*info->value));
1226
1251
        info->value->CPU = cpu->cpu_index;
1227
 
        info->value->current = (env == first_cpu);
 
1252
        info->value->current = (cpu == first_cpu);
1228
1253
        info->value->halted = cpu->halted;
1229
1254
        info->value->thread_id = cpu->thread_id;
1230
1255
#if defined(TARGET_I386)
1260
1285
{
1261
1286
    FILE *f;
1262
1287
    uint32_t l;
1263
 
    CPUArchState *env;
1264
1288
    CPUState *cpu;
1265
1289
    uint8_t buf[1024];
1266
1290
 
1274
1298
                  "a CPU number");
1275
1299
        return;
1276
1300
    }
1277
 
    env = cpu->env_ptr;
1278
1301
 
1279
1302
    f = fopen(filename, "wb");
1280
1303
    if (!f) {
1281
 
        error_set(errp, QERR_OPEN_FILE_FAILED, filename);
 
1304
        error_setg_file_open(errp, errno, filename);
1282
1305
        return;
1283
1306
    }
1284
1307
 
1286
1309
        l = sizeof(buf);
1287
1310
        if (l > size)
1288
1311
            l = size;
1289
 
        cpu_memory_rw_debug(env, addr, buf, l, 0);
 
1312
        cpu_memory_rw_debug(cpu, addr, buf, l, 0);
1290
1313
        if (fwrite(buf, 1, l, f) != l) {
1291
1314
            error_set(errp, QERR_IO_ERROR);
1292
1315
            goto exit;
1308
1331
 
1309
1332
    f = fopen(filename, "wb");
1310
1333
    if (!f) {
1311
 
        error_set(errp, QERR_OPEN_FILE_FAILED, filename);
 
1334
        error_setg_file_open(errp, errno, filename);
1312
1335
        return;
1313
1336
    }
1314
1337
 
1332
1355
void qmp_inject_nmi(Error **errp)
1333
1356
{
1334
1357
#if defined(TARGET_I386)
1335
 
    CPUArchState *env;
1336
 
 
1337
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
 
1358
    CPUState *cs;
 
1359
 
 
1360
    for (cs = first_cpu; cs != NULL; cs = cs->next_cpu) {
 
1361
        X86CPU *cpu = X86_CPU(cs);
 
1362
        CPUX86State *env = &cpu->env;
 
1363
 
1338
1364
        if (!env->apic_state) {
1339
 
            cpu_interrupt(CPU(x86_env_get_cpu(env)), CPU_INTERRUPT_NMI);
 
1365
            cpu_interrupt(cs, CPU_INTERRUPT_NMI);
1340
1366
        } else {
1341
1367
            apic_deliver_nmi(env->apic_state);
1342
1368
        }