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