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 /* Auditing of memory sharing code? */
28 #define MEM_SHARING_AUDIT 1
29 
30 typedef uint64_t shr_handle_t;
31 
32 typedef struct rmap_hashtab {
33     struct list_head *bucket;
34     /* Overlaps with prev pointer of list_head in union below.
35      * Unlike the prev pointer, this can be NULL. */
36     void *flag;
37 } rmap_hashtab_t;
38 
39 struct page_sharing_info
40 {
41     struct page_info *pg;   /* Back pointer to the page. */
42     shr_handle_t handle;    /* Globally unique version / handle. */
43 #if MEM_SHARING_AUDIT
44     struct list_head entry; /* List of all shared pages (entry). */
45     struct rcu_head rcu_head; /* List of all shared pages (entry). */
46 #endif
47     /* Reverse map of <domain,gfn> tuples for this shared frame. */
48     union {
49         struct list_head    gfns;
50         rmap_hashtab_t      hash_table;
51     };
52 };
53 
54 #define sharing_supported(_d) \
55     (is_hvm_domain(_d) && paging_mode_hap(_d))
56 
57 unsigned int mem_sharing_get_nr_saved_mfns(void);
58 unsigned int mem_sharing_get_nr_shared_mfns(void);
59 
60 #define MEM_SHARING_DESTROY_GFN       (1<<1)
61 /* Only fails with -ENOMEM. Enforce it with a BUG_ON wrapper. */
62 int __mem_sharing_unshare_page(struct domain *d,
63                              unsigned long gfn,
64                              uint16_t flags);
mem_sharing_unshare_page(struct domain * d,unsigned long gfn,uint16_t flags)65 static inline int mem_sharing_unshare_page(struct domain *d,
66                                            unsigned long gfn,
67                                            uint16_t flags)
68 {
69     int rc = __mem_sharing_unshare_page(d, gfn, flags);
70     BUG_ON( rc && (rc != -ENOMEM) );
71     return rc;
72 }
73 
74 /* If called by a foreign domain, possible errors are
75  *   -EBUSY -> ring full
76  *   -ENOSYS -> no ring to begin with
77  * and the foreign mapper is responsible for retrying.
78  *
79  * If called by the guest vcpu itself and allow_sleep is set, may
80  * sleep on a wait queue, so the caller is responsible for not
81  * holding locks on entry. It may only fail with ENOSYS
82  *
83  * If called by the guest vcpu itself and allow_sleep is not set,
84  * then it's the same as a foreign domain.
85  */
86 int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
87                                 bool_t allow_sleep);
88 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg);
89 int mem_sharing_domctl(struct domain *d,
90                        struct xen_domctl_mem_sharing_op *mec);
91 void mem_sharing_init(void);
92 
93 /* Scans the p2m and relinquishes any shared pages, destroying
94  * those for which this domain holds the final reference.
95  * Preemptible.
96  */
97 int relinquish_shared_pages(struct domain *d);
98 
99 #endif /* __MEM_SHARING_H__ */
100