xen_backend_ops.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * QEMU Xen backend support
  3. *
  4. * Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * Authors: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  9. * See the COPYING file in the top-level directory.
  10. */
  11. #ifndef QEMU_XEN_BACKEND_OPS_H
  12. #define QEMU_XEN_BACKEND_OPS_H
  13. #include "hw/xen/xen.h"
  14. #include "hw/xen/interface/xen.h"
  15. #include "hw/xen/interface/io/xenbus.h"
  16. /*
  17. * For the time being, these operations map fairly closely to the API of
  18. * the actual Xen libraries, e.g. libxenevtchn. As we complete the migration
  19. * from XenLegacyDevice back ends to the new XenDevice model, they may
  20. * evolve to slightly higher-level APIs.
  21. *
  22. * The internal emulations do not emulate the Xen APIs entirely faithfully;
  23. * only enough to be used by the Xen backend devices. For example, only one
  24. * event channel can be bound to each handle, since that's sufficient for
  25. * the device support (only the true Xen HVM backend uses more). And the
  26. * behaviour of unmask() and pending() is different too because the device
  27. * backends don't care.
  28. */
  29. typedef struct xenevtchn_handle xenevtchn_handle;
  30. typedef int xenevtchn_port_or_error_t;
  31. typedef uint32_t evtchn_port_t;
  32. typedef uint16_t domid_t;
  33. typedef uint32_t grant_ref_t;
  34. #define XEN_PAGE_SHIFT 12
  35. #define XEN_PAGE_SIZE (1UL << XEN_PAGE_SHIFT)
  36. #define XEN_PAGE_MASK (~(XEN_PAGE_SIZE - 1))
  37. #ifndef xen_rmb
  38. #define xen_rmb() smp_rmb()
  39. #endif
  40. #ifndef xen_wmb
  41. #define xen_wmb() smp_wmb()
  42. #endif
  43. #ifndef xen_mb
  44. #define xen_mb() smp_mb()
  45. #endif
  46. struct evtchn_backend_ops {
  47. xenevtchn_handle *(*open)(void);
  48. int (*bind_interdomain)(xenevtchn_handle *xc, uint32_t domid,
  49. evtchn_port_t guest_port);
  50. int (*unbind)(xenevtchn_handle *xc, evtchn_port_t port);
  51. int (*close)(struct xenevtchn_handle *xc);
  52. int (*get_fd)(struct xenevtchn_handle *xc);
  53. int (*notify)(struct xenevtchn_handle *xc, evtchn_port_t port);
  54. int (*unmask)(struct xenevtchn_handle *xc, evtchn_port_t port);
  55. int (*pending)(struct xenevtchn_handle *xc);
  56. };
  57. extern struct evtchn_backend_ops *xen_evtchn_ops;
  58. static inline xenevtchn_handle *qemu_xen_evtchn_open(void)
  59. {
  60. if (!xen_evtchn_ops) {
  61. return NULL;
  62. }
  63. return xen_evtchn_ops->open();
  64. }
  65. static inline int qemu_xen_evtchn_bind_interdomain(xenevtchn_handle *xc,
  66. uint32_t domid,
  67. evtchn_port_t guest_port)
  68. {
  69. if (!xen_evtchn_ops) {
  70. return -ENOSYS;
  71. }
  72. return xen_evtchn_ops->bind_interdomain(xc, domid, guest_port);
  73. }
  74. static inline int qemu_xen_evtchn_unbind(xenevtchn_handle *xc,
  75. evtchn_port_t port)
  76. {
  77. if (!xen_evtchn_ops) {
  78. return -ENOSYS;
  79. }
  80. return xen_evtchn_ops->unbind(xc, port);
  81. }
  82. static inline int qemu_xen_evtchn_close(xenevtchn_handle *xc)
  83. {
  84. if (!xen_evtchn_ops) {
  85. return -ENOSYS;
  86. }
  87. return xen_evtchn_ops->close(xc);
  88. }
  89. static inline int qemu_xen_evtchn_fd(xenevtchn_handle *xc)
  90. {
  91. if (!xen_evtchn_ops) {
  92. return -ENOSYS;
  93. }
  94. return xen_evtchn_ops->get_fd(xc);
  95. }
  96. static inline int qemu_xen_evtchn_notify(xenevtchn_handle *xc,
  97. evtchn_port_t port)
  98. {
  99. if (!xen_evtchn_ops) {
  100. return -ENOSYS;
  101. }
  102. return xen_evtchn_ops->notify(xc, port);
  103. }
  104. static inline int qemu_xen_evtchn_unmask(xenevtchn_handle *xc,
  105. evtchn_port_t port)
  106. {
  107. if (!xen_evtchn_ops) {
  108. return -ENOSYS;
  109. }
  110. return xen_evtchn_ops->unmask(xc, port);
  111. }
  112. static inline int qemu_xen_evtchn_pending(xenevtchn_handle *xc)
  113. {
  114. if (!xen_evtchn_ops) {
  115. return -ENOSYS;
  116. }
  117. return xen_evtchn_ops->pending(xc);
  118. }
  119. typedef struct xengntdev_handle xengnttab_handle;
  120. typedef struct XenGrantCopySegment {
  121. union {
  122. void *virt;
  123. struct {
  124. uint32_t ref;
  125. off_t offset;
  126. } foreign;
  127. } source, dest;
  128. size_t len;
  129. } XenGrantCopySegment;
  130. #define XEN_GNTTAB_OP_FEATURE_MAP_MULTIPLE (1U << 0)
  131. struct gnttab_backend_ops {
  132. uint32_t features;
  133. xengnttab_handle *(*open)(void);
  134. int (*close)(xengnttab_handle *xgt);
  135. int (*grant_copy)(xengnttab_handle *xgt, bool to_domain, uint32_t domid,
  136. XenGrantCopySegment *segs, uint32_t nr_segs,
  137. Error **errp);
  138. int (*set_max_grants)(xengnttab_handle *xgt, uint32_t nr_grants);
  139. void *(*map_refs)(xengnttab_handle *xgt, uint32_t count, uint32_t domid,
  140. uint32_t *refs, int prot);
  141. int (*unmap)(xengnttab_handle *xgt, void *start_address, uint32_t *refs,
  142. uint32_t count);
  143. };
  144. extern struct gnttab_backend_ops *xen_gnttab_ops;
  145. static inline bool qemu_xen_gnttab_can_map_multi(void)
  146. {
  147. return xen_gnttab_ops &&
  148. !!(xen_gnttab_ops->features & XEN_GNTTAB_OP_FEATURE_MAP_MULTIPLE);
  149. }
  150. static inline xengnttab_handle *qemu_xen_gnttab_open(void)
  151. {
  152. if (!xen_gnttab_ops) {
  153. return NULL;
  154. }
  155. return xen_gnttab_ops->open();
  156. }
  157. static inline int qemu_xen_gnttab_close(xengnttab_handle *xgt)
  158. {
  159. if (!xen_gnttab_ops) {
  160. return -ENOSYS;
  161. }
  162. return xen_gnttab_ops->close(xgt);
  163. }
  164. static inline int qemu_xen_gnttab_grant_copy(xengnttab_handle *xgt,
  165. bool to_domain, uint32_t domid,
  166. XenGrantCopySegment *segs,
  167. uint32_t nr_segs, Error **errp)
  168. {
  169. if (!xen_gnttab_ops) {
  170. return -ENOSYS;
  171. }
  172. return xen_gnttab_ops->grant_copy(xgt, to_domain, domid, segs, nr_segs,
  173. errp);
  174. }
  175. static inline int qemu_xen_gnttab_set_max_grants(xengnttab_handle *xgt,
  176. uint32_t nr_grants)
  177. {
  178. if (!xen_gnttab_ops) {
  179. return -ENOSYS;
  180. }
  181. return xen_gnttab_ops->set_max_grants(xgt, nr_grants);
  182. }
  183. static inline void *qemu_xen_gnttab_map_refs(xengnttab_handle *xgt,
  184. uint32_t count, uint32_t domid,
  185. uint32_t *refs, int prot)
  186. {
  187. if (!xen_gnttab_ops) {
  188. return NULL;
  189. }
  190. return xen_gnttab_ops->map_refs(xgt, count, domid, refs, prot);
  191. }
  192. static inline int qemu_xen_gnttab_unmap(xengnttab_handle *xgt,
  193. void *start_address, uint32_t *refs,
  194. uint32_t count)
  195. {
  196. if (!xen_gnttab_ops) {
  197. return -ENOSYS;
  198. }
  199. return xen_gnttab_ops->unmap(xgt, start_address, refs, count);
  200. }
  201. struct foreignmem_backend_ops {
  202. void *(*map)(uint32_t dom, void *addr, int prot, size_t pages,
  203. xen_pfn_t *pfns, int *errs);
  204. int (*unmap)(void *addr, size_t pages);
  205. };
  206. extern struct foreignmem_backend_ops *xen_foreignmem_ops;
  207. static inline void *qemu_xen_foreignmem_map(uint32_t dom, void *addr, int prot,
  208. size_t pages, xen_pfn_t *pfns,
  209. int *errs)
  210. {
  211. if (!xen_foreignmem_ops) {
  212. return NULL;
  213. }
  214. return xen_foreignmem_ops->map(dom, addr, prot, pages, pfns, errs);
  215. }
  216. static inline int qemu_xen_foreignmem_unmap(void *addr, size_t pages)
  217. {
  218. if (!xen_foreignmem_ops) {
  219. return -ENOSYS;
  220. }
  221. return xen_foreignmem_ops->unmap(addr, pages);
  222. }
  223. typedef void (*xs_watch_fn)(void *opaque, const char *path);
  224. struct qemu_xs_handle;
  225. struct qemu_xs_watch;
  226. typedef uint32_t xs_transaction_t;
  227. #define XBT_NULL 0
  228. #define XS_PERM_NONE 0x00
  229. #define XS_PERM_READ 0x01
  230. #define XS_PERM_WRITE 0x02
  231. struct xenstore_backend_ops {
  232. struct qemu_xs_handle *(*open)(void);
  233. void (*close)(struct qemu_xs_handle *h);
  234. char *(*get_domain_path)(struct qemu_xs_handle *h, unsigned int domid);
  235. char **(*directory)(struct qemu_xs_handle *h, xs_transaction_t t,
  236. const char *path, unsigned int *num);
  237. void *(*read)(struct qemu_xs_handle *h, xs_transaction_t t,
  238. const char *path, unsigned int *len);
  239. bool (*write)(struct qemu_xs_handle *h, xs_transaction_t t,
  240. const char *path, const void *data, unsigned int len);
  241. bool (*create)(struct qemu_xs_handle *h, xs_transaction_t t,
  242. unsigned int owner, unsigned int domid,
  243. unsigned int perms, const char *path);
  244. bool (*destroy)(struct qemu_xs_handle *h, xs_transaction_t t,
  245. const char *path);
  246. struct qemu_xs_watch *(*watch)(struct qemu_xs_handle *h, const char *path,
  247. xs_watch_fn fn, void *opaque);
  248. void (*unwatch)(struct qemu_xs_handle *h, struct qemu_xs_watch *w);
  249. xs_transaction_t (*transaction_start)(struct qemu_xs_handle *h);
  250. bool (*transaction_end)(struct qemu_xs_handle *h, xs_transaction_t t,
  251. bool abort);
  252. };
  253. extern struct xenstore_backend_ops *xen_xenstore_ops;
  254. static inline struct qemu_xs_handle *qemu_xen_xs_open(void)
  255. {
  256. if (!xen_xenstore_ops) {
  257. return NULL;
  258. }
  259. return xen_xenstore_ops->open();
  260. }
  261. static inline void qemu_xen_xs_close(struct qemu_xs_handle *h)
  262. {
  263. if (!xen_xenstore_ops) {
  264. return;
  265. }
  266. xen_xenstore_ops->close(h);
  267. }
  268. static inline char *qemu_xen_xs_get_domain_path(struct qemu_xs_handle *h,
  269. unsigned int domid)
  270. {
  271. if (!xen_xenstore_ops) {
  272. return NULL;
  273. }
  274. return xen_xenstore_ops->get_domain_path(h, domid);
  275. }
  276. static inline char **qemu_xen_xs_directory(struct qemu_xs_handle *h,
  277. xs_transaction_t t, const char *path,
  278. unsigned int *num)
  279. {
  280. if (!xen_xenstore_ops) {
  281. return NULL;
  282. }
  283. return xen_xenstore_ops->directory(h, t, path, num);
  284. }
  285. static inline void *qemu_xen_xs_read(struct qemu_xs_handle *h,
  286. xs_transaction_t t, const char *path,
  287. unsigned int *len)
  288. {
  289. if (!xen_xenstore_ops) {
  290. return NULL;
  291. }
  292. return xen_xenstore_ops->read(h, t, path, len);
  293. }
  294. static inline bool qemu_xen_xs_write(struct qemu_xs_handle *h,
  295. xs_transaction_t t, const char *path,
  296. const void *data, unsigned int len)
  297. {
  298. if (!xen_xenstore_ops) {
  299. return false;
  300. }
  301. return xen_xenstore_ops->write(h, t, path, data, len);
  302. }
  303. static inline bool qemu_xen_xs_create(struct qemu_xs_handle *h,
  304. xs_transaction_t t, unsigned int owner,
  305. unsigned int domid, unsigned int perms,
  306. const char *path)
  307. {
  308. if (!xen_xenstore_ops) {
  309. return false;
  310. }
  311. return xen_xenstore_ops->create(h, t, owner, domid, perms, path);
  312. }
  313. static inline bool qemu_xen_xs_destroy(struct qemu_xs_handle *h,
  314. xs_transaction_t t, const char *path)
  315. {
  316. if (!xen_xenstore_ops) {
  317. return false;
  318. }
  319. return xen_xenstore_ops->destroy(h, t, path);
  320. }
  321. static inline struct qemu_xs_watch *qemu_xen_xs_watch(struct qemu_xs_handle *h,
  322. const char *path,
  323. xs_watch_fn fn,
  324. void *opaque)
  325. {
  326. if (!xen_xenstore_ops) {
  327. return NULL;
  328. }
  329. return xen_xenstore_ops->watch(h, path, fn, opaque);
  330. }
  331. static inline void qemu_xen_xs_unwatch(struct qemu_xs_handle *h,
  332. struct qemu_xs_watch *w)
  333. {
  334. if (!xen_xenstore_ops) {
  335. return;
  336. }
  337. xen_xenstore_ops->unwatch(h, w);
  338. }
  339. static inline xs_transaction_t qemu_xen_xs_transaction_start(struct qemu_xs_handle *h)
  340. {
  341. if (!xen_xenstore_ops) {
  342. return XBT_NULL;
  343. }
  344. return xen_xenstore_ops->transaction_start(h);
  345. }
  346. static inline bool qemu_xen_xs_transaction_end(struct qemu_xs_handle *h,
  347. xs_transaction_t t, bool abort)
  348. {
  349. if (!xen_xenstore_ops) {
  350. return false;
  351. }
  352. return xen_xenstore_ops->transaction_end(h, t, abort);
  353. }
  354. void setup_xen_backend_ops(void);
  355. #endif /* QEMU_XEN_BACKEND_OPS_H */