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