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.h (14899B)


      1/*
      2 * Virtio Support
      3 *
      4 * Copyright IBM, Corp. 2007
      5 *
      6 * Authors:
      7 *  Anthony Liguori   <aliguori@us.ibm.com>
      8 *
      9 * This work is licensed under the terms of the GNU GPL, version 2.  See
     10 * the COPYING file in the top-level directory.
     11 *
     12 */
     13
     14#ifndef QEMU_VIRTIO_H
     15#define QEMU_VIRTIO_H
     16
     17#include "exec/memory.h"
     18#include "hw/qdev-core.h"
     19#include "net/net.h"
     20#include "migration/vmstate.h"
     21#include "qemu/event_notifier.h"
     22#include "standard-headers/linux/virtio_config.h"
     23#include "standard-headers/linux/virtio_ring.h"
     24#include "qom/object.h"
     25
     26/* A guest should never accept this.  It implies negotiation is broken. */
     27#define VIRTIO_F_BAD_FEATURE		30
     28
     29#define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \
     30                                (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
     31                                (0x1ULL << VIRTIO_F_ANY_LAYOUT))
     32
     33struct VirtQueue;
     34
     35static inline hwaddr vring_align(hwaddr addr,
     36                                             unsigned long align)
     37{
     38    return QEMU_ALIGN_UP(addr, align);
     39}
     40
     41typedef struct VirtIOFeature {
     42    uint64_t flags;
     43    size_t end;
     44} VirtIOFeature;
     45
     46size_t virtio_feature_get_config_size(const VirtIOFeature *features,
     47                                      uint64_t host_features);
     48
     49typedef struct VirtQueue VirtQueue;
     50
     51#define VIRTQUEUE_MAX_SIZE 1024
     52
     53typedef struct VirtQueueElement
     54{
     55    unsigned int index;
     56    unsigned int len;
     57    unsigned int ndescs;
     58    unsigned int out_num;
     59    unsigned int in_num;
     60    hwaddr *in_addr;
     61    hwaddr *out_addr;
     62    struct iovec *in_sg;
     63    struct iovec *out_sg;
     64} VirtQueueElement;
     65
     66#define VIRTIO_QUEUE_MAX 1024
     67
     68#define VIRTIO_NO_VECTOR 0xffff
     69
     70#define TYPE_VIRTIO_DEVICE "virtio-device"
     71OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE)
     72
     73enum virtio_device_endian {
     74    VIRTIO_DEVICE_ENDIAN_UNKNOWN,
     75    VIRTIO_DEVICE_ENDIAN_LITTLE,
     76    VIRTIO_DEVICE_ENDIAN_BIG,
     77};
     78
     79struct VirtIODevice
     80{
     81    DeviceState parent_obj;
     82    const char *name;
     83    uint8_t status;
     84    uint8_t isr;
     85    uint16_t queue_sel;
     86    uint64_t guest_features;
     87    uint64_t host_features;
     88    uint64_t backend_features;
     89    size_t config_len;
     90    void *config;
     91    uint16_t config_vector;
     92    uint32_t generation;
     93    int nvectors;
     94    VirtQueue *vq;
     95    MemoryListener listener;
     96    uint16_t device_id;
     97    bool vm_running;
     98    bool broken; /* device in invalid state, needs reset */
     99    bool use_disabled_flag; /* allow use of 'disable' flag when needed */
    100    bool disabled; /* device in temporarily disabled state */
    101    bool use_started;
    102    bool started;
    103    bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */
    104    bool disable_legacy_check;
    105    VMChangeStateEntry *vmstate;
    106    char *bus_name;
    107    uint8_t device_endian;
    108    bool use_guest_notifier_mask;
    109    AddressSpace *dma_as;
    110    QLIST_HEAD(, VirtQueue) *vector_queues;
    111};
    112
    113struct VirtioDeviceClass {
    114    /*< private >*/
    115    DeviceClass parent;
    116    /*< public >*/
    117
    118    /* This is what a VirtioDevice must implement */
    119    DeviceRealize realize;
    120    DeviceUnrealize unrealize;
    121    uint64_t (*get_features)(VirtIODevice *vdev,
    122                             uint64_t requested_features,
    123                             Error **errp);
    124    uint64_t (*bad_features)(VirtIODevice *vdev);
    125    void (*set_features)(VirtIODevice *vdev, uint64_t val);
    126    int (*validate_features)(VirtIODevice *vdev);
    127    void (*get_config)(VirtIODevice *vdev, uint8_t *config);
    128    void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
    129    void (*reset)(VirtIODevice *vdev);
    130    void (*set_status)(VirtIODevice *vdev, uint8_t val);
    131    /* For transitional devices, this is a bitmap of features
    132     * that are only exposed on the legacy interface but not
    133     * the modern one.
    134     */
    135    uint64_t legacy_features;
    136    /* Test and clear event pending status.
    137     * Should be called after unmask to avoid losing events.
    138     * If backend does not support masking,
    139     * must check in frontend instead.
    140     */
    141    bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
    142    /* Mask/unmask events from this vq. Any events reported
    143     * while masked will become pending.
    144     * If backend does not support masking,
    145     * must mask in frontend instead.
    146     */
    147    void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
    148    int (*start_ioeventfd)(VirtIODevice *vdev);
    149    void (*stop_ioeventfd)(VirtIODevice *vdev);
    150    /* Saving and loading of a device; trying to deprecate save/load
    151     * use vmsd for new devices.
    152     */
    153    void (*save)(VirtIODevice *vdev, QEMUFile *f);
    154    int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
    155    /* Post load hook in vmsd is called early while device is processed, and
    156     * when VirtIODevice isn't fully initialized.  Devices should use this instead,
    157     * unless they specifically want to verify the migration stream as it's
    158     * processed, e.g. for bounds checking.
    159     */
    160    int (*post_load)(VirtIODevice *vdev);
    161    const VMStateDescription *vmsd;
    162    bool (*primary_unplug_pending)(void *opaque);
    163};
    164
    165void virtio_instance_init_common(Object *proxy_obj, void *data,
    166                                 size_t vdev_size, const char *vdev_name);
    167
    168void virtio_init(VirtIODevice *vdev, const char *name,
    169                         uint16_t device_id, size_t config_size);
    170void virtio_cleanup(VirtIODevice *vdev);
    171
    172void virtio_error(VirtIODevice *vdev, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
    173
    174/* Set the child bus name. */
    175void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name);
    176
    177typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *);
    178typedef bool (*VirtIOHandleAIOOutput)(VirtIODevice *, VirtQueue *);
    179
    180VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
    181                            VirtIOHandleOutput handle_output);
    182
    183void virtio_del_queue(VirtIODevice *vdev, int n);
    184
    185void virtio_delete_queue(VirtQueue *vq);
    186
    187void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
    188                    unsigned int len);
    189void virtqueue_flush(VirtQueue *vq, unsigned int count);
    190void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
    191                              unsigned int len);
    192void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
    193                     unsigned int len);
    194bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
    195void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
    196                    unsigned int len, unsigned int idx);
    197
    198void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
    199void *virtqueue_pop(VirtQueue *vq, size_t sz);
    200unsigned int virtqueue_drop_all(VirtQueue *vq);
    201void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
    202void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
    203                                VirtQueueElement *elem);
    204int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
    205                          unsigned int out_bytes);
    206void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
    207                               unsigned int *out_bytes,
    208                               unsigned max_in_bytes, unsigned max_out_bytes);
    209
    210void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq);
    211void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
    212
    213int virtio_save(VirtIODevice *vdev, QEMUFile *f);
    214
    215extern const VMStateInfo virtio_vmstate_info;
    216
    217#define VMSTATE_VIRTIO_DEVICE \
    218    {                                         \
    219        .name = "virtio",                     \
    220        .info = &virtio_vmstate_info,         \
    221        .flags = VMS_SINGLE,                  \
    222    }
    223
    224int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id);
    225
    226void virtio_notify_config(VirtIODevice *vdev);
    227
    228bool virtio_queue_get_notification(VirtQueue *vq);
    229void virtio_queue_set_notification(VirtQueue *vq, int enable);
    230
    231int virtio_queue_ready(VirtQueue *vq);
    232
    233int virtio_queue_empty(VirtQueue *vq);
    234
    235/* Host binding interface.  */
    236
    237uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
    238uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
    239uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
    240void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
    241void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
    242void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
    243uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr);
    244uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr);
    245uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr);
    246void virtio_config_modern_writeb(VirtIODevice *vdev,
    247                                 uint32_t addr, uint32_t data);
    248void virtio_config_modern_writew(VirtIODevice *vdev,
    249                                 uint32_t addr, uint32_t data);
    250void virtio_config_modern_writel(VirtIODevice *vdev,
    251                                 uint32_t addr, uint32_t data);
    252void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
    253hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
    254void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
    255int virtio_queue_get_num(VirtIODevice *vdev, int n);
    256int virtio_queue_get_max_num(VirtIODevice *vdev, int n);
    257int virtio_get_num_queues(VirtIODevice *vdev);
    258void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
    259                            hwaddr avail, hwaddr used);
    260void virtio_queue_update_rings(VirtIODevice *vdev, int n);
    261void virtio_queue_set_align(VirtIODevice *vdev, int n, int align);
    262void virtio_queue_notify(VirtIODevice *vdev, int n);
    263uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
    264void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
    265int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
    266                                      MemoryRegion *mr, bool assign);
    267int virtio_set_status(VirtIODevice *vdev, uint8_t val);
    268void virtio_reset(void *opaque);
    269void virtio_update_irq(VirtIODevice *vdev);
    270int virtio_set_features(VirtIODevice *vdev, uint64_t val);
    271
    272/* Base devices.  */
    273typedef struct VirtIOBlkConf VirtIOBlkConf;
    274struct virtio_net_conf;
    275typedef struct virtio_serial_conf virtio_serial_conf;
    276typedef struct virtio_input_conf virtio_input_conf;
    277typedef struct VirtIOSCSIConf VirtIOSCSIConf;
    278typedef struct VirtIORNGConf VirtIORNGConf;
    279
    280#define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
    281    DEFINE_PROP_BIT64("indirect_desc", _state, _field,    \
    282                      VIRTIO_RING_F_INDIRECT_DESC, true), \
    283    DEFINE_PROP_BIT64("event_idx", _state, _field,        \
    284                      VIRTIO_RING_F_EVENT_IDX, true),     \
    285    DEFINE_PROP_BIT64("notify_on_empty", _state, _field,  \
    286                      VIRTIO_F_NOTIFY_ON_EMPTY, true), \
    287    DEFINE_PROP_BIT64("any_layout", _state, _field, \
    288                      VIRTIO_F_ANY_LAYOUT, true), \
    289    DEFINE_PROP_BIT64("iommu_platform", _state, _field, \
    290                      VIRTIO_F_IOMMU_PLATFORM, false), \
    291    DEFINE_PROP_BIT64("packed", _state, _field, \
    292                      VIRTIO_F_RING_PACKED, false)
    293
    294hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
    295bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n);
    296bool virtio_queue_enabled(VirtIODevice *vdev, int n);
    297hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
    298hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
    299hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
    300hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
    301hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
    302unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
    303void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
    304                                     unsigned int idx);
    305void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n);
    306void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n);
    307void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
    308VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
    309uint16_t virtio_get_queue_index(VirtQueue *vq);
    310EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
    311void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
    312                                                bool with_irqfd);
    313int virtio_device_start_ioeventfd(VirtIODevice *vdev);
    314int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
    315void virtio_device_release_ioeventfd(VirtIODevice *vdev);
    316bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
    317EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
    318void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
    319void virtio_queue_host_notifier_read(EventNotifier *n);
    320void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
    321                                                VirtIOHandleAIOOutput handle_output);
    322VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
    323VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
    324
    325static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
    326{
    327    assert(fbit < 64);
    328    *features |= (1ULL << fbit);
    329}
    330
    331static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit)
    332{
    333    assert(fbit < 64);
    334    *features &= ~(1ULL << fbit);
    335}
    336
    337static inline bool virtio_has_feature(uint64_t features, unsigned int fbit)
    338{
    339    assert(fbit < 64);
    340    return !!(features & (1ULL << fbit));
    341}
    342
    343static inline bool virtio_vdev_has_feature(VirtIODevice *vdev,
    344                                           unsigned int fbit)
    345{
    346    return virtio_has_feature(vdev->guest_features, fbit);
    347}
    348
    349static inline bool virtio_host_has_feature(VirtIODevice *vdev,
    350                                           unsigned int fbit)
    351{
    352    return virtio_has_feature(vdev->host_features, fbit);
    353}
    354
    355static inline bool virtio_is_big_endian(VirtIODevice *vdev)
    356{
    357    if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
    358        assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
    359        return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
    360    }
    361    /* Devices conforming to VIRTIO 1.0 or later are always LE. */
    362    return false;
    363}
    364
    365static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status)
    366{
    367    if (vdev->use_started) {
    368        return vdev->started;
    369    }
    370
    371    return status & VIRTIO_CONFIG_S_DRIVER_OK;
    372}
    373
    374static inline void virtio_set_started(VirtIODevice *vdev, bool started)
    375{
    376    if (started) {
    377        vdev->start_on_kick = false;
    378    }
    379
    380    if (vdev->use_started) {
    381        vdev->started = started;
    382    }
    383}
    384
    385static inline void virtio_set_disabled(VirtIODevice *vdev, bool disable)
    386{
    387    if (vdev->use_disabled_flag) {
    388        vdev->disabled = disable;
    389    }
    390}
    391
    392static inline bool virtio_device_disabled(VirtIODevice *vdev)
    393{
    394    return unlikely(vdev->disabled || vdev->broken);
    395}
    396
    397bool virtio_legacy_allowed(VirtIODevice *vdev);
    398bool virtio_legacy_check_disabled(VirtIODevice *vdev);
    399
    400#endif