From patchwork Thu Jul 14 00:03:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 9228631 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D705860868 for ; Thu, 14 Jul 2016 00:09:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C626E27D9B for ; Thu, 14 Jul 2016 00:09:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BAB8A27FBE; Thu, 14 Jul 2016 00:09:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CAA1627FB6 for ; Thu, 14 Jul 2016 00:09:10 +0000 (UTC) Received: from localhost ([::1]:50639 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bNUDF-0004d6-Th for patchwork-qemu-devel@patchwork.kernel.org; Wed, 13 Jul 2016 20:09:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33588) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bNU8Q-0007rE-2X for qemu-devel@nongnu.org; Wed, 13 Jul 2016 20:04:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bNU8L-0000Q1-QM for qemu-devel@nongnu.org; Wed, 13 Jul 2016 20:04:09 -0400 Received: from mail-bl2nam02on0049.outbound.protection.outlook.com ([104.47.38.49]:35731 helo=NAM02-BL2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bNU8L-0000Pu-LV for qemu-devel@nongnu.org; Wed, 13 Jul 2016 20:04:05 -0400 Received: from BL2NAM02FT030.eop-nam02.prod.protection.outlook.com (10.152.76.59) by BL2NAM02HT028.eop-nam02.prod.protection.outlook.com (10.152.77.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.534.7; Thu, 14 Jul 2016 00:04:04 +0000 Authentication-Results: spf=fail (sender IP is 149.199.60.96) smtp.mailfrom=xilinx.com; redhat.com; dkim=none (message not signed) header.d=none; redhat.com; dmarc=none action=none header.from=xilinx.com; Received-SPF: Fail (protection.outlook.com: domain of xilinx.com does not designate 149.199.60.96 as permitted sender) receiver=protection.outlook.com; client-ip=149.199.60.96; helo=xsj-tvapsmtpgw01; Received: from xsj-tvapsmtpgw01 (149.199.60.96) by BL2NAM02FT030.mail.protection.outlook.com (10.152.77.172) with Microsoft SMTP Server (TLS) id 15.1.523.9 via Frontend Transport; Thu, 14 Jul 2016 00:04:03 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:38583 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1bNU8I-0007OH-VV; Wed, 13 Jul 2016 17:04:02 -0700 Received: from [127.0.0.1] (port=43871 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1bNU8I-0001AH-Ta; Wed, 13 Jul 2016 17:04:02 -0700 Received: from xsj-tvapsmtp02 (smtptest.xilinx.com [172.16.1.203]) by tsj-smtp-dlp1.xlnx.xilinx.com (8.13.8/8.13.1) with ESMTP id u6DNvNEq011034; Wed, 13 Jul 2016 16:57:23 -0700 Received: from [172.19.74.182] (port=53734 helo=xsjalistai50.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1bNU8I-0001AD-2w; Wed, 13 Jul 2016 17:04:02 -0700 From: Alistair Francis To: , Date: Wed, 13 Jul 2016 17:03:39 -0700 Message-ID: <6fe47650a3e796b085e5471e40c4767029b81e1b.1468454556.git.alistair.francis@xilinx.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: X-RCIS-Action: ALLOW X-TM-AS-MML: disable X-TM-AS-Product-Ver: IMSS-7.1.0.1679-8.0.0.1202-22448.005 X-TM-AS-Result: No--14.091-7.0-31-10 X-imss-scan-details: No--14.091-7.0-31-10 X-TMASE-MatchedRID: NCA+VdI4JahYXTxImR5ZvCZm6wdY+F8KM3PBQtDBME+voBtU8+SlKYpZ vGWtY3IJRjfWvITVJoxBNVNaChpY1IBdohkDm/9NUylSIDEm+Mdzd7C7BtJobtN4wOZZ8JcGDZV LXrOM4unB55sn6soSHGKt8MP+rfhxX79c1SLXpCxIOSHptb5tx8DZw0nL+opS2e73tJcoE9gnVT 2S49OfF0V8edMVEsKhJ9mVAtKoclsvjtMPTTOa8hcanaCAqviGVNyumldWne3fUZT83lbkEKcwf OzvS1WBTpgKTWoi1s3nU7CCEjbK3wa1NXbjqus0wLYk+NSxc5qqxXpq+unGVVrCirnOr0+sU3m+ JXAyVvAAX8fSJuWY0s5KMh5KMcHqvpR6acywKCslcqT+ugT9ENMB70WyBtjHCqIJhrrDy29yzNk fTVuQ1oxiDZnDw/PKuOU7l9FBif1viLFHxu+T9i4uTw19Klh6Q95F2IiVUkQXGHD9U1OuR10QPh PwqGAW1PRxaXMsma9jgysChoSkvE/qpx4DtjGJ9Ib/6w+1lWSZZm9OwABc9nmdNky49LCS30bsD mXjAEaweF8ygxYqFfvQCPiy8IEcHxPMjOKY7A8LbigRnpKlKSPzRlrdFGDwoFwak7AupABqkJ0h koa3s55bSJ0NzXcBj4zz17AieYDUdEFoDWpzeQ== X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:149.199.60.96; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(1110001)(1109001)(339900001)(199003)(189002)(6806005)(47776003)(33646002)(5001770100001)(50226002)(11100500001)(7696003)(5003600100003)(356003)(81166006)(8936002)(229853001)(305945005)(76176999)(106466001)(86362001)(19580405001)(105606002)(7846002)(48376002)(586003)(4326007)(71366001)(19580395003)(189998001)(77096005)(50466002)(2950100001)(5003940100001)(8676002)(85426001)(36756003)(81156014)(87936001)(64026002)(118296001)(92566002)(2906002)(50986999)(575784001)(9786002)(41533002)(107986001)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:BL2NAM02HT028; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; PTR:unknown-60-96.xilinx.com; A:1; MX:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 8b48c3f5-0585-4484-2e24-08d3ab7a5dff X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BL2NAM02HT028; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192813158149592)(170618885588394); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13023025)(13024025)(13017025)(13018025)(8121501046)(13015025)(5005006)(10201501046)(3002001)(6055026); SRVR:BL2NAM02HT028; BCL:0; PCL:0; RULEID:; SRVR:BL2NAM02HT028; X-Forefront-PRVS: 00032065B2 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2016 00:04:03.7298 (UTC) X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c; Ip=[149.199.60.96]; Helo=[xsj-tvapsmtpgw01] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2NAM02HT028 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.38.49 Subject: [Qemu-devel] [PATCH v9 7/8] generic-loader: Add a generic loader X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: cov@codeaurora.org, crosthwaitepeter@gmail.com, pbonzini@redhat.com, armbru@redhat.com, alistair.francis@xilinx.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add a generic loader to QEMU which can be used to load images or set memory values. Signed-off-by: Alistair Francis --- V9: - Fix error messages - Updated some incorrect logic - Add address space loading support for all image types - Explain why the reset is manually registered V8: - Code corrections - Rebase V7: - Rebase V6: - Add error checking V5: - Rebase V4: - Allow the loader to work with every architecture - Move the file to hw/core - Increase the maximum number of CPUs - Make the CPU operations conditional - Convert the cpu option to cpu-num - Require the user to specify endianess V3: - Pass the ram_size to load_image_targphys() V2: - Add maintainers entry - Perform bounds checking - Register and unregister the reset in the realise/unrealise Changes since RFC: - Add BE support MAINTAINERS | 6 ++ hw/core/Makefile.objs | 2 + hw/core/generic-loader.c | 185 +++++++++++++++++++++++++++++++++++++++ include/hw/core/generic-loader.h | 45 ++++++++++ 4 files changed, 238 insertions(+) create mode 100644 hw/core/generic-loader.c create mode 100644 include/hw/core/generic-loader.h diff --git a/MAINTAINERS b/MAINTAINERS index 1d0e2c3..ea3a3b5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -993,6 +993,12 @@ M: Dmitry Fleytman S: Maintained F: hw/net/e1000e* +Generic Loader +M: Alistair Francis +S: Maintained +F: hw/core/generic-loader.c +F: include/hw/core/generic-loader.h + Subsystems ---------- Audio diff --git a/hw/core/Makefile.objs b/hw/core/Makefile.objs index cfd4840..939c94e 100644 --- a/hw/core/Makefile.objs +++ b/hw/core/Makefile.objs @@ -17,3 +17,5 @@ common-obj-$(CONFIG_SOFTMMU) += loader.o common-obj-$(CONFIG_SOFTMMU) += qdev-properties-system.o common-obj-$(CONFIG_SOFTMMU) += register.o common-obj-$(CONFIG_PLATFORM_BUS) += platform-bus.o + +obj-$(CONFIG_SOFTMMU) += generic-loader.o diff --git a/hw/core/generic-loader.c b/hw/core/generic-loader.c new file mode 100644 index 0000000..3fbb954 --- /dev/null +++ b/hw/core/generic-loader.c @@ -0,0 +1,185 @@ +/* + * Generic Loader + * + * Copyright (C) 2014 Li Guang + * Copyright (C) 2016 Xilinx Inc. + * Written by Li Guang + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "qemu/osdep.h" +#include "qom/cpu.h" +#include "hw/sysbus.h" +#include "sysemu/dma.h" +#include "hw/loader.h" +#include "qapi/error.h" +#include "hw/core/generic-loader.h" + +#define CPU_NONE 0xFFFFFFFF + +static void generic_loader_reset(void *opaque) +{ + GenericLoaderState *s = GENERIC_LOADER(opaque); + + if (s->cpu) { + CPUClass *cc = CPU_GET_CLASS(s->cpu); + cpu_reset(s->cpu); + if (cc) { + cc->set_pc(s->cpu, s->addr); + } + } + + if (s->data_len) { + assert(s->data_len < sizeof(s->data)); + dma_memory_write(first_cpu->as, s->addr, &s->data, + s->data_len); + } +} + +static void generic_loader_realize(DeviceState *dev, Error **errp) +{ + GenericLoaderState *s = GENERIC_LOADER(dev); + hwaddr entry; + int big_endian; + int size = 0; + + /* Perform some error checking on the user's options */ + if (s->data || s->data_len || s->data_be) { + /* User is loading memory values */ + if (s->file) { + error_setg(errp, "Specifying a file is not supported when loading " + "memory values"); + return; + } else if (s->force_raw) { + error_setg(errp, "Specifying force-raw is not supported when " + "loading memory values"); + return; + } else if (!s->data || !s->data_len) { + error_setg(errp, "Both data and data-len must be specified"); + return; + } else if (s->cpu_num != CPU_NONE) { + error_setg(errp, "Setting data and cpu-num is not supported"); + return; + } + } else if (s->file || s->force_raw) { + /* User is loading an image */ + if (s->data || s->data_len || s->data_be) { + error_setg(errp, "data can not be specified when loading an " + "image"); + return; + } + } else if (s->data_len) { + if (s->data_len > 8) { + error_setg(errp, "data-len cannot be greater then 8 bytes"); + return; + } else if (s->data_len > sizeof(s->data)) { + error_setg(errp, "data-len cannot be more then the data size"); + return; + } + } + + qemu_register_reset(generic_loader_reset, dev); + + if (s->cpu_num != CPU_NONE) { + s->cpu = qemu_get_cpu(s->cpu_num); + if (!s->cpu) { + error_setg(errp, "Specified boot CPU#%d is nonexistent", + s->cpu_num); + return; + } + } + +#ifdef TARGET_WORDS_BIGENDIAN + big_endian = 1; +#else + big_endian = 0; +#endif + + if (s->file) { + if (!s->force_raw) { + size = load_elf_as(s->file, NULL, NULL, &entry, NULL, NULL, + big_endian, 0, 0, 0, + (s->cpu ? s->cpu : first_cpu)->as); + + if (size < 0) { + size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL, + (s->cpu ? s->cpu : first_cpu)->as); + } + } + + if (size < 0 || s->force_raw) { + /* Default to the maximum size being the machine's ram size */ + size = load_image_targphys_as(s->file, s->addr, ram_size, + (s->cpu ? s->cpu : first_cpu)->as); + } else { + s->addr = entry; + } + + if (size < 0) { + error_setg(errp, "Cannot load specified image %s", s->file); + return; + } + } + + /* Convert the data endiannes */ + if (s->data_be) { + s->data = cpu_to_be64(s->data); + } else { + s->data = cpu_to_le64(s->data); + } +} + +static void generic_loader_unrealize(DeviceState *dev, Error **errp) +{ + qemu_unregister_reset(generic_loader_reset, dev); +} + +static Property generic_loader_props[] = { + DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0), + DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0), + DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0), + DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false), + DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE), + DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false), + DEFINE_PROP_STRING("file", GenericLoaderState, file), + DEFINE_PROP_END_OF_LIST(), +}; + +static void generic_loader_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + + /* The reset function is not registered here and is instead registered in + * the realize function to allow this device to be added via the device_add + * command in the QEMU monitor. + * TODO: Improve the device_add functionality to allow resets to be + * connected + */ + dc->realize = generic_loader_realize; + dc->unrealize = generic_loader_unrealize; + dc->props = generic_loader_props; + dc->desc = "Generic Loader"; +} + +static TypeInfo generic_loader_info = { + .name = TYPE_GENERIC_LOADER, + .parent = TYPE_DEVICE, + .instance_size = sizeof(GenericLoaderState), + .class_init = generic_loader_class_init, +}; + +static void generic_loader_register_type(void) +{ + type_register_static(&generic_loader_info); +} + +type_init(generic_loader_register_type) diff --git a/include/hw/core/generic-loader.h b/include/hw/core/generic-loader.h new file mode 100644 index 0000000..4dd2361 --- /dev/null +++ b/include/hw/core/generic-loader.h @@ -0,0 +1,45 @@ +/* + * Generic Loader + * + * Copyright (C) 2014 Li Guang + * Written by Li Guang + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef GENERIC_LOADER_H +#define GENERIC_LOADER_H + +#include "elf.h" + +typedef struct GenericLoaderState { + /* */ + DeviceState parent_obj; + + /* */ + CPUState *cpu; + + uint64_t addr; + uint64_t data; + uint8_t data_len; + uint32_t cpu_num; + + char *file; + + bool force_raw; + bool data_be; +} GenericLoaderState; + +#define TYPE_GENERIC_LOADER "loader" +#define GENERIC_LOADER(obj) OBJECT_CHECK(GenericLoaderState, (obj), \ + TYPE_GENERIC_LOADER) + +#endif