vmsvga: correct bitmap and pixmap size checks
[qemu/ar7.git] / hw / virtio / virtio-balloon.c
blob49a2f4aade16988322cfecd76aa942fd50fc7a86
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 "qemu/osdep.h"
17 #include "qemu/iov.h"
18 #include "qemu/timer.h"
19 #include "qemu-common.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/i386/pc.h"
22 #include "sysemu/balloon.h"
23 #include "hw/virtio/virtio-balloon.h"
24 #include "sysemu/kvm.h"
25 #include "exec/address-spaces.h"
26 #include "qapi/visitor.h"
27 #include "qapi-event.h"
28 #include "trace.h"
30 #include "hw/virtio/virtio-bus.h"
31 #include "hw/virtio/virtio-access.h"
33 #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)
35 static void balloon_page(void *addr, int deflate)
37 #if defined(__linux__)
38 if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
39 kvm_has_sync_mmu())) {
40 qemu_madvise(addr, BALLOON_PAGE_SIZE,
41 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
43 #endif
46 static const char *balloon_stat_names[] = {
47 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
48 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
49 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
50 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
51 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
52 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
53 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
54 [VIRTIO_BALLOON_S_NR] = NULL
58 * reset_stats - Mark all items in the stats array as unset
60 * This function needs to be called at device initialization and before
61 * updating to a set of newly-generated stats. This will ensure that no
62 * stale values stick around in case the guest reports a subset of the supported
63 * statistics.
65 static inline void reset_stats(VirtIOBalloon *dev)
67 int i;
68 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
71 static bool balloon_stats_supported(const VirtIOBalloon *s)
73 VirtIODevice *vdev = VIRTIO_DEVICE(s);
74 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
77 static bool balloon_stats_enabled(const VirtIOBalloon *s)
79 return s->stats_poll_interval > 0;
82 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
84 if (balloon_stats_enabled(s)) {
85 timer_del(s->stats_timer);
86 timer_free(s->stats_timer);
87 s->stats_timer = NULL;
88 s->stats_poll_interval = 0;
92 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
94 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
97 static void balloon_stats_poll_cb(void *opaque)
99 VirtIOBalloon *s = opaque;
100 VirtIODevice *vdev = VIRTIO_DEVICE(s);
102 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
103 /* re-schedule */
104 balloon_stats_change_timer(s, s->stats_poll_interval);
105 return;
108 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
109 virtio_notify(vdev, s->svq);
110 g_free(s->stats_vq_elem);
111 s->stats_vq_elem = NULL;
114 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
115 void *opaque, Error **errp)
117 Error *err = NULL;
118 VirtIOBalloon *s = opaque;
119 int i;
121 visit_start_struct(v, name, NULL, 0, &err);
122 if (err) {
123 goto out;
125 visit_type_int(v, "last-update", &s->stats_last_update, &err);
126 if (err) {
127 goto out_end;
130 visit_start_struct(v, "stats", NULL, 0, &err);
131 if (err) {
132 goto out_end;
134 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
135 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
136 if (err) {
137 goto out_nested;
140 visit_check_struct(v, &err);
141 out_nested:
142 visit_end_struct(v, NULL);
144 if (!err) {
145 visit_check_struct(v, &err);
147 out_end:
148 visit_end_struct(v, NULL);
149 out:
150 error_propagate(errp, err);
153 static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
154 const char *name, void *opaque,
155 Error **errp)
157 VirtIOBalloon *s = opaque;
158 visit_type_int(v, name, &s->stats_poll_interval, errp);
161 static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
162 const char *name, void *opaque,
163 Error **errp)
165 VirtIOBalloon *s = opaque;
166 Error *local_err = NULL;
167 int64_t value;
169 visit_type_int(v, name, &value, &local_err);
170 if (local_err) {
171 error_propagate(errp, local_err);
172 return;
175 if (value < 0) {
176 error_setg(errp, "timer value must be greater than zero");
177 return;
180 if (value > UINT32_MAX) {
181 error_setg(errp, "timer value is too big");
182 return;
185 if (value == s->stats_poll_interval) {
186 return;
189 if (value == 0) {
190 /* timer=0 disables the timer */
191 balloon_stats_destroy_timer(s);
192 return;
195 if (balloon_stats_enabled(s)) {
196 /* timer interval change */
197 s->stats_poll_interval = value;
198 balloon_stats_change_timer(s, value);
199 return;
202 /* create a new timer */
203 g_assert(s->stats_timer == NULL);
204 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
205 s->stats_poll_interval = value;
206 balloon_stats_change_timer(s, 0);
209 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
211 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
212 VirtQueueElement *elem;
213 MemoryRegionSection section;
215 for (;;) {
216 size_t offset = 0;
217 uint32_t pfn;
218 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
219 if (!elem) {
220 return;
223 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
224 ram_addr_t pa;
225 ram_addr_t addr;
226 int p = virtio_ldl_p(vdev, &pfn);
228 pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
229 offset += 4;
231 /* FIXME: remove get_system_memory(), but how? */
232 section = memory_region_find(get_system_memory(), pa, 1);
233 if (!int128_nz(section.size) || !memory_region_is_ram(section.mr))
234 continue;
236 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
237 pa);
238 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
239 should be OK because we only want a single page. */
240 addr = section.offset_within_region;
241 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
242 !!(vq == s->dvq));
243 memory_region_unref(section.mr);
246 virtqueue_push(vq, elem, offset);
247 virtio_notify(vdev, vq);
248 g_free(elem);
252 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
254 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
255 VirtQueueElement *elem;
256 VirtIOBalloonStat stat;
257 size_t offset = 0;
258 qemu_timeval tv;
260 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
261 if (!elem) {
262 goto out;
265 if (s->stats_vq_elem != NULL) {
266 /* This should never happen if the driver follows the spec. */
267 virtqueue_push(vq, s->stats_vq_elem, 0);
268 virtio_notify(vdev, vq);
269 g_free(s->stats_vq_elem);
272 s->stats_vq_elem = elem;
274 /* Initialize the stats to get rid of any stale values. This is only
275 * needed to handle the case where a guest supports fewer stats than it
276 * used to (ie. it has booted into an old kernel).
278 reset_stats(s);
280 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
281 == sizeof(stat)) {
282 uint16_t tag = virtio_tswap16(vdev, stat.tag);
283 uint64_t val = virtio_tswap64(vdev, stat.val);
285 offset += sizeof(stat);
286 if (tag < VIRTIO_BALLOON_S_NR)
287 s->stats[tag] = val;
289 s->stats_vq_offset = offset;
291 if (qemu_gettimeofday(&tv) < 0) {
292 fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);
293 goto out;
296 s->stats_last_update = tv.tv_sec;
298 out:
299 if (balloon_stats_enabled(s)) {
300 balloon_stats_change_timer(s, s->stats_poll_interval);
304 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
306 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
307 struct virtio_balloon_config config;
309 config.num_pages = cpu_to_le32(dev->num_pages);
310 config.actual = cpu_to_le32(dev->actual);
312 trace_virtio_balloon_get_config(config.num_pages, config.actual);
313 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
316 static int build_dimm_list(Object *obj, void *opaque)
318 GSList **list = opaque;
320 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
321 DeviceState *dev = DEVICE(obj);
322 if (dev->realized) { /* only realized DIMMs matter */
323 *list = g_slist_prepend(*list, dev);
327 object_child_foreach(obj, build_dimm_list, opaque);
328 return 0;
331 static ram_addr_t get_current_ram_size(void)
333 GSList *list = NULL, *item;
334 ram_addr_t size = ram_size;
336 build_dimm_list(qdev_get_machine(), &list);
337 for (item = list; item; item = g_slist_next(item)) {
338 Object *obj = OBJECT(item->data);
339 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
340 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
341 &error_abort);
344 g_slist_free(list);
346 return size;
349 static void virtio_balloon_set_config(VirtIODevice *vdev,
350 const uint8_t *config_data)
352 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
353 struct virtio_balloon_config config;
354 uint32_t oldactual = dev->actual;
355 ram_addr_t vm_ram_size = get_current_ram_size();
357 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
358 dev->actual = le32_to_cpu(config.actual);
359 if (dev->actual != oldactual) {
360 qapi_event_send_balloon_change(vm_ram_size -
361 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
362 &error_abort);
364 trace_virtio_balloon_set_config(dev->actual, oldactual);
367 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
368 Error **errp)
370 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
371 f |= dev->host_features;
372 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
373 return f;
376 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
378 VirtIOBalloon *dev = opaque;
379 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
380 VIRTIO_BALLOON_PFN_SHIFT);
383 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
385 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
386 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
387 ram_addr_t vm_ram_size = get_current_ram_size();
389 if (target > vm_ram_size) {
390 target = vm_ram_size;
392 if (target) {
393 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
394 virtio_notify_config(vdev);
396 trace_virtio_balloon_to_target(target, dev->num_pages);
399 static void virtio_balloon_save_device(VirtIODevice *vdev, QEMUFile *f)
401 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
403 qemu_put_be32(f, s->num_pages);
404 qemu_put_be32(f, s->actual);
407 static int virtio_balloon_load(QEMUFile *f, void *opaque, size_t size)
409 return virtio_load(VIRTIO_DEVICE(opaque), f, 1);
412 static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
413 int version_id)
415 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
417 s->num_pages = qemu_get_be32(f);
418 s->actual = qemu_get_be32(f);
420 if (balloon_stats_enabled(s)) {
421 balloon_stats_change_timer(s, s->stats_poll_interval);
423 return 0;
426 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
428 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
429 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
430 int ret;
432 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
433 sizeof(struct virtio_balloon_config));
435 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
436 virtio_balloon_stat, s);
438 if (ret < 0) {
439 error_setg(errp, "Only one balloon device is supported");
440 virtio_cleanup(vdev);
441 return;
444 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
445 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
446 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
448 reset_stats(s);
451 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
453 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
454 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
456 balloon_stats_destroy_timer(s);
457 qemu_remove_balloon_handler(s);
458 virtio_cleanup(vdev);
461 static void virtio_balloon_device_reset(VirtIODevice *vdev)
463 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
465 if (s->stats_vq_elem != NULL) {
466 virtqueue_discard(s->svq, s->stats_vq_elem, 0);
467 g_free(s->stats_vq_elem);
468 s->stats_vq_elem = NULL;
472 static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
474 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
476 if (!s->stats_vq_elem && vdev->vm_running &&
477 (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) {
478 /* poll stats queue for the element we have discarded when the VM
479 * was stopped */
480 virtio_balloon_receive_stats(vdev, s->svq);
484 static void virtio_balloon_instance_init(Object *obj)
486 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
488 object_property_add(obj, "guest-stats", "guest statistics",
489 balloon_stats_get_all, NULL, NULL, s, NULL);
491 object_property_add(obj, "guest-stats-polling-interval", "int",
492 balloon_stats_get_poll_interval,
493 balloon_stats_set_poll_interval,
494 NULL, s, NULL);
497 VMSTATE_VIRTIO_DEVICE(balloon, 1, virtio_balloon_load, virtio_vmstate_save);
499 static Property virtio_balloon_properties[] = {
500 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
501 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
502 DEFINE_PROP_END_OF_LIST(),
505 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
507 DeviceClass *dc = DEVICE_CLASS(klass);
508 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
510 dc->props = virtio_balloon_properties;
511 dc->vmsd = &vmstate_virtio_balloon;
512 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
513 vdc->realize = virtio_balloon_device_realize;
514 vdc->unrealize = virtio_balloon_device_unrealize;
515 vdc->reset = virtio_balloon_device_reset;
516 vdc->get_config = virtio_balloon_get_config;
517 vdc->set_config = virtio_balloon_set_config;
518 vdc->get_features = virtio_balloon_get_features;
519 vdc->save = virtio_balloon_save_device;
520 vdc->load = virtio_balloon_load_device;
521 vdc->set_status = virtio_balloon_set_status;
524 static const TypeInfo virtio_balloon_info = {
525 .name = TYPE_VIRTIO_BALLOON,
526 .parent = TYPE_VIRTIO_DEVICE,
527 .instance_size = sizeof(VirtIOBalloon),
528 .instance_init = virtio_balloon_instance_init,
529 .class_init = virtio_balloon_class_init,
532 static void virtio_register_types(void)
534 type_register_static(&virtio_balloon_info);
537 type_init(virtio_register_types)