Skip to contentSkip to conversation

AMD/Xilinx Versal TRNG support

PATCH Superseded Tong Ho 3 participants 10 messages Aug 31, 2023 – Sep 8, 2023 Arm/Amlogic Meson SoC support

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Tong Ho

author
#0 lore raw

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.

Best regards,
Tong Ho

References:
[1] https://docs.xilinx.com/r/en-US/am011-versal-acap-trm/True-Random-Number-Generator

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-virt.c           |  20 +
 hw/arm/xlnx-versal.c                |  16 +
 hw/misc/Kconfig                     |   3 +
 hw/misc/meson.build                 |   3 +
 hw/misc/xlnx-versal-trng.c          | 725 ++++++++++++++++++++++++++++
 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 | 490 +++++++++++++++++++
 10 files changed, 1322 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 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 model is only intended for non-real world testing of guest software, where cryptographically strong TRNG is not needed.

This model supports versions 1 & 2 of the Versal TRNG, 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, "true random reseed" is replaced by deterministic reseed based on the given value and other deterministic parameters, even when guest software has configured the TRNG as "true random reseed". This option allows guest software 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         | 725 +++++++++++++++++++++++++++++
 include/hw/misc/xlnx-versal-trng.h |  58 +++
 4 files changed, 789 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 6996d265e4..6b6105dcbf 100644
@@ -186,4 +186,7 @@ config AXP2XX_PMU
     bool
     depends on I2C
 
+config XLNX_VERSAL_TRNG
+    bool
+
 source macio/Kconfig
hw/misc/meson.build +3 -0
index d9a370c1de..2425187c51 100644
@@ -99,6 +99,9 @@ system_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files(
   'xlnx-versal-xramc.c',
   'xlnx-versal-pmc-iou-slcr.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 +725 -0

725 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 1/3] hw/misc: Introduce AMD/Xilix Versal TRNG device

Quoted 25 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 model is only intended for non-real world testing of guest software, where cryptographically strong TRNG is not needed.

This model supports versions 1 & 2 of the Versal TRNG, 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, "true random reseed" is replaced by deterministic reseed based on the given value and other deterministic parameters, even when guest software has configured the TRNG as "true random reseed". This option allows guest software 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

Quoted 8 lines
---
  hw/misc/Kconfig                    |   3 +
  hw/misc/meson.build                |   3 +
  hw/misc/xlnx-versal-trng.c         | 725 +++++++++++++++++++++++++++++
  include/hw/misc/xlnx-versal-trng.h |  58 +++
  4 files changed, 789 insertions(+)

create mode 100644 hw/misc/xlnx-versal-trng.c create mode 100644 include/hw/misc/xlnx-versal-trng.h

I don't think you should be inventing another PRNG, or related properties.

We already have qemu/guest-random.h, and the -seed command-line parameter to force the use of a deterministic PRNG with a given seed value.

r~

author
#3 lore raw

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

Hi Richard,

Thanks for your input.

I have questions regarding using qemu/guest-random.h for QEMU device models.

Using qemu/guest-random.h, how can this TRNG model ensure its independence from other uses of the same qemu_guest_getrandom() and qemu_guest_random_seed_*()?

By "other uses", I mean components and/or devices using qemu/guest-random.h but unrelated to this Xilinx Versal TRNG device.

By "independent", I mean the Xilinx Versal TRNG device is:

1. Not impacted by other uses that may or may not need to set the '-seed' option, and

2. Not impacting other uses just because a Xilinx Versal machine user decides to use deterministic mode *only" for this TRNG device.

Also, I am at a loss in how unrelated QEMU devices can remain independent when:

3. qemu/guest-random.h uses '__thread' variable for GRand context, but

4. QEMU devices run mostly as co-routines and not as separate threads.

I suppose the Versal TRNG implementation could use g_rand_*() directly, having a GRand object in the device state and seeding through g_rand_set_seed_array().

Best regards,
Tong Ho

-----Original Message-----
From: Richard Henderson <[email protected]>
Sent: Thursday, August 31, 2023 4:45 PM
To: Ho, Tong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/3] hw/misc: Introduce AMD/Xilix Versal TRNG device

Quoted 24 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 model is only intended for non-real world testing of guest software, where cryptographically strong TRNG is not needed.

This model supports versions 1 & 2 of the Versal TRNG, 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, "true random reseed" is replaced by deterministic reseed based on the given value and other deterministic parameters, even when guest software has configured the TRNG as "true random reseed". This option allows guest software 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

