linux/include/linux/indirect_call_wrapper.h

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_INDIRECT_CALL_WRAPPER_H
#define _LINUX_INDIRECT_CALL_WRAPPER_H

#ifdef CONFIG_MITIGATION_RETPOLINE

/*
 * INDIRECT_CALL_$NR - wrapper for indirect calls with $NR known builtin
 *  @f: function pointer
 *  @f$NR: builtin functions names, up to $NR of them
 *  @__VA_ARGS__: arguments for @f
 *
 * Avoid retpoline overhead for known builtin, checking @f vs each of them and
 * eventually invoking directly the builtin function. The functions are checked
 * in the given order. Fallback to the indirect call.
 */
#define INDIRECT_CALL_1(f, f1, ...)
#define INDIRECT_CALL_2(f, f2, f1, ...)
#define INDIRECT_CALL_3(f, f3, f2, f1, ...)
#define INDIRECT_CALL_4(f, f4, f3, f2, f1, ...)

#define INDIRECT_CALLABLE_DECLARE(f)
#define INDIRECT_CALLABLE_SCOPE
#define EXPORT_INDIRECT_CALLABLE(f)

#else
#define INDIRECT_CALL_1
#define INDIRECT_CALL_2
#define INDIRECT_CALL_3
#define INDIRECT_CALL_4
#define INDIRECT_CALLABLE_DECLARE
#define INDIRECT_CALLABLE_SCOPE
#define EXPORT_INDIRECT_CALLABLE
#endif

/*
 * We can use INDIRECT_CALL_$NR for ipv6 related functions only if ipv6 is
 * builtin, this macro simplify dealing with indirect calls with only ipv4/ipv6
 * alternatives
 */
#if IS_BUILTIN(CONFIG_IPV6)
#define INDIRECT_CALL_INET(f, f2, f1, ...)
#elif IS_ENABLED(CONFIG_INET)
#define INDIRECT_CALL_INET
#else
#define INDIRECT_CALL_INET
#endif

#if IS_ENABLED(CONFIG_INET)
#define INDIRECT_CALL_INET_1(f, f1, ...)
#else
#define INDIRECT_CALL_INET_1
#endif

#endif