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