diff mbox

[v2,1/2] monitor: Split mon_get_cpu fn to remove ENV_GET_CPU

Message ID a68f13aedf088eca8021956b980ced6599e26077.1432501932.git.crosthwaite.peter@gmail.com
State New
Headers show

Commit Message

Peter Crosthwaite May 24, 2015, 9:20 p.m. UTC
The monitor currently has one helper, mon_get_cpu() which will return
an env pointer. The target specific users of this API want an env, but
all the target agnostic users really just want the cpu pointer. These
users then need to use the target-specifically defined ENV_GET_CPU to
navigate back up to the CPU from the ENV. Split the API for the two
uses cases to remove all need for ENV_GET_CPU.

Reviewed-by: Richard Henderson <rth@twiddle.net>
Reviewed-by: Andreas Färber <afaerber@suse.de>
Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
---
Changed since v1:
s/mon_get_env/mon_get_cpu_env (Andreas review)
Avoid C99 declaration (RH review)
---
 monitor.c | 65 ++++++++++++++++++++++++++++-----------------------------------
 1 file changed, 29 insertions(+), 36 deletions(-)

Comments

Markus Armbruster June 12, 2015, 6:07 a.m. UTC | #1
Peter Crosthwaite <crosthwaitepeter@gmail.com> writes:

> The monitor currently has one helper, mon_get_cpu() which will return
> an env pointer. The target specific users of this API want an env, but
> all the target agnostic users really just want the cpu pointer. These
> users then need to use the target-specifically defined ENV_GET_CPU to
> navigate back up to the CPU from the ENV. Split the API for the two
> uses cases to remove all need for ENV_GET_CPU.
>
> Reviewed-by: Richard Henderson <rth@twiddle.net>
> Reviewed-by: Andreas Färber <afaerber@suse.de>
> Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
> ---
> Changed since v1:
> s/mon_get_env/mon_get_cpu_env (Andreas review)
> Avoid C99 declaration (RH review)

  CC    x86_64-softmmu/monitor.o
