From patchwork Wed May 25 18:49:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 9136067 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 1C72C60221 for ; Wed, 25 May 2016 18:56:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10D15281D4 for ; Wed, 25 May 2016 18:56:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0585428253; Wed, 25 May 2016 18:56:44 +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 1DB42281D4 for ; Wed, 25 May 2016 18:56:43 +0000 (UTC) Received: from localhost ([::1]:34781 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5dz0-00088Z-AJ for patchwork-qemu-devel@patchwork.kernel.org; Wed, 25 May 2016 14:56:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40333) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5dvC-00058V-O6 for qemu-devel@nongnu.org; Wed, 25 May 2016 14:52:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5dv7-0006lw-Fp for qemu-devel@nongnu.org; Wed, 25 May 2016 14:52:45 -0400 Received: from mail-sn1nam02on0042.outbound.protection.outlook.com ([104.47.36.42]:4800 helo=NAM02-SN1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5dv7-0006lo-4d for qemu-devel@nongnu.org; Wed, 25 May 2016 14:52:41 -0400 Received: from BL2NAM02FT062.eop-nam02.prod.protection.outlook.com (10.152.76.54) by BL2NAM02HT191.eop-nam02.prod.protection.outlook.com (10.152.77.248) with Microsoft SMTP Server (TLS) id 15.1.497.8; Wed, 25 May 2016 18:52:39 +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 BL2NAM02FT062.mail.protection.outlook.com (10.152.77.57) with Microsoft SMTP Server (TLS) id 15.1.497.8 via Frontend Transport; Wed, 25 May 2016 18:52:39 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:60240 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1b5dv4-0000He-7X; Wed, 25 May 2016 11:52:38 -0700 Received: from [127.0.0.1] (port=40848 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1b5dv4-0004Xp-4U; Wed, 25 May 2016 11:52:38 -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 u4PIkHRT014341; Wed, 25 May 2016 11:46:17 -0700 Received: from [172.19.74.182] (port=46508 helo=xsjalistai50.xlnx.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1b5dv3-0004Xm-CZ; Wed, 25 May 2016 11:52:37 -0700 From: Alistair Francis To: Date: Wed, 25 May 2016 11:49:05 -0700 Message-ID: <1d2acebce401b23134d2211d7488df71a9f056b9.1464201427.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-22344.005 X-TM-AS-Result: No--14.016-7.0-31-10 X-imss-scan-details: No--14.016-7.0-31-10 X-TMASE-MatchedRID: x1M1994sXQpYXTxImR5ZvCZm6wdY+F8KM3PBQtDBME+voBtU8+SlKeXK q62Og2nzjYC4p9NNfExlvIjRY+gzqxOkOmo9tETYO9+ygK4Bw/6l9VzHf0qr7plqjRbrcKRBfK8 AMFnqwZhqx609/HMZ7dMyP7mv/gF2i9Om4SO4imTJ1E/nrJFED7EhhZOhPCz3sLnDceAXxSKZAn qxTGmm4tpTvcHSSI8FU3vFXL0jFjApcfVg5rBYdvGBihrnFyBPwx0jRRxcQfPjqg04WykOlWjL8 zGxrxCrwvrUmY0aeTwCZxEGZ8yh0Jya2tEru0x72Hdvv/MGE3WtfY6YPaw3YTzzULHH1XTjYYhe 576ws92FWYX2TPbDEl13lCMXIHsKlYSFi/RRLjcER9Ta+6BEXX4h7mUB3ODznWBUWlAKnBNfnwt 1mX0Th5adOLsReBJpc8wGexlxD6pkwsxe1DtvMWMGiV639iF0Ct59Uh3p/NWSTXLHuU3fHCyW7M C1GOnhoDz6JOyTqkCAMuqetGVetnyef22ep6XYOwBXM346/+wKOcqhtbGcmtVF8sODS4noQsZ85 K16MCPgAQshPdWsq5q6dhtwBPbN X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:149.199.60.96; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(1110001)(1109001)(339900001)(189002)(199003)(9170700003)(47776003)(105606002)(5003940100001)(92566002)(229853001)(64026002)(50226002)(11100500001)(6806005)(2351001)(86362001)(36756003)(9786002)(50466002)(71366001)(106466001)(189998001)(2950100001)(5003600100002)(33646002)(76176999)(2906002)(110136002)(48376002)(118296001)(77096005)(81166006)(87936001)(8676002)(19580395003)(50986999)(4326007)(8936002)(586003)(5008740100001)(1220700001)(85426001)(19580405001)(41533002)(2004002)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:BL2NAM02HT191; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; MLV:sfv; MX:1; A:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 8df95c7a-b5eb-4203-a356-08d384cdbecd X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BL2NAM02HT191; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(8121501046)(13024025)(13017025)(13018025)(13023025)(13015025)(5005006)(3002001)(10201501046)(6055026); SRVR:BL2NAM02HT191; BCL:0; PCL:0; RULEID:; SRVR:BL2NAM02HT191; X-Forefront-PRVS: 09538D3531 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2016 18:52:39.0869 (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: BL2NAM02HT191 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.36.42 Subject: [Qemu-devel] [PATCH v7 2/3] 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: peter.maydell@linaro.org, armbru@redhat.com, alistair.francis@xilinx.com, crosthwaitepeter@gmail.com, cov@codeaurora.org, pbonzini@redhat.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 --- 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 | 170 +++++++++++++++++++++++++++++++++++++++ include/hw/core/generic-loader.h | 45 +++++++++++ 4 files changed, 223 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 81e7fac..b4a77d8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -972,6 +972,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/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 70951d4..eb00e7d 100644 --- a/hw/core/Makefile.objs +++ b/hw/core/Makefile.objs @@ -15,3 +15,5 @@ common-obj-$(CONFIG_SOFTMMU) += null-machine.o common-obj-$(CONFIG_SOFTMMU) += loader.o common-obj-$(CONFIG_SOFTMMU) += qdev-properties-system.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..7160d58 --- /dev/null +++ b/hw/core/generic-loader.c @@ -0,0 +1,170 @@ +/* + * 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 0xFFFF + +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((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; + + /* Perform some eror checking on the users 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 length must be specified."); + 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; + } + } + + 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(s->file, NULL, NULL, &entry, NULL, NULL, + big_endian, 0, 0, 0); + + if (size < 0) { + size = load_uimage(s->file, &entry, NULL, NULL, NULL, NULL); + } + } + + if (size < 0) { + /* Default to the maximum size being the machine's ram size */ + size = load_image_targphys(s->file, s->addr, ram_size); + } 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; + } + + /* 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_UINT16("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); + + 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..ceec51f --- /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; + uint16_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