virtio_ring: introduce virtqueue_dma_dev()
[linux-2.6-microblaze.git] / include / linux / rethook.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Return hooking with list-based shadow stack.
4  */
5 #ifndef _LINUX_RETHOOK_H
6 #define _LINUX_RETHOOK_H
7
8 #include <linux/compiler.h>
9 #include <linux/freelist.h>
10 #include <linux/kallsyms.h>
11 #include <linux/llist.h>
12 #include <linux/rcupdate.h>
13 #include <linux/refcount.h>
14
15 struct rethook_node;
16
17 typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
18
19 /**
20  * struct rethook - The rethook management data structure.
21  * @data: The user-defined data storage.
22  * @handler: The user-defined return hook handler.
23  * @pool: The pool of struct rethook_node.
24  * @ref: The reference counter.
25  * @rcu: The rcu_head for deferred freeing.
26  *
27  * Don't embed to another data structure, because this is a self-destructive
28  * data structure when all rethook_node are freed.
29  */
30 struct rethook {
31         void                    *data;
32         rethook_handler_t       handler;
33         struct freelist_head    pool;
34         refcount_t              ref;
35         struct rcu_head         rcu;
36 };
37
38 /**
39  * struct rethook_node - The rethook shadow-stack entry node.
40  * @freelist: The freelist, linked to struct rethook::pool.
41  * @rcu: The rcu_head for deferred freeing.
42  * @llist: The llist, linked to a struct task_struct::rethooks.
43  * @rethook: The pointer to the struct rethook.
44  * @ret_addr: The storage for the real return address.
45  * @frame: The storage for the frame pointer.
46  *
47  * You can embed this to your extended data structure to store any data
48  * on each entry of the shadow stack.
49  */
50 struct rethook_node {
51         union {
52                 struct freelist_node freelist;
53                 struct rcu_head      rcu;
54         };
55         struct llist_node       llist;
56         struct rethook          *rethook;
57         unsigned long           ret_addr;
58         unsigned long           frame;
59 };
60
61 struct rethook *rethook_alloc(void *data, rethook_handler_t handler);
62 void rethook_stop(struct rethook *rh);
63 void rethook_free(struct rethook *rh);
64 void rethook_add_node(struct rethook *rh, struct rethook_node *node);
65 struct rethook_node *rethook_try_get(struct rethook *rh);
66 void rethook_recycle(struct rethook_node *node);
67 void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount);
68 unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame,
69                                     struct llist_node **cur);
70
71 /* Arch dependent code must implement arch_* and trampoline code */
72 void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount);
73 void arch_rethook_trampoline(void);
74
75 /**
76  * is_rethook_trampoline() - Check whether the address is rethook trampoline
77  * @addr: The address to be checked
78  *
79  * Return true if the @addr is the rethook trampoline address.
80  */
81 static inline bool is_rethook_trampoline(unsigned long addr)
82 {
83         return addr == (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline);
84 }
85
86 /* If the architecture needs to fixup the return address, implement it. */
87 void arch_rethook_fixup_return(struct pt_regs *regs,
88                                unsigned long correct_ret_addr);
89
90 /* Generic trampoline handler, arch code must prepare asm stub */
91 unsigned long rethook_trampoline_handler(struct pt_regs *regs,
92                                          unsigned long frame);
93
94 #ifdef CONFIG_RETHOOK
95 void rethook_flush_task(struct task_struct *tk);
96 #else
97 #define rethook_flush_task(tsk) do { } while (0)
98 #endif
99
100 #endif
101