[PATCH 04/17] bsd-user: Implement cpu_copy()

Warner Losh posted 17 patches 3 months, 3 weeks ago
[PATCH 04/17] bsd-user: Implement cpu_copy()
Posted by Warner Losh 3 months, 3 weeks ago
From: Stacey Son <sson@FreeBSD.org>

Catch up with 30ba0ee52d15 and implement cpu_copy(). It's needed for
threading. Stacey's original code, with bug fixes from Jessica, Justin
and myself.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
Signed-off-by: Justin Hibbits <chmeeedalf@gmail.com>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/main.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 1533fd51168..9ad31bd1efe 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -224,6 +224,37 @@ void init_task_state(TaskState *ts)
     };
 }
 
+CPUArchState *cpu_copy(CPUArchState *env)
+{
+    CPUState *cpu = env_cpu(env);
+    CPUState *new_cpu = cpu_create(cpu_type);
+    CPUArchState *new_env = cpu_env(new_cpu);
+    CPUBreakpoint *bp;
+    CPUWatchpoint *wp;
+
+    /* Reset non arch specific state */
+    cpu_reset(new_cpu);
+
+    new_cpu->tcg_cflags = cpu->tcg_cflags;
+    memcpy(new_env, env, sizeof(CPUArchState));
+
+    /*
+     * 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.
+     */
+    QTAILQ_INIT(&cpu->breakpoints);
+    QTAILQ_INIT(&cpu->watchpoints);
+    QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
+        cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
+    }
+    QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
+        cpu_watchpoint_insert(new_cpu, wp->vaddr, wp->len, wp->flags, NULL);
+    }
+
+    return new_env;
+}
+
 void gemu_log(const char *fmt, ...)
 {
     va_list ap;
-- 
2.45.1
Re: [PATCH 04/17] bsd-user: Implement cpu_copy()
Posted by Richard Henderson 3 months, 3 weeks ago
On 8/3/24 09:56, Warner Losh wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Catch up with 30ba0ee52d15 and implement cpu_copy(). It's needed for
> threading. Stacey's original code, with bug fixes from Jessica, Justin
> and myself.
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Jessica Clarke<jrtc27@jrtc27.com>
> Signed-off-by: Justin Hibbits<chmeeedalf@gmail.com>
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/main.c | 31 +++++++++++++++++++++++++++++++
>   1 file changed, 31 insertions(+)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~