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