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