1 #ifndef QEMU_HW_XEN_COMMON_H
2 #define QEMU_HW_XEN_COMMON_H 1
4 #include "config-host.h"
10 * If we have new enough libxenctrl then we do not want/need these compat
11 * interfaces, despite what the user supplied cflags might say. They
12 * must be undefined before including xenctrl.h
14 #undef XC_WANT_COMPAT_EVTCHN_API
15 #undef XC_WANT_COMPAT_GNTTAB_API
16 #undef XC_WANT_COMPAT_MAP_FOREIGN_API
20 #include <xen/io/xenbus.h>
23 #include "hw/xen/xen.h"
24 #include "hw/pci/pci.h"
25 #include "qemu/queue.h"
29 * We don't support Xen prior to 4.2.0.
32 /* Xen 4.2 thru 4.6 */
33 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
35 typedef xc_interface xenforeignmemory_handle
;
36 typedef xc_evtchn xenevtchn_handle
;
37 typedef xc_gnttab xengnttab_handle
;
39 #define xenevtchn_open(l, f) xc_evtchn_open(l, f);
40 #define xenevtchn_close(h) xc_evtchn_close(h)
41 #define xenevtchn_fd(h) xc_evtchn_fd(h)
42 #define xenevtchn_pending(h) xc_evtchn_pending(h)
43 #define xenevtchn_notify(h, p) xc_evtchn_notify(h, p)
44 #define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(h, d, p)
45 #define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p)
46 #define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p)
48 #define xengnttab_open(l, f) xc_gnttab_open(l, f)
49 #define xengnttab_close(h) xc_gnttab_close(h)
50 #define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(h, n)
51 #define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(h, d, r, p)
52 #define xengnttab_unmap(h, a, n) xc_gnttab_munmap(h, a, n)
53 #define xengnttab_map_grant_refs(h, c, d, r, p) \
54 xc_gnttab_map_grant_refs(h, c, d, r, p)
56 /* See below for xenforeignmemory_* APIs */
58 #else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
60 #include <xenevtchn.h>
61 #include <xengnttab.h>
62 #include <xenforeignmemory.h>
66 void destroy_hvm_domain(bool reboot
);
68 /* shutdown/destroy current domain because of an error */
69 void xen_shutdown_fatal_error(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
71 #ifdef HVM_PARAM_VMPORT_REGS_PFN
72 static inline int xen_get_vmport_regs_pfn(xc_interface
*xc
, domid_t dom
,
73 xen_pfn_t
*vmport_regs_pfn
)
77 rc
= xc_hvm_param_get(xc
, dom
, HVM_PARAM_VMPORT_REGS_PFN
, &value
);
79 *vmport_regs_pfn
= (xen_pfn_t
) value
;
84 static inline int xen_get_vmport_regs_pfn(xc_interface
*xc
, domid_t dom
,
85 xen_pfn_t
*vmport_regs_pfn
)
92 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 460
94 #ifndef HVM_IOREQSRV_BUFIOREQ_ATOMIC
95 #define HVM_IOREQSRV_BUFIOREQ_ATOMIC 2
101 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 450
103 #ifndef HVM_PARAM_BUFIOREQ_EVTCHN
104 #define HVM_PARAM_BUFIOREQ_EVTCHN 26
107 #define IOREQ_TYPE_PCI_CONFIG 2
109 typedef uint16_t ioservid_t
;
111 static inline void xen_map_memory_section(xc_interface
*xc
, domid_t dom
,
113 MemoryRegionSection
*section
)
117 static inline void xen_unmap_memory_section(xc_interface
*xc
, domid_t dom
,
119 MemoryRegionSection
*section
)
123 static inline void xen_map_io_section(xc_interface
*xc
, domid_t dom
,
125 MemoryRegionSection
*section
)
129 static inline void xen_unmap_io_section(xc_interface
*xc
, domid_t dom
,
131 MemoryRegionSection
*section
)
135 static inline void xen_map_pcidev(xc_interface
*xc
, domid_t dom
,
141 static inline void xen_unmap_pcidev(xc_interface
*xc
, domid_t dom
,
147 static inline int xen_create_ioreq_server(xc_interface
*xc
, domid_t dom
,
148 ioservid_t
*ioservid
)
153 static inline void xen_destroy_ioreq_server(xc_interface
*xc
, domid_t dom
,
158 static inline int xen_get_ioreq_server_info(xc_interface
*xc
, domid_t dom
,
160 xen_pfn_t
*ioreq_pfn
,
161 xen_pfn_t
*bufioreq_pfn
,
162 evtchn_port_t
*bufioreq_evtchn
)
167 rc
= xc_get_hvm_param(xc
, dom
, HVM_PARAM_IOREQ_PFN
, ¶m
);
169 fprintf(stderr
, "failed to get HVM_PARAM_IOREQ_PFN\n");
175 rc
= xc_get_hvm_param(xc
, dom
, HVM_PARAM_BUFIOREQ_PFN
, ¶m
);
177 fprintf(stderr
, "failed to get HVM_PARAM_BUFIOREQ_PFN\n");
181 *bufioreq_pfn
= param
;
183 rc
= xc_get_hvm_param(xc
, dom
, HVM_PARAM_BUFIOREQ_EVTCHN
,
186 fprintf(stderr
, "failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n");
190 *bufioreq_evtchn
= param
;
195 static inline int xen_set_ioreq_server_state(xc_interface
*xc
, domid_t dom
,
205 static inline void xen_map_memory_section(xc_interface
*xc
, domid_t dom
,
207 MemoryRegionSection
*section
)
209 hwaddr start_addr
= section
->offset_within_address_space
;
210 ram_addr_t size
= int128_get64(section
->size
);
211 hwaddr end_addr
= start_addr
+ size
- 1;
213 trace_xen_map_mmio_range(ioservid
, start_addr
, end_addr
);
214 xc_hvm_map_io_range_to_ioreq_server(xc
, dom
, ioservid
, 1,
215 start_addr
, end_addr
);
218 static inline void xen_unmap_memory_section(xc_interface
*xc
, domid_t dom
,
220 MemoryRegionSection
*section
)
222 hwaddr start_addr
= section
->offset_within_address_space
;
223 ram_addr_t size
= int128_get64(section
->size
);
224 hwaddr end_addr
= start_addr
+ size
- 1;
226 trace_xen_unmap_mmio_range(ioservid
, start_addr
, end_addr
);
227 xc_hvm_unmap_io_range_from_ioreq_server(xc
, dom
, ioservid
, 1,
228 start_addr
, end_addr
);
231 static inline void xen_map_io_section(xc_interface
*xc
, domid_t dom
,
233 MemoryRegionSection
*section
)
235 hwaddr start_addr
= section
->offset_within_address_space
;
236 ram_addr_t size
= int128_get64(section
->size
);
237 hwaddr end_addr
= start_addr
+ size
- 1;
239 trace_xen_map_portio_range(ioservid
, start_addr
, end_addr
);
240 xc_hvm_map_io_range_to_ioreq_server(xc
, dom
, ioservid
, 0,
241 start_addr
, end_addr
);
244 static inline void xen_unmap_io_section(xc_interface
*xc
, domid_t dom
,
246 MemoryRegionSection
*section
)
248 hwaddr start_addr
= section
->offset_within_address_space
;
249 ram_addr_t size
= int128_get64(section
->size
);
250 hwaddr end_addr
= start_addr
+ size
- 1;
252 trace_xen_unmap_portio_range(ioservid
, start_addr
, end_addr
);
253 xc_hvm_unmap_io_range_from_ioreq_server(xc
, dom
, ioservid
, 0,
254 start_addr
, end_addr
);
257 static inline void xen_map_pcidev(xc_interface
*xc
, domid_t dom
,
261 trace_xen_map_pcidev(ioservid
, pci_bus_num(pci_dev
->bus
),
262 PCI_SLOT(pci_dev
->devfn
), PCI_FUNC(pci_dev
->devfn
));
263 xc_hvm_map_pcidev_to_ioreq_server(xc
, dom
, ioservid
,
264 0, pci_bus_num(pci_dev
->bus
),
265 PCI_SLOT(pci_dev
->devfn
),
266 PCI_FUNC(pci_dev
->devfn
));
269 static inline void xen_unmap_pcidev(xc_interface
*xc
, domid_t dom
,
273 trace_xen_unmap_pcidev(ioservid
, pci_bus_num(pci_dev
->bus
),
274 PCI_SLOT(pci_dev
->devfn
), PCI_FUNC(pci_dev
->devfn
));
275 xc_hvm_unmap_pcidev_from_ioreq_server(xc
, dom
, ioservid
,
276 0, pci_bus_num(pci_dev
->bus
),
277 PCI_SLOT(pci_dev
->devfn
),
278 PCI_FUNC(pci_dev
->devfn
));
281 static inline int xen_create_ioreq_server(xc_interface
*xc
, domid_t dom
,
282 ioservid_t
*ioservid
)
284 int rc
= xc_hvm_create_ioreq_server(xc
, dom
, HVM_IOREQSRV_BUFIOREQ_ATOMIC
,
288 trace_xen_ioreq_server_create(*ioservid
);
294 static inline void xen_destroy_ioreq_server(xc_interface
*xc
, domid_t dom
,
297 trace_xen_ioreq_server_destroy(ioservid
);
298 xc_hvm_destroy_ioreq_server(xc
, dom
, ioservid
);
301 static inline int xen_get_ioreq_server_info(xc_interface
*xc
, domid_t dom
,
303 xen_pfn_t
*ioreq_pfn
,
304 xen_pfn_t
*bufioreq_pfn
,
305 evtchn_port_t
*bufioreq_evtchn
)
307 return xc_hvm_get_ioreq_server_info(xc
, dom
, ioservid
,
308 ioreq_pfn
, bufioreq_pfn
,
312 static inline int xen_set_ioreq_server_state(xc_interface
*xc
, domid_t dom
,
316 trace_xen_ioreq_server_state(ioservid
, enable
);
317 return xc_hvm_set_ioreq_server_state(xc
, dom
, ioservid
, enable
);
322 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 460
323 static inline int xen_xc_domain_add_to_physmap(xc_interface
*xch
, uint32_t domid
,
328 return xc_domain_add_to_physmap(xch
, domid
, space
, idx
, gpfn
);
331 static inline int xen_xc_domain_add_to_physmap(xc_interface
*xch
, uint32_t domid
,
336 /* In Xen 4.6 rc is -1 and errno contains the error value. */
337 int rc
= xc_domain_add_to_physmap(xch
, domid
, space
, idx
, gpfn
);
344 #ifdef CONFIG_XEN_PV_DOMAIN_BUILD
345 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 470
346 static inline int xen_domain_create(xc_interface
*xc
, uint32_t ssidref
,
347 xen_domain_handle_t handle
, uint32_t flags
,
350 return xc_domain_create(xc
, ssidref
, handle
, flags
, pdomid
);
353 static inline int xen_domain_create(xc_interface
*xc
, uint32_t ssidref
,
354 xen_domain_handle_t handle
, uint32_t flags
,
357 return xc_domain_create(xc
, ssidref
, handle
, flags
, pdomid
, NULL
);
362 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
364 #define xenforeignmemory_open(l, f) xen_xc
366 static inline void *xenforeignmemory_map(xc_interface
*h
, uint32_t dom
,
367 int prot
, size_t pages
,
368 const xen_pfn_t arr
[/*pages*/],
372 return xc_map_foreign_bulk(h
, dom
, prot
, arr
, err
, pages
);
374 return xc_map_foreign_pages(h
, dom
, prot
, arr
, pages
);
377 #define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE)
381 #endif /* QEMU_HW_XEN_COMMON_H */