]> begriffs open source - freertos/blob - portable/IAR/ARM_CM33/secure/secure_heap.c
[AUTO][RELEASE]: Bump file header version to "10.5.0"
[freertos] / portable / IAR / ARM_CM33 / secure / secure_heap.c
1 /*\r
2  * FreeRTOS Kernel V10.5.0\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, * pxEnd = NULL;\r
117 \r
118 /**\r
119  * @brief Keeps track of the number of free bytes remaining, but says nothing\r
120  * about fragmentation.\r
121  */\r
122 static size_t xFreeBytesRemaining = 0U;\r
123 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
124 \r
125 /**\r
126  * @brief Gets set to the top bit of an size_t type.\r
127  *\r
128  * When this bit in the xBlockSize member of an BlockLink_t structure is set\r
129  * then the block belongs to the application. When the bit is free the block is\r
130  * still part of the free heap space.\r
131  */\r
132 static size_t xBlockAllocatedBit = 0;\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 static void prvHeapInit( void )\r
136 {\r
137     BlockLink_t * pxFirstFreeBlock;\r
138     uint8_t * pucAlignedHeap;\r
139     size_t uxAddress;\r
140     size_t xTotalHeapSize = secureconfigTOTAL_HEAP_SIZE;\r
141 \r
142     /* Ensure the heap starts on a correctly aligned boundary. */\r
143     uxAddress = ( size_t ) ucHeap;\r
144 \r
145     if( ( uxAddress & secureportBYTE_ALIGNMENT_MASK ) != 0 )\r
146     {\r
147         uxAddress += ( secureportBYTE_ALIGNMENT - 1 );\r
148         uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );\r
149         xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;\r
150     }\r
151 \r
152     pucAlignedHeap = ( uint8_t * ) uxAddress;\r
153 \r
154     /* xStart is used to hold a pointer to the first item in the list of free\r
155      * blocks.  The void cast is used to prevent compiler warnings. */\r
156     xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
157     xStart.xBlockSize = ( size_t ) 0;\r
158 \r
159     /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
160      * at the end of the heap space. */\r
161     uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;\r
162     uxAddress -= xHeapStructSize;\r
163     uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );\r
164     pxEnd = ( void * ) uxAddress;\r
165     pxEnd->xBlockSize = 0;\r
166     pxEnd->pxNextFreeBlock = NULL;\r
167 \r
168     /* To start with there is a single free block that is sized to take up the\r
169      * entire heap space, minus the space taken by pxEnd. */\r
170     pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
171     pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;\r
172     pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
173 \r
174     /* Only one block exists - and it covers the entire usable heap space. */\r
175     xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
176     xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
177 \r
178     /* Work out the position of the top bit in a size_t variable. */\r
179     xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * secureheapBITS_PER_BYTE ) - 1 );\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert )\r
184 {\r
185     BlockLink_t * pxIterator;\r
186     uint8_t * puc;\r
187 \r
188     /* Iterate through the list until a block is found that has a higher address\r
189      * than the block being inserted. */\r
190     for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
191     {\r
192         /* Nothing to do here, just iterate to the right position. */\r
193     }\r
194 \r
195     /* Do the block being inserted, and the block it is being inserted after\r
196      * make a contiguous block of memory? */\r
197     puc = ( uint8_t * ) pxIterator;\r
198 \r
199     if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
200     {\r
201         pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
202         pxBlockToInsert = pxIterator;\r
203     }\r
204     else\r
205     {\r
206         mtCOVERAGE_TEST_MARKER();\r
207     }\r
208 \r
209     /* Do the block being inserted, and the block it is being inserted before\r
210      * make a contiguous block of memory? */\r
211     puc = ( uint8_t * ) pxBlockToInsert;\r
212 \r
213     if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
214     {\r
215         if( pxIterator->pxNextFreeBlock != pxEnd )\r
216         {\r
217             /* Form one big block from the two blocks. */\r
218             pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
219             pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
220         }\r
221         else\r
222         {\r
223             pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
224         }\r
225     }\r
226     else\r
227     {\r
228         pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
229     }\r
230 \r
231     /* If the block being inserted plugged a gab, so was merged with the block\r
232      * before and the block after, then it's pxNextFreeBlock pointer will have\r
233      * already been set, and should not be set here as that would make it point\r
234      * to itself. */\r
235     if( pxIterator != pxBlockToInsert )\r
236     {\r
237         pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
238     }\r
239     else\r
240     {\r
241         mtCOVERAGE_TEST_MARKER();\r
242     }\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 void * pvPortMalloc( size_t xWantedSize )\r
247 {\r
248     BlockLink_t * pxBlock, * pxPreviousBlock, * pxNewBlockLink;\r
249     void * pvReturn = NULL;\r
250 \r
251     /* If this is the first call to malloc then the heap will require\r
252      * initialisation to setup the list of free blocks. */\r
253     if( pxEnd == NULL )\r
254     {\r
255         prvHeapInit();\r
256     }\r
257     else\r
258     {\r
259         mtCOVERAGE_TEST_MARKER();\r
260     }\r
261 \r
262     /* Check the requested block size is not so large that the top bit is set.\r
263      * The top bit of the block size member of the BlockLink_t structure is used\r
264      * to determine who owns the block - the application or the kernel, so it\r
265      * must be free. */\r
266     if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
267     {\r
268         /* The wanted size is increased so it can contain a BlockLink_t\r
269          * structure in addition to the requested amount of bytes. */\r
270         if( xWantedSize > 0 )\r
271         {\r
272             xWantedSize += xHeapStructSize;\r
273 \r
274             /* Ensure that blocks are always aligned to the required number of\r
275              * bytes. */\r
276             if( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) != 0x00 )\r
277             {\r
278                 /* Byte alignment required. */\r
279                 xWantedSize += ( secureportBYTE_ALIGNMENT - ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) );\r
280                 secureportASSERT( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) == 0 );\r
281             }\r
282             else\r
283             {\r
284                 mtCOVERAGE_TEST_MARKER();\r
285             }\r
286         }\r
287         else\r
288         {\r
289             mtCOVERAGE_TEST_MARKER();\r
290         }\r
291 \r
292         if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
293         {\r
294             /* Traverse the list from the start (lowest address) block until\r
295              * one of adequate size is found. */\r
296             pxPreviousBlock = &xStart;\r
297             pxBlock = xStart.pxNextFreeBlock;\r
298 \r
299             while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
300             {\r
301                 pxPreviousBlock = pxBlock;\r
302                 pxBlock = pxBlock->pxNextFreeBlock;\r
303             }\r
304 \r
305             /* If the end marker was reached then a block of adequate size was\r
306              * not found. */\r
307             if( pxBlock != pxEnd )\r
308             {\r
309                 /* Return the memory space pointed to - jumping over the\r
310                  * BlockLink_t structure at its start. */\r
311                 pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );\r
312 \r
313                 /* This block is being returned for use so must be taken out\r
314                  * of the list of free blocks. */\r
315                 pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
316 \r
317                 /* If the block is larger than required it can be split into\r
318                  * two. */\r
319                 if( ( pxBlock->xBlockSize - xWantedSize ) > secureheapMINIMUM_BLOCK_SIZE )\r
320                 {\r
321                     /* This block is to be split into two.  Create a new\r
322                      * block following the number of bytes requested. The void\r
323                      * cast is used to prevent byte alignment warnings from the\r
324                      * compiler. */\r
325                     pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
326                     secureportASSERT( ( ( ( size_t ) pxNewBlockLink ) & secureportBYTE_ALIGNMENT_MASK ) == 0 );\r
327 \r
328                     /* Calculate the sizes of two blocks split from the single\r
329                      * block. */\r
330                     pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
331                     pxBlock->xBlockSize = xWantedSize;\r
332 \r
333                     /* Insert the new block into the list of free blocks. */\r
334                     prvInsertBlockIntoFreeList( pxNewBlockLink );\r
335                 }\r
336                 else\r
337                 {\r
338                     mtCOVERAGE_TEST_MARKER();\r
339                 }\r
340 \r
341                 xFreeBytesRemaining -= pxBlock->xBlockSize;\r
342 \r
343                 if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
344                 {\r
345                     xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
346                 }\r
347                 else\r
348                 {\r
349                     mtCOVERAGE_TEST_MARKER();\r
350                 }\r
351 \r
352                 /* The block is being returned - it is allocated and owned by\r
353                  * the application and has no "next" block. */\r
354                 pxBlock->xBlockSize |= xBlockAllocatedBit;\r
355                 pxBlock->pxNextFreeBlock = NULL;\r
356             }\r
357             else\r
358             {\r
359                 mtCOVERAGE_TEST_MARKER();\r
360             }\r
361         }\r
362         else\r
363         {\r
364             mtCOVERAGE_TEST_MARKER();\r
365         }\r
366     }\r
367     else\r
368     {\r
369         mtCOVERAGE_TEST_MARKER();\r
370     }\r
371 \r
372     traceMALLOC( pvReturn, xWantedSize );\r
373 \r
374     #if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 )\r
375         {\r
376             if( pvReturn == NULL )\r
377             {\r
378                 extern void vApplicationMallocFailedHook( void );\r
379                 vApplicationMallocFailedHook();\r
380             }\r
381             else\r
382             {\r
383                 mtCOVERAGE_TEST_MARKER();\r
384             }\r
385         }\r
386     #endif /* if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 ) */\r
387 \r
388     secureportASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) secureportBYTE_ALIGNMENT_MASK ) == 0 );\r
389     return pvReturn;\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r
393 void vPortFree( void * pv )\r
394 {\r
395     uint8_t * puc = ( uint8_t * ) pv;\r
396     BlockLink_t * pxLink;\r
397 \r
398     if( pv != NULL )\r
399     {\r
400         /* The memory being freed will have an BlockLink_t structure immediately\r
401          * before it. */\r
402         puc -= xHeapStructSize;\r
403 \r
404         /* This casting is to keep the compiler from issuing warnings. */\r
405         pxLink = ( void * ) puc;\r
406 \r
407         /* Check the block is actually allocated. */\r
408         secureportASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
409         secureportASSERT( pxLink->pxNextFreeBlock == NULL );\r
410 \r
411         if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
412         {\r
413             if( pxLink->pxNextFreeBlock == NULL )\r
414             {\r
415                 /* The block is being returned to the heap - it is no longer\r
416                  * allocated. */\r
417                 pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
418 \r
419                 secureportDISABLE_NON_SECURE_INTERRUPTS();\r
420                 {\r
421                     /* Add this block to the list of free blocks. */\r
422                     xFreeBytesRemaining += pxLink->xBlockSize;\r
423                     traceFREE( pv, pxLink->xBlockSize );\r
424                     prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
425                 }\r
426                 secureportENABLE_NON_SECURE_INTERRUPTS();\r
427             }\r
428             else\r
429             {\r
430                 mtCOVERAGE_TEST_MARKER();\r
431             }\r
432         }\r
433         else\r
434         {\r
435             mtCOVERAGE_TEST_MARKER();\r
436         }\r
437     }\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 size_t xPortGetFreeHeapSize( void )\r
442 {\r
443     return xFreeBytesRemaining;\r
444 }\r
445 /*-----------------------------------------------------------*/\r
446 \r
447 size_t xPortGetMinimumEverFreeHeapSize( void )\r
448 {\r
449     return xMinimumEverFreeBytesRemaining;\r
450 }\r
451 /*-----------------------------------------------------------*/\r