Disable preadv/pwritev support
[qemu-kvm/fedora.git] / hw / virtio-balloon.c
blob7ca783e49edb13d78a03a4cd1a54833ecbd5dec9
1 /*
2 * Virtio Block Device
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "virtio.h"
16 #include "pc.h"
17 #include "sysemu.h"
18 #include "cpu.h"
19 #include "balloon.h"
20 #include "virtio-balloon.h"
21 #include "kvm.h"
22 #include "qemu-kvm.h"
24 #if defined(__linux__)
25 #include <sys/mman.h>
26 #endif
28 typedef struct VirtIOBalloon
30 VirtIODevice vdev;
31 VirtQueue *ivq, *dvq;
32 uint32_t num_pages;
33 uint32_t actual;
34 } VirtIOBalloon;
36 static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
38 return (VirtIOBalloon *)vdev;
41 static void balloon_page(void *addr, int deflate)
43 #if defined(__linux__)
44 if (!kvm_enabled() || kvm_has_sync_mmu())
45 madvise(addr, TARGET_PAGE_SIZE,
46 deflate ? MADV_WILLNEED : MADV_DONTNEED);
47 #endif
50 /* FIXME: once we do a virtio refactoring, this will get subsumed into common
51 * code */
52 static size_t memcpy_from_iovector(void *data, size_t offset, size_t size,
53 struct iovec *iov, int iovlen)
55 int i;
56 uint8_t *ptr = data;
57 size_t iov_off = 0;
58 size_t data_off = 0;
60 for (i = 0; i < iovlen && size; i++) {
61 if (offset < (iov_off + iov[i].iov_len)) {
62 size_t len = MIN((iov_off + iov[i].iov_len) - offset , size);
64 memcpy(ptr + data_off, iov[i].iov_base + (offset - iov_off), len);
66 data_off += len;
67 offset += len;
68 size -= len;
71 iov_off += iov[i].iov_len;
74 return data_off;
77 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
79 VirtIOBalloon *s = to_virtio_balloon(vdev);
80 VirtQueueElement elem;
82 while (virtqueue_pop(vq, &elem)) {
83 size_t offset = 0;
84 uint32_t pfn;
86 while (memcpy_from_iovector(&pfn, offset, 4,
87 elem.out_sg, elem.out_num) == 4) {
88 ram_addr_t pa;
89 ram_addr_t addr;
91 pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
92 offset += 4;
94 addr = cpu_get_physical_page_desc(pa);
95 if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM)
96 continue;
98 /* Using qemu_get_ram_ptr is bending the rules a bit, but
99 should be OK because we only want a single page. */
100 balloon_page(qemu_get_ram_ptr(addr), !!(vq == s->dvq));
103 virtqueue_push(vq, &elem, offset);
104 virtio_notify(vdev, vq);
108 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
110 VirtIOBalloon *dev = to_virtio_balloon(vdev);
111 struct virtio_balloon_config config;
113 config.num_pages = cpu_to_le32(dev->num_pages);
114 config.actual = cpu_to_le32(dev->actual);
116 memcpy(config_data, &config, 8);
119 static void virtio_balloon_set_config(VirtIODevice *vdev,
120 const uint8_t *config_data)
122 VirtIOBalloon *dev = to_virtio_balloon(vdev);
123 struct virtio_balloon_config config;
124 memcpy(&config, config_data, 8);
125 dev->actual = config.actual;
128 static uint32_t virtio_balloon_get_features(VirtIODevice *vdev)
130 return 0;
133 static ram_addr_t virtio_balloon_to_target(void *opaque, ram_addr_t target)
135 VirtIOBalloon *dev = opaque;
137 if (target > ram_size)
138 target = ram_size;
140 if (target) {
141 dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
142 virtio_notify_config(&dev->vdev);
145 return ram_size - (dev->actual << VIRTIO_BALLOON_PFN_SHIFT);
148 static void virtio_balloon_save(QEMUFile *f, void *opaque)
150 VirtIOBalloon *s = opaque;
152 virtio_save(&s->vdev, f);
154 qemu_put_be32(f, s->num_pages);
155 qemu_put_be32(f, s->actual);
158 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
160 VirtIOBalloon *s = opaque;
162 if (version_id != 1)
163 return -EINVAL;
165 virtio_load(&s->vdev, f);
167 s->num_pages = qemu_get_be32(f);
168 s->actual = qemu_get_be32(f);
170 return 0;
173 VirtIODevice *virtio_balloon_init(DeviceState *dev)
175 VirtIOBalloon *s;
177 s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
178 VIRTIO_ID_BALLOON,
179 8, sizeof(VirtIOBalloon));
181 s->vdev.get_config = virtio_balloon_get_config;
182 s->vdev.set_config = virtio_balloon_set_config;
183 s->vdev.get_features = virtio_balloon_get_features;
185 s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
186 s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
188 qemu_add_balloon_handler(virtio_balloon_to_target, s);
190 register_savevm("virtio-balloon", -1, 1, virtio_balloon_save, virtio_balloon_load, s);
192 return &s->vdev;