From c594ce02427bad187be77b487170948c3ea9571e Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Tue, 16 Nov 2021 09:31:22 +0000 Subject: [PATCH] Various enhancements. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15086 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/oslib/include/chlib.h | 2 ++ os/oslib/include/chmemchecks.h | 30 +++++++++++++++++++++++++++--- os/oslib/src/chmemchecks.c | 23 +++++++++-------------- readme.txt | 1 + 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/os/oslib/include/chlib.h b/os/oslib/include/chlib.h index 291653b39..52a31fbb4 100644 --- a/os/oslib/include/chlib.h +++ b/os/oslib/include/chlib.h @@ -207,8 +207,10 @@ /* Restricted subsystems.*/ #undef CH_CFG_USE_MAILBOXES +#undef CH_CFG_USE_MEMCHECKS #define CH_CFG_USE_MAILBOXES FALSE +#define CH_CFG_USE_MEMCHECKS FALSE #endif /* CH_CUSTOMER_LIC_OSLIB == FALSE */ diff --git a/os/oslib/include/chmemchecks.h b/os/oslib/include/chmemchecks.h index 549423882..6858f3680 100644 --- a/os/oslib/include/chmemchecks.h +++ b/os/oslib/include/chmemchecks.h @@ -72,7 +72,7 @@ typedef struct { extern "C" { #endif #if CH_CFG_USE_MEMCHECKS == TRUE - bool chMemIsAreaContainedX(const memory_region_t *mrp, + bool chMemIsAreaContainedX(const memory_region_t regions[], const void *base, size_t size); bool chMemIsAreaWritableX(const void *p, @@ -90,14 +90,38 @@ extern "C" { /* Module inline functions. */ /*===========================================================================*/ +/** + * @brief Memory area check. + * @details Checks if specified area belongs to the specified region. + * + * @param[in] mrp pointer to an array of valid regions terminated with + * a zero element + * @param[in] base pointer to the area to be checked + * @param[in] size size of the area to be checked + * @return The test result. + * @retval false if the area is entirely contained within one of the + * specified regions. + * @retval true if the area check failed. + * + * @xclass + */ +static inline bool chMemIsAreaWithinX(const memory_region_t *mrp, + const void *base, + size_t size) { + uint8_t *start = (uint8_t *)base; + + return (bool)((start >= mrp->base) && (start <= mrp->end) && + (size <= (size_t)(mrp->end - start + 1U))); +} + #if CH_CFG_USE_MEMCHECKS == FALSE /* Stub implementations for when the functionality is disabled, areas are always reported as valid.*/ -static inline bool chMemIsAreaContainedX(const memory_region_t *mrp, +static inline bool chMemIsAreaContainedX(const memory_region_t regions[], const void *base, size_t size) { - (void)mrp; + (void)regions; (void)base; (void)size; diff --git a/os/oslib/src/chmemchecks.c b/os/oslib/src/chmemchecks.c index 157d28668..a42bfe90a 100644 --- a/os/oslib/src/chmemchecks.c +++ b/os/oslib/src/chmemchecks.c @@ -74,11 +74,9 @@ CC_WEAK memory_region_t __ch_mem_readable_regions[] = { /** * @brief Memory area check. - * @details Checks if specified area belongs to one of the system-defined - * writable regions. + * @details Checks if specified area belongs to one of the specified regions. * - * @param[in] mrp pointer to an array of valid regions terminated with - * a zero element + * @param[in] mrp array of valid regions terminated with a zero element * @param[in] base pointer to the area to be checked * @param[in] size size of the area to be checked * @return The test result. @@ -88,23 +86,20 @@ CC_WEAK memory_region_t __ch_mem_readable_regions[] = { * * @xclass */ -bool chMemIsAreaContainedX(const memory_region_t *mrp, +bool chMemIsAreaContainedX(const memory_region_t regions[], const void *base, size_t size) { - uint8_t *start; + const memory_region_t *mrp = ®ions[0]; chDbgCheck(base != NULL); - start = (uint8_t *)base; - /* Scanning the array of the valid regions for a mismatch.*/ - do { - if ((start >= mrp->base) && (start <= mrp->end) && - (size <= (size_t)(mrp->base - start))) { + while (mrp->base != mrp->end) { + if (chMemIsAreaWithinX(mrp, base, size)) { return true; } mrp++; - } while (mrp->base != mrp->end); + } return false; } @@ -112,7 +107,7 @@ bool chMemIsAreaContainedX(const memory_region_t *mrp, /** * @brief Memory writable area check. * @details Checks if specified pointer belongs to one of the system-defined - * writable regions. + * writable regions and is aligned as specified. * @note This function is only effective if @p CH_CFG_SYS_WRITABLE_REGIONS * is defined, if it is not defined then just the alignment of * the pointer is checked. @@ -145,7 +140,7 @@ bool chMemIsAreaWritableX(const void *p, /** * @brief Memory readable area check. * @details Checks if specified pointer belongs to one of the system-defined - * readable regions. + * readable regions and is aligned as specified. * @note This function is only effective if @p CH_CFG_SYS_READABLE_REGIONS * is defined, if it is not defined then just the alignment of * the pointer is checked. diff --git a/readme.txt b/readme.txt index 575b05844..c9890c503 100644 --- a/readme.txt +++ b/readme.txt @@ -74,6 +74,7 @@ ***************************************************************************** *** Next *** +- NEW: RT and NIL upgraded to support the enhanced OSLIB. - NEW: Memory areas/pointers checker functions added to OSLIB. - NEW: STM32G0B1 USBv2 driver. - NEW: USBv1 driver optimization and removal of a potential race condition