1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_ASM_H
3#define _ASM_X86_ASM_H
4
5#ifdef __ASSEMBLER__
6# define __ASM_FORM(x, ...) x,## __VA_ARGS__
7# define __ASM_FORM_RAW(x, ...) x,## __VA_ARGS__
8# define __ASM_FORM_COMMA(x, ...) x,## __VA_ARGS__,
9# define __ASM_REGPFX %
10#else
11#include <linux/stringify.h>
12# define __ASM_FORM(x, ...) " " __stringify(x,##__VA_ARGS__) " "
13# define __ASM_FORM_RAW(x, ...) __stringify(x,##__VA_ARGS__)
14# define __ASM_FORM_COMMA(x, ...) " " __stringify(x,##__VA_ARGS__) ","
15# define __ASM_REGPFX %%
16#endif
17
18#define _ASM_BYTES(x, ...) __ASM_FORM(.byte x,##__VA_ARGS__ ;)
19
20#ifndef __x86_64__
21/* 32 bit */
22# define __ASM_SEL(a,b) __ASM_FORM(a)
23# define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(a)
24#else
25/* 64 bit */
26# define __ASM_SEL(a,b) __ASM_FORM(b)
27# define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(b)
28#endif
29
30#define __ASM_SIZE(inst, ...) __ASM_SEL(inst##l##__VA_ARGS__, \
31 inst##q##__VA_ARGS__)
32#define __ASM_REG(reg) __ASM_SEL_RAW(e##reg, r##reg)
33
34#define _ASM_PTR __ASM_SEL(.long, .quad)
35#define _ASM_ALIGN __ASM_SEL(.balign 4, .balign 8)
36
37#define _ASM_MOV __ASM_SIZE(mov)
38#define _ASM_INC __ASM_SIZE(inc)
39#define _ASM_DEC __ASM_SIZE(dec)
40#define _ASM_ADD __ASM_SIZE(add)
41#define _ASM_SUB __ASM_SIZE(sub)
42#define _ASM_XADD __ASM_SIZE(xadd)
43#define _ASM_MUL __ASM_SIZE(mul)
44
45#define _ASM_AX __ASM_REG(ax)
46#define _ASM_BX __ASM_REG(bx)
47#define _ASM_CX __ASM_REG(cx)
48#define _ASM_DX __ASM_REG(dx)
49#define _ASM_SP __ASM_REG(sp)
50#define _ASM_BP __ASM_REG(bp)
51#define _ASM_SI __ASM_REG(si)
52#define _ASM_DI __ASM_REG(di)
53
54/* Adds a (%rip) suffix on 64 bits only; for immediate memory references */
55#define _ASM_RIP(x) __ASM_SEL_RAW(x, x (__ASM_REGPFX rip))
56
57#ifndef __x86_64__
58/* 32 bit */
59
60#define _ASM_ARG1 _ASM_AX
61#define _ASM_ARG2 _ASM_DX
62#define _ASM_ARG3 _ASM_CX
63
64#define _ASM_ARG1L eax
65#define _ASM_ARG2L edx
66#define _ASM_ARG3L ecx
67
68#define _ASM_ARG1W ax
69#define _ASM_ARG2W dx
70#define _ASM_ARG3W cx
71
72#define _ASM_ARG1B al
73#define _ASM_ARG2B dl
74#define _ASM_ARG3B cl
75
76#else
77/* 64 bit */
78
79#define _ASM_ARG1 _ASM_DI
80#define _ASM_ARG2 _ASM_SI
81#define _ASM_ARG3 _ASM_DX
82#define _ASM_ARG4 _ASM_CX
83#define _ASM_ARG5 r8
84#define _ASM_ARG6 r9
85
86#define _ASM_ARG1Q rdi
87#define _ASM_ARG2Q rsi
88#define _ASM_ARG3Q rdx
89#define _ASM_ARG4Q rcx
90#define _ASM_ARG5Q r8
91#define _ASM_ARG6Q r9
92
93#define _ASM_ARG1L edi
94#define _ASM_ARG2L esi
95#define _ASM_ARG3L edx
96#define _ASM_ARG4L ecx
97#define _ASM_ARG5L r8d
98#define _ASM_ARG6L r9d
99
100#define _ASM_ARG1W di
101#define _ASM_ARG2W si
102#define _ASM_ARG3W dx
103#define _ASM_ARG4W cx
104#define _ASM_ARG5W r8w
105#define _ASM_ARG6W r9w
106
107#define _ASM_ARG1B dil
108#define _ASM_ARG2B sil
109#define _ASM_ARG3B dl
110#define _ASM_ARG4B cl
111#define _ASM_ARG5B r8b
112#define _ASM_ARG6B r9b
113
114#endif
115
116#ifndef __ASSEMBLER__
117static __always_inline __pure void *rip_rel_ptr(void *p)
118{
119 asm("leaq %c1(%%rip), %0" : "=r"(p) : "i"(p));
120
121 return p;
122}
123#endif
124
125#ifdef __KERNEL__
126
127# include <asm/extable_fixup_types.h>
128
129/* Exception table entry */
130#ifdef __ASSEMBLER__
131
132# define _ASM_EXTABLE_TYPE(from, to, type) \
133 .pushsection "__ex_table","a" ; \
134 .balign 4 ; \
135 .long (from) - . ; \
136 .long (to) - . ; \
137 .long type ; \
138 .popsection
139
140# ifdef CONFIG_KPROBES
141# define _ASM_NOKPROBE(entry) \
142 .pushsection "_kprobe_blacklist","aw" ; \
143 _ASM_ALIGN ; \
144 _ASM_PTR (entry); \
145 .popsection
146# else
147# define _ASM_NOKPROBE(entry)
148# endif
149
150#else /* ! __ASSEMBLER__ */
151
152# define DEFINE_EXTABLE_TYPE_REG \
153 ".macro extable_type_reg type:req reg:req\n" \
154 ".set .Lfound, 0\n" \
155 ".set .Lregnr, 0\n" \
156 ".irp rs,rax,rcx,rdx,rbx,rsp,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15\n" \
157 ".ifc \\reg, %%\\rs\n" \
158 ".set .Lfound, .Lfound+1\n" \
159 ".long \\type + (.Lregnr << 8)\n" \
160 ".endif\n" \
161 ".set .Lregnr, .Lregnr+1\n" \
162 ".endr\n" \
163 ".set .Lregnr, 0\n" \
164 ".irp rs,eax,ecx,edx,ebx,esp,ebp,esi,edi,r8d,r9d,r10d,r11d,r12d,r13d,r14d,r15d\n" \
165 ".ifc \\reg, %%\\rs\n" \
166 ".set .Lfound, .Lfound+1\n" \
167 ".long \\type + (.Lregnr << 8)\n" \
168 ".endif\n" \
169 ".set .Lregnr, .Lregnr+1\n" \
170 ".endr\n" \
171 ".if (.Lfound != 1)\n" \
172 ".error \"extable_type_reg: bad register argument\"\n" \
173 ".endif\n" \
174 ".endm\n"
175
176# define UNDEFINE_EXTABLE_TYPE_REG \
177 ".purgem extable_type_reg\n"
178
179# define _ASM_EXTABLE_TYPE(from, to, type) \
180 " .pushsection \"__ex_table\",\"a\"\n" \
181 " .balign 4\n" \
182 " .long (" #from ") - .\n" \
183 " .long (" #to ") - .\n" \
184 " .long " __stringify(type) " \n" \
185 " .popsection\n"
186
187# define _ASM_EXTABLE_TYPE_REG(from, to, type, reg) \
188 " .pushsection \"__ex_table\",\"a\"\n" \
189 " .balign 4\n" \
190 " .long (" #from ") - .\n" \
191 " .long (" #to ") - .\n" \
192 DEFINE_EXTABLE_TYPE_REG \
193 "extable_type_reg reg=" __stringify(reg) ", type=" __stringify(type) " \n"\
194 UNDEFINE_EXTABLE_TYPE_REG \
195 " .popsection\n"
196
197/* For C file, we already have NOKPROBE_SYMBOL macro */
198
199/* Insert a comma if args are non-empty */
200#define COMMA(x...) __COMMA(x)
201#define __COMMA(...) , ##__VA_ARGS__
202
203/*
204 * Combine multiple asm inline constraint args into a single arg for passing to
205 * another macro.
206 */
207#define ASM_OUTPUT(x...) x
208#define ASM_INPUT(x...) x
209
210/*
211 * This output constraint should be used for any inline asm which has a "call"
212 * instruction. Otherwise the asm may be inserted before the frame pointer
213 * gets set up by the containing function. If you forget to do this, objtool
214 * may print a "call without frame pointer save/setup" warning.
215 */
216register unsigned long current_stack_pointer asm(_ASM_SP);
217#define ASM_CALL_CONSTRAINT "+r" (current_stack_pointer)
218#endif /* __ASSEMBLER__ */
219
220#define _ASM_EXTABLE(from, to) \
221 _ASM_EXTABLE_TYPE(from, to, EX_TYPE_DEFAULT)
222
223#define _ASM_EXTABLE_UA(from, to) \
224 _ASM_EXTABLE_TYPE(from, to, EX_TYPE_UACCESS)
225
226#define _ASM_EXTABLE_FAULT(from, to) \
227 _ASM_EXTABLE_TYPE(from, to, EX_TYPE_FAULT)
228
229/*
230 * Both i386 and x86_64 returns 64-bit values in edx:eax for certain
231 * instructions, but GCC's "A" constraint has different meanings.
232 * For i386, "A" means exactly edx:eax, while for x86_64 it
233 * means rax *or* rdx.
234 *
235 * These helpers wrapping these semantic differences save one instruction
236 * clearing the high half of 'low':
237 */
238#ifdef CONFIG_X86_64
239# define EAX_EDX_DECLARE_ARGS(val, low, high) unsigned long low, high
240# define EAX_EDX_VAL(val, low, high) ((low) | (high) << 32)
241# define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
242#else
243# define EAX_EDX_DECLARE_ARGS(val, low, high) u64 val
244# define EAX_EDX_VAL(val, low, high) (val)
245# define EAX_EDX_RET(val, low, high) "=A" (val)
246#endif
247
248#endif /* __KERNEL__ */
249#endif /* _ASM_X86_ASM_H */
250