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.
15 #include "qemu-common.h"
22 #include "virtio-balloon.h"
28 #if defined(__linux__)
32 typedef struct VirtIOBalloon
35 VirtQueue
*ivq
, *dvq
, *svq
;
38 uint64_t stats
[VIRTIO_BALLOON_S_NR
];
39 VirtQueueElement stats_vq_elem
;
40 size_t stats_vq_offset
;
41 MonitorCompletion
*stats_callback
;
42 void *stats_opaque_callback_data
;
45 static VirtIOBalloon
*to_virtio_balloon(VirtIODevice
*vdev
)
47 return (VirtIOBalloon
*)vdev
;
50 static void balloon_page(void *addr
, int deflate
)
52 #if defined(__linux__)
53 if (!kvm_enabled() || kvm_has_sync_mmu())
54 madvise(addr
, TARGET_PAGE_SIZE
,
55 deflate
? MADV_WILLNEED
: MADV_DONTNEED
);
60 * reset_stats - Mark all items in the stats array as unset
62 * This function needs to be called at device intialization and before
63 * before updating to a set of newly-generated stats. This will ensure that no
64 * stale values stick around in case the guest reports a subset of the supported
67 static inline void reset_stats(VirtIOBalloon
*dev
)
70 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
73 static void stat_put(QDict
*dict
, const char *label
, uint64_t val
)
76 qdict_put(dict
, label
, qint_from_int(val
));
79 static QObject
*get_stats_qobject(VirtIOBalloon
*dev
)
81 QDict
*dict
= qdict_new();
82 uint64_t actual
= ram_size
- ((uint64_t) dev
->actual
<<
83 VIRTIO_BALLOON_PFN_SHIFT
);
85 stat_put(dict
, "actual", actual
);
86 stat_put(dict
, "mem_swapped_in", dev
->stats
[VIRTIO_BALLOON_S_SWAP_IN
]);
87 stat_put(dict
, "mem_swapped_out", dev
->stats
[VIRTIO_BALLOON_S_SWAP_OUT
]);
88 stat_put(dict
, "major_page_faults", dev
->stats
[VIRTIO_BALLOON_S_MAJFLT
]);
89 stat_put(dict
, "minor_page_faults", dev
->stats
[VIRTIO_BALLOON_S_MINFLT
]);
90 stat_put(dict
, "free_mem", dev
->stats
[VIRTIO_BALLOON_S_MEMFREE
]);
91 stat_put(dict
, "total_mem", dev
->stats
[VIRTIO_BALLOON_S_MEMTOT
]);
96 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
98 VirtIOBalloon
*s
= to_virtio_balloon(vdev
);
99 VirtQueueElement elem
;
101 while (virtqueue_pop(vq
, &elem
)) {
105 while (iov_to_buf(elem
.out_sg
, elem
.out_num
, &pfn
, offset
, 4) == 4) {
109 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
112 addr
= cpu_get_physical_page_desc(pa
);
113 if ((addr
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
116 /* Using qemu_get_ram_ptr is bending the rules a bit, but
117 should be OK because we only want a single page. */
118 balloon_page(qemu_get_ram_ptr(addr
), !!(vq
== s
->dvq
));
121 virtqueue_push(vq
, &elem
, offset
);
122 virtio_notify(vdev
, vq
);
126 static void complete_stats_request(VirtIOBalloon
*vb
)
130 if (!vb
->stats_opaque_callback_data
)
133 stats
= get_stats_qobject(vb
);
134 vb
->stats_callback(vb
->stats_opaque_callback_data
, stats
);
135 qobject_decref(stats
);
136 vb
->stats_opaque_callback_data
= NULL
;
137 vb
->stats_callback
= NULL
;
140 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
142 VirtIOBalloon
*s
= DO_UPCAST(VirtIOBalloon
, vdev
, vdev
);
143 VirtQueueElement
*elem
= &s
->stats_vq_elem
;
144 VirtIOBalloonStat stat
;
147 if (!virtqueue_pop(vq
, elem
)) {
151 /* Initialize the stats to get rid of any stale values. This is only
152 * needed to handle the case where a guest supports fewer stats than it
153 * used to (ie. it has booted into an old kernel).
157 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, &stat
, offset
, sizeof(stat
))
159 uint16_t tag
= tswap16(stat
.tag
);
160 uint64_t val
= tswap64(stat
.val
);
162 offset
+= sizeof(stat
);
163 if (tag
< VIRTIO_BALLOON_S_NR
)
166 s
->stats_vq_offset
= offset
;
168 complete_stats_request(s
);
171 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
173 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
174 struct virtio_balloon_config config
;
176 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
177 config
.actual
= cpu_to_le32(dev
->actual
);
179 memcpy(config_data
, &config
, 8);
182 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
183 const uint8_t *config_data
)
185 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
186 struct virtio_balloon_config config
;
187 memcpy(&config
, config_data
, 8);
188 dev
->actual
= config
.actual
;
191 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint32_t f
)
193 f
|= (1 << VIRTIO_BALLOON_F_STATS_VQ
);
197 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
,
198 MonitorCompletion cb
, void *cb_data
)
200 VirtIOBalloon
*dev
= opaque
;
202 if (target
> ram_size
)
206 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
207 virtio_notify_config(&dev
->vdev
);
209 /* For now, only allow one request at a time. This restriction can be
210 * removed later by queueing callback and data pairs.
212 if (dev
->stats_callback
!= NULL
) {
215 dev
->stats_callback
= cb
;
216 dev
->stats_opaque_callback_data
= cb_data
;
217 if (dev
->vdev
.guest_features
& (1 << VIRTIO_BALLOON_F_STATS_VQ
)) {
218 virtqueue_push(dev
->svq
, &dev
->stats_vq_elem
, dev
->stats_vq_offset
);
219 virtio_notify(&dev
->vdev
, dev
->svq
);
221 /* Stats are not supported. Clear out any stale values that might
222 * have been set by a more featureful guest kernel.
225 complete_stats_request(dev
);
230 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
232 VirtIOBalloon
*s
= opaque
;
234 virtio_save(&s
->vdev
, f
);
236 qemu_put_be32(f
, s
->num_pages
);
237 qemu_put_be32(f
, s
->actual
);
240 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
242 VirtIOBalloon
*s
= opaque
;
247 virtio_load(&s
->vdev
, f
);
249 s
->num_pages
= qemu_get_be32(f
);
250 s
->actual
= qemu_get_be32(f
);
254 VirtIODevice
*virtio_balloon_init(DeviceState
*dev
)
258 s
= (VirtIOBalloon
*)virtio_common_init("virtio-balloon",
260 8, sizeof(VirtIOBalloon
));
262 s
->vdev
.get_config
= virtio_balloon_get_config
;
263 s
->vdev
.set_config
= virtio_balloon_set_config
;
264 s
->vdev
.get_features
= virtio_balloon_get_features
;
266 s
->ivq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
267 s
->dvq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
268 s
->svq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_receive_stats
);
271 qemu_add_balloon_handler(virtio_balloon_to_target
, s
);
273 register_savevm("virtio-balloon", -1, 1, virtio_balloon_save
, virtio_balloon_load
, s
);