From patchwork Thu May 12 16:13:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 621663 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r5J6T1CFMz9sDC for ; Fri, 13 May 2016 02:17:21 +1000 (AEST) Received: from localhost ([::1]:58633 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0tIc-0002ba-MP for incoming@patchwork.ozlabs.org; Thu, 12 May 2016 12:17:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51405) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0tI8-0001rJ-6p for qemu-devel@nongnu.org; Thu, 12 May 2016 12:16:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b0tI3-0004g7-NP for qemu-devel@nongnu.org; Thu, 12 May 2016 12:16:48 -0400 Received: from mail-cys01nam02on0071.outbound.protection.outlook.com ([104.47.37.71]:16736 helo=NAM02-CY1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0tI3-0004ft-9r for qemu-devel@nongnu.org; Thu, 12 May 2016 12:16:43 -0400 Received: from BL2NAM02FT060.eop-nam02.prod.protection.outlook.com (10.152.76.59) by BL2NAM02HT100.eop-nam02.prod.protection.outlook.com (10.152.77.34) with Microsoft SMTP Server (TLS) id 15.1.492.8; Thu, 12 May 2016 16:16:41 +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 BL2NAM02FT060.mail.protection.outlook.com (10.152.76.124) with Microsoft SMTP Server (TLS) id 15.1.492.8 via Frontend Transport; Thu, 12 May 2016 16:16:40 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:36263 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1b0tHw-0007cp-8H; Thu, 12 May 2016 09:16:36 -0700 Received: from [127.0.0.1] (port=45103 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1b0tHw-0000Yx-6M; Thu, 12 May 2016 09:16:36 -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 u4CGAK34004744; Thu, 12 May 2016 09:10:20 -0700 Received: from [172.19.74.182] (port=54100 helo=xsjalistai50.xlnx.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1b0tHv-0000Yu-CT; Thu, 12 May 2016 09:16:35 -0700 From: Alistair Francis To: Date: Thu, 12 May 2016 09:13:10 -0700 Message-ID: <0035a7cc48a812b871814b2d2efa70ec40942d20.1463069453.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-22316.005 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 42LRYkClgXHNP79lSamDIlfoC+zAnQtUb99nkFue1b7tvs4F9+3BndfXUhXQ== X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:149.199.60.96; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(1109001)(1110001)(339900001)(199003)(189002)(9170700003)(6806005)(229853001)(5003600100002)(36756003)(92566002)(11100500001)(47776003)(50466002)(5008740100001)(50226002)(8936002)(9786002)(19580405001)(19580395003)(118296001)(5003940100001)(106466001)(85426001)(2906002)(4326007)(77096005)(2351001)(71366001)(110136002)(87936001)(64026002)(48376002)(105606002)(33646002)(586003)(81166006)(76176999)(2950100001)(189998001)(1220700001)(86362001)(50986999)(41533002)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:BL2NAM02HT100; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; MLV:sfv; A:1; MX:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: b1dc0192-e85b-42a5-e2bd-08d37a80cd78 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BL2NAM02HT100; X-Microsoft-Antispam-PRVS: <72fb1643897943bcb2e4ce1d7f946330@BL2NAM02HT100.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)(13023025)(8121501046)(13015025)(13024025)(13017025)(5005006)(13018025)(10201501046)(3002001)(6055026); SRVR:BL2NAM02HT100; BCL:0; PCL:0; RULEID:; SRVR:BL2NAM02HT100; X-Forefront-PRVS: 0940A19703 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 May 2016 16:16:40.7901 (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: BL2NAM02HT100 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.37.71 Subject: [Qemu-devel] [PATCH v5 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, afaerber@suse.de Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Add a generic loader to QEMU which can be used to load images or set memory values. Signed-off-by: Alistair Francis --- 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 | 145 +++++++++++++++++++++++++++++++++++++++ include/hw/core/generic-loader.h | 45 ++++++++++++ 4 files changed, 198 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 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..9e114f4 --- /dev/null +++ b/hw/core/generic-loader.c @@ -0,0 +1,145 @@ +/* + * 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 "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; + + 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