You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xengrant.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef _IPXE_XENGRANT_H
  2. #define _IPXE_XENGRANT_H
  3. /** @file
  4. *
  5. * Xen grant tables
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <stdint.h>
  10. #include <ipxe/io.h>
  11. #include <ipxe/xen.h>
  12. #include <xen/grant_table.h>
  13. /**
  14. * Query grant table size
  15. *
  16. * @v xen Xen hypervisor
  17. * @v size Table size
  18. * @ret xenrc Xen status code
  19. */
  20. static inline __attribute__ (( always_inline )) int
  21. xengrant_query_size ( struct xen_hypervisor *xen,
  22. struct gnttab_query_size *size ) {
  23. return xen_hypercall_3 ( xen, __HYPERVISOR_grant_table_op,
  24. GNTTABOP_query_size,
  25. virt_to_phys ( size ), 1 );
  26. }
  27. /**
  28. * Set grant table version
  29. *
  30. * @v xen Xen hypervisor
  31. * @v version Version
  32. * @ret xenrc Xen status code
  33. */
  34. static inline __attribute__ (( always_inline )) int
  35. xengrant_set_version ( struct xen_hypervisor *xen,
  36. struct gnttab_set_version *version ) {
  37. return xen_hypercall_3 ( xen, __HYPERVISOR_grant_table_op,
  38. GNTTABOP_set_version,
  39. virt_to_phys ( version ), 1 );
  40. }
  41. /**
  42. * Invalidate access to a page
  43. *
  44. * @v xen Xen hypervisor
  45. * @v ref Grant reference
  46. */
  47. static inline __attribute__ (( always_inline )) void
  48. xengrant_invalidate ( struct xen_hypervisor *xen, grant_ref_t ref ) {
  49. union grant_entry_v2 *entry = &xen->grant.table[ref];
  50. /* Sanity check */
  51. assert ( ( readw ( &entry->hdr.flags ) &
  52. ( GTF_reading | GTF_writing ) ) == 0 );
  53. /* This should apparently be done using a cmpxchg instruction.
  54. * We omit this: partly in the interests of simplicity, but
  55. * mainly since our control flow generally does not permit
  56. * failure paths to themselves fail.
  57. */
  58. writew ( 0, &entry->hdr.flags );
  59. }
  60. /**
  61. * Permit access to a page
  62. *
  63. * @v xen Xen hypervisor
  64. * @v ref Grant reference
  65. * @v domid Domain ID
  66. * @v subflags Additional flags
  67. * @v page Page start
  68. */
  69. static inline __attribute__ (( always_inline )) void
  70. xengrant_permit_access ( struct xen_hypervisor *xen, grant_ref_t ref,
  71. domid_t domid, unsigned int subflags, void *page ) {
  72. union grant_entry_v2 *entry = &xen->grant.table[ref];
  73. unsigned long frame = ( virt_to_phys ( page ) / PAGE_SIZE );
  74. writew ( domid, &entry->full_page.hdr.domid );
  75. if ( sizeof ( physaddr_t ) == sizeof ( uint64_t ) ) {
  76. writeq ( frame, &entry->full_page.frame );
  77. } else {
  78. writel ( frame, &entry->full_page.frame );
  79. }
  80. wmb();
  81. writew ( ( GTF_permit_access | subflags ), &entry->full_page.hdr.flags);
  82. wmb();
  83. }
  84. extern int xengrant_alloc ( struct xen_hypervisor *xen, grant_ref_t *refs,
  85. unsigned int count );
  86. extern void xengrant_free ( struct xen_hypervisor *xen, grant_ref_t *refs,
  87. unsigned int count );
  88. #endif /* _IPXE_XENGRANT_H */