From patchwork Mon Oct 10 00:34:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 9368843 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 706606048F for ; Mon, 10 Oct 2016 00:43:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5BFFA28B42 for ; Mon, 10 Oct 2016 00:43:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4A6A728B51; Mon, 10 Oct 2016 00:43:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6A7F228B42 for ; Mon, 10 Oct 2016 00:43:08 +0000 (UTC) Received: from localhost ([::1]:46629 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btOgN-0003wD-6U for patchwork-qemu-devel@patchwork.kernel.org; Sun, 09 Oct 2016 20:43:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51038) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btOYl-0006BL-99 for qemu-devel@nongnu.org; Sun, 09 Oct 2016 20:35:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1btOYg-0003It-VM for qemu-devel@nongnu.org; Sun, 09 Oct 2016 20:35:14 -0400 Received: from mga04.intel.com ([192.55.52.120]:40048) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btOYg-0003IU-Ji for qemu-devel@nongnu.org; Sun, 09 Oct 2016 20:35:10 -0400 Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga104.fm.intel.com with ESMTP; 09 Oct 2016 17:35:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,469,1473145200"; d="scan'208";a="17867484" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.148]) by fmsmga005.fm.intel.com with ESMTP; 09 Oct 2016 17:35:08 -0700 From: Haozhong Zhang To: qemu-devel@nongnu.org, xen-devel@lists.xensource.com Date: Mon, 10 Oct 2016 08:34:21 +0800 Message-Id: <20161010003423.4333-7-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161010003423.4333-1-haozhong.zhang@intel.com> References: <20161010003423.4333-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.120 Subject: [Qemu-devel] [RFC QEMU PATCH 6/8] hostmem: add a host memory backend for Xen X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , Igor Mammedov , Xiao Guangrong , Eduardo Habkost , Konrad Rzeszutek Wilk Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Some virtual devices (e.g. NVDIMM) use the host memory backend to map its backend resources to the guest. When those devices are used on Xen, the mapping has to be managed out of QEMU. In order to reuse other parts of the implementation of those devices, we introduce a host memory backend for Xen (memory-backend-xen) which plays as a placeholder and does not map any backend resource. Therefore, those devices when used on Xen just needs to use the new backend. Following is an example of the command options of an NVDIMM device using a file backend on Xen: -object memory-backend-xen,id=mem1,mem-path=$FILENAME,size=$SIZE -device nvdimm,id=nvdimm1,memdev=mem1 Signed-off-by: Haozhong Zhang --- Cc: Eduardo Habkost Cc: Igor Mammedov --- backends/Makefile.objs | 1 + backends/hostmem-xen.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++ backends/hostmem.c | 9 ++++ 3 files changed, 130 insertions(+) create mode 100644 backends/hostmem-xen.c diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 31a3a89..a587aca 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -9,3 +9,4 @@ common-obj-$(CONFIG_TPM) += tpm.o common-obj-y += hostmem.o hostmem-ram.o common-obj-$(CONFIG_LINUX) += hostmem-file.o +common-obj-${CONFIG_XEN_BACKEND} += hostmem-xen.o diff --git a/backends/hostmem-xen.c b/backends/hostmem-xen.c new file mode 100644 index 0000000..144feee --- /dev/null +++ b/backends/hostmem-xen.c @@ -0,0 +1,120 @@ +/* + * QEMU Host Memory Backend for Xen + * + * Copyright(C) 2016 Intel Corporation. + * + * Author: + * Haozhong Zhang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + */ + +#include "qemu/osdep.h" +#include "qemu/mmap-alloc.h" +#include "sysemu/hostmem.h" +#include "qapi/error.h" +#include "qom/object_interfaces.h" + +#define TYPE_MEMORY_BACKEND_XEN "memory-backend-xen" + +#define MEMORY_BACKEND_XEN(obj) \ + OBJECT_CHECK(HostMemoryBackendXen, (obj), TYPE_MEMORY_BACKEND_XEN) + +typedef struct HostMemoryBackendXen HostMemoryBackendXen; + +struct HostMemoryBackendXen { + HostMemoryBackend parent_obj; + + char *mem_path; +}; + +static void xen_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) +{ + HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(backend); + int fd; + size_t page_size; + + if (!backend->size) { + error_setg(errp, "can't create backend with size 0"); + return; + } + memory_region_init(&backend->mr, + OBJECT(backend), db->mem_path, backend->size); + + fd = open(db->mem_path, O_RDONLY); + if (!fd) { + error_setg(errp, "can't open file %s, err %d", db->mem_path, errno); + return; + } + page_size = qemu_fd_getpagesize(fd); + backend->mr.align = MAX(page_size, QEMU_VMALLOC_ALIGN); + close(fd); +} + +static void xen_backend_class_init(ObjectClass *oc, void *data) +{ + HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); + + bc->alloc = xen_backend_memory_alloc; +} + +static char *get_mem_path(Object *o, Error **errp) +{ + HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(o); + + return g_strdup(db->mem_path); +} + +static void set_mem_path(Object *o, const char *str, Error **errp) +{ + HostMemoryBackend *backend = MEMORY_BACKEND(o); + HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(o); + + if (memory_region_size(&backend->mr)) { + error_setg(errp, "cannot change property value"); + return; + } + g_free(db->mem_path); + db->mem_path = g_strdup(str); +} + +static void +xen_backend_instance_init(Object *o) +{ + object_property_add_str(o, "mem-path", get_mem_path, + set_mem_path, NULL); +} + +static void xen_backend_instance_finalize(Object *o) +{ + HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(o); + + g_free(db->mem_path); +} + +static const TypeInfo xen_backend_info = { + .name = TYPE_MEMORY_BACKEND_XEN, + .parent = TYPE_MEMORY_BACKEND, + .class_init = xen_backend_class_init, + .instance_init = xen_backend_instance_init, + .instance_finalize = xen_backend_instance_finalize, + .instance_size = sizeof(HostMemoryBackendXen), +}; + +static void register_types(void) +{ + type_register_static(&xen_backend_info); +} + +type_init(register_types); diff --git a/backends/hostmem.c b/backends/hostmem.c index b7a208d..75a969d 100644 --- a/backends/hostmem.c +++ b/backends/hostmem.c @@ -27,6 +27,8 @@ QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND); QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE); #endif +#include "hw/xen/xen.h" + static void host_memory_backend_get_size(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) @@ -294,6 +296,13 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) goto out; } + /* The backend storage of MEMORY_BACKEND_XEN is managed by Xen, + * so no further work in this function is needed. + */ + if (xen_enabled() && !backend->mr.ram_block) { + goto out; + } + ptr = memory_region_get_ram_ptr(&backend->mr); sz = memory_region_size(&backend->mr);