mbox series

[v2,00/23] hw, target: Prefer fast cpu_env() over slower CPU QOM cast macro

Message ID 20240126220407.95022-1-philmd@linaro.org
Headers show
Series hw, target: Prefer fast cpu_env() over slower CPU QOM cast macro | expand

Message

Philippe Mathieu-Daudé Jan. 26, 2024, 10:03 p.m. UTC
Use cpu_env() -- which is fast path -- when possible.
Bulk conversion using Coccinelle spatch (script included).

Since v1:
- Avoid CPU() cast (Paolo)
- Split per targets (Thomas)

Philippe Mathieu-Daudé (23):
  hw/acpi/cpu: Use CPUState typedef
  scripts/coccinelle: Add cpu_env.cocci_template script
  bulk: Call in place single use cpu_env()
  target/alpha: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/arm: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/avr: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/cris: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/hppa: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/i386/hvf: Use CPUState typedef
  target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/m68k: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/microblaze: Prefer fast cpu_env() over slower CPU QOM cast
    macro
  target/mips: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/nios2: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/openrisc: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/ppc: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/riscv: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/rx: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/s390x: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/sh4: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/tricore: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/xtensa: Prefer fast cpu_env() over slower CPU QOM cast macro
  target/sparc: Prefer fast cpu_env() over slower CPU QOM cast macro

 MAINTAINERS                               |  1 +
 include/hw/acpi/cpu.h                     |  2 +-
 target/i386/hvf/vmx.h                     | 13 +---
 target/i386/hvf/x86.h                     | 26 +++----
 target/i386/hvf/x86_descr.h               | 14 ++--
 target/i386/hvf/x86_emu.h                 |  4 +-
 target/i386/hvf/x86_mmu.h                 |  6 +-
 accel/tcg/cpu-exec.c                      |  3 +-
 hw/i386/vmmouse.c                         |  6 +-
 hw/i386/xen/xen-hvm.c                     |  3 +-
 hw/intc/arm_gicv3_cpuif_common.c          |  5 +-
 hw/ppc/mpc8544_guts.c                     |  3 +-
 hw/ppc/pnv.c                              |  3 +-
 hw/ppc/pnv_xscom.c                        |  5 +-
 hw/ppc/ppce500_spin.c                     |  3 +-
 hw/ppc/spapr.c                            |  3 +-
 hw/ppc/spapr_caps.c                       |  6 +-
 linux-user/i386/cpu_loop.c                |  4 +-
 target/alpha/cpu.c                        | 31 ++------
 target/alpha/gdbstub.c                    |  6 +-
 target/alpha/helper.c                     | 12 +--
 target/alpha/mem_helper.c                 | 11 +--
 target/arm/cpu.c                          | 19 ++---
 target/arm/debug_helper.c                 |  8 +-
 target/arm/gdbstub.c                      |  6 +-
 target/arm/gdbstub64.c                    |  6 +-
 target/arm/helper.c                       |  9 +--
 target/arm/hvf/hvf.c                      | 12 +--
 target/arm/kvm.c                          |  3 +-
 target/arm/ptw.c                          |  3 +-
 target/arm/tcg/cpu32.c                    |  3 +-
 target/avr/cpu.c                          | 27 ++-----
 target/avr/gdbstub.c                      |  6 +-
 target/avr/helper.c                       | 10 +--
 target/cris/cpu.c                         |  5 +-
 target/cris/gdbstub.c                     |  9 +--
 target/cris/helper.c                      | 12 +--
 target/cris/translate.c                   |  3 +-
 target/hppa/cpu.c                         |  8 +-
 target/hppa/int_helper.c                  |  8 +-
 target/hppa/mem_helper.c                  |  6 +-
 target/hppa/translate.c                   |  3 +-
 target/i386/arch_memory_mapping.c         |  3 +-
 target/i386/cpu-dump.c                    |  3 +-
 target/i386/cpu.c                         | 37 +++------
 target/i386/helper.c                      | 39 +++-------
 target/i386/hvf/hvf.c                     |  8 +-
 target/i386/hvf/x86.c                     | 30 ++++----
 target/i386/hvf/x86_descr.c               |  8 +-
 target/i386/hvf/x86_emu.c                 |  6 +-
 target/i386/hvf/x86_mmu.c                 | 14 ++--
 target/i386/hvf/x86_task.c                | 10 +--
 target/i386/hvf/x86hvf.c                  |  6 +-
 target/i386/kvm/kvm.c                     |  6 +-
 target/i386/kvm/xen-emu.c                 | 32 +++-----
 target/i386/nvmm/nvmm-all.c               |  6 +-
 target/i386/tcg/sysemu/bpt_helper.c       |  3 +-
 target/i386/tcg/tcg-cpu.c                 | 14 +---
 target/i386/tcg/user/excp_helper.c        |  3 +-
 target/i386/tcg/user/seg_helper.c         |  3 +-
 target/i386/whpx/whpx-all.c               | 18 ++---
 target/loongarch/tcg/translate.c          |  3 +-
 target/m68k/cpu.c                         | 30 +++-----
 target/m68k/gdbstub.c                     |  6 +-
 target/m68k/helper.c                      |  3 +-
 target/m68k/m68k-semi.c                   |  6 +-
 target/m68k/op_helper.c                   | 11 +--
 target/m68k/translate.c                   |  3 +-
 target/microblaze/helper.c                |  3 +-
 target/microblaze/translate.c             |  3 +-
 target/mips/cpu.c                         | 11 +--
 target/mips/gdbstub.c                     |  6 +-
 target/mips/kvm.c                         | 27 +++----
 target/mips/sysemu/physaddr.c             |  3 +-
 target/mips/tcg/exception.c               |  3 +-
 target/mips/tcg/op_helper.c               |  3 +-
 target/mips/tcg/sysemu/special_helper.c   |  3 +-
 target/mips/tcg/sysemu/tlb_helper.c       |  6 +-
 target/mips/tcg/translate.c               |  3 +-
 target/nios2/cpu.c                        | 15 +---
 target/nios2/helper.c                     |  3 +-
 target/nios2/nios2-semi.c                 |  6 +-
 target/openrisc/gdbstub.c                 |  3 +-
 target/openrisc/interrupt.c               |  6 +-
 target/openrisc/translate.c               |  3 +-
 target/ppc/cpu_init.c                     | 11 +--
 target/ppc/excp_helper.c                  |  3 +-
 target/ppc/gdbstub.c                      | 12 +--
 target/ppc/kvm.c                          |  6 +-
 target/ppc/ppc-qmp-cmds.c                 |  3 +-
 target/ppc/user_only_helper.c             |  3 +-
 target/riscv/arch_dump.c                  |  6 +-
 target/riscv/cpu.c                        | 17 ++---
 target/riscv/cpu_helper.c                 | 14 +---
 target/riscv/debug.c                      |  9 +--
 target/riscv/gdbstub.c                    |  6 +-
 target/riscv/kvm/kvm-cpu.c                |  6 +-
 target/riscv/tcg/tcg-cpu.c                |  9 +--
 target/riscv/translate.c                  |  3 +-
 target/rx/gdbstub.c                       |  6 +-
 target/rx/helper.c                        |  6 +-
 target/rx/translate.c                     |  6 +-
 target/s390x/cpu-dump.c                   |  3 +-
 target/s390x/gdbstub.c                    |  6 +-
 target/s390x/helper.c                     |  3 +-
 target/s390x/kvm/kvm.c                    |  6 +-
 target/s390x/tcg/excp_helper.c            | 11 +--
 target/s390x/tcg/translate.c              |  3 +-
 target/sh4/cpu.c                          | 15 ++--
 target/sh4/gdbstub.c                      |  6 +-
 target/sh4/helper.c                       | 11 +--
 target/sh4/op_helper.c                    |  4 +-
 target/sh4/translate.c                    |  3 +-
 target/sparc/cpu.c                        | 14 +---
 target/sparc/gdbstub.c                    |  3 +-
 target/sparc/int32_helper.c               |  3 +-
 target/sparc/int64_helper.c               |  3 +-
 target/sparc/ldst_helper.c                |  6 +-
 target/sparc/mmu_helper.c                 | 15 ++--
 target/sparc/translate.c                  |  3 +-
 target/tricore/cpu.c                      | 20 +----
 target/tricore/gdbstub.c                  |  6 +-
 target/tricore/helper.c                   |  3 +-
 target/tricore/translate.c                |  3 +-
 target/xtensa/dbg_helper.c                |  3 +-
 target/xtensa/exc_helper.c                |  3 +-
 target/xtensa/gdbstub.c                   |  6 +-
 target/xtensa/helper.c                    |  9 +--
 target/xtensa/translate.c                 |  3 +-
 scripts/coccinelle/cpu_env.cocci_template | 92 +++++++++++++++++++++++
 130 files changed, 432 insertions(+), 714 deletions(-)
 create mode 100644 scripts/coccinelle/cpu_env.cocci_template

