117
TimersState timers_state;
115
static TimersState timers_state;
119
117
/* Return the virtual CPU time, based on the instruction counter. */
120
118
int64_t cpu_get_icount(void)
123
CPUArchState *env = cpu_single_env;
121
CPUState *cpu = current_cpu;
125
123
icount = qemu_icount;
125
CPUArchState *env = cpu->env_ptr;
127
126
if (!can_do_io(env)) {
128
127
fprintf(stderr, "Bad clock read\n");
389
388
void hw_error(const char *fmt, ...)
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);
417
414
void cpu_synchronize_all_post_reset(void)
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);
426
423
void cpu_synchronize_all_post_init(void)
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);
437
434
return !runstate_is_running() || cpu->stopped;
440
static void do_vm_stop(RunState state)
437
static int do_vm_stop(RunState state)
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);
449
446
monitor_protocol_event(QEVENT_STOP, NULL);
450
ret = bdrv_flush_all();
453
455
static bool cpu_can_run(CPUState *cpu)
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);
588
588
fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
665
666
qemu_cpu_kick(cpu);
666
667
while (!wi.done) {
667
CPUArchState *self_env = cpu_single_env;
668
CPUState *self_cpu = current_cpu;
669
670
qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
670
cpu_single_env = self_env;
671
current_cpu = self_cpu;
675
void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
677
struct qemu_work_item *wi;
679
if (qemu_cpu_is_self(cpu)) {
684
wi = g_malloc0(sizeof(struct qemu_work_item));
688
if (cpu->queued_work_first == NULL) {
689
cpu->queued_work_first = wi;
691
cpu->queued_work_last->next = wi;
693
cpu->queued_work_last = wi;
674
700
static void flush_queued_work(CPUState *cpu)
714
743
qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
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);
722
static void qemu_kvm_wait_io_event(CPUArchState *env)
751
static void qemu_kvm_wait_io_event(CPUState *cpu)
724
CPUState *cpu = ENV_GET_CPU(env);
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);
831
855
qemu_cond_signal(&qemu_cpu_cond);
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);
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);
902
926
void qemu_cpu_kick_self(void)
905
assert(cpu_single_env);
906
CPUState *cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
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;
1040
static void qemu_kvm_start_vcpu(CPUArchState *env)
1042
CPUState *cpu = ENV_GET_CPU(env);
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);
1054
static void qemu_dummy_start_vcpu(CPUArchState *env)
1056
CPUState *cpu = ENV_GET_CPU(env);
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);
1068
void qemu_init_vcpu(void *_env)
1070
CPUArchState *env = _env;
1071
CPUState *cpu = ENV_GET_CPU(env);
1059
static void qemu_kvm_start_vcpu(CPUState *cpu)
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);
1071
static void qemu_dummy_start_vcpu(CPUState *cpu)
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);
1083
void qemu_init_vcpu(CPUState *cpu)
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);
1081
qemu_dummy_start_vcpu(env);
1093
qemu_dummy_start_vcpu(cpu);
1085
1097
void cpu_stop_current(void)
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);
1100
current_cpu->stop = false;
1101
current_cpu->stopped = true;
1102
cpu_exit(current_cpu);
1092
1103
qemu_cond_signal(&qemu_pause_cond);
1096
void vm_stop(RunState state)
1107
int vm_stop(RunState state)
1098
1109
if (qemu_in_vcpu_thread()) {
1099
1110
qemu_system_vmstop_request(state);
1102
1113
* vm_stop() has been requested.
1104
1115
cpu_stop_current();
1119
return do_vm_stop(state);
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)
1114
1126
if (runstate_is_running()) {
1127
return vm_stop(state);
1117
1129
runstate_set(state);
1130
/* Make sure to return an error if the flush in a previous vm_stop()
1132
return bdrv_flush_all();
1167
1182
next_cpu = first_cpu;
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;
1173
1188
qemu_clock_enable(vm_clock,
1174
(env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1189
(cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
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);
1182
1197
} else if (cpu->stop || cpu->stopped) {
1213
1226
CpuInfoList *qmp_query_cpus(Error **errp)
1215
1228
CpuInfoList *head = NULL, *cur_item = NULL;
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;
1222
cpu_synchronize_state(env);
1247
cpu_synchronize_state(cpu);
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)
1286
1309
l = sizeof(buf);
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);
1332
1355
void qmp_inject_nmi(Error **errp)
1334
1357
#if defined(TARGET_I386)
1337
for (env = first_cpu; env != NULL; env = env->next_cpu) {
1360
for (cs = first_cpu; cs != NULL; cs = cs->next_cpu) {
1361
X86CPU *cpu = X86_CPU(cs);
1362
CPUX86State *env = &cpu->env;
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);
1341
1367
apic_deliver_nmi(env->apic_state);