xref: /xnu-8792.61.2/osfmk/kdp/processor_core.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1*42e22086SApple OSS Distributions /*
2*42e22086SApple OSS Distributions  * Copyright (c) 2017 Apple Computer, Inc. All rights reserved.
3*42e22086SApple OSS Distributions  *
4*42e22086SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*42e22086SApple OSS Distributions  *
6*42e22086SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*42e22086SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*42e22086SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*42e22086SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*42e22086SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*42e22086SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*42e22086SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*42e22086SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*42e22086SApple OSS Distributions  *
15*42e22086SApple OSS Distributions  * Please obtain a copy of the License at
16*42e22086SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*42e22086SApple OSS Distributions  *
18*42e22086SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*42e22086SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*42e22086SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*42e22086SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*42e22086SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*42e22086SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*42e22086SApple OSS Distributions  * limitations under the License.
25*42e22086SApple OSS Distributions  *
26*42e22086SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*42e22086SApple OSS Distributions  */
28*42e22086SApple OSS Distributions #ifndef _PROCESSOR_CORE_H_
29*42e22086SApple OSS Distributions #define _PROCESSOR_CORE_H_
30*42e22086SApple OSS Distributions 
31*42e22086SApple OSS Distributions #include <stdint.h>
32*42e22086SApple OSS Distributions #include <mach/vm_types.h>
33*42e22086SApple OSS Distributions #include <mach/kern_return.h>
34*42e22086SApple OSS Distributions #include <mach/machine.h>
35*42e22086SApple OSS Distributions #include <mach_debug/mach_debug_types.h>
36*42e22086SApple OSS Distributions 
37*42e22086SApple OSS Distributions __BEGIN_DECLS
38*42e22086SApple OSS Distributions 
39*42e22086SApple OSS Distributions /*
40*42e22086SApple OSS Distributions  * Kernel support for generating corefiles on device.
41*42e22086SApple OSS Distributions  *
42*42e22086SApple OSS Distributions  * The kernel provides support for co-operatively generating core files
43*42e22086SApple OSS Distributions  * for any co/processors that register a coredump handler callback.
44*42e22086SApple OSS Distributions  *
45*42e22086SApple OSS Distributions  * The kernel will use the provided callbacks to generate a compressed
46*42e22086SApple OSS Distributions  * corefile in a file on disk.
47*42e22086SApple OSS Distributions  *
48*42e22086SApple OSS Distributions  * Corefiles consist of three main sections
49*42e22086SApple OSS Distributions  *      -- The headers that describe the corefile -- number of segments, etc
50*42e22086SApple OSS Distributions  *      -- The segment commands that describe the data in the corefile
51*42e22086SApple OSS Distributions  *      -- The segment data
52*42e22086SApple OSS Distributions  *
53*42e22086SApple OSS Distributions  * When a coredump handler is registered, a pointer to a kern_coredump_callback_config
54*42e22086SApple OSS Distributions  * structure is provided with callbacks that will be called as part of generating the
55*42e22086SApple OSS Distributions  * coredump.
56*42e22086SApple OSS Distributions  *
57*42e22086SApple OSS Distributions  * It's expected that each of these callbacks will return 0 on success (and non-zero on
58*42e22086SApple OSS Distributions  * error).
59*42e22086SApple OSS Distributions  */
60*42e22086SApple OSS Distributions 
61*42e22086SApple OSS Distributions void kern_coredump_log(void *context, const char *string, ...) __printflike(2, 3);
62*42e22086SApple OSS Distributions 
63*42e22086SApple OSS Distributions /*
64*42e22086SApple OSS Distributions  * The core_save_summary callback is provided with the call to the kcc_coredump_get_summary
65*42e22086SApple OSS Distributions  * routine that was registered. The caller should provide the following
66*42e22086SApple OSS Distributions  *
67*42e22086SApple OSS Distributions  * core_segment_count   -- Number of segments (LC_SEGMENT_KERNEL) that will be recorded
68*42e22086SApple OSS Distributions  * core_byte_count      -- Overall length of all data to be included across all segments
69*42e22086SApple OSS Distributions  * thread_count         -- Number of threads that will be recorded with thread state (LC_THREAD)
70*42e22086SApple OSS Distributions  * thread_state_size    -- Size of a thread's saved state (should be the overall LC_THREAD command size)
71*42e22086SApple OSS Distributions  * misc_bytes_count     -- Length of misc data that will be included in the core
72*42e22086SApple OSS Distributions  * mh_magic             -- mh_magic to be included in corefile
73*42e22086SApple OSS Distributions  * cpu_type             -- CPU type
74*42e22086SApple OSS Distributions  * cpu_subtype          -- CPU subtype
75*42e22086SApple OSS Distributions  * context              -- Passed to kcc_coredump_get_summary_routine
76*42e22086SApple OSS Distributions  */
77*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_summary_cb)(uint64_t core_segment_count, uint64_t core_byte_count,
78*42e22086SApple OSS Distributions     uint64_t thread_count, uint64_t thread_state_size,
79*42e22086SApple OSS Distributions     uint64_t misc_bytes_count, void *context);
80*42e22086SApple OSS Distributions 
81*42e22086SApple OSS Distributions /*
82*42e22086SApple OSS Distributions  * The core_save_note_summary callback is provided with the call to the
83*42e22086SApple OSS Distributions  * kcc_coredump_save_note_summary routine that was registered. The caller should
84*42e22086SApple OSS Distributions  * provide the following
85*42e22086SApple OSS Distributions  *
86*42e22086SApple OSS Distributions  * core_note_count      -- the number of LC_NOTE segments that will be recorded
87*42e22086SApple OSS Distributions  * core_note_byte_count -- overall length of all data to be included across all LC_NOTE segments
88*42e22086SApple OSS Distributions  * context              -- Passed to kcc_coredump_save_note_summary routine
89*42e22086SApple OSS Distributions  */
90*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_note_summary_cb)(uint64_t core_note_count, uint64_t core_note_byte_count,
91*42e22086SApple OSS Distributions     void *context);
92*42e22086SApple OSS Distributions 
93*42e22086SApple OSS Distributions /*
94*42e22086SApple OSS Distributions  * The core_save_segment_descriptions callback is provided with the call to the
95*42e22086SApple OSS Distributions  * kcc_coredump_save_segment_descriptions routine that was registered.
96*42e22086SApple OSS Distributions  *
97*42e22086SApple OSS Distributions  * It's expected that the caller should iterate all of the segments they want to include in
98*42e22086SApple OSS Distributions  * the corefile and call the callback with the following for each:
99*42e22086SApple OSS Distributions  *
100*42e22086SApple OSS Distributions  * Please note that seg_end is address of the byte immediately following the last byte in the segment.
101*42e22086SApple OSS Distributions  * For example, if a segment spans addresses 0x1000 to 0x1FFF, seg_end would be 0x2000.
102*42e22086SApple OSS Distributions  *
103*42e22086SApple OSS Distributions  * seg_start -- Start of the segment in the core's address space
104*42e22086SApple OSS Distributions  * seg_end   -- End of the segment in the core's address space
105*42e22086SApple OSS Distributions  * context   -- Passed to kcc_coredump_save_segment_descriptions routine
106*42e22086SApple OSS Distributions  */
107*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_segment_descriptions_cb)(uint64_t seg_start, uint64_t seg_end,
108*42e22086SApple OSS Distributions     void *context);
109*42e22086SApple OSS Distributions /*
110*42e22086SApple OSS Distributions  * The core_save_thread_state callback is provided with the call to the
111*42e22086SApple OSS Distributions  * kcc_coredump_save_thread_state routine that was registered.
112*42e22086SApple OSS Distributions  *
113*42e22086SApple OSS Distributions  * The routine is provided a pointer to a buffer of thread_state_size (as specified
114*42e22086SApple OSS Distributions  * previously) that can be used to populate thread state.
115*42e22086SApple OSS Distributions  *
116*42e22086SApple OSS Distributions  * It's expected that the caller should iterate all of the threads
117*42e22086SApple OSS Distributions  * that they would like to include and call the callback with the following
118*42e22086SApple OSS Distributions  * for each:
119*42e22086SApple OSS Distributions  *
120*42e22086SApple OSS Distributions  * thread_state -- A pointer to the buffer with an LC_THREAD command
121*42e22086SApple OSS Distributions  * context      -- Passed to kcc_coredump_save_thread_state routine
122*42e22086SApple OSS Distributions  */
123*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_thread_state_cb)(void *thread_state, void *context);
124*42e22086SApple OSS Distributions 
125*42e22086SApple OSS Distributions /*
126*42e22086SApple OSS Distributions  * The core_save_note_descriptions_cb callback is provided with the call to the
127*42e22086SApple OSS Distributions  * kcc_coredump_save_note_descriptions routine that was registered.
128*42e22086SApple OSS Distributions  *
129*42e22086SApple OSS Distributions  * It's expected that the caller should call this function for each LC_NOTE segment that
130*42e22086SApple OSS Distributions  * they want to include in the corefile and provide the following for each:
131*42e22086SApple OSS Distributions  *
132*42e22086SApple OSS Distributions  * data_owner   -- owner name for this LC_NOTE
133*42e22086SApple OSS Distributions  * length       -- length of the payload for this LC_NOTE
134*42e22086SApple OSS Distributions  * context      -- Passed to kcc_coredump_save_note_descriptions routine
135*42e22086SApple OSS Distributions  */
136*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_note_descriptions_cb)(const char *data_owner, uint64_t length, void *context);
137*42e22086SApple OSS Distributions 
138*42e22086SApple OSS Distributions /*
139*42e22086SApple OSS Distributions  * deprecated - please switch to core_save_sw_vers_detail_cb
140*42e22086SApple OSS Distributions  *
141*42e22086SApple OSS Distributions  * The core_save_sw_vers callback is provided with the call to the
142*42e22086SApple OSS Distributions  * kcc_coredump_save_sw_vers routine that was registered.
143*42e22086SApple OSS Distributions  *
144*42e22086SApple OSS Distributions  * The caller should call the callback with the following:
145*42e22086SApple OSS Distributions  *
146*42e22086SApple OSS Distributions  * sw_vers -- A pointer the software version information
147*42e22086SApple OSS Distributions  * length  -- Length of the software version information to be copied (< KERN_COREDUMP_VERSIONSTRINGMAXSIZE)
148*42e22086SApple OSS Distributions  * context -- Passed to kcc_coredump_save_sw_vers routine
149*42e22086SApple OSS Distributions  */
150*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_sw_vers_cb)(void *sw_vers, uint64_t length, void *context);
151*42e22086SApple OSS Distributions 
152*42e22086SApple OSS Distributions /*
153*42e22086SApple OSS Distributions  * The core_save_sw_vers_detail_cb callback is provided with the call to the
154*42e22086SApple OSS Distributions  * kcc_coredump_save_sw_vers_detail routine that was registered.
155*42e22086SApple OSS Distributions  *
156*42e22086SApple OSS Distributions  * The caller should call the callback with the following:
157*42e22086SApple OSS Distributions  *
158*42e22086SApple OSS Distributions  * address       -- base address of TEXT segment
159*42e22086SApple OSS Distributions  * uuid          -- uuid of running binary
160*42e22086SApple OSS Distributions  * log2_pagesize -- process page size in log base 2 (e.g. 4k pages are 12. 0 for unspecified)
161*42e22086SApple OSS Distributions  * context       -- Passed to kcc_coredump_save_sw_vers_detail routine
162*42e22086SApple OSS Distributions  */
163*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_sw_vers_detail_cb)(uint64_t address, uuid_t uuid, uint32_t log2_pagesize, void *context);
164*42e22086SApple OSS Distributions 
165*42e22086SApple OSS Distributions /*
166*42e22086SApple OSS Distributions  * The core_save_segment_data callback is provided with the call to the
167*42e22086SApple OSS Distributions  * kcc_coredump_save_segment_data routine that was registered.
168*42e22086SApple OSS Distributions  *
169*42e22086SApple OSS Distributions  * It's expected that the caller should iterate all of the segments they want to include in
170*42e22086SApple OSS Distributions  * the corefile and call the callback with the following for each:
171*42e22086SApple OSS Distributions  *
172*42e22086SApple OSS Distributions  * seg_data -- A pointer to the segment data (mapped in the kernel's address space)
173*42e22086SApple OSS Distributions  * length   -- Length of the data to be copied from the segment
174*42e22086SApple OSS Distributions  * context  -- Passed to kcc_coredump_save_segment_data routine
175*42e22086SApple OSS Distributions  */
176*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_segment_data_cb)(void *seg_data, uint64_t length, void *context);
177*42e22086SApple OSS Distributions 
178*42e22086SApple OSS Distributions /*
179*42e22086SApple OSS Distributions  * The core_save_note_data_cb callback is provided with the call to the
180*42e22086SApple OSS Distributions  * kcc_coredump_save_note_data routine that was registered.
181*42e22086SApple OSS Distributions  *
182*42e22086SApple OSS Distributions  * It's expected that the caller should iterate all of the notes they want to include in
183*42e22086SApple OSS Distributions  * the corefile and call the callback with the following for each:
184*42e22086SApple OSS Distributions  *
185*42e22086SApple OSS Distributions  * note_data    -- A pointer to the payload data for this LC_NOTE (mapped in the kernel's address space)
186*42e22086SApple OSS Distributions  * length       -- length of the payload for this LC_NOTE
187*42e22086SApple OSS Distributions  * context      -- Passed to kcc_coredump_save_note_data routine
188*42e22086SApple OSS Distributions  */
189*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_note_data_cb)(void *note_data, uint64_t length, void *context);
190*42e22086SApple OSS Distributions 
191*42e22086SApple OSS Distributions /*
192*42e22086SApple OSS Distributions  * deprecated - please switch to core_save_note*
193*42e22086SApple OSS Distributions  *
194*42e22086SApple OSS Distributions  * The core_save_misc_data callback is provided with the call to the
195*42e22086SApple OSS Distributions  * kcc_coredump_save_misc_data routine that was registered
196*42e22086SApple OSS Distributions  *
197*42e22086SApple OSS Distributions  * The caller should call the callback with the following:
198*42e22086SApple OSS Distributions  *
199*42e22086SApple OSS Distributions  * misc_data -- A pointer to the data to be copied
200*42e22086SApple OSS Distributions  * length    -- The length of the data to be copied
201*42e22086SApple OSS Distributions  * context   -- Passed to kcc_coredump_save_misc_data routine
202*42e22086SApple OSS Distributions  */
203*42e22086SApple OSS Distributions typedef kern_return_t (*core_save_misc_data_cb)(void *misc_data, uint64_t length, void *context);
204*42e22086SApple OSS Distributions 
205*42e22086SApple OSS Distributions typedef struct {
206*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_init)(void *refcon, void *context); /* OPTIONAL -- return KERN_NODE_DOWN if the co-processor should be skipped */
207*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_get_summary)(void *refcon, core_save_summary_cb callback, void *context);
208*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_segment_descriptions)(void *refcon, core_save_segment_descriptions_cb callback, void *context);
209*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_thread_state)(void *refcon, void *buf, core_save_thread_state_cb callback, void *context);
210*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_sw_vers)(void *refcon, core_save_sw_vers_cb callback, void *context) __deprecated_msg("please switch to kcc_coredump_save_sw_vers_detail");
211*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_segment_data)(void *refcon, core_save_segment_data_cb callback, void *context);
212*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_misc_data)(void *refcon, core_save_misc_data_cb callback, void *context) __deprecated_msg("please switch to kcc_coredump_save_note_*");
213*42e22086SApple OSS Distributions 	/* End of version 1 */
214*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_note_summary)(void *refcon, core_save_note_summary_cb callback, void *context);
215*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_note_descriptions)(void *refcon, core_save_note_descriptions_cb callback, void *context);
216*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_note_data)(void *refcon, core_save_note_data_cb callback, void *context);
217*42e22086SApple OSS Distributions 	kern_return_t (*kcc_coredump_save_sw_vers_detail)(void *refcon, core_save_sw_vers_detail_cb callback, void *context);
218*42e22086SApple OSS Distributions 	/* End of version 2 */
219*42e22086SApple OSS Distributions } kern_coredump_callback_config;
220*42e22086SApple OSS Distributions 
221*42e22086SApple OSS Distributions #define KERN_COREDUMP_MAX_CORES 64
222*42e22086SApple OSS Distributions #define KERN_COREDUMP_MIN_CONFIG_VERSION 1
223*42e22086SApple OSS Distributions #define KERN_COREDUMP_MIN_CONFIG_NOTES 2
224*42e22086SApple OSS Distributions #define KERN_COREDUMP_CONFIG_VERSION 2
225*42e22086SApple OSS Distributions #define KERN_COREDUMP_VERSIONSTRINGMAXSIZE 256
226*42e22086SApple OSS Distributions 
227*42e22086SApple OSS Distributions /*
228*42e22086SApple OSS Distributions  * kern_register_coredump_helper is called to register a core with the kernel
229*42e22086SApple OSS Distributions  * coredump infrastructure. In addition to the callback config and version of the config
230*42e22086SApple OSS Distributions  * structure, a description of the core should be provided -- i.e.: AP
231*42e22086SApple OSS Distributions  */
232*42e22086SApple OSS Distributions kern_return_t kern_register_coredump_helper(int kern_coredump_config_vers, const kern_coredump_callback_config *kc_callbacks, void *refcon,
233*42e22086SApple OSS Distributions     const char *core_description, boolean_t is64bit, uint32_t mh_magic, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype);
234*42e22086SApple OSS Distributions 
235*42e22086SApple OSS Distributions #if PRIVATE
236*42e22086SApple OSS Distributions 
237*42e22086SApple OSS Distributions kern_return_t kern_register_xnu_coredump_helper(kern_coredump_callback_config *kc_callbacks);
238*42e22086SApple OSS Distributions kern_return_t kern_register_userspace_coredump(task_t task, const char * name);
239*42e22086SApple OSS Distributions kern_return_t kern_unregister_userspace_coredump(task_t task);
240*42e22086SApple OSS Distributions 
241*42e22086SApple OSS Distributions kern_return_t kern_do_coredump(void *core_outvars, boolean_t kernel_only, uint64_t first_file_offset, uint64_t *last_file_offset, uint64_t details_flags);
242*42e22086SApple OSS Distributions 
243*42e22086SApple OSS Distributions #define KERN_COREDUMP_MAXDEBUGLOGSIZE 16384
244*42e22086SApple OSS Distributions #define KERN_COREDUMP_BEGIN_FILEBYTES_ALIGN 4096
245*42e22086SApple OSS Distributions #define KERN_COREDUMP_THREADSIZE_MAX 1024
246*42e22086SApple OSS Distributions 
247*42e22086SApple OSS Distributions #if XNU_KERNEL_PRIVATE
248*42e22086SApple OSS Distributions 
249*42e22086SApple OSS Distributions struct kern_userspace_coredump_context {
250*42e22086SApple OSS Distributions 	/* Task to dump */
251*42e22086SApple OSS Distributions 	task_t task;
252*42e22086SApple OSS Distributions };
253*42e22086SApple OSS Distributions 
254*42e22086SApple OSS Distributions kern_return_t user_dump_init(void *refcon, void *context);
255*42e22086SApple OSS Distributions kern_return_t user_dump_save_summary(void *refcon, core_save_summary_cb callback, void *context);
256*42e22086SApple OSS Distributions kern_return_t user_dump_save_seg_descriptions(void *refcon, core_save_segment_descriptions_cb callback, void *context);
257*42e22086SApple OSS Distributions kern_return_t user_dump_save_thread_state(void *refcon, void *buf, core_save_thread_state_cb callback, void *context);
258*42e22086SApple OSS Distributions kern_return_t user_dump_save_sw_vers_detail(void *refcon, core_save_sw_vers_detail_cb callback, void *context);
259*42e22086SApple OSS Distributions kern_return_t user_dump_save_segment_data(void *refcon, core_save_segment_data_cb callback, void *context);
260*42e22086SApple OSS Distributions kern_return_t user_dump_save_note_summary(void *refcon, core_save_note_summary_cb callback, void *context);
261*42e22086SApple OSS Distributions kern_return_t user_dump_save_note_descriptions(void *refcon, core_save_note_descriptions_cb callback, void *context);
262*42e22086SApple OSS Distributions kern_return_t user_dump_save_note_data(void *refcon, core_save_note_data_cb callback, void *context);
263*42e22086SApple OSS Distributions 
264*42e22086SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
265*42e22086SApple OSS Distributions 
266*42e22086SApple OSS Distributions #endif /* PRIVATE */
267*42e22086SApple OSS Distributions 
268*42e22086SApple OSS Distributions __END_DECLS
269*42e22086SApple OSS Distributions #endif /* _PROCESSOR_CORE_H_ */
270