1*043036a2SApple OSS Distributions // Copyright (c) 2000-2021 Apple Inc. All rights reserved. 2*043036a2SApple OSS Distributions // 3*043036a2SApple OSS Distributions // @Apple_LICENSE_HEADER_START@ 4*043036a2SApple OSS Distributions // 5*043036a2SApple OSS Distributions // The contents of this file constitute Original Code as defined in and 6*043036a2SApple OSS Distributions // are subject to the Apple Public Source License Version 1.1 (the 7*043036a2SApple OSS Distributions // "License"). You may not use this file except in compliance with the 8*043036a2SApple OSS Distributions // License. Please obtain a copy of the License at 9*043036a2SApple OSS Distributions // http://www.apple.com/publicsource and read it before using this file. 10*043036a2SApple OSS Distributions // 11*043036a2SApple OSS Distributions // This Original Code and all software distributed under the License are 12*043036a2SApple OSS Distributions // distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 13*043036a2SApple OSS Distributions // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 14*043036a2SApple OSS Distributions // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 15*043036a2SApple OSS Distributions // FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the 16*043036a2SApple OSS Distributions // License for the specific language governing rights and limitations 17*043036a2SApple OSS Distributions // under the License. 18*043036a2SApple OSS Distributions // 19*043036a2SApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 20*043036a2SApple OSS Distributions 21*043036a2SApple OSS Distributions #ifndef BSD_SYS_KDEBUG_COMMON_H 22*043036a2SApple OSS Distributions #define BSD_SYS_KDEBUG_COMMON_H 23*043036a2SApple OSS Distributions 24*043036a2SApple OSS Distributions #include <kern/assert.h> 25*043036a2SApple OSS Distributions #include <kern/clock.h> 26*043036a2SApple OSS Distributions #include <kern/cpu_data.h> 27*043036a2SApple OSS Distributions #include <kern/cpu_number.h> 28*043036a2SApple OSS Distributions #include <kern/thread.h> 29*043036a2SApple OSS Distributions #include <kperf/kperf.h> 30*043036a2SApple OSS Distributions #include <mach/clock_types.h> 31*043036a2SApple OSS Distributions #include <mach/mach_host.h> 32*043036a2SApple OSS Distributions #include <mach/mach_time.h> 33*043036a2SApple OSS Distributions #include <mach/mach_types.h> 34*043036a2SApple OSS Distributions #include <machine/machine_routines.h> 35*043036a2SApple OSS Distributions #include <sys/kdebug_private.h> 36*043036a2SApple OSS Distributions #include <sys/mcache.h> 37*043036a2SApple OSS Distributions #include <sys/param.h> 38*043036a2SApple OSS Distributions #include <sys/systm.h> 39*043036a2SApple OSS Distributions #include <vm/vm_kern.h> 40*043036a2SApple OSS Distributions 41*043036a2SApple OSS Distributions #if defined(__x86_64__) 42*043036a2SApple OSS Distributions #include <i386/machine_routines.h> 43*043036a2SApple OSS Distributions #include <i386/mp.h> 44*043036a2SApple OSS Distributions #include <i386/rtclock_protos.h> 45*043036a2SApple OSS Distributions #include <i386/tsc.h> 46*043036a2SApple OSS Distributions #endif // defined(__x86_64__) 47*043036a2SApple OSS Distributions 48*043036a2SApple OSS Distributions #define TRIAGE_EVENTS_PER_STORAGE_UNIT 128 49*043036a2SApple OSS Distributions #define TRIAGE_MIN_STORAGE_UNITS_PER_CPU 2 50*043036a2SApple OSS Distributions 51*043036a2SApple OSS Distributions #define TRACE_EVENTS_PER_STORAGE_UNIT 2048 52*043036a2SApple OSS Distributions #define TRACE_MIN_STORAGE_UNITS_PER_CPU 4 53*043036a2SApple OSS Distributions 54*043036a2SApple OSS Distributions // How to filter events. 55*043036a2SApple OSS Distributions __enum_decl(kdebug_emit_filter_t, uint32_t, { 56*043036a2SApple OSS Distributions KDEMIT_DISABLE, 57*043036a2SApple OSS Distributions KDEMIT_ALL, 58*043036a2SApple OSS Distributions KDEMIT_TYPEFILTER, 59*043036a2SApple OSS Distributions KDEMIT_RANGE, 60*043036a2SApple OSS Distributions KDEMIT_EXACT, 61*043036a2SApple OSS Distributions }); 62*043036a2SApple OSS Distributions 63*043036a2SApple OSS Distributions extern lck_grp_t kdebug_lck_grp; 64*043036a2SApple OSS Distributions 65*043036a2SApple OSS Distributions union kds_ptr { 66*043036a2SApple OSS Distributions struct { 67*043036a2SApple OSS Distributions uint32_t buffer_index:21; 68*043036a2SApple OSS Distributions uint16_t offset:11; 69*043036a2SApple OSS Distributions }; 70*043036a2SApple OSS Distributions uint32_t raw; 71*043036a2SApple OSS Distributions }; 72*043036a2SApple OSS Distributions 73*043036a2SApple OSS Distributions struct kd_storage { 74*043036a2SApple OSS Distributions union kds_ptr kds_next; 75*043036a2SApple OSS Distributions uint32_t kds_bufindx; 76*043036a2SApple OSS Distributions uint32_t kds_bufcnt; 77*043036a2SApple OSS Distributions uint32_t kds_readlast; 78*043036a2SApple OSS Distributions uint32_t kds_lostevents:1; 79*043036a2SApple OSS Distributions uint32_t unused:31; 80*043036a2SApple OSS Distributions uint64_t kds_timestamp; 81*043036a2SApple OSS Distributions 82*043036a2SApple OSS Distributions kd_buf kds_records[TRACE_EVENTS_PER_STORAGE_UNIT]; 83*043036a2SApple OSS Distributions }; 84*043036a2SApple OSS Distributions 85*043036a2SApple OSS Distributions #define MAX_BUFFER_SIZE (1024 * 1024 * 128) 86*043036a2SApple OSS Distributions #define N_STORAGE_UNITS_PER_BUFFER (MAX_BUFFER_SIZE / sizeof(struct kd_storage)) 87*043036a2SApple OSS Distributions static_assert(N_STORAGE_UNITS_PER_BUFFER <= 0x7ff, 88*043036a2SApple OSS Distributions "shoudn't overflow kds_ptr.offset"); 89*043036a2SApple OSS Distributions 90*043036a2SApple OSS Distributions struct kd_region { 91*043036a2SApple OSS Distributions struct kd_storage *kdr_addr; 92*043036a2SApple OSS Distributions uint32_t kdr_size; 93*043036a2SApple OSS Distributions }; 94*043036a2SApple OSS Distributions 95*043036a2SApple OSS Distributions #define KDS_PTR_NULL 0xffffffff 96*043036a2SApple OSS Distributions 97*043036a2SApple OSS Distributions struct kd_bufinfo { 98*043036a2SApple OSS Distributions union kds_ptr kd_list_head; 99*043036a2SApple OSS Distributions union kds_ptr kd_list_tail; 100*043036a2SApple OSS Distributions bool kd_lostevents; 101*043036a2SApple OSS Distributions uint32_t _pad; 102*043036a2SApple OSS Distributions uint64_t kd_prev_timebase; 103*043036a2SApple OSS Distributions uint32_t num_bufs; 104*043036a2SApple OSS Distributions uint64_t latest_past_event_timestamp; 105*043036a2SApple OSS Distributions bool continuous_timestamps; 106*043036a2SApple OSS Distributions } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE))) __attribute__((packed)); 107*043036a2SApple OSS Distributions 108*043036a2SApple OSS Distributions struct kd_coproc; 109*043036a2SApple OSS Distributions 110*043036a2SApple OSS Distributions struct kd_control { 111*043036a2SApple OSS Distributions union kds_ptr kds_free_list; 112*043036a2SApple OSS Distributions uint32_t enabled:1, 113*043036a2SApple OSS Distributions mode:3, 114*043036a2SApple OSS Distributions _pad0:28; 115*043036a2SApple OSS Distributions uint32_t kdebug_events_per_storage_unit; 116*043036a2SApple OSS Distributions uint32_t kdebug_min_storage_units_per_cpu; 117*043036a2SApple OSS Distributions uint32_t kdebug_kdcopybuf_count; 118*043036a2SApple OSS Distributions uint32_t kdebug_kdcopybuf_size; 119*043036a2SApple OSS Distributions uint32_t kdebug_cpus; 120*043036a2SApple OSS Distributions uint32_t alloc_cpus; 121*043036a2SApple OSS Distributions uint32_t kdc_flags; 122*043036a2SApple OSS Distributions kdebug_emit_filter_t kdc_emit; 123*043036a2SApple OSS Distributions kdebug_live_flags_t kdc_live_flags; 124*043036a2SApple OSS Distributions uint64_t kdc_oldest_time; 125*043036a2SApple OSS Distributions int kdc_storage_used; 126*043036a2SApple OSS Distributions 127*043036a2SApple OSS Distributions lck_spin_t kdc_storage_lock; 128*043036a2SApple OSS Distributions 129*043036a2SApple OSS Distributions struct kd_coproc *kdc_coprocs; 130*043036a2SApple OSS Distributions 131*043036a2SApple OSS Distributions kd_event_matcher disable_event_match; 132*043036a2SApple OSS Distributions kd_event_matcher disable_event_mask; 133*043036a2SApple OSS Distributions }; 134*043036a2SApple OSS Distributions 135*043036a2SApple OSS Distributions struct kd_buffer { 136*043036a2SApple OSS Distributions int kdb_event_count; 137*043036a2SApple OSS Distributions int kdb_storage_count; 138*043036a2SApple OSS Distributions int kdb_storage_threshold; 139*043036a2SApple OSS Distributions uint32_t kdb_region_count; 140*043036a2SApple OSS Distributions struct kd_bufinfo *kdb_info; 141*043036a2SApple OSS Distributions struct kd_region *kd_bufs; 142*043036a2SApple OSS Distributions kd_buf *kdcopybuf; 143*043036a2SApple OSS Distributions }; 144*043036a2SApple OSS Distributions 145*043036a2SApple OSS Distributions struct kd_record { 146*043036a2SApple OSS Distributions int32_t cpu; 147*043036a2SApple OSS Distributions uint32_t debugid; 148*043036a2SApple OSS Distributions int64_t timestamp; 149*043036a2SApple OSS Distributions kd_buf_argtype arg1; 150*043036a2SApple OSS Distributions kd_buf_argtype arg2; 151*043036a2SApple OSS Distributions kd_buf_argtype arg3; 152*043036a2SApple OSS Distributions kd_buf_argtype arg4; 153*043036a2SApple OSS Distributions kd_buf_argtype arg5; 154*043036a2SApple OSS Distributions } __attribute__((packed)); 155*043036a2SApple OSS Distributions 156*043036a2SApple OSS Distributions #define POINTER_FROM_KDS_PTR(kd_bufs, x) (&kd_bufs[x.buffer_index].kdr_addr[x.offset]) 157*043036a2SApple OSS Distributions 158*043036a2SApple OSS Distributions extern bool kdbg_continuous_time; 159*043036a2SApple OSS Distributions extern int kdbg_debug; 160*043036a2SApple OSS Distributions 161*043036a2SApple OSS Distributions uint32_t kdbg_cpu_count(void); 162*043036a2SApple OSS Distributions 163*043036a2SApple OSS Distributions void kdebug_lck_init(void); 164*043036a2SApple OSS Distributions int kdebug_storage_lock(struct kd_control *ctl); 165*043036a2SApple OSS Distributions void kdebug_storage_unlock(struct kd_control *ctl, int intrs_en); 166*043036a2SApple OSS Distributions 167*043036a2SApple OSS Distributions bool kdebug_storage_alloc( 168*043036a2SApple OSS Distributions struct kd_control *kd_ctrl_page, 169*043036a2SApple OSS Distributions struct kd_buffer *kd_data_page, 170*043036a2SApple OSS Distributions int cpu); 171*043036a2SApple OSS Distributions 172*043036a2SApple OSS Distributions void create_buffers_triage(void); 173*043036a2SApple OSS Distributions 174*043036a2SApple OSS Distributions int create_buffers(struct kd_control *ctl, struct kd_buffer *buf, vm_tag_t tag); 175*043036a2SApple OSS Distributions 176*043036a2SApple OSS Distributions void delete_buffers(struct kd_control *ctl, struct kd_buffer *buf); 177*043036a2SApple OSS Distributions 178*043036a2SApple OSS Distributions void commpage_update_kdebug_state(void); 179*043036a2SApple OSS Distributions 180*043036a2SApple OSS Distributions #endif /* BSD_SYS_KDEBUG_COMMON_H */ 181