Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * code tagging framework
4 */
5#ifndef _LINUX_CODETAG_H
6#define _LINUX_CODETAG_H
7
8#include <linux/types.h>
9
10struct codetag_iterator;
11struct codetag_type;
12struct codetag_module;
13struct seq_buf;
14struct module;
15
16#define CODETAG_SECTION_START_PREFIX "__start_"
17#define CODETAG_SECTION_STOP_PREFIX "__stop_"
18
19/*
20 * An instance of this structure is created in a special ELF section at every
21 * code location being tagged. At runtime, the special section is treated as
22 * an array of these.
23 */
24struct codetag {
25 unsigned int flags; /* used in later patches */
26 unsigned int lineno;
27 const char *modname;
28 const char *function;
29 const char *filename;
30} __aligned(8);
31
32union codetag_ref {
33 struct codetag *ct;
34};
35
36struct codetag_type_desc {
37 const char *section;
38 size_t tag_size;
39 int (*module_load)(struct module *mod,
40 struct codetag *start, struct codetag *end);
41 void (*module_unload)(struct module *mod,
42 struct codetag *start, struct codetag *end);
43#ifdef CONFIG_MODULES
44 void (*module_replaced)(struct module *mod, struct module *new_mod);
45 bool (*needs_section_mem)(struct module *mod, unsigned long size);
46 void *(*alloc_section_mem)(struct module *mod, unsigned long size,
47 unsigned int prepend, unsigned long align);
48 void (*free_section_mem)(struct module *mod, bool used);
49#endif
50};
51
52struct codetag_iterator {
53 struct codetag_type *cttype;
54 struct codetag_module *cmod;
55 unsigned long mod_id;
56 struct codetag *ct;
57 unsigned long mod_seq;
58};
59
60#ifdef MODULE
61#define CT_MODULE_NAME KBUILD_MODNAME
62#else
63#define CT_MODULE_NAME NULL
64#endif
65
66#define CODE_TAG_INIT { \
67 .modname = CT_MODULE_NAME, \
68 .function = __func__, \
69 .filename = __FILE__, \
70 .lineno = __LINE__, \
71 .flags = 0, \
72}
73
74void codetag_lock_module_list(struct codetag_type *cttype, bool lock);
75bool codetag_trylock_module_list(struct codetag_type *cttype);
76struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype);
77struct codetag *codetag_next_ct(struct codetag_iterator *iter);
78
79void codetag_to_text(struct seq_buf *out, struct codetag *ct);
80
81struct codetag_type *
82codetag_register_type(const struct codetag_type_desc *desc);
83
84#if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES)
85
86bool codetag_needs_module_section(struct module *mod, const char *name,
87 unsigned long size);
88void *codetag_alloc_module_section(struct module *mod, const char *name,
89 unsigned long size, unsigned int prepend,
90 unsigned long align);
91void codetag_free_module_sections(struct module *mod);
92void codetag_module_replaced(struct module *mod, struct module *new_mod);
93int codetag_load_module(struct module *mod);
94void codetag_unload_module(struct module *mod);
95
96#else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
97
98static inline bool
99codetag_needs_module_section(struct module *mod, const char *name,
100 unsigned long size) { return false; }
101static inline void *
102codetag_alloc_module_section(struct module *mod, const char *name,
103 unsigned long size, unsigned int prepend,
104 unsigned long align) { return NULL; }
105static inline void codetag_free_module_sections(struct module *mod) {}
106static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {}
107static inline int codetag_load_module(struct module *mod) { return 0; }
108static inline void codetag_unload_module(struct module *mod) {}
109
110#endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
111
112#endif /* _LINUX_CODETAG_H */