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