Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define TX_SOURCE_CODE
#include "tx_api.h"
#include "txm_module.h"
...
...
_txm_module_manager_region_size_get(ULONG)
...
...
_txm_module_manager_calculate_srd_bits(ULONG, ULONG)
...
...
_txm_module_manager_mm_register_setup(TXM_MODULE_INSTANCE *)
...
...
Files
loading...
CodeScopeSTM32 Libraries and Samplesthreadxports_module/cortex_m4/gnu/module_manager/src/txm_module_manager_mm_register_setup.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** ThreadX Component */ /** */ /** Module Manager */ /** */... /**************************************************************************/ /**************************************************************************/ #define TX_SOURCE_CODE #include "tx_api.h" #include "txm_module.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _txm_module_manager_region_size_get Cortex-M4 */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function converts the region size in bytes to the block size */ /* for the Cortex-M4 MPU specification. */ /* */ /* INPUT */ /* */ /* block_size Size of the block in bytes */ /* */ /* OUTPUT */ /* */ /* MPU size specification */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _txm_module_manager_mm_register_setup */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 10-15-2021 Scott Larson Initial Version 6.1.9 */ /* */... /**************************************************************************/ ULONG _txm_module_manager_region_size_get(ULONG block_size) { ULONG return_value; /* Process relative to the input block size. */ if (block_size == 32) { return_value = 0x04; }if (block_size == 32) { ... } else if (block_size == 64) { return_value = 0x05; }else if (block_size == 64) { ... } else if (block_size == 128) { return_value = 0x06; }else if (block_size == 128) { ... } else if (block_size == 256) { return_value = 0x07; }else if (block_size == 256) { ... } else if (block_size == 512) { return_value = 0x08; }else if (block_size == 512) { ... } else if (block_size == 1024) { return_value = 0x09; }else if (block_size == 1024) { ... } else if (block_size == 2048) { return_value = 0x0A; }else if (block_size == 2048) { ... } else if (block_size == 4096) { return_value = 0x0B; }else if (block_size == 4096) { ... } else if (block_size == 8192) { return_value = 0x0C; }else if (block_size == 8192) { ... } else if (block_size == 16384) { return_value = 0x0D; }else if (block_size == 16384) { ... } else if (block_size == 32768) { return_value = 0x0E; }else if (block_size == 32768) { ... } else if (block_size == 65536) { return_value = 0x0F; }else if (block_size == 65536) { ... } else if (block_size == 131072) { return_value = 0x10; }else if (block_size == 131072) { ... } else if (block_size == 262144) { return_value = 0x11; }else if (block_size == 262144) { ... } else if (block_size == 524288) { return_value = 0x12; }else if (block_size == 524288) { ... } else if (block_size == 1048576) { return_value = 0x13; }else if (block_size == 1048576) { ... } else if (block_size == 2097152) { return_value = 0x14; }else if (block_size == 2097152) { ... } else { /* Max 4MB MPU pages for modules. */ return_value = 0x15; }else { ... } return(return_value); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _txm_module_manager_calculate_srd_bits Cortex-M4 */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function calculates the SRD bits that need to be set to */ /* protect "length" bytes in a block. */ /* */ /* INPUT */ /* */ /* block_size Size of the block in bytes */ /* length Actual length in bytes */ /* */ /* OUTPUT */ /* */ /* SRD bits to be OR'ed with region attribute register. */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _txm_module_manager_mm_register_setup */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 10-15-2021 Scott Larson Initial Version 6.1.9 */ /* */... /**************************************************************************/ ULONG _txm_module_manager_calculate_srd_bits(ULONG block_size, ULONG length) { ULONG srd_bits = 0; UINT srd_bit_index; /* length is smaller than block_size, set SRD bits if block_size is 256 or more. */ if((block_size >= 256) && (length < block_size)) { /* Divide block_size by 8 by shifting right 3. Result is size of subregion. */ block_size = block_size >> 3; /* Set SRD index into attribute register. */ srd_bit_index = 8; /* If subregion overlaps length, move to the next subregion. */ while(length > block_size) { length = length - block_size; srd_bit_index++; }while (length > block_size) { ... } /* Check for a portion of code remaining. */ if(length) { srd_bit_index++; }if (length) { ... } /* Set unused subregion bits. */ while(srd_bit_index < 16) { srd_bits = srd_bits | (0x1 << srd_bit_index); srd_bit_index++; }while (srd_bit_index < 16) { ... } }if ((block_size >= 256) && (length < block_size)) { ... } return(srd_bits); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _txm_module_manager_mm_register_setup Cortex-M4 */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function sets up the MPU register definitions based on the */ /* module's memory characteristics. */ /* */ /* Default MPU layout: */ /* Entry Description */ /* 0 Kernel mode entry */ /* 1 Module code region */ /* 2 Module code region */ /* 3 Module code region */ /* 4 Module code region */ /* 5 Module data region */ /* 6 Module data region */ /* 7 Module data region */ /* */ /* If TXM_MODULE_MANAGER_16_MPU is defined, there are 16 MPU slots. */ /* MPU layout for the Cortex-M7: */ /* Entry Description */ /* 0 Kernel mode entry */ /* 1 Module code region */ /* 2 Module code region */ /* 3 Module code region */ /* 4 Module code region */ /* 5 Module data region */ /* 6 Module data region */ /* 7 Module data region */ /* 8 Module data region */ /* 9 Module shared memory region */ /* 10 Module shared memory region */ /* 11 Module shared memory region */ /* 12 Unused region */ /* 13 Unused region */ /* 14 Unused region */ /* 15 Unused region */ /* */ /* */ /* INPUT */ /* */ /* module_instance Pointer to module instance */ /* */ /* OUTPUT */ /* */ /* MPU specifications for module in module_instance */ /* */ /* CALLS */ /* */ /* _txm_module_manager_region_size_get */ /* */ /* CALLED BY */ /* */ /* _txm_module_manager_thread_create */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 10-15-2021 Scott Larson Initial Version 6.1.9 */ /* */... /**************************************************************************/ VOID _txm_module_manager_mm_register_setup(TXM_MODULE_INSTANCE *module_instance) { #ifdef TXM_MODULE_MANAGER_16_MPU ULONG code_address; ULONG code_size; ULONG data_address; ULONG data_size; ULONG start_stop_stack_size; ULONG callback_stack_size; ULONG block_size; ULONG region_size; ULONG srd_bits = 0; UINT mpu_table_index; UINT i; /* Setup the first MPU region for kernel mode entry. */ /* Set address register to user mode entry function address, which is guaranteed to be at least 32-byte aligned. Mask address to proper range, region 0, set Valid bit. *//* ... */ module_instance -> txm_module_instance_mpu_registers[TXM_MODULE_MPU_KERNEL_ENTRY_INDEX].txm_module_mpu_region_address = ((ULONG) _txm_module_manager_user_mode_entry & 0xFFFFFFE0) | 0x10; /* Set the attributes, size (32 bytes) and enable bit. */ module_instance -> txm_module_instance_mpu_registers[TXM_MODULE_MPU_KERNEL_ENTRY_INDEX].txm_module_mpu_region_attribute_size = TXM_MODULE_MPU_CODE_ACCESS_CONTROL | (_txm_module_manager_region_size_get(32) << 1) | TXM_ENABLE_REGION; /* End of kernel mode entry setup. */ /* Setup code protection. */ /* Initialize the MPU table index. */ mpu_table_index = 1; /* Pickup code starting address and actual size. */ code_address = (ULONG) module_instance -> txm_module_instance_code_start; code_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_code_size; /* Determine code block sizes. Minimize the alignment requirement. There are 4 MPU code entries available. The following is how the code size will be distributed: 1. 1/4 of the largest power of two that is greater than or equal to code size. 2. 1/4 of the largest power of two that is greater than or equal to code size. 3. Largest power of 2 that fits in the remaining space. 4. Smallest power of 2 that exceeds the remaining space, minimum 32. *//* ... */ /* Now loop through to setup MPU protection for the code area. */ for (i = 0; i < TXM_MODULE_MPU_CODE_ENTRIES; i++) { /* First two MPU blocks are 1/4 of the largest power of two that is greater than or equal to code size. *//* ... */ if (i < 2) { block_size = _txm_power_of_two_block_size(code_size) >> 2; }if (i < 2) { ... } /* Third MPU block is the largest power of 2 that fits in the remaining space. */ else if (i == 2) { /* Subtract (block_size*2) from code_size to calculate remaining space. */ code_size = code_size - (block_size << 1); block_size = _txm_power_of_two_block_size(code_size) >> 1; }else if (i == 2) { ... } /* Last MPU block is the smallest power of 2 that exceeds the remaining space, minimum 32. */ else { /* Calculate remaining space. */ code_size = code_size - block_size; block_size = _txm_power_of_two_block_size(code_size); srd_bits = _txm_module_manager_calculate_srd_bits(block_size, code_size); }else { ... } /* Calculate the region size information. */ region_size = (_txm_module_manager_region_size_get(block_size) << 1); /* Build the base address register with address, MPU region, set Valid bit. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index].txm_module_mpu_region_address = (code_address & ~(block_size - 1)) | mpu_table_index | 0x10; /* Build the attribute-size register with permissions, SRD, size, enable. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index].txm_module_mpu_region_attribute_size = TXM_MODULE_MPU_CODE_ACCESS_CONTROL | srd_bits | region_size | TXM_ENABLE_REGION; /* Adjust the code address. */ code_address = code_address + block_size; /* Increment MPU table index. */ mpu_table_index++; }for (i = 0; i < TXM_MODULE_MPU_CODE_ENTRIES; i++) { ... } /* End of code protection. */ /* Setup data protection. */ /* Reset SRD bitfield. */ srd_bits = 0; /* Pickup data starting address and actual size. */ data_address = (ULONG) module_instance -> txm_module_instance_data_start; /* Adjust the size of the module elements to be aligned to the default alignment. We do this so that when we partition the allocated memory, we can simply place these regions right beside each other without having to align their pointers. Note this only works when they all have the same alignment. *//* ... */ data_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_data_size; start_stop_stack_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_start_stop_stack_size; callback_stack_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_callback_stack_size; data_size = ((data_size + TXM_MODULE_DATA_ALIGNMENT - 1)/TXM_MODULE_DATA_ALIGNMENT) * TXM_MODULE_DATA_ALIGNMENT; start_stop_stack_size = ((start_stop_stack_size + TXM_MODULE_DATA_ALIGNMENT - 1)/TXM_MODULE_DATA_ALIGNMENT) * TXM_MODULE_DATA_ALIGNMENT; callback_stack_size = ((callback_stack_size + TXM_MODULE_DATA_ALIGNMENT - 1)/TXM_MODULE_DATA_ALIGNMENT) * TXM_MODULE_DATA_ALIGNMENT; /* Update the data size to include thread stacks. */ data_size = data_size + start_stop_stack_size + callback_stack_size; /* Determine data block sizes. Minimize the alignment requirement. There are 4 MPU data entries available. The following is how the data size will be distributed: 1. 1/4 of the largest power of two that is greater than or equal to data size. 2. 1/4 of the largest power of two that is greater than or equal to data size. 3. Largest power of 2 that fits in the remaining space. 4. Smallest power of 2 that exceeds the remaining space, minimum 32. *//* ... */ /* Now loop through to setup MPU protection for the data area. */ for (i = 0; i < TXM_MODULE_MPU_DATA_ENTRIES; i++) { /* First two MPU blocks are 1/4 of the largest power of two that is greater than or equal to data size. *//* ... */ if (i < 2) { block_size = _txm_power_of_two_block_size(data_size) >> 2; }if (i < 2) { ... } /* Third MPU block is the largest power of 2 that fits in the remaining space. */ else if (i == 2) { /* Subtract (block_size*2) from data_size to calculate remaining space. */ data_size = data_size - (block_size << 1); block_size = _txm_power_of_two_block_size(data_size) >> 1; }else if (i == 2) { ... } /* Last MPU block is the smallest power of 2 that exceeds the remaining space, minimum 32. */ else { /* Calculate remaining space. */ data_size = data_size - block_size; block_size = _txm_power_of_two_block_size(data_size); srd_bits = _txm_module_manager_calculate_srd_bits(block_size, data_size); }else { ... } /* Calculate the region size information. */ region_size = (_txm_module_manager_region_size_get(block_size) << 1); /* Build the base address register with address, MPU region, set Valid bit. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index].txm_module_mpu_region_address = (data_address & ~(block_size - 1)) | mpu_table_index | 0x10; /* Build the attribute-size register with permissions, SRD, size, enable. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index].txm_module_mpu_region_attribute_size = TXM_MODULE_MPU_DATA_ACCESS_CONTROL | srd_bits | region_size | TXM_ENABLE_REGION; /* Adjust the data address. */ data_address = data_address + block_size; /* Increment MPU table index. */ mpu_table_index++; }for (i = 0; i < TXM_MODULE_MPU_DATA_ENTRIES; i++) { ... } /* Setup MPU for the remaining regions. */ while (mpu_table_index < TXM_MODULE_MPU_TOTAL_ENTRIES) { /* Build the base address register with address, MPU region, set Valid bit. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index].txm_module_mpu_region_address = mpu_table_index | 0x10; /* Increment MPU table index. */ mpu_table_index++; }while (mpu_table_index < TXM_MODULE_MPU_TOTAL_ENTRIES) { ... } /* ... */ #else ULONG code_address; ULONG code_size; ULONG data_address; ULONG data_size; ULONG start_stop_stack_size; ULONG callback_stack_size; ULONG block_size; ULONG base_address_register; ULONG base_attribute_register; ULONG region_size; ULONG srd_bits = 0; UINT mpu_register = 0; UINT mpu_table_index; UINT i; /* Setup the first region for the ThreadX trampoline code. */ /* Set base register to user mode entry, which is guaranteed to be at least 32-byte aligned. */ base_address_register = (ULONG) _txm_module_manager_user_mode_entry; /* Mask address to proper range, region 0, set Valid bit. */ base_address_register = (base_address_register & 0xFFFFFFE0) | mpu_register | 0x10; module_instance -> txm_module_instance_mpu_registers[0] = base_address_register; /* Attributes: read only, write-back, shareable, size 32 bytes, region enabled. */ module_instance -> txm_module_instance_mpu_registers[1] = 0x06070009; /* Initialize the MPU register. */ mpu_register = 1; /* Initialize the MPU table index. */ mpu_table_index = 2; /* Setup values for code area. */ code_address = (ULONG) module_instance -> txm_module_instance_code_start; code_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_code_size; /* Check if shared memory was set up. If so, only 3 entries are available for code protection. If not set up, 4 code entries are available. *//* ... */ if(module_instance -> txm_module_instance_mpu_registers[TXM_MODULE_MANAGER_SHARED_MPU_INDEX] == 0) { /* Determine code block sizes. Minimize the alignment requirement. There are 4 MPU code entries available. The following is how the code size will be distributed: 1. 1/4 of the largest power of two that is greater than or equal to code size. 2. 1/4 of the largest power of two that is greater than or equal to code size. 3. Largest power of 2 that fits in the remaining space. 4. Smallest power of 2 that exceeds the remaining space, minimum 32. *//* ... */ /* Now loop through to setup MPU protection for the code area. */ for (i = 0; i < TXM_MODULE_MANAGER_CODE_MPU_ENTRIES; i++) { /* First two MPU blocks are 1/4 of the largest power of two that is greater than or equal to code size. *//* ... */ if (i < 2) { block_size = _txm_power_of_two_block_size(code_size) >> 2; }if (i < 2) { ... } /* Third MPU block is the largest power of 2 that fits in the remaining space. */ else if (i == 2) { /* Subtract (block_size*2) from code_size to calculate remaining space. */ code_size = code_size - (block_size << 1); block_size = _txm_power_of_two_block_size(code_size) >> 1; }else if (i == 2) { ... } /* Last MPU block is the smallest power of 2 that exceeds the remaining space, minimum 32. */ else { /* Calculate remaining space. */ code_size = code_size - block_size; block_size = _txm_power_of_two_block_size(code_size); srd_bits = _txm_module_manager_calculate_srd_bits(block_size, code_size); }else { ... } /* Build the base address register. */ base_address_register = (code_address & ~(block_size - 1)) | mpu_register | 0x10; /* Calculate the region size information. */ region_size = (_txm_module_manager_region_size_get(block_size) << 1); /* Build the base attribute register. */ base_attribute_register = region_size | srd_bits | 0x06070001; /* Setup the MPU Base Address Register. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index] = base_address_register; /* Setup the MPU Base Attribute Register. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index+1] = base_attribute_register; /* Adjust the code address. */ code_address = code_address + block_size; /* Move MPU table index. */ mpu_table_index = mpu_table_index + 2; /* Increment the MPU register index. */ mpu_register++; }for (i = 0; i < TXM_MODULE_MANAGER_CODE_MPU_ENTRIES; i++) { ... } }if (module_instance -> txm_module_instance_mpu_registers[TXM_MODULE_MANAGER_SHARED_MPU_INDEX] == 0) { ... } /* Only 3 code entries available. */ else { /* Calculate block size, one code entry taken up by shared memory. */ block_size = _txm_power_of_two_block_size(code_size / (TXM_MODULE_MANAGER_CODE_MPU_ENTRIES - 1)); /* Calculate the region size information. */ region_size = (_txm_module_manager_region_size_get(block_size) << 1); /* Now loop through to setup MPU protection for the code area. */ for (i = 0; i < TXM_MODULE_MANAGER_CODE_MPU_ENTRIES - 1; i++) { /* Build the base address register. */ base_address_register = (code_address & ~(block_size - 1)) | mpu_register | 0x10; /* Check if SRD bits need to be set. */ if (code_size < block_size) { srd_bits = _txm_module_manager_calculate_srd_bits(block_size, code_size); }if (code_size < block_size) { ... } /* Build the base attribute register. */ base_attribute_register = region_size | srd_bits | 0x06070000; /* Is there still some code? If so set the region enable bit. */ if (code_size) { /* Set the region enable bit. */ base_attribute_register = base_attribute_register | 0x1; }if (code_size) { ... } /* Setup the MPU Base Address Register. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index] = base_address_register; /* Setup the MPU Base Attribute Register. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index+1] = base_attribute_register; /* Adjust the code address. */ code_address = code_address + block_size; /* Decrement the code size. */ if (code_size > block_size) { code_size = code_size - block_size; }if (code_size > block_size) { ... } else { code_size = 0; }else { ... } /* Move MPU table index. */ mpu_table_index = mpu_table_index + 2; /* Increment the MPU register index. */ mpu_register++; }for (i = 0; i < TXM_MODULE_MANAGER_CODE_MPU_ENTRIES - 1; i++) { ... } /* Adjust indeces to pass over the shared memory entry. */ /* Move MPU table index. */ mpu_table_index = mpu_table_index + 2; /* Increment the MPU register index. */ mpu_register++; }else { ... } /* Setup values for data area. */ data_address = (ULONG) module_instance -> txm_module_instance_data_start; /* Adjust the size of the module elements to be aligned to the default alignment. We do this so that when we partition the allocated memory, we can simply place these regions right beside each other without having to align their pointers. Note this only works when they all have the same alignment. *//* ... */ data_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_data_size; start_stop_stack_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_start_stop_stack_size; callback_stack_size = module_instance -> txm_module_instance_preamble_ptr -> txm_module_preamble_callback_stack_size; data_size = ((data_size + TXM_MODULE_DATA_ALIGNMENT - 1)/TXM_MODULE_DATA_ALIGNMENT) * TXM_MODULE_DATA_ALIGNMENT; start_stop_stack_size = ((start_stop_stack_size + TXM_MODULE_DATA_ALIGNMENT - 1)/TXM_MODULE_DATA_ALIGNMENT) * TXM_MODULE_DATA_ALIGNMENT; callback_stack_size = ((callback_stack_size + TXM_MODULE_DATA_ALIGNMENT - 1)/TXM_MODULE_DATA_ALIGNMENT) * TXM_MODULE_DATA_ALIGNMENT; /* Update the data size to include thread stacks. */ data_size = data_size + start_stop_stack_size + callback_stack_size; block_size = _txm_power_of_two_block_size(data_size / TXM_MODULE_MANAGER_DATA_MPU_ENTRIES); /* Reset SRD bitfield. */ srd_bits = 0; /* Calculate the region size information. */ region_size = (_txm_module_manager_region_size_get(block_size) << 1); /* Now loop through to setup MPU protection for the data area. */ for (i = 0; i < TXM_MODULE_MANAGER_DATA_MPU_ENTRIES; i++) { /* Build the base address register. */ base_address_register = (data_address & ~(block_size - 1)) | mpu_register | 0x10; /* Check if SRD bits need to be set. */ if (data_size < block_size) { srd_bits = _txm_module_manager_calculate_srd_bits(block_size, data_size); }if (data_size < block_size) { ... } /* Build the base attribute register. */ base_attribute_register = region_size | srd_bits | 0x13070000; /* Is there still some data? If so set the region enable bit. */ if (data_size) { /* Set the region enable bit. */ base_attribute_register = base_attribute_register | 0x1; }if (data_size) { ... } /* Setup the MPU Base Address Register. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index] = base_address_register; /* Setup the MPU Base Attribute Register. */ module_instance -> txm_module_instance_mpu_registers[mpu_table_index+1] = base_attribute_register; /* Adjust the data address. */ data_address = data_address + block_size; /* Decrement the data size. */ if (data_size > block_size) { data_size = data_size - block_size; }if (data_size > block_size) { ... } else { data_size = 0; }else { ... } /* Move MPU table index. */ mpu_table_index = mpu_table_index + 2; /* Increment the MPU register index. */ mpu_register++; }for (i = 0; i < TXM_MODULE_MANAGER_DATA_MPU_ENTRIES; i++) { ... } /* ... */ #endif }{ ... } #ifdef TXM_MODULE_MANAGER_16_MPU... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _txm_module_manager_inside_data_check Cortex-M4 */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function checks if the specified object is inside shared */ /* memory. */ /* */ /* INPUT */ /* */ /* module_instance Pointer to module instance */ /* obj_ptr Pointer to the object */ /* obj_size Size of the object */ /* */ /* OUTPUT */ /* */ /* Whether the object is inside the shared memory region. */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Module dispatch check functions */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 10-15-2021 Scott Larson Initial Version 6.1.9 */ /* */... /**************************************************************************/ UINT _txm_module_manager_inside_data_check(TXM_MODULE_INSTANCE *module_instance, ALIGN_TYPE obj_ptr, UINT obj_size) { UINT shared_memory_index; UINT num_shared_memory_mpu_entries; ALIGN_TYPE shared_memory_address_start; ALIGN_TYPE shared_memory_address_end; /* Check for overflow. */ if ((obj_ptr) > ((obj_ptr) + (obj_size))) { return(TX_FALSE); }if ((obj_ptr) > ((obj_ptr) + (obj_size))) { ... } /* Check if the object is inside the module data. */ if ((obj_ptr >= (ALIGN_TYPE) module_instance -> txm_module_instance_data_start) && ((obj_ptr + obj_size) <= ((ALIGN_TYPE) module_instance -> txm_module_instance_data_end + 1))) { return(TX_TRUE); }if ((obj_ptr >= (ALIGN_TYPE) module_instance -> txm_module_instance_data_start) && ((obj_ptr + obj_size) <= ((ALIGN_TYPE) module_instance -> txm_module_instance_data_end + 1))) { ... } /* Check if the object is inside the shared memory. */ num_shared_memory_mpu_entries = module_instance -> txm_module_instance_shared_memory_count; for (shared_memory_index = 0; shared_memory_index < num_shared_memory_mpu_entries; shared_memory_index++) { shared_memory_address_start = (ALIGN_TYPE) module_instance -> txm_module_instance_shared_memory_address[shared_memory_index]; shared_memory_address_end = shared_memory_address_start + module_instance -> txm_module_instance_shared_memory_length[shared_memory_index]; if ((obj_ptr >= (ALIGN_TYPE) shared_memory_address_start) && ((obj_ptr + obj_size) <= (ALIGN_TYPE) shared_memory_address_end)) { return(TX_TRUE); }if ((obj_ptr >= (ALIGN_TYPE) shared_memory_address_start) && ((obj_ptr + obj_size) <= (ALIGN_TYPE) shared_memory_address_end)) { ... } }for (shared_memory_index = 0; shared_memory_index < num_shared_memory_mpu_entries; shared_memory_index++) { ... } return(TX_FALSE); }_txm_module_manager_inside_data_check (TXM_MODULE_INSTANCE *module_instance, ALIGN_TYPE obj_ptr, UINT obj_size) { ... } ...#endif/* ... */