kabi.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2024 Google LLC
  4. *
  5. * Example macros for maintaining kABI stability.
  6. *
  7. * This file is based on android_kabi.h, which has the following notice:
  8. *
  9. * Heavily influenced by rh_kabi.h which came from the RHEL/CENTOS kernel
  10. * and was:
  11. * Copyright (c) 2014 Don Zickus
  12. * Copyright (c) 2015-2018 Jiri Benc
  13. * Copyright (c) 2015 Sabrina Dubroca, Hannes Frederic Sowa
  14. * Copyright (c) 2016-2018 Prarit Bhargava
  15. * Copyright (c) 2017 Paolo Abeni, Larry Woodman
  16. */
  17. #ifndef __KABI_H__
  18. #define __KABI_H__
  19. /* Kernel macros for userspace testing. */
  20. #ifndef __aligned
  21. #define __aligned(x) __attribute__((__aligned__(x)))
  22. #endif
  23. #ifndef __used
  24. #define __used __attribute__((__used__))
  25. #endif
  26. #ifndef __section
  27. #define __section(section) __attribute__((__section__(section)))
  28. #endif
  29. #ifndef __PASTE
  30. #define ___PASTE(a, b) a##b
  31. #define __PASTE(a, b) ___PASTE(a, b)
  32. #endif
  33. #ifndef __stringify
  34. #define __stringify_1(x...) #x
  35. #define __stringify(x...) __stringify_1(x)
  36. #endif
  37. #define ___KABI_RULE(hint, target, value) \
  38. static const char __PASTE(__gendwarfksyms_rule_, \
  39. __COUNTER__)[] __used __aligned(1) \
  40. __section(".discard.gendwarfksyms.kabi_rules") = \
  41. "1\0" #hint "\0" target "\0" value
  42. #define __KABI_RULE(hint, target, value) \
  43. ___KABI_RULE(hint, #target, #value)
  44. #define __KABI_NORMAL_SIZE_ALIGN(_orig, _new) \
  45. union { \
  46. _Static_assert( \
  47. sizeof(struct { _new; }) <= sizeof(struct { _orig; }), \
  48. __FILE__ ":" __stringify(__LINE__) ": " __stringify( \
  49. _new) " is larger than " __stringify(_orig)); \
  50. _Static_assert( \
  51. __alignof__(struct { _new; }) <= \
  52. __alignof__(struct { _orig; }), \
  53. __FILE__ ":" __stringify(__LINE__) ": " __stringify( \
  54. _orig) " is not aligned the same as " __stringify(_new)); \
  55. }
  56. #define __KABI_REPLACE(_orig, _new) \
  57. union { \
  58. _new; \
  59. struct { \
  60. _orig; \
  61. }; \
  62. __KABI_NORMAL_SIZE_ALIGN(_orig, _new); \
  63. }
  64. /*
  65. * KABI_DECLONLY(fqn)
  66. * Treat the struct/union/enum fqn as a declaration, i.e. even if
  67. * a definition is available, don't expand the contents.
  68. */
  69. #define KABI_DECLONLY(fqn) __KABI_RULE(declonly, fqn, )
  70. /*
  71. * KABI_ENUMERATOR_IGNORE(fqn, field)
  72. * When expanding enum fqn, skip the provided field. This makes it
  73. * possible to hide added enum fields from versioning.
  74. */
  75. #define KABI_ENUMERATOR_IGNORE(fqn, field) \
  76. __KABI_RULE(enumerator_ignore, fqn field, )
  77. /*
  78. * KABI_ENUMERATOR_VALUE(fqn, field, value)
  79. * When expanding enum fqn, use the provided value for the
  80. * specified field. This makes it possible to override enumerator
  81. * values when calculating versions.
  82. */
  83. #define KABI_ENUMERATOR_VALUE(fqn, field, value) \
  84. __KABI_RULE(enumerator_value, fqn field, value)
  85. /*
  86. * KABI_BYTE_SIZE(fqn, value)
  87. * Set the byte_size attribute for the struct/union/enum fqn to
  88. * value bytes.
  89. */
  90. #define KABI_BYTE_SIZE(fqn, value) __KABI_RULE(byte_size, fqn, value)
  91. /*
  92. * KABI_TYPE_STRING(type, str)
  93. * For the given type, override the type string used in symtypes
  94. * output and version calculation with str.
  95. */
  96. #define KABI_TYPE_STRING(type, str) ___KABI_RULE(type_string, type, str)
  97. /*
  98. * KABI_RESERVE
  99. * Reserve some "padding" in a structure for use by LTS backports.
  100. * This is normally placed at the end of a structure.
  101. * number: the "number" of the padding variable in the structure. Start with
  102. * 1 and go up.
  103. */
  104. #define KABI_RESERVE(n) unsigned long __kabi_reserved##n
  105. /*
  106. * KABI_RESERVE_ARRAY
  107. * Same as _BACKPORT_RESERVE but allocates an array with the specified
  108. * size in bytes.
  109. */
  110. #define KABI_RESERVE_ARRAY(n, s) \
  111. unsigned char __aligned(8) __kabi_reserved##n[s]
  112. /*
  113. * KABI_IGNORE
  114. * Add a new field that's ignored in versioning.
  115. */
  116. #define KABI_IGNORE(n, _new) \
  117. union { \
  118. _new; \
  119. unsigned char __kabi_ignored##n; \
  120. }
  121. /*
  122. * KABI_REPLACE
  123. * Replace a field with a compatible new field.
  124. */
  125. #define KABI_REPLACE(_oldtype, _oldname, _new) \
  126. __KABI_REPLACE(_oldtype __kabi_renamed##_oldname, struct { _new; })
  127. /*
  128. * KABI_USE(number, _new)
  129. * Use a previous padding entry that was defined with KABI_RESERVE
  130. * number: the previous "number" of the padding variable
  131. * _new: the variable to use now instead of the padding variable
  132. */
  133. #define KABI_USE(number, _new) __KABI_REPLACE(KABI_RESERVE(number), _new)
  134. /*
  135. * KABI_USE2(number, _new1, _new2)
  136. * Use a previous padding entry that was defined with KABI_RESERVE for
  137. * two new variables that fit into 64 bits. This is good for when you do not
  138. * want to "burn" a 64bit padding variable for a smaller variable size if not
  139. * needed.
  140. */
  141. #define KABI_USE2(number, _new1, _new2) \
  142. __KABI_REPLACE( \
  143. KABI_RESERVE(number), struct { \
  144. _new1; \
  145. _new2; \
  146. })
  147. /*
  148. * KABI_USE_ARRAY(number, bytes, _new)
  149. * Use a previous padding entry that was defined with KABI_RESERVE_ARRAY
  150. * number: the previous "number" of the padding variable
  151. * bytes: the size in bytes reserved for the array
  152. * _new: the variable to use now instead of the padding variable
  153. */
  154. #define KABI_USE_ARRAY(number, bytes, _new) \
  155. __KABI_REPLACE(KABI_RESERVE_ARRAY(number, bytes), _new)
  156. #endif /* __KABI_H__ */