sync with tizen_2.2
[sdk/emulator/qemu.git] / hw / virtio-balloon.c
1 /*
2  * Virtio Balloon Device
3  *
4  * Copyright IBM, Corp. 2008
5  * Copyright (C) 2011 Red Hat, Inc.
6  * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
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
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 "virtio-transport.h"
24 #include "kvm.h"
25 #include "exec-memory.h"
26
27 #if defined(__linux__)
28 #include <sys/mman.h>
29 #endif
30
31 typedef struct VirtIOBalloon
32 {
33     VirtIODevice vdev;
34     VirtQueue *ivq, *dvq, *svq;
35     uint32_t num_pages;
36     uint32_t actual;
37     uint64_t stats[VIRTIO_BALLOON_S_NR];
38     VirtQueueElement stats_vq_elem;
39     size_t stats_vq_offset;
40     DeviceState *qdev;
41 } VirtIOBalloon;
42
43 static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
44 {
45     return (VirtIOBalloon *)vdev;
46 }
47
48 static void balloon_page(void *addr, int deflate)
49 {
50 #if defined(__linux__)
51     if (!kvm_enabled() || kvm_has_sync_mmu())
52         qemu_madvise(addr, TARGET_PAGE_SIZE,
53                 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
54 #endif
55 }
56
57 /*
58  * reset_stats - Mark all items in the stats array as unset
59  *
60  * This function needs to be called at device intialization and before
61  * before 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.
64  */
65 static inline void reset_stats(VirtIOBalloon *dev)
66 {
67     int i;
68     for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
69 }
70
71 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
72 {
73     VirtIOBalloon *s = to_virtio_balloon(vdev);
74     VirtQueueElement elem;
75     MemoryRegionSection section;
76
77     while (virtqueue_pop(vq, &elem)) {
78         size_t offset = 0;
79         uint32_t pfn;
80
81         while (iov_to_buf(elem.out_sg, elem.out_num, offset, &pfn, 4) == 4) {
82             ram_addr_t pa;
83             ram_addr_t addr;
84
85             pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
86             offset += 4;
87
88             /* FIXME: remove get_system_memory(), but how? */
89             section = memory_region_find(get_system_memory(), pa, 1);
90             if (!section.size || !memory_region_is_ram(section.mr))
91                 continue;
92
93             /* Using memory_region_get_ram_ptr is bending the rules a bit, but
94                should be OK because we only want a single page.  */
95             addr = section.offset_within_region;
96             balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
97                          !!(vq == s->dvq));
98         }
99
100         virtqueue_push(vq, &elem, offset);
101         virtio_notify(vdev, vq);
102     }
103 }
104
105 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
106 {
107     VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
108     VirtQueueElement *elem = &s->stats_vq_elem;
109     VirtIOBalloonStat stat;
110     size_t offset = 0;
111
112     if (!virtqueue_pop(vq, elem)) {
113         return;
114     }
115
116     /* Initialize the stats to get rid of any stale values.  This is only
117      * needed to handle the case where a guest supports fewer stats than it
118      * used to (ie. it has booted into an old kernel).
119      */
120     reset_stats(s);
121
122     while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
123            == sizeof(stat)) {
124         uint16_t tag = tswap16(stat.tag);
125         uint64_t val = tswap64(stat.val);
126
127         offset += sizeof(stat);
128         if (tag < VIRTIO_BALLOON_S_NR)
129             s->stats[tag] = val;
130     }
131     s->stats_vq_offset = offset;
132 }
133
134 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
135 {
136     VirtIOBalloon *dev = to_virtio_balloon(vdev);
137     struct virtio_balloon_config config;
138
139     config.num_pages = cpu_to_le32(dev->num_pages);
140     config.actual = cpu_to_le32(dev->actual);
141
142     memcpy(config_data, &config, 8);
143 }
144
145 static void virtio_balloon_set_config(VirtIODevice *vdev,
146                                       const uint8_t *config_data)
147 {
148     VirtIOBalloon *dev = to_virtio_balloon(vdev);
149     struct virtio_balloon_config config;
150     uint32_t oldactual = dev->actual;
151     memcpy(&config, config_data, 8);
152     dev->actual = le32_to_cpu(config.actual);
153     if (dev->actual != oldactual) {
154         qemu_balloon_changed(ram_size -
155                              (dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
156     }
157 }
158
159 static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
160 {
161     f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
162     return f;
163 }
164
165 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
166 {
167     VirtIOBalloon *dev = opaque;
168
169 #if 0
170     /* Disable guest-provided stats for now. For more details please check:
171      * https://bugzilla.redhat.com/show_bug.cgi?id=623903
172      *
173      * If you do enable it (which is probably not going to happen as we
174      * need a new command for it), remember that you also need to fill the
175      * appropriate members of the BalloonInfo structure so that the stats
176      * are returned to the client.
177      */
178     if (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ)) {
179         virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset);
180         virtio_notify(&dev->vdev, dev->svq);
181         return;
182     }
183 #endif
184
185     /* Stats are not supported.  Clear out any stale values that might
186      * have been set by a more featureful guest kernel.
187      */
188     reset_stats(dev);
189
190     info->actual = ram_size - ((uint64_t) dev->actual <<
191                                VIRTIO_BALLOON_PFN_SHIFT);
192 }
193
194 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
195 {
196     VirtIOBalloon *dev = opaque;
197
198     if (target > ram_size) {
199         target = ram_size;
200     }
201     if (target) {
202         dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
203         virtio_notify_config(&dev->vdev);
204     }
205 }
206
207 static void virtio_balloon_save(QEMUFile *f, void *opaque)
208 {
209     VirtIOBalloon *s = opaque;
210
211     virtio_save(&s->vdev, f);
212
213     qemu_put_be32(f, s->num_pages);
214     qemu_put_be32(f, s->actual);
215 }
216
217 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
218 {
219     VirtIOBalloon *s = opaque;
220     int ret;
221
222     if (version_id != 1)
223         return -EINVAL;
224
225     ret = virtio_load(&s->vdev, f);
226     if (ret) {
227         return ret;
228     }
229
230     s->num_pages = qemu_get_be32(f);
231     s->actual = qemu_get_be32(f);
232     return 0;
233 }
234
235 VirtIODevice *virtio_balloon_init(DeviceState *dev)
236 {
237     VirtIOBalloon *s;
238     int ret;
239
240     s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
241                                             VIRTIO_ID_BALLOON,
242                                             8, sizeof(VirtIOBalloon));
243
244     s->vdev.get_config = virtio_balloon_get_config;
245     s->vdev.set_config = virtio_balloon_set_config;
246     s->vdev.get_features = virtio_balloon_get_features;
247
248     ret = qemu_add_balloon_handler(virtio_balloon_to_target,
249                                    virtio_balloon_stat, s);
250     if (ret < 0) {
251         virtio_cleanup(&s->vdev);
252         return NULL;
253     }
254
255     s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
256     s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
257     s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
258
259     reset_stats(s);
260
261     s->qdev = dev;
262     register_savevm(dev, "virtio-balloon", -1, 1,
263                     virtio_balloon_save, virtio_balloon_load, s);
264
265     return &s->vdev;
266 }
267
268 void virtio_balloon_exit(VirtIODevice *vdev)
269 {
270     VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
271
272     qemu_remove_balloon_handler(s);
273     unregister_savevm(s->qdev, "virtio-balloon", s);
274     virtio_cleanup(vdev);
275 }
276
277 /******************** VirtIOBaloon Device **********************/
278
279 static int virtio_balloondev_init(DeviceState *dev)
280 {
281     VirtIODevice *vdev;
282     VirtIOBaloonState *s = VIRTIO_BALLOON_FROM_QDEV(dev);
283     vdev = virtio_balloon_init(dev);
284     if (!vdev) {
285         return -1;
286     }
287
288     assert(s->trl != NULL);
289
290     return virtio_call_backend_init_cb(dev, s->trl, vdev);
291 }
292
293 static Property virtio_balloon_properties[] = {
294     DEFINE_PROP_END_OF_LIST(),
295 };
296
297 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
298 {
299     DeviceClass *dc = DEVICE_CLASS(klass);
300     dc->init = virtio_balloondev_init;
301     dc->props = virtio_balloon_properties;
302 }
303
304 static TypeInfo virtio_balloon_info = {
305     .name = "virtio-balloon",
306     .parent = TYPE_DEVICE,
307     .instance_size = sizeof(VirtIOBaloonState),
308     .class_init = virtio_balloon_class_init,
309 };
310
311 static void virtio_baloon_register_types(void)
312 {
313     type_register_static(&virtio_balloon_info);
314 }
315
316 type_init(virtio_baloon_register_types)