Quoted 8 lines
---
  hw/misc/Kconfig                    |   3 +
  hw/misc/meson.build                |   3 +
  hw/misc/xlnx-versal-trng.c         | 725 +++++++++++++++++++++++++++++
  include/hw/misc/xlnx-versal-trng.h |  58 +++
  4 files changed, 789 insertions(+)

create mode 100644 hw/misc/xlnx-versal-trng.c create mode 100644 include/hw/misc/xlnx-versal-trng.h

I don't think you should be inventing another PRNG, or related properties.

We already have qemu/guest-random.h, and the -seed command-line parameter to force the use of a deterministic PRNG with a given seed value.

r~

#4 lore raw

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

Quoted 16 lines · Tong

Hi Richard,

Thanks for your input.

I have questions regarding using qemu/guest-random.h for QEMU device models.

Using qemu/guest-random.h, how can this TRNG model ensure its independence from other uses of the same qemu_guest_getrandom() and qemu_guest_random_seed_*()?

By "other uses", I mean components and/or devices using qemu/guest-random.h but unrelated to this Xilinx Versal TRNG device.

By "independent", I mean the Xilinx Versal TRNG device is:

1. Not impacted by other uses that may or may not need to set the '-seed' option, and

Quoted 7 lines

2. Not impacting other uses just because a Xilinx Versal machine user decides to use deterministic mode *only" for this TRNG device.

Also, I am at a loss in how unrelated QEMU devices can remain independent when:

3. qemu/guest-random.h uses '__thread' variable for GRand context, but

4. QEMU devices run mostly as co-routines and not as separate threads.

You shouldn't need to care about any of this. Just assume you can get decent quality random numbers from qemu_guest_getrandom() or qemu_guest_getrandom_nofail(). The -seed option is for the entire simulation, not specific to individual RNG devices.

Quoted 2 lines

I suppose the Versal TRNG implementation could use g_rand_*() directly, having a GRand object in the device state and seeding through g_rand_set_seed_array().

Don't do something non-standard. Write this RNG device the same way we do all other RNG devices in QEMU.

thanks
-- PMM

author
#5 lore raw

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

Hi Peter,

The Versal TRNG device is required to support both TRNG mode and PRNG mode, and target/guest software selects and changes the mode at will during runtime.

I do agree that, in the TRNG mode, the model using qemu_guest_getrandom() will work without any issues.

When software selects the PRNG mode, the Versal TRNG device is expected to output a reproducible and deterministic sequence of values for a given seed. This is part of the hardware spec.

I fail to see how qemu_guest_getrandom() can be used to model such requirement correctly.

Regards,
Tong Ho

-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Friday, September 8, 2023 6:50 AM
To: Ho, Tong <[email protected]>
Cc: Richard Henderson <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/3] hw/misc: Introduce AMD/Xilix Versal TRNG device

Quoted 17 lines · Tong

Hi Richard,

Thanks for your input.

I have questions regarding using qemu/guest-random.h for QEMU device models.

Using qemu/guest-random.h, how can this TRNG model ensure its
independence from other uses of the same qemu_guest_getrandom() and qemu_guest_random_seed_*()?

By "other uses", I mean components and/or devices using qemu/guest-random.h but unrelated to this Xilinx Versal TRNG device.

By "independent", I mean the Xilinx Versal TRNG device is:

1. Not impacted by other uses that may or may not need to set the '-seed' option, and

Quoted 7 lines

2. Not impacting other uses just because a Xilinx Versal machine user decides to use deterministic mode *only" for this TRNG device.

Also, I am at a loss in how unrelated QEMU devices can remain independent when:

3. qemu/guest-random.h uses '__thread' variable for GRand context, but

4. QEMU devices run mostly as co-routines and not as separate threads.

You shouldn't need to care about any of this. Just assume you can get decent quality random numbers from qemu_guest_getrandom() or qemu_guest_getrandom_nofail(). The -seed option is for the entire simulation, not specific to individual RNG devices.

Quoted 2 lines

I suppose the Versal TRNG implementation could use g_rand_*()
directly, having a GRand object in the device state and seeding through g_rand_set_seed_array().

Don't do something non-standard. Write this RNG device the same way we do all other RNG devices in QEMU.

thanks
-- PMM

#6 lore raw

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

Quoted 6 lines · Tong

Hi Peter,

The Versal TRNG device is required to support both TRNG mode and PRNG mode, and target/guest software selects and changes the mode at will during runtime.

Quoted 6 lines

I do agree that, in the TRNG mode, the model using qemu_guest_getrandom() will work without any issues.

