17c2aad20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
27c2aad20Sopenharmony_ci/* Copyright (c) 2018 Facebook */
37c2aad20Sopenharmony_ci#ifndef _UAPI__LINUX_BTF_H__
47c2aad20Sopenharmony_ci#define _UAPI__LINUX_BTF_H__
57c2aad20Sopenharmony_ci
67c2aad20Sopenharmony_ci#include <linux/types.h>
77c2aad20Sopenharmony_ci
87c2aad20Sopenharmony_ci#define BTF_MAGIC	0xeB9F
97c2aad20Sopenharmony_ci#define BTF_VERSION	1
107c2aad20Sopenharmony_ci
117c2aad20Sopenharmony_cistruct btf_header {
127c2aad20Sopenharmony_ci	__u16	magic;
137c2aad20Sopenharmony_ci	__u8	version;
147c2aad20Sopenharmony_ci	__u8	flags;
157c2aad20Sopenharmony_ci	__u32	hdr_len;
167c2aad20Sopenharmony_ci
177c2aad20Sopenharmony_ci	/* All offsets are in bytes relative to the end of this header */
187c2aad20Sopenharmony_ci	__u32	type_off;	/* offset of type section	*/
197c2aad20Sopenharmony_ci	__u32	type_len;	/* length of type section	*/
207c2aad20Sopenharmony_ci	__u32	str_off;	/* offset of string section	*/
217c2aad20Sopenharmony_ci	__u32	str_len;	/* length of string section	*/
227c2aad20Sopenharmony_ci};
237c2aad20Sopenharmony_ci
247c2aad20Sopenharmony_ci/* Max # of type identifier */
257c2aad20Sopenharmony_ci#define BTF_MAX_TYPE	0x000fffff
267c2aad20Sopenharmony_ci/* Max offset into the string section */
277c2aad20Sopenharmony_ci#define BTF_MAX_NAME_OFFSET	0x00ffffff
287c2aad20Sopenharmony_ci/* Max # of struct/union/enum members or func args */
297c2aad20Sopenharmony_ci#define BTF_MAX_VLEN	0xffff
307c2aad20Sopenharmony_ci
317c2aad20Sopenharmony_cistruct btf_type {
327c2aad20Sopenharmony_ci	__u32 name_off;
337c2aad20Sopenharmony_ci	/* "info" bits arrangement
347c2aad20Sopenharmony_ci	 * bits  0-15: vlen (e.g. # of struct's members)
357c2aad20Sopenharmony_ci	 * bits 16-23: unused
367c2aad20Sopenharmony_ci	 * bits 24-28: kind (e.g. int, ptr, array...etc)
377c2aad20Sopenharmony_ci	 * bits 29-30: unused
387c2aad20Sopenharmony_ci	 * bit     31: kind_flag, currently used by
397c2aad20Sopenharmony_ci	 *             struct, union, enum, fwd and enum64
407c2aad20Sopenharmony_ci	 */
417c2aad20Sopenharmony_ci	__u32 info;
427c2aad20Sopenharmony_ci	/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64.
437c2aad20Sopenharmony_ci	 * "size" tells the size of the type it is describing.
447c2aad20Sopenharmony_ci	 *
457c2aad20Sopenharmony_ci	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
467c2aad20Sopenharmony_ci	 * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
477c2aad20Sopenharmony_ci	 * "type" is a type_id referring to another type.
487c2aad20Sopenharmony_ci	 */
497c2aad20Sopenharmony_ci	union {
507c2aad20Sopenharmony_ci		__u32 size;
517c2aad20Sopenharmony_ci		__u32 type;
527c2aad20Sopenharmony_ci	};
537c2aad20Sopenharmony_ci};
547c2aad20Sopenharmony_ci
557c2aad20Sopenharmony_ci#define BTF_INFO_KIND(info)	(((info) >> 24) & 0x1f)
567c2aad20Sopenharmony_ci#define BTF_INFO_VLEN(info)	((info) & 0xffff)
577c2aad20Sopenharmony_ci#define BTF_INFO_KFLAG(info)	((info) >> 31)
587c2aad20Sopenharmony_ci
597c2aad20Sopenharmony_cienum {
607c2aad20Sopenharmony_ci	BTF_KIND_UNKN		= 0,	/* Unknown	*/
617c2aad20Sopenharmony_ci	BTF_KIND_INT		= 1,	/* Integer	*/
627c2aad20Sopenharmony_ci	BTF_KIND_PTR		= 2,	/* Pointer	*/
637c2aad20Sopenharmony_ci	BTF_KIND_ARRAY		= 3,	/* Array	*/
647c2aad20Sopenharmony_ci	BTF_KIND_STRUCT		= 4,	/* Struct	*/
657c2aad20Sopenharmony_ci	BTF_KIND_UNION		= 5,	/* Union	*/
667c2aad20Sopenharmony_ci	BTF_KIND_ENUM		= 6,	/* Enumeration up to 32-bit values */
677c2aad20Sopenharmony_ci	BTF_KIND_FWD		= 7,	/* Forward	*/
687c2aad20Sopenharmony_ci	BTF_KIND_TYPEDEF	= 8,	/* Typedef	*/
697c2aad20Sopenharmony_ci	BTF_KIND_VOLATILE	= 9,	/* Volatile	*/
707c2aad20Sopenharmony_ci	BTF_KIND_CONST		= 10,	/* Const	*/
717c2aad20Sopenharmony_ci	BTF_KIND_RESTRICT	= 11,	/* Restrict	*/
727c2aad20Sopenharmony_ci	BTF_KIND_FUNC		= 12,	/* Function	*/
737c2aad20Sopenharmony_ci	BTF_KIND_FUNC_PROTO	= 13,	/* Function Proto	*/
747c2aad20Sopenharmony_ci	BTF_KIND_VAR		= 14,	/* Variable	*/
757c2aad20Sopenharmony_ci	BTF_KIND_DATASEC	= 15,	/* Section	*/
767c2aad20Sopenharmony_ci	BTF_KIND_FLOAT		= 16,	/* Floating point	*/
777c2aad20Sopenharmony_ci	BTF_KIND_DECL_TAG	= 17,	/* Decl Tag */
787c2aad20Sopenharmony_ci	BTF_KIND_TYPE_TAG	= 18,	/* Type Tag */
797c2aad20Sopenharmony_ci	BTF_KIND_ENUM64		= 19,	/* Enumeration up to 64-bit values */
807c2aad20Sopenharmony_ci
817c2aad20Sopenharmony_ci	NR_BTF_KINDS,
827c2aad20Sopenharmony_ci	BTF_KIND_MAX		= NR_BTF_KINDS - 1,
837c2aad20Sopenharmony_ci};
847c2aad20Sopenharmony_ci
857c2aad20Sopenharmony_ci/* For some specific BTF_KIND, "struct btf_type" is immediately
867c2aad20Sopenharmony_ci * followed by extra data.
877c2aad20Sopenharmony_ci */
887c2aad20Sopenharmony_ci
897c2aad20Sopenharmony_ci/* BTF_KIND_INT is followed by a u32 and the following
907c2aad20Sopenharmony_ci * is the 32 bits arrangement:
917c2aad20Sopenharmony_ci */
927c2aad20Sopenharmony_ci#define BTF_INT_ENCODING(VAL)	(((VAL) & 0x0f000000) >> 24)
937c2aad20Sopenharmony_ci#define BTF_INT_OFFSET(VAL)	(((VAL) & 0x00ff0000) >> 16)
947c2aad20Sopenharmony_ci#define BTF_INT_BITS(VAL)	((VAL)  & 0x000000ff)
957c2aad20Sopenharmony_ci
967c2aad20Sopenharmony_ci/* Attributes stored in the BTF_INT_ENCODING */
977c2aad20Sopenharmony_ci#define BTF_INT_SIGNED	(1 << 0)
987c2aad20Sopenharmony_ci#define BTF_INT_CHAR	(1 << 1)
997c2aad20Sopenharmony_ci#define BTF_INT_BOOL	(1 << 2)
1007c2aad20Sopenharmony_ci
1017c2aad20Sopenharmony_ci/* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
1027c2aad20Sopenharmony_ci * The exact number of btf_enum is stored in the vlen (of the
1037c2aad20Sopenharmony_ci * info in "struct btf_type").
1047c2aad20Sopenharmony_ci */
1057c2aad20Sopenharmony_cistruct btf_enum {
1067c2aad20Sopenharmony_ci	__u32	name_off;
1077c2aad20Sopenharmony_ci	__s32	val;
1087c2aad20Sopenharmony_ci};
1097c2aad20Sopenharmony_ci
1107c2aad20Sopenharmony_ci/* BTF_KIND_ARRAY is followed by one "struct btf_array" */
1117c2aad20Sopenharmony_cistruct btf_array {
1127c2aad20Sopenharmony_ci	__u32	type;
1137c2aad20Sopenharmony_ci	__u32	index_type;
1147c2aad20Sopenharmony_ci	__u32	nelems;
1157c2aad20Sopenharmony_ci};
1167c2aad20Sopenharmony_ci
1177c2aad20Sopenharmony_ci/* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
1187c2aad20Sopenharmony_ci * by multiple "struct btf_member".  The exact number
1197c2aad20Sopenharmony_ci * of btf_member is stored in the vlen (of the info in
1207c2aad20Sopenharmony_ci * "struct btf_type").
1217c2aad20Sopenharmony_ci */
1227c2aad20Sopenharmony_cistruct btf_member {
1237c2aad20Sopenharmony_ci	__u32	name_off;
1247c2aad20Sopenharmony_ci	__u32	type;
1257c2aad20Sopenharmony_ci	/* If the type info kind_flag is set, the btf_member offset
1267c2aad20Sopenharmony_ci	 * contains both member bitfield size and bit offset. The
1277c2aad20Sopenharmony_ci	 * bitfield size is set for bitfield members. If the type
1287c2aad20Sopenharmony_ci	 * info kind_flag is not set, the offset contains only bit
1297c2aad20Sopenharmony_ci	 * offset.
1307c2aad20Sopenharmony_ci	 */
1317c2aad20Sopenharmony_ci	__u32	offset;
1327c2aad20Sopenharmony_ci};
1337c2aad20Sopenharmony_ci
1347c2aad20Sopenharmony_ci/* If the struct/union type info kind_flag is set, the
1357c2aad20Sopenharmony_ci * following two macros are used to access bitfield_size
1367c2aad20Sopenharmony_ci * and bit_offset from btf_member.offset.
1377c2aad20Sopenharmony_ci */
1387c2aad20Sopenharmony_ci#define BTF_MEMBER_BITFIELD_SIZE(val)	((val) >> 24)
1397c2aad20Sopenharmony_ci#define BTF_MEMBER_BIT_OFFSET(val)	((val) & 0xffffff)
1407c2aad20Sopenharmony_ci
1417c2aad20Sopenharmony_ci/* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param".
1427c2aad20Sopenharmony_ci * The exact number of btf_param is stored in the vlen (of the
1437c2aad20Sopenharmony_ci * info in "struct btf_type").
1447c2aad20Sopenharmony_ci */
1457c2aad20Sopenharmony_cistruct btf_param {
1467c2aad20Sopenharmony_ci	__u32	name_off;
1477c2aad20Sopenharmony_ci	__u32	type;
1487c2aad20Sopenharmony_ci};
1497c2aad20Sopenharmony_ci
1507c2aad20Sopenharmony_cienum {
1517c2aad20Sopenharmony_ci	BTF_VAR_STATIC = 0,
1527c2aad20Sopenharmony_ci	BTF_VAR_GLOBAL_ALLOCATED = 1,
1537c2aad20Sopenharmony_ci	BTF_VAR_GLOBAL_EXTERN = 2,
1547c2aad20Sopenharmony_ci};
1557c2aad20Sopenharmony_ci
1567c2aad20Sopenharmony_cienum btf_func_linkage {
1577c2aad20Sopenharmony_ci	BTF_FUNC_STATIC = 0,
1587c2aad20Sopenharmony_ci	BTF_FUNC_GLOBAL = 1,
1597c2aad20Sopenharmony_ci	BTF_FUNC_EXTERN = 2,
1607c2aad20Sopenharmony_ci};
1617c2aad20Sopenharmony_ci
1627c2aad20Sopenharmony_ci/* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
1637c2aad20Sopenharmony_ci * additional information related to the variable such as its linkage.
1647c2aad20Sopenharmony_ci */
1657c2aad20Sopenharmony_cistruct btf_var {
1667c2aad20Sopenharmony_ci	__u32	linkage;
1677c2aad20Sopenharmony_ci};
1687c2aad20Sopenharmony_ci
1697c2aad20Sopenharmony_ci/* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo"
1707c2aad20Sopenharmony_ci * to describe all BTF_KIND_VAR types it contains along with it's
1717c2aad20Sopenharmony_ci * in-section offset as well as size.
1727c2aad20Sopenharmony_ci */
1737c2aad20Sopenharmony_cistruct btf_var_secinfo {
1747c2aad20Sopenharmony_ci	__u32	type;
1757c2aad20Sopenharmony_ci	__u32	offset;
1767c2aad20Sopenharmony_ci	__u32	size;
1777c2aad20Sopenharmony_ci};
1787c2aad20Sopenharmony_ci
1797c2aad20Sopenharmony_ci/* BTF_KIND_DECL_TAG is followed by a single "struct btf_decl_tag" to describe
1807c2aad20Sopenharmony_ci * additional information related to the tag applied location.
1817c2aad20Sopenharmony_ci * If component_idx == -1, the tag is applied to a struct, union,
1827c2aad20Sopenharmony_ci * variable or function. Otherwise, it is applied to a struct/union
1837c2aad20Sopenharmony_ci * member or a func argument, and component_idx indicates which member
1847c2aad20Sopenharmony_ci * or argument (0 ... vlen-1).
1857c2aad20Sopenharmony_ci */
1867c2aad20Sopenharmony_cistruct btf_decl_tag {
1877c2aad20Sopenharmony_ci       __s32   component_idx;
1887c2aad20Sopenharmony_ci};
1897c2aad20Sopenharmony_ci
1907c2aad20Sopenharmony_ci/* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64".
1917c2aad20Sopenharmony_ci * The exact number of btf_enum64 is stored in the vlen (of the
1927c2aad20Sopenharmony_ci * info in "struct btf_type").
1937c2aad20Sopenharmony_ci */
1947c2aad20Sopenharmony_cistruct btf_enum64 {
1957c2aad20Sopenharmony_ci	__u32	name_off;
1967c2aad20Sopenharmony_ci	__u32	val_lo32;
1977c2aad20Sopenharmony_ci	__u32	val_hi32;
1987c2aad20Sopenharmony_ci};
1997c2aad20Sopenharmony_ci
2007c2aad20Sopenharmony_ci#endif /* _UAPI__LINUX_BTF_H__ */
201