1 /******************************************************************************
2  * include/asm-x86/mem_sharing.h
3  *
4  * Memory sharing support.
5  *
6  * Copyright (c) 2009 Citrix Systems, Inc. (Grzegorz Milos)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; If not, see <http://www.gnu.org/licenses/>.
20  */
21 #ifndef __MEM_SHARING_H__
22 #define __MEM_SHARING_H__
23 
24 #include <public/domctl.h>
25 #include <public/memory.h>
26 
27 #ifdef CONFIG_MEM_SHARING
28 
29 #define mem_sharing_enabled(d) ((d)->arch.hvm.mem_sharing.enabled)
30 
31 /* Auditing of memory sharing code? */
32 #ifndef NDEBUG
33 #define MEM_SHARING_AUDIT 1
34 #else
35 #define MEM_SHARING_AUDIT 0
36 #endif
37 
38 typedef uint64_t shr_handle_t;
39 
40 typedef struct rmap_hashtab {
41     struct list_head *bucket;
42     /*
43      * Overlaps with prev pointer of list_head in union below.
44      * Unlike the prev pointer, this can be NULL.
45      */
46     void *flag;
47 } rmap_hashtab_t;
48 
49 struct page_sharing_info
50 {
51     struct page_info *pg;   /* Back pointer to the page. */
52     shr_handle_t handle;    /* Globally unique version / handle. */
53 #if MEM_SHARING_AUDIT
54     struct list_head entry; /* List of all shared pages (entry). */
55     struct rcu_head rcu_head; /* List of all shared pages (entry). */
56 #endif
57     /* Reverse map of <domain,gfn> tuples for this shared frame. */
58     union {
59         struct list_head    gfns;
60         rmap_hashtab_t      hash_table;
61     };
62 };
63 
64 unsigned int mem_sharing_get_nr_saved_mfns(void);
65 unsigned int mem_sharing_get_nr_shared_mfns(void);
66 
67 /* Only fails with -ENOMEM. Enforce it with a BUG_ON wrapper. */
68 int __mem_sharing_unshare_page(struct domain *d,
69                                unsigned long gfn,
70                                bool destroy);
71 
mem_sharing_unshare_page(struct domain * d,unsigned long gfn)72 static inline int mem_sharing_unshare_page(struct domain *d,
73                                            unsigned long gfn)
74 {
75     int rc = __mem_sharing_unshare_page(d, gfn, false);
76     BUG_ON(rc && (rc != -ENOMEM));
77     return rc;
78 }
79 
mem_sharing_is_fork(const struct domain * d)80 static inline bool mem_sharing_is_fork(const struct domain *d)
81 {
82     return d->parent;
83 }
84 
85 int mem_sharing_fork_page(struct domain *d, gfn_t gfn,
86                           bool unsharing);
87 
88 /*
89  * If called by a foreign domain, possible errors are
90  *   -EBUSY -> ring full
91  *   -ENOSYS -> no ring to begin with
92  * and the foreign mapper is responsible for retrying.
93  *
94  * If called by the guest vcpu itself and allow_sleep is set, may
95  * sleep on a wait queue, so the caller is responsible for not
96  * holding locks on entry. It may only fail with ENOSYS
97  *
98  * If called by the guest vcpu itself and allow_sleep is not set,
99  * then it's the same as a foreign domain.
100  */
101 int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
102                               bool allow_sleep);
103 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg);
104 int mem_sharing_domctl(struct domain *d,
105                        struct xen_domctl_mem_sharing_op *mec);
106 
107 /*
108  * Scans the p2m and relinquishes any shared pages, destroying
109  * those for which this domain holds the final reference.
110  * Preemptible.
111  */
112 int relinquish_shared_pages(struct domain *d);
113 
114 #else
115 
116 #define mem_sharing_enabled(d) false
117 
mem_sharing_get_nr_saved_mfns(void)118 static inline unsigned int mem_sharing_get_nr_saved_mfns(void)
119 {
120     return 0;
121 }
122 
mem_sharing_get_nr_shared_mfns(void)123 static inline unsigned int mem_sharing_get_nr_shared_mfns(void)
124 {
125     return 0;
126 }
127 
mem_sharing_unshare_page(struct domain * d,unsigned long gfn)128 static inline int mem_sharing_unshare_page(struct domain *d, unsigned long gfn)
129 {
130     ASSERT_UNREACHABLE();
131     return -EOPNOTSUPP;
132 }
133 
mem_sharing_notify_enomem(struct domain * d,unsigned long gfn,bool allow_sleep)134 static inline int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
135                                             bool allow_sleep)
136 {
137     ASSERT_UNREACHABLE();
138     return -EOPNOTSUPP;
139 }
140 
mem_sharing_is_fork(const struct domain * d)141 static inline bool mem_sharing_is_fork(const struct domain *d)
142 {
143     return false;
144 }
145 
mem_sharing_fork_page(struct domain * d,gfn_t gfn,bool lock)146 static inline int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool lock)
147 {
148     return -EOPNOTSUPP;
149 }
150 
151 #endif
152 
153 #endif /* __MEM_SHARING_H__ */
154