xref: /xnu-8019.80.24/bsd/sys/kdebug_common.h (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
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 #pragma pack(0)
107 struct kd_bufinfo {
108 	union  kds_ptr kd_list_head;
109 	union  kds_ptr kd_list_tail;
110 	bool kd_lostevents;
111 	uint32_t _pad;
112 	uint64_t kd_prev_timebase;
113 	uint32_t num_bufs;
114 	bool continuous_timestamps;
115 } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE)));
116 
117 
118 struct kd_iop;
119 struct kd_ctrl_page_t {
120 	union kds_ptr kds_free_list;
121 	uint32_t enabled:1,
122 	    mode:3,
123 	    _pad0:28;
124 	int      kds_inuse_count;
125 	uint32_t kdebug_events_per_storage_unit;
126 	uint32_t kdebug_min_storage_units_per_cpu;
127 	uint32_t kdebug_kdcopybuf_count;
128 	uint32_t kdebug_kdcopybuf_size;
129 	uint32_t kdebug_cpus;
130 	uint32_t kdebug_flags;
131 	uint32_t kdebug_slowcheck;
132 	uint64_t oldest_time;
133 
134 	lck_spin_t  kds_spin_lock;
135 
136 	/*
137 	 * The number of kd_bufinfo structs allocated may not match the current
138 	 * number of active cpus. We capture the iops list head at initialization
139 	 * which we could use to calculate the number of cpus we allocated data for,
140 	 * unless it happens to be null. To avoid that case, we explicitly also
141 	 * capture a cpu count.
142 	 */
143 	struct kd_iop* kdebug_iops;
144 };
145 
146 struct kd_data_page_t {
147 	int nkdbufs;
148 	int n_storage_units;
149 	int n_storage_threshold;
150 	uint32_t n_storage_buffer;
151 	struct kd_bufinfo *kdbip;
152 	struct kd_storage_buffers *kd_bufs;
153 	kd_buf *kdcopybuf;
154 };
155 
156 struct kd_record {
157 	int32_t  cpu;
158 	uint32_t debugid;
159 	int64_t timestamp;
160 	kd_buf_argtype arg1;
161 	kd_buf_argtype arg2;
162 	kd_buf_argtype arg3;
163 	kd_buf_argtype arg4;
164 	kd_buf_argtype arg5;
165 };
166 
167 #define POINTER_FROM_KDS_PTR(kd_bufs, x) (&kd_bufs[x.buffer_index].kdsb_addr[x.offset])
168 
169 extern bool kdbg_continuous_time;
170 extern int kdbg_debug;
171 
172 uint32_t kdbg_cpu_count(bool);
173 
174 void kdebug_lck_init(void);
175 int _storage_lock(struct kd_ctrl_page_t *kd_ctrl_page);
176 void _storage_unlock(struct kd_ctrl_page_t *kd_ctrl_page, int intrs_en);
177 
178 void
179 enable_wrap(struct kd_ctrl_page_t *kd_ctrl_page, uint32_t old_slowcheck);
180 
181 bool
182 disable_wrap(struct kd_ctrl_page_t *kd_ctrl_page, uint32_t *old_slowcheck, uint32_t *old_flags);
183 
184 int
185 create_buffers(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page, vm_tag_t tag);
186 
187 void
188 delete_buffers(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page);
189 
190 bool
191 allocate_storage_unit(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page, int cpu);
192 
193 void
194 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);
195 
196 void
197 kernel_debug_write(struct kd_ctrl_page_t *kd_ctrl_page,
198     struct kd_data_page_t *kd_data_page,
199     struct kd_record kd_rec);
200 
201 int
202 kernel_debug_read(struct kd_ctrl_page_t *kd_ctrl_page, struct kd_data_page_t *kd_data_page, user_addr_t buffer,
203     size_t *number, vnode_t vp, vfs_context_t ctx, uint32_t file_version);
204 
205 
206 extern int     RAW_file_written;
207 #define RAW_FLUSH_SIZE  (2 * 1024 * 1024)
208 
209 void commpage_update_kdebug_state(void); /* XXX sign */
210 
211 #endif /* BSD_SYS_KDEBUG_COMMON_H */
212