]>
Commit | Line | Data |
---|---|---|
91447636 A |
1 | /* |
2 | * Copyright (c) 2005 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
91447636 | 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. | |
8f6c56a5 | 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. | |
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 | |
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. | |
8f6c56a5 | 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 A |
39 | |
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 | ||
2d21ac55 | 46 | extern int etext; |
91447636 A |
47 | __BEGIN_DECLS |
48 | // From osmfk/kern/thread.h but considered to be private | |
49 | extern vm_offset_t min_valid_stack_address(void); | |
50 | extern vm_offset_t max_valid_stack_address(void); | |
51 | ||
52 | // From osfmk/kmod.c | |
53 | extern void kmod_dump_log(vm_offset_t *addr, unsigned int cnt); | |
0c530ab8 A |
54 | |
55 | extern addr64_t kvtophys(vm_offset_t va); | |
2d21ac55 | 56 | |
91447636 A |
57 | __END_DECLS |
58 | ||
b0d623f7 A |
59 | extern lck_grp_t *IOLockGroup; |
60 | ||
61 | static lck_mtx_t *sOSReportLock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL); | |
91447636 | 62 | |
2d21ac55 A |
63 | /* Use kernel_debug() to log a backtrace */ |
64 | void | |
b0d623f7 | 65 | trace_backtrace(uint32_t debugid, uint32_t debugid2, uintptr_t size, uintptr_t data) { |
2d21ac55 A |
66 | void *bt[16]; |
67 | const unsigned cnt = sizeof(bt) / sizeof(bt[0]); | |
68 | unsigned i; | |
69 | int found = 0; | |
70 | ||
71 | OSBacktrace(bt, cnt); | |
72 | ||
73 | /* find first non-kernel frame */ | |
74 | for (i = 3; i < cnt && bt[i]; i++) { | |
75 | if (bt[i] > (void*)&etext) { | |
76 | found = 1; | |
77 | break; | |
78 | } | |
79 | } | |
80 | /* | |
81 | * if there are non-kernel frames, only log these | |
82 | * otherwise, log everything but the first two | |
83 | */ | |
84 | if (!found) i=2; | |
85 | ||
b0d623f7 | 86 | #define safe_bt(a) (uintptr_t)(a<cnt ? bt[a] : 0) |
2d21ac55 A |
87 | kernel_debug(debugid, data, size, safe_bt(i), safe_bt(i+1), 0); |
88 | kernel_debug(debugid2, safe_bt(i+2), safe_bt(i+3), safe_bt(i+4), safe_bt(i+5), 0); | |
89 | } | |
90 | ||
91447636 A |
91 | /* Report a message with a 4 entry backtrace - very slow */ |
92 | void | |
93 | OSReportWithBacktrace(const char *str, ...) | |
94 | { | |
95 | char buf[128]; | |
96 | void *bt[9]; | |
97 | const unsigned cnt = sizeof(bt) / sizeof(bt[0]); | |
98 | va_list listp; | |
99 | ||
100 | // Ignore the our and our callers stackframes, skipping frames 0 & 1 | |
101 | (void) OSBacktrace(bt, cnt); | |
102 | ||
103 | va_start(listp, str); | |
104 | vsnprintf(buf, sizeof(buf), str, listp); | |
105 | va_end(listp); | |
106 | ||
b0d623f7 | 107 | lck_mtx_lock(sOSReportLock); |
91447636 | 108 | { |
b0d623f7 A |
109 | printf("%s\nBacktrace %p %p %p %p %p %p %p\n", |
110 | buf, bt[2], bt[3], bt[4], bt[5], bt[6], bt[7], bt[8]); | |
111 | kmod_dump_log((vm_offset_t *) &bt[2], cnt - 2); | |
91447636 | 112 | } |
b0d623f7 | 113 | lck_mtx_unlock(sOSReportLock); |
91447636 A |
114 | } |
115 | ||
116 | static vm_offset_t minstackaddr = min_valid_stack_address(); | |
117 | static vm_offset_t maxstackaddr = max_valid_stack_address(); | |
118 | ||
0c530ab8 A |
119 | #if __i386__ |
120 | #define i386_RETURN_OFFSET 4 | |
121 | ||
122 | static unsigned int | |
123 | i386_validate_stackptr(vm_offset_t stackptr) | |
124 | { | |
125 | /* Existence and alignment check | |
126 | */ | |
127 | if (!stackptr || (stackptr & 0x3)) | |
128 | return 0; | |
129 | ||
130 | /* Is a virtual->physical translation present? | |
131 | */ | |
132 | if (!kvtophys(stackptr)) | |
133 | return 0; | |
134 | ||
135 | /* Check if the return address lies on the same page; | |
136 | * If not, verify that a translation exists. | |
137 | */ | |
138 | if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < i386_RETURN_OFFSET) && | |
139 | !kvtophys(stackptr + i386_RETURN_OFFSET)) | |
140 | return 0; | |
141 | return 1; | |
142 | } | |
143 | ||
144 | static unsigned int | |
145 | i386_validate_raddr(vm_offset_t raddr) | |
146 | { | |
b0d623f7 A |
147 | return ((raddr > VM_MIN_KERNEL_AND_KEXT_ADDRESS) && |
148 | (raddr < VM_MAX_KERNEL_ADDRESS)); | |
149 | } | |
150 | #endif | |
151 | ||
152 | #if __x86_64__ | |
153 | #define x86_64_RETURN_OFFSET 8 | |
154 | static unsigned int | |
155 | x86_64_validate_raddr(vm_offset_t raddr) | |
156 | { | |
157 | return ((raddr > VM_MIN_KERNEL_AND_KEXT_ADDRESS) && | |
0c530ab8 A |
158 | (raddr < VM_MAX_KERNEL_ADDRESS)); |
159 | } | |
b0d623f7 A |
160 | static unsigned int |
161 | x86_64_validate_stackptr(vm_offset_t stackptr) | |
162 | { | |
163 | /* Existence and alignment check | |
164 | */ | |
165 | if (!stackptr || (stackptr & 0x7) || !x86_64_validate_raddr(stackptr)) | |
166 | return 0; | |
167 | ||
168 | /* Is a virtual->physical translation present? | |
169 | */ | |
170 | if (!kvtophys(stackptr)) | |
171 | return 0; | |
172 | ||
173 | /* Check if the return address lies on the same page; | |
174 | * If not, verify that a translation exists. | |
175 | */ | |
176 | if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < x86_64_RETURN_OFFSET) && | |
177 | !kvtophys(stackptr + x86_64_RETURN_OFFSET)) | |
178 | return 0; | |
179 | return 1; | |
180 | } | |
0c530ab8 A |
181 | #endif |
182 | ||
6d2010ae A |
183 | void |
184 | OSPrintBacktrace(void) | |
185 | { | |
186 | void * btbuf[20]; | |
187 | int tmp = OSBacktrace(btbuf, 20); | |
188 | int i; | |
189 | for(i=0;i<tmp;i++) | |
190 | { | |
191 | kprintf("bt[%.2d] = %p\n", i, btbuf[i]); | |
192 | } | |
193 | } | |
b0d623f7 | 194 | |
91447636 A |
195 | unsigned OSBacktrace(void **bt, unsigned maxAddrs) |
196 | { | |
197 | unsigned frame; | |
198 | ||
6d2010ae | 199 | #if __i386__ |
b0d623f7 | 200 | #define SANE_i386_FRAME_SIZE (kernel_stack_size >> 1) |
0c530ab8 A |
201 | vm_offset_t stackptr, stackptr_prev, raddr; |
202 | unsigned frame_index = 0; | |
203 | /* Obtain current frame pointer */ | |
204 | __asm__ volatile("movl %%ebp, %0" : "=m" (stackptr)); | |
4452a7af | 205 | |
0c530ab8 A |
206 | if (!i386_validate_stackptr(stackptr)) |
207 | goto pad; | |
4452a7af | 208 | |
0c530ab8 A |
209 | raddr = *((vm_offset_t *) (stackptr + i386_RETURN_OFFSET)); |
210 | ||
211 | if (!i386_validate_raddr(raddr)) | |
212 | goto pad; | |
213 | ||
214 | bt[frame_index++] = (void *) raddr; | |
215 | ||
216 | for ( ; frame_index < maxAddrs; frame_index++) { | |
217 | stackptr_prev = stackptr; | |
218 | stackptr = *((vm_offset_t *) stackptr_prev); | |
219 | ||
220 | if (!i386_validate_stackptr(stackptr)) | |
221 | break; | |
222 | /* Stack grows downwards */ | |
223 | if (stackptr < stackptr_prev) | |
224 | break; | |
4452a7af | 225 | |
b0d623f7 | 226 | if ((stackptr - stackptr_prev) > SANE_i386_FRAME_SIZE) |
0c530ab8 A |
227 | break; |
228 | ||
229 | raddr = *((vm_offset_t *) (stackptr + i386_RETURN_OFFSET)); | |
230 | ||
231 | if (!i386_validate_raddr(raddr)) | |
232 | break; | |
233 | ||
234 | bt[frame_index] = (void *) raddr; | |
91447636 | 235 | } |
0c530ab8 A |
236 | pad: |
237 | frame = frame_index; | |
91447636 | 238 | |
b0d623f7 A |
239 | for ( ; frame_index < maxAddrs; frame_index++) |
240 | bt[frame_index] = (void *) 0; | |
241 | #elif __x86_64__ | |
242 | #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1) | |
243 | vm_offset_t stackptr, stackptr_prev, raddr; | |
244 | unsigned frame_index = 0; | |
245 | /* Obtain current frame pointer */ | |
246 | ||
247 | __asm__ volatile("movq %%rbp, %0" : "=m" (stackptr)); | |
248 | ||
249 | if (!x86_64_validate_stackptr(stackptr)) | |
250 | goto pad; | |
251 | ||
252 | raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET)); | |
253 | ||
254 | if (!x86_64_validate_raddr(raddr)) | |
255 | goto pad; | |
256 | ||
257 | bt[frame_index++] = (void *) raddr; | |
258 | ||
259 | for ( ; frame_index < maxAddrs; frame_index++) { | |
260 | stackptr_prev = stackptr; | |
261 | stackptr = *((vm_offset_t *) stackptr_prev); | |
262 | ||
263 | if (!x86_64_validate_stackptr(stackptr)) | |
264 | break; | |
265 | /* Stack grows downwards */ | |
266 | if (stackptr < stackptr_prev) | |
267 | break; | |
268 | ||
269 | if ((stackptr - stackptr_prev) > SANE_x86_64_FRAME_SIZE) | |
270 | break; | |
271 | ||
272 | raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET)); | |
273 | ||
274 | if (!x86_64_validate_raddr(raddr)) | |
275 | break; | |
276 | ||
277 | bt[frame_index] = (void *) raddr; | |
278 | } | |
279 | pad: | |
280 | frame = frame_index; | |
281 | ||
0c530ab8 A |
282 | for ( ; frame_index < maxAddrs; frame_index++) |
283 | bt[frame_index] = (void *) 0; | |
284 | #else | |
285 | #error arch | |
91447636 | 286 | #endif |
91447636 A |
287 | return frame; |
288 | } |