qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] linux-user: fix bug about incorrect base addresss of idt and  gdt on i386 and x86_64
@ 2023-01-01 15:57 fanwj
  2023-01-01 16:46 ` Alex Bennée
  0 siblings, 1 reply; 6+ messages in thread
From: fanwj @ 2023-01-01 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1692 bytes --]

From 4601a624f40b2c89e7df2dec1adffb4f4308ba2d Mon Sep 17 00:00:00 2001
From: fanwenjie <fanwj@mail.ustc.edu.cn>
Date: Sun, 1 Jan 2023 23:13:34 +0800
Subject: [PATCH] linux-user: fix bug about incorrect base addresss of idt and
 gdt on i386 and x86_64


Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1405
Signed-off-by: fanwenjie <fanwj@mail.ustc.edu.cn>
---
 linux-user/main.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)


diff --git a/linux-user/main.c b/linux-user/main.c
index a17fed045b..5d673c95b3 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -171,6 +171,12 @@ void fork_end(int child)
 
 __thread CPUState *thread_cpu;
 
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+#include <stdalign.h>
+__thread alignas(TARGET_PAGE_SIZE) static uint64_t gdt_base[TARGET_GDT_ENTRIES];
+__thread alignas(TARGET_PAGE_SIZE) static uint64_t idt_base[TARGET_PAGE_SIZE / sizeof(uint64_t)];
+#endif
+
 bool qemu_cpu_is_self(CPUState *cpu)
 {
     return thread_cpu == cpu;
@@ -235,6 +241,13 @@ 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)
+    memcpy(idt_base, (void*)new_env->idt.base, sizeof(uint64_t) * (new_env->idt.limit + 1));
+    memcpy(gdt_base, (void*)new_env->gdt.base, sizeof(uint64_t) * TARGET_GDT_ENTRIES);
+    new_env->idt.base = (target_ulong)idt_base;
+    new_env->gdt.base = (target_ulong)gdt_base;
+#endif
+
     /* Clone all break/watchpoints.
        Note: Once we support ptrace with hw-debug register access, make sure
        BP_CPU break/watchpoints are handled correctly on clone. */
-- 
2.34.1


[-- Attachment #2: Type: text/html, Size: 2391 bytes --]

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] linux-user: fix bug about incorrect base addresss of idt and  gdt on i386 and x86_64
  2023-01-01 15:57 fanwj
@ 2023-01-01 16:46 ` Alex Bennée
  0 siblings, 0 replies; 6+ messages in thread
From: Alex Bennée @ 2023-01-01 16:46 UTC (permalink / raw)
  To: fanwj; +Cc: qemu-devel


fanwj@mail.ustc.edu.cn writes:

> From 4601a624f40b2c89e7df2dec1adffb4f4308ba2d Mon Sep 17 00:00:00 2001
> From: fanwenjie <fanwj@mail.ustc.edu.cn>
> Date: Sun, 1 Jan 2023 23:13:34 +0800
> Subject: [PATCH] linux-user: fix bug about incorrect base addresss of idt and
>  gdt on i386 and x86_64
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1405
> Signed-off-by: fanwenjie <fanwj@mail.ustc.edu.cn>
> ---
>  linux-user/main.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/linux-user/main.c b/linux-user/main.c
> index a17fed045b..5d673c95b3 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -171,6 +171,12 @@ void fork_end(int child)
>  
>  __thread CPUState *thread_cpu;
>  
> +#if defined(TARGET_I386) || defined(TARGET_X86_64)
> +#include <stdalign.h>
> +__thread alignas(TARGET_PAGE_SIZE) static uint64_t gdt_base[TARGET_GDT_ENTRIES];
> +__thread alignas(TARGET_PAGE_SIZE) static uint64_t idt_base[TARGET_PAGE_SIZE / sizeof(uint64_t)];
> +#endif
> +
>  bool qemu_cpu_is_self(CPUState *cpu)
>  {
>      return thread_cpu == cpu;
> @@ -235,6 +241,13 @@ 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)
> +    memcpy(idt_base, (void*)new_env->idt.base, sizeof(uint64_t) * (new_env->idt.limit + 1));
> +    memcpy(gdt_base, (void*)new_env->gdt.base, sizeof(uint64_t) * TARGET_GDT_ENTRIES);
> +    new_env->idt.base = (target_ulong)idt_base;
> +    new_env->gdt.base = (target_ulong)gdt_base;
> +#endif
> +

