@@ -1048,6 +1048,22 @@ static void printSanityCheck() {
10481048}
10491049#endif // DEBUG_ESP_HWDT_DEV_DEBUG
10501050
1051+ /*
1052+ hwdt_pre_sdk_init() is the result of a hook for development diagnotics which
1053+ evolved and was generlized to run any optional diagnostic code supplied at
1054+ link time.
1055+
1056+ Summary of the hwdt_pre_sdk_init() runtime environment:
1057+ * The code can run from flash and use PROGMEM strings.
1058+ * All functions must be extern "C" type
1059+ * C/C++ runtime has not started. Structures have not been initialized and
1060+ should have the values prior to reboot. With the exception of hwdt_info,
1061+ which was updated before this call.
1062+ * You can reference hwdt_info.reset_reason to control the action of the diagnostic.
1063+ * The stack is on the SYS stack. You have about 3K available before you
1064+ overwrite ROM Data area.
1065+ * Printing will work best with ets_uart_printf and umm_info_safe_printf_P.
1066+ */
10511067void hwdt_pre_sdk_init (void ) __attribute__((weak));
10521068void hwdt_pre_sdk_init (void ) {
10531069#if defined(DEBUG_ESP_HWDT_DEV_DEBUG) && !defined(USE_IRAM)
@@ -1072,14 +1088,12 @@ void hwdt_pre_sdk_init_icache(void) {
10721088 Cache_Read_Disable ();
10731089}
10741090
1075-
1076- #if 1
10771091/*
1078- An asm function alternative to the function with inline asm at the #else. I
1079- find the inline asm requires constant inspection to verify that the compiler
1080- optimizer does not clobber needed registers, after small changes in code or
1081- compiler updates. Hints to the compiler don't always work for me. Last I
1082- checked, the inline version below was working .
1092+ For app_entry_redefinable, use Basic ASM instead of "C" with Extended ASM. The
1093+ (inline) Extended ASM approach required constant inspection to verify that the
1094+ compiler's optimizer did not clobber needed registers or do something weird
1095+ after minor changes in code or compiler updates. Also, I think Basic ASM is
1096+ the safer route when changing the stack pointer multiple times .
10831097*/
10841098cont_t *hwdt_app_entry__cont_stack __attribute__ ((used)) = CONT_STACK;
10851099
@@ -1089,6 +1103,7 @@ asm (
10891103 " .literal .g_pcont, g_pcont\n\t "
10901104 " .literal .pcont_stack, hwdt_app_entry__cont_stack\n\t "
10911105 " .literal .sys_stack_first, sys_stack_first\n\t "
1106+ " .literal .umm_init, umm_init\n\t "
10921107 " .literal .call_user_start, call_user_start\n\t "
10931108 " .literal .get_noextra4k_g_pcont, get_noextra4k_g_pcont\n\t "
10941109 " .align 4\n\t "
@@ -1135,90 +1150,39 @@ asm (
11351150 " l32r a13, .pcont_stack\n\t "
11361151 " l32r a0, .get_noextra4k_g_pcont\n\t "
11371152 " l32r a14, .g_pcont\n\t "
1153+ // We now switch to the SYS stack the SDK will use
11381154 " l32i.n a1, a2, 0\n\t " // delayed load for pipeline
11391155 " l32i.n a13, a13, 0\n\t "
11401156 " callx0 a0\n\t "
11411157 " moveqz a2, a13, a2\n\t "
11421158 " s32i.n a2, a14, 0\n\t "
11431159
1160+ /*
1161+ * Allow for running additional diagnotics supplied at link time.
1162+ */
11441163 " call0 hwdt_pre_sdk_init_icache\n\t "
11451164
1165+ // In case somebody cares, leave things as we found them
1166+ // - Restore ROM BSS zeros.
11461167 " movi a2, 0x3FFFE000\n\t " // ROM BSS Area
11471168 " movi a3, 0x0b30\n\t " // ROM BSS Size
11481169 " call0 ets_bzero\n\t "
11491170
1171+ /*
1172+ * Up until this call, the heap at crash time has been available for
1173+ * analysis. This is needed for dumping the bearssl stack. Also, future
1174+ * improvements could possibly use hwdt_pre_sdk_init() to run other early
1175+ * diagnostic tools.
1176+ */
1177+ " l32r a0, .umm_init\n\t "
1178+ " callx0 a0\n\t "
1179+
11501180 " l32r a3, .call_user_start\n\t "
11511181 " movi a0, 0x4000044c\n\t "
11521182 " jx a3\n\t "
11531183 " .size app_entry_redefinable, .-app_entry_redefinable\n\t "
11541184);
11551185
1156- #else
1157- void IRAM_ATTR app_entry_start(void) {
1158-
1159- #ifdef USE_IRAM
1160- handle_hwdt();
1161- #else
1162- handle_hwdt_icache();
1163- #endif
1164-
1165- /*
1166- * Continuation context is in BSS.
1167- */
1168- g_pcont = get_noextra4k_g_pcont();
1169-
1170- if (!g_pcont) {
1171- /*
1172- * The continuation context is on the stack just after the reserved
1173- * space for the ROM/eboot stack and before the SYS stack begins. All
1174- * computations were done at top, save pointer to it now.
1175- */
1176- g_pcont = CONT_STACK;
1177- }
1178- #if defined(DEBUG_ESP_HWDT_DEV_DEBUG) && !defined(USE_IRAM)
1179- print_sanity_check_icache();
1180- #endif
1181- /*
1182- * Use new calculated SYS stack from top.
1183- * Call the entry point of the SDK code.
1184- */
1185- asm volatile ("mov.n a1, %0\n\t"
1186- "movi a0, 0x4000044c\n\t" /* Should never return; however, set return to Boot ROM Breakpoint */
1187- "jx %1\n\t" ::
1188- "r" (sys_stack_first), "r" (call_user_start):
1189- "a0", "memory");
1190-
1191- __builtin_unreachable();
1192- }
1193-
1194- void IRAM_ATTR app_entry_redefinable(void) {
1195- /*
1196- * There are 4 sections of code that share the stack starting near
1197- * 0x40000000.
1198- * 1) The Boot ROM (uses around 640 bytes)
1199- * 2) The Bootloader, eboot.elf (last seen using 720 bytes.)
1200- * 3) `app_entry_redefinable()` just before it starts the SDK.
1201- * 4) The NONOS SDK, optionally the Core when the extra 4K option is
1202- * selected.
1203- *
1204- * Use the ROM BSS zeroed out memory as the home for our temporary stack.
1205- * This way no additional information will be lost. That will remove this
1206- * tool from the list of possible concerns for stack overwrite.
1207- *
1208- */
1209-
1210- asm volatile ("movi a1, 0x3fffeb30\n\t"
1211- "j app_entry_start" ::: "memory");
1212-
1213- /*
1214- * Keep this function with just asm seems to help avoid a stack frame being
1215- * created for this function and things getting really confused.
1216- */
1217-
1218- __builtin_unreachable();
1219- }
1220- #endif
1221-
12221186#if defined(DEBUG_ESP_HWDT_INFO) || defined(ROM_STACK_DUMP)
12231187void debug_hwdt_init (void ) {
12241188 /*
0 commit comments