xref: /xnu-12377.81.4/osfmk/machine/trap.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2000-2007 Apple 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 #ifndef _MACHINE_TRAP_H
29 #define _MACHINE_TRAP_H
30 
31 #if defined (__i386__) || defined (__x86_64__)
32 #include "i386/trap.h"
33 #elif defined (__arm__) || defined (__arm64__)
34 #include "arm/trap.h"
35 #else
36 #error architecture not supported
37 #endif
38 
39 #define ml_trap_pin_value_1(a) ({ \
40 	register long _a __asm__(ML_TRAP_REGISTER_1) = (long)(a);               \
41                                                                                 \
42 	__asm__ __volatile__ ("" : "+r"(_a));                                   \
43 })
44 #define ml_trap_pin_value_2(a, b) ({ \
45 	register long _a __asm__(ML_TRAP_REGISTER_1) = (long)(a);               \
46 	register long _b __asm__(ML_TRAP_REGISTER_2) = (long)(b);               \
47                                                                                 \
48 	__asm__ __volatile__ ("" : "+r"(_a), "+r"(_b));                         \
49 })
50 #define ml_trap_pin_value_3(a, b, c) ({ \
51 	register long _a __asm__(ML_TRAP_REGISTER_1) = (long)(a);               \
52 	register long _b __asm__(ML_TRAP_REGISTER_2) = (long)(b);               \
53 	register long _c __asm__(ML_TRAP_REGISTER_3) = (long)(c);               \
54                                                                                 \
55 	__asm__ __volatile__ ("" : "+r"(_a), "+r"(_b), "+r"(_c));               \
56 })
57 
58 #ifndef __BUILDING_XNU_LIB_UNITTEST__
59 
60 #define ml_fatal_trap_with_value(code, a)  ({ \
61 	ml_trap_pin_value_1(a); \
62 	ml_fatal_trap(code); \
63 })
64 
65 #define ml_fatal_trap_with_value2(code, a, b)  ({ \
66 	ml_trap_pin_value_2(a, b); \
67 	ml_fatal_trap(code); \
68 })
69 
70 #define ml_fatal_trap_with_value3(code, a, b, c)  ({ \
71 	ml_trap_pin_value_3(a, b, c); \
72 	ml_fatal_trap(code); \
73 })
74 
75 #else /* __BUILDING_XNU_LIB_UNITTEST__ */
76 /* assert trap call into unit-test harness instead of calling brk */
77 #ifdef __cplusplus
78 extern "C"
79 #else
80 extern
81 #endif
82 __attribute__((noreturn)) void ut_assert_trap(int code, long a, long b, long c);
83 
84 #define ml_fatal_trap_with_value(code, a)  ({ \
85 	ut_assert_trap(code, (long)a, 0, 0); \
86 })
87 
88 #define ml_fatal_trap_with_value2(code, a)  ({ \
89 	ut_assert_trap(code, (long)a, (long)b, 0); \
90 })
91 
92 #define ml_fatal_trap_with_value3(code, a, b, c)  ({ \
93 	ut_assert_trap(code, (long)a, (long)b, (long)c); \
94 })
95 
96 #endif /* __BUILDING_XNU_LIB_UNITTEST__ */
97 
98 /*
99  * Used for when `e` failed a linked list safe unlinking check.
100  * On optimized builds, `e`'s value will be in:
101  * - %rax for Intel
102  * - x8 for arm64
103  * - r8 on armv7
104  */
105 __attribute__((cold, noreturn, always_inline))
106 static inline void
ml_fatal_trap_invalid_list_linkage(unsigned long e)107 ml_fatal_trap_invalid_list_linkage(unsigned long e)
108 {
109 	ml_fatal_trap_with_value(/* XNU_HARD_TRAP_SAFE_UNLINK */ 0xbffd, e);
110 }
111 
112 #endif /* _MACHINE_TRAP_H */
113