[PATCH] Added Getentropy() support to beagle BSP

Udit agarwal dev.madaari at gmail.com
Thu Mar 15 07:13:57 UTC 2018


On Thu, Mar 15, 2018 at 11:29 AM, Sebastian Huber <
sebastian.huber at embedded-brains.de> wrote:

>
>
> On 15/03/18 05:56, Udit agarwal wrote:
>
>>  c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c | 6 ++++++
>>  1 file changed, 6 insertions(+)
>>
>> diff --git a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> index 11e24dc..b26b6a8 100644
>> --- a/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> +++ b/c/src/lib/libbsp/arm/atsam/startup/getentropy-trng.c
>> @@ -17,6 +17,8 @@
>>  #include <string.h>
>>  #include <rtems/sysinit.h>
>>
>> +static rtems_mutex atsam_trng_reg = RTEMS_MUTEX_INITIALIZER("atsam
>> _trng_reg");
>>
>
> Is this a register or a mutex?
>

> +
>>  static void atsam_trng_enable(void)
>>  {
>>      PMC_EnablePeripheral(ID_TRNG);
>> @@ -29,9 +31,12 @@ int getentropy(void *ptr, size_t n)
>>          uint32_t random;
>>          size_t copy;
>>
>> +        rtems_mutex_lock(&atsam_trng_reg);
>>          while ((TRNG_GetStatus() & TRNG_ISR_DATRDY) == 0) {
>>              /* wait */
>>          }
>> +
>> +        rtems_mutex_unlock(&atsam_trng_reg);
>>
>
> Now the status register read is protected by a mutex.
>
>     random = TRNG_GetRandData();
>>
>
> Can you ensure that this read is correct in a multi-threaded application
> (the status register has the right value during the read)?
>
> Oh yes, i think i misinterpreted your previous suggestion of surrounding
the while loop by mutex, it should be the outer while loop. some thing like
this?:
while (n > 0) {
        uint32_t random;
        size_t copy;

        rtems_mutex_lock(&atsam_trng_mutex);

        while ((TRNG_GetStatus() & TRNG_ISR_DATRDY) == 0) {
            /* wait */
        }

        random = TRNG_GetRandData();

        /*
         * Read TRNG status one more time to avoid race condition.
         * Otherwise we can read (and clear) an old ready status but get
         * a new value. The ready status for this value wouldn't be
         * reset.
         */
        TRNG_GetStatus();

        copy = sizeof(random);
        if (n < copy ) {
            copy = n;
        }
        memcpy(ptr, &random, copy);
        n -= copy;
        ptr += copy;

        rtems_mutex_unlock(&atsam_trng_mutex);
    }


>
>>          /*
>> @@ -51,6 +56,7 @@ int getentropy(void *ptr, size_t n)
>>          ptr += copy;
>>      }
>>
>> +    rtems_mutex_destroy(&atsam_trng_reg);
>>
>
> Here  you destroy the mutex. What happens if you call getentropy() a
> second time?


> return 0;
>>  }
>>
>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> Phone   : +49 89 189 47 41-16
> Fax     : +49 89 189 47 41-09
> E-Mail  : sebastian.huber at embedded-brains.de
> PGP     : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20180315/cf489251/attachment-0002.html>


More information about the devel mailing list