Repository: chipsalliance/caliptra-sw
Pull request #596 (2023-08-04 19:58:00)

Fixes for linker script generation
1. RTALIAS_TBS_ORG was missing from memory_layout.rs. This takes 1K away
   from the DATA section.
2. Generate the FMC and RT linker scripts entirely from the constants in
   memory_layout.rs
3. Shrink ROM data section by 1K to account for RTALIAS_TBS_ORG. This
   prevents ROM from overwriting the RT Alias Cert on resets.
4. Increase RT and FMC stack size to 22K (from 14). This is necessary to
   account for the lager DPE command/response buffers. This takes
   another 8K away from data.

Total change:

* Stack increases by 8K
* RT Alias takes up 1K
* Data shrinks by 9K


Patch:
diff --git a/cpu/gen/src/lib.rs b/cpu/gen/src/lib.rs
index c792a656..bd7de260 100644
--- a/cpu/gen/src/lib.rs
+++ b/cpu/gen/src/lib.rs
@@ -1,30 +1,30 @@
 // Licensed under the Apache-2.0 license
-use caliptra_common::memory_layout::{FMCALIAS_TBS_ORG, LDEVID_TBS_ORG};
+use caliptra_common::memory_layout::*;
 pub fn gen_memory_x(iccm_org: u32, iccm_size: u32) -> String {
     format!(
         r#"
-        ICCM_ORG  		 = 0x{:08X};
-        DCCM_ORG  		 = 0x50000000;
-        DATA_ORG  		 = 0x50004800;
-        STACK_ORG 		 = 0x5001C000;
-        ESTACK_ORG  	 = 0x5001F800;
-        NSTACK_ORG       = 0x5001FC00;
-        LDEVID_TBS_ORG   = 0x{:08X};;
-        FMCALIAS_TBS_ORG = 0x{:08X};;
-        RTALIAS_TBS_ORG  = 0x50004000;
-        
-        LDEVID_TBS_SIZE   = 1K;
-        FMCALIAS_TBS_SIZE = 1K;
-        RTALIAS_TBS_SIZE  = 1K;
-              
-        ICCM_SIZE   = 0x{:08X};
-        DCCM_SIZE   = 128K;
-        DATA_SIZE   = 94K;
-        STACK_SIZE  = 14K;
-        ESTACK_SIZE = 1K;
-        NSTACK_SIZE = 1K;
-        
-        
+        ICCM_ORG  		 = 0x{iccm_org:08X};
+        DCCM_ORG  		 = 0x{DCCM_ORG:08X};
+        DATA_ORG  		 = 0x{DATA_ORG:08X};
+        STACK_ORG 		 = 0x{STACK_ORG:08X};
+        ESTACK_ORG  	 = 0x{ESTACK_ORG:08X};
+        NSTACK_ORG       = 0x{NSTACK_ORG:08X};
+        LDEVID_TBS_ORG   = 0x{LDEVID_TBS_ORG:08X};;
+        FMCALIAS_TBS_ORG = 0x{FMCALIAS_TBS_ORG:08X};;
+        RTALIAS_TBS_ORG  = 0x{RTALIAS_TBS_ORG:08X};
+
+        LDEVID_TBS_SIZE   = 0x{LDEVID_TBS_SIZE:08X};
+        FMCALIAS_TBS_SIZE = 0x{FMCALIAS_TBS_SIZE:08X};
+        RTALIAS_TBS_SIZE  = 0x{RTALIAS_TBS_SIZE:08X};
+
+        ICCM_SIZE   = 0x{iccm_size:08X};
+        DCCM_SIZE   = 0x{DCCM_SIZE:08X};
+        DATA_SIZE   = 0x{DATA_SIZE:08X};
+        STACK_SIZE  = 0x{STACK_SIZE:08X};
+        ESTACK_SIZE = 0x{ESTACK_SIZE:08X};
+        NSTACK_SIZE = 0x{NSTACK_SIZE:08X};
+
+
         MEMORY
         {{
             ICCM 		 (rx) : ORIGIN = ICCM_ORG, 		   LENGTH = ICCM_SIZE
@@ -33,14 +33,13 @@ pub fn gen_memory_x(iccm_org: u32, iccm_size: u32) -> String {
             STACK	     (rw) : ORIGIN = STACK_ORG,  	   LENGTH = STACK_SIZE
             ESTACK 		 (rw) : ORIGIN = ESTACK_ORG,       LENGTH = ESTACK_SIZE
             NSTACK       (rw) : ORIGIN = NSTACK_ORG,       LENGTH = NSTACK_SIZE
-        }} 
-        REGION_ALIAS("REGION_TEXT", ICCM);     
-        REGION_ALIAS("REGION_RODATA", ICCM);  
-        REGION_ALIAS("REGION_DATA", DATA);     
-        REGION_ALIAS("REGION_BSS", DATA);      
-        REGION_ALIAS("REGION_STACK", STACK);     
-        REGION_ALIAS("REGION_ESTACK", ESTACK);     
-        REGION_ALIAS("REGION_NSTACK", NSTACK);"#,
-        iccm_org, LDEVID_TBS_ORG, FMCALIAS_TBS_ORG, iccm_size,
+        }}
+        REGION_ALIAS("REGION_TEXT", ICCM);
+        REGION_ALIAS("REGION_RODATA", ICCM);
+        REGION_ALIAS("REGION_DATA", DATA);
+        REGION_ALIAS("REGION_BSS", DATA);
+        REGION_ALIAS("REGION_STACK", STACK);
+        REGION_ALIAS("REGION_ESTACK", ESTACK);
+        REGION_ALIAS("REGION_NSTACK", NSTACK);"#
     )
 }
