Skip to content

Commit c172b48

Browse files
popcornmixpelwell
authored andcommitted
vclog: Avoid bus errors with memcpy on mmapped buffers
It is not safe to call memcpy on unaligned mmapped buffer. It resulrs in bus error. Replace these calls with a fixed width version
1 parent 94b89a6 commit c172b48

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

vclog/vclog.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,31 @@ static void destroy_vc_mapping(void)
413413
}
414414
}
415415

416+
417+
/********************************************************************************/
418+
/* Note: gcc with -O2 or higher may replace most of this code with memcpy */
419+
/* which causes a bus error when given an insufficiently aligned mmap-ed buffer */
420+
/* Using volatile disables that optimisation */
421+
/********************************************************************************/
422+
static void memcpy_vc_memory(void *restrict dest, const volatile void *restrict src,
423+
size_t n)
424+
{
425+
if ((((uintptr_t)dest | (uintptr_t)src | n) & 3) == 0)
426+
{
427+
uint32_t *restrict d = (uint32_t *restrict )dest;
428+
const volatile uint32_t *restrict s = (const volatile uint32_t *restrict)src;
429+
while (n)
430+
*d++ = *s++, n -= 4;
431+
}
432+
else
433+
{
434+
uint8_t *restrict d = (uint8_t *restrict )dest;
435+
const volatile uint8_t *restrict s = (const volatile uint8_t *restrict)src;
436+
while (n--)
437+
*d++ = *s++;
438+
}
439+
}
440+
416441
static void read_vc_mem(uint32_t vc_addr, uint32_t size, void *dest)
417442
{
418443
vc_addr &= 0x3fffffff;
@@ -431,7 +456,7 @@ static void read_vc_mem(uint32_t vc_addr, uint32_t size, void *dest)
431456
}
432457
else
433458
{
434-
memcpy(dest, vc_map + vc_addr - vc_map_base, size);
459+
memcpy_vc_memory(dest, vc_map + vc_addr - vc_map_base, size);
435460
}
436461
}
437462

0 commit comments

Comments
 (0)