Skip to contentSkip to conversation

aspeed_scu: Implement RNG register

PATCH Superseded Joel Stanley 2 participants 5 messages May 28, 2018 Arm/Aspeed machine support

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Joel Stanley

author maintainer
#0 lore raw

PATCH 1/1 aspeed_scu: Implement RNG register

The ASPEED SoCs contain a single register that returns random data when read. This models that register so that guests can use it.

---
 hw/misc/aspeed_scu.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
hw/misc/aspeed_scu.c +19 -0
index 5e6d5744eeca..8fa0cecf0fa1 100644
@@ -16,6 +16,7 @@
 #include "qapi/visitor.h"
 #include "qemu/bitops.h"
 #include "qemu/log.h"
+#include "crypto/random.h"
 #include "trace.h"
 
 #define TO_REG(offset) ((offset) >> 2)
@@ -154,6 +155,18 @@ static const uint32_t ast2500_a1_resets[ASPEED_SCU_NR_REGS] = {
      [BMC_DEV_ID]      = 0x00002402U
 };
 
+static uint32_t aspeed_scu_get_random(void)
+{
+    Error *err = NULL;
+    uint32_t num;
+
+    if (qcrypto_random_bytes((uint8_t *)&num, sizeof(num), &err)) {
+        error_report_err(err);
+    }
+
+    return num;
+}
+
 static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
 {
     AspeedSCUState *s = ASPEED_SCU(opaque);
@@ -167,6 +180,9 @@ static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
     }
 
     switch (reg) {
+    case RNG_DATA:
+        return aspeed_scu_get_random();
+        break;
     case WAKEUP_EN:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Read of write-only offset 0x%" HWADDR_PRIx "\n",
@@ -287,6 +303,9 @@ static void aspeed_scu_realize(DeviceState *dev, Error **errp)
                           TYPE_ASPEED_SCU, SCU_IO_REGION_SIZE);
 
     sysbus_init_mmio(sbd, &s->iomem);
+
+    if (qcrypto_random_init(errp))
+        return;
 }
 
 static const VMStateDescription vmstate_aspeed_scu = {
Signature · 1 line
2.17.0
#1 lore raw

Hello Joel,

Quoted 40 lines · Joel Stanley

The ASPEED SoCs contain a single register that returns random data when read. This models that register so that guests can use it.

---
 hw/misc/aspeed_scu.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
hw/misc/aspeed_scu.c +13 -0
index 5e6d5744eeca..8fa0cecf0fa1 100644
@@ -16,6 +16,7 @@
 #include "qapi/visitor.h"
 #include "qemu/bitops.h"
 #include "qemu/log.h"
+#include "crypto/random.h"
 #include "trace.h"
 
 #define TO_REG(offset) ((offset) >> 2)
@@ -154,6 +155,18 @@ static const uint32_t ast2500_a1_resets[ASPEED_SCU_NR_REGS] = {
      [BMC_DEV_ID]      = 0x00002402U
 };
 
+static uint32_t aspeed_scu_get_random(void)
+{
+    Error *err = NULL;
+    uint32_t num;
+
+    if (qcrypto_random_bytes((uint8_t *)&num, sizeof(num), &err)) {
+        error_report_err(err);
+    }
+
+    return num;
+}
+
 static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
 {
     AspeedSCUState *s = ASPEED_SCU(opaque);

hw/misc/aspeed_scu.c static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size) · line 184

Quoted 5 lines
hw/misc/aspeed_scu.c +2 -0
@@ -167,6 +180,9 @@ static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
     }
 
     switch (reg) {
+    case RNG_DATA:
+        return aspeed_scu_get_random();

may be we could test bit 1 of RNG_CTRL to check if it is enabled or not.

hw/misc/aspeed_scu.c static void aspeed_scu_realize(DeviceState *dev, Error **errp) · line 309

Quoted 12 lines
hw/misc/aspeed_scu.c +4 -0
+        break;
     case WAKEUP_EN:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Read of write-only offset 0x%" HWADDR_PRIx "\n",
@@ -287,6 +303,9 @@ static void aspeed_scu_realize(DeviceState *dev, Error **errp)
                           TYPE_ASPEED_SCU, SCU_IO_REGION_SIZE);
 
     sysbus_init_mmio(sbd, &s->iomem);
+
+    if (qcrypto_random_init(errp))
+        return;
 }

Isn't this routine called from main() already ?

C.

Quoted 3 lines
hw/misc/aspeed_scu.c +0 -0
 static const VMStateDescription vmstate_aspeed_scu = {
 
author maintainer
#2 lore raw
Quoted 45 lines · Cédric Le Goater

Hello Joel,

Quoted 42 lines · Joel Stanley

The ASPEED SoCs contain a single register that returns random data when read. This models that register so that guests can use it.

---
 hw/misc/aspeed_scu.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
hw/misc/aspeed_scu.c +13 -0
index 5e6d5744eeca..8fa0cecf0fa1 100644
@@ -16,6 +16,7 @@
 #include "qapi/visitor.h"
 #include "qemu/bitops.h"
 #include "qemu/log.h"
+#include "crypto/random.h"
 #include "trace.h"
 
 #define TO_REG(offset) ((offset) >> 2)
@@ -154,6 +155,18 @@ static const uint32_t ast2500_a1_resets[ASPEED_SCU_NR_REGS] = {
      [BMC_DEV_ID]      = 0x00002402U
 };
 
+static uint32_t aspeed_scu_get_random(void)
+{
+    Error *err = NULL;
+    uint32_t num;
+
+    if (qcrypto_random_bytes((uint8_t *)&num, sizeof(num), &err)) {
+        error_report_err(err);
+    }
+
+    return num;
+}
+
 static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
 {
     AspeedSCUState *s = ASPEED_SCU(opaque);
@@ -167,6 +180,9 @@ static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
     }
 
Quoted 5 lines

hw/misc/aspeed_scu.c static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size) · line 184

