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