cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

virtio-pmem.c (6044B)


      1/*
      2 * Virtio PMEM device
      3 *
      4 * Copyright (C) 2018-2019 Red Hat, Inc.
      5 *
      6 * Authors:
      7 *  Pankaj Gupta <pagupta@redhat.com>
      8 *  David Hildenbrand <david@redhat.com>
      9 *
     10 * This work is licensed under the terms of the GNU GPL, version 2.
     11 * See the COPYING file in the top-level directory.
     12 */
     13
     14#include "qemu/osdep.h"
     15#include "qapi/error.h"
     16#include "qemu-common.h"
     17#include "qemu/error-report.h"
     18#include "qemu/main-loop.h"
     19#include "hw/virtio/virtio-pmem.h"
     20#include "hw/qdev-properties.h"
     21#include "hw/virtio/virtio-access.h"
     22#include "standard-headers/linux/virtio_ids.h"
     23#include "standard-headers/linux/virtio_pmem.h"
     24#include "sysemu/hostmem.h"
     25#include "block/aio.h"
     26#include "block/thread-pool.h"
     27#include "trace.h"
     28
     29typedef struct VirtIODeviceRequest {
     30    VirtQueueElement elem;
     31    int fd;
     32    VirtIOPMEM *pmem;
     33    VirtIODevice *vdev;
     34    struct virtio_pmem_req req;
     35    struct virtio_pmem_resp resp;
     36} VirtIODeviceRequest;
     37
     38static int worker_cb(void *opaque)
     39{
     40    VirtIODeviceRequest *req_data = opaque;
     41    int err = 0;
     42
     43    /* flush raw backing image */
     44    err = fsync(req_data->fd);
     45    trace_virtio_pmem_flush_done(err);
     46    if (err != 0) {
     47        err = 1;
     48    }
     49
     50    virtio_stl_p(req_data->vdev, &req_data->resp.ret, err);
     51
     52    return 0;
     53}
     54
     55static void done_cb(void *opaque, int ret)
     56{
     57    VirtIODeviceRequest *req_data = opaque;
     58    int len = iov_from_buf(req_data->elem.in_sg, req_data->elem.in_num, 0,
     59                              &req_data->resp, sizeof(struct virtio_pmem_resp));
     60
     61    /* Callbacks are serialized, so no need to use atomic ops. */
     62    virtqueue_push(req_data->pmem->rq_vq, &req_data->elem, len);
     63    virtio_notify((VirtIODevice *)req_data->pmem, req_data->pmem->rq_vq);
     64    trace_virtio_pmem_response();
     65    g_free(req_data);
     66}
     67
     68static void virtio_pmem_flush(VirtIODevice *vdev, VirtQueue *vq)
     69{
     70    VirtIODeviceRequest *req_data;
     71    VirtIOPMEM *pmem = VIRTIO_PMEM(vdev);
     72    HostMemoryBackend *backend = MEMORY_BACKEND(pmem->memdev);
     73    ThreadPool *pool = aio_get_thread_pool(qemu_get_aio_context());
     74
     75    trace_virtio_pmem_flush_request();
     76    req_data = virtqueue_pop(vq, sizeof(VirtIODeviceRequest));
     77    if (!req_data) {
     78        virtio_error(vdev, "virtio-pmem missing request data");
     79        return;
     80    }
     81
     82    if (req_data->elem.out_num < 1 || req_data->elem.in_num < 1) {
     83        virtio_error(vdev, "virtio-pmem request not proper");
     84        virtqueue_detach_element(vq, (VirtQueueElement *)req_data, 0);
     85        g_free(req_data);
     86        return;
     87    }
     88    req_data->fd   = memory_region_get_fd(&backend->mr);
     89    req_data->pmem = pmem;
     90    req_data->vdev = vdev;
     91    thread_pool_submit_aio(pool, worker_cb, req_data, done_cb, req_data);
     92}
     93
     94static void virtio_pmem_get_config(VirtIODevice *vdev, uint8_t *config)
     95{
     96    VirtIOPMEM *pmem = VIRTIO_PMEM(vdev);
     97    struct virtio_pmem_config *pmemcfg = (struct virtio_pmem_config *) config;
     98
     99    virtio_stq_p(vdev, &pmemcfg->start, pmem->start);
    100    virtio_stq_p(vdev, &pmemcfg->size, memory_region_size(&pmem->memdev->mr));
    101}
    102
    103static uint64_t virtio_pmem_get_features(VirtIODevice *vdev, uint64_t features,
    104                                        Error **errp)
    105{
    106    return features;
    107}
    108
    109static void virtio_pmem_realize(DeviceState *dev, Error **errp)
    110{
    111    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    112    VirtIOPMEM *pmem = VIRTIO_PMEM(dev);
    113
    114    if (!pmem->memdev) {
    115        error_setg(errp, "virtio-pmem memdev not set");
    116        return;
    117    }
    118
    119    if (host_memory_backend_is_mapped(pmem->memdev)) {
    120        error_setg(errp, "can't use already busy memdev: %s",
    121                   object_get_canonical_path_component(OBJECT(pmem->memdev)));
    122        return;
    123    }
    124
    125    host_memory_backend_set_mapped(pmem->memdev, true);
    126    virtio_init(vdev, TYPE_VIRTIO_PMEM, VIRTIO_ID_PMEM,
    127                sizeof(struct virtio_pmem_config));
    128    pmem->rq_vq = virtio_add_queue(vdev, 128, virtio_pmem_flush);
    129}
    130
    131static void virtio_pmem_unrealize(DeviceState *dev)
    132{
    133    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    134    VirtIOPMEM *pmem = VIRTIO_PMEM(dev);
    135
    136    host_memory_backend_set_mapped(pmem->memdev, false);
    137    virtio_delete_queue(pmem->rq_vq);
    138    virtio_cleanup(vdev);
    139}
    140
    141static void virtio_pmem_fill_device_info(const VirtIOPMEM *pmem,
    142                                         VirtioPMEMDeviceInfo *vi)
    143{
    144    vi->memaddr = pmem->start;
    145    vi->size    = memory_region_size(&pmem->memdev->mr);
    146    vi->memdev  = object_get_canonical_path(OBJECT(pmem->memdev));
    147}
    148
    149static MemoryRegion *virtio_pmem_get_memory_region(VirtIOPMEM *pmem,
    150                                                   Error **errp)
    151{
    152    if (!pmem->memdev) {
    153        error_setg(errp, "'%s' property must be set", VIRTIO_PMEM_MEMDEV_PROP);
    154        return NULL;
    155    }
    156
    157    return &pmem->memdev->mr;
    158}
    159
    160static Property virtio_pmem_properties[] = {
    161    DEFINE_PROP_UINT64(VIRTIO_PMEM_ADDR_PROP, VirtIOPMEM, start, 0),
    162    DEFINE_PROP_LINK(VIRTIO_PMEM_MEMDEV_PROP, VirtIOPMEM, memdev,
    163                     TYPE_MEMORY_BACKEND, HostMemoryBackend *),
    164    DEFINE_PROP_END_OF_LIST(),
    165};
    166
    167static void virtio_pmem_class_init(ObjectClass *klass, void *data)
    168{
    169    DeviceClass *dc = DEVICE_CLASS(klass);
    170    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
    171    VirtIOPMEMClass *vpc = VIRTIO_PMEM_CLASS(klass);
    172
    173    device_class_set_props(dc, virtio_pmem_properties);
    174
    175    vdc->realize = virtio_pmem_realize;
    176    vdc->unrealize = virtio_pmem_unrealize;
    177    vdc->get_config = virtio_pmem_get_config;
    178    vdc->get_features = virtio_pmem_get_features;
    179
    180    vpc->fill_device_info = virtio_pmem_fill_device_info;
    181    vpc->get_memory_region = virtio_pmem_get_memory_region;
    182    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
    183}
    184
    185static TypeInfo virtio_pmem_info = {
    186    .name          = TYPE_VIRTIO_PMEM,
    187    .parent        = TYPE_VIRTIO_DEVICE,
    188    .class_size    = sizeof(VirtIOPMEMClass),
    189    .class_init    = virtio_pmem_class_init,
    190    .instance_size = sizeof(VirtIOPMEM),
    191};
    192
    193static void virtio_register_types(void)
    194{
    195    type_register_static(&virtio_pmem_info);
    196}
    197
    198type_init(virtio_register_types)