Hi,
Here is a updated version:
- fixes panic when host requests more memory to deflate than guest has
- some cleanup
Index: files.pci
===
RCS file: /cvs/src/sys/dev/pci/files.pci,v
retrieving revision 1.289
diff -u -r1.289 files.pci
--- files.pci 29 Oct 2012 18:36:42 - 1.289
+++ files.pci 6 Nov 2012 10:34:22 -
@@ -837,3 +837,7 @@
device vioblk: scsi
attach vioblk at virtio
file dev/pci/vioblk.cvioblk
+
+device viomb
+attach viomb at virtio
+file dev/pci/viomb.c viomb
Index: GENERIC
===
RCS file: /cvs/src/sys/arch/i386/conf/GENERIC,v
retrieving revision 1.741
diff -u -r1.741 GENERIC
--- GENERIC 8 Oct 2012 17:26:02 - 1.741
+++ GENERIC 6 Nov 2012 10:36:25 -
@@ -799,3 +799,4 @@
virtio*at pci? # Virtio PCI device
vioblk*at virtio? # Virtio block device
vio* at virtio? # Virtio network device
+viomb* at virtio? # Virtio memory ballooning device
--- /dev/null Thu Nov 8 11:03:03 2012
+++ sys/dev/pci/viomb.c Thu Nov 8 11:02:31 2012
@@ -0,0 +1,447 @@
+/* $OpenBSD$*/
+/* $NetBSD: viomb.c,v 1.1 2011/10/30 12:12:21 hannken Exp $ */
+/*
+ * Copyright (c) 2012 Dinar Talypov di...@i-nk.ru
+ * Copyright (c) 2010 Minoura Makoto.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include sys/param.h
+#include sys/systm.h
+#include sys/malloc.h
+#include sys/device.h
+#include sys/workq.h
+#include sys/stdint.h
+#include uvm/uvm.h
+#include dev/pci/pcidevs.h
+#include dev/pci/pcivar.h
+
+#include dev/pci/virtioreg.h
+#include dev/pci/virtiovar.h
+
+#defineDEVNAME(sc) sc-sc_dev.dv_xname
+
+#if VIRTIO_DEBUG
+#define VIOMBDEBUG(sc, format, args...)\
+ do { printf(%s: format, sc-sc_dev.dv_xname, ##args);} \
+ while (0)
+#else
+#define VIOMBDEBUG(...)
+#endif
+
+/* flags used to specify kind of operation,
+ * actually should be moved to virtiovar.h
+ */
+#define VRING_READ 0
+#define VRING_WRITE1
+
+/* notify or don't notify */
+#define VRING_NO_NOTIFY0
+#define VRING_NOTIFY 1
+
+/* Configuration registers */
+#define VIRTIO_BALLOON_CONFIG_NUM_PAGES0 /* 32bit */
+#define VIRTIO_BALLOON_CONFIG_ACTUAL 4 /* 32bit */
+
+/* Feature bits */
+#define VIRTIO_BALLOON_F_MUST_TELL_HOST (10)
+#define VIRTIO_BALLOON_F_STATS_VQ (11)
+
+static const struct virtio_feature_name viomb_feature_names[] = {
+ {VIRTIO_BALLOON_F_MUST_TELL_HOST, TellHost},
+ {VIRTIO_BALLOON_F_STATS_VQ, StatVQ},
+ {0, NULL}
+};
+#define PGS_PER_REQ256 /* 1MB, 4KB/page */
+#define VQ_INFLATE 0
+#define VQ_DEFLATE 1
+
+struct balloon_req {
+ bus_dmamap_tbl_dmamap;
+ struct pglist bl_pglist;
+ int bl_nentries;
+ uint32_tbl_pages[PGS_PER_REQ];
+};
+
+struct viomb_softc {
+ struct device sc_dev;
+ struct virtio_softc *sc_virtio;
+
+ struct virtqueuesc_vq[2];
+ u_int32_t sc_npages;
+ u_int32_t sc_actual;
+ u_int32_t sc_balloon_size;
+ int sc_inflight;
+ struct balloon_req sc_req;
+ struct pglist sc_balloon_pages;
+ int sc_flags;
+#define INFALTE_DONE 0x01
+#define DEFLATE_DONE 0x02
+};
+
+int viomb_match(struct device *, void *, void *);
+voidviomb_attach(struct device *, struct device *, void *);
+int viomb_intr(void *);
+void