diff --git a/drivers/src/memory_layout.rs b/drivers/src/memory_layout.rs
index e1c342e3..796cdaa4 100644
--- a/drivers/src/memory_layout.rs
+++ b/drivers/src/memory_layout.rs
@@ -30,11 +30,12 @@ pub const MAN2_ORG: u32 = 0x50001800;
 pub const FHT_ORG: u32 = 0x50003000;
 pub const LDEVID_TBS_ORG: u32 = 0x50003800;
 pub const FMCALIAS_TBS_ORG: u32 = 0x50003C00;
-pub const PCR_LOG_ORG: u32 = 0x50004000;
-pub const FUSE_LOG_ORG: u32 = 0x50004400;
-pub const BOOT_STATUS_ORG: u32 = 0x500047FC;
-pub const DATA_ORG: u32 = 0x50004800;
-pub const STACK_ORG: u32 = 0x5001C000;
+pub const RTALIAS_TBS_ORG: u32 = 0x50004000;
+pub const PCR_LOG_ORG: u32 = 0x50004400;
+pub const FUSE_LOG_ORG: u32 = 0x50004800;
+pub const BOOT_STATUS_ORG: u32 = 0x50004BFC;
+pub const DATA_ORG: u32 = 0x50004C00;
+pub const STACK_ORG: u32 = 0x5001A000;
 pub const ESTACK_ORG: u32 = 0x5001F800;
 pub const NSTACK_ORG: u32 = 0x5001FC00;
 
@@ -51,11 +52,12 @@ pub const MAN2_SIZE: u32 = 6 * 1024;
 pub const FHT_SIZE: u32 = 2 * 1024;
 pub const LDEVID_TBS_SIZE: u32 = 1024;
 pub const FMCALIAS_TBS_SIZE: u32 = 1024;
+pub const RTALIAS_TBS_SIZE: u32 = 1024;
 pub const PCR_LOG_SIZE: usize = 1024;
 pub const FUSE_LOG_SIZE: usize = 1020;
 pub const BOOT_STATUS_SIZE: u32 = 4;
-pub const DATA_SIZE: u32 = 94 * 1024;
-pub const STACK_SIZE: u32 = 14 * 1024;
+pub const DATA_SIZE: u32 = 85 * 1024;
+pub const STACK_SIZE: u32 = 22 * 1024;
 pub const ESTACK_SIZE: u32 = 1024;
 pub const NSTACK_SIZE: u32 = 1024;
 