Quoted 3 lines
hw/misc/aspeed_scu.c +2 -0
     switch (reg) {
+    case RNG_DATA:
+        return aspeed_scu_get_random();

may be we could test bit 1 of RNG_CTRL to check if it is enabled or not.

The RNG is enabled by default, and I didn't find any software that disables it, but it can't hurt to have that check.

I'll send a v2 with that check.

Quoted 8 lines
Quoted 8 lines
hw/misc/aspeed_scu.c +1 -0
+        break;
     case WAKEUP_EN:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Read of write-only offset 0x%" HWADDR_PRIx "\n",
@@ -287,6 +303,9 @@ static void aspeed_scu_realize(DeviceState *dev, Error **errp)
                           TYPE_ASPEED_SCU, SCU_IO_REGION_SIZE);
 
Quoted 7 lines

hw/misc/aspeed_scu.c static void aspeed_scu_realize(DeviceState *dev, Error **errp) · line 309

Quoted 5 lines
hw/misc/aspeed_scu.c +3 -0
     sysbus_init_mmio(sbd, &s->iomem);
+
+    if (qcrypto_random_init(errp))
+        return;
 }

Isn't this routine called from main() already ?

It is indirectly called, yes. I'll remove this.

Cheers,

Joel

author maintainer
#3 lore raw
Quoted 46 lines · Joel Stanley
Quoted 45 lines · Cédric Le Goater

Hello Joel,

Quoted 42 lines · Joel Stanley

The ASPEED SoCs contain a single register that returns random data when read. This models that register so that guests can use it.

---
 hw/misc/aspeed_scu.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
hw/misc/aspeed_scu.c +13 -0
index 5e6d5744eeca..8fa0cecf0fa1 100644
@@ -16,6 +16,7 @@
 #include "qapi/visitor.h"
 #include "qemu/bitops.h"
 #include "qemu/log.h"
+#include "crypto/random.h"
 #include "trace.h"
 
 #define TO_REG(offset) ((offset) >> 2)
@@ -154,6 +155,18 @@ static const uint32_t ast2500_a1_resets[ASPEED_SCU_NR_REGS] = {
      [BMC_DEV_ID]      = 0x00002402U
 };
 
+static uint32_t aspeed_scu_get_random(void)
+{
+    Error *err = NULL;
+    uint32_t num;
+
+    if (qcrypto_random_bytes((uint8_t *)&num, sizeof(num), &err)) {
+        error_report_err(err);
+    }
+
+    return num;
+}
+
 static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
 {
     AspeedSCUState *s = ASPEED_SCU(opaque);
@@ -167,6 +180,9 @@ static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
     }
 
Quoted 8 lines
Quoted 5 lines

hw/misc/aspeed_scu.c static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size) · line 184

Quoted 3 lines
hw/misc/aspeed_scu.c +2 -0
     switch (reg) {
+    case RNG_DATA:
+        return aspeed_scu_get_random();

may be we could test bit 1 of RNG_CTRL to check if it is enabled or not.

The RNG is enabled by default, and I didn't find any software that disables it, but it can't hurt to have that check.

I did some testing on hardware, and the rng still outputs a different number each time I ask for one regardless of the state of the enabled bit.

How should we model that?

#4 lore raw
Quoted 53 lines · Joel Stanley
Quoted 52 lines · Joel Stanley
Quoted 50 lines · Cédric Le Goater

Hello Joel,

Quoted 40 lines · Joel Stanley

The ASPEED SoCs contain a single register that returns random data when read. This models that register so that guests can use it.

---
 hw/misc/aspeed_scu.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
hw/misc/aspeed_scu.c +13 -0
index 5e6d5744eeca..8fa0cecf0fa1 100644
@@ -16,6 +16,7 @@
 #include "qapi/visitor.h"
 #include "qemu/bitops.h"
 #include "qemu/log.h"
+#include "crypto/random.h"
 #include "trace.h"
 
 #define TO_REG(offset) ((offset) >> 2)
@@ -154,6 +155,18 @@ static const uint32_t ast2500_a1_resets[ASPEED_SCU_NR_REGS] = {
      [BMC_DEV_ID]      = 0x00002402U
 };
 
+static uint32_t aspeed_scu_get_random(void)
+{
+    Error *err = NULL;
+    uint32_t num;
+
+    if (qcrypto_random_bytes((uint8_t *)&num, sizeof(num), &err)) {
+        error_report_err(err);
+    }
+
+    return num;
+}
+
 static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
 {
     AspeedSCUState *s = ASPEED_SCU(opaque);

hw/misc/aspeed_scu.c static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size) · line 184

Quoted 5 lines
hw/misc/aspeed_scu.c +2 -0
@@ -167,6 +180,9 @@ static uint64_t aspeed_scu_read(void *opaque, hwaddr offset, unsigned size)
     }
 
     switch (reg) {
+    case RNG_DATA:
+        return aspeed_scu_get_random();

may be we could test bit 1 of RNG_CTRL to check if it is enabled or not.

Quoted 9 lines
Quoted 2 lines

The RNG is enabled by default, and I didn't find any software that disables it, but it can't hurt to have that check.

I did some testing on hardware, and the rng still outputs a different number each time I ask for one regardless of the state of the enabled bit.

How should we model that?

I confirm that the HW doesn't really care about the enabled bit. Let's ignore it then ?

C.