You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "eenurkka (via GitHub)" <gi...@apache.org> on 2023/09/20 12:28:21 UTC

[GitHub] [nuttx] eenurkka opened a new pull request, #10724: riscv/riscv_pmp.c: fix broken TOR checks

eenurkka opened a new pull request, #10724:
URL: https://github.com/apache/nuttx/pull/10724

   PMPCFG_A_TOR region may have zero size. The pmp configuration currently fails for zero-sized TOR. This patch bypasses such a restriction.
   
   Also replace log2ceil with LOG2_CEIL from lib/math32.h.
   
   ## Summary
   
   All risc-v TOR implementations are broken.
   
   ## Impact
   
   Risc-v TOR implentations
   
   ## Testing
   
   Polarfire soc


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`. Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   https://en.cppreference.com/w/cpp/numeric/bit_width
   https://en.cppreference.com/w/cpp/numeric/countl_zero
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`. Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   https://en.cppreference.com/w/cpp/numeric/bit_width
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`. Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`. Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   https://en.cppreference.com/w/cpp/numeric/bit_width
   https://en.cppreference.com/w/cpp/numeric/countl_zero
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```
   
   edit, if there is no counting leading zero (i.e CLZ instruction) available on one target here a fair soft fallback implementation (you may ignore my MU0_HAVE_C99 || MU0_HAVE_CPP11, there are just here to trigger errors in case of long long is not supported or is an language extension, e.g mainly CPP98-03 or prior C99 try to use):
   
   ```c
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u2__(const ___mu0_uint2_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u1__(const ___mu0_uint1_t___ __x);
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x)
   {
   	const ___mu0_sint4_t___ d = __mu0_bit_digits_u4__();
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	return ((__x & 0xFFFFFFFF00000000ULL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFULL)
   	);
   #	else
   	return ((__x & 0xFFFFFFFF00000000UL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFUL)
   	);
   #	endif
   }
   
   #	if MU0_HAVE_LP64
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u8__(__x); }
   #	else
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u4__(__x); }
   #	endif
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x)
   {
   	___mu0_uint1_t___ d = 0;
   	___mu0_uint4_t___ x = __x;
   	if (x) {
   		if (x & 0xFFFF0000U) { x = x >> 16U; } else { d += 16; }
   		return d + __mu0_cntlz_u2__ (x);
   	}
   	return __mu0_bit_digits_u4__();
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] acassis commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331669011


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   @eenurkka maybe this log2ceil could be moved to libdsp or other place to be reused by other arches that don't have it in binutils (older version of binutils).
   
   BTW did you see that: https://stackoverflow.com/questions/15043483/faster-log2ceil
   
   There is a faster implementation of log2ceil that just use logic shift and AND operation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   in 'mu0` you have a bit_floor and bit_ceil implementation macthing c++. `__mu0_bit_floor_u8__` is performed on `unsigned long long` size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   in 'mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long` size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 merged pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #10724:
URL: https://github.com/apache/nuttx/pull/10724


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long` size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`. Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   https://en.cppreference.com/w/cpp/numeric/bit_width
   https://en.cppreference.com/w/cpp/numeric/countl_zero
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```
   
   edit, if there is no counting leading zero (i.e CLZ instruction) available on one target here a fair soft fallback implementation (you may ignore my MU0_HAVE_C99 || MU0_HAVE_CPP11, there are just here to trigger errors in case of long long is not supported or is a language extension, e.g mainly CPP98-03 or prior C99 try to use):
   
   ```c
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u2__(const ___mu0_uint2_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u1__(const ___mu0_uint1_t___ __x);
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x)
   {
   	const ___mu0_sint4_t___ d = __mu0_bit_digits_u4__();
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	return ((__x & 0xFFFFFFFF00000000ULL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFULL)
   	);
   #	else
   	return ((__x & 0xFFFFFFFF00000000UL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFUL)
   	);
   #	endif
   }
   
   #	if MU0_HAVE_LP64
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u8__(__x); }
   #	else
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u4__(__x); }
   #	endif
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x)
   {
   	___mu0_uint1_t___ d = 0;
   	___mu0_uint4_t___ x = __x;
   	if (x) {
   		if (x & 0xFFFF0000U) { x = x >> 16U; } else { d += 16; }
   		return d + __mu0_cntlz_u2__ (x);
   	}
   	return __mu0_bit_digits_u4__();
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] acassis commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331669011


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   @eenurkka maybe this log2ceil could be moved to libdsp or other place to be reused for other arches that don't have it in binutils (older version of binutils).
   
   BTW did you see that: https://stackoverflow.com/questions/15043483/faster-log2ceil
   
   There is a faster implementation of log2ceil that just use logic shift and AND operation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331949482


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   is this enough?
   https://github.com/apache/nuttx/blob/master/include/nuttx/lib/math32.h#L41-L60



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   in 'mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long` size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331949482


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   @acassis is this enough?
   https://github.com/apache/nuttx/blob/master/include/nuttx/lib/math32.h#L41-L60
   Anyway, let's merge this patch and create a new pr later if need.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`. Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   https://en.cppreference.com/w/cpp/numeric/bit_width
   https://en.cppreference.com/w/cpp/numeric/countl_zero
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```
   
   edit, if there is no counting leading zero (i.e CLZ instruction) available on one target here a fair soft fallback implementation (you may ignore my MU0_HAVE_C99 || MU0_HAVE_CPP11, there are just here to trigger errors in case of long long is not supported or is a language extension, e.g mainly CPP98-03 or prior C99 try to use):
   
   ```c
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u2__(const ___mu0_uint2_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u1__(const ___mu0_uint1_t___ __x);
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x)
   {
   	const ___mu0_sint4_t___ d = __mu0_bit_digits_u4__();
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	return ((__x & 0xFFFFFFFF00000000ULL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFULL)
   	);
   #	else
   	return ((__x & 0xFFFFFFFF00000000UL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFUL)
   	);
   #	endif
   }
   
   #	if MU0_HAVE_LP64
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u8__(__x); }
   #	else
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u4__(__x); }
   #	endif
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x)
   {
   	___mu0_uint1_t___ d = 0;
   	___mu0_uint4_t___ x = __x;
   	if (x) {
   		if (x & 0xFFFF0000U) { x = x >> 16U; } else { d += 16; }
   		return d + __mu0_cntlz_u2__ (x);
   	}
   	return __mu0_bit_digits_u4__();
   }
   
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u2__(const ___mu0_uint2_t___ __x)
   {
   	___mu0_uint1_t___ d = 0;
   	___mu0_uint2_t___ x = __x;
   	if (x) {
   		if (x & 0xFF00U) { x >>= 8U; } else { d += 8; }
   		return d + __mu0_cntlz_u1__ (x);
   	}
   	return __mu0_bit_digits_u2__();
   }
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u1__(const ___mu0_uint1_t___ __x)
   {
   	___mu0_uint1_t___ d = 0;
   	___mu0_uint1_t___ x = __x;
   	if (x) {
   		if (((x & 0xF0U))) { x  = x >> 4U; } else { d += 4; }
   		if (((x & 0x0CU))) { x  = x >> 2U; } else { d += 2; }
   		if (!(x & 0x02U) ) { d += 1;       }
   		return d;
   	}
   	return __mu0_bit_digits_u1__();
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] mu578 commented on a diff in pull request #10724: riscv/riscv_pmp.c: fix broken TOR checks

