forked from memfault/memfault-firmware-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memfault_fault_handling_arm.c
576 lines (490 loc) · 16.7 KB
/
memfault_fault_handling_arm.c
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
//! @file
//!
//! Copyright (c) Memfault, Inc.
//! See License.txt for details
//!
//! @brief
//! Fault handling for Cortex M based devices
#include "memfault/core/compiler.h"
#if MEMFAULT_COMPILER_ARM
#include "memfault/panics/fault_handling.h"
#include "memfault/core/platform/core.h"
#include "memfault/core/reboot_tracking.h"
#include "memfault/panics/arch/arm/cortex_m.h"
#include "memfault/panics/coredump.h"
#include "memfault/panics/coredump_impl.h"
static eMemfaultRebootReason s_crash_reason = kMfltRebootReason_Unknown;
typedef MEMFAULT_PACKED_STRUCT MfltCortexMRegs {
uint32_t r0;
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r4;
uint32_t r5;
uint32_t r6;
uint32_t r7;
uint32_t r8;
uint32_t r9;
uint32_t r10;
uint32_t r11;
uint32_t r12;
uint32_t sp;
uint32_t lr;
uint32_t pc;
uint32_t psr;
uint32_t msp;
uint32_t psp;
} sMfltCortexMRegs;
size_t memfault_coredump_storage_compute_size_required(void) {
// actual values don't matter since we are just computing the size
sMfltCortexMRegs core_regs = { 0 };
sMemfaultCoredumpSaveInfo save_info = {
.regs = &core_regs,
.regs_size = sizeof(core_regs),
.trace_reason = kMfltRebootReason_UnknownError,
};
sCoredumpCrashInfo info = {
// we'll just pass the current stack pointer, value shouldn't matter
.stack_address = (void *)&core_regs,
.trace_reason = save_info.trace_reason,
.exception_reg_state = NULL,
};
save_info.regions = memfault_platform_coredump_get_regions(&info, &save_info.num_regions);
return memfault_coredump_get_save_size(&save_info);
}
#if defined(__CC_ARM)
static uint32_t prv_read_psp_reg(void) {
register uint32_t reg_val __asm("psp");
return reg_val;
}
static uint32_t prv_read_msp_reg(void) {
register uint32_t reg_val __asm("msp");
return reg_val;
}
#elif defined(__TI_ARM__)
static uint32_t prv_read_psp_reg(void) {
return __get_PSP();
}
static uint32_t prv_read_msp_reg(void) {
return __get_MSP();
}
#elif defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
static uint32_t prv_read_psp_reg(void) {
uint32_t reg_val;
__asm volatile ("mrs %0, psp" : "=r" (reg_val));
return reg_val;
}
static uint32_t prv_read_msp_reg(void) {
uint32_t reg_val;
__asm volatile ("mrs %0, msp" : "=r" (reg_val));
return reg_val;
}
#else
# error "New compiler to add support for!"
#endif
#if !MEMFAULT_PLATFORM_FAULT_HANDLER_CUSTOM
MEMFAULT_WEAK
void memfault_platform_fault_handler(MEMFAULT_UNUSED const sMfltRegState *regs,
MEMFAULT_UNUSED eMemfaultRebootReason reason) {
}
#endif /* MEMFAULT_PLATFORM_FAULT_HANDLER_CUSTOM */
MEMFAULT_USED
void memfault_fault_handler(const sMfltRegState *regs, eMemfaultRebootReason reason) {
memfault_platform_fault_handler(regs, reason);
if (s_crash_reason == kMfltRebootReason_Unknown) {
sMfltRebootTrackingRegInfo info = {
.pc = regs->exception_frame->pc,
.lr = regs->exception_frame->lr,
};
memfault_reboot_tracking_mark_reset_imminent(reason, &info);
s_crash_reason = reason;
}
const bool fpu_stack_space_rsvd = ((regs->exc_return & (1 << 4)) == 0);
const bool stack_alignment_forced = ((regs->exception_frame->xpsr & (1 << 9)) != 0);
uint32_t sp_prior_to_exception =
(uint32_t)regs->exception_frame + (fpu_stack_space_rsvd ? 0x68 : 0x20);
if (stack_alignment_forced) {
sp_prior_to_exception += 0x4;
}
// Read the "SPSEL" bit where
// 0 = Main Stack Pointer in use prior to exception
// 1 = Process Stack Pointer in use prior to exception
const bool msp_was_active = (regs->exc_return & (1 << 2)) == 0;
sMfltCortexMRegs core_regs = {
.r0 = regs->exception_frame->r0,
.r1 = regs->exception_frame->r1,
.r2 = regs->exception_frame->r2,
.r3 = regs->exception_frame->r3,
.r4 = regs->r4,
.r5 = regs->r5,
.r6 = regs->r6,
.r7 = regs->r7,
.r8 = regs->r8,
.r9 = regs->r9,
.r10 = regs->r10,
.r11 = regs->r11,
.r12 = regs->exception_frame->r12,
.sp = sp_prior_to_exception,
.lr = regs->exception_frame->lr,
.pc = regs->exception_frame->pc,
.psr = regs->exception_frame->xpsr,
.msp = msp_was_active ? sp_prior_to_exception : prv_read_msp_reg(),
.psp = !msp_was_active ? sp_prior_to_exception : prv_read_psp_reg(),
};
sMemfaultCoredumpSaveInfo save_info = {
.regs = &core_regs,
.regs_size = sizeof(core_regs),
.trace_reason = s_crash_reason,
};
sCoredumpCrashInfo info = {
.stack_address = (void *)sp_prior_to_exception,
.trace_reason = save_info.trace_reason,
.exception_reg_state = regs,
};
save_info.regions = memfault_platform_coredump_get_regions(&info, &save_info.num_regions);
const bool coredump_saved = memfault_coredump_save(&save_info);
if (coredump_saved) {
memfault_reboot_tracking_mark_coredump_saved();
}
memfault_platform_reboot();
MEMFAULT_UNREACHABLE;
}
// The fault handling shims below figure out what stack was being used leading up to the exception,
// build the sMfltRegState argument and pass that as well as the reboot reason to memfault_fault_handler
#if defined(__CC_ARM)
// armcc emits a define for the CPU target.
//
// Use that information to decide whether or not to pick up the ARMV6M port by default
//
// Cortex M0 (--cpu=cortex-m0)
// __TARGET_CPU_CORTEX_M0
// Cortex M0+ (--cpu=cortex-m0plus or --cpu=cortex-m0+)
// __TARGET_CPU_CORTEX_M0PLUS
// __TARGET_CPU_CORTEX_M0_
#if defined(__TARGET_CPU_CORTEX_M0) || defined (__TARGET_CPU_CORTEX_M0_) || defined(__TARGET_CPU_CORTEX_M0PLUS)
#define MEMFAULT_USE_ARMV6M_FAULT_HANDLER 1
#endif
#if !defined(MEMFAULT_USE_ARMV6M_FAULT_HANDLER)
__asm __forceinline void memfault_fault_handling_shim(int reason) {
extern memfault_fault_handler;
tst lr, #4
ite eq
mrseq r3, msp
mrsne r3, psp
push {r3-r11, lr}
mov r1, r0
mov r0, sp
b memfault_fault_handler
ALIGN
}
#else
__asm __forceinline void memfault_fault_handling_shim(int reason) {
extern memfault_fault_handler;
PRESERVE8
mov r1, lr
movs r2, #4
tst r1,r2
mrs r12, msp
beq msp_active_at_crash
mrs r12, psp
msp_active_at_crash mov r3, r11
mov r2, r10
mov r1, r9
mov r9, r0
mov r0, r8
push {r0-r3, lr}
mov r3, r12
push {r3-r7}
mov r0, sp
mov r1, r9
ldr r2, =memfault_fault_handler
bx r2
ALIGN
}
#endif
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_HARD_FAULT(void) {
ldr r0, =0x9400 // kMfltRebootReason_HardFault
ldr r1, =memfault_fault_handling_shim
bx r1
ALIGN
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_MEMORY_MANAGEMENT(void) {
ldr r0, =0x9200 // kMfltRebootReason_MemFault
ldr r1, =memfault_fault_handling_shim
bx r1
ALIGN
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_BUS_FAULT(void) {
ldr r0, =0x9100 // kMfltRebootReason_BusFault
ldr r1, =memfault_fault_handling_shim
bx r1
ALIGN
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_USAGE_FAULT(void) {
ldr r0, =0x9300 // kMfltRebootReason_UsageFault
ldr r1, =memfault_fault_handling_shim
bx r1
ALIGN
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_NMI(void) {
ldr r0, =0x8004 // kMfltRebootReason_Nmi
ldr r1, =memfault_fault_handling_shim
bx r1
ALIGN
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_WATCHDOG(void) {
ldr r0, =0x8006 // kMfltRebootReason_SoftwareWatchdog
ldr r1, =memfault_fault_handling_shim
bx r1
ALIGN
}
#elif defined(__TI_ARM__)
// Note: 'reason' is passed as arg0. However we mark the function
// as void so the TI compiler does not emit any function prologue
// pushing args on the stack
MEMFAULT_NAKED_FUNC
void memfault_fault_handling_shim(void /* int reason */) {
__asm(" tst lr, #4 \n"
" ite eq \n"
" mrseq r3, msp \n"
" mrsne r3, psp \n"
" push {r3-r11, lr} \n"
" mov r1, r0 \n"
" mov r0, sp \n"
" b memfault_fault_handler");
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_HARD_FAULT(void) {
__asm(" mov r0, #0x9400 \n" // kMfltRebootReason_HardFault
" b memfault_fault_handling_shim \n");
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_MEMORY_MANAGEMENT(void) {
__asm(" mov r0, #0x9200 \n" // kMfltRebootReason_MemFault
" b memfault_fault_handling_shim \n");
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_BUS_FAULT(void) {
__asm(" mov r0, #0x9100 \n" // kMfltRebootReason_BusFault
" b memfault_fault_handling_shim \n");
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_USAGE_FAULT(void) {
__asm(" mov r0, #0x9300 \n" // kMfltRebootReason_UsageFault
" b memfault_fault_handling_shim \n");
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_NMI(void) {
__asm(" mov r0, #0x8004 \n" // kMfltRebootReason_Nmi
" b memfault_fault_handling_shim \n");
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_WATCHDOG(void) {
__asm(" mov r0, #0x8006 \n" // kMfltRebootReason_SoftwareWatchdog
" b memfault_fault_handling_shim \n");
}
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__ARM_ARCH) && (__ARM_ARCH == 6)
#define MEMFAULT_USE_ARMV6M_FAULT_HANDLER 1
#endif
// Note: ARMV8-M has a subprofile referred to as the "Baseline" implementation
// with an instruction set similar to ARMV6-M. See https://mflt.io/armv8m-subprofiles
// for more details.
#if defined(__ARM_ARCH_8M_BASE__) && (__ARM_ARCH_8M_BASE__ == 1)
#define MEMFAULT_USE_ARMV8M_BASE_FAULT_HANDLER 1
#endif
#if (!defined(MEMFAULT_USE_ARMV6M_FAULT_HANDLER) && \
!defined(MEMFAULT_USE_ARMV8M_BASE_FAULT_HANDLER))
#define MEMFAULT_HARDFAULT_HANDLING_ASM(_x) \
__asm volatile( \
"tst lr, #4 \n" \
"ite eq \n" \
"mrseq r3, msp \n" \
"mrsne r3, psp \n" \
"push {r3-r11, lr} \n" \
"mov r0, sp \n" \
"ldr r1, =%c0 \n" \
"b memfault_fault_handler \n" \
: \
: "i" ((uint16_t)_x) \
)
#else
#define MEMFAULT_HARDFAULT_HANDLING_ASM(_x) \
__asm volatile( \
"mov r0, lr \n" \
"movs r1, #4 \n" \
"tst r0,r1 \n" \
"mrs r12, msp \n" \
"beq msp_active_at_crash_%= \n" \
"mrs r12, psp \n" \
"msp_active_at_crash_%=: \n" \
"mov r0, r8 \n" \
"mov r1, r9 \n" \
"mov r2, r10 \n" \
"mov r3, r11 \n" \
"push {r0-r3, lr} \n" \
"mov r3, r12 \n" \
"push {r3-r7} \n" \
"mov r0, sp \n" \
"ldr r1, =%c0 \n" \
"b memfault_fault_handler \n" \
: \
: "i" ((uint16_t)_x) \
)
#endif
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_HARD_FAULT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_HardFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_MEMORY_MANAGEMENT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_MemFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_BUS_FAULT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_BusFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_USAGE_FAULT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_UsageFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_NMI(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_Nmi);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_WATCHDOG(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_SoftwareWatchdog);
}
#elif defined(__ICCARM__)
#if __ARM_ARCH == 6
#define MEMFAULT_USE_ARMV6M_FAULT_HANDLER 1
#endif
#if !defined(MEMFAULT_USE_ARMV6M_FAULT_HANDLER)
#define MEMFAULT_HARDFAULT_HANDLING_ASM(_x) \
__asm volatile( \
"tst lr, #4 \n" \
"ite eq \n" \
"mrseq r3, msp \n" \
"mrsne r3, psp \n" \
"push {r3-r11, lr} \n" \
"mov r0, sp \n" \
"mov r1, %0 \n" \
"b memfault_fault_handler \n" \
: \
: "i" (_x) \
)
#else
// Note: Below IAR will build the enum value
// as part of the prologue to the asm statement and
// place the value in r0
#define MEMFAULT_HARDFAULT_HANDLING_ASM(_x) \
__asm volatile( \
"mov r1, lr \n" \
"movs r2, #4 \n" \
"tst r1,r2 \n" \
"mrs r12, msp \n" \
"beq msp_active_at_crash \n" \
"mrs r12, psp \n" \
"msp_active_at_crash: \n" \
"mov r3, r11 \n" \
"mov r2, r10 \n" \
"mov r1, r9 \n" \
"mov r9, r0 \n" \
"mov r0, r8 \n" \
"push {r0-r3, lr} \n" \
"mov r3, r12 \n" \
"push {r3-r7} \n" \
"mov r0, sp \n" \
"mov r1, r9 \n" \
"ldr r2, =memfault_fault_handler \n" \
"bx r2 \n" \
: \
: "r" (_x) \
)
#endif
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_HARD_FAULT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_HardFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_MEMORY_MANAGEMENT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_MemFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_BUS_FAULT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_BusFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_USAGE_FAULT(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_UsageFault);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_NMI(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_Nmi);
}
MEMFAULT_NAKED_FUNC
void MEMFAULT_EXC_HANDLER_WATCHDOG(void) {
MEMFAULT_HARDFAULT_HANDLING_ASM(kMfltRebootReason_SoftwareWatchdog);
}
#else
# error "New compiler to add support for!"
#endif
// The ARM architecture has a reserved instruction that is "Permanently Undefined" and always
// generates an Undefined Instruction exception causing an ARM fault handler to be invoked.
//
// We use this instruction to "trap" into the fault handler logic. We use 'M' (77) as the immediate
// value for easy disambiguation from any other udf invocations in a system.
#if defined(__CC_ARM)
__asm __forceinline void MEMFAULT_ASSERT_TRAP(void) {
PRESERVE8
UND #77
ALIGN
}
#elif defined(__TI_ARM__)
// The TI Compiler doesn't support the udf asm instruction
// so we encode the instruction & a nop as a word literal
#pragma diag_push
#pragma diag_suppress 1119
void MEMFAULT_ASSERT_TRAP(void) {
__asm(" .word 3204505165"); // 0xbf00de4d
}
#pragma diag_pop
#else
#define MEMFAULT_ASSERT_TRAP() __asm volatile ("udf #77")
#endif
static void prv_fault_handling_assert(void *pc, void *lr, eMemfaultRebootReason reason) {
sMfltRebootTrackingRegInfo info = {
.pc = (uint32_t)pc,
.lr = (uint32_t)lr,
};
s_crash_reason = reason;
memfault_reboot_tracking_mark_reset_imminent(s_crash_reason, &info);
#if MEMFAULT_ASSERT_HALT_IF_DEBUGGING_ENABLED
memfault_platform_halt_if_debugging();
#endif
MEMFAULT_ASSERT_TRAP();
// We just trap'd into the fault handler logic so it should never be possible to get here but if
// we do the best thing that can be done is rebooting the system to recover it.
memfault_platform_reboot();
}
// Note: These functions are annotated as "noreturn" which can be useful for static analysis.
// However, this can also lead to compiler optimizations that make recovering local variables
// difficult (such as ignoring ABI requirements to preserve callee-saved registers)
MEMFAULT_NO_OPT
void memfault_fault_handling_assert(void *pc, void *lr) {
prv_fault_handling_assert(pc, lr, kMfltRebootReason_Assert);
MEMFAULT_UNREACHABLE;
}
MEMFAULT_NO_OPT
void memfault_fault_handling_assert_extra(void *pc, void *lr, sMemfaultAssertInfo *extra_info) {
prv_fault_handling_assert(pc, lr, extra_info->assert_reason);
MEMFAULT_UNREACHABLE;
}
#endif /* MEMFAULT_COMPILER_ARM */