From patchwork Thu Mar 3 19:42:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 8495881 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DDB92C0553 for ; Thu, 3 Mar 2016 19:46:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F0499201DD for ; Thu, 3 Mar 2016 19:46:52 +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 B0D3920117 for ; Thu, 3 Mar 2016 19:46:51 +0000 (UTC) Received: from localhost ([::1]:37040 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abZD1-0001va-4i for patchwork-qemu-devel@patchwork.kernel.org; Thu, 03 Mar 2016 14:46:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60667) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abZBM-0007zC-5b for qemu-devel@nongnu.org; Thu, 03 Mar 2016 14:45:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1abZBI-0001XD-1g for qemu-devel@nongnu.org; Thu, 03 Mar 2016 14:45:08 -0500 Received: from mail-cys01nam02on0079.outbound.protection.outlook.com ([104.47.37.79]:17852 helo=NAM02-CY1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abZBH-0001X8-ON for qemu-devel@nongnu.org; Thu, 03 Mar 2016 14:45:03 -0500 Received: from SN1NAM02FT008.eop-nam02.prod.protection.outlook.com (10.152.72.57) by SN1NAM02HT042.eop-nam02.prod.protection.outlook.com (10.152.72.229) with Microsoft SMTP Server (TLS) id 15.1.422.5; Thu, 3 Mar 2016 19:45:01 +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 SN1NAM02FT008.mail.protection.outlook.com (10.152.72.119) with Microsoft SMTP Server (TLS) id 15.1.427.7 via Frontend Transport; Thu, 3 Mar 2016 19:45:01 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:39796 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1abZBE-0002OT-QH; Thu, 03 Mar 2016 11:45:00 -0800 Received: from [127.0.0.1] (port=40738 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1abZBE-0003C0-N1; Thu, 03 Mar 2016 11:45:00 -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 u23JdCQi015268; Thu, 3 Mar 2016 11:39:12 -0800 Received: from [172.19.74.182] (port=36982 helo=xsjalistai50.xlnx.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1abZBD-0003Bx-Rx; Thu, 03 Mar 2016 11:44:59 -0800 From: Alistair Francis To: Date: Thu, 3 Mar 2016 11:42:09 -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-22168.006 X-TM-AS-Result: No--11.536-7.0-31-10 X-imss-scan-details: No--11.536-7.0-31-10 X-TMASE-MatchedRID: Yq+di/JQz8lYXTxImR5ZvCZm6wdY+F8KM3PBQtDBME+voBtU8+SlKUmc n2whsCKIweebJ+rKEhxirfDD/q34cV+/XNUi16QsSDkh6bW+bcfA2cNJy/qKUtnu97SXKBPYJ1U 9kuPTnxdFfHnTFRLCoSfZlQLSqHJbEz0V3q2NEuPJ1E/nrJFED7EhhZOhPCz3sLnDceAXxSKZAn qxTGmm4tpTvcHSSI8FU3vFXL0jFjApcfVg5rBYdvGBihrnFyBPwx0jRRxcQfPjqg04WykOlWjL8 zGxrxCrwvrUmY0aeTwCZxEGZ8yh0Jya2tEru0x72Hdvv/MGE3WtfY6YPaw3YTzzULHH1XTjYYhe 576ws92FWYX2TPbDEl13lCMXIHsKlYSFi/RRLjdpR7+L0B6mE1PgO2JKQydYv8D7QPW2jo9NboK gffrvDdmhOXLc8ZbgauCY++1iRsuXC3sMAGu+n54CIKY/Hg3AGdQnQSTrKGPEQdG7H66TyH4gKq 42LRYk7LZh4yr6G1E4nuFg/jwbOP27a7Zg/OQlw9zbpL6X6r9+3BndfXUhXQ== 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)(199003)(189002)(71366001)(229853001)(50226001)(48376002)(50986999)(2351001)(76176999)(50466002)(586003)(1220700001)(2906002)(47776003)(33646002)(106466001)(86362001)(110136002)(5003600100002)(36756003)(105606002)(2950100001)(189998001)(19580405001)(92566002)(64026002)(11100500001)(5003940100001)(6806005)(87936001)(4326007)(1096002)(77096005)(19580395003)(118296001)(5008740100001)(85426001)(81166005)(41533002)(2004002)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1NAM02HT042; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; MLV:sfv; A:1; MX:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: f19e4426-3200-4c94-fe4c-08d3439c4f85 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN1NAM02HT042; X-Microsoft-Antispam-PRVS: <081f87810c2840a9aea32d84be0a3571@SN1NAM02HT042.eop-nam02.prod.protection.outlook.com> X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(13018025)(13017025)(8121501046)(13015025)(13023025)(13024025)(3002001)(10201501046); SRVR:SN1NAM02HT042; BCL:0; PCL:0; RULEID:; SRVR:SN1NAM02HT042; X-Forefront-PRVS: 0870212862 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Mar 2016 19:45:01.3449 (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: SN1NAM02HT042 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.37.79 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 v4 2/3] 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. Signed-off-by: Alistair Francis --- 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 | 144 +++++++++++++++++++++++++++++++++++++++ include/hw/core/generic-loader.h | 45 ++++++++++++ 4 files changed, 197 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 a5853cd..337cc1b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -971,6 +971,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 abb3560..b5d5197 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..1b21934 --- /dev/null +++ b/hw/core/generic-loader.c @@ -0,0 +1,144 @@ +/* + * 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/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; + + 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); + + 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