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