1 #ifndef QEMU_HW_XEN_COMMON_H
2 #define QEMU_HW_XEN_COMMON_H 1
7 * If we have new enough libxenctrl then we do not want/need these compat
8 * interfaces, despite what the user supplied cflags might say. They
9 * must be undefined before including xenctrl.h
11 #undef XC_WANT_COMPAT_EVTCHN_API
12 #undef XC_WANT_COMPAT_GNTTAB_API
13 #undef XC_WANT_COMPAT_MAP_FOREIGN_API
17 #include <xen/io/xenbus.h>
20 #include "hw/xen/xen.h"
21 #include "hw/pci/pci.h"
22 #include "qemu/queue.h"
26 * We don't support Xen prior to 4.2.0.
29 /* Xen 4.2 thru 4.6 */
30 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
32 typedef xc_interface xenforeignmemory_handle;
33 typedef xc_evtchn xenevtchn_handle;
34 typedef xc_gnttab xengnttab_handle;
36 #define xenevtchn_open(l, f) xc_evtchn_open(l, f);
37 #define xenevtchn_close(h) xc_evtchn_close(h)
38 #define xenevtchn_fd(h) xc_evtchn_fd(h)
39 #define xenevtchn_pending(h) xc_evtchn_pending(h)
40 #define xenevtchn_notify(h, p) xc_evtchn_notify(h, p)
41 #define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(h, d, p)
42 #define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p)
43 #define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p)
45 #define xengnttab_open(l, f) xc_gnttab_open(l, f)
46 #define xengnttab_close(h) xc_gnttab_close(h)
47 #define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(h, n)
48 #define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(h, d, r, p)
49 #define xengnttab_unmap(h, a, n) xc_gnttab_munmap(h, a, n)
50 #define xengnttab_map_grant_refs(h, c, d, r, p) \
51 xc_gnttab_map_grant_refs(h, c, d, r, p)
53 #define xenforeignmemory_open(l, f) xen_xc
55 static inline void *xenforeignmemory_map(xc_interface *h, uint32_t dom,
56 int prot, size_t pages,
57 const xen_pfn_t arr[/*pages*/],
61 return xc_map_foreign_bulk(h, dom, prot, arr, err, pages);
63 return xc_map_foreign_pages(h, dom, prot, arr, pages);
66 #define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE)
68 #else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
70 #include <xenevtchn.h>
71 #include <xengnttab.h>
72 #include <xenforeignmemory.h>
76 void destroy_hvm_domain(bool reboot);
78 /* shutdown/destroy current domain because of an error */
79 void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
81 #ifdef HVM_PARAM_VMPORT_REGS_PFN
82 static inline int xen_get_vmport_regs_pfn(xc_interface *xc, domid_t dom,
83 xen_pfn_t *vmport_regs_pfn)
87 rc = xc_hvm_param_get(xc, dom, HVM_PARAM_VMPORT_REGS_PFN, &value);
89 *vmport_regs_pfn = (xen_pfn_t) value;
94 static inline int xen_get_vmport_regs_pfn(xc_interface *xc, domid_t dom,
95 xen_pfn_t *vmport_regs_pfn)
102 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 460
104 #ifndef HVM_IOREQSRV_BUFIOREQ_ATOMIC
105 #define HVM_IOREQSRV_BUFIOREQ_ATOMIC 2
111 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 450
113 #ifndef HVM_PARAM_BUFIOREQ_EVTCHN
114 #define HVM_PARAM_BUFIOREQ_EVTCHN 26
117 #define IOREQ_TYPE_PCI_CONFIG 2
119 typedef uint16_t ioservid_t;
121 static inline void xen_map_memory_section(xc_interface *xc, domid_t dom,
123 MemoryRegionSection *section)
127 static inline void xen_unmap_memory_section(xc_interface *xc, domid_t dom,
129 MemoryRegionSection *section)
133 static inline void xen_map_io_section(xc_interface *xc, domid_t dom,
135 MemoryRegionSection *section)
139 static inline void xen_unmap_io_section(xc_interface *xc, domid_t dom,
141 MemoryRegionSection *section)
145 static inline void xen_map_pcidev(xc_interface *xc, domid_t dom,
151 static inline void xen_unmap_pcidev(xc_interface *xc, domid_t dom,
157 static inline int xen_create_ioreq_server(xc_interface *xc, domid_t dom,
158 ioservid_t *ioservid)
163 static inline void xen_destroy_ioreq_server(xc_interface *xc, domid_t dom,
168 static inline int xen_get_ioreq_server_info(xc_interface *xc, domid_t dom,
170 xen_pfn_t *ioreq_pfn,
171 xen_pfn_t *bufioreq_pfn,
172 evtchn_port_t *bufioreq_evtchn)
177 rc = xc_get_hvm_param(xc, dom, HVM_PARAM_IOREQ_PFN, ¶m);
179 fprintf(stderr, "failed to get HVM_PARAM_IOREQ_PFN\n");
185 rc = xc_get_hvm_param(xc, dom, HVM_PARAM_BUFIOREQ_PFN, ¶m);
187 fprintf(stderr, "failed to get HVM_PARAM_BUFIOREQ_PFN\n");
191 *bufioreq_pfn = param;
193 rc = xc_get_hvm_param(xc, dom, HVM_PARAM_BUFIOREQ_EVTCHN,
196 fprintf(stderr, "failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n");
200 *bufioreq_evtchn = param;
205 static inline int xen_set_ioreq_server_state(xc_interface *xc, domid_t dom,
215 static inline void xen_map_memory_section(xc_interface *xc, domid_t dom,
217 MemoryRegionSection *section)
219 hwaddr start_addr = section->offset_within_address_space;
220 ram_addr_t size = int128_get64(section->size);
221 hwaddr end_addr = start_addr + size - 1;
223 trace_xen_map_mmio_range(ioservid, start_addr, end_addr);
224 xc_hvm_map_io_range_to_ioreq_server(xc, dom, ioservid, 1,
225 start_addr, end_addr);
228 static inline void xen_unmap_memory_section(xc_interface *xc, domid_t dom,
230 MemoryRegionSection *section)
232 hwaddr start_addr = section->offset_within_address_space;
233 ram_addr_t size = int128_get64(section->size);
234 hwaddr end_addr = start_addr + size - 1;
236 trace_xen_unmap_mmio_range(ioservid, start_addr, end_addr);
237 xc_hvm_unmap_io_range_from_ioreq_server(xc, dom, ioservid, 1,
238 start_addr, end_addr);
241 static inline void xen_map_io_section(xc_interface *xc, domid_t dom,
243 MemoryRegionSection *section)
245 hwaddr start_addr = section->offset_within_address_space;
246 ram_addr_t size = int128_get64(section->size);
247 hwaddr end_addr = start_addr + size - 1;
249 trace_xen_map_portio_range(ioservid, start_addr, end_addr);
250 xc_hvm_map_io_range_to_ioreq_server(xc, dom, ioservid, 0,
251 start_addr, end_addr);
254 static inline void xen_unmap_io_section(xc_interface *xc, domid_t dom,
256 MemoryRegionSection *section)
258 hwaddr start_addr = section->offset_within_address_space;
259 ram_addr_t size = int128_get64(section->size);
260 hwaddr end_addr = start_addr + size - 1;
262 trace_xen_unmap_portio_range(ioservid, start_addr, end_addr);
263 xc_hvm_unmap_io_range_from_ioreq_server(xc, dom, ioservid, 0,
264 start_addr, end_addr);
267 static inline void xen_map_pcidev(xc_interface *xc, domid_t dom,
271 trace_xen_map_pcidev(ioservid, pci_bus_num(pci_dev->bus),
272 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn));
273 xc_hvm_map_pcidev_to_ioreq_server(xc, dom, ioservid,
274 0, pci_bus_num(pci_dev->bus),
275 PCI_SLOT(pci_dev->devfn),
276 PCI_FUNC(pci_dev->devfn));
279 static inline void xen_unmap_pcidev(xc_interface *xc, domid_t dom,
283 trace_xen_unmap_pcidev(ioservid, pci_bus_num(pci_dev->bus),
284 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn));
285 xc_hvm_unmap_pcidev_from_ioreq_server(xc, dom, ioservid,
286 0, pci_bus_num(pci_dev->bus),
287 PCI_SLOT(pci_dev->devfn),
288 PCI_FUNC(pci_dev->devfn));
291 static inline int xen_create_ioreq_server(xc_interface *xc, domid_t dom,
292 ioservid_t *ioservid)
294 int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
298 trace_xen_ioreq_server_create(*ioservid);
304 static inline void xen_destroy_ioreq_server(xc_interface *xc, domid_t dom,
307 trace_xen_ioreq_server_destroy(ioservid);
308 xc_hvm_destroy_ioreq_server(xc, dom, ioservid);
311 static inline int xen_get_ioreq_server_info(xc_interface *xc, domid_t dom,
313 xen_pfn_t *ioreq_pfn,
314 xen_pfn_t *bufioreq_pfn,
315 evtchn_port_t *bufioreq_evtchn)
317 return xc_hvm_get_ioreq_server_info(xc, dom, ioservid,
318 ioreq_pfn, bufioreq_pfn,
322 static inline int xen_set_ioreq_server_state(xc_interface *xc, domid_t dom,
326 trace_xen_ioreq_server_state(ioservid, enable);
327 return xc_hvm_set_ioreq_server_state(xc, dom, ioservid, enable);
332 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 460
333 static inline int xen_xc_domain_add_to_physmap(xc_interface *xch, uint32_t domid,
338 return xc_domain_add_to_physmap(xch, domid, space, idx, gpfn);
341 static inline int xen_xc_domain_add_to_physmap(xc_interface *xch, uint32_t domid,
346 /* In Xen 4.6 rc is -1 and errno contains the error value. */
347 int rc = xc_domain_add_to_physmap(xch, domid, space, idx, gpfn);
354 #ifdef CONFIG_XEN_PV_DOMAIN_BUILD
355 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 470
356 static inline int xen_domain_create(xc_interface *xc, uint32_t ssidref,
357 xen_domain_handle_t handle, uint32_t flags,
360 return xc_domain_create(xc, ssidref, handle, flags, pdomid);
363 static inline int xen_domain_create(xc_interface *xc, uint32_t ssidref,
364 xen_domain_handle_t handle, uint32_t flags,
367 return xc_domain_create(xc, ssidref, handle, flags, pdomid, NULL);
372 #endif /* QEMU_HW_XEN_COMMON_H */