]> git.proxmox.com Git - qemu.git/blame - hw/virtio-balloon.c
PPC: Fix TLB invalidation bug within the PPC interrupt handler.
[qemu.git] / hw / virtio-balloon.c
CommitLineData
bd322087 1/*
d4443cb6 2 * Virtio Balloon Device
bd322087
AL
3 *
4 * Copyright IBM, Corp. 2008
d4443cb6
AS
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
bd322087
AL
7 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
14 */
15
fa6111f2 16#include "iov.h"
bd322087
AL
17#include "qemu-common.h"
18#include "virtio.h"
19#include "pc.h"
bd322087
AL
20#include "cpu.h"
21#include "balloon.h"
22#include "virtio-balloon.h"
23#include "kvm.h"
b7c28c74 24#include "exec-memory.h"
bd322087
AL
25
26#if defined(__linux__)
27#include <sys/mman.h>
28#endif
29
30typedef struct VirtIOBalloon
31{
32 VirtIODevice vdev;
625a5bef 33 VirtQueue *ivq, *dvq, *svq;
bd322087
AL
34 uint32_t num_pages;
35 uint32_t actual;
625a5bef
AL
36 uint64_t stats[VIRTIO_BALLOON_S_NR];
37 VirtQueueElement stats_vq_elem;
38 size_t stats_vq_offset;
ac720400 39 DeviceState *qdev;
bd322087
AL
40} VirtIOBalloon;
41
42static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
43{
44 return (VirtIOBalloon *)vdev;
45}
46
47static void balloon_page(void *addr, int deflate)
48{
49#if defined(__linux__)
50 if (!kvm_enabled() || kvm_has_sync_mmu())
e78815a5
AF
51 qemu_madvise(addr, TARGET_PAGE_SIZE,
52 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
bd322087
AL
53#endif
54}
55
625a5bef
AL
56/*
57 * reset_stats - Mark all items in the stats array as unset
58 *
59 * This function needs to be called at device intialization and before
60 * before updating to a set of newly-generated stats. This will ensure that no
61 * stale values stick around in case the guest reports a subset of the supported
62 * statistics.
63 */
64static inline void reset_stats(VirtIOBalloon *dev)
65{
66 int i;
67 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
68}
69
bd322087
AL
70static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
71{
72 VirtIOBalloon *s = to_virtio_balloon(vdev);
73 VirtQueueElement elem;
b7c28c74 74 MemoryRegionSection section;
bd322087
AL
75
76 while (virtqueue_pop(vq, &elem)) {
77 size_t offset = 0;
78 uint32_t pfn;
79
fa6111f2 80 while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) {
c227f099
AL
81 ram_addr_t pa;
82 ram_addr_t addr;
bd322087 83
c227f099 84 pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
bd322087
AL
85 offset += 4;
86
b7c28c74
AK
87 /* FIXME: remove get_system_memory(), but how? */
88 section = memory_region_find(get_system_memory(), pa, 1);
89 if (!section.size || !memory_region_is_ram(section.mr))
bd322087
AL
90 continue;
91
b7c28c74 92 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
5c130f65 93 should be OK because we only want a single page. */
b7c28c74
AK
94 addr = section.offset_within_region;
95 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
96 !!(vq == s->dvq));
bd322087
AL
97 }
98
99 virtqueue_push(vq, &elem, offset);
100 virtio_notify(vdev, vq);
101 }
102}
103
625a5bef
AL
104static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
105{
106 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
107 VirtQueueElement *elem = &s->stats_vq_elem;
108 VirtIOBalloonStat stat;
109 size_t offset = 0;
110
111 if (!virtqueue_pop(vq, elem)) {
112 return;
113 }
114
115 /* Initialize the stats to get rid of any stale values. This is only
116 * needed to handle the case where a guest supports fewer stats than it
117 * used to (ie. it has booted into an old kernel).
118 */
119 reset_stats(s);
120
fa6111f2
AS
121 while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat))
122 == sizeof(stat)) {
625a5bef
AL
123 uint16_t tag = tswap16(stat.tag);
124 uint64_t val = tswap64(stat.val);
125
126 offset += sizeof(stat);
127 if (tag < VIRTIO_BALLOON_S_NR)
128 s->stats[tag] = val;
129 }
130 s->stats_vq_offset = offset;
625a5bef
AL
131}
132
bd322087
AL
133static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
134{
135 VirtIOBalloon *dev = to_virtio_balloon(vdev);
136 struct virtio_balloon_config config;
137
138 config.num_pages = cpu_to_le32(dev->num_pages);
139 config.actual = cpu_to_le32(dev->actual);
140
141 memcpy(config_data, &config, 8);
142}
143
144static void virtio_balloon_set_config(VirtIODevice *vdev,
145 const uint8_t *config_data)
146{
147 VirtIOBalloon *dev = to_virtio_balloon(vdev);
148 struct virtio_balloon_config config;
149 memcpy(&config, config_data, 8);
e54f1771 150 dev->actual = le32_to_cpu(config.actual);
bd322087
AL
151}
152
8172539d 153static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
bd322087 154{
625a5bef 155 f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
8172539d 156 return f;
bd322087
AL
157}
158
96637bcd 159static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
dce911c7
AS
160{
161 VirtIOBalloon *dev = opaque;
162
96637bcd
LC
163#if 0
164 /* Disable guest-provided stats for now. For more details please check:
165 * https://bugzilla.redhat.com/show_bug.cgi?id=623903
166 *
167 * If you do enable it (which is probably not going to happen as we
168 * need a new command for it), remember that you also need to fill the
169 * appropriate members of the BalloonInfo structure so that the stats
170 * are returned to the client.
dce911c7 171 */
96637bcd 172 if (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ)) {
dce911c7
AS
173 virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset);
174 virtio_notify(&dev->vdev, dev->svq);
175 return;
176 }
96637bcd 177#endif
dce911c7
AS
178
179 /* Stats are not supported. Clear out any stale values that might
180 * have been set by a more featureful guest kernel.
181 */
182 reset_stats(dev);
96637bcd
LC
183
184 info->actual = ram_size - ((uint64_t) dev->actual <<
185 VIRTIO_BALLOON_PFN_SHIFT);
dce911c7
AS
186}
187
30fb2ca6 188static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
bd322087
AL
189{
190 VirtIOBalloon *dev = opaque;
191
dce911c7 192 if (target > ram_size) {
bd322087 193 target = ram_size;
dce911c7 194 }
bd322087
AL
195 if (target) {
196 dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
197 virtio_notify_config(&dev->vdev);
198 }
bd322087
AL
199}
200
201static void virtio_balloon_save(QEMUFile *f, void *opaque)
202{
203 VirtIOBalloon *s = opaque;
204
205 virtio_save(&s->vdev, f);
206
207 qemu_put_be32(f, s->num_pages);
208 qemu_put_be32(f, s->actual);
209}
210
211static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
212{
213 VirtIOBalloon *s = opaque;
214
215 if (version_id != 1)
216 return -EINVAL;
217
218 virtio_load(&s->vdev, f);
219
220 s->num_pages = qemu_get_be32(f);
221 s->actual = qemu_get_be32(f);
bd322087
AL
222 return 0;
223}
224
53c25cea 225VirtIODevice *virtio_balloon_init(DeviceState *dev)
bd322087
AL
226{
227 VirtIOBalloon *s;
f76f6655 228 int ret;
bd322087 229
53c25cea
PB
230 s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
231 VIRTIO_ID_BALLOON,
232 8, sizeof(VirtIOBalloon));
bd322087
AL
233
234 s->vdev.get_config = virtio_balloon_get_config;
235 s->vdev.set_config = virtio_balloon_set_config;
236 s->vdev.get_features = virtio_balloon_get_features;
237
f76f6655
AS
238 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
239 virtio_balloon_stat, s);
240 if (ret < 0) {
241 virtio_cleanup(&s->vdev);
242 return NULL;
243 }
244
bd322087
AL
245 s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
246 s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
625a5bef 247 s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
bd322087 248
625a5bef 249 reset_stats(s);
bd322087 250
ac720400 251 s->qdev = dev;
0be71e32
AW
252 register_savevm(dev, "virtio-balloon", -1, 1,
253 virtio_balloon_save, virtio_balloon_load, s);
bd322087 254
53c25cea 255 return &s->vdev;
bd322087 256}
855d7e25
AS
257
258void virtio_balloon_exit(VirtIODevice *vdev)
259{
ac720400 260 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
3b86b13a
AS
261
262 qemu_remove_balloon_handler(s);
ac720400 263 unregister_savevm(s->qdev, "virtio-balloon", s);
855d7e25
AS
264 virtio_cleanup(vdev);
265}