When software selects the PRNG mode, the Versal TRNG device is expected to output a reproducible and deterministic sequence of values for a given seed. This is part of the hardware spec.

I fail to see how qemu_guest_getrandom() can be used to model such requirement correctly.

If the hardware documents a specific RNG that it must use, then yes, we should model that, and the comments need to make it clear that we're modelling a very specific thing, not merely "here is an arbitrary PRNG".

-- PMM

author
#7 lore raw

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

Hi Peter,

Your recommendation is noted. Thanks for your valuable input. I will present V2 with better alignment.

Regards,
Tong Ho

-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Friday, September 8, 2023 11:35 AM
To: Ho, Tong <[email protected]>
Cc: Richard Henderson <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/3] hw/misc: Introduce AMD/Xilix Versal TRNG device

Quoted 6 lines · Tong

Hi Peter,

The Versal TRNG device is required to support both TRNG mode and PRNG mode, and target/guest software selects and changes the mode at will during runtime.

Quoted 6 lines

I do agree that, in the TRNG mode, the model using qemu_guest_getrandom() will work without any issues.

When software selects the PRNG mode, the Versal TRNG device is
expected to output a reproducible and deterministic sequence of values for a given seed. This is part of the hardware spec.

I fail to see how qemu_guest_getrandom() can be used to model such requirement correctly.

If the hardware documents a specific RNG that it must use, then yes, we should model that, and the comments need to make it clear that we're modelling a very specific thing, not merely "here is an arbitrary PRNG".

-- PMM

author
#8 lore raw

PATCH 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-virt.c    | 20 ++++++++++++++++++++
 hw/arm/xlnx-versal.c         | 16 ++++++++++++++++
 include/hw/arm/xlnx-versal.h |  5 +++++
 4 files changed, 42 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-virt.c +20 -0
index 1ee2b8697f..c0704f416e 100644
@@ -391,6 +391,25 @@ static void fdt_add_rtc_node(VersalVirt *s)
     g_free(name);
 }
 
+static void fdt_add_trng_node(VersalVirt *s)
+{
+    const char compat[] = TYPE_XLNX_VERSAL_TRNG;
+    const char interrupt_names[] = "trng";
+    g_autofree char *name = g_strdup_printf("/trng@%x", MM_PMC_TRNG);
+
+    qemu_fdt_add_subnode(s->fdt, name);
+
+    qemu_fdt_setprop_cells(s->fdt, name, "interrupts",
+                           GIC_FDT_IRQ_TYPE_SPI, VERSAL_TRNG_IRQ,
+                           GIC_FDT_IRQ_FLAGS_LEVEL_HI);
+    qemu_fdt_setprop(s->fdt, name, "interrupt-names",
+                     interrupt_names, sizeof(interrupt_names));
+    qemu_fdt_setprop_sized_cells(s->fdt, name, "reg",
+                                 2, MM_PMC_TRNG,
+                                 2, MM_PMC_TRNG_SIZE);
+    qemu_fdt_setprop(s->fdt, name, "compatible", compat, sizeof(compat));
+}
+
 static void fdt_add_bbram_node(VersalVirt *s)
 {
     const char compat[] = TYPE_XLNX_BBRAM;
@@ -690,6 +709,7 @@ static void versal_virt_init(MachineState *machine)
     fdt_add_usb_xhci_nodes(s);
     fdt_add_sd_nodes(s);
     fdt_add_rtc_node(s);
+    fdt_add_trng_node(s);
     fdt_add_bbram_node(s);
     fdt_add_efuse_ctrl_node(s);
     fdt_add_efuse_cache_node(s);
hw/arm/xlnx-versal.c +16 -0
index 60bf5fe657..4fd1924cf6 100644
@@ -372,6 +372,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);
@@ -757,6 +772,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 39ee31185c..159ee26aad 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"
 
 #define TYPE_XLNX_VERSAL "xlnx-versal"
@@ -113,6 +114,7 @@ struct Versal {
         } iou;
 
         XlnxZynqMPRTC rtc;
+        XlnxVersalTRng trng;
         XlnxBBRam bbram;
         XlnxEFuse efuse;
         XlnxVersalEFuseCtrl efuse_ctrl;
@@ -151,6 +153,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
 
@@ -244,4 +247,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
author
#9 lore raw

PATCH 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 | 490 ++++++++++++++++++++++++++++
 2 files changed, 491 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/xlnx-versal-trng-test.c
tests/qtest/meson.build +1 -1
index b071d400b3..af060d9e5b 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 +490 -0

490 lines not on this page · raw message

Signature · 1 line
2.25.1