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