1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** ThreadX Component */
16 /** */
17 /** Module Manager */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22
23 /* Include necessary system files. */
24
25 #define TX_SOURCE_CODE
26
27 #include "txm_module.h"
28 #include "txm_module_manager_util.h"
29
30
31 /**************************************************************************/
32 /* */
33 /* FUNCTION RELEASE */
34 /* */
35 /* _txm_module_manager_object_memory_check PORTABLE C */
36 /* 6.1 */
37 /* AUTHOR */
38 /* */
39 /* Scott Larson, Microsoft Corporation */
40 /* */
41 /* DESCRIPTION */
42 /* */
43 /* This function checks if the object is inside a module's object pool */
44 /* or, if it's a privileged module, inside the module's data area. */
45 /* */
46 /* INPUT */
47 /* */
48 /* module_instance Module instance that the object */
49 /* belongs to */
50 /* object_ptr Pointer to object to check */
51 /* object_size Size of the object to check */
52 /* */
53 /* OUTPUT */
54 /* */
55 /* status Whether the object resides in a */
56 /* valid location */
57 /* */
58 /* CALLS */
59 /* */
60 /* None */
61 /* */
62 /* CALLED BY */
63 /* */
64 /* _txm_module_manager_kernel_dispatch Kernel dispatch function */
65 /* */
66 /* RELEASE HISTORY */
67 /* */
68 /* DATE NAME DESCRIPTION */
69 /* */
70 /* 09-30-2020 Scott Larson Initial Version 6.1 */
71 /* */
72 /**************************************************************************/
_txm_module_manager_object_memory_check(TXM_MODULE_INSTANCE * module_instance,ALIGN_TYPE object_ptr,ULONG object_size)73 UINT _txm_module_manager_object_memory_check(TXM_MODULE_INSTANCE *module_instance, ALIGN_TYPE object_ptr, ULONG object_size)
74 {
75
76 /* Is the object pointer from the module manager's object pool? */
77 if ((_txm_module_manager_object_pool_created == TX_TRUE) &&
78 (object_ptr >= (ALIGN_TYPE) _txm_module_manager_object_pool.tx_byte_pool_start) &&
79 ((object_ptr+object_size) <= (ALIGN_TYPE) (_txm_module_manager_object_pool.tx_byte_pool_start + _txm_module_manager_object_pool.tx_byte_pool_size)))
80 {
81 /* Object is from manager object pool. */
82 return(TX_SUCCESS);
83 }
84
85 /* If memory protection is not required, check if object is in module data. */
86 else if (!(module_instance -> txm_module_instance_property_flags & TXM_MODULE_MEMORY_PROTECTION))
87 {
88 if ((object_ptr >= (ALIGN_TYPE) module_instance -> txm_module_instance_data_start) &&
89 ((object_ptr+object_size) <= (ALIGN_TYPE) module_instance -> txm_module_instance_data_end))
90 {
91 /* Object is from the local module memory. */
92 return(TX_SUCCESS);
93 }
94 }
95
96 /* Object is from invalid memory. */
97 return(TXM_MODULE_INVALID_MEMORY);
98
99 }
100
101
102 /**************************************************************************/
103 /* */
104 /* FUNCTION RELEASE */
105 /* */
106 /* _txm_module_manager_created_object_check PORTABLE C */
107 /* 6.1 */
108 /* AUTHOR */
109 /* */
110 /* Scott Larson, Microsoft Corporation */
111 /* */
112 /* DESCRIPTION */
113 /* */
114 /* This functions checks if the specified object was created by the */
115 /* specified module */
116 /* */
117 /* INPUT */
118 /* */
119 /* module_instance The module instance to check */
120 /* object_ptr The object to check */
121 /* */
122 /* OUTPUT */
123 /* */
124 /* status Whether the module created the */
125 /* object */
126 /* */
127 /* CALLS */
128 /* */
129 /* None */
130 /* */
131 /* CALLED BY */
132 /* */
133 /* txm_module_manager*_stop Module manager stop functions */
134 /* */
135 /* RELEASE HISTORY */
136 /* */
137 /* DATE NAME DESCRIPTION */
138 /* */
139 /* 09-30-2020 Scott Larson Initial Version 6.1 */
140 /* */
141 /**************************************************************************/
_txm_module_manager_created_object_check(TXM_MODULE_INSTANCE * module_instance,VOID * object_ptr)142 UCHAR _txm_module_manager_created_object_check(TXM_MODULE_INSTANCE *module_instance, VOID *object_ptr)
143 {
144
145 TXM_MODULE_ALLOCATED_OBJECT *allocated_object_ptr;
146
147 /* Determine if the socket control block is inside the module. */
148 if ( (((CHAR *) object_ptr) >= ((CHAR *) module_instance -> txm_module_instance_data_start)) &&
149 (((CHAR *) object_ptr) < ((CHAR *) module_instance -> txm_module_instance_data_end)))
150 {
151 return TX_TRUE;
152 }
153
154 /* Determine if this object control block was allocated by this module instance. */
155 else if (_txm_module_manager_object_pool_created)
156 {
157
158 /* Determine if the current object is from the pool of dynamically allocated objects. */
159 if ((((UCHAR *) object_ptr) >= _txm_module_manager_object_pool.tx_byte_pool_start) &&
160 (((UCHAR *) object_ptr) < (_txm_module_manager_object_pool.tx_byte_pool_start + _txm_module_manager_object_pool.tx_byte_pool_size)))
161 {
162
163 /* Pickup object pointer. */
164 allocated_object_ptr = (TXM_MODULE_ALLOCATED_OBJECT *) object_ptr;
165
166 /* Move back to get the header information. */
167 allocated_object_ptr--;
168
169 /* Now determine if this object belongs to this module. */
170 if (allocated_object_ptr -> txm_module_allocated_object_module_instance == module_instance)
171 {
172 return TX_TRUE;
173 }
174 }
175 }
176
177 return TX_FALSE;
178 }
179
180
181 /**************************************************************************/
182 /* */
183 /* FUNCTION RELEASE */
184 /* */
185 /* _txm_module_manager_object_size_check PORTABLE C */
186 /* 6.1 */
187 /* AUTHOR */
188 /* */
189 /* Scott Larson, Microsoft Corporation */
190 /* */
191 /* DESCRIPTION */
192 /* */
193 /* This function checks if the specified object's size matches what is */
194 /* inside the object pool. */
195 /* */
196 /* INPUT */
197 /* */
198 /* object_ptr Pointer to object to check */
199 /* object_size Size of the object to check */
200 /* */
201 /* OUTPUT */
202 /* */
203 /* status Whether the object's size matches */
204 /* what's inside the object pool */
205 /* */
206 /* CALLS */
207 /* */
208 /* None */
209 /* */
210 /* CALLED BY */
211 /* */
212 /* _txm_module_manager_kernel_dispatch Kernel dispatch function */
213 /* */
214 /* RELEASE HISTORY */
215 /* */
216 /* DATE NAME DESCRIPTION */
217 /* */
218 /* 09-30-2020 Scott Larson Initial Version 6.1 */
219 /* */
220 /**************************************************************************/
_txm_module_manager_object_size_check(ALIGN_TYPE object_ptr,ULONG object_size)221 UINT _txm_module_manager_object_size_check(ALIGN_TYPE object_ptr, ULONG object_size)
222 {
223 TXM_MODULE_ALLOCATED_OBJECT *module_allocated_object_ptr;
224 UINT return_value;
225
226 /* Pickup the allocated object pointer. */
227 module_allocated_object_ptr = ((TXM_MODULE_ALLOCATED_OBJECT *) object_ptr) - 1;
228
229 /* Does the allocated memory match the expected object size? */
230 if (module_allocated_object_ptr -> txm_module_object_size == object_size)
231 return_value = TX_SUCCESS;
232 else
233 return_value = TXM_MODULE_INVALID_MEMORY;
234
235 return(return_value);
236 }
237
238
239 /**************************************************************************/
240 /* */
241 /* FUNCTION RELEASE */
242 /* */
243 /* _txm_module_manager_name_compare PORTABLE C */
244 /* 6.1 */
245 /* AUTHOR */
246 /* */
247 /* Scott Larson, Microsoft Corporation */
248 /* */
249 /* DESCRIPTION */
250 /* */
251 /* This function compares the specified object names. */
252 /* */
253 /* INPUT */
254 /* */
255 /* search_name String pointer to the object's */
256 /* name being searched for */
257 /* search_name_length Length of search_name */
258 /* object_name String pointer to an object's name*/
259 /* to compare the search name to */
260 /* */
261 /* OUTPUT */
262 /* */
263 /* status Whether the names are equal */
264 /* */
265 /* CALLS */
266 /* */
267 /* None */
268 /* */
269 /* CALLED BY */
270 /* */
271 /* *_object_pointer_get Kernel dispatch function */
272 /* */
273 /* RELEASE HISTORY */
274 /* */
275 /* DATE NAME DESCRIPTION */
276 /* */
277 /* 09-30-2020 Scott Larson Initial Version 6.1 */
278 /* */
279 /**************************************************************************/
_txm_module_manager_object_name_compare(CHAR * search_name,UINT search_name_length,CHAR * object_name)280 UINT _txm_module_manager_object_name_compare(CHAR *search_name, UINT search_name_length, CHAR *object_name)
281 {
282
283 CHAR search_name_char;
284 CHAR object_name_char;
285
286
287 /* Is the object name null? Note that the search name has already been checked
288 by the caller. */
289 if (object_name == TX_NULL)
290 {
291
292 /* The strings can't match. */
293 return(TX_FALSE);
294 }
295
296 /* Loop through the names. */
297 while (1)
298 {
299
300 /* Get the current characters from each name. */
301 search_name_char = *search_name;
302 object_name_char = *object_name;
303
304 /* Check for match. */
305 if (search_name_char == object_name_char)
306 {
307
308 /* Are they null-terminators? */
309 if (search_name_char == '\0')
310 {
311
312 /* The strings match. */
313 return(TX_TRUE);
314 }
315 }
316 else
317 {
318
319 /* The strings don't match. */
320 return(TX_FALSE);
321 }
322
323 /* Are we at the end of the search name? */
324 if (search_name_length == 0)
325 {
326
327 /* The strings don't match. */
328 return(TX_FALSE);
329 }
330
331 /* Move to next character. */
332 search_name++;
333 object_name++;
334 search_name_length--;
335 }
336 }
337
338
339 /**************************************************************************/
340 /* */
341 /* FUNCTION RELEASE */
342 /* */
343 /* _txm_module_manager_util_code_allocation_size_and_alignment_get */
344 /* PORTABLE C */
345 /* 6.1 */
346 /* AUTHOR */
347 /* */
348 /* Scott Larson, Microsoft Corporation */
349 /* */
350 /* DESCRIPTION */
351 /* */
352 /* This function returns the required alignment and allocation size */
353 /* for a module's code area. */
354 /* */
355 /* INPUT */
356 /* */
357 /* module_preamble Preamble of module to return code */
358 /* values for */
359 /* code_alignment_dest Address to return code alignment */
360 /* code_allocation_size_desk Address to return code allocation */
361 /* size */
362 /* */
363 /* OUTPUT */
364 /* */
365 /* status Success if no math overflow */
366 /* occurred during calculation */
367 /* */
368 /* CALLS */
369 /* */
370 /* None */
371 /* */
372 /* CALLED BY */
373 /* */
374 /* txm_module_manager_*_load Module load functions */
375 /* */
376 /* RELEASE HISTORY */
377 /* */
378 /* DATE NAME DESCRIPTION */
379 /* */
380 /* 09-30-2020 Scott Larson Initial Version 6.1 */
381 /* */
382 /**************************************************************************/
_txm_module_manager_util_code_allocation_size_and_alignment_get(TXM_MODULE_PREAMBLE * module_preamble,ULONG * code_alignment_dest,ULONG * code_allocation_size_dest)383 UINT _txm_module_manager_util_code_allocation_size_and_alignment_get(TXM_MODULE_PREAMBLE *module_preamble,
384 ULONG *code_alignment_dest, ULONG *code_allocation_size_dest)
385 {
386
387 ULONG code_size;
388 ULONG code_alignment;
389 ULONG data_size_ignored;
390 ULONG data_alignment_ignored;
391
392
393 /* Pickup the module code size. */
394 code_size = module_preamble -> txm_module_preamble_code_size;
395
396 /* Adjust the size of the module elements to be aligned to the default alignment. */
397 TXM_MODULE_MANAGER_UTIL_MATH_ADD_ULONG(code_size, TXM_MODULE_CODE_ALIGNMENT, code_size);
398 code_size = ((code_size - 1)/TXM_MODULE_CODE_ALIGNMENT) * TXM_MODULE_CODE_ALIGNMENT;
399
400 /* Setup the default code and data alignments. */
401 code_alignment = (ULONG) TXM_MODULE_CODE_ALIGNMENT;
402
403 /* Get the port-specific alignment for the code size. Note we only want code so we pass 'null' values for data. */
404 data_size_ignored = 1;
405 data_alignment_ignored = 1;
406 TXM_MODULE_MANAGER_ALIGNMENT_ADJUST(module_preamble, code_size, code_alignment, data_size_ignored, data_alignment_ignored)
407
408 /* Calculate the code memory allocation size. */
409 TXM_MODULE_MANAGER_UTIL_MATH_ADD_ULONG(code_size, code_alignment, *code_allocation_size_dest);
410
411 /* Write the alignment result into the caller's destination address. */
412 *code_alignment_dest = code_alignment;
413
414 /* Return success. */
415 return(TX_SUCCESS);
416 }
417