xref: /xnu-12377.81.4/osfmk/kdp/output_stages/out_buffer.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2021 Apple 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 #ifdef CONFIG_KDP_INTERACTIVE_DEBUGGING
30 
31 #include <mach/mach_types.h>
32 #include <IOKit/IOTypes.h>
33 #include <kdp/output_stages/output_stages.h>
34 #include <kdp/kdp_core.h>
35 #include <kdp/processor_core.h>
36 #include <vm/vm_kern_xnu.h>
37 
38 struct buffer_stage_data {
39 	size_t total_buffer_size;
40 	size_t current_size;
41 	char   buffer[];
42 };
43 
44 static kern_return_t
buffer_stage_reset(struct kdp_output_stage * stage,__unused const char * corename,__unused kern_coredump_type_t coretype)45 buffer_stage_reset(struct kdp_output_stage *stage, __unused const char *corename, __unused kern_coredump_type_t coretype)
46 {
47 	struct buffer_stage_data *data = (struct buffer_stage_data *) stage->kos_data;
48 
49 	data->current_size = 0;
50 	stage->kos_bypass = false;
51 	stage->kos_bytes_written = 0;
52 
53 	return KERN_SUCCESS;
54 }
55 
56 static kern_return_t
buffer_stage_flush(struct kdp_output_stage * stage)57 buffer_stage_flush(struct kdp_output_stage *stage)
58 {
59 	kern_return_t err = KERN_SUCCESS;
60 	struct buffer_stage_data *data = (struct buffer_stage_data *) stage->kos_data;
61 	struct kdp_output_stage *next_stage = STAILQ_NEXT(stage, kos_next);
62 
63 	err = next_stage->kos_funcs.kosf_outproc(next_stage, KDP_DATA, NULL, data->current_size, data->buffer);
64 
65 	if (KERN_SUCCESS != err) {
66 		return err;
67 	} else {
68 		stage->kos_bytes_written += data->current_size;
69 		data->current_size = 0;
70 	}
71 
72 	return err;
73 }
74 
75 static kern_return_t
buffer_stage_outproc(struct kdp_output_stage * stage,unsigned int request,char * corename,uint64_t length,void * panic_data)76 buffer_stage_outproc(struct kdp_output_stage *stage, unsigned int request,
77     char *corename, uint64_t length, void * panic_data)
78 {
79 	kern_return_t err = KERN_SUCCESS;
80 	struct buffer_stage_data *data = (struct buffer_stage_data *) stage->kos_data;
81 	struct kdp_output_stage  *next_stage = STAILQ_NEXT(stage, kos_next);
82 
83 	boolean_t should_flush = FALSE;
84 
85 	assert(next_stage != NULL);
86 
87 	if ((data->current_size && (request == KDP_SEEK || request == KDP_FLUSH || request == KDP_EOF))
88 	    || (request == KDP_DATA && length == 0 && !panic_data)) {
89 		should_flush = TRUE;
90 	}
91 
92 	if (should_flush) {
93 		err = buffer_stage_flush(stage);
94 		if (KERN_SUCCESS != err) {
95 			kern_coredump_log(NULL, "buffer_stage_outproc (during flush) returned 0x%x\n", err);
96 			return err;
97 		}
98 	}
99 
100 	if (request == KDP_WRQ || request == KDP_SEEK || request == KDP_EOF) {
101 		err = next_stage->kos_funcs.kosf_outproc(next_stage, request, corename, length, panic_data);
102 
103 		if (KERN_SUCCESS != err) {
104 			kern_coredump_log(NULL, "buffer_stage_outproc (during forwarding) returned 0x%x\n", err);
105 			return err;
106 		}
107 	} else if (request == KDP_DATA) {
108 		while (length != 0) {
109 			size_t bytes_to_copy = data->total_buffer_size - data->current_size;
110 
111 			if (length < bytes_to_copy) {
112 				/* Safe to cast to size_t here since we just checked that 'length' is less
113 				 * than a size_t value. */
114 				bytes_to_copy = (size_t) length;
115 			}
116 
117 			bcopy(panic_data, (void *)((uintptr_t)data->buffer + data->current_size), bytes_to_copy);
118 
119 			data->current_size += bytes_to_copy;
120 			length -= bytes_to_copy;
121 			panic_data = (void *) ((uintptr_t) panic_data + bytes_to_copy);
122 
123 			if (data->current_size == data->total_buffer_size) {
124 				err = buffer_stage_flush(stage);
125 				if (KERN_SUCCESS != err) {
126 					kern_coredump_log(NULL, "buffer_stage_outproc (during flush) returned 0x%x\n", err);
127 					return err;
128 				}
129 			}
130 		}
131 	}
132 
133 	return err;
134 }
135 
136 static void
buffer_stage_free(struct kdp_output_stage * stage)137 buffer_stage_free(struct kdp_output_stage *stage)
138 {
139 	kmem_free(kernel_map, (vm_offset_t) stage->kos_data, stage->kos_data_size);
140 
141 	stage->kos_data = NULL;
142 	stage->kos_data_size = 0;
143 	stage->kos_initialized = false;
144 }
145 
146 kern_return_t
buffer_stage_initialize(struct kdp_output_stage * stage,size_t buffer_size)147 buffer_stage_initialize(struct kdp_output_stage *stage, size_t buffer_size)
148 {
149 	kern_return_t ret = KERN_SUCCESS;
150 	struct buffer_stage_data *data = NULL;
151 
152 	assert(stage != NULL);
153 	assert(stage->kos_initialized == false);
154 	assert(stage->kos_data == NULL);
155 	assert(buffer_size != 0);
156 
157 	stage->kos_data_size = sizeof(struct buffer_stage_data) + buffer_size;
158 	ret = kmem_alloc(kernel_map, (vm_offset_t*) &stage->kos_data, stage->kos_data_size,
159 	    KMA_DATA_SHARED, VM_KERN_MEMORY_DIAG);
160 	if (KERN_SUCCESS != ret) {
161 		printf("buffer_stage_initialize failed to allocate memory. Error 0x%x\n", ret);
162 		return ret;
163 	}
164 
165 	data = (struct buffer_stage_data *) stage->kos_data;
166 	data->total_buffer_size = buffer_size;
167 	data->current_size = 0;
168 
169 	stage->kos_funcs.kosf_reset = buffer_stage_reset;
170 	stage->kos_funcs.kosf_outproc = buffer_stage_outproc;
171 	stage->kos_funcs.kosf_free = buffer_stage_free;
172 
173 	stage->kos_initialized = true;
174 
175 	return KERN_SUCCESS;
176 }
177 
178 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
179