1*27b03b36SApple OSS Distributions /* 2*27b03b36SApple OSS Distributions * Copyright (c) 2000-2020 Apple Inc. All rights reserved. 3*27b03b36SApple OSS Distributions * 4*27b03b36SApple OSS Distributions * @Apple_LICENSE_HEADER_START@ 5*27b03b36SApple OSS Distributions * 6*27b03b36SApple OSS Distributions * The contents of this file constitute Original Code as defined in and 7*27b03b36SApple OSS Distributions * are subject to the Apple Public Source License Version 1.1 (the 8*27b03b36SApple OSS Distributions * "License"). You may not use this file except in compliance with the 9*27b03b36SApple OSS Distributions * License. Please obtain a copy of the License at 10*27b03b36SApple OSS Distributions * http://www.apple.com/publicsource and read it before using this file. 11*27b03b36SApple OSS Distributions * 12*27b03b36SApple OSS Distributions * This Original Code and all software distributed under the License are 13*27b03b36SApple OSS Distributions * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 14*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 15*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 16*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the 17*27b03b36SApple OSS Distributions * License for the specific language governing rights and limitations 18*27b03b36SApple OSS Distributions * under the License. 19*27b03b36SApple OSS Distributions * 20*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 21*27b03b36SApple OSS Distributions */ 22*27b03b36SApple OSS Distributions 23*27b03b36SApple OSS Distributions #include <sys/kdebug_private.h> 24*27b03b36SApple OSS Distributions #include <sys/param.h> 25*27b03b36SApple OSS Distributions #include <kern/assert.h> 26*27b03b36SApple OSS Distributions #include <mach/clock_types.h> 27*27b03b36SApple OSS Distributions #include <mach/mach_types.h> 28*27b03b36SApple OSS Distributions #include <sys/mcache.h> 29*27b03b36SApple OSS Distributions #include <vm/vm_kern.h> 30*27b03b36SApple OSS Distributions #include <kern/cpu_number.h> 31*27b03b36SApple OSS Distributions #include <kern/cpu_data.h> 32*27b03b36SApple OSS Distributions #include <mach/mach_time.h> 33*27b03b36SApple OSS Distributions #include <kern/clock.h> 34*27b03b36SApple OSS Distributions #include <kern/thread.h> 35*27b03b36SApple OSS Distributions #include <sys/systm.h> 36*27b03b36SApple OSS Distributions #include <machine/machine_routines.h> 37*27b03b36SApple OSS Distributions #include <kperf/kperf.h> 38*27b03b36SApple OSS Distributions #include <mach/mach_host.h> /* for host_info() */ 39*27b03b36SApple OSS Distributions 40*27b03b36SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__) 41*27b03b36SApple OSS Distributions #include <i386/rtclock_protos.h> 42*27b03b36SApple OSS Distributions #include <i386/mp.h> 43*27b03b36SApple OSS Distributions #include <i386/machine_routines.h> 44*27b03b36SApple OSS Distributions #include <i386/tsc.h> 45*27b03b36SApple OSS Distributions #endif 46*27b03b36SApple OSS Distributions 47*27b03b36SApple OSS Distributions 48*27b03b36SApple OSS Distributions #ifndef BSD_SYS_KDEBUG_COMMON_H 49*27b03b36SApple OSS Distributions #define BSD_SYS_KDEBUG_COMMON_H 50*27b03b36SApple OSS Distributions 51*27b03b36SApple OSS Distributions #define TRIAGE_EVENTS_PER_STORAGE_UNIT 128 52*27b03b36SApple OSS Distributions #define TRIAGE_MIN_STORAGE_UNITS_PER_CPU 1 53*27b03b36SApple OSS Distributions 54*27b03b36SApple OSS Distributions #define TRACE_EVENTS_PER_STORAGE_UNIT 2048 55*27b03b36SApple OSS Distributions #define TRACE_MIN_STORAGE_UNITS_PER_CPU 4 56*27b03b36SApple OSS Distributions 57*27b03b36SApple OSS Distributions #define SLOW_NOLOG 0x01 58*27b03b36SApple OSS Distributions #define SLOW_CHECKS 0x02 59*27b03b36SApple OSS Distributions 60*27b03b36SApple OSS Distributions extern lck_grp_t kdebug_lck_grp; 61*27b03b36SApple OSS Distributions 62*27b03b36SApple OSS Distributions struct kd_buf; 63*27b03b36SApple OSS Distributions union kds_ptr { 64*27b03b36SApple OSS Distributions struct { 65*27b03b36SApple OSS Distributions uint32_t buffer_index:21; 66*27b03b36SApple OSS Distributions uint16_t offset:11; 67*27b03b36SApple OSS Distributions }; 68*27b03b36SApple OSS Distributions uint32_t raw; 69*27b03b36SApple OSS Distributions }; 70*27b03b36SApple OSS Distributions 71*27b03b36SApple OSS Distributions /* 72*27b03b36SApple OSS Distributions * Ensure that both LP32 and LP64 variants of arm64 use the same kd_buf 73*27b03b36SApple OSS Distributions * structure. 74*27b03b36SApple OSS Distributions */ 75*27b03b36SApple OSS Distributions #if defined(__arm64__) 76*27b03b36SApple OSS Distributions typedef uint64_t kd_buf_argtype; 77*27b03b36SApple OSS Distributions #else 78*27b03b36SApple OSS Distributions typedef uintptr_t kd_buf_argtype; 79*27b03b36SApple OSS Distributions #endif 80*27b03b36SApple OSS Distributions 81*27b03b36SApple OSS Distributions struct kd_storage { 82*27b03b36SApple OSS Distributions union kds_ptr kds_next; 83*27b03b36SApple OSS Distributions uint32_t kds_bufindx; 84*27b03b36SApple OSS Distributions uint32_t kds_bufcnt; 85*27b03b36SApple OSS Distributions uint32_t kds_readlast; 86*27b03b36SApple OSS Distributions uint32_t kds_lostevents:1, 87*27b03b36SApple OSS Distributions kds_mode:1, 88*27b03b36SApple OSS Distributions unused:30; 89*27b03b36SApple OSS Distributions uint64_t kds_timestamp; 90*27b03b36SApple OSS Distributions 91*27b03b36SApple OSS Distributions kd_buf kds_records[TRACE_EVENTS_PER_STORAGE_UNIT]; 92*27b03b36SApple OSS Distributions }; 93*27b03b36SApple OSS Distributions 94*27b03b36SApple OSS Distributions #define MAX_BUFFER_SIZE (1024 * 1024 * 128) 95*27b03b36SApple OSS Distributions #define N_STORAGE_UNITS_PER_BUFFER (MAX_BUFFER_SIZE / sizeof(struct kd_storage)) 96*27b03b36SApple OSS Distributions static_assert(N_STORAGE_UNITS_PER_BUFFER <= 0x7ff, 97*27b03b36SApple OSS Distributions "shoudn't overflow kds_ptr.offset"); 98*27b03b36SApple OSS Distributions 99*27b03b36SApple OSS Distributions struct kd_storage_buffers { 100*27b03b36SApple OSS Distributions struct kd_storage *kdsb_addr; 101*27b03b36SApple OSS Distributions uint32_t kdsb_size; 102*27b03b36SApple OSS Distributions }; 103*27b03b36SApple OSS Distributions 104*27b03b36SApple OSS Distributions #define KDS_PTR_NULL 0xffffffff 105*27b03b36SApple OSS Distributions 106*27b03b36SApple OSS Distributions struct kd_bufinfo { 107*27b03b36SApple OSS Distributions union kds_ptr kd_list_head; 108*27b03b36SApple OSS Distributions union kds_ptr kd_list_tail; 109*27b03b36SApple OSS Distributions bool kd_lostevents; 110*27b03b36SApple OSS Distributions uint32_t _pad; 111*27b03b36SApple OSS Distributions uint64_t kd_prev_timebase; 112*27b03b36SApple OSS Distributions uint32_t num_bufs; 113*27b03b36SApple OSS Distributions bool continuous_timestamps; 114*27b03b36SApple OSS Distributions } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE))) __attribute__((packed)); 115*27b03b36SApple OSS Distributions 116*27b03b36SApple OSS Distributions 117*27b03b36SApple OSS Distributions struct kd_iop; 118*27b03b36SApple OSS Distributions struct kd_ctrl_page_t { 119*27b03b36SApple OSS Distributions union kds_ptr kds_free_list; 120*27b03b36SApple OSS Distributions uint32_t enabled:1, 121*27b03b36SApple OSS Distributions mode:3, 122*27b03b36SApple OSS Distributions _pad0:28; 123*27b03b36SApple OSS Distributions int kds_inuse_count; 124*27b03b36SApple OSS Distributions uint32_t kdebug_events_per_storage_unit; 125*27b03b36SApple OSS Distributions uint32_t kdebug_min_storage_units_per_cpu; 126*27b03b36SApple OSS Distributions uint32_t kdebug_kdcopybuf_count; 127*27b03b36SApple OSS Distributions uint32_t kdebug_kdcopybuf_size; 128*27b03b36SApple OSS Distributions uint32_t kdebug_cpus; 129*27b03b36SApple OSS Distributions uint32_t kdebug_flags; 130*27b03b36SApple OSS Distributions uint32_t kdebug_slowcheck; 131*27b03b36SApple OSS Distributions uint64_t oldest_time; 132*27b03b36SApple OSS Distributions 133*27b03b36SApple OSS Distributions lck_spin_t kds_spin_lock; 134*27b03b36SApple OSS Distributions 135*27b03b36SApple OSS Distributions /* 136*27b03b36SApple OSS Distributions * The number of kd_bufinfo structs allocated may not match the current 137*27b03b36SApple OSS Distributions * number of active cpus. We capture the iops list head at initialization 138*27b03b36SApple OSS Distributions * which we could use to calculate the number of cpus we allocated data for, 139*27b03b36SApple OSS Distributions * unless it happens to be null. To avoid that case, we explicitly also 140*27b03b36SApple OSS Distributions * capture a cpu count. 141*27b03b36SApple OSS Distributions */ 142*27b03b36SApple OSS Distributions struct kd_iop* kdebug_iops; 143*27b03b36SApple OSS Distributions 144*27b03b36SApple OSS Distributions kd_event_matcher disable_event_match; 145*27b03b36SApple OSS Distributions kd_event_matcher disable_event_mask; 146*27b03b36SApple OSS Distributions }; 147*27b03b36SApple OSS Distributions 148*27b03b36SApple OSS Distributions struct kd_data_page_t { 149*27b03b36SApple OSS Distributions int nkdbufs; 150*27b03b36SApple OSS Distributions int n_storage_units; 151*27b03b36SApple OSS Distributions int n_storage_threshold; 152*27b03b36SApple OSS Distributions uint32_t n_storage_buffer; 153*27b03b36SApple OSS Distributions struct kd_bufinfo *kdbip; 154*27b03b36SApple OSS Distributions struct kd_storage_buffers *kd_bufs; 155*27b03b36SApple OSS Distributions kd_buf *kdcopybuf; 156*27b03b36SApple OSS Distributions }; 157*27b03b36SApple OSS Distributions 158*27b03b36SApple OSS Distributions struct kd_record { 159*27b03b36SApple OSS Distributions int32_t cpu; 160*27b03b36SApple OSS Distributions uint32_t debugid; 161*27b03b36SApple OSS Distributions int64_t timestamp; 162*27b03b36SApple OSS Distributions kd_buf_argtype arg1; 163*27b03b36SApple OSS Distributions kd_buf_argtype arg2; 164*27b03b36SApple OSS Distributions kd_buf_argtype arg3; 165*27b03b36SApple OSS Distributions kd_buf_argtype arg4; 166*27b03b36SApple OSS Distributions kd_buf_argtype arg5; 167*27b03b36SApple OSS Distributions } __attribute__((packed)); 168*27b03b36SApple OSS Distributions 169*27b03b36SApple OSS Distributions #define POINTER_FROM_KDS_PTR(kd_bufs, x) (&kd_bufs[x.buffer_index].kdsb_addr[x.offset]) 170*27b03b36SApple OSS Distributions 171*27b03b36SApple OSS Distributions extern bool kdbg_continuous_time; 172*27b03b36SApple OSS Distributions extern int kdbg_debug; 173*27b03b36SApple OSS Distributions 174*27b03b36SApple OSS Distributions uint32_t kdbg_cpu_count(bool); 175*27b03b36SApple OSS Distributions 176*27b03b36SApple OSS Distributions void kdebug_lck_init(void); 177*27b03b36SApple OSS Distributions int kdebug_storage_lock(struct kd_ctrl_page_t *kd_ctrl_page); 178*27b03b36SApple OSS Distributions void kdebug_storage_unlock(struct kd_ctrl_page_t *kd_ctrl_page, int intrs_en); 179*27b03b36SApple OSS Distributions 180*27b03b36SApple OSS Distributions void 181*27b03b36SApple OSS Distributions enable_wrap(struct kd_ctrl_page_t *kd_ctrl_page, uint32_t old_slowcheck); 182*27b03b36SApple OSS Distributions 183*27b03b36SApple OSS Distributions bool 184*27b03b36SApple OSS Distributions disable_wrap(struct kd_ctrl_page_t *kd_ctrl_page, uint32_t *old_slowcheck, uint32_t *old_flags); 185*27b03b36SApple OSS Distributions 186*27b03b36SApple OSS Distributions int 187*27b03b36SApple OSS Distributions create_buffers(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page, vm_tag_t tag); 188*27b03b36SApple OSS Distributions 189*27b03b36SApple OSS Distributions void 190*27b03b36SApple OSS Distributions delete_buffers(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page); 191*27b03b36SApple OSS Distributions 192*27b03b36SApple OSS Distributions bool 193*27b03b36SApple OSS Distributions allocate_storage_unit(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page, int cpu); 194*27b03b36SApple OSS Distributions 195*27b03b36SApple OSS Distributions void 196*27b03b36SApple OSS Distributions release_storage_unit(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page, int cpu, uint32_t kdsp_raw); 197*27b03b36SApple OSS Distributions 198*27b03b36SApple OSS Distributions void 199*27b03b36SApple OSS Distributions kernel_debug_write(struct kd_ctrl_page_t *kd_ctrl_page, 200*27b03b36SApple OSS Distributions struct kd_data_page_t *kd_data_page, 201*27b03b36SApple OSS Distributions struct kd_record kd_rec); 202*27b03b36SApple OSS Distributions 203*27b03b36SApple OSS Distributions int 204*27b03b36SApple OSS Distributions kernel_debug_read(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page, user_addr_t buffer, 205*27b03b36SApple OSS Distributions size_t *number, vnode_t vp, vfs_context_t ctx, uint32_t file_version); 206*27b03b36SApple OSS Distributions 207*27b03b36SApple OSS Distributions 208*27b03b36SApple OSS Distributions extern int RAW_file_written; 209*27b03b36SApple OSS Distributions #define RAW_FLUSH_SIZE (2 * 1024 * 1024) 210*27b03b36SApple OSS Distributions 211*27b03b36SApple OSS Distributions void commpage_update_kdebug_state(void); /* XXX sign */ 212*27b03b36SApple OSS Distributions 213*27b03b36SApple OSS Distributions #endif /* BSD_SYS_KDEBUG_COMMON_H */ 214