From 9e470ad73eaf68c33a69a1f694ad1440637d4d00 Mon Sep 17 00:00:00 2001 From: "Nakamura, Yuuichi" Date: Fri, 16 Oct 2020 22:49:46 +0900 Subject: [PATCH] Fix note_syscall_leave_s to avoid unaligned access --- include/nuttx/sched_note.h | 12 ++++++------ sched/sched/sched_note.c | 29 +++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/include/nuttx/sched_note.h b/include/nuttx/sched_note.h index 81d13f42e0..51011631b9 100644 --- a/include/nuttx/sched_note.h +++ b/include/nuttx/sched_note.h @@ -269,9 +269,9 @@ struct note_csection_s struct note_spinlock_s { - struct note_common_s nsp_cmn; /* Common note parameters */ - FAR void *nsp_spinlock; /* Address of spinlock */ - uint8_t nsp_value; /* Value of spinlock */ + struct note_common_s nsp_cmn; /* Common note parameters */ + uint8_t nsp_spinlock[sizeof(uintptr_t)]; /* Address of spinlock */ + uint8_t nsp_value; /* Value of spinlock */ }; #endif /* CONFIG_SCHED_INSTRUMENTATION_SPINLOCKS */ @@ -286,9 +286,9 @@ struct note_syscall_enter_s struct note_syscall_leave_s { - struct note_common_s nsc_cmn; /* Common note parameters */ - uintptr_t nsc_result; /* Result of the system call */ - uint8_t nsc_nr; /* System call number */ + struct note_common_s nsc_cmn; /* Common note parameters */ + uint8_t nsc_nr; /* System call number */ + uint8_t nsc_result[sizeof(uintptr_t)]; /* Result of the system call */ }; #endif /* CONFIG_SCHED_INSTRUMENTATION_SYSCALL */ diff --git a/sched/sched/sched_note.c b/sched/sched/sched_note.c index 95e32b1629..bf8d22fc8a 100644 --- a/sched/sched/sched_note.c +++ b/sched/sched/sched_note.c @@ -319,7 +319,20 @@ static void note_spincommon(FAR struct tcb_s *tcb, /* Format the note */ note_common(tcb, ¬e.nsp_cmn, sizeof(struct note_spinlock_s), type); - note.nsp_spinlock = (FAR void *)spinlock; + + note.nsp_spinlock[0] = (uint8_t)((uintptr_t)spinlock & 0xff); + note.nsp_spinlock[1] = (uint8_t)(((uintptr_t)spinlock >> 8) & 0xff); +#if UINTPTR_MAX > UINT16_MAX + note.nsp_spinlock[2] = (uint8_t)(((uintptr_t)spinlock >> 16) & 0xff); + note.nsp_spinlock[3] = (uint8_t)(((uintptr_t)spinlock >> 24) & 0xff); +#if UINTPTR_MAX > UINT32_MAX + note.nsp_spinlock[4] = (uint8_t)(((uintptr_t)spinlock >> 32) & 0xff); + note.nsp_spinlock[5] = (uint8_t)(((uintptr_t)spinlock >> 40) & 0xff); + note.nsp_spinlock[6] = (uint8_t)(((uintptr_t)spinlock >> 48) & 0xff); + note.nsp_spinlock[7] = (uint8_t)(((uintptr_t)spinlock >> 56) & 0xff); +#endif +#endif + note.nsp_value = (uint8_t)*spinlock; /* Add the note to circular buffer */ @@ -670,10 +683,22 @@ void sched_note_syscall_leave(int nr, uintptr_t result) note_common(tcb, ¬e.nsc_cmn, sizeof(struct note_syscall_leave_s), NOTE_SYSCALL_LEAVE); - note.nsc_result = result; DEBUGASSERT(nr <= UCHAR_MAX); note.nsc_nr = nr; + note.nsc_result[0] = (uint8_t)(result & 0xff); + note.nsc_result[1] = (uint8_t)((result >> 8) & 0xff); +#if UINTPTR_MAX > UINT16_MAX + note.nsc_result[2] = (uint8_t)((result >> 16) & 0xff); + note.nsc_result[3] = (uint8_t)((result >> 24) & 0xff); +#if UINTPTR_MAX > UINT32_MAX + note.nsc_result[4] = (uint8_t)((result >> 32) & 0xff); + note.nsc_result[5] = (uint8_t)((result >> 40) & 0xff); + note.nsc_result[6] = (uint8_t)((result >> 48) & 0xff); + note.nsc_result[7] = (uint8_t)((result >> 56) & 0xff); +#endif +#endif + /* Add the note to circular buffer */ sched_note_add(¬e, sizeof(struct note_syscall_leave_s));