qemu/hw/core/generic-loader.c
<<
>>
Prefs
   1/*
   2 * Generic Loader
   3 *
   4 * Copyright (C) 2014 Li Guang
   5 * Copyright (C) 2016 Xilinx Inc.
   6 * Written by Li Guang <lig.fnst@cn.fujitsu.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License as published by the
  10 * Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful, but WITHOUT
  14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16 * for more details.
  17 *
  18 */
  19
  20/*
  21 * Internally inside QEMU this is a device. It is a strange device that
  22 * provides no hardware interface but allows QEMU to monkey patch memory
  23 * specified when it is created. To be able to do this it has a reset
  24 * callback that does the memory operations.
  25
  26 * This device allows the user to monkey patch memory. To be able to do
  27 * this it needs a backend to manage the datas, the same as other
  28 * memory-related devices. In this case as the backend is so trivial we
  29 * have merged it with the frontend instead of creating and maintaining a
  30 * separate backend.
  31 */
  32
  33#include "qemu/osdep.h"
  34#include "hw/core/cpu.h"
  35#include "hw/sysbus.h"
  36#include "sysemu/dma.h"
  37#include "sysemu/reset.h"
  38#include "hw/loader.h"
  39#include "hw/qdev-properties.h"
  40#include "qapi/error.h"
  41#include "qemu/module.h"
  42#include "hw/core/generic-loader.h"
  43#include "exec/memory.h"
  44
  45#define CPU_NONE 0xFFFFFFFF
  46
  47static void generic_loader_reset(void *opaque)
  48{
  49    GenericLoaderState *s = GENERIC_LOADER(opaque);
  50
  51    if (s->set_pc) {
  52        CPUClass *cc = CPU_GET_CLASS(s->cpu);
  53        cpu_reset(s->cpu);
  54        if (cc) {
  55            cc->set_pc(s->cpu, s->addr);
  56        }
  57    }
  58
  59    if (s->data_len) {
  60        MemTxAttrs attrs = { .unspecified = 0,
  61                             .secure = 0,
  62                             .user = 0,
  63                             .debug = 0,
  64                             .requester_id = 0,
  65        };
  66
  67        attrs.debug = s->attrs.debug;
  68        attrs.secure = s->attrs.secure;
  69        attrs.requester_id = s->attrs.requester_id;
  70
  71        assert(s->data_len < sizeof(s->data));
  72        address_space_rw(s->cpu->as, s->addr, attrs, (uint8_t *)&s->data,
  73                         s->data_len, true);
  74    }
  75}
  76
  77static void generic_loader_realize(DeviceState *dev, Error **errp)
  78{
  79    GenericLoaderState *s = GENERIC_LOADER(dev);
  80    hwaddr entry;
  81    int big_endian;
  82    int size = 0;
  83
  84    s->set_pc = false;
  85
  86    /* Perform some error checking on the user's options */
  87    if (s->data || s->data_len  || s->data_be) {
  88        /* User is loading memory values */
  89        if (s->file) {
  90            error_setg(errp, "Specifying a file is not supported when loading "
  91                       "memory values");
  92            return;
  93        } else if (s->force_raw) {
  94            error_setg(errp, "Specifying force-raw is not supported when "
  95                       "loading memory values");
  96            return;
  97        } else if (!s->data_len) {
  98            /* We can't check for !data here as a value of 0 is still valid. */
  99            error_setg(errp, "Both data and data-len must be specified");
 100            return;
 101        } else if (s->data_len > 8) {
 102            error_setg(errp, "data-len cannot be greater then 8 bytes");
 103            return;
 104        }
 105    } else if (s->file || s->force_raw)  {
 106        /* User is loading an image */
 107        if (s->data || s->data_len || s->data_be) {
 108            error_setg(errp, "data can not be specified when loading an "
 109                       "image");
 110            return;
 111        }
 112        /* The user specified a file, only set the PC if they also specified
 113         * a CPU to use.
 114         */
 115        if (s->cpu_num != CPU_NONE) {
 116            s->set_pc = true;
 117        }
 118    } else if (s->addr) {
 119        /* User is setting the PC */
 120        if (s->data || s->data_len || s->data_be) {
 121            error_setg(errp, "data can not be specified when setting a "
 122                       "program counter");
 123            return;
 124        } else if (s->cpu_num == CPU_NONE) {
 125            error_setg(errp, "cpu_num must be specified when setting a "
 126                       "program counter");
 127            return;
 128        }
 129        s->set_pc = true;
 130    } else {
 131        /* Did the user specify anything? */
 132        error_setg(errp, "please include valid arguments");
 133        return;
 134    }
 135
 136    qemu_register_reset_loader(generic_loader_reset, dev);
 137
 138    if (s->cpu_num != CPU_NONE) {
 139        s->cpu = qemu_get_cpu(s->cpu_num);
 140        if (!s->cpu) {
 141            error_setg(errp, "Specified boot CPU#%d is nonexistent",
 142                       s->cpu_num);
 143            return;
 144        }
 145    } else {
 146        s->cpu = first_cpu;
 147    }
 148
 149    big_endian = target_words_bigendian();
 150
 151    if (s->file) {
 152        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;
 153
 154        if (!s->force_raw) {
 155            size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
 156                               NULL, big_endian, 0, 0, 0, as);
 157
 158            if (size < 0) {
 159                size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
 160                                      as);
 161            }
 162
 163            if (size < 0) {
 164                size = load_targphys_hex_as(s->file, &entry, as);
 165            }
 166        }
 167
 168        if (size < 0 || s->force_raw) {
 169            /* Default to the maximum size being the machine's ram size */
 170            size = load_image_targphys_as(s->file, s->addr, ram_size, as);
 171        } else {
 172            s->addr = entry;
 173        }
 174
 175        if (size < 0) {
 176            error_setg(errp, "Cannot load specified image %s", s->file);
 177            return;
 178        }
 179    }
 180
 181    /* Convert the data endiannes */
 182    if (s->data_be) {
 183        s->data = cpu_to_be64(s->data);
 184    } else {
 185        s->data = cpu_to_le64(s->data);
 186    }
 187
 188    /* Xilinx: If qdev_hotplug is set then the machine has already been
 189     * created. This means we are hot-plugging a device. We need to forefully
 190     * call the reset function to ensure the operation completes.
 191     */
 192    if (qdev_hotplug) {
 193        generic_loader_reset(dev);
 194    }
 195}
 196
 197static void generic_loader_unrealize(DeviceState *dev)
 198{
 199    qemu_unregister_reset_loader(generic_loader_reset, dev);
 200}
 201
 202static Property generic_loader_props[] = {
 203    DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
 204    DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
 205    DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
 206    DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
 207    DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
 208    DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
 209    DEFINE_PROP_STRING("file", GenericLoaderState, file),
 210    DEFINE_PROP_UINT16("attrs-requester-id", GenericLoaderState,
 211                       attrs.requester_id, 0),
 212    DEFINE_PROP_BOOL("attrs-debug", GenericLoaderState, attrs.debug, false),
 213    DEFINE_PROP_BOOL("attrs-secure", GenericLoaderState, attrs.secure, false),
 214    DEFINE_PROP_END_OF_LIST(),
 215};
 216
 217static void generic_loader_class_init(ObjectClass *klass, void *data)
 218{
 219    DeviceClass *dc = DEVICE_CLASS(klass);
 220
 221    /* The reset function is not registered here and is instead registered in
 222     * the realize function to allow this device to be added via the device_add
 223     * command in the QEMU monitor.
 224     * TODO: Improve the device_add functionality to allow resets to be
 225     * connected
 226     */
 227    dc->realize = generic_loader_realize;
 228    dc->unrealize = generic_loader_unrealize;
 229    device_class_set_props(dc, generic_loader_props);
 230    dc->desc = "Generic Loader";
 231    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 232}
 233
 234static TypeInfo generic_loader_info = {
 235    .name = TYPE_GENERIC_LOADER,
 236    .parent = TYPE_DEVICE,
 237    .instance_size = sizeof(GenericLoaderState),
 238    .class_init = generic_loader_class_init,
 239};
 240
 241static void generic_loader_register_type(void)
 242{
 243    type_register_static(&generic_loader_info);
 244}
 245
 246type_init(generic_loader_register_type)
 247