]> begriffs open source - freertos/blob - portable/GCC/ARM_CM85/secure/secure_heap.c
[AUTO][RELEASE]: Bump file header version to "10.5.1"
[freertos] / portable / GCC / ARM_CM85 / secure / secure_heap.c
1 /*\r
2  * FreeRTOS Kernel V10.5.1\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT\r
6  *\r
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\r
14  * The above copyright notice and this permission notice shall be included in all\r
15  * copies or substantial portions of the Software.\r
16  *\r
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include <stdint.h>\r
31 \r
32 /* Secure context heap includes. */\r
33 #include "secure_heap.h"\r
34 \r
35 /* Secure port macros. */\r
36 #include "secure_port_macros.h"\r
37 \r
38 /**\r
39  * @brief Total heap size.\r
40  */\r
41 #ifndef secureconfigTOTAL_HEAP_SIZE\r
42     #define secureconfigTOTAL_HEAP_SIZE    ( ( ( size_t ) ( 10 * 1024 ) ) )\r
43 #endif\r
44 \r
45 /* No test marker by default. */\r
46 #ifndef mtCOVERAGE_TEST_MARKER\r
47     #define mtCOVERAGE_TEST_MARKER()\r
48 #endif\r
49 \r
50 /* No tracing by default. */\r
51 #ifndef traceMALLOC\r
52     #define traceMALLOC( pvReturn, xWantedSize )\r
53 #endif\r
54 \r
55 /* No tracing by default. */\r
56 #ifndef traceFREE\r
57     #define traceFREE( pv, xBlockSize )\r
58 #endif\r
59 \r
60 /* Block sizes must not get too small. */\r
61 #define secureheapMINIMUM_BLOCK_SIZE    ( ( size_t ) ( xHeapStructSize << 1 ) )\r
62 \r
63 /* Assumes 8bit bytes! */\r
64 #define secureheapBITS_PER_BYTE         ( ( size_t ) 8 )\r
65 /*-----------------------------------------------------------*/\r
66 \r
67 /* Allocate the memory for the heap. */\r
68 #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )\r
69 \r
70 /* The application writer has already defined the array used for the RTOS\r
71 * heap - probably so it can be placed in a special segment or address. */\r
72     extern uint8_t ucHeap[ secureconfigTOTAL_HEAP_SIZE ];\r
73 #else /* configAPPLICATION_ALLOCATED_HEAP */\r
74     static uint8_t ucHeap[ secureconfigTOTAL_HEAP_SIZE ];\r
75 #endif /* configAPPLICATION_ALLOCATED_HEAP */\r
76 \r
77 /**\r
78  * @brief The linked list structure.\r
79  *\r
80  * This is used to link free blocks in order of their memory address.\r
81  */\r
82 typedef struct A_BLOCK_LINK\r
83 {\r
84     struct A_BLOCK_LINK * pxNextFreeBlock; /**< The next free block in the list. */\r
85     size_t xBlockSize;                     /**< The size of the free block. */\r
86 } BlockLink_t;\r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /**\r
90  * @brief Called automatically to setup the required heap structures the first\r
91  * time pvPortMalloc() is called.\r
92  */\r
93 static void prvHeapInit( void );\r
94 \r
95 /**\r
96  * @brief Inserts a block of memory that is being freed into the correct\r
97  * position in the list of free memory blocks.\r
98  *\r
99  * The block being freed will be merged with the block in front it and/or the\r
100  * block behind it if the memory blocks are adjacent to each other.\r
101  *\r
102  * @param[in] pxBlockToInsert The block being freed.\r
103  */\r
104 static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert );\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /**\r
108  * @brief The size of the structure placed at the beginning of each allocated\r
109  * memory block must by correctly byte aligned.\r
110  */\r
111 static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( secureportBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );\r
112 \r
113 /**\r
114  * @brief Create a couple of list links to mark the start and end of the list.\r
115  */\r
116 static BlockLink_t xStart;\r
117 static BlockLink_t * pxEnd = NULL;\r
118 \r
119 /**\r
120  * @brief Keeps track of the number of free bytes remaining, but says nothing\r
121  * about fragmentation.\r
122  */\r
123 static size_t xFreeBytesRemaining = 0U;\r
124 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
125 \r
126 /**\r
127  * @brief Gets set to the top bit of an size_t type.\r
128  *\r
129  * When this bit in the xBlockSize member of an BlockLink_t structure is set\r
130  * then the block belongs to the application. When the bit is free the block is\r
131  * still part of the free heap space.\r
132  */\r
133 static size_t xBlockAllocatedBit = 0;\r
134 /*-----------------------------------------------------------*/\r
135 \r
136 static void prvHeapInit( void )\r
137 {\r
138     BlockLink_t * pxFirstFreeBlock;\r
139     uint8_t * pucAlignedHeap;\r
140     size_t uxAddress;\r
141     size_t xTotalHeapSize = secureconfigTOTAL_HEAP_SIZE;\r
142 \r
143     /* Ensure the heap starts on a correctly aligned boundary. */\r
144     uxAddress = ( size_t ) ucHeap;\r
145 \r
146     if( ( uxAddress & secureportBYTE_ALIGNMENT_MASK ) != 0 )\r
147     {\r
148         uxAddress += ( secureportBYTE_ALIGNMENT - 1 );\r
149         uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );\r
150         xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;\r
151     }\r
152 \r
153     pucAlignedHeap = ( uint8_t * ) uxAddress;\r
154 \r
155     /* xStart is used to hold a pointer to the first item in the list of free\r
156      * blocks.  The void cast is used to prevent compiler warnings. */\r
157     xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
158     xStart.xBlockSize = ( size_t ) 0;\r
159 \r
160     /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
161      * at the end of the heap space. */\r
162     uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;\r
163     uxAddress -= xHeapStructSize;\r
164     uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );\r
165     pxEnd = ( void * ) uxAddress;\r
166     pxEnd->xBlockSize = 0;\r
167     pxEnd->pxNextFreeBlock = NULL;\r
168 \r
169     /* To start with there is a single free block that is sized to take up the\r
170      * entire heap space, minus the space taken by pxEnd. */\r
171     pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
172     pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;\r
173     pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
174 \r
175     /* Only one block exists - and it covers the entire usable heap space. */\r
176     xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
177     xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
178 \r
179     /* Work out the position of the top bit in a size_t variable. */\r
180     xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * secureheapBITS_PER_BYTE ) - 1 );\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert )\r
185 {\r
186     BlockLink_t * pxIterator;\r
187     uint8_t * puc;\r
188 \r
189     /* Iterate through the list until a block is found that has a higher address\r
190      * than the block being inserted. */\r
191     for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
192     {\r
193         /* Nothing to do here, just iterate to the right position. */\r
194     }\r
195 \r
196     /* Do the block being inserted, and the block it is being inserted after\r
197      * make a contiguous block of memory? */\r
198     puc = ( uint8_t * ) pxIterator;\r
199 \r
200     if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
201     {\r
202         pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
203         pxBlockToInsert = pxIterator;\r
204     }\r
205     else\r
206     {\r
207         mtCOVERAGE_TEST_MARKER();\r
208     }\r
209 \r
210     /* Do the block being inserted, and the block it is being inserted before\r
211      * make a contiguous block of memory? */\r
212     puc = ( uint8_t * ) pxBlockToInsert;\r
213 \r
214     if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
215     {\r
216         if( pxIterator->pxNextFreeBlock != pxEnd )\r
217         {\r
218             /* Form one big block from the two blocks. */\r
219             pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
220             pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
221         }\r
222         else\r
223         {\r
224             pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
225         }\r
226     }\r
227     else\r
228     {\r
229         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
230     }\r
231 \r
232     /* If the block being inserted plugged a gab, so was merged with the block\r
233      * before and the block after, then it's pxNextFreeBlock pointer will have\r
234      * already been set, and should not be set here as that would make it point\r
235      * to itself. */\r
236     if( pxIterator != pxBlockToInsert )\r
237     {\r
238         pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
239     }\r
240     else\r
241     {\r
242         mtCOVERAGE_TEST_MARKER();\r
243     }\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 void * pvPortMalloc( size_t xWantedSize )\r
248 {\r
249     BlockLink_t * pxBlock;\r
250     BlockLink_t * pxPreviousBlock;\r
251     BlockLink_t * pxNewBlockLink;\r
252     void * pvReturn = NULL;\r
253 \r
254     /* If this is the first call to malloc then the heap will require\r
255      * initialisation to setup the list of free blocks. */\r
256     if( pxEnd == NULL )\r
257     {\r
258         prvHeapInit();\r
259     }\r
260     else\r
261     {\r
262         mtCOVERAGE_TEST_MARKER();\r
263     }\r
264 \r
265     /* Check the requested block size is not so large that the top bit is set.\r
266      * The top bit of the block size member of the BlockLink_t structure is used\r
267      * to determine who owns the block - the application or the kernel, so it\r
268      * must be free. */\r
269     if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
270     {\r
271         /* The wanted size is increased so it can contain a BlockLink_t\r
272          * structure in addition to the requested amount of bytes. */\r
273         if( xWantedSize > 0 )\r
274         {\r
275             xWantedSize += xHeapStructSize;\r
276 \r
277             /* Ensure that blocks are always aligned to the required number of\r
278              * bytes. */\r
279             if( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) != 0x00 )\r
280             {\r
281                 /* Byte alignment required. */\r
282                 xWantedSize += ( secureportBYTE_ALIGNMENT - ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) );\r
283                 secureportASSERT( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) == 0 );\r
284             }\r
285             else\r
286             {\r
287                 mtCOVERAGE_TEST_MARKER();\r
288             }\r
289         }\r
290         else\r
291         {\r
292             mtCOVERAGE_TEST_MARKER();\r
293         }\r
294 \r
295         if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
296         {\r
297             /* Traverse the list from the start (lowest address) block until\r
298              * one of adequate size is found. */\r
299             pxPreviousBlock = &xStart;\r
300             pxBlock = xStart.pxNextFreeBlock;\r
301 \r
302             while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
303             {\r
304                 pxPreviousBlock = pxBlock;\r
305                 pxBlock = pxBlock->pxNextFreeBlock;\r
306             }\r
307 \r
308             /* If the end marker was reached then a block of adequate size was\r
309              * not found. */\r
310             if( pxBlock != pxEnd )\r
311             {\r
312                 /* Return the memory space pointed to - jumping over the\r
313                  * BlockLink_t structure at its start. */\r
314                 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );\r
315 \r
316                 /* This block is being returned for use so must be taken out\r
317                  * of the list of free blocks. */\r
318                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
319 \r
320                 /* If the block is larger than required it can be split into\r
321                  * two. */\r
322                 if( ( pxBlock->xBlockSize - xWantedSize ) > secureheapMINIMUM_BLOCK_SIZE )\r
323                 {\r
324                     /* This block is to be split into two.  Create a new\r
325                      * block following the number of bytes requested. The void\r
326                      * cast is used to prevent byte alignment warnings from the\r
327                      * compiler. */\r
328                     pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
329                     secureportASSERT( ( ( ( size_t ) pxNewBlockLink ) & secureportBYTE_ALIGNMENT_MASK ) == 0 );\r
330 \r
331                     /* Calculate the sizes of two blocks split from the single\r
332                      * block. */\r
333                     pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
334                     pxBlock->xBlockSize = xWantedSize;\r
335 \r
336                     /* Insert the new block into the list of free blocks. */\r
337                     prvInsertBlockIntoFreeList( pxNewBlockLink );\r
338                 }\r
339                 else\r
340                 {\r
341                     mtCOVERAGE_TEST_MARKER();\r
342                 }\r
343 \r
344                 xFreeBytesRemaining -= pxBlock->xBlockSize;\r
345 \r
346                 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
347                 {\r
348                     xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
349                 }\r
350                 else\r
351                 {\r
352                     mtCOVERAGE_TEST_MARKER();\r
353                 }\r
354 \r
355                 /* The block is being returned - it is allocated and owned by\r
356                  * the application and has no "next" block. */\r
357                 pxBlock->xBlockSize |= xBlockAllocatedBit;\r
358                 pxBlock->pxNextFreeBlock = NULL;\r
359             }\r
360             else\r
361             {\r
362                 mtCOVERAGE_TEST_MARKER();\r
363             }\r
364         }\r
365         else\r
366         {\r
367             mtCOVERAGE_TEST_MARKER();\r
368         }\r
369     }\r
370     else\r
371     {\r
372         mtCOVERAGE_TEST_MARKER();\r
373     }\r
374 \r
375     traceMALLOC( pvReturn, xWantedSize );\r
376 \r
377     #if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 )\r
378         {\r
379             if( pvReturn == NULL )\r
380             {\r
381                 extern void vApplicationMallocFailedHook( void );\r
382                 vApplicationMallocFailedHook();\r
383             }\r
384             else\r
385             {\r
386                 mtCOVERAGE_TEST_MARKER();\r
387             }\r
388         }\r
389     #endif /* if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 ) */\r
390 \r
391     secureportASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) secureportBYTE_ALIGNMENT_MASK ) == 0 );\r
392     return pvReturn;\r
393 }\r
394 /*-----------------------------------------------------------*/\r
395 \r
396 void vPortFree( void * pv )\r
397 {\r
398     uint8_t * puc = ( uint8_t * ) pv;\r
399     BlockLink_t * pxLink;\r
400 \r
401     if( pv != NULL )\r
402     {\r
403         /* The memory being freed will have an BlockLink_t structure immediately\r
404          * before it. */\r
405         puc -= xHeapStructSize;\r
406 \r
407         /* This casting is to keep the compiler from issuing warnings. */\r
408         pxLink = ( void * ) puc;\r
409 \r
410         /* Check the block is actually allocated. */\r
411         secureportASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
412         secureportASSERT( pxLink->pxNextFreeBlock == NULL );\r
413 \r
414         if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
415         {\r
416             if( pxLink->pxNextFreeBlock == NULL )\r
417             {\r
418                 /* The block is being returned to the heap - it is no longer\r
419                  * allocated. */\r
420                 pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
421 \r
422                 secureportDISABLE_NON_SECURE_INTERRUPTS();\r
423                 {\r
424                     /* Add this block to the list of free blocks. */\r
425                     xFreeBytesRemaining += pxLink->xBlockSize;\r
426                     traceFREE( pv, pxLink->xBlockSize );\r
427                     prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
428                 }\r
429                 secureportENABLE_NON_SECURE_INTERRUPTS();\r
430             }\r
431             else\r
432             {\r
433                 mtCOVERAGE_TEST_MARKER();\r
434             }\r
435         }\r
436         else\r
437         {\r
438             mtCOVERAGE_TEST_MARKER();\r
439         }\r
440     }\r
441 }\r
442 /*-----------------------------------------------------------*/\r
443 \r
444 size_t xPortGetFreeHeapSize( void )\r
445 {\r
446     return xFreeBytesRemaining;\r
447 }\r
448 /*-----------------------------------------------------------*/\r
449 \r
450 size_t xPortGetMinimumEverFreeHeapSize( void )\r
451 {\r
452     return xMinimumEverFreeBytesRemaining;\r
453 }\r
454 /*-----------------------------------------------------------*/\r