[{"id":1779003,"web_url":"http://patchwork.ozlabs.org/comment/1779003/","msgid":"<20171003141002.GB7087@localhost.localdomain>","list_archive_url":null,"date":"2017-10-03T14:10:02","subject":"Re: [Qemu-devel] [RFC v2 1/2] machine: Add a valid_cpu_types\n\tproperty","submitter":{"id":195,"url":"http://patchwork.ozlabs.org/api/people/195/","name":"Eduardo Habkost","email":"ehabkost@redhat.com"},"content":"On Thu, Sep 21, 2017 at 04:41:50PM -0700, Alistair Francis wrote:\n> Signed-off-by: Alistair Francis <alistair.francis@xilinx.com>\n> ---\n> \n> RFC v2:\n>  - Rebase on Igor's cpu_type work\n>  - Use object_class_dynamic_cast()\n>  - Use a NULL terminated cahr** list\n>  - Do the check before the machine_class init() is called\n> \n> \n>  hw/core/machine.c   | 35 +++++++++++++++++++++++++++++++++++\n>  include/hw/boards.h |  1 +\n>  2 files changed, 36 insertions(+)\n> \n> diff --git a/hw/core/machine.c b/hw/core/machine.c\n> index 80647edc2a..abebfabdb8 100644\n> --- a/hw/core/machine.c\n> +++ b/hw/core/machine.c\n> @@ -758,6 +758,41 @@ void machine_run_board_init(MachineState *machine)\n>      if (nb_numa_nodes) {\n>          machine_numa_finish_init(machine);\n>      }\n> +\n> +    if (machine_class->valid_cpu_types && machine->cpu_type) {\n> +        int i;\n> +\n> +        for (i = 0; machine_class->valid_cpu_types[i]; i++) {\n> +            ObjectClass *class = object_class_by_name(machine->cpu_type);\n> +\n> +            if (!class) {\n> +                break;\n> +            }\n> +\n> +            if (object_class_dynamic_cast(class,\n> +                                          machine_class->valid_cpu_types[i])) {\n> +                /* The user specificed CPU is in the valid field, we are\n> +                 * good to go.\n> +                 */\n> +                goto done;\n\nI would move the object_class_by_name() call outside the for loop\nand remove the \"goto\", like this:\n\n    if (machine->cpu_type && machine_class->valid_cpu_types) {\n        ObjectClass *class = object_class_by_name(machine->cpu_type);\n        int i;\n\n        /* machine->cpu_type is supposed to be always a valid QOM type */\n        assert(class);\n\n        for (i = 0; machine_class->valid_cpu_types[i]; i++) {\n            if (object_class_dynamic_cast(class,\n                                          machine_class->valid_cpu_types[i])) {\n                /* Valid CPU type, we're good to go */\n                break;\n            }\n        }\n        if (!machine_class->valid_cpu_types[i]) {\n            error_report(...);\n            ...\n        }\n    }\n\n    machine_class->init(machine);\n\n> +            }\n> +        }\n> +\n> +        /* The user specified CPU must not be a valid CPU, print a sane\n> +         * error\n> +         */\n> +        error_report(\"Invalid CPU: %s\", machine->cpu_type);\n> +        error_printf(\"The valid options are: %s\",\n> +                     machine_class->valid_cpu_types[0]);\n> +        for (i = 1; machine_class->valid_cpu_types[i]; i++) {\n> +            error_printf(\", %s\", machine_class->valid_cpu_types[i]);\n> +        }\n> +        error_printf(\"\\n\");\n\nI would still like to make this share code with\nquery-cpu-definitions one day, but I'm OK with this\nimplementation.\n\nI would just rewrite the message as \"valid types are:\" instead of\n\"valid options are:\" and \"Invalid CPU type:\" instead of \"Invalid\nCPU:\", because the -cpu option doesn't need to match a string in\nvalid_cpu_types exactly, it just needs to resolve to a type that\nimplements a valid type.\n\n\n> +\n> +        exit(1);\n> +    }\n> +\n> +done:\n>      machine_class->init(machine);\n>  }\n>  \n> diff --git a/include/hw/boards.h b/include/hw/boards.h\n> index 156e0a5701..191a5b3cd8 100644\n> --- a/include/hw/boards.h\n> +++ b/include/hw/boards.h\n> @@ -191,6 +191,7 @@ struct MachineClass {\n>      bool has_hotpluggable_cpus;\n>      bool ignore_memory_transaction_failures;\n>      int numa_mem_align_shift;\n> +    const char **valid_cpu_types;\n>      void (*numa_auto_assign_ram)(MachineClass *mc, NodeInfo *nodes,\n>                                   int nb_nodes, ram_addr_t size);\n>  \n> -- \n> 2.11.0\n> \n>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=ehabkost@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3y61D04sQDz9t2Q\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed,  4 Oct 2017 01:11:12 +1100 (AEDT)","from localhost ([::1]:58820 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dzNug-0001Nx-P1\n\tfor incoming@patchwork.ozlabs.org; Tue, 03 Oct 2017 10:11:10 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:48975)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <ehabkost@redhat.com>) id 1dzNu1-0001Fz-Fv\n\tfor qemu-devel@nongnu.org; Tue, 03 Oct 2017 10:10:39 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <ehabkost@redhat.com>) id 1dzNtr-0007uz-Ii\n\tfor qemu-devel@nongnu.org; Tue, 03 Oct 2017 10:10:29 -0400","from mx1.redhat.com ([209.132.183.28]:58592)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <ehabkost@redhat.com>) id 1dzNtr-0007tP-8w\n\tfor qemu-devel@nongnu.org; Tue, 03 Oct 2017 10:10:19 -0400","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 4DC3A776D3;\n\tTue,  3 Oct 2017 14:10:18 +0000 (UTC)","from localhost (ovpn-116-6.gru2.redhat.com [10.97.116.6])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 0EB3A6292A;\n\tTue,  3 Oct 2017 14:10:03 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 4DC3A776D3","Date":"Tue, 3 Oct 2017 11:10:02 -0300","From":"Eduardo Habkost <ehabkost@redhat.com>","To":"Alistair Francis <alistair.francis@xilinx.com>","Message-ID":"<20171003141002.GB7087@localhost.localdomain>","References":"<cover.1506037164.git.alistair.francis@xilinx.com>\n\t<d2e446ccb46d71bed3191fc97fe1aa6c884ea4ba.1506037164.git.alistair.francis@xilinx.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<d2e446ccb46d71bed3191fc97fe1aa6c884ea4ba.1506037164.git.alistair.francis@xilinx.com>","X-Fnord":"you can see the fnord","User-Agent":"Mutt/1.8.3 (2017-05-23)","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.15","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.27]);\n\tTue, 03 Oct 2017 14:10:18 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"Re: [Qemu-devel] [RFC v2 1/2] machine: Add a valid_cpu_types\n\tproperty","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"marcel@redhat.com, alistair23@gmail.com, qemu-devel@nongnu.org,\n\timammedo@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"}}]