Skip to content

Commit

Permalink
use qemu_memalign for all cpu structs
Browse files Browse the repository at this point in the history
Some structs, specically CPUARMState is 16-bytes aligned.

This causes segment fault because gcc tends to vectorize the initalization

of the struct with infamous movaps tricks

qemu_memalign will ensure the alignment across platforms
  • Loading branch information
wtdcode committed Oct 17, 2024
1 parent e8ca3cb commit 7b2cd7f
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 12 deletions.
4 changes: 3 additions & 1 deletion qemu/target/arm/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2102,10 +2102,12 @@ ARMCPU *cpu_arm_init(struct uc_struct *uc)
CPUClass *cc;
CPUARMState *env;

cpu = calloc(1, sizeof(*cpu));
// cpu->env is 16 bytes aligned
cpu = qemu_memalign(16, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

#if !defined(TARGET_AARCH64)
if (uc->mode & UC_MODE_MCLASS) {
Expand Down
4 changes: 3 additions & 1 deletion qemu/target/arm/cpu64.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,12 @@ ARMCPU *cpu_aarch64_init(struct uc_struct *uc)
CPUClass *cc;
CPUARMState *env;

cpu = calloc(1, sizeof(*cpu));
// cpu->env is 16 bytes alignment
cpu = qemu_memalign(16, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

if (uc->cpu_model == INT_MAX) {
uc->cpu_model = UC_CPU_ARM64_A72;
Expand Down
3 changes: 2 additions & 1 deletion qemu/target/i386/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -5076,10 +5076,11 @@ X86CPU *cpu_x86_init(struct uc_struct *uc)
CPUClass *cc;
X86CPUClass *xcc;

cpu = calloc(1, sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

if (uc->cpu_model == INT_MAX) {
#ifdef TARGET_X86_64
Expand Down
3 changes: 2 additions & 1 deletion qemu/target/m68k/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ M68kCPU *cpu_m68k_init(struct uc_struct *uc)
CPUState *cs;
CPUClass *cc;

cpu = calloc(1, sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

if (uc->cpu_model == INT_MAX) {
uc->cpu_model = UC_CPU_M68K_CFV4E; // cfv4e
Expand Down
3 changes: 2 additions & 1 deletion qemu/target/mips/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ MIPSCPU *cpu_mips_init(struct uc_struct *uc)
CPUClass *cc;
CPUMIPSState *env;

cpu = calloc(1, sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

#ifdef TARGET_MIPS64
if (uc->cpu_model == INT_MAX) {
Expand Down
2 changes: 1 addition & 1 deletion qemu/target/ppc/translate_init.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11016,7 +11016,7 @@ PowerPCCPU *cpu_ppc_init(struct uc_struct *uc)
CPUClass *cc;
PowerPCCPUClass *pcc;

cpu = malloc(sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion qemu/target/riscv/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,11 @@ RISCVCPU *cpu_riscv_init(struct uc_struct *uc)
CPUState *cs;
CPUClass *cc;

cpu = calloc(1, sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

#ifdef TARGET_RISCV32
if (uc->cpu_model == INT_MAX) {
Expand Down
3 changes: 2 additions & 1 deletion qemu/target/s390x/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,11 @@ S390CPU *cpu_s390_init(struct uc_struct *uc, const char *cpu_model)
CPUClass *cc;
// int i;

cpu = calloc(1, sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

if (uc->cpu_model == INT_MAX) {
uc->cpu_model = UC_CPU_S390X_QEMU; // qemu-s390x-cpu
Expand Down
4 changes: 2 additions & 2 deletions qemu/target/sparc/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ SPARCCPU *cpu_sparc_init(struct uc_struct *uc)
CPUClass *cc;
SPARCCPUClass *scc;

cpu = malloc(sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset(cpu, 0, sizeof(*cpu));
memset((void*)cpu, 0, sizeof(*cpu));

if (uc->cpu_model == INT_MAX) {
#ifdef TARGET_SPARC64
Expand Down
3 changes: 2 additions & 1 deletion qemu/target/tricore/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ TriCoreCPU *cpu_tricore_init(struct uc_struct *uc)
CPUState *cs;
CPUClass *cc;

cpu = calloc(1, sizeof(*cpu));
cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) {
return NULL;
}
memset((void*)cpu, 0, sizeof(*cpu));

if (uc->cpu_model == INT_MAX) {
uc->cpu_model = 2; // tc27x
Expand Down
2 changes: 1 addition & 1 deletion uc.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ uc_err uc_close(uc_engine *uc)
g_free(uc->cpu->thread);

/* cpu */
free(uc->cpu);
qemu_vfree(uc->cpu);

/* flatviews */
g_hash_table_destroy(uc->flat_views);
Expand Down

0 comments on commit 7b2cd7f

Please sign in to comment.