]>
Commit | Line | Data |
---|---|---|
91447636 | 1 | /* |
39236c6e | 2 | * Copyright (c) 2005-2012 Apple Inc. All rights reserved. |
91447636 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
91447636 A |
27 | */ |
28 | ||
29 | // NOTE: This file is only c++ so I can get static initialisers going | |
30 | #include <libkern/OSDebug.h> | |
6d2010ae | 31 | #include <IOKit/IOLib.h> |
91447636 A |
32 | |
33 | #include <sys/cdefs.h> | |
34 | ||
35 | #include <stdarg.h> | |
36 | #include <mach/mach_types.h> | |
37 | #include <mach/kmod.h> | |
b0d623f7 | 38 | #include <kern/locks.h> |
91447636 | 39 | |
0a7de745 | 40 | #include <libkern/libkern.h> // From bsd's libkern directory |
0c530ab8 | 41 | #include <mach/vm_param.h> |
91447636 | 42 | |
2d21ac55 | 43 | #include <sys/kdebug.h> |
b0d623f7 A |
44 | #include <kern/thread.h> |
45 | ||
cb323159 A |
46 | #if defined(HAS_APPLE_PAC) |
47 | #include <ptrauth.h> | |
48 | #endif | |
d9a64523 | 49 | |
2d21ac55 | 50 | extern int etext; |
91447636 A |
51 | __BEGIN_DECLS |
52 | // From osmfk/kern/thread.h but considered to be private | |
53 | extern vm_offset_t min_valid_stack_address(void); | |
54 | extern vm_offset_t max_valid_stack_address(void); | |
55 | ||
3e170ce0 A |
56 | // From osfmk/kern/printf.c |
57 | extern boolean_t doprnt_hide_pointers; | |
58 | ||
91447636 | 59 | // From osfmk/kmod.c |
fe8ab488 | 60 | extern void kmod_dump_log(vm_offset_t *addr, unsigned int cnt, boolean_t doUnslide); |
0c530ab8 A |
61 | |
62 | extern addr64_t kvtophys(vm_offset_t va); | |
0a7de745 | 63 | #if __arm__ |
5ba3f43e A |
64 | extern int copyinframe(vm_address_t fp, char *frame); |
65 | #elif defined(__arm64__) | |
66 | extern int copyinframe(vm_address_t fp, char *frame, boolean_t is64bit); | |
67 | #endif | |
2d21ac55 | 68 | |
91447636 A |
69 | __END_DECLS |
70 | ||
b0d623f7 A |
71 | extern lck_grp_t *IOLockGroup; |
72 | ||
73 | static lck_mtx_t *sOSReportLock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL); | |
91447636 | 74 | |
0a7de745 | 75 | /* Use kernel_debug() to log a backtrace */ |
2d21ac55 | 76 | void |
0a7de745 A |
77 | trace_backtrace(uint32_t debugid, uint32_t debugid2, uintptr_t size, uintptr_t data) |
78 | { | |
2d21ac55 A |
79 | void *bt[16]; |
80 | const unsigned cnt = sizeof(bt) / sizeof(bt[0]); | |
0a7de745 | 81 | unsigned i; |
2d21ac55 A |
82 | int found = 0; |
83 | ||
0a7de745 A |
84 | OSBacktrace(bt, cnt); |
85 | ||
2d21ac55 | 86 | /* find first non-kernel frame */ |
0a7de745 A |
87 | for (i = 3; i < cnt && bt[i]; i++) { |
88 | if (bt[i] > (void*)&etext) { | |
2d21ac55 | 89 | found = 1; |
0a7de745 | 90 | break; |
2d21ac55 A |
91 | } |
92 | } | |
0a7de745 | 93 | /* |
2d21ac55 A |
94 | * if there are non-kernel frames, only log these |
95 | * otherwise, log everything but the first two | |
96 | */ | |
0a7de745 A |
97 | if (!found) { |
98 | i = 2; | |
99 | } | |
2d21ac55 | 100 | |
cb323159 | 101 | #define safe_bt(a) (uintptr_t)(a<cnt ? bt[a] : NULL) |
0a7de745 A |
102 | kernel_debug(debugid, data, size, safe_bt(i), safe_bt(i + 1), 0); |
103 | kernel_debug(debugid2, safe_bt(i + 2), safe_bt(i + 3), safe_bt(i + 4), safe_bt(i + 5), 0); | |
2d21ac55 A |
104 | } |
105 | ||
91447636 A |
106 | /* Report a message with a 4 entry backtrace - very slow */ |
107 | void | |
108 | OSReportWithBacktrace(const char *str, ...) | |
109 | { | |
0a7de745 A |
110 | char buf[128]; |
111 | void *bt[9] = {}; | |
112 | const unsigned cnt = sizeof(bt) / sizeof(bt[0]); | |
113 | va_list listp; | |
114 | ||
115 | // Ignore the our and our callers stackframes, skipping frames 0 & 1 | |
116 | (void) OSBacktrace(bt, cnt); | |
117 | ||
118 | va_start(listp, str); | |
119 | vsnprintf(buf, sizeof(buf), str, listp); | |
120 | va_end(listp); | |
121 | ||
122 | lck_mtx_lock(sOSReportLock); | |
123 | { | |
124 | boolean_t old_doprnt_hide_pointers = doprnt_hide_pointers; | |
125 | doprnt_hide_pointers = FALSE; | |
126 | printf("%s\nBacktrace 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", buf, | |
127 | (unsigned long) VM_KERNEL_UNSLIDE(bt[2]), (unsigned long) VM_KERNEL_UNSLIDE(bt[3]), | |
128 | (unsigned long) VM_KERNEL_UNSLIDE(bt[4]), (unsigned long) VM_KERNEL_UNSLIDE(bt[5]), | |
129 | (unsigned long) VM_KERNEL_UNSLIDE(bt[6]), (unsigned long) VM_KERNEL_UNSLIDE(bt[7]), | |
130 | (unsigned long) VM_KERNEL_UNSLIDE(bt[8])); | |
131 | kmod_dump_log((vm_offset_t *) &bt[2], cnt - 2, TRUE); | |
132 | doprnt_hide_pointers = old_doprnt_hide_pointers; | |
133 | } | |
134 | lck_mtx_unlock(sOSReportLock); | |
91447636 A |
135 | } |
136 | ||
137 | static vm_offset_t minstackaddr = min_valid_stack_address(); | |
138 | static vm_offset_t maxstackaddr = max_valid_stack_address(); | |
139 | ||
b0d623f7 A |
140 | |
141 | #if __x86_64__ | |
142 | #define x86_64_RETURN_OFFSET 8 | |
143 | static unsigned int | |
144 | x86_64_validate_raddr(vm_offset_t raddr) | |
145 | { | |
0a7de745 A |
146 | return (raddr > VM_MIN_KERNEL_AND_KEXT_ADDRESS) && |
147 | (raddr < VM_MAX_KERNEL_ADDRESS); | |
0c530ab8 | 148 | } |
b0d623f7 A |
149 | static unsigned int |
150 | x86_64_validate_stackptr(vm_offset_t stackptr) | |
151 | { | |
152 | /* Existence and alignment check | |
153 | */ | |
0a7de745 | 154 | if (!stackptr || (stackptr & 0x7) || !x86_64_validate_raddr(stackptr)) { |
b0d623f7 | 155 | return 0; |
0a7de745 A |
156 | } |
157 | ||
b0d623f7 A |
158 | /* Is a virtual->physical translation present? |
159 | */ | |
0a7de745 | 160 | if (!kvtophys(stackptr)) { |
b0d623f7 | 161 | return 0; |
0a7de745 A |
162 | } |
163 | ||
b0d623f7 A |
164 | /* Check if the return address lies on the same page; |
165 | * If not, verify that a translation exists. | |
166 | */ | |
167 | if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < x86_64_RETURN_OFFSET) && | |
0a7de745 | 168 | !kvtophys(stackptr + x86_64_RETURN_OFFSET)) { |
b0d623f7 | 169 | return 0; |
0a7de745 | 170 | } |
b0d623f7 A |
171 | return 1; |
172 | } | |
0c530ab8 A |
173 | #endif |
174 | ||
6d2010ae A |
175 | void |
176 | OSPrintBacktrace(void) | |
177 | { | |
178 | void * btbuf[20]; | |
179 | int tmp = OSBacktrace(btbuf, 20); | |
180 | int i; | |
0a7de745 | 181 | for (i = 0; i < tmp; i++) { |
6d2010ae A |
182 | kprintf("bt[%.2d] = %p\n", i, btbuf[i]); |
183 | } | |
184 | } | |
b0d623f7 | 185 | |
0a7de745 A |
186 | unsigned |
187 | OSBacktrace(void **bt, unsigned maxAddrs) | |
91447636 | 188 | { |
0a7de745 A |
189 | unsigned frame; |
190 | if (!current_thread()) { | |
191 | return 0; | |
192 | } | |
91447636 | 193 | |
39236c6e | 194 | #if __x86_64__ |
b0d623f7 | 195 | #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1) |
0a7de745 A |
196 | vm_offset_t stackptr, stackptr_prev, raddr; |
197 | unsigned frame_index = 0; | |
b0d623f7 A |
198 | /* Obtain current frame pointer */ |
199 | ||
0a7de745 | 200 | __asm__ volatile ("movq %%rbp, %0" : "=m" (stackptr)); |
b0d623f7 | 201 | |
0a7de745 A |
202 | if (!x86_64_validate_stackptr(stackptr)) { |
203 | goto pad; | |
204 | } | |
b0d623f7 | 205 | |
0a7de745 | 206 | raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET)); |
b0d623f7 | 207 | |
0a7de745 A |
208 | if (!x86_64_validate_raddr(raddr)) { |
209 | goto pad; | |
210 | } | |
b0d623f7 | 211 | |
0a7de745 | 212 | bt[frame_index++] = (void *) raddr; |
b0d623f7 | 213 | |
0a7de745 A |
214 | for (; frame_index < maxAddrs; frame_index++) { |
215 | stackptr_prev = stackptr; | |
216 | stackptr = *((vm_offset_t *) stackptr_prev); | |
b0d623f7 | 217 | |
0a7de745 A |
218 | if (!x86_64_validate_stackptr(stackptr)) { |
219 | break; | |
220 | } | |
221 | /* Stack grows downwards */ | |
222 | if (stackptr < stackptr_prev) { | |
223 | break; | |
224 | } | |
b0d623f7 | 225 | |
0a7de745 A |
226 | if ((stackptr - stackptr_prev) > SANE_x86_64_FRAME_SIZE) { |
227 | break; | |
228 | } | |
b0d623f7 | 229 | |
0a7de745 | 230 | raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET)); |
b0d623f7 | 231 | |
0a7de745 A |
232 | if (!x86_64_validate_raddr(raddr)) { |
233 | break; | |
234 | } | |
b0d623f7 | 235 | |
0a7de745 A |
236 | bt[frame_index] = (void *) raddr; |
237 | } | |
b0d623f7 | 238 | pad: |
0a7de745 | 239 | frame = frame_index; |
b0d623f7 | 240 | |
0a7de745 | 241 | for (; frame_index < maxAddrs; frame_index++) { |
cb323159 | 242 | bt[frame_index] = (void *) NULL; |
0a7de745 | 243 | } |
5ba3f43e | 244 | #elif __arm__ || __arm64__ |
0a7de745 A |
245 | uint32_t i = 0; |
246 | uintptr_t frameb[2]; | |
247 | uintptr_t fp = 0; | |
248 | ||
249 | // get the current frame pointer for this thread | |
5ba3f43e A |
250 | #if defined(__arm__) |
251 | #define OSBacktraceFrameAlignOK(x) (((x) & 0x3) == 0) | |
0a7de745 | 252 | __asm__ volatile ("mov %0,r7" : "=r" (fp)); |
5ba3f43e A |
253 | #elif defined(__arm64__) |
254 | #define OSBacktraceFrameAlignOK(x) (((x) & 0xf) == 0) | |
0a7de745 | 255 | __asm__ volatile ("mov %0, fp" : "=r" (fp)); |
5ba3f43e A |
256 | #else |
257 | #error Unknown architecture. | |
258 | #endif | |
0a7de745 A |
259 | |
260 | // now crawl up the stack recording the link value of each frame | |
261 | do { | |
262 | // check bounds | |
263 | if ((fp == 0) || (!OSBacktraceFrameAlignOK(fp)) || (fp > VM_MAX_KERNEL_ADDRESS) || (fp < VM_MIN_KERNEL_AND_KEXT_ADDRESS)) { | |
264 | break; | |
265 | } | |
266 | // safely read frame | |
5ba3f43e | 267 | #ifdef __arm64__ |
0a7de745 | 268 | if (copyinframe(fp, (char*)frameb, TRUE) != 0) { |
5ba3f43e | 269 | #else |
0a7de745 | 270 | if (copyinframe(fp, (char*)frameb) != 0) { |
5ba3f43e | 271 | #endif |
0a7de745 A |
272 | break; |
273 | } | |
274 | ||
275 | // No need to use copyin as this is always a kernel address, see check above | |
cb323159 A |
276 | #if defined(HAS_APPLE_PAC) |
277 | /* return addresses on stack signed by arm64e ABI */ | |
278 | bt[i] = ptrauth_strip((void*)frameb[1], ptrauth_key_return_address); // link register | |
279 | #else | |
0a7de745 | 280 | bt[i] = (void*)frameb[1]; // link register |
cb323159 | 281 | #endif |
0a7de745 A |
282 | fp = frameb[0]; |
283 | } while (++i < maxAddrs); | |
284 | frame = i; | |
0c530ab8 A |
285 | #else |
286 | #error arch | |
91447636 | 287 | #endif |
0a7de745 | 288 | return frame; |
91447636 | 289 | } |