This is the wrong place to copy target specific bits of code. I think
this belongs with cpu_clone_regs_child and the gdt/idt structures in
linux-user/i386/cpu_loop.c I think.

>      /* Clone all break/watchpoints.
>         Note: Once we support ptrace with hw-debug register access, make sure
>         BP_CPU break/watchpoints are handled correctly on clone. */


-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] linux-user: fix bug about incorrect base addresss of idt and  gdt on i386 and x86_64
@ 2023-01-02  3:06 fanwj
  0 siblings, 0 replies; 6+ messages in thread
From: fanwj @ 2023-01-02  3:06 UTC (permalink / raw)
  To: qemu-devel; +Cc: laurent

[-- Attachment #1: Type: text/plain, Size: 2494 bytes --]

From: fanwenjie <fanwj@mail.ustc.edu.cn>


On linux user mode, CPUX86State::idt::base and CPUX86State::gdt::base from Different CPUX86State Objects have same value, It is incorrect! Every CPUX86State::idt::base and 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>


---
 linux-user/i386/cpu_loop.c | 10 ++++++++++
 linux-user/main.c          | 11 +++++++++++
 2 files changed, 21 insertions(+)


diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c
index 865413c08f..1f23bc5e3a 100644
--- a/linux-user/i386/cpu_loop.c
+++ b/linux-user/i386/cpu_loop.c
@@ -314,8 +314,18 @@ void cpu_loop(CPUX86State *env)
     }
 }
 
