From patchwork Mon Sep 11 04:41:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 9946631 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 48A7E6035D for ; Mon, 11 Sep 2017 04:43:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E60D28ABB for ; Mon, 11 Sep 2017 04:43:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3313D28AD7; Mon, 11 Sep 2017 04:43:47 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9A8F428ABB for ; Mon, 11 Sep 2017 04:43:46 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1drGY5-00065v-Iu; Mon, 11 Sep 2017 04:42:17 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1drGY4-00063i-3Y for xen-devel@lists.xen.org; Mon, 11 Sep 2017 04:42:16 +0000 Received: from [85.158.137.68] by server-11.bemta-3.messagelabs.com id E8/48-01812-72416B95; Mon, 11 Sep 2017 04:42:15 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmkeJIrShJLcpLzFFi42Jpa+uQ11UT2RZ psH2VjsWSj4tZHBg9ju7+zRTAGMWamZeUX5HAmnG8+xdjwR3jipY7p9kbGF9pdjFycggJVEo0 3+hnB7ElBHgljiybwQphB0jMPPCMCaKml1Fi4r4YEJtNQF9ixeODQDUcHCICxhJtN527GLk4m AVWMElcaHjIBhIXFgiUOLzOC6ScRUBV4sD3yUwgYV4BW4mPJ3kgpstL7Gq7CLaJU8BOoq/lPT PEJluJtSsfsExg5F3AyLCKUaM4tagstUjXyEwvqSgzPaMkNzEzR9fQwFgvN7W4ODE9NScxqVg vOT93EyMwEOoZGBh3MDbs9TvEKMnBpCTK++74lkghvqT8lMqMxOKM+KLSnNTiQ4wyHBxKErx3 hbZFCgkWpaanVqRl5gBDEiYtwcGjJMIbBZLmLS5IzC3OTIdInWK05Di26fIfJo4DE64AyY6bd /8wCbHk5eelSonzTgdpEABpyCjNgxsHi5tLjLJSwryMDAwMQjwFqUW5mSWo8q8YxTkYlYR5L4 JM4cnMK4Hb+groICagg3gubQE5qCQRISXVwOj87Ny7KM4O8an7zr2Mn+u699RO+7nsTLOrGTX cQk83Xff/yrD2dnipIOO3+6VrHt4XmWx4WSTdzvXgA9kdi4UcK+fKG3GlpZhqRl9e4nzdpuFx kBpbuEmGXp78lx+R981jAi34Jd23mqSIqi2N/eybaLdsAZuRQPc99U0MhW3yyWnOhtHHlFiKM xINtZiLihMBDM4vKpYCAAA= X-Env-Sender: haozhong.zhang@intel.com X-Msg-Ref: server-8.tower-31.messagelabs.com!1505104925!114186094!4 X-Originating-IP: [134.134.136.31] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 33355 invoked from network); 11 Sep 2017 04:42:14 -0000 Received: from mga06.intel.com (HELO mga06.intel.com) (134.134.136.31) by server-8.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 11 Sep 2017 04:42:14 -0000 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP; 10 Sep 2017 21:42:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.42,376,1500966000"; d="scan'208"; a="1217079110" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.142]) by fmsmga002.fm.intel.com with ESMTP; 10 Sep 2017 21:42:11 -0700 From: Haozhong Zhang To: qemu-devel@nongnu.org, xen-devel@lists.xen.org Date: Mon, 11 Sep 2017 12:41:50 +0800 Message-Id: <20170911044157.15403-4-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170911044157.15403-1-haozhong.zhang@intel.com> References: <20170911044157.15403-1-haozhong.zhang@intel.com> Cc: Haozhong Zhang , Eduardo Habkost , "Michael S. Tsirkin" , Igor Mammedov , Chao Peng , Dan Williams Subject: [Xen-devel] [RFC QEMU PATCH v3 03/10] hostmem-xen: add a host memory backend for Xen X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP vNVDIMM requires a host memory backend to allocate its backend resources to the guest. When QEMU is used as Xen device model, the backend resource allocation of vNVDIMM is managed out of QEMU. A new host memory backend 'memory-backend-xen' is introduced to represent the backend resource allocated by Xen. It simply creates a memory region of the specified size as a placeholder in the guest address space, which will be mapped by Xen to the actual backend resource. Following example QEMU options create a vNVDIMM device backed by a 4GB host PMEM region at host physical address 0x100000000: -object memory-backend-xen,id=mem1,host-addr=0x100000000,size=4G -device nvdimm,id=nvdimm1,memdev=mem1 Signed-off-by: Haozhong Zhang --- Cc: Eduardo Habkost Cc: Igor Mammedov Cc: "Michael S. Tsirkin" --- backends/Makefile.objs | 1 + backends/hostmem-xen.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ backends/hostmem.c | 9 +++++ hw/mem/pc-dimm.c | 6 ++- 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 backends/hostmem-xen.c diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 0400799efd..3096fde21f 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -5,6 +5,7 @@ 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 common-obj-y += cryptodev.o common-obj-y += cryptodev-builtin.o diff --git a/backends/hostmem-xen.c b/backends/hostmem-xen.c new file mode 100644 index 0000000000..99211efd81 --- /dev/null +++ b/backends/hostmem-xen.c @@ -0,0 +1,108 @@ +/* + * QEMU Host Memory Backend for Xen + * + * Copyright(C) 2017 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 "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; + + uint64_t host_addr; +}; + +static void xen_backend_get_host_addr(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + HostMemoryBackendXen *backend = MEMORY_BACKEND_XEN(obj); + uint64_t value = backend->host_addr; + + visit_type_size(v, name, &value, errp); +} + +static void xen_backend_set_host_addr(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + HostMemoryBackend *backend = MEMORY_BACKEND(obj); + HostMemoryBackendXen *xb = MEMORY_BACKEND_XEN(obj); + Error *local_err = NULL; + uint64_t value; + + if (memory_region_size(&backend->mr)) { + error_setg(&local_err, "cannot change property value"); + goto out; + } + + visit_type_size(v, name, &value, &local_err); + if (local_err) { + goto out; + } + xb->host_addr = value; + + out: + error_propagate(errp, local_err); +} + +static void xen_backend_alloc(HostMemoryBackend *backend, Error **errp) +{ + if (!backend->size) { + error_setg(errp, "can't create backend with size 0"); + return; + } + memory_region_init(&backend->mr, OBJECT(backend), "hostmem-xen", + backend->size); + backend->mr.align = getpagesize(); +} + +static void xen_backend_class_init(ObjectClass *oc, void *data) +{ + HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); + + bc->alloc = xen_backend_alloc; + + object_class_property_add(oc, "host-addr", "int", + xen_backend_get_host_addr, + xen_backend_set_host_addr, + NULL, NULL, &error_abort); +} + +static const TypeInfo xen_backend_info = { + .name = TYPE_MEMORY_BACKEND_XEN, + .parent = TYPE_MEMORY_BACKEND, + .class_init = xen_backend_class_init, + .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 ee2c2d5bfd..ba13a52994 100644 --- a/backends/hostmem.c +++ b/backends/hostmem.c @@ -12,6 +12,7 @@ #include "qemu/osdep.h" #include "sysemu/hostmem.h" #include "hw/boards.h" +#include "hw/xen/xen.h" #include "qapi/error.h" #include "qapi/visitor.h" #include "qapi-types.h" @@ -277,6 +278,14 @@ 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); diff --git a/hw/mem/pc-dimm.c b/hw/mem/pc-dimm.c index bdf6649083..7e1fe005ee 100644 --- a/hw/mem/pc-dimm.c +++ b/hw/mem/pc-dimm.c @@ -28,6 +28,7 @@ #include "sysemu/kvm.h" #include "trace.h" #include "hw/virtio/vhost.h" +#include "hw/xen/xen.h" typedef struct pc_dimms_capacity { uint64_t size; @@ -108,7 +109,10 @@ void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, } memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr); - vmstate_register_ram(vmstate_mr, dev); + /* memory-backend-xen is not backed by RAM. */ + if (!xen_enabled()) { + vmstate_register_ram(vmstate_mr, dev); + } numa_set_mem_node_id(addr, memory_region_size(mr), dimm->node); out: