Skip to content

Commit 682d10f

Browse files
committed
[nrf fromlist] debug: mipi_stp_decoder: Refactor to avoid unaligned 64 bit access
When optimized for speed it was possible that compiler for ARM Cortex-M used double word read for code that was reading two words from an array. However, it was possible that this array was not word-aligned and double word access was resulting in a bus fault. Refactored the function to ensure that only word access is used (word access on Cortex-M4 and later supports unaligned access). Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no> Upstream PR #: 108644
1 parent 3d219f3 commit 682d10f

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

subsys/debug/mipi_stp_decoder.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -463,26 +463,27 @@ static inline void get_nibbles64(const uint8_t *src, size_t src_noff, uint8_t *d
463463
bool dst_ba = (dst_noff & 0x1UL) == 0;
464464
uint32_t *src32 = (uint32_t *)&src[src_noff / 2];
465465
uint32_t *dst32 = (uint32_t *)&dst[dst_noff / 2];
466+
uint32_t s0 = src32[0];
467+
uint32_t s1 = src32[1];
466468

467469
if (nlen == 16) {
468470
/* dst must be aligned. */
471+
469472
if (src_ba) {
470-
dst32[0] = src32[0];
471-
dst32[1] = src32[1];
473+
dst32[0] = s0;
474+
dst32[1] = s1;
472475
} else {
473-
uint64_t src0 = src32[0] | ((uint64_t)src32[1] << 32);
474-
uint64_t src1 = src32[2] | ((uint64_t)src32[3] << 32);
475-
uint64_t part_a = src0 >> 4;
476-
uint64_t part_b = src1 << 60;
477-
uint64_t out = part_a | part_b;
478-
479-
dst32[0] = (uint32_t)out;
480-
dst32[1] = (uint32_t)(out >> 32);
476+
uint32_t s2 = src32[2];
477+
478+
dst32[0] = (s0 >> 4) | (s1 << 28);
479+
dst32[1] = (s1 >> 4) | (s2 << 28);
481480
}
482481
return;
483482
}
484483

485-
uint64_t src0 = src32[0] | ((uint64_t)src32[1] << 32);
484+
uint64_t src0a = (uint64_t)s0;
485+
uint64_t src0b = (uint64_t)s1 << 32;
486+
uint64_t src0 = src0a | src0b;
486487
uint64_t mask = BIT64_MASK(nlen * 4) << (src_ba ? 0 : 4);
487488
uint64_t src_d = src0 & mask;
488489

0 commit comments

Comments
 (0)