mksck_shared.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief The monitor-kernel socket interface shared area definitions.
  24. */
  25. #ifndef _MKSCK_SHARED_H
  26. #define _MKSCK_SHARED_H
  27. #define INCLUDE_ALLOW_MVPD
  28. #define INCLUDE_ALLOW_VMX
  29. #define INCLUDE_ALLOW_MODULE
  30. #define INCLUDE_ALLOW_MONITOR
  31. #define INCLUDE_ALLOW_GPL
  32. #include "include_check.h"
  33. /*
  34. * Allocated MksckPages are stored in an array of size
  35. * MKSCK_MAX_SHARES. The vmid and the slot index of a shared page is
  36. * not unrelated: vmid = idx%MKSCK_MAX_SHARES.
  37. */
  38. #define MKSCK_MAX_SHARES_LOG2 4 /* 16: one per VM + one per VCPU */
  39. #define MKSCK_MAX_SHARES (1U << MKSCK_MAX_SHARES_LOG2)
  40. #define MKSCK_VMID2IDX(idx) ((idx)%MKSCK_MAX_SHARES)
  41. #define MKSCK_TGID2VMID(tgid) (((((tgid)<<1)^((tgid)>>15))&0xfffe)|1)
  42. /*
  43. * The size of a shared page determines how many sockets can be open
  44. * concurrently.
  45. */
  46. #define MKSCKPAGE_TOTAL 8 /* number of shared pages */
  47. #define MKSCKPAGE_SIZE (PAGE_SIZE * MKSCKPAGE_TOTAL)
  48. #define MKSCK_SOCKETS_PER_PAGE \
  49. ((MKSCKPAGE_SIZE-offsetof(MksckPage, sockets[0])) / sizeof(Mksck))
  50. /*
  51. * Individual datagrams are aligned on a MKSCK_ALIGNMENT byte boundary
  52. * in the data receive area of a socket.
  53. */
  54. #define MKSCK_ALIGNMENT 8 /* data packet alignment */
  55. #define MKSCK_ALIGN(x) MVP_ALIGN(x, MKSCK_ALIGNMENT)
  56. #define MKSCK_DGSIZE(len) offsetof(Mksck_Datagram, data[MKSCK_ALIGN(len)])
  57. #define MKSCK_BUFSIZE MKSCK_DGSIZE(MKSCK_XFER_MAX + 1)
  58. /*
  59. * Conditional variables for sleeping on.
  60. */
  61. #define MKSCK_CVAR_ROOM 0 /* senders waiting for room for message */
  62. #define MKSCK_CVAR_FILL 1 /* receivers waiting for a message to fetch */
  63. #define MKSCK_FINDSENDROOM_FULL 0xFFFFFFFFU
  64. /*
  65. * Shutdown bits
  66. */
  67. #define MKSCK_SHUT_WR (1 << 0) /* socket can't send data anymore */
  68. #define MKSCK_SHUT_RD (1 << 1) /* socket can't receive data anymore */
  69. typedef struct Mksck Mksck;
  70. typedef struct Mksck_Datagram Mksck_Datagram;
  71. typedef struct MksckPage MksckPage;
  72. #include "atomic.h"
  73. #include "mksck.h"
  74. #include "mmu_defs.h"
  75. #include "mutex.h"
  76. #include "arm_inline.h"
  77. /**
  78. * @brief Monitor-kernel socket datagram structure
  79. */
  80. struct Mksck_Datagram {
  81. Mksck_Address fromAddr; /**< source address */
  82. uint32 len:16; /**< length of the data */
  83. uint32 pad:3; /**< padding between untyped message and */
  84. /**< mpn array. */
  85. uint32 pages:13; /**< number of pages in mpn array */
  86. uint8 data[1] /**< start of the data */
  87. __attribute__((aligned(MKSCK_ALIGNMENT)));
  88. };
  89. /**
  90. * @brief one particular socket's shared page data.
  91. */
  92. struct Mksck {
  93. AtmUInt32 refCount; /**< when zero, struct is free */
  94. /**< ... increment only with mksckPage->mutex */
  95. /**< ... decrement at any time */
  96. Mksck_Address addr; /**< this socket's address if open */
  97. /**< ... MKSCK_ADDR_UNDEF if closed */
  98. /**< ... open only with mksckPage->mutex */
  99. Mksck_Address peerAddr; /**< peer's address if connected */
  100. /**< ... MKSCK_ADDR_UNDEF if not */
  101. struct Mksck *peer; /**< connected peer's ptr or NULL if not */
  102. /**< ... ptr is MVA for monitor sockets and */
  103. /**< ... HKVA for sockets of host processes */
  104. /**< ... holds ref count on target socket */
  105. uint32 index; /**< index of this socket in page */
  106. /**< empty ring indicated by read == write */
  107. /**< ring never completely fills, always at */
  108. /**< least room for one more byte so we can */
  109. /**< tell empty from full */
  110. uint32 write; /**< index within buff to insert next data */
  111. /**< ... always < MKSCK_BUFSIZE */
  112. uint32 read; /**< index within buff to remove next data */
  113. /**< ... always < MKSCK_BUFSIZE */
  114. uint32 wrap; /**< current wrapping point */
  115. /**< ... valid only whenever write < read */
  116. uint32 shutDown; /**< MKSCK_SHUT_RD, MKSCK_SHUT_WR bitfield */
  117. uint32 foundEmpty; /**< number of times a receive has blocked */
  118. uint32 foundFull; /**< number of times a send has blocked */
  119. Mutex mutex; /**< locks the ring buffer */
  120. MVA rcvCBEntryMVA; /**< monitor's receive callback entrypoint */
  121. MVA rcvCBParamMVA; /**< monitor's receive callback parameter */
  122. uint8 buff[MKSCK_BUFSIZE] /**< data going TO this socket */
  123. __attribute__((aligned(MKSCK_ALIGNMENT)));
  124. };
  125. /**
  126. * @brief the shared page of an address domain (vmId)
  127. */
  128. struct MksckPage {
  129. _Bool isGuest; /**< the page belongs to a monitor/guest */
  130. uint32 tgid; /**< thread group id if isGuest=true */
  131. /**< undefined otherwise */
  132. volatile HKVA vmHKVA; /**< host side local data structure for vm */
  133. AtmUInt32 refCount; /**< page cannot be freed unless this is zero */
  134. /**< ... increment with mksckPageListLock */
  135. /**< ... decrement at any time */
  136. /**< ... initialized to 1 for wsp->mksckPage* */
  137. /**< ... pointers */
  138. uint32 wakeHostRecv; /**< bitmask of sockets to be woken for read */
  139. /**< ... access from VCPU thread only */
  140. AtmUInt32 wakeVMMRecv; /**< likewise for monitor receive callbacks */
  141. Mutex mutex; /**< locks list of open sockets */
  142. Mksck_VmId vmId; /**< hostId or guestId these sockets are for */
  143. Mksck_Port portStore; /**< used to assign ephemeral port numbers */
  144. uint32 numAllocSocks; /**< number of elements in sockets[] array */
  145. Mksck sockets[1]; /**< array of sockets (fill MKSCKPAGE_SIZE) */
  146. };
  147. MksckPage *MksckPage_GetFromVmId(Mksck_VmId vmId);
  148. Mksck_Port MksckPage_GetFreePort(MksckPage *mksckPage, Mksck_Port port);
  149. Mksck *MksckPage_GetFromAddr(MksckPage *mksckPage, Mksck_Address addr);
  150. Mksck *MksckPage_AllocSocket(MksckPage *mksckPage, Mksck_Address addr);
  151. void MksckPage_DecRefc(MksckPage *mksckPage);
  152. void Mksck_DecRefc(Mksck *mksck);
  153. void Mksck_CloseCommon(Mksck *mksck);
  154. _Bool Mksck_IncReadIndex(Mksck *mksck, uint32 read, Mksck_Datagram *dg);
  155. uint32 Mksck_FindSendRoom(Mksck *mksck, uint32 needed);
  156. void Mksck_IncWriteIndex(Mksck *mksck, uint32 write, uint32 needed);
  157. void Mksck_DisconnectPeer(Mksck *mksck);
  158. /**
  159. * @brief determine which shared page a given socket is on
  160. * Note that this process does not rely on any directory.
  161. * @param mksck pointer to socket
  162. * @return pointer to shared page
  163. */
  164. static inline MksckPage *
  165. Mksck_ToSharedPage(Mksck *mksck)
  166. {
  167. return (MksckPage *)((char *)(mksck - mksck->index) -
  168. offsetof(MksckPage, sockets));
  169. }
  170. #endif