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_RESERVE 94 * Reserve some "padding" in a structure for use by LTS backports. 95 * This is normally placed at the end of a structure. 96 * number: the "number" of the padding variable in the structure. Start with 97 * 1 and go up. 98 */ 99 #define KABI_RESERVE(n) unsigned long __kabi_reserved##n 100 101 /* 102 * KABI_RESERVE_ARRAY 103 * Same as _BACKPORT_RESERVE but allocates an array with the specified 104 * size in bytes. 105 */ 106 #define KABI_RESERVE_ARRAY(n, s) \ 107 unsigned char __aligned(8) __kabi_reserved##n[s] 108 109 /* 110 * KABI_IGNORE 111 * Add a new field that's ignored in versioning. 112 */ 113 #define KABI_IGNORE(n, _new) \ 114 union { \ 115 _new; \ 116 unsigned char __kabi_ignored##n; \ 117 } 118 119 /* 120 * KABI_REPLACE 121 * Replace a field with a compatible new field. 122 */ 123 #define KABI_REPLACE(_oldtype, _oldname, _new) \ 124 __KABI_REPLACE(_oldtype __kabi_renamed##_oldname, struct { _new; }) 125 126 /* 127 * KABI_USE(number, _new) 128 * Use a previous padding entry that was defined with KABI_RESERVE 129 * number: the previous "number" of the padding variable 130 * _new: the variable to use now instead of the padding variable 131 */ 132 #define KABI_USE(number, _new) __KABI_REPLACE(KABI_RESERVE(number), _new) 133 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 157 #endif /* __KABI_H__ */ 158