utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 General architecture-independent definitions, typedefs, and macros.
  24. */
  25. #ifndef _UTILS_H
  26. #define _UTILS_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_PV
  32. #define INCLUDE_ALLOW_HOSTUSER
  33. #define INCLUDE_ALLOW_GUESTUSER
  34. #define INCLUDE_ALLOW_WORKSTATION
  35. #define INCLUDE_ALLOW_GPL
  36. #include "include_check.h"
  37. #define MAX_FILENAME 128
  38. /* Round address up to given size boundary */
  39. /* Note: ALIGN() conflicts with Linux */
  40. #define MVP_ALIGN(_v, _n) (((_v) + (_n) - 1) & -(_n))
  41. #define ALIGNVA(_addr, _size) MVP_ALIGN(_addr, _size)
  42. #define alignof(t) offsetof(struct { char c; typeof(t) x; }, x)
  43. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  44. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  45. #ifndef NULL
  46. #define NULL ((void *)0)
  47. #endif
  48. #define KB(_X_) ((_X_)*1024U)
  49. #define MB(_X_) (KB(_X_)*1024)
  50. #define GB(_X_) (MB(_X_)*1024)
  51. #define NELEM(x) (sizeof(x)/sizeof((x)[0]))
  52. /*
  53. * x in [low, high)
  54. * args evaluated once
  55. */
  56. #define RANGE(x, low, high) \
  57. ({ \
  58. typeof(x) _x = (x); \
  59. typeof(x) _low = (typeof(x))(low); \
  60. typeof(x) _high = (typeof(x))(high); \
  61. (_Bool)((_low <= _x) && (_x < _high)); \
  62. })
  63. #define OBJECTS_PER_PAGE(_type) (PAGE_SIZE / sizeof(_type))
  64. #define MA_2_MPN(_ma) ((MPN)((_ma) / PAGE_SIZE))
  65. #define MPN_2_MA(_mpn) ((MA)((_mpn) * PAGE_SIZE))
  66. #define VA_2_VPN(_va) ((_va) / PAGE_SIZE)
  67. #define VPN_2_vA(_vpn) ((_vpn) * PAGE_SIZE)
  68. /*
  69. * The following convenience macro can be used in a following situation
  70. *
  71. * send(..., &foo, sizeof(foo)) --> send(..., PTR_N_SIZE(foo))
  72. */
  73. #define PTR_N_SIZE(_var) &(_var), sizeof(_var)
  74. /*
  75. *
  76. * BIT-PULLING macros
  77. *
  78. */
  79. #define MVP_BIT(val, n) (((val)>>(n))&1)
  80. #define MVP_BITS(val, m, n) (((val)<<(31-(n))) >> ((31-(n)) + (m)))
  81. #define MVP_EXTRACT_FIELD(w, m, n) MVP_BITS((w), (m), ((m) + (n) - 1))
  82. #define MVP_MASK(m, n) (MVP_EXTRACT_FIELD(~(uint32)0U, (m), (n)) << (m))
  83. #define MVP_UPDATE_FIELD(old_val, field_val, m, n) \
  84. (((old_val) & ~MVP_MASK((m), (n))) | \
  85. (MVP_EXTRACT_FIELD((field_val), 0, (n)) << (m)))
  86. /*
  87. *
  88. * 64BIT-PULLING macros
  89. *
  90. */
  91. #define MVP_BITS64(val, m, n) (((val)<<(63-(n))) >> ((63-(n)) + (m)))
  92. #define MVP_EXTRACT_FIELD64(w, m, n) MVP_BITS64((w), (m), ((m) + (n) - 1))
  93. #define MVP_MASK64(m, n) (MVP_EXTRACT_FIELD64(~(uint64)0ULL, (m), (n)) << (m))
  94. #define MVP_UPDATE_FIELD64(old_val, field_val, m, n) \
  95. (((old_val) & ~MVP_MASK64((m), (n))) | \
  96. (MVP_EXTRACT_FIELD64(((uint64)(field_val)), 0ULL, (n)) << (m)))
  97. /*
  98. *
  99. * BIT-CHANGING macros
  100. *
  101. */
  102. #define MVP_SETBIT(val, n) ((val) |= (1<<(n)))
  103. #define MVP_CLRBIT(val, n) ((val) &= (~(1<<(n))))
  104. /*
  105. * Fixed bit-width sign extension.
  106. */
  107. #define MVP_SIGN_EXTEND(val, width) \
  108. (((val) ^ (1 << ((width) - 1))) - (1 << ((width) - 1)))
  109. /*
  110. * Assembler helpers.
  111. */
  112. #define _MVP_HASH #
  113. #define MVP_HASH() _MVP_HASH
  114. #define _MVP_STRINGIFY(...) #__VA_ARGS__
  115. #define MVP_STRINGIFY(...) _MVP_STRINGIFY(__VA_ARGS__)
  116. #ifndef __ASSEMBLER__
  117. #include <stddef.h>
  118. #include <stdbool.h>
  119. /*
  120. * Constant equivalents of build-flags.
  121. *
  122. * Test these when possible instead of using #ifdef so that your code
  123. * gets parsed.
  124. */
  125. #ifdef MVP_DEBUG
  126. static const _Bool mvpDebug = true;
  127. #else
  128. static const _Bool mvpDebug = false;
  129. #endif
  130. #ifdef MVP_STATS
  131. static const _Bool mvpStats = true;
  132. #else
  133. static const _Bool mvpStats = false;
  134. #endif
  135. #ifdef MVP_DEVEL
  136. static const _Bool mvpDevel = true;
  137. #else
  138. static const _Bool mvpDevel = false;
  139. #endif
  140. #endif
  141. #endif