Skip to content

Commit

Permalink
linux-user: fix bug about incorrect base addresss of gdt on i386 and …
Browse files Browse the repository at this point in the history
…x86_64

On linux user mode, CPUX86State::gdt::base from Different CPUX86State Objects have same value, It is incorrect! Every CPUX86State::gdt::base Must points to independent memory space.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1405
Signed-off-by: fanwenjie <fanwj@mail.ustc.edu.cn>
Message-Id: <4172b90.58b08.18631b77860.Coremail.fanwj@mail.ustc.edu.cn>
[lv: remove unnecessary casts, split overlong line]
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
  • Loading branch information
fan-wenjie authored and vivier committed Mar 7, 2023
1 parent b7fe6e7 commit 6d0df64
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions linux-user/i386/cpu_loop.c
Expand Up @@ -314,8 +314,17 @@ void cpu_loop(CPUX86State *env)
}
}

static void target_cpu_free(void *obj)
{
CPUArchState* env = ((CPUState*)obj)->env_ptr;
target_munmap(env->gdt.base, sizeof(uint64_t) * TARGET_GDT_ENTRIES);
g_free(obj);
}

void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
{
CPUState* cpu = env_cpu(env);
OBJECT(cpu)->free = target_cpu_free;
env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
env->hflags |= HF_PE_MASK | HF_CPL_MASK;
if (env->features[FEAT_1_EDX] & CPUID_SSE) {
Expand Down
8 changes: 8 additions & 0 deletions linux-user/main.c
Expand Up @@ -238,6 +238,14 @@ CPUArchState *cpu_copy(CPUArchState *env)

new_cpu->tcg_cflags = cpu->tcg_cflags;
memcpy(new_env, env, sizeof(CPUArchState));
#if defined(TARGET_I386) || defined(TARGET_X86_64)
new_env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
memcpy(g2h_untagged(new_env->gdt.base), g2h_untagged(env->gdt.base),
sizeof(uint64_t) * TARGET_GDT_ENTRIES);
OBJECT(new_cpu)->free = OBJECT(cpu)->free;
#endif

/* Clone all break/watchpoints.
Note: Once we support ptrace with hw-debug register access, make sure
Expand Down

0 comments on commit 6d0df64

Please sign in to comment.