2
0

bcm2835_rng.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * BCM2835 Random Number Generator emulation
  3. *
  4. * Copyright (C) 2017 Marcin Chojnacki <marcinch7@gmail.com>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/log.h"
  11. #include "qemu/guest-random.h"
  12. #include "qemu/module.h"
  13. #include "hw/misc/bcm2835_rng.h"
  14. #include "migration/vmstate.h"
  15. static uint32_t get_random_bytes(void)
  16. {
  17. uint32_t res;
  18. /*
  19. * On failure we don't want to return the guest a non-random
  20. * value in case they're really using it for cryptographic
  21. * purposes, so the best we can do is die here.
  22. * This shouldn't happen unless something's broken.
  23. * In theory we could implement this device's full FIFO
  24. * and interrupt semantics and then just stop filling the
  25. * FIFO. That's a lot of work, though, so we assume any
  26. * errors are systematic problems and trust that if we didn't
  27. * fail as the guest inited then we won't fail later on
  28. * mid-run.
  29. */
  30. qemu_guest_getrandom_nofail(&res, sizeof(res));
  31. return res;
  32. }
  33. static uint64_t bcm2835_rng_read(void *opaque, hwaddr offset,
  34. unsigned size)
  35. {
  36. BCM2835RngState *s = (BCM2835RngState *)opaque;
  37. uint32_t res = 0;
  38. assert(size == 4);
  39. switch (offset) {
  40. case 0x0: /* rng_ctrl */
  41. res = s->rng_ctrl;
  42. break;
  43. case 0x4: /* rng_status */
  44. res = s->rng_status | (1 << 24);
  45. break;
  46. case 0x8: /* rng_data */
  47. res = get_random_bytes();
  48. break;
  49. default:
  50. qemu_log_mask(LOG_GUEST_ERROR,
  51. "bcm2835_rng_read: Bad offset %x\n",
  52. (int)offset);
  53. res = 0;
  54. break;
  55. }
  56. return res;
  57. }
  58. static void bcm2835_rng_write(void *opaque, hwaddr offset,
  59. uint64_t value, unsigned size)
  60. {
  61. BCM2835RngState *s = (BCM2835RngState *)opaque;
  62. assert(size == 4);
  63. switch (offset) {
  64. case 0x0: /* rng_ctrl */
  65. s->rng_ctrl = value;
  66. break;
  67. case 0x4: /* rng_status */
  68. /* we shouldn't let the guest write to bits [31..20] */
  69. s->rng_status &= ~0xFFFFF; /* clear 20 lower bits */
  70. s->rng_status |= value & 0xFFFFF; /* set them to new value */
  71. break;
  72. default:
  73. qemu_log_mask(LOG_GUEST_ERROR,
  74. "bcm2835_rng_write: Bad offset %x\n",
  75. (int)offset);
  76. break;
  77. }
  78. }
  79. static const MemoryRegionOps bcm2835_rng_ops = {
  80. .read = bcm2835_rng_read,
  81. .write = bcm2835_rng_write,
  82. .endianness = DEVICE_NATIVE_ENDIAN,
  83. };
  84. static const VMStateDescription vmstate_bcm2835_rng = {
  85. .name = TYPE_BCM2835_RNG,
  86. .version_id = 1,
  87. .minimum_version_id = 1,
  88. .fields = (const VMStateField[]) {
  89. VMSTATE_UINT32(rng_ctrl, BCM2835RngState),
  90. VMSTATE_UINT32(rng_status, BCM2835RngState),
  91. VMSTATE_END_OF_LIST()
  92. }
  93. };
  94. static void bcm2835_rng_init(Object *obj)
  95. {
  96. BCM2835RngState *s = BCM2835_RNG(obj);
  97. memory_region_init_io(&s->iomem, obj, &bcm2835_rng_ops, s,
  98. TYPE_BCM2835_RNG, 0x10);
  99. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
  100. }
  101. static void bcm2835_rng_reset(DeviceState *dev)
  102. {
  103. BCM2835RngState *s = BCM2835_RNG(dev);
  104. s->rng_ctrl = 0;
  105. s->rng_status = 0;
  106. }
  107. static void bcm2835_rng_class_init(ObjectClass *klass, void *data)
  108. {
  109. DeviceClass *dc = DEVICE_CLASS(klass);
  110. device_class_set_legacy_reset(dc, bcm2835_rng_reset);
  111. dc->vmsd = &vmstate_bcm2835_rng;
  112. }
  113. static const TypeInfo bcm2835_rng_info = {
  114. .name = TYPE_BCM2835_RNG,
  115. .parent = TYPE_SYS_BUS_DEVICE,
  116. .instance_size = sizeof(BCM2835RngState),
  117. .class_init = bcm2835_rng_class_init,
  118. .instance_init = bcm2835_rng_init,
  119. };
  120. static void bcm2835_rng_register_types(void)
  121. {
  122. type_register_static(&bcm2835_rng_info);
  123. }
  124. type_init(bcm2835_rng_register_types)