Comments

Richard Henderson Jan. 27, 2024, 4:31 a.m. UTC | #1
On 1/27/24 08:03, Philippe Mathieu-Daudé wrote:
> Mechanical patch produced running the command documented
> in scripts/coccinelle/cpu_env.cocci_template header.
> 
> Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org>
> ---
>   target/i386/hvf/vmx.h               | 13 +++-------
>   hw/i386/vmmouse.c                   |  6 ++---
>   hw/i386/xen/xen-hvm.c               |  3 +--
>   target/i386/arch_memory_mapping.c   |  3 +--
>   target/i386/cpu-dump.c              |  3 +--
>   target/i386/cpu.c                   | 37 +++++++++------------------
>   target/i386/helper.c                | 39 ++++++++---------------------
>   target/i386/hvf/hvf.c               |  8 ++----
>   target/i386/hvf/x86.c               |  4 +--
>   target/i386/hvf/x86_emu.c           |  6 ++---
>   target/i386/hvf/x86_task.c          | 10 +++-----
>   target/i386/hvf/x86hvf.c            |  6 ++---
>   target/i386/kvm/kvm.c               |  6 ++---
>   target/i386/kvm/xen-emu.c           | 32 ++++++++---------------
>   target/i386/tcg/sysemu/bpt_helper.c |  3 +--
>   target/i386/tcg/tcg-cpu.c           | 14 +++--------
>   target/i386/tcg/user/excp_helper.c  |  3 +--
>   target/i386/tcg/user/seg_helper.c   |  3 +--
>   18 files changed, 59 insertions(+), 140 deletions(-)

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

