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