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;
5  * version 2.1 of the License.
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 
16 #ifndef _XC_DOM_H
17 #define _XC_DOM_H
18 
19 #include <xen/libelf/libelf.h>
20 #include <xenguest.h>
21 
22 #define INVALID_PFN ((xen_pfn_t)-1)
23 #define X86_HVM_NR_SPECIAL_PAGES    8
24 #define X86_HVM_END_SPECIAL_REGION  0xff000u
25 #define XG_MAX_MODULES 2
26 
27 /* --- typedefs and structs ---------------------------------------- */
28 
29 typedef uint64_t xen_vaddr_t;
30 typedef uint64_t xen_paddr_t;
31 
32 #define PRIpfn PRI_xen_pfn
33 
34 struct xc_dom_seg {
35     xen_vaddr_t vstart;
36     xen_vaddr_t vend;
37     xen_pfn_t pfn;
38     xen_pfn_t pages;
39 };
40 
41 struct xc_dom_mem {
42     struct xc_dom_mem *next;
43     void *ptr;
44     enum {
45         XC_DOM_MEM_TYPE_MALLOC_INTERNAL,
46         XC_DOM_MEM_TYPE_MALLOC_EXTERNAL,
47         XC_DOM_MEM_TYPE_MMAP,
48     } type;
49     size_t len;
50     unsigned char memory[0];
51 };
52 
53 struct xc_dom_phys {
54     struct xc_dom_phys *next;
55     void *ptr;
56     xen_pfn_t first;
57     xen_pfn_t count;
58 };
59 
60 struct xc_dom_module {
61     void *blob;
62     size_t size;
63     void *cmdline;
64     /* If seg.vstart is non zero then the module will be loaded at that
65      * address, otherwise it will automatically placed.
66      *
67      * If automatic placement is used and the module is gzip
68      * compressed then it will be decompressed as it is loaded. If the
69      * module has been explicitly placed then it is loaded as is
70      * otherwise decompressing risks undoing the manual placement.
71      */
72     struct xc_dom_seg seg;
73 };
74 
75 struct xc_dom_image {
76     /* files */
77     void *kernel_blob;
78     size_t kernel_size;
79     unsigned int num_modules;
80     struct xc_dom_module modules[XG_MAX_MODULES];
81     void *devicetree_blob;
82     size_t devicetree_size;
83 
84     size_t max_kernel_size;
85     size_t max_module_size;
86     size_t max_devicetree_size;
87 
88     /* arguments and parameters */
89     char *cmdline;
90     size_t cmdline_size;
91     uint32_t f_requested[XENFEAT_NR_SUBMAPS];
92 
93     /* info from (elf) kernel image */
94     struct elf_dom_parms parms;
95     char *guest_type;
96 
97     /* memory layout */
98     struct xc_dom_seg kernel_seg;
99     struct xc_dom_seg p2m_seg;
100     struct xc_dom_seg pgtables_seg;
101     struct xc_dom_seg devicetree_seg;
102     struct xc_dom_seg start_info_seg; /* HVMlite only */
103     xen_pfn_t start_info_pfn;
104     xen_pfn_t console_pfn;
105     xen_pfn_t xenstore_pfn;
106     xen_pfn_t shared_info_pfn;
107     xen_pfn_t bootstack_pfn;
108     xen_pfn_t pfn_alloc_end;
109     xen_vaddr_t virt_alloc_end;
110     xen_vaddr_t bsd_symtab_start;
111 
112     /*
113      * initrd parameters as specified in start_info page
114      * Depending on capabilities of the booted kernel this may be a virtual
115      * address or a pfn. Type is neutral and large enough to hold a virtual
116      * address of a 64 bit kernel even with 32 bit toolstack.
117      */
118     uint64_t initrd_start;
119     uint64_t initrd_len;
120 
121     unsigned int alloc_bootstack;
122     xen_vaddr_t virt_pgtab_end;
123 
124     /* other state info */
125     uint32_t f_active[XENFEAT_NR_SUBMAPS];
126     /*
127      * p2m_host maps guest physical addresses an offset from
128      * rambase_pfn (see below) into gfns.
129      *
130      * For a pure PV guest this means that it maps GPFNs into MFNs for
131      * a hybrid guest this means that it maps GPFNs to GPFNS.
132      *
133      * Note that the input is offset by rambase.
134      */
135     xen_pfn_t *p2m_host;
136     void *p2m_guest;
137 
138     /* physical memory
139      *
140      * An x86 PV guest has one or more blocks of physical RAM,
141      * consisting of total_pages starting at rambase_pfn. The start
142      * address and size of each block is controlled by vNUMA
143      * structures.
144      *
145      * An ARM guest has GUEST_RAM_BANKS regions of RAM, with
146      * rambank_size[i] pages in each. The lowest RAM address
147      * (corresponding to the base of the p2m arrays above) is stored
148      * in rambase_pfn.
149      */
150     xen_pfn_t rambase_pfn;
151     xen_pfn_t total_pages;
152     xen_pfn_t p2m_size;         /* number of pfns covered by p2m */
153     struct xc_dom_phys *phys_pages;
154 #if defined (__arm__) || defined(__aarch64__)
155     xen_pfn_t rambank_size[GUEST_RAM_BANKS];
156 #endif
157 
158     /* malloc memory pool */
159     struct xc_dom_mem *memblocks;
160 
161     /* memory footprint stats */
162     size_t alloc_malloc;
163     size_t alloc_mem_map;
164     size_t alloc_file_map;
165     size_t alloc_domU_map;
166 
167     /* misc xen domain config stuff */
168     unsigned long flags;
169     unsigned int console_evtchn;
170     unsigned int xenstore_evtchn;
171     uint32_t console_domid;
172     uint32_t xenstore_domid;
173     xen_pfn_t shared_info_mfn;
174 
175     xc_interface *xch;
176     uint32_t guest_domid;
177     int claim_enabled; /* 0 by default, 1 enables it */
178 
179     int xen_version;
180     xen_capabilities_info_t xen_caps;
181 
182     /* kernel loader, arch hooks */
183     struct xc_dom_loader *kernel_loader;
184     void *private_loader;
185 
186     /* vNUMA information */
187     xen_vmemrange_t *vmemranges;
188     unsigned int nr_vmemranges;
189     unsigned int *vnode_to_pnode;
190     unsigned int nr_vnodes;
191 
192     /* domain type/architecture specific data */
193     void *arch_private;
194 
195     /* kernel loader */
196     struct xc_dom_arch *arch_hooks;
197     /* allocate up to pfn_alloc_end */
198     int (*allocate) (struct xc_dom_image * dom);
199 
200     /* Container type (HVM or PV). */
201     enum {
202         XC_DOM_PV_CONTAINER,
203         XC_DOM_HVM_CONTAINER,
204     } container_type;
205 
206     /* HVM specific fields. */
207     xen_pfn_t target_pages;
208     xen_paddr_t mmio_start;
209     xen_paddr_t mmio_size;
210     xen_paddr_t lowmem_end;
211     xen_paddr_t highmem_end;
212     xen_pfn_t vga_hole_size;
213 
214     /* If unset disables the setup of the IOREQ pages. */
215     bool device_model;
216 
217     /* BIOS/Firmware passed to HVMLOADER */
218     struct xc_hvm_firmware_module system_firmware_module;
219 
220     /* Extra ACPI tables */
221 #define MAX_ACPI_MODULES        4
222     struct xc_hvm_firmware_module acpi_modules[MAX_ACPI_MODULES];
223 
224     /* Extra SMBIOS structures passed to HVMLOADER */
225     struct xc_hvm_firmware_module smbios_module;
226 
227     xen_pfn_t vuart_gfn;
228 };
229 
230 /* --- pluggable kernel loader ------------------------------------- */
231 
232 struct xc_dom_loader {
233     char *name;
234     /* Sadly the error returns from these functions are not consistent: */
235     elf_negerrnoval (*probe) (struct xc_dom_image * dom);
236     elf_negerrnoval (*parser) (struct xc_dom_image * dom);
237     elf_errorstatus (*loader) (struct xc_dom_image * dom);
238 
239     struct xc_dom_loader *next;
240 };
241 
242 #define __init __attribute__ ((constructor))
243 void xc_dom_register_loader(struct xc_dom_loader *loader);
244 
245 /* --- arch specific hooks ----------------------------------------- */
246 
247 struct xc_dom_arch {
248     /* pagetable setup */
249     int (*alloc_magic_pages) (struct xc_dom_image * dom);
250     int (*alloc_pgtables) (struct xc_dom_image * dom);
251     int (*alloc_p2m_list) (struct xc_dom_image * dom);
252     int (*setup_pgtables) (struct xc_dom_image * dom);
253 
254     /* arch-specific data structs setup */
255     int (*start_info) (struct xc_dom_image * dom);
256     int (*shared_info) (struct xc_dom_image * dom, void *shared_info);
257     int (*vcpu) (struct xc_dom_image * dom);
258     int (*bootearly) (struct xc_dom_image * dom);
259     int (*bootlate) (struct xc_dom_image * dom);
260 
261     /* arch-specific memory initialization. */
262     int (*meminit) (struct xc_dom_image * dom);
263 
264     char *guest_type;
265     char *native_protocol;
266     int page_shift;
267     int sizeof_pfn;
268     int p2m_base_supported;
269     int arch_private_size;
270 
271     struct xc_dom_arch *next;
272 };
273 void xc_dom_register_arch_hooks(struct xc_dom_arch *hooks);
274 
275 #define XC_DOM_PAGE_SHIFT(dom)  ((dom)->arch_hooks->page_shift)
276 #define XC_DOM_PAGE_SIZE(dom)   (1LL << (dom)->arch_hooks->page_shift)
277 
278 /* --- main functions ---------------------------------------------- */
279 
280 struct xc_dom_image *xc_dom_allocate(xc_interface *xch,
281                                      const char *cmdline, const char *features);
282 void xc_dom_release_phys(struct xc_dom_image *dom);
283 void xc_dom_release(struct xc_dom_image *dom);
284 int xc_dom_rambase_init(struct xc_dom_image *dom, uint64_t rambase);
285 int xc_dom_mem_init(struct xc_dom_image *dom, unsigned int mem_mb);
286 
287 /* Set this larger if you have enormous modules/kernels. Note that
288  * you should trust all kernels not to be maliciously large (e.g. to
289  * exhaust all dom0 memory) if you do this (see CVE-2012-4544 /
290  * XSA-25). You can also set the default independently for
291  * modules/kernels in xc_dom_allocate() or call
292  * xc_dom_{kernel,module}_max_size.
293  */
294 #ifndef XC_DOM_DECOMPRESS_MAX
295 #define XC_DOM_DECOMPRESS_MAX (1024*1024*1024) /* 1GB */
296 #endif
297 
298 int xc_dom_kernel_check_size(struct xc_dom_image *dom, size_t sz);
299 int xc_dom_kernel_max_size(struct xc_dom_image *dom, size_t sz);
300 
301 int xc_dom_module_check_size(struct xc_dom_image *dom, size_t sz);
302 int xc_dom_module_max_size(struct xc_dom_image *dom, size_t sz);
303 
304 int xc_dom_devicetree_max_size(struct xc_dom_image *dom, size_t sz);
305 
306 size_t xc_dom_check_gzip(xc_interface *xch,
307                      void *blob, size_t ziplen);
308 int xc_dom_do_gunzip(xc_interface *xch,
309                      void *src, size_t srclen, void *dst, size_t dstlen);
310 int xc_dom_try_gunzip(struct xc_dom_image *dom, void **blob, size_t * size);
311 
312 int xc_dom_kernel_file(struct xc_dom_image *dom, const char *filename);
313 int xc_dom_module_file(struct xc_dom_image *dom, const char *filename,
314                        const char *cmdline);
315 int xc_dom_kernel_mem(struct xc_dom_image *dom, const void *mem,
316                       size_t memsize);
317 int xc_dom_module_mem(struct xc_dom_image *dom, const void *mem,
318                        size_t memsize, const char *cmdline);
319 int xc_dom_devicetree_file(struct xc_dom_image *dom, const char *filename);
320 int xc_dom_devicetree_mem(struct xc_dom_image *dom, const void *mem,
321                           size_t memsize);
322 
323 int xc_dom_parse_image(struct xc_dom_image *dom);
324 int xc_dom_set_arch_hooks(struct xc_dom_image *dom);
325 int xc_dom_build_image(struct xc_dom_image *dom);
326 int xc_dom_update_guest_p2m(struct xc_dom_image *dom);
327 
328 int xc_dom_boot_xen_init(struct xc_dom_image *dom, xc_interface *xch,
329                          uint32_t domid);
330 int xc_dom_boot_mem_init(struct xc_dom_image *dom);
331 void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn,
332                            xen_pfn_t count);
333 int xc_dom_boot_image(struct xc_dom_image *dom);
334 int xc_dom_compat_check(struct xc_dom_image *dom);
335 int xc_dom_gnttab_init(struct xc_dom_image *dom);
336 int xc_dom_gnttab_hvm_seed(xc_interface *xch, uint32_t domid,
337                            xen_pfn_t console_gmfn,
338                            xen_pfn_t xenstore_gmfn,
339                            uint32_t console_domid,
340                            uint32_t xenstore_domid);
341 int xc_dom_gnttab_seed(xc_interface *xch, uint32_t domid,
342                        xen_pfn_t console_gmfn,
343                        xen_pfn_t xenstore_gmfn,
344                        uint32_t console_domid,
345                        uint32_t xenstore_domid);
346 bool xc_dom_translated(const struct xc_dom_image *dom);
347 
348 /* --- debugging bits ---------------------------------------------- */
349 
350 int xc_dom_loginit(xc_interface *xch);
351 
352 void xc_dom_printf(xc_interface *xch, const char *fmt, ...)
353      __attribute__ ((format(printf, 2, 3)));
354 void xc_dom_panic_func(xc_interface *xch,
355                       const char *file, int line, xc_error_code err,
356                       const char *fmt, ...)
357     __attribute__ ((format(printf, 5, 6)));
358 
359 #define xc_dom_panic(xch, err, fmt, args...) \
360     xc_dom_panic_func(xch, __FILE__, __LINE__, err, fmt, ## args)
361 #define xc_dom_trace(mark) \
362     xc_dom_printf("%s:%d: trace %s\n", __FILE__, __LINE__, mark)
363 
364 void xc_dom_log_memory_footprint(struct xc_dom_image *dom);
365 
366 /* --- simple memory pool ------------------------------------------ */
367 
368 void *xc_dom_malloc(struct xc_dom_image *dom, size_t size);
369 int xc_dom_register_external(struct xc_dom_image *dom, void *ptr, size_t size);
370 void *xc_dom_malloc_page_aligned(struct xc_dom_image *dom, size_t size);
371 void *xc_dom_malloc_filemap(struct xc_dom_image *dom,
372                             const char *filename, size_t * size,
373                             const size_t max_size);
374 char *xc_dom_strdup(struct xc_dom_image *dom, const char *str);
375 
376 /* --- alloc memory pool ------------------------------------------- */
377 
378 xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
379 int xc_dom_alloc_segment(struct xc_dom_image *dom,
380                          struct xc_dom_seg *seg, char *name,
381                          xen_vaddr_t start, xen_vaddr_t size);
382 
383 /* --- misc bits --------------------------------------------------- */
384 
385 void *xc_dom_pfn_to_ptr(struct xc_dom_image *dom, xen_pfn_t first,
386                         xen_pfn_t count);
387 void *xc_dom_pfn_to_ptr_retcount(struct xc_dom_image *dom, xen_pfn_t first,
388                                  xen_pfn_t count, xen_pfn_t *count_out);
389 void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn);
390 void xc_dom_unmap_all(struct xc_dom_image *dom);
391 
xc_dom_seg_to_ptr_pages(struct xc_dom_image * dom,struct xc_dom_seg * seg,xen_pfn_t * pages_out)392 static inline void *xc_dom_seg_to_ptr_pages(struct xc_dom_image *dom,
393                                       struct xc_dom_seg *seg,
394                                       xen_pfn_t *pages_out)
395 {
396     void *retval;
397 
398     retval = xc_dom_pfn_to_ptr(dom, seg->pfn, seg->pages);
399 
400     *pages_out = retval ? seg->pages : 0;
401     return retval;
402 }
403 
xc_dom_seg_to_ptr(struct xc_dom_image * dom,struct xc_dom_seg * seg)404 static inline void *xc_dom_seg_to_ptr(struct xc_dom_image *dom,
405                                       struct xc_dom_seg *seg)
406 {
407     xen_pfn_t dummy;
408 
409     return xc_dom_seg_to_ptr_pages(dom, seg, &dummy);
410 }
411 
xc_dom_vaddr_to_ptr(struct xc_dom_image * dom,xen_vaddr_t vaddr,size_t * safe_region_out)412 static inline void *xc_dom_vaddr_to_ptr(struct xc_dom_image *dom,
413                                         xen_vaddr_t vaddr,
414                                         size_t *safe_region_out)
415 {
416     unsigned int page_size = XC_DOM_PAGE_SIZE(dom);
417     xen_pfn_t page = (vaddr - dom->parms.virt_base) / page_size;
418     unsigned int offset = (vaddr - dom->parms.virt_base) % page_size;
419     xen_pfn_t safe_region_count;
420     void *ptr;
421 
422     *safe_region_out = 0;
423     ptr = xc_dom_pfn_to_ptr_retcount(dom, page, 0, &safe_region_count);
424     if ( ptr == NULL )
425         return ptr;
426     *safe_region_out = (safe_region_count << XC_DOM_PAGE_SHIFT(dom)) - offset;
427     return ptr + offset;
428 }
429 
xc_dom_p2m(struct xc_dom_image * dom,xen_pfn_t pfn)430 static inline xen_pfn_t xc_dom_p2m(struct xc_dom_image *dom, xen_pfn_t pfn)
431 {
432     if ( xc_dom_translated(dom) )
433         return pfn;
434     if (pfn < dom->rambase_pfn || pfn >= dom->rambase_pfn + dom->total_pages)
435         return INVALID_MFN;
436     return dom->p2m_host[pfn - dom->rambase_pfn];
437 }
438 
439 #endif /* _XC_DOM_H */
440 
441 /*
442  * Local variables:
443  * mode: C
444  * c-file-style: "BSD"
445  * c-basic-offset: 4
446  * tab-width: 4
447  * indent-tabs-mode: nil
448  * End:
449  */
450