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