From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52845) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VGt7t-0003ha-3s for qemu-devel@nongnu.org; Tue, 03 Sep 2013 12:06:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VGt7m-0006no-5Q for qemu-devel@nongnu.org; Tue, 03 Sep 2013 12:06:45 -0400 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Tue, 3 Sep 2013 18:06:25 +0200 Message-Id: <1378224387-18760-3-git-send-email-afaerber@suse.de> In-Reply-To: <1378224387-18760-1-git-send-email-afaerber@suse.de> References: <1378224387-18760-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 2/4] cpu: Use QTAILQ for CPU list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , "open list:Overall" , Gleb Natapov , Riku Voipio , Alexander Graf , Luiz Capitulino , "open list:e500" , Paul Brook , Anthony Liguori , Scott Wood , Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= , Aurelien Jarno , Richard Henderson Introduce CPU_FOREACH(), CPU_FOREACH_SAFE() and CPU_NEXT() shorthand macros. Signed-off-by: Andreas F=C3=A4rber --- cpus.c | 49 ++++++++++++++++++++--------------------= ------ cputlb.c | 2 +- dump.c | 10 +++++----- exec.c | 26 ++++++++++-------------- gdbstub.c | 14 ++++++------- hw/arm/boot.c | 2 +- hw/i386/kvm/clock.c | 2 +- hw/i386/kvmvapic.c | 2 +- hw/i386/pc.c | 3 +-- hw/ppc/e500.c | 2 +- hw/ppc/ppc.c | 2 +- hw/ppc/spapr.c | 4 ++-- hw/ppc/spapr_hcall.c | 4 ++-- include/qom/cpu.h | 11 +++++++++-- kvm-all.c | 8 ++++---- linux-user/elfload.c | 2 +- linux-user/main.c | 10 +++++++--- linux-user/syscall.c | 17 ++-------------- memory_mapping.c | 5 +++-- monitor.c | 2 +- target-i386/helper.c | 3 +-- target-i386/misc_helper.c | 2 +- target-mips/op_helper.c | 10 ++++------ target-ppc/excp_helper.c | 2 +- target-s390x/misc_helper.c | 8 ++++---- translate-all.c | 4 ++-- 26 files changed, 94 insertions(+), 112 deletions(-) diff --git a/cpus.c b/cpus.c index d74cc11..363d392 100644 --- a/cpus.c +++ b/cpus.c @@ -86,7 +86,7 @@ static bool all_cpu_threads_idle(void) { CPUState *cpu; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { if (!cpu_thread_is_idle(cpu)) { return false; } @@ -416,7 +416,7 @@ void hw_error(const char *fmt, ...) fprintf(stderr, "qemu: hardware error: "); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { fprintf(stderr, "CPU #%d:\n", cpu->cpu_index); cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU); } @@ -428,7 +428,7 @@ void cpu_synchronize_all_states(void) { CPUState *cpu; =20 - for (cpu =3D first_cpu; cpu; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { cpu_synchronize_state(cpu); } } @@ -437,7 +437,7 @@ void cpu_synchronize_all_post_reset(void) { CPUState *cpu; =20 - for (cpu =3D first_cpu; cpu; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { cpu_synchronize_post_reset(cpu); } } @@ -446,7 +446,7 @@ void cpu_synchronize_all_post_init(void) { CPUState *cpu; =20 - for (cpu =3D first_cpu; cpu; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { cpu_synchronize_post_init(cpu); } } @@ -760,7 +760,7 @@ static void qemu_tcg_wait_io_event(void) qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex); } =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { qemu_wait_io_event_common(cpu); } } @@ -872,11 +872,11 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) qemu_cond_signal(&qemu_cpu_cond); =20 /* wait for initial kick-off after machine start */ - while (first_cpu->stopped) { + while (QTAILQ_FIRST(&cpus)->stopped) { qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex); =20 /* process any pending work */ - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { qemu_wait_io_event_common(cpu); } } @@ -991,13 +991,12 @@ void qemu_mutex_unlock_iothread(void) =20 static int all_vcpus_paused(void) { - CPUState *cpu =3D first_cpu; + CPUState *cpu; =20 - while (cpu) { + CPU_FOREACH(cpu) { if (!cpu->stopped) { return 0; } - cpu =3D cpu->next_cpu; } =20 return 1; @@ -1005,23 +1004,20 @@ static int all_vcpus_paused(void) =20 void pause_all_vcpus(void) { - CPUState *cpu =3D first_cpu; + CPUState *cpu; =20 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false); - while (cpu) { + CPU_FOREACH(cpu) { cpu->stop =3D true; qemu_cpu_kick(cpu); - cpu =3D cpu->next_cpu; } =20 if (qemu_in_vcpu_thread()) { cpu_stop_current(); if (!kvm_enabled()) { - cpu =3D first_cpu; - while (cpu) { + CPU_FOREACH(cpu) { cpu->stop =3D false; cpu->stopped =3D true; - cpu =3D cpu->next_cpu; } return; } @@ -1029,10 +1025,8 @@ void pause_all_vcpus(void) =20 while (!all_vcpus_paused()) { qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex); - cpu =3D first_cpu; - while (cpu) { + CPU_FOREACH(cpu) { qemu_cpu_kick(cpu); - cpu =3D cpu->next_cpu; } } } @@ -1046,12 +1040,11 @@ void cpu_resume(CPUState *cpu) =20 void resume_all_vcpus(void) { - CPUState *cpu =3D first_cpu; + CPUState *cpu; =20 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); - while (cpu) { + CPU_FOREACH(cpu) { cpu_resume(cpu); - cpu =3D cpu->next_cpu; } } =20 @@ -1215,7 +1208,7 @@ static void tcg_exec_all(void) if (next_cpu =3D=3D NULL) { next_cpu =3D first_cpu; } - for (; next_cpu !=3D NULL && !exit_request; next_cpu =3D next_cpu->n= ext_cpu) { + for (; next_cpu !=3D NULL && !exit_request; next_cpu =3D CPU_NEXT(ne= xt_cpu)) { CPUState *cpu =3D next_cpu; CPUArchState *env =3D cpu->env_ptr; =20 @@ -1240,7 +1233,7 @@ void set_numa_modes(void) CPUState *cpu; int i; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { for (i =3D 0; i < nb_numa_nodes; i++) { if (test_bit(cpu->cpu_index, node_cpumask[i])) { cpu->numa_node =3D i; @@ -1262,7 +1255,7 @@ CpuInfoList *qmp_query_cpus(Error **errp) CpuInfoList *head =3D NULL, *cur_item =3D NULL; CPUState *cpu; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { CpuInfoList *info; #if defined(TARGET_I386) X86CPU *x86_cpu =3D X86_CPU(cpu); @@ -1391,7 +1384,7 @@ void qmp_inject_nmi(Error **errp) #if defined(TARGET_I386) CPUState *cs; =20 - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { X86CPU *cpu =3D X86_CPU(cs); CPUX86State *env =3D &cpu->env; =20 @@ -1405,7 +1398,7 @@ void qmp_inject_nmi(Error **errp) CPUState *cs; S390CPU *cpu; =20 - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { cpu =3D S390_CPU(cs); if (cpu->env.cpu_num =3D=3D monitor_get_cpu_index()) { if (s390_cpu_restart(S390_CPU(cs)) =3D=3D -1) { diff --git a/cputlb.c b/cputlb.c index 977c0ca..19ecf60 100644 --- a/cputlb.c +++ b/cputlb.c @@ -189,7 +189,7 @@ void cpu_tlb_reset_dirty_all(ram_addr_t start1, ram_a= ddr_t length) CPUState *cpu; CPUArchState *env; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { int mmu_idx; =20 env =3D cpu->env_ptr; diff --git a/dump.c b/dump.c index c0dae2c..846155c 100644 --- a/dump.c +++ b/dump.c @@ -277,7 +277,7 @@ static int write_elf64_notes(DumpState *s) int ret; int id; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { id =3D cpu_index(cpu); ret =3D cpu_write_elf64_note(fd_write_vmcore, cpu, id, s); if (ret < 0) { @@ -286,7 +286,7 @@ static int write_elf64_notes(DumpState *s) } } =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { ret =3D cpu_write_elf64_qemunote(fd_write_vmcore, cpu, s); if (ret < 0) { dump_error(s, "dump: failed to write CPU status.\n"); @@ -327,7 +327,7 @@ static int write_elf32_notes(DumpState *s) int ret; int id; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { id =3D cpu_index(cpu); ret =3D cpu_write_elf32_note(fd_write_vmcore, cpu, id, s); if (ret < 0) { @@ -336,7 +336,7 @@ static int write_elf32_notes(DumpState *s) } } =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { ret =3D cpu_write_elf32_qemunote(fd_write_vmcore, cpu, s); if (ret < 0) { dump_error(s, "dump: failed to write CPU status.\n"); @@ -734,7 +734,7 @@ static int dump_init(DumpState *s, int fd, bool pagin= g, bool has_filter, */ cpu_synchronize_all_states(); nr_cpus =3D 0; - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { nr_cpus++; } =20 diff --git a/exec.c b/exec.c index 3ca9381..ca2a504 100644 --- a/exec.c +++ b/exec.c @@ -69,7 +69,7 @@ static MemoryRegion io_mem_unassigned; =20 #endif =20 -CPUState *first_cpu; +struct CPUTailQ cpus =3D QTAILQ_HEAD_INITIALIZER(cpus); /* current CPU in the current thread. It is only valid inside cpu_exec() */ DEFINE_TLS(CPUState *, current_cpu); @@ -351,26 +351,23 @@ const VMStateDescription vmstate_cpu_common =3D { =20 CPUState *qemu_get_cpu(int index) { - CPUState *cpu =3D first_cpu; + CPUState *cpu; =20 - while (cpu) { + CPU_FOREACH(cpu) { if (cpu->cpu_index =3D=3D index) { - break; + return cpu; } - cpu =3D cpu->next_cpu; } =20 - return cpu; + return NULL; } =20 void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *da= ta) { CPUState *cpu; =20 - cpu =3D first_cpu; - while (cpu) { + CPU_FOREACH(cpu) { func(cpu, data); - cpu =3D cpu->next_cpu; } } =20 @@ -378,17 +375,14 @@ void cpu_exec_init(CPUArchState *env) { CPUState *cpu =3D ENV_GET_CPU(env); CPUClass *cc =3D CPU_GET_CLASS(cpu); - CPUState **pcpu; + CPUState *some_cpu; int cpu_index; =20 #if defined(CONFIG_USER_ONLY) cpu_list_lock(); #endif - cpu->next_cpu =3D NULL; - pcpu =3D &first_cpu; cpu_index =3D 0; - while (*pcpu !=3D NULL) { - pcpu =3D &(*pcpu)->next_cpu; + CPU_FOREACH(some_cpu) { cpu_index++; } cpu->cpu_index =3D cpu_index; @@ -398,7 +392,7 @@ void cpu_exec_init(CPUArchState *env) #ifndef CONFIG_USER_ONLY cpu->thread_id =3D qemu_get_thread_id(); #endif - *pcpu =3D cpu; + QTAILQ_INSERT_TAIL(&cpus, cpu, node); #if defined(CONFIG_USER_ONLY) cpu_list_unlock(); #endif @@ -1762,7 +1756,7 @@ static void tcg_commit(MemoryListener *listener) /* since each CPU stores ram addresses in its TLB cache, we must reset the modified entries */ /* XXX: slow ! */ - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { CPUArchState *env =3D cpu->env_ptr; =20 tlb_flush(env, 1); diff --git a/gdbstub.c b/gdbstub.c index 9d067d6..2b7f22b 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -648,7 +648,7 @@ static int gdb_breakpoint_insert(target_ulong addr, t= arget_ulong len, int type) switch (type) { case GDB_BREAKPOINT_SW: case GDB_BREAKPOINT_HW: - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { env =3D cpu->env_ptr; err =3D cpu_breakpoint_insert(env, addr, BP_GDB, NULL); if (err) @@ -659,7 +659,7 @@ static int gdb_breakpoint_insert(target_ulong addr, t= arget_ulong len, int type) case GDB_WATCHPOINT_WRITE: case GDB_WATCHPOINT_READ: case GDB_WATCHPOINT_ACCESS: - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { env =3D cpu->env_ptr; err =3D cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[= type], NULL); @@ -686,7 +686,7 @@ static int gdb_breakpoint_remove(target_ulong addr, t= arget_ulong len, int type) switch (type) { case GDB_BREAKPOINT_SW: case GDB_BREAKPOINT_HW: - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { env =3D cpu->env_ptr; err =3D cpu_breakpoint_remove(env, addr, BP_GDB); if (err) @@ -697,7 +697,7 @@ static int gdb_breakpoint_remove(target_ulong addr, t= arget_ulong len, int type) case GDB_WATCHPOINT_WRITE: case GDB_WATCHPOINT_READ: case GDB_WATCHPOINT_ACCESS: - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { env =3D cpu->env_ptr; err =3D cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[= type]); if (err) @@ -720,7 +720,7 @@ static void gdb_breakpoint_remove_all(void) return; } =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { env =3D cpu->env_ptr; cpu_breakpoint_remove_all(env, BP_GDB); #ifndef CONFIG_USER_ONLY @@ -744,7 +744,7 @@ static CPUState *find_cpu(uint32_t thread_id) { CPUState *cpu; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { if (cpu_index(cpu) =3D=3D thread_id) { return cpu; } @@ -1070,7 +1070,7 @@ static int gdb_handle_packet(GDBState *s, const cha= r *line_buf) if (s->query_cpu) { snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu= )); put_packet(s, buf); - s->query_cpu =3D s->query_cpu->next_cpu; + s->query_cpu =3D CPU_NEXT(s->query_cpu); } else put_packet(s, "l"); break; diff --git a/hw/arm/boot.c b/hw/arm/boot.c index 2cbeefd..1e313af 100644 --- a/hw/arm/boot.c +++ b/hw/arm/boot.c @@ -468,7 +468,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_inf= o *info) } info->is_linux =3D is_linux; =20 - for (; cs; cs =3D cs->next_cpu) { + for (; cs; cs =3D CPU_NEXT(cs)) { cpu =3D ARM_CPU(cs); cpu->env.boot_info =3D info; qemu_register_reset(do_cpu_reset, cpu); diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c index e89e2f7..92aabb8 100644 --- a/hw/i386/kvm/clock.c +++ b/hw/i386/kvm/clock.c @@ -59,7 +59,7 @@ static void kvmclock_vm_state_change(void *opaque, int = running, if (!cap_clock_ctrl) { return; } - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { ret =3D kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0); if (ret) { if (ret !=3D -EINVAL) { diff --git a/hw/i386/kvmvapic.c b/hw/i386/kvmvapic.c index 15beb80..d3a6fbe 100644 --- a/hw/i386/kvmvapic.c +++ b/hw/i386/kvmvapic.c @@ -498,7 +498,7 @@ static void vapic_enable_tpr_reporting(bool enable) X86CPU *cpu; CPUX86State *env; =20 - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { cpu =3D X86_CPU(cs); env =3D &cpu->env; info.apic =3D env->apic_state; diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 3a620a1..0c313fe 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -191,13 +191,12 @@ static void pic_irq_request(void *opaque, int irq, = int level) =20 DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq); if (env->apic_state) { - while (cs) { + CPU_FOREACH(cs) { cpu =3D X86_CPU(cs); env =3D &cpu->env; if (apic_accept_pic_intr(env->apic_state)) { apic_deliver_pic_intr(env->apic_state, level); } - cs =3D cs->next_cpu; } } else { if (level) { diff --git a/hw/ppc/e500.c b/hw/ppc/e500.c index 9059ff9..cfdd84b 100644 --- a/hw/ppc/e500.c +++ b/hw/ppc/e500.c @@ -540,7 +540,7 @@ static DeviceState *ppce500_init_mpic_kvm(PPCE500Para= ms *params, return NULL; } =20 - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { if (kvm_openpic_connect_vcpu(dev, cs)) { fprintf(stderr, "%s: failed to connect vcpu to irqchip\n", __func__); diff --git a/hw/ppc/ppc.c b/hw/ppc/ppc.c index 59b41cb..bf2d3d4 100644 --- a/hw/ppc/ppc.c +++ b/hw/ppc/ppc.c @@ -443,7 +443,7 @@ void ppce500_set_mpic_proxy(bool enabled) { CPUState *cs; =20 - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { PowerPCCPU *cpu =3D POWERPC_CPU(cs); =20 cpu->env.mpic_proxy =3D enabled; diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 04f0ee3..8c6e296 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -187,7 +187,7 @@ static int spapr_fixup_cpu_dt(void *fdt, sPAPREnviron= ment *spapr) =20 assert(spapr->cpu_model); =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { uint32_t associativity[] =3D {cpu_to_be32(0x5), cpu_to_be32(0x0), cpu_to_be32(0x0), @@ -351,7 +351,7 @@ static void *spapr_create_fdt_skel(const char *cpu_mo= del, /* This is needed during FDT finalization */ spapr->cpu_model =3D g_strdup(modelname); =20 - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { PowerPCCPU *cpu =3D POWERPC_CPU(cs); CPUPPCState *env =3D &cpu->env; PowerPCCPUClass *pcc =3D POWERPC_CPU_GET_CLASS(cs); diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c index 89e6a00..f10ba8a 100644 --- a/hw/ppc/spapr_hcall.c +++ b/hw/ppc/spapr_hcall.c @@ -679,7 +679,7 @@ static target_ulong h_set_mode(PowerPCCPU *cpu, sPAPR= Environment *spapr, =20 switch (mflags) { case H_SET_MODE_ENDIAN_BIG: - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { PowerPCCPU *cp =3D POWERPC_CPU(cs); CPUPPCState *env =3D &cp->env; env->spr[SPR_LPCR] &=3D ~LPCR_ILE; @@ -688,7 +688,7 @@ static target_ulong h_set_mode(PowerPCCPU *cpu, sPAPR= Environment *spapr, break; =20 case H_SET_MODE_ENDIAN_LITTLE: - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { PowerPCCPU *cp =3D POWERPC_CPU(cs); CPUPPCState *env =3D &cp->env; env->spr[SPR_LPCR] |=3D LPCR_ILE; diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 3e49936..79f7c87 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -23,6 +23,7 @@ #include #include "hw/qdev-core.h" #include "exec/hwaddr.h" +#include "qemu/queue.h" #include "qemu/thread.h" #include "qemu/tls.h" #include "qemu/typedefs.h" @@ -190,7 +191,7 @@ struct CPUState { struct GDBRegisterState *gdb_regs; int gdb_num_regs; int gdb_num_g_regs; - CPUState *next_cpu; + QTAILQ_ENTRY(CPUState) node; =20 int kvm_fd; bool kvm_vcpu_dirty; @@ -202,7 +203,13 @@ struct CPUState { uint32_t halted; /* used by alpha, cris, ppc TCG */ }; =20 -extern CPUState *first_cpu; +QTAILQ_HEAD(CPUTailQ, CPUState); +extern struct CPUTailQ cpus; +#define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node) +#define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node) +#define CPU_FOREACH_SAFE(cpu, next_cpu) \ + QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu) +#define first_cpu QTAILQ_FIRST(&cpus) =20 DECLARE_TLS(CPUState *, current_cpu); #define current_cpu tls_var(current_cpu) diff --git a/kvm-all.c b/kvm-all.c index 875e32e..c29a015 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -1925,7 +1925,7 @@ int kvm_insert_breakpoint(CPUState *cpu, target_ulo= ng addr, } } =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { err =3D kvm_update_guest_debug(cpu, 0); if (err) { return err; @@ -1965,7 +1965,7 @@ int kvm_remove_breakpoint(CPUState *cpu, target_ulo= ng addr, } } =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { err =3D kvm_update_guest_debug(cpu, 0); if (err) { return err; @@ -1982,7 +1982,7 @@ void kvm_remove_all_breakpoints(CPUState *cpu) QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) { if (kvm_arch_remove_sw_breakpoint(cpu, bp) !=3D 0) { /* Try harder to find a CPU that currently sees the breakpoi= nt. */ - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu= ) { + CPU_FOREACH(cpu) { if (kvm_arch_remove_sw_breakpoint(cpu, bp) =3D=3D 0) { break; } @@ -1993,7 +1993,7 @@ void kvm_remove_all_breakpoints(CPUState *cpu) } kvm_arch_remove_all_hw_breakpoints(); =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { kvm_update_guest_debug(cpu, 0); } } diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 7ce2eab..72d9270 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2668,7 +2668,7 @@ static int fill_note_info(struct elf_note_info *inf= o, =20 /* read and fill status of all threads */ cpu_list_lock(); - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { if (cpu =3D=3D thread_cpu) { continue; } diff --git a/linux-user/main.c b/linux-user/main.c index 03859bc..5c2f7b2 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -117,10 +117,14 @@ void fork_end(int child) { mmap_fork_end(child); if (child) { + CPUState *cpu, *next_cpu; /* Child processes created by fork() only have a single thread. Discard information about the parent threads. */ - first_cpu =3D thread_cpu; - first_cpu->next_cpu =3D NULL; + CPU_FOREACH_SAFE(cpu, next_cpu) { + if (cpu !=3D thread_cpu) { + QTAILQ_REMOVE(&cpus, thread_cpu, node); + } + } pending_cpus =3D 0; pthread_mutex_init(&exclusive_lock, NULL); pthread_mutex_init(&cpu_list_mutex, NULL); @@ -154,7 +158,7 @@ static inline void start_exclusive(void) =20 pending_cpus =3D 1; /* Make all other cpus stop executing. */ - for (other_cpu =3D first_cpu; other_cpu; other_cpu =3D other_cpu->ne= xt_cpu) { + CPU_FOREACH(other_cpu) { if (other_cpu->running) { pending_cpus++; cpu_exit(other_cpu); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index f986548..ecead51 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -5113,25 +5113,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_l= ong arg1, Do thread termination if we have more then one thread. */ /* FIXME: This probably breaks if a signal arrives. We should p= robably be disabling signals. */ - if (first_cpu->next_cpu) { + if (CPU_NEXT(first_cpu)) { TaskState *ts; - CPUState **lastp; - CPUState *p; =20 cpu_list_lock(); - lastp =3D &first_cpu; - p =3D first_cpu; - while (p && p !=3D cpu) { - lastp =3D &p->next_cpu; - p =3D p->next_cpu; - } - /* If we didn't find the CPU for this thread then something = is - horribly wrong. */ - if (!p) { - abort(); - } /* Remove the CPU from the list. */ - *lastp =3D p->next_cpu; + QTAILQ_REMOVE(&cpus, cpu, node); cpu_list_unlock(); ts =3D ((CPUArchState *)cpu_env)->opaque; if (ts->child_tidptr) { diff --git a/memory_mapping.c b/memory_mapping.c index eeeeb44..87a6ed5 100644 --- a/memory_mapping.c +++ b/memory_mapping.c @@ -270,7 +270,7 @@ static CPUState *find_paging_enabled_cpu(CPUState *st= art_cpu) { CPUState *cpu; =20 - for (cpu =3D start_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { if (cpu_paging_enabled(cpu)) { return cpu; } @@ -289,7 +289,8 @@ void qemu_get_guest_memory_mapping(MemoryMappingList = *list, =20 first_paging_enabled_cpu =3D find_paging_enabled_cpu(first_cpu); if (first_paging_enabled_cpu) { - for (cpu =3D first_paging_enabled_cpu; cpu !=3D NULL; cpu =3D cp= u->next_cpu) { + for (cpu =3D first_paging_enabled_cpu; cpu !=3D NULL; + cpu =3D CPU_NEXT(cpu)) { Error *err =3D NULL; cpu_get_memory_mapping(cpu, list, &err); if (err) { diff --git a/monitor.c b/monitor.c index 0aeaf6c..683babf 100644 --- a/monitor.c +++ b/monitor.c @@ -2002,7 +2002,7 @@ static void do_info_numa(Monitor *mon, const QDict = *qdict) monitor_printf(mon, "%d nodes\n", nb_numa_nodes); for (i =3D 0; i < nb_numa_nodes; i++) { monitor_printf(mon, "node %d cpus:", i); - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { if (cpu->numa_node =3D=3D i) { monitor_printf(mon, " %d", cpu->cpu_index); } diff --git a/target-i386/helper.c b/target-i386/helper.c index bf3e2ac..7c58e27 100644 --- a/target-i386/helper.c +++ b/target-i386/helper.c @@ -1231,8 +1231,7 @@ void cpu_x86_inject_mce(Monitor *mon, X86CPU *cpu, = int bank, params.mcg_status =3D MCG_STATUS_MCIP | MCG_STATUS_RIPV; params.addr =3D 0; params.misc =3D 0; - for (other_cs =3D first_cpu; other_cs !=3D NULL; - other_cs =3D other_cs->next_cpu) { + CPU_FOREACH(other_cs) { if (other_cs =3D=3D cs) { continue; } diff --git a/target-i386/misc_helper.c b/target-i386/misc_helper.c index 957926c..93933fd 100644 --- a/target-i386/misc_helper.c +++ b/target-i386/misc_helper.c @@ -610,7 +610,7 @@ void helper_mwait(CPUX86State *env, int next_eip_adde= nd) cpu =3D x86_env_get_cpu(env); cs =3D CPU(cpu); /* XXX: not complete but not completely erroneous */ - if (cs->cpu_index !=3D 0 || cs->next_cpu !=3D NULL) { + if (cs->cpu_index !=3D 0 || CPU_NEXT(cs) !=3D NULL) { /* more than one CPU: do not sleep because another CPU may wake this one */ } else { diff --git a/target-mips/op_helper.c b/target-mips/op_helper.c index b828375..8e3a6d7 100644 --- a/target-mips/op_helper.c +++ b/target-mips/op_helper.c @@ -1699,15 +1699,14 @@ target_ulong helper_dvpe(CPUMIPSState *env) CPUState *other_cs =3D first_cpu; target_ulong prev =3D env->mvp->CP0_MVPControl; =20 - do { + CPU_FOREACH(other_cs) { MIPSCPU *other_cpu =3D MIPS_CPU(other_cs); /* Turn off all VPEs except the one executing the dvpe. */ if (&other_cpu->env !=3D env) { other_cpu->env.mvp->CP0_MVPControl &=3D ~(1 << CP0MVPCo_EVP)= ; mips_vpe_sleep(other_cpu); } - other_cs =3D other_cs->next_cpu; - } while (other_cs); + } return prev; } =20 @@ -1716,7 +1715,7 @@ target_ulong helper_evpe(CPUMIPSState *env) CPUState *other_cs =3D first_cpu; target_ulong prev =3D env->mvp->CP0_MVPControl; =20 - do { + CPU_FOREACH(other_cs) { MIPSCPU *other_cpu =3D MIPS_CPU(other_cs); =20 if (&other_cpu->env !=3D env @@ -1726,8 +1725,7 @@ target_ulong helper_evpe(CPUMIPSState *env) other_cpu->env.mvp->CP0_MVPControl |=3D (1 << CP0MVPCo_EVP); mips_vpe_wake(other_cpu); /* And wake it up. */ } - other_cs =3D other_cs->next_cpu; - } while (other_cs); + } return prev; } #endif /* !CONFIG_USER_ONLY */ diff --git a/target-ppc/excp_helper.c b/target-ppc/excp_helper.c index e957761..c959460 100644 --- a/target-ppc/excp_helper.c +++ b/target-ppc/excp_helper.c @@ -1002,7 +1002,7 @@ void helper_msgsnd(target_ulong rb) return; } =20 - for (cs =3D first_cpu; cs !=3D NULL; cs =3D cs->next_cpu) { + CPU_FOREACH(cs) { PowerPCCPU *cpu =3D POWERPC_CPU(cs); CPUPPCState *cenv =3D &cpu->env; =20 diff --git a/target-s390x/misc_helper.c b/target-s390x/misc_helper.c index 4afd7da..1690907 100644 --- a/target-s390x/misc_helper.c +++ b/target-s390x/misc_helper.c @@ -183,12 +183,12 @@ uint32_t HELPER(servc)(CPUS390XState *env, uint64_t= r1, uint64_t r2) #ifndef CONFIG_USER_ONLY static void cpu_reset_all(void) { - CPUState *cpu; + CPUState *cs; S390CPUClass *scc; =20 - for (cpu =3D first_cpu; cpu; cpu =3D cpu->next_cpu) { - scc =3D S390_CPU_GET_CLASS(CPU(cpu)); - scc->cpu_reset(CPU(cpu)); + CPU_FOREACH(cs) { + scc =3D S390_CPU_GET_CLASS(cs); + scc->cpu_reset(cs); } } =20 diff --git a/translate-all.c b/translate-all.c index 3b5fc7c..2c923c6 100644 --- a/translate-all.c +++ b/translate-all.c @@ -696,7 +696,7 @@ void tb_flush(CPUArchState *env1) } tcg_ctx.tb_ctx.nb_tbs =3D 0; =20 - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { CPUArchState *env =3D cpu->env_ptr; =20 memset(env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof(void *))= ; @@ -850,7 +850,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page= _addr_t page_addr) =20 /* remove the TB from the hash list */ h =3D tb_jmp_cache_hash_func(tb->pc); - for (cpu =3D first_cpu; cpu !=3D NULL; cpu =3D cpu->next_cpu) { + CPU_FOREACH(cpu) { CPUArchState *env =3D cpu->env_ptr; =20 if (env->tb_jmp_cache[h] =3D=3D tb) { --=20 1.8.1.4