@@ -4,25 +4,165 @@ config RT_USING_MEM_PROTECTION
44 bool "Enable memory protection"
55 default n
66 select RT_USING_HEAP
7+ help
8+ Enable memory protection framework using MPU (Memory Protection Unit).
9+
10+ Provides hardware-based memory protection to prevent:
11+ - Stack overflow and underflow
12+ - Buffer overruns
13+ - Unauthorized access to memory regions
14+ - Corruption of kernel data structures
15+
16+ Features:
17+ - Per-thread memory protection
18+ - Stack guard regions
19+ - Configurable memory regions with access permissions
20+ - Exclusive regions for sensitive data
21+
22+ Requirements:
23+ - CPU with MPU support (Cortex-M3/M4/M7/M33/R52, etc.)
24+ - ARCH_MM_MPU or ARCH_ARM_MPU enabled
25+ - RT_USING_HEAP (dynamic memory)
26+
27+ Benefits:
28+ - Catch memory bugs early (development)
29+ - Improve system safety (production)
30+ - Isolate thread memory spaces
31+
32+ Costs:
33+ - Context switch overhead (save/restore MPU regions)
34+ - Configuration complexity
35+ - Slight runtime overhead
36+
37+ Ideal for safety-critical applications and debugging memory issues.
738
839config RT_USING_HW_STACK_GUARD
940 bool "Enable hardware stack guard"
1041 default n
1142 select RT_USING_MEM_PROTECTION
43+ help
44+ Enable hardware-based stack overflow protection using MPU.
45+
46+ Automatically creates guard regions at the end of each thread's stack
47+ to detect stack overflow at the moment it occurs.
48+
49+ How it works:
50+ - Configures MPU region below stack as no-access
51+ - Stack overflow triggers immediate MPU fault
52+ - Exception handler identifies the overflowing thread
53+
54+ Benefits:
55+ - Immediate detection (vs periodic checks)
56+ - Precise identification of overflow location
57+ - No runtime overhead (hardware-based)
58+ - Catches stack corruption before it spreads
59+
60+ Requirements:
61+ - RT_USING_MEM_PROTECTION enabled
62+ - Sufficient MPU regions (at least 1 per thread)
63+
64+ Recommended for:
65+ - Development and debugging
66+ - Safety-critical systems
67+ - Systems with limited stack space
68+
69+ Note: Requires one MPU region per thread. Check NUM_MEM_REGIONS configuration.
1270
1371if RT_USING_MEM_PROTECTION
1472 config USE_MEM_PROTECTION_EXAMPLES
1573 bool "Use memory protection examples"
1674 default y
75+ help
76+ Build and include memory protection example code.
77+
78+ Provides demonstration code showing how to:
79+ - Configure memory protection regions
80+ - Set up stack guards
81+ - Add exclusive regions
82+ - Handle protection faults
83+
84+ Useful for:
85+ - Learning memory protection concepts
86+ - Testing MPU functionality
87+ - Debugging protection configurations
88+
89+ Disable for production builds to save code space.
1790
1891 config NUM_MEM_REGIONS
1992 int "Total number of memory protection regions supported by hardware"
93+ help
94+ Total number of MPU regions available in hardware.
95+
96+ This depends on your CPU architecture:
97+ - Cortex-M3/M4: Typically 8 regions
98+ - Cortex-M7: 8 or 16 regions
99+ - Cortex-M33/M85: 8 or 16 regions (configurable in silicon)
100+ - Cortex-R52: 16 or 32 regions
101+
102+ Check your MCU datasheet or reference manual for exact count.
103+
104+ Usage breakdown:
105+ - Stack guard per thread: 1 region per thread
106+ - Exclusive regions: NUM_EXCLUSIVE_REGIONS
107+ - Configurable per thread: NUM_CONFIGURABLE_REGIONS
108+
109+ Formula: NUM_MEM_REGIONS = 1 (stack) + NUM_EXCLUSIVE_REGIONS + NUM_CONFIGURABLE_REGIONS
110+
111+ Example for 8-region MPU with 2 threads:
112+ - Thread 1 stack guard: 1 region
113+ - Thread 2 stack guard: 1 region
114+ - Shared exclusive regions: 2 regions
115+ - Per-thread config: 2 regions per thread
116+ Total: 2 + 2 + 4 = 8 regions
20117
21118 config NUM_EXCLUSIVE_REGIONS
22119 int "Total number of exclusive memory regions added using rt_mprotect_add_exclusive_region API"
120+ help
121+ Number of shared exclusive memory protection regions.
122+
123+ Exclusive regions are memory areas with specific access permissions
124+ shared across all threads (e.g., peripheral registers, kernel data).
125+
126+ Use cases:
127+ - Protect peripheral registers from thread access
128+ - Mark flash memory as read-only/execute-only
129+ - Protect kernel data structures
130+ - Define shared memory with specific permissions
131+
132+ Example configurations:
133+ - Peripheral protection: 1-2 regions
134+ - Kernel data protection: 1 region
135+ - Shared buffers: 0-2 regions
136+
137+ Note: These regions are configured once and apply to all threads.
138+ Set based on your system's protection requirements.
23139
24140 config NUM_CONFIGURABLE_REGIONS
25141 int "Maximum number of configurable memory regions for each thread, excluding stack guard and exclusive regions added using rt_mprotect_add_exclusive_region API"
142+ help
143+ Per-thread configurable memory protection regions.
144+
145+ These regions are specific to each thread and can be configured independently
146+ for thread-specific memory protection needs.
147+
148+ Use cases:
149+ - Protect thread's private data buffers
150+ - Restrict access to thread-local peripherals
151+ - Define custom memory permissions per thread
152+
153+ Calculation:
154+ NUM_CONFIGURABLE_REGIONS = NUM_MEM_REGIONS - 1 (stack) - NUM_EXCLUSIVE_REGIONS
155+
156+ Example for 8-region MPU with 2 exclusive regions:
157+ NUM_CONFIGURABLE_REGIONS = 8 - 1 - 2 = 5 regions per thread
158+
159+ More regions = more flexibility but:
160+ - Increased configuration complexity
161+ - Longer context switch time
162+ - More memory for region descriptors
163+
164+ Set based on your application's per-thread protection needs.
165+ Typical values: 1-4 regions per thread.
26166endif
27167
28168endmenu
0 commit comments