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 "qom/object_interfaces.h"
  35#include "hw/core/cpu.h"
  36#include "sysemu/dma.h"
  37#include "sysemu/reset.h"
  38#include "hw/boards.h"
  39#include "hw/loader.h"
  40#include "hw/qdev-properties.h"
  41#include "qapi/error.h"
  42#include "qemu/module.h"
  43#include "hw/core/generic-loader.h"
  44#include "exec/memory.h"
  45
  46#define CPU_NONE 0xFFFFFFFF
  47
  48static void generic_loader_reset(void *opaque)
  49{
  50    GenericLoaderState *s = GENERIC_LOADER(opaque);
  51
  52    if (s->set_pc) {
  53        CPUClass *cc = CPU_GET_CLASS(s->cpu);
  54        cpu_reset(s->cpu);
  55        if (cc) {
  56            cc->set_pc(s->cpu, s->addr);
  57        }
  58    }
  59
  60    if (s->data_len) {
  61        MemTxAttrs attrs = { .unspecified = 0,
  62                             .secure = 0,
  63                             .user = 0,
  64                             .debug = 0,
  65                             .requester_id = 0,
  66        };
  67
  68        attrs.debug = s->attrs.debug;
  69        attrs.secure = s->attrs.secure;
  70        attrs.requester_id = s->attrs.requester_id;
  71
  72        assert(s->data_len <= sizeof(s->data));
  73        address_space_rw(s->cpu->as, s->addr, attrs, (uint8_t *)&s->data,
  74                         s->data_len, true);
  75    }
  76}
  77
  78static void generic_loader_realize(DeviceState *dev, Error **errp)
  79{
  80    GenericLoaderState *s = GENERIC_LOADER(dev);
  81    hwaddr entry;
  82    int big_endian;
  83    ssize_t size = 0;
  84
  85    s->set_pc = false;
  86
  87    /* Perform some error checking on the user's options */
  88    if (s->data || s->data_len  || s->data_be) {
  89        /* User is loading memory values */
  90        if (s->file) {
  91            error_setg(errp, "Specifying a file is not supported when loading "
  92                       "memory values");
  93            return;
  94        } else if (s->force_raw) {
  95            error_setg(errp, "Specifying force-raw is not supported when "
  96                       "loading memory values");
  97            return;
  98        } else if (!s->data_len) {
  99            /* We can't check for !data here as a value of 0 is still valid. */
 100            error_setg(errp, "Both data and data-len must be specified");
 101            return;
 102        } else if (s->data_len > 8) {
 103            error_setg(errp, "data-len cannot be greater then 8 bytes");
 104            return;
 105        }
 106    } else if (s->file || s->force_raw)  {
 107        /* User is loading an image */
 108        if (s->data || s->data_len || s->data_be) {
 109            error_setg(errp, "data can not be specified when loading an "
 110                       "image");
 111            return;
 112        }
 113        /* The user specified a file, only set the PC if they also specified
 114         * a CPU to use.
 115         */
 116        if (s->cpu_num != CPU_NONE) {
 117            s->set_pc = true;
 118        }
 119    } else if (s->addr) {
 120        /* User is setting the PC */
 121        if (s->data || s->data_len || s->data_be) {
 122            error_setg(errp, "data can not be specified when setting a "
 123                       "program counter");
 124            return;
 125        } else if (s->cpu_num == CPU_NONE) {
 126            error_setg(errp, "cpu_num must be specified when setting a "
 127                       "program counter");
 128            return;
 129        }
 130        s->set_pc = true;
 131    } else {
 132        /* Did the user specify anything? */
 133        error_setg(errp, "please include valid arguments");
 134        return;
 135    }
 136
 137    qemu_register_reset_loader(generic_loader_reset, dev);
 138
 139    if (s->cpu_num != CPU_NONE) {
 140        s->cpu = qemu_get_cpu(s->cpu_num);
 141        if (!s->cpu) {
 142            error_setg(errp, "Specified boot CPU#%d is nonexistent",
 143                       s->cpu_num);
 144            return;
 145        }
 146    } else {
 147        s->cpu = first_cpu;
 148    }
 149
 150    big_endian = target_words_bigendian();
 151
 152    if (s->file) {
 153        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;
 154
 155        if (!s->force_raw) {
 156            size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
 157                               NULL, big_endian, 0, 0, 0, as);
 158
 159            if (size < 0) {
 160                size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
 161                                      as);
 162            }
 163
 164            if (size < 0) {
 165                size = load_targphys_hex_as(s->file, &entry, as);
 166            }
 167        }
 168
 169        if (size < 0 || s->force_raw) {
 170            /* Default to the maximum size being the machine's ram size */
 171            size = load_image_targphys_as(s->file, s->addr, current_machine->ram_size, as);
 172        } else {
 173            s->addr = entry;
 174        }
 175
 176        if (size < 0) {
 177            error_setg(errp, "Cannot load specified image %s", s->file);
 178            return;
 179        }
 180    }
 181
 182    /* Convert the data endiannes */
 183    if (s->data_be) {
 184        s->data = cpu_to_be64(s->data);
 185    } else {
 186        s->data = cpu_to_le64(s->data);
 187    }
 188
 189    /* Xilinx: If qdev_hotplug is set then the machine has already been
 190     * created. This means we are hot-plugging a device. We need to forefully
 191     * call the reset function to ensure the operation completes.
 192     */
 193    if (phase_check(PHASE_MACHINE_READY)) {
 194        generic_loader_reset(dev);
 195    }
 196}
 197
 198static void generic_loader_unrealize(DeviceState *dev)
 199{
 200    qemu_unregister_reset_loader(generic_loader_reset, dev);
 201}
 202
 203static Property generic_loader_props[] = {
 204    DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
 205    DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
 206    DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
 207    DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
 208    DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
 209    DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
 210    DEFINE_PROP_STRING("file", GenericLoaderState, file),
 211    DEFINE_PROP_UINT16("attrs-requester-id", GenericLoaderState,
 212                       attrs.requester_id, 0),
 213    DEFINE_PROP_BOOL("attrs-debug", GenericLoaderState, attrs.debug, false),
 214    DEFINE_PROP_BOOL("attrs-secure", GenericLoaderState, attrs.secure, false),
 215    DEFINE_PROP_END_OF_LIST(),
 216};
 217
 218static void generic_loader_class_init(ObjectClass *klass, void *data)
 219{
 220    DeviceClass *dc = DEVICE_CLASS(klass);
 221
 222    /* The reset function is not registered here and is instead registered in
 223     * the realize function to allow this device to be added via the device_add
 224     * command in the QEMU monitor.
 225     * TODO: Improve the device_add functionality to allow resets to be
 226     * connected
 227     */
 228    dc->realize = generic_loader_realize;
 229    dc->unrealize = generic_loader_unrealize;
 230    device_class_set_props(dc, generic_loader_props);
 231    dc->desc = "Generic Loader";
 232    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 233}
 234
 235static const TypeInfo generic_loader_info = {
 236    .name = TYPE_GENERIC_LOADER,
 237    .parent = TYPE_DEVICE,
 238    .instance_size = sizeof(GenericLoaderState),
 239    .class_init = generic_loader_class_init,
 240};
 241
 242static void generic_loader_register_type(void)
 243{
 244    type_register_static(&generic_loader_info);
 245}
 246
 247type_init(generic_loader_register_type)
 248