Skip to contentSkip to conversation

AMD/Xilinx Versal TRNG support

PATCH Superseded Tong Ho 2 participants 10 messages Oct 17, 2023 – Oct 30, 2023 Arm/Amlogic Meson SoC support

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Tong Ho

author
#0 lore raw

v4 cover AMD/Xilinx Versal TRNG support

This series adds support for the True Random Number Generator (TRNG) in the AMD/Xilinx Versal family of devices.

The series starts by introducing a non-cryptographic grade model of the TRNG controller in the Versal family of devices, followed by instantiating the model in Xilinx Versal machine.

The series ends with a q-test for sanity check of the TRNG model in the Xilinx Versal machine.

V3 => V4
1) Patch #1
a) Simplify the data given to g_rand_set_seed_array() as an array of guint32.
b) Use qemu_guest_getrandom_nofail() as the entropy source when the device is configured in TRNG (mode 3).
c) Allow 0 and (2^32-1) as valid output of the generator
d) Add output-related context to VMSTATE.
2) Patch #3
Remove test's assumption of PRNG output != TRNG output.

V2 => V3
1) Patch #1 Use DEFINE_PROP to define a property with custom action on set
2) Patch #1 Remove duplicated version-check on writing to CTRL4 register
3) Patch #2 Defer adding TRNG to machine's device-tree to a future patch
4) Patch #3 Code style fix: group all local vars definitions together

V1 => V2
1) Change patch #1 only
2) Use g_rand_*() PRNG from glib to replace V1's custom PRNG.
3) Implement ResettableClass for device-reset.
4) Add device-mode description to commit-message.

Best regards,
Tong Ho

Tong Ho (3):
hw/misc: Introduce AMD/Xilix Versal TRNG device hw/arm: xlnx-versal-virt: Add AMD/Xilinx TRNG device tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device

 hw/arm/Kconfig                      |   1 +
 hw/arm/xlnx-versal.c                |  16 +
 hw/misc/Kconfig                     |   3 +
 hw/misc/meson.build                 |   3 +
 hw/misc/xlnx-versal-trng.c          | 727 ++++++++++++++++++++++++++++
 include/hw/arm/xlnx-versal.h        |   5 +
 include/hw/misc/xlnx-versal-trng.h  |  58 +++
 tests/qtest/meson.build             |   2 +-
 tests/qtest/xlnx-versal-trng-test.c | 486 +++++++++++++++++++
 9 files changed, 1300 insertions(+), 1 deletion(-)
 create mode 100644 hw/misc/xlnx-versal-trng.c
 create mode 100644 include/hw/misc/xlnx-versal-trng.h
 create mode 100644 tests/qtest/xlnx-versal-trng-test.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v4 1/3 hw/misc: Introduce AMD/Xilix Versal TRNG device

This adds a non-cryptographic grade implementation of the model for the True Random Number Generator (TRNG) component in AMD/Xilinx Versal device family.

This implements all 3 modes defined by the actual hardware specs, all of which selectable by guest software at will at anytime:
1) PRNG mode, in which the generated sequence is required to be reproducible after reseeded by the same 384-bit value as supplied by guest software.
2) Test mode, in which the generated sequence is required to be reproducible ater reseeded by the same 128-bit test seed supplied by guest software.
3) TRNG mode, in which non-reproducible sequence is generated based on periodic reseed by a suitable entropy source.

This model is only intended for non-real world testing of guest software, where cryptographically strong PRNG or TRNG is not needed.

This model supports versions 1 & 2 of the device, with default to be version 2; the 'hw-version' uint32 property can be set to 0x0100 to override the default.

Other implemented properties:
- 'forced-prng', uint64
When set to non-zero, mode 3's entropy source is implemented as a deterministic sequence based on the given value and other deterministic parameters.
This option allows the emulation to test guest software using mode 3 and to reproduce data-dependent defects.

- 'fips-fault-events', uint32, bit-mask
bit 3: Triggers the SP800-90B entropy health test fault irq bit 1: Triggers the FIPS 140-2 continuous test fault irq