r~
David Woodhouse Jan. 27, 2024, 10:07 a.m. UTC | #2
On Fri, 2024-01-26 at 23:03 +0100, Philippe Mathieu-Daudé wrote:
> Mechanical patch produced running the command documented
> in scripts/coccinelle/cpu_env.cocci_template header.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---

For the KVM/Xen parts

Acked-by: David Woodhouse <dwmw@amazon.co.uk>
Zhao Liu Jan. 27, 2024, 12:21 p.m. UTC | #3
Hi Philippe,

On Fri, Jan 26, 2024 at 11:03:52PM +0100, Philippe Mathieu-Daudé wrote:
> Date: Fri, 26 Jan 2024 23:03:52 +0100
> From: Philippe Mathieu-Daudé <philmd@linaro.org>
> Subject: [PATCH v2 10/23] target/i386: Prefer fast cpu_env() over slower
>  CPU QOM cast macro
> X-Mailer: git-send-email 2.41.0
> 
> Mechanical patch produced running the command documented
> in scripts/coccinelle/cpu_env.cocci_template header.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  target/i386/hvf/vmx.h               | 13 +++-------
>  hw/i386/vmmouse.c                   |  6 ++---
>  hw/i386/xen/xen-hvm.c               |  3 +--
>  target/i386/arch_memory_mapping.c   |  3 +--
>  target/i386/cpu-dump.c              |  3 +--
>  target/i386/cpu.c                   | 37 +++++++++------------------
>  target/i386/helper.c                | 39 ++++++++---------------------
>  target/i386/hvf/hvf.c               |  8 ++----
>  target/i386/hvf/x86.c               |  4 +--
>  target/i386/hvf/x86_emu.c           |  6 ++---
>  target/i386/hvf/x86_task.c          | 10 +++-----
>  target/i386/hvf/x86hvf.c            |  6 ++---
>  target/i386/kvm/kvm.c               |  6 ++---
>  target/i386/kvm/xen-emu.c           | 32 ++++++++---------------
>  target/i386/tcg/sysemu/bpt_helper.c |  3 +--
>  target/i386/tcg/tcg-cpu.c           | 14 +++--------
>  target/i386/tcg/user/excp_helper.c  |  3 +--
>  target/i386/tcg/user/seg_helper.c   |  3 +--
>  18 files changed, 59 insertions(+), 140 deletions(-)
> 

[snip]

