From patchwork Fri Feb 19 01:16:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 8355291 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E2B729F399 for ; Fri, 19 Feb 2016 01:20:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 83749203AB for ; Fri, 19 Feb 2016 01:20:24 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id C53B420397 for ; Fri, 19 Feb 2016 01:20:22 +0000 (UTC) Received: from localhost ([::1]:47170 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWZk6-0008Qq-6T for patchwork-qemu-devel@patchwork.kernel.org; Thu, 18 Feb 2016 20:20:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56525) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWZiq-0006Va-Lz for qemu-devel@nongnu.org; Thu, 18 Feb 2016 20:19:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWZin-0006lF-9W for qemu-devel@nongnu.org; Thu, 18 Feb 2016 20:19:04 -0500 Received: from mail-bl2nam02on0051.outbound.protection.outlook.com ([104.47.38.51]:2775 helo=NAM02-BL2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWZin-0006lA-4c for qemu-devel@nongnu.org; Thu, 18 Feb 2016 20:19:01 -0500 Received: from BL2NAM02FT022.eop-nam02.prod.protection.outlook.com (10.152.76.56) by BL2NAM02HT005.eop-nam02.prod.protection.outlook.com (10.152.76.170) with Microsoft SMTP Server (TLS) id 15.1.415.6; Fri, 19 Feb 2016 01:18:59 +0000 Authentication-Results: spf=fail (sender IP is 149.199.60.96) smtp.mailfrom=xilinx.com; suse.de; dkim=none (message not signed) header.d=none; suse.de; 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 BL2NAM02FT022.mail.protection.outlook.com (10.152.77.153) with Microsoft SMTP Server (TLS) id 15.1.415.6 via Frontend Transport; Fri, 19 Feb 2016 01:18:59 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:41598 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1aWZik-0003wP-Gm; Thu, 18 Feb 2016 17:18:58 -0800 Received: from [127.0.0.1] (port=35235 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1aWZik-00019c-Cu; Thu, 18 Feb 2016 17:18:58 -0800 Received: from xsj-tvapsmtp02 (xsj-tvapsmtp02.xilinx.com [172.16.1.203]) by tsj-smtp-dlp1.xlnx.xilinx.com (8.13.8/8.13.1) with ESMTP id u1J1DErT009940; Thu, 18 Feb 2016 17:13:14 -0800 Received: from [172.19.74.182] (port=54252 helo=xsjalistai50.xlnx.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1aWZij-00019Z-H3; Thu, 18 Feb 2016 17:18:57 -0800 From: Alistair Francis To: Date: Thu, 18 Feb 2016 17:16:17 -0800 Message-ID: X-Mailer: git-send-email 2.5.0 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-22138.006 X-TM-AS-Result: No--6.559-7.0-31-10 X-imss-scan-details: No--6.559-7.0-31-10 X-TMASE-MatchedRID: 3gvURbR79jBYXTxImR5ZvCZm6wdY+F8KM3PBQtDBME+voBtU8+SlKeXK q62Og2nz/5YyiAZ6NU9fpDE3h+Imb/UKQfPzAUrjFxqdoICq+IZU3K6aV1ad7d9RlPzeVuQQdY+ OGWBMP+GcPcDHCp4L+kmYu5iCefhTN9efX2r2stGEwZM25BMds4PfQve7FwfAGiJD6+TqBuUu7K 3kCTZ3O7pUJwN0ZtHiUi43GA8Sjd8GlwKxlQJO5vsgrpDX6v2e7f6JAS2hKPgar/YIrQdFgKt3L bZQ6GwM761f9S75s1U4SL43Gxx30oCkWuQJNIQ/i/b+LxU2p47/lBG+uXYJkGq//5Ao1w8uVSdA A6mVeIZt2EWew0eZ13QeHo/CecVWetI/1G0ZliieAiCmPx4NwBnUJ0Ek6yhjxEHRux+uk8h+ICq uNi0WJHkL/HYFcscGoOuv8pqVnfl1skCq44hOu9sTCyMqXmgbftwZ3X11IV0= X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:149.199.60.96; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(1110001)(1109001)(339900001)(189002)(199003)(2950100001)(77096005)(5008740100001)(2906002)(586003)(50986999)(50226001)(5003940100001)(4326007)(5003600100002)(85426001)(92566002)(36756003)(118296001)(76176999)(2351001)(33646002)(229853001)(105606002)(71366001)(106466001)(110136002)(5001960100002)(48376002)(86362001)(50466002)(64026002)(19580405001)(1096002)(87936001)(1220700001)(47776003)(11100500001)(189998001)(19580395003)(6806005)(41533002)(2004002)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:BL2NAM02HT005; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; MLV:sfv; MX:1; A:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 78c22954-5032-42b4-e6d3-08d338caa539 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BL2NAM02HT005; X-Microsoft-Antispam-PRVS: <029ec0b86872416a9257a3c3463c71d7@BL2NAM02HT005.eop-nam02.prod.protection.outlook.com> X-Exchange-Antispam-Report-Test: UriScan:(192813158149592); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13024025)(13023025)(13017025)(13015025)(13018025)(5005006)(8121501046)(3002001)(10201501046); SRVR:BL2NAM02HT005; BCL:0; PCL:0; RULEID:; SRVR:BL2NAM02HT005; X-Forefront-PRVS: 08572BD77F X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Feb 2016 01:18:59.2987 (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: BL2NAM02HT005 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.38.51 Cc: peter.maydell@linaro.org, armbru@redhat.com, alistair.francis@xilinx.com, crosthwaitepeter@gmail.com, cov@codeaurora.org, pbonzini@redhat.com, afaerber@suse.de Subject: [Qemu-devel] [PATCH v2 1/2] generic-loader: Add a generic loader X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a generic loader to QEMU which can be used to load images or set memory values. This only supports ARM architectures at the moment. Signed-off-by: Alistair Francis --- V2: - Add maintainers entry - Perform bounds checking - Register and unregister the reset in the realise/unrealise Changes since RFC: - Add BE support MAINTAINERS | 6 ++ default-configs/arm-softmmu.mak | 1 + hw/misc/Makefile.objs | 2 + hw/misc/generic-loader.c | 142 +++++++++++++++++++++++++++++++++++++++ include/hw/misc/generic-loader.h | 50 ++++++++++++++ 5 files changed, 201 insertions(+) create mode 100644 hw/misc/generic-loader.c create mode 100644 include/hw/misc/generic-loader.h diff --git a/MAINTAINERS b/MAINTAINERS index 02710f8..1bc92c3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -956,6 +956,12 @@ F: hw/acpi/nvdimm.c F: hw/mem/nvdimm.c F: include/hw/mem/nvdimm.h +Generic Loader +M: Alistair Francis +S: Maintained +F: hw/misc/generic-loader.c +F: include/hw/misc/generic-loader.h + Subsystems ---------- Audio diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak index a9f82a1..b246b75 100644 --- a/default-configs/arm-softmmu.mak +++ b/default-configs/arm-softmmu.mak @@ -110,3 +110,4 @@ CONFIG_IOH3420=y CONFIG_I82801B11=y CONFIG_ACPI=y CONFIG_SMBIOS=y +CONFIG_LOADER=y diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs index ea6cd3c..9f05dcf 100644 --- a/hw/misc/Makefile.objs +++ b/hw/misc/Makefile.objs @@ -46,3 +46,5 @@ obj-$(CONFIG_STM32F2XX_SYSCFG) += stm32f2xx_syscfg.o obj-$(CONFIG_PVPANIC) += pvpanic.o obj-$(CONFIG_EDU) += edu.o obj-$(CONFIG_HYPERV_TESTDEV) += hyperv_testdev.o + +obj-$(CONFIG_LOADER) += generic-loader.o diff --git a/hw/misc/generic-loader.c b/hw/misc/generic-loader.c new file mode 100644 index 0000000..e551a38 --- /dev/null +++ b/hw/misc/generic-loader.c @@ -0,0 +1,142 @@ +/* + * 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 "hw/sysbus.h" +#include "sysemu/dma.h" +#include "hw/loader.h" +#include "hw/misc/generic-loader.h" + +#define CPU_NONE 0xFF + +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); + cc->set_pc(s->cpu, s->addr); + } + + if (s->data_len) { + assert(s->data_len < sizeof(s->data)); + dma_memory_write((s->cpu ? s->cpu : 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; + + qemu_register_reset(generic_loader_reset, dev); + + if (s->cpu_nr != CPU_NONE) { + CPUState *cs = first_cpu; + int cpu_num = 0; + + CPU_FOREACH(cs) { + if (cpu_num == s->cpu_nr) { + s->cpu = cs; + break; + } else if (!CPU_NEXT(cs)) { + error_setg(errp, "Specified boot CPU#%d is nonexistent", + s->cpu_nr); + return; + } else { + cpu_num++; + } + } + } + +#ifdef TARGET_WORDS_BIGENDIAN + big_endian = 1; +#else + big_endian = 0; +#endif + + if (s->file) { + if (!s->force_raw) { + size = load_elf(s->file, NULL, NULL, &entry, NULL, NULL, + big_endian, ELF_ARCH, 0); + + if (size < 0) { + size = load_uimage(s->file, &entry, NULL, NULL, NULL, NULL); + } + } + + if (size < 0) { + size = load_image_targphys(s->file, s->addr, 0); + } else { + s->addr = entry; + } + + if (size < 0) { + error_setg(errp, "Cannot load specified image %s", s->file); + return; + } + } + + if (s->data_len && (s->data_len > sizeof(s->data))) { + error_setg(errp, "data-len cannot be more then the data size"); + return; + } +} + +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_UINT8("cpu", GenericLoaderState, cpu_nr, 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); + + 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/misc/generic-loader.h b/include/hw/misc/generic-loader.h new file mode 100644 index 0000000..79b5536 --- /dev/null +++ b/include/hw/misc/generic-loader.h @@ -0,0 +1,50 @@ +/* + * 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" + +#if defined(TARGET_AARCH64) + #define ELF_ARCH EM_AARCH64 +#elif defined(TARGET_ARM) + #define ELF_ARCH EM_ARM +#endif + +typedef struct GenericLoaderState { + /* */ + DeviceState parent_obj; + + /* */ + CPUState *cpu; + + uint64_t addr; + uint64_t data; + uint8_t data_len; + uint8_t cpu_nr; + + char *file; + + bool force_raw; +} GenericLoaderState; + +#define TYPE_GENERIC_LOADER "loader" +#define GENERIC_LOADER(obj) OBJECT_CHECK(GenericLoaderState, (obj), \ + TYPE_GENERIC_LOADER) + +#endif