Posted by "mu578 (via GitHub)" <gi...@apache.org>.
mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388


##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- *   Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- *   size - The size of the PMP region.
- *
- * Returned Value:
- *   Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
-  uintptr_t pot = 0;
-
-  for (size = size - 1; size > 1; size >>= 1)
-    {
-      pot++;
-    }
-
-  return pot;
-}
-

Review Comment:
   In `mu0` you have a bit_floor and bit_ceil implementation matching c++ definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`. Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
   
   https://github.com/mu578/mu0
   https://en.cppreference.com/w/cpp/numeric/bit_floor
   https://en.cppreference.com/w/cpp/numeric/bit_ceil
   https://en.cppreference.com/w/cpp/numeric/bit_width
   https://en.cppreference.com/w/cpp/numeric/countl_zero
   
   example:
   
   ```c
   __mu0_static_inline__
   const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
   {
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	const ___mu0_uint8_t___ one  = 1ULL;
   	const ___mu0_uint8_t___ zero = 0ULL;
   #	else
   	const ___mu0_uint8_t___ one  = 1UL;
   	const ___mu0_uint8_t___ zero = 0UL;
   #	endif
   	return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
   }
   ```
   
   edit, if there is no counting leading zero (i.e CLZ instruction) available on one target here a fair soft fallback implementation (you may ignore my MU0_HAVE_C99 || MU0_HAVE_CPP11, there are just here to trigger errors in case of long long is not supported or is an language extension, e.g mainly CPP98-03 or prior C99 try to use):
   
   ```
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u2__(const ___mu0_uint2_t___ __x);
   __mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u1__(const ___mu0_uint1_t___ __x);
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x)
   {
   	const ___mu0_sint4_t___ d = __mu0_bit_digits_u4__();
   #	if MU0_HAVE_C99 || MU0_HAVE_CPP11
   	return ((__x & 0xFFFFFFFF00000000ULL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFULL)
   	);
   #	else
   	return ((__x & 0xFFFFFFFF00000000UL)
   		?     __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___, (__x >> d)))
   		: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFUL)
   	);
   #	endif
   }
   
   #	if MU0_HAVE_LP64
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u8__(__x); }
   #	else
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
   { return __mu0_cntlz_u4__(__x); }
   #	endif
   
   __mu0_static_inline__
   const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x)
   {
   	___mu0_uint1_t___ d = 0;
   	___mu0_uint4_t___ x = __x;
   	if (x) {
   		if (x & 0xFFFF0000U) { x = x >> 16U; } else { d += 16; }
   		return d + __mu0_cntlz_u2__ (x);
   	}
   	return __mu0_bit_digits_u4__();
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org