diff mbox series

[20/24] machine: drop MachineState::cpu_model

Message ID 1516203816-19374-21-git-send-email-imammedo@redhat.com
State New
Headers show
Series generalize parsing of cpu_model (part 4) | expand

Commit Message

Igor Mammedov Jan. 17, 2018, 3:43 p.m. UTC
The last user of it was machine type 'none', which used field
to create CPU id user requested it on CLI with -cpu option.

We could compare pointers of MachineState::cpu_type and
MachineClass::default_cpu_type to check for the same condition,
and drop cpu_model concept completly from machine/boards code
So that no one would try to reuse obsolete field and only
place to deal with cpu model would be vl.c and
foo_cpu_class_by_name() callbacks.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
CC: Eduardo Habkost <ehabkost@redhat.com>
CC: Marcel Apfelbaum <marcel@redhat.com>
CC: Paolo Bonzini <pbonzini@redhat.com>
---
 include/hw/boards.h    |  1 -
 hw/core/null-machine.c | 10 +++++++---
 vl.c                   |  8 +++++++-
 3 files changed, 14 insertions(+), 5 deletions(-)

Comments

Eduardo Habkost Jan. 18, 2018, 1:48 a.m. UTC | #1
On Wed, Jan 17, 2018 at 04:43:32PM +0100, Igor Mammedov wrote:
> The last user of it was machine type 'none', which used field
> to create CPU id user requested it on CLI with -cpu option.
> 
> We could compare pointers of MachineState::cpu_type and
> MachineClass::default_cpu_type to check for the same condition,
> and drop cpu_model concept completly from machine/boards code
> So that no one would try to reuse obsolete field and only
> place to deal with cpu model would be vl.c and
> foo_cpu_class_by_name() callbacks.
> 
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
> CC: Eduardo Habkost <ehabkost@redhat.com>
> CC: Marcel Apfelbaum <marcel@redhat.com>
> CC: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  include/hw/boards.h    |  1 -
>  hw/core/null-machine.c | 10 +++++++---
>  vl.c                   |  8 +++++++-
>  3 files changed, 14 insertions(+), 5 deletions(-)
> 
> diff --git a/include/hw/boards.h b/include/hw/boards.h
> index 156b16f..decd0ec 100644
> --- a/include/hw/boards.h
> +++ b/include/hw/boards.h
> @@ -246,7 +246,6 @@ struct MachineState {
>      char *kernel_filename;
>      char *kernel_cmdline;
>      char *initrd_filename;
> -    const char *cpu_model;
>      const char *cpu_type;
>      AccelState *accelerator;
>      CPUArchIdList *possible_cpus;
> diff --git a/hw/core/null-machine.c b/hw/core/null-machine.c
> index 864832d..c2e466c 100644
> --- a/hw/core/null-machine.c
> +++ b/hw/core/null-machine.c
> @@ -23,10 +23,13 @@
>  static void machine_none_init(MachineState *mch)
>  {
>      CPUState *cpu = NULL;
> +    MachineClass *mc = MACHINE_GET_CLASS(mch);
>  
> -    /* Initialize CPU (if a model has been specified) */
> -    if (mch->cpu_model) {
> -        cpu = cpu_init(mch->cpu_model);
> +    /* Initialize CPU if cpu_type pointer is user provided
> +     * (i.e. != to pointer tot static default cpu type string)
> +     */
> +    if (mch->cpu_type != mc->default_cpu_type) {
> +        cpu = cpu_create(mch->cpu_type);

This is a big assumption about the code that sets mch->cpu_type.
A simple g_strdup(machine_class->default_cpu_type) would break
this silently (as it won't trigger the assert() below).


>          if (!cpu) {
>              error_report("Unable to initialize CPU");
>              exit(1);
> @@ -54,6 +57,7 @@ static void machine_none_machine_init(MachineClass *mc)
>      mc->init = machine_none_init;
>      mc->max_cpus = 1;
>      mc->default_ram_size = 0;
> +    mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;

Why do you need this?  Isn't it simpler to just leave
default_cpu_type=NULL here?


>  }
>  
>  DEFINE_MACHINE("none", machine_none_machine_init)
> diff --git a/vl.c b/vl.c
> index 2586f25..8aa0131 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -4609,7 +4609,6 @@ int main(int argc, char **argv, char **envp)
>      current_machine->maxram_size = maxram_size;
>      current_machine->ram_slots = ram_slots;
>      current_machine->boot_order = boot_order;
> -    current_machine->cpu_model = cpu_model;
>  
>      parse_numa_opts(current_machine);
>  
> @@ -4619,6 +4618,13 @@ int main(int argc, char **argv, char **envp)
>          if (cpu_model) {
>              current_machine->cpu_type =
>                  cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
> +
> +            /* machine 'none' depends on default cpu type pointer not being
> +             * equal to resolved type name pointer to fugure out if type was
> +             * user provided, make sure that if it becomes not true in future
> +             * it won't beark silently */
> +            g_assert(
> +                current_machine->cpu_type != machine_class->default_cpu_type);
>          }
>      }
>  
> -- 
> 2.7.4
>
Igor Mammedov Jan. 18, 2018, 10:10 a.m. UTC | #2
On Wed, 17 Jan 2018 23:48:46 -0200
Eduardo Habkost <ehabkost@redhat.com> wrote:

> On Wed, Jan 17, 2018 at 04:43:32PM +0100, Igor Mammedov wrote:
> > The last user of it was machine type 'none', which used field
> > to create CPU id user requested it on CLI with -cpu option.
> > 
> > We could compare pointers of MachineState::cpu_type and
> > MachineClass::default_cpu_type to check for the same condition,
> > and drop cpu_model concept completly from machine/boards code
> > So that no one would try to reuse obsolete field and only
> > place to deal with cpu model would be vl.c and
> > foo_cpu_class_by_name() callbacks.
> > 
> > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > ---
> > CC: Eduardo Habkost <ehabkost@redhat.com>
> > CC: Marcel Apfelbaum <marcel@redhat.com>
> > CC: Paolo Bonzini <pbonzini@redhat.com>
> > ---
> >  include/hw/boards.h    |  1 -
> >  hw/core/null-machine.c | 10 +++++++---
> >  vl.c                   |  8 +++++++-
> >  3 files changed, 14 insertions(+), 5 deletions(-)
> > 
> > diff --git a/include/hw/boards.h b/include/hw/boards.h
> > index 156b16f..decd0ec 100644
> > --- a/include/hw/boards.h
> > +++ b/include/hw/boards.h
> > @@ -246,7 +246,6 @@ struct MachineState {
> >      char *kernel_filename;
> >      char *kernel_cmdline;
> >      char *initrd_filename;
> > -    const char *cpu_model;
> >      const char *cpu_type;
> >      AccelState *accelerator;
> >      CPUArchIdList *possible_cpus;
> > diff --git a/hw/core/null-machine.c b/hw/core/null-machine.c
> > index 864832d..c2e466c 100644
> > --- a/hw/core/null-machine.c
> > +++ b/hw/core/null-machine.c
> > @@ -23,10 +23,13 @@
> >  static void machine_none_init(MachineState *mch)
> >  {
> >      CPUState *cpu = NULL;
> > +    MachineClass *mc = MACHINE_GET_CLASS(mch);
> >  
> > -    /* Initialize CPU (if a model has been specified) */
> > -    if (mch->cpu_model) {
> > -        cpu = cpu_init(mch->cpu_model);
> > +    /* Initialize CPU if cpu_type pointer is user provided
> > +     * (i.e. != to pointer tot static default cpu type string)
> > +     */
> > +    if (mch->cpu_type != mc->default_cpu_type) {
> > +        cpu = cpu_create(mch->cpu_type);  
> 
> This is a big assumption about the code that sets mch->cpu_type.
> A simple g_strdup(machine_class->default_cpu_type) would break
> this silently (as it won't trigger the assert() below).
Yes, it's a bit of a hack to figure out is user has requested
cpu type explicitly. But so far there isn't need to do
   g_strdup(machine_class->default_cpu_type)
when copying default in vl.c and guarding against it
looks like overkill currently.

Cleaner way would be to make cpu_type property, add new property
API to set/check flags and use that here, there are other places
that would benefit from such API as well.
But it looks beyond scope of this series, so I used simple
hackish way to make it work with current code.

I can amend comment here:
    /* Initialize CPU if cpu_type pointer is user provided
     * (i.e. != to pointer to static default cpu type string)
     * MachineClass::default_cpu_type must be assigned to
     * MachineState::cpu_type directly for this to work.
     * TODO:
     *   - make cpu_type a property
     *   - add API to add/check user_set flag to property
     *   - use new API to check if property was user set
     */

> 
> 
> >          if (!cpu) {
> >              error_report("Unable to initialize CPU");
> >              exit(1);
> > @@ -54,6 +57,7 @@ static void machine_none_machine_init(MachineClass *mc)
> >      mc->init = machine_none_init;
> >      mc->max_cpus = 1;
> >      mc->default_ram_size = 0;
> > +    mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;  
> 
> Why do you need this?  Isn't it simpler to just leave
> default_cpu_type=NULL here?
vl.c "-cpu" parsing depends on default_cpu_type being set
 ...
 if (machine_class->default_cpu_type) {
   ...
 }
when we get rid of requirement for proxy type in
 cpu_parse_cpu_model()
we can drop this ugliness in null-machine.

I'm doing it dirty way to prevent cpu_model resurgence
in boards code as it happened with nios2, even though
I've tried to monitor list for such patches.
 
> >  }
> >  
> >  DEFINE_MACHINE("none", machine_none_machine_init)
> > diff --git a/vl.c b/vl.c
> > index 2586f25..8aa0131 100644
> > --- a/vl.c
> > +++ b/vl.c
> > @@ -4609,7 +4609,6 @@ int main(int argc, char **argv, char **envp)
> >      current_machine->maxram_size = maxram_size;
> >      current_machine->ram_slots = ram_slots;
> >      current_machine->boot_order = boot_order;
> > -    current_machine->cpu_model = cpu_model;
> >  
> >      parse_numa_opts(current_machine);
> >  
> > @@ -4619,6 +4618,13 @@ int main(int argc, char **argv, char **envp)
> >          if (cpu_model) {
> >              current_machine->cpu_type =
> >                  cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
> > +
> > +            /* machine 'none' depends on default cpu type pointer not being
> > +             * equal to resolved type name pointer to fugure out if type was
> > +             * user provided, make sure that if it becomes not true in future
> > +             * it won't beark silently */
> > +            g_assert(
> > +                current_machine->cpu_type != machine_class->default_cpu_type);
> >          }
> >      }
> >  
> > -- 
> > 2.7.4
> >   
>
Eduardo Habkost Jan. 18, 2018, 7:18 p.m. UTC | #3
On Thu, Jan 18, 2018 at 11:10:35AM +0100, Igor Mammedov wrote:
> On Wed, 17 Jan 2018 23:48:46 -0200
> Eduardo Habkost <ehabkost@redhat.com> wrote:
> 
> > On Wed, Jan 17, 2018 at 04:43:32PM +0100, Igor Mammedov wrote:
> > > The last user of it was machine type 'none', which used field
> > > to create CPU id user requested it on CLI with -cpu option.
> > > 
> > > We could compare pointers of MachineState::cpu_type and
> > > MachineClass::default_cpu_type to check for the same condition,
> > > and drop cpu_model concept completly from machine/boards code
> > > So that no one would try to reuse obsolete field and only
> > > place to deal with cpu model would be vl.c and
> > > foo_cpu_class_by_name() callbacks.
> > > 
> > > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > > ---
> > > CC: Eduardo Habkost <ehabkost@redhat.com>
> > > CC: Marcel Apfelbaum <marcel@redhat.com>
> > > CC: Paolo Bonzini <pbonzini@redhat.com>
> > > ---
> > >  include/hw/boards.h    |  1 -
> > >  hw/core/null-machine.c | 10 +++++++---
> > >  vl.c                   |  8 +++++++-
> > >  3 files changed, 14 insertions(+), 5 deletions(-)
> > > 
> > > diff --git a/include/hw/boards.h b/include/hw/boards.h
> > > index 156b16f..decd0ec 100644
> > > --- a/include/hw/boards.h
> > > +++ b/include/hw/boards.h
> > > @@ -246,7 +246,6 @@ struct MachineState {
> > >      char *kernel_filename;
> > >      char *kernel_cmdline;
> > >      char *initrd_filename;
> > > -    const char *cpu_model;
> > >      const char *cpu_type;
> > >      AccelState *accelerator;
> > >      CPUArchIdList *possible_cpus;
> > > diff --git a/hw/core/null-machine.c b/hw/core/null-machine.c
> > > index 864832d..c2e466c 100644
> > > --- a/hw/core/null-machine.c
> > > +++ b/hw/core/null-machine.c
> > > @@ -23,10 +23,13 @@
> > >  static void machine_none_init(MachineState *mch)
> > >  {
> > >      CPUState *cpu = NULL;
> > > +    MachineClass *mc = MACHINE_GET_CLASS(mch);
> > >  
> > > -    /* Initialize CPU (if a model has been specified) */
> > > -    if (mch->cpu_model) {
> > > -        cpu = cpu_init(mch->cpu_model);
> > > +    /* Initialize CPU if cpu_type pointer is user provided
> > > +     * (i.e. != to pointer tot static default cpu type string)
> > > +     */
> > > +    if (mch->cpu_type != mc->default_cpu_type) {
> > > +        cpu = cpu_create(mch->cpu_type);  
> > 
> > This is a big assumption about the code that sets mch->cpu_type.
> > A simple g_strdup(machine_class->default_cpu_type) would break
> > this silently (as it won't trigger the assert() below).
> Yes, it's a bit of a hack to figure out is user has requested
> cpu type explicitly. But so far there isn't need to do
>    g_strdup(machine_class->default_cpu_type)
> when copying default in vl.c and guarding against it
> looks like overkill currently.
> 
> Cleaner way would be to make cpu_type property, add new property
> API to set/check flags and use that here, there are other places
> that would benefit from such API as well.
> But it looks beyond scope of this series, so I used simple
> hackish way to make it work with current code.
> 
> I can amend comment here:
>     /* Initialize CPU if cpu_type pointer is user provided
>      * (i.e. != to pointer to static default cpu type string)
>      * MachineClass::default_cpu_type must be assigned to
>      * MachineState::cpu_type directly for this to work.
>      * TODO:
>      *   - make cpu_type a property
>      *   - add API to add/check user_set flag to property
>      *   - use new API to check if property was user set
>      */
> 
> > 
> > 
> > >          if (!cpu) {
> > >              error_report("Unable to initialize CPU");
> > >              exit(1);
> > > @@ -54,6 +57,7 @@ static void machine_none_machine_init(MachineClass *mc)
> > >      mc->init = machine_none_init;
> > >      mc->max_cpus = 1;
> > >      mc->default_ram_size = 0;
> > > +    mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;  
> > 
> > Why do you need this?  Isn't it simpler to just leave
> > default_cpu_type=NULL here?
> vl.c "-cpu" parsing depends on default_cpu_type being set
>  ...
>  if (machine_class->default_cpu_type) {
>    ...
>  }

Right, this makes sense now.

It looks like default_cpu_type is being overloaded for two
different roles: 1) specifying the default CPU type; 2) finding
the arch-specific class to be used to parse -cpu.

In the case of null-machine, these two roles conflict with each
other.  I believe we can find other solutions instead of this
hack that involves lying on MachineClass::default_cpu_type (and
then having to work around the lie on machine_none_init()).

I see multiple options: adding a new MachineClass field for that
(e.g.  resolving_cpu_type, which defaults to default_cpu_type if
NULL); moving the CPU parsing code to arch_init.c (so it could
use CPU_RESOLVING_TYPE or something similar); adding a optional
MachineClass::parse_cpu_model hook.  We could even try to get rid
of CPUClass::parse_features completely


> when we get rid of requirement for proxy type in
>  cpu_parse_cpu_model()
> we can drop this ugliness in null-machine.

I'd prefer to not introduce this ugliness in the first place.

> 
> I'm doing it dirty way to prevent cpu_model resurgence
> in boards code as it happened with nios2, even though
> I've tried to monitor list for such patches.

To be honest, I don't think the harm in having new code using
MachineState::cpu_model is so big to justify this hack.


>  
> > >  }
> > >  
> > >  DEFINE_MACHINE("none", machine_none_machine_init)
> > > diff --git a/vl.c b/vl.c
> > > index 2586f25..8aa0131 100644
> > > --- a/vl.c
> > > +++ b/vl.c
> > > @@ -4609,7 +4609,6 @@ int main(int argc, char **argv, char **envp)
> > >      current_machine->maxram_size = maxram_size;
> > >      current_machine->ram_slots = ram_slots;
> > >      current_machine->boot_order = boot_order;
> > > -    current_machine->cpu_model = cpu_model;
> > >  
> > >      parse_numa_opts(current_machine);
> > >  
> > > @@ -4619,6 +4618,13 @@ int main(int argc, char **argv, char **envp)
> > >          if (cpu_model) {
> > >              current_machine->cpu_type =
> > >                  cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
> > > +
> > > +            /* machine 'none' depends on default cpu type pointer not being
> > > +             * equal to resolved type name pointer to fugure out if type was
> > > +             * user provided, make sure that if it becomes not true in future
> > > +             * it won't beark silently */
> > > +            g_assert(
> > > +                current_machine->cpu_type != machine_class->default_cpu_type);
> > >          }
> > >      }
> > >  
> > > -- 
> > > 2.7.4
> > >   
> > 
>
Igor Mammedov Jan. 19, 2018, 10:14 a.m. UTC | #4
On Thu, 18 Jan 2018 17:18:09 -0200
Eduardo Habkost <ehabkost@redhat.com> wrote:

> On Thu, Jan 18, 2018 at 11:10:35AM +0100, Igor Mammedov wrote:
> > On Wed, 17 Jan 2018 23:48:46 -0200
> > Eduardo Habkost <ehabkost@redhat.com> wrote:
> >   
> > > On Wed, Jan 17, 2018 at 04:43:32PM +0100, Igor Mammedov wrote:  
> > > > The last user of it was machine type 'none', which used field
> > > > to create CPU id user requested it on CLI with -cpu option.
[...]

> It looks like default_cpu_type is being overloaded for two
> different roles: 1) specifying the default CPU type; 2) finding
> the arch-specific class to be used to parse -cpu.
> 
> In the case of null-machine, these two roles conflict with each
> other.  I believe we can find other solutions instead of this
> hack that involves lying on MachineClass::default_cpu_type (and
> then having to work around the lie on machine_none_init()).
> 
> I see multiple options: adding a new MachineClass field for that
> (e.g.  resolving_cpu_type, which defaults to default_cpu_type if
> NULL); moving the CPU parsing code to arch_init.c (so it could
> use CPU_RESOLVING_TYPE or something similar); adding a optional
> MachineClass::parse_cpu_model hook.  We could even try to get rid
> of CPUClass::parse_features completely
Adding hooks just for the sake on null-machine seems to be overkill,
I'd go for arch_init.c but it won't work for linux-user, how about
exec.c as following:

diff --git a/include/qom/cpu.h b/include/qom/cpu.h
index 93bd546..0185589 100644
--- a/include/qom/cpu.h
+++ b/include/qom/cpu.h
@@ -661,8 +661,7 @@ ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
[...]

diff --git a/exec.c b/exec.c
index d28fc0c..4543f06 100644
--- a/exec.c
+++ b/exec.c
@@ -817,6 +817,29 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
 #endif
 }
 
+const char *parse_cpu_model(const char *cpu_model)
+{
+    ObjectClass *oc;
+    CPUClass *cc;
+    gchar **model_pieces;
+    const char *cpu_type;
+
+    model_pieces = g_strsplit(cpu_model, ",", 2);
+
+    oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
+    if (oc == NULL) {
+        error_report("unable to find CPU model '%s'", model_pieces[0]);
+        g_strfreev(model_pieces);
+        exit(EXIT_FAILURE);
+    }
+
+    cpu_type = object_class_get_name(oc);
+    cc = CPU_CLASS(oc);
+    cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
+    g_strfreev(model_pieces);
+    return cpu_type;
+}
+
 #if defined(CONFIG_USER_ONLY)
 static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
 {
diff --git a/hw/core/null-machine.c b/hw/core/null-machine.c
index 864832d..cde4d3e 100644
--- a/hw/core/null-machine.c
+++ b/hw/core/null-machine.c
@@ -24,9 +24,9 @@ static void machine_none_init(MachineState *mch)
 {
     CPUState *cpu = NULL;
 
-    /* Initialize CPU (if a model has been specified) */
-    if (mch->cpu_model) {
-        cpu = cpu_init(mch->cpu_model);
+    /* Initialize CPU (if user asked for it) */
+    if (mch->cpu_type) {
+        cpu = cpu_create(mch->cpu_type);
         if (!cpu) {
             error_report("Unable to initialize CPU");
             exit(1);
diff --git a/linux-user/main.c b/linux-user/main.c
index a35477e..0afb3f4 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -4357,10 +4358,13 @@ int main(int argc, char **argv, char **envp)
         cpu_model = "any";
 #endif
     }
+    cpu_type = parse_cpu_model(cpu_model);
+
     tcg_exec_init(0);
     /* NOTE: we need to init the CPU at this stage to get
        qemu_host_page_size */
-    cpu = cpu_init(cpu_model);
+
+    cpu = cpu_create(cpu_type);
     env = cpu->env_ptr;
     cpu_reset(cpu);
 
diff --git a/qom/cpu.c b/qom/cpu.c
index e42d9a7..aab8437 100644
--- a/qom/cpu.c
+++ b/qom/cpu.c
@@ -67,37 +67,6 @@ CPUState *cpu_create(const char *typename)
     return cpu;
 }
 
-const char *cpu_parse_cpu_model(const char *typename, const char *cpu_model)
-{
[...]
-}
-
-CPUState *cpu_generic_init(const char *typename, const char *cpu_model)
-{
[...]
-}
-
 bool cpu_paging_enabled(const CPUState *cpu)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
diff --git a/vl.c b/vl.c
index 2586f25..178bca3 100644
--- a/vl.c
+++ b/vl.c
@@ -4609,17 +4609,13 @@ int main(int argc, char **argv, char **envp)
     current_machine->maxram_size = maxram_size;
     current_machine->ram_slots = ram_slots;
     current_machine->boot_order = boot_order;
-    current_machine->cpu_model = cpu_model;
 
     parse_numa_opts(current_machine);
 
     /* parse features once if machine provides default cpu_type */
-    if (machine_class->default_cpu_type) {
-        current_machine->cpu_type = machine_class->default_cpu_type;
-        if (cpu_model) {
-            current_machine->cpu_type =
-                cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
-        }
+    current_machine->cpu_type = machine_class->default_cpu_type;
+    if (cpu_model) {
+        current_machine->cpu_type = parse_cpu_model(cpu_model);
     }
 
     machine_run_board_init(current_machine);
Eduardo Habkost Jan. 19, 2018, 1:14 p.m. UTC | #5
On Fri, Jan 19, 2018 at 11:14:39AM +0100, Igor Mammedov wrote:
> On Thu, 18 Jan 2018 17:18:09 -0200
> Eduardo Habkost <ehabkost@redhat.com> wrote:
> 
> > On Thu, Jan 18, 2018 at 11:10:35AM +0100, Igor Mammedov wrote:
> > > On Wed, 17 Jan 2018 23:48:46 -0200
> > > Eduardo Habkost <ehabkost@redhat.com> wrote:
> > >   
> > > > On Wed, Jan 17, 2018 at 04:43:32PM +0100, Igor Mammedov wrote:  
> > > > > The last user of it was machine type 'none', which used field
> > > > > to create CPU id user requested it on CLI with -cpu option.
> [...]
> 
> > It looks like default_cpu_type is being overloaded for two
> > different roles: 1) specifying the default CPU type; 2) finding
> > the arch-specific class to be used to parse -cpu.
> > 
> > In the case of null-machine, these two roles conflict with each
> > other.  I believe we can find other solutions instead of this
> > hack that involves lying on MachineClass::default_cpu_type (and
> > then having to work around the lie on machine_none_init()).
> > 
> > I see multiple options: adding a new MachineClass field for that
> > (e.g.  resolving_cpu_type, which defaults to default_cpu_type if
> > NULL); moving the CPU parsing code to arch_init.c (so it could
> > use CPU_RESOLVING_TYPE or something similar); adding a optional
> > MachineClass::parse_cpu_model hook.  We could even try to get rid
> > of CPUClass::parse_features completely
> Adding hooks just for the sake on null-machine seems to be overkill,
> I'd go for arch_init.c but it won't work for linux-user, how about
> exec.c as following:
> 
> diff --git a/include/qom/cpu.h b/include/qom/cpu.h
> index 93bd546..0185589 100644
> --- a/include/qom/cpu.h
> +++ b/include/qom/cpu.h
> @@ -661,8 +661,7 @@ ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
> [...]
> 
> diff --git a/exec.c b/exec.c
> index d28fc0c..4543f06 100644
> --- a/exec.c
> +++ b/exec.c
> @@ -817,6 +817,29 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
>  #endif
>  }
>  
> +const char *parse_cpu_model(const char *cpu_model)
> +{
> +    ObjectClass *oc;
> +    CPUClass *cc;
> +    gchar **model_pieces;
> +    const char *cpu_type;
> +
> +    model_pieces = g_strsplit(cpu_model, ",", 2);
> +
> +    oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
> +    if (oc == NULL) {
> +        error_report("unable to find CPU model '%s'", model_pieces[0]);
> +        g_strfreev(model_pieces);
> +        exit(EXIT_FAILURE);
> +    }
> +
> +    cpu_type = object_class_get_name(oc);
> +    cc = CPU_CLASS(oc);
> +    cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
> +    g_strfreev(model_pieces);
> +    return cpu_type;
> +}

Sounds good to me.  Only two comments:

This looks like duplication of cpu_parse_cpu_model().  Should
this function body be replaced with:
  cpu_parse_cpu_model(CPU_RESOLVING_TYPE, cpu_model)
?

I would move this to arch_init.c, because that's where existing
target-dependent initialization code lives.


> [...]
Igor Mammedov Jan. 19, 2018, 1:39 p.m. UTC | #6
On Fri, 19 Jan 2018 11:14:30 -0200
Eduardo Habkost <ehabkost@redhat.com> wrote:

> On Fri, Jan 19, 2018 at 11:14:39AM +0100, Igor Mammedov wrote:
> > On Thu, 18 Jan 2018 17:18:09 -0200
> > Eduardo Habkost <ehabkost@redhat.com> wrote:
> > 
> > > On Thu, Jan 18, 2018 at 11:10:35AM +0100, Igor Mammedov wrote:
> > > > On Wed, 17 Jan 2018 23:48:46 -0200
> > > > Eduardo Habkost <ehabkost@redhat.com> wrote:
> > > >   
> > > > > On Wed, Jan 17, 2018 at 04:43:32PM +0100, Igor Mammedov wrote:  
> > > > > > The last user of it was machine type 'none', which used field
> > > > > > to create CPU id user requested it on CLI with -cpu option.
> > [...]
> > 
> > > It looks like default_cpu_type is being overloaded for two
> > > different roles: 1) specifying the default CPU type; 2) finding
> > > the arch-specific class to be used to parse -cpu.
> > > 
> > > In the case of null-machine, these two roles conflict with each
> > > other.  I believe we can find other solutions instead of this
> > > hack that involves lying on MachineClass::default_cpu_type (and
> > > then having to work around the lie on machine_none_init()).
> > > 
> > > I see multiple options: adding a new MachineClass field for that
> > > (e.g.  resolving_cpu_type, which defaults to default_cpu_type if
> > > NULL); moving the CPU parsing code to arch_init.c (so it could
> > > use CPU_RESOLVING_TYPE or something similar); adding a optional
> > > MachineClass::parse_cpu_model hook.  We could even try to get rid
> > > of CPUClass::parse_features completely
> > Adding hooks just for the sake on null-machine seems to be overkill,
> > I'd go for arch_init.c but it won't work for linux-user, how about
> > exec.c as following:
> > 
> > diff --git a/include/qom/cpu.h b/include/qom/cpu.h
> > index 93bd546..0185589 100644
> > --- a/include/qom/cpu.h
> > +++ b/include/qom/cpu.h
> > @@ -661,8 +661,7 @@ ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
> > [...]
> > 
> > diff --git a/exec.c b/exec.c
> > index d28fc0c..4543f06 100644
> > --- a/exec.c
> > +++ b/exec.c
> > @@ -817,6 +817,29 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
> >  #endif
> >  }
> >  
> > +const char *parse_cpu_model(const char *cpu_model)
> > +{
> > +    ObjectClass *oc;
> > +    CPUClass *cc;
> > +    gchar **model_pieces;
> > +    const char *cpu_type;
> > +
> > +    model_pieces = g_strsplit(cpu_model, ",", 2);
> > +
> > +    oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
> > +    if (oc == NULL) {
> > +        error_report("unable to find CPU model '%s'", model_pieces[0]);
> > +        g_strfreev(model_pieces);
> > +        exit(EXIT_FAILURE);
> > +    }
> > +
> > +    cpu_type = object_class_get_name(oc);
> > +    cc = CPU_CLASS(oc);
> > +    cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
> > +    g_strfreev(model_pieces);
> > +    return cpu_type;
> > +}
> 
> Sounds good to me.  Only two comments:
> 
> This looks like duplication of cpu_parse_cpu_model().  Should
> this function body be replaced with:
>   cpu_parse_cpu_model(CPU_RESOLVING_TYPE, cpu_model)
it's cpu_parse_cpu_model() which is moved to exec.c
and first typename argument is replaced by inline CPU_RESOLVING_TYPE


> I would move this to arch_init.c, because that's where existing
> target-dependent initialization code lives.
arch_init.c doesn't fit linux-user, it has only sys emulation code
while exec.c is used by both and still target depended so CPU_RESOLVING_TYPE
could be used there

> 
> 
> > [...]
>
Eduardo Habkost Jan. 19, 2018, 2:23 p.m. UTC | #7
On Fri, Jan 19, 2018 at 02:39:49PM +0100, Igor Mammedov wrote:
> On Fri, 19 Jan 2018 11:14:30 -0200
> Eduardo Habkost <ehabkost@redhat.com> wrote:
> 
> > On Fri, Jan 19, 2018 at 11:14:39AM +0100, Igor Mammedov wrote:
> > > On Thu, 18 Jan 2018 17:18:09 -0200
> > > Eduardo Habkost <ehabkost@redhat.com> wrote:
> > > 
> > > > On Thu, Jan 18, 2018 at 11:10:35AM +0100, Igor Mammedov wrote:
> > > > > On Wed, 17 Jan 2018 23:48:46 -0200
> > > > > Eduardo Habkost <ehabkost@redhat.com> wrote:
> > > > >   
> > > > > > On Wed, Jan 17, 2018 at 04:43:32PM +0100, Igor Mammedov wrote:  
> > > > > > > The last user of it was machine type 'none', which used field
> > > > > > > to create CPU id user requested it on CLI with -cpu option.
> > > [...]
> > > 
> > > > It looks like default_cpu_type is being overloaded for two
> > > > different roles: 1) specifying the default CPU type; 2) finding
> > > > the arch-specific class to be used to parse -cpu.
> > > > 
> > > > In the case of null-machine, these two roles conflict with each
> > > > other.  I believe we can find other solutions instead of this
> > > > hack that involves lying on MachineClass::default_cpu_type (and
> > > > then having to work around the lie on machine_none_init()).
> > > > 
> > > > I see multiple options: adding a new MachineClass field for that
> > > > (e.g.  resolving_cpu_type, which defaults to default_cpu_type if
> > > > NULL); moving the CPU parsing code to arch_init.c (so it could
> > > > use CPU_RESOLVING_TYPE or something similar); adding a optional
> > > > MachineClass::parse_cpu_model hook.  We could even try to get rid
> > > > of CPUClass::parse_features completely
> > > Adding hooks just for the sake on null-machine seems to be overkill,
> > > I'd go for arch_init.c but it won't work for linux-user, how about
> > > exec.c as following:
> > > 
> > > diff --git a/include/qom/cpu.h b/include/qom/cpu.h
> > > index 93bd546..0185589 100644
> > > --- a/include/qom/cpu.h
> > > +++ b/include/qom/cpu.h
> > > @@ -661,8 +661,7 @@ ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
> > > [...]
> > > 
> > > diff --git a/exec.c b/exec.c
> > > index d28fc0c..4543f06 100644
> > > --- a/exec.c
> > > +++ b/exec.c
> > > @@ -817,6 +817,29 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
> > >  #endif
> > >  }
> > >  
> > > +const char *parse_cpu_model(const char *cpu_model)
> > > +{
> > > +    ObjectClass *oc;
> > > +    CPUClass *cc;
> > > +    gchar **model_pieces;
> > > +    const char *cpu_type;
> > > +
> > > +    model_pieces = g_strsplit(cpu_model, ",", 2);
> > > +
> > > +    oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
> > > +    if (oc == NULL) {
> > > +        error_report("unable to find CPU model '%s'", model_pieces[0]);
> > > +        g_strfreev(model_pieces);
> > > +        exit(EXIT_FAILURE);
> > > +    }
> > > +
> > > +    cpu_type = object_class_get_name(oc);
> > > +    cc = CPU_CLASS(oc);
> > > +    cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
> > > +    g_strfreev(model_pieces);
> > > +    return cpu_type;
> > > +}
> > 
> > Sounds good to me.  Only two comments:
> > 
> > This looks like duplication of cpu_parse_cpu_model().  Should
> > this function body be replaced with:
> >   cpu_parse_cpu_model(CPU_RESOLVING_TYPE, cpu_model)
> it's cpu_parse_cpu_model() which is moved to exec.c
> and first typename argument is replaced by inline CPU_RESOLVING_TYPE
> 
> 
> > I would move this to arch_init.c, because that's where existing
> > target-dependent initialization code lives.
> arch_init.c doesn't fit linux-user, it has only sys emulation code
> while exec.c is used by both and still target depended so CPU_RESOLVING_TYPE
> could be used there

I don't really like adding more code to the mess that is exec.c,
but this looks like the simplest solution.
diff mbox series

Patch

diff --git a/include/hw/boards.h b/include/hw/boards.h
index 156b16f..decd0ec 100644
--- a/include/hw/boards.h
+++ b/include/hw/boards.h
@@ -246,7 +246,6 @@  struct MachineState {
     char *kernel_filename;
     char *kernel_cmdline;
     char *initrd_filename;
-    const char *cpu_model;
     const char *cpu_type;
     AccelState *accelerator;
     CPUArchIdList *possible_cpus;
diff --git a/hw/core/null-machine.c b/hw/core/null-machine.c
index 864832d..c2e466c 100644
--- a/hw/core/null-machine.c
+++ b/hw/core/null-machine.c
@@ -23,10 +23,13 @@ 
 static void machine_none_init(MachineState *mch)
 {
     CPUState *cpu = NULL;
+    MachineClass *mc = MACHINE_GET_CLASS(mch);
 
-    /* Initialize CPU (if a model has been specified) */
-    if (mch->cpu_model) {
-        cpu = cpu_init(mch->cpu_model);
+    /* Initialize CPU if cpu_type pointer is user provided
+     * (i.e. != to pointer tot static default cpu type string)
+     */
+    if (mch->cpu_type != mc->default_cpu_type) {
+        cpu = cpu_create(mch->cpu_type);
         if (!cpu) {
             error_report("Unable to initialize CPU");
             exit(1);
@@ -54,6 +57,7 @@  static void machine_none_machine_init(MachineClass *mc)
     mc->init = machine_none_init;
     mc->max_cpus = 1;
     mc->default_ram_size = 0;
+    mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
 }
 
 DEFINE_MACHINE("none", machine_none_machine_init)
diff --git a/vl.c b/vl.c
index 2586f25..8aa0131 100644
--- a/vl.c
+++ b/vl.c
@@ -4609,7 +4609,6 @@  int main(int argc, char **argv, char **envp)
     current_machine->maxram_size = maxram_size;
     current_machine->ram_slots = ram_slots;
     current_machine->boot_order = boot_order;
-    current_machine->cpu_model = cpu_model;
 
     parse_numa_opts(current_machine);
 
@@ -4619,6 +4618,13 @@  int main(int argc, char **argv, char **envp)
         if (cpu_model) {
             current_machine->cpu_type =
                 cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
+
+            /* machine 'none' depends on default cpu type pointer not being
+             * equal to resolved type name pointer to fugure out if type was
+             * user provided, make sure that if it becomes not true in future
+             * it won't beark silently */
+            g_assert(
+                current_machine->cpu_type != machine_class->default_cpu_type);
         }
     }