GNU Linux-libre 4.9.328-gnu1
[releases.git] / arch / x86 / include / asm / unwind_hints.h
1 #ifndef _ASM_X86_UNWIND_HINTS_H
2 #define _ASM_X86_UNWIND_HINTS_H
3
4 #include "orc_types.h"
5
6 #ifdef __ASSEMBLY__
7
8 /*
9  * In asm, there are two kinds of code: normal C-type callable functions and
10  * the rest.  The normal callable functions can be called by other code, and
11  * don't do anything unusual with the stack.  Such normal callable functions
12  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
13  * category.  In this case, no special debugging annotations are needed because
14  * objtool can automatically generate the ORC data for the ORC unwinder to read
15  * at runtime.
16  *
17  * Anything which doesn't fall into the above category, such as syscall and
18  * interrupt handlers, tends to not be called directly by other functions, and
19  * often does unusual non-C-function-type things with the stack pointer.  Such
20  * code needs to be annotated such that objtool can understand it.  The
21  * following CFI hint macros are for this type of code.
22  *
23  * These macros provide hints to objtool about the state of the stack at each
24  * instruction.  Objtool starts from the hints and follows the code flow,
25  * making automatic CFI adjustments when it sees pushes and pops, filling out
26  * the debuginfo as necessary.  It will also warn if it sees any
27  * inconsistencies.
28  */
29 .macro UNWIND_HINT sp_reg=ORC_REG_SP sp_offset=0 type=ORC_TYPE_CALL
30 #ifdef CONFIG_STACK_VALIDATION
31 .Lunwind_hint_ip_\@:
32         .pushsection .discard.unwind_hints
33                 /* struct unwind_hint */
34                 .long .Lunwind_hint_ip_\@ - .
35                 .short \sp_offset
36                 .byte \sp_reg
37                 .byte \type
38         .popsection
39 #endif
40 .endm
41
42 .macro UNWIND_HINT_EMPTY
43         UNWIND_HINT sp_reg=ORC_REG_UNDEFINED
44 .endm
45
46 .macro UNWIND_HINT_REGS base=%rsp offset=0 indirect=0 extra=1 iret=0
47         .if \base == %rsp && \indirect
48                 .set sp_reg, ORC_REG_SP_INDIRECT
49         .elseif \base == %rsp
50                 .set sp_reg, ORC_REG_SP
51         .elseif \base == %rbp
52                 .set sp_reg, ORC_REG_BP
53         .elseif \base == %rdi
54                 .set sp_reg, ORC_REG_DI
55         .elseif \base == %rdx
56                 .set sp_reg, ORC_REG_DX
57         .elseif \base == %r10
58                 .set sp_reg, ORC_REG_R10
59         .else
60                 .error "UNWIND_HINT_REGS: bad base register"
61         .endif
62
63         .set sp_offset, \offset
64
65         .if \iret
66                 .set type, ORC_TYPE_REGS_IRET
67         .elseif \extra == 0
68                 .set type, ORC_TYPE_REGS_IRET
69                 .set sp_offset, \offset + (16*8)
70         .else
71                 .set type, ORC_TYPE_REGS
72         .endif
73
74         UNWIND_HINT sp_reg=sp_reg sp_offset=sp_offset type=type
75 .endm
76
77 .macro UNWIND_HINT_IRET_REGS base=%rsp offset=0
78         UNWIND_HINT_REGS base=\base offset=\offset iret=1
79 .endm
80
81 .macro UNWIND_HINT_FUNC sp_offset=8
82         UNWIND_HINT sp_offset=\sp_offset
83 .endm
84
85 #else /* !__ASSEMBLY__ */
86
87 #define UNWIND_HINT(sp_reg, sp_offset, type)                    \
88         "987: \n\t"                                             \
89         ".pushsection .discard.unwind_hints\n\t"                \
90         /* struct unwind_hint */                                \
91         ".long 987b - .\n\t"                                    \
92         ".short " __stringify(sp_offset) "\n\t"         \
93         ".byte " __stringify(sp_reg) "\n\t"                     \
94         ".byte " __stringify(type) "\n\t"                       \
95         ".popsection\n\t"
96
97 #define UNWIND_HINT_SAVE UNWIND_HINT(0, 0, UNWIND_HINT_TYPE_SAVE)
98
99 #define UNWIND_HINT_RESTORE UNWIND_HINT(0, 0, UNWIND_HINT_TYPE_RESTORE)
100
101 #endif /* __ASSEMBLY__ */
102
103 #endif /* _ASM_X86_UNWIND_HINTS_H */