4 * Copyright IBM, Corp. 2008
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"
21 #include "virtio-balloon.h"
27 #if defined(__linux__)
31 typedef struct VirtIOBalloon
34 VirtQueue
*ivq
, *dvq
, *svq
;
37 uint64_t stats
[VIRTIO_BALLOON_S_NR
];
38 VirtQueueElement stats_vq_elem
;
39 size_t stats_vq_offset
;
40 MonitorCompletion
*stats_callback
;
41 void *stats_opaque_callback_data
;
44 static VirtIOBalloon
*to_virtio_balloon(VirtIODevice
*vdev
)
46 return (VirtIOBalloon
*)vdev
;
49 static void balloon_page(void *addr
, int deflate
)
51 #if defined(__linux__)
52 if (!kvm_enabled() || kvm_has_sync_mmu())
53 madvise(addr
, TARGET_PAGE_SIZE
,
54 deflate
? MADV_WILLNEED
: MADV_DONTNEED
);
59 * reset_stats - Mark all items in the stats array as unset
61 * This function needs to be called at device intialization and before
62 * before updating to a set of newly-generated stats. This will ensure that no
63 * stale values stick around in case the guest reports a subset of the supported
66 static inline void reset_stats(VirtIOBalloon
*dev
)
69 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
72 static void stat_put(QDict
*dict
, const char *label
, uint64_t val
)
75 qdict_put(dict
, label
, qint_from_int(val
));
78 static QObject
*get_stats_qobject(VirtIOBalloon
*dev
)
80 QDict
*dict
= qdict_new();
81 uint64_t actual
= ram_size
- ((uint64_t) dev
->actual
<<
82 VIRTIO_BALLOON_PFN_SHIFT
);
84 stat_put(dict
, "actual", actual
);
85 stat_put(dict
, "mem_swapped_in", dev
->stats
[VIRTIO_BALLOON_S_SWAP_IN
]);
86 stat_put(dict
, "mem_swapped_out", dev
->stats
[VIRTIO_BALLOON_S_SWAP_OUT
]);
87 stat_put(dict
, "major_page_faults", dev
->stats
[VIRTIO_BALLOON_S_MAJFLT
]);
88 stat_put(dict
, "minor_page_faults", dev
->stats
[VIRTIO_BALLOON_S_MINFLT
]);
89 stat_put(dict
, "free_mem", dev
->stats
[VIRTIO_BALLOON_S_MEMFREE
]);
90 stat_put(dict
, "total_mem", dev
->stats
[VIRTIO_BALLOON_S_MEMTOT
]);
95 /* FIXME: once we do a virtio refactoring, this will get subsumed into common
97 static size_t memcpy_from_iovector(void *data
, size_t offset
, size_t size
,
98 struct iovec
*iov
, int iovlen
)
105 for (i
= 0; i
< iovlen
&& size
; i
++) {
106 if (offset
< (iov_off
+ iov
[i
].iov_len
)) {
107 size_t len
= MIN((iov_off
+ iov
[i
].iov_len
) - offset
, size
);
109 memcpy(ptr
+ data_off
, iov
[i
].iov_base
+ (offset
- iov_off
), len
);
116 iov_off
+= iov
[i
].iov_len
;
122 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
124 VirtIOBalloon
*s
= to_virtio_balloon(vdev
);
125 VirtQueueElement elem
;
127 while (virtqueue_pop(vq
, &elem
)) {
131 while (memcpy_from_iovector(&pfn
, offset
, 4,
132 elem
.out_sg
, elem
.out_num
) == 4) {
136 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
139 addr
= cpu_get_physical_page_desc(pa
);
140 if ((addr
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
143 /* Using qemu_get_ram_ptr is bending the rules a bit, but
144 should be OK because we only want a single page. */
145 balloon_page(qemu_get_ram_ptr(addr
), !!(vq
== s
->dvq
));
148 virtqueue_push(vq
, &elem
, offset
);
149 virtio_notify(vdev
, vq
);
153 static void complete_stats_request(VirtIOBalloon
*vb
)
157 if (!vb
->stats_opaque_callback_data
)
160 stats
= get_stats_qobject(vb
);
161 vb
->stats_callback(vb
->stats_opaque_callback_data
, stats
);
162 qobject_decref(stats
);
163 vb
->stats_opaque_callback_data
= NULL
;
164 vb
->stats_callback
= NULL
;
167 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
169 VirtIOBalloon
*s
= DO_UPCAST(VirtIOBalloon
, vdev
, vdev
);
170 VirtQueueElement
*elem
= &s
->stats_vq_elem
;
171 VirtIOBalloonStat stat
;
174 if (!virtqueue_pop(vq
, elem
)) {
178 /* Initialize the stats to get rid of any stale values. This is only
179 * needed to handle the case where a guest supports fewer stats than it
180 * used to (ie. it has booted into an old kernel).
184 while (memcpy_from_iovector(&stat
, offset
, sizeof(stat
), elem
->out_sg
,
185 elem
->out_num
) == sizeof(stat
)) {
186 uint16_t tag
= tswap16(stat
.tag
);
187 uint64_t val
= tswap64(stat
.val
);
189 offset
+= sizeof(stat
);
190 if (tag
< VIRTIO_BALLOON_S_NR
)
193 s
->stats_vq_offset
= offset
;
195 complete_stats_request(s
);
198 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
200 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
201 struct virtio_balloon_config config
;
203 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
204 config
.actual
= cpu_to_le32(dev
->actual
);
206 memcpy(config_data
, &config
, 8);
209 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
210 const uint8_t *config_data
)
212 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
213 struct virtio_balloon_config config
;
214 memcpy(&config
, config_data
, 8);
215 dev
->actual
= config
.actual
;
218 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint32_t f
)
220 f
|= (1 << VIRTIO_BALLOON_F_STATS_VQ
);
224 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
,
225 MonitorCompletion cb
, void *cb_data
)
227 VirtIOBalloon
*dev
= opaque
;
229 if (target
> ram_size
)
233 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
234 virtio_notify_config(&dev
->vdev
);
236 /* For now, only allow one request at a time. This restriction can be
237 * removed later by queueing callback and data pairs.
239 if (dev
->stats_callback
!= NULL
) {
242 dev
->stats_callback
= cb
;
243 dev
->stats_opaque_callback_data
= cb_data
;
244 if (dev
->vdev
.guest_features
& (1 << VIRTIO_BALLOON_F_STATS_VQ
)) {
245 virtqueue_push(dev
->svq
, &dev
->stats_vq_elem
, dev
->stats_vq_offset
);
246 virtio_notify(&dev
->vdev
, dev
->svq
);
248 /* Stats are not supported. Clear out any stale values that might
249 * have been set by a more featureful guest kernel.
252 complete_stats_request(dev
);
257 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
259 VirtIOBalloon
*s
= opaque
;
261 virtio_save(&s
->vdev
, f
);
263 qemu_put_be32(f
, s
->num_pages
);
264 qemu_put_be32(f
, s
->actual
);
267 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
269 VirtIOBalloon
*s
= opaque
;
274 virtio_load(&s
->vdev
, f
);
276 s
->num_pages
= qemu_get_be32(f
);
277 s
->actual
= qemu_get_be32(f
);
281 VirtIODevice
*virtio_balloon_init(DeviceState
*dev
)
285 s
= (VirtIOBalloon
*)virtio_common_init("virtio-balloon",
287 8, sizeof(VirtIOBalloon
));
289 s
->vdev
.get_config
= virtio_balloon_get_config
;
290 s
->vdev
.set_config
= virtio_balloon_set_config
;
291 s
->vdev
.get_features
= virtio_balloon_get_features
;
293 s
->ivq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
294 s
->dvq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
295 s
->svq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_receive_stats
);
298 qemu_add_balloon_handler(virtio_balloon_to_target
, s
);
300 register_savevm("virtio-balloon", -1, 1, virtio_balloon_save
, virtio_balloon_load
, s
);