clear-pkgs-linux-iot-lts2018/0462-VHM-add-vhm-char-devic...

1065 lines
36 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: liang ding <liang.ding@intel.com>
Date: Fri, 31 Aug 2018 10:58:55 +0800
Subject: [PATCH] VHM: add vhm char device driver
VHM(virtio and hypervisor service module) is the important middle layer
to run virtio and hypervisor services in linux kernel for ACRN hypervisor.
The vhm char device is the main interface. It provides ioctls to
applications and interacts with ACRN hypervisor through different
hypercalls.
This patch enable ACRN vhm service based on CONFIG_ACRN; added a basic vhm
char device which contains services for VM management in drivers/char/vhm;
and located vhm service lib in drivers/vhm.
Change-Id: Ib6c95d810581abd226692cbec9649a24b466a93b
Tracked-On: 218445
Signed-off-by: liang ding <liang.ding@intel.com>
Signed-off-by: Jason Zeng <jason.zeng@intel.com>
Signed-off-by: Xiao Zheng <xiao.zheng@intel.com>
Signed-off-by: Jason Chen CJ <jason.cj.chen@intel.com>
Signed-off-by: Jack Ren <jack.ren@intel.com>
Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com>
Reviewed-on:
Reviewed-by: Dong, Eddie <eddie.dong@intel.com>
Tested-by: Dong, Eddie <eddie.dong@intel.com>
---
drivers/Makefile | 1 +
drivers/char/Makefile | 1 +
drivers/char/vhm/Makefile | 1 +
drivers/char/vhm/vhm_dev.c | 247 +++++++++++++++++++++++++++++
drivers/vhm/Makefile | 1 +
drivers/vhm/vhm_hypercall.c | 134 ++++++++++++++++
drivers/vhm/vhm_vm_mngt.c | 107 +++++++++++++
include/linux/vhm/acrn_common.h | 65 ++++++++
include/linux/vhm/acrn_hv_defs.h | 81 ++++++++++
include/linux/vhm/vhm_hypercall.h | 148 +++++++++++++++++
include/linux/vhm/vhm_ioctl_defs.h | 67 ++++++++
include/linux/vhm/vhm_vm_mngt.h | 78 +++++++++
12 files changed, 931 insertions(+)
create mode 100644 drivers/char/vhm/Makefile
create mode 100644 drivers/char/vhm/vhm_dev.c
create mode 100644 drivers/vhm/Makefile
create mode 100644 drivers/vhm/vhm_hypercall.c
create mode 100644 drivers/vhm/vhm_vm_mngt.c
create mode 100644 include/linux/vhm/acrn_common.h
create mode 100644 include/linux/vhm/acrn_hv_defs.h
create mode 100644 include/linux/vhm/vhm_hypercall.h
create mode 100644 include/linux/vhm/vhm_ioctl_defs.h
create mode 100644 include/linux/vhm/vhm_vm_mngt.h
diff --git a/drivers/Makefile b/drivers/Makefile
index a1a8bf6df4d3..b260b5a5f222 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -183,6 +183,7 @@ obj-$(CONFIG_FPGA) += fpga/
obj-$(CONFIG_FSI) += fsi/
obj-$(CONFIG_TEE) += tee/
obj-$(CONFIG_MULTIPLEXER) += mux/
+obj-$(CONFIG_ACRN) += vhm/
obj-$(CONFIG_UNISYS_VISORBUS) += visorbus/
obj-$(CONFIG_SIOX) += siox/
obj-$(CONFIG_GNSS) += gnss/
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index 88764b76d975..63717938df85 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -59,3 +59,4 @@ obj-$(CONFIG_XILLYBUS) += xillybus/
obj-$(CONFIG_POWERNV_OP_PANEL) += powernv-op-panel.o
obj-$(CONFIG_ADI) += adi.o
obj-$(CONFIG_RPMB) += rpmb/
+obj-$(CONFIG_ACRN) += vhm/
diff --git a/drivers/char/vhm/Makefile b/drivers/char/vhm/Makefile
new file mode 100644
index 000000000000..cb801c70a37e
--- /dev/null
+++ b/drivers/char/vhm/Makefile
@@ -0,0 +1 @@
+obj-y += vhm_dev.o
diff --git a/drivers/char/vhm/vhm_dev.c b/drivers/char/vhm/vhm_dev.c
new file mode 100644
index 000000000000..527e90b187cf
--- /dev/null
+++ b/drivers/char/vhm/vhm_dev.c
@@ -0,0 +1,247 @@
+/*
+ * virtio and hyperviosr service module (VHM): main framework
+ *
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ * Liang Ding <liang.ding@intel.com>
+ * Jason Zeng <jason.zeng@intel.com>
+ * Xiao Zheng <xiao.zheng@intel.com>
+ * Jason Chen CJ <jason.cj.chen@intel.com>
+ * Jack Ren <jack.ren@intel.com>
+ * Mingqiang Chi <mingqiang.chi@intel.com>
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/highmem.h>
+#include <linux/page-flags.h>
+#include <linux/pagemap.h>
+#include <linux/fs.h>
+#include <linux/poll.h>
+#include <linux/wait.h>
+#include <linux/freezer.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+#include <linux/list.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/pci.h>
+
+#include <linux/vhm/acrn_hv_defs.h>
+#include <linux/vhm/vhm_ioctl_defs.h>
+#include <linux/vhm/vhm_vm_mngt.h>
+#include <linux/vhm/vhm_hypercall.h>
+
+#define DEVICE_NAME "acrn_vhm"
+#define CLASS_NAME "vhm"
+
+static int major;
+static struct class *vhm_class;
+static struct device *vhm_device;
+
+static int vhm_dev_open(struct inode *inodep, struct file *filep)
+{
+ struct vhm_vm *vm;
+
+ vm = kzalloc(sizeof(struct vhm_vm), GFP_KERNEL);
+ pr_info("vhm_dev_open: opening device node\n");
+
+ if (!vm)
+ return -ENOMEM;
+ vm->vmid = ACRN_INVALID_VMID;
+ vm->dev = vhm_device;
+
+ vm_mutex_lock(&vhm_vm_list_lock);
+ vm->refcnt = 1;
+ vm_list_add(&vm->list);
+ vm_mutex_unlock(&vhm_vm_list_lock);
+ filep->private_data = vm;
+ return 0;
+}
+
+static ssize_t vhm_dev_read(struct file *filep, char *buffer, size_t len,
+ loff_t *offset)
+{
+ /* Does Nothing */
+ pr_info("vhm_dev_read: reading device node\n");
+ return 0;
+}
+
+static ssize_t vhm_dev_write(struct file *filep, const char *buffer,
+ size_t len, loff_t *offset)
+{
+ /* Does Nothing */
+ pr_info("vhm_dev_read: writing device node\n");
+ return 0;
+}
+
+static long vhm_dev_ioctl(struct file *filep,
+ unsigned int ioctl_num, unsigned long ioctl_param)
+{
+ long ret = 0;
+ struct vhm_vm *vm;
+
+ trace_printk("[%s] ioctl_num=0x%x\n", __func__, ioctl_num);
+
+ vm = (struct vhm_vm *)filep->private_data;
+ if (vm == NULL) {
+ pr_err("vhm: invalid VM !\n");
+ return -EFAULT;
+ }
+ if ((vm->vmid == ACRN_INVALID_VMID) && (ioctl_num != IC_CREATE_VM)) {
+ pr_err("vhm: invalid VM ID !\n");
+ return -EFAULT;
+ }
+
+ switch (ioctl_num) {
+ case IC_CREATE_VM:
+ ret = vhm_create_vm(vm, ioctl_param);
+ break;
+
+ case IC_RESUME_VM:
+ ret = vhm_resume_vm(vm);
+ break;
+
+ case IC_PAUSE_VM:
+ ret = vhm_pause_vm(vm);
+ break;
+
+ case IC_DESTROY_VM:
+ ret = vhm_destroy_vm(vm);
+ break;
+
+ case IC_QUERY_VMSTATE:
+ ret = vhm_query_vm_state(vm);
+ break;
+
+ default:
+ pr_warn("Unknown IOCTL 0x%x\n", ioctl_num);
+ ret = 0;
+ break;
+ }
+
+ return ret;
+}
+
+static int vhm_dev_release(struct inode *inodep, struct file *filep)
+{
+ struct vhm_vm *vm = filep->private_data;
+
+ if (vm == NULL) {
+ pr_err("vhm: invalid VM !\n");
+ return -EFAULT;
+ }
+ put_vm(vm);
+ filep->private_data = NULL;
+ return 0;
+}
+
+static const struct file_operations fops = {
+ .open = vhm_dev_open,
+ .read = vhm_dev_read,
+ .write = vhm_dev_write,
+ .release = vhm_dev_release,
+ .unlocked_ioctl = vhm_dev_ioctl,
+};
+
+static int __init vhm_init(void)
+{
+ pr_info("vhm: initializing\n");
+
+ /* Try to dynamically allocate a major number for the device */
+ major = register_chrdev(0, DEVICE_NAME, &fops);
+ if (major < 0) {
+ pr_warn("vhm: failed to register a major number\n");
+ return major;
+ }
+ pr_info("vhm: registered correctly with major number %d\n", major);
+
+ /* Register the device class */
+ vhm_class = class_create(THIS_MODULE, CLASS_NAME);
+ if (IS_ERR(vhm_class)) {
+ unregister_chrdev(major, DEVICE_NAME);
+ pr_warn("vhm: failed to register device class\n");
+ return PTR_ERR(vhm_class);
+ }
+ pr_info("vhm: device class registered correctly\n");
+
+ /* Register the device driver */
+ vhm_device = device_create(vhm_class, NULL, MKDEV(major, 0),
+ NULL, DEVICE_NAME);
+ if (IS_ERR(vhm_device)) {
+ class_destroy(vhm_class);
+ unregister_chrdev(major, DEVICE_NAME);
+ pr_warn("vhm: failed to create the device\n");
+ return PTR_ERR(vhm_device);
+ }
+
+ pr_info("vhm: Virtio & Hypervisor service module initialized\n");
+ return 0;
+}
+static void __exit vhm_exit(void)
+{
+ device_destroy(vhm_class, MKDEV(major, 0));
+ class_unregister(vhm_class);
+ class_destroy(vhm_class);
+ unregister_chrdev(major, DEVICE_NAME);
+ pr_info("vhm: exit\n");
+}
+
+module_init(vhm_init);
+module_exit(vhm_exit);
+
+MODULE_AUTHOR("Intel");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("This is a char device driver, acts as a route "
+ "responsible for transferring IO requsts from other modules "
+ "either in user-space or in kernel to and from hypervisor");
+MODULE_VERSION("0.1");
diff --git a/drivers/vhm/Makefile b/drivers/vhm/Makefile
new file mode 100644
index 000000000000..220697aaccb7
--- /dev/null
+++ b/drivers/vhm/Makefile
@@ -0,0 +1 @@
+obj-y += vhm_vm_mngt.o vhm_hypercall.o
diff --git a/drivers/vhm/vhm_hypercall.c b/drivers/vhm/vhm_hypercall.c
new file mode 100644
index 000000000000..ddc085d0fa11
--- /dev/null
+++ b/drivers/vhm/vhm_hypercall.c
@@ -0,0 +1,134 @@
+/*
+ * virtio and hyperviosr service module (VHM): hypercall wrap
+ *
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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 <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/vhm/acrn_hv_defs.h>
+#include <linux/vhm/vhm_hypercall.h>
+
+inline long vhm_create_vm(struct vhm_vm *vm, unsigned long ioctl_param)
+{
+ long ret = 0;
+ struct acrn_create_vm created_vm;
+
+ if (copy_from_user(&created_vm, (void *)ioctl_param,
+ sizeof(struct acrn_create_vm)))
+ return -EFAULT;
+
+ ret = acrn_hypercall2(HC_CREATE_VM, 0,
+ virt_to_phys(&created_vm));
+ if ((ret < 0) ||
+ (created_vm.vmid == ACRN_INVALID_VMID)) {
+ pr_err("vhm: failed to create VM from Hypervisor !\n");
+ return -EFAULT;
+ }
+
+ if (copy_to_user((void *)ioctl_param, &created_vm,
+ sizeof(struct acrn_create_vm)))
+ return -EFAULT;
+
+ vm->vmid = created_vm.vmid;
+ pr_info("vhm: VM %ld created\n", created_vm.vmid);
+
+ return ret;
+}
+
+inline long vhm_resume_vm(struct vhm_vm *vm)
+{
+ long ret = 0;
+
+ ret = acrn_hypercall1(HC_RESUME_VM, vm->vmid);
+ if (ret < 0) {
+ pr_err("vhm: failed to start VM %ld!\n", vm->vmid);
+ return -EFAULT;
+ }
+
+ return ret;
+}
+
+inline long vhm_pause_vm(struct vhm_vm *vm)
+{
+ long ret = 0;
+
+ ret = acrn_hypercall1(HC_PAUSE_VM, vm->vmid);
+ if (ret < 0) {
+ pr_err("vhm: failed to pause VM %ld!\n", vm->vmid);
+ return -EFAULT;
+ }
+
+ return ret;
+}
+
+inline long vhm_destroy_vm(struct vhm_vm *vm)
+{
+ long ret = 0;
+
+ ret = acrn_hypercall1(HC_DESTROY_VM, vm->vmid);
+ if (ret < 0) {
+ pr_err("failed to destroy VM %ld\n", vm->vmid);
+ return -EFAULT;
+ }
+ vm->vmid = ACRN_INVALID_VMID;
+
+ return ret;
+}
+
+inline long vhm_query_vm_state(struct vhm_vm *vm)
+{
+ long ret = 0;
+
+ ret = acrn_hypercall1(HC_QUERY_VMSTATE, vm->vmid);
+ if (ret < 0) {
+ pr_err("vhm: failed to query VM State%ld!\n", vm->vmid);
+ return -EFAULT;
+ }
+
+ return ret;
+}
diff --git a/drivers/vhm/vhm_vm_mngt.c b/drivers/vhm/vhm_vm_mngt.c
new file mode 100644
index 000000000000..61db04b57362
--- /dev/null
+++ b/drivers/vhm/vhm_vm_mngt.c
@@ -0,0 +1,107 @@
+/*
+ * virtio and hyperviosr service module (VHM): vm management
+ *
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ * Liang Ding <liang.ding@intel.com>
+ * Jason Zeng <jason.zeng@intel.com>
+ *
+ */
+
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/init.h>
+#include <asm/processor.h>
+#include <linux/vhm/acrn_hv_defs.h>
+#include <linux/vhm/vhm_ioctl_defs.h>
+#include <linux/vhm/vhm_hypercall.h>
+
+LIST_HEAD(vhm_vm_list);
+DEFINE_MUTEX(vhm_vm_list_lock);
+
+struct vhm_vm *find_get_vm(unsigned long vmid)
+{
+ struct vhm_vm *vm;
+
+ mutex_lock(&vhm_vm_list_lock);
+ list_for_each_entry(vm, &vhm_vm_list, list) {
+ if (vm->vmid == vmid) {
+ vm->refcnt++;
+ mutex_unlock(&vhm_vm_list_lock);
+ return vm;
+ }
+ }
+ mutex_unlock(&vhm_vm_list_lock);
+ return NULL;
+}
+
+void put_vm(struct vhm_vm *vm)
+{
+ mutex_lock(&vhm_vm_list_lock);
+ vm->refcnt--;
+ if (vm->refcnt == 0) {
+ list_del(&vm->list);
+ kfree(vm);
+ pr_info("vhm: freed vm\n");
+ }
+ mutex_unlock(&vhm_vm_list_lock);
+}
+
+void vm_list_add(struct list_head *list)
+{
+ list_add(list, &vhm_vm_list);
+}
+
+void vm_mutex_lock(struct mutex *mlock)
+{
+ mutex_lock(mlock);
+}
+
+void vm_mutex_unlock(struct mutex *mlock)
+{
+ mutex_unlock(mlock);
+}
diff --git a/include/linux/vhm/acrn_common.h b/include/linux/vhm/acrn_common.h
new file mode 100644
index 000000000000..08e47732f4d0
--- /dev/null
+++ b/include/linux/vhm/acrn_common.h
@@ -0,0 +1,65 @@
+/*
+ * virtio and hyperviosr service module (VHM): commom.h
+ *
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ */
+
+#ifndef ACRN_COMMON_H
+#define ACRN_COMMON_H
+
+/*
+ * Commmon structures for ACRN/VHM/DM
+ */
+
+/* Common API params */
+struct acrn_create_vm {
+ unsigned long vmid; /* OUT: HV return vmid to VHM */
+ unsigned long vcpu_num; /* IN: VM vcpu number */
+} __attribute__((aligned(8)));
+
+#endif /* ACRN_COMMON_H */
diff --git a/include/linux/vhm/acrn_hv_defs.h b/include/linux/vhm/acrn_hv_defs.h
new file mode 100644
index 000000000000..f338a8fbad3d
--- /dev/null
+++ b/include/linux/vhm/acrn_hv_defs.h
@@ -0,0 +1,81 @@
+/*
+ * virtio and hyperviosr service module (VHM): hypercall header
+ *
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ */
+
+#ifndef ACRN_HV_DEFS_H
+#define ACRN_HV_DEFS_H
+
+/*
+ * Commmon structures for ACRN/VHM/DM
+ */
+#include "acrn_common.h"
+
+/*
+ * Commmon structures for HV/VHM
+ */
+
+#define _HC_ID(x, y) (((x)<<24)|(y))
+
+#define HC_ID 0x7FUL
+
+/* VM management */
+#define HC_ID_VM_BASE 0x0UL
+#define HC_GET_API_VERSION _HC_ID(HC_ID, HC_ID_VM_BASE + 0x00)
+#define HC_CREATE_VM _HC_ID(HC_ID, HC_ID_VM_BASE + 0x01)
+#define HC_DESTROY_VM _HC_ID(HC_ID, HC_ID_VM_BASE + 0x02)
+#define HC_RESUME_VM _HC_ID(HC_ID, HC_ID_VM_BASE + 0x03)
+#define HC_PAUSE_VM _HC_ID(HC_ID, HC_ID_VM_BASE + 0x04)
+#define HC_QUERY_VMSTATE _HC_ID(HC_ID, HC_ID_VM_BASE + 0x05)
+
+#define ACRN_DOM0_VMID (0UL)
+#define ACRN_INVALID_VMID (-1UL)
+#define ACRN_INVALID_HPA (-1UL)
+
+#endif /* ACRN_HV_DEFS_H */
diff --git a/include/linux/vhm/vhm_hypercall.h b/include/linux/vhm/vhm_hypercall.h
new file mode 100644
index 000000000000..c07163dbc3bd
--- /dev/null
+++ b/include/linux/vhm/vhm_hypercall.h
@@ -0,0 +1,148 @@
+/*
+ * virtio and hyperviosr service module (VHM): hypercall.h
+ *
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ */
+
+#ifndef VHM_HYPERCALL_H
+#define VHM_HYPERCALL_H
+
+#include <linux/vhm/vhm_vm_mngt.h>
+
+static inline long acrn_hypercall0(unsigned long hyp_id)
+{
+
+ /* x86-64 System V ABI register usage */
+ register signed long result asm("rax");
+ register unsigned long r8 asm("r8") = hyp_id;
+
+ /* Execute vmcall */
+ asm volatile(".byte 0x0F,0x01,0xC1\n"
+ : "=r"(result)
+ : "r"(r8));
+
+ /* Return result to caller */
+ return result;
+}
+
+static inline long acrn_hypercall1(unsigned long hyp_id, unsigned long param1)
+{
+
+ /* x86-64 System V ABI register usage */
+ register signed long result asm("rax");
+ register unsigned long r8 asm("r8") = hyp_id;
+
+ /* Execute vmcall */
+ asm volatile(".byte 0x0F,0x01,0xC1\n"
+ : "=r"(result)
+ : "D"(param1), "r"(r8));
+
+ /* Return result to caller */
+ return result;
+}
+
+static inline long acrn_hypercall2(unsigned long hyp_id, unsigned long param1,
+ unsigned long param2)
+{
+
+ /* x86-64 System V ABI register usage */
+ register signed long result asm("rax");
+ register unsigned long r8 asm("r8") = hyp_id;
+
+ /* Execute vmcall */
+ asm volatile(".byte 0x0F,0x01,0xC1\n"
+ : "=r"(result)
+ : "D"(param1), "S"(param2), "r"(r8));
+
+ /* Return result to caller */
+ return result;
+}
+
+static inline long acrn_hypercall3(unsigned long hyp_id, unsigned long param1,
+ unsigned long param2, unsigned long param3)
+{
+
+ /* x86-64 System V ABI register usage */
+ register signed long result asm("rax");
+ register unsigned long r8 asm("r8") = hyp_id;
+
+ /* Execute vmcall */
+ asm volatile(".byte 0x0F,0x01,0xC1\n"
+ : "=r"(result)
+ : "D"(param1), "S"(param2), "d"(param3), "r"(r8));
+
+ /* Return result to caller */
+ return result;
+}
+
+static inline long acrn_hypercall4(unsigned long hyp_id, unsigned long param1,
+ unsigned long param2, unsigned long param3,
+ unsigned long param4)
+{
+
+ /* x86-64 System V ABI register usage */
+ register signed long result asm("rax");
+ register unsigned long r8 asm("r8") = hyp_id;
+
+ /* Execute vmcall */
+ asm volatile(".byte 0x0F,0x01,0xC1\n"
+ : "=r"(result)
+ : "D"(param1), "S"(param2), "d"(param3),
+ "c"(param4), "r"(r8));
+
+ /* Return result to caller */
+ return result;
+}
+
+inline long vhm_create_vm(struct vhm_vm *vm, unsigned long ioctl_param);
+inline long vhm_resume_vm(struct vhm_vm *vm);
+inline long vhm_pause_vm(struct vhm_vm *vm);
+inline long vhm_destroy_vm(struct vhm_vm *vm);
+inline long vhm_query_vm_state(struct vhm_vm *vm);
+
+#endif /* VHM_HYPERCALL_H */
diff --git a/include/linux/vhm/vhm_ioctl_defs.h b/include/linux/vhm/vhm_ioctl_defs.h
new file mode 100644
index 000000000000..d8c81b6e9306
--- /dev/null
+++ b/include/linux/vhm/vhm_ioctl_defs.h
@@ -0,0 +1,67 @@
+/*
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. 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 NETAPP, INC ``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 NETAPP, INC OR CONTRIBUTORS 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _VHM_IOCTL_DEFS_H_
+#define _VHM_IOCTL_DEFS_H_
+
+/* Commmon structures for ACRN/VHM/DM */
+#include "acrn_common.h"
+
+/*
+ * Commmon IOCTL ID defination for VHM/DM
+ */
+#define _IC_ID(x, y) (((x)<<24)|(y))
+#define IC_ID 0x5FUL
+
+/* VM management */
+#define IC_ID_VM_BASE 0x0UL
+#define IC_GET_API_VERSION _IC_ID(IC_ID, IC_ID_VM_BASE + 0x00)
+#define IC_CREATE_VM _IC_ID(IC_ID, IC_ID_VM_BASE + 0x01)
+#define IC_DESTROY_VM _IC_ID(IC_ID, IC_ID_VM_BASE + 0x02)
+#define IC_RESUME_VM _IC_ID(IC_ID, IC_ID_VM_BASE + 0x03)
+#define IC_PAUSE_VM _IC_ID(IC_ID, IC_ID_VM_BASE + 0x04)
+#define IC_QUERY_VMSTATE _IC_ID(IC_ID, IC_ID_VM_BASE + 0x05)
+
+#endif /* VHM_IOCTL_DEFS_H */
diff --git a/include/linux/vhm/vhm_vm_mngt.h b/include/linux/vhm/vhm_vm_mngt.h
new file mode 100644
index 000000000000..dcb246af561a
--- /dev/null
+++ b/include/linux/vhm/vhm_vm_mngt.h
@@ -0,0 +1,78 @@
+/*
+ * virtio and hyperviosr service module (VHM): vm management
+ *
+ * This file is provided under a dual BSD/GPLv2 license. When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Copyright (C) 2017 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ * Liang Ding <liang.ding@intel.com>
+ * Jason Zeng <jason.zeng@intel.com>
+ * Xiao Zheng <xiao.zheng@intel.com>
+ * Jason Chen CJ <jason.cj.chen@intel.com>
+ *
+ */
+#ifndef VHM_VM_MNGT_H
+#define VHM_VM_MNGT_H
+
+#include <linux/list.h>
+
+extern struct list_head vhm_vm_list;
+extern struct mutex vhm_vm_list_lock;
+
+struct vhm_vm {
+ struct device *dev;
+ struct list_head list;
+ unsigned long vmid;
+ long refcnt;
+};
+
+struct vhm_vm *find_get_vm(unsigned long vmid);
+void put_vm(struct vhm_vm *vm);
+
+void vm_list_add(struct list_head *list);
+void vm_mutex_lock(struct mutex *mlock);
+void vm_mutex_unlock(struct mutex *mlock);
+
+#endif
--
https://clearlinux.org