---
 hw/misc/Kconfig                    |   3 +
 hw/misc/meson.build                |   3 +
 hw/misc/xlnx-versal-trng.c         | 727 +++++++++++++++++++++++++++++
 include/hw/misc/xlnx-versal-trng.h |  58 +++
 4 files changed, 791 insertions(+)
 create mode 100644 hw/misc/xlnx-versal-trng.c
 create mode 100644 include/hw/misc/xlnx-versal-trng.h
hw/misc/Kconfig +3 -0
index dba41afe67..cc8a8c1418 100644
@@ -197,4 +197,7 @@ config DJMEMC
 config IOSB
     bool
 
+config XLNX_VERSAL_TRNG
+    bool
+
 source macio/Kconfig
hw/misc/meson.build +3 -0
index f60de33f9a..36c20d5637 100644
@@ -104,6 +104,9 @@ system_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files(
   'xlnx-cfi-if.c',
   'xlnx-versal-cframe-reg.c',
 ))
+system_ss.add(when: 'CONFIG_XLNX_VERSAL_TRNG', if_true: files(
+  'xlnx-versal-trng.c',
+))
 system_ss.add(when: 'CONFIG_STM32F2XX_SYSCFG', if_true: files('stm32f2xx_syscfg.c'))
 system_ss.add(when: 'CONFIG_STM32F4XX_SYSCFG', if_true: files('stm32f4xx_syscfg.c'))
 system_ss.add(when: 'CONFIG_STM32F4XX_EXTI', if_true: files('stm32f4xx_exti.c'))
hw/misc/xlnx-versal-trng.c new file +727 -0

727 lines not on this page · raw message

include/hw/misc/xlnx-versal-trng.h new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v4 1/3] hw/misc: Introduce AMD/Xilix Versal TRNG device

Quoted 40 lines · Tong Ho

This adds a non-cryptographic grade implementation of the model for the True Random Number Generator (TRNG) component in AMD/Xilinx Versal device family.

This implements all 3 modes defined by the actual hardware specs, all of which selectable by guest software at will at anytime:
1) PRNG mode, in which the generated sequence is required to be reproducible after reseeded by the same 384-bit value as supplied by guest software.
2) Test mode, in which the generated sequence is required to be reproducible ater reseeded by the same 128-bit test seed supplied by guest software.
3) TRNG mode, in which non-reproducible sequence is generated based on periodic reseed by a suitable entropy source.

This model is only intended for non-real world testing of guest software, where cryptographically strong PRNG or TRNG is not needed.

This model supports versions 1 & 2 of the device, with default to be version 2; the 'hw-version' uint32 property can be set to 0x0100 to override the default.

Other implemented properties:
- 'forced-prng', uint64
When set to non-zero, mode 3's entropy source is implemented as a deterministic sequence based on the given value and other deterministic parameters.
This option allows the emulation to test guest software using mode 3 and to reproduce data-dependent defects.

- 'fips-fault-events', uint32, bit-mask
bit 3: Triggers the SP800-90B entropy health test fault irq bit 1: Triggers the FIPS 140-2 continuous test fault irq

---

hw/misc/xlnx-versal-trng.c line 180

