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