From patchwork Sat Jul 2 01:07:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 9210659 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 B168560752 for ; Sat, 2 Jul 2016 01:11:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A33D7286C7 for ; Sat, 2 Jul 2016 01:11:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 96DC9286CE; Sat, 2 Jul 2016 01:11: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 A97F3286C7 for ; Sat, 2 Jul 2016 01:11:43 +0000 (UTC) Received: from localhost ([::1]:36522 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ9TC-0000Pp-NW for patchwork-qemu-devel@patchwork.kernel.org; Fri, 01 Jul 2016 21:11:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34435) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ9PF-0004lJ-I6 for qemu-devel@nongnu.org; Fri, 01 Jul 2016 21:07:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bJ9PC-0001vm-CP for qemu-devel@nongnu.org; Fri, 01 Jul 2016 21:07:37 -0400 Received: from mail-cys01nam02on0088.outbound.protection.outlook.com ([104.47.37.88]:8609 helo=NAM02-CY1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ9PC-0001vf-0S for qemu-devel@nongnu.org; Fri, 01 Jul 2016 21:07:34 -0400 Received: from BL2NAM02FT047.eop-nam02.prod.protection.outlook.com (10.152.76.54) by BL2NAM02HT223.eop-nam02.prod.protection.outlook.com (10.152.77.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.534.7; Sat, 2 Jul 2016 01:07:31 +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 BL2NAM02FT047.mail.protection.outlook.com (10.152.77.9) with Microsoft SMTP Server (TLS) id 15.1.523.9 via Frontend Transport; Sat, 2 Jul 2016 01:07:31 +0000 Received: from 172-16-1-203.xilinx.com ([172.16.1.203]:39541 helo=xsj-tvapsmtp02.xilinx.com) by xsj-tvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1bJ9P8-0006cQ-Gy; Fri, 01 Jul 2016 18:07:30 -0700 Received: from [127.0.0.1] (port=48381 helo=tsj-smtp-dlp1.xlnx.xilinx.com) by xsj-tvapsmtp02.xilinx.com with esmtp (Exim 4.63) (envelope-from ) id 1bJ9P8-0003bc-FJ; Fri, 01 Jul 2016 18:07:30 -0700 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 u6210tsN007071; Fri, 1 Jul 2016 18:00:55 -0700 Received: from [172.19.74.182] (port=38978 helo=xsjalistai50.xilinx.com) by xsj-tvapsmtp02 with esmtp (Exim 4.63) (envelope-from ) id 1bJ9P7-0003bZ-Iy; Fri, 01 Jul 2016 18:07:29 -0700 From: Alistair Francis To: , Date: Fri, 1 Jul 2016 18:07:11 -0700 Message-ID: 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-22424.005 X-TM-AS-Result: No--14.220-7.0-31-10 X-imss-scan-details: No--14.220-7.0-31-10 X-TMASE-MatchedRID: 6Sn0SY51qeVYXTxImR5ZvCZm6wdY+F8KM3PBQtDBME+voBtU8+SlKSFt AzREqaec6zrynV+ooQNk50w7YeqSow8LKInWUps8W7gz/Gbgpl4aJDwYgQY/f3q20v2fBg+ystZ tcmEJWfmGeWeG9rR3ojMqW8WukwCxHywp8FQrghWjrlYm3WTU74fsPVs/8Vw6R2YNIFh+clHdx4 tc+6RUGLZc7ovJH6ffoBjmd5ti2OcyNFM0Xa1tqJU7Bltw5qVLj5C1jxP0hbr8lHkwIdGZLAJ93 8nKFPegUeKEUp53SjCV4NsYpivEpOlVfYNOXGqmoiN8YTmq+cvoFXQb3ZgkUdqCxkzSpW/XslW/ Xd1k18fQf0+oXmNTYchjJgLpBM+sGAS4UVZrvjY00dkxYNMRt2f6wD367VgtrmYBEB5nhBy9Hmq +2gxO+QaFoscKg0q0UHN19ZU8rvnw/Fi+yB1av2lHv4vQHqYTU+A7YkpDJ1i/wPtA9baOj01ugq B9+u8N2aE5ctzxluBq4Jj77WJGy5cLewwAa76fngIgpj8eDcAZ1CdBJOsoY8RB0bsfrpPIfiAqr jYtFiTN3pNRMoGxJPHkcBcP5dLDJ9zNaEtDVp9WKa9jYFHm2n7cGd19dSFd 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)(189002)(199003)(8676002)(85426001)(36756003)(356003)(2950100001)(9786002)(305945005)(81166006)(11100500001)(81156014)(64026002)(5003940100001)(6806005)(87936001)(189998001)(47776003)(5001770100001)(92566002)(2906002)(71366001)(86362001)(19580395003)(77096005)(118296001)(8936002)(7696003)(5003600100003)(50466002)(19580405001)(229853001)(50986999)(7846002)(48376002)(4326007)(586003)(33646002)(50226002)(106466001)(105606002)(76176999)(41533002)(2004002)(107986001); DIR:OUT; SFP:1101; SCL:1; SRVR:BL2NAM02HT223; H:xsj-tvapsmtpgw01; FPR:; SPF:Fail; PTR:unknown-60-96.xilinx.com; MX:1; A:1; LANG:en; MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: cb0e759e-8888-4bc7-8c3d-08d3a2153e64 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BL2NAM02HT223; 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)(13017025)(8121501046)(5005006)(13015025)(13018025)(13024025)(13023025)(3002001)(10201501046)(6055026); SRVR:BL2NAM02HT223; BCL:0; PCL:0; RULEID:; SRVR:BL2NAM02HT223; X-Forefront-PRVS: 0991CAB7B3 X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jul 2016 01:07:31.1036 (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: BL2NAM02HT223 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.37.88 Subject: [Qemu-devel] [PATCH v8 4/5] 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 --- 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 | 177 +++++++++++++++++++++++++++++++++++++++ include/hw/core/generic-loader.h | 45 ++++++++++ 4 files changed, 230 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 2ab6e3b..0077e22 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -992,6 +992,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 82a9ef8..ab238fa 100644 --- a/hw/core/Makefile.objs +++ b/hw/core/Makefile.objs @@ -16,3 +16,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..c9b0572 --- /dev/null +++ b/hw/core/generic-loader.c @@ -0,0 +1,177 @@ +/* + * 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 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 length must be specified"); + return; + } else if (s->cpu_num) { + error_setg(errp, "Setting data and a cpu number 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 greate 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(s->file, &entry, NULL, NULL, NULL, NULL); + } + } + + if (size < 0 || s->force_raw) { + /* 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; + } + } + + /* 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); + + 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