@@ -85,7 +87,13 @@ fn mem_layout_test_ldevid() {
 #[test]
 #[allow(clippy::assertions_on_constants)]
 fn mem_layout_test_fmcalias() {
-    assert_eq!((PCR_LOG_ORG - FMCALIAS_TBS_ORG), FMCALIAS_TBS_SIZE);
+    assert_eq!((RTALIAS_TBS_ORG - FMCALIAS_TBS_ORG), FMCALIAS_TBS_SIZE);
+}
+
+#[test]
+#[allow(clippy::assertions_on_constants)]
+fn mem_layout_test_rtalias() {
+    assert_eq!((PCR_LOG_ORG - RTALIAS_TBS_ORG), RTALIAS_TBS_SIZE);
 }
 
 #[test]
diff --git a/rom/dev/src/rom.ld b/rom/dev/src/rom.ld
index 6b343763..243f2e55 100644
--- a/rom/dev/src/rom.ld
+++ b/rom/dev/src/rom.ld
@@ -19,7 +19,7 @@ ENTRY(_start)
 ROM_ORG          = 0x00000000;
 ICCM_ORG         = 0x40000000;
 DCCM_ORG         = 0x50000000;
-DATA_ORG         = 0x50004800;
+DATA_ORG         = 0x50004C00;
 STACK_ORG        = 0x5001C000;
 ESTACK_ORG       = 0x5001F800;
 NSTACK_ORG       = 0x5001FC00;
@@ -33,7 +33,7 @@ ROM_RELAXATION_PADDING = 4k;
 ROM_SIZE          = 48K;
 ICCM_SIZE         = 128K;
 DCCM_SIZE         = 128K;
-DATA_SIZE         = 94K;
+DATA_SIZE         = 93K;
 STACK_SIZE        = 14K;
 ESTACK_SIZE       = 1K;
 NSTACK_SIZE       = 1K;


Test patch:
diff --git a/fmc/test-fw/test-rt/memory.x b/fmc/test-fw/test-rt/memory.x
index cd6c0f7c..405acd02 100644
--- a/fmc/test-fw/test-rt/memory.x
+++ b/fmc/test-fw/test-rt/memory.x
@@ -5,7 +5,7 @@ META_ORG  = ROM_SIZE;
 ICCM_ORG  = 0x40004000; /* Range [0x40000000 - 0x40003FFF] is reserved for FMC */
 DCCM_ORG  = 0x50000000;
 FHT_ORG   = 0x50003000;
-DATA_ORG  = 0x50004800;
+DATA_ORG  = 0x50004C00;
 STACK_ORG = 0x5001C000;
 ESTACK_ORG  = 0x5001F800;
 NSTACK_ORG  = 0x5001FC00;
@@ -18,7 +18,7 @@ DCCM_SIZE   = 128K;
 MAN1_SIZE   = 6K;
 MAN2_SIZE   = 6K;
 FHT_SIZE    = 2K;
-DATA_SIZE   = 94K;
+DATA_SIZE   = 93K;
 STACK_SIZE  = 14K;
 ESTACK_SIZE = 1K;
 NSTACK_SIZE = 1K;
diff --git a/rom/dev/tests/test_fmcalias_derivation.rs b/rom/dev/tests/test_fmcalias_derivation.rs
index 8c72b632..72a92db0 100644
--- a/rom/dev/tests/test_fmcalias_derivation.rs
+++ b/rom/dev/tests/test_fmcalias_derivation.rs
@@ -638,8 +638,8 @@ fn test_fht_info() {
     assert_eq!(fht.fmcalias_tbs_size, 745);
     assert_eq!(fht.ldevid_tbs_addr, 0x50003800);
     assert_eq!(fht.fmcalias_tbs_addr, 0x50003C00);
-    assert_eq!(fht.pcr_log_addr, 0x50004000);
-    assert_eq!(fht.fuse_log_addr, 0x50004400);
+    assert_eq!(fht.pcr_log_addr, 0x50004400);
+    assert_eq!(fht.fuse_log_addr, 0x50004800);
 
     // [TODO] Expand test to validate additional FHT fields.
 }
diff --git a/rom/dev/tools/test-fmc/src/fmc.ld b/rom/dev/tools/test-fmc/src/fmc.ld
index 5dfa727d..d0dd9b36 100644
--- a/rom/dev/tools/test-fmc/src/fmc.ld
+++ b/rom/dev/tools/test-fmc/src/fmc.ld
@@ -19,7 +19,7 @@ ENTRY(_start)
 
 ICCM_ORG         = 0x40000000;
 DCCM_ORG         = 0x50000000;
-DATA_ORG         = 0x50004800;
+DATA_ORG         = 0x50004C00;
 STACK_ORG        = 0x5001C000;
 ESTACK_ORG       = 0x5001F800;
 NSTACK_ORG       = 0x5001FC00;
@@ -27,7 +27,7 @@ NSTACK_ORG       = 0x5001FC00;
 
 ICCM_SIZE         = 128K;
 DCCM_SIZE         = 128K;
-DATA_SIZE         = 94K;
+DATA_SIZE         = 93K;
 STACK_SIZE        = 14K;
 ESTACK_SIZE       = 1K;
 NSTACK_SIZE       = 1K;
diff --git a/rom/dev/tools/test-rt/src/rt.ld b/rom/dev/tools/test-rt/src/rt.ld
index 7b3809bc..4edeb020 100644
--- a/rom/dev/tools/test-rt/src/rt.ld
+++ b/rom/dev/tools/test-rt/src/rt.ld
@@ -19,14 +19,14 @@ ENTRY(_start)
 
 ICCM_ORG    = 0x40004000; /* Range [0x40000000 - 0x40003FFF] is reserved for FMC */
 DCCM_ORG    = 0x50000000;
-DATA_ORG    = 0x50004800;
+DATA_ORG    = 0x50004C00;
 STACK_ORG   = 0x5001C000;
 ESTACK_ORG  = 0x5001F800;
 NSTACK_ORG  = 0x5001FC00;
 
 ICCM_SIZE   = 112K;
 DCCM_SIZE   = 128K;
-DATA_SIZE   = 94K;
+DATA_SIZE   = 93K;
 STACK_SIZE  = 14K;
 ESTACK_SIZE = 1K;
 NSTACK_SIZE = 1K;