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;
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     /*
128      * pv_p2m is specific to x86 PV guests, and maps GFNs to MFNs.  It is
129      * eventually copied into guest context.
130      */
131     xen_pfn_t *pv_p2m;
132 
133     /* physical memory
134      *
135      * An x86 PV guest has one or more blocks of physical RAM,
136      * consisting of total_pages starting at 0. The start address and
137      * size of each block is controlled by vNUMA structures.
138      *
139      * An ARM guest has GUEST_RAM_BANKS regions of RAM, with
140      * rambank_size[i] pages in each. The lowest RAM address
141      * (corresponding to the base of the p2m arrays above) is stored
142      * in rambase_pfn.
143      */
144     xen_pfn_t rambase_pfn;
145     xen_pfn_t total_pages;
146     xen_pfn_t p2m_size;         /* number of pfns covered by p2m */
147     struct xc_dom_phys *phys_pages;
148 #if defined (__arm__) || defined(__aarch64__)
149     xen_pfn_t rambank_size[GUEST_RAM_BANKS];
150 #endif
151 
152     /* malloc memory pool */
153     struct xc_dom_mem *memblocks;
154 
155     /* memory footprint stats */
156     size_t alloc_malloc;
157     size_t alloc_mem_map;
158     size_t alloc_file_map;
159     size_t alloc_domU_map;
160 
161     /* misc xen domain config stuff */
162     unsigned long flags;
163     unsigned int console_evtchn;
164     unsigned int xenstore_evtchn;
165     uint32_t console_domid;
166     uint32_t xenstore_domid;
167     xen_pfn_t shared_info_mfn;
168 
169     xc_interface *xch;
170     uint32_t guest_domid;
171     int claim_enabled; /* 0 by default, 1 enables it */
172 
173     int xen_version;
174     xen_capabilities_info_t xen_caps;
175 
176     /* kernel loader, arch hooks */
177     struct xc_dom_loader *kernel_loader;
178     void *private_loader;
179 
180     /* vNUMA information */
181     xen_vmemrange_t *vmemranges;
182     unsigned int nr_vmemranges;
183     unsigned int *vnode_to_pnode;
184     unsigned int nr_vnodes;
185 
186     /* domain type/architecture specific data */
187     void *arch_private;
188 
189     /* kernel loader */
190     struct xc_dom_arch *arch_hooks;
191     /* allocate up to pfn_alloc_end */
192     int (*allocate) (struct xc_dom_image * dom);
193 
194     /* Container type (HVM or PV). */
195     enum {
196         XC_DOM_PV_CONTAINER,
197         XC_DOM_HVM_CONTAINER,
198     } container_type;
199 
200     /* HVM specific fields. */
201     xen_pfn_t target_pages;
202     xen_paddr_t mmio_start;
203     xen_paddr_t mmio_size;
204     xen_paddr_t lowmem_end;
205     xen_paddr_t highmem_end;
206     xen_pfn_t vga_hole_size;
207 
208     /* If unset disables the setup of the IOREQ pages. */
209     bool device_model;
210 
211     /* BIOS/Firmware passed to HVMLOADER */
212     struct xc_hvm_firmware_module system_firmware_module;
213 
214     /* Extra ACPI tables */
215 #define MAX_ACPI_MODULES        4
216     struct xc_hvm_firmware_module acpi_modules[MAX_ACPI_MODULES];
217 
218     /* Extra SMBIOS structures passed to HVMLOADER */
219     struct xc_hvm_firmware_module smbios_module;
220 
221 #if defined(__i386__) || defined(__x86_64__)
222     struct e820entry *e820;
223     unsigned int e820_entries;
224 #endif
225 
226     xen_pfn_t vuart_gfn;
227 
228     /* Number of vCPUs */
229     unsigned int max_vcpus;
230 };
231 
232 /* --- pluggable kernel loader ------------------------------------- */
233 
234 struct xc_dom_loader {
235     char *name;
236     /* Sadly the error returns from these functions are not consistent: */
237     elf_negerrnoval (*probe) (struct xc_dom_image * dom);
238     elf_negerrnoval (*parser) (struct xc_dom_image * dom);
239     elf_errorstatus (*loader) (struct xc_dom_image * dom);
240 
241     struct xc_dom_loader *next;
242 };
243 
244 #define __init __attribute__ ((constructor))
245 void xc_dom_register_loader(struct xc_dom_loader *loader);
246 
247 /* --- arch specific hooks ----------------------------------------- */
248 
249 struct xc_dom_arch {
250     int (*alloc_magic_pages) (struct xc_dom_image * dom);
251 
252     /* pagetable setup - x86 PV only */
253     int (*alloc_pgtables) (struct xc_dom_image * dom);
254     int (*alloc_p2m_list) (struct xc_dom_image * dom);
255     int (*setup_pgtables) (struct xc_dom_image * dom);
256 
257     /* arch-specific data structs setup */
258     int (*start_info) (struct xc_dom_image * dom);
259     int (*shared_info) (struct xc_dom_image * dom, void *shared_info);
260     int (*vcpu) (struct xc_dom_image * dom);
261     int (*bootearly) (struct xc_dom_image * dom);
262     int (*bootlate) (struct xc_dom_image * dom);
263 
264     /* arch-specific memory initialization. */
265     int (*meminit) (struct xc_dom_image * dom);
266 
267     char *guest_type;
268     char *native_protocol;
269     int page_shift;
270     int sizeof_pfn;
271     int p2m_base_supported;
272     int arch_private_size;
273 
274     struct xc_dom_arch *next;
275 };
276 void xc_dom_register_arch_hooks(struct xc_dom_arch *hooks);
277 
278 #define XC_DOM_PAGE_SHIFT(dom)  ((dom)->arch_hooks->page_shift)
279 #define XC_DOM_PAGE_SIZE(dom)   (1LL << (dom)->arch_hooks->page_shift)
280 
281 /* --- main functions ---------------------------------------------- */
282 
283 struct xc_dom_image *xc_dom_allocate(xc_interface *xch,
284                                      const char *cmdline, const char *features);
285 void xc_dom_release_phys(struct xc_dom_image *dom);
286 void xc_dom_release(struct xc_dom_image *dom);
287 int xc_dom_rambase_init(struct xc_dom_image *dom, uint64_t rambase);
288 int xc_dom_mem_init(struct xc_dom_image *dom, unsigned int mem_mb);
289 
290 /* Set this larger if you have enormous modules/kernels. Note that
291  * you should trust all kernels not to be maliciously large (e.g. to
292  * exhaust all dom0 memory) if you do this (see CVE-2012-4544 /
293  * XSA-25). You can also set the default independently for
294  * modules/kernels in xc_dom_allocate() or call
295  * xc_dom_{kernel,module}_max_size.
296  */
297 #ifndef XC_DOM_DECOMPRESS_MAX
298 #define XC_DOM_DECOMPRESS_MAX (1024*1024*1024) /* 1GB */
299 #endif
300 
301 int xc_dom_kernel_check_size(struct xc_dom_image *dom, size_t sz);
302 int xc_dom_kernel_max_size(struct xc_dom_image *dom, size_t sz);
303 
304 int xc_dom_module_max_size(struct xc_dom_image *dom, size_t sz);
305 
306 int xc_dom_devicetree_max_size(struct xc_dom_image *dom, size_t sz);
307 
308 size_t xc_dom_check_gzip(xc_interface *xch,
309                      void *blob, size_t ziplen);
310 int xc_dom_do_gunzip(xc_interface *xch,
311                      void *src, size_t srclen, void *dst, size_t dstlen);
312 int xc_dom_try_gunzip(struct xc_dom_image *dom, void **blob, size_t * size);
313 
314 int xc_dom_kernel_file(struct xc_dom_image *dom, const char *filename);
315 int xc_dom_module_file(struct xc_dom_image *dom, const char *filename,
316                        const char *cmdline);
317 int xc_dom_kernel_mem(struct xc_dom_image *dom, const void *mem,
318                       size_t memsize);
319 int xc_dom_module_mem(struct xc_dom_image *dom, const void *mem,
320                        size_t memsize, const char *cmdline);
321 int xc_dom_devicetree_file(struct xc_dom_image *dom, const char *filename);
322 int xc_dom_devicetree_mem(struct xc_dom_image *dom, const void *mem,
323                           size_t memsize);
324 
325 int xc_dom_parse_image(struct xc_dom_image *dom);
326 int xc_dom_set_arch_hooks(struct xc_dom_image *dom);
327 int xc_dom_build_image(struct xc_dom_image *dom);
328 
329 int xc_dom_boot_xen_init(struct xc_dom_image *dom, xc_interface *xch,
330                          uint32_t domid);
331 int xc_dom_boot_mem_init(struct xc_dom_image *dom);
332 void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn,
333                            xen_pfn_t count);
334 int xc_dom_boot_image(struct xc_dom_image *dom);
335 int xc_dom_compat_check(struct xc_dom_image *dom);
336 int xc_dom_gnttab_init(struct xc_dom_image *dom);
337 int xc_dom_gnttab_seed(xc_interface *xch, uint32_t guest_domid,
338                        bool is_hvm,
339                        xen_pfn_t console_gfn,
340                        xen_pfn_t xenstore_gfn,
341                        uint32_t console_domid,
342                        uint32_t xenstore_domid);
343 bool xc_dom_translated(const struct xc_dom_image *dom);
344 
345 /* --- debugging bits ---------------------------------------------- */
346 
347 int xc_dom_loginit(xc_interface *xch);
348 
349 void xc_dom_printf(xc_interface *xch, const char *fmt, ...)
350      __attribute__ ((format(printf, 2, 3)));
351 void xc_dom_panic_func(xc_interface *xch,
352                       const char *file, int line, xc_error_code err,
353                       const char *fmt, ...)
354     __attribute__ ((format(printf, 5, 6)));
355 
356 #define xc_dom_panic(xch, err, fmt, args...) \
357     xc_dom_panic_func(xch, __FILE__, __LINE__, err, fmt, ## args)
358 #define xc_dom_trace(mark) \
359     xc_dom_printf("%s:%d: trace %s\n", __FILE__, __LINE__, mark)
360 
361 void xc_dom_log_memory_footprint(struct xc_dom_image *dom);
362 
363 /* --- simple memory pool ------------------------------------------ */
364 
365 void *xc_dom_malloc(struct xc_dom_image *dom, size_t size);
366 int xc_dom_register_external(struct xc_dom_image *dom, void *ptr, size_t size);
367 void *xc_dom_malloc_page_aligned(struct xc_dom_image *dom, size_t size);
368 void *xc_dom_malloc_filemap(struct xc_dom_image *dom,
369                             const char *filename, size_t * size,
370                             const size_t max_size);
371 char *xc_dom_strdup(struct xc_dom_image *dom, const char *str);
372 
373 /* --- alloc memory pool ------------------------------------------- */
374 
375 xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
376 int xc_dom_alloc_segment(struct xc_dom_image *dom,
377                          struct xc_dom_seg *seg, char *name,
378                          xen_vaddr_t start, xen_vaddr_t size);
379 
380 /* --- misc bits --------------------------------------------------- */
381 
382 void *xc_dom_pfn_to_ptr(struct xc_dom_image *dom, xen_pfn_t first,
383                         xen_pfn_t count);
384 void *xc_dom_pfn_to_ptr_retcount(struct xc_dom_image *dom, xen_pfn_t first,
385                                  xen_pfn_t count, xen_pfn_t *count_out);
386 void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn);
387 void xc_dom_unmap_all(struct xc_dom_image *dom);
388 
xc_dom_seg_to_ptr_pages(struct xc_dom_image * dom,struct xc_dom_seg * seg,xen_pfn_t * pages_out)389 static inline void *xc_dom_seg_to_ptr_pages(struct xc_dom_image *dom,
390                                       struct xc_dom_seg *seg,
391                                       xen_pfn_t *pages_out)
392 {
393     void *retval;
394 
395     retval = xc_dom_pfn_to_ptr(dom, seg->pfn, seg->pages);
396 
397     *pages_out = retval ? seg->pages : 0;
398     return retval;
399 }
400 
xc_dom_seg_to_ptr(struct xc_dom_image * dom,struct xc_dom_seg * seg)401 static inline void *xc_dom_seg_to_ptr(struct xc_dom_image *dom,
402                                       struct xc_dom_seg *seg)
403 {
404     xen_pfn_t dummy;
405 
406     return xc_dom_seg_to_ptr_pages(dom, seg, &dummy);
407 }
408 
xc_dom_vaddr_to_ptr(struct xc_dom_image * dom,xen_vaddr_t vaddr,size_t * safe_region_out)409 static inline void *xc_dom_vaddr_to_ptr(struct xc_dom_image *dom,
410                                         xen_vaddr_t vaddr,
411                                         size_t *safe_region_out)
412 {
413     unsigned int page_size = XC_DOM_PAGE_SIZE(dom);
414     xen_pfn_t page = (vaddr - dom->parms.virt_base) / page_size;
415     unsigned int offset = (vaddr - dom->parms.virt_base) % page_size;
416     xen_pfn_t safe_region_count;
417     void *ptr;
418 
419     *safe_region_out = 0;
420     ptr = xc_dom_pfn_to_ptr_retcount(dom, page, 0, &safe_region_count);
421     if ( ptr == NULL )
422         return ptr;
423     *safe_region_out = (safe_region_count << XC_DOM_PAGE_SHIFT(dom)) - offset;
424     return ptr + offset;
425 }
426 
xc_dom_p2m(struct xc_dom_image * dom,xen_pfn_t pfn)427 static inline xen_pfn_t xc_dom_p2m(struct xc_dom_image *dom, xen_pfn_t pfn)
428 {
429     if ( xc_dom_translated(dom) )
430         return pfn;
431 
432     /* x86 PV only now. */
433     if ( pfn >= dom->total_pages )
434         return INVALID_MFN;
435 
436     return dom->pv_p2m[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