diff mbox series

[v2,41/43] bsd-user: Implement cpu_copy() helper routine

Message ID 20210826211201.98877-42-imp@bsdimp.com
State New
Headers show
Series bsd-user updates to run hello world | expand

Commit Message

Warner Losh Aug. 26, 2021, 9:11 p.m. UTC
From: Warner Losh <imp@FreeBSD.org>

cpu_copy shouldbe called when processes are creating new threads. It
copies the current state of the CPU to a new cpu state needed for the
new thread.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Justin Hibbits <chmeeedalf@gmail.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 bsd-user/main.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

Comments

Philippe Mathieu-Daudé Aug. 27, 2021, 4:47 a.m. UTC | #1
On 8/26/21 11:11 PM, imp@bsdimp.com wrote:
> From: Warner Losh <imp@FreeBSD.org>
> 
> cpu_copy shouldbe called when processes are creating new threads. It

Typo "should be"

> copies the current state of the CPU to a new cpu state needed for the
> new thread.
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> Signed-off-by: Justin Hibbits <chmeeedalf@gmail.com>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  bsd-user/main.c | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/bsd-user/main.c b/bsd-user/main.c
> index e2ed9e32ba..b35bcf4d1e 100644
> --- a/bsd-user/main.c
> +++ b/bsd-user/main.c
> @@ -180,6 +180,36 @@ void init_task_state(TaskState *ts)
>      ts->sigqueue_table[i].next = NULL;
>  }
>  
> +CPUArchState *cpu_copy(CPUArchState *env)
> +{
> +    CPUState *cpu = env_cpu(env);
> +    CPUState *new_cpu = cpu_create(cpu_type);
> +    CPUArchState *new_env = new_cpu->env_ptr;
> +    CPUBreakpoint *bp;
> +    CPUWatchpoint *wp;
> +
> +    /* Reset non arch specific state */
> +    cpu_reset(new_cpu);
> +
> +    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;
> +}

But where is it called?
Warner Losh Aug. 27, 2021, 2:56 p.m. UTC | #2
> On Aug 26, 2021, at 10:47 PM, Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
> 
> On 8/26/21 11:11 PM, imp@bsdimp.com wrote:
>> From: Warner Losh <imp@FreeBSD.org>
>> 
>> cpu_copy shouldbe called when processes are creating new threads. It
> 
> Typo "should be"
> 
>> copies the current state of the CPU to a new cpu state needed for the
>> new thread.
>> 
>> Signed-off-by: Stacey Son <sson@FreeBSD.org>
>> Signed-off-by: Warner Losh <imp@bsdimp.com>
>> Signed-off-by: Justin Hibbits <chmeeedalf@gmail.com>
>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>> ---
>> bsd-user/main.c | 30 ++++++++++++++++++++++++++++++
>> 1 file changed, 30 insertions(+)
>> 
>> diff --git a/bsd-user/main.c b/bsd-user/main.c
>> index e2ed9e32ba..b35bcf4d1e 100644
>> --- a/bsd-user/main.c
>> +++ b/bsd-user/main.c
>> @@ -180,6 +180,36 @@ void init_task_state(TaskState *ts)
>>     ts->sigqueue_table[i].next = NULL;
>> }
>> 
>> +CPUArchState *cpu_copy(CPUArchState *env)
>> +{
>> +    CPUState *cpu = env_cpu(env);
>> +    CPUState *new_cpu = cpu_create(cpu_type);
>> +    CPUArchState *new_env = new_cpu->env_ptr;
>> +    CPUBreakpoint *bp;
>> +    CPUWatchpoint *wp;
>> +
>> +    /* Reset non arch specific state */
>> +    cpu_reset(new_cpu);
>> +
>> +    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;
>> +}
> 
> But where is it called?

It’s in the bsd-user fork’d proc code:

https://github.com/qemu-bsd-user/qemu-bsd-user/blob/079d45942db8d1038806cb459992b4f016b52b51/bsd-user/freebsd/os-thread.c#L1566

Is where it’s called from. I wanted to get it out of the way in this review since I was trying to get all the changes to main.c done, but if you’d like, I can drop it and submit in the next round.