+static void target_cpu_free(void *obj)
+{
+    CPUArchState* env = ((CPUState*)obj)->env_ptr;
+    target_munmap(env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1));
+    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) {
diff --git a/linux-user/main.c b/linux-user/main.c
index a17fed045b..2276040548 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -234,6 +234,17 @@ 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);
+    new_env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
+                                PROT_READ|PROT_WRITE,
+                                MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+    memcpy((void*)new_env->gdt.base, (void*)env->gdt.base, sizeof(uint64_t) * TARGET_GDT_ENTRIES);
+    memcpy((void*)new_env->idt.base, (void*)env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1));
+    OBJECT(new_cpu)->free = OBJECT(cpu)->free;
+#endif
 
     /* Clone all break/watchpoints.
        Note: Once we support ptrace with hw-debug register access, make sure
-- 
2.34.1




[-- Attachment #2: Type: text/html, Size: 3870 bytes --]

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] linux-user: fix bug about incorrect base addresss of idt and gdt on i386 and x86_64
@ 2023-01-02  9:44 fanwj
  0 siblings, 0 replies; 6+ messages in thread
From: fanwj @ 2023-01-02  9:44 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 2184 bytes --]

On linux user mode, CPUX86State::idt::base and CPUX86State::gdt::base from Different CPUX86State Objects have same value, It is incorrect! Every CPUX86State::idt::base and Every CPUX86State::gdt::base Must points to independent memory space. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1405 Signed-off-by: fanwenjie --- linux-user/i386/cpu_loop.c | 10 ++++++++++ linux-user/main.c | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c index 865413c08f..1f23bc5e3a 100644 --- a/linux-user/i386/cpu_loop.c +++ b/linux-user/i386/cpu_loop.c @@ -314,8 +314,18 @@ void cpu_loop(CPUX86State *env) } } +static void target_cpu_free(void *obj) +{ + CPUArchState* env = ((CPUState*)obj)->env_ptr; + target_munmap(env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1)); + 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) { diff --git a/linux-user/main.c b/linux-user/main.c index a17fed045b..2276040548 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -234,6 +234,17 @@ 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); + new_env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1), + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + memcpy((void*)new_env->gdt.base, (void*)env->gdt.base, sizeof(uint64_t) * TARGET_GDT_ENTRIES); + memcpy((void*)new_env->idt.base, (void*)env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1)); + OBJECT(new_cpu)->free = OBJECT(cpu)->free; +#endif /* Clone all break/watchpoints. Note: Once we support ptrace with hw-debug register access, make sure -- 2.34.1

[-- Attachment #2: Type: text/html, Size: 2499 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] linux-user: fix bug about incorrect base addresss of idt and gdt on i386 and x86_64
@ 2023-01-02 10:00 fanwenjie
  0 siblings, 0 replies; 6+ messages in thread
From: fanwenjie @ 2023-01-02 10:00 UTC (permalink / raw)
  To: qemu-devel; +Cc: fanwenjie

---
 linux-user/i386/cpu_loop.c | 10 ++++++++++
 linux-user/main.c          | 11 +++++++++++
 2 files changed, 21 insertions(+)

diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c
index 865413c08f..1f23bc5e3a 100644
--- a/linux-user/i386/cpu_loop.c
+++ b/linux-user/i386/cpu_loop.c
@@ -314,8 +314,18 @@ void cpu_loop(CPUX86State *env)
     }
 }
 
+static void target_cpu_free(void *obj)
+{
+    CPUArchState* env = ((CPUState*)obj)->env_ptr;
+    target_munmap(env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1));
+    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) {
diff --git a/linux-user/main.c b/linux-user/main.c
index a17fed045b..2276040548 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -234,6 +234,17 @@ 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);
+    new_env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
+                                PROT_READ|PROT_WRITE,
+                                MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+    memcpy((void*)new_env->gdt.base, (void*)env->gdt.base, sizeof(uint64_t) * TARGET_GDT_ENTRIES);
+    memcpy((void*)new_env->idt.base, (void*)env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1));
+    OBJECT(new_cpu)->free = OBJECT(cpu)->free;
+#endif
 
     /* Clone all break/watchpoints.
        Note: Once we support ptrace with hw-debug register access, make sure
-- 
2.34.1




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] linux-user: fix bug about incorrect base addresss of idt and gdt on i386 and x86_64
@ 2023-01-02 10:03 fanwenjie
  0 siblings, 0 replies; 6+ messages in thread
From: fanwenjie @ 2023-01-02 10:03 UTC (permalink / raw)
  To: qemu-devel; +Cc: laurent, fanwenjie

On linux user mode, CPUX86State::idt::base and CPUX86State::gdt::base from Different CPUX86State Objects have same value, It is incorrect! Every CPUX86State::idt::base and 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>

---
 linux-user/i386/cpu_loop.c | 10 ++++++++++
 linux-user/main.c          | 11 +++++++++++
 2 files changed, 21 insertions(+)

diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c
index 865413c08f..1f23bc5e3a 100644
--- a/linux-user/i386/cpu_loop.c
+++ b/linux-user/i386/cpu_loop.c
@@ -314,8 +314,18 @@ void cpu_loop(CPUX86State *env)
     }
 }
 
+static void target_cpu_free(void *obj)
+{
+    CPUArchState* env = ((CPUState*)obj)->env_ptr;
+    target_munmap(env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1));
+    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) {
diff --git a/linux-user/main.c b/linux-user/main.c
index a17fed045b..2276040548 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -234,6 +234,17 @@ 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);
+    new_env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
+                                PROT_READ|PROT_WRITE,
+                                MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+    memcpy((void*)new_env->gdt.base, (void*)env->gdt.base, sizeof(uint64_t) * TARGET_GDT_ENTRIES);
+    memcpy((void*)new_env->idt.base, (void*)env->idt.base, sizeof(uint64_t) * (env->idt.limit + 1));
+    OBJECT(new_cpu)->free = OBJECT(cpu)->free;
+#endif
 
     /* Clone all break/watchpoints.
        Note: Once we support ptrace with hw-debug register access, make sure
-- 
2.34.1




^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-01-02 10:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-02 10:00 [PATCH] linux-user: fix bug about incorrect base addresss of idt and gdt on i386 and x86_64 fanwenjie
  -- strict thread matches above, loose matches on Subject: below --
2023-01-02 10:03 fanwenjie
2023-01-02  9:44 fanwj
2023-01-02  3:06 fanwj
2023-01-01 15:57 fanwj
2023-01-01 16:46 ` Alex Bennée

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).