xref: /xnu-11215/osfmk/kdp/processor_core.c (revision 8d741a5d)
1 /*
2  * Copyright (c) 2017 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <kdp/kdp_core.h>
30 #include <kdp/processor_core.h>
31 #include <kdp/core_notes.h>
32 #include <kern/assert.h>
33 #include <kern/monotonic.h>
34 #include <kern/zalloc.h>
35 #include <libkern/kernel_mach_header.h>
36 #include <libkern/OSAtomic.h>
37 #include <libsa/types.h>
38 #include <pexpert/pexpert.h>
39 #include <vm/vm_map.h>
40 
41 #ifdef CONFIG_KDP_INTERACTIVE_DEBUGGING
42 
43 #define roundup(x, y)   ((((x) % (y)) == 0) ? \
44 	                (x) : ((x) + ((y) - ((x) % (y)))))
45 
46 #define DATA_OWNER_LEGACY_BIN_SPEC "kern ver str"
47 /*
48  * Format of the legacy bin spec (LC_IDENT-like) LC_NOTE payload as expected by LLDB
49  */
50 typedef struct {
51 	uint32_t version; // currently 1
52 	char version_string[KERN_COREDUMP_VERSIONSTRINGMAXSIZE];
53 } __attribute__((packed)) legacy_bin_spec;
54 #define LEGACY_BIN_SPEC_VERSION 1
55 
56 __enum_closed_decl(kern_coredump_type_t, uint8_t, {
57 	XNU_COREDUMP,
58 	USERSPACE_COREDUMP,
59 	COPROCESSOR_COREDUMP,
60 	SECURE_COREDUMP,
61 	NUM_COREDUMP_TYPES,
62 });
63 
64 static uint32_t bin_spec_map[NUM_COREDUMP_TYPES] = {
65 	[XNU_COREDUMP] = MAIN_BIN_SPEC_TYPE_KERNEL,
66 	[USERSPACE_COREDUMP] = MAIN_BIN_SPEC_TYPE_USER,
67 	[COPROCESSOR_COREDUMP] = MAIN_BIN_SPEC_TYPE_STANDALONE,
68 	[SECURE_COREDUMP] = MAIN_BIN_SPEC_TYPE_STANDALONE
69 };
70 
71 /*
72  * The processor_core_context structure describes the current
73  * corefile that's being generated. It also includes a pointer
74  * to the core_outvars which is used by the KDP code for context
75  * about the specific output mechanism being used.
76  *
77  * We include *remaining variables to catch inconsistencies / bugs
78  * in the co-processor coredump callbacks.
79  */
80 typedef struct {
81 	struct kdp_core_out_vars * core_outvars;     /* Output procedure info (see kdp_out_stage.h) */
82 	kern_coredump_callback_config *core_config;  /* Information about core currently being dumped */
83 	void *core_refcon;                           /* Reference constant associated with the coredump helper */
84 	boolean_t core_should_be_skipped;            /* Indicates whether this specific core should not be dumped */
85 	boolean_t core_is64bit;                      /* Bitness of CPU */
86 	kern_coredump_type_t core_type;              /* Indicates type of this core*/
87 	uint32_t core_mh_magic;                      /* Magic for mach header */
88 	cpu_type_t core_cpu_type;                    /* CPU type for mach header */
89 	cpu_subtype_t core_cpu_subtype;              /* CPU subtype for mach header */
90 	uint64_t core_file_length;                   /* Overall corefile length including any zero padding */
91 	uint64_t core_file_length_compressed;        /* File length after compression */
92 	uint64_t core_segment_count;                 /* Number of LC_SEGMENTs in the core currently being dumped */
93 	uint64_t core_segments_remaining;            /* Number of LC_SEGMENTs that have not been added to the header */
94 	uint64_t core_segment_byte_total;            /* Sum of all the data from the LC_SEGMENTS in the core */
95 	uint64_t core_segment_bytes_remaining;       /* Quantity of data remaining from LC_SEGMENTs that have yet to be added */
96 	uint64_t core_thread_count;                  /* Number of LC_THREADs to be included */
97 	uint64_t core_threads_remaining;             /* Number of LC_THREADs that have yet to be included */
98 	uint64_t core_thread_state_size;             /* Size of each LC_THREAD */
99 	uint64_t core_note_count;                    /* Number of LC_NOTEs to be included */
100 	uint64_t core_notes_remaining;               /* Number of LC_NOTEs that have not been added to the header */
101 	uint64_t core_note_bytes_total;              /* Sum of all data from the LC_NOTE segments in the core */
102 	uint64_t core_note_bytes_remaining;          /* Quantity of data remaining from LC_NOTEs that have yet to be added */
103 	uint64_t core_cur_hoffset;                   /* Current offset in this core's header */
104 	uint64_t core_cur_foffset;                   /* Current offset in this core's overall file */
105 	uint64_t core_header_size;                   /* Size of this core's header */
106 	uint64_t core_total_bytes;                   /* Total amount of data to be included in this core (excluding zero fill) */
107 } processor_core_context;
108 
109 /*
110  * The kern_coredump_core structure describes a core that has been
111  * registered for use by the coredump mechanism.
112  */
113 struct kern_coredump_core {
114 	struct kern_coredump_core *kcc_next;             /* Next processor to dump */
115 	void *kcc_refcon;                                /* Reference constant to be passed to callbacks */
116 	char kcc_corename[MACH_CORE_FILEHEADER_NAMELEN]; /* Description of this processor */
117 	boolean_t kcc_is64bit;                           /* Processor bitness */
118 	uint32_t kcc_mh_magic;                           /* Magic for mach header */
119 	cpu_type_t kcc_cpu_type;                         /* CPU type for mach header */
120 	cpu_subtype_t kcc_cpu_subtype;                   /* CPU subtype for mach header */
121 	kern_coredump_callback_config kcc_cb;            /* Registered processor callbacks for coredump */
122 };
123 
124 struct kern_coredump_core * kern_coredump_core_list = NULL;
125 struct kern_coredump_core * kern_userspace_coredump_core_list = NULL;
126 LCK_GRP_DECLARE(kern_userspace_coredump_core_list_lock_grp, "userspace coredump list");
127 LCK_MTX_DECLARE(kern_userspace_coredump_core_list_lock, &kern_userspace_coredump_core_list_lock_grp);
128 
129 typedef kern_return_t (*legacy_sw_vers_registered_cb)(void *refcon, core_save_sw_vers_cb callback, void *context);
130 
131 uint32_t coredump_registered_count = 0;
132 
133 struct kern_coredump_core *kernel_helper = NULL;
134 struct kern_coredump_core *sk_helper = NULL;
135 
136 static struct kern_coredump_core *
kern_register_coredump_helper_internal(int kern_coredump_config_vers,const kern_coredump_callback_config * kc_callbacks,void * refcon,const char * core_description,kern_coredump_type_t type,boolean_t is64bit,uint32_t mh_magic,cpu_type_t cpu_type,cpu_subtype_t cpu_subtype)137 kern_register_coredump_helper_internal(int kern_coredump_config_vers, const kern_coredump_callback_config *kc_callbacks,
138     void *refcon, const char *core_description, kern_coredump_type_t type, boolean_t is64bit,
139     uint32_t mh_magic, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype)
140 {
141 	struct kern_coredump_core *core_helper = NULL;
142 	kern_coredump_callback_config *core_callbacks = NULL;
143 
144 	if (kern_coredump_config_vers < KERN_COREDUMP_MIN_CONFIG_VERSION) {
145 		return NULL;
146 	}
147 	if (kc_callbacks == NULL) {
148 		return NULL;
149 	}
150 	;
151 	if (core_description == NULL) {
152 		return NULL;
153 	}
154 
155 	if (kc_callbacks->kcc_coredump_get_summary == NULL ||
156 	    kc_callbacks->kcc_coredump_save_segment_descriptions == NULL ||
157 	    kc_callbacks->kcc_coredump_save_segment_data == NULL ||
158 	    kc_callbacks->kcc_coredump_save_thread_state == NULL) {
159 		return NULL;
160 	}
161 
162 #pragma clang diagnostic push
163 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
164 	legacy_sw_vers_registered_cb legacy_vers_callback = kc_callbacks->kcc_coredump_save_sw_vers;
165 #pragma clang diagnostic pop
166 
167 	if (kern_coredump_config_vers >= KERN_COREDUMP_MIN_CONFIG_NOTES) {
168 		if (legacy_vers_callback == NULL &&
169 		    kc_callbacks->kcc_coredump_save_sw_vers_detail == NULL) {
170 			return NULL;
171 		}
172 	} else {
173 		if (legacy_vers_callback == NULL) {
174 			return NULL;
175 		}
176 	}
177 
178 
179 	if (kern_coredump_config_vers >= KERN_COREDUMP_MIN_CONFIG_NOTES) {
180 		/* Either all note related callbacks should be set or none should be set */
181 		if ((kc_callbacks->kcc_coredump_save_note_summary == NULL) != (kc_callbacks->kcc_coredump_save_note_descriptions == NULL)) {
182 			return NULL;
183 		}
184 		if ((kc_callbacks->kcc_coredump_save_note_descriptions == NULL) != (kc_callbacks->kcc_coredump_save_note_data == NULL)) {
185 			return NULL;
186 		}
187 	}
188 
189 
190 #if !defined(__LP64__)
191 	/* We don't support generating 64-bit cores on 32-bit platforms */
192 	if (is64bit) {
193 		return NULL;
194 	}
195 #endif
196 
197 	core_helper = zalloc_permanent_type(struct kern_coredump_core);
198 	core_helper->kcc_next = NULL;
199 	core_helper->kcc_refcon = refcon;
200 	if (type == XNU_COREDUMP || type == USERSPACE_COREDUMP || type == SECURE_COREDUMP) {
201 		snprintf((char *)&core_helper->kcc_corename, MACH_CORE_FILEHEADER_NAMELEN, "%s", core_description);
202 	} else {
203 		assert(type == COPROCESSOR_COREDUMP);
204 		/* Make sure there's room for the -cp suffix (16 - NULL char - strlen(-cp)) */
205 		snprintf((char *)&core_helper->kcc_corename, MACH_CORE_FILEHEADER_NAMELEN, "%.12s-cp", core_description);
206 	}
207 	core_helper->kcc_is64bit = is64bit;
208 	core_helper->kcc_mh_magic = mh_magic;
209 	core_helper->kcc_cpu_type = cpu_type;
210 	core_helper->kcc_cpu_subtype = cpu_subtype;
211 	core_callbacks = &core_helper->kcc_cb;
212 
213 	core_callbacks->kcc_coredump_init = kc_callbacks->kcc_coredump_init;
214 	core_callbacks->kcc_coredump_get_summary = kc_callbacks->kcc_coredump_get_summary;
215 	core_callbacks->kcc_coredump_save_segment_descriptions = kc_callbacks->kcc_coredump_save_segment_descriptions;
216 	core_callbacks->kcc_coredump_save_segment_data = kc_callbacks->kcc_coredump_save_segment_data;
217 	core_callbacks->kcc_coredump_save_thread_state = kc_callbacks->kcc_coredump_save_thread_state;
218 #pragma clang diagnostic push
219 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
220 	core_callbacks->kcc_coredump_save_sw_vers = kc_callbacks->kcc_coredump_save_sw_vers;
221 #pragma clang diagnostic pop
222 
223 
224 	if (kern_coredump_config_vers >= KERN_COREDUMP_MIN_CONFIG_NOTES) {
225 		core_callbacks->kcc_coredump_save_note_summary = kc_callbacks->kcc_coredump_save_note_summary;
226 		core_callbacks->kcc_coredump_save_note_descriptions = kc_callbacks->kcc_coredump_save_note_descriptions;
227 		core_callbacks->kcc_coredump_save_note_data = kc_callbacks->kcc_coredump_save_note_data;
228 		core_callbacks->kcc_coredump_save_sw_vers_detail = kc_callbacks->kcc_coredump_save_sw_vers_detail;
229 	}
230 
231 	if (type == XNU_COREDUMP) {
232 		assert(kernel_helper == NULL);
233 		kernel_helper = core_helper;
234 	} else if (type == SECURE_COREDUMP) {
235 		assert(sk_helper == NULL);
236 		sk_helper = core_helper;
237 	} else if (type == USERSPACE_COREDUMP) {
238 		lck_mtx_lock(&kern_userspace_coredump_core_list_lock);
239 		core_helper->kcc_next = kern_userspace_coredump_core_list;
240 		kern_userspace_coredump_core_list = core_helper;
241 		lck_mtx_unlock(&kern_userspace_coredump_core_list_lock);
242 	} else {
243 		assert(type == COPROCESSOR_COREDUMP);
244 		do {
245 			core_helper->kcc_next = kern_coredump_core_list;
246 		} while (!OSCompareAndSwapPtr(kern_coredump_core_list, core_helper, &kern_coredump_core_list));
247 	}
248 
249 	OSAddAtomic(1, &coredump_registered_count);
250 	kprintf("Registered coredump handler for %s\n", core_description);
251 
252 	return core_helper;
253 }
254 
255 kern_return_t
kern_register_coredump_helper(int kern_coredump_config_vers,const kern_coredump_callback_config * kc_callbacks,void * refcon,const char * core_description,boolean_t is64bit,uint32_t mh_magic,cpu_type_t cpu_type,cpu_subtype_t cpu_subtype)256 kern_register_coredump_helper(int kern_coredump_config_vers, const kern_coredump_callback_config *kc_callbacks,
257     void *refcon, const char *core_description, boolean_t is64bit, uint32_t mh_magic,
258     cpu_type_t cpu_type, cpu_subtype_t cpu_subtype)
259 {
260 	if (coredump_registered_count >= KERN_COREDUMP_MAX_CORES) {
261 		return KERN_RESOURCE_SHORTAGE;
262 	}
263 
264 	if (kern_register_coredump_helper_internal(kern_coredump_config_vers, kc_callbacks, refcon, core_description, COPROCESSOR_COREDUMP,
265 	    is64bit, mh_magic, cpu_type, cpu_subtype) == NULL) {
266 		return KERN_INVALID_ARGUMENT;
267 	}
268 
269 	return KERN_SUCCESS;
270 }
271 
272 kern_return_t
kern_register_xnu_coredump_helper(kern_coredump_callback_config * kc_callbacks)273 kern_register_xnu_coredump_helper(kern_coredump_callback_config *kc_callbacks)
274 {
275 #if defined(__LP64__)
276 	boolean_t is64bit = TRUE;
277 #else
278 	boolean_t is64bit = FALSE;
279 #endif
280 
281 	if (kern_register_coredump_helper_internal(KERN_COREDUMP_CONFIG_VERSION, kc_callbacks, NULL, "kernel", XNU_COREDUMP, is64bit,
282 	    _mh_execute_header.magic, _mh_execute_header.cputype, _mh_execute_header.cpusubtype) == NULL) {
283 		return KERN_FAILURE;
284 	}
285 
286 	return KERN_SUCCESS;
287 }
288 
289 kern_return_t
kern_register_sk_coredump_helper(kern_coredump_callback_config * sk_callbacks,void * refcon)290 kern_register_sk_coredump_helper(kern_coredump_callback_config *sk_callbacks, void *refcon)
291 {
292 	if (kern_register_coredump_helper_internal(KERN_COREDUMP_CONFIG_VERSION, sk_callbacks,
293 	    refcon, "secure-kernel", SECURE_COREDUMP, TRUE, _mh_execute_header.magic,
294 	    _mh_execute_header.cputype, _mh_execute_header.cpusubtype) == NULL) {
295 		return KERN_FAILURE;
296 	}
297 
298 	return KERN_SUCCESS;
299 }
300 
301 extern cpu_type_t
302 process_cpu_type(void * bsd_info);
303 
304 extern cpu_type_t
305 process_cpu_subtype(void * bsd_info);
306 
307 extern char     *proc_name_address(void *p);
308 
309 kern_return_t
kern_register_userspace_coredump(task_t task,const char * name)310 kern_register_userspace_coredump(task_t task, const char * name)
311 {
312 	kern_return_t result;
313 	struct kern_userspace_coredump_context * context = NULL;
314 	boolean_t is64bit;
315 	uint32_t mh_magic;
316 	uint32_t mh_cputype;
317 	uint32_t mh_cpusubtype;
318 	kern_coredump_callback_config userkc_callbacks;
319 
320 	is64bit = task_has_64Bit_addr(task);
321 	mh_magic = is64bit ? MH_MAGIC_64 : MH_MAGIC;
322 	mh_cputype = process_cpu_type(get_bsdtask_info(task));
323 	mh_cpusubtype = process_cpu_subtype(get_bsdtask_info(task));
324 
325 
326 	context = kalloc_type(struct kern_userspace_coredump_context, (zalloc_flags_t)(Z_WAITOK | Z_ZERO));
327 	context->task = task;
328 
329 	userkc_callbacks.kcc_coredump_init = user_dump_init;
330 	userkc_callbacks.kcc_coredump_get_summary = user_dump_save_summary;
331 	userkc_callbacks.kcc_coredump_save_segment_descriptions = user_dump_save_seg_descriptions;
332 	userkc_callbacks.kcc_coredump_save_thread_state = user_dump_save_thread_state;
333 	userkc_callbacks.kcc_coredump_save_sw_vers_detail = user_dump_save_sw_vers_detail;
334 	userkc_callbacks.kcc_coredump_save_segment_data = user_dump_save_segment_data;
335 	userkc_callbacks.kcc_coredump_save_note_summary = user_dump_save_note_summary;
336 	userkc_callbacks.kcc_coredump_save_note_descriptions = user_dump_save_note_descriptions;
337 	userkc_callbacks.kcc_coredump_save_note_data = user_dump_save_note_data;
338 
339 	if (kern_register_coredump_helper_internal(KERN_COREDUMP_CONFIG_VERSION, &userkc_callbacks, context, name, USERSPACE_COREDUMP, is64bit,
340 	    mh_magic, mh_cputype, mh_cpusubtype) == NULL) {
341 		result = KERN_FAILURE;
342 		goto finish;
343 	}
344 
345 	result = KERN_SUCCESS;
346 
347 finish:
348 	if (result != KERN_SUCCESS && context != NULL) {
349 		kfree_type(struct kern_userspace_coredump_context, context);
350 	}
351 
352 	return result;
353 }
354 
355 kern_return_t
kern_unregister_userspace_coredump(task_t task)356 kern_unregister_userspace_coredump(task_t task)
357 {
358 	struct kern_coredump_core * current_core = NULL;
359 	struct kern_coredump_core * previous_core = NULL;
360 
361 	lck_mtx_lock(&kern_userspace_coredump_core_list_lock);
362 	current_core = kern_userspace_coredump_core_list;
363 	while (current_core) {
364 		struct kern_userspace_coredump_context * context = (struct kern_userspace_coredump_context *)current_core->kcc_refcon;
365 		assert(context != NULL);
366 		if (context->task == task) {
367 			/* remove current_core from the list */
368 			if (previous_core == NULL) {
369 				kern_userspace_coredump_core_list = current_core->kcc_next;
370 			} else {
371 				previous_core->kcc_next = current_core->kcc_next;
372 			}
373 			break;
374 		}
375 		previous_core = current_core;
376 		current_core = current_core->kcc_next;
377 	}
378 	lck_mtx_unlock(&kern_userspace_coredump_core_list_lock);
379 
380 	if (current_core) {
381 		kfree_type(struct kern_userspace_coredump_context, current_core->kcc_refcon);
382 		OSAddAtomic(-1, &coredump_registered_count);
383 		return KERN_SUCCESS;
384 	}
385 
386 	return KERN_NOT_FOUND;
387 }
388 
389 /*
390  * Save LC_NOTE metadata about the core we are going to write before we write the mach header
391  */
392 static int
coredump_save_note_summary(uint64_t core_note_count,uint64_t core_note_byte_count,void * context)393 coredump_save_note_summary(uint64_t core_note_count, uint64_t core_note_byte_count, void *context)
394 {
395 	processor_core_context *core_context = (processor_core_context *)context;
396 
397 	if (!core_note_count || !core_note_byte_count || !context) {
398 		return KERN_INVALID_ARGUMENT;
399 	}
400 
401 	core_context->core_note_count = core_context->core_notes_remaining = core_note_count;
402 	core_context->core_note_bytes_total = core_context->core_note_bytes_remaining = core_note_byte_count;
403 
404 	return KERN_SUCCESS;
405 }
406 
407 /*
408  * Save metadata about the core we're about to write, write out the mach header
409  */
410 static int
coredump_save_summary(uint64_t core_segment_count,uint64_t core_byte_count,uint64_t thread_count,uint64_t thread_state_size,__unused uint64_t misc_bytes_count,void * context)411 coredump_save_summary(uint64_t core_segment_count, uint64_t core_byte_count,
412     uint64_t thread_count, uint64_t thread_state_size,
413     __unused uint64_t misc_bytes_count, void *context)
414 {
415 	processor_core_context *core_context = (processor_core_context *)context;
416 	uint32_t sizeofcmds = 0, numcmds = 0;
417 	bool should_skip = false;
418 	int ret = 0;
419 
420 	if (!core_segment_count || !core_byte_count
421 	    || (thread_state_size > KERN_COREDUMP_THREADSIZE_MAX)) {
422 		return KERN_INVALID_ARGUMENT;
423 	}
424 
425 	/*
426 	 * secure coredumps and coprocessor coredumps aren't required to contain any thread state,
427 	 * because it's reconstructed during the lldb session
428 	 */
429 	if (core_context->core_type != SECURE_COREDUMP && core_context->core_type != COPROCESSOR_COREDUMP
430 	    && (!thread_count || !thread_state_size)) {
431 		return KERN_INVALID_ARGUMENT;
432 	}
433 
434 	/* Initialize core_context */
435 	core_context->core_segments_remaining = core_context->core_segment_count = core_segment_count;
436 	core_context->core_segment_bytes_remaining = core_context->core_segment_byte_total = core_byte_count;
437 	core_context->core_threads_remaining = core_context->core_thread_count = thread_count;
438 	core_context->core_thread_state_size = thread_state_size;
439 
440 	/* Account for the LC_NOTE needed to store version/load information */
441 	core_context->core_note_count = core_context->core_notes_remaining = (core_context->core_note_count + 1);
442 	size_t vers_note_length = sizeof(main_bin_spec_note_t);
443 	if (core_context->core_config->kcc_coredump_save_sw_vers_detail == NULL) {
444 		vers_note_length = sizeof(legacy_bin_spec);
445 	}
446 	core_context->core_note_bytes_total = core_context->core_note_bytes_remaining = (core_context->core_note_bytes_total + vers_note_length);
447 
448 #if defined(__LP64__)
449 	if (core_context->core_is64bit) {
450 		sizeofcmds = (uint32_t)(core_context->core_segment_count * sizeof(struct segment_command_64) +
451 		    (core_context->core_threads_remaining * core_context->core_thread_state_size) +
452 		    (core_context->core_note_count * sizeof(struct note_command)));
453 		core_context->core_header_size = sizeofcmds + sizeof(struct mach_header_64);
454 	} else
455 #endif /* defined(__LP64__) */
456 	{
457 		sizeofcmds = (uint32_t)(core_context->core_segment_count * sizeof(struct segment_command) +
458 		    (core_context->core_threads_remaining * core_context->core_thread_state_size) +
459 		    (core_context->core_note_count * sizeof(struct note_command)));
460 		core_context->core_header_size = sizeofcmds + sizeof(struct mach_header);
461 	}
462 
463 
464 	core_context->core_total_bytes = core_context->core_header_size + core_context->core_segment_byte_total + core_context->core_note_bytes_total;
465 	core_context->core_file_length = round_page(core_context->core_header_size) + core_context->core_segment_byte_total + core_context->core_note_bytes_total;
466 	core_context->core_cur_foffset = round_page(core_context->core_header_size);
467 
468 	numcmds = (uint32_t)(core_context->core_segment_count + core_context->core_thread_count + core_context->core_note_count);
469 
470 	/*
471 	 * Reset the zstream and other output context before writing any data out. We do this here
472 	 * to update the total file length on the outvars before we start writing out.
473 	 */
474 	ret = kdp_reset_output_vars(core_context->core_outvars, core_context->core_file_length, true, &should_skip);
475 	if (ret != KERN_SUCCESS) {
476 		kern_coredump_log(context, "%s() : failed to reset the out vars : kdp_reset_output_vars(%p, %llu, true, %p) returned error 0x%x\n",
477 		    __func__, core_context->core_outvars, core_context->core_file_length, &should_skip, ret);
478 		return ret;
479 	}
480 
481 	if (should_skip) {
482 		core_context->core_should_be_skipped = TRUE;
483 		return KERN_SUCCESS;
484 	}
485 
486 	/* Construct core file header */
487 #if defined(__LP64__)
488 	if (core_context->core_is64bit) {
489 		struct mach_header_64 core_header = { };
490 
491 		core_header.magic = core_context->core_mh_magic;
492 		core_header.cputype = core_context->core_cpu_type;
493 		core_header.cpusubtype = core_context->core_cpu_subtype;
494 		core_header.filetype = MH_CORE;
495 		core_header.ncmds = numcmds;
496 		core_header.sizeofcmds = sizeofcmds;
497 		core_header.flags = 0;
498 
499 		/* Send the core_header to the output procedure */
500 		ret =  kdp_core_output(core_context->core_outvars, sizeof(core_header), (caddr_t)&core_header);
501 		if (ret != KERN_SUCCESS) {
502 			kern_coredump_log(context, "%s() : failed to write mach header : kdp_core_output(%p, %lu, %p) returned error 0x%x\n",
503 			    __func__, core_context->core_outvars, sizeof(core_header), &core_header, ret);
504 			return ret;
505 		}
506 
507 		core_context->core_cur_hoffset += sizeof(core_header);
508 	} else
509 #endif /* defined(__LP64__) */
510 	{
511 		struct mach_header core_header = { };
512 
513 		core_header.magic = core_context->core_mh_magic;
514 		core_header.cputype = core_context->core_cpu_type;
515 		core_header.cpusubtype = core_context->core_cpu_subtype;
516 		core_header.filetype = MH_CORE;
517 		core_header.ncmds = numcmds;
518 		core_header.sizeofcmds = sizeofcmds;
519 		core_header.flags = 0;
520 
521 		/* Send the core_header to the output procedure */
522 		ret =  kdp_core_output(core_context->core_outvars, sizeof(core_header), (caddr_t)&core_header);
523 		if (ret != KERN_SUCCESS) {
524 			kern_coredump_log(context, "%s() : failed to write mach header : kdp_core_output(%p, %lu, %p) returned error 0x%x\n",
525 			    __func__, core_context->core_outvars, sizeof(core_header), &core_header, ret);
526 			return ret;
527 		}
528 
529 		core_context->core_cur_hoffset += sizeof(core_header);
530 	}
531 
532 	return KERN_SUCCESS;
533 }
534 
535 /*
536  * Construct a segment command for the specified segment.
537  */
538 static int
coredump_save_segment_descriptions(uint64_t seg_start,uint64_t seg_end,void * context)539 coredump_save_segment_descriptions(uint64_t seg_start, uint64_t seg_end,
540     void *context)
541 {
542 	processor_core_context *core_context = (processor_core_context *)context;
543 	int ret;
544 	uint64_t size = seg_end - seg_start;
545 
546 	if (seg_end <= seg_start) {
547 		kern_coredump_log(context, "%s(0x%llx, 0x%llx, %p) : called with invalid addresses : start 0x%llx >= end 0x%llx\n",
548 		    __func__, seg_start, seg_end, context, seg_start, seg_end);
549 		return KERN_INVALID_ARGUMENT;
550 	}
551 
552 	if (core_context->core_segments_remaining == 0) {
553 		kern_coredump_log(context, "%s(0x%llx, 0x%llx, %p) : coredump_save_segment_descriptions() called too many times, %llu segment descriptions already recorded\n",
554 		    __func__, seg_start, seg_end, context, core_context->core_segment_count);
555 		return KERN_INVALID_ARGUMENT;
556 	}
557 
558 	/* Construct segment command */
559 #if defined(__LP64__)
560 	if (core_context->core_is64bit) {
561 		struct segment_command_64 seg_command = { };
562 
563 		if (core_context->core_cur_hoffset + sizeof(seg_command) > core_context->core_header_size) {
564 			kern_coredump_log(context, "%s(0x%llx, 0x%llx, %p) : ran out of space to save commands with %llu of %llu remaining\n",
565 			    __func__, seg_start, seg_end, context, core_context->core_segments_remaining, core_context->core_segment_count);
566 			return KERN_NO_SPACE;
567 		}
568 
569 		seg_command.cmd = LC_SEGMENT_64;
570 		seg_command.cmdsize = sizeof(seg_command);
571 		seg_command.segname[0] = 0;
572 		seg_command.vmaddr = seg_start;
573 		seg_command.vmsize = size;
574 		seg_command.fileoff = core_context->core_cur_foffset;
575 		seg_command.filesize = size;
576 		seg_command.maxprot = VM_PROT_READ;
577 		seg_command.initprot = VM_PROT_READ;
578 
579 		/* Flush new command to output */
580 		ret = kdp_core_output(core_context->core_outvars, sizeof(seg_command), (caddr_t)&seg_command);
581 		if (ret != KERN_SUCCESS) {
582 			kern_coredump_log(context, "%s(0x%llx, 0x%llx, %p) : failed to write segment %llu of %llu. kdp_core_output(%p, %lu, %p) returned error %d\n",
583 			    __func__, seg_start, seg_end, context, core_context->core_segment_count - core_context->core_segments_remaining,
584 			    core_context->core_segment_count, core_context->core_outvars, sizeof(seg_command), &seg_command, ret);
585 			return ret;
586 		}
587 
588 		core_context->core_cur_hoffset += sizeof(seg_command);
589 	} else
590 #endif /* defined(__LP64__) */
591 	{
592 		struct segment_command seg_command = { };
593 
594 		if (seg_start > UINT32_MAX || seg_end > UINT32_MAX) {
595 			kern_coredump_log(context, "%s(0x%llx, 0x%llx, %p) : called with invalid addresses for 32-bit : start 0x%llx, end 0x%llx\n",
596 			    __func__, seg_start, seg_end, context, seg_start, seg_end);
597 			return KERN_INVALID_ARGUMENT;
598 		}
599 
600 		if (core_context->core_cur_hoffset + sizeof(seg_command) > core_context->core_header_size) {
601 			kern_coredump_log(context, "%s(0x%llx, 0x%llx, %p) : ran out of space to save commands with %llu of %llu remaining\n",
602 			    __func__, seg_start, seg_end, context, core_context->core_segments_remaining, core_context->core_segment_count);
603 			return KERN_NO_SPACE;
604 		}
605 
606 		seg_command.cmd = LC_SEGMENT;
607 		seg_command.cmdsize = sizeof(seg_command);
608 		seg_command.segname[0] = 0;
609 		seg_command.vmaddr = (uint32_t) seg_start;
610 		seg_command.vmsize = (uint32_t) size;
611 		seg_command.fileoff = (uint32_t) core_context->core_cur_foffset;
612 		seg_command.filesize = (uint32_t) size;
613 		seg_command.maxprot = VM_PROT_READ;
614 		seg_command.initprot = VM_PROT_READ;
615 
616 		/* Flush new command to output */
617 		ret = kdp_core_output(core_context->core_outvars, sizeof(seg_command), (caddr_t)&seg_command);
618 		if (ret != KERN_SUCCESS) {
619 			kern_coredump_log(context, "%s(0x%llx, 0x%llx, %p) : failed to write segment %llu of %llu : kdp_core_output(%p, %lu, %p) returned  error 0x%x\n",
620 			    __func__, seg_start, seg_end, context, core_context->core_segment_count - core_context->core_segments_remaining,
621 			    core_context->core_segment_count, core_context->core_outvars, sizeof(seg_command), &seg_command, ret);
622 			return ret;
623 		}
624 
625 		core_context->core_cur_hoffset += sizeof(seg_command);
626 	}
627 
628 	/* Update coredump context */
629 	core_context->core_segments_remaining--;
630 	core_context->core_cur_foffset += size;
631 
632 	return KERN_SUCCESS;
633 }
634 
635 /*
636  * Construct a LC_NOTE command for the specified note
637  */
638 static int
coredump_save_note_description(const char * data_owner,uint64_t length,void * context)639 coredump_save_note_description(const char * data_owner, uint64_t length, void *context)
640 {
641 	processor_core_context *core_context = (processor_core_context *)context;
642 	int ret;
643 
644 	if (data_owner == NULL || (strlen(data_owner) == 0)) {
645 		kern_coredump_log(context, "%s() called with invalid data_owner\n", __func__);
646 		return KERN_INVALID_ARGUMENT;
647 	}
648 
649 	if (core_context->core_notes_remaining == 0) {
650 		kern_coredump_log(context, "%s() called too many times, %llu note descriptions already recorded\n",
651 		    __func__, core_context->core_note_count);
652 		return KERN_INVALID_ARGUMENT;
653 	}
654 
655 	struct note_command note = { .cmd = LC_NOTE,
656 		                     .cmdsize = sizeof(struct note_command),
657 		                     .offset = core_context->core_cur_foffset,
658 		                     .size = length, };
659 	strlcpy((char *) &note.data_owner, data_owner, sizeof(note.data_owner));
660 
661 	/* Flush new command to output */
662 	ret = kdp_core_output(core_context->core_outvars, sizeof(note), (caddr_t)&note);
663 	if (ret != KERN_SUCCESS) {
664 		kern_coredump_log(context, "%s() : failed to write note %llu of %llu : kdp_core_output() returned  error 0x%x\n",
665 		    __func__, core_context->core_note_count - core_context->core_notes_remaining,
666 		    core_context->core_note_count, ret);
667 		return ret;
668 	}
669 
670 	/* Update coredump context */
671 	core_context->core_cur_foffset += length;
672 	core_context->core_cur_hoffset += sizeof(note);
673 	core_context->core_notes_remaining--;
674 
675 	return KERN_SUCCESS;
676 }
677 
678 /*
679  * Save thread state.
680  *
681  * Passed thread_state is expected to be a struct thread_command
682  */
683 static int
coredump_save_thread_state(void * thread_state,void * context)684 coredump_save_thread_state(void *thread_state, void *context)
685 {
686 	processor_core_context *core_context = (processor_core_context *)context;
687 	struct thread_command *tc = (struct thread_command *)thread_state;
688 	int ret;
689 
690 	if (tc->cmd != LC_THREAD) {
691 		kern_coredump_log(context, "%s() : found %d expected LC_THREAD (%d)\n", __func__, tc->cmd, LC_THREAD);
692 		return KERN_INVALID_ARGUMENT;
693 	}
694 
695 	if (core_context->core_cur_hoffset + core_context->core_thread_state_size > core_context->core_header_size) {
696 		kern_coredump_log(context, "%s() : ran out of space to save threads with %llu of %llu remaining\n", __func__,
697 		    core_context->core_threads_remaining, core_context->core_thread_count);
698 		return KERN_NO_SPACE;
699 	}
700 
701 	ret = kdp_core_output(core_context->core_outvars, core_context->core_thread_state_size, (caddr_t)thread_state);
702 	if (ret != KERN_SUCCESS) {
703 		kern_coredump_log(context, "%s() : failed to write thread data : kdp_core_output() returned 0x%x\n", __func__, ret);
704 		return ret;
705 	}
706 
707 	core_context->core_threads_remaining--;
708 	core_context->core_cur_hoffset += core_context->core_thread_state_size;
709 
710 	return KERN_SUCCESS;
711 }
712 
713 static int
coredump_save_segment_data(void * seg_data,uint64_t length,void * context)714 coredump_save_segment_data(void *seg_data, uint64_t length, void *context)
715 {
716 	int ret;
717 	processor_core_context *core_context = (processor_core_context *)context;
718 
719 	if (length > core_context->core_segment_bytes_remaining) {
720 		kern_coredump_log(context, "%s(%p, %llu, %p) : called with too much data, %llu written, %llu left\n", __func__,
721 		    seg_data, length, context, core_context->core_segment_byte_total - core_context->core_segment_bytes_remaining,
722 		    core_context->core_segment_bytes_remaining);
723 		return KERN_INVALID_ARGUMENT;
724 	}
725 
726 	ret = kdp_core_output(core_context->core_outvars, length, (caddr_t)seg_data);
727 	if (ret != KERN_SUCCESS) {
728 		kern_coredump_log(context, "%s() : failed to write data (%llu bytes remaining) :%d\n", __func__,
729 		    core_context->core_segment_bytes_remaining, ret);
730 		return ret;
731 	}
732 
733 	core_context->core_segment_bytes_remaining -= length;
734 	core_context->core_cur_foffset += length;
735 
736 	return KERN_SUCCESS;
737 }
738 
739 static int
coredump_save_note_data(void * note_data,uint64_t length,void * context)740 coredump_save_note_data(void *note_data, uint64_t length, void *context)
741 {
742 	int ret;
743 	processor_core_context *core_context = (processor_core_context *)context;
744 
745 	if (length > core_context->core_note_bytes_remaining) {
746 		kern_coredump_log(context, "%s(%p, %llu, %p) : called with too much data, %llu written, %llu left\n", __func__,
747 		    note_data, length, context, core_context->core_note_bytes_total - core_context->core_note_bytes_remaining,
748 		    core_context->core_note_bytes_remaining);
749 		return KERN_INVALID_ARGUMENT;
750 	}
751 
752 	ret = kdp_core_output(core_context->core_outvars, length, (caddr_t)note_data);
753 	if (ret != KERN_SUCCESS) {
754 		kern_coredump_log(context, "%s() : failed to write data (%llu bytes remaining) :%d\n", __func__,
755 		    core_context->core_note_bytes_remaining, ret);
756 		return ret;
757 	}
758 
759 	core_context->core_note_bytes_remaining -= length;
760 	core_context->core_cur_foffset += length;
761 
762 	return KERN_SUCCESS;
763 }
764 
765 static int
coredump_save_sw_vers_legacy(void * sw_vers,uint64_t length,void * context)766 coredump_save_sw_vers_legacy(void *sw_vers, uint64_t length, void *context)
767 {
768 	processor_core_context *core_context = (processor_core_context *)context;
769 	int ret;
770 
771 	if (length > KERN_COREDUMP_VERSIONSTRINGMAXSIZE || !length) {
772 		kern_coredump_log(context, "%s(%p, %llu, %p) : called with invalid length %llu\n", __func__,
773 		    sw_vers, length, context, length);
774 		return KERN_INVALID_ARGUMENT;
775 	}
776 
777 	uint32_t version = LEGACY_BIN_SPEC_VERSION;
778 	ret = coredump_save_note_data(&version, sizeof(version), context);
779 	if (ret != KERN_SUCCESS) {
780 		kern_coredump_log(context, "%s() : failed to write legacy bin spec version : coredump_save_note_data() returned 0x%x\n",
781 		    __func__, ret);
782 		return ret;
783 	}
784 
785 	ret = coredump_save_note_data(sw_vers, length, context);
786 	if (ret != KERN_SUCCESS) {
787 		kern_coredump_log(context, "%s() : failed to write sw_vers string : coredump_save_note_data() returned 0x%x\n",
788 		    __func__, ret);
789 		return ret;
790 	}
791 
792 	if (length < KERN_COREDUMP_VERSIONSTRINGMAXSIZE) {
793 		/* Zero fill to the full size */
794 		uint64_t length_to_zero = (KERN_COREDUMP_VERSIONSTRINGMAXSIZE - length);
795 		ret = kdp_core_output(core_context->core_outvars, length_to_zero, NULL);
796 		if (ret != KERN_SUCCESS) {
797 			kern_coredump_log(context, "%s() : failed to write zero fill padding : kdp_core_output(%p, %llu, NULL) returned 0x%x\n",
798 			    __func__, core_context->core_outvars, length_to_zero, ret);
799 			return ret;
800 		}
801 
802 		core_context->core_note_bytes_remaining -= length_to_zero;
803 		core_context->core_cur_foffset += length_to_zero;
804 	}
805 
806 	return KERN_SUCCESS;
807 }
808 
809 static int
coredump_save_sw_vers(uint64_t address,uuid_t uuid,uint32_t log2_pagesize,void * context)810 coredump_save_sw_vers(uint64_t address, uuid_t uuid, uint32_t log2_pagesize, void *context)
811 {
812 	processor_core_context *core_context = (processor_core_context *)context;
813 	int ret;
814 
815 	uint32_t type = bin_spec_map[core_context->core_type];
816 	main_bin_spec_note_t spec = {
817 		.version = MAIN_BIN_SPEC_VERSION,
818 		.type = type,
819 		.address = address,
820 		.log2_pagesize = log2_pagesize,
821 	};
822 	uuid_copy(*((uuid_t *)&spec.uuid), uuid);
823 
824 	ret = coredump_save_note_data(&spec, sizeof(spec), context);
825 	if (ret != KERN_SUCCESS) {
826 		kern_coredump_log(context, "%s() : failed to write main bin spec structure : coredump_save_note_data() returned 0x%x\n", __func__, ret);
827 		return ret;
828 	}
829 
830 	return KERN_SUCCESS;
831 }
832 
833 static kern_return_t
kern_coredump_routine(void * core_outvars,struct kern_coredump_core * current_core,uint64_t core_begin_offset,uint64_t * core_file_length,boolean_t * header_update_failed,kern_coredump_type_t type,uint64_t details_flags)834 kern_coredump_routine(void *core_outvars, struct kern_coredump_core *current_core, uint64_t core_begin_offset, uint64_t *core_file_length, boolean_t *header_update_failed, kern_coredump_type_t type, uint64_t details_flags)
835 {
836 #if CONFIG_CPU_COUNTERS
837 	uint64_t start_cycles;
838 	uint64_t end_cycles;
839 #endif // CONFIG_CPU_COUNTERS
840 	kern_return_t ret;
841 	processor_core_context context = { };
842 	*core_file_length = 0;
843 	*header_update_failed = FALSE;
844 
845 #if CONFIG_CPU_COUNTERS
846 	start_cycles = mt_cur_cpu_cycles();
847 #endif // CONFIG_CPU_COUNTERS
848 
849 	/* Setup the coredump context */
850 	context.core_outvars = core_outvars;
851 	context.core_config = &current_core->kcc_cb;
852 	context.core_refcon = current_core->kcc_refcon;
853 	context.core_is64bit = current_core->kcc_is64bit;
854 	context.core_mh_magic = current_core->kcc_mh_magic;
855 	context.core_cpu_type = current_core->kcc_cpu_type;
856 	context.core_cpu_subtype = current_core->kcc_cpu_subtype;
857 	context.core_type = type;
858 
859 	kern_coredump_log(&context, "\nBeginning coredump of %s\n", current_core->kcc_corename);
860 
861 	if (current_core->kcc_cb.kcc_coredump_init != NULL) {
862 		ret = current_core->kcc_cb.kcc_coredump_init(context.core_refcon, &context);
863 		if (ret == KERN_NODE_DOWN) {
864 			kern_coredump_log(&context, "coredump_init returned KERN_NODE_DOWN, skipping this core\n");
865 			return KERN_SUCCESS;
866 		} else if (ret != KERN_SUCCESS) {
867 			kern_coredump_log(&context, "(%s) : coredump_init failed with %d\n", __func__, ret);
868 			return ret;
869 		}
870 	}
871 
872 	/* Retrieve information about LC_NOTE data we will write out as part of the core before we populate the general header */
873 	if (current_core->kcc_cb.kcc_coredump_save_note_summary != NULL) {
874 		ret = current_core->kcc_cb.kcc_coredump_save_note_summary(context.core_refcon, coredump_save_note_summary, &context);
875 		if (ret != KERN_SUCCESS) {
876 			kern_coredump_log(&context, "(%s) : save_note_note_summary failed with %d\n", __func__, ret);
877 			return ret;
878 		}
879 	}
880 
881 	/* Populate the context with metadata about the corefile (cmd info, sizes etc) */
882 	ret = current_core->kcc_cb.kcc_coredump_get_summary(context.core_refcon, coredump_save_summary, &context);
883 	if (ret != KERN_SUCCESS) {
884 		kern_coredump_log(&context, "(%s) : get_summary failed with %d\n", __func__, ret);
885 		return ret;
886 	}
887 
888 	if (context.core_should_be_skipped) {
889 		kern_coredump_log(&context, "Skipping coredump\n");
890 		return KERN_SUCCESS;
891 	}
892 
893 	if (context.core_header_size == 0) {
894 		kern_coredump_log(&context, "(%s) : header size not populated after coredump_get_summary\n", __func__);
895 		return KERN_FAILURE;
896 	}
897 
898 	/* Save the segment descriptions for the segments to be included */
899 	ret = current_core->kcc_cb.kcc_coredump_save_segment_descriptions(context.core_refcon, coredump_save_segment_descriptions,
900 	    &context);
901 	if (ret != KERN_SUCCESS) {
902 		kern_coredump_log(&context, "(%s) : save_segment_descriptions failed with %d\n", __func__, ret);
903 		return ret;
904 	}
905 
906 	if (context.core_segments_remaining != 0) {
907 		kern_coredump_log(&context, "(%s) : save_segment_descriptions returned without all segment descriptions written, %llu of %llu remaining\n",
908 		    __func__, context.core_segments_remaining, context.core_segment_count);
909 		return KERN_FAILURE;
910 	}
911 
912 	/* write out the LC_NOTE with the binary info */
913 	if (current_core->kcc_cb.kcc_coredump_save_sw_vers_detail != NULL) {
914 		ret = coredump_save_note_description(MAIN_BIN_SPEC_DATA_OWNER, sizeof(main_bin_spec_note_t), &context);
915 	} else {
916 		ret = coredump_save_note_description(DATA_OWNER_LEGACY_BIN_SPEC, sizeof(legacy_bin_spec), &context);
917 	}
918 	if (ret != KERN_SUCCESS) {
919 		kern_coredump_log(&context, "(%s) : coredump_save_note_description returned %d while writing binary info LC_NOTE description", __func__, ret);
920 		return ret;
921 	}
922 
923 	/* Save LC_NOTE desciptions for any additional notes to be included */
924 	if (current_core->kcc_cb.kcc_coredump_save_note_descriptions != NULL) {
925 		ret = current_core->kcc_cb.kcc_coredump_save_note_descriptions(context.core_refcon, coredump_save_note_description, &context);
926 		if (ret != KERN_SUCCESS) {
927 			kern_coredump_log(&context, "(%s) : kcc_coredump_save_note_descriptions failed with %d\n", __func__, ret);
928 			return ret;
929 		}
930 	}
931 
932 	if (context.core_notes_remaining != 0) {
933 		kern_coredump_log(&context, "(%s) : save_note_descriptions returned without all note descriptions written, %llu of %llu remaining\n",
934 		    __func__, context.core_notes_remaining, context.core_note_count);
935 		return KERN_FAILURE;
936 	}
937 
938 	/*
939 	 * Save the thread commands/state
940 	 *
941 	 * TODO: Should this buffer be allocated at boot rather than on the stack?
942 	 */
943 	if (context.core_thread_state_size) {
944 		char threadstatebuf[context.core_thread_state_size];
945 		ret = current_core->kcc_cb.kcc_coredump_save_thread_state(context.core_refcon, &threadstatebuf, coredump_save_thread_state,
946 		    &context);
947 		if (ret != KERN_SUCCESS) {
948 			kern_coredump_log(&context, "(%s) : save_thread_state failed with %d\n", __func__, ret);
949 			return ret;
950 		}
951 	}
952 
953 	if (context.core_threads_remaining != 0) {
954 		kern_coredump_log(&context, "(%s) : save_thread_state returned without all thread descriptions written, %llu of %llu remaining\n",
955 		    __func__, context.core_threads_remaining, context.core_thread_count);
956 		return KERN_FAILURE;
957 	}
958 	assert(context.core_cur_hoffset == context.core_header_size);
959 
960 	/* Zero fill between the end of the header and the beginning of the segment data file offset */
961 	ret = kdp_core_output(context.core_outvars, (round_page(context.core_header_size) - context.core_header_size), NULL);
962 	if (ret != KERN_SUCCESS) {
963 		kern_coredump_log(&context, "(kern_coredump_routine) : failed to write zero fill padding (%llu bytes remaining) : kdp_core_output(%p, %llu, NULL) returned 0x%x\n",
964 		    context.core_segment_bytes_remaining, context.core_outvars, (round_page(context.core_header_size) - context.core_header_size), ret);
965 		return ret;
966 	}
967 
968 	/* Reset our local current file offset before we start writing out segment data */
969 	context.core_cur_foffset = round_page(context.core_header_size);
970 
971 	ret = current_core->kcc_cb.kcc_coredump_save_segment_data(context.core_refcon, coredump_save_segment_data, &context);
972 	if (ret != KERN_SUCCESS) {
973 		kern_coredump_log(&context, "coredump_save_segment_data failed with %d\n", ret);
974 		return ret;
975 	}
976 
977 	if (context.core_segment_bytes_remaining != 0) {
978 		kern_coredump_log(&context, "(kern_coredump_routine) : save_segment_data returned without all segment data written, %llu of %llu remaining\n",
979 		    context.core_segment_bytes_remaining, context.core_segment_byte_total);
980 		return KERN_FAILURE;
981 	}
982 
983 	/* Save out the LC_NOTE segment data, starting with the binary info / sw vers one */
984 	if (current_core->kcc_cb.kcc_coredump_save_sw_vers_detail != NULL) {
985 		ret = current_core->kcc_cb.kcc_coredump_save_sw_vers_detail(context.core_refcon, coredump_save_sw_vers, &context);
986 		if (ret != KERN_SUCCESS) {
987 			kern_coredump_log(&context, "(%s) : kcc_coredump_save_sw_vers_detail_cb failed with 0x%x\n", __func__, ret);
988 			return ret;
989 		}
990 	} else {
991 #pragma clang diagnostic push
992 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
993 		ret = current_core->kcc_cb.kcc_coredump_save_sw_vers(context.core_refcon, coredump_save_sw_vers_legacy, &context);
994 #pragma clang diagnostic pop
995 		if (ret != KERN_SUCCESS) {
996 			kern_coredump_log(&context, "(%s) : kcc_coredump_save_sw_vers failed with 0x%x\n", __func__, ret);
997 			return ret;
998 		}
999 	}
1000 
1001 	if (current_core->kcc_cb.kcc_coredump_save_note_data != NULL) {
1002 		ret = current_core->kcc_cb.kcc_coredump_save_note_data(context.core_refcon, coredump_save_note_data, &context);
1003 		if (ret != KERN_SUCCESS) {
1004 			kern_coredump_log(&context, "(%s) : kcc_coredump_save_note_data failed with 0x%x\n", __func__, ret);
1005 			return ret;
1006 		}
1007 	}
1008 
1009 	if (context.core_note_bytes_remaining != 0) {
1010 		kern_coredump_log(&context, "(%s) : kcc_coredump_save_note_data returned without all note data written, %llu of %llu remaining\n",
1011 		    __func__, context.core_note_bytes_remaining, context.core_note_bytes_total);
1012 		return KERN_FAILURE;
1013 	}
1014 
1015 
1016 	/* Flush the last data out */
1017 	ret = kdp_core_output(context.core_outvars, 0, NULL);
1018 	if (ret != KERN_SUCCESS) {
1019 		kern_coredump_log(&context, "(kern_coredump_routine) : failed to flush final core data : kdp_core_output(%p, 0, NULL) returned 0x%x\n",
1020 		    context.core_outvars, ret);
1021 		return ret;
1022 	}
1023 
1024 	kern_coredump_log(&context, "Done\nCoredump complete of %s, dumped %llu segments (%llu bytes), %llu threads (%llu bytes) overall uncompressed file length %llu bytes.",
1025 	    current_core->kcc_corename, context.core_segment_count, context.core_segment_byte_total, context.core_thread_count,
1026 	    (context.core_thread_count * context.core_thread_state_size), context.core_file_length);
1027 
1028 #if CONFIG_CPU_COUNTERS
1029 	end_cycles = mt_cur_cpu_cycles();
1030 	kern_coredump_log(&context, "\nCore dump took %llu cycles\n", end_cycles - start_cycles);
1031 #endif // CONFIG_CPU_COUNTERS
1032 
1033 	if (core_begin_offset) {
1034 		/* If we're writing to disk (we have a begin offset), we need to update the header */
1035 		ret = kern_dump_record_file(context.core_outvars, current_core->kcc_corename, core_begin_offset, &context.core_file_length_compressed, details_flags);
1036 		if (ret != KERN_SUCCESS) {
1037 			*header_update_failed = TRUE;
1038 			kern_coredump_log(&context, "\n(kern_coredump_routine) : kern_dump_record_file failed with %d\n", ret);
1039 			return ret;
1040 		}
1041 	}
1042 
1043 	kern_coredump_log(&context, " Compressed file length is %llu bytes\n", context.core_file_length_compressed);
1044 
1045 	*core_file_length = context.core_file_length_compressed;
1046 
1047 	return KERN_SUCCESS;
1048 }
1049 
1050 /*
1051  * Collect coprocessor and userspace coredumps
1052  */
1053 static kern_return_t
kern_do_auxiliary_coredump(void * core_outvars,struct kern_coredump_core * list,uint64_t * last_file_offset,uint64_t details_flags)1054 kern_do_auxiliary_coredump(void * core_outvars, struct kern_coredump_core * list, uint64_t * last_file_offset, uint64_t details_flags)
1055 {
1056 	struct kern_coredump_core *current_core = list;
1057 	uint64_t prev_core_length = 0;
1058 	boolean_t header_update_failed = FALSE;
1059 	kern_coredump_type_t type = current_core == kern_userspace_coredump_core_list ? USERSPACE_COREDUMP : COPROCESSOR_COREDUMP;
1060 	kern_return_t ret = KERN_SUCCESS;
1061 	kern_return_t cur_ret = KERN_SUCCESS;
1062 
1063 	if (type == USERSPACE_COREDUMP && kdp_lck_mtx_lock_spin_is_acquired(&kern_userspace_coredump_core_list_lock)) {
1064 		// Userspace coredump list was being modified at the time of the panic. Skip collecting userspace coredumps
1065 		kern_coredump_log(NULL, "Skipping userspace coredump, coredump list is locked\n");
1066 		return KERN_FAILURE;
1067 	}
1068 
1069 	while (current_core) {
1070 		/* Seek to the beginning of the next file */
1071 		cur_ret = kern_dump_seek_to_next_file(core_outvars, *last_file_offset);
1072 		if (cur_ret != KERN_SUCCESS) {
1073 			kern_coredump_log(NULL, "Failed to seek to beginning of next core\n");
1074 			return KERN_FAILURE;
1075 		}
1076 
1077 		cur_ret = kern_coredump_routine(core_outvars, current_core, *last_file_offset, &prev_core_length, &header_update_failed, type, details_flags);
1078 		if (cur_ret != KERN_SUCCESS) {
1079 			// As long as we didn't fail while updating the header for the raw file, we should be able to try
1080 			// to capture other corefiles.
1081 			if (header_update_failed) {
1082 				// The header may be in an inconsistent state, so bail now
1083 				return KERN_FAILURE;
1084 			} else {
1085 				// Try to capture other corefiles even if one failed, update the overall return
1086 				// status though
1087 				prev_core_length = 0;
1088 				ret = KERN_FAILURE;
1089 			}
1090 		}
1091 
1092 		/* Calculate the offset of the beginning of the next core in the raw file */
1093 		*last_file_offset = roundup(((*last_file_offset) + prev_core_length), KERN_COREDUMP_BEGIN_FILEBYTES_ALIGN);
1094 		prev_core_length = 0;
1095 		current_core = current_core->kcc_next;
1096 	}
1097 
1098 	return ret;
1099 }
1100 
1101 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)1102 kern_do_coredump(void *core_outvars, boolean_t kernel_only, uint64_t first_file_offset, uint64_t *last_file_offset, uint64_t details_flags)
1103 {
1104 	uint64_t prev_core_length = 0;
1105 	kern_return_t cur_ret = KERN_SUCCESS, ret = KERN_SUCCESS;
1106 	boolean_t header_update_failed = FALSE;
1107 
1108 	assert(last_file_offset != NULL);
1109 
1110 
1111 	*last_file_offset = first_file_offset;
1112 	cur_ret = kern_coredump_routine(core_outvars, kernel_helper, *last_file_offset, &prev_core_length, &header_update_failed, XNU_COREDUMP, details_flags);
1113 
1114 
1115 	if (cur_ret != KERN_SUCCESS) {
1116 		// As long as we didn't fail while updating the header for the raw file, we should be able to try
1117 		// to capture other corefiles.
1118 		if (header_update_failed) {
1119 			// The header may be in an inconsistent state, so bail now
1120 			return KERN_FAILURE;
1121 		} else {
1122 			prev_core_length = 0;
1123 			ret = KERN_FAILURE;
1124 		}
1125 	}
1126 
1127 	*last_file_offset = roundup(((*last_file_offset) + prev_core_length), KERN_COREDUMP_BEGIN_FILEBYTES_ALIGN);
1128 
1129 	if (kernel_only) {
1130 		return ret;
1131 	}
1132 
1133 	/* Dump secure kernel if allowed */
1134 	if (sk_helper) {
1135 		/* Seek to the beginning of next file. */
1136 		cur_ret = kern_dump_seek_to_next_file(core_outvars, *last_file_offset);
1137 		if (cur_ret != KERN_SUCCESS) {
1138 			kern_coredump_log(NULL, "secure_core: Unable to seek to the start of file: %d\n", cur_ret);
1139 			return KERN_FAILURE;
1140 		}
1141 
1142 		/* Dump the secure core to disk. */
1143 		cur_ret = kern_coredump_routine(core_outvars, sk_helper, *last_file_offset, &prev_core_length, &header_update_failed, SECURE_COREDUMP, details_flags);
1144 		if (cur_ret != KERN_SUCCESS) {
1145 			if (header_update_failed) {
1146 				return KERN_FAILURE;
1147 			} else {
1148 				prev_core_length = 0;
1149 				ret = KERN_FAILURE;
1150 			}
1151 		}
1152 
1153 		*last_file_offset = roundup(((*last_file_offset) + prev_core_length), KERN_COREDUMP_BEGIN_FILEBYTES_ALIGN);
1154 	}
1155 
1156 	// Collect coprocessor coredumps first, in case userspace coredumps fail
1157 	ret = kern_do_auxiliary_coredump(core_outvars, kern_coredump_core_list, last_file_offset, details_flags);
1158 	if (ret != KERN_SUCCESS) {
1159 		kern_coredump_log(NULL, "Failed to dump coprocessor cores\n");
1160 		return ret;
1161 	}
1162 
1163 	ret = kern_do_auxiliary_coredump(core_outvars, kern_userspace_coredump_core_list, last_file_offset, details_flags);
1164 	if (ret != KERN_SUCCESS) {
1165 		kern_coredump_log(NULL, "Failed to dump userspace process cores\n");
1166 		return ret;
1167 	}
1168 
1169 	return KERN_SUCCESS;
1170 }
1171 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
1172 
1173 kern_return_t
kern_register_coredump_helper(int kern_coredump_config_vers,const kern_coredump_callback_config * kc_callbacks,void * refcon,const char * core_description,boolean_t is64bit,uint32_t mh_magic,cpu_type_t cpu_type,cpu_subtype_t cpu_subtype)1174 kern_register_coredump_helper(int kern_coredump_config_vers, const kern_coredump_callback_config *kc_callbacks, void* refcon,
1175     const char *core_description, boolean_t is64bit, uint32_t mh_magic,
1176     cpu_type_t cpu_type, cpu_subtype_t cpu_subtype)
1177 {
1178 #pragma unused(kern_coredump_config_vers, kc_callbacks, refcon, core_description, is64bit, mh_magic, cpu_type, cpu_subtype)
1179 	return KERN_NOT_SUPPORTED;
1180 }
1181 
1182 kern_return_t
kern_register_sk_coredump_helper(__unused kern_coredump_callback_config * sk_callbacks,__unused void * refcon)1183 kern_register_sk_coredump_helper(__unused kern_coredump_callback_config *sk_callbacks, __unused void *refcon)
1184 {
1185 	return KERN_NOT_SUPPORTED;
1186 }
1187 
1188 kern_return_t
kern_register_userspace_coredump(task_t task,const char * name)1189 kern_register_userspace_coredump(task_t task, const char * name)
1190 {
1191 	(void)task;
1192 	(void)name;
1193 	return KERN_NOT_SUPPORTED;
1194 }
1195 
1196 kern_return_t
kern_unregister_userspace_coredump(task_t task)1197 kern_unregister_userspace_coredump(task_t task)
1198 {
1199 	(void)task;
1200 	return KERN_NOT_SUPPORTED;
1201 }
1202 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
1203 
1204 /*
1205  * Must be callable with a NULL context
1206  */
1207 void
kern_coredump_log(void * context,const char * string,...)1208 kern_coredump_log(void *context, const char *string, ...)
1209 {
1210 #pragma unused(context)
1211 	va_list coredump_log_args;
1212 
1213 	va_start(coredump_log_args, string);
1214 	_doprnt(string, &coredump_log_args, consdebug_putc, 16);
1215 	va_end(coredump_log_args);
1216 
1217 #if defined(__arm64__)
1218 	paniclog_flush();
1219 #endif
1220 }
1221