xref: /xnu-11215/libkern/gen/OSDebug.cpp (revision 1031c584)
114e3d835SApple OSS Distributions /*
2186b8fceSApple OSS Distributions  * Copyright (c) 2005-2012 Apple Inc. All rights reserved.
314e3d835SApple OSS Distributions  *
4e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
514e3d835SApple OSS Distributions  *
6e13b1fa5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
1414e3d835SApple OSS Distributions  *
15e13b1fa5SApple OSS Distributions  * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions  *
18e13b1fa5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
2014e3d835SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
2114e3d835SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions  * limitations under the License.
2514e3d835SApple OSS Distributions  *
26e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
2714e3d835SApple OSS Distributions  */
2814e3d835SApple OSS Distributions 
2914e3d835SApple OSS Distributions // NOTE:  This file is only c++ so I can get static initialisers going
3014e3d835SApple OSS Distributions #include <libkern/OSDebug.h>
31855239e5SApple OSS Distributions #include <IOKit/IOLib.h>
3214e3d835SApple OSS Distributions 
3314e3d835SApple OSS Distributions #include <sys/cdefs.h>
3414e3d835SApple OSS Distributions 
3514e3d835SApple OSS Distributions #include <stdarg.h>
3614e3d835SApple OSS Distributions #include <mach/mach_types.h>
3714e3d835SApple OSS Distributions #include <mach/kmod.h>
383ca3bd55SApple OSS Distributions #include <kern/locks.h>
3914e3d835SApple OSS Distributions 
4014e3d835SApple OSS Distributions #include <libkern/libkern.h>    // From bsd's libkern directory
41e13b1fa5SApple OSS Distributions #include <mach/vm_param.h>
4214e3d835SApple OSS Distributions 
43e13b1fa5SApple OSS Distributions #include <sys/kdebug.h>
443ca3bd55SApple OSS Distributions #include <kern/thread.h>
453ca3bd55SApple OSS Distributions 
46a5e72196SApple OSS Distributions #if defined(HAS_APPLE_PAC)
47a5e72196SApple OSS Distributions #include <ptrauth.h>
48a5e72196SApple OSS Distributions #endif
49cc9a6355SApple OSS Distributions 
50e13b1fa5SApple OSS Distributions extern int etext;
5114e3d835SApple OSS Distributions __BEGIN_DECLS
5214e3d835SApple OSS Distributions // From osmfk/kern/thread.h but considered to be private
5314e3d835SApple OSS Distributions extern vm_offset_t min_valid_stack_address(void);
5414e3d835SApple OSS Distributions extern vm_offset_t max_valid_stack_address(void);
5514e3d835SApple OSS Distributions 
560f3703acSApple OSS Distributions // From osfmk/kern/printf.c
570f3703acSApple OSS Distributions extern boolean_t doprnt_hide_pointers;
580f3703acSApple OSS Distributions 
5914e3d835SApple OSS Distributions // From osfmk/kmod.c
60a3bb9fccSApple OSS Distributions extern void kmod_dump_log(vm_offset_t *addr, unsigned int cnt, boolean_t doUnslide);
61e13b1fa5SApple OSS Distributions 
62e13b1fa5SApple OSS Distributions extern addr64_t kvtophys(vm_offset_t va);
6376e12aa3SApple OSS Distributions #if __arm__
6476e12aa3SApple OSS Distributions extern int copyinframe(vm_address_t fp, char *frame);
6576e12aa3SApple OSS Distributions #elif defined(__arm64__)
6676e12aa3SApple OSS Distributions extern int copyinframe(vm_address_t fp, char *frame, boolean_t is64bit);
6776e12aa3SApple OSS Distributions #endif
68e13b1fa5SApple OSS Distributions 
6914e3d835SApple OSS Distributions __END_DECLS
7014e3d835SApple OSS Distributions 
713ca3bd55SApple OSS Distributions extern lck_grp_t * IOLockGroup;
723ca3bd55SApple OSS Distributions 
733ca3bd55SApple OSS Distributions static lck_mtx_t *sOSReportLock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
7414e3d835SApple OSS Distributions 
7514e3d835SApple OSS Distributions /* Report a message with a 4 entry backtrace - very slow */
7614e3d835SApple OSS Distributions void
OSReportWithBacktrace(const char * str,...)7714e3d835SApple OSS Distributions OSReportWithBacktrace(const char *str, ...)
7814e3d835SApple OSS Distributions {
7914e3d835SApple OSS Distributions 	char buf[128];
8076e12aa3SApple OSS Distributions 	void *bt[9] = {};
8114e3d835SApple OSS Distributions 	const unsigned cnt = sizeof(bt) / sizeof(bt[0]);
8214e3d835SApple OSS Distributions 	va_list listp;
8314e3d835SApple OSS Distributions 
8414e3d835SApple OSS Distributions 	// Ignore the our and our callers stackframes, skipping frames 0 & 1
8514e3d835SApple OSS Distributions 	(void) OSBacktrace(bt, cnt);
8614e3d835SApple OSS Distributions 
8714e3d835SApple OSS Distributions 	va_start(listp, str);
8814e3d835SApple OSS Distributions 	vsnprintf(buf, sizeof(buf), str, listp);
8914e3d835SApple OSS Distributions 	va_end(listp);
9014e3d835SApple OSS Distributions 
913ca3bd55SApple OSS Distributions 	lck_mtx_lock(sOSReportLock);
9214e3d835SApple OSS Distributions 	{
930f3703acSApple OSS Distributions 		boolean_t old_doprnt_hide_pointers = doprnt_hide_pointers;
940f3703acSApple OSS Distributions 		doprnt_hide_pointers = FALSE;
95186b8fceSApple OSS Distributions 		printf("%s\nBacktrace 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", buf,
96186b8fceSApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[2]), (unsigned long) VM_KERNEL_UNSLIDE(bt[3]),
97186b8fceSApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[4]), (unsigned long) VM_KERNEL_UNSLIDE(bt[5]),
98186b8fceSApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[6]), (unsigned long) VM_KERNEL_UNSLIDE(bt[7]),
99186b8fceSApple OSS Distributions 		    (unsigned long) VM_KERNEL_UNSLIDE(bt[8]));
100a3bb9fccSApple OSS Distributions 		kmod_dump_log((vm_offset_t *) &bt[2], cnt - 2, TRUE);
1010f3703acSApple OSS Distributions 		doprnt_hide_pointers = old_doprnt_hide_pointers;
10214e3d835SApple OSS Distributions 	}
1033ca3bd55SApple OSS Distributions 	lck_mtx_unlock(sOSReportLock);
10414e3d835SApple OSS Distributions }
10514e3d835SApple OSS Distributions 
10614e3d835SApple OSS Distributions static vm_offset_t minstackaddr = min_valid_stack_address();
10714e3d835SApple OSS Distributions static vm_offset_t maxstackaddr = max_valid_stack_address();
10814e3d835SApple OSS Distributions 
109e13b1fa5SApple OSS Distributions 
1103ca3bd55SApple OSS Distributions #if __x86_64__
1113ca3bd55SApple OSS Distributions #define x86_64_RETURN_OFFSET 8
1123ca3bd55SApple OSS Distributions static unsigned int
x86_64_validate_raddr(vm_offset_t raddr)1133ca3bd55SApple OSS Distributions x86_64_validate_raddr(vm_offset_t raddr)
1143ca3bd55SApple OSS Distributions {
115a5e72196SApple OSS Distributions 	return (raddr > VM_MIN_KERNEL_AND_KEXT_ADDRESS) &&
116a5e72196SApple OSS Distributions 	       (raddr < VM_MAX_KERNEL_ADDRESS);
1173ca3bd55SApple OSS Distributions }
1183ca3bd55SApple OSS Distributions static unsigned int
x86_64_validate_stackptr(vm_offset_t stackptr)1193ca3bd55SApple OSS Distributions x86_64_validate_stackptr(vm_offset_t stackptr)
1203ca3bd55SApple OSS Distributions {
1213ca3bd55SApple OSS Distributions 	/* Existence and alignment check
1223ca3bd55SApple OSS Distributions 	 */
123a5e72196SApple OSS Distributions 	if (!stackptr || (stackptr & 0x7) || !x86_64_validate_raddr(stackptr)) {
1243ca3bd55SApple OSS Distributions 		return 0;
125a5e72196SApple OSS Distributions 	}
1263ca3bd55SApple OSS Distributions 
1273ca3bd55SApple OSS Distributions 	/* Is a virtual->physical translation present?
1283ca3bd55SApple OSS Distributions 	 */
129a5e72196SApple OSS Distributions 	if (!kvtophys(stackptr)) {
1303ca3bd55SApple OSS Distributions 		return 0;
131a5e72196SApple OSS Distributions 	}
1323ca3bd55SApple OSS Distributions 
1333ca3bd55SApple OSS Distributions 	/* Check if the return address lies on the same page;
1343ca3bd55SApple OSS Distributions 	 * If not, verify that a translation exists.
1353ca3bd55SApple OSS Distributions 	 */
1363ca3bd55SApple OSS Distributions 	if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < x86_64_RETURN_OFFSET) &&
137a5e72196SApple OSS Distributions 	    !kvtophys(stackptr + x86_64_RETURN_OFFSET)) {
1383ca3bd55SApple OSS Distributions 		return 0;
139a5e72196SApple OSS Distributions 	}
1403ca3bd55SApple OSS Distributions 	return 1;
1413ca3bd55SApple OSS Distributions }
1423ca3bd55SApple OSS Distributions #endif
1433ca3bd55SApple OSS Distributions 
144855239e5SApple OSS Distributions void
OSPrintBacktrace(void)145855239e5SApple OSS Distributions OSPrintBacktrace(void)
146855239e5SApple OSS Distributions {
147855239e5SApple OSS Distributions 	void * btbuf[20];
148855239e5SApple OSS Distributions 	int tmp = OSBacktrace(btbuf, 20);
149855239e5SApple OSS Distributions 	int i;
150a5e72196SApple OSS Distributions 	for (i = 0; i < tmp; i++) {
151855239e5SApple OSS Distributions 		kprintf("bt[%.2d] = %p\n", i, btbuf[i]);
152855239e5SApple OSS Distributions 	}
153855239e5SApple OSS Distributions }
1543ca3bd55SApple OSS Distributions 
155a5e72196SApple OSS Distributions unsigned
OSBacktrace(void ** bt,unsigned maxAddrs)156a5e72196SApple OSS Distributions OSBacktrace(void **bt, unsigned maxAddrs)
15714e3d835SApple OSS Distributions {
15814e3d835SApple OSS Distributions 	unsigned frame;
159a5e72196SApple OSS Distributions 	if (!current_thread()) {
160a5e72196SApple OSS Distributions 		return 0;
161a5e72196SApple OSS Distributions 	}
16214e3d835SApple OSS Distributions 
163186b8fceSApple OSS Distributions #if   __x86_64__
1643ca3bd55SApple OSS Distributions #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1)
1653ca3bd55SApple OSS Distributions 	vm_offset_t stackptr, stackptr_prev, raddr;
1663ca3bd55SApple OSS Distributions 	unsigned frame_index = 0;
1673ca3bd55SApple OSS Distributions /* Obtain current frame pointer */
168e13b1fa5SApple OSS Distributions 
1693ca3bd55SApple OSS Distributions 	__asm__ volatile ("movq %%rbp, %0" : "=m" (stackptr));
170e13b1fa5SApple OSS Distributions 
171a5e72196SApple OSS Distributions 	if (!x86_64_validate_stackptr(stackptr)) {
1723ca3bd55SApple OSS Distributions 		goto pad;
173a5e72196SApple OSS Distributions 	}
1743ca3bd55SApple OSS Distributions 
1753ca3bd55SApple OSS Distributions 	raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET));
1763ca3bd55SApple OSS Distributions 
177a5e72196SApple OSS Distributions 	if (!x86_64_validate_raddr(raddr)) {
1783ca3bd55SApple OSS Distributions 		goto pad;
179a5e72196SApple OSS Distributions 	}
1803ca3bd55SApple OSS Distributions 
1813ca3bd55SApple OSS Distributions 	bt[frame_index++] = (void *) raddr;
1823ca3bd55SApple OSS Distributions 
1833ca3bd55SApple OSS Distributions 	for (; frame_index < maxAddrs; frame_index++) {
1843ca3bd55SApple OSS Distributions 		stackptr_prev = stackptr;
1853ca3bd55SApple OSS Distributions 		stackptr = *((vm_offset_t *) stackptr_prev);
1863ca3bd55SApple OSS Distributions 
187a5e72196SApple OSS Distributions 		if (!x86_64_validate_stackptr(stackptr)) {
188e13b1fa5SApple OSS Distributions 			break;
189a5e72196SApple OSS Distributions 		}
1903ca3bd55SApple OSS Distributions 		/* Stack grows downwards */
191a5e72196SApple OSS Distributions 		if (stackptr < stackptr_prev) {
19214e3d835SApple OSS Distributions 			break;
193a5e72196SApple OSS Distributions 		}
19414e3d835SApple OSS Distributions 
195a5e72196SApple OSS Distributions 		if ((stackptr - stackptr_prev) > SANE_x86_64_FRAME_SIZE) {
1963ca3bd55SApple OSS Distributions 			break;
197a5e72196SApple OSS Distributions 		}
1983ca3bd55SApple OSS Distributions 
1993ca3bd55SApple OSS Distributions 		raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET));
2003ca3bd55SApple OSS Distributions 
201a5e72196SApple OSS Distributions 		if (!x86_64_validate_raddr(raddr)) {
2023ca3bd55SApple OSS Distributions 			break;
203a5e72196SApple OSS Distributions 		}
2043ca3bd55SApple OSS Distributions 
2053ca3bd55SApple OSS Distributions 		bt[frame_index] = (void *) raddr;
2063ca3bd55SApple OSS Distributions 	}
2073ca3bd55SApple OSS Distributions pad:
2083ca3bd55SApple OSS Distributions 	frame = frame_index;
2093ca3bd55SApple OSS Distributions 
210a5e72196SApple OSS Distributions 	for (; frame_index < maxAddrs; frame_index++) {
211a5e72196SApple OSS Distributions 		bt[frame_index] = (void *) NULL;
212a5e72196SApple OSS Distributions 	}
21376e12aa3SApple OSS Distributions #elif __arm__ || __arm64__
21476e12aa3SApple OSS Distributions 	uint32_t i = 0;
21576e12aa3SApple OSS Distributions 	uintptr_t frameb[2];
21676e12aa3SApple OSS Distributions 	uintptr_t fp = 0;
21776e12aa3SApple OSS Distributions 
21876e12aa3SApple OSS Distributions 	// get the current frame pointer for this thread
21976e12aa3SApple OSS Distributions #if defined(__arm__)
22076e12aa3SApple OSS Distributions #define OSBacktraceFrameAlignOK(x) (((x) & 0x3) == 0)
22176e12aa3SApple OSS Distributions 	__asm__ volatile ("mov %0,r7" : "=r" (fp));
22276e12aa3SApple OSS Distributions #elif defined(__arm64__)
22376e12aa3SApple OSS Distributions #define OSBacktraceFrameAlignOK(x) (((x) & 0xf) == 0)
22476e12aa3SApple OSS Distributions 	__asm__ volatile ("mov %0, fp" : "=r" (fp));
22576e12aa3SApple OSS Distributions #else
22676e12aa3SApple OSS Distributions #error Unknown architecture.
22776e12aa3SApple OSS Distributions #endif
22876e12aa3SApple OSS Distributions 
22976e12aa3SApple OSS Distributions 	// now crawl up the stack recording the link value of each frame
23076e12aa3SApple OSS Distributions 	do {
23176e12aa3SApple OSS Distributions 		// check bounds
23276e12aa3SApple OSS Distributions 		if ((fp == 0) || (!OSBacktraceFrameAlignOK(fp)) || (fp > VM_MAX_KERNEL_ADDRESS) || (fp < VM_MIN_KERNEL_AND_KEXT_ADDRESS)) {
23376e12aa3SApple OSS Distributions 			break;
23476e12aa3SApple OSS Distributions 		}
23576e12aa3SApple OSS Distributions 		// safely read frame
23676e12aa3SApple OSS Distributions #ifdef __arm64__
23776e12aa3SApple OSS Distributions 		if (copyinframe(fp, (char*)frameb, TRUE) != 0) {
23876e12aa3SApple OSS Distributions #else
23976e12aa3SApple OSS Distributions 		if (copyinframe(fp, (char*)frameb) != 0) {
24076e12aa3SApple OSS Distributions #endif
24176e12aa3SApple OSS Distributions 			break;
24276e12aa3SApple OSS Distributions 		}
24376e12aa3SApple OSS Distributions 
24476e12aa3SApple OSS Distributions 		// No need to use copyin as this is always a kernel address, see check above
245a5e72196SApple OSS Distributions #if defined(HAS_APPLE_PAC)
246a5e72196SApple OSS Distributions 		/* return addresses on stack signed by arm64e ABI */
247a5e72196SApple OSS Distributions 		bt[i] = ptrauth_strip((void*)frameb[1], ptrauth_key_return_address); // link register
248a5e72196SApple OSS Distributions #else
24976e12aa3SApple OSS Distributions 		bt[i] = (void*)frameb[1]; // link register
250a5e72196SApple OSS Distributions #endif
25176e12aa3SApple OSS Distributions 		fp = frameb[0];
252*1031c584SApple OSS Distributions #if defined(HAS_APPLE_PAC)
253*1031c584SApple OSS Distributions 		fp = (uintptr_t)ptrauth_strip((void *)fp, ptrauth_key_frame_pointer);
254*1031c584SApple OSS Distributions #endif
25576e12aa3SApple OSS Distributions 	} while (++i < maxAddrs);
25676e12aa3SApple OSS Distributions 	frame = i;
257e13b1fa5SApple OSS Distributions #else
258e13b1fa5SApple OSS Distributions #error arch
25914e3d835SApple OSS Distributions #endif
26014e3d835SApple OSS Distributions 	return frame;
26114e3d835SApple OSS Distributions }
262