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 #define ml_fatal_trap_with_value(code, a) ({ \
59 ml_trap_pin_value_1(a); \
60 ml_fatal_trap(code); \
61 })
62
63 #define ml_fatal_trap_with_value2(code, a, b) ({ \
64 ml_trap_pin_value_2(a, b); \
65 ml_fatal_trap(code); \
66 })
67
68 #define ml_fatal_trap_with_value3(code, a, b, c) ({ \
69 ml_trap_pin_value_3(a, b, c); \
70 ml_fatal_trap(code); \
71 })
72
73 /*
74 * Used for when `e` failed a linked list safe unlinking check.
75 * On optimized builds, `e`'s value will be in:
76 * - %rax for Intel
77 * - x8 for arm64
78 * - r8 on armv7
79 */
80 __attribute__((cold, noreturn, always_inline))
81 static inline void
ml_fatal_trap_invalid_list_linkage(unsigned long e)82 ml_fatal_trap_invalid_list_linkage(unsigned long e)
83 {
84 ml_fatal_trap_with_value(/* XNU_HARD_TRAP_SAFE_UNLINK */ 0xbffd, e);
85 }
86
87 #endif /* _MACHINE_TRAP_H */
88