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 18 #ifndef __KABI_H__ 19 #define __KABI_H__ 20 21 /* Kernel macros for userspace testing. */ 22 #ifndef __aligned 23 #define __aligned(x) __attribute__((__aligned__(x))) 24 #endif 25 #ifndef __used 26 #define __used __attribute__((__used__)) 27 #endif 28 #ifndef __section 29 #define __section(section) __attribute__((__section__(section))) 30 #endif 31 #ifndef __PASTE 32 #define ___PASTE(a, b) a##b 33 #define __PASTE(a, b) ___PASTE(a, b) 34 #endif 35 #ifndef __stringify 36 #define __stringify_1(x...) #x 37 #define __stringify(x...) __stringify_1(x) 38 #endif 39 40 #define __KABI_RULE(hint, target, value) \ 41 static const char __PASTE(__gendwarfksyms_rule_, \ 42 __COUNTER__)[] __used __aligned(1) \ 43 __section(".discard.gendwarfksyms.kabi_rules") = \ 44 "1\0" #hint "\0" #target "\0" #value 45 46 #define __KABI_NORMAL_SIZE_ALIGN(_orig, _new) \ 47 union { \ 48 _Static_assert( \ 49 sizeof(struct { _new; }) <= sizeof(struct { _orig; }), \ 50 __FILE__ ":" __stringify(__LINE__) ": " __stringify( \ 51 _new) " is larger than " __stringify(_orig)); \ 52 _Static_assert( \ 53 __alignof__(struct { _new; }) <= \ 54 __alignof__(struct { _orig; }), \ 55 __FILE__ ":" __stringify(__LINE__) ": " __stringify( \ 56 _orig) " is not aligned the same as " __stringify(_new)); \ 57 } 58 59 #define __KABI_REPLACE(_orig, _new) \ 60 union { \ 61 _new; \ 62 struct { \ 63 _orig; \ 64 }; \ 65 __KABI_NORMAL_SIZE_ALIGN(_orig, _new); \ 66 } 67 68 /* 69 * KABI_DECLONLY(fqn) 70 * Treat the struct/union/enum fqn as a declaration, i.e. even if 71 * a definition is available, don't expand the contents. 72 */ 73 #define KABI_DECLONLY(fqn) __KABI_RULE(declonly, fqn, ) 74 75 /* 76 * KABI_ENUMERATOR_IGNORE(fqn, field) 77 * When expanding enum fqn, skip the provided field. This makes it 78 * possible to hide added enum fields from versioning. 79 */ 80 #define KABI_ENUMERATOR_IGNORE(fqn, field) \ 81 __KABI_RULE(enumerator_ignore, fqn field, ) 82 83 /* 84 * KABI_ENUMERATOR_VALUE(fqn, field, value) 85 * When expanding enum fqn, use the provided value for the 86 * specified field. This makes it possible to override enumerator 87 * values when calculating versions. 88 */ 89 #define KABI_ENUMERATOR_VALUE(fqn, field, value) \ 90 __KABI_RULE(enumerator_value, fqn field, value) 91 92 /* 93 * KABI_BYTE_SIZE(fqn, value) 94 * Set the byte_size attribute for the struct/union/enum fqn to 95 * value bytes. 96 */ 97 #define KABI_BYTE_SIZE(fqn, value) __KABI_RULE(byte_size, fqn, value) 98 99 /* 100 * KABI_RESERVE 101 * Reserve some "padding" in a structure for use by LTS backports. 102 * This is normally placed at the end of a structure. 103 * number: the "number" of the padding variable in the structure. Start with 104 * 1 and go up. 105 */ 106 #define KABI_RESERVE(n) unsigned long __kabi_reserved##n 107 108 /* 109 * KABI_RESERVE_ARRAY 110 * Same as _BACKPORT_RESERVE but allocates an array with the specified 111 * size in bytes. 112 */ 113 #define KABI_RESERVE_ARRAY(n, s) \ 114 unsigned char __aligned(8) __kabi_reserved##n[s] 115 116 /* 117 * KABI_IGNORE 118 * Add a new field that's ignored in versioning. 119 */ 120 #define KABI_IGNORE(n, _new) \ 121 union { \ 122 _new; \ 123 unsigned char __kabi_ignored##n; \ 124 } 125 126 /* 127 * KABI_REPLACE 128 * Replace a field with a compatible new field. 129 */ 130 #define KABI_REPLACE(_oldtype, _oldname, _new) \ 131 __KABI_REPLACE(_oldtype __kabi_renamed##_oldname, struct { _new; }) 132 133 /* 134 * KABI_USE(number, _new) 135 * Use a previous padding entry that was defined with KABI_RESERVE 136 * number: the previous "number" of the padding variable 137 * _new: the variable to use now instead of the padding variable 138 */ 139 #define KABI_USE(number, _new) __KABI_REPLACE(KABI_RESERVE(number), _new) 140 141 /* 142 * KABI_USE2(number, _new1, _new2) 143 * Use a previous padding entry that was defined with KABI_RESERVE for 144 * two new variables that fit into 64 bits. This is good for when you do not 145 * want to "burn" a 64bit padding variable for a smaller variable size if not 146 * needed. 147 */ 148 #define KABI_USE2(number, _new1, _new2) \ 149 __KABI_REPLACE( \ 150 KABI_RESERVE(number), struct { \ 151 _new1; \ 152 _new2; \ 153 }) 154 /* 155 * KABI_USE_ARRAY(number, bytes, _new) 156 * Use a previous padding entry that was defined with KABI_RESERVE_ARRAY 157 * number: the previous "number" of the padding variable 158 * bytes: the size in bytes reserved for the array 159 * _new: the variable to use now instead of the padding variable 160 */ 161 #define KABI_USE_ARRAY(number, bytes, _new) \ 162 __KABI_REPLACE(KABI_RESERVE_ARRAY(number, bytes), _new) 163 164 #endif /* __KABI_H__ */ 165