> diff --git a/target/i386/hvf/x86hvf.c b/target/i386/hvf/x86hvf.c
> index 3b1ef5f49a..1e7fd587fe 100644
> --- a/target/i386/hvf/x86hvf.c
> +++ b/target/i386/hvf/x86hvf.c
> @@ -238,8 +238,7 @@ void hvf_get_msrs(CPUState *cs)
>  
>  int hvf_put_registers(CPUState *cs)
>  {
> -    X86CPU *x86cpu = X86_CPU(cs);
> -    CPUX86State *env = &x86cpu->env;
> +    CPUX86State *env = cpu_env(cs);
>  
>      wreg(cs->accel->fd, HV_X86_RAX, env->regs[R_EAX]);
>      wreg(cs->accel->fd, HV_X86_RBX, env->regs[R_EBX]);
> @@ -282,8 +281,7 @@ int hvf_put_registers(CPUState *cs)
>  
>  int hvf_get_registers(CPUState *cs)
>  {
> -    X86CPU *x86cpu = X86_CPU(cs);
> -    CPUX86State *env = &x86cpu->env;
> +    CPUX86State *env = cpu_env(cs);
>  
>      env->regs[R_EAX] = rreg(cs->accel->fd, HV_X86_RAX);
>      env->regs[R_EBX] = rreg(cs->accel->fd, HV_X86_RBX);

In this file, there's another corner case:

diff --git a/target/i386/hvf/x86hvf.c b/target/i386/hvf/x86hvf.c
index 3b1ef5f49a8a..9a145aa5aa4f 100644
--- a/target/i386/hvf/x86hvf.c
+++ b/target/i386/hvf/x86hvf.c
@@ -342,8 +342,7 @@ void vmx_clear_int_window_exiting(CPUState *cs)

 bool hvf_inject_interrupts(CPUState *cs)
 {
-    X86CPU *x86cpu = X86_CPU(cs);
-    CPUX86State *env = &x86cpu->env;
+    CPUX86State *env = cpu_env(cs);

     uint8_t vector;
     uint64_t intr_type;
@@ -408,7 +407,7 @@ bool hvf_inject_interrupts(CPUState *cs)
     if (!(env->hflags & HF_INHIBIT_IRQ_MASK) &&
         (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
         (env->eflags & IF_MASK) && !(info & VMCS_INTR_VALID)) {
-        int line = cpu_get_pic_interrupt(&x86cpu->env);
+        int line = cpu_get_pic_interrupt(env);
         cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
         if (line >= 0) {
             wvmcs(cs->accel->fd, VMCS_ENTRY_INTR_INFO, line |


For this special case, I'm not sure if the script can cover it as well,
otherwise maybe it's OK to be cleaned up manually ;-).

> diff --git a/target/i386/tcg/user/excp_helper.c b/target/i386/tcg/user/excp_helper.c
> index b3bdb7831a..bfcae9f39e 100644
> --- a/target/i386/tcg/user/excp_helper.c
> +++ b/target/i386/tcg/user/excp_helper.c
> @@ -26,8 +26,7 @@ void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr,
>                              MMUAccessType access_type,
>                              bool maperr, uintptr_t ra)
>  {
> -    X86CPU *cpu = X86_CPU(cs);
> -    CPUX86State *env = &cpu->env;
> +    CPUX86State *env = cpu_env(cs);
>  
>      /*
>       * The error_code that hw reports as part of the exception frame

In this file, there's another case:

diff --git a/target/i386/tcg/user/excp_helper.c b/target/i386/tcg/user/excp_helper.c
index b3bdb7831a7a..02fcd64fc080 100644
--- a/target/i386/tcg/user/excp_helper.c
+++ b/target/i386/tcg/user/excp_helper.c
@@ -52,6 +52,5 @@ void x86_cpu_record_sigsegv(CPUState *cs, vaddr addr,
 void x86_cpu_record_sigbus(CPUState *cs, vaddr addr,
                            MMUAccessType access_type, uintptr_t ra)
 {
-    X86CPU *cpu = X86_CPU(cs);
-    handle_unaligned_access(&cpu->env, addr, access_type, ra);
+    handle_unaligned_access(cpu_env(cs), addr, access_type, ra);
 }

[snip]

LGTM.
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Philippe Mathieu-Daudé Jan. 28, 2024, 4:16 p.m. UTC | #4
On 27/1/24 13:21, Zhao Liu wrote:
> Hi Philippe,
> 
> On Fri, Jan 26, 2024 at 11:03:52PM +0100, Philippe Mathieu-Daudé wrote:
>> Date: Fri, 26 Jan 2024 23:03:52 +0100
>> From: Philippe Mathieu-Daudé <philmd@linaro.org>
>> Subject: [PATCH v2 10/23] target/i386: Prefer fast cpu_env() over slower
>>   CPU QOM cast macro
>> X-Mailer: git-send-email 2.41.0
>>
>> Mechanical patch produced running the command documented
>> in scripts/coccinelle/cpu_env.cocci_template header.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>>   target/i386/hvf/vmx.h               | 13 +++-------
>>   hw/i386/vmmouse.c                   |  6 ++---
>>   hw/i386/xen/xen-hvm.c               |  3 +--
>>   target/i386/arch_memory_mapping.c   |  3 +--
>>   target/i386/cpu-dump.c              |  3 +--
>>   target/i386/cpu.c                   | 37 +++++++++------------------
>>   target/i386/helper.c                | 39 ++++++++---------------------
>>   target/i386/hvf/hvf.c               |  8 ++----
>>   target/i386/hvf/x86.c               |  4 +--
>>   target/i386/hvf/x86_emu.c           |  6 ++---
>>   target/i386/hvf/x86_task.c          | 10 +++-----
>>   target/i386/hvf/x86hvf.c            |  6 ++---
>>   target/i386/kvm/kvm.c               |  6 ++---
>>   target/i386/kvm/xen-emu.c           | 32 ++++++++---------------
>>   target/i386/tcg/sysemu/bpt_helper.c |  3 +--
>>   target/i386/tcg/tcg-cpu.c           | 14 +++--------
>>   target/i386/tcg/user/excp_helper.c  |  3 +--
>>   target/i386/tcg/user/seg_helper.c   |  3 +--
>>   18 files changed, 59 insertions(+), 140 deletions(-)
>>
> 
> [snip]
> 
>> diff --git a/target/i386/hvf/x86hvf.c b/target/i386/hvf/x86hvf.c
>> index 3b1ef5f49a..1e7fd587fe 100644
>> --- a/target/i386/hvf/x86hvf.c
>> +++ b/target/i386/hvf/x86hvf.c
>> @@ -238,8 +238,7 @@ void hvf_get_msrs(CPUState *cs)
>>   
>>   int hvf_put_registers(CPUState *cs)
>>   {
>> -    X86CPU *x86cpu = X86_CPU(cs);
>> -    CPUX86State *env = &x86cpu->env;
>> +    CPUX86State *env = cpu_env(cs);
>>   
>>       wreg(cs->accel->fd, HV_X86_RAX, env->regs[R_EAX]);
>>       wreg(cs->accel->fd, HV_X86_RBX, env->regs[R_EBX]);
>> @@ -282,8 +281,7 @@ int hvf_put_registers(CPUState *cs)
>>   
>>   int hvf_get_registers(CPUState *cs)
>>   {
>> -    X86CPU *x86cpu = X86_CPU(cs);
>> -    CPUX86State *env = &x86cpu->env;
>> +    CPUX86State *env = cpu_env(cs);
>>   
>>       env->regs[R_EAX] = rreg(cs->accel->fd, HV_X86_RAX);
>>       env->regs[R_EBX] = rreg(cs->accel->fd, HV_X86_RBX);
> 
> In this file, there's another corner case:
> 
> diff --git a/target/i386/hvf/x86hvf.c b/target/i386/hvf/x86hvf.c
> index 3b1ef5f49a8a..9a145aa5aa4f 100644
> --- a/target/i386/hvf/x86hvf.c
> +++ b/target/i386/hvf/x86hvf.c
> @@ -342,8 +342,7 @@ void vmx_clear_int_window_exiting(CPUState *cs)
> 
>   bool hvf_inject_interrupts(CPUState *cs)
>   {
> -    X86CPU *x86cpu = X86_CPU(cs);
> -    CPUX86State *env = &x86cpu->env;
> +    CPUX86State *env = cpu_env(cs);
> 
>       uint8_t vector;
>       uint64_t intr_type;
> @@ -408,7 +407,7 @@ bool hvf_inject_interrupts(CPUState *cs)
>       if (!(env->hflags & HF_INHIBIT_IRQ_MASK) &&
>           (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
>           (env->eflags & IF_MASK) && !(info & VMCS_INTR_VALID)) {
> -        int line = cpu_get_pic_interrupt(&x86cpu->env);
> +        int line = cpu_get_pic_interrupt(env);
>           cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
>           if (line >= 0) {
>               wvmcs(cs->accel->fd, VMCS_ENTRY_INTR_INFO, line |
> 
> 
> For this special case, I'm not sure if the script can cover it as well,
> otherwise maybe it's OK to be cleaned up manually ;-).

BTW I forgot to mention I had to skip target/i386/tcg/translate.c
(7100 LoC) because it is too complex for Coccinelle.