x86/cpuid: Fix crash on -cpu ""
[qemu/opensuse.git] / hw / virtio-balloon.c
blobe24a2bf1f36e74722e1f07f65ad8560cc9900354
1 /*
2 * Virtio Balloon Device
4 * Copyright IBM, Corp. 2008
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include "iov.h"
17 #include "qemu-common.h"
18 #include "virtio.h"
19 #include "pc.h"
20 #include "cpu.h"
21 #include "balloon.h"
22 #include "virtio-balloon.h"
23 #include "kvm.h"
25 #if defined(__linux__)
26 #include <sys/mman.h>
27 #endif
29 typedef struct VirtIOBalloon
31 VirtIODevice vdev;
32 VirtQueue *ivq, *dvq, *svq;
33 uint32_t num_pages;
34 uint32_t actual;
35 uint64_t stats[VIRTIO_BALLOON_S_NR];
36 VirtQueueElement stats_vq_elem;
37 size_t stats_vq_offset;
38 DeviceState *qdev;
39 } VirtIOBalloon;
41 static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
43 return (VirtIOBalloon *)vdev;
46 static void balloon_page(void *addr, int deflate)
48 #if defined(__linux__)
49 if (!kvm_enabled() || kvm_has_sync_mmu())
50 qemu_madvise(addr, TARGET_PAGE_SIZE,
51 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
52 #endif
56 * reset_stats - Mark all items in the stats array as unset
58 * This function needs to be called at device intialization and before
59 * before updating to a set of newly-generated stats. This will ensure that no
60 * stale values stick around in case the guest reports a subset of the supported
61 * statistics.
63 static inline void reset_stats(VirtIOBalloon *dev)
65 int i;
66 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
69 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
71 VirtIOBalloon *s = to_virtio_balloon(vdev);
72 VirtQueueElement elem;
74 while (virtqueue_pop(vq, &elem)) {
75 size_t offset = 0;
76 uint32_t pfn;
78 while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) {
79 ram_addr_t pa;
80 ram_addr_t addr;
82 pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
83 offset += 4;
85 addr = cpu_get_physical_page_desc(pa);
86 if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM)
87 continue;
89 /* Using qemu_get_ram_ptr is bending the rules a bit, but
90 should be OK because we only want a single page. */
91 balloon_page(qemu_get_ram_ptr(addr), !!(vq == s->dvq));
94 virtqueue_push(vq, &elem, offset);
95 virtio_notify(vdev, vq);
99 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
101 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
102 VirtQueueElement *elem = &s->stats_vq_elem;
103 VirtIOBalloonStat stat;
104 size_t offset = 0;
106 if (!virtqueue_pop(vq, elem)) {
107 return;
110 /* Initialize the stats to get rid of any stale values. This is only
111 * needed to handle the case where a guest supports fewer stats than it
112 * used to (ie. it has booted into an old kernel).
114 reset_stats(s);
116 while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat))
117 == sizeof(stat)) {
118 uint16_t tag = tswap16(stat.tag);
119 uint64_t val = tswap64(stat.val);
121 offset += sizeof(stat);
122 if (tag < VIRTIO_BALLOON_S_NR)
123 s->stats[tag] = val;
125 s->stats_vq_offset = offset;
128 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
130 VirtIOBalloon *dev = to_virtio_balloon(vdev);
131 struct virtio_balloon_config config;
133 config.num_pages = cpu_to_le32(dev->num_pages);
134 config.actual = cpu_to_le32(dev->actual);
136 memcpy(config_data, &config, 8);
139 static void virtio_balloon_set_config(VirtIODevice *vdev,
140 const uint8_t *config_data)
142 VirtIOBalloon *dev = to_virtio_balloon(vdev);
143 struct virtio_balloon_config config;
144 memcpy(&config, config_data, 8);
145 dev->actual = le32_to_cpu(config.actual);
148 static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
150 f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
151 return f;
154 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
156 VirtIOBalloon *dev = opaque;
158 #if 0
159 /* Disable guest-provided stats for now. For more details please check:
160 * https://bugzilla.redhat.com/show_bug.cgi?id=623903
162 * If you do enable it (which is probably not going to happen as we
163 * need a new command for it), remember that you also need to fill the
164 * appropriate members of the BalloonInfo structure so that the stats
165 * are returned to the client.
167 if (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ)) {
168 virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset);
169 virtio_notify(&dev->vdev, dev->svq);
170 return;
172 #endif
174 /* Stats are not supported. Clear out any stale values that might
175 * have been set by a more featureful guest kernel.
177 reset_stats(dev);
179 info->actual = ram_size - ((uint64_t) dev->actual <<
180 VIRTIO_BALLOON_PFN_SHIFT);
183 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
185 VirtIOBalloon *dev = opaque;
187 if (target > ram_size) {
188 target = ram_size;
190 if (target) {
191 dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
192 virtio_notify_config(&dev->vdev);
196 static void virtio_balloon_save(QEMUFile *f, void *opaque)
198 VirtIOBalloon *s = opaque;
200 virtio_save(&s->vdev, f);
202 qemu_put_be32(f, s->num_pages);
203 qemu_put_be32(f, s->actual);
206 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
208 VirtIOBalloon *s = opaque;
210 if (version_id != 1)
211 return -EINVAL;
213 virtio_load(&s->vdev, f);
215 s->num_pages = qemu_get_be32(f);
216 s->actual = qemu_get_be32(f);
217 return 0;
220 VirtIODevice *virtio_balloon_init(DeviceState *dev)
222 VirtIOBalloon *s;
223 int ret;
225 s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
226 VIRTIO_ID_BALLOON,
227 8, sizeof(VirtIOBalloon));
229 s->vdev.get_config = virtio_balloon_get_config;
230 s->vdev.set_config = virtio_balloon_set_config;
231 s->vdev.get_features = virtio_balloon_get_features;
233 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
234 virtio_balloon_stat, s);
235 if (ret < 0) {
236 virtio_cleanup(&s->vdev);
237 return NULL;
240 s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
241 s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
242 s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
244 reset_stats(s);
246 s->qdev = dev;
247 register_savevm(dev, "virtio-balloon", -1, 1,
248 virtio_balloon_save, virtio_balloon_load, s);
250 return &s->vdev;
253 void virtio_balloon_exit(VirtIODevice *vdev)
255 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
257 qemu_remove_balloon_handler(s);
258 unregister_savevm(s->qdev, "virtio-balloon", s);
259 virtio_cleanup(vdev);