From nobody Tue Apr 16 15:49:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1590078024; cv=none; d=zohomail.com; s=zohoarc; b=flh+3I3AHhHTYDV2yI9X9o5DsxZ+CnSMzneh8IbTmLTmH6M2ewb6LY1goOgRQhWO9lwHljHj5CFbhENfe86tS15orI0R43AGL+LeZEaFeQFuhllqHMopi4rXSQPz89oco9FsSxOg3NbZ09ZhiyjGpqBUhVSoXsBVO+/FxJ4Deao= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590078024; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Sl8/Ipswh+utcciRPDSVGPSr0kxIyFRjP4FJbLloSIY=; b=JpgqsNKqZ4hYne8tJjCFbm2wxvVgHcMBM5xiCCrlzHoY3jW5h38VOTcv+wWyEWTRpbpm7A/0X5YLLm6SJjf09tIqfxOqOg0g9zHPh5X5ccChVFqKe06rYQ9RKSzDz9H+Q9ZA3yyfDphX4wtHtNdpTcAnnIpIt2EOaw9w3T9atUA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1590078024125498.3130285164109; Thu, 21 May 2020 09:20:24 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvB-0000ud-Aa; Thu, 21 May 2020 16:19:49 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvA-0000uQ-2a for xen-devel@lists.xenproject.org; Thu, 21 May 2020 16:19:48 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e24521b0-9b7e-11ea-b07b-bc764e2007e4; Thu, 21 May 2020 16:19:45 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnv6-0006nZ-9o; Thu, 21 May 2020 16:19:44 +0000 Received: from 54-240-197-224.amazon.com ([54.240.197.224] helo=u2f063a87eabd5f.cbg10.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jbnv5-00088L-RV; Thu, 21 May 2020 16:19:44 +0000 X-Inumbo-ID: e24521b0-9b7e-11ea-b07b-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Sl8/Ipswh+utcciRPDSVGPSr0kxIyFRjP4FJbLloSIY=; b=tsUXmy0C9USvafkXAyIhtWgvvy z+N7xmDQxY/fMr85ZlyIV74Mo5iXVOP6Gx0UgADzl7NynfZKhTV+UzWOrzqAJKWoOzMm/+nJ6ID4h XgkUHYwhjuikcuQF+jAC2Ha/1snJM3UF9AGSbxvZy+Ty/qMtJ/GH0uYToKBv5ZDVLG2s=; From: Paul Durrant To: xen-devel@lists.xenproject.org Subject: [PATCH v5 1/5] xen/common: introduce a new framework for save/restore of 'domain' context Date: Thu, 21 May 2020 17:19:35 +0100 Message-Id: <20200521161939.4508-2-paul@xen.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200521161939.4508-1-paul@xen.org> References: <20200521161939.4508-1-paul@xen.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Paul Durrant , Andrew Cooper , Paul Durrant , Ian Jackson , George Dunlap , Jan Beulich , Volodymyr Babchuk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) To allow enlightened HVM guests (i.e. those that have PV drivers) to be migrated without their co-operation it will be necessary to transfer 'PV' state such as event channel state, grant entry state, etc. Currently there is a framework (entered via the hvm_save/load() functions) that allows a domain's 'HVM' (architectural) state to be transferred but 'PV' state is also common with pure PV guests and so this framework is not really suitable. This patch adds the new public header and low level implementation of a new common framework, entered via the domain_save/load() functions. Subsequent patches will introduce other parts of the framework, and code that will make use of it within the current version of the libxc migration stream. This patch also marks the HVM-only framework as deprecated in favour of the new framework. Signed-off-by: Paul Durrant Acked-by: Julien Grall Reviewed-by: Jan Beulich --- Cc: Andrew Cooper Cc: George Dunlap Cc: Ian Jackson Cc: Jan Beulich Cc: Stefano Stabellini Cc: Wei Liu Cc: Volodymyr Babchuk Cc: "Roger Pau Monn=C3=A9" v4: - Addressed further comments from Jan v3: - Addressed comments from Julien and Jan - Save handlers no longer need to state entry length up-front - Save handlers expected to deal with multiple instances internally - Entries are now auto-padded to 8 byte boundary v2: - Allow multi-stage save/load to avoid the need to double-buffer - Get rid of the masks and add an 'ignore' flag instead - Create copy function union to preserve const save buffer - Deprecate HVM-only framework --- xen/common/Makefile | 1 + xen/common/save.c | 314 +++++++++++++++++++++++++ xen/include/public/arch-arm/hvm/save.h | 5 + xen/include/public/arch-x86/hvm/save.h | 5 + xen/include/public/save.h | 89 +++++++ xen/include/xen/save.h | 170 +++++++++++++ 6 files changed, 584 insertions(+) create mode 100644 xen/common/save.c create mode 100644 xen/include/public/save.h create mode 100644 xen/include/xen/save.h diff --git a/xen/common/Makefile b/xen/common/Makefile index e8cde65370..90553ba5d7 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -37,6 +37,7 @@ obj-y +=3D radix-tree.o obj-y +=3D rbtree.o obj-y +=3D rcupdate.o obj-y +=3D rwlock.o +obj-y +=3D save.o obj-y +=3D shutdown.o obj-y +=3D softirq.o obj-y +=3D sort.o diff --git a/xen/common/save.c b/xen/common/save.c new file mode 100644 index 0000000000..f7b815b5ef --- /dev/null +++ b/xen/common/save.c @@ -0,0 +1,314 @@ +/* + * save.c: Save and restore PV guest state common to all domain types. + * + * Copyright Amazon.com Inc. or its affiliates. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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 f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#include +#include + +struct domain_context { + struct domain *domain; + const char *name; /* for logging purposes */ + struct domain_save_descriptor desc; + size_t len; /* for internal accounting */ + union { + const struct domain_save_ops *save; + const struct domain_load_ops *load; + } ops; + void *priv; +}; + +static struct { + const char *name; + domain_save_handler save; + domain_load_handler load; +} handlers[DOMAIN_SAVE_CODE_MAX + 1]; + +void __init domain_register_save_type(unsigned int typecode, + const char *name, + domain_save_handler save, + domain_load_handler load) +{ + BUG_ON(typecode >=3D ARRAY_SIZE(handlers)); + + ASSERT(!handlers[typecode].save); + ASSERT(!handlers[typecode].load); + + handlers[typecode].name =3D name; + handlers[typecode].save =3D save; + handlers[typecode].load =3D load; +} + +int domain_save_begin(struct domain_context *c, unsigned int typecode, + unsigned int instance) +{ + int rc; + + if ( typecode !=3D c->desc.typecode ) + { + ASSERT_UNREACHABLE(); + return -EINVAL; + } + ASSERT(!c->desc.length); /* Should always be zero during domain_save()= */ + ASSERT(!c->len); /* Verify domain_save_end() was called */ + + rc =3D c->ops.save->begin(c->priv, &c->desc); + if ( rc ) + return rc; + + return 0; +} + +int domain_save_data(struct domain_context *c, const void *src, size_t len) +{ + int rc =3D c->ops.save->append(c->priv, src, len); + + if ( !rc ) + c->len +=3D len; + + return rc; +} + +#define DOMAIN_SAVE_ALIGN 8 + +int domain_save_end(struct domain_context *c) +{ + struct domain *d =3D c->domain; + size_t len =3D ROUNDUP(c->len, DOMAIN_SAVE_ALIGN) - c->len; /* padding= */ + int rc; + + if ( len ) + { + static const uint8_t pad[DOMAIN_SAVE_ALIGN] =3D {}; + + rc =3D domain_save_data(c, pad, len); + + if ( rc ) + return rc; + } + ASSERT(IS_ALIGNED(c->len, DOMAIN_SAVE_ALIGN)); + + if ( c->name ) + gdprintk(XENLOG_INFO, "%pd save: %s[%u] +%zu (-%zu)\n", d, c->name, + c->desc.instance, c->len, len); + + rc =3D c->ops.save->end(c->priv, c->len); + c->len =3D 0; + + return rc; +} + +int domain_save(struct domain *d, const struct domain_save_ops *ops, + void *priv, bool dry_run) +{ + struct domain_context c =3D { + .domain =3D d, + .ops.save =3D ops, + .priv =3D priv, + }; + static const struct domain_save_header h =3D { + .magic =3D DOMAIN_SAVE_MAGIC, + .xen_major =3D XEN_VERSION, + .xen_minor =3D XEN_SUBVERSION, + .version =3D DOMAIN_SAVE_VERSION, + }; + const struct domain_save_end e =3D {}; + unsigned int i; + int rc; + + ASSERT(d !=3D current->domain); + domain_pause(d); + + c.name =3D !dry_run ? "HEADER" : NULL; + c.desc.typecode =3D DOMAIN_SAVE_CODE(HEADER); + + rc =3D DOMAIN_SAVE_ENTRY(HEADER, &c, 0, &h, sizeof(h)); + if ( rc ) + goto out; + + for ( i =3D 0; i < ARRAY_SIZE(handlers); i++ ) + { + domain_save_handler save =3D handlers[i].save; + + if ( !save ) + continue; + + c.name =3D !dry_run ? handlers[i].name : NULL; + memset(&c.desc, 0, sizeof(c.desc)); + c.desc.typecode =3D i; + + rc =3D save(d, &c, dry_run); + if ( rc ) + goto out; + } + + c.name =3D !dry_run ? "END" : NULL; + memset(&c.desc, 0, sizeof(c.desc)); + c.desc.typecode =3D DOMAIN_SAVE_CODE(END); + + rc =3D DOMAIN_SAVE_ENTRY(END, &c, 0, &e, sizeof(e)); + + out: + domain_unpause(d); + + return rc; +} + +int domain_load_begin(struct domain_context *c, unsigned int typecode, + unsigned int *instance) +{ + if ( typecode !=3D c->desc.typecode ) + { + ASSERT_UNREACHABLE(); + return -EINVAL; + } + + ASSERT(!c->len); /* Verify domain_load_end() was called */ + + *instance =3D c->desc.instance; + + return 0; +} + +int domain_load_data(struct domain_context *c, void *dst, size_t len) +{ + size_t copy_len =3D min_t(size_t, len, c->desc.length - c->len); + int rc; + + c->len +=3D copy_len; + ASSERT(c->len <=3D c->desc.length); + + rc =3D copy_len ? c->ops.load->read(c->priv, dst, copy_len) : 0; + if ( rc ) + return rc; + + /* Zero extend if the entry is exhausted */ + len -=3D copy_len; + if ( len ) + { + dst +=3D copy_len; + memset(dst, 0, len); + } + + return 0; +} + +int domain_load_end(struct domain_context *c) +{ + struct domain *d =3D c->domain; + size_t len =3D c->desc.length - c->len; + + while ( c->len !=3D c->desc.length ) /* unconsumed data or pad */ + { + uint8_t pad; + int rc =3D domain_load_data(c, &pad, sizeof(pad)); + + if ( rc ) + return rc; + + if ( pad ) + return -EINVAL; + } + + gdprintk(XENLOG_INFO, "%pd load: %s[%u] +%zu (-%zu)\n", d, c->name, + c->desc.instance, c->len, len); + + c->len =3D 0; + + return 0; +} + +int domain_load(struct domain *d, const struct domain_load_ops *ops, + void *priv) +{ + struct domain_context c =3D { + .domain =3D d, + .ops.load =3D ops, + .priv =3D priv, + }; + unsigned int instance; + struct domain_save_header h; + int rc; + + ASSERT(d !=3D current->domain); + + rc =3D c.ops.load->read(c.priv, &c.desc, sizeof(c.desc)); + if ( rc ) + return rc; + + c.name =3D "HEADER"; + + rc =3D DOMAIN_LOAD_ENTRY(HEADER, &c, &instance, &h, sizeof(h)); + if ( rc ) + return rc; + + if ( instance || h.magic !=3D DOMAIN_SAVE_MAGIC || + h.version !=3D DOMAIN_SAVE_VERSION ) + return -EINVAL; + + domain_pause(d); + + for (;;) + { + unsigned int i; + domain_load_handler load; + + rc =3D c.ops.load->read(c.priv, &c.desc, sizeof(c.desc)); + if ( rc ) + return rc; + + rc =3D -EINVAL; + + if ( c.desc.typecode =3D=3D DOMAIN_SAVE_CODE(END) ) + { + struct domain_save_end e; + + c.name =3D "END"; + + rc =3D DOMAIN_LOAD_ENTRY(END, &c, &instance, &e, sizeof(e)); + + if ( instance ) + return -EINVAL; + + break; + } + + i =3D c.desc.typecode; + if ( i >=3D ARRAY_SIZE(handlers) ) + break; + + c.name =3D handlers[i].name; + load =3D handlers[i].load; + + rc =3D load ? load(d, &c) : -EOPNOTSUPP; + if ( rc ) + break; + } + + domain_unpause(d); + + return rc; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/include/public/arch-arm/hvm/save.h b/xen/include/public/ar= ch-arm/hvm/save.h index 75b8e65bcb..d5b0c15203 100644 --- a/xen/include/public/arch-arm/hvm/save.h +++ b/xen/include/public/arch-arm/hvm/save.h @@ -26,6 +26,11 @@ #ifndef __XEN_PUBLIC_HVM_SAVE_ARM_H__ #define __XEN_PUBLIC_HVM_SAVE_ARM_H__ =20 +/* + * Further use of HVM state is deprecated. New state records should only + * be added to the domain state header: public/save.h + */ + #endif =20 /* diff --git a/xen/include/public/arch-x86/hvm/save.h b/xen/include/public/ar= ch-x86/hvm/save.h index 773a380bc2..e61e2dbcd7 100644 --- a/xen/include/public/arch-x86/hvm/save.h +++ b/xen/include/public/arch-x86/hvm/save.h @@ -648,6 +648,11 @@ struct hvm_msr { */ #define HVM_SAVE_CODE_MAX 20 =20 +/* + * Further use of HVM state is deprecated. New state records should only + * be added to the domain state header: public/save.h + */ + #endif /* __XEN_PUBLIC_HVM_SAVE_X86_H__ */ =20 /* diff --git a/xen/include/public/save.h b/xen/include/public/save.h new file mode 100644 index 0000000000..551dbbddb8 --- /dev/null +++ b/xen/include/public/save.h @@ -0,0 +1,89 @@ +/* + * save.h + * + * Structure definitions for common PV/HVM domain state that is held by + * Xen and must be saved along with the domain's memory. + * + * Copyright Amazon.com Inc. or its affiliates. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation = the + * rights to use, copy, modify, merge, publish, distribute, sublicense, an= d/or + * sell copies of the Software, and to permit persons to whom the Software= is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL= THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef XEN_PUBLIC_SAVE_H +#define XEN_PUBLIC_SAVE_H + +#if defined(__XEN__) || defined(__XEN_TOOLS__) + +#include "xen.h" + +/* Entry data is preceded by a descriptor */ +struct domain_save_descriptor { + uint16_t typecode; + + /* + * Instance number of the entry (since there may be multiple of some + * types of entries). + */ + uint16_t instance; + + /* Entry length not including this descriptor */ + uint32_t length; +}; + +/* + * Each entry has a type associated with it. DECLARE_DOMAIN_SAVE_TYPE + * binds these things together, although it is not intended that the + * resulting type is ever instantiated. + */ +#define DECLARE_DOMAIN_SAVE_TYPE(_x, _code, _type) \ + struct DOMAIN_SAVE_TYPE_##_x { char c[_code]; _type t; }; + +#define DOMAIN_SAVE_CODE(_x) \ + (sizeof(((struct DOMAIN_SAVE_TYPE_##_x *)0)->c)) +#define DOMAIN_SAVE_TYPE(_x) \ + typeof(((struct DOMAIN_SAVE_TYPE_##_x *)0)->t) + +/* + * All entries will be zero-padded to the next 64-bit boundary when saved, + * so there is no need to include trailing pad fields in structure + * definitions. + * When loading, entries will be zero-extended if the load handler reads + * beyond the length specified in the descriptor. + */ + +/* Terminating entry */ +struct domain_save_end {}; +DECLARE_DOMAIN_SAVE_TYPE(END, 0, struct domain_save_end); + +#define DOMAIN_SAVE_MAGIC 0x53415645 +#define DOMAIN_SAVE_VERSION 0x00000001 + +/* Initial entry */ +struct domain_save_header { + uint32_t magic; /* Must be DOMAIN_SAVE_MAGIC */ + uint16_t xen_major, xen_minor; /* Xen version */ + uint32_t version; /* Save format version */ +}; +DECLARE_DOMAIN_SAVE_TYPE(HEADER, 1, struct domain_save_header); + +#define DOMAIN_SAVE_CODE_MAX 1 + +#endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */ + +#endif /* XEN_PUBLIC_SAVE_H */ diff --git a/xen/include/xen/save.h b/xen/include/xen/save.h new file mode 100644 index 0000000000..f2e58bafef --- /dev/null +++ b/xen/include/xen/save.h @@ -0,0 +1,170 @@ +/* + * save.h: support routines for save/restore + * + * Copyright Amazon.com Inc. or its affiliates. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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 f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#ifndef XEN_SAVE_H +#define XEN_SAVE_H + +#include +#include +#include + +#include + +struct domain_context; + +int domain_save_begin(struct domain_context *c, unsigned int typecode, + unsigned int instance); + +#define DOMAIN_SAVE_BEGIN(x, c, i) \ + domain_save_begin((c), DOMAIN_SAVE_CODE(x), (i)) + +int domain_save_data(struct domain_context *c, const void *data, size_t le= n); +int domain_save_end(struct domain_context *c); + +static inline int domain_save_entry(struct domain_context *c, + unsigned int typecode, + unsigned int instance, const void *src, + size_t len) +{ + int rc; + + rc =3D domain_save_begin(c, typecode, instance); + if ( rc ) + return rc; + + rc =3D domain_save_data(c, src, len); + if ( rc ) + return rc; + + return domain_save_end(c); +} + +#define DOMAIN_SAVE_ENTRY(x, c, i, s, l) \ + domain_save_entry((c), DOMAIN_SAVE_CODE(x), (i), (s), (l)) + +int domain_load_begin(struct domain_context *c, unsigned int typecode, + unsigned int *instance); + +#define DOMAIN_LOAD_BEGIN(x, c, i) \ + domain_load_begin((c), DOMAIN_SAVE_CODE(x), (i)) + +int domain_load_data(struct domain_context *c, void *data, size_t len); +int domain_load_end(struct domain_context *c); + +static inline int domain_load_entry(struct domain_context *c, + unsigned int typecode, + unsigned int *instance, void *dst, + size_t len) +{ + int rc; + + rc =3D domain_load_begin(c, typecode, instance); + if ( rc ) + return rc; + + rc =3D domain_load_data(c, dst, len); + if ( rc ) + return rc; + + return domain_load_end(c); +} + +#define DOMAIN_LOAD_ENTRY(x, c, i, d, l) \ + domain_load_entry((c), DOMAIN_SAVE_CODE(x), (i), (d), (l)) + +/* + * The 'dry_run' flag indicates that the caller of domain_save() (see belo= w) + * is not trying to actually acquire the data, only the size of the data. + * The save handler can therefore limit work to only that which is necessa= ry + * to call domain_save_data() the correct number of times with accurate va= lues + * for 'len'. + */ +typedef int (*domain_save_handler)(const struct domain *d, + struct domain_context *c, + bool dry_run); +typedef int (*domain_load_handler)(struct domain *d, + struct domain_context *c); + +void domain_register_save_type(unsigned int typecode, const char *name, + domain_save_handler save, + domain_load_handler load); + +/* + * Register save and load handlers. + * + * Save handlers will be invoked in an order which copes with any inter- + * entry dependencies. For now this means that HEADER will come first and + * END will come last, all others being invoked in order of 'typecode'. + * + * Load handlers will be invoked in the order of entries present in the + * buffer. + */ +#define DOMAIN_REGISTER_SAVE_LOAD(x, s, l) \ + static int __init __domain_register_##x##_save_load(void) \ + { \ + domain_register_save_type( \ + DOMAIN_SAVE_CODE(x), \ + #x, \ + &(s), \ + &(l)); \ + \ + return 0; \ + } \ + __initcall(__domain_register_##x##_save_load); + +/* Callback functions */ +struct domain_save_ops { + /* + * Begin a new entry with the given descriptor (only type and instance + * are valid). + */ + int (*begin)(void *priv, const struct domain_save_descriptor *desc); + /* Append data/padding to the buffer */ + int (*append)(void *priv, const void *data, size_t len); + /* + * Complete the entry by updating the descriptor with the total + * length of the appended data (not including padding). + */ + int (*end)(void *priv, size_t len); +}; + +struct domain_load_ops { + /* Read data/padding from the buffer */ + int (*read)(void *priv, void *data, size_t len); +}; + +/* + * Entry points: + * + * ops: These are callback functions provided by the caller that will + * be used to write to (in the save case) or read from (in the + * load case) the context buffer. See above for more detail. + * priv: This is a pointer that will be passed to the copy function to + * allow it to identify the context buffer and the current state + * of the save or load operation. + * dry_run: If this is set then the caller of domain_save() is only trying + * to acquire the total size of the data, not the data itself. + * In this case the caller may supply different ops to avoid doing + * unnecessary work. + */ +int domain_save(struct domain *d, const struct domain_save_ops *ops, + void *priv, bool dry_run); +int domain_load(struct domain *d, const struct domain_load_ops *ops, + void *priv); + +#endif /* XEN_SAVE_H */ --=20 2.20.1 From nobody Tue Apr 16 15:49:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1590078030; cv=none; d=zohomail.com; s=zohoarc; b=TQ/YT6GHqiaosOUMshICyP1+ClR9lTaqjikvvU5jzLoQwSGP5yxp3ojWi1uq0x6rp/Mb4I5HDCONbUmyU6Z6//ThkzygQWnfDvY63yp6zQri+KYu2plMjpFA8kKhWylZ3im2s8TwU2vVlYGtpH3Qk/JqtjPWSkD5MMB89u4dIVM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590078030; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QfGmoXyhQuN/EVxQAxJlmVEjrcceHfUNWPHkPI0BpEQ=; b=bhP6Fs2+sj8GpoQzQEvaLbrzAipWiyiKbbfDqMv6ccIz/q33zDI6I2+OoWzIbaowW1FCW8FFBari2HfXm0YsmaGhZJYhDeLBdjyswd3QNZ851qqsjCArLL/x19z9POeUUJcyPP4aRXbVhp4jXTrYWaPGZQJE1+7b/X8E9dQWTHw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1590078030618973.6041325424759; Thu, 21 May 2020 09:20:30 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvL-0000ys-Ac; Thu, 21 May 2020 16:19:59 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvK-0000yS-3T for xen-devel@lists.xenproject.org; Thu, 21 May 2020 16:19:58 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e3bbed44-9b7e-11ea-b9cf-bc764e2007e4; Thu, 21 May 2020 16:19:47 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnv8-0006ne-1u; Thu, 21 May 2020 16:19:46 +0000 Received: from 54-240-197-224.amazon.com ([54.240.197.224] helo=u2f063a87eabd5f.cbg10.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jbnv7-00088L-OK; Thu, 21 May 2020 16:19:45 +0000 X-Inumbo-ID: e3bbed44-9b7e-11ea-b9cf-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=QfGmoXyhQuN/EVxQAxJlmVEjrcceHfUNWPHkPI0BpEQ=; b=0UcGuSWjabQ6qD4s4WMXkM5UJm JhbRjcH0xTpYT6vGQcXaa9L+rYKNtWZl+1l1QbYX6R7eyVmkvkdN7KxdYhlOncJYTSTGO5Dxp3BrJ 0JttAfT/5a3/Ng6JM8HMSd98J56qjwLtU0GCMlV6CFzimu4OY9Hy3bk8h8aXIGq8IzUw=; From: Paul Durrant To: xen-devel@lists.xenproject.org Subject: [PATCH v5 2/5] xen/common/domctl: introduce XEN_DOMCTL_get/setdomaincontext Date: Thu, 21 May 2020 17:19:36 +0100 Message-Id: <20200521161939.4508-3-paul@xen.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200521161939.4508-1-paul@xen.org> References: <20200521161939.4508-1-paul@xen.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Paul Durrant , Andrew Cooper , Paul Durrant , Ian Jackson , George Dunlap , Jan Beulich , Daniel De Graaf Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" These domctls provide a mechanism to get and set domain context from the toolstack. Signed-off-by: Paul Durrant Reviewed-by: Julien Grall --- Cc: Daniel De Graaf Cc: Ian Jackson Cc: Wei Liu Cc: Andrew Cooper Cc: George Dunlap Cc: Jan Beulich Cc: Stefano Stabellini v4: - Add missing zero pad checks v3: - Addressed comments from Julien and Jan - Use vmalloc() rather than xmalloc_bytes() v2: - drop mask parameter - const-ify some more buffers --- tools/flask/policy/modules/xen.if | 4 +- tools/libxc/include/xenctrl.h | 5 + tools/libxc/xc_domain.c | 56 +++++++++ xen/common/domctl.c | 173 ++++++++++++++++++++++++++++ xen/include/public/domctl.h | 41 +++++++ xen/xsm/flask/hooks.c | 6 + xen/xsm/flask/policy/access_vectors | 4 + 7 files changed, 287 insertions(+), 2 deletions(-) diff --git a/tools/flask/policy/modules/xen.if b/tools/flask/policy/modules= /xen.if index 8eb2293a52..2bc9db4f64 100644 --- a/tools/flask/policy/modules/xen.if +++ b/tools/flask/policy/modules/xen.if @@ -53,7 +53,7 @@ define(`create_domain_common', ` allow $1 $2:domain2 { set_cpu_policy settsc setscheduler setclaim set_vnumainfo get_vnumainfo cacheflush psr_cmt_op psr_alloc soft_reset - resource_map get_cpu_policy }; + resource_map get_cpu_policy setcontext }; allow $1 $2:security check_context; allow $1 $2:shadow enable; allow $1 $2:mmu { map_read map_write adjust memorymap physmap pinpage mmu= ext_op updatemp }; @@ -97,7 +97,7 @@ define(`migrate_domain_out', ` allow $1 $2:hvm { gethvmc getparam }; allow $1 $2:mmu { stat pageinfo map_read }; allow $1 $2:domain { getaddrsize getvcpucontext pause destroy }; - allow $1 $2:domain2 gettsc; + allow $1 $2:domain2 { gettsc getcontext }; allow $1 $2:shadow { enable disable logdirty }; ') =20 diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h index 45ff7db1e8..0ce2372e2f 100644 --- a/tools/libxc/include/xenctrl.h +++ b/tools/libxc/include/xenctrl.h @@ -867,6 +867,11 @@ int xc_domain_hvm_setcontext(xc_interface *xch, uint8_t *hvm_ctxt, uint32_t size); =20 +int xc_domain_getcontext(xc_interface *xch, uint32_t domid, + void *ctxt_buf, size_t *size); +int xc_domain_setcontext(xc_interface *xch, uint32_t domid, + const void *ctxt_buf, size_t size); + /** * This function will return guest IO ABI protocol * diff --git a/tools/libxc/xc_domain.c b/tools/libxc/xc_domain.c index 71829c2bce..e462a6f728 100644 --- a/tools/libxc/xc_domain.c +++ b/tools/libxc/xc_domain.c @@ -537,6 +537,62 @@ int xc_domain_hvm_setcontext(xc_interface *xch, return ret; } =20 +int xc_domain_getcontext(xc_interface *xch, uint32_t domid, + void *ctxt_buf, size_t *size) +{ + int ret; + DECLARE_DOMCTL =3D { + .cmd =3D XEN_DOMCTL_getdomaincontext, + .domain =3D domid, + .u.getdomaincontext.size =3D *size, + }; + DECLARE_HYPERCALL_BOUNCE(ctxt_buf, *size, XC_HYPERCALL_BUFFER_BOUNCE_O= UT); + + if ( xc_hypercall_bounce_pre(xch, ctxt_buf) ) + return -1; + + set_xen_guest_handle(domctl.u.setdomaincontext.buffer, ctxt_buf); + + ret =3D do_domctl(xch, &domctl); + + xc_hypercall_bounce_post(xch, ctxt_buf); + + if ( ret ) + return ret; + + *size =3D domctl.u.getdomaincontext.size; + if ( *size !=3D domctl.u.getdomaincontext.size ) + { + errno =3D EOVERFLOW; + return -1; + } + + return 0; +} + +int xc_domain_setcontext(xc_interface *xch, uint32_t domid, + const void *ctxt_buf, size_t size) +{ + int ret; + DECLARE_DOMCTL =3D { + .cmd =3D XEN_DOMCTL_setdomaincontext, + .domain =3D domid, + .u.setdomaincontext.size =3D size, + }; + DECLARE_HYPERCALL_BOUNCE_IN(ctxt_buf, size); + + if ( xc_hypercall_bounce_pre(xch, ctxt_buf) ) + return -1; + + set_xen_guest_handle(domctl.u.setdomaincontext.buffer, ctxt_buf); + + ret =3D do_domctl(xch, &domctl); + + xc_hypercall_bounce_post(xch, ctxt_buf); + + return ret; +} + int xc_vcpu_getcontext(xc_interface *xch, uint32_t domid, uint32_t vcpu, diff --git a/xen/common/domctl.c b/xen/common/domctl.c index a69b3b59a8..44758034a6 100644 --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include #include @@ -358,6 +360,168 @@ static struct vnuma_info *vnuma_init(const struct xen= _domctl_vnuma *uinfo, return ERR_PTR(ret); } =20 +struct domctl_context +{ + void *buffer; + struct domain_save_descriptor *desc; + size_t len; + size_t cur; +}; + +static int dry_run_append(void *priv, const void *data, size_t len) +{ + struct domctl_context *c =3D priv; + + if ( c->len + len < c->len ) + return -EOVERFLOW; + + c->len +=3D len; + + return 0; +} + +static int dry_run_begin(void *priv, const struct domain_save_descriptor *= desc) +{ + return dry_run_append(priv, NULL, sizeof(*desc)); +} + +static int dry_run_end(void *priv, size_t len) +{ + return 0; +} + +static struct domain_save_ops dry_run_ops =3D { + .begin =3D dry_run_begin, + .append =3D dry_run_append, + .end =3D dry_run_end, +}; + +static int save_begin(void *priv, const struct domain_save_descriptor *des= c) +{ + struct domctl_context *c =3D priv; + + if ( c->len - c->cur < sizeof(*desc) ) + return -ENOSPC; + + c->desc =3D c->buffer + c->cur; /* stash pointer to descriptor */ + *c->desc =3D *desc; + + c->cur +=3D sizeof(*desc); + + return 0; +} + +static int save_append(void *priv, const void *data, size_t len) +{ + struct domctl_context *c =3D priv; + + if ( c->len - c->cur < len ) + return -ENOSPC; + + memcpy(c->buffer + c->cur, data, len); + c->cur +=3D len; + + return 0; +} + +static int save_end(void *priv, size_t len) +{ + struct domctl_context *c =3D priv; + + c->desc->length =3D len; + + return 0; +} + +static struct domain_save_ops save_ops =3D { + .begin =3D save_begin, + .append =3D save_append, + .end =3D save_end, +}; + +static int getdomaincontext(struct domain *d, + struct xen_domctl_getdomaincontext *gdc) +{ + struct domctl_context c =3D { .buffer =3D ZERO_BLOCK_PTR }; + int rc; + + if ( d =3D=3D current->domain ) + return -EPERM; + + if ( gdc->pad ) + return -EINVAL; + + if ( guest_handle_is_null(gdc->buffer) ) /* query for buffer size */ + { + if ( gdc->size ) + return -EINVAL; + + /* dry run to acquire buffer size */ + rc =3D domain_save(d, &dry_run_ops, &c, true); + if ( rc ) + return rc; + + gdc->size =3D c.len; + return 0; + } + + c.len =3D gdc->size; + c.buffer =3D vmalloc(c.len); + if ( !c.buffer ) + return -ENOMEM; + + rc =3D domain_save(d, &save_ops, &c, false); + + gdc->size =3D c.cur; + if ( !rc && copy_to_guest(gdc->buffer, c.buffer, gdc->size) ) + rc =3D -EFAULT; + + vfree(c.buffer); + + return rc; +} + +static int load_read(void *priv, void *data, size_t len) +{ + struct domctl_context *c =3D priv; + + if ( c->len - c->cur < len ) + return -ENODATA; + + memcpy(data, c->buffer + c->cur, len); + c->cur +=3D len; + + return 0; +} + +static struct domain_load_ops load_ops =3D { + .read =3D load_read, +}; + +static int setdomaincontext(struct domain *d, + const struct xen_domctl_setdomaincontext *sdc) +{ + struct domctl_context c =3D { .buffer =3D ZERO_BLOCK_PTR, .len =3D sdc= ->size }; + int rc; + + if ( d =3D=3D current->domain ) + return -EPERM; + + if ( sdc->pad ) + return -EINVAL; + + c.buffer =3D vmalloc(c.len); + if ( !c.buffer ) + return -ENOMEM; + + rc =3D !copy_from_guest(c.buffer, sdc->buffer, c.len) ? + domain_load(d, &load_ops, &c) : -EFAULT; + + vfree(c.buffer); + + return rc; +} + long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) { long ret =3D 0; @@ -942,6 +1106,15 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u= _domctl) copyback =3D 1; break; =20 + case XEN_DOMCTL_getdomaincontext: + ret =3D getdomaincontext(d, &op->u.getdomaincontext); + copyback =3D !ret; + break; + + case XEN_DOMCTL_setdomaincontext: + ret =3D setdomaincontext(d, &op->u.setdomaincontext); + break; + default: ret =3D arch_do_domctl(op, d, u_domctl); break; diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 1ad34c35eb..1b133bda59 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -1129,6 +1129,43 @@ struct xen_domctl_vuart_op { */ }; =20 +/* + * XEN_DOMCTL_getdomaincontext + * --------------------------- + * + * buffer (IN): The buffer into which the context data should be + * copied, or NULL to query the buffer size that should + * be allocated. + * size (IN/OUT): If 'buffer' is NULL then the value passed in must be + * zero, and the value passed out will be the size of the + * buffer to allocate. + * If 'buffer' is non-NULL then the value passed in must + * be the size of the buffer into which data may be copied. + * The value passed out will be the size of data written. + */ +struct xen_domctl_getdomaincontext { + uint32_t size; + uint32_t pad; + XEN_GUEST_HANDLE_64(void) buffer; +}; + +/* XEN_DOMCTL_setdomaincontext + * --------------------------- + * + * buffer (IN): The buffer from which the context data should be + * copied. + * size (IN): The size of the buffer from which data may be copied. + * This data must include DOMAIN_SAVE_CODE_HEADER at the + * start and terminate with a DOMAIN_SAVE_CODE_END record. + * Any data beyond the DOMAIN_SAVE_CODE_END record will be + * ignored. + */ +struct xen_domctl_setdomaincontext { + uint32_t size; + uint32_t pad; + XEN_GUEST_HANDLE_64(const_void) buffer; +}; + struct xen_domctl { uint32_t cmd; #define XEN_DOMCTL_createdomain 1 @@ -1210,6 +1247,8 @@ struct xen_domctl { #define XEN_DOMCTL_vuart_op 81 #define XEN_DOMCTL_get_cpu_policy 82 #define XEN_DOMCTL_set_cpu_policy 83 +#define XEN_DOMCTL_getdomaincontext 84 +#define XEN_DOMCTL_setdomaincontext 85 #define XEN_DOMCTL_gdbsx_guestmemio 1000 #define XEN_DOMCTL_gdbsx_pausevcpu 1001 #define XEN_DOMCTL_gdbsx_unpausevcpu 1002 @@ -1270,6 +1309,8 @@ struct xen_domctl { struct xen_domctl_monitor_op monitor_op; struct xen_domctl_psr_alloc psr_alloc; struct xen_domctl_vuart_op vuart_op; + struct xen_domctl_getdomaincontext getdomaincontext; + struct xen_domctl_setdomaincontext setdomaincontext; uint8_t pad[128]; } u; }; diff --git a/xen/xsm/flask/hooks.c b/xen/xsm/flask/hooks.c index 4649e6fd95..6f3db276ef 100644 --- a/xen/xsm/flask/hooks.c +++ b/xen/xsm/flask/hooks.c @@ -745,6 +745,12 @@ static int flask_domctl(struct domain *d, int cmd) case XEN_DOMCTL_get_cpu_policy: return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__GET_CPU_POLI= CY); =20 + case XEN_DOMCTL_setdomaincontext: + return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SETCONTEXT); + + case XEN_DOMCTL_getdomaincontext: + return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__GETCONTEXT); + default: return avc_unknown_permission("domctl", cmd); } diff --git a/xen/xsm/flask/policy/access_vectors b/xen/xsm/flask/policy/acc= ess_vectors index c055c14c26..fccfb9de82 100644 --- a/xen/xsm/flask/policy/access_vectors +++ b/xen/xsm/flask/policy/access_vectors @@ -245,6 +245,10 @@ class domain2 resource_map # XEN_DOMCTL_get_cpu_policy get_cpu_policy +# XEN_DOMCTL_setdomaincontext + setcontext +# XEN_DOMCTL_getdomaincontext + getcontext } =20 # Similar to class domain, but primarily contains domctls related to HVM d= omains --=20 2.20.1 From nobody Tue Apr 16 15:49:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1590078020; cv=none; d=zohomail.com; s=zohoarc; b=kLjgPQFg2TbYYP0yMU80mfzt3oznGMZdALMn2MMvldQuk0pX3+EjbdLL8sI1RGpQuitcBarnvZCmK/GnD6On4sQOwgj9jPFlFRn3EftXRlcwvXeBDc5PMksNldhNXvJMGsC0yBFwTbLurgMpREuDYihakuJNZ/0woF15pq9HVHw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590078020; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gk5Uuo1wIab8QnJ+XZOG7n7mFLhPsfwOIEwveUEZ4oE=; b=ZULZKsNZeeG5ntjtZ6d1wfAuDUF3LyKzRvRt0DccnCXLiAUpFB+W72jwlwMur+V0XIMmTm3j1Zoasoxuz1vYHQDD5CTdZx7AzczMxWURnFF7mE/wgvrpcB6yIonlUPZ6c3Aw1vdhSxGVt4FaYg4Bpo2pB7obwxI7mm+LLGdWpYE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1590078020653109.13614544335462; Thu, 21 May 2020 09:20:20 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvG-0000wH-1M; Thu, 21 May 2020 16:19:54 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvF-0000vc-2l for xen-devel@lists.xenproject.org; Thu, 21 May 2020 16:19:53 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e39de952-9b7e-11ea-b9cf-bc764e2007e4; Thu, 21 May 2020 16:19:47 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnv9-0006nh-7J; Thu, 21 May 2020 16:19:47 +0000 Received: from 54-240-197-224.amazon.com ([54.240.197.224] helo=u2f063a87eabd5f.cbg10.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jbnv8-00088L-UP; Thu, 21 May 2020 16:19:47 +0000 X-Inumbo-ID: e39de952-9b7e-11ea-b9cf-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=gk5Uuo1wIab8QnJ+XZOG7n7mFLhPsfwOIEwveUEZ4oE=; b=UYXK/EzL0sRrOHyvc95H5YGi0v vcb9hjMzg38FDiq8G2dwW7rtDLsVRsdC7otPj3M1pRl8BQwk0rbP8wADa8KXg+OHABTu6mq/HZJPk s53wYp8jtYXUK7vprDdYGP4DjPwu9WcqpxxIohpJhEqK66JHkcR+elDKOV+M5jLSkqos=; From: Paul Durrant To: xen-devel@lists.xenproject.org Subject: [PATCH v5 3/5] tools/misc: add xen-domctx to present domain context Date: Thu, 21 May 2020 17:19:37 +0100 Message-Id: <20200521161939.4508-4-paul@xen.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200521161939.4508-1-paul@xen.org> References: <20200521161939.4508-1-paul@xen.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Paul Durrant , Ian Jackson , Wei Liu , Paul Durrant Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This tool is analogous to 'xen-hvmctx' which presents HVM context. Subsequent patches will add 'dump' functions when new records are introduced. Signed-off-by: Paul Durrant Acked-by: Ian Jackson --- Cc: Andrew Cooper Cc: Wei Liu NOTE: Ian requested ack from Andrew v3: - Re-worked to avoid copying onto stack - Added optional typecode and instance arguments v2: - Change name from 'xen-ctx' to 'xen-domctx' --- .gitignore | 1 + tools/misc/Makefile | 4 + tools/misc/xen-domctx.c | 200 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 tools/misc/xen-domctx.c diff --git a/.gitignore b/.gitignore index 7418ce9829..6da3030f0d 100644 --- a/.gitignore +++ b/.gitignore @@ -209,6 +209,7 @@ tools/misc/xen_cpuperf tools/misc/xen-cpuid tools/misc/xen-detect tools/misc/xen-diag +tools/misc/xen-domctx tools/misc/xen-tmem-list-parse tools/misc/xen-livepatch tools/misc/xenperf diff --git a/tools/misc/Makefile b/tools/misc/Makefile index 63947bfadc..ef25524354 100644 --- a/tools/misc/Makefile +++ b/tools/misc/Makefile @@ -30,6 +30,7 @@ INSTALL_SBIN +=3D xenpm INSTALL_SBIN +=3D xenwatchdogd INSTALL_SBIN +=3D xen-livepatch INSTALL_SBIN +=3D xen-diag +INSTALL_SBIN +=3D xen-domctx INSTALL_SBIN +=3D $(INSTALL_SBIN-y) =20 # Everything to be installed in a private bin/ @@ -108,6 +109,9 @@ xen-livepatch: xen-livepatch.o xen-diag: xen-diag.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) =20 +xen-domctx: xen-domctx.o + $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) + xen-lowmemd: xen-lowmemd.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(L= DLIBS_libxenstore) $(APPEND_LDFLAGS) =20 diff --git a/tools/misc/xen-domctx.c b/tools/misc/xen-domctx.c new file mode 100644 index 0000000000..243325dfce --- /dev/null +++ b/tools/misc/xen-domctx.c @@ -0,0 +1,200 @@ +/* + * xen-domctx.c + * + * Print out domain save records in a human-readable way. + * + * Copyright Amazon.com Inc. or its affiliates. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software= "), + * to deal in the Software without restriction, including without limitati= on + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static void *buf =3D NULL; +static size_t len, off; + +#define GET_PTR(_x) = \ + do { = \ + if ( len - off < sizeof(*(_x)) ) = \ + { = \ + fprintf(stderr, = \ + "error: need another %lu bytes, only %lu available\n",= \ + sizeof(*(_x)), len - off); = \ + exit(1); = \ + } = \ + (_x) =3D buf + off; = \ + } while (false); + +static void dump_header(void) +{ + DOMAIN_SAVE_TYPE(HEADER) *h; + + GET_PTR(h); + + printf(" HEADER: magic %#x, version %u\n", + h->magic, h->version); + +} + +static void dump_end(void) +{ + DOMAIN_SAVE_TYPE(END) *e; + + GET_PTR(e); + + printf(" END\n"); +} + +static void usage(const char *prog) +{ + fprintf(stderr, "usage: %s [ [ ]]\n", + prog); + exit(1); +} + +int main(int argc, char **argv) +{ + char *s, *e; + long domid; + long typecode =3D -1; + long instance =3D -1; + unsigned int entry; + xc_interface *xch; + int rc; + + if ( argc < 2 || argc > 4 ) + usage(argv[0]); + + s =3D e =3D argv[1]; + domid =3D strtol(s, &e, 0); + + if ( *s =3D=3D '\0' || *e !=3D '\0' || + domid < 0 || domid >=3D DOMID_FIRST_RESERVED ) + { + fprintf(stderr, "invalid domid '%s'\n", s); + exit(1); + } + + if ( argc >=3D 3 ) + { + s =3D e =3D argv[2]; + typecode =3D strtol(s, &e, 0); + + if ( *s =3D=3D '\0' || *e !=3D '\0' ) + { + fprintf(stderr, "invalid typecode '%s'\n", s); + exit(1); + } + } + + if ( argc =3D=3D 4 ) + { + s =3D e =3D argv[3]; + instance =3D strtol(s, &e, 0); + + if ( *s =3D=3D '\0' || *e !=3D '\0' ) + { + fprintf(stderr, "invalid instance '%s'\n", s); + exit(1); + } + } + + xch =3D xc_interface_open(0, 0, 0); + if ( !xch ) + { + fprintf(stderr, "error: can't open libxc handle\n"); + exit(1); + } + + rc =3D xc_domain_getcontext(xch, domid, NULL, &len); + if ( rc < 0 ) + { + fprintf(stderr, "error: can't get record length for dom %lu: %s\n", + domid, strerror(errno)); + exit(1); + } + + buf =3D malloc(len); + if ( !buf ) + { + fprintf(stderr, "error: can't allocate %lu bytes\n", len); + exit(1); + } + + rc =3D xc_domain_getcontext(xch, domid, buf, &len); + if ( rc < 0 ) + { + fprintf(stderr, "error: can't get domain record for dom %lu: %s\n", + domid, strerror(errno)); + exit(1); + } + off =3D 0; + + entry =3D 0; + for ( ; ; ) + { + struct domain_save_descriptor *desc; + + GET_PTR(desc); + + off +=3D sizeof(*desc); + + if ( (typecode < 0 || typecode =3D=3D desc->typecode) && + (instance < 0 || instance =3D=3D desc->instance) ) + { + printf("[%u] type: %u instance: %u length: %u\n", entry++, + desc->typecode, desc->instance, desc->length); + + switch (desc->typecode) + { + case DOMAIN_SAVE_CODE(HEADER): dump_header(); break; + case DOMAIN_SAVE_CODE(END): dump_end(); break; + default: + printf("Unknown type %u: skipping\n", desc->typecode); + break; + } + } + + if ( desc->typecode =3D=3D DOMAIN_SAVE_CODE(END) ) + break; + + off +=3D desc->length; + } + + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ --=20 2.20.1 From nobody Tue Apr 16 15:49:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1590078031; cv=none; d=zohomail.com; s=zohoarc; b=Bh7u2BoZqq/9zxHVnR+dT06CVOmgB6ICSVSYhFCYtWUcOaiXtJRN637/8qmWkK9sPpzDiStCJYJG0yfVs9/VUSVUoA9Bsx5oOeXGcrT/35d8SQHAEVgYOFIBOwMxFvmVZdndoS/MP+WcuYDlvL6OveptTTRGYcyIH/MEl/7PHCE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590078031; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/LS/E6IoF0MQgW42gOcJtVpnONoGSVfqwfRwEvxiDwc=; b=hYDQg0Vm2lyWUbUmIKmAGG9Vev+MuCHbkkEobxveviMdkSQoLRqfVz/rb6ii2rKH6wOadD3efyiH9OnIneY1zLNAqG2mXz3bX+pcTtgCEFsKZUyi3HOzcBPQV36AdsAgMCD2OBvVo8kApJjCoFvpFSukQSDIgw7WGn/PTKVx7sg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1590078031987770.0949231589464; Thu, 21 May 2020 09:20:31 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvQ-0001Qf-KH; Thu, 21 May 2020 16:20:04 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvP-0001DP-3j for xen-devel@lists.xenproject.org; Thu, 21 May 2020 16:20:03 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e4ea68a8-9b7e-11ea-b9cf-bc764e2007e4; Thu, 21 May 2020 16:19:49 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvA-0006nw-PB; Thu, 21 May 2020 16:19:48 +0000 Received: from 54-240-197-224.amazon.com ([54.240.197.224] helo=u2f063a87eabd5f.cbg10.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jbnvA-00088L-Fg; Thu, 21 May 2020 16:19:48 +0000 X-Inumbo-ID: e4ea68a8-9b7e-11ea-b9cf-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=/LS/E6IoF0MQgW42gOcJtVpnONoGSVfqwfRwEvxiDwc=; b=5Go9HNihDCxMPDKISozp/bPwXd bOYSDD1UcGPjly5CJX0D1Q10KHKtuXh6YDNajBkVYMGsZQvM7ZdrgpnHzywOc8+MdD2SnrJLxOqLm 8Jz41l/9biOMYzHObvpmDqLLwLsuzce145G/81elqBWI6MrQygYmUjL/UT3gcLNEibBk=; From: Paul Durrant To: xen-devel@lists.xenproject.org Subject: [PATCH v5 4/5] common/domain: add a domain context record for shared_info... Date: Thu, 21 May 2020 17:19:38 +0100 Message-Id: <20200521161939.4508-5-paul@xen.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200521161939.4508-1-paul@xen.org> References: <20200521161939.4508-1-paul@xen.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Andrew Cooper , Paul Durrant , Ian Jackson , George Dunlap , Jan Beulich Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paul Durrant ... and update xen-domctx to dump some information describing the record. NOTE: The domain may or may not be using the embedded vcpu_info array so ultimately separate context records will be added for vcpu_info when this becomes necessary. Signed-off-by: Paul Durrant --- Cc: Ian Jackson Cc: Wei Liu Cc: Andrew Cooper Cc: George Dunlap Cc: Jan Beulich Cc: Julien Grall Cc: Stefano Stabellini v5: - Addressed comments from Julien v4: - Addressed comments from Jan v3: - Actually dump some of the content of shared_info v2: - Drop the header change to define a 'Xen' page size and instead use a variable length struct now that the framework makes this is feasible - Guard use of 'has_32bit_shinfo' in common code with CONFIG_COMPAT --- tools/misc/xen-domctx.c | 78 +++++++++++++++++++++++++++++++++++++++ xen/common/domain.c | 65 ++++++++++++++++++++++++++++++++ xen/include/public/save.h | 13 ++++++- 3 files changed, 155 insertions(+), 1 deletion(-) diff --git a/tools/misc/xen-domctx.c b/tools/misc/xen-domctx.c index 243325dfce..6ead7ea89d 100644 --- a/tools/misc/xen-domctx.c +++ b/tools/misc/xen-domctx.c @@ -31,6 +31,7 @@ #include =20 #include +#include #include #include #include @@ -61,6 +62,82 @@ static void dump_header(void) =20 } =20 +static void print_binary(const char *prefix, const void *val, size_t size, + const char *suffix) +{ + printf("%s", prefix); + + while ( size-- ) + { + uint8_t octet =3D *(const uint8_t *)val++; + unsigned int i; + + for ( i =3D 0; i < 8; i++ ) + { + printf("%u", octet & 1); + octet >>=3D 1; + } + } + + printf("%s", suffix); +} + +static void dump_shared_info(void) +{ + DOMAIN_SAVE_TYPE(SHARED_INFO) *s; + bool has_32bit_shinfo; + shared_info_any_t *info; + unsigned int i, n; + + GET_PTR(s); + has_32bit_shinfo =3D s->flags & DOMAIN_SAVE_32BIT_SHINFO; + + printf(" SHARED_INFO: has_32bit_shinfo: %s buffer_size: %u\n", + has_32bit_shinfo ? "true" : "false", s->buffer_size); + + info =3D (shared_info_any_t *)s->buffer; + +#define GET_FIELD_PTR(_f) \ + (has_32bit_shinfo ? \ + (const void *)&(info->x32._f) : \ + (const void *)&(info->x64._f)) +#define GET_FIELD_SIZE(_f) \ + (has_32bit_shinfo ? sizeof(info->x32._f) : sizeof(info->x64._f)) +#define GET_FIELD(_f) \ + (has_32bit_shinfo ? info->x32._f : info->x64._f) + + n =3D has_32bit_shinfo ? + ARRAY_SIZE(info->x32.evtchn_pending) : + ARRAY_SIZE(info->x64.evtchn_pending); + + for ( i =3D 0; i < n; i++ ) + { + const char *prefix =3D !i ? + " evtchn_pending: " : + " "; + + print_binary(prefix, GET_FIELD_PTR(evtchn_pending[0]), + GET_FIELD_SIZE(evtchn_pending[0]), "\n"); + } + + for ( i =3D 0; i < n; i++ ) + { + const char *prefix =3D !i ? + " evtchn_mask: " : + " "; + + print_binary(prefix, GET_FIELD_PTR(evtchn_mask[0]), + GET_FIELD_SIZE(evtchn_mask[0]), "\n"); + } + + printf(" wc: version: %u sec: %u nsec: %u\n", + GET_FIELD(wc_version), GET_FIELD(wc_sec), GET_FIELD(wc_nsec)); + +#undef GET_FIELD +#undef GET_FIELD_SIZE +#undef GET_FIELD_PTR +} + static void dump_end(void) { DOMAIN_SAVE_TYPE(END) *e; @@ -173,6 +250,7 @@ int main(int argc, char **argv) switch (desc->typecode) { case DOMAIN_SAVE_CODE(HEADER): dump_header(); break; + case DOMAIN_SAVE_CODE(SHARED_INFO): dump_shared_info(); break; case DOMAIN_SAVE_CODE(END): dump_end(); break; default: printf("Unknown type %u: skipping\n", desc->typecode); diff --git a/xen/common/domain.c b/xen/common/domain.c index 7cc9526139..9d156da84d 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -1649,6 +1650,70 @@ int continue_hypercall_on_cpu( return 0; } =20 +static int save_shared_info(const struct domain *d, struct domain_context = *c, + bool dry_run) +{ + struct domain_shared_info_context ctxt =3D { +#ifdef CONFIG_COMPAT + .flags =3D has_32bit_shinfo(d) ? DOMAIN_SAVE_32BIT_SHINFO : 0, +#endif + .buffer_size =3D sizeof(shared_info_t), + }; + size_t hdr_size =3D offsetof(typeof(ctxt), buffer); + int rc; + + rc =3D DOMAIN_SAVE_BEGIN(SHARED_INFO, c, 0); + if ( rc ) + return rc; + + rc =3D domain_save_data(c, &ctxt, hdr_size); + if ( rc ) + return rc; + + rc =3D domain_save_data(c, d->shared_info, ctxt.buffer_size); + if ( rc ) + return rc; + + return domain_save_end(c); +} + +static int load_shared_info(struct domain *d, struct domain_context *c) +{ + struct domain_shared_info_context ctxt; + size_t hdr_size =3D offsetof(typeof(ctxt), buffer); + unsigned int i; + int rc; + + rc =3D DOMAIN_LOAD_BEGIN(SHARED_INFO, c, &i); + if ( rc ) + return rc; + + if ( i ) /* expect only a single instance */ + return -ENXIO; + + rc =3D domain_load_data(c, &ctxt, hdr_size); + if ( rc ) + return rc; + + if ( ctxt.buffer_size !=3D sizeof(shared_info_t) ) + return -EINVAL; + + if ( ctxt.flags & DOMAIN_SAVE_32BIT_SHINFO ) +#ifdef CONFIG_COMPAT + has_32bit_shinfo(d) =3D true; +#else + return -EINVAL; +#endif + + rc =3D domain_load_data(c, d->shared_info, sizeof(shared_info_t)); + if ( rc ) + return rc; + + return domain_load_end(c); +} + +DOMAIN_REGISTER_SAVE_LOAD(SHARED_INFO, save_shared_info, load_shared_info); + /* * Local variables: * mode: C diff --git a/xen/include/public/save.h b/xen/include/public/save.h index 551dbbddb8..0e855a4b97 100644 --- a/xen/include/public/save.h +++ b/xen/include/public/save.h @@ -82,7 +82,18 @@ struct domain_save_header { }; DECLARE_DOMAIN_SAVE_TYPE(HEADER, 1, struct domain_save_header); =20 -#define DOMAIN_SAVE_CODE_MAX 1 +struct domain_shared_info_context { + uint32_t flags; + +#define DOMAIN_SAVE_32BIT_SHINFO 0x00000001 + + uint32_t buffer_size; + uint8_t buffer[XEN_FLEX_ARRAY_DIM]; /* Implementation specific size */ +}; + +DECLARE_DOMAIN_SAVE_TYPE(SHARED_INFO, 2, struct domain_shared_info_context= ); + +#define DOMAIN_SAVE_CODE_MAX 2 =20 #endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */ =20 --=20 2.20.1 From nobody Tue Apr 16 15:49:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1590078023; cv=none; d=zohomail.com; s=zohoarc; b=DSq+oyw2cRHlCcm7OXmcvk0qi2F7F3Uyihia3cpQtnZXqn3qnLKB38Wzmxa6QfuhqVkdvcKN+54x7q3Etk8AHXsdUGTla0jF1CE4ItK4iiZu9X+cjxUweregJsTpOs4BZMxEOfEfkyRfbPqcih7P3DqQdtxv3PNEQ6bQhKkRLFs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590078023; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6HfwJMUCo4w2pkMGpLCkcV69ZEzum7mNXZZZUP2qHUY=; b=ckmYo37k86bX4EBCbOQnex7kqQ2qEw1lTKN6zmzthNdWZpJOJcdYDuYV7Rcd10LjXs88jMxwJRTDfIfT8MK5T5k8Pvhn0hQiPRJV0JXgylcy4SbNSD2NUUV6vmbtEYlkmQXOSWxfvM8S23YvdVGWTP785VXiLd2XFwvCmk0t9OA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1590078023263445.89945622183245; Thu, 21 May 2020 09:20:23 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvF-0000vo-Iw; Thu, 21 May 2020 16:19:53 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvD-0000vL-Qp for xen-devel@lists.xenproject.org; Thu, 21 May 2020 16:19:51 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id e5567c29-9b7e-11ea-ab32-12813bfff9fa; Thu, 21 May 2020 16:19:50 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jbnvB-0006o2-Ul; Thu, 21 May 2020 16:19:49 +0000 Received: from 54-240-197-224.amazon.com ([54.240.197.224] helo=u2f063a87eabd5f.cbg10.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jbnvB-00088L-Lj; Thu, 21 May 2020 16:19:49 +0000 X-Inumbo-ID: e5567c29-9b7e-11ea-ab32-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=6HfwJMUCo4w2pkMGpLCkcV69ZEzum7mNXZZZUP2qHUY=; b=u6DDMzGH5Gxw3WKDRDRVj/TT2I D9Ca7wBKZU4NZyJ2TnScvbO/DttCdZIEjKzkCr72QID+AeZvrDvlnH9tAQCGs1lCgIs8Y/OWM6cWS loDLyFvww/WSB7fTayunAQYoroBLDAHrQRIxIOGfw/vxBS7dMCa1gKWiGdES8Sakke+o=; From: Paul Durrant To: xen-devel@lists.xenproject.org Subject: [PATCH v5 5/5] tools/libxc: make use of domain context SHARED_INFO record... Date: Thu, 21 May 2020 17:19:39 +0100 Message-Id: <20200521161939.4508-6-paul@xen.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200521161939.4508-1-paul@xen.org> References: <20200521161939.4508-1-paul@xen.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Paul Durrant , Ian Jackson , Wei Liu , Paul Durrant Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" ... in the save/restore code. This patch replaces direct mapping of the shared_info_frame (retrieved using XEN_DOMCTL_getdomaininfo) with save/load of the domain context SHARED_INFO record. No modifications are made to the definition of the migration stream at this point. Subsequent patches will define a record in the libxc domain image format for passing domain context and convert the save/restore code to use that. Signed-off-by: Paul Durrant --- Cc: Andrew Cooper Cc: Ian Jackson Cc: Wei Liu NOTE: Ian requested ack from Andrew v5: - Added BUILD_BUG_ON() in write_shared_info() to ensure copied data is not bigger than the record buffer v4: - write_shared_info() now needs to allocate the record data since the shared info buffer is smaller than PAGE_SIZE v3: - Moved basic get/set domain context functions to common code v2: - Re-based (now making use of DOMAIN_SAVE_FLAG_IGNORE) --- tools/libxc/xc_sr_common.c | 67 +++++++++++++++++++++++++++ tools/libxc/xc_sr_common.h | 11 ++++- tools/libxc/xc_sr_common_x86_pv.c | 74 ++++++++++++++++++++++++++++++ tools/libxc/xc_sr_common_x86_pv.h | 3 ++ tools/libxc/xc_sr_restore_x86_pv.c | 26 ++++------- tools/libxc/xc_sr_save_x86_pv.c | 44 ++++++++---------- tools/libxc/xg_save_restore.h | 1 + 7 files changed, 182 insertions(+), 44 deletions(-) diff --git a/tools/libxc/xc_sr_common.c b/tools/libxc/xc_sr_common.c index dd9a11b4b5..1acb3765aa 100644 --- a/tools/libxc/xc_sr_common.c +++ b/tools/libxc/xc_sr_common.c @@ -138,6 +138,73 @@ int read_record(struct xc_sr_context *ctx, int fd, str= uct xc_sr_record *rec) return 0; }; =20 +int get_domain_context(struct xc_sr_context *ctx) +{ + xc_interface *xch =3D ctx->xch; + size_t len =3D 0; + int rc; + + if ( ctx->domain_context.buffer ) + { + ERROR("Domain context already present"); + return -1; + } + + rc =3D xc_domain_getcontext(xch, ctx->domid, NULL, &len); + if ( rc < 0 ) + { + PERROR("Unable to get size of domain context"); + return -1; + } + + ctx->domain_context.buffer =3D malloc(len); + if ( !ctx->domain_context.buffer ) + { + PERROR("Unable to allocate memory for domain context"); + return -1; + } + + rc =3D xc_domain_getcontext(xch, ctx->domid, ctx->domain_context.buffe= r, + &len); + if ( rc < 0 ) + { + PERROR("Unable to get domain context"); + return -1; + } + + ctx->domain_context.len =3D len; + + return 0; +} + +int set_domain_context(struct xc_sr_context *ctx) +{ + xc_interface *xch =3D ctx->xch; + int rc; + + if ( !ctx->domain_context.buffer ) + { + ERROR("Domain context not present"); + return -1; + } + + rc =3D xc_domain_setcontext(xch, ctx->domid, ctx->domain_context.buffe= r, + ctx->domain_context.len); + + if ( rc < 0 ) + { + PERROR("Unable to set domain context"); + return -1; + } + + return 0; +} + +void common_cleanup(struct xc_sr_context *ctx) +{ + free(ctx->domain_context.buffer); +} + static void __attribute__((unused)) build_assertions(void) { BUILD_BUG_ON(sizeof(struct xc_sr_ihdr) !=3D 24); diff --git a/tools/libxc/xc_sr_common.h b/tools/libxc/xc_sr_common.h index 5dd51ccb15..0d61978b08 100644 --- a/tools/libxc/xc_sr_common.h +++ b/tools/libxc/xc_sr_common.h @@ -208,6 +208,11 @@ struct xc_sr_context =20 xc_dominfo_t dominfo; =20 + struct { + void *buffer; + unsigned int len; + } domain_context; + union /* Common save or restore data. */ { struct /* Save data. */ @@ -314,7 +319,7 @@ struct xc_sr_context /* The guest pfns containing the p2m leaves */ xen_pfn_t *p2m_pfns; =20 - /* Read-only mapping of guests shared info page */ + /* Pointer to shared_info (located in context buffer) */ shared_info_any_t *shinfo; =20 /* p2m generation count for verifying validity of local p2= m. */ @@ -425,6 +430,10 @@ int read_record(struct xc_sr_context *ctx, int fd, str= uct xc_sr_record *rec); int populate_pfns(struct xc_sr_context *ctx, unsigned int count, const xen_pfn_t *original_pfns, const uint32_t *types); =20 +int get_domain_context(struct xc_sr_context *ctx); +int set_domain_context(struct xc_sr_context *ctx); +void common_cleanup(struct xc_sr_context *ctx); + #endif /* * Local variables: diff --git a/tools/libxc/xc_sr_common_x86_pv.c b/tools/libxc/xc_sr_common_x= 86_pv.c index d3d425cb82..69d9b142b8 100644 --- a/tools/libxc/xc_sr_common_x86_pv.c +++ b/tools/libxc/xc_sr_common_x86_pv.c @@ -182,6 +182,80 @@ int x86_pv_map_m2p(struct xc_sr_context *ctx) return rc; } =20 +int x86_pv_get_shinfo(struct xc_sr_context *ctx) +{ + xc_interface *xch =3D ctx->xch; + unsigned int off =3D 0; + int rc; + +#define GET_PTR(_x) = \ + do { = \ + if ( ctx->domain_context.len - off < sizeof(*(_x)) ) = \ + { = \ + ERROR("Need another %lu bytes of context, only %u available\n"= , \ + sizeof(*(_x)), ctx->domain_context.len - off); = \ + return -1; = \ + } = \ + (_x) =3D ctx->domain_context.buffer + off; = \ + } while (false); + + rc =3D get_domain_context(ctx); + if ( rc ) + return rc; + + for ( ; ; ) + { + struct domain_save_descriptor *desc; + + GET_PTR(desc); + + off +=3D sizeof(*desc); + + switch (desc->typecode) + { + case DOMAIN_SAVE_CODE(SHARED_INFO): + { + DOMAIN_SAVE_TYPE(SHARED_INFO) *s; + + GET_PTR(s); + + ctx->x86.pv.shinfo =3D (shared_info_any_t *)s->buffer; + break; + } + default: + break; + } + + if ( desc->typecode =3D=3D DOMAIN_SAVE_CODE(END) ) + break; + + off +=3D desc->length; + } + + if ( !ctx->x86.pv.shinfo ) + { + ERROR("Failed to get SHARED_INFO\n"); + return -1; + } + + return 0; + +#undef GET_PTR +} + +int x86_pv_set_shinfo(struct xc_sr_context *ctx) +{ + xc_interface *xch =3D ctx->xch; + + if ( !ctx->x86.pv.shinfo ) + { + ERROR("SHARED_INFO buffer not present\n"); + return -1; + } + + return set_domain_context(ctx); +} + /* * Local variables: * mode: C diff --git a/tools/libxc/xc_sr_common_x86_pv.h b/tools/libxc/xc_sr_common_x= 86_pv.h index 2ed03309af..01442f48fb 100644 --- a/tools/libxc/xc_sr_common_x86_pv.h +++ b/tools/libxc/xc_sr_common_x86_pv.h @@ -97,6 +97,9 @@ int x86_pv_domain_info(struct xc_sr_context *ctx); */ int x86_pv_map_m2p(struct xc_sr_context *ctx); =20 +int x86_pv_get_shinfo(struct xc_sr_context *ctx); +int x86_pv_set_shinfo(struct xc_sr_context *ctx); + #endif /* * Local variables: diff --git a/tools/libxc/xc_sr_restore_x86_pv.c b/tools/libxc/xc_sr_restore= _x86_pv.c index 904ccc462a..21982a38ad 100644 --- a/tools/libxc/xc_sr_restore_x86_pv.c +++ b/tools/libxc/xc_sr_restore_x86_pv.c @@ -865,7 +865,7 @@ static int handle_shared_info(struct xc_sr_context *ctx, xc_interface *xch =3D ctx->xch; unsigned int i; int rc =3D -1; - shared_info_any_t *guest_shinfo =3D NULL; + shared_info_any_t *guest_shinfo; const shared_info_any_t *old_shinfo =3D rec->data; =20 if ( !ctx->x86.pv.restore.seen_pv_info ) @@ -878,18 +878,14 @@ static int handle_shared_info(struct xc_sr_context *c= tx, { ERROR("X86_PV_SHARED_INFO record wrong size: length %u" ", expected 4096", rec->length); - goto err; + return -1; } =20 - guest_shinfo =3D xc_map_foreign_range( - xch, ctx->domid, PAGE_SIZE, PROT_READ | PROT_WRITE, - ctx->dominfo.shared_info_frame); - if ( !guest_shinfo ) - { - PERROR("Failed to map Shared Info at mfn %#lx", - ctx->dominfo.shared_info_frame); - goto err; - } + rc =3D x86_pv_get_shinfo(ctx); + if ( rc ) + return rc; + + guest_shinfo =3D ctx->x86.pv.shinfo; =20 MEMCPY_FIELD(guest_shinfo, old_shinfo, vcpu_info, ctx->x86.pv.width); MEMCPY_FIELD(guest_shinfo, old_shinfo, arch, ctx->x86.pv.width); @@ -904,13 +900,7 @@ static int handle_shared_info(struct xc_sr_context *ct= x, =20 MEMSET_ARRAY_FIELD(guest_shinfo, evtchn_mask, 0xff, ctx->x86.pv.width); =20 - rc =3D 0; - - err: - if ( guest_shinfo ) - munmap(guest_shinfo, PAGE_SIZE); - - return rc; + return x86_pv_set_shinfo(ctx); } =20 /* restore_ops function. */ diff --git a/tools/libxc/xc_sr_save_x86_pv.c b/tools/libxc/xc_sr_save_x86_p= v.c index f3ccf5bb4b..fdd172b639 100644 --- a/tools/libxc/xc_sr_save_x86_pv.c +++ b/tools/libxc/xc_sr_save_x86_pv.c @@ -9,25 +9,6 @@ static inline bool is_canonical_address(xen_vaddr_t vaddr) return ((int64_t)vaddr >> 47) =3D=3D ((int64_t)vaddr >> 63); } =20 -/* - * Maps the guests shared info page. - */ -static int map_shinfo(struct xc_sr_context *ctx) -{ - xc_interface *xch =3D ctx->xch; - - ctx->x86.pv.shinfo =3D xc_map_foreign_range( - xch, ctx->domid, PAGE_SIZE, PROT_READ, ctx->dominfo.shared_info_fr= ame); - if ( !ctx->x86.pv.shinfo ) - { - PERROR("Failed to map shared info frame at mfn %#lx", - ctx->dominfo.shared_info_frame); - return -1; - } - - return 0; -} - /* * Copy a list of mfns from a guest, accounting for differences between gu= est * and toolstack width. Can fail if truncation would occur. @@ -854,13 +835,27 @@ static int write_x86_pv_p2m_frames(struct xc_sr_conte= xt *ctx) */ static int write_shared_info(struct xc_sr_context *ctx) { + xc_interface *xch =3D ctx->xch; struct xc_sr_record rec =3D { .type =3D REC_TYPE_SHARED_INFO, .length =3D PAGE_SIZE, - .data =3D ctx->x86.pv.shinfo, }; + int rc; =20 - return write_record(ctx, &rec); + if ( !(rec.data =3D calloc(1, PAGE_SIZE)) ) + { + ERROR("Cannot allocate buffer for SHARED_INFO data"); + return -1; + } + + BUILD_BUG_ON(sizeof(*ctx->x86.pv.shinfo) > PAGE_SIZE); + memcpy(rec.data, ctx->x86.pv.shinfo, sizeof(*ctx->x86.pv.shinfo)); + + rc =3D write_record(ctx, &rec); + + free(rec.data); + + return rc; } =20 /* @@ -1041,7 +1036,7 @@ static int x86_pv_setup(struct xc_sr_context *ctx) if ( rc ) return rc; =20 - rc =3D map_shinfo(ctx); + rc =3D x86_pv_get_shinfo(ctx); if ( rc ) return rc; =20 @@ -1112,12 +1107,11 @@ static int x86_pv_cleanup(struct xc_sr_context *ctx) if ( ctx->x86.pv.p2m ) munmap(ctx->x86.pv.p2m, ctx->x86.pv.p2m_frames * PAGE_SIZE); =20 - if ( ctx->x86.pv.shinfo ) - munmap(ctx->x86.pv.shinfo, PAGE_SIZE); - if ( ctx->x86.pv.m2p ) munmap(ctx->x86.pv.m2p, ctx->x86.pv.nr_m2p_frames * PAGE_SIZE); =20 + common_cleanup(ctx); + return 0; } =20 diff --git a/tools/libxc/xg_save_restore.h b/tools/libxc/xg_save_restore.h index 303081df0d..296b523963 100644 --- a/tools/libxc/xg_save_restore.h +++ b/tools/libxc/xg_save_restore.h @@ -19,6 +19,7 @@ =20 #include #include +#include =20 /* ** We process save/restore/migrate in batches of pages; the below --=20 2.20.1