13d0407baSopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
23d0407baSopenharmony_ci/*
33d0407baSopenharmony_ci * android_kabi.h - Android kernel abi abstraction header
43d0407baSopenharmony_ci *
53d0407baSopenharmony_ci * Copyright (C) 2020 Google, Inc.
63d0407baSopenharmony_ci *
73d0407baSopenharmony_ci * Heavily influenced by rh_kabi.h which came from the RHEL/CENTOS kernel and
83d0407baSopenharmony_ci * was:
93d0407baSopenharmony_ci *    Copyright (c) 2014 Don Zickus
103d0407baSopenharmony_ci *    Copyright (c) 2015-2018 Jiri Benc
113d0407baSopenharmony_ci *    Copyright (c) 2015 Sabrina Dubroca, Hannes Frederic Sowa
123d0407baSopenharmony_ci *    Copyright (c) 2016-2018 Prarit Bhargava
133d0407baSopenharmony_ci *    Copyright (c) 2017 Paolo Abeni, Larry Woodman
143d0407baSopenharmony_ci *
153d0407baSopenharmony_ci * These macros are to be used to try to help alleviate future kernel abi
163d0407baSopenharmony_ci * changes that will occur as LTS and other kernel patches are merged into the
173d0407baSopenharmony_ci * tree during a period in which the kernel abi is wishing to not be disturbed.
183d0407baSopenharmony_ci *
193d0407baSopenharmony_ci * There are two times these macros should be used:
203d0407baSopenharmony_ci *  - Before the kernel abi is "frozen"
213d0407baSopenharmony_ci *    Padding can be added to various kernel structures that have in the past
223d0407baSopenharmony_ci *    been known to change over time.  That will give "room" in the structure
233d0407baSopenharmony_ci *    that can then be used when fields are added so that the structure size
243d0407baSopenharmony_ci *    will not change.
253d0407baSopenharmony_ci *
263d0407baSopenharmony_ci *  - After the kernel abi is "frozen"
273d0407baSopenharmony_ci *    If a structure's field is changed to a type that is identical in size to
283d0407baSopenharmony_ci *    the previous type, it can be changed with a union macro
293d0407baSopenharmony_ci *    If a field is added to a structure, the padding fields can be used to add
303d0407baSopenharmony_ci *    the new field in a "safe" way.
313d0407baSopenharmony_ci */
323d0407baSopenharmony_ci#ifndef _ANDROID_KABI_H
333d0407baSopenharmony_ci#define _ANDROID_KABI_H
343d0407baSopenharmony_ci
353d0407baSopenharmony_ci#include <linux/compiler.h>
363d0407baSopenharmony_ci
373d0407baSopenharmony_ci/*
383d0407baSopenharmony_ci * Worker macros, don't use these, use the ones without a leading '_'
393d0407baSopenharmony_ci */
403d0407baSopenharmony_ci
413d0407baSopenharmony_ci#define __ANDROID_KABI_CHECK_SIZE_ALIGN(_orig, _new)                                                                   \
423d0407baSopenharmony_ci    union {                                                                                                            \
433d0407baSopenharmony_ci        _Static_assert(sizeof(struct { _new; }) <= sizeof(struct { _orig; }), __FILE__                                 \
443d0407baSopenharmony_ci                       ":" __stringify(__LINE__) ": " __stringify(_new) " is larger than " __stringify(_orig));        \
453d0407baSopenharmony_ci        _Static_assert(                                                                                                \
463d0407baSopenharmony_ci            __alignof__(struct { _new; }) <= __alignof__(struct { _orig; }), __FILE__                                  \
473d0407baSopenharmony_ci            ":" __stringify(__LINE__) ": " __stringify(_orig) " is not aligned the same as " __stringify(_new));       \
483d0407baSopenharmony_ci    }
493d0407baSopenharmony_ci
503d0407baSopenharmony_ci#ifdef __GENKSYMS__
513d0407baSopenharmony_ci
523d0407baSopenharmony_ci#define _ANDROID_KABI_REPLACE(_orig, _new) _orig
533d0407baSopenharmony_ci
543d0407baSopenharmony_ci#else
553d0407baSopenharmony_ci
563d0407baSopenharmony_ci#define _ANDROID_KABI_REPLACE(_orig, _new)                                                                             \
573d0407baSopenharmony_ci    union {                                                                                                            \
583d0407baSopenharmony_ci        _new;                                                                                                          \
593d0407baSopenharmony_ci        struct {                                                                                                       \
603d0407baSopenharmony_ci            _orig;                                                                                                     \
613d0407baSopenharmony_ci        } __UNIQUE_ID(android_kabi_hide);                                                                              \
623d0407baSopenharmony_ci        __ANDROID_KABI_CHECK_SIZE_ALIGN(_orig, _new);                                                                  \
633d0407baSopenharmony_ci    }
643d0407baSopenharmony_ci
653d0407baSopenharmony_ci#endif /* __GENKSYMS__ */
663d0407baSopenharmony_ci
673d0407baSopenharmony_ci#define _ANDROID_KABI_RESERVE(n) u64 android_kabi_reserved##n
683d0407baSopenharmony_ci
693d0407baSopenharmony_ci/*
703d0407baSopenharmony_ci * Macros to use _before_ the ABI is frozen
713d0407baSopenharmony_ci */
723d0407baSopenharmony_ci
733d0407baSopenharmony_ci/*
743d0407baSopenharmony_ci * ANDROID_KABI_RESERVE
753d0407baSopenharmony_ci *   Reserve some "padding" in a structure for potential future use.
763d0407baSopenharmony_ci *   This normally placed at the end of a structure.
773d0407baSopenharmony_ci *   number: the "number" of the padding variable in the structure.  Start with
783d0407baSopenharmony_ci *   1 and go up.
793d0407baSopenharmony_ci */
803d0407baSopenharmony_ci#define ANDROID_KABI_RESERVE(number) _ANDROID_KABI_RESERVE(number)
813d0407baSopenharmony_ci
823d0407baSopenharmony_ci/*
833d0407baSopenharmony_ci * Macros to use _after_ the ABI is frozen
843d0407baSopenharmony_ci */
853d0407baSopenharmony_ci
863d0407baSopenharmony_ci/*
873d0407baSopenharmony_ci * ANDROID_KABI_USE(number, _new)
883d0407baSopenharmony_ci *   Use a previous padding entry that was defined with ANDROID_KABI_RESERVE
893d0407baSopenharmony_ci *   number: the previous "number" of the padding variable
903d0407baSopenharmony_ci *   _new: the variable to use now instead of the padding variable
913d0407baSopenharmony_ci */
923d0407baSopenharmony_ci#define ANDROID_KABI_USE(number, _new) _ANDROID_KABI_REPLACE(_ANDROID_KABI_RESERVE(number), _new)
933d0407baSopenharmony_ci
943d0407baSopenharmony_ci/*
953d0407baSopenharmony_ci * ANDROID_KABI_USE2(number, _new1, _new2)
963d0407baSopenharmony_ci *   Use a previous padding entry that was defined with ANDROID_KABI_RESERVE for
973d0407baSopenharmony_ci *   two new variables that fit into 64 bits.  This is good for when you do not
983d0407baSopenharmony_ci *   want to "burn" a 64bit padding variable for a smaller variable size if not
993d0407baSopenharmony_ci *   needed.
1003d0407baSopenharmony_ci */
1013d0407baSopenharmony_ci#define ANDROID_KABI_USE2(number, _new1, _new2)                                                                        \
1023d0407baSopenharmony_ci    _ANDROID_KABI_REPLACE(                                                                                             \
1033d0407baSopenharmony_ci        _ANDROID_KABI_RESERVE(number), struct {                                                                        \
1043d0407baSopenharmony_ci            _new1;                                                                                                     \
1053d0407baSopenharmony_ci            _new2;                                                                                                     \
1063d0407baSopenharmony_ci        })
1073d0407baSopenharmony_ci
1083d0407baSopenharmony_ci#endif /* _ANDROID_KABI_H */
109