|
| 1 | + .cfi_sections .debug_frame |
| 2 | + |
| 3 | + # Notes for function attributes: |
| 4 | + # .type and .thumb_func are _both_ required, otherwise the Thumb mode bit |
| 5 | + # will not be set and an invalid vector table is generated. |
| 6 | + # LLD requires that section flags are set explicitly. |
| 7 | + |
| 8 | + .section .HardFaultTrampoline, "ax" |
| 9 | + .global HardFaultTrampoline |
| 10 | + .type HardFaultTrampoline,%function |
| 11 | + .thumb_func |
| 12 | + .cfi_startproc |
| 13 | + # HardFault exceptions are bounced through this trampoline which grabs the |
| 14 | + # stack pointer at the time of the exception and passes it to the user's |
| 15 | + # HardFault handler in r0. |
| 16 | +HardFaultTrampoline: |
| 17 | + # Depending on the stack mode in EXC_RETURN, fetch stack pointer from |
| 18 | + # PSP or MSP. |
| 19 | + mov r0, lr |
| 20 | + mov r1, #4 |
| 21 | + tst r0, r1 |
| 22 | + bne 0f |
| 23 | + mrs r0, MSP |
| 24 | + b HardFault |
| 25 | +0: |
| 26 | + mrs r0, PSP |
| 27 | + b HardFault |
| 28 | + .cfi_endproc |
| 29 | + .size HardFaultTrampoline, . - HardFaultTrampoline |
| 30 | + |
| 31 | + .section .Reset, "ax" |
| 32 | + .global Reset |
| 33 | + .type Reset,%function |
| 34 | + .thumb_func |
| 35 | + .cfi_startproc |
| 36 | + # Main entry point after reset. This jumps to the user __pre_init function, |
| 37 | + # which cannot be called from Rust code without invoking UB, then |
| 38 | + # initialises RAM. If the target has an FPU, it is enabled. Finally, jumps |
| 39 | + # to the user main function. |
| 40 | +Reset: |
| 41 | + # ARMv6-M does not initialise LR, but many tools expect it to be 0xFFFF_FFFF |
| 42 | + # when reaching the first call frame, so we set it at startup. |
| 43 | + # ARMv7-M and above initialise LR to 0xFFFF_FFFF at reset. |
| 44 | + ldr r4,=0xffffffff |
| 45 | + mov lr,r4 |
| 46 | + |
| 47 | + # Run user pre-init code, which must be executed immediately after startup, |
| 48 | + # before the potentially time-consuming memory initialisation takes place. |
| 49 | + # Example use cases include disabling default watchdogs or enabling RAM. |
| 50 | + bl __pre_init |
| 51 | + |
| 52 | + # Restore LR after calling __pre_init (r4 is preserved by subroutines). |
| 53 | + mov lr,r4 |
| 54 | + |
| 55 | + # Initialise .bss memory. `__sbss` and `__ebss` come from the linker script. |
| 56 | + ldr r0,=__sbss |
| 57 | + ldr r1,=__ebss |
| 58 | + mov r2,#0 |
| 59 | +0: |
| 60 | + cmp r1, r0 |
| 61 | + beq 1f |
| 62 | + stm r0!, {r2} |
| 63 | + b 0b |
| 64 | +1: |
| 65 | + |
| 66 | + # Initialise .data memory. `__sdata`, `__sidata`, and `__edata` come from the |
| 67 | + # linker script. Copy from r2 into r0 until r0 reaches r1. |
| 68 | + ldr r0,=__sdata |
| 69 | + ldr r1,=__edata |
| 70 | + ldr r2,=__sidata |
| 71 | +2: |
| 72 | + cmp r1, r0 |
| 73 | + beq 3f |
| 74 | + # load 1 word from r2 to r3, inc r2 |
| 75 | + ldm r2!, {r3} |
| 76 | + # store 1 word from r3 to r0, inc r0 |
| 77 | + stm r0!, {r3} |
| 78 | + b 2b |
| 79 | +3: |
| 80 | + |
| 81 | +#ifdef HAS_FPU |
| 82 | + # Conditionally enable the FPU. |
| 83 | + # Address of SCB.CPACR. |
| 84 | + ldr r0, =0xE000ED88 |
| 85 | + # Enable access to CP10 and CP11 from both privileged and unprivileged mode. |
| 86 | + ldr r1, =(0b1111 << 20) |
| 87 | + # RMW. |
| 88 | + ldr r2, [r0] |
| 89 | + orr r2, r2, r1 |
| 90 | + str r2, [r0] |
| 91 | + # Barrier is required on some processors. |
| 92 | + dsb |
| 93 | + isb |
| 94 | +#endif |
| 95 | + |
| 96 | +4: |
| 97 | + # Jump to user main function. We use bl for the extended range, but the |
| 98 | + # user main function may not return. |
| 99 | + bl main |
| 100 | + |
| 101 | + # Trap on return. |
| 102 | + udf |
| 103 | + |
| 104 | + .cfi_endproc |
| 105 | + .size Reset, . - Reset |
0 commit comments