Re: [PATCH v2 10/23] target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro

2024-01-28 Thread Philippe Mathieu-Daudé

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é 
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é 
---
  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.



Re: [PATCH v2 10/23] target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro

2024-01-27 Thread Zhao Liu
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é 
> 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é 
> ---
>  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 





Re: [PATCH v2 10/23] target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro

2024-01-27 Thread David Woodhouse
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é 
> ---

For the KVM/Xen parts

Acked-by: David Woodhouse 


smime.p7s
Description: S/MIME cryptographic signature


Re: [PATCH v2 10/23] target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro

2024-01-26 Thread Richard Henderson

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é
---
  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 

r~



[PATCH v2 10/23] target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro

2024-01-26 Thread Philippe Mathieu-Daudé
Mechanical patch produced running the command documented
in scripts/coccinelle/cpu_env.cocci_template header.

Signed-off-by: Philippe Mathieu-Daudé 
---
 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(-)

diff --git a/target/i386/hvf/vmx.h b/target/i386/hvf/vmx.h
index 0fffcfa46c..1ad042269b 100644
--- a/target/i386/hvf/vmx.h
+++ b/target/i386/hvf/vmx.h
@@ -175,8 +175,7 @@ static inline void macvm_set_cr4(hv_vcpuid_t vcpu, uint64_t 
cr4)
 
 static inline void macvm_set_rip(CPUState *cpu, uint64_t rip)
 {
-X86CPU *x86_cpu = X86_CPU(cpu);
-CPUX86State *env = &x86_cpu->env;
+CPUX86State *env = cpu_env(cpu);
 uint64_t val;
 
 /* BUG, should take considering overlap.. */
@@ -196,10 +195,7 @@ static inline void macvm_set_rip(CPUState *cpu, uint64_t 
rip)
 
 static inline void vmx_clear_nmi_blocking(CPUState *cpu)
 {
-X86CPU *x86_cpu = X86_CPU(cpu);
-CPUX86State *env = &x86_cpu->env;
-
-env->hflags2 &= ~HF2_NMI_MASK;
+cpu_env(cpu)->hflags2 &= ~HF2_NMI_MASK;
 uint32_t gi = (uint32_t) rvmcs(cpu->accel->fd, 
VMCS_GUEST_INTERRUPTIBILITY);
 gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
 wvmcs(cpu->accel->fd, VMCS_GUEST_INTERRUPTIBILITY, gi);
@@ -207,10 +203,7 @@ static inline void vmx_clear_nmi_blocking(CPUState *cpu)
 
 static inline void vmx_set_nmi_blocking(CPUState *cpu)
 {
-X86CPU *x86_cpu = X86_CPU(cpu);
-CPUX86State *env = &x86_cpu->env;
-
-env->hflags2 |= HF2_NMI_MASK;
+cpu_env(cpu)->hflags2 |= HF2_NMI_MASK;
 uint32_t gi = (uint32_t)rvmcs(cpu->accel->fd, VMCS_GUEST_INTERRUPTIBILITY);
 gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING;
 wvmcs(cpu->accel->fd, VMCS_GUEST_INTERRUPTIBILITY, gi);
diff --git a/hw/i386/vmmouse.c b/hw/i386/vmmouse.c
index a8d014d09a..f292a14a15 100644
--- a/hw/i386/vmmouse.c
+++ b/hw/i386/vmmouse.c
@@ -74,8 +74,7 @@ struct VMMouseState {
 
 static void vmmouse_get_data(uint32_t *data)
 {
-X86CPU *cpu = X86_CPU(current_cpu);
-CPUX86State *env = &cpu->env;
+CPUX86State *env = cpu_env(current_cpu);
 
 data[0] = env->regs[R_EAX]; data[1] = env->regs[R_EBX];
 data[2] = env->regs[R_ECX]; data[3] = env->regs[R_EDX];
@@ -84,8 +83,7 @@ static void vmmouse_get_data(uint32_t *data)
 
 static void vmmouse_set_data(const uint32_t *data)
 {
-X86CPU *cpu = X86_CPU(current_cpu);
-CPUX86State *env = &cpu->env;
+CPUX86State *env = cpu_env(current_cpu);
 
 env->regs[R_EAX] = data[0]; env->regs[R_EBX] = data[1];
 env->regs[R_ECX] = data[2]; env->regs[R_EDX] = data[3];
diff --git a/hw/i386/xen/xen-hvm.c b/hw/i386/xen/xen-hvm.c
index f42621e674..61e5060117 100644
--- a/hw/i386/xen/xen-hvm.c
+++ b/hw/i386/xen/xen-hvm.c
@@ -487,8 +487,7 @@ static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t 
*req)
 
 static void regs_from_cpu(vmware_regs_t *vmport_regs)
 {
-X86CPU *cpu = X86_CPU(current_cpu);
-CPUX86State *env = &cpu->env;
+CPUX86State *env = cpu_env(current_cpu);
 
 vmport_regs->ebx = env->regs[R_EBX];
 vmport_regs->ecx = env->regs[R_ECX];
diff --git a/target/i386/arch_memory_mapping.c 
b/target/i386/arch_memory_mapping.c
index d1ff659128..c0604d5956 100644
--- a/target/i386/arch_memory_mapping.c
+++ b/target/i386/arch_memory_mapping.c
@@ -269,8 +269,7 @@ static void walk_pml5e(MemoryMappingList *list, 
AddressSpace *as,
 bool x86_cpu_get_memory_mapping(CPUState *cs, MemoryMappingList *list,
 Error **errp)
 {
-X86CPU *cpu = X86_CPU(cs);
-CPUX86State *env = &cpu->env;
+CPUX86State *env = cpu_env(cs);
 int32_t a20_mask;
 
 if (!cpu_paging_enabled(cs)) {
diff --git a/target/i386/cpu-dump.c b/target/i386/cpu-dump.c
index 40697064d9..5459d84abd 100644
--- a/target/i386/cpu-dump.c
+++ b/target/i386/cpu-dump.c
@@ -343,8 +343,7 @@ void x86_cpu_dump_local_apic_state(CPUState *cs, int flags)
 
 void x86_cpu_dump_state(CPUState *cs, FILE *f, int flags)
 {
-X86CPU *cpu = X86_CPU(cs);
-CPUX86State *env = &cpu->env;
+CPUX86State *env = cpu_env(cs);
 int eflags, i, nb;
 char cc_op_name[32];
 static const