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