/work/armbru/qemu/monitor.c: In function ‘memory_search’:
/work/armbru/qemu/monitor.c:1222:9: warning: passing argument 1 of ‘x86_env_get_cpu’ from incompatible pointer type [enabled by default]
         } else if (cpu_memory_rw_debug(ENV_GET_CPU(mon_get_cpu()), addr,
         ^
In file included from /work/armbru/qemu/target-i386/cpu.h:982:0,
                 from /work/armbru/qemu/include/qemu-common.h:124,
                 from /work/armbru/qemu/include/hw/hw.h:5,
                 from /work/armbru/qemu/monitor.c:25:
/work/armbru/qemu/target-i386/cpu-qom.h:119:23: note: expected ‘struct CPUX86State *’ but argument is of type ‘struct CPUState *’
 static inline X86CPU *x86_env_get_cpu(CPUX86State *env)
                       ^
Markus Armbruster June 12, 2015, 6:14 a.m. UTC | #2
Markus Armbruster <armbru@redhat.com> writes:

> Peter Crosthwaite <crosthwaitepeter@gmail.com> writes:
>
>> The monitor currently has one helper, mon_get_cpu() which will return
>> an env pointer. The target specific users of this API want an env, but
>> all the target agnostic users really just want the cpu pointer. These
>> users then need to use the target-specifically defined ENV_GET_CPU to
>> navigate back up to the CPU from the ENV. Split the API for the two
>> uses cases to remove all need for ENV_GET_CPU.
>>
>> Reviewed-by: Richard Henderson <rth@twiddle.net>
>> Reviewed-by: Andreas Färber <afaerber@suse.de>
>> Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
>> ---
>> Changed since v1:
>> s/mon_get_env/mon_get_cpu_env (Andreas review)
>> Avoid C99 declaration (RH review)
>
>   CC    x86_64-softmmu/monitor.o
> /work/armbru/qemu/monitor.c: In function ‘memory_search’:
> /work/armbru/qemu/monitor.c:1222:9: warning: passing argument 1 of ‘x86_env_get_cpu’ from incompatible pointer type [enabled by default]
>          } else if (cpu_memory_rw_debug(ENV_GET_CPU(mon_get_cpu()), addr,
>          ^
> In file included from /work/armbru/qemu/target-i386/cpu.h:982:0,
>                  from /work/armbru/qemu/include/qemu-common.h:124,
>                  from /work/armbru/qemu/include/hw/hw.h:5,
>                  from /work/armbru/qemu/monitor.c:25:
> /work/armbru/qemu/target-i386/cpu-qom.h:119:23: note: expected ‘struct CPUX86State *’ but argument is of type ‘struct CPUState *’
>  static inline X86CPU *x86_env_get_cpu(CPUX86State *env)
>                        ^

Semantic conflict with
[RFC v6 0/2] monitor: add memory search commands s, sp

Since that series is marked RFC, I'm picking up yours, and will ask
Claudio to rebase.
Markus Armbruster June 16, 2015, 3:09 p.m. UTC | #3
Peter Crosthwaite <crosthwaitepeter@gmail.com> writes:

> The monitor currently has one helper, mon_get_cpu() which will return
> an env pointer. The target specific users of this API want an env, but
> all the target agnostic users really just want the cpu pointer. These
> users then need to use the target-specifically defined ENV_GET_CPU to
> navigate back up to the CPU from the ENV. Split the API for the two
> uses cases to remove all need for ENV_GET_CPU.
>
> Reviewed-by: Richard Henderson <rth@twiddle.net>
> Reviewed-by: Andreas Färber <afaerber@suse.de>
> Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
> ---
> Changed since v1:
> s/mon_get_env/mon_get_cpu_env (Andreas review)
> Avoid C99 declaration (RH review)
> ---
>  monitor.c | 65 ++++++++++++++++++++++++++++-----------------------------------
>  1 file changed, 29 insertions(+), 36 deletions(-)
>
[...]
> @@ -1208,16 +1203,14 @@ static void monitor_printc(Monitor *mon, int c)
>  static void memory_dump(Monitor *mon, int count, int format, int wsize,
>                          hwaddr addr, int is_physical)
>  {
> -    CPUArchState *env;
>      int l, line_size, i, max_digits, len;
>      uint8_t buf[16];
>      uint64_t v;
>  
>      if (format == 'i') {
> -        int flags;
> -        flags = 0;
> -        env = mon_get_cpu();
> +        int flags = 0;
>  #ifdef TARGET_I386
> +        CPUArchState *env = mon_get_cpu_env();
>          if (wsize == 2) {
>              flags = 1;
>          } else if (wsize == 4) {
> @@ -1238,10 +1231,11 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>          }
>  #endif
>  #ifdef TARGET_PPC
> +        CPUArchState *env = mon_get_cpu_env();
>          flags = msr_le << 16;
>          flags |= env->bfd_mach;
>  #endif
> -        monitor_disas(mon, env, addr, count, is_physical, flags);
> +        monitor_disas(mon, mon_get_cpu_env(), addr, count, is_physical, flags);
>          return;
>      }
>  
> @@ -1280,8 +1274,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>          if (is_physical) {
>              cpu_physical_memory_read(addr, buf, l);
>          } else {
> -            env = mon_get_cpu();
> -            if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
> +            if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
>                  monitor_printf(mon, " Cannot access memory\n");
>                  break;
>              }

You call mon_get_cpu_env() and mon_get_cpu() multiple times in this
function, and declare CPUArchState *env twice (which rang my shadowing
alarm bell; fortunately the two are under mutually exclusive #ifdef).
Why not simply do

    CPUState *cpu = mon_get_cpu();
    CPUArchState *env = mon_get_cpu_env();

right at the beginning and be done with it?

Not a big deal, I'm willing to take this patch through my tree as is.

[...]
Peter Crosthwaite June 17, 2015, 5:39 a.m. UTC | #4
On Tue, Jun 16, 2015 at 8:09 AM, Markus Armbruster <armbru@redhat.com> wrote:
> Peter Crosthwaite <crosthwaitepeter@gmail.com> writes:
>
>> The monitor currently has one helper, mon_get_cpu() which will return
>> an env pointer. The target specific users of this API want an env, but
>> all the target agnostic users really just want the cpu pointer. These
>> users then need to use the target-specifically defined ENV_GET_CPU to
>> navigate back up to the CPU from the ENV. Split the API for the two
>> uses cases to remove all need for ENV_GET_CPU.
>>
>> Reviewed-by: Richard Henderson <rth@twiddle.net>
>> Reviewed-by: Andreas Färber <afaerber@suse.de>
>> Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
>> ---
>> Changed since v1:
>> s/mon_get_env/mon_get_cpu_env (Andreas review)
>> Avoid C99 declaration (RH review)
>> ---
>>  monitor.c | 65 ++++++++++++++++++++++++++++-----------------------------------
>>  1 file changed, 29 insertions(+), 36 deletions(-)
>>
> [...]
>> @@ -1208,16 +1203,14 @@ static void monitor_printc(Monitor *mon, int c)
>>  static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>                          hwaddr addr, int is_physical)
>>  {
>> -    CPUArchState *env;
>>      int l, line_size, i, max_digits, len;
>>      uint8_t buf[16];
>>      uint64_t v;
>>
>>      if (format == 'i') {
>> -        int flags;
>> -        flags = 0;
>> -        env = mon_get_cpu();
>> +        int flags = 0;
>>  #ifdef TARGET_I386
>> +        CPUArchState *env = mon_get_cpu_env();
>>          if (wsize == 2) {
>>              flags = 1;
>>          } else if (wsize == 4) {
>> @@ -1238,10 +1231,11 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>          }
>>  #endif
>>  #ifdef TARGET_PPC
>> +        CPUArchState *env = mon_get_cpu_env();
>>          flags = msr_le << 16;
>>          flags |= env->bfd_mach;
>>  #endif
>> -        monitor_disas(mon, env, addr, count, is_physical, flags);
>> +        monitor_disas(mon, mon_get_cpu_env(), addr, count, is_physical, flags);
>>          return;
>>      }
>>
>> @@ -1280,8 +1274,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>          if (is_physical) {
>>              cpu_physical_memory_read(addr, buf, l);
>>          } else {
>> -            env = mon_get_cpu();
>> -            if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
>> +            if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
>>                  monitor_printf(mon, " Cannot access memory\n");
>>                  break;
>>              }
>
> You call mon_get_cpu_env() and mon_get_cpu() multiple times in this
> function, and declare CPUArchState *env twice (which rang my shadowing
> alarm bell; fortunately the two are under mutually exclusive #ifdef).
> Why not simply do
>
>     CPUState *cpu = mon_get_cpu();

This we can do easily and the choice of the existing code is pure
personal preference. I generally prefer inline calls to trivial
getters for a low number of call sites as I think code is slightly
easier to read when you don't have to do a local variable lookup on
where all the function args are coming from.

>     CPUArchState *env = mon_get_cpu_env();
>

So the multiple decl of env is now needed to avoid an unused variable
werror for non-x86-non-PPC builds when considering the change in P2.
Not strictly needed until P2, but doing it this way straight up
slightly minimises churn. The ENV_GET_CPU removal and the change in P2
remove the only two unconditional uses of env forcing this variable to
go local to its #ifdef usages. It also has the advantage that only
those two arches use the env at all. Envlessness is a good thing for
common code so prefer to leave the multi-defs in target specific code
with a plan to get rid of them one by one with X86/PPC specific
patches that operate solely on their respective #ifdef sections.

FWIW, the follow up to this series adds the infrastructure needed for
getting rid of these #ifdef sections (once I muster the courage to
patch X86 and PPC):

https://lists.gnu.org/archive/html/qemu-devel/2015-05/msg04742.html

> right at the beginning and be done with it?
>
> Not a big deal, I'm willing to take this patch through my tree as is.
>

Let me know if you need respin for the first change. I can turn it
around quickly, i'd really like to get this one through as its
blocking a lot of the multi-arch work!

Regards,
Peter

> [...]
>
Markus Armbruster June 17, 2015, 6:42 a.m. UTC | #5
Peter Crosthwaite <peter.crosthwaite@xilinx.com> writes:

> On Tue, Jun 16, 2015 at 8:09 AM, Markus Armbruster <armbru@redhat.com> wrote:
>> Peter Crosthwaite <crosthwaitepeter@gmail.com> writes:
>>
>>> The monitor currently has one helper, mon_get_cpu() which will return
>>> an env pointer. The target specific users of this API want an env, but
>>> all the target agnostic users really just want the cpu pointer. These
>>> users then need to use the target-specifically defined ENV_GET_CPU to
>>> navigate back up to the CPU from the ENV. Split the API for the two
>>> uses cases to remove all need for ENV_GET_CPU.
>>>
>>> Reviewed-by: Richard Henderson <rth@twiddle.net>
>>> Reviewed-by: Andreas Färber <afaerber@suse.de>
>>> Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
>>> ---
>>> Changed since v1:
>>> s/mon_get_env/mon_get_cpu_env (Andreas review)
>>> Avoid C99 declaration (RH review)
>>> ---
>>>  monitor.c | 65
>>> ++++++++++++++++++++++++++++-----------------------------------
>>>  1 file changed, 29 insertions(+), 36 deletions(-)
>>>
>> [...]
>>> @@ -1208,16 +1203,14 @@ static void monitor_printc(Monitor *mon, int c)
>>>  static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>>                          hwaddr addr, int is_physical)
>>>  {
>>> -    CPUArchState *env;
>>>      int l, line_size, i, max_digits, len;
>>>      uint8_t buf[16];
>>>      uint64_t v;
>>>
>>>      if (format == 'i') {
>>> -        int flags;
>>> -        flags = 0;
>>> -        env = mon_get_cpu();
>>> +        int flags = 0;
>>>  #ifdef TARGET_I386
>>> +        CPUArchState *env = mon_get_cpu_env();
>>>          if (wsize == 2) {
>>>              flags = 1;
>>>          } else if (wsize == 4) {
>>> @@ -1238,10 +1231,11 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>>          }
>>>  #endif
>>>  #ifdef TARGET_PPC
>>> +        CPUArchState *env = mon_get_cpu_env();
>>>          flags = msr_le << 16;
>>>          flags |= env->bfd_mach;
>>>  #endif
>>> -        monitor_disas(mon, env, addr, count, is_physical, flags);
>>> +        monitor_disas(mon, mon_get_cpu_env(), addr, count, is_physical, flags);
>>>          return;
>>>      }
>>>
>>> @@ -1280,8 +1274,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>>          if (is_physical) {
>>>              cpu_physical_memory_read(addr, buf, l);
>>>          } else {
>>> -            env = mon_get_cpu();
>>> -            if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
>>> +            if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
>>>                  monitor_printf(mon, " Cannot access memory\n");
>>>                  break;
>>>              }
>>
>> You call mon_get_cpu_env() and mon_get_cpu() multiple times in this
>> function, and declare CPUArchState *env twice (which rang my shadowing
>> alarm bell; fortunately the two are under mutually exclusive #ifdef).
>> Why not simply do
>>
>>     CPUState *cpu = mon_get_cpu();
>
> This we can do easily and the choice of the existing code is pure
> personal preference. I generally prefer inline calls to trivial
> getters for a low number of call sites as I think code is slightly
> easier to read when you don't have to do a local variable lookup on
> where all the function args are coming from.

Point taken.

The getter isn't quite trivial, though: cpu_synchronize_state().

>>     CPUArchState *env = mon_get_cpu_env();
>>
>
> So the multiple decl of env is now needed to avoid an unused variable
> werror for non-x86-non-PPC builds when considering the change in P2.
> Not strictly needed until P2, but doing it this way straight up
> slightly minimises churn. The ENV_GET_CPU removal and the change in P2
> remove the only two unconditional uses of env forcing this variable to
> go local to its #ifdef usages. It also has the advantage that only
> those two arches use the env at all. Envlessness is a good thing for
> common code so prefer to leave the multi-defs in target specific code
> with a plan to get rid of them one by one with X86/PPC specific
> patches that operate solely on their respective #ifdef sections.
>
> FWIW, the follow up to this series adds the infrastructure needed for
> getting rid of these #ifdef sections (once I muster the courage to
> patch X86 and PPC):
>
> https://lists.gnu.org/archive/html/qemu-devel/2015-05/msg04742.html
>
>> right at the beginning and be done with it?
>>
>> Not a big deal, I'm willing to take this patch through my tree as is.
>>
>
> Let me know if you need respin for the first change. I can turn it

Your choice.  As I said, I'm willing to take it as is.

> around quickly, i'd really like to get this one through as its
> blocking a lot of the multi-arch work!

Aiming for a pull request this week.
Peter Crosthwaite June 17, 2015, 7:19 a.m. UTC | #6
On Tue, Jun 16, 2015 at 11:42 PM, Markus Armbruster <armbru@redhat.com> wrote:
> Peter Crosthwaite <peter.crosthwaite@xilinx.com> writes:
>
>> On Tue, Jun 16, 2015 at 8:09 AM, Markus Armbruster <armbru@redhat.com> wrote:
>>> Peter Crosthwaite <crosthwaitepeter@gmail.com> writes:
>>>
>>>> The monitor currently has one helper, mon_get_cpu() which will return
>>>> an env pointer. The target specific users of this API want an env, but
>>>> all the target agnostic users really just want the cpu pointer. These
>>>> users then need to use the target-specifically defined ENV_GET_CPU to
>>>> navigate back up to the CPU from the ENV. Split the API for the two
>>>> uses cases to remove all need for ENV_GET_CPU.
>>>>
>>>> Reviewed-by: Richard Henderson <rth@twiddle.net>
>>>> Reviewed-by: Andreas Färber <afaerber@suse.de>
>>>> Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
>>>> ---
>>>> Changed since v1:
>>>> s/mon_get_env/mon_get_cpu_env (Andreas review)
>>>> Avoid C99 declaration (RH review)
>>>> ---
>>>>  monitor.c | 65
>>>> ++++++++++++++++++++++++++++-----------------------------------
>>>>  1 file changed, 29 insertions(+), 36 deletions(-)
>>>>
>>> [...]
>>>> @@ -1208,16 +1203,14 @@ static void monitor_printc(Monitor *mon, int c)
>>>>  static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>>>                          hwaddr addr, int is_physical)
>>>>  {
>>>> -    CPUArchState *env;
>>>>      int l, line_size, i, max_digits, len;
>>>>      uint8_t buf[16];
>>>>      uint64_t v;
>>>>
>>>>      if (format == 'i') {
>>>> -        int flags;
>>>> -        flags = 0;
>>>> -        env = mon_get_cpu();
>>>> +        int flags = 0;
>>>>  #ifdef TARGET_I386
>>>> +        CPUArchState *env = mon_get_cpu_env();
>>>>          if (wsize == 2) {
>>>>              flags = 1;
>>>>          } else if (wsize == 4) {
>>>> @@ -1238,10 +1231,11 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>>>          }
>>>>  #endif
>>>>  #ifdef TARGET_PPC
>>>> +        CPUArchState *env = mon_get_cpu_env();
>>>>          flags = msr_le << 16;
>>>>          flags |= env->bfd_mach;
>>>>  #endif
>>>> -        monitor_disas(mon, env, addr, count, is_physical, flags);
>>>> +        monitor_disas(mon, mon_get_cpu_env(), addr, count, is_physical, flags);
>>>>          return;
>>>>      }
>>>>
>>>> @@ -1280,8 +1274,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
>>>>          if (is_physical) {
>>>>              cpu_physical_memory_read(addr, buf, l);
>>>>          } else {
>>>> -            env = mon_get_cpu();
>>>> -            if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
>>>> +            if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
>>>>                  monitor_printf(mon, " Cannot access memory\n");
>>>>                  break;
>>>>              }
>>>
>>> You call mon_get_cpu_env() and mon_get_cpu() multiple times in this
>>> function, and declare CPUArchState *env twice (which rang my shadowing
>>> alarm bell; fortunately the two are under mutually exclusive #ifdef).
>>> Why not simply do
>>>
>>>     CPUState *cpu = mon_get_cpu();
>>
>> This we can do easily and the choice of the existing code is pure
>> personal preference. I generally prefer inline calls to trivial
>> getters for a low number of call sites as I think code is slightly
>> easier to read when you don't have to do a local variable lookup on
>> where all the function args are coming from.
>
> Point taken.
>
> The getter isn't quite trivial, though: cpu_synchronize_state().
>
>>>     CPUArchState *env = mon_get_cpu_env();
>>>
>>
>> So the multiple decl of env is now needed to avoid an unused variable
>> werror for non-x86-non-PPC builds when considering the change in P2.
>> Not strictly needed until P2, but doing it this way straight up
>> slightly minimises churn. The ENV_GET_CPU removal and the change in P2
>> remove the only two unconditional uses of env forcing this variable to
>> go local to its #ifdef usages. It also has the advantage that only
>> those two arches use the env at all. Envlessness is a good thing for
>> common code so prefer to leave the multi-defs in target specific code
>> with a plan to get rid of them one by one with X86/PPC specific
>> patches that operate solely on their respective #ifdef sections.
>>
>> FWIW, the follow up to this series adds the infrastructure needed for
>> getting rid of these #ifdef sections (once I muster the courage to
>> patch X86 and PPC):
>>
>> https://lists.gnu.org/archive/html/qemu-devel/2015-05/msg04742.html
>>
>>> right at the beginning and be done with it?
>>>
>>> Not a big deal, I'm willing to take this patch through my tree as is.
>>>
>>
>> Let me know if you need respin for the first change. I can turn it
>
> Your choice.  As I said, I'm willing to take it as is.
>

Thanks, as it is queued I will leave it and I have made a note in my
todos to follow this up.

Regards,
Peter

>> around quickly, i'd really like to get this one through as its
>> blocking a lot of the multi-arch work!
>
> Aiming for a pull request this week.
>
diff mbox

Patch

diff --git a/monitor.c b/monitor.c
index b2561e1..365c0e4 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1010,28 +1010,28 @@  int monitor_set_cpu(int cpu_index)
     return 0;
 }
 
-static CPUArchState *mon_get_cpu(void)
+static CPUState *mon_get_cpu(void)
 {
     if (!cur_mon->mon_cpu) {
         monitor_set_cpu(0);
     }
     cpu_synchronize_state(cur_mon->mon_cpu);
-    return cur_mon->mon_cpu->env_ptr;
+    return cur_mon->mon_cpu;
+}
+
+static CPUArchState *mon_get_cpu_env(void)
+{
+    return mon_get_cpu()->env_ptr;
 }
 
 int monitor_get_cpu_index(void)
 {
-    CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
-    return cpu->cpu_index;
+    return mon_get_cpu()->cpu_index;
 }
 
 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
 {
-    CPUState *cpu;
-    CPUArchState *env;
-    env = mon_get_cpu();
-    cpu = ENV_GET_CPU(env);
-    cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
+    cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
 }
 
 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
@@ -1064,12 +1064,7 @@  static void hmp_info_history(Monitor *mon, const QDict *qdict)
 
 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
 {
-    CPUState *cpu;
-    CPUArchState *env;
-
-    env = mon_get_cpu();
-    cpu = ENV_GET_CPU(env);
-    cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
+    cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
 }
 
 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
@@ -1208,16 +1203,14 @@  static void monitor_printc(Monitor *mon, int c)
 static void memory_dump(Monitor *mon, int count, int format, int wsize,
                         hwaddr addr, int is_physical)
 {
-    CPUArchState *env;
     int l, line_size, i, max_digits, len;
     uint8_t buf[16];
     uint64_t v;
 
     if (format == 'i') {
-        int flags;
-        flags = 0;
-        env = mon_get_cpu();
+        int flags = 0;
 #ifdef TARGET_I386
+        CPUArchState *env = mon_get_cpu_env();
         if (wsize == 2) {
             flags = 1;
         } else if (wsize == 4) {
@@ -1238,10 +1231,11 @@  static void memory_dump(Monitor *mon, int count, int format, int wsize,
         }
 #endif
 #ifdef TARGET_PPC
+        CPUArchState *env = mon_get_cpu_env();
         flags = msr_le << 16;
         flags |= env->bfd_mach;
 #endif
-        monitor_disas(mon, env, addr, count, is_physical, flags);
+        monitor_disas(mon, mon_get_cpu_env(), addr, count, is_physical, flags);
         return;
     }
 
@@ -1280,8 +1274,7 @@  static void memory_dump(Monitor *mon, int count, int format, int wsize,
         if (is_physical) {
             cpu_physical_memory_read(addr, buf, l);
         } else {
-            env = mon_get_cpu();
-            if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
+            if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
                 monitor_printf(mon, " Cannot access memory\n");
                 break;
             }
@@ -1660,7 +1653,7 @@  static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
 {
     CPUArchState *env;
 
-    env = mon_get_cpu();
+    env = mon_get_cpu_env();
 
     if (!(env->cr[0] & CR0_PG_MASK)) {
         monitor_printf(mon, "PG disabled\n");
@@ -1883,7 +1876,7 @@  static void hmp_info_mem(Monitor *mon, const QDict *qdict)
 {
     CPUArchState *env;
 
-    env = mon_get_cpu();
+    env = mon_get_cpu_env();
 
     if (!(env->cr[0] & CR0_PG_MASK)) {
         monitor_printf(mon, "PG disabled\n");
@@ -1920,7 +1913,7 @@  static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
 
 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     int i;
 
     monitor_printf (mon, "ITLB:\n");
@@ -1936,7 +1929,7 @@  static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
 {
-    CPUArchState *env1 = mon_get_cpu();
+    CPUArchState *env1 = mon_get_cpu_env();
 
     dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
 }
@@ -2969,7 +2962,7 @@  typedef struct MonitorDef {
 #if defined(TARGET_I386)
 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     return env->eip + env->segs[R_CS].base;
 }
 #endif
@@ -2977,7 +2970,7 @@  static target_long monitor_get_pc (const struct MonitorDef *md, int val)
 #if defined(TARGET_PPC)
 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     unsigned int u;
     int i;
 
@@ -2990,31 +2983,31 @@  static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
 
 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     return env->msr;
 }
 
 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     return env->xer;
 }
 
 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     return cpu_ppc_load_decr(env);
 }
 
 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     return cpu_ppc_load_tbu(env);
 }
 
 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     return cpu_ppc_load_tbl(env);
 }
 #endif
@@ -3023,7 +3016,7 @@  static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
 #ifndef TARGET_SPARC64
 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
 
     return cpu_get_psr(env);
 }
@@ -3031,7 +3024,7 @@  static target_long monitor_get_psr (const struct MonitorDef *md, int val)
 
 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
 {
-    CPUArchState *env = mon_get_cpu();
+    CPUArchState *env = mon_get_cpu_env();
     return env->regwptr[val];
 }
 #endif
@@ -3367,7 +3360,7 @@  static int get_monitor_def(target_long *pval, const char *name)
             if (md->get_value) {
                 *pval = md->get_value(md, md->offset);
             } else {
-                CPUArchState *env = mon_get_cpu();
+                CPUArchState *env = mon_get_cpu_env();
                 ptr = (uint8_t *)env + md->offset;
                 switch(md->type) {
                 case MD_I32: