[PATCH rtems 2/3] bsps/atsam: Add NULL pointer protection

Christian Mauderer christian.mauderer at embedded-brains.de
Fri Dec 9 09:20:17 UTC 2022


---
 bsps/arm/atsam/README                           |  6 +++++-
 .../libraries/libboard/source/board_lowlevel.c  | 17 +++++++++++++++++
 bsps/arm/atsam/include/bsp.h                    |  4 ++++
 bsps/arm/atsam/include/libchip/include/mpu.h    |  1 +
 bsps/arm/atsam/start/bspstarthooks.c            |  2 +-
 spec/build/bsps/arm/atsam/bspatsam.yml          |  2 ++
 spec/build/bsps/arm/atsam/linkcmds.yml          |  7 ++++++-
 spec/build/bsps/arm/atsam/optnullsz.yml         | 17 +++++++++++++++++
 spec/build/bsps/arm/atsam/opttcmsz.yml          |  3 ++-
 9 files changed, 55 insertions(+), 4 deletions(-)
 create mode 100644 spec/build/bsps/arm/atsam/optnullsz.yml

diff --git a/bsps/arm/atsam/README b/bsps/arm/atsam/README
index 2ebaa726c8..47aa11d2c0 100644
--- a/bsps/arm/atsam/README
+++ b/bsps/arm/atsam/README
@@ -59,9 +59,13 @@ Use ATSAM_CONSOLE_DEVICE_INDEX=XYZ to set the device index for /dev/console
 Use ATSAM_CONSOLE_USE_INTERRUPTS=XYZ to set the use interrupt driven mode for
 console devices (used by default).
 
-Use ATSAM_MEMORY_TCM_SIZE=XYZ to set the size of tightly coupled memories (TCM)
+Use ATSAM_MEMORY_NULL_SIZE=XYZ to set the size of NULL pointer protection area
 in bytes (default 0x00000000).
 
+Use ATSAM_MEMORY_TCM_SIZE=XYZ to set the size of tightly coupled memories (TCM)
+in bytes (default 0x00000000). Note: ITCM is reduced by the
+ATSAM_MEMORY_NULL_SIZE.
+
 Use ATSAM_MEMORY_INTFLASH_SIZE=XYZ to set the size of internal flash in bytes
 (default is derived from chip variant).
 
diff --git a/bsps/arm/atsam/contrib/libraries/libboard/source/board_lowlevel.c b/bsps/arm/atsam/contrib/libraries/libboard/source/board_lowlevel.c
index cbdf41ba73..a2dd595fb5 100644
--- a/bsps/arm/atsam/contrib/libraries/libboard/source/board_lowlevel.c
+++ b/bsps/arm/atsam/contrib/libraries/libboard/source/board_lowlevel.c
@@ -347,6 +347,23 @@ void _SetupMemoryRegion(void)
 	SCB->SHCSR |= (SCB_SHCSR_MEMFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk
 				   | SCB_SHCSR_USGFAULTENA_Msk);
 
+#ifdef __rtems__
+	dwRegionBaseAddr =
+		((uintptr_t)atsam_memory_null_begin) |
+		MPU_REGION_VALID |
+		MPU_NULL_REGION;
+	if (atsam_memory_null_begin != atsam_memory_itcm_end) {
+		dwRegionAttr =
+			MPU_AP_NO_ACCESS |
+			MPU_REGION_EXECUTE_NEVER |
+			MPU_CalMPURegionSize((uintptr_t)atsam_memory_null_size) |
+			MPU_REGION_ENABLE;
+	} else {
+		dwRegionAttr = MPU_REGION_DISABLE;
+	}
+	MPU_SetRegion(dwRegionBaseAddr, dwRegionAttr);
+#endif /* __rtems__ */
+
 	/* Enable the MPU region */
 #ifndef __rtems__
 	MPU_Enable(MPU_ENABLE | MPU_PRIVDEFENA);
diff --git a/bsps/arm/atsam/include/bsp.h b/bsps/arm/atsam/include/bsp.h
index 8fe98be364..0bca3d2c22 100644
--- a/bsps/arm/atsam/include/bsp.h
+++ b/bsps/arm/atsam/include/bsp.h
@@ -89,6 +89,10 @@ typedef struct {
   uint8_t phy_addr;
 } if_atsam_config;
 
+extern char atsam_memory_null_begin[];
+extern char atsam_memory_null_end[];
+extern char atsam_memory_null_size[];
+
 extern char atsam_memory_dtcm_begin[];
 extern char atsam_memory_dtcm_end[];
 extern char atsam_memory_dtcm_size[];
diff --git a/bsps/arm/atsam/include/libchip/include/mpu.h b/bsps/arm/atsam/include/libchip/include/mpu.h
index a5d00a681b..034ba58474 100644
--- a/bsps/arm/atsam/include/libchip/include/mpu.h
+++ b/bsps/arm/atsam/include/libchip/include/mpu.h
@@ -56,6 +56,7 @@
 #endif
 #define MPU_SYSTEM_REGION                       (12)
 #ifdef __rtems__
+#define MPU_NULL_REGION                         (13)
 /* Reserve the region with highest priority for user applications */
 #define MPU_USER_DEFINED_REGION                 (15)
 #endif /* __rtems__ */
diff --git a/bsps/arm/atsam/start/bspstarthooks.c b/bsps/arm/atsam/start/bspstarthooks.c
index 516b86228d..2be2fc050b 100644
--- a/bsps/arm/atsam/start/bspstarthooks.c
+++ b/bsps/arm/atsam/start/bspstarthooks.c
@@ -106,7 +106,7 @@ static void configure_tcm(void)
   uintptr_t tcm_size;
   uint32_t itcmcr_sz;
 
-  tcm_size = (uintptr_t) atsam_memory_itcm_size;
+  tcm_size = (uintptr_t) atsam_memory_dtcm_size;
   itcmcr_sz = (SCB->ITCMCR & SCB_ITCMCR_SZ_Msk) >> SCB_ITCMCR_SZ_Pos;
 
   if (tcm_setup_and_check_if_do_efc_config(tcm_size, itcmcr_sz)) {
diff --git a/spec/build/bsps/arm/atsam/bspatsam.yml b/spec/build/bsps/arm/atsam/bspatsam.yml
index 6716bea248..679889d253 100644
--- a/spec/build/bsps/arm/atsam/bspatsam.yml
+++ b/spec/build/bsps/arm/atsam/bspatsam.yml
@@ -296,6 +296,8 @@ links:
   uid: optmck
 - role: build-dependency
   uid: optnocachesz
+- role: build-dependency
+  uid: optnullsz
 - role: build-dependency
   uid: optoscmain
 - role: build-dependency
diff --git a/spec/build/bsps/arm/atsam/linkcmds.yml b/spec/build/bsps/arm/atsam/linkcmds.yml
index fe6211f82f..d475b6a245 100644
--- a/spec/build/bsps/arm/atsam/linkcmds.yml
+++ b/spec/build/bsps/arm/atsam/linkcmds.yml
@@ -2,7 +2,8 @@ SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
 build-type: config-file
 content: |
   MEMORY {
-    ITCM      : ORIGIN = 0x00000000, LENGTH = ${ATSAM_MEMORY_TCM_SIZE}
+    NULL      : ORIGIN = 0x00000000, LENGTH = ${ATSAM_MEMORY_NULL_SIZE}
+    ITCM      : ORIGIN = ${ATSAM_MEMORY_NULL_SIZE}, LENGTH = ((${ATSAM_MEMORY_TCM_SIZE} > ${ATSAM_MEMORY_NULL_SIZE}) ? (${ATSAM_MEMORY_TCM_SIZE} - ${ATSAM_MEMORY_NULL_SIZE}) : 0)
     INTFLASH  : ORIGIN = 0x00400000, LENGTH = ${ATSAM_MEMORY_INTFLASH_SIZE}
     DTCM      : ORIGIN = 0x20000000, LENGTH = ${ATSAM_MEMORY_TCM_SIZE}
     INTSRAM   : ORIGIN = 0x20400000, LENGTH = ${ATSAM_MEMORY_INTSRAM_SIZE} - 2 * ${ATSAM_MEMORY_TCM_SIZE} - ${ATSAM_MEMORY_NOCACHE_SIZE}
@@ -13,6 +14,10 @@ content: |
 
   /* Must be used only for MPU definitions */
 
+  atsam_memory_null_begin = ORIGIN (NULL);
+  atsam_memory_null_end = ORIGIN (NULL) + LENGTH (NULL);
+  atsam_memory_null_size = LENGTH (NULL);
+
   atsam_memory_itcm_begin = ORIGIN (ITCM);
   atsam_memory_itcm_end = ORIGIN (ITCM) + LENGTH (ITCM);
   atsam_memory_itcm_size = LENGTH (ITCM);
diff --git a/spec/build/bsps/arm/atsam/optnullsz.yml b/spec/build/bsps/arm/atsam/optnullsz.yml
new file mode 100644
index 0000000000..d992c286e7
--- /dev/null
+++ b/spec/build/bsps/arm/atsam/optnullsz.yml
@@ -0,0 +1,17 @@
+SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
+actions:
+- get-integer: null
+- env-assign: null
+build-type: option
+default: 0
+default-by-variant: []
+enabled-by: true
+format: '{:#010x}'
+links: []
+name: ATSAM_MEMORY_NULL_SIZE
+description: |
+  Size of the NULL pointer protection area in bytes.  This memory area reduces
+  the size of the ITCM available to the application.
+type: build
+copyrights:
+- Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
diff --git a/spec/build/bsps/arm/atsam/opttcmsz.yml b/spec/build/bsps/arm/atsam/opttcmsz.yml
index 7e8b1121e8..da3e575f55 100644
--- a/spec/build/bsps/arm/atsam/opttcmsz.yml
+++ b/spec/build/bsps/arm/atsam/opttcmsz.yml
@@ -9,7 +9,8 @@ copyrights:
 default: 0
 default-by-variant: []
 description: |
-  size of tightly coupled memories (TCM) in bytes
+  Size of tightly coupled memories (TCM) in bytes.  Note that the ITCM is
+  reduced by the ATSAM_MEMORY_NULL_SIZE option.  DTCM is unaffected.
 enabled-by: true
 format: '{:#010x}'
 links: []
-- 
2.35.3



More information about the devel mailing list