xref: /xnu-8792.61.2/osfmk/x86_64/dwarf_unwind.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /*
2  * Copyright (c) 2019 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 
29 #ifndef _X86_64_DWARF_UNWIND_H_
30 #define _X86_64_DWARF_UNWIND_H_
31 
32 /*
33  * This file contains the architecture specific DWARF definitions needed for unwind
34  * information added to trap handlers.
35  */
36 
37 /* DWARF Register numbers for x86 */
38 
39 #define DWARF_RAX 0
40 #define DWARF_RDX 1
41 #define DWARF_RCX 2
42 #define DWARF_RBX 3
43 #define DWARF_RSI 4
44 #define DWARF_RDI 5
45 #define DWARF_RBP 6
46 #define DWARF_RSP 7
47 #define DWARF_R8  8
48 #define DWARF_R9  9
49 #define DWARF_R10 10
50 #define DWARF_R11 11
51 #define DWARF_R12 12
52 #define DWARF_R13 13
53 #define DWARF_R14 14
54 #define DWARF_R15 15
55 #define DWARF_RIP 16
56 
57 /* Dwarf opcodes */
58 
59 #define DW_OP_breg15      0x7f
60 #define DW_CFA_expression 0x10
61 
62 /* Convenient DWARF expression macros */
63 
64 #define DW_FORM_LEN_TWO_BYTE_SLEB 3
65 #define DW_FORM_LEN_ONE_BYTE_SLEB 2
66 
67 /* Additional constants for register offsets in the saved state that need to be expressed as SLEB128 */
68 
69 #define R64_RAX_SLEB128 0x88, 0x01
70 #define R64_RCX_SLEB128 0x80, 0x01
71 #define R64_RBX_SLEB128 0xf8, 0x00
72 #define R64_RBP_SLEB128 0xf0, 0x00
73 #define R64_RSP_SLEB128 0xd0, 0x01
74 #define R64_R11_SLEB128 0xe8, 0x00
75 #define R64_R12_SLEB128 0xe0, 0x00
76 #define R64_R13_SLEB128 0xd8, 0x00
77 #define R64_R14_SLEB128 0xd0, 0x00
78 #define R64_R15_SLEB128 0xc8, 0x00
79 #define R64_RIP_SLEB128 0xb8, 0x01
80 
81 /* The actual unwind directives added to trap handlers to let the debugger know where the register state is stored */
82 
83 /* Unwind Prologue added to each function to indicate the start of the unwind information. */
84 
85 #define UNWIND_PROLOGUE \
86 .cfi_sections .eh_frame ;\
87 .cfi_startproc;        ;\
88 .cfi_signal_frame       ;\
89 
90 
91 /* Unwind Epilogue added to each function to indicate the end of the unwind information */
92 
93 #define UNWIND_EPILOGUE .cfi_endproc
94 
95 
96 #define UNWIND_DIRECTIVES \
97 .cfi_escape DW_CFA_expression, DWARF_RAX, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_RAX_SLEB128 ;\
98 .cfi_escape DW_CFA_expression, DWARF_RDX, DW_FORM_LEN_ONE_BYTE_SLEB, DW_OP_breg15, R64_RDX         ;\
99 .cfi_escape DW_CFA_expression, DWARF_RCX, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_RCX_SLEB128 ;\
100 .cfi_escape DW_CFA_expression, DWARF_RBX, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_RBX_SLEB128 ;\
101 .cfi_escape DW_CFA_expression, DWARF_RSI, DW_FORM_LEN_ONE_BYTE_SLEB, DW_OP_breg15, R64_RSI         ;\
102 .cfi_escape DW_CFA_expression, DWARF_RDI, DW_FORM_LEN_ONE_BYTE_SLEB, DW_OP_breg15, R64_RDI         ;\
103 .cfi_escape DW_CFA_expression, DWARF_RBP, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_RBP_SLEB128 ;\
104 .cfi_escape DW_CFA_expression, DWARF_RSP, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_RSP_SLEB128 ;\
105 .cfi_escape DW_CFA_expression, DWARF_R8,  DW_FORM_LEN_ONE_BYTE_SLEB, DW_OP_breg15, R64_R8          ;\
106 .cfi_escape DW_CFA_expression, DWARF_R9,  DW_FORM_LEN_ONE_BYTE_SLEB, DW_OP_breg15, R64_R9          ;\
107 .cfi_escape DW_CFA_expression, DWARF_R10, DW_FORM_LEN_ONE_BYTE_SLEB, DW_OP_breg15, R64_R10         ;\
108 .cfi_escape DW_CFA_expression, DWARF_R11, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_R11_SLEB128 ;\
109 .cfi_escape DW_CFA_expression, DWARF_R12, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_R12_SLEB128 ;\
110 .cfi_escape DW_CFA_expression, DWARF_R13, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_R13_SLEB128 ;\
111 .cfi_escape DW_CFA_expression, DWARF_R14, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_R14_SLEB128 ;\
112 .cfi_escape DW_CFA_expression, DWARF_R15, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_R15_SLEB128 ;\
113 .cfi_escape DW_CFA_expression, DWARF_RIP, DW_FORM_LEN_TWO_BYTE_SLEB, DW_OP_breg15, R64_RIP_SLEB128 ;\
114 
115 #endif /*  _X86_64_DWARF_UNWIND_H_ */
116