]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/asm.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / i386 / asm.h
1 /*
2 * Copyright (c) 2000-2018 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
57 #ifndef _I386_ASM_H_
58 #define _I386_ASM_H_
59
60 #if defined(__i386__)
61
62 #define S_PC (%esp)
63 #define S_ARG0 4(%esp)
64 #define S_ARG1 8(%esp)
65 #define S_ARG2 12(%esp)
66 #define S_ARG3 16(%esp)
67 #define S_ARG4 20(%esp)
68
69 #define FRAME pushl %ebp; movl %esp, %ebp
70 #define EMARF leave
71
72 #define B_LINK (%ebp)
73 #define B_PC 4(%ebp)
74 #define B_ARG0 8(%ebp)
75 #define B_ARG1 12(%ebp)
76 #define B_ARG2 16(%ebp)
77 #define B_ARG3 20(%ebp)
78
79 #elif defined(__x86_64__)
80
81 #define S_PC (%rsp)
82
83 #define FRAME pushq %rbp; movq %rsp, %rbp
84 #define EMARF leave
85
86 #define B_LINK (%rbp)
87 #define B_PC 8(%rbp)
88
89 #else
90 #error unsupported architecture
91 #endif
92
93 /* There is another definition of ALIGN for .c sources */
94 #ifdef __ASSEMBLER__
95 #define ALIGN 4,0x90
96 #endif /* __ASSEMBLER__ */
97
98 #ifndef FALIGN
99 #define FALIGN ALIGN
100 #endif
101
102 #define LB(x,n) n
103 #if __STDC__
104 #ifndef __NO_UNDERSCORES__
105 #define LCL(x) L ## x
106 #define EXT(x) _ ## x
107 #define LEXT(x) _ ## x ## :
108 #else
109 #define LCL(x) .L ## x
110 #define EXT(x) x
111 #define LEXT(x) x ## :
112 #endif
113 #define LBc(x,n) n ## :
114 #define LBb(x,n) n ## b
115 #define LBf(x,n) n ## f
116 #else /* __STDC__ */
117 #ifndef __NO_UNDERSCORES__
118 #define LCL(x) L/**/x
119 #define EXT(x) _/**/x
120 #define LEXT(x) _/**/x/**/:
121 #else /* __NO_UNDERSCORES__ */
122 #define LCL(x) .L/**/x
123 #define EXT(x) x
124 #define LEXT(x) x/**/:
125 #endif /* __NO_UNDERSCORES__ */
126 #define LBc(x,n) n/**/:
127 #define LBb(x,n) n/**/b
128 #define LBf(x,n) n/**/f
129 #endif /* __STDC__ */
130
131 #define SVC .byte 0x9a; .long 0; .word 0x7
132
133 #define RPC_SVC .byte 0x9a; .long 0; .word 0xf
134
135 #define String .asciz
136 #define Value .word
137 #define Times(a,b) (a*b)
138 #define Divide(a,b) (a/b)
139
140 #define INB inb %dx, %al
141 #define OUTB outb %al, %dx
142 #define INL inl %dx, %eax
143 #define OUTL outl %eax, %dx
144
145 #define data16 .byte 0x66
146 #define addr16 .byte 0x67
147
148 #define MCOUNT
149
150 #if defined(__SHARED__)
151 #define MCOUNT ; .data;\
152 .align ALIGN;\
153 LBc(x, 8) .long 0;\
154 .text;\
155 Gpush;\
156 Gload;\
157 leal Gotoff(LBb(x,8)),%edx;\
158 Egaddr(%eax,_mcount_ptr);\
159 Gpop;\
160 call *(%eax);
161 #endif /* __SHARED__ */
162
163 #ifdef __ELF__
164 #define ELF_FUNC(x) .type x,@function
165 #define ELF_DATA(x) .type x,@object
166 #define ELF_SIZE(x,s) .size x,s
167 #else
168 #define ELF_FUNC(x)
169 #define ELF_DATA(x)
170 #define ELF_SIZE(x,s)
171 #endif
172
173 #define Entry(x) .globl EXT(x); ELF_FUNC(EXT(x)); .align FALIGN; LEXT(x)
174 #define ENTRY(x) Entry(x) MCOUNT
175 #define ENTRY2(x,y) .globl EXT(x); .globl EXT(y); \
176 ELF_FUNC(EXT(x)); ELF_FUNC(EXT(y)); \
177 .align FALIGN; LEXT(x); LEXT(y) \
178 MCOUNT
179 #if __STDC__
180 #define ASENTRY(x) .globl x; .align FALIGN; x ## : ELF_FUNC(x) MCOUNT
181 #else
182 #define ASENTRY(x) .globl x; .align FALIGN; x: ELF_FUNC(x) MCOUNT
183 #endif /* __STDC__ */
184
185 #define DATA(x) .globl EXT(x); ELF_DATA(EXT(x)); .align ALIGN; LEXT(x)
186
187 #define End(x) ELF_SIZE(x,.-x)
188 #define END(x) End(EXT(x))
189 #define ENDDATA(x) END(x)
190 #define Enddata(x) End(x)
191
192 /*
193 * ELF shared library accessor macros.
194 * Gpush saves the %ebx register used for the GOT address
195 * Gpop pops %ebx if we need a GOT
196 * Gload loads %ebx with the GOT address if shared libraries are used
197 * Gcall calls an external function.
198 * Gotoff allows you to reference local labels.
199 * Gotoff2 allows you to reference local labels with an index reg.
200 * Gotoff3 allows you to reference local labels with an index reg & size.
201 * Gaddr loads up a register with an address of an external item.
202 * Gstack is the number of bytes that Gpush pushes on the stack.
203 *
204 * Varients of the above with E or L prefixes do EXT(name) or LCL(name)
205 * respectively.
206 */
207
208 #ifndef __SHARED__
209 #define Gpush
210 #define Gpop
211 #define Gload
212 #define Gcall(func) call func
213 #define Gotoff(lab) lab
214 #define Gotoff2(l,r) l(r)
215 #define Gotoff3(l,r,s) l(,r,s)
216 #define Gaddr(to,lab) movl $lab,to
217 #define Gcmp(lab,reg) cmpl $lab,reg
218 #define Gmemload(lab,reg) movl lab,reg
219 #define Gmemstore(reg,lab,tmp) movl reg,lab
220 #define Gstack 0
221
222 #else
223 #ifdef __ELF__ /* ELF shared libraries */
224 #define Gpush pushl %ebx
225 #define Gpop popl %ebx
226 #define Gload call 9f; 9: popl %ebx; addl $_GLOBAL_OFFSET_TABLE_+[.-9b],%ebx
227 #define Gcall(func) call EXT(func)@PLT
228 #define Gotoff(lab) lab@GOTOFF(%ebx)
229 #define Gotoff2(l,r) l@GOTOFF(%ebx,r)
230 #define Gotoff3(l,r,s) l@GOTOFF(%ebx,r,s)
231 #define Gaddr(to,lab) movl lab@GOT(%ebx),to
232 #define Gcmp(lab,reg) cmpl reg,lab@GOT(%ebx)
233 #define Gmemload(lab,reg) movl lab@GOT(%ebx),reg; movl (reg),reg
234 #define Gmemstore(reg,lab,tmp) movl lab@GOT(%ebx),tmp; movl reg,(tmp)
235 #define Gstack 4
236
237 #else /* ROSE shared libraries */
238 #define Gpush
239 #define Gpop
240 #define Gload
241 #define Gcall(func) call *9f; .data; .align ALIGN; 9: .long func; .text
242 #define Gotoff(lab) lab
243 #define Gotoff2(l,r) l(r)
244 #define Gotoff3(l,r,s) l(,r,s)
245 #define Gaddr(to,lab) movl 9f,to; .data; .align ALIGN; 9: .long lab; .text
246 #define Gcmp(lab,reg) cmpl reg,9f; .data; .align ALIGN; 9: .long lab; .text
247 #define Gmemload(lab,reg) movl 9f,reg; movl (reg),reg; .data; .align ALIGN; 9: .long lab; .text
248 #define Gmemstore(reg,lab,tmp) movl 9f,tmp; movl reg,(tmp); .data; .align ALIGN; 9: .long lab; .text
249 #define Gstack 0
250 #endif /* __ELF__ */
251 #endif /* __SHARED__ */
252
253 /* Egotoff is not provided, since external symbols should not use @GOTOFF
254 relocations. */
255 #define Egcall(func) Gcall(EXT(func))
256 #define Egaddr(to,lab) Gaddr(to,EXT(lab))
257 #define Egcmp(lab,reg) Gcmp(EXT(lab),reg)
258 #define Egmemload(lab,reg) Gmemload(EXT(lab),reg)
259 #define Egmemstore(reg,lab,tmp) Gmemstore(reg,EXT(lab),tmp)
260
261 #define Lgotoff(lab) Gotoff(LCL(lab))
262 #define Lgotoff2(l,r) Gotoff2(LCL(l),r)
263 #define Lgotoff3(l,r,s) Gotoff3(LCL(l),r,s)
264 #define Lgcmp(lab,reg) Gcmp(LCL(lab),reg)
265 #define Lgmemload(lab,reg) movl Lgotoff(lab),reg
266 #define Lgmemstore(reg,lab,tmp) movl reg,Lgotoff(lab)
267
268 #ifndef __ASSEMBLER__
269 /* These defines are here for .c files that wish to reference global symbols
270 * within __asm__ statements.
271 */
272 #ifndef __NO_UNDERSCORES__
273 #define CC_SYM_PREFIX "_"
274 #else
275 #define CC_SYM_PREFIX ""
276 #endif /* __NO_UNDERSCORES__ */
277 #endif /* __ASSEMBLER__ */
278
279 /*
280 * The following macros make calls into C code.
281 * They dynamically align the stack to 16 bytes.
282 */
283 #if defined(__i386__)
284 /*
285 * Arguments are moved (not pushed) onto the correctly aligned stack.
286 * NOTE: ESI is destroyed in the process, and hence cannot
287 * be directly used as a parameter. Users of this macro must
288 * independently preserve ESI (a non-volatile) if the routine is
289 * intended to be called from C, for instance.
290 */
291
292 #define CCALL(fn) \
293 movl %esp, %esi ;\
294 andl $0xFFFFFFF0, %esp ;\
295 call EXT(fn) ;\
296 movl %esi, %esp
297
298 #define CCALL1(fn, arg1) \
299 movl %esp, %esi ;\
300 subl $4, %esp ;\
301 andl $0xFFFFFFF0, %esp ;\
302 movl arg1, (%esp) ;\
303 call EXT(fn) ;\
304 movl %esi, %esp
305
306 #define CCALL2(fn, arg1, arg2) \
307 movl %esp, %esi ;\
308 subl $8, %esp ;\
309 andl $0xFFFFFFF0, %esp ;\
310 movl arg2, 4(%esp) ;\
311 movl arg1, (%esp) ;\
312 call EXT(fn) ;\
313 movl %esi, %esp
314
315 /* This variant exists to permit adjustment of the stack by "dtrace" */
316 #define CCALL1WITHSP(fn, arg1) \
317 movl %esp, %esi ;\
318 subl $12, %esp ;\
319 andl $0xFFFFFFF0, %esp ;\
320 movl %esi, 8(%esp) ;\
321 leal 8(%esp), %esi ;\
322 movl %esi, 4(%esp) ;\
323 movl arg1, (%esp) ;\
324 call EXT(fn) ;\
325 movl 8(%esp), %esp
326
327 /*
328 * CCALL5 is used for callee functions with 3 arguments but
329 * where arg2 (a3:a2) and arg3 (a5:a4) are 64-bit values.
330 */
331 #define CCALL5(fn, a1, a2, a3, a4, a5) \
332 movl %esp, %esi ;\
333 subl $20, %esp ;\
334 andl $0xFFFFFFF0, %esp ;\
335 movl a5, 16(%esp) ;\
336 movl a4, 12(%esp) ;\
337 movl a3, 8(%esp) ;\
338 movl a2, 4(%esp) ;\
339 movl a1, (%esp) ;\
340 call EXT(fn) ;\
341 movl %esi, %esp
342
343 #elif defined(__x86_64__)
344
345 /* This variant exists to permit adjustment of the stack by "dtrace" */
346 #define CCALLWITHSP(fn) \
347 mov %rsp, %r12 ;\
348 sub $8, %rsp ;\
349 and $0xFFFFFFFFFFFFFFF0, %rsp ;\
350 mov %r12, (%rsp) ;\
351 leaq (%rsp), %rsi ;\
352 call EXT(fn) ;\
353 mov (%rsp), %rsp
354
355 #define CCALL(fn) \
356 mov %rsp, %r12 ;\
357 and $0xFFFFFFFFFFFFFFF0, %rsp ;\
358 call EXT(fn) ;\
359 mov %r12, %rsp
360
361 #define CCALL1(fn, arg1) \
362 mov arg1, %rdi ;\
363 CCALL(fn)
364
365 #define CCALL2(fn, arg1, arg2) \
366 mov arg1, %rdi ;\
367 mov arg2, %rsi ;\
368 CCALL(fn)
369
370 #define CCALL3(fn, arg1, arg2, arg3) \
371 mov arg1, %rdi ;\
372 mov arg2, %rsi ;\
373 mov arg3, %rdx ;\
374 CCALL(fn)
375
376 #else
377 #error unsupported architecture
378 #endif
379
380 #endif /* _I386_ASM_H_ */