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"
20 #include "virtio-balloon.h"
24 #if defined(__linux__)
28 typedef struct VirtIOBalloon
36 static VirtIOBalloon
*to_virtio_balloon(VirtIODevice
*vdev
)
38 return (VirtIOBalloon
*)vdev
;
41 static void balloon_page(void *addr
, int deflate
)
43 #if defined(__linux__)
44 if (!kvm_enabled() || kvm_has_sync_mmu())
45 madvise(addr
, TARGET_PAGE_SIZE
,
46 deflate
? MADV_WILLNEED
: MADV_DONTNEED
);
50 /* FIXME: once we do a virtio refactoring, this will get subsumed into common
52 static size_t memcpy_from_iovector(void *data
, size_t offset
, size_t size
,
53 struct iovec
*iov
, int iovlen
)
60 for (i
= 0; i
< iovlen
&& size
; i
++) {
61 if (offset
< (iov_off
+ iov
[i
].iov_len
)) {
62 size_t len
= MIN((iov_off
+ iov
[i
].iov_len
) - offset
, size
);
64 memcpy(ptr
+ data_off
, iov
[i
].iov_base
+ (offset
- iov_off
), len
);
71 iov_off
+= iov
[i
].iov_len
;
77 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
79 VirtIOBalloon
*s
= to_virtio_balloon(vdev
);
80 VirtQueueElement elem
;
82 while (virtqueue_pop(vq
, &elem
)) {
86 while (memcpy_from_iovector(&pfn
, offset
, 4,
87 elem
.out_sg
, elem
.out_num
) == 4) {
91 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
94 addr
= cpu_get_physical_page_desc(pa
);
95 if ((addr
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
98 /* Using qemu_get_ram_ptr is bending the rules a bit, but
99 should be OK because we only want a single page. */
100 balloon_page(qemu_get_ram_ptr(addr
), !!(vq
== s
->dvq
));
103 virtqueue_push(vq
, &elem
, offset
);
104 virtio_notify(vdev
, vq
);
108 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
110 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
111 struct virtio_balloon_config config
;
113 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
114 config
.actual
= cpu_to_le32(dev
->actual
);
116 memcpy(config_data
, &config
, 8);
119 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
120 const uint8_t *config_data
)
122 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
123 struct virtio_balloon_config config
;
124 memcpy(&config
, config_data
, 8);
125 dev
->actual
= config
.actual
;
128 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
)
133 static ram_addr_t
virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
135 VirtIOBalloon
*dev
= opaque
;
137 if (target
> ram_size
)
141 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
142 virtio_notify_config(&dev
->vdev
);
145 return ram_size
- (dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
);
148 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
150 VirtIOBalloon
*s
= opaque
;
152 virtio_save(&s
->vdev
, f
);
154 qemu_put_be32(f
, s
->num_pages
);
155 qemu_put_be32(f
, s
->actual
);
158 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
160 VirtIOBalloon
*s
= opaque
;
165 virtio_load(&s
->vdev
, f
);
167 s
->num_pages
= qemu_get_be32(f
);
168 s
->actual
= qemu_get_be32(f
);
173 VirtIODevice
*virtio_balloon_init(DeviceState
*dev
)
177 s
= (VirtIOBalloon
*)virtio_common_init("virtio-balloon",
179 8, sizeof(VirtIOBalloon
));
181 s
->vdev
.get_config
= virtio_balloon_get_config
;
182 s
->vdev
.set_config
= virtio_balloon_set_config
;
183 s
->vdev
.get_features
= virtio_balloon_get_features
;
185 s
->ivq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
186 s
->dvq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
188 qemu_add_balloon_handler(virtio_balloon_to_target
, s
);
190 register_savevm("virtio-balloon", -1, 1, virtio_balloon_save
, virtio_balloon_load
, s
);