xref: /xnu-10002.81.5/libkern/gen/OSDebug.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2005-2012 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions // NOTE:  This file is only c++ so I can get static initialisers going
30*5e3eaea3SApple OSS Distributions #include <libkern/OSDebug.h>
31*5e3eaea3SApple OSS Distributions #include <IOKit/IOLib.h>
32*5e3eaea3SApple OSS Distributions 
33*5e3eaea3SApple OSS Distributions #include <sys/cdefs.h>
34*5e3eaea3SApple OSS Distributions 
35*5e3eaea3SApple OSS Distributions #include <stdarg.h>
36*5e3eaea3SApple OSS Distributions #include <mach/mach_types.h>
37*5e3eaea3SApple OSS Distributions #include <mach/kmod.h>
38*5e3eaea3SApple OSS Distributions #include <kern/locks.h>
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions #include <libkern/libkern.h>    // From bsd's libkern directory
41*5e3eaea3SApple OSS Distributions #include <mach/vm_param.h>
42*5e3eaea3SApple OSS Distributions 
43*5e3eaea3SApple OSS Distributions #include <sys/kdebug.h>
44*5e3eaea3SApple OSS Distributions #include <kern/thread.h>
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions #if defined(HAS_APPLE_PAC)
47*5e3eaea3SApple OSS Distributions #include <ptrauth.h>
48*5e3eaea3SApple OSS Distributions #endif
49*5e3eaea3SApple OSS Distributions 
50*5e3eaea3SApple OSS Distributions extern int etext;
51*5e3eaea3SApple OSS Distributions __BEGIN_DECLS
52*5e3eaea3SApple OSS Distributions // From osmfk/kern/thread.h but considered to be private
53*5e3eaea3SApple OSS Distributions extern vm_offset_t min_valid_stack_address(void);
54*5e3eaea3SApple OSS Distributions extern vm_offset_t max_valid_stack_address(void);
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions // From osfmk/kern/printf.c
57*5e3eaea3SApple OSS Distributions extern boolean_t doprnt_hide_pointers;
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions // From osfmk/kmod.c
60*5e3eaea3SApple OSS Distributions extern void kmod_dump_log(vm_offset_t *addr, unsigned int cnt, boolean_t doUnslide);
61*5e3eaea3SApple OSS Distributions 
62*5e3eaea3SApple OSS Distributions extern addr64_t kvtophys(vm_offset_t va);
63*5e3eaea3SApple OSS Distributions #if __arm__
64*5e3eaea3SApple OSS Distributions extern int copyinframe(vm_address_t fp, char *frame);
65*5e3eaea3SApple OSS Distributions #elif defined(__arm64__)
66*5e3eaea3SApple OSS Distributions extern int copyinframe(vm_address_t fp, char *frame, boolean_t is64bit);
67*5e3eaea3SApple OSS Distributions #endif
68*5e3eaea3SApple OSS Distributions 
69*5e3eaea3SApple OSS Distributions __END_DECLS
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions extern lck_grp_t * IOLockGroup;
72*5e3eaea3SApple OSS Distributions 
73*5e3eaea3SApple OSS Distributions static lck_mtx_t *sOSReportLock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
74*5e3eaea3SApple OSS Distributions 
75*5e3eaea3SApple OSS Distributions /* Report a message with a 4 entry backtrace - very slow */
76*5e3eaea3SApple OSS Distributions void
OSReportWithBacktrace(const char * str,...)77*5e3eaea3SApple OSS Distributions OSReportWithBacktrace(const char *str, ...)
78*5e3eaea3SApple OSS Distributions {
79*5e3eaea3SApple OSS Distributions 	char buf[128];
80*5e3eaea3SApple OSS Distributions 	void *bt[9] = {};
81*5e3eaea3SApple OSS Distributions 	const unsigned cnt = sizeof(bt) / sizeof(bt[0]);
82*5e3eaea3SApple OSS Distributions 	va_list listp;
83*5e3eaea3SApple OSS Distributions 
84*5e3eaea3SApple OSS Distributions 	// Ignore the our and our callers stackframes, skipping frames 0 & 1
85*5e3eaea3SApple OSS Distributions 	(void) OSBacktrace(bt, cnt);
86*5e3eaea3SApple OSS Distributions 
87*5e3eaea3SApple OSS Distributions 	va_start(listp, str);
88*5e3eaea3SApple OSS Distributions 	vsnprintf(buf, sizeof(buf), str, listp);
89*5e3eaea3SApple OSS Distributions 	va_end(listp);
90*5e3eaea3SApple OSS Distributions 
91*5e3eaea3SApple OSS Distributions 	lck_mtx_lock(sOSReportLock);
92*5e3eaea3SApple OSS Distributions 	{
93*5e3eaea3SApple OSS Distributions 		boolean_t old_doprnt_hide_pointers = doprnt_hide_pointers;
94*5e3eaea3SApple OSS Distributions 		doprnt_hide_pointers = FALSE;
95*5e3eaea3SApple OSS Distributions 		printf("%s\nBacktrace 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", buf,
96*5e3eaea3SApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[2]), (unsigned long) VM_KERNEL_UNSLIDE(bt[3]),
97*5e3eaea3SApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[4]), (unsigned long) VM_KERNEL_UNSLIDE(bt[5]),
98*5e3eaea3SApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[6]), (unsigned long) VM_KERNEL_UNSLIDE(bt[7]),
99*5e3eaea3SApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[8]));
100*5e3eaea3SApple OSS Distributions 		kmod_dump_log((vm_offset_t *) &bt[2], cnt - 2, TRUE);
101*5e3eaea3SApple OSS Distributions 		doprnt_hide_pointers = old_doprnt_hide_pointers;
102*5e3eaea3SApple OSS Distributions 	}
103*5e3eaea3SApple OSS Distributions 	lck_mtx_unlock(sOSReportLock);
104*5e3eaea3SApple OSS Distributions }
105*5e3eaea3SApple OSS Distributions 
106*5e3eaea3SApple OSS Distributions static vm_offset_t minstackaddr = min_valid_stack_address();
107*5e3eaea3SApple OSS Distributions static vm_offset_t maxstackaddr = max_valid_stack_address();
108*5e3eaea3SApple OSS Distributions 
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions #if __x86_64__
111*5e3eaea3SApple OSS Distributions #define x86_64_RETURN_OFFSET 8
112*5e3eaea3SApple OSS Distributions static unsigned int
x86_64_validate_raddr(vm_offset_t raddr)113*5e3eaea3SApple OSS Distributions x86_64_validate_raddr(vm_offset_t raddr)
114*5e3eaea3SApple OSS Distributions {
115*5e3eaea3SApple OSS Distributions 	return (raddr > VM_MIN_KERNEL_AND_KEXT_ADDRESS) &&
116*5e3eaea3SApple OSS Distributions 	       (raddr < VM_MAX_KERNEL_ADDRESS);
117*5e3eaea3SApple OSS Distributions }
118*5e3eaea3SApple OSS Distributions static unsigned int
x86_64_validate_stackptr(vm_offset_t stackptr)119*5e3eaea3SApple OSS Distributions x86_64_validate_stackptr(vm_offset_t stackptr)
120*5e3eaea3SApple OSS Distributions {
121*5e3eaea3SApple OSS Distributions 	/* Existence and alignment check
122*5e3eaea3SApple OSS Distributions 	 */
123*5e3eaea3SApple OSS Distributions 	if (!stackptr || (stackptr & 0x7) || !x86_64_validate_raddr(stackptr)) {
124*5e3eaea3SApple OSS Distributions 		return 0;
125*5e3eaea3SApple OSS Distributions 	}
126*5e3eaea3SApple OSS Distributions 
127*5e3eaea3SApple OSS Distributions 	/* Is a virtual->physical translation present?
128*5e3eaea3SApple OSS Distributions 	 */
129*5e3eaea3SApple OSS Distributions 	if (!kvtophys(stackptr)) {
130*5e3eaea3SApple OSS Distributions 		return 0;
131*5e3eaea3SApple OSS Distributions 	}
132*5e3eaea3SApple OSS Distributions 
133*5e3eaea3SApple OSS Distributions 	/* Check if the return address lies on the same page;
134*5e3eaea3SApple OSS Distributions 	 * If not, verify that a translation exists.
135*5e3eaea3SApple OSS Distributions 	 */
136*5e3eaea3SApple OSS Distributions 	if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < x86_64_RETURN_OFFSET) &&
137*5e3eaea3SApple OSS Distributions 	    !kvtophys(stackptr + x86_64_RETURN_OFFSET)) {
138*5e3eaea3SApple OSS Distributions 		return 0;
139*5e3eaea3SApple OSS Distributions 	}
140*5e3eaea3SApple OSS Distributions 	return 1;
141*5e3eaea3SApple OSS Distributions }
142*5e3eaea3SApple OSS Distributions #endif
143*5e3eaea3SApple OSS Distributions 
144*5e3eaea3SApple OSS Distributions void
OSPrintBacktrace(void)145*5e3eaea3SApple OSS Distributions OSPrintBacktrace(void)
146*5e3eaea3SApple OSS Distributions {
147*5e3eaea3SApple OSS Distributions 	void * btbuf[20];
148*5e3eaea3SApple OSS Distributions 	int tmp = OSBacktrace(btbuf, 20);
149*5e3eaea3SApple OSS Distributions 	int i;
150*5e3eaea3SApple OSS Distributions 	for (i = 0; i < tmp; i++) {
151*5e3eaea3SApple OSS Distributions 		kprintf("bt[%.2d] = %p\n", i, btbuf[i]);
152*5e3eaea3SApple OSS Distributions 	}
153*5e3eaea3SApple OSS Distributions }
154*5e3eaea3SApple OSS Distributions 
155*5e3eaea3SApple OSS Distributions unsigned
OSBacktrace(void ** bt,unsigned maxAddrs)156*5e3eaea3SApple OSS Distributions OSBacktrace(void **bt, unsigned maxAddrs)
157*5e3eaea3SApple OSS Distributions {
158*5e3eaea3SApple OSS Distributions 	unsigned frame;
159*5e3eaea3SApple OSS Distributions 	if (!current_thread()) {
160*5e3eaea3SApple OSS Distributions 		return 0;
161*5e3eaea3SApple OSS Distributions 	}
162*5e3eaea3SApple OSS Distributions 
163*5e3eaea3SApple OSS Distributions #if   __x86_64__
164*5e3eaea3SApple OSS Distributions #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1)
165*5e3eaea3SApple OSS Distributions 	vm_offset_t stackptr, stackptr_prev, raddr;
166*5e3eaea3SApple OSS Distributions 	unsigned frame_index = 0;
167*5e3eaea3SApple OSS Distributions /* Obtain current frame pointer */
168*5e3eaea3SApple OSS Distributions 
169*5e3eaea3SApple OSS Distributions 	__asm__ volatile ("movq %%rbp, %0" : "=m" (stackptr));
170*5e3eaea3SApple OSS Distributions 
171*5e3eaea3SApple OSS Distributions 	if (!x86_64_validate_stackptr(stackptr)) {
172*5e3eaea3SApple OSS Distributions 		goto pad;
173*5e3eaea3SApple OSS Distributions 	}
174*5e3eaea3SApple OSS Distributions 
175*5e3eaea3SApple OSS Distributions 	raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET));
176*5e3eaea3SApple OSS Distributions 
177*5e3eaea3SApple OSS Distributions 	if (!x86_64_validate_raddr(raddr)) {
178*5e3eaea3SApple OSS Distributions 		goto pad;
179*5e3eaea3SApple OSS Distributions 	}
180*5e3eaea3SApple OSS Distributions 
181*5e3eaea3SApple OSS Distributions 	bt[frame_index++] = (void *) raddr;
182*5e3eaea3SApple OSS Distributions 
183*5e3eaea3SApple OSS Distributions 	for (; frame_index < maxAddrs; frame_index++) {
184*5e3eaea3SApple OSS Distributions 		stackptr_prev = stackptr;
185*5e3eaea3SApple OSS Distributions 		stackptr = *((vm_offset_t *) stackptr_prev);
186*5e3eaea3SApple OSS Distributions 
187*5e3eaea3SApple OSS Distributions 		if (!x86_64_validate_stackptr(stackptr)) {
188*5e3eaea3SApple OSS Distributions 			break;
189*5e3eaea3SApple OSS Distributions 		}
190*5e3eaea3SApple OSS Distributions 		/* Stack grows downwards */
191*5e3eaea3SApple OSS Distributions 		if (stackptr < stackptr_prev) {
192*5e3eaea3SApple OSS Distributions 			break;
193*5e3eaea3SApple OSS Distributions 		}
194*5e3eaea3SApple OSS Distributions 
195*5e3eaea3SApple OSS Distributions 		if ((stackptr - stackptr_prev) > SANE_x86_64_FRAME_SIZE) {
196*5e3eaea3SApple OSS Distributions 			break;
197*5e3eaea3SApple OSS Distributions 		}
198*5e3eaea3SApple OSS Distributions 
199*5e3eaea3SApple OSS Distributions 		raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET));
200*5e3eaea3SApple OSS Distributions 
201*5e3eaea3SApple OSS Distributions 		if (!x86_64_validate_raddr(raddr)) {
202*5e3eaea3SApple OSS Distributions 			break;
203*5e3eaea3SApple OSS Distributions 		}
204*5e3eaea3SApple OSS Distributions 
205*5e3eaea3SApple OSS Distributions 		bt[frame_index] = (void *) raddr;
206*5e3eaea3SApple OSS Distributions 	}
207*5e3eaea3SApple OSS Distributions pad:
208*5e3eaea3SApple OSS Distributions 	frame = frame_index;
209*5e3eaea3SApple OSS Distributions 
210*5e3eaea3SApple OSS Distributions 	for (; frame_index < maxAddrs; frame_index++) {
211*5e3eaea3SApple OSS Distributions 		bt[frame_index] = (void *) NULL;
212*5e3eaea3SApple OSS Distributions 	}
213*5e3eaea3SApple OSS Distributions #elif __arm__ || __arm64__
214*5e3eaea3SApple OSS Distributions 	uint32_t i = 0;
215*5e3eaea3SApple OSS Distributions 	uintptr_t frameb[2];
216*5e3eaea3SApple OSS Distributions 	uintptr_t fp = 0;
217*5e3eaea3SApple OSS Distributions 
218*5e3eaea3SApple OSS Distributions 	// get the current frame pointer for this thread
219*5e3eaea3SApple OSS Distributions #if defined(__arm__)
220*5e3eaea3SApple OSS Distributions #define OSBacktraceFrameAlignOK(x) (((x) & 0x3) == 0)
221*5e3eaea3SApple OSS Distributions 	__asm__ volatile ("mov %0,r7" : "=r" (fp));
222*5e3eaea3SApple OSS Distributions #elif defined(__arm64__)
223*5e3eaea3SApple OSS Distributions #define OSBacktraceFrameAlignOK(x) (((x) & 0xf) == 0)
224*5e3eaea3SApple OSS Distributions 	__asm__ volatile ("mov %0, fp" : "=r" (fp));
225*5e3eaea3SApple OSS Distributions #else
226*5e3eaea3SApple OSS Distributions #error Unknown architecture.
227*5e3eaea3SApple OSS Distributions #endif
228*5e3eaea3SApple OSS Distributions 
229*5e3eaea3SApple OSS Distributions 	// now crawl up the stack recording the link value of each frame
230*5e3eaea3SApple OSS Distributions 	do {
231*5e3eaea3SApple OSS Distributions 		// check bounds
232*5e3eaea3SApple OSS Distributions 		if ((fp == 0) || (!OSBacktraceFrameAlignOK(fp)) || (fp > VM_MAX_KERNEL_ADDRESS) || (fp < VM_MIN_KERNEL_AND_KEXT_ADDRESS)) {
233*5e3eaea3SApple OSS Distributions 			break;
234*5e3eaea3SApple OSS Distributions 		}
235*5e3eaea3SApple OSS Distributions 		// safely read frame
236*5e3eaea3SApple OSS Distributions #ifdef __arm64__
237*5e3eaea3SApple OSS Distributions 		if (copyinframe(fp, (char*)frameb, TRUE) != 0) {
238*5e3eaea3SApple OSS Distributions #else
239*5e3eaea3SApple OSS Distributions 		if (copyinframe(fp, (char*)frameb) != 0) {
240*5e3eaea3SApple OSS Distributions #endif
241*5e3eaea3SApple OSS Distributions 			break;
242*5e3eaea3SApple OSS Distributions 		}
243*5e3eaea3SApple OSS Distributions 
244*5e3eaea3SApple OSS Distributions 		// No need to use copyin as this is always a kernel address, see check above
245*5e3eaea3SApple OSS Distributions #if defined(HAS_APPLE_PAC)
246*5e3eaea3SApple OSS Distributions 		/* return addresses on stack signed by arm64e ABI */
247*5e3eaea3SApple OSS Distributions 		bt[i] = ptrauth_strip((void*)frameb[1], ptrauth_key_return_address); // link register
248*5e3eaea3SApple OSS Distributions #else
249*5e3eaea3SApple OSS Distributions 		bt[i] = (void*)frameb[1]; // link register
250*5e3eaea3SApple OSS Distributions #endif
251*5e3eaea3SApple OSS Distributions 		fp = frameb[0];
252*5e3eaea3SApple OSS Distributions #if defined(HAS_APPLE_PAC)
253*5e3eaea3SApple OSS Distributions 		fp = (uintptr_t)ptrauth_strip((void *)fp, ptrauth_key_frame_pointer);
254*5e3eaea3SApple OSS Distributions #endif
255*5e3eaea3SApple OSS Distributions 	} while (++i < maxAddrs);
256*5e3eaea3SApple OSS Distributions 	frame = i;
257*5e3eaea3SApple OSS Distributions #else
258*5e3eaea3SApple OSS Distributions #error arch
259*5e3eaea3SApple OSS Distributions #endif
260*5e3eaea3SApple OSS Distributions 	return frame;
261*5e3eaea3SApple OSS Distributions }
262