Quoted 6 lines
hw/misc/xlnx-versal-trng.c new file +6 -0
+static void trng_le128(uint32_t *le128, uint64_t h00, uint64_t h64)
+{
+    le128[0] = cpu_to_le32(h00 & 0xffffffffU);
+    le128[1] = cpu_to_le32(h00 >> 32);
+    le128[2] = cpu_to_le32(h64 & 0xffffffffU);
+    le128[3] = cpu_to_le32(h64 >> 32);

Why are we using cpu_to_le32() here ? We have a host-order pair of uint64_t values, and we want a set of host-order uint32_t values (because we're passing a guint32 array to the glib g_rand_set_seed_array() function). So I think we want

   seedarray[0] = extract64(h00, 0, 32);
   seedarray[1] = extract64(h00, 32, 32);
   seedarray[2] = extract64(h64, 0, 32);
   seedarray[3] = extract64(h64, 32, 32);

(I renamed the function argument because le128 is a red herring, and used extract64 because I find that clearer. The function itself could also use a different name now it isn't doing anything little-endian related.)

hw/misc/xlnx-versal-trng.c line 189

Quoted 9 lines
hw/misc/xlnx-versal-trng.c new file +9 -0
+}
+
+static void trng_le384(uint32_t *le384, const uint32_t *h384)
+{
+    size_t i;
+
+    for (i = 0; i < (384 / 32); i++) {
+        le384[i] = cpu_to_le32(h384[i]);
+    }

Similarly here we have an array of host-order 32-bit values (from the s->regs[] array) and we want an array of host-order 32-bit values for g_rand_set_seed_array(), so the cpu_to_le32() is unnecessary (and we're just doing a copy).

Quoted 39 lines
hw/misc/xlnx-versal-trng.c new file +39 -0
+}
+
+static void trng_reseed(XlnxVersalTRng *s)
+{
+    bool ext_seed = ARRAY_FIELD_EX32(s->regs, CTRL, PRNGXS);
+    bool pers_disabled = ARRAY_FIELD_EX32(s->regs, CTRL, PERSODISABLE);
+
+    enum {
+        U384_U32 = 384 / 32,
+    };
+
+    /*
+     * Maximum seed length is len(personalized string) + len(ext seed).
+     *
+     * Use little-endian to ensure guest sequence being indepedent of
+     * host endian.
+     */
+    guint32 gs[U384_U32 * 2], *seed = &gs[U384_U32];
+
+    /*
+     * A disabled personalized string is the same as
+     * a string with all zeros.
+     *
+     * The device's hardware spec defines 3 modes (all selectable
+     * by guest at will and at anytime):
+     * 1) External seeding
+     *    This is a PRNG mode, in which the produced sequence shall
+     *    be reproducible if reseeded by the same 384-bit seed, as
+     *    supplied by guest software.
+     * 2) Test seeding
+     *    This is a PRNG mode, in which the produced sequence shall
+     *    be reproducible if reseeded by a 128-bit test seed, as
+     *    supplied by guest software.
+     * 3) Truly-random seeding
+     *    This is the TRNG mode, in which the produced sequence is
+     *    periodically reseeded by a crypto-strength entropy source.
+     *
+     * To assist debugging of certain classes of software defects,
+     * this QEMU model implements a 4th mode,

hw/misc/xlnx-versal-trng.c line 238

Quoted 10 lines
hw/misc/xlnx-versal-trng.c new file +10 -0
+     * 4) Forced PRNG
+     *    When in this mode, a reproducible sequence is generated
+     *    if software has selected the TRNG mode (mode 2).
+     *
+     *    This emulation-only mode can only be selected by setting
+     *    the uint64 property 'forced-prng' to a non-zero value.
+     *    Guest software cannot select this mode.
+     */
+    memset(gs, 0, sizeof(gs));
+    stb_p((uint8_t *)gs + sizeof(gs) - 1, 1);

This looks like it gives different results on big and little endian. The gs[] array is an array of g_uint32, not an array of bytes. What's the intention behind setting this byte to 1, anyway?

Quoted 11 lines
hw/misc/xlnx-versal-trng.c new file +11 -0
+
+    if (!pers_disabled) {
+        trng_le384(gs, &s->regs[R_PER_STRNG_0]);
+    }
+
+    if (ext_seed) {
+        trng_le384(seed, &s->regs[R_EXT_SEED_0]);
+    } else if (trng_test_enabled(s)) {
+        trng_le128(seed, s->tst_seed[0], s->tst_seed[1]);
+    } else if (s->forced_prng_seed) {
+        s->forced_prng_count++;

hw/misc/xlnx-versal-trng.c line 259

Quoted 10 lines
hw/misc/xlnx-versal-trng.c new file +10 -0
+        trng_le128(seed, s->forced_prng_count, s->forced_prng_seed);
+    } else {
+        qemu_guest_getrandom_nofail(seed, U384_U32 * 4);
+    }
+
+    g_rand_set_seed_array(s->prng, gs, ARRAY_SIZE(gs));
+
+    s->rand_count = 0;
+    s->rand_reseed = 1ULL << 48;
+}

thanks
-- PMM

author
#3 lore raw

PATCH v4 2/3 hw/arm: xlnx-versal-virt: Add AMD/Xilinx TRNG device

In reply to Tong Ho

Connect the support for Versal True Random Number Generator (TRNG) device.

Warning: unlike the TRNG component in a real device from the Versal device familiy, the connected TRNG model is not of cryptographic grade and is not intended for use cases when cryptograpically strong TRNG is needed.

---
 hw/arm/Kconfig               |  1 +
 hw/arm/xlnx-versal.c         | 16 ++++++++++++++++
 include/hw/arm/xlnx-versal.h |  5 +++++
 3 files changed, 22 insertions(+)
hw/arm/Kconfig +1 -0
index 7e68348440..0a3ff6748d 100644
@@ -482,6 +482,7 @@ config XLNX_VERSAL
     select XLNX_BBRAM
     select XLNX_EFUSE_VERSAL
     select XLNX_USB_SUBSYS
+    select XLNX_VERSAL_TRNG
 
 config NPCM7XX
     bool
hw/arm/xlnx-versal.c +16 -0
index fa556d8764..4f74a64a0d 100644
@@ -373,6 +373,21 @@ static void versal_create_rtc(Versal *s, qemu_irq *pic)
                        qdev_get_gpio_in(DEVICE(&s->pmc.apb_irq_orgate), 0));
 }
 
+static void versal_create_trng(Versal *s, qemu_irq *pic)
+{
+    SysBusDevice *sbd;
+    MemoryRegion *mr;
+
+    object_initialize_child(OBJECT(s), "trng", &s->pmc.trng,
+                            TYPE_XLNX_VERSAL_TRNG);
+    sbd = SYS_BUS_DEVICE(&s->pmc.trng);
+    sysbus_realize(sbd, &error_fatal);
+
+    mr = sysbus_mmio_get_region(sbd, 0);
+    memory_region_add_subregion(&s->mr_ps, MM_PMC_TRNG, mr);
+    sysbus_connect_irq(sbd, 0, pic[VERSAL_TRNG_IRQ]);
+}
+
 static void versal_create_xrams(Versal *s, qemu_irq *pic)
 {
     int nr_xrams = ARRAY_SIZE(s->lpd.xram.ctrl);
@@ -909,6 +924,7 @@ static void versal_realize(DeviceState *dev, Error **errp)
     versal_create_sds(s, pic);
     versal_create_pmc_apb_irq_orgate(s, pic);
     versal_create_rtc(s, pic);
+    versal_create_trng(s, pic);
     versal_create_xrams(s, pic);
     versal_create_bbram(s, pic);
     versal_create_efuse(s, pic);
include/hw/arm/xlnx-versal.h +5 -0
index 7b419f88c2..54f4b98d9d 100644
@@ -31,6 +31,7 @@
 #include "hw/dma/xlnx_csu_dma.h"
 #include "hw/misc/xlnx-versal-crl.h"
 #include "hw/misc/xlnx-versal-pmc-iou-slcr.h"
+#include "hw/misc/xlnx-versal-trng.h"
 #include "hw/net/xlnx-versal-canfd.h"
 #include "hw/misc/xlnx-versal-cfu.h"
 #include "hw/misc/xlnx-versal-cframe-reg.h"
@@ -116,6 +117,7 @@ struct Versal {
         } iou;
 
         XlnxZynqMPRTC rtc;
+        XlnxVersalTRng trng;
         XlnxBBRam bbram;
         XlnxEFuse efuse;
         XlnxVersalEFuseCtrl efuse_ctrl;
@@ -160,6 +162,7 @@ struct Versal {
 #define VERSAL_OSPI_IRQ            124
 #define VERSAL_SD0_IRQ_0           126
 #define VERSAL_EFUSE_IRQ           139
+#define VERSAL_TRNG_IRQ            141
 #define VERSAL_RTC_ALARM_IRQ       142
 #define VERSAL_RTC_SECONDS_IRQ     143
 
@@ -329,4 +332,6 @@ struct Versal {
 #define MM_PMC_CRP_SIZE             0x10000
 #define MM_PMC_RTC                  0xf12a0000
 #define MM_PMC_RTC_SIZE             0x10000
+#define MM_PMC_TRNG                 0xf1230000
+#define MM_PMC_TRNG_SIZE            0x10000
 #endif
Signature · 1 line
2.25.1
Reviewed-by on v4 2/3
#4 lore raw

Re: [PATCH v4 2/3] hw/arm: xlnx-versal-virt: Add AMD/Xilinx TRNG device

Quoted 11 lines · Tong Ho

Connect the support for Versal True Random Number Generator (TRNG) device.

Warning: unlike the TRNG component in a real device from the Versal device familiy, the connected TRNG model is not of cryptographic grade and is not intended for use cases when cryptograpically strong TRNG is needed.

---

thanks
-- PMM

author
#5 lore raw

PATCH v4 3/3 tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device

In reply to Tong Ho
---
 tests/qtest/meson.build             |   2 +-
 tests/qtest/xlnx-versal-trng-test.c | 486 ++++++++++++++++++++++++++++
 2 files changed, 487 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/xlnx-versal-trng-test.c
tests/qtest/meson.build +1 -1
index 66795cfcd2..593ca6714b 100644
@@ -216,7 +216,7 @@ qtests_aarch64 = \
   (config_all.has_key('CONFIG_TCG') and config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ?            \
     ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) +                                         \
   (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \
-  (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \
   (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test'] : []) +  \
   (config_all.has_key('CONFIG_TCG') and                                            \
    config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \
tests/qtest/xlnx-versal-trng-test.c new file +486 -0

486 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v4 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device

tests/qtest/xlnx-versal-trng-test.c line 164

Quoted 12 lines · Tong Ho
---
 tests/qtest/meson.build             |   2 +-
 tests/qtest/xlnx-versal-trng-test.c | 486 ++++++++++++++++++++++++++++
 2 files changed, 487 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/xlnx-versal-trng-test.c
tests/qtest/xlnx-versal-trng-test.c new file +3 -0
+static void trng_wait(uint32_t wait_mask, bool on, const char *act)
+{
+    time_t tmo = time(NULL) + 2; /* at most 2 seconds */

Don't put timeouts this short into tests, please; ideally, avoid them entirely. Sometimes the CI machines are heavily loaded and the test might not be able to complete in a short time like this. If you must have a timeout, it should be at least a minute. (And see below on whether we need one.)

Quoted 23 lines
tests/qtest/xlnx-versal-trng-test.c new file +23 -0
+    uint32_t event_mask = 0;
+    uint32_t clear_mask = 0;
+
+    /*
+     * Only selected bits are events in R_TRNG_STATUS, and
+     * clear them needs to go through R_INT_CTRL.
+     */
+    if (wait_mask & TRNG_STATUS_CERTF_MASK) {
+        event_mask |= TRNG_STATUS_CERTF_MASK;
+        clear_mask |= TRNG_INT_CTRL_CERTF_RST_MASK;
+    }
+    if (wait_mask & TRNG_STATUS_DTF_MASK) {
+        event_mask |= TRNG_STATUS_DTF_MASK;
+        clear_mask |= TRNG_INT_CTRL_DTF_RST_MASK;
+    }
+    if (wait_mask & TRNG_STATUS_DONE_MASK) {
+        event_mask |= TRNG_STATUS_DONE_MASK;
+        clear_mask |= TRNG_INT_CTRL_DONE_RST_MASK;
+    }
+
+    for (;;) {
+        bool sta = !!(trng_status() & event_mask);
+

tests/qtest/xlnx-versal-trng-test.c line 197

Quoted 10 lines
tests/qtest/xlnx-versal-trng-test.c new file +10 -0
+        if ((on ^ sta) == 0) {
+            break;
+        }
+
+        if (time(NULL) >= tmo) {
+            FAILED("%s: Timed out waiting for event 0x%x to be %d%s",
+                   act, event_mask, (int)on, trng_info());
+        }
+
+        g_usleep(10000);

Why does this test need to use sleeps and timeouts? A qtest test controls the guest 'clock' directly, so usually they're completely deterministic. Is there some behaviour in the TRNG device which is asynchronous (in a way not driven from the QEMU guest clock) that I've missed ?

tests/qtest/xlnx-versal-trng-test.c line 207

Quoted 10 lines
tests/qtest/xlnx-versal-trng-test.c new file +10 -0
+    }
+
+    /* Remove event */
+    trng_bit_set(R_TRNG_INT_CTRL, clear_mask);
+
+    if (!!(trng_read(R_TRNG_STATUS) & event_mask)) {
+        FAILED("%s: Event 0x%0x stuck at 1 after clear: %s",
+               act, event_mask, trng_info());
+    }
+}

thanks
-- PMM

author
#7 lore raw

Re: [PATCH v4 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device

Hi Peter,

This is in regard to your comment on the use of g_usleep() in waiting for an event-bit update from the device under test.

The TRNG device model presented in patch #1 does not have any asynchronous behavior.

So, do you mean that, although the qtest process and the qemu-system-* process are running as 2 separate processes, qtest infrastructure already has the necessary mechanism to ensure that:

1. After qtest test sets a register that has the correct deterministic behavior to update an event bit,

2. The same qtest test subsequently issuing a register read will be guaranteed to observe the updated bit?

If that is the case, then there would be no need for any timeout. Instead, a qtest test can declare fail when the expected bit update is not observed by the test.

I would like to know.

Thanks,
Tong Ho

________________________________
From: Peter Maydell <[email protected]>
Sent: Friday, October 27, 2023 6:03 AM
To: Ho, Tong <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH v4 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device

tests/qtest/xlnx-versal-trng-test.c line 164

Quoted 12 lines · Tong Ho
---
 tests/qtest/meson.build             |   2 +-
 tests/qtest/xlnx-versal-trng-test.c | 486 ++++++++++++++++++++++++++++
 2 files changed, 487 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/xlnx-versal-trng-test.c
tests/qtest/xlnx-versal-trng-test.c new file +3 -0
+static void trng_wait(uint32_t wait_mask, bool on, const char *act)
+{
+    time_t tmo = time(NULL) + 2; /* at most 2 seconds */

Don't put timeouts this short into tests, please; ideally, avoid them entirely. Sometimes the CI machines are heavily loaded and the test might not be able to complete in a short time like this. If you must have a timeout, it should be at least a minute. (And see below on whether we need one.)

Quoted 23 lines
tests/qtest/xlnx-versal-trng-test.c new file +23 -0
+    uint32_t event_mask = 0;
+    uint32_t clear_mask = 0;
+
+    /*
+     * Only selected bits are events in R_TRNG_STATUS, and
+     * clear them needs to go through R_INT_CTRL.
+     */
+    if (wait_mask & TRNG_STATUS_CERTF_MASK) {
+        event_mask |= TRNG_STATUS_CERTF_MASK;
+        clear_mask |= TRNG_INT_CTRL_CERTF_RST_MASK;
+    }
+    if (wait_mask & TRNG_STATUS_DTF_MASK) {
+        event_mask |= TRNG_STATUS_DTF_MASK;
+        clear_mask |= TRNG_INT_CTRL_DTF_RST_MASK;
+    }
+    if (wait_mask & TRNG_STATUS_DONE_MASK) {
+        event_mask |= TRNG_STATUS_DONE_MASK;
+        clear_mask |= TRNG_INT_CTRL_DONE_RST_MASK;
+    }
+
+    for (;;) {
+        bool sta = !!(trng_status() & event_mask);
+

tests/qtest/xlnx-versal-trng-test.c line 197

Quoted 10 lines
tests/qtest/xlnx-versal-trng-test.c new file +10 -0
+        if ((on ^ sta) == 0) {
+            break;
+        }
+
+        if (time(NULL) >= tmo) {
+            FAILED("%s: Timed out waiting for event 0x%x to be %d%s",
+                   act, event_mask, (int)on, trng_info());
+        }
+
+        g_usleep(10000);

Why does this test need to use sleeps and timeouts? A qtest test controls the guest 'clock' directly, so usually they're completely deterministic. Is there some behaviour in the TRNG device which is asynchronous (in a way not driven from the QEMU guest clock) that I've missed ?

tests/qtest/xlnx-versal-trng-test.c line 207

Quoted 10 lines
tests/qtest/xlnx-versal-trng-test.c new file +10 -0
+    }
+
+    /* Remove event */
+    trng_bit_set(R_TRNG_INT_CTRL, clear_mask);
+
+    if (!!(trng_read(R_TRNG_STATUS) & event_mask)) {
+        FAILED("%s: Event 0x%0x stuck at 1 after clear: %s",
+               act, event_mask, trng_info());
+    }
+}

thanks
-- PMM

#8 lore raw

Re: [PATCH v4 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device

Quoted 8 lines · Tong

Hi Peter,

This is in regard to your comment on the use of g_usleep() in waiting for an event-bit update from the device under test.

The TRNG device model presented in patch #1 does not have any asynchronous behavior.

Quoted 7 lines

So, do you mean that, although the qtest process and the qemu-system-* process are running as 2 separate processes, qtest infrastructure already has the necessary mechanism to ensure that:

1. After qtest test sets a register that has the correct deterministic behavior to update an event bit,

2. The same qtest test subsequently issuing a register read will be guaranteed to observe the updated bit?

Yes. With a qtest test, there is no guest CPU inside QEMU. Instead when the test says "write a register" there's a protocol between the QEMU-under-test and the test process that says "write this value to this address". Time within the simulation only advances if the test specifically asks for it.

So if your device under test has the usual property of "we just immediately update the state on register write" you're guarenteed to see things on a subsequent read. If you are attempting a more exact simulation and do things like "when this register is written we set up a QEMU timer so that we only report the device ready after a certain time" then your test needs to manually advance the simulation clock with clock_step() to the point when the device would report ready. (Usually we don't care about that level of simulation accuracy, so clock_step() is used largely in tests of timer devices.) Either way, this is unrelated to wall-clock time.

thanks
-- PMM

#9 lore raw

Re: [PATCH v4 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device

In reply to Tong Ho
Quoted 27 lines · Tong Ho
---
 tests/qtest/meson.build             |   2 +-
 tests/qtest/xlnx-versal-trng-test.c | 486 ++++++++++++++++++++++++++++
 2 files changed, 487 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/xlnx-versal-trng-test.c
tests/qtest/meson.build +1 -1
index 66795cfcd2..593ca6714b 100644
@@ -216,7 +216,7 @@ qtests_aarch64 = \
   (config_all.has_key('CONFIG_TCG') and config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ?            \
     ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) +                                         \
   (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \
-  (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \
   (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test'] : []) +  \
   (config_all.has_key('CONFIG_TCG') and                                            \
    config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \
tests/qtest/xlnx-versal-trng-test.c new file +0 -0
new file mode 100644
index 0000000000..dc19c1e83b

tests/qtest/xlnx-versal-trng-test.c line 71

Quoted 10 lines
tests/qtest/xlnx-versal-trng-test.c new file +9 -0
@@ -0,0 +1,486 @@
+/*
+ * QTests for the Xilinx Versal True Random Number Generator device
+ *
+ * Copyright (c) 2023 Advanced Micro Devices, Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
 
+#define FAILED(FMT, ...) g_error("%s(): " FMT, __func__, ## __VA_ARGS__)

This fails to build for Windows because this macro name clashes with one from the system winerror.h:

../tests/qtest/xlnx-versal-trng-test.c:71: error: "FAILED" redefined [-Werror] 71 | #define FAILED(FMT, ...) g_error("%s(): " FMT, __func__, ## __VA_ARGS__)

Also, qtests should not be using g_error() to report errors, because this turns into a call to abort(), which the test harness will not report nicely as an error. Use g_assert_true() or one of the family of g_assert_cmp*() functions to test checks that you want to be test passes or failures (but *not* g_assert()).

thanks
-- PMM