Warner
Philippe Mathieu-Daudé Aug. 27, 2021, 4 p.m. UTC | #3
On 8/27/21 4:56 PM, Warner Losh wrote:
>> On Aug 26, 2021, at 10:47 PM, Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>>
>> On 8/26/21 11:11 PM, imp@bsdimp.com wrote:
>>> From: Warner Losh <imp@FreeBSD.org>
>>>
>>> cpu_copy shouldbe called when processes are creating new threads. It
>>
>> Typo "should be"
>>
>>> copies the current state of the CPU to a new cpu state needed for the
>>> new thread.
>>>
>>> Signed-off-by: Stacey Son <sson@FreeBSD.org>
>>> Signed-off-by: Warner Losh <imp@bsdimp.com>
>>> Signed-off-by: Justin Hibbits <chmeeedalf@gmail.com>
>>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>>> ---
>>> bsd-user/main.c | 30 ++++++++++++++++++++++++++++++
>>> 1 file changed, 30 insertions(+)
>>>
>>> diff --git a/bsd-user/main.c b/bsd-user/main.c
>>> index e2ed9e32ba..b35bcf4d1e 100644
>>> --- a/bsd-user/main.c
>>> +++ b/bsd-user/main.c
>>> @@ -180,6 +180,36 @@ void init_task_state(TaskState *ts)
>>>     ts->sigqueue_table[i].next = NULL;
>>> }
>>>
>>> +CPUArchState *cpu_copy(CPUArchState *env)
>>> +{
>>> +    CPUState *cpu = env_cpu(env);
>>> +    CPUState *new_cpu = cpu_create(cpu_type);
>>> +    CPUArchState *new_env = new_cpu->env_ptr;
>>> +    CPUBreakpoint *bp;
>>> +    CPUWatchpoint *wp;
>>> +
>>> +    /* Reset non arch specific state */
>>> +    cpu_reset(new_cpu);
>>> +
>>> +    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;
>>> +}
>>
>> But where is it called?
> 
> It’s in the bsd-user fork’d proc code:
> 
> https://github.com/qemu-bsd-user/qemu-bsd-user/blob/079d45942db8d1038806cb459992b4f016b52b51/bsd-user/freebsd/os-thread.c#L1566
> 
> Is where it’s called from. I wanted to get it out of the way in this review since I was trying to get all the changes to main.c done, but if you’d like, I can drop it and submit in the next round.

Better keep it for next round :)
Warner Losh Aug. 27, 2021, 4:30 p.m. UTC | #4
> On Aug 27, 2021, at 10:00 AM, Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
> 
> On 8/27/21 4:56 PM, Warner Losh wrote:
>>> On Aug 26, 2021, at 10:47 PM, Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>>> 
>>> On 8/26/21 11:11 PM, imp@bsdimp.com wrote:
>>>> From: Warner Losh <imp@FreeBSD.org>
>>>> 
>>>> cpu_copy shouldbe called when processes are creating new threads. It
>>> 
>>> Typo "should be"
>>> 
>>>> copies the current state of the CPU to a new cpu state needed for the
>>>> new thread.
>>>> 
>>>> Signed-off-by: Stacey Son <sson@FreeBSD.org>
>>>> Signed-off-by: Warner Losh <imp@bsdimp.com>
>>>> Signed-off-by: Justin Hibbits <chmeeedalf@gmail.com>
>>>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>>>> ---
>>>> bsd-user/main.c | 30 ++++++++++++++++++++++++++++++
>>>> 1 file changed, 30 insertions(+)
>>>> 
>>>> diff --git a/bsd-user/main.c b/bsd-user/main.c
>>>> index e2ed9e32ba..b35bcf4d1e 100644
>>>> --- a/bsd-user/main.c
>>>> +++ b/bsd-user/main.c
>>>> @@ -180,6 +180,36 @@ void init_task_state(TaskState *ts)
>>>>    ts->sigqueue_table[i].next = NULL;
>>>> }
>>>> 
>>>> +CPUArchState *cpu_copy(CPUArchState *env)
>>>> +{
>>>> +    CPUState *cpu = env_cpu(env);
>>>> +    CPUState *new_cpu = cpu_create(cpu_type);
>>>> +    CPUArchState *new_env = new_cpu->env_ptr;
>>>> +    CPUBreakpoint *bp;
>>>> +    CPUWatchpoint *wp;
>>>> +
>>>> +    /* Reset non arch specific state */
>>>> +    cpu_reset(new_cpu);
>>>> +
>>>> +    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;
>>>> +}
>>> 
>>> But where is it called?
>> 
>> It’s in the bsd-user fork’d proc code:
>> 
>> https://github.com/qemu-bsd-user/qemu-bsd-user/blob/079d45942db8d1038806cb459992b4f016b52b51/bsd-user/freebsd/os-thread.c#L1566
>> 
>> Is where it’s called from. I wanted to get it out of the way in this review since I was trying to get all the changes to main.c done, but if you’d like, I can drop it and submit in the next round.
> 
> Better keep it for next round :)

OK. I’ll drop and queue up next time.

Warner
diff mbox series

Patch

diff --git a/bsd-user/main.c b/bsd-user/main.c
index e2ed9e32ba..b35bcf4d1e 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -180,6 +180,36 @@  void init_task_state(TaskState *ts)
     ts->sigqueue_table[i].next = NULL;
 }
 
+CPUArchState *cpu_copy(CPUArchState *env)
+{
+    CPUState *cpu = env_cpu(env);
+    CPUState *new_cpu = cpu_create(cpu_type);
+    CPUArchState *new_env = new_cpu->env_ptr;
+    CPUBreakpoint *bp;
+    CPUWatchpoint *wp;
+
+    /* Reset non arch specific state */
+    cpu_reset(new_cpu);
+
+    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;