1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; If not, see <http://www.gnu.org/licenses/>.
14 *
15 * Copyright (c) 2007 Isaku Yamahata <yamahata at valinux co jp>
16 * VA Linux Systems Japan K.K.
17 *
18 */
19
20 #include "xg_private.h"
21 #include "xc_core.h"
22 #include <xen/hvm/e820.h>
23
24 #define GET_FIELD(_p, _f) ((dinfo->guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f))
25
26 int
xc_core_arch_gpfn_may_present(struct xc_core_arch_context * arch_ctxt,unsigned long pfn)27 xc_core_arch_gpfn_may_present(struct xc_core_arch_context *arch_ctxt,
28 unsigned long pfn)
29 {
30 if ((pfn >= 0xa0 && pfn < 0xc0) /* VGA hole */
31 || (pfn >= (HVM_BELOW_4G_MMIO_START >> PAGE_SHIFT)
32 && pfn < (1ULL<<32) >> PAGE_SHIFT)) /* MMIO */
33 return 0;
34 return 1;
35 }
36
37 int
xc_core_arch_auto_translated_physmap(const xc_dominfo_t * info)38 xc_core_arch_auto_translated_physmap(const xc_dominfo_t *info)
39 {
40 return info->hvm;
41 }
42
43 int
xc_core_arch_memory_map_get(xc_interface * xch,struct xc_core_arch_context * unused,xc_dominfo_t * info,shared_info_any_t * live_shinfo,xc_core_memory_map_t ** mapp,unsigned int * nr_entries)44 xc_core_arch_memory_map_get(xc_interface *xch, struct xc_core_arch_context *unused,
45 xc_dominfo_t *info, shared_info_any_t *live_shinfo,
46 xc_core_memory_map_t **mapp,
47 unsigned int *nr_entries)
48 {
49 xen_pfn_t p2m_size = 0;
50 xc_core_memory_map_t *map;
51
52 if ( xc_domain_nr_gpfns(xch, info->domid, &p2m_size) < 0 )
53 return -1;
54
55 map = malloc(sizeof(*map));
56 if ( map == NULL )
57 {
58 PERROR("Could not allocate memory");
59 return -1;
60 }
61
62 map->addr = 0;
63 map->size = ((uint64_t)p2m_size) << PAGE_SHIFT;
64
65 *mapp = map;
66 *nr_entries = 1;
67 return 0;
68 }
69
70 static int
xc_core_arch_map_p2m_rw(xc_interface * xch,struct domain_info_context * dinfo,xc_dominfo_t * info,shared_info_any_t * live_shinfo,xen_pfn_t ** live_p2m,unsigned long * pfnp,int rw)71 xc_core_arch_map_p2m_rw(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info,
72 shared_info_any_t *live_shinfo, xen_pfn_t **live_p2m,
73 unsigned long *pfnp, int rw)
74 {
75 /* Double and single indirect references to the live P2M table */
76 xen_pfn_t *live_p2m_frame_list_list = NULL;
77 xen_pfn_t *live_p2m_frame_list = NULL;
78 /* Copies of the above. */
79 xen_pfn_t *p2m_frame_list_list = NULL;
80 xen_pfn_t *p2m_frame_list = NULL;
81
82 uint32_t dom = info->domid;
83 int ret = -1;
84 int err;
85 int i;
86
87 if ( xc_domain_nr_gpfns(xch, info->domid, &dinfo->p2m_size) < 0 )
88 {
89 ERROR("Could not get maximum GPFN!");
90 goto out;
91 }
92
93 if ( dinfo->p2m_size < info->nr_pages )
94 {
95 ERROR("p2m_size < nr_pages -1 (%lx < %lx", dinfo->p2m_size, info->nr_pages - 1);
96 goto out;
97 }
98
99 live_p2m_frame_list_list =
100 xc_map_foreign_range(xch, dom, PAGE_SIZE, PROT_READ,
101 GET_FIELD(live_shinfo, arch.pfn_to_mfn_frame_list_list));
102
103 if ( !live_p2m_frame_list_list )
104 {
105 PERROR("Couldn't map p2m_frame_list_list (errno %d)", errno);
106 goto out;
107 }
108
109 /* Get a local copy of the live_P2M_frame_list_list */
110 if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) )
111 {
112 ERROR("Couldn't allocate p2m_frame_list_list array");
113 goto out;
114 }
115 memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE);
116
117 /* Canonicalize guest's unsigned long vs ours */
118 if ( dinfo->guest_width > sizeof(unsigned long) )
119 for ( i = 0; i < PAGE_SIZE/sizeof(unsigned long); i++ )
120 if ( i < PAGE_SIZE/dinfo->guest_width )
121 p2m_frame_list_list[i] = ((uint64_t *)p2m_frame_list_list)[i];
122 else
123 p2m_frame_list_list[i] = 0;
124 else if ( dinfo->guest_width < sizeof(unsigned long) )
125 for ( i = PAGE_SIZE/sizeof(unsigned long) - 1; i >= 0; i-- )
126 p2m_frame_list_list[i] = ((uint32_t *)p2m_frame_list_list)[i];
127
128 live_p2m_frame_list =
129 xc_map_foreign_pages(xch, dom, PROT_READ,
130 p2m_frame_list_list,
131 P2M_FLL_ENTRIES);
132
133 if ( !live_p2m_frame_list )
134 {
135 PERROR("Couldn't map p2m_frame_list");
136 goto out;
137 }
138
139 /* Get a local copy of the live_P2M_frame_list */
140 if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) )
141 {
142 ERROR("Couldn't allocate p2m_frame_list array");
143 goto out;
144 }
145 memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE);
146 memcpy(p2m_frame_list, live_p2m_frame_list, P2M_GUEST_FL_SIZE);
147
148 /* Canonicalize guest's unsigned long vs ours */
149 if ( dinfo->guest_width > sizeof(unsigned long) )
150 for ( i = 0; i < P2M_FL_ENTRIES; i++ )
151 p2m_frame_list[i] = ((uint64_t *)p2m_frame_list)[i];
152 else if ( dinfo->guest_width < sizeof(unsigned long) )
153 for ( i = P2M_FL_ENTRIES - 1; i >= 0; i-- )
154 p2m_frame_list[i] = ((uint32_t *)p2m_frame_list)[i];
155
156 *live_p2m = xc_map_foreign_pages(xch, dom,
157 rw ? (PROT_READ | PROT_WRITE) : PROT_READ,
158 p2m_frame_list,
159 P2M_FL_ENTRIES);
160
161 if ( !*live_p2m )
162 {
163 PERROR("Couldn't map p2m table");
164 goto out;
165 }
166
167 *pfnp = dinfo->p2m_size;
168
169 ret = 0;
170
171 out:
172 err = errno;
173
174 if ( live_p2m_frame_list_list )
175 munmap(live_p2m_frame_list_list, PAGE_SIZE);
176
177 if ( live_p2m_frame_list )
178 munmap(live_p2m_frame_list, P2M_FLL_ENTRIES * PAGE_SIZE);
179
180 free(p2m_frame_list_list);
181
182 free(p2m_frame_list);
183
184 errno = err;
185 return ret;
186 }
187
188 int
xc_core_arch_map_p2m(xc_interface * xch,unsigned int guest_width,xc_dominfo_t * info,shared_info_any_t * live_shinfo,xen_pfn_t ** live_p2m,unsigned long * pfnp)189 xc_core_arch_map_p2m(xc_interface *xch, unsigned int guest_width, xc_dominfo_t *info,
190 shared_info_any_t *live_shinfo, xen_pfn_t **live_p2m,
191 unsigned long *pfnp)
192 {
193 struct domain_info_context _dinfo = { .guest_width = guest_width };
194 struct domain_info_context *dinfo = &_dinfo;
195 return xc_core_arch_map_p2m_rw(xch, dinfo, info,
196 live_shinfo, live_p2m, pfnp, 0);
197 }
198
199 int
xc_core_arch_map_p2m_writable(xc_interface * xch,unsigned int guest_width,xc_dominfo_t * info,shared_info_any_t * live_shinfo,xen_pfn_t ** live_p2m,unsigned long * pfnp)200 xc_core_arch_map_p2m_writable(xc_interface *xch, unsigned int guest_width, xc_dominfo_t *info,
201 shared_info_any_t *live_shinfo, xen_pfn_t **live_p2m,
202 unsigned long *pfnp)
203 {
204 struct domain_info_context _dinfo = { .guest_width = guest_width };
205 struct domain_info_context *dinfo = &_dinfo;
206 return xc_core_arch_map_p2m_rw(xch, dinfo, info,
207 live_shinfo, live_p2m, pfnp, 1);
208 }
209
210 int
xc_core_arch_get_scratch_gpfn(xc_interface * xch,uint32_t domid,xen_pfn_t * gpfn)211 xc_core_arch_get_scratch_gpfn(xc_interface *xch, uint32_t domid,
212 xen_pfn_t *gpfn)
213 {
214 return xc_domain_nr_gpfns(xch, domid, gpfn);
215 }
216
217 /*
218 * Local variables:
219 * mode: C
220 * c-file-style: "BSD"
221 * c-basic-offset: 4
222 * tab-width: 4
223 * indent-tabs-mode: nil
224 * End:
225 */
226