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