Loading...
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 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | /* * Copyright (c) 2014 Wind River Systems, Inc. * * SPDX-License-Identifier: Apache-2.0 */ /** * @file * @brief Common fault handler for ARM Cortex-M * * Common fault handler for ARM Cortex-M processors. */ #include <toolchain.h> #include <linker/sections.h> #include <kernel.h> #include <kernel_structs.h> #include <inttypes.h> #include <exc_handle.h> #include <logging/log_ctrl.h> #if defined(CONFIG_PRINTK) || defined(CONFIG_LOG) #define PR_EXC(...) z_fatal_print(__VA_ARGS__) #define STORE_xFAR(reg_var, reg) u32_t reg_var = (u32_t)reg #else #define PR_EXC(...) #define STORE_xFAR(reg_var, reg) #endif /* CONFIG_PRINTK || CONFIG_LOG */ #if (CONFIG_FAULT_DUMP == 2) #define PR_FAULT_INFO(...) PR_EXC(__VA_ARGS__) #else #define PR_FAULT_INFO(...) #endif #if defined(CONFIG_ARM_MPU) && defined(CONFIG_CPU_HAS_NXP_MPU) #define EMN(edr) (((edr) & SYSMPU_EDR_EMN_MASK) >> SYSMPU_EDR_EMN_SHIFT) #define EACD(edr) (((edr) & SYSMPU_EDR_EACD_MASK) >> SYSMPU_EDR_EACD_SHIFT) #endif #if defined(CONFIG_ARM_SECURE_FIRMWARE) || \ defined(CONFIG_ARM_NONSECURE_FIRMWARE) /* Exception Return (EXC_RETURN) is provided in LR upon exception entry. * It is used to perform an exception return and to detect possible state * transition upon exception. */ /* Prefix. Indicates that this is an EXC_RETURN value. * This field reads as 0b11111111. */ #define EXC_RETURN_INDICATOR_PREFIX (0xFF << 24) /* bit[0]: Exception Secure. The security domain the exception was taken to. */ #define EXC_RETURN_EXCEPTION_SECURE_Pos 0 #define EXC_RETURN_EXCEPTION_SECURE_Msk \ BIT(EXC_RETURN_EXCEPTION_SECURE_Pos) #define EXC_RETURN_EXCEPTION_SECURE_Non_Secure 0 #define EXC_RETURN_EXCEPTION_SECURE_Secure EXC_RETURN_EXCEPTION_SECURE_Msk /* bit[2]: Stack Pointer selection. */ #define EXC_RETURN_SPSEL_Pos 2 #define EXC_RETURN_SPSEL_Msk BIT(EXC_RETURN_SPSEL_Pos) #define EXC_RETURN_SPSEL_MAIN 0 #define EXC_RETURN_SPSEL_PROCESS EXC_RETURN_SPSEL_Msk /* bit[3]: Mode. Indicates the Mode that was stacked from. */ #define EXC_RETURN_MODE_Pos 3 #define EXC_RETURN_MODE_Msk BIT(EXC_RETURN_MODE_Pos) #define EXC_RETURN_MODE_HANDLER 0 #define EXC_RETURN_MODE_THREAD EXC_RETURN_MODE_Msk /* bit[4]: Stack frame type. Indicates whether the stack frame is a standard * integer only stack frame or an extended floating-point stack frame. */ #define EXC_RETURN_STACK_FRAME_TYPE_Pos 4 #define EXC_RETURN_STACK_FRAME_TYPE_Msk BIT(EXC_RETURN_STACK_FRAME_TYPE_Pos) #define EXC_RETURN_STACK_FRAME_TYPE_EXTENDED 0 #define EXC_RETURN_STACK_FRAME_TYPE_STANDARD EXC_RETURN_STACK_FRAME_TYPE_Msk /* bit[5]: Default callee register stacking. Indicates whether the default * stacking rules apply, or whether the callee registers are already on the * stack. */ #define EXC_RETURN_CALLEE_STACK_Pos 5 #define EXC_RETURN_CALLEE_STACK_Msk BIT(EXC_RETURN_CALLEE_STACK_Pos) #define EXC_RETURN_CALLEE_STACK_SKIPPED 0 #define EXC_RETURN_CALLEE_STACK_DEFAULT EXC_RETURN_CALLEE_STACK_Msk /* bit[6]: Secure or Non-secure stack. Indicates whether a Secure or * Non-secure stack is used to restore stack frame on exception return. */ #define EXC_RETURN_RETURN_STACK_Pos 6 #define EXC_RETURN_RETURN_STACK_Msk BIT(EXC_RETURN_RETURN_STACK_Pos) #define EXC_RETURN_RETURN_STACK_Non_Secure 0 #define EXC_RETURN_RETURN_STACK_Secure EXC_RETURN_RETURN_STACK_Msk /* Integrity signature for an ARMv8-M implementation */ #if defined(CONFIG_ARMV7_M_ARMV8_M_FP) #define INTEGRITY_SIGNATURE_STD 0xFEFA125BUL #define INTEGRITY_SIGNATURE_EXT 0xFEFA125AUL #else #define INTEGRITY_SIGNATURE 0xFEFA125BUL #endif /* CONFIG_ARMV7_M_ARMV8_M_FP */ /* Size (in words) of the additional state context that is pushed * to the Secure stack during a Non-Secure exception entry. */ #define ADDITIONAL_STATE_CONTEXT_WORDS 10 #endif /* CONFIG_ARM_SECURE_FIRMWARE || CONFIG_ARM_NONSECURE_FIRMWARE */ /** * * Dump information regarding fault (FAULT_DUMP == 1) * * Dump information regarding the fault when CONFIG_FAULT_DUMP is set to 1 * (short form). * * eg. (precise bus error escalated to hard fault): * * Fault! EXC #3 * HARD FAULT: Escalation (see below)! * MMFSR: 0x00000000, BFSR: 0x00000082, UFSR: 0x00000000 * BFAR: 0xff001234 * * * * Dump information regarding fault (FAULT_DUMP == 2) * * Dump information regarding the fault when CONFIG_FAULT_DUMP is set to 2 * (long form), and return the error code for the kernel to identify the fatal * error reason. * * eg. (precise bus error escalated to hard fault): * * ***** HARD FAULT ***** * Fault escalation (see below) * ***** BUS FAULT ***** * Precise data bus error * Address: 0xff001234 * */ #if (CONFIG_FAULT_DUMP == 1) static void FaultShow(const z_arch_esf_t *esf, int fault) { PR_EXC("Fault! EXC #%d", fault); #if defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE) PR_EXC("MMFSR: 0x%x, BFSR: 0x%x, UFSR: 0x%x", SCB_MMFSR, SCB_BFSR, SCB_UFSR); #if defined(CONFIG_ARM_SECURE_FIRMWARE) PR_EXC("SFSR: 0x%x", SAU->SFSR); #endif /* CONFIG_ARM_SECURE_FIRMWARE */ #endif /* CONFIG_ARMV7_M_ARMV8_M_MAINLINE */ } #else /* For Dump level 2, detailed information is generated by the * fault handling functions for individual fault conditions, so this * function is left empty. * * For Dump level 0, no information needs to be generated. */ static void FaultShow(const z_arch_esf_t *esf, int fault) { (void)esf; (void)fault; } #endif /* FAULT_DUMP == 1 */ #ifdef CONFIG_USERSPACE Z_EXC_DECLARE(z_arch_user_string_nlen); static const struct z_exc_handle exceptions[] = { Z_EXC_HANDLE(z_arch_user_string_nlen) }; #endif /* Perform an assessment whether an MPU fault shall be * treated as recoverable. * * @return true if error is recoverable, otherwise return false. */ static bool memory_fault_recoverable(z_arch_esf_t *esf) { #ifdef CONFIG_USERSPACE for (int i = 0; i < ARRAY_SIZE(exceptions); i++) { /* Mask out instruction mode */ u32_t start = (u32_t)exceptions[i].start & ~0x1; u32_t end = (u32_t)exceptions[i].end & ~0x1; if (esf->basic.pc >= start && esf->basic.pc < end) { esf->basic.pc = (u32_t)(exceptions[i].fixup); return true; } } #endif return false; } #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) /* HardFault is used for all fault conditions on ARMv6-M. */ #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE) #if defined(CONFIG_MPU_STACK_GUARD) || defined(CONFIG_USERSPACE) u32_t z_check_thread_stack_fail(const u32_t fault_addr, const u32_t psp); #endif /* CONFIG_MPU_STACK_GUARD || defined(CONFIG_USERSPACE) */ /** * * @brief Dump MPU fault information * * See _FaultDump() for example. * * @return error code to identify the fatal error reason */ static u32_t MpuFault(z_arch_esf_t *esf, int fromHardFault, bool *recoverable) { u32_t reason = K_ERR_CPU_EXCEPTION; u32_t mmfar = -EINVAL; PR_FAULT_INFO("***** MPU FAULT *****"); if ((SCB->CFSR & SCB_CFSR_MSTKERR_Msk) != 0) { PR_FAULT_INFO(" Stacking error (context area might be" " not valid)"); } if ((SCB->CFSR & SCB_CFSR_MUNSTKERR_Msk) != 0) { PR_FAULT_INFO(" Unstacking error"); } if ((SCB->CFSR & SCB_CFSR_DACCVIOL_Msk) != 0) { PR_FAULT_INFO(" Data Access Violation"); /* In a fault handler, to determine the true faulting address: * 1. Read and save the MMFAR value. * 2. Read the MMARVALID bit in the MMFSR. * The MMFAR address is valid only if this bit is 1. * * Software must follow this sequence because another higher * priority exception might change the MMFAR value. */ mmfar = SCB->MMFAR; if ((SCB->CFSR & SCB_CFSR_MMARVALID_Msk) != 0) { PR_EXC(" MMFAR Address: 0x%x", mmfar); if (fromHardFault) { /* clear SCB_MMAR[VALID] to reset */ SCB->CFSR &= ~SCB_CFSR_MMARVALID_Msk; } } } if ((SCB->CFSR & SCB_CFSR_IACCVIOL_Msk) != 0) { PR_FAULT_INFO(" Instruction Access Violation"); } #if defined(CONFIG_ARMV7_M_ARMV8_M_FP) if ((SCB->CFSR & SCB_CFSR_MLSPERR_Msk) != 0) { PR_FAULT_INFO( " Floating-point lazy state preservation error"); } #endif /* !defined(CONFIG_ARMV7_M_ARMV8_M_FP) */ /* When stack protection is enabled, we need to assess * if the memory violation error is a stack corruption. * * By design, being a Stacking MemManage fault is a necessary * and sufficient condition for a thread stack corruption. */ if (SCB->CFSR & SCB_CFSR_MSTKERR_Msk) { #if defined(CONFIG_MPU_STACK_GUARD) || defined(CONFIG_USERSPACE) /* MemManage Faults are always banked between security * states. Therefore, we can safely assume the fault * originated from the same security state. * * As we only assess thread stack corruption, we only * process the error further if the stack frame is on * PSP. For always-banked MemManage Fault, this is * equivalent to inspecting the RETTOBASE flag. */ if (SCB->ICSR & SCB_ICSR_RETTOBASE_Msk) { u32_t min_stack_ptr = z_check_thread_stack_fail(mmfar, ((u32_t) &esf[0])); if (min_stack_ptr) { /* When MemManage Stacking Error has occurred, * the stack context frame might be corrupted * but the stack pointer may have actually * descent below the allowed (thread) stack * area. We may face a problem with un-stacking * the frame, upon the exception return, if we * do not have sufficient access permissions to * read the corrupted stack frame. Therefore, * we manually force the stack pointer to the * lowest allowed position, inside the thread's * stack. * * Note: * The PSP will normally be adjusted in a tail- * chained exception performing context switch, * after aborting the corrupted thread. The * adjustment, here, is required as tail-chain * cannot always be guaranteed. * * The manual adjustment of PSP is safe, as we * will not be re-scheduling this thread again * for execution; thread stack corruption is a * fatal error and a thread that corrupted its * stack needs to be aborted. */ __set_PSP(min_stack_ptr); reason = K_ERR_STACK_CHK_FAIL; } else { __ASSERT(0, "Stacking error not a stack fail\n"); } } #else (void)mmfar; __ASSERT(0, "Stacking error without stack guard / User-mode support\n"); #endif /* CONFIG_MPU_STACK_GUARD || CONFIG_USERSPACE */ } /* clear MMFSR sticky bits */ SCB->CFSR |= SCB_CFSR_MEMFAULTSR_Msk; /* Assess whether system shall ignore/recover from this MPU fault. */ *recoverable = memory_fault_recoverable(esf); return reason; } /** * * @brief Dump bus fault information * * See _FaultDump() for example. * * @return N/A */ static int BusFault(z_arch_esf_t *esf, int fromHardFault, bool *recoverable) { u32_t reason = K_ERR_CPU_EXCEPTION; PR_FAULT_INFO("***** BUS FAULT *****"); if (SCB->CFSR & SCB_CFSR_STKERR_Msk) { PR_FAULT_INFO(" Stacking error"); } if (SCB->CFSR & SCB_CFSR_UNSTKERR_Msk) { PR_FAULT_INFO(" Unstacking error"); } if (SCB->CFSR & SCB_CFSR_PRECISERR_Msk) { PR_FAULT_INFO(" Precise data bus error"); /* In a fault handler, to determine the true faulting address: * 1. Read and save the BFAR value. * 2. Read the BFARVALID bit in the BFSR. * The BFAR address is valid only if this bit is 1. * * Software must follow this sequence because another * higher priority exception might change the BFAR value. */ STORE_xFAR(bfar, SCB->BFAR); if ((SCB->CFSR & SCB_CFSR_BFARVALID_Msk) != 0) { PR_EXC(" BFAR Address: 0x%x", bfar); if (fromHardFault) { /* clear SCB_CFSR_BFAR[VALID] to reset */ SCB->CFSR &= ~SCB_CFSR_BFARVALID_Msk; } } } if (SCB->CFSR & SCB_CFSR_IMPRECISERR_Msk) { PR_FAULT_INFO(" Imprecise data bus error"); } if ((SCB->CFSR & SCB_CFSR_IBUSERR_Msk) != 0) { PR_FAULT_INFO(" Instruction bus error"); #if !defined(CONFIG_ARMV7_M_ARMV8_M_FP) } #else } else if (SCB->CFSR & SCB_CFSR_LSPERR_Msk) { PR_FAULT_INFO(" Floating-point lazy state preservation error"); } #endif /* !defined(CONFIG_ARMV7_M_ARMV8_M_FP) */ #if defined(CONFIG_ARM_MPU) && defined(CONFIG_CPU_HAS_NXP_MPU) u32_t sperr = SYSMPU->CESR & SYSMPU_CESR_SPERR_MASK; u32_t mask = BIT(31); int i; u32_t ear = -EINVAL; if (sperr) { for (i = 0; i < SYSMPU_EAR_COUNT; i++, mask >>= 1) { if ((sperr & mask) == 0U) { continue; } STORE_xFAR(edr, SYSMPU->SP[i].EDR); ear = SYSMPU->SP[i].EAR; PR_FAULT_INFO(" NXP MPU error, port %d", i); PR_FAULT_INFO(" Mode: %s, %s Address: 0x%x", edr & BIT(2) ? "Supervisor" : "User", edr & BIT(1) ? "Data" : "Instruction", ear); PR_FAULT_INFO( " Type: %s, Master: %d, Regions: 0x%x", edr & BIT(0) ? "Write" : "Read", EMN(edr), EACD(edr)); /* When stack protection is enabled, we need to assess * if the memory violation error is a stack corruption. * * By design, being a Stacking Bus fault is a necessary * and sufficient condition for a stack corruption. */ if (SCB->CFSR & SCB_CFSR_STKERR_Msk) { #if defined(CONFIG_MPU_STACK_GUARD) || defined(CONFIG_USERSPACE) /* Note: we can assume the fault originated * from the same security state for ARM * platforms implementing the NXP MPU * (CONFIG_CPU_HAS_NXP_MPU=y). * * As we only assess thread stack corruption, * we only process the error further, if the * stack frame is on PSP. For NXP MPU-related * Bus Faults (banked), this is equivalent to * inspecting the RETTOBASE flag. */ if (SCB->ICSR & SCB_ICSR_RETTOBASE_Msk) { u32_t min_stack_ptr = z_check_thread_stack_fail(ear, ((u32_t) &esf[0])); if (min_stack_ptr) { /* When BusFault Stacking Error * has occurred, the stack * context frame might be * corrupted but the stack * pointer may have actually * moved. We may face problems * with un-stacking the frame, * upon exception return, if we * do not have sufficient * permissions to read the * corrupted stack frame. * Therefore, we manually force * the stack pointer to the * lowest allowed position. * * Note: * The PSP will normally be * adjusted in a tail-chained * exception performing context * switch, after aborting the * corrupted thread. Here, the * adjustment is required as * tail-chain cannot always be * guaranteed. */ __set_PSP(min_stack_ptr); reason = K_ERR_STACK_CHK_FAIL; break; } } #else (void)ear; __ASSERT(0, "Stacking error without stack guard" "or User-mode support"); #endif /* CONFIG_MPU_STACK_GUARD || CONFIG_USERSPACE */ } } SYSMPU->CESR &= ~sperr; } #endif /* defined(CONFIG_ARM_MPU) && defined(CONFIG_CPU_HAS_NXP_MPU) */ /* clear BFSR sticky bits */ SCB->CFSR |= SCB_CFSR_BUSFAULTSR_Msk; *recoverable = memory_fault_recoverable(esf); return reason; } /** * * @brief Dump usage fault information * * See _FaultDump() for example. * * @return error code to identify the fatal error reason */ static u32_t UsageFault(const z_arch_esf_t *esf) { u32_t reason = K_ERR_CPU_EXCEPTION; PR_FAULT_INFO("***** USAGE FAULT *****"); /* bits are sticky: they stack and must be reset */ if ((SCB->CFSR & SCB_CFSR_DIVBYZERO_Msk) != 0) { PR_FAULT_INFO(" Division by zero"); } if ((SCB->CFSR & SCB_CFSR_UNALIGNED_Msk) != 0) { PR_FAULT_INFO(" Unaligned memory access"); } #if defined(CONFIG_ARMV8_M_MAINLINE) if ((SCB->CFSR & SCB_CFSR_STKOF_Msk) != 0) { PR_FAULT_INFO(" Stack overflow (context area not valid)"); #if defined(CONFIG_BUILTIN_STACK_GUARD) /* Stack Overflows are always reported as stack corruption * errors. Note that the built-in stack overflow mechanism * prevents the context area to be loaded on the stack upon * UsageFault exception entry. As a result, we cannot rely * on the reported faulty instruction address, to determine * the instruction that triggered the stack overflow. */ reason = K_ERR_STACK_CHK_FAIL; #endif /* CONFIG_BUILTIN_STACK_GUARD */ } #endif /* CONFIG_ARMV8_M_MAINLINE */ if ((SCB->CFSR & SCB_CFSR_NOCP_Msk) != 0) { PR_FAULT_INFO(" No coprocessor instructions"); } if ((SCB->CFSR & SCB_CFSR_INVPC_Msk) != 0) { PR_FAULT_INFO(" Illegal load of EXC_RETURN into PC"); } if ((SCB->CFSR & SCB_CFSR_INVSTATE_Msk) != 0) { PR_FAULT_INFO(" Illegal use of the EPSR"); } if ((SCB->CFSR & SCB_CFSR_UNDEFINSTR_Msk) != 0) { PR_FAULT_INFO(" Attempt to execute undefined instruction"); } /* clear UFSR sticky bits */ SCB->CFSR |= SCB_CFSR_USGFAULTSR_Msk; return reason; } #if defined(CONFIG_ARM_SECURE_FIRMWARE) /** * * @brief Dump secure fault information * * See _FaultDump() for example. * * @return N/A */ static void SecureFault(const z_arch_esf_t *esf) { PR_FAULT_INFO("***** SECURE FAULT *****"); STORE_xFAR(sfar, SAU->SFAR); if ((SAU->SFSR & SAU_SFSR_SFARVALID_Msk) != 0) { PR_EXC(" Address: 0x%x", sfar); } /* bits are sticky: they stack and must be reset */ if ((SAU->SFSR & SAU_SFSR_INVEP_Msk) != 0) { PR_FAULT_INFO(" Invalid entry point"); } else if ((SAU->SFSR & SAU_SFSR_INVIS_Msk) != 0) { PR_FAULT_INFO(" Invalid integrity signature"); } else if ((SAU->SFSR & SAU_SFSR_INVER_Msk) != 0) { PR_FAULT_INFO(" Invalid exception return"); } else if ((SAU->SFSR & SAU_SFSR_AUVIOL_Msk) != 0) { PR_FAULT_INFO(" Attribution unit violation"); } else if ((SAU->SFSR & SAU_SFSR_INVTRAN_Msk) != 0) { PR_FAULT_INFO(" Invalid transition"); } else if ((SAU->SFSR & SAU_SFSR_LSPERR_Msk) != 0) { PR_FAULT_INFO(" Lazy state preservation"); } else if ((SAU->SFSR & SAU_SFSR_LSERR_Msk) != 0) { PR_FAULT_INFO(" Lazy state error"); } /* clear SFSR sticky bits */ SAU->SFSR |= 0xFF; } #endif /* defined(CONFIG_ARM_SECURE_FIRMWARE) */ /** * * @brief Dump debug monitor exception information * * See _FaultDump() for example. * * @return N/A */ static void DebugMonitor(const z_arch_esf_t *esf) { ARG_UNUSED(esf); PR_FAULT_INFO( "***** Debug monitor exception (not implemented) *****"); } #else #error Unknown ARM architecture #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */ /** * * @brief Dump hard fault information * * See _FaultDump() for example. * * @return error code to identify the fatal error reason */ static u32_t HardFault(z_arch_esf_t *esf, bool *recoverable) { u32_t reason = K_ERR_CPU_EXCEPTION; PR_FAULT_INFO("***** HARD FAULT *****"); #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) /* Workaround for #18712: * HardFault may be due to escalation, as a result of * an SVC instruction that could not be executed; this * can occur if Z_ARCH_EXCEPT() is called by an ISR, * which executes at priority equal to the SVC handler * priority. We handle the case of Kernel OOPS and Stack * Fail here. */ u16_t *ret_addr = (u16_t *)esf->basic.pc; /* SVC is a 16-bit instruction. On a synchronous SVC * escalated to Hard Fault, the return address is the * next instruction, i.e. after the SVC. */ #define _SVC_OPCODE 0xDF00 u16_t fault_insn = *(ret_addr - 1); if (((fault_insn & 0xff00) == _SVC_OPCODE) && ((fault_insn & 0x00ff) == _SVC_CALL_RUNTIME_EXCEPT)) { PR_EXC("Z_ARCH_EXCEPT with reason %x\n", esf->basic.r0); reason = esf->basic.r0; } #undef _SVC_OPCODE *recoverable = memory_fault_recoverable(esf); #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE) *recoverable = false; if ((SCB->HFSR & SCB_HFSR_VECTTBL_Msk) != 0) { PR_EXC(" Bus fault on vector table read"); } else if ((SCB->HFSR & SCB_HFSR_FORCED_Msk) != 0) { PR_EXC(" Fault escalation (see below)"); if (SCB_MMFSR != 0) { reason = MpuFault(esf, 1, recoverable); } else if (SCB_BFSR != 0) { reason = BusFault(esf, 1, recoverable); } else if (SCB_UFSR != 0) { reason = UsageFault(esf); #if defined(CONFIG_ARM_SECURE_FIRMWARE) } else if (SAU->SFSR != 0) { SecureFault(esf); #endif /* CONFIG_ARM_SECURE_FIRMWARE */ } } #else #error Unknown ARM architecture #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */ return reason; } /** * * @brief Dump reserved exception information * * See _FaultDump() for example. * * @return N/A */ static void ReservedException(const z_arch_esf_t *esf, int fault) { ARG_UNUSED(esf); PR_FAULT_INFO("***** %s %d) *****", fault < 16 ? "Reserved Exception (" : "Spurious interrupt (IRQ ", fault - 16); } /* Handler function for ARM fault conditions. */ static u32_t FaultHandle(z_arch_esf_t *esf, int fault, bool *recoverable) { u32_t reason = K_ERR_CPU_EXCEPTION; *recoverable = false; switch (fault) { case 3: reason = HardFault(esf, recoverable); break; #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) /* HardFault is used for all fault conditions on ARMv6-M. */ #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE) case 4: reason = MpuFault(esf, 0, recoverable); break; case 5: reason = BusFault(esf, 0, recoverable); break; case 6: reason = UsageFault(esf); break; #if defined(CONFIG_ARM_SECURE_FIRMWARE) case 7: SecureFault(esf); break; #endif /* CONFIG_ARM_SECURE_FIRMWARE */ case 12: DebugMonitor(esf); break; #else #error Unknown ARM architecture #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */ default: ReservedException(esf, fault); break; } if ((*recoverable) == false) { /* Dump generic information about the fault. */ FaultShow(esf, fault); } return reason; } #if defined(CONFIG_ARM_SECURE_FIRMWARE) #if (CONFIG_FAULT_DUMP == 2) /** * @brief Dump the Secure Stack information for an exception that * has occurred in Non-Secure state. * * @param secure_esf Pointer to the secure stack frame. */ static void SecureStackDump(const z_arch_esf_t *secure_esf) { /* * In case a Non-Secure exception interrupted the Secure * execution, the Secure state has stacked the additional * state context and the top of the stack contains the * integrity signature. * * In case of a Non-Secure function call the top of the * stack contains the return address to Secure state. */ u32_t *top_of_sec_stack = (u32_t *)secure_esf; u32_t sec_ret_addr; #if defined(CONFIG_ARMV7_M_ARMV8_M_FP) if ((*top_of_sec_stack == INTEGRITY_SIGNATURE_STD) || (*top_of_sec_stack == INTEGRITY_SIGNATURE_EXT)) { #else if (*top_of_sec_stack == INTEGRITY_SIGNATURE) { #endif /* CONFIG_ARMV7_M_ARMV8_M_FP */ /* Secure state interrupted by a Non-Secure exception. * The return address after the additional state * context, stacked by the Secure code upon * Non-Secure exception entry. */ top_of_sec_stack += ADDITIONAL_STATE_CONTEXT_WORDS; secure_esf = (const z_arch_esf_t *)top_of_sec_stack; sec_ret_addr = secure_esf->basic.pc; } else { /* Exception during Non-Secure function call. * The return address is located on top of stack. */ sec_ret_addr = *top_of_sec_stack; } PR_FAULT_INFO(" S instruction address: 0x%x", sec_ret_addr); } #define SECURE_STACK_DUMP(esf) SecureStackDump(esf) #else /* We do not dump the Secure stack information for lower dump levels. */ #define SECURE_STACK_DUMP(esf) #endif /* CONFIG_FAULT_DUMP== 2 */ #endif /* CONFIG_ARM_SECURE_FIRMWARE */ /** * * @brief ARM Fault handler * * This routine is called when fatal error conditions are detected by hardware * and is responsible for: * - resetting the processor fault status registers (for the case when the * error handling policy allows the system to recover from the error), * - reporting the error information, * - determining the error reason to be provided as input to the user- * provided routine, k_sys_fatal_error_handler(). * The k_sys_fatal_error_handler() is invoked once the above operations are * completed, and is responsible for implementing the error handling policy. * * The provided ESF pointer points to the exception stack frame of the current * security state. Note that the current security state might not be the actual * state in which the processor was executing, when the exception occurred. * The actual state may need to be determined by inspecting the EXC_RETURN * value, which is provided as argument to the Fault handler. * * @param esf Pointer to the exception stack frame of the current security * state. The stack frame may be either on the Main stack (MSP) or Process * stack (PSP) depending at what execution state the exception was taken. * * @param exc_return EXC_RETURN value present in LR after exception entry. * * Note: exc_return argument shall only be used by the Fault handler if we are * running a Secure Firmware. */ void _Fault(z_arch_esf_t *esf, u32_t exc_return) { u32_t reason = K_ERR_CPU_EXCEPTION; int fault = SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk; bool recoverable; #if defined(CONFIG_ARM_SECURE_FIRMWARE) if ((exc_return & EXC_RETURN_INDICATOR_PREFIX) != EXC_RETURN_INDICATOR_PREFIX) { /* Invalid EXC_RETURN value */ goto _exit_fatal; } if ((exc_return & EXC_RETURN_EXCEPTION_SECURE_Secure) == 0U) { /* Secure Firmware shall only handle Secure Exceptions. * This is a fatal error. */ goto _exit_fatal; } if (exc_return & EXC_RETURN_RETURN_STACK_Secure) { /* Exception entry occurred in Secure stack. */ } else { /* Exception entry occurred in Non-Secure stack. Therefore, 'esf' * holds the Secure stack information, however, the actual * exception stack frame is located in the Non-Secure stack. */ /* Dump the Secure stack before handling the actual fault. */ SECURE_STACK_DUMP(esf); /* Handle the actual fault. * Extract the correct stack frame from the Non-Secure state * and supply it to the fault handing function. */ if (exc_return & EXC_RETURN_MODE_THREAD) { esf = (z_arch_esf_t *)__TZ_get_PSP_NS(); if ((SCB->ICSR & SCB_ICSR_RETTOBASE_Msk) == 0) { PR_EXC("RETTOBASE does not match EXC_RETURN"); goto _exit_fatal; } } else { esf = (z_arch_esf_t *)__TZ_get_MSP_NS(); if ((SCB->ICSR & SCB_ICSR_RETTOBASE_Msk) != 0) { PR_EXC("RETTOBASE does not match EXC_RETURN"); goto _exit_fatal; } } } #elif defined(CONFIG_ARM_NONSECURE_FIRMWARE) if ((exc_return & EXC_RETURN_INDICATOR_PREFIX) != EXC_RETURN_INDICATOR_PREFIX) { /* Invalid EXC_RETURN value */ goto _exit_fatal; } if (exc_return & EXC_RETURN_EXCEPTION_SECURE_Secure) { /* Non-Secure Firmware shall only handle Non-Secure Exceptions. * This is a fatal error. */ goto _exit_fatal; } if (exc_return & EXC_RETURN_RETURN_STACK_Secure) { /* Exception entry occurred in Secure stack. * * Note that Non-Secure firmware cannot inspect the Secure * stack to determine the root cause of the fault. Fault * inspection will indicate the Non-Secure instruction * that performed the branch to the Secure domain. */ PR_FAULT_INFO("Exception occurred in Secure State"); } #else (void) exc_return; #endif /* CONFIG_ARM_SECURE_FIRMWARE */ reason = FaultHandle(esf, fault, &recoverable); if (recoverable) { return; } #if defined(CONFIG_ARM_SECURE_FIRMWARE) || \ defined(CONFIG_ARM_NONSECURE_FIRMWARE) _exit_fatal: #endif z_arm_fatal_error(reason, esf); } /** * * @brief Initialization of fault handling * * Turns on the desired hardware faults. * * @return N/A */ void z_FaultInit(void) { #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE) SCB->CCR |= SCB_CCR_DIV_0_TRP_Msk; #else #error Unknown ARM architecture #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */ #if defined(CONFIG_BUILTIN_STACK_GUARD) /* If Stack guarding via SP limit checking is enabled, disable * SP limit checking inside HardFault and NMI. This is done * in order to allow for the desired fault logging to execute * properly in all cases. * * Note that this could allow a Secure Firmware Main Stack * to descend into non-secure region during HardFault and * NMI exception entry. To prevent from this, non-secure * memory regions must be located higher than secure memory * regions. * * For Non-Secure Firmware this could allow the Non-Secure Main * Stack to attempt to descend into secure region, in which case a * Secure Hard Fault will occur and we can track the fault from there. */ SCB->CCR |= SCB_CCR_STKOFHFNMIGN_Msk; #endif /* CONFIG_BUILTIN_